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