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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 that stubs are released when the containing apartment is destroyed */
476 static void test_marshal_stub_apartment_shutdown(void)
479 IStream *pStream = NULL;
480 IUnknown *pProxy = NULL;
486 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
487 ok_ole_success(hr, CreateStreamOnHGlobal);
488 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
490 ok_more_than_one_lock();
492 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
493 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
494 ok_ole_success(hr, CoUnmarshalInterface);
495 IStream_Release(pStream);
497 ok_more_than_one_lock();
499 end_host_object(tid, thread);
503 IUnknown_Release(pProxy);
508 /* tests that proxies are released when the containing apartment is destroyed */
509 static void test_marshal_proxy_apartment_shutdown(void)
512 IStream *pStream = NULL;
513 IUnknown *pProxy = NULL;
519 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
520 ok_ole_success(hr, CreateStreamOnHGlobal);
521 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
523 ok_more_than_one_lock();
525 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
526 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
527 ok_ole_success(hr, CoUnmarshalInterface);
528 IStream_Release(pStream);
530 ok_more_than_one_lock();
536 IUnknown_Release(pProxy);
540 end_host_object(tid, thread);
542 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
545 /* tests that proxies are released when the containing mta apartment is destroyed */
546 static void test_marshal_proxy_mta_apartment_shutdown(void)
549 IStream *pStream = NULL;
550 IUnknown *pProxy = NULL;
555 pCoInitializeEx(NULL, COINIT_MULTITHREADED);
559 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
560 ok_ole_success(hr, CreateStreamOnHGlobal);
561 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
563 ok_more_than_one_lock();
565 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
566 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
567 ok_ole_success(hr, CoUnmarshalInterface);
568 IStream_Release(pStream);
570 ok_more_than_one_lock();
576 IUnknown_Release(pProxy);
580 end_host_object(tid, thread);
582 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
588 HANDLE marshal_event;
589 HANDLE unmarshal_event;
592 /* helper for test_no_couninitialize_server */
593 static DWORD CALLBACK no_couninitialize_server_proc(LPVOID p)
595 struct ncu_params *ncu_params = (struct ncu_params *)p;
598 pCoInitializeEx(NULL, COINIT_MULTITHREADED);
600 hr = CoMarshalInterface(ncu_params->stream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
601 ok_ole_success(hr, CoMarshalInterface);
603 SetEvent(ncu_params->marshal_event);
605 WaitForSingleObject(ncu_params->unmarshal_event, INFINITE);
607 /* die without calling CoUninitialize */
612 /* tests apartment that an apartment with a stub is released without deadlock
613 * if the owning thread exits */
614 static void test_no_couninitialize_server(void)
617 IStream *pStream = NULL;
618 IUnknown *pProxy = NULL;
621 struct ncu_params ncu_params;
625 ncu_params.marshal_event = CreateEvent(NULL, TRUE, FALSE, NULL);
626 ncu_params.unmarshal_event = CreateEvent(NULL, TRUE, FALSE, NULL);
628 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
629 ok_ole_success(hr, CreateStreamOnHGlobal);
630 ncu_params.stream = pStream;
632 thread = CreateThread(NULL, 0, no_couninitialize_server_proc, &ncu_params, 0, &tid);
634 WaitForSingleObject(ncu_params.marshal_event, INFINITE);
635 ok_more_than_one_lock();
637 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
638 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
639 ok_ole_success(hr, CoUnmarshalInterface);
640 IStream_Release(pStream);
642 ok_more_than_one_lock();
644 SetEvent(ncu_params.unmarshal_event);
645 WaitForSingleObject(thread, INFINITE);
650 CloseHandle(ncu_params.marshal_event);
651 CloseHandle(ncu_params.unmarshal_event);
653 IUnknown_Release(pProxy);
658 /* STA -> STA call during DLL_THREAD_DETACH */
659 static DWORD CALLBACK no_couninitialize_client_proc(LPVOID p)
661 struct ncu_params *ncu_params = (struct ncu_params *)p;
663 IUnknown *pProxy = NULL;
665 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
667 hr = CoUnmarshalInterface(ncu_params->stream, &IID_IClassFactory, (void **)&pProxy);
668 ok_ole_success(hr, CoUnmarshalInterface);
670 ok_more_than_one_lock();
672 /* die without calling CoUninitialize */
677 /* tests STA -> STA call during DLL_THREAD_DETACH doesn't deadlock */
678 static void test_no_couninitialize_client(void)
681 IStream *pStream = NULL;
686 struct ncu_params ncu_params;
690 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
691 ok_ole_success(hr, CreateStreamOnHGlobal);
692 ncu_params.stream = pStream;
694 /* NOTE: assumes start_host_object uses an STA to host the object, as MTAs
695 * always deadlock when called from within DllMain */
696 host_tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown *)&Test_ClassFactory, MSHLFLAGS_NORMAL, &host_thread);
697 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
699 ok_more_than_one_lock();
701 thread = CreateThread(NULL, 0, no_couninitialize_client_proc, &ncu_params, 0, &tid);
703 WaitForSingleObject(thread, INFINITE);
708 end_host_object(host_tid, host_thread);
711 /* tests success case of a same-thread table-weak marshal, unmarshal, unmarshal */
712 static void test_tableweak_marshal_and_unmarshal_twice(void)
715 IStream *pStream = NULL;
716 IUnknown *pProxy1 = NULL;
717 IUnknown *pProxy2 = NULL;
723 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
724 ok_ole_success(hr, CreateStreamOnHGlobal);
725 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_TABLEWEAK, &thread);
727 ok_more_than_one_lock();
729 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
730 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy1);
731 ok_ole_success(hr, CoUnmarshalInterface);
733 ok_more_than_one_lock();
735 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
736 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
737 ok_ole_success(hr, CoUnmarshalInterface);
739 ok_more_than_one_lock();
741 IUnknown_Release(pProxy1);
742 IUnknown_Release(pProxy2);
744 /* this line is shows the difference between weak and strong table marshaling:
745 * weak has cLocks == 0
746 * strong has cLocks > 0 */
749 end_host_object(tid, thread);
752 /* tests releasing after unmarshaling one object */
753 static void test_tableweak_marshal_releasedata1(void)
756 IStream *pStream = NULL;
757 IUnknown *pProxy1 = NULL;
758 IUnknown *pProxy2 = NULL;
764 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
765 ok_ole_success(hr, CreateStreamOnHGlobal);
766 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_TABLEWEAK, &thread);
768 ok_more_than_one_lock();
770 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
771 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy1);
772 ok_ole_success(hr, CoUnmarshalInterface);
774 ok_more_than_one_lock();
776 /* release the remaining reference on the object by calling
777 * CoReleaseMarshalData in the hosting thread */
778 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
779 release_host_object(tid);
781 ok_more_than_one_lock();
783 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
784 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
785 ok_ole_success(hr, CoUnmarshalInterface);
786 IStream_Release(pStream);
788 ok_more_than_one_lock();
790 IUnknown_Release(pProxy1);
792 IUnknown_Release(pProxy2);
794 /* this line is shows the difference between weak and strong table marshaling:
795 * weak has cLocks == 0
796 * strong has cLocks > 0 */
799 end_host_object(tid, thread);
802 /* tests releasing after unmarshaling one object */
803 static void test_tableweak_marshal_releasedata2(void)
806 IStream *pStream = NULL;
807 IUnknown *pProxy = NULL;
813 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
814 ok_ole_success(hr, CreateStreamOnHGlobal);
815 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_TABLEWEAK, &thread);
817 ok_more_than_one_lock();
819 /* release the remaining reference on the object by calling
820 * CoReleaseMarshalData in the hosting thread */
821 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
822 release_host_object(tid);
826 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
827 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
830 ok(hr == CO_E_OBJNOTREG,
831 "CoUnmarshalInterface should have failed with CO_E_OBJNOTREG, but returned 0x%08lx instead\n",
834 IStream_Release(pStream);
838 end_host_object(tid, thread);
841 /* tests success case of a same-thread table-strong marshal, unmarshal, unmarshal */
842 static void test_tablestrong_marshal_and_unmarshal_twice(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_TABLESTRONG, &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 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
866 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
867 ok_ole_success(hr, CoUnmarshalInterface);
869 ok_more_than_one_lock();
871 if (pProxy1) IUnknown_Release(pProxy1);
872 if (pProxy2) IUnknown_Release(pProxy2);
874 /* this line is shows the difference between weak and strong table marshaling:
875 * weak has cLocks == 0
876 * strong has cLocks > 0 */
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);
883 IStream_Release(pStream);
887 end_host_object(tid, thread);
890 /* tests CoLockObjectExternal */
891 static void test_lock_object_external(void)
894 IStream *pStream = NULL;
898 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
899 ok_ole_success(hr, CreateStreamOnHGlobal);
900 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
901 ok_ole_success(hr, CoMarshalInterface);
903 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, TRUE, TRUE);
905 ok_more_than_one_lock();
907 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
908 hr = CoReleaseMarshalData(pStream);
909 ok_ole_success(hr, CoReleaseMarshalData);
910 IStream_Release(pStream);
912 ok_more_than_one_lock();
914 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, FALSE, TRUE);
919 /* tests disconnecting stubs */
920 static void test_disconnect_stub(void)
923 IStream *pStream = NULL;
927 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
928 ok_ole_success(hr, CreateStreamOnHGlobal);
929 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
930 ok_ole_success(hr, CoMarshalInterface);
932 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, TRUE, TRUE);
934 ok_more_than_one_lock();
936 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
937 hr = CoReleaseMarshalData(pStream);
938 ok_ole_success(hr, CoReleaseMarshalData);
939 IStream_Release(pStream);
941 ok_more_than_one_lock();
943 CoDisconnectObject((IUnknown*)&Test_ClassFactory, 0);
948 /* tests failure case of a same-thread marshal and unmarshal twice */
949 static void test_normal_marshal_and_unmarshal_twice(void)
952 IStream *pStream = NULL;
953 IUnknown *pProxy1 = NULL;
954 IUnknown *pProxy2 = 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 ok_more_than_one_lock();
965 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
966 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy1);
967 ok_ole_success(hr, CoUnmarshalInterface);
969 ok_more_than_one_lock();
971 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
972 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
973 ok(hr == CO_E_OBJNOTCONNECTED,
974 "CoUnmarshalInterface should have failed with error CO_E_OBJNOTCONNECTED for double unmarshal, instead of 0x%08lx\n", hr);
976 IStream_Release(pStream);
978 ok_more_than_one_lock();
980 IUnknown_Release(pProxy1);
985 /* tests success case of marshaling and unmarshaling an HRESULT */
986 static void test_hresult_marshaling(void)
989 HRESULT hr_marshaled = 0;
990 IStream *pStream = NULL;
991 static const HRESULT E_DEADBEEF = 0xdeadbeef;
993 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
994 ok_ole_success(hr, CreateStreamOnHGlobal);
996 hr = CoMarshalHresult(pStream, E_DEADBEEF);
997 ok_ole_success(hr, CoMarshalHresult);
999 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1000 hr = IStream_Read(pStream, &hr_marshaled, sizeof(HRESULT), NULL);
1001 ok_ole_success(hr, IStream_Read);
1003 ok(hr_marshaled == E_DEADBEEF, "Didn't marshal HRESULT as expected: got value 0x%08lx instead\n", hr_marshaled);
1006 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1007 hr = CoUnmarshalHresult(pStream, &hr_marshaled);
1008 ok_ole_success(hr, CoUnmarshalHresult);
1010 ok(hr_marshaled == E_DEADBEEF, "Didn't marshal HRESULT as expected: got value 0x%08lx instead\n", hr_marshaled);
1012 IStream_Release(pStream);
1016 /* helper for test_proxy_used_in_wrong_thread */
1017 static DWORD CALLBACK bad_thread_proc(LPVOID p)
1019 IClassFactory * cf = (IClassFactory *)p;
1021 IUnknown * proxy = NULL;
1023 pCoInitializeEx(NULL, COINIT_MULTITHREADED);
1025 hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
1026 if (proxy) IUnknown_Release(proxy);
1027 ok(hr == RPC_E_WRONG_THREAD,
1028 "COM should have failed with RPC_E_WRONG_THREAD on using proxy from wrong apartment, but instead returned 0x%08lx\n",
1036 /* tests failure case of a using a proxy in the wrong apartment */
1037 static void test_proxy_used_in_wrong_thread(void)
1040 IStream *pStream = NULL;
1041 IUnknown *pProxy = NULL;
1048 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1049 ok_ole_success(hr, CreateStreamOnHGlobal);
1050 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &host_thread);
1052 ok_more_than_one_lock();
1054 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1055 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
1056 ok_ole_success(hr, CoUnmarshalInterface);
1057 IStream_Release(pStream);
1059 ok_more_than_one_lock();
1061 /* create a thread that we can misbehave in */
1062 thread = CreateThread(NULL, 0, bad_thread_proc, (LPVOID)pProxy, 0, &tid2);
1064 WaitForSingleObject(thread, INFINITE);
1065 CloseHandle(thread);
1067 IUnknown_Release(pProxy);
1071 end_host_object(tid, host_thread);
1074 static HRESULT WINAPI MessageFilter_QueryInterface(IMessageFilter *iface, REFIID riid, void ** ppvObj)
1076 if (ppvObj == NULL) return E_POINTER;
1078 if (IsEqualGUID(riid, &IID_IUnknown) ||
1079 IsEqualGUID(riid, &IID_IClassFactory))
1081 *ppvObj = (LPVOID)iface;
1082 IClassFactory_AddRef(iface);
1086 return E_NOINTERFACE;
1089 static ULONG WINAPI MessageFilter_AddRef(IMessageFilter *iface)
1091 return 2; /* non-heap object */
1094 static ULONG WINAPI MessageFilter_Release(IMessageFilter *iface)
1096 return 1; /* non-heap object */
1099 static DWORD WINAPI MessageFilter_HandleInComingCall(
1100 IMessageFilter *iface,
1102 HTASK threadIDCaller,
1104 LPINTERFACEINFO lpInterfaceInfo)
1106 static int callcount = 0;
1108 trace("HandleInComingCall\n");
1112 ret = SERVERCALL_REJECTED;
1115 ret = SERVERCALL_RETRYLATER;
1118 ret = SERVERCALL_ISHANDLED;
1125 static DWORD WINAPI MessageFilter_RetryRejectedCall(
1126 IMessageFilter *iface,
1127 HTASK threadIDCallee,
1131 trace("RetryRejectedCall\n");
1135 static DWORD WINAPI MessageFilter_MessagePending(
1136 IMessageFilter *iface,
1137 HTASK threadIDCallee,
1139 DWORD dwPendingType)
1141 trace("MessagePending\n");
1142 return PENDINGMSG_WAITNOPROCESS;
1145 static const IMessageFilterVtbl MessageFilter_Vtbl =
1147 MessageFilter_QueryInterface,
1148 MessageFilter_AddRef,
1149 MessageFilter_Release,
1150 MessageFilter_HandleInComingCall,
1151 MessageFilter_RetryRejectedCall,
1152 MessageFilter_MessagePending
1155 static IMessageFilter MessageFilter = { &MessageFilter_Vtbl };
1157 static void test_message_filter(void)
1160 IStream *pStream = NULL;
1161 IClassFactory *cf = NULL;
1163 IUnknown *proxy = NULL;
1164 IMessageFilter *prev_filter = NULL;
1169 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1170 ok_ole_success(hr, CreateStreamOnHGlobal);
1171 tid = start_host_object2(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &MessageFilter, &thread);
1173 ok_more_than_one_lock();
1175 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1176 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&cf);
1177 ok_ole_success(hr, CoUnmarshalInterface);
1178 IStream_Release(pStream);
1180 ok_more_than_one_lock();
1182 hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
1183 todo_wine { ok(hr == RPC_E_CALL_REJECTED, "Call should have returned RPC_E_CALL_REJECTED, but return 0x%08lx instead\n", hr); }
1184 if (proxy) IUnknown_Release(proxy);
1187 hr = CoRegisterMessageFilter(&MessageFilter, &prev_filter);
1188 ok_ole_success(hr, CoRegisterMessageFilter);
1189 if (prev_filter) IMessageFilter_Release(prev_filter);
1191 hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
1192 ok_ole_success(hr, IClassFactory_CreateInstance);
1194 IUnknown_Release(proxy);
1196 IClassFactory_Release(cf);
1200 end_host_object(tid, thread);
1203 /* test failure case of trying to unmarshal from bad stream */
1204 static void test_bad_marshal_stream(void)
1207 IStream *pStream = NULL;
1209 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1210 ok_ole_success(hr, CreateStreamOnHGlobal);
1211 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1212 ok_ole_success(hr, CoMarshalInterface);
1214 ok_more_than_one_lock();
1216 /* try to read beyond end of stream */
1217 hr = CoReleaseMarshalData(pStream);
1218 ok(hr == STG_E_READFAULT, "Should have failed with STG_E_READFAULT, but returned 0x%08lx instead\n", hr);
1220 /* now release for real */
1221 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1222 hr = CoReleaseMarshalData(pStream);
1223 ok_ole_success(hr, CoReleaseMarshalData);
1225 IStream_Release(pStream);
1228 /* tests that proxies implement certain interfaces */
1229 static void test_proxy_interfaces(void)
1232 IStream *pStream = NULL;
1233 IUnknown *pProxy = NULL;
1234 IUnknown *pOtherUnknown = NULL;
1240 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1241 ok_ole_success(hr, CreateStreamOnHGlobal);
1242 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
1244 ok_more_than_one_lock();
1246 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1247 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)&pProxy);
1248 ok_ole_success(hr, CoUnmarshalInterface);
1249 IStream_Release(pStream);
1251 ok_more_than_one_lock();
1253 hr = IUnknown_QueryInterface(pProxy, &IID_IUnknown, (LPVOID*)&pOtherUnknown);
1254 ok_ole_success(hr, IUnknown_QueryInterface IID_IUnknown);
1255 if (hr == S_OK) IUnknown_Release(pOtherUnknown);
1257 hr = IUnknown_QueryInterface(pProxy, &IID_IClientSecurity, (LPVOID*)&pOtherUnknown);
1258 todo_wine { ok_ole_success(hr, IUnknown_QueryInterface IID_IClientSecurity); }
1259 if (hr == S_OK) IUnknown_Release(pOtherUnknown);
1261 hr = IUnknown_QueryInterface(pProxy, &IID_IMultiQI, (LPVOID*)&pOtherUnknown);
1262 ok_ole_success(hr, IUnknown_QueryInterface IID_IMultiQI);
1263 if (hr == S_OK) IUnknown_Release(pOtherUnknown);
1265 hr = IUnknown_QueryInterface(pProxy, &IID_IMarshal, (LPVOID*)&pOtherUnknown);
1266 ok_ole_success(hr, IUnknown_QueryInterface IID_IMarshal);
1267 if (hr == S_OK) IUnknown_Release(pOtherUnknown);
1269 /* IMarshal2 is also supported on NT-based systems, but is pretty much
1270 * useless as it has no more methods over IMarshal that it inherits from. */
1272 IUnknown_Release(pProxy);
1276 end_host_object(tid, thread);
1281 const IUnknownVtbl *lpVtbl;
1285 static HRESULT WINAPI HeapUnknown_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
1287 if (IsEqualIID(riid, &IID_IUnknown))
1289 IUnknown_AddRef(iface);
1290 *ppv = (LPVOID)iface;
1294 return E_NOINTERFACE;
1297 static ULONG WINAPI HeapUnknown_AddRef(IUnknown *iface)
1299 HeapUnknown *This = (HeapUnknown *)iface;
1300 trace("HeapUnknown_AddRef(%p)\n", iface);
1301 return InterlockedIncrement((LONG*)&This->refs);
1304 static ULONG WINAPI HeapUnknown_Release(IUnknown *iface)
1306 HeapUnknown *This = (HeapUnknown *)iface;
1307 ULONG refs = InterlockedDecrement((LONG*)&This->refs);
1308 trace("HeapUnknown_Release(%p)\n", iface);
1309 if (!refs) HeapFree(GetProcessHeap(), 0, This);
1313 static const IUnknownVtbl HeapUnknown_Vtbl =
1315 HeapUnknown_QueryInterface,
1320 static void test_proxybuffer(REFIID riid)
1323 IPSFactoryBuffer *psfb;
1324 IRpcProxyBuffer *proxy;
1328 HeapUnknown *pUnkOuter = (HeapUnknown *)HeapAlloc(GetProcessHeap(), 0, sizeof(*pUnkOuter));
1330 pUnkOuter->lpVtbl = &HeapUnknown_Vtbl;
1331 pUnkOuter->refs = 1;
1333 hr = CoGetPSClsid(riid, &clsid);
1334 ok_ole_success(hr, CoGetPSClsid);
1336 hr = CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL, &IID_IPSFactoryBuffer, (LPVOID*)&psfb);
1337 ok_ole_success(hr, CoGetClassObject);
1339 hr = IPSFactoryBuffer_CreateProxy(psfb, (IUnknown*)pUnkOuter, riid, &proxy, &lpvtbl);
1340 ok_ole_success(hr, IPSFactoryBuffer_CreateProxy);
1341 ok(lpvtbl != NULL, "IPSFactoryBuffer_CreateProxy succeeded, but returned a NULL vtable!\n");
1343 refs = IPSFactoryBuffer_Release(psfb);
1344 #if 0 /* not reliable on native. maybe it leaks references! */
1345 ok(refs == 0, "Ref-count leak of %ld on IPSFactoryBuffer\n", refs);
1348 refs = IUnknown_Release((IUnknown *)lpvtbl);
1349 ok(refs == 1, "Ref-count leak of %ld on IRpcProxyBuffer\n", refs-1);
1351 refs = IRpcProxyBuffer_Release(proxy);
1352 ok(refs == 0, "Ref-count leak of %ld on IRpcProxyBuffer\n", refs);
1355 static void test_stubbuffer(REFIID riid)
1358 IPSFactoryBuffer *psfb;
1359 IRpcStubBuffer *stub;
1365 hr = CoGetPSClsid(riid, &clsid);
1366 ok_ole_success(hr, CoGetPSClsid);
1368 hr = CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL, &IID_IPSFactoryBuffer, (LPVOID*)&psfb);
1369 ok_ole_success(hr, CoGetClassObject);
1371 hr = IPSFactoryBuffer_CreateStub(psfb, riid, (IUnknown*)&Test_ClassFactory, &stub);
1372 ok_ole_success(hr, IPSFactoryBuffer_CreateStub);
1374 refs = IPSFactoryBuffer_Release(psfb);
1375 #if 0 /* not reliable on native. maybe it leaks references */
1376 ok(refs == 0, "Ref-count leak of %ld on IPSFactoryBuffer\n", refs);
1379 ok_more_than_one_lock();
1381 IRpcStubBuffer_Disconnect(stub);
1385 refs = IRpcStubBuffer_Release(stub);
1386 ok(refs == 0, "Ref-count leak of %ld on IRpcProxyBuffer\n", refs);
1389 static HWND hwnd_app;
1391 static HRESULT WINAPI TestRE_IClassFactory_CreateInstance(
1392 LPCLASSFACTORY iface,
1393 LPUNKNOWN pUnkOuter,
1398 if (IsEqualIID(riid, &IID_IWineTest))
1400 BOOL ret = SendMessageTimeout(hwnd_app, WM_NULL, 0, 0, SMTO_BLOCK, 5000, &res);
1401 ok(ret, "Timed out sending a message to originating window during RPC call\n");
1406 static const IClassFactoryVtbl TestREClassFactory_Vtbl =
1408 Test_IClassFactory_QueryInterface,
1409 Test_IClassFactory_AddRef,
1410 Test_IClassFactory_Release,
1411 TestRE_IClassFactory_CreateInstance,
1412 Test_IClassFactory_LockServer
1415 IClassFactory TestRE_ClassFactory = { &TestREClassFactory_Vtbl };
1417 static LRESULT CALLBACK window_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1424 IStream *pStream = NULL;
1425 IClassFactory *proxy = NULL;
1432 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1433 ok_ole_success(hr, CreateStreamOnHGlobal);
1434 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&TestRE_ClassFactory, MSHLFLAGS_NORMAL, &thread);
1436 ok_more_than_one_lock();
1438 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1439 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&proxy);
1440 ok_ole_success(hr, CoReleaseMarshalData);
1441 IStream_Release(pStream);
1443 ok_more_than_one_lock();
1445 /* note the use of the magic IID_IWineTest value to tell remote thread
1446 * to try to send a message back to us */
1447 hr = IClassFactory_CreateInstance(proxy, NULL, &IID_IWineTest, (void **)&object);
1449 IClassFactory_Release(proxy);
1453 end_host_object(tid, thread);
1455 PostMessage(hwnd, WM_QUIT, 0, 0);
1462 IStream *pStream = NULL;
1463 IClassFactory *proxy = NULL;
1470 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1471 ok_ole_success(hr, CreateStreamOnHGlobal);
1472 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&TestRE_ClassFactory, MSHLFLAGS_NORMAL, &thread);
1474 ok_more_than_one_lock();
1476 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1477 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&proxy);
1478 ok_ole_success(hr, CoReleaseMarshalData);
1479 IStream_Release(pStream);
1481 ok_more_than_one_lock();
1483 /* post quit message before a doing a COM call to show that a pending
1484 * WM_QUIT message doesn't stop the call from succeeding */
1485 PostMessage(hwnd, WM_QUIT, 0, 0);
1486 hr = IClassFactory_CreateInstance(proxy, NULL, &IID_IUnknown, (void **)&object);
1488 IClassFactory_Release(proxy);
1492 end_host_object(tid, thread);
1497 return DefWindowProc(hwnd, msg, wparam, lparam);
1501 static void test_message_reentrancy(void)
1506 memset(&wndclass, 0, sizeof(wndclass));
1507 wndclass.lpfnWndProc = window_proc;
1508 wndclass.lpszClassName = "WineCOMTest";
1509 RegisterClass(&wndclass);
1511 hwnd_app = CreateWindow("WineCOMTest", NULL, 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, NULL, 0);
1512 ok(hwnd_app != NULL, "Window creation failed\n");
1514 /* start message re-entrancy test */
1515 PostMessage(hwnd_app, WM_USER, 0, 0);
1517 while (GetMessage(&msg, NULL, 0, 0))
1519 TranslateMessage(&msg);
1520 DispatchMessage(&msg);
1524 static void test_WM_QUIT_handling(void)
1528 hwnd_app = CreateWindow("WineCOMTest", NULL, 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, NULL, 0);
1529 ok(hwnd_app != NULL, "Window creation failed\n");
1531 /* start WM_QUIT handling test */
1532 PostMessage(hwnd_app, WM_USER+1, 0, 0);
1534 while (GetMessage(&msg, NULL, 0, 0))
1536 TranslateMessage(&msg);
1537 DispatchMessage(&msg);
1541 /* doesn't pass with Win9x COM DLLs (even though Essential COM says it should) */
1544 static HANDLE heventShutdown;
1546 static void LockModuleOOP()
1548 InterlockedIncrement(&cLocks); /* for test purposes only */
1549 CoAddRefServerProcess();
1552 static void UnlockModuleOOP()
1554 InterlockedDecrement(&cLocks); /* for test purposes only */
1555 if (!CoReleaseServerProcess())
1556 SetEvent(heventShutdown);
1559 static HWND hwnd_app;
1561 static HRESULT WINAPI TestOOP_IClassFactory_QueryInterface(
1562 LPCLASSFACTORY iface,
1566 if (ppvObj == NULL) return E_POINTER;
1568 if (IsEqualGUID(riid, &IID_IUnknown) ||
1569 IsEqualGUID(riid, &IID_IClassFactory))
1571 *ppvObj = (LPVOID)iface;
1572 IClassFactory_AddRef(iface);
1576 return E_NOINTERFACE;
1579 static ULONG WINAPI TestOOP_IClassFactory_AddRef(LPCLASSFACTORY iface)
1581 return 2; /* non-heap-based object */
1584 static ULONG WINAPI TestOOP_IClassFactory_Release(LPCLASSFACTORY iface)
1586 return 1; /* non-heap-based object */
1589 static HRESULT WINAPI TestOOP_IClassFactory_CreateInstance(
1590 LPCLASSFACTORY iface,
1591 LPUNKNOWN pUnkOuter,
1595 return CLASS_E_CLASSNOTAVAILABLE;
1598 static HRESULT WINAPI TestOOP_IClassFactory_LockServer(
1599 LPCLASSFACTORY iface,
1609 static const IClassFactoryVtbl TestClassFactoryOOP_Vtbl =
1611 TestOOP_IClassFactory_QueryInterface,
1612 TestOOP_IClassFactory_AddRef,
1613 TestOOP_IClassFactory_Release,
1614 TestOOP_IClassFactory_CreateInstance,
1615 TestOOP_IClassFactory_LockServer
1618 static IClassFactory TestOOP_ClassFactory = { &TestClassFactoryOOP_Vtbl };
1620 /* tests functions commonly used by out of process COM servers */
1621 static void test_out_of_process_com(void)
1623 static const CLSID CLSID_WineOOPTest = {
1627 {0xa1, 0xa2, 0x5d, 0x5a, 0x36, 0x54, 0xd3, 0xbd}
1628 }; /* 5201163f-8164-4fd0-a1a2-5d5a3654d3bd */
1634 heventShutdown = CreateEvent(NULL, TRUE, FALSE, NULL);
1638 /* Start the object suspended */
1639 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&TestOOP_ClassFactory,
1640 CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE | REGCLS_SUSPENDED, &cookie);
1641 ok_ole_success(hr, CoRegisterClassObject);
1643 /* ... and CoGetClassObject does not find it and fails when it looks for the
1644 * class in the registry */
1645 hr = CoGetClassObject(&CLSID_WineOOPTest, CLSCTX_INPROC_SERVER,
1646 NULL, &IID_IClassFactory, (LPVOID*)&cf);
1648 ok(hr == REGDB_E_CLASSNOTREG,
1649 "CoGetClassObject should have returned REGDB_E_CLASSNOTREG instead of 0x%08lx\n", hr);
1652 /* Resume the object suspended above ... */
1653 hr = CoResumeClassObjects();
1654 ok_ole_success(hr, CoResumeClassObjects);
1656 /* ... and now it should succeed */
1657 hr = CoGetClassObject(&CLSID_WineOOPTest, CLSCTX_INPROC_SERVER,
1658 NULL, &IID_IClassFactory, (LPVOID*)&cf);
1659 ok_ole_success(hr, CoGetClassObject);
1661 /* Now check the locking is working */
1662 /* NOTE: we are accessing the class directly, not through a proxy */
1666 hr = IClassFactory_LockServer(cf, TRUE);
1667 trace("IClassFactory_LockServer returned 0x%08lx\n", hr);
1669 ok_more_than_one_lock();
1671 IClassFactory_LockServer(cf, FALSE);
1675 IClassFactory_Release(cf);
1677 /* wait for shutdown signal */
1678 ret = WaitForSingleObject(heventShutdown, 5000);
1679 todo_wine { ok(ret != WAIT_TIMEOUT, "Server didn't shut down or machine is under very heavy load\n"); }
1681 /* try to connect again after SCM has suspended registered class objects */
1682 hr = CoGetClassObject(&CLSID_WineOOPTest, CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER, NULL,
1683 &IID_IClassFactory, (LPVOID*)&cf);
1685 ok(hr == CO_E_SERVER_STOPPING,
1686 "CoGetClassObject should have returned CO_E_SERVER_STOPPING instead of 0x%08lx\n", hr);
1689 hr = CoRevokeClassObject(cookie);
1690 ok_ole_success(hr, CoRevokeClassObject);
1692 CloseHandle(heventShutdown);
1696 static void test_ROT(void)
1698 static const WCHAR wszFileName[] = {'B','E','2','0','E','2','F','5','-',
1699 '1','9','0','3','-','4','A','A','E','-','B','1','A','F','-',
1700 '2','0','4','6','E','5','8','6','C','9','2','5',0};
1702 IMoniker *pMoniker = NULL;
1703 IRunningObjectTable *pROT = NULL;
1708 hr = CreateFileMoniker(wszFileName, &pMoniker);
1709 ok_ole_success(hr, CreateClassMoniker);
1710 hr = GetRunningObjectTable(0, &pROT);
1711 ok_ole_success(hr, GetRunningObjectTable);
1712 hr = IRunningObjectTable_Register(pROT, 0, (IUnknown*)&Test_ClassFactory, pMoniker, &dwCookie);
1713 ok_ole_success(hr, IRunningObjectTable_Register);
1715 ok_more_than_one_lock();
1717 hr = IRunningObjectTable_Revoke(pROT, dwCookie);
1718 ok_ole_success(hr, IRunningObjectTable_Revoke);
1723 static const char cf_marshaled[] =
1735 static void test_marshal_CLIPFORMAT(void)
1737 unsigned char *buffer;
1739 unsigned long flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
1740 wireCLIPFORMAT wirecf;
1741 CLIPFORMAT cf = RegisterClipboardFormatA("MyFormat");
1744 size = CLIPFORMAT_UserSize(&flags, 0, &cf);
1745 ok(size == sizeof(*wirecf) + sizeof(cf_marshaled), "Size should be %d, instead of %ld\n", sizeof(*wirecf) + sizeof(cf_marshaled), size);
1747 buffer = HeapAlloc(GetProcessHeap(), 0, size);
1748 CLIPFORMAT_UserMarshal(&flags, buffer, &cf);
1749 wirecf = (wireCLIPFORMAT)buffer;
1750 ok(wirecf->fContext == WDT_REMOTE_CALL, "Context should be WDT_REMOTE_CALL instead of 0x%08lx\n", wirecf->fContext);
1751 ok(wirecf->u.dwValue == cf, "Marshaled value should be 0x%04x instead of 0x%04lx\n", cf, wirecf->u.dwValue);
1752 ok(!memcmp(wirecf+1, cf_marshaled, sizeof(cf_marshaled)), "Marshaled data differs\n");
1754 CLIPFORMAT_UserUnmarshal(&flags, buffer, &cf2);
1755 ok(cf == cf2, "Didn't unmarshal properly\n");
1756 HeapFree(GetProcessHeap(), 0, buffer);
1758 CLIPFORMAT_UserFree(&flags, &cf2);
1761 static void test_marshal_HWND(void)
1763 unsigned char *buffer;
1765 unsigned long flags = MAKELONG(MSHCTX_LOCAL, NDR_LOCAL_DATA_REPRESENTATION);
1766 HWND hwnd = GetDesktopWindow();
1770 size = HWND_UserSize(&flags, 0, &hwnd);
1771 ok(size == sizeof(*wirehwnd), "Size should be %d, instead of %ld\n", sizeof(*wirehwnd), size);
1773 buffer = HeapAlloc(GetProcessHeap(), 0, size);
1774 HWND_UserMarshal(&flags, buffer, &hwnd);
1775 wirehwnd = (wireHWND)buffer;
1776 ok(wirehwnd->fContext == WDT_INPROC_CALL, "Context should be WDT_INPROC_CALL instead of 0x%08lx\n", wirehwnd->fContext);
1777 ok(wirehwnd->u.hInproc == (LONG_PTR)hwnd, "Marshaled value should be %p instead of %p\n", hwnd, (HANDLE)wirehwnd->u.hRemote);
1779 HWND_UserUnmarshal(&flags, buffer, &hwnd2);
1780 ok(hwnd == hwnd2, "Didn't unmarshal properly\n");
1781 HeapFree(GetProcessHeap(), 0, buffer);
1783 HWND_UserFree(&flags, &hwnd2);
1786 static void test_marshal_HGLOBAL(void)
1788 unsigned char *buffer;
1790 unsigned long flags = MAKELONG(MSHCTX_LOCAL, NDR_LOCAL_DATA_REPRESENTATION);
1793 unsigned char *wirehglobal;
1797 flags = MAKELONG(MSHCTX_LOCAL, NDR_LOCAL_DATA_REPRESENTATION);
1798 size = HGLOBAL_UserSize(&flags, 0, &hglobal);
1799 /* native is poorly programmed and allocates 4 bytes more than it needs to
1800 * here - Wine doesn't have to emulate that */
1801 ok((size == 8) || (size == 12), "Size should be 12, instead of %ld\n", size);
1802 buffer = HeapAlloc(GetProcessHeap(), 0, size);
1803 HGLOBAL_UserMarshal(&flags, buffer, &hglobal);
1804 wirehglobal = buffer;
1805 ok(*(ULONG *)wirehglobal == WDT_REMOTE_CALL, "Context should be WDT_REMOTE_CALL instead of 0x%08lx\n", *(ULONG *)wirehglobal);
1806 wirehglobal += sizeof(ULONG);
1807 ok(*(ULONG *)wirehglobal == (ULONG)hglobal, "buffer+4 should be HGLOBAL\n");
1808 HGLOBAL_UserUnmarshal(&flags, buffer, &hglobal2);
1809 ok(hglobal2 == hglobal, "Didn't unmarshal properly\n");
1810 HeapFree(GetProcessHeap(), 0, buffer);
1811 HGLOBAL_UserFree(&flags, &hglobal2);
1813 hglobal = GlobalAlloc(0, 4);
1814 buffer = GlobalLock(hglobal);
1815 for (i = 0; i < 4; i++)
1817 GlobalUnlock(hglobal);
1818 flags = MAKELONG(MSHCTX_LOCAL, NDR_LOCAL_DATA_REPRESENTATION);
1819 size = HGLOBAL_UserSize(&flags, 0, &hglobal);
1820 /* native is poorly programmed and allocates 4 bytes more than it needs to
1821 * here - Wine doesn't have to emulate that */
1822 ok((size == 24) || (size == 28), "Size should be 24 or 28, instead of %ld\n", size);
1823 buffer = HeapAlloc(GetProcessHeap(), 0, size);
1824 HGLOBAL_UserMarshal(&flags, buffer, &hglobal);
1825 wirehglobal = buffer;
1826 ok(*(ULONG *)wirehglobal == WDT_REMOTE_CALL, "Context should be WDT_REMOTE_CALL instead of 0x%08lx\n", *(ULONG *)wirehglobal);
1827 wirehglobal += sizeof(ULONG);
1828 ok(*(ULONG *)wirehglobal == (ULONG)hglobal, "buffer+0x4 should be HGLOBAL\n");
1829 wirehglobal += sizeof(ULONG);
1830 ok(*(ULONG *)wirehglobal == 4, "buffer+0x8 should be size of HGLOBAL\n");
1831 wirehglobal += sizeof(ULONG);
1832 ok(*(ULONG *)wirehglobal == (ULONG)hglobal, "buffer+0xc should be HGLOBAL\n");
1833 wirehglobal += sizeof(ULONG);
1834 ok(*(ULONG *)wirehglobal == 4, "buffer+0x10 should be size of HGLOBAL\n");
1835 wirehglobal += sizeof(ULONG);
1836 for (i = 0; i < 4; i++)
1837 ok(wirehglobal[i] == i, "buffer+0x%x should be %d\n", 0x10 + i, i);
1838 HGLOBAL_UserUnmarshal(&flags, buffer, &hglobal2);
1839 ok(hglobal2 != NULL, "Didn't unmarshal properly\n");
1840 HeapFree(GetProcessHeap(), 0, buffer);
1841 HGLOBAL_UserFree(&flags, &hglobal2);
1842 GlobalFree(hglobal);
1845 static HENHMETAFILE create_emf(void)
1847 RECT rect = {0, 0, 100, 100};
1848 HDC hdc = CreateEnhMetaFile(NULL, NULL, &rect, "HENHMETAFILE Marshaling Test\0Test\0\0");
1849 ExtTextOut(hdc, 0, 0, ETO_OPAQUE, NULL, "Test String", strlen("Test String"), NULL);
1850 return CloseEnhMetaFile(hdc);
1853 static void test_marshal_HENHMETAFILE(void)
1855 unsigned char *buffer;
1857 unsigned long flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
1859 HENHMETAFILE hemf2 = NULL;
1860 unsigned char *wirehemf;
1862 hemf = create_emf();
1864 size = HENHMETAFILE_UserSize(&flags, 0, &hemf);
1865 ok(size > 20, "size should be at least 20 bytes, not %ld\n", size);
1866 buffer = HeapAlloc(GetProcessHeap(), 0, size);
1867 HENHMETAFILE_UserMarshal(&flags, buffer, &hemf);
1869 ok(*(DWORD *)wirehemf == WDT_REMOTE_CALL, "wirestgm + 0x0 should be WDT_REMOTE_CALL instead of 0x%08lx\n", *(DWORD *)wirehemf);
1870 wirehemf += sizeof(DWORD);
1871 ok(*(DWORD *)wirehemf == (DWORD)(DWORD_PTR)hemf, "wirestgm + 0x4 should be hemf instead of 0x%08lx\n", *(DWORD *)wirehemf);
1872 wirehemf += sizeof(DWORD);
1873 ok(*(DWORD *)wirehemf == (size - 0x10), "wirestgm + 0x8 should be size - 0x10 instead of 0x%08lx\n", *(DWORD *)wirehemf);
1874 wirehemf += sizeof(DWORD);
1875 ok(*(DWORD *)wirehemf == (size - 0x10), "wirestgm + 0xc should be size - 0x10 instead of 0x%08lx\n", *(DWORD *)wirehemf);
1876 wirehemf += sizeof(DWORD);
1877 ok(*(DWORD *)wirehemf == EMR_HEADER, "wirestgm + 0x10 should be EMR_HEADER instead of %ld\n", *(DWORD *)wirehemf);
1878 wirehemf += sizeof(DWORD);
1879 /* ... rest of data not tested - refer to tests for GetEnhMetaFileBits
1882 HENHMETAFILE_UserUnmarshal(&flags, buffer, &hemf2);
1883 ok(hemf2 != NULL, "HENHMETAFILE didn't unmarshal\n");
1884 HeapFree(GetProcessHeap(), 0, buffer);
1885 HENHMETAFILE_UserFree(&flags, &hemf2);
1886 DeleteEnhMetaFile(hemf);
1891 size = HENHMETAFILE_UserSize(&flags, 0, &hemf);
1892 ok(size == 8, "size should be 8 bytes, not %ld\n", size);
1893 buffer = (unsigned char *)HeapAlloc(GetProcessHeap(), 0, size);
1894 HENHMETAFILE_UserMarshal(&flags, buffer, &hemf);
1896 ok(*(DWORD *)wirehemf == WDT_REMOTE_CALL, "wirestgm + 0x0 should be WDT_REMOTE_CALL instead of 0x%08lx\n", *(DWORD *)wirehemf);
1897 wirehemf += sizeof(DWORD);
1898 ok(*(DWORD *)wirehemf == (DWORD)(DWORD_PTR)hemf, "wirestgm + 0x4 should be hemf instead of 0x%08lx\n", *(DWORD *)wirehemf);
1899 wirehemf += sizeof(DWORD);
1901 HENHMETAFILE_UserUnmarshal(&flags, buffer, &hemf2);
1902 ok(hemf2 == NULL, "NULL HENHMETAFILE didn't unmarshal\n");
1903 HeapFree(GetProcessHeap(), 0, buffer);
1904 HENHMETAFILE_UserFree(&flags, &hemf2);
1910 HMODULE hOle32 = GetModuleHandle("ole32");
1911 if (!(pCoInitializeEx = (void*)GetProcAddress(hOle32, "CoInitializeEx"))) goto no_test;
1913 /* register a window class used in several tests */
1914 memset(&wndclass, 0, sizeof(wndclass));
1915 wndclass.lpfnWndProc = window_proc;
1916 wndclass.lpszClassName = "WineCOMTest";
1917 RegisterClass(&wndclass);
1919 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1921 /* FIXME: test CoCreateInstanceEx */
1923 /* helper function tests */
1924 test_CoGetPSClsid();
1926 /* lifecycle management and marshaling tests */
1927 test_no_marshaler();
1928 test_normal_marshal_and_release();
1929 test_normal_marshal_and_unmarshal();
1930 test_marshal_and_unmarshal_invalid();
1931 test_interthread_marshal_and_unmarshal();
1932 test_proxy_marshal_and_unmarshal();
1933 test_marshal_stub_apartment_shutdown();
1934 test_marshal_proxy_apartment_shutdown();
1935 test_marshal_proxy_mta_apartment_shutdown();
1936 test_no_couninitialize_server();
1937 test_no_couninitialize_client();
1938 test_tableweak_marshal_and_unmarshal_twice();
1939 test_tableweak_marshal_releasedata1();
1940 test_tableweak_marshal_releasedata2();
1941 test_tablestrong_marshal_and_unmarshal_twice();
1942 test_lock_object_external();
1943 test_disconnect_stub();
1944 test_normal_marshal_and_unmarshal_twice();
1945 test_hresult_marshaling();
1946 test_proxy_used_in_wrong_thread();
1947 test_message_filter();
1948 test_bad_marshal_stream();
1949 test_proxy_interfaces();
1950 test_stubbuffer(&IID_IClassFactory);
1951 test_proxybuffer(&IID_IClassFactory);
1952 test_message_reentrancy();
1953 test_WM_QUIT_handling();
1955 /* test_out_of_process_com(); */
1958 /* FIXME: test GIT */
1960 test_marshal_CLIPFORMAT();
1961 test_marshal_HWND();
1962 test_marshal_HGLOBAL();
1963 test_marshal_HENHMETAFILE();
1969 trace("You need DCOM95 installed to run this test\n");