[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