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