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 void test_CoGetPSClsid()
44 static const CLSID IID_IWineTest = {
48 {0xa1, 0xa2, 0x5d, 0x5a, 0x36, 0x54, 0xd3, 0xbd}
49 }; /* 5201163f-8164-4fd0-a1a2-5d5a3654d3bd */
51 hr = CoGetPSClsid(&IID_IClassFactory, &clsid);
52 ok_ole_success(hr, CoGetPSClsid);
54 hr = CoGetPSClsid(&IID_IWineTest, &clsid);
55 ok(hr == REGDB_E_IIDNOTREG,
56 "CoGetPSClsid for random IID returned 0x%08lx instead of REGDB_E_IIDNOTREG\n",
60 static const LARGE_INTEGER ullZero;
63 static void LockModule()
65 InterlockedIncrement(&cLocks);
68 static void UnlockModule()
70 InterlockedDecrement(&cLocks);
74 static HRESULT WINAPI Test_IUnknown_QueryInterface(
79 if (ppvObj == NULL) return E_POINTER;
81 if (IsEqualGUID(riid, &IID_IUnknown))
83 *ppvObj = (LPVOID)iface;
84 IUnknown_AddRef(iface);
92 static ULONG WINAPI Test_IUnknown_AddRef(LPUNKNOWN iface)
95 return 2; /* non-heap-based object */
98 static ULONG WINAPI Test_IUnknown_Release(LPUNKNOWN iface)
101 return 1; /* non-heap-based object */
104 static IUnknownVtbl TestUnknown_Vtbl =
106 Test_IUnknown_QueryInterface,
107 Test_IUnknown_AddRef,
108 Test_IUnknown_Release,
111 static IUnknown Test_Unknown = { &TestUnknown_Vtbl };
114 static HRESULT WINAPI Test_IClassFactory_QueryInterface(
115 LPCLASSFACTORY iface,
119 if (ppvObj == NULL) return E_POINTER;
121 if (IsEqualGUID(riid, &IID_IUnknown) ||
122 IsEqualGUID(riid, &IID_IClassFactory))
124 *ppvObj = (LPVOID)iface;
125 IClassFactory_AddRef(iface);
130 return E_NOINTERFACE;
133 static ULONG WINAPI Test_IClassFactory_AddRef(LPCLASSFACTORY iface)
136 return 2; /* non-heap-based object */
139 static ULONG WINAPI Test_IClassFactory_Release(LPCLASSFACTORY iface)
142 return 1; /* non-heap-based object */
145 static HRESULT WINAPI Test_IClassFactory_CreateInstance(
146 LPCLASSFACTORY iface,
151 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
152 return IUnknown_QueryInterface((IUnknown*)&Test_Unknown, riid, ppvObj);
155 static HRESULT WINAPI Test_IClassFactory_LockServer(
156 LPCLASSFACTORY iface,
162 static IClassFactoryVtbl TestClassFactory_Vtbl =
164 Test_IClassFactory_QueryInterface,
165 Test_IClassFactory_AddRef,
166 Test_IClassFactory_Release,
167 Test_IClassFactory_CreateInstance,
168 Test_IClassFactory_LockServer
171 static IClassFactory Test_ClassFactory = { &TestClassFactory_Vtbl };
173 #define RELEASEMARSHALDATA WM_USER
175 struct host_object_data
180 MSHLFLAGS marshal_flags;
181 HANDLE marshal_event;
182 IMessageFilter *filter;
185 static DWORD CALLBACK host_object_proc(LPVOID p)
187 struct host_object_data *data = (struct host_object_data *)p;
191 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
195 IMessageFilter * prev_filter = NULL;
196 hr = CoRegisterMessageFilter(data->filter, &prev_filter);
197 if (prev_filter) IMessageFilter_Release(prev_filter);
198 ok_ole_success(hr, CoRegisterMessageFilter);
201 hr = CoMarshalInterface(data->stream, &data->iid, data->object, MSHCTX_INPROC, NULL, data->marshal_flags);
202 ok_ole_success(hr, CoMarshalInterface);
204 /* force the message queue to be created before signaling parent thread */
205 PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
207 SetEvent(data->marshal_event);
209 while (GetMessage(&msg, NULL, 0, 0))
211 if (msg.hwnd == NULL && msg.message == RELEASEMARSHALDATA)
213 trace("releasing marshal data\n");
214 CoReleaseMarshalData(data->stream);
215 SetEvent((HANDLE)msg.lParam);
218 DispatchMessage(&msg);
221 HeapFree(GetProcessHeap(), 0, data);
228 static DWORD start_host_object2(IStream *stream, REFIID riid, IUnknown *object, MSHLFLAGS marshal_flags, IMessageFilter *filter, HANDLE *thread)
231 HANDLE marshal_event = CreateEvent(NULL, FALSE, FALSE, NULL);
232 struct host_object_data *data = (struct host_object_data *)HeapAlloc(GetProcessHeap(), 0, sizeof(*data));
234 data->stream = stream;
236 data->object = object;
237 data->marshal_flags = marshal_flags;
238 data->marshal_event = marshal_event;
239 data->filter = filter;
241 *thread = CreateThread(NULL, 0, host_object_proc, data, 0, &tid);
243 /* wait for marshaling to complete before returning */
244 WaitForSingleObject(marshal_event, INFINITE);
245 CloseHandle(marshal_event);
250 static DWORD start_host_object(IStream *stream, REFIID riid, IUnknown *object, MSHLFLAGS marshal_flags, HANDLE *thread)
252 return start_host_object2(stream, riid, object, marshal_flags, NULL, thread);
255 /* asks thread to release the marshal data because it has to be done by the
256 * same thread that marshaled the interface in the first place. */
257 static void release_host_object(DWORD tid)
259 HANDLE event = CreateEvent(NULL, FALSE, FALSE, NULL);
260 PostThreadMessage(tid, RELEASEMARSHALDATA, 0, (LPARAM)event);
261 WaitForSingleObject(event, INFINITE);
265 static void end_host_object(DWORD tid, HANDLE thread)
267 BOOL ret = PostThreadMessage(tid, WM_QUIT, 0, 0);
268 ok(ret, "PostThreadMessage failed with error %ld\n", GetLastError());
269 /* be careful of races - don't return until hosting thread has terminated */
270 WaitForSingleObject(thread, INFINITE);
274 /* tests failure case of interface not having a marshaler specified in the
276 static void test_no_marshaler()
280 static const CLSID IID_IWineTest = {
284 {0xa1, 0xa2, 0x5d, 0x5a, 0x36, 0x54, 0xd3, 0xbd}
285 }; /* 5201163f-8164-4fd0-a1a2-5d5a3654d3bd */
287 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
288 ok_ole_success(hr, CreateStreamOnHGlobal);
289 hr = CoMarshalInterface(pStream, &IID_IWineTest, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
290 ok(hr == E_NOINTERFACE, "CoMarshalInterface should have returned E_NOINTERFACE instead of 0x%08lx\n", hr);
292 IStream_Release(pStream);
295 /* tests normal marshal and then release without unmarshaling */
296 static void test_normal_marshal_and_release()
299 IStream *pStream = NULL;
303 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
304 ok_ole_success(hr, CreateStreamOnHGlobal);
305 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
306 ok_ole_success(hr, CoMarshalInterface);
308 ok_more_than_one_lock();
310 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
311 hr = CoReleaseMarshalData(pStream);
312 ok_ole_success(hr, CoReleaseMarshalData);
313 IStream_Release(pStream);
318 /* tests success case of a same-thread marshal and unmarshal */
319 static void test_normal_marshal_and_unmarshal()
322 IStream *pStream = NULL;
323 IUnknown *pProxy = NULL;
327 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
328 ok_ole_success(hr, CreateStreamOnHGlobal);
329 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
330 ok_ole_success(hr, CoMarshalInterface);
332 ok_more_than_one_lock();
334 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
335 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
336 ok_ole_success(hr, CoUnmarshalInterface);
337 IStream_Release(pStream);
339 ok_more_than_one_lock();
341 IUnknown_Release(pProxy);
346 /* tests failure case of a unmarshaling an freed object */
347 static void test_marshal_and_unmarshal_invalid()
350 IStream *pStream = NULL;
351 IClassFactory *pProxy = NULL;
358 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
359 ok_ole_success(hr, CreateStreamOnHGlobal);
360 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
362 ok_more_than_one_lock();
364 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
365 hr = CoReleaseMarshalData(pStream);
366 ok_ole_success(hr, CoReleaseMarshalData);
370 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
371 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
372 todo_wine { ok_ole_success(hr, CoUnmarshalInterface); }
378 hr = IClassFactory_CreateInstance(pProxy, NULL, &IID_IUnknown, &dummy);
379 ok(hr == RPC_E_DISCONNECTED, "Remote call should have returned RPC_E_DISCONNECTED, instead of 0x%08lx\n", hr);
381 IClassFactory_Release(pProxy);
384 IStream_Release(pStream);
386 end_host_object(tid, thread);
389 /* tests success case of an interthread marshal */
390 static void test_interthread_marshal_and_unmarshal()
393 IStream *pStream = NULL;
394 IUnknown *pProxy = NULL;
400 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
401 ok_ole_success(hr, CreateStreamOnHGlobal);
402 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
404 ok_more_than_one_lock();
406 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
407 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
408 ok_ole_success(hr, CoUnmarshalInterface);
409 IStream_Release(pStream);
411 ok_more_than_one_lock();
413 IUnknown_Release(pProxy);
417 end_host_object(tid, thread);
420 /* tests that stubs are released when the containing apartment is destroyed */
421 static void test_marshal_stub_apartment_shutdown()
424 IStream *pStream = NULL;
425 IUnknown *pProxy = NULL;
431 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
432 ok_ole_success(hr, CreateStreamOnHGlobal);
433 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
435 ok_more_than_one_lock();
437 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
438 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
439 ok_ole_success(hr, CoUnmarshalInterface);
440 IStream_Release(pStream);
442 ok_more_than_one_lock();
444 end_host_object(tid, thread);
448 IUnknown_Release(pProxy);
453 /* tests that proxies are released when the containing apartment is destroyed */
454 static void test_marshal_proxy_apartment_shutdown()
457 IStream *pStream = NULL;
458 IUnknown *pProxy = NULL;
464 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
465 ok_ole_success(hr, CreateStreamOnHGlobal);
466 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
468 ok_more_than_one_lock();
470 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
471 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
472 ok_ole_success(hr, CoUnmarshalInterface);
473 IStream_Release(pStream);
475 ok_more_than_one_lock();
481 IUnknown_Release(pProxy);
485 end_host_object(tid, thread);
487 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
490 /* tests that proxies are released when the containing mta apartment is destroyed */
491 static void test_marshal_proxy_mta_apartment_shutdown()
494 IStream *pStream = NULL;
495 IUnknown *pProxy = NULL;
500 CoInitializeEx(NULL, COINIT_MULTITHREADED);
504 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
505 ok_ole_success(hr, CreateStreamOnHGlobal);
506 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
508 ok_more_than_one_lock();
510 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
511 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
512 ok_ole_success(hr, CoUnmarshalInterface);
513 IStream_Release(pStream);
515 ok_more_than_one_lock();
521 IUnknown_Release(pProxy);
525 end_host_object(tid, thread);
527 CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
533 HANDLE marshal_event;
534 HANDLE unmarshal_event;
537 /* helper for test_proxy_used_in_wrong_thread */
538 static DWORD CALLBACK no_couninitialize_proc(LPVOID p)
540 struct ncu_params *ncu_params = (struct ncu_params *)p;
543 CoInitializeEx(NULL, COINIT_MULTITHREADED);
545 hr = CoMarshalInterface(ncu_params->stream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
546 ok_ole_success(hr, CoMarshalInterface);
548 SetEvent(ncu_params->marshal_event);
550 WaitForSingleObject(ncu_params->unmarshal_event, INFINITE);
552 /* die without calling CoUninitialize */
557 /* tests apartment that an apartment is released if the owning thread exits */
558 static void test_no_couninitialize()
561 IStream *pStream = NULL;
562 IUnknown *pProxy = NULL;
565 struct ncu_params ncu_params;
569 ncu_params.marshal_event = CreateEvent(NULL, TRUE, FALSE, NULL);
570 ncu_params.unmarshal_event = CreateEvent(NULL, TRUE, FALSE, NULL);
572 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
573 ok_ole_success(hr, CreateStreamOnHGlobal);
574 ncu_params.stream = pStream;
576 thread = CreateThread(NULL, 0, no_couninitialize_proc, &ncu_params, 0, &tid);
578 WaitForSingleObject(ncu_params.marshal_event, INFINITE);
579 ok_more_than_one_lock();
581 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
582 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
583 ok_ole_success(hr, CoUnmarshalInterface);
584 IStream_Release(pStream);
586 ok_more_than_one_lock();
588 SetEvent(ncu_params.unmarshal_event);
589 WaitForSingleObject(thread, INFINITE);
594 CloseHandle(ncu_params.marshal_event);
595 CloseHandle(ncu_params.unmarshal_event);
597 IUnknown_Release(pProxy);
602 /* tests success case of a same-thread table-weak marshal, unmarshal, unmarshal */
603 static void test_tableweak_marshal_and_unmarshal_twice()
606 IStream *pStream = NULL;
607 IUnknown *pProxy1 = NULL;
608 IUnknown *pProxy2 = NULL;
614 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
615 ok_ole_success(hr, CreateStreamOnHGlobal);
616 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_TABLEWEAK, &thread);
618 ok_more_than_one_lock();
620 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
621 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy1);
622 ok_ole_success(hr, CoUnmarshalInterface);
624 ok_more_than_one_lock();
626 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
627 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
628 ok_ole_success(hr, CoUnmarshalInterface);
630 ok_more_than_one_lock();
632 IUnknown_Release(pProxy1);
633 IUnknown_Release(pProxy2);
635 /* this line is shows the difference between weak and strong table marshaling:
636 * weak has cLocks == 0
637 * strong has cLocks > 0 */
640 end_host_object(tid, thread);
643 /* tests releasing after unmarshaling one object */
644 static void test_tableweak_marshal_releasedata1()
647 IStream *pStream = NULL;
648 IUnknown *pProxy1 = NULL;
649 IUnknown *pProxy2 = NULL;
655 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
656 ok_ole_success(hr, CreateStreamOnHGlobal);
657 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_TABLEWEAK, &thread);
659 ok_more_than_one_lock();
661 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
662 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy1);
663 ok_ole_success(hr, CoUnmarshalInterface);
665 ok_more_than_one_lock();
667 /* release the remaining reference on the object by calling
668 * CoReleaseMarshalData in the hosting thread */
669 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
670 release_host_object(tid);
672 todo_wine { ok_more_than_one_lock(); }
674 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
675 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
676 todo_wine { ok_ole_success(hr, CoUnmarshalInterface); }
677 IStream_Release(pStream);
679 todo_wine { ok_more_than_one_lock(); }
681 IUnknown_Release(pProxy1);
683 IUnknown_Release(pProxy2);
685 /* this line is shows the difference between weak and strong table marshaling:
686 * weak has cLocks == 0
687 * strong has cLocks > 0 */
690 end_host_object(tid, thread);
693 /* tests releasing after unmarshaling one object */
694 static void test_tableweak_marshal_releasedata2()
697 IStream *pStream = NULL;
698 IUnknown *pProxy = NULL;
704 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
705 ok_ole_success(hr, CreateStreamOnHGlobal);
706 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_TABLEWEAK, &thread);
708 ok_more_than_one_lock();
710 /* release the remaining reference on the object by calling
711 * CoReleaseMarshalData in the hosting thread */
712 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
713 release_host_object(tid);
720 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
721 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
722 ok(hr == CO_E_OBJNOTREG,
723 "CoUnmarshalInterface should have failed with CO_E_OBJNOTREG, but returned 0x%08lx instead\n",
725 IStream_Release(pStream);
730 end_host_object(tid, thread);
733 /* tests success case of a same-thread table-strong marshal, unmarshal, unmarshal */
734 static void test_tablestrong_marshal_and_unmarshal_twice()
737 IStream *pStream = NULL;
738 IUnknown *pProxy1 = NULL;
739 IUnknown *pProxy2 = NULL;
745 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
746 ok_ole_success(hr, CreateStreamOnHGlobal);
747 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_TABLESTRONG, &thread);
749 ok_more_than_one_lock();
751 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
752 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy1);
753 ok_ole_success(hr, CoUnmarshalInterface);
755 ok_more_than_one_lock();
757 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
758 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
759 ok_ole_success(hr, CoUnmarshalInterface);
761 ok_more_than_one_lock();
763 if (pProxy1) IUnknown_Release(pProxy1);
764 if (pProxy2) IUnknown_Release(pProxy2);
766 /* this line is shows the difference between weak and strong table marshaling:
767 * weak has cLocks == 0
768 * strong has cLocks > 0 */
769 ok_more_than_one_lock();
771 /* release the remaining reference on the object by calling
772 * CoReleaseMarshalData in the hosting thread */
773 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
774 release_host_object(tid);
775 IStream_Release(pStream);
779 end_host_object(tid, thread);
782 /* tests CoLockObjectExternal */
783 static void test_lock_object_external()
786 IStream *pStream = NULL;
790 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
791 ok_ole_success(hr, CreateStreamOnHGlobal);
792 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
793 ok_ole_success(hr, CoMarshalInterface);
795 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, TRUE, TRUE);
797 ok_more_than_one_lock();
799 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
800 hr = CoReleaseMarshalData(pStream);
801 ok_ole_success(hr, CoReleaseMarshalData);
802 IStream_Release(pStream);
804 ok_more_than_one_lock();
806 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, FALSE, TRUE);
811 /* tests disconnecting stubs */
812 static void test_disconnect_stub()
815 IStream *pStream = NULL;
819 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
820 ok_ole_success(hr, CreateStreamOnHGlobal);
821 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
822 ok_ole_success(hr, CoMarshalInterface);
824 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, TRUE, TRUE);
826 ok_more_than_one_lock();
828 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
829 hr = CoReleaseMarshalData(pStream);
830 ok_ole_success(hr, CoReleaseMarshalData);
831 IStream_Release(pStream);
833 ok_more_than_one_lock();
835 CoDisconnectObject((IUnknown*)&Test_ClassFactory, 0);
837 todo_wine { ok_no_locks(); }
840 /* tests failure case of a same-thread marshal and unmarshal twice */
841 static void test_normal_marshal_and_unmarshal_twice()
844 IStream *pStream = NULL;
845 IUnknown *pProxy1 = NULL;
846 IUnknown *pProxy2 = NULL;
850 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
851 ok_ole_success(hr, CreateStreamOnHGlobal);
852 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
853 ok_ole_success(hr, CoMarshalInterface);
855 ok_more_than_one_lock();
857 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
858 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy1);
859 ok_ole_success(hr, CoUnmarshalInterface);
861 ok_more_than_one_lock();
863 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
864 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
865 ok(hr == CO_E_OBJNOTCONNECTED,
866 "CoUnmarshalInterface should have failed with error CO_E_OBJNOTCONNECTED for double unmarshal, instead of 0x%08lx\n", hr);
868 IStream_Release(pStream);
870 ok_more_than_one_lock();
872 IUnknown_Release(pProxy1);
877 /* tests success case of marshaling and unmarshaling an HRESULT */
878 static void test_hresult_marshaling()
881 HRESULT hr_marshaled = 0;
882 IStream *pStream = NULL;
883 static const HRESULT E_DEADBEEF = 0xdeadbeef;
885 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
886 ok_ole_success(hr, CreateStreamOnHGlobal);
888 hr = CoMarshalHresult(pStream, E_DEADBEEF);
889 ok_ole_success(hr, CoMarshalHresult);
891 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
892 hr = IStream_Read(pStream, &hr_marshaled, sizeof(HRESULT), NULL);
893 ok_ole_success(hr, IStream_Read);
895 ok(hr_marshaled == E_DEADBEEF, "Didn't marshal HRESULT as expected: got value 0x%08lx instead\n", hr_marshaled);
898 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
899 hr = CoUnmarshalHresult(pStream, &hr_marshaled);
900 ok_ole_success(hr, CoUnmarshalHresult);
902 ok(hr_marshaled == E_DEADBEEF, "Didn't marshal HRESULT as expected: got value 0x%08lx instead\n", hr_marshaled);
904 IStream_Release(pStream);
908 /* helper for test_proxy_used_in_wrong_thread */
909 static DWORD CALLBACK bad_thread_proc(LPVOID p)
911 IClassFactory * cf = (IClassFactory *)p;
913 IUnknown * proxy = NULL;
915 CoInitializeEx(NULL, COINIT_MULTITHREADED);
917 hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
918 if (proxy) IUnknown_Release(proxy);
920 ok(hr == RPC_E_WRONG_THREAD,
921 "COM should have failed with RPC_E_WRONG_THREAD on using proxy from wrong apartment, but instead returned 0x%08lx\n",
930 /* tests failure case of a using a proxy in the wrong apartment */
931 static void test_proxy_used_in_wrong_thread()
934 IStream *pStream = NULL;
935 IUnknown *pProxy = NULL;
942 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
943 ok_ole_success(hr, CreateStreamOnHGlobal);
944 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &host_thread);
946 ok_more_than_one_lock();
948 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
949 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
950 ok_ole_success(hr, CoUnmarshalInterface);
951 IStream_Release(pStream);
953 ok_more_than_one_lock();
955 /* create a thread that we can misbehave in */
956 thread = CreateThread(NULL, 0, bad_thread_proc, (LPVOID)pProxy, 0, &tid2);
958 WaitForSingleObject(thread, INFINITE);
961 IUnknown_Release(pProxy);
965 end_host_object(tid, host_thread);
968 static HRESULT WINAPI MessageFilter_QueryInterface(IMessageFilter *iface, REFIID riid, void ** ppvObj)
970 if (ppvObj == NULL) return E_POINTER;
972 if (IsEqualGUID(riid, &IID_IUnknown) ||
973 IsEqualGUID(riid, &IID_IClassFactory))
975 *ppvObj = (LPVOID)iface;
976 IClassFactory_AddRef(iface);
980 return E_NOINTERFACE;
983 static ULONG WINAPI MessageFilter_AddRef(IMessageFilter *iface)
985 return 2; /* non-heap object */
988 static ULONG WINAPI MessageFilter_Release(IMessageFilter *iface)
990 return 1; /* non-heap object */
993 static DWORD WINAPI MessageFilter_HandleInComingCall(
994 IMessageFilter *iface,
996 HTASK threadIDCaller,
998 LPINTERFACEINFO lpInterfaceInfo)
1000 static int callcount = 0;
1002 trace("HandleInComingCall\n");
1006 ret = SERVERCALL_REJECTED;
1009 ret = SERVERCALL_RETRYLATER;
1012 ret = SERVERCALL_ISHANDLED;
1019 static DWORD WINAPI MessageFilter_RetryRejectedCall(
1020 IMessageFilter *iface,
1021 HTASK threadIDCallee,
1025 trace("RetryRejectedCall\n");
1029 static DWORD WINAPI MessageFilter_MessagePending(
1030 IMessageFilter *iface,
1031 HTASK threadIDCallee,
1033 DWORD dwPendingType)
1035 trace("MessagePending\n");
1036 return PENDINGMSG_WAITNOPROCESS;
1039 static IMessageFilterVtbl MessageFilter_Vtbl =
1041 MessageFilter_QueryInterface,
1042 MessageFilter_AddRef,
1043 MessageFilter_Release,
1044 MessageFilter_HandleInComingCall,
1045 MessageFilter_RetryRejectedCall,
1046 MessageFilter_MessagePending
1049 static IMessageFilter MessageFilter = { &MessageFilter_Vtbl };
1051 static void test_message_filter()
1054 IStream *pStream = NULL;
1055 IClassFactory *cf = NULL;
1057 IUnknown *proxy = NULL;
1058 IMessageFilter *prev_filter = NULL;
1063 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1064 ok_ole_success(hr, CreateStreamOnHGlobal);
1065 tid = start_host_object2(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &MessageFilter, &thread);
1067 ok_more_than_one_lock();
1069 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1070 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&cf);
1071 ok_ole_success(hr, CoUnmarshalInterface);
1072 IStream_Release(pStream);
1074 ok_more_than_one_lock();
1076 hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
1077 todo_wine { ok(hr == RPC_E_CALL_REJECTED, "Call should have returned RPC_E_CALL_REJECTED, but return 0x%08lx instead\n", hr); }
1078 if (proxy) IUnknown_Release(proxy);
1081 hr = CoRegisterMessageFilter(&MessageFilter, &prev_filter);
1082 ok_ole_success(hr, CoRegisterMessageFilter);
1083 if (prev_filter) IMessageFilter_Release(prev_filter);
1085 hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
1086 ok_ole_success(hr, IClassFactory_CreateInstance);
1088 IUnknown_Release(proxy);
1090 IClassFactory_Release(cf);
1094 end_host_object(tid, thread);
1097 /* test failure case of trying to unmarshal from bad stream */
1098 static void test_bad_marshal_stream()
1101 IStream *pStream = NULL;
1103 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1104 ok_ole_success(hr, CreateStreamOnHGlobal);
1105 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1106 ok_ole_success(hr, CoMarshalInterface);
1108 ok_more_than_one_lock();
1110 /* try to read beyond end of stream */
1111 hr = CoReleaseMarshalData(pStream);
1112 ok(hr == STG_E_READFAULT, "Should have failed with STG_E_READFAULT, but returned 0x%08lx instead\n", hr);
1114 /* now release for real */
1115 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1116 hr = CoReleaseMarshalData(pStream);
1117 ok_ole_success(hr, CoReleaseMarshalData);
1119 IStream_Release(pStream);
1122 /* tests that proxies implement certain interfaces */
1123 static void test_proxy_interfaces()
1126 IStream *pStream = NULL;
1127 IUnknown *pProxy = NULL;
1128 IUnknown *pOtherUnknown = NULL;
1134 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1135 ok_ole_success(hr, CreateStreamOnHGlobal);
1136 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
1138 ok_more_than_one_lock();
1140 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1141 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)&pProxy);
1142 ok_ole_success(hr, CoUnmarshalInterface);
1143 IStream_Release(pStream);
1145 ok_more_than_one_lock();
1147 hr = IUnknown_QueryInterface(pProxy, &IID_IUnknown, (LPVOID*)&pOtherUnknown);
1148 ok_ole_success(hr, IUnknown_QueryInterface IID_IUnknown);
1149 if (hr == S_OK) IUnknown_Release(pOtherUnknown);
1151 hr = IUnknown_QueryInterface(pProxy, &IID_IClientSecurity, (LPVOID*)&pOtherUnknown);
1152 todo_wine { ok_ole_success(hr, IUnknown_QueryInterface IID_IClientSecurity); }
1153 if (hr == S_OK) IUnknown_Release(pOtherUnknown);
1155 hr = IUnknown_QueryInterface(pProxy, &IID_IMultiQI, (LPVOID*)&pOtherUnknown);
1156 ok_ole_success(hr, IUnknown_QueryInterface IID_IMultiQI);
1157 if (hr == S_OK) IUnknown_Release(pOtherUnknown);
1159 hr = IUnknown_QueryInterface(pProxy, &IID_IMarshal, (LPVOID*)&pOtherUnknown);
1160 todo_wine { ok_ole_success(hr, IUnknown_QueryInterface IID_IMarshal); }
1161 if (hr == S_OK) IUnknown_Release(pOtherUnknown);
1163 /* IMarshal2 is also supported on NT-based systems, but is pretty much
1164 * useless as it has no more methods over IMarshal that it inherits from. */
1166 IUnknown_Release(pProxy);
1170 end_host_object(tid, thread);
1173 static void test_stubbuffer(REFIID riid)
1176 IPSFactoryBuffer *psfb;
1177 IRpcStubBuffer *stub;
1183 hr = CoGetPSClsid(riid, &clsid);
1184 ok_ole_success(hr, CoGetPSClsid);
1186 hr = CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL, &IID_IPSFactoryBuffer, (LPVOID*)&psfb);
1187 ok_ole_success(hr, CoGetClassObject);
1189 hr = IPSFactoryBuffer_CreateStub(psfb, riid, (IUnknown*)&Test_ClassFactory, &stub);
1190 ok_ole_success(hr, IPSFactoryBuffer_CreateStub);
1192 refs = IPSFactoryBuffer_Release(psfb);
1193 #if 0 /* not reliable on native. maybe it leaks references */
1194 ok(refs == 0, "Ref-count leak of %ld on IPSFactoryBuffer\n", refs);
1197 ok_more_than_one_lock();
1199 IRpcStubBuffer_Disconnect(stub);
1203 refs = IRpcStubBuffer_Release(stub);
1204 ok(refs == 0, "Ref-count leak of %ld on IRpcProxyBuffer\n", refs);
1208 /* doesn't pass with Win9x COM DLLs (even though Essential COM says it should) */
1211 static HANDLE heventShutdown;
1213 static void LockModuleOOP()
1215 InterlockedIncrement(&cLocks); /* for test purposes only */
1216 CoAddRefServerProcess();
1219 static void UnlockModuleOOP()
1221 InterlockedDecrement(&cLocks); /* for test purposes only */
1222 if (!CoReleaseServerProcess())
1223 SetEvent(heventShutdown);
1227 static HRESULT WINAPI TestOOP_IClassFactory_QueryInterface(
1228 LPCLASSFACTORY iface,
1232 if (ppvObj == NULL) return E_POINTER;
1234 if (IsEqualGUID(riid, &IID_IUnknown) ||
1235 IsEqualGUID(riid, &IID_IClassFactory))
1237 *ppvObj = (LPVOID)iface;
1238 IClassFactory_AddRef(iface);
1242 return E_NOINTERFACE;
1245 static ULONG WINAPI TestOOP_IClassFactory_AddRef(LPCLASSFACTORY iface)
1247 return 2; /* non-heap-based object */
1250 static ULONG WINAPI TestOOP_IClassFactory_Release(LPCLASSFACTORY iface)
1252 return 1; /* non-heap-based object */
1255 static HRESULT WINAPI TestOOP_IClassFactory_CreateInstance(
1256 LPCLASSFACTORY iface,
1257 LPUNKNOWN pUnkOuter,
1261 return CLASS_E_CLASSNOTAVAILABLE;
1264 static HRESULT WINAPI TestOOP_IClassFactory_LockServer(
1265 LPCLASSFACTORY iface,
1275 static IClassFactoryVtbl TestClassFactoryOOP_Vtbl =
1277 TestOOP_IClassFactory_QueryInterface,
1278 TestOOP_IClassFactory_AddRef,
1279 TestOOP_IClassFactory_Release,
1280 TestOOP_IClassFactory_CreateInstance,
1281 TestOOP_IClassFactory_LockServer
1284 static IClassFactory TestOOP_ClassFactory = { &TestClassFactoryOOP_Vtbl };
1286 /* tests functions commonly used by out of process COM servers */
1287 static void test_out_of_process_com()
1289 static const CLSID CLSID_WineOOPTest = {
1293 {0xa1, 0xa2, 0x5d, 0x5a, 0x36, 0x54, 0xd3, 0xbd}
1294 }; /* 5201163f-8164-4fd0-a1a2-5d5a3654d3bd */
1300 heventShutdown = CreateEvent(NULL, TRUE, FALSE, NULL);
1304 /* Start the object suspended */
1305 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&TestOOP_ClassFactory,
1306 CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE | REGCLS_SUSPENDED, &cookie);
1307 ok_ole_success(hr, CoRegisterClassObject);
1309 /* ... and CoGetClassObject does not find it and fails when it looks for the
1310 * class in the registry */
1311 hr = CoGetClassObject(&CLSID_WineOOPTest, CLSCTX_INPROC_SERVER,
1312 NULL, &IID_IClassFactory, (LPVOID*)&cf);
1314 ok(hr == REGDB_E_CLASSNOTREG,
1315 "CoGetClassObject should have returned REGDB_E_CLASSNOTREG instead of 0x%08lx\n", hr);
1318 /* Resume the object suspended above ... */
1319 hr = CoResumeClassObjects();
1320 ok_ole_success(hr, CoResumeClassObjects);
1322 /* ... and now it should succeed */
1323 hr = CoGetClassObject(&CLSID_WineOOPTest, CLSCTX_INPROC_SERVER,
1324 NULL, &IID_IClassFactory, (LPVOID*)&cf);
1325 ok_ole_success(hr, CoGetClassObject);
1327 /* Now check the locking is working */
1328 /* NOTE: we are accessing the class directly, not through a proxy */
1332 hr = IClassFactory_LockServer(cf, TRUE);
1333 trace("IClassFactory_LockServer returned 0x%08lx\n", hr);
1335 ok_more_than_one_lock();
1337 IClassFactory_LockServer(cf, FALSE);
1341 IClassFactory_Release(cf);
1343 /* wait for shutdown signal */
1344 ret = WaitForSingleObject(heventShutdown, 5000);
1345 todo_wine { ok(ret != WAIT_TIMEOUT, "Server didn't shut down or machine is under very heavy load\n"); }
1347 /* try to connect again after SCM has suspended registered class objects */
1348 hr = CoGetClassObject(&CLSID_WineOOPTest, CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER, NULL,
1349 &IID_IClassFactory, (LPVOID*)&cf);
1351 ok(hr == CO_E_SERVER_STOPPING,
1352 "CoGetClassObject should have returned CO_E_SERVER_STOPPING instead of 0x%08lx\n", hr);
1355 hr = CoRevokeClassObject(cookie);
1356 ok_ole_success(hr, CoRevokeClassObject);
1358 CloseHandle(heventShutdown);
1364 HMODULE hOle32 = GetModuleHandle("ole32");
1365 if (!(pCoInitializeEx = (void*)GetProcAddress(hOle32, "CoInitializeEx"))) goto no_test;
1367 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1369 /* FIXME: test CoCreateInstanceEx */
1371 /* helper function tests */
1372 test_CoGetPSClsid();
1374 /* lifecycle management and marshaling tests */
1375 test_no_marshaler();
1376 test_normal_marshal_and_release();
1377 test_normal_marshal_and_unmarshal();
1378 test_marshal_and_unmarshal_invalid();
1379 test_interthread_marshal_and_unmarshal();
1380 test_marshal_stub_apartment_shutdown();
1381 test_marshal_proxy_apartment_shutdown();
1382 test_marshal_proxy_mta_apartment_shutdown();
1383 test_no_couninitialize();
1384 test_tableweak_marshal_and_unmarshal_twice();
1385 test_tableweak_marshal_releasedata1();
1386 test_tableweak_marshal_releasedata2();
1387 test_tablestrong_marshal_and_unmarshal_twice();
1388 test_lock_object_external();
1389 test_disconnect_stub();
1390 test_normal_marshal_and_unmarshal_twice();
1391 test_hresult_marshaling();
1392 test_proxy_used_in_wrong_thread();
1393 test_message_filter();
1394 test_bad_marshal_stream();
1395 test_proxy_interfaces();
1396 test_stubbuffer(&IID_IClassFactory);
1397 /* FIXME: test GIT */
1398 /* FIXME: test COM re-entrancy */
1400 /* test_out_of_process_com(); */
1405 trace("You need DCOM95 installed to run this test\n");