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