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