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(void)
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(void)
65 InterlockedIncrement(&cLocks);
68 static void UnlockModule(void)
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 const 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 const 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 = 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(void)
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(void)
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(void)
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 unmarshaling a freed object */
347 static void test_marshal_and_unmarshal_invalid(void)
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(void)
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 success case of an interthread marshal and then marshaling the proxy */
421 static void test_proxy_marshal_and_unmarshal(void)
424 IStream *pStream = NULL;
425 IUnknown *pProxy = NULL;
426 IUnknown *pProxy2 = NULL;
432 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
433 ok_ole_success(hr, CreateStreamOnHGlobal);
434 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
436 ok_more_than_one_lock();
438 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
439 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
440 ok_ole_success(hr, CoUnmarshalInterface);
442 ok_more_than_one_lock();
444 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
445 /* marshal the proxy */
446 hr = CoMarshalInterface(pStream, &IID_IClassFactory, pProxy, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
447 ok_ole_success(hr, CoMarshalInterface);
449 ok_more_than_one_lock();
451 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
452 /* unmarshal the second proxy to the object */
453 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
454 ok_ole_success(hr, CoUnmarshalInterface);
455 IStream_Release(pStream);
457 /* now the proxies should be as follows:
458 * pProxy -> &Test_ClassFactory
459 * pProxy2 -> &Test_ClassFactory
460 * they should NOT be as follows:
461 * pProxy -> &Test_ClassFactory
463 * the above can only really be tested by looking in +ole traces
466 ok_more_than_one_lock();
468 IUnknown_Release(pProxy);
470 ok_more_than_one_lock();
472 IUnknown_Release(pProxy2);
476 end_host_object(tid, thread);
479 /* tests that stubs are released when the containing apartment is destroyed */
480 static void test_marshal_stub_apartment_shutdown(void)
483 IStream *pStream = NULL;
484 IUnknown *pProxy = NULL;
490 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
491 ok_ole_success(hr, CreateStreamOnHGlobal);
492 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
494 ok_more_than_one_lock();
496 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
497 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
498 ok_ole_success(hr, CoUnmarshalInterface);
499 IStream_Release(pStream);
501 ok_more_than_one_lock();
503 end_host_object(tid, thread);
507 IUnknown_Release(pProxy);
512 /* tests that proxies are released when the containing apartment is destroyed */
513 static void test_marshal_proxy_apartment_shutdown(void)
516 IStream *pStream = NULL;
517 IUnknown *pProxy = NULL;
523 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
524 ok_ole_success(hr, CreateStreamOnHGlobal);
525 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
527 ok_more_than_one_lock();
529 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
530 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
531 ok_ole_success(hr, CoUnmarshalInterface);
532 IStream_Release(pStream);
534 ok_more_than_one_lock();
540 IUnknown_Release(pProxy);
544 end_host_object(tid, thread);
546 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
549 /* tests that proxies are released when the containing mta apartment is destroyed */
550 static void test_marshal_proxy_mta_apartment_shutdown(void)
553 IStream *pStream = NULL;
554 IUnknown *pProxy = NULL;
559 pCoInitializeEx(NULL, COINIT_MULTITHREADED);
563 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
564 ok_ole_success(hr, CreateStreamOnHGlobal);
565 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
567 ok_more_than_one_lock();
569 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
570 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
571 ok_ole_success(hr, CoUnmarshalInterface);
572 IStream_Release(pStream);
574 ok_more_than_one_lock();
580 IUnknown_Release(pProxy);
584 end_host_object(tid, thread);
586 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
592 HANDLE marshal_event;
593 HANDLE unmarshal_event;
596 /* helper for test_no_couninitialize_server */
597 static DWORD CALLBACK no_couninitialize_server_proc(LPVOID p)
599 struct ncu_params *ncu_params = (struct ncu_params *)p;
602 pCoInitializeEx(NULL, COINIT_MULTITHREADED);
604 hr = CoMarshalInterface(ncu_params->stream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
605 ok_ole_success(hr, CoMarshalInterface);
607 SetEvent(ncu_params->marshal_event);
609 WaitForSingleObject(ncu_params->unmarshal_event, INFINITE);
611 /* die without calling CoUninitialize */
616 /* tests apartment that an apartment with a stub is released without deadlock
617 * if the owning thread exits */
618 static void test_no_couninitialize_server()
621 IStream *pStream = NULL;
622 IUnknown *pProxy = NULL;
625 struct ncu_params ncu_params;
629 ncu_params.marshal_event = CreateEvent(NULL, TRUE, FALSE, NULL);
630 ncu_params.unmarshal_event = CreateEvent(NULL, TRUE, FALSE, NULL);
632 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
633 ok_ole_success(hr, CreateStreamOnHGlobal);
634 ncu_params.stream = pStream;
636 thread = CreateThread(NULL, 0, no_couninitialize_server_proc, &ncu_params, 0, &tid);
638 WaitForSingleObject(ncu_params.marshal_event, INFINITE);
639 ok_more_than_one_lock();
641 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
642 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
643 ok_ole_success(hr, CoUnmarshalInterface);
644 IStream_Release(pStream);
646 ok_more_than_one_lock();
648 SetEvent(ncu_params.unmarshal_event);
649 WaitForSingleObject(thread, INFINITE);
654 CloseHandle(ncu_params.marshal_event);
655 CloseHandle(ncu_params.unmarshal_event);
657 IUnknown_Release(pProxy);
662 /* STA -> STA call during DLL_THREAD_DETACH */
663 static DWORD CALLBACK no_couninitialize_client_proc(LPVOID p)
665 struct ncu_params *ncu_params = (struct ncu_params *)p;
667 IUnknown *pProxy = NULL;
669 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
671 hr = CoUnmarshalInterface(ncu_params->stream, &IID_IClassFactory, (void **)&pProxy);
672 ok_ole_success(hr, CoUnmarshalInterface);
674 ok_more_than_one_lock();
676 /* die without calling CoUninitialize */
681 /* tests STA -> STA call during DLL_THREAD_DETACH doesn't deadlock */
682 static void test_no_couninitialize_client()
685 IStream *pStream = NULL;
690 struct ncu_params ncu_params;
694 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
695 ok_ole_success(hr, CreateStreamOnHGlobal);
696 ncu_params.stream = pStream;
698 /* NOTE: assumes start_host_object uses an STA to host the object, as MTAs
699 * always deadlock when called from within DllMain */
700 host_tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown *)&Test_ClassFactory, MSHLFLAGS_NORMAL, &host_thread);
701 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
703 ok_more_than_one_lock();
705 thread = CreateThread(NULL, 0, no_couninitialize_client_proc, &ncu_params, 0, &tid);
707 WaitForSingleObject(thread, INFINITE);
712 end_host_object(host_tid, host_thread);
715 /* tests success case of a same-thread table-weak marshal, unmarshal, unmarshal */
716 static void test_tableweak_marshal_and_unmarshal_twice(void)
719 IStream *pStream = NULL;
720 IUnknown *pProxy1 = NULL;
721 IUnknown *pProxy2 = NULL;
727 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
728 ok_ole_success(hr, CreateStreamOnHGlobal);
729 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_TABLEWEAK, &thread);
731 ok_more_than_one_lock();
733 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
734 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy1);
735 ok_ole_success(hr, CoUnmarshalInterface);
737 ok_more_than_one_lock();
739 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
740 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
741 ok_ole_success(hr, CoUnmarshalInterface);
743 ok_more_than_one_lock();
745 IUnknown_Release(pProxy1);
746 IUnknown_Release(pProxy2);
748 /* this line is shows the difference between weak and strong table marshaling:
749 * weak has cLocks == 0
750 * strong has cLocks > 0 */
753 end_host_object(tid, thread);
756 /* tests releasing after unmarshaling one object */
757 static void test_tableweak_marshal_releasedata1(void)
760 IStream *pStream = NULL;
761 IUnknown *pProxy1 = NULL;
762 IUnknown *pProxy2 = NULL;
768 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
769 ok_ole_success(hr, CreateStreamOnHGlobal);
770 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_TABLEWEAK, &thread);
772 ok_more_than_one_lock();
774 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
775 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy1);
776 ok_ole_success(hr, CoUnmarshalInterface);
778 ok_more_than_one_lock();
780 /* release the remaining reference on the object by calling
781 * CoReleaseMarshalData in the hosting thread */
782 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
783 release_host_object(tid);
785 ok_more_than_one_lock();
787 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
788 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
789 ok_ole_success(hr, CoUnmarshalInterface);
790 IStream_Release(pStream);
792 ok_more_than_one_lock();
794 IUnknown_Release(pProxy1);
796 IUnknown_Release(pProxy2);
798 /* this line is shows the difference between weak and strong table marshaling:
799 * weak has cLocks == 0
800 * strong has cLocks > 0 */
803 end_host_object(tid, thread);
806 /* tests releasing after unmarshaling one object */
807 static void test_tableweak_marshal_releasedata2(void)
810 IStream *pStream = NULL;
811 IUnknown *pProxy = NULL;
817 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
818 ok_ole_success(hr, CreateStreamOnHGlobal);
819 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_TABLEWEAK, &thread);
821 ok_more_than_one_lock();
823 /* release the remaining reference on the object by calling
824 * CoReleaseMarshalData in the hosting thread */
825 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
826 release_host_object(tid);
830 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
831 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
834 ok(hr == CO_E_OBJNOTREG,
835 "CoUnmarshalInterface should have failed with CO_E_OBJNOTREG, but returned 0x%08lx instead\n",
838 IStream_Release(pStream);
842 end_host_object(tid, thread);
845 /* tests success case of a same-thread table-strong marshal, unmarshal, unmarshal */
846 static void test_tablestrong_marshal_and_unmarshal_twice(void)
849 IStream *pStream = NULL;
850 IUnknown *pProxy1 = NULL;
851 IUnknown *pProxy2 = NULL;
857 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
858 ok_ole_success(hr, CreateStreamOnHGlobal);
859 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_TABLESTRONG, &thread);
861 ok_more_than_one_lock();
863 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
864 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy1);
865 ok_ole_success(hr, CoUnmarshalInterface);
867 ok_more_than_one_lock();
869 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
870 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
871 ok_ole_success(hr, CoUnmarshalInterface);
873 ok_more_than_one_lock();
875 if (pProxy1) IUnknown_Release(pProxy1);
876 if (pProxy2) IUnknown_Release(pProxy2);
878 /* this line is shows the difference between weak and strong table marshaling:
879 * weak has cLocks == 0
880 * strong has cLocks > 0 */
881 ok_more_than_one_lock();
883 /* release the remaining reference on the object by calling
884 * CoReleaseMarshalData in the hosting thread */
885 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
886 release_host_object(tid);
887 IStream_Release(pStream);
891 end_host_object(tid, thread);
894 /* tests CoLockObjectExternal */
895 static void test_lock_object_external(void)
898 IStream *pStream = NULL;
902 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
903 ok_ole_success(hr, CreateStreamOnHGlobal);
904 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
905 ok_ole_success(hr, CoMarshalInterface);
907 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, TRUE, TRUE);
909 ok_more_than_one_lock();
911 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
912 hr = CoReleaseMarshalData(pStream);
913 ok_ole_success(hr, CoReleaseMarshalData);
914 IStream_Release(pStream);
916 ok_more_than_one_lock();
918 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, FALSE, TRUE);
923 /* tests disconnecting stubs */
924 static void test_disconnect_stub(void)
927 IStream *pStream = NULL;
931 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
932 ok_ole_success(hr, CreateStreamOnHGlobal);
933 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
934 ok_ole_success(hr, CoMarshalInterface);
936 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, TRUE, TRUE);
938 ok_more_than_one_lock();
940 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
941 hr = CoReleaseMarshalData(pStream);
942 ok_ole_success(hr, CoReleaseMarshalData);
943 IStream_Release(pStream);
945 ok_more_than_one_lock();
947 CoDisconnectObject((IUnknown*)&Test_ClassFactory, 0);
952 /* tests failure case of a same-thread marshal and unmarshal twice */
953 static void test_normal_marshal_and_unmarshal_twice(void)
956 IStream *pStream = NULL;
957 IUnknown *pProxy1 = NULL;
958 IUnknown *pProxy2 = NULL;
962 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
963 ok_ole_success(hr, CreateStreamOnHGlobal);
964 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
965 ok_ole_success(hr, CoMarshalInterface);
967 ok_more_than_one_lock();
969 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
970 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy1);
971 ok_ole_success(hr, CoUnmarshalInterface);
973 ok_more_than_one_lock();
975 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
976 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
977 ok(hr == CO_E_OBJNOTCONNECTED,
978 "CoUnmarshalInterface should have failed with error CO_E_OBJNOTCONNECTED for double unmarshal, instead of 0x%08lx\n", hr);
980 IStream_Release(pStream);
982 ok_more_than_one_lock();
984 IUnknown_Release(pProxy1);
989 /* tests success case of marshaling and unmarshaling an HRESULT */
990 static void test_hresult_marshaling(void)
993 HRESULT hr_marshaled = 0;
994 IStream *pStream = NULL;
995 static const HRESULT E_DEADBEEF = 0xdeadbeef;
997 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
998 ok_ole_success(hr, CreateStreamOnHGlobal);
1000 hr = CoMarshalHresult(pStream, E_DEADBEEF);
1001 ok_ole_success(hr, CoMarshalHresult);
1003 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1004 hr = IStream_Read(pStream, &hr_marshaled, sizeof(HRESULT), NULL);
1005 ok_ole_success(hr, IStream_Read);
1007 ok(hr_marshaled == E_DEADBEEF, "Didn't marshal HRESULT as expected: got value 0x%08lx instead\n", hr_marshaled);
1010 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1011 hr = CoUnmarshalHresult(pStream, &hr_marshaled);
1012 ok_ole_success(hr, CoUnmarshalHresult);
1014 ok(hr_marshaled == E_DEADBEEF, "Didn't marshal HRESULT as expected: got value 0x%08lx instead\n", hr_marshaled);
1016 IStream_Release(pStream);
1020 /* helper for test_proxy_used_in_wrong_thread */
1021 static DWORD CALLBACK bad_thread_proc(LPVOID p)
1023 IClassFactory * cf = (IClassFactory *)p;
1025 IUnknown * proxy = NULL;
1027 pCoInitializeEx(NULL, COINIT_MULTITHREADED);
1029 hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
1030 if (proxy) IUnknown_Release(proxy);
1032 ok(hr == RPC_E_WRONG_THREAD,
1033 "COM should have failed with RPC_E_WRONG_THREAD on using proxy from wrong apartment, but instead returned 0x%08lx\n",
1042 /* tests failure case of a using a proxy in the wrong apartment */
1043 static void test_proxy_used_in_wrong_thread(void)
1046 IStream *pStream = NULL;
1047 IUnknown *pProxy = NULL;
1054 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1055 ok_ole_success(hr, CreateStreamOnHGlobal);
1056 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &host_thread);
1058 ok_more_than_one_lock();
1060 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1061 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
1062 ok_ole_success(hr, CoUnmarshalInterface);
1063 IStream_Release(pStream);
1065 ok_more_than_one_lock();
1067 /* create a thread that we can misbehave in */
1068 thread = CreateThread(NULL, 0, bad_thread_proc, (LPVOID)pProxy, 0, &tid2);
1070 WaitForSingleObject(thread, INFINITE);
1071 CloseHandle(thread);
1073 IUnknown_Release(pProxy);
1077 end_host_object(tid, host_thread);
1080 static HRESULT WINAPI MessageFilter_QueryInterface(IMessageFilter *iface, REFIID riid, void ** ppvObj)
1082 if (ppvObj == NULL) return E_POINTER;
1084 if (IsEqualGUID(riid, &IID_IUnknown) ||
1085 IsEqualGUID(riid, &IID_IClassFactory))
1087 *ppvObj = (LPVOID)iface;
1088 IClassFactory_AddRef(iface);
1092 return E_NOINTERFACE;
1095 static ULONG WINAPI MessageFilter_AddRef(IMessageFilter *iface)
1097 return 2; /* non-heap object */
1100 static ULONG WINAPI MessageFilter_Release(IMessageFilter *iface)
1102 return 1; /* non-heap object */
1105 static DWORD WINAPI MessageFilter_HandleInComingCall(
1106 IMessageFilter *iface,
1108 HTASK threadIDCaller,
1110 LPINTERFACEINFO lpInterfaceInfo)
1112 static int callcount = 0;
1114 trace("HandleInComingCall\n");
1118 ret = SERVERCALL_REJECTED;
1121 ret = SERVERCALL_RETRYLATER;
1124 ret = SERVERCALL_ISHANDLED;
1131 static DWORD WINAPI MessageFilter_RetryRejectedCall(
1132 IMessageFilter *iface,
1133 HTASK threadIDCallee,
1137 trace("RetryRejectedCall\n");
1141 static DWORD WINAPI MessageFilter_MessagePending(
1142 IMessageFilter *iface,
1143 HTASK threadIDCallee,
1145 DWORD dwPendingType)
1147 trace("MessagePending\n");
1148 return PENDINGMSG_WAITNOPROCESS;
1151 static const IMessageFilterVtbl MessageFilter_Vtbl =
1153 MessageFilter_QueryInterface,
1154 MessageFilter_AddRef,
1155 MessageFilter_Release,
1156 MessageFilter_HandleInComingCall,
1157 MessageFilter_RetryRejectedCall,
1158 MessageFilter_MessagePending
1161 static IMessageFilter MessageFilter = { &MessageFilter_Vtbl };
1163 static void test_message_filter(void)
1166 IStream *pStream = NULL;
1167 IClassFactory *cf = NULL;
1169 IUnknown *proxy = NULL;
1170 IMessageFilter *prev_filter = NULL;
1175 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1176 ok_ole_success(hr, CreateStreamOnHGlobal);
1177 tid = start_host_object2(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &MessageFilter, &thread);
1179 ok_more_than_one_lock();
1181 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1182 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&cf);
1183 ok_ole_success(hr, CoUnmarshalInterface);
1184 IStream_Release(pStream);
1186 ok_more_than_one_lock();
1188 hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
1189 todo_wine { ok(hr == RPC_E_CALL_REJECTED, "Call should have returned RPC_E_CALL_REJECTED, but return 0x%08lx instead\n", hr); }
1190 if (proxy) IUnknown_Release(proxy);
1193 hr = CoRegisterMessageFilter(&MessageFilter, &prev_filter);
1194 ok_ole_success(hr, CoRegisterMessageFilter);
1195 if (prev_filter) IMessageFilter_Release(prev_filter);
1197 hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
1198 ok_ole_success(hr, IClassFactory_CreateInstance);
1200 IUnknown_Release(proxy);
1202 IClassFactory_Release(cf);
1206 end_host_object(tid, thread);
1209 /* test failure case of trying to unmarshal from bad stream */
1210 static void test_bad_marshal_stream(void)
1213 IStream *pStream = NULL;
1215 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1216 ok_ole_success(hr, CreateStreamOnHGlobal);
1217 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1218 ok_ole_success(hr, CoMarshalInterface);
1220 ok_more_than_one_lock();
1222 /* try to read beyond end of stream */
1223 hr = CoReleaseMarshalData(pStream);
1224 ok(hr == STG_E_READFAULT, "Should have failed with STG_E_READFAULT, but returned 0x%08lx instead\n", hr);
1226 /* now release for real */
1227 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1228 hr = CoReleaseMarshalData(pStream);
1229 ok_ole_success(hr, CoReleaseMarshalData);
1231 IStream_Release(pStream);
1234 /* tests that proxies implement certain interfaces */
1235 static void test_proxy_interfaces(void)
1238 IStream *pStream = NULL;
1239 IUnknown *pProxy = NULL;
1240 IUnknown *pOtherUnknown = NULL;
1246 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1247 ok_ole_success(hr, CreateStreamOnHGlobal);
1248 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
1250 ok_more_than_one_lock();
1252 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1253 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)&pProxy);
1254 ok_ole_success(hr, CoUnmarshalInterface);
1255 IStream_Release(pStream);
1257 ok_more_than_one_lock();
1259 hr = IUnknown_QueryInterface(pProxy, &IID_IUnknown, (LPVOID*)&pOtherUnknown);
1260 ok_ole_success(hr, IUnknown_QueryInterface IID_IUnknown);
1261 if (hr == S_OK) IUnknown_Release(pOtherUnknown);
1263 hr = IUnknown_QueryInterface(pProxy, &IID_IClientSecurity, (LPVOID*)&pOtherUnknown);
1264 todo_wine { ok_ole_success(hr, IUnknown_QueryInterface IID_IClientSecurity); }
1265 if (hr == S_OK) IUnknown_Release(pOtherUnknown);
1267 hr = IUnknown_QueryInterface(pProxy, &IID_IMultiQI, (LPVOID*)&pOtherUnknown);
1268 ok_ole_success(hr, IUnknown_QueryInterface IID_IMultiQI);
1269 if (hr == S_OK) IUnknown_Release(pOtherUnknown);
1271 hr = IUnknown_QueryInterface(pProxy, &IID_IMarshal, (LPVOID*)&pOtherUnknown);
1272 ok_ole_success(hr, IUnknown_QueryInterface IID_IMarshal);
1273 if (hr == S_OK) IUnknown_Release(pOtherUnknown);
1275 /* IMarshal2 is also supported on NT-based systems, but is pretty much
1276 * useless as it has no more methods over IMarshal that it inherits from. */
1278 IUnknown_Release(pProxy);
1282 end_host_object(tid, thread);
1287 const IUnknownVtbl *lpVtbl;
1291 static HRESULT WINAPI HeapUnknown_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
1293 if (IsEqualIID(riid, &IID_IUnknown))
1295 IUnknown_AddRef(iface);
1296 *ppv = (LPVOID)iface;
1300 return E_NOINTERFACE;
1303 static ULONG WINAPI HeapUnknown_AddRef(IUnknown *iface)
1305 HeapUnknown *This = (HeapUnknown *)iface;
1306 trace("HeapUnknown_AddRef(%p)\n", iface);
1307 return InterlockedIncrement((LONG*)&This->refs);
1310 static ULONG WINAPI HeapUnknown_Release(IUnknown *iface)
1312 HeapUnknown *This = (HeapUnknown *)iface;
1313 ULONG refs = InterlockedDecrement((LONG*)&This->refs);
1314 trace("HeapUnknown_Release(%p)\n", iface);
1315 if (!refs) HeapFree(GetProcessHeap(), 0, This);
1319 static const IUnknownVtbl HeapUnknown_Vtbl =
1321 HeapUnknown_QueryInterface,
1326 static void test_proxybuffer(REFIID riid)
1329 IPSFactoryBuffer *psfb;
1330 IRpcProxyBuffer *proxy;
1334 HeapUnknown *pUnkOuter = (HeapUnknown *)HeapAlloc(GetProcessHeap(), 0, sizeof(*pUnkOuter));
1336 pUnkOuter->lpVtbl = &HeapUnknown_Vtbl;
1337 pUnkOuter->refs = 1;
1339 hr = CoGetPSClsid(riid, &clsid);
1340 ok_ole_success(hr, CoGetPSClsid);
1342 hr = CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL, &IID_IPSFactoryBuffer, (LPVOID*)&psfb);
1343 ok_ole_success(hr, CoGetClassObject);
1345 hr = IPSFactoryBuffer_CreateProxy(psfb, (IUnknown*)pUnkOuter, riid, &proxy, &lpvtbl);
1346 ok_ole_success(hr, IPSFactoryBuffer_CreateProxy);
1347 ok(lpvtbl != NULL, "IPSFactoryBuffer_CreateProxy succeeded, but returned a NULL vtable!\n");
1349 refs = IPSFactoryBuffer_Release(psfb);
1350 #if 0 /* not reliable on native. maybe it leaks references! */
1351 ok(refs == 0, "Ref-count leak of %ld on IPSFactoryBuffer\n", refs);
1354 refs = IUnknown_Release((IUnknown *)lpvtbl);
1355 ok(refs == 1, "Ref-count leak of %ld on IRpcProxyBuffer\n", refs-1);
1357 refs = IRpcProxyBuffer_Release(proxy);
1358 ok(refs == 0, "Ref-count leak of %ld on IRpcProxyBuffer\n", refs);
1361 static void test_stubbuffer(REFIID riid)
1364 IPSFactoryBuffer *psfb;
1365 IRpcStubBuffer *stub;
1371 hr = CoGetPSClsid(riid, &clsid);
1372 ok_ole_success(hr, CoGetPSClsid);
1374 hr = CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL, &IID_IPSFactoryBuffer, (LPVOID*)&psfb);
1375 ok_ole_success(hr, CoGetClassObject);
1377 hr = IPSFactoryBuffer_CreateStub(psfb, riid, (IUnknown*)&Test_ClassFactory, &stub);
1378 ok_ole_success(hr, IPSFactoryBuffer_CreateStub);
1380 refs = IPSFactoryBuffer_Release(psfb);
1381 #if 0 /* not reliable on native. maybe it leaks references */
1382 ok(refs == 0, "Ref-count leak of %ld on IPSFactoryBuffer\n", refs);
1385 ok_more_than_one_lock();
1387 IRpcStubBuffer_Disconnect(stub);
1391 refs = IRpcStubBuffer_Release(stub);
1392 ok(refs == 0, "Ref-count leak of %ld on IRpcProxyBuffer\n", refs);
1395 static HWND hwnd_app;
1397 static HRESULT WINAPI TestRE_IClassFactory_CreateInstance(
1398 LPCLASSFACTORY iface,
1399 LPUNKNOWN pUnkOuter,
1404 BOOL ret = SendMessageTimeout(hwnd_app, WM_NULL, 0, 0, SMTO_BLOCK, 5000, &res);
1405 ok(ret, "Timed out sending a message to originating window during RPC call\n");
1409 static const IClassFactoryVtbl TestREClassFactory_Vtbl =
1411 Test_IClassFactory_QueryInterface,
1412 Test_IClassFactory_AddRef,
1413 Test_IClassFactory_Release,
1414 TestRE_IClassFactory_CreateInstance,
1415 Test_IClassFactory_LockServer
1418 IClassFactory TestRE_ClassFactory = { &TestREClassFactory_Vtbl };
1420 static LRESULT CALLBACK window_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1427 IStream *pStream = NULL;
1428 IClassFactory *proxy = NULL;
1435 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1436 ok_ole_success(hr, CreateStreamOnHGlobal);
1437 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&TestRE_ClassFactory, MSHLFLAGS_NORMAL, &thread);
1439 ok_more_than_one_lock();
1441 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1442 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&proxy);
1443 ok_ole_success(hr, CoReleaseMarshalData);
1444 IStream_Release(pStream);
1446 ok_more_than_one_lock();
1448 hr = IClassFactory_CreateInstance(proxy, NULL, &IID_IUnknown, (void **)&object);
1450 IClassFactory_Release(proxy);
1454 end_host_object(tid, thread);
1456 PostMessage(hwnd, WM_QUIT, 0, 0);
1461 return DefWindowProc(hwnd, msg, wparam, lparam);
1465 static void test_message_reentrancy(void)
1470 memset(&wndclass, 0, sizeof(wndclass));
1471 wndclass.lpfnWndProc = window_proc;
1472 wndclass.lpszClassName = "WineCOMTest";
1473 RegisterClass(&wndclass);
1475 hwnd_app = CreateWindow("WineCOMTest", NULL, 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, NULL, 0);
1476 ok(hwnd_app != NULL, "Window creation failed\n");
1478 PostMessage(hwnd_app, WM_USER, 0, 0);
1480 while (GetMessage(&msg, NULL, 0, 0))
1482 TranslateMessage(&msg);
1483 DispatchMessage(&msg);
1487 /* doesn't pass with Win9x COM DLLs (even though Essential COM says it should) */
1490 static HANDLE heventShutdown;
1492 static void LockModuleOOP()
1494 InterlockedIncrement(&cLocks); /* for test purposes only */
1495 CoAddRefServerProcess();
1498 static void UnlockModuleOOP()
1500 InterlockedDecrement(&cLocks); /* for test purposes only */
1501 if (!CoReleaseServerProcess())
1502 SetEvent(heventShutdown);
1505 static HWND hwnd_app;
1507 static HRESULT WINAPI TestOOP_IClassFactory_QueryInterface(
1508 LPCLASSFACTORY iface,
1512 if (ppvObj == NULL) return E_POINTER;
1514 if (IsEqualGUID(riid, &IID_IUnknown) ||
1515 IsEqualGUID(riid, &IID_IClassFactory))
1517 *ppvObj = (LPVOID)iface;
1518 IClassFactory_AddRef(iface);
1522 return E_NOINTERFACE;
1525 static ULONG WINAPI TestOOP_IClassFactory_AddRef(LPCLASSFACTORY iface)
1527 return 2; /* non-heap-based object */
1530 static ULONG WINAPI TestOOP_IClassFactory_Release(LPCLASSFACTORY iface)
1532 return 1; /* non-heap-based object */
1535 static HRESULT WINAPI TestOOP_IClassFactory_CreateInstance(
1536 LPCLASSFACTORY iface,
1537 LPUNKNOWN pUnkOuter,
1541 return CLASS_E_CLASSNOTAVAILABLE;
1544 static HRESULT WINAPI TestOOP_IClassFactory_LockServer(
1545 LPCLASSFACTORY iface,
1555 static const IClassFactoryVtbl TestClassFactoryOOP_Vtbl =
1557 TestOOP_IClassFactory_QueryInterface,
1558 TestOOP_IClassFactory_AddRef,
1559 TestOOP_IClassFactory_Release,
1560 TestOOP_IClassFactory_CreateInstance,
1561 TestOOP_IClassFactory_LockServer
1564 static IClassFactory TestOOP_ClassFactory = { &TestClassFactoryOOP_Vtbl };
1566 /* tests functions commonly used by out of process COM servers */
1567 static void test_out_of_process_com()
1569 static const CLSID CLSID_WineOOPTest = {
1573 {0xa1, 0xa2, 0x5d, 0x5a, 0x36, 0x54, 0xd3, 0xbd}
1574 }; /* 5201163f-8164-4fd0-a1a2-5d5a3654d3bd */
1580 heventShutdown = CreateEvent(NULL, TRUE, FALSE, NULL);
1584 /* Start the object suspended */
1585 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&TestOOP_ClassFactory,
1586 CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE | REGCLS_SUSPENDED, &cookie);
1587 ok_ole_success(hr, CoRegisterClassObject);
1589 /* ... and CoGetClassObject does not find it and fails when it looks for the
1590 * class in the registry */
1591 hr = CoGetClassObject(&CLSID_WineOOPTest, CLSCTX_INPROC_SERVER,
1592 NULL, &IID_IClassFactory, (LPVOID*)&cf);
1594 ok(hr == REGDB_E_CLASSNOTREG,
1595 "CoGetClassObject should have returned REGDB_E_CLASSNOTREG instead of 0x%08lx\n", hr);
1598 /* Resume the object suspended above ... */
1599 hr = CoResumeClassObjects();
1600 ok_ole_success(hr, CoResumeClassObjects);
1602 /* ... and now it should succeed */
1603 hr = CoGetClassObject(&CLSID_WineOOPTest, CLSCTX_INPROC_SERVER,
1604 NULL, &IID_IClassFactory, (LPVOID*)&cf);
1605 ok_ole_success(hr, CoGetClassObject);
1607 /* Now check the locking is working */
1608 /* NOTE: we are accessing the class directly, not through a proxy */
1612 hr = IClassFactory_LockServer(cf, TRUE);
1613 trace("IClassFactory_LockServer returned 0x%08lx\n", hr);
1615 ok_more_than_one_lock();
1617 IClassFactory_LockServer(cf, FALSE);
1621 IClassFactory_Release(cf);
1623 /* wait for shutdown signal */
1624 ret = WaitForSingleObject(heventShutdown, 5000);
1625 todo_wine { ok(ret != WAIT_TIMEOUT, "Server didn't shut down or machine is under very heavy load\n"); }
1627 /* try to connect again after SCM has suspended registered class objects */
1628 hr = CoGetClassObject(&CLSID_WineOOPTest, CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER, NULL,
1629 &IID_IClassFactory, (LPVOID*)&cf);
1631 ok(hr == CO_E_SERVER_STOPPING,
1632 "CoGetClassObject should have returned CO_E_SERVER_STOPPING instead of 0x%08lx\n", hr);
1635 hr = CoRevokeClassObject(cookie);
1636 ok_ole_success(hr, CoRevokeClassObject);
1638 CloseHandle(heventShutdown);
1642 static void test_ROT(void)
1644 static const WCHAR wszFileName[] = {'B','E','2','0','E','2','F','5','-',
1645 '1','9','0','3','-','4','A','A','E','-','B','1','A','F','-',
1646 '2','0','4','6','E','5','8','6','C','9','2','5',0};
1648 IMoniker *pMoniker = NULL;
1649 IRunningObjectTable *pROT = NULL;
1654 hr = CreateFileMoniker(wszFileName, &pMoniker);
1655 ok_ole_success(hr, CreateClassMoniker);
1656 hr = GetRunningObjectTable(0, &pROT);
1657 ok_ole_success(hr, GetRunningObjectTable);
1658 hr = IRunningObjectTable_Register(pROT, 0, (IUnknown*)&Test_ClassFactory, pMoniker, &dwCookie);
1659 ok_ole_success(hr, IRunningObjectTable_Register);
1661 ok_more_than_one_lock();
1663 hr = IRunningObjectTable_Revoke(pROT, dwCookie);
1664 ok_ole_success(hr, IRunningObjectTable_Revoke);
1669 static const char cf_marshaled[] =
1681 static void test_marshal_CLIPFORMAT(void)
1683 unsigned char *buffer;
1685 unsigned long flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
1686 wireCLIPFORMAT wirecf;
1687 CLIPFORMAT cf = RegisterClipboardFormatA("MyFormat");
1690 size = CLIPFORMAT_UserSize(&flags, 0, &cf);
1691 ok(size == sizeof(*wirecf) + sizeof(cf_marshaled), "Size should be %d, instead of %ld\n", sizeof(*wirecf) + sizeof(cf_marshaled), size);
1693 buffer = HeapAlloc(GetProcessHeap(), 0, size);
1694 CLIPFORMAT_UserMarshal(&flags, buffer, &cf);
1695 wirecf = (wireCLIPFORMAT)buffer;
1696 ok(wirecf->fContext == WDT_REMOTE_CALL, "Context should be WDT_REMOTE_CALL instead of 0x%08lx\n", wirecf->fContext);
1697 ok(wirecf->u.dwValue == cf, "Marshaled value should be 0x%04x instead of 0x%04lx\n", cf, wirecf->u.dwValue);
1698 ok(!memcmp(wirecf+1, cf_marshaled, sizeof(cf_marshaled)), "Marshaled data differs\n");
1700 CLIPFORMAT_UserUnmarshal(&flags, buffer, &cf2);
1701 ok(cf == cf2, "Didn't unmarshal properly\n");
1702 HeapFree(GetProcessHeap(), 0, buffer);
1704 CLIPFORMAT_UserFree(&flags, &cf2);
1707 static void test_marshal_HWND(void)
1709 unsigned char *buffer;
1711 unsigned long flags = MAKELONG(MSHCTX_LOCAL, NDR_LOCAL_DATA_REPRESENTATION);
1712 HWND hwnd = GetDesktopWindow();
1716 size = HWND_UserSize(&flags, 0, &hwnd);
1717 ok(size == sizeof(*wirehwnd), "Size should be %d, instead of %ld\n", sizeof(*wirehwnd), size);
1719 buffer = HeapAlloc(GetProcessHeap(), 0, size);
1720 HWND_UserMarshal(&flags, buffer, &hwnd);
1721 wirehwnd = (wireHWND)buffer;
1722 ok(wirehwnd->fContext == WDT_INPROC_CALL, "Context should be WDT_INPROC_CALL instead of 0x%08lx\n", wirehwnd->fContext);
1723 ok(wirehwnd->u.hInproc == (LONG_PTR)hwnd, "Marshaled value should be %p instead of %p\n", hwnd, (HANDLE)wirehwnd->u.hRemote);
1725 HWND_UserUnmarshal(&flags, buffer, &hwnd2);
1726 ok(hwnd == hwnd2, "Didn't unmarshal properly\n");
1727 HeapFree(GetProcessHeap(), 0, buffer);
1729 HWND_UserFree(&flags, &hwnd2);
1732 static void test_marshal_HGLOBAL(void)
1734 unsigned char *buffer;
1736 unsigned long flags = MAKELONG(MSHCTX_LOCAL, NDR_LOCAL_DATA_REPRESENTATION);
1739 unsigned char *wirehglobal;
1743 flags = MAKELONG(MSHCTX_LOCAL, NDR_LOCAL_DATA_REPRESENTATION);
1744 size = HGLOBAL_UserSize(&flags, 0, &hglobal);
1745 /* native is poorly programmed and allocates 4 bytes more than it needs to
1746 * here - Wine doesn't have to emulate that */
1747 ok((size == 8) || (size == 12), "Size should be 12, instead of %ld\n", size);
1748 buffer = HeapAlloc(GetProcessHeap(), 0, size);
1749 HGLOBAL_UserMarshal(&flags, buffer, &hglobal);
1750 wirehglobal = buffer;
1751 ok(*(ULONG *)wirehglobal == WDT_REMOTE_CALL, "Context should be WDT_REMOTE_CALL instead of 0x%08lx\n", *(ULONG *)wirehglobal);
1752 wirehglobal += sizeof(ULONG);
1753 ok(*(ULONG *)wirehglobal == (ULONG)hglobal, "buffer+4 should be HGLOBAL\n");
1754 HGLOBAL_UserUnmarshal(&flags, buffer, &hglobal2);
1755 ok(hglobal2 == hglobal, "Didn't unmarshal properly\n");
1756 HeapFree(GetProcessHeap(), 0, buffer);
1757 HGLOBAL_UserFree(&flags, &hglobal2);
1759 hglobal = GlobalAlloc(0, 4);
1760 buffer = GlobalLock(hglobal);
1761 for (i = 0; i < 4; i++)
1763 GlobalUnlock(hglobal);
1764 flags = MAKELONG(MSHCTX_LOCAL, NDR_LOCAL_DATA_REPRESENTATION);
1765 size = HGLOBAL_UserSize(&flags, 0, &hglobal);
1766 /* native is poorly programmed and allocates 4 bytes more than it needs to
1767 * here - Wine doesn't have to emulate that */
1768 ok((size == 24) || (size == 28), "Size should be 24 or 28, instead of %ld\n", size);
1769 buffer = HeapAlloc(GetProcessHeap(), 0, size);
1770 HGLOBAL_UserMarshal(&flags, buffer, &hglobal);
1771 wirehglobal = buffer;
1772 ok(*(ULONG *)wirehglobal == WDT_REMOTE_CALL, "Context should be WDT_REMOTE_CALL instead of 0x%08lx\n", *(ULONG *)wirehglobal);
1773 wirehglobal += sizeof(ULONG);
1774 ok(*(ULONG *)wirehglobal == (ULONG)hglobal, "buffer+0x4 should be HGLOBAL\n");
1775 wirehglobal += sizeof(ULONG);
1776 ok(*(ULONG *)wirehglobal == 4, "buffer+0x8 should be size of HGLOBAL\n");
1777 wirehglobal += sizeof(ULONG);
1778 ok(*(ULONG *)wirehglobal == (ULONG)hglobal, "buffer+0xc should be HGLOBAL\n");
1779 wirehglobal += sizeof(ULONG);
1780 ok(*(ULONG *)wirehglobal == 4, "buffer+0x10 should be size of HGLOBAL\n");
1781 wirehglobal += sizeof(ULONG);
1782 for (i = 0; i < 4; i++)
1783 ok(wirehglobal[i] == i, "buffer+0x%x should be %d\n", 0x10 + i, i);
1784 HGLOBAL_UserUnmarshal(&flags, buffer, &hglobal2);
1785 ok(hglobal2 != NULL, "Didn't unmarshal properly\n");
1786 HeapFree(GetProcessHeap(), 0, buffer);
1787 HGLOBAL_UserFree(&flags, &hglobal2);
1788 GlobalFree(hglobal);
1791 static HENHMETAFILE create_emf(void)
1793 RECT rect = {0, 0, 100, 100};
1794 HDC hdc = CreateEnhMetaFile(NULL, NULL, &rect, "HENHMETAFILE Marshaling Test\0Test\0\0");
1795 ExtTextOut(hdc, 0, 0, ETO_OPAQUE, NULL, "Test String", strlen("Test String"), NULL);
1796 return CloseEnhMetaFile(hdc);
1799 static void test_marshal_HENHMETAFILE(void)
1801 unsigned char *buffer;
1803 unsigned long flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
1805 HENHMETAFILE hemf2 = NULL;
1806 unsigned char *wirehemf;
1808 hemf = create_emf();
1810 size = HENHMETAFILE_UserSize(&flags, 0, &hemf);
1811 ok(size > 20, "size should be at least 20 bytes, not %ld\n", size);
1812 buffer = HeapAlloc(GetProcessHeap(), 0, size);
1813 HENHMETAFILE_UserMarshal(&flags, buffer, &hemf);
1815 ok(*(DWORD *)wirehemf == WDT_REMOTE_CALL, "wirestgm + 0x0 should be WDT_REMOTE_CALL instead of 0x%08lx\n", *(DWORD *)wirehemf);
1816 wirehemf += sizeof(DWORD);
1817 ok(*(DWORD *)wirehemf == (DWORD)(DWORD_PTR)hemf, "wirestgm + 0x4 should be hemf instead of 0x%08lx\n", *(DWORD *)wirehemf);
1818 wirehemf += sizeof(DWORD);
1819 ok(*(DWORD *)wirehemf == (size - 0x10), "wirestgm + 0x8 should be size - 0x10 instead of 0x%08lx\n", *(DWORD *)wirehemf);
1820 wirehemf += sizeof(DWORD);
1821 ok(*(DWORD *)wirehemf == (size - 0x10), "wirestgm + 0xc should be size - 0x10 instead of 0x%08lx\n", *(DWORD *)wirehemf);
1822 wirehemf += sizeof(DWORD);
1823 ok(*(DWORD *)wirehemf == EMR_HEADER, "wirestgm + 0x10 should be EMR_HEADER instead of %ld\n", *(DWORD *)wirehemf);
1824 wirehemf += sizeof(DWORD);
1825 /* ... rest of data not tested - refer to tests for GetEnhMetaFileBits
1828 HENHMETAFILE_UserUnmarshal(&flags, buffer, &hemf2);
1829 ok(hemf2 != NULL, "HENHMETAFILE didn't unmarshal\n");
1830 HeapFree(GetProcessHeap(), 0, buffer);
1831 HENHMETAFILE_UserFree(&flags, &hemf2);
1832 DeleteEnhMetaFile(hemf);
1837 size = HENHMETAFILE_UserSize(&flags, 0, &hemf);
1838 ok(size == 8, "size should be 8 bytes, not %ld\n", size);
1839 buffer = (unsigned char *)HeapAlloc(GetProcessHeap(), 0, size);
1840 HENHMETAFILE_UserMarshal(&flags, buffer, &hemf);
1842 ok(*(DWORD *)wirehemf == WDT_REMOTE_CALL, "wirestgm + 0x0 should be WDT_REMOTE_CALL instead of 0x%08lx\n", *(DWORD *)wirehemf);
1843 wirehemf += sizeof(DWORD);
1844 ok(*(DWORD *)wirehemf == (DWORD)(DWORD_PTR)hemf, "wirestgm + 0x4 should be hemf instead of 0x%08lx\n", *(DWORD *)wirehemf);
1845 wirehemf += sizeof(DWORD);
1847 HENHMETAFILE_UserUnmarshal(&flags, buffer, &hemf2);
1848 ok(hemf2 == NULL, "NULL HENHMETAFILE didn't unmarshal\n");
1849 HeapFree(GetProcessHeap(), 0, buffer);
1850 HENHMETAFILE_UserFree(&flags, &hemf2);
1855 HMODULE hOle32 = GetModuleHandle("ole32");
1856 if (!(pCoInitializeEx = (void*)GetProcAddress(hOle32, "CoInitializeEx"))) goto no_test;
1858 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1860 /* FIXME: test CoCreateInstanceEx */
1862 /* helper function tests */
1863 test_CoGetPSClsid();
1865 /* lifecycle management and marshaling tests */
1866 test_no_marshaler();
1867 test_normal_marshal_and_release();
1868 test_normal_marshal_and_unmarshal();
1869 test_marshal_and_unmarshal_invalid();
1870 test_interthread_marshal_and_unmarshal();
1871 test_proxy_marshal_and_unmarshal();
1872 test_marshal_stub_apartment_shutdown();
1873 test_marshal_proxy_apartment_shutdown();
1874 test_marshal_proxy_mta_apartment_shutdown();
1875 test_no_couninitialize_server();
1876 test_no_couninitialize_client();
1877 test_tableweak_marshal_and_unmarshal_twice();
1878 test_tableweak_marshal_releasedata1();
1879 test_tableweak_marshal_releasedata2();
1880 test_tablestrong_marshal_and_unmarshal_twice();
1881 test_lock_object_external();
1882 test_disconnect_stub();
1883 test_normal_marshal_and_unmarshal_twice();
1884 test_hresult_marshaling();
1885 test_proxy_used_in_wrong_thread();
1886 test_message_filter();
1887 test_bad_marshal_stream();
1888 test_proxy_interfaces();
1889 test_stubbuffer(&IID_IClassFactory);
1890 test_proxybuffer(&IID_IClassFactory);
1891 test_message_reentrancy();
1893 /* test_out_of_process_com(); */
1896 /* FIXME: test GIT */
1898 test_marshal_CLIPFORMAT();
1899 test_marshal_HWND();
1900 test_marshal_HGLOBAL();
1901 test_marshal_HENHMETAFILE();
1907 trace("You need DCOM95 installed to run this test\n");