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
28 #define USE_COM_CONTEXT_DEF
32 #include "urlmon.h" /* for CLSID_FileProtocol */
36 #include "wine/test.h"
38 /* functions that are not present on all versions of Windows */
39 HRESULT (WINAPI * pCoInitializeEx)(LPVOID lpReserved, DWORD dwCoInit);
40 HRESULT (WINAPI * pCoGetObjectContext)(REFIID riid, LPVOID *ppv);
41 HRESULT (WINAPI * pCoSwitchCallContext)(IUnknown *pObject, IUnknown **ppOldObject);
42 HRESULT (WINAPI * pCoGetTreatAsClass)(REFCLSID clsidOld, LPCLSID pClsidNew);
43 HRESULT (WINAPI * pCoGetContextToken)(ULONG_PTR *token);
45 #define ok_ole_success(hr, func) ok(hr == S_OK, func " failed with error 0x%08x\n", hr)
46 #define ok_more_than_one_lock() ok(cLocks > 0, "Number of locks should be > 0, but actually is %d\n", cLocks)
47 #define ok_no_locks() ok(cLocks == 0, "Number of locks should be 0, but actually is %d\n", cLocks)
49 static const CLSID CLSID_non_existent = { 0x12345678, 0x1234, 0x1234, { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 } };
50 static const CLSID CLSID_StdFont = { 0x0be35203, 0x8f91, 0x11ce, { 0x9d, 0xe3, 0x00, 0xaa, 0x00, 0x4b, 0xb8, 0x51 } };
51 static WCHAR stdfont[] = {'S','t','d','F','o','n','t',0};
52 static const WCHAR wszNonExistent[] = {'N','o','n','E','x','i','s','t','e','n','t',0};
53 static WCHAR wszCLSID_StdFont[] =
55 '{','0','b','e','3','5','2','0','3','-','8','f','9','1','-','1','1','c','e','-',
56 '9','d','e','3','-','0','0','a','a','0','0','4','b','b','8','5','1','}',0
59 static const IID IID_IWineTest =
64 {0xa1, 0xa2, 0x5d, 0x5a, 0x36, 0x54, 0xd3, 0xbd}
65 }; /* 5201163f-8164-4fd0-a1a2-5d5a3654d3bd */
66 static const CLSID CLSID_WineOOPTest = {
70 {0xa1, 0xa2, 0x5d, 0x5a, 0x36, 0x54, 0xd3, 0xbd}
71 }; /* 5201163f-8164-4fd0-a1a2-5d5a3654d3bd */
75 static void LockModule(void)
77 InterlockedIncrement(&cLocks);
80 static void UnlockModule(void)
82 InterlockedDecrement(&cLocks);
85 static HRESULT WINAPI Test_IClassFactory_QueryInterface(
90 if (ppvObj == NULL) return E_POINTER;
92 if (IsEqualGUID(riid, &IID_IUnknown) ||
93 IsEqualGUID(riid, &IID_IClassFactory))
96 IClassFactory_AddRef(iface);
101 return E_NOINTERFACE;
104 static ULONG WINAPI Test_IClassFactory_AddRef(LPCLASSFACTORY iface)
107 return 2; /* non-heap-based object */
110 static ULONG WINAPI Test_IClassFactory_Release(LPCLASSFACTORY iface)
113 return 1; /* non-heap-based object */
116 static HRESULT WINAPI Test_IClassFactory_CreateInstance(
117 LPCLASSFACTORY iface,
123 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
124 return E_NOINTERFACE;
127 static HRESULT WINAPI Test_IClassFactory_LockServer(
128 LPCLASSFACTORY iface,
134 static const IClassFactoryVtbl TestClassFactory_Vtbl =
136 Test_IClassFactory_QueryInterface,
137 Test_IClassFactory_AddRef,
138 Test_IClassFactory_Release,
139 Test_IClassFactory_CreateInstance,
140 Test_IClassFactory_LockServer
143 static IClassFactory Test_ClassFactory = { &TestClassFactory_Vtbl };
145 static void test_ProgIDFromCLSID(void)
148 HRESULT hr = ProgIDFromCLSID(&CLSID_StdFont, &progid);
149 ok(hr == S_OK, "ProgIDFromCLSID failed with error 0x%08x\n", hr);
152 ok(!lstrcmpiW(progid, stdfont), "Didn't get expected prog ID\n");
153 CoTaskMemFree(progid);
156 progid = (LPWSTR)0xdeadbeef;
157 hr = ProgIDFromCLSID(&CLSID_non_existent, &progid);
158 ok(hr == REGDB_E_CLASSNOTREG, "ProgIDFromCLSID returned %08x\n", hr);
159 ok(progid == NULL, "ProgIDFromCLSID returns with progid %p\n", progid);
161 hr = ProgIDFromCLSID(&CLSID_StdFont, NULL);
162 ok(hr == E_INVALIDARG, "ProgIDFromCLSID should return E_INVALIDARG instead of 0x%08x\n", hr);
165 static void test_CLSIDFromProgID(void)
168 HRESULT hr = CLSIDFromProgID(stdfont, &clsid);
169 ok(hr == S_OK, "CLSIDFromProgID failed with error 0x%08x\n", hr);
170 ok(IsEqualCLSID(&clsid, &CLSID_StdFont), "clsid wasn't equal to CLSID_StdFont\n");
172 hr = CLSIDFromString(stdfont, &clsid);
173 ok_ole_success(hr, "CLSIDFromString");
174 ok(IsEqualCLSID(&clsid, &CLSID_StdFont), "clsid wasn't equal to CLSID_StdFont\n");
176 /* test some failure cases */
178 hr = CLSIDFromProgID(wszNonExistent, NULL);
179 ok(hr == E_INVALIDARG, "CLSIDFromProgID should have returned E_INVALIDARG instead of 0x%08x\n", hr);
181 hr = CLSIDFromProgID(NULL, &clsid);
182 ok(hr == E_INVALIDARG, "CLSIDFromProgID should have returned E_INVALIDARG instead of 0x%08x\n", hr);
184 memset(&clsid, 0xcc, sizeof(clsid));
185 hr = CLSIDFromProgID(wszNonExistent, &clsid);
186 ok(hr == CO_E_CLASSSTRING, "CLSIDFromProgID on nonexistent ProgID should have returned CO_E_CLASSSTRING instead of 0x%08x\n", hr);
187 ok(IsEqualCLSID(&clsid, &CLSID_NULL), "CLSIDFromProgID should have set clsid to all-zeros on failure\n");
190 static void test_CLSIDFromString(void)
193 HRESULT hr = CLSIDFromString(wszCLSID_StdFont, &clsid);
194 ok_ole_success(hr, "CLSIDFromString");
195 ok(IsEqualCLSID(&clsid, &CLSID_StdFont), "clsid wasn't equal to CLSID_StdFont\n");
197 hr = CLSIDFromString(NULL, &clsid);
198 ok_ole_success(hr, "CLSIDFromString");
199 ok(IsEqualCLSID(&clsid, &CLSID_NULL), "clsid wasn't equal to CLSID_NULL\n");
202 static void test_StringFromGUID2(void)
207 /* invalid pointer */
208 SetLastError(0xdeadbeef);
209 len = StringFromGUID2(NULL,str,50);
210 ok(len == 0, "len: %d (expected 0)\n", len);
211 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %x\n", GetLastError());
213 /* Test corner cases for buffer size */
214 len = StringFromGUID2(&CLSID_StdFont,str,50);
215 ok(len == 39, "len: %d (expected 39)\n", len);
216 ok(!lstrcmpiW(str, wszCLSID_StdFont),"string wasn't equal for CLSID_StdFont\n");
218 memset(str,0,sizeof str);
219 len = StringFromGUID2(&CLSID_StdFont,str,39);
220 ok(len == 39, "len: %d (expected 39)\n", len);
221 ok(!lstrcmpiW(str, wszCLSID_StdFont),"string wasn't equal for CLSID_StdFont\n");
223 len = StringFromGUID2(&CLSID_StdFont,str,38);
224 ok(len == 0, "len: %d (expected 0)\n", len);
226 len = StringFromGUID2(&CLSID_StdFont,str,30);
227 ok(len == 0, "len: %d (expected 0)\n", len);
235 static DWORD CALLBACK ole_initialize_thread(LPVOID pv)
238 struct info *info = pv;
240 hr = pCoInitializeEx(NULL, COINIT_MULTITHREADED);
242 SetEvent(info->wait);
243 WaitForSingleObject(info->stop, 10000);
249 static void test_CoCreateInstance(void)
256 REFCLSID rclsid = &CLSID_InternetZoneManager;
258 pUnk = (IUnknown *)0xdeadbeef;
259 hr = CoCreateInstance(rclsid, NULL, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void **)&pUnk);
260 ok(hr == CO_E_NOTINITIALIZED, "CoCreateInstance should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n", hr);
261 ok(pUnk == NULL, "CoCreateInstance should have changed the passed in pointer to NULL, instead of %p\n", pUnk);
264 hr = CoCreateInstance(rclsid, NULL, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void **)&pUnk);
265 if(hr == REGDB_E_CLASSNOTREG)
267 skip("IE not installed so can't test CoCreateInstance\n");
272 ok_ole_success(hr, "CoCreateInstance");
273 if(pUnk) IUnknown_Release(pUnk);
276 hr = CoCreateInstance(rclsid, NULL, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void **)&pUnk);
277 ok(hr == CO_E_NOTINITIALIZED, "CoCreateInstance should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n", hr);
279 /* show that COM doesn't have to be initialized for multi-threaded apartments if another
280 thread has already done so */
282 info.wait = CreateEvent(NULL, TRUE, FALSE, NULL);
283 ok(info.wait != NULL, "CreateEvent failed with error %d\n", GetLastError());
285 info.stop = CreateEvent(NULL, TRUE, FALSE, NULL);
286 ok(info.stop != NULL, "CreateEvent failed with error %d\n", GetLastError());
288 thread = CreateThread(NULL, 0, ole_initialize_thread, &info, 0, &tid);
289 ok(thread != NULL, "CreateThread failed with error %d\n", GetLastError());
291 ok( !WaitForSingleObject(info.wait, 10000 ), "wait timed out\n" );
293 pUnk = (IUnknown *)0xdeadbeef;
294 hr = CoCreateInstance(rclsid, NULL, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void **)&pUnk);
295 ok(hr == S_OK, "CoCreateInstance should have returned S_OK instead of 0x%08x\n", hr);
296 if (pUnk) IUnknown_Release(pUnk);
299 ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
301 GetExitCodeThread(thread, &exitcode);
303 ok(hr == S_OK, "thread should have returned S_OK instead of 0x%08x\n", hr);
306 CloseHandle(info.wait);
307 CloseHandle(info.stop);
310 static void test_CoGetClassObject(void)
317 REFCLSID rclsid = &CLSID_InternetZoneManager;
319 hr = CoGetClassObject(rclsid, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void **)&pUnk);
320 ok(hr == CO_E_NOTINITIALIZED, "CoGetClassObject should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n", hr);
321 ok(pUnk == NULL, "CoGetClassObject should have changed the passed in pointer to NULL, instead of %p\n", pUnk);
323 hr = CoGetClassObject(rclsid, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, NULL);
324 ok(hr == E_INVALIDARG ||
325 broken(hr == CO_E_NOTINITIALIZED), /* win9x */
326 "CoGetClassObject should have returned E_INVALIDARG instead of 0x%08x\n", hr);
328 /* show that COM doesn't have to be initialized for multi-threaded apartments if another
329 thread has already done so */
331 info.wait = CreateEvent(NULL, TRUE, FALSE, NULL);
332 ok(info.wait != NULL, "CreateEvent failed with error %d\n", GetLastError());
334 info.stop = CreateEvent(NULL, TRUE, FALSE, NULL);
335 ok(info.stop != NULL, "CreateEvent failed with error %d\n", GetLastError());
337 thread = CreateThread(NULL, 0, ole_initialize_thread, &info, 0, &tid);
338 ok(thread != NULL, "CreateThread failed with error %d\n", GetLastError());
340 ok( !WaitForSingleObject(info.wait, 10000), "wait timed out\n" );
342 pUnk = (IUnknown *)0xdeadbeef;
343 hr = CoGetClassObject(rclsid, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void **)&pUnk);
344 if(hr == REGDB_E_CLASSNOTREG)
345 skip("IE not installed so can't test CoGetClassObject\n");
348 ok(hr == S_OK, "CoGetClassObject should have returned S_OK instead of 0x%08x\n", hr);
349 if (pUnk) IUnknown_Release(pUnk);
353 ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
355 GetExitCodeThread(thread, &exitcode);
357 ok(hr == S_OK, "thread should have returned S_OK instead of 0x%08x\n", hr);
360 CloseHandle(info.wait);
361 CloseHandle(info.stop);
364 static ATOM register_dummy_class(void)
372 GetModuleHandle(NULL),
374 LoadCursor(NULL, IDC_ARROW),
375 (HBRUSH)(COLOR_BTNFACE+1),
377 TEXT("WineOleTestClass"),
380 return RegisterClass(&wc);
383 static void test_ole_menu(void)
388 hwndFrame = CreateWindow(MAKEINTATOM(register_dummy_class()), "Test", 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, NULL, NULL);
389 hr = OleSetMenuDescriptor(NULL, hwndFrame, NULL, NULL, NULL);
390 todo_wine ok_ole_success(hr, "OleSetMenuDescriptor");
392 DestroyWindow(hwndFrame);
396 static HRESULT WINAPI MessageFilter_QueryInterface(IMessageFilter *iface, REFIID riid, void ** ppvObj)
398 if (ppvObj == NULL) return E_POINTER;
400 if (IsEqualGUID(riid, &IID_IUnknown) ||
401 IsEqualGUID(riid, &IID_IClassFactory))
404 IMessageFilter_AddRef(iface);
408 return E_NOINTERFACE;
411 static ULONG WINAPI MessageFilter_AddRef(IMessageFilter *iface)
413 return 2; /* non-heap object */
416 static ULONG WINAPI MessageFilter_Release(IMessageFilter *iface)
418 return 1; /* non-heap object */
421 static DWORD WINAPI MessageFilter_HandleInComingCall(
422 IMessageFilter *iface,
424 HTASK threadIDCaller,
426 LPINTERFACEINFO lpInterfaceInfo)
428 trace("HandleInComingCall\n");
429 return SERVERCALL_ISHANDLED;
432 static DWORD WINAPI MessageFilter_RetryRejectedCall(
433 IMessageFilter *iface,
434 HTASK threadIDCallee,
438 trace("RetryRejectedCall\n");
442 static DWORD WINAPI MessageFilter_MessagePending(
443 IMessageFilter *iface,
444 HTASK threadIDCallee,
448 trace("MessagePending\n");
449 return PENDINGMSG_WAITNOPROCESS;
452 static const IMessageFilterVtbl MessageFilter_Vtbl =
454 MessageFilter_QueryInterface,
455 MessageFilter_AddRef,
456 MessageFilter_Release,
457 MessageFilter_HandleInComingCall,
458 MessageFilter_RetryRejectedCall,
459 MessageFilter_MessagePending
462 static IMessageFilter MessageFilter = { &MessageFilter_Vtbl };
464 static void test_CoRegisterMessageFilter(void)
467 IMessageFilter *prev_filter;
469 hr = CoRegisterMessageFilter(&MessageFilter, &prev_filter);
470 ok(hr == CO_E_NOT_SUPPORTED,
471 "CoRegisterMessageFilter should have failed with CO_E_NOT_SUPPORTED instead of 0x%08x\n",
474 pCoInitializeEx(NULL, COINIT_MULTITHREADED);
475 prev_filter = (IMessageFilter *)0xdeadbeef;
476 hr = CoRegisterMessageFilter(&MessageFilter, &prev_filter);
477 ok(hr == CO_E_NOT_SUPPORTED,
478 "CoRegisterMessageFilter should have failed with CO_E_NOT_SUPPORTED instead of 0x%08x\n",
480 ok(prev_filter == (IMessageFilter *)0xdeadbeef,
481 "prev_filter should have been set to %p\n", prev_filter);
484 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
486 hr = CoRegisterMessageFilter(NULL, NULL);
487 ok_ole_success(hr, "CoRegisterMessageFilter");
489 prev_filter = (IMessageFilter *)0xdeadbeef;
490 hr = CoRegisterMessageFilter(NULL, &prev_filter);
491 ok_ole_success(hr, "CoRegisterMessageFilter");
492 ok(prev_filter == NULL, "prev_filter should have been set to NULL instead of %p\n", prev_filter);
494 hr = CoRegisterMessageFilter(&MessageFilter, &prev_filter);
495 ok_ole_success(hr, "CoRegisterMessageFilter");
496 ok(prev_filter == NULL, "prev_filter should have been set to NULL instead of %p\n", prev_filter);
498 hr = CoRegisterMessageFilter(NULL, NULL);
499 ok_ole_success(hr, "CoRegisterMessageFilter");
504 static HRESULT WINAPI Test_IUnknown_QueryInterface(
509 if (ppvObj == NULL) return E_POINTER;
511 if (IsEqualIID(riid, &IID_IUnknown) ||
512 IsEqualIID(riid, &IID_IWineTest))
515 IUnknown_AddRef(iface);
520 return E_NOINTERFACE;
523 static ULONG WINAPI Test_IUnknown_AddRef(LPUNKNOWN iface)
525 return 2; /* non-heap-based object */
528 static ULONG WINAPI Test_IUnknown_Release(LPUNKNOWN iface)
530 return 1; /* non-heap-based object */
533 static const IUnknownVtbl TestUnknown_Vtbl =
535 Test_IUnknown_QueryInterface,
536 Test_IUnknown_AddRef,
537 Test_IUnknown_Release,
540 static IUnknown Test_Unknown = { &TestUnknown_Vtbl };
542 static HRESULT WINAPI PSFactoryBuffer_QueryInterface(
543 IPSFactoryBuffer * This,
544 /* [in] */ REFIID riid,
545 /* [iid_is][out] */ void **ppvObject)
547 if (IsEqualIID(riid, &IID_IUnknown) ||
548 IsEqualIID(riid, &IID_IPSFactoryBuffer))
551 IPSFactoryBuffer_AddRef(This);
554 return E_NOINTERFACE;
557 static ULONG WINAPI PSFactoryBuffer_AddRef(
558 IPSFactoryBuffer * This)
563 static ULONG WINAPI PSFactoryBuffer_Release(
564 IPSFactoryBuffer * This)
569 static HRESULT WINAPI PSFactoryBuffer_CreateProxy(
570 IPSFactoryBuffer * This,
571 /* [in] */ IUnknown *pUnkOuter,
572 /* [in] */ REFIID riid,
573 /* [out] */ IRpcProxyBuffer **ppProxy,
574 /* [out] */ void **ppv)
579 static HRESULT WINAPI PSFactoryBuffer_CreateStub(
580 IPSFactoryBuffer * This,
581 /* [in] */ REFIID riid,
582 /* [unique][in] */ IUnknown *pUnkServer,
583 /* [out] */ IRpcStubBuffer **ppStub)
588 static IPSFactoryBufferVtbl PSFactoryBufferVtbl =
590 PSFactoryBuffer_QueryInterface,
591 PSFactoryBuffer_AddRef,
592 PSFactoryBuffer_Release,
593 PSFactoryBuffer_CreateProxy,
594 PSFactoryBuffer_CreateStub
597 static IPSFactoryBuffer PSFactoryBuffer = { &PSFactoryBufferVtbl };
599 static const CLSID CLSID_WineTestPSFactoryBuffer =
604 {0xa1, 0xa2, 0x5d, 0x5a, 0x36, 0x54, 0xd3, 0xbd}
605 }; /* 52011640-8164-4fd0-a1a2-5d5a3654d3bd */
607 static void test_CoRegisterPSClsid(void)
610 DWORD dwRegistrationKey;
614 hr = CoRegisterPSClsid(&IID_IWineTest, &CLSID_WineTestPSFactoryBuffer);
615 ok(hr == CO_E_NOTINITIALIZED, "CoRegisterPSClsid should have returened CO_E_NOTINITIALIZED instead of 0x%08x\n", hr);
617 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
619 hr = CoRegisterClassObject(&CLSID_WineTestPSFactoryBuffer, (IUnknown *)&PSFactoryBuffer,
620 CLSCTX_INPROC_SERVER, REGCLS_MULTIPLEUSE, &dwRegistrationKey);
621 ok_ole_success(hr, "CoRegisterClassObject");
623 hr = CoRegisterPSClsid(&IID_IWineTest, &CLSID_WineTestPSFactoryBuffer);
624 ok_ole_success(hr, "CoRegisterPSClsid");
626 hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
627 ok_ole_success(hr, "CreateStreamOnHGlobal");
629 hr = CoMarshalInterface(stream, &IID_IWineTest, &Test_Unknown, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
630 ok(hr == E_NOTIMPL, "CoMarshalInterface should have returned E_NOTIMPL instead of 0x%08x\n", hr);
631 IStream_Release(stream);
633 hr = CoRevokeClassObject(dwRegistrationKey);
634 ok_ole_success(hr, "CoRevokeClassObject");
638 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
640 hr = CoGetPSClsid(&IID_IWineTest, &clsid);
641 ok(hr == REGDB_E_IIDNOTREG, "CoGetPSClsid should have returned REGDB_E_IIDNOTREG instead of 0x%08x\n", hr);
646 static void test_CoGetPSClsid(void)
651 hr = CoGetPSClsid(&IID_IClassFactory, &clsid);
652 ok(hr == CO_E_NOTINITIALIZED,
653 "CoGetPSClsid should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n",
656 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
658 hr = CoGetPSClsid(&IID_IClassFactory, &clsid);
659 ok_ole_success(hr, "CoGetPSClsid");
661 hr = CoGetPSClsid(&IID_IWineTest, &clsid);
662 ok(hr == REGDB_E_IIDNOTREG,
663 "CoGetPSClsid for random IID returned 0x%08x instead of REGDB_E_IIDNOTREG\n",
666 hr = CoGetPSClsid(&IID_IClassFactory, NULL);
667 ok(hr == E_INVALIDARG,
668 "CoGetPSClsid for null clsid returned 0x%08x instead of E_INVALIDARG\n",
674 /* basic test, mainly for invalid arguments. see marshal.c for more */
675 static void test_CoUnmarshalInterface(void)
681 hr = CoUnmarshalInterface(NULL, &IID_IUnknown, (void **)&pProxy);
682 ok(hr == E_INVALIDARG, "CoUnmarshalInterface should have returned E_INVALIDARG instead of 0x%08x\n", hr);
684 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
685 ok_ole_success(hr, "CreateStreamOnHGlobal");
687 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)&pProxy);
689 ok(hr == CO_E_NOTINITIALIZED, "CoUnmarshalInterface should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n", hr);
691 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
693 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)&pProxy);
694 ok(hr == STG_E_READFAULT, "CoUnmarshalInterface should have returned STG_E_READFAULT instead of 0x%08x\n", hr);
698 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, NULL);
699 ok(hr == E_INVALIDARG, "CoUnmarshalInterface should have returned E_INVALIDARG instead of 0x%08x\n", hr);
701 IStream_Release(pStream);
704 static void test_CoGetInterfaceAndReleaseStream(void)
709 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
711 hr = CoGetInterfaceAndReleaseStream(NULL, &IID_IUnknown, (void**)&pUnk);
712 ok(hr == E_INVALIDARG, "hr %08x\n", hr);
717 /* basic test, mainly for invalid arguments. see marshal.c for more */
718 static void test_CoMarshalInterface(void)
722 static const LARGE_INTEGER llZero;
724 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
726 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
727 ok_ole_success(hr, "CreateStreamOnHGlobal");
729 hr = CoMarshalInterface(pStream, &IID_IUnknown, NULL, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
730 ok(hr == E_INVALIDARG, "CoMarshalInterface should have returned E_INVALIDARG instead of 0x%08x\n", hr);
732 hr = CoMarshalInterface(NULL, &IID_IUnknown, (IUnknown *)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
733 ok(hr == E_INVALIDARG, "CoMarshalInterface should have returned E_INVALIDARG instead of 0x%08x\n", hr);
735 hr = CoMarshalInterface(pStream, &IID_IUnknown, (IUnknown *)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
736 ok_ole_success(hr, "CoMarshalInterface");
738 /* stream not rewound */
739 hr = CoReleaseMarshalData(pStream);
740 ok(hr == STG_E_READFAULT, "CoReleaseMarshalData should have returned STG_E_READFAULT instead of 0x%08x\n", hr);
742 hr = IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
743 ok_ole_success(hr, "IStream_Seek");
745 hr = CoReleaseMarshalData(pStream);
746 ok_ole_success(hr, "CoReleaseMarshalData");
748 IStream_Release(pStream);
753 static void test_CoMarshalInterThreadInterfaceInStream(void)
757 IClassFactory *pProxy;
759 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
763 hr = CoMarshalInterThreadInterfaceInStream(&IID_IUnknown, (IUnknown *)&Test_ClassFactory, NULL);
764 ok(hr == E_INVALIDARG, "CoMarshalInterThreadInterfaceInStream should have returned E_INVALIDARG instead of 0x%08x\n", hr);
766 hr = CoMarshalInterThreadInterfaceInStream(&IID_IUnknown, NULL, &pStream);
767 ok(hr == E_INVALIDARG, "CoMarshalInterThreadInterfaceInStream should have returned E_INVALIDARG instead of 0x%08x\n", hr);
771 hr = CoMarshalInterThreadInterfaceInStream(&IID_IUnknown, (IUnknown *)&Test_ClassFactory, &pStream);
772 ok_ole_success(hr, "CoMarshalInterThreadInterfaceInStream");
774 ok_more_than_one_lock();
776 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
777 ok_ole_success(hr, "CoUnmarshalInterface");
779 IClassFactory_Release(pProxy);
780 IStream_Release(pStream);
787 static void test_CoRegisterClassObject(void)
793 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
795 /* CLSCTX_INPROC_SERVER */
796 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
797 CLSCTX_INPROC_SERVER, REGCLS_SINGLEUSE, &cookie);
798 ok_ole_success(hr, "CoRegisterClassObject");
799 hr = CoRevokeClassObject(cookie);
800 ok_ole_success(hr, "CoRevokeClassObject");
802 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
803 CLSCTX_INPROC_SERVER, REGCLS_MULTIPLEUSE, &cookie);
804 ok_ole_success(hr, "CoRegisterClassObject");
805 hr = CoRevokeClassObject(cookie);
806 ok_ole_success(hr, "CoRevokeClassObject");
808 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
809 CLSCTX_INPROC_SERVER, REGCLS_MULTI_SEPARATE, &cookie);
810 ok_ole_success(hr, "CoRegisterClassObject");
811 hr = CoRevokeClassObject(cookie);
812 ok_ole_success(hr, "CoRevokeClassObject");
814 /* CLSCTX_LOCAL_SERVER */
815 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
816 CLSCTX_LOCAL_SERVER, REGCLS_SINGLEUSE, &cookie);
817 ok_ole_success(hr, "CoRegisterClassObject");
818 hr = CoRevokeClassObject(cookie);
819 ok_ole_success(hr, "CoRevokeClassObject");
821 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
822 CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE, &cookie);
823 ok_ole_success(hr, "CoRegisterClassObject");
824 hr = CoRevokeClassObject(cookie);
825 ok_ole_success(hr, "CoRevokeClassObject");
827 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
828 CLSCTX_LOCAL_SERVER, REGCLS_MULTI_SEPARATE, &cookie);
829 ok_ole_success(hr, "CoRegisterClassObject");
830 hr = CoRevokeClassObject(cookie);
831 ok_ole_success(hr, "CoRevokeClassObject");
833 /* CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER */
834 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
835 CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, REGCLS_SINGLEUSE, &cookie);
836 ok_ole_success(hr, "CoRegisterClassObject");
837 hr = CoRevokeClassObject(cookie);
838 ok_ole_success(hr, "CoRevokeClassObject");
840 /* test whether an object that doesn't support IClassFactory can be
841 * registered for CLSCTX_LOCAL_SERVER */
842 hr = CoRegisterClassObject(&CLSID_WineOOPTest, &Test_Unknown,
843 CLSCTX_LOCAL_SERVER, REGCLS_SINGLEUSE, &cookie);
844 ok_ole_success(hr, "CoRegisterClassObject");
845 hr = CoRevokeClassObject(cookie);
846 ok_ole_success(hr, "CoRevokeClassObject");
848 /* test whether registered class becomes invalid when apartment is destroyed */
849 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
850 CLSCTX_INPROC_SERVER, REGCLS_SINGLEUSE, &cookie);
851 ok_ole_success(hr, "CoRegisterClassObject");
854 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
856 hr = CoGetClassObject(&CLSID_WineOOPTest, CLSCTX_INPROC_SERVER, NULL,
857 &IID_IClassFactory, (void **)&pcf);
858 ok(hr == REGDB_E_CLASSNOTREG, "object registered in an apartment shouldn't accessible after it is destroyed\n");
860 /* crashes with at least win9x DCOM! */
862 hr = CoRevokeClassObject(cookie);
867 static HRESULT get_class_object(CLSCTX clsctx)
872 hr = CoGetClassObject(&CLSID_WineOOPTest, clsctx, NULL, &IID_IClassFactory,
876 IClassFactory_Release(pcf);
881 static DWORD CALLBACK get_class_object_thread(LPVOID pv)
883 CLSCTX clsctx = (CLSCTX)(DWORD_PTR)pv;
886 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
888 hr = get_class_object(clsctx);
895 static DWORD CALLBACK get_class_object_proxy_thread(LPVOID pv)
897 CLSCTX clsctx = (CLSCTX)(DWORD_PTR)pv;
902 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
904 hr = CoGetClassObject(&CLSID_WineOOPTest, clsctx, NULL, &IID_IClassFactory,
909 hr = IClassFactory_QueryInterface(pcf, &IID_IMultiQI, (void **)&pMQI);
911 IMultiQI_Release(pMQI);
912 IClassFactory_Release(pcf);
920 static DWORD CALLBACK register_class_object_thread(LPVOID pv)
925 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
927 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
928 CLSCTX_INPROC_SERVER, REGCLS_SINGLEUSE, &cookie);
935 static DWORD CALLBACK revoke_class_object_thread(LPVOID pv)
937 DWORD cookie = (DWORD_PTR)pv;
940 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
942 hr = CoRevokeClassObject(cookie);
949 static void test_registered_object_thread_affinity(void)
957 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
959 /* CLSCTX_INPROC_SERVER */
961 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
962 CLSCTX_INPROC_SERVER, REGCLS_SINGLEUSE, &cookie);
963 ok_ole_success(hr, "CoRegisterClassObject");
965 thread = CreateThread(NULL, 0, get_class_object_thread, (LPVOID)CLSCTX_INPROC_SERVER, 0, &tid);
966 ok(thread != NULL, "CreateThread failed with error %d\n", GetLastError());
967 ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
968 GetExitCodeThread(thread, &exitcode);
970 ok(hr == REGDB_E_CLASSNOTREG, "CoGetClassObject on inproc object "
971 "registered in different thread should return REGDB_E_CLASSNOTREG "
972 "instead of 0x%08x\n", hr);
974 hr = get_class_object(CLSCTX_INPROC_SERVER);
975 ok(hr == S_OK, "CoGetClassObject on inproc object registered in same "
976 "thread should return S_OK instead of 0x%08x\n", hr);
978 thread = CreateThread(NULL, 0, register_class_object_thread, NULL, 0, &tid);
979 ok(thread != NULL, "CreateThread failed with error %d\n", GetLastError());
980 ok ( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
981 GetExitCodeThread(thread, &exitcode);
983 ok(hr == S_OK, "CoRegisterClassObject with same CLSID but in different thread should return S_OK instead of 0x%08x\n", hr);
985 hr = CoRevokeClassObject(cookie);
986 ok_ole_success(hr, "CoRevokeClassObject");
988 /* CLSCTX_LOCAL_SERVER */
990 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
991 CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE, &cookie);
992 ok_ole_success(hr, "CoRegisterClassObject");
994 thread = CreateThread(NULL, 0, get_class_object_proxy_thread, (LPVOID)CLSCTX_LOCAL_SERVER, 0, &tid);
995 ok(thread != NULL, "CreateThread failed with error %d\n", GetLastError());
996 while (MsgWaitForMultipleObjects(1, &thread, FALSE, 10000, QS_ALLINPUT) == WAIT_OBJECT_0 + 1)
999 while (PeekMessageA(&msg, NULL, 0, 0, PM_REMOVE))
1001 TranslateMessage(&msg);
1002 DispatchMessageA(&msg);
1005 GetExitCodeThread(thread, &exitcode);
1007 ok(hr == S_OK, "CoGetClassObject on local server object "
1008 "registered in different thread should return S_OK "
1009 "instead of 0x%08x\n", hr);
1011 hr = get_class_object(CLSCTX_LOCAL_SERVER);
1012 ok(hr == S_OK, "CoGetClassObject on local server object registered in same "
1013 "thread should return S_OK instead of 0x%08x\n", hr);
1015 thread = CreateThread(NULL, 0, revoke_class_object_thread, (LPVOID)(DWORD_PTR)cookie, 0, &tid);
1016 ok(thread != NULL, "CreateThread failed with error %d\n", GetLastError());
1017 ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
1018 GetExitCodeThread(thread, &exitcode);
1020 ok(hr == RPC_E_WRONG_THREAD, "CoRevokeClassObject called from different "
1021 "thread to where registered should return RPC_E_WRONG_THREAD instead of 0x%08x\n", hr);
1023 thread = CreateThread(NULL, 0, register_class_object_thread, NULL, 0, &tid);
1024 ok(thread != NULL, "CreateThread failed with error %d\n", GetLastError());
1025 ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
1026 GetExitCodeThread(thread, &exitcode);
1028 ok(hr == S_OK, "CoRegisterClassObject with same CLSID but in different "
1029 "thread should return S_OK instead of 0x%08x\n", hr);
1031 hr = CoRevokeClassObject(cookie);
1032 ok_ole_success(hr, "CoRevokeClassObject");
1037 static DWORD CALLBACK free_libraries_thread(LPVOID p)
1039 CoFreeUnusedLibraries();
1043 static inline BOOL is_module_loaded(const char *module)
1045 return GetModuleHandle(module) ? TRUE : FALSE;
1048 static void test_CoFreeUnusedLibraries(void)
1055 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1057 ok(!is_module_loaded("urlmon.dll"), "urlmon.dll shouldn't be loaded\n");
1059 hr = CoCreateInstance(&CLSID_FileProtocol, NULL, CLSCTX_INPROC_SERVER, &IID_IInternetProtocol, (void **)&pUnk);
1060 if (hr == REGDB_E_CLASSNOTREG)
1062 skip("IE not installed so can't run CoFreeUnusedLibraries test\n");
1066 ok_ole_success(hr, "CoCreateInstance");
1068 ok(is_module_loaded("urlmon.dll"), "urlmon.dll should be loaded\n");
1071 broken(pUnk == NULL), /* win9x */
1072 "Expected a valid pointer\n");
1074 IUnknown_Release(pUnk);
1076 ok(is_module_loaded("urlmon.dll"), "urlmon.dll should be loaded\n");
1078 thread = CreateThread(NULL, 0, free_libraries_thread, NULL, 0, &tid);
1079 ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
1080 CloseHandle(thread);
1082 ok(is_module_loaded("urlmon.dll"), "urlmon.dll should be loaded\n");
1084 CoFreeUnusedLibraries();
1086 ok(!is_module_loaded("urlmon.dll"), "urlmon.dll shouldn't be loaded\n");
1091 static void test_CoGetObjectContext(void)
1095 IComThreadingInfo *pComThreadingInfo;
1096 IContextCallback *pContextCallback;
1097 IObjContext *pObjContext;
1102 DWORD tid, exitcode;
1104 if (!pCoGetObjectContext)
1106 skip("CoGetObjectContext not present\n");
1110 hr = pCoGetObjectContext(&IID_IComThreadingInfo, (void **)&pComThreadingInfo);
1111 ok(hr == CO_E_NOTINITIALIZED, "CoGetObjectContext should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n", hr);
1112 ok(pComThreadingInfo == NULL, "pComThreadingInfo should have been set to NULL\n");
1114 /* show that COM doesn't have to be initialized for multi-threaded apartments if another
1115 thread has already done so */
1117 info.wait = CreateEvent(NULL, TRUE, FALSE, NULL);
1118 ok(info.wait != NULL, "CreateEvent failed with error %d\n", GetLastError());
1120 info.stop = CreateEvent(NULL, TRUE, FALSE, NULL);
1121 ok(info.stop != NULL, "CreateEvent failed with error %d\n", GetLastError());
1123 thread = CreateThread(NULL, 0, ole_initialize_thread, &info, 0, &tid);
1124 ok(thread != NULL, "CreateThread failed with error %d\n", GetLastError());
1126 ok( !WaitForSingleObject(info.wait, 10000), "wait timed out\n" );
1128 pComThreadingInfo = NULL;
1129 hr = pCoGetObjectContext(&IID_IComThreadingInfo, (void **)&pComThreadingInfo);
1130 ok(hr == S_OK, "Expected S_OK, got 0x%08x\n", hr);
1131 IComThreadingInfo_Release(pComThreadingInfo);
1133 SetEvent(info.stop);
1134 ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
1136 GetExitCodeThread(thread, &exitcode);
1138 ok(hr == S_OK, "thread should have returned S_OK instead of 0x%08x\n", hr);
1140 CloseHandle(thread);
1141 CloseHandle(info.wait);
1142 CloseHandle(info.stop);
1144 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1146 hr = pCoGetObjectContext(&IID_IComThreadingInfo, (void **)&pComThreadingInfo);
1147 ok_ole_success(hr, "CoGetObjectContext");
1149 hr = IComThreadingInfo_GetCurrentApartmentType(pComThreadingInfo, &apttype);
1150 ok_ole_success(hr, "IComThreadingInfo_GetCurrentApartmentType");
1151 ok(apttype == APTTYPE_MAINSTA, "apartment type should be APTTYPE_MAINSTA instead of %d\n", apttype);
1153 hr = IComThreadingInfo_GetCurrentThreadType(pComThreadingInfo, &thdtype);
1154 ok_ole_success(hr, "IComThreadingInfo_GetCurrentThreadType");
1155 ok(thdtype == THDTYPE_PROCESSMESSAGES, "thread type should be THDTYPE_PROCESSMESSAGES instead of %d\n", thdtype);
1157 refs = IComThreadingInfo_Release(pComThreadingInfo);
1158 ok(refs == 0, "pComThreadingInfo should have 0 refs instead of %d refs\n", refs);
1160 hr = pCoGetObjectContext(&IID_IContextCallback, (void **)&pContextCallback);
1161 ok_ole_success(hr, "CoGetObjectContext(ContextCallback)");
1165 refs = IContextCallback_Release(pContextCallback);
1166 ok(refs == 0, "pContextCallback should have 0 refs instead of %d refs\n", refs);
1171 pCoInitializeEx(NULL, COINIT_MULTITHREADED);
1173 hr = pCoGetObjectContext(&IID_IComThreadingInfo, (void **)&pComThreadingInfo);
1174 ok_ole_success(hr, "CoGetObjectContext");
1176 hr = IComThreadingInfo_GetCurrentApartmentType(pComThreadingInfo, &apttype);
1177 ok_ole_success(hr, "IComThreadingInfo_GetCurrentApartmentType");
1178 ok(apttype == APTTYPE_MTA, "apartment type should be APTTYPE_MTA instead of %d\n", apttype);
1180 hr = IComThreadingInfo_GetCurrentThreadType(pComThreadingInfo, &thdtype);
1181 ok_ole_success(hr, "IComThreadingInfo_GetCurrentThreadType");
1182 ok(thdtype == THDTYPE_BLOCKMESSAGES, "thread type should be THDTYPE_BLOCKMESSAGES instead of %d\n", thdtype);
1184 refs = IComThreadingInfo_Release(pComThreadingInfo);
1185 ok(refs == 0, "pComThreadingInfo should have 0 refs instead of %d refs\n", refs);
1187 hr = pCoGetObjectContext(&IID_IContextCallback, (void **)&pContextCallback);
1188 ok_ole_success(hr, "CoGetObjectContext(ContextCallback)");
1192 refs = IContextCallback_Release(pContextCallback);
1193 ok(refs == 0, "pContextCallback should have 0 refs instead of %d refs\n", refs);
1196 hr = pCoGetObjectContext(&IID_IObjContext, (void **)&pObjContext);
1197 ok_ole_success(hr, "CoGetObjectContext");
1199 refs = IObjContext_Release(pObjContext);
1200 ok(refs == 0, "pObjContext should have 0 refs instead of %d refs\n", refs);
1206 const IUnknownVtbl *lpVtbl;
1210 static HRESULT WINAPI Test_CallContext_QueryInterface(
1215 if (ppvObj == NULL) return E_POINTER;
1217 if (IsEqualGUID(riid, &IID_IUnknown))
1220 IUnknown_AddRef(iface);
1225 return E_NOINTERFACE;
1228 static ULONG WINAPI Test_CallContext_AddRef(IUnknown *iface)
1230 Test_CallContext *This = (Test_CallContext*)iface;
1231 return InterlockedIncrement(&This->refs);
1234 static ULONG WINAPI Test_CallContext_Release(IUnknown *iface)
1236 Test_CallContext *This = (Test_CallContext*)iface;
1237 ULONG refs = InterlockedDecrement(&This->refs);
1239 HeapFree(GetProcessHeap(), 0, This);
1243 static const IUnknownVtbl TestCallContext_Vtbl =
1245 Test_CallContext_QueryInterface,
1246 Test_CallContext_AddRef,
1247 Test_CallContext_Release
1250 static void test_CoGetCallContext(void)
1255 IUnknown *test_object;
1257 if (!pCoSwitchCallContext)
1259 skip("CoSwitchCallContext not present\n");
1265 test_object = HeapAlloc(GetProcessHeap(), 0, sizeof(Test_CallContext));
1266 ((Test_CallContext*)test_object)->lpVtbl = &TestCallContext_Vtbl;
1267 ((Test_CallContext*)test_object)->refs = 1;
1269 hr = CoGetCallContext(&IID_IUnknown, (void**)&pUnk);
1270 ok(hr == RPC_E_CALL_COMPLETE, "Expected RPC_E_CALL_COMPLETE, got 0x%08x\n", hr);
1272 pUnk = (IUnknown*)0xdeadbeef;
1273 hr = pCoSwitchCallContext(test_object, &pUnk);
1274 ok_ole_success(hr, "CoSwitchCallContext");
1275 ok(pUnk == NULL, "expected NULL, got %p\n", pUnk);
1276 refs = IUnknown_AddRef(test_object);
1277 ok(refs == 2, "Expected refcount 2, got %d\n", refs);
1278 IUnknown_Release(test_object);
1280 pUnk = (IUnknown*)0xdeadbeef;
1281 hr = CoGetCallContext(&IID_IUnknown, (void**)&pUnk);
1282 ok_ole_success(hr, "CoGetCallContext");
1283 ok(pUnk == test_object, "expected %p, got %p\n", test_object, pUnk);
1284 refs = IUnknown_AddRef(test_object);
1285 ok(refs == 3, "Expected refcount 3, got %d\n", refs);
1286 IUnknown_Release(test_object);
1287 IUnknown_Release(pUnk);
1289 pUnk = (IUnknown*)0xdeadbeef;
1290 hr = pCoSwitchCallContext(NULL, &pUnk);
1291 ok_ole_success(hr, "CoSwitchCallContext");
1292 ok(pUnk == test_object, "expected %p, got %p\n", test_object, pUnk);
1293 refs = IUnknown_AddRef(test_object);
1294 ok(refs == 2, "Expected refcount 2, got %d\n", refs);
1295 IUnknown_Release(test_object);
1297 hr = CoGetCallContext(&IID_IUnknown, (void**)&pUnk);
1298 ok(hr == RPC_E_CALL_COMPLETE, "Expected RPC_E_CALL_COMPLETE, got 0x%08x\n", hr);
1300 IUnknown_Release(test_object);
1305 static void test_CoGetContextToken(void)
1313 DWORD tid, exitcode;
1315 if (!pCoGetContextToken)
1317 win_skip("CoGetContextToken not present\n");
1322 hr = pCoGetContextToken(&token);
1323 ok(hr == CO_E_NOTINITIALIZED, "Expected CO_E_NOTINITIALIZED, got 0x%08x\n", hr);
1324 ok(token == 0xdeadbeef, "Expected 0, got 0x%lx\n", token);
1326 /* show that COM doesn't have to be initialized for multi-threaded apartments if another
1327 thread has already done so */
1329 info.wait = CreateEvent(NULL, TRUE, FALSE, NULL);
1330 ok(info.wait != NULL, "CreateEvent failed with error %d\n", GetLastError());
1332 info.stop = CreateEvent(NULL, TRUE, FALSE, NULL);
1333 ok(info.stop != NULL, "CreateEvent failed with error %d\n", GetLastError());
1335 thread = CreateThread(NULL, 0, ole_initialize_thread, &info, 0, &tid);
1336 ok(thread != NULL, "CreateThread failed with error %d\n", GetLastError());
1338 ok( !WaitForSingleObject(info.wait, 10000), "wait timed out\n" );
1341 hr = pCoGetContextToken(&token);
1342 ok(hr == S_OK, "Expected S_OK, got 0x%08x\n", hr);
1344 SetEvent(info.stop);
1345 ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
1347 GetExitCodeThread(thread, &exitcode);
1349 ok(hr == S_OK, "thread should have returned S_OK instead of 0x%08x\n", hr);
1351 CloseHandle(thread);
1352 CloseHandle(info.wait);
1353 CloseHandle(info.stop);
1357 hr = pCoGetContextToken(NULL);
1358 ok(hr == E_POINTER, "Expected E_POINTER, got 0x%08x\n", hr);
1361 hr = pCoGetContextToken(&token);
1362 ok(hr == S_OK, "Expected S_OK, got 0x%08x\n", hr);
1363 ok(token, "Expected token != 0\n");
1365 refs = IUnknown_AddRef((IUnknown *)token);
1366 todo_wine ok(refs == 1, "Expected 1, got %u\n", refs);
1368 hr = pCoGetObjectContext(&IID_IObjContext, (void **)&ctx);
1369 ok(hr == S_OK, "Expected S_OK, got 0x%08x\n", hr);
1370 todo_wine ok(ctx == (IObjContext *)token, "Expected interface pointers to be the same\n");
1372 refs = IUnknown_AddRef((IUnknown *)ctx);
1373 todo_wine ok(refs == 3, "Expected 3, got %u\n", refs);
1375 refs = IUnknown_Release((IUnknown *)ctx);
1376 todo_wine ok(refs == 2, "Expected 2, got %u\n", refs);
1378 refs = IUnknown_Release((IUnknown *)token);
1379 ok(refs == 1, "Expected 1, got %u\n", refs);
1381 /* CoGetContextToken does not add a reference */
1383 hr = pCoGetContextToken(&token);
1384 ok(hr == S_OK, "Expected S_OK, got 0x%08x\n", hr);
1385 ok(token, "Expected token != 0\n");
1386 todo_wine ok(ctx == (IObjContext *)token, "Expected interface pointers to be the same\n");
1388 refs = IUnknown_AddRef((IUnknown *)ctx);
1389 ok(refs == 2, "Expected 1, got %u\n", refs);
1391 refs = IUnknown_Release((IUnknown *)ctx);
1392 ok(refs == 1, "Expected 0, got %u\n", refs);
1394 refs = IUnknown_Release((IUnknown *)ctx);
1395 ok(refs == 0, "Expected 0, got %u\n", refs);
1400 static void test_CoGetTreatAsClass(void)
1404 static GUID deadbeef = {0xdeadbeef,0xdead,0xbeef,{0xde,0xad,0xbe,0xef,0xde,0xad,0xbe,0xef}};
1406 if (!pCoGetTreatAsClass)
1408 win_skip("CoGetTreatAsClass not present\n");
1411 hr = pCoGetTreatAsClass(&deadbeef,&out);
1412 ok (hr == S_FALSE, "expected S_FALSE got %x\n",hr);
1413 ok (IsEqualGUID(&out,&deadbeef), "expected to get same clsid back\n");
1416 static void test_CoInitializeEx(void)
1420 hr = pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1421 ok(hr == S_OK, "CoInitializeEx failed with error 0x%08x\n", hr);
1423 /* Calling OleInitialize for the first time should yield S_OK even with
1424 * apartment already initialized by previous CoInitialize(Ex) calls. */
1425 hr = OleInitialize(NULL);
1426 ok(hr == S_OK, "OleInitialize failed with error 0x%08x\n", hr);
1428 /* Subsequent calls to OleInitialize should return S_FALSE */
1429 hr = OleInitialize(NULL);
1430 ok(hr == S_FALSE, "Expected S_FALSE, hr = 0x%08x\n", hr);
1439 HMODULE hOle32 = GetModuleHandle("ole32");
1440 pCoGetObjectContext = (void*)GetProcAddress(hOle32, "CoGetObjectContext");
1441 pCoSwitchCallContext = (void*)GetProcAddress(hOle32, "CoSwitchCallContext");
1442 pCoGetTreatAsClass = (void*)GetProcAddress(hOle32,"CoGetTreatAsClass");
1443 pCoGetContextToken = (void*)GetProcAddress(hOle32, "CoGetContextToken");
1444 if (!(pCoInitializeEx = (void*)GetProcAddress(hOle32, "CoInitializeEx")))
1446 trace("You need DCOM95 installed to run this test\n");
1450 test_ProgIDFromCLSID();
1451 test_CLSIDFromProgID();
1452 test_CLSIDFromString();
1453 test_StringFromGUID2();
1454 test_CoCreateInstance();
1456 test_CoGetClassObject();
1457 test_CoRegisterMessageFilter();
1458 test_CoRegisterPSClsid();
1459 test_CoGetPSClsid();
1460 test_CoUnmarshalInterface();
1461 test_CoGetInterfaceAndReleaseStream();
1462 test_CoMarshalInterface();
1463 test_CoMarshalInterThreadInterfaceInStream();
1464 test_CoRegisterClassObject();
1465 test_registered_object_thread_affinity();
1466 test_CoFreeUnusedLibraries();
1467 test_CoGetObjectContext();
1468 test_CoGetCallContext();
1469 test_CoGetContextToken();
1470 test_CoGetTreatAsClass();
1471 test_CoInitializeEx();