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
34 #include "wine/test.h"
36 /* functions that are not present on all versions of Windows */
37 HRESULT (WINAPI
* pCoInitializeEx
)(LPVOID lpReserved
, DWORD dwCoInit
);
39 /* helper macros to make tests a bit leaner */
40 #define ok_more_than_one_lock() ok(cLocks > 0, "Number of locks should be > 0, but actually is %d\n", cLocks)
41 #define ok_no_locks() ok(cLocks == 0, "Number of locks should be 0, but actually is %d\n", cLocks)
42 #define ok_ole_success(hr, func) ok(hr == S_OK, #func " failed with error 0x%08x\n", hr)
44 static const IID IID_IWineTest
=
49 {0xa1, 0xa2, 0x5d, 0x5a, 0x36, 0x54, 0xd3, 0xbd}
50 }; /* 5201163f-8164-4fd0-a1a2-5d5a3654d3bd */
52 static const IID IID_IRemUnknown
=
57 {0xc0,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46}
60 #define EXTENTID_WineTest IID_IWineTest
61 #define CLSID_WineTest IID_IWineTest
63 static const CLSID CLSID_WineOOPTest
=
68 {0xa1, 0xa2, 0x5d, 0x5a, 0x36, 0x54, 0xd3, 0xbd}
69 }; /* 5201163f-8164-4fd0-a1a2-5d5a3654d3bd */
71 static void test_cocreateinstance_proxy(void)
77 pCoInitializeEx(NULL
, COINIT_MULTITHREADED
);
79 hr
= CoCreateInstance(&CLSID_ShellDesktop
, NULL
, CLSCTX_INPROC
, &IID_IUnknown
, (void **)&pProxy
);
80 ok_ole_success(hr
, CoCreateInstance
);
81 hr
= IUnknown_QueryInterface(pProxy
, &IID_IMultiQI
, (void **)&pMQI
);
82 ok(hr
== S_OK
, "created object is not a proxy, so was created in the wrong apartment\n");
84 IMultiQI_Release(pMQI
);
85 IUnknown_Release(pProxy
);
90 static const LARGE_INTEGER ullZero
;
93 static void LockModule(void)
95 InterlockedIncrement(&cLocks
);
98 static void UnlockModule(void)
100 InterlockedDecrement(&cLocks
);
104 static HRESULT WINAPI
Test_IUnknown_QueryInterface(
109 if (ppvObj
== NULL
) return E_POINTER
;
111 if (IsEqualGUID(riid
, &IID_IUnknown
))
113 *ppvObj
= (LPVOID
)iface
;
114 IUnknown_AddRef(iface
);
119 return E_NOINTERFACE
;
122 static ULONG WINAPI
Test_IUnknown_AddRef(LPUNKNOWN iface
)
125 return 2; /* non-heap-based object */
128 static ULONG WINAPI
Test_IUnknown_Release(LPUNKNOWN iface
)
131 return 1; /* non-heap-based object */
134 static const IUnknownVtbl TestUnknown_Vtbl
=
136 Test_IUnknown_QueryInterface
,
137 Test_IUnknown_AddRef
,
138 Test_IUnknown_Release
,
141 static IUnknown Test_Unknown
= { &TestUnknown_Vtbl
};
144 static HRESULT WINAPI
Test_IClassFactory_QueryInterface(
145 LPCLASSFACTORY iface
,
149 if (ppvObj
== NULL
) return E_POINTER
;
151 if (IsEqualGUID(riid
, &IID_IUnknown
) ||
152 IsEqualGUID(riid
, &IID_IClassFactory
) ||
153 /* the only other interface Wine is currently able to marshal (for testing two proxies) */
154 IsEqualGUID(riid
, &IID_IRemUnknown
))
156 *ppvObj
= (LPVOID
)iface
;
157 IClassFactory_AddRef(iface
);
162 return E_NOINTERFACE
;
165 static ULONG WINAPI
Test_IClassFactory_AddRef(LPCLASSFACTORY iface
)
168 return 2; /* non-heap-based object */
171 static ULONG WINAPI
Test_IClassFactory_Release(LPCLASSFACTORY iface
)
174 return 1; /* non-heap-based object */
177 static HRESULT WINAPI
Test_IClassFactory_CreateInstance(
178 LPCLASSFACTORY iface
,
183 if (pUnkOuter
) return CLASS_E_NOAGGREGATION
;
184 return IUnknown_QueryInterface((IUnknown
*)&Test_Unknown
, riid
, ppvObj
);
187 static HRESULT WINAPI
Test_IClassFactory_LockServer(
188 LPCLASSFACTORY iface
,
194 static const IClassFactoryVtbl TestClassFactory_Vtbl
=
196 Test_IClassFactory_QueryInterface
,
197 Test_IClassFactory_AddRef
,
198 Test_IClassFactory_Release
,
199 Test_IClassFactory_CreateInstance
,
200 Test_IClassFactory_LockServer
203 static IClassFactory Test_ClassFactory
= { &TestClassFactory_Vtbl
};
205 #define RELEASEMARSHALDATA WM_USER
207 struct host_object_data
212 MSHLFLAGS marshal_flags
;
213 HANDLE marshal_event
;
214 IMessageFilter
*filter
;
217 static DWORD CALLBACK
host_object_proc(LPVOID p
)
219 struct host_object_data
*data
= (struct host_object_data
*)p
;
223 pCoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
227 IMessageFilter
* prev_filter
= NULL
;
228 hr
= CoRegisterMessageFilter(data
->filter
, &prev_filter
);
229 if (prev_filter
) IMessageFilter_Release(prev_filter
);
230 ok_ole_success(hr
, CoRegisterMessageFilter
);
233 hr
= CoMarshalInterface(data
->stream
, &data
->iid
, data
->object
, MSHCTX_INPROC
, NULL
, data
->marshal_flags
);
234 ok_ole_success(hr
, CoMarshalInterface
);
236 /* force the message queue to be created before signaling parent thread */
237 PeekMessage(&msg
, NULL
, WM_USER
, WM_USER
, PM_NOREMOVE
);
239 SetEvent(data
->marshal_event
);
241 while (GetMessage(&msg
, NULL
, 0, 0))
243 if (msg
.hwnd
== NULL
&& msg
.message
== RELEASEMARSHALDATA
)
245 CoReleaseMarshalData(data
->stream
);
246 SetEvent((HANDLE
)msg
.lParam
);
249 DispatchMessage(&msg
);
252 HeapFree(GetProcessHeap(), 0, data
);
259 static DWORD
start_host_object2(IStream
*stream
, REFIID riid
, IUnknown
*object
, MSHLFLAGS marshal_flags
, IMessageFilter
*filter
, HANDLE
*thread
)
262 HANDLE marshal_event
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
263 struct host_object_data
*data
= HeapAlloc(GetProcessHeap(), 0, sizeof(*data
));
265 data
->stream
= stream
;
267 data
->object
= object
;
268 data
->marshal_flags
= marshal_flags
;
269 data
->marshal_event
= marshal_event
;
270 data
->filter
= filter
;
272 *thread
= CreateThread(NULL
, 0, host_object_proc
, data
, 0, &tid
);
274 /* wait for marshaling to complete before returning */
275 WaitForSingleObject(marshal_event
, INFINITE
);
276 CloseHandle(marshal_event
);
281 static DWORD
start_host_object(IStream
*stream
, REFIID riid
, IUnknown
*object
, MSHLFLAGS marshal_flags
, HANDLE
*thread
)
283 return start_host_object2(stream
, riid
, object
, marshal_flags
, NULL
, thread
);
286 /* asks thread to release the marshal data because it has to be done by the
287 * same thread that marshaled the interface in the first place. */
288 static void release_host_object(DWORD tid
)
290 HANDLE event
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
291 PostThreadMessage(tid
, RELEASEMARSHALDATA
, 0, (LPARAM
)event
);
292 WaitForSingleObject(event
, INFINITE
);
296 static void end_host_object(DWORD tid
, HANDLE thread
)
298 BOOL ret
= PostThreadMessage(tid
, WM_QUIT
, 0, 0);
299 ok(ret
, "PostThreadMessage failed with error %d\n", GetLastError());
300 /* be careful of races - don't return until hosting thread has terminated */
301 WaitForSingleObject(thread
, INFINITE
);
305 /* tests failure case of interface not having a marshaler specified in the
307 static void test_no_marshaler(void)
312 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
313 ok_ole_success(hr
, CreateStreamOnHGlobal
);
314 hr
= CoMarshalInterface(pStream
, &IID_IWineTest
, (IUnknown
*)&Test_ClassFactory
, MSHCTX_INPROC
, NULL
, MSHLFLAGS_NORMAL
);
315 ok(hr
== E_NOINTERFACE
, "CoMarshalInterface should have returned E_NOINTERFACE instead of 0x%08x\n", hr
);
317 IStream_Release(pStream
);
320 /* tests normal marshal and then release without unmarshaling */
321 static void test_normal_marshal_and_release(void)
324 IStream
*pStream
= NULL
;
328 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
329 ok_ole_success(hr
, CreateStreamOnHGlobal
);
330 hr
= CoMarshalInterface(pStream
, &IID_IClassFactory
, (IUnknown
*)&Test_ClassFactory
, MSHCTX_INPROC
, NULL
, MSHLFLAGS_NORMAL
);
331 ok_ole_success(hr
, CoMarshalInterface
);
333 ok_more_than_one_lock();
335 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
336 hr
= CoReleaseMarshalData(pStream
);
337 ok_ole_success(hr
, CoReleaseMarshalData
);
338 IStream_Release(pStream
);
343 /* tests success case of a same-thread marshal and unmarshal */
344 static void test_normal_marshal_and_unmarshal(void)
347 IStream
*pStream
= NULL
;
348 IUnknown
*pProxy
= NULL
;
352 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
353 ok_ole_success(hr
, CreateStreamOnHGlobal
);
354 hr
= CoMarshalInterface(pStream
, &IID_IClassFactory
, (IUnknown
*)&Test_ClassFactory
, MSHCTX_INPROC
, NULL
, MSHLFLAGS_NORMAL
);
355 ok_ole_success(hr
, CoMarshalInterface
);
357 ok_more_than_one_lock();
359 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
360 hr
= CoUnmarshalInterface(pStream
, &IID_IClassFactory
, (void **)&pProxy
);
361 ok_ole_success(hr
, CoUnmarshalInterface
);
362 IStream_Release(pStream
);
364 ok_more_than_one_lock();
366 IUnknown_Release(pProxy
);
371 /* tests failure case of unmarshaling a freed object */
372 static void test_marshal_and_unmarshal_invalid(void)
375 IStream
*pStream
= NULL
;
376 IClassFactory
*pProxy
= NULL
;
383 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
384 ok_ole_success(hr
, CreateStreamOnHGlobal
);
385 tid
= start_host_object(pStream
, &IID_IClassFactory
, (IUnknown
*)&Test_ClassFactory
, MSHLFLAGS_NORMAL
, &thread
);
387 ok_more_than_one_lock();
389 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
390 hr
= CoReleaseMarshalData(pStream
);
391 ok_ole_success(hr
, CoReleaseMarshalData
);
395 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
396 hr
= CoUnmarshalInterface(pStream
, &IID_IClassFactory
, (void **)&pProxy
);
397 todo_wine
{ ok_ole_success(hr
, CoUnmarshalInterface
); }
403 hr
= IClassFactory_CreateInstance(pProxy
, NULL
, &IID_IUnknown
, &dummy
);
404 ok(hr
== RPC_E_DISCONNECTED
, "Remote call should have returned RPC_E_DISCONNECTED, instead of 0x%08x\n", hr
);
406 IClassFactory_Release(pProxy
);
409 IStream_Release(pStream
);
411 end_host_object(tid
, thread
);
414 static void test_same_apartment_unmarshal_failure(void)
419 static const LARGE_INTEGER llZero
;
423 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
424 ok_ole_success(hr
, CreateStreamOnHGlobal
);
426 hr
= CoMarshalInterface(pStream
, &IID_IUnknown
, (IUnknown
*)&Test_ClassFactory
, MSHCTX_INPROC
, NULL
, MSHLFLAGS_NORMAL
);
427 ok_ole_success(hr
, CoMarshalInterface
);
429 ok_more_than_one_lock();
431 hr
= IStream_Seek(pStream
, llZero
, STREAM_SEEK_SET
, NULL
);
432 ok_ole_success(hr
, IStream_Seek
);
434 hr
= CoUnmarshalInterface(pStream
, &IID_IParseDisplayName
, (void **)&pProxy
);
435 ok(hr
== E_NOINTERFACE
, "CoUnmarshalInterface should have returned E_NOINTERFACE instead of 0x%08x\n", hr
);
439 IStream_Release(pStream
);
442 /* tests success case of an interthread marshal */
443 static void test_interthread_marshal_and_unmarshal(void)
446 IStream
*pStream
= NULL
;
447 IUnknown
*pProxy
= NULL
;
453 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
454 ok_ole_success(hr
, CreateStreamOnHGlobal
);
455 tid
= start_host_object(pStream
, &IID_IClassFactory
, (IUnknown
*)&Test_ClassFactory
, MSHLFLAGS_NORMAL
, &thread
);
457 ok_more_than_one_lock();
459 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
460 hr
= CoUnmarshalInterface(pStream
, &IID_IClassFactory
, (void **)&pProxy
);
461 ok_ole_success(hr
, CoUnmarshalInterface
);
462 IStream_Release(pStream
);
464 ok_more_than_one_lock();
466 IUnknown_Release(pProxy
);
470 end_host_object(tid
, thread
);
473 /* the number of external references that Wine's proxy manager normally gives
474 * out, so we can test the border case of running out of references */
475 #define NORMALEXTREFS 5
477 /* tests success case of an interthread marshal and then marshaling the proxy */
478 static void test_proxy_marshal_and_unmarshal(void)
481 IStream
*pStream
= NULL
;
482 IUnknown
*pProxy
= NULL
;
483 IUnknown
*pProxy2
= 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
);
500 ok_more_than_one_lock();
502 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
503 /* marshal the proxy */
504 hr
= CoMarshalInterface(pStream
, &IID_IClassFactory
, pProxy
, MSHCTX_INPROC
, NULL
, MSHLFLAGS_NORMAL
);
505 ok_ole_success(hr
, CoMarshalInterface
);
507 ok_more_than_one_lock();
509 /* marshal 5 more times to exhaust the normal external references of 5 */
510 for (i
= 0; i
< NORMALEXTREFS
; i
++)
512 hr
= CoMarshalInterface(pStream
, &IID_IClassFactory
, pProxy
, MSHCTX_INPROC
, NULL
, MSHLFLAGS_NORMAL
);
513 ok_ole_success(hr
, CoMarshalInterface
);
516 ok_more_than_one_lock();
518 /* release the original proxy to test that we successfully keep the
519 * original object alive */
520 IUnknown_Release(pProxy
);
522 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
523 hr
= CoUnmarshalInterface(pStream
, &IID_IClassFactory
, (void **)&pProxy2
);
524 ok_ole_success(hr
, CoUnmarshalInterface
);
526 ok_more_than_one_lock();
528 IUnknown_Release(pProxy2
);
530 /* unmarshal all of the proxies to check that the object stub still exists */
531 for (i
= 0; i
< NORMALEXTREFS
; i
++)
533 hr
= CoUnmarshalInterface(pStream
, &IID_IClassFactory
, (void **)&pProxy2
);
534 ok_ole_success(hr
, CoUnmarshalInterface
);
536 IUnknown_Release(pProxy2
);
541 IStream_Release(pStream
);
543 end_host_object(tid
, thread
);
546 /* tests success case of an interthread marshal and then marshaling the proxy
547 * using an iid that hasn't previously been unmarshaled */
548 static void test_proxy_marshal_and_unmarshal2(void)
551 IStream
*pStream
= NULL
;
552 IUnknown
*pProxy
= NULL
;
553 IUnknown
*pProxy2
= NULL
;
559 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
560 ok_ole_success(hr
, CreateStreamOnHGlobal
);
561 tid
= start_host_object(pStream
, &IID_IUnknown
, (IUnknown
*)&Test_ClassFactory
, MSHLFLAGS_NORMAL
, &thread
);
563 ok_more_than_one_lock();
565 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
566 hr
= CoUnmarshalInterface(pStream
, &IID_IUnknown
, (void **)&pProxy
);
567 ok_ole_success(hr
, CoUnmarshalInterface
);
569 ok_more_than_one_lock();
571 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
572 /* marshal the proxy */
573 hr
= CoMarshalInterface(pStream
, &IID_IClassFactory
, pProxy
, MSHCTX_INPROC
, NULL
, MSHLFLAGS_NORMAL
);
574 ok_ole_success(hr
, CoMarshalInterface
);
576 ok_more_than_one_lock();
578 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
579 /* unmarshal the second proxy to the object */
580 hr
= CoUnmarshalInterface(pStream
, &IID_IClassFactory
, (void **)&pProxy2
);
581 ok_ole_success(hr
, CoUnmarshalInterface
);
582 IStream_Release(pStream
);
584 /* now the proxies should be as follows:
585 * pProxy -> &Test_ClassFactory
586 * pProxy2 -> &Test_ClassFactory
587 * they should NOT be as follows:
588 * pProxy -> &Test_ClassFactory
590 * the above can only really be tested by looking in +ole traces
593 ok_more_than_one_lock();
595 IUnknown_Release(pProxy
);
597 ok_more_than_one_lock();
599 IUnknown_Release(pProxy2
);
603 end_host_object(tid
, thread
);
606 /* tests success case of an interthread marshal and then table-weak-marshaling the proxy */
607 static void test_proxy_marshal_and_unmarshal_weak(void)
610 IStream
*pStream
= NULL
;
611 IUnknown
*pProxy
= NULL
;
612 IUnknown
*pProxy2
= NULL
;
618 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
619 ok_ole_success(hr
, CreateStreamOnHGlobal
);
620 tid
= start_host_object(pStream
, &IID_IClassFactory
, (IUnknown
*)&Test_ClassFactory
, MSHLFLAGS_NORMAL
, &thread
);
622 ok_more_than_one_lock();
624 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
625 hr
= CoUnmarshalInterface(pStream
, &IID_IClassFactory
, (void **)&pProxy
);
626 ok_ole_success(hr
, CoUnmarshalInterface
);
628 ok_more_than_one_lock();
630 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
631 /* marshal the proxy */
632 hr
= CoMarshalInterface(pStream
, &IID_IClassFactory
, pProxy
, MSHCTX_INPROC
, NULL
, MSHLFLAGS_TABLEWEAK
);
633 ok_ole_success(hr
, CoMarshalInterface
);
635 ok_more_than_one_lock();
637 /* release the original proxy to test that we successfully keep the
638 * original object alive */
639 IUnknown_Release(pProxy
);
641 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
642 hr
= CoUnmarshalInterface(pStream
, &IID_IClassFactory
, (void **)&pProxy2
);
644 ok(hr
== CO_E_OBJNOTREG
, "CoUnmarshalInterface should return CO_E_OBJNOTREG instead of 0x%08x\n", hr
);
648 IStream_Release(pStream
);
650 end_host_object(tid
, thread
);
653 /* tests success case of an interthread marshal and then table-strong-marshaling the proxy */
654 static void test_proxy_marshal_and_unmarshal_strong(void)
657 IStream
*pStream
= NULL
;
658 IUnknown
*pProxy
= NULL
;
659 IUnknown
*pProxy2
= NULL
;
665 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
666 ok_ole_success(hr
, CreateStreamOnHGlobal
);
667 tid
= start_host_object(pStream
, &IID_IClassFactory
, (IUnknown
*)&Test_ClassFactory
, MSHLFLAGS_NORMAL
, &thread
);
669 ok_more_than_one_lock();
671 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
672 hr
= CoUnmarshalInterface(pStream
, &IID_IClassFactory
, (void **)&pProxy
);
673 ok_ole_success(hr
, CoUnmarshalInterface
);
675 ok_more_than_one_lock();
677 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
678 /* marshal the proxy */
679 hr
= CoMarshalInterface(pStream
, &IID_IClassFactory
, pProxy
, MSHCTX_INPROC
, NULL
, MSHLFLAGS_TABLESTRONG
);
680 ok(hr
== S_OK
/* WinNT */ || hr
== E_INVALIDARG
/* Win9x */,
681 "CoMarshalInterface should have return S_OK or E_INVALIDARG instead of 0x%08x\n", hr
);
684 IUnknown_Release(pProxy
);
688 ok_more_than_one_lock();
690 /* release the original proxy to test that we successfully keep the
691 * original object alive */
692 IUnknown_Release(pProxy
);
694 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
695 hr
= CoUnmarshalInterface(pStream
, &IID_IClassFactory
, (void **)&pProxy2
);
696 ok_ole_success(hr
, CoUnmarshalInterface
);
698 ok_more_than_one_lock();
700 IUnknown_Release(pProxy2
);
702 ok_more_than_one_lock();
705 IStream_Release(pStream
);
707 end_host_object(tid
, thread
);
712 /* tests that stubs are released when the containing apartment is destroyed */
713 static void test_marshal_stub_apartment_shutdown(void)
716 IStream
*pStream
= NULL
;
717 IUnknown
*pProxy
= NULL
;
723 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
724 ok_ole_success(hr
, CreateStreamOnHGlobal
);
725 tid
= start_host_object(pStream
, &IID_IClassFactory
, (IUnknown
*)&Test_ClassFactory
, MSHLFLAGS_NORMAL
, &thread
);
727 ok_more_than_one_lock();
729 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
730 hr
= CoUnmarshalInterface(pStream
, &IID_IClassFactory
, (void **)&pProxy
);
731 ok_ole_success(hr
, CoUnmarshalInterface
);
732 IStream_Release(pStream
);
734 ok_more_than_one_lock();
736 end_host_object(tid
, thread
);
740 IUnknown_Release(pProxy
);
745 /* tests that proxies are released when the containing apartment is destroyed */
746 static void test_marshal_proxy_apartment_shutdown(void)
749 IStream
*pStream
= NULL
;
750 IUnknown
*pProxy
= NULL
;
756 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
757 ok_ole_success(hr
, CreateStreamOnHGlobal
);
758 tid
= start_host_object(pStream
, &IID_IClassFactory
, (IUnknown
*)&Test_ClassFactory
, MSHLFLAGS_NORMAL
, &thread
);
760 ok_more_than_one_lock();
762 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
763 hr
= CoUnmarshalInterface(pStream
, &IID_IClassFactory
, (void **)&pProxy
);
764 ok_ole_success(hr
, CoUnmarshalInterface
);
765 IStream_Release(pStream
);
767 ok_more_than_one_lock();
773 IUnknown_Release(pProxy
);
777 end_host_object(tid
, thread
);
779 pCoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
782 /* tests that proxies are released when the containing mta apartment is destroyed */
783 static void test_marshal_proxy_mta_apartment_shutdown(void)
786 IStream
*pStream
= NULL
;
787 IUnknown
*pProxy
= NULL
;
792 pCoInitializeEx(NULL
, COINIT_MULTITHREADED
);
796 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
797 ok_ole_success(hr
, CreateStreamOnHGlobal
);
798 tid
= start_host_object(pStream
, &IID_IClassFactory
, (IUnknown
*)&Test_ClassFactory
, MSHLFLAGS_NORMAL
, &thread
);
800 ok_more_than_one_lock();
802 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
803 hr
= CoUnmarshalInterface(pStream
, &IID_IClassFactory
, (void **)&pProxy
);
804 ok_ole_success(hr
, CoUnmarshalInterface
);
805 IStream_Release(pStream
);
807 ok_more_than_one_lock();
813 IUnknown_Release(pProxy
);
817 end_host_object(tid
, thread
);
819 pCoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
825 HANDLE marshal_event
;
826 HANDLE unmarshal_event
;
829 /* helper for test_no_couninitialize_server */
830 static DWORD CALLBACK
no_couninitialize_server_proc(LPVOID p
)
832 struct ncu_params
*ncu_params
= (struct ncu_params
*)p
;
835 pCoInitializeEx(NULL
, COINIT_MULTITHREADED
);
837 hr
= CoMarshalInterface(ncu_params
->stream
, &IID_IClassFactory
, (IUnknown
*)&Test_ClassFactory
, MSHCTX_INPROC
, NULL
, MSHLFLAGS_NORMAL
);
838 ok_ole_success(hr
, CoMarshalInterface
);
840 SetEvent(ncu_params
->marshal_event
);
842 WaitForSingleObject(ncu_params
->unmarshal_event
, INFINITE
);
844 /* die without calling CoUninitialize */
849 /* tests apartment that an apartment with a stub is released without deadlock
850 * if the owning thread exits */
851 static void test_no_couninitialize_server(void)
854 IStream
*pStream
= NULL
;
855 IUnknown
*pProxy
= NULL
;
858 struct ncu_params ncu_params
;
862 ncu_params
.marshal_event
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
863 ncu_params
.unmarshal_event
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
865 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
866 ok_ole_success(hr
, CreateStreamOnHGlobal
);
867 ncu_params
.stream
= pStream
;
869 thread
= CreateThread(NULL
, 0, no_couninitialize_server_proc
, &ncu_params
, 0, &tid
);
871 WaitForSingleObject(ncu_params
.marshal_event
, INFINITE
);
872 ok_more_than_one_lock();
874 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
875 hr
= CoUnmarshalInterface(pStream
, &IID_IClassFactory
, (void **)&pProxy
);
876 ok_ole_success(hr
, CoUnmarshalInterface
);
877 IStream_Release(pStream
);
879 ok_more_than_one_lock();
881 SetEvent(ncu_params
.unmarshal_event
);
882 WaitForSingleObject(thread
, INFINITE
);
887 CloseHandle(ncu_params
.marshal_event
);
888 CloseHandle(ncu_params
.unmarshal_event
);
890 IUnknown_Release(pProxy
);
895 /* STA -> STA call during DLL_THREAD_DETACH */
896 static DWORD CALLBACK
no_couninitialize_client_proc(LPVOID p
)
898 struct ncu_params
*ncu_params
= (struct ncu_params
*)p
;
900 IUnknown
*pProxy
= NULL
;
902 pCoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
904 hr
= CoUnmarshalInterface(ncu_params
->stream
, &IID_IClassFactory
, (void **)&pProxy
);
905 ok_ole_success(hr
, CoUnmarshalInterface
);
906 IStream_Release(ncu_params
->stream
);
908 ok_more_than_one_lock();
910 /* die without calling CoUninitialize */
915 /* tests STA -> STA call during DLL_THREAD_DETACH doesn't deadlock */
916 static void test_no_couninitialize_client(void)
919 IStream
*pStream
= NULL
;
924 struct ncu_params ncu_params
;
928 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
929 ok_ole_success(hr
, CreateStreamOnHGlobal
);
930 ncu_params
.stream
= pStream
;
932 /* NOTE: assumes start_host_object uses an STA to host the object, as MTAs
933 * always deadlock when called from within DllMain */
934 host_tid
= start_host_object(pStream
, &IID_IClassFactory
, (IUnknown
*)&Test_ClassFactory
, MSHLFLAGS_NORMAL
, &host_thread
);
935 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
937 ok_more_than_one_lock();
939 thread
= CreateThread(NULL
, 0, no_couninitialize_client_proc
, &ncu_params
, 0, &tid
);
941 WaitForSingleObject(thread
, INFINITE
);
946 end_host_object(host_tid
, host_thread
);
949 /* tests success case of a same-thread table-weak marshal, unmarshal, unmarshal */
950 static void test_tableweak_marshal_and_unmarshal_twice(void)
953 IStream
*pStream
= NULL
;
954 IUnknown
*pProxy1
= NULL
;
955 IUnknown
*pProxy2
= NULL
;
961 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
962 ok_ole_success(hr
, CreateStreamOnHGlobal
);
963 tid
= start_host_object(pStream
, &IID_IClassFactory
, (IUnknown
*)&Test_ClassFactory
, MSHLFLAGS_TABLEWEAK
, &thread
);
965 ok_more_than_one_lock();
967 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
968 hr
= CoUnmarshalInterface(pStream
, &IID_IClassFactory
, (void **)&pProxy1
);
969 ok_ole_success(hr
, CoUnmarshalInterface
);
971 ok_more_than_one_lock();
973 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
974 hr
= CoUnmarshalInterface(pStream
, &IID_IClassFactory
, (void **)&pProxy2
);
975 IStream_Release(pStream
);
976 ok_ole_success(hr
, CoUnmarshalInterface
);
978 ok_more_than_one_lock();
980 IUnknown_Release(pProxy1
);
981 IUnknown_Release(pProxy2
);
983 /* this line is shows the difference between weak and strong table marshaling:
984 * weak has cLocks == 0
985 * strong has cLocks > 0 */
988 end_host_object(tid
, thread
);
991 /* tests releasing after unmarshaling one object */
992 static void test_tableweak_marshal_releasedata1(void)
995 IStream
*pStream
= NULL
;
996 IUnknown
*pProxy1
= NULL
;
997 IUnknown
*pProxy2
= NULL
;
1003 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
1004 ok_ole_success(hr
, CreateStreamOnHGlobal
);
1005 tid
= start_host_object(pStream
, &IID_IClassFactory
, (IUnknown
*)&Test_ClassFactory
, MSHLFLAGS_TABLEWEAK
, &thread
);
1007 ok_more_than_one_lock();
1009 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
1010 hr
= CoUnmarshalInterface(pStream
, &IID_IClassFactory
, (void **)&pProxy1
);
1011 ok_ole_success(hr
, CoUnmarshalInterface
);
1013 ok_more_than_one_lock();
1015 /* release the remaining reference on the object by calling
1016 * CoReleaseMarshalData in the hosting thread */
1017 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
1018 release_host_object(tid
);
1020 ok_more_than_one_lock();
1022 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
1023 hr
= CoUnmarshalInterface(pStream
, &IID_IClassFactory
, (void **)&pProxy2
);
1024 ok_ole_success(hr
, CoUnmarshalInterface
);
1025 IStream_Release(pStream
);
1027 ok_more_than_one_lock();
1029 IUnknown_Release(pProxy1
);
1031 IUnknown_Release(pProxy2
);
1033 /* this line is shows the difference between weak and strong table marshaling:
1034 * weak has cLocks == 0
1035 * strong has cLocks > 0 */
1038 end_host_object(tid
, thread
);
1041 /* tests releasing after unmarshaling one object */
1042 static void test_tableweak_marshal_releasedata2(void)
1045 IStream
*pStream
= NULL
;
1046 IUnknown
*pProxy
= NULL
;
1052 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
1053 ok_ole_success(hr
, CreateStreamOnHGlobal
);
1054 tid
= start_host_object(pStream
, &IID_IClassFactory
, (IUnknown
*)&Test_ClassFactory
, MSHLFLAGS_TABLEWEAK
, &thread
);
1056 ok_more_than_one_lock();
1058 /* release the remaining reference on the object by calling
1059 * CoReleaseMarshalData in the hosting thread */
1060 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
1061 release_host_object(tid
);
1065 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
1066 hr
= CoUnmarshalInterface(pStream
, &IID_IClassFactory
, (void **)&pProxy
);
1069 ok(hr
== CO_E_OBJNOTREG
,
1070 "CoUnmarshalInterface should have failed with CO_E_OBJNOTREG, but returned 0x%08x instead\n",
1073 IStream_Release(pStream
);
1077 end_host_object(tid
, thread
);
1080 /* tests success case of a same-thread table-strong marshal, unmarshal, unmarshal */
1081 static void test_tablestrong_marshal_and_unmarshal_twice(void)
1084 IStream
*pStream
= NULL
;
1085 IUnknown
*pProxy1
= NULL
;
1086 IUnknown
*pProxy2
= NULL
;
1092 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
1093 ok_ole_success(hr
, CreateStreamOnHGlobal
);
1094 tid
= start_host_object(pStream
, &IID_IClassFactory
, (IUnknown
*)&Test_ClassFactory
, MSHLFLAGS_TABLESTRONG
, &thread
);
1096 ok_more_than_one_lock();
1098 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
1099 hr
= CoUnmarshalInterface(pStream
, &IID_IClassFactory
, (void **)&pProxy1
);
1100 ok_ole_success(hr
, CoUnmarshalInterface
);
1102 ok_more_than_one_lock();
1104 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
1105 hr
= CoUnmarshalInterface(pStream
, &IID_IClassFactory
, (void **)&pProxy2
);
1106 ok_ole_success(hr
, CoUnmarshalInterface
);
1108 ok_more_than_one_lock();
1110 if (pProxy1
) IUnknown_Release(pProxy1
);
1111 if (pProxy2
) IUnknown_Release(pProxy2
);
1113 /* this line is shows the difference between weak and strong table marshaling:
1114 * weak has cLocks == 0
1115 * strong has cLocks > 0 */
1116 ok_more_than_one_lock();
1118 /* release the remaining reference on the object by calling
1119 * CoReleaseMarshalData in the hosting thread */
1120 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
1121 release_host_object(tid
);
1122 IStream_Release(pStream
);
1126 end_host_object(tid
, thread
);
1129 /* tests CoLockObjectExternal */
1130 static void test_lock_object_external(void)
1133 IStream
*pStream
= NULL
;
1137 /* test the stub manager creation aspect of CoLockObjectExternal when the
1138 * object hasn't been marshaled yet */
1139 CoLockObjectExternal((IUnknown
*)&Test_ClassFactory
, TRUE
, TRUE
);
1141 ok_more_than_one_lock();
1143 CoDisconnectObject((IUnknown
*)&Test_ClassFactory
, 0);
1147 /* test our empty stub manager being handled correctly in
1148 * CoMarshalInterface */
1149 CoLockObjectExternal((IUnknown
*)&Test_ClassFactory
, TRUE
, TRUE
);
1151 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
1152 ok_ole_success(hr
, CreateStreamOnHGlobal
);
1153 hr
= CoMarshalInterface(pStream
, &IID_IClassFactory
, (IUnknown
*)&Test_ClassFactory
, MSHCTX_INPROC
, NULL
, MSHLFLAGS_NORMAL
);
1154 ok_ole_success(hr
, CoMarshalInterface
);
1156 CoLockObjectExternal((IUnknown
*)&Test_ClassFactory
, TRUE
, TRUE
);
1158 ok_more_than_one_lock();
1160 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
1161 hr
= CoReleaseMarshalData(pStream
);
1162 ok_ole_success(hr
, CoReleaseMarshalData
);
1163 IStream_Release(pStream
);
1165 ok_more_than_one_lock();
1167 CoLockObjectExternal((IUnknown
*)&Test_ClassFactory
, FALSE
, TRUE
);
1169 ok_more_than_one_lock();
1171 CoLockObjectExternal((IUnknown
*)&Test_ClassFactory
, FALSE
, TRUE
);
1176 /* tests disconnecting stubs */
1177 static void test_disconnect_stub(void)
1180 IStream
*pStream
= NULL
;
1184 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
1185 ok_ole_success(hr
, CreateStreamOnHGlobal
);
1186 hr
= CoMarshalInterface(pStream
, &IID_IClassFactory
, (IUnknown
*)&Test_ClassFactory
, MSHCTX_INPROC
, NULL
, MSHLFLAGS_NORMAL
);
1187 ok_ole_success(hr
, CoMarshalInterface
);
1189 CoLockObjectExternal((IUnknown
*)&Test_ClassFactory
, TRUE
, TRUE
);
1191 ok_more_than_one_lock();
1193 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
1194 hr
= CoReleaseMarshalData(pStream
);
1195 ok_ole_success(hr
, CoReleaseMarshalData
);
1196 IStream_Release(pStream
);
1198 ok_more_than_one_lock();
1200 CoDisconnectObject((IUnknown
*)&Test_ClassFactory
, 0);
1205 /* tests failure case of a same-thread marshal and unmarshal twice */
1206 static void test_normal_marshal_and_unmarshal_twice(void)
1209 IStream
*pStream
= NULL
;
1210 IUnknown
*pProxy1
= NULL
;
1211 IUnknown
*pProxy2
= 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 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
1223 hr
= CoUnmarshalInterface(pStream
, &IID_IClassFactory
, (void **)&pProxy1
);
1224 ok_ole_success(hr
, CoUnmarshalInterface
);
1226 ok_more_than_one_lock();
1228 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
1229 hr
= CoUnmarshalInterface(pStream
, &IID_IClassFactory
, (void **)&pProxy2
);
1230 ok(hr
== CO_E_OBJNOTCONNECTED
,
1231 "CoUnmarshalInterface should have failed with error CO_E_OBJNOTCONNECTED for double unmarshal, instead of 0x%08x\n", hr
);
1233 IStream_Release(pStream
);
1235 ok_more_than_one_lock();
1237 IUnknown_Release(pProxy1
);
1242 /* tests success case of marshaling and unmarshaling an HRESULT */
1243 static void test_hresult_marshaling(void)
1246 HRESULT hr_marshaled
= 0;
1247 IStream
*pStream
= NULL
;
1248 static const HRESULT E_DEADBEEF
= 0xdeadbeef;
1250 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
1251 ok_ole_success(hr
, CreateStreamOnHGlobal
);
1253 hr
= CoMarshalHresult(pStream
, E_DEADBEEF
);
1254 ok_ole_success(hr
, CoMarshalHresult
);
1256 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
1257 hr
= IStream_Read(pStream
, &hr_marshaled
, sizeof(HRESULT
), NULL
);
1258 ok_ole_success(hr
, IStream_Read
);
1260 ok(hr_marshaled
== E_DEADBEEF
, "Didn't marshal HRESULT as expected: got value 0x%08x instead\n", hr_marshaled
);
1263 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
1264 hr
= CoUnmarshalHresult(pStream
, &hr_marshaled
);
1265 ok_ole_success(hr
, CoUnmarshalHresult
);
1267 ok(hr_marshaled
== E_DEADBEEF
, "Didn't marshal HRESULT as expected: got value 0x%08x instead\n", hr_marshaled
);
1269 IStream_Release(pStream
);
1273 /* helper for test_proxy_used_in_wrong_thread */
1274 static DWORD CALLBACK
bad_thread_proc(LPVOID p
)
1276 IClassFactory
* cf
= (IClassFactory
*)p
;
1278 IUnknown
* proxy
= NULL
;
1280 hr
= IClassFactory_CreateInstance(cf
, NULL
, &IID_IUnknown
, (LPVOID
*)&proxy
);
1282 ok(hr
== CO_E_NOTINITIALIZED
,
1283 "COM should have failed with CO_E_NOTINITIALIZED on using proxy without apartment, but instead returned 0x%08x\n",
1286 hr
= IClassFactory_QueryInterface(cf
, &IID_IMultiQI
, (LPVOID
*)&proxy
);
1287 /* Win9x returns S_OK, whilst NT returns RPC_E_WRONG_THREAD */
1288 trace("call to proxy's QueryInterface for local interface without apartment returned 0x%08x\n", hr
);
1290 IUnknown_Release(proxy
);
1292 hr
= IClassFactory_QueryInterface(cf
, &IID_IStream
, (LPVOID
*)&proxy
);
1293 /* Win9x returns E_NOINTERFACE, whilst NT returns RPC_E_WRONG_THREAD */
1294 trace("call to proxy's QueryInterface without apartment returned 0x%08x\n", hr
);
1296 IUnknown_Release(proxy
);
1298 pCoInitializeEx(NULL
, COINIT_MULTITHREADED
);
1300 hr
= IClassFactory_CreateInstance(cf
, NULL
, &IID_IUnknown
, (LPVOID
*)&proxy
);
1301 if (proxy
) IUnknown_Release(proxy
);
1302 ok(hr
== RPC_E_WRONG_THREAD
,
1303 "COM should have failed with RPC_E_WRONG_THREAD on using proxy from wrong apartment, but instead returned 0x%08x\n",
1306 hr
= IClassFactory_QueryInterface(cf
, &IID_IStream
, (LPVOID
*)&proxy
);
1307 /* Win9x returns E_NOINTERFACE, whilst NT returns RPC_E_WRONG_THREAD */
1308 trace("call to proxy's QueryInterface from wrong apartment returned 0x%08x\n", hr
);
1310 /* this statement causes Win9x DCOM to crash during CoUninitialize of
1311 * other apartment, so don't test this on Win9x (signified by NT-only
1312 * export of CoRegisterSurrogateEx) */
1313 if (GetProcAddress(GetModuleHandle("ole32"), "CoRegisterSurrogateEx"))
1314 /* now be really bad and release the proxy from the wrong apartment */
1315 IUnknown_Release(cf
);
1317 skip("skipping test for releasing proxy from wrong apartment that will succeed, but cause a crash during CoUninitialize\n");
1324 /* tests failure case of a using a proxy in the wrong apartment */
1325 static void test_proxy_used_in_wrong_thread(void)
1328 IStream
*pStream
= NULL
;
1329 IUnknown
*pProxy
= NULL
;
1336 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
1337 ok_ole_success(hr
, CreateStreamOnHGlobal
);
1338 tid
= start_host_object(pStream
, &IID_IClassFactory
, (IUnknown
*)&Test_ClassFactory
, MSHLFLAGS_NORMAL
, &host_thread
);
1340 ok_more_than_one_lock();
1342 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
1343 hr
= CoUnmarshalInterface(pStream
, &IID_IClassFactory
, (void **)&pProxy
);
1344 ok_ole_success(hr
, CoUnmarshalInterface
);
1345 IStream_Release(pStream
);
1347 ok_more_than_one_lock();
1349 /* do a call that will fail, but result in IRemUnknown being used by the proxy */
1350 IClassFactory_QueryInterface(pProxy
, &IID_IStream
, (LPVOID
*)&pStream
);
1352 /* create a thread that we can misbehave in */
1353 thread
= CreateThread(NULL
, 0, bad_thread_proc
, (LPVOID
)pProxy
, 0, &tid2
);
1355 WaitForSingleObject(thread
, INFINITE
);
1356 CloseHandle(thread
);
1358 /* do release statement on Win9x that we should have done above */
1359 if (!GetProcAddress(GetModuleHandle("ole32"), "CoRegisterSurrogateEx"))
1360 IUnknown_Release(pProxy
);
1364 end_host_object(tid
, host_thread
);
1367 static HRESULT WINAPI
MessageFilter_QueryInterface(IMessageFilter
*iface
, REFIID riid
, void ** ppvObj
)
1369 if (ppvObj
== NULL
) return E_POINTER
;
1371 if (IsEqualGUID(riid
, &IID_IUnknown
) ||
1372 IsEqualGUID(riid
, &IID_IClassFactory
))
1374 *ppvObj
= (LPVOID
)iface
;
1375 IClassFactory_AddRef(iface
);
1379 return E_NOINTERFACE
;
1382 static ULONG WINAPI
MessageFilter_AddRef(IMessageFilter
*iface
)
1384 return 2; /* non-heap object */
1387 static ULONG WINAPI
MessageFilter_Release(IMessageFilter
*iface
)
1389 return 1; /* non-heap object */
1392 static DWORD WINAPI
MessageFilter_HandleInComingCall(
1393 IMessageFilter
*iface
,
1395 HTASK threadIDCaller
,
1397 LPINTERFACEINFO lpInterfaceInfo
)
1399 static int callcount
= 0;
1401 trace("HandleInComingCall\n");
1405 ret
= SERVERCALL_REJECTED
;
1408 ret
= SERVERCALL_RETRYLATER
;
1411 ret
= SERVERCALL_ISHANDLED
;
1418 static DWORD WINAPI
MessageFilter_RetryRejectedCall(
1419 IMessageFilter
*iface
,
1420 HTASK threadIDCallee
,
1424 trace("RetryRejectedCall\n");
1428 static DWORD WINAPI
MessageFilter_MessagePending(
1429 IMessageFilter
*iface
,
1430 HTASK threadIDCallee
,
1432 DWORD dwPendingType
)
1434 trace("MessagePending\n");
1435 return PENDINGMSG_WAITNOPROCESS
;
1438 static const IMessageFilterVtbl MessageFilter_Vtbl
=
1440 MessageFilter_QueryInterface
,
1441 MessageFilter_AddRef
,
1442 MessageFilter_Release
,
1443 MessageFilter_HandleInComingCall
,
1444 MessageFilter_RetryRejectedCall
,
1445 MessageFilter_MessagePending
1448 static IMessageFilter MessageFilter
= { &MessageFilter_Vtbl
};
1450 static void test_message_filter(void)
1453 IStream
*pStream
= NULL
;
1454 IClassFactory
*cf
= NULL
;
1456 IUnknown
*proxy
= NULL
;
1457 IMessageFilter
*prev_filter
= NULL
;
1462 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
1463 ok_ole_success(hr
, CreateStreamOnHGlobal
);
1464 tid
= start_host_object2(pStream
, &IID_IClassFactory
, (IUnknown
*)&Test_ClassFactory
, MSHLFLAGS_NORMAL
, &MessageFilter
, &thread
);
1466 ok_more_than_one_lock();
1468 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
1469 hr
= CoUnmarshalInterface(pStream
, &IID_IClassFactory
, (void **)&cf
);
1470 ok_ole_success(hr
, CoUnmarshalInterface
);
1471 IStream_Release(pStream
);
1473 ok_more_than_one_lock();
1475 hr
= IClassFactory_CreateInstance(cf
, NULL
, &IID_IUnknown
, (LPVOID
*)&proxy
);
1476 ok(hr
== RPC_E_CALL_REJECTED
, "Call should have returned RPC_E_CALL_REJECTED, but return 0x%08x instead\n", hr
);
1477 if (proxy
) IUnknown_Release(proxy
);
1480 hr
= CoRegisterMessageFilter(&MessageFilter
, &prev_filter
);
1481 ok_ole_success(hr
, CoRegisterMessageFilter
);
1483 hr
= IClassFactory_CreateInstance(cf
, NULL
, &IID_IUnknown
, (LPVOID
*)&proxy
);
1484 ok_ole_success(hr
, IClassFactory_CreateInstance
);
1486 IUnknown_Release(proxy
);
1488 IClassFactory_Release(cf
);
1492 end_host_object(tid
, thread
);
1494 hr
= CoRegisterMessageFilter(prev_filter
, NULL
);
1495 ok_ole_success(hr
, CoRegisterMessageFilter
);
1498 /* test failure case of trying to unmarshal from bad stream */
1499 static void test_bad_marshal_stream(void)
1502 IStream
*pStream
= NULL
;
1504 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
1505 ok_ole_success(hr
, CreateStreamOnHGlobal
);
1506 hr
= CoMarshalInterface(pStream
, &IID_IClassFactory
, (IUnknown
*)&Test_ClassFactory
, MSHCTX_INPROC
, NULL
, MSHLFLAGS_NORMAL
);
1507 ok_ole_success(hr
, CoMarshalInterface
);
1509 ok_more_than_one_lock();
1511 /* try to read beyond end of stream */
1512 hr
= CoReleaseMarshalData(pStream
);
1513 ok(hr
== STG_E_READFAULT
, "Should have failed with STG_E_READFAULT, but returned 0x%08x instead\n", hr
);
1515 /* now release for real */
1516 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
1517 hr
= CoReleaseMarshalData(pStream
);
1518 ok_ole_success(hr
, CoReleaseMarshalData
);
1520 IStream_Release(pStream
);
1523 /* tests that proxies implement certain interfaces */
1524 static void test_proxy_interfaces(void)
1527 IStream
*pStream
= NULL
;
1528 IUnknown
*pProxy
= NULL
;
1529 IUnknown
*pOtherUnknown
= NULL
;
1535 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
1536 ok_ole_success(hr
, CreateStreamOnHGlobal
);
1537 tid
= start_host_object(pStream
, &IID_IClassFactory
, (IUnknown
*)&Test_ClassFactory
, MSHLFLAGS_NORMAL
, &thread
);
1539 ok_more_than_one_lock();
1541 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
1542 hr
= CoUnmarshalInterface(pStream
, &IID_IUnknown
, (void **)&pProxy
);
1543 ok_ole_success(hr
, CoUnmarshalInterface
);
1544 IStream_Release(pStream
);
1546 ok_more_than_one_lock();
1548 hr
= IUnknown_QueryInterface(pProxy
, &IID_IUnknown
, (LPVOID
*)&pOtherUnknown
);
1549 ok_ole_success(hr
, IUnknown_QueryInterface IID_IUnknown
);
1550 if (hr
== S_OK
) IUnknown_Release(pOtherUnknown
);
1552 hr
= IUnknown_QueryInterface(pProxy
, &IID_IClientSecurity
, (LPVOID
*)&pOtherUnknown
);
1553 ok_ole_success(hr
, IUnknown_QueryInterface IID_IClientSecurity
);
1554 if (hr
== S_OK
) IUnknown_Release(pOtherUnknown
);
1556 hr
= IUnknown_QueryInterface(pProxy
, &IID_IMultiQI
, (LPVOID
*)&pOtherUnknown
);
1557 ok_ole_success(hr
, IUnknown_QueryInterface IID_IMultiQI
);
1558 if (hr
== S_OK
) IUnknown_Release(pOtherUnknown
);
1560 hr
= IUnknown_QueryInterface(pProxy
, &IID_IMarshal
, (LPVOID
*)&pOtherUnknown
);
1561 ok_ole_success(hr
, IUnknown_QueryInterface IID_IMarshal
);
1562 if (hr
== S_OK
) IUnknown_Release(pOtherUnknown
);
1564 /* IMarshal2 is also supported on NT-based systems, but is pretty much
1565 * useless as it has no more methods over IMarshal that it inherits from. */
1567 IUnknown_Release(pProxy
);
1571 end_host_object(tid
, thread
);
1576 const IUnknownVtbl
*lpVtbl
;
1580 static HRESULT WINAPI
HeapUnknown_QueryInterface(IUnknown
*iface
, REFIID riid
, void **ppv
)
1582 if (IsEqualIID(riid
, &IID_IUnknown
))
1584 IUnknown_AddRef(iface
);
1585 *ppv
= (LPVOID
)iface
;
1589 return E_NOINTERFACE
;
1592 static ULONG WINAPI
HeapUnknown_AddRef(IUnknown
*iface
)
1594 HeapUnknown
*This
= (HeapUnknown
*)iface
;
1595 return InterlockedIncrement((LONG
*)&This
->refs
);
1598 static ULONG WINAPI
HeapUnknown_Release(IUnknown
*iface
)
1600 HeapUnknown
*This
= (HeapUnknown
*)iface
;
1601 ULONG refs
= InterlockedDecrement((LONG
*)&This
->refs
);
1602 if (!refs
) HeapFree(GetProcessHeap(), 0, This
);
1606 static const IUnknownVtbl HeapUnknown_Vtbl
=
1608 HeapUnknown_QueryInterface
,
1613 static void test_proxybuffer(REFIID riid
)
1616 IPSFactoryBuffer
*psfb
;
1617 IRpcProxyBuffer
*proxy
;
1621 HeapUnknown
*pUnkOuter
= HeapAlloc(GetProcessHeap(), 0, sizeof(*pUnkOuter
));
1623 pUnkOuter
->lpVtbl
= &HeapUnknown_Vtbl
;
1624 pUnkOuter
->refs
= 1;
1626 hr
= CoGetPSClsid(riid
, &clsid
);
1627 ok_ole_success(hr
, CoGetPSClsid
);
1629 hr
= CoGetClassObject(&clsid
, CLSCTX_INPROC_SERVER
, NULL
, &IID_IPSFactoryBuffer
, (LPVOID
*)&psfb
);
1630 ok_ole_success(hr
, CoGetClassObject
);
1632 hr
= IPSFactoryBuffer_CreateProxy(psfb
, (IUnknown
*)pUnkOuter
, riid
, &proxy
, &lpvtbl
);
1633 ok_ole_success(hr
, IPSFactoryBuffer_CreateProxy
);
1634 ok(lpvtbl
!= NULL
, "IPSFactoryBuffer_CreateProxy succeeded, but returned a NULL vtable!\n");
1636 /* release our reference to the outer unknown object - the PS factory
1637 * buffer will have AddRef's it in the CreateProxy call */
1638 refs
= IUnknown_Release((IUnknown
*)pUnkOuter
);
1639 ok(refs
== 1, "Ref count of outer unknown should have been 1 instead of %d\n", refs
);
1641 refs
= IPSFactoryBuffer_Release(psfb
);
1644 /* not reliable on native. maybe it leaks references! */
1645 ok(refs
== 0, "Ref-count leak of %d on IPSFactoryBuffer\n", refs
);
1648 refs
= IUnknown_Release((IUnknown
*)lpvtbl
);
1649 ok(refs
== 0, "Ref-count leak of %d on IRpcProxyBuffer\n", refs
);
1651 refs
= IRpcProxyBuffer_Release(proxy
);
1652 ok(refs
== 0, "Ref-count leak of %d on IRpcProxyBuffer\n", refs
);
1655 static void test_stubbuffer(REFIID riid
)
1658 IPSFactoryBuffer
*psfb
;
1659 IRpcStubBuffer
*stub
;
1665 hr
= CoGetPSClsid(riid
, &clsid
);
1666 ok_ole_success(hr
, CoGetPSClsid
);
1668 hr
= CoGetClassObject(&clsid
, CLSCTX_INPROC_SERVER
, NULL
, &IID_IPSFactoryBuffer
, (LPVOID
*)&psfb
);
1669 ok_ole_success(hr
, CoGetClassObject
);
1671 hr
= IPSFactoryBuffer_CreateStub(psfb
, riid
, (IUnknown
*)&Test_ClassFactory
, &stub
);
1672 ok_ole_success(hr
, IPSFactoryBuffer_CreateStub
);
1674 refs
= IPSFactoryBuffer_Release(psfb
);
1677 /* not reliable on native. maybe it leaks references */
1678 ok(refs
== 0, "Ref-count leak of %d on IPSFactoryBuffer\n", refs
);
1681 ok_more_than_one_lock();
1683 IRpcStubBuffer_Disconnect(stub
);
1687 refs
= IRpcStubBuffer_Release(stub
);
1688 ok(refs
== 0, "Ref-count leak of %d on IRpcProxyBuffer\n", refs
);
1691 static HWND hwnd_app
;
1693 static HRESULT WINAPI
TestRE_IClassFactory_CreateInstance(
1694 LPCLASSFACTORY iface
,
1695 LPUNKNOWN pUnkOuter
,
1700 if (IsEqualIID(riid
, &IID_IWineTest
))
1702 BOOL ret
= SendMessageTimeout(hwnd_app
, WM_NULL
, 0, 0, SMTO_BLOCK
, 5000, &res
);
1703 ok(ret
, "Timed out sending a message to originating window during RPC call\n");
1708 static const IClassFactoryVtbl TestREClassFactory_Vtbl
=
1710 Test_IClassFactory_QueryInterface
,
1711 Test_IClassFactory_AddRef
,
1712 Test_IClassFactory_Release
,
1713 TestRE_IClassFactory_CreateInstance
,
1714 Test_IClassFactory_LockServer
1717 IClassFactory TestRE_ClassFactory
= { &TestREClassFactory_Vtbl
};
1719 static LRESULT CALLBACK
window_proc(HWND hwnd
, UINT msg
, WPARAM wparam
, LPARAM lparam
)
1726 IStream
*pStream
= NULL
;
1727 IClassFactory
*proxy
= NULL
;
1734 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
1735 ok_ole_success(hr
, CreateStreamOnHGlobal
);
1736 tid
= start_host_object(pStream
, &IID_IClassFactory
, (IUnknown
*)&TestRE_ClassFactory
, MSHLFLAGS_NORMAL
, &thread
);
1738 ok_more_than_one_lock();
1740 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
1741 hr
= CoUnmarshalInterface(pStream
, &IID_IClassFactory
, (void **)&proxy
);
1742 ok_ole_success(hr
, CoReleaseMarshalData
);
1743 IStream_Release(pStream
);
1745 ok_more_than_one_lock();
1747 /* note the use of the magic IID_IWineTest value to tell remote thread
1748 * to try to send a message back to us */
1749 hr
= IClassFactory_CreateInstance(proxy
, NULL
, &IID_IWineTest
, (void **)&object
);
1751 IClassFactory_Release(proxy
);
1755 end_host_object(tid
, thread
);
1757 PostMessage(hwnd
, WM_QUIT
, 0, 0);
1764 IStream
*pStream
= NULL
;
1765 IClassFactory
*proxy
= NULL
;
1772 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
1773 ok_ole_success(hr
, CreateStreamOnHGlobal
);
1774 tid
= start_host_object(pStream
, &IID_IClassFactory
, (IUnknown
*)&TestRE_ClassFactory
, MSHLFLAGS_NORMAL
, &thread
);
1776 ok_more_than_one_lock();
1778 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
1779 hr
= CoUnmarshalInterface(pStream
, &IID_IClassFactory
, (void **)&proxy
);
1780 ok_ole_success(hr
, CoReleaseMarshalData
);
1781 IStream_Release(pStream
);
1783 ok_more_than_one_lock();
1785 /* post quit message before a doing a COM call to show that a pending
1786 * WM_QUIT message doesn't stop the call from succeeding */
1787 PostMessage(hwnd
, WM_QUIT
, 0, 0);
1788 hr
= IClassFactory_CreateInstance(proxy
, NULL
, &IID_IUnknown
, (void **)&object
);
1790 IClassFactory_Release(proxy
);
1794 end_host_object(tid
, thread
);
1801 IStream
*pStream
= NULL
;
1802 IClassFactory
*proxy
= NULL
;
1807 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
1808 ok_ole_success(hr
, CreateStreamOnHGlobal
);
1809 tid
= start_host_object(pStream
, &IID_IClassFactory
, (IUnknown
*)&Test_ClassFactory
, MSHLFLAGS_NORMAL
, &thread
);
1811 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
1812 hr
= CoUnmarshalInterface(pStream
, &IID_IClassFactory
, (void **)&proxy
);
1813 ok_ole_success(hr
, CoReleaseMarshalData
);
1814 IStream_Release(pStream
);
1816 /* shows that COM calls executed during the processing of sent
1817 * messages should fail */
1818 hr
= IClassFactory_CreateInstance(proxy
, NULL
, &IID_IUnknown
, (void **)&object
);
1819 ok(hr
== RPC_E_CANTCALLOUT_ININPUTSYNCCALL
,
1820 "COM call during processing of sent message should return RPC_E_CANTCALLOUT_ININPUTSYNCCALL instead of 0x%08x\n", hr
);
1822 IClassFactory_Release(proxy
);
1824 end_host_object(tid
, thread
);
1831 return DefWindowProc(hwnd
, msg
, wparam
, lparam
);
1835 static void register_test_window(void)
1839 memset(&wndclass
, 0, sizeof(wndclass
));
1840 wndclass
.lpfnWndProc
= window_proc
;
1841 wndclass
.lpszClassName
= "WineCOMTest";
1842 RegisterClass(&wndclass
);
1845 static void test_message_reentrancy(void)
1849 hwnd_app
= CreateWindow("WineCOMTest", NULL
, 0, CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
, NULL
, NULL
, NULL
, 0);
1850 ok(hwnd_app
!= NULL
, "Window creation failed\n");
1852 /* start message re-entrancy test */
1853 PostMessage(hwnd_app
, WM_USER
, 0, 0);
1855 while (GetMessage(&msg
, NULL
, 0, 0))
1857 TranslateMessage(&msg
);
1858 DispatchMessage(&msg
);
1860 DestroyWindow(hwnd_app
);
1863 static HRESULT WINAPI
TestMsg_IClassFactory_CreateInstance(
1864 LPCLASSFACTORY iface
,
1865 LPUNKNOWN pUnkOuter
,
1870 SendMessage(hwnd_app
, WM_USER
+2, 0, 0);
1874 static IClassFactoryVtbl TestMsgClassFactory_Vtbl
=
1876 Test_IClassFactory_QueryInterface
,
1877 Test_IClassFactory_AddRef
,
1878 Test_IClassFactory_Release
,
1879 TestMsg_IClassFactory_CreateInstance
,
1880 Test_IClassFactory_LockServer
1883 IClassFactory TestMsg_ClassFactory
= { &TestMsgClassFactory_Vtbl
};
1885 static void test_call_from_message(void)
1890 IClassFactory
*proxy
;
1895 hwnd_app
= CreateWindow("WineCOMTest", NULL
, 0, CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
, NULL
, NULL
, NULL
, 0);
1896 ok(hwnd_app
!= NULL
, "Window creation failed\n");
1898 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
1899 ok_ole_success(hr
, CreateStreamOnHGlobal
);
1900 tid
= start_host_object(pStream
, &IID_IClassFactory
, (IUnknown
*)&TestMsg_ClassFactory
, MSHLFLAGS_NORMAL
, &thread
);
1902 ok_more_than_one_lock();
1904 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
1905 hr
= CoUnmarshalInterface(pStream
, &IID_IClassFactory
, (void **)&proxy
);
1906 ok_ole_success(hr
, CoReleaseMarshalData
);
1907 IStream_Release(pStream
);
1909 ok_more_than_one_lock();
1911 /* start message re-entrancy test */
1912 hr
= IClassFactory_CreateInstance(proxy
, NULL
, &IID_IUnknown
, (void **)&object
);
1913 ok_ole_success(hr
, IClassFactory_CreateInstance
);
1915 IClassFactory_Release(proxy
);
1919 end_host_object(tid
, thread
);
1921 while (GetMessage(&msg
, NULL
, 0, 0))
1923 TranslateMessage(&msg
);
1924 DispatchMessage(&msg
);
1926 DestroyWindow(hwnd_app
);
1929 static void test_WM_QUIT_handling(void)
1933 hwnd_app
= CreateWindow("WineCOMTest", NULL
, 0, CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
, NULL
, NULL
, NULL
, 0);
1934 ok(hwnd_app
!= NULL
, "Window creation failed\n");
1936 /* start WM_QUIT handling test */
1937 PostMessage(hwnd_app
, WM_USER
+1, 0, 0);
1939 while (GetMessage(&msg
, NULL
, 0, 0))
1941 TranslateMessage(&msg
);
1942 DispatchMessage(&msg
);
1946 static void test_freethreadedmarshaldata(IStream
*pStream
, MSHCTX mshctx
, void *ptr
, DWORD mshlflags
)
1953 hr
= GetHGlobalFromStream(pStream
, &hglobal
);
1954 ok_ole_success(hr
, GetHGlobalFromStream
);
1956 size
= GlobalSize(hglobal
);
1958 marshal_data
= (char *)GlobalLock(hglobal
);
1960 if (mshctx
== MSHCTX_INPROC
)
1962 DWORD expected_size
= sizeof(DWORD
) + sizeof(void *) + sizeof(DWORD
) + sizeof(GUID
);
1963 ok(size
== expected_size
, "size should have been %d instead of %d\n", expected_size
, size
);
1965 ok(*(DWORD
*)marshal_data
== mshlflags
, "expected 0x%x, but got 0x%x for mshctx\n", mshlflags
, *(DWORD
*)marshal_data
);
1966 marshal_data
+= sizeof(DWORD
);
1967 ok(*(void **)marshal_data
== ptr
, "expected %p, but got %p for mshctx\n", ptr
, *(void **)marshal_data
);
1968 marshal_data
+= sizeof(void *);
1969 ok(*(DWORD
*)marshal_data
== 0, "expected 0x0, but got 0x%x\n", *(DWORD
*)marshal_data
);
1970 marshal_data
+= sizeof(DWORD
);
1971 trace("got guid data: {%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\n",
1972 ((GUID
*)marshal_data
)->Data1
, ((GUID
*)marshal_data
)->Data2
, ((GUID
*)marshal_data
)->Data3
,
1973 ((GUID
*)marshal_data
)->Data4
[0], ((GUID
*)marshal_data
)->Data4
[1], ((GUID
*)marshal_data
)->Data4
[2], ((GUID
*)marshal_data
)->Data4
[3],
1974 ((GUID
*)marshal_data
)->Data4
[4], ((GUID
*)marshal_data
)->Data4
[5], ((GUID
*)marshal_data
)->Data4
[6], ((GUID
*)marshal_data
)->Data4
[7]);
1978 ok(size
> sizeof(DWORD
), "size should have been > sizeof(DWORD), not %d\n", size
);
1979 ok(*(DWORD
*)marshal_data
== 0x574f454d /* MEOW */,
1980 "marshal data should be filled by standard marshal and start with MEOW signature\n");
1983 GlobalUnlock(hglobal
);
1986 static void test_freethreadedmarshaler(void)
1989 IUnknown
*pFTUnknown
;
1990 IMarshal
*pFTMarshal
;
1993 static const LARGE_INTEGER llZero
;
1996 hr
= CoCreateFreeThreadedMarshaler(NULL
, &pFTUnknown
);
1997 ok_ole_success(hr
, CoCreateFreeThreadedMarshaler
);
1998 hr
= IUnknown_QueryInterface(pFTUnknown
, &IID_IMarshal
, (void **)&pFTMarshal
);
1999 ok_ole_success(hr
, IUnknown_QueryInterface
);
2000 IUnknown_Release(pFTUnknown
);
2002 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
2003 ok_ole_success(hr
, CreateStreamOnHGlobal
);
2005 /* inproc normal marshaling */
2007 hr
= IMarshal_MarshalInterface(pFTMarshal
, pStream
, &IID_IClassFactory
,
2008 (IUnknown
*)&Test_ClassFactory
, MSHCTX_INPROC
, NULL
, MSHLFLAGS_NORMAL
);
2009 ok_ole_success(hr
, IMarshal_MarshalInterface
);
2011 ok_more_than_one_lock();
2013 test_freethreadedmarshaldata(pStream
, MSHCTX_INPROC
, &Test_ClassFactory
, MSHLFLAGS_NORMAL
);
2015 IStream_Seek(pStream
, llZero
, STREAM_SEEK_SET
, NULL
);
2016 hr
= IMarshal_UnmarshalInterface(pFTMarshal
, pStream
, &IID_IUnknown
, (void **)&pProxy
);
2017 ok_ole_success(hr
, IMarshal_UnmarshalInterface
);
2019 IUnknown_Release(pProxy
);
2023 /* native doesn't allow us to unmarshal or release the stream data,
2024 * presumably because it wants us to call CoMarshalInterface instead */
2027 /* local normal marshaling */
2029 IStream_Seek(pStream
, llZero
, STREAM_SEEK_SET
, NULL
);
2030 hr
= IMarshal_MarshalInterface(pFTMarshal
, pStream
, &IID_IClassFactory
, (IUnknown
*)&Test_ClassFactory
, MSHCTX_LOCAL
, NULL
, MSHLFLAGS_NORMAL
);
2031 ok_ole_success(hr
, IMarshal_MarshalInterface
);
2033 ok_more_than_one_lock();
2035 test_freethreadedmarshaldata(pStream
, MSHCTX_LOCAL
, &Test_ClassFactory
, MSHLFLAGS_NORMAL
);
2037 IStream_Seek(pStream
, llZero
, STREAM_SEEK_SET
, NULL
);
2038 hr
= IMarshal_ReleaseMarshalData(pFTMarshal
, pStream
);
2039 ok_ole_success(hr
, IMarshal_ReleaseMarshalData
);
2044 /* inproc table-strong marshaling */
2046 IStream_Seek(pStream
, llZero
, STREAM_SEEK_SET
, NULL
);
2047 hr
= IMarshal_MarshalInterface(pFTMarshal
, pStream
, &IID_IClassFactory
,
2048 (IUnknown
*)&Test_ClassFactory
, MSHCTX_INPROC
, (void *)0xdeadbeef,
2049 MSHLFLAGS_TABLESTRONG
);
2050 ok_ole_success(hr
, IMarshal_MarshalInterface
);
2052 ok_more_than_one_lock();
2054 test_freethreadedmarshaldata(pStream
, MSHCTX_INPROC
, &Test_ClassFactory
, MSHLFLAGS_TABLESTRONG
);
2056 IStream_Seek(pStream
, llZero
, STREAM_SEEK_SET
, NULL
);
2057 hr
= IMarshal_UnmarshalInterface(pFTMarshal
, pStream
, &IID_IUnknown
, (void **)&pProxy
);
2058 ok_ole_success(hr
, IMarshal_UnmarshalInterface
);
2060 IUnknown_Release(pProxy
);
2062 ok_more_than_one_lock();
2064 IStream_Seek(pStream
, llZero
, STREAM_SEEK_SET
, NULL
);
2065 hr
= IMarshal_ReleaseMarshalData(pFTMarshal
, pStream
);
2066 ok_ole_success(hr
, IMarshal_ReleaseMarshalData
);
2070 /* inproc table-weak marshaling */
2072 IStream_Seek(pStream
, llZero
, STREAM_SEEK_SET
, NULL
);
2073 hr
= IMarshal_MarshalInterface(pFTMarshal
, pStream
, &IID_IClassFactory
,
2074 (IUnknown
*)&Test_ClassFactory
, MSHCTX_INPROC
, (void *)0xdeadbeef,
2075 MSHLFLAGS_TABLEWEAK
);
2076 ok_ole_success(hr
, IMarshal_MarshalInterface
);
2080 test_freethreadedmarshaldata(pStream
, MSHCTX_INPROC
, &Test_ClassFactory
, MSHLFLAGS_TABLEWEAK
);
2082 IStream_Seek(pStream
, llZero
, STREAM_SEEK_SET
, NULL
);
2083 hr
= IMarshal_UnmarshalInterface(pFTMarshal
, pStream
, &IID_IUnknown
, (void **)&pProxy
);
2084 ok_ole_success(hr
, IMarshal_UnmarshalInterface
);
2086 ok_more_than_one_lock();
2088 IUnknown_Release(pProxy
);
2092 /* inproc normal marshaling (for extraordinary cases) */
2094 IStream_Seek(pStream
, llZero
, STREAM_SEEK_SET
, NULL
);
2095 hr
= IMarshal_MarshalInterface(pFTMarshal
, pStream
, &IID_IClassFactory
,
2096 (IUnknown
*)&Test_ClassFactory
, MSHCTX_INPROC
, NULL
, MSHLFLAGS_NORMAL
);
2097 ok_ole_success(hr
, IMarshal_MarshalInterface
);
2099 ok_more_than_one_lock();
2101 /* this call shows that DisconnectObject does nothing */
2102 hr
= IMarshal_DisconnectObject(pFTMarshal
, 0);
2103 ok_ole_success(hr
, IMarshal_DisconnectObject
);
2105 ok_more_than_one_lock();
2107 IStream_Seek(pStream
, llZero
, STREAM_SEEK_SET
, NULL
);
2108 hr
= IMarshal_ReleaseMarshalData(pFTMarshal
, pStream
);
2109 ok_ole_success(hr
, IMarshal_ReleaseMarshalData
);
2113 /* doesn't enforce marshaling rules here and allows us to unmarshal the
2114 * interface, even though it was freed above */
2115 IStream_Seek(pStream
, llZero
, STREAM_SEEK_SET
, NULL
);
2116 hr
= IMarshal_UnmarshalInterface(pFTMarshal
, pStream
, &IID_IUnknown
, (void **)&pProxy
);
2117 ok_ole_success(hr
, IMarshal_UnmarshalInterface
);
2121 IStream_Release(pStream
);
2122 IMarshal_Release(pFTMarshal
);
2125 static void test_inproc_handler(void)
2133 DWORD dwDisposition
;
2136 hr
= StringFromCLSID(&CLSID_WineTest
, &pszClsid
);
2137 ok_ole_success(hr
, "StringFromCLSID");
2138 strcpy(buffer
, "CLSID\\");
2139 WideCharToMultiByte(CP_ACP
, 0, pszClsid
, -1, buffer
+ strlen(buffer
), sizeof(buffer
) - strlen(buffer
), NULL
, NULL
);
2140 CoTaskMemFree(pszClsid
);
2141 strcat(buffer
, "\\InprocHandler32");
2142 error
= RegCreateKeyEx(HKEY_CLASSES_ROOT
, buffer
, 0, NULL
, 0, KEY_SET_VALUE
, NULL
, &hkey
, &dwDisposition
);
2143 ok(error
== ERROR_SUCCESS
, "RegCreateKeyEx failed with error %d\n", error
);
2144 error
= RegSetValueEx(hkey
, NULL
, 0, REG_SZ
, (const unsigned char *)"ole32.dll", strlen("ole32.dll") + 1);
2145 ok(error
== ERROR_SUCCESS
, "RegSetValueEx failed with error %d\n", error
);
2148 hr
= CoCreateInstance(&CLSID_WineTest
, NULL
, CLSCTX_INPROC_HANDLER
, &IID_IUnknown
, (void **)&pObject
);
2150 ok_ole_success(hr
, "CoCreateInstance");
2154 hr
= IUnknown_QueryInterface(pObject
, &IID_IWineTest
, (void **)&pObject2
);
2155 ok(hr
== E_NOINTERFACE
, "IUnknown_QueryInterface on handler for invalid interface returned 0x%08x instead of E_NOINTERFACE\n", hr
);
2157 /* it's a handler as it supports IOleObject */
2158 hr
= IUnknown_QueryInterface(pObject
, &IID_IOleObject
, (void **)&pObject2
);
2159 ok_ole_success(hr
, "IUnknown_QueryInterface(&IID_IOleObject)");
2160 IUnknown_Release(pObject2
);
2162 IUnknown_Release(pObject
);
2165 RegDeleteKey(HKEY_CLASSES_ROOT
, buffer
);
2166 *strrchr(buffer
, '\\') = '\0';
2167 RegDeleteKey(HKEY_CLASSES_ROOT
, buffer
);
2170 static HRESULT WINAPI
Test_SMI_QueryInterface(
2171 IStdMarshalInfo
*iface
,
2175 if (ppvObj
== NULL
) return E_POINTER
;
2177 if (IsEqualGUID(riid
, &IID_IUnknown
) ||
2178 IsEqualGUID(riid
, &IID_IStdMarshalInfo
))
2180 *ppvObj
= (LPVOID
)iface
;
2181 IClassFactory_AddRef(iface
);
2185 return E_NOINTERFACE
;
2188 static ULONG WINAPI
Test_SMI_AddRef(IStdMarshalInfo
*iface
)
2191 return 2; /* non-heap-based object */
2194 static ULONG WINAPI
Test_SMI_Release(IStdMarshalInfo
*iface
)
2197 return 1; /* non-heap-based object */
2200 static HRESULT WINAPI
Test_SMI_GetClassForHandler(
2201 IStdMarshalInfo
*iface
,
2202 DWORD dwDestContext
,
2203 void *pvDestContext
,
2206 *pClsid
= CLSID_WineTest
;
2210 static const IStdMarshalInfoVtbl Test_SMI_Vtbl
=
2212 Test_SMI_QueryInterface
,
2215 Test_SMI_GetClassForHandler
2218 static IStdMarshalInfo Test_SMI
= {&Test_SMI_Vtbl
};
2220 static void test_handler_marshaling(void)
2223 IStream
*pStream
= NULL
;
2224 IUnknown
*pProxy
= NULL
;
2228 static const LARGE_INTEGER ullZero
;
2232 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
2233 ok_ole_success(hr
, "CreateStreamOnHGlobal");
2234 tid
= start_host_object(pStream
, &IID_IUnknown
, (IUnknown
*)&Test_SMI
, MSHLFLAGS_NORMAL
, &thread
);
2236 ok_more_than_one_lock();
2238 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
2239 hr
= CoUnmarshalInterface(pStream
, &IID_IUnknown
, (void **)&pProxy
);
2240 ok_ole_success(hr
, "CoUnmarshalInterface");
2241 IStream_Release(pStream
);
2243 ok_more_than_one_lock();
2245 hr
= IUnknown_QueryInterface(pProxy
, &IID_IWineTest
, (void **)&pObject
);
2246 ok(hr
== E_NOINTERFACE
, "IUnknown_QueryInterface with unknown IID should have returned E_NOINTERFACE instead of 0x%08x\n", hr
);
2248 /* it's a handler as it supports IOleObject */
2249 hr
= IUnknown_QueryInterface(pProxy
, &IID_IOleObject
, (void **)&pObject
);
2251 ok_ole_success(hr
, "IUnknown_QueryInterface(&IID_IOleObject)");
2252 if (SUCCEEDED(hr
)) IUnknown_Release(pObject
);
2254 IUnknown_Release(pProxy
);
2258 end_host_object(tid
, thread
);
2260 /* FIXME: test IPersist interface has the same effect as IStdMarshalInfo */
2264 static void test_client_security(void)
2267 IStream
*pStream
= NULL
;
2268 IClassFactory
*pProxy
= NULL
;
2269 IUnknown
*pProxy2
= NULL
;
2270 IUnknown
*pUnknown1
= NULL
;
2271 IUnknown
*pUnknown2
= NULL
;
2272 IClientSecurity
*pCliSec
= NULL
;
2276 static const LARGE_INTEGER ullZero
;
2279 OLECHAR
*pServerPrincName
;
2283 DWORD dwCapabilities
;
2288 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
2289 ok_ole_success(hr
, "CreateStreamOnHGlobal");
2290 tid
= start_host_object(pStream
, &IID_IClassFactory
, (IUnknown
*)&Test_ClassFactory
, MSHLFLAGS_NORMAL
, &thread
);
2292 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
2293 hr
= CoUnmarshalInterface(pStream
, &IID_IClassFactory
, (void **)&pProxy
);
2294 ok_ole_success(hr
, "CoUnmarshalInterface");
2295 IStream_Release(pStream
);
2297 hr
= IUnknown_QueryInterface(pProxy
, &IID_IUnknown
, (LPVOID
*)&pUnknown1
);
2298 ok_ole_success(hr
, "IUnknown_QueryInterface IID_IUnknown");
2300 hr
= IUnknown_QueryInterface(pProxy
, &IID_IRemUnknown
, (LPVOID
*)&pProxy2
);
2301 ok_ole_success(hr
, "IUnknown_QueryInterface IID_IStream");
2303 hr
= IUnknown_QueryInterface(pProxy2
, &IID_IUnknown
, (LPVOID
*)&pUnknown2
);
2304 ok_ole_success(hr
, "IUnknown_QueryInterface IID_IUnknown");
2306 ok(pUnknown1
== pUnknown2
, "both proxy's IUnknowns should be the same - %p, %p\n", pUnknown1
, pUnknown2
);
2308 hr
= IUnknown_QueryInterface(pProxy
, &IID_IMarshal
, (LPVOID
*)&pMarshal
);
2309 ok_ole_success(hr
, "IUnknown_QueryInterface IID_IMarshal");
2311 hr
= IUnknown_QueryInterface(pProxy
, &IID_IClientSecurity
, (LPVOID
*)&pCliSec
);
2312 ok_ole_success(hr
, "IUnknown_QueryInterface IID_IClientSecurity");
2314 hr
= IClientSecurity_QueryBlanket(pCliSec
, (IUnknown
*)pProxy
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
2315 todo_wine
ok_ole_success(hr
, "IClientSecurity_QueryBlanket (all NULLs)");
2317 hr
= IClientSecurity_QueryBlanket(pCliSec
, (IUnknown
*)pMarshal
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
2318 todo_wine
ok(hr
== E_NOINTERFACE
, "IClientSecurity_QueryBlanket with local interface should have returned E_NOINTERFACE instead of 0x%08x\n", hr
);
2320 hr
= IClientSecurity_QueryBlanket(pCliSec
, (IUnknown
*)pProxy
, &dwAuthnSvc
, &dwAuthzSvc
, &pServerPrincName
, &dwAuthnLevel
, &dwImpLevel
, &pAuthInfo
, &dwCapabilities
);
2321 todo_wine
ok_ole_success(hr
, "IClientSecurity_QueryBlanket");
2323 hr
= IClientSecurity_SetBlanket(pCliSec
, (IUnknown
*)pProxy
, dwAuthnSvc
, dwAuthzSvc
, pServerPrincName
, dwAuthnLevel
, RPC_C_IMP_LEVEL_IMPERSONATE
, pAuthInfo
, dwCapabilities
);
2324 todo_wine
ok_ole_success(hr
, "IClientSecurity_SetBlanket");
2326 hr
= IClassFactory_CreateInstance(pProxy
, NULL
, &IID_IWineTest
, &pv
);
2327 ok(hr
== E_NOINTERFACE
, "COM call should have succeeded instead of returning 0x%08x\n", hr
);
2329 hr
= IClientSecurity_SetBlanket(pCliSec
, (IUnknown
*)pMarshal
, dwAuthnSvc
, dwAuthzSvc
, pServerPrincName
, dwAuthnLevel
, dwImpLevel
, pAuthInfo
, dwCapabilities
);
2330 todo_wine
ok(hr
== E_NOINTERFACE
, "IClientSecurity_SetBlanket with local interface should have returned E_NOINTERFACE instead of 0x%08x\n", hr
);
2332 hr
= IClientSecurity_SetBlanket(pCliSec
, (IUnknown
*)pProxy
, 0xdeadbeef, dwAuthzSvc
, pServerPrincName
, dwAuthnLevel
, dwImpLevel
, pAuthInfo
, dwCapabilities
);
2333 todo_wine
ok(hr
== E_INVALIDARG
, "IClientSecurity_SetBlanke with invalid dwAuthnSvc should have returned E_INVALIDARG instead of 0x%08x\n", hr
);
2335 CoTaskMemFree(pServerPrincName
);
2337 hr
= IClientSecurity_QueryBlanket(pCliSec
, (IUnknown
*)pUnknown1
, &dwAuthnSvc
, &dwAuthzSvc
, &pServerPrincName
, &dwAuthnLevel
, &dwImpLevel
, &pAuthInfo
, &dwCapabilities
);
2338 todo_wine
ok_ole_success(hr
, "IClientSecurity_QueryBlanket(IUnknown)");
2340 CoTaskMemFree(pServerPrincName
);
2342 IClassFactory_Release(pProxy
);
2343 IUnknown_Release(pProxy2
);
2344 IUnknown_Release(pUnknown1
);
2345 IUnknown_Release(pUnknown2
);
2346 IMarshal_Release(pMarshal
);
2347 IClientSecurity_Release(pCliSec
);
2349 end_host_object(tid
, thread
);
2352 static HANDLE heventShutdown
;
2354 static void LockModuleOOP(void)
2356 InterlockedIncrement(&cLocks
); /* for test purposes only */
2357 CoAddRefServerProcess();
2360 static void UnlockModuleOOP(void)
2362 InterlockedDecrement(&cLocks
); /* for test purposes only */
2363 if (!CoReleaseServerProcess())
2364 SetEvent(heventShutdown
);
2367 static HWND hwnd_app
;
2369 static HRESULT WINAPI
TestOOP_IClassFactory_QueryInterface(
2370 LPCLASSFACTORY iface
,
2374 if (ppvObj
== NULL
) return E_POINTER
;
2376 if (IsEqualGUID(riid
, &IID_IUnknown
) ||
2377 IsEqualGUID(riid
, &IID_IClassFactory
))
2379 *ppvObj
= (LPVOID
)iface
;
2380 IClassFactory_AddRef(iface
);
2384 return E_NOINTERFACE
;
2387 static ULONG WINAPI
TestOOP_IClassFactory_AddRef(LPCLASSFACTORY iface
)
2389 return 2; /* non-heap-based object */
2392 static ULONG WINAPI
TestOOP_IClassFactory_Release(LPCLASSFACTORY iface
)
2394 return 1; /* non-heap-based object */
2397 static HRESULT WINAPI
TestOOP_IClassFactory_CreateInstance(
2398 LPCLASSFACTORY iface
,
2399 LPUNKNOWN pUnkOuter
,
2403 if (IsEqualIID(riid
, &IID_IClassFactory
) || IsEqualIID(riid
, &IID_IUnknown
))
2408 return CLASS_E_CLASSNOTAVAILABLE
;
2411 static HRESULT WINAPI
TestOOP_IClassFactory_LockServer(
2412 LPCLASSFACTORY iface
,
2422 static const IClassFactoryVtbl TestClassFactoryOOP_Vtbl
=
2424 TestOOP_IClassFactory_QueryInterface
,
2425 TestOOP_IClassFactory_AddRef
,
2426 TestOOP_IClassFactory_Release
,
2427 TestOOP_IClassFactory_CreateInstance
,
2428 TestOOP_IClassFactory_LockServer
2431 static IClassFactory TestOOP_ClassFactory
= { &TestClassFactoryOOP_Vtbl
};
2433 static void test_register_local_server(void)
2441 heventShutdown
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2443 hr
= CoRegisterClassObject(&CLSID_WineOOPTest
, (IUnknown
*)&TestOOP_ClassFactory
,
2444 CLSCTX_LOCAL_SERVER
, REGCLS_SINGLEUSE
, &cookie
);
2445 ok_ole_success(hr
, CoRegisterClassObject
);
2447 ready_event
= CreateEvent(NULL
, FALSE
, FALSE
, "Wine COM Test Ready Event");
2448 SetEvent(ready_event
);
2450 quit_event
= CreateEvent(NULL
, FALSE
, FALSE
, "Wine COM Test Quit Event");
2454 wait
= MsgWaitForMultipleObjects(1, &quit_event
, FALSE
, INFINITE
, QS_ALLINPUT
);
2455 if (wait
== WAIT_OBJECT_0
+1)
2458 BOOL ret
= PeekMessage(&msg
, NULL
, 0, 0, PM_REMOVE
);
2461 trace("Message 0x%x\n", msg
.message
);
2462 TranslateMessage(&msg
);
2463 DispatchMessage(&msg
);
2467 while (wait
== WAIT_OBJECT_0
+1);
2469 hr
= CoRevokeClassObject(cookie
);
2470 ok_ole_success(hr
, CoRevokeClassObject
);
2473 static HANDLE
create_target_process(const char *arg
)
2476 char cmdline
[MAX_PATH
];
2477 PROCESS_INFORMATION pi
;
2478 STARTUPINFO si
= { 0 };
2483 winetest_get_mainargs( &argv
);
2484 sprintf(cmdline
, "%s %s %s", argv
[0], argv
[1], arg
);
2485 ok(CreateProcess(argv
[0], cmdline
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
,
2486 &si
, &pi
) != 0, "CreateProcess failed with error: %u\n", GetLastError());
2487 if (pi
.hThread
) CloseHandle(pi
.hThread
);
2491 /* tests functions commonly used by out of process COM servers */
2492 static void test_local_server(void)
2502 heventShutdown
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2506 /* Start the object suspended */
2507 hr
= CoRegisterClassObject(&CLSID_WineOOPTest
, (IUnknown
*)&TestOOP_ClassFactory
,
2508 CLSCTX_LOCAL_SERVER
, REGCLS_MULTIPLEUSE
| REGCLS_SUSPENDED
, &cookie
);
2509 ok_ole_success(hr
, CoRegisterClassObject
);
2511 /* ... and CoGetClassObject does not find it and fails when it looks for the
2512 * class in the registry */
2513 hr
= CoGetClassObject(&CLSID_WineOOPTest
, CLSCTX_INPROC_SERVER
,
2514 NULL
, &IID_IClassFactory
, (LPVOID
*)&cf
);
2515 ok(hr
== REGDB_E_CLASSNOTREG
|| /* NT */
2516 hr
== S_OK
/* Win9x */,
2517 "CoGetClassObject should have returned REGDB_E_CLASSNOTREG instead of 0x%08x\n", hr
);
2519 /* Resume the object suspended above ... */
2520 hr
= CoResumeClassObjects();
2521 ok_ole_success(hr
, CoResumeClassObjects
);
2523 /* ... and now it should succeed */
2524 hr
= CoGetClassObject(&CLSID_WineOOPTest
, CLSCTX_INPROC_SERVER
,
2525 NULL
, &IID_IClassFactory
, (LPVOID
*)&cf
);
2526 ok_ole_success(hr
, CoGetClassObject
);
2528 /* Now check the locking is working */
2529 /* NOTE: we are accessing the class directly, not through a proxy */
2533 hr
= IClassFactory_LockServer(cf
, TRUE
);
2534 ok_ole_success(hr
, IClassFactory_LockServer
);
2536 ok_more_than_one_lock();
2538 IClassFactory_LockServer(cf
, FALSE
);
2539 ok_ole_success(hr
, IClassFactory_LockServer
);
2543 IClassFactory_Release(cf
);
2545 /* wait for shutdown signal */
2546 ret
= WaitForSingleObject(heventShutdown
, 0);
2547 ok(ret
!= WAIT_TIMEOUT
, "Server didn't shut down\n");
2549 /* try to connect again after SCM has suspended registered class objects */
2550 hr
= CoGetClassObject(&CLSID_WineOOPTest
, CLSCTX_INPROC_SERVER
| CLSCTX_LOCAL_SERVER
, NULL
,
2551 &IID_IClassFactory
, (LPVOID
*)&cf
);
2552 ok(hr
== CO_E_SERVER_STOPPING
|| /* NT */
2553 hr
== S_OK
/* Win9x */,
2554 "CoGetClassObject should have returned CO_E_SERVER_STOPPING instead of 0x%08x\n", hr
);
2556 hr
= CoRevokeClassObject(cookie
);
2557 ok_ole_success(hr
, CoRevokeClassObject
);
2559 CloseHandle(heventShutdown
);
2561 process
= create_target_process("-Embedding");
2562 ok(process
!= NULL
, "couldn't start local server process, error was %d\n", GetLastError());
2564 ready_event
= CreateEvent(NULL
, FALSE
, FALSE
, "Wine COM Test Ready Event");
2565 WaitForSingleObject(ready_event
, INFINITE
);
2566 CloseHandle(ready_event
);
2568 hr
= CoCreateInstance(&CLSID_WineOOPTest
, NULL
, CLSCTX_LOCAL_SERVER
, &IID_IClassFactory
, (void **)&cf
);
2569 ok_ole_success(hr
, CoCreateInstance
);
2571 IClassFactory_Release(cf
);
2573 hr
= CoCreateInstance(&CLSID_WineOOPTest
, NULL
, CLSCTX_LOCAL_SERVER
, &IID_IClassFactory
, (void **)&cf
);
2574 ok(hr
== REGDB_E_CLASSNOTREG
, "Second CoCreateInstance on REGCLS_SINGLEUSE object should have failed\n");
2576 quit_event
= CreateEvent(NULL
, FALSE
, FALSE
, "Wine COM Test Quit Event");
2577 SetEvent(quit_event
);
2579 winetest_wait_child_process( process
);
2580 CloseHandle(quit_event
);
2581 CloseHandle(process
);
2587 IGlobalInterfaceTable
*git
;
2590 static DWORD CALLBACK
get_global_interface_proc(LPVOID pv
)
2593 struct git_params
*params
= (struct git_params
*)pv
;
2596 hr
= IGlobalInterfaceTable_GetInterfaceFromGlobal(params
->git
, params
->cookie
, &IID_IClassFactory
, (void **)&cf
);
2597 ok(hr
== CO_E_NOTINITIALIZED
,
2598 "IGlobalInterfaceTable_GetInterfaceFromGlobal should have failed with error CO_E_NOTINITIALIZED instead of 0x%08x\n",
2603 hr
= IGlobalInterfaceTable_GetInterfaceFromGlobal(params
->git
, params
->cookie
, &IID_IClassFactory
, (void **)&cf
);
2604 ok_ole_success(hr
, IGlobalInterfaceTable_GetInterfaceFromGlobal
);
2606 IClassFactory_Release(cf
);
2613 static void test_globalinterfacetable(void)
2616 IGlobalInterfaceTable
*git
;
2620 struct git_params params
;
2624 trace("test_globalinterfacetable\n");
2627 hr
= CoCreateInstance(&CLSID_StdGlobalInterfaceTable
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IGlobalInterfaceTable
, (void **)&git
);
2628 ok_ole_success(hr
, CoCreateInstance
);
2630 hr
= IGlobalInterfaceTable_RegisterInterfaceInGlobal(git
, (IUnknown
*)&Test_ClassFactory
, &IID_IClassFactory
, &cookie
);
2631 ok_ole_success(hr
, IGlobalInterfaceTable_RegisterInterfaceInGlobal
);
2633 ok_more_than_one_lock();
2635 params
.cookie
= cookie
;
2637 /* note: params is on stack so we MUST wait for get_global_interface_proc
2638 * to exit before we can return */
2639 thread
= CreateThread(NULL
, 0, get_global_interface_proc
, ¶ms
, 0, &tid
);
2641 ret
= MsgWaitForMultipleObjects(1, &thread
, FALSE
, INFINITE
, QS_ALLINPUT
);
2642 while (ret
== WAIT_OBJECT_0
+ 1)
2645 while (PeekMessage(&msg
, NULL
, 0, 0, PM_REMOVE
))
2646 DispatchMessage(&msg
);
2647 ret
= MsgWaitForMultipleObjects(1, &thread
, FALSE
, INFINITE
, QS_ALLINPUT
);
2650 CloseHandle(thread
);
2652 /* test getting interface from global with different iid */
2653 hr
= IGlobalInterfaceTable_GetInterfaceFromGlobal(git
, cookie
, &IID_IUnknown
, (void **)&object
);
2654 ok_ole_success(hr
, IGlobalInterfaceTable_GetInterfaceFromGlobal
);
2655 IUnknown_Release(object
);
2657 /* test getting interface from global with same iid */
2658 hr
= IGlobalInterfaceTable_GetInterfaceFromGlobal(git
, cookie
, &IID_IClassFactory
, (void **)&object
);
2659 ok_ole_success(hr
, IGlobalInterfaceTable_GetInterfaceFromGlobal
);
2660 IUnknown_Release(object
);
2662 hr
= IGlobalInterfaceTable_RevokeInterfaceFromGlobal(git
, cookie
);
2663 ok_ole_success(hr
, IGlobalInterfaceTable_RevokeInterfaceFromGlobal
);
2667 IGlobalInterfaceTable_Release(git
);
2670 static const char *debugstr_iid(REFIID riid
)
2672 static char name
[256];
2676 LONG name_size
= sizeof(name
);
2677 StringFromGUID2(riid
, bufferW
, sizeof(bufferW
)/sizeof(bufferW
[0]));
2678 WideCharToMultiByte(CP_ACP
, 0, bufferW
, sizeof(bufferW
)/sizeof(bufferW
[0]), buffer
, sizeof(buffer
), NULL
, NULL
);
2679 if (RegOpenKeyEx(HKEY_CLASSES_ROOT
, "Interface", 0, KEY_QUERY_VALUE
, &hkeyInterface
) != ERROR_SUCCESS
)
2681 memcpy(name
, buffer
, sizeof(buffer
));
2684 if (RegQueryValue(hkeyInterface
, buffer
, name
, &name_size
) != ERROR_SUCCESS
)
2686 memcpy(name
, buffer
, sizeof(buffer
));
2689 RegCloseKey(hkeyInterface
);
2694 static HRESULT WINAPI
TestChannelHook_QueryInterface(IChannelHook
*iface
, REFIID riid
, void **ppv
)
2696 if (IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IChannelHook
))
2699 IUnknown_AddRef(iface
);
2704 return E_NOINTERFACE
;
2707 static ULONG WINAPI
TestChannelHook_AddRef(IChannelHook
*iface
)
2712 static ULONG WINAPI
TestChannelHook_Release(IChannelHook
*iface
)
2717 static void WINAPI
TestChannelHook_ClientGetSize(
2718 IChannelHook
*iface
,
2723 SChannelHookCallInfo
*info
= (SChannelHookCallInfo
*)riid
;
2724 trace("TestChannelHook_ClientGetBuffer\n");
2725 trace("\t%s method %d\n", debugstr_iid(riid
), info
->iMethod
);
2726 trace("\tcid: %s\n", debugstr_iid(&info
->uCausality
));
2727 ok(info
->cbSize
== sizeof(*info
), "info->cbSize was %d instead of %d\n", info
->cbSize
, (int)sizeof(*info
));
2728 ok(info
->dwServerPid
== GetCurrentProcessId(), "info->dwServerPid was 0x%x instead of 0x%x\n", info
->dwServerPid
, GetCurrentProcessId());
2729 ok(!info
->pObject
, "info->pObject should be NULL\n");
2730 ok(IsEqualGUID(uExtent
, &EXTENTID_WineTest
), "uExtent wasn't correct\n");
2735 static void WINAPI
TestChannelHook_ClientFillBuffer(
2736 IChannelHook
*iface
,
2742 SChannelHookCallInfo
*info
= (SChannelHookCallInfo
*)riid
;
2743 trace("TestChannelHook_ClientFillBuffer\n");
2744 ok(info
->cbSize
== sizeof(*info
), "info->cbSize was %d instead of %d\n", info
->cbSize
, (int)sizeof(*info
));
2745 ok(info
->dwServerPid
== GetCurrentProcessId(), "info->dwServerPid was 0x%x instead of 0x%x\n", info
->dwServerPid
, GetCurrentProcessId());
2746 ok(!info
->pObject
, "info->pObject should be NULL\n");
2747 ok(IsEqualGUID(uExtent
, &EXTENTID_WineTest
), "uExtent wasn't correct\n");
2749 *(unsigned char *)pDataBuffer
= 0xcc;
2753 static void WINAPI
TestChannelHook_ClientNotify(
2754 IChannelHook
*iface
,
2762 SChannelHookCallInfo
*info
= (SChannelHookCallInfo
*)riid
;
2763 trace("TestChannelHook_ClientNotify hrFault = 0x%08x\n", hrFault
);
2764 ok(info
->cbSize
== sizeof(*info
), "info->cbSize was %d instead of %d\n", info
->cbSize
, (int)sizeof(*info
));
2765 ok(info
->dwServerPid
== GetCurrentProcessId(), "info->dwServerPid was 0x%x instead of 0x%x\n", info
->dwServerPid
, GetCurrentProcessId());
2767 ok(info
->pObject
!= NULL
, "info->pObject shouldn't be NULL\n");
2769 ok(IsEqualGUID(uExtent
, &EXTENTID_WineTest
), "uExtent wasn't correct\n");
2772 static void WINAPI
TestChannelHook_ServerNotify(
2773 IChannelHook
*iface
,
2780 SChannelHookCallInfo
*info
= (SChannelHookCallInfo
*)riid
;
2781 trace("TestChannelHook_ServerNotify\n");
2782 ok(info
->cbSize
== sizeof(*info
), "info->cbSize was %d instead of %d\n", info
->cbSize
, (int)sizeof(*info
));
2783 ok(info
->dwServerPid
== GetCurrentProcessId(), "info->dwServerPid was 0x%x instead of 0x%x\n", info
->dwServerPid
, GetCurrentProcessId());
2784 ok(info
->pObject
!= NULL
, "info->pObject shouldn't be NULL\n");
2785 ok(cbDataSize
== 1, "cbDataSize should have been 1 instead of %d\n", cbDataSize
);
2786 ok(*(unsigned char *)pDataBuffer
== 0xcc, "pDataBuffer should have contained 0xcc instead of 0x%x\n", *(unsigned char *)pDataBuffer
);
2787 ok(IsEqualGUID(uExtent
, &EXTENTID_WineTest
), "uExtent wasn't correct\n");
2790 static void WINAPI
TestChannelHook_ServerGetSize(
2791 IChannelHook
*iface
,
2797 SChannelHookCallInfo
*info
= (SChannelHookCallInfo
*)riid
;
2798 trace("TestChannelHook_ServerGetSize\n");
2799 trace("\t%s method %d\n", debugstr_iid(riid
), info
->iMethod
);
2800 ok(info
->cbSize
== sizeof(*info
), "info->cbSize was %d instead of %d\n", info
->cbSize
, (int)sizeof(*info
));
2801 ok(info
->dwServerPid
== GetCurrentProcessId(), "info->dwServerPid was 0x%x instead of 0x%x\n", info
->dwServerPid
, GetCurrentProcessId());
2802 ok(info
->pObject
!= NULL
, "info->pObject shouldn't be NULL\n");
2803 ok(IsEqualGUID(uExtent
, &EXTENTID_WineTest
), "uExtent wasn't correct\n");
2804 if (hrFault
!= S_OK
)
2805 trace("\thrFault = 0x%08x\n", hrFault
);
2810 static void WINAPI
TestChannelHook_ServerFillBuffer(
2811 IChannelHook
*iface
,
2818 trace("TestChannelHook_ServerFillBuffer\n");
2819 ok(0, "TestChannelHook_ServerFillBuffer shouldn't be called\n");
2822 static const IChannelHookVtbl TestChannelHookVtbl
=
2824 TestChannelHook_QueryInterface
,
2825 TestChannelHook_AddRef
,
2826 TestChannelHook_Release
,
2827 TestChannelHook_ClientGetSize
,
2828 TestChannelHook_ClientFillBuffer
,
2829 TestChannelHook_ClientNotify
,
2830 TestChannelHook_ServerNotify
,
2831 TestChannelHook_ServerGetSize
,
2832 TestChannelHook_ServerFillBuffer
,
2835 static IChannelHook TestChannelHook
= { &TestChannelHookVtbl
};
2837 static void test_channel_hook(void)
2839 IStream
*pStream
= NULL
;
2840 IClassFactory
*cf
= NULL
;
2842 IUnknown
*proxy
= NULL
;
2846 hr
= CoRegisterChannelHook(&EXTENTID_WineTest
, &TestChannelHook
);
2847 ok_ole_success(hr
, CoRegisterChannelHook
);
2849 hr
= CoRegisterMessageFilter(&MessageFilter
, NULL
);
2850 ok_ole_success(hr
, CoRegisterMessageFilter
);
2854 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
2855 ok_ole_success(hr
, CreateStreamOnHGlobal
);
2856 tid
= start_host_object2(pStream
, &IID_IClassFactory
, (IUnknown
*)&Test_ClassFactory
, MSHLFLAGS_NORMAL
, &MessageFilter
, &thread
);
2858 ok_more_than_one_lock();
2860 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
2861 hr
= CoUnmarshalInterface(pStream
, &IID_IClassFactory
, (void **)&cf
);
2862 ok_ole_success(hr
, CoUnmarshalInterface
);
2863 IStream_Release(pStream
);
2865 ok_more_than_one_lock();
2867 hr
= IClassFactory_CreateInstance(cf
, NULL
, &IID_IUnknown
, (LPVOID
*)&proxy
);
2868 ok_ole_success(hr
, IClassFactory_CreateInstance
);
2869 IUnknown_Release(proxy
);
2871 IClassFactory_Release(cf
);
2875 end_host_object(tid
, thread
);
2877 hr
= CoRegisterMessageFilter(NULL
, NULL
);
2878 ok_ole_success(hr
, CoRegisterMessageFilter
);
2883 HMODULE hOle32
= GetModuleHandle("ole32");
2887 if (!(pCoInitializeEx
= (void*)GetProcAddress(hOle32
, "CoInitializeEx"))) goto no_test
;
2889 argc
= winetest_get_mainargs( &argv
);
2890 if (argc
> 2 && (!strcmp(argv
[2], "-Embedding")))
2892 pCoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
2893 test_register_local_server();
2899 register_test_window();
2901 test_cocreateinstance_proxy();
2903 pCoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
2905 /* FIXME: test CoCreateInstanceEx */
2907 /* lifecycle management and marshaling tests */
2908 test_no_marshaler();
2909 test_normal_marshal_and_release();
2910 test_normal_marshal_and_unmarshal();
2911 test_marshal_and_unmarshal_invalid();
2912 test_same_apartment_unmarshal_failure();
2913 test_interthread_marshal_and_unmarshal();
2914 test_proxy_marshal_and_unmarshal();
2915 test_proxy_marshal_and_unmarshal2();
2916 test_proxy_marshal_and_unmarshal_weak();
2917 test_proxy_marshal_and_unmarshal_strong();
2918 test_marshal_stub_apartment_shutdown();
2919 test_marshal_proxy_apartment_shutdown();
2920 test_marshal_proxy_mta_apartment_shutdown();
2921 test_no_couninitialize_server();
2922 test_no_couninitialize_client();
2923 test_tableweak_marshal_and_unmarshal_twice();
2924 test_tableweak_marshal_releasedata1();
2925 test_tableweak_marshal_releasedata2();
2926 test_tablestrong_marshal_and_unmarshal_twice();
2927 test_lock_object_external();
2928 test_disconnect_stub();
2929 test_normal_marshal_and_unmarshal_twice();
2930 test_hresult_marshaling();
2931 test_proxy_used_in_wrong_thread();
2932 test_message_filter();
2933 test_bad_marshal_stream();
2934 test_proxy_interfaces();
2935 test_stubbuffer(&IID_IClassFactory
);
2936 test_proxybuffer(&IID_IClassFactory
);
2937 test_message_reentrancy();
2938 test_call_from_message();
2939 test_WM_QUIT_handling();
2940 test_freethreadedmarshaler();
2941 test_inproc_handler();
2942 test_handler_marshaling();
2943 test_client_security();
2945 test_local_server();
2947 test_globalinterfacetable();
2949 /* must be last test as channel hooks can't be unregistered */
2950 test_channel_hook();
2956 trace("You need DCOM95 installed to run this test\n");