4 * Copyright 2004 Robert Shearman
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
33 #include "wine/test.h"
35 /* functions that are not present on all versions of Windows */
36 HRESULT (WINAPI * pCoInitializeEx)(LPVOID lpReserved, DWORD dwCoInit);
38 /* helper macros to make tests a bit leaner */
39 #define ok_more_than_one_lock() ok(cLocks > 0, "Number of locks should be > 0, but actually is %d\n", cLocks)
40 #define ok_no_locks() ok(cLocks == 0, "Number of locks should be 0, but actually is %d\n", cLocks)
41 #define ok_ole_success(hr, func) ok(hr == S_OK, #func " failed with error 0x%08x\n", hr)
43 static const IID IID_IWineTest =
48 {0xa1, 0xa2, 0x5d, 0x5a, 0x36, 0x54, 0xd3, 0xbd}
49 }; /* 5201163f-8164-4fd0-a1a2-5d5a3654d3bd */
51 static const IID IID_IRemUnknown =
56 {0xc0,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46}
59 #define EXTENTID_WineTest IID_IWineTest
60 #define CLSID_WineTest IID_IWineTest
62 static const CLSID CLSID_WineOOPTest =
67 {0xa1, 0xa2, 0x5d, 0x5a, 0x36, 0x54, 0xd3, 0xbd}
68 }; /* 5201163f-8164-4fd0-a1a2-5d5a3654d3bd */
70 static void test_cocreateinstance_proxy(void)
76 pCoInitializeEx(NULL, COINIT_MULTITHREADED);
78 hr = CoCreateInstance(&CLSID_ShellDesktop, NULL, CLSCTX_INPROC, &IID_IUnknown, (void **)&pProxy);
79 ok_ole_success(hr, CoCreateInstance);
80 hr = IUnknown_QueryInterface(pProxy, &IID_IMultiQI, (void **)&pMQI);
81 ok(hr == S_OK, "created object is not a proxy, so was created in the wrong apartment\n");
83 IMultiQI_Release(pMQI);
84 IUnknown_Release(pProxy);
89 static const LARGE_INTEGER ullZero;
92 static void LockModule(void)
94 InterlockedIncrement(&cLocks);
97 static void UnlockModule(void)
99 InterlockedDecrement(&cLocks);
103 static HRESULT WINAPI Test_IUnknown_QueryInterface(
108 if (ppvObj == NULL) return E_POINTER;
110 if (IsEqualGUID(riid, &IID_IUnknown))
112 *ppvObj = (LPVOID)iface;
113 IUnknown_AddRef(iface);
118 return E_NOINTERFACE;
121 static ULONG WINAPI Test_IUnknown_AddRef(LPUNKNOWN iface)
124 return 2; /* non-heap-based object */
127 static ULONG WINAPI Test_IUnknown_Release(LPUNKNOWN iface)
130 return 1; /* non-heap-based object */
133 static const IUnknownVtbl TestUnknown_Vtbl =
135 Test_IUnknown_QueryInterface,
136 Test_IUnknown_AddRef,
137 Test_IUnknown_Release,
140 static IUnknown Test_Unknown = { &TestUnknown_Vtbl };
143 static HRESULT WINAPI Test_IClassFactory_QueryInterface(
144 LPCLASSFACTORY iface,
148 if (ppvObj == NULL) return E_POINTER;
150 if (IsEqualGUID(riid, &IID_IUnknown) ||
151 IsEqualGUID(riid, &IID_IClassFactory) ||
152 /* the only other interface Wine is currently able to marshal (for testing two proxies) */
153 IsEqualGUID(riid, &IID_IRemUnknown))
155 *ppvObj = (LPVOID)iface;
156 IClassFactory_AddRef(iface);
161 return E_NOINTERFACE;
164 static ULONG WINAPI Test_IClassFactory_AddRef(LPCLASSFACTORY iface)
167 return 2; /* non-heap-based object */
170 static ULONG WINAPI Test_IClassFactory_Release(LPCLASSFACTORY iface)
173 return 1; /* non-heap-based object */
176 static HRESULT WINAPI Test_IClassFactory_CreateInstance(
177 LPCLASSFACTORY iface,
182 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
183 return IUnknown_QueryInterface((IUnknown*)&Test_Unknown, riid, ppvObj);
186 static HRESULT WINAPI Test_IClassFactory_LockServer(
187 LPCLASSFACTORY iface,
193 static const IClassFactoryVtbl TestClassFactory_Vtbl =
195 Test_IClassFactory_QueryInterface,
196 Test_IClassFactory_AddRef,
197 Test_IClassFactory_Release,
198 Test_IClassFactory_CreateInstance,
199 Test_IClassFactory_LockServer
202 static IClassFactory Test_ClassFactory = { &TestClassFactory_Vtbl };
204 #define RELEASEMARSHALDATA WM_USER
206 struct host_object_data
211 MSHLFLAGS marshal_flags;
212 HANDLE marshal_event;
213 IMessageFilter *filter;
216 static DWORD CALLBACK host_object_proc(LPVOID p)
218 struct host_object_data *data = (struct host_object_data *)p;
222 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
226 IMessageFilter * prev_filter = NULL;
227 hr = CoRegisterMessageFilter(data->filter, &prev_filter);
228 if (prev_filter) IMessageFilter_Release(prev_filter);
229 ok_ole_success(hr, CoRegisterMessageFilter);
232 hr = CoMarshalInterface(data->stream, &data->iid, data->object, MSHCTX_INPROC, NULL, data->marshal_flags);
233 ok_ole_success(hr, CoMarshalInterface);
235 /* force the message queue to be created before signaling parent thread */
236 PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
238 SetEvent(data->marshal_event);
240 while (GetMessage(&msg, NULL, 0, 0))
242 if (msg.hwnd == NULL && msg.message == RELEASEMARSHALDATA)
244 CoReleaseMarshalData(data->stream);
245 SetEvent((HANDLE)msg.lParam);
248 DispatchMessage(&msg);
251 HeapFree(GetProcessHeap(), 0, data);
258 static DWORD start_host_object2(IStream *stream, REFIID riid, IUnknown *object, MSHLFLAGS marshal_flags, IMessageFilter *filter, HANDLE *thread)
261 HANDLE marshal_event = CreateEvent(NULL, FALSE, FALSE, NULL);
262 struct host_object_data *data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data));
264 data->stream = stream;
266 data->object = object;
267 data->marshal_flags = marshal_flags;
268 data->marshal_event = marshal_event;
269 data->filter = filter;
271 *thread = CreateThread(NULL, 0, host_object_proc, data, 0, &tid);
273 /* wait for marshaling to complete before returning */
274 WaitForSingleObject(marshal_event, INFINITE);
275 CloseHandle(marshal_event);
280 static DWORD start_host_object(IStream *stream, REFIID riid, IUnknown *object, MSHLFLAGS marshal_flags, HANDLE *thread)
282 return start_host_object2(stream, riid, object, marshal_flags, NULL, thread);
285 /* asks thread to release the marshal data because it has to be done by the
286 * same thread that marshaled the interface in the first place. */
287 static void release_host_object(DWORD tid)
289 HANDLE event = CreateEvent(NULL, FALSE, FALSE, NULL);
290 PostThreadMessage(tid, RELEASEMARSHALDATA, 0, (LPARAM)event);
291 WaitForSingleObject(event, INFINITE);
295 static void end_host_object(DWORD tid, HANDLE thread)
297 BOOL ret = PostThreadMessage(tid, WM_QUIT, 0, 0);
298 ok(ret, "PostThreadMessage failed with error %d\n", GetLastError());
299 /* be careful of races - don't return until hosting thread has terminated */
300 WaitForSingleObject(thread, INFINITE);
304 /* tests failure case of interface not having a marshaler specified in the
306 static void test_no_marshaler(void)
311 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
312 ok_ole_success(hr, CreateStreamOnHGlobal);
313 hr = CoMarshalInterface(pStream, &IID_IWineTest, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
314 ok(hr == E_NOINTERFACE, "CoMarshalInterface should have returned E_NOINTERFACE instead of 0x%08x\n", hr);
316 IStream_Release(pStream);
319 /* tests normal marshal and then release without unmarshaling */
320 static void test_normal_marshal_and_release(void)
323 IStream *pStream = 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 = CoReleaseMarshalData(pStream);
336 ok_ole_success(hr, CoReleaseMarshalData);
337 IStream_Release(pStream);
342 /* tests success case of a same-thread marshal and unmarshal */
343 static void test_normal_marshal_and_unmarshal(void)
346 IStream *pStream = NULL;
347 IUnknown *pProxy = NULL;
351 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
352 ok_ole_success(hr, CreateStreamOnHGlobal);
353 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
354 ok_ole_success(hr, CoMarshalInterface);
356 ok_more_than_one_lock();
358 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
359 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
360 ok_ole_success(hr, CoUnmarshalInterface);
361 IStream_Release(pStream);
363 ok_more_than_one_lock();
365 IUnknown_Release(pProxy);
370 /* tests failure case of unmarshaling a freed object */
371 static void test_marshal_and_unmarshal_invalid(void)
374 IStream *pStream = NULL;
375 IClassFactory *pProxy = NULL;
382 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
383 ok_ole_success(hr, CreateStreamOnHGlobal);
384 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
386 ok_more_than_one_lock();
388 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
389 hr = CoReleaseMarshalData(pStream);
390 ok_ole_success(hr, CoReleaseMarshalData);
394 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
395 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
396 todo_wine { ok_ole_success(hr, CoUnmarshalInterface); }
402 hr = IClassFactory_CreateInstance(pProxy, NULL, &IID_IUnknown, &dummy);
403 ok(hr == RPC_E_DISCONNECTED, "Remote call should have returned RPC_E_DISCONNECTED, instead of 0x%08x\n", hr);
405 IClassFactory_Release(pProxy);
408 IStream_Release(pStream);
410 end_host_object(tid, thread);
413 static void test_same_apartment_unmarshal_failure(void)
418 static const LARGE_INTEGER llZero;
422 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
423 ok_ole_success(hr, CreateStreamOnHGlobal);
425 hr = CoMarshalInterface(pStream, &IID_IUnknown, (IUnknown *)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
426 ok_ole_success(hr, CoMarshalInterface);
428 ok_more_than_one_lock();
430 hr = IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
431 ok_ole_success(hr, IStream_Seek);
433 hr = CoUnmarshalInterface(pStream, &IID_IParseDisplayName, (void **)&pProxy);
434 ok(hr == E_NOINTERFACE, "CoUnmarshalInterface should have returned E_NOINTERFACE instead of 0x%08x\n", hr);
438 IStream_Release(pStream);
441 /* tests success case of an interthread marshal */
442 static void test_interthread_marshal_and_unmarshal(void)
445 IStream *pStream = NULL;
446 IUnknown *pProxy = NULL;
452 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
453 ok_ole_success(hr, CreateStreamOnHGlobal);
454 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
456 ok_more_than_one_lock();
458 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
459 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
460 ok_ole_success(hr, CoUnmarshalInterface);
461 IStream_Release(pStream);
463 ok_more_than_one_lock();
465 IUnknown_Release(pProxy);
469 end_host_object(tid, thread);
472 /* the number of external references that Wine's proxy manager normally gives
473 * out, so we can test the border case of running out of references */
474 #define NORMALEXTREFS 5
476 /* tests success case of an interthread marshal and then marshaling the proxy */
477 static void test_proxy_marshal_and_unmarshal(void)
480 IStream *pStream = NULL;
481 IUnknown *pProxy = NULL;
482 IUnknown *pProxy2 = NULL;
489 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
490 ok_ole_success(hr, CreateStreamOnHGlobal);
491 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
493 ok_more_than_one_lock();
495 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
496 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
497 ok_ole_success(hr, CoUnmarshalInterface);
499 ok_more_than_one_lock();
501 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
502 /* marshal the proxy */
503 hr = CoMarshalInterface(pStream, &IID_IClassFactory, pProxy, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
504 ok_ole_success(hr, CoMarshalInterface);
506 ok_more_than_one_lock();
508 /* marshal 5 more times to exhaust the normal external references of 5 */
509 for (i = 0; i < NORMALEXTREFS; i++)
511 hr = CoMarshalInterface(pStream, &IID_IClassFactory, pProxy, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
512 ok_ole_success(hr, CoMarshalInterface);
515 ok_more_than_one_lock();
517 /* release the original proxy to test that we successfully keep the
518 * original object alive */
519 IUnknown_Release(pProxy);
521 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
522 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
523 ok_ole_success(hr, CoUnmarshalInterface);
525 ok_more_than_one_lock();
527 IUnknown_Release(pProxy2);
529 /* unmarshal all of the proxies to check that the object stub still exists */
530 for (i = 0; i < NORMALEXTREFS; i++)
532 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
533 ok_ole_success(hr, CoUnmarshalInterface);
535 IUnknown_Release(pProxy2);
540 IStream_Release(pStream);
542 end_host_object(tid, thread);
545 /* tests success case of an interthread marshal and then marshaling the proxy
546 * using an iid that hasn't previously been unmarshaled */
547 static void test_proxy_marshal_and_unmarshal2(void)
550 IStream *pStream = NULL;
551 IUnknown *pProxy = NULL;
552 IUnknown *pProxy2 = NULL;
558 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
559 ok_ole_success(hr, CreateStreamOnHGlobal);
560 tid = start_host_object(pStream, &IID_IUnknown, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
562 ok_more_than_one_lock();
564 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
565 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)&pProxy);
566 ok_ole_success(hr, CoUnmarshalInterface);
568 ok_more_than_one_lock();
570 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
571 /* marshal the proxy */
572 hr = CoMarshalInterface(pStream, &IID_IClassFactory, pProxy, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
573 ok_ole_success(hr, CoMarshalInterface);
575 ok_more_than_one_lock();
577 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
578 /* unmarshal the second proxy to the object */
579 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
580 ok_ole_success(hr, CoUnmarshalInterface);
581 IStream_Release(pStream);
583 /* now the proxies should be as follows:
584 * pProxy -> &Test_ClassFactory
585 * pProxy2 -> &Test_ClassFactory
586 * they should NOT be as follows:
587 * pProxy -> &Test_ClassFactory
589 * the above can only really be tested by looking in +ole traces
592 ok_more_than_one_lock();
594 IUnknown_Release(pProxy);
596 ok_more_than_one_lock();
598 IUnknown_Release(pProxy2);
602 end_host_object(tid, thread);
605 /* tests success case of an interthread marshal and then table-weak-marshaling the proxy */
606 static void test_proxy_marshal_and_unmarshal_weak(void)
609 IStream *pStream = NULL;
610 IUnknown *pProxy = NULL;
611 IUnknown *pProxy2 = NULL;
617 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
618 ok_ole_success(hr, CreateStreamOnHGlobal);
619 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
621 ok_more_than_one_lock();
623 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
624 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
625 ok_ole_success(hr, CoUnmarshalInterface);
627 ok_more_than_one_lock();
629 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
630 /* marshal the proxy */
631 hr = CoMarshalInterface(pStream, &IID_IClassFactory, pProxy, MSHCTX_INPROC, NULL, MSHLFLAGS_TABLEWEAK);
632 ok_ole_success(hr, CoMarshalInterface);
634 ok_more_than_one_lock();
636 /* release the original proxy to test that we successfully keep the
637 * original object alive */
638 IUnknown_Release(pProxy);
640 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
641 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
643 ok(hr == CO_E_OBJNOTREG, "CoUnmarshalInterface should return CO_E_OBJNOTREG instead of 0x%08x\n", hr);
647 IStream_Release(pStream);
649 end_host_object(tid, thread);
652 /* tests success case of an interthread marshal and then table-strong-marshaling the proxy */
653 static void test_proxy_marshal_and_unmarshal_strong(void)
656 IStream *pStream = NULL;
657 IUnknown *pProxy = NULL;
658 IUnknown *pProxy2 = NULL;
664 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
665 ok_ole_success(hr, CreateStreamOnHGlobal);
666 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
668 ok_more_than_one_lock();
670 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
671 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
672 ok_ole_success(hr, CoUnmarshalInterface);
674 ok_more_than_one_lock();
676 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
677 /* marshal the proxy */
678 hr = CoMarshalInterface(pStream, &IID_IClassFactory, pProxy, MSHCTX_INPROC, NULL, MSHLFLAGS_TABLESTRONG);
679 ok(hr == S_OK /* WinNT */ || hr == E_INVALIDARG /* Win9x */,
680 "CoMarshalInterface should have return S_OK or E_INVALIDARG instead of 0x%08x\n", hr);
683 IUnknown_Release(pProxy);
687 ok_more_than_one_lock();
689 /* release the original proxy to test that we successfully keep the
690 * original object alive */
691 IUnknown_Release(pProxy);
693 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
694 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
695 ok_ole_success(hr, CoUnmarshalInterface);
697 ok_more_than_one_lock();
699 IUnknown_Release(pProxy2);
701 ok_more_than_one_lock();
704 IStream_Release(pStream);
706 end_host_object(tid, thread);
711 /* tests that stubs are released when the containing apartment is destroyed */
712 static void test_marshal_stub_apartment_shutdown(void)
715 IStream *pStream = NULL;
716 IUnknown *pProxy = NULL;
722 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
723 ok_ole_success(hr, CreateStreamOnHGlobal);
724 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
726 ok_more_than_one_lock();
728 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
729 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
730 ok_ole_success(hr, CoUnmarshalInterface);
731 IStream_Release(pStream);
733 ok_more_than_one_lock();
735 end_host_object(tid, thread);
739 IUnknown_Release(pProxy);
744 /* tests that proxies are released when the containing apartment is destroyed */
745 static void test_marshal_proxy_apartment_shutdown(void)
748 IStream *pStream = NULL;
749 IUnknown *pProxy = NULL;
755 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
756 ok_ole_success(hr, CreateStreamOnHGlobal);
757 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
759 ok_more_than_one_lock();
761 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
762 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
763 ok_ole_success(hr, CoUnmarshalInterface);
764 IStream_Release(pStream);
766 ok_more_than_one_lock();
772 IUnknown_Release(pProxy);
776 end_host_object(tid, thread);
778 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
781 /* tests that proxies are released when the containing mta apartment is destroyed */
782 static void test_marshal_proxy_mta_apartment_shutdown(void)
785 IStream *pStream = NULL;
786 IUnknown *pProxy = NULL;
791 pCoInitializeEx(NULL, COINIT_MULTITHREADED);
795 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
796 ok_ole_success(hr, CreateStreamOnHGlobal);
797 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
799 ok_more_than_one_lock();
801 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
802 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
803 ok_ole_success(hr, CoUnmarshalInterface);
804 IStream_Release(pStream);
806 ok_more_than_one_lock();
812 IUnknown_Release(pProxy);
816 end_host_object(tid, thread);
818 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
824 HANDLE marshal_event;
825 HANDLE unmarshal_event;
828 /* helper for test_no_couninitialize_server */
829 static DWORD CALLBACK no_couninitialize_server_proc(LPVOID p)
831 struct ncu_params *ncu_params = (struct ncu_params *)p;
834 pCoInitializeEx(NULL, COINIT_MULTITHREADED);
836 hr = CoMarshalInterface(ncu_params->stream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
837 ok_ole_success(hr, CoMarshalInterface);
839 SetEvent(ncu_params->marshal_event);
841 WaitForSingleObject(ncu_params->unmarshal_event, INFINITE);
843 /* die without calling CoUninitialize */
848 /* tests apartment that an apartment with a stub is released without deadlock
849 * if the owning thread exits */
850 static void test_no_couninitialize_server(void)
853 IStream *pStream = NULL;
854 IUnknown *pProxy = NULL;
857 struct ncu_params ncu_params;
861 ncu_params.marshal_event = CreateEvent(NULL, TRUE, FALSE, NULL);
862 ncu_params.unmarshal_event = CreateEvent(NULL, TRUE, FALSE, NULL);
864 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
865 ok_ole_success(hr, CreateStreamOnHGlobal);
866 ncu_params.stream = pStream;
868 thread = CreateThread(NULL, 0, no_couninitialize_server_proc, &ncu_params, 0, &tid);
870 WaitForSingleObject(ncu_params.marshal_event, INFINITE);
871 ok_more_than_one_lock();
873 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
874 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
875 ok_ole_success(hr, CoUnmarshalInterface);
876 IStream_Release(pStream);
878 ok_more_than_one_lock();
880 SetEvent(ncu_params.unmarshal_event);
881 WaitForSingleObject(thread, INFINITE);
886 CloseHandle(ncu_params.marshal_event);
887 CloseHandle(ncu_params.unmarshal_event);
889 IUnknown_Release(pProxy);
894 /* STA -> STA call during DLL_THREAD_DETACH */
895 static DWORD CALLBACK no_couninitialize_client_proc(LPVOID p)
897 struct ncu_params *ncu_params = (struct ncu_params *)p;
899 IUnknown *pProxy = NULL;
901 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
903 hr = CoUnmarshalInterface(ncu_params->stream, &IID_IClassFactory, (void **)&pProxy);
904 ok_ole_success(hr, CoUnmarshalInterface);
905 IStream_Release(ncu_params->stream);
907 ok_more_than_one_lock();
909 /* die without calling CoUninitialize */
914 /* tests STA -> STA call during DLL_THREAD_DETACH doesn't deadlock */
915 static void test_no_couninitialize_client(void)
918 IStream *pStream = NULL;
923 struct ncu_params ncu_params;
927 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
928 ok_ole_success(hr, CreateStreamOnHGlobal);
929 ncu_params.stream = pStream;
931 /* NOTE: assumes start_host_object uses an STA to host the object, as MTAs
932 * always deadlock when called from within DllMain */
933 host_tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown *)&Test_ClassFactory, MSHLFLAGS_NORMAL, &host_thread);
934 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
936 ok_more_than_one_lock();
938 thread = CreateThread(NULL, 0, no_couninitialize_client_proc, &ncu_params, 0, &tid);
940 WaitForSingleObject(thread, INFINITE);
945 end_host_object(host_tid, host_thread);
948 /* tests success case of a same-thread table-weak marshal, unmarshal, unmarshal */
949 static void test_tableweak_marshal_and_unmarshal_twice(void)
952 IStream *pStream = NULL;
953 IUnknown *pProxy1 = NULL;
954 IUnknown *pProxy2 = NULL;
960 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
961 ok_ole_success(hr, CreateStreamOnHGlobal);
962 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_TABLEWEAK, &thread);
964 ok_more_than_one_lock();
966 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
967 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy1);
968 ok_ole_success(hr, CoUnmarshalInterface);
970 ok_more_than_one_lock();
972 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
973 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
974 IStream_Release(pStream);
975 ok_ole_success(hr, CoUnmarshalInterface);
977 ok_more_than_one_lock();
979 IUnknown_Release(pProxy1);
980 IUnknown_Release(pProxy2);
982 /* this line is shows the difference between weak and strong table marshaling:
983 * weak has cLocks == 0
984 * strong has cLocks > 0 */
987 end_host_object(tid, thread);
990 /* tests releasing after unmarshaling one object */
991 static void test_tableweak_marshal_releasedata1(void)
994 IStream *pStream = NULL;
995 IUnknown *pProxy1 = NULL;
996 IUnknown *pProxy2 = NULL;
1002 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1003 ok_ole_success(hr, CreateStreamOnHGlobal);
1004 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_TABLEWEAK, &thread);
1006 ok_more_than_one_lock();
1008 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1009 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy1);
1010 ok_ole_success(hr, CoUnmarshalInterface);
1012 ok_more_than_one_lock();
1014 /* release the remaining reference on the object by calling
1015 * CoReleaseMarshalData in the hosting thread */
1016 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1017 release_host_object(tid);
1019 ok_more_than_one_lock();
1021 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1022 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
1023 ok_ole_success(hr, CoUnmarshalInterface);
1024 IStream_Release(pStream);
1026 ok_more_than_one_lock();
1028 IUnknown_Release(pProxy1);
1030 IUnknown_Release(pProxy2);
1032 /* this line is shows the difference between weak and strong table marshaling:
1033 * weak has cLocks == 0
1034 * strong has cLocks > 0 */
1037 end_host_object(tid, thread);
1040 /* tests releasing after unmarshaling one object */
1041 static void test_tableweak_marshal_releasedata2(void)
1044 IStream *pStream = NULL;
1045 IUnknown *pProxy = NULL;
1051 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1052 ok_ole_success(hr, CreateStreamOnHGlobal);
1053 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_TABLEWEAK, &thread);
1055 ok_more_than_one_lock();
1057 /* release the remaining reference on the object by calling
1058 * CoReleaseMarshalData in the hosting thread */
1059 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1060 release_host_object(tid);
1064 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1065 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
1068 ok(hr == CO_E_OBJNOTREG,
1069 "CoUnmarshalInterface should have failed with CO_E_OBJNOTREG, but returned 0x%08x instead\n",
1072 IStream_Release(pStream);
1076 end_host_object(tid, thread);
1079 /* tests success case of a same-thread table-strong marshal, unmarshal, unmarshal */
1080 static void test_tablestrong_marshal_and_unmarshal_twice(void)
1083 IStream *pStream = NULL;
1084 IUnknown *pProxy1 = NULL;
1085 IUnknown *pProxy2 = NULL;
1091 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1092 ok_ole_success(hr, CreateStreamOnHGlobal);
1093 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_TABLESTRONG, &thread);
1095 ok_more_than_one_lock();
1097 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1098 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy1);
1099 ok_ole_success(hr, CoUnmarshalInterface);
1101 ok_more_than_one_lock();
1103 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1104 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
1105 ok_ole_success(hr, CoUnmarshalInterface);
1107 ok_more_than_one_lock();
1109 if (pProxy1) IUnknown_Release(pProxy1);
1110 if (pProxy2) IUnknown_Release(pProxy2);
1112 /* this line is shows the difference between weak and strong table marshaling:
1113 * weak has cLocks == 0
1114 * strong has cLocks > 0 */
1115 ok_more_than_one_lock();
1117 /* release the remaining reference on the object by calling
1118 * CoReleaseMarshalData in the hosting thread */
1119 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1120 release_host_object(tid);
1121 IStream_Release(pStream);
1125 end_host_object(tid, thread);
1128 /* tests CoLockObjectExternal */
1129 static void test_lock_object_external(void)
1132 IStream *pStream = NULL;
1136 /* test the stub manager creation aspect of CoLockObjectExternal when the
1137 * object hasn't been marshaled yet */
1138 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, TRUE, TRUE);
1140 ok_more_than_one_lock();
1142 CoDisconnectObject((IUnknown*)&Test_ClassFactory, 0);
1146 /* test our empty stub manager being handled correctly in
1147 * CoMarshalInterface */
1148 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, TRUE, TRUE);
1150 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1151 ok_ole_success(hr, CreateStreamOnHGlobal);
1152 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1153 ok_ole_success(hr, CoMarshalInterface);
1155 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, TRUE, TRUE);
1157 ok_more_than_one_lock();
1159 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1160 hr = CoReleaseMarshalData(pStream);
1161 ok_ole_success(hr, CoReleaseMarshalData);
1162 IStream_Release(pStream);
1164 ok_more_than_one_lock();
1166 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, FALSE, TRUE);
1168 ok_more_than_one_lock();
1170 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, FALSE, TRUE);
1175 /* tests disconnecting stubs */
1176 static void test_disconnect_stub(void)
1179 IStream *pStream = NULL;
1183 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1184 ok_ole_success(hr, CreateStreamOnHGlobal);
1185 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1186 ok_ole_success(hr, CoMarshalInterface);
1188 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, TRUE, TRUE);
1190 ok_more_than_one_lock();
1192 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1193 hr = CoReleaseMarshalData(pStream);
1194 ok_ole_success(hr, CoReleaseMarshalData);
1195 IStream_Release(pStream);
1197 ok_more_than_one_lock();
1199 CoDisconnectObject((IUnknown*)&Test_ClassFactory, 0);
1204 /* tests failure case of a same-thread marshal and unmarshal twice */
1205 static void test_normal_marshal_and_unmarshal_twice(void)
1208 IStream *pStream = NULL;
1209 IUnknown *pProxy1 = NULL;
1210 IUnknown *pProxy2 = NULL;
1214 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1215 ok_ole_success(hr, CreateStreamOnHGlobal);
1216 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1217 ok_ole_success(hr, CoMarshalInterface);
1219 ok_more_than_one_lock();
1221 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1222 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy1);
1223 ok_ole_success(hr, CoUnmarshalInterface);
1225 ok_more_than_one_lock();
1227 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1228 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
1229 ok(hr == CO_E_OBJNOTCONNECTED,
1230 "CoUnmarshalInterface should have failed with error CO_E_OBJNOTCONNECTED for double unmarshal, instead of 0x%08x\n", hr);
1232 IStream_Release(pStream);
1234 ok_more_than_one_lock();
1236 IUnknown_Release(pProxy1);
1241 /* tests success case of marshaling and unmarshaling an HRESULT */
1242 static void test_hresult_marshaling(void)
1245 HRESULT hr_marshaled = 0;
1246 IStream *pStream = NULL;
1247 static const HRESULT E_DEADBEEF = 0xdeadbeef;
1249 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1250 ok_ole_success(hr, CreateStreamOnHGlobal);
1252 hr = CoMarshalHresult(pStream, E_DEADBEEF);
1253 ok_ole_success(hr, CoMarshalHresult);
1255 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1256 hr = IStream_Read(pStream, &hr_marshaled, sizeof(HRESULT), NULL);
1257 ok_ole_success(hr, IStream_Read);
1259 ok(hr_marshaled == E_DEADBEEF, "Didn't marshal HRESULT as expected: got value 0x%08x instead\n", hr_marshaled);
1262 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1263 hr = CoUnmarshalHresult(pStream, &hr_marshaled);
1264 ok_ole_success(hr, CoUnmarshalHresult);
1266 ok(hr_marshaled == E_DEADBEEF, "Didn't marshal HRESULT as expected: got value 0x%08x instead\n", hr_marshaled);
1268 IStream_Release(pStream);
1272 /* helper for test_proxy_used_in_wrong_thread */
1273 static DWORD CALLBACK bad_thread_proc(LPVOID p)
1275 IClassFactory * cf = (IClassFactory *)p;
1277 IUnknown * proxy = NULL;
1279 hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
1281 ok(hr == CO_E_NOTINITIALIZED,
1282 "COM should have failed with CO_E_NOTINITIALIZED on using proxy without apartment, but instead returned 0x%08x\n",
1285 hr = IClassFactory_QueryInterface(cf, &IID_IMultiQI, (LPVOID *)&proxy);
1286 /* Win9x returns S_OK, whilst NT returns RPC_E_WRONG_THREAD */
1287 trace("call to proxy's QueryInterface for local interface without apartment returned 0x%08x\n", hr);
1289 IUnknown_Release(proxy);
1291 hr = IClassFactory_QueryInterface(cf, &IID_IStream, (LPVOID *)&proxy);
1292 /* Win9x returns E_NOINTERFACE, whilst NT returns RPC_E_WRONG_THREAD */
1293 trace("call to proxy's QueryInterface without apartment returned 0x%08x\n", hr);
1295 IUnknown_Release(proxy);
1297 pCoInitializeEx(NULL, COINIT_MULTITHREADED);
1299 hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
1300 if (proxy) IUnknown_Release(proxy);
1301 ok(hr == RPC_E_WRONG_THREAD,
1302 "COM should have failed with RPC_E_WRONG_THREAD on using proxy from wrong apartment, but instead returned 0x%08x\n",
1305 hr = IClassFactory_QueryInterface(cf, &IID_IStream, (LPVOID *)&proxy);
1306 /* Win9x returns E_NOINTERFACE, whilst NT returns RPC_E_WRONG_THREAD */
1307 trace("call to proxy's QueryInterface from wrong apartment returned 0x%08x\n", hr);
1309 /* this statement causes Win9x DCOM to crash during CoUninitialize of
1310 * other apartment, so don't test this on Win9x (signified by NT-only
1311 * export of CoRegisterSurrogateEx) */
1312 if (GetProcAddress(GetModuleHandle("ole32"), "CoRegisterSurrogateEx"))
1313 /* now be really bad and release the proxy from the wrong apartment */
1314 IUnknown_Release(cf);
1316 skip("skipping test for releasing proxy from wrong apartment that will succeed, but cause a crash during CoUninitialize\n");
1323 /* tests failure case of a using a proxy in the wrong apartment */
1324 static void test_proxy_used_in_wrong_thread(void)
1327 IStream *pStream = NULL;
1328 IUnknown *pProxy = NULL;
1335 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1336 ok_ole_success(hr, CreateStreamOnHGlobal);
1337 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &host_thread);
1339 ok_more_than_one_lock();
1341 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1342 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
1343 ok_ole_success(hr, CoUnmarshalInterface);
1344 IStream_Release(pStream);
1346 ok_more_than_one_lock();
1348 /* do a call that will fail, but result in IRemUnknown being used by the proxy */
1349 IClassFactory_QueryInterface(pProxy, &IID_IStream, (LPVOID *)&pStream);
1351 /* create a thread that we can misbehave in */
1352 thread = CreateThread(NULL, 0, bad_thread_proc, (LPVOID)pProxy, 0, &tid2);
1354 WaitForSingleObject(thread, INFINITE);
1355 CloseHandle(thread);
1357 /* do release statement on Win9x that we should have done above */
1358 if (!GetProcAddress(GetModuleHandle("ole32"), "CoRegisterSurrogateEx"))
1359 IUnknown_Release(pProxy);
1363 end_host_object(tid, host_thread);
1366 static HRESULT WINAPI MessageFilter_QueryInterface(IMessageFilter *iface, REFIID riid, void ** ppvObj)
1368 if (ppvObj == NULL) return E_POINTER;
1370 if (IsEqualGUID(riid, &IID_IUnknown) ||
1371 IsEqualGUID(riid, &IID_IClassFactory))
1373 *ppvObj = (LPVOID)iface;
1374 IClassFactory_AddRef(iface);
1378 return E_NOINTERFACE;
1381 static ULONG WINAPI MessageFilter_AddRef(IMessageFilter *iface)
1383 return 2; /* non-heap object */
1386 static ULONG WINAPI MessageFilter_Release(IMessageFilter *iface)
1388 return 1; /* non-heap object */
1391 static DWORD WINAPI MessageFilter_HandleInComingCall(
1392 IMessageFilter *iface,
1394 HTASK threadIDCaller,
1396 LPINTERFACEINFO lpInterfaceInfo)
1398 static int callcount = 0;
1400 trace("HandleInComingCall\n");
1404 ret = SERVERCALL_REJECTED;
1407 ret = SERVERCALL_RETRYLATER;
1410 ret = SERVERCALL_ISHANDLED;
1417 static DWORD WINAPI MessageFilter_RetryRejectedCall(
1418 IMessageFilter *iface,
1419 HTASK threadIDCallee,
1423 trace("RetryRejectedCall\n");
1427 static DWORD WINAPI MessageFilter_MessagePending(
1428 IMessageFilter *iface,
1429 HTASK threadIDCallee,
1431 DWORD dwPendingType)
1433 trace("MessagePending\n");
1434 return PENDINGMSG_WAITNOPROCESS;
1437 static const IMessageFilterVtbl MessageFilter_Vtbl =
1439 MessageFilter_QueryInterface,
1440 MessageFilter_AddRef,
1441 MessageFilter_Release,
1442 MessageFilter_HandleInComingCall,
1443 MessageFilter_RetryRejectedCall,
1444 MessageFilter_MessagePending
1447 static IMessageFilter MessageFilter = { &MessageFilter_Vtbl };
1449 static void test_message_filter(void)
1452 IStream *pStream = NULL;
1453 IClassFactory *cf = NULL;
1455 IUnknown *proxy = NULL;
1456 IMessageFilter *prev_filter = NULL;
1461 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1462 ok_ole_success(hr, CreateStreamOnHGlobal);
1463 tid = start_host_object2(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &MessageFilter, &thread);
1465 ok_more_than_one_lock();
1467 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1468 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&cf);
1469 ok_ole_success(hr, CoUnmarshalInterface);
1470 IStream_Release(pStream);
1472 ok_more_than_one_lock();
1474 hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
1475 ok(hr == RPC_E_CALL_REJECTED, "Call should have returned RPC_E_CALL_REJECTED, but return 0x%08x instead\n", hr);
1476 if (proxy) IUnknown_Release(proxy);
1479 hr = CoRegisterMessageFilter(&MessageFilter, &prev_filter);
1480 ok_ole_success(hr, CoRegisterMessageFilter);
1482 hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
1483 ok_ole_success(hr, IClassFactory_CreateInstance);
1485 IUnknown_Release(proxy);
1487 IClassFactory_Release(cf);
1491 end_host_object(tid, thread);
1493 hr = CoRegisterMessageFilter(prev_filter, NULL);
1494 ok_ole_success(hr, CoRegisterMessageFilter);
1497 /* test failure case of trying to unmarshal from bad stream */
1498 static void test_bad_marshal_stream(void)
1501 IStream *pStream = NULL;
1503 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1504 ok_ole_success(hr, CreateStreamOnHGlobal);
1505 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1506 ok_ole_success(hr, CoMarshalInterface);
1508 ok_more_than_one_lock();
1510 /* try to read beyond end of stream */
1511 hr = CoReleaseMarshalData(pStream);
1512 ok(hr == STG_E_READFAULT, "Should have failed with STG_E_READFAULT, but returned 0x%08x instead\n", hr);
1514 /* now release for real */
1515 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1516 hr = CoReleaseMarshalData(pStream);
1517 ok_ole_success(hr, CoReleaseMarshalData);
1519 IStream_Release(pStream);
1522 /* tests that proxies implement certain interfaces */
1523 static void test_proxy_interfaces(void)
1526 IStream *pStream = NULL;
1527 IUnknown *pProxy = NULL;
1528 IUnknown *pOtherUnknown = NULL;
1534 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1535 ok_ole_success(hr, CreateStreamOnHGlobal);
1536 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
1538 ok_more_than_one_lock();
1540 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1541 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)&pProxy);
1542 ok_ole_success(hr, CoUnmarshalInterface);
1543 IStream_Release(pStream);
1545 ok_more_than_one_lock();
1547 hr = IUnknown_QueryInterface(pProxy, &IID_IUnknown, (LPVOID*)&pOtherUnknown);
1548 ok_ole_success(hr, IUnknown_QueryInterface IID_IUnknown);
1549 if (hr == S_OK) IUnknown_Release(pOtherUnknown);
1551 hr = IUnknown_QueryInterface(pProxy, &IID_IClientSecurity, (LPVOID*)&pOtherUnknown);
1552 ok_ole_success(hr, IUnknown_QueryInterface IID_IClientSecurity);
1553 if (hr == S_OK) IUnknown_Release(pOtherUnknown);
1555 hr = IUnknown_QueryInterface(pProxy, &IID_IMultiQI, (LPVOID*)&pOtherUnknown);
1556 ok_ole_success(hr, IUnknown_QueryInterface IID_IMultiQI);
1557 if (hr == S_OK) IUnknown_Release(pOtherUnknown);
1559 hr = IUnknown_QueryInterface(pProxy, &IID_IMarshal, (LPVOID*)&pOtherUnknown);
1560 ok_ole_success(hr, IUnknown_QueryInterface IID_IMarshal);
1561 if (hr == S_OK) IUnknown_Release(pOtherUnknown);
1563 /* IMarshal2 is also supported on NT-based systems, but is pretty much
1564 * useless as it has no more methods over IMarshal that it inherits from. */
1566 IUnknown_Release(pProxy);
1570 end_host_object(tid, thread);
1575 const IUnknownVtbl *lpVtbl;
1579 static HRESULT WINAPI HeapUnknown_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
1581 if (IsEqualIID(riid, &IID_IUnknown))
1583 IUnknown_AddRef(iface);
1584 *ppv = (LPVOID)iface;
1588 return E_NOINTERFACE;
1591 static ULONG WINAPI HeapUnknown_AddRef(IUnknown *iface)
1593 HeapUnknown *This = (HeapUnknown *)iface;
1594 return InterlockedIncrement((LONG*)&This->refs);
1597 static ULONG WINAPI HeapUnknown_Release(IUnknown *iface)
1599 HeapUnknown *This = (HeapUnknown *)iface;
1600 ULONG refs = InterlockedDecrement((LONG*)&This->refs);
1601 if (!refs) HeapFree(GetProcessHeap(), 0, This);
1605 static const IUnknownVtbl HeapUnknown_Vtbl =
1607 HeapUnknown_QueryInterface,
1612 static void test_proxybuffer(REFIID riid)
1615 IPSFactoryBuffer *psfb;
1616 IRpcProxyBuffer *proxy;
1620 HeapUnknown *pUnkOuter = HeapAlloc(GetProcessHeap(), 0, sizeof(*pUnkOuter));
1622 pUnkOuter->lpVtbl = &HeapUnknown_Vtbl;
1623 pUnkOuter->refs = 1;
1625 hr = CoGetPSClsid(riid, &clsid);
1626 ok_ole_success(hr, CoGetPSClsid);
1628 hr = CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL, &IID_IPSFactoryBuffer, (LPVOID*)&psfb);
1629 ok_ole_success(hr, CoGetClassObject);
1631 hr = IPSFactoryBuffer_CreateProxy(psfb, (IUnknown*)pUnkOuter, riid, &proxy, &lpvtbl);
1632 ok_ole_success(hr, IPSFactoryBuffer_CreateProxy);
1633 ok(lpvtbl != NULL, "IPSFactoryBuffer_CreateProxy succeeded, but returned a NULL vtable!\n");
1635 /* release our reference to the outer unknown object - the PS factory
1636 * buffer will have AddRef's it in the CreateProxy call */
1637 refs = IUnknown_Release((IUnknown *)pUnkOuter);
1638 ok(refs == 1, "Ref count of outer unknown should have been 1 instead of %d\n", refs);
1640 refs = IPSFactoryBuffer_Release(psfb);
1643 /* not reliable on native. maybe it leaks references! */
1644 ok(refs == 0, "Ref-count leak of %d on IPSFactoryBuffer\n", refs);
1647 refs = IUnknown_Release((IUnknown *)lpvtbl);
1648 ok(refs == 0, "Ref-count leak of %d on IRpcProxyBuffer\n", refs);
1650 refs = IRpcProxyBuffer_Release(proxy);
1651 ok(refs == 0, "Ref-count leak of %d on IRpcProxyBuffer\n", refs);
1654 static void test_stubbuffer(REFIID riid)
1657 IPSFactoryBuffer *psfb;
1658 IRpcStubBuffer *stub;
1664 hr = CoGetPSClsid(riid, &clsid);
1665 ok_ole_success(hr, CoGetPSClsid);
1667 hr = CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL, &IID_IPSFactoryBuffer, (LPVOID*)&psfb);
1668 ok_ole_success(hr, CoGetClassObject);
1670 hr = IPSFactoryBuffer_CreateStub(psfb, riid, (IUnknown*)&Test_ClassFactory, &stub);
1671 ok_ole_success(hr, IPSFactoryBuffer_CreateStub);
1673 refs = IPSFactoryBuffer_Release(psfb);
1676 /* not reliable on native. maybe it leaks references */
1677 ok(refs == 0, "Ref-count leak of %d on IPSFactoryBuffer\n", refs);
1680 ok_more_than_one_lock();
1682 IRpcStubBuffer_Disconnect(stub);
1686 refs = IRpcStubBuffer_Release(stub);
1687 ok(refs == 0, "Ref-count leak of %d on IRpcProxyBuffer\n", refs);
1690 static HWND hwnd_app;
1692 static HRESULT WINAPI TestRE_IClassFactory_CreateInstance(
1693 LPCLASSFACTORY iface,
1694 LPUNKNOWN pUnkOuter,
1699 if (IsEqualIID(riid, &IID_IWineTest))
1701 BOOL ret = SendMessageTimeout(hwnd_app, WM_NULL, 0, 0, SMTO_BLOCK, 5000, &res);
1702 ok(ret, "Timed out sending a message to originating window during RPC call\n");
1707 static const IClassFactoryVtbl TestREClassFactory_Vtbl =
1709 Test_IClassFactory_QueryInterface,
1710 Test_IClassFactory_AddRef,
1711 Test_IClassFactory_Release,
1712 TestRE_IClassFactory_CreateInstance,
1713 Test_IClassFactory_LockServer
1716 IClassFactory TestRE_ClassFactory = { &TestREClassFactory_Vtbl };
1718 static LRESULT CALLBACK window_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1725 IStream *pStream = NULL;
1726 IClassFactory *proxy = NULL;
1733 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1734 ok_ole_success(hr, CreateStreamOnHGlobal);
1735 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&TestRE_ClassFactory, MSHLFLAGS_NORMAL, &thread);
1737 ok_more_than_one_lock();
1739 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1740 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&proxy);
1741 ok_ole_success(hr, CoReleaseMarshalData);
1742 IStream_Release(pStream);
1744 ok_more_than_one_lock();
1746 /* note the use of the magic IID_IWineTest value to tell remote thread
1747 * to try to send a message back to us */
1748 hr = IClassFactory_CreateInstance(proxy, NULL, &IID_IWineTest, (void **)&object);
1750 IClassFactory_Release(proxy);
1754 end_host_object(tid, thread);
1756 PostMessage(hwnd, WM_QUIT, 0, 0);
1763 IStream *pStream = NULL;
1764 IClassFactory *proxy = NULL;
1771 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1772 ok_ole_success(hr, CreateStreamOnHGlobal);
1773 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&TestRE_ClassFactory, MSHLFLAGS_NORMAL, &thread);
1775 ok_more_than_one_lock();
1777 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1778 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&proxy);
1779 ok_ole_success(hr, CoReleaseMarshalData);
1780 IStream_Release(pStream);
1782 ok_more_than_one_lock();
1784 /* post quit message before a doing a COM call to show that a pending
1785 * WM_QUIT message doesn't stop the call from succeeding */
1786 PostMessage(hwnd, WM_QUIT, 0, 0);
1787 hr = IClassFactory_CreateInstance(proxy, NULL, &IID_IUnknown, (void **)&object);
1789 IClassFactory_Release(proxy);
1793 end_host_object(tid, thread);
1800 IStream *pStream = NULL;
1801 IClassFactory *proxy = NULL;
1806 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1807 ok_ole_success(hr, CreateStreamOnHGlobal);
1808 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
1810 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1811 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&proxy);
1812 ok_ole_success(hr, CoReleaseMarshalData);
1813 IStream_Release(pStream);
1815 /* shows that COM calls executed during the processing of sent
1816 * messages should fail */
1817 hr = IClassFactory_CreateInstance(proxy, NULL, &IID_IUnknown, (void **)&object);
1818 ok(hr == RPC_E_CANTCALLOUT_ININPUTSYNCCALL,
1819 "COM call during processing of sent message should return RPC_E_CANTCALLOUT_ININPUTSYNCCALL instead of 0x%08x\n", hr);
1821 IClassFactory_Release(proxy);
1823 end_host_object(tid, thread);
1830 return DefWindowProc(hwnd, msg, wparam, lparam);
1834 static void test_message_reentrancy(void)
1839 memset(&wndclass, 0, sizeof(wndclass));
1840 wndclass.lpfnWndProc = window_proc;
1841 wndclass.lpszClassName = "WineCOMTest";
1842 RegisterClass(&wndclass);
1844 hwnd_app = CreateWindow("WineCOMTest", NULL, 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, NULL, 0);
1845 ok(hwnd_app != NULL, "Window creation failed\n");
1847 /* start message re-entrancy test */
1848 PostMessage(hwnd_app, WM_USER, 0, 0);
1850 while (GetMessage(&msg, NULL, 0, 0))
1852 TranslateMessage(&msg);
1853 DispatchMessage(&msg);
1857 static HRESULT WINAPI TestMsg_IClassFactory_CreateInstance(
1858 LPCLASSFACTORY iface,
1859 LPUNKNOWN pUnkOuter,
1864 SendMessage(hwnd_app, WM_USER+2, 0, 0);
1868 static IClassFactoryVtbl TestMsgClassFactory_Vtbl =
1870 Test_IClassFactory_QueryInterface,
1871 Test_IClassFactory_AddRef,
1872 Test_IClassFactory_Release,
1873 TestMsg_IClassFactory_CreateInstance,
1874 Test_IClassFactory_LockServer
1877 IClassFactory TestMsg_ClassFactory = { &TestMsgClassFactory_Vtbl };
1879 static void test_call_from_message(void)
1884 IClassFactory *proxy;
1889 hwnd_app = CreateWindow("WineCOMTest", NULL, 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, NULL, 0);
1890 ok(hwnd_app != NULL, "Window creation failed\n");
1892 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1893 ok_ole_success(hr, CreateStreamOnHGlobal);
1894 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&TestMsg_ClassFactory, MSHLFLAGS_NORMAL, &thread);
1896 ok_more_than_one_lock();
1898 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1899 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&proxy);
1900 ok_ole_success(hr, CoReleaseMarshalData);
1901 IStream_Release(pStream);
1903 ok_more_than_one_lock();
1905 /* start message re-entrancy test */
1906 hr = IClassFactory_CreateInstance(proxy, NULL, &IID_IUnknown, (void **)&object);
1907 ok_ole_success(hr, IClassFactory_CreateInstance);
1909 IClassFactory_Release(proxy);
1913 end_host_object(tid, thread);
1915 while (GetMessage(&msg, NULL, 0, 0))
1917 TranslateMessage(&msg);
1918 DispatchMessage(&msg);
1922 static void test_WM_QUIT_handling(void)
1926 hwnd_app = CreateWindow("WineCOMTest", NULL, 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, NULL, 0);
1927 ok(hwnd_app != NULL, "Window creation failed\n");
1929 /* start WM_QUIT handling test */
1930 PostMessage(hwnd_app, WM_USER+1, 0, 0);
1932 while (GetMessage(&msg, NULL, 0, 0))
1934 TranslateMessage(&msg);
1935 DispatchMessage(&msg);
1939 static void test_freethreadedmarshaldata(IStream *pStream, MSHCTX mshctx, void *ptr, DWORD mshlflags)
1946 hr = GetHGlobalFromStream(pStream, &hglobal);
1947 ok_ole_success(hr, GetHGlobalFromStream);
1949 size = GlobalSize(hglobal);
1951 marshal_data = (char *)GlobalLock(hglobal);
1953 if (mshctx == MSHCTX_INPROC)
1955 DWORD expected_size = sizeof(DWORD) + sizeof(void *) + sizeof(DWORD) + sizeof(GUID);
1956 ok(size == expected_size, "size should have been %d instead of %d\n", expected_size, size);
1958 ok(*(DWORD *)marshal_data == mshlflags, "expected 0x%x, but got 0x%x for mshctx\n", mshlflags, *(DWORD *)marshal_data);
1959 marshal_data += sizeof(DWORD);
1960 ok(*(void **)marshal_data == ptr, "expected %p, but got %p for mshctx\n", ptr, *(void **)marshal_data);
1961 marshal_data += sizeof(void *);
1962 ok(*(DWORD *)marshal_data == 0, "expected 0x0, but got 0x%x\n", *(DWORD *)marshal_data);
1963 marshal_data += sizeof(DWORD);
1964 trace("got guid data: {%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\n",
1965 ((GUID *)marshal_data)->Data1, ((GUID *)marshal_data)->Data2, ((GUID *)marshal_data)->Data3,
1966 ((GUID *)marshal_data)->Data4[0], ((GUID *)marshal_data)->Data4[1], ((GUID *)marshal_data)->Data4[2], ((GUID *)marshal_data)->Data4[3],
1967 ((GUID *)marshal_data)->Data4[4], ((GUID *)marshal_data)->Data4[5], ((GUID *)marshal_data)->Data4[6], ((GUID *)marshal_data)->Data4[7]);
1971 ok(size > sizeof(DWORD), "size should have been > sizeof(DWORD), not %d\n", size);
1972 ok(*(DWORD *)marshal_data == 0x574f454d /* MEOW */,
1973 "marshal data should be filled by standard marshal and start with MEOW signature\n");
1976 GlobalUnlock(hglobal);
1979 static void test_freethreadedmarshaler(void)
1982 IUnknown *pFTUnknown;
1983 IMarshal *pFTMarshal;
1986 static const LARGE_INTEGER llZero;
1989 hr = CoCreateFreeThreadedMarshaler(NULL, &pFTUnknown);
1990 ok_ole_success(hr, CoCreateFreeThreadedMarshaler);
1991 hr = IUnknown_QueryInterface(pFTUnknown, &IID_IMarshal, (void **)&pFTMarshal);
1992 ok_ole_success(hr, IUnknown_QueryInterface);
1993 IUnknown_Release(pFTUnknown);
1995 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1996 ok_ole_success(hr, CreateStreamOnHGlobal);
1998 /* inproc normal marshaling */
2000 hr = IMarshal_MarshalInterface(pFTMarshal, pStream, &IID_IClassFactory,
2001 (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
2002 ok_ole_success(hr, IMarshal_MarshalInterface);
2004 ok_more_than_one_lock();
2006 test_freethreadedmarshaldata(pStream, MSHCTX_INPROC, &Test_ClassFactory, MSHLFLAGS_NORMAL);
2008 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2009 hr = IMarshal_UnmarshalInterface(pFTMarshal, pStream, &IID_IUnknown, (void **)&pProxy);
2010 ok_ole_success(hr, IMarshal_UnmarshalInterface);
2012 IUnknown_Release(pProxy);
2016 /* native doesn't allow us to unmarshal or release the stream data,
2017 * presumably because it wants us to call CoMarshalInterface instead */
2020 /* local normal marshaling */
2022 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2023 hr = IMarshal_MarshalInterface(pFTMarshal, pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_LOCAL, NULL, MSHLFLAGS_NORMAL);
2024 ok_ole_success(hr, IMarshal_MarshalInterface);
2026 ok_more_than_one_lock();
2028 test_freethreadedmarshaldata(pStream, MSHCTX_LOCAL, &Test_ClassFactory, MSHLFLAGS_NORMAL);
2030 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2031 hr = IMarshal_ReleaseMarshalData(pFTMarshal, pStream);
2032 ok_ole_success(hr, IMarshal_ReleaseMarshalData);
2037 /* inproc table-strong marshaling */
2039 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2040 hr = IMarshal_MarshalInterface(pFTMarshal, pStream, &IID_IClassFactory,
2041 (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, (void *)0xdeadbeef,
2042 MSHLFLAGS_TABLESTRONG);
2043 ok_ole_success(hr, IMarshal_MarshalInterface);
2045 ok_more_than_one_lock();
2047 test_freethreadedmarshaldata(pStream, MSHCTX_INPROC, &Test_ClassFactory, MSHLFLAGS_TABLESTRONG);
2049 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2050 hr = IMarshal_UnmarshalInterface(pFTMarshal, pStream, &IID_IUnknown, (void **)&pProxy);
2051 ok_ole_success(hr, IMarshal_UnmarshalInterface);
2053 IUnknown_Release(pProxy);
2055 ok_more_than_one_lock();
2057 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2058 hr = IMarshal_ReleaseMarshalData(pFTMarshal, pStream);
2059 ok_ole_success(hr, IMarshal_ReleaseMarshalData);
2063 /* inproc table-weak marshaling */
2065 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2066 hr = IMarshal_MarshalInterface(pFTMarshal, pStream, &IID_IClassFactory,
2067 (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, (void *)0xdeadbeef,
2068 MSHLFLAGS_TABLEWEAK);
2069 ok_ole_success(hr, IMarshal_MarshalInterface);
2073 test_freethreadedmarshaldata(pStream, MSHCTX_INPROC, &Test_ClassFactory, MSHLFLAGS_TABLEWEAK);
2075 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2076 hr = IMarshal_UnmarshalInterface(pFTMarshal, pStream, &IID_IUnknown, (void **)&pProxy);
2077 ok_ole_success(hr, IMarshal_UnmarshalInterface);
2079 ok_more_than_one_lock();
2081 IUnknown_Release(pProxy);
2085 /* inproc normal marshaling (for extraordinary cases) */
2087 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2088 hr = IMarshal_MarshalInterface(pFTMarshal, pStream, &IID_IClassFactory,
2089 (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
2090 ok_ole_success(hr, IMarshal_MarshalInterface);
2092 ok_more_than_one_lock();
2094 /* this call shows that DisconnectObject does nothing */
2095 hr = IMarshal_DisconnectObject(pFTMarshal, 0);
2096 ok_ole_success(hr, IMarshal_DisconnectObject);
2098 ok_more_than_one_lock();
2100 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2101 hr = IMarshal_ReleaseMarshalData(pFTMarshal, pStream);
2102 ok_ole_success(hr, IMarshal_ReleaseMarshalData);
2106 /* doesn't enforce marshaling rules here and allows us to unmarshal the
2107 * interface, even though it was freed above */
2108 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2109 hr = IMarshal_UnmarshalInterface(pFTMarshal, pStream, &IID_IUnknown, (void **)&pProxy);
2110 ok_ole_success(hr, IMarshal_UnmarshalInterface);
2114 IStream_Release(pStream);
2115 IMarshal_Release(pFTMarshal);
2118 static void test_inproc_handler(void)
2126 DWORD dwDisposition;
2129 hr = StringFromCLSID(&CLSID_WineTest, &pszClsid);
2130 ok_ole_success(hr, "StringFromCLSID");
2131 strcpy(buffer, "CLSID\\");
2132 WideCharToMultiByte(CP_ACP, 0, pszClsid, -1, buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), NULL, NULL);
2133 CoTaskMemFree(pszClsid);
2134 strcat(buffer, "\\InprocHandler32");
2135 error = RegCreateKeyEx(HKEY_CLASSES_ROOT, buffer, 0, NULL, 0, KEY_SET_VALUE, NULL, &hkey, &dwDisposition);
2136 ok(error == ERROR_SUCCESS, "RegCreateKeyEx failed with error %d\n", error);
2137 error = RegSetValueEx(hkey, NULL, 0, REG_SZ, (const unsigned char *)"ole32.dll", strlen("ole32.dll") + 1);
2138 ok(error == ERROR_SUCCESS, "RegSetValueEx failed with error %d\n", error);
2141 hr = CoCreateInstance(&CLSID_WineTest, NULL, CLSCTX_INPROC_HANDLER, &IID_IUnknown, (void **)&pObject);
2143 ok_ole_success(hr, "CoCreateInstance");
2147 hr = IUnknown_QueryInterface(pObject, &IID_IWineTest, (void **)&pObject2);
2148 ok(hr == E_NOINTERFACE, "IUnknown_QueryInterface on handler for invalid interface returned 0x%08x instead of E_NOINTERFACE\n", hr);
2150 /* it's a handler as it supports IOleObject */
2151 hr = IUnknown_QueryInterface(pObject, &IID_IOleObject, (void **)&pObject2);
2152 ok_ole_success(hr, "IUnknown_QueryInterface(&IID_IOleObject)");
2153 IUnknown_Release(pObject2);
2155 IUnknown_Release(pObject);
2158 RegDeleteKey(HKEY_CLASSES_ROOT, buffer);
2159 *strrchr(buffer, '\\') = '\0';
2160 RegDeleteKey(HKEY_CLASSES_ROOT, buffer);
2163 static HRESULT WINAPI Test_SMI_QueryInterface(
2164 IStdMarshalInfo *iface,
2168 if (ppvObj == NULL) return E_POINTER;
2170 if (IsEqualGUID(riid, &IID_IUnknown) ||
2171 IsEqualGUID(riid, &IID_IStdMarshalInfo))
2173 *ppvObj = (LPVOID)iface;
2174 IClassFactory_AddRef(iface);
2178 return E_NOINTERFACE;
2181 static ULONG WINAPI Test_SMI_AddRef(IStdMarshalInfo *iface)
2184 return 2; /* non-heap-based object */
2187 static ULONG WINAPI Test_SMI_Release(IStdMarshalInfo *iface)
2190 return 1; /* non-heap-based object */
2193 static HRESULT WINAPI Test_SMI_GetClassForHandler(
2194 IStdMarshalInfo *iface,
2195 DWORD dwDestContext,
2196 void *pvDestContext,
2199 *pClsid = CLSID_WineTest;
2203 static const IStdMarshalInfoVtbl Test_SMI_Vtbl =
2205 Test_SMI_QueryInterface,
2208 Test_SMI_GetClassForHandler
2211 static IStdMarshalInfo Test_SMI = {&Test_SMI_Vtbl};
2213 static void test_handler_marshaling(void)
2216 IStream *pStream = NULL;
2217 IUnknown *pProxy = NULL;
2221 static const LARGE_INTEGER ullZero;
2225 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
2226 ok_ole_success(hr, "CreateStreamOnHGlobal");
2227 tid = start_host_object(pStream, &IID_IUnknown, (IUnknown*)&Test_SMI, MSHLFLAGS_NORMAL, &thread);
2229 ok_more_than_one_lock();
2231 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
2232 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)&pProxy);
2233 ok_ole_success(hr, "CoUnmarshalInterface");
2234 IStream_Release(pStream);
2236 ok_more_than_one_lock();
2238 hr = IUnknown_QueryInterface(pProxy, &IID_IWineTest, (void **)&pObject);
2239 ok(hr == E_NOINTERFACE, "IUnknown_QueryInterface with unknown IID should have returned E_NOINTERFACE instead of 0x%08x\n", hr);
2241 /* it's a handler as it supports IOleObject */
2242 hr = IUnknown_QueryInterface(pProxy, &IID_IOleObject, (void **)&pObject);
2244 ok_ole_success(hr, "IUnknown_QueryInterface(&IID_IOleObject)");
2245 if (SUCCEEDED(hr)) IUnknown_Release(pObject);
2247 IUnknown_Release(pProxy);
2251 end_host_object(tid, thread);
2253 /* FIXME: test IPersist interface has the same effect as IStdMarshalInfo */
2257 static void test_client_security(void)
2260 IStream *pStream = NULL;
2261 IClassFactory *pProxy = NULL;
2262 IUnknown *pProxy2 = NULL;
2263 IUnknown *pUnknown1 = NULL;
2264 IUnknown *pUnknown2 = NULL;
2265 IClientSecurity *pCliSec = NULL;
2269 static const LARGE_INTEGER ullZero;
2272 OLECHAR *pServerPrincName;
2276 DWORD dwCapabilities;
2281 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
2282 ok_ole_success(hr, "CreateStreamOnHGlobal");
2283 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
2285 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
2286 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
2287 ok_ole_success(hr, "CoUnmarshalInterface");
2288 IStream_Release(pStream);
2290 hr = IUnknown_QueryInterface(pProxy, &IID_IUnknown, (LPVOID*)&pUnknown1);
2291 ok_ole_success(hr, "IUnknown_QueryInterface IID_IUnknown");
2293 hr = IUnknown_QueryInterface(pProxy, &IID_IRemUnknown, (LPVOID*)&pProxy2);
2294 ok_ole_success(hr, "IUnknown_QueryInterface IID_IStream");
2296 hr = IUnknown_QueryInterface(pProxy2, &IID_IUnknown, (LPVOID*)&pUnknown2);
2297 ok_ole_success(hr, "IUnknown_QueryInterface IID_IUnknown");
2299 ok(pUnknown1 == pUnknown2, "both proxy's IUnknowns should be the same - %p, %p\n", pUnknown1, pUnknown2);
2301 hr = IUnknown_QueryInterface(pProxy, &IID_IMarshal, (LPVOID*)&pMarshal);
2302 ok_ole_success(hr, "IUnknown_QueryInterface IID_IMarshal");
2304 hr = IUnknown_QueryInterface(pProxy, &IID_IClientSecurity, (LPVOID*)&pCliSec);
2305 ok_ole_success(hr, "IUnknown_QueryInterface IID_IClientSecurity");
2307 hr = IClientSecurity_QueryBlanket(pCliSec, (IUnknown *)pProxy, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
2308 todo_wine ok_ole_success(hr, "IClientSecurity_QueryBlanket (all NULLs)");
2310 hr = IClientSecurity_QueryBlanket(pCliSec, (IUnknown *)pMarshal, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
2311 todo_wine ok(hr == E_NOINTERFACE, "IClientSecurity_QueryBlanket with local interface should have returned E_NOINTERFACE instead of 0x%08x\n", hr);
2313 hr = IClientSecurity_QueryBlanket(pCliSec, (IUnknown *)pProxy, &dwAuthnSvc, &dwAuthzSvc, &pServerPrincName, &dwAuthnLevel, &dwImpLevel, &pAuthInfo, &dwCapabilities);
2314 todo_wine ok_ole_success(hr, "IClientSecurity_QueryBlanket");
2316 hr = IClientSecurity_SetBlanket(pCliSec, (IUnknown *)pProxy, dwAuthnSvc, dwAuthzSvc, pServerPrincName, dwAuthnLevel, RPC_C_IMP_LEVEL_IMPERSONATE, pAuthInfo, dwCapabilities);
2317 todo_wine ok_ole_success(hr, "IClientSecurity_SetBlanket");
2319 hr = IClassFactory_CreateInstance(pProxy, NULL, &IID_IWineTest, &pv);
2320 ok(hr == E_NOINTERFACE, "COM call should have succeeded instead of returning 0x%08x\n", hr);
2322 hr = IClientSecurity_SetBlanket(pCliSec, (IUnknown *)pMarshal, dwAuthnSvc, dwAuthzSvc, pServerPrincName, dwAuthnLevel, dwImpLevel, pAuthInfo, dwCapabilities);
2323 todo_wine ok(hr == E_NOINTERFACE, "IClientSecurity_SetBlanket with local interface should have returned E_NOINTERFACE instead of 0x%08x\n", hr);
2325 hr = IClientSecurity_SetBlanket(pCliSec, (IUnknown *)pProxy, 0xdeadbeef, dwAuthzSvc, pServerPrincName, dwAuthnLevel, dwImpLevel, pAuthInfo, dwCapabilities);
2326 todo_wine ok(hr == E_INVALIDARG, "IClientSecurity_SetBlanke with invalid dwAuthnSvc should have returned E_INVALIDARG instead of 0x%08x\n", hr);
2328 CoTaskMemFree(pServerPrincName);
2330 hr = IClientSecurity_QueryBlanket(pCliSec, (IUnknown *)pUnknown1, &dwAuthnSvc, &dwAuthzSvc, &pServerPrincName, &dwAuthnLevel, &dwImpLevel, &pAuthInfo, &dwCapabilities);
2331 todo_wine ok_ole_success(hr, "IClientSecurity_QueryBlanket(IUnknown)");
2333 CoTaskMemFree(pServerPrincName);
2335 IClassFactory_Release(pProxy);
2336 IUnknown_Release(pProxy2);
2337 IUnknown_Release(pUnknown1);
2338 IUnknown_Release(pUnknown2);
2339 IMarshal_Release(pMarshal);
2340 IClientSecurity_Release(pCliSec);
2342 end_host_object(tid, thread);
2345 static HANDLE heventShutdown;
2347 static void LockModuleOOP(void)
2349 InterlockedIncrement(&cLocks); /* for test purposes only */
2350 CoAddRefServerProcess();
2353 static void UnlockModuleOOP(void)
2355 InterlockedDecrement(&cLocks); /* for test purposes only */
2356 if (!CoReleaseServerProcess())
2357 SetEvent(heventShutdown);
2360 static HWND hwnd_app;
2362 static HRESULT WINAPI TestOOP_IClassFactory_QueryInterface(
2363 LPCLASSFACTORY iface,
2367 if (ppvObj == NULL) return E_POINTER;
2369 if (IsEqualGUID(riid, &IID_IUnknown) ||
2370 IsEqualGUID(riid, &IID_IClassFactory))
2372 *ppvObj = (LPVOID)iface;
2373 IClassFactory_AddRef(iface);
2377 return E_NOINTERFACE;
2380 static ULONG WINAPI TestOOP_IClassFactory_AddRef(LPCLASSFACTORY iface)
2382 return 2; /* non-heap-based object */
2385 static ULONG WINAPI TestOOP_IClassFactory_Release(LPCLASSFACTORY iface)
2387 return 1; /* non-heap-based object */
2390 static HRESULT WINAPI TestOOP_IClassFactory_CreateInstance(
2391 LPCLASSFACTORY iface,
2392 LPUNKNOWN pUnkOuter,
2396 if (IsEqualIID(riid, &IID_IClassFactory) || IsEqualIID(riid, &IID_IUnknown))
2401 return CLASS_E_CLASSNOTAVAILABLE;
2404 static HRESULT WINAPI TestOOP_IClassFactory_LockServer(
2405 LPCLASSFACTORY iface,
2415 static const IClassFactoryVtbl TestClassFactoryOOP_Vtbl =
2417 TestOOP_IClassFactory_QueryInterface,
2418 TestOOP_IClassFactory_AddRef,
2419 TestOOP_IClassFactory_Release,
2420 TestOOP_IClassFactory_CreateInstance,
2421 TestOOP_IClassFactory_LockServer
2424 static IClassFactory TestOOP_ClassFactory = { &TestClassFactoryOOP_Vtbl };
2426 static void test_register_local_server(void)
2434 heventShutdown = CreateEvent(NULL, TRUE, FALSE, NULL);
2436 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&TestOOP_ClassFactory,
2437 CLSCTX_LOCAL_SERVER, REGCLS_SINGLEUSE, &cookie);
2438 ok_ole_success(hr, CoRegisterClassObject);
2440 ready_event = CreateEvent(NULL, FALSE, FALSE, "Wine COM Test Ready Event");
2441 SetEvent(ready_event);
2443 quit_event = CreateEvent(NULL, FALSE, FALSE, "Wine COM Test Quit Event");
2447 wait = MsgWaitForMultipleObjects(1, &quit_event, FALSE, INFINITE, QS_ALLINPUT);
2448 if (wait == WAIT_OBJECT_0+1)
2451 BOOL ret = PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
2454 trace("Message 0x%x\n", msg.message);
2455 TranslateMessage(&msg);
2456 DispatchMessage(&msg);
2460 while (wait == WAIT_OBJECT_0+1);
2462 hr = CoRevokeClassObject(cookie);
2463 ok_ole_success(hr, CoRevokeClassObject);
2466 static HANDLE create_target_process(const char *arg)
2469 char cmdline[MAX_PATH];
2470 PROCESS_INFORMATION pi;
2471 STARTUPINFO si = { 0 };
2476 winetest_get_mainargs( &argv );
2477 sprintf(cmdline, "%s %s %s", argv[0], argv[1], arg);
2478 ok(CreateProcess(argv[0], cmdline, NULL, NULL, FALSE, 0, NULL, NULL,
2479 &si, &pi) != 0, "CreateProcess failed with error: %u\n", GetLastError());
2480 if (pi.hThread) CloseHandle(pi.hThread);
2484 /* tests functions commonly used by out of process COM servers */
2485 static void test_local_server(void)
2495 heventShutdown = CreateEvent(NULL, TRUE, FALSE, NULL);
2499 /* Start the object suspended */
2500 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&TestOOP_ClassFactory,
2501 CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE | REGCLS_SUSPENDED, &cookie);
2502 ok_ole_success(hr, CoRegisterClassObject);
2504 /* ... and CoGetClassObject does not find it and fails when it looks for the
2505 * class in the registry */
2506 hr = CoGetClassObject(&CLSID_WineOOPTest, CLSCTX_INPROC_SERVER,
2507 NULL, &IID_IClassFactory, (LPVOID*)&cf);
2508 ok(hr == REGDB_E_CLASSNOTREG || /* NT */
2509 hr == S_OK /* Win9x */,
2510 "CoGetClassObject should have returned REGDB_E_CLASSNOTREG instead of 0x%08x\n", hr);
2512 /* Resume the object suspended above ... */
2513 hr = CoResumeClassObjects();
2514 ok_ole_success(hr, CoResumeClassObjects);
2516 /* ... and now it should succeed */
2517 hr = CoGetClassObject(&CLSID_WineOOPTest, CLSCTX_INPROC_SERVER,
2518 NULL, &IID_IClassFactory, (LPVOID*)&cf);
2519 ok_ole_success(hr, CoGetClassObject);
2521 /* Now check the locking is working */
2522 /* NOTE: we are accessing the class directly, not through a proxy */
2526 hr = IClassFactory_LockServer(cf, TRUE);
2527 ok_ole_success(hr, IClassFactory_LockServer);
2529 ok_more_than_one_lock();
2531 IClassFactory_LockServer(cf, FALSE);
2532 ok_ole_success(hr, IClassFactory_LockServer);
2536 IClassFactory_Release(cf);
2538 /* wait for shutdown signal */
2539 ret = WaitForSingleObject(heventShutdown, 0);
2540 ok(ret != WAIT_TIMEOUT, "Server didn't shut down\n");
2542 /* try to connect again after SCM has suspended registered class objects */
2543 hr = CoGetClassObject(&CLSID_WineOOPTest, CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER, NULL,
2544 &IID_IClassFactory, (LPVOID*)&cf);
2545 ok(hr == CO_E_SERVER_STOPPING || /* NT */
2546 hr == S_OK /* Win9x */,
2547 "CoGetClassObject should have returned CO_E_SERVER_STOPPING instead of 0x%08x\n", hr);
2549 hr = CoRevokeClassObject(cookie);
2550 ok_ole_success(hr, CoRevokeClassObject);
2552 CloseHandle(heventShutdown);
2554 process = create_target_process("-Embedding");
2555 ok(process != NULL, "couldn't start local server process, error was %d\n", GetLastError());
2557 ready_event = CreateEvent(NULL, FALSE, FALSE, "Wine COM Test Ready Event");
2558 WaitForSingleObject(ready_event, INFINITE);
2559 CloseHandle(ready_event);
2561 hr = CoCreateInstance(&CLSID_WineOOPTest, NULL, CLSCTX_LOCAL_SERVER, &IID_IClassFactory, (void **)&cf);
2562 ok_ole_success(hr, CoCreateInstance);
2564 IClassFactory_Release(cf);
2566 hr = CoCreateInstance(&CLSID_WineOOPTest, NULL, CLSCTX_LOCAL_SERVER, &IID_IClassFactory, (void **)&cf);
2567 ok(hr == REGDB_E_CLASSNOTREG, "Second CoCreateInstance on REGCLS_SINGLEUSE object should have failed\n");
2569 quit_event = CreateEvent(NULL, FALSE, FALSE, "Wine COM Test Quit Event");
2570 SetEvent(quit_event);
2572 WaitForSingleObject(process, INFINITE);
2573 CloseHandle(quit_event);
2574 CloseHandle(process);
2580 IGlobalInterfaceTable *git;
2583 static DWORD CALLBACK get_global_interface_proc(LPVOID pv)
2586 struct git_params *params = (struct git_params *)pv;
2589 hr = IGlobalInterfaceTable_GetInterfaceFromGlobal(params->git, params->cookie, &IID_IClassFactory, (void **)&cf);
2590 ok(hr == CO_E_NOTINITIALIZED,
2591 "IGlobalInterfaceTable_GetInterfaceFromGlobal should have failed with error CO_E_NOTINITIALIZED instead of 0x%08x\n",
2596 hr = IGlobalInterfaceTable_GetInterfaceFromGlobal(params->git, params->cookie, &IID_IClassFactory, (void **)&cf);
2597 ok_ole_success(hr, IGlobalInterfaceTable_GetInterfaceFromGlobal);
2599 IClassFactory_Release(cf);
2606 static void test_globalinterfacetable(void)
2609 IGlobalInterfaceTable *git;
2613 struct git_params params;
2617 trace("test_globalinterfacetable\n");
2620 hr = CoCreateInstance(&CLSID_StdGlobalInterfaceTable, NULL, CLSCTX_INPROC_SERVER, &IID_IGlobalInterfaceTable, (void **)&git);
2621 ok_ole_success(hr, CoCreateInstance);
2623 hr = IGlobalInterfaceTable_RegisterInterfaceInGlobal(git, (IUnknown *)&Test_ClassFactory, &IID_IClassFactory, &cookie);
2624 ok_ole_success(hr, IGlobalInterfaceTable_RegisterInterfaceInGlobal);
2626 ok_more_than_one_lock();
2628 params.cookie = cookie;
2630 /* note: params is on stack so we MUST wait for get_global_interface_proc
2631 * to exit before we can return */
2632 thread = CreateThread(NULL, 0, get_global_interface_proc, ¶ms, 0, &tid);
2634 ret = MsgWaitForMultipleObjects(1, &thread, FALSE, INFINITE, QS_ALLINPUT);
2635 while (ret == WAIT_OBJECT_0 + 1)
2638 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
2639 DispatchMessage(&msg);
2640 ret = MsgWaitForMultipleObjects(1, &thread, FALSE, INFINITE, QS_ALLINPUT);
2643 CloseHandle(thread);
2645 /* test getting interface from global with different iid */
2646 hr = IGlobalInterfaceTable_GetInterfaceFromGlobal(git, cookie, &IID_IUnknown, (void **)&object);
2647 ok_ole_success(hr, IGlobalInterfaceTable_GetInterfaceFromGlobal);
2648 IUnknown_Release(object);
2650 /* test getting interface from global with same iid */
2651 hr = IGlobalInterfaceTable_GetInterfaceFromGlobal(git, cookie, &IID_IClassFactory, (void **)&object);
2652 ok_ole_success(hr, IGlobalInterfaceTable_GetInterfaceFromGlobal);
2653 IUnknown_Release(object);
2655 hr = IGlobalInterfaceTable_RevokeInterfaceFromGlobal(git, cookie);
2656 ok_ole_success(hr, IGlobalInterfaceTable_RevokeInterfaceFromGlobal);
2660 IGlobalInterfaceTable_Release(git);
2663 static const char *debugstr_iid(REFIID riid)
2665 static char name[256];
2669 LONG name_size = sizeof(name);
2670 StringFromGUID2(riid, bufferW, sizeof(bufferW)/sizeof(bufferW[0]));
2671 WideCharToMultiByte(CP_ACP, 0, bufferW, sizeof(bufferW)/sizeof(bufferW[0]), buffer, sizeof(buffer), NULL, NULL);
2672 if (RegOpenKeyEx(HKEY_CLASSES_ROOT, "Interface", 0, KEY_QUERY_VALUE, &hkeyInterface) != ERROR_SUCCESS)
2674 memcpy(name, buffer, sizeof(buffer));
2677 if (RegQueryValue(hkeyInterface, buffer, name, &name_size) != ERROR_SUCCESS)
2679 memcpy(name, buffer, sizeof(buffer));
2682 RegCloseKey(hkeyInterface);
2687 static HRESULT WINAPI TestChannelHook_QueryInterface(IChannelHook *iface, REFIID riid, void **ppv)
2689 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IChannelHook))
2692 IUnknown_AddRef(iface);
2697 return E_NOINTERFACE;
2700 static ULONG WINAPI TestChannelHook_AddRef(IChannelHook *iface)
2705 static ULONG WINAPI TestChannelHook_Release(IChannelHook *iface)
2710 static void WINAPI TestChannelHook_ClientGetSize(
2711 IChannelHook *iface,
2716 SChannelHookCallInfo *info = (SChannelHookCallInfo *)riid;
2717 trace("TestChannelHook_ClientGetBuffer\n");
2718 trace("\t%s method %d\n", debugstr_iid(riid), info->iMethod);
2719 trace("\tcid: %s\n", debugstr_iid(&info->uCausality));
2720 ok(info->cbSize == sizeof(*info), "info->cbSize was %d instead of %d\n", info->cbSize, (int)sizeof(*info));
2721 ok(info->dwServerPid == GetCurrentProcessId(), "info->dwServerPid was 0x%x instead of 0x%x\n", info->dwServerPid, GetCurrentProcessId());
2722 ok(!info->pObject, "info->pObject should be NULL\n");
2723 ok(IsEqualGUID(uExtent, &EXTENTID_WineTest), "uExtent wasn't correct\n");
2728 static void WINAPI TestChannelHook_ClientFillBuffer(
2729 IChannelHook *iface,
2735 SChannelHookCallInfo *info = (SChannelHookCallInfo *)riid;
2736 trace("TestChannelHook_ClientFillBuffer\n");
2737 ok(info->cbSize == sizeof(*info), "info->cbSize was %d instead of %d\n", info->cbSize, (int)sizeof(*info));
2738 ok(info->dwServerPid == GetCurrentProcessId(), "info->dwServerPid was 0x%x instead of 0x%x\n", info->dwServerPid, GetCurrentProcessId());
2739 ok(!info->pObject, "info->pObject should be NULL\n");
2740 ok(IsEqualGUID(uExtent, &EXTENTID_WineTest), "uExtent wasn't correct\n");
2742 *(unsigned char *)pDataBuffer = 0xcc;
2746 static void WINAPI TestChannelHook_ClientNotify(
2747 IChannelHook *iface,
2755 SChannelHookCallInfo *info = (SChannelHookCallInfo *)riid;
2756 trace("TestChannelHook_ClientNotify hrFault = 0x%08x\n", hrFault);
2757 ok(info->cbSize == sizeof(*info), "info->cbSize was %d instead of %d\n", info->cbSize, (int)sizeof(*info));
2758 ok(info->dwServerPid == GetCurrentProcessId(), "info->dwServerPid was 0x%x instead of 0x%x\n", info->dwServerPid, GetCurrentProcessId());
2760 ok(info->pObject != NULL, "info->pObject shouldn't be NULL\n");
2762 ok(IsEqualGUID(uExtent, &EXTENTID_WineTest), "uExtent wasn't correct\n");
2765 static void WINAPI TestChannelHook_ServerNotify(
2766 IChannelHook *iface,
2773 SChannelHookCallInfo *info = (SChannelHookCallInfo *)riid;
2774 trace("TestChannelHook_ServerNotify\n");
2775 ok(info->cbSize == sizeof(*info), "info->cbSize was %d instead of %d\n", info->cbSize, (int)sizeof(*info));
2776 ok(info->dwServerPid == GetCurrentProcessId(), "info->dwServerPid was 0x%x instead of 0x%x\n", info->dwServerPid, GetCurrentProcessId());
2777 ok(info->pObject != NULL, "info->pObject shouldn't be NULL\n");
2778 ok(cbDataSize == 1, "cbDataSize should have been 1 instead of %d\n", cbDataSize);
2779 ok(*(unsigned char *)pDataBuffer == 0xcc, "pDataBuffer should have contained 0xcc instead of 0x%x\n", *(unsigned char *)pDataBuffer);
2780 ok(IsEqualGUID(uExtent, &EXTENTID_WineTest), "uExtent wasn't correct\n");
2783 static void WINAPI TestChannelHook_ServerGetSize(
2784 IChannelHook *iface,
2790 SChannelHookCallInfo *info = (SChannelHookCallInfo *)riid;
2791 trace("TestChannelHook_ServerGetSize\n");
2792 trace("\t%s method %d\n", debugstr_iid(riid), info->iMethod);
2793 ok(info->cbSize == sizeof(*info), "info->cbSize was %d instead of %d\n", info->cbSize, (int)sizeof(*info));
2794 ok(info->dwServerPid == GetCurrentProcessId(), "info->dwServerPid was 0x%x instead of 0x%x\n", info->dwServerPid, GetCurrentProcessId());
2795 ok(info->pObject != NULL, "info->pObject shouldn't be NULL\n");
2796 ok(IsEqualGUID(uExtent, &EXTENTID_WineTest), "uExtent wasn't correct\n");
2797 if (hrFault != S_OK)
2798 trace("\thrFault = 0x%08x\n", hrFault);
2803 static void WINAPI TestChannelHook_ServerFillBuffer(
2804 IChannelHook *iface,
2811 trace("TestChannelHook_ServerFillBuffer\n");
2812 ok(0, "TestChannelHook_ServerFillBuffer shouldn't be called\n");
2815 static const IChannelHookVtbl TestChannelHookVtbl =
2817 TestChannelHook_QueryInterface,
2818 TestChannelHook_AddRef,
2819 TestChannelHook_Release,
2820 TestChannelHook_ClientGetSize,
2821 TestChannelHook_ClientFillBuffer,
2822 TestChannelHook_ClientNotify,
2823 TestChannelHook_ServerNotify,
2824 TestChannelHook_ServerGetSize,
2825 TestChannelHook_ServerFillBuffer,
2828 static IChannelHook TestChannelHook = { &TestChannelHookVtbl };
2830 static void test_channel_hook(void)
2832 IStream *pStream = NULL;
2833 IClassFactory *cf = NULL;
2835 IUnknown *proxy = NULL;
2839 hr = CoRegisterChannelHook(&EXTENTID_WineTest, &TestChannelHook);
2840 ok_ole_success(hr, CoRegisterChannelHook);
2842 hr = CoRegisterMessageFilter(&MessageFilter, NULL);
2843 ok_ole_success(hr, CoRegisterMessageFilter);
2847 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
2848 ok_ole_success(hr, CreateStreamOnHGlobal);
2849 tid = start_host_object2(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &MessageFilter, &thread);
2851 ok_more_than_one_lock();
2853 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
2854 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&cf);
2855 ok_ole_success(hr, CoUnmarshalInterface);
2856 IStream_Release(pStream);
2858 ok_more_than_one_lock();
2860 hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
2861 ok_ole_success(hr, IClassFactory_CreateInstance);
2862 IUnknown_Release(proxy);
2864 IClassFactory_Release(cf);
2868 end_host_object(tid, thread);
2870 hr = CoRegisterMessageFilter(NULL, NULL);
2871 ok_ole_success(hr, CoRegisterMessageFilter);
2877 HMODULE hOle32 = GetModuleHandle("ole32");
2881 if (!(pCoInitializeEx = (void*)GetProcAddress(hOle32, "CoInitializeEx"))) goto no_test;
2883 argc = winetest_get_mainargs( &argv );
2884 if (argc > 2 && (!strcmp(argv[2], "-Embedding")))
2886 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
2887 test_register_local_server();
2893 /* register a window class used in several tests */
2894 memset(&wndclass, 0, sizeof(wndclass));
2895 wndclass.lpfnWndProc = window_proc;
2896 wndclass.lpszClassName = "WineCOMTest";
2897 RegisterClass(&wndclass);
2899 test_cocreateinstance_proxy();
2901 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
2903 /* FIXME: test CoCreateInstanceEx */
2905 /* lifecycle management and marshaling tests */
2906 test_no_marshaler();
2907 test_normal_marshal_and_release();
2908 test_normal_marshal_and_unmarshal();
2909 test_marshal_and_unmarshal_invalid();
2910 test_same_apartment_unmarshal_failure();
2911 test_interthread_marshal_and_unmarshal();
2912 test_proxy_marshal_and_unmarshal();
2913 test_proxy_marshal_and_unmarshal2();
2914 test_proxy_marshal_and_unmarshal_weak();
2915 test_proxy_marshal_and_unmarshal_strong();
2916 test_marshal_stub_apartment_shutdown();
2917 test_marshal_proxy_apartment_shutdown();
2918 test_marshal_proxy_mta_apartment_shutdown();
2919 test_no_couninitialize_server();
2920 test_no_couninitialize_client();
2921 test_tableweak_marshal_and_unmarshal_twice();
2922 test_tableweak_marshal_releasedata1();
2923 test_tableweak_marshal_releasedata2();
2924 test_tablestrong_marshal_and_unmarshal_twice();
2925 test_lock_object_external();
2926 test_disconnect_stub();
2927 test_normal_marshal_and_unmarshal_twice();
2928 test_hresult_marshaling();
2929 test_proxy_used_in_wrong_thread();
2930 test_message_filter();
2931 test_bad_marshal_stream();
2932 test_proxy_interfaces();
2933 test_stubbuffer(&IID_IClassFactory);
2934 test_proxybuffer(&IID_IClassFactory);
2935 test_message_reentrancy();
2936 test_call_from_message();
2937 test_WM_QUIT_handling();
2938 test_freethreadedmarshaler();
2939 test_inproc_handler();
2940 test_handler_marshaling();
2941 test_client_security();
2943 test_local_server();
2945 test_globalinterfacetable();
2947 /* must be last test as channel hooks can't be unregistered */
2948 test_channel_hook();
2954 trace("You need DCOM95 installed to run this test\n");