4 * Copyright 2004 Robert Shearman
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.
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.
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
36 #include "wine/test.h"
38 DEFINE_GUID(CLSID_StdGlobalInterfaceTable,0x00000323,0x0000,0x0000,0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46);
40 /* functions that are not present on all versions of Windows */
41 HRESULT (WINAPI * pCoInitializeEx)(LPVOID lpReserved, DWORD dwCoInit);
43 /* helper macros to make tests a bit leaner */
44 #define ok_more_than_one_lock() ok(cLocks > 0, "Number of locks should be > 0, but actually is %d\n", cLocks)
45 #define ok_no_locks() ok(cLocks == 0, "Number of locks should be 0, but actually is %d\n", cLocks)
46 #define ok_ole_success(hr, func) ok(hr == S_OK, #func " failed with error 0x%08x\n", hr)
48 static const IID IID_IWineTest =
53 {0xa1, 0xa2, 0x5d, 0x5a, 0x36, 0x54, 0xd3, 0xbd}
54 }; /* 5201163f-8164-4fd0-a1a2-5d5a3654d3bd */
56 static const IID IID_IRemUnknown =
61 {0xc0,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46}
64 #define EXTENTID_WineTest IID_IWineTest
65 #define CLSID_WineTest IID_IWineTest
67 static const CLSID CLSID_WineOOPTest =
72 {0xa1, 0xa2, 0x5d, 0x5a, 0x36, 0x54, 0xd3, 0xbd}
73 }; /* 5201163f-8164-4fd0-a1a2-5d5a3654d3bd */
75 static void test_cocreateinstance_proxy(void)
81 pCoInitializeEx(NULL, COINIT_MULTITHREADED);
83 hr = CoCreateInstance(&CLSID_ShellDesktop, NULL, CLSCTX_INPROC, &IID_IUnknown, (void **)&pProxy);
84 ok_ole_success(hr, CoCreateInstance);
85 hr = IUnknown_QueryInterface(pProxy, &IID_IMultiQI, (void **)&pMQI);
86 ok(hr == S_OK, "created object is not a proxy, so was created in the wrong apartment\n");
88 IMultiQI_Release(pMQI);
89 IUnknown_Release(pProxy);
94 static const LARGE_INTEGER ullZero;
97 static void LockModule(void)
99 InterlockedIncrement(&cLocks);
102 static void UnlockModule(void)
104 InterlockedDecrement(&cLocks);
108 static HRESULT WINAPI Test_IUnknown_QueryInterface(
113 if (ppvObj == NULL) return E_POINTER;
115 if (IsEqualGUID(riid, &IID_IUnknown))
118 IUnknown_AddRef(iface);
123 return E_NOINTERFACE;
126 static ULONG WINAPI Test_IUnknown_AddRef(LPUNKNOWN iface)
129 return 2; /* non-heap-based object */
132 static ULONG WINAPI Test_IUnknown_Release(LPUNKNOWN iface)
135 return 1; /* non-heap-based object */
138 static const IUnknownVtbl TestUnknown_Vtbl =
140 Test_IUnknown_QueryInterface,
141 Test_IUnknown_AddRef,
142 Test_IUnknown_Release,
145 static IUnknown Test_Unknown = { &TestUnknown_Vtbl };
148 static HRESULT WINAPI Test_IClassFactory_QueryInterface(
149 LPCLASSFACTORY iface,
153 if (ppvObj == NULL) return E_POINTER;
155 if (IsEqualGUID(riid, &IID_IUnknown) ||
156 IsEqualGUID(riid, &IID_IClassFactory) ||
157 /* the only other interface Wine is currently able to marshal (for testing two proxies) */
158 IsEqualGUID(riid, &IID_IRemUnknown))
161 IClassFactory_AddRef(iface);
166 return E_NOINTERFACE;
169 static ULONG WINAPI Test_IClassFactory_AddRef(LPCLASSFACTORY iface)
172 return 2; /* non-heap-based object */
175 static ULONG WINAPI Test_IClassFactory_Release(LPCLASSFACTORY iface)
178 return 1; /* non-heap-based object */
181 static HRESULT WINAPI Test_IClassFactory_CreateInstance(
182 LPCLASSFACTORY iface,
187 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
188 return IUnknown_QueryInterface((IUnknown*)&Test_Unknown, riid, ppvObj);
191 static HRESULT WINAPI Test_IClassFactory_LockServer(
192 LPCLASSFACTORY iface,
198 static const IClassFactoryVtbl TestClassFactory_Vtbl =
200 Test_IClassFactory_QueryInterface,
201 Test_IClassFactory_AddRef,
202 Test_IClassFactory_Release,
203 Test_IClassFactory_CreateInstance,
204 Test_IClassFactory_LockServer
207 static IClassFactory Test_ClassFactory = { &TestClassFactory_Vtbl };
209 #define RELEASEMARSHALDATA WM_USER
211 struct host_object_data
216 MSHLFLAGS marshal_flags;
217 HANDLE marshal_event;
218 IMessageFilter *filter;
221 static DWORD CALLBACK host_object_proc(LPVOID p)
223 struct host_object_data *data = p;
227 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
231 IMessageFilter * prev_filter = NULL;
232 hr = CoRegisterMessageFilter(data->filter, &prev_filter);
233 if (prev_filter) IMessageFilter_Release(prev_filter);
234 ok_ole_success(hr, CoRegisterMessageFilter);
237 hr = CoMarshalInterface(data->stream, &data->iid, data->object, MSHCTX_INPROC, NULL, data->marshal_flags);
238 ok_ole_success(hr, CoMarshalInterface);
240 /* force the message queue to be created before signaling parent thread */
241 PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
243 SetEvent(data->marshal_event);
245 while (GetMessage(&msg, NULL, 0, 0))
247 if (msg.hwnd == NULL && msg.message == RELEASEMARSHALDATA)
249 CoReleaseMarshalData(data->stream);
250 SetEvent((HANDLE)msg.lParam);
253 DispatchMessage(&msg);
256 HeapFree(GetProcessHeap(), 0, data);
263 static DWORD start_host_object2(IStream *stream, REFIID riid, IUnknown *object, MSHLFLAGS marshal_flags, IMessageFilter *filter, HANDLE *thread)
266 HANDLE marshal_event = CreateEvent(NULL, FALSE, FALSE, NULL);
267 struct host_object_data *data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data));
269 data->stream = stream;
271 data->object = object;
272 data->marshal_flags = marshal_flags;
273 data->marshal_event = marshal_event;
274 data->filter = filter;
276 *thread = CreateThread(NULL, 0, host_object_proc, data, 0, &tid);
278 /* wait for marshaling to complete before returning */
279 ok( !WaitForSingleObject(marshal_event, 10000), "wait timed out\n" );
280 CloseHandle(marshal_event);
285 static DWORD start_host_object(IStream *stream, REFIID riid, IUnknown *object, MSHLFLAGS marshal_flags, HANDLE *thread)
287 return start_host_object2(stream, riid, object, marshal_flags, NULL, thread);
290 /* asks thread to release the marshal data because it has to be done by the
291 * same thread that marshaled the interface in the first place. */
292 static void release_host_object(DWORD tid)
294 HANDLE event = CreateEvent(NULL, FALSE, FALSE, NULL);
295 PostThreadMessage(tid, RELEASEMARSHALDATA, 0, (LPARAM)event);
296 ok( !WaitForSingleObject(event, 10000), "wait timed out\n" );
300 static void end_host_object(DWORD tid, HANDLE thread)
302 BOOL ret = PostThreadMessage(tid, WM_QUIT, 0, 0);
303 ok(ret, "PostThreadMessage failed with error %d\n", GetLastError());
304 /* be careful of races - don't return until hosting thread has terminated */
305 ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
309 /* tests failure case of interface not having a marshaler specified in the
311 static void test_no_marshaler(void)
316 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
317 ok_ole_success(hr, CreateStreamOnHGlobal);
318 hr = CoMarshalInterface(pStream, &IID_IWineTest, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
319 ok(hr == E_NOINTERFACE, "CoMarshalInterface should have returned E_NOINTERFACE instead of 0x%08x\n", hr);
321 IStream_Release(pStream);
324 /* tests normal marshal and then release without unmarshaling */
325 static void test_normal_marshal_and_release(void)
328 IStream *pStream = NULL;
332 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
333 ok_ole_success(hr, CreateStreamOnHGlobal);
334 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
335 ok_ole_success(hr, CoMarshalInterface);
337 ok_more_than_one_lock();
339 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
340 hr = CoReleaseMarshalData(pStream);
341 ok_ole_success(hr, CoReleaseMarshalData);
342 IStream_Release(pStream);
347 /* tests success case of a same-thread marshal and unmarshal */
348 static void test_normal_marshal_and_unmarshal(void)
351 IStream *pStream = NULL;
352 IUnknown *pProxy = NULL;
356 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
357 ok_ole_success(hr, CreateStreamOnHGlobal);
358 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
359 ok_ole_success(hr, CoMarshalInterface);
361 ok_more_than_one_lock();
363 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
364 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
365 ok_ole_success(hr, CoUnmarshalInterface);
366 IStream_Release(pStream);
368 ok_more_than_one_lock();
370 IUnknown_Release(pProxy);
375 /* tests failure case of unmarshaling a freed object */
376 static void test_marshal_and_unmarshal_invalid(void)
379 IStream *pStream = NULL;
380 IClassFactory *pProxy = NULL;
387 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
388 ok_ole_success(hr, CreateStreamOnHGlobal);
389 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
391 ok_more_than_one_lock();
393 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
394 hr = CoReleaseMarshalData(pStream);
395 ok_ole_success(hr, CoReleaseMarshalData);
399 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
400 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
401 todo_wine { ok_ole_success(hr, CoUnmarshalInterface); }
407 hr = IClassFactory_CreateInstance(pProxy, NULL, &IID_IUnknown, &dummy);
408 ok(hr == RPC_E_DISCONNECTED, "Remote call should have returned RPC_E_DISCONNECTED, instead of 0x%08x\n", hr);
410 IClassFactory_Release(pProxy);
413 IStream_Release(pStream);
415 end_host_object(tid, thread);
418 static void test_same_apartment_unmarshal_failure(void)
423 static const LARGE_INTEGER llZero;
427 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
428 ok_ole_success(hr, CreateStreamOnHGlobal);
430 hr = CoMarshalInterface(pStream, &IID_IUnknown, (IUnknown *)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
431 ok_ole_success(hr, CoMarshalInterface);
433 ok_more_than_one_lock();
435 hr = IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
436 ok_ole_success(hr, IStream_Seek);
438 hr = CoUnmarshalInterface(pStream, &IID_IParseDisplayName, (void **)&pProxy);
439 ok(hr == E_NOINTERFACE, "CoUnmarshalInterface should have returned E_NOINTERFACE instead of 0x%08x\n", hr);
443 IStream_Release(pStream);
446 /* tests success case of an interthread marshal */
447 static void test_interthread_marshal_and_unmarshal(void)
450 IStream *pStream = NULL;
451 IUnknown *pProxy = NULL;
457 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
458 ok_ole_success(hr, CreateStreamOnHGlobal);
459 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
461 ok_more_than_one_lock();
463 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
464 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
465 ok_ole_success(hr, CoUnmarshalInterface);
466 IStream_Release(pStream);
468 ok_more_than_one_lock();
470 IUnknown_Release(pProxy);
474 end_host_object(tid, thread);
477 /* the number of external references that Wine's proxy manager normally gives
478 * out, so we can test the border case of running out of references */
479 #define NORMALEXTREFS 5
481 /* tests success case of an interthread marshal and then marshaling the proxy */
482 static void test_proxy_marshal_and_unmarshal(void)
485 IStream *pStream = NULL;
486 IUnknown *pProxy = NULL;
487 IUnknown *pProxy2 = NULL;
494 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
495 ok_ole_success(hr, CreateStreamOnHGlobal);
496 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
498 ok_more_than_one_lock();
500 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
501 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
502 ok_ole_success(hr, CoUnmarshalInterface);
504 ok_more_than_one_lock();
506 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
507 /* marshal the proxy */
508 hr = CoMarshalInterface(pStream, &IID_IClassFactory, pProxy, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
509 ok_ole_success(hr, CoMarshalInterface);
511 ok_more_than_one_lock();
513 /* marshal 5 more times to exhaust the normal external references of 5 */
514 for (i = 0; i < NORMALEXTREFS; i++)
516 hr = CoMarshalInterface(pStream, &IID_IClassFactory, pProxy, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
517 ok_ole_success(hr, CoMarshalInterface);
520 ok_more_than_one_lock();
522 /* release the original proxy to test that we successfully keep the
523 * original object alive */
524 IUnknown_Release(pProxy);
526 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
527 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
528 ok_ole_success(hr, CoUnmarshalInterface);
530 ok_more_than_one_lock();
532 IUnknown_Release(pProxy2);
534 /* unmarshal all of the proxies to check that the object stub still exists */
535 for (i = 0; i < NORMALEXTREFS; i++)
537 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
538 ok_ole_success(hr, CoUnmarshalInterface);
540 IUnknown_Release(pProxy2);
545 IStream_Release(pStream);
547 end_host_object(tid, thread);
550 /* tests success case of an interthread marshal and then marshaling the proxy
551 * using an iid that hasn't previously been unmarshaled */
552 static void test_proxy_marshal_and_unmarshal2(void)
555 IStream *pStream = NULL;
556 IUnknown *pProxy = NULL;
557 IUnknown *pProxy2 = NULL;
563 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
564 ok_ole_success(hr, CreateStreamOnHGlobal);
565 tid = start_host_object(pStream, &IID_IUnknown, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
567 ok_more_than_one_lock();
569 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
570 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)&pProxy);
571 ok_ole_success(hr, CoUnmarshalInterface);
573 ok_more_than_one_lock();
575 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
576 /* marshal the proxy */
577 hr = CoMarshalInterface(pStream, &IID_IClassFactory, pProxy, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
578 ok_ole_success(hr, CoMarshalInterface);
580 ok_more_than_one_lock();
582 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
583 /* unmarshal the second proxy to the object */
584 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
585 ok_ole_success(hr, CoUnmarshalInterface);
586 IStream_Release(pStream);
588 /* now the proxies should be as follows:
589 * pProxy -> &Test_ClassFactory
590 * pProxy2 -> &Test_ClassFactory
591 * they should NOT be as follows:
592 * pProxy -> &Test_ClassFactory
594 * the above can only really be tested by looking in +ole traces
597 ok_more_than_one_lock();
599 IUnknown_Release(pProxy);
601 ok_more_than_one_lock();
603 IUnknown_Release(pProxy2);
607 end_host_object(tid, thread);
610 /* tests success case of an interthread marshal and then table-weak-marshaling the proxy */
611 static void test_proxy_marshal_and_unmarshal_weak(void)
614 IStream *pStream = NULL;
615 IUnknown *pProxy = NULL;
616 IUnknown *pProxy2 = NULL;
622 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
623 ok_ole_success(hr, CreateStreamOnHGlobal);
624 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
626 ok_more_than_one_lock();
628 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
629 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
630 ok_ole_success(hr, CoUnmarshalInterface);
632 ok_more_than_one_lock();
634 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
635 /* marshal the proxy */
636 hr = CoMarshalInterface(pStream, &IID_IClassFactory, pProxy, MSHCTX_INPROC, NULL, MSHLFLAGS_TABLEWEAK);
637 ok_ole_success(hr, CoMarshalInterface);
639 ok_more_than_one_lock();
641 /* release the original proxy to test that we successfully keep the
642 * original object alive */
643 IUnknown_Release(pProxy);
645 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
646 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
648 ok(hr == CO_E_OBJNOTREG, "CoUnmarshalInterface should return CO_E_OBJNOTREG instead of 0x%08x\n", hr);
652 IStream_Release(pStream);
654 end_host_object(tid, thread);
657 /* tests success case of an interthread marshal and then table-strong-marshaling the proxy */
658 static void test_proxy_marshal_and_unmarshal_strong(void)
661 IStream *pStream = NULL;
662 IUnknown *pProxy = NULL;
663 IUnknown *pProxy2 = NULL;
669 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
670 ok_ole_success(hr, CreateStreamOnHGlobal);
671 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
673 ok_more_than_one_lock();
675 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
676 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
677 ok_ole_success(hr, CoUnmarshalInterface);
679 ok_more_than_one_lock();
681 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
682 /* marshal the proxy */
683 hr = CoMarshalInterface(pStream, &IID_IClassFactory, pProxy, MSHCTX_INPROC, NULL, MSHLFLAGS_TABLESTRONG);
684 ok(hr == S_OK /* WinNT */ || hr == E_INVALIDARG /* Win9x */,
685 "CoMarshalInterface should have return S_OK or E_INVALIDARG instead of 0x%08x\n", hr);
688 IUnknown_Release(pProxy);
692 ok_more_than_one_lock();
694 /* release the original proxy to test that we successfully keep the
695 * original object alive */
696 IUnknown_Release(pProxy);
698 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
699 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
700 ok_ole_success(hr, CoUnmarshalInterface);
702 ok_more_than_one_lock();
704 IUnknown_Release(pProxy2);
706 ok_more_than_one_lock();
709 IStream_Release(pStream);
711 end_host_object(tid, thread);
716 /* tests that stubs are released when the containing apartment is destroyed */
717 static void test_marshal_stub_apartment_shutdown(void)
720 IStream *pStream = NULL;
721 IUnknown *pProxy = NULL;
727 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
728 ok_ole_success(hr, CreateStreamOnHGlobal);
729 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
731 ok_more_than_one_lock();
733 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
734 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
735 ok_ole_success(hr, CoUnmarshalInterface);
736 IStream_Release(pStream);
738 ok_more_than_one_lock();
740 end_host_object(tid, thread);
744 IUnknown_Release(pProxy);
749 /* tests that proxies are released when the containing apartment is destroyed */
750 static void test_marshal_proxy_apartment_shutdown(void)
753 IStream *pStream = NULL;
754 IUnknown *pProxy = NULL;
760 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
761 ok_ole_success(hr, CreateStreamOnHGlobal);
762 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
764 ok_more_than_one_lock();
766 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
767 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
768 ok_ole_success(hr, CoUnmarshalInterface);
769 IStream_Release(pStream);
771 ok_more_than_one_lock();
777 IUnknown_Release(pProxy);
781 end_host_object(tid, thread);
783 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
786 /* tests that proxies are released when the containing mta apartment is destroyed */
787 static void test_marshal_proxy_mta_apartment_shutdown(void)
790 IStream *pStream = NULL;
791 IUnknown *pProxy = NULL;
796 pCoInitializeEx(NULL, COINIT_MULTITHREADED);
800 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
801 ok_ole_success(hr, CreateStreamOnHGlobal);
802 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
804 ok_more_than_one_lock();
806 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
807 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
808 ok_ole_success(hr, CoUnmarshalInterface);
809 IStream_Release(pStream);
811 ok_more_than_one_lock();
817 IUnknown_Release(pProxy);
821 end_host_object(tid, thread);
823 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
829 HANDLE marshal_event;
830 HANDLE unmarshal_event;
833 /* helper for test_no_couninitialize_server */
834 static DWORD CALLBACK no_couninitialize_server_proc(LPVOID p)
836 struct ncu_params *ncu_params = p;
839 pCoInitializeEx(NULL, COINIT_MULTITHREADED);
841 hr = CoMarshalInterface(ncu_params->stream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
842 ok_ole_success(hr, CoMarshalInterface);
844 SetEvent(ncu_params->marshal_event);
846 ok( !WaitForSingleObject(ncu_params->unmarshal_event, 10000), "wait timed out\n" );
848 /* die without calling CoUninitialize */
853 /* tests apartment that an apartment with a stub is released without deadlock
854 * if the owning thread exits */
855 static void test_no_couninitialize_server(void)
858 IStream *pStream = NULL;
859 IUnknown *pProxy = NULL;
862 struct ncu_params ncu_params;
866 ncu_params.marshal_event = CreateEvent(NULL, TRUE, FALSE, NULL);
867 ncu_params.unmarshal_event = CreateEvent(NULL, TRUE, FALSE, NULL);
869 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
870 ok_ole_success(hr, CreateStreamOnHGlobal);
871 ncu_params.stream = pStream;
873 thread = CreateThread(NULL, 0, no_couninitialize_server_proc, &ncu_params, 0, &tid);
875 ok( !WaitForSingleObject(ncu_params.marshal_event, 10000), "wait timed out\n" );
876 ok_more_than_one_lock();
878 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
879 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
880 ok_ole_success(hr, CoUnmarshalInterface);
881 IStream_Release(pStream);
883 ok_more_than_one_lock();
885 SetEvent(ncu_params.unmarshal_event);
886 ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
891 CloseHandle(ncu_params.marshal_event);
892 CloseHandle(ncu_params.unmarshal_event);
894 IUnknown_Release(pProxy);
899 /* STA -> STA call during DLL_THREAD_DETACH */
900 static DWORD CALLBACK no_couninitialize_client_proc(LPVOID p)
902 struct ncu_params *ncu_params = p;
904 IUnknown *pProxy = NULL;
906 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
908 hr = CoUnmarshalInterface(ncu_params->stream, &IID_IClassFactory, (void **)&pProxy);
909 ok_ole_success(hr, CoUnmarshalInterface);
910 IStream_Release(ncu_params->stream);
912 ok_more_than_one_lock();
914 /* die without calling CoUninitialize */
919 /* tests STA -> STA call during DLL_THREAD_DETACH doesn't deadlock */
920 static void test_no_couninitialize_client(void)
923 IStream *pStream = NULL;
928 struct ncu_params ncu_params;
932 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
933 ok_ole_success(hr, CreateStreamOnHGlobal);
934 ncu_params.stream = pStream;
936 /* NOTE: assumes start_host_object uses an STA to host the object, as MTAs
937 * always deadlock when called from within DllMain */
938 host_tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown *)&Test_ClassFactory, MSHLFLAGS_NORMAL, &host_thread);
939 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
941 ok_more_than_one_lock();
943 thread = CreateThread(NULL, 0, no_couninitialize_client_proc, &ncu_params, 0, &tid);
945 ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
950 end_host_object(host_tid, host_thread);
953 /* tests success case of a same-thread table-weak marshal, unmarshal, unmarshal */
954 static void test_tableweak_marshal_and_unmarshal_twice(void)
957 IStream *pStream = NULL;
958 IUnknown *pProxy1 = NULL;
959 IUnknown *pProxy2 = NULL;
965 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
966 ok_ole_success(hr, CreateStreamOnHGlobal);
967 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_TABLEWEAK, &thread);
969 ok_more_than_one_lock();
971 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
972 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy1);
973 ok_ole_success(hr, CoUnmarshalInterface);
975 ok_more_than_one_lock();
977 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
978 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
979 IStream_Release(pStream);
980 ok_ole_success(hr, CoUnmarshalInterface);
982 ok_more_than_one_lock();
984 IUnknown_Release(pProxy1);
985 IUnknown_Release(pProxy2);
987 /* this line is shows the difference between weak and strong table marshaling:
988 * weak has cLocks == 0
989 * strong has cLocks > 0 */
992 end_host_object(tid, thread);
995 /* tests releasing after unmarshaling one object */
996 static void test_tableweak_marshal_releasedata1(void)
999 IStream *pStream = NULL;
1000 IUnknown *pProxy1 = NULL;
1001 IUnknown *pProxy2 = NULL;
1007 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1008 ok_ole_success(hr, CreateStreamOnHGlobal);
1009 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_TABLEWEAK, &thread);
1011 ok_more_than_one_lock();
1013 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1014 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy1);
1015 ok_ole_success(hr, CoUnmarshalInterface);
1017 ok_more_than_one_lock();
1019 /* release the remaining reference on the object by calling
1020 * CoReleaseMarshalData in the hosting thread */
1021 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1022 release_host_object(tid);
1024 ok_more_than_one_lock();
1026 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1027 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
1028 ok_ole_success(hr, CoUnmarshalInterface);
1029 IStream_Release(pStream);
1031 ok_more_than_one_lock();
1033 IUnknown_Release(pProxy1);
1035 IUnknown_Release(pProxy2);
1037 /* this line is shows the difference between weak and strong table marshaling:
1038 * weak has cLocks == 0
1039 * strong has cLocks > 0 */
1042 end_host_object(tid, thread);
1045 /* tests releasing after unmarshaling one object */
1046 static void test_tableweak_marshal_releasedata2(void)
1049 IStream *pStream = NULL;
1050 IUnknown *pProxy = NULL;
1056 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1057 ok_ole_success(hr, CreateStreamOnHGlobal);
1058 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_TABLEWEAK, &thread);
1060 ok_more_than_one_lock();
1062 /* release the remaining reference on the object by calling
1063 * CoReleaseMarshalData in the hosting thread */
1064 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1065 release_host_object(tid);
1069 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1070 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
1073 ok(hr == CO_E_OBJNOTREG,
1074 "CoUnmarshalInterface should have failed with CO_E_OBJNOTREG, but returned 0x%08x instead\n",
1077 IStream_Release(pStream);
1081 end_host_object(tid, thread);
1084 struct weak_and_normal_marshal_data
1086 IStream *pStreamWeak;
1087 IStream *pStreamNormal;
1092 static DWORD CALLBACK weak_and_normal_marshal_thread_proc(void *p)
1095 struct weak_and_normal_marshal_data *data = p;
1096 HANDLE hQuitEvent = data->hQuitEvent;
1099 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1101 hr = CoMarshalInterface(data->pStreamWeak, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_TABLEWEAK);
1102 ok_ole_success(hr, "CoMarshalInterface");
1104 hr = CoMarshalInterface(data->pStreamNormal, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1105 ok_ole_success(hr, "CoMarshalInterface");
1107 /* force the message queue to be created before signaling parent thread */
1108 PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
1110 SetEvent(data->hReadyEvent);
1112 while (WAIT_OBJECT_0 + 1 == MsgWaitForMultipleObjects(1, &hQuitEvent, FALSE, 10000, QS_ALLINPUT))
1114 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
1115 DispatchMessage(&msg);
1117 CloseHandle(hQuitEvent);
1124 /* tests interaction between table-weak and normal marshalling of an object */
1125 static void test_tableweak_and_normal_marshal_and_unmarshal(void)
1128 IUnknown *pProxyWeak = NULL;
1129 IUnknown *pProxyNormal = NULL;
1132 struct weak_and_normal_marshal_data data;
1136 data.hReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
1137 data.hQuitEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
1138 hr = CreateStreamOnHGlobal(NULL, TRUE, &data.pStreamWeak);
1139 ok_ole_success(hr, CreateStreamOnHGlobal);
1140 hr = CreateStreamOnHGlobal(NULL, TRUE, &data.pStreamNormal);
1141 ok_ole_success(hr, CreateStreamOnHGlobal);
1143 thread = CreateThread(NULL, 0, weak_and_normal_marshal_thread_proc, &data, 0, &tid);
1144 ok( !WaitForSingleObject(data.hReadyEvent, 10000), "wait timed out\n" );
1145 CloseHandle(data.hReadyEvent);
1147 ok_more_than_one_lock();
1149 IStream_Seek(data.pStreamWeak, ullZero, STREAM_SEEK_SET, NULL);
1150 hr = CoUnmarshalInterface(data.pStreamWeak, &IID_IClassFactory, (void **)&pProxyWeak);
1151 ok_ole_success(hr, CoUnmarshalInterface);
1153 ok_more_than_one_lock();
1155 IStream_Seek(data.pStreamNormal, ullZero, STREAM_SEEK_SET, NULL);
1156 hr = CoUnmarshalInterface(data.pStreamNormal, &IID_IClassFactory, (void **)&pProxyNormal);
1157 ok_ole_success(hr, CoUnmarshalInterface);
1159 ok_more_than_one_lock();
1161 IUnknown_Release(pProxyNormal);
1163 ok_more_than_one_lock();
1165 IUnknown_Release(pProxyWeak);
1169 IStream_Release(data.pStreamWeak);
1170 IStream_Release(data.pStreamNormal);
1172 SetEvent(data.hQuitEvent);
1173 ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
1174 CloseHandle(thread);
1177 /* tests success case of a same-thread table-strong marshal, unmarshal, unmarshal */
1178 static void test_tablestrong_marshal_and_unmarshal_twice(void)
1181 IStream *pStream = NULL;
1182 IUnknown *pProxy1 = NULL;
1183 IUnknown *pProxy2 = NULL;
1189 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1190 ok_ole_success(hr, CreateStreamOnHGlobal);
1191 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_TABLESTRONG, &thread);
1193 ok_more_than_one_lock();
1195 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1196 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy1);
1197 ok_ole_success(hr, CoUnmarshalInterface);
1199 ok_more_than_one_lock();
1201 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1202 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
1203 ok_ole_success(hr, CoUnmarshalInterface);
1205 ok_more_than_one_lock();
1207 if (pProxy1) IUnknown_Release(pProxy1);
1208 if (pProxy2) IUnknown_Release(pProxy2);
1210 /* this line is shows the difference between weak and strong table marshaling:
1211 * weak has cLocks == 0
1212 * strong has cLocks > 0 */
1213 ok_more_than_one_lock();
1215 /* release the remaining reference on the object by calling
1216 * CoReleaseMarshalData in the hosting thread */
1217 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1218 release_host_object(tid);
1219 IStream_Release(pStream);
1223 end_host_object(tid, thread);
1226 /* tests CoLockObjectExternal */
1227 static void test_lock_object_external(void)
1230 IStream *pStream = NULL;
1234 /* test the stub manager creation aspect of CoLockObjectExternal when the
1235 * object hasn't been marshaled yet */
1236 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, TRUE, TRUE);
1238 ok_more_than_one_lock();
1240 CoDisconnectObject((IUnknown*)&Test_ClassFactory, 0);
1244 /* test our empty stub manager being handled correctly in
1245 * CoMarshalInterface */
1246 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, TRUE, TRUE);
1248 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1249 ok_ole_success(hr, CreateStreamOnHGlobal);
1250 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1251 ok_ole_success(hr, CoMarshalInterface);
1253 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, TRUE, TRUE);
1255 ok_more_than_one_lock();
1257 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1258 hr = CoReleaseMarshalData(pStream);
1259 ok_ole_success(hr, CoReleaseMarshalData);
1260 IStream_Release(pStream);
1262 ok_more_than_one_lock();
1264 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, FALSE, TRUE);
1266 ok_more_than_one_lock();
1268 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, FALSE, TRUE);
1273 /* tests disconnecting stubs */
1274 static void test_disconnect_stub(void)
1277 IStream *pStream = NULL;
1281 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1282 ok_ole_success(hr, CreateStreamOnHGlobal);
1283 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1284 ok_ole_success(hr, CoMarshalInterface);
1286 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, TRUE, TRUE);
1288 ok_more_than_one_lock();
1290 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1291 hr = CoReleaseMarshalData(pStream);
1292 ok_ole_success(hr, CoReleaseMarshalData);
1293 IStream_Release(pStream);
1295 ok_more_than_one_lock();
1297 CoDisconnectObject((IUnknown*)&Test_ClassFactory, 0);
1302 /* tests failure case of a same-thread marshal and unmarshal twice */
1303 static void test_normal_marshal_and_unmarshal_twice(void)
1306 IStream *pStream = NULL;
1307 IUnknown *pProxy1 = NULL;
1308 IUnknown *pProxy2 = NULL;
1312 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1313 ok_ole_success(hr, CreateStreamOnHGlobal);
1314 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1315 ok_ole_success(hr, CoMarshalInterface);
1317 ok_more_than_one_lock();
1319 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1320 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy1);
1321 ok_ole_success(hr, CoUnmarshalInterface);
1323 ok_more_than_one_lock();
1325 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1326 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
1327 ok(hr == CO_E_OBJNOTCONNECTED,
1328 "CoUnmarshalInterface should have failed with error CO_E_OBJNOTCONNECTED for double unmarshal, instead of 0x%08x\n", hr);
1330 IStream_Release(pStream);
1332 ok_more_than_one_lock();
1334 IUnknown_Release(pProxy1);
1339 /* tests success case of marshaling and unmarshaling an HRESULT */
1340 static void test_hresult_marshaling(void)
1343 HRESULT hr_marshaled = 0;
1344 IStream *pStream = NULL;
1345 static const HRESULT E_DEADBEEF = 0xdeadbeef;
1347 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1348 ok_ole_success(hr, CreateStreamOnHGlobal);
1350 hr = CoMarshalHresult(pStream, E_DEADBEEF);
1351 ok_ole_success(hr, CoMarshalHresult);
1353 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1354 hr = IStream_Read(pStream, &hr_marshaled, sizeof(HRESULT), NULL);
1355 ok_ole_success(hr, IStream_Read);
1357 ok(hr_marshaled == E_DEADBEEF, "Didn't marshal HRESULT as expected: got value 0x%08x instead\n", hr_marshaled);
1360 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1361 hr = CoUnmarshalHresult(pStream, &hr_marshaled);
1362 ok_ole_success(hr, CoUnmarshalHresult);
1364 ok(hr_marshaled == E_DEADBEEF, "Didn't marshal HRESULT as expected: got value 0x%08x instead\n", hr_marshaled);
1366 IStream_Release(pStream);
1370 /* helper for test_proxy_used_in_wrong_thread */
1371 static DWORD CALLBACK bad_thread_proc(LPVOID p)
1373 IClassFactory * cf = p;
1375 IUnknown * proxy = NULL;
1377 hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
1379 ok(hr == CO_E_NOTINITIALIZED,
1380 "COM should have failed with CO_E_NOTINITIALIZED on using proxy without apartment, but instead returned 0x%08x\n",
1383 hr = IClassFactory_QueryInterface(cf, &IID_IMultiQI, (LPVOID *)&proxy);
1384 /* Win9x returns S_OK, whilst NT returns RPC_E_WRONG_THREAD */
1385 trace("call to proxy's QueryInterface for local interface without apartment returned 0x%08x\n", hr);
1387 IUnknown_Release(proxy);
1389 hr = IClassFactory_QueryInterface(cf, &IID_IStream, (LPVOID *)&proxy);
1390 /* Win9x returns E_NOINTERFACE, whilst NT returns RPC_E_WRONG_THREAD */
1391 trace("call to proxy's QueryInterface without apartment returned 0x%08x\n", hr);
1393 IUnknown_Release(proxy);
1395 pCoInitializeEx(NULL, COINIT_MULTITHREADED);
1397 hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
1398 if (proxy) IUnknown_Release(proxy);
1399 ok(hr == RPC_E_WRONG_THREAD,
1400 "COM should have failed with RPC_E_WRONG_THREAD on using proxy from wrong apartment, but instead returned 0x%08x\n",
1403 hr = IClassFactory_QueryInterface(cf, &IID_IStream, (LPVOID *)&proxy);
1404 /* Win9x returns E_NOINTERFACE, whilst NT returns RPC_E_WRONG_THREAD */
1405 trace("call to proxy's QueryInterface from wrong apartment returned 0x%08x\n", hr);
1407 /* this statement causes Win9x DCOM to crash during CoUninitialize of
1408 * other apartment, so don't test this on Win9x (signified by NT-only
1409 * export of CoRegisterSurrogateEx) */
1410 if (GetProcAddress(GetModuleHandle("ole32"), "CoRegisterSurrogateEx"))
1411 /* now be really bad and release the proxy from the wrong apartment */
1412 IUnknown_Release(cf);
1414 skip("skipping test for releasing proxy from wrong apartment that will succeed, but cause a crash during CoUninitialize\n");
1421 /* tests failure case of a using a proxy in the wrong apartment */
1422 static void test_proxy_used_in_wrong_thread(void)
1425 IStream *pStream = NULL;
1426 IUnknown *pProxy = NULL;
1433 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1434 ok_ole_success(hr, CreateStreamOnHGlobal);
1435 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &host_thread);
1437 ok_more_than_one_lock();
1439 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1440 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
1441 ok_ole_success(hr, CoUnmarshalInterface);
1442 IStream_Release(pStream);
1444 ok_more_than_one_lock();
1446 /* do a call that will fail, but result in IRemUnknown being used by the proxy */
1447 IClassFactory_QueryInterface(pProxy, &IID_IStream, (LPVOID *)&pStream);
1449 /* create a thread that we can misbehave in */
1450 thread = CreateThread(NULL, 0, bad_thread_proc, pProxy, 0, &tid2);
1452 ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
1453 CloseHandle(thread);
1455 /* do release statement on Win9x that we should have done above */
1456 if (!GetProcAddress(GetModuleHandle("ole32"), "CoRegisterSurrogateEx"))
1457 IUnknown_Release(pProxy);
1461 end_host_object(tid, host_thread);
1464 static HRESULT WINAPI MessageFilter_QueryInterface(IMessageFilter *iface, REFIID riid, void ** ppvObj)
1466 if (ppvObj == NULL) return E_POINTER;
1468 if (IsEqualGUID(riid, &IID_IUnknown) ||
1469 IsEqualGUID(riid, &IID_IClassFactory))
1472 IClassFactory_AddRef(iface);
1476 return E_NOINTERFACE;
1479 static ULONG WINAPI MessageFilter_AddRef(IMessageFilter *iface)
1481 return 2; /* non-heap object */
1484 static ULONG WINAPI MessageFilter_Release(IMessageFilter *iface)
1486 return 1; /* non-heap object */
1489 static DWORD WINAPI MessageFilter_HandleInComingCall(
1490 IMessageFilter *iface,
1492 HTASK threadIDCaller,
1494 LPINTERFACEINFO lpInterfaceInfo)
1496 static int callcount = 0;
1498 trace("HandleInComingCall\n");
1502 ret = SERVERCALL_REJECTED;
1505 ret = SERVERCALL_RETRYLATER;
1508 ret = SERVERCALL_ISHANDLED;
1515 static DWORD WINAPI MessageFilter_RetryRejectedCall(
1516 IMessageFilter *iface,
1517 HTASK threadIDCallee,
1521 trace("RetryRejectedCall\n");
1525 static DWORD WINAPI MessageFilter_MessagePending(
1526 IMessageFilter *iface,
1527 HTASK threadIDCallee,
1529 DWORD dwPendingType)
1531 trace("MessagePending\n");
1532 return PENDINGMSG_WAITNOPROCESS;
1535 static const IMessageFilterVtbl MessageFilter_Vtbl =
1537 MessageFilter_QueryInterface,
1538 MessageFilter_AddRef,
1539 MessageFilter_Release,
1540 MessageFilter_HandleInComingCall,
1541 MessageFilter_RetryRejectedCall,
1542 MessageFilter_MessagePending
1545 static IMessageFilter MessageFilter = { &MessageFilter_Vtbl };
1547 static void test_message_filter(void)
1550 IStream *pStream = NULL;
1551 IClassFactory *cf = NULL;
1553 IUnknown *proxy = NULL;
1554 IMessageFilter *prev_filter = NULL;
1559 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1560 ok_ole_success(hr, CreateStreamOnHGlobal);
1561 tid = start_host_object2(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &MessageFilter, &thread);
1563 ok_more_than_one_lock();
1565 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1566 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&cf);
1567 ok_ole_success(hr, CoUnmarshalInterface);
1568 IStream_Release(pStream);
1570 ok_more_than_one_lock();
1572 hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
1573 ok(hr == RPC_E_CALL_REJECTED, "Call should have returned RPC_E_CALL_REJECTED, but return 0x%08x instead\n", hr);
1574 if (proxy) IUnknown_Release(proxy);
1577 hr = CoRegisterMessageFilter(&MessageFilter, &prev_filter);
1578 ok_ole_success(hr, CoRegisterMessageFilter);
1580 hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
1581 ok_ole_success(hr, IClassFactory_CreateInstance);
1583 IUnknown_Release(proxy);
1585 IClassFactory_Release(cf);
1589 end_host_object(tid, thread);
1591 hr = CoRegisterMessageFilter(prev_filter, NULL);
1592 ok_ole_success(hr, CoRegisterMessageFilter);
1595 /* test failure case of trying to unmarshal from bad stream */
1596 static void test_bad_marshal_stream(void)
1599 IStream *pStream = NULL;
1601 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1602 ok_ole_success(hr, CreateStreamOnHGlobal);
1603 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1604 ok_ole_success(hr, CoMarshalInterface);
1606 ok_more_than_one_lock();
1608 /* try to read beyond end of stream */
1609 hr = CoReleaseMarshalData(pStream);
1610 ok(hr == STG_E_READFAULT, "Should have failed with STG_E_READFAULT, but returned 0x%08x instead\n", hr);
1612 /* now release for real */
1613 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1614 hr = CoReleaseMarshalData(pStream);
1615 ok_ole_success(hr, CoReleaseMarshalData);
1617 IStream_Release(pStream);
1620 /* tests that proxies implement certain interfaces */
1621 static void test_proxy_interfaces(void)
1624 IStream *pStream = NULL;
1625 IUnknown *pProxy = NULL;
1626 IUnknown *pOtherUnknown = NULL;
1632 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1633 ok_ole_success(hr, CreateStreamOnHGlobal);
1634 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
1636 ok_more_than_one_lock();
1638 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1639 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)&pProxy);
1640 ok_ole_success(hr, CoUnmarshalInterface);
1641 IStream_Release(pStream);
1643 ok_more_than_one_lock();
1645 hr = IUnknown_QueryInterface(pProxy, &IID_IUnknown, (LPVOID*)&pOtherUnknown);
1646 ok_ole_success(hr, IUnknown_QueryInterface IID_IUnknown);
1647 if (hr == S_OK) IUnknown_Release(pOtherUnknown);
1649 hr = IUnknown_QueryInterface(pProxy, &IID_IClientSecurity, (LPVOID*)&pOtherUnknown);
1650 ok_ole_success(hr, IUnknown_QueryInterface IID_IClientSecurity);
1651 if (hr == S_OK) IUnknown_Release(pOtherUnknown);
1653 hr = IUnknown_QueryInterface(pProxy, &IID_IMultiQI, (LPVOID*)&pOtherUnknown);
1654 ok_ole_success(hr, IUnknown_QueryInterface IID_IMultiQI);
1655 if (hr == S_OK) IUnknown_Release(pOtherUnknown);
1657 hr = IUnknown_QueryInterface(pProxy, &IID_IMarshal, (LPVOID*)&pOtherUnknown);
1658 ok_ole_success(hr, IUnknown_QueryInterface IID_IMarshal);
1659 if (hr == S_OK) IUnknown_Release(pOtherUnknown);
1661 /* IMarshal2 is also supported on NT-based systems, but is pretty much
1662 * useless as it has no more methods over IMarshal that it inherits from. */
1664 IUnknown_Release(pProxy);
1668 end_host_object(tid, thread);
1673 const IUnknownVtbl *lpVtbl;
1677 static HRESULT WINAPI HeapUnknown_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
1679 if (IsEqualIID(riid, &IID_IUnknown))
1681 IUnknown_AddRef(iface);
1686 return E_NOINTERFACE;
1689 static ULONG WINAPI HeapUnknown_AddRef(IUnknown *iface)
1691 HeapUnknown *This = (HeapUnknown *)iface;
1692 return InterlockedIncrement((LONG*)&This->refs);
1695 static ULONG WINAPI HeapUnknown_Release(IUnknown *iface)
1697 HeapUnknown *This = (HeapUnknown *)iface;
1698 ULONG refs = InterlockedDecrement((LONG*)&This->refs);
1699 if (!refs) HeapFree(GetProcessHeap(), 0, This);
1703 static const IUnknownVtbl HeapUnknown_Vtbl =
1705 HeapUnknown_QueryInterface,
1710 static void test_proxybuffer(REFIID riid)
1713 IPSFactoryBuffer *psfb;
1714 IRpcProxyBuffer *proxy;
1718 HeapUnknown *pUnkOuter = HeapAlloc(GetProcessHeap(), 0, sizeof(*pUnkOuter));
1720 pUnkOuter->lpVtbl = &HeapUnknown_Vtbl;
1721 pUnkOuter->refs = 1;
1723 hr = CoGetPSClsid(riid, &clsid);
1724 ok_ole_success(hr, CoGetPSClsid);
1726 hr = CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL, &IID_IPSFactoryBuffer, (LPVOID*)&psfb);
1727 ok_ole_success(hr, CoGetClassObject);
1729 hr = IPSFactoryBuffer_CreateProxy(psfb, (IUnknown*)pUnkOuter, riid, &proxy, &lpvtbl);
1730 ok_ole_success(hr, IPSFactoryBuffer_CreateProxy);
1731 ok(lpvtbl != NULL, "IPSFactoryBuffer_CreateProxy succeeded, but returned a NULL vtable!\n");
1733 /* release our reference to the outer unknown object - the PS factory
1734 * buffer will have AddRef's it in the CreateProxy call */
1735 refs = IUnknown_Release((IUnknown *)pUnkOuter);
1736 ok(refs == 1, "Ref count of outer unknown should have been 1 instead of %d\n", refs);
1738 refs = IPSFactoryBuffer_Release(psfb);
1741 /* not reliable on native. maybe it leaks references! */
1742 ok(refs == 0, "Ref-count leak of %d on IPSFactoryBuffer\n", refs);
1745 refs = IUnknown_Release((IUnknown *)lpvtbl);
1746 ok(refs == 0, "Ref-count leak of %d on IRpcProxyBuffer\n", refs);
1748 refs = IRpcProxyBuffer_Release(proxy);
1749 ok(refs == 0, "Ref-count leak of %d on IRpcProxyBuffer\n", refs);
1752 static void test_stubbuffer(REFIID riid)
1755 IPSFactoryBuffer *psfb;
1756 IRpcStubBuffer *stub;
1762 hr = CoGetPSClsid(riid, &clsid);
1763 ok_ole_success(hr, CoGetPSClsid);
1765 hr = CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL, &IID_IPSFactoryBuffer, (LPVOID*)&psfb);
1766 ok_ole_success(hr, CoGetClassObject);
1768 hr = IPSFactoryBuffer_CreateStub(psfb, riid, (IUnknown*)&Test_ClassFactory, &stub);
1769 ok_ole_success(hr, IPSFactoryBuffer_CreateStub);
1771 refs = IPSFactoryBuffer_Release(psfb);
1774 /* not reliable on native. maybe it leaks references */
1775 ok(refs == 0, "Ref-count leak of %d on IPSFactoryBuffer\n", refs);
1778 ok_more_than_one_lock();
1780 IRpcStubBuffer_Disconnect(stub);
1784 refs = IRpcStubBuffer_Release(stub);
1785 ok(refs == 0, "Ref-count leak of %d on IRpcProxyBuffer\n", refs);
1788 static HWND hwnd_app;
1790 static HRESULT WINAPI TestRE_IClassFactory_CreateInstance(
1791 LPCLASSFACTORY iface,
1792 LPUNKNOWN pUnkOuter,
1797 if (IsEqualIID(riid, &IID_IWineTest))
1799 BOOL ret = SendMessageTimeout(hwnd_app, WM_NULL, 0, 0, SMTO_BLOCK, 5000, &res);
1800 ok(ret, "Timed out sending a message to originating window during RPC call\n");
1806 static const IClassFactoryVtbl TestREClassFactory_Vtbl =
1808 Test_IClassFactory_QueryInterface,
1809 Test_IClassFactory_AddRef,
1810 Test_IClassFactory_Release,
1811 TestRE_IClassFactory_CreateInstance,
1812 Test_IClassFactory_LockServer
1815 IClassFactory TestRE_ClassFactory = { &TestREClassFactory_Vtbl };
1817 static LRESULT CALLBACK window_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1824 IStream *pStream = NULL;
1825 IClassFactory *proxy = NULL;
1832 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1833 ok_ole_success(hr, CreateStreamOnHGlobal);
1834 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&TestRE_ClassFactory, MSHLFLAGS_NORMAL, &thread);
1836 ok_more_than_one_lock();
1838 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1839 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&proxy);
1840 ok_ole_success(hr, CoReleaseMarshalData);
1841 IStream_Release(pStream);
1843 ok_more_than_one_lock();
1845 /* note the use of the magic IID_IWineTest value to tell remote thread
1846 * to try to send a message back to us */
1847 hr = IClassFactory_CreateInstance(proxy, NULL, &IID_IWineTest, (void **)&object);
1849 IClassFactory_Release(proxy);
1853 end_host_object(tid, thread);
1855 PostMessage(hwnd, WM_QUIT, 0, 0);
1862 IStream *pStream = NULL;
1863 IClassFactory *proxy = NULL;
1870 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1871 ok_ole_success(hr, CreateStreamOnHGlobal);
1872 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&TestRE_ClassFactory, MSHLFLAGS_NORMAL, &thread);
1874 ok_more_than_one_lock();
1876 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1877 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&proxy);
1878 ok_ole_success(hr, CoReleaseMarshalData);
1879 IStream_Release(pStream);
1881 ok_more_than_one_lock();
1883 /* post quit message before a doing a COM call to show that a pending
1884 * WM_QUIT message doesn't stop the call from succeeding */
1885 PostMessage(hwnd, WM_QUIT, 0, 0);
1886 hr = IClassFactory_CreateInstance(proxy, NULL, &IID_IUnknown, (void **)&object);
1888 IClassFactory_Release(proxy);
1892 end_host_object(tid, thread);
1899 IStream *pStream = NULL;
1900 IClassFactory *proxy = NULL;
1905 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1906 ok_ole_success(hr, CreateStreamOnHGlobal);
1907 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
1909 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1910 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&proxy);
1911 ok_ole_success(hr, CoReleaseMarshalData);
1912 IStream_Release(pStream);
1914 /* shows that COM calls executed during the processing of sent
1915 * messages should fail */
1916 hr = IClassFactory_CreateInstance(proxy, NULL, &IID_IUnknown, (void **)&object);
1917 ok(hr == RPC_E_CANTCALLOUT_ININPUTSYNCCALL,
1918 "COM call during processing of sent message should return RPC_E_CANTCALLOUT_ININPUTSYNCCALL instead of 0x%08x\n", hr);
1920 IClassFactory_Release(proxy);
1922 end_host_object(tid, thread);
1929 return DefWindowProc(hwnd, msg, wparam, lparam);
1933 static void register_test_window(void)
1937 memset(&wndclass, 0, sizeof(wndclass));
1938 wndclass.lpfnWndProc = window_proc;
1939 wndclass.lpszClassName = "WineCOMTest";
1940 RegisterClass(&wndclass);
1943 static void test_message_reentrancy(void)
1947 hwnd_app = CreateWindow("WineCOMTest", NULL, 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, NULL, 0);
1948 ok(hwnd_app != NULL, "Window creation failed\n");
1950 /* start message re-entrancy test */
1951 PostMessage(hwnd_app, WM_USER, 0, 0);
1953 while (GetMessage(&msg, NULL, 0, 0))
1955 TranslateMessage(&msg);
1956 DispatchMessage(&msg);
1958 DestroyWindow(hwnd_app);
1961 static HRESULT WINAPI TestMsg_IClassFactory_CreateInstance(
1962 LPCLASSFACTORY iface,
1963 LPUNKNOWN pUnkOuter,
1968 SendMessage(hwnd_app, WM_USER+2, 0, 0);
1972 static IClassFactoryVtbl TestMsgClassFactory_Vtbl =
1974 Test_IClassFactory_QueryInterface,
1975 Test_IClassFactory_AddRef,
1976 Test_IClassFactory_Release,
1977 TestMsg_IClassFactory_CreateInstance,
1978 Test_IClassFactory_LockServer
1981 IClassFactory TestMsg_ClassFactory = { &TestMsgClassFactory_Vtbl };
1983 static void test_call_from_message(void)
1988 IClassFactory *proxy;
1993 hwnd_app = CreateWindow("WineCOMTest", NULL, 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, NULL, 0);
1994 ok(hwnd_app != NULL, "Window creation failed\n");
1996 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1997 ok_ole_success(hr, CreateStreamOnHGlobal);
1998 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&TestMsg_ClassFactory, MSHLFLAGS_NORMAL, &thread);
2000 ok_more_than_one_lock();
2002 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
2003 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&proxy);
2004 ok_ole_success(hr, CoReleaseMarshalData);
2005 IStream_Release(pStream);
2007 ok_more_than_one_lock();
2009 /* start message re-entrancy test */
2010 hr = IClassFactory_CreateInstance(proxy, NULL, &IID_IUnknown, (void **)&object);
2011 ok_ole_success(hr, IClassFactory_CreateInstance);
2013 IClassFactory_Release(proxy);
2017 end_host_object(tid, thread);
2019 while (GetMessage(&msg, NULL, 0, 0))
2021 TranslateMessage(&msg);
2022 DispatchMessage(&msg);
2024 DestroyWindow(hwnd_app);
2027 static void test_WM_QUIT_handling(void)
2031 hwnd_app = CreateWindow("WineCOMTest", NULL, 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, NULL, 0);
2032 ok(hwnd_app != NULL, "Window creation failed\n");
2034 /* start WM_QUIT handling test */
2035 PostMessage(hwnd_app, WM_USER+1, 0, 0);
2037 while (GetMessage(&msg, NULL, 0, 0))
2039 TranslateMessage(&msg);
2040 DispatchMessage(&msg);
2044 static SIZE_T round_global_size(SIZE_T size)
2046 static SIZE_T global_size_alignment = -1;
2047 if (global_size_alignment == -1)
2049 void *p = GlobalAlloc(GMEM_FIXED, 1);
2050 global_size_alignment = GlobalSize(p);
2054 return ((size + global_size_alignment - 1) & ~(global_size_alignment - 1));
2057 static void test_freethreadedmarshaldata(IStream *pStream, MSHCTX mshctx, void *ptr, DWORD mshlflags)
2064 hr = GetHGlobalFromStream(pStream, &hglobal);
2065 ok_ole_success(hr, GetHGlobalFromStream);
2067 size = GlobalSize(hglobal);
2069 marshal_data = GlobalLock(hglobal);
2071 if (mshctx == MSHCTX_INPROC)
2073 DWORD expected_size = round_global_size(3*sizeof(DWORD) + sizeof(GUID));
2074 ok(size == expected_size ||
2075 broken(size == (2*sizeof(DWORD))) /* Win9x & NT4 */,
2076 "size should have been %d instead of %d\n", expected_size, size);
2078 ok(*(DWORD *)marshal_data == mshlflags, "expected 0x%x, but got 0x%x for mshctx\n", mshlflags, *(DWORD *)marshal_data);
2079 marshal_data += sizeof(DWORD);
2080 ok(*(void **)marshal_data == ptr, "expected %p, but got %p for mshctx\n", ptr, *(void **)marshal_data);
2081 marshal_data += sizeof(void *);
2082 if (sizeof(void*) == 4 && size >= 3*sizeof(DWORD))
2084 ok(*(DWORD *)marshal_data == 0, "expected 0x0, but got 0x%x\n", *(DWORD *)marshal_data);
2085 marshal_data += sizeof(DWORD);
2087 if (size >= 3*sizeof(DWORD) + sizeof(GUID))
2089 trace("got guid data: {%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\n",
2090 ((GUID *)marshal_data)->Data1, ((GUID *)marshal_data)->Data2, ((GUID *)marshal_data)->Data3,
2091 ((GUID *)marshal_data)->Data4[0], ((GUID *)marshal_data)->Data4[1], ((GUID *)marshal_data)->Data4[2], ((GUID *)marshal_data)->Data4[3],
2092 ((GUID *)marshal_data)->Data4[4], ((GUID *)marshal_data)->Data4[5], ((GUID *)marshal_data)->Data4[6], ((GUID *)marshal_data)->Data4[7]);
2097 ok(size > sizeof(DWORD), "size should have been > sizeof(DWORD), not %d\n", size);
2098 ok(*(DWORD *)marshal_data == 0x574f454d /* MEOW */,
2099 "marshal data should be filled by standard marshal and start with MEOW signature\n");
2102 GlobalUnlock(hglobal);
2105 static void test_freethreadedmarshaler(void)
2108 IUnknown *pFTUnknown;
2109 IMarshal *pFTMarshal;
2112 static const LARGE_INTEGER llZero;
2115 hr = CoCreateFreeThreadedMarshaler(NULL, &pFTUnknown);
2116 ok_ole_success(hr, CoCreateFreeThreadedMarshaler);
2117 hr = IUnknown_QueryInterface(pFTUnknown, &IID_IMarshal, (void **)&pFTMarshal);
2118 ok_ole_success(hr, IUnknown_QueryInterface);
2119 IUnknown_Release(pFTUnknown);
2121 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
2122 ok_ole_success(hr, CreateStreamOnHGlobal);
2124 /* inproc normal marshaling */
2126 hr = IMarshal_MarshalInterface(pFTMarshal, pStream, &IID_IClassFactory,
2127 &Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
2128 ok_ole_success(hr, IMarshal_MarshalInterface);
2130 ok_more_than_one_lock();
2132 test_freethreadedmarshaldata(pStream, MSHCTX_INPROC, &Test_ClassFactory, MSHLFLAGS_NORMAL);
2134 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2135 hr = IMarshal_UnmarshalInterface(pFTMarshal, pStream, &IID_IUnknown, (void **)&pProxy);
2136 ok_ole_success(hr, IMarshal_UnmarshalInterface);
2138 IUnknown_Release(pProxy);
2142 /* native doesn't allow us to unmarshal or release the stream data,
2143 * presumably because it wants us to call CoMarshalInterface instead */
2146 /* local normal marshaling */
2148 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2149 hr = IMarshal_MarshalInterface(pFTMarshal, pStream, &IID_IClassFactory, &Test_ClassFactory, MSHCTX_LOCAL, NULL, MSHLFLAGS_NORMAL);
2150 ok_ole_success(hr, IMarshal_MarshalInterface);
2152 ok_more_than_one_lock();
2154 test_freethreadedmarshaldata(pStream, MSHCTX_LOCAL, &Test_ClassFactory, MSHLFLAGS_NORMAL);
2156 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2157 hr = IMarshal_ReleaseMarshalData(pFTMarshal, pStream);
2158 ok_ole_success(hr, IMarshal_ReleaseMarshalData);
2163 /* inproc table-strong marshaling */
2165 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2166 hr = IMarshal_MarshalInterface(pFTMarshal, pStream, &IID_IClassFactory,
2167 (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, (void *)0xdeadbeef,
2168 MSHLFLAGS_TABLESTRONG);
2169 ok_ole_success(hr, IMarshal_MarshalInterface);
2171 ok_more_than_one_lock();
2173 test_freethreadedmarshaldata(pStream, MSHCTX_INPROC, &Test_ClassFactory, MSHLFLAGS_TABLESTRONG);
2175 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2176 hr = IMarshal_UnmarshalInterface(pFTMarshal, pStream, &IID_IUnknown, (void **)&pProxy);
2177 ok_ole_success(hr, IMarshal_UnmarshalInterface);
2179 IUnknown_Release(pProxy);
2181 ok_more_than_one_lock();
2183 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2184 hr = IMarshal_ReleaseMarshalData(pFTMarshal, pStream);
2185 ok_ole_success(hr, IMarshal_ReleaseMarshalData);
2189 /* inproc table-weak marshaling */
2191 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2192 hr = IMarshal_MarshalInterface(pFTMarshal, pStream, &IID_IClassFactory,
2193 (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, (void *)0xdeadbeef,
2194 MSHLFLAGS_TABLEWEAK);
2195 ok_ole_success(hr, IMarshal_MarshalInterface);
2199 test_freethreadedmarshaldata(pStream, MSHCTX_INPROC, &Test_ClassFactory, MSHLFLAGS_TABLEWEAK);
2201 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2202 hr = IMarshal_UnmarshalInterface(pFTMarshal, pStream, &IID_IUnknown, (void **)&pProxy);
2203 ok_ole_success(hr, IMarshal_UnmarshalInterface);
2205 ok_more_than_one_lock();
2207 IUnknown_Release(pProxy);
2211 /* inproc normal marshaling (for extraordinary cases) */
2213 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2214 hr = IMarshal_MarshalInterface(pFTMarshal, pStream, &IID_IClassFactory,
2215 &Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
2216 ok_ole_success(hr, IMarshal_MarshalInterface);
2218 ok_more_than_one_lock();
2220 /* this call shows that DisconnectObject does nothing */
2221 hr = IMarshal_DisconnectObject(pFTMarshal, 0);
2222 ok_ole_success(hr, IMarshal_DisconnectObject);
2224 ok_more_than_one_lock();
2226 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2227 hr = IMarshal_ReleaseMarshalData(pFTMarshal, pStream);
2228 ok_ole_success(hr, IMarshal_ReleaseMarshalData);
2232 /* doesn't enforce marshaling rules here and allows us to unmarshal the
2233 * interface, even though it was freed above */
2234 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2235 hr = IMarshal_UnmarshalInterface(pFTMarshal, pStream, &IID_IUnknown, (void **)&pProxy);
2236 ok_ole_success(hr, IMarshal_UnmarshalInterface);
2240 IStream_Release(pStream);
2241 IMarshal_Release(pFTMarshal);
2244 static void reg_unreg_wine_test_class(BOOL Register)
2250 DWORD dwDisposition;
2253 hr = StringFromCLSID(&CLSID_WineTest, &pszClsid);
2254 ok_ole_success(hr, "StringFromCLSID");
2255 strcpy(buffer, "CLSID\\");
2256 WideCharToMultiByte(CP_ACP, 0, pszClsid, -1, buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), NULL, NULL);
2257 CoTaskMemFree(pszClsid);
2258 strcat(buffer, "\\InprocHandler32");
2261 error = RegCreateKeyEx(HKEY_CLASSES_ROOT, buffer, 0, NULL, 0, KEY_SET_VALUE, NULL, &hkey, &dwDisposition);
2262 ok(error == ERROR_SUCCESS, "RegCreateKeyEx failed with error %d\n", error);
2263 error = RegSetValueEx(hkey, NULL, 0, REG_SZ, (const unsigned char *)"ole32.dll", strlen("ole32.dll") + 1);
2264 ok(error == ERROR_SUCCESS, "RegSetValueEx failed with error %d\n", error);
2269 RegDeleteKey(HKEY_CLASSES_ROOT, buffer);
2270 *strrchr(buffer, '\\') = '\0';
2271 RegDeleteKey(HKEY_CLASSES_ROOT, buffer);
2275 static void test_inproc_handler(void)
2281 reg_unreg_wine_test_class(TRUE);
2283 hr = CoCreateInstance(&CLSID_WineTest, NULL, CLSCTX_INPROC_HANDLER, &IID_IUnknown, (void **)&pObject);
2285 ok_ole_success(hr, "CoCreateInstance");
2289 hr = IUnknown_QueryInterface(pObject, &IID_IWineTest, (void **)&pObject2);
2290 ok(hr == E_NOINTERFACE, "IUnknown_QueryInterface on handler for invalid interface returned 0x%08x instead of E_NOINTERFACE\n", hr);
2292 /* it's a handler as it supports IOleObject */
2293 hr = IUnknown_QueryInterface(pObject, &IID_IOleObject, (void **)&pObject2);
2294 ok_ole_success(hr, "IUnknown_QueryInterface(&IID_IOleObject)");
2295 IUnknown_Release(pObject2);
2297 IUnknown_Release(pObject);
2300 reg_unreg_wine_test_class(FALSE);
2303 static HRESULT WINAPI Test_SMI_QueryInterface(
2304 IStdMarshalInfo *iface,
2308 if (ppvObj == NULL) return E_POINTER;
2310 if (IsEqualGUID(riid, &IID_IUnknown) ||
2311 IsEqualGUID(riid, &IID_IStdMarshalInfo))
2314 IClassFactory_AddRef(iface);
2318 return E_NOINTERFACE;
2321 static ULONG WINAPI Test_SMI_AddRef(IStdMarshalInfo *iface)
2324 return 2; /* non-heap-based object */
2327 static ULONG WINAPI Test_SMI_Release(IStdMarshalInfo *iface)
2330 return 1; /* non-heap-based object */
2333 static HRESULT WINAPI Test_SMI_GetClassForHandler(
2334 IStdMarshalInfo *iface,
2335 DWORD dwDestContext,
2336 void *pvDestContext,
2339 *pClsid = CLSID_WineTest;
2343 static const IStdMarshalInfoVtbl Test_SMI_Vtbl =
2345 Test_SMI_QueryInterface,
2348 Test_SMI_GetClassForHandler
2351 static IStdMarshalInfo Test_SMI = {&Test_SMI_Vtbl};
2353 static void test_handler_marshaling(void)
2356 IStream *pStream = NULL;
2357 IUnknown *pProxy = NULL;
2361 static const LARGE_INTEGER ullZero;
2363 reg_unreg_wine_test_class(TRUE);
2366 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
2367 ok_ole_success(hr, "CreateStreamOnHGlobal");
2368 tid = start_host_object(pStream, &IID_IUnknown, (IUnknown*)&Test_SMI, MSHLFLAGS_NORMAL, &thread);
2370 ok_more_than_one_lock();
2372 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
2373 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)&pProxy);
2374 ok_ole_success(hr, "CoUnmarshalInterface");
2375 IStream_Release(pStream);
2379 ok_more_than_one_lock();
2381 hr = IUnknown_QueryInterface(pProxy, &IID_IWineTest, (void **)&pObject);
2382 ok(hr == E_NOINTERFACE, "IUnknown_QueryInterface with unknown IID should have returned E_NOINTERFACE instead of 0x%08x\n", hr);
2384 /* it's a handler as it supports IOleObject */
2385 hr = IUnknown_QueryInterface(pProxy, &IID_IOleObject, (void **)&pObject);
2387 ok_ole_success(hr, "IUnknown_QueryInterface(&IID_IOleObject)");
2388 if (SUCCEEDED(hr)) IUnknown_Release(pObject);
2390 IUnknown_Release(pProxy);
2395 end_host_object(tid, thread);
2396 reg_unreg_wine_test_class(FALSE);
2398 /* FIXME: test IPersist interface has the same effect as IStdMarshalInfo */
2402 static void test_client_security(void)
2405 IStream *pStream = NULL;
2406 IClassFactory *pProxy = NULL;
2407 IUnknown *pProxy2 = NULL;
2408 IUnknown *pUnknown1 = NULL;
2409 IUnknown *pUnknown2 = NULL;
2410 IClientSecurity *pCliSec = NULL;
2414 static const LARGE_INTEGER ullZero;
2417 OLECHAR *pServerPrincName;
2421 DWORD dwCapabilities;
2426 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
2427 ok_ole_success(hr, "CreateStreamOnHGlobal");
2428 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
2430 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
2431 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
2432 ok_ole_success(hr, "CoUnmarshalInterface");
2433 IStream_Release(pStream);
2435 hr = IUnknown_QueryInterface(pProxy, &IID_IUnknown, (LPVOID*)&pUnknown1);
2436 ok_ole_success(hr, "IUnknown_QueryInterface IID_IUnknown");
2438 hr = IUnknown_QueryInterface(pProxy, &IID_IRemUnknown, (LPVOID*)&pProxy2);
2439 ok_ole_success(hr, "IUnknown_QueryInterface IID_IStream");
2441 hr = IUnknown_QueryInterface(pProxy2, &IID_IUnknown, (LPVOID*)&pUnknown2);
2442 ok_ole_success(hr, "IUnknown_QueryInterface IID_IUnknown");
2444 ok(pUnknown1 == pUnknown2, "both proxy's IUnknowns should be the same - %p, %p\n", pUnknown1, pUnknown2);
2446 hr = IUnknown_QueryInterface(pProxy, &IID_IMarshal, (LPVOID*)&pMarshal);
2447 ok_ole_success(hr, "IUnknown_QueryInterface IID_IMarshal");
2449 hr = IUnknown_QueryInterface(pProxy, &IID_IClientSecurity, (LPVOID*)&pCliSec);
2450 ok_ole_success(hr, "IUnknown_QueryInterface IID_IClientSecurity");
2452 hr = IClientSecurity_QueryBlanket(pCliSec, (IUnknown *)pProxy, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
2453 todo_wine ok_ole_success(hr, "IClientSecurity_QueryBlanket (all NULLs)");
2455 hr = IClientSecurity_QueryBlanket(pCliSec, (IUnknown *)pMarshal, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
2456 todo_wine ok(hr == E_NOINTERFACE, "IClientSecurity_QueryBlanket with local interface should have returned E_NOINTERFACE instead of 0x%08x\n", hr);
2458 hr = IClientSecurity_QueryBlanket(pCliSec, (IUnknown *)pProxy, &dwAuthnSvc, &dwAuthzSvc, &pServerPrincName, &dwAuthnLevel, &dwImpLevel, &pAuthInfo, &dwCapabilities);
2459 todo_wine ok_ole_success(hr, "IClientSecurity_QueryBlanket");
2461 hr = IClientSecurity_SetBlanket(pCliSec, (IUnknown *)pProxy, dwAuthnSvc, dwAuthzSvc, pServerPrincName, dwAuthnLevel, RPC_C_IMP_LEVEL_IMPERSONATE, pAuthInfo, dwCapabilities);
2462 todo_wine ok_ole_success(hr, "IClientSecurity_SetBlanket");
2464 hr = IClassFactory_CreateInstance(pProxy, NULL, &IID_IWineTest, &pv);
2465 ok(hr == E_NOINTERFACE, "COM call should have succeeded instead of returning 0x%08x\n", hr);
2467 hr = IClientSecurity_SetBlanket(pCliSec, (IUnknown *)pMarshal, dwAuthnSvc, dwAuthzSvc, pServerPrincName, dwAuthnLevel, dwImpLevel, pAuthInfo, dwCapabilities);
2468 todo_wine ok(hr == E_NOINTERFACE, "IClientSecurity_SetBlanket with local interface should have returned E_NOINTERFACE instead of 0x%08x\n", hr);
2470 hr = IClientSecurity_SetBlanket(pCliSec, (IUnknown *)pProxy, 0xdeadbeef, dwAuthzSvc, pServerPrincName, dwAuthnLevel, dwImpLevel, pAuthInfo, dwCapabilities);
2471 todo_wine ok(hr == E_INVALIDARG, "IClientSecurity_SetBlanke with invalid dwAuthnSvc should have returned E_INVALIDARG instead of 0x%08x\n", hr);
2473 CoTaskMemFree(pServerPrincName);
2475 hr = IClientSecurity_QueryBlanket(pCliSec, pUnknown1, &dwAuthnSvc, &dwAuthzSvc, &pServerPrincName, &dwAuthnLevel, &dwImpLevel, &pAuthInfo, &dwCapabilities);
2476 todo_wine ok_ole_success(hr, "IClientSecurity_QueryBlanket(IUnknown)");
2478 CoTaskMemFree(pServerPrincName);
2480 IClassFactory_Release(pProxy);
2481 IUnknown_Release(pProxy2);
2482 IUnknown_Release(pUnknown1);
2483 IUnknown_Release(pUnknown2);
2484 IMarshal_Release(pMarshal);
2485 IClientSecurity_Release(pCliSec);
2487 end_host_object(tid, thread);
2490 static HANDLE heventShutdown;
2492 static void LockModuleOOP(void)
2494 InterlockedIncrement(&cLocks); /* for test purposes only */
2495 CoAddRefServerProcess();
2498 static void UnlockModuleOOP(void)
2500 InterlockedDecrement(&cLocks); /* for test purposes only */
2501 if (!CoReleaseServerProcess())
2502 SetEvent(heventShutdown);
2505 static HWND hwnd_app;
2507 static HRESULT WINAPI TestOOP_IClassFactory_QueryInterface(
2508 LPCLASSFACTORY iface,
2512 if (ppvObj == NULL) return E_POINTER;
2514 if (IsEqualGUID(riid, &IID_IUnknown) ||
2515 IsEqualGUID(riid, &IID_IClassFactory))
2518 IClassFactory_AddRef(iface);
2522 return E_NOINTERFACE;
2525 static ULONG WINAPI TestOOP_IClassFactory_AddRef(LPCLASSFACTORY iface)
2527 return 2; /* non-heap-based object */
2530 static ULONG WINAPI TestOOP_IClassFactory_Release(LPCLASSFACTORY iface)
2532 return 1; /* non-heap-based object */
2535 static HRESULT WINAPI TestOOP_IClassFactory_CreateInstance(
2536 LPCLASSFACTORY iface,
2537 LPUNKNOWN pUnkOuter,
2541 if (IsEqualIID(riid, &IID_IClassFactory) || IsEqualIID(riid, &IID_IUnknown))
2546 return CLASS_E_CLASSNOTAVAILABLE;
2549 static HRESULT WINAPI TestOOP_IClassFactory_LockServer(
2550 LPCLASSFACTORY iface,
2560 static const IClassFactoryVtbl TestClassFactoryOOP_Vtbl =
2562 TestOOP_IClassFactory_QueryInterface,
2563 TestOOP_IClassFactory_AddRef,
2564 TestOOP_IClassFactory_Release,
2565 TestOOP_IClassFactory_CreateInstance,
2566 TestOOP_IClassFactory_LockServer
2569 static IClassFactory TestOOP_ClassFactory = { &TestClassFactoryOOP_Vtbl };
2571 static void test_register_local_server(void)
2579 heventShutdown = CreateEvent(NULL, TRUE, FALSE, NULL);
2581 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&TestOOP_ClassFactory,
2582 CLSCTX_LOCAL_SERVER, REGCLS_SINGLEUSE, &cookie);
2583 ok_ole_success(hr, CoRegisterClassObject);
2585 ready_event = CreateEvent(NULL, FALSE, FALSE, "Wine COM Test Ready Event");
2586 SetEvent(ready_event);
2588 quit_event = CreateEvent(NULL, FALSE, FALSE, "Wine COM Test Quit Event");
2592 wait = MsgWaitForMultipleObjects(1, &quit_event, FALSE, 30000, QS_ALLINPUT);
2593 if (wait == WAIT_OBJECT_0+1)
2596 BOOL ret = PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
2599 trace("Message 0x%x\n", msg.message);
2600 TranslateMessage(&msg);
2601 DispatchMessage(&msg);
2605 while (wait == WAIT_OBJECT_0+1);
2607 ok( wait == WAIT_OBJECT_0, "quit event wait timed out\n" );
2608 hr = CoRevokeClassObject(cookie);
2609 ok_ole_success(hr, CoRevokeClassObject);
2612 static HANDLE create_target_process(const char *arg)
2615 char cmdline[MAX_PATH];
2616 PROCESS_INFORMATION pi;
2617 STARTUPINFO si = { 0 };
2622 winetest_get_mainargs( &argv );
2623 sprintf(cmdline, "%s %s %s", argv[0], argv[1], arg);
2624 ok(CreateProcess(argv[0], cmdline, NULL, NULL, FALSE, 0, NULL, NULL,
2625 &si, &pi) != 0, "CreateProcess failed with error: %u\n", GetLastError());
2626 if (pi.hThread) CloseHandle(pi.hThread);
2630 /* tests functions commonly used by out of process COM servers */
2631 static void test_local_server(void)
2641 heventShutdown = CreateEvent(NULL, TRUE, FALSE, NULL);
2645 /* Start the object suspended */
2646 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&TestOOP_ClassFactory,
2647 CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE | REGCLS_SUSPENDED, &cookie);
2648 ok_ole_success(hr, CoRegisterClassObject);
2650 /* ... and CoGetClassObject does not find it and fails when it looks for the
2651 * class in the registry */
2652 hr = CoGetClassObject(&CLSID_WineOOPTest, CLSCTX_INPROC_SERVER,
2653 NULL, &IID_IClassFactory, (LPVOID*)&cf);
2654 ok(hr == REGDB_E_CLASSNOTREG || /* NT */
2655 hr == S_OK /* Win9x */,
2656 "CoGetClassObject should have returned REGDB_E_CLASSNOTREG instead of 0x%08x\n", hr);
2658 /* Resume the object suspended above ... */
2659 hr = CoResumeClassObjects();
2660 ok_ole_success(hr, CoResumeClassObjects);
2662 /* ... and now it should succeed */
2663 hr = CoGetClassObject(&CLSID_WineOOPTest, CLSCTX_INPROC_SERVER,
2664 NULL, &IID_IClassFactory, (LPVOID*)&cf);
2665 ok_ole_success(hr, CoGetClassObject);
2667 /* Now check the locking is working */
2668 /* NOTE: we are accessing the class directly, not through a proxy */
2672 hr = IClassFactory_LockServer(cf, TRUE);
2673 ok_ole_success(hr, IClassFactory_LockServer);
2675 ok_more_than_one_lock();
2677 IClassFactory_LockServer(cf, FALSE);
2678 ok_ole_success(hr, IClassFactory_LockServer);
2682 IClassFactory_Release(cf);
2684 /* wait for shutdown signal */
2685 ret = WaitForSingleObject(heventShutdown, 0);
2686 ok(ret != WAIT_TIMEOUT, "Server didn't shut down\n");
2688 /* try to connect again after SCM has suspended registered class objects */
2689 hr = CoGetClassObject(&CLSID_WineOOPTest, CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER, NULL,
2690 &IID_IClassFactory, (LPVOID*)&cf);
2691 ok(hr == CO_E_SERVER_STOPPING || /* NT */
2692 hr == REGDB_E_CLASSNOTREG || /* win2k */
2693 hr == S_OK /* Win9x */,
2694 "CoGetClassObject should have returned CO_E_SERVER_STOPPING or REGDB_E_CLASSNOTREG instead of 0x%08x\n", hr);
2696 hr = CoRevokeClassObject(cookie);
2697 ok_ole_success(hr, CoRevokeClassObject);
2699 CloseHandle(heventShutdown);
2701 process = create_target_process("-Embedding");
2702 ok(process != NULL, "couldn't start local server process, error was %d\n", GetLastError());
2704 ready_event = CreateEvent(NULL, FALSE, FALSE, "Wine COM Test Ready Event");
2705 ok( !WaitForSingleObject(ready_event, 10000), "wait timed out\n" );
2706 CloseHandle(ready_event);
2708 hr = CoCreateInstance(&CLSID_WineOOPTest, NULL, CLSCTX_LOCAL_SERVER, &IID_IClassFactory, (void **)&cf);
2709 ok_ole_success(hr, CoCreateInstance);
2711 IClassFactory_Release(cf);
2713 hr = CoCreateInstance(&CLSID_WineOOPTest, NULL, CLSCTX_LOCAL_SERVER, &IID_IClassFactory, (void **)&cf);
2714 ok(hr == REGDB_E_CLASSNOTREG, "Second CoCreateInstance on REGCLS_SINGLEUSE object should have failed\n");
2716 quit_event = CreateEvent(NULL, FALSE, FALSE, "Wine COM Test Quit Event");
2717 SetEvent(quit_event);
2719 winetest_wait_child_process( process );
2720 CloseHandle(quit_event);
2721 CloseHandle(process);
2727 IGlobalInterfaceTable *git;
2730 static DWORD CALLBACK get_global_interface_proc(LPVOID pv)
2733 struct git_params *params = pv;
2736 hr = IGlobalInterfaceTable_GetInterfaceFromGlobal(params->git, params->cookie, &IID_IClassFactory, (void **)&cf);
2737 ok(hr == CO_E_NOTINITIALIZED ||
2738 broken(hr == E_UNEXPECTED) /* win2k */ ||
2739 broken(hr == S_OK) /* NT 4 */,
2740 "IGlobalInterfaceTable_GetInterfaceFromGlobal should have failed with error CO_E_NOTINITIALIZED or E_UNEXPECTED instead of 0x%08x\n",
2743 IClassFactory_Release(cf);
2747 hr = IGlobalInterfaceTable_GetInterfaceFromGlobal(params->git, params->cookie, &IID_IClassFactory, (void **)&cf);
2748 ok_ole_success(hr, IGlobalInterfaceTable_GetInterfaceFromGlobal);
2750 IClassFactory_Release(cf);
2757 static void test_globalinterfacetable(void)
2760 IGlobalInterfaceTable *git;
2764 struct git_params params;
2768 trace("test_globalinterfacetable\n");
2771 hr = CoCreateInstance(&CLSID_StdGlobalInterfaceTable, NULL, CLSCTX_INPROC_SERVER, &IID_IGlobalInterfaceTable, (void **)&git);
2772 ok_ole_success(hr, CoCreateInstance);
2774 hr = IGlobalInterfaceTable_RegisterInterfaceInGlobal(git, (IUnknown *)&Test_ClassFactory, &IID_IClassFactory, &cookie);
2775 ok_ole_success(hr, IGlobalInterfaceTable_RegisterInterfaceInGlobal);
2777 ok_more_than_one_lock();
2779 params.cookie = cookie;
2781 /* note: params is on stack so we MUST wait for get_global_interface_proc
2782 * to exit before we can return */
2783 thread = CreateThread(NULL, 0, get_global_interface_proc, ¶ms, 0, &tid);
2785 ret = MsgWaitForMultipleObjects(1, &thread, FALSE, 10000, QS_ALLINPUT);
2786 while (ret == WAIT_OBJECT_0 + 1)
2789 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
2790 DispatchMessage(&msg);
2791 ret = MsgWaitForMultipleObjects(1, &thread, FALSE, 10000, QS_ALLINPUT);
2794 CloseHandle(thread);
2796 /* test getting interface from global with different iid */
2797 hr = IGlobalInterfaceTable_GetInterfaceFromGlobal(git, cookie, &IID_IUnknown, (void **)&object);
2798 ok_ole_success(hr, IGlobalInterfaceTable_GetInterfaceFromGlobal);
2799 IUnknown_Release(object);
2801 /* test getting interface from global with same iid */
2802 hr = IGlobalInterfaceTable_GetInterfaceFromGlobal(git, cookie, &IID_IClassFactory, (void **)&object);
2803 ok_ole_success(hr, IGlobalInterfaceTable_GetInterfaceFromGlobal);
2804 IUnknown_Release(object);
2806 hr = IGlobalInterfaceTable_RevokeInterfaceFromGlobal(git, cookie);
2807 ok_ole_success(hr, IGlobalInterfaceTable_RevokeInterfaceFromGlobal);
2811 IGlobalInterfaceTable_Release(git);
2814 static const char *debugstr_iid(REFIID riid)
2816 static char name[256];
2820 LONG name_size = sizeof(name);
2821 StringFromGUID2(riid, bufferW, sizeof(bufferW)/sizeof(bufferW[0]));
2822 WideCharToMultiByte(CP_ACP, 0, bufferW, sizeof(bufferW)/sizeof(bufferW[0]), buffer, sizeof(buffer), NULL, NULL);
2823 if (RegOpenKeyEx(HKEY_CLASSES_ROOT, "Interface", 0, KEY_QUERY_VALUE, &hkeyInterface) != ERROR_SUCCESS)
2825 memcpy(name, buffer, sizeof(buffer));
2828 if (RegQueryValue(hkeyInterface, buffer, name, &name_size) != ERROR_SUCCESS)
2830 memcpy(name, buffer, sizeof(buffer));
2833 RegCloseKey(hkeyInterface);
2838 static HRESULT WINAPI TestChannelHook_QueryInterface(IChannelHook *iface, REFIID riid, void **ppv)
2840 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IChannelHook))
2843 IUnknown_AddRef(iface);
2848 return E_NOINTERFACE;
2851 static ULONG WINAPI TestChannelHook_AddRef(IChannelHook *iface)
2856 static ULONG WINAPI TestChannelHook_Release(IChannelHook *iface)
2861 static void WINAPI TestChannelHook_ClientGetSize(
2862 IChannelHook *iface,
2867 SChannelHookCallInfo *info = (SChannelHookCallInfo *)riid;
2868 trace("TestChannelHook_ClientGetBuffer\n");
2869 trace("\t%s method %d\n", debugstr_iid(riid), info->iMethod);
2870 trace("\tcid: %s\n", debugstr_iid(&info->uCausality));
2871 ok(info->cbSize == sizeof(*info), "info->cbSize was %d instead of %d\n", info->cbSize, (int)sizeof(*info));
2872 ok(info->dwServerPid == GetCurrentProcessId(), "info->dwServerPid was 0x%x instead of 0x%x\n", info->dwServerPid, GetCurrentProcessId());
2873 ok(!info->pObject, "info->pObject should be NULL\n");
2874 ok(IsEqualGUID(uExtent, &EXTENTID_WineTest), "uExtent wasn't correct\n");
2879 static void WINAPI TestChannelHook_ClientFillBuffer(
2880 IChannelHook *iface,
2886 SChannelHookCallInfo *info = (SChannelHookCallInfo *)riid;
2887 trace("TestChannelHook_ClientFillBuffer\n");
2888 ok(info->cbSize == sizeof(*info), "info->cbSize was %d instead of %d\n", info->cbSize, (int)sizeof(*info));
2889 ok(info->dwServerPid == GetCurrentProcessId(), "info->dwServerPid was 0x%x instead of 0x%x\n", info->dwServerPid, GetCurrentProcessId());
2890 ok(!info->pObject, "info->pObject should be NULL\n");
2891 ok(IsEqualGUID(uExtent, &EXTENTID_WineTest), "uExtent wasn't correct\n");
2893 *(unsigned char *)pDataBuffer = 0xcc;
2897 static void WINAPI TestChannelHook_ClientNotify(
2898 IChannelHook *iface,
2906 SChannelHookCallInfo *info = (SChannelHookCallInfo *)riid;
2907 trace("TestChannelHook_ClientNotify hrFault = 0x%08x\n", hrFault);
2908 ok(info->cbSize == sizeof(*info), "info->cbSize was %d instead of %d\n", info->cbSize, (int)sizeof(*info));
2909 ok(info->dwServerPid == GetCurrentProcessId(), "info->dwServerPid was 0x%x instead of 0x%x\n", info->dwServerPid, GetCurrentProcessId());
2911 ok(info->pObject != NULL, "info->pObject shouldn't be NULL\n");
2913 ok(IsEqualGUID(uExtent, &EXTENTID_WineTest), "uExtent wasn't correct\n");
2916 static void WINAPI TestChannelHook_ServerNotify(
2917 IChannelHook *iface,
2924 SChannelHookCallInfo *info = (SChannelHookCallInfo *)riid;
2925 trace("TestChannelHook_ServerNotify\n");
2926 ok(info->cbSize == sizeof(*info), "info->cbSize was %d instead of %d\n", info->cbSize, (int)sizeof(*info));
2927 ok(info->dwServerPid == GetCurrentProcessId(), "info->dwServerPid was 0x%x instead of 0x%x\n", info->dwServerPid, GetCurrentProcessId());
2928 ok(info->pObject != NULL, "info->pObject shouldn't be NULL\n");
2929 ok(cbDataSize == 1, "cbDataSize should have been 1 instead of %d\n", cbDataSize);
2930 ok(*(unsigned char *)pDataBuffer == 0xcc, "pDataBuffer should have contained 0xcc instead of 0x%x\n", *(unsigned char *)pDataBuffer);
2931 ok(IsEqualGUID(uExtent, &EXTENTID_WineTest), "uExtent wasn't correct\n");
2934 static void WINAPI TestChannelHook_ServerGetSize(
2935 IChannelHook *iface,
2941 SChannelHookCallInfo *info = (SChannelHookCallInfo *)riid;
2942 trace("TestChannelHook_ServerGetSize\n");
2943 trace("\t%s method %d\n", debugstr_iid(riid), info->iMethod);
2944 ok(info->cbSize == sizeof(*info), "info->cbSize was %d instead of %d\n", info->cbSize, (int)sizeof(*info));
2945 ok(info->dwServerPid == GetCurrentProcessId(), "info->dwServerPid was 0x%x instead of 0x%x\n", info->dwServerPid, GetCurrentProcessId());
2946 ok(info->pObject != NULL, "info->pObject shouldn't be NULL\n");
2947 ok(IsEqualGUID(uExtent, &EXTENTID_WineTest), "uExtent wasn't correct\n");
2948 if (hrFault != S_OK)
2949 trace("\thrFault = 0x%08x\n", hrFault);
2954 static void WINAPI TestChannelHook_ServerFillBuffer(
2955 IChannelHook *iface,
2962 trace("TestChannelHook_ServerFillBuffer\n");
2963 ok(0, "TestChannelHook_ServerFillBuffer shouldn't be called\n");
2966 static const IChannelHookVtbl TestChannelHookVtbl =
2968 TestChannelHook_QueryInterface,
2969 TestChannelHook_AddRef,
2970 TestChannelHook_Release,
2971 TestChannelHook_ClientGetSize,
2972 TestChannelHook_ClientFillBuffer,
2973 TestChannelHook_ClientNotify,
2974 TestChannelHook_ServerNotify,
2975 TestChannelHook_ServerGetSize,
2976 TestChannelHook_ServerFillBuffer,
2979 static IChannelHook TestChannelHook = { &TestChannelHookVtbl };
2981 static void test_channel_hook(void)
2983 IStream *pStream = NULL;
2984 IClassFactory *cf = NULL;
2986 IUnknown *proxy = NULL;
2990 hr = CoRegisterChannelHook(&EXTENTID_WineTest, &TestChannelHook);
2991 ok_ole_success(hr, CoRegisterChannelHook);
2993 hr = CoRegisterMessageFilter(&MessageFilter, NULL);
2994 ok_ole_success(hr, CoRegisterMessageFilter);
2998 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
2999 ok_ole_success(hr, CreateStreamOnHGlobal);
3000 tid = start_host_object2(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &MessageFilter, &thread);
3002 ok_more_than_one_lock();
3004 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
3005 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&cf);
3006 ok_ole_success(hr, CoUnmarshalInterface);
3007 IStream_Release(pStream);
3009 ok_more_than_one_lock();
3011 hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
3012 ok_ole_success(hr, IClassFactory_CreateInstance);
3013 IUnknown_Release(proxy);
3015 IClassFactory_Release(cf);
3019 end_host_object(tid, thread);
3021 hr = CoRegisterMessageFilter(NULL, NULL);
3022 ok_ole_success(hr, CoRegisterMessageFilter);
3027 HMODULE hOle32 = GetModuleHandle("ole32");
3031 if (!(pCoInitializeEx = (void*)GetProcAddress(hOle32, "CoInitializeEx"))) goto no_test;
3033 argc = winetest_get_mainargs( &argv );
3034 if (argc > 2 && (!strcmp(argv[2], "-Embedding")))
3036 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
3037 test_register_local_server();
3043 register_test_window();
3045 test_cocreateinstance_proxy();
3047 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
3049 /* FIXME: test CoCreateInstanceEx */
3051 /* lifecycle management and marshaling tests */
3052 test_no_marshaler();
3053 test_normal_marshal_and_release();
3054 test_normal_marshal_and_unmarshal();
3055 test_marshal_and_unmarshal_invalid();
3056 test_same_apartment_unmarshal_failure();
3057 test_interthread_marshal_and_unmarshal();
3058 test_proxy_marshal_and_unmarshal();
3059 test_proxy_marshal_and_unmarshal2();
3060 test_proxy_marshal_and_unmarshal_weak();
3061 test_proxy_marshal_and_unmarshal_strong();
3062 test_marshal_stub_apartment_shutdown();
3063 test_marshal_proxy_apartment_shutdown();
3064 test_marshal_proxy_mta_apartment_shutdown();
3065 test_no_couninitialize_server();
3066 test_no_couninitialize_client();
3067 test_tableweak_marshal_and_unmarshal_twice();
3068 test_tableweak_marshal_releasedata1();
3069 test_tableweak_marshal_releasedata2();
3070 test_tableweak_and_normal_marshal_and_unmarshal();
3071 test_tablestrong_marshal_and_unmarshal_twice();
3072 test_lock_object_external();
3073 test_disconnect_stub();
3074 test_normal_marshal_and_unmarshal_twice();
3075 test_hresult_marshaling();
3076 test_proxy_used_in_wrong_thread();
3077 test_message_filter();
3078 test_bad_marshal_stream();
3079 test_proxy_interfaces();
3080 test_stubbuffer(&IID_IClassFactory);
3081 test_proxybuffer(&IID_IClassFactory);
3082 test_message_reentrancy();
3083 test_call_from_message();
3084 test_WM_QUIT_handling();
3085 test_freethreadedmarshaler();
3086 test_inproc_handler();
3087 test_handler_marshaling();
3088 test_client_security();
3090 test_local_server();
3092 test_globalinterfacetable();
3094 /* must be last test as channel hooks can't be unregistered */
3095 test_channel_hook();
3101 trace("You need DCOM95 installed to run this test\n");