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
21 #define WIN32_NO_STATUS
23 #define COM_NO_WINDOWS_H
33 #define USE_COM_CONTEXT_DEF
34 //#include "initguid.h"
35 //#include "objbase.h"
36 //#include "shlguid.h"
38 //#include "urlmon.h" /* for CLSID_FileProtocol */
42 #include <wine/test.h>
44 extern const IID GUID_NULL
;
46 /* functions that are not present on all versions of Windows */
47 static HRESULT (WINAPI
* pCoInitializeEx
)(LPVOID lpReserved
, DWORD dwCoInit
);
48 static HRESULT (WINAPI
* pCoGetObjectContext
)(REFIID riid
, LPVOID
*ppv
);
49 static HRESULT (WINAPI
* pCoSwitchCallContext
)(IUnknown
*pObject
, IUnknown
**ppOldObject
);
50 static HRESULT (WINAPI
* pCoGetTreatAsClass
)(REFCLSID clsidOld
, LPCLSID pClsidNew
);
51 static HRESULT (WINAPI
* pCoGetContextToken
)(ULONG_PTR
*token
);
52 static LONG (WINAPI
* pRegOverridePredefKey
)(HKEY key
, HKEY override
);
54 #define ok_ole_success(hr, func) ok(hr == S_OK, func " failed with error 0x%08x\n", hr)
55 #define ok_more_than_one_lock() ok(cLocks > 0, "Number of locks should be > 0, but actually is %d\n", cLocks)
56 #define ok_no_locks() ok(cLocks == 0, "Number of locks should be 0, but actually is %d\n", cLocks)
58 static const CLSID CLSID_non_existent
= { 0x12345678, 0x1234, 0x1234, { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 } };
59 static const CLSID CLSID_StdFont
= { 0x0be35203, 0x8f91, 0x11ce, { 0x9d, 0xe3, 0x00, 0xaa, 0x00, 0x4b, 0xb8, 0x51 } };
60 static WCHAR stdfont
[] = {'S','t','d','F','o','n','t',0};
61 static const WCHAR wszNonExistent
[] = {'N','o','n','E','x','i','s','t','e','n','t',0};
62 static WCHAR wszCLSID_StdFont
[] =
64 '{','0','b','e','3','5','2','0','3','-','8','f','9','1','-','1','1','c','e','-',
65 '9','d','e','3','-','0','0','a','a','0','0','4','b','b','8','5','1','}',0
68 static const IID IID_IWineTest
=
73 {0xa1, 0xa2, 0x5d, 0x5a, 0x36, 0x54, 0xd3, 0xbd}
74 }; /* 5201163f-8164-4fd0-a1a2-5d5a3654d3bd */
75 static const CLSID CLSID_WineOOPTest
= {
79 {0xa1, 0xa2, 0x5d, 0x5a, 0x36, 0x54, 0xd3, 0xbd}
80 }; /* 5201163f-8164-4fd0-a1a2-5d5a3654d3bd */
84 static void LockModule(void)
86 InterlockedIncrement(&cLocks
);
89 static void UnlockModule(void)
91 InterlockedDecrement(&cLocks
);
94 static HRESULT WINAPI
Test_IClassFactory_QueryInterface(
99 if (ppvObj
== NULL
) return E_POINTER
;
101 if (IsEqualGUID(riid
, &IID_IUnknown
) ||
102 IsEqualGUID(riid
, &IID_IClassFactory
))
105 IClassFactory_AddRef(iface
);
110 return E_NOINTERFACE
;
113 static ULONG WINAPI
Test_IClassFactory_AddRef(LPCLASSFACTORY iface
)
116 return 2; /* non-heap-based object */
119 static ULONG WINAPI
Test_IClassFactory_Release(LPCLASSFACTORY iface
)
122 return 1; /* non-heap-based object */
125 static HRESULT WINAPI
Test_IClassFactory_CreateInstance(
126 LPCLASSFACTORY iface
,
132 if (pUnkOuter
) return CLASS_E_NOAGGREGATION
;
133 return E_NOINTERFACE
;
136 static HRESULT WINAPI
Test_IClassFactory_LockServer(
137 LPCLASSFACTORY iface
,
143 static const IClassFactoryVtbl TestClassFactory_Vtbl
=
145 Test_IClassFactory_QueryInterface
,
146 Test_IClassFactory_AddRef
,
147 Test_IClassFactory_Release
,
148 Test_IClassFactory_CreateInstance
,
149 Test_IClassFactory_LockServer
152 static IClassFactory Test_ClassFactory
= { &TestClassFactory_Vtbl
};
154 static void test_ProgIDFromCLSID(void)
157 HRESULT hr
= ProgIDFromCLSID(&CLSID_StdFont
, &progid
);
158 ok(hr
== S_OK
, "ProgIDFromCLSID failed with error 0x%08x\n", hr
);
161 ok(!lstrcmpiW(progid
, stdfont
), "Didn't get expected prog ID\n");
162 CoTaskMemFree(progid
);
165 progid
= (LPWSTR
)0xdeadbeef;
166 hr
= ProgIDFromCLSID(&CLSID_non_existent
, &progid
);
167 ok(hr
== REGDB_E_CLASSNOTREG
, "ProgIDFromCLSID returned %08x\n", hr
);
168 ok(progid
== NULL
, "ProgIDFromCLSID returns with progid %p\n", progid
);
170 hr
= ProgIDFromCLSID(&CLSID_StdFont
, NULL
);
171 ok(hr
== E_INVALIDARG
, "ProgIDFromCLSID should return E_INVALIDARG instead of 0x%08x\n", hr
);
174 static void test_CLSIDFromProgID(void)
177 HRESULT hr
= CLSIDFromProgID(stdfont
, &clsid
);
178 ok(hr
== S_OK
, "CLSIDFromProgID failed with error 0x%08x\n", hr
);
179 ok(IsEqualCLSID(&clsid
, &CLSID_StdFont
), "clsid wasn't equal to CLSID_StdFont\n");
181 hr
= CLSIDFromString(stdfont
, &clsid
);
182 ok_ole_success(hr
, "CLSIDFromString");
183 ok(IsEqualCLSID(&clsid
, &CLSID_StdFont
), "clsid wasn't equal to CLSID_StdFont\n");
185 /* test some failure cases */
187 hr
= CLSIDFromProgID(wszNonExistent
, NULL
);
188 ok(hr
== E_INVALIDARG
, "CLSIDFromProgID should have returned E_INVALIDARG instead of 0x%08x\n", hr
);
190 hr
= CLSIDFromProgID(NULL
, &clsid
);
191 ok(hr
== E_INVALIDARG
, "CLSIDFromProgID should have returned E_INVALIDARG instead of 0x%08x\n", hr
);
193 memset(&clsid
, 0xcc, sizeof(clsid
));
194 hr
= CLSIDFromProgID(wszNonExistent
, &clsid
);
195 ok(hr
== CO_E_CLASSSTRING
, "CLSIDFromProgID on nonexistent ProgID should have returned CO_E_CLASSSTRING instead of 0x%08x\n", hr
);
196 ok(IsEqualCLSID(&clsid
, &CLSID_NULL
), "CLSIDFromProgID should have set clsid to all-zeros on failure\n");
199 static void test_CLSIDFromString(void)
202 WCHAR wszCLSID_Broken
[50];
205 HRESULT hr
= CLSIDFromString(wszCLSID_StdFont
, &clsid
);
206 ok_ole_success(hr
, "CLSIDFromString");
207 ok(IsEqualCLSID(&clsid
, &CLSID_StdFont
), "clsid wasn't equal to CLSID_StdFont\n");
209 hr
= CLSIDFromString(NULL
, &clsid
);
210 ok_ole_success(hr
, "CLSIDFromString");
211 ok(IsEqualCLSID(&clsid
, &CLSID_NULL
), "clsid wasn't equal to CLSID_NULL\n");
213 lstrcpyW(wszCLSID_Broken
, wszCLSID_StdFont
);
214 for(i
= lstrlenW(wszCLSID_StdFont
); i
< 49; i
++)
215 wszCLSID_Broken
[i
] = 'A';
216 wszCLSID_Broken
[i
] = '\0';
218 memset(&clsid
, 0, sizeof(CLSID
));
219 hr
= CLSIDFromString(wszCLSID_Broken
, &clsid
);
220 ok(hr
== CO_E_CLASSSTRING
, "Got %08x\n", hr
);
221 ok(IsEqualCLSID(&clsid
, &CLSID_StdFont
), "clsid wasn't equal to CLSID_StdFont\n");
223 wszCLSID_Broken
[lstrlenW(wszCLSID_StdFont
)-1] = 'A';
224 memset(&clsid
, 0, sizeof(CLSID
));
225 hr
= CLSIDFromString(wszCLSID_Broken
, &clsid
);
226 ok(hr
== CO_E_CLASSSTRING
, "Got %08x\n", hr
);
227 ok(IsEqualCLSID(&clsid
, &CLSID_StdFont
), "clsid wasn't equal to CLSID_StdFont\n");
229 wszCLSID_Broken
[lstrlenW(wszCLSID_StdFont
)] = '\0';
230 memset(&clsid
, 0, sizeof(CLSID
));
231 hr
= CLSIDFromString(wszCLSID_Broken
, &clsid
);
232 ok(hr
== CO_E_CLASSSTRING
, "Got %08x\n", hr
);
233 ok(IsEqualCLSID(&clsid
, &CLSID_StdFont
), "clsid wasn't equal to CLSID_StdFont\n");
235 wszCLSID_Broken
[lstrlenW(wszCLSID_StdFont
)-1] = '\0';
236 memset(&clsid
, 0, sizeof(CLSID
));
237 hr
= CLSIDFromString(wszCLSID_Broken
, &clsid
);
238 ok(hr
== CO_E_CLASSSTRING
, "Got %08x\n", hr
);
239 ok(IsEqualCLSID(&clsid
, &CLSID_StdFont
), "clsid wasn't equal to CLSID_StdFont\n");
241 memset(&clsid
, 0xcc, sizeof(CLSID
));
242 hr
= CLSIDFromString(wszCLSID_Broken
+1, &clsid
);
243 ok(hr
== CO_E_CLASSSTRING
, "Got %08x\n", hr
);
244 ok(IsEqualCLSID(&clsid
, &CLSID_NULL
), "clsid wasn't equal to CLSID_NULL\n");
246 wszCLSID_Broken
[9] = '*';
247 memset(&clsid
, 0xcc, sizeof(CLSID
));
248 hr
= CLSIDFromString(wszCLSID_Broken
, &clsid
);
249 ok(hr
== CO_E_CLASSSTRING
, "Got %08x\n", hr
);
250 ok(clsid
.Data1
== CLSID_StdFont
.Data1
, "Got %08x\n", clsid
.Data1
);
251 ok(clsid
.Data2
== 0xcccc, "Got %04x\n", clsid
.Data2
);
253 wszCLSID_Broken
[3] = '*';
254 memset(&clsid
, 0xcc, sizeof(CLSID
));
255 hr
= CLSIDFromString(wszCLSID_Broken
, &clsid
);
256 ok(hr
== CO_E_CLASSSTRING
, "Got %08x\n", hr
);
257 ok(clsid
.Data1
== 0xb, "Got %08x\n", clsid
.Data1
);
258 ok(clsid
.Data2
== 0xcccc, "Got %04x\n", clsid
.Data2
);
260 wszCLSID_Broken
[3] = '\0';
261 memset(&clsid
, 0xcc, sizeof(CLSID
));
262 hr
= CLSIDFromString(wszCLSID_Broken
, &clsid
);
263 ok(hr
== CO_E_CLASSSTRING
, "Got %08x\n", hr
);
264 ok(clsid
.Data1
== 0xb, "Got %08x\n", clsid
.Data1
);
265 ok(clsid
.Data2
== 0xcccc, "Got %04x\n", clsid
.Data2
);
268 static void test_StringFromGUID2(void)
273 /* invalid pointer */
274 SetLastError(0xdeadbeef);
275 len
= StringFromGUID2(NULL
,str
,50);
276 ok(len
== 0, "len: %d (expected 0)\n", len
);
277 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %x\n", GetLastError());
279 /* Test corner cases for buffer size */
280 len
= StringFromGUID2(&CLSID_StdFont
,str
,50);
281 ok(len
== 39, "len: %d (expected 39)\n", len
);
282 ok(!lstrcmpiW(str
, wszCLSID_StdFont
),"string wasn't equal for CLSID_StdFont\n");
284 memset(str
,0,sizeof str
);
285 len
= StringFromGUID2(&CLSID_StdFont
,str
,39);
286 ok(len
== 39, "len: %d (expected 39)\n", len
);
287 ok(!lstrcmpiW(str
, wszCLSID_StdFont
),"string wasn't equal for CLSID_StdFont\n");
289 len
= StringFromGUID2(&CLSID_StdFont
,str
,38);
290 ok(len
== 0, "len: %d (expected 0)\n", len
);
292 len
= StringFromGUID2(&CLSID_StdFont
,str
,30);
293 ok(len
== 0, "len: %d (expected 0)\n", len
);
301 static DWORD CALLBACK
ole_initialize_thread(LPVOID pv
)
304 struct info
*info
= pv
;
306 hr
= pCoInitializeEx(NULL
, COINIT_MULTITHREADED
);
308 SetEvent(info
->wait
);
309 WaitForSingleObject(info
->stop
, 10000);
315 static void test_CoCreateInstance(void)
322 REFCLSID rclsid
= &CLSID_InternetZoneManager
;
324 pUnk
= (IUnknown
*)0xdeadbeef;
325 hr
= CoCreateInstance(rclsid
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IUnknown
, (void **)&pUnk
);
326 ok(hr
== CO_E_NOTINITIALIZED
, "CoCreateInstance should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n", hr
);
327 ok(pUnk
== NULL
, "CoCreateInstance should have changed the passed in pointer to NULL, instead of %p\n", pUnk
);
331 /* test errors returned for non-registered clsids */
332 hr
= CoCreateInstance(&CLSID_non_existent
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IUnknown
, (void **)&pUnk
);
333 ok(hr
== REGDB_E_CLASSNOTREG
, "CoCreateInstance for non-registered inproc server should have returned REGDB_E_CLASSNOTREG instead of 0x%08x\n", hr
);
334 hr
= CoCreateInstance(&CLSID_non_existent
, NULL
, CLSCTX_INPROC_HANDLER
, &IID_IUnknown
, (void **)&pUnk
);
335 ok(hr
== REGDB_E_CLASSNOTREG
, "CoCreateInstance for non-registered inproc handler should have returned REGDB_E_CLASSNOTREG instead of 0x%08x\n", hr
);
336 hr
= CoCreateInstance(&CLSID_non_existent
, NULL
, CLSCTX_LOCAL_SERVER
, &IID_IUnknown
, (void **)&pUnk
);
337 ok(hr
== REGDB_E_CLASSNOTREG
, "CoCreateInstance for non-registered local server should have returned REGDB_E_CLASSNOTREG instead of 0x%08x\n", hr
);
338 hr
= CoCreateInstance(&CLSID_non_existent
, NULL
, CLSCTX_REMOTE_SERVER
, &IID_IUnknown
, (void **)&pUnk
);
339 ok(hr
== REGDB_E_CLASSNOTREG
, "CoCreateInstance for non-registered remote server should have returned REGDB_E_CLASSNOTREG instead of 0x%08x\n", hr
);
341 hr
= CoCreateInstance(rclsid
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IUnknown
, (void **)&pUnk
);
342 if(hr
== REGDB_E_CLASSNOTREG
)
344 skip("IE not installed so can't test CoCreateInstance\n");
349 ok_ole_success(hr
, "CoCreateInstance");
350 if(pUnk
) IUnknown_Release(pUnk
);
353 hr
= CoCreateInstance(rclsid
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IUnknown
, (void **)&pUnk
);
354 ok(hr
== CO_E_NOTINITIALIZED
, "CoCreateInstance should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n", hr
);
356 /* show that COM doesn't have to be initialized for multi-threaded apartments if another
357 thread has already done so */
359 info
.wait
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
360 ok(info
.wait
!= NULL
, "CreateEvent failed with error %d\n", GetLastError());
362 info
.stop
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
363 ok(info
.stop
!= NULL
, "CreateEvent failed with error %d\n", GetLastError());
365 thread
= CreateThread(NULL
, 0, ole_initialize_thread
, &info
, 0, &tid
);
366 ok(thread
!= NULL
, "CreateThread failed with error %d\n", GetLastError());
368 ok( !WaitForSingleObject(info
.wait
, 10000 ), "wait timed out\n" );
370 pUnk
= (IUnknown
*)0xdeadbeef;
371 hr
= CoCreateInstance(rclsid
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IUnknown
, (void **)&pUnk
);
372 ok(hr
== S_OK
, "CoCreateInstance should have returned S_OK instead of 0x%08x\n", hr
);
373 if (pUnk
) IUnknown_Release(pUnk
);
376 ok( !WaitForSingleObject(thread
, 10000), "wait timed out\n" );
378 GetExitCodeThread(thread
, &exitcode
);
380 ok(hr
== S_OK
, "thread should have returned S_OK instead of 0x%08x\n", hr
);
383 CloseHandle(info
.wait
);
384 CloseHandle(info
.stop
);
387 static void test_CoGetClassObject(void)
394 REFCLSID rclsid
= &CLSID_InternetZoneManager
;
398 hr
= CoGetClassObject(rclsid
, CLSCTX_INPROC_SERVER
, NULL
, &IID_IUnknown
, (void **)&pUnk
);
399 ok(hr
== CO_E_NOTINITIALIZED
, "CoGetClassObject should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n", hr
);
400 ok(pUnk
== NULL
, "CoGetClassObject should have changed the passed in pointer to NULL, instead of %p\n", pUnk
);
402 hr
= CoGetClassObject(rclsid
, CLSCTX_INPROC_SERVER
, NULL
, &IID_IUnknown
, NULL
);
403 ok(hr
== E_INVALIDARG
||
404 broken(hr
== CO_E_NOTINITIALIZED
), /* win9x */
405 "CoGetClassObject should have returned E_INVALIDARG instead of 0x%08x\n", hr
);
407 /* show that COM doesn't have to be initialized for multi-threaded apartments if another
408 thread has already done so */
410 info
.wait
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
411 ok(info
.wait
!= NULL
, "CreateEvent failed with error %d\n", GetLastError());
413 info
.stop
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
414 ok(info
.stop
!= NULL
, "CreateEvent failed with error %d\n", GetLastError());
416 thread
= CreateThread(NULL
, 0, ole_initialize_thread
, &info
, 0, &tid
);
417 ok(thread
!= NULL
, "CreateThread failed with error %d\n", GetLastError());
419 ok( !WaitForSingleObject(info
.wait
, 10000), "wait timed out\n" );
421 pUnk
= (IUnknown
*)0xdeadbeef;
422 hr
= CoGetClassObject(rclsid
, CLSCTX_INPROC_SERVER
, NULL
, &IID_IUnknown
, (void **)&pUnk
);
423 if(hr
== REGDB_E_CLASSNOTREG
)
424 skip("IE not installed so can't test CoGetClassObject\n");
427 ok(hr
== S_OK
, "CoGetClassObject should have returned S_OK instead of 0x%08x\n", hr
);
428 if (pUnk
) IUnknown_Release(pUnk
);
432 ok( !WaitForSingleObject(thread
, 10000), "wait timed out\n" );
434 GetExitCodeThread(thread
, &exitcode
);
436 ok(hr
== S_OK
, "thread should have returned S_OK instead of 0x%08x\n", hr
);
439 CloseHandle(info
.wait
);
440 CloseHandle(info
.stop
);
442 pCoInitializeEx(NULL
, COINIT_MULTITHREADED
);
444 hr
= CoGetClassObject(rclsid
, CLSCTX_INPROC_SERVER
, NULL
, &IID_IUnknown
, (void **)&pUnk
);
447 IUnknown_Release(pUnk
);
449 res
= RegOpenKeyExA(HKEY_CURRENT_USER
, "Software\\Classes", 0, KEY_ALL_ACCESS
, &hkey
);
450 ok(!res
, "RegOpenKeyExA returned %d\n", res
);
452 res
= pRegOverridePredefKey(HKEY_CLASSES_ROOT
, hkey
);
453 ok(!res
, "RegOverridePredefKey returned %d\n", res
);
455 hr
= CoGetClassObject(rclsid
, CLSCTX_INPROC_SERVER
, NULL
, &IID_IUnknown
, (void **)&pUnk
);
456 ok(hr
== S_OK
, "CoGetClassObject should have returned S_OK instead of 0x%08x\n", hr
);
458 res
= pRegOverridePredefKey(HKEY_CLASSES_ROOT
, NULL
);
459 ok(!res
, "RegOverridePredefKey returned %d\n", res
);
461 if (hr
== S_OK
) IUnknown_Release(pUnk
);
467 static ATOM
register_dummy_class(void)
475 GetModuleHandle(NULL
),
477 LoadCursor(NULL
, IDC_ARROW
),
478 (HBRUSH
)(COLOR_BTNFACE
+1),
480 TEXT("WineOleTestClass"),
483 return RegisterClass(&wc
);
486 static void test_ole_menu(void)
491 hwndFrame
= CreateWindow(MAKEINTATOM(register_dummy_class()), "Test", 0, CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
, NULL
, NULL
, NULL
, NULL
);
492 hr
= OleSetMenuDescriptor(NULL
, hwndFrame
, NULL
, NULL
, NULL
);
493 todo_wine
ok_ole_success(hr
, "OleSetMenuDescriptor");
495 DestroyWindow(hwndFrame
);
499 static HRESULT WINAPI
MessageFilter_QueryInterface(IMessageFilter
*iface
, REFIID riid
, void ** ppvObj
)
501 if (ppvObj
== NULL
) return E_POINTER
;
503 if (IsEqualGUID(riid
, &IID_IUnknown
) ||
504 IsEqualGUID(riid
, &IID_IClassFactory
))
507 IMessageFilter_AddRef(iface
);
511 return E_NOINTERFACE
;
514 static ULONG WINAPI
MessageFilter_AddRef(IMessageFilter
*iface
)
516 return 2; /* non-heap object */
519 static ULONG WINAPI
MessageFilter_Release(IMessageFilter
*iface
)
521 return 1; /* non-heap object */
524 static DWORD WINAPI
MessageFilter_HandleInComingCall(
525 IMessageFilter
*iface
,
527 HTASK threadIDCaller
,
529 LPINTERFACEINFO lpInterfaceInfo
)
531 trace("HandleInComingCall\n");
532 return SERVERCALL_ISHANDLED
;
535 static DWORD WINAPI
MessageFilter_RetryRejectedCall(
536 IMessageFilter
*iface
,
537 HTASK threadIDCallee
,
541 trace("RetryRejectedCall\n");
545 static DWORD WINAPI
MessageFilter_MessagePending(
546 IMessageFilter
*iface
,
547 HTASK threadIDCallee
,
551 trace("MessagePending\n");
552 return PENDINGMSG_WAITNOPROCESS
;
555 static const IMessageFilterVtbl MessageFilter_Vtbl
=
557 MessageFilter_QueryInterface
,
558 MessageFilter_AddRef
,
559 MessageFilter_Release
,
560 MessageFilter_HandleInComingCall
,
561 MessageFilter_RetryRejectedCall
,
562 MessageFilter_MessagePending
565 static IMessageFilter MessageFilter
= { &MessageFilter_Vtbl
};
567 static void test_CoRegisterMessageFilter(void)
570 IMessageFilter
*prev_filter
;
572 hr
= CoRegisterMessageFilter(&MessageFilter
, &prev_filter
);
573 ok(hr
== CO_E_NOT_SUPPORTED
,
574 "CoRegisterMessageFilter should have failed with CO_E_NOT_SUPPORTED instead of 0x%08x\n",
577 pCoInitializeEx(NULL
, COINIT_MULTITHREADED
);
578 prev_filter
= (IMessageFilter
*)0xdeadbeef;
579 hr
= CoRegisterMessageFilter(&MessageFilter
, &prev_filter
);
580 ok(hr
== CO_E_NOT_SUPPORTED
,
581 "CoRegisterMessageFilter should have failed with CO_E_NOT_SUPPORTED instead of 0x%08x\n",
583 ok(prev_filter
== (IMessageFilter
*)0xdeadbeef,
584 "prev_filter should have been set to %p\n", prev_filter
);
587 pCoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
589 hr
= CoRegisterMessageFilter(NULL
, NULL
);
590 ok_ole_success(hr
, "CoRegisterMessageFilter");
592 prev_filter
= (IMessageFilter
*)0xdeadbeef;
593 hr
= CoRegisterMessageFilter(NULL
, &prev_filter
);
594 ok_ole_success(hr
, "CoRegisterMessageFilter");
595 ok(prev_filter
== NULL
, "prev_filter should have been set to NULL instead of %p\n", prev_filter
);
597 hr
= CoRegisterMessageFilter(&MessageFilter
, &prev_filter
);
598 ok_ole_success(hr
, "CoRegisterMessageFilter");
599 ok(prev_filter
== NULL
, "prev_filter should have been set to NULL instead of %p\n", prev_filter
);
601 hr
= CoRegisterMessageFilter(NULL
, NULL
);
602 ok_ole_success(hr
, "CoRegisterMessageFilter");
607 static HRESULT WINAPI
Test_IUnknown_QueryInterface(
612 if (ppvObj
== NULL
) return E_POINTER
;
614 if (IsEqualIID(riid
, &IID_IUnknown
) ||
615 IsEqualIID(riid
, &IID_IWineTest
))
618 IUnknown_AddRef(iface
);
623 return E_NOINTERFACE
;
626 static ULONG WINAPI
Test_IUnknown_AddRef(IUnknown
*iface
)
628 return 2; /* non-heap-based object */
631 static ULONG WINAPI
Test_IUnknown_Release(IUnknown
*iface
)
633 return 1; /* non-heap-based object */
636 static const IUnknownVtbl TestUnknown_Vtbl
=
638 Test_IUnknown_QueryInterface
,
639 Test_IUnknown_AddRef
,
640 Test_IUnknown_Release
,
643 static IUnknown Test_Unknown
= { &TestUnknown_Vtbl
};
645 static HRESULT WINAPI
PSFactoryBuffer_QueryInterface(
646 IPSFactoryBuffer
* This
,
647 /* [in] */ REFIID riid
,
648 /* [iid_is][out] */ void **ppvObject
)
650 if (IsEqualIID(riid
, &IID_IUnknown
) ||
651 IsEqualIID(riid
, &IID_IPSFactoryBuffer
))
654 IPSFactoryBuffer_AddRef(This
);
657 return E_NOINTERFACE
;
660 static ULONG WINAPI
PSFactoryBuffer_AddRef(
661 IPSFactoryBuffer
* This
)
666 static ULONG WINAPI
PSFactoryBuffer_Release(
667 IPSFactoryBuffer
* This
)
672 static HRESULT WINAPI
PSFactoryBuffer_CreateProxy(
673 IPSFactoryBuffer
* This
,
674 /* [in] */ IUnknown
*pUnkOuter
,
675 /* [in] */ REFIID riid
,
676 /* [out] */ IRpcProxyBuffer
**ppProxy
,
677 /* [out] */ void **ppv
)
682 static HRESULT WINAPI
PSFactoryBuffer_CreateStub(
683 IPSFactoryBuffer
* This
,
684 /* [in] */ REFIID riid
,
685 /* [unique][in] */ IUnknown
*pUnkServer
,
686 /* [out] */ IRpcStubBuffer
**ppStub
)
691 static IPSFactoryBufferVtbl PSFactoryBufferVtbl
=
693 PSFactoryBuffer_QueryInterface
,
694 PSFactoryBuffer_AddRef
,
695 PSFactoryBuffer_Release
,
696 PSFactoryBuffer_CreateProxy
,
697 PSFactoryBuffer_CreateStub
700 static IPSFactoryBuffer PSFactoryBuffer
= { &PSFactoryBufferVtbl
};
702 static const CLSID CLSID_WineTestPSFactoryBuffer
=
707 {0xa1, 0xa2, 0x5d, 0x5a, 0x36, 0x54, 0xd3, 0xbd}
708 }; /* 52011640-8164-4fd0-a1a2-5d5a3654d3bd */
710 static void test_CoRegisterPSClsid(void)
713 DWORD dwRegistrationKey
;
717 hr
= CoRegisterPSClsid(&IID_IWineTest
, &CLSID_WineTestPSFactoryBuffer
);
718 ok(hr
== CO_E_NOTINITIALIZED
, "CoRegisterPSClsid should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n", hr
);
720 pCoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
722 hr
= CoRegisterClassObject(&CLSID_WineTestPSFactoryBuffer
, (IUnknown
*)&PSFactoryBuffer
,
723 CLSCTX_INPROC_SERVER
, REGCLS_MULTIPLEUSE
, &dwRegistrationKey
);
724 ok_ole_success(hr
, "CoRegisterClassObject");
726 hr
= CoRegisterPSClsid(&IID_IWineTest
, &CLSID_WineTestPSFactoryBuffer
);
727 ok_ole_success(hr
, "CoRegisterPSClsid");
729 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &stream
);
730 ok_ole_success(hr
, "CreateStreamOnHGlobal");
732 hr
= CoMarshalInterface(stream
, &IID_IWineTest
, &Test_Unknown
, MSHCTX_INPROC
, NULL
, MSHLFLAGS_NORMAL
);
733 ok(hr
== E_NOTIMPL
, "CoMarshalInterface should have returned E_NOTIMPL instead of 0x%08x\n", hr
);
734 IStream_Release(stream
);
736 hr
= CoRevokeClassObject(dwRegistrationKey
);
737 ok_ole_success(hr
, "CoRevokeClassObject");
741 pCoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
743 hr
= CoGetPSClsid(&IID_IWineTest
, &clsid
);
744 ok(hr
== REGDB_E_IIDNOTREG
, "CoGetPSClsid should have returned REGDB_E_IIDNOTREG instead of 0x%08x\n", hr
);
749 static void test_CoGetPSClsid(void)
756 hr
= CoGetPSClsid(&IID_IClassFactory
, &clsid
);
757 ok(hr
== CO_E_NOTINITIALIZED
,
758 "CoGetPSClsid should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n",
761 pCoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
763 hr
= CoGetPSClsid(&IID_IClassFactory
, &clsid
);
764 ok_ole_success(hr
, "CoGetPSClsid");
766 hr
= CoGetPSClsid(&IID_IWineTest
, &clsid
);
767 ok(hr
== REGDB_E_IIDNOTREG
,
768 "CoGetPSClsid for random IID returned 0x%08x instead of REGDB_E_IIDNOTREG\n",
771 hr
= CoGetPSClsid(&IID_IClassFactory
, NULL
);
772 ok(hr
== E_INVALIDARG
,
773 "CoGetPSClsid for null clsid returned 0x%08x instead of E_INVALIDARG\n",
776 if (!pRegOverridePredefKey
)
778 win_skip("RegOverridePredefKey not available\n");
782 hr
= CoGetPSClsid(&IID_IClassFactory
, &clsid
);
783 ok_ole_success(hr
, "CoGetPSClsid");
785 res
= RegOpenKeyExA(HKEY_CURRENT_USER
, "Software\\Classes", 0, KEY_ALL_ACCESS
, &hkey
);
786 ok(!res
, "RegOpenKeyExA returned %d\n", res
);
788 res
= pRegOverridePredefKey(HKEY_CLASSES_ROOT
, hkey
);
789 ok(!res
, "RegOverridePredefKey returned %d\n", res
);
791 hr
= CoGetPSClsid(&IID_IClassFactory
, &clsid
);
792 ok_ole_success(hr
, "CoGetPSClsid");
794 res
= pRegOverridePredefKey(HKEY_CLASSES_ROOT
, NULL
);
795 ok(!res
, "RegOverridePredefKey returned %d\n", res
);
801 /* basic test, mainly for invalid arguments. see marshal.c for more */
802 static void test_CoUnmarshalInterface(void)
808 hr
= CoUnmarshalInterface(NULL
, &IID_IUnknown
, (void **)&pProxy
);
809 ok(hr
== E_INVALIDARG
, "CoUnmarshalInterface should have returned E_INVALIDARG instead of 0x%08x\n", hr
);
811 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
812 ok_ole_success(hr
, "CreateStreamOnHGlobal");
814 hr
= CoUnmarshalInterface(pStream
, &IID_IUnknown
, (void **)&pProxy
);
816 ok(hr
== CO_E_NOTINITIALIZED
, "CoUnmarshalInterface should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n", hr
);
818 pCoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
820 hr
= CoUnmarshalInterface(pStream
, &IID_IUnknown
, (void **)&pProxy
);
821 ok(hr
== STG_E_READFAULT
, "CoUnmarshalInterface should have returned STG_E_READFAULT instead of 0x%08x\n", hr
);
825 hr
= CoUnmarshalInterface(pStream
, &IID_IUnknown
, NULL
);
826 ok(hr
== E_INVALIDARG
, "CoUnmarshalInterface should have returned E_INVALIDARG instead of 0x%08x\n", hr
);
828 IStream_Release(pStream
);
831 static void test_CoGetInterfaceAndReleaseStream(void)
836 pCoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
838 hr
= CoGetInterfaceAndReleaseStream(NULL
, &IID_IUnknown
, (void**)&pUnk
);
839 ok(hr
== E_INVALIDARG
, "hr %08x\n", hr
);
844 /* basic test, mainly for invalid arguments. see marshal.c for more */
845 static void test_CoMarshalInterface(void)
849 static const LARGE_INTEGER llZero
;
851 pCoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
853 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
854 ok_ole_success(hr
, "CreateStreamOnHGlobal");
856 hr
= CoMarshalInterface(pStream
, &IID_IUnknown
, NULL
, MSHCTX_INPROC
, NULL
, MSHLFLAGS_NORMAL
);
857 ok(hr
== E_INVALIDARG
, "CoMarshalInterface should have returned E_INVALIDARG instead of 0x%08x\n", hr
);
859 hr
= CoMarshalInterface(NULL
, &IID_IUnknown
, (IUnknown
*)&Test_ClassFactory
, MSHCTX_INPROC
, NULL
, MSHLFLAGS_NORMAL
);
860 ok(hr
== E_INVALIDARG
, "CoMarshalInterface should have returned E_INVALIDARG instead of 0x%08x\n", hr
);
862 hr
= CoMarshalInterface(pStream
, &IID_IUnknown
, (IUnknown
*)&Test_ClassFactory
, MSHCTX_INPROC
, NULL
, MSHLFLAGS_NORMAL
);
863 ok_ole_success(hr
, "CoMarshalInterface");
865 /* stream not rewound */
866 hr
= CoReleaseMarshalData(pStream
);
867 ok(hr
== STG_E_READFAULT
, "CoReleaseMarshalData should have returned STG_E_READFAULT instead of 0x%08x\n", hr
);
869 hr
= IStream_Seek(pStream
, llZero
, STREAM_SEEK_SET
, NULL
);
870 ok_ole_success(hr
, "IStream_Seek");
872 hr
= CoReleaseMarshalData(pStream
);
873 ok_ole_success(hr
, "CoReleaseMarshalData");
875 IStream_Release(pStream
);
880 static void test_CoMarshalInterThreadInterfaceInStream(void)
884 IClassFactory
*pProxy
;
886 pCoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
890 hr
= CoMarshalInterThreadInterfaceInStream(&IID_IUnknown
, (IUnknown
*)&Test_ClassFactory
, NULL
);
891 ok(hr
== E_INVALIDARG
, "CoMarshalInterThreadInterfaceInStream should have returned E_INVALIDARG instead of 0x%08x\n", hr
);
893 hr
= CoMarshalInterThreadInterfaceInStream(&IID_IUnknown
, NULL
, &pStream
);
894 ok(hr
== E_INVALIDARG
, "CoMarshalInterThreadInterfaceInStream should have returned E_INVALIDARG instead of 0x%08x\n", hr
);
898 hr
= CoMarshalInterThreadInterfaceInStream(&IID_IUnknown
, (IUnknown
*)&Test_ClassFactory
, &pStream
);
899 ok_ole_success(hr
, "CoMarshalInterThreadInterfaceInStream");
901 ok_more_than_one_lock();
903 hr
= CoUnmarshalInterface(pStream
, &IID_IClassFactory
, (void **)&pProxy
);
904 ok_ole_success(hr
, "CoUnmarshalInterface");
906 IClassFactory_Release(pProxy
);
907 IStream_Release(pStream
);
914 static void test_CoRegisterClassObject(void)
920 pCoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
922 /* CLSCTX_INPROC_SERVER */
923 hr
= CoRegisterClassObject(&CLSID_WineOOPTest
, (IUnknown
*)&Test_ClassFactory
,
924 CLSCTX_INPROC_SERVER
, REGCLS_SINGLEUSE
, &cookie
);
925 ok_ole_success(hr
, "CoRegisterClassObject");
926 hr
= CoRevokeClassObject(cookie
);
927 ok_ole_success(hr
, "CoRevokeClassObject");
929 hr
= CoRegisterClassObject(&CLSID_WineOOPTest
, (IUnknown
*)&Test_ClassFactory
,
930 CLSCTX_INPROC_SERVER
, REGCLS_MULTIPLEUSE
, &cookie
);
931 ok_ole_success(hr
, "CoRegisterClassObject");
932 hr
= CoRevokeClassObject(cookie
);
933 ok_ole_success(hr
, "CoRevokeClassObject");
935 hr
= CoRegisterClassObject(&CLSID_WineOOPTest
, (IUnknown
*)&Test_ClassFactory
,
936 CLSCTX_INPROC_SERVER
, REGCLS_MULTI_SEPARATE
, &cookie
);
937 ok_ole_success(hr
, "CoRegisterClassObject");
938 hr
= CoRevokeClassObject(cookie
);
939 ok_ole_success(hr
, "CoRevokeClassObject");
941 /* CLSCTX_LOCAL_SERVER */
942 hr
= CoRegisterClassObject(&CLSID_WineOOPTest
, (IUnknown
*)&Test_ClassFactory
,
943 CLSCTX_LOCAL_SERVER
, REGCLS_SINGLEUSE
, &cookie
);
944 ok_ole_success(hr
, "CoRegisterClassObject");
945 hr
= CoRevokeClassObject(cookie
);
946 ok_ole_success(hr
, "CoRevokeClassObject");
948 hr
= CoRegisterClassObject(&CLSID_WineOOPTest
, (IUnknown
*)&Test_ClassFactory
,
949 CLSCTX_LOCAL_SERVER
, REGCLS_MULTIPLEUSE
, &cookie
);
950 ok_ole_success(hr
, "CoRegisterClassObject");
951 hr
= CoRevokeClassObject(cookie
);
952 ok_ole_success(hr
, "CoRevokeClassObject");
954 hr
= CoRegisterClassObject(&CLSID_WineOOPTest
, (IUnknown
*)&Test_ClassFactory
,
955 CLSCTX_LOCAL_SERVER
, REGCLS_MULTI_SEPARATE
, &cookie
);
956 ok_ole_success(hr
, "CoRegisterClassObject");
957 hr
= CoRevokeClassObject(cookie
);
958 ok_ole_success(hr
, "CoRevokeClassObject");
960 /* CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER */
961 hr
= CoRegisterClassObject(&CLSID_WineOOPTest
, (IUnknown
*)&Test_ClassFactory
,
962 CLSCTX_INPROC_SERVER
|CLSCTX_LOCAL_SERVER
, REGCLS_SINGLEUSE
, &cookie
);
963 ok_ole_success(hr
, "CoRegisterClassObject");
964 hr
= CoRevokeClassObject(cookie
);
965 ok_ole_success(hr
, "CoRevokeClassObject");
967 /* test whether an object that doesn't support IClassFactory can be
968 * registered for CLSCTX_LOCAL_SERVER */
969 hr
= CoRegisterClassObject(&CLSID_WineOOPTest
, &Test_Unknown
,
970 CLSCTX_LOCAL_SERVER
, REGCLS_SINGLEUSE
, &cookie
);
971 ok_ole_success(hr
, "CoRegisterClassObject");
972 hr
= CoRevokeClassObject(cookie
);
973 ok_ole_success(hr
, "CoRevokeClassObject");
975 /* test whether registered class becomes invalid when apartment is destroyed */
976 hr
= CoRegisterClassObject(&CLSID_WineOOPTest
, (IUnknown
*)&Test_ClassFactory
,
977 CLSCTX_INPROC_SERVER
, REGCLS_SINGLEUSE
, &cookie
);
978 ok_ole_success(hr
, "CoRegisterClassObject");
981 pCoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
983 hr
= CoGetClassObject(&CLSID_WineOOPTest
, CLSCTX_INPROC_SERVER
, NULL
,
984 &IID_IClassFactory
, (void **)&pcf
);
985 ok(hr
== REGDB_E_CLASSNOTREG
, "object registered in an apartment shouldn't accessible after it is destroyed\n");
987 /* crashes with at least win9x DCOM! */
989 CoRevokeClassObject(cookie
);
994 static HRESULT
get_class_object(CLSCTX clsctx
)
999 hr
= CoGetClassObject(&CLSID_WineOOPTest
, clsctx
, NULL
, &IID_IClassFactory
,
1003 IClassFactory_Release(pcf
);
1008 static DWORD CALLBACK
get_class_object_thread(LPVOID pv
)
1010 CLSCTX clsctx
= (CLSCTX
)(DWORD_PTR
)pv
;
1013 pCoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
1015 hr
= get_class_object(clsctx
);
1022 static DWORD CALLBACK
get_class_object_proxy_thread(LPVOID pv
)
1024 CLSCTX clsctx
= (CLSCTX
)(DWORD_PTR
)pv
;
1029 pCoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
1031 hr
= CoGetClassObject(&CLSID_WineOOPTest
, clsctx
, NULL
, &IID_IClassFactory
,
1036 hr
= IClassFactory_QueryInterface(pcf
, &IID_IMultiQI
, (void **)&pMQI
);
1038 IMultiQI_Release(pMQI
);
1039 IClassFactory_Release(pcf
);
1047 static DWORD CALLBACK
register_class_object_thread(LPVOID pv
)
1052 pCoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
1054 hr
= CoRegisterClassObject(&CLSID_WineOOPTest
, (IUnknown
*)&Test_ClassFactory
,
1055 CLSCTX_INPROC_SERVER
, REGCLS_SINGLEUSE
, &cookie
);
1062 static DWORD CALLBACK
revoke_class_object_thread(LPVOID pv
)
1064 DWORD cookie
= (DWORD_PTR
)pv
;
1067 pCoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
1069 hr
= CoRevokeClassObject(cookie
);
1076 static void test_registered_object_thread_affinity(void)
1084 pCoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
1086 /* CLSCTX_INPROC_SERVER */
1088 hr
= CoRegisterClassObject(&CLSID_WineOOPTest
, (IUnknown
*)&Test_ClassFactory
,
1089 CLSCTX_INPROC_SERVER
, REGCLS_SINGLEUSE
, &cookie
);
1090 ok_ole_success(hr
, "CoRegisterClassObject");
1092 thread
= CreateThread(NULL
, 0, get_class_object_thread
, (LPVOID
)CLSCTX_INPROC_SERVER
, 0, &tid
);
1093 ok(thread
!= NULL
, "CreateThread failed with error %d\n", GetLastError());
1094 ok( !WaitForSingleObject(thread
, 10000), "wait timed out\n" );
1095 GetExitCodeThread(thread
, &exitcode
);
1097 ok(hr
== REGDB_E_CLASSNOTREG
, "CoGetClassObject on inproc object "
1098 "registered in different thread should return REGDB_E_CLASSNOTREG "
1099 "instead of 0x%08x\n", hr
);
1101 hr
= get_class_object(CLSCTX_INPROC_SERVER
);
1102 ok(hr
== S_OK
, "CoGetClassObject on inproc object registered in same "
1103 "thread should return S_OK instead of 0x%08x\n", hr
);
1105 thread
= CreateThread(NULL
, 0, register_class_object_thread
, NULL
, 0, &tid
);
1106 ok(thread
!= NULL
, "CreateThread failed with error %d\n", GetLastError());
1107 ok ( !WaitForSingleObject(thread
, 10000), "wait timed out\n" );
1108 GetExitCodeThread(thread
, &exitcode
);
1110 ok(hr
== S_OK
, "CoRegisterClassObject with same CLSID but in different thread should return S_OK instead of 0x%08x\n", hr
);
1112 hr
= CoRevokeClassObject(cookie
);
1113 ok_ole_success(hr
, "CoRevokeClassObject");
1115 /* CLSCTX_LOCAL_SERVER */
1117 hr
= CoRegisterClassObject(&CLSID_WineOOPTest
, (IUnknown
*)&Test_ClassFactory
,
1118 CLSCTX_LOCAL_SERVER
, REGCLS_MULTIPLEUSE
, &cookie
);
1119 ok_ole_success(hr
, "CoRegisterClassObject");
1121 thread
= CreateThread(NULL
, 0, get_class_object_proxy_thread
, (LPVOID
)CLSCTX_LOCAL_SERVER
, 0, &tid
);
1122 ok(thread
!= NULL
, "CreateThread failed with error %d\n", GetLastError());
1123 while (MsgWaitForMultipleObjects(1, &thread
, FALSE
, 10000, QS_ALLINPUT
) == WAIT_OBJECT_0
+ 1)
1126 while (PeekMessageA(&msg
, NULL
, 0, 0, PM_REMOVE
))
1128 TranslateMessage(&msg
);
1129 DispatchMessageA(&msg
);
1132 GetExitCodeThread(thread
, &exitcode
);
1134 ok(hr
== S_OK
, "CoGetClassObject on local server object "
1135 "registered in different thread should return S_OK "
1136 "instead of 0x%08x\n", hr
);
1138 hr
= get_class_object(CLSCTX_LOCAL_SERVER
);
1139 ok(hr
== S_OK
, "CoGetClassObject on local server object registered in same "
1140 "thread should return S_OK instead of 0x%08x\n", hr
);
1142 thread
= CreateThread(NULL
, 0, revoke_class_object_thread
, (LPVOID
)(DWORD_PTR
)cookie
, 0, &tid
);
1143 ok(thread
!= NULL
, "CreateThread failed with error %d\n", GetLastError());
1144 ok( !WaitForSingleObject(thread
, 10000), "wait timed out\n" );
1145 GetExitCodeThread(thread
, &exitcode
);
1147 ok(hr
== RPC_E_WRONG_THREAD
, "CoRevokeClassObject called from different "
1148 "thread to where registered should return RPC_E_WRONG_THREAD instead of 0x%08x\n", hr
);
1150 thread
= CreateThread(NULL
, 0, register_class_object_thread
, NULL
, 0, &tid
);
1151 ok(thread
!= NULL
, "CreateThread failed with error %d\n", GetLastError());
1152 ok( !WaitForSingleObject(thread
, 10000), "wait timed out\n" );
1153 GetExitCodeThread(thread
, &exitcode
);
1155 ok(hr
== S_OK
, "CoRegisterClassObject with same CLSID but in different "
1156 "thread should return S_OK instead of 0x%08x\n", hr
);
1158 hr
= CoRevokeClassObject(cookie
);
1159 ok_ole_success(hr
, "CoRevokeClassObject");
1164 static DWORD CALLBACK
free_libraries_thread(LPVOID p
)
1166 CoFreeUnusedLibraries();
1170 static inline BOOL
is_module_loaded(const char *module
)
1172 return GetModuleHandle(module
) != 0;
1175 static void test_CoFreeUnusedLibraries(void)
1182 pCoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
1184 ok(!is_module_loaded("urlmon.dll"), "urlmon.dll shouldn't be loaded\n");
1186 hr
= CoCreateInstance(&CLSID_FileProtocol
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IInternetProtocol
, (void **)&pUnk
);
1187 if (hr
== REGDB_E_CLASSNOTREG
)
1189 skip("IE not installed so can't run CoFreeUnusedLibraries test\n");
1193 ok_ole_success(hr
, "CoCreateInstance");
1195 ok(is_module_loaded("urlmon.dll"), "urlmon.dll should be loaded\n");
1198 broken(pUnk
== NULL
), /* win9x */
1199 "Expected a valid pointer\n");
1201 IUnknown_Release(pUnk
);
1203 ok(is_module_loaded("urlmon.dll"), "urlmon.dll should be loaded\n");
1205 thread
= CreateThread(NULL
, 0, free_libraries_thread
, NULL
, 0, &tid
);
1206 ok( !WaitForSingleObject(thread
, 10000), "wait timed out\n" );
1207 CloseHandle(thread
);
1209 ok(is_module_loaded("urlmon.dll"), "urlmon.dll should be loaded\n");
1211 CoFreeUnusedLibraries();
1213 ok(!is_module_loaded("urlmon.dll"), "urlmon.dll shouldn't be loaded\n");
1218 static void test_CoGetObjectContext(void)
1222 IComThreadingInfo
*pComThreadingInfo
;
1223 IContextCallback
*pContextCallback
;
1224 IObjContext
*pObjContext
;
1229 DWORD tid
, exitcode
;
1231 if (!pCoGetObjectContext
)
1233 skip("CoGetObjectContext not present\n");
1237 hr
= pCoGetObjectContext(&IID_IComThreadingInfo
, (void **)&pComThreadingInfo
);
1238 ok(hr
== CO_E_NOTINITIALIZED
, "CoGetObjectContext should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n", hr
);
1239 ok(pComThreadingInfo
== NULL
, "pComThreadingInfo should have been set to NULL\n");
1241 /* show that COM doesn't have to be initialized for multi-threaded apartments if another
1242 thread has already done so */
1244 info
.wait
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1245 ok(info
.wait
!= NULL
, "CreateEvent failed with error %d\n", GetLastError());
1247 info
.stop
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1248 ok(info
.stop
!= NULL
, "CreateEvent failed with error %d\n", GetLastError());
1250 thread
= CreateThread(NULL
, 0, ole_initialize_thread
, &info
, 0, &tid
);
1251 ok(thread
!= NULL
, "CreateThread failed with error %d\n", GetLastError());
1253 ok( !WaitForSingleObject(info
.wait
, 10000), "wait timed out\n" );
1255 pComThreadingInfo
= NULL
;
1256 hr
= pCoGetObjectContext(&IID_IComThreadingInfo
, (void **)&pComThreadingInfo
);
1257 ok(hr
== S_OK
, "Expected S_OK, got 0x%08x\n", hr
);
1258 IComThreadingInfo_Release(pComThreadingInfo
);
1260 SetEvent(info
.stop
);
1261 ok( !WaitForSingleObject(thread
, 10000), "wait timed out\n" );
1263 GetExitCodeThread(thread
, &exitcode
);
1265 ok(hr
== S_OK
, "thread should have returned S_OK instead of 0x%08x\n", hr
);
1267 CloseHandle(thread
);
1268 CloseHandle(info
.wait
);
1269 CloseHandle(info
.stop
);
1271 pCoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
1273 hr
= pCoGetObjectContext(&IID_IComThreadingInfo
, (void **)&pComThreadingInfo
);
1274 ok_ole_success(hr
, "CoGetObjectContext");
1276 hr
= IComThreadingInfo_GetCurrentApartmentType(pComThreadingInfo
, &apttype
);
1277 ok_ole_success(hr
, "IComThreadingInfo_GetCurrentApartmentType");
1278 ok(apttype
== APTTYPE_MAINSTA
, "apartment type should be APTTYPE_MAINSTA instead of %d\n", apttype
);
1280 hr
= IComThreadingInfo_GetCurrentThreadType(pComThreadingInfo
, &thdtype
);
1281 ok_ole_success(hr
, "IComThreadingInfo_GetCurrentThreadType");
1282 ok(thdtype
== THDTYPE_PROCESSMESSAGES
, "thread type should be THDTYPE_PROCESSMESSAGES instead of %d\n", thdtype
);
1284 refs
= IComThreadingInfo_Release(pComThreadingInfo
);
1285 ok(refs
== 0, "pComThreadingInfo should have 0 refs instead of %d refs\n", refs
);
1287 hr
= pCoGetObjectContext(&IID_IContextCallback
, (void **)&pContextCallback
);
1288 ok_ole_success(hr
, "CoGetObjectContext(ContextCallback)");
1292 refs
= IContextCallback_Release(pContextCallback
);
1293 ok(refs
== 0, "pContextCallback should have 0 refs instead of %d refs\n", refs
);
1298 pCoInitializeEx(NULL
, COINIT_MULTITHREADED
);
1300 hr
= pCoGetObjectContext(&IID_IComThreadingInfo
, (void **)&pComThreadingInfo
);
1301 ok_ole_success(hr
, "CoGetObjectContext");
1303 hr
= IComThreadingInfo_GetCurrentApartmentType(pComThreadingInfo
, &apttype
);
1304 ok_ole_success(hr
, "IComThreadingInfo_GetCurrentApartmentType");
1305 ok(apttype
== APTTYPE_MTA
, "apartment type should be APTTYPE_MTA instead of %d\n", apttype
);
1307 hr
= IComThreadingInfo_GetCurrentThreadType(pComThreadingInfo
, &thdtype
);
1308 ok_ole_success(hr
, "IComThreadingInfo_GetCurrentThreadType");
1309 ok(thdtype
== THDTYPE_BLOCKMESSAGES
, "thread type should be THDTYPE_BLOCKMESSAGES instead of %d\n", thdtype
);
1311 refs
= IComThreadingInfo_Release(pComThreadingInfo
);
1312 ok(refs
== 0, "pComThreadingInfo should have 0 refs instead of %d refs\n", refs
);
1314 hr
= pCoGetObjectContext(&IID_IContextCallback
, (void **)&pContextCallback
);
1315 ok_ole_success(hr
, "CoGetObjectContext(ContextCallback)");
1319 refs
= IContextCallback_Release(pContextCallback
);
1320 ok(refs
== 0, "pContextCallback should have 0 refs instead of %d refs\n", refs
);
1323 hr
= pCoGetObjectContext(&IID_IObjContext
, (void **)&pObjContext
);
1324 ok_ole_success(hr
, "CoGetObjectContext");
1326 refs
= IObjContext_Release(pObjContext
);
1327 ok(refs
== 0, "pObjContext should have 0 refs instead of %d refs\n", refs
);
1333 IUnknown IUnknown_iface
;
1337 static inline Test_CallContext
*impl_from_IUnknown(IUnknown
*iface
)
1339 return CONTAINING_RECORD(iface
, Test_CallContext
, IUnknown_iface
);
1342 static HRESULT WINAPI
Test_CallContext_QueryInterface(
1347 if (ppvObj
== NULL
) return E_POINTER
;
1349 if (IsEqualGUID(riid
, &IID_IUnknown
))
1352 IUnknown_AddRef(iface
);
1357 return E_NOINTERFACE
;
1360 static ULONG WINAPI
Test_CallContext_AddRef(IUnknown
*iface
)
1362 Test_CallContext
*This
= impl_from_IUnknown(iface
);
1363 return InterlockedIncrement(&This
->refs
);
1366 static ULONG WINAPI
Test_CallContext_Release(IUnknown
*iface
)
1368 Test_CallContext
*This
= impl_from_IUnknown(iface
);
1369 ULONG refs
= InterlockedDecrement(&This
->refs
);
1371 HeapFree(GetProcessHeap(), 0, This
);
1375 static const IUnknownVtbl TestCallContext_Vtbl
=
1377 Test_CallContext_QueryInterface
,
1378 Test_CallContext_AddRef
,
1379 Test_CallContext_Release
1382 static void test_CoGetCallContext(void)
1387 Test_CallContext
*test_object
;
1389 if (!pCoSwitchCallContext
)
1391 skip("CoSwitchCallContext not present\n");
1397 test_object
= HeapAlloc(GetProcessHeap(), 0, sizeof(Test_CallContext
));
1398 test_object
->IUnknown_iface
.lpVtbl
= &TestCallContext_Vtbl
;
1399 test_object
->refs
= 1;
1401 hr
= CoGetCallContext(&IID_IUnknown
, (void**)&pUnk
);
1402 ok(hr
== RPC_E_CALL_COMPLETE
, "Expected RPC_E_CALL_COMPLETE, got 0x%08x\n", hr
);
1404 pUnk
= (IUnknown
*)0xdeadbeef;
1405 hr
= pCoSwitchCallContext(&test_object
->IUnknown_iface
, &pUnk
);
1406 ok_ole_success(hr
, "CoSwitchCallContext");
1407 ok(pUnk
== NULL
, "expected NULL, got %p\n", pUnk
);
1408 refs
= IUnknown_AddRef(&test_object
->IUnknown_iface
);
1409 ok(refs
== 2, "Expected refcount 2, got %d\n", refs
);
1410 IUnknown_Release(&test_object
->IUnknown_iface
);
1412 pUnk
= (IUnknown
*)0xdeadbeef;
1413 hr
= CoGetCallContext(&IID_IUnknown
, (void**)&pUnk
);
1414 ok_ole_success(hr
, "CoGetCallContext");
1415 ok(pUnk
== &test_object
->IUnknown_iface
, "expected %p, got %p\n",
1416 &test_object
->IUnknown_iface
, pUnk
);
1417 refs
= IUnknown_AddRef(&test_object
->IUnknown_iface
);
1418 ok(refs
== 3, "Expected refcount 3, got %d\n", refs
);
1419 IUnknown_Release(&test_object
->IUnknown_iface
);
1420 IUnknown_Release(pUnk
);
1422 pUnk
= (IUnknown
*)0xdeadbeef;
1423 hr
= pCoSwitchCallContext(NULL
, &pUnk
);
1424 ok_ole_success(hr
, "CoSwitchCallContext");
1425 ok(pUnk
== &test_object
->IUnknown_iface
, "expected %p, got %p\n",
1426 &test_object
->IUnknown_iface
, pUnk
);
1427 refs
= IUnknown_AddRef(&test_object
->IUnknown_iface
);
1428 ok(refs
== 2, "Expected refcount 2, got %d\n", refs
);
1429 IUnknown_Release(&test_object
->IUnknown_iface
);
1431 hr
= CoGetCallContext(&IID_IUnknown
, (void**)&pUnk
);
1432 ok(hr
== RPC_E_CALL_COMPLETE
, "Expected RPC_E_CALL_COMPLETE, got 0x%08x\n", hr
);
1434 IUnknown_Release(&test_object
->IUnknown_iface
);
1439 static void test_CoGetContextToken(void)
1447 DWORD tid
, exitcode
;
1449 if (!pCoGetContextToken
)
1451 win_skip("CoGetContextToken not present\n");
1456 hr
= pCoGetContextToken(&token
);
1457 ok(hr
== CO_E_NOTINITIALIZED
, "Expected CO_E_NOTINITIALIZED, got 0x%08x\n", hr
);
1458 ok(token
== 0xdeadbeef, "Expected 0, got 0x%lx\n", token
);
1460 /* show that COM doesn't have to be initialized for multi-threaded apartments if another
1461 thread has already done so */
1463 info
.wait
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1464 ok(info
.wait
!= NULL
, "CreateEvent failed with error %d\n", GetLastError());
1466 info
.stop
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1467 ok(info
.stop
!= NULL
, "CreateEvent failed with error %d\n", GetLastError());
1469 thread
= CreateThread(NULL
, 0, ole_initialize_thread
, &info
, 0, &tid
);
1470 ok(thread
!= NULL
, "CreateThread failed with error %d\n", GetLastError());
1472 ok( !WaitForSingleObject(info
.wait
, 10000), "wait timed out\n" );
1475 hr
= pCoGetContextToken(&token
);
1476 ok(hr
== S_OK
, "Expected S_OK, got 0x%08x\n", hr
);
1478 SetEvent(info
.stop
);
1479 ok( !WaitForSingleObject(thread
, 10000), "wait timed out\n" );
1481 GetExitCodeThread(thread
, &exitcode
);
1483 ok(hr
== S_OK
, "thread should have returned S_OK instead of 0x%08x\n", hr
);
1485 CloseHandle(thread
);
1486 CloseHandle(info
.wait
);
1487 CloseHandle(info
.stop
);
1491 hr
= pCoGetContextToken(NULL
);
1492 ok(hr
== E_POINTER
, "Expected E_POINTER, got 0x%08x\n", hr
);
1495 hr
= pCoGetContextToken(&token
);
1496 ok(hr
== S_OK
, "Expected S_OK, got 0x%08x\n", hr
);
1497 ok(token
, "Expected token != 0\n");
1499 refs
= IUnknown_AddRef((IUnknown
*)token
);
1500 todo_wine
ok(refs
== 1, "Expected 1, got %u\n", refs
);
1502 hr
= pCoGetObjectContext(&IID_IObjContext
, (void **)&ctx
);
1503 ok(hr
== S_OK
, "Expected S_OK, got 0x%08x\n", hr
);
1504 todo_wine
ok(ctx
== (IObjContext
*)token
, "Expected interface pointers to be the same\n");
1506 refs
= IUnknown_AddRef((IUnknown
*)ctx
);
1507 todo_wine
ok(refs
== 3, "Expected 3, got %u\n", refs
);
1509 refs
= IUnknown_Release((IUnknown
*)ctx
);
1510 todo_wine
ok(refs
== 2, "Expected 2, got %u\n", refs
);
1512 refs
= IUnknown_Release((IUnknown
*)token
);
1513 ok(refs
== 1, "Expected 1, got %u\n", refs
);
1515 /* CoGetContextToken does not add a reference */
1517 hr
= pCoGetContextToken(&token
);
1518 ok(hr
== S_OK
, "Expected S_OK, got 0x%08x\n", hr
);
1519 ok(token
, "Expected token != 0\n");
1520 todo_wine
ok(ctx
== (IObjContext
*)token
, "Expected interface pointers to be the same\n");
1522 refs
= IUnknown_AddRef((IUnknown
*)ctx
);
1523 ok(refs
== 2, "Expected 1, got %u\n", refs
);
1525 refs
= IUnknown_Release((IUnknown
*)ctx
);
1526 ok(refs
== 1, "Expected 0, got %u\n", refs
);
1528 refs
= IUnknown_Release((IUnknown
*)ctx
);
1529 ok(refs
== 0, "Expected 0, got %u\n", refs
);
1534 static void test_CoGetTreatAsClass(void)
1538 static GUID deadbeef
= {0xdeadbeef,0xdead,0xbeef,{0xde,0xad,0xbe,0xef,0xde,0xad,0xbe,0xef}};
1540 if (!pCoGetTreatAsClass
)
1542 win_skip("CoGetTreatAsClass not present\n");
1545 hr
= pCoGetTreatAsClass(&deadbeef
,&out
);
1546 ok (hr
== S_FALSE
, "expected S_FALSE got %x\n",hr
);
1547 ok (IsEqualGUID(&out
,&deadbeef
), "expected to get same clsid back\n");
1550 static void test_CoInitializeEx(void)
1554 hr
= pCoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
1555 ok(hr
== S_OK
, "CoInitializeEx failed with error 0x%08x\n", hr
);
1557 /* Calling OleInitialize for the first time should yield S_OK even with
1558 * apartment already initialized by previous CoInitialize(Ex) calls. */
1559 hr
= OleInitialize(NULL
);
1560 ok(hr
== S_OK
, "OleInitialize failed with error 0x%08x\n", hr
);
1562 /* Subsequent calls to OleInitialize should return S_FALSE */
1563 hr
= OleInitialize(NULL
);
1564 ok(hr
== S_FALSE
, "Expected S_FALSE, hr = 0x%08x\n", hr
);
1573 HMODULE hOle32
= GetModuleHandle("ole32");
1574 HMODULE hAdvapi32
= GetModuleHandle("advapi32");
1575 pCoGetObjectContext
= (void*)GetProcAddress(hOle32
, "CoGetObjectContext");
1576 pCoSwitchCallContext
= (void*)GetProcAddress(hOle32
, "CoSwitchCallContext");
1577 pCoGetTreatAsClass
= (void*)GetProcAddress(hOle32
,"CoGetTreatAsClass");
1578 pCoGetContextToken
= (void*)GetProcAddress(hOle32
, "CoGetContextToken");
1579 pRegOverridePredefKey
= (void*)GetProcAddress(hAdvapi32
, "RegOverridePredefKey");
1580 if (!(pCoInitializeEx
= (void*)GetProcAddress(hOle32
, "CoInitializeEx")))
1582 trace("You need DCOM95 installed to run this test\n");
1586 test_ProgIDFromCLSID();
1587 test_CLSIDFromProgID();
1588 test_CLSIDFromString();
1589 test_StringFromGUID2();
1590 test_CoCreateInstance();
1592 test_CoGetClassObject();
1593 test_CoRegisterMessageFilter();
1594 test_CoRegisterPSClsid();
1595 test_CoGetPSClsid();
1596 test_CoUnmarshalInterface();
1597 test_CoGetInterfaceAndReleaseStream();
1598 test_CoMarshalInterface();
1599 test_CoMarshalInterThreadInterfaceInStream();
1600 test_CoRegisterClassObject();
1601 test_registered_object_thread_affinity();
1602 test_CoFreeUnusedLibraries();
1603 test_CoGetObjectContext();
1604 test_CoGetCallContext();
1605 test_CoGetContextToken();
1606 test_CoGetTreatAsClass();
1607 test_CoInitializeEx();