user32: Delay registration of the builtin classes until the first window is created.
[wine.git] / dlls / ole32 / tests / compobj.c
blobab7ba8c0ca68cc3679574d0f4a146ed078579597
1 /*
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 COBJMACROS
22 #define CONST_VTABLE
24 #include <stdarg.h>
25 #include <stdio.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #define USE_COM_CONTEXT_DEF
30 #include "initguid.h"
31 #include "objbase.h"
32 #include "shlguid.h"
33 #include "urlmon.h" /* for CLSID_FileProtocol */
35 #include "ctxtcall.h"
37 #include "wine/test.h"
39 /* functions that are not present on all versions of Windows */
40 static HRESULT (WINAPI * pCoInitializeEx)(LPVOID lpReserved, DWORD dwCoInit);
41 static HRESULT (WINAPI * pCoGetObjectContext)(REFIID riid, LPVOID *ppv);
42 static HRESULT (WINAPI * pCoSwitchCallContext)(IUnknown *pObject, IUnknown **ppOldObject);
43 static HRESULT (WINAPI * pCoGetTreatAsClass)(REFCLSID clsidOld, LPCLSID pClsidNew);
44 static HRESULT (WINAPI * pCoGetContextToken)(ULONG_PTR *token);
45 static LONG (WINAPI * pRegOverridePredefKey)(HKEY key, HKEY override);
47 static BOOL (WINAPI *pActivateActCtx)(HANDLE,ULONG_PTR*);
48 static HANDLE (WINAPI *pCreateActCtxW)(PCACTCTXW);
49 static BOOL (WINAPI *pDeactivateActCtx)(DWORD,ULONG_PTR);
50 static void (WINAPI *pReleaseActCtx)(HANDLE);
52 #define ok_ole_success(hr, func) ok(hr == S_OK, func " failed with error 0x%08x\n", hr)
53 #define ok_more_than_one_lock() ok(cLocks > 0, "Number of locks should be > 0, but actually is %d\n", cLocks)
54 #define ok_no_locks() ok(cLocks == 0, "Number of locks should be 0, but actually is %d\n", cLocks)
56 static const CLSID CLSID_non_existent = { 0x12345678, 0x1234, 0x1234, { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 } };
57 static const CLSID CLSID_StdFont = { 0x0be35203, 0x8f91, 0x11ce, { 0x9d, 0xe3, 0x00, 0xaa, 0x00, 0x4b, 0xb8, 0x51 } };
58 static const GUID IID_Testiface = { 0x22222222, 0x1234, 0x1234, { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 } };
59 static const GUID IID_Testiface2 = { 0x32222222, 0x1234, 0x1234, { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 } };
60 static const GUID IID_Testiface3 = { 0x42222222, 0x1234, 0x1234, { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 } };
61 static const GUID IID_Testiface4 = { 0x52222222, 0x1234, 0x1234, { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 } };
62 static const GUID IID_Testiface5 = { 0x62222222, 0x1234, 0x1234, { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 } };
63 static const GUID IID_Testiface6 = { 0x72222222, 0x1234, 0x1234, { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 } };
64 static const GUID IID_TestPS = { 0x66666666, 0x8888, 0x7777, { 0x66, 0x66, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55 } };
66 static WCHAR stdfont[] = {'S','t','d','F','o','n','t',0};
67 static const WCHAR wszNonExistent[] = {'N','o','n','E','x','i','s','t','e','n','t',0};
68 static WCHAR wszCLSID_StdFont[] =
70 '{','0','b','e','3','5','2','0','3','-','8','f','9','1','-','1','1','c','e','-',
71 '9','d','e','3','-','0','0','a','a','0','0','4','b','b','8','5','1','}',0
73 static const WCHAR progidW[] = {'P','r','o','g','I','d','.','P','r','o','g','I','d',0};
75 DEFINE_GUID(IID_IWineTest, 0x5201163f, 0x8164, 0x4fd0, 0xa1, 0xa2, 0x5d, 0x5a, 0x36, 0x54, 0xd3, 0xbd);
76 DEFINE_GUID(CLSID_WineOOPTest, 0x5201163f, 0x8164, 0x4fd0, 0xa1, 0xa2, 0x5d, 0x5a, 0x36, 0x54, 0xd3, 0xbd);
78 static const char *debugstr_guid(REFIID riid)
80 static char buf[50];
82 sprintf(buf, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
83 riid->Data1, riid->Data2, riid->Data3, riid->Data4[0],
84 riid->Data4[1], riid->Data4[2], riid->Data4[3], riid->Data4[4],
85 riid->Data4[5], riid->Data4[6], riid->Data4[7]);
87 return buf;
90 static LONG cLocks;
92 static void LockModule(void)
94 InterlockedIncrement(&cLocks);
97 static void UnlockModule(void)
99 InterlockedDecrement(&cLocks);
102 static HRESULT WINAPI Test_IClassFactory_QueryInterface(
103 LPCLASSFACTORY iface,
104 REFIID riid,
105 LPVOID *ppvObj)
107 if (ppvObj == NULL) return E_POINTER;
109 if (IsEqualGUID(riid, &IID_IUnknown) ||
110 IsEqualGUID(riid, &IID_IClassFactory))
112 *ppvObj = iface;
113 IClassFactory_AddRef(iface);
114 return S_OK;
117 *ppvObj = NULL;
118 return E_NOINTERFACE;
121 static ULONG WINAPI Test_IClassFactory_AddRef(LPCLASSFACTORY iface)
123 LockModule();
124 return 2; /* non-heap-based object */
127 static ULONG WINAPI Test_IClassFactory_Release(LPCLASSFACTORY iface)
129 UnlockModule();
130 return 1; /* non-heap-based object */
133 static HRESULT WINAPI Test_IClassFactory_CreateInstance(
134 LPCLASSFACTORY iface,
135 IUnknown *pUnkOuter,
136 REFIID riid,
137 LPVOID *ppvObj)
139 *ppvObj = NULL;
140 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
141 return E_NOINTERFACE;
144 static HRESULT WINAPI Test_IClassFactory_LockServer(
145 LPCLASSFACTORY iface,
146 BOOL fLock)
148 return S_OK;
151 static const IClassFactoryVtbl TestClassFactory_Vtbl =
153 Test_IClassFactory_QueryInterface,
154 Test_IClassFactory_AddRef,
155 Test_IClassFactory_Release,
156 Test_IClassFactory_CreateInstance,
157 Test_IClassFactory_LockServer
160 static IClassFactory Test_ClassFactory = { &TestClassFactory_Vtbl };
162 static WCHAR manifest_path[MAX_PATH];
164 static BOOL create_manifest_file(const char *filename, const char *manifest)
166 int manifest_len;
167 DWORD size;
168 HANDLE file;
169 WCHAR path[MAX_PATH];
171 MultiByteToWideChar( CP_ACP, 0, filename, -1, path, MAX_PATH );
172 GetFullPathNameW(path, sizeof(manifest_path)/sizeof(WCHAR), manifest_path, NULL);
174 manifest_len = strlen(manifest);
175 file = CreateFileW(path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
176 FILE_ATTRIBUTE_NORMAL, NULL);
177 ok(file != INVALID_HANDLE_VALUE, "CreateFile failed: %u\n", GetLastError());
178 if(file == INVALID_HANDLE_VALUE)
179 return FALSE;
180 WriteFile(file, manifest, manifest_len, &size, NULL);
181 CloseHandle(file);
183 return TRUE;
186 static HANDLE activate_context(const char *manifest, ULONG_PTR *cookie)
188 WCHAR path[MAX_PATH];
189 ACTCTXW actctx;
190 HANDLE handle;
191 BOOL ret;
193 if (!pCreateActCtxW) return NULL;
195 create_manifest_file("file.manifest", manifest);
197 MultiByteToWideChar( CP_ACP, 0, "file.manifest", -1, path, MAX_PATH );
198 memset(&actctx, 0, sizeof(ACTCTXW));
199 actctx.cbSize = sizeof(ACTCTXW);
200 actctx.lpSource = path;
202 handle = pCreateActCtxW(&actctx);
203 ok(handle != INVALID_HANDLE_VALUE, "handle == INVALID_HANDLE_VALUE, error %u\n", GetLastError());
205 ok(actctx.cbSize == sizeof(ACTCTXW), "actctx.cbSize=%d\n", actctx.cbSize);
206 ok(actctx.dwFlags == 0, "actctx.dwFlags=%d\n", actctx.dwFlags);
207 ok(actctx.lpSource == path, "actctx.lpSource=%p\n", actctx.lpSource);
208 ok(actctx.wProcessorArchitecture == 0, "actctx.wProcessorArchitecture=%d\n", actctx.wProcessorArchitecture);
209 ok(actctx.wLangId == 0, "actctx.wLangId=%d\n", actctx.wLangId);
210 ok(actctx.lpAssemblyDirectory == NULL, "actctx.lpAssemblyDirectory=%p\n", actctx.lpAssemblyDirectory);
211 ok(actctx.lpResourceName == NULL, "actctx.lpResourceName=%p\n", actctx.lpResourceName);
212 ok(actctx.lpApplicationName == NULL, "actctx.lpApplicationName=%p\n", actctx.lpApplicationName);
213 ok(actctx.hModule == NULL, "actctx.hModule=%p\n", actctx.hModule);
215 DeleteFileA("file.manifest");
217 ret = pActivateActCtx(handle, cookie);
218 ok(ret, "ActivateActCtx failed: %u\n", GetLastError());
220 return handle;
223 static const char actctx_manifest[] =
224 "<assembly xmlns=\"urn:schemas-microsoft-com:asm.v1\" manifestVersion=\"1.0\">"
225 "<assemblyIdentity version=\"1.2.3.4\" name=\"Wine.Test\" type=\"win32\""
226 " publicKeyToken=\"6595b6414666f1df\" />"
227 "<file name=\"testlib.dll\">"
228 " <comClass description=\"Test com class\""
229 " clsid=\"{12345678-1234-1234-1234-56789abcdef0}\""
230 " progid=\"ProgId.ProgId\""
231 " miscStatusIcon=\"recomposeonresize\""
232 " />"
233 " <comClass clsid=\"{0be35203-8f91-11ce-9de3-00aa004bb851}\""
234 " progid=\"CustomFont\""
235 " miscStatusIcon=\"recomposeonresize\""
236 " miscStatusContent=\"insideout\""
237 " />"
238 " <comClass clsid=\"{0be35203-8f91-11ce-9de3-00aa004bb852}\""
239 " progid=\"StdFont\""
240 " />"
241 " <comClass clsid=\"{62222222-1234-1234-1234-56789abcdef0}\" >"
242 " <progid>ProgId.ProgId.1</progid>"
243 " </comClass>"
244 " <comInterfaceProxyStub "
245 " name=\"Iifaceps\""
246 " iid=\"{22222222-1234-1234-1234-56789abcdef0}\""
247 " proxyStubClsid32=\"{66666666-8888-7777-6666-555555555555}\""
248 " />"
249 "</file>"
250 " <comInterfaceExternalProxyStub "
251 " name=\"Iifaceps2\""
252 " iid=\"{32222222-1234-1234-1234-56789abcdef0}\""
253 " />"
254 " <comInterfaceExternalProxyStub "
255 " name=\"Iifaceps3\""
256 " iid=\"{42222222-1234-1234-1234-56789abcdef0}\""
257 " proxyStubClsid32=\"{66666666-8888-7777-6666-555555555555}\""
258 " />"
259 " <comInterfaceExternalProxyStub "
260 " name=\"Iifaceps4\""
261 " iid=\"{52222222-1234-1234-1234-56789abcdef0}\""
262 " proxyStubClsid32=\"{00000000-0000-0000-0000-000000000000}\""
263 " />"
264 " <clrClass "
265 " clsid=\"{72222222-1234-1234-1234-56789abcdef0}\""
266 " name=\"clrclass\""
267 " >"
268 " <progid>clrprogid.1</progid>"
269 " </clrClass>"
270 "</assembly>";
272 DEFINE_GUID(CLSID_Testclass, 0x12345678, 0x1234, 0x1234, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0);
274 static void test_ProgIDFromCLSID(void)
276 ULONG_PTR cookie = 0;
277 LPWSTR progid;
278 HANDLE handle;
279 HRESULT hr;
281 hr = ProgIDFromCLSID(&CLSID_StdFont, &progid);
282 ok(hr == S_OK, "ProgIDFromCLSID failed with error 0x%08x\n", hr);
283 if (hr == S_OK)
285 ok(!lstrcmpiW(progid, stdfont), "Didn't get expected prog ID\n");
286 CoTaskMemFree(progid);
289 progid = (LPWSTR)0xdeadbeef;
290 hr = ProgIDFromCLSID(&CLSID_non_existent, &progid);
291 ok(hr == REGDB_E_CLASSNOTREG, "ProgIDFromCLSID returned %08x\n", hr);
292 ok(progid == NULL, "ProgIDFromCLSID returns with progid %p\n", progid);
294 hr = ProgIDFromCLSID(&CLSID_StdFont, NULL);
295 ok(hr == E_INVALIDARG, "ProgIDFromCLSID should return E_INVALIDARG instead of 0x%08x\n", hr);
297 if ((handle = activate_context(actctx_manifest, &cookie)))
299 static const WCHAR customfontW[] = {'C','u','s','t','o','m','F','o','n','t',0};
301 hr = ProgIDFromCLSID(&CLSID_non_existent, &progid);
302 todo_wine
303 ok(hr == S_OK, "got 0x%08x\n", hr);
304 if (hr == S_OK)
306 ok(!lstrcmpiW(progid, progidW), "got %s\n", wine_dbgstr_w(progid));
307 CoTaskMemFree(progid);
310 /* try something registered and redirected */
311 progid = NULL;
312 hr = ProgIDFromCLSID(&CLSID_StdFont, &progid);
313 ok(hr == S_OK, "got 0x%08x\n", hr);
314 todo_wine
315 ok(!lstrcmpiW(progid, customfontW), "got wrong progid %s\n", wine_dbgstr_w(progid));
316 CoTaskMemFree(progid);
318 /* classes without default progid, progid list is not used */
319 hr = ProgIDFromCLSID(&IID_Testiface5, &progid);
320 ok(hr == REGDB_E_CLASSNOTREG, "got 0x%08x\n", hr);
322 hr = ProgIDFromCLSID(&IID_Testiface6, &progid);
323 ok(hr == REGDB_E_CLASSNOTREG, "got 0x%08x\n", hr);
325 pDeactivateActCtx(0, cookie);
326 pReleaseActCtx(handle);
330 static void test_CLSIDFromProgID(void)
332 ULONG_PTR cookie = 0;
333 HANDLE handle;
334 CLSID clsid;
335 HRESULT hr = CLSIDFromProgID(stdfont, &clsid);
336 ok(hr == S_OK, "CLSIDFromProgID failed with error 0x%08x\n", hr);
337 ok(IsEqualCLSID(&clsid, &CLSID_StdFont), "clsid wasn't equal to CLSID_StdFont\n");
339 hr = CLSIDFromString(stdfont, &clsid);
340 ok_ole_success(hr, "CLSIDFromString");
341 ok(IsEqualCLSID(&clsid, &CLSID_StdFont), "clsid wasn't equal to CLSID_StdFont\n");
343 /* test some failure cases */
345 hr = CLSIDFromProgID(wszNonExistent, NULL);
346 ok(hr == E_INVALIDARG, "CLSIDFromProgID should have returned E_INVALIDARG instead of 0x%08x\n", hr);
348 hr = CLSIDFromProgID(NULL, &clsid);
349 ok(hr == E_INVALIDARG, "CLSIDFromProgID should have returned E_INVALIDARG instead of 0x%08x\n", hr);
351 memset(&clsid, 0xcc, sizeof(clsid));
352 hr = CLSIDFromProgID(wszNonExistent, &clsid);
353 ok(hr == CO_E_CLASSSTRING, "CLSIDFromProgID on nonexistent ProgID should have returned CO_E_CLASSSTRING instead of 0x%08x\n", hr);
354 ok(IsEqualCLSID(&clsid, &CLSID_NULL), "CLSIDFromProgID should have set clsid to all-zeros on failure\n");
356 /* fails without proper context */
357 memset(&clsid, 0xcc, sizeof(clsid));
358 hr = CLSIDFromProgID(progidW, &clsid);
359 ok(hr == CO_E_CLASSSTRING, "got 0x%08x\n", hr);
360 ok(IsEqualCLSID(&clsid, &CLSID_NULL), "wrong clsid\n");
362 if ((handle = activate_context(actctx_manifest, &cookie)))
364 GUID clsid1;
366 clsid = CLSID_NULL;
367 hr = CLSIDFromProgID(progidW, &clsid);
368 todo_wine
369 ok(hr == S_OK, "got 0x%08x\n", hr);
370 if (hr == S_OK)
371 /* it returns generated CLSID here */
372 ok(!IsEqualCLSID(&clsid, &CLSID_non_existent) && !IsEqualCLSID(&clsid, &CLSID_NULL),
373 "got wrong clsid %s\n", debugstr_guid(&clsid));
375 /* duplicate progid present in context - returns generated guid here too */
376 clsid = CLSID_NULL;
377 hr = CLSIDFromProgID(stdfont, &clsid);
378 ok(hr == S_OK, "got 0x%08x\n", hr);
379 clsid1 = CLSID_StdFont;
380 /* that's where it differs from StdFont */
381 clsid1.Data4[7] = 0x52;
382 todo_wine
383 ok(!IsEqualCLSID(&clsid, &CLSID_StdFont) && !IsEqualCLSID(&clsid, &CLSID_NULL) && !IsEqualCLSID(&clsid, &clsid1),
384 "got %s\n", debugstr_guid(&clsid));
386 pDeactivateActCtx(0, cookie);
387 pReleaseActCtx(handle);
391 static void test_CLSIDFromString(void)
393 CLSID clsid;
394 WCHAR wszCLSID_Broken[50];
395 UINT i;
397 HRESULT hr = CLSIDFromString(wszCLSID_StdFont, &clsid);
398 ok_ole_success(hr, "CLSIDFromString");
399 ok(IsEqualCLSID(&clsid, &CLSID_StdFont), "clsid wasn't equal to CLSID_StdFont\n");
401 hr = CLSIDFromString(NULL, &clsid);
402 ok_ole_success(hr, "CLSIDFromString");
403 ok(IsEqualCLSID(&clsid, &CLSID_NULL), "clsid wasn't equal to CLSID_NULL\n");
405 lstrcpyW(wszCLSID_Broken, wszCLSID_StdFont);
406 for(i = lstrlenW(wszCLSID_StdFont); i < 49; i++)
407 wszCLSID_Broken[i] = 'A';
408 wszCLSID_Broken[i] = '\0';
410 memset(&clsid, 0, sizeof(CLSID));
411 hr = CLSIDFromString(wszCLSID_Broken, &clsid);
412 ok(hr == CO_E_CLASSSTRING, "Got %08x\n", hr);
413 ok(IsEqualCLSID(&clsid, &CLSID_StdFont), "clsid wasn't equal to CLSID_StdFont\n");
415 wszCLSID_Broken[lstrlenW(wszCLSID_StdFont)-1] = 'A';
416 memset(&clsid, 0, sizeof(CLSID));
417 hr = CLSIDFromString(wszCLSID_Broken, &clsid);
418 ok(hr == CO_E_CLASSSTRING, "Got %08x\n", hr);
419 ok(IsEqualCLSID(&clsid, &CLSID_StdFont), "clsid wasn't equal to CLSID_StdFont\n");
421 wszCLSID_Broken[lstrlenW(wszCLSID_StdFont)] = '\0';
422 memset(&clsid, 0, sizeof(CLSID));
423 hr = CLSIDFromString(wszCLSID_Broken, &clsid);
424 ok(hr == CO_E_CLASSSTRING, "Got %08x\n", hr);
425 ok(IsEqualCLSID(&clsid, &CLSID_StdFont), "clsid wasn't equal to CLSID_StdFont\n");
427 wszCLSID_Broken[lstrlenW(wszCLSID_StdFont)-1] = '\0';
428 memset(&clsid, 0, sizeof(CLSID));
429 hr = CLSIDFromString(wszCLSID_Broken, &clsid);
430 ok(hr == CO_E_CLASSSTRING, "Got %08x\n", hr);
431 ok(IsEqualCLSID(&clsid, &CLSID_StdFont), "clsid wasn't equal to CLSID_StdFont\n");
433 memset(&clsid, 0xcc, sizeof(CLSID));
434 hr = CLSIDFromString(wszCLSID_Broken+1, &clsid);
435 ok(hr == CO_E_CLASSSTRING, "Got %08x\n", hr);
436 ok(IsEqualCLSID(&clsid, &CLSID_NULL), "clsid wasn't equal to CLSID_NULL\n");
438 wszCLSID_Broken[9] = '*';
439 memset(&clsid, 0xcc, sizeof(CLSID));
440 hr = CLSIDFromString(wszCLSID_Broken, &clsid);
441 ok(hr == CO_E_CLASSSTRING, "Got %08x\n", hr);
442 ok(clsid.Data1 == CLSID_StdFont.Data1, "Got %08x\n", clsid.Data1);
443 ok(clsid.Data2 == 0xcccc, "Got %04x\n", clsid.Data2);
445 wszCLSID_Broken[3] = '*';
446 memset(&clsid, 0xcc, sizeof(CLSID));
447 hr = CLSIDFromString(wszCLSID_Broken, &clsid);
448 ok(hr == CO_E_CLASSSTRING, "Got %08x\n", hr);
449 ok(clsid.Data1 == 0xb, "Got %08x\n", clsid.Data1);
450 ok(clsid.Data2 == 0xcccc, "Got %04x\n", clsid.Data2);
452 wszCLSID_Broken[3] = '\0';
453 memset(&clsid, 0xcc, sizeof(CLSID));
454 hr = CLSIDFromString(wszCLSID_Broken, &clsid);
455 ok(hr == CO_E_CLASSSTRING, "Got %08x\n", hr);
456 ok(clsid.Data1 == 0xb, "Got %08x\n", clsid.Data1);
457 ok(clsid.Data2 == 0xcccc, "Got %04x\n", clsid.Data2);
460 static void test_StringFromGUID2(void)
462 WCHAR str[50];
463 int len;
465 /* invalid pointer */
466 SetLastError(0xdeadbeef);
467 len = StringFromGUID2(NULL,str,50);
468 ok(len == 0, "len: %d (expected 0)\n", len);
469 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %x\n", GetLastError());
471 /* Test corner cases for buffer size */
472 len = StringFromGUID2(&CLSID_StdFont,str,50);
473 ok(len == 39, "len: %d (expected 39)\n", len);
474 ok(!lstrcmpiW(str, wszCLSID_StdFont),"string wasn't equal for CLSID_StdFont\n");
476 memset(str,0,sizeof str);
477 len = StringFromGUID2(&CLSID_StdFont,str,39);
478 ok(len == 39, "len: %d (expected 39)\n", len);
479 ok(!lstrcmpiW(str, wszCLSID_StdFont),"string wasn't equal for CLSID_StdFont\n");
481 len = StringFromGUID2(&CLSID_StdFont,str,38);
482 ok(len == 0, "len: %d (expected 0)\n", len);
484 len = StringFromGUID2(&CLSID_StdFont,str,30);
485 ok(len == 0, "len: %d (expected 0)\n", len);
488 struct info
490 HANDLE wait, stop;
493 static DWORD CALLBACK ole_initialize_thread(LPVOID pv)
495 HRESULT hr;
496 struct info *info = pv;
498 hr = pCoInitializeEx(NULL, COINIT_MULTITHREADED);
500 SetEvent(info->wait);
501 WaitForSingleObject(info->stop, 10000);
503 CoUninitialize();
504 return hr;
507 static void test_CoCreateInstance(void)
509 HRESULT hr;
510 HANDLE thread;
511 DWORD tid, exitcode;
512 IUnknown *pUnk;
513 struct info info;
514 REFCLSID rclsid = &CLSID_InternetZoneManager;
516 pUnk = (IUnknown *)0xdeadbeef;
517 hr = CoCreateInstance(rclsid, NULL, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void **)&pUnk);
518 ok(hr == CO_E_NOTINITIALIZED, "CoCreateInstance should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n", hr);
519 ok(pUnk == NULL, "CoCreateInstance should have changed the passed in pointer to NULL, instead of %p\n", pUnk);
521 OleInitialize(NULL);
523 /* test errors returned for non-registered clsids */
524 hr = CoCreateInstance(&CLSID_non_existent, NULL, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void **)&pUnk);
525 ok(hr == REGDB_E_CLASSNOTREG, "CoCreateInstance for non-registered inproc server should have returned REGDB_E_CLASSNOTREG instead of 0x%08x\n", hr);
526 hr = CoCreateInstance(&CLSID_non_existent, NULL, CLSCTX_INPROC_HANDLER, &IID_IUnknown, (void **)&pUnk);
527 ok(hr == REGDB_E_CLASSNOTREG, "CoCreateInstance for non-registered inproc handler should have returned REGDB_E_CLASSNOTREG instead of 0x%08x\n", hr);
528 hr = CoCreateInstance(&CLSID_non_existent, NULL, CLSCTX_LOCAL_SERVER, &IID_IUnknown, (void **)&pUnk);
529 ok(hr == REGDB_E_CLASSNOTREG, "CoCreateInstance for non-registered local server should have returned REGDB_E_CLASSNOTREG instead of 0x%08x\n", hr);
530 hr = CoCreateInstance(&CLSID_non_existent, NULL, CLSCTX_REMOTE_SERVER, &IID_IUnknown, (void **)&pUnk);
531 ok(hr == REGDB_E_CLASSNOTREG, "CoCreateInstance for non-registered remote server should have returned REGDB_E_CLASSNOTREG instead of 0x%08x\n", hr);
533 hr = CoCreateInstance(rclsid, NULL, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void **)&pUnk);
534 if(hr == REGDB_E_CLASSNOTREG)
536 skip("IE not installed so can't test CoCreateInstance\n");
537 OleUninitialize();
538 return;
541 ok_ole_success(hr, "CoCreateInstance");
542 if(pUnk) IUnknown_Release(pUnk);
543 OleUninitialize();
545 hr = CoCreateInstance(rclsid, NULL, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void **)&pUnk);
546 ok(hr == CO_E_NOTINITIALIZED, "CoCreateInstance should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n", hr);
548 /* show that COM doesn't have to be initialized for multi-threaded apartments if another
549 thread has already done so */
551 info.wait = CreateEventA(NULL, TRUE, FALSE, NULL);
552 ok(info.wait != NULL, "CreateEvent failed with error %d\n", GetLastError());
554 info.stop = CreateEventA(NULL, TRUE, FALSE, NULL);
555 ok(info.stop != NULL, "CreateEvent failed with error %d\n", GetLastError());
557 thread = CreateThread(NULL, 0, ole_initialize_thread, &info, 0, &tid);
558 ok(thread != NULL, "CreateThread failed with error %d\n", GetLastError());
560 ok( !WaitForSingleObject(info.wait, 10000 ), "wait timed out\n" );
562 pUnk = (IUnknown *)0xdeadbeef;
563 hr = CoCreateInstance(rclsid, NULL, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void **)&pUnk);
564 ok(hr == S_OK, "CoCreateInstance should have returned S_OK instead of 0x%08x\n", hr);
565 if (pUnk) IUnknown_Release(pUnk);
567 SetEvent(info.stop);
568 ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
570 GetExitCodeThread(thread, &exitcode);
571 hr = exitcode;
572 ok(hr == S_OK, "thread should have returned S_OK instead of 0x%08x\n", hr);
574 CloseHandle(thread);
575 CloseHandle(info.wait);
576 CloseHandle(info.stop);
579 static void test_CoGetClassObject(void)
581 HRESULT hr;
582 HANDLE thread;
583 DWORD tid, exitcode;
584 IUnknown *pUnk;
585 struct info info;
586 REFCLSID rclsid = &CLSID_InternetZoneManager;
587 HKEY hkey;
588 LONG res;
590 hr = CoGetClassObject(rclsid, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void **)&pUnk);
591 ok(hr == CO_E_NOTINITIALIZED, "CoGetClassObject should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n", hr);
592 ok(pUnk == NULL, "CoGetClassObject should have changed the passed in pointer to NULL, instead of %p\n", pUnk);
594 hr = CoGetClassObject(rclsid, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, NULL);
595 ok(hr == E_INVALIDARG ||
596 broken(hr == CO_E_NOTINITIALIZED), /* win9x */
597 "CoGetClassObject should have returned E_INVALIDARG instead of 0x%08x\n", hr);
599 /* show that COM doesn't have to be initialized for multi-threaded apartments if another
600 thread has already done so */
602 info.wait = CreateEventA(NULL, TRUE, FALSE, NULL);
603 ok(info.wait != NULL, "CreateEvent failed with error %d\n", GetLastError());
605 info.stop = CreateEventA(NULL, TRUE, FALSE, NULL);
606 ok(info.stop != NULL, "CreateEvent failed with error %d\n", GetLastError());
608 thread = CreateThread(NULL, 0, ole_initialize_thread, &info, 0, &tid);
609 ok(thread != NULL, "CreateThread failed with error %d\n", GetLastError());
611 ok( !WaitForSingleObject(info.wait, 10000), "wait timed out\n" );
613 pUnk = (IUnknown *)0xdeadbeef;
614 hr = CoGetClassObject(rclsid, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void **)&pUnk);
615 if(hr == REGDB_E_CLASSNOTREG)
616 skip("IE not installed so can't test CoGetClassObject\n");
617 else
619 ok(hr == S_OK, "CoGetClassObject should have returned S_OK instead of 0x%08x\n", hr);
620 if (pUnk) IUnknown_Release(pUnk);
623 SetEvent(info.stop);
624 ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
626 GetExitCodeThread(thread, &exitcode);
627 hr = exitcode;
628 ok(hr == S_OK, "thread should have returned S_OK instead of 0x%08x\n", hr);
630 CloseHandle(thread);
631 CloseHandle(info.wait);
632 CloseHandle(info.stop);
634 if (!pRegOverridePredefKey)
636 win_skip("RegOverridePredefKey not available\n");
637 return;
640 pCoInitializeEx(NULL, COINIT_MULTITHREADED);
642 hr = CoGetClassObject(rclsid, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void **)&pUnk);
643 if (hr == S_OK)
645 IUnknown_Release(pUnk);
647 res = RegCreateKeyExA(HKEY_CURRENT_USER, "Software\\Classes", 0, NULL, 0,
648 KEY_ALL_ACCESS, NULL, &hkey, NULL);
649 ok(!res, "RegCreateKeyEx returned %d\n", res);
651 res = pRegOverridePredefKey(HKEY_CLASSES_ROOT, hkey);
652 ok(!res, "RegOverridePredefKey returned %d\n", res);
654 hr = CoGetClassObject(rclsid, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void **)&pUnk);
655 ok(hr == S_OK, "CoGetClassObject should have returned S_OK instead of 0x%08x\n", hr);
657 res = pRegOverridePredefKey(HKEY_CLASSES_ROOT, NULL);
658 ok(!res, "RegOverridePredefKey returned %d\n", res);
660 if (hr == S_OK) IUnknown_Release(pUnk);
661 RegCloseKey(hkey);
663 CoUninitialize();
666 static ATOM register_dummy_class(void)
668 WNDCLASSA wc =
671 DefWindowProcA,
674 GetModuleHandleA(NULL),
675 NULL,
676 LoadCursorA(NULL, (LPSTR)IDC_ARROW),
677 (HBRUSH)(COLOR_BTNFACE+1),
678 NULL,
679 "WineOleTestClass",
682 return RegisterClassA(&wc);
685 static void test_ole_menu(void)
687 HWND hwndFrame;
688 HRESULT hr;
690 hwndFrame = CreateWindowA((LPCSTR)MAKEINTATOM(register_dummy_class()), "Test", 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, NULL, NULL);
691 hr = OleSetMenuDescriptor(NULL, hwndFrame, NULL, NULL, NULL);
692 todo_wine ok_ole_success(hr, "OleSetMenuDescriptor");
694 DestroyWindow(hwndFrame);
698 static HRESULT WINAPI MessageFilter_QueryInterface(IMessageFilter *iface, REFIID riid, void ** ppvObj)
700 if (ppvObj == NULL) return E_POINTER;
702 if (IsEqualGUID(riid, &IID_IUnknown) ||
703 IsEqualGUID(riid, &IID_IClassFactory))
705 *ppvObj = iface;
706 IMessageFilter_AddRef(iface);
707 return S_OK;
710 return E_NOINTERFACE;
713 static ULONG WINAPI MessageFilter_AddRef(IMessageFilter *iface)
715 return 2; /* non-heap object */
718 static ULONG WINAPI MessageFilter_Release(IMessageFilter *iface)
720 return 1; /* non-heap object */
723 static DWORD WINAPI MessageFilter_HandleInComingCall(
724 IMessageFilter *iface,
725 DWORD dwCallType,
726 HTASK threadIDCaller,
727 DWORD dwTickCount,
728 LPINTERFACEINFO lpInterfaceInfo)
730 trace("HandleInComingCall\n");
731 return SERVERCALL_ISHANDLED;
734 static DWORD WINAPI MessageFilter_RetryRejectedCall(
735 IMessageFilter *iface,
736 HTASK threadIDCallee,
737 DWORD dwTickCount,
738 DWORD dwRejectType)
740 trace("RetryRejectedCall\n");
741 return 0;
744 static DWORD WINAPI MessageFilter_MessagePending(
745 IMessageFilter *iface,
746 HTASK threadIDCallee,
747 DWORD dwTickCount,
748 DWORD dwPendingType)
750 trace("MessagePending\n");
751 return PENDINGMSG_WAITNOPROCESS;
754 static const IMessageFilterVtbl MessageFilter_Vtbl =
756 MessageFilter_QueryInterface,
757 MessageFilter_AddRef,
758 MessageFilter_Release,
759 MessageFilter_HandleInComingCall,
760 MessageFilter_RetryRejectedCall,
761 MessageFilter_MessagePending
764 static IMessageFilter MessageFilter = { &MessageFilter_Vtbl };
766 static void test_CoRegisterMessageFilter(void)
768 HRESULT hr;
769 IMessageFilter *prev_filter;
771 hr = CoRegisterMessageFilter(&MessageFilter, &prev_filter);
772 ok(hr == CO_E_NOT_SUPPORTED,
773 "CoRegisterMessageFilter should have failed with CO_E_NOT_SUPPORTED instead of 0x%08x\n",
774 hr);
776 pCoInitializeEx(NULL, COINIT_MULTITHREADED);
777 prev_filter = (IMessageFilter *)0xdeadbeef;
778 hr = CoRegisterMessageFilter(&MessageFilter, &prev_filter);
779 ok(hr == CO_E_NOT_SUPPORTED,
780 "CoRegisterMessageFilter should have failed with CO_E_NOT_SUPPORTED instead of 0x%08x\n",
781 hr);
782 ok(prev_filter == (IMessageFilter *)0xdeadbeef,
783 "prev_filter should have been set to %p\n", prev_filter);
784 CoUninitialize();
786 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
788 hr = CoRegisterMessageFilter(NULL, NULL);
789 ok_ole_success(hr, "CoRegisterMessageFilter");
791 prev_filter = (IMessageFilter *)0xdeadbeef;
792 hr = CoRegisterMessageFilter(NULL, &prev_filter);
793 ok_ole_success(hr, "CoRegisterMessageFilter");
794 ok(prev_filter == NULL, "prev_filter should have been set to NULL instead of %p\n", prev_filter);
796 hr = CoRegisterMessageFilter(&MessageFilter, &prev_filter);
797 ok_ole_success(hr, "CoRegisterMessageFilter");
798 ok(prev_filter == NULL, "prev_filter should have been set to NULL instead of %p\n", prev_filter);
800 hr = CoRegisterMessageFilter(NULL, NULL);
801 ok_ole_success(hr, "CoRegisterMessageFilter");
803 CoUninitialize();
806 static HRESULT WINAPI Test_IUnknown_QueryInterface(
807 IUnknown *iface,
808 REFIID riid,
809 LPVOID *ppvObj)
811 if (ppvObj == NULL) return E_POINTER;
813 if (IsEqualIID(riid, &IID_IUnknown) ||
814 IsEqualIID(riid, &IID_IWineTest))
816 *ppvObj = iface;
817 IUnknown_AddRef(iface);
818 return S_OK;
821 *ppvObj = NULL;
822 return E_NOINTERFACE;
825 static ULONG WINAPI Test_IUnknown_AddRef(IUnknown *iface)
827 return 2; /* non-heap-based object */
830 static ULONG WINAPI Test_IUnknown_Release(IUnknown *iface)
832 return 1; /* non-heap-based object */
835 static const IUnknownVtbl TestUnknown_Vtbl =
837 Test_IUnknown_QueryInterface,
838 Test_IUnknown_AddRef,
839 Test_IUnknown_Release,
842 static IUnknown Test_Unknown = { &TestUnknown_Vtbl };
844 static HRESULT WINAPI PSFactoryBuffer_QueryInterface(
845 IPSFactoryBuffer * This,
846 /* [in] */ REFIID riid,
847 /* [iid_is][out] */ void **ppvObject)
849 if (IsEqualIID(riid, &IID_IUnknown) ||
850 IsEqualIID(riid, &IID_IPSFactoryBuffer))
852 *ppvObject = This;
853 IPSFactoryBuffer_AddRef(This);
854 return S_OK;
856 return E_NOINTERFACE;
859 static ULONG WINAPI PSFactoryBuffer_AddRef(
860 IPSFactoryBuffer * This)
862 return 2;
865 static ULONG WINAPI PSFactoryBuffer_Release(
866 IPSFactoryBuffer * This)
868 return 1;
871 static HRESULT WINAPI PSFactoryBuffer_CreateProxy(
872 IPSFactoryBuffer * This,
873 /* [in] */ IUnknown *pUnkOuter,
874 /* [in] */ REFIID riid,
875 /* [out] */ IRpcProxyBuffer **ppProxy,
876 /* [out] */ void **ppv)
878 return E_NOTIMPL;
881 static HRESULT WINAPI PSFactoryBuffer_CreateStub(
882 IPSFactoryBuffer * This,
883 /* [in] */ REFIID riid,
884 /* [unique][in] */ IUnknown *pUnkServer,
885 /* [out] */ IRpcStubBuffer **ppStub)
887 return E_NOTIMPL;
890 static IPSFactoryBufferVtbl PSFactoryBufferVtbl =
892 PSFactoryBuffer_QueryInterface,
893 PSFactoryBuffer_AddRef,
894 PSFactoryBuffer_Release,
895 PSFactoryBuffer_CreateProxy,
896 PSFactoryBuffer_CreateStub
899 static IPSFactoryBuffer PSFactoryBuffer = { &PSFactoryBufferVtbl };
901 static const CLSID CLSID_WineTestPSFactoryBuffer =
903 0x52011640,
904 0x8164,
905 0x4fd0,
906 {0xa1, 0xa2, 0x5d, 0x5a, 0x36, 0x54, 0xd3, 0xbd}
907 }; /* 52011640-8164-4fd0-a1a2-5d5a3654d3bd */
909 static void test_CoRegisterPSClsid(void)
911 HRESULT hr;
912 DWORD dwRegistrationKey;
913 IStream *stream;
914 CLSID clsid;
916 hr = CoRegisterPSClsid(&IID_IWineTest, &CLSID_WineTestPSFactoryBuffer);
917 ok(hr == CO_E_NOTINITIALIZED, "CoRegisterPSClsid should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n", hr);
919 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
921 hr = CoRegisterClassObject(&CLSID_WineTestPSFactoryBuffer, (IUnknown *)&PSFactoryBuffer,
922 CLSCTX_INPROC_SERVER, REGCLS_MULTIPLEUSE, &dwRegistrationKey);
923 ok_ole_success(hr, "CoRegisterClassObject");
925 hr = CoRegisterPSClsid(&IID_IWineTest, &CLSID_WineTestPSFactoryBuffer);
926 ok_ole_success(hr, "CoRegisterPSClsid");
928 hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
929 ok_ole_success(hr, "CreateStreamOnHGlobal");
931 hr = CoMarshalInterface(stream, &IID_IWineTest, &Test_Unknown, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
932 ok(hr == E_NOTIMPL, "CoMarshalInterface should have returned E_NOTIMPL instead of 0x%08x\n", hr);
933 IStream_Release(stream);
935 hr = CoRevokeClassObject(dwRegistrationKey);
936 ok_ole_success(hr, "CoRevokeClassObject");
938 CoUninitialize();
940 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
942 hr = CoGetPSClsid(&IID_IWineTest, &clsid);
943 ok(hr == REGDB_E_IIDNOTREG, "CoGetPSClsid should have returned REGDB_E_IIDNOTREG instead of 0x%08x\n", hr);
945 CoUninitialize();
948 static void test_CoGetPSClsid(void)
950 ULONG_PTR cookie;
951 HANDLE handle;
952 HRESULT hr;
953 CLSID clsid;
954 HKEY hkey;
955 LONG res;
957 hr = CoGetPSClsid(&IID_IClassFactory, &clsid);
958 ok(hr == CO_E_NOTINITIALIZED,
959 "CoGetPSClsid should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n",
960 hr);
962 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
964 hr = CoGetPSClsid(&IID_IClassFactory, &clsid);
965 ok_ole_success(hr, "CoGetPSClsid");
967 hr = CoGetPSClsid(&IID_IWineTest, &clsid);
968 ok(hr == REGDB_E_IIDNOTREG,
969 "CoGetPSClsid for random IID returned 0x%08x instead of REGDB_E_IIDNOTREG\n",
970 hr);
972 hr = CoGetPSClsid(&IID_IClassFactory, NULL);
973 ok(hr == E_INVALIDARG,
974 "CoGetPSClsid for null clsid returned 0x%08x instead of E_INVALIDARG\n",
975 hr);
977 if (!pRegOverridePredefKey)
979 win_skip("RegOverridePredefKey not available\n");
980 CoUninitialize();
981 return;
983 hr = CoGetPSClsid(&IID_IClassFactory, &clsid);
984 ok_ole_success(hr, "CoGetPSClsid");
986 res = RegCreateKeyExA(HKEY_CURRENT_USER, "Software\\Classes", 0, NULL, 0,
987 KEY_ALL_ACCESS, NULL, &hkey, NULL);
988 ok(!res, "RegCreateKeyEx returned %d\n", res);
990 res = pRegOverridePredefKey(HKEY_CLASSES_ROOT, hkey);
991 ok(!res, "RegOverridePredefKey returned %d\n", res);
993 hr = CoGetPSClsid(&IID_IClassFactory, &clsid);
994 ok_ole_success(hr, "CoGetPSClsid");
996 res = pRegOverridePredefKey(HKEY_CLASSES_ROOT, NULL);
997 ok(!res, "RegOverridePredefKey returned %d\n", res);
999 RegCloseKey(hkey);
1001 /* not registered CLSID */
1002 hr = CoGetPSClsid(&IID_Testiface, &clsid);
1003 ok(hr == REGDB_E_IIDNOTREG, "got 0x%08x\n", hr);
1005 if ((handle = activate_context(actctx_manifest, &cookie)))
1007 todo_wine {
1008 memset(&clsid, 0, sizeof(clsid));
1009 hr = CoGetPSClsid(&IID_Testiface, &clsid);
1010 ok(hr == S_OK, "got 0x%08x\n", hr);
1011 ok(IsEqualGUID(&clsid, &IID_Testiface), "got clsid %s\n", debugstr_guid(&clsid));
1013 memset(&clsid, 0, sizeof(clsid));
1014 hr = CoGetPSClsid(&IID_Testiface2, &clsid);
1015 ok(hr == S_OK, "got 0x%08x\n", hr);
1016 ok(IsEqualGUID(&clsid, &IID_Testiface2), "got clsid %s\n", debugstr_guid(&clsid));
1018 memset(&clsid, 0, sizeof(clsid));
1019 hr = CoGetPSClsid(&IID_Testiface3, &clsid);
1020 ok(hr == S_OK, "got 0x%08x\n", hr);
1021 ok(IsEqualGUID(&clsid, &IID_TestPS), "got clsid %s\n", debugstr_guid(&clsid));
1023 memset(&clsid, 0xaa, sizeof(clsid));
1024 hr = CoGetPSClsid(&IID_Testiface4, &clsid);
1025 ok(hr == S_OK, "got 0x%08x\n", hr);
1026 ok(IsEqualGUID(&clsid, &GUID_NULL), "got clsid %s\n", debugstr_guid(&clsid));
1028 pDeactivateActCtx(0, cookie);
1029 pReleaseActCtx(handle);
1032 CoUninitialize();
1035 /* basic test, mainly for invalid arguments. see marshal.c for more */
1036 static void test_CoUnmarshalInterface(void)
1038 IUnknown *pProxy;
1039 IStream *pStream;
1040 HRESULT hr;
1042 hr = CoUnmarshalInterface(NULL, &IID_IUnknown, (void **)&pProxy);
1043 ok(hr == E_INVALIDARG, "CoUnmarshalInterface should have returned E_INVALIDARG instead of 0x%08x\n", hr);
1045 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1046 ok_ole_success(hr, "CreateStreamOnHGlobal");
1048 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)&pProxy);
1049 todo_wine
1050 ok(hr == CO_E_NOTINITIALIZED, "CoUnmarshalInterface should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n", hr);
1052 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1054 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)&pProxy);
1055 ok(hr == STG_E_READFAULT, "CoUnmarshalInterface should have returned STG_E_READFAULT instead of 0x%08x\n", hr);
1057 CoUninitialize();
1059 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, NULL);
1060 ok(hr == E_INVALIDARG, "CoUnmarshalInterface should have returned E_INVALIDARG instead of 0x%08x\n", hr);
1062 IStream_Release(pStream);
1065 static void test_CoGetInterfaceAndReleaseStream(void)
1067 HRESULT hr;
1068 IUnknown *pUnk;
1070 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1072 hr = CoGetInterfaceAndReleaseStream(NULL, &IID_IUnknown, (void**)&pUnk);
1073 ok(hr == E_INVALIDARG, "hr %08x\n", hr);
1075 CoUninitialize();
1078 /* basic test, mainly for invalid arguments. see marshal.c for more */
1079 static void test_CoMarshalInterface(void)
1081 IStream *pStream;
1082 HRESULT hr;
1083 static const LARGE_INTEGER llZero;
1085 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1087 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1088 ok_ole_success(hr, "CreateStreamOnHGlobal");
1090 hr = CoMarshalInterface(pStream, &IID_IUnknown, NULL, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1091 ok(hr == E_INVALIDARG, "CoMarshalInterface should have returned E_INVALIDARG instead of 0x%08x\n", hr);
1093 hr = CoMarshalInterface(NULL, &IID_IUnknown, (IUnknown *)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1094 ok(hr == E_INVALIDARG, "CoMarshalInterface should have returned E_INVALIDARG instead of 0x%08x\n", hr);
1096 hr = CoMarshalInterface(pStream, &IID_IUnknown, (IUnknown *)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1097 ok_ole_success(hr, "CoMarshalInterface");
1099 /* stream not rewound */
1100 hr = CoReleaseMarshalData(pStream);
1101 ok(hr == STG_E_READFAULT, "CoReleaseMarshalData should have returned STG_E_READFAULT instead of 0x%08x\n", hr);
1103 hr = IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
1104 ok_ole_success(hr, "IStream_Seek");
1106 hr = CoReleaseMarshalData(pStream);
1107 ok_ole_success(hr, "CoReleaseMarshalData");
1109 IStream_Release(pStream);
1111 CoUninitialize();
1114 static void test_CoMarshalInterThreadInterfaceInStream(void)
1116 IStream *pStream;
1117 HRESULT hr;
1118 IClassFactory *pProxy;
1120 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1122 cLocks = 0;
1124 hr = CoMarshalInterThreadInterfaceInStream(&IID_IUnknown, (IUnknown *)&Test_ClassFactory, NULL);
1125 ok(hr == E_INVALIDARG, "CoMarshalInterThreadInterfaceInStream should have returned E_INVALIDARG instead of 0x%08x\n", hr);
1127 hr = CoMarshalInterThreadInterfaceInStream(&IID_IUnknown, NULL, &pStream);
1128 ok(hr == E_INVALIDARG, "CoMarshalInterThreadInterfaceInStream should have returned E_INVALIDARG instead of 0x%08x\n", hr);
1130 ok_no_locks();
1132 hr = CoMarshalInterThreadInterfaceInStream(&IID_IUnknown, (IUnknown *)&Test_ClassFactory, &pStream);
1133 ok_ole_success(hr, "CoMarshalInterThreadInterfaceInStream");
1135 ok_more_than_one_lock();
1137 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
1138 ok_ole_success(hr, "CoUnmarshalInterface");
1140 IClassFactory_Release(pProxy);
1141 IStream_Release(pStream);
1143 ok_no_locks();
1145 CoUninitialize();
1148 static void test_CoRegisterClassObject(void)
1150 DWORD cookie;
1151 HRESULT hr;
1152 IClassFactory *pcf;
1154 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1156 /* CLSCTX_INPROC_SERVER */
1157 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
1158 CLSCTX_INPROC_SERVER, REGCLS_SINGLEUSE, &cookie);
1159 ok_ole_success(hr, "CoRegisterClassObject");
1160 hr = CoRevokeClassObject(cookie);
1161 ok_ole_success(hr, "CoRevokeClassObject");
1163 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
1164 CLSCTX_INPROC_SERVER, REGCLS_MULTIPLEUSE, &cookie);
1165 ok_ole_success(hr, "CoRegisterClassObject");
1166 hr = CoRevokeClassObject(cookie);
1167 ok_ole_success(hr, "CoRevokeClassObject");
1169 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
1170 CLSCTX_INPROC_SERVER, REGCLS_MULTI_SEPARATE, &cookie);
1171 ok_ole_success(hr, "CoRegisterClassObject");
1172 hr = CoRevokeClassObject(cookie);
1173 ok_ole_success(hr, "CoRevokeClassObject");
1175 /* CLSCTX_LOCAL_SERVER */
1176 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
1177 CLSCTX_LOCAL_SERVER, REGCLS_SINGLEUSE, &cookie);
1178 ok_ole_success(hr, "CoRegisterClassObject");
1179 hr = CoRevokeClassObject(cookie);
1180 ok_ole_success(hr, "CoRevokeClassObject");
1182 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
1183 CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE, &cookie);
1184 ok_ole_success(hr, "CoRegisterClassObject");
1185 hr = CoRevokeClassObject(cookie);
1186 ok_ole_success(hr, "CoRevokeClassObject");
1188 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
1189 CLSCTX_LOCAL_SERVER, REGCLS_MULTI_SEPARATE, &cookie);
1190 ok_ole_success(hr, "CoRegisterClassObject");
1191 hr = CoRevokeClassObject(cookie);
1192 ok_ole_success(hr, "CoRevokeClassObject");
1194 /* CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER */
1195 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
1196 CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, REGCLS_SINGLEUSE, &cookie);
1197 ok_ole_success(hr, "CoRegisterClassObject");
1198 hr = CoRevokeClassObject(cookie);
1199 ok_ole_success(hr, "CoRevokeClassObject");
1201 /* test whether an object that doesn't support IClassFactory can be
1202 * registered for CLSCTX_LOCAL_SERVER */
1203 hr = CoRegisterClassObject(&CLSID_WineOOPTest, &Test_Unknown,
1204 CLSCTX_LOCAL_SERVER, REGCLS_SINGLEUSE, &cookie);
1205 ok_ole_success(hr, "CoRegisterClassObject");
1206 hr = CoRevokeClassObject(cookie);
1207 ok_ole_success(hr, "CoRevokeClassObject");
1209 /* test whether registered class becomes invalid when apartment is destroyed */
1210 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
1211 CLSCTX_INPROC_SERVER, REGCLS_SINGLEUSE, &cookie);
1212 ok_ole_success(hr, "CoRegisterClassObject");
1214 CoUninitialize();
1215 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1217 hr = CoGetClassObject(&CLSID_WineOOPTest, CLSCTX_INPROC_SERVER, NULL,
1218 &IID_IClassFactory, (void **)&pcf);
1219 ok(hr == REGDB_E_CLASSNOTREG, "object registered in an apartment shouldn't accessible after it is destroyed\n");
1221 /* crashes with at least win9x DCOM! */
1222 if (0)
1223 CoRevokeClassObject(cookie);
1225 CoUninitialize();
1228 static HRESULT get_class_object(CLSCTX clsctx)
1230 HRESULT hr;
1231 IClassFactory *pcf;
1233 hr = CoGetClassObject(&CLSID_WineOOPTest, clsctx, NULL, &IID_IClassFactory,
1234 (void **)&pcf);
1236 if (SUCCEEDED(hr))
1237 IClassFactory_Release(pcf);
1239 return hr;
1242 static DWORD CALLBACK get_class_object_thread(LPVOID pv)
1244 CLSCTX clsctx = (CLSCTX)(DWORD_PTR)pv;
1245 HRESULT hr;
1247 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1249 hr = get_class_object(clsctx);
1251 CoUninitialize();
1253 return hr;
1256 static DWORD CALLBACK get_class_object_proxy_thread(LPVOID pv)
1258 CLSCTX clsctx = (CLSCTX)(DWORD_PTR)pv;
1259 HRESULT hr;
1260 IClassFactory *pcf;
1261 IMultiQI *pMQI;
1263 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1265 hr = CoGetClassObject(&CLSID_WineOOPTest, clsctx, NULL, &IID_IClassFactory,
1266 (void **)&pcf);
1268 if (SUCCEEDED(hr))
1270 hr = IClassFactory_QueryInterface(pcf, &IID_IMultiQI, (void **)&pMQI);
1271 if (SUCCEEDED(hr))
1272 IMultiQI_Release(pMQI);
1273 IClassFactory_Release(pcf);
1276 CoUninitialize();
1278 return hr;
1281 static DWORD CALLBACK register_class_object_thread(LPVOID pv)
1283 HRESULT hr;
1284 DWORD cookie;
1286 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1288 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
1289 CLSCTX_INPROC_SERVER, REGCLS_SINGLEUSE, &cookie);
1291 CoUninitialize();
1293 return hr;
1296 static DWORD CALLBACK revoke_class_object_thread(LPVOID pv)
1298 DWORD cookie = (DWORD_PTR)pv;
1299 HRESULT hr;
1301 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1303 hr = CoRevokeClassObject(cookie);
1305 CoUninitialize();
1307 return hr;
1310 static void test_registered_object_thread_affinity(void)
1312 HRESULT hr;
1313 DWORD cookie;
1314 HANDLE thread;
1315 DWORD tid;
1316 DWORD exitcode;
1318 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1320 /* CLSCTX_INPROC_SERVER */
1322 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
1323 CLSCTX_INPROC_SERVER, REGCLS_SINGLEUSE, &cookie);
1324 ok_ole_success(hr, "CoRegisterClassObject");
1326 thread = CreateThread(NULL, 0, get_class_object_thread, (LPVOID)CLSCTX_INPROC_SERVER, 0, &tid);
1327 ok(thread != NULL, "CreateThread failed with error %d\n", GetLastError());
1328 ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
1329 GetExitCodeThread(thread, &exitcode);
1330 hr = exitcode;
1331 ok(hr == REGDB_E_CLASSNOTREG, "CoGetClassObject on inproc object "
1332 "registered in different thread should return REGDB_E_CLASSNOTREG "
1333 "instead of 0x%08x\n", hr);
1335 hr = get_class_object(CLSCTX_INPROC_SERVER);
1336 ok(hr == S_OK, "CoGetClassObject on inproc object registered in same "
1337 "thread should return S_OK instead of 0x%08x\n", hr);
1339 thread = CreateThread(NULL, 0, register_class_object_thread, NULL, 0, &tid);
1340 ok(thread != NULL, "CreateThread failed with error %d\n", GetLastError());
1341 ok ( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
1342 GetExitCodeThread(thread, &exitcode);
1343 hr = exitcode;
1344 ok(hr == S_OK, "CoRegisterClassObject with same CLSID but in different thread should return S_OK instead of 0x%08x\n", hr);
1346 hr = CoRevokeClassObject(cookie);
1347 ok_ole_success(hr, "CoRevokeClassObject");
1349 /* CLSCTX_LOCAL_SERVER */
1351 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
1352 CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE, &cookie);
1353 ok_ole_success(hr, "CoRegisterClassObject");
1355 thread = CreateThread(NULL, 0, get_class_object_proxy_thread, (LPVOID)CLSCTX_LOCAL_SERVER, 0, &tid);
1356 ok(thread != NULL, "CreateThread failed with error %d\n", GetLastError());
1357 while (MsgWaitForMultipleObjects(1, &thread, FALSE, 10000, QS_ALLINPUT) == WAIT_OBJECT_0 + 1)
1359 MSG msg;
1360 while (PeekMessageA(&msg, NULL, 0, 0, PM_REMOVE))
1362 TranslateMessage(&msg);
1363 DispatchMessageA(&msg);
1366 GetExitCodeThread(thread, &exitcode);
1367 hr = exitcode;
1368 ok(hr == S_OK, "CoGetClassObject on local server object "
1369 "registered in different thread should return S_OK "
1370 "instead of 0x%08x\n", hr);
1372 hr = get_class_object(CLSCTX_LOCAL_SERVER);
1373 ok(hr == S_OK, "CoGetClassObject on local server object registered in same "
1374 "thread should return S_OK instead of 0x%08x\n", hr);
1376 thread = CreateThread(NULL, 0, revoke_class_object_thread, (LPVOID)(DWORD_PTR)cookie, 0, &tid);
1377 ok(thread != NULL, "CreateThread failed with error %d\n", GetLastError());
1378 ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
1379 GetExitCodeThread(thread, &exitcode);
1380 hr = exitcode;
1381 ok(hr == RPC_E_WRONG_THREAD, "CoRevokeClassObject called from different "
1382 "thread to where registered should return RPC_E_WRONG_THREAD instead of 0x%08x\n", hr);
1384 thread = CreateThread(NULL, 0, register_class_object_thread, NULL, 0, &tid);
1385 ok(thread != NULL, "CreateThread failed with error %d\n", GetLastError());
1386 ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
1387 GetExitCodeThread(thread, &exitcode);
1388 hr = exitcode;
1389 ok(hr == S_OK, "CoRegisterClassObject with same CLSID but in different "
1390 "thread should return S_OK instead of 0x%08x\n", hr);
1392 hr = CoRevokeClassObject(cookie);
1393 ok_ole_success(hr, "CoRevokeClassObject");
1395 CoUninitialize();
1398 static DWORD CALLBACK free_libraries_thread(LPVOID p)
1400 CoFreeUnusedLibraries();
1401 return 0;
1404 static inline BOOL is_module_loaded(const char *module)
1406 return GetModuleHandleA(module) != 0;
1409 static void test_CoFreeUnusedLibraries(void)
1411 HRESULT hr;
1412 IUnknown *pUnk;
1413 DWORD tid;
1414 HANDLE thread;
1416 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1418 ok(!is_module_loaded("urlmon.dll"), "urlmon.dll shouldn't be loaded\n");
1420 hr = CoCreateInstance(&CLSID_FileProtocol, NULL, CLSCTX_INPROC_SERVER, &IID_IInternetProtocol, (void **)&pUnk);
1421 if (hr == REGDB_E_CLASSNOTREG)
1423 skip("IE not installed so can't run CoFreeUnusedLibraries test\n");
1424 CoUninitialize();
1425 return;
1427 ok_ole_success(hr, "CoCreateInstance");
1429 ok(is_module_loaded("urlmon.dll"), "urlmon.dll should be loaded\n");
1431 ok(pUnk != NULL ||
1432 broken(pUnk == NULL), /* win9x */
1433 "Expected a valid pointer\n");
1434 if (pUnk)
1435 IUnknown_Release(pUnk);
1437 ok(is_module_loaded("urlmon.dll"), "urlmon.dll should be loaded\n");
1439 thread = CreateThread(NULL, 0, free_libraries_thread, NULL, 0, &tid);
1440 ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
1441 CloseHandle(thread);
1443 ok(is_module_loaded("urlmon.dll"), "urlmon.dll should be loaded\n");
1445 CoFreeUnusedLibraries();
1447 ok(!is_module_loaded("urlmon.dll"), "urlmon.dll shouldn't be loaded\n");
1449 CoUninitialize();
1452 static void test_CoGetObjectContext(void)
1454 HRESULT hr;
1455 ULONG refs;
1456 IComThreadingInfo *pComThreadingInfo;
1457 IContextCallback *pContextCallback;
1458 IObjContext *pObjContext;
1459 APTTYPE apttype;
1460 THDTYPE thdtype;
1461 struct info info;
1462 HANDLE thread;
1463 DWORD tid, exitcode;
1465 if (!pCoGetObjectContext)
1467 skip("CoGetObjectContext not present\n");
1468 return;
1471 hr = pCoGetObjectContext(&IID_IComThreadingInfo, (void **)&pComThreadingInfo);
1472 ok(hr == CO_E_NOTINITIALIZED, "CoGetObjectContext should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n", hr);
1473 ok(pComThreadingInfo == NULL, "pComThreadingInfo should have been set to NULL\n");
1475 /* show that COM doesn't have to be initialized for multi-threaded apartments if another
1476 thread has already done so */
1478 info.wait = CreateEventA(NULL, TRUE, FALSE, NULL);
1479 ok(info.wait != NULL, "CreateEvent failed with error %d\n", GetLastError());
1481 info.stop = CreateEventA(NULL, TRUE, FALSE, NULL);
1482 ok(info.stop != NULL, "CreateEvent failed with error %d\n", GetLastError());
1484 thread = CreateThread(NULL, 0, ole_initialize_thread, &info, 0, &tid);
1485 ok(thread != NULL, "CreateThread failed with error %d\n", GetLastError());
1487 ok( !WaitForSingleObject(info.wait, 10000), "wait timed out\n" );
1489 pComThreadingInfo = NULL;
1490 hr = pCoGetObjectContext(&IID_IComThreadingInfo, (void **)&pComThreadingInfo);
1491 ok(hr == S_OK, "Expected S_OK, got 0x%08x\n", hr);
1492 IComThreadingInfo_Release(pComThreadingInfo);
1494 SetEvent(info.stop);
1495 ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
1497 GetExitCodeThread(thread, &exitcode);
1498 hr = exitcode;
1499 ok(hr == S_OK, "thread should have returned S_OK instead of 0x%08x\n", hr);
1501 CloseHandle(thread);
1502 CloseHandle(info.wait);
1503 CloseHandle(info.stop);
1505 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1507 hr = pCoGetObjectContext(&IID_IComThreadingInfo, (void **)&pComThreadingInfo);
1508 ok_ole_success(hr, "CoGetObjectContext");
1510 hr = IComThreadingInfo_GetCurrentApartmentType(pComThreadingInfo, &apttype);
1511 ok_ole_success(hr, "IComThreadingInfo_GetCurrentApartmentType");
1512 ok(apttype == APTTYPE_MAINSTA, "apartment type should be APTTYPE_MAINSTA instead of %d\n", apttype);
1514 hr = IComThreadingInfo_GetCurrentThreadType(pComThreadingInfo, &thdtype);
1515 ok_ole_success(hr, "IComThreadingInfo_GetCurrentThreadType");
1516 ok(thdtype == THDTYPE_PROCESSMESSAGES, "thread type should be THDTYPE_PROCESSMESSAGES instead of %d\n", thdtype);
1518 refs = IComThreadingInfo_Release(pComThreadingInfo);
1519 ok(refs == 0, "pComThreadingInfo should have 0 refs instead of %d refs\n", refs);
1521 hr = pCoGetObjectContext(&IID_IContextCallback, (void **)&pContextCallback);
1522 ok_ole_success(hr, "CoGetObjectContext(ContextCallback)");
1524 if (hr == S_OK)
1526 refs = IContextCallback_Release(pContextCallback);
1527 ok(refs == 0, "pContextCallback should have 0 refs instead of %d refs\n", refs);
1530 CoUninitialize();
1532 pCoInitializeEx(NULL, COINIT_MULTITHREADED);
1534 hr = pCoGetObjectContext(&IID_IComThreadingInfo, (void **)&pComThreadingInfo);
1535 ok_ole_success(hr, "CoGetObjectContext");
1537 hr = IComThreadingInfo_GetCurrentApartmentType(pComThreadingInfo, &apttype);
1538 ok_ole_success(hr, "IComThreadingInfo_GetCurrentApartmentType");
1539 ok(apttype == APTTYPE_MTA, "apartment type should be APTTYPE_MTA instead of %d\n", apttype);
1541 hr = IComThreadingInfo_GetCurrentThreadType(pComThreadingInfo, &thdtype);
1542 ok_ole_success(hr, "IComThreadingInfo_GetCurrentThreadType");
1543 ok(thdtype == THDTYPE_BLOCKMESSAGES, "thread type should be THDTYPE_BLOCKMESSAGES instead of %d\n", thdtype);
1545 refs = IComThreadingInfo_Release(pComThreadingInfo);
1546 ok(refs == 0, "pComThreadingInfo should have 0 refs instead of %d refs\n", refs);
1548 hr = pCoGetObjectContext(&IID_IContextCallback, (void **)&pContextCallback);
1549 ok_ole_success(hr, "CoGetObjectContext(ContextCallback)");
1551 if (hr == S_OK)
1553 refs = IContextCallback_Release(pContextCallback);
1554 ok(refs == 0, "pContextCallback should have 0 refs instead of %d refs\n", refs);
1557 hr = pCoGetObjectContext(&IID_IObjContext, (void **)&pObjContext);
1558 ok_ole_success(hr, "CoGetObjectContext");
1560 refs = IObjContext_Release(pObjContext);
1561 ok(refs == 0, "pObjContext should have 0 refs instead of %d refs\n", refs);
1563 CoUninitialize();
1566 typedef struct {
1567 IUnknown IUnknown_iface;
1568 LONG refs;
1569 } Test_CallContext;
1571 static inline Test_CallContext *impl_from_IUnknown(IUnknown *iface)
1573 return CONTAINING_RECORD(iface, Test_CallContext, IUnknown_iface);
1576 static HRESULT WINAPI Test_CallContext_QueryInterface(
1577 IUnknown *iface,
1578 REFIID riid,
1579 LPVOID *ppvObj)
1581 if (ppvObj == NULL) return E_POINTER;
1583 if (IsEqualGUID(riid, &IID_IUnknown))
1585 *ppvObj = iface;
1586 IUnknown_AddRef(iface);
1587 return S_OK;
1590 *ppvObj = NULL;
1591 return E_NOINTERFACE;
1594 static ULONG WINAPI Test_CallContext_AddRef(IUnknown *iface)
1596 Test_CallContext *This = impl_from_IUnknown(iface);
1597 return InterlockedIncrement(&This->refs);
1600 static ULONG WINAPI Test_CallContext_Release(IUnknown *iface)
1602 Test_CallContext *This = impl_from_IUnknown(iface);
1603 ULONG refs = InterlockedDecrement(&This->refs);
1604 if (!refs)
1605 HeapFree(GetProcessHeap(), 0, This);
1606 return refs;
1609 static const IUnknownVtbl TestCallContext_Vtbl =
1611 Test_CallContext_QueryInterface,
1612 Test_CallContext_AddRef,
1613 Test_CallContext_Release
1616 static void test_CoGetCallContext(void)
1618 HRESULT hr;
1619 ULONG refs;
1620 IUnknown *pUnk;
1621 Test_CallContext *test_object;
1623 if (!pCoSwitchCallContext)
1625 skip("CoSwitchCallContext not present\n");
1626 return;
1629 CoInitialize(NULL);
1631 test_object = HeapAlloc(GetProcessHeap(), 0, sizeof(Test_CallContext));
1632 test_object->IUnknown_iface.lpVtbl = &TestCallContext_Vtbl;
1633 test_object->refs = 1;
1635 hr = CoGetCallContext(&IID_IUnknown, (void**)&pUnk);
1636 ok(hr == RPC_E_CALL_COMPLETE, "Expected RPC_E_CALL_COMPLETE, got 0x%08x\n", hr);
1638 pUnk = (IUnknown*)0xdeadbeef;
1639 hr = pCoSwitchCallContext(&test_object->IUnknown_iface, &pUnk);
1640 ok_ole_success(hr, "CoSwitchCallContext");
1641 ok(pUnk == NULL, "expected NULL, got %p\n", pUnk);
1642 refs = IUnknown_AddRef(&test_object->IUnknown_iface);
1643 ok(refs == 2, "Expected refcount 2, got %d\n", refs);
1644 IUnknown_Release(&test_object->IUnknown_iface);
1646 pUnk = (IUnknown*)0xdeadbeef;
1647 hr = CoGetCallContext(&IID_IUnknown, (void**)&pUnk);
1648 ok_ole_success(hr, "CoGetCallContext");
1649 ok(pUnk == &test_object->IUnknown_iface, "expected %p, got %p\n",
1650 &test_object->IUnknown_iface, pUnk);
1651 refs = IUnknown_AddRef(&test_object->IUnknown_iface);
1652 ok(refs == 3, "Expected refcount 3, got %d\n", refs);
1653 IUnknown_Release(&test_object->IUnknown_iface);
1654 IUnknown_Release(pUnk);
1656 pUnk = (IUnknown*)0xdeadbeef;
1657 hr = pCoSwitchCallContext(NULL, &pUnk);
1658 ok_ole_success(hr, "CoSwitchCallContext");
1659 ok(pUnk == &test_object->IUnknown_iface, "expected %p, got %p\n",
1660 &test_object->IUnknown_iface, pUnk);
1661 refs = IUnknown_AddRef(&test_object->IUnknown_iface);
1662 ok(refs == 2, "Expected refcount 2, got %d\n", refs);
1663 IUnknown_Release(&test_object->IUnknown_iface);
1665 hr = CoGetCallContext(&IID_IUnknown, (void**)&pUnk);
1666 ok(hr == RPC_E_CALL_COMPLETE, "Expected RPC_E_CALL_COMPLETE, got 0x%08x\n", hr);
1668 IUnknown_Release(&test_object->IUnknown_iface);
1670 CoUninitialize();
1673 static void test_CoGetContextToken(void)
1675 HRESULT hr;
1676 ULONG refs;
1677 ULONG_PTR token;
1678 IObjContext *ctx;
1679 struct info info;
1680 HANDLE thread;
1681 DWORD tid, exitcode;
1683 if (!pCoGetContextToken)
1685 win_skip("CoGetContextToken not present\n");
1686 return;
1689 token = 0xdeadbeef;
1690 hr = pCoGetContextToken(&token);
1691 ok(hr == CO_E_NOTINITIALIZED, "Expected CO_E_NOTINITIALIZED, got 0x%08x\n", hr);
1692 ok(token == 0xdeadbeef, "Expected 0, got 0x%lx\n", token);
1694 /* show that COM doesn't have to be initialized for multi-threaded apartments if another
1695 thread has already done so */
1697 info.wait = CreateEventA(NULL, TRUE, FALSE, NULL);
1698 ok(info.wait != NULL, "CreateEvent failed with error %d\n", GetLastError());
1700 info.stop = CreateEventA(NULL, TRUE, FALSE, NULL);
1701 ok(info.stop != NULL, "CreateEvent failed with error %d\n", GetLastError());
1703 thread = CreateThread(NULL, 0, ole_initialize_thread, &info, 0, &tid);
1704 ok(thread != NULL, "CreateThread failed with error %d\n", GetLastError());
1706 ok( !WaitForSingleObject(info.wait, 10000), "wait timed out\n" );
1708 token = 0;
1709 hr = pCoGetContextToken(&token);
1710 ok(hr == S_OK, "Expected S_OK, got 0x%08x\n", hr);
1712 SetEvent(info.stop);
1713 ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
1715 GetExitCodeThread(thread, &exitcode);
1716 hr = exitcode;
1717 ok(hr == S_OK, "thread should have returned S_OK instead of 0x%08x\n", hr);
1719 CloseHandle(thread);
1720 CloseHandle(info.wait);
1721 CloseHandle(info.stop);
1723 CoInitialize(NULL);
1725 hr = pCoGetContextToken(NULL);
1726 ok(hr == E_POINTER, "Expected E_POINTER, got 0x%08x\n", hr);
1728 token = 0;
1729 hr = pCoGetContextToken(&token);
1730 ok(hr == S_OK, "Expected S_OK, got 0x%08x\n", hr);
1731 ok(token, "Expected token != 0\n");
1733 refs = IUnknown_AddRef((IUnknown *)token);
1734 todo_wine ok(refs == 1, "Expected 1, got %u\n", refs);
1736 hr = pCoGetObjectContext(&IID_IObjContext, (void **)&ctx);
1737 ok(hr == S_OK, "Expected S_OK, got 0x%08x\n", hr);
1738 todo_wine ok(ctx == (IObjContext *)token, "Expected interface pointers to be the same\n");
1740 refs = IUnknown_AddRef((IUnknown *)ctx);
1741 todo_wine ok(refs == 3, "Expected 3, got %u\n", refs);
1743 refs = IUnknown_Release((IUnknown *)ctx);
1744 todo_wine ok(refs == 2, "Expected 2, got %u\n", refs);
1746 refs = IUnknown_Release((IUnknown *)token);
1747 ok(refs == 1, "Expected 1, got %u\n", refs);
1749 /* CoGetContextToken does not add a reference */
1750 token = 0;
1751 hr = pCoGetContextToken(&token);
1752 ok(hr == S_OK, "Expected S_OK, got 0x%08x\n", hr);
1753 ok(token, "Expected token != 0\n");
1754 todo_wine ok(ctx == (IObjContext *)token, "Expected interface pointers to be the same\n");
1756 refs = IUnknown_AddRef((IUnknown *)ctx);
1757 ok(refs == 2, "Expected 1, got %u\n", refs);
1759 refs = IUnknown_Release((IUnknown *)ctx);
1760 ok(refs == 1, "Expected 0, got %u\n", refs);
1762 refs = IUnknown_Release((IUnknown *)ctx);
1763 ok(refs == 0, "Expected 0, got %u\n", refs);
1765 CoUninitialize();
1768 static void test_CoGetTreatAsClass(void)
1770 HRESULT hr;
1771 CLSID out;
1772 static GUID deadbeef = {0xdeadbeef,0xdead,0xbeef,{0xde,0xad,0xbe,0xef,0xde,0xad,0xbe,0xef}};
1774 if (!pCoGetTreatAsClass)
1776 win_skip("CoGetTreatAsClass not present\n");
1777 return;
1779 hr = pCoGetTreatAsClass(&deadbeef,&out);
1780 ok (hr == S_FALSE, "expected S_FALSE got %x\n",hr);
1781 ok (IsEqualGUID(&out,&deadbeef), "expected to get same clsid back\n");
1784 static void test_CoInitializeEx(void)
1786 HRESULT hr;
1788 hr = pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1789 ok(hr == S_OK, "CoInitializeEx failed with error 0x%08x\n", hr);
1791 /* Calling OleInitialize for the first time should yield S_OK even with
1792 * apartment already initialized by previous CoInitialize(Ex) calls. */
1793 hr = OleInitialize(NULL);
1794 ok(hr == S_OK, "OleInitialize failed with error 0x%08x\n", hr);
1796 /* Subsequent calls to OleInitialize should return S_FALSE */
1797 hr = OleInitialize(NULL);
1798 ok(hr == S_FALSE, "Expected S_FALSE, hr = 0x%08x\n", hr);
1800 /* Cleanup */
1801 CoUninitialize();
1802 OleUninitialize();
1805 static void test_OleRegGetMiscStatus(void)
1807 ULONG_PTR cookie;
1808 HANDLE handle;
1809 DWORD status;
1810 HRESULT hr;
1812 hr = OleRegGetMiscStatus(&CLSID_Testclass, DVASPECT_ICON, NULL);
1813 ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
1815 status = 0xdeadbeef;
1816 hr = OleRegGetMiscStatus(&CLSID_Testclass, DVASPECT_ICON, &status);
1817 ok(hr == REGDB_E_CLASSNOTREG, "got 0x%08x\n", hr);
1818 ok(status == 0, "got 0x%08x\n", status);
1820 status = -1;
1821 hr = OleRegGetMiscStatus(&CLSID_StdFont, DVASPECT_ICON, &status);
1822 ok(hr == S_OK, "got 0x%08x\n", hr);
1823 ok(status == 0, "got 0x%08x\n", status);
1825 if ((handle = activate_context(actctx_manifest, &cookie)))
1827 status = 0;
1828 hr = OleRegGetMiscStatus(&CLSID_Testclass, DVASPECT_ICON, &status);
1829 todo_wine {
1830 ok(hr == S_OK, "got 0x%08x\n", hr);
1831 ok(status == OLEMISC_RECOMPOSEONRESIZE, "got 0x%08x\n", status);
1833 /* context data takes precedence over registration info */
1834 status = 0;
1835 hr = OleRegGetMiscStatus(&CLSID_StdFont, DVASPECT_ICON, &status);
1836 ok(hr == S_OK, "got 0x%08x\n", hr);
1837 todo_wine
1838 ok(status == OLEMISC_RECOMPOSEONRESIZE, "got 0x%08x\n", status);
1840 /* there's no such attribute in context */
1841 status = -1;
1842 hr = OleRegGetMiscStatus(&CLSID_Testclass, DVASPECT_DOCPRINT, &status);
1843 todo_wine
1844 ok(hr == S_OK, "got 0x%08x\n", hr);
1845 ok(status == 0, "got 0x%08x\n", status);
1847 pDeactivateActCtx(0, cookie);
1848 pReleaseActCtx(handle);
1852 static void init_funcs(void)
1854 HMODULE hOle32 = GetModuleHandleA("ole32");
1855 HMODULE hAdvapi32 = GetModuleHandleA("advapi32");
1856 HMODULE hkernel32 = GetModuleHandleA("kernel32");
1858 pCoGetObjectContext = (void*)GetProcAddress(hOle32, "CoGetObjectContext");
1859 pCoSwitchCallContext = (void*)GetProcAddress(hOle32, "CoSwitchCallContext");
1860 pCoGetTreatAsClass = (void*)GetProcAddress(hOle32,"CoGetTreatAsClass");
1861 pCoGetContextToken = (void*)GetProcAddress(hOle32, "CoGetContextToken");
1862 pRegOverridePredefKey = (void*)GetProcAddress(hAdvapi32, "RegOverridePredefKey");
1863 pCoInitializeEx = (void*)GetProcAddress(hOle32, "CoInitializeEx");
1865 pActivateActCtx = (void*)GetProcAddress(hkernel32, "ActivateActCtx");
1866 pCreateActCtxW = (void*)GetProcAddress(hkernel32, "CreateActCtxW");
1867 pDeactivateActCtx = (void*)GetProcAddress(hkernel32, "DeactivateActCtx");
1868 pReleaseActCtx = (void*)GetProcAddress(hkernel32, "ReleaseActCtx");
1871 START_TEST(compobj)
1873 init_funcs();
1875 if (!pCoInitializeEx)
1877 trace("You need DCOM95 installed to run this test\n");
1878 return;
1881 test_ProgIDFromCLSID();
1882 test_CLSIDFromProgID();
1883 test_CLSIDFromString();
1884 test_StringFromGUID2();
1885 test_CoCreateInstance();
1886 test_ole_menu();
1887 test_CoGetClassObject();
1888 test_CoRegisterMessageFilter();
1889 test_CoRegisterPSClsid();
1890 test_CoGetPSClsid();
1891 test_CoUnmarshalInterface();
1892 test_CoGetInterfaceAndReleaseStream();
1893 test_CoMarshalInterface();
1894 test_CoMarshalInterThreadInterfaceInStream();
1895 test_CoRegisterClassObject();
1896 test_registered_object_thread_affinity();
1897 test_CoFreeUnusedLibraries();
1898 test_CoGetObjectContext();
1899 test_CoGetCallContext();
1900 test_CoGetContextToken();
1901 test_CoGetTreatAsClass();
1902 test_CoInitializeEx();
1903 test_OleRegGetMiscStatus();