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 extern const IID GUID_NULL
;
40 /* functions that are not present on all versions of Windows */
41 HRESULT (WINAPI
* pCoInitializeEx
)(LPVOID lpReserved
, DWORD dwCoInit
);
42 HRESULT (WINAPI
* pCoGetObjectContext
)(REFIID riid
, LPVOID
*ppv
);
43 HRESULT (WINAPI
* pCoSwitchCallContext
)(IUnknown
*pObject
, IUnknown
**ppOldObject
);
44 HRESULT (WINAPI
* pCoGetTreatAsClass
)(REFCLSID clsidOld
, LPCLSID pClsidNew
);
45 HRESULT (WINAPI
* pCoGetContextToken
)(ULONG_PTR
*token
);
47 #define ok_ole_success(hr, func) ok(hr == S_OK, func " failed with error 0x%08x\n", hr)
48 #define ok_more_than_one_lock() ok(cLocks > 0, "Number of locks should be > 0, but actually is %d\n", cLocks)
49 #define ok_no_locks() ok(cLocks == 0, "Number of locks should be 0, but actually is %d\n", cLocks)
51 static const CLSID CLSID_non_existent
= { 0x12345678, 0x1234, 0x1234, { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 } };
52 static const CLSID CLSID_StdFont
= { 0x0be35203, 0x8f91, 0x11ce, { 0x9d, 0xe3, 0x00, 0xaa, 0x00, 0x4b, 0xb8, 0x51 } };
53 static WCHAR stdfont
[] = {'S','t','d','F','o','n','t',0};
54 static const WCHAR wszNonExistent
[] = {'N','o','n','E','x','i','s','t','e','n','t',0};
55 static WCHAR wszCLSID_StdFont
[] =
57 '{','0','b','e','3','5','2','0','3','-','8','f','9','1','-','1','1','c','e','-',
58 '9','d','e','3','-','0','0','a','a','0','0','4','b','b','8','5','1','}',0
61 static const IID IID_IWineTest
=
66 {0xa1, 0xa2, 0x5d, 0x5a, 0x36, 0x54, 0xd3, 0xbd}
67 }; /* 5201163f-8164-4fd0-a1a2-5d5a3654d3bd */
68 static const CLSID CLSID_WineOOPTest
= {
72 {0xa1, 0xa2, 0x5d, 0x5a, 0x36, 0x54, 0xd3, 0xbd}
73 }; /* 5201163f-8164-4fd0-a1a2-5d5a3654d3bd */
77 static void LockModule(void)
79 InterlockedIncrement(&cLocks
);
82 static void UnlockModule(void)
84 InterlockedDecrement(&cLocks
);
87 static HRESULT WINAPI
Test_IClassFactory_QueryInterface(
92 if (ppvObj
== NULL
) return E_POINTER
;
94 if (IsEqualGUID(riid
, &IID_IUnknown
) ||
95 IsEqualGUID(riid
, &IID_IClassFactory
))
98 IClassFactory_AddRef(iface
);
103 return E_NOINTERFACE
;
106 static ULONG WINAPI
Test_IClassFactory_AddRef(LPCLASSFACTORY iface
)
109 return 2; /* non-heap-based object */
112 static ULONG WINAPI
Test_IClassFactory_Release(LPCLASSFACTORY iface
)
115 return 1; /* non-heap-based object */
118 static HRESULT WINAPI
Test_IClassFactory_CreateInstance(
119 LPCLASSFACTORY iface
,
125 if (pUnkOuter
) return CLASS_E_NOAGGREGATION
;
126 return E_NOINTERFACE
;
129 static HRESULT WINAPI
Test_IClassFactory_LockServer(
130 LPCLASSFACTORY iface
,
136 static const IClassFactoryVtbl TestClassFactory_Vtbl
=
138 Test_IClassFactory_QueryInterface
,
139 Test_IClassFactory_AddRef
,
140 Test_IClassFactory_Release
,
141 Test_IClassFactory_CreateInstance
,
142 Test_IClassFactory_LockServer
145 static IClassFactory Test_ClassFactory
= { &TestClassFactory_Vtbl
};
147 static void test_ProgIDFromCLSID(void)
150 HRESULT hr
= ProgIDFromCLSID(&CLSID_StdFont
, &progid
);
151 ok(hr
== S_OK
, "ProgIDFromCLSID failed with error 0x%08x\n", hr
);
154 ok(!lstrcmpiW(progid
, stdfont
), "Didn't get expected prog ID\n");
155 CoTaskMemFree(progid
);
158 progid
= (LPWSTR
)0xdeadbeef;
159 hr
= ProgIDFromCLSID(&CLSID_non_existent
, &progid
);
160 ok(hr
== REGDB_E_CLASSNOTREG
, "ProgIDFromCLSID returned %08x\n", hr
);
161 ok(progid
== NULL
, "ProgIDFromCLSID returns with progid %p\n", progid
);
163 hr
= ProgIDFromCLSID(&CLSID_StdFont
, NULL
);
164 ok(hr
== E_INVALIDARG
, "ProgIDFromCLSID should return E_INVALIDARG instead of 0x%08x\n", hr
);
167 static void test_CLSIDFromProgID(void)
170 HRESULT hr
= CLSIDFromProgID(stdfont
, &clsid
);
171 ok(hr
== S_OK
, "CLSIDFromProgID failed with error 0x%08x\n", hr
);
172 ok(IsEqualCLSID(&clsid
, &CLSID_StdFont
), "clsid wasn't equal to CLSID_StdFont\n");
174 hr
= CLSIDFromString(stdfont
, &clsid
);
175 ok_ole_success(hr
, "CLSIDFromString");
176 ok(IsEqualCLSID(&clsid
, &CLSID_StdFont
), "clsid wasn't equal to CLSID_StdFont\n");
178 /* test some failure cases */
180 hr
= CLSIDFromProgID(wszNonExistent
, NULL
);
181 ok(hr
== E_INVALIDARG
, "CLSIDFromProgID should have returned E_INVALIDARG instead of 0x%08x\n", hr
);
183 hr
= CLSIDFromProgID(NULL
, &clsid
);
184 ok(hr
== E_INVALIDARG
, "CLSIDFromProgID should have returned E_INVALIDARG instead of 0x%08x\n", hr
);
186 memset(&clsid
, 0xcc, sizeof(clsid
));
187 hr
= CLSIDFromProgID(wszNonExistent
, &clsid
);
188 ok(hr
== CO_E_CLASSSTRING
, "CLSIDFromProgID on nonexistent ProgID should have returned CO_E_CLASSSTRING instead of 0x%08x\n", hr
);
189 ok(IsEqualCLSID(&clsid
, &CLSID_NULL
), "CLSIDFromProgID should have set clsid to all-zeros on failure\n");
192 static void test_CLSIDFromString(void)
195 HRESULT hr
= CLSIDFromString(wszCLSID_StdFont
, &clsid
);
196 ok_ole_success(hr
, "CLSIDFromString");
197 ok(IsEqualCLSID(&clsid
, &CLSID_StdFont
), "clsid wasn't equal to CLSID_StdFont\n");
199 hr
= CLSIDFromString(NULL
, &clsid
);
200 ok_ole_success(hr
, "CLSIDFromString");
201 ok(IsEqualCLSID(&clsid
, &CLSID_NULL
), "clsid wasn't equal to CLSID_NULL\n");
204 static void test_StringFromGUID2(void)
209 /* invalid pointer */
210 SetLastError(0xdeadbeef);
211 len
= StringFromGUID2(NULL
,str
,50);
212 ok(len
== 0, "len: %d (expected 0)\n", len
);
213 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %x\n", GetLastError());
215 /* Test corner cases for buffer size */
216 len
= StringFromGUID2(&CLSID_StdFont
,str
,50);
217 ok(len
== 39, "len: %d (expected 39)\n", len
);
218 ok(!lstrcmpiW(str
, wszCLSID_StdFont
),"string wasn't equal for CLSID_StdFont\n");
220 memset(str
,0,sizeof str
);
221 len
= StringFromGUID2(&CLSID_StdFont
,str
,39);
222 ok(len
== 39, "len: %d (expected 39)\n", len
);
223 ok(!lstrcmpiW(str
, wszCLSID_StdFont
),"string wasn't equal for CLSID_StdFont\n");
225 len
= StringFromGUID2(&CLSID_StdFont
,str
,38);
226 ok(len
== 0, "len: %d (expected 0)\n", len
);
228 len
= StringFromGUID2(&CLSID_StdFont
,str
,30);
229 ok(len
== 0, "len: %d (expected 0)\n", len
);
237 static DWORD CALLBACK
ole_initialize_thread(LPVOID pv
)
240 struct info
*info
= pv
;
242 hr
= pCoInitializeEx(NULL
, COINIT_MULTITHREADED
);
244 SetEvent(info
->wait
);
245 WaitForSingleObject(info
->stop
, 10000);
251 static void test_CoCreateInstance(void)
258 REFCLSID rclsid
= &CLSID_InternetZoneManager
;
260 pUnk
= (IUnknown
*)0xdeadbeef;
261 hr
= CoCreateInstance(rclsid
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IUnknown
, (void **)&pUnk
);
262 ok(hr
== CO_E_NOTINITIALIZED
, "CoCreateInstance should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n", hr
);
263 ok(pUnk
== NULL
, "CoCreateInstance should have changed the passed in pointer to NULL, instead of %p\n", pUnk
);
267 /* test errors returned for non-registered clsids */
268 hr
= CoCreateInstance(&CLSID_non_existent
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IUnknown
, (void **)&pUnk
);
269 ok(hr
== REGDB_E_CLASSNOTREG
, "CoCreateInstance for non-registered inproc server should have returned REGDB_E_CLASSNOTREG instead of 0x%08x\n", hr
);
270 hr
= CoCreateInstance(&CLSID_non_existent
, NULL
, CLSCTX_INPROC_HANDLER
, &IID_IUnknown
, (void **)&pUnk
);
271 ok(hr
== REGDB_E_CLASSNOTREG
, "CoCreateInstance for non-registered inproc handler should have returned REGDB_E_CLASSNOTREG instead of 0x%08x\n", hr
);
272 hr
= CoCreateInstance(&CLSID_non_existent
, NULL
, CLSCTX_LOCAL_SERVER
, &IID_IUnknown
, (void **)&pUnk
);
273 ok(hr
== REGDB_E_CLASSNOTREG
, "CoCreateInstance for non-registered local server should have returned REGDB_E_CLASSNOTREG instead of 0x%08x\n", hr
);
274 hr
= CoCreateInstance(&CLSID_non_existent
, NULL
, CLSCTX_REMOTE_SERVER
, &IID_IUnknown
, (void **)&pUnk
);
275 ok(hr
== REGDB_E_CLASSNOTREG
, "CoCreateInstance for non-registered remote server should have returned REGDB_E_CLASSNOTREG instead of 0x%08x\n", hr
);
277 hr
= CoCreateInstance(rclsid
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IUnknown
, (void **)&pUnk
);
278 if(hr
== REGDB_E_CLASSNOTREG
)
280 skip("IE not installed so can't test CoCreateInstance\n");
285 ok_ole_success(hr
, "CoCreateInstance");
286 if(pUnk
) IUnknown_Release(pUnk
);
289 hr
= CoCreateInstance(rclsid
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IUnknown
, (void **)&pUnk
);
290 ok(hr
== CO_E_NOTINITIALIZED
, "CoCreateInstance should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n", hr
);
292 /* show that COM doesn't have to be initialized for multi-threaded apartments if another
293 thread has already done so */
295 info
.wait
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
296 ok(info
.wait
!= NULL
, "CreateEvent failed with error %d\n", GetLastError());
298 info
.stop
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
299 ok(info
.stop
!= NULL
, "CreateEvent failed with error %d\n", GetLastError());
301 thread
= CreateThread(NULL
, 0, ole_initialize_thread
, &info
, 0, &tid
);
302 ok(thread
!= NULL
, "CreateThread failed with error %d\n", GetLastError());
304 ok( !WaitForSingleObject(info
.wait
, 10000 ), "wait timed out\n" );
306 pUnk
= (IUnknown
*)0xdeadbeef;
307 hr
= CoCreateInstance(rclsid
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IUnknown
, (void **)&pUnk
);
308 ok(hr
== S_OK
, "CoCreateInstance should have returned S_OK instead of 0x%08x\n", hr
);
309 if (pUnk
) IUnknown_Release(pUnk
);
312 ok( !WaitForSingleObject(thread
, 10000), "wait timed out\n" );
314 GetExitCodeThread(thread
, &exitcode
);
316 ok(hr
== S_OK
, "thread should have returned S_OK instead of 0x%08x\n", hr
);
319 CloseHandle(info
.wait
);
320 CloseHandle(info
.stop
);
323 static void test_CoGetClassObject(void)
330 REFCLSID rclsid
= &CLSID_InternetZoneManager
;
332 hr
= CoGetClassObject(rclsid
, CLSCTX_INPROC_SERVER
, NULL
, &IID_IUnknown
, (void **)&pUnk
);
333 ok(hr
== CO_E_NOTINITIALIZED
, "CoGetClassObject should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n", hr
);
334 ok(pUnk
== NULL
, "CoGetClassObject should have changed the passed in pointer to NULL, instead of %p\n", pUnk
);
336 hr
= CoGetClassObject(rclsid
, CLSCTX_INPROC_SERVER
, NULL
, &IID_IUnknown
, NULL
);
337 ok(hr
== E_INVALIDARG
||
338 broken(hr
== CO_E_NOTINITIALIZED
), /* win9x */
339 "CoGetClassObject should have returned E_INVALIDARG instead of 0x%08x\n", hr
);
341 /* show that COM doesn't have to be initialized for multi-threaded apartments if another
342 thread has already done so */
344 info
.wait
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
345 ok(info
.wait
!= NULL
, "CreateEvent failed with error %d\n", GetLastError());
347 info
.stop
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
348 ok(info
.stop
!= NULL
, "CreateEvent failed with error %d\n", GetLastError());
350 thread
= CreateThread(NULL
, 0, ole_initialize_thread
, &info
, 0, &tid
);
351 ok(thread
!= NULL
, "CreateThread failed with error %d\n", GetLastError());
353 ok( !WaitForSingleObject(info
.wait
, 10000), "wait timed out\n" );
355 pUnk
= (IUnknown
*)0xdeadbeef;
356 hr
= CoGetClassObject(rclsid
, CLSCTX_INPROC_SERVER
, NULL
, &IID_IUnknown
, (void **)&pUnk
);
357 if(hr
== REGDB_E_CLASSNOTREG
)
358 skip("IE not installed so can't test CoGetClassObject\n");
361 ok(hr
== S_OK
, "CoGetClassObject should have returned S_OK instead of 0x%08x\n", hr
);
362 if (pUnk
) IUnknown_Release(pUnk
);
366 ok( !WaitForSingleObject(thread
, 10000), "wait timed out\n" );
368 GetExitCodeThread(thread
, &exitcode
);
370 ok(hr
== S_OK
, "thread should have returned S_OK instead of 0x%08x\n", hr
);
373 CloseHandle(info
.wait
);
374 CloseHandle(info
.stop
);
377 static ATOM
register_dummy_class(void)
385 GetModuleHandle(NULL
),
387 LoadCursor(NULL
, IDC_ARROW
),
388 (HBRUSH
)(COLOR_BTNFACE
+1),
390 TEXT("WineOleTestClass"),
393 return RegisterClass(&wc
);
396 static void test_ole_menu(void)
401 hwndFrame
= CreateWindow(MAKEINTATOM(register_dummy_class()), "Test", 0, CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
, NULL
, NULL
, NULL
, NULL
);
402 hr
= OleSetMenuDescriptor(NULL
, hwndFrame
, NULL
, NULL
, NULL
);
403 todo_wine
ok_ole_success(hr
, "OleSetMenuDescriptor");
405 DestroyWindow(hwndFrame
);
409 static HRESULT WINAPI
MessageFilter_QueryInterface(IMessageFilter
*iface
, REFIID riid
, void ** ppvObj
)
411 if (ppvObj
== NULL
) return E_POINTER
;
413 if (IsEqualGUID(riid
, &IID_IUnknown
) ||
414 IsEqualGUID(riid
, &IID_IClassFactory
))
417 IMessageFilter_AddRef(iface
);
421 return E_NOINTERFACE
;
424 static ULONG WINAPI
MessageFilter_AddRef(IMessageFilter
*iface
)
426 return 2; /* non-heap object */
429 static ULONG WINAPI
MessageFilter_Release(IMessageFilter
*iface
)
431 return 1; /* non-heap object */
434 static DWORD WINAPI
MessageFilter_HandleInComingCall(
435 IMessageFilter
*iface
,
437 HTASK threadIDCaller
,
439 LPINTERFACEINFO lpInterfaceInfo
)
441 trace("HandleInComingCall\n");
442 return SERVERCALL_ISHANDLED
;
445 static DWORD WINAPI
MessageFilter_RetryRejectedCall(
446 IMessageFilter
*iface
,
447 HTASK threadIDCallee
,
451 trace("RetryRejectedCall\n");
455 static DWORD WINAPI
MessageFilter_MessagePending(
456 IMessageFilter
*iface
,
457 HTASK threadIDCallee
,
461 trace("MessagePending\n");
462 return PENDINGMSG_WAITNOPROCESS
;
465 static const IMessageFilterVtbl MessageFilter_Vtbl
=
467 MessageFilter_QueryInterface
,
468 MessageFilter_AddRef
,
469 MessageFilter_Release
,
470 MessageFilter_HandleInComingCall
,
471 MessageFilter_RetryRejectedCall
,
472 MessageFilter_MessagePending
475 static IMessageFilter MessageFilter
= { &MessageFilter_Vtbl
};
477 static void test_CoRegisterMessageFilter(void)
480 IMessageFilter
*prev_filter
;
482 hr
= CoRegisterMessageFilter(&MessageFilter
, &prev_filter
);
483 ok(hr
== CO_E_NOT_SUPPORTED
,
484 "CoRegisterMessageFilter should have failed with CO_E_NOT_SUPPORTED instead of 0x%08x\n",
487 pCoInitializeEx(NULL
, COINIT_MULTITHREADED
);
488 prev_filter
= (IMessageFilter
*)0xdeadbeef;
489 hr
= CoRegisterMessageFilter(&MessageFilter
, &prev_filter
);
490 ok(hr
== CO_E_NOT_SUPPORTED
,
491 "CoRegisterMessageFilter should have failed with CO_E_NOT_SUPPORTED instead of 0x%08x\n",
493 ok(prev_filter
== (IMessageFilter
*)0xdeadbeef,
494 "prev_filter should have been set to %p\n", prev_filter
);
497 pCoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
499 hr
= CoRegisterMessageFilter(NULL
, NULL
);
500 ok_ole_success(hr
, "CoRegisterMessageFilter");
502 prev_filter
= (IMessageFilter
*)0xdeadbeef;
503 hr
= CoRegisterMessageFilter(NULL
, &prev_filter
);
504 ok_ole_success(hr
, "CoRegisterMessageFilter");
505 ok(prev_filter
== NULL
, "prev_filter should have been set to NULL instead of %p\n", prev_filter
);
507 hr
= CoRegisterMessageFilter(&MessageFilter
, &prev_filter
);
508 ok_ole_success(hr
, "CoRegisterMessageFilter");
509 ok(prev_filter
== NULL
, "prev_filter should have been set to NULL instead of %p\n", prev_filter
);
511 hr
= CoRegisterMessageFilter(NULL
, NULL
);
512 ok_ole_success(hr
, "CoRegisterMessageFilter");
517 static HRESULT WINAPI
Test_IUnknown_QueryInterface(
522 if (ppvObj
== NULL
) return E_POINTER
;
524 if (IsEqualIID(riid
, &IID_IUnknown
) ||
525 IsEqualIID(riid
, &IID_IWineTest
))
528 IUnknown_AddRef(iface
);
533 return E_NOINTERFACE
;
536 static ULONG WINAPI
Test_IUnknown_AddRef(LPUNKNOWN iface
)
538 return 2; /* non-heap-based object */
541 static ULONG WINAPI
Test_IUnknown_Release(LPUNKNOWN iface
)
543 return 1; /* non-heap-based object */
546 static const IUnknownVtbl TestUnknown_Vtbl
=
548 Test_IUnknown_QueryInterface
,
549 Test_IUnknown_AddRef
,
550 Test_IUnknown_Release
,
553 static IUnknown Test_Unknown
= { &TestUnknown_Vtbl
};
555 static HRESULT WINAPI
PSFactoryBuffer_QueryInterface(
556 IPSFactoryBuffer
* This
,
557 /* [in] */ REFIID riid
,
558 /* [iid_is][out] */ void **ppvObject
)
560 if (IsEqualIID(riid
, &IID_IUnknown
) ||
561 IsEqualIID(riid
, &IID_IPSFactoryBuffer
))
564 IPSFactoryBuffer_AddRef(This
);
567 return E_NOINTERFACE
;
570 static ULONG WINAPI
PSFactoryBuffer_AddRef(
571 IPSFactoryBuffer
* This
)
576 static ULONG WINAPI
PSFactoryBuffer_Release(
577 IPSFactoryBuffer
* This
)
582 static HRESULT WINAPI
PSFactoryBuffer_CreateProxy(
583 IPSFactoryBuffer
* This
,
584 /* [in] */ IUnknown
*pUnkOuter
,
585 /* [in] */ REFIID riid
,
586 /* [out] */ IRpcProxyBuffer
**ppProxy
,
587 /* [out] */ void **ppv
)
592 static HRESULT WINAPI
PSFactoryBuffer_CreateStub(
593 IPSFactoryBuffer
* This
,
594 /* [in] */ REFIID riid
,
595 /* [unique][in] */ IUnknown
*pUnkServer
,
596 /* [out] */ IRpcStubBuffer
**ppStub
)
601 static IPSFactoryBufferVtbl PSFactoryBufferVtbl
=
603 PSFactoryBuffer_QueryInterface
,
604 PSFactoryBuffer_AddRef
,
605 PSFactoryBuffer_Release
,
606 PSFactoryBuffer_CreateProxy
,
607 PSFactoryBuffer_CreateStub
610 static IPSFactoryBuffer PSFactoryBuffer
= { &PSFactoryBufferVtbl
};
612 static const CLSID CLSID_WineTestPSFactoryBuffer
=
617 {0xa1, 0xa2, 0x5d, 0x5a, 0x36, 0x54, 0xd3, 0xbd}
618 }; /* 52011640-8164-4fd0-a1a2-5d5a3654d3bd */
620 static void test_CoRegisterPSClsid(void)
623 DWORD dwRegistrationKey
;
627 hr
= CoRegisterPSClsid(&IID_IWineTest
, &CLSID_WineTestPSFactoryBuffer
);
628 ok(hr
== CO_E_NOTINITIALIZED
, "CoRegisterPSClsid should have returened CO_E_NOTINITIALIZED instead of 0x%08x\n", hr
);
630 pCoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
632 hr
= CoRegisterClassObject(&CLSID_WineTestPSFactoryBuffer
, (IUnknown
*)&PSFactoryBuffer
,
633 CLSCTX_INPROC_SERVER
, REGCLS_MULTIPLEUSE
, &dwRegistrationKey
);
634 ok_ole_success(hr
, "CoRegisterClassObject");
636 hr
= CoRegisterPSClsid(&IID_IWineTest
, &CLSID_WineTestPSFactoryBuffer
);
637 ok_ole_success(hr
, "CoRegisterPSClsid");
639 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &stream
);
640 ok_ole_success(hr
, "CreateStreamOnHGlobal");
642 hr
= CoMarshalInterface(stream
, &IID_IWineTest
, &Test_Unknown
, MSHCTX_INPROC
, NULL
, MSHLFLAGS_NORMAL
);
643 ok(hr
== E_NOTIMPL
, "CoMarshalInterface should have returned E_NOTIMPL instead of 0x%08x\n", hr
);
644 IStream_Release(stream
);
646 hr
= CoRevokeClassObject(dwRegistrationKey
);
647 ok_ole_success(hr
, "CoRevokeClassObject");
651 pCoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
653 hr
= CoGetPSClsid(&IID_IWineTest
, &clsid
);
654 ok(hr
== REGDB_E_IIDNOTREG
, "CoGetPSClsid should have returned REGDB_E_IIDNOTREG instead of 0x%08x\n", hr
);
659 static void test_CoGetPSClsid(void)
664 hr
= CoGetPSClsid(&IID_IClassFactory
, &clsid
);
665 ok(hr
== CO_E_NOTINITIALIZED
,
666 "CoGetPSClsid should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n",
669 pCoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
671 hr
= CoGetPSClsid(&IID_IClassFactory
, &clsid
);
672 ok_ole_success(hr
, "CoGetPSClsid");
674 hr
= CoGetPSClsid(&IID_IWineTest
, &clsid
);
675 ok(hr
== REGDB_E_IIDNOTREG
,
676 "CoGetPSClsid for random IID returned 0x%08x instead of REGDB_E_IIDNOTREG\n",
679 hr
= CoGetPSClsid(&IID_IClassFactory
, NULL
);
680 ok(hr
== E_INVALIDARG
,
681 "CoGetPSClsid for null clsid returned 0x%08x instead of E_INVALIDARG\n",
687 /* basic test, mainly for invalid arguments. see marshal.c for more */
688 static void test_CoUnmarshalInterface(void)
694 hr
= CoUnmarshalInterface(NULL
, &IID_IUnknown
, (void **)&pProxy
);
695 ok(hr
== E_INVALIDARG
, "CoUnmarshalInterface should have returned E_INVALIDARG instead of 0x%08x\n", hr
);
697 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
698 ok_ole_success(hr
, "CreateStreamOnHGlobal");
700 hr
= CoUnmarshalInterface(pStream
, &IID_IUnknown
, (void **)&pProxy
);
702 ok(hr
== CO_E_NOTINITIALIZED
, "CoUnmarshalInterface should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n", hr
);
704 pCoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
706 hr
= CoUnmarshalInterface(pStream
, &IID_IUnknown
, (void **)&pProxy
);
707 ok(hr
== STG_E_READFAULT
, "CoUnmarshalInterface should have returned STG_E_READFAULT instead of 0x%08x\n", hr
);
711 hr
= CoUnmarshalInterface(pStream
, &IID_IUnknown
, NULL
);
712 ok(hr
== E_INVALIDARG
, "CoUnmarshalInterface should have returned E_INVALIDARG instead of 0x%08x\n", hr
);
714 IStream_Release(pStream
);
717 static void test_CoGetInterfaceAndReleaseStream(void)
722 pCoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
724 hr
= CoGetInterfaceAndReleaseStream(NULL
, &IID_IUnknown
, (void**)&pUnk
);
725 ok(hr
== E_INVALIDARG
, "hr %08x\n", hr
);
730 /* basic test, mainly for invalid arguments. see marshal.c for more */
731 static void test_CoMarshalInterface(void)
735 static const LARGE_INTEGER llZero
;
737 pCoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
739 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
740 ok_ole_success(hr
, "CreateStreamOnHGlobal");
742 hr
= CoMarshalInterface(pStream
, &IID_IUnknown
, NULL
, MSHCTX_INPROC
, NULL
, MSHLFLAGS_NORMAL
);
743 ok(hr
== E_INVALIDARG
, "CoMarshalInterface should have returned E_INVALIDARG instead of 0x%08x\n", hr
);
745 hr
= CoMarshalInterface(NULL
, &IID_IUnknown
, (IUnknown
*)&Test_ClassFactory
, MSHCTX_INPROC
, NULL
, MSHLFLAGS_NORMAL
);
746 ok(hr
== E_INVALIDARG
, "CoMarshalInterface should have returned E_INVALIDARG instead of 0x%08x\n", hr
);
748 hr
= CoMarshalInterface(pStream
, &IID_IUnknown
, (IUnknown
*)&Test_ClassFactory
, MSHCTX_INPROC
, NULL
, MSHLFLAGS_NORMAL
);
749 ok_ole_success(hr
, "CoMarshalInterface");
751 /* stream not rewound */
752 hr
= CoReleaseMarshalData(pStream
);
753 ok(hr
== STG_E_READFAULT
, "CoReleaseMarshalData should have returned STG_E_READFAULT instead of 0x%08x\n", hr
);
755 hr
= IStream_Seek(pStream
, llZero
, STREAM_SEEK_SET
, NULL
);
756 ok_ole_success(hr
, "IStream_Seek");
758 hr
= CoReleaseMarshalData(pStream
);
759 ok_ole_success(hr
, "CoReleaseMarshalData");
761 IStream_Release(pStream
);
766 static void test_CoMarshalInterThreadInterfaceInStream(void)
770 IClassFactory
*pProxy
;
772 pCoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
776 hr
= CoMarshalInterThreadInterfaceInStream(&IID_IUnknown
, (IUnknown
*)&Test_ClassFactory
, NULL
);
777 ok(hr
== E_INVALIDARG
, "CoMarshalInterThreadInterfaceInStream should have returned E_INVALIDARG instead of 0x%08x\n", hr
);
779 hr
= CoMarshalInterThreadInterfaceInStream(&IID_IUnknown
, NULL
, &pStream
);
780 ok(hr
== E_INVALIDARG
, "CoMarshalInterThreadInterfaceInStream should have returned E_INVALIDARG instead of 0x%08x\n", hr
);
784 hr
= CoMarshalInterThreadInterfaceInStream(&IID_IUnknown
, (IUnknown
*)&Test_ClassFactory
, &pStream
);
785 ok_ole_success(hr
, "CoMarshalInterThreadInterfaceInStream");
787 ok_more_than_one_lock();
789 hr
= CoUnmarshalInterface(pStream
, &IID_IClassFactory
, (void **)&pProxy
);
790 ok_ole_success(hr
, "CoUnmarshalInterface");
792 IClassFactory_Release(pProxy
);
793 IStream_Release(pStream
);
800 static void test_CoRegisterClassObject(void)
806 pCoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
808 /* CLSCTX_INPROC_SERVER */
809 hr
= CoRegisterClassObject(&CLSID_WineOOPTest
, (IUnknown
*)&Test_ClassFactory
,
810 CLSCTX_INPROC_SERVER
, REGCLS_SINGLEUSE
, &cookie
);
811 ok_ole_success(hr
, "CoRegisterClassObject");
812 hr
= CoRevokeClassObject(cookie
);
813 ok_ole_success(hr
, "CoRevokeClassObject");
815 hr
= CoRegisterClassObject(&CLSID_WineOOPTest
, (IUnknown
*)&Test_ClassFactory
,
816 CLSCTX_INPROC_SERVER
, REGCLS_MULTIPLEUSE
, &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_INPROC_SERVER
, REGCLS_MULTI_SEPARATE
, &cookie
);
823 ok_ole_success(hr
, "CoRegisterClassObject");
824 hr
= CoRevokeClassObject(cookie
);
825 ok_ole_success(hr
, "CoRevokeClassObject");
827 /* CLSCTX_LOCAL_SERVER */
828 hr
= CoRegisterClassObject(&CLSID_WineOOPTest
, (IUnknown
*)&Test_ClassFactory
,
829 CLSCTX_LOCAL_SERVER
, REGCLS_SINGLEUSE
, &cookie
);
830 ok_ole_success(hr
, "CoRegisterClassObject");
831 hr
= CoRevokeClassObject(cookie
);
832 ok_ole_success(hr
, "CoRevokeClassObject");
834 hr
= CoRegisterClassObject(&CLSID_WineOOPTest
, (IUnknown
*)&Test_ClassFactory
,
835 CLSCTX_LOCAL_SERVER
, REGCLS_MULTIPLEUSE
, &cookie
);
836 ok_ole_success(hr
, "CoRegisterClassObject");
837 hr
= CoRevokeClassObject(cookie
);
838 ok_ole_success(hr
, "CoRevokeClassObject");
840 hr
= CoRegisterClassObject(&CLSID_WineOOPTest
, (IUnknown
*)&Test_ClassFactory
,
841 CLSCTX_LOCAL_SERVER
, REGCLS_MULTI_SEPARATE
, &cookie
);
842 ok_ole_success(hr
, "CoRegisterClassObject");
843 hr
= CoRevokeClassObject(cookie
);
844 ok_ole_success(hr
, "CoRevokeClassObject");
846 /* CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER */
847 hr
= CoRegisterClassObject(&CLSID_WineOOPTest
, (IUnknown
*)&Test_ClassFactory
,
848 CLSCTX_INPROC_SERVER
|CLSCTX_LOCAL_SERVER
, REGCLS_SINGLEUSE
, &cookie
);
849 ok_ole_success(hr
, "CoRegisterClassObject");
850 hr
= CoRevokeClassObject(cookie
);
851 ok_ole_success(hr
, "CoRevokeClassObject");
853 /* test whether an object that doesn't support IClassFactory can be
854 * registered for CLSCTX_LOCAL_SERVER */
855 hr
= CoRegisterClassObject(&CLSID_WineOOPTest
, &Test_Unknown
,
856 CLSCTX_LOCAL_SERVER
, REGCLS_SINGLEUSE
, &cookie
);
857 ok_ole_success(hr
, "CoRegisterClassObject");
858 hr
= CoRevokeClassObject(cookie
);
859 ok_ole_success(hr
, "CoRevokeClassObject");
861 /* test whether registered class becomes invalid when apartment is destroyed */
862 hr
= CoRegisterClassObject(&CLSID_WineOOPTest
, (IUnknown
*)&Test_ClassFactory
,
863 CLSCTX_INPROC_SERVER
, REGCLS_SINGLEUSE
, &cookie
);
864 ok_ole_success(hr
, "CoRegisterClassObject");
867 pCoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
869 hr
= CoGetClassObject(&CLSID_WineOOPTest
, CLSCTX_INPROC_SERVER
, NULL
,
870 &IID_IClassFactory
, (void **)&pcf
);
871 ok(hr
== REGDB_E_CLASSNOTREG
, "object registered in an apartment shouldn't accessible after it is destroyed\n");
873 /* crashes with at least win9x DCOM! */
875 hr
= CoRevokeClassObject(cookie
);
880 static HRESULT
get_class_object(CLSCTX clsctx
)
885 hr
= CoGetClassObject(&CLSID_WineOOPTest
, clsctx
, NULL
, &IID_IClassFactory
,
889 IClassFactory_Release(pcf
);
894 static DWORD CALLBACK
get_class_object_thread(LPVOID pv
)
896 CLSCTX clsctx
= (CLSCTX
)(DWORD_PTR
)pv
;
899 pCoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
901 hr
= get_class_object(clsctx
);
908 static DWORD CALLBACK
get_class_object_proxy_thread(LPVOID pv
)
910 CLSCTX clsctx
= (CLSCTX
)(DWORD_PTR
)pv
;
915 pCoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
917 hr
= CoGetClassObject(&CLSID_WineOOPTest
, clsctx
, NULL
, &IID_IClassFactory
,
922 hr
= IClassFactory_QueryInterface(pcf
, &IID_IMultiQI
, (void **)&pMQI
);
924 IMultiQI_Release(pMQI
);
925 IClassFactory_Release(pcf
);
933 static DWORD CALLBACK
register_class_object_thread(LPVOID pv
)
938 pCoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
940 hr
= CoRegisterClassObject(&CLSID_WineOOPTest
, (IUnknown
*)&Test_ClassFactory
,
941 CLSCTX_INPROC_SERVER
, REGCLS_SINGLEUSE
, &cookie
);
948 static DWORD CALLBACK
revoke_class_object_thread(LPVOID pv
)
950 DWORD cookie
= (DWORD_PTR
)pv
;
953 pCoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
955 hr
= CoRevokeClassObject(cookie
);
962 static void test_registered_object_thread_affinity(void)
970 pCoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
972 /* CLSCTX_INPROC_SERVER */
974 hr
= CoRegisterClassObject(&CLSID_WineOOPTest
, (IUnknown
*)&Test_ClassFactory
,
975 CLSCTX_INPROC_SERVER
, REGCLS_SINGLEUSE
, &cookie
);
976 ok_ole_success(hr
, "CoRegisterClassObject");
978 thread
= CreateThread(NULL
, 0, get_class_object_thread
, (LPVOID
)CLSCTX_INPROC_SERVER
, 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
== REGDB_E_CLASSNOTREG
, "CoGetClassObject on inproc object "
984 "registered in different thread should return REGDB_E_CLASSNOTREG "
985 "instead of 0x%08x\n", hr
);
987 hr
= get_class_object(CLSCTX_INPROC_SERVER
);
988 ok(hr
== S_OK
, "CoGetClassObject on inproc object registered in same "
989 "thread should return S_OK instead of 0x%08x\n", hr
);
991 thread
= CreateThread(NULL
, 0, register_class_object_thread
, NULL
, 0, &tid
);
992 ok(thread
!= NULL
, "CreateThread failed with error %d\n", GetLastError());
993 ok ( !WaitForSingleObject(thread
, 10000), "wait timed out\n" );
994 GetExitCodeThread(thread
, &exitcode
);
996 ok(hr
== S_OK
, "CoRegisterClassObject with same CLSID but in different thread should return S_OK instead of 0x%08x\n", hr
);
998 hr
= CoRevokeClassObject(cookie
);
999 ok_ole_success(hr
, "CoRevokeClassObject");
1001 /* CLSCTX_LOCAL_SERVER */
1003 hr
= CoRegisterClassObject(&CLSID_WineOOPTest
, (IUnknown
*)&Test_ClassFactory
,
1004 CLSCTX_LOCAL_SERVER
, REGCLS_MULTIPLEUSE
, &cookie
);
1005 ok_ole_success(hr
, "CoRegisterClassObject");
1007 thread
= CreateThread(NULL
, 0, get_class_object_proxy_thread
, (LPVOID
)CLSCTX_LOCAL_SERVER
, 0, &tid
);
1008 ok(thread
!= NULL
, "CreateThread failed with error %d\n", GetLastError());
1009 while (MsgWaitForMultipleObjects(1, &thread
, FALSE
, 10000, QS_ALLINPUT
) == WAIT_OBJECT_0
+ 1)
1012 while (PeekMessageA(&msg
, NULL
, 0, 0, PM_REMOVE
))
1014 TranslateMessage(&msg
);
1015 DispatchMessageA(&msg
);
1018 GetExitCodeThread(thread
, &exitcode
);
1020 ok(hr
== S_OK
, "CoGetClassObject on local server object "
1021 "registered in different thread should return S_OK "
1022 "instead of 0x%08x\n", hr
);
1024 hr
= get_class_object(CLSCTX_LOCAL_SERVER
);
1025 ok(hr
== S_OK
, "CoGetClassObject on local server object registered in same "
1026 "thread should return S_OK instead of 0x%08x\n", hr
);
1028 thread
= CreateThread(NULL
, 0, revoke_class_object_thread
, (LPVOID
)(DWORD_PTR
)cookie
, 0, &tid
);
1029 ok(thread
!= NULL
, "CreateThread failed with error %d\n", GetLastError());
1030 ok( !WaitForSingleObject(thread
, 10000), "wait timed out\n" );
1031 GetExitCodeThread(thread
, &exitcode
);
1033 ok(hr
== RPC_E_WRONG_THREAD
, "CoRevokeClassObject called from different "
1034 "thread to where registered should return RPC_E_WRONG_THREAD instead of 0x%08x\n", hr
);
1036 thread
= CreateThread(NULL
, 0, register_class_object_thread
, NULL
, 0, &tid
);
1037 ok(thread
!= NULL
, "CreateThread failed with error %d\n", GetLastError());
1038 ok( !WaitForSingleObject(thread
, 10000), "wait timed out\n" );
1039 GetExitCodeThread(thread
, &exitcode
);
1041 ok(hr
== S_OK
, "CoRegisterClassObject with same CLSID but in different "
1042 "thread should return S_OK instead of 0x%08x\n", hr
);
1044 hr
= CoRevokeClassObject(cookie
);
1045 ok_ole_success(hr
, "CoRevokeClassObject");
1050 static DWORD CALLBACK
free_libraries_thread(LPVOID p
)
1052 CoFreeUnusedLibraries();
1056 static inline BOOL
is_module_loaded(const char *module
)
1058 return GetModuleHandle(module
) ? TRUE
: FALSE
;
1061 static void test_CoFreeUnusedLibraries(void)
1068 pCoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
1070 ok(!is_module_loaded("urlmon.dll"), "urlmon.dll shouldn't be loaded\n");
1072 hr
= CoCreateInstance(&CLSID_FileProtocol
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IInternetProtocol
, (void **)&pUnk
);
1073 if (hr
== REGDB_E_CLASSNOTREG
)
1075 skip("IE not installed so can't run CoFreeUnusedLibraries test\n");
1079 ok_ole_success(hr
, "CoCreateInstance");
1081 ok(is_module_loaded("urlmon.dll"), "urlmon.dll should be loaded\n");
1084 broken(pUnk
== NULL
), /* win9x */
1085 "Expected a valid pointer\n");
1087 IUnknown_Release(pUnk
);
1089 ok(is_module_loaded("urlmon.dll"), "urlmon.dll should be loaded\n");
1091 thread
= CreateThread(NULL
, 0, free_libraries_thread
, NULL
, 0, &tid
);
1092 ok( !WaitForSingleObject(thread
, 10000), "wait timed out\n" );
1093 CloseHandle(thread
);
1095 ok(is_module_loaded("urlmon.dll"), "urlmon.dll should be loaded\n");
1097 CoFreeUnusedLibraries();
1099 ok(!is_module_loaded("urlmon.dll"), "urlmon.dll shouldn't be loaded\n");
1104 static void test_CoGetObjectContext(void)
1108 IComThreadingInfo
*pComThreadingInfo
;
1109 IContextCallback
*pContextCallback
;
1110 IObjContext
*pObjContext
;
1115 DWORD tid
, exitcode
;
1117 if (!pCoGetObjectContext
)
1119 skip("CoGetObjectContext not present\n");
1123 hr
= pCoGetObjectContext(&IID_IComThreadingInfo
, (void **)&pComThreadingInfo
);
1124 ok(hr
== CO_E_NOTINITIALIZED
, "CoGetObjectContext should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n", hr
);
1125 ok(pComThreadingInfo
== NULL
, "pComThreadingInfo should have been set to NULL\n");
1127 /* show that COM doesn't have to be initialized for multi-threaded apartments if another
1128 thread has already done so */
1130 info
.wait
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1131 ok(info
.wait
!= NULL
, "CreateEvent failed with error %d\n", GetLastError());
1133 info
.stop
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1134 ok(info
.stop
!= NULL
, "CreateEvent failed with error %d\n", GetLastError());
1136 thread
= CreateThread(NULL
, 0, ole_initialize_thread
, &info
, 0, &tid
);
1137 ok(thread
!= NULL
, "CreateThread failed with error %d\n", GetLastError());
1139 ok( !WaitForSingleObject(info
.wait
, 10000), "wait timed out\n" );
1141 pComThreadingInfo
= NULL
;
1142 hr
= pCoGetObjectContext(&IID_IComThreadingInfo
, (void **)&pComThreadingInfo
);
1143 ok(hr
== S_OK
, "Expected S_OK, got 0x%08x\n", hr
);
1144 IComThreadingInfo_Release(pComThreadingInfo
);
1146 SetEvent(info
.stop
);
1147 ok( !WaitForSingleObject(thread
, 10000), "wait timed out\n" );
1149 GetExitCodeThread(thread
, &exitcode
);
1151 ok(hr
== S_OK
, "thread should have returned S_OK instead of 0x%08x\n", hr
);
1153 CloseHandle(thread
);
1154 CloseHandle(info
.wait
);
1155 CloseHandle(info
.stop
);
1157 pCoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
1159 hr
= pCoGetObjectContext(&IID_IComThreadingInfo
, (void **)&pComThreadingInfo
);
1160 ok_ole_success(hr
, "CoGetObjectContext");
1162 hr
= IComThreadingInfo_GetCurrentApartmentType(pComThreadingInfo
, &apttype
);
1163 ok_ole_success(hr
, "IComThreadingInfo_GetCurrentApartmentType");
1164 ok(apttype
== APTTYPE_MAINSTA
, "apartment type should be APTTYPE_MAINSTA instead of %d\n", apttype
);
1166 hr
= IComThreadingInfo_GetCurrentThreadType(pComThreadingInfo
, &thdtype
);
1167 ok_ole_success(hr
, "IComThreadingInfo_GetCurrentThreadType");
1168 ok(thdtype
== THDTYPE_PROCESSMESSAGES
, "thread type should be THDTYPE_PROCESSMESSAGES instead of %d\n", thdtype
);
1170 refs
= IComThreadingInfo_Release(pComThreadingInfo
);
1171 ok(refs
== 0, "pComThreadingInfo should have 0 refs instead of %d refs\n", refs
);
1173 hr
= pCoGetObjectContext(&IID_IContextCallback
, (void **)&pContextCallback
);
1174 ok_ole_success(hr
, "CoGetObjectContext(ContextCallback)");
1178 refs
= IContextCallback_Release(pContextCallback
);
1179 ok(refs
== 0, "pContextCallback should have 0 refs instead of %d refs\n", refs
);
1184 pCoInitializeEx(NULL
, COINIT_MULTITHREADED
);
1186 hr
= pCoGetObjectContext(&IID_IComThreadingInfo
, (void **)&pComThreadingInfo
);
1187 ok_ole_success(hr
, "CoGetObjectContext");
1189 hr
= IComThreadingInfo_GetCurrentApartmentType(pComThreadingInfo
, &apttype
);
1190 ok_ole_success(hr
, "IComThreadingInfo_GetCurrentApartmentType");
1191 ok(apttype
== APTTYPE_MTA
, "apartment type should be APTTYPE_MTA instead of %d\n", apttype
);
1193 hr
= IComThreadingInfo_GetCurrentThreadType(pComThreadingInfo
, &thdtype
);
1194 ok_ole_success(hr
, "IComThreadingInfo_GetCurrentThreadType");
1195 ok(thdtype
== THDTYPE_BLOCKMESSAGES
, "thread type should be THDTYPE_BLOCKMESSAGES instead of %d\n", thdtype
);
1197 refs
= IComThreadingInfo_Release(pComThreadingInfo
);
1198 ok(refs
== 0, "pComThreadingInfo should have 0 refs instead of %d refs\n", refs
);
1200 hr
= pCoGetObjectContext(&IID_IContextCallback
, (void **)&pContextCallback
);
1201 ok_ole_success(hr
, "CoGetObjectContext(ContextCallback)");
1205 refs
= IContextCallback_Release(pContextCallback
);
1206 ok(refs
== 0, "pContextCallback should have 0 refs instead of %d refs\n", refs
);
1209 hr
= pCoGetObjectContext(&IID_IObjContext
, (void **)&pObjContext
);
1210 ok_ole_success(hr
, "CoGetObjectContext");
1212 refs
= IObjContext_Release(pObjContext
);
1213 ok(refs
== 0, "pObjContext should have 0 refs instead of %d refs\n", refs
);
1219 const IUnknownVtbl
*lpVtbl
;
1223 static HRESULT WINAPI
Test_CallContext_QueryInterface(
1228 if (ppvObj
== NULL
) return E_POINTER
;
1230 if (IsEqualGUID(riid
, &IID_IUnknown
))
1233 IUnknown_AddRef(iface
);
1238 return E_NOINTERFACE
;
1241 static ULONG WINAPI
Test_CallContext_AddRef(IUnknown
*iface
)
1243 Test_CallContext
*This
= (Test_CallContext
*)iface
;
1244 return InterlockedIncrement(&This
->refs
);
1247 static ULONG WINAPI
Test_CallContext_Release(IUnknown
*iface
)
1249 Test_CallContext
*This
= (Test_CallContext
*)iface
;
1250 ULONG refs
= InterlockedDecrement(&This
->refs
);
1252 HeapFree(GetProcessHeap(), 0, This
);
1256 static const IUnknownVtbl TestCallContext_Vtbl
=
1258 Test_CallContext_QueryInterface
,
1259 Test_CallContext_AddRef
,
1260 Test_CallContext_Release
1263 static void test_CoGetCallContext(void)
1268 IUnknown
*test_object
;
1270 if (!pCoSwitchCallContext
)
1272 skip("CoSwitchCallContext not present\n");
1278 test_object
= HeapAlloc(GetProcessHeap(), 0, sizeof(Test_CallContext
));
1279 ((Test_CallContext
*)test_object
)->lpVtbl
= &TestCallContext_Vtbl
;
1280 ((Test_CallContext
*)test_object
)->refs
= 1;
1282 hr
= CoGetCallContext(&IID_IUnknown
, (void**)&pUnk
);
1283 ok(hr
== RPC_E_CALL_COMPLETE
, "Expected RPC_E_CALL_COMPLETE, got 0x%08x\n", hr
);
1285 pUnk
= (IUnknown
*)0xdeadbeef;
1286 hr
= pCoSwitchCallContext(test_object
, &pUnk
);
1287 ok_ole_success(hr
, "CoSwitchCallContext");
1288 ok(pUnk
== NULL
, "expected NULL, got %p\n", pUnk
);
1289 refs
= IUnknown_AddRef(test_object
);
1290 ok(refs
== 2, "Expected refcount 2, got %d\n", refs
);
1291 IUnknown_Release(test_object
);
1293 pUnk
= (IUnknown
*)0xdeadbeef;
1294 hr
= CoGetCallContext(&IID_IUnknown
, (void**)&pUnk
);
1295 ok_ole_success(hr
, "CoGetCallContext");
1296 ok(pUnk
== test_object
, "expected %p, got %p\n", test_object
, pUnk
);
1297 refs
= IUnknown_AddRef(test_object
);
1298 ok(refs
== 3, "Expected refcount 3, got %d\n", refs
);
1299 IUnknown_Release(test_object
);
1300 IUnknown_Release(pUnk
);
1302 pUnk
= (IUnknown
*)0xdeadbeef;
1303 hr
= pCoSwitchCallContext(NULL
, &pUnk
);
1304 ok_ole_success(hr
, "CoSwitchCallContext");
1305 ok(pUnk
== test_object
, "expected %p, got %p\n", test_object
, pUnk
);
1306 refs
= IUnknown_AddRef(test_object
);
1307 ok(refs
== 2, "Expected refcount 2, got %d\n", refs
);
1308 IUnknown_Release(test_object
);
1310 hr
= CoGetCallContext(&IID_IUnknown
, (void**)&pUnk
);
1311 ok(hr
== RPC_E_CALL_COMPLETE
, "Expected RPC_E_CALL_COMPLETE, got 0x%08x\n", hr
);
1313 IUnknown_Release(test_object
);
1318 static void test_CoGetContextToken(void)
1326 DWORD tid
, exitcode
;
1328 if (!pCoGetContextToken
)
1330 win_skip("CoGetContextToken not present\n");
1335 hr
= pCoGetContextToken(&token
);
1336 ok(hr
== CO_E_NOTINITIALIZED
, "Expected CO_E_NOTINITIALIZED, got 0x%08x\n", hr
);
1337 ok(token
== 0xdeadbeef, "Expected 0, got 0x%lx\n", token
);
1339 /* show that COM doesn't have to be initialized for multi-threaded apartments if another
1340 thread has already done so */
1342 info
.wait
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1343 ok(info
.wait
!= NULL
, "CreateEvent failed with error %d\n", GetLastError());
1345 info
.stop
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1346 ok(info
.stop
!= NULL
, "CreateEvent failed with error %d\n", GetLastError());
1348 thread
= CreateThread(NULL
, 0, ole_initialize_thread
, &info
, 0, &tid
);
1349 ok(thread
!= NULL
, "CreateThread failed with error %d\n", GetLastError());
1351 ok( !WaitForSingleObject(info
.wait
, 10000), "wait timed out\n" );
1354 hr
= pCoGetContextToken(&token
);
1355 ok(hr
== S_OK
, "Expected S_OK, got 0x%08x\n", hr
);
1357 SetEvent(info
.stop
);
1358 ok( !WaitForSingleObject(thread
, 10000), "wait timed out\n" );
1360 GetExitCodeThread(thread
, &exitcode
);
1362 ok(hr
== S_OK
, "thread should have returned S_OK instead of 0x%08x\n", hr
);
1364 CloseHandle(thread
);
1365 CloseHandle(info
.wait
);
1366 CloseHandle(info
.stop
);
1370 hr
= pCoGetContextToken(NULL
);
1371 ok(hr
== E_POINTER
, "Expected E_POINTER, got 0x%08x\n", hr
);
1374 hr
= pCoGetContextToken(&token
);
1375 ok(hr
== S_OK
, "Expected S_OK, got 0x%08x\n", hr
);
1376 ok(token
, "Expected token != 0\n");
1378 refs
= IUnknown_AddRef((IUnknown
*)token
);
1379 todo_wine
ok(refs
== 1, "Expected 1, got %u\n", refs
);
1381 hr
= pCoGetObjectContext(&IID_IObjContext
, (void **)&ctx
);
1382 ok(hr
== S_OK
, "Expected S_OK, got 0x%08x\n", hr
);
1383 todo_wine
ok(ctx
== (IObjContext
*)token
, "Expected interface pointers to be the same\n");
1385 refs
= IUnknown_AddRef((IUnknown
*)ctx
);
1386 todo_wine
ok(refs
== 3, "Expected 3, got %u\n", refs
);
1388 refs
= IUnknown_Release((IUnknown
*)ctx
);
1389 todo_wine
ok(refs
== 2, "Expected 2, got %u\n", refs
);
1391 refs
= IUnknown_Release((IUnknown
*)token
);
1392 ok(refs
== 1, "Expected 1, got %u\n", refs
);
1394 /* CoGetContextToken does not add a reference */
1396 hr
= pCoGetContextToken(&token
);
1397 ok(hr
== S_OK
, "Expected S_OK, got 0x%08x\n", hr
);
1398 ok(token
, "Expected token != 0\n");
1399 todo_wine
ok(ctx
== (IObjContext
*)token
, "Expected interface pointers to be the same\n");
1401 refs
= IUnknown_AddRef((IUnknown
*)ctx
);
1402 ok(refs
== 2, "Expected 1, got %u\n", refs
);
1404 refs
= IUnknown_Release((IUnknown
*)ctx
);
1405 ok(refs
== 1, "Expected 0, got %u\n", refs
);
1407 refs
= IUnknown_Release((IUnknown
*)ctx
);
1408 ok(refs
== 0, "Expected 0, got %u\n", refs
);
1413 static void test_CoGetTreatAsClass(void)
1417 static GUID deadbeef
= {0xdeadbeef,0xdead,0xbeef,{0xde,0xad,0xbe,0xef,0xde,0xad,0xbe,0xef}};
1419 if (!pCoGetTreatAsClass
)
1421 win_skip("CoGetTreatAsClass not present\n");
1424 hr
= pCoGetTreatAsClass(&deadbeef
,&out
);
1425 ok (hr
== S_FALSE
, "expected S_FALSE got %x\n",hr
);
1426 ok (IsEqualGUID(&out
,&deadbeef
), "expected to get same clsid back\n");
1429 static void test_CoInitializeEx(void)
1433 hr
= pCoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
1434 ok(hr
== S_OK
, "CoInitializeEx failed with error 0x%08x\n", hr
);
1436 /* Calling OleInitialize for the first time should yield S_OK even with
1437 * apartment already initialized by previous CoInitialize(Ex) calls. */
1438 hr
= OleInitialize(NULL
);
1439 ok(hr
== S_OK
, "OleInitialize failed with error 0x%08x\n", hr
);
1441 /* Subsequent calls to OleInitialize should return S_FALSE */
1442 hr
= OleInitialize(NULL
);
1443 ok(hr
== S_FALSE
, "Expected S_FALSE, hr = 0x%08x\n", hr
);
1452 HMODULE hOle32
= GetModuleHandle("ole32");
1453 pCoGetObjectContext
= (void*)GetProcAddress(hOle32
, "CoGetObjectContext");
1454 pCoSwitchCallContext
= (void*)GetProcAddress(hOle32
, "CoSwitchCallContext");
1455 pCoGetTreatAsClass
= (void*)GetProcAddress(hOle32
,"CoGetTreatAsClass");
1456 pCoGetContextToken
= (void*)GetProcAddress(hOle32
, "CoGetContextToken");
1457 if (!(pCoInitializeEx
= (void*)GetProcAddress(hOle32
, "CoInitializeEx")))
1459 trace("You need DCOM95 installed to run this test\n");
1463 test_ProgIDFromCLSID();
1464 test_CLSIDFromProgID();
1465 test_CLSIDFromString();
1466 test_StringFromGUID2();
1467 test_CoCreateInstance();
1469 test_CoGetClassObject();
1470 test_CoRegisterMessageFilter();
1471 test_CoRegisterPSClsid();
1472 test_CoGetPSClsid();
1473 test_CoUnmarshalInterface();
1474 test_CoGetInterfaceAndReleaseStream();
1475 test_CoMarshalInterface();
1476 test_CoMarshalInterThreadInterfaceInStream();
1477 test_CoRegisterClassObject();
1478 test_registered_object_thread_affinity();
1479 test_CoFreeUnusedLibraries();
1480 test_CoGetObjectContext();
1481 test_CoGetCallContext();
1482 test_CoGetContextToken();
1483 test_CoGetTreatAsClass();
1484 test_CoInitializeEx();