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
31 #include "wine/test.h"
33 /* functions that are not present on all versions of Windows */
34 HRESULT (WINAPI * pCoInitializeEx)(LPVOID lpReserved, DWORD dwCoInit);
36 /* helper macros to make tests a bit leaner */
37 #define ok_more_than_one_lock() ok(cLocks > 0, "Number of locks should be > 0, but actually is %ld\n", cLocks)
38 #define ok_no_locks() ok(cLocks == 0, "Number of locks should be 0, but actually is %ld\n", cLocks)
39 #define ok_ole_success(hr, func) ok(hr == S_OK, #func " failed with error 0x%08lx\n", hr)
41 static const IID IID_IWineTest =
46 {0xa1, 0xa2, 0x5d, 0x5a, 0x36, 0x54, 0xd3, 0xbd}
47 }; /* 5201163f-8164-4fd0-a1a2-5d5a3654d3bd */
49 static void test_cocreateinstance_proxy(void)
55 pCoInitializeEx(NULL, COINIT_MULTITHREADED);
57 hr = CoCreateInstance(&CLSID_ShellDesktop, NULL, CLSCTX_INPROC, &IID_IUnknown, (void **)&pProxy);
58 ok_ole_success(hr, CoCreateInstance);
59 hr = IUnknown_QueryInterface(pProxy, &IID_IMultiQI, (void **)&pMQI);
61 ok(hr == S_OK, "created object is not a proxy, so was created in the wrong apartment\n");
63 IMultiQI_Release(pMQI);
64 IUnknown_Release(pProxy);
69 static const LARGE_INTEGER ullZero;
72 static void LockModule(void)
74 InterlockedIncrement(&cLocks);
77 static void UnlockModule(void)
79 InterlockedDecrement(&cLocks);
83 static HRESULT WINAPI Test_IUnknown_QueryInterface(
88 if (ppvObj == NULL) return E_POINTER;
90 if (IsEqualGUID(riid, &IID_IUnknown))
92 *ppvObj = (LPVOID)iface;
93 IUnknown_AddRef(iface);
101 static ULONG WINAPI Test_IUnknown_AddRef(LPUNKNOWN iface)
104 return 2; /* non-heap-based object */
107 static ULONG WINAPI Test_IUnknown_Release(LPUNKNOWN iface)
110 return 1; /* non-heap-based object */
113 static const IUnknownVtbl TestUnknown_Vtbl =
115 Test_IUnknown_QueryInterface,
116 Test_IUnknown_AddRef,
117 Test_IUnknown_Release,
120 static IUnknown Test_Unknown = { &TestUnknown_Vtbl };
123 static HRESULT WINAPI Test_IClassFactory_QueryInterface(
124 LPCLASSFACTORY iface,
128 if (ppvObj == NULL) return E_POINTER;
130 if (IsEqualGUID(riid, &IID_IUnknown) ||
131 IsEqualGUID(riid, &IID_IClassFactory))
133 *ppvObj = (LPVOID)iface;
134 IClassFactory_AddRef(iface);
139 return E_NOINTERFACE;
142 static ULONG WINAPI Test_IClassFactory_AddRef(LPCLASSFACTORY iface)
145 return 2; /* non-heap-based object */
148 static ULONG WINAPI Test_IClassFactory_Release(LPCLASSFACTORY iface)
151 return 1; /* non-heap-based object */
154 static HRESULT WINAPI Test_IClassFactory_CreateInstance(
155 LPCLASSFACTORY iface,
160 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
161 return IUnknown_QueryInterface((IUnknown*)&Test_Unknown, riid, ppvObj);
164 static HRESULT WINAPI Test_IClassFactory_LockServer(
165 LPCLASSFACTORY iface,
171 static const IClassFactoryVtbl TestClassFactory_Vtbl =
173 Test_IClassFactory_QueryInterface,
174 Test_IClassFactory_AddRef,
175 Test_IClassFactory_Release,
176 Test_IClassFactory_CreateInstance,
177 Test_IClassFactory_LockServer
180 static IClassFactory Test_ClassFactory = { &TestClassFactory_Vtbl };
182 #define RELEASEMARSHALDATA WM_USER
184 struct host_object_data
189 MSHLFLAGS marshal_flags;
190 HANDLE marshal_event;
191 IMessageFilter *filter;
194 static DWORD CALLBACK host_object_proc(LPVOID p)
196 struct host_object_data *data = (struct host_object_data *)p;
200 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
204 IMessageFilter * prev_filter = NULL;
205 hr = CoRegisterMessageFilter(data->filter, &prev_filter);
206 if (prev_filter) IMessageFilter_Release(prev_filter);
207 ok_ole_success(hr, CoRegisterMessageFilter);
210 hr = CoMarshalInterface(data->stream, &data->iid, data->object, MSHCTX_INPROC, NULL, data->marshal_flags);
211 ok_ole_success(hr, CoMarshalInterface);
213 /* force the message queue to be created before signaling parent thread */
214 PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
216 SetEvent(data->marshal_event);
218 while (GetMessage(&msg, NULL, 0, 0))
220 if (msg.hwnd == NULL && msg.message == RELEASEMARSHALDATA)
222 trace("releasing marshal data\n");
223 CoReleaseMarshalData(data->stream);
224 SetEvent((HANDLE)msg.lParam);
227 DispatchMessage(&msg);
230 HeapFree(GetProcessHeap(), 0, data);
237 static DWORD start_host_object2(IStream *stream, REFIID riid, IUnknown *object, MSHLFLAGS marshal_flags, IMessageFilter *filter, HANDLE *thread)
240 HANDLE marshal_event = CreateEvent(NULL, FALSE, FALSE, NULL);
241 struct host_object_data *data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data));
243 data->stream = stream;
245 data->object = object;
246 data->marshal_flags = marshal_flags;
247 data->marshal_event = marshal_event;
248 data->filter = filter;
250 *thread = CreateThread(NULL, 0, host_object_proc, data, 0, &tid);
252 /* wait for marshaling to complete before returning */
253 WaitForSingleObject(marshal_event, INFINITE);
254 CloseHandle(marshal_event);
259 static DWORD start_host_object(IStream *stream, REFIID riid, IUnknown *object, MSHLFLAGS marshal_flags, HANDLE *thread)
261 return start_host_object2(stream, riid, object, marshal_flags, NULL, thread);
264 /* asks thread to release the marshal data because it has to be done by the
265 * same thread that marshaled the interface in the first place. */
266 static void release_host_object(DWORD tid)
268 HANDLE event = CreateEvent(NULL, FALSE, FALSE, NULL);
269 PostThreadMessage(tid, RELEASEMARSHALDATA, 0, (LPARAM)event);
270 WaitForSingleObject(event, INFINITE);
274 static void end_host_object(DWORD tid, HANDLE thread)
276 BOOL ret = PostThreadMessage(tid, WM_QUIT, 0, 0);
277 ok(ret, "PostThreadMessage failed with error %ld\n", GetLastError());
278 /* be careful of races - don't return until hosting thread has terminated */
279 WaitForSingleObject(thread, INFINITE);
283 /* tests failure case of interface not having a marshaler specified in the
285 static void test_no_marshaler(void)
290 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
291 ok_ole_success(hr, CreateStreamOnHGlobal);
292 hr = CoMarshalInterface(pStream, &IID_IWineTest, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
293 ok(hr == E_NOINTERFACE, "CoMarshalInterface should have returned E_NOINTERFACE instead of 0x%08lx\n", hr);
295 IStream_Release(pStream);
298 /* tests normal marshal and then release without unmarshaling */
299 static void test_normal_marshal_and_release(void)
302 IStream *pStream = NULL;
306 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
307 ok_ole_success(hr, CreateStreamOnHGlobal);
308 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
309 ok_ole_success(hr, CoMarshalInterface);
311 ok_more_than_one_lock();
313 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
314 hr = CoReleaseMarshalData(pStream);
315 ok_ole_success(hr, CoReleaseMarshalData);
316 IStream_Release(pStream);
321 /* tests success case of a same-thread marshal and unmarshal */
322 static void test_normal_marshal_and_unmarshal(void)
325 IStream *pStream = NULL;
326 IUnknown *pProxy = NULL;
330 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
331 ok_ole_success(hr, CreateStreamOnHGlobal);
332 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
333 ok_ole_success(hr, CoMarshalInterface);
335 ok_more_than_one_lock();
337 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
338 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
339 ok_ole_success(hr, CoUnmarshalInterface);
340 IStream_Release(pStream);
342 ok_more_than_one_lock();
344 IUnknown_Release(pProxy);
349 /* tests failure case of unmarshaling a freed object */
350 static void test_marshal_and_unmarshal_invalid(void)
353 IStream *pStream = NULL;
354 IClassFactory *pProxy = NULL;
361 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
362 ok_ole_success(hr, CreateStreamOnHGlobal);
363 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
365 ok_more_than_one_lock();
367 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
368 hr = CoReleaseMarshalData(pStream);
369 ok_ole_success(hr, CoReleaseMarshalData);
373 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
374 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
375 todo_wine { ok_ole_success(hr, CoUnmarshalInterface); }
381 hr = IClassFactory_CreateInstance(pProxy, NULL, &IID_IUnknown, &dummy);
382 ok(hr == RPC_E_DISCONNECTED, "Remote call should have returned RPC_E_DISCONNECTED, instead of 0x%08lx\n", hr);
384 IClassFactory_Release(pProxy);
387 IStream_Release(pStream);
389 end_host_object(tid, thread);
392 /* tests success case of an interthread marshal */
393 static void test_interthread_marshal_and_unmarshal(void)
396 IStream *pStream = NULL;
397 IUnknown *pProxy = NULL;
403 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
404 ok_ole_success(hr, CreateStreamOnHGlobal);
405 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
407 ok_more_than_one_lock();
409 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
410 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
411 ok_ole_success(hr, CoUnmarshalInterface);
412 IStream_Release(pStream);
414 ok_more_than_one_lock();
416 IUnknown_Release(pProxy);
420 end_host_object(tid, thread);
423 /* the number of external references that Wine's proxy manager normally gives
424 * out, so we can test the border case of running out of references */
425 #define NORMALEXTREFS 5
427 /* tests success case of an interthread marshal and then marshaling the proxy */
428 static void test_proxy_marshal_and_unmarshal(void)
431 IStream *pStream = NULL;
432 IUnknown *pProxy = NULL;
433 IUnknown *pProxy2 = NULL;
440 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
441 ok_ole_success(hr, CreateStreamOnHGlobal);
442 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
444 ok_more_than_one_lock();
446 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
447 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
448 ok_ole_success(hr, CoUnmarshalInterface);
450 ok_more_than_one_lock();
452 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
453 /* marshal the proxy */
454 hr = CoMarshalInterface(pStream, &IID_IClassFactory, pProxy, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
455 ok_ole_success(hr, CoMarshalInterface);
457 ok_more_than_one_lock();
459 /* marshal 5 more times to exhaust the normal external references of 5 */
460 for (i = 0; i < NORMALEXTREFS; i++)
462 hr = CoMarshalInterface(pStream, &IID_IClassFactory, pProxy, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
463 ok_ole_success(hr, CoMarshalInterface);
466 ok_more_than_one_lock();
468 /* release the original proxy to test that we successfully keep the
469 * original object alive */
470 IUnknown_Release(pProxy);
472 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
473 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
474 ok_ole_success(hr, CoUnmarshalInterface);
476 ok_more_than_one_lock();
478 /* now the proxies should be as follows:
479 * pProxy2 -> &Test_ClassFactory
480 * they should NOT be as follows:
481 * pProxy -> &Test_ClassFactory
483 * the above can only really be tested by looking in +ole traces
486 IUnknown_Release(pProxy2);
488 /* unmarshal all of the proxies to check that the object stub still exists */
489 for (i = 0; i < NORMALEXTREFS; i++)
491 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
492 ok_ole_success(hr, CoUnmarshalInterface);
494 IUnknown_Release(pProxy2);
499 IStream_Release(pStream);
501 end_host_object(tid, thread);
504 /* tests success case of an interthread marshal and then marshaling the proxy
505 * using an iid that hasn't previously been unmarshaled */
506 static void test_proxy_marshal_and_unmarshal2(void)
509 IStream *pStream = NULL;
510 IUnknown *pProxy = NULL;
511 IUnknown *pProxy2 = NULL;
517 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
518 ok_ole_success(hr, CreateStreamOnHGlobal);
519 tid = start_host_object(pStream, &IID_IUnknown, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
521 ok_more_than_one_lock();
523 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
524 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)&pProxy);
525 ok_ole_success(hr, CoUnmarshalInterface);
527 ok_more_than_one_lock();
529 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
530 /* marshal the proxy */
531 hr = CoMarshalInterface(pStream, &IID_IClassFactory, pProxy, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
532 ok_ole_success(hr, CoMarshalInterface);
534 ok_more_than_one_lock();
536 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
537 /* unmarshal the second proxy to the object */
538 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
539 ok_ole_success(hr, CoUnmarshalInterface);
540 IStream_Release(pStream);
542 /* now the proxies should be as follows:
543 * pProxy -> &Test_ClassFactory
544 * pProxy2 -> &Test_ClassFactory
545 * they should NOT be as follows:
546 * pProxy -> &Test_ClassFactory
548 * the above can only really be tested by looking in +ole traces
551 ok_more_than_one_lock();
553 IUnknown_Release(pProxy);
555 ok_more_than_one_lock();
557 IUnknown_Release(pProxy2);
561 end_host_object(tid, thread);
564 /* tests that stubs are released when the containing apartment is destroyed */
565 static void test_marshal_stub_apartment_shutdown(void)
568 IStream *pStream = NULL;
569 IUnknown *pProxy = NULL;
575 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
576 ok_ole_success(hr, CreateStreamOnHGlobal);
577 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
579 ok_more_than_one_lock();
581 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
582 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
583 ok_ole_success(hr, CoUnmarshalInterface);
584 IStream_Release(pStream);
586 ok_more_than_one_lock();
588 end_host_object(tid, thread);
592 IUnknown_Release(pProxy);
597 /* tests that proxies are released when the containing apartment is destroyed */
598 static void test_marshal_proxy_apartment_shutdown(void)
601 IStream *pStream = NULL;
602 IUnknown *pProxy = NULL;
608 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
609 ok_ole_success(hr, CreateStreamOnHGlobal);
610 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
612 ok_more_than_one_lock();
614 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
615 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
616 ok_ole_success(hr, CoUnmarshalInterface);
617 IStream_Release(pStream);
619 ok_more_than_one_lock();
625 IUnknown_Release(pProxy);
629 end_host_object(tid, thread);
631 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
634 /* tests that proxies are released when the containing mta apartment is destroyed */
635 static void test_marshal_proxy_mta_apartment_shutdown(void)
638 IStream *pStream = NULL;
639 IUnknown *pProxy = NULL;
644 pCoInitializeEx(NULL, COINIT_MULTITHREADED);
648 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
649 ok_ole_success(hr, CreateStreamOnHGlobal);
650 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
652 ok_more_than_one_lock();
654 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
655 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
656 ok_ole_success(hr, CoUnmarshalInterface);
657 IStream_Release(pStream);
659 ok_more_than_one_lock();
665 IUnknown_Release(pProxy);
669 end_host_object(tid, thread);
671 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
677 HANDLE marshal_event;
678 HANDLE unmarshal_event;
681 /* helper for test_no_couninitialize_server */
682 static DWORD CALLBACK no_couninitialize_server_proc(LPVOID p)
684 struct ncu_params *ncu_params = (struct ncu_params *)p;
687 pCoInitializeEx(NULL, COINIT_MULTITHREADED);
689 hr = CoMarshalInterface(ncu_params->stream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
690 ok_ole_success(hr, CoMarshalInterface);
692 SetEvent(ncu_params->marshal_event);
694 WaitForSingleObject(ncu_params->unmarshal_event, INFINITE);
696 /* die without calling CoUninitialize */
701 /* tests apartment that an apartment with a stub is released without deadlock
702 * if the owning thread exits */
703 static void test_no_couninitialize_server(void)
706 IStream *pStream = NULL;
707 IUnknown *pProxy = NULL;
710 struct ncu_params ncu_params;
714 ncu_params.marshal_event = CreateEvent(NULL, TRUE, FALSE, NULL);
715 ncu_params.unmarshal_event = CreateEvent(NULL, TRUE, FALSE, NULL);
717 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
718 ok_ole_success(hr, CreateStreamOnHGlobal);
719 ncu_params.stream = pStream;
721 thread = CreateThread(NULL, 0, no_couninitialize_server_proc, &ncu_params, 0, &tid);
723 WaitForSingleObject(ncu_params.marshal_event, INFINITE);
724 ok_more_than_one_lock();
726 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
727 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
728 ok_ole_success(hr, CoUnmarshalInterface);
729 IStream_Release(pStream);
731 ok_more_than_one_lock();
733 SetEvent(ncu_params.unmarshal_event);
734 WaitForSingleObject(thread, INFINITE);
739 CloseHandle(ncu_params.marshal_event);
740 CloseHandle(ncu_params.unmarshal_event);
742 IUnknown_Release(pProxy);
747 /* STA -> STA call during DLL_THREAD_DETACH */
748 static DWORD CALLBACK no_couninitialize_client_proc(LPVOID p)
750 struct ncu_params *ncu_params = (struct ncu_params *)p;
752 IUnknown *pProxy = NULL;
754 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
756 hr = CoUnmarshalInterface(ncu_params->stream, &IID_IClassFactory, (void **)&pProxy);
757 ok_ole_success(hr, CoUnmarshalInterface);
759 ok_more_than_one_lock();
761 /* die without calling CoUninitialize */
766 /* tests STA -> STA call during DLL_THREAD_DETACH doesn't deadlock */
767 static void test_no_couninitialize_client(void)
770 IStream *pStream = NULL;
775 struct ncu_params ncu_params;
779 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
780 ok_ole_success(hr, CreateStreamOnHGlobal);
781 ncu_params.stream = pStream;
783 /* NOTE: assumes start_host_object uses an STA to host the object, as MTAs
784 * always deadlock when called from within DllMain */
785 host_tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown *)&Test_ClassFactory, MSHLFLAGS_NORMAL, &host_thread);
786 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
788 ok_more_than_one_lock();
790 thread = CreateThread(NULL, 0, no_couninitialize_client_proc, &ncu_params, 0, &tid);
792 WaitForSingleObject(thread, INFINITE);
797 end_host_object(host_tid, host_thread);
800 /* tests success case of a same-thread table-weak marshal, unmarshal, unmarshal */
801 static void test_tableweak_marshal_and_unmarshal_twice(void)
804 IStream *pStream = NULL;
805 IUnknown *pProxy1 = NULL;
806 IUnknown *pProxy2 = NULL;
812 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
813 ok_ole_success(hr, CreateStreamOnHGlobal);
814 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_TABLEWEAK, &thread);
816 ok_more_than_one_lock();
818 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
819 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy1);
820 ok_ole_success(hr, CoUnmarshalInterface);
822 ok_more_than_one_lock();
824 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
825 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
826 ok_ole_success(hr, CoUnmarshalInterface);
828 ok_more_than_one_lock();
830 IUnknown_Release(pProxy1);
831 IUnknown_Release(pProxy2);
833 /* this line is shows the difference between weak and strong table marshaling:
834 * weak has cLocks == 0
835 * strong has cLocks > 0 */
838 end_host_object(tid, thread);
841 /* tests releasing after unmarshaling one object */
842 static void test_tableweak_marshal_releasedata1(void)
845 IStream *pStream = NULL;
846 IUnknown *pProxy1 = NULL;
847 IUnknown *pProxy2 = NULL;
853 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
854 ok_ole_success(hr, CreateStreamOnHGlobal);
855 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_TABLEWEAK, &thread);
857 ok_more_than_one_lock();
859 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
860 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy1);
861 ok_ole_success(hr, CoUnmarshalInterface);
863 ok_more_than_one_lock();
865 /* release the remaining reference on the object by calling
866 * CoReleaseMarshalData in the hosting thread */
867 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
868 release_host_object(tid);
870 ok_more_than_one_lock();
872 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
873 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
874 ok_ole_success(hr, CoUnmarshalInterface);
875 IStream_Release(pStream);
877 ok_more_than_one_lock();
879 IUnknown_Release(pProxy1);
881 IUnknown_Release(pProxy2);
883 /* this line is shows the difference between weak and strong table marshaling:
884 * weak has cLocks == 0
885 * strong has cLocks > 0 */
888 end_host_object(tid, thread);
891 /* tests releasing after unmarshaling one object */
892 static void test_tableweak_marshal_releasedata2(void)
895 IStream *pStream = NULL;
896 IUnknown *pProxy = NULL;
902 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
903 ok_ole_success(hr, CreateStreamOnHGlobal);
904 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_TABLEWEAK, &thread);
906 ok_more_than_one_lock();
908 /* release the remaining reference on the object by calling
909 * CoReleaseMarshalData in the hosting thread */
910 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
911 release_host_object(tid);
915 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
916 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
919 ok(hr == CO_E_OBJNOTREG,
920 "CoUnmarshalInterface should have failed with CO_E_OBJNOTREG, but returned 0x%08lx instead\n",
923 IStream_Release(pStream);
927 end_host_object(tid, thread);
930 /* tests success case of a same-thread table-strong marshal, unmarshal, unmarshal */
931 static void test_tablestrong_marshal_and_unmarshal_twice(void)
934 IStream *pStream = NULL;
935 IUnknown *pProxy1 = NULL;
936 IUnknown *pProxy2 = NULL;
942 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
943 ok_ole_success(hr, CreateStreamOnHGlobal);
944 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_TABLESTRONG, &thread);
946 ok_more_than_one_lock();
948 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
949 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy1);
950 ok_ole_success(hr, CoUnmarshalInterface);
952 ok_more_than_one_lock();
954 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
955 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
956 ok_ole_success(hr, CoUnmarshalInterface);
958 ok_more_than_one_lock();
960 if (pProxy1) IUnknown_Release(pProxy1);
961 if (pProxy2) IUnknown_Release(pProxy2);
963 /* this line is shows the difference between weak and strong table marshaling:
964 * weak has cLocks == 0
965 * strong has cLocks > 0 */
966 ok_more_than_one_lock();
968 /* release the remaining reference on the object by calling
969 * CoReleaseMarshalData in the hosting thread */
970 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
971 release_host_object(tid);
972 IStream_Release(pStream);
976 end_host_object(tid, thread);
979 /* tests CoLockObjectExternal */
980 static void test_lock_object_external(void)
983 IStream *pStream = NULL;
987 /* test the stub manager creation aspect of CoLockObjectExternal when the
988 * object hasn't been marshaled yet */
989 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, TRUE, TRUE);
991 ok_more_than_one_lock();
993 CoDisconnectObject((IUnknown*)&Test_ClassFactory, 0);
997 /* test our empty stub manager being handled correctly in
998 * CoMarshalInterface */
999 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, TRUE, TRUE);
1001 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1002 ok_ole_success(hr, CreateStreamOnHGlobal);
1003 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1004 ok_ole_success(hr, CoMarshalInterface);
1006 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, TRUE, TRUE);
1008 ok_more_than_one_lock();
1010 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1011 hr = CoReleaseMarshalData(pStream);
1012 ok_ole_success(hr, CoReleaseMarshalData);
1013 IStream_Release(pStream);
1015 ok_more_than_one_lock();
1017 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, FALSE, TRUE);
1019 ok_more_than_one_lock();
1021 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, FALSE, TRUE);
1026 /* tests disconnecting stubs */
1027 static void test_disconnect_stub(void)
1030 IStream *pStream = NULL;
1034 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1035 ok_ole_success(hr, CreateStreamOnHGlobal);
1036 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1037 ok_ole_success(hr, CoMarshalInterface);
1039 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, TRUE, TRUE);
1041 ok_more_than_one_lock();
1043 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1044 hr = CoReleaseMarshalData(pStream);
1045 ok_ole_success(hr, CoReleaseMarshalData);
1046 IStream_Release(pStream);
1048 ok_more_than_one_lock();
1050 CoDisconnectObject((IUnknown*)&Test_ClassFactory, 0);
1055 /* tests failure case of a same-thread marshal and unmarshal twice */
1056 static void test_normal_marshal_and_unmarshal_twice(void)
1059 IStream *pStream = NULL;
1060 IUnknown *pProxy1 = NULL;
1061 IUnknown *pProxy2 = NULL;
1065 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1066 ok_ole_success(hr, CreateStreamOnHGlobal);
1067 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1068 ok_ole_success(hr, CoMarshalInterface);
1070 ok_more_than_one_lock();
1072 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1073 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy1);
1074 ok_ole_success(hr, CoUnmarshalInterface);
1076 ok_more_than_one_lock();
1078 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1079 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
1080 ok(hr == CO_E_OBJNOTCONNECTED,
1081 "CoUnmarshalInterface should have failed with error CO_E_OBJNOTCONNECTED for double unmarshal, instead of 0x%08lx\n", hr);
1083 IStream_Release(pStream);
1085 ok_more_than_one_lock();
1087 IUnknown_Release(pProxy1);
1092 /* tests success case of marshaling and unmarshaling an HRESULT */
1093 static void test_hresult_marshaling(void)
1096 HRESULT hr_marshaled = 0;
1097 IStream *pStream = NULL;
1098 static const HRESULT E_DEADBEEF = 0xdeadbeef;
1100 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1101 ok_ole_success(hr, CreateStreamOnHGlobal);
1103 hr = CoMarshalHresult(pStream, E_DEADBEEF);
1104 ok_ole_success(hr, CoMarshalHresult);
1106 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1107 hr = IStream_Read(pStream, &hr_marshaled, sizeof(HRESULT), NULL);
1108 ok_ole_success(hr, IStream_Read);
1110 ok(hr_marshaled == E_DEADBEEF, "Didn't marshal HRESULT as expected: got value 0x%08lx instead\n", hr_marshaled);
1113 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1114 hr = CoUnmarshalHresult(pStream, &hr_marshaled);
1115 ok_ole_success(hr, CoUnmarshalHresult);
1117 ok(hr_marshaled == E_DEADBEEF, "Didn't marshal HRESULT as expected: got value 0x%08lx instead\n", hr_marshaled);
1119 IStream_Release(pStream);
1123 /* helper for test_proxy_used_in_wrong_thread */
1124 static DWORD CALLBACK bad_thread_proc(LPVOID p)
1126 IClassFactory * cf = (IClassFactory *)p;
1128 IUnknown * proxy = NULL;
1130 pCoInitializeEx(NULL, COINIT_MULTITHREADED);
1132 hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
1133 if (proxy) IUnknown_Release(proxy);
1134 ok(hr == RPC_E_WRONG_THREAD,
1135 "COM should have failed with RPC_E_WRONG_THREAD on using proxy from wrong apartment, but instead returned 0x%08lx\n",
1143 /* tests failure case of a using a proxy in the wrong apartment */
1144 static void test_proxy_used_in_wrong_thread(void)
1147 IStream *pStream = NULL;
1148 IUnknown *pProxy = NULL;
1155 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1156 ok_ole_success(hr, CreateStreamOnHGlobal);
1157 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &host_thread);
1159 ok_more_than_one_lock();
1161 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1162 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
1163 ok_ole_success(hr, CoUnmarshalInterface);
1164 IStream_Release(pStream);
1166 ok_more_than_one_lock();
1168 /* create a thread that we can misbehave in */
1169 thread = CreateThread(NULL, 0, bad_thread_proc, (LPVOID)pProxy, 0, &tid2);
1171 WaitForSingleObject(thread, INFINITE);
1172 CloseHandle(thread);
1174 IUnknown_Release(pProxy);
1178 end_host_object(tid, host_thread);
1181 static HRESULT WINAPI MessageFilter_QueryInterface(IMessageFilter *iface, REFIID riid, void ** ppvObj)
1183 if (ppvObj == NULL) return E_POINTER;
1185 if (IsEqualGUID(riid, &IID_IUnknown) ||
1186 IsEqualGUID(riid, &IID_IClassFactory))
1188 *ppvObj = (LPVOID)iface;
1189 IClassFactory_AddRef(iface);
1193 return E_NOINTERFACE;
1196 static ULONG WINAPI MessageFilter_AddRef(IMessageFilter *iface)
1198 return 2; /* non-heap object */
1201 static ULONG WINAPI MessageFilter_Release(IMessageFilter *iface)
1203 return 1; /* non-heap object */
1206 static DWORD WINAPI MessageFilter_HandleInComingCall(
1207 IMessageFilter *iface,
1209 HTASK threadIDCaller,
1211 LPINTERFACEINFO lpInterfaceInfo)
1213 static int callcount = 0;
1215 trace("HandleInComingCall\n");
1219 ret = SERVERCALL_REJECTED;
1222 ret = SERVERCALL_RETRYLATER;
1225 ret = SERVERCALL_ISHANDLED;
1232 static DWORD WINAPI MessageFilter_RetryRejectedCall(
1233 IMessageFilter *iface,
1234 HTASK threadIDCallee,
1238 trace("RetryRejectedCall\n");
1242 static DWORD WINAPI MessageFilter_MessagePending(
1243 IMessageFilter *iface,
1244 HTASK threadIDCallee,
1246 DWORD dwPendingType)
1248 trace("MessagePending\n");
1249 return PENDINGMSG_WAITNOPROCESS;
1252 static const IMessageFilterVtbl MessageFilter_Vtbl =
1254 MessageFilter_QueryInterface,
1255 MessageFilter_AddRef,
1256 MessageFilter_Release,
1257 MessageFilter_HandleInComingCall,
1258 MessageFilter_RetryRejectedCall,
1259 MessageFilter_MessagePending
1262 static IMessageFilter MessageFilter = { &MessageFilter_Vtbl };
1264 static void test_message_filter(void)
1267 IStream *pStream = NULL;
1268 IClassFactory *cf = NULL;
1270 IUnknown *proxy = NULL;
1271 IMessageFilter *prev_filter = NULL;
1276 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1277 ok_ole_success(hr, CreateStreamOnHGlobal);
1278 tid = start_host_object2(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &MessageFilter, &thread);
1280 ok_more_than_one_lock();
1282 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1283 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&cf);
1284 ok_ole_success(hr, CoUnmarshalInterface);
1285 IStream_Release(pStream);
1287 ok_more_than_one_lock();
1289 hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
1290 todo_wine { ok(hr == RPC_E_CALL_REJECTED, "Call should have returned RPC_E_CALL_REJECTED, but return 0x%08lx instead\n", hr); }
1291 if (proxy) IUnknown_Release(proxy);
1294 hr = CoRegisterMessageFilter(&MessageFilter, &prev_filter);
1295 ok_ole_success(hr, CoRegisterMessageFilter);
1296 if (prev_filter) IMessageFilter_Release(prev_filter);
1298 hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
1299 ok_ole_success(hr, IClassFactory_CreateInstance);
1301 IUnknown_Release(proxy);
1303 IClassFactory_Release(cf);
1307 end_host_object(tid, thread);
1310 /* test failure case of trying to unmarshal from bad stream */
1311 static void test_bad_marshal_stream(void)
1314 IStream *pStream = NULL;
1316 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1317 ok_ole_success(hr, CreateStreamOnHGlobal);
1318 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1319 ok_ole_success(hr, CoMarshalInterface);
1321 ok_more_than_one_lock();
1323 /* try to read beyond end of stream */
1324 hr = CoReleaseMarshalData(pStream);
1325 ok(hr == STG_E_READFAULT, "Should have failed with STG_E_READFAULT, but returned 0x%08lx instead\n", hr);
1327 /* now release for real */
1328 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1329 hr = CoReleaseMarshalData(pStream);
1330 ok_ole_success(hr, CoReleaseMarshalData);
1332 IStream_Release(pStream);
1335 /* tests that proxies implement certain interfaces */
1336 static void test_proxy_interfaces(void)
1339 IStream *pStream = NULL;
1340 IUnknown *pProxy = NULL;
1341 IUnknown *pOtherUnknown = NULL;
1347 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1348 ok_ole_success(hr, CreateStreamOnHGlobal);
1349 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
1351 ok_more_than_one_lock();
1353 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1354 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)&pProxy);
1355 ok_ole_success(hr, CoUnmarshalInterface);
1356 IStream_Release(pStream);
1358 ok_more_than_one_lock();
1360 hr = IUnknown_QueryInterface(pProxy, &IID_IUnknown, (LPVOID*)&pOtherUnknown);
1361 ok_ole_success(hr, IUnknown_QueryInterface IID_IUnknown);
1362 if (hr == S_OK) IUnknown_Release(pOtherUnknown);
1364 hr = IUnknown_QueryInterface(pProxy, &IID_IClientSecurity, (LPVOID*)&pOtherUnknown);
1365 todo_wine { ok_ole_success(hr, IUnknown_QueryInterface IID_IClientSecurity); }
1366 if (hr == S_OK) IUnknown_Release(pOtherUnknown);
1368 hr = IUnknown_QueryInterface(pProxy, &IID_IMultiQI, (LPVOID*)&pOtherUnknown);
1369 ok_ole_success(hr, IUnknown_QueryInterface IID_IMultiQI);
1370 if (hr == S_OK) IUnknown_Release(pOtherUnknown);
1372 hr = IUnknown_QueryInterface(pProxy, &IID_IMarshal, (LPVOID*)&pOtherUnknown);
1373 ok_ole_success(hr, IUnknown_QueryInterface IID_IMarshal);
1374 if (hr == S_OK) IUnknown_Release(pOtherUnknown);
1376 /* IMarshal2 is also supported on NT-based systems, but is pretty much
1377 * useless as it has no more methods over IMarshal that it inherits from. */
1379 IUnknown_Release(pProxy);
1383 end_host_object(tid, thread);
1388 const IUnknownVtbl *lpVtbl;
1392 static HRESULT WINAPI HeapUnknown_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
1394 if (IsEqualIID(riid, &IID_IUnknown))
1396 IUnknown_AddRef(iface);
1397 *ppv = (LPVOID)iface;
1401 return E_NOINTERFACE;
1404 static ULONG WINAPI HeapUnknown_AddRef(IUnknown *iface)
1406 HeapUnknown *This = (HeapUnknown *)iface;
1407 trace("HeapUnknown_AddRef(%p)\n", iface);
1408 return InterlockedIncrement((LONG*)&This->refs);
1411 static ULONG WINAPI HeapUnknown_Release(IUnknown *iface)
1413 HeapUnknown *This = (HeapUnknown *)iface;
1414 ULONG refs = InterlockedDecrement((LONG*)&This->refs);
1415 trace("HeapUnknown_Release(%p)\n", iface);
1416 if (!refs) HeapFree(GetProcessHeap(), 0, This);
1420 static const IUnknownVtbl HeapUnknown_Vtbl =
1422 HeapUnknown_QueryInterface,
1427 static void test_proxybuffer(REFIID riid)
1430 IPSFactoryBuffer *psfb;
1431 IRpcProxyBuffer *proxy;
1435 HeapUnknown *pUnkOuter = (HeapUnknown *)HeapAlloc(GetProcessHeap(), 0, sizeof(*pUnkOuter));
1437 pUnkOuter->lpVtbl = &HeapUnknown_Vtbl;
1438 pUnkOuter->refs = 1;
1440 hr = CoGetPSClsid(riid, &clsid);
1441 ok_ole_success(hr, CoGetPSClsid);
1443 hr = CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL, &IID_IPSFactoryBuffer, (LPVOID*)&psfb);
1444 ok_ole_success(hr, CoGetClassObject);
1446 hr = IPSFactoryBuffer_CreateProxy(psfb, (IUnknown*)pUnkOuter, riid, &proxy, &lpvtbl);
1447 ok_ole_success(hr, IPSFactoryBuffer_CreateProxy);
1448 ok(lpvtbl != NULL, "IPSFactoryBuffer_CreateProxy succeeded, but returned a NULL vtable!\n");
1450 /* release our reference to the outer unknown object - the PS factory
1451 * buffer will have AddRef's it in the CreateProxy call */
1452 refs = IUnknown_Release((IUnknown *)pUnkOuter);
1453 ok(refs == 1, "Ref count of outer unknown should have been 1 instead of %ld\n", refs);
1455 refs = IPSFactoryBuffer_Release(psfb);
1456 #if 0 /* not reliable on native. maybe it leaks references! */
1457 ok(refs == 0, "Ref-count leak of %ld on IPSFactoryBuffer\n", refs);
1460 refs = IUnknown_Release((IUnknown *)lpvtbl);
1461 ok(refs == 0, "Ref-count leak of %ld on IRpcProxyBuffer\n", refs);
1463 refs = IRpcProxyBuffer_Release(proxy);
1464 ok(refs == 0, "Ref-count leak of %ld on IRpcProxyBuffer\n", refs);
1467 static void test_stubbuffer(REFIID riid)
1470 IPSFactoryBuffer *psfb;
1471 IRpcStubBuffer *stub;
1477 hr = CoGetPSClsid(riid, &clsid);
1478 ok_ole_success(hr, CoGetPSClsid);
1480 hr = CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL, &IID_IPSFactoryBuffer, (LPVOID*)&psfb);
1481 ok_ole_success(hr, CoGetClassObject);
1483 hr = IPSFactoryBuffer_CreateStub(psfb, riid, (IUnknown*)&Test_ClassFactory, &stub);
1484 ok_ole_success(hr, IPSFactoryBuffer_CreateStub);
1486 refs = IPSFactoryBuffer_Release(psfb);
1487 #if 0 /* not reliable on native. maybe it leaks references */
1488 ok(refs == 0, "Ref-count leak of %ld on IPSFactoryBuffer\n", refs);
1491 ok_more_than_one_lock();
1493 IRpcStubBuffer_Disconnect(stub);
1497 refs = IRpcStubBuffer_Release(stub);
1498 ok(refs == 0, "Ref-count leak of %ld on IRpcProxyBuffer\n", refs);
1501 static HWND hwnd_app;
1503 static HRESULT WINAPI TestRE_IClassFactory_CreateInstance(
1504 LPCLASSFACTORY iface,
1505 LPUNKNOWN pUnkOuter,
1510 if (IsEqualIID(riid, &IID_IWineTest))
1512 BOOL ret = SendMessageTimeout(hwnd_app, WM_NULL, 0, 0, SMTO_BLOCK, 5000, &res);
1513 ok(ret, "Timed out sending a message to originating window during RPC call\n");
1518 static const IClassFactoryVtbl TestREClassFactory_Vtbl =
1520 Test_IClassFactory_QueryInterface,
1521 Test_IClassFactory_AddRef,
1522 Test_IClassFactory_Release,
1523 TestRE_IClassFactory_CreateInstance,
1524 Test_IClassFactory_LockServer
1527 IClassFactory TestRE_ClassFactory = { &TestREClassFactory_Vtbl };
1529 static LRESULT CALLBACK window_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1536 IStream *pStream = NULL;
1537 IClassFactory *proxy = NULL;
1544 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1545 ok_ole_success(hr, CreateStreamOnHGlobal);
1546 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&TestRE_ClassFactory, MSHLFLAGS_NORMAL, &thread);
1548 ok_more_than_one_lock();
1550 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1551 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&proxy);
1552 ok_ole_success(hr, CoReleaseMarshalData);
1553 IStream_Release(pStream);
1555 ok_more_than_one_lock();
1557 /* note the use of the magic IID_IWineTest value to tell remote thread
1558 * to try to send a message back to us */
1559 hr = IClassFactory_CreateInstance(proxy, NULL, &IID_IWineTest, (void **)&object);
1561 IClassFactory_Release(proxy);
1565 end_host_object(tid, thread);
1567 PostMessage(hwnd, WM_QUIT, 0, 0);
1574 IStream *pStream = NULL;
1575 IClassFactory *proxy = NULL;
1582 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1583 ok_ole_success(hr, CreateStreamOnHGlobal);
1584 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&TestRE_ClassFactory, MSHLFLAGS_NORMAL, &thread);
1586 ok_more_than_one_lock();
1588 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1589 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&proxy);
1590 ok_ole_success(hr, CoReleaseMarshalData);
1591 IStream_Release(pStream);
1593 ok_more_than_one_lock();
1595 /* post quit message before a doing a COM call to show that a pending
1596 * WM_QUIT message doesn't stop the call from succeeding */
1597 PostMessage(hwnd, WM_QUIT, 0, 0);
1598 hr = IClassFactory_CreateInstance(proxy, NULL, &IID_IUnknown, (void **)&object);
1600 IClassFactory_Release(proxy);
1604 end_host_object(tid, thread);
1609 return DefWindowProc(hwnd, msg, wparam, lparam);
1613 static void test_message_reentrancy(void)
1618 memset(&wndclass, 0, sizeof(wndclass));
1619 wndclass.lpfnWndProc = window_proc;
1620 wndclass.lpszClassName = "WineCOMTest";
1621 RegisterClass(&wndclass);
1623 hwnd_app = CreateWindow("WineCOMTest", NULL, 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, NULL, 0);
1624 ok(hwnd_app != NULL, "Window creation failed\n");
1626 /* start message re-entrancy test */
1627 PostMessage(hwnd_app, WM_USER, 0, 0);
1629 while (GetMessage(&msg, NULL, 0, 0))
1631 TranslateMessage(&msg);
1632 DispatchMessage(&msg);
1636 static void test_WM_QUIT_handling(void)
1640 hwnd_app = CreateWindow("WineCOMTest", NULL, 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, NULL, 0);
1641 ok(hwnd_app != NULL, "Window creation failed\n");
1643 /* start WM_QUIT handling test */
1644 PostMessage(hwnd_app, WM_USER+1, 0, 0);
1646 while (GetMessage(&msg, NULL, 0, 0))
1648 TranslateMessage(&msg);
1649 DispatchMessage(&msg);
1653 static void test_freethreadedmarshaldata(IStream *pStream, MSHCTX mshctx, void *ptr, DWORD mshlflags)
1660 hr = GetHGlobalFromStream(pStream, &hglobal);
1661 ok_ole_success(hr, GetHGlobalFromStream);
1663 size = GlobalSize(hglobal);
1665 marshal_data = (char *)GlobalLock(hglobal);
1667 if (mshctx == MSHCTX_INPROC)
1669 DWORD expected_size = sizeof(DWORD) + sizeof(void *) + sizeof(DWORD) + sizeof(GUID);
1670 ok(size == expected_size, "size should have been %ld instead of %ld\n", expected_size, size);
1672 ok(*(DWORD *)marshal_data == mshlflags, "expected 0x%lx, but got 0x%lx for mshctx\n", mshlflags, *(DWORD *)marshal_data);
1673 marshal_data += sizeof(DWORD);
1674 ok(*(void **)marshal_data == ptr, "expected %p, but got %p for mshctx\n", ptr, *(void **)marshal_data);
1675 marshal_data += sizeof(void *);
1676 ok(*(DWORD *)marshal_data == 0, "expected 0x0, but got 0x%lx\n", *(DWORD *)marshal_data);
1677 marshal_data += sizeof(DWORD);
1678 trace("got guid data: {%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\n",
1679 ((GUID *)marshal_data)->Data1, ((GUID *)marshal_data)->Data2, ((GUID *)marshal_data)->Data3,
1680 ((GUID *)marshal_data)->Data4[0], ((GUID *)marshal_data)->Data4[1], ((GUID *)marshal_data)->Data4[2], ((GUID *)marshal_data)->Data4[3],
1681 ((GUID *)marshal_data)->Data4[4], ((GUID *)marshal_data)->Data4[5], ((GUID *)marshal_data)->Data4[6], ((GUID *)marshal_data)->Data4[7]);
1685 ok(size > sizeof(DWORD), "size should have been > sizeof(DWORD), not %ld\n", size);
1686 ok(*(DWORD *)marshal_data == 0x574f454d /* MEOW */,
1687 "marshal data should be filled by standard marshal and start with MEOW signature\n");
1690 GlobalUnlock(hglobal);
1693 static void test_freethreadedmarshaler(void)
1696 IUnknown *pFTUnknown;
1697 IMarshal *pFTMarshal;
1700 static const LARGE_INTEGER llZero;
1703 hr = CoCreateFreeThreadedMarshaler(NULL, &pFTUnknown);
1704 ok_ole_success(hr, CoCreateFreeThreadedMarshaler);
1705 hr = IUnknown_QueryInterface(pFTUnknown, &IID_IMarshal, (void **)&pFTMarshal);
1706 ok_ole_success(hr, IUnknown_QueryInterface);
1707 IUnknown_Release(pFTUnknown);
1709 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1710 ok_ole_success(hr, CreateStreamOnHGlobal);
1712 /* inproc normal marshaling */
1714 hr = IMarshal_MarshalInterface(pFTMarshal, pStream, &IID_IClassFactory,
1715 (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1716 ok_ole_success(hr, IMarshal_MarshalInterface);
1718 ok_more_than_one_lock();
1720 test_freethreadedmarshaldata(pStream, MSHCTX_INPROC, &Test_ClassFactory, MSHLFLAGS_NORMAL);
1722 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
1723 hr = IMarshal_UnmarshalInterface(pFTMarshal, pStream, &IID_IUnknown, (void **)&pProxy);
1724 ok_ole_success(hr, IMarshal_UnmarshalInterface);
1726 IUnknown_Release(pProxy);
1730 /* native doesn't allow us to unmarshal or release the stream data,
1731 * presumably because it wants us to call CoMarshalInterface instead */
1733 /* local normal marshaling */
1735 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
1736 hr = IMarshal_MarshalInterface(pFTMarshal, pStream, IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_LOCAL, NULL, MSHLFLAGS_NORMAL);
1737 ok_ole_success(hr, IMarshal_MarshalInterface);
1739 ok_more_than_one_lock();
1741 test_freethreadedmarshaldata(pStream, MSHCTX_LOCAL, &Test_ClassFactory, MSHLFLAGS_NORMAL);
1743 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
1744 hr = IMarshal_ReleaseMarshalData(pFTMarshal, pStream);
1745 ok_ole_success(hr, IMarshal_ReleaseMarshalData);
1750 /* inproc table-strong marshaling */
1752 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
1753 hr = IMarshal_MarshalInterface(pFTMarshal, pStream, &IID_IClassFactory,
1754 (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, (void *)0xdeadbeef,
1755 MSHLFLAGS_TABLESTRONG);
1756 ok_ole_success(hr, IMarshal_MarshalInterface);
1758 ok_more_than_one_lock();
1760 test_freethreadedmarshaldata(pStream, MSHCTX_INPROC, &Test_ClassFactory, MSHLFLAGS_TABLESTRONG);
1762 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
1763 hr = IMarshal_UnmarshalInterface(pFTMarshal, pStream, &IID_IUnknown, (void **)&pProxy);
1764 ok_ole_success(hr, IMarshal_UnmarshalInterface);
1766 IUnknown_Release(pProxy);
1768 ok_more_than_one_lock();
1770 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
1771 hr = IMarshal_ReleaseMarshalData(pFTMarshal, pStream);
1772 ok_ole_success(hr, IMarshal_ReleaseMarshalData);
1776 /* inproc table-weak marshaling */
1778 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
1779 hr = IMarshal_MarshalInterface(pFTMarshal, pStream, &IID_IClassFactory,
1780 (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, (void *)0xdeadbeef,
1781 MSHLFLAGS_TABLEWEAK);
1782 ok_ole_success(hr, IMarshal_MarshalInterface);
1786 test_freethreadedmarshaldata(pStream, MSHCTX_INPROC, &Test_ClassFactory, MSHLFLAGS_TABLEWEAK);
1788 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
1789 hr = IMarshal_UnmarshalInterface(pFTMarshal, pStream, &IID_IUnknown, (void **)&pProxy);
1790 ok_ole_success(hr, IMarshal_UnmarshalInterface);
1792 ok_more_than_one_lock();
1794 IUnknown_Release(pProxy);
1798 /* inproc normal marshaling (for extraordinary cases) */
1800 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
1801 hr = IMarshal_MarshalInterface(pFTMarshal, pStream, &IID_IClassFactory,
1802 (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1803 ok_ole_success(hr, IMarshal_MarshalInterface);
1805 ok_more_than_one_lock();
1807 /* this call shows that DisconnectObject does nothing */
1808 hr = IMarshal_DisconnectObject(pFTMarshal, 0);
1809 ok_ole_success(hr, IMarshal_DisconnectObject);
1811 ok_more_than_one_lock();
1813 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
1814 hr = IMarshal_ReleaseMarshalData(pFTMarshal, pStream);
1815 ok_ole_success(hr, IMarshal_ReleaseMarshalData);
1819 /* doesn't enforce marshaling rules here and allows us to unmarshal the
1820 * interface, even though it was freed above */
1821 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
1822 hr = IMarshal_UnmarshalInterface(pFTMarshal, pStream, &IID_IUnknown, (void **)&pProxy);
1823 ok_ole_success(hr, IMarshal_UnmarshalInterface);
1827 IStream_Release(pStream);
1828 IMarshal_Release(pFTMarshal);
1831 static HANDLE heventShutdown;
1833 static void LockModuleOOP(void)
1835 InterlockedIncrement(&cLocks); /* for test purposes only */
1836 CoAddRefServerProcess();
1839 static void UnlockModuleOOP(void)
1841 InterlockedDecrement(&cLocks); /* for test purposes only */
1842 if (!CoReleaseServerProcess())
1843 SetEvent(heventShutdown);
1846 static HWND hwnd_app;
1848 static HRESULT WINAPI TestOOP_IClassFactory_QueryInterface(
1849 LPCLASSFACTORY iface,
1853 if (ppvObj == NULL) return E_POINTER;
1855 if (IsEqualGUID(riid, &IID_IUnknown) ||
1856 IsEqualGUID(riid, &IID_IClassFactory))
1858 *ppvObj = (LPVOID)iface;
1859 IClassFactory_AddRef(iface);
1863 return E_NOINTERFACE;
1866 static ULONG WINAPI TestOOP_IClassFactory_AddRef(LPCLASSFACTORY iface)
1868 return 2; /* non-heap-based object */
1871 static ULONG WINAPI TestOOP_IClassFactory_Release(LPCLASSFACTORY iface)
1873 return 1; /* non-heap-based object */
1876 static HRESULT WINAPI TestOOP_IClassFactory_CreateInstance(
1877 LPCLASSFACTORY iface,
1878 LPUNKNOWN pUnkOuter,
1882 return CLASS_E_CLASSNOTAVAILABLE;
1885 static HRESULT WINAPI TestOOP_IClassFactory_LockServer(
1886 LPCLASSFACTORY iface,
1896 static const IClassFactoryVtbl TestClassFactoryOOP_Vtbl =
1898 TestOOP_IClassFactory_QueryInterface,
1899 TestOOP_IClassFactory_AddRef,
1900 TestOOP_IClassFactory_Release,
1901 TestOOP_IClassFactory_CreateInstance,
1902 TestOOP_IClassFactory_LockServer
1905 static IClassFactory TestOOP_ClassFactory = { &TestClassFactoryOOP_Vtbl };
1907 /* tests functions commonly used by out of process COM servers */
1908 static void test_out_of_process_com(void)
1910 static const CLSID CLSID_WineOOPTest = {
1914 {0xa1, 0xa2, 0x5d, 0x5a, 0x36, 0x54, 0xd3, 0xbd}
1915 }; /* 5201163f-8164-4fd0-a1a2-5d5a3654d3bd */
1921 heventShutdown = CreateEvent(NULL, TRUE, FALSE, NULL);
1925 /* Start the object suspended */
1926 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&TestOOP_ClassFactory,
1927 CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE | REGCLS_SUSPENDED, &cookie);
1928 ok_ole_success(hr, CoRegisterClassObject);
1930 /* ... and CoGetClassObject does not find it and fails when it looks for the
1931 * class in the registry */
1932 hr = CoGetClassObject(&CLSID_WineOOPTest, CLSCTX_INPROC_SERVER,
1933 NULL, &IID_IClassFactory, (LPVOID*)&cf);
1935 ok(hr == REGDB_E_CLASSNOTREG,
1936 "CoGetClassObject should have returned REGDB_E_CLASSNOTREG instead of 0x%08lx\n", hr);
1939 /* Resume the object suspended above ... */
1940 hr = CoResumeClassObjects();
1941 ok_ole_success(hr, CoResumeClassObjects);
1943 /* ... and now it should succeed */
1944 hr = CoGetClassObject(&CLSID_WineOOPTest, CLSCTX_INPROC_SERVER,
1945 NULL, &IID_IClassFactory, (LPVOID*)&cf);
1946 ok_ole_success(hr, CoGetClassObject);
1948 /* Now check the locking is working */
1949 /* NOTE: we are accessing the class directly, not through a proxy */
1953 hr = IClassFactory_LockServer(cf, TRUE);
1954 trace("IClassFactory_LockServer returned 0x%08lx\n", hr);
1956 ok_more_than_one_lock();
1958 IClassFactory_LockServer(cf, FALSE);
1962 IClassFactory_Release(cf);
1964 /* wait for shutdown signal */
1965 ret = WaitForSingleObject(heventShutdown, 5000);
1966 todo_wine { ok(ret != WAIT_TIMEOUT, "Server didn't shut down or machine is under very heavy load\n"); }
1968 /* try to connect again after SCM has suspended registered class objects */
1969 hr = CoGetClassObject(&CLSID_WineOOPTest, CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER, NULL,
1970 &IID_IClassFactory, (LPVOID*)&cf);
1972 ok(hr == CO_E_SERVER_STOPPING,
1973 "CoGetClassObject should have returned CO_E_SERVER_STOPPING instead of 0x%08lx\n", hr);
1976 hr = CoRevokeClassObject(cookie);
1977 ok_ole_success(hr, CoRevokeClassObject);
1979 CloseHandle(heventShutdown);
1982 static void test_ROT(void)
1984 static const WCHAR wszFileName[] = {'B','E','2','0','E','2','F','5','-',
1985 '1','9','0','3','-','4','A','A','E','-','B','1','A','F','-',
1986 '2','0','4','6','E','5','8','6','C','9','2','5',0};
1988 IMoniker *pMoniker = NULL;
1989 IRunningObjectTable *pROT = NULL;
1994 hr = CreateFileMoniker(wszFileName, &pMoniker);
1995 ok_ole_success(hr, CreateClassMoniker);
1996 hr = GetRunningObjectTable(0, &pROT);
1997 ok_ole_success(hr, GetRunningObjectTable);
1998 hr = IRunningObjectTable_Register(pROT, 0, (IUnknown*)&Test_ClassFactory, pMoniker, &dwCookie);
1999 ok_ole_success(hr, IRunningObjectTable_Register);
2001 ok_more_than_one_lock();
2003 hr = IRunningObjectTable_Revoke(pROT, dwCookie);
2004 ok_ole_success(hr, IRunningObjectTable_Revoke);
2012 IGlobalInterfaceTable *git;
2015 static DWORD CALLBACK get_global_interface_proc(LPVOID pv)
2018 struct git_params *params = (struct git_params *)pv;
2021 hr = IGlobalInterfaceTable_GetInterfaceFromGlobal(params->git, params->cookie, &IID_IClassFactory, (void **)&cf);
2022 ok(hr == CO_E_NOTINITIALIZED,
2023 "IGlobalInterfaceTable_GetInterfaceFromGlobal should have failed with error CO_E_NOTINITIALIZED instead of 0x%08lx\n",
2027 hr = IGlobalInterfaceTable_GetInterfaceFromGlobal(params->git, params->cookie, &IID_IClassFactory, (void **)&cf);
2028 ok_ole_success(hr, IGlobalInterfaceTable_GetInterfaceFromGlobal);
2034 static void test_globalinterfacetable(void)
2037 IGlobalInterfaceTable *git;
2041 struct git_params params;
2044 hr = CoCreateInstance(&CLSID_StdGlobalInterfaceTable, NULL, CLSCTX_INPROC_SERVER, &IID_IGlobalInterfaceTable, (void **)&git);
2045 ok_ole_success(hr, CoCreateInstance);
2047 hr = IGlobalInterfaceTable_RegisterInterfaceInGlobal(git, (IUnknown *)&Test_ClassFactory, &IID_IClassFactory, &cookie);
2048 ok_ole_success(hr, IGlobalInterfaceTable_RegisterInterfaceInGlobal);
2050 params.cookie = cookie;
2052 /* note: params is on stack so we MUST wait for get_global_interface_proc
2053 * to exit before we can return */
2054 thread = CreateThread(NULL, 0, get_global_interface_proc, ¶ms, 0, &tid);
2056 ret = MsgWaitForMultipleObjects(1, &thread, FALSE, INFINITE, QS_ALLINPUT);
2057 while (ret == WAIT_OBJECT_0 + 1)
2060 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
2061 DispatchMessage(&msg);
2062 ret = MsgWaitForMultipleObjects(1, &thread, FALSE, INFINITE, QS_ALLINPUT);
2065 CloseHandle(thread);
2068 static const char cf_marshaled[] =
2080 static void test_marshal_CLIPFORMAT(void)
2082 unsigned char *buffer;
2084 unsigned long flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
2085 wireCLIPFORMAT wirecf;
2086 CLIPFORMAT cf = RegisterClipboardFormatA("MyFormat");
2089 size = CLIPFORMAT_UserSize(&flags, 0, &cf);
2090 ok(size == sizeof(*wirecf) + sizeof(cf_marshaled), "Wrong size %ld\n", size);
2092 buffer = HeapAlloc(GetProcessHeap(), 0, size);
2093 CLIPFORMAT_UserMarshal(&flags, buffer, &cf);
2094 wirecf = (wireCLIPFORMAT)buffer;
2095 ok(wirecf->fContext == WDT_REMOTE_CALL, "Context should be WDT_REMOTE_CALL instead of 0x%08lx\n", wirecf->fContext);
2096 ok(wirecf->u.dwValue == cf, "Marshaled value should be 0x%04x instead of 0x%04lx\n", cf, wirecf->u.dwValue);
2097 ok(!memcmp(wirecf+1, cf_marshaled, sizeof(cf_marshaled)), "Marshaled data differs\n");
2099 CLIPFORMAT_UserUnmarshal(&flags, buffer, &cf2);
2100 ok(cf == cf2, "Didn't unmarshal properly\n");
2101 HeapFree(GetProcessHeap(), 0, buffer);
2103 CLIPFORMAT_UserFree(&flags, &cf2);
2106 static void test_marshal_HWND(void)
2108 unsigned char *buffer;
2110 unsigned long flags = MAKELONG(MSHCTX_LOCAL, NDR_LOCAL_DATA_REPRESENTATION);
2111 HWND hwnd = GetDesktopWindow();
2115 size = HWND_UserSize(&flags, 0, &hwnd);
2116 ok(size == sizeof(*wirehwnd), "Wrong size %ld\n", size);
2118 buffer = HeapAlloc(GetProcessHeap(), 0, size);
2119 HWND_UserMarshal(&flags, buffer, &hwnd);
2120 wirehwnd = (wireHWND)buffer;
2121 ok(wirehwnd->fContext == WDT_INPROC_CALL, "Context should be WDT_INPROC_CALL instead of 0x%08lx\n", wirehwnd->fContext);
2122 ok(wirehwnd->u.hInproc == (LONG_PTR)hwnd, "Marshaled value should be %p instead of %p\n", hwnd, (HANDLE)wirehwnd->u.hRemote);
2124 HWND_UserUnmarshal(&flags, buffer, &hwnd2);
2125 ok(hwnd == hwnd2, "Didn't unmarshal properly\n");
2126 HeapFree(GetProcessHeap(), 0, buffer);
2128 HWND_UserFree(&flags, &hwnd2);
2131 static void test_marshal_HGLOBAL(void)
2133 unsigned char *buffer;
2135 unsigned long flags = MAKELONG(MSHCTX_LOCAL, NDR_LOCAL_DATA_REPRESENTATION);
2138 unsigned char *wirehglobal;
2142 flags = MAKELONG(MSHCTX_LOCAL, NDR_LOCAL_DATA_REPRESENTATION);
2143 size = HGLOBAL_UserSize(&flags, 0, &hglobal);
2144 /* native is poorly programmed and allocates 4 bytes more than it needs to
2145 * here - Wine doesn't have to emulate that */
2146 ok((size == 8) || (size == 12), "Size should be 12, instead of %ld\n", size);
2147 buffer = HeapAlloc(GetProcessHeap(), 0, size);
2148 HGLOBAL_UserMarshal(&flags, buffer, &hglobal);
2149 wirehglobal = buffer;
2150 ok(*(ULONG *)wirehglobal == WDT_REMOTE_CALL, "Context should be WDT_REMOTE_CALL instead of 0x%08lx\n", *(ULONG *)wirehglobal);
2151 wirehglobal += sizeof(ULONG);
2152 ok(*(ULONG *)wirehglobal == (ULONG)hglobal, "buffer+4 should be HGLOBAL\n");
2153 HGLOBAL_UserUnmarshal(&flags, buffer, &hglobal2);
2154 ok(hglobal2 == hglobal, "Didn't unmarshal properly\n");
2155 HeapFree(GetProcessHeap(), 0, buffer);
2156 HGLOBAL_UserFree(&flags, &hglobal2);
2158 hglobal = GlobalAlloc(0, 4);
2159 buffer = GlobalLock(hglobal);
2160 for (i = 0; i < 4; i++)
2162 GlobalUnlock(hglobal);
2163 flags = MAKELONG(MSHCTX_LOCAL, NDR_LOCAL_DATA_REPRESENTATION);
2164 size = HGLOBAL_UserSize(&flags, 0, &hglobal);
2165 /* native is poorly programmed and allocates 4 bytes more than it needs to
2166 * here - Wine doesn't have to emulate that */
2167 ok((size == 24) || (size == 28), "Size should be 24 or 28, instead of %ld\n", size);
2168 buffer = HeapAlloc(GetProcessHeap(), 0, size);
2169 HGLOBAL_UserMarshal(&flags, buffer, &hglobal);
2170 wirehglobal = buffer;
2171 ok(*(ULONG *)wirehglobal == WDT_REMOTE_CALL, "Context should be WDT_REMOTE_CALL instead of 0x%08lx\n", *(ULONG *)wirehglobal);
2172 wirehglobal += sizeof(ULONG);
2173 ok(*(ULONG *)wirehglobal == (ULONG)hglobal, "buffer+0x4 should be HGLOBAL\n");
2174 wirehglobal += sizeof(ULONG);
2175 ok(*(ULONG *)wirehglobal == 4, "buffer+0x8 should be size of HGLOBAL\n");
2176 wirehglobal += sizeof(ULONG);
2177 ok(*(ULONG *)wirehglobal == (ULONG)hglobal, "buffer+0xc should be HGLOBAL\n");
2178 wirehglobal += sizeof(ULONG);
2179 ok(*(ULONG *)wirehglobal == 4, "buffer+0x10 should be size of HGLOBAL\n");
2180 wirehglobal += sizeof(ULONG);
2181 for (i = 0; i < 4; i++)
2182 ok(wirehglobal[i] == i, "buffer+0x%x should be %d\n", 0x10 + i, i);
2183 HGLOBAL_UserUnmarshal(&flags, buffer, &hglobal2);
2184 ok(hglobal2 != NULL, "Didn't unmarshal properly\n");
2185 HeapFree(GetProcessHeap(), 0, buffer);
2186 HGLOBAL_UserFree(&flags, &hglobal2);
2187 GlobalFree(hglobal);
2190 static HENHMETAFILE create_emf(void)
2192 RECT rect = {0, 0, 100, 100};
2193 HDC hdc = CreateEnhMetaFile(NULL, NULL, &rect, "HENHMETAFILE Marshaling Test\0Test\0\0");
2194 ExtTextOut(hdc, 0, 0, ETO_OPAQUE, NULL, "Test String", strlen("Test String"), NULL);
2195 return CloseEnhMetaFile(hdc);
2198 static void test_marshal_HENHMETAFILE(void)
2200 unsigned char *buffer;
2202 unsigned long flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
2204 HENHMETAFILE hemf2 = NULL;
2205 unsigned char *wirehemf;
2207 hemf = create_emf();
2209 size = HENHMETAFILE_UserSize(&flags, 0, &hemf);
2210 ok(size > 20, "size should be at least 20 bytes, not %ld\n", size);
2211 buffer = HeapAlloc(GetProcessHeap(), 0, size);
2212 HENHMETAFILE_UserMarshal(&flags, buffer, &hemf);
2214 ok(*(DWORD *)wirehemf == WDT_REMOTE_CALL, "wirestgm + 0x0 should be WDT_REMOTE_CALL instead of 0x%08lx\n", *(DWORD *)wirehemf);
2215 wirehemf += sizeof(DWORD);
2216 ok(*(DWORD *)wirehemf == (DWORD)(DWORD_PTR)hemf, "wirestgm + 0x4 should be hemf instead of 0x%08lx\n", *(DWORD *)wirehemf);
2217 wirehemf += sizeof(DWORD);
2218 ok(*(DWORD *)wirehemf == (size - 0x10), "wirestgm + 0x8 should be size - 0x10 instead of 0x%08lx\n", *(DWORD *)wirehemf);
2219 wirehemf += sizeof(DWORD);
2220 ok(*(DWORD *)wirehemf == (size - 0x10), "wirestgm + 0xc should be size - 0x10 instead of 0x%08lx\n", *(DWORD *)wirehemf);
2221 wirehemf += sizeof(DWORD);
2222 ok(*(DWORD *)wirehemf == EMR_HEADER, "wirestgm + 0x10 should be EMR_HEADER instead of %ld\n", *(DWORD *)wirehemf);
2223 wirehemf += sizeof(DWORD);
2224 /* ... rest of data not tested - refer to tests for GetEnhMetaFileBits
2227 HENHMETAFILE_UserUnmarshal(&flags, buffer, &hemf2);
2228 ok(hemf2 != NULL, "HENHMETAFILE didn't unmarshal\n");
2229 HeapFree(GetProcessHeap(), 0, buffer);
2230 HENHMETAFILE_UserFree(&flags, &hemf2);
2231 DeleteEnhMetaFile(hemf);
2236 size = HENHMETAFILE_UserSize(&flags, 0, &hemf);
2237 ok(size == 8, "size should be 8 bytes, not %ld\n", size);
2238 buffer = (unsigned char *)HeapAlloc(GetProcessHeap(), 0, size);
2239 HENHMETAFILE_UserMarshal(&flags, buffer, &hemf);
2241 ok(*(DWORD *)wirehemf == WDT_REMOTE_CALL, "wirestgm + 0x0 should be WDT_REMOTE_CALL instead of 0x%08lx\n", *(DWORD *)wirehemf);
2242 wirehemf += sizeof(DWORD);
2243 ok(*(DWORD *)wirehemf == (DWORD)(DWORD_PTR)hemf, "wirestgm + 0x4 should be hemf instead of 0x%08lx\n", *(DWORD *)wirehemf);
2244 wirehemf += sizeof(DWORD);
2246 HENHMETAFILE_UserUnmarshal(&flags, buffer, &hemf2);
2247 ok(hemf2 == NULL, "NULL HENHMETAFILE didn't unmarshal\n");
2248 HeapFree(GetProcessHeap(), 0, buffer);
2249 HENHMETAFILE_UserFree(&flags, &hemf2);
2255 HMODULE hOle32 = GetModuleHandle("ole32");
2256 if (!(pCoInitializeEx = (void*)GetProcAddress(hOle32, "CoInitializeEx"))) goto no_test;
2258 /* register a window class used in several tests */
2259 memset(&wndclass, 0, sizeof(wndclass));
2260 wndclass.lpfnWndProc = window_proc;
2261 wndclass.lpszClassName = "WineCOMTest";
2262 RegisterClass(&wndclass);
2264 test_cocreateinstance_proxy();
2266 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
2268 /* FIXME: test CoCreateInstanceEx */
2270 /* lifecycle management and marshaling tests */
2271 test_no_marshaler();
2272 test_normal_marshal_and_release();
2273 test_normal_marshal_and_unmarshal();
2274 test_marshal_and_unmarshal_invalid();
2275 test_interthread_marshal_and_unmarshal();
2276 test_proxy_marshal_and_unmarshal();
2277 test_proxy_marshal_and_unmarshal2();
2278 test_marshal_stub_apartment_shutdown();
2279 test_marshal_proxy_apartment_shutdown();
2280 test_marshal_proxy_mta_apartment_shutdown();
2281 test_no_couninitialize_server();
2282 test_no_couninitialize_client();
2283 test_tableweak_marshal_and_unmarshal_twice();
2284 test_tableweak_marshal_releasedata1();
2285 test_tableweak_marshal_releasedata2();
2286 test_tablestrong_marshal_and_unmarshal_twice();
2287 test_lock_object_external();
2288 test_disconnect_stub();
2289 test_normal_marshal_and_unmarshal_twice();
2290 test_hresult_marshaling();
2291 test_proxy_used_in_wrong_thread();
2292 test_message_filter();
2293 test_bad_marshal_stream();
2294 test_proxy_interfaces();
2295 test_stubbuffer(&IID_IClassFactory);
2296 test_proxybuffer(&IID_IClassFactory);
2297 test_message_reentrancy();
2298 test_WM_QUIT_handling();
2299 test_freethreadedmarshaler();
2301 /* doesn't pass with Win9x COM DLLs (even though Essential COM says it should) */
2302 if (0) test_out_of_process_com();
2305 test_globalinterfacetable();
2307 test_marshal_CLIPFORMAT();
2308 test_marshal_HWND();
2309 test_marshal_HGLOBAL();
2310 test_marshal_HENHMETAFILE();
2316 trace("You need DCOM95 installed to run this test\n");