ole32: Don't call spy on IMalloc::Free(NULL).
[wine.git] / dlls / ole32 / tests / compobj.c
blob39beee4c35fd429ac130574ecfae95eec35c7c12
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 */
34 #include "dde.h"
36 #include "ctxtcall.h"
38 #include "wine/test.h"
40 #define DEFINE_EXPECT(func) \
41 static BOOL expect_ ## func = FALSE, called_ ## func = FALSE
43 #define SET_EXPECT(func) \
44 expect_ ## func = TRUE
46 #define CHECK_EXPECT2(func) \
47 do { \
48 ok(expect_ ##func, "unexpected call " #func "\n"); \
49 called_ ## func = TRUE; \
50 }while(0)
52 #define CHECK_EXPECT(func) \
53 do { \
54 CHECK_EXPECT2(func); \
55 expect_ ## func = FALSE; \
56 }while(0)
58 #define CHECK_CALLED(func) \
59 do { \
60 ok(called_ ## func, "expected " #func "\n"); \
61 expect_ ## func = called_ ## func = FALSE; \
62 }while(0)
64 DEFINE_EXPECT(CreateStub);
66 /* functions that are not present on all versions of Windows */
67 static HRESULT (WINAPI * pCoInitializeEx)(LPVOID lpReserved, DWORD dwCoInit);
68 static HRESULT (WINAPI * pCoGetObjectContext)(REFIID riid, LPVOID *ppv);
69 static HRESULT (WINAPI * pCoSwitchCallContext)(IUnknown *pObject, IUnknown **ppOldObject);
70 static HRESULT (WINAPI * pCoGetTreatAsClass)(REFCLSID clsidOld, LPCLSID pClsidNew);
71 static HRESULT (WINAPI * pCoTreatAsClass)(REFCLSID clsidOld, REFCLSID pClsidNew);
72 static HRESULT (WINAPI * pCoGetContextToken)(ULONG_PTR *token);
73 static HRESULT (WINAPI * pCoGetApartmentType)(APTTYPE *type, APTTYPEQUALIFIER *qualifier);
74 static LONG (WINAPI * pRegDeleteKeyExA)(HKEY, LPCSTR, REGSAM, DWORD);
75 static LONG (WINAPI * pRegOverridePredefKey)(HKEY key, HKEY override);
77 static BOOL (WINAPI *pActivateActCtx)(HANDLE,ULONG_PTR*);
78 static HANDLE (WINAPI *pCreateActCtxW)(PCACTCTXW);
79 static BOOL (WINAPI *pDeactivateActCtx)(DWORD,ULONG_PTR);
80 static BOOL (WINAPI *pIsWow64Process)(HANDLE, LPBOOL);
81 static void (WINAPI *pReleaseActCtx)(HANDLE);
83 #define ok_ole_success(hr, func) ok(hr == S_OK, func " failed with error 0x%08x\n", hr)
84 #define ok_more_than_one_lock() ok(cLocks > 0, "Number of locks should be > 0, but actually is %d\n", cLocks)
85 #define ok_no_locks() ok(cLocks == 0, "Number of locks should be 0, but actually is %d\n", cLocks)
87 static const CLSID CLSID_non_existent = { 0x12345678, 0x1234, 0x1234, { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 } };
88 static const CLSID CLSID_StdFont = { 0x0be35203, 0x8f91, 0x11ce, { 0x9d, 0xe3, 0x00, 0xaa, 0x00, 0x4b, 0xb8, 0x51 } };
89 static const GUID IID_Testiface = { 0x22222222, 0x1234, 0x1234, { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 } };
90 static const GUID IID_Testiface2 = { 0x32222222, 0x1234, 0x1234, { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 } };
91 static const GUID IID_Testiface3 = { 0x42222222, 0x1234, 0x1234, { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 } };
92 static const GUID IID_Testiface4 = { 0x52222222, 0x1234, 0x1234, { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 } };
93 static const GUID IID_Testiface5 = { 0x62222222, 0x1234, 0x1234, { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 } };
94 static const GUID IID_Testiface6 = { 0x72222222, 0x1234, 0x1234, { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 } };
95 static const GUID IID_TestPS = { 0x66666666, 0x8888, 0x7777, { 0x66, 0x66, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55 } };
97 DEFINE_GUID(CLSID_InProcFreeMarshaler, 0x0000033a,0x0000,0x0000,0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46);
99 static const WCHAR stdfont[] = {'S','t','d','F','o','n','t',0};
100 static const WCHAR wszNonExistent[] = {'N','o','n','E','x','i','s','t','e','n','t',0};
101 static const WCHAR wszCLSID_StdFont[] =
103 '{','0','b','e','3','5','2','0','3','-','8','f','9','1','-','1','1','c','e','-',
104 '9','d','e','3','-','0','0','a','a','0','0','4','b','b','8','5','1','}',0
106 static const WCHAR progidW[] = {'P','r','o','g','I','d','.','P','r','o','g','I','d',0};
107 static const WCHAR cf_brokenW[] = {'{','0','0','0','0','0','0','0','1','-','0','0','0','0','-','0','0','0','0','-',
108 'c','0','0','0','-','0','0','0','0','0','0','0','0','0','0','4','6','}','a',0};
110 DEFINE_GUID(IID_IWineTest, 0x5201163f, 0x8164, 0x4fd0, 0xa1, 0xa2, 0x5d, 0x5a, 0x36, 0x54, 0xd3, 0xbd);
111 DEFINE_GUID(CLSID_WineOOPTest, 0x5201163f, 0x8164, 0x4fd0, 0xa1, 0xa2, 0x5d, 0x5a, 0x36, 0x54, 0xd3, 0xbd);
113 static LONG cLocks;
115 static void LockModule(void)
117 InterlockedIncrement(&cLocks);
120 static void UnlockModule(void)
122 InterlockedDecrement(&cLocks);
125 static HRESULT WINAPI Test_IClassFactory_QueryInterface(
126 LPCLASSFACTORY iface,
127 REFIID riid,
128 LPVOID *ppvObj)
130 if (ppvObj == NULL) return E_POINTER;
132 if (IsEqualGUID(riid, &IID_IUnknown) ||
133 IsEqualGUID(riid, &IID_IClassFactory))
135 *ppvObj = iface;
136 IClassFactory_AddRef(iface);
137 return S_OK;
140 *ppvObj = NULL;
141 return E_NOINTERFACE;
144 static ULONG WINAPI Test_IClassFactory_AddRef(LPCLASSFACTORY iface)
146 LockModule();
147 return 2; /* non-heap-based object */
150 static ULONG WINAPI Test_IClassFactory_Release(LPCLASSFACTORY iface)
152 UnlockModule();
153 return 1; /* non-heap-based object */
156 static IID create_instance_iid;
157 static HRESULT WINAPI Test_IClassFactory_CreateInstance(
158 LPCLASSFACTORY iface,
159 IUnknown *pUnkOuter,
160 REFIID riid,
161 LPVOID *ppvObj)
163 *ppvObj = NULL;
164 create_instance_iid = *riid;
165 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
166 return E_NOINTERFACE;
169 static HRESULT WINAPI Test_IClassFactory_LockServer(
170 LPCLASSFACTORY iface,
171 BOOL fLock)
173 return S_OK;
176 static const IClassFactoryVtbl TestClassFactory_Vtbl =
178 Test_IClassFactory_QueryInterface,
179 Test_IClassFactory_AddRef,
180 Test_IClassFactory_Release,
181 Test_IClassFactory_CreateInstance,
182 Test_IClassFactory_LockServer
185 static IClassFactory Test_ClassFactory = { &TestClassFactory_Vtbl };
187 static WCHAR manifest_path[MAX_PATH];
189 static BOOL create_manifest_file(const char *filename, const char *manifest)
191 int manifest_len;
192 DWORD size;
193 HANDLE file;
194 WCHAR path[MAX_PATH];
196 MultiByteToWideChar( CP_ACP, 0, filename, -1, path, MAX_PATH );
197 GetFullPathNameW(path, sizeof(manifest_path)/sizeof(WCHAR), manifest_path, NULL);
199 manifest_len = strlen(manifest);
200 file = CreateFileW(path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
201 FILE_ATTRIBUTE_NORMAL, NULL);
202 ok(file != INVALID_HANDLE_VALUE, "CreateFile failed: %u\n", GetLastError());
203 if(file == INVALID_HANDLE_VALUE)
204 return FALSE;
205 WriteFile(file, manifest, manifest_len, &size, NULL);
206 CloseHandle(file);
208 return TRUE;
211 static HANDLE activate_context(const char *manifest, ULONG_PTR *cookie)
213 WCHAR path[MAX_PATH];
214 ACTCTXW actctx;
215 HANDLE handle;
216 BOOL ret;
218 if (!pCreateActCtxW) return NULL;
220 create_manifest_file("file.manifest", manifest);
222 MultiByteToWideChar( CP_ACP, 0, "file.manifest", -1, path, MAX_PATH );
223 memset(&actctx, 0, sizeof(ACTCTXW));
224 actctx.cbSize = sizeof(ACTCTXW);
225 actctx.lpSource = path;
227 handle = pCreateActCtxW(&actctx);
228 ok(handle != INVALID_HANDLE_VALUE || broken(handle == INVALID_HANDLE_VALUE) /* some old XP/2k3 versions */,
229 "handle == INVALID_HANDLE_VALUE, error %u\n", GetLastError());
230 if (handle == INVALID_HANDLE_VALUE)
232 win_skip("activation context generation failed, some tests will be skipped\n");
233 handle = NULL;
236 ok(actctx.cbSize == sizeof(ACTCTXW), "actctx.cbSize=%d\n", actctx.cbSize);
237 ok(actctx.dwFlags == 0, "actctx.dwFlags=%d\n", actctx.dwFlags);
238 ok(actctx.lpSource == path, "actctx.lpSource=%p\n", actctx.lpSource);
239 ok(actctx.wProcessorArchitecture == 0, "actctx.wProcessorArchitecture=%d\n", actctx.wProcessorArchitecture);
240 ok(actctx.wLangId == 0, "actctx.wLangId=%d\n", actctx.wLangId);
241 ok(actctx.lpAssemblyDirectory == NULL, "actctx.lpAssemblyDirectory=%p\n", actctx.lpAssemblyDirectory);
242 ok(actctx.lpResourceName == NULL, "actctx.lpResourceName=%p\n", actctx.lpResourceName);
243 ok(actctx.lpApplicationName == NULL, "actctx.lpApplicationName=%p\n", actctx.lpApplicationName);
244 ok(actctx.hModule == NULL, "actctx.hModule=%p\n", actctx.hModule);
246 DeleteFileA("file.manifest");
248 if (handle)
250 ret = pActivateActCtx(handle, cookie);
251 ok(ret, "ActivateActCtx failed: %u\n", GetLastError());
254 return handle;
257 static const char actctx_manifest[] =
258 "<assembly xmlns=\"urn:schemas-microsoft-com:asm.v1\" manifestVersion=\"1.0\">"
259 "<assemblyIdentity version=\"1.2.3.4\" name=\"Wine.Test\" type=\"win32\""
260 " publicKeyToken=\"6595b6414666f1df\" />"
261 "<file name=\"testlib.dll\">"
262 " <comClass"
263 " clsid=\"{0000033a-0000-0000-c000-000000000046}\""
264 " progid=\"FTMarshal\""
265 " />"
266 " <comClass"
267 " clsid=\"{5201163f-8164-4fd0-a1a2-5d5a3654d3bd}\""
268 " progid=\"WineOOPTest\""
269 " />"
270 " <comClass description=\"Test com class\""
271 " clsid=\"{12345678-1234-1234-1234-56789abcdef0}\""
272 " progid=\"ProgId.ProgId\""
273 " miscStatusIcon=\"recomposeonresize\""
274 " />"
275 " <comClass description=\"CustomFont Description\" clsid=\"{0be35203-8f91-11ce-9de3-00aa004bb851}\""
276 " progid=\"CustomFont\""
277 " miscStatusIcon=\"recomposeonresize\""
278 " miscStatusContent=\"insideout\""
279 " />"
280 " <comClass description=\"StdFont Description\" clsid=\"{0be35203-8f91-11ce-9de3-00aa004bb852}\""
281 " progid=\"StdFont\""
282 " />"
283 " <comClass clsid=\"{62222222-1234-1234-1234-56789abcdef0}\" >"
284 " <progid>ProgId.ProgId.1</progid>"
285 " </comClass>"
286 " <comInterfaceProxyStub "
287 " name=\"Iifaceps\""
288 " iid=\"{22222222-1234-1234-1234-56789abcdef0}\""
289 " proxyStubClsid32=\"{66666666-8888-7777-6666-555555555555}\""
290 " />"
291 "</file>"
292 " <comInterfaceExternalProxyStub "
293 " name=\"Iifaceps2\""
294 " iid=\"{32222222-1234-1234-1234-56789abcdef0}\""
295 " />"
296 " <comInterfaceExternalProxyStub "
297 " name=\"Iifaceps3\""
298 " iid=\"{42222222-1234-1234-1234-56789abcdef0}\""
299 " proxyStubClsid32=\"{66666666-8888-7777-6666-555555555555}\""
300 " />"
301 " <comInterfaceExternalProxyStub "
302 " name=\"Iifaceps4\""
303 " iid=\"{52222222-1234-1234-1234-56789abcdef0}\""
304 " proxyStubClsid32=\"{00000000-0000-0000-0000-000000000000}\""
305 " />"
306 " <clrClass "
307 " clsid=\"{72222222-1234-1234-1234-56789abcdef0}\""
308 " name=\"clrclass\""
309 " >"
310 " <progid>clrprogid.1</progid>"
311 " </clrClass>"
312 "</assembly>";
314 DEFINE_GUID(CLSID_Testclass, 0x12345678, 0x1234, 0x1234, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0);
316 static void test_ProgIDFromCLSID(void)
318 ULONG_PTR cookie = 0;
319 LPWSTR progid;
320 HANDLE handle;
321 HRESULT hr;
323 hr = ProgIDFromCLSID(&CLSID_StdFont, &progid);
324 ok(hr == S_OK, "ProgIDFromCLSID failed with error 0x%08x\n", hr);
325 if (hr == S_OK)
327 ok(!lstrcmpiW(progid, stdfont), "Didn't get expected prog ID\n");
328 CoTaskMemFree(progid);
331 progid = (LPWSTR)0xdeadbeef;
332 hr = ProgIDFromCLSID(&CLSID_non_existent, &progid);
333 ok(hr == REGDB_E_CLASSNOTREG, "ProgIDFromCLSID returned %08x\n", hr);
334 ok(progid == NULL, "ProgIDFromCLSID returns with progid %p\n", progid);
336 hr = ProgIDFromCLSID(&CLSID_StdFont, NULL);
337 ok(hr == E_INVALIDARG, "ProgIDFromCLSID should return E_INVALIDARG instead of 0x%08x\n", hr);
339 if ((handle = activate_context(actctx_manifest, &cookie)))
341 static const WCHAR customfontW[] = {'C','u','s','t','o','m','F','o','n','t',0};
343 hr = ProgIDFromCLSID(&CLSID_non_existent, &progid);
344 ok(hr == S_OK, "got 0x%08x\n", hr);
345 ok(!lstrcmpiW(progid, progidW), "got %s\n", wine_dbgstr_w(progid));
346 CoTaskMemFree(progid);
348 /* try something registered and redirected */
349 progid = NULL;
350 hr = ProgIDFromCLSID(&CLSID_StdFont, &progid);
351 ok(hr == S_OK, "got 0x%08x\n", hr);
352 ok(!lstrcmpiW(progid, customfontW), "got wrong progid %s\n", wine_dbgstr_w(progid));
353 CoTaskMemFree(progid);
355 /* classes without default progid, progid list is not used */
356 hr = ProgIDFromCLSID(&IID_Testiface5, &progid);
357 ok(hr == REGDB_E_CLASSNOTREG, "got 0x%08x\n", hr);
359 hr = ProgIDFromCLSID(&IID_Testiface6, &progid);
360 ok(hr == REGDB_E_CLASSNOTREG, "got 0x%08x\n", hr);
362 pDeactivateActCtx(0, cookie);
363 pReleaseActCtx(handle);
367 static void test_CLSIDFromProgID(void)
369 ULONG_PTR cookie = 0;
370 HANDLE handle;
371 CLSID clsid;
372 HRESULT hr = CLSIDFromProgID(stdfont, &clsid);
373 ok(hr == S_OK, "CLSIDFromProgID failed with error 0x%08x\n", hr);
374 ok(IsEqualCLSID(&clsid, &CLSID_StdFont), "clsid wasn't equal to CLSID_StdFont\n");
376 hr = CLSIDFromString(stdfont, &clsid);
377 ok_ole_success(hr, "CLSIDFromString");
378 ok(IsEqualCLSID(&clsid, &CLSID_StdFont), "clsid wasn't equal to CLSID_StdFont\n");
380 /* test some failure cases */
382 hr = CLSIDFromProgID(wszNonExistent, NULL);
383 ok(hr == E_INVALIDARG, "CLSIDFromProgID should have returned E_INVALIDARG instead of 0x%08x\n", hr);
385 hr = CLSIDFromProgID(NULL, &clsid);
386 ok(hr == E_INVALIDARG, "CLSIDFromProgID should have returned E_INVALIDARG instead of 0x%08x\n", hr);
388 memset(&clsid, 0xcc, sizeof(clsid));
389 hr = CLSIDFromProgID(wszNonExistent, &clsid);
390 ok(hr == CO_E_CLASSSTRING, "CLSIDFromProgID on nonexistent ProgID should have returned CO_E_CLASSSTRING instead of 0x%08x\n", hr);
391 ok(IsEqualCLSID(&clsid, &CLSID_NULL), "CLSIDFromProgID should have set clsid to all-zeros on failure\n");
393 /* fails without proper context */
394 memset(&clsid, 0xcc, sizeof(clsid));
395 hr = CLSIDFromProgID(progidW, &clsid);
396 ok(hr == CO_E_CLASSSTRING, "got 0x%08x\n", hr);
397 ok(IsEqualCLSID(&clsid, &CLSID_NULL), "wrong clsid\n");
399 if ((handle = activate_context(actctx_manifest, &cookie)))
401 GUID clsid1;
403 memset(&clsid, 0xcc, sizeof(clsid));
404 hr = CLSIDFromProgID(wszNonExistent, &clsid);
405 ok(hr == CO_E_CLASSSTRING, "got 0x%08x\n", hr);
406 ok(IsEqualCLSID(&clsid, &CLSID_NULL), "should have zero CLSID on failure\n");
408 /* CLSIDFromString() doesn't check activation context */
409 hr = CLSIDFromString(progidW, &clsid);
410 ok(hr == CO_E_CLASSSTRING, "got 0x%08x\n", hr);
412 clsid = CLSID_NULL;
413 hr = CLSIDFromProgID(progidW, &clsid);
414 ok(hr == S_OK, "got 0x%08x\n", hr);
415 /* it returns generated CLSID here */
416 ok(!IsEqualCLSID(&clsid, &CLSID_non_existent) && !IsEqualCLSID(&clsid, &CLSID_NULL),
417 "got wrong clsid %s\n", wine_dbgstr_guid(&clsid));
419 /* duplicate progid present in context - returns generated guid here too */
420 clsid = CLSID_NULL;
421 hr = CLSIDFromProgID(stdfont, &clsid);
422 ok(hr == S_OK, "got 0x%08x\n", hr);
423 clsid1 = CLSID_StdFont;
424 /* that's where it differs from StdFont */
425 clsid1.Data4[7] = 0x52;
426 ok(!IsEqualCLSID(&clsid, &CLSID_StdFont) && !IsEqualCLSID(&clsid, &CLSID_NULL) && !IsEqualCLSID(&clsid, &clsid1),
427 "got %s\n", wine_dbgstr_guid(&clsid));
429 pDeactivateActCtx(0, cookie);
430 pReleaseActCtx(handle);
434 static void test_CLSIDFromString(void)
436 CLSID clsid;
437 WCHAR wszCLSID_Broken[50];
438 UINT i;
440 HRESULT hr = CLSIDFromString(wszCLSID_StdFont, &clsid);
441 ok_ole_success(hr, "CLSIDFromString");
442 ok(IsEqualCLSID(&clsid, &CLSID_StdFont), "clsid wasn't equal to CLSID_StdFont\n");
444 memset(&clsid, 0xab, sizeof(clsid));
445 hr = CLSIDFromString(NULL, &clsid);
446 ok(hr == S_OK, "got 0x%08x\n", hr);
447 ok(IsEqualCLSID(&clsid, &CLSID_NULL), "clsid wasn't equal to CLSID_NULL\n");
449 /* string is longer, but starts with a valid CLSID */
450 memset(&clsid, 0, sizeof(clsid));
451 hr = CLSIDFromString(cf_brokenW, &clsid);
452 ok(hr == CO_E_CLASSSTRING, "got 0x%08x\n", hr);
453 ok(IsEqualCLSID(&clsid, &IID_IClassFactory), "got %s\n", wine_dbgstr_guid(&clsid));
455 lstrcpyW(wszCLSID_Broken, wszCLSID_StdFont);
456 for(i = lstrlenW(wszCLSID_StdFont); i < 49; i++)
457 wszCLSID_Broken[i] = 'A';
458 wszCLSID_Broken[i] = '\0';
460 memset(&clsid, 0, sizeof(CLSID));
461 hr = CLSIDFromString(wszCLSID_Broken, &clsid);
462 ok(hr == CO_E_CLASSSTRING, "Got %08x\n", hr);
463 ok(IsEqualCLSID(&clsid, &CLSID_StdFont), "clsid wasn't equal to CLSID_StdFont\n");
465 wszCLSID_Broken[lstrlenW(wszCLSID_StdFont)-1] = 'A';
466 memset(&clsid, 0, sizeof(CLSID));
467 hr = CLSIDFromString(wszCLSID_Broken, &clsid);
468 ok(hr == CO_E_CLASSSTRING, "Got %08x\n", hr);
469 ok(IsEqualCLSID(&clsid, &CLSID_StdFont), "clsid wasn't equal to CLSID_StdFont\n");
471 wszCLSID_Broken[lstrlenW(wszCLSID_StdFont)] = '\0';
472 memset(&clsid, 0, sizeof(CLSID));
473 hr = CLSIDFromString(wszCLSID_Broken, &clsid);
474 ok(hr == CO_E_CLASSSTRING, "Got %08x\n", hr);
475 ok(IsEqualCLSID(&clsid, &CLSID_StdFont), "clsid wasn't equal to CLSID_StdFont\n");
477 wszCLSID_Broken[lstrlenW(wszCLSID_StdFont)-1] = '\0';
478 memset(&clsid, 0, sizeof(CLSID));
479 hr = CLSIDFromString(wszCLSID_Broken, &clsid);
480 ok(hr == CO_E_CLASSSTRING, "Got %08x\n", hr);
481 ok(IsEqualCLSID(&clsid, &CLSID_StdFont), "clsid wasn't equal to CLSID_StdFont\n");
483 memset(&clsid, 0xcc, sizeof(CLSID));
484 hr = CLSIDFromString(wszCLSID_Broken+1, &clsid);
485 ok(hr == CO_E_CLASSSTRING, "Got %08x\n", hr);
486 ok(IsEqualCLSID(&clsid, &CLSID_NULL), "clsid wasn't equal to CLSID_NULL\n");
488 wszCLSID_Broken[9] = '*';
489 memset(&clsid, 0xcc, sizeof(CLSID));
490 hr = CLSIDFromString(wszCLSID_Broken, &clsid);
491 ok(hr == CO_E_CLASSSTRING, "Got %08x\n", hr);
492 ok(clsid.Data1 == CLSID_StdFont.Data1, "Got %08x\n", clsid.Data1);
493 ok(clsid.Data2 == 0xcccc, "Got %04x\n", clsid.Data2);
495 wszCLSID_Broken[3] = '*';
496 memset(&clsid, 0xcc, sizeof(CLSID));
497 hr = CLSIDFromString(wszCLSID_Broken, &clsid);
498 ok(hr == CO_E_CLASSSTRING, "Got %08x\n", hr);
499 ok(clsid.Data1 == 0xb, "Got %08x\n", clsid.Data1);
500 ok(clsid.Data2 == 0xcccc, "Got %04x\n", clsid.Data2);
502 wszCLSID_Broken[3] = '\0';
503 memset(&clsid, 0xcc, sizeof(CLSID));
504 hr = CLSIDFromString(wszCLSID_Broken, &clsid);
505 ok(hr == CO_E_CLASSSTRING, "Got %08x\n", hr);
506 ok(clsid.Data1 == 0xb, "Got %08x\n", clsid.Data1);
507 ok(clsid.Data2 == 0xcccc, "Got %04x\n", clsid.Data2);
510 static void test_IIDFromString(void)
512 static const WCHAR cfW[] = {'{','0','0','0','0','0','0','0','1','-','0','0','0','0','-','0','0','0','0','-',
513 'c','0','0','0','-','0','0','0','0','0','0','0','0','0','0','4','6','}',0};
514 static const WCHAR brokenW[] = {'{','0','0','0','0','0','0','0','1','-','0','0','0','0','-','0','0','0','0','-',
515 'g','0','0','0','-','0','0','0','0','0','0','0','0','0','0','4','6','}',0};
516 static const WCHAR broken2W[] = {'{','0','0','0','0','0','0','0','1','=','0','0','0','0','-','0','0','0','0','-',
517 'g','0','0','0','-','0','0','0','0','0','0','0','0','0','0','4','6','}',0};
518 static const WCHAR broken3W[] = {'b','r','o','k','e','n','0','0','1','=','0','0','0','0','-','0','0','0','0','-',
519 'g','0','0','0','-','0','0','0','0','0','0','0','0','0','0','4','6','}',0};
520 HRESULT hr;
521 IID iid;
523 hr = IIDFromString(wszCLSID_StdFont, &iid);
524 ok(hr == S_OK, "got 0x%08x\n", hr);
525 ok(IsEqualIID(&iid, &CLSID_StdFont), "got iid %s\n", wine_dbgstr_guid(&iid));
527 memset(&iid, 0xab, sizeof(iid));
528 hr = IIDFromString(NULL, &iid);
529 ok(hr == S_OK, "got 0x%08x\n", hr);
530 ok(IsEqualIID(&iid, &CLSID_NULL), "got iid %s\n", wine_dbgstr_guid(&iid));
532 hr = IIDFromString(cfW, &iid);
533 ok(hr == S_OK, "got 0x%08x\n", hr);
534 ok(IsEqualIID(&iid, &IID_IClassFactory), "got iid %s\n", wine_dbgstr_guid(&iid));
536 /* string starts with a valid IID but is longer */
537 memset(&iid, 0xab, sizeof(iid));
538 hr = IIDFromString(cf_brokenW, &iid);
539 ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
540 ok(iid.Data1 == 0xabababab, "Got %08x\n", iid.Data1);
542 /* invalid IID in a valid format */
543 memset(&iid, 0xab, sizeof(iid));
544 hr = IIDFromString(brokenW, &iid);
545 ok(hr == CO_E_IIDSTRING, "got 0x%08x\n", hr);
546 ok(iid.Data1 == 0x00000001, "Got %08x\n", iid.Data1);
548 memset(&iid, 0xab, sizeof(iid));
549 hr = IIDFromString(broken2W, &iid);
550 ok(hr == CO_E_IIDSTRING, "got 0x%08x\n", hr);
551 ok(iid.Data1 == 0x00000001, "Got %08x\n", iid.Data1);
553 /* format is broken, but string length is okay */
554 memset(&iid, 0xab, sizeof(iid));
555 hr = IIDFromString(broken3W, &iid);
556 ok(hr == CO_E_IIDSTRING, "got 0x%08x\n", hr);
557 ok(iid.Data1 == 0xabababab, "Got %08x\n", iid.Data1);
559 /* invalid string */
560 memset(&iid, 0xab, sizeof(iid));
561 hr = IIDFromString(wszNonExistent, &iid);
562 ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
563 ok(iid.Data1 == 0xabababab, "Got %08x\n", iid.Data1);
565 /* valid ProgID */
566 memset(&iid, 0xab, sizeof(iid));
567 hr = IIDFromString(stdfont, &iid);
568 ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
569 ok(iid.Data1 == 0xabababab, "Got %08x\n", iid.Data1);
572 static void test_StringFromGUID2(void)
574 WCHAR str[50];
575 int len;
577 /* invalid pointer */
578 SetLastError(0xdeadbeef);
579 len = StringFromGUID2(NULL,str,50);
580 ok(len == 0, "len: %d (expected 0)\n", len);
581 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %x\n", GetLastError());
583 /* Test corner cases for buffer size */
584 len = StringFromGUID2(&CLSID_StdFont,str,50);
585 ok(len == 39, "len: %d (expected 39)\n", len);
586 ok(!lstrcmpiW(str, wszCLSID_StdFont),"string wasn't equal for CLSID_StdFont\n");
588 memset(str,0,sizeof str);
589 len = StringFromGUID2(&CLSID_StdFont,str,39);
590 ok(len == 39, "len: %d (expected 39)\n", len);
591 ok(!lstrcmpiW(str, wszCLSID_StdFont),"string wasn't equal for CLSID_StdFont\n");
593 len = StringFromGUID2(&CLSID_StdFont,str,38);
594 ok(len == 0, "len: %d (expected 0)\n", len);
596 len = StringFromGUID2(&CLSID_StdFont,str,30);
597 ok(len == 0, "len: %d (expected 0)\n", len);
600 struct info
602 HANDLE wait, stop;
605 static DWORD CALLBACK ole_initialize_thread(LPVOID pv)
607 HRESULT hr;
608 struct info *info = pv;
610 hr = pCoInitializeEx(NULL, COINIT_MULTITHREADED);
612 SetEvent(info->wait);
613 WaitForSingleObject(info->stop, 10000);
615 CoUninitialize();
616 return hr;
619 static void test_CoCreateInstance(void)
621 HRESULT hr;
622 HANDLE thread;
623 DWORD tid, exitcode;
624 IUnknown *pUnk;
625 struct info info;
626 REFCLSID rclsid = &CLSID_InternetZoneManager;
628 pUnk = (IUnknown *)0xdeadbeef;
629 hr = CoCreateInstance(rclsid, NULL, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void **)&pUnk);
630 ok(hr == CO_E_NOTINITIALIZED, "CoCreateInstance should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n", hr);
631 ok(pUnk == NULL, "CoCreateInstance should have changed the passed in pointer to NULL, instead of %p\n", pUnk);
633 OleInitialize(NULL);
635 /* test errors returned for non-registered clsids */
636 hr = CoCreateInstance(&CLSID_non_existent, NULL, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void **)&pUnk);
637 ok(hr == REGDB_E_CLASSNOTREG, "CoCreateInstance for non-registered inproc server should have returned REGDB_E_CLASSNOTREG instead of 0x%08x\n", hr);
638 hr = CoCreateInstance(&CLSID_non_existent, NULL, CLSCTX_INPROC_HANDLER, &IID_IUnknown, (void **)&pUnk);
639 ok(hr == REGDB_E_CLASSNOTREG, "CoCreateInstance for non-registered inproc handler should have returned REGDB_E_CLASSNOTREG instead of 0x%08x\n", hr);
640 hr = CoCreateInstance(&CLSID_non_existent, NULL, CLSCTX_LOCAL_SERVER, &IID_IUnknown, (void **)&pUnk);
641 ok(hr == REGDB_E_CLASSNOTREG, "CoCreateInstance for non-registered local server should have returned REGDB_E_CLASSNOTREG instead of 0x%08x\n", hr);
642 hr = CoCreateInstance(&CLSID_non_existent, NULL, CLSCTX_REMOTE_SERVER, &IID_IUnknown, (void **)&pUnk);
643 ok(hr == REGDB_E_CLASSNOTREG, "CoCreateInstance for non-registered remote server should have returned REGDB_E_CLASSNOTREG instead of 0x%08x\n", hr);
645 hr = CoCreateInstance(rclsid, NULL, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void **)&pUnk);
646 if(hr == REGDB_E_CLASSNOTREG)
648 skip("IE not installed so can't test CoCreateInstance\n");
649 OleUninitialize();
650 return;
653 ok_ole_success(hr, "CoCreateInstance");
654 if(pUnk) IUnknown_Release(pUnk);
655 OleUninitialize();
657 hr = CoCreateInstance(rclsid, NULL, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void **)&pUnk);
658 ok(hr == CO_E_NOTINITIALIZED, "CoCreateInstance should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n", hr);
660 /* show that COM doesn't have to be initialized for multi-threaded apartments if another
661 thread has already done so */
663 info.wait = CreateEventA(NULL, TRUE, FALSE, NULL);
664 ok(info.wait != NULL, "CreateEvent failed with error %d\n", GetLastError());
666 info.stop = CreateEventA(NULL, TRUE, FALSE, NULL);
667 ok(info.stop != NULL, "CreateEvent failed with error %d\n", GetLastError());
669 thread = CreateThread(NULL, 0, ole_initialize_thread, &info, 0, &tid);
670 ok(thread != NULL, "CreateThread failed with error %d\n", GetLastError());
672 ok( !WaitForSingleObject(info.wait, 10000 ), "wait timed out\n" );
674 pUnk = (IUnknown *)0xdeadbeef;
675 hr = CoCreateInstance(rclsid, NULL, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void **)&pUnk);
676 ok(hr == S_OK, "CoCreateInstance should have returned S_OK instead of 0x%08x\n", hr);
677 if (pUnk) IUnknown_Release(pUnk);
679 SetEvent(info.stop);
680 ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
682 GetExitCodeThread(thread, &exitcode);
683 hr = exitcode;
684 ok(hr == S_OK, "thread should have returned S_OK instead of 0x%08x\n", hr);
686 CloseHandle(thread);
687 CloseHandle(info.wait);
688 CloseHandle(info.stop);
691 static void test_CoGetClassObject(void)
693 HRESULT hr;
694 HANDLE thread, handle;
695 DWORD tid, exitcode;
696 ULONG_PTR cookie;
697 IUnknown *pUnk;
698 struct info info;
699 REFCLSID rclsid = &CLSID_InternetZoneManager;
700 HKEY hkey;
701 LONG res;
703 hr = CoGetClassObject(rclsid, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void **)&pUnk);
704 ok(hr == CO_E_NOTINITIALIZED, "CoGetClassObject should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n", hr);
705 ok(pUnk == NULL, "CoGetClassObject should have changed the passed in pointer to NULL, instead of %p\n", pUnk);
707 hr = CoGetClassObject(rclsid, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, NULL);
708 ok(hr == E_INVALIDARG ||
709 broken(hr == CO_E_NOTINITIALIZED), /* win9x */
710 "CoGetClassObject should have returned E_INVALIDARG instead of 0x%08x\n", hr);
712 /* show that COM doesn't have to be initialized for multi-threaded apartments if another
713 thread has already done so */
715 info.wait = CreateEventA(NULL, TRUE, FALSE, NULL);
716 ok(info.wait != NULL, "CreateEvent failed with error %d\n", GetLastError());
718 info.stop = CreateEventA(NULL, TRUE, FALSE, NULL);
719 ok(info.stop != NULL, "CreateEvent failed with error %d\n", GetLastError());
721 thread = CreateThread(NULL, 0, ole_initialize_thread, &info, 0, &tid);
722 ok(thread != NULL, "CreateThread failed with error %d\n", GetLastError());
724 ok( !WaitForSingleObject(info.wait, 10000), "wait timed out\n" );
726 pUnk = (IUnknown *)0xdeadbeef;
727 hr = CoGetClassObject(rclsid, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void **)&pUnk);
728 if(hr == REGDB_E_CLASSNOTREG)
729 skip("IE not installed so can't test CoGetClassObject\n");
730 else
732 ok(hr == S_OK, "CoGetClassObject should have returned S_OK instead of 0x%08x\n", hr);
733 if (pUnk) IUnknown_Release(pUnk);
736 SetEvent(info.stop);
737 ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
739 GetExitCodeThread(thread, &exitcode);
740 hr = exitcode;
741 ok(hr == S_OK, "thread should have returned S_OK instead of 0x%08x\n", hr);
743 CloseHandle(thread);
744 CloseHandle(info.wait);
745 CloseHandle(info.stop);
747 if (!pRegOverridePredefKey)
749 win_skip("RegOverridePredefKey not available\n");
750 return;
753 pCoInitializeEx(NULL, COINIT_MULTITHREADED);
755 hr = CoGetClassObject(rclsid, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void **)&pUnk);
756 if (hr == S_OK)
758 IUnknown_Release(pUnk);
760 res = RegCreateKeyExA(HKEY_CURRENT_USER, "Software\\Classes", 0, NULL, 0,
761 KEY_ALL_ACCESS, NULL, &hkey, NULL);
762 ok(!res, "RegCreateKeyEx returned %d\n", res);
764 res = pRegOverridePredefKey(HKEY_CLASSES_ROOT, hkey);
765 ok(!res, "RegOverridePredefKey returned %d\n", res);
767 hr = CoGetClassObject(rclsid, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void **)&pUnk);
768 ok(hr == S_OK, "CoGetClassObject should have returned S_OK instead of 0x%08x\n", hr);
770 res = pRegOverridePredefKey(HKEY_CLASSES_ROOT, NULL);
771 ok(!res, "RegOverridePredefKey returned %d\n", res);
773 if (hr == S_OK) IUnknown_Release(pUnk);
774 RegCloseKey(hkey);
777 hr = CoGetClassObject(&CLSID_InProcFreeMarshaler, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void **)&pUnk);
778 ok(hr == S_OK, "got 0x%08x\n", hr);
779 IUnknown_Release(pUnk);
781 /* context redefines FreeMarshaler CLSID */
782 if ((handle = activate_context(actctx_manifest, &cookie)))
784 hr = CoGetClassObject(&CLSID_InProcFreeMarshaler, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void **)&pUnk);
785 ok(hr == S_OK, "got 0x%08x\n", hr);
786 IUnknown_Release(pUnk);
788 pDeactivateActCtx(0, cookie);
789 pReleaseActCtx(handle);
792 CoUninitialize();
795 static void test_CoCreateInstanceEx(void)
797 MULTI_QI qi_res = { &IID_IMoniker };
798 DWORD cookie;
799 HRESULT hr;
801 CoInitialize(NULL);
803 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
804 CLSCTX_INPROC_SERVER, REGCLS_MULTIPLEUSE, &cookie);
805 ok_ole_success(hr, "CoRegisterClassObject");
807 create_instance_iid = IID_NULL;
808 hr = CoCreateInstanceEx(&CLSID_WineOOPTest, NULL, CLSCTX_INPROC_SERVER, NULL, 1, &qi_res);
809 ok(hr == E_NOINTERFACE, "CoCreateInstanceEx failed: %08x\n", hr);
810 ok(IsEqualGUID(&create_instance_iid, qi_res.pIID), "Unexpected CreateInstance iid %s\n",
811 wine_dbgstr_guid(&create_instance_iid));
813 hr = CoRevokeClassObject(cookie);
814 ok_ole_success(hr, "CoRevokeClassObject");
816 CoUninitialize();
819 static ATOM register_dummy_class(void)
821 WNDCLASSA wc =
824 DefWindowProcA,
827 GetModuleHandleA(NULL),
828 NULL,
829 LoadCursorA(NULL, (LPSTR)IDC_ARROW),
830 (HBRUSH)(COLOR_BTNFACE+1),
831 NULL,
832 "WineOleTestClass",
835 return RegisterClassA(&wc);
838 static void test_ole_menu(void)
840 HWND hwndFrame;
841 HRESULT hr;
843 hwndFrame = CreateWindowA((LPCSTR)MAKEINTATOM(register_dummy_class()), "Test", 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, NULL, NULL);
844 hr = OleSetMenuDescriptor(NULL, hwndFrame, NULL, NULL, NULL);
845 todo_wine ok_ole_success(hr, "OleSetMenuDescriptor");
847 DestroyWindow(hwndFrame);
851 static HRESULT WINAPI MessageFilter_QueryInterface(IMessageFilter *iface, REFIID riid, void ** ppvObj)
853 if (ppvObj == NULL) return E_POINTER;
855 if (IsEqualGUID(riid, &IID_IUnknown) ||
856 IsEqualGUID(riid, &IID_IClassFactory))
858 *ppvObj = iface;
859 IMessageFilter_AddRef(iface);
860 return S_OK;
863 return E_NOINTERFACE;
866 static ULONG WINAPI MessageFilter_AddRef(IMessageFilter *iface)
868 return 2; /* non-heap object */
871 static ULONG WINAPI MessageFilter_Release(IMessageFilter *iface)
873 return 1; /* non-heap object */
876 static DWORD WINAPI MessageFilter_HandleInComingCall(
877 IMessageFilter *iface,
878 DWORD dwCallType,
879 HTASK threadIDCaller,
880 DWORD dwTickCount,
881 LPINTERFACEINFO lpInterfaceInfo)
883 trace("HandleInComingCall\n");
884 return SERVERCALL_ISHANDLED;
887 static DWORD WINAPI MessageFilter_RetryRejectedCall(
888 IMessageFilter *iface,
889 HTASK threadIDCallee,
890 DWORD dwTickCount,
891 DWORD dwRejectType)
893 trace("RetryRejectedCall\n");
894 return 0;
897 static DWORD WINAPI MessageFilter_MessagePending(
898 IMessageFilter *iface,
899 HTASK threadIDCallee,
900 DWORD dwTickCount,
901 DWORD dwPendingType)
903 trace("MessagePending\n");
904 return PENDINGMSG_WAITNOPROCESS;
907 static const IMessageFilterVtbl MessageFilter_Vtbl =
909 MessageFilter_QueryInterface,
910 MessageFilter_AddRef,
911 MessageFilter_Release,
912 MessageFilter_HandleInComingCall,
913 MessageFilter_RetryRejectedCall,
914 MessageFilter_MessagePending
917 static IMessageFilter MessageFilter = { &MessageFilter_Vtbl };
919 static void test_CoRegisterMessageFilter(void)
921 HRESULT hr;
922 IMessageFilter *prev_filter;
924 hr = CoRegisterMessageFilter(&MessageFilter, &prev_filter);
925 ok(hr == CO_E_NOT_SUPPORTED,
926 "CoRegisterMessageFilter should have failed with CO_E_NOT_SUPPORTED instead of 0x%08x\n",
927 hr);
929 pCoInitializeEx(NULL, COINIT_MULTITHREADED);
930 prev_filter = (IMessageFilter *)0xdeadbeef;
931 hr = CoRegisterMessageFilter(&MessageFilter, &prev_filter);
932 ok(hr == CO_E_NOT_SUPPORTED,
933 "CoRegisterMessageFilter should have failed with CO_E_NOT_SUPPORTED instead of 0x%08x\n",
934 hr);
935 ok(prev_filter == (IMessageFilter *)0xdeadbeef,
936 "prev_filter should have been set to %p\n", prev_filter);
937 CoUninitialize();
939 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
941 hr = CoRegisterMessageFilter(NULL, NULL);
942 ok_ole_success(hr, "CoRegisterMessageFilter");
944 prev_filter = (IMessageFilter *)0xdeadbeef;
945 hr = CoRegisterMessageFilter(NULL, &prev_filter);
946 ok_ole_success(hr, "CoRegisterMessageFilter");
947 ok(prev_filter == NULL, "prev_filter should have been set to NULL instead of %p\n", prev_filter);
949 hr = CoRegisterMessageFilter(&MessageFilter, &prev_filter);
950 ok_ole_success(hr, "CoRegisterMessageFilter");
951 ok(prev_filter == NULL, "prev_filter should have been set to NULL instead of %p\n", prev_filter);
953 hr = CoRegisterMessageFilter(NULL, NULL);
954 ok_ole_success(hr, "CoRegisterMessageFilter");
956 CoUninitialize();
959 static IUnknown Test_Unknown;
961 static HRESULT WINAPI EnumOLEVERB_QueryInterface(IEnumOLEVERB *iface, REFIID riid, void **ppv)
963 return IUnknown_QueryInterface(&Test_Unknown, riid, ppv);
966 static ULONG WINAPI EnumOLEVERB_AddRef(IEnumOLEVERB *iface)
968 return 2;
971 static ULONG WINAPI EnumOLEVERB_Release(IEnumOLEVERB *iface)
973 return 1;
976 static HRESULT WINAPI EnumOLEVERB_Next(IEnumOLEVERB *iface, ULONG celt, OLEVERB *rgelt, ULONG *fetched)
978 ok(0, "unexpected call\n");
979 return E_NOTIMPL;
982 static HRESULT WINAPI EnumOLEVERB_Skip(IEnumOLEVERB *iface, ULONG celt)
984 ok(0, "unexpected call\n");
985 return E_NOTIMPL;
988 static HRESULT WINAPI EnumOLEVERB_Reset(IEnumOLEVERB *iface)
990 ok(0, "unexpected call\n");
991 return E_NOTIMPL;
994 static HRESULT WINAPI EnumOLEVERB_Clone(IEnumOLEVERB *iface, IEnumOLEVERB **ppenum)
996 ok(0, "unexpected call\n");
997 return E_NOTIMPL;
1000 static const IEnumOLEVERBVtbl EnumOLEVERBVtbl = {
1001 EnumOLEVERB_QueryInterface,
1002 EnumOLEVERB_AddRef,
1003 EnumOLEVERB_Release,
1004 EnumOLEVERB_Next,
1005 EnumOLEVERB_Skip,
1006 EnumOLEVERB_Reset,
1007 EnumOLEVERB_Clone
1010 static IEnumOLEVERB EnumOLEVERB = { &EnumOLEVERBVtbl };
1012 static HRESULT WINAPI Test_IUnknown_QueryInterface(
1013 IUnknown *iface,
1014 REFIID riid,
1015 LPVOID *ppvObj)
1017 if (ppvObj == NULL) return E_POINTER;
1019 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IWineTest)) {
1020 *ppvObj = iface;
1021 }else if(IsEqualIID(riid, &IID_IEnumOLEVERB)) {
1022 *ppvObj = &EnumOLEVERB;
1023 }else {
1024 *ppvObj = NULL;
1025 return E_NOINTERFACE;
1028 IUnknown_AddRef((IUnknown*)*ppvObj);
1029 return S_OK;
1032 static ULONG WINAPI Test_IUnknown_AddRef(IUnknown *iface)
1034 return 2; /* non-heap-based object */
1037 static ULONG WINAPI Test_IUnknown_Release(IUnknown *iface)
1039 return 1; /* non-heap-based object */
1042 static const IUnknownVtbl TestUnknown_Vtbl =
1044 Test_IUnknown_QueryInterface,
1045 Test_IUnknown_AddRef,
1046 Test_IUnknown_Release,
1049 static IUnknown Test_Unknown = { &TestUnknown_Vtbl };
1051 static IPSFactoryBuffer *ps_factory_buffer;
1053 static HRESULT WINAPI PSFactoryBuffer_QueryInterface(
1054 IPSFactoryBuffer * This,
1055 /* [in] */ REFIID riid,
1056 /* [iid_is][out] */ void **ppvObject)
1058 if (IsEqualIID(riid, &IID_IUnknown) ||
1059 IsEqualIID(riid, &IID_IPSFactoryBuffer))
1061 *ppvObject = This;
1062 IPSFactoryBuffer_AddRef(This);
1063 return S_OK;
1065 return E_NOINTERFACE;
1068 static ULONG WINAPI PSFactoryBuffer_AddRef(
1069 IPSFactoryBuffer * This)
1071 return 2;
1074 static ULONG WINAPI PSFactoryBuffer_Release(
1075 IPSFactoryBuffer * This)
1077 return 1;
1080 static HRESULT WINAPI PSFactoryBuffer_CreateProxy(
1081 IPSFactoryBuffer * This,
1082 /* [in] */ IUnknown *pUnkOuter,
1083 /* [in] */ REFIID riid,
1084 /* [out] */ IRpcProxyBuffer **ppProxy,
1085 /* [out] */ void **ppv)
1087 return E_NOTIMPL;
1090 static HRESULT WINAPI PSFactoryBuffer_CreateStub(
1091 IPSFactoryBuffer * This,
1092 /* [in] */ REFIID riid,
1093 /* [unique][in] */ IUnknown *pUnkServer,
1094 /* [out] */ IRpcStubBuffer **ppStub)
1096 CHECK_EXPECT(CreateStub);
1098 ok(pUnkServer == (IUnknown*)&Test_Unknown, "unexpected pUnkServer %p\n", pUnkServer);
1099 if(!ps_factory_buffer)
1100 return E_NOTIMPL;
1102 return IPSFactoryBuffer_CreateStub(ps_factory_buffer, &IID_IEnumOLEVERB, pUnkServer, ppStub);
1105 static IPSFactoryBufferVtbl PSFactoryBufferVtbl =
1107 PSFactoryBuffer_QueryInterface,
1108 PSFactoryBuffer_AddRef,
1109 PSFactoryBuffer_Release,
1110 PSFactoryBuffer_CreateProxy,
1111 PSFactoryBuffer_CreateStub
1114 static IPSFactoryBuffer PSFactoryBuffer = { &PSFactoryBufferVtbl };
1116 static const CLSID CLSID_WineTestPSFactoryBuffer =
1118 0x52011640,
1119 0x8164,
1120 0x4fd0,
1121 {0xa1, 0xa2, 0x5d, 0x5a, 0x36, 0x54, 0xd3, 0xbd}
1122 }; /* 52011640-8164-4fd0-a1a2-5d5a3654d3bd */
1124 static void test_CoRegisterPSClsid(void)
1126 HRESULT hr;
1127 DWORD dwRegistrationKey;
1128 IStream *stream;
1129 CLSID clsid;
1131 hr = CoRegisterPSClsid(&IID_IWineTest, &CLSID_WineTestPSFactoryBuffer);
1132 ok(hr == CO_E_NOTINITIALIZED, "CoRegisterPSClsid should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n", hr);
1134 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1136 hr = CoRegisterClassObject(&CLSID_WineTestPSFactoryBuffer, (IUnknown *)&PSFactoryBuffer,
1137 CLSCTX_INPROC_SERVER, REGCLS_MULTIPLEUSE, &dwRegistrationKey);
1138 ok_ole_success(hr, "CoRegisterClassObject");
1140 hr = CoRegisterPSClsid(&IID_IWineTest, &CLSID_WineTestPSFactoryBuffer);
1141 ok_ole_success(hr, "CoRegisterPSClsid");
1143 hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
1144 ok_ole_success(hr, "CreateStreamOnHGlobal");
1146 SET_EXPECT(CreateStub);
1147 hr = CoMarshalInterface(stream, &IID_IWineTest, &Test_Unknown, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1148 ok(hr == E_NOTIMPL, "CoMarshalInterface should have returned E_NOTIMPL instead of 0x%08x\n", hr);
1149 CHECK_CALLED(CreateStub);
1151 hr = CoGetPSClsid(&IID_IEnumOLEVERB, &clsid);
1152 ok_ole_success(hr, "CoGetPSClsid");
1154 hr = CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL, &IID_IPSFactoryBuffer, (void **)&ps_factory_buffer);
1155 ok_ole_success(hr, "CoGetClassObject");
1157 hr = CoRegisterPSClsid(&IID_IEnumOLEVERB, &CLSID_WineTestPSFactoryBuffer);
1158 ok_ole_success(hr, "CoRegisterPSClsid");
1160 SET_EXPECT(CreateStub);
1161 hr = CoMarshalInterface(stream, &IID_IEnumOLEVERB, (IUnknown*)&EnumOLEVERB, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1162 ok(hr == S_OK, "CoMarshalInterface should have returned E_NOTIMPL instead of 0x%08x\n", hr);
1163 CHECK_CALLED(CreateStub);
1165 hr = CoMarshalInterface(stream, &IID_IEnumOLEVERB, &Test_Unknown, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1166 ok(hr == S_OK, "CoMarshalInterface should have returned E_NOTIMPL instead of 0x%08x\n", hr);
1168 IStream_Release(stream);
1169 IPSFactoryBuffer_Release(ps_factory_buffer);
1170 ps_factory_buffer = NULL;
1172 hr = CoRevokeClassObject(dwRegistrationKey);
1173 ok_ole_success(hr, "CoRevokeClassObject");
1175 CoUninitialize();
1177 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1179 hr = CoGetPSClsid(&IID_IWineTest, &clsid);
1180 ok(hr == REGDB_E_IIDNOTREG, "CoGetPSClsid should have returned REGDB_E_IIDNOTREG instead of 0x%08x\n", hr);
1182 CoUninitialize();
1185 static void test_CoGetPSClsid(void)
1187 ULONG_PTR cookie;
1188 HANDLE handle;
1189 HRESULT hr;
1190 CLSID clsid;
1191 HKEY hkey;
1192 LONG res;
1193 const BOOL is_win64 = (sizeof(void*) != sizeof(int));
1194 BOOL is_wow64 = FALSE;
1196 hr = CoGetPSClsid(&IID_IClassFactory, &clsid);
1197 ok(hr == CO_E_NOTINITIALIZED,
1198 "CoGetPSClsid should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n",
1199 hr);
1201 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1203 hr = CoGetPSClsid(&IID_IClassFactory, &clsid);
1204 ok_ole_success(hr, "CoGetPSClsid");
1206 hr = CoGetPSClsid(&IID_IWineTest, &clsid);
1207 ok(hr == REGDB_E_IIDNOTREG,
1208 "CoGetPSClsid for random IID returned 0x%08x instead of REGDB_E_IIDNOTREG\n",
1209 hr);
1211 hr = CoGetPSClsid(&IID_IClassFactory, NULL);
1212 ok(hr == E_INVALIDARG,
1213 "CoGetPSClsid for null clsid returned 0x%08x instead of E_INVALIDARG\n",
1214 hr);
1216 if (!pRegOverridePredefKey)
1218 win_skip("RegOverridePredefKey not available\n");
1219 CoUninitialize();
1220 return;
1222 hr = CoGetPSClsid(&IID_IClassFactory, &clsid);
1223 ok_ole_success(hr, "CoGetPSClsid");
1225 res = RegCreateKeyExA(HKEY_CURRENT_USER, "Software\\Classes", 0, NULL, 0,
1226 KEY_ALL_ACCESS, NULL, &hkey, NULL);
1227 ok(!res, "RegCreateKeyEx returned %d\n", res);
1229 res = pRegOverridePredefKey(HKEY_CLASSES_ROOT, hkey);
1230 ok(!res, "RegOverridePredefKey returned %d\n", res);
1232 hr = CoGetPSClsid(&IID_IClassFactory, &clsid);
1233 ok_ole_success(hr, "CoGetPSClsid");
1235 res = pRegOverridePredefKey(HKEY_CLASSES_ROOT, NULL);
1236 ok(!res, "RegOverridePredefKey returned %d\n", res);
1238 RegCloseKey(hkey);
1240 /* not registered CLSID */
1241 hr = CoGetPSClsid(&IID_Testiface, &clsid);
1242 ok(hr == REGDB_E_IIDNOTREG, "got 0x%08x\n", hr);
1244 if ((handle = activate_context(actctx_manifest, &cookie)))
1246 memset(&clsid, 0, sizeof(clsid));
1247 hr = CoGetPSClsid(&IID_Testiface, &clsid);
1248 ok(hr == S_OK, "got 0x%08x\n", hr);
1249 ok(IsEqualGUID(&clsid, &IID_Testiface), "got clsid %s\n", wine_dbgstr_guid(&clsid));
1251 memset(&clsid, 0, sizeof(clsid));
1252 hr = CoGetPSClsid(&IID_Testiface2, &clsid);
1253 ok(hr == S_OK, "got 0x%08x\n", hr);
1254 ok(IsEqualGUID(&clsid, &IID_Testiface2), "got clsid %s\n", wine_dbgstr_guid(&clsid));
1256 memset(&clsid, 0, sizeof(clsid));
1257 hr = CoGetPSClsid(&IID_Testiface3, &clsid);
1258 ok(hr == S_OK, "got 0x%08x\n", hr);
1259 ok(IsEqualGUID(&clsid, &IID_TestPS), "got clsid %s\n", wine_dbgstr_guid(&clsid));
1261 memset(&clsid, 0xaa, sizeof(clsid));
1262 hr = CoGetPSClsid(&IID_Testiface4, &clsid);
1263 ok(hr == S_OK, "got 0x%08x\n", hr);
1264 ok(IsEqualGUID(&clsid, &GUID_NULL), "got clsid %s\n", wine_dbgstr_guid(&clsid));
1266 /* register same interface and try to get CLSID back */
1267 hr = CoRegisterPSClsid(&IID_Testiface, &IID_Testiface4);
1268 ok(hr == S_OK, "got 0x%08x\n", hr);
1269 memset(&clsid, 0, sizeof(clsid));
1270 hr = CoGetPSClsid(&IID_Testiface, &clsid);
1271 ok(hr == S_OK, "got 0x%08x\n", hr);
1272 ok(IsEqualGUID(&clsid, &IID_Testiface4), "got clsid %s\n", wine_dbgstr_guid(&clsid));
1274 pDeactivateActCtx(0, cookie);
1275 pReleaseActCtx(handle);
1278 if (pRegDeleteKeyExA &&
1279 (is_win64 ||
1280 (pIsWow64Process && pIsWow64Process(GetCurrentProcess(), &is_wow64) && is_wow64)))
1282 static GUID IID_DeadBeef = {0xdeadbeef,0xdead,0xbeef,{0xde,0xad,0xbe,0xef,0xde,0xad,0xbe,0xef}};
1283 static const char clsidDeadBeef[] = "{deadbeef-dead-beef-dead-beefdeadbeef}";
1284 static const char clsidA[] = "{66666666-8888-7777-6666-555555555555}";
1285 HKEY hkey_iface, hkey_psclsid;
1286 REGSAM opposite = is_win64 ? KEY_WOW64_32KEY : KEY_WOW64_64KEY;
1288 hr = CoGetPSClsid(&IID_DeadBeef, &clsid);
1289 ok(hr == REGDB_E_IIDNOTREG, "got 0x%08x\n", hr);
1291 res = RegCreateKeyExA(HKEY_CLASSES_ROOT, "Interface",
1292 0, NULL, 0, KEY_ALL_ACCESS | opposite, NULL, &hkey_iface, NULL);
1293 ok(!res, "RegCreateKeyEx returned %d\n", res);
1294 res = RegCreateKeyExA(hkey_iface, clsidDeadBeef,
1295 0, NULL, 0, KEY_ALL_ACCESS | opposite, NULL, &hkey, NULL);
1296 ok(!res, "RegCreateKeyEx returned %d\n", res);
1297 res = RegCreateKeyExA(hkey, "ProxyStubClsid32",
1298 0, NULL, 0, KEY_ALL_ACCESS | opposite, NULL, &hkey_psclsid, NULL);
1299 ok(!res, "RegCreateKeyEx returned %d\n", res);
1300 res = RegSetValueExA(hkey_psclsid, NULL, 0, REG_SZ, (const BYTE *)clsidA, strlen(clsidA)+1);
1301 ok(!res, "RegSetValueEx returned %d\n", res);
1302 RegCloseKey(hkey_psclsid);
1304 hr = CoGetPSClsid(&IID_DeadBeef, &clsid);
1305 ok_ole_success(hr, "CoGetPSClsid");
1306 ok(IsEqualGUID(&clsid, &IID_TestPS), "got clsid %s\n", wine_dbgstr_guid(&clsid));
1308 res = pRegDeleteKeyExA(hkey, "ProxyStubClsid32", opposite, 0);
1309 ok(!res, "RegDeleteKeyEx returned %d\n", res);
1310 RegCloseKey(hkey);
1311 res = pRegDeleteKeyExA(hkey_iface, clsidDeadBeef, opposite, 0);
1312 ok(!res, "RegDeleteKeyEx returned %d\n", res);
1313 RegCloseKey(hkey_iface);
1316 CoUninitialize();
1319 /* basic test, mainly for invalid arguments. see marshal.c for more */
1320 static void test_CoUnmarshalInterface(void)
1322 IUnknown *pProxy;
1323 IStream *pStream;
1324 HRESULT hr;
1326 hr = CoUnmarshalInterface(NULL, &IID_IUnknown, (void **)&pProxy);
1327 ok(hr == E_INVALIDARG, "CoUnmarshalInterface should have returned E_INVALIDARG instead of 0x%08x\n", hr);
1329 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1330 ok_ole_success(hr, "CreateStreamOnHGlobal");
1332 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)&pProxy);
1333 todo_wine
1334 ok(hr == CO_E_NOTINITIALIZED, "CoUnmarshalInterface should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n", hr);
1336 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1338 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)&pProxy);
1339 ok(hr == STG_E_READFAULT, "CoUnmarshalInterface should have returned STG_E_READFAULT instead of 0x%08x\n", hr);
1341 CoUninitialize();
1343 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, NULL);
1344 ok(hr == E_INVALIDARG, "CoUnmarshalInterface should have returned E_INVALIDARG instead of 0x%08x\n", hr);
1346 IStream_Release(pStream);
1349 static void test_CoGetInterfaceAndReleaseStream(void)
1351 HRESULT hr;
1352 IUnknown *pUnk;
1354 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1356 hr = CoGetInterfaceAndReleaseStream(NULL, &IID_IUnknown, (void**)&pUnk);
1357 ok(hr == E_INVALIDARG, "hr %08x\n", hr);
1359 CoUninitialize();
1362 /* basic test, mainly for invalid arguments. see marshal.c for more */
1363 static void test_CoMarshalInterface(void)
1365 IStream *pStream;
1366 HRESULT hr;
1367 static const LARGE_INTEGER llZero;
1369 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1371 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1372 ok_ole_success(hr, "CreateStreamOnHGlobal");
1374 hr = CoMarshalInterface(pStream, &IID_IUnknown, NULL, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1375 ok(hr == E_INVALIDARG, "CoMarshalInterface should have returned E_INVALIDARG instead of 0x%08x\n", hr);
1377 hr = CoMarshalInterface(NULL, &IID_IUnknown, (IUnknown *)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1378 ok(hr == E_INVALIDARG, "CoMarshalInterface should have returned E_INVALIDARG instead of 0x%08x\n", hr);
1380 hr = CoMarshalInterface(pStream, &IID_IUnknown, (IUnknown *)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1381 ok_ole_success(hr, "CoMarshalInterface");
1383 /* stream not rewound */
1384 hr = CoReleaseMarshalData(pStream);
1385 ok(hr == STG_E_READFAULT, "CoReleaseMarshalData should have returned STG_E_READFAULT instead of 0x%08x\n", hr);
1387 hr = IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
1388 ok_ole_success(hr, "IStream_Seek");
1390 hr = CoReleaseMarshalData(pStream);
1391 ok_ole_success(hr, "CoReleaseMarshalData");
1393 IStream_Release(pStream);
1395 CoUninitialize();
1398 static void test_CoMarshalInterThreadInterfaceInStream(void)
1400 IStream *pStream;
1401 HRESULT hr;
1402 IClassFactory *pProxy;
1404 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1406 cLocks = 0;
1408 hr = CoMarshalInterThreadInterfaceInStream(&IID_IUnknown, (IUnknown *)&Test_ClassFactory, NULL);
1409 ok(hr == E_INVALIDARG, "CoMarshalInterThreadInterfaceInStream should have returned E_INVALIDARG instead of 0x%08x\n", hr);
1411 hr = CoMarshalInterThreadInterfaceInStream(&IID_IUnknown, NULL, &pStream);
1412 ok(hr == E_INVALIDARG, "CoMarshalInterThreadInterfaceInStream should have returned E_INVALIDARG instead of 0x%08x\n", hr);
1414 ok_no_locks();
1416 hr = CoMarshalInterThreadInterfaceInStream(&IID_IUnknown, (IUnknown *)&Test_ClassFactory, &pStream);
1417 ok_ole_success(hr, "CoMarshalInterThreadInterfaceInStream");
1419 ok_more_than_one_lock();
1421 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
1422 ok_ole_success(hr, "CoUnmarshalInterface");
1424 IClassFactory_Release(pProxy);
1425 IStream_Release(pStream);
1427 ok_no_locks();
1429 CoUninitialize();
1432 static void test_CoRegisterClassObject(void)
1434 ULONG_PTR ctxcookie;
1435 HANDLE handle;
1436 DWORD cookie;
1437 HRESULT hr;
1438 IClassFactory *pcf;
1440 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1442 /* CLSCTX_INPROC_SERVER */
1443 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
1444 CLSCTX_INPROC_SERVER, REGCLS_SINGLEUSE, &cookie);
1445 ok_ole_success(hr, "CoRegisterClassObject");
1446 hr = CoRevokeClassObject(cookie);
1447 ok_ole_success(hr, "CoRevokeClassObject");
1449 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
1450 CLSCTX_INPROC_SERVER, REGCLS_MULTIPLEUSE, &cookie);
1451 ok_ole_success(hr, "CoRegisterClassObject");
1452 hr = CoRevokeClassObject(cookie);
1453 ok_ole_success(hr, "CoRevokeClassObject");
1455 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
1456 CLSCTX_INPROC_SERVER, REGCLS_MULTI_SEPARATE, &cookie);
1457 ok_ole_success(hr, "CoRegisterClassObject");
1458 hr = CoRevokeClassObject(cookie);
1459 ok_ole_success(hr, "CoRevokeClassObject");
1461 /* CLSCTX_LOCAL_SERVER */
1462 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
1463 CLSCTX_LOCAL_SERVER, REGCLS_SINGLEUSE, &cookie);
1464 ok_ole_success(hr, "CoRegisterClassObject");
1465 hr = CoRevokeClassObject(cookie);
1466 ok_ole_success(hr, "CoRevokeClassObject");
1468 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
1469 CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE, &cookie);
1470 ok_ole_success(hr, "CoRegisterClassObject");
1471 hr = CoRevokeClassObject(cookie);
1472 ok_ole_success(hr, "CoRevokeClassObject");
1474 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
1475 CLSCTX_LOCAL_SERVER, REGCLS_MULTI_SEPARATE, &cookie);
1476 ok_ole_success(hr, "CoRegisterClassObject");
1477 hr = CoRevokeClassObject(cookie);
1478 ok_ole_success(hr, "CoRevokeClassObject");
1480 /* CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER */
1481 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
1482 CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, REGCLS_SINGLEUSE, &cookie);
1483 ok_ole_success(hr, "CoRegisterClassObject");
1484 hr = CoRevokeClassObject(cookie);
1485 ok_ole_success(hr, "CoRevokeClassObject");
1487 /* test whether an object that doesn't support IClassFactory can be
1488 * registered for CLSCTX_LOCAL_SERVER */
1489 hr = CoRegisterClassObject(&CLSID_WineOOPTest, &Test_Unknown,
1490 CLSCTX_LOCAL_SERVER, REGCLS_SINGLEUSE, &cookie);
1491 ok_ole_success(hr, "CoRegisterClassObject");
1492 hr = CoRevokeClassObject(cookie);
1493 ok_ole_success(hr, "CoRevokeClassObject");
1495 /* test whether registered class becomes invalid when apartment is destroyed */
1496 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
1497 CLSCTX_INPROC_SERVER, REGCLS_SINGLEUSE, &cookie);
1498 ok_ole_success(hr, "CoRegisterClassObject");
1500 CoUninitialize();
1501 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1503 hr = CoGetClassObject(&CLSID_WineOOPTest, CLSCTX_INPROC_SERVER, NULL,
1504 &IID_IClassFactory, (void **)&pcf);
1505 ok(hr == REGDB_E_CLASSNOTREG, "object registered in an apartment shouldn't accessible after it is destroyed\n");
1507 /* crashes with at least win9x DCOM! */
1508 if (0)
1509 CoRevokeClassObject(cookie);
1511 /* test that object is accessible */
1512 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory, CLSCTX_INPROC_SERVER,
1513 REGCLS_MULTIPLEUSE, &cookie);
1514 ok(hr == S_OK, "got 0x%08x\n", hr);
1516 hr = CoGetClassObject(&CLSID_WineOOPTest, CLSCTX_INPROC_SERVER, NULL, &IID_IClassFactory, (void**)&pcf);
1517 ok(hr == S_OK, "got 0x%08x\n", hr);
1518 IClassFactory_Release(pcf);
1520 /* context now contains CLSID_WineOOPTest, test if registered one could still be used */
1521 if ((handle = activate_context(actctx_manifest, &ctxcookie)))
1523 hr = CoGetClassObject(&CLSID_WineOOPTest, CLSCTX_INPROC_SERVER, NULL, &IID_IClassFactory, (void**)&pcf);
1524 todo_wine
1525 ok(hr == HRESULT_FROM_WIN32(ERROR_MOD_NOT_FOUND), "got 0x%08x\n", hr);
1527 pDeactivateActCtx(0, ctxcookie);
1528 pReleaseActCtx(handle);
1531 hr = CoGetClassObject(&CLSID_WineOOPTest, CLSCTX_INPROC_SERVER, NULL, &IID_IClassFactory, (void**)&pcf);
1532 ok(hr == S_OK, "got 0x%08x\n", hr);
1533 IClassFactory_Release(pcf);
1535 hr = CoRevokeClassObject(cookie);
1536 ok(hr == S_OK, "got 0x%08x\n", hr);
1538 CoUninitialize();
1541 static HRESULT get_class_object(CLSCTX clsctx)
1543 HRESULT hr;
1544 IClassFactory *pcf;
1546 hr = CoGetClassObject(&CLSID_WineOOPTest, clsctx, NULL, &IID_IClassFactory,
1547 (void **)&pcf);
1549 if (SUCCEEDED(hr))
1550 IClassFactory_Release(pcf);
1552 return hr;
1555 static DWORD CALLBACK get_class_object_thread(LPVOID pv)
1557 CLSCTX clsctx = (CLSCTX)(DWORD_PTR)pv;
1558 HRESULT hr;
1560 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1562 hr = get_class_object(clsctx);
1564 CoUninitialize();
1566 return hr;
1569 static DWORD CALLBACK get_class_object_proxy_thread(LPVOID pv)
1571 CLSCTX clsctx = (CLSCTX)(DWORD_PTR)pv;
1572 HRESULT hr;
1573 IClassFactory *pcf;
1574 IMultiQI *pMQI;
1576 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1578 hr = CoGetClassObject(&CLSID_WineOOPTest, clsctx, NULL, &IID_IClassFactory,
1579 (void **)&pcf);
1581 if (SUCCEEDED(hr))
1583 hr = IClassFactory_QueryInterface(pcf, &IID_IMultiQI, (void **)&pMQI);
1584 if (SUCCEEDED(hr))
1585 IMultiQI_Release(pMQI);
1586 IClassFactory_Release(pcf);
1589 CoUninitialize();
1591 return hr;
1594 static DWORD CALLBACK register_class_object_thread(LPVOID pv)
1596 HRESULT hr;
1597 DWORD cookie;
1599 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1601 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
1602 CLSCTX_INPROC_SERVER, REGCLS_SINGLEUSE, &cookie);
1604 CoUninitialize();
1606 return hr;
1609 static DWORD CALLBACK revoke_class_object_thread(LPVOID pv)
1611 DWORD cookie = (DWORD_PTR)pv;
1612 HRESULT hr;
1614 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1616 hr = CoRevokeClassObject(cookie);
1618 CoUninitialize();
1620 return hr;
1623 static void test_registered_object_thread_affinity(void)
1625 HRESULT hr;
1626 DWORD cookie;
1627 HANDLE thread;
1628 DWORD tid;
1629 DWORD exitcode;
1631 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1633 /* CLSCTX_INPROC_SERVER */
1635 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
1636 CLSCTX_INPROC_SERVER, REGCLS_SINGLEUSE, &cookie);
1637 ok_ole_success(hr, "CoRegisterClassObject");
1639 thread = CreateThread(NULL, 0, get_class_object_thread, (LPVOID)CLSCTX_INPROC_SERVER, 0, &tid);
1640 ok(thread != NULL, "CreateThread failed with error %d\n", GetLastError());
1641 ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
1642 GetExitCodeThread(thread, &exitcode);
1643 hr = exitcode;
1644 ok(hr == REGDB_E_CLASSNOTREG, "CoGetClassObject on inproc object "
1645 "registered in different thread should return REGDB_E_CLASSNOTREG "
1646 "instead of 0x%08x\n", hr);
1648 hr = get_class_object(CLSCTX_INPROC_SERVER);
1649 ok(hr == S_OK, "CoGetClassObject on inproc object registered in same "
1650 "thread should return S_OK instead of 0x%08x\n", hr);
1652 thread = CreateThread(NULL, 0, register_class_object_thread, NULL, 0, &tid);
1653 ok(thread != NULL, "CreateThread failed with error %d\n", GetLastError());
1654 ok ( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
1655 GetExitCodeThread(thread, &exitcode);
1656 hr = exitcode;
1657 ok(hr == S_OK, "CoRegisterClassObject with same CLSID but in different thread should return S_OK instead of 0x%08x\n", hr);
1659 hr = CoRevokeClassObject(cookie);
1660 ok_ole_success(hr, "CoRevokeClassObject");
1662 /* CLSCTX_LOCAL_SERVER */
1664 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
1665 CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE, &cookie);
1666 ok_ole_success(hr, "CoRegisterClassObject");
1668 thread = CreateThread(NULL, 0, get_class_object_proxy_thread, (LPVOID)CLSCTX_LOCAL_SERVER, 0, &tid);
1669 ok(thread != NULL, "CreateThread failed with error %d\n", GetLastError());
1670 while (MsgWaitForMultipleObjects(1, &thread, FALSE, 10000, QS_ALLINPUT) == WAIT_OBJECT_0 + 1)
1672 MSG msg;
1673 while (PeekMessageA(&msg, NULL, 0, 0, PM_REMOVE))
1675 TranslateMessage(&msg);
1676 DispatchMessageA(&msg);
1679 GetExitCodeThread(thread, &exitcode);
1680 hr = exitcode;
1681 ok(hr == S_OK, "CoGetClassObject on local server object "
1682 "registered in different thread should return S_OK "
1683 "instead of 0x%08x\n", hr);
1685 hr = get_class_object(CLSCTX_LOCAL_SERVER);
1686 ok(hr == S_OK, "CoGetClassObject on local server object registered in same "
1687 "thread should return S_OK instead of 0x%08x\n", hr);
1689 thread = CreateThread(NULL, 0, revoke_class_object_thread, (LPVOID)(DWORD_PTR)cookie, 0, &tid);
1690 ok(thread != NULL, "CreateThread failed with error %d\n", GetLastError());
1691 ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
1692 GetExitCodeThread(thread, &exitcode);
1693 hr = exitcode;
1694 ok(hr == RPC_E_WRONG_THREAD || broken(hr == S_OK) /* win8 */, "CoRevokeClassObject called from different "
1695 "thread to where registered should return RPC_E_WRONG_THREAD instead of 0x%08x\n", hr);
1697 thread = CreateThread(NULL, 0, register_class_object_thread, NULL, 0, &tid);
1698 ok(thread != NULL, "CreateThread failed with error %d\n", GetLastError());
1699 ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
1700 GetExitCodeThread(thread, &exitcode);
1701 hr = exitcode;
1702 ok(hr == S_OK, "CoRegisterClassObject with same CLSID but in different "
1703 "thread should return S_OK instead of 0x%08x\n", hr);
1705 hr = CoRevokeClassObject(cookie);
1706 ok_ole_success(hr, "CoRevokeClassObject");
1708 CoUninitialize();
1711 static DWORD CALLBACK free_libraries_thread(LPVOID p)
1713 CoFreeUnusedLibraries();
1714 return 0;
1717 static inline BOOL is_module_loaded(const char *module)
1719 return GetModuleHandleA(module) != 0;
1722 static void test_CoFreeUnusedLibraries(void)
1724 HRESULT hr;
1725 IUnknown *pUnk;
1726 DWORD tid;
1727 HANDLE thread;
1729 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1731 ok(!is_module_loaded("urlmon.dll"), "urlmon.dll shouldn't be loaded\n");
1733 hr = CoCreateInstance(&CLSID_FileProtocol, NULL, CLSCTX_INPROC_SERVER, &IID_IInternetProtocol, (void **)&pUnk);
1734 if (hr == REGDB_E_CLASSNOTREG)
1736 skip("IE not installed so can't run CoFreeUnusedLibraries test\n");
1737 CoUninitialize();
1738 return;
1740 ok_ole_success(hr, "CoCreateInstance");
1742 ok(is_module_loaded("urlmon.dll"), "urlmon.dll should be loaded\n");
1744 ok(pUnk != NULL ||
1745 broken(pUnk == NULL), /* win9x */
1746 "Expected a valid pointer\n");
1747 if (pUnk)
1748 IUnknown_Release(pUnk);
1750 ok(is_module_loaded("urlmon.dll"), "urlmon.dll should be loaded\n");
1752 thread = CreateThread(NULL, 0, free_libraries_thread, NULL, 0, &tid);
1753 ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
1754 CloseHandle(thread);
1756 ok(is_module_loaded("urlmon.dll"), "urlmon.dll should be loaded\n");
1758 CoFreeUnusedLibraries();
1760 ok(!is_module_loaded("urlmon.dll"), "urlmon.dll shouldn't be loaded\n");
1762 CoUninitialize();
1765 static void test_CoGetObjectContext(void)
1767 HRESULT hr;
1768 ULONG refs;
1769 IComThreadingInfo *pComThreadingInfo;
1770 IContextCallback *pContextCallback;
1771 IObjContext *pObjContext;
1772 APTTYPE apttype;
1773 THDTYPE thdtype;
1774 struct info info;
1775 HANDLE thread;
1776 DWORD tid, exitcode;
1778 if (!pCoGetObjectContext)
1780 skip("CoGetObjectContext not present\n");
1781 return;
1784 hr = pCoGetObjectContext(&IID_IComThreadingInfo, (void **)&pComThreadingInfo);
1785 ok(hr == CO_E_NOTINITIALIZED, "CoGetObjectContext should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n", hr);
1786 ok(pComThreadingInfo == NULL, "pComThreadingInfo should have been set to NULL\n");
1788 /* show that COM doesn't have to be initialized for multi-threaded apartments if another
1789 thread has already done so */
1791 info.wait = CreateEventA(NULL, TRUE, FALSE, NULL);
1792 ok(info.wait != NULL, "CreateEvent failed with error %d\n", GetLastError());
1794 info.stop = CreateEventA(NULL, TRUE, FALSE, NULL);
1795 ok(info.stop != NULL, "CreateEvent failed with error %d\n", GetLastError());
1797 thread = CreateThread(NULL, 0, ole_initialize_thread, &info, 0, &tid);
1798 ok(thread != NULL, "CreateThread failed with error %d\n", GetLastError());
1800 ok( !WaitForSingleObject(info.wait, 10000), "wait timed out\n" );
1802 pComThreadingInfo = NULL;
1803 hr = pCoGetObjectContext(&IID_IComThreadingInfo, (void **)&pComThreadingInfo);
1804 ok(hr == S_OK, "Expected S_OK, got 0x%08x\n", hr);
1805 IComThreadingInfo_Release(pComThreadingInfo);
1807 SetEvent(info.stop);
1808 ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
1810 GetExitCodeThread(thread, &exitcode);
1811 hr = exitcode;
1812 ok(hr == S_OK, "thread should have returned S_OK instead of 0x%08x\n", hr);
1814 CloseHandle(thread);
1815 CloseHandle(info.wait);
1816 CloseHandle(info.stop);
1818 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1820 hr = pCoGetObjectContext(&IID_IComThreadingInfo, (void **)&pComThreadingInfo);
1821 ok_ole_success(hr, "CoGetObjectContext");
1823 hr = IComThreadingInfo_GetCurrentApartmentType(pComThreadingInfo, &apttype);
1824 ok_ole_success(hr, "IComThreadingInfo_GetCurrentApartmentType");
1825 ok(apttype == APTTYPE_MAINSTA, "apartment type should be APTTYPE_MAINSTA instead of %d\n", apttype);
1827 hr = IComThreadingInfo_GetCurrentThreadType(pComThreadingInfo, &thdtype);
1828 ok_ole_success(hr, "IComThreadingInfo_GetCurrentThreadType");
1829 ok(thdtype == THDTYPE_PROCESSMESSAGES, "thread type should be THDTYPE_PROCESSMESSAGES instead of %d\n", thdtype);
1831 refs = IComThreadingInfo_Release(pComThreadingInfo);
1832 ok(refs == 0, "pComThreadingInfo should have 0 refs instead of %d refs\n", refs);
1834 hr = pCoGetObjectContext(&IID_IContextCallback, (void **)&pContextCallback);
1835 ok_ole_success(hr, "CoGetObjectContext(ContextCallback)");
1837 if (hr == S_OK)
1839 refs = IContextCallback_Release(pContextCallback);
1840 ok(refs == 0, "pContextCallback should have 0 refs instead of %d refs\n", refs);
1843 CoUninitialize();
1845 pCoInitializeEx(NULL, COINIT_MULTITHREADED);
1847 hr = pCoGetObjectContext(&IID_IComThreadingInfo, (void **)&pComThreadingInfo);
1848 ok_ole_success(hr, "CoGetObjectContext");
1850 hr = IComThreadingInfo_GetCurrentApartmentType(pComThreadingInfo, &apttype);
1851 ok_ole_success(hr, "IComThreadingInfo_GetCurrentApartmentType");
1852 ok(apttype == APTTYPE_MTA, "apartment type should be APTTYPE_MTA instead of %d\n", apttype);
1854 hr = IComThreadingInfo_GetCurrentThreadType(pComThreadingInfo, &thdtype);
1855 ok_ole_success(hr, "IComThreadingInfo_GetCurrentThreadType");
1856 ok(thdtype == THDTYPE_BLOCKMESSAGES, "thread type should be THDTYPE_BLOCKMESSAGES instead of %d\n", thdtype);
1858 refs = IComThreadingInfo_Release(pComThreadingInfo);
1859 ok(refs == 0, "pComThreadingInfo should have 0 refs instead of %d refs\n", refs);
1861 hr = pCoGetObjectContext(&IID_IContextCallback, (void **)&pContextCallback);
1862 ok_ole_success(hr, "CoGetObjectContext(ContextCallback)");
1864 if (hr == S_OK)
1866 refs = IContextCallback_Release(pContextCallback);
1867 ok(refs == 0, "pContextCallback should have 0 refs instead of %d refs\n", refs);
1870 hr = pCoGetObjectContext(&IID_IObjContext, (void **)&pObjContext);
1871 ok_ole_success(hr, "CoGetObjectContext");
1873 refs = IObjContext_Release(pObjContext);
1874 ok(refs == 0, "pObjContext should have 0 refs instead of %d refs\n", refs);
1876 CoUninitialize();
1879 typedef struct {
1880 IUnknown IUnknown_iface;
1881 LONG refs;
1882 } Test_CallContext;
1884 static inline Test_CallContext *impl_from_IUnknown(IUnknown *iface)
1886 return CONTAINING_RECORD(iface, Test_CallContext, IUnknown_iface);
1889 static HRESULT WINAPI Test_CallContext_QueryInterface(
1890 IUnknown *iface,
1891 REFIID riid,
1892 LPVOID *ppvObj)
1894 if (ppvObj == NULL) return E_POINTER;
1896 if (IsEqualGUID(riid, &IID_IUnknown))
1898 *ppvObj = iface;
1899 IUnknown_AddRef(iface);
1900 return S_OK;
1903 *ppvObj = NULL;
1904 return E_NOINTERFACE;
1907 static ULONG WINAPI Test_CallContext_AddRef(IUnknown *iface)
1909 Test_CallContext *This = impl_from_IUnknown(iface);
1910 return InterlockedIncrement(&This->refs);
1913 static ULONG WINAPI Test_CallContext_Release(IUnknown *iface)
1915 Test_CallContext *This = impl_from_IUnknown(iface);
1916 ULONG refs = InterlockedDecrement(&This->refs);
1917 if (!refs)
1918 HeapFree(GetProcessHeap(), 0, This);
1919 return refs;
1922 static const IUnknownVtbl TestCallContext_Vtbl =
1924 Test_CallContext_QueryInterface,
1925 Test_CallContext_AddRef,
1926 Test_CallContext_Release
1929 static void test_CoGetCallContext(void)
1931 HRESULT hr;
1932 ULONG refs;
1933 IUnknown *pUnk;
1934 Test_CallContext *test_object;
1936 if (!pCoSwitchCallContext)
1938 skip("CoSwitchCallContext not present\n");
1939 return;
1942 CoInitialize(NULL);
1944 test_object = HeapAlloc(GetProcessHeap(), 0, sizeof(Test_CallContext));
1945 test_object->IUnknown_iface.lpVtbl = &TestCallContext_Vtbl;
1946 test_object->refs = 1;
1948 hr = CoGetCallContext(&IID_IUnknown, (void**)&pUnk);
1949 ok(hr == RPC_E_CALL_COMPLETE, "Expected RPC_E_CALL_COMPLETE, got 0x%08x\n", hr);
1951 pUnk = (IUnknown*)0xdeadbeef;
1952 hr = pCoSwitchCallContext(&test_object->IUnknown_iface, &pUnk);
1953 ok_ole_success(hr, "CoSwitchCallContext");
1954 ok(pUnk == NULL, "expected NULL, got %p\n", pUnk);
1955 refs = IUnknown_AddRef(&test_object->IUnknown_iface);
1956 ok(refs == 2, "Expected refcount 2, got %d\n", refs);
1957 IUnknown_Release(&test_object->IUnknown_iface);
1959 pUnk = (IUnknown*)0xdeadbeef;
1960 hr = CoGetCallContext(&IID_IUnknown, (void**)&pUnk);
1961 ok_ole_success(hr, "CoGetCallContext");
1962 ok(pUnk == &test_object->IUnknown_iface, "expected %p, got %p\n",
1963 &test_object->IUnknown_iface, pUnk);
1964 refs = IUnknown_AddRef(&test_object->IUnknown_iface);
1965 ok(refs == 3, "Expected refcount 3, got %d\n", refs);
1966 IUnknown_Release(&test_object->IUnknown_iface);
1967 IUnknown_Release(pUnk);
1969 pUnk = (IUnknown*)0xdeadbeef;
1970 hr = pCoSwitchCallContext(NULL, &pUnk);
1971 ok_ole_success(hr, "CoSwitchCallContext");
1972 ok(pUnk == &test_object->IUnknown_iface, "expected %p, got %p\n",
1973 &test_object->IUnknown_iface, pUnk);
1974 refs = IUnknown_AddRef(&test_object->IUnknown_iface);
1975 ok(refs == 2, "Expected refcount 2, got %d\n", refs);
1976 IUnknown_Release(&test_object->IUnknown_iface);
1978 hr = CoGetCallContext(&IID_IUnknown, (void**)&pUnk);
1979 ok(hr == RPC_E_CALL_COMPLETE, "Expected RPC_E_CALL_COMPLETE, got 0x%08x\n", hr);
1981 IUnknown_Release(&test_object->IUnknown_iface);
1983 CoUninitialize();
1986 static void test_CoGetContextToken(void)
1988 HRESULT hr;
1989 ULONG refs;
1990 ULONG_PTR token;
1991 IObjContext *ctx;
1992 struct info info;
1993 HANDLE thread;
1994 DWORD tid, exitcode;
1996 if (!pCoGetContextToken)
1998 win_skip("CoGetContextToken not present\n");
1999 return;
2002 token = 0xdeadbeef;
2003 hr = pCoGetContextToken(&token);
2004 ok(hr == CO_E_NOTINITIALIZED, "Expected CO_E_NOTINITIALIZED, got 0x%08x\n", hr);
2005 ok(token == 0xdeadbeef, "Expected 0, got 0x%lx\n", token);
2007 /* show that COM doesn't have to be initialized for multi-threaded apartments if another
2008 thread has already done so */
2010 info.wait = CreateEventA(NULL, TRUE, FALSE, NULL);
2011 ok(info.wait != NULL, "CreateEvent failed with error %d\n", GetLastError());
2013 info.stop = CreateEventA(NULL, TRUE, FALSE, NULL);
2014 ok(info.stop != NULL, "CreateEvent failed with error %d\n", GetLastError());
2016 thread = CreateThread(NULL, 0, ole_initialize_thread, &info, 0, &tid);
2017 ok(thread != NULL, "CreateThread failed with error %d\n", GetLastError());
2019 ok( !WaitForSingleObject(info.wait, 10000), "wait timed out\n" );
2021 token = 0;
2022 hr = pCoGetContextToken(&token);
2023 ok(hr == S_OK, "Expected S_OK, got 0x%08x\n", hr);
2025 SetEvent(info.stop);
2026 ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
2028 GetExitCodeThread(thread, &exitcode);
2029 hr = exitcode;
2030 ok(hr == S_OK, "thread should have returned S_OK instead of 0x%08x\n", hr);
2032 CloseHandle(thread);
2033 CloseHandle(info.wait);
2034 CloseHandle(info.stop);
2036 CoInitialize(NULL);
2038 hr = pCoGetContextToken(NULL);
2039 ok(hr == E_POINTER, "Expected E_POINTER, got 0x%08x\n", hr);
2041 token = 0;
2042 hr = pCoGetContextToken(&token);
2043 ok(hr == S_OK, "Expected S_OK, got 0x%08x\n", hr);
2044 ok(token, "Expected token != 0\n");
2046 refs = IUnknown_AddRef((IUnknown *)token);
2047 todo_wine ok(refs == 1, "Expected 1, got %u\n", refs);
2049 hr = pCoGetObjectContext(&IID_IObjContext, (void **)&ctx);
2050 ok(hr == S_OK, "Expected S_OK, got 0x%08x\n", hr);
2051 todo_wine ok(ctx == (IObjContext *)token, "Expected interface pointers to be the same\n");
2053 refs = IObjContext_AddRef(ctx);
2054 todo_wine ok(refs == 3, "Expected 3, got %u\n", refs);
2056 refs = IObjContext_Release(ctx);
2057 todo_wine ok(refs == 2, "Expected 2, got %u\n", refs);
2059 refs = IUnknown_Release((IUnknown *)token);
2060 ok(refs == 1, "Expected 1, got %u\n", refs);
2062 /* CoGetContextToken does not add a reference */
2063 token = 0;
2064 hr = pCoGetContextToken(&token);
2065 ok(hr == S_OK, "Expected S_OK, got 0x%08x\n", hr);
2066 ok(token, "Expected token != 0\n");
2067 todo_wine ok(ctx == (IObjContext *)token, "Expected interface pointers to be the same\n");
2069 refs = IObjContext_AddRef(ctx);
2070 ok(refs == 2, "Expected 1, got %u\n", refs);
2072 refs = IObjContext_Release(ctx);
2073 ok(refs == 1, "Expected 0, got %u\n", refs);
2075 refs = IObjContext_Release(ctx);
2076 ok(refs == 0, "Expected 0, got %u\n", refs);
2078 CoUninitialize();
2081 static void test_TreatAsClass(void)
2083 HRESULT hr;
2084 CLSID out;
2085 static GUID deadbeef = {0xdeadbeef,0xdead,0xbeef,{0xde,0xad,0xbe,0xef,0xde,0xad,0xbe,0xef}};
2086 static const char deadbeefA[] = "{DEADBEEF-DEAD-BEEF-DEAD-BEEFDEADBEEF}";
2087 IInternetProtocol *pIP = NULL;
2088 HKEY clsidkey, deadbeefkey;
2089 LONG lr;
2091 if (!pCoGetTreatAsClass)
2093 win_skip("CoGetTreatAsClass not present\n");
2094 return;
2096 hr = pCoGetTreatAsClass(&deadbeef,&out);
2097 ok (hr == S_FALSE, "expected S_FALSE got %x\n",hr);
2098 ok (IsEqualGUID(&out,&deadbeef), "expected to get same clsid back\n");
2100 lr = RegOpenKeyExA(HKEY_CLASSES_ROOT, "CLSID", 0, KEY_READ, &clsidkey);
2101 ok(lr == ERROR_SUCCESS, "Couldn't open CLSID key\n");
2103 lr = RegCreateKeyExA(clsidkey, deadbeefA, 0, NULL, 0, KEY_WRITE, NULL, &deadbeefkey, NULL);
2104 if (lr) {
2105 win_skip("CoGetTreatAsClass() tests will be skipped (failed to create a test key, error %d)\n",
2106 GetLastError());
2107 RegCloseKey(clsidkey);
2108 return;
2111 hr = pCoTreatAsClass(&deadbeef, &deadbeef);
2112 ok(hr == REGDB_E_WRITEREGDB, "CoTreatAsClass gave wrong error: %08x\n", hr);
2114 hr = pCoTreatAsClass(&deadbeef, &CLSID_FileProtocol);
2115 if(hr == REGDB_E_WRITEREGDB){
2116 win_skip("Insufficient privileges to use CoTreatAsClass\n");
2117 goto exit;
2119 ok(hr == S_OK, "CoTreatAsClass failed: %08x\n", hr);
2121 hr = pCoGetTreatAsClass(&deadbeef, &out);
2122 ok(hr == S_OK, "CoGetTreatAsClass failed: %08x\n",hr);
2123 ok(IsEqualGUID(&out, &CLSID_FileProtocol), "expected to get substituted clsid\n");
2125 OleInitialize(NULL);
2127 hr = CoCreateInstance(&deadbeef, NULL, CLSCTX_INPROC_SERVER, &IID_IInternetProtocol, (void **)&pIP);
2128 if(hr == REGDB_E_CLASSNOTREG)
2130 win_skip("IE not installed so can't test CoCreateInstance\n");
2131 goto exit;
2134 ok(hr == S_OK, "CoCreateInstance failed: %08x\n", hr);
2135 if(pIP){
2136 IInternetProtocol_Release(pIP);
2137 pIP = NULL;
2140 hr = pCoTreatAsClass(&deadbeef, &CLSID_NULL);
2141 ok(hr == S_OK, "CoTreatAsClass failed: %08x\n", hr);
2143 hr = pCoGetTreatAsClass(&deadbeef, &out);
2144 ok(hr == S_FALSE, "expected S_FALSE got %08x\n", hr);
2145 ok(IsEqualGUID(&out, &deadbeef), "expected to get same clsid back\n");
2147 /* bizarrely, native's CoTreatAsClass takes some time to take effect in CoCreateInstance */
2148 Sleep(200);
2150 hr = CoCreateInstance(&deadbeef, NULL, CLSCTX_INPROC_SERVER, &IID_IInternetProtocol, (void **)&pIP);
2151 ok(hr == REGDB_E_CLASSNOTREG, "CoCreateInstance gave wrong error: %08x\n", hr);
2153 if(pIP)
2154 IInternetProtocol_Release(pIP);
2156 exit:
2157 OleUninitialize();
2158 RegCloseKey(deadbeefkey);
2159 RegDeleteKeyA(clsidkey, deadbeefA);
2160 RegCloseKey(clsidkey);
2163 static void test_CoInitializeEx(void)
2165 HRESULT hr;
2167 hr = pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
2168 ok(hr == S_OK, "CoInitializeEx failed with error 0x%08x\n", hr);
2170 /* Calling OleInitialize for the first time should yield S_OK even with
2171 * apartment already initialized by previous CoInitialize(Ex) calls. */
2172 hr = OleInitialize(NULL);
2173 ok(hr == S_OK, "OleInitialize failed with error 0x%08x\n", hr);
2175 /* Subsequent calls to OleInitialize should return S_FALSE */
2176 hr = OleInitialize(NULL);
2177 ok(hr == S_FALSE, "Expected S_FALSE, hr = 0x%08x\n", hr);
2179 /* Cleanup */
2180 CoUninitialize();
2181 OleUninitialize();
2182 OleUninitialize();
2185 static void test_OleInitialize_InitCounting(void)
2187 HRESULT hr;
2188 IUnknown *pUnk;
2189 REFCLSID rclsid = &CLSID_InternetZoneManager;
2191 /* 1. OleInitialize fails but OleUnintialize is still called: apartment stays inited */
2192 hr = pCoInitializeEx(NULL, COINIT_MULTITHREADED);
2193 ok(hr == S_OK, "CoInitializeEx(COINIT_MULTITHREADED) failed with error 0x%08x\n", hr);
2195 hr = OleInitialize(NULL);
2196 ok(hr == RPC_E_CHANGED_MODE, "OleInitialize should have returned 0x%08x instead of 0x%08x\n", RPC_E_CHANGED_MODE, hr);
2197 OleUninitialize();
2199 pUnk = (IUnknown *)0xdeadbeef;
2200 hr = CoCreateInstance(rclsid, NULL, 0x17, &IID_IUnknown, (void **)&pUnk);
2201 ok(hr == S_OK, "CoCreateInstance should have returned 0x%08x instead of 0x%08x\n", S_OK, hr);
2202 if (pUnk) IUnknown_Release(pUnk);
2204 CoUninitialize();
2206 /* 2. Extra multiple OleUninitialize: apartment stays inited until CoUnitialize */
2207 hr = CoInitialize(NULL);
2208 ok(hr == S_OK, "CoInitialize() failed with error 0x%08x\n", hr);
2210 hr = OleInitialize(NULL);
2211 ok(hr == S_OK, "OleInitialize should have returned 0x%08x instead of 0x%08x\n", S_OK, hr);
2212 OleUninitialize();
2213 OleUninitialize();
2214 OleUninitialize();
2216 pUnk = (IUnknown *)0xdeadbeef;
2217 hr = CoCreateInstance(rclsid, NULL, 0x17, &IID_IUnknown, (void **)&pUnk);
2218 ok(hr == S_OK, "CoCreateInstance should have returned 0x%08x instead of 0x%08x\n", S_OK, hr);
2219 if (pUnk) IUnknown_Release(pUnk);
2221 CoUninitialize();
2223 pUnk = (IUnknown *)0xdeadbeef;
2224 hr = CoCreateInstance(rclsid, NULL, 0x17, &IID_IUnknown, (void **)&pUnk);
2225 ok(hr == CO_E_NOTINITIALIZED, "CoCreateInstance should have returned 0x%08x instead of 0x%08x\n", CO_E_NOTINITIALIZED, hr);
2226 if (pUnk) IUnknown_Release(pUnk);
2228 /* 3. CoUninitialize does not formally deinit Ole */
2229 hr = CoInitialize(NULL);
2230 ok(hr == S_OK, "CoInitialize() failed with error 0x%08x\n", hr);
2232 hr = OleInitialize(NULL);
2233 ok(hr == S_OK, "OleInitialize should have returned 0x%08x instead of 0x%08x\n", S_OK, hr);
2235 CoUninitialize();
2236 CoUninitialize();
2238 pUnk = (IUnknown *)0xdeadbeef;
2239 hr = CoCreateInstance(rclsid, NULL, 0x17, &IID_IUnknown, (void **)&pUnk);
2240 ok(hr == CO_E_NOTINITIALIZED, "CoCreateInstance should have returned 0x%08x instead of 0x%08x\n", CO_E_NOTINITIALIZED, hr);
2241 /* COM is not initialized anymore */
2242 if (pUnk) IUnknown_Release(pUnk);
2244 hr = OleInitialize(NULL);
2245 ok(hr == S_FALSE, "OleInitialize should have returned 0x%08x instead of 0x%08x\n", S_FALSE, hr);
2246 /* ... but native OleInit returns S_FALSE as if Ole is considered initialized */
2248 OleUninitialize();
2252 static void test_OleRegGetMiscStatus(void)
2254 ULONG_PTR cookie;
2255 HANDLE handle;
2256 DWORD status;
2257 HRESULT hr;
2259 hr = OleRegGetMiscStatus(&CLSID_Testclass, DVASPECT_ICON, NULL);
2260 ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
2262 status = 0xdeadbeef;
2263 hr = OleRegGetMiscStatus(&CLSID_Testclass, DVASPECT_ICON, &status);
2264 ok(hr == REGDB_E_CLASSNOTREG, "got 0x%08x\n", hr);
2265 ok(status == 0, "got 0x%08x\n", status);
2267 status = -1;
2268 hr = OleRegGetMiscStatus(&CLSID_StdFont, DVASPECT_ICON, &status);
2269 ok(hr == S_OK, "got 0x%08x\n", hr);
2270 ok(status == 0, "got 0x%08x\n", status);
2272 if ((handle = activate_context(actctx_manifest, &cookie)))
2274 status = 0;
2275 hr = OleRegGetMiscStatus(&CLSID_Testclass, DVASPECT_ICON, &status);
2276 ok(hr == S_OK, "got 0x%08x\n", hr);
2277 ok(status == OLEMISC_RECOMPOSEONRESIZE, "got 0x%08x\n", status);
2279 /* context data takes precedence over registration info */
2280 status = 0;
2281 hr = OleRegGetMiscStatus(&CLSID_StdFont, DVASPECT_ICON, &status);
2282 ok(hr == S_OK, "got 0x%08x\n", hr);
2283 ok(status == OLEMISC_RECOMPOSEONRESIZE, "got 0x%08x\n", status);
2285 /* there's no such attribute in context */
2286 status = -1;
2287 hr = OleRegGetMiscStatus(&CLSID_Testclass, DVASPECT_DOCPRINT, &status);
2288 ok(hr == S_OK, "got 0x%08x\n", hr);
2289 ok(status == 0, "got 0x%08x\n", status);
2291 pDeactivateActCtx(0, cookie);
2292 pReleaseActCtx(handle);
2296 static void test_OleRegGetUserType(void)
2298 static const WCHAR stdfont_usertypeW[] = {'S','t','a','n','d','a','r','d',' ','F','o','n','t',0};
2299 static const WCHAR stdfont2_usertypeW[] = {'C','L','S','I','D','_','S','t','d','F','o','n','t',0};
2300 static const WCHAR clsidkeyW[] = {'C','L','S','I','D',0};
2301 static const WCHAR defvalueW[] = {'D','e','f','a','u','l','t',' ','N','a','m','e',0};
2302 static const WCHAR auxvalue0W[] = {'A','u','x',' ','N','a','m','e',' ','0',0};
2303 static const WCHAR auxvalue2W[] = {'A','u','x',' ','N','a','m','e',' ','2',0};
2304 static const WCHAR auxvalue3W[] = {'A','u','x',' ','N','a','m','e',' ','3',0};
2305 static const WCHAR auxvalue4W[] = {'A','u','x',' ','N','a','m','e',' ','4',0};
2307 static const char auxvalues[][16] = {
2308 "Aux Name 0",
2309 "Aux Name 1",
2310 "Aux Name 2",
2311 "Aux Name 3",
2312 "Aux Name 4"
2315 HKEY clsidhkey, hkey, auxhkey, classkey;
2316 DWORD form, ret, disposition;
2317 WCHAR clsidW[39];
2318 ULONG_PTR cookie;
2319 HANDLE handle;
2320 HRESULT hr;
2321 WCHAR *str;
2322 int i;
2324 for (form = 0; form <= USERCLASSTYPE_APPNAME+1; form++) {
2325 hr = OleRegGetUserType(&CLSID_Testclass, form, NULL);
2326 ok(hr == E_INVALIDARG, "form %u: got 0x%08x\n", form, hr);
2328 str = (void*)0xdeadbeef;
2329 hr = OleRegGetUserType(&CLSID_Testclass, form, &str);
2330 ok(hr == REGDB_E_CLASSNOTREG, "form %u: got 0x%08x\n", form, hr);
2331 ok(str == NULL, "form %u: got %p\n", form, str);
2333 /* same string returned for StdFont for all form types */
2334 str = NULL;
2335 hr = OleRegGetUserType(&CLSID_StdFont, form, &str);
2336 ok(hr == S_OK, "form %u: got 0x%08x\n", form, hr);
2337 ok(!lstrcmpW(str, stdfont_usertypeW) || !lstrcmpW(str, stdfont2_usertypeW) /* winxp */,
2338 "form %u, got %s\n", form, wine_dbgstr_w(str));
2339 CoTaskMemFree(str);
2342 if ((handle = activate_context(actctx_manifest, &cookie)))
2344 for (form = 0; form <= USERCLASSTYPE_APPNAME+1; form++) {
2345 str = (void*)0xdeadbeef;
2346 hr = OleRegGetUserType(&CLSID_Testclass, form, &str);
2347 ok(hr == REGDB_E_CLASSNOTREG, "form %u: got 0x%08x\n", form, hr);
2348 ok(str == NULL, "form %u: got %s\n", form, wine_dbgstr_w(str));
2350 /* same string returned for StdFont for all form types */
2351 str = NULL;
2352 hr = OleRegGetUserType(&CLSID_StdFont, form, &str);
2353 ok(hr == S_OK, "form %u: got 0x%08x\n", form, hr);
2354 ok(!lstrcmpW(str, stdfont_usertypeW) || !lstrcmpW(str, stdfont2_usertypeW) /* winxp */,
2355 "form %u, got %s\n", form, wine_dbgstr_w(str));
2356 CoTaskMemFree(str);
2359 pDeactivateActCtx(0, cookie);
2360 pReleaseActCtx(handle);
2363 /* test using registered CLSID */
2364 StringFromGUID2(&CLSID_non_existent, clsidW, sizeof(clsidW)/sizeof(clsidW[0]));
2366 ret = RegCreateKeyExW(HKEY_CLASSES_ROOT, clsidkeyW, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &clsidhkey, &disposition);
2367 if (ret == ERROR_ACCESS_DENIED)
2369 skip("Failed to create test key, skipping some of OleRegGetUserType() tests.\n");
2370 return;
2373 ok(!ret, "failed to create a key %d, error %d\n", ret, GetLastError());
2375 ret = RegCreateKeyExW(clsidhkey, clsidW, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &classkey, NULL);
2376 ok(!ret, "failed to create a key %d, error %d\n", ret, GetLastError());
2378 ret = RegSetValueExW(classkey, NULL, 0, REG_SZ, (const BYTE*)defvalueW, sizeof(defvalueW));
2379 ok(!ret, "got %d, error %d\n", ret, GetLastError());
2381 ret = RegCreateKeyExA(classkey, "AuxUserType", 0, NULL, 0, KEY_ALL_ACCESS, NULL, &auxhkey, NULL);
2382 ok(!ret, "got %d, error %d\n", ret, GetLastError());
2384 /* populate AuxUserType */
2385 for (i = 0; i <= 4; i++) {
2386 char name[16];
2388 sprintf(name, "AuxUserType\\%d", i);
2389 ret = RegCreateKeyExA(classkey, name, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hkey, NULL);
2390 ok(!ret, "got %d, error %d\n", ret, GetLastError());
2392 ret = RegSetValueExA(hkey, NULL, 0, REG_SZ, (const BYTE*)auxvalues[i], strlen(auxvalues[i]));
2393 ok(!ret, "got %d, error %d\n", ret, GetLastError());
2394 RegCloseKey(hkey);
2397 str = NULL;
2398 hr = OleRegGetUserType(&CLSID_non_existent, 0, &str);
2399 ok(hr == S_OK, "got 0x%08x\n", hr);
2400 ok(!lstrcmpW(str, auxvalue0W), "got %s\n", wine_dbgstr_w(str));
2401 CoTaskMemFree(str);
2403 str = NULL;
2404 hr = OleRegGetUserType(&CLSID_non_existent, USERCLASSTYPE_FULL, &str);
2405 ok(hr == S_OK, "got 0x%08x\n", hr);
2406 ok(!lstrcmpW(str, defvalueW), "got %s\n", wine_dbgstr_w(str));
2407 CoTaskMemFree(str);
2409 str = NULL;
2410 hr = OleRegGetUserType(&CLSID_non_existent, USERCLASSTYPE_SHORT, &str);
2411 ok(hr == S_OK, "got 0x%08x\n", hr);
2412 ok(!lstrcmpW(str, auxvalue2W), "got %s\n", wine_dbgstr_w(str));
2413 CoTaskMemFree(str);
2415 str = NULL;
2416 hr = OleRegGetUserType(&CLSID_non_existent, USERCLASSTYPE_APPNAME, &str);
2417 ok(hr == S_OK, "got 0x%08x\n", hr);
2418 ok(!lstrcmpW(str, auxvalue3W), "got %s\n", wine_dbgstr_w(str));
2419 CoTaskMemFree(str);
2421 str = NULL;
2422 hr = OleRegGetUserType(&CLSID_non_existent, USERCLASSTYPE_APPNAME+1, &str);
2423 ok(hr == S_OK, "got 0x%08x\n", hr);
2424 ok(!lstrcmpW(str, auxvalue4W), "got %s\n", wine_dbgstr_w(str));
2425 CoTaskMemFree(str);
2427 str = NULL;
2428 hr = OleRegGetUserType(&CLSID_non_existent, USERCLASSTYPE_APPNAME+2, &str);
2429 ok(hr == S_OK, "got 0x%08x\n", hr);
2430 ok(!lstrcmpW(str, defvalueW), "got %s\n", wine_dbgstr_w(str));
2431 CoTaskMemFree(str);
2433 /* registry cleanup */
2434 for (i = 0; i <= 4; i++)
2436 char name[2];
2437 sprintf(name, "%d", i);
2438 RegDeleteKeyA(auxhkey, name);
2440 RegCloseKey(auxhkey);
2441 RegDeleteKeyA(classkey, "AuxUserType");
2442 RegCloseKey(classkey);
2443 RegDeleteKeyW(clsidhkey, clsidW);
2444 RegCloseKey(clsidhkey);
2445 if (disposition == REG_CREATED_NEW_KEY)
2446 RegDeleteKeyA(HKEY_CLASSES_ROOT, "CLSID");
2449 static void test_CoCreateGuid(void)
2451 HRESULT hr;
2453 hr = CoCreateGuid(NULL);
2454 ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
2457 static void CALLBACK apc_test_proc(ULONG_PTR param)
2459 /* nothing */
2462 static DWORD CALLBACK release_semaphore_thread( LPVOID arg )
2464 HANDLE handle = arg;
2465 if (WaitForSingleObject(handle, 200) == WAIT_TIMEOUT)
2466 ReleaseSemaphore(handle, 1, NULL);
2467 return 0;
2470 static DWORD CALLBACK send_message_thread(LPVOID arg)
2472 HWND hWnd = arg;
2473 Sleep(50);
2474 SendMessageA(hWnd, WM_DDE_FIRST, 0, 0);
2475 return 0;
2478 static DWORD CALLBACK post_message_thread(LPVOID arg)
2480 HWND hWnd = arg;
2481 Sleep(50);
2482 PostMessageA(hWnd, WM_DDE_FIRST, 0, 0);
2483 return 0;
2486 static const char cls_name[] = "cowait_test_class";
2487 static DWORD CALLBACK test_CoWaitForMultipleHandles_thread(LPVOID arg)
2489 HANDLE *handles = arg;
2490 BOOL success;
2491 DWORD index;
2492 HRESULT hr;
2493 HWND hWnd;
2494 MSG msg;
2496 hr = pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
2497 ok(hr == S_OK, "CoInitializeEx failed with error 0x%08x\n", hr);
2499 hWnd = CreateWindowExA(0, cls_name, "Test (thread)", WS_TILEDWINDOW, 0, 0, 640, 480, 0, 0, 0, 0);
2500 ok(hWnd != 0, "CreateWindowExA failed %u\n", GetLastError());
2502 index = 0xdeadbeef;
2503 PostMessageA(hWnd, WM_DDE_FIRST, 0, 0);
2504 hr = CoWaitForMultipleHandles(0, 50, 2, handles, &index);
2505 ok(hr == RPC_S_CALLPENDING, "expected RPC_S_CALLPENDING, got 0x%08x\n", hr);
2506 ok(index==0 || index==0xdeadbeef/* Win 8 */, "expected index 0, got %u\n", index);
2507 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_REMOVE);
2508 ok(!success, "CoWaitForMultipleHandles didn't pump any messages\n");
2510 index = 0xdeadbeef;
2511 PostMessageA(hWnd, WM_USER, 0, 0);
2512 hr = CoWaitForMultipleHandles(0, 50, 2, handles, &index);
2513 ok(hr == RPC_S_CALLPENDING, "expected RPC_S_CALLPENDING, got 0x%08x\n", hr);
2514 ok(index==0 || index==0xdeadbeef/* Win 8 */, "expected index 0, got %u\n", index);
2515 success = PeekMessageA(&msg, hWnd, WM_USER, WM_USER, PM_REMOVE);
2516 ok(success, "CoWaitForMultipleHandles unexpectedly pumped messages\n");
2518 DestroyWindow(hWnd);
2519 CoUninitialize();
2520 return 0;
2523 static void test_CoWaitForMultipleHandles(void)
2525 HANDLE handles[2], thread;
2526 DWORD index, tid;
2527 WNDCLASSEXA wc;
2528 BOOL success;
2529 HRESULT hr;
2530 HWND hWnd;
2531 MSG msg;
2533 hr = pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
2534 ok(hr == S_OK, "CoInitializeEx failed with error 0x%08x\n", hr);
2536 memset(&wc, 0, sizeof(wc));
2537 wc.cbSize = sizeof(wc);
2538 wc.style = CS_VREDRAW | CS_HREDRAW;
2539 wc.hInstance = GetModuleHandleA(0);
2540 wc.hCursor = LoadCursorA(NULL, (LPCSTR)IDC_ARROW);
2541 wc.hbrBackground = NULL;
2542 wc.lpszClassName = cls_name;
2543 wc.lpfnWndProc = DefWindowProcA;
2544 success = RegisterClassExA(&wc) != 0;
2545 ok(success, "RegisterClassExA failed %u\n", GetLastError());
2547 hWnd = CreateWindowExA(0, cls_name, "Test", WS_TILEDWINDOW, 0, 0, 640, 480, 0, 0, 0, 0);
2548 ok(hWnd != 0, "CreateWindowExA failed %u\n", GetLastError());
2549 handles[0] = CreateSemaphoreA(NULL, 1, 1, NULL);
2550 ok(handles[0] != 0, "CreateSemaphoreA failed %u\n", GetLastError());
2551 handles[1] = CreateSemaphoreA(NULL, 1, 1, NULL);
2552 ok(handles[1] != 0, "CreateSemaphoreA failed %u\n", GetLastError());
2554 /* test without flags */
2556 PostMessageA(hWnd, WM_DDE_FIRST, 0, 0);
2557 hr = CoWaitForMultipleHandles(0, 50, 0, handles, NULL);
2558 ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got 0x%08x\n", hr);
2559 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_REMOVE);
2560 ok(success, "CoWaitForMultipleHandles unexpectedly pumped messages\n");
2562 index = 0xdeadbeef;
2563 PostMessageA(hWnd, WM_DDE_FIRST, 0, 0);
2564 hr = CoWaitForMultipleHandles(0, 50, 0, NULL, &index);
2565 ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got 0x%08x\n", hr);
2566 ok(index == 0, "expected index 0, got %u\n", index);
2567 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_REMOVE);
2568 ok(success, "CoWaitForMultipleHandles unexpectedly pumped messages\n");
2570 index = 0xdeadbeef;
2571 PostMessageA(hWnd, WM_DDE_FIRST, 0, 0);
2572 hr = CoWaitForMultipleHandles(0, 50, 0, handles, &index);
2573 ok(hr == RPC_E_NO_SYNC, "expected RPC_E_NO_SYNC, got 0x%08x\n", hr);
2574 ok(index == 0, "expected index 0, got %u\n", index);
2575 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_REMOVE);
2576 ok(success, "CoWaitForMultipleHandles unexpectedly pumped messages\n");
2578 index = 0xdeadbeef;
2579 PostMessageA(hWnd, WM_DDE_FIRST, 0, 0);
2580 hr = CoWaitForMultipleHandles(0, 50, 1, handles, &index);
2581 ok(hr == S_OK, "expected S_OK, got 0x%08x\n", hr);
2582 ok(index == 0, "expected index 0, got %u\n", index);
2583 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_REMOVE);
2584 ok(success, "CoWaitForMultipleHandles unexpectedly pumped messages\n");
2586 index = 0xdeadbeef;
2587 PostMessageA(hWnd, WM_DDE_FIRST, 0, 0);
2588 hr = CoWaitForMultipleHandles(0, 50, 2, handles, &index);
2589 ok(hr == S_OK, "expected S_OK, got 0x%08x\n", hr);
2590 ok(index == 1, "expected index 1, got %u\n", index);
2591 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_REMOVE);
2592 ok(success, "CoWaitForMultipleHandles unexpectedly pumped messages\n");
2594 index = 0xdeadbeef;
2595 PostMessageA(hWnd, WM_DDE_FIRST, 0, 0);
2596 hr = CoWaitForMultipleHandles(0, 50, 2, handles, &index);
2597 ok(hr == RPC_S_CALLPENDING, "expected RPC_S_CALLPENDING, got 0x%08x\n", hr);
2598 ok(index == 0 || broken(index == 0xdeadbeef) /* Win 8 */, "expected index 0, got %u\n", index);
2599 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_REMOVE);
2600 ok(!success, "CoWaitForMultipleHandles didn't pump any messages\n");
2602 /* test PostMessageA/SendMessageA from a different thread */
2604 index = 0xdeadbeef;
2605 thread = CreateThread(NULL, 0, post_message_thread, hWnd, 0, &tid);
2606 ok(thread != NULL, "CreateThread failed, error %u\n", GetLastError());
2607 hr = CoWaitForMultipleHandles(0, 100, 2, handles, &index);
2608 ok(hr == RPC_S_CALLPENDING, "expected RPC_S_CALLPENDING, got 0x%08x\n", hr);
2609 ok(index == 0 || broken(index == 0xdeadbeef) /* Win 8 */, "expected index 0, got %u\n", index);
2610 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_REMOVE);
2611 ok(!success, "CoWaitForMultipleHandles didn't pump any messages\n");
2612 index = WaitForSingleObject(thread, 200);
2613 ok(index == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
2614 CloseHandle(thread);
2616 index = 0xdeadbeef;
2617 thread = CreateThread(NULL, 0, send_message_thread, hWnd, 0, &tid);
2618 ok(thread != NULL, "CreateThread failed, error %u\n", GetLastError());
2619 hr = CoWaitForMultipleHandles(0, 100, 2, handles, &index);
2620 ok(hr == RPC_S_CALLPENDING, "expected RPC_S_CALLPENDING, got 0x%08x\n", hr);
2621 ok(index == 0 || broken(index == 0xdeadbeef) /* Win 8 */, "expected index 0, got %u\n", index);
2622 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_REMOVE);
2623 ok(!success, "CoWaitForMultipleHandles didn't pump any messages\n");
2624 index = WaitForSingleObject(thread, 200);
2625 ok(index == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
2626 CloseHandle(thread);
2628 ReleaseSemaphore(handles[0], 1, NULL);
2629 ReleaseSemaphore(handles[1], 1, NULL);
2631 /* test with COWAIT_WAITALL */
2633 index = 0xdeadbeef;
2634 PostMessageA(hWnd, WM_DDE_FIRST, 0, 0);
2635 hr = CoWaitForMultipleHandles(COWAIT_WAITALL, 50, 2, handles, &index);
2636 ok(hr == S_OK, "expected S_OK, got 0x%08x\n", hr);
2637 ok(index == 0, "expected index 0, got %u\n", index);
2638 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_REMOVE);
2639 ok(success, "CoWaitForMultipleHandles unexpectedly pumped messages\n");
2641 index = 0xdeadbeef;
2642 PostMessageA(hWnd, WM_DDE_FIRST, 0, 0);
2643 hr = CoWaitForMultipleHandles(0, 50, 2, handles, &index);
2644 ok(hr == RPC_S_CALLPENDING, "expected RPC_S_CALLPENDING, got 0x%08x\n", hr);
2645 ok(index == 0 || broken(index == 0xdeadbeef) /* Win 8 */, "expected index 0, got %u\n", index);
2646 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_REMOVE);
2647 ok(!success, "CoWaitForMultipleHandles didn't pump any messages\n");
2649 ReleaseSemaphore(handles[0], 1, NULL);
2650 ReleaseSemaphore(handles[1], 1, NULL);
2652 /* test with COWAIT_ALERTABLE */
2654 index = 0xdeadbeef;
2655 PostMessageA(hWnd, WM_DDE_FIRST, 0, 0);
2656 hr = CoWaitForMultipleHandles(COWAIT_ALERTABLE, 50, 1, handles, &index);
2657 ok(hr == S_OK, "expected S_OK, got 0x%08x\n", hr);
2658 ok(index == 0, "expected index 0, got %u\n", index);
2659 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_REMOVE);
2660 ok(success, "CoWaitForMultipleHandles unexpectedly pumped messages\n");
2662 index = 0xdeadbeef;
2663 PostMessageA(hWnd, WM_DDE_FIRST, 0, 0);
2664 hr = CoWaitForMultipleHandles(COWAIT_ALERTABLE, 50, 2, handles, &index);
2665 ok(hr == S_OK, "expected S_OK, got 0x%08x\n", hr);
2666 ok(index == 1, "expected index 1, got %u\n", index);
2667 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_REMOVE);
2668 ok(success, "CoWaitForMultipleHandles unexpectedly pumped messages\n");
2670 index = 0xdeadbeef;
2671 PostMessageA(hWnd, WM_DDE_FIRST, 0, 0);
2672 hr = CoWaitForMultipleHandles(COWAIT_ALERTABLE, 50, 2, handles, &index);
2673 ok(hr == RPC_S_CALLPENDING, "expected RPC_S_CALLPENDING, got 0x%08x\n", hr);
2674 ok(index == 0 || broken(index == 0xdeadbeef) /* Win 8 */, "expected index 0, got %u\n", index);
2675 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_REMOVE);
2676 ok(!success, "CoWaitForMultipleHandles didn't pump any messages\n");
2678 index = 0xdeadbeef;
2679 PostMessageA(hWnd, WM_DDE_FIRST, 0, 0);
2680 success = QueueUserAPC(apc_test_proc, GetCurrentThread(), 0);
2681 ok(success, "QueueUserAPC failed %u\n", GetLastError());
2682 hr = CoWaitForMultipleHandles(COWAIT_ALERTABLE, 50, 2, handles, &index);
2683 ok(hr == S_OK, "expected S_OK, got 0x%08x\n", hr);
2684 ok(index == WAIT_IO_COMPLETION, "expected index WAIT_IO_COMPLETION, got %u\n", index);
2685 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_REMOVE);
2686 ok(success, "CoWaitForMultipleHandles unexpectedly pumped messages\n");
2688 /* test with COWAIT_INPUTAVAILABLE (semaphores are still locked) */
2690 index = 0xdeadbeef;
2691 PostMessageA(hWnd, WM_DDE_FIRST, 0, 0);
2692 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_NOREMOVE);
2693 ok(success, "PeekMessageA returned FALSE\n");
2694 hr = CoWaitForMultipleHandles(0, 50, 2, handles, &index);
2695 ok(hr == RPC_S_CALLPENDING, "expected RPC_S_CALLPENDING, got 0x%08x\n", hr);
2696 ok(index == 0 || broken(index == 0xdeadbeef) /* Win 8 */, "expected index 0, got %u\n", index);
2697 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_REMOVE);
2698 ok(!success, "CoWaitForMultipleHandles didn't pump any messages\n");
2700 index = 0xdeadbeef;
2701 PostMessageA(hWnd, WM_DDE_FIRST, 0, 0);
2702 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_NOREMOVE);
2703 ok(success, "PeekMessageA returned FALSE\n");
2704 thread = CreateThread(NULL, 0, release_semaphore_thread, handles[1], 0, &tid);
2705 ok(thread != NULL, "CreateThread failed, error %u\n", GetLastError());
2706 hr = CoWaitForMultipleHandles(COWAIT_INPUTAVAILABLE, 50, 2, handles, &index);
2707 ok(hr == RPC_S_CALLPENDING || broken(hr == E_INVALIDARG) || broken(hr == S_OK) /* Win 8 */,
2708 "expected RPC_S_CALLPENDING, got 0x%08x\n", hr);
2709 if (hr != S_OK) ReleaseSemaphore(handles[1], 1, NULL);
2710 ok(index == 0 || broken(index == 1) /* Win 8 */, "expected index 0, got %u\n", index);
2711 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_REMOVE);
2712 ok(!success || broken(success && hr == E_INVALIDARG),
2713 "CoWaitForMultipleHandles didn't pump any messages\n");
2714 index = WaitForSingleObject(thread, 200);
2715 ok(index == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
2716 CloseHandle(thread);
2718 /* test behaviour of WM_QUIT (semaphores are still locked) */
2720 PostMessageA(hWnd, WM_QUIT, 40, 0);
2721 memset(&msg, 0, sizeof(msg));
2722 success = PeekMessageA(&msg, hWnd, WM_QUIT, WM_QUIT, PM_REMOVE);
2723 ok(success, "PeekMessageA failed, error %u\n", GetLastError());
2724 ok(msg.message == WM_QUIT, "expected msg.message = WM_QUIT, got %u\n", msg.message);
2725 ok(msg.wParam == 40, "expected msg.wParam = 40, got %lu\n", msg.wParam);
2726 success = PeekMessageA(&msg, hWnd, WM_QUIT, WM_QUIT, PM_REMOVE);
2727 ok(!success, "PeekMessageA succeeded\n");
2729 index = 0xdeadbeef;
2730 PostMessageA(hWnd, WM_DDE_FIRST, 0, 0);
2731 PostMessageA(hWnd, WM_QUIT, 41, 0);
2732 thread = CreateThread(NULL, 0, post_message_thread, hWnd, 0, &tid);
2733 ok(thread != NULL, "CreateThread failed, error %u\n", GetLastError());
2734 hr = CoWaitForMultipleHandles(0, 100, 2, handles, &index);
2735 ok(hr == RPC_S_CALLPENDING, "expected RPC_S_CALLPENDING, got 0x%08x\n", hr);
2736 ok(index == 0 || broken(index == 0xdeadbeef) /* Win 8 */, "expected index 0, got %u\n", index);
2737 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_REMOVE);
2738 todo_wine
2739 ok(success || broken(!success) /* Win 2000/XP/8 */, "PeekMessageA failed, error %u\n", GetLastError());
2740 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_REMOVE);
2741 ok(!success, "PeekMessageA succeeded\n");
2742 memset(&msg, 0, sizeof(msg));
2743 success = PeekMessageA(&msg, hWnd, WM_QUIT, WM_QUIT, PM_REMOVE);
2744 todo_wine
2745 ok(!success || broken(success) /* Win 2000/XP/8 */, "PeekMessageA succeeded\n");
2746 if (success)
2748 ok(msg.message == WM_QUIT, "expected msg.message = WM_QUIT, got %u\n", msg.message);
2749 ok(msg.wParam == 41, "expected msg.wParam = 41, got %lu\n", msg.wParam);
2751 index = WaitForSingleObject(thread, 200);
2752 ok(index == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
2753 CloseHandle(thread);
2755 index = 0xdeadbeef;
2756 PostMessageA(hWnd, WM_DDE_FIRST, 0, 0);
2757 PostMessageA(hWnd, WM_QUIT, 42, 0);
2758 thread = CreateThread(NULL, 0, send_message_thread, hWnd, 0, &tid);
2759 ok(thread != NULL, "CreateThread failed, error %u\n", GetLastError());
2760 hr = CoWaitForMultipleHandles(0, 100, 2, handles, &index);
2761 ok(hr == RPC_S_CALLPENDING, "expected RPC_S_CALLPENDING, got 0x%08x\n", hr);
2762 ok(index == 0 || broken(index == 0xdeadbeef) /* Win 8 */, "expected index 0, got %u\n", index);
2763 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_REMOVE);
2764 ok(!success, "CoWaitForMultipleHandles didn't pump all WM_DDE_FIRST messages\n");
2765 memset(&msg, 0, sizeof(msg));
2766 success = PeekMessageA(&msg, hWnd, WM_QUIT, WM_QUIT, PM_REMOVE);
2767 ok(success, "PeekMessageA failed, error %u\n", GetLastError());
2768 ok(msg.message == WM_QUIT, "expected msg.message = WM_QUIT, got %u\n", msg.message);
2769 ok(msg.wParam == 42, "expected msg.wParam = 42, got %lu\n", msg.wParam);
2770 index = WaitForSingleObject(thread, 200);
2771 ok(index == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
2772 CloseHandle(thread);
2774 PostQuitMessage(43);
2775 memset(&msg, 0, sizeof(msg));
2776 success = PeekMessageA(&msg, hWnd, WM_QUIT, WM_QUIT, PM_REMOVE);
2777 ok(success || broken(!success) /* Win 8 */, "PeekMessageA failed, error %u\n", GetLastError());
2778 if (!success)
2779 win_skip("PostQuitMessage didn't queue a WM_QUIT message, skipping tests\n");
2780 else
2782 ok(msg.message == WM_QUIT, "expected msg.message = WM_QUIT, got %u\n", msg.message);
2783 ok(msg.wParam == 43, "expected msg.wParam = 43, got %lu\n", msg.wParam);
2784 success = PeekMessageA(&msg, hWnd, WM_QUIT, WM_QUIT, PM_REMOVE);
2785 ok(!success, "PeekMessageA succeeded\n");
2787 index = 0xdeadbeef;
2788 PostMessageA(hWnd, WM_DDE_FIRST, 0, 0);
2789 PostQuitMessage(44);
2790 thread = CreateThread(NULL, 0, post_message_thread, hWnd, 0, &tid);
2791 ok(thread != NULL, "CreateThread failed, error %u\n", GetLastError());
2792 hr = CoWaitForMultipleHandles(0, 100, 2, handles, &index);
2793 ok(hr == RPC_S_CALLPENDING, "expected RPC_S_CALLPENDING, got 0x%08x\n", hr);
2794 ok(index == 0 || broken(index == 0xdeadbeef) /* Win 8 */, "expected index 0, got %u\n", index);
2795 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_REMOVE);
2796 ok(success, "PeekMessageA failed, error %u\n", GetLastError());
2797 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_REMOVE);
2798 todo_wine
2799 ok(!success, "PeekMessageA succeeded\n");
2800 success = PeekMessageA(&msg, hWnd, WM_QUIT, WM_QUIT, PM_REMOVE);
2801 todo_wine
2802 ok(!success, "CoWaitForMultipleHandles didn't remove WM_QUIT messages\n");
2803 index = WaitForSingleObject(thread, 200);
2804 ok(index == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
2805 CloseHandle(thread);
2807 index = 0xdeadbeef;
2808 PostMessageA(hWnd, WM_DDE_FIRST, 0, 0);
2809 PostQuitMessage(45);
2810 thread = CreateThread(NULL, 0, send_message_thread, hWnd, 0, &tid);
2811 ok(thread != NULL, "CreateThread failed, error %u\n", GetLastError());
2812 hr = CoWaitForMultipleHandles(0, 100, 2, handles, &index);
2813 ok(hr == RPC_S_CALLPENDING, "expected RPC_S_CALLPENDING, got 0x%08x\n", hr);
2814 ok(index == 0 || broken(index == 0xdeadbeef) /* Win 8 */, "expected index 0, got %u\n", index);
2815 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_REMOVE);
2816 ok(success, "PeekMessageA failed, error %u\n", GetLastError());
2817 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_REMOVE);
2818 todo_wine
2819 ok(!success, "PeekMessageA succeeded\n");
2820 success = PeekMessageA(&msg, hWnd, WM_QUIT, WM_QUIT, PM_REMOVE);
2821 ok(!success, "CoWaitForMultipleHandles didn't remove WM_QUIT messages\n");
2822 index = WaitForSingleObject(thread, 200);
2823 ok(index == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
2824 CloseHandle(thread);
2827 /* test message pumping when CoWaitForMultipleHandles is called from non main apartment thread */
2828 thread = CreateThread(NULL, 0, test_CoWaitForMultipleHandles_thread, handles, 0, &tid);
2829 index = WaitForSingleObject(thread, 500);
2830 ok(index == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
2831 CloseHandle(thread);
2833 CloseHandle(handles[0]);
2834 CloseHandle(handles[1]);
2835 DestroyWindow(hWnd);
2837 success = UnregisterClassA(cls_name, GetModuleHandleA(0));
2838 ok(success, "UnregisterClass failed %u\n", GetLastError());
2840 CoUninitialize();
2843 static void test_CoGetMalloc(void)
2845 IMalloc *imalloc;
2846 HRESULT hr;
2848 if (0) /* crashes on native */
2849 hr = CoGetMalloc(0, NULL);
2851 imalloc = (void*)0xdeadbeef;
2852 hr = CoGetMalloc(0, &imalloc);
2853 ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
2854 ok(imalloc == NULL, "got %p\n", imalloc);
2856 imalloc = (void*)0xdeadbeef;
2857 hr = CoGetMalloc(MEMCTX_SHARED, &imalloc);
2858 ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
2859 ok(imalloc == NULL, "got %p\n", imalloc);
2861 imalloc = (void*)0xdeadbeef;
2862 hr = CoGetMalloc(MEMCTX_MACSYSTEM, &imalloc);
2863 ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
2864 ok(imalloc == NULL, "got %p\n", imalloc);
2866 imalloc = (void*)0xdeadbeef;
2867 hr = CoGetMalloc(MEMCTX_UNKNOWN, &imalloc);
2868 ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
2869 ok(imalloc == NULL, "got %p\n", imalloc);
2871 imalloc = (void*)0xdeadbeef;
2872 hr = CoGetMalloc(MEMCTX_SAME, &imalloc);
2873 ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
2874 ok(imalloc == NULL, "got %p\n", imalloc);
2876 imalloc = NULL;
2877 hr = CoGetMalloc(MEMCTX_TASK, &imalloc);
2878 ok(hr == S_OK, "got 0x%08x\n", hr);
2879 ok(imalloc != NULL, "got %p\n", imalloc);
2880 IMalloc_Release(imalloc);
2883 static void test_CoGetApartmentType(void)
2885 APTTYPEQUALIFIER qualifier;
2886 APTTYPE type;
2887 HRESULT hr;
2889 if (!pCoGetApartmentType)
2891 win_skip("CoGetApartmentType not present\n");
2892 return;
2895 hr = pCoGetApartmentType(NULL, NULL);
2896 ok(hr == E_INVALIDARG, "CoGetApartmentType succeeded, error: 0x%08x\n", hr);
2898 type = 0xdeadbeef;
2899 hr = pCoGetApartmentType(&type, NULL);
2900 ok(hr == E_INVALIDARG, "CoGetApartmentType succeeded, error: 0x%08x\n", hr);
2901 ok(type == 0xdeadbeef, "Expected 0xdeadbeef, got %u\n", type);
2903 qualifier = 0xdeadbeef;
2904 hr = pCoGetApartmentType(NULL, &qualifier);
2905 ok(hr == E_INVALIDARG, "CoGetApartmentType succeeded, error: 0x%08x\n", hr);
2906 ok(qualifier == 0xdeadbeef, "Expected 0xdeadbeef, got %u\n", qualifier);
2908 type = 0xdeadbeef;
2909 qualifier = 0xdeadbeef;
2910 hr = pCoGetApartmentType(&type, &qualifier);
2911 ok(hr == CO_E_NOTINITIALIZED, "CoGetApartmentType succeeded, error: 0x%08x\n", hr);
2912 ok(type == APTTYPE_CURRENT, "Expected APTTYPE_CURRENT, got %u\n", type);
2913 ok(qualifier == APTTYPEQUALIFIER_NONE, "Expected APTTYPEQUALIFIER_NONE, got %u\n", qualifier);
2915 type = 0xdeadbeef;
2916 qualifier = 0xdeadbeef;
2917 hr = pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
2918 ok(hr == S_OK, "CoInitializeEx failed, error: 0x%08x\n", hr);
2919 hr = pCoGetApartmentType(&type, &qualifier);
2920 ok(hr == S_OK, "CoGetApartmentType failed, error: 0x%08x\n", hr);
2921 ok(type == APTTYPE_MAINSTA, "Expected APTTYPE_MAINSTA, got %u\n", type);
2922 ok(qualifier == APTTYPEQUALIFIER_NONE, "Expected APTTYPEQUALIFIER_NONE, got %u\n", qualifier);
2923 CoUninitialize();
2925 type = 0xdeadbeef;
2926 qualifier = 0xdeadbeef;
2927 hr = pCoInitializeEx(NULL, COINIT_MULTITHREADED);
2928 ok(hr == S_OK, "CoInitializeEx failed, error: 0x%08x\n", hr);
2929 hr = pCoGetApartmentType(&type, &qualifier);
2930 ok(hr == S_OK, "CoGetApartmentType failed, error: 0x%08x\n", hr);
2931 ok(type == APTTYPE_MTA, "Expected APTTYPE_MTA, got %u\n", type);
2932 ok(qualifier == APTTYPEQUALIFIER_NONE, "Expected APTTYPEQUALIFIER_NONE, got %u\n", qualifier);
2933 CoUninitialize();
2937 static HRESULT WINAPI testspy_QI(IMallocSpy *iface, REFIID riid, void **obj)
2939 if (IsEqualIID(riid, &IID_IMallocSpy) || IsEqualIID(riid, &IID_IUnknown))
2941 *obj = iface;
2942 IMallocSpy_AddRef(iface);
2943 return S_OK;
2946 return E_NOINTERFACE;
2949 static ULONG WINAPI testspy_AddRef(IMallocSpy *iface)
2951 return 2;
2954 static ULONG WINAPI testspy_Release(IMallocSpy *iface)
2956 return 1;
2959 static SIZE_T WINAPI testspy_PreAlloc(IMallocSpy *iface, SIZE_T cb)
2961 ok(0, "unexpected call\n");
2962 return 0;
2965 static void* WINAPI testspy_PostAlloc(IMallocSpy *iface, void *ptr)
2967 ok(0, "unexpected call\n");
2968 return NULL;
2971 static void* WINAPI testspy_PreFree(IMallocSpy *iface, void *ptr, BOOL spyed)
2973 ok(0, "unexpected call\n");
2974 return NULL;
2977 static void WINAPI testspy_PostFree(IMallocSpy *iface, BOOL spyed)
2979 ok(0, "unexpected call\n");
2982 static SIZE_T WINAPI testspy_PreRealloc(IMallocSpy *iface, void *ptr, SIZE_T cb, void **newptr, BOOL spyed)
2984 ok(0, "unexpected call\n");
2985 return 0;
2988 static void* WINAPI testspy_PostRealloc(IMallocSpy *iface, void *ptr, BOOL spyed)
2990 ok(0, "unexpected call\n");
2991 return NULL;
2994 static void* WINAPI testspy_PreGetSize(IMallocSpy *iface, void *ptr, BOOL spyed)
2996 ok(0, "unexpected call\n");
2997 return NULL;
3000 static SIZE_T WINAPI testspy_PostGetSize(IMallocSpy *iface, SIZE_T actual, BOOL spyed)
3002 ok(0, "unexpected call\n");
3003 return 0;
3006 static void* WINAPI testspy_PreDidAlloc(IMallocSpy *iface, void *ptr, BOOL spyed)
3008 ok(0, "unexpected call\n");
3009 return NULL;
3012 static int WINAPI testspy_PostDidAlloc(IMallocSpy *iface, void *ptr, BOOL spyed, int actual)
3014 ok(0, "unexpected call\n");
3015 return 0;
3018 static void WINAPI testspy_PreHeapMinimize(IMallocSpy *iface)
3020 ok(0, "unexpected call\n");
3023 static void WINAPI testspy_PostHeapMinimize(IMallocSpy *iface)
3025 ok(0, "unexpected call\n");
3028 static const IMallocSpyVtbl testspyvtbl =
3030 testspy_QI,
3031 testspy_AddRef,
3032 testspy_Release,
3033 testspy_PreAlloc,
3034 testspy_PostAlloc,
3035 testspy_PreFree,
3036 testspy_PostFree,
3037 testspy_PreRealloc,
3038 testspy_PostRealloc,
3039 testspy_PreGetSize,
3040 testspy_PostGetSize,
3041 testspy_PreDidAlloc,
3042 testspy_PostDidAlloc,
3043 testspy_PreHeapMinimize,
3044 testspy_PostHeapMinimize
3047 static IMallocSpy testspy = { &testspyvtbl };
3049 static void test_IMallocSpy(void)
3051 IMalloc *imalloc;
3052 HRESULT hr;
3054 hr = CoRegisterMallocSpy(&testspy);
3055 ok(hr == S_OK, "got 0x%08x\n", hr);
3057 imalloc = NULL;
3058 hr = CoGetMalloc(MEMCTX_TASK, &imalloc);
3059 ok(hr == S_OK, "got 0x%08x\n", hr);
3060 ok(imalloc != NULL, "got %p\n", imalloc);
3062 IMalloc_Free(imalloc, NULL);
3064 IMalloc_Release(imalloc);
3066 hr = CoRevokeMallocSpy();
3067 ok(hr == S_OK, "got 0x%08x\n", hr);
3070 static void init_funcs(void)
3072 HMODULE hOle32 = GetModuleHandleA("ole32");
3073 HMODULE hAdvapi32 = GetModuleHandleA("advapi32");
3074 HMODULE hkernel32 = GetModuleHandleA("kernel32");
3076 pCoGetObjectContext = (void*)GetProcAddress(hOle32, "CoGetObjectContext");
3077 pCoSwitchCallContext = (void*)GetProcAddress(hOle32, "CoSwitchCallContext");
3078 pCoGetTreatAsClass = (void*)GetProcAddress(hOle32,"CoGetTreatAsClass");
3079 pCoTreatAsClass = (void*)GetProcAddress(hOle32,"CoTreatAsClass");
3080 pCoGetContextToken = (void*)GetProcAddress(hOle32, "CoGetContextToken");
3081 pCoGetApartmentType = (void*)GetProcAddress(hOle32, "CoGetApartmentType");
3082 pRegDeleteKeyExA = (void*)GetProcAddress(hAdvapi32, "RegDeleteKeyExA");
3083 pRegOverridePredefKey = (void*)GetProcAddress(hAdvapi32, "RegOverridePredefKey");
3084 pCoInitializeEx = (void*)GetProcAddress(hOle32, "CoInitializeEx");
3086 pActivateActCtx = (void*)GetProcAddress(hkernel32, "ActivateActCtx");
3087 pCreateActCtxW = (void*)GetProcAddress(hkernel32, "CreateActCtxW");
3088 pDeactivateActCtx = (void*)GetProcAddress(hkernel32, "DeactivateActCtx");
3089 pIsWow64Process = (void*)GetProcAddress(hkernel32, "IsWow64Process");
3090 pReleaseActCtx = (void*)GetProcAddress(hkernel32, "ReleaseActCtx");
3093 START_TEST(compobj)
3095 init_funcs();
3097 if (!pCoInitializeEx)
3099 trace("You need DCOM95 installed to run this test\n");
3100 return;
3103 if (!pCreateActCtxW)
3104 win_skip("Activation contexts are not supported, some tests will be skipped.\n");
3106 test_ProgIDFromCLSID();
3107 test_CLSIDFromProgID();
3108 test_CLSIDFromString();
3109 test_IIDFromString();
3110 test_StringFromGUID2();
3111 test_CoCreateInstance();
3112 test_ole_menu();
3113 test_CoGetClassObject();
3114 test_CoCreateInstanceEx();
3115 test_CoRegisterMessageFilter();
3116 test_CoRegisterPSClsid();
3117 test_CoGetPSClsid();
3118 test_CoUnmarshalInterface();
3119 test_CoGetInterfaceAndReleaseStream();
3120 test_CoMarshalInterface();
3121 test_CoMarshalInterThreadInterfaceInStream();
3122 test_CoRegisterClassObject();
3123 test_registered_object_thread_affinity();
3124 test_CoFreeUnusedLibraries();
3125 test_CoGetObjectContext();
3126 test_CoGetCallContext();
3127 test_CoGetContextToken();
3128 test_TreatAsClass();
3129 test_CoInitializeEx();
3130 test_OleInitialize_InitCounting();
3131 test_OleRegGetMiscStatus();
3132 test_CoCreateGuid();
3133 test_CoWaitForMultipleHandles();
3134 test_CoGetMalloc();
3135 test_OleRegGetUserType();
3136 test_CoGetApartmentType();
3137 test_IMallocSpy();