[OLE32] Sync with Wine Staging 4.0. CORE-15682
[reactos.git] / modules / rostests / winetests / ole32 / compobj.c
1 /*
2 * Component Object Tests
3 *
4 * Copyright 2005 Robert Shearman
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #define COBJMACROS
22 #define CONST_VTABLE
23
24 #include <stdarg.h>
25 #include <stdio.h>
26
27 #include "windef.h"
28 #include "winbase.h"
29 #define USE_COM_CONTEXT_DEF
30 #ifndef __REACTOS__
31 #include "initguid.h"
32 #endif
33 #include "objbase.h"
34 #include "shlguid.h"
35 #include "urlmon.h" /* for CLSID_FileProtocol */
36 #include "dde.h"
37 #include "cguid.h"
38
39 #include "ctxtcall.h"
40
41 #include "wine/test.h"
42
43 #ifdef __REACTOS__
44 #include <initguid.h>
45 #endif
46
47 #define DEFINE_EXPECT(func) \
48 static BOOL expect_ ## func = FALSE, called_ ## func = FALSE
49
50 #define SET_EXPECT(func) \
51 expect_ ## func = TRUE
52
53 #define CHECK_EXPECT2(func) \
54 do { \
55 ok(expect_ ##func, "unexpected call " #func "\n"); \
56 called_ ## func = TRUE; \
57 }while(0)
58
59 #define CHECK_EXPECT(func) \
60 do { \
61 CHECK_EXPECT2(func); \
62 expect_ ## func = FALSE; \
63 }while(0)
64
65 #define CHECK_CALLED(func) \
66 do { \
67 ok(called_ ## func, "expected " #func "\n"); \
68 expect_ ## func = called_ ## func = FALSE; \
69 }while(0)
70
71 DEFINE_EXPECT(CreateStub);
72
73 /* functions that are not present on all versions of Windows */
74 static HRESULT (WINAPI * pCoInitializeEx)(LPVOID lpReserved, DWORD dwCoInit);
75 static HRESULT (WINAPI * pCoGetObjectContext)(REFIID riid, LPVOID *ppv);
76 static HRESULT (WINAPI * pCoSwitchCallContext)(IUnknown *pObject, IUnknown **ppOldObject);
77 static HRESULT (WINAPI * pCoGetTreatAsClass)(REFCLSID clsidOld, LPCLSID pClsidNew);
78 static HRESULT (WINAPI * pCoTreatAsClass)(REFCLSID clsidOld, REFCLSID pClsidNew);
79 static HRESULT (WINAPI * pCoGetContextToken)(ULONG_PTR *token);
80 static HRESULT (WINAPI * pCoGetApartmentType)(APTTYPE *type, APTTYPEQUALIFIER *qualifier);
81 static LONG (WINAPI * pRegDeleteKeyExA)(HKEY, LPCSTR, REGSAM, DWORD);
82 static LONG (WINAPI * pRegOverridePredefKey)(HKEY key, HKEY override);
83
84 static BOOL (WINAPI *pActivateActCtx)(HANDLE,ULONG_PTR*);
85 static HANDLE (WINAPI *pCreateActCtxW)(PCACTCTXW);
86 static BOOL (WINAPI *pDeactivateActCtx)(DWORD,ULONG_PTR);
87 static BOOL (WINAPI *pIsWow64Process)(HANDLE, LPBOOL);
88 static void (WINAPI *pReleaseActCtx)(HANDLE);
89
90 #define ok_ole_success(hr, func) ok(hr == S_OK, func " failed with error 0x%08x\n", hr)
91 #define ok_more_than_one_lock() ok(cLocks > 0, "Number of locks should be > 0, but actually is %d\n", cLocks)
92 #define ok_no_locks() ok(cLocks == 0, "Number of locks should be 0, but actually is %d\n", cLocks)
93
94 static const CLSID CLSID_non_existent = { 0x12345678, 0x1234, 0x1234, { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 } };
95 static const CLSID CLSID_StdFont = { 0x0be35203, 0x8f91, 0x11ce, { 0x9d, 0xe3, 0x00, 0xaa, 0x00, 0x4b, 0xb8, 0x51 } };
96 static const GUID IID_Testiface = { 0x22222222, 0x1234, 0x1234, { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 } };
97 static const GUID IID_Testiface2 = { 0x32222222, 0x1234, 0x1234, { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 } };
98 static const GUID IID_Testiface3 = { 0x42222222, 0x1234, 0x1234, { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 } };
99 static const GUID IID_Testiface4 = { 0x52222222, 0x1234, 0x1234, { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 } };
100 static const GUID IID_Testiface5 = { 0x62222222, 0x1234, 0x1234, { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 } };
101 static const GUID IID_Testiface6 = { 0x72222222, 0x1234, 0x1234, { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 } };
102 static const GUID IID_TestPS = { 0x66666666, 0x8888, 0x7777, { 0x66, 0x66, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55 } };
103
104 DEFINE_GUID(CLSID_InProcFreeMarshaler, 0x0000033a,0x0000,0x0000,0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46);
105 DEFINE_GUID(CLSID_testclsid, 0xacd014c7,0x9535,0x4fac,0x8b,0x53,0xa4,0x8c,0xa7,0xf4,0xd7,0x26);
106 DEFINE_GUID(CLSID_GlobalOptions, 0x0000034b,0x0000,0x0000,0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46);
107
108 static const WCHAR stdfont[] = {'S','t','d','F','o','n','t',0};
109 static const WCHAR wszNonExistent[] = {'N','o','n','E','x','i','s','t','e','n','t',0};
110 static const WCHAR wszCLSID_StdFont[] =
111 {
112 '{','0','b','e','3','5','2','0','3','-','8','f','9','1','-','1','1','c','e','-',
113 '9','d','e','3','-','0','0','a','a','0','0','4','b','b','8','5','1','}',0
114 };
115 static const WCHAR progidW[] = {'P','r','o','g','I','d','.','P','r','o','g','I','d',0};
116 static const WCHAR cf_brokenW[] = {'{','0','0','0','0','0','0','0','1','-','0','0','0','0','-','0','0','0','0','-',
117 'c','0','0','0','-','0','0','0','0','0','0','0','0','0','0','4','6','}','a',0};
118
119 DEFINE_GUID(IID_IWineTest, 0x5201163f, 0x8164, 0x4fd0, 0xa1, 0xa2, 0x5d, 0x5a, 0x36, 0x54, 0xd3, 0xbd);
120 DEFINE_GUID(CLSID_WineOOPTest, 0x5201163f, 0x8164, 0x4fd0, 0xa1, 0xa2, 0x5d, 0x5a, 0x36, 0x54, 0xd3, 0xbd);
121
122 static LONG cLocks;
123
124 static void LockModule(void)
125 {
126 InterlockedIncrement(&cLocks);
127 }
128
129 static void UnlockModule(void)
130 {
131 InterlockedDecrement(&cLocks);
132 }
133
134 static HRESULT WINAPI Test_IClassFactory_QueryInterface(
135 LPCLASSFACTORY iface,
136 REFIID riid,
137 LPVOID *ppvObj)
138 {
139 if (ppvObj == NULL) return E_POINTER;
140
141 if (IsEqualGUID(riid, &IID_IUnknown) ||
142 IsEqualGUID(riid, &IID_IClassFactory))
143 {
144 *ppvObj = iface;
145 IClassFactory_AddRef(iface);
146 return S_OK;
147 }
148
149 *ppvObj = NULL;
150 return E_NOINTERFACE;
151 }
152
153 static ULONG WINAPI Test_IClassFactory_AddRef(LPCLASSFACTORY iface)
154 {
155 LockModule();
156 return 2; /* non-heap-based object */
157 }
158
159 static ULONG WINAPI Test_IClassFactory_Release(LPCLASSFACTORY iface)
160 {
161 UnlockModule();
162 return 1; /* non-heap-based object */
163 }
164
165 static IID create_instance_iid;
166 static HRESULT WINAPI Test_IClassFactory_CreateInstance(
167 LPCLASSFACTORY iface,
168 IUnknown *pUnkOuter,
169 REFIID riid,
170 LPVOID *ppvObj)
171 {
172 *ppvObj = NULL;
173 create_instance_iid = *riid;
174 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
175 return E_NOINTERFACE;
176 }
177
178 static HRESULT WINAPI Test_IClassFactory_LockServer(
179 LPCLASSFACTORY iface,
180 BOOL fLock)
181 {
182 return S_OK;
183 }
184
185 static const IClassFactoryVtbl TestClassFactory_Vtbl =
186 {
187 Test_IClassFactory_QueryInterface,
188 Test_IClassFactory_AddRef,
189 Test_IClassFactory_Release,
190 Test_IClassFactory_CreateInstance,
191 Test_IClassFactory_LockServer
192 };
193
194 static IClassFactory Test_ClassFactory = { &TestClassFactory_Vtbl };
195
196 static WCHAR manifest_path[MAX_PATH];
197
198 static BOOL create_manifest_file(const char *filename, const char *manifest)
199 {
200 int manifest_len;
201 DWORD size;
202 HANDLE file;
203 WCHAR path[MAX_PATH];
204
205 MultiByteToWideChar( CP_ACP, 0, filename, -1, path, MAX_PATH );
206 GetFullPathNameW(path, sizeof(manifest_path)/sizeof(WCHAR), manifest_path, NULL);
207
208 manifest_len = strlen(manifest);
209 file = CreateFileW(path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
210 FILE_ATTRIBUTE_NORMAL, NULL);
211 ok(file != INVALID_HANDLE_VALUE, "CreateFile failed: %u\n", GetLastError());
212 if(file == INVALID_HANDLE_VALUE)
213 return FALSE;
214 WriteFile(file, manifest, manifest_len, &size, NULL);
215 CloseHandle(file);
216
217 return TRUE;
218 }
219
220 static HANDLE activate_context(const char *manifest, ULONG_PTR *cookie)
221 {
222 WCHAR path[MAX_PATH];
223 ACTCTXW actctx;
224 HANDLE handle;
225 BOOL ret;
226
227 if (!pCreateActCtxW) return NULL;
228
229 create_manifest_file("file.manifest", manifest);
230
231 MultiByteToWideChar( CP_ACP, 0, "file.manifest", -1, path, MAX_PATH );
232 memset(&actctx, 0, sizeof(ACTCTXW));
233 actctx.cbSize = sizeof(ACTCTXW);
234 actctx.lpSource = path;
235
236 handle = pCreateActCtxW(&actctx);
237 ok(handle != INVALID_HANDLE_VALUE || broken(handle == INVALID_HANDLE_VALUE) /* some old XP/2k3 versions */,
238 "handle == INVALID_HANDLE_VALUE, error %u\n", GetLastError());
239 if (handle == INVALID_HANDLE_VALUE)
240 {
241 win_skip("activation context generation failed, some tests will be skipped\n");
242 handle = NULL;
243 }
244
245 ok(actctx.cbSize == sizeof(ACTCTXW), "actctx.cbSize=%d\n", actctx.cbSize);
246 ok(actctx.dwFlags == 0, "actctx.dwFlags=%d\n", actctx.dwFlags);
247 ok(actctx.lpSource == path, "actctx.lpSource=%p\n", actctx.lpSource);
248 ok(actctx.wProcessorArchitecture == 0, "actctx.wProcessorArchitecture=%d\n", actctx.wProcessorArchitecture);
249 ok(actctx.wLangId == 0, "actctx.wLangId=%d\n", actctx.wLangId);
250 ok(actctx.lpAssemblyDirectory == NULL, "actctx.lpAssemblyDirectory=%p\n", actctx.lpAssemblyDirectory);
251 ok(actctx.lpResourceName == NULL, "actctx.lpResourceName=%p\n", actctx.lpResourceName);
252 ok(actctx.lpApplicationName == NULL, "actctx.lpApplicationName=%p\n", actctx.lpApplicationName);
253 ok(actctx.hModule == NULL, "actctx.hModule=%p\n", actctx.hModule);
254
255 DeleteFileA("file.manifest");
256
257 if (handle)
258 {
259 ret = pActivateActCtx(handle, cookie);
260 ok(ret, "ActivateActCtx failed: %u\n", GetLastError());
261 }
262
263 return handle;
264 }
265
266 static const char actctx_manifest[] =
267 "<assembly xmlns=\"urn:schemas-microsoft-com:asm.v1\" manifestVersion=\"1.0\">"
268 "<assemblyIdentity version=\"1.2.3.4\" name=\"Wine.Test\" type=\"win32\""
269 " publicKeyToken=\"6595b6414666f1df\" />"
270 "<file name=\"testlib.dll\">"
271 " <comClass"
272 " clsid=\"{0000033a-0000-0000-c000-000000000046}\""
273 " progid=\"FTMarshal\""
274 " />"
275 " <comClass"
276 " clsid=\"{5201163f-8164-4fd0-a1a2-5d5a3654d3bd}\""
277 " progid=\"WineOOPTest\""
278 " />"
279 " <comClass description=\"Test com class\""
280 " clsid=\"{12345678-1234-1234-1234-56789abcdef0}\""
281 " progid=\"ProgId.ProgId\""
282 " miscStatusIcon=\"recomposeonresize\""
283 " />"
284 " <comClass description=\"CustomFont Description\" clsid=\"{0be35203-8f91-11ce-9de3-00aa004bb851}\""
285 " progid=\"CustomFont\""
286 " miscStatusIcon=\"recomposeonresize\""
287 " miscStatusContent=\"insideout\""
288 " />"
289 " <comClass description=\"StdFont Description\" clsid=\"{0be35203-8f91-11ce-9de3-00aa004bb852}\""
290 " progid=\"StdFont\""
291 " />"
292 " <comClass clsid=\"{62222222-1234-1234-1234-56789abcdef0}\" >"
293 " <progid>ProgId.ProgId.1</progid>"
294 " </comClass>"
295 " <comInterfaceProxyStub "
296 " name=\"Iifaceps\""
297 " iid=\"{22222222-1234-1234-1234-56789abcdef0}\""
298 " proxyStubClsid32=\"{66666666-8888-7777-6666-555555555555}\""
299 " />"
300 "</file>"
301 " <comInterfaceExternalProxyStub "
302 " name=\"Iifaceps2\""
303 " iid=\"{32222222-1234-1234-1234-56789abcdef0}\""
304 " />"
305 " <comInterfaceExternalProxyStub "
306 " name=\"Iifaceps3\""
307 " iid=\"{42222222-1234-1234-1234-56789abcdef0}\""
308 " proxyStubClsid32=\"{66666666-8888-7777-6666-555555555555}\""
309 " />"
310 " <comInterfaceExternalProxyStub "
311 " name=\"Iifaceps4\""
312 " iid=\"{52222222-1234-1234-1234-56789abcdef0}\""
313 " proxyStubClsid32=\"{00000000-0000-0000-0000-000000000000}\""
314 " />"
315 " <clrClass "
316 " clsid=\"{72222222-1234-1234-1234-56789abcdef0}\""
317 " name=\"clrclass\""
318 " >"
319 " <progid>clrprogid.1</progid>"
320 " </clrClass>"
321 "</assembly>";
322
323 DEFINE_GUID(CLSID_Testclass, 0x12345678, 0x1234, 0x1234, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0);
324
325 static void test_ProgIDFromCLSID(void)
326 {
327 ULONG_PTR cookie = 0;
328 LPWSTR progid;
329 HANDLE handle;
330 HRESULT hr;
331
332 hr = ProgIDFromCLSID(&CLSID_StdFont, &progid);
333 ok(hr == S_OK, "ProgIDFromCLSID failed with error 0x%08x\n", hr);
334 if (hr == S_OK)
335 {
336 ok(!lstrcmpiW(progid, stdfont), "Didn't get expected prog ID\n");
337 CoTaskMemFree(progid);
338 }
339
340 progid = (LPWSTR)0xdeadbeef;
341 hr = ProgIDFromCLSID(&CLSID_non_existent, &progid);
342 ok(hr == REGDB_E_CLASSNOTREG, "ProgIDFromCLSID returned %08x\n", hr);
343 ok(progid == NULL, "ProgIDFromCLSID returns with progid %p\n", progid);
344
345 hr = ProgIDFromCLSID(&CLSID_StdFont, NULL);
346 ok(hr == E_INVALIDARG, "ProgIDFromCLSID should return E_INVALIDARG instead of 0x%08x\n", hr);
347
348 if ((handle = activate_context(actctx_manifest, &cookie)))
349 {
350 static const WCHAR customfontW[] = {'C','u','s','t','o','m','F','o','n','t',0};
351
352 hr = ProgIDFromCLSID(&CLSID_non_existent, &progid);
353 ok(hr == S_OK, "got 0x%08x\n", hr);
354 ok(!lstrcmpiW(progid, progidW), "got %s\n", wine_dbgstr_w(progid));
355 CoTaskMemFree(progid);
356
357 /* try something registered and redirected */
358 progid = NULL;
359 hr = ProgIDFromCLSID(&CLSID_StdFont, &progid);
360 ok(hr == S_OK, "got 0x%08x\n", hr);
361 ok(!lstrcmpiW(progid, customfontW), "got wrong progid %s\n", wine_dbgstr_w(progid));
362 CoTaskMemFree(progid);
363
364 /* classes without default progid, progid list is not used */
365 progid = (void *)0xdeadbeef;
366 hr = ProgIDFromCLSID(&IID_Testiface5, &progid);
367 ok(hr == REGDB_E_CLASSNOTREG && progid == NULL, "got 0x%08x, progid %p\n", hr, progid);
368
369 progid = (void *)0xdeadbeef;
370 hr = ProgIDFromCLSID(&IID_Testiface6, &progid);
371 ok(hr == REGDB_E_CLASSNOTREG && progid == NULL, "got 0x%08x, progid %p\n", hr, progid);
372
373 pDeactivateActCtx(0, cookie);
374 pReleaseActCtx(handle);
375 }
376 }
377
378 static void test_CLSIDFromProgID(void)
379 {
380 ULONG_PTR cookie = 0;
381 HANDLE handle;
382 CLSID clsid;
383 HRESULT hr = CLSIDFromProgID(stdfont, &clsid);
384 ok(hr == S_OK, "CLSIDFromProgID failed with error 0x%08x\n", hr);
385 ok(IsEqualCLSID(&clsid, &CLSID_StdFont), "clsid wasn't equal to CLSID_StdFont\n");
386
387 hr = CLSIDFromString(stdfont, &clsid);
388 ok_ole_success(hr, "CLSIDFromString");
389 ok(IsEqualCLSID(&clsid, &CLSID_StdFont), "clsid wasn't equal to CLSID_StdFont\n");
390
391 /* test some failure cases */
392
393 hr = CLSIDFromProgID(wszNonExistent, NULL);
394 ok(hr == E_INVALIDARG, "CLSIDFromProgID should have returned E_INVALIDARG instead of 0x%08x\n", hr);
395
396 hr = CLSIDFromProgID(NULL, &clsid);
397 ok(hr == E_INVALIDARG, "CLSIDFromProgID should have returned E_INVALIDARG instead of 0x%08x\n", hr);
398
399 memset(&clsid, 0xcc, sizeof(clsid));
400 hr = CLSIDFromProgID(wszNonExistent, &clsid);
401 ok(hr == CO_E_CLASSSTRING, "CLSIDFromProgID on nonexistent ProgID should have returned CO_E_CLASSSTRING instead of 0x%08x\n", hr);
402 ok(IsEqualCLSID(&clsid, &CLSID_NULL), "CLSIDFromProgID should have set clsid to all-zeros on failure\n");
403
404 /* fails without proper context */
405 memset(&clsid, 0xcc, sizeof(clsid));
406 hr = CLSIDFromProgID(progidW, &clsid);
407 ok(hr == CO_E_CLASSSTRING, "got 0x%08x\n", hr);
408 ok(IsEqualCLSID(&clsid, &CLSID_NULL), "wrong clsid\n");
409
410 if ((handle = activate_context(actctx_manifest, &cookie)))
411 {
412 GUID clsid1;
413
414 memset(&clsid, 0xcc, sizeof(clsid));
415 hr = CLSIDFromProgID(wszNonExistent, &clsid);
416 ok(hr == CO_E_CLASSSTRING, "got 0x%08x\n", hr);
417 ok(IsEqualCLSID(&clsid, &CLSID_NULL), "should have zero CLSID on failure\n");
418
419 /* CLSIDFromString() doesn't check activation context */
420 hr = CLSIDFromString(progidW, &clsid);
421 ok(hr == CO_E_CLASSSTRING, "got 0x%08x\n", hr);
422
423 clsid = CLSID_NULL;
424 hr = CLSIDFromProgID(progidW, &clsid);
425 ok(hr == S_OK, "got 0x%08x\n", hr);
426 /* it returns generated CLSID here */
427 ok(!IsEqualCLSID(&clsid, &CLSID_non_existent) && !IsEqualCLSID(&clsid, &CLSID_NULL),
428 "got wrong clsid %s\n", wine_dbgstr_guid(&clsid));
429
430 /* duplicate progid present in context - returns generated guid here too */
431 clsid = CLSID_NULL;
432 hr = CLSIDFromProgID(stdfont, &clsid);
433 ok(hr == S_OK, "got 0x%08x\n", hr);
434 clsid1 = CLSID_StdFont;
435 /* that's where it differs from StdFont */
436 clsid1.Data4[7] = 0x52;
437 ok(!IsEqualCLSID(&clsid, &CLSID_StdFont) && !IsEqualCLSID(&clsid, &CLSID_NULL) && !IsEqualCLSID(&clsid, &clsid1),
438 "got %s\n", wine_dbgstr_guid(&clsid));
439
440 pDeactivateActCtx(0, cookie);
441 pReleaseActCtx(handle);
442 }
443 }
444
445 static void test_CLSIDFromString(void)
446 {
447 CLSID clsid;
448 WCHAR wszCLSID_Broken[50];
449 UINT i;
450
451 HRESULT hr = CLSIDFromString(wszCLSID_StdFont, &clsid);
452 ok_ole_success(hr, "CLSIDFromString");
453 ok(IsEqualCLSID(&clsid, &CLSID_StdFont), "clsid wasn't equal to CLSID_StdFont\n");
454
455 memset(&clsid, 0xab, sizeof(clsid));
456 hr = CLSIDFromString(NULL, &clsid);
457 ok(hr == S_OK, "got 0x%08x\n", hr);
458 ok(IsEqualCLSID(&clsid, &CLSID_NULL), "clsid wasn't equal to CLSID_NULL\n");
459
460 /* string is longer, but starts with a valid CLSID */
461 memset(&clsid, 0, sizeof(clsid));
462 hr = CLSIDFromString(cf_brokenW, &clsid);
463 ok(hr == CO_E_CLASSSTRING, "got 0x%08x\n", hr);
464 ok(IsEqualCLSID(&clsid, &IID_IClassFactory), "got %s\n", wine_dbgstr_guid(&clsid));
465
466 lstrcpyW(wszCLSID_Broken, wszCLSID_StdFont);
467 for(i = lstrlenW(wszCLSID_StdFont); i < 49; i++)
468 wszCLSID_Broken[i] = 'A';
469 wszCLSID_Broken[i] = '\0';
470
471 memset(&clsid, 0, sizeof(CLSID));
472 hr = CLSIDFromString(wszCLSID_Broken, &clsid);
473 ok(hr == CO_E_CLASSSTRING, "Got %08x\n", hr);
474 ok(IsEqualCLSID(&clsid, &CLSID_StdFont), "clsid wasn't equal to CLSID_StdFont\n");
475
476 wszCLSID_Broken[lstrlenW(wszCLSID_StdFont)-1] = 'A';
477 memset(&clsid, 0, sizeof(CLSID));
478 hr = CLSIDFromString(wszCLSID_Broken, &clsid);
479 ok(hr == CO_E_CLASSSTRING, "Got %08x\n", hr);
480 ok(IsEqualCLSID(&clsid, &CLSID_StdFont), "clsid wasn't equal to CLSID_StdFont\n");
481
482 wszCLSID_Broken[lstrlenW(wszCLSID_StdFont)] = '\0';
483 memset(&clsid, 0, sizeof(CLSID));
484 hr = CLSIDFromString(wszCLSID_Broken, &clsid);
485 ok(hr == CO_E_CLASSSTRING, "Got %08x\n", hr);
486 ok(IsEqualCLSID(&clsid, &CLSID_StdFont), "clsid wasn't equal to CLSID_StdFont\n");
487
488 wszCLSID_Broken[lstrlenW(wszCLSID_StdFont)-1] = '\0';
489 memset(&clsid, 0, sizeof(CLSID));
490 hr = CLSIDFromString(wszCLSID_Broken, &clsid);
491 ok(hr == CO_E_CLASSSTRING, "Got %08x\n", hr);
492 ok(IsEqualCLSID(&clsid, &CLSID_StdFont), "clsid wasn't equal to CLSID_StdFont\n");
493
494 memset(&clsid, 0xcc, sizeof(CLSID));
495 hr = CLSIDFromString(wszCLSID_Broken+1, &clsid);
496 ok(hr == CO_E_CLASSSTRING, "Got %08x\n", hr);
497 ok(IsEqualCLSID(&clsid, &CLSID_NULL), "clsid wasn't equal to CLSID_NULL\n");
498
499 wszCLSID_Broken[9] = '*';
500 memset(&clsid, 0xcc, sizeof(CLSID));
501 hr = CLSIDFromString(wszCLSID_Broken, &clsid);
502 ok(hr == CO_E_CLASSSTRING, "Got %08x\n", hr);
503 ok(clsid.Data1 == CLSID_StdFont.Data1, "Got %08x\n", clsid.Data1);
504 ok(clsid.Data2 == 0xcccc, "Got %04x\n", clsid.Data2);
505
506 wszCLSID_Broken[3] = '*';
507 memset(&clsid, 0xcc, sizeof(CLSID));
508 hr = CLSIDFromString(wszCLSID_Broken, &clsid);
509 ok(hr == CO_E_CLASSSTRING, "Got %08x\n", hr);
510 ok(clsid.Data1 == 0xb, "Got %08x\n", clsid.Data1);
511 ok(clsid.Data2 == 0xcccc, "Got %04x\n", clsid.Data2);
512
513 wszCLSID_Broken[3] = '\0';
514 memset(&clsid, 0xcc, sizeof(CLSID));
515 hr = CLSIDFromString(wszCLSID_Broken, &clsid);
516 ok(hr == CO_E_CLASSSTRING, "Got %08x\n", hr);
517 ok(clsid.Data1 == 0xb, "Got %08x\n", clsid.Data1);
518 ok(clsid.Data2 == 0xcccc, "Got %04x\n", clsid.Data2);
519 }
520
521 static void test_IIDFromString(void)
522 {
523 static const WCHAR cfW[] = {'{','0','0','0','0','0','0','0','1','-','0','0','0','0','-','0','0','0','0','-',
524 'c','0','0','0','-','0','0','0','0','0','0','0','0','0','0','4','6','}',0};
525 static const WCHAR brokenW[] = {'{','0','0','0','0','0','0','0','1','-','0','0','0','0','-','0','0','0','0','-',
526 'g','0','0','0','-','0','0','0','0','0','0','0','0','0','0','4','6','}',0};
527 static const WCHAR broken2W[] = {'{','0','0','0','0','0','0','0','1','=','0','0','0','0','-','0','0','0','0','-',
528 'g','0','0','0','-','0','0','0','0','0','0','0','0','0','0','4','6','}',0};
529 static const WCHAR broken3W[] = {'b','r','o','k','e','n','0','0','1','=','0','0','0','0','-','0','0','0','0','-',
530 'g','0','0','0','-','0','0','0','0','0','0','0','0','0','0','4','6','}',0};
531 HRESULT hr;
532 IID iid;
533
534 hr = IIDFromString(wszCLSID_StdFont, &iid);
535 ok(hr == S_OK, "got 0x%08x\n", hr);
536 ok(IsEqualIID(&iid, &CLSID_StdFont), "got iid %s\n", wine_dbgstr_guid(&iid));
537
538 memset(&iid, 0xab, sizeof(iid));
539 hr = IIDFromString(NULL, &iid);
540 ok(hr == S_OK, "got 0x%08x\n", hr);
541 ok(IsEqualIID(&iid, &CLSID_NULL), "got iid %s\n", wine_dbgstr_guid(&iid));
542
543 hr = IIDFromString(cfW, &iid);
544 ok(hr == S_OK, "got 0x%08x\n", hr);
545 ok(IsEqualIID(&iid, &IID_IClassFactory), "got iid %s\n", wine_dbgstr_guid(&iid));
546
547 /* string starts with a valid IID but is longer */
548 memset(&iid, 0xab, sizeof(iid));
549 hr = IIDFromString(cf_brokenW, &iid);
550 ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
551 ok(iid.Data1 == 0xabababab, "Got %08x\n", iid.Data1);
552
553 /* invalid IID in a valid format */
554 memset(&iid, 0xab, sizeof(iid));
555 hr = IIDFromString(brokenW, &iid);
556 ok(hr == CO_E_IIDSTRING, "got 0x%08x\n", hr);
557 ok(iid.Data1 == 0x00000001, "Got %08x\n", iid.Data1);
558
559 memset(&iid, 0xab, sizeof(iid));
560 hr = IIDFromString(broken2W, &iid);
561 ok(hr == CO_E_IIDSTRING, "got 0x%08x\n", hr);
562 ok(iid.Data1 == 0x00000001, "Got %08x\n", iid.Data1);
563
564 /* format is broken, but string length is okay */
565 memset(&iid, 0xab, sizeof(iid));
566 hr = IIDFromString(broken3W, &iid);
567 ok(hr == CO_E_IIDSTRING, "got 0x%08x\n", hr);
568 ok(iid.Data1 == 0xabababab, "Got %08x\n", iid.Data1);
569
570 /* invalid string */
571 memset(&iid, 0xab, sizeof(iid));
572 hr = IIDFromString(wszNonExistent, &iid);
573 ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
574 ok(iid.Data1 == 0xabababab, "Got %08x\n", iid.Data1);
575
576 /* valid ProgID */
577 memset(&iid, 0xab, sizeof(iid));
578 hr = IIDFromString(stdfont, &iid);
579 ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
580 ok(iid.Data1 == 0xabababab, "Got %08x\n", iid.Data1);
581 }
582
583 static void test_StringFromGUID2(void)
584 {
585 WCHAR str[50];
586 int len;
587
588 /* invalid pointer */
589 SetLastError(0xdeadbeef);
590 len = StringFromGUID2(NULL,str,50);
591 ok(len == 0, "len: %d (expected 0)\n", len);
592 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %x\n", GetLastError());
593
594 /* Test corner cases for buffer size */
595 len = StringFromGUID2(&CLSID_StdFont,str,50);
596 ok(len == 39, "len: %d (expected 39)\n", len);
597 ok(!lstrcmpiW(str, wszCLSID_StdFont),"string wasn't equal for CLSID_StdFont\n");
598
599 memset(str,0,sizeof str);
600 len = StringFromGUID2(&CLSID_StdFont,str,39);
601 ok(len == 39, "len: %d (expected 39)\n", len);
602 ok(!lstrcmpiW(str, wszCLSID_StdFont),"string wasn't equal for CLSID_StdFont\n");
603
604 len = StringFromGUID2(&CLSID_StdFont,str,38);
605 ok(len == 0, "len: %d (expected 0)\n", len);
606
607 len = StringFromGUID2(&CLSID_StdFont,str,30);
608 ok(len == 0, "len: %d (expected 0)\n", len);
609 }
610
611 #define test_apt_type(t, q) _test_apt_type(t, q, __LINE__)
612 static void _test_apt_type(APTTYPE expected_type, APTTYPEQUALIFIER expected_qualifier, int line)
613 {
614 APTTYPEQUALIFIER qualifier = ~0u;
615 APTTYPE type = ~0u;
616 HRESULT hr;
617
618 if (!pCoGetApartmentType)
619 return;
620
621 hr = pCoGetApartmentType(&type, &qualifier);
622 ok_(__FILE__, line)(hr == S_OK || hr == CO_E_NOTINITIALIZED, "Unexpected return code: 0x%08x\n", hr);
623 ok_(__FILE__, line)(type == expected_type, "Wrong apartment type %d, expected %d\n", type, expected_type);
624 ok_(__FILE__, line)(qualifier == expected_qualifier, "Wrong apartment qualifier %d, expected %d\n", qualifier,
625 expected_qualifier);
626 }
627
628 static void test_CoCreateInstance(void)
629 {
630 HRESULT hr;
631 IUnknown *pUnk;
632 REFCLSID rclsid = &CLSID_InternetZoneManager;
633
634 pUnk = (IUnknown *)0xdeadbeef;
635 hr = CoCreateInstance(rclsid, NULL, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void **)&pUnk);
636 ok(hr == CO_E_NOTINITIALIZED, "CoCreateInstance should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n", hr);
637 ok(pUnk == NULL, "CoCreateInstance should have changed the passed in pointer to NULL, instead of %p\n", pUnk);
638
639 OleInitialize(NULL);
640
641 /* test errors returned for non-registered clsids */
642 hr = CoCreateInstance(&CLSID_non_existent, NULL, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void **)&pUnk);
643 ok(hr == REGDB_E_CLASSNOTREG, "CoCreateInstance for non-registered inproc server should have returned REGDB_E_CLASSNOTREG instead of 0x%08x\n", hr);
644 hr = CoCreateInstance(&CLSID_non_existent, NULL, CLSCTX_INPROC_HANDLER, &IID_IUnknown, (void **)&pUnk);
645 ok(hr == REGDB_E_CLASSNOTREG, "CoCreateInstance for non-registered inproc handler should have returned REGDB_E_CLASSNOTREG instead of 0x%08x\n", hr);
646 hr = CoCreateInstance(&CLSID_non_existent, NULL, CLSCTX_LOCAL_SERVER, &IID_IUnknown, (void **)&pUnk);
647 ok(hr == REGDB_E_CLASSNOTREG, "CoCreateInstance for non-registered local server should have returned REGDB_E_CLASSNOTREG instead of 0x%08x\n", hr);
648 hr = CoCreateInstance(&CLSID_non_existent, NULL, CLSCTX_REMOTE_SERVER, &IID_IUnknown, (void **)&pUnk);
649 ok(hr == REGDB_E_CLASSNOTREG, "CoCreateInstance for non-registered remote server should have returned REGDB_E_CLASSNOTREG instead of 0x%08x\n", hr);
650
651 hr = CoCreateInstance(rclsid, NULL, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void **)&pUnk);
652 if(hr == REGDB_E_CLASSNOTREG)
653 {
654 skip("IE not installed so can't test CoCreateInstance\n");
655 OleUninitialize();
656 return;
657 }
658
659 ok_ole_success(hr, "CoCreateInstance");
660 if(pUnk) IUnknown_Release(pUnk);
661 OleUninitialize();
662
663 hr = CoCreateInstance(rclsid, NULL, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void **)&pUnk);
664 ok(hr == CO_E_NOTINITIALIZED, "CoCreateInstance should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n", hr);
665
666 test_apt_type(APTTYPE_CURRENT, APTTYPEQUALIFIER_NONE);
667 }
668
669 static void test_CoGetClassObject(void)
670 {
671 HRESULT hr;
672 HANDLE handle;
673 ULONG_PTR cookie;
674 IUnknown *pUnk;
675 REFCLSID rclsid = &CLSID_InternetZoneManager;
676 HKEY hkey;
677 LONG res;
678
679 hr = CoGetClassObject(rclsid, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void **)&pUnk);
680 ok(hr == CO_E_NOTINITIALIZED, "CoGetClassObject should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n", hr);
681 ok(pUnk == NULL, "CoGetClassObject should have changed the passed in pointer to NULL, instead of %p\n", pUnk);
682
683 hr = CoGetClassObject(rclsid, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, NULL);
684 ok(hr == E_INVALIDARG ||
685 broken(hr == CO_E_NOTINITIALIZED), /* win9x */
686 "CoGetClassObject should have returned E_INVALIDARG instead of 0x%08x\n", hr);
687
688 test_apt_type(APTTYPE_CURRENT, APTTYPEQUALIFIER_NONE);
689
690 if (!pRegOverridePredefKey)
691 {
692 win_skip("RegOverridePredefKey not available\n");
693 return;
694 }
695
696 pCoInitializeEx(NULL, COINIT_MULTITHREADED);
697
698 hr = CoGetClassObject(rclsid, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void **)&pUnk);
699 if (hr == S_OK)
700 {
701 IUnknown_Release(pUnk);
702
703 res = RegCreateKeyExA(HKEY_CURRENT_USER, "Software\\Classes", 0, NULL, 0,
704 KEY_ALL_ACCESS, NULL, &hkey, NULL);
705 ok(!res, "RegCreateKeyEx returned %d\n", res);
706
707 res = pRegOverridePredefKey(HKEY_CLASSES_ROOT, hkey);
708 ok(!res, "RegOverridePredefKey returned %d\n", res);
709
710 hr = CoGetClassObject(rclsid, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void **)&pUnk);
711 ok(hr == S_OK, "CoGetClassObject should have returned S_OK instead of 0x%08x\n", hr);
712
713 res = pRegOverridePredefKey(HKEY_CLASSES_ROOT, NULL);
714 ok(!res, "RegOverridePredefKey returned %d\n", res);
715
716 if (hr == S_OK) IUnknown_Release(pUnk);
717 RegCloseKey(hkey);
718 }
719
720 hr = CoGetClassObject(&CLSID_InProcFreeMarshaler, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void **)&pUnk);
721 ok(hr == S_OK, "got 0x%08x\n", hr);
722 IUnknown_Release(pUnk);
723
724 /* context redefines FreeMarshaler CLSID */
725 if ((handle = activate_context(actctx_manifest, &cookie)))
726 {
727 hr = CoGetClassObject(&CLSID_InProcFreeMarshaler, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void **)&pUnk);
728 ok(hr == S_OK, "got 0x%08x\n", hr);
729 IUnknown_Release(pUnk);
730
731 pDeactivateActCtx(0, cookie);
732 pReleaseActCtx(handle);
733 }
734
735 CoUninitialize();
736 }
737
738 static void test_CoCreateInstanceEx(void)
739 {
740 MULTI_QI qi_res = { &IID_IMoniker };
741 DWORD cookie;
742 HRESULT hr;
743
744 CoInitialize(NULL);
745
746 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
747 CLSCTX_INPROC_SERVER, REGCLS_MULTIPLEUSE, &cookie);
748 ok_ole_success(hr, "CoRegisterClassObject");
749
750 create_instance_iid = IID_NULL;
751 hr = CoCreateInstanceEx(&CLSID_WineOOPTest, NULL, CLSCTX_INPROC_SERVER, NULL, 1, &qi_res);
752 ok(hr == E_NOINTERFACE, "CoCreateInstanceEx failed: %08x\n", hr);
753 ok(IsEqualGUID(&create_instance_iid, qi_res.pIID), "Unexpected CreateInstance iid %s\n",
754 wine_dbgstr_guid(&create_instance_iid));
755
756 hr = CoRevokeClassObject(cookie);
757 ok_ole_success(hr, "CoRevokeClassObject");
758
759 CoUninitialize();
760 }
761
762 static ATOM register_dummy_class(void)
763 {
764 WNDCLASSA wc =
765 {
766 0,
767 DefWindowProcA,
768 0,
769 0,
770 GetModuleHandleA(NULL),
771 NULL,
772 LoadCursorA(NULL, (LPSTR)IDC_ARROW),
773 (HBRUSH)(COLOR_BTNFACE+1),
774 NULL,
775 "WineOleTestClass",
776 };
777
778 return RegisterClassA(&wc);
779 }
780
781 static void test_ole_menu(void)
782 {
783 HWND hwndFrame;
784 HRESULT hr;
785
786 hwndFrame = CreateWindowA((LPCSTR)MAKEINTATOM(register_dummy_class()), "Test", 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, NULL, NULL);
787 hr = OleSetMenuDescriptor(NULL, hwndFrame, NULL, NULL, NULL);
788 todo_wine ok_ole_success(hr, "OleSetMenuDescriptor");
789
790 DestroyWindow(hwndFrame);
791 }
792
793
794 static HRESULT WINAPI MessageFilter_QueryInterface(IMessageFilter *iface, REFIID riid, void ** ppvObj)
795 {
796 if (ppvObj == NULL) return E_POINTER;
797
798 if (IsEqualGUID(riid, &IID_IUnknown) ||
799 IsEqualGUID(riid, &IID_IClassFactory))
800 {
801 *ppvObj = iface;
802 IMessageFilter_AddRef(iface);
803 return S_OK;
804 }
805
806 return E_NOINTERFACE;
807 }
808
809 static ULONG WINAPI MessageFilter_AddRef(IMessageFilter *iface)
810 {
811 return 2; /* non-heap object */
812 }
813
814 static ULONG WINAPI MessageFilter_Release(IMessageFilter *iface)
815 {
816 return 1; /* non-heap object */
817 }
818
819 static DWORD WINAPI MessageFilter_HandleInComingCall(
820 IMessageFilter *iface,
821 DWORD dwCallType,
822 HTASK threadIDCaller,
823 DWORD dwTickCount,
824 LPINTERFACEINFO lpInterfaceInfo)
825 {
826 trace("HandleInComingCall\n");
827 return SERVERCALL_ISHANDLED;
828 }
829
830 static DWORD WINAPI MessageFilter_RetryRejectedCall(
831 IMessageFilter *iface,
832 HTASK threadIDCallee,
833 DWORD dwTickCount,
834 DWORD dwRejectType)
835 {
836 trace("RetryRejectedCall\n");
837 return 0;
838 }
839
840 static DWORD WINAPI MessageFilter_MessagePending(
841 IMessageFilter *iface,
842 HTASK threadIDCallee,
843 DWORD dwTickCount,
844 DWORD dwPendingType)
845 {
846 trace("MessagePending\n");
847 todo_wine ok(0, "unexpected call\n");
848 return PENDINGMSG_WAITNOPROCESS;
849 }
850
851 static const IMessageFilterVtbl MessageFilter_Vtbl =
852 {
853 MessageFilter_QueryInterface,
854 MessageFilter_AddRef,
855 MessageFilter_Release,
856 MessageFilter_HandleInComingCall,
857 MessageFilter_RetryRejectedCall,
858 MessageFilter_MessagePending
859 };
860
861 static IMessageFilter MessageFilter = { &MessageFilter_Vtbl };
862
863 static void test_CoRegisterMessageFilter(void)
864 {
865 HRESULT hr;
866 IMessageFilter *prev_filter;
867
868 hr = CoRegisterMessageFilter(&MessageFilter, &prev_filter);
869 ok(hr == CO_E_NOT_SUPPORTED,
870 "CoRegisterMessageFilter should have failed with CO_E_NOT_SUPPORTED instead of 0x%08x\n",
871 hr);
872
873 pCoInitializeEx(NULL, COINIT_MULTITHREADED);
874 prev_filter = (IMessageFilter *)0xdeadbeef;
875 hr = CoRegisterMessageFilter(&MessageFilter, &prev_filter);
876 ok(hr == CO_E_NOT_SUPPORTED,
877 "CoRegisterMessageFilter should have failed with CO_E_NOT_SUPPORTED instead of 0x%08x\n",
878 hr);
879 ok(prev_filter == (IMessageFilter *)0xdeadbeef,
880 "prev_filter should have been set to %p\n", prev_filter);
881 CoUninitialize();
882
883 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
884
885 hr = CoRegisterMessageFilter(NULL, NULL);
886 ok_ole_success(hr, "CoRegisterMessageFilter");
887
888 prev_filter = (IMessageFilter *)0xdeadbeef;
889 hr = CoRegisterMessageFilter(NULL, &prev_filter);
890 ok_ole_success(hr, "CoRegisterMessageFilter");
891 ok(prev_filter == NULL, "prev_filter should have been set to NULL instead of %p\n", prev_filter);
892
893 hr = CoRegisterMessageFilter(&MessageFilter, &prev_filter);
894 ok_ole_success(hr, "CoRegisterMessageFilter");
895 ok(prev_filter == NULL, "prev_filter should have been set to NULL instead of %p\n", prev_filter);
896
897 hr = CoRegisterMessageFilter(NULL, NULL);
898 ok_ole_success(hr, "CoRegisterMessageFilter");
899
900 CoUninitialize();
901 }
902
903 static IUnknown Test_Unknown;
904
905 static HRESULT WINAPI EnumOLEVERB_QueryInterface(IEnumOLEVERB *iface, REFIID riid, void **ppv)
906 {
907 return IUnknown_QueryInterface(&Test_Unknown, riid, ppv);
908 }
909
910 static ULONG WINAPI EnumOLEVERB_AddRef(IEnumOLEVERB *iface)
911 {
912 return 2;
913 }
914
915 static ULONG WINAPI EnumOLEVERB_Release(IEnumOLEVERB *iface)
916 {
917 return 1;
918 }
919
920 static HRESULT WINAPI EnumOLEVERB_Next(IEnumOLEVERB *iface, ULONG celt, OLEVERB *rgelt, ULONG *fetched)
921 {
922 ok(0, "unexpected call\n");
923 return E_NOTIMPL;
924 }
925
926 static HRESULT WINAPI EnumOLEVERB_Skip(IEnumOLEVERB *iface, ULONG celt)
927 {
928 ok(0, "unexpected call\n");
929 return E_NOTIMPL;
930 }
931
932 static HRESULT WINAPI EnumOLEVERB_Reset(IEnumOLEVERB *iface)
933 {
934 ok(0, "unexpected call\n");
935 return E_NOTIMPL;
936 }
937
938 static HRESULT WINAPI EnumOLEVERB_Clone(IEnumOLEVERB *iface, IEnumOLEVERB **ppenum)
939 {
940 ok(0, "unexpected call\n");
941 return E_NOTIMPL;
942 }
943
944 static const IEnumOLEVERBVtbl EnumOLEVERBVtbl = {
945 EnumOLEVERB_QueryInterface,
946 EnumOLEVERB_AddRef,
947 EnumOLEVERB_Release,
948 EnumOLEVERB_Next,
949 EnumOLEVERB_Skip,
950 EnumOLEVERB_Reset,
951 EnumOLEVERB_Clone
952 };
953
954 static IEnumOLEVERB EnumOLEVERB = { &EnumOLEVERBVtbl };
955
956 static HRESULT WINAPI Test_IUnknown_QueryInterface(
957 IUnknown *iface,
958 REFIID riid,
959 LPVOID *ppvObj)
960 {
961 if (ppvObj == NULL) return E_POINTER;
962
963 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IWineTest)) {
964 *ppvObj = iface;
965 }else if(IsEqualIID(riid, &IID_IEnumOLEVERB)) {
966 *ppvObj = &EnumOLEVERB;
967 }else {
968 *ppvObj = NULL;
969 return E_NOINTERFACE;
970 }
971
972 IUnknown_AddRef((IUnknown*)*ppvObj);
973 return S_OK;
974 }
975
976 static ULONG WINAPI Test_IUnknown_AddRef(IUnknown *iface)
977 {
978 return 2; /* non-heap-based object */
979 }
980
981 static ULONG WINAPI Test_IUnknown_Release(IUnknown *iface)
982 {
983 return 1; /* non-heap-based object */
984 }
985
986 static const IUnknownVtbl TestUnknown_Vtbl =
987 {
988 Test_IUnknown_QueryInterface,
989 Test_IUnknown_AddRef,
990 Test_IUnknown_Release,
991 };
992
993 static IUnknown Test_Unknown = { &TestUnknown_Vtbl };
994
995 static IPSFactoryBuffer *ps_factory_buffer;
996
997 static HRESULT WINAPI PSFactoryBuffer_QueryInterface(
998 IPSFactoryBuffer * This,
999 /* [in] */ REFIID riid,
1000 /* [iid_is][out] */ void **ppvObject)
1001 {
1002 if (IsEqualIID(riid, &IID_IUnknown) ||
1003 IsEqualIID(riid, &IID_IPSFactoryBuffer))
1004 {
1005 *ppvObject = This;
1006 IPSFactoryBuffer_AddRef(This);
1007 return S_OK;
1008 }
1009 return E_NOINTERFACE;
1010 }
1011
1012 static ULONG WINAPI PSFactoryBuffer_AddRef(
1013 IPSFactoryBuffer * This)
1014 {
1015 return 2;
1016 }
1017
1018 static ULONG WINAPI PSFactoryBuffer_Release(
1019 IPSFactoryBuffer * This)
1020 {
1021 return 1;
1022 }
1023
1024 static HRESULT WINAPI PSFactoryBuffer_CreateProxy(
1025 IPSFactoryBuffer * This,
1026 /* [in] */ IUnknown *pUnkOuter,
1027 /* [in] */ REFIID riid,
1028 /* [out] */ IRpcProxyBuffer **ppProxy,
1029 /* [out] */ void **ppv)
1030 {
1031 return E_NOTIMPL;
1032 }
1033
1034 static HRESULT WINAPI PSFactoryBuffer_CreateStub(
1035 IPSFactoryBuffer * This,
1036 /* [in] */ REFIID riid,
1037 /* [unique][in] */ IUnknown *pUnkServer,
1038 /* [out] */ IRpcStubBuffer **ppStub)
1039 {
1040 CHECK_EXPECT(CreateStub);
1041
1042 ok(pUnkServer == (IUnknown*)&Test_Unknown, "unexpected pUnkServer %p\n", pUnkServer);
1043 if(!ps_factory_buffer)
1044 return E_NOTIMPL;
1045
1046 return IPSFactoryBuffer_CreateStub(ps_factory_buffer, &IID_IEnumOLEVERB, pUnkServer, ppStub);
1047 }
1048
1049 static IPSFactoryBufferVtbl PSFactoryBufferVtbl =
1050 {
1051 PSFactoryBuffer_QueryInterface,
1052 PSFactoryBuffer_AddRef,
1053 PSFactoryBuffer_Release,
1054 PSFactoryBuffer_CreateProxy,
1055 PSFactoryBuffer_CreateStub
1056 };
1057
1058 static IPSFactoryBuffer PSFactoryBuffer = { &PSFactoryBufferVtbl };
1059
1060 static const CLSID CLSID_WineTestPSFactoryBuffer =
1061 {
1062 0x52011640,
1063 0x8164,
1064 0x4fd0,
1065 {0xa1, 0xa2, 0x5d, 0x5a, 0x36, 0x54, 0xd3, 0xbd}
1066 }; /* 52011640-8164-4fd0-a1a2-5d5a3654d3bd */
1067
1068 static DWORD CALLBACK register_ps_clsid_thread(void *context)
1069 {
1070 HRESULT hr;
1071 CLSID clsid = {0};
1072
1073 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1074
1075 hr = CoGetPSClsid(&IID_IWineTest, &clsid);
1076 ok_ole_success(hr, "CoGetPSClsid");
1077 ok(IsEqualGUID(&clsid, &CLSID_WineTestPSFactoryBuffer), "expected %s, got %s\n",
1078 wine_dbgstr_guid(&CLSID_WineTestPSFactoryBuffer), wine_dbgstr_guid(&clsid));
1079
1080 /* test registering a PSClsid in an apartment which is then destroyed */
1081 hr = CoRegisterPSClsid(&IID_TestPS, &clsid);
1082 ok_ole_success(hr, "CoRegisterPSClsid");
1083
1084 CoUninitialize();
1085
1086 return hr;
1087 }
1088
1089 static void test_CoRegisterPSClsid(void)
1090 {
1091 HRESULT hr;
1092 DWORD dwRegistrationKey;
1093 IStream *stream;
1094 CLSID clsid;
1095 HANDLE thread;
1096 DWORD tid;
1097
1098 hr = CoRegisterPSClsid(&IID_IWineTest, &CLSID_WineTestPSFactoryBuffer);
1099 ok(hr == CO_E_NOTINITIALIZED, "CoRegisterPSClsid should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n", hr);
1100
1101 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1102
1103 hr = CoRegisterClassObject(&CLSID_WineTestPSFactoryBuffer, (IUnknown *)&PSFactoryBuffer,
1104 CLSCTX_INPROC_SERVER, REGCLS_MULTIPLEUSE, &dwRegistrationKey);
1105 ok_ole_success(hr, "CoRegisterClassObject");
1106
1107 hr = CoRegisterPSClsid(&IID_IWineTest, &CLSID_WineTestPSFactoryBuffer);
1108 ok_ole_success(hr, "CoRegisterPSClsid");
1109
1110 hr = CoGetPSClsid(&IID_IWineTest, &clsid);
1111 ok_ole_success(hr, "CoGetPSClsid");
1112 ok(IsEqualGUID(&clsid, &CLSID_WineTestPSFactoryBuffer), "expected %s, got %s\n",
1113 wine_dbgstr_guid(&CLSID_WineTestPSFactoryBuffer), wine_dbgstr_guid(&clsid));
1114
1115 thread = CreateThread(NULL, 0, register_ps_clsid_thread, NULL, 0, &tid);
1116 ok(thread != NULL, "CreateThread failed with error %d\n", GetLastError());
1117 ok(!WaitForSingleObject(thread, 10000), "wait timed out\n");
1118 CloseHandle(thread);
1119
1120 hr = CoGetPSClsid(&IID_TestPS, &clsid);
1121 ok_ole_success(hr, "CoGetPSClsid");
1122 ok(IsEqualGUID(&clsid, &CLSID_WineTestPSFactoryBuffer), "expected %s, got %s\n",
1123 wine_dbgstr_guid(&CLSID_WineTestPSFactoryBuffer), wine_dbgstr_guid(&clsid));
1124
1125 hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
1126 ok_ole_success(hr, "CreateStreamOnHGlobal");
1127
1128 SET_EXPECT(CreateStub);
1129 hr = CoMarshalInterface(stream, &IID_IWineTest, &Test_Unknown, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1130 ok(hr == E_NOTIMPL, "CoMarshalInterface should have returned E_NOTIMPL instead of 0x%08x\n", hr);
1131 CHECK_CALLED(CreateStub);
1132
1133 hr = CoGetPSClsid(&IID_IEnumOLEVERB, &clsid);
1134 ok_ole_success(hr, "CoGetPSClsid");
1135
1136 hr = CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL, &IID_IPSFactoryBuffer, (void **)&ps_factory_buffer);
1137 ok_ole_success(hr, "CoGetClassObject");
1138
1139 hr = CoRegisterPSClsid(&IID_IEnumOLEVERB, &CLSID_WineTestPSFactoryBuffer);
1140 ok_ole_success(hr, "CoRegisterPSClsid");
1141
1142 SET_EXPECT(CreateStub);
1143 hr = CoMarshalInterface(stream, &IID_IEnumOLEVERB, (IUnknown*)&EnumOLEVERB, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1144 ok(hr == S_OK, "CoMarshalInterface should have returned S_OK instead of 0x%08x\n", hr);
1145 CHECK_CALLED(CreateStub);
1146
1147 hr = CoMarshalInterface(stream, &IID_IEnumOLEVERB, &Test_Unknown, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1148 ok(hr == S_OK, "CoMarshalInterface should have returned S_OK instead of 0x%08x\n", hr);
1149
1150 IStream_Release(stream);
1151 IPSFactoryBuffer_Release(ps_factory_buffer);
1152 ps_factory_buffer = NULL;
1153
1154 hr = CoRevokeClassObject(dwRegistrationKey);
1155 ok_ole_success(hr, "CoRevokeClassObject");
1156
1157 CoUninitialize();
1158
1159 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1160
1161 hr = CoGetPSClsid(&IID_IWineTest, &clsid);
1162 ok(hr == REGDB_E_IIDNOTREG, "CoGetPSClsid should have returned REGDB_E_IIDNOTREG instead of 0x%08x\n", hr);
1163
1164 hr = CoGetPSClsid(&IID_TestPS, &clsid);
1165 ok(hr == REGDB_E_IIDNOTREG, "CoGetPSClsid should have returned REGDB_E_IIDNOTREG instead of 0x%08x\n", hr);
1166
1167 CoUninitialize();
1168
1169 pCoInitializeEx(NULL, COINIT_MULTITHREADED);
1170
1171 hr = CoRegisterPSClsid(&IID_IWineTest, &CLSID_WineTestPSFactoryBuffer);
1172 ok_ole_success(hr, "CoRegisterPSClsid");
1173
1174 hr = CoGetPSClsid(&IID_IWineTest, &clsid);
1175 ok_ole_success(hr, "CoGetPSClsid");
1176 ok(IsEqualGUID(&clsid, &CLSID_WineTestPSFactoryBuffer), "expected %s, got %s\n",
1177 wine_dbgstr_guid(&CLSID_WineTestPSFactoryBuffer), wine_dbgstr_guid(&clsid));
1178
1179 thread = CreateThread(NULL, 0, register_ps_clsid_thread, NULL, 0, &tid);
1180 ok(thread != NULL, "CreateThread failed with error %d\n", GetLastError());
1181 ok(!WaitForSingleObject(thread, 10000), "wait timed out\n");
1182 CloseHandle(thread);
1183
1184 hr = CoGetPSClsid(&IID_TestPS, &clsid);
1185 ok_ole_success(hr, "CoGetPSClsid");
1186 ok(IsEqualGUID(&clsid, &CLSID_WineTestPSFactoryBuffer), "expected %s, got %s\n",
1187 wine_dbgstr_guid(&CLSID_WineTestPSFactoryBuffer), wine_dbgstr_guid(&clsid));
1188
1189 CoUninitialize();
1190 }
1191
1192 static void test_CoGetPSClsid(void)
1193 {
1194 ULONG_PTR cookie;
1195 HANDLE handle;
1196 HRESULT hr;
1197 CLSID clsid;
1198 HKEY hkey;
1199 LONG res;
1200 const BOOL is_win64 = (sizeof(void*) != sizeof(int));
1201 BOOL is_wow64 = FALSE;
1202
1203 hr = CoGetPSClsid(&IID_IClassFactory, &clsid);
1204 ok(hr == CO_E_NOTINITIALIZED,
1205 "CoGetPSClsid should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n",
1206 hr);
1207
1208 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1209
1210 hr = CoGetPSClsid(&IID_IClassFactory, &clsid);
1211 ok_ole_success(hr, "CoGetPSClsid");
1212
1213 hr = CoGetPSClsid(&IID_IWineTest, &clsid);
1214 ok(hr == REGDB_E_IIDNOTREG,
1215 "CoGetPSClsid for random IID returned 0x%08x instead of REGDB_E_IIDNOTREG\n",
1216 hr);
1217
1218 hr = CoGetPSClsid(&IID_IClassFactory, NULL);
1219 ok(hr == E_INVALIDARG,
1220 "CoGetPSClsid for null clsid returned 0x%08x instead of E_INVALIDARG\n",
1221 hr);
1222
1223 if (!pRegOverridePredefKey)
1224 {
1225 win_skip("RegOverridePredefKey not available\n");
1226 CoUninitialize();
1227 return;
1228 }
1229 hr = CoGetPSClsid(&IID_IClassFactory, &clsid);
1230 ok_ole_success(hr, "CoGetPSClsid");
1231
1232 res = RegCreateKeyExA(HKEY_CURRENT_USER, "Software\\Classes", 0, NULL, 0,
1233 KEY_ALL_ACCESS, NULL, &hkey, NULL);
1234 ok(!res, "RegCreateKeyEx returned %d\n", res);
1235
1236 res = pRegOverridePredefKey(HKEY_CLASSES_ROOT, hkey);
1237 ok(!res, "RegOverridePredefKey returned %d\n", res);
1238
1239 hr = CoGetPSClsid(&IID_IClassFactory, &clsid);
1240 ok_ole_success(hr, "CoGetPSClsid");
1241
1242 res = pRegOverridePredefKey(HKEY_CLASSES_ROOT, NULL);
1243 ok(!res, "RegOverridePredefKey returned %d\n", res);
1244
1245 RegCloseKey(hkey);
1246
1247 /* not registered CLSID */
1248 hr = CoGetPSClsid(&IID_Testiface, &clsid);
1249 ok(hr == REGDB_E_IIDNOTREG, "got 0x%08x\n", hr);
1250
1251 if ((handle = activate_context(actctx_manifest, &cookie)))
1252 {
1253 memset(&clsid, 0, sizeof(clsid));
1254 hr = CoGetPSClsid(&IID_Testiface, &clsid);
1255 ok(hr == S_OK, "got 0x%08x\n", hr);
1256 ok(IsEqualGUID(&clsid, &IID_Testiface), "got clsid %s\n", wine_dbgstr_guid(&clsid));
1257
1258 memset(&clsid, 0, sizeof(clsid));
1259 hr = CoGetPSClsid(&IID_Testiface2, &clsid);
1260 ok(hr == S_OK, "got 0x%08x\n", hr);
1261 ok(IsEqualGUID(&clsid, &IID_Testiface2), "got clsid %s\n", wine_dbgstr_guid(&clsid));
1262
1263 memset(&clsid, 0, sizeof(clsid));
1264 hr = CoGetPSClsid(&IID_Testiface3, &clsid);
1265 ok(hr == S_OK, "got 0x%08x\n", hr);
1266 ok(IsEqualGUID(&clsid, &IID_TestPS), "got clsid %s\n", wine_dbgstr_guid(&clsid));
1267
1268 memset(&clsid, 0xaa, sizeof(clsid));
1269 hr = CoGetPSClsid(&IID_Testiface4, &clsid);
1270 ok(hr == S_OK, "got 0x%08x\n", hr);
1271 ok(IsEqualGUID(&clsid, &GUID_NULL), "got clsid %s\n", wine_dbgstr_guid(&clsid));
1272
1273 /* register same interface and try to get CLSID back */
1274 hr = CoRegisterPSClsid(&IID_Testiface, &IID_Testiface4);
1275 ok(hr == S_OK, "got 0x%08x\n", hr);
1276 memset(&clsid, 0, sizeof(clsid));
1277 hr = CoGetPSClsid(&IID_Testiface, &clsid);
1278 ok(hr == S_OK, "got 0x%08x\n", hr);
1279 ok(IsEqualGUID(&clsid, &IID_Testiface4), "got clsid %s\n", wine_dbgstr_guid(&clsid));
1280
1281 pDeactivateActCtx(0, cookie);
1282 pReleaseActCtx(handle);
1283 }
1284
1285 if (pRegDeleteKeyExA &&
1286 (is_win64 ||
1287 (pIsWow64Process && pIsWow64Process(GetCurrentProcess(), &is_wow64) && is_wow64)))
1288 {
1289 static GUID IID_DeadBeef = {0xdeadbeef,0xdead,0xbeef,{0xde,0xad,0xbe,0xef,0xde,0xad,0xbe,0xef}};
1290 static const char clsidDeadBeef[] = "{deadbeef-dead-beef-dead-beefdeadbeef}";
1291 static const char clsidA[] = "{66666666-8888-7777-6666-555555555555}";
1292 HKEY hkey_iface, hkey_psclsid;
1293 REGSAM opposite = is_win64 ? KEY_WOW64_32KEY : KEY_WOW64_64KEY;
1294
1295 hr = CoGetPSClsid(&IID_DeadBeef, &clsid);
1296 ok(hr == REGDB_E_IIDNOTREG, "got 0x%08x\n", hr);
1297
1298 res = RegCreateKeyExA(HKEY_CLASSES_ROOT, "Interface",
1299 0, NULL, 0, KEY_ALL_ACCESS | opposite, NULL, &hkey_iface, NULL);
1300 ok(!res, "RegCreateKeyEx returned %d\n", res);
1301 res = RegCreateKeyExA(hkey_iface, clsidDeadBeef,
1302 0, NULL, 0, KEY_ALL_ACCESS | opposite, NULL, &hkey, NULL);
1303 if (res == ERROR_ACCESS_DENIED)
1304 {
1305 win_skip("Failed to create a key, skipping some of CoGetPSClsid() tests\n");
1306 goto cleanup;
1307 }
1308
1309 ok(!res, "RegCreateKeyEx returned %d\n", res);
1310 res = RegCreateKeyExA(hkey, "ProxyStubClsid32",
1311 0, NULL, 0, KEY_ALL_ACCESS | opposite, NULL, &hkey_psclsid, NULL);
1312 ok(!res, "RegCreateKeyEx returned %d\n", res);
1313 res = RegSetValueExA(hkey_psclsid, NULL, 0, REG_SZ, (const BYTE *)clsidA, strlen(clsidA)+1);
1314 ok(!res, "RegSetValueEx returned %d\n", res);
1315 RegCloseKey(hkey_psclsid);
1316
1317 hr = CoGetPSClsid(&IID_DeadBeef, &clsid);
1318 ok_ole_success(hr, "CoGetPSClsid");
1319 ok(IsEqualGUID(&clsid, &IID_TestPS), "got clsid %s\n", wine_dbgstr_guid(&clsid));
1320
1321 res = pRegDeleteKeyExA(hkey, "ProxyStubClsid32", opposite, 0);
1322 ok(!res, "RegDeleteKeyEx returned %d\n", res);
1323 RegCloseKey(hkey);
1324 res = pRegDeleteKeyExA(hkey_iface, clsidDeadBeef, opposite, 0);
1325 ok(!res, "RegDeleteKeyEx returned %d\n", res);
1326
1327 cleanup:
1328 RegCloseKey(hkey_iface);
1329 }
1330
1331 CoUninitialize();
1332 }
1333
1334 /* basic test, mainly for invalid arguments. see marshal.c for more */
1335 static void test_CoUnmarshalInterface(void)
1336 {
1337 IUnknown *pProxy;
1338 IStream *pStream;
1339 HRESULT hr;
1340
1341 hr = CoUnmarshalInterface(NULL, &IID_IUnknown, (void **)&pProxy);
1342 ok(hr == E_INVALIDARG, "CoUnmarshalInterface should have returned E_INVALIDARG instead of 0x%08x\n", hr);
1343
1344 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1345 ok_ole_success(hr, "CreateStreamOnHGlobal");
1346
1347 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)&pProxy);
1348 todo_wine
1349 ok(hr == CO_E_NOTINITIALIZED, "CoUnmarshalInterface should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n", hr);
1350
1351 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1352
1353 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)&pProxy);
1354 ok(hr == STG_E_READFAULT, "CoUnmarshalInterface should have returned STG_E_READFAULT instead of 0x%08x\n", hr);
1355
1356 CoUninitialize();
1357
1358 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, NULL);
1359 ok(hr == E_INVALIDARG, "CoUnmarshalInterface should have returned E_INVALIDARG instead of 0x%08x\n", hr);
1360
1361 IStream_Release(pStream);
1362 }
1363
1364 static void test_CoGetInterfaceAndReleaseStream(void)
1365 {
1366 HRESULT hr;
1367 IUnknown *pUnk;
1368
1369 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1370
1371 hr = CoGetInterfaceAndReleaseStream(NULL, &IID_IUnknown, (void**)&pUnk);
1372 ok(hr == E_INVALIDARG, "hr %08x\n", hr);
1373
1374 CoUninitialize();
1375 }
1376
1377 /* basic test, mainly for invalid arguments. see marshal.c for more */
1378 static void test_CoMarshalInterface(void)
1379 {
1380 IStream *pStream;
1381 HRESULT hr;
1382 static const LARGE_INTEGER llZero;
1383
1384 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1385
1386 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1387 ok_ole_success(hr, "CreateStreamOnHGlobal");
1388
1389 hr = CoMarshalInterface(pStream, &IID_IUnknown, NULL, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1390 ok(hr == E_INVALIDARG, "CoMarshalInterface should have returned E_INVALIDARG instead of 0x%08x\n", hr);
1391
1392 hr = CoMarshalInterface(NULL, &IID_IUnknown, (IUnknown *)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1393 ok(hr == E_INVALIDARG, "CoMarshalInterface should have returned E_INVALIDARG instead of 0x%08x\n", hr);
1394
1395 hr = CoMarshalInterface(pStream, &IID_IUnknown, (IUnknown *)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1396 ok_ole_success(hr, "CoMarshalInterface");
1397
1398 /* stream not rewound */
1399 hr = CoReleaseMarshalData(pStream);
1400 ok(hr == STG_E_READFAULT, "CoReleaseMarshalData should have returned STG_E_READFAULT instead of 0x%08x\n", hr);
1401
1402 hr = IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
1403 ok_ole_success(hr, "IStream_Seek");
1404
1405 hr = CoReleaseMarshalData(pStream);
1406 ok_ole_success(hr, "CoReleaseMarshalData");
1407
1408 IStream_Release(pStream);
1409
1410 CoUninitialize();
1411 }
1412
1413 static void test_CoMarshalInterThreadInterfaceInStream(void)
1414 {
1415 IStream *pStream;
1416 HRESULT hr;
1417 IClassFactory *pProxy;
1418
1419 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1420
1421 cLocks = 0;
1422
1423 hr = CoMarshalInterThreadInterfaceInStream(&IID_IUnknown, (IUnknown *)&Test_ClassFactory, NULL);
1424 ok(hr == E_INVALIDARG, "CoMarshalInterThreadInterfaceInStream should have returned E_INVALIDARG instead of 0x%08x\n", hr);
1425
1426 hr = CoMarshalInterThreadInterfaceInStream(&IID_IUnknown, NULL, &pStream);
1427 ok(hr == E_INVALIDARG, "CoMarshalInterThreadInterfaceInStream should have returned E_INVALIDARG instead of 0x%08x\n", hr);
1428
1429 ok_no_locks();
1430
1431 hr = CoMarshalInterThreadInterfaceInStream(&IID_IUnknown, (IUnknown *)&Test_ClassFactory, &pStream);
1432 ok_ole_success(hr, "CoMarshalInterThreadInterfaceInStream");
1433
1434 ok_more_than_one_lock();
1435
1436 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
1437 ok_ole_success(hr, "CoUnmarshalInterface");
1438
1439 IClassFactory_Release(pProxy);
1440 IStream_Release(pStream);
1441
1442 ok_no_locks();
1443
1444 CoUninitialize();
1445 }
1446
1447 static void test_CoRegisterClassObject(void)
1448 {
1449 ULONG_PTR ctxcookie;
1450 HANDLE handle;
1451 DWORD cookie;
1452 HRESULT hr;
1453 IClassFactory *pcf;
1454
1455 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1456
1457 /* CLSCTX_INPROC_SERVER */
1458 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
1459 CLSCTX_INPROC_SERVER, REGCLS_SINGLEUSE, &cookie);
1460 ok_ole_success(hr, "CoRegisterClassObject");
1461 hr = CoRevokeClassObject(cookie);
1462 ok_ole_success(hr, "CoRevokeClassObject");
1463
1464 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
1465 CLSCTX_INPROC_SERVER, REGCLS_MULTIPLEUSE, &cookie);
1466 ok_ole_success(hr, "CoRegisterClassObject");
1467 hr = CoRevokeClassObject(cookie);
1468 ok_ole_success(hr, "CoRevokeClassObject");
1469
1470 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
1471 CLSCTX_INPROC_SERVER, REGCLS_MULTI_SEPARATE, &cookie);
1472 ok_ole_success(hr, "CoRegisterClassObject");
1473 hr = CoRevokeClassObject(cookie);
1474 ok_ole_success(hr, "CoRevokeClassObject");
1475
1476 /* CLSCTX_LOCAL_SERVER */
1477 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
1478 CLSCTX_LOCAL_SERVER, REGCLS_SINGLEUSE, &cookie);
1479 ok_ole_success(hr, "CoRegisterClassObject");
1480 hr = CoRevokeClassObject(cookie);
1481 ok_ole_success(hr, "CoRevokeClassObject");
1482
1483 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
1484 CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE, &cookie);
1485 ok_ole_success(hr, "CoRegisterClassObject");
1486 hr = CoRevokeClassObject(cookie);
1487 ok_ole_success(hr, "CoRevokeClassObject");
1488
1489 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
1490 CLSCTX_LOCAL_SERVER, REGCLS_MULTI_SEPARATE, &cookie);
1491 ok_ole_success(hr, "CoRegisterClassObject");
1492 hr = CoRevokeClassObject(cookie);
1493 ok_ole_success(hr, "CoRevokeClassObject");
1494
1495 /* CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER */
1496 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
1497 CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, REGCLS_SINGLEUSE, &cookie);
1498 ok_ole_success(hr, "CoRegisterClassObject");
1499 hr = CoRevokeClassObject(cookie);
1500 ok_ole_success(hr, "CoRevokeClassObject");
1501
1502 /* test whether an object that doesn't support IClassFactory can be
1503 * registered for CLSCTX_LOCAL_SERVER */
1504 hr = CoRegisterClassObject(&CLSID_WineOOPTest, &Test_Unknown,
1505 CLSCTX_LOCAL_SERVER, REGCLS_SINGLEUSE, &cookie);
1506 ok_ole_success(hr, "CoRegisterClassObject");
1507 hr = CoRevokeClassObject(cookie);
1508 ok_ole_success(hr, "CoRevokeClassObject");
1509
1510 /* test whether registered class becomes invalid when apartment is destroyed */
1511 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
1512 CLSCTX_INPROC_SERVER, REGCLS_SINGLEUSE, &cookie);
1513 ok_ole_success(hr, "CoRegisterClassObject");
1514
1515 CoUninitialize();
1516 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1517
1518 hr = CoGetClassObject(&CLSID_WineOOPTest, CLSCTX_INPROC_SERVER, NULL,
1519 &IID_IClassFactory, (void **)&pcf);
1520 ok(hr == REGDB_E_CLASSNOTREG, "object registered in an apartment shouldn't accessible after it is destroyed\n");
1521
1522 /* crashes with at least win9x DCOM! */
1523 if (0)
1524 CoRevokeClassObject(cookie);
1525
1526 /* test that object is accessible */
1527 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory, CLSCTX_INPROC_SERVER,
1528 REGCLS_MULTIPLEUSE, &cookie);
1529 ok(hr == S_OK, "got 0x%08x\n", hr);
1530
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);
1534
1535 /* context now contains CLSID_WineOOPTest, test if registered one could still be used */
1536 if ((handle = activate_context(actctx_manifest, &ctxcookie)))
1537 {
1538 hr = CoGetClassObject(&CLSID_WineOOPTest, CLSCTX_INPROC_SERVER, NULL, &IID_IClassFactory, (void**)&pcf);
1539 todo_wine
1540 ok(hr == HRESULT_FROM_WIN32(ERROR_MOD_NOT_FOUND), "got 0x%08x\n", hr);
1541
1542 pDeactivateActCtx(0, ctxcookie);
1543 pReleaseActCtx(handle);
1544 }
1545
1546 hr = CoGetClassObject(&CLSID_WineOOPTest, CLSCTX_INPROC_SERVER, NULL, &IID_IClassFactory, (void**)&pcf);
1547 ok(hr == S_OK, "got 0x%08x\n", hr);
1548 IClassFactory_Release(pcf);
1549
1550 hr = CoRevokeClassObject(cookie);
1551 ok(hr == S_OK, "got 0x%08x\n", hr);
1552
1553 CoUninitialize();
1554 }
1555
1556 static HRESULT get_class_object(CLSCTX clsctx)
1557 {
1558 HRESULT hr;
1559 IClassFactory *pcf;
1560
1561 hr = CoGetClassObject(&CLSID_WineOOPTest, clsctx, NULL, &IID_IClassFactory,
1562 (void **)&pcf);
1563
1564 if (SUCCEEDED(hr))
1565 IClassFactory_Release(pcf);
1566
1567 return hr;
1568 }
1569
1570 static DWORD CALLBACK get_class_object_thread(LPVOID pv)
1571 {
1572 CLSCTX clsctx = (CLSCTX)(DWORD_PTR)pv;
1573 HRESULT hr;
1574
1575 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1576
1577 hr = get_class_object(clsctx);
1578
1579 CoUninitialize();
1580
1581 return hr;
1582 }
1583
1584 static DWORD CALLBACK get_class_object_proxy_thread(LPVOID pv)
1585 {
1586 CLSCTX clsctx = (CLSCTX)(DWORD_PTR)pv;
1587 HRESULT hr;
1588 IClassFactory *pcf;
1589 IMultiQI *pMQI;
1590
1591 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1592
1593 hr = CoGetClassObject(&CLSID_WineOOPTest, clsctx, NULL, &IID_IClassFactory,
1594 (void **)&pcf);
1595
1596 if (SUCCEEDED(hr))
1597 {
1598 hr = IClassFactory_QueryInterface(pcf, &IID_IMultiQI, (void **)&pMQI);
1599 if (SUCCEEDED(hr))
1600 IMultiQI_Release(pMQI);
1601 IClassFactory_Release(pcf);
1602 }
1603
1604 CoUninitialize();
1605
1606 return hr;
1607 }
1608
1609 static DWORD CALLBACK register_class_object_thread(LPVOID pv)
1610 {
1611 HRESULT hr;
1612 DWORD cookie;
1613
1614 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1615
1616 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
1617 CLSCTX_INPROC_SERVER, REGCLS_SINGLEUSE, &cookie);
1618
1619 CoUninitialize();
1620
1621 return hr;
1622 }
1623
1624 static DWORD CALLBACK revoke_class_object_thread(LPVOID pv)
1625 {
1626 DWORD cookie = (DWORD_PTR)pv;
1627 HRESULT hr;
1628
1629 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1630
1631 hr = CoRevokeClassObject(cookie);
1632
1633 CoUninitialize();
1634
1635 return hr;
1636 }
1637
1638 static void test_registered_object_thread_affinity(void)
1639 {
1640 HRESULT hr;
1641 DWORD cookie;
1642 HANDLE thread;
1643 DWORD tid;
1644 DWORD exitcode;
1645
1646 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1647
1648 /* CLSCTX_INPROC_SERVER */
1649
1650 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
1651 CLSCTX_INPROC_SERVER, REGCLS_SINGLEUSE, &cookie);
1652 ok_ole_success(hr, "CoRegisterClassObject");
1653
1654 thread = CreateThread(NULL, 0, get_class_object_thread, (LPVOID)CLSCTX_INPROC_SERVER, 0, &tid);
1655 ok(thread != NULL, "CreateThread failed with error %d\n", GetLastError());
1656 ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
1657 GetExitCodeThread(thread, &exitcode);
1658 hr = exitcode;
1659 ok(hr == REGDB_E_CLASSNOTREG, "CoGetClassObject on inproc object "
1660 "registered in different thread should return REGDB_E_CLASSNOTREG "
1661 "instead of 0x%08x\n", hr);
1662
1663 hr = get_class_object(CLSCTX_INPROC_SERVER);
1664 ok(hr == S_OK, "CoGetClassObject on inproc object registered in same "
1665 "thread should return S_OK instead of 0x%08x\n", hr);
1666
1667 thread = CreateThread(NULL, 0, register_class_object_thread, NULL, 0, &tid);
1668 ok(thread != NULL, "CreateThread failed with error %d\n", GetLastError());
1669 ok ( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
1670 GetExitCodeThread(thread, &exitcode);
1671 hr = exitcode;
1672 ok(hr == S_OK, "CoRegisterClassObject with same CLSID but in different thread should return S_OK instead of 0x%08x\n", hr);
1673
1674 hr = CoRevokeClassObject(cookie);
1675 ok_ole_success(hr, "CoRevokeClassObject");
1676
1677 /* CLSCTX_LOCAL_SERVER */
1678
1679 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
1680 CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE, &cookie);
1681 ok_ole_success(hr, "CoRegisterClassObject");
1682
1683 thread = CreateThread(NULL, 0, get_class_object_proxy_thread, (LPVOID)CLSCTX_LOCAL_SERVER, 0, &tid);
1684 ok(thread != NULL, "CreateThread failed with error %d\n", GetLastError());
1685 while (MsgWaitForMultipleObjects(1, &thread, FALSE, 10000, QS_ALLINPUT) == WAIT_OBJECT_0 + 1)
1686 {
1687 MSG msg;
1688 while (PeekMessageA(&msg, NULL, 0, 0, PM_REMOVE))
1689 {
1690 TranslateMessage(&msg);
1691 DispatchMessageA(&msg);
1692 }
1693 }
1694 GetExitCodeThread(thread, &exitcode);
1695 hr = exitcode;
1696 ok(hr == S_OK, "CoGetClassObject on local server object "
1697 "registered in different thread should return S_OK "
1698 "instead of 0x%08x\n", hr);
1699
1700 hr = get_class_object(CLSCTX_LOCAL_SERVER);
1701 ok(hr == S_OK, "CoGetClassObject on local server object registered in same "
1702 "thread should return S_OK instead of 0x%08x\n", hr);
1703
1704 thread = CreateThread(NULL, 0, revoke_class_object_thread, (LPVOID)(DWORD_PTR)cookie, 0, &tid);
1705 ok(thread != NULL, "CreateThread failed with error %d\n", GetLastError());
1706 ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
1707 GetExitCodeThread(thread, &exitcode);
1708 hr = exitcode;
1709 ok(hr == RPC_E_WRONG_THREAD || broken(hr == S_OK) /* win8 */, "CoRevokeClassObject called from different "
1710 "thread to where registered should return RPC_E_WRONG_THREAD instead of 0x%08x\n", hr);
1711
1712 thread = CreateThread(NULL, 0, register_class_object_thread, NULL, 0, &tid);
1713 ok(thread != NULL, "CreateThread failed with error %d\n", GetLastError());
1714 ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
1715 GetExitCodeThread(thread, &exitcode);
1716 hr = exitcode;
1717 ok(hr == S_OK, "CoRegisterClassObject with same CLSID but in different "
1718 "thread should return S_OK instead of 0x%08x\n", hr);
1719
1720 hr = CoRevokeClassObject(cookie);
1721 ok_ole_success(hr, "CoRevokeClassObject");
1722
1723 CoUninitialize();
1724 }
1725
1726 static DWORD CALLBACK free_libraries_thread(LPVOID p)
1727 {
1728 CoFreeUnusedLibraries();
1729 return 0;
1730 }
1731
1732 static inline BOOL is_module_loaded(const char *module)
1733 {
1734 return GetModuleHandleA(module) != 0;
1735 }
1736
1737 static void test_CoFreeUnusedLibraries(void)
1738 {
1739 HRESULT hr;
1740 IUnknown *pUnk;
1741 DWORD tid;
1742 HANDLE thread;
1743
1744 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1745
1746 ok(!is_module_loaded("urlmon.dll"), "urlmon.dll shouldn't be loaded\n");
1747
1748 hr = CoCreateInstance(&CLSID_FileProtocol, NULL, CLSCTX_INPROC_SERVER, &IID_IInternetProtocol, (void **)&pUnk);
1749 if (hr == REGDB_E_CLASSNOTREG)
1750 {
1751 skip("IE not installed so can't run CoFreeUnusedLibraries test\n");
1752 CoUninitialize();
1753 return;
1754 }
1755 ok_ole_success(hr, "CoCreateInstance");
1756
1757 ok(is_module_loaded("urlmon.dll"), "urlmon.dll should be loaded\n");
1758
1759 ok(pUnk != NULL ||
1760 broken(pUnk == NULL), /* win9x */
1761 "Expected a valid pointer\n");
1762 if (pUnk)
1763 IUnknown_Release(pUnk);
1764
1765 ok(is_module_loaded("urlmon.dll"), "urlmon.dll should be loaded\n");
1766
1767 thread = CreateThread(NULL, 0, free_libraries_thread, NULL, 0, &tid);
1768 ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
1769 CloseHandle(thread);
1770
1771 ok(is_module_loaded("urlmon.dll"), "urlmon.dll should be loaded\n");
1772
1773 CoFreeUnusedLibraries();
1774
1775 ok(!is_module_loaded("urlmon.dll"), "urlmon.dll shouldn't be loaded\n");
1776
1777 CoUninitialize();
1778 }
1779
1780 static void test_CoGetObjectContext(void)
1781 {
1782 HRESULT hr;
1783 ULONG refs;
1784 IComThreadingInfo *pComThreadingInfo, *threadinginfo2;
1785 IContextCallback *pContextCallback;
1786 IObjContext *pObjContext;
1787 APTTYPE apttype;
1788 THDTYPE thdtype;
1789 GUID id, id2;
1790
1791 if (!pCoGetObjectContext)
1792 {
1793 win_skip("CoGetObjectContext not present\n");
1794 return;
1795 }
1796
1797 hr = pCoGetObjectContext(&IID_IComThreadingInfo, (void **)&pComThreadingInfo);
1798 ok(hr == CO_E_NOTINITIALIZED, "CoGetObjectContext should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n", hr);
1799 ok(pComThreadingInfo == NULL, "pComThreadingInfo should have been set to NULL\n");
1800
1801 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1802
1803 test_apt_type(APTTYPE_MAINSTA, APTTYPEQUALIFIER_NONE);
1804
1805 hr = pCoGetObjectContext(&IID_IComThreadingInfo, (void **)&pComThreadingInfo);
1806 ok_ole_success(hr, "CoGetObjectContext");
1807
1808 threadinginfo2 = NULL;
1809 hr = pCoGetObjectContext(&IID_IComThreadingInfo, (void **)&threadinginfo2);
1810 ok(hr == S_OK, "Expected S_OK, got 0x%08x\n", hr);
1811 ok(pComThreadingInfo == threadinginfo2, "got different instance\n");
1812 IComThreadingInfo_Release(threadinginfo2);
1813
1814 hr = IComThreadingInfo_GetCurrentLogicalThreadId(pComThreadingInfo, NULL);
1815 ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
1816
1817 id = id2 = GUID_NULL;
1818 hr = IComThreadingInfo_GetCurrentLogicalThreadId(pComThreadingInfo, &id);
1819 ok(hr == S_OK, "got 0x%08x\n", hr);
1820
1821 hr = CoGetCurrentLogicalThreadId(&id2);
1822 ok(IsEqualGUID(&id, &id2), "got %s, expected %s\n", wine_dbgstr_guid(&id), wine_dbgstr_guid(&id2));
1823
1824 hr = IComThreadingInfo_GetCurrentApartmentType(pComThreadingInfo, &apttype);
1825 ok_ole_success(hr, "IComThreadingInfo_GetCurrentApartmentType");
1826 ok(apttype == APTTYPE_MAINSTA, "apartment type should be APTTYPE_MAINSTA instead of %d\n", apttype);
1827
1828 hr = IComThreadingInfo_GetCurrentThreadType(pComThreadingInfo, &thdtype);
1829 ok_ole_success(hr, "IComThreadingInfo_GetCurrentThreadType");
1830 ok(thdtype == THDTYPE_PROCESSMESSAGES, "thread type should be THDTYPE_PROCESSMESSAGES instead of %d\n", thdtype);
1831
1832 refs = IComThreadingInfo_Release(pComThreadingInfo);
1833 ok(refs == 0, "pComThreadingInfo should have 0 refs instead of %d refs\n", refs);
1834
1835 hr = pCoGetObjectContext(&IID_IContextCallback, (void **)&pContextCallback);
1836 ok_ole_success(hr, "CoGetObjectContext(ContextCallback)");
1837
1838 refs = IContextCallback_Release(pContextCallback);
1839 ok(refs == 0, "pContextCallback should have 0 refs instead of %d refs\n", refs);
1840
1841 CoUninitialize();
1842
1843 pCoInitializeEx(NULL, COINIT_MULTITHREADED);
1844
1845 hr = pCoGetObjectContext(&IID_IComThreadingInfo, (void **)&pComThreadingInfo);
1846 ok_ole_success(hr, "CoGetObjectContext");
1847
1848 hr = IComThreadingInfo_GetCurrentApartmentType(pComThreadingInfo, &apttype);
1849 ok_ole_success(hr, "IComThreadingInfo_GetCurrentApartmentType");
1850 ok(apttype == APTTYPE_MTA, "apartment type should be APTTYPE_MTA instead of %d\n", apttype);
1851
1852 hr = IComThreadingInfo_GetCurrentThreadType(pComThreadingInfo, &thdtype);
1853 ok_ole_success(hr, "IComThreadingInfo_GetCurrentThreadType");
1854 ok(thdtype == THDTYPE_BLOCKMESSAGES, "thread type should be THDTYPE_BLOCKMESSAGES instead of %d\n", thdtype);
1855
1856 refs = IComThreadingInfo_Release(pComThreadingInfo);
1857 ok(refs == 0, "pComThreadingInfo should have 0 refs instead of %d refs\n", refs);
1858
1859 hr = pCoGetObjectContext(&IID_IContextCallback, (void **)&pContextCallback);
1860 ok_ole_success(hr, "CoGetObjectContext(ContextCallback)");
1861
1862 refs = IContextCallback_Release(pContextCallback);
1863 ok(refs == 0, "pContextCallback should have 0 refs instead of %d refs\n", refs);
1864
1865 hr = pCoGetObjectContext(&IID_IObjContext, (void **)&pObjContext);
1866 ok_ole_success(hr, "CoGetObjectContext");
1867
1868 refs = IObjContext_Release(pObjContext);
1869 ok(refs == 0, "pObjContext should have 0 refs instead of %d refs\n", refs);
1870
1871 CoUninitialize();
1872 }
1873
1874 typedef struct {
1875 IUnknown IUnknown_iface;
1876 LONG refs;
1877 } Test_CallContext;
1878
1879 static inline Test_CallContext *impl_from_IUnknown(IUnknown *iface)
1880 {
1881 return CONTAINING_RECORD(iface, Test_CallContext, IUnknown_iface);
1882 }
1883
1884 static HRESULT WINAPI Test_CallContext_QueryInterface(
1885 IUnknown *iface,
1886 REFIID riid,
1887 LPVOID *ppvObj)
1888 {
1889 if (ppvObj == NULL) return E_POINTER;
1890
1891 if (IsEqualGUID(riid, &IID_IUnknown))
1892 {
1893 *ppvObj = iface;
1894 IUnknown_AddRef(iface);
1895 return S_OK;
1896 }
1897
1898 *ppvObj = NULL;
1899 return E_NOINTERFACE;
1900 }
1901
1902 static ULONG WINAPI Test_CallContext_AddRef(IUnknown *iface)
1903 {
1904 Test_CallContext *This = impl_from_IUnknown(iface);
1905 return InterlockedIncrement(&This->refs);
1906 }
1907
1908 static ULONG WINAPI Test_CallContext_Release(IUnknown *iface)
1909 {
1910 Test_CallContext *This = impl_from_IUnknown(iface);
1911 ULONG refs = InterlockedDecrement(&This->refs);
1912 if (!refs)
1913 HeapFree(GetProcessHeap(), 0, This);
1914 return refs;
1915 }
1916
1917 static const IUnknownVtbl TestCallContext_Vtbl =
1918 {
1919 Test_CallContext_QueryInterface,
1920 Test_CallContext_AddRef,
1921 Test_CallContext_Release
1922 };
1923
1924 static void test_CoGetCallContext(void)
1925 {
1926 HRESULT hr;
1927 ULONG refs;
1928 IUnknown *pUnk;
1929 Test_CallContext *test_object;
1930
1931 if (!pCoSwitchCallContext)
1932 {
1933 skip("CoSwitchCallContext not present\n");
1934 return;
1935 }
1936
1937 CoInitialize(NULL);
1938
1939 test_object = HeapAlloc(GetProcessHeap(), 0, sizeof(Test_CallContext));
1940 test_object->IUnknown_iface.lpVtbl = &TestCallContext_Vtbl;
1941 test_object->refs = 1;
1942
1943 hr = CoGetCallContext(&IID_IUnknown, (void**)&pUnk);
1944 ok(hr == RPC_E_CALL_COMPLETE, "Expected RPC_E_CALL_COMPLETE, got 0x%08x\n", hr);
1945
1946 pUnk = (IUnknown*)0xdeadbeef;
1947 hr = pCoSwitchCallContext(&test_object->IUnknown_iface, &pUnk);
1948 ok_ole_success(hr, "CoSwitchCallContext");
1949 ok(pUnk == NULL, "expected NULL, got %p\n", pUnk);
1950 refs = IUnknown_AddRef(&test_object->IUnknown_iface);
1951 ok(refs == 2, "Expected refcount 2, got %d\n", refs);
1952 IUnknown_Release(&test_object->IUnknown_iface);
1953
1954 pUnk = (IUnknown*)0xdeadbeef;
1955 hr = CoGetCallContext(&IID_IUnknown, (void**)&pUnk);
1956 ok_ole_success(hr, "CoGetCallContext");
1957 ok(pUnk == &test_object->IUnknown_iface, "expected %p, got %p\n",
1958 &test_object->IUnknown_iface, pUnk);
1959 refs = IUnknown_AddRef(&test_object->IUnknown_iface);
1960 ok(refs == 3, "Expected refcount 3, got %d\n", refs);
1961 IUnknown_Release(&test_object->IUnknown_iface);
1962 IUnknown_Release(pUnk);
1963
1964 pUnk = (IUnknown*)0xdeadbeef;
1965 hr = pCoSwitchCallContext(NULL, &pUnk);
1966 ok_ole_success(hr, "CoSwitchCallContext");
1967 ok(pUnk == &test_object->IUnknown_iface, "expected %p, got %p\n",
1968 &test_object->IUnknown_iface, pUnk);
1969 refs = IUnknown_AddRef(&test_object->IUnknown_iface);
1970 ok(refs == 2, "Expected refcount 2, got %d\n", refs);
1971 IUnknown_Release(&test_object->IUnknown_iface);
1972
1973 hr = CoGetCallContext(&IID_IUnknown, (void**)&pUnk);
1974 ok(hr == RPC_E_CALL_COMPLETE, "Expected RPC_E_CALL_COMPLETE, got 0x%08x\n", hr);
1975
1976 IUnknown_Release(&test_object->IUnknown_iface);
1977
1978 CoUninitialize();
1979 }
1980
1981 static void test_CoGetContextToken(void)
1982 {
1983 HRESULT hr;
1984 ULONG refs;
1985 ULONG_PTR token, token2;
1986 IObjContext *ctx;
1987
1988 if (!pCoGetContextToken)
1989 {
1990 win_skip("CoGetContextToken not present\n");
1991 return;
1992 }
1993
1994 token = 0xdeadbeef;
1995 hr = pCoGetContextToken(&token);
1996 ok(hr == CO_E_NOTINITIALIZED, "Expected CO_E_NOTINITIALIZED, got 0x%08x\n", hr);
1997 ok(token == 0xdeadbeef, "Expected 0, got 0x%lx\n", token);
1998
1999 test_apt_type(APTTYPE_CURRENT, APTTYPEQUALIFIER_NONE);
2000
2001 CoInitialize(NULL);
2002
2003 test_apt_type(APTTYPE_MAINSTA, APTTYPEQUALIFIER_NONE);
2004
2005 hr = pCoGetContextToken(NULL);
2006 ok(hr == E_POINTER, "Expected E_POINTER, got 0x%08x\n", hr);
2007
2008 token = 0;
2009 hr = pCoGetContextToken(&token);
2010 ok(hr == S_OK, "Expected S_OK, got 0x%08x\n", hr);
2011 ok(token, "Expected token != 0\n");
2012
2013 token2 = 0;
2014 hr = pCoGetContextToken(&token2);
2015 ok(hr == S_OK, "Expected S_OK, got 0x%08x\n", hr);
2016 ok(token2 == token, "got different token\n");
2017
2018 refs = IUnknown_AddRef((IUnknown *)token);
2019 ok(refs == 1, "Expected 1, got %u\n", refs);
2020
2021 hr = pCoGetObjectContext(&IID_IObjContext, (void **)&ctx);
2022 ok(hr == S_OK, "Expected S_OK, got 0x%08x\n", hr);
2023 ok(ctx == (IObjContext *)token, "Expected interface pointers to be the same\n");
2024
2025 refs = IObjContext_AddRef(ctx);
2026 ok(refs == 3, "Expected 3, got %u\n", refs);
2027
2028 refs = IObjContext_Release(ctx);
2029 ok(refs == 2, "Expected 2, got %u\n", refs);
2030
2031 refs = IUnknown_Release((IUnknown *)token);
2032 ok(refs == 1, "Expected 1, got %u\n", refs);
2033
2034 /* CoGetContextToken does not add a reference */
2035 token = 0;
2036 hr = pCoGetContextToken(&token);
2037 ok(hr == S_OK, "Expected S_OK, got 0x%08x\n", hr);
2038 ok(token, "Expected token != 0\n");
2039 ok(ctx == (IObjContext *)token, "Expected interface pointers to be the same\n");
2040
2041 refs = IObjContext_AddRef(ctx);
2042 ok(refs == 2, "Expected 1, got %u\n", refs);
2043
2044 refs = IObjContext_Release(ctx);
2045 ok(refs == 1, "Expected 0, got %u\n", refs);
2046
2047 refs = IObjContext_Release(ctx);
2048 ok(refs == 0, "Expected 0, got %u\n", refs);
2049
2050 CoUninitialize();
2051 }
2052
2053 static void test_TreatAsClass(void)
2054 {
2055 HRESULT hr;
2056 CLSID out;
2057 static GUID deadbeef = {0xdeadbeef,0xdead,0xbeef,{0xde,0xad,0xbe,0xef,0xde,0xad,0xbe,0xef}};
2058 static const char deadbeefA[] = "{DEADBEEF-DEAD-BEEF-DEAD-BEEFDEADBEEF}";
2059 IInternetProtocol *pIP = NULL;
2060 HKEY clsidkey, deadbeefkey;
2061 LONG lr;
2062
2063 if (!pCoGetTreatAsClass)
2064 {
2065 win_skip("CoGetTreatAsClass not present\n");
2066 return;
2067 }
2068
2069 hr = pCoGetTreatAsClass(&deadbeef,&out);
2070 ok (hr == S_FALSE, "expected S_FALSE got %x\n",hr);
2071 ok (IsEqualGUID(&out,&deadbeef), "expected to get same clsid back\n");
2072
2073 hr = pCoGetTreatAsClass(NULL, &out);
2074 ok(hr == E_INVALIDARG, "expected E_INVALIDARG got %08x\n", hr);
2075 ok(IsEqualGUID(&out, &deadbeef), "expected no change to the clsid\n");
2076
2077 hr = pCoGetTreatAsClass(&deadbeef, NULL);
2078 ok(hr == E_INVALIDARG, "expected E_INVALIDARG got %08x\n", hr);
2079
2080 lr = RegOpenKeyExA(HKEY_CLASSES_ROOT, "CLSID", 0, KEY_READ, &clsidkey);
2081 ok(!lr, "Couldn't open CLSID key, error %d\n", lr);
2082
2083 lr = RegCreateKeyExA(clsidkey, deadbeefA, 0, NULL, 0, KEY_WRITE, NULL, &deadbeefkey, NULL);
2084 if (lr) {
2085 win_skip("CoGetTreatAsClass() tests will be skipped (failed to create a test key, error %d)\n", lr);
2086 RegCloseKey(clsidkey);
2087 return;
2088 }
2089
2090 hr = pCoTreatAsClass(&deadbeef, &deadbeef);
2091 ok(hr == REGDB_E_WRITEREGDB, "CoTreatAsClass gave wrong error: %08x\n", hr);
2092
2093 hr = pCoTreatAsClass(&deadbeef, &CLSID_FileProtocol);
2094 if(hr == REGDB_E_WRITEREGDB){
2095 win_skip("Insufficient privileges to use CoTreatAsClass\n");
2096 goto exit;
2097 }
2098 ok(hr == S_OK, "CoTreatAsClass failed: %08x\n", hr);
2099
2100 hr = pCoGetTreatAsClass(&deadbeef, &out);
2101 ok(hr == S_OK, "CoGetTreatAsClass failed: %08x\n",hr);
2102 ok(IsEqualGUID(&out, &CLSID_FileProtocol), "expected to get substituted clsid\n");
2103
2104 OleInitialize(NULL);
2105
2106 hr = CoCreateInstance(&deadbeef, NULL, CLSCTX_INPROC_SERVER, &IID_IInternetProtocol, (void **)&pIP);
2107 if(hr == REGDB_E_CLASSNOTREG)
2108 {
2109 win_skip("IE not installed so can't test CoCreateInstance\n");
2110 goto exit;
2111 }
2112
2113 ok(hr == S_OK, "CoCreateInstance failed: %08x\n", hr);
2114 if(pIP){
2115 IInternetProtocol_Release(pIP);
2116 pIP = NULL;
2117 }
2118
2119 hr = pCoTreatAsClass(&deadbeef, &CLSID_NULL);
2120 ok(hr == S_OK, "CoTreatAsClass failed: %08x\n", hr);
2121
2122 hr = pCoGetTreatAsClass(&deadbeef, &out);
2123 ok(hr == S_FALSE, "expected S_FALSE got %08x\n", hr);
2124 ok(IsEqualGUID(&out, &deadbeef), "expected to get same clsid back\n");
2125
2126 /* bizarrely, native's CoTreatAsClass takes some time to take effect in CoCreateInstance */
2127 Sleep(200);
2128
2129 hr = CoCreateInstance(&deadbeef, NULL, CLSCTX_INPROC_SERVER, &IID_IInternetProtocol, (void **)&pIP);
2130 ok(hr == REGDB_E_CLASSNOTREG, "CoCreateInstance gave wrong error: %08x\n", hr);
2131
2132 if(pIP)
2133 IInternetProtocol_Release(pIP);
2134
2135 exit:
2136 OleUninitialize();
2137 RegCloseKey(deadbeefkey);
2138 RegDeleteKeyA(clsidkey, deadbeefA);
2139 RegCloseKey(clsidkey);
2140 }
2141
2142 static void test_CoInitializeEx(void)
2143 {
2144 HRESULT hr;
2145
2146 hr = pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
2147 ok(hr == S_OK, "CoInitializeEx failed with error 0x%08x\n", hr);
2148
2149 /* Calling OleInitialize for the first time should yield S_OK even with
2150 * apartment already initialized by previous CoInitialize(Ex) calls. */
2151 hr = OleInitialize(NULL);
2152 ok(hr == S_OK, "OleInitialize failed with error 0x%08x\n", hr);
2153
2154 /* Subsequent calls to OleInitialize should return S_FALSE */
2155 hr = OleInitialize(NULL);
2156 ok(hr == S_FALSE, "Expected S_FALSE, hr = 0x%08x\n", hr);
2157
2158 /* Cleanup */
2159 CoUninitialize();
2160 OleUninitialize();
2161 OleUninitialize();
2162 }
2163
2164 static void test_OleInitialize_InitCounting(void)
2165 {
2166 HRESULT hr;
2167 IUnknown *pUnk;
2168 REFCLSID rclsid = &CLSID_InternetZoneManager;
2169
2170 /* 1. OleInitialize fails but OleUninitialize is still called: apartment stays initialized */
2171 hr = pCoInitializeEx(NULL, COINIT_MULTITHREADED);
2172 ok(hr == S_OK, "CoInitializeEx(COINIT_MULTITHREADED) failed with error 0x%08x\n", hr);
2173
2174 hr = OleInitialize(NULL);
2175 ok(hr == RPC_E_CHANGED_MODE, "OleInitialize should have returned 0x%08x instead of 0x%08x\n", RPC_E_CHANGED_MODE, hr);
2176 OleUninitialize();
2177
2178 pUnk = (IUnknown *)0xdeadbeef;
2179 hr = CoCreateInstance(rclsid, NULL, 0x17, &IID_IUnknown, (void **)&pUnk);
2180 ok(hr == S_OK, "CoCreateInstance should have returned 0x%08x instead of 0x%08x\n", S_OK, hr);
2181 if (pUnk) IUnknown_Release(pUnk);
2182
2183 CoUninitialize();
2184
2185 /* 2. Extra multiple OleUninitialize: apartment stays initialized until CoUninitialize */
2186 hr = CoInitialize(NULL);
2187 ok(hr == S_OK, "CoInitialize() failed with error 0x%08x\n", hr);
2188
2189 hr = OleInitialize(NULL);
2190 ok(hr == S_OK, "OleInitialize should have returned 0x%08x instead of 0x%08x\n", S_OK, hr);
2191 OleUninitialize();
2192 OleUninitialize();
2193 OleUninitialize();
2194
2195 pUnk = (IUnknown *)0xdeadbeef;
2196 hr = CoCreateInstance(rclsid, NULL, 0x17, &IID_IUnknown, (void **)&pUnk);
2197 ok(hr == S_OK, "CoCreateInstance should have returned 0x%08x instead of 0x%08x\n", S_OK, hr);
2198 if (pUnk) IUnknown_Release(pUnk);
2199
2200 CoUninitialize();
2201
2202 pUnk = (IUnknown *)0xdeadbeef;
2203 hr = CoCreateInstance(rclsid, NULL, 0x17, &IID_IUnknown, (void **)&pUnk);
2204 ok(hr == CO_E_NOTINITIALIZED, "CoCreateInstance should have returned 0x%08x instead of 0x%08x\n", CO_E_NOTINITIALIZED, hr);
2205 if (pUnk) IUnknown_Release(pUnk);
2206
2207 /* 3. CoUninitialize does not formally deinit Ole */
2208 hr = CoInitialize(NULL);
2209 ok(hr == S_OK, "CoInitialize() failed with error 0x%08x\n", hr);
2210
2211 hr = OleInitialize(NULL);
2212 ok(hr == S_OK, "OleInitialize should have returned 0x%08x instead of 0x%08x\n", S_OK, hr);
2213
2214 CoUninitialize();
2215 CoUninitialize();
2216
2217 pUnk = (IUnknown *)0xdeadbeef;
2218 hr = CoCreateInstance(rclsid, NULL, 0x17, &IID_IUnknown, (void **)&pUnk);
2219 ok(hr == CO_E_NOTINITIALIZED, "CoCreateInstance should have returned 0x%08x instead of 0x%08x\n", CO_E_NOTINITIALIZED, hr);
2220 /* COM is not initialized anymore */
2221 if (pUnk) IUnknown_Release(pUnk);
2222
2223 hr = OleInitialize(NULL);
2224 ok(hr == S_FALSE, "OleInitialize should have returned 0x%08x instead of 0x%08x\n", S_FALSE, hr);
2225 /* ... but native OleInit returns S_FALSE as if Ole is considered initialized */
2226
2227 OleUninitialize();
2228
2229 }
2230
2231 static void test_OleRegGetMiscStatus(void)
2232 {
2233 ULONG_PTR cookie;
2234 HANDLE handle;
2235 DWORD status;
2236 HRESULT hr;
2237
2238 hr = OleRegGetMiscStatus(&CLSID_Testclass, DVASPECT_ICON, NULL);
2239 ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
2240
2241 status = 0xdeadbeef;
2242 hr = OleRegGetMiscStatus(&CLSID_Testclass, DVASPECT_ICON, &status);
2243 ok(hr == REGDB_E_CLASSNOTREG, "got 0x%08x\n", hr);
2244 ok(status == 0, "got 0x%08x\n", status);
2245
2246 status = -1;
2247 hr = OleRegGetMiscStatus(&CLSID_StdFont, DVASPECT_ICON, &status);
2248 ok(hr == S_OK, "got 0x%08x\n", hr);
2249 ok(status == 0, "got 0x%08x\n", status);
2250
2251 if ((handle = activate_context(actctx_manifest, &cookie)))
2252 {
2253 status = 0;
2254 hr = OleRegGetMiscStatus(&CLSID_Testclass, DVASPECT_ICON, &status);
2255 ok(hr == S_OK, "got 0x%08x\n", hr);
2256 ok(status == OLEMISC_RECOMPOSEONRESIZE, "got 0x%08x\n", status);
2257
2258 /* context data takes precedence over registration info */
2259 status = 0;
2260 hr = OleRegGetMiscStatus(&CLSID_StdFont, DVASPECT_ICON, &status);
2261 ok(hr == S_OK, "got 0x%08x\n", hr);
2262 ok(status == OLEMISC_RECOMPOSEONRESIZE, "got 0x%08x\n", status);
2263
2264 /* there's no such attribute in context */
2265 status = -1;
2266 hr = OleRegGetMiscStatus(&CLSID_Testclass, DVASPECT_DOCPRINT, &status);
2267 ok(hr == S_OK, "got 0x%08x\n", hr);
2268 ok(status == 0, "got 0x%08x\n", status);
2269
2270 pDeactivateActCtx(0, cookie);
2271 pReleaseActCtx(handle);
2272 }
2273 }
2274
2275 static void test_OleRegGetUserType(void)
2276 {
2277 static const WCHAR stdfont_usertypeW[] = {'S','t','a','n','d','a','r','d',' ','F','o','n','t',0};
2278 static const WCHAR stdfont2_usertypeW[] = {'C','L','S','I','D','_','S','t','d','F','o','n','t',0};
2279 static const WCHAR clsidkeyW[] = {'C','L','S','I','D',0};
2280 static const WCHAR defvalueW[] = {'D','e','f','a','u','l','t',' ','N','a','m','e',0};
2281 static const WCHAR auxvalue0W[] = {'A','u','x',' ','N','a','m','e',' ','0',0};
2282 static const WCHAR auxvalue2W[] = {'A','u','x',' ','N','a','m','e',' ','2',0};
2283 static const WCHAR auxvalue3W[] = {'A','u','x',' ','N','a','m','e',' ','3',0};
2284 static const WCHAR auxvalue4W[] = {'A','u','x',' ','N','a','m','e',' ','4',0};
2285
2286 static const char auxvalues[][16] = {
2287 "Aux Name 0",
2288 "Aux Name 1",
2289 "Aux Name 2",
2290 "Aux Name 3",
2291 "Aux Name 4"
2292 };
2293
2294 HKEY clsidhkey, hkey, auxhkey, classkey;
2295 DWORD form, ret, disposition;
2296 WCHAR clsidW[39];
2297 ULONG_PTR cookie;
2298 HANDLE handle;
2299 HRESULT hr;
2300 WCHAR *str;
2301 int i;
2302
2303 for (form = 0; form <= USERCLASSTYPE_APPNAME+1; form++) {
2304 hr = OleRegGetUserType(&CLSID_Testclass, form, NULL);
2305 ok(hr == E_INVALIDARG, "form %u: got 0x%08x\n", form, hr);
2306
2307 str = (void*)0xdeadbeef;
2308 hr = OleRegGetUserType(&CLSID_Testclass, form, &str);
2309 ok(hr == REGDB_E_CLASSNOTREG, "form %u: got 0x%08x\n", form, hr);
2310 ok(str == NULL, "form %u: got %p\n", form, str);
2311
2312 /* same string returned for StdFont for all form types */
2313 str = NULL;
2314 hr = OleRegGetUserType(&CLSID_StdFont, form, &str);
2315 ok(hr == S_OK, "form %u: got 0x%08x\n", form, hr);
2316 ok(!lstrcmpW(str, stdfont_usertypeW) || !lstrcmpW(str, stdfont2_usertypeW) /* winxp */,
2317 "form %u, got %s\n", form, wine_dbgstr_w(str));
2318 CoTaskMemFree(str);
2319 }
2320
2321 if ((handle = activate_context(actctx_manifest, &cookie)))
2322 {
2323 for (form = 0; form <= USERCLASSTYPE_APPNAME+1; form++) {
2324 str = (void*)0xdeadbeef;
2325 hr = OleRegGetUserType(&CLSID_Testclass, form, &str);
2326 ok(hr == REGDB_E_CLASSNOTREG, "form %u: got 0x%08x\n", form, hr);
2327 ok(str == NULL, "form %u: got %s\n", form, wine_dbgstr_w(str));
2328
2329 /* same string returned for StdFont for all form types */
2330 str = NULL;
2331 hr = OleRegGetUserType(&CLSID_StdFont, form, &str);
2332 ok(hr == S_OK, "form %u: got 0x%08x\n", form, hr);
2333 ok(!lstrcmpW(str, stdfont_usertypeW) || !lstrcmpW(str, stdfont2_usertypeW) /* winxp */,
2334 "form %u, got %s\n", form, wine_dbgstr_w(str));
2335 CoTaskMemFree(str);
2336 }
2337
2338 pDeactivateActCtx(0, cookie);
2339 pReleaseActCtx(handle);
2340 }
2341
2342 /* test using registered CLSID */
2343 StringFromGUID2(&CLSID_non_existent, clsidW, sizeof(clsidW)/sizeof(clsidW[0]));
2344
2345 ret = RegCreateKeyExW(HKEY_CLASSES_ROOT, clsidkeyW, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &clsidhkey, &disposition);
2346 if (!ret)
2347 {
2348 ret = RegCreateKeyExW(clsidhkey, clsidW, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &classkey, NULL);
2349 if (ret)
2350 RegCloseKey(clsidhkey);
2351 }
2352
2353 if (ret == ERROR_ACCESS_DENIED)
2354 {
2355 win_skip("Failed to create test key, skipping some of OleRegGetUserType() tests.\n");
2356 return;
2357 }
2358
2359 ok(!ret, "failed to create a key, error %d\n", ret);
2360
2361 ret = RegSetValueExW(classkey, NULL, 0, REG_SZ, (const BYTE*)defvalueW, sizeof(defvalueW));
2362 ok(!ret, "got error %d\n", ret);
2363
2364 ret = RegCreateKeyExA(classkey, "AuxUserType", 0, NULL, 0, KEY_ALL_ACCESS, NULL, &auxhkey, NULL);
2365 ok(!ret, "got error %d\n", ret);
2366
2367 /* populate AuxUserType */
2368 for (i = 0; i <= 4; i++) {
2369 char name[16];
2370
2371 sprintf(name, "AuxUserType\\%d", i);
2372 ret = RegCreateKeyExA(classkey, name, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hkey, NULL);
2373 ok(!ret, "got error %d\n", ret);
2374
2375 ret = RegSetValueExA(hkey, NULL, 0, REG_SZ, (const BYTE*)auxvalues[i], strlen(auxvalues[i]));
2376 ok(!ret, "got error %d\n", ret);
2377 RegCloseKey(hkey);
2378 }
2379
2380 str = NULL;
2381 hr = OleRegGetUserType(&CLSID_non_existent, 0, &str);
2382 ok(hr == S_OK, "got 0x%08x\n", hr);
2383 ok(!lstrcmpW(str, auxvalue0W), "got %s\n", wine_dbgstr_w(str));
2384 CoTaskMemFree(str);
2385
2386 str = NULL;
2387 hr = OleRegGetUserType(&CLSID_non_existent, USERCLASSTYPE_FULL, &str);
2388 ok(hr == S_OK, "got 0x%08x\n", hr);
2389 ok(!lstrcmpW(str, defvalueW), "got %s\n", wine_dbgstr_w(str));
2390 CoTaskMemFree(str);
2391
2392 str = NULL;
2393 hr = OleRegGetUserType(&CLSID_non_existent, USERCLASSTYPE_SHORT, &str);
2394 ok(hr == S_OK, "got 0x%08x\n", hr);
2395 ok(!lstrcmpW(str, auxvalue2W), "got %s\n", wine_dbgstr_w(str));
2396 CoTaskMemFree(str);
2397
2398 str = NULL;
2399 hr = OleRegGetUserType(&CLSID_non_existent, USERCLASSTYPE_APPNAME, &str);
2400 ok(hr == S_OK, "got 0x%08x\n", hr);
2401 ok(!lstrcmpW(str, auxvalue3W), "got %s\n", wine_dbgstr_w(str));
2402 CoTaskMemFree(str);
2403
2404 str = NULL;
2405 hr = OleRegGetUserType(&CLSID_non_existent, USERCLASSTYPE_APPNAME+1, &str);
2406 ok(hr == S_OK, "got 0x%08x\n", hr);
2407 ok(!lstrcmpW(str, auxvalue4W), "got %s\n", wine_dbgstr_w(str));
2408 CoTaskMemFree(str);
2409
2410 str = NULL;
2411 hr = OleRegGetUserType(&CLSID_non_existent, USERCLASSTYPE_APPNAME+2, &str);
2412 ok(hr == S_OK, "got 0x%08x\n", hr);
2413 ok(!lstrcmpW(str, defvalueW), "got %s\n", wine_dbgstr_w(str));
2414 CoTaskMemFree(str);
2415
2416 /* registry cleanup */
2417 for (i = 0; i <= 4; i++)
2418 {
2419 char name[2];
2420 sprintf(name, "%d", i);
2421 RegDeleteKeyA(auxhkey, name);
2422 }
2423 RegCloseKey(auxhkey);
2424 RegDeleteKeyA(classkey, "AuxUserType");
2425 RegCloseKey(classkey);
2426 RegDeleteKeyW(clsidhkey, clsidW);
2427 RegCloseKey(clsidhkey);
2428 if (disposition == REG_CREATED_NEW_KEY)
2429 RegDeleteKeyA(HKEY_CLASSES_ROOT, "CLSID");
2430 }
2431
2432 static void test_CoCreateGuid(void)
2433 {
2434 HRESULT hr;
2435
2436 hr = CoCreateGuid(NULL);
2437 ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
2438 }
2439
2440 static void CALLBACK apc_test_proc(ULONG_PTR param)
2441 {
2442 /* nothing */
2443 }
2444
2445 static DWORD CALLBACK release_semaphore_thread( LPVOID arg )
2446 {
2447 HANDLE handle = arg;
2448 if (WaitForSingleObject(handle, 200) == WAIT_TIMEOUT)
2449 ReleaseSemaphore(handle, 1, NULL);
2450 return 0;
2451 }
2452
2453 static DWORD CALLBACK send_message_thread(LPVOID arg)
2454 {
2455 HWND hWnd = arg;
2456 Sleep(50);
2457 SendMessageA(hWnd, WM_DDE_FIRST, 0, 0);
2458 return 0;
2459 }
2460
2461 static DWORD CALLBACK send_and_post_user_message_thread(void *arg)
2462 {
2463 HWND hwnd = arg;
2464 Sleep(30);
2465 SendMessageA(hwnd, WM_USER, 0, 0);
2466 PostMessageA(hwnd, WM_USER, 0, 0);
2467 return 0;
2468 }
2469
2470 static DWORD CALLBACK post_message_thread(LPVOID arg)
2471 {
2472 HWND hWnd = arg;
2473 Sleep(50);
2474 PostMessageA(hWnd, WM_DDE_FIRST, 0, 0);
2475 return 0;
2476 }
2477
2478 static const char cls_name[] = "cowait_test_class";
2479
2480 static UINT cowait_msgs[100], cowait_msgs_first, cowait_msgs_last;
2481
2482 static void cowait_msgs_reset(void)
2483 {
2484 cowait_msgs_first = cowait_msgs_last = 0;
2485 }
2486
2487 #define cowait_msgs_expect_empty() _cowait_msgs_expect_empty(__LINE__)
2488 static void _cowait_msgs_expect_empty(unsigned line)
2489 {
2490 while(cowait_msgs_first < cowait_msgs_last) {
2491 ok_(__FILE__,line)(0, "unexpected message %u\n", cowait_msgs[cowait_msgs_first]);
2492 cowait_msgs_first++;
2493 }
2494 cowait_msgs_reset();
2495 }
2496
2497 #define cowait_msgs_expect_notified(a) _cowait_msgs_expect_notified(__LINE__,a)
2498 static void _cowait_msgs_expect_notified(unsigned line, UINT expected_msg)
2499 {
2500 if(cowait_msgs_first == cowait_msgs_last) {
2501 ok_(__FILE__,line)(0, "expected message %u, received none\n", expected_msg);
2502 }else {
2503 ok_(__FILE__,line)(cowait_msgs[cowait_msgs_first] == expected_msg,
2504 "expected message %u, received %u \n",
2505 expected_msg, cowait_msgs[cowait_msgs_first]);
2506 cowait_msgs_first++;
2507 }
2508 }
2509
2510 #define cowait_msgs_expect_queued(a,b) _cowait_msgs_expect_queued(__LINE__,a,b)
2511 static void _cowait_msgs_expect_queued(unsigned line, HWND hwnd, UINT expected_msg)
2512 {
2513 MSG msg;
2514 BOOL success;
2515
2516 success = PeekMessageA(&msg, hwnd, expected_msg, expected_msg, PM_REMOVE);
2517 ok_(__FILE__,line)(success, "PeekMessageA failed: %u\n", GetLastError());
2518 if(success)
2519 ok_(__FILE__,line)(msg.message == expected_msg, "unexpected message %u, expected %u\n",
2520 msg.message, expected_msg);
2521 }
2522
2523 static void flush_messages(void)
2524 {
2525 MSG msg;
2526 while (PeekMessageA( &msg, 0, 0, 0, PM_REMOVE ));
2527 }
2528
2529 static LRESULT CALLBACK cowait_window_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
2530 {
2531 if(cowait_msgs_last < sizeof(cowait_msgs)/sizeof(*cowait_msgs))
2532 cowait_msgs[cowait_msgs_last++] = msg;
2533 if(msg == WM_DDE_FIRST)
2534 return 6;
2535 return DefWindowProcA(hwnd, msg, wparam, lparam);
2536 }
2537
2538 static DWORD CALLBACK cowait_unmarshal_thread(void *arg)
2539 {
2540 IStream *stream = arg;
2541 IEnumOLEVERB *enum_verb;
2542 LARGE_INTEGER zero;
2543 IUnknown *unk;
2544 HRESULT hr;
2545
2546 CoInitialize(NULL);
2547
2548 zero.QuadPart = 0;
2549 hr = IStream_Seek(stream, zero, STREAM_SEEK_SET, NULL);
2550 ok(hr == S_OK, "Seek failed: %08x\n", hr);
2551
2552 hr = CoUnmarshalInterface(stream, &IID_IUnknown, (void**)&unk);
2553 ok(hr == S_OK, "CoUnmarshalInterface failed: %08x\n", hr);
2554
2555 hr = IUnknown_QueryInterface(unk, &IID_IEnumOLEVERB, (void**)&enum_verb);
2556 ok(hr == S_OK, "QueryInterface failed: %08x\n", hr);
2557
2558 IEnumOLEVERB_Release(enum_verb);
2559 IUnknown_Release(unk);
2560
2561 CoUninitialize();
2562 return 0;
2563 }
2564
2565 static DWORD CALLBACK test_CoWaitForMultipleHandles_thread(LPVOID arg)
2566 {
2567 HANDLE *handles = arg, event, thread;
2568 IStream *stream;
2569 BOOL success;
2570 DWORD index, tid;
2571 HRESULT hr;
2572 HWND hWnd;
2573 UINT uMSG = 0xc065;
2574 MSG msg;
2575 int ret;
2576
2577 hr = pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
2578 ok(hr == S_OK, "CoInitializeEx failed with error 0x%08x\n", hr);
2579
2580 hWnd = CreateWindowExA(0, cls_name, "Test (thread)", WS_TILEDWINDOW, 0, 0, 640, 480, 0, 0, 0, 0);
2581 ok(hWnd != 0, "CreateWindowExA failed %u\n", GetLastError());
2582
2583 index = 0xdeadbeef;
2584 PostMessageA(hWnd, WM_DDE_FIRST, 0, 0);
2585 hr = CoWaitForMultipleHandles(0, 50, 2, handles, &index);
2586 ok(hr == RPC_S_CALLPENDING, "expected RPC_S_CALLPENDING, got 0x%08x\n", hr);
2587 ok(index==0 || index==0xdeadbeef/* Win 8 */, "expected index 0, got %u\n", index);
2588 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_REMOVE);
2589 ok(!success, "CoWaitForMultipleHandles didn't pump any messages\n");
2590
2591 index = 0xdeadbeef;
2592 PostMessageA(hWnd, WM_USER, 0, 0);
2593 hr = CoWaitForMultipleHandles(0, 50, 2, handles, &index);
2594 ok(hr == RPC_S_CALLPENDING, "expected RPC_S_CALLPENDING, got 0x%08x\n", hr);
2595 ok(index==0 || index==0xdeadbeef/* Win 8 */, "expected index 0, got %u\n", index);
2596 success = PeekMessageA(&msg, hWnd, WM_USER, WM_USER, PM_REMOVE);
2597 ok(success, "CoWaitForMultipleHandles unexpectedly pumped messages\n");
2598
2599 /* Even if CoWaitForMultipleHandles does not pump a message it peeks
2600 * at ALL of them */
2601 index = 0xdeadbeef;
2602 PostMessageA(NULL, uMSG, 0, 0);
2603
2604 hr = CoWaitForMultipleHandles(COWAIT_ALERTABLE, 50, 2, handles, &index);
2605 ok(hr == RPC_S_CALLPENDING, "expected RPC_S_CALLPENDING, got 0x%08x\n", hr);
2606 ok(index == 0 || broken(index == 0xdeadbeef) /* Win 8 */, "expected index 0, got %u\n", index);
2607
2608 /* Make sure message was peeked at */
2609 ret = MsgWaitForMultipleObjectsEx(0, NULL, 2, QS_ALLPOSTMESSAGE, MWMO_ALERTABLE);
2610 ok(ret == WAIT_TIMEOUT, "MsgWaitForMultipleObjects returned %x\n", ret);
2611
2612 /* But not pumped */
2613 success = PeekMessageA(&msg, NULL, uMSG, uMSG, PM_REMOVE);
2614 ok(success, "CoWaitForMultipleHandles unexpectedly pumped messages\n");
2615
2616 DestroyWindow(hWnd);
2617 CoUninitialize();
2618
2619 hr = pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
2620 ok(hr == S_OK, "CoInitializeEx failed with error 0x%08x\n", hr);
2621
2622 hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
2623 ok(hr == S_OK, "CreateStreamOnHGlobal failed: %08x\n", hr);
2624
2625 hr = CoMarshalInterface(stream, &IID_IUnknown, &Test_Unknown, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
2626 ok(hr == S_OK, "CoMarshalInterface should have returned S_OK instead of 0x%08x\n", hr);
2627
2628 event = CreateEventW(NULL, TRUE, FALSE, NULL);
2629
2630 PostQuitMessage(66);
2631 PostThreadMessageW(GetCurrentThreadId(), WM_QUIT, 0, 0);
2632
2633 hr = CoRegisterMessageFilter(&MessageFilter, NULL);
2634 ok(hr == S_OK, "CoRegisterMessageFilter failed: %08x\n", hr);
2635
2636 thread = CreateThread(NULL, 0, cowait_unmarshal_thread, stream, 0, &tid);
2637 ok(thread != NULL, "CreateThread failed, error %u\n", GetLastError());
2638 hr = CoWaitForMultipleHandles(0, 50, 1, &event, &index);
2639 ok(hr == RPC_S_CALLPENDING, "expected RPC_S_CALLPENDING, got 0x%08x\n", hr);
2640 index = WaitForSingleObject(thread, 200);
2641 ok(index == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
2642 CloseHandle(thread);
2643
2644 hr = CoRegisterMessageFilter(NULL, NULL);
2645 ok(hr == S_OK, "CoRegisterMessageFilter failed: %08x\n", hr);
2646
2647 IStream_Release(stream);
2648
2649 CloseHandle(event);
2650 CoUninitialize();
2651 return 0;
2652 }
2653
2654 static void test_CoWaitForMultipleHandles(void)
2655 {
2656 HANDLE handles[2], thread;
2657 DWORD index, tid;
2658 WNDCLASSEXA wc;
2659 BOOL success;
2660 HRESULT hr;
2661 HWND hWnd;
2662 MSG msg;
2663
2664 hr = pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
2665 ok(hr == S_OK, "CoInitializeEx failed with error 0x%08x\n", hr);
2666
2667 memset(&wc, 0, sizeof(wc));
2668 wc.cbSize = sizeof(wc);
2669 wc.style = CS_VREDRAW | CS_HREDRAW;
2670 wc.hInstance = GetModuleHandleA(0);
2671 wc.hCursor = LoadCursorA(NULL, (LPCSTR)IDC_ARROW);
2672 wc.hbrBackground = NULL;
2673 wc.lpszClassName = cls_name;
2674 wc.lpfnWndProc = cowait_window_proc;
2675 success = RegisterClassExA(&wc) != 0;
2676 ok(success, "RegisterClassExA failed %u\n", GetLastError());
2677
2678 hWnd = CreateWindowExA(0, cls_name, "Test", WS_TILEDWINDOW, 0, 0, 640, 480, 0, 0, 0, 0);
2679 ok(hWnd != 0, "CreateWindowExA failed %u\n", GetLastError());
2680 handles[0] = CreateSemaphoreA(NULL, 1, 1, NULL);
2681 ok(handles[0] != 0, "CreateSemaphoreA failed %u\n", GetLastError());
2682 handles[1] = CreateSemaphoreA(NULL, 1, 1, NULL);
2683 ok(handles[1] != 0, "CreateSemaphoreA failed %u\n", GetLastError());
2684
2685 /* test without flags */
2686
2687 PostMessageA(hWnd, WM_DDE_FIRST, 0, 0);
2688 hr = CoWaitForMultipleHandles(0, 50, 0, handles, NULL);
2689 ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got 0x%08x\n", hr);
2690 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_REMOVE);
2691 ok(success, "CoWaitForMultipleHandles unexpectedly pumped messages\n");
2692
2693 index = 0xdeadbeef;
2694 PostMessageA(hWnd, WM_DDE_FIRST, 0, 0);
2695 hr = CoWaitForMultipleHandles(0, 50, 0, NULL, &index);
2696 ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got 0x%08x\n", hr);
2697 ok(index == 0, "expected index 0, got %u\n", index);
2698 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_REMOVE);
2699 ok(success, "CoWaitForMultipleHandles unexpectedly pumped messages\n");
2700
2701 index = 0xdeadbeef;
2702 PostMessageA(hWnd, WM_DDE_FIRST, 0, 0);
2703 hr = CoWaitForMultipleHandles(0, 50, 0, handles, &index);
2704 ok(hr == RPC_E_NO_SYNC, "expected RPC_E_NO_SYNC, got 0x%08x\n", hr);
2705 ok(index == 0, "expected index 0, got %u\n", index);
2706 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_REMOVE);
2707 ok(success, "CoWaitForMultipleHandles unexpectedly pumped messages\n");
2708
2709 index = 0xdeadbeef;
2710 PostMessageA(hWnd, WM_DDE_FIRST, 0, 0);
2711 hr = CoWaitForMultipleHandles(0, 50, 1, handles, &index);
2712 ok(hr == S_OK, "expected S_OK, got 0x%08x\n", hr);
2713 ok(index == 0, "expected index 0, got %u\n", index);
2714 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_REMOVE);
2715 ok(success, "CoWaitForMultipleHandles unexpectedly pumped messages\n");
2716
2717 index = 0xdeadbeef;
2718 PostMessageA(hWnd, WM_DDE_FIRST, 0, 0);
2719 hr = CoWaitForMultipleHandles(0, 50, 2, handles, &index);
2720 ok(hr == S_OK, "expected S_OK, got 0x%08x\n", hr);
2721 ok(index == 1, "expected index 1, got %u\n", index);
2722 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_REMOVE);
2723 ok(success, "CoWaitForMultipleHandles unexpectedly pumped messages\n");
2724
2725 index = 0xdeadbeef;
2726 PostMessageA(hWnd, WM_DDE_FIRST, 0, 0);
2727 hr = CoWaitForMultipleHandles(0, 50, 2, handles, &index);
2728 ok(hr == RPC_S_CALLPENDING, "expected RPC_S_CALLPENDING, got 0x%08x\n", hr);
2729 ok(index == 0 || broken(index == 0xdeadbeef) /* Win 8 */, "expected index 0, got %u\n", index);
2730 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_REMOVE);
2731 ok(!success, "CoWaitForMultipleHandles didn't pump any messages\n");
2732
2733 /* test PostMessageA/SendMessageA from a different thread */
2734
2735 index = 0xdeadbeef;
2736 thread = CreateThread(NULL, 0, post_message_thread, hWnd, 0, &tid);
2737 ok(thread != NULL, "CreateThread failed, error %u\n", GetLastError());
2738 hr = CoWaitForMultipleHandles(0, 100, 2, handles, &index);
2739 ok(hr == RPC_S_CALLPENDING, "expected RPC_S_CALLPENDING, got 0x%08x\n", hr);
2740 ok(index == 0 || broken(index == 0xdeadbeef) /* Win 8 */, "expected index 0, got %u\n", index);
2741 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_REMOVE);
2742 ok(!success, "CoWaitForMultipleHandles didn't pump any messages\n");
2743 index = WaitForSingleObject(thread, 200);
2744 ok(index == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
2745 CloseHandle(thread);
2746
2747 index = 0xdeadbeef;
2748 thread = CreateThread(NULL, 0, send_message_thread, hWnd, 0, &tid);
2749 ok(thread != NULL, "CreateThread failed, error %u\n", GetLastError());
2750 hr = CoWaitForMultipleHandles(0, 100, 2, handles, &index);
2751 ok(hr == RPC_S_CALLPENDING, "expected RPC_S_CALLPENDING, got 0x%08x\n", hr);
2752 ok(index == 0 || broken(index == 0xdeadbeef) /* Win 8 */, "expected index 0, got %u\n", index);
2753 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_REMOVE);
2754 ok(!success, "CoWaitForMultipleHandles didn't pump any messages\n");
2755 index = WaitForSingleObject(thread, 200);
2756 ok(index == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
2757 CloseHandle(thread);
2758
2759 ReleaseSemaphore(handles[0], 1, NULL);
2760 ReleaseSemaphore(handles[1], 1, NULL);
2761
2762 /* test with COWAIT_WAITALL */
2763
2764 index = 0xdeadbeef;
2765 PostMessageA(hWnd, WM_DDE_FIRST, 0, 0);
2766 hr = CoWaitForMultipleHandles(COWAIT_WAITALL, 50, 2, handles, &index);
2767 ok(hr == S_OK, "expected S_OK, got 0x%08x\n", hr);
2768 ok(index == 0, "expected index 0, got %u\n", index);
2769 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_REMOVE);
2770 ok(success, "CoWaitForMultipleHandles unexpectedly pumped messages\n");
2771
2772 index = 0xdeadbeef;
2773 PostMessageA(hWnd, WM_DDE_FIRST, 0, 0);
2774 hr = CoWaitForMultipleHandles(0, 50, 2, handles, &index);
2775 ok(hr == RPC_S_CALLPENDING, "expected RPC_S_CALLPENDING, got 0x%08x\n", hr);
2776 ok(index == 0 || broken(index == 0xdeadbeef) /* Win 8 */, "expected index 0, got %u\n", index);
2777 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_REMOVE);
2778 ok(!success, "CoWaitForMultipleHandles didn't pump any messages\n");
2779
2780 ReleaseSemaphore(handles[0], 1, NULL);
2781 ReleaseSemaphore(handles[1], 1, NULL);
2782
2783 /* test with COWAIT_ALERTABLE */
2784
2785 index = 0xdeadbeef;
2786 PostMessageA(hWnd, WM_DDE_FIRST, 0, 0);
2787 hr = CoWaitForMultipleHandles(COWAIT_ALERTABLE, 50, 1, handles, &index);
2788 ok(hr == S_OK, "expected S_OK, got 0x%08x\n", hr);
2789 ok(index == 0, "expected index 0, got %u\n", index);
2790 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_REMOVE);
2791 ok(success, "CoWaitForMultipleHandles unexpectedly pumped messages\n");
2792
2793 index = 0xdeadbeef;
2794 PostMessageA(hWnd, WM_DDE_FIRST, 0, 0);
2795 hr = CoWaitForMultipleHandles(COWAIT_ALERTABLE, 50, 2, handles, &index);
2796 ok(hr == S_OK, "expected S_OK, got 0x%08x\n", hr);
2797 ok(index == 1, "expected index 1, got %u\n", index);
2798 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_REMOVE);
2799 ok(success, "CoWaitForMultipleHandles unexpectedly pumped messages\n");
2800
2801 index = 0xdeadbeef;
2802 PostMessageA(hWnd, WM_DDE_FIRST, 0, 0);
2803 hr = CoWaitForMultipleHandles(COWAIT_ALERTABLE, 50, 2, handles, &index);
2804 ok(hr == RPC_S_CALLPENDING, "expected RPC_S_CALLPENDING, got 0x%08x\n", hr);
2805 ok(index == 0 || broken(index == 0xdeadbeef) /* Win 8 */, "expected index 0, got %u\n", index);
2806 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_REMOVE);
2807 ok(!success, "CoWaitForMultipleHandles didn't pump any messages\n");
2808
2809 index = 0xdeadbeef;
2810 PostMessageA(hWnd, WM_DDE_FIRST, 0, 0);
2811 success = QueueUserAPC(apc_test_proc, GetCurrentThread(), 0);
2812 ok(success, "QueueUserAPC failed %u\n", GetLastError());
2813 hr = CoWaitForMultipleHandles(COWAIT_ALERTABLE, 50, 2, handles, &index);
2814 ok(hr == S_OK, "expected S_OK, got 0x%08x\n", hr);
2815 ok(index == WAIT_IO_COMPLETION, "expected index WAIT_IO_COMPLETION, got %u\n", index);
2816 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_REMOVE);
2817 ok(success, "CoWaitForMultipleHandles unexpectedly pumped messages\n");
2818
2819 /* test with COWAIT_INPUTAVAILABLE (semaphores are still locked) */
2820
2821 index = 0xdeadbeef;
2822 PostMessageA(hWnd, WM_DDE_FIRST, 0, 0);
2823 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_NOREMOVE);
2824 ok(success, "PeekMessageA returned FALSE\n");
2825 hr = CoWaitForMultipleHandles(0, 50, 2, handles, &index);
2826 ok(hr == RPC_S_CALLPENDING, "expected RPC_S_CALLPENDING, got 0x%08x\n", hr);
2827 ok(index == 0 || broken(index == 0xdeadbeef) /* Win 8 */, "expected index 0, got %u\n", index);
2828 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_REMOVE);
2829 ok(!success, "CoWaitForMultipleHandles didn't pump any messages\n");
2830
2831 index = 0xdeadbeef;
2832 PostMessageA(hWnd, WM_DDE_FIRST, 0, 0);
2833 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_NOREMOVE);
2834 ok(success, "PeekMessageA returned FALSE\n");
2835 thread = CreateThread(NULL, 0, release_semaphore_thread, handles[1], 0, &tid);
2836 ok(thread != NULL, "CreateThread failed, error %u\n", GetLastError());
2837 hr = CoWaitForMultipleHandles(COWAIT_INPUTAVAILABLE, 50, 2, handles, &index);
2838 ok(hr == RPC_S_CALLPENDING || broken(hr == E_INVALIDARG) || broken(hr == S_OK) /* Win 8 */,
2839 "expected RPC_S_CALLPENDING, got 0x%08x\n", hr);
2840 if (hr != S_OK) ReleaseSemaphore(handles[1], 1, NULL);
2841 ok(index == 0 || broken(index == 1) /* Win 8 */, "expected index 0, got %u\n", index);
2842 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_REMOVE);
2843 ok(!success || broken(success && hr == E_INVALIDARG),
2844 "CoWaitForMultipleHandles didn't pump any messages\n");
2845 index = WaitForSingleObject(thread, 200);
2846 ok(index == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
2847 CloseHandle(thread);
2848
2849 cowait_msgs_reset();
2850 PostMessageA(hWnd, 0, 0, 0);
2851 PostMessageA(hWnd, WM_DDE_FIRST, 0, 0);
2852 PostMessageA(hWnd, WM_USER+1, 0, 0);
2853 PostMessageA(hWnd, WM_DDE_FIRST+1, 0, 0);
2854 thread = CreateThread(NULL, 0, send_and_post_user_message_thread, hWnd, 0, &tid);
2855 ok(thread != NULL, "CreateThread failed, error %u\n", GetLastError());
2856
2857 hr = CoWaitForMultipleHandles(0, 100, 2, handles, &index);
2858 ok(hr == RPC_S_CALLPENDING, "expected RPC_S_CALLPENDING, got 0x%08x\n", hr);
2859
2860 cowait_msgs_expect_notified(WM_DDE_FIRST);
2861 cowait_msgs_expect_notified(WM_DDE_FIRST+1);
2862 cowait_msgs_expect_notified(WM_USER);
2863 cowait_msgs_expect_empty();
2864 cowait_msgs_expect_queued(hWnd, WM_USER);
2865 cowait_msgs_expect_queued(hWnd, WM_USER+1);
2866 flush_messages();
2867
2868 index = WaitForSingleObject(thread, 200);
2869 ok(index == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
2870 CloseHandle(thread);
2871
2872 /* test behaviour of WM_QUIT (semaphores are still locked) */
2873
2874 PostMessageA(hWnd, WM_QUIT, 40, 0);
2875 memset(&msg, 0, sizeof(msg));
2876 success = PeekMessageA(&msg, hWnd, WM_QUIT, WM_QUIT, PM_REMOVE);
2877 ok(success, "PeekMessageA failed, error %u\n", GetLastError());
2878 ok(msg.message == WM_QUIT, "expected msg.message = WM_QUIT, got %u\n", msg.message);
2879 ok(msg.wParam == 40, "expected msg.wParam = 40, got %lu\n", msg.wParam);
2880 success = PeekMessageA(&msg, hWnd, WM_QUIT, WM_QUIT, PM_REMOVE);
2881 ok(!success, "PeekMessageA succeeded\n");
2882
2883 cowait_msgs_reset();
2884 PostMessageA(hWnd, WM_QUIT, 40, 0);
2885 PostMessageA(hWnd, 0, 0, 0);
2886 PostMessageA(hWnd, WM_DDE_FIRST, 0, 0);
2887 PostMessageA(hWnd, WM_USER+1, 0, 0);
2888 PostMessageA(hWnd, WM_DDE_FIRST+1, 0, 0);
2889 thread = CreateThread(NULL, 0, send_and_post_user_message_thread, hWnd, 0, &tid);
2890 ok(thread != NULL, "CreateThread failed, error %u\n", GetLastError());
2891
2892 hr = CoWaitForMultipleHandles(0, 100, 2, handles, &index);
2893 ok(hr == RPC_S_CALLPENDING, "expected RPC_S_CALLPENDING, got 0x%08x\n", hr);
2894
2895 cowait_msgs_expect_notified(WM_DDE_FIRST);
2896 cowait_msgs_expect_notified(WM_DDE_FIRST+1);
2897 cowait_msgs_expect_notified(WM_USER);
2898 cowait_msgs_expect_empty();
2899 cowait_msgs_expect_queued(hWnd, WM_USER);
2900 flush_messages();
2901
2902 index = WaitForSingleObject(thread, 200);
2903 ok(index == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
2904 CloseHandle(thread);
2905
2906 index = 0xdeadbeef;
2907 PostMessageA(hWnd, WM_DDE_FIRST, 0, 0);
2908 PostMessageA(hWnd, WM_QUIT, 41, 0);
2909 thread = CreateThread(NULL, 0, post_message_thread, hWnd, 0, &tid);
2910 ok(thread != NULL, "CreateThread failed, error %u\n", GetLastError());
2911 hr = CoWaitForMultipleHandles(0, 100, 2, handles, &index);
2912 ok(hr == RPC_S_CALLPENDING, "expected RPC_S_CALLPENDING, got 0x%08x\n", hr);
2913 ok(index == 0 || broken(index == 0xdeadbeef) /* Win 8 */, "expected index 0, got %u\n", index);
2914 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_REMOVE);
2915 todo_wine
2916 ok(success || broken(!success) /* Win 2000/XP/8 */, "PeekMessageA failed, error %u\n", GetLastError());
2917 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_REMOVE);
2918 ok(!success, "PeekMessageA succeeded\n");
2919 memset(&msg, 0, sizeof(msg));
2920 success = PeekMessageA(&msg, hWnd, WM_QUIT, WM_QUIT, PM_REMOVE);
2921 todo_wine
2922 ok(!success || broken(success) /* Win 2000/XP/8 */, "PeekMessageA succeeded\n");
2923 if (success)
2924 {
2925 ok(msg.message == WM_QUIT, "expected msg.message = WM_QUIT, got %u\n", msg.message);
2926 ok(msg.wParam == 41, "expected msg.wParam = 41, got %lu\n", msg.wParam);
2927 }
2928 index = WaitForSingleObject(thread, 200);
2929 ok(index == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
2930 CloseHandle(thread);
2931
2932 index = 0xdeadbeef;
2933 PostMessageA(hWnd, WM_DDE_FIRST, 0, 0);
2934 PostMessageA(hWnd, WM_QUIT, 42, 0);
2935 thread = CreateThread(NULL, 0, send_message_thread, hWnd, 0, &tid);
2936 ok(thread != NULL, "CreateThread failed, error %u\n", GetLastError());
2937 hr = CoWaitForMultipleHandles(0, 100, 2, handles, &index);
2938 ok(hr == RPC_S_CALLPENDING, "expected RPC_S_CALLPENDING, got 0x%08x\n", hr);
2939 ok(index == 0 || broken(index == 0xdeadbeef) /* Win 8 */, "expected index 0, got %u\n", index);
2940 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_REMOVE);
2941 ok(!success, "CoWaitForMultipleHandles didn't pump all WM_DDE_FIRST messages\n");
2942 memset(&msg, 0, sizeof(msg));
2943 success = PeekMessageA(&msg, hWnd, WM_QUIT, WM_QUIT, PM_REMOVE);
2944 ok(success, "PeekMessageA failed, error %u\n", GetLastError());
2945 ok(msg.message == WM_QUIT, "expected msg.message = WM_QUIT, got %u\n", msg.message);
2946 ok(msg.wParam == 42, "expected msg.wParam = 42, got %lu\n", msg.wParam);
2947 index = WaitForSingleObject(thread, 200);
2948 ok(index == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
2949 CloseHandle(thread);
2950
2951 PostQuitMessage(43);
2952 memset(&msg, 0, sizeof(msg));
2953 success = PeekMessageA(&msg, hWnd, WM_QUIT, WM_QUIT, PM_REMOVE);
2954 ok(success || broken(!success) /* Win 8 */, "PeekMessageA failed, error %u\n", GetLastError());
2955 if (!success)
2956 win_skip("PostQuitMessage didn't queue a WM_QUIT message, skipping tests\n");
2957 else
2958 {
2959 ok(msg.message == WM_QUIT, "expected msg.message = WM_QUIT, got %u\n", msg.message);
2960 ok(msg.wParam == 43, "expected msg.wParam = 43, got %lu\n", msg.wParam);
2961 success = PeekMessageA(&msg, hWnd, WM_QUIT, WM_QUIT, PM_REMOVE);
2962 ok(!success, "PeekMessageA succeeded\n");
2963
2964 index = 0xdeadbeef;
2965 PostMessageA(hWnd, WM_DDE_FIRST, 0, 0);
2966 PostQuitMessage(44);
2967 thread = CreateThread(NULL, 0, post_message_thread, hWnd, 0, &tid);
2968 ok(thread != NULL, "CreateThread failed, error %u\n", GetLastError());
2969 hr = CoWaitForMultipleHandles(0, 100, 2, handles, &index);
2970 ok(hr == RPC_S_CALLPENDING, "expected RPC_S_CALLPENDING, got 0x%08x\n", hr);
2971 ok(index == 0 || broken(index == 0xdeadbeef) /* Win 8 */, "expected index 0, got %u\n", index);
2972 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_REMOVE);
2973 ok(success, "PeekMessageA failed, error %u\n", GetLastError());
2974 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_REMOVE);
2975 ok(!success, "PeekMessageA succeeded\n");
2976 success = PeekMessageA(&msg, hWnd, WM_QUIT, WM_QUIT, PM_REMOVE);
2977 ok(!success, "CoWaitForMultipleHandles didn't remove WM_QUIT messages\n");
2978 index = WaitForSingleObject(thread, 200);
2979 ok(index == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
2980 CloseHandle(thread);
2981
2982 index = 0xdeadbeef;
2983 PostMessageA(hWnd, WM_DDE_FIRST, 0, 0);
2984 PostQuitMessage(45);
2985 thread = CreateThread(NULL, 0, send_message_thread, hWnd, 0, &tid);
2986 ok(thread != NULL, "CreateThread failed, error %u\n", GetLastError());
2987 hr = CoWaitForMultipleHandles(0, 100, 2, handles, &index);
2988 ok(hr == RPC_S_CALLPENDING, "expected RPC_S_CALLPENDING, got 0x%08x\n", hr);
2989 ok(index == 0 || broken(index == 0xdeadbeef) /* Win 8 */, "expected index 0, got %u\n", index);
2990 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_REMOVE);
2991 ok(success, "PeekMessageA failed, error %u\n", GetLastError());
2992 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_REMOVE);
2993 ok(!success, "PeekMessageA succeeded\n");
2994 success = PeekMessageA(&msg, hWnd, WM_QUIT, WM_QUIT, PM_REMOVE);
2995 ok(!success, "CoWaitForMultipleHandles didn't remove WM_QUIT messages\n");
2996 index = WaitForSingleObject(thread, 200);
2997 ok(index == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
2998 CloseHandle(thread);
2999 }
3000
3001 /* test message pumping when CoWaitForMultipleHandles is called from non main apartment thread */
3002 thread = CreateThread(NULL, 0, test_CoWaitForMultipleHandles_thread, handles, 0, &tid);
3003 index = WaitForSingleObject(thread, 500);
3004 ok(index == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
3005 CloseHandle(thread);
3006
3007 CoUninitialize();
3008
3009 /* If COM was not initialized, messages are neither pumped nor peeked at */
3010 PostMessageA(hWnd, WM_DDE_FIRST, 0, 0);
3011 hr = CoWaitForMultipleHandles(0, 100, 2, handles, &index);
3012 ok(hr == RPC_S_CALLPENDING, "got %#x\n", hr);
3013 success = MsgWaitForMultipleObjectsEx(0, NULL, 2, QS_ALLPOSTMESSAGE, MWMO_ALERTABLE);
3014 ok(success == 0, "MsgWaitForMultipleObjects returned %x\n", success);
3015 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_REMOVE);
3016 ok(success, "PeekMessage failed: %u\n", GetLastError());
3017
3018 /* same in an MTA */
3019 CoInitializeEx(NULL, COINIT_MULTITHREADED);
3020
3021 PostMessageA(hWnd, WM_DDE_FIRST, 0, 0);
3022 hr = CoWaitForMultipleHandles(0, 100, 2, handles, &index);
3023 ok(hr == RPC_S_CALLPENDING, "got %#x\n", hr);
3024 success = MsgWaitForMultipleObjectsEx(0, NULL, 2, QS_ALLPOSTMESSAGE, MWMO_ALERTABLE);
3025 ok(success == 0, "MsgWaitForMultipleObjects returned %x\n", success);
3026 success = PeekMessageA(&msg, hWnd, WM_DDE_FIRST, WM_DDE_FIRST, PM_REMOVE);
3027 ok(success, "PeekMessage failed: %u\n", GetLastError());
3028
3029 CoUninitialize();
3030
3031 CloseHandle(handles[0]);
3032 CloseHandle(handles[1]);
3033 DestroyWindow(hWnd);
3034
3035 success = UnregisterClassA(cls_name, GetModuleHandleA(0));
3036 ok(success, "UnregisterClass failed %u\n", GetLastError());
3037 }
3038
3039 static void test_CoGetMalloc(void)
3040 {
3041 IMalloc *imalloc;
3042 HRESULT hr;
3043
3044 if (0) /* crashes on native */
3045 hr = CoGetMalloc(0, NULL);
3046
3047 imalloc = (void*)0xdeadbeef;
3048 hr = CoGetMalloc(0, &imalloc);
3049 ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
3050 ok(imalloc == NULL, "got %p\n", imalloc);
3051
3052 imalloc = (void*)0xdeadbeef;
3053 hr = CoGetMalloc(MEMCTX_SHARED, &imalloc);
3054 ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
3055 ok(imalloc == NULL, "got %p\n", imalloc);
3056
3057 imalloc = (void*)0xdeadbeef;
3058 hr = CoGetMalloc(MEMCTX_MACSYSTEM, &imalloc);
3059 ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
3060 ok(imalloc == NULL, "got %p\n", imalloc);
3061
3062 imalloc = (void*)0xdeadbeef;
3063 hr = CoGetMalloc(MEMCTX_UNKNOWN, &imalloc);
3064 ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
3065 ok(imalloc == NULL, "got %p\n", imalloc);
3066
3067 imalloc = (void*)0xdeadbeef;
3068 hr = CoGetMalloc(MEMCTX_SAME, &imalloc);
3069 ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
3070 ok(imalloc == NULL, "got %p\n", imalloc);
3071
3072 imalloc = NULL;
3073 hr = CoGetMalloc(MEMCTX_TASK, &imalloc);
3074 ok(hr == S_OK, "got 0x%08x\n", hr);
3075 ok(imalloc != NULL, "got %p\n", imalloc);
3076 IMalloc_Release(imalloc);
3077 }
3078
3079 static void test_CoGetApartmentType(void)
3080 {
3081 APTTYPEQUALIFIER qualifier;
3082 APTTYPE type;
3083 HRESULT hr;
3084
3085 if (!pCoGetApartmentType)
3086 {
3087 win_skip("CoGetApartmentType not present\n");
3088 return;
3089 }
3090
3091 hr = pCoGetApartmentType(NULL, NULL);
3092 ok(hr == E_INVALIDARG, "CoGetApartmentType succeeded, error: 0x%08x\n", hr);
3093
3094 type = 0xdeadbeef;
3095 hr = pCoGetApartmentType(&type, NULL);
3096 ok(hr == E_INVALIDARG, "CoGetApartmentType succeeded, error: 0x%08x\n", hr);
3097 ok(type == 0xdeadbeef, "Expected 0xdeadbeef, got %u\n", type);
3098
3099 qualifier = 0xdeadbeef;
3100 hr = pCoGetApartmentType(NULL, &qualifier);
3101 ok(hr == E_INVALIDARG, "CoGetApartmentType succeeded, error: 0x%08x\n", hr);
3102 ok(qualifier == 0xdeadbeef, "Expected 0xdeadbeef, got %u\n", qualifier);
3103
3104 type = 0xdeadbeef;
3105 qualifier = 0xdeadbeef;
3106 hr = pCoGetApartmentType(&type, &qualifier);
3107 ok(hr == CO_E_NOTINITIALIZED, "CoGetApartmentType succeeded, error: 0x%08x\n", hr);
3108 ok(type == APTTYPE_CURRENT, "Expected APTTYPE_CURRENT, got %u\n", type);
3109 ok(qualifier == APTTYPEQUALIFIER_NONE, "Expected APTTYPEQUALIFIER_NONE, got %u\n", qualifier);
3110
3111 type = 0xdeadbeef;
3112 qualifier = 0xdeadbeef;
3113 hr = pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
3114 ok(hr == S_OK, "CoInitializeEx failed, error: 0x%08x\n", hr);
3115 hr = pCoGetApartmentType(&type, &qualifier);
3116 ok(hr == S_OK, "CoGetApartmentType failed, error: 0x%08x\n", hr);
3117 ok(type == APTTYPE_MAINSTA, "Expected APTTYPE_MAINSTA, got %u\n", type);
3118 ok(qualifier == APTTYPEQUALIFIER_NONE, "Expected APTTYPEQUALIFIER_NONE, got %u\n", qualifier);
3119 CoUninitialize();
3120
3121 type = 0xdeadbeef;
3122 qualifier = 0xdeadbeef;
3123 hr = pCoInitializeEx(NULL, COINIT_MULTITHREADED);
3124 ok(hr == S_OK, "CoInitializeEx failed, error: 0x%08x\n", hr);
3125 hr = pCoGetApartmentType(&type, &qualifier);
3126 ok(hr == S_OK, "CoGetApartmentType failed, error: 0x%08x\n", hr);
3127 ok(type == APTTYPE_MTA, "Expected APTTYPE_MTA, got %u\n", type);
3128 ok(qualifier == APTTYPEQUALIFIER_NONE, "Expected APTTYPEQUALIFIER_NONE, got %u\n", qualifier);
3129 CoUninitialize();
3130 }
3131
3132 static HRESULT WINAPI testspy_QI(IMallocSpy *iface, REFIID riid, void **obj)
3133 {
3134 if (IsEqualIID(riid, &IID_IMallocSpy) || IsEqualIID(riid, &IID_IUnknown))
3135 {
3136 *obj = iface;
3137 IMallocSpy_AddRef(iface);
3138 return S_OK;
3139 }
3140
3141 return E_NOINTERFACE;
3142 }
3143
3144 static ULONG WINAPI testspy_AddRef(IMallocSpy *iface)
3145 {
3146 return 2;
3147 }
3148
3149 static ULONG WINAPI testspy_Release(IMallocSpy *iface)
3150 {
3151 return 1;
3152 }
3153
3154 static SIZE_T WINAPI testspy_PreAlloc(IMallocSpy *iface, SIZE_T cb)
3155 {
3156 ok(0, "unexpected call\n");
3157 return 0;
3158 }
3159
3160 static void* WINAPI testspy_PostAlloc(IMallocSpy *iface, void *ptr)
3161 {
3162 ok(0, "unexpected call\n");
3163 return NULL;
3164 }
3165
3166 static void* WINAPI testspy_PreFree(IMallocSpy *iface, void *ptr, BOOL spyed)
3167 {
3168 ok(0, "unexpected call\n");
3169 return NULL;
3170 }
3171
3172 static void WINAPI testspy_PostFree(IMallocSpy *iface, BOOL spyed)
3173 {
3174 ok(0, "unexpected call\n");
3175 }
3176
3177 static SIZE_T WINAPI testspy_PreRealloc(IMallocSpy *iface, void *ptr, SIZE_T cb, void **newptr, BOOL spyed)
3178 {
3179 ok(0, "unexpected call\n");
3180 return 0;
3181 }
3182
3183 static void* WINAPI testspy_PostRealloc(IMallocSpy *iface, void *ptr, BOOL spyed)
3184 {
3185 ok(0, "unexpected call\n");
3186 return NULL;
3187 }
3188
3189 static void* WINAPI testspy_PreGetSize(IMallocSpy *iface, void *ptr, BOOL spyed)
3190 {
3191 ok(0, "unexpected call\n");
3192 return NULL;
3193 }
3194
3195 static SIZE_T WINAPI testspy_PostGetSize(IMallocSpy *iface, SIZE_T actual, BOOL spyed)
3196 {
3197 ok(0, "unexpected call\n");
3198 return 0;
3199 }
3200
3201 static void* WINAPI testspy_PreDidAlloc(IMallocSpy *iface, void *ptr, BOOL spyed)
3202 {
3203 ok(0, "unexpected call\n");
3204 return NULL;
3205 }
3206
3207 static int WINAPI testspy_PostDidAlloc(IMallocSpy *iface, void *ptr, BOOL spyed, int actual)
3208 {
3209 ok(0, "unexpected call\n");
3210 return 0;
3211 }
3212
3213 static void WINAPI testspy_PreHeapMinimize(IMallocSpy *iface)
3214 {
3215 ok(0, "unexpected call\n");
3216 }
3217
3218 static void WINAPI testspy_PostHeapMinimize(IMallocSpy *iface)
3219 {
3220 ok(0, "unexpected call\n");
3221 }
3222
3223 static const IMallocSpyVtbl testspyvtbl =
3224 {
3225 testspy_QI,
3226 testspy_AddRef,
3227 testspy_Release,
3228 testspy_PreAlloc,
3229 testspy_PostAlloc,
3230 testspy_PreFree,
3231 testspy_PostFree,
3232 testspy_PreRealloc,
3233 testspy_PostRealloc,
3234 testspy_PreGetSize,
3235 testspy_PostGetSize,
3236 testspy_PreDidAlloc,
3237 testspy_PostDidAlloc,
3238 testspy_PreHeapMinimize,
3239 testspy_PostHeapMinimize
3240 };
3241
3242 static IMallocSpy testspy = { &testspyvtbl };
3243
3244 static void test_IMallocSpy(void)
3245 {
3246 IMalloc *imalloc;
3247 HRESULT hr;
3248
3249 hr = CoRegisterMallocSpy(NULL);
3250 ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
3251
3252 hr = CoRevokeMallocSpy();
3253 ok(hr == CO_E_OBJNOTREG, "got 0x%08x\n", hr);
3254
3255 hr = CoRegisterMallocSpy(&testspy);
3256 ok(hr == S_OK, "got 0x%08x\n", hr);
3257
3258 hr = CoRegisterMallocSpy(NULL);
3259 ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
3260
3261 hr = CoRegisterMallocSpy(&testspy);
3262 ok(hr == CO_E_OBJISREG, "got 0x%08x\n", hr);
3263
3264 imalloc = NULL;
3265 hr = CoGetMalloc(MEMCTX_TASK, &imalloc);
3266 ok(hr == S_OK, "got 0x%08x\n", hr);
3267 ok(imalloc != NULL, "got %p\n", imalloc);
3268
3269 IMalloc_Free(imalloc, NULL);
3270
3271 IMalloc_Release(imalloc);
3272
3273 hr = CoRevokeMallocSpy();
3274 ok(hr == S_OK, "got 0x%08x\n", hr);
3275
3276 hr = CoRevokeMallocSpy();
3277 ok(hr == CO_E_OBJNOTREG, "got 0x%08x\n", hr);
3278 }
3279
3280 static void test_CoGetCurrentLogicalThreadId(void)
3281 {
3282 HRESULT hr;
3283 GUID id;
3284
3285 hr = CoGetCurrentLogicalThreadId(NULL);
3286 ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
3287
3288 id = GUID_NULL;
3289 hr = CoGetCurrentLogicalThreadId(&id);
3290 ok(hr == S_OK, "got 0x%08x\n", hr);
3291 ok(!IsEqualGUID(&id, &GUID_NULL), "got null id\n");
3292 }
3293
3294 static HRESULT WINAPI testinitialize_QI(IInitializeSpy *iface, REFIID riid, void **obj)
3295 {
3296 if (IsEqualIID(riid, &IID_IInitializeSpy) || IsEqualIID(riid, &IID_IUnknown))
3297 {
3298 *obj = iface;
3299 IInitializeSpy_AddRef(iface);
3300 return S_OK;
3301 }
3302
3303 *obj = NULL;
3304 return E_NOINTERFACE;
3305 }
3306
3307 static ULONG WINAPI testinitialize_AddRef(IInitializeSpy *iface)
3308 {
3309 return 2;
3310 }
3311
3312 static ULONG WINAPI testinitialize_Release(IInitializeSpy *iface)
3313 {
3314 return 1;
3315 }
3316
3317 static HRESULT WINAPI testinitialize_PreInitialize(IInitializeSpy *iface, DWORD coinit, DWORD aptrefs)
3318 {
3319 ok(0, "unexpected call\n");
3320 return E_NOTIMPL;
3321 }
3322
3323 static HRESULT WINAPI testinitialize_PostInitialize(IInitializeSpy *iface, HRESULT hr, DWORD coinit, DWORD aptrefs)
3324 {
3325 ok(0, "unexpected call\n");
3326 return E_NOTIMPL;
3327 }
3328
3329 static HRESULT WINAPI testinitialize_PreUninitialize(IInitializeSpy *iface, DWORD aptrefs)
3330 {
3331 ok(0, "unexpected call\n");
3332 return E_NOTIMPL;
3333 }
3334
3335 static HRESULT WINAPI testinitialize_PostUninitialize(IInitializeSpy *iface, DWORD aptrefs)
3336 {
3337 ok(0, "unexpected call\n");
3338 return E_NOTIMPL;
3339 }
3340
3341 static const IInitializeSpyVtbl testinitializevtbl =
3342 {
3343 testinitialize_QI,
3344 testinitialize_AddRef,
3345 testinitialize_Release,
3346 testinitialize_PreInitialize,
3347 testinitialize_PostInitialize,
3348 testinitialize_PreUninitialize,
3349 testinitialize_PostUninitialize
3350 };
3351
3352 static IInitializeSpy testinitialize = { &testinitializevtbl };
3353
3354 static void test_IInitializeSpy(void)
3355 {
3356 ULARGE_INTEGER cookie, cookie1, cookie2;
3357 HRESULT hr;
3358
3359 hr = CoRegisterInitializeSpy(NULL, NULL);
3360 ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
3361
3362 cookie.QuadPart = 1;
3363 hr = CoRegisterInitializeSpy(NULL, &cookie);
3364 ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
3365 ok(cookie.QuadPart == 1, "got wrong cookie\n");
3366
3367 hr = CoRegisterInitializeSpy(&testinitialize, NULL);
3368 ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
3369
3370 cookie.HighPart = 0;
3371 cookie.LowPart = 1;
3372 hr = CoRegisterInitializeSpy(&testinitialize, &cookie);
3373 ok(hr == S_OK, "got 0x%08x\n", hr);
3374 todo_wine {
3375 ok(cookie.HighPart == GetCurrentThreadId(), "got high part 0x%08x, expected 0x%08x\n", cookie.HighPart,
3376 GetCurrentThreadId());
3377 ok(cookie.LowPart == 0, "got wrong low part 0x%x\n", cookie.LowPart);
3378 }
3379 /* register same instance one more time */
3380 cookie1.HighPart = 0;
3381 cookie1.LowPart = 0;
3382 hr = CoRegisterInitializeSpy(&testinitialize, &cookie1);
3383 todo_wine {
3384 ok(hr == S_OK, "got 0x%08x\n", hr);
3385 ok(cookie1.HighPart == GetCurrentThreadId(), "got high part 0x%08x, expected 0x%08x\n", cookie1.HighPart,
3386 GetCurrentThreadId());
3387 ok(cookie1.LowPart == 1, "got wrong low part 0x%x\n", cookie1.LowPart);
3388 }
3389 cookie2.HighPart = 0;
3390 cookie2.LowPart = 0;
3391 hr = CoRegisterInitializeSpy(&testinitialize, &cookie2);
3392 todo_wine {
3393 ok(hr == S_OK, "got 0x%08x\n", hr);
3394 ok(cookie2.HighPart == GetCurrentThreadId(), "got high part 0x%08x, expected 0x%08x\n", cookie2.HighPart,
3395 GetCurrentThreadId());
3396 ok(cookie2.LowPart == 2, "got wrong low part 0x%x\n", cookie2.LowPart);
3397 }
3398 hr = CoRevokeInitializeSpy(cookie1);
3399 todo_wine
3400 ok(hr == S_OK, "got 0x%08x\n", hr);
3401
3402 hr = CoRevokeInitializeSpy(cookie1);
3403 ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
3404
3405 cookie1.HighPart = 0;
3406 cookie1.LowPart = 0;
3407 hr = CoRegisterInitializeSpy(&testinitialize, &cookie1);
3408 todo_wine {
3409 ok(hr == S_OK, "got 0x%08x\n", hr);
3410 ok(cookie1.HighPart == GetCurrentThreadId(), "got high part 0x%08x, expected 0x%08x\n", cookie1.HighPart,
3411 GetCurrentThreadId());
3412 ok(cookie1.LowPart == 1, "got wrong low part 0x%x\n", cookie1.LowPart);
3413 }
3414 hr = CoRevokeInitializeSpy(cookie);
3415 ok(hr == S_OK, "got 0x%08x\n", hr);
3416
3417 hr = CoRevokeInitializeSpy(cookie1);
3418 todo_wine
3419 ok(hr == S_OK, "got 0x%08x\n", hr);
3420
3421 hr = CoRevokeInitializeSpy(cookie2);
3422 todo_wine
3423 ok(hr == S_OK, "got 0x%08x\n", hr);
3424 }
3425
3426 static HRESULT g_persistfile_qi_ret;
3427 static HRESULT g_persistfile_load_ret;
3428 static HRESULT WINAPI testinstance_QI(IPersistFile *iface, REFIID riid, void **obj)
3429 {
3430 if (IsEqualIID(riid, &IID_IUnknown)) {
3431 *obj = iface;
3432 IUnknown_AddRef(iface);
3433 return S_OK;
3434 }
3435
3436 if (IsEqualIID(riid, &IID_IPersistFile)) {
3437 if (SUCCEEDED(g_persistfile_qi_ret)) {
3438 *obj = iface;
3439 IUnknown_AddRef(iface);
3440 }
3441 else
3442 *obj = NULL;
3443 return g_persistfile_qi_ret;
3444 }
3445
3446 ok(0, "unexpected riid %s\n", wine_dbgstr_guid(riid));
3447 *obj = NULL;
3448 return E_NOINTERFACE;
3449 }
3450
3451 static ULONG WINAPI testinstance_AddRef(IPersistFile *iface)
3452 {
3453 return 2;
3454 }
3455
3456 static ULONG WINAPI testinstance_Release(IPersistFile *iface)
3457 {
3458 return 1;
3459 }
3460
3461 static HRESULT WINAPI testinstance_GetClassID(IPersistFile *iface, CLSID *clsid)
3462 {
3463 ok(0, "unexpected call\n");
3464 return E_NOTIMPL;
3465 }
3466
3467 static HRESULT WINAPI testinstance_IsDirty(IPersistFile *iface)
3468 {
3469 ok(0, "unexpected call\n");
3470 return E_NOTIMPL;
3471 }
3472
3473 static HRESULT WINAPI testinstance_Load(IPersistFile *iface, LPCOLESTR filename, DWORD mode)
3474 {
3475 return g_persistfile_load_ret;
3476 }
3477
3478 static HRESULT WINAPI testinstance_Save(IPersistFile *iface, LPCOLESTR filename, BOOL remember)
3479 {
3480 return E_NOTIMPL;
3481 }
3482
3483 static HRESULT WINAPI testinstance_SaveCompleted(IPersistFile *iface, LPCOLESTR filename)
3484 {
3485 ok(0, "unexpected call\n");
3486 return E_NOTIMPL;
3487 }
3488
3489 static HRESULT WINAPI testinstance_GetCurFile(IPersistFile *iface, LPOLESTR *filename)
3490 {
3491 ok(0, "unexpected call\n");
3492 return E_NOTIMPL;
3493 }
3494
3495 static const IPersistFileVtbl testpersistfilevtbl = {
3496 testinstance_QI,
3497 testinstance_AddRef,
3498 testinstance_Release,
3499 testinstance_GetClassID,
3500 testinstance_IsDirty,
3501 testinstance_Load,
3502 testinstance_Save,
3503 testinstance_SaveCompleted,
3504 testinstance_GetCurFile
3505 };
3506
3507 static IPersistFile testpersistfile = { &testpersistfilevtbl };
3508
3509 static HRESULT WINAPI getinstance_cf_QI(IClassFactory *iface, REFIID riid, void **obj)
3510 {
3511 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory)) {
3512 *obj = iface;
3513 IClassFactory_AddRef(iface);
3514 return S_OK;
3515 }
3516
3517 *obj = NULL;
3518 return E_NOINTERFACE;
3519 }
3520
3521 static ULONG WINAPI getinstance_cf_AddRef(IClassFactory *iface)
3522 {
3523 return 2;
3524 }
3525
3526 static ULONG WINAPI getinstance_cf_Release(IClassFactory *iface)
3527 {
3528 return 1;
3529 }
3530
3531 static HRESULT WINAPI getinstance_cf_CreateInstance(IClassFactory *iface, IUnknown *outer,
3532 REFIID riid, void **obj)
3533 {
3534 if (IsEqualIID(riid, &IID_IUnknown)) {
3535 *obj = &testpersistfile;
3536 return S_OK;
3537 }
3538
3539 ok(0, "unexpected call, riid %s\n", wine_dbgstr_guid(riid));
3540 *obj = NULL;
3541 return E_NOTIMPL;
3542 }
3543
3544 static HRESULT WINAPI getinstance_cf_LockServer(IClassFactory *iface, BOOL lock)
3545 {
3546 ok(0, "unexpected call\n");
3547 return E_NOTIMPL;
3548 }
3549
3550 static const IClassFactoryVtbl getinstance_cf_vtbl = {
3551 getinstance_cf_QI,
3552 getinstance_cf_AddRef,
3553 getinstance_cf_Release,
3554 getinstance_cf_CreateInstance,
3555 getinstance_cf_LockServer
3556 };
3557
3558 static IClassFactory getinstance_cf = { &getinstance_cf_vtbl };
3559
3560 static void test_CoGetInstanceFromFile(void)
3561 {
3562 static const WCHAR filenameW[] = {'d','u','m','m','y','p','a','t','h',0};
3563 CLSID *clsid = (CLSID*)&CLSID_testclsid;
3564 MULTI_QI mqi[2];
3565 DWORD cookie;
3566 HRESULT hr;
3567
3568 hr = pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
3569 ok(hr == S_OK, "got 0x%08x\n", hr);
3570
3571 /* CLSID is not specified, file does not exist */
3572 mqi[0].pIID = &IID_IUnknown;
3573 mqi[0].pItf = NULL;
3574 mqi[0].hr = E_NOTIMPL;
3575 hr = CoGetInstanceFromFile(NULL, NULL, NULL, CLSCTX_INPROC_SERVER, STGM_READ, (OLECHAR*)filenameW, 1, mqi);
3576 todo_wine
3577 ok(hr == MK_E_CANTOPENFILE, "got 0x%08x\n", hr);
3578 ok(mqi[0].pItf == NULL, "got %p\n", mqi[0].pItf);
3579 ok(mqi[0].hr == E_NOINTERFACE, "got 0x%08x\n", mqi[0].hr);
3580
3581 /* class is not available */
3582 mqi[0].pIID = &IID_IUnknown;
3583 mqi[0].pItf = NULL;
3584 mqi[0].hr = E_NOTIMPL;
3585 hr = CoGetInstanceFromFile(NULL, clsid, NULL, CLSCTX_INPROC_SERVER, STGM_READ, (OLECHAR*)filenameW, 1, mqi);
3586 ok(hr == REGDB_E_CLASSNOTREG, "got 0x%08x\n", hr);
3587 ok(mqi[0].pItf == NULL, "got %p\n", mqi[0].pItf);
3588 ok(mqi[0].hr == REGDB_E_CLASSNOTREG, "got 0x%08x\n", mqi[0].hr);
3589
3590 hr = CoRegisterClassObject(clsid, (IUnknown*)&getinstance_cf, CLSCTX_INPROC_SERVER, REGCLS_MULTIPLEUSE,
3591 &cookie);
3592 ok(hr == S_OK, "got 0x%08x\n", hr);
3593
3594 mqi[0].pIID = &IID_IUnknown;
3595 mqi[0].pItf = (void*)0xdeadbeef;
3596 mqi[0].hr = S_OK;
3597 hr = CoGetInstanceFromFile(NULL, clsid, NULL, CLSCTX_INPROC_SERVER, STGM_READ, (OLECHAR*)filenameW, 1, mqi);
3598 todo_wine {
3599 ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
3600 ok(mqi[0].pItf == (void*)0xdeadbeef, "got %p\n", mqi[0].pItf);
3601 }
3602 ok(mqi[0].hr == S_OK, "got 0x%08x\n", mqi[0].hr);
3603
3604 mqi[0].pIID = &IID_IUnknown;
3605 mqi[0].pItf = (void*)0xdeadbeef;
3606 mqi[0].hr = E_NOTIMPL;
3607 hr = CoGetInstanceFromFile(NULL, clsid, NULL, CLSCTX_INPROC_SERVER, STGM_READ, (OLECHAR*)filenameW, 1, mqi);
3608 todo_wine {
3609 ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
3610 ok(mqi[0].pItf == (void*)0xdeadbeef, "got %p\n", mqi[0].pItf);
3611 ok(mqi[0].hr == E_NOTIMPL, "got 0x%08x\n", mqi[0].hr);
3612 }
3613 mqi[0].pIID = &IID_IUnknown;
3614 mqi[0].pItf = NULL;
3615 mqi[0].hr = E_NOTIMPL;
3616 hr = CoGetInstanceFromFile(NULL, clsid, NULL, CLSCTX_INPROC_SERVER, STGM_READ, (OLECHAR*)filenameW, 1, mqi);
3617 ok(hr == S_OK, "got 0x%08x\n", hr);
3618 ok(mqi[0].pItf != NULL, "got %p\n", mqi[0].pItf);
3619 ok(mqi[0].hr == S_OK, "got 0x%08x\n", mqi[0].hr);
3620
3621 mqi[0].pIID = &IID_IUnknown;
3622 mqi[0].pItf = NULL;
3623 mqi[0].hr = S_OK;
3624 hr = CoGetInstanceFromFile(NULL, clsid, NULL, CLSCTX_INPROC_SERVER, STGM_READ, (OLECHAR*)filenameW, 1, mqi);
3625 ok(hr == S_OK, "got 0x%08x\n", hr);
3626 ok(mqi[0].pItf != NULL, "got %p\n", mqi[0].pItf);
3627 ok(mqi[0].hr == S_OK, "got 0x%08x\n", mqi[0].hr);
3628
3629 mqi[0].pIID = &IID_IUnknown;
3630 mqi[0].pItf = NULL;
3631 mqi[0].hr = S_OK;
3632 g_persistfile_qi_ret = S_FALSE;
3633 hr = CoGetInstanceFromFile(NULL, clsid, NULL, CLSCTX_INPROC_SERVER, STGM_READ, (OLECHAR*)filenameW, 1, mqi);
3634 ok(hr == S_OK, "got 0x%08x\n", hr);
3635 ok(mqi[0].pItf != NULL, "got %p\n", mqi[0].pItf);
3636 ok(mqi[0].hr == S_OK, "got 0x%08x\n", mqi[0].hr);
3637 g_persistfile_qi_ret = S_OK;
3638
3639 mqi[0].pIID = &IID_IUnknown;
3640 mqi[0].pItf = NULL;
3641 mqi[0].hr = S_OK;
3642 mqi[1].pIID = &IID_IUnknown;
3643 mqi[1].pItf = NULL;
3644 mqi[1].hr = S_OK;
3645 g_persistfile_qi_ret = 0x8000efef;
3646 hr = CoGetInstanceFromFile(NULL, clsid, NULL, CLSCTX_INPROC_SERVER, STGM_READ, (OLECHAR*)filenameW, 2, mqi);
3647 ok(hr == 0x8000efef, "got 0x%08x\n", hr);
3648 ok(mqi[0].pItf == NULL, "got %p\n", mqi[0].pItf);
3649 ok(mqi[0].hr == 0x8000efef, "got 0x%08x\n", mqi[0].hr);
3650 ok(mqi[1].pItf == NULL, "got %p\n", mqi[1].pItf);
3651 ok(mqi[1].hr == 0x8000efef, "got 0x%08x\n", mqi[1].hr);
3652 g_persistfile_qi_ret = S_OK;
3653
3654 mqi[0].pIID = &IID_IUnknown;
3655 mqi[0].pItf = NULL;
3656 mqi[0].hr = S_OK;
3657 mqi[1].pIID = &IID_IUnknown;
3658 mqi[1].pItf = NULL;
3659 mqi[1].hr = S_OK;
3660 g_persistfile_load_ret = 0x8000fefe;
3661 hr = CoGetInstanceFromFile(NULL, clsid, NULL, CLSCTX_INPROC_SERVER, STGM_READ, (OLECHAR*)filenameW, 2, mqi);
3662 ok(hr == 0x8000fefe, "got 0x%08x\n", hr);
3663 ok(mqi[0].pItf == NULL, "got %p\n", mqi[0].pItf);
3664 ok(mqi[0].hr == 0x8000fefe, "got 0x%08x\n", mqi[0].hr);
3665 ok(mqi[1].pItf == NULL, "got %p\n", mqi[1].pItf);
3666 ok(mqi[1].hr == 0x8000fefe, "got 0x%08x\n", mqi[1].hr);
3667 g_persistfile_load_ret = S_OK;
3668
3669 hr = CoRevokeClassObject(cookie);
3670 ok(hr == S_OK, "got 0x%08x\n", hr);
3671
3672 CoUninitialize();
3673 }
3674
3675 static void test_GlobalOptions(void)
3676 {
3677 IGlobalOptions *global_options;
3678 HRESULT hres;
3679
3680 CoInitialize(NULL);
3681
3682 hres = CoCreateInstance(&CLSID_GlobalOptions, NULL, CLSCTX_INPROC_SERVER,
3683 &IID_IGlobalOptions, (void**)&global_options);
3684 ok(hres == S_OK || broken(hres == E_NOINTERFACE), "CoCreateInstance(CLSID_GlobalOptions) failed: %08x\n", hres);
3685 if(FAILED(hres))
3686 {
3687 win_skip("CLSID_GlobalOptions not available\n");
3688 CoUninitialize();
3689 return;
3690 }
3691
3692 IGlobalOptions_Release(global_options);
3693
3694 hres = CoCreateInstance(&CLSID_GlobalOptions, (IUnknown*)0xdeadbeef, CLSCTX_INPROC_SERVER,
3695 &IID_IGlobalOptions, (void**)&global_options);
3696 ok(hres == E_INVALIDARG, "CoCreateInstance(CLSID_GlobalOptions) failed: %08x\n", hres);
3697
3698 CoUninitialize();
3699 }
3700
3701 static void init_funcs(void)
3702 {
3703 HMODULE hOle32 = GetModuleHandleA("ole32");
3704 HMODULE hAdvapi32 = GetModuleHandleA("advapi32");
3705 HMODULE hkernel32 = GetModuleHandleA("kernel32");
3706
3707 pCoGetObjectContext = (void*)GetProcAddress(hOle32, "CoGetObjectContext");
3708 pCoSwitchCallContext = (void*)GetProcAddress(hOle32, "CoSwitchCallContext");
3709 pCoGetTreatAsClass = (void*)GetProcAddress(hOle32,"CoGetTreatAsClass");
3710 pCoTreatAsClass = (void*)GetProcAddress(hOle32,"CoTreatAsClass");
3711 pCoGetContextToken = (void*)GetProcAddress(hOle32, "CoGetContextToken");
3712 pCoGetApartmentType = (void*)GetProcAddress(hOle32, "CoGetApartmentType");
3713 pRegDeleteKeyExA = (void*)GetProcAddress(hAdvapi32, "RegDeleteKeyExA");
3714 pRegOverridePredefKey = (void*)GetProcAddress(hAdvapi32, "RegOverridePredefKey");
3715 pCoInitializeEx = (void*)GetProcAddress(hOle32, "CoInitializeEx");
3716
3717 pActivateActCtx = (void*)GetProcAddress(hkernel32, "ActivateActCtx");
3718 pCreateActCtxW = (void*)GetProcAddress(hkernel32, "CreateActCtxW");
3719 pDeactivateActCtx = (void*)GetProcAddress(hkernel32, "DeactivateActCtx");
3720 pIsWow64Process = (void*)GetProcAddress(hkernel32, "IsWow64Process");
3721 pReleaseActCtx = (void*)GetProcAddress(hkernel32, "ReleaseActCtx");
3722 }
3723
3724 static DWORD CALLBACK implicit_mta_proc(void *param)
3725 {
3726 IComThreadingInfo *threading_info;
3727 ULONG_PTR token;
3728 IUnknown *unk;
3729 DWORD cookie;
3730 CLSID clsid;
3731 HRESULT hr;
3732
3733 test_apt_type(APTTYPE_MTA, APTTYPEQUALIFIER_IMPLICIT_MTA);
3734
3735 hr = CoCreateInstance(&CLSID_InternetZoneManager, NULL, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void **)&unk);
3736 ok_ole_success(hr, "CoCreateInstance");
3737 IUnknown_Release(unk);
3738
3739 hr = CoGetClassObject(&CLSID_InternetZoneManager, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void **)&unk);
3740 ok_ole_success(hr, "CoGetClassObject");
3741 IUnknown_Release(unk);
3742
3743 hr = CoGetObjectContext(&IID_IComThreadingInfo, (void **)&threading_info);
3744 ok_ole_success(hr, "CoGetObjectContext");
3745 IComThreadingInfo_Release(threading_info);
3746
3747 hr = CoGetContextToken(&token);
3748 ok_ole_success(hr, "CoGetContextToken");
3749
3750 hr = CoRegisterPSClsid(&IID_IWineTest, &CLSID_WineTestPSFactoryBuffer);
3751 ok_ole_success(hr, "CoRegisterPSClsid");
3752
3753 hr = CoGetPSClsid(&IID_IClassFactory, &clsid);
3754 ok_ole_success(hr, "CoGetPSClsid");
3755
3756 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
3757 CLSCTX_INPROC_SERVER, REGCLS_SINGLEUSE, &cookie);
3758 ok_ole_success(hr, "CoRegisterClassObject");
3759
3760 hr = CoRevokeClassObject(cookie);
3761 ok_ole_success(hr, "CoRevokeClassObject");
3762
3763 hr = CoRegisterMessageFilter(NULL, NULL);
3764 ok(hr == CO_E_NOT_SUPPORTED, "got %#x\n", hr);
3765
3766 hr = CoLockObjectExternal((IUnknown *)&Test_Unknown, TRUE, TRUE);
3767 ok_ole_success(hr, "CoLockObjectExternal");
3768
3769 hr = CoDisconnectObject((IUnknown *)&Test_Unknown, 0);
3770 ok_ole_success(hr, "CoDisconnectObject");
3771
3772 return 0;
3773 }
3774
3775 /* Some COM functions (perhaps even all of them?) can make use of an "implicit"
3776 * multi-threaded apartment created by another thread in the same process. */
3777 static void test_implicit_mta(void)
3778 {
3779 HANDLE thread;
3780
3781 CoInitializeEx(NULL, COINIT_MULTITHREADED);
3782
3783 thread = CreateThread(NULL, 0, implicit_mta_proc, NULL, 0, NULL);
3784 ok(!WaitForSingleObject(thread, 1000), "wait failed\n");
3785
3786 CoUninitialize();
3787 }
3788
3789 START_TEST(compobj)
3790 {
3791 init_funcs();
3792
3793 if (!pCoInitializeEx)
3794 {
3795 trace("You need DCOM95 installed to run this test\n");
3796 return;
3797 }
3798
3799 if (!pCreateActCtxW)
3800 win_skip("Activation contexts are not supported, some tests will be skipped.\n");
3801
3802 test_ProgIDFromCLSID();
3803 test_CLSIDFromProgID();
3804 test_CLSIDFromString();
3805 test_IIDFromString();
3806 test_StringFromGUID2();
3807 test_CoCreateInstance();
3808 test_ole_menu();
3809 test_CoGetClassObject();
3810 test_CoCreateInstanceEx();
3811 test_CoRegisterMessageFilter();
3812 test_CoRegisterPSClsid();
3813 test_CoGetPSClsid();
3814 test_CoUnmarshalInterface();
3815 test_CoGetInterfaceAndReleaseStream();
3816 test_CoMarshalInterface();
3817 test_CoMarshalInterThreadInterfaceInStream();
3818 test_CoRegisterClassObject();
3819 test_registered_object_thread_affinity();
3820 test_CoFreeUnusedLibraries();
3821 test_CoGetObjectContext();
3822 test_CoGetCallContext();
3823 test_CoGetContextToken();
3824 test_TreatAsClass();
3825 test_CoInitializeEx();
3826 test_OleInitialize_InitCounting();
3827 test_OleRegGetMiscStatus();
3828 test_CoCreateGuid();
3829 test_CoWaitForMultipleHandles();
3830 test_CoGetMalloc();
3831 test_OleRegGetUserType();
3832 test_CoGetApartmentType();
3833 test_IMallocSpy();
3834 test_CoGetCurrentLogicalThreadId();
3835 test_IInitializeSpy();
3836 test_CoGetInstanceFromFile();
3837 test_GlobalOptions();
3838 test_implicit_mta();
3839 }