[OLE32_WINETEST]
[reactos.git] / rostests / winetests / ole32 / marshal.c
1 /*
2 * Marshaling Tests
3 *
4 * Copyright 2004 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 _WIN32_DCOM
22 #define COBJMACROS
23 #define CONST_VTABLE
24
25 #include <stdarg.h>
26 #include <stdio.h>
27
28 #include "windef.h"
29 #include "winbase.h"
30 #include "objbase.h"
31 #include "olectl.h"
32 #include "shlguid.h"
33 #include "shobjidl.h"
34 #include "initguid.h"
35
36 #include "wine/test.h"
37
38 DEFINE_GUID(CLSID_StdGlobalInterfaceTable,0x00000323,0x0000,0x0000,0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46);
39 DEFINE_GUID(CLSID_ManualResetEvent, 0x0000032c,0x0000,0x0000,0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46);
40
41 /* functions that are not present on all versions of Windows */
42 static HRESULT (WINAPI * pCoInitializeEx)(LPVOID lpReserved, DWORD dwCoInit);
43
44 /* helper macros to make tests a bit leaner */
45 #define ok_more_than_one_lock() ok(cLocks > 0, "Number of locks should be > 0, but actually is %d\n", cLocks)
46 #define ok_no_locks() ok(cLocks == 0, "Number of locks should be 0, but actually is %d\n", cLocks)
47 #define ok_ole_success(hr, func) ok(hr == S_OK, #func " failed with error 0x%08x\n", hr)
48
49 static const IID IID_IWineTest =
50 {
51 0x5201163f,
52 0x8164,
53 0x4fd0,
54 {0xa1, 0xa2, 0x5d, 0x5a, 0x36, 0x54, 0xd3, 0xbd}
55 }; /* 5201163f-8164-4fd0-a1a2-5d5a3654d3bd */
56
57 static const IID IID_IRemUnknown =
58 {
59 0x00000131,
60 0x0000,
61 0x0000,
62 {0xc0,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46}
63 };
64
65 #define EXTENTID_WineTest IID_IWineTest
66 #define CLSID_WineTest IID_IWineTest
67
68 static const CLSID CLSID_WineOOPTest =
69 {
70 0x5201163f,
71 0x8164,
72 0x4fd0,
73 {0xa1, 0xa2, 0x5d, 0x5a, 0x36, 0x54, 0xd3, 0xbd}
74 }; /* 5201163f-8164-4fd0-a1a2-5d5a3654d3bd */
75
76 static void test_cocreateinstance_proxy(void)
77 {
78 IUnknown *pProxy;
79 IMultiQI *pMQI;
80 HRESULT hr;
81
82 pCoInitializeEx(NULL, COINIT_MULTITHREADED);
83
84 hr = CoCreateInstance(&CLSID_ShellDesktop, NULL, CLSCTX_INPROC, &IID_IUnknown, (void **)&pProxy);
85 ok_ole_success(hr, CoCreateInstance);
86 hr = IUnknown_QueryInterface(pProxy, &IID_IMultiQI, (void **)&pMQI);
87 ok(hr == S_OK, "created object is not a proxy, so was created in the wrong apartment\n");
88 if (hr == S_OK)
89 IMultiQI_Release(pMQI);
90 IUnknown_Release(pProxy);
91
92 CoUninitialize();
93 }
94
95 static const LARGE_INTEGER ullZero;
96 static LONG cLocks;
97
98 static void LockModule(void)
99 {
100 InterlockedIncrement(&cLocks);
101 }
102
103 static void UnlockModule(void)
104 {
105 InterlockedDecrement(&cLocks);
106 }
107
108
109 static HRESULT WINAPI Test_IUnknown_QueryInterface(
110 LPUNKNOWN iface,
111 REFIID riid,
112 LPVOID *ppvObj)
113 {
114 if (ppvObj == NULL) return E_POINTER;
115
116 if (IsEqualGUID(riid, &IID_IUnknown))
117 {
118 *ppvObj = iface;
119 IUnknown_AddRef(iface);
120 return S_OK;
121 }
122
123 *ppvObj = NULL;
124 return E_NOINTERFACE;
125 }
126
127 static ULONG WINAPI Test_IUnknown_AddRef(LPUNKNOWN iface)
128 {
129 LockModule();
130 return 2; /* non-heap-based object */
131 }
132
133 static ULONG WINAPI Test_IUnknown_Release(LPUNKNOWN iface)
134 {
135 UnlockModule();
136 return 1; /* non-heap-based object */
137 }
138
139 static const IUnknownVtbl TestUnknown_Vtbl =
140 {
141 Test_IUnknown_QueryInterface,
142 Test_IUnknown_AddRef,
143 Test_IUnknown_Release,
144 };
145
146 static IUnknown Test_Unknown = { &TestUnknown_Vtbl };
147
148
149 static HRESULT WINAPI Test_IClassFactory_QueryInterface(
150 LPCLASSFACTORY iface,
151 REFIID riid,
152 LPVOID *ppvObj)
153 {
154 if (ppvObj == NULL) return E_POINTER;
155
156 if (IsEqualGUID(riid, &IID_IUnknown) ||
157 IsEqualGUID(riid, &IID_IClassFactory) ||
158 /* the only other interface Wine is currently able to marshal (for testing two proxies) */
159 IsEqualGUID(riid, &IID_IRemUnknown))
160 {
161 *ppvObj = iface;
162 IClassFactory_AddRef(iface);
163 return S_OK;
164 }
165
166 *ppvObj = NULL;
167 return E_NOINTERFACE;
168 }
169
170 static ULONG WINAPI Test_IClassFactory_AddRef(LPCLASSFACTORY iface)
171 {
172 LockModule();
173 return 2; /* non-heap-based object */
174 }
175
176 static ULONG WINAPI Test_IClassFactory_Release(LPCLASSFACTORY iface)
177 {
178 UnlockModule();
179 return 1; /* non-heap-based object */
180 }
181
182 static HRESULT WINAPI Test_IClassFactory_CreateInstance(
183 LPCLASSFACTORY iface,
184 LPUNKNOWN pUnkOuter,
185 REFIID riid,
186 LPVOID *ppvObj)
187 {
188 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
189 return IUnknown_QueryInterface((IUnknown*)&Test_Unknown, riid, ppvObj);
190 }
191
192 static HRESULT WINAPI Test_IClassFactory_LockServer(
193 LPCLASSFACTORY iface,
194 BOOL fLock)
195 {
196 return S_OK;
197 }
198
199 static const IClassFactoryVtbl TestClassFactory_Vtbl =
200 {
201 Test_IClassFactory_QueryInterface,
202 Test_IClassFactory_AddRef,
203 Test_IClassFactory_Release,
204 Test_IClassFactory_CreateInstance,
205 Test_IClassFactory_LockServer
206 };
207
208 static IClassFactory Test_ClassFactory = { &TestClassFactory_Vtbl };
209
210 #define RELEASEMARSHALDATA WM_USER
211
212 struct host_object_data
213 {
214 IStream *stream;
215 IID iid;
216 IUnknown *object;
217 MSHLFLAGS marshal_flags;
218 HANDLE marshal_event;
219 IMessageFilter *filter;
220 };
221
222 static DWORD CALLBACK host_object_proc(LPVOID p)
223 {
224 struct host_object_data *data = p;
225 HRESULT hr;
226 MSG msg;
227
228 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
229
230 if (data->filter)
231 {
232 IMessageFilter * prev_filter = NULL;
233 hr = CoRegisterMessageFilter(data->filter, &prev_filter);
234 if (prev_filter) IMessageFilter_Release(prev_filter);
235 ok_ole_success(hr, CoRegisterMessageFilter);
236 }
237
238 hr = CoMarshalInterface(data->stream, &data->iid, data->object, MSHCTX_INPROC, NULL, data->marshal_flags);
239 ok_ole_success(hr, CoMarshalInterface);
240
241 /* force the message queue to be created before signaling parent thread */
242 PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
243
244 SetEvent(data->marshal_event);
245
246 while (GetMessage(&msg, NULL, 0, 0))
247 {
248 if (msg.hwnd == NULL && msg.message == RELEASEMARSHALDATA)
249 {
250 CoReleaseMarshalData(data->stream);
251 SetEvent((HANDLE)msg.lParam);
252 }
253 else
254 DispatchMessage(&msg);
255 }
256
257 HeapFree(GetProcessHeap(), 0, data);
258
259 CoUninitialize();
260
261 return hr;
262 }
263
264 static DWORD start_host_object2(IStream *stream, REFIID riid, IUnknown *object, MSHLFLAGS marshal_flags, IMessageFilter *filter, HANDLE *thread)
265 {
266 DWORD tid = 0;
267 HANDLE marshal_event = CreateEvent(NULL, FALSE, FALSE, NULL);
268 struct host_object_data *data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data));
269
270 data->stream = stream;
271 data->iid = *riid;
272 data->object = object;
273 data->marshal_flags = marshal_flags;
274 data->marshal_event = marshal_event;
275 data->filter = filter;
276
277 *thread = CreateThread(NULL, 0, host_object_proc, data, 0, &tid);
278
279 /* wait for marshaling to complete before returning */
280 ok( !WaitForSingleObject(marshal_event, 10000), "wait timed out\n" );
281 CloseHandle(marshal_event);
282
283 return tid;
284 }
285
286 static DWORD start_host_object(IStream *stream, REFIID riid, IUnknown *object, MSHLFLAGS marshal_flags, HANDLE *thread)
287 {
288 return start_host_object2(stream, riid, object, marshal_flags, NULL, thread);
289 }
290
291 /* asks thread to release the marshal data because it has to be done by the
292 * same thread that marshaled the interface in the first place. */
293 static void release_host_object(DWORD tid)
294 {
295 HANDLE event = CreateEvent(NULL, FALSE, FALSE, NULL);
296 PostThreadMessage(tid, RELEASEMARSHALDATA, 0, (LPARAM)event);
297 ok( !WaitForSingleObject(event, 10000), "wait timed out\n" );
298 CloseHandle(event);
299 }
300
301 static void end_host_object(DWORD tid, HANDLE thread)
302 {
303 BOOL ret = PostThreadMessage(tid, WM_QUIT, 0, 0);
304 ok(ret, "PostThreadMessage failed with error %d\n", GetLastError());
305 /* be careful of races - don't return until hosting thread has terminated */
306 ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
307 CloseHandle(thread);
308 }
309
310 /* tests failure case of interface not having a marshaler specified in the
311 * registry */
312 static void test_no_marshaler(void)
313 {
314 IStream *pStream;
315 HRESULT hr;
316
317 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
318 ok_ole_success(hr, CreateStreamOnHGlobal);
319 hr = CoMarshalInterface(pStream, &IID_IWineTest, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
320 ok(hr == E_NOINTERFACE, "CoMarshalInterface should have returned E_NOINTERFACE instead of 0x%08x\n", hr);
321
322 IStream_Release(pStream);
323 }
324
325 /* tests normal marshal and then release without unmarshaling */
326 static void test_normal_marshal_and_release(void)
327 {
328 HRESULT hr;
329 IStream *pStream = NULL;
330
331 cLocks = 0;
332
333 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
334 ok_ole_success(hr, CreateStreamOnHGlobal);
335 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
336 ok_ole_success(hr, CoMarshalInterface);
337
338 ok_more_than_one_lock();
339
340 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
341 hr = CoReleaseMarshalData(pStream);
342 ok_ole_success(hr, CoReleaseMarshalData);
343 IStream_Release(pStream);
344
345 ok_no_locks();
346 }
347
348 /* tests success case of a same-thread marshal and unmarshal */
349 static void test_normal_marshal_and_unmarshal(void)
350 {
351 HRESULT hr;
352 IStream *pStream = NULL;
353 IUnknown *pProxy = NULL;
354
355 cLocks = 0;
356
357 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
358 ok_ole_success(hr, CreateStreamOnHGlobal);
359 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
360 ok_ole_success(hr, CoMarshalInterface);
361
362 ok_more_than_one_lock();
363
364 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
365 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
366 ok_ole_success(hr, CoUnmarshalInterface);
367 IStream_Release(pStream);
368
369 ok_more_than_one_lock();
370
371 IUnknown_Release(pProxy);
372
373 ok_no_locks();
374 }
375
376 /* tests failure case of unmarshaling a freed object */
377 static void test_marshal_and_unmarshal_invalid(void)
378 {
379 HRESULT hr;
380 IStream *pStream = NULL;
381 IClassFactory *pProxy = NULL;
382 DWORD tid;
383 void * dummy;
384 HANDLE thread;
385
386 cLocks = 0;
387
388 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
389 ok_ole_success(hr, CreateStreamOnHGlobal);
390 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
391
392 ok_more_than_one_lock();
393
394 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
395 hr = CoReleaseMarshalData(pStream);
396 ok_ole_success(hr, CoReleaseMarshalData);
397
398 ok_no_locks();
399
400 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
401 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
402 todo_wine { ok_ole_success(hr, CoUnmarshalInterface); }
403
404 ok_no_locks();
405
406 if (pProxy)
407 {
408 hr = IClassFactory_CreateInstance(pProxy, NULL, &IID_IUnknown, &dummy);
409 ok(hr == RPC_E_DISCONNECTED, "Remote call should have returned RPC_E_DISCONNECTED, instead of 0x%08x\n", hr);
410
411 IClassFactory_Release(pProxy);
412 }
413
414 IStream_Release(pStream);
415
416 end_host_object(tid, thread);
417 }
418
419 static void test_same_apartment_unmarshal_failure(void)
420 {
421 HRESULT hr;
422 IStream *pStream;
423 IUnknown *pProxy;
424 static const LARGE_INTEGER llZero;
425
426 cLocks = 0;
427
428 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
429 ok_ole_success(hr, CreateStreamOnHGlobal);
430
431 hr = CoMarshalInterface(pStream, &IID_IUnknown, (IUnknown *)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
432 ok_ole_success(hr, CoMarshalInterface);
433
434 ok_more_than_one_lock();
435
436 hr = IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
437 ok_ole_success(hr, IStream_Seek);
438
439 hr = CoUnmarshalInterface(pStream, &IID_IParseDisplayName, (void **)&pProxy);
440 ok(hr == E_NOINTERFACE, "CoUnmarshalInterface should have returned E_NOINTERFACE instead of 0x%08x\n", hr);
441
442 ok_no_locks();
443
444 IStream_Release(pStream);
445 }
446
447 /* tests success case of an interthread marshal */
448 static void test_interthread_marshal_and_unmarshal(void)
449 {
450 HRESULT hr;
451 IStream *pStream = NULL;
452 IUnknown *pProxy = NULL;
453 DWORD tid;
454 HANDLE thread;
455
456 cLocks = 0;
457
458 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
459 ok_ole_success(hr, CreateStreamOnHGlobal);
460 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
461
462 ok_more_than_one_lock();
463
464 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
465 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
466 ok_ole_success(hr, CoUnmarshalInterface);
467 IStream_Release(pStream);
468
469 ok_more_than_one_lock();
470
471 IUnknown_Release(pProxy);
472
473 ok_no_locks();
474
475 end_host_object(tid, thread);
476 }
477
478 /* the number of external references that Wine's proxy manager normally gives
479 * out, so we can test the border case of running out of references */
480 #define NORMALEXTREFS 5
481
482 /* tests success case of an interthread marshal and then marshaling the proxy */
483 static void test_proxy_marshal_and_unmarshal(void)
484 {
485 HRESULT hr;
486 IStream *pStream = NULL;
487 IUnknown *pProxy = NULL;
488 IUnknown *pProxy2 = NULL;
489 DWORD tid;
490 HANDLE thread;
491 int i;
492
493 cLocks = 0;
494
495 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
496 ok_ole_success(hr, CreateStreamOnHGlobal);
497 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
498
499 ok_more_than_one_lock();
500
501 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
502 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
503 ok_ole_success(hr, CoUnmarshalInterface);
504
505 ok_more_than_one_lock();
506
507 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
508 /* marshal the proxy */
509 hr = CoMarshalInterface(pStream, &IID_IClassFactory, pProxy, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
510 ok_ole_success(hr, CoMarshalInterface);
511
512 ok_more_than_one_lock();
513
514 /* marshal 5 more times to exhaust the normal external references of 5 */
515 for (i = 0; i < NORMALEXTREFS; i++)
516 {
517 hr = CoMarshalInterface(pStream, &IID_IClassFactory, pProxy, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
518 ok_ole_success(hr, CoMarshalInterface);
519 }
520
521 ok_more_than_one_lock();
522
523 /* release the original proxy to test that we successfully keep the
524 * original object alive */
525 IUnknown_Release(pProxy);
526
527 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
528 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
529 ok_ole_success(hr, CoUnmarshalInterface);
530
531 ok_more_than_one_lock();
532
533 IUnknown_Release(pProxy2);
534
535 /* unmarshal all of the proxies to check that the object stub still exists */
536 for (i = 0; i < NORMALEXTREFS; i++)
537 {
538 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
539 ok_ole_success(hr, CoUnmarshalInterface);
540
541 IUnknown_Release(pProxy2);
542 }
543
544 ok_no_locks();
545
546 IStream_Release(pStream);
547
548 end_host_object(tid, thread);
549 }
550
551 /* tests success case of an interthread marshal and then marshaling the proxy
552 * using an iid that hasn't previously been unmarshaled */
553 static void test_proxy_marshal_and_unmarshal2(void)
554 {
555 HRESULT hr;
556 IStream *pStream = NULL;
557 IUnknown *pProxy = NULL;
558 IUnknown *pProxy2 = NULL;
559 DWORD tid;
560 HANDLE thread;
561
562 cLocks = 0;
563
564 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
565 ok_ole_success(hr, CreateStreamOnHGlobal);
566 tid = start_host_object(pStream, &IID_IUnknown, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
567
568 ok_more_than_one_lock();
569
570 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
571 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)&pProxy);
572 ok_ole_success(hr, CoUnmarshalInterface);
573
574 ok_more_than_one_lock();
575
576 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
577 /* marshal the proxy */
578 hr = CoMarshalInterface(pStream, &IID_IClassFactory, pProxy, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
579 ok_ole_success(hr, CoMarshalInterface);
580
581 ok_more_than_one_lock();
582
583 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
584 /* unmarshal the second proxy to the object */
585 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
586 ok_ole_success(hr, CoUnmarshalInterface);
587 IStream_Release(pStream);
588
589 /* now the proxies should be as follows:
590 * pProxy -> &Test_ClassFactory
591 * pProxy2 -> &Test_ClassFactory
592 * they should NOT be as follows:
593 * pProxy -> &Test_ClassFactory
594 * pProxy2 -> pProxy
595 * the above can only really be tested by looking in +ole traces
596 */
597
598 ok_more_than_one_lock();
599
600 IUnknown_Release(pProxy);
601
602 ok_more_than_one_lock();
603
604 IUnknown_Release(pProxy2);
605
606 ok_no_locks();
607
608 end_host_object(tid, thread);
609 }
610
611 /* tests success case of an interthread marshal and then table-weak-marshaling the proxy */
612 static void test_proxy_marshal_and_unmarshal_weak(void)
613 {
614 HRESULT hr;
615 IStream *pStream = NULL;
616 IUnknown *pProxy = NULL;
617 IUnknown *pProxy2 = NULL;
618 DWORD tid;
619 HANDLE thread;
620
621 cLocks = 0;
622
623 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
624 ok_ole_success(hr, CreateStreamOnHGlobal);
625 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
626
627 ok_more_than_one_lock();
628
629 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
630 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
631 ok_ole_success(hr, CoUnmarshalInterface);
632
633 ok_more_than_one_lock();
634
635 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
636 /* marshal the proxy */
637 hr = CoMarshalInterface(pStream, &IID_IClassFactory, pProxy, MSHCTX_INPROC, NULL, MSHLFLAGS_TABLEWEAK);
638 ok_ole_success(hr, CoMarshalInterface);
639
640 ok_more_than_one_lock();
641
642 /* release the original proxy to test that we successfully keep the
643 * original object alive */
644 IUnknown_Release(pProxy);
645
646 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
647 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
648 todo_wine
649 ok(hr == CO_E_OBJNOTREG, "CoUnmarshalInterface should return CO_E_OBJNOTREG instead of 0x%08x\n", hr);
650
651 ok_no_locks();
652
653 IStream_Release(pStream);
654
655 end_host_object(tid, thread);
656 }
657
658 /* tests success case of an interthread marshal and then table-strong-marshaling the proxy */
659 static void test_proxy_marshal_and_unmarshal_strong(void)
660 {
661 HRESULT hr;
662 IStream *pStream = NULL;
663 IUnknown *pProxy = NULL;
664 IUnknown *pProxy2 = NULL;
665 DWORD tid;
666 HANDLE thread;
667
668 cLocks = 0;
669
670 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
671 ok_ole_success(hr, CreateStreamOnHGlobal);
672 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
673
674 ok_more_than_one_lock();
675
676 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
677 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
678 ok_ole_success(hr, CoUnmarshalInterface);
679
680 ok_more_than_one_lock();
681
682 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
683 /* marshal the proxy */
684 hr = CoMarshalInterface(pStream, &IID_IClassFactory, pProxy, MSHCTX_INPROC, NULL, MSHLFLAGS_TABLESTRONG);
685 ok(hr == S_OK /* WinNT */ || hr == E_INVALIDARG /* Win9x */,
686 "CoMarshalInterface should have return S_OK or E_INVALIDARG instead of 0x%08x\n", hr);
687 if (FAILED(hr))
688 {
689 IUnknown_Release(pProxy);
690 goto end;
691 }
692
693 ok_more_than_one_lock();
694
695 /* release the original proxy to test that we successfully keep the
696 * original object alive */
697 IUnknown_Release(pProxy);
698
699 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
700 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
701 ok_ole_success(hr, CoUnmarshalInterface);
702
703 ok_more_than_one_lock();
704
705 IUnknown_Release(pProxy2);
706
707 ok_more_than_one_lock();
708
709 end:
710 IStream_Release(pStream);
711
712 end_host_object(tid, thread);
713
714 ok_no_locks();
715 }
716
717 /* tests that stubs are released when the containing apartment is destroyed */
718 static void test_marshal_stub_apartment_shutdown(void)
719 {
720 HRESULT hr;
721 IStream *pStream = NULL;
722 IUnknown *pProxy = NULL;
723 DWORD tid;
724 HANDLE thread;
725
726 cLocks = 0;
727
728 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
729 ok_ole_success(hr, CreateStreamOnHGlobal);
730 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
731
732 ok_more_than_one_lock();
733
734 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
735 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
736 ok_ole_success(hr, CoUnmarshalInterface);
737 IStream_Release(pStream);
738
739 ok_more_than_one_lock();
740
741 end_host_object(tid, thread);
742
743 ok_no_locks();
744
745 IUnknown_Release(pProxy);
746
747 ok_no_locks();
748 }
749
750 /* tests that proxies are released when the containing apartment is destroyed */
751 static void test_marshal_proxy_apartment_shutdown(void)
752 {
753 HRESULT hr;
754 IStream *pStream = NULL;
755 IUnknown *pProxy = NULL;
756 DWORD tid;
757 HANDLE thread;
758
759 cLocks = 0;
760
761 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
762 ok_ole_success(hr, CreateStreamOnHGlobal);
763 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
764
765 ok_more_than_one_lock();
766
767 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
768 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
769 ok_ole_success(hr, CoUnmarshalInterface);
770 IStream_Release(pStream);
771
772 ok_more_than_one_lock();
773
774 CoUninitialize();
775
776 ok_no_locks();
777
778 IUnknown_Release(pProxy);
779
780 ok_no_locks();
781
782 end_host_object(tid, thread);
783
784 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
785 }
786
787 /* tests that proxies are released when the containing mta apartment is destroyed */
788 static void test_marshal_proxy_mta_apartment_shutdown(void)
789 {
790 HRESULT hr;
791 IStream *pStream = NULL;
792 IUnknown *pProxy = NULL;
793 DWORD tid;
794 HANDLE thread;
795
796 CoUninitialize();
797 pCoInitializeEx(NULL, COINIT_MULTITHREADED);
798
799 cLocks = 0;
800
801 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
802 ok_ole_success(hr, CreateStreamOnHGlobal);
803 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
804
805 ok_more_than_one_lock();
806
807 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
808 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
809 ok_ole_success(hr, CoUnmarshalInterface);
810 IStream_Release(pStream);
811
812 ok_more_than_one_lock();
813
814 CoUninitialize();
815
816 ok_no_locks();
817
818 IUnknown_Release(pProxy);
819
820 ok_no_locks();
821
822 end_host_object(tid, thread);
823
824 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
825 }
826
827 struct ncu_params
828 {
829 LPSTREAM stream;
830 HANDLE marshal_event;
831 HANDLE unmarshal_event;
832 };
833
834 /* helper for test_no_couninitialize_server */
835 static DWORD CALLBACK no_couninitialize_server_proc(LPVOID p)
836 {
837 struct ncu_params *ncu_params = p;
838 HRESULT hr;
839
840 pCoInitializeEx(NULL, COINIT_MULTITHREADED);
841
842 hr = CoMarshalInterface(ncu_params->stream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
843 ok_ole_success(hr, CoMarshalInterface);
844
845 SetEvent(ncu_params->marshal_event);
846
847 ok( !WaitForSingleObject(ncu_params->unmarshal_event, 10000), "wait timed out\n" );
848
849 /* die without calling CoUninitialize */
850
851 return 0;
852 }
853
854 /* tests apartment that an apartment with a stub is released without deadlock
855 * if the owning thread exits */
856 static void test_no_couninitialize_server(void)
857 {
858 HRESULT hr;
859 IStream *pStream = NULL;
860 IUnknown *pProxy = NULL;
861 DWORD tid;
862 HANDLE thread;
863 struct ncu_params ncu_params;
864
865 cLocks = 0;
866
867 ncu_params.marshal_event = CreateEvent(NULL, TRUE, FALSE, NULL);
868 ncu_params.unmarshal_event = CreateEvent(NULL, TRUE, FALSE, NULL);
869
870 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
871 ok_ole_success(hr, CreateStreamOnHGlobal);
872 ncu_params.stream = pStream;
873
874 thread = CreateThread(NULL, 0, no_couninitialize_server_proc, &ncu_params, 0, &tid);
875
876 ok( !WaitForSingleObject(ncu_params.marshal_event, 10000), "wait timed out\n" );
877 ok_more_than_one_lock();
878
879 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
880 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
881 ok_ole_success(hr, CoUnmarshalInterface);
882 IStream_Release(pStream);
883
884 ok_more_than_one_lock();
885
886 SetEvent(ncu_params.unmarshal_event);
887 ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
888
889 ok_no_locks();
890
891 CloseHandle(thread);
892 CloseHandle(ncu_params.marshal_event);
893 CloseHandle(ncu_params.unmarshal_event);
894
895 IUnknown_Release(pProxy);
896
897 ok_no_locks();
898 }
899
900 /* STA -> STA call during DLL_THREAD_DETACH */
901 static DWORD CALLBACK no_couninitialize_client_proc(LPVOID p)
902 {
903 struct ncu_params *ncu_params = p;
904 HRESULT hr;
905 IUnknown *pProxy = NULL;
906
907 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
908
909 hr = CoUnmarshalInterface(ncu_params->stream, &IID_IClassFactory, (void **)&pProxy);
910 ok_ole_success(hr, CoUnmarshalInterface);
911 IStream_Release(ncu_params->stream);
912
913 ok_more_than_one_lock();
914
915 /* die without calling CoUninitialize */
916
917 return 0;
918 }
919
920 /* tests STA -> STA call during DLL_THREAD_DETACH doesn't deadlock */
921 static void test_no_couninitialize_client(void)
922 {
923 HRESULT hr;
924 IStream *pStream = NULL;
925 DWORD tid;
926 DWORD host_tid;
927 HANDLE thread;
928 HANDLE host_thread;
929 struct ncu_params ncu_params;
930
931 cLocks = 0;
932
933 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
934 ok_ole_success(hr, CreateStreamOnHGlobal);
935 ncu_params.stream = pStream;
936
937 /* NOTE: assumes start_host_object uses an STA to host the object, as MTAs
938 * always deadlock when called from within DllMain */
939 host_tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown *)&Test_ClassFactory, MSHLFLAGS_NORMAL, &host_thread);
940 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
941
942 ok_more_than_one_lock();
943
944 thread = CreateThread(NULL, 0, no_couninitialize_client_proc, &ncu_params, 0, &tid);
945
946 ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
947 CloseHandle(thread);
948
949 ok_no_locks();
950
951 end_host_object(host_tid, host_thread);
952 }
953
954 /* tests success case of a same-thread table-weak marshal, unmarshal, unmarshal */
955 static void test_tableweak_marshal_and_unmarshal_twice(void)
956 {
957 HRESULT hr;
958 IStream *pStream = NULL;
959 IUnknown *pProxy1 = NULL;
960 IUnknown *pProxy2 = NULL;
961 DWORD tid;
962 HANDLE thread;
963
964 cLocks = 0;
965
966 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
967 ok_ole_success(hr, CreateStreamOnHGlobal);
968 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_TABLEWEAK, &thread);
969
970 ok_more_than_one_lock();
971
972 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
973 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy1);
974 ok_ole_success(hr, CoUnmarshalInterface);
975
976 ok_more_than_one_lock();
977
978 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
979 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
980 IStream_Release(pStream);
981 ok_ole_success(hr, CoUnmarshalInterface);
982
983 ok_more_than_one_lock();
984
985 IUnknown_Release(pProxy1);
986 IUnknown_Release(pProxy2);
987
988 /* this line is shows the difference between weak and strong table marshaling:
989 * weak has cLocks == 0
990 * strong has cLocks > 0 */
991 ok_no_locks();
992
993 end_host_object(tid, thread);
994 }
995
996 /* tests releasing after unmarshaling one object */
997 static void test_tableweak_marshal_releasedata1(void)
998 {
999 HRESULT hr;
1000 IStream *pStream = NULL;
1001 IUnknown *pProxy1 = NULL;
1002 IUnknown *pProxy2 = NULL;
1003 DWORD tid;
1004 HANDLE thread;
1005
1006 cLocks = 0;
1007
1008 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1009 ok_ole_success(hr, CreateStreamOnHGlobal);
1010 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_TABLEWEAK, &thread);
1011
1012 ok_more_than_one_lock();
1013
1014 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1015 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy1);
1016 ok_ole_success(hr, CoUnmarshalInterface);
1017
1018 ok_more_than_one_lock();
1019
1020 /* release the remaining reference on the object by calling
1021 * CoReleaseMarshalData in the hosting thread */
1022 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1023 release_host_object(tid);
1024
1025 ok_more_than_one_lock();
1026
1027 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1028 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
1029 ok_ole_success(hr, CoUnmarshalInterface);
1030 IStream_Release(pStream);
1031
1032 ok_more_than_one_lock();
1033
1034 IUnknown_Release(pProxy1);
1035 if (pProxy2)
1036 IUnknown_Release(pProxy2);
1037
1038 /* this line is shows the difference between weak and strong table marshaling:
1039 * weak has cLocks == 0
1040 * strong has cLocks > 0 */
1041 ok_no_locks();
1042
1043 end_host_object(tid, thread);
1044 }
1045
1046 /* tests releasing after unmarshaling one object */
1047 static void test_tableweak_marshal_releasedata2(void)
1048 {
1049 HRESULT hr;
1050 IStream *pStream = NULL;
1051 IUnknown *pProxy = NULL;
1052 DWORD tid;
1053 HANDLE thread;
1054
1055 cLocks = 0;
1056
1057 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1058 ok_ole_success(hr, CreateStreamOnHGlobal);
1059 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_TABLEWEAK, &thread);
1060
1061 ok_more_than_one_lock();
1062
1063 /* release the remaining reference on the object by calling
1064 * CoReleaseMarshalData in the hosting thread */
1065 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1066 release_host_object(tid);
1067
1068 ok_no_locks();
1069
1070 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1071 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
1072 todo_wine
1073 {
1074 ok(hr == CO_E_OBJNOTREG,
1075 "CoUnmarshalInterface should have failed with CO_E_OBJNOTREG, but returned 0x%08x instead\n",
1076 hr);
1077 }
1078 IStream_Release(pStream);
1079
1080 ok_no_locks();
1081
1082 end_host_object(tid, thread);
1083 }
1084
1085 struct weak_and_normal_marshal_data
1086 {
1087 IStream *pStreamWeak;
1088 IStream *pStreamNormal;
1089 HANDLE hReadyEvent;
1090 HANDLE hQuitEvent;
1091 };
1092
1093 static DWORD CALLBACK weak_and_normal_marshal_thread_proc(void *p)
1094 {
1095 HRESULT hr;
1096 struct weak_and_normal_marshal_data *data = p;
1097 HANDLE hQuitEvent = data->hQuitEvent;
1098 MSG msg;
1099
1100 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1101
1102 hr = CoMarshalInterface(data->pStreamWeak, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_TABLEWEAK);
1103 ok_ole_success(hr, "CoMarshalInterface");
1104
1105 hr = CoMarshalInterface(data->pStreamNormal, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1106 ok_ole_success(hr, "CoMarshalInterface");
1107
1108 /* force the message queue to be created before signaling parent thread */
1109 PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
1110
1111 SetEvent(data->hReadyEvent);
1112
1113 while (WAIT_OBJECT_0 + 1 == MsgWaitForMultipleObjects(1, &hQuitEvent, FALSE, 10000, QS_ALLINPUT))
1114 {
1115 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
1116 DispatchMessage(&msg);
1117 }
1118 CloseHandle(hQuitEvent);
1119
1120 CoUninitialize();
1121
1122 return 0;
1123 }
1124
1125 /* tests interaction between table-weak and normal marshalling of an object */
1126 static void test_tableweak_and_normal_marshal_and_unmarshal(void)
1127 {
1128 HRESULT hr;
1129 IUnknown *pProxyWeak = NULL;
1130 IUnknown *pProxyNormal = NULL;
1131 DWORD tid;
1132 HANDLE thread;
1133 struct weak_and_normal_marshal_data data;
1134
1135 cLocks = 0;
1136
1137 data.hReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
1138 data.hQuitEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
1139 hr = CreateStreamOnHGlobal(NULL, TRUE, &data.pStreamWeak);
1140 ok_ole_success(hr, CreateStreamOnHGlobal);
1141 hr = CreateStreamOnHGlobal(NULL, TRUE, &data.pStreamNormal);
1142 ok_ole_success(hr, CreateStreamOnHGlobal);
1143
1144 thread = CreateThread(NULL, 0, weak_and_normal_marshal_thread_proc, &data, 0, &tid);
1145 ok( !WaitForSingleObject(data.hReadyEvent, 10000), "wait timed out\n" );
1146 CloseHandle(data.hReadyEvent);
1147
1148 ok_more_than_one_lock();
1149
1150 IStream_Seek(data.pStreamWeak, ullZero, STREAM_SEEK_SET, NULL);
1151 hr = CoUnmarshalInterface(data.pStreamWeak, &IID_IClassFactory, (void **)&pProxyWeak);
1152 ok_ole_success(hr, CoUnmarshalInterface);
1153
1154 ok_more_than_one_lock();
1155
1156 IStream_Seek(data.pStreamNormal, ullZero, STREAM_SEEK_SET, NULL);
1157 hr = CoUnmarshalInterface(data.pStreamNormal, &IID_IClassFactory, (void **)&pProxyNormal);
1158 ok_ole_success(hr, CoUnmarshalInterface);
1159
1160 ok_more_than_one_lock();
1161
1162 IUnknown_Release(pProxyNormal);
1163
1164 ok_more_than_one_lock();
1165
1166 IUnknown_Release(pProxyWeak);
1167
1168 ok_no_locks();
1169
1170 IStream_Release(data.pStreamWeak);
1171 IStream_Release(data.pStreamNormal);
1172
1173 SetEvent(data.hQuitEvent);
1174 ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
1175 CloseHandle(thread);
1176 }
1177
1178 /* tests success case of a same-thread table-strong marshal, unmarshal, unmarshal */
1179 static void test_tablestrong_marshal_and_unmarshal_twice(void)
1180 {
1181 HRESULT hr;
1182 IStream *pStream = NULL;
1183 IUnknown *pProxy1 = NULL;
1184 IUnknown *pProxy2 = NULL;
1185 DWORD tid;
1186 HANDLE thread;
1187
1188 cLocks = 0;
1189
1190 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1191 ok_ole_success(hr, CreateStreamOnHGlobal);
1192 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_TABLESTRONG, &thread);
1193
1194 ok_more_than_one_lock();
1195
1196 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1197 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy1);
1198 ok_ole_success(hr, CoUnmarshalInterface);
1199
1200 ok_more_than_one_lock();
1201
1202 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1203 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
1204 ok_ole_success(hr, CoUnmarshalInterface);
1205
1206 ok_more_than_one_lock();
1207
1208 if (pProxy1) IUnknown_Release(pProxy1);
1209 if (pProxy2) IUnknown_Release(pProxy2);
1210
1211 /* this line is shows the difference between weak and strong table marshaling:
1212 * weak has cLocks == 0
1213 * strong has cLocks > 0 */
1214 ok_more_than_one_lock();
1215
1216 /* release the remaining reference on the object by calling
1217 * CoReleaseMarshalData in the hosting thread */
1218 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1219 release_host_object(tid);
1220 IStream_Release(pStream);
1221
1222 ok_no_locks();
1223
1224 end_host_object(tid, thread);
1225 }
1226
1227 /* tests CoLockObjectExternal */
1228 static void test_lock_object_external(void)
1229 {
1230 HRESULT hr;
1231 IStream *pStream = NULL;
1232
1233 cLocks = 0;
1234
1235 /* test the stub manager creation aspect of CoLockObjectExternal when the
1236 * object hasn't been marshaled yet */
1237 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, TRUE, TRUE);
1238
1239 ok_more_than_one_lock();
1240
1241 CoDisconnectObject((IUnknown*)&Test_ClassFactory, 0);
1242
1243 ok_no_locks();
1244
1245 /* test our empty stub manager being handled correctly in
1246 * CoMarshalInterface */
1247 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, TRUE, TRUE);
1248
1249 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1250 ok_ole_success(hr, CreateStreamOnHGlobal);
1251 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1252 ok_ole_success(hr, CoMarshalInterface);
1253
1254 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, TRUE, TRUE);
1255
1256 ok_more_than_one_lock();
1257
1258 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1259 hr = CoReleaseMarshalData(pStream);
1260 ok_ole_success(hr, CoReleaseMarshalData);
1261 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1262
1263 ok_more_than_one_lock();
1264
1265 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, FALSE, TRUE);
1266
1267 ok_more_than_one_lock();
1268
1269 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, FALSE, TRUE);
1270
1271 ok_no_locks();
1272
1273 /* test CoLockObjectExternal releases reference to object with
1274 * fLastUnlockReleases as TRUE and there are only strong references on
1275 * the object */
1276 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, TRUE, FALSE);
1277
1278 ok_more_than_one_lock();
1279
1280 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, FALSE, FALSE);
1281
1282 ok_no_locks();
1283
1284 /* test CoLockObjectExternal doesn't release the last reference to an
1285 * object with fLastUnlockReleases as TRUE and there is a weak reference
1286 * on the object */
1287 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_TABLEWEAK);
1288 ok_ole_success(hr, CoMarshalInterface);
1289
1290 ok_more_than_one_lock();
1291
1292 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, TRUE, FALSE);
1293
1294 ok_more_than_one_lock();
1295
1296 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, FALSE, FALSE);
1297
1298 ok_more_than_one_lock();
1299
1300 CoDisconnectObject((IUnknown*)&Test_ClassFactory, 0);
1301
1302 ok_no_locks();
1303
1304 IStream_Release(pStream);
1305 }
1306
1307 /* tests disconnecting stubs */
1308 static void test_disconnect_stub(void)
1309 {
1310 HRESULT hr;
1311 IStream *pStream = NULL;
1312
1313 cLocks = 0;
1314
1315 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1316 ok_ole_success(hr, CreateStreamOnHGlobal);
1317 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1318 ok_ole_success(hr, CoMarshalInterface);
1319
1320 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, TRUE, TRUE);
1321
1322 ok_more_than_one_lock();
1323
1324 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1325 hr = CoReleaseMarshalData(pStream);
1326 ok_ole_success(hr, CoReleaseMarshalData);
1327 IStream_Release(pStream);
1328
1329 ok_more_than_one_lock();
1330
1331 CoDisconnectObject((IUnknown*)&Test_ClassFactory, 0);
1332
1333 ok_no_locks();
1334 }
1335
1336 /* tests failure case of a same-thread marshal and unmarshal twice */
1337 static void test_normal_marshal_and_unmarshal_twice(void)
1338 {
1339 HRESULT hr;
1340 IStream *pStream = NULL;
1341 IUnknown *pProxy1 = NULL;
1342 IUnknown *pProxy2 = NULL;
1343
1344 cLocks = 0;
1345
1346 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1347 ok_ole_success(hr, CreateStreamOnHGlobal);
1348 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1349 ok_ole_success(hr, CoMarshalInterface);
1350
1351 ok_more_than_one_lock();
1352
1353 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1354 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy1);
1355 ok_ole_success(hr, CoUnmarshalInterface);
1356
1357 ok_more_than_one_lock();
1358
1359 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1360 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
1361 ok(hr == CO_E_OBJNOTCONNECTED,
1362 "CoUnmarshalInterface should have failed with error CO_E_OBJNOTCONNECTED for double unmarshal, instead of 0x%08x\n", hr);
1363
1364 IStream_Release(pStream);
1365
1366 ok_more_than_one_lock();
1367
1368 IUnknown_Release(pProxy1);
1369
1370 ok_no_locks();
1371 }
1372
1373 /* tests success case of marshaling and unmarshaling an HRESULT */
1374 static void test_hresult_marshaling(void)
1375 {
1376 HRESULT hr;
1377 HRESULT hr_marshaled = 0;
1378 IStream *pStream = NULL;
1379 static const HRESULT E_DEADBEEF = 0xdeadbeef;
1380
1381 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1382 ok_ole_success(hr, CreateStreamOnHGlobal);
1383
1384 hr = CoMarshalHresult(pStream, E_DEADBEEF);
1385 ok_ole_success(hr, CoMarshalHresult);
1386
1387 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1388 hr = IStream_Read(pStream, &hr_marshaled, sizeof(HRESULT), NULL);
1389 ok_ole_success(hr, IStream_Read);
1390
1391 ok(hr_marshaled == E_DEADBEEF, "Didn't marshal HRESULT as expected: got value 0x%08x instead\n", hr_marshaled);
1392
1393 hr_marshaled = 0;
1394 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1395 hr = CoUnmarshalHresult(pStream, &hr_marshaled);
1396 ok_ole_success(hr, CoUnmarshalHresult);
1397
1398 ok(hr_marshaled == E_DEADBEEF, "Didn't marshal HRESULT as expected: got value 0x%08x instead\n", hr_marshaled);
1399
1400 IStream_Release(pStream);
1401 }
1402
1403
1404 /* helper for test_proxy_used_in_wrong_thread */
1405 static DWORD CALLBACK bad_thread_proc(LPVOID p)
1406 {
1407 IClassFactory * cf = p;
1408 HRESULT hr;
1409 IUnknown * proxy = NULL;
1410
1411 hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
1412 todo_wine
1413 ok(hr == CO_E_NOTINITIALIZED,
1414 "COM should have failed with CO_E_NOTINITIALIZED on using proxy without apartment, but instead returned 0x%08x\n",
1415 hr);
1416
1417 hr = IClassFactory_QueryInterface(cf, &IID_IMultiQI, (LPVOID *)&proxy);
1418 /* Win9x returns S_OK, whilst NT returns RPC_E_WRONG_THREAD */
1419 trace("call to proxy's QueryInterface for local interface without apartment returned 0x%08x\n", hr);
1420 if (SUCCEEDED(hr))
1421 IUnknown_Release(proxy);
1422
1423 hr = IClassFactory_QueryInterface(cf, &IID_IStream, (LPVOID *)&proxy);
1424 /* Win9x returns E_NOINTERFACE, whilst NT returns RPC_E_WRONG_THREAD */
1425 trace("call to proxy's QueryInterface without apartment returned 0x%08x\n", hr);
1426 if (SUCCEEDED(hr))
1427 IUnknown_Release(proxy);
1428
1429 pCoInitializeEx(NULL, COINIT_MULTITHREADED);
1430
1431 hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
1432 if (proxy) IUnknown_Release(proxy);
1433 ok(hr == RPC_E_WRONG_THREAD,
1434 "COM should have failed with RPC_E_WRONG_THREAD on using proxy from wrong apartment, but instead returned 0x%08x\n",
1435 hr);
1436
1437 hr = IClassFactory_QueryInterface(cf, &IID_IStream, (LPVOID *)&proxy);
1438 /* Win9x returns E_NOINTERFACE, whilst NT returns RPC_E_WRONG_THREAD */
1439 trace("call to proxy's QueryInterface from wrong apartment returned 0x%08x\n", hr);
1440
1441 /* now be really bad and release the proxy from the wrong apartment */
1442 IUnknown_Release(cf);
1443
1444 CoUninitialize();
1445
1446 return 0;
1447 }
1448
1449 /* tests failure case of a using a proxy in the wrong apartment */
1450 static void test_proxy_used_in_wrong_thread(void)
1451 {
1452 HRESULT hr;
1453 IStream *pStream = NULL;
1454 IUnknown *pProxy = NULL;
1455 DWORD tid, tid2;
1456 HANDLE thread;
1457 HANDLE host_thread;
1458
1459 cLocks = 0;
1460
1461 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1462 ok_ole_success(hr, CreateStreamOnHGlobal);
1463 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &host_thread);
1464
1465 ok_more_than_one_lock();
1466
1467 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1468 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
1469 ok_ole_success(hr, CoUnmarshalInterface);
1470 IStream_Release(pStream);
1471
1472 ok_more_than_one_lock();
1473
1474 /* do a call that will fail, but result in IRemUnknown being used by the proxy */
1475 IClassFactory_QueryInterface(pProxy, &IID_IStream, (LPVOID *)&pStream);
1476
1477 /* create a thread that we can misbehave in */
1478 thread = CreateThread(NULL, 0, bad_thread_proc, pProxy, 0, &tid2);
1479
1480 ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
1481 CloseHandle(thread);
1482
1483 /* do release statement on Win9x that we should have done above */
1484 if (!GetProcAddress(GetModuleHandle("ole32"), "CoRegisterSurrogateEx"))
1485 IUnknown_Release(pProxy);
1486
1487 ok_no_locks();
1488
1489 end_host_object(tid, host_thread);
1490 }
1491
1492 static HRESULT WINAPI MessageFilter_QueryInterface(IMessageFilter *iface, REFIID riid, void ** ppvObj)
1493 {
1494 if (ppvObj == NULL) return E_POINTER;
1495
1496 if (IsEqualGUID(riid, &IID_IUnknown) ||
1497 IsEqualGUID(riid, &IID_IClassFactory))
1498 {
1499 *ppvObj = iface;
1500 IClassFactory_AddRef(iface);
1501 return S_OK;
1502 }
1503
1504 return E_NOINTERFACE;
1505 }
1506
1507 static ULONG WINAPI MessageFilter_AddRef(IMessageFilter *iface)
1508 {
1509 return 2; /* non-heap object */
1510 }
1511
1512 static ULONG WINAPI MessageFilter_Release(IMessageFilter *iface)
1513 {
1514 return 1; /* non-heap object */
1515 }
1516
1517 static DWORD WINAPI MessageFilter_HandleInComingCall(
1518 IMessageFilter *iface,
1519 DWORD dwCallType,
1520 HTASK threadIDCaller,
1521 DWORD dwTickCount,
1522 LPINTERFACEINFO lpInterfaceInfo)
1523 {
1524 static int callcount = 0;
1525 DWORD ret;
1526 trace("HandleInComingCall\n");
1527 switch (callcount)
1528 {
1529 case 0:
1530 ret = SERVERCALL_REJECTED;
1531 break;
1532 case 1:
1533 ret = SERVERCALL_RETRYLATER;
1534 break;
1535 default:
1536 ret = SERVERCALL_ISHANDLED;
1537 break;
1538 }
1539 callcount++;
1540 return ret;
1541 }
1542
1543 static DWORD WINAPI MessageFilter_RetryRejectedCall(
1544 IMessageFilter *iface,
1545 HTASK threadIDCallee,
1546 DWORD dwTickCount,
1547 DWORD dwRejectType)
1548 {
1549 trace("RetryRejectedCall\n");
1550 return 0;
1551 }
1552
1553 static DWORD WINAPI MessageFilter_MessagePending(
1554 IMessageFilter *iface,
1555 HTASK threadIDCallee,
1556 DWORD dwTickCount,
1557 DWORD dwPendingType)
1558 {
1559 trace("MessagePending\n");
1560 return PENDINGMSG_WAITNOPROCESS;
1561 }
1562
1563 static const IMessageFilterVtbl MessageFilter_Vtbl =
1564 {
1565 MessageFilter_QueryInterface,
1566 MessageFilter_AddRef,
1567 MessageFilter_Release,
1568 MessageFilter_HandleInComingCall,
1569 MessageFilter_RetryRejectedCall,
1570 MessageFilter_MessagePending
1571 };
1572
1573 static IMessageFilter MessageFilter = { &MessageFilter_Vtbl };
1574
1575 static void test_message_filter(void)
1576 {
1577 HRESULT hr;
1578 IStream *pStream = NULL;
1579 IClassFactory *cf = NULL;
1580 DWORD tid;
1581 IUnknown *proxy = NULL;
1582 IMessageFilter *prev_filter = NULL;
1583 HANDLE thread;
1584
1585 cLocks = 0;
1586
1587 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1588 ok_ole_success(hr, CreateStreamOnHGlobal);
1589 tid = start_host_object2(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &MessageFilter, &thread);
1590
1591 ok_more_than_one_lock();
1592
1593 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1594 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&cf);
1595 ok_ole_success(hr, CoUnmarshalInterface);
1596 IStream_Release(pStream);
1597
1598 ok_more_than_one_lock();
1599
1600 hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
1601 ok(hr == RPC_E_CALL_REJECTED, "Call should have returned RPC_E_CALL_REJECTED, but return 0x%08x instead\n", hr);
1602 if (proxy) IUnknown_Release(proxy);
1603 proxy = NULL;
1604
1605 hr = CoRegisterMessageFilter(&MessageFilter, &prev_filter);
1606 ok_ole_success(hr, CoRegisterMessageFilter);
1607
1608 hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
1609 ok_ole_success(hr, IClassFactory_CreateInstance);
1610
1611 IUnknown_Release(proxy);
1612
1613 IClassFactory_Release(cf);
1614
1615 ok_no_locks();
1616
1617 end_host_object(tid, thread);
1618
1619 hr = CoRegisterMessageFilter(prev_filter, NULL);
1620 ok_ole_success(hr, CoRegisterMessageFilter);
1621 }
1622
1623 /* test failure case of trying to unmarshal from bad stream */
1624 static void test_bad_marshal_stream(void)
1625 {
1626 HRESULT hr;
1627 IStream *pStream = NULL;
1628
1629 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1630 ok_ole_success(hr, CreateStreamOnHGlobal);
1631 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1632 ok_ole_success(hr, CoMarshalInterface);
1633
1634 ok_more_than_one_lock();
1635
1636 /* try to read beyond end of stream */
1637 hr = CoReleaseMarshalData(pStream);
1638 ok(hr == STG_E_READFAULT, "Should have failed with STG_E_READFAULT, but returned 0x%08x instead\n", hr);
1639
1640 /* now release for real */
1641 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1642 hr = CoReleaseMarshalData(pStream);
1643 ok_ole_success(hr, CoReleaseMarshalData);
1644
1645 IStream_Release(pStream);
1646 }
1647
1648 /* tests that proxies implement certain interfaces */
1649 static void test_proxy_interfaces(void)
1650 {
1651 HRESULT hr;
1652 IStream *pStream = NULL;
1653 IUnknown *pProxy = NULL;
1654 IUnknown *pOtherUnknown = NULL;
1655 DWORD tid;
1656 HANDLE thread;
1657
1658 cLocks = 0;
1659
1660 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1661 ok_ole_success(hr, CreateStreamOnHGlobal);
1662 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
1663
1664 ok_more_than_one_lock();
1665
1666 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1667 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)&pProxy);
1668 ok_ole_success(hr, CoUnmarshalInterface);
1669 IStream_Release(pStream);
1670
1671 ok_more_than_one_lock();
1672
1673 hr = IUnknown_QueryInterface(pProxy, &IID_IUnknown, (LPVOID*)&pOtherUnknown);
1674 ok_ole_success(hr, IUnknown_QueryInterface IID_IUnknown);
1675 if (hr == S_OK) IUnknown_Release(pOtherUnknown);
1676
1677 hr = IUnknown_QueryInterface(pProxy, &IID_IClientSecurity, (LPVOID*)&pOtherUnknown);
1678 ok_ole_success(hr, IUnknown_QueryInterface IID_IClientSecurity);
1679 if (hr == S_OK) IUnknown_Release(pOtherUnknown);
1680
1681 hr = IUnknown_QueryInterface(pProxy, &IID_IMultiQI, (LPVOID*)&pOtherUnknown);
1682 ok_ole_success(hr, IUnknown_QueryInterface IID_IMultiQI);
1683 if (hr == S_OK) IUnknown_Release(pOtherUnknown);
1684
1685 hr = IUnknown_QueryInterface(pProxy, &IID_IMarshal, (LPVOID*)&pOtherUnknown);
1686 ok_ole_success(hr, IUnknown_QueryInterface IID_IMarshal);
1687 if (hr == S_OK) IUnknown_Release(pOtherUnknown);
1688
1689 /* IMarshal2 is also supported on NT-based systems, but is pretty much
1690 * useless as it has no more methods over IMarshal that it inherits from. */
1691
1692 IUnknown_Release(pProxy);
1693
1694 ok_no_locks();
1695
1696 end_host_object(tid, thread);
1697 }
1698
1699 typedef struct
1700 {
1701 IUnknown IUnknown_iface;
1702 ULONG refs;
1703 } HeapUnknown;
1704
1705 static inline HeapUnknown *impl_from_IUnknown(IUnknown *iface)
1706 {
1707 return CONTAINING_RECORD(iface, HeapUnknown, IUnknown_iface);
1708 }
1709
1710 static HRESULT WINAPI HeapUnknown_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
1711 {
1712 if (IsEqualIID(riid, &IID_IUnknown))
1713 {
1714 IUnknown_AddRef(iface);
1715 *ppv = iface;
1716 return S_OK;
1717 }
1718 *ppv = NULL;
1719 return E_NOINTERFACE;
1720 }
1721
1722 static ULONG WINAPI HeapUnknown_AddRef(IUnknown *iface)
1723 {
1724 HeapUnknown *This = impl_from_IUnknown(iface);
1725 return InterlockedIncrement((LONG*)&This->refs);
1726 }
1727
1728 static ULONG WINAPI HeapUnknown_Release(IUnknown *iface)
1729 {
1730 HeapUnknown *This = impl_from_IUnknown(iface);
1731 ULONG refs = InterlockedDecrement((LONG*)&This->refs);
1732 if (!refs) HeapFree(GetProcessHeap(), 0, This);
1733 return refs;
1734 }
1735
1736 static const IUnknownVtbl HeapUnknown_Vtbl =
1737 {
1738 HeapUnknown_QueryInterface,
1739 HeapUnknown_AddRef,
1740 HeapUnknown_Release
1741 };
1742
1743 static void test_proxybuffer(REFIID riid)
1744 {
1745 HRESULT hr;
1746 IPSFactoryBuffer *psfb;
1747 IRpcProxyBuffer *proxy;
1748 LPVOID lpvtbl;
1749 ULONG refs;
1750 CLSID clsid;
1751 HeapUnknown *pUnkOuter = HeapAlloc(GetProcessHeap(), 0, sizeof(*pUnkOuter));
1752
1753 pUnkOuter->IUnknown_iface.lpVtbl = &HeapUnknown_Vtbl;
1754 pUnkOuter->refs = 1;
1755
1756 hr = CoGetPSClsid(riid, &clsid);
1757 ok_ole_success(hr, CoGetPSClsid);
1758
1759 hr = CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL, &IID_IPSFactoryBuffer, (LPVOID*)&psfb);
1760 ok_ole_success(hr, CoGetClassObject);
1761
1762 hr = IPSFactoryBuffer_CreateProxy(psfb, &pUnkOuter->IUnknown_iface, riid, &proxy, &lpvtbl);
1763 ok_ole_success(hr, IPSFactoryBuffer_CreateProxy);
1764 ok(lpvtbl != NULL, "IPSFactoryBuffer_CreateProxy succeeded, but returned a NULL vtable!\n");
1765
1766 /* release our reference to the outer unknown object - the PS factory
1767 * buffer will have AddRef's it in the CreateProxy call */
1768 refs = IUnknown_Release(&pUnkOuter->IUnknown_iface);
1769 ok(refs == 1, "Ref count of outer unknown should have been 1 instead of %d\n", refs);
1770
1771 /* Not checking return, unreliable on native. Maybe it leaks references? */
1772 IPSFactoryBuffer_Release(psfb);
1773
1774 refs = IUnknown_Release((IUnknown *)lpvtbl);
1775 ok(refs == 0, "Ref-count leak of %d on IRpcProxyBuffer\n", refs);
1776
1777 refs = IRpcProxyBuffer_Release(proxy);
1778 ok(refs == 0, "Ref-count leak of %d on IRpcProxyBuffer\n", refs);
1779 }
1780
1781 static void test_stubbuffer(REFIID riid)
1782 {
1783 HRESULT hr;
1784 IPSFactoryBuffer *psfb;
1785 IRpcStubBuffer *stub;
1786 ULONG refs;
1787 CLSID clsid;
1788
1789 cLocks = 0;
1790
1791 hr = CoGetPSClsid(riid, &clsid);
1792 ok_ole_success(hr, CoGetPSClsid);
1793
1794 hr = CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL, &IID_IPSFactoryBuffer, (LPVOID*)&psfb);
1795 ok_ole_success(hr, CoGetClassObject);
1796
1797 hr = IPSFactoryBuffer_CreateStub(psfb, riid, (IUnknown*)&Test_ClassFactory, &stub);
1798 ok_ole_success(hr, IPSFactoryBuffer_CreateStub);
1799
1800 /* Not checking return, unreliable on native. Maybe it leaks references? */
1801 IPSFactoryBuffer_Release(psfb);
1802
1803 ok_more_than_one_lock();
1804
1805 IRpcStubBuffer_Disconnect(stub);
1806
1807 ok_no_locks();
1808
1809 refs = IRpcStubBuffer_Release(stub);
1810 ok(refs == 0, "Ref-count leak of %d on IRpcProxyBuffer\n", refs);
1811 }
1812
1813 static HWND hwnd_app;
1814
1815 static HRESULT WINAPI TestRE_IClassFactory_CreateInstance(
1816 LPCLASSFACTORY iface,
1817 LPUNKNOWN pUnkOuter,
1818 REFIID riid,
1819 LPVOID *ppvObj)
1820 {
1821 DWORD_PTR res;
1822 if (IsEqualIID(riid, &IID_IWineTest))
1823 {
1824 BOOL ret = SendMessageTimeout(hwnd_app, WM_NULL, 0, 0, SMTO_BLOCK, 5000, &res);
1825 ok(ret, "Timed out sending a message to originating window during RPC call\n");
1826 }
1827 *ppvObj = NULL;
1828 return S_FALSE;
1829 }
1830
1831 static const IClassFactoryVtbl TestREClassFactory_Vtbl =
1832 {
1833 Test_IClassFactory_QueryInterface,
1834 Test_IClassFactory_AddRef,
1835 Test_IClassFactory_Release,
1836 TestRE_IClassFactory_CreateInstance,
1837 Test_IClassFactory_LockServer
1838 };
1839
1840 static IClassFactory TestRE_ClassFactory = { &TestREClassFactory_Vtbl };
1841
1842 static LRESULT CALLBACK window_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1843 {
1844 switch (msg)
1845 {
1846 case WM_USER:
1847 {
1848 HRESULT hr;
1849 IStream *pStream = NULL;
1850 IClassFactory *proxy = NULL;
1851 IUnknown *object;
1852 DWORD tid;
1853 HANDLE thread;
1854
1855 cLocks = 0;
1856
1857 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1858 ok_ole_success(hr, CreateStreamOnHGlobal);
1859 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&TestRE_ClassFactory, MSHLFLAGS_NORMAL, &thread);
1860
1861 ok_more_than_one_lock();
1862
1863 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1864 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&proxy);
1865 ok_ole_success(hr, CoReleaseMarshalData);
1866 IStream_Release(pStream);
1867
1868 ok_more_than_one_lock();
1869
1870 /* note the use of the magic IID_IWineTest value to tell remote thread
1871 * to try to send a message back to us */
1872 hr = IClassFactory_CreateInstance(proxy, NULL, &IID_IWineTest, (void **)&object);
1873 ok(hr == S_FALSE, "expected S_FALSE, got %d\n", hr);
1874
1875 IClassFactory_Release(proxy);
1876
1877 ok_no_locks();
1878
1879 end_host_object(tid, thread);
1880
1881 PostMessage(hwnd, WM_QUIT, 0, 0);
1882
1883 return 0;
1884 }
1885 case WM_USER+1:
1886 {
1887 HRESULT hr;
1888 IStream *pStream = NULL;
1889 IClassFactory *proxy = NULL;
1890 IUnknown *object;
1891 DWORD tid;
1892 HANDLE thread;
1893
1894 cLocks = 0;
1895
1896 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1897 ok_ole_success(hr, CreateStreamOnHGlobal);
1898 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&TestRE_ClassFactory, MSHLFLAGS_NORMAL, &thread);
1899
1900 ok_more_than_one_lock();
1901
1902 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1903 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&proxy);
1904 ok_ole_success(hr, CoReleaseMarshalData);
1905 IStream_Release(pStream);
1906
1907 ok_more_than_one_lock();
1908
1909 /* post quit message before a doing a COM call to show that a pending
1910 * WM_QUIT message doesn't stop the call from succeeding */
1911 PostMessage(hwnd, WM_QUIT, 0, 0);
1912 hr = IClassFactory_CreateInstance(proxy, NULL, &IID_IUnknown, (void **)&object);
1913 ok(hr == S_FALSE, "IClassFactory_CreateInstance returned 0x%08x, expected S_FALSE\n", hr);
1914
1915 IClassFactory_Release(proxy);
1916
1917 ok_no_locks();
1918
1919 end_host_object(tid, thread);
1920
1921 return 0;
1922 }
1923 case WM_USER+2:
1924 {
1925 HRESULT hr;
1926 IStream *pStream = NULL;
1927 IClassFactory *proxy = NULL;
1928 IUnknown *object;
1929 DWORD tid;
1930 HANDLE thread;
1931
1932 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1933 ok_ole_success(hr, CreateStreamOnHGlobal);
1934 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
1935
1936 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1937 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&proxy);
1938 ok_ole_success(hr, CoReleaseMarshalData);
1939 IStream_Release(pStream);
1940
1941 /* shows that COM calls executed during the processing of sent
1942 * messages should fail */
1943 hr = IClassFactory_CreateInstance(proxy, NULL, &IID_IUnknown, (void **)&object);
1944 ok(hr == RPC_E_CANTCALLOUT_ININPUTSYNCCALL,
1945 "COM call during processing of sent message should return RPC_E_CANTCALLOUT_ININPUTSYNCCALL instead of 0x%08x\n", hr);
1946
1947 IClassFactory_Release(proxy);
1948
1949 end_host_object(tid, thread);
1950
1951 PostQuitMessage(0);
1952
1953 return 0;
1954 }
1955 default:
1956 return DefWindowProc(hwnd, msg, wparam, lparam);
1957 }
1958 }
1959
1960 static void register_test_window(void)
1961 {
1962 WNDCLASS wndclass;
1963
1964 memset(&wndclass, 0, sizeof(wndclass));
1965 wndclass.lpfnWndProc = window_proc;
1966 wndclass.lpszClassName = "WineCOMTest";
1967 RegisterClass(&wndclass);
1968 }
1969
1970 static void test_message_reentrancy(void)
1971 {
1972 MSG msg;
1973
1974 hwnd_app = CreateWindow("WineCOMTest", NULL, 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, NULL, 0);
1975 ok(hwnd_app != NULL, "Window creation failed\n");
1976
1977 /* start message re-entrancy test */
1978 PostMessage(hwnd_app, WM_USER, 0, 0);
1979
1980 while (GetMessage(&msg, NULL, 0, 0))
1981 {
1982 TranslateMessage(&msg);
1983 DispatchMessage(&msg);
1984 }
1985 DestroyWindow(hwnd_app);
1986 }
1987
1988 static HRESULT WINAPI TestMsg_IClassFactory_CreateInstance(
1989 LPCLASSFACTORY iface,
1990 LPUNKNOWN pUnkOuter,
1991 REFIID riid,
1992 LPVOID *ppvObj)
1993 {
1994 *ppvObj = NULL;
1995 SendMessage(hwnd_app, WM_USER+2, 0, 0);
1996 return S_OK;
1997 }
1998
1999 static IClassFactoryVtbl TestMsgClassFactory_Vtbl =
2000 {
2001 Test_IClassFactory_QueryInterface,
2002 Test_IClassFactory_AddRef,
2003 Test_IClassFactory_Release,
2004 TestMsg_IClassFactory_CreateInstance,
2005 Test_IClassFactory_LockServer
2006 };
2007
2008 static IClassFactory TestMsg_ClassFactory = { &TestMsgClassFactory_Vtbl };
2009
2010 static void test_call_from_message(void)
2011 {
2012 MSG msg;
2013 IStream *pStream;
2014 HRESULT hr;
2015 IClassFactory *proxy;
2016 DWORD tid;
2017 HANDLE thread;
2018 IUnknown *object;
2019
2020 hwnd_app = CreateWindow("WineCOMTest", NULL, 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, NULL, 0);
2021 ok(hwnd_app != NULL, "Window creation failed\n");
2022
2023 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
2024 ok_ole_success(hr, CreateStreamOnHGlobal);
2025 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&TestMsg_ClassFactory, MSHLFLAGS_NORMAL, &thread);
2026
2027 ok_more_than_one_lock();
2028
2029 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
2030 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&proxy);
2031 ok_ole_success(hr, CoReleaseMarshalData);
2032 IStream_Release(pStream);
2033
2034 ok_more_than_one_lock();
2035
2036 /* start message re-entrancy test */
2037 hr = IClassFactory_CreateInstance(proxy, NULL, &IID_IUnknown, (void **)&object);
2038 ok_ole_success(hr, IClassFactory_CreateInstance);
2039
2040 IClassFactory_Release(proxy);
2041
2042 ok_no_locks();
2043
2044 end_host_object(tid, thread);
2045
2046 while (GetMessage(&msg, NULL, 0, 0))
2047 {
2048 TranslateMessage(&msg);
2049 DispatchMessage(&msg);
2050 }
2051 DestroyWindow(hwnd_app);
2052 }
2053
2054 static void test_WM_QUIT_handling(void)
2055 {
2056 MSG msg;
2057
2058 hwnd_app = CreateWindow("WineCOMTest", NULL, 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, NULL, 0);
2059 ok(hwnd_app != NULL, "Window creation failed\n");
2060
2061 /* start WM_QUIT handling test */
2062 PostMessage(hwnd_app, WM_USER+1, 0, 0);
2063
2064 while (GetMessage(&msg, NULL, 0, 0))
2065 {
2066 TranslateMessage(&msg);
2067 DispatchMessage(&msg);
2068 }
2069 }
2070
2071 static SIZE_T round_global_size(SIZE_T size)
2072 {
2073 static SIZE_T global_size_alignment = -1;
2074 if (global_size_alignment == -1)
2075 {
2076 void *p = GlobalAlloc(GMEM_FIXED, 1);
2077 global_size_alignment = GlobalSize(p);
2078 GlobalFree(p);
2079 }
2080
2081 return ((size + global_size_alignment - 1) & ~(global_size_alignment - 1));
2082 }
2083
2084 static void test_freethreadedmarshaldata(IStream *pStream, MSHCTX mshctx, void *ptr, DWORD mshlflags)
2085 {
2086 HGLOBAL hglobal;
2087 DWORD size;
2088 char *marshal_data;
2089 HRESULT hr;
2090
2091 hr = GetHGlobalFromStream(pStream, &hglobal);
2092 ok_ole_success(hr, GetHGlobalFromStream);
2093
2094 size = GlobalSize(hglobal);
2095
2096 marshal_data = GlobalLock(hglobal);
2097
2098 if (mshctx == MSHCTX_INPROC)
2099 {
2100 DWORD expected_size = round_global_size(3*sizeof(DWORD) + sizeof(GUID));
2101 ok(size == expected_size ||
2102 broken(size == (2*sizeof(DWORD))) /* Win9x & NT4 */,
2103 "size should have been %d instead of %d\n", expected_size, size);
2104
2105 ok(*(DWORD *)marshal_data == mshlflags, "expected 0x%x, but got 0x%x for mshctx\n", mshlflags, *(DWORD *)marshal_data);
2106 marshal_data += sizeof(DWORD);
2107 ok(*(void **)marshal_data == ptr, "expected %p, but got %p for mshctx\n", ptr, *(void **)marshal_data);
2108 marshal_data += sizeof(void *);
2109 if (sizeof(void*) == 4 && size >= 3*sizeof(DWORD))
2110 {
2111 ok(*(DWORD *)marshal_data == 0, "expected 0x0, but got 0x%x\n", *(DWORD *)marshal_data);
2112 marshal_data += sizeof(DWORD);
2113 }
2114 if (size >= 3*sizeof(DWORD) + sizeof(GUID))
2115 {
2116 trace("got guid data: {%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\n",
2117 ((GUID *)marshal_data)->Data1, ((GUID *)marshal_data)->Data2, ((GUID *)marshal_data)->Data3,
2118 ((GUID *)marshal_data)->Data4[0], ((GUID *)marshal_data)->Data4[1], ((GUID *)marshal_data)->Data4[2], ((GUID *)marshal_data)->Data4[3],
2119 ((GUID *)marshal_data)->Data4[4], ((GUID *)marshal_data)->Data4[5], ((GUID *)marshal_data)->Data4[6], ((GUID *)marshal_data)->Data4[7]);
2120 }
2121 }
2122 else
2123 {
2124 ok(size > sizeof(DWORD), "size should have been > sizeof(DWORD), not %d\n", size);
2125 ok(*(DWORD *)marshal_data == 0x574f454d /* MEOW */,
2126 "marshal data should be filled by standard marshal and start with MEOW signature\n");
2127 }
2128
2129 GlobalUnlock(hglobal);
2130 }
2131
2132 static void test_freethreadedmarshaler(void)
2133 {
2134 HRESULT hr;
2135 IUnknown *pFTUnknown;
2136 IMarshal *pFTMarshal;
2137 IStream *pStream;
2138 IUnknown *pProxy;
2139 static const LARGE_INTEGER llZero;
2140
2141 cLocks = 0;
2142 hr = CoCreateFreeThreadedMarshaler(NULL, &pFTUnknown);
2143 ok_ole_success(hr, CoCreateFreeThreadedMarshaler);
2144 hr = IUnknown_QueryInterface(pFTUnknown, &IID_IMarshal, (void **)&pFTMarshal);
2145 ok_ole_success(hr, IUnknown_QueryInterface);
2146 IUnknown_Release(pFTUnknown);
2147
2148 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
2149 ok_ole_success(hr, CreateStreamOnHGlobal);
2150
2151 /* inproc normal marshaling */
2152
2153 hr = IMarshal_MarshalInterface(pFTMarshal, pStream, &IID_IClassFactory,
2154 &Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
2155 ok_ole_success(hr, IMarshal_MarshalInterface);
2156
2157 ok_more_than_one_lock();
2158
2159 test_freethreadedmarshaldata(pStream, MSHCTX_INPROC, &Test_ClassFactory, MSHLFLAGS_NORMAL);
2160
2161 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2162 hr = IMarshal_UnmarshalInterface(pFTMarshal, pStream, &IID_IUnknown, (void **)&pProxy);
2163 ok_ole_success(hr, IMarshal_UnmarshalInterface);
2164
2165 IUnknown_Release(pProxy);
2166
2167 ok_no_locks();
2168
2169 /* native doesn't allow us to unmarshal or release the stream data,
2170 * presumably because it wants us to call CoMarshalInterface instead */
2171 if (0)
2172 {
2173 /* local normal marshaling */
2174
2175 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2176 hr = IMarshal_MarshalInterface(pFTMarshal, pStream, &IID_IClassFactory, &Test_ClassFactory, MSHCTX_LOCAL, NULL, MSHLFLAGS_NORMAL);
2177 ok_ole_success(hr, IMarshal_MarshalInterface);
2178
2179 ok_more_than_one_lock();
2180
2181 test_freethreadedmarshaldata(pStream, MSHCTX_LOCAL, &Test_ClassFactory, MSHLFLAGS_NORMAL);
2182
2183 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2184 hr = IMarshal_ReleaseMarshalData(pFTMarshal, pStream);
2185 ok_ole_success(hr, IMarshal_ReleaseMarshalData);
2186
2187 ok_no_locks();
2188 }
2189
2190 /* inproc table-strong marshaling */
2191
2192 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2193 hr = IMarshal_MarshalInterface(pFTMarshal, pStream, &IID_IClassFactory,
2194 (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, (void *)0xdeadbeef,
2195 MSHLFLAGS_TABLESTRONG);
2196 ok_ole_success(hr, IMarshal_MarshalInterface);
2197
2198 ok_more_than_one_lock();
2199
2200 test_freethreadedmarshaldata(pStream, MSHCTX_INPROC, &Test_ClassFactory, MSHLFLAGS_TABLESTRONG);
2201
2202 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2203 hr = IMarshal_UnmarshalInterface(pFTMarshal, pStream, &IID_IUnknown, (void **)&pProxy);
2204 ok_ole_success(hr, IMarshal_UnmarshalInterface);
2205
2206 IUnknown_Release(pProxy);
2207
2208 ok_more_than_one_lock();
2209
2210 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2211 hr = IMarshal_ReleaseMarshalData(pFTMarshal, pStream);
2212 ok_ole_success(hr, IMarshal_ReleaseMarshalData);
2213
2214 ok_no_locks();
2215
2216 /* inproc table-weak marshaling */
2217
2218 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2219 hr = IMarshal_MarshalInterface(pFTMarshal, pStream, &IID_IClassFactory,
2220 (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, (void *)0xdeadbeef,
2221 MSHLFLAGS_TABLEWEAK);
2222 ok_ole_success(hr, IMarshal_MarshalInterface);
2223
2224 ok_no_locks();
2225
2226 test_freethreadedmarshaldata(pStream, MSHCTX_INPROC, &Test_ClassFactory, MSHLFLAGS_TABLEWEAK);
2227
2228 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2229 hr = IMarshal_UnmarshalInterface(pFTMarshal, pStream, &IID_IUnknown, (void **)&pProxy);
2230 ok_ole_success(hr, IMarshal_UnmarshalInterface);
2231
2232 ok_more_than_one_lock();
2233
2234 IUnknown_Release(pProxy);
2235
2236 ok_no_locks();
2237
2238 /* inproc normal marshaling (for extraordinary cases) */
2239
2240 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2241 hr = IMarshal_MarshalInterface(pFTMarshal, pStream, &IID_IClassFactory,
2242 &Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
2243 ok_ole_success(hr, IMarshal_MarshalInterface);
2244
2245 ok_more_than_one_lock();
2246
2247 /* this call shows that DisconnectObject does nothing */
2248 hr = IMarshal_DisconnectObject(pFTMarshal, 0);
2249 ok_ole_success(hr, IMarshal_DisconnectObject);
2250
2251 ok_more_than_one_lock();
2252
2253 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2254 hr = IMarshal_ReleaseMarshalData(pFTMarshal, pStream);
2255 ok_ole_success(hr, IMarshal_ReleaseMarshalData);
2256
2257 ok_no_locks();
2258
2259 /* doesn't enforce marshaling rules here and allows us to unmarshal the
2260 * interface, even though it was freed above */
2261 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2262 hr = IMarshal_UnmarshalInterface(pFTMarshal, pStream, &IID_IUnknown, (void **)&pProxy);
2263 ok_ole_success(hr, IMarshal_UnmarshalInterface);
2264
2265 ok_no_locks();
2266
2267 IStream_Release(pStream);
2268 IMarshal_Release(pFTMarshal);
2269 }
2270
2271 static HRESULT reg_unreg_wine_test_class(BOOL Register)
2272 {
2273 HRESULT hr;
2274 char buffer[256];
2275 LPOLESTR pszClsid;
2276 HKEY hkey;
2277 DWORD dwDisposition;
2278 DWORD error;
2279
2280 hr = StringFromCLSID(&CLSID_WineTest, &pszClsid);
2281 ok_ole_success(hr, "StringFromCLSID");
2282 strcpy(buffer, "CLSID\\");
2283 WideCharToMultiByte(CP_ACP, 0, pszClsid, -1, buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), NULL, NULL);
2284 CoTaskMemFree(pszClsid);
2285 strcat(buffer, "\\InprocHandler32");
2286 if (Register)
2287 {
2288 error = RegCreateKeyEx(HKEY_CLASSES_ROOT, buffer, 0, NULL, 0, KEY_SET_VALUE, NULL, &hkey, &dwDisposition);
2289 if (error == ERROR_ACCESS_DENIED)
2290 {
2291 skip("Not authorized to modify the Classes key\n");
2292 return E_FAIL;
2293 }
2294 ok(error == ERROR_SUCCESS, "RegCreateKeyEx failed with error %d\n", error);
2295 if (error != ERROR_SUCCESS) hr = E_FAIL;
2296 error = RegSetValueEx(hkey, NULL, 0, REG_SZ, (const unsigned char *)"\"ole32.dll\"", strlen("\"ole32.dll\"") + 1);
2297 ok(error == ERROR_SUCCESS, "RegSetValueEx failed with error %d\n", error);
2298 if (error != ERROR_SUCCESS) hr = E_FAIL;
2299 RegCloseKey(hkey);
2300 }
2301 else
2302 {
2303 RegDeleteKey(HKEY_CLASSES_ROOT, buffer);
2304 *strrchr(buffer, '\\') = '\0';
2305 RegDeleteKey(HKEY_CLASSES_ROOT, buffer);
2306 }
2307 return hr;
2308 }
2309
2310 static void test_inproc_handler(void)
2311 {
2312 HRESULT hr;
2313 IUnknown *pObject;
2314 IUnknown *pObject2;
2315
2316 if (FAILED(reg_unreg_wine_test_class(TRUE)))
2317 return;
2318
2319 hr = CoCreateInstance(&CLSID_WineTest, NULL, CLSCTX_INPROC_HANDLER, &IID_IUnknown, (void **)&pObject);
2320 ok_ole_success(hr, "CoCreateInstance");
2321
2322 if (SUCCEEDED(hr))
2323 {
2324 hr = IUnknown_QueryInterface(pObject, &IID_IWineTest, (void **)&pObject2);
2325 ok(hr == E_NOINTERFACE, "IUnknown_QueryInterface on handler for invalid interface returned 0x%08x instead of E_NOINTERFACE\n", hr);
2326
2327 /* it's a handler as it supports IOleObject */
2328 hr = IUnknown_QueryInterface(pObject, &IID_IOleObject, (void **)&pObject2);
2329 ok_ole_success(hr, "IUnknown_QueryInterface(&IID_IOleObject)");
2330 IUnknown_Release(pObject2);
2331
2332 IUnknown_Release(pObject);
2333 }
2334
2335 reg_unreg_wine_test_class(FALSE);
2336 }
2337
2338 static HRESULT WINAPI Test_SMI_QueryInterface(
2339 IStdMarshalInfo *iface,
2340 REFIID riid,
2341 LPVOID *ppvObj)
2342 {
2343 if (ppvObj == NULL) return E_POINTER;
2344
2345 if (IsEqualGUID(riid, &IID_IUnknown) ||
2346 IsEqualGUID(riid, &IID_IStdMarshalInfo))
2347 {
2348 *ppvObj = iface;
2349 IClassFactory_AddRef(iface);
2350 return S_OK;
2351 }
2352
2353 return E_NOINTERFACE;
2354 }
2355
2356 static ULONG WINAPI Test_SMI_AddRef(IStdMarshalInfo *iface)
2357 {
2358 LockModule();
2359 return 2; /* non-heap-based object */
2360 }
2361
2362 static ULONG WINAPI Test_SMI_Release(IStdMarshalInfo *iface)
2363 {
2364 UnlockModule();
2365 return 1; /* non-heap-based object */
2366 }
2367
2368 static HRESULT WINAPI Test_SMI_GetClassForHandler(
2369 IStdMarshalInfo *iface,
2370 DWORD dwDestContext,
2371 void *pvDestContext,
2372 CLSID *pClsid)
2373 {
2374 *pClsid = CLSID_WineTest;
2375 return S_OK;
2376 }
2377
2378 static const IStdMarshalInfoVtbl Test_SMI_Vtbl =
2379 {
2380 Test_SMI_QueryInterface,
2381 Test_SMI_AddRef,
2382 Test_SMI_Release,
2383 Test_SMI_GetClassForHandler
2384 };
2385
2386 static IStdMarshalInfo Test_SMI = {&Test_SMI_Vtbl};
2387
2388 static void test_handler_marshaling(void)
2389 {
2390 HRESULT hr;
2391 IStream *pStream = NULL;
2392 IUnknown *pProxy = NULL;
2393 IUnknown *pObject;
2394 DWORD tid;
2395 HANDLE thread;
2396 static const LARGE_INTEGER ullZero;
2397
2398 if (FAILED(reg_unreg_wine_test_class(TRUE)))
2399 return;
2400 cLocks = 0;
2401
2402 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
2403 ok_ole_success(hr, "CreateStreamOnHGlobal");
2404 tid = start_host_object(pStream, &IID_IUnknown, (IUnknown*)&Test_SMI, MSHLFLAGS_NORMAL, &thread);
2405
2406 ok_more_than_one_lock();
2407
2408 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
2409 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)&pProxy);
2410 ok_ole_success(hr, "CoUnmarshalInterface");
2411 IStream_Release(pStream);
2412
2413 if(hr == S_OK)
2414 {
2415 ok_more_than_one_lock();
2416
2417 hr = IUnknown_QueryInterface(pProxy, &IID_IWineTest, (void **)&pObject);
2418 ok(hr == E_NOINTERFACE, "IUnknown_QueryInterface with unknown IID should have returned E_NOINTERFACE instead of 0x%08x\n", hr);
2419
2420 /* it's a handler as it supports IOleObject */
2421 hr = IUnknown_QueryInterface(pProxy, &IID_IOleObject, (void **)&pObject);
2422 todo_wine
2423 ok_ole_success(hr, "IUnknown_QueryInterface(&IID_IOleObject)");
2424 if (SUCCEEDED(hr)) IUnknown_Release(pObject);
2425
2426 IUnknown_Release(pProxy);
2427
2428 ok_no_locks();
2429 }
2430
2431 end_host_object(tid, thread);
2432 reg_unreg_wine_test_class(FALSE);
2433
2434 /* FIXME: test IPersist interface has the same effect as IStdMarshalInfo */
2435 }
2436
2437
2438 static void test_client_security(void)
2439 {
2440 HRESULT hr;
2441 IStream *pStream = NULL;
2442 IClassFactory *pProxy = NULL;
2443 IUnknown *pProxy2 = NULL;
2444 IUnknown *pUnknown1 = NULL;
2445 IUnknown *pUnknown2 = NULL;
2446 IClientSecurity *pCliSec = NULL;
2447 IMarshal *pMarshal;
2448 DWORD tid;
2449 HANDLE thread;
2450 static const LARGE_INTEGER ullZero;
2451 DWORD dwAuthnSvc;
2452 DWORD dwAuthzSvc;
2453 OLECHAR *pServerPrincName;
2454 DWORD dwAuthnLevel;
2455 DWORD dwImpLevel;
2456 void *pAuthInfo;
2457 DWORD dwCapabilities;
2458 void *pv;
2459
2460 cLocks = 0;
2461
2462 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
2463 ok_ole_success(hr, "CreateStreamOnHGlobal");
2464 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
2465
2466 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
2467 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
2468 ok_ole_success(hr, "CoUnmarshalInterface");
2469 IStream_Release(pStream);
2470
2471 hr = IUnknown_QueryInterface(pProxy, &IID_IUnknown, (LPVOID*)&pUnknown1);
2472 ok_ole_success(hr, "IUnknown_QueryInterface IID_IUnknown");
2473
2474 hr = IUnknown_QueryInterface(pProxy, &IID_IRemUnknown, (LPVOID*)&pProxy2);
2475 ok_ole_success(hr, "IUnknown_QueryInterface IID_IStream");
2476
2477 hr = IUnknown_QueryInterface(pProxy2, &IID_IUnknown, (LPVOID*)&pUnknown2);
2478 ok_ole_success(hr, "IUnknown_QueryInterface IID_IUnknown");
2479
2480 ok(pUnknown1 == pUnknown2, "both proxy's IUnknowns should be the same - %p, %p\n", pUnknown1, pUnknown2);
2481
2482 hr = IUnknown_QueryInterface(pProxy, &IID_IMarshal, (LPVOID*)&pMarshal);
2483 ok_ole_success(hr, "IUnknown_QueryInterface IID_IMarshal");
2484
2485 hr = IUnknown_QueryInterface(pProxy, &IID_IClientSecurity, (LPVOID*)&pCliSec);
2486 ok_ole_success(hr, "IUnknown_QueryInterface IID_IClientSecurity");
2487
2488 hr = IClientSecurity_QueryBlanket(pCliSec, (IUnknown *)pProxy, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
2489 todo_wine ok_ole_success(hr, "IClientSecurity_QueryBlanket (all NULLs)");
2490
2491 hr = IClientSecurity_QueryBlanket(pCliSec, (IUnknown *)pMarshal, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
2492 todo_wine ok(hr == E_NOINTERFACE, "IClientSecurity_QueryBlanket with local interface should have returned E_NOINTERFACE instead of 0x%08x\n", hr);
2493
2494 hr = IClientSecurity_QueryBlanket(pCliSec, (IUnknown *)pProxy, &dwAuthnSvc, &dwAuthzSvc, &pServerPrincName, &dwAuthnLevel, &dwImpLevel, &pAuthInfo, &dwCapabilities);
2495 todo_wine ok_ole_success(hr, "IClientSecurity_QueryBlanket");
2496
2497 hr = IClientSecurity_SetBlanket(pCliSec, (IUnknown *)pProxy, dwAuthnSvc, dwAuthzSvc, pServerPrincName, dwAuthnLevel, RPC_C_IMP_LEVEL_IMPERSONATE, pAuthInfo, dwCapabilities);
2498 todo_wine ok_ole_success(hr, "IClientSecurity_SetBlanket");
2499
2500 hr = IClassFactory_CreateInstance(pProxy, NULL, &IID_IWineTest, &pv);
2501 ok(hr == E_NOINTERFACE, "COM call should have succeeded instead of returning 0x%08x\n", hr);
2502
2503 hr = IClientSecurity_SetBlanket(pCliSec, (IUnknown *)pMarshal, dwAuthnSvc, dwAuthzSvc, pServerPrincName, dwAuthnLevel, dwImpLevel, pAuthInfo, dwCapabilities);
2504 todo_wine ok(hr == E_NOINTERFACE, "IClientSecurity_SetBlanket with local interface should have returned E_NOINTERFACE instead of 0x%08x\n", hr);
2505
2506 hr = IClientSecurity_SetBlanket(pCliSec, (IUnknown *)pProxy, 0xdeadbeef, dwAuthzSvc, pServerPrincName, dwAuthnLevel, dwImpLevel, pAuthInfo, dwCapabilities);
2507 todo_wine ok(hr == E_INVALIDARG, "IClientSecurity_SetBlanke with invalid dwAuthnSvc should have returned E_INVALIDARG instead of 0x%08x\n", hr);
2508
2509 CoTaskMemFree(pServerPrincName);
2510
2511 hr = IClientSecurity_QueryBlanket(pCliSec, pUnknown1, &dwAuthnSvc, &dwAuthzSvc, &pServerPrincName, &dwAuthnLevel, &dwImpLevel, &pAuthInfo, &dwCapabilities);
2512 todo_wine ok_ole_success(hr, "IClientSecurity_QueryBlanket(IUnknown)");
2513
2514 CoTaskMemFree(pServerPrincName);
2515
2516 IClassFactory_Release(pProxy);
2517 IUnknown_Release(pProxy2);
2518 IUnknown_Release(pUnknown1);
2519 IUnknown_Release(pUnknown2);
2520 IMarshal_Release(pMarshal);
2521 IClientSecurity_Release(pCliSec);
2522
2523 end_host_object(tid, thread);
2524 }
2525
2526 static HANDLE heventShutdown;
2527
2528 static void LockModuleOOP(void)
2529 {
2530 InterlockedIncrement(&cLocks); /* for test purposes only */
2531 CoAddRefServerProcess();
2532 }
2533
2534 static void UnlockModuleOOP(void)
2535 {
2536 InterlockedDecrement(&cLocks); /* for test purposes only */
2537 if (!CoReleaseServerProcess())
2538 SetEvent(heventShutdown);
2539 }
2540
2541 static HWND hwnd_app;
2542
2543 static HRESULT WINAPI TestOOP_IClassFactory_QueryInterface(
2544 LPCLASSFACTORY iface,
2545 REFIID riid,
2546 LPVOID *ppvObj)
2547 {
2548 if (ppvObj == NULL) return E_POINTER;
2549
2550 if (IsEqualGUID(riid, &IID_IUnknown) ||
2551 IsEqualGUID(riid, &IID_IClassFactory))
2552 {
2553 *ppvObj = iface;
2554 IClassFactory_AddRef(iface);
2555 return S_OK;
2556 }
2557
2558 return E_NOINTERFACE;
2559 }
2560
2561 static ULONG WINAPI TestOOP_IClassFactory_AddRef(LPCLASSFACTORY iface)
2562 {
2563 return 2; /* non-heap-based object */
2564 }
2565
2566 static ULONG WINAPI TestOOP_IClassFactory_Release(LPCLASSFACTORY iface)
2567 {
2568 return 1; /* non-heap-based object */
2569 }
2570
2571 static HRESULT WINAPI TestOOP_IClassFactory_CreateInstance(
2572 LPCLASSFACTORY iface,
2573 LPUNKNOWN pUnkOuter,
2574 REFIID riid,
2575 LPVOID *ppvObj)
2576 {
2577 if (IsEqualIID(riid, &IID_IClassFactory) || IsEqualIID(riid, &IID_IUnknown))
2578 {
2579 *ppvObj = iface;
2580 return S_OK;
2581 }
2582 return CLASS_E_CLASSNOTAVAILABLE;
2583 }
2584
2585 static HRESULT WINAPI TestOOP_IClassFactory_LockServer(
2586 LPCLASSFACTORY iface,
2587 BOOL fLock)
2588 {
2589 if (fLock)
2590 LockModuleOOP();
2591 else
2592 UnlockModuleOOP();
2593 return S_OK;
2594 }
2595
2596 static const IClassFactoryVtbl TestClassFactoryOOP_Vtbl =
2597 {
2598 TestOOP_IClassFactory_QueryInterface,
2599 TestOOP_IClassFactory_AddRef,
2600 TestOOP_IClassFactory_Release,
2601 TestOOP_IClassFactory_CreateInstance,
2602 TestOOP_IClassFactory_LockServer
2603 };
2604
2605 static IClassFactory TestOOP_ClassFactory = { &TestClassFactoryOOP_Vtbl };
2606
2607 static void test_register_local_server(void)
2608 {
2609 DWORD cookie;
2610 HRESULT hr;
2611 HANDLE ready_event;
2612 HANDLE quit_event;
2613 DWORD wait;
2614
2615 heventShutdown = CreateEvent(NULL, TRUE, FALSE, NULL);
2616
2617 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&TestOOP_ClassFactory,
2618 CLSCTX_LOCAL_SERVER, REGCLS_SINGLEUSE, &cookie);
2619 ok_ole_success(hr, CoRegisterClassObject);
2620
2621 ready_event = CreateEvent(NULL, FALSE, FALSE, "Wine COM Test Ready Event");
2622 SetEvent(ready_event);
2623
2624 quit_event = CreateEvent(NULL, FALSE, FALSE, "Wine COM Test Quit Event");
2625
2626 do
2627 {
2628 wait = MsgWaitForMultipleObjects(1, &quit_event, FALSE, 30000, QS_ALLINPUT);
2629 if (wait == WAIT_OBJECT_0+1)
2630 {
2631 MSG msg;
2632 BOOL ret = PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
2633 if (ret)
2634 {
2635 trace("Message 0x%x\n", msg.message);
2636 TranslateMessage(&msg);
2637 DispatchMessage(&msg);
2638 }
2639 }
2640 }
2641 while (wait == WAIT_OBJECT_0+1);
2642
2643 ok( wait == WAIT_OBJECT_0, "quit event wait timed out\n" );
2644 hr = CoRevokeClassObject(cookie);
2645 ok_ole_success(hr, CoRevokeClassObject);
2646 }
2647
2648 static HANDLE create_target_process(const char *arg)
2649 {
2650 char **argv;
2651 char cmdline[MAX_PATH];
2652 BOOL ret;
2653 PROCESS_INFORMATION pi;
2654 STARTUPINFO si = { 0 };
2655 si.cb = sizeof(si);
2656
2657 pi.hThread = NULL;
2658 pi.hProcess = NULL;
2659 winetest_get_mainargs( &argv );
2660 sprintf(cmdline, "%s %s %s", argv[0], argv[1], arg);
2661 ret = CreateProcess(argv[0], cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
2662 ok(ret, "CreateProcess failed with error: %u\n", GetLastError());
2663 if (pi.hThread) CloseHandle(pi.hThread);
2664 return pi.hProcess;
2665 }
2666
2667 /* tests functions commonly used by out of process COM servers */
2668 static void test_local_server(void)
2669 {
2670 DWORD cookie;
2671 HRESULT hr;
2672 IClassFactory * cf;
2673 DWORD ret;
2674 HANDLE process;
2675 HANDLE quit_event;
2676 HANDLE ready_event;
2677
2678 heventShutdown = CreateEvent(NULL, TRUE, FALSE, NULL);
2679
2680 cLocks = 0;
2681
2682 /* Start the object suspended */
2683 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&TestOOP_ClassFactory,
2684 CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE | REGCLS_SUSPENDED, &cookie);
2685 ok_ole_success(hr, CoRegisterClassObject);
2686
2687 /* ... and CoGetClassObject does not find it and fails when it looks for the
2688 * class in the registry */
2689 hr = CoGetClassObject(&CLSID_WineOOPTest, CLSCTX_INPROC_SERVER,
2690 NULL, &IID_IClassFactory, (LPVOID*)&cf);
2691 ok(hr == REGDB_E_CLASSNOTREG || /* NT */
2692 hr == S_OK /* Win9x */,
2693 "CoGetClassObject should have returned REGDB_E_CLASSNOTREG instead of 0x%08x\n", hr);
2694
2695 /* Resume the object suspended above ... */
2696 hr = CoResumeClassObjects();
2697 ok_ole_success(hr, CoResumeClassObjects);
2698
2699 /* ... and now it should succeed */
2700 hr = CoGetClassObject(&CLSID_WineOOPTest, CLSCTX_INPROC_SERVER,
2701 NULL, &IID_IClassFactory, (LPVOID*)&cf);
2702 ok_ole_success(hr, CoGetClassObject);
2703
2704 /* Now check the locking is working */
2705 /* NOTE: we are accessing the class directly, not through a proxy */
2706
2707 ok_no_locks();
2708
2709 hr = IClassFactory_LockServer(cf, TRUE);
2710 ok_ole_success(hr, IClassFactory_LockServer);
2711
2712 ok_more_than_one_lock();
2713
2714 IClassFactory_LockServer(cf, FALSE);
2715 ok_ole_success(hr, IClassFactory_LockServer);
2716
2717 ok_no_locks();
2718
2719 IClassFactory_Release(cf);
2720
2721 /* wait for shutdown signal */
2722 ret = WaitForSingleObject(heventShutdown, 0);
2723 ok(ret != WAIT_TIMEOUT, "Server didn't shut down\n");
2724
2725 /* try to connect again after SCM has suspended registered class objects */
2726 hr = CoGetClassObject(&CLSID_WineOOPTest, CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER, NULL,
2727 &IID_IClassFactory, (LPVOID*)&cf);
2728 ok(hr == CO_E_SERVER_STOPPING || /* NT */
2729 hr == REGDB_E_CLASSNOTREG || /* win2k */
2730 hr == S_OK /* Win9x */,
2731 "CoGetClassObject should have returned CO_E_SERVER_STOPPING or REGDB_E_CLASSNOTREG instead of 0x%08x\n", hr);
2732
2733 hr = CoRevokeClassObject(cookie);
2734 ok_ole_success(hr, CoRevokeClassObject);
2735
2736 CloseHandle(heventShutdown);
2737
2738 process = create_target_process("-Embedding");
2739 ok(process != NULL, "couldn't start local server process, error was %d\n", GetLastError());
2740
2741 ready_event = CreateEvent(NULL, FALSE, FALSE, "Wine COM Test Ready Event");
2742 ok( !WaitForSingleObject(ready_event, 10000), "wait timed out\n" );
2743 CloseHandle(ready_event);
2744
2745 hr = CoCreateInstance(&CLSID_WineOOPTest, NULL, CLSCTX_LOCAL_SERVER, &IID_IClassFactory, (void **)&cf);
2746 ok_ole_success(hr, CoCreateInstance);
2747
2748 IClassFactory_Release(cf);
2749
2750 hr = CoCreateInstance(&CLSID_WineOOPTest, NULL, CLSCTX_LOCAL_SERVER, &IID_IClassFactory, (void **)&cf);
2751 ok(hr == REGDB_E_CLASSNOTREG, "Second CoCreateInstance on REGCLS_SINGLEUSE object should have failed\n");
2752
2753 quit_event = CreateEvent(NULL, FALSE, FALSE, "Wine COM Test Quit Event");
2754 SetEvent(quit_event);
2755
2756 winetest_wait_child_process( process );
2757 CloseHandle(quit_event);
2758 CloseHandle(process);
2759 }
2760
2761 struct git_params
2762 {
2763 DWORD cookie;
2764 IGlobalInterfaceTable *git;
2765 };
2766
2767 static DWORD CALLBACK get_global_interface_proc(LPVOID pv)
2768 {
2769 HRESULT hr;
2770 struct git_params *params = pv;
2771 IClassFactory *cf;
2772
2773 hr = IGlobalInterfaceTable_GetInterfaceFromGlobal(params->git, params->cookie, &IID_IClassFactory, (void **)&cf);
2774 ok(hr == CO_E_NOTINITIALIZED ||
2775 broken(hr == E_UNEXPECTED) /* win2k */ ||
2776 broken(hr == S_OK) /* NT 4 */,
2777 "IGlobalInterfaceTable_GetInterfaceFromGlobal should have failed with error CO_E_NOTINITIALIZED or E_UNEXPECTED instead of 0x%08x\n",
2778 hr);
2779 if (hr == S_OK)
2780 IClassFactory_Release(cf);
2781
2782 CoInitialize(NULL);
2783
2784 hr = IGlobalInterfaceTable_GetInterfaceFromGlobal(params->git, params->cookie, &IID_IClassFactory, (void **)&cf);
2785 ok_ole_success(hr, IGlobalInterfaceTable_GetInterfaceFromGlobal);
2786
2787 IClassFactory_Release(cf);
2788
2789 CoUninitialize();
2790
2791 return hr;
2792 }
2793
2794 static void test_globalinterfacetable(void)
2795 {
2796 HRESULT hr;
2797 IGlobalInterfaceTable *git;
2798 DWORD cookie;
2799 HANDLE thread;
2800 DWORD tid;
2801 struct git_params params;
2802 DWORD ret;
2803 IUnknown *object;
2804
2805 trace("test_globalinterfacetable\n");
2806 cLocks = 0;
2807
2808 hr = CoCreateInstance(&CLSID_StdGlobalInterfaceTable, NULL, CLSCTX_INPROC_SERVER, &IID_IGlobalInterfaceTable, (void **)&git);
2809 ok_ole_success(hr, CoCreateInstance);
2810
2811 hr = IGlobalInterfaceTable_RegisterInterfaceInGlobal(git, (IUnknown *)&Test_ClassFactory, &IID_IClassFactory, &cookie);
2812 ok_ole_success(hr, IGlobalInterfaceTable_RegisterInterfaceInGlobal);
2813
2814 ok_more_than_one_lock();
2815
2816 params.cookie = cookie;
2817 params.git = git;
2818 /* note: params is on stack so we MUST wait for get_global_interface_proc
2819 * to exit before we can return */
2820 thread = CreateThread(NULL, 0, get_global_interface_proc, &params, 0, &tid);
2821
2822 ret = MsgWaitForMultipleObjects(1, &thread, FALSE, 10000, QS_ALLINPUT);
2823 while (ret == WAIT_OBJECT_0 + 1)
2824 {
2825 MSG msg;
2826 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
2827 DispatchMessage(&msg);
2828 ret = MsgWaitForMultipleObjects(1, &thread, FALSE, 10000, QS_ALLINPUT);
2829 }
2830
2831 CloseHandle(thread);
2832
2833 /* test getting interface from global with different iid */
2834 hr = IGlobalInterfaceTable_GetInterfaceFromGlobal(git, cookie, &IID_IUnknown, (void **)&object);
2835 ok_ole_success(hr, IGlobalInterfaceTable_GetInterfaceFromGlobal);
2836 IUnknown_Release(object);
2837
2838 /* test getting interface from global with same iid */
2839 hr = IGlobalInterfaceTable_GetInterfaceFromGlobal(git, cookie, &IID_IClassFactory, (void **)&object);
2840 ok_ole_success(hr, IGlobalInterfaceTable_GetInterfaceFromGlobal);
2841 IUnknown_Release(object);
2842
2843 hr = IGlobalInterfaceTable_RevokeInterfaceFromGlobal(git, cookie);
2844 ok_ole_success(hr, IGlobalInterfaceTable_RevokeInterfaceFromGlobal);
2845
2846 ok_no_locks();
2847
2848 IGlobalInterfaceTable_Release(git);
2849 }
2850
2851 static void test_manualresetevent(void)
2852 {
2853 ISynchronize *psync1, *psync2;
2854 IUnknown *punk;
2855 LONG ref;
2856 HRESULT hr;
2857
2858 hr = CoCreateInstance(&CLSID_ManualResetEvent, NULL, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void**)&punk);
2859 ok(hr == S_OK, "Got 0x%08x\n", hr);
2860 ok(!!punk, "Got NULL.\n");
2861 IUnknown_Release(punk);
2862
2863 hr = CoCreateInstance(&CLSID_ManualResetEvent, NULL, CLSCTX_INPROC_SERVER, &IID_ISynchronize, (void**)&psync1);
2864 ok(hr == S_OK, "Got 0x%08x\n", hr);
2865 ok(!!psync1, "Got NULL.\n");
2866
2867 hr = ISynchronize_Wait(psync1, 0, 5);
2868 ok(hr == RPC_S_CALLPENDING, "Got 0x%08x\n", hr);
2869
2870 hr = ISynchronize_Reset(psync1);
2871 ok(hr == S_OK, "Got 0x%08x\n", hr);
2872 hr = ISynchronize_Signal(psync1);
2873 ok(hr == S_OK, "Got 0x%08x\n", hr);
2874 hr = ISynchronize_Wait(psync1, 0, 5);
2875 ok(hr == S_OK, "Got 0x%08x\n", hr);
2876 hr = ISynchronize_Wait(psync1, 0, 5);
2877 ok(hr == S_OK, "Got 0x%08x\n", hr);
2878 hr = ISynchronize_Reset(psync1);
2879 ok(hr == S_OK, "Got 0x%08x\n", hr);
2880 hr = ISynchronize_Wait(psync1, 0, 5);
2881 ok(hr == RPC_S_CALLPENDING, "Got 0x%08x\n", hr);
2882
2883 hr = CoCreateInstance(&CLSID_ManualResetEvent, NULL, CLSCTX_INPROC_SERVER, &IID_ISynchronize, (void**)&psync2);
2884 ok(hr == S_OK, "Got 0x%08x\n", hr);
2885 ok(!!psync2, "Got NULL.\n");
2886 ok(psync1 != psync2, "psync1 == psync2.\n");
2887 hr = ISynchronize_Wait(psync2, 0, 5);
2888 ok(hr == RPC_S_CALLPENDING, "Got 0x%08x\n", hr);
2889
2890 hr = ISynchronize_Reset(psync1);
2891 ok(hr == S_OK, "Got 0x%08x\n", hr);
2892 hr = ISynchronize_Reset(psync2);
2893 ok(hr == S_OK, "Got 0x%08x\n", hr);
2894 hr = ISynchronize_Signal(psync1);
2895 ok(hr == S_OK, "Got 0x%08x\n", hr);
2896 hr = ISynchronize_Wait(psync2, 0, 5);
2897 ok(hr == RPC_S_CALLPENDING, "Got 0x%08x\n", hr);
2898
2899 ref = ISynchronize_AddRef(psync1);
2900 ok(ref == 2, "Got ref: %d\n", ref);
2901 ref = ISynchronize_AddRef(psync1);
2902 ok(ref == 3, "Got ref: %d\n", ref);
2903 ref = ISynchronize_Release(psync1);
2904 ok(ref == 2, "Got nonzero ref: %d\n", ref);
2905 ref = ISynchronize_Release(psync2);
2906 ok(!ref, "Got nonzero ref: %d\n", ref);
2907 ref = ISynchronize_Release(psync1);
2908 ok(ref == 1, "Got nonzero ref: %d\n", ref);
2909 ref = ISynchronize_Release(psync1);
2910 ok(!ref, "Got nonzero ref: %d\n", ref);
2911 }
2912
2913 static const char *debugstr_iid(REFIID riid)
2914 {
2915 static char name[256];
2916 HKEY hkeyInterface;
2917 WCHAR bufferW[39];
2918 char buffer[39];
2919 LONG name_size = sizeof(name);
2920 StringFromGUID2(riid, bufferW, sizeof(bufferW)/sizeof(bufferW[0]));
2921 WideCharToMultiByte(CP_ACP, 0, bufferW, sizeof(bufferW)/sizeof(bufferW[0]), buffer, sizeof(buffer), NULL, NULL);
2922 if (RegOpenKeyEx(HKEY_CLASSES_ROOT, "Interface", 0, KEY_QUERY_VALUE, &hkeyInterface) != ERROR_SUCCESS)
2923 {
2924 memcpy(name, buffer, sizeof(buffer));
2925 goto done;
2926 }
2927 if (RegQueryValue(hkeyInterface, buffer, name, &name_size) != ERROR_SUCCESS)
2928 {
2929 memcpy(name, buffer, sizeof(buffer));
2930 goto done;
2931 }
2932 RegCloseKey(hkeyInterface);
2933 done:
2934 return name;
2935 }
2936
2937 static HRESULT WINAPI TestChannelHook_QueryInterface(IChannelHook *iface, REFIID riid, void **ppv)
2938 {
2939 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IChannelHook))
2940 {
2941 *ppv = iface;
2942 IUnknown_AddRef(iface);
2943 return S_OK;
2944 }
2945
2946 *ppv = NULL;
2947 return E_NOINTERFACE;
2948 }
2949
2950 static ULONG WINAPI TestChannelHook_AddRef(IChannelHook *iface)
2951 {
2952 return 2;
2953 }
2954
2955 static ULONG WINAPI TestChannelHook_Release(IChannelHook *iface)
2956 {
2957 return 1;
2958 }
2959
2960 static void WINAPI TestChannelHook_ClientGetSize(
2961 IChannelHook *iface,
2962 REFGUID uExtent,
2963 REFIID riid,
2964 ULONG *pDataSize )
2965 {
2966 SChannelHookCallInfo *info = (SChannelHookCallInfo *)riid;
2967 trace("TestChannelHook_ClientGetBuffer\n");
2968 trace("\t%s method %d\n", debugstr_iid(riid), info->iMethod);
2969 trace("\tcid: %s\n", debugstr_iid(&info->uCausality));
2970 ok(info->cbSize == sizeof(*info), "info->cbSize was %d instead of %d\n", info->cbSize, (int)sizeof(*info));
2971 ok(info->dwServerPid == GetCurrentProcessId(), "info->dwServerPid was 0x%x instead of 0x%x\n", info->dwServerPid, GetCurrentProcessId());
2972 ok(!info->pObject, "info->pObject should be NULL\n");
2973 ok(IsEqualGUID(uExtent, &EXTENTID_WineTest), "uExtent wasn't correct\n");
2974
2975 *pDataSize = 1;
2976 }
2977
2978 static void WINAPI TestChannelHook_ClientFillBuffer(
2979 IChannelHook *iface,
2980 REFGUID uExtent,
2981 REFIID riid,
2982 ULONG *pDataSize,
2983 void *pDataBuffer )
2984 {
2985 SChannelHookCallInfo *info = (SChannelHookCallInfo *)riid;
2986 trace("TestChannelHook_ClientFillBuffer\n");
2987 ok(info->cbSize == sizeof(*info), "info->cbSize was %d instead of %d\n", info->cbSize, (int)sizeof(*info));
2988 ok(info->dwServerPid == GetCurrentProcessId(), "info->dwServerPid was 0x%x instead of 0x%x\n", info->dwServerPid, GetCurrentProcessId());
2989 ok(!info->pObject, "info->pObject should be NULL\n");
2990 ok(IsEqualGUID(uExtent, &EXTENTID_WineTest), "uExtent wasn't correct\n");
2991
2992 *(unsigned char *)pDataBuffer = 0xcc;
2993 *pDataSize = 1;
2994 }
2995
2996 static void WINAPI TestChannelHook_ClientNotify(
2997 IChannelHook *iface,
2998 REFGUID uExtent,
2999 REFIID riid,
3000 ULONG cbDataSize,
3001 void *pDataBuffer,
3002 DWORD lDataRep,
3003 HRESULT hrFault )
3004 {
3005 SChannelHookCallInfo *info = (SChannelHookCallInfo *)riid;
3006 trace("TestChannelHook_ClientNotify hrFault = 0x%08x\n", hrFault);
3007 ok(info->cbSize == sizeof(*info), "info->cbSize was %d instead of %d\n", info->cbSize, (int)sizeof(*info));
3008 ok(info->dwServerPid == GetCurrentProcessId(), "info->dwServerPid was 0x%x instead of 0x%x\n", info->dwServerPid, GetCurrentProcessId());
3009 todo_wine {
3010 ok(info->pObject != NULL, "info->pObject shouldn't be NULL\n");
3011 }
3012 ok(IsEqualGUID(uExtent, &EXTENTID_WineTest), "uExtent wasn't correct\n");
3013 }
3014
3015 static void WINAPI TestChannelHook_ServerNotify(
3016 IChannelHook *iface,
3017 REFGUID uExtent,
3018 REFIID riid,
3019 ULONG cbDataSize,
3020 void *pDataBuffer,
3021 DWORD lDataRep )
3022 {
3023 SChannelHookCallInfo *info = (SChannelHookCallInfo *)riid;
3024 trace("TestChannelHook_ServerNotify\n");
3025 ok(info->cbSize == sizeof(*info), "info->cbSize was %d instead of %d\n", info->cbSize, (int)sizeof(*info));
3026 ok(info->dwServerPid == GetCurrentProcessId(), "info->dwServerPid was 0x%x instead of 0x%x\n", info->dwServerPid, GetCurrentProcessId());
3027 ok(info->pObject != NULL, "info->pObject shouldn't be NULL\n");
3028 ok(cbDataSize == 1, "cbDataSize should have been 1 instead of %d\n", cbDataSize);
3029 ok(*(unsigned char *)pDataBuffer == 0xcc, "pDataBuffer should have contained 0xcc instead of 0x%x\n", *(unsigned char *)pDataBuffer);
3030 ok(IsEqualGUID(uExtent, &EXTENTID_WineTest), "uExtent wasn't correct\n");
3031 }
3032
3033 static void WINAPI TestChannelHook_ServerGetSize(
3034 IChannelHook *iface,
3035 REFGUID uExtent,
3036 REFIID riid,
3037 HRESULT hrFault,
3038 ULONG *pDataSize )
3039 {
3040 SChannelHookCallInfo *info = (SChannelHookCallInfo *)riid;
3041 trace("TestChannelHook_ServerGetSize\n");
3042 trace("\t%s method %d\n", debugstr_iid(riid), info->iMethod);
3043 ok(info->cbSize == sizeof(*info), "info->cbSize was %d instead of %d\n", info->cbSize, (int)sizeof(*info));
3044 ok(info->dwServerPid == GetCurrentProcessId(), "info->dwServerPid was 0x%x instead of 0x%x\n", info->dwServerPid, GetCurrentProcessId());
3045 ok(info->pObject != NULL, "info->pObject shouldn't be NULL\n");
3046 ok(IsEqualGUID(uExtent, &EXTENTID_WineTest), "uExtent wasn't correct\n");
3047 if (hrFault != S_OK)
3048 trace("\thrFault = 0x%08x\n", hrFault);
3049
3050 *pDataSize = 0;
3051 }
3052
3053 static void WINAPI TestChannelHook_ServerFillBuffer(
3054 IChannelHook *iface,
3055 REFGUID uExtent,
3056 REFIID riid,
3057 ULONG *pDataSize,
3058 void *pDataBuffer,
3059 HRESULT hrFault )
3060 {
3061 trace("TestChannelHook_ServerFillBuffer\n");
3062 ok(0, "TestChannelHook_ServerFillBuffer shouldn't be called\n");
3063 }
3064
3065 static const IChannelHookVtbl TestChannelHookVtbl =
3066 {
3067 TestChannelHook_QueryInterface,
3068 TestChannelHook_AddRef,
3069 TestChannelHook_Release,
3070 TestChannelHook_ClientGetSize,
3071 TestChannelHook_ClientFillBuffer,
3072 TestChannelHook_ClientNotify,
3073 TestChannelHook_ServerNotify,
3074 TestChannelHook_ServerGetSize,
3075 TestChannelHook_ServerFillBuffer,
3076 };
3077
3078 static IChannelHook TestChannelHook = { &TestChannelHookVtbl };
3079
3080 static void test_channel_hook(void)
3081 {
3082 IStream *pStream = NULL;
3083 IClassFactory *cf = NULL;
3084 DWORD tid;
3085 IUnknown *proxy = NULL;
3086 HANDLE thread;
3087 HRESULT hr;
3088
3089 hr = CoRegisterChannelHook(&EXTENTID_WineTest, &TestChannelHook);
3090 ok_ole_success(hr, CoRegisterChannelHook);
3091
3092 hr = CoRegisterMessageFilter(&MessageFilter, NULL);
3093 ok_ole_success(hr, CoRegisterMessageFilter);
3094
3095 cLocks = 0;
3096
3097 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
3098 ok_ole_success(hr, CreateStreamOnHGlobal);
3099 tid = start_host_object2(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &MessageFilter, &thread);
3100
3101 ok_more_than_one_lock();
3102
3103 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
3104 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&cf);
3105 ok_ole_success(hr, CoUnmarshalInterface);
3106 IStream_Release(pStream);
3107
3108 ok_more_than_one_lock();
3109
3110 hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
3111 ok_ole_success(hr, IClassFactory_CreateInstance);
3112 IUnknown_Release(proxy);
3113
3114 IClassFactory_Release(cf);
3115
3116 ok_no_locks();
3117
3118 end_host_object(tid, thread);
3119
3120 hr = CoRegisterMessageFilter(NULL, NULL);
3121 ok_ole_success(hr, CoRegisterMessageFilter);
3122 }
3123
3124 START_TEST(marshal)
3125 {
3126 HMODULE hOle32 = GetModuleHandle("ole32");
3127 int argc;
3128 char **argv;
3129
3130 if (!GetProcAddress(hOle32, "CoRegisterSurrogateEx")) {
3131 win_skip("skipping test on win9x\n");
3132 return;
3133 }
3134
3135 pCoInitializeEx = (void*)GetProcAddress(hOle32, "CoInitializeEx");
3136
3137 argc = winetest_get_mainargs( &argv );
3138 if (argc > 2 && (!strcmp(argv[2], "-Embedding")))
3139 {
3140 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
3141 test_register_local_server();
3142 CoUninitialize();
3143
3144 return;
3145 }
3146
3147 register_test_window();
3148
3149 test_cocreateinstance_proxy();
3150
3151 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
3152
3153 /* FIXME: test CoCreateInstanceEx */
3154
3155 /* lifecycle management and marshaling tests */
3156 test_no_marshaler();
3157 test_normal_marshal_and_release();
3158 test_normal_marshal_and_unmarshal();
3159 test_marshal_and_unmarshal_invalid();
3160 test_same_apartment_unmarshal_failure();
3161 test_interthread_marshal_and_unmarshal();
3162 test_proxy_marshal_and_unmarshal();
3163 test_proxy_marshal_and_unmarshal2();
3164 test_proxy_marshal_and_unmarshal_weak();
3165 test_proxy_marshal_and_unmarshal_strong();
3166 test_marshal_stub_apartment_shutdown();
3167 test_marshal_proxy_apartment_shutdown();
3168 test_marshal_proxy_mta_apartment_shutdown();
3169 test_no_couninitialize_server();
3170 test_no_couninitialize_client();
3171 test_tableweak_marshal_and_unmarshal_twice();
3172 test_tableweak_marshal_releasedata1();
3173 test_tableweak_marshal_releasedata2();
3174 test_tableweak_and_normal_marshal_and_unmarshal();
3175 test_tablestrong_marshal_and_unmarshal_twice();
3176 test_lock_object_external();
3177 test_disconnect_stub();
3178 test_normal_marshal_and_unmarshal_twice();
3179 test_hresult_marshaling();
3180 test_proxy_used_in_wrong_thread();
3181 test_message_filter();
3182 test_bad_marshal_stream();
3183 test_proxy_interfaces();
3184 test_stubbuffer(&IID_IClassFactory);
3185 test_proxybuffer(&IID_IClassFactory);
3186 test_message_reentrancy();
3187 test_call_from_message();
3188 test_WM_QUIT_handling();
3189 test_freethreadedmarshaler();
3190 test_inproc_handler();
3191 test_handler_marshaling();
3192 test_client_security();
3193
3194 test_local_server();
3195
3196 test_globalinterfacetable();
3197 test_manualresetevent();
3198
3199 /* must be last test as channel hooks can't be unregistered */
3200 test_channel_hook();
3201
3202 CoUninitialize();
3203 }