hlink/tests: Sign compare fix.
[wine] / dlls / ole32 / tests / compobj.c
1 /*
2  * Component Object Tests
3  *
4  * Copyright 2005 Robert Shearman
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #define COBJMACROS
22 #define CONST_VTABLE
23
24 #include <stdarg.h>
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "objbase.h"
29 #include "shlguid.h"
30 #include "urlmon.h" /* for CLSID_FileProtocol */
31
32 #include "initguid.h"
33 #include "ctxtcall.h"
34
35 #include "wine/test.h"
36
37 /* functions that are not present on all versions of Windows */
38 HRESULT (WINAPI * pCoInitializeEx)(LPVOID lpReserved, DWORD dwCoInit);
39 HRESULT (WINAPI * pCoGetObjectContext)(REFIID riid, LPVOID *ppv);
40 HRESULT (WINAPI * pCoSwitchCallContext)(IUnknown *pObject, IUnknown **ppOldObject);
41 HRESULT (WINAPI * pCoGetTreatAsClass)(REFCLSID clsidOld, LPCLSID pClsidNew);
42
43 #define ok_ole_success(hr, func) ok(hr == S_OK, func " failed with error 0x%08x\n", hr)
44 #define ok_more_than_one_lock() ok(cLocks > 0, "Number of locks should be > 0, but actually is %d\n", cLocks)
45 #define ok_no_locks() ok(cLocks == 0, "Number of locks should be 0, but actually is %d\n", cLocks)
46
47 static const CLSID CLSID_non_existent =   { 0x12345678, 0x1234, 0x1234, { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 } };
48 static const CLSID CLSID_StdFont = { 0x0be35203, 0x8f91, 0x11ce, { 0x9d, 0xe3, 0x00, 0xaa, 0x00, 0x4b, 0xb8, 0x51 } };
49 static WCHAR stdfont[] = {'S','t','d','F','o','n','t',0};
50 static const WCHAR wszNonExistent[] = {'N','o','n','E','x','i','s','t','e','n','t',0};
51 static WCHAR wszCLSID_StdFont[] =
52 {
53     '{','0','b','e','3','5','2','0','3','-','8','f','9','1','-','1','1','c','e','-',
54     '9','d','e','3','-','0','0','a','a','0','0','4','b','b','8','5','1','}',0
55 };
56
57 static const IID IID_IWineTest =
58 {
59     0x5201163f,
60     0x8164,
61     0x4fd0,
62     {0xa1, 0xa2, 0x5d, 0x5a, 0x36, 0x54, 0xd3, 0xbd}
63 }; /* 5201163f-8164-4fd0-a1a2-5d5a3654d3bd */
64 static const CLSID CLSID_WineOOPTest = {
65     0x5201163f,
66     0x8164,
67     0x4fd0,
68     {0xa1, 0xa2, 0x5d, 0x5a, 0x36, 0x54, 0xd3, 0xbd}
69 }; /* 5201163f-8164-4fd0-a1a2-5d5a3654d3bd */
70
71 static LONG cLocks;
72
73 static void LockModule(void)
74 {
75     InterlockedIncrement(&cLocks);
76 }
77
78 static void UnlockModule(void)
79 {
80     InterlockedDecrement(&cLocks);
81 }
82
83 static HRESULT WINAPI Test_IClassFactory_QueryInterface(
84     LPCLASSFACTORY iface,
85     REFIID riid,
86     LPVOID *ppvObj)
87 {
88     if (ppvObj == NULL) return E_POINTER;
89
90     if (IsEqualGUID(riid, &IID_IUnknown) ||
91         IsEqualGUID(riid, &IID_IClassFactory))
92     {
93         *ppvObj = iface;
94         IClassFactory_AddRef(iface);
95         return S_OK;
96     }
97
98     *ppvObj = NULL;
99     return E_NOINTERFACE;
100 }
101
102 static ULONG WINAPI Test_IClassFactory_AddRef(LPCLASSFACTORY iface)
103 {
104     LockModule();
105     return 2; /* non-heap-based object */
106 }
107
108 static ULONG WINAPI Test_IClassFactory_Release(LPCLASSFACTORY iface)
109 {
110     UnlockModule();
111     return 1; /* non-heap-based object */
112 }
113
114 static HRESULT WINAPI Test_IClassFactory_CreateInstance(
115     LPCLASSFACTORY iface,
116     LPUNKNOWN pUnkOuter,
117     REFIID riid,
118     LPVOID *ppvObj)
119 {
120     *ppvObj = NULL;
121     if (pUnkOuter) return CLASS_E_NOAGGREGATION;
122     return E_NOINTERFACE;
123 }
124
125 static HRESULT WINAPI Test_IClassFactory_LockServer(
126     LPCLASSFACTORY iface,
127     BOOL fLock)
128 {
129     return S_OK;
130 }
131
132 static const IClassFactoryVtbl TestClassFactory_Vtbl =
133 {
134     Test_IClassFactory_QueryInterface,
135     Test_IClassFactory_AddRef,
136     Test_IClassFactory_Release,
137     Test_IClassFactory_CreateInstance,
138     Test_IClassFactory_LockServer
139 };
140
141 static IClassFactory Test_ClassFactory = { &TestClassFactory_Vtbl };
142
143 static void test_ProgIDFromCLSID(void)
144 {
145     LPWSTR progid;
146     HRESULT hr = ProgIDFromCLSID(&CLSID_StdFont, &progid);
147     ok(hr == S_OK, "ProgIDFromCLSID failed with error 0x%08x\n", hr);
148     if (hr == S_OK)
149     {
150         ok(!lstrcmpiW(progid, stdfont), "Didn't get expected prog ID\n");
151         CoTaskMemFree(progid);
152     }
153
154     progid = (LPWSTR)0xdeadbeef;
155     hr = ProgIDFromCLSID(&CLSID_non_existent, &progid);
156     ok(hr == REGDB_E_CLASSNOTREG, "ProgIDFromCLSID returned %08x\n", hr);
157     ok(progid == NULL, "ProgIDFromCLSID returns with progid %p\n", progid);
158
159     hr = ProgIDFromCLSID(&CLSID_StdFont, NULL);
160     ok(hr == E_INVALIDARG, "ProgIDFromCLSID should return E_INVALIDARG instead of 0x%08x\n", hr);
161 }
162
163 static void test_CLSIDFromProgID(void)
164 {
165     CLSID clsid;
166     HRESULT hr = CLSIDFromProgID(stdfont, &clsid);
167     ok(hr == S_OK, "CLSIDFromProgID failed with error 0x%08x\n", hr);
168     ok(IsEqualCLSID(&clsid, &CLSID_StdFont), "clsid wasn't equal to CLSID_StdFont\n");
169
170     hr = CLSIDFromString(stdfont, &clsid);
171     ok_ole_success(hr, "CLSIDFromString");
172     ok(IsEqualCLSID(&clsid, &CLSID_StdFont), "clsid wasn't equal to CLSID_StdFont\n");
173
174     /* test some failure cases */
175
176     hr = CLSIDFromProgID(wszNonExistent, NULL);
177     ok(hr == E_INVALIDARG, "CLSIDFromProgID should have returned E_INVALIDARG instead of 0x%08x\n", hr);
178
179     hr = CLSIDFromProgID(NULL, &clsid);
180     ok(hr == E_INVALIDARG, "CLSIDFromProgID should have returned E_INVALIDARG instead of 0x%08x\n", hr);
181
182     memset(&clsid, 0xcc, sizeof(clsid));
183     hr = CLSIDFromProgID(wszNonExistent, &clsid);
184     ok(hr == CO_E_CLASSSTRING, "CLSIDFromProgID on nonexistent ProgID should have returned CO_E_CLASSSTRING instead of 0x%08x\n", hr);
185     ok(IsEqualCLSID(&clsid, &CLSID_NULL), "CLSIDFromProgID should have set clsid to all-zeros on failure\n");
186 }
187
188 static void test_CLSIDFromString(void)
189 {
190     CLSID clsid;
191     HRESULT hr = CLSIDFromString(wszCLSID_StdFont, &clsid);
192     ok_ole_success(hr, "CLSIDFromString");
193     ok(IsEqualCLSID(&clsid, &CLSID_StdFont), "clsid wasn't equal to CLSID_StdFont\n");
194
195     hr = CLSIDFromString(NULL, &clsid);
196     ok_ole_success(hr, "CLSIDFromString");
197     ok(IsEqualCLSID(&clsid, &CLSID_NULL), "clsid wasn't equal to CLSID_NULL\n");
198 }
199
200 static void test_StringFromGUID2(void)
201 {
202   WCHAR str[50];
203   int len;
204   /* Test corner cases for buffer size */
205   len = StringFromGUID2(&CLSID_StdFont,str,50);
206   ok(len == 39, "len: %d (expected 39)\n", len);
207   ok(!lstrcmpiW(str, wszCLSID_StdFont),"string wasn't equal for CLSID_StdFont\n");
208
209   memset(str,0,sizeof str);
210   len = StringFromGUID2(&CLSID_StdFont,str,39);
211   ok(len == 39, "len: %d (expected 39)\n", len);
212   ok(!lstrcmpiW(str, wszCLSID_StdFont),"string wasn't equal for CLSID_StdFont\n");
213
214   len = StringFromGUID2(&CLSID_StdFont,str,38);
215   ok(len == 0, "len: %d (expected 0)\n", len);
216
217   len = StringFromGUID2(&CLSID_StdFont,str,30);
218   ok(len == 0, "len: %d (expected 0)\n", len);
219 }
220
221 struct info
222 {
223     HANDLE wait, stop;
224 };
225
226 static DWORD CALLBACK ole_initialize_thread(LPVOID pv)
227 {
228     HRESULT hr;
229     struct info *info = pv;
230
231     hr = pCoInitializeEx(NULL, COINIT_MULTITHREADED);
232
233     SetEvent(info->wait);
234     WaitForSingleObject(info->stop, INFINITE);
235
236     CoUninitialize();
237     return hr;
238 }
239
240 static void test_CoCreateInstance(void)
241 {
242     HRESULT hr;
243     HANDLE thread;
244     DWORD tid, exitcode;
245     IUnknown *pUnk;
246     struct info info;
247     REFCLSID rclsid = &CLSID_InternetZoneManager;
248
249     pUnk = (IUnknown *)0xdeadbeef;
250     hr = CoCreateInstance(rclsid, NULL, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void **)&pUnk);
251     ok(hr == CO_E_NOTINITIALIZED, "CoCreateInstance should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n", hr);
252     ok(pUnk == NULL, "CoCreateInstance should have changed the passed in pointer to NULL, instead of %p\n", pUnk);
253
254     OleInitialize(NULL);
255     hr = CoCreateInstance(rclsid, NULL, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void **)&pUnk);
256     ok_ole_success(hr, "CoCreateInstance");
257     if(pUnk) IUnknown_Release(pUnk);
258     OleUninitialize();
259
260     hr = CoCreateInstance(rclsid, NULL, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void **)&pUnk);
261     ok(hr == CO_E_NOTINITIALIZED, "CoCreateInstance should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n", hr);
262
263     /* show that COM doesn't have to be initialized for multi-threaded apartments if another
264        thread has already done so */
265
266     info.wait = CreateEvent(NULL, TRUE, FALSE, NULL);
267     ok(info.wait != NULL, "CreateEvent failed with error %d\n", GetLastError());
268
269     info.stop = CreateEvent(NULL, TRUE, FALSE, NULL);
270     ok(info.stop != NULL, "CreateEvent failed with error %d\n", GetLastError());
271
272     thread = CreateThread(NULL, 0, ole_initialize_thread, &info, 0, &tid);
273     ok(thread != NULL, "CreateThread failed with error %d\n", GetLastError());
274
275     WaitForSingleObject(info.wait, INFINITE);
276
277     pUnk = (IUnknown *)0xdeadbeef;
278     hr = CoCreateInstance(rclsid, NULL, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void **)&pUnk);
279     ok(hr == S_OK, "CoCreateInstance should have returned S_OK instead of 0x%08x\n", hr);
280     if (pUnk) IUnknown_Release(pUnk);
281
282     SetEvent(info.stop);
283     WaitForSingleObject(thread, INFINITE);
284
285     GetExitCodeThread(thread, &exitcode);
286     hr = exitcode;
287     ok(hr == S_OK, "thread should have returned S_OK instead of 0x%08x\n", hr);
288
289     CloseHandle(thread);
290     CloseHandle(info.wait);
291     CloseHandle(info.stop);
292 }
293
294 static void test_CoGetClassObject(void)
295 {
296     HRESULT hr;
297     HANDLE thread;
298     DWORD tid, exitcode;
299     IUnknown *pUnk;
300     struct info info;
301     REFCLSID rclsid = &CLSID_InternetZoneManager;
302
303     hr = CoGetClassObject(rclsid, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void **)&pUnk);
304     ok(hr == CO_E_NOTINITIALIZED, "CoGetClassObject should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n", hr);
305     ok(pUnk == NULL, "CoGetClassObject should have changed the passed in pointer to NULL, instead of %p\n", pUnk);
306
307     hr = CoGetClassObject(rclsid, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, NULL);
308     ok(hr == E_INVALIDARG ||
309        broken(hr == CO_E_NOTINITIALIZED), /* win9x */
310        "CoGetClassObject should have returned E_INVALIDARG instead of 0x%08x\n", hr);
311
312     /* show that COM doesn't have to be initialized for multi-threaded apartments if another
313        thread has already done so */
314
315     info.wait = CreateEvent(NULL, TRUE, FALSE, NULL);
316     ok(info.wait != NULL, "CreateEvent failed with error %d\n", GetLastError());
317
318     info.stop = CreateEvent(NULL, TRUE, FALSE, NULL);
319     ok(info.stop != NULL, "CreateEvent failed with error %d\n", GetLastError());
320
321     thread = CreateThread(NULL, 0, ole_initialize_thread, &info, 0, &tid);
322     ok(thread != NULL, "CreateThread failed with error %d\n", GetLastError());
323
324     WaitForSingleObject(info.wait, INFINITE);
325
326     pUnk = (IUnknown *)0xdeadbeef;
327     hr = CoGetClassObject(rclsid, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void **)&pUnk);
328     ok(hr == S_OK, "CoGetClassObject should have returned S_OK instead of 0x%08x\n", hr);
329     if (pUnk) IUnknown_Release(pUnk);
330
331     SetEvent(info.stop);
332     WaitForSingleObject(thread, INFINITE);
333
334     GetExitCodeThread(thread, &exitcode);
335     hr = exitcode;
336     ok(hr == S_OK, "thread should have returned S_OK instead of 0x%08x\n", hr);
337
338     CloseHandle(thread);
339     CloseHandle(info.wait);
340     CloseHandle(info.stop);
341 }
342
343 static ATOM register_dummy_class(void)
344 {
345     WNDCLASS wc =
346     {
347         0,
348         DefWindowProc,
349         0,
350         0,
351         GetModuleHandle(NULL),
352         NULL,
353         LoadCursor(NULL, IDC_ARROW),
354         (HBRUSH)(COLOR_BTNFACE+1),
355         NULL,
356         TEXT("WineOleTestClass"),
357     };
358
359     return RegisterClass(&wc);
360 }
361
362 static void test_ole_menu(void)
363 {
364         HWND hwndFrame;
365         HRESULT hr;
366
367         hwndFrame = CreateWindow(MAKEINTATOM(register_dummy_class()), "Test", 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, NULL, NULL);
368         hr = OleSetMenuDescriptor(NULL, hwndFrame, NULL, NULL, NULL);
369         todo_wine ok_ole_success(hr, "OleSetMenuDescriptor");
370
371         DestroyWindow(hwndFrame);
372 }
373
374
375 static HRESULT WINAPI MessageFilter_QueryInterface(IMessageFilter *iface, REFIID riid, void ** ppvObj)
376 {
377     if (ppvObj == NULL) return E_POINTER;
378
379     if (IsEqualGUID(riid, &IID_IUnknown) ||
380         IsEqualGUID(riid, &IID_IClassFactory))
381     {
382         *ppvObj = iface;
383         IMessageFilter_AddRef(iface);
384         return S_OK;
385     }
386
387     return E_NOINTERFACE;
388 }
389
390 static ULONG WINAPI MessageFilter_AddRef(IMessageFilter *iface)
391 {
392     return 2; /* non-heap object */
393 }
394
395 static ULONG WINAPI MessageFilter_Release(IMessageFilter *iface)
396 {
397     return 1; /* non-heap object */
398 }
399
400 static DWORD WINAPI MessageFilter_HandleInComingCall(
401   IMessageFilter *iface,
402   DWORD dwCallType,
403   HTASK threadIDCaller,
404   DWORD dwTickCount,
405   LPINTERFACEINFO lpInterfaceInfo)
406 {
407     trace("HandleInComingCall\n");
408     return SERVERCALL_ISHANDLED;
409 }
410
411 static DWORD WINAPI MessageFilter_RetryRejectedCall(
412   IMessageFilter *iface,
413   HTASK threadIDCallee,
414   DWORD dwTickCount,
415   DWORD dwRejectType)
416 {
417     trace("RetryRejectedCall\n");
418     return 0;
419 }
420
421 static DWORD WINAPI MessageFilter_MessagePending(
422   IMessageFilter *iface,
423   HTASK threadIDCallee,
424   DWORD dwTickCount,
425   DWORD dwPendingType)
426 {
427     trace("MessagePending\n");
428     return PENDINGMSG_WAITNOPROCESS;
429 }
430
431 static const IMessageFilterVtbl MessageFilter_Vtbl =
432 {
433     MessageFilter_QueryInterface,
434     MessageFilter_AddRef,
435     MessageFilter_Release,
436     MessageFilter_HandleInComingCall,
437     MessageFilter_RetryRejectedCall,
438     MessageFilter_MessagePending
439 };
440
441 static IMessageFilter MessageFilter = { &MessageFilter_Vtbl };
442
443 static void test_CoRegisterMessageFilter(void)
444 {
445     HRESULT hr;
446     IMessageFilter *prev_filter;
447
448     hr = CoRegisterMessageFilter(&MessageFilter, &prev_filter);
449     ok(hr == CO_E_NOT_SUPPORTED,
450         "CoRegisterMessageFilter should have failed with CO_E_NOT_SUPPORTED instead of 0x%08x\n",
451         hr);
452
453     pCoInitializeEx(NULL, COINIT_MULTITHREADED);
454     prev_filter = (IMessageFilter *)0xdeadbeef;
455     hr = CoRegisterMessageFilter(&MessageFilter, &prev_filter);
456     ok(hr == CO_E_NOT_SUPPORTED,
457         "CoRegisterMessageFilter should have failed with CO_E_NOT_SUPPORTED instead of 0x%08x\n",
458         hr);
459     ok(prev_filter == (IMessageFilter *)0xdeadbeef,
460         "prev_filter should have been set to %p\n", prev_filter);
461     CoUninitialize();
462
463     pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
464
465     hr = CoRegisterMessageFilter(NULL, NULL);
466     ok_ole_success(hr, "CoRegisterMessageFilter");
467
468     prev_filter = (IMessageFilter *)0xdeadbeef;
469     hr = CoRegisterMessageFilter(NULL, &prev_filter);
470     ok_ole_success(hr, "CoRegisterMessageFilter");
471     ok(prev_filter == NULL, "prev_filter should have been set to NULL instead of %p\n", prev_filter);
472
473     hr = CoRegisterMessageFilter(&MessageFilter, &prev_filter);
474     ok_ole_success(hr, "CoRegisterMessageFilter");
475     ok(prev_filter == NULL, "prev_filter should have been set to NULL instead of %p\n", prev_filter);
476
477     hr = CoRegisterMessageFilter(NULL, NULL);
478     ok_ole_success(hr, "CoRegisterMessageFilter");
479
480     CoUninitialize();
481 }
482
483 static HRESULT WINAPI Test_IUnknown_QueryInterface(
484     LPUNKNOWN iface,
485     REFIID riid,
486     LPVOID *ppvObj)
487 {
488     if (ppvObj == NULL) return E_POINTER;
489
490     if (IsEqualIID(riid, &IID_IUnknown) ||
491         IsEqualIID(riid, &IID_IWineTest))
492     {
493         *ppvObj = iface;
494         IUnknown_AddRef(iface);
495         return S_OK;
496     }
497
498     *ppvObj = NULL;
499     return E_NOINTERFACE;
500 }
501
502 static ULONG WINAPI Test_IUnknown_AddRef(LPUNKNOWN iface)
503 {
504     return 2; /* non-heap-based object */
505 }
506
507 static ULONG WINAPI Test_IUnknown_Release(LPUNKNOWN iface)
508 {
509     return 1; /* non-heap-based object */
510 }
511
512 static const IUnknownVtbl TestUnknown_Vtbl =
513 {
514     Test_IUnknown_QueryInterface,
515     Test_IUnknown_AddRef,
516     Test_IUnknown_Release,
517 };
518
519 static IUnknown Test_Unknown = { &TestUnknown_Vtbl };
520
521 static HRESULT WINAPI PSFactoryBuffer_QueryInterface(
522     IPSFactoryBuffer * This,
523     /* [in] */ REFIID riid,
524     /* [iid_is][out] */ void **ppvObject)
525 {
526     if (IsEqualIID(riid, &IID_IUnknown) ||
527         IsEqualIID(riid, &IID_IPSFactoryBuffer))
528     {
529         *ppvObject = This;
530         IPSFactoryBuffer_AddRef(This);
531         return S_OK;
532     }
533     return E_NOINTERFACE;
534 }
535
536 static ULONG WINAPI PSFactoryBuffer_AddRef(
537     IPSFactoryBuffer * This)
538 {
539     return 2;
540 }
541
542 static ULONG WINAPI PSFactoryBuffer_Release(
543     IPSFactoryBuffer * This)
544 {
545     return 1;
546 }
547
548 static HRESULT WINAPI PSFactoryBuffer_CreateProxy(
549     IPSFactoryBuffer * This,
550     /* [in] */ IUnknown *pUnkOuter,
551     /* [in] */ REFIID riid,
552     /* [out] */ IRpcProxyBuffer **ppProxy,
553     /* [out] */ void **ppv)
554 {
555     return E_NOTIMPL;
556 }
557
558 static HRESULT WINAPI PSFactoryBuffer_CreateStub(
559     IPSFactoryBuffer * This,
560     /* [in] */ REFIID riid,
561     /* [unique][in] */ IUnknown *pUnkServer,
562     /* [out] */ IRpcStubBuffer **ppStub)
563 {
564     return E_NOTIMPL;
565 }
566
567 static IPSFactoryBufferVtbl PSFactoryBufferVtbl =
568 {
569     PSFactoryBuffer_QueryInterface,
570     PSFactoryBuffer_AddRef,
571     PSFactoryBuffer_Release,
572     PSFactoryBuffer_CreateProxy,
573     PSFactoryBuffer_CreateStub
574 };
575
576 static IPSFactoryBuffer PSFactoryBuffer = { &PSFactoryBufferVtbl };
577
578 static const CLSID CLSID_WineTestPSFactoryBuffer =
579 {
580     0x52011640,
581     0x8164,
582     0x4fd0,
583     {0xa1, 0xa2, 0x5d, 0x5a, 0x36, 0x54, 0xd3, 0xbd}
584 }; /* 52011640-8164-4fd0-a1a2-5d5a3654d3bd */
585
586 static void test_CoRegisterPSClsid(void)
587 {
588     HRESULT hr;
589     DWORD dwRegistrationKey;
590     IStream *stream;
591     CLSID clsid;
592
593     hr = CoRegisterPSClsid(&IID_IWineTest, &CLSID_WineTestPSFactoryBuffer);
594     ok(hr == CO_E_NOTINITIALIZED, "CoRegisterPSClsid should have returened CO_E_NOTINITIALIZED instead of 0x%08x\n", hr);
595
596     pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
597
598     hr = CoRegisterClassObject(&CLSID_WineTestPSFactoryBuffer, (IUnknown *)&PSFactoryBuffer,
599         CLSCTX_INPROC_SERVER, REGCLS_MULTIPLEUSE, &dwRegistrationKey);
600     ok_ole_success(hr, "CoRegisterClassObject");
601
602     hr = CoRegisterPSClsid(&IID_IWineTest, &CLSID_WineTestPSFactoryBuffer);
603     ok_ole_success(hr, "CoRegisterPSClsid");
604
605     hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
606     ok_ole_success(hr, "CreateStreamOnHGlobal");
607
608     hr = CoMarshalInterface(stream, &IID_IWineTest, &Test_Unknown, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
609     ok(hr == E_NOTIMPL, "CoMarshalInterface should have returned E_NOTIMPL instead of 0x%08x\n", hr);
610     IStream_Release(stream);
611
612     hr = CoRevokeClassObject(dwRegistrationKey);
613     ok_ole_success(hr, "CoRevokeClassObject");
614
615     CoUninitialize();
616
617     pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
618
619     hr = CoGetPSClsid(&IID_IWineTest, &clsid);
620     ok(hr == REGDB_E_IIDNOTREG, "CoGetPSClsid should have returned REGDB_E_IIDNOTREG instead of 0x%08x\n", hr);
621
622     CoUninitialize();
623 }
624
625 static void test_CoGetPSClsid(void)
626 {
627     HRESULT hr;
628     CLSID clsid;
629
630     hr = CoGetPSClsid(&IID_IClassFactory, &clsid);
631     ok(hr == CO_E_NOTINITIALIZED,
632        "CoGetPSClsid should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n",
633        hr);
634
635     pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
636
637     hr = CoGetPSClsid(&IID_IClassFactory, &clsid);
638     ok_ole_success(hr, "CoGetPSClsid");
639
640     hr = CoGetPSClsid(&IID_IWineTest, &clsid);
641     ok(hr == REGDB_E_IIDNOTREG,
642        "CoGetPSClsid for random IID returned 0x%08x instead of REGDB_E_IIDNOTREG\n",
643        hr);
644
645     hr = CoGetPSClsid(&IID_IClassFactory, NULL);
646     ok(hr == E_INVALIDARG,
647        "CoGetPSClsid for null clsid returned 0x%08x instead of E_INVALIDARG\n",
648        hr);
649
650     CoUninitialize();
651 }
652
653 /* basic test, mainly for invalid arguments. see marshal.c for more */
654 static void test_CoUnmarshalInterface(void)
655 {
656     IUnknown *pProxy;
657     IStream *pStream;
658     HRESULT hr;
659
660     hr = CoUnmarshalInterface(NULL, &IID_IUnknown, (void **)&pProxy);
661     ok(hr == E_INVALIDARG, "CoUnmarshalInterface should have returned E_INVALIDARG instead of 0x%08x\n", hr);
662
663     hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
664     ok_ole_success(hr, "CreateStreamOnHGlobal");
665
666     hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)&pProxy);
667     todo_wine
668     ok(hr == CO_E_NOTINITIALIZED, "CoUnmarshalInterface should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n", hr);
669
670     pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
671
672     hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)&pProxy);
673     ok(hr == STG_E_READFAULT, "CoUnmarshalInterface should have returned STG_E_READFAULT instead of 0x%08x\n", hr);
674
675     CoUninitialize();
676
677     hr = CoUnmarshalInterface(pStream, &IID_IUnknown, NULL);
678     ok(hr == E_INVALIDARG, "CoUnmarshalInterface should have returned E_INVALIDARG instead of 0x%08x\n", hr);
679
680     IStream_Release(pStream);
681 }
682
683 static void test_CoGetInterfaceAndReleaseStream(void)
684 {
685     HRESULT hr;
686     IUnknown *pUnk;
687
688     pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
689
690     hr = CoGetInterfaceAndReleaseStream(NULL, &IID_IUnknown, (void**)&pUnk);
691     ok(hr == E_INVALIDARG, "hr %08x\n", hr);
692
693     CoUninitialize();
694 }
695
696 /* basic test, mainly for invalid arguments. see marshal.c for more */
697 static void test_CoMarshalInterface(void)
698 {
699     IStream *pStream;
700     HRESULT hr;
701     static const LARGE_INTEGER llZero;
702
703     pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
704
705     hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
706     ok_ole_success(hr, "CreateStreamOnHGlobal");
707
708     hr = CoMarshalInterface(pStream, &IID_IUnknown, NULL, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
709     ok(hr == E_INVALIDARG, "CoMarshalInterface should have returned E_INVALIDARG instead of 0x%08x\n", hr);
710
711     hr = CoMarshalInterface(NULL, &IID_IUnknown, (IUnknown *)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
712     ok(hr == E_INVALIDARG, "CoMarshalInterface should have returned E_INVALIDARG instead of 0x%08x\n", hr);
713
714     hr = CoMarshalInterface(pStream, &IID_IUnknown, (IUnknown *)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
715     ok_ole_success(hr, "CoMarshalInterface");
716
717     /* stream not rewound */
718     hr = CoReleaseMarshalData(pStream);
719     ok(hr == STG_E_READFAULT, "CoReleaseMarshalData should have returned STG_E_READFAULT instead of 0x%08x\n", hr);
720
721     hr = IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
722     ok_ole_success(hr, "IStream_Seek");
723
724     hr = CoReleaseMarshalData(pStream);
725     ok_ole_success(hr, "CoReleaseMarshalData");
726
727     IStream_Release(pStream);
728
729     CoUninitialize();
730 }
731
732 static void test_CoMarshalInterThreadInterfaceInStream(void)
733 {
734     IStream *pStream;
735     HRESULT hr;
736     IClassFactory *pProxy;
737
738     pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
739
740     cLocks = 0;
741
742     hr = CoMarshalInterThreadInterfaceInStream(&IID_IUnknown, (IUnknown *)&Test_ClassFactory, NULL);
743     ok(hr == E_INVALIDARG, "CoMarshalInterThreadInterfaceInStream should have returned E_INVALIDARG instead of 0x%08x\n", hr);
744
745     hr = CoMarshalInterThreadInterfaceInStream(&IID_IUnknown, NULL, &pStream);
746     ok(hr == E_INVALIDARG, "CoMarshalInterThreadInterfaceInStream should have returned E_INVALIDARG instead of 0x%08x\n", hr);
747
748     ok_no_locks();
749
750     hr = CoMarshalInterThreadInterfaceInStream(&IID_IUnknown, (IUnknown *)&Test_ClassFactory, &pStream);
751     ok_ole_success(hr, "CoMarshalInterThreadInterfaceInStream");
752
753     ok_more_than_one_lock();
754
755     hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
756     ok_ole_success(hr, "CoUnmarshalInterface");
757
758     IClassFactory_Release(pProxy);
759     IStream_Release(pStream);
760
761     ok_no_locks();
762
763     CoUninitialize();
764 }
765
766 static void test_CoRegisterClassObject(void)
767 {
768     DWORD cookie;
769     HRESULT hr;
770     IClassFactory *pcf;
771
772     pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
773
774     /* CLSCTX_INPROC_SERVER */
775     hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
776                                CLSCTX_INPROC_SERVER, REGCLS_SINGLEUSE, &cookie);
777     ok_ole_success(hr, "CoRegisterClassObject");
778     hr = CoRevokeClassObject(cookie);
779     ok_ole_success(hr, "CoRevokeClassObject");
780
781     hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
782                                CLSCTX_INPROC_SERVER, REGCLS_MULTIPLEUSE, &cookie);
783     ok_ole_success(hr, "CoRegisterClassObject");
784     hr = CoRevokeClassObject(cookie);
785     ok_ole_success(hr, "CoRevokeClassObject");
786
787     hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
788                                CLSCTX_INPROC_SERVER, REGCLS_MULTI_SEPARATE, &cookie);
789     ok_ole_success(hr, "CoRegisterClassObject");
790     hr = CoRevokeClassObject(cookie);
791     ok_ole_success(hr, "CoRevokeClassObject");
792
793     /* CLSCTX_LOCAL_SERVER */
794     hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
795                                CLSCTX_LOCAL_SERVER, REGCLS_SINGLEUSE, &cookie);
796     ok_ole_success(hr, "CoRegisterClassObject");
797     hr = CoRevokeClassObject(cookie);
798     ok_ole_success(hr, "CoRevokeClassObject");
799
800     hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
801                                CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE, &cookie);
802     ok_ole_success(hr, "CoRegisterClassObject");
803     hr = CoRevokeClassObject(cookie);
804     ok_ole_success(hr, "CoRevokeClassObject");
805
806     hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
807                                CLSCTX_LOCAL_SERVER, REGCLS_MULTI_SEPARATE, &cookie);
808     ok_ole_success(hr, "CoRegisterClassObject");
809     hr = CoRevokeClassObject(cookie);
810     ok_ole_success(hr, "CoRevokeClassObject");
811
812     /* CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER */
813     hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
814                                CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, REGCLS_SINGLEUSE, &cookie);
815     ok_ole_success(hr, "CoRegisterClassObject");
816     hr = CoRevokeClassObject(cookie);
817     ok_ole_success(hr, "CoRevokeClassObject");
818
819     /* test whether registered class becomes invalid when apartment is destroyed */
820     hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
821                                CLSCTX_INPROC_SERVER, REGCLS_SINGLEUSE, &cookie);
822     ok_ole_success(hr, "CoRegisterClassObject");
823
824     CoUninitialize();
825     pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
826
827     hr = CoGetClassObject(&CLSID_WineOOPTest, CLSCTX_INPROC_SERVER, NULL,
828                           &IID_IClassFactory, (void **)&pcf);
829     ok(hr == REGDB_E_CLASSNOTREG, "object registered in an apartment shouldn't accessible after it is destroyed\n");
830
831     /* crashes with at least win9x DCOM! */
832     if (0)
833         hr = CoRevokeClassObject(cookie);
834
835     CoUninitialize();
836 }
837
838 static HRESULT get_class_object(CLSCTX clsctx)
839 {
840     HRESULT hr;
841     IClassFactory *pcf;
842
843     hr = CoGetClassObject(&CLSID_WineOOPTest, clsctx, NULL, &IID_IClassFactory,
844                           (void **)&pcf);
845
846     if (SUCCEEDED(hr))
847         IClassFactory_Release(pcf);
848
849     return hr;
850 }
851
852 static DWORD CALLBACK get_class_object_thread(LPVOID pv)
853 {
854     CLSCTX clsctx = (CLSCTX)(DWORD_PTR)pv;
855     HRESULT hr;
856
857     pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
858
859     hr = get_class_object(clsctx);
860
861     CoUninitialize();
862
863     return hr;
864 }
865
866 static DWORD CALLBACK get_class_object_proxy_thread(LPVOID pv)
867 {
868     CLSCTX clsctx = (CLSCTX)(DWORD_PTR)pv;
869     HRESULT hr;
870     IClassFactory *pcf;
871     IMultiQI *pMQI;
872
873     pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
874
875     hr = CoGetClassObject(&CLSID_WineOOPTest, clsctx, NULL, &IID_IClassFactory,
876                           (void **)&pcf);
877
878     if (SUCCEEDED(hr))
879     {
880         hr = IClassFactory_QueryInterface(pcf, &IID_IMultiQI, (void **)&pMQI);
881         if (SUCCEEDED(hr))
882             IMultiQI_Release(pMQI);
883         IClassFactory_Release(pcf);
884     }
885
886     CoUninitialize();
887
888     return hr;
889 }
890
891 static DWORD CALLBACK register_class_object_thread(LPVOID pv)
892 {
893     HRESULT hr;
894     DWORD cookie;
895
896     pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
897
898     hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
899                                CLSCTX_INPROC_SERVER, REGCLS_SINGLEUSE, &cookie);
900
901     CoUninitialize();
902
903     return hr;
904 }
905
906 static DWORD CALLBACK revoke_class_object_thread(LPVOID pv)
907 {
908     DWORD cookie = (DWORD_PTR)pv;
909     HRESULT hr;
910
911     pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
912
913     hr = CoRevokeClassObject(cookie);
914
915     CoUninitialize();
916
917     return hr;
918 }
919
920 static void test_registered_object_thread_affinity(void)
921 {
922     HRESULT hr;
923     DWORD cookie;
924     HANDLE thread;
925     DWORD tid;
926     DWORD exitcode;
927
928     pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
929
930     /* CLSCTX_INPROC_SERVER */
931
932     hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
933                                CLSCTX_INPROC_SERVER, REGCLS_SINGLEUSE, &cookie);
934     ok_ole_success(hr, "CoRegisterClassObject");
935
936     thread = CreateThread(NULL, 0, get_class_object_thread, (LPVOID)CLSCTX_INPROC_SERVER, 0, &tid);
937     ok(thread != NULL, "CreateThread failed with error %d\n", GetLastError());
938     WaitForSingleObject(thread, INFINITE);
939     GetExitCodeThread(thread, &exitcode);
940     hr = exitcode;
941     ok(hr == REGDB_E_CLASSNOTREG, "CoGetClassObject on inproc object "
942        "registered in different thread should return REGDB_E_CLASSNOTREG "
943        "instead of 0x%08x\n", hr);
944
945     hr = get_class_object(CLSCTX_INPROC_SERVER);
946     ok(hr == S_OK, "CoGetClassObject on inproc object registered in same "
947        "thread should return S_OK instead of 0x%08x\n", hr);
948
949     thread = CreateThread(NULL, 0, register_class_object_thread, NULL, 0, &tid);
950     ok(thread != NULL, "CreateThread failed with error %d\n", GetLastError());
951     WaitForSingleObject(thread, INFINITE);
952     GetExitCodeThread(thread, &exitcode);
953     hr = exitcode;
954     ok(hr == S_OK, "CoRegisterClassObject with same CLSID but in different thread should return S_OK instead of 0x%08x\n", hr);
955
956     hr = CoRevokeClassObject(cookie);
957     ok_ole_success(hr, "CoRevokeClassObject");
958
959     /* CLSCTX_LOCAL_SERVER */
960
961     hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
962                                CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE, &cookie);
963     ok_ole_success(hr, "CoRegisterClassObject");
964
965     thread = CreateThread(NULL, 0, get_class_object_proxy_thread, (LPVOID)CLSCTX_LOCAL_SERVER, 0, &tid);
966     ok(thread != NULL, "CreateThread failed with error %d\n", GetLastError());
967     while (MsgWaitForMultipleObjects(1, &thread, FALSE, INFINITE, QS_ALLINPUT) == WAIT_OBJECT_0 + 1)
968     {
969         MSG msg;
970         while (PeekMessageA(&msg, NULL, 0, 0, PM_REMOVE))
971         {
972             TranslateMessage(&msg);
973             DispatchMessageA(&msg);
974         }
975     }
976     GetExitCodeThread(thread, &exitcode);
977     hr = exitcode;
978     ok(hr == S_OK, "CoGetClassObject on local server object "
979        "registered in different thread should return S_OK "
980        "instead of 0x%08x\n", hr);
981
982     hr = get_class_object(CLSCTX_LOCAL_SERVER);
983     ok(hr == S_OK, "CoGetClassObject on local server object registered in same "
984        "thread should return S_OK instead of 0x%08x\n", hr);
985
986     thread = CreateThread(NULL, 0, revoke_class_object_thread, (LPVOID)(DWORD_PTR)cookie, 0, &tid);
987     ok(thread != NULL, "CreateThread failed with error %d\n", GetLastError());
988     WaitForSingleObject(thread, INFINITE);
989     GetExitCodeThread(thread, &exitcode);
990     hr = exitcode;
991     ok(hr == RPC_E_WRONG_THREAD, "CoRevokeClassObject called from different "
992        "thread to where registered should return RPC_E_WRONG_THREAD instead of 0x%08x\n", hr);
993
994     thread = CreateThread(NULL, 0, register_class_object_thread, NULL, 0, &tid);
995     ok(thread != NULL, "CreateThread failed with error %d\n", GetLastError());
996     WaitForSingleObject(thread, INFINITE);
997     GetExitCodeThread(thread, &exitcode);
998     hr = exitcode;
999     ok(hr == S_OK, "CoRegisterClassObject with same CLSID but in different "
1000         "thread should return S_OK instead of 0x%08x\n", hr);
1001
1002     hr = CoRevokeClassObject(cookie);
1003     ok_ole_success(hr, "CoRevokeClassObject");
1004
1005     CoUninitialize();
1006 }
1007
1008 static DWORD CALLBACK free_libraries_thread(LPVOID p)
1009 {
1010     CoFreeUnusedLibraries();
1011     return 0;
1012 }
1013
1014 static inline BOOL is_module_loaded(const char *module)
1015 {
1016     return GetModuleHandle(module) ? TRUE : FALSE;
1017 }
1018
1019 static void test_CoFreeUnusedLibraries(void)
1020 {
1021     HRESULT hr;
1022     IUnknown *pUnk;
1023     DWORD tid;
1024     HANDLE thread;
1025
1026     pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1027
1028     ok(!is_module_loaded("urlmon.dll"), "urlmon.dll shouldn't be loaded\n");
1029
1030     hr = CoCreateInstance(&CLSID_FileProtocol, NULL, CLSCTX_INPROC_SERVER, &IID_IInternetProtocol, (void **)&pUnk);
1031     if (hr == REGDB_E_CLASSNOTREG)
1032     {
1033         trace("IE not installed so can't run CoFreeUnusedLibraries test\n");
1034         CoUninitialize();
1035         return;
1036     }
1037     ok_ole_success(hr, "CoCreateInstance");
1038
1039     ok(is_module_loaded("urlmon.dll"), "urlmon.dll should be loaded\n");
1040
1041     ok(pUnk != NULL ||
1042        broken(pUnk == NULL), /* win9x */
1043        "Expected a valid pointer\n");
1044     if (pUnk)
1045         IUnknown_Release(pUnk);
1046
1047     ok(is_module_loaded("urlmon.dll"), "urlmon.dll should be loaded\n");
1048
1049     thread = CreateThread(NULL, 0, free_libraries_thread, NULL, 0, &tid);
1050     WaitForSingleObject(thread, INFINITE);
1051     CloseHandle(thread);
1052
1053     ok(is_module_loaded("urlmon.dll"), "urlmon.dll should be loaded\n");
1054
1055     CoFreeUnusedLibraries();
1056
1057     ok(!is_module_loaded("urlmon.dll"), "urlmon.dll shouldn't be loaded\n");
1058
1059     CoUninitialize();
1060 }
1061
1062 static void test_CoGetObjectContext(void)
1063 {
1064     HRESULT hr;
1065     ULONG refs;
1066     IComThreadingInfo *pComThreadingInfo;
1067     IContextCallback *pContextCallback;
1068     APTTYPE apttype;
1069     THDTYPE thdtype;
1070
1071     if (!pCoGetObjectContext)
1072     {
1073         skip("CoGetObjectContext not present\n");
1074         return;
1075     }
1076
1077     hr = pCoGetObjectContext(&IID_IComThreadingInfo, (void **)&pComThreadingInfo);
1078     ok(hr == CO_E_NOTINITIALIZED, "CoGetObjectContext should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n", hr);
1079     ok(pComThreadingInfo == NULL, "pComThreadingInfo should have been set to NULL\n");
1080
1081     pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1082
1083     hr = pCoGetObjectContext(&IID_IComThreadingInfo, (void **)&pComThreadingInfo);
1084     ok_ole_success(hr, "CoGetObjectContext");
1085
1086     hr = IComThreadingInfo_GetCurrentApartmentType(pComThreadingInfo, &apttype);
1087     ok_ole_success(hr, "IComThreadingInfo_GetCurrentApartmentType");
1088     ok(apttype == APTTYPE_MAINSTA, "apartment type should be APTTYPE_MAINSTA instead of %d\n", apttype);
1089
1090     hr = IComThreadingInfo_GetCurrentThreadType(pComThreadingInfo, &thdtype);
1091     ok_ole_success(hr, "IComThreadingInfo_GetCurrentThreadType");
1092     ok(thdtype == THDTYPE_PROCESSMESSAGES, "thread type should be THDTYPE_PROCESSMESSAGES instead of %d\n", thdtype);
1093
1094     refs = IComThreadingInfo_Release(pComThreadingInfo);
1095     ok(refs == 0, "pComThreadingInfo should have 0 refs instead of %d refs\n", refs);
1096
1097     hr = pCoGetObjectContext(&IID_IContextCallback, (void **)&pContextCallback);
1098     ok_ole_success(hr, "CoGetObjectContext(ContextCallback)");
1099
1100     if (hr == S_OK)
1101     {
1102         refs = IContextCallback_Release(pContextCallback);
1103         ok(refs == 0, "pContextCallback should have 0 refs instead of %d refs\n", refs);
1104     }
1105
1106     CoUninitialize();
1107
1108     pCoInitializeEx(NULL, COINIT_MULTITHREADED);
1109
1110     hr = pCoGetObjectContext(&IID_IComThreadingInfo, (void **)&pComThreadingInfo);
1111     ok_ole_success(hr, "CoGetObjectContext");
1112
1113     hr = IComThreadingInfo_GetCurrentApartmentType(pComThreadingInfo, &apttype);
1114     ok_ole_success(hr, "IComThreadingInfo_GetCurrentApartmentType");
1115     ok(apttype == APTTYPE_MTA, "apartment type should be APTTYPE_MTA instead of %d\n", apttype);
1116
1117     hr = IComThreadingInfo_GetCurrentThreadType(pComThreadingInfo, &thdtype);
1118     ok_ole_success(hr, "IComThreadingInfo_GetCurrentThreadType");
1119     ok(thdtype == THDTYPE_BLOCKMESSAGES, "thread type should be THDTYPE_BLOCKMESSAGES instead of %d\n", thdtype);
1120
1121     refs = IComThreadingInfo_Release(pComThreadingInfo);
1122     ok(refs == 0, "pComThreadingInfo should have 0 refs instead of %d refs\n", refs);
1123
1124     hr = pCoGetObjectContext(&IID_IContextCallback, (void **)&pContextCallback);
1125     ok_ole_success(hr, "CoGetObjectContext(ContextCallback)");
1126
1127     if (hr == S_OK)
1128     {
1129         refs = IContextCallback_Release(pContextCallback);
1130         ok(refs == 0, "pContextCallback should have 0 refs instead of %d refs\n", refs);
1131     }
1132
1133     CoUninitialize();
1134 }
1135
1136 typedef struct {
1137     const IUnknownVtbl *lpVtbl;
1138     LONG refs;
1139 } Test_CallContext;
1140
1141 static HRESULT WINAPI Test_CallContext_QueryInterface(
1142     IUnknown *iface,
1143     REFIID riid,
1144     LPVOID *ppvObj)
1145 {
1146     if (ppvObj == NULL) return E_POINTER;
1147
1148     if (IsEqualGUID(riid, &IID_IUnknown))
1149     {
1150         *ppvObj = iface;
1151         IUnknown_AddRef(iface);
1152         return S_OK;
1153     }
1154
1155     *ppvObj = NULL;
1156     return E_NOINTERFACE;
1157 }
1158
1159 static ULONG WINAPI Test_CallContext_AddRef(IUnknown *iface)
1160 {
1161     Test_CallContext *This = (Test_CallContext*)iface;
1162     return InterlockedIncrement(&This->refs);
1163 }
1164
1165 static ULONG WINAPI Test_CallContext_Release(IUnknown *iface)
1166 {
1167     Test_CallContext *This = (Test_CallContext*)iface;
1168     ULONG refs = InterlockedDecrement(&This->refs);
1169     if (!refs)
1170         HeapFree(GetProcessHeap(), 0, This);
1171     return refs;
1172 }
1173
1174 static const IUnknownVtbl TestCallContext_Vtbl =
1175 {
1176     Test_CallContext_QueryInterface,
1177     Test_CallContext_AddRef,
1178     Test_CallContext_Release
1179 };
1180
1181 static void test_CoGetCallContext(void)
1182 {
1183     HRESULT hr;
1184     ULONG refs;
1185     IUnknown *pUnk;
1186     IUnknown *test_object;
1187
1188     if (!pCoSwitchCallContext)
1189     {
1190         skip("CoSwitchCallContext not present\n");
1191         return;
1192     }
1193
1194     CoInitialize(NULL);
1195
1196     test_object = HeapAlloc(GetProcessHeap(), 0, sizeof(Test_CallContext));
1197     ((Test_CallContext*)test_object)->lpVtbl = &TestCallContext_Vtbl;
1198     ((Test_CallContext*)test_object)->refs = 1;
1199
1200     hr = CoGetCallContext(&IID_IUnknown, (void**)&pUnk);
1201     ok(hr == RPC_E_CALL_COMPLETE, "Expected RPC_E_CALL_COMPLETE, got 0x%08x\n", hr);
1202
1203     pUnk = (IUnknown*)0xdeadbeef;
1204     hr = pCoSwitchCallContext(test_object, &pUnk);
1205     ok_ole_success(hr, "CoSwitchCallContext");
1206     ok(pUnk == NULL, "expected NULL, got %p\n", pUnk);
1207     refs = IUnknown_AddRef(test_object);
1208     ok(refs == 2, "Expected refcount 2, got %d\n", refs);
1209     IUnknown_Release(test_object);
1210
1211     pUnk = (IUnknown*)0xdeadbeef;
1212     hr = CoGetCallContext(&IID_IUnknown, (void**)&pUnk);
1213     ok_ole_success(hr, "CoGetCallContext");
1214     ok(pUnk == test_object, "expected %p, got %p\n", test_object, pUnk);
1215     refs = IUnknown_AddRef(test_object);
1216     ok(refs == 3, "Expected refcount 3, got %d\n", refs);
1217     IUnknown_Release(test_object);
1218     IUnknown_Release(pUnk);
1219
1220     pUnk = (IUnknown*)0xdeadbeef;
1221     hr = pCoSwitchCallContext(NULL, &pUnk);
1222     ok_ole_success(hr, "CoSwitchCallContext");
1223     ok(pUnk == test_object, "expected %p, got %p\n", test_object, pUnk);
1224     refs = IUnknown_AddRef(test_object);
1225     ok(refs == 2, "Expected refcount 2, got %d\n", refs);
1226     IUnknown_Release(test_object);
1227
1228     hr = CoGetCallContext(&IID_IUnknown, (void**)&pUnk);
1229     ok(hr == RPC_E_CALL_COMPLETE, "Expected RPC_E_CALL_COMPLETE, got 0x%08x\n", hr);
1230
1231     IUnknown_Release(test_object);
1232
1233     CoUninitialize();
1234 }
1235
1236 static void test_CoGetTreatAsClass(void)
1237 {
1238     HRESULT hr;
1239     CLSID out;
1240     static GUID deadbeef = {0xdeadbeef,0xdead,0xbeef,{0xde,0xad,0xbe,0xef,0xde,0xad,0xbe,0xef}};
1241
1242     if (!pCoGetTreatAsClass)
1243     {
1244         win_skip("CoGetTreatAsClass not present\n");
1245         return;
1246     }
1247     hr = pCoGetTreatAsClass(&deadbeef,&out);
1248     ok (hr == S_FALSE, "expected S_FALSE got %x\n",hr);
1249     ok (IsEqualGUID(&out,&deadbeef), "expected to get same clsid back\n");
1250 }
1251
1252 static void test_CoInitializeEx(void)
1253 {
1254     HRESULT hr;
1255
1256     hr = pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1257     ok(hr == S_OK, "CoInitializeEx failed with error 0x%08x\n", hr);
1258
1259     /* Calling OleInitialize for the first time should yield S_OK even with
1260      * apartment already initialized by previous CoInitialize(Ex) calls. */
1261     hr = OleInitialize(NULL);
1262     todo_wine ok(hr == S_OK, "OleInitialize failed with error 0x%08x\n", hr);
1263
1264     /* Subsequent calls to OleInitialize should return S_FALSE */
1265     hr = OleInitialize(NULL);
1266     ok(hr == S_FALSE, "Expected S_FALSE, hr = 0x%08x\n", hr);
1267
1268     /* Cleanup */
1269     CoUninitialize();
1270     OleUninitialize();
1271 }
1272
1273 START_TEST(compobj)
1274 {
1275     HMODULE hOle32 = GetModuleHandle("ole32");
1276     pCoGetObjectContext = (void*)GetProcAddress(hOle32, "CoGetObjectContext");
1277     pCoSwitchCallContext = (void*)GetProcAddress(hOle32, "CoSwitchCallContext");
1278     pCoGetTreatAsClass = (void*)GetProcAddress(hOle32,"CoGetTreatAsClass");
1279     if (!(pCoInitializeEx = (void*)GetProcAddress(hOle32, "CoInitializeEx")))
1280     {
1281         trace("You need DCOM95 installed to run this test\n");
1282         return;
1283     }
1284
1285     test_ProgIDFromCLSID();
1286     test_CLSIDFromProgID();
1287     test_CLSIDFromString();
1288     test_StringFromGUID2();
1289     test_CoCreateInstance();
1290     test_ole_menu();
1291     test_CoGetClassObject();
1292     test_CoRegisterMessageFilter();
1293     test_CoRegisterPSClsid();
1294     test_CoGetPSClsid();
1295     test_CoUnmarshalInterface();
1296     test_CoGetInterfaceAndReleaseStream();
1297     test_CoMarshalInterface();
1298     test_CoMarshalInterThreadInterfaceInStream();
1299     test_CoRegisterClassObject();
1300     test_registered_object_thread_affinity();
1301     test_CoFreeUnusedLibraries();
1302     test_CoGetObjectContext();
1303     test_CoGetCallContext();
1304     test_CoGetTreatAsClass();
1305     test_CoInitializeEx();
1306 }