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