2 * Component Object Tests
4 * Copyright 2005 Robert Shearman
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.
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.
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
30 #include "urlmon.h" /* for CLSID_FileProtocol */
32 #include "wine/test.h"
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);
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)
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[] =
49 '4','3','1','5','d','4','3','7','-',
53 '0','0','a','0','c','9','1','1','c','e','8','6',
57 static const IID IID_IWineTest =
62 {0xa1, 0xa2, 0x5d, 0x5a, 0x36, 0x54, 0xd3, 0xbd}
63 }; /* 5201163f-8164-4fd0-a1a2-5d5a3654d3bd */
64 static const CLSID CLSID_WineOOPTest = {
68 {0xa1, 0xa2, 0x5d, 0x5a, 0x36, 0x54, 0xd3, 0xbd}
69 }; /* 5201163f-8164-4fd0-a1a2-5d5a3654d3bd */
73 static void LockModule(void)
75 InterlockedIncrement(&cLocks);
78 static void UnlockModule(void)
80 InterlockedDecrement(&cLocks);
83 static HRESULT WINAPI Test_IClassFactory_QueryInterface(
88 if (ppvObj == NULL) return E_POINTER;
90 if (IsEqualGUID(riid, &IID_IUnknown) ||
91 IsEqualGUID(riid, &IID_IClassFactory))
93 *ppvObj = (LPVOID)iface;
94 IClassFactory_AddRef(iface);
102 static ULONG WINAPI Test_IClassFactory_AddRef(LPCLASSFACTORY iface)
105 return 2; /* non-heap-based object */
108 static ULONG WINAPI Test_IClassFactory_Release(LPCLASSFACTORY iface)
111 return 1; /* non-heap-based object */
114 static HRESULT WINAPI Test_IClassFactory_CreateInstance(
115 LPCLASSFACTORY iface,
121 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
122 return E_NOINTERFACE;
125 static HRESULT WINAPI Test_IClassFactory_LockServer(
126 LPCLASSFACTORY iface,
132 static const IClassFactoryVtbl TestClassFactory_Vtbl =
134 Test_IClassFactory_QueryInterface,
135 Test_IClassFactory_AddRef,
136 Test_IClassFactory_Release,
137 Test_IClassFactory_CreateInstance,
138 Test_IClassFactory_LockServer
141 static IClassFactory Test_ClassFactory = { &TestClassFactory_Vtbl };
143 static void test_ProgIDFromCLSID(void)
146 HRESULT hr = ProgIDFromCLSID(&CLSID_CDeviceMoniker, &progid);
147 ok(hr == S_OK, "ProgIDFromCLSID failed with error 0x%08x\n", hr);
150 ok(!lstrcmpiW(progid, devicedotone), "Didn't get expected prog ID\n");
151 CoTaskMemFree(progid);
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);
159 hr = ProgIDFromCLSID(&CLSID_CDeviceMoniker, NULL);
160 ok(hr == E_INVALIDARG, "ProgIDFromCLSID should return E_INVALIDARG instead of 0x%08x\n", hr);
163 static void test_CLSIDFromProgID(void)
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");
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");
174 /* test some failure cases */
176 hr = CLSIDFromProgID(wszNonExistent, NULL);
177 ok(hr == E_INVALIDARG, "CLSIDFromProgID should have returned E_INVALIDARG instead of 0x%08x\n", hr);
179 hr = CLSIDFromProgID(NULL, &clsid);
180 ok(hr == E_INVALIDARG, "CLSIDFromProgID should have returned E_INVALIDARG instead of 0x%08x\n", hr);
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");
188 static void test_CLSIDFromString(void)
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");
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");
200 static void test_StringFromGUID2(void)
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");
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");
214 len = StringFromGUID2(&CLSID_CDeviceMoniker,str,38);
215 ok(len == 0, "len: %d (expected 0)\n", len);
217 len = StringFromGUID2(&CLSID_CDeviceMoniker,str,30);
218 ok(len == 0, "len: %d (expected 0)\n", len);
221 static void test_CoCreateInstance(void)
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);
230 hr = CoCreateInstance(rclsid, NULL, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void **)&pUnk);
231 ok_ole_success(hr, "CoCreateInstance");
232 if(pUnk) IUnknown_Release(pUnk);
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);
239 static void test_CoGetClassObject(void)
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);
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);
252 static ATOM register_dummy_class(void)
260 GetModuleHandle(NULL),
262 LoadCursor(NULL, IDC_ARROW),
263 (HBRUSH)(COLOR_BTNFACE+1),
265 TEXT("WineOleTestClass"),
268 return RegisterClass(&wc);
271 static void test_ole_menu(void)
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");
280 DestroyWindow(hwndFrame);
284 static HRESULT WINAPI MessageFilter_QueryInterface(IMessageFilter *iface, REFIID riid, void ** ppvObj)
286 if (ppvObj == NULL) return E_POINTER;
288 if (IsEqualGUID(riid, &IID_IUnknown) ||
289 IsEqualGUID(riid, &IID_IClassFactory))
291 *ppvObj = (LPVOID)iface;
292 IMessageFilter_AddRef(iface);
296 return E_NOINTERFACE;
299 static ULONG WINAPI MessageFilter_AddRef(IMessageFilter *iface)
301 return 2; /* non-heap object */
304 static ULONG WINAPI MessageFilter_Release(IMessageFilter *iface)
306 return 1; /* non-heap object */
309 static DWORD WINAPI MessageFilter_HandleInComingCall(
310 IMessageFilter *iface,
312 HTASK threadIDCaller,
314 LPINTERFACEINFO lpInterfaceInfo)
316 trace("HandleInComingCall\n");
317 return SERVERCALL_ISHANDLED;
320 static DWORD WINAPI MessageFilter_RetryRejectedCall(
321 IMessageFilter *iface,
322 HTASK threadIDCallee,
326 trace("RetryRejectedCall\n");
330 static DWORD WINAPI MessageFilter_MessagePending(
331 IMessageFilter *iface,
332 HTASK threadIDCallee,
336 trace("MessagePending\n");
337 return PENDINGMSG_WAITNOPROCESS;
340 static const IMessageFilterVtbl MessageFilter_Vtbl =
342 MessageFilter_QueryInterface,
343 MessageFilter_AddRef,
344 MessageFilter_Release,
345 MessageFilter_HandleInComingCall,
346 MessageFilter_RetryRejectedCall,
347 MessageFilter_MessagePending
350 static IMessageFilter MessageFilter = { &MessageFilter_Vtbl };
352 static void test_CoRegisterMessageFilter(void)
355 IMessageFilter *prev_filter;
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",
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",
368 ok(prev_filter == (IMessageFilter *)0xdeadbeef,
369 "prev_filter should have been set to %p\n", prev_filter);
372 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
374 hr = CoRegisterMessageFilter(NULL, NULL);
375 ok_ole_success(hr, "CoRegisterMessageFilter");
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);
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);
386 hr = CoRegisterMessageFilter(NULL, NULL);
387 ok_ole_success(hr, "CoRegisterMessageFilter");
392 static HRESULT WINAPI Test_IUnknown_QueryInterface(
397 if (ppvObj == NULL) return E_POINTER;
399 if (IsEqualIID(riid, &IID_IUnknown) ||
400 IsEqualIID(riid, &IID_IWineTest))
402 *ppvObj = (LPVOID)iface;
403 IUnknown_AddRef(iface);
408 return E_NOINTERFACE;
411 static ULONG WINAPI Test_IUnknown_AddRef(LPUNKNOWN iface)
413 return 2; /* non-heap-based object */
416 static ULONG WINAPI Test_IUnknown_Release(LPUNKNOWN iface)
418 return 1; /* non-heap-based object */
421 static const IUnknownVtbl TestUnknown_Vtbl =
423 Test_IUnknown_QueryInterface,
424 Test_IUnknown_AddRef,
425 Test_IUnknown_Release,
428 static IUnknown Test_Unknown = { &TestUnknown_Vtbl };
430 static HRESULT WINAPI PSFactoryBuffer_QueryInterface(
431 IPSFactoryBuffer * This,
432 /* [in] */ REFIID riid,
433 /* [iid_is][out] */ void **ppvObject)
435 if (IsEqualIID(riid, &IID_IUnknown) ||
436 IsEqualIID(riid, &IID_IPSFactoryBuffer))
439 IPSFactoryBuffer_AddRef(This);
442 return E_NOINTERFACE;
445 static ULONG WINAPI PSFactoryBuffer_AddRef(
446 IPSFactoryBuffer * This)
451 static ULONG WINAPI PSFactoryBuffer_Release(
452 IPSFactoryBuffer * This)
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)
467 static HRESULT WINAPI PSFactoryBuffer_CreateStub(
468 IPSFactoryBuffer * This,
469 /* [in] */ REFIID riid,
470 /* [unique][in] */ IUnknown *pUnkServer,
471 /* [out] */ IRpcStubBuffer **ppStub)
476 static IPSFactoryBufferVtbl PSFactoryBufferVtbl =
478 PSFactoryBuffer_QueryInterface,
479 PSFactoryBuffer_AddRef,
480 PSFactoryBuffer_Release,
481 PSFactoryBuffer_CreateProxy,
482 PSFactoryBuffer_CreateStub
485 static IPSFactoryBuffer PSFactoryBuffer = { &PSFactoryBufferVtbl };
487 static const CLSID CLSID_WineTestPSFactoryBuffer =
492 {0xa1, 0xa2, 0x5d, 0x5a, 0x36, 0x54, 0xd3, 0xbd}
493 }; /* 52011640-8164-4fd0-a1a2-5d5a3654d3bd */
495 static void test_CoRegisterPSClsid(void)
498 DWORD dwRegistrationKey;
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);
505 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
507 hr = CoRegisterClassObject(&CLSID_WineTestPSFactoryBuffer, (IUnknown *)&PSFactoryBuffer,
508 CLSCTX_INPROC_SERVER, REGCLS_MULTIPLEUSE, &dwRegistrationKey);
509 ok_ole_success(hr, "CoRegisterClassObject");
511 hr = CoRegisterPSClsid(&IID_IWineTest, &CLSID_WineTestPSFactoryBuffer);
512 ok_ole_success(hr, "CoRegisterPSClsid");
514 hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
515 ok_ole_success(hr, "CreateStreamOnHGlobal");
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);
521 hr = CoRevokeClassObject(dwRegistrationKey);
522 ok_ole_success(hr, "CoRevokeClassObject");
526 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
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);
534 static void test_CoGetPSClsid(void)
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",
544 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
546 hr = CoGetPSClsid(&IID_IClassFactory, &clsid);
547 ok_ole_success(hr, "CoGetPSClsid");
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",
554 hr = CoGetPSClsid(&IID_IClassFactory, NULL);
555 ok(hr == E_INVALIDARG,
556 "CoGetPSClsid for null clsid returned 0x%08x instead of E_INVALIDARG\n",
562 /* basic test, mainly for invalid arguments. see marshal.c for more */
563 static void test_CoUnmarshalInterface(void)
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);
572 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
573 ok_ole_success(hr, "CreateStreamOnHGlobal");
575 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)&pProxy);
577 ok(hr == CO_E_NOTINITIALIZED, "CoUnmarshalInterface should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n", hr);
579 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
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);
586 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, NULL);
587 ok(hr == E_INVALIDARG, "CoUnmarshalInterface should have returned E_INVALIDARG instead of 0x%08x\n", hr);
589 IStream_Release(pStream);
592 static void test_CoGetInterfaceAndReleaseStream(void)
597 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
599 hr = CoGetInterfaceAndReleaseStream(NULL, &IID_IUnknown, (void**)&pUnk);
600 ok(hr == E_INVALIDARG, "hr %08x\n", hr);
605 /* basic test, mainly for invalid arguments. see marshal.c for more */
606 static void test_CoMarshalInterface(void)
610 static const LARGE_INTEGER llZero;
612 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
614 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
615 ok_ole_success(hr, "CreateStreamOnHGlobal");
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);
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);
623 hr = CoMarshalInterface(pStream, &IID_IUnknown, (IUnknown *)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
624 ok_ole_success(hr, "CoMarshalInterface");
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);
630 hr = IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
631 ok_ole_success(hr, "IStream_Seek");
633 hr = CoReleaseMarshalData(pStream);
634 ok_ole_success(hr, "CoReleaseMarshalData");
636 IStream_Release(pStream);
641 static void test_CoMarshalInterThreadInterfaceInStream(void)
645 IClassFactory *pProxy;
647 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
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);
654 hr = CoMarshalInterThreadInterfaceInStream(&IID_IUnknown, NULL, &pStream);
655 ok(hr == E_INVALIDARG, "CoMarshalInterThreadInterfaceInStream should have returned E_INVALIDARG instead of 0x%08x\n", hr);
659 hr = CoMarshalInterThreadInterfaceInStream(&IID_IUnknown, (IUnknown *)&Test_ClassFactory, &pStream);
660 ok_ole_success(hr, "CoMarshalInterThreadInterfaceInStream");
662 ok_more_than_one_lock();
664 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
665 ok_ole_success(hr, "CoUnmarshalInterface");
667 IClassFactory_Release(pProxy);
668 IStream_Release(pStream);
675 static void test_CoRegisterClassObject(void)
681 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
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");
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");
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");
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");
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");
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");
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");
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");
734 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
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");
740 /* crashes with at least win9x DCOM! */
742 hr = CoRevokeClassObject(cookie);
747 static HRESULT get_class_object(CLSCTX clsctx)
752 hr = CoGetClassObject(&CLSID_WineOOPTest, clsctx, NULL, &IID_IClassFactory,
756 IClassFactory_Release(pcf);
761 static DWORD CALLBACK get_class_object_thread(LPVOID pv)
763 CLSCTX clsctx = (CLSCTX)(DWORD_PTR)pv;
766 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
768 hr = get_class_object(clsctx);
775 static DWORD CALLBACK get_class_object_proxy_thread(LPVOID pv)
777 CLSCTX clsctx = (CLSCTX)(DWORD_PTR)pv;
782 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
784 hr = CoGetClassObject(&CLSID_WineOOPTest, clsctx, NULL, &IID_IClassFactory,
789 hr = IClassFactory_QueryInterface(pcf, &IID_IMultiQI, (void **)&pMQI);
791 IMultiQI_Release(pMQI);
792 IClassFactory_Release(pcf);
800 static DWORD CALLBACK register_class_object_thread(LPVOID pv)
805 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
807 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
808 CLSCTX_INPROC_SERVER, REGCLS_SINGLEUSE, &cookie);
815 static DWORD CALLBACK revoke_class_object_thread(LPVOID pv)
817 DWORD cookie = (DWORD_PTR)pv;
820 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
822 hr = CoRevokeClassObject(cookie);
829 static void test_registered_object_thread_affinity(void)
837 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
839 /* CLSCTX_INPROC_SERVER */
841 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
842 CLSCTX_INPROC_SERVER, REGCLS_SINGLEUSE, &cookie);
843 ok_ole_success(hr, "CoRegisterClassObject");
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);
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);
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);
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);
863 ok(hr == S_OK, "CoRegisterClassObject with same CLSID but in different thread should return S_OK instead of 0x%08x\n", hr);
865 hr = CoRevokeClassObject(cookie);
866 ok_ole_success(hr, "CoRevokeClassObject");
868 /* CLSCTX_LOCAL_SERVER */
870 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
871 CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE, &cookie);
872 ok_ole_success(hr, "CoRegisterClassObject");
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)
879 while (PeekMessageA(&msg, NULL, 0, 0, PM_REMOVE))
881 TranslateMessage(&msg);
882 DispatchMessageA(&msg);
885 GetExitCodeThread(thread, &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);
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);
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);
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);
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);
908 ok(hr == S_OK, "CoRegisterClassObject with same CLSID but in different "
909 "thread should return S_OK instead of 0x%08x\n", hr);
911 hr = CoRevokeClassObject(cookie);
912 ok_ole_success(hr, "CoRevokeClassObject");
917 static DWORD CALLBACK free_libraries_thread(LPVOID p)
919 CoFreeUnusedLibraries();
923 static inline BOOL is_module_loaded(const char *module)
925 return GetModuleHandle(module) ? TRUE : FALSE;
928 static void test_CoFreeUnusedLibraries(void)
935 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
937 ok(!is_module_loaded("urlmon.dll"), "urlmon.dll shouldn't be loaded\n");
939 hr = CoCreateInstance(&CLSID_FileProtocol, NULL, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void **)&pUnk);
940 if (hr == REGDB_E_CLASSNOTREG)
942 trace("IE not installed so can't run CoFreeUnusedLibraries test\n");
945 ok_ole_success(hr, "CoCreateInstance");
947 ok(is_module_loaded("urlmon.dll"), "urlmon.dll should be loaded\n");
950 broken(pUnk == NULL), /* win9x */
951 "Expected a valid pointer\n");
953 IUnknown_Release(pUnk);
955 ok(is_module_loaded("urlmon.dll"), "urlmon.dll should be loaded\n");
957 thread = CreateThread(NULL, 0, free_libraries_thread, NULL, 0, &tid);
958 WaitForSingleObject(thread, INFINITE);
961 ok(is_module_loaded("urlmon.dll"), "urlmon.dll should be loaded\n");
963 CoFreeUnusedLibraries();
965 ok(!is_module_loaded("urlmon.dll"), "urlmon.dll shouldn't be loaded\n");
970 static void test_CoGetObjectContext(void)
974 IComThreadingInfo *pComThreadingInfo;
978 if (!pCoGetObjectContext)
980 skip("CoGetObjectContext not present\n");
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");
988 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
990 hr = pCoGetObjectContext(&IID_IComThreadingInfo, (void **)&pComThreadingInfo);
991 ok_ole_success(hr, "CoGetObjectContext");
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);
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);
1001 refs = IComThreadingInfo_Release(pComThreadingInfo);
1002 ok(refs == 0, "pComThreadingInfo should have 0 refs instead of %d refs\n", refs);
1006 pCoInitializeEx(NULL, COINIT_MULTITHREADED);
1008 hr = pCoGetObjectContext(&IID_IComThreadingInfo, (void **)&pComThreadingInfo);
1009 ok_ole_success(hr, "CoGetObjectContext");
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);
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);
1019 refs = IComThreadingInfo_Release(pComThreadingInfo);
1020 ok(refs == 0, "pComThreadingInfo should have 0 refs instead of %d refs\n", refs);
1027 HMODULE hOle32 = GetModuleHandle("ole32");
1028 pCoGetObjectContext = (void*)GetProcAddress(hOle32, "CoGetObjectContext");
1029 if (!(pCoInitializeEx = (void*)GetProcAddress(hOle32, "CoInitializeEx")))
1031 trace("You need DCOM95 installed to run this test\n");
1035 test_ProgIDFromCLSID();
1036 test_CLSIDFromProgID();
1037 test_CLSIDFromString();
1038 test_StringFromGUID2();
1039 test_CoCreateInstance();
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();