ole32: Remove some superfluous casts of void pointers and zero.
[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 "wine/test.h"
33
34 /* functions that are not present on all versions of Windows */
35 HRESULT (WINAPI * pCoInitializeEx)(LPVOID lpReserved, DWORD dwCoInit);
36 HRESULT (WINAPI * pCoGetObjectContext)(REFIID riid, LPVOID *ppv);
37
38 #define ok_ole_success(hr, func) ok(hr == S_OK, func " failed with error 0x%08x\n", hr)
39 #define ok_more_than_one_lock() ok(cLocks > 0, "Number of locks should be > 0, but actually is %d\n", cLocks)
40 #define ok_no_locks() ok(cLocks == 0, "Number of locks should be 0, but actually is %d\n", cLocks)
41
42 static const CLSID CLSID_non_existent =   { 0x12345678, 0x1234, 0x1234, { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 } };
43 static const CLSID CLSID_CDeviceMoniker = { 0x4315d437, 0x5b8c, 0x11d0, { 0xbd, 0x3b, 0x00, 0xa0, 0xc9, 0x11, 0xce, 0x86 } };
44 static WCHAR devicedotone[] = {'d','e','v','i','c','e','.','1',0};
45 static const WCHAR wszNonExistent[] = {'N','o','n','E','x','i','s','t','e','n','t',0};
46 static WCHAR wszCLSID_CDeviceMoniker[] =
47 {
48     '{',
49     '4','3','1','5','d','4','3','7','-',
50     '5','b','8','c','-',
51     '1','1','d','0','-',
52     'b','d','3','b','-',
53     '0','0','a','0','c','9','1','1','c','e','8','6',
54     '}',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 = (LPVOID)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_CDeviceMoniker, &progid);
147     ok(hr == S_OK, "ProgIDFromCLSID failed with error 0x%08x\n", hr);
148     if (hr == S_OK)
149     {
150         ok(!lstrcmpiW(progid, devicedotone), "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_CDeviceMoniker, 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(devicedotone, &clsid);
167     ok(hr == S_OK, "CLSIDFromProgID failed with error 0x%08x\n", hr);
168     ok(IsEqualCLSID(&clsid, &CLSID_CDeviceMoniker), "clsid wasn't equal to CLSID_CDeviceMoniker\n");
169
170     hr = CLSIDFromString(devicedotone, &clsid);
171     ok_ole_success(hr, "CLSIDFromString");
172     ok(IsEqualCLSID(&clsid, &CLSID_CDeviceMoniker), "clsid wasn't equal to CLSID_CDeviceMoniker\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_CDeviceMoniker, &clsid);
192     ok_ole_success(hr, "CLSIDFromString");
193     ok(IsEqualCLSID(&clsid, &CLSID_CDeviceMoniker), "clsid wasn't equal to CLSID_CDeviceMoniker\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_CDeviceMoniker,str,50);
206   ok(len == 39, "len: %d (expected 39)\n", len);
207   ok(!lstrcmpiW(str, wszCLSID_CDeviceMoniker),"string wan't equal for CLSID_CDeviceMoniker\n");
208
209   memset(str,0,sizeof str);
210   len = StringFromGUID2(&CLSID_CDeviceMoniker,str,39);
211   ok(len == 39, "len: %d (expected 39)\n", len);
212   ok(!lstrcmpiW(str, wszCLSID_CDeviceMoniker),"string wan't equal for CLSID_CDeviceMoniker\n");
213
214   len = StringFromGUID2(&CLSID_CDeviceMoniker,str,38);
215   ok(len == 0, "len: %d (expected 0)\n", len);
216
217   len = StringFromGUID2(&CLSID_CDeviceMoniker,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 = (LPVOID)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 = (LPVOID)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, (IUnknown *)&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)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_IUnknown, (void **)&pUnk);
940     if (hr == REGDB_E_CLASSNOTREG)
941     {
942         trace("IE not installed so can't run CoFreeUnusedLibraries test\n");
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     APTTYPE apttype;
976     THDTYPE thdtype;
977
978     if (!pCoGetObjectContext)
979     {
980         skip("CoGetObjectContext not present\n");
981         return;
982     }
983
984     hr = pCoGetObjectContext(&IID_IComThreadingInfo, (void **)&pComThreadingInfo);
985     ok(hr == CO_E_NOTINITIALIZED, "CoGetObjectContext should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n", hr);
986     ok(pComThreadingInfo == NULL, "pComThreadingInfo should have been set to NULL\n");
987
988     pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
989
990     hr = pCoGetObjectContext(&IID_IComThreadingInfo, (void **)&pComThreadingInfo);
991     ok_ole_success(hr, "CoGetObjectContext");
992
993     hr = IComThreadingInfo_GetCurrentApartmentType(pComThreadingInfo, &apttype);
994     ok_ole_success(hr, "IComThreadingInfo_GetCurrentApartmentType");
995     ok(apttype == APTTYPE_MAINSTA, "apartment type should be APTTYPE_MAINSTA instead of %d\n", apttype);
996
997     hr = IComThreadingInfo_GetCurrentThreadType(pComThreadingInfo, &thdtype);
998     ok_ole_success(hr, "IComThreadingInfo_GetCurrentThreadType");
999     ok(thdtype == THDTYPE_PROCESSMESSAGES, "thread type should be THDTYPE_PROCESSMESSAGES instead of %d\n", thdtype);
1000
1001     refs = IComThreadingInfo_Release(pComThreadingInfo);
1002     ok(refs == 0, "pComThreadingInfo should have 0 refs instead of %d refs\n", refs);
1003
1004     CoUninitialize();
1005
1006     pCoInitializeEx(NULL, COINIT_MULTITHREADED);
1007
1008     hr = pCoGetObjectContext(&IID_IComThreadingInfo, (void **)&pComThreadingInfo);
1009     ok_ole_success(hr, "CoGetObjectContext");
1010
1011     hr = IComThreadingInfo_GetCurrentApartmentType(pComThreadingInfo, &apttype);
1012     ok_ole_success(hr, "IComThreadingInfo_GetCurrentApartmentType");
1013     ok(apttype == APTTYPE_MTA, "apartment type should be APTTYPE_MTA instead of %d\n", apttype);
1014
1015     hr = IComThreadingInfo_GetCurrentThreadType(pComThreadingInfo, &thdtype);
1016     ok_ole_success(hr, "IComThreadingInfo_GetCurrentThreadType");
1017     ok(thdtype == THDTYPE_BLOCKMESSAGES, "thread type should be THDTYPE_BLOCKMESSAGES instead of %d\n", thdtype);
1018
1019     refs = IComThreadingInfo_Release(pComThreadingInfo);
1020     ok(refs == 0, "pComThreadingInfo should have 0 refs instead of %d refs\n", refs);
1021
1022     CoUninitialize();
1023 }
1024
1025 START_TEST(compobj)
1026 {
1027     HMODULE hOle32 = GetModuleHandle("ole32");
1028     pCoGetObjectContext = (void*)GetProcAddress(hOle32, "CoGetObjectContext");
1029     if (!(pCoInitializeEx = (void*)GetProcAddress(hOle32, "CoInitializeEx")))
1030     {
1031         trace("You need DCOM95 installed to run this test\n");
1032         return;
1033     }
1034
1035     test_ProgIDFromCLSID();
1036     test_CLSIDFromProgID();
1037     test_CLSIDFromString();
1038     test_StringFromGUID2();
1039     test_CoCreateInstance();
1040     test_ole_menu();
1041     test_CoGetClassObject();
1042     test_CoRegisterMessageFilter();
1043     test_CoRegisterPSClsid();
1044     test_CoGetPSClsid();
1045     test_CoUnmarshalInterface();
1046     test_CoGetInterfaceAndReleaseStream();
1047     test_CoMarshalInterface();
1048     test_CoMarshalInterThreadInterfaceInStream();
1049     test_CoRegisterClassObject();
1050     test_registered_object_thread_affinity();
1051     test_CoFreeUnusedLibraries();
1052     test_CoGetObjectContext();
1053 }