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 struct weak_and_normal_marshal_data
1082 IStream
*pStreamWeak
;
1083 IStream
*pStreamNormal
;
1088 static DWORD CALLBACK
weak_and_normal_marshal_thread_proc(void *p
)
1091 struct weak_and_normal_marshal_data
*data
= p
;
1092 HANDLE hQuitEvent
= data
->hQuitEvent
;
1095 pCoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
1097 hr
= CoMarshalInterface(data
->pStreamWeak
, &IID_IClassFactory
, (IUnknown
*)&Test_ClassFactory
, MSHCTX_INPROC
, NULL
, MSHLFLAGS_TABLEWEAK
);
1098 ok_ole_success(hr
, "CoMarshalInterface");
1100 hr
= CoMarshalInterface(data
->pStreamNormal
, &IID_IClassFactory
, (IUnknown
*)&Test_ClassFactory
, MSHCTX_INPROC
, NULL
, MSHLFLAGS_NORMAL
);
1101 ok_ole_success(hr
, "CoMarshalInterface");
1103 /* force the message queue to be created before signaling parent thread */
1104 PeekMessage(&msg
, NULL
, WM_USER
, WM_USER
, PM_NOREMOVE
);
1106 SetEvent(data
->hReadyEvent
);
1108 while (WAIT_OBJECT_0
+ 1 == MsgWaitForMultipleObjects(1, &hQuitEvent
, FALSE
, INFINITE
, QS_ALLINPUT
))
1110 while (PeekMessage(&msg
, NULL
, 0, 0, PM_REMOVE
))
1111 DispatchMessage(&msg
);
1113 CloseHandle(hQuitEvent
);
1120 /* tests interaction between table-weak and normal marshalling of an object */
1121 static void test_tableweak_and_normal_marshal_and_unmarshal(void)
1124 IUnknown
*pProxyWeak
= NULL
;
1125 IUnknown
*pProxyNormal
= NULL
;
1128 struct weak_and_normal_marshal_data data
;
1132 data
.hReadyEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1133 data
.hQuitEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1134 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &data
.pStreamWeak
);
1135 ok_ole_success(hr
, CreateStreamOnHGlobal
);
1136 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &data
.pStreamNormal
);
1137 ok_ole_success(hr
, CreateStreamOnHGlobal
);
1139 thread
= CreateThread(NULL
, 0, weak_and_normal_marshal_thread_proc
, &data
, 0, &tid
);
1140 WaitForSingleObject(data
.hReadyEvent
, INFINITE
);
1141 CloseHandle(data
.hReadyEvent
);
1143 ok_more_than_one_lock();
1145 IStream_Seek(data
.pStreamWeak
, ullZero
, STREAM_SEEK_SET
, NULL
);
1146 hr
= CoUnmarshalInterface(data
.pStreamWeak
, &IID_IClassFactory
, (void **)&pProxyWeak
);
1147 ok_ole_success(hr
, CoUnmarshalInterface
);
1149 ok_more_than_one_lock();
1151 IStream_Seek(data
.pStreamNormal
, ullZero
, STREAM_SEEK_SET
, NULL
);
1152 hr
= CoUnmarshalInterface(data
.pStreamNormal
, &IID_IClassFactory
, (void **)&pProxyNormal
);
1153 ok_ole_success(hr
, CoUnmarshalInterface
);
1155 ok_more_than_one_lock();
1157 IUnknown_Release(pProxyNormal
);
1159 ok_more_than_one_lock();
1161 IUnknown_Release(pProxyWeak
);
1165 IStream_Release(data
.pStreamWeak
);
1166 IStream_Release(data
.pStreamNormal
);
1168 SetEvent(data
.hQuitEvent
);
1169 WaitForSingleObject(thread
, INFINITE
);
1170 CloseHandle(thread
);
1173 /* tests success case of a same-thread table-strong marshal, unmarshal, unmarshal */
1174 static void test_tablestrong_marshal_and_unmarshal_twice(void)
1177 IStream
*pStream
= NULL
;
1178 IUnknown
*pProxy1
= NULL
;
1179 IUnknown
*pProxy2
= NULL
;
1185 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
1186 ok_ole_success(hr
, CreateStreamOnHGlobal
);
1187 tid
= start_host_object(pStream
, &IID_IClassFactory
, (IUnknown
*)&Test_ClassFactory
, MSHLFLAGS_TABLESTRONG
, &thread
);
1189 ok_more_than_one_lock();
1191 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
1192 hr
= CoUnmarshalInterface(pStream
, &IID_IClassFactory
, (void **)&pProxy1
);
1193 ok_ole_success(hr
, CoUnmarshalInterface
);
1195 ok_more_than_one_lock();
1197 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
1198 hr
= CoUnmarshalInterface(pStream
, &IID_IClassFactory
, (void **)&pProxy2
);
1199 ok_ole_success(hr
, CoUnmarshalInterface
);
1201 ok_more_than_one_lock();
1203 if (pProxy1
) IUnknown_Release(pProxy1
);
1204 if (pProxy2
) IUnknown_Release(pProxy2
);
1206 /* this line is shows the difference between weak and strong table marshaling:
1207 * weak has cLocks == 0
1208 * strong has cLocks > 0 */
1209 ok_more_than_one_lock();
1211 /* release the remaining reference on the object by calling
1212 * CoReleaseMarshalData in the hosting thread */
1213 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
1214 release_host_object(tid
);
1215 IStream_Release(pStream
);
1219 end_host_object(tid
, thread
);
1222 /* tests CoLockObjectExternal */
1223 static void test_lock_object_external(void)
1226 IStream
*pStream
= NULL
;
1230 /* test the stub manager creation aspect of CoLockObjectExternal when the
1231 * object hasn't been marshaled yet */
1232 CoLockObjectExternal((IUnknown
*)&Test_ClassFactory
, TRUE
, TRUE
);
1234 ok_more_than_one_lock();
1236 CoDisconnectObject((IUnknown
*)&Test_ClassFactory
, 0);
1240 /* test our empty stub manager being handled correctly in
1241 * CoMarshalInterface */
1242 CoLockObjectExternal((IUnknown
*)&Test_ClassFactory
, TRUE
, TRUE
);
1244 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
1245 ok_ole_success(hr
, CreateStreamOnHGlobal
);
1246 hr
= CoMarshalInterface(pStream
, &IID_IClassFactory
, (IUnknown
*)&Test_ClassFactory
, MSHCTX_INPROC
, NULL
, MSHLFLAGS_NORMAL
);
1247 ok_ole_success(hr
, CoMarshalInterface
);
1249 CoLockObjectExternal((IUnknown
*)&Test_ClassFactory
, TRUE
, TRUE
);
1251 ok_more_than_one_lock();
1253 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
1254 hr
= CoReleaseMarshalData(pStream
);
1255 ok_ole_success(hr
, CoReleaseMarshalData
);
1256 IStream_Release(pStream
);
1258 ok_more_than_one_lock();
1260 CoLockObjectExternal((IUnknown
*)&Test_ClassFactory
, FALSE
, TRUE
);
1262 ok_more_than_one_lock();
1264 CoLockObjectExternal((IUnknown
*)&Test_ClassFactory
, FALSE
, TRUE
);
1269 /* tests disconnecting stubs */
1270 static void test_disconnect_stub(void)
1273 IStream
*pStream
= NULL
;
1277 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
1278 ok_ole_success(hr
, CreateStreamOnHGlobal
);
1279 hr
= CoMarshalInterface(pStream
, &IID_IClassFactory
, (IUnknown
*)&Test_ClassFactory
, MSHCTX_INPROC
, NULL
, MSHLFLAGS_NORMAL
);
1280 ok_ole_success(hr
, CoMarshalInterface
);
1282 CoLockObjectExternal((IUnknown
*)&Test_ClassFactory
, TRUE
, TRUE
);
1284 ok_more_than_one_lock();
1286 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
1287 hr
= CoReleaseMarshalData(pStream
);
1288 ok_ole_success(hr
, CoReleaseMarshalData
);
1289 IStream_Release(pStream
);
1291 ok_more_than_one_lock();
1293 CoDisconnectObject((IUnknown
*)&Test_ClassFactory
, 0);
1298 /* tests failure case of a same-thread marshal and unmarshal twice */
1299 static void test_normal_marshal_and_unmarshal_twice(void)
1302 IStream
*pStream
= NULL
;
1303 IUnknown
*pProxy1
= NULL
;
1304 IUnknown
*pProxy2
= NULL
;
1308 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
1309 ok_ole_success(hr
, CreateStreamOnHGlobal
);
1310 hr
= CoMarshalInterface(pStream
, &IID_IClassFactory
, (IUnknown
*)&Test_ClassFactory
, MSHCTX_INPROC
, NULL
, MSHLFLAGS_NORMAL
);
1311 ok_ole_success(hr
, CoMarshalInterface
);
1313 ok_more_than_one_lock();
1315 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
1316 hr
= CoUnmarshalInterface(pStream
, &IID_IClassFactory
, (void **)&pProxy1
);
1317 ok_ole_success(hr
, CoUnmarshalInterface
);
1319 ok_more_than_one_lock();
1321 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
1322 hr
= CoUnmarshalInterface(pStream
, &IID_IClassFactory
, (void **)&pProxy2
);
1323 ok(hr
== CO_E_OBJNOTCONNECTED
,
1324 "CoUnmarshalInterface should have failed with error CO_E_OBJNOTCONNECTED for double unmarshal, instead of 0x%08x\n", hr
);
1326 IStream_Release(pStream
);
1328 ok_more_than_one_lock();
1330 IUnknown_Release(pProxy1
);
1335 /* tests success case of marshaling and unmarshaling an HRESULT */
1336 static void test_hresult_marshaling(void)
1339 HRESULT hr_marshaled
= 0;
1340 IStream
*pStream
= NULL
;
1341 static const HRESULT E_DEADBEEF
= 0xdeadbeef;
1343 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
1344 ok_ole_success(hr
, CreateStreamOnHGlobal
);
1346 hr
= CoMarshalHresult(pStream
, E_DEADBEEF
);
1347 ok_ole_success(hr
, CoMarshalHresult
);
1349 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
1350 hr
= IStream_Read(pStream
, &hr_marshaled
, sizeof(HRESULT
), NULL
);
1351 ok_ole_success(hr
, IStream_Read
);
1353 ok(hr_marshaled
== E_DEADBEEF
, "Didn't marshal HRESULT as expected: got value 0x%08x instead\n", hr_marshaled
);
1356 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
1357 hr
= CoUnmarshalHresult(pStream
, &hr_marshaled
);
1358 ok_ole_success(hr
, CoUnmarshalHresult
);
1360 ok(hr_marshaled
== E_DEADBEEF
, "Didn't marshal HRESULT as expected: got value 0x%08x instead\n", hr_marshaled
);
1362 IStream_Release(pStream
);
1366 /* helper for test_proxy_used_in_wrong_thread */
1367 static DWORD CALLBACK
bad_thread_proc(LPVOID p
)
1369 IClassFactory
* cf
= (IClassFactory
*)p
;
1371 IUnknown
* proxy
= NULL
;
1373 hr
= IClassFactory_CreateInstance(cf
, NULL
, &IID_IUnknown
, (LPVOID
*)&proxy
);
1375 ok(hr
== CO_E_NOTINITIALIZED
,
1376 "COM should have failed with CO_E_NOTINITIALIZED on using proxy without apartment, but instead returned 0x%08x\n",
1379 hr
= IClassFactory_QueryInterface(cf
, &IID_IMultiQI
, (LPVOID
*)&proxy
);
1380 /* Win9x returns S_OK, whilst NT returns RPC_E_WRONG_THREAD */
1381 trace("call to proxy's QueryInterface for local interface without apartment returned 0x%08x\n", hr
);
1383 IUnknown_Release(proxy
);
1385 hr
= IClassFactory_QueryInterface(cf
, &IID_IStream
, (LPVOID
*)&proxy
);
1386 /* Win9x returns E_NOINTERFACE, whilst NT returns RPC_E_WRONG_THREAD */
1387 trace("call to proxy's QueryInterface without apartment returned 0x%08x\n", hr
);
1389 IUnknown_Release(proxy
);
1391 pCoInitializeEx(NULL
, COINIT_MULTITHREADED
);
1393 hr
= IClassFactory_CreateInstance(cf
, NULL
, &IID_IUnknown
, (LPVOID
*)&proxy
);
1394 if (proxy
) IUnknown_Release(proxy
);
1395 ok(hr
== RPC_E_WRONG_THREAD
,
1396 "COM should have failed with RPC_E_WRONG_THREAD on using proxy from wrong apartment, but instead returned 0x%08x\n",
1399 hr
= IClassFactory_QueryInterface(cf
, &IID_IStream
, (LPVOID
*)&proxy
);
1400 /* Win9x returns E_NOINTERFACE, whilst NT returns RPC_E_WRONG_THREAD */
1401 trace("call to proxy's QueryInterface from wrong apartment returned 0x%08x\n", hr
);
1403 /* this statement causes Win9x DCOM to crash during CoUninitialize of
1404 * other apartment, so don't test this on Win9x (signified by NT-only
1405 * export of CoRegisterSurrogateEx) */
1406 if (GetProcAddress(GetModuleHandle("ole32"), "CoRegisterSurrogateEx"))
1407 /* now be really bad and release the proxy from the wrong apartment */
1408 IUnknown_Release(cf
);
1410 skip("skipping test for releasing proxy from wrong apartment that will succeed, but cause a crash during CoUninitialize\n");
1417 /* tests failure case of a using a proxy in the wrong apartment */
1418 static void test_proxy_used_in_wrong_thread(void)
1421 IStream
*pStream
= NULL
;
1422 IUnknown
*pProxy
= NULL
;
1429 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
1430 ok_ole_success(hr
, CreateStreamOnHGlobal
);
1431 tid
= start_host_object(pStream
, &IID_IClassFactory
, (IUnknown
*)&Test_ClassFactory
, MSHLFLAGS_NORMAL
, &host_thread
);
1433 ok_more_than_one_lock();
1435 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
1436 hr
= CoUnmarshalInterface(pStream
, &IID_IClassFactory
, (void **)&pProxy
);
1437 ok_ole_success(hr
, CoUnmarshalInterface
);
1438 IStream_Release(pStream
);
1440 ok_more_than_one_lock();
1442 /* do a call that will fail, but result in IRemUnknown being used by the proxy */
1443 IClassFactory_QueryInterface(pProxy
, &IID_IStream
, (LPVOID
*)&pStream
);
1445 /* create a thread that we can misbehave in */
1446 thread
= CreateThread(NULL
, 0, bad_thread_proc
, (LPVOID
)pProxy
, 0, &tid2
);
1448 WaitForSingleObject(thread
, INFINITE
);
1449 CloseHandle(thread
);
1451 /* do release statement on Win9x that we should have done above */
1452 if (!GetProcAddress(GetModuleHandle("ole32"), "CoRegisterSurrogateEx"))
1453 IUnknown_Release(pProxy
);
1457 end_host_object(tid
, host_thread
);
1460 static HRESULT WINAPI
MessageFilter_QueryInterface(IMessageFilter
*iface
, REFIID riid
, void ** ppvObj
)
1462 if (ppvObj
== NULL
) return E_POINTER
;
1464 if (IsEqualGUID(riid
, &IID_IUnknown
) ||
1465 IsEqualGUID(riid
, &IID_IClassFactory
))
1467 *ppvObj
= (LPVOID
)iface
;
1468 IClassFactory_AddRef(iface
);
1472 return E_NOINTERFACE
;
1475 static ULONG WINAPI
MessageFilter_AddRef(IMessageFilter
*iface
)
1477 return 2; /* non-heap object */
1480 static ULONG WINAPI
MessageFilter_Release(IMessageFilter
*iface
)
1482 return 1; /* non-heap object */
1485 static DWORD WINAPI
MessageFilter_HandleInComingCall(
1486 IMessageFilter
*iface
,
1488 HTASK threadIDCaller
,
1490 LPINTERFACEINFO lpInterfaceInfo
)
1492 static int callcount
= 0;
1494 trace("HandleInComingCall\n");
1498 ret
= SERVERCALL_REJECTED
;
1501 ret
= SERVERCALL_RETRYLATER
;
1504 ret
= SERVERCALL_ISHANDLED
;
1511 static DWORD WINAPI
MessageFilter_RetryRejectedCall(
1512 IMessageFilter
*iface
,
1513 HTASK threadIDCallee
,
1517 trace("RetryRejectedCall\n");
1521 static DWORD WINAPI
MessageFilter_MessagePending(
1522 IMessageFilter
*iface
,
1523 HTASK threadIDCallee
,
1525 DWORD dwPendingType
)
1527 trace("MessagePending\n");
1528 return PENDINGMSG_WAITNOPROCESS
;
1531 static const IMessageFilterVtbl MessageFilter_Vtbl
=
1533 MessageFilter_QueryInterface
,
1534 MessageFilter_AddRef
,
1535 MessageFilter_Release
,
1536 MessageFilter_HandleInComingCall
,
1537 MessageFilter_RetryRejectedCall
,
1538 MessageFilter_MessagePending
1541 static IMessageFilter MessageFilter
= { &MessageFilter_Vtbl
};
1543 static void test_message_filter(void)
1546 IStream
*pStream
= NULL
;
1547 IClassFactory
*cf
= NULL
;
1549 IUnknown
*proxy
= NULL
;
1550 IMessageFilter
*prev_filter
= NULL
;
1555 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
1556 ok_ole_success(hr
, CreateStreamOnHGlobal
);
1557 tid
= start_host_object2(pStream
, &IID_IClassFactory
, (IUnknown
*)&Test_ClassFactory
, MSHLFLAGS_NORMAL
, &MessageFilter
, &thread
);
1559 ok_more_than_one_lock();
1561 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
1562 hr
= CoUnmarshalInterface(pStream
, &IID_IClassFactory
, (void **)&cf
);
1563 ok_ole_success(hr
, CoUnmarshalInterface
);
1564 IStream_Release(pStream
);
1566 ok_more_than_one_lock();
1568 hr
= IClassFactory_CreateInstance(cf
, NULL
, &IID_IUnknown
, (LPVOID
*)&proxy
);
1569 ok(hr
== RPC_E_CALL_REJECTED
, "Call should have returned RPC_E_CALL_REJECTED, but return 0x%08x instead\n", hr
);
1570 if (proxy
) IUnknown_Release(proxy
);
1573 hr
= CoRegisterMessageFilter(&MessageFilter
, &prev_filter
);
1574 ok_ole_success(hr
, CoRegisterMessageFilter
);
1576 hr
= IClassFactory_CreateInstance(cf
, NULL
, &IID_IUnknown
, (LPVOID
*)&proxy
);
1577 ok_ole_success(hr
, IClassFactory_CreateInstance
);
1579 IUnknown_Release(proxy
);
1581 IClassFactory_Release(cf
);
1585 end_host_object(tid
, thread
);
1587 hr
= CoRegisterMessageFilter(prev_filter
, NULL
);
1588 ok_ole_success(hr
, CoRegisterMessageFilter
);
1591 /* test failure case of trying to unmarshal from bad stream */
1592 static void test_bad_marshal_stream(void)
1595 IStream
*pStream
= NULL
;
1597 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
1598 ok_ole_success(hr
, CreateStreamOnHGlobal
);
1599 hr
= CoMarshalInterface(pStream
, &IID_IClassFactory
, (IUnknown
*)&Test_ClassFactory
, MSHCTX_INPROC
, NULL
, MSHLFLAGS_NORMAL
);
1600 ok_ole_success(hr
, CoMarshalInterface
);
1602 ok_more_than_one_lock();
1604 /* try to read beyond end of stream */
1605 hr
= CoReleaseMarshalData(pStream
);
1606 ok(hr
== STG_E_READFAULT
, "Should have failed with STG_E_READFAULT, but returned 0x%08x instead\n", hr
);
1608 /* now release for real */
1609 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
1610 hr
= CoReleaseMarshalData(pStream
);
1611 ok_ole_success(hr
, CoReleaseMarshalData
);
1613 IStream_Release(pStream
);
1616 /* tests that proxies implement certain interfaces */
1617 static void test_proxy_interfaces(void)
1620 IStream
*pStream
= NULL
;
1621 IUnknown
*pProxy
= NULL
;
1622 IUnknown
*pOtherUnknown
= NULL
;
1628 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
1629 ok_ole_success(hr
, CreateStreamOnHGlobal
);
1630 tid
= start_host_object(pStream
, &IID_IClassFactory
, (IUnknown
*)&Test_ClassFactory
, MSHLFLAGS_NORMAL
, &thread
);
1632 ok_more_than_one_lock();
1634 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
1635 hr
= CoUnmarshalInterface(pStream
, &IID_IUnknown
, (void **)&pProxy
);
1636 ok_ole_success(hr
, CoUnmarshalInterface
);
1637 IStream_Release(pStream
);
1639 ok_more_than_one_lock();
1641 hr
= IUnknown_QueryInterface(pProxy
, &IID_IUnknown
, (LPVOID
*)&pOtherUnknown
);
1642 ok_ole_success(hr
, IUnknown_QueryInterface IID_IUnknown
);
1643 if (hr
== S_OK
) IUnknown_Release(pOtherUnknown
);
1645 hr
= IUnknown_QueryInterface(pProxy
, &IID_IClientSecurity
, (LPVOID
*)&pOtherUnknown
);
1646 ok_ole_success(hr
, IUnknown_QueryInterface IID_IClientSecurity
);
1647 if (hr
== S_OK
) IUnknown_Release(pOtherUnknown
);
1649 hr
= IUnknown_QueryInterface(pProxy
, &IID_IMultiQI
, (LPVOID
*)&pOtherUnknown
);
1650 ok_ole_success(hr
, IUnknown_QueryInterface IID_IMultiQI
);
1651 if (hr
== S_OK
) IUnknown_Release(pOtherUnknown
);
1653 hr
= IUnknown_QueryInterface(pProxy
, &IID_IMarshal
, (LPVOID
*)&pOtherUnknown
);
1654 ok_ole_success(hr
, IUnknown_QueryInterface IID_IMarshal
);
1655 if (hr
== S_OK
) IUnknown_Release(pOtherUnknown
);
1657 /* IMarshal2 is also supported on NT-based systems, but is pretty much
1658 * useless as it has no more methods over IMarshal that it inherits from. */
1660 IUnknown_Release(pProxy
);
1664 end_host_object(tid
, thread
);
1669 const IUnknownVtbl
*lpVtbl
;
1673 static HRESULT WINAPI
HeapUnknown_QueryInterface(IUnknown
*iface
, REFIID riid
, void **ppv
)
1675 if (IsEqualIID(riid
, &IID_IUnknown
))
1677 IUnknown_AddRef(iface
);
1678 *ppv
= (LPVOID
)iface
;
1682 return E_NOINTERFACE
;
1685 static ULONG WINAPI
HeapUnknown_AddRef(IUnknown
*iface
)
1687 HeapUnknown
*This
= (HeapUnknown
*)iface
;
1688 return InterlockedIncrement((LONG
*)&This
->refs
);
1691 static ULONG WINAPI
HeapUnknown_Release(IUnknown
*iface
)
1693 HeapUnknown
*This
= (HeapUnknown
*)iface
;
1694 ULONG refs
= InterlockedDecrement((LONG
*)&This
->refs
);
1695 if (!refs
) HeapFree(GetProcessHeap(), 0, This
);
1699 static const IUnknownVtbl HeapUnknown_Vtbl
=
1701 HeapUnknown_QueryInterface
,
1706 static void test_proxybuffer(REFIID riid
)
1709 IPSFactoryBuffer
*psfb
;
1710 IRpcProxyBuffer
*proxy
;
1714 HeapUnknown
*pUnkOuter
= HeapAlloc(GetProcessHeap(), 0, sizeof(*pUnkOuter
));
1716 pUnkOuter
->lpVtbl
= &HeapUnknown_Vtbl
;
1717 pUnkOuter
->refs
= 1;
1719 hr
= CoGetPSClsid(riid
, &clsid
);
1720 ok_ole_success(hr
, CoGetPSClsid
);
1722 hr
= CoGetClassObject(&clsid
, CLSCTX_INPROC_SERVER
, NULL
, &IID_IPSFactoryBuffer
, (LPVOID
*)&psfb
);
1723 ok_ole_success(hr
, CoGetClassObject
);
1725 hr
= IPSFactoryBuffer_CreateProxy(psfb
, (IUnknown
*)pUnkOuter
, riid
, &proxy
, &lpvtbl
);
1726 ok_ole_success(hr
, IPSFactoryBuffer_CreateProxy
);
1727 ok(lpvtbl
!= NULL
, "IPSFactoryBuffer_CreateProxy succeeded, but returned a NULL vtable!\n");
1729 /* release our reference to the outer unknown object - the PS factory
1730 * buffer will have AddRef's it in the CreateProxy call */
1731 refs
= IUnknown_Release((IUnknown
*)pUnkOuter
);
1732 ok(refs
== 1, "Ref count of outer unknown should have been 1 instead of %d\n", refs
);
1734 refs
= IPSFactoryBuffer_Release(psfb
);
1737 /* not reliable on native. maybe it leaks references! */
1738 ok(refs
== 0, "Ref-count leak of %d on IPSFactoryBuffer\n", refs
);
1741 refs
= IUnknown_Release((IUnknown
*)lpvtbl
);
1742 ok(refs
== 0, "Ref-count leak of %d on IRpcProxyBuffer\n", refs
);
1744 refs
= IRpcProxyBuffer_Release(proxy
);
1745 ok(refs
== 0, "Ref-count leak of %d on IRpcProxyBuffer\n", refs
);
1748 static void test_stubbuffer(REFIID riid
)
1751 IPSFactoryBuffer
*psfb
;
1752 IRpcStubBuffer
*stub
;
1758 hr
= CoGetPSClsid(riid
, &clsid
);
1759 ok_ole_success(hr
, CoGetPSClsid
);
1761 hr
= CoGetClassObject(&clsid
, CLSCTX_INPROC_SERVER
, NULL
, &IID_IPSFactoryBuffer
, (LPVOID
*)&psfb
);
1762 ok_ole_success(hr
, CoGetClassObject
);
1764 hr
= IPSFactoryBuffer_CreateStub(psfb
, riid
, (IUnknown
*)&Test_ClassFactory
, &stub
);
1765 ok_ole_success(hr
, IPSFactoryBuffer_CreateStub
);
1767 refs
= IPSFactoryBuffer_Release(psfb
);
1770 /* not reliable on native. maybe it leaks references */
1771 ok(refs
== 0, "Ref-count leak of %d on IPSFactoryBuffer\n", refs
);
1774 ok_more_than_one_lock();
1776 IRpcStubBuffer_Disconnect(stub
);
1780 refs
= IRpcStubBuffer_Release(stub
);
1781 ok(refs
== 0, "Ref-count leak of %d on IRpcProxyBuffer\n", refs
);
1784 static HWND hwnd_app
;
1786 static HRESULT WINAPI
TestRE_IClassFactory_CreateInstance(
1787 LPCLASSFACTORY iface
,
1788 LPUNKNOWN pUnkOuter
,
1793 if (IsEqualIID(riid
, &IID_IWineTest
))
1795 BOOL ret
= SendMessageTimeout(hwnd_app
, WM_NULL
, 0, 0, SMTO_BLOCK
, 5000, &res
);
1796 ok(ret
, "Timed out sending a message to originating window during RPC call\n");
1801 static const IClassFactoryVtbl TestREClassFactory_Vtbl
=
1803 Test_IClassFactory_QueryInterface
,
1804 Test_IClassFactory_AddRef
,
1805 Test_IClassFactory_Release
,
1806 TestRE_IClassFactory_CreateInstance
,
1807 Test_IClassFactory_LockServer
1810 IClassFactory TestRE_ClassFactory
= { &TestREClassFactory_Vtbl
};
1812 static LRESULT CALLBACK
window_proc(HWND hwnd
, UINT msg
, WPARAM wparam
, LPARAM lparam
)
1819 IStream
*pStream
= NULL
;
1820 IClassFactory
*proxy
= NULL
;
1827 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
1828 ok_ole_success(hr
, CreateStreamOnHGlobal
);
1829 tid
= start_host_object(pStream
, &IID_IClassFactory
, (IUnknown
*)&TestRE_ClassFactory
, MSHLFLAGS_NORMAL
, &thread
);
1831 ok_more_than_one_lock();
1833 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
1834 hr
= CoUnmarshalInterface(pStream
, &IID_IClassFactory
, (void **)&proxy
);
1835 ok_ole_success(hr
, CoReleaseMarshalData
);
1836 IStream_Release(pStream
);
1838 ok_more_than_one_lock();
1840 /* note the use of the magic IID_IWineTest value to tell remote thread
1841 * to try to send a message back to us */
1842 hr
= IClassFactory_CreateInstance(proxy
, NULL
, &IID_IWineTest
, (void **)&object
);
1844 IClassFactory_Release(proxy
);
1848 end_host_object(tid
, thread
);
1850 PostMessage(hwnd
, WM_QUIT
, 0, 0);
1857 IStream
*pStream
= NULL
;
1858 IClassFactory
*proxy
= NULL
;
1865 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
1866 ok_ole_success(hr
, CreateStreamOnHGlobal
);
1867 tid
= start_host_object(pStream
, &IID_IClassFactory
, (IUnknown
*)&TestRE_ClassFactory
, MSHLFLAGS_NORMAL
, &thread
);
1869 ok_more_than_one_lock();
1871 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
1872 hr
= CoUnmarshalInterface(pStream
, &IID_IClassFactory
, (void **)&proxy
);
1873 ok_ole_success(hr
, CoReleaseMarshalData
);
1874 IStream_Release(pStream
);
1876 ok_more_than_one_lock();
1878 /* post quit message before a doing a COM call to show that a pending
1879 * WM_QUIT message doesn't stop the call from succeeding */
1880 PostMessage(hwnd
, WM_QUIT
, 0, 0);
1881 hr
= IClassFactory_CreateInstance(proxy
, NULL
, &IID_IUnknown
, (void **)&object
);
1883 IClassFactory_Release(proxy
);
1887 end_host_object(tid
, thread
);
1894 IStream
*pStream
= NULL
;
1895 IClassFactory
*proxy
= NULL
;
1900 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
1901 ok_ole_success(hr
, CreateStreamOnHGlobal
);
1902 tid
= start_host_object(pStream
, &IID_IClassFactory
, (IUnknown
*)&Test_ClassFactory
, MSHLFLAGS_NORMAL
, &thread
);
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 /* shows that COM calls executed during the processing of sent
1910 * messages should fail */
1911 hr
= IClassFactory_CreateInstance(proxy
, NULL
, &IID_IUnknown
, (void **)&object
);
1912 ok(hr
== RPC_E_CANTCALLOUT_ININPUTSYNCCALL
,
1913 "COM call during processing of sent message should return RPC_E_CANTCALLOUT_ININPUTSYNCCALL instead of 0x%08x\n", hr
);
1915 IClassFactory_Release(proxy
);
1917 end_host_object(tid
, thread
);
1924 return DefWindowProc(hwnd
, msg
, wparam
, lparam
);
1928 static void register_test_window(void)
1932 memset(&wndclass
, 0, sizeof(wndclass
));
1933 wndclass
.lpfnWndProc
= window_proc
;
1934 wndclass
.lpszClassName
= "WineCOMTest";
1935 RegisterClass(&wndclass
);
1938 static void test_message_reentrancy(void)
1942 hwnd_app
= CreateWindow("WineCOMTest", NULL
, 0, CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
, NULL
, NULL
, NULL
, 0);
1943 ok(hwnd_app
!= NULL
, "Window creation failed\n");
1945 /* start message re-entrancy test */
1946 PostMessage(hwnd_app
, WM_USER
, 0, 0);
1948 while (GetMessage(&msg
, NULL
, 0, 0))
1950 TranslateMessage(&msg
);
1951 DispatchMessage(&msg
);
1953 DestroyWindow(hwnd_app
);
1956 static HRESULT WINAPI
TestMsg_IClassFactory_CreateInstance(
1957 LPCLASSFACTORY iface
,
1958 LPUNKNOWN pUnkOuter
,
1963 SendMessage(hwnd_app
, WM_USER
+2, 0, 0);
1967 static IClassFactoryVtbl TestMsgClassFactory_Vtbl
=
1969 Test_IClassFactory_QueryInterface
,
1970 Test_IClassFactory_AddRef
,
1971 Test_IClassFactory_Release
,
1972 TestMsg_IClassFactory_CreateInstance
,
1973 Test_IClassFactory_LockServer
1976 IClassFactory TestMsg_ClassFactory
= { &TestMsgClassFactory_Vtbl
};
1978 static void test_call_from_message(void)
1983 IClassFactory
*proxy
;
1988 hwnd_app
= CreateWindow("WineCOMTest", NULL
, 0, CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
, NULL
, NULL
, NULL
, 0);
1989 ok(hwnd_app
!= NULL
, "Window creation failed\n");
1991 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
1992 ok_ole_success(hr
, CreateStreamOnHGlobal
);
1993 tid
= start_host_object(pStream
, &IID_IClassFactory
, (IUnknown
*)&TestMsg_ClassFactory
, MSHLFLAGS_NORMAL
, &thread
);
1995 ok_more_than_one_lock();
1997 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
1998 hr
= CoUnmarshalInterface(pStream
, &IID_IClassFactory
, (void **)&proxy
);
1999 ok_ole_success(hr
, CoReleaseMarshalData
);
2000 IStream_Release(pStream
);
2002 ok_more_than_one_lock();
2004 /* start message re-entrancy test */
2005 hr
= IClassFactory_CreateInstance(proxy
, NULL
, &IID_IUnknown
, (void **)&object
);
2006 ok_ole_success(hr
, IClassFactory_CreateInstance
);
2008 IClassFactory_Release(proxy
);
2012 end_host_object(tid
, thread
);
2014 while (GetMessage(&msg
, NULL
, 0, 0))
2016 TranslateMessage(&msg
);
2017 DispatchMessage(&msg
);
2019 DestroyWindow(hwnd_app
);
2022 static void test_WM_QUIT_handling(void)
2026 hwnd_app
= CreateWindow("WineCOMTest", NULL
, 0, CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
, NULL
, NULL
, NULL
, 0);
2027 ok(hwnd_app
!= NULL
, "Window creation failed\n");
2029 /* start WM_QUIT handling test */
2030 PostMessage(hwnd_app
, WM_USER
+1, 0, 0);
2032 while (GetMessage(&msg
, NULL
, 0, 0))
2034 TranslateMessage(&msg
);
2035 DispatchMessage(&msg
);
2039 static void test_freethreadedmarshaldata(IStream
*pStream
, MSHCTX mshctx
, void *ptr
, DWORD mshlflags
)
2046 hr
= GetHGlobalFromStream(pStream
, &hglobal
);
2047 ok_ole_success(hr
, GetHGlobalFromStream
);
2049 size
= GlobalSize(hglobal
);
2051 marshal_data
= (char *)GlobalLock(hglobal
);
2053 if (mshctx
== MSHCTX_INPROC
)
2055 DWORD expected_size
= sizeof(DWORD
) + sizeof(void *) + sizeof(DWORD
) + sizeof(GUID
);
2056 ok(size
== expected_size
, "size should have been %d instead of %d\n", expected_size
, size
);
2058 ok(*(DWORD
*)marshal_data
== mshlflags
, "expected 0x%x, but got 0x%x for mshctx\n", mshlflags
, *(DWORD
*)marshal_data
);
2059 marshal_data
+= sizeof(DWORD
);
2060 ok(*(void **)marshal_data
== ptr
, "expected %p, but got %p for mshctx\n", ptr
, *(void **)marshal_data
);
2061 marshal_data
+= sizeof(void *);
2062 ok(*(DWORD
*)marshal_data
== 0, "expected 0x0, but got 0x%x\n", *(DWORD
*)marshal_data
);
2063 marshal_data
+= sizeof(DWORD
);
2064 trace("got guid data: {%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\n",
2065 ((GUID
*)marshal_data
)->Data1
, ((GUID
*)marshal_data
)->Data2
, ((GUID
*)marshal_data
)->Data3
,
2066 ((GUID
*)marshal_data
)->Data4
[0], ((GUID
*)marshal_data
)->Data4
[1], ((GUID
*)marshal_data
)->Data4
[2], ((GUID
*)marshal_data
)->Data4
[3],
2067 ((GUID
*)marshal_data
)->Data4
[4], ((GUID
*)marshal_data
)->Data4
[5], ((GUID
*)marshal_data
)->Data4
[6], ((GUID
*)marshal_data
)->Data4
[7]);
2071 ok(size
> sizeof(DWORD
), "size should have been > sizeof(DWORD), not %d\n", size
);
2072 ok(*(DWORD
*)marshal_data
== 0x574f454d /* MEOW */,
2073 "marshal data should be filled by standard marshal and start with MEOW signature\n");
2076 GlobalUnlock(hglobal
);
2079 static void test_freethreadedmarshaler(void)
2082 IUnknown
*pFTUnknown
;
2083 IMarshal
*pFTMarshal
;
2086 static const LARGE_INTEGER llZero
;
2089 hr
= CoCreateFreeThreadedMarshaler(NULL
, &pFTUnknown
);
2090 ok_ole_success(hr
, CoCreateFreeThreadedMarshaler
);
2091 hr
= IUnknown_QueryInterface(pFTUnknown
, &IID_IMarshal
, (void **)&pFTMarshal
);
2092 ok_ole_success(hr
, IUnknown_QueryInterface
);
2093 IUnknown_Release(pFTUnknown
);
2095 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
2096 ok_ole_success(hr
, CreateStreamOnHGlobal
);
2098 /* inproc normal marshaling */
2100 hr
= IMarshal_MarshalInterface(pFTMarshal
, pStream
, &IID_IClassFactory
,
2101 (IUnknown
*)&Test_ClassFactory
, MSHCTX_INPROC
, NULL
, MSHLFLAGS_NORMAL
);
2102 ok_ole_success(hr
, IMarshal_MarshalInterface
);
2104 ok_more_than_one_lock();
2106 test_freethreadedmarshaldata(pStream
, MSHCTX_INPROC
, &Test_ClassFactory
, MSHLFLAGS_NORMAL
);
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
);
2112 IUnknown_Release(pProxy
);
2116 /* native doesn't allow us to unmarshal or release the stream data,
2117 * presumably because it wants us to call CoMarshalInterface instead */
2120 /* local normal marshaling */
2122 IStream_Seek(pStream
, llZero
, STREAM_SEEK_SET
, NULL
);
2123 hr
= IMarshal_MarshalInterface(pFTMarshal
, pStream
, &IID_IClassFactory
, (IUnknown
*)&Test_ClassFactory
, MSHCTX_LOCAL
, NULL
, MSHLFLAGS_NORMAL
);
2124 ok_ole_success(hr
, IMarshal_MarshalInterface
);
2126 ok_more_than_one_lock();
2128 test_freethreadedmarshaldata(pStream
, MSHCTX_LOCAL
, &Test_ClassFactory
, MSHLFLAGS_NORMAL
);
2130 IStream_Seek(pStream
, llZero
, STREAM_SEEK_SET
, NULL
);
2131 hr
= IMarshal_ReleaseMarshalData(pFTMarshal
, pStream
);
2132 ok_ole_success(hr
, IMarshal_ReleaseMarshalData
);
2137 /* inproc table-strong marshaling */
2139 IStream_Seek(pStream
, llZero
, STREAM_SEEK_SET
, NULL
);
2140 hr
= IMarshal_MarshalInterface(pFTMarshal
, pStream
, &IID_IClassFactory
,
2141 (IUnknown
*)&Test_ClassFactory
, MSHCTX_INPROC
, (void *)0xdeadbeef,
2142 MSHLFLAGS_TABLESTRONG
);
2143 ok_ole_success(hr
, IMarshal_MarshalInterface
);
2145 ok_more_than_one_lock();
2147 test_freethreadedmarshaldata(pStream
, MSHCTX_INPROC
, &Test_ClassFactory
, MSHLFLAGS_TABLESTRONG
);
2149 IStream_Seek(pStream
, llZero
, STREAM_SEEK_SET
, NULL
);
2150 hr
= IMarshal_UnmarshalInterface(pFTMarshal
, pStream
, &IID_IUnknown
, (void **)&pProxy
);
2151 ok_ole_success(hr
, IMarshal_UnmarshalInterface
);
2153 IUnknown_Release(pProxy
);
2155 ok_more_than_one_lock();
2157 IStream_Seek(pStream
, llZero
, STREAM_SEEK_SET
, NULL
);
2158 hr
= IMarshal_ReleaseMarshalData(pFTMarshal
, pStream
);
2159 ok_ole_success(hr
, IMarshal_ReleaseMarshalData
);
2163 /* inproc table-weak marshaling */
2165 IStream_Seek(pStream
, llZero
, STREAM_SEEK_SET
, NULL
);
2166 hr
= IMarshal_MarshalInterface(pFTMarshal
, pStream
, &IID_IClassFactory
,
2167 (IUnknown
*)&Test_ClassFactory
, MSHCTX_INPROC
, (void *)0xdeadbeef,
2168 MSHLFLAGS_TABLEWEAK
);
2169 ok_ole_success(hr
, IMarshal_MarshalInterface
);
2173 test_freethreadedmarshaldata(pStream
, MSHCTX_INPROC
, &Test_ClassFactory
, MSHLFLAGS_TABLEWEAK
);
2175 IStream_Seek(pStream
, llZero
, STREAM_SEEK_SET
, NULL
);
2176 hr
= IMarshal_UnmarshalInterface(pFTMarshal
, pStream
, &IID_IUnknown
, (void **)&pProxy
);
2177 ok_ole_success(hr
, IMarshal_UnmarshalInterface
);
2179 ok_more_than_one_lock();
2181 IUnknown_Release(pProxy
);
2185 /* inproc normal marshaling (for extraordinary cases) */
2187 IStream_Seek(pStream
, llZero
, STREAM_SEEK_SET
, NULL
);
2188 hr
= IMarshal_MarshalInterface(pFTMarshal
, pStream
, &IID_IClassFactory
,
2189 (IUnknown
*)&Test_ClassFactory
, MSHCTX_INPROC
, NULL
, MSHLFLAGS_NORMAL
);
2190 ok_ole_success(hr
, IMarshal_MarshalInterface
);
2192 ok_more_than_one_lock();
2194 /* this call shows that DisconnectObject does nothing */
2195 hr
= IMarshal_DisconnectObject(pFTMarshal
, 0);
2196 ok_ole_success(hr
, IMarshal_DisconnectObject
);
2198 ok_more_than_one_lock();
2200 IStream_Seek(pStream
, llZero
, STREAM_SEEK_SET
, NULL
);
2201 hr
= IMarshal_ReleaseMarshalData(pFTMarshal
, pStream
);
2202 ok_ole_success(hr
, IMarshal_ReleaseMarshalData
);
2206 /* doesn't enforce marshaling rules here and allows us to unmarshal the
2207 * interface, even though it was freed above */
2208 IStream_Seek(pStream
, llZero
, STREAM_SEEK_SET
, NULL
);
2209 hr
= IMarshal_UnmarshalInterface(pFTMarshal
, pStream
, &IID_IUnknown
, (void **)&pProxy
);
2210 ok_ole_success(hr
, IMarshal_UnmarshalInterface
);
2214 IStream_Release(pStream
);
2215 IMarshal_Release(pFTMarshal
);
2218 static void test_inproc_handler(void)
2226 DWORD dwDisposition
;
2229 hr
= StringFromCLSID(&CLSID_WineTest
, &pszClsid
);
2230 ok_ole_success(hr
, "StringFromCLSID");
2231 strcpy(buffer
, "CLSID\\");
2232 WideCharToMultiByte(CP_ACP
, 0, pszClsid
, -1, buffer
+ strlen(buffer
), sizeof(buffer
) - strlen(buffer
), NULL
, NULL
);
2233 CoTaskMemFree(pszClsid
);
2234 strcat(buffer
, "\\InprocHandler32");
2235 error
= RegCreateKeyEx(HKEY_CLASSES_ROOT
, buffer
, 0, NULL
, 0, KEY_SET_VALUE
, NULL
, &hkey
, &dwDisposition
);
2236 ok(error
== ERROR_SUCCESS
, "RegCreateKeyEx failed with error %d\n", error
);
2237 error
= RegSetValueEx(hkey
, NULL
, 0, REG_SZ
, (const unsigned char *)"ole32.dll", strlen("ole32.dll") + 1);
2238 ok(error
== ERROR_SUCCESS
, "RegSetValueEx failed with error %d\n", error
);
2241 hr
= CoCreateInstance(&CLSID_WineTest
, NULL
, CLSCTX_INPROC_HANDLER
, &IID_IUnknown
, (void **)&pObject
);
2243 ok_ole_success(hr
, "CoCreateInstance");
2247 hr
= IUnknown_QueryInterface(pObject
, &IID_IWineTest
, (void **)&pObject2
);
2248 ok(hr
== E_NOINTERFACE
, "IUnknown_QueryInterface on handler for invalid interface returned 0x%08x instead of E_NOINTERFACE\n", hr
);
2250 /* it's a handler as it supports IOleObject */
2251 hr
= IUnknown_QueryInterface(pObject
, &IID_IOleObject
, (void **)&pObject2
);
2252 ok_ole_success(hr
, "IUnknown_QueryInterface(&IID_IOleObject)");
2253 IUnknown_Release(pObject2
);
2255 IUnknown_Release(pObject
);
2258 RegDeleteKey(HKEY_CLASSES_ROOT
, buffer
);
2259 *strrchr(buffer
, '\\') = '\0';
2260 RegDeleteKey(HKEY_CLASSES_ROOT
, buffer
);
2263 static HRESULT WINAPI
Test_SMI_QueryInterface(
2264 IStdMarshalInfo
*iface
,
2268 if (ppvObj
== NULL
) return E_POINTER
;
2270 if (IsEqualGUID(riid
, &IID_IUnknown
) ||
2271 IsEqualGUID(riid
, &IID_IStdMarshalInfo
))
2273 *ppvObj
= (LPVOID
)iface
;
2274 IClassFactory_AddRef(iface
);
2278 return E_NOINTERFACE
;
2281 static ULONG WINAPI
Test_SMI_AddRef(IStdMarshalInfo
*iface
)
2284 return 2; /* non-heap-based object */
2287 static ULONG WINAPI
Test_SMI_Release(IStdMarshalInfo
*iface
)
2290 return 1; /* non-heap-based object */
2293 static HRESULT WINAPI
Test_SMI_GetClassForHandler(
2294 IStdMarshalInfo
*iface
,
2295 DWORD dwDestContext
,
2296 void *pvDestContext
,
2299 *pClsid
= CLSID_WineTest
;
2303 static const IStdMarshalInfoVtbl Test_SMI_Vtbl
=
2305 Test_SMI_QueryInterface
,
2308 Test_SMI_GetClassForHandler
2311 static IStdMarshalInfo Test_SMI
= {&Test_SMI_Vtbl
};
2313 static void test_handler_marshaling(void)
2316 IStream
*pStream
= NULL
;
2317 IUnknown
*pProxy
= NULL
;
2321 static const LARGE_INTEGER ullZero
;
2325 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
2326 ok_ole_success(hr
, "CreateStreamOnHGlobal");
2327 tid
= start_host_object(pStream
, &IID_IUnknown
, (IUnknown
*)&Test_SMI
, MSHLFLAGS_NORMAL
, &thread
);
2329 ok_more_than_one_lock();
2331 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
2332 hr
= CoUnmarshalInterface(pStream
, &IID_IUnknown
, (void **)&pProxy
);
2333 ok_ole_success(hr
, "CoUnmarshalInterface");
2334 IStream_Release(pStream
);
2336 ok_more_than_one_lock();
2338 hr
= IUnknown_QueryInterface(pProxy
, &IID_IWineTest
, (void **)&pObject
);
2339 ok(hr
== E_NOINTERFACE
, "IUnknown_QueryInterface with unknown IID should have returned E_NOINTERFACE instead of 0x%08x\n", hr
);
2341 /* it's a handler as it supports IOleObject */
2342 hr
= IUnknown_QueryInterface(pProxy
, &IID_IOleObject
, (void **)&pObject
);
2344 ok_ole_success(hr
, "IUnknown_QueryInterface(&IID_IOleObject)");
2345 if (SUCCEEDED(hr
)) IUnknown_Release(pObject
);
2347 IUnknown_Release(pProxy
);
2351 end_host_object(tid
, thread
);
2353 /* FIXME: test IPersist interface has the same effect as IStdMarshalInfo */
2357 static void test_client_security(void)
2360 IStream
*pStream
= NULL
;
2361 IClassFactory
*pProxy
= NULL
;
2362 IUnknown
*pProxy2
= NULL
;
2363 IUnknown
*pUnknown1
= NULL
;
2364 IUnknown
*pUnknown2
= NULL
;
2365 IClientSecurity
*pCliSec
= NULL
;
2369 static const LARGE_INTEGER ullZero
;
2372 OLECHAR
*pServerPrincName
;
2376 DWORD dwCapabilities
;
2381 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
2382 ok_ole_success(hr
, "CreateStreamOnHGlobal");
2383 tid
= start_host_object(pStream
, &IID_IClassFactory
, (IUnknown
*)&Test_ClassFactory
, MSHLFLAGS_NORMAL
, &thread
);
2385 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
2386 hr
= CoUnmarshalInterface(pStream
, &IID_IClassFactory
, (void **)&pProxy
);
2387 ok_ole_success(hr
, "CoUnmarshalInterface");
2388 IStream_Release(pStream
);
2390 hr
= IUnknown_QueryInterface(pProxy
, &IID_IUnknown
, (LPVOID
*)&pUnknown1
);
2391 ok_ole_success(hr
, "IUnknown_QueryInterface IID_IUnknown");
2393 hr
= IUnknown_QueryInterface(pProxy
, &IID_IRemUnknown
, (LPVOID
*)&pProxy2
);
2394 ok_ole_success(hr
, "IUnknown_QueryInterface IID_IStream");
2396 hr
= IUnknown_QueryInterface(pProxy2
, &IID_IUnknown
, (LPVOID
*)&pUnknown2
);
2397 ok_ole_success(hr
, "IUnknown_QueryInterface IID_IUnknown");
2399 ok(pUnknown1
== pUnknown2
, "both proxy's IUnknowns should be the same - %p, %p\n", pUnknown1
, pUnknown2
);
2401 hr
= IUnknown_QueryInterface(pProxy
, &IID_IMarshal
, (LPVOID
*)&pMarshal
);
2402 ok_ole_success(hr
, "IUnknown_QueryInterface IID_IMarshal");
2404 hr
= IUnknown_QueryInterface(pProxy
, &IID_IClientSecurity
, (LPVOID
*)&pCliSec
);
2405 ok_ole_success(hr
, "IUnknown_QueryInterface IID_IClientSecurity");
2407 hr
= IClientSecurity_QueryBlanket(pCliSec
, (IUnknown
*)pProxy
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
2408 todo_wine
ok_ole_success(hr
, "IClientSecurity_QueryBlanket (all NULLs)");
2410 hr
= IClientSecurity_QueryBlanket(pCliSec
, (IUnknown
*)pMarshal
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
2411 todo_wine
ok(hr
== E_NOINTERFACE
, "IClientSecurity_QueryBlanket with local interface should have returned E_NOINTERFACE instead of 0x%08x\n", hr
);
2413 hr
= IClientSecurity_QueryBlanket(pCliSec
, (IUnknown
*)pProxy
, &dwAuthnSvc
, &dwAuthzSvc
, &pServerPrincName
, &dwAuthnLevel
, &dwImpLevel
, &pAuthInfo
, &dwCapabilities
);
2414 todo_wine
ok_ole_success(hr
, "IClientSecurity_QueryBlanket");
2416 hr
= IClientSecurity_SetBlanket(pCliSec
, (IUnknown
*)pProxy
, dwAuthnSvc
, dwAuthzSvc
, pServerPrincName
, dwAuthnLevel
, RPC_C_IMP_LEVEL_IMPERSONATE
, pAuthInfo
, dwCapabilities
);
2417 todo_wine
ok_ole_success(hr
, "IClientSecurity_SetBlanket");
2419 hr
= IClassFactory_CreateInstance(pProxy
, NULL
, &IID_IWineTest
, &pv
);
2420 ok(hr
== E_NOINTERFACE
, "COM call should have succeeded instead of returning 0x%08x\n", hr
);
2422 hr
= IClientSecurity_SetBlanket(pCliSec
, (IUnknown
*)pMarshal
, dwAuthnSvc
, dwAuthzSvc
, pServerPrincName
, dwAuthnLevel
, dwImpLevel
, pAuthInfo
, dwCapabilities
);
2423 todo_wine
ok(hr
== E_NOINTERFACE
, "IClientSecurity_SetBlanket with local interface should have returned E_NOINTERFACE instead of 0x%08x\n", hr
);
2425 hr
= IClientSecurity_SetBlanket(pCliSec
, (IUnknown
*)pProxy
, 0xdeadbeef, dwAuthzSvc
, pServerPrincName
, dwAuthnLevel
, dwImpLevel
, pAuthInfo
, dwCapabilities
);
2426 todo_wine
ok(hr
== E_INVALIDARG
, "IClientSecurity_SetBlanke with invalid dwAuthnSvc should have returned E_INVALIDARG instead of 0x%08x\n", hr
);
2428 CoTaskMemFree(pServerPrincName
);
2430 hr
= IClientSecurity_QueryBlanket(pCliSec
, (IUnknown
*)pUnknown1
, &dwAuthnSvc
, &dwAuthzSvc
, &pServerPrincName
, &dwAuthnLevel
, &dwImpLevel
, &pAuthInfo
, &dwCapabilities
);
2431 todo_wine
ok_ole_success(hr
, "IClientSecurity_QueryBlanket(IUnknown)");
2433 CoTaskMemFree(pServerPrincName
);
2435 IClassFactory_Release(pProxy
);
2436 IUnknown_Release(pProxy2
);
2437 IUnknown_Release(pUnknown1
);
2438 IUnknown_Release(pUnknown2
);
2439 IMarshal_Release(pMarshal
);
2440 IClientSecurity_Release(pCliSec
);
2442 end_host_object(tid
, thread
);
2445 static HANDLE heventShutdown
;
2447 static void LockModuleOOP(void)
2449 InterlockedIncrement(&cLocks
); /* for test purposes only */
2450 CoAddRefServerProcess();
2453 static void UnlockModuleOOP(void)
2455 InterlockedDecrement(&cLocks
); /* for test purposes only */
2456 if (!CoReleaseServerProcess())
2457 SetEvent(heventShutdown
);
2460 static HWND hwnd_app
;
2462 static HRESULT WINAPI
TestOOP_IClassFactory_QueryInterface(
2463 LPCLASSFACTORY iface
,
2467 if (ppvObj
== NULL
) return E_POINTER
;
2469 if (IsEqualGUID(riid
, &IID_IUnknown
) ||
2470 IsEqualGUID(riid
, &IID_IClassFactory
))
2472 *ppvObj
= (LPVOID
)iface
;
2473 IClassFactory_AddRef(iface
);
2477 return E_NOINTERFACE
;
2480 static ULONG WINAPI
TestOOP_IClassFactory_AddRef(LPCLASSFACTORY iface
)
2482 return 2; /* non-heap-based object */
2485 static ULONG WINAPI
TestOOP_IClassFactory_Release(LPCLASSFACTORY iface
)
2487 return 1; /* non-heap-based object */
2490 static HRESULT WINAPI
TestOOP_IClassFactory_CreateInstance(
2491 LPCLASSFACTORY iface
,
2492 LPUNKNOWN pUnkOuter
,
2496 if (IsEqualIID(riid
, &IID_IClassFactory
) || IsEqualIID(riid
, &IID_IUnknown
))
2501 return CLASS_E_CLASSNOTAVAILABLE
;
2504 static HRESULT WINAPI
TestOOP_IClassFactory_LockServer(
2505 LPCLASSFACTORY iface
,
2515 static const IClassFactoryVtbl TestClassFactoryOOP_Vtbl
=
2517 TestOOP_IClassFactory_QueryInterface
,
2518 TestOOP_IClassFactory_AddRef
,
2519 TestOOP_IClassFactory_Release
,
2520 TestOOP_IClassFactory_CreateInstance
,
2521 TestOOP_IClassFactory_LockServer
2524 static IClassFactory TestOOP_ClassFactory
= { &TestClassFactoryOOP_Vtbl
};
2526 static void test_register_local_server(void)
2534 heventShutdown
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2536 hr
= CoRegisterClassObject(&CLSID_WineOOPTest
, (IUnknown
*)&TestOOP_ClassFactory
,
2537 CLSCTX_LOCAL_SERVER
, REGCLS_SINGLEUSE
, &cookie
);
2538 ok_ole_success(hr
, CoRegisterClassObject
);
2540 ready_event
= CreateEvent(NULL
, FALSE
, FALSE
, "Wine COM Test Ready Event");
2541 SetEvent(ready_event
);
2543 quit_event
= CreateEvent(NULL
, FALSE
, FALSE
, "Wine COM Test Quit Event");
2547 wait
= MsgWaitForMultipleObjects(1, &quit_event
, FALSE
, INFINITE
, QS_ALLINPUT
);
2548 if (wait
== WAIT_OBJECT_0
+1)
2551 BOOL ret
= PeekMessage(&msg
, NULL
, 0, 0, PM_REMOVE
);
2554 trace("Message 0x%x\n", msg
.message
);
2555 TranslateMessage(&msg
);
2556 DispatchMessage(&msg
);
2560 while (wait
== WAIT_OBJECT_0
+1);
2562 hr
= CoRevokeClassObject(cookie
);
2563 ok_ole_success(hr
, CoRevokeClassObject
);
2566 static HANDLE
create_target_process(const char *arg
)
2569 char cmdline
[MAX_PATH
];
2570 PROCESS_INFORMATION pi
;
2571 STARTUPINFO si
= { 0 };
2576 winetest_get_mainargs( &argv
);
2577 sprintf(cmdline
, "%s %s %s", argv
[0], argv
[1], arg
);
2578 ok(CreateProcess(argv
[0], cmdline
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
,
2579 &si
, &pi
) != 0, "CreateProcess failed with error: %u\n", GetLastError());
2580 if (pi
.hThread
) CloseHandle(pi
.hThread
);
2584 /* tests functions commonly used by out of process COM servers */
2585 static void test_local_server(void)
2595 heventShutdown
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2599 /* Start the object suspended */
2600 hr
= CoRegisterClassObject(&CLSID_WineOOPTest
, (IUnknown
*)&TestOOP_ClassFactory
,
2601 CLSCTX_LOCAL_SERVER
, REGCLS_MULTIPLEUSE
| REGCLS_SUSPENDED
, &cookie
);
2602 ok_ole_success(hr
, CoRegisterClassObject
);
2604 /* ... and CoGetClassObject does not find it and fails when it looks for the
2605 * class in the registry */
2606 hr
= CoGetClassObject(&CLSID_WineOOPTest
, CLSCTX_INPROC_SERVER
,
2607 NULL
, &IID_IClassFactory
, (LPVOID
*)&cf
);
2608 ok(hr
== REGDB_E_CLASSNOTREG
|| /* NT */
2609 hr
== S_OK
/* Win9x */,
2610 "CoGetClassObject should have returned REGDB_E_CLASSNOTREG instead of 0x%08x\n", hr
);
2612 /* Resume the object suspended above ... */
2613 hr
= CoResumeClassObjects();
2614 ok_ole_success(hr
, CoResumeClassObjects
);
2616 /* ... and now it should succeed */
2617 hr
= CoGetClassObject(&CLSID_WineOOPTest
, CLSCTX_INPROC_SERVER
,
2618 NULL
, &IID_IClassFactory
, (LPVOID
*)&cf
);
2619 ok_ole_success(hr
, CoGetClassObject
);
2621 /* Now check the locking is working */
2622 /* NOTE: we are accessing the class directly, not through a proxy */
2626 hr
= IClassFactory_LockServer(cf
, TRUE
);
2627 ok_ole_success(hr
, IClassFactory_LockServer
);
2629 ok_more_than_one_lock();
2631 IClassFactory_LockServer(cf
, FALSE
);
2632 ok_ole_success(hr
, IClassFactory_LockServer
);
2636 IClassFactory_Release(cf
);
2638 /* wait for shutdown signal */
2639 ret
= WaitForSingleObject(heventShutdown
, 0);
2640 ok(ret
!= WAIT_TIMEOUT
, "Server didn't shut down\n");
2642 /* try to connect again after SCM has suspended registered class objects */
2643 hr
= CoGetClassObject(&CLSID_WineOOPTest
, CLSCTX_INPROC_SERVER
| CLSCTX_LOCAL_SERVER
, NULL
,
2644 &IID_IClassFactory
, (LPVOID
*)&cf
);
2645 ok(hr
== CO_E_SERVER_STOPPING
|| /* NT */
2646 hr
== REGDB_E_CLASSNOTREG
|| /* win2k */
2647 hr
== S_OK
/* Win9x */,
2648 "CoGetClassObject should have returned CO_E_SERVER_STOPPING or REGDB_E_CLASSNOTREG instead of 0x%08x\n", hr
);
2650 hr
= CoRevokeClassObject(cookie
);
2651 ok_ole_success(hr
, CoRevokeClassObject
);
2653 CloseHandle(heventShutdown
);
2655 process
= create_target_process("-Embedding");
2656 ok(process
!= NULL
, "couldn't start local server process, error was %d\n", GetLastError());
2658 ready_event
= CreateEvent(NULL
, FALSE
, FALSE
, "Wine COM Test Ready Event");
2659 WaitForSingleObject(ready_event
, INFINITE
);
2660 CloseHandle(ready_event
);
2662 hr
= CoCreateInstance(&CLSID_WineOOPTest
, NULL
, CLSCTX_LOCAL_SERVER
, &IID_IClassFactory
, (void **)&cf
);
2663 ok_ole_success(hr
, CoCreateInstance
);
2665 IClassFactory_Release(cf
);
2667 hr
= CoCreateInstance(&CLSID_WineOOPTest
, NULL
, CLSCTX_LOCAL_SERVER
, &IID_IClassFactory
, (void **)&cf
);
2668 ok(hr
== REGDB_E_CLASSNOTREG
, "Second CoCreateInstance on REGCLS_SINGLEUSE object should have failed\n");
2670 quit_event
= CreateEvent(NULL
, FALSE
, FALSE
, "Wine COM Test Quit Event");
2671 SetEvent(quit_event
);
2673 winetest_wait_child_process( process
);
2674 CloseHandle(quit_event
);
2675 CloseHandle(process
);
2681 IGlobalInterfaceTable
*git
;
2684 static DWORD CALLBACK
get_global_interface_proc(LPVOID pv
)
2687 struct git_params
*params
= (struct git_params
*)pv
;
2690 hr
= IGlobalInterfaceTable_GetInterfaceFromGlobal(params
->git
, params
->cookie
, &IID_IClassFactory
, (void **)&cf
);
2691 ok(hr
== CO_E_NOTINITIALIZED
||
2692 hr
== E_UNEXPECTED
, /* win2k */
2693 "IGlobalInterfaceTable_GetInterfaceFromGlobal should have failed with error CO_E_NOTINITIALIZED or E_UNEXPECTED instead of 0x%08x\n",
2698 hr
= IGlobalInterfaceTable_GetInterfaceFromGlobal(params
->git
, params
->cookie
, &IID_IClassFactory
, (void **)&cf
);
2699 ok_ole_success(hr
, IGlobalInterfaceTable_GetInterfaceFromGlobal
);
2701 IClassFactory_Release(cf
);
2708 static void test_globalinterfacetable(void)
2711 IGlobalInterfaceTable
*git
;
2715 struct git_params params
;
2719 trace("test_globalinterfacetable\n");
2722 hr
= CoCreateInstance(&CLSID_StdGlobalInterfaceTable
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IGlobalInterfaceTable
, (void **)&git
);
2723 ok_ole_success(hr
, CoCreateInstance
);
2725 hr
= IGlobalInterfaceTable_RegisterInterfaceInGlobal(git
, (IUnknown
*)&Test_ClassFactory
, &IID_IClassFactory
, &cookie
);
2726 ok_ole_success(hr
, IGlobalInterfaceTable_RegisterInterfaceInGlobal
);
2728 ok_more_than_one_lock();
2730 params
.cookie
= cookie
;
2732 /* note: params is on stack so we MUST wait for get_global_interface_proc
2733 * to exit before we can return */
2734 thread
= CreateThread(NULL
, 0, get_global_interface_proc
, ¶ms
, 0, &tid
);
2736 ret
= MsgWaitForMultipleObjects(1, &thread
, FALSE
, INFINITE
, QS_ALLINPUT
);
2737 while (ret
== WAIT_OBJECT_0
+ 1)
2740 while (PeekMessage(&msg
, NULL
, 0, 0, PM_REMOVE
))
2741 DispatchMessage(&msg
);
2742 ret
= MsgWaitForMultipleObjects(1, &thread
, FALSE
, INFINITE
, QS_ALLINPUT
);
2745 CloseHandle(thread
);
2747 /* test getting interface from global with different iid */
2748 hr
= IGlobalInterfaceTable_GetInterfaceFromGlobal(git
, cookie
, &IID_IUnknown
, (void **)&object
);
2749 ok_ole_success(hr
, IGlobalInterfaceTable_GetInterfaceFromGlobal
);
2750 IUnknown_Release(object
);
2752 /* test getting interface from global with same iid */
2753 hr
= IGlobalInterfaceTable_GetInterfaceFromGlobal(git
, cookie
, &IID_IClassFactory
, (void **)&object
);
2754 ok_ole_success(hr
, IGlobalInterfaceTable_GetInterfaceFromGlobal
);
2755 IUnknown_Release(object
);
2757 hr
= IGlobalInterfaceTable_RevokeInterfaceFromGlobal(git
, cookie
);
2758 ok_ole_success(hr
, IGlobalInterfaceTable_RevokeInterfaceFromGlobal
);
2762 IGlobalInterfaceTable_Release(git
);
2765 static const char *debugstr_iid(REFIID riid
)
2767 static char name
[256];
2771 LONG name_size
= sizeof(name
);
2772 StringFromGUID2(riid
, bufferW
, sizeof(bufferW
)/sizeof(bufferW
[0]));
2773 WideCharToMultiByte(CP_ACP
, 0, bufferW
, sizeof(bufferW
)/sizeof(bufferW
[0]), buffer
, sizeof(buffer
), NULL
, NULL
);
2774 if (RegOpenKeyEx(HKEY_CLASSES_ROOT
, "Interface", 0, KEY_QUERY_VALUE
, &hkeyInterface
) != ERROR_SUCCESS
)
2776 memcpy(name
, buffer
, sizeof(buffer
));
2779 if (RegQueryValue(hkeyInterface
, buffer
, name
, &name_size
) != ERROR_SUCCESS
)
2781 memcpy(name
, buffer
, sizeof(buffer
));
2784 RegCloseKey(hkeyInterface
);
2789 static HRESULT WINAPI
TestChannelHook_QueryInterface(IChannelHook
*iface
, REFIID riid
, void **ppv
)
2791 if (IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IChannelHook
))
2794 IUnknown_AddRef(iface
);
2799 return E_NOINTERFACE
;
2802 static ULONG WINAPI
TestChannelHook_AddRef(IChannelHook
*iface
)
2807 static ULONG WINAPI
TestChannelHook_Release(IChannelHook
*iface
)
2812 static void WINAPI
TestChannelHook_ClientGetSize(
2813 IChannelHook
*iface
,
2818 SChannelHookCallInfo
*info
= (SChannelHookCallInfo
*)riid
;
2819 trace("TestChannelHook_ClientGetBuffer\n");
2820 trace("\t%s method %d\n", debugstr_iid(riid
), info
->iMethod
);
2821 trace("\tcid: %s\n", debugstr_iid(&info
->uCausality
));
2822 ok(info
->cbSize
== sizeof(*info
), "info->cbSize was %d instead of %d\n", info
->cbSize
, (int)sizeof(*info
));
2823 ok(info
->dwServerPid
== GetCurrentProcessId(), "info->dwServerPid was 0x%x instead of 0x%x\n", info
->dwServerPid
, GetCurrentProcessId());
2824 ok(!info
->pObject
, "info->pObject should be NULL\n");
2825 ok(IsEqualGUID(uExtent
, &EXTENTID_WineTest
), "uExtent wasn't correct\n");
2830 static void WINAPI
TestChannelHook_ClientFillBuffer(
2831 IChannelHook
*iface
,
2837 SChannelHookCallInfo
*info
= (SChannelHookCallInfo
*)riid
;
2838 trace("TestChannelHook_ClientFillBuffer\n");
2839 ok(info
->cbSize
== sizeof(*info
), "info->cbSize was %d instead of %d\n", info
->cbSize
, (int)sizeof(*info
));
2840 ok(info
->dwServerPid
== GetCurrentProcessId(), "info->dwServerPid was 0x%x instead of 0x%x\n", info
->dwServerPid
, GetCurrentProcessId());
2841 ok(!info
->pObject
, "info->pObject should be NULL\n");
2842 ok(IsEqualGUID(uExtent
, &EXTENTID_WineTest
), "uExtent wasn't correct\n");
2844 *(unsigned char *)pDataBuffer
= 0xcc;
2848 static void WINAPI
TestChannelHook_ClientNotify(
2849 IChannelHook
*iface
,
2857 SChannelHookCallInfo
*info
= (SChannelHookCallInfo
*)riid
;
2858 trace("TestChannelHook_ClientNotify hrFault = 0x%08x\n", hrFault
);
2859 ok(info
->cbSize
== sizeof(*info
), "info->cbSize was %d instead of %d\n", info
->cbSize
, (int)sizeof(*info
));
2860 ok(info
->dwServerPid
== GetCurrentProcessId(), "info->dwServerPid was 0x%x instead of 0x%x\n", info
->dwServerPid
, GetCurrentProcessId());
2862 ok(info
->pObject
!= NULL
, "info->pObject shouldn't be NULL\n");
2864 ok(IsEqualGUID(uExtent
, &EXTENTID_WineTest
), "uExtent wasn't correct\n");
2867 static void WINAPI
TestChannelHook_ServerNotify(
2868 IChannelHook
*iface
,
2875 SChannelHookCallInfo
*info
= (SChannelHookCallInfo
*)riid
;
2876 trace("TestChannelHook_ServerNotify\n");
2877 ok(info
->cbSize
== sizeof(*info
), "info->cbSize was %d instead of %d\n", info
->cbSize
, (int)sizeof(*info
));
2878 ok(info
->dwServerPid
== GetCurrentProcessId(), "info->dwServerPid was 0x%x instead of 0x%x\n", info
->dwServerPid
, GetCurrentProcessId());
2879 ok(info
->pObject
!= NULL
, "info->pObject shouldn't be NULL\n");
2880 ok(cbDataSize
== 1, "cbDataSize should have been 1 instead of %d\n", cbDataSize
);
2881 ok(*(unsigned char *)pDataBuffer
== 0xcc, "pDataBuffer should have contained 0xcc instead of 0x%x\n", *(unsigned char *)pDataBuffer
);
2882 ok(IsEqualGUID(uExtent
, &EXTENTID_WineTest
), "uExtent wasn't correct\n");
2885 static void WINAPI
TestChannelHook_ServerGetSize(
2886 IChannelHook
*iface
,
2892 SChannelHookCallInfo
*info
= (SChannelHookCallInfo
*)riid
;
2893 trace("TestChannelHook_ServerGetSize\n");
2894 trace("\t%s method %d\n", debugstr_iid(riid
), info
->iMethod
);
2895 ok(info
->cbSize
== sizeof(*info
), "info->cbSize was %d instead of %d\n", info
->cbSize
, (int)sizeof(*info
));
2896 ok(info
->dwServerPid
== GetCurrentProcessId(), "info->dwServerPid was 0x%x instead of 0x%x\n", info
->dwServerPid
, GetCurrentProcessId());
2897 ok(info
->pObject
!= NULL
, "info->pObject shouldn't be NULL\n");
2898 ok(IsEqualGUID(uExtent
, &EXTENTID_WineTest
), "uExtent wasn't correct\n");
2899 if (hrFault
!= S_OK
)
2900 trace("\thrFault = 0x%08x\n", hrFault
);
2905 static void WINAPI
TestChannelHook_ServerFillBuffer(
2906 IChannelHook
*iface
,
2913 trace("TestChannelHook_ServerFillBuffer\n");
2914 ok(0, "TestChannelHook_ServerFillBuffer shouldn't be called\n");
2917 static const IChannelHookVtbl TestChannelHookVtbl
=
2919 TestChannelHook_QueryInterface
,
2920 TestChannelHook_AddRef
,
2921 TestChannelHook_Release
,
2922 TestChannelHook_ClientGetSize
,
2923 TestChannelHook_ClientFillBuffer
,
2924 TestChannelHook_ClientNotify
,
2925 TestChannelHook_ServerNotify
,
2926 TestChannelHook_ServerGetSize
,
2927 TestChannelHook_ServerFillBuffer
,
2930 static IChannelHook TestChannelHook
= { &TestChannelHookVtbl
};
2932 static void test_channel_hook(void)
2934 IStream
*pStream
= NULL
;
2935 IClassFactory
*cf
= NULL
;
2937 IUnknown
*proxy
= NULL
;
2941 hr
= CoRegisterChannelHook(&EXTENTID_WineTest
, &TestChannelHook
);
2942 ok_ole_success(hr
, CoRegisterChannelHook
);
2944 hr
= CoRegisterMessageFilter(&MessageFilter
, NULL
);
2945 ok_ole_success(hr
, CoRegisterMessageFilter
);
2949 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
2950 ok_ole_success(hr
, CreateStreamOnHGlobal
);
2951 tid
= start_host_object2(pStream
, &IID_IClassFactory
, (IUnknown
*)&Test_ClassFactory
, MSHLFLAGS_NORMAL
, &MessageFilter
, &thread
);
2953 ok_more_than_one_lock();
2955 IStream_Seek(pStream
, ullZero
, STREAM_SEEK_SET
, NULL
);
2956 hr
= CoUnmarshalInterface(pStream
, &IID_IClassFactory
, (void **)&cf
);
2957 ok_ole_success(hr
, CoUnmarshalInterface
);
2958 IStream_Release(pStream
);
2960 ok_more_than_one_lock();
2962 hr
= IClassFactory_CreateInstance(cf
, NULL
, &IID_IUnknown
, (LPVOID
*)&proxy
);
2963 ok_ole_success(hr
, IClassFactory_CreateInstance
);
2964 IUnknown_Release(proxy
);
2966 IClassFactory_Release(cf
);
2970 end_host_object(tid
, thread
);
2972 hr
= CoRegisterMessageFilter(NULL
, NULL
);
2973 ok_ole_success(hr
, CoRegisterMessageFilter
);
2978 HMODULE hOle32
= GetModuleHandle("ole32");
2982 if (!(pCoInitializeEx
= (void*)GetProcAddress(hOle32
, "CoInitializeEx"))) goto no_test
;
2984 argc
= winetest_get_mainargs( &argv
);
2985 if (argc
> 2 && (!strcmp(argv
[2], "-Embedding")))
2987 pCoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
2988 test_register_local_server();
2994 register_test_window();
2996 test_cocreateinstance_proxy();
2998 pCoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
3000 /* FIXME: test CoCreateInstanceEx */
3002 /* lifecycle management and marshaling tests */
3003 test_no_marshaler();
3004 test_normal_marshal_and_release();
3005 test_normal_marshal_and_unmarshal();
3006 test_marshal_and_unmarshal_invalid();
3007 test_same_apartment_unmarshal_failure();
3008 test_interthread_marshal_and_unmarshal();
3009 test_proxy_marshal_and_unmarshal();
3010 test_proxy_marshal_and_unmarshal2();
3011 test_proxy_marshal_and_unmarshal_weak();
3012 test_proxy_marshal_and_unmarshal_strong();
3013 test_marshal_stub_apartment_shutdown();
3014 test_marshal_proxy_apartment_shutdown();
3015 test_marshal_proxy_mta_apartment_shutdown();
3016 test_no_couninitialize_server();
3017 test_no_couninitialize_client();
3018 test_tableweak_marshal_and_unmarshal_twice();
3019 test_tableweak_marshal_releasedata1();
3020 test_tableweak_marshal_releasedata2();
3021 test_tableweak_and_normal_marshal_and_unmarshal();
3022 test_tablestrong_marshal_and_unmarshal_twice();
3023 test_lock_object_external();
3024 test_disconnect_stub();
3025 test_normal_marshal_and_unmarshal_twice();
3026 test_hresult_marshaling();
3027 test_proxy_used_in_wrong_thread();
3028 test_message_filter();
3029 test_bad_marshal_stream();
3030 test_proxy_interfaces();
3031 test_stubbuffer(&IID_IClassFactory
);
3032 test_proxybuffer(&IID_IClassFactory
);
3033 test_message_reentrancy();
3034 test_call_from_message();
3035 test_WM_QUIT_handling();
3036 test_freethreadedmarshaler();
3037 test_inproc_handler();
3038 test_handler_marshaling();
3039 test_client_security();
3041 test_local_server();
3043 test_globalinterfacetable();
3045 /* must be last test as channel hooks can't be unregistered */
3046 test_channel_hook();
3052 trace("You need DCOM95 installed to run this test\n");