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
30 #include "wine/test.h"
32 /* functions that are not present on all versions of Windows */
33 HRESULT (WINAPI * pCoInitializeEx)(LPVOID lpReserved, DWORD dwCoInit);
35 /* helper macros to make tests a bit leaner */
36 #define ok_more_than_one_lock() ok(cLocks > 0, "Number of locks should be > 0, but actually is %ld\n", cLocks)
37 #define ok_no_locks() ok(cLocks == 0, "Number of locks should be 0, but actually is %ld\n", cLocks)
38 #define ok_ole_success(hr, func) ok(hr == S_OK, #func " failed with error 0x%08lx\n", hr)
40 static const IID IID_IWineTest =
45 {0xa1, 0xa2, 0x5d, 0x5a, 0x36, 0x54, 0xd3, 0xbd}
46 }; /* 5201163f-8164-4fd0-a1a2-5d5a3654d3bd */
48 static void test_CoGetPSClsid(void)
53 hr = CoGetPSClsid(&IID_IClassFactory, &clsid);
54 ok_ole_success(hr, CoGetPSClsid);
56 hr = CoGetPSClsid(&IID_IWineTest, &clsid);
57 ok(hr == REGDB_E_IIDNOTREG,
58 "CoGetPSClsid for random IID returned 0x%08lx instead of REGDB_E_IIDNOTREG\n",
62 static const LARGE_INTEGER ullZero;
65 static void LockModule(void)
67 InterlockedIncrement(&cLocks);
70 static void UnlockModule(void)
72 InterlockedDecrement(&cLocks);
76 static HRESULT WINAPI Test_IUnknown_QueryInterface(
81 if (ppvObj == NULL) return E_POINTER;
83 if (IsEqualGUID(riid, &IID_IUnknown))
85 *ppvObj = (LPVOID)iface;
86 IUnknown_AddRef(iface);
94 static ULONG WINAPI Test_IUnknown_AddRef(LPUNKNOWN iface)
97 return 2; /* non-heap-based object */
100 static ULONG WINAPI Test_IUnknown_Release(LPUNKNOWN iface)
103 return 1; /* non-heap-based object */
106 static const IUnknownVtbl TestUnknown_Vtbl =
108 Test_IUnknown_QueryInterface,
109 Test_IUnknown_AddRef,
110 Test_IUnknown_Release,
113 static IUnknown Test_Unknown = { &TestUnknown_Vtbl };
116 static HRESULT WINAPI Test_IClassFactory_QueryInterface(
117 LPCLASSFACTORY iface,
121 if (ppvObj == NULL) return E_POINTER;
123 if (IsEqualGUID(riid, &IID_IUnknown) ||
124 IsEqualGUID(riid, &IID_IClassFactory))
126 *ppvObj = (LPVOID)iface;
127 IClassFactory_AddRef(iface);
132 return E_NOINTERFACE;
135 static ULONG WINAPI Test_IClassFactory_AddRef(LPCLASSFACTORY iface)
138 return 2; /* non-heap-based object */
141 static ULONG WINAPI Test_IClassFactory_Release(LPCLASSFACTORY iface)
144 return 1; /* non-heap-based object */
147 static HRESULT WINAPI Test_IClassFactory_CreateInstance(
148 LPCLASSFACTORY iface,
153 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
154 return IUnknown_QueryInterface((IUnknown*)&Test_Unknown, riid, ppvObj);
157 static HRESULT WINAPI Test_IClassFactory_LockServer(
158 LPCLASSFACTORY iface,
164 static const IClassFactoryVtbl TestClassFactory_Vtbl =
166 Test_IClassFactory_QueryInterface,
167 Test_IClassFactory_AddRef,
168 Test_IClassFactory_Release,
169 Test_IClassFactory_CreateInstance,
170 Test_IClassFactory_LockServer
173 static IClassFactory Test_ClassFactory = { &TestClassFactory_Vtbl };
175 #define RELEASEMARSHALDATA WM_USER
177 struct host_object_data
182 MSHLFLAGS marshal_flags;
183 HANDLE marshal_event;
184 IMessageFilter *filter;
187 static DWORD CALLBACK host_object_proc(LPVOID p)
189 struct host_object_data *data = (struct host_object_data *)p;
193 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
197 IMessageFilter * prev_filter = NULL;
198 hr = CoRegisterMessageFilter(data->filter, &prev_filter);
199 if (prev_filter) IMessageFilter_Release(prev_filter);
200 ok_ole_success(hr, CoRegisterMessageFilter);
203 hr = CoMarshalInterface(data->stream, &data->iid, data->object, MSHCTX_INPROC, NULL, data->marshal_flags);
204 ok_ole_success(hr, CoMarshalInterface);
206 /* force the message queue to be created before signaling parent thread */
207 PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
209 SetEvent(data->marshal_event);
211 while (GetMessage(&msg, NULL, 0, 0))
213 if (msg.hwnd == NULL && msg.message == RELEASEMARSHALDATA)
215 trace("releasing marshal data\n");
216 CoReleaseMarshalData(data->stream);
217 SetEvent((HANDLE)msg.lParam);
220 DispatchMessage(&msg);
223 HeapFree(GetProcessHeap(), 0, data);
230 static DWORD start_host_object2(IStream *stream, REFIID riid, IUnknown *object, MSHLFLAGS marshal_flags, IMessageFilter *filter, HANDLE *thread)
233 HANDLE marshal_event = CreateEvent(NULL, FALSE, FALSE, NULL);
234 struct host_object_data *data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data));
236 data->stream = stream;
238 data->object = object;
239 data->marshal_flags = marshal_flags;
240 data->marshal_event = marshal_event;
241 data->filter = filter;
243 *thread = CreateThread(NULL, 0, host_object_proc, data, 0, &tid);
245 /* wait for marshaling to complete before returning */
246 WaitForSingleObject(marshal_event, INFINITE);
247 CloseHandle(marshal_event);
252 static DWORD start_host_object(IStream *stream, REFIID riid, IUnknown *object, MSHLFLAGS marshal_flags, HANDLE *thread)
254 return start_host_object2(stream, riid, object, marshal_flags, NULL, thread);
257 /* asks thread to release the marshal data because it has to be done by the
258 * same thread that marshaled the interface in the first place. */
259 static void release_host_object(DWORD tid)
261 HANDLE event = CreateEvent(NULL, FALSE, FALSE, NULL);
262 PostThreadMessage(tid, RELEASEMARSHALDATA, 0, (LPARAM)event);
263 WaitForSingleObject(event, INFINITE);
267 static void end_host_object(DWORD tid, HANDLE thread)
269 BOOL ret = PostThreadMessage(tid, WM_QUIT, 0, 0);
270 ok(ret, "PostThreadMessage failed with error %ld\n", GetLastError());
271 /* be careful of races - don't return until hosting thread has terminated */
272 WaitForSingleObject(thread, INFINITE);
276 /* tests failure case of interface not having a marshaler specified in the
278 static void test_no_marshaler(void)
283 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
284 ok_ole_success(hr, CreateStreamOnHGlobal);
285 hr = CoMarshalInterface(pStream, &IID_IWineTest, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
286 ok(hr == E_NOINTERFACE, "CoMarshalInterface should have returned E_NOINTERFACE instead of 0x%08lx\n", hr);
288 IStream_Release(pStream);
291 /* tests normal marshal and then release without unmarshaling */
292 static void test_normal_marshal_and_release(void)
295 IStream *pStream = NULL;
299 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
300 ok_ole_success(hr, CreateStreamOnHGlobal);
301 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
302 ok_ole_success(hr, CoMarshalInterface);
304 ok_more_than_one_lock();
306 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
307 hr = CoReleaseMarshalData(pStream);
308 ok_ole_success(hr, CoReleaseMarshalData);
309 IStream_Release(pStream);
314 /* tests success case of a same-thread marshal and unmarshal */
315 static void test_normal_marshal_and_unmarshal(void)
318 IStream *pStream = NULL;
319 IUnknown *pProxy = NULL;
323 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
324 ok_ole_success(hr, CreateStreamOnHGlobal);
325 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
326 ok_ole_success(hr, CoMarshalInterface);
328 ok_more_than_one_lock();
330 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
331 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
332 ok_ole_success(hr, CoUnmarshalInterface);
333 IStream_Release(pStream);
335 ok_more_than_one_lock();
337 IUnknown_Release(pProxy);
342 /* tests failure case of unmarshaling a freed object */
343 static void test_marshal_and_unmarshal_invalid(void)
346 IStream *pStream = NULL;
347 IClassFactory *pProxy = NULL;
354 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
355 ok_ole_success(hr, CreateStreamOnHGlobal);
356 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
358 ok_more_than_one_lock();
360 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
361 hr = CoReleaseMarshalData(pStream);
362 ok_ole_success(hr, CoReleaseMarshalData);
366 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
367 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
368 todo_wine { ok_ole_success(hr, CoUnmarshalInterface); }
374 hr = IClassFactory_CreateInstance(pProxy, NULL, &IID_IUnknown, &dummy);
375 ok(hr == RPC_E_DISCONNECTED, "Remote call should have returned RPC_E_DISCONNECTED, instead of 0x%08lx\n", hr);
377 IClassFactory_Release(pProxy);
380 IStream_Release(pStream);
382 end_host_object(tid, thread);
385 /* tests success case of an interthread marshal */
386 static void test_interthread_marshal_and_unmarshal(void)
389 IStream *pStream = NULL;
390 IUnknown *pProxy = NULL;
396 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
397 ok_ole_success(hr, CreateStreamOnHGlobal);
398 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
400 ok_more_than_one_lock();
402 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
403 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
404 ok_ole_success(hr, CoUnmarshalInterface);
405 IStream_Release(pStream);
407 ok_more_than_one_lock();
409 IUnknown_Release(pProxy);
413 end_host_object(tid, thread);
416 /* tests success case of an interthread marshal and then marshaling the proxy */
417 static void test_proxy_marshal_and_unmarshal(void)
420 IStream *pStream = NULL;
421 IUnknown *pProxy = NULL;
422 IUnknown *pProxy2 = NULL;
428 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
429 ok_ole_success(hr, CreateStreamOnHGlobal);
430 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
432 ok_more_than_one_lock();
434 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
435 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
436 ok_ole_success(hr, CoUnmarshalInterface);
438 ok_more_than_one_lock();
440 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
441 /* marshal the proxy */
442 hr = CoMarshalInterface(pStream, &IID_IClassFactory, pProxy, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
443 ok_ole_success(hr, CoMarshalInterface);
445 ok_more_than_one_lock();
447 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
448 /* unmarshal the second proxy to the object */
449 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
450 ok_ole_success(hr, CoUnmarshalInterface);
451 IStream_Release(pStream);
453 /* now the proxies should be as follows:
454 * pProxy -> &Test_ClassFactory
455 * pProxy2 -> &Test_ClassFactory
456 * they should NOT be as follows:
457 * pProxy -> &Test_ClassFactory
459 * the above can only really be tested by looking in +ole traces
462 ok_more_than_one_lock();
464 IUnknown_Release(pProxy);
466 ok_more_than_one_lock();
468 IUnknown_Release(pProxy2);
472 end_host_object(tid, thread);
475 /* tests success case of an interthread marshal and then marshaling the proxy
476 * using an iid that hasn't previously been unmarshaled */
477 static void test_proxy_marshal_and_unmarshal2(void)
480 IStream *pStream = NULL;
481 IUnknown *pProxy = NULL;
482 IUnknown *pProxy2 = NULL;
488 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
489 ok_ole_success(hr, CreateStreamOnHGlobal);
490 tid = start_host_object(pStream, &IID_IUnknown, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
492 ok_more_than_one_lock();
494 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
495 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)&pProxy);
496 ok_ole_success(hr, CoUnmarshalInterface);
498 ok_more_than_one_lock();
500 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
501 /* marshal the proxy */
502 hr = CoMarshalInterface(pStream, &IID_IClassFactory, pProxy, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
503 ok_ole_success(hr, CoMarshalInterface);
505 ok_more_than_one_lock();
507 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
508 /* unmarshal the second proxy to the object */
509 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
510 ok_ole_success(hr, CoUnmarshalInterface);
511 IStream_Release(pStream);
513 /* now the proxies should be as follows:
514 * pProxy -> &Test_ClassFactory
515 * pProxy2 -> &Test_ClassFactory
516 * they should NOT be as follows:
517 * pProxy -> &Test_ClassFactory
519 * the above can only really be tested by looking in +ole traces
522 ok_more_than_one_lock();
524 IUnknown_Release(pProxy);
526 ok_more_than_one_lock();
528 IUnknown_Release(pProxy2);
532 end_host_object(tid, thread);
535 /* tests that stubs are released when the containing apartment is destroyed */
536 static void test_marshal_stub_apartment_shutdown(void)
539 IStream *pStream = NULL;
540 IUnknown *pProxy = NULL;
546 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
547 ok_ole_success(hr, CreateStreamOnHGlobal);
548 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
550 ok_more_than_one_lock();
552 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
553 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
554 ok_ole_success(hr, CoUnmarshalInterface);
555 IStream_Release(pStream);
557 ok_more_than_one_lock();
559 end_host_object(tid, thread);
563 IUnknown_Release(pProxy);
568 /* tests that proxies are released when the containing apartment is destroyed */
569 static void test_marshal_proxy_apartment_shutdown(void)
572 IStream *pStream = NULL;
573 IUnknown *pProxy = NULL;
579 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
580 ok_ole_success(hr, CreateStreamOnHGlobal);
581 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
583 ok_more_than_one_lock();
585 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
586 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
587 ok_ole_success(hr, CoUnmarshalInterface);
588 IStream_Release(pStream);
590 ok_more_than_one_lock();
596 IUnknown_Release(pProxy);
600 end_host_object(tid, thread);
602 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
605 /* tests that proxies are released when the containing mta apartment is destroyed */
606 static void test_marshal_proxy_mta_apartment_shutdown(void)
609 IStream *pStream = NULL;
610 IUnknown *pProxy = NULL;
615 pCoInitializeEx(NULL, COINIT_MULTITHREADED);
619 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
620 ok_ole_success(hr, CreateStreamOnHGlobal);
621 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
623 ok_more_than_one_lock();
625 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
626 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
627 ok_ole_success(hr, CoUnmarshalInterface);
628 IStream_Release(pStream);
630 ok_more_than_one_lock();
636 IUnknown_Release(pProxy);
640 end_host_object(tid, thread);
642 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
648 HANDLE marshal_event;
649 HANDLE unmarshal_event;
652 /* helper for test_no_couninitialize_server */
653 static DWORD CALLBACK no_couninitialize_server_proc(LPVOID p)
655 struct ncu_params *ncu_params = (struct ncu_params *)p;
658 pCoInitializeEx(NULL, COINIT_MULTITHREADED);
660 hr = CoMarshalInterface(ncu_params->stream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
661 ok_ole_success(hr, CoMarshalInterface);
663 SetEvent(ncu_params->marshal_event);
665 WaitForSingleObject(ncu_params->unmarshal_event, INFINITE);
667 /* die without calling CoUninitialize */
672 /* tests apartment that an apartment with a stub is released without deadlock
673 * if the owning thread exits */
674 static void test_no_couninitialize_server(void)
677 IStream *pStream = NULL;
678 IUnknown *pProxy = NULL;
681 struct ncu_params ncu_params;
685 ncu_params.marshal_event = CreateEvent(NULL, TRUE, FALSE, NULL);
686 ncu_params.unmarshal_event = CreateEvent(NULL, TRUE, FALSE, NULL);
688 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
689 ok_ole_success(hr, CreateStreamOnHGlobal);
690 ncu_params.stream = pStream;
692 thread = CreateThread(NULL, 0, no_couninitialize_server_proc, &ncu_params, 0, &tid);
694 WaitForSingleObject(ncu_params.marshal_event, INFINITE);
695 ok_more_than_one_lock();
697 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
698 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
699 ok_ole_success(hr, CoUnmarshalInterface);
700 IStream_Release(pStream);
702 ok_more_than_one_lock();
704 SetEvent(ncu_params.unmarshal_event);
705 WaitForSingleObject(thread, INFINITE);
710 CloseHandle(ncu_params.marshal_event);
711 CloseHandle(ncu_params.unmarshal_event);
713 IUnknown_Release(pProxy);
718 /* STA -> STA call during DLL_THREAD_DETACH */
719 static DWORD CALLBACK no_couninitialize_client_proc(LPVOID p)
721 struct ncu_params *ncu_params = (struct ncu_params *)p;
723 IUnknown *pProxy = NULL;
725 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
727 hr = CoUnmarshalInterface(ncu_params->stream, &IID_IClassFactory, (void **)&pProxy);
728 ok_ole_success(hr, CoUnmarshalInterface);
730 ok_more_than_one_lock();
732 /* die without calling CoUninitialize */
737 /* tests STA -> STA call during DLL_THREAD_DETACH doesn't deadlock */
738 static void test_no_couninitialize_client(void)
741 IStream *pStream = NULL;
746 struct ncu_params ncu_params;
750 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
751 ok_ole_success(hr, CreateStreamOnHGlobal);
752 ncu_params.stream = pStream;
754 /* NOTE: assumes start_host_object uses an STA to host the object, as MTAs
755 * always deadlock when called from within DllMain */
756 host_tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown *)&Test_ClassFactory, MSHLFLAGS_NORMAL, &host_thread);
757 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
759 ok_more_than_one_lock();
761 thread = CreateThread(NULL, 0, no_couninitialize_client_proc, &ncu_params, 0, &tid);
763 WaitForSingleObject(thread, INFINITE);
768 end_host_object(host_tid, host_thread);
771 /* tests success case of a same-thread table-weak marshal, unmarshal, unmarshal */
772 static void test_tableweak_marshal_and_unmarshal_twice(void)
775 IStream *pStream = NULL;
776 IUnknown *pProxy1 = NULL;
777 IUnknown *pProxy2 = NULL;
783 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
784 ok_ole_success(hr, CreateStreamOnHGlobal);
785 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_TABLEWEAK, &thread);
787 ok_more_than_one_lock();
789 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
790 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy1);
791 ok_ole_success(hr, CoUnmarshalInterface);
793 ok_more_than_one_lock();
795 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
796 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
797 ok_ole_success(hr, CoUnmarshalInterface);
799 ok_more_than_one_lock();
801 IUnknown_Release(pProxy1);
802 IUnknown_Release(pProxy2);
804 /* this line is shows the difference between weak and strong table marshaling:
805 * weak has cLocks == 0
806 * strong has cLocks > 0 */
809 end_host_object(tid, thread);
812 /* tests releasing after unmarshaling one object */
813 static void test_tableweak_marshal_releasedata1(void)
816 IStream *pStream = NULL;
817 IUnknown *pProxy1 = NULL;
818 IUnknown *pProxy2 = NULL;
824 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
825 ok_ole_success(hr, CreateStreamOnHGlobal);
826 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_TABLEWEAK, &thread);
828 ok_more_than_one_lock();
830 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
831 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy1);
832 ok_ole_success(hr, CoUnmarshalInterface);
834 ok_more_than_one_lock();
836 /* release the remaining reference on the object by calling
837 * CoReleaseMarshalData in the hosting thread */
838 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
839 release_host_object(tid);
841 ok_more_than_one_lock();
843 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
844 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
845 ok_ole_success(hr, CoUnmarshalInterface);
846 IStream_Release(pStream);
848 ok_more_than_one_lock();
850 IUnknown_Release(pProxy1);
852 IUnknown_Release(pProxy2);
854 /* this line is shows the difference between weak and strong table marshaling:
855 * weak has cLocks == 0
856 * strong has cLocks > 0 */
859 end_host_object(tid, thread);
862 /* tests releasing after unmarshaling one object */
863 static void test_tableweak_marshal_releasedata2(void)
866 IStream *pStream = NULL;
867 IUnknown *pProxy = NULL;
873 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
874 ok_ole_success(hr, CreateStreamOnHGlobal);
875 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_TABLEWEAK, &thread);
877 ok_more_than_one_lock();
879 /* release the remaining reference on the object by calling
880 * CoReleaseMarshalData in the hosting thread */
881 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
882 release_host_object(tid);
886 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
887 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
890 ok(hr == CO_E_OBJNOTREG,
891 "CoUnmarshalInterface should have failed with CO_E_OBJNOTREG, but returned 0x%08lx instead\n",
894 IStream_Release(pStream);
898 end_host_object(tid, thread);
901 /* tests success case of a same-thread table-strong marshal, unmarshal, unmarshal */
902 static void test_tablestrong_marshal_and_unmarshal_twice(void)
905 IStream *pStream = NULL;
906 IUnknown *pProxy1 = NULL;
907 IUnknown *pProxy2 = NULL;
913 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
914 ok_ole_success(hr, CreateStreamOnHGlobal);
915 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_TABLESTRONG, &thread);
917 ok_more_than_one_lock();
919 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
920 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy1);
921 ok_ole_success(hr, CoUnmarshalInterface);
923 ok_more_than_one_lock();
925 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
926 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
927 ok_ole_success(hr, CoUnmarshalInterface);
929 ok_more_than_one_lock();
931 if (pProxy1) IUnknown_Release(pProxy1);
932 if (pProxy2) IUnknown_Release(pProxy2);
934 /* this line is shows the difference between weak and strong table marshaling:
935 * weak has cLocks == 0
936 * strong has cLocks > 0 */
937 ok_more_than_one_lock();
939 /* release the remaining reference on the object by calling
940 * CoReleaseMarshalData in the hosting thread */
941 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
942 release_host_object(tid);
943 IStream_Release(pStream);
947 end_host_object(tid, thread);
950 /* tests CoLockObjectExternal */
951 static void test_lock_object_external(void)
954 IStream *pStream = NULL;
958 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
959 ok_ole_success(hr, CreateStreamOnHGlobal);
960 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
961 ok_ole_success(hr, CoMarshalInterface);
963 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, TRUE, TRUE);
965 ok_more_than_one_lock();
967 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
968 hr = CoReleaseMarshalData(pStream);
969 ok_ole_success(hr, CoReleaseMarshalData);
970 IStream_Release(pStream);
972 ok_more_than_one_lock();
974 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, FALSE, TRUE);
979 /* tests disconnecting stubs */
980 static void test_disconnect_stub(void)
983 IStream *pStream = NULL;
987 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
988 ok_ole_success(hr, CreateStreamOnHGlobal);
989 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
990 ok_ole_success(hr, CoMarshalInterface);
992 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, TRUE, TRUE);
994 ok_more_than_one_lock();
996 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
997 hr = CoReleaseMarshalData(pStream);
998 ok_ole_success(hr, CoReleaseMarshalData);
999 IStream_Release(pStream);
1001 ok_more_than_one_lock();
1003 CoDisconnectObject((IUnknown*)&Test_ClassFactory, 0);
1008 /* tests failure case of a same-thread marshal and unmarshal twice */
1009 static void test_normal_marshal_and_unmarshal_twice(void)
1012 IStream *pStream = NULL;
1013 IUnknown *pProxy1 = NULL;
1014 IUnknown *pProxy2 = NULL;
1018 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1019 ok_ole_success(hr, CreateStreamOnHGlobal);
1020 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1021 ok_ole_success(hr, CoMarshalInterface);
1023 ok_more_than_one_lock();
1025 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1026 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy1);
1027 ok_ole_success(hr, CoUnmarshalInterface);
1029 ok_more_than_one_lock();
1031 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1032 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
1033 ok(hr == CO_E_OBJNOTCONNECTED,
1034 "CoUnmarshalInterface should have failed with error CO_E_OBJNOTCONNECTED for double unmarshal, instead of 0x%08lx\n", hr);
1036 IStream_Release(pStream);
1038 ok_more_than_one_lock();
1040 IUnknown_Release(pProxy1);
1045 /* tests success case of marshaling and unmarshaling an HRESULT */
1046 static void test_hresult_marshaling(void)
1049 HRESULT hr_marshaled = 0;
1050 IStream *pStream = NULL;
1051 static const HRESULT E_DEADBEEF = 0xdeadbeef;
1053 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1054 ok_ole_success(hr, CreateStreamOnHGlobal);
1056 hr = CoMarshalHresult(pStream, E_DEADBEEF);
1057 ok_ole_success(hr, CoMarshalHresult);
1059 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1060 hr = IStream_Read(pStream, &hr_marshaled, sizeof(HRESULT), NULL);
1061 ok_ole_success(hr, IStream_Read);
1063 ok(hr_marshaled == E_DEADBEEF, "Didn't marshal HRESULT as expected: got value 0x%08lx instead\n", hr_marshaled);
1066 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1067 hr = CoUnmarshalHresult(pStream, &hr_marshaled);
1068 ok_ole_success(hr, CoUnmarshalHresult);
1070 ok(hr_marshaled == E_DEADBEEF, "Didn't marshal HRESULT as expected: got value 0x%08lx instead\n", hr_marshaled);
1072 IStream_Release(pStream);
1076 /* helper for test_proxy_used_in_wrong_thread */
1077 static DWORD CALLBACK bad_thread_proc(LPVOID p)
1079 IClassFactory * cf = (IClassFactory *)p;
1081 IUnknown * proxy = NULL;
1083 pCoInitializeEx(NULL, COINIT_MULTITHREADED);
1085 hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
1086 if (proxy) IUnknown_Release(proxy);
1087 ok(hr == RPC_E_WRONG_THREAD,
1088 "COM should have failed with RPC_E_WRONG_THREAD on using proxy from wrong apartment, but instead returned 0x%08lx\n",
1096 /* tests failure case of a using a proxy in the wrong apartment */
1097 static void test_proxy_used_in_wrong_thread(void)
1100 IStream *pStream = NULL;
1101 IUnknown *pProxy = NULL;
1108 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1109 ok_ole_success(hr, CreateStreamOnHGlobal);
1110 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &host_thread);
1112 ok_more_than_one_lock();
1114 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1115 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
1116 ok_ole_success(hr, CoUnmarshalInterface);
1117 IStream_Release(pStream);
1119 ok_more_than_one_lock();
1121 /* create a thread that we can misbehave in */
1122 thread = CreateThread(NULL, 0, bad_thread_proc, (LPVOID)pProxy, 0, &tid2);
1124 WaitForSingleObject(thread, INFINITE);
1125 CloseHandle(thread);
1127 IUnknown_Release(pProxy);
1131 end_host_object(tid, host_thread);
1134 static HRESULT WINAPI MessageFilter_QueryInterface(IMessageFilter *iface, REFIID riid, void ** ppvObj)
1136 if (ppvObj == NULL) return E_POINTER;
1138 if (IsEqualGUID(riid, &IID_IUnknown) ||
1139 IsEqualGUID(riid, &IID_IClassFactory))
1141 *ppvObj = (LPVOID)iface;
1142 IClassFactory_AddRef(iface);
1146 return E_NOINTERFACE;
1149 static ULONG WINAPI MessageFilter_AddRef(IMessageFilter *iface)
1151 return 2; /* non-heap object */
1154 static ULONG WINAPI MessageFilter_Release(IMessageFilter *iface)
1156 return 1; /* non-heap object */
1159 static DWORD WINAPI MessageFilter_HandleInComingCall(
1160 IMessageFilter *iface,
1162 HTASK threadIDCaller,
1164 LPINTERFACEINFO lpInterfaceInfo)
1166 static int callcount = 0;
1168 trace("HandleInComingCall\n");
1172 ret = SERVERCALL_REJECTED;
1175 ret = SERVERCALL_RETRYLATER;
1178 ret = SERVERCALL_ISHANDLED;
1185 static DWORD WINAPI MessageFilter_RetryRejectedCall(
1186 IMessageFilter *iface,
1187 HTASK threadIDCallee,
1191 trace("RetryRejectedCall\n");
1195 static DWORD WINAPI MessageFilter_MessagePending(
1196 IMessageFilter *iface,
1197 HTASK threadIDCallee,
1199 DWORD dwPendingType)
1201 trace("MessagePending\n");
1202 return PENDINGMSG_WAITNOPROCESS;
1205 static const IMessageFilterVtbl MessageFilter_Vtbl =
1207 MessageFilter_QueryInterface,
1208 MessageFilter_AddRef,
1209 MessageFilter_Release,
1210 MessageFilter_HandleInComingCall,
1211 MessageFilter_RetryRejectedCall,
1212 MessageFilter_MessagePending
1215 static IMessageFilter MessageFilter = { &MessageFilter_Vtbl };
1217 static void test_message_filter(void)
1220 IStream *pStream = NULL;
1221 IClassFactory *cf = NULL;
1223 IUnknown *proxy = NULL;
1224 IMessageFilter *prev_filter = NULL;
1229 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1230 ok_ole_success(hr, CreateStreamOnHGlobal);
1231 tid = start_host_object2(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &MessageFilter, &thread);
1233 ok_more_than_one_lock();
1235 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1236 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&cf);
1237 ok_ole_success(hr, CoUnmarshalInterface);
1238 IStream_Release(pStream);
1240 ok_more_than_one_lock();
1242 hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
1243 todo_wine { ok(hr == RPC_E_CALL_REJECTED, "Call should have returned RPC_E_CALL_REJECTED, but return 0x%08lx instead\n", hr); }
1244 if (proxy) IUnknown_Release(proxy);
1247 hr = CoRegisterMessageFilter(&MessageFilter, &prev_filter);
1248 ok_ole_success(hr, CoRegisterMessageFilter);
1249 if (prev_filter) IMessageFilter_Release(prev_filter);
1251 hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
1252 ok_ole_success(hr, IClassFactory_CreateInstance);
1254 IUnknown_Release(proxy);
1256 IClassFactory_Release(cf);
1260 end_host_object(tid, thread);
1263 /* test failure case of trying to unmarshal from bad stream */
1264 static void test_bad_marshal_stream(void)
1267 IStream *pStream = NULL;
1269 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1270 ok_ole_success(hr, CreateStreamOnHGlobal);
1271 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1272 ok_ole_success(hr, CoMarshalInterface);
1274 ok_more_than_one_lock();
1276 /* try to read beyond end of stream */
1277 hr = CoReleaseMarshalData(pStream);
1278 ok(hr == STG_E_READFAULT, "Should have failed with STG_E_READFAULT, but returned 0x%08lx instead\n", hr);
1280 /* now release for real */
1281 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1282 hr = CoReleaseMarshalData(pStream);
1283 ok_ole_success(hr, CoReleaseMarshalData);
1285 IStream_Release(pStream);
1288 /* tests that proxies implement certain interfaces */
1289 static void test_proxy_interfaces(void)
1292 IStream *pStream = NULL;
1293 IUnknown *pProxy = NULL;
1294 IUnknown *pOtherUnknown = NULL;
1300 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1301 ok_ole_success(hr, CreateStreamOnHGlobal);
1302 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
1304 ok_more_than_one_lock();
1306 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1307 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)&pProxy);
1308 ok_ole_success(hr, CoUnmarshalInterface);
1309 IStream_Release(pStream);
1311 ok_more_than_one_lock();
1313 hr = IUnknown_QueryInterface(pProxy, &IID_IUnknown, (LPVOID*)&pOtherUnknown);
1314 ok_ole_success(hr, IUnknown_QueryInterface IID_IUnknown);
1315 if (hr == S_OK) IUnknown_Release(pOtherUnknown);
1317 hr = IUnknown_QueryInterface(pProxy, &IID_IClientSecurity, (LPVOID*)&pOtherUnknown);
1318 todo_wine { ok_ole_success(hr, IUnknown_QueryInterface IID_IClientSecurity); }
1319 if (hr == S_OK) IUnknown_Release(pOtherUnknown);
1321 hr = IUnknown_QueryInterface(pProxy, &IID_IMultiQI, (LPVOID*)&pOtherUnknown);
1322 ok_ole_success(hr, IUnknown_QueryInterface IID_IMultiQI);
1323 if (hr == S_OK) IUnknown_Release(pOtherUnknown);
1325 hr = IUnknown_QueryInterface(pProxy, &IID_IMarshal, (LPVOID*)&pOtherUnknown);
1326 ok_ole_success(hr, IUnknown_QueryInterface IID_IMarshal);
1327 if (hr == S_OK) IUnknown_Release(pOtherUnknown);
1329 /* IMarshal2 is also supported on NT-based systems, but is pretty much
1330 * useless as it has no more methods over IMarshal that it inherits from. */
1332 IUnknown_Release(pProxy);
1336 end_host_object(tid, thread);
1341 const IUnknownVtbl *lpVtbl;
1345 static HRESULT WINAPI HeapUnknown_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
1347 if (IsEqualIID(riid, &IID_IUnknown))
1349 IUnknown_AddRef(iface);
1350 *ppv = (LPVOID)iface;
1354 return E_NOINTERFACE;
1357 static ULONG WINAPI HeapUnknown_AddRef(IUnknown *iface)
1359 HeapUnknown *This = (HeapUnknown *)iface;
1360 trace("HeapUnknown_AddRef(%p)\n", iface);
1361 return InterlockedIncrement((LONG*)&This->refs);
1364 static ULONG WINAPI HeapUnknown_Release(IUnknown *iface)
1366 HeapUnknown *This = (HeapUnknown *)iface;
1367 ULONG refs = InterlockedDecrement((LONG*)&This->refs);
1368 trace("HeapUnknown_Release(%p)\n", iface);
1369 if (!refs) HeapFree(GetProcessHeap(), 0, This);
1373 static const IUnknownVtbl HeapUnknown_Vtbl =
1375 HeapUnknown_QueryInterface,
1380 static void test_proxybuffer(REFIID riid)
1383 IPSFactoryBuffer *psfb;
1384 IRpcProxyBuffer *proxy;
1388 HeapUnknown *pUnkOuter = (HeapUnknown *)HeapAlloc(GetProcessHeap(), 0, sizeof(*pUnkOuter));
1390 pUnkOuter->lpVtbl = &HeapUnknown_Vtbl;
1391 pUnkOuter->refs = 1;
1393 hr = CoGetPSClsid(riid, &clsid);
1394 ok_ole_success(hr, CoGetPSClsid);
1396 hr = CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL, &IID_IPSFactoryBuffer, (LPVOID*)&psfb);
1397 ok_ole_success(hr, CoGetClassObject);
1399 hr = IPSFactoryBuffer_CreateProxy(psfb, (IUnknown*)pUnkOuter, riid, &proxy, &lpvtbl);
1400 ok_ole_success(hr, IPSFactoryBuffer_CreateProxy);
1401 ok(lpvtbl != NULL, "IPSFactoryBuffer_CreateProxy succeeded, but returned a NULL vtable!\n");
1403 /* release our reference to the outer unknown object - the PS factory
1404 * buffer will have AddRef's it in the CreateProxy call */
1405 refs = IUnknown_Release((IUnknown *)pUnkOuter);
1406 ok(refs == 1, "Ref count of outer unknown should have been 1 instead of %ld\n", refs);
1408 refs = IPSFactoryBuffer_Release(psfb);
1409 #if 0 /* not reliable on native. maybe it leaks references! */
1410 ok(refs == 0, "Ref-count leak of %ld on IPSFactoryBuffer\n", refs);
1413 refs = IUnknown_Release((IUnknown *)lpvtbl);
1414 ok(refs == 0, "Ref-count leak of %ld on IRpcProxyBuffer\n", refs);
1416 refs = IRpcProxyBuffer_Release(proxy);
1417 ok(refs == 0, "Ref-count leak of %ld on IRpcProxyBuffer\n", refs);
1420 static void test_stubbuffer(REFIID riid)
1423 IPSFactoryBuffer *psfb;
1424 IRpcStubBuffer *stub;
1430 hr = CoGetPSClsid(riid, &clsid);
1431 ok_ole_success(hr, CoGetPSClsid);
1433 hr = CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL, &IID_IPSFactoryBuffer, (LPVOID*)&psfb);
1434 ok_ole_success(hr, CoGetClassObject);
1436 hr = IPSFactoryBuffer_CreateStub(psfb, riid, (IUnknown*)&Test_ClassFactory, &stub);
1437 ok_ole_success(hr, IPSFactoryBuffer_CreateStub);
1439 refs = IPSFactoryBuffer_Release(psfb);
1440 #if 0 /* not reliable on native. maybe it leaks references */
1441 ok(refs == 0, "Ref-count leak of %ld on IPSFactoryBuffer\n", refs);
1444 ok_more_than_one_lock();
1446 IRpcStubBuffer_Disconnect(stub);
1450 refs = IRpcStubBuffer_Release(stub);
1451 ok(refs == 0, "Ref-count leak of %ld on IRpcProxyBuffer\n", refs);
1454 static HWND hwnd_app;
1456 static HRESULT WINAPI TestRE_IClassFactory_CreateInstance(
1457 LPCLASSFACTORY iface,
1458 LPUNKNOWN pUnkOuter,
1463 if (IsEqualIID(riid, &IID_IWineTest))
1465 BOOL ret = SendMessageTimeout(hwnd_app, WM_NULL, 0, 0, SMTO_BLOCK, 5000, &res);
1466 ok(ret, "Timed out sending a message to originating window during RPC call\n");
1471 static const IClassFactoryVtbl TestREClassFactory_Vtbl =
1473 Test_IClassFactory_QueryInterface,
1474 Test_IClassFactory_AddRef,
1475 Test_IClassFactory_Release,
1476 TestRE_IClassFactory_CreateInstance,
1477 Test_IClassFactory_LockServer
1480 IClassFactory TestRE_ClassFactory = { &TestREClassFactory_Vtbl };
1482 static LRESULT CALLBACK window_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1489 IStream *pStream = NULL;
1490 IClassFactory *proxy = NULL;
1497 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1498 ok_ole_success(hr, CreateStreamOnHGlobal);
1499 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&TestRE_ClassFactory, MSHLFLAGS_NORMAL, &thread);
1501 ok_more_than_one_lock();
1503 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1504 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&proxy);
1505 ok_ole_success(hr, CoReleaseMarshalData);
1506 IStream_Release(pStream);
1508 ok_more_than_one_lock();
1510 /* note the use of the magic IID_IWineTest value to tell remote thread
1511 * to try to send a message back to us */
1512 hr = IClassFactory_CreateInstance(proxy, NULL, &IID_IWineTest, (void **)&object);
1514 IClassFactory_Release(proxy);
1518 end_host_object(tid, thread);
1520 PostMessage(hwnd, WM_QUIT, 0, 0);
1527 IStream *pStream = NULL;
1528 IClassFactory *proxy = NULL;
1535 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1536 ok_ole_success(hr, CreateStreamOnHGlobal);
1537 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&TestRE_ClassFactory, MSHLFLAGS_NORMAL, &thread);
1539 ok_more_than_one_lock();
1541 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1542 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&proxy);
1543 ok_ole_success(hr, CoReleaseMarshalData);
1544 IStream_Release(pStream);
1546 ok_more_than_one_lock();
1548 /* post quit message before a doing a COM call to show that a pending
1549 * WM_QUIT message doesn't stop the call from succeeding */
1550 PostMessage(hwnd, WM_QUIT, 0, 0);
1551 hr = IClassFactory_CreateInstance(proxy, NULL, &IID_IUnknown, (void **)&object);
1553 IClassFactory_Release(proxy);
1557 end_host_object(tid, thread);
1562 return DefWindowProc(hwnd, msg, wparam, lparam);
1566 static void test_message_reentrancy(void)
1571 memset(&wndclass, 0, sizeof(wndclass));
1572 wndclass.lpfnWndProc = window_proc;
1573 wndclass.lpszClassName = "WineCOMTest";
1574 RegisterClass(&wndclass);
1576 hwnd_app = CreateWindow("WineCOMTest", NULL, 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, NULL, 0);
1577 ok(hwnd_app != NULL, "Window creation failed\n");
1579 /* start message re-entrancy test */
1580 PostMessage(hwnd_app, WM_USER, 0, 0);
1582 while (GetMessage(&msg, NULL, 0, 0))
1584 TranslateMessage(&msg);
1585 DispatchMessage(&msg);
1589 static void test_WM_QUIT_handling(void)
1593 hwnd_app = CreateWindow("WineCOMTest", NULL, 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, NULL, 0);
1594 ok(hwnd_app != NULL, "Window creation failed\n");
1596 /* start WM_QUIT handling test */
1597 PostMessage(hwnd_app, WM_USER+1, 0, 0);
1599 while (GetMessage(&msg, NULL, 0, 0))
1601 TranslateMessage(&msg);
1602 DispatchMessage(&msg);
1606 static void test_freethreadedmarshaldata(IStream *pStream, MSHCTX mshctx, void *ptr, DWORD mshlflags)
1613 hr = GetHGlobalFromStream(pStream, &hglobal);
1614 ok_ole_success(hr, GetHGlobalFromStream);
1616 size = GlobalSize(hglobal);
1618 marshal_data = (char *)GlobalLock(hglobal);
1620 if (mshctx == MSHCTX_INPROC)
1622 DWORD expected_size = sizeof(DWORD) + sizeof(void *) + sizeof(DWORD) + sizeof(GUID);
1623 ok(size == expected_size, "size should have been %ld instead of %ld\n", expected_size, size);
1625 ok(*(DWORD *)marshal_data == mshlflags, "expected 0x%lx, but got 0x%lx for mshctx\n", mshlflags, *(DWORD *)marshal_data);
1626 marshal_data += sizeof(DWORD);
1627 ok(*(void **)marshal_data == ptr, "expected %p, but got %p for mshctx\n", ptr, *(void **)marshal_data);
1628 marshal_data += sizeof(void *);
1629 ok(*(DWORD *)marshal_data == 0, "expected 0x0, but got 0x%lx\n", *(DWORD *)marshal_data);
1630 marshal_data += sizeof(DWORD);
1631 trace("got guid data: {%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\n",
1632 ((GUID *)marshal_data)->Data1, ((GUID *)marshal_data)->Data2, ((GUID *)marshal_data)->Data3,
1633 ((GUID *)marshal_data)->Data4[0], ((GUID *)marshal_data)->Data4[1], ((GUID *)marshal_data)->Data4[2], ((GUID *)marshal_data)->Data4[3],
1634 ((GUID *)marshal_data)->Data4[4], ((GUID *)marshal_data)->Data4[5], ((GUID *)marshal_data)->Data4[6], ((GUID *)marshal_data)->Data4[7]);
1638 ok(size > sizeof(DWORD), "size should have been > sizeof(DWORD), not %ld\n", size);
1639 ok(*(DWORD *)marshal_data == 0x574f454d /* MEOW */,
1640 "marshal data should be filled by standard marshal and start with MEOW signature\n");
1643 GlobalUnlock(hglobal);
1646 static void test_freethreadedmarshaler(void)
1649 IUnknown *pFTUnknown;
1650 IMarshal *pFTMarshal;
1653 static const LARGE_INTEGER llZero;
1656 hr = CoCreateFreeThreadedMarshaler(NULL, &pFTUnknown);
1657 ok_ole_success(hr, CoCreateFreeThreadedMarshaler);
1658 hr = IUnknown_QueryInterface(pFTUnknown, &IID_IMarshal, (void **)&pFTMarshal);
1659 ok_ole_success(hr, IUnknown_QueryInterface);
1660 IUnknown_Release(pFTUnknown);
1662 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1663 ok_ole_success(hr, CreateStreamOnHGlobal);
1665 /* inproc normal marshaling */
1667 hr = IMarshal_MarshalInterface(pFTMarshal, pStream, &IID_IClassFactory,
1668 (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1669 ok_ole_success(hr, IMarshal_MarshalInterface);
1671 ok_more_than_one_lock();
1673 test_freethreadedmarshaldata(pStream, MSHCTX_INPROC, &Test_ClassFactory, MSHLFLAGS_NORMAL);
1675 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
1676 hr = IMarshal_UnmarshalInterface(pFTMarshal, pStream, &IID_IUnknown, (void **)&pProxy);
1677 ok_ole_success(hr, IMarshal_UnmarshalInterface);
1679 IUnknown_Release(pProxy);
1683 /* native doesn't allow us to unmarshal or release the stream data,
1684 * presumably because it wants us to call CoMarshalInterface instead */
1686 /* local normal marshaling */
1688 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
1689 hr = IMarshal_MarshalInterface(pFTMarshal, pStream, IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_LOCAL, NULL, MSHLFLAGS_NORMAL);
1690 ok_ole_success(hr, IMarshal_MarshalInterface);
1692 ok_more_than_one_lock();
1694 test_freethreadedmarshaldata(pStream, MSHCTX_LOCAL, &Test_ClassFactory, MSHLFLAGS_NORMAL);
1696 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
1697 hr = IMarshal_ReleaseMarshalData(pFTMarshal, pStream);
1698 ok_ole_success(hr, IMarshal_ReleaseMarshalData);
1703 /* inproc table-strong marshaling */
1705 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
1706 hr = IMarshal_MarshalInterface(pFTMarshal, pStream, &IID_IClassFactory,
1707 (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, (void *)0xdeadbeef,
1708 MSHLFLAGS_TABLESTRONG);
1709 ok_ole_success(hr, IMarshal_MarshalInterface);
1711 ok_more_than_one_lock();
1713 test_freethreadedmarshaldata(pStream, MSHCTX_INPROC, &Test_ClassFactory, MSHLFLAGS_TABLESTRONG);
1715 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
1716 hr = IMarshal_UnmarshalInterface(pFTMarshal, pStream, &IID_IUnknown, (void **)&pProxy);
1717 ok_ole_success(hr, IMarshal_UnmarshalInterface);
1719 IUnknown_Release(pProxy);
1721 ok_more_than_one_lock();
1723 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
1724 hr = IMarshal_ReleaseMarshalData(pFTMarshal, pStream);
1725 ok_ole_success(hr, IMarshal_ReleaseMarshalData);
1729 /* inproc table-weak marshaling */
1731 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
1732 hr = IMarshal_MarshalInterface(pFTMarshal, pStream, &IID_IClassFactory,
1733 (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, (void *)0xdeadbeef,
1734 MSHLFLAGS_TABLEWEAK);
1735 ok_ole_success(hr, IMarshal_MarshalInterface);
1739 test_freethreadedmarshaldata(pStream, MSHCTX_INPROC, &Test_ClassFactory, MSHLFLAGS_TABLEWEAK);
1741 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
1742 hr = IMarshal_UnmarshalInterface(pFTMarshal, pStream, &IID_IUnknown, (void **)&pProxy);
1743 ok_ole_success(hr, IMarshal_UnmarshalInterface);
1745 ok_more_than_one_lock();
1747 IUnknown_Release(pProxy);
1751 /* inproc normal marshaling (for extraordinary cases) */
1753 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
1754 hr = IMarshal_MarshalInterface(pFTMarshal, pStream, &IID_IClassFactory,
1755 (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1756 ok_ole_success(hr, IMarshal_MarshalInterface);
1758 ok_more_than_one_lock();
1760 /* this call shows that DisconnectObject does nothing */
1761 hr = IMarshal_DisconnectObject(pFTMarshal, 0);
1762 ok_ole_success(hr, IMarshal_DisconnectObject);
1764 ok_more_than_one_lock();
1766 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
1767 hr = IMarshal_ReleaseMarshalData(pFTMarshal, pStream);
1768 ok_ole_success(hr, IMarshal_ReleaseMarshalData);
1772 /* doesn't enforce marshaling rules here and allows us to unmarshal the
1773 * interface, even though it was freed above */
1774 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
1775 hr = IMarshal_UnmarshalInterface(pFTMarshal, pStream, &IID_IUnknown, (void **)&pProxy);
1776 ok_ole_success(hr, IMarshal_UnmarshalInterface);
1780 IStream_Release(pStream);
1781 IMarshal_Release(pFTMarshal);
1784 static HANDLE heventShutdown;
1786 static void LockModuleOOP(void)
1788 InterlockedIncrement(&cLocks); /* for test purposes only */
1789 CoAddRefServerProcess();
1792 static void UnlockModuleOOP(void)
1794 InterlockedDecrement(&cLocks); /* for test purposes only */
1795 if (!CoReleaseServerProcess())
1796 SetEvent(heventShutdown);
1799 static HWND hwnd_app;
1801 static HRESULT WINAPI TestOOP_IClassFactory_QueryInterface(
1802 LPCLASSFACTORY iface,
1806 if (ppvObj == NULL) return E_POINTER;
1808 if (IsEqualGUID(riid, &IID_IUnknown) ||
1809 IsEqualGUID(riid, &IID_IClassFactory))
1811 *ppvObj = (LPVOID)iface;
1812 IClassFactory_AddRef(iface);
1816 return E_NOINTERFACE;
1819 static ULONG WINAPI TestOOP_IClassFactory_AddRef(LPCLASSFACTORY iface)
1821 return 2; /* non-heap-based object */
1824 static ULONG WINAPI TestOOP_IClassFactory_Release(LPCLASSFACTORY iface)
1826 return 1; /* non-heap-based object */
1829 static HRESULT WINAPI TestOOP_IClassFactory_CreateInstance(
1830 LPCLASSFACTORY iface,
1831 LPUNKNOWN pUnkOuter,
1835 return CLASS_E_CLASSNOTAVAILABLE;
1838 static HRESULT WINAPI TestOOP_IClassFactory_LockServer(
1839 LPCLASSFACTORY iface,
1849 static const IClassFactoryVtbl TestClassFactoryOOP_Vtbl =
1851 TestOOP_IClassFactory_QueryInterface,
1852 TestOOP_IClassFactory_AddRef,
1853 TestOOP_IClassFactory_Release,
1854 TestOOP_IClassFactory_CreateInstance,
1855 TestOOP_IClassFactory_LockServer
1858 static IClassFactory TestOOP_ClassFactory = { &TestClassFactoryOOP_Vtbl };
1860 /* tests functions commonly used by out of process COM servers */
1861 static void test_out_of_process_com(void)
1863 static const CLSID CLSID_WineOOPTest = {
1867 {0xa1, 0xa2, 0x5d, 0x5a, 0x36, 0x54, 0xd3, 0xbd}
1868 }; /* 5201163f-8164-4fd0-a1a2-5d5a3654d3bd */
1874 heventShutdown = CreateEvent(NULL, TRUE, FALSE, NULL);
1878 /* Start the object suspended */
1879 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&TestOOP_ClassFactory,
1880 CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE | REGCLS_SUSPENDED, &cookie);
1881 ok_ole_success(hr, CoRegisterClassObject);
1883 /* ... and CoGetClassObject does not find it and fails when it looks for the
1884 * class in the registry */
1885 hr = CoGetClassObject(&CLSID_WineOOPTest, CLSCTX_INPROC_SERVER,
1886 NULL, &IID_IClassFactory, (LPVOID*)&cf);
1888 ok(hr == REGDB_E_CLASSNOTREG,
1889 "CoGetClassObject should have returned REGDB_E_CLASSNOTREG instead of 0x%08lx\n", hr);
1892 /* Resume the object suspended above ... */
1893 hr = CoResumeClassObjects();
1894 ok_ole_success(hr, CoResumeClassObjects);
1896 /* ... and now it should succeed */
1897 hr = CoGetClassObject(&CLSID_WineOOPTest, CLSCTX_INPROC_SERVER,
1898 NULL, &IID_IClassFactory, (LPVOID*)&cf);
1899 ok_ole_success(hr, CoGetClassObject);
1901 /* Now check the locking is working */
1902 /* NOTE: we are accessing the class directly, not through a proxy */
1906 hr = IClassFactory_LockServer(cf, TRUE);
1907 trace("IClassFactory_LockServer returned 0x%08lx\n", hr);
1909 ok_more_than_one_lock();
1911 IClassFactory_LockServer(cf, FALSE);
1915 IClassFactory_Release(cf);
1917 /* wait for shutdown signal */
1918 ret = WaitForSingleObject(heventShutdown, 5000);
1919 todo_wine { ok(ret != WAIT_TIMEOUT, "Server didn't shut down or machine is under very heavy load\n"); }
1921 /* try to connect again after SCM has suspended registered class objects */
1922 hr = CoGetClassObject(&CLSID_WineOOPTest, CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER, NULL,
1923 &IID_IClassFactory, (LPVOID*)&cf);
1925 ok(hr == CO_E_SERVER_STOPPING,
1926 "CoGetClassObject should have returned CO_E_SERVER_STOPPING instead of 0x%08lx\n", hr);
1929 hr = CoRevokeClassObject(cookie);
1930 ok_ole_success(hr, CoRevokeClassObject);
1932 CloseHandle(heventShutdown);
1935 static void test_ROT(void)
1937 static const WCHAR wszFileName[] = {'B','E','2','0','E','2','F','5','-',
1938 '1','9','0','3','-','4','A','A','E','-','B','1','A','F','-',
1939 '2','0','4','6','E','5','8','6','C','9','2','5',0};
1941 IMoniker *pMoniker = NULL;
1942 IRunningObjectTable *pROT = NULL;
1947 hr = CreateFileMoniker(wszFileName, &pMoniker);
1948 ok_ole_success(hr, CreateClassMoniker);
1949 hr = GetRunningObjectTable(0, &pROT);
1950 ok_ole_success(hr, GetRunningObjectTable);
1951 hr = IRunningObjectTable_Register(pROT, 0, (IUnknown*)&Test_ClassFactory, pMoniker, &dwCookie);
1952 ok_ole_success(hr, IRunningObjectTable_Register);
1954 ok_more_than_one_lock();
1956 hr = IRunningObjectTable_Revoke(pROT, dwCookie);
1957 ok_ole_success(hr, IRunningObjectTable_Revoke);
1965 IGlobalInterfaceTable *git;
1968 static DWORD CALLBACK get_global_interface_proc(LPVOID pv)
1971 struct git_params *params = (struct git_params *)pv;
1974 hr = IGlobalInterfaceTable_GetInterfaceFromGlobal(params->git, params->cookie, &IID_IClassFactory, (void **)&cf);
1975 ok(hr == CO_E_NOTINITIALIZED,
1976 "IGlobalInterfaceTable_GetInterfaceFromGlobal should have failed with error CO_E_NOTINITIALIZED instead of 0x%08lx\n",
1980 hr = IGlobalInterfaceTable_GetInterfaceFromGlobal(params->git, params->cookie, &IID_IClassFactory, (void **)&cf);
1981 ok_ole_success(hr, IGlobalInterfaceTable_GetInterfaceFromGlobal);
1987 static void test_globalinterfacetable(void)
1990 IGlobalInterfaceTable *git;
1994 struct git_params params;
1997 hr = CoCreateInstance(&CLSID_StdGlobalInterfaceTable, NULL, CLSCTX_INPROC_SERVER, &IID_IGlobalInterfaceTable, (void **)&git);
1998 ok_ole_success(hr, CoCreateInstance);
2000 hr = IGlobalInterfaceTable_RegisterInterfaceInGlobal(git, (IUnknown *)&Test_ClassFactory, &IID_IClassFactory, &cookie);
2001 ok_ole_success(hr, IGlobalInterfaceTable_RegisterInterfaceInGlobal);
2003 params.cookie = cookie;
2005 /* note: params is on stack so we MUST wait for get_global_interface_proc
2006 * to exit before we can return */
2007 thread = CreateThread(NULL, 0, get_global_interface_proc, ¶ms, 0, &tid);
2009 ret = MsgWaitForMultipleObjects(1, &thread, FALSE, INFINITE, QS_ALLINPUT);
2010 while (ret == WAIT_OBJECT_0 + 1)
2013 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
2014 DispatchMessage(&msg);
2015 ret = MsgWaitForMultipleObjects(1, &thread, FALSE, INFINITE, QS_ALLINPUT);
2018 CloseHandle(thread);
2021 static const char cf_marshaled[] =
2033 static void test_marshal_CLIPFORMAT(void)
2035 unsigned char *buffer;
2037 unsigned long flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
2038 wireCLIPFORMAT wirecf;
2039 CLIPFORMAT cf = RegisterClipboardFormatA("MyFormat");
2042 size = CLIPFORMAT_UserSize(&flags, 0, &cf);
2043 ok(size == sizeof(*wirecf) + sizeof(cf_marshaled), "Wrong size %ld\n", size);
2045 buffer = HeapAlloc(GetProcessHeap(), 0, size);
2046 CLIPFORMAT_UserMarshal(&flags, buffer, &cf);
2047 wirecf = (wireCLIPFORMAT)buffer;
2048 ok(wirecf->fContext == WDT_REMOTE_CALL, "Context should be WDT_REMOTE_CALL instead of 0x%08lx\n", wirecf->fContext);
2049 ok(wirecf->u.dwValue == cf, "Marshaled value should be 0x%04x instead of 0x%04lx\n", cf, wirecf->u.dwValue);
2050 ok(!memcmp(wirecf+1, cf_marshaled, sizeof(cf_marshaled)), "Marshaled data differs\n");
2052 CLIPFORMAT_UserUnmarshal(&flags, buffer, &cf2);
2053 ok(cf == cf2, "Didn't unmarshal properly\n");
2054 HeapFree(GetProcessHeap(), 0, buffer);
2056 CLIPFORMAT_UserFree(&flags, &cf2);
2059 static void test_marshal_HWND(void)
2061 unsigned char *buffer;
2063 unsigned long flags = MAKELONG(MSHCTX_LOCAL, NDR_LOCAL_DATA_REPRESENTATION);
2064 HWND hwnd = GetDesktopWindow();
2068 size = HWND_UserSize(&flags, 0, &hwnd);
2069 ok(size == sizeof(*wirehwnd), "Wrong size %ld\n", size);
2071 buffer = HeapAlloc(GetProcessHeap(), 0, size);
2072 HWND_UserMarshal(&flags, buffer, &hwnd);
2073 wirehwnd = (wireHWND)buffer;
2074 ok(wirehwnd->fContext == WDT_INPROC_CALL, "Context should be WDT_INPROC_CALL instead of 0x%08lx\n", wirehwnd->fContext);
2075 ok(wirehwnd->u.hInproc == (LONG_PTR)hwnd, "Marshaled value should be %p instead of %p\n", hwnd, (HANDLE)wirehwnd->u.hRemote);
2077 HWND_UserUnmarshal(&flags, buffer, &hwnd2);
2078 ok(hwnd == hwnd2, "Didn't unmarshal properly\n");
2079 HeapFree(GetProcessHeap(), 0, buffer);
2081 HWND_UserFree(&flags, &hwnd2);
2084 static void test_marshal_HGLOBAL(void)
2086 unsigned char *buffer;
2088 unsigned long flags = MAKELONG(MSHCTX_LOCAL, NDR_LOCAL_DATA_REPRESENTATION);
2091 unsigned char *wirehglobal;
2095 flags = MAKELONG(MSHCTX_LOCAL, NDR_LOCAL_DATA_REPRESENTATION);
2096 size = HGLOBAL_UserSize(&flags, 0, &hglobal);
2097 /* native is poorly programmed and allocates 4 bytes more than it needs to
2098 * here - Wine doesn't have to emulate that */
2099 ok((size == 8) || (size == 12), "Size should be 12, instead of %ld\n", size);
2100 buffer = HeapAlloc(GetProcessHeap(), 0, size);
2101 HGLOBAL_UserMarshal(&flags, buffer, &hglobal);
2102 wirehglobal = buffer;
2103 ok(*(ULONG *)wirehglobal == WDT_REMOTE_CALL, "Context should be WDT_REMOTE_CALL instead of 0x%08lx\n", *(ULONG *)wirehglobal);
2104 wirehglobal += sizeof(ULONG);
2105 ok(*(ULONG *)wirehglobal == (ULONG)hglobal, "buffer+4 should be HGLOBAL\n");
2106 HGLOBAL_UserUnmarshal(&flags, buffer, &hglobal2);
2107 ok(hglobal2 == hglobal, "Didn't unmarshal properly\n");
2108 HeapFree(GetProcessHeap(), 0, buffer);
2109 HGLOBAL_UserFree(&flags, &hglobal2);
2111 hglobal = GlobalAlloc(0, 4);
2112 buffer = GlobalLock(hglobal);
2113 for (i = 0; i < 4; i++)
2115 GlobalUnlock(hglobal);
2116 flags = MAKELONG(MSHCTX_LOCAL, NDR_LOCAL_DATA_REPRESENTATION);
2117 size = HGLOBAL_UserSize(&flags, 0, &hglobal);
2118 /* native is poorly programmed and allocates 4 bytes more than it needs to
2119 * here - Wine doesn't have to emulate that */
2120 ok((size == 24) || (size == 28), "Size should be 24 or 28, instead of %ld\n", size);
2121 buffer = HeapAlloc(GetProcessHeap(), 0, size);
2122 HGLOBAL_UserMarshal(&flags, buffer, &hglobal);
2123 wirehglobal = buffer;
2124 ok(*(ULONG *)wirehglobal == WDT_REMOTE_CALL, "Context should be WDT_REMOTE_CALL instead of 0x%08lx\n", *(ULONG *)wirehglobal);
2125 wirehglobal += sizeof(ULONG);
2126 ok(*(ULONG *)wirehglobal == (ULONG)hglobal, "buffer+0x4 should be HGLOBAL\n");
2127 wirehglobal += sizeof(ULONG);
2128 ok(*(ULONG *)wirehglobal == 4, "buffer+0x8 should be size of HGLOBAL\n");
2129 wirehglobal += sizeof(ULONG);
2130 ok(*(ULONG *)wirehglobal == (ULONG)hglobal, "buffer+0xc should be HGLOBAL\n");
2131 wirehglobal += sizeof(ULONG);
2132 ok(*(ULONG *)wirehglobal == 4, "buffer+0x10 should be size of HGLOBAL\n");
2133 wirehglobal += sizeof(ULONG);
2134 for (i = 0; i < 4; i++)
2135 ok(wirehglobal[i] == i, "buffer+0x%x should be %d\n", 0x10 + i, i);
2136 HGLOBAL_UserUnmarshal(&flags, buffer, &hglobal2);
2137 ok(hglobal2 != NULL, "Didn't unmarshal properly\n");
2138 HeapFree(GetProcessHeap(), 0, buffer);
2139 HGLOBAL_UserFree(&flags, &hglobal2);
2140 GlobalFree(hglobal);
2143 static HENHMETAFILE create_emf(void)
2145 RECT rect = {0, 0, 100, 100};
2146 HDC hdc = CreateEnhMetaFile(NULL, NULL, &rect, "HENHMETAFILE Marshaling Test\0Test\0\0");
2147 ExtTextOut(hdc, 0, 0, ETO_OPAQUE, NULL, "Test String", strlen("Test String"), NULL);
2148 return CloseEnhMetaFile(hdc);
2151 static void test_marshal_HENHMETAFILE(void)
2153 unsigned char *buffer;
2155 unsigned long flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
2157 HENHMETAFILE hemf2 = NULL;
2158 unsigned char *wirehemf;
2160 hemf = create_emf();
2162 size = HENHMETAFILE_UserSize(&flags, 0, &hemf);
2163 ok(size > 20, "size should be at least 20 bytes, not %ld\n", size);
2164 buffer = HeapAlloc(GetProcessHeap(), 0, size);
2165 HENHMETAFILE_UserMarshal(&flags, buffer, &hemf);
2167 ok(*(DWORD *)wirehemf == WDT_REMOTE_CALL, "wirestgm + 0x0 should be WDT_REMOTE_CALL instead of 0x%08lx\n", *(DWORD *)wirehemf);
2168 wirehemf += sizeof(DWORD);
2169 ok(*(DWORD *)wirehemf == (DWORD)(DWORD_PTR)hemf, "wirestgm + 0x4 should be hemf instead of 0x%08lx\n", *(DWORD *)wirehemf);
2170 wirehemf += sizeof(DWORD);
2171 ok(*(DWORD *)wirehemf == (size - 0x10), "wirestgm + 0x8 should be size - 0x10 instead of 0x%08lx\n", *(DWORD *)wirehemf);
2172 wirehemf += sizeof(DWORD);
2173 ok(*(DWORD *)wirehemf == (size - 0x10), "wirestgm + 0xc should be size - 0x10 instead of 0x%08lx\n", *(DWORD *)wirehemf);
2174 wirehemf += sizeof(DWORD);
2175 ok(*(DWORD *)wirehemf == EMR_HEADER, "wirestgm + 0x10 should be EMR_HEADER instead of %ld\n", *(DWORD *)wirehemf);
2176 wirehemf += sizeof(DWORD);
2177 /* ... rest of data not tested - refer to tests for GetEnhMetaFileBits
2180 HENHMETAFILE_UserUnmarshal(&flags, buffer, &hemf2);
2181 ok(hemf2 != NULL, "HENHMETAFILE didn't unmarshal\n");
2182 HeapFree(GetProcessHeap(), 0, buffer);
2183 HENHMETAFILE_UserFree(&flags, &hemf2);
2184 DeleteEnhMetaFile(hemf);
2189 size = HENHMETAFILE_UserSize(&flags, 0, &hemf);
2190 ok(size == 8, "size should be 8 bytes, not %ld\n", size);
2191 buffer = (unsigned char *)HeapAlloc(GetProcessHeap(), 0, size);
2192 HENHMETAFILE_UserMarshal(&flags, buffer, &hemf);
2194 ok(*(DWORD *)wirehemf == WDT_REMOTE_CALL, "wirestgm + 0x0 should be WDT_REMOTE_CALL instead of 0x%08lx\n", *(DWORD *)wirehemf);
2195 wirehemf += sizeof(DWORD);
2196 ok(*(DWORD *)wirehemf == (DWORD)(DWORD_PTR)hemf, "wirestgm + 0x4 should be hemf instead of 0x%08lx\n", *(DWORD *)wirehemf);
2197 wirehemf += sizeof(DWORD);
2199 HENHMETAFILE_UserUnmarshal(&flags, buffer, &hemf2);
2200 ok(hemf2 == NULL, "NULL HENHMETAFILE didn't unmarshal\n");
2201 HeapFree(GetProcessHeap(), 0, buffer);
2202 HENHMETAFILE_UserFree(&flags, &hemf2);
2208 HMODULE hOle32 = GetModuleHandle("ole32");
2209 if (!(pCoInitializeEx = (void*)GetProcAddress(hOle32, "CoInitializeEx"))) goto no_test;
2211 /* register a window class used in several tests */
2212 memset(&wndclass, 0, sizeof(wndclass));
2213 wndclass.lpfnWndProc = window_proc;
2214 wndclass.lpszClassName = "WineCOMTest";
2215 RegisterClass(&wndclass);
2217 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
2219 /* FIXME: test CoCreateInstanceEx */
2221 /* helper function tests */
2222 test_CoGetPSClsid();
2224 /* lifecycle management and marshaling tests */
2225 test_no_marshaler();
2226 test_normal_marshal_and_release();
2227 test_normal_marshal_and_unmarshal();
2228 test_marshal_and_unmarshal_invalid();
2229 test_interthread_marshal_and_unmarshal();
2230 test_proxy_marshal_and_unmarshal();
2231 test_proxy_marshal_and_unmarshal2();
2232 test_marshal_stub_apartment_shutdown();
2233 test_marshal_proxy_apartment_shutdown();
2234 test_marshal_proxy_mta_apartment_shutdown();
2235 test_no_couninitialize_server();
2236 test_no_couninitialize_client();
2237 test_tableweak_marshal_and_unmarshal_twice();
2238 test_tableweak_marshal_releasedata1();
2239 test_tableweak_marshal_releasedata2();
2240 test_tablestrong_marshal_and_unmarshal_twice();
2241 test_lock_object_external();
2242 test_disconnect_stub();
2243 test_normal_marshal_and_unmarshal_twice();
2244 test_hresult_marshaling();
2245 test_proxy_used_in_wrong_thread();
2246 test_message_filter();
2247 test_bad_marshal_stream();
2248 test_proxy_interfaces();
2249 test_stubbuffer(&IID_IClassFactory);
2250 test_proxybuffer(&IID_IClassFactory);
2251 test_message_reentrancy();
2252 test_WM_QUIT_handling();
2253 test_freethreadedmarshaler();
2255 /* doesn't pass with Win9x COM DLLs (even though Essential COM says it should) */
2256 if (0) test_out_of_process_com();
2259 test_globalinterfacetable();
2261 test_marshal_CLIPFORMAT();
2262 test_marshal_HWND();
2263 test_marshal_HGLOBAL();
2264 test_marshal_HENHMETAFILE();
2270 trace("You need DCOM95 installed to run this test\n");