ole32: Add a NULL pointer check in CoDisconnectObject.
[wine/multimedia.git] / dlls / ole32 / tests / marshal.c
blob91de43b2672797839135952282730b0c6fc1d436
1 /*
2 * Marshaling Tests
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
21 #define _WIN32_DCOM
22 #define COBJMACROS
23 #define CONST_VTABLE
25 #include <stdarg.h>
26 #include <stdio.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "objbase.h"
31 #include "olectl.h"
32 #include "shlguid.h"
33 #include "shobjidl.h"
34 #include "initguid.h"
36 #include "wine/test.h"
38 DEFINE_GUID(CLSID_StdGlobalInterfaceTable,0x00000323,0x0000,0x0000,0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46);
39 DEFINE_GUID(CLSID_ManualResetEvent, 0x0000032c,0x0000,0x0000,0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46);
41 /* functions that are not present on all versions of Windows */
42 static HRESULT (WINAPI * pCoInitializeEx)(LPVOID lpReserved, DWORD dwCoInit);
44 /* helper macros to make tests a bit leaner */
45 #define ok_more_than_one_lock() ok(cLocks > 0, "Number of locks should be > 0, but actually is %d\n", cLocks)
46 #define ok_no_locks() ok(cLocks == 0, "Number of locks should be 0, but actually is %d\n", cLocks)
47 #define ok_ole_success(hr, func) ok(hr == S_OK, #func " failed with error 0x%08x\n", hr)
49 static const IID IID_IWineTest =
51 0x5201163f,
52 0x8164,
53 0x4fd0,
54 {0xa1, 0xa2, 0x5d, 0x5a, 0x36, 0x54, 0xd3, 0xbd}
55 }; /* 5201163f-8164-4fd0-a1a2-5d5a3654d3bd */
57 static const IID IID_IRemUnknown =
59 0x00000131,
60 0x0000,
61 0x0000,
62 {0xc0,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46}
65 #define EXTENTID_WineTest IID_IWineTest
66 #define CLSID_WineTest IID_IWineTest
68 static const CLSID CLSID_WineOOPTest =
70 0x5201163f,
71 0x8164,
72 0x4fd0,
73 {0xa1, 0xa2, 0x5d, 0x5a, 0x36, 0x54, 0xd3, 0xbd}
74 }; /* 5201163f-8164-4fd0-a1a2-5d5a3654d3bd */
76 static void test_cocreateinstance_proxy(void)
78 IUnknown *pProxy;
79 IMultiQI *pMQI;
80 HRESULT hr;
82 pCoInitializeEx(NULL, COINIT_MULTITHREADED);
84 hr = CoCreateInstance(&CLSID_ShellDesktop, NULL, CLSCTX_INPROC, &IID_IUnknown, (void **)&pProxy);
85 ok_ole_success(hr, CoCreateInstance);
86 hr = IUnknown_QueryInterface(pProxy, &IID_IMultiQI, (void **)&pMQI);
87 ok(hr == S_OK, "created object is not a proxy, so was created in the wrong apartment\n");
88 if (hr == S_OK)
89 IMultiQI_Release(pMQI);
90 IUnknown_Release(pProxy);
92 CoUninitialize();
95 static const LARGE_INTEGER ullZero;
96 static LONG cLocks;
98 static void LockModule(void)
100 InterlockedIncrement(&cLocks);
103 static void UnlockModule(void)
105 InterlockedDecrement(&cLocks);
109 static HRESULT WINAPI Test_IUnknown_QueryInterface(
110 LPUNKNOWN iface,
111 REFIID riid,
112 LPVOID *ppvObj)
114 if (ppvObj == NULL) return E_POINTER;
116 if (IsEqualGUID(riid, &IID_IUnknown))
118 *ppvObj = iface;
119 IUnknown_AddRef(iface);
120 return S_OK;
123 *ppvObj = NULL;
124 return E_NOINTERFACE;
127 static ULONG WINAPI Test_IUnknown_AddRef(LPUNKNOWN iface)
129 LockModule();
130 return 2; /* non-heap-based object */
133 static ULONG WINAPI Test_IUnknown_Release(LPUNKNOWN iface)
135 UnlockModule();
136 return 1; /* non-heap-based object */
139 static const IUnknownVtbl TestUnknown_Vtbl =
141 Test_IUnknown_QueryInterface,
142 Test_IUnknown_AddRef,
143 Test_IUnknown_Release,
146 static IUnknown Test_Unknown = { &TestUnknown_Vtbl };
149 static HRESULT WINAPI Test_IClassFactory_QueryInterface(
150 LPCLASSFACTORY iface,
151 REFIID riid,
152 LPVOID *ppvObj)
154 if (ppvObj == NULL) return E_POINTER;
156 if (IsEqualGUID(riid, &IID_IUnknown) ||
157 IsEqualGUID(riid, &IID_IClassFactory) ||
158 /* the only other interface Wine is currently able to marshal (for testing two proxies) */
159 IsEqualGUID(riid, &IID_IRemUnknown))
161 *ppvObj = iface;
162 IClassFactory_AddRef(iface);
163 return S_OK;
166 *ppvObj = NULL;
167 return E_NOINTERFACE;
170 static ULONG WINAPI Test_IClassFactory_AddRef(LPCLASSFACTORY iface)
172 LockModule();
173 return 2; /* non-heap-based object */
176 static ULONG WINAPI Test_IClassFactory_Release(LPCLASSFACTORY iface)
178 UnlockModule();
179 return 1; /* non-heap-based object */
182 static HRESULT WINAPI Test_IClassFactory_CreateInstance(
183 LPCLASSFACTORY iface,
184 LPUNKNOWN pUnkOuter,
185 REFIID riid,
186 LPVOID *ppvObj)
188 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
189 return IUnknown_QueryInterface((IUnknown*)&Test_Unknown, riid, ppvObj);
192 static HRESULT WINAPI Test_IClassFactory_LockServer(
193 LPCLASSFACTORY iface,
194 BOOL fLock)
196 return S_OK;
199 static const IClassFactoryVtbl TestClassFactory_Vtbl =
201 Test_IClassFactory_QueryInterface,
202 Test_IClassFactory_AddRef,
203 Test_IClassFactory_Release,
204 Test_IClassFactory_CreateInstance,
205 Test_IClassFactory_LockServer
208 static IClassFactory Test_ClassFactory = { &TestClassFactory_Vtbl };
210 #define RELEASEMARSHALDATA WM_USER
212 struct host_object_data
214 IStream *stream;
215 IID iid;
216 IUnknown *object;
217 MSHLFLAGS marshal_flags;
218 HANDLE marshal_event;
219 IMessageFilter *filter;
222 static DWORD CALLBACK host_object_proc(LPVOID p)
224 struct host_object_data *data = p;
225 HRESULT hr;
226 MSG msg;
228 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
230 if (data->filter)
232 IMessageFilter * prev_filter = NULL;
233 hr = CoRegisterMessageFilter(data->filter, &prev_filter);
234 if (prev_filter) IMessageFilter_Release(prev_filter);
235 ok_ole_success(hr, CoRegisterMessageFilter);
238 hr = CoMarshalInterface(data->stream, &data->iid, data->object, MSHCTX_INPROC, NULL, data->marshal_flags);
239 ok_ole_success(hr, CoMarshalInterface);
241 /* force the message queue to be created before signaling parent thread */
242 PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
244 SetEvent(data->marshal_event);
246 while (GetMessage(&msg, NULL, 0, 0))
248 if (msg.hwnd == NULL && msg.message == RELEASEMARSHALDATA)
250 CoReleaseMarshalData(data->stream);
251 SetEvent((HANDLE)msg.lParam);
253 else
254 DispatchMessage(&msg);
257 HeapFree(GetProcessHeap(), 0, data);
259 CoUninitialize();
261 return hr;
264 static DWORD start_host_object2(IStream *stream, REFIID riid, IUnknown *object, MSHLFLAGS marshal_flags, IMessageFilter *filter, HANDLE *thread)
266 DWORD tid = 0;
267 HANDLE marshal_event = CreateEvent(NULL, FALSE, FALSE, NULL);
268 struct host_object_data *data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data));
270 data->stream = stream;
271 data->iid = *riid;
272 data->object = object;
273 data->marshal_flags = marshal_flags;
274 data->marshal_event = marshal_event;
275 data->filter = filter;
277 *thread = CreateThread(NULL, 0, host_object_proc, data, 0, &tid);
279 /* wait for marshaling to complete before returning */
280 ok( !WaitForSingleObject(marshal_event, 10000), "wait timed out\n" );
281 CloseHandle(marshal_event);
283 return tid;
286 static DWORD start_host_object(IStream *stream, REFIID riid, IUnknown *object, MSHLFLAGS marshal_flags, HANDLE *thread)
288 return start_host_object2(stream, riid, object, marshal_flags, NULL, thread);
291 /* asks thread to release the marshal data because it has to be done by the
292 * same thread that marshaled the interface in the first place. */
293 static void release_host_object(DWORD tid)
295 HANDLE event = CreateEvent(NULL, FALSE, FALSE, NULL);
296 PostThreadMessage(tid, RELEASEMARSHALDATA, 0, (LPARAM)event);
297 ok( !WaitForSingleObject(event, 10000), "wait timed out\n" );
298 CloseHandle(event);
301 static void end_host_object(DWORD tid, HANDLE thread)
303 BOOL ret = PostThreadMessage(tid, WM_QUIT, 0, 0);
304 ok(ret, "PostThreadMessage failed with error %d\n", GetLastError());
305 /* be careful of races - don't return until hosting thread has terminated */
306 ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
307 CloseHandle(thread);
310 /* tests failure case of interface not having a marshaler specified in the
311 * registry */
312 static void test_no_marshaler(void)
314 IStream *pStream;
315 HRESULT hr;
317 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
318 ok_ole_success(hr, CreateStreamOnHGlobal);
319 hr = CoMarshalInterface(pStream, &IID_IWineTest, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
320 ok(hr == E_NOINTERFACE, "CoMarshalInterface should have returned E_NOINTERFACE instead of 0x%08x\n", hr);
322 IStream_Release(pStream);
325 /* tests normal marshal and then release without unmarshaling */
326 static void test_normal_marshal_and_release(void)
328 HRESULT hr;
329 IStream *pStream = NULL;
331 cLocks = 0;
333 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
334 ok_ole_success(hr, CreateStreamOnHGlobal);
335 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
336 ok_ole_success(hr, CoMarshalInterface);
338 ok_more_than_one_lock();
340 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
341 hr = CoReleaseMarshalData(pStream);
342 ok_ole_success(hr, CoReleaseMarshalData);
343 IStream_Release(pStream);
345 ok_no_locks();
348 /* tests success case of a same-thread marshal and unmarshal */
349 static void test_normal_marshal_and_unmarshal(void)
351 HRESULT hr;
352 IStream *pStream = NULL;
353 IUnknown *pProxy = NULL;
355 cLocks = 0;
357 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
358 ok_ole_success(hr, CreateStreamOnHGlobal);
359 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
360 ok_ole_success(hr, CoMarshalInterface);
362 ok_more_than_one_lock();
364 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
365 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
366 ok_ole_success(hr, CoUnmarshalInterface);
367 IStream_Release(pStream);
369 ok_more_than_one_lock();
371 IUnknown_Release(pProxy);
373 ok_no_locks();
376 /* tests failure case of unmarshaling a freed object */
377 static void test_marshal_and_unmarshal_invalid(void)
379 HRESULT hr;
380 IStream *pStream = NULL;
381 IClassFactory *pProxy = NULL;
382 DWORD tid;
383 void * dummy;
384 HANDLE thread;
386 cLocks = 0;
388 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
389 ok_ole_success(hr, CreateStreamOnHGlobal);
390 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
392 ok_more_than_one_lock();
394 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
395 hr = CoReleaseMarshalData(pStream);
396 ok_ole_success(hr, CoReleaseMarshalData);
398 ok_no_locks();
400 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
401 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
402 todo_wine { ok_ole_success(hr, CoUnmarshalInterface); }
404 ok_no_locks();
406 if (pProxy)
408 hr = IClassFactory_CreateInstance(pProxy, NULL, &IID_IUnknown, &dummy);
409 ok(hr == RPC_E_DISCONNECTED, "Remote call should have returned RPC_E_DISCONNECTED, instead of 0x%08x\n", hr);
411 IClassFactory_Release(pProxy);
414 IStream_Release(pStream);
416 end_host_object(tid, thread);
419 static void test_same_apartment_unmarshal_failure(void)
421 HRESULT hr;
422 IStream *pStream;
423 IUnknown *pProxy;
424 static const LARGE_INTEGER llZero;
426 cLocks = 0;
428 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
429 ok_ole_success(hr, CreateStreamOnHGlobal);
431 hr = CoMarshalInterface(pStream, &IID_IUnknown, (IUnknown *)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
432 ok_ole_success(hr, CoMarshalInterface);
434 ok_more_than_one_lock();
436 hr = IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
437 ok_ole_success(hr, IStream_Seek);
439 hr = CoUnmarshalInterface(pStream, &IID_IParseDisplayName, (void **)&pProxy);
440 ok(hr == E_NOINTERFACE, "CoUnmarshalInterface should have returned E_NOINTERFACE instead of 0x%08x\n", hr);
442 ok_no_locks();
444 IStream_Release(pStream);
447 /* tests success case of an interthread marshal */
448 static void test_interthread_marshal_and_unmarshal(void)
450 HRESULT hr;
451 IStream *pStream = NULL;
452 IUnknown *pProxy = NULL;
453 DWORD tid;
454 HANDLE thread;
456 cLocks = 0;
458 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
459 ok_ole_success(hr, CreateStreamOnHGlobal);
460 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
462 ok_more_than_one_lock();
464 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
465 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
466 ok_ole_success(hr, CoUnmarshalInterface);
467 IStream_Release(pStream);
469 ok_more_than_one_lock();
471 IUnknown_Release(pProxy);
473 ok_no_locks();
475 end_host_object(tid, thread);
478 /* the number of external references that Wine's proxy manager normally gives
479 * out, so we can test the border case of running out of references */
480 #define NORMALEXTREFS 5
482 /* tests success case of an interthread marshal and then marshaling the proxy */
483 static void test_proxy_marshal_and_unmarshal(void)
485 HRESULT hr;
486 IStream *pStream = NULL;
487 IUnknown *pProxy = NULL;
488 IUnknown *pProxy2 = NULL;
489 DWORD tid;
490 HANDLE thread;
491 int i;
493 cLocks = 0;
495 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
496 ok_ole_success(hr, CreateStreamOnHGlobal);
497 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
499 ok_more_than_one_lock();
501 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
502 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
503 ok_ole_success(hr, CoUnmarshalInterface);
505 ok_more_than_one_lock();
507 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
508 /* marshal the proxy */
509 hr = CoMarshalInterface(pStream, &IID_IClassFactory, pProxy, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
510 ok_ole_success(hr, CoMarshalInterface);
512 ok_more_than_one_lock();
514 /* marshal 5 more times to exhaust the normal external references of 5 */
515 for (i = 0; i < NORMALEXTREFS; i++)
517 hr = CoMarshalInterface(pStream, &IID_IClassFactory, pProxy, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
518 ok_ole_success(hr, CoMarshalInterface);
521 ok_more_than_one_lock();
523 /* release the original proxy to test that we successfully keep the
524 * original object alive */
525 IUnknown_Release(pProxy);
527 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
528 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
529 ok_ole_success(hr, CoUnmarshalInterface);
531 ok_more_than_one_lock();
533 IUnknown_Release(pProxy2);
535 /* unmarshal all of the proxies to check that the object stub still exists */
536 for (i = 0; i < NORMALEXTREFS; i++)
538 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
539 ok_ole_success(hr, CoUnmarshalInterface);
541 IUnknown_Release(pProxy2);
544 ok_no_locks();
546 IStream_Release(pStream);
548 end_host_object(tid, thread);
551 /* tests success case of an interthread marshal and then marshaling the proxy
552 * using an iid that hasn't previously been unmarshaled */
553 static void test_proxy_marshal_and_unmarshal2(void)
555 HRESULT hr;
556 IStream *pStream = NULL;
557 IUnknown *pProxy = NULL;
558 IUnknown *pProxy2 = NULL;
559 DWORD tid;
560 HANDLE thread;
562 cLocks = 0;
564 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
565 ok_ole_success(hr, CreateStreamOnHGlobal);
566 tid = start_host_object(pStream, &IID_IUnknown, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
568 ok_more_than_one_lock();
570 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
571 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)&pProxy);
572 ok_ole_success(hr, CoUnmarshalInterface);
574 ok_more_than_one_lock();
576 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
577 /* marshal the proxy */
578 hr = CoMarshalInterface(pStream, &IID_IClassFactory, pProxy, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
579 ok_ole_success(hr, CoMarshalInterface);
581 ok_more_than_one_lock();
583 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
584 /* unmarshal the second proxy to the object */
585 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
586 ok_ole_success(hr, CoUnmarshalInterface);
587 IStream_Release(pStream);
589 /* now the proxies should be as follows:
590 * pProxy -> &Test_ClassFactory
591 * pProxy2 -> &Test_ClassFactory
592 * they should NOT be as follows:
593 * pProxy -> &Test_ClassFactory
594 * pProxy2 -> pProxy
595 * the above can only really be tested by looking in +ole traces
598 ok_more_than_one_lock();
600 IUnknown_Release(pProxy);
602 ok_more_than_one_lock();
604 IUnknown_Release(pProxy2);
606 ok_no_locks();
608 end_host_object(tid, thread);
611 /* tests success case of an interthread marshal and then table-weak-marshaling the proxy */
612 static void test_proxy_marshal_and_unmarshal_weak(void)
614 HRESULT hr;
615 IStream *pStream = NULL;
616 IUnknown *pProxy = NULL;
617 IUnknown *pProxy2 = NULL;
618 DWORD tid;
619 HANDLE thread;
621 cLocks = 0;
623 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
624 ok_ole_success(hr, CreateStreamOnHGlobal);
625 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
627 ok_more_than_one_lock();
629 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
630 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
631 ok_ole_success(hr, CoUnmarshalInterface);
633 ok_more_than_one_lock();
635 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
636 /* marshal the proxy */
637 hr = CoMarshalInterface(pStream, &IID_IClassFactory, pProxy, MSHCTX_INPROC, NULL, MSHLFLAGS_TABLEWEAK);
638 ok_ole_success(hr, CoMarshalInterface);
640 ok_more_than_one_lock();
642 /* release the original proxy to test that we successfully keep the
643 * original object alive */
644 IUnknown_Release(pProxy);
646 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
647 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
648 todo_wine
649 ok(hr == CO_E_OBJNOTREG, "CoUnmarshalInterface should return CO_E_OBJNOTREG instead of 0x%08x\n", hr);
651 ok_no_locks();
653 IStream_Release(pStream);
655 end_host_object(tid, thread);
658 /* tests success case of an interthread marshal and then table-strong-marshaling the proxy */
659 static void test_proxy_marshal_and_unmarshal_strong(void)
661 HRESULT hr;
662 IStream *pStream = NULL;
663 IUnknown *pProxy = NULL;
664 IUnknown *pProxy2 = NULL;
665 DWORD tid;
666 HANDLE thread;
668 cLocks = 0;
670 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
671 ok_ole_success(hr, CreateStreamOnHGlobal);
672 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
674 ok_more_than_one_lock();
676 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
677 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
678 ok_ole_success(hr, CoUnmarshalInterface);
680 ok_more_than_one_lock();
682 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
683 /* marshal the proxy */
684 hr = CoMarshalInterface(pStream, &IID_IClassFactory, pProxy, MSHCTX_INPROC, NULL, MSHLFLAGS_TABLESTRONG);
685 ok(hr == S_OK /* WinNT */ || hr == E_INVALIDARG /* Win9x */,
686 "CoMarshalInterface should have return S_OK or E_INVALIDARG instead of 0x%08x\n", hr);
687 if (FAILED(hr))
689 IUnknown_Release(pProxy);
690 goto end;
693 ok_more_than_one_lock();
695 /* release the original proxy to test that we successfully keep the
696 * original object alive */
697 IUnknown_Release(pProxy);
699 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
700 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
701 ok_ole_success(hr, CoUnmarshalInterface);
703 ok_more_than_one_lock();
705 IUnknown_Release(pProxy2);
707 ok_more_than_one_lock();
709 end:
710 IStream_Release(pStream);
712 end_host_object(tid, thread);
714 ok_no_locks();
717 /* tests that stubs are released when the containing apartment is destroyed */
718 static void test_marshal_stub_apartment_shutdown(void)
720 HRESULT hr;
721 IStream *pStream = NULL;
722 IUnknown *pProxy = NULL;
723 DWORD tid;
724 HANDLE thread;
726 cLocks = 0;
728 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
729 ok_ole_success(hr, CreateStreamOnHGlobal);
730 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
732 ok_more_than_one_lock();
734 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
735 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
736 ok_ole_success(hr, CoUnmarshalInterface);
737 IStream_Release(pStream);
739 ok_more_than_one_lock();
741 end_host_object(tid, thread);
743 ok_no_locks();
745 IUnknown_Release(pProxy);
747 ok_no_locks();
750 /* tests that proxies are released when the containing apartment is destroyed */
751 static void test_marshal_proxy_apartment_shutdown(void)
753 HRESULT hr;
754 IStream *pStream = NULL;
755 IUnknown *pProxy = NULL;
756 DWORD tid;
757 HANDLE thread;
759 cLocks = 0;
761 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
762 ok_ole_success(hr, CreateStreamOnHGlobal);
763 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
765 ok_more_than_one_lock();
767 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
768 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
769 ok_ole_success(hr, CoUnmarshalInterface);
770 IStream_Release(pStream);
772 ok_more_than_one_lock();
774 CoUninitialize();
776 ok_no_locks();
778 IUnknown_Release(pProxy);
780 ok_no_locks();
782 end_host_object(tid, thread);
784 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
787 /* tests that proxies are released when the containing mta apartment is destroyed */
788 static void test_marshal_proxy_mta_apartment_shutdown(void)
790 HRESULT hr;
791 IStream *pStream = NULL;
792 IUnknown *pProxy = NULL;
793 DWORD tid;
794 HANDLE thread;
796 CoUninitialize();
797 pCoInitializeEx(NULL, COINIT_MULTITHREADED);
799 cLocks = 0;
801 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
802 ok_ole_success(hr, CreateStreamOnHGlobal);
803 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
805 ok_more_than_one_lock();
807 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
808 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
809 ok_ole_success(hr, CoUnmarshalInterface);
810 IStream_Release(pStream);
812 ok_more_than_one_lock();
814 CoUninitialize();
816 ok_no_locks();
818 IUnknown_Release(pProxy);
820 ok_no_locks();
822 end_host_object(tid, thread);
824 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
827 struct ncu_params
829 LPSTREAM stream;
830 HANDLE marshal_event;
831 HANDLE unmarshal_event;
834 /* helper for test_no_couninitialize_server */
835 static DWORD CALLBACK no_couninitialize_server_proc(LPVOID p)
837 struct ncu_params *ncu_params = p;
838 HRESULT hr;
840 pCoInitializeEx(NULL, COINIT_MULTITHREADED);
842 hr = CoMarshalInterface(ncu_params->stream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
843 ok_ole_success(hr, CoMarshalInterface);
845 SetEvent(ncu_params->marshal_event);
847 ok( !WaitForSingleObject(ncu_params->unmarshal_event, 10000), "wait timed out\n" );
849 /* die without calling CoUninitialize */
851 return 0;
854 /* tests apartment that an apartment with a stub is released without deadlock
855 * if the owning thread exits */
856 static void test_no_couninitialize_server(void)
858 HRESULT hr;
859 IStream *pStream = NULL;
860 IUnknown *pProxy = NULL;
861 DWORD tid;
862 HANDLE thread;
863 struct ncu_params ncu_params;
865 cLocks = 0;
867 ncu_params.marshal_event = CreateEvent(NULL, TRUE, FALSE, NULL);
868 ncu_params.unmarshal_event = CreateEvent(NULL, TRUE, FALSE, NULL);
870 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
871 ok_ole_success(hr, CreateStreamOnHGlobal);
872 ncu_params.stream = pStream;
874 thread = CreateThread(NULL, 0, no_couninitialize_server_proc, &ncu_params, 0, &tid);
876 ok( !WaitForSingleObject(ncu_params.marshal_event, 10000), "wait timed out\n" );
877 ok_more_than_one_lock();
879 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
880 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
881 ok_ole_success(hr, CoUnmarshalInterface);
882 IStream_Release(pStream);
884 ok_more_than_one_lock();
886 SetEvent(ncu_params.unmarshal_event);
887 ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
889 ok_no_locks();
891 CloseHandle(thread);
892 CloseHandle(ncu_params.marshal_event);
893 CloseHandle(ncu_params.unmarshal_event);
895 IUnknown_Release(pProxy);
897 ok_no_locks();
900 /* STA -> STA call during DLL_THREAD_DETACH */
901 static DWORD CALLBACK no_couninitialize_client_proc(LPVOID p)
903 struct ncu_params *ncu_params = p;
904 HRESULT hr;
905 IUnknown *pProxy = NULL;
907 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
909 hr = CoUnmarshalInterface(ncu_params->stream, &IID_IClassFactory, (void **)&pProxy);
910 ok_ole_success(hr, CoUnmarshalInterface);
911 IStream_Release(ncu_params->stream);
913 ok_more_than_one_lock();
915 /* die without calling CoUninitialize */
917 return 0;
920 /* tests STA -> STA call during DLL_THREAD_DETACH doesn't deadlock */
921 static void test_no_couninitialize_client(void)
923 HRESULT hr;
924 IStream *pStream = NULL;
925 DWORD tid;
926 DWORD host_tid;
927 HANDLE thread;
928 HANDLE host_thread;
929 struct ncu_params ncu_params;
931 cLocks = 0;
933 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
934 ok_ole_success(hr, CreateStreamOnHGlobal);
935 ncu_params.stream = pStream;
937 /* NOTE: assumes start_host_object uses an STA to host the object, as MTAs
938 * always deadlock when called from within DllMain */
939 host_tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown *)&Test_ClassFactory, MSHLFLAGS_NORMAL, &host_thread);
940 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
942 ok_more_than_one_lock();
944 thread = CreateThread(NULL, 0, no_couninitialize_client_proc, &ncu_params, 0, &tid);
946 ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
947 CloseHandle(thread);
949 ok_no_locks();
951 end_host_object(host_tid, host_thread);
954 /* tests success case of a same-thread table-weak marshal, unmarshal, unmarshal */
955 static void test_tableweak_marshal_and_unmarshal_twice(void)
957 HRESULT hr;
958 IStream *pStream = NULL;
959 IUnknown *pProxy1 = NULL;
960 IUnknown *pProxy2 = NULL;
961 DWORD tid;
962 HANDLE thread;
964 cLocks = 0;
966 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
967 ok_ole_success(hr, CreateStreamOnHGlobal);
968 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_TABLEWEAK, &thread);
970 ok_more_than_one_lock();
972 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
973 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy1);
974 ok_ole_success(hr, CoUnmarshalInterface);
976 ok_more_than_one_lock();
978 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
979 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
980 IStream_Release(pStream);
981 ok_ole_success(hr, CoUnmarshalInterface);
983 ok_more_than_one_lock();
985 IUnknown_Release(pProxy1);
986 IUnknown_Release(pProxy2);
988 /* this line is shows the difference between weak and strong table marshaling:
989 * weak has cLocks == 0
990 * strong has cLocks > 0 */
991 ok_no_locks();
993 end_host_object(tid, thread);
996 /* tests releasing after unmarshaling one object */
997 static void test_tableweak_marshal_releasedata1(void)
999 HRESULT hr;
1000 IStream *pStream = NULL;
1001 IUnknown *pProxy1 = NULL;
1002 IUnknown *pProxy2 = NULL;
1003 DWORD tid;
1004 HANDLE thread;
1006 cLocks = 0;
1008 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1009 ok_ole_success(hr, CreateStreamOnHGlobal);
1010 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_TABLEWEAK, &thread);
1012 ok_more_than_one_lock();
1014 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1015 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy1);
1016 ok_ole_success(hr, CoUnmarshalInterface);
1018 ok_more_than_one_lock();
1020 /* release the remaining reference on the object by calling
1021 * CoReleaseMarshalData in the hosting thread */
1022 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1023 release_host_object(tid);
1025 ok_more_than_one_lock();
1027 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1028 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
1029 ok_ole_success(hr, CoUnmarshalInterface);
1030 IStream_Release(pStream);
1032 ok_more_than_one_lock();
1034 IUnknown_Release(pProxy1);
1035 if (pProxy2)
1036 IUnknown_Release(pProxy2);
1038 /* this line is shows the difference between weak and strong table marshaling:
1039 * weak has cLocks == 0
1040 * strong has cLocks > 0 */
1041 ok_no_locks();
1043 end_host_object(tid, thread);
1046 /* tests releasing after unmarshaling one object */
1047 static void test_tableweak_marshal_releasedata2(void)
1049 HRESULT hr;
1050 IStream *pStream = NULL;
1051 IUnknown *pProxy = NULL;
1052 DWORD tid;
1053 HANDLE thread;
1055 cLocks = 0;
1057 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1058 ok_ole_success(hr, CreateStreamOnHGlobal);
1059 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_TABLEWEAK, &thread);
1061 ok_more_than_one_lock();
1063 /* release the remaining reference on the object by calling
1064 * CoReleaseMarshalData in the hosting thread */
1065 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1066 release_host_object(tid);
1068 ok_no_locks();
1070 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1071 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
1072 todo_wine
1074 ok(hr == CO_E_OBJNOTREG,
1075 "CoUnmarshalInterface should have failed with CO_E_OBJNOTREG, but returned 0x%08x instead\n",
1076 hr);
1078 IStream_Release(pStream);
1080 ok_no_locks();
1082 end_host_object(tid, thread);
1085 struct weak_and_normal_marshal_data
1087 IStream *pStreamWeak;
1088 IStream *pStreamNormal;
1089 HANDLE hReadyEvent;
1090 HANDLE hQuitEvent;
1093 static DWORD CALLBACK weak_and_normal_marshal_thread_proc(void *p)
1095 HRESULT hr;
1096 struct weak_and_normal_marshal_data *data = p;
1097 HANDLE hQuitEvent = data->hQuitEvent;
1098 MSG msg;
1100 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1102 hr = CoMarshalInterface(data->pStreamWeak, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_TABLEWEAK);
1103 ok_ole_success(hr, "CoMarshalInterface");
1105 hr = CoMarshalInterface(data->pStreamNormal, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1106 ok_ole_success(hr, "CoMarshalInterface");
1108 /* force the message queue to be created before signaling parent thread */
1109 PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
1111 SetEvent(data->hReadyEvent);
1113 while (WAIT_OBJECT_0 + 1 == MsgWaitForMultipleObjects(1, &hQuitEvent, FALSE, 10000, QS_ALLINPUT))
1115 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
1116 DispatchMessage(&msg);
1118 CloseHandle(hQuitEvent);
1120 CoUninitialize();
1122 return 0;
1125 /* tests interaction between table-weak and normal marshalling of an object */
1126 static void test_tableweak_and_normal_marshal_and_unmarshal(void)
1128 HRESULT hr;
1129 IUnknown *pProxyWeak = NULL;
1130 IUnknown *pProxyNormal = NULL;
1131 DWORD tid;
1132 HANDLE thread;
1133 struct weak_and_normal_marshal_data data;
1135 cLocks = 0;
1137 data.hReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
1138 data.hQuitEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
1139 hr = CreateStreamOnHGlobal(NULL, TRUE, &data.pStreamWeak);
1140 ok_ole_success(hr, CreateStreamOnHGlobal);
1141 hr = CreateStreamOnHGlobal(NULL, TRUE, &data.pStreamNormal);
1142 ok_ole_success(hr, CreateStreamOnHGlobal);
1144 thread = CreateThread(NULL, 0, weak_and_normal_marshal_thread_proc, &data, 0, &tid);
1145 ok( !WaitForSingleObject(data.hReadyEvent, 10000), "wait timed out\n" );
1146 CloseHandle(data.hReadyEvent);
1148 ok_more_than_one_lock();
1150 IStream_Seek(data.pStreamWeak, ullZero, STREAM_SEEK_SET, NULL);
1151 hr = CoUnmarshalInterface(data.pStreamWeak, &IID_IClassFactory, (void **)&pProxyWeak);
1152 ok_ole_success(hr, CoUnmarshalInterface);
1154 ok_more_than_one_lock();
1156 IStream_Seek(data.pStreamNormal, ullZero, STREAM_SEEK_SET, NULL);
1157 hr = CoUnmarshalInterface(data.pStreamNormal, &IID_IClassFactory, (void **)&pProxyNormal);
1158 ok_ole_success(hr, CoUnmarshalInterface);
1160 ok_more_than_one_lock();
1162 IUnknown_Release(pProxyNormal);
1164 ok_more_than_one_lock();
1166 IUnknown_Release(pProxyWeak);
1168 ok_no_locks();
1170 IStream_Release(data.pStreamWeak);
1171 IStream_Release(data.pStreamNormal);
1173 SetEvent(data.hQuitEvent);
1174 ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
1175 CloseHandle(thread);
1178 /* tests success case of a same-thread table-strong marshal, unmarshal, unmarshal */
1179 static void test_tablestrong_marshal_and_unmarshal_twice(void)
1181 HRESULT hr;
1182 IStream *pStream = NULL;
1183 IUnknown *pProxy1 = NULL;
1184 IUnknown *pProxy2 = NULL;
1185 DWORD tid;
1186 HANDLE thread;
1188 cLocks = 0;
1190 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1191 ok_ole_success(hr, CreateStreamOnHGlobal);
1192 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_TABLESTRONG, &thread);
1194 ok_more_than_one_lock();
1196 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1197 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy1);
1198 ok_ole_success(hr, CoUnmarshalInterface);
1200 ok_more_than_one_lock();
1202 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1203 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
1204 ok_ole_success(hr, CoUnmarshalInterface);
1206 ok_more_than_one_lock();
1208 if (pProxy1) IUnknown_Release(pProxy1);
1209 if (pProxy2) IUnknown_Release(pProxy2);
1211 /* this line is shows the difference between weak and strong table marshaling:
1212 * weak has cLocks == 0
1213 * strong has cLocks > 0 */
1214 ok_more_than_one_lock();
1216 /* release the remaining reference on the object by calling
1217 * CoReleaseMarshalData in the hosting thread */
1218 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1219 release_host_object(tid);
1220 IStream_Release(pStream);
1222 ok_no_locks();
1224 end_host_object(tid, thread);
1227 /* tests CoLockObjectExternal */
1228 static void test_lock_object_external(void)
1230 HRESULT hr;
1231 IStream *pStream = NULL;
1233 cLocks = 0;
1235 /* test the stub manager creation aspect of CoLockObjectExternal when the
1236 * object hasn't been marshaled yet */
1237 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, TRUE, TRUE);
1239 ok_more_than_one_lock();
1241 CoDisconnectObject((IUnknown*)&Test_ClassFactory, 0);
1243 ok_no_locks();
1245 /* test our empty stub manager being handled correctly in
1246 * CoMarshalInterface */
1247 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, TRUE, TRUE);
1249 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1250 ok_ole_success(hr, CreateStreamOnHGlobal);
1251 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1252 ok_ole_success(hr, CoMarshalInterface);
1254 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, TRUE, TRUE);
1256 ok_more_than_one_lock();
1258 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1259 hr = CoReleaseMarshalData(pStream);
1260 ok_ole_success(hr, CoReleaseMarshalData);
1261 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1263 ok_more_than_one_lock();
1265 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, FALSE, TRUE);
1267 ok_more_than_one_lock();
1269 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, FALSE, TRUE);
1271 ok_no_locks();
1273 /* test CoLockObjectExternal releases reference to object with
1274 * fLastUnlockReleases as TRUE and there are only strong references on
1275 * the object */
1276 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, TRUE, FALSE);
1278 ok_more_than_one_lock();
1280 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, FALSE, FALSE);
1282 ok_no_locks();
1284 /* test CoLockObjectExternal doesn't release the last reference to an
1285 * object with fLastUnlockReleases as TRUE and there is a weak reference
1286 * on the object */
1287 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_TABLEWEAK);
1288 ok_ole_success(hr, CoMarshalInterface);
1290 ok_more_than_one_lock();
1292 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, TRUE, FALSE);
1294 ok_more_than_one_lock();
1296 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, FALSE, FALSE);
1298 ok_more_than_one_lock();
1300 CoDisconnectObject((IUnknown*)&Test_ClassFactory, 0);
1302 ok_no_locks();
1304 IStream_Release(pStream);
1307 /* tests disconnecting stubs */
1308 static void test_disconnect_stub(void)
1310 HRESULT hr;
1311 IStream *pStream = NULL;
1313 cLocks = 0;
1315 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1316 ok_ole_success(hr, CreateStreamOnHGlobal);
1317 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1318 ok_ole_success(hr, CoMarshalInterface);
1320 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, TRUE, TRUE);
1322 ok_more_than_one_lock();
1324 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1325 hr = CoReleaseMarshalData(pStream);
1326 ok_ole_success(hr, CoReleaseMarshalData);
1327 IStream_Release(pStream);
1329 ok_more_than_one_lock();
1331 CoDisconnectObject((IUnknown*)&Test_ClassFactory, 0);
1333 ok_no_locks();
1335 hr = CoDisconnectObject(NULL, 0);
1336 ok( hr == E_INVALIDARG, "wrong status %x\n", hr );
1339 /* tests failure case of a same-thread marshal and unmarshal twice */
1340 static void test_normal_marshal_and_unmarshal_twice(void)
1342 HRESULT hr;
1343 IStream *pStream = NULL;
1344 IUnknown *pProxy1 = NULL;
1345 IUnknown *pProxy2 = NULL;
1347 cLocks = 0;
1349 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1350 ok_ole_success(hr, CreateStreamOnHGlobal);
1351 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1352 ok_ole_success(hr, CoMarshalInterface);
1354 ok_more_than_one_lock();
1356 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1357 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy1);
1358 ok_ole_success(hr, CoUnmarshalInterface);
1360 ok_more_than_one_lock();
1362 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1363 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
1364 ok(hr == CO_E_OBJNOTCONNECTED,
1365 "CoUnmarshalInterface should have failed with error CO_E_OBJNOTCONNECTED for double unmarshal, instead of 0x%08x\n", hr);
1367 IStream_Release(pStream);
1369 ok_more_than_one_lock();
1371 IUnknown_Release(pProxy1);
1373 ok_no_locks();
1376 /* tests success case of marshaling and unmarshaling an HRESULT */
1377 static void test_hresult_marshaling(void)
1379 HRESULT hr;
1380 HRESULT hr_marshaled = 0;
1381 IStream *pStream = NULL;
1382 static const HRESULT E_DEADBEEF = 0xdeadbeef;
1384 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1385 ok_ole_success(hr, CreateStreamOnHGlobal);
1387 hr = CoMarshalHresult(pStream, E_DEADBEEF);
1388 ok_ole_success(hr, CoMarshalHresult);
1390 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1391 hr = IStream_Read(pStream, &hr_marshaled, sizeof(HRESULT), NULL);
1392 ok_ole_success(hr, IStream_Read);
1394 ok(hr_marshaled == E_DEADBEEF, "Didn't marshal HRESULT as expected: got value 0x%08x instead\n", hr_marshaled);
1396 hr_marshaled = 0;
1397 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1398 hr = CoUnmarshalHresult(pStream, &hr_marshaled);
1399 ok_ole_success(hr, CoUnmarshalHresult);
1401 ok(hr_marshaled == E_DEADBEEF, "Didn't marshal HRESULT as expected: got value 0x%08x instead\n", hr_marshaled);
1403 IStream_Release(pStream);
1407 /* helper for test_proxy_used_in_wrong_thread */
1408 static DWORD CALLBACK bad_thread_proc(LPVOID p)
1410 IClassFactory * cf = p;
1411 HRESULT hr;
1412 IUnknown * proxy = NULL;
1414 hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
1415 todo_wine
1416 ok(hr == CO_E_NOTINITIALIZED,
1417 "COM should have failed with CO_E_NOTINITIALIZED on using proxy without apartment, but instead returned 0x%08x\n",
1418 hr);
1420 hr = IClassFactory_QueryInterface(cf, &IID_IMultiQI, (LPVOID *)&proxy);
1421 /* Win9x returns S_OK, whilst NT returns RPC_E_WRONG_THREAD */
1422 trace("call to proxy's QueryInterface for local interface without apartment returned 0x%08x\n", hr);
1423 if (SUCCEEDED(hr))
1424 IUnknown_Release(proxy);
1426 hr = IClassFactory_QueryInterface(cf, &IID_IStream, (LPVOID *)&proxy);
1427 /* Win9x returns E_NOINTERFACE, whilst NT returns RPC_E_WRONG_THREAD */
1428 trace("call to proxy's QueryInterface without apartment returned 0x%08x\n", hr);
1429 if (SUCCEEDED(hr))
1430 IUnknown_Release(proxy);
1432 pCoInitializeEx(NULL, COINIT_MULTITHREADED);
1434 hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
1435 if (proxy) IUnknown_Release(proxy);
1436 ok(hr == RPC_E_WRONG_THREAD,
1437 "COM should have failed with RPC_E_WRONG_THREAD on using proxy from wrong apartment, but instead returned 0x%08x\n",
1438 hr);
1440 hr = IClassFactory_QueryInterface(cf, &IID_IStream, (LPVOID *)&proxy);
1441 /* Win9x returns E_NOINTERFACE, whilst NT returns RPC_E_WRONG_THREAD */
1442 trace("call to proxy's QueryInterface from wrong apartment returned 0x%08x\n", hr);
1444 /* now be really bad and release the proxy from the wrong apartment */
1445 IUnknown_Release(cf);
1447 CoUninitialize();
1449 return 0;
1452 /* tests failure case of a using a proxy in the wrong apartment */
1453 static void test_proxy_used_in_wrong_thread(void)
1455 HRESULT hr;
1456 IStream *pStream = NULL;
1457 IUnknown *pProxy = NULL;
1458 DWORD tid, tid2;
1459 HANDLE thread;
1460 HANDLE host_thread;
1462 cLocks = 0;
1464 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1465 ok_ole_success(hr, CreateStreamOnHGlobal);
1466 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &host_thread);
1468 ok_more_than_one_lock();
1470 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1471 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
1472 ok_ole_success(hr, CoUnmarshalInterface);
1473 IStream_Release(pStream);
1475 ok_more_than_one_lock();
1477 /* do a call that will fail, but result in IRemUnknown being used by the proxy */
1478 IClassFactory_QueryInterface(pProxy, &IID_IStream, (LPVOID *)&pStream);
1480 /* create a thread that we can misbehave in */
1481 thread = CreateThread(NULL, 0, bad_thread_proc, pProxy, 0, &tid2);
1483 ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
1484 CloseHandle(thread);
1486 /* do release statement on Win9x that we should have done above */
1487 if (!GetProcAddress(GetModuleHandle("ole32"), "CoRegisterSurrogateEx"))
1488 IUnknown_Release(pProxy);
1490 ok_no_locks();
1492 end_host_object(tid, host_thread);
1495 static HRESULT WINAPI MessageFilter_QueryInterface(IMessageFilter *iface, REFIID riid, void ** ppvObj)
1497 if (ppvObj == NULL) return E_POINTER;
1499 if (IsEqualGUID(riid, &IID_IUnknown) ||
1500 IsEqualGUID(riid, &IID_IClassFactory))
1502 *ppvObj = iface;
1503 IClassFactory_AddRef(iface);
1504 return S_OK;
1507 return E_NOINTERFACE;
1510 static ULONG WINAPI MessageFilter_AddRef(IMessageFilter *iface)
1512 return 2; /* non-heap object */
1515 static ULONG WINAPI MessageFilter_Release(IMessageFilter *iface)
1517 return 1; /* non-heap object */
1520 static DWORD WINAPI MessageFilter_HandleInComingCall(
1521 IMessageFilter *iface,
1522 DWORD dwCallType,
1523 HTASK threadIDCaller,
1524 DWORD dwTickCount,
1525 LPINTERFACEINFO lpInterfaceInfo)
1527 static int callcount = 0;
1528 DWORD ret;
1529 trace("HandleInComingCall\n");
1530 switch (callcount)
1532 case 0:
1533 ret = SERVERCALL_REJECTED;
1534 break;
1535 case 1:
1536 ret = SERVERCALL_RETRYLATER;
1537 break;
1538 default:
1539 ret = SERVERCALL_ISHANDLED;
1540 break;
1542 callcount++;
1543 return ret;
1546 static DWORD WINAPI MessageFilter_RetryRejectedCall(
1547 IMessageFilter *iface,
1548 HTASK threadIDCallee,
1549 DWORD dwTickCount,
1550 DWORD dwRejectType)
1552 trace("RetryRejectedCall\n");
1553 return 0;
1556 static DWORD WINAPI MessageFilter_MessagePending(
1557 IMessageFilter *iface,
1558 HTASK threadIDCallee,
1559 DWORD dwTickCount,
1560 DWORD dwPendingType)
1562 trace("MessagePending\n");
1563 return PENDINGMSG_WAITNOPROCESS;
1566 static const IMessageFilterVtbl MessageFilter_Vtbl =
1568 MessageFilter_QueryInterface,
1569 MessageFilter_AddRef,
1570 MessageFilter_Release,
1571 MessageFilter_HandleInComingCall,
1572 MessageFilter_RetryRejectedCall,
1573 MessageFilter_MessagePending
1576 static IMessageFilter MessageFilter = { &MessageFilter_Vtbl };
1578 static void test_message_filter(void)
1580 HRESULT hr;
1581 IStream *pStream = NULL;
1582 IClassFactory *cf = NULL;
1583 DWORD tid;
1584 IUnknown *proxy = NULL;
1585 IMessageFilter *prev_filter = NULL;
1586 HANDLE thread;
1588 cLocks = 0;
1590 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1591 ok_ole_success(hr, CreateStreamOnHGlobal);
1592 tid = start_host_object2(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &MessageFilter, &thread);
1594 ok_more_than_one_lock();
1596 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1597 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&cf);
1598 ok_ole_success(hr, CoUnmarshalInterface);
1599 IStream_Release(pStream);
1601 ok_more_than_one_lock();
1603 hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
1604 ok(hr == RPC_E_CALL_REJECTED, "Call should have returned RPC_E_CALL_REJECTED, but return 0x%08x instead\n", hr);
1605 if (proxy) IUnknown_Release(proxy);
1606 proxy = NULL;
1608 hr = CoRegisterMessageFilter(&MessageFilter, &prev_filter);
1609 ok_ole_success(hr, CoRegisterMessageFilter);
1611 hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
1612 ok_ole_success(hr, IClassFactory_CreateInstance);
1614 IUnknown_Release(proxy);
1616 IClassFactory_Release(cf);
1618 ok_no_locks();
1620 end_host_object(tid, thread);
1622 hr = CoRegisterMessageFilter(prev_filter, NULL);
1623 ok_ole_success(hr, CoRegisterMessageFilter);
1626 /* test failure case of trying to unmarshal from bad stream */
1627 static void test_bad_marshal_stream(void)
1629 HRESULT hr;
1630 IStream *pStream = NULL;
1632 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1633 ok_ole_success(hr, CreateStreamOnHGlobal);
1634 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1635 ok_ole_success(hr, CoMarshalInterface);
1637 ok_more_than_one_lock();
1639 /* try to read beyond end of stream */
1640 hr = CoReleaseMarshalData(pStream);
1641 ok(hr == STG_E_READFAULT, "Should have failed with STG_E_READFAULT, but returned 0x%08x instead\n", hr);
1643 /* now release for real */
1644 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1645 hr = CoReleaseMarshalData(pStream);
1646 ok_ole_success(hr, CoReleaseMarshalData);
1648 IStream_Release(pStream);
1651 /* tests that proxies implement certain interfaces */
1652 static void test_proxy_interfaces(void)
1654 HRESULT hr;
1655 IStream *pStream = NULL;
1656 IUnknown *pProxy = NULL;
1657 IUnknown *pOtherUnknown = NULL;
1658 DWORD tid;
1659 HANDLE thread;
1661 cLocks = 0;
1663 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1664 ok_ole_success(hr, CreateStreamOnHGlobal);
1665 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
1667 ok_more_than_one_lock();
1669 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1670 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)&pProxy);
1671 ok_ole_success(hr, CoUnmarshalInterface);
1672 IStream_Release(pStream);
1674 ok_more_than_one_lock();
1676 hr = IUnknown_QueryInterface(pProxy, &IID_IUnknown, (LPVOID*)&pOtherUnknown);
1677 ok_ole_success(hr, IUnknown_QueryInterface IID_IUnknown);
1678 if (hr == S_OK) IUnknown_Release(pOtherUnknown);
1680 hr = IUnknown_QueryInterface(pProxy, &IID_IClientSecurity, (LPVOID*)&pOtherUnknown);
1681 ok_ole_success(hr, IUnknown_QueryInterface IID_IClientSecurity);
1682 if (hr == S_OK) IUnknown_Release(pOtherUnknown);
1684 hr = IUnknown_QueryInterface(pProxy, &IID_IMultiQI, (LPVOID*)&pOtherUnknown);
1685 ok_ole_success(hr, IUnknown_QueryInterface IID_IMultiQI);
1686 if (hr == S_OK) IUnknown_Release(pOtherUnknown);
1688 hr = IUnknown_QueryInterface(pProxy, &IID_IMarshal, (LPVOID*)&pOtherUnknown);
1689 ok_ole_success(hr, IUnknown_QueryInterface IID_IMarshal);
1690 if (hr == S_OK) IUnknown_Release(pOtherUnknown);
1692 /* IMarshal2 is also supported on NT-based systems, but is pretty much
1693 * useless as it has no more methods over IMarshal that it inherits from. */
1695 IUnknown_Release(pProxy);
1697 ok_no_locks();
1699 end_host_object(tid, thread);
1702 typedef struct
1704 IUnknown IUnknown_iface;
1705 ULONG refs;
1706 } HeapUnknown;
1708 static inline HeapUnknown *impl_from_IUnknown(IUnknown *iface)
1710 return CONTAINING_RECORD(iface, HeapUnknown, IUnknown_iface);
1713 static HRESULT WINAPI HeapUnknown_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
1715 if (IsEqualIID(riid, &IID_IUnknown))
1717 IUnknown_AddRef(iface);
1718 *ppv = iface;
1719 return S_OK;
1721 *ppv = NULL;
1722 return E_NOINTERFACE;
1725 static ULONG WINAPI HeapUnknown_AddRef(IUnknown *iface)
1727 HeapUnknown *This = impl_from_IUnknown(iface);
1728 return InterlockedIncrement((LONG*)&This->refs);
1731 static ULONG WINAPI HeapUnknown_Release(IUnknown *iface)
1733 HeapUnknown *This = impl_from_IUnknown(iface);
1734 ULONG refs = InterlockedDecrement((LONG*)&This->refs);
1735 if (!refs) HeapFree(GetProcessHeap(), 0, This);
1736 return refs;
1739 static const IUnknownVtbl HeapUnknown_Vtbl =
1741 HeapUnknown_QueryInterface,
1742 HeapUnknown_AddRef,
1743 HeapUnknown_Release
1746 static void test_proxybuffer(REFIID riid)
1748 HRESULT hr;
1749 IPSFactoryBuffer *psfb;
1750 IRpcProxyBuffer *proxy;
1751 LPVOID lpvtbl;
1752 ULONG refs;
1753 CLSID clsid;
1754 HeapUnknown *pUnkOuter = HeapAlloc(GetProcessHeap(), 0, sizeof(*pUnkOuter));
1756 pUnkOuter->IUnknown_iface.lpVtbl = &HeapUnknown_Vtbl;
1757 pUnkOuter->refs = 1;
1759 hr = CoGetPSClsid(riid, &clsid);
1760 ok_ole_success(hr, CoGetPSClsid);
1762 hr = CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL, &IID_IPSFactoryBuffer, (LPVOID*)&psfb);
1763 ok_ole_success(hr, CoGetClassObject);
1765 hr = IPSFactoryBuffer_CreateProxy(psfb, &pUnkOuter->IUnknown_iface, riid, &proxy, &lpvtbl);
1766 ok_ole_success(hr, IPSFactoryBuffer_CreateProxy);
1767 ok(lpvtbl != NULL, "IPSFactoryBuffer_CreateProxy succeeded, but returned a NULL vtable!\n");
1769 /* release our reference to the outer unknown object - the PS factory
1770 * buffer will have AddRef's it in the CreateProxy call */
1771 refs = IUnknown_Release(&pUnkOuter->IUnknown_iface);
1772 ok(refs == 1, "Ref count of outer unknown should have been 1 instead of %d\n", refs);
1774 /* Not checking return, unreliable on native. Maybe it leaks references? */
1775 IPSFactoryBuffer_Release(psfb);
1777 refs = IUnknown_Release((IUnknown *)lpvtbl);
1778 ok(refs == 0, "Ref-count leak of %d on IRpcProxyBuffer\n", refs);
1780 refs = IRpcProxyBuffer_Release(proxy);
1781 ok(refs == 0, "Ref-count leak of %d on IRpcProxyBuffer\n", refs);
1784 static void test_stubbuffer(REFIID riid)
1786 HRESULT hr;
1787 IPSFactoryBuffer *psfb;
1788 IRpcStubBuffer *stub;
1789 ULONG refs;
1790 CLSID clsid;
1792 cLocks = 0;
1794 hr = CoGetPSClsid(riid, &clsid);
1795 ok_ole_success(hr, CoGetPSClsid);
1797 hr = CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL, &IID_IPSFactoryBuffer, (LPVOID*)&psfb);
1798 ok_ole_success(hr, CoGetClassObject);
1800 hr = IPSFactoryBuffer_CreateStub(psfb, riid, (IUnknown*)&Test_ClassFactory, &stub);
1801 ok_ole_success(hr, IPSFactoryBuffer_CreateStub);
1803 /* Not checking return, unreliable on native. Maybe it leaks references? */
1804 IPSFactoryBuffer_Release(psfb);
1806 ok_more_than_one_lock();
1808 IRpcStubBuffer_Disconnect(stub);
1810 ok_no_locks();
1812 refs = IRpcStubBuffer_Release(stub);
1813 ok(refs == 0, "Ref-count leak of %d on IRpcProxyBuffer\n", refs);
1816 static HWND hwnd_app;
1818 static HRESULT WINAPI TestRE_IClassFactory_CreateInstance(
1819 LPCLASSFACTORY iface,
1820 LPUNKNOWN pUnkOuter,
1821 REFIID riid,
1822 LPVOID *ppvObj)
1824 DWORD_PTR res;
1825 if (IsEqualIID(riid, &IID_IWineTest))
1827 BOOL ret = SendMessageTimeout(hwnd_app, WM_NULL, 0, 0, SMTO_BLOCK, 5000, &res);
1828 ok(ret, "Timed out sending a message to originating window during RPC call\n");
1830 *ppvObj = NULL;
1831 return S_FALSE;
1834 static const IClassFactoryVtbl TestREClassFactory_Vtbl =
1836 Test_IClassFactory_QueryInterface,
1837 Test_IClassFactory_AddRef,
1838 Test_IClassFactory_Release,
1839 TestRE_IClassFactory_CreateInstance,
1840 Test_IClassFactory_LockServer
1843 static IClassFactory TestRE_ClassFactory = { &TestREClassFactory_Vtbl };
1845 static LRESULT CALLBACK window_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1847 switch (msg)
1849 case WM_USER:
1851 HRESULT hr;
1852 IStream *pStream = NULL;
1853 IClassFactory *proxy = NULL;
1854 IUnknown *object;
1855 DWORD tid;
1856 HANDLE thread;
1858 cLocks = 0;
1860 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1861 ok_ole_success(hr, CreateStreamOnHGlobal);
1862 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&TestRE_ClassFactory, MSHLFLAGS_NORMAL, &thread);
1864 ok_more_than_one_lock();
1866 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1867 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&proxy);
1868 ok_ole_success(hr, CoReleaseMarshalData);
1869 IStream_Release(pStream);
1871 ok_more_than_one_lock();
1873 /* note the use of the magic IID_IWineTest value to tell remote thread
1874 * to try to send a message back to us */
1875 hr = IClassFactory_CreateInstance(proxy, NULL, &IID_IWineTest, (void **)&object);
1876 ok(hr == S_FALSE, "expected S_FALSE, got %d\n", hr);
1878 IClassFactory_Release(proxy);
1880 ok_no_locks();
1882 end_host_object(tid, thread);
1884 PostMessage(hwnd, WM_QUIT, 0, 0);
1886 return 0;
1888 case WM_USER+1:
1890 HRESULT hr;
1891 IStream *pStream = NULL;
1892 IClassFactory *proxy = NULL;
1893 IUnknown *object;
1894 DWORD tid;
1895 HANDLE thread;
1897 cLocks = 0;
1899 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1900 ok_ole_success(hr, CreateStreamOnHGlobal);
1901 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&TestRE_ClassFactory, MSHLFLAGS_NORMAL, &thread);
1903 ok_more_than_one_lock();
1905 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1906 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&proxy);
1907 ok_ole_success(hr, CoReleaseMarshalData);
1908 IStream_Release(pStream);
1910 ok_more_than_one_lock();
1912 /* post quit message before a doing a COM call to show that a pending
1913 * WM_QUIT message doesn't stop the call from succeeding */
1914 PostMessage(hwnd, WM_QUIT, 0, 0);
1915 hr = IClassFactory_CreateInstance(proxy, NULL, &IID_IUnknown, (void **)&object);
1916 ok(hr == S_FALSE, "IClassFactory_CreateInstance returned 0x%08x, expected S_FALSE\n", hr);
1918 IClassFactory_Release(proxy);
1920 ok_no_locks();
1922 end_host_object(tid, thread);
1924 return 0;
1926 case WM_USER+2:
1928 HRESULT hr;
1929 IStream *pStream = NULL;
1930 IClassFactory *proxy = NULL;
1931 IUnknown *object;
1932 DWORD tid;
1933 HANDLE thread;
1935 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1936 ok_ole_success(hr, CreateStreamOnHGlobal);
1937 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
1939 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1940 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&proxy);
1941 ok_ole_success(hr, CoReleaseMarshalData);
1942 IStream_Release(pStream);
1944 /* shows that COM calls executed during the processing of sent
1945 * messages should fail */
1946 hr = IClassFactory_CreateInstance(proxy, NULL, &IID_IUnknown, (void **)&object);
1947 ok(hr == RPC_E_CANTCALLOUT_ININPUTSYNCCALL,
1948 "COM call during processing of sent message should return RPC_E_CANTCALLOUT_ININPUTSYNCCALL instead of 0x%08x\n", hr);
1950 IClassFactory_Release(proxy);
1952 end_host_object(tid, thread);
1954 PostQuitMessage(0);
1956 return 0;
1958 default:
1959 return DefWindowProc(hwnd, msg, wparam, lparam);
1963 static void register_test_window(void)
1965 WNDCLASS wndclass;
1967 memset(&wndclass, 0, sizeof(wndclass));
1968 wndclass.lpfnWndProc = window_proc;
1969 wndclass.lpszClassName = "WineCOMTest";
1970 RegisterClass(&wndclass);
1973 static void test_message_reentrancy(void)
1975 MSG msg;
1977 hwnd_app = CreateWindow("WineCOMTest", NULL, 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, NULL, 0);
1978 ok(hwnd_app != NULL, "Window creation failed\n");
1980 /* start message re-entrancy test */
1981 PostMessage(hwnd_app, WM_USER, 0, 0);
1983 while (GetMessage(&msg, NULL, 0, 0))
1985 TranslateMessage(&msg);
1986 DispatchMessage(&msg);
1988 DestroyWindow(hwnd_app);
1991 static HRESULT WINAPI TestMsg_IClassFactory_CreateInstance(
1992 LPCLASSFACTORY iface,
1993 LPUNKNOWN pUnkOuter,
1994 REFIID riid,
1995 LPVOID *ppvObj)
1997 *ppvObj = NULL;
1998 SendMessage(hwnd_app, WM_USER+2, 0, 0);
1999 return S_OK;
2002 static IClassFactoryVtbl TestMsgClassFactory_Vtbl =
2004 Test_IClassFactory_QueryInterface,
2005 Test_IClassFactory_AddRef,
2006 Test_IClassFactory_Release,
2007 TestMsg_IClassFactory_CreateInstance,
2008 Test_IClassFactory_LockServer
2011 static IClassFactory TestMsg_ClassFactory = { &TestMsgClassFactory_Vtbl };
2013 static void test_call_from_message(void)
2015 MSG msg;
2016 IStream *pStream;
2017 HRESULT hr;
2018 IClassFactory *proxy;
2019 DWORD tid;
2020 HANDLE thread;
2021 IUnknown *object;
2023 hwnd_app = CreateWindow("WineCOMTest", NULL, 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, NULL, 0);
2024 ok(hwnd_app != NULL, "Window creation failed\n");
2026 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
2027 ok_ole_success(hr, CreateStreamOnHGlobal);
2028 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&TestMsg_ClassFactory, MSHLFLAGS_NORMAL, &thread);
2030 ok_more_than_one_lock();
2032 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
2033 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&proxy);
2034 ok_ole_success(hr, CoReleaseMarshalData);
2035 IStream_Release(pStream);
2037 ok_more_than_one_lock();
2039 /* start message re-entrancy test */
2040 hr = IClassFactory_CreateInstance(proxy, NULL, &IID_IUnknown, (void **)&object);
2041 ok_ole_success(hr, IClassFactory_CreateInstance);
2043 IClassFactory_Release(proxy);
2045 ok_no_locks();
2047 end_host_object(tid, thread);
2049 while (GetMessage(&msg, NULL, 0, 0))
2051 TranslateMessage(&msg);
2052 DispatchMessage(&msg);
2054 DestroyWindow(hwnd_app);
2057 static void test_WM_QUIT_handling(void)
2059 MSG msg;
2061 hwnd_app = CreateWindow("WineCOMTest", NULL, 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, NULL, 0);
2062 ok(hwnd_app != NULL, "Window creation failed\n");
2064 /* start WM_QUIT handling test */
2065 PostMessage(hwnd_app, WM_USER+1, 0, 0);
2067 while (GetMessage(&msg, NULL, 0, 0))
2069 TranslateMessage(&msg);
2070 DispatchMessage(&msg);
2074 static SIZE_T round_global_size(SIZE_T size)
2076 static SIZE_T global_size_alignment = -1;
2077 if (global_size_alignment == -1)
2079 void *p = GlobalAlloc(GMEM_FIXED, 1);
2080 global_size_alignment = GlobalSize(p);
2081 GlobalFree(p);
2084 return ((size + global_size_alignment - 1) & ~(global_size_alignment - 1));
2087 static void test_freethreadedmarshaldata(IStream *pStream, MSHCTX mshctx, void *ptr, DWORD mshlflags)
2089 HGLOBAL hglobal;
2090 DWORD size;
2091 char *marshal_data;
2092 HRESULT hr;
2094 hr = GetHGlobalFromStream(pStream, &hglobal);
2095 ok_ole_success(hr, GetHGlobalFromStream);
2097 size = GlobalSize(hglobal);
2099 marshal_data = GlobalLock(hglobal);
2101 if (mshctx == MSHCTX_INPROC)
2103 DWORD expected_size = round_global_size(3*sizeof(DWORD) + sizeof(GUID));
2104 ok(size == expected_size ||
2105 broken(size == (2*sizeof(DWORD))) /* Win9x & NT4 */,
2106 "size should have been %d instead of %d\n", expected_size, size);
2108 ok(*(DWORD *)marshal_data == mshlflags, "expected 0x%x, but got 0x%x for mshctx\n", mshlflags, *(DWORD *)marshal_data);
2109 marshal_data += sizeof(DWORD);
2110 ok(*(void **)marshal_data == ptr, "expected %p, but got %p for mshctx\n", ptr, *(void **)marshal_data);
2111 marshal_data += sizeof(void *);
2112 if (sizeof(void*) == 4 && size >= 3*sizeof(DWORD))
2114 ok(*(DWORD *)marshal_data == 0, "expected 0x0, but got 0x%x\n", *(DWORD *)marshal_data);
2115 marshal_data += sizeof(DWORD);
2117 if (size >= 3*sizeof(DWORD) + sizeof(GUID))
2119 trace("got guid data: {%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\n",
2120 ((GUID *)marshal_data)->Data1, ((GUID *)marshal_data)->Data2, ((GUID *)marshal_data)->Data3,
2121 ((GUID *)marshal_data)->Data4[0], ((GUID *)marshal_data)->Data4[1], ((GUID *)marshal_data)->Data4[2], ((GUID *)marshal_data)->Data4[3],
2122 ((GUID *)marshal_data)->Data4[4], ((GUID *)marshal_data)->Data4[5], ((GUID *)marshal_data)->Data4[6], ((GUID *)marshal_data)->Data4[7]);
2125 else
2127 ok(size > sizeof(DWORD), "size should have been > sizeof(DWORD), not %d\n", size);
2128 ok(*(DWORD *)marshal_data == 0x574f454d /* MEOW */,
2129 "marshal data should be filled by standard marshal and start with MEOW signature\n");
2132 GlobalUnlock(hglobal);
2135 static void test_freethreadedmarshaler(void)
2137 HRESULT hr;
2138 IUnknown *pFTUnknown;
2139 IMarshal *pFTMarshal;
2140 IStream *pStream;
2141 IUnknown *pProxy;
2142 static const LARGE_INTEGER llZero;
2144 cLocks = 0;
2145 hr = CoCreateFreeThreadedMarshaler(NULL, &pFTUnknown);
2146 ok_ole_success(hr, CoCreateFreeThreadedMarshaler);
2147 hr = IUnknown_QueryInterface(pFTUnknown, &IID_IMarshal, (void **)&pFTMarshal);
2148 ok_ole_success(hr, IUnknown_QueryInterface);
2149 IUnknown_Release(pFTUnknown);
2151 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
2152 ok_ole_success(hr, CreateStreamOnHGlobal);
2154 /* inproc normal marshaling */
2156 hr = IMarshal_MarshalInterface(pFTMarshal, pStream, &IID_IClassFactory,
2157 &Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
2158 ok_ole_success(hr, IMarshal_MarshalInterface);
2160 ok_more_than_one_lock();
2162 test_freethreadedmarshaldata(pStream, MSHCTX_INPROC, &Test_ClassFactory, MSHLFLAGS_NORMAL);
2164 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2165 hr = IMarshal_UnmarshalInterface(pFTMarshal, pStream, &IID_IUnknown, (void **)&pProxy);
2166 ok_ole_success(hr, IMarshal_UnmarshalInterface);
2168 IUnknown_Release(pProxy);
2170 ok_no_locks();
2172 /* native doesn't allow us to unmarshal or release the stream data,
2173 * presumably because it wants us to call CoMarshalInterface instead */
2174 if (0)
2176 /* local normal marshaling */
2178 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2179 hr = IMarshal_MarshalInterface(pFTMarshal, pStream, &IID_IClassFactory, &Test_ClassFactory, MSHCTX_LOCAL, NULL, MSHLFLAGS_NORMAL);
2180 ok_ole_success(hr, IMarshal_MarshalInterface);
2182 ok_more_than_one_lock();
2184 test_freethreadedmarshaldata(pStream, MSHCTX_LOCAL, &Test_ClassFactory, MSHLFLAGS_NORMAL);
2186 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2187 hr = IMarshal_ReleaseMarshalData(pFTMarshal, pStream);
2188 ok_ole_success(hr, IMarshal_ReleaseMarshalData);
2190 ok_no_locks();
2193 /* inproc table-strong marshaling */
2195 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2196 hr = IMarshal_MarshalInterface(pFTMarshal, pStream, &IID_IClassFactory,
2197 (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, (void *)0xdeadbeef,
2198 MSHLFLAGS_TABLESTRONG);
2199 ok_ole_success(hr, IMarshal_MarshalInterface);
2201 ok_more_than_one_lock();
2203 test_freethreadedmarshaldata(pStream, MSHCTX_INPROC, &Test_ClassFactory, MSHLFLAGS_TABLESTRONG);
2205 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2206 hr = IMarshal_UnmarshalInterface(pFTMarshal, pStream, &IID_IUnknown, (void **)&pProxy);
2207 ok_ole_success(hr, IMarshal_UnmarshalInterface);
2209 IUnknown_Release(pProxy);
2211 ok_more_than_one_lock();
2213 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2214 hr = IMarshal_ReleaseMarshalData(pFTMarshal, pStream);
2215 ok_ole_success(hr, IMarshal_ReleaseMarshalData);
2217 ok_no_locks();
2219 /* inproc table-weak marshaling */
2221 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2222 hr = IMarshal_MarshalInterface(pFTMarshal, pStream, &IID_IClassFactory,
2223 (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, (void *)0xdeadbeef,
2224 MSHLFLAGS_TABLEWEAK);
2225 ok_ole_success(hr, IMarshal_MarshalInterface);
2227 ok_no_locks();
2229 test_freethreadedmarshaldata(pStream, MSHCTX_INPROC, &Test_ClassFactory, MSHLFLAGS_TABLEWEAK);
2231 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2232 hr = IMarshal_UnmarshalInterface(pFTMarshal, pStream, &IID_IUnknown, (void **)&pProxy);
2233 ok_ole_success(hr, IMarshal_UnmarshalInterface);
2235 ok_more_than_one_lock();
2237 IUnknown_Release(pProxy);
2239 ok_no_locks();
2241 /* inproc normal marshaling (for extraordinary cases) */
2243 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2244 hr = IMarshal_MarshalInterface(pFTMarshal, pStream, &IID_IClassFactory,
2245 &Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
2246 ok_ole_success(hr, IMarshal_MarshalInterface);
2248 ok_more_than_one_lock();
2250 /* this call shows that DisconnectObject does nothing */
2251 hr = IMarshal_DisconnectObject(pFTMarshal, 0);
2252 ok_ole_success(hr, IMarshal_DisconnectObject);
2254 ok_more_than_one_lock();
2256 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2257 hr = IMarshal_ReleaseMarshalData(pFTMarshal, pStream);
2258 ok_ole_success(hr, IMarshal_ReleaseMarshalData);
2260 ok_no_locks();
2262 /* doesn't enforce marshaling rules here and allows us to unmarshal the
2263 * interface, even though it was freed above */
2264 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2265 hr = IMarshal_UnmarshalInterface(pFTMarshal, pStream, &IID_IUnknown, (void **)&pProxy);
2266 ok_ole_success(hr, IMarshal_UnmarshalInterface);
2268 ok_no_locks();
2270 IStream_Release(pStream);
2271 IMarshal_Release(pFTMarshal);
2274 static HRESULT reg_unreg_wine_test_class(BOOL Register)
2276 HRESULT hr;
2277 char buffer[256];
2278 LPOLESTR pszClsid;
2279 HKEY hkey;
2280 DWORD dwDisposition;
2281 DWORD error;
2283 hr = StringFromCLSID(&CLSID_WineTest, &pszClsid);
2284 ok_ole_success(hr, "StringFromCLSID");
2285 strcpy(buffer, "CLSID\\");
2286 WideCharToMultiByte(CP_ACP, 0, pszClsid, -1, buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), NULL, NULL);
2287 CoTaskMemFree(pszClsid);
2288 strcat(buffer, "\\InprocHandler32");
2289 if (Register)
2291 error = RegCreateKeyEx(HKEY_CLASSES_ROOT, buffer, 0, NULL, 0, KEY_SET_VALUE, NULL, &hkey, &dwDisposition);
2292 if (error == ERROR_ACCESS_DENIED)
2294 skip("Not authorized to modify the Classes key\n");
2295 return E_FAIL;
2297 ok(error == ERROR_SUCCESS, "RegCreateKeyEx failed with error %d\n", error);
2298 if (error != ERROR_SUCCESS) hr = E_FAIL;
2299 error = RegSetValueEx(hkey, NULL, 0, REG_SZ, (const unsigned char *)"\"ole32.dll\"", strlen("\"ole32.dll\"") + 1);
2300 ok(error == ERROR_SUCCESS, "RegSetValueEx failed with error %d\n", error);
2301 if (error != ERROR_SUCCESS) hr = E_FAIL;
2302 RegCloseKey(hkey);
2304 else
2306 RegDeleteKey(HKEY_CLASSES_ROOT, buffer);
2307 *strrchr(buffer, '\\') = '\0';
2308 RegDeleteKey(HKEY_CLASSES_ROOT, buffer);
2310 return hr;
2313 static void test_inproc_handler(void)
2315 HRESULT hr;
2316 IUnknown *pObject;
2317 IUnknown *pObject2;
2319 if (FAILED(reg_unreg_wine_test_class(TRUE)))
2320 return;
2322 hr = CoCreateInstance(&CLSID_WineTest, NULL, CLSCTX_INPROC_HANDLER, &IID_IUnknown, (void **)&pObject);
2323 ok_ole_success(hr, "CoCreateInstance");
2325 if (SUCCEEDED(hr))
2327 hr = IUnknown_QueryInterface(pObject, &IID_IWineTest, (void **)&pObject2);
2328 ok(hr == E_NOINTERFACE, "IUnknown_QueryInterface on handler for invalid interface returned 0x%08x instead of E_NOINTERFACE\n", hr);
2330 /* it's a handler as it supports IOleObject */
2331 hr = IUnknown_QueryInterface(pObject, &IID_IOleObject, (void **)&pObject2);
2332 ok_ole_success(hr, "IUnknown_QueryInterface(&IID_IOleObject)");
2333 IUnknown_Release(pObject2);
2335 IUnknown_Release(pObject);
2338 reg_unreg_wine_test_class(FALSE);
2341 static HRESULT WINAPI Test_SMI_QueryInterface(
2342 IStdMarshalInfo *iface,
2343 REFIID riid,
2344 LPVOID *ppvObj)
2346 if (ppvObj == NULL) return E_POINTER;
2348 if (IsEqualGUID(riid, &IID_IUnknown) ||
2349 IsEqualGUID(riid, &IID_IStdMarshalInfo))
2351 *ppvObj = iface;
2352 IClassFactory_AddRef(iface);
2353 return S_OK;
2356 return E_NOINTERFACE;
2359 static ULONG WINAPI Test_SMI_AddRef(IStdMarshalInfo *iface)
2361 LockModule();
2362 return 2; /* non-heap-based object */
2365 static ULONG WINAPI Test_SMI_Release(IStdMarshalInfo *iface)
2367 UnlockModule();
2368 return 1; /* non-heap-based object */
2371 static HRESULT WINAPI Test_SMI_GetClassForHandler(
2372 IStdMarshalInfo *iface,
2373 DWORD dwDestContext,
2374 void *pvDestContext,
2375 CLSID *pClsid)
2377 *pClsid = CLSID_WineTest;
2378 return S_OK;
2381 static const IStdMarshalInfoVtbl Test_SMI_Vtbl =
2383 Test_SMI_QueryInterface,
2384 Test_SMI_AddRef,
2385 Test_SMI_Release,
2386 Test_SMI_GetClassForHandler
2389 static IStdMarshalInfo Test_SMI = {&Test_SMI_Vtbl};
2391 static void test_handler_marshaling(void)
2393 HRESULT hr;
2394 IStream *pStream = NULL;
2395 IUnknown *pProxy = NULL;
2396 IUnknown *pObject;
2397 DWORD tid;
2398 HANDLE thread;
2399 static const LARGE_INTEGER ullZero;
2401 if (FAILED(reg_unreg_wine_test_class(TRUE)))
2402 return;
2403 cLocks = 0;
2405 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
2406 ok_ole_success(hr, "CreateStreamOnHGlobal");
2407 tid = start_host_object(pStream, &IID_IUnknown, (IUnknown*)&Test_SMI, MSHLFLAGS_NORMAL, &thread);
2409 ok_more_than_one_lock();
2411 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
2412 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)&pProxy);
2413 ok_ole_success(hr, "CoUnmarshalInterface");
2414 IStream_Release(pStream);
2416 if(hr == S_OK)
2418 ok_more_than_one_lock();
2420 hr = IUnknown_QueryInterface(pProxy, &IID_IWineTest, (void **)&pObject);
2421 ok(hr == E_NOINTERFACE, "IUnknown_QueryInterface with unknown IID should have returned E_NOINTERFACE instead of 0x%08x\n", hr);
2423 /* it's a handler as it supports IOleObject */
2424 hr = IUnknown_QueryInterface(pProxy, &IID_IOleObject, (void **)&pObject);
2425 todo_wine
2426 ok_ole_success(hr, "IUnknown_QueryInterface(&IID_IOleObject)");
2427 if (SUCCEEDED(hr)) IUnknown_Release(pObject);
2429 IUnknown_Release(pProxy);
2431 ok_no_locks();
2434 end_host_object(tid, thread);
2435 reg_unreg_wine_test_class(FALSE);
2437 /* FIXME: test IPersist interface has the same effect as IStdMarshalInfo */
2441 static void test_client_security(void)
2443 HRESULT hr;
2444 IStream *pStream = NULL;
2445 IClassFactory *pProxy = NULL;
2446 IUnknown *pProxy2 = NULL;
2447 IUnknown *pUnknown1 = NULL;
2448 IUnknown *pUnknown2 = NULL;
2449 IClientSecurity *pCliSec = NULL;
2450 IMarshal *pMarshal;
2451 DWORD tid;
2452 HANDLE thread;
2453 static const LARGE_INTEGER ullZero;
2454 DWORD dwAuthnSvc;
2455 DWORD dwAuthzSvc;
2456 OLECHAR *pServerPrincName;
2457 DWORD dwAuthnLevel;
2458 DWORD dwImpLevel;
2459 void *pAuthInfo;
2460 DWORD dwCapabilities;
2461 void *pv;
2463 cLocks = 0;
2465 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
2466 ok_ole_success(hr, "CreateStreamOnHGlobal");
2467 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
2469 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
2470 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
2471 ok_ole_success(hr, "CoUnmarshalInterface");
2472 IStream_Release(pStream);
2474 hr = IUnknown_QueryInterface(pProxy, &IID_IUnknown, (LPVOID*)&pUnknown1);
2475 ok_ole_success(hr, "IUnknown_QueryInterface IID_IUnknown");
2477 hr = IUnknown_QueryInterface(pProxy, &IID_IRemUnknown, (LPVOID*)&pProxy2);
2478 ok_ole_success(hr, "IUnknown_QueryInterface IID_IStream");
2480 hr = IUnknown_QueryInterface(pProxy2, &IID_IUnknown, (LPVOID*)&pUnknown2);
2481 ok_ole_success(hr, "IUnknown_QueryInterface IID_IUnknown");
2483 ok(pUnknown1 == pUnknown2, "both proxy's IUnknowns should be the same - %p, %p\n", pUnknown1, pUnknown2);
2485 hr = IUnknown_QueryInterface(pProxy, &IID_IMarshal, (LPVOID*)&pMarshal);
2486 ok_ole_success(hr, "IUnknown_QueryInterface IID_IMarshal");
2488 hr = IUnknown_QueryInterface(pProxy, &IID_IClientSecurity, (LPVOID*)&pCliSec);
2489 ok_ole_success(hr, "IUnknown_QueryInterface IID_IClientSecurity");
2491 hr = IClientSecurity_QueryBlanket(pCliSec, (IUnknown *)pProxy, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
2492 todo_wine ok_ole_success(hr, "IClientSecurity_QueryBlanket (all NULLs)");
2494 hr = IClientSecurity_QueryBlanket(pCliSec, (IUnknown *)pMarshal, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
2495 todo_wine ok(hr == E_NOINTERFACE, "IClientSecurity_QueryBlanket with local interface should have returned E_NOINTERFACE instead of 0x%08x\n", hr);
2497 hr = IClientSecurity_QueryBlanket(pCliSec, (IUnknown *)pProxy, &dwAuthnSvc, &dwAuthzSvc, &pServerPrincName, &dwAuthnLevel, &dwImpLevel, &pAuthInfo, &dwCapabilities);
2498 todo_wine ok_ole_success(hr, "IClientSecurity_QueryBlanket");
2500 hr = IClientSecurity_SetBlanket(pCliSec, (IUnknown *)pProxy, dwAuthnSvc, dwAuthzSvc, pServerPrincName, dwAuthnLevel, RPC_C_IMP_LEVEL_IMPERSONATE, pAuthInfo, dwCapabilities);
2501 todo_wine ok_ole_success(hr, "IClientSecurity_SetBlanket");
2503 hr = IClassFactory_CreateInstance(pProxy, NULL, &IID_IWineTest, &pv);
2504 ok(hr == E_NOINTERFACE, "COM call should have succeeded instead of returning 0x%08x\n", hr);
2506 hr = IClientSecurity_SetBlanket(pCliSec, (IUnknown *)pMarshal, dwAuthnSvc, dwAuthzSvc, pServerPrincName, dwAuthnLevel, dwImpLevel, pAuthInfo, dwCapabilities);
2507 todo_wine ok(hr == E_NOINTERFACE, "IClientSecurity_SetBlanket with local interface should have returned E_NOINTERFACE instead of 0x%08x\n", hr);
2509 hr = IClientSecurity_SetBlanket(pCliSec, (IUnknown *)pProxy, 0xdeadbeef, dwAuthzSvc, pServerPrincName, dwAuthnLevel, dwImpLevel, pAuthInfo, dwCapabilities);
2510 todo_wine ok(hr == E_INVALIDARG, "IClientSecurity_SetBlanke with invalid dwAuthnSvc should have returned E_INVALIDARG instead of 0x%08x\n", hr);
2512 CoTaskMemFree(pServerPrincName);
2514 hr = IClientSecurity_QueryBlanket(pCliSec, pUnknown1, &dwAuthnSvc, &dwAuthzSvc, &pServerPrincName, &dwAuthnLevel, &dwImpLevel, &pAuthInfo, &dwCapabilities);
2515 todo_wine ok_ole_success(hr, "IClientSecurity_QueryBlanket(IUnknown)");
2517 CoTaskMemFree(pServerPrincName);
2519 IClassFactory_Release(pProxy);
2520 IUnknown_Release(pProxy2);
2521 IUnknown_Release(pUnknown1);
2522 IUnknown_Release(pUnknown2);
2523 IMarshal_Release(pMarshal);
2524 IClientSecurity_Release(pCliSec);
2526 end_host_object(tid, thread);
2529 static HANDLE heventShutdown;
2531 static void LockModuleOOP(void)
2533 InterlockedIncrement(&cLocks); /* for test purposes only */
2534 CoAddRefServerProcess();
2537 static void UnlockModuleOOP(void)
2539 InterlockedDecrement(&cLocks); /* for test purposes only */
2540 if (!CoReleaseServerProcess())
2541 SetEvent(heventShutdown);
2544 static HWND hwnd_app;
2546 static HRESULT WINAPI TestOOP_IClassFactory_QueryInterface(
2547 LPCLASSFACTORY iface,
2548 REFIID riid,
2549 LPVOID *ppvObj)
2551 if (ppvObj == NULL) return E_POINTER;
2553 if (IsEqualGUID(riid, &IID_IUnknown) ||
2554 IsEqualGUID(riid, &IID_IClassFactory))
2556 *ppvObj = iface;
2557 IClassFactory_AddRef(iface);
2558 return S_OK;
2561 return E_NOINTERFACE;
2564 static ULONG WINAPI TestOOP_IClassFactory_AddRef(LPCLASSFACTORY iface)
2566 return 2; /* non-heap-based object */
2569 static ULONG WINAPI TestOOP_IClassFactory_Release(LPCLASSFACTORY iface)
2571 return 1; /* non-heap-based object */
2574 static HRESULT WINAPI TestOOP_IClassFactory_CreateInstance(
2575 LPCLASSFACTORY iface,
2576 LPUNKNOWN pUnkOuter,
2577 REFIID riid,
2578 LPVOID *ppvObj)
2580 if (IsEqualIID(riid, &IID_IClassFactory) || IsEqualIID(riid, &IID_IUnknown))
2582 *ppvObj = iface;
2583 return S_OK;
2585 return CLASS_E_CLASSNOTAVAILABLE;
2588 static HRESULT WINAPI TestOOP_IClassFactory_LockServer(
2589 LPCLASSFACTORY iface,
2590 BOOL fLock)
2592 if (fLock)
2593 LockModuleOOP();
2594 else
2595 UnlockModuleOOP();
2596 return S_OK;
2599 static const IClassFactoryVtbl TestClassFactoryOOP_Vtbl =
2601 TestOOP_IClassFactory_QueryInterface,
2602 TestOOP_IClassFactory_AddRef,
2603 TestOOP_IClassFactory_Release,
2604 TestOOP_IClassFactory_CreateInstance,
2605 TestOOP_IClassFactory_LockServer
2608 static IClassFactory TestOOP_ClassFactory = { &TestClassFactoryOOP_Vtbl };
2610 static void test_register_local_server(void)
2612 DWORD cookie;
2613 HRESULT hr;
2614 HANDLE ready_event;
2615 HANDLE quit_event;
2616 DWORD wait;
2618 heventShutdown = CreateEvent(NULL, TRUE, FALSE, NULL);
2620 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&TestOOP_ClassFactory,
2621 CLSCTX_LOCAL_SERVER, REGCLS_SINGLEUSE, &cookie);
2622 ok_ole_success(hr, CoRegisterClassObject);
2624 ready_event = CreateEvent(NULL, FALSE, FALSE, "Wine COM Test Ready Event");
2625 SetEvent(ready_event);
2627 quit_event = CreateEvent(NULL, FALSE, FALSE, "Wine COM Test Quit Event");
2631 wait = MsgWaitForMultipleObjects(1, &quit_event, FALSE, 30000, QS_ALLINPUT);
2632 if (wait == WAIT_OBJECT_0+1)
2634 MSG msg;
2635 BOOL ret = PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
2636 if (ret)
2638 trace("Message 0x%x\n", msg.message);
2639 TranslateMessage(&msg);
2640 DispatchMessage(&msg);
2644 while (wait == WAIT_OBJECT_0+1);
2646 ok( wait == WAIT_OBJECT_0, "quit event wait timed out\n" );
2647 hr = CoRevokeClassObject(cookie);
2648 ok_ole_success(hr, CoRevokeClassObject);
2651 static HANDLE create_target_process(const char *arg)
2653 char **argv;
2654 char cmdline[MAX_PATH];
2655 BOOL ret;
2656 PROCESS_INFORMATION pi;
2657 STARTUPINFO si = { 0 };
2658 si.cb = sizeof(si);
2660 pi.hThread = NULL;
2661 pi.hProcess = NULL;
2662 winetest_get_mainargs( &argv );
2663 sprintf(cmdline, "%s %s %s", argv[0], argv[1], arg);
2664 ret = CreateProcess(argv[0], cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
2665 ok(ret, "CreateProcess failed with error: %u\n", GetLastError());
2666 if (pi.hThread) CloseHandle(pi.hThread);
2667 return pi.hProcess;
2670 /* tests functions commonly used by out of process COM servers */
2671 static void test_local_server(void)
2673 DWORD cookie;
2674 HRESULT hr;
2675 IClassFactory * cf;
2676 DWORD ret;
2677 HANDLE process;
2678 HANDLE quit_event;
2679 HANDLE ready_event;
2681 heventShutdown = CreateEvent(NULL, TRUE, FALSE, NULL);
2683 cLocks = 0;
2685 /* Start the object suspended */
2686 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&TestOOP_ClassFactory,
2687 CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE | REGCLS_SUSPENDED, &cookie);
2688 ok_ole_success(hr, CoRegisterClassObject);
2690 /* ... and CoGetClassObject does not find it and fails when it looks for the
2691 * class in the registry */
2692 hr = CoGetClassObject(&CLSID_WineOOPTest, CLSCTX_INPROC_SERVER,
2693 NULL, &IID_IClassFactory, (LPVOID*)&cf);
2694 ok(hr == REGDB_E_CLASSNOTREG || /* NT */
2695 hr == S_OK /* Win9x */,
2696 "CoGetClassObject should have returned REGDB_E_CLASSNOTREG instead of 0x%08x\n", hr);
2698 /* Resume the object suspended above ... */
2699 hr = CoResumeClassObjects();
2700 ok_ole_success(hr, CoResumeClassObjects);
2702 /* ... and now it should succeed */
2703 hr = CoGetClassObject(&CLSID_WineOOPTest, CLSCTX_INPROC_SERVER,
2704 NULL, &IID_IClassFactory, (LPVOID*)&cf);
2705 ok_ole_success(hr, CoGetClassObject);
2707 /* Now check the locking is working */
2708 /* NOTE: we are accessing the class directly, not through a proxy */
2710 ok_no_locks();
2712 hr = IClassFactory_LockServer(cf, TRUE);
2713 ok_ole_success(hr, IClassFactory_LockServer);
2715 ok_more_than_one_lock();
2717 IClassFactory_LockServer(cf, FALSE);
2718 ok_ole_success(hr, IClassFactory_LockServer);
2720 ok_no_locks();
2722 IClassFactory_Release(cf);
2724 /* wait for shutdown signal */
2725 ret = WaitForSingleObject(heventShutdown, 0);
2726 ok(ret != WAIT_TIMEOUT, "Server didn't shut down\n");
2728 /* try to connect again after SCM has suspended registered class objects */
2729 hr = CoGetClassObject(&CLSID_WineOOPTest, CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER, NULL,
2730 &IID_IClassFactory, (LPVOID*)&cf);
2731 ok(hr == CO_E_SERVER_STOPPING || /* NT */
2732 hr == REGDB_E_CLASSNOTREG || /* win2k */
2733 hr == S_OK /* Win9x */,
2734 "CoGetClassObject should have returned CO_E_SERVER_STOPPING or REGDB_E_CLASSNOTREG instead of 0x%08x\n", hr);
2736 hr = CoRevokeClassObject(cookie);
2737 ok_ole_success(hr, CoRevokeClassObject);
2739 CloseHandle(heventShutdown);
2741 process = create_target_process("-Embedding");
2742 ok(process != NULL, "couldn't start local server process, error was %d\n", GetLastError());
2744 ready_event = CreateEvent(NULL, FALSE, FALSE, "Wine COM Test Ready Event");
2745 ok( !WaitForSingleObject(ready_event, 10000), "wait timed out\n" );
2746 CloseHandle(ready_event);
2748 hr = CoCreateInstance(&CLSID_WineOOPTest, NULL, CLSCTX_LOCAL_SERVER, &IID_IClassFactory, (void **)&cf);
2749 ok_ole_success(hr, CoCreateInstance);
2751 IClassFactory_Release(cf);
2753 hr = CoCreateInstance(&CLSID_WineOOPTest, NULL, CLSCTX_LOCAL_SERVER, &IID_IClassFactory, (void **)&cf);
2754 ok(hr == REGDB_E_CLASSNOTREG, "Second CoCreateInstance on REGCLS_SINGLEUSE object should have failed\n");
2756 quit_event = CreateEvent(NULL, FALSE, FALSE, "Wine COM Test Quit Event");
2757 SetEvent(quit_event);
2759 winetest_wait_child_process( process );
2760 CloseHandle(quit_event);
2761 CloseHandle(process);
2764 struct git_params
2766 DWORD cookie;
2767 IGlobalInterfaceTable *git;
2770 static DWORD CALLBACK get_global_interface_proc(LPVOID pv)
2772 HRESULT hr;
2773 struct git_params *params = pv;
2774 IClassFactory *cf;
2776 hr = IGlobalInterfaceTable_GetInterfaceFromGlobal(params->git, params->cookie, &IID_IClassFactory, (void **)&cf);
2777 ok(hr == CO_E_NOTINITIALIZED ||
2778 broken(hr == E_UNEXPECTED) /* win2k */ ||
2779 broken(hr == S_OK) /* NT 4 */,
2780 "IGlobalInterfaceTable_GetInterfaceFromGlobal should have failed with error CO_E_NOTINITIALIZED or E_UNEXPECTED instead of 0x%08x\n",
2781 hr);
2782 if (hr == S_OK)
2783 IClassFactory_Release(cf);
2785 CoInitialize(NULL);
2787 hr = IGlobalInterfaceTable_GetInterfaceFromGlobal(params->git, params->cookie, &IID_IClassFactory, (void **)&cf);
2788 ok_ole_success(hr, IGlobalInterfaceTable_GetInterfaceFromGlobal);
2790 IClassFactory_Release(cf);
2792 CoUninitialize();
2794 return hr;
2797 static void test_globalinterfacetable(void)
2799 HRESULT hr;
2800 IGlobalInterfaceTable *git;
2801 DWORD cookie;
2802 HANDLE thread;
2803 DWORD tid;
2804 struct git_params params;
2805 DWORD ret;
2806 IUnknown *object;
2808 trace("test_globalinterfacetable\n");
2809 cLocks = 0;
2811 hr = CoCreateInstance(&CLSID_StdGlobalInterfaceTable, NULL, CLSCTX_INPROC_SERVER, &IID_IGlobalInterfaceTable, (void **)&git);
2812 ok_ole_success(hr, CoCreateInstance);
2814 hr = IGlobalInterfaceTable_RegisterInterfaceInGlobal(git, (IUnknown *)&Test_ClassFactory, &IID_IClassFactory, &cookie);
2815 ok_ole_success(hr, IGlobalInterfaceTable_RegisterInterfaceInGlobal);
2817 ok_more_than_one_lock();
2819 params.cookie = cookie;
2820 params.git = git;
2821 /* note: params is on stack so we MUST wait for get_global_interface_proc
2822 * to exit before we can return */
2823 thread = CreateThread(NULL, 0, get_global_interface_proc, &params, 0, &tid);
2825 ret = MsgWaitForMultipleObjects(1, &thread, FALSE, 10000, QS_ALLINPUT);
2826 while (ret == WAIT_OBJECT_0 + 1)
2828 MSG msg;
2829 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
2830 DispatchMessage(&msg);
2831 ret = MsgWaitForMultipleObjects(1, &thread, FALSE, 10000, QS_ALLINPUT);
2834 CloseHandle(thread);
2836 /* test getting interface from global with different iid */
2837 hr = IGlobalInterfaceTable_GetInterfaceFromGlobal(git, cookie, &IID_IUnknown, (void **)&object);
2838 ok_ole_success(hr, IGlobalInterfaceTable_GetInterfaceFromGlobal);
2839 IUnknown_Release(object);
2841 /* test getting interface from global with same iid */
2842 hr = IGlobalInterfaceTable_GetInterfaceFromGlobal(git, cookie, &IID_IClassFactory, (void **)&object);
2843 ok_ole_success(hr, IGlobalInterfaceTable_GetInterfaceFromGlobal);
2844 IUnknown_Release(object);
2846 hr = IGlobalInterfaceTable_RevokeInterfaceFromGlobal(git, cookie);
2847 ok_ole_success(hr, IGlobalInterfaceTable_RevokeInterfaceFromGlobal);
2849 ok_no_locks();
2851 IGlobalInterfaceTable_Release(git);
2854 static void test_manualresetevent(void)
2856 ISynchronize *psync1, *psync2;
2857 IUnknown *punk;
2858 LONG ref;
2859 HRESULT hr;
2861 hr = CoCreateInstance(&CLSID_ManualResetEvent, NULL, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void**)&punk);
2862 ok(hr == S_OK, "Got 0x%08x\n", hr);
2863 ok(!!punk, "Got NULL.\n");
2864 IUnknown_Release(punk);
2866 hr = CoCreateInstance(&CLSID_ManualResetEvent, NULL, CLSCTX_INPROC_SERVER, &IID_ISynchronize, (void**)&psync1);
2867 ok(hr == S_OK, "Got 0x%08x\n", hr);
2868 ok(!!psync1, "Got NULL.\n");
2870 hr = ISynchronize_Wait(psync1, 0, 5);
2871 ok(hr == RPC_S_CALLPENDING, "Got 0x%08x\n", hr);
2873 hr = ISynchronize_Reset(psync1);
2874 ok(hr == S_OK, "Got 0x%08x\n", hr);
2875 hr = ISynchronize_Signal(psync1);
2876 ok(hr == S_OK, "Got 0x%08x\n", hr);
2877 hr = ISynchronize_Wait(psync1, 0, 5);
2878 ok(hr == S_OK, "Got 0x%08x\n", hr);
2879 hr = ISynchronize_Wait(psync1, 0, 5);
2880 ok(hr == S_OK, "Got 0x%08x\n", hr);
2881 hr = ISynchronize_Reset(psync1);
2882 ok(hr == S_OK, "Got 0x%08x\n", hr);
2883 hr = ISynchronize_Wait(psync1, 0, 5);
2884 ok(hr == RPC_S_CALLPENDING, "Got 0x%08x\n", hr);
2886 hr = CoCreateInstance(&CLSID_ManualResetEvent, NULL, CLSCTX_INPROC_SERVER, &IID_ISynchronize, (void**)&psync2);
2887 ok(hr == S_OK, "Got 0x%08x\n", hr);
2888 ok(!!psync2, "Got NULL.\n");
2889 ok(psync1 != psync2, "psync1 == psync2.\n");
2890 hr = ISynchronize_Wait(psync2, 0, 5);
2891 ok(hr == RPC_S_CALLPENDING, "Got 0x%08x\n", hr);
2893 hr = ISynchronize_Reset(psync1);
2894 ok(hr == S_OK, "Got 0x%08x\n", hr);
2895 hr = ISynchronize_Reset(psync2);
2896 ok(hr == S_OK, "Got 0x%08x\n", hr);
2897 hr = ISynchronize_Signal(psync1);
2898 ok(hr == S_OK, "Got 0x%08x\n", hr);
2899 hr = ISynchronize_Wait(psync2, 0, 5);
2900 ok(hr == RPC_S_CALLPENDING, "Got 0x%08x\n", hr);
2902 ref = ISynchronize_AddRef(psync1);
2903 ok(ref == 2, "Got ref: %d\n", ref);
2904 ref = ISynchronize_AddRef(psync1);
2905 ok(ref == 3, "Got ref: %d\n", ref);
2906 ref = ISynchronize_Release(psync1);
2907 ok(ref == 2, "Got nonzero ref: %d\n", ref);
2908 ref = ISynchronize_Release(psync2);
2909 ok(!ref, "Got nonzero ref: %d\n", ref);
2910 ref = ISynchronize_Release(psync1);
2911 ok(ref == 1, "Got nonzero ref: %d\n", ref);
2912 ref = ISynchronize_Release(psync1);
2913 ok(!ref, "Got nonzero ref: %d\n", ref);
2916 static const char *debugstr_iid(REFIID riid)
2918 static char name[256];
2919 HKEY hkeyInterface;
2920 WCHAR bufferW[39];
2921 char buffer[39];
2922 LONG name_size = sizeof(name);
2923 StringFromGUID2(riid, bufferW, sizeof(bufferW)/sizeof(bufferW[0]));
2924 WideCharToMultiByte(CP_ACP, 0, bufferW, sizeof(bufferW)/sizeof(bufferW[0]), buffer, sizeof(buffer), NULL, NULL);
2925 if (RegOpenKeyEx(HKEY_CLASSES_ROOT, "Interface", 0, KEY_QUERY_VALUE, &hkeyInterface) != ERROR_SUCCESS)
2927 memcpy(name, buffer, sizeof(buffer));
2928 goto done;
2930 if (RegQueryValue(hkeyInterface, buffer, name, &name_size) != ERROR_SUCCESS)
2932 memcpy(name, buffer, sizeof(buffer));
2933 goto done;
2935 RegCloseKey(hkeyInterface);
2936 done:
2937 return name;
2940 static HRESULT WINAPI TestChannelHook_QueryInterface(IChannelHook *iface, REFIID riid, void **ppv)
2942 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IChannelHook))
2944 *ppv = iface;
2945 IUnknown_AddRef(iface);
2946 return S_OK;
2949 *ppv = NULL;
2950 return E_NOINTERFACE;
2953 static ULONG WINAPI TestChannelHook_AddRef(IChannelHook *iface)
2955 return 2;
2958 static ULONG WINAPI TestChannelHook_Release(IChannelHook *iface)
2960 return 1;
2963 static void WINAPI TestChannelHook_ClientGetSize(
2964 IChannelHook *iface,
2965 REFGUID uExtent,
2966 REFIID riid,
2967 ULONG *pDataSize )
2969 SChannelHookCallInfo *info = (SChannelHookCallInfo *)riid;
2970 trace("TestChannelHook_ClientGetBuffer\n");
2971 trace("\t%s method %d\n", debugstr_iid(riid), info->iMethod);
2972 trace("\tcid: %s\n", debugstr_iid(&info->uCausality));
2973 ok(info->cbSize == sizeof(*info), "info->cbSize was %d instead of %d\n", info->cbSize, (int)sizeof(*info));
2974 ok(info->dwServerPid == GetCurrentProcessId(), "info->dwServerPid was 0x%x instead of 0x%x\n", info->dwServerPid, GetCurrentProcessId());
2975 ok(!info->pObject, "info->pObject should be NULL\n");
2976 ok(IsEqualGUID(uExtent, &EXTENTID_WineTest), "uExtent wasn't correct\n");
2978 *pDataSize = 1;
2981 static void WINAPI TestChannelHook_ClientFillBuffer(
2982 IChannelHook *iface,
2983 REFGUID uExtent,
2984 REFIID riid,
2985 ULONG *pDataSize,
2986 void *pDataBuffer )
2988 SChannelHookCallInfo *info = (SChannelHookCallInfo *)riid;
2989 trace("TestChannelHook_ClientFillBuffer\n");
2990 ok(info->cbSize == sizeof(*info), "info->cbSize was %d instead of %d\n", info->cbSize, (int)sizeof(*info));
2991 ok(info->dwServerPid == GetCurrentProcessId(), "info->dwServerPid was 0x%x instead of 0x%x\n", info->dwServerPid, GetCurrentProcessId());
2992 ok(!info->pObject, "info->pObject should be NULL\n");
2993 ok(IsEqualGUID(uExtent, &EXTENTID_WineTest), "uExtent wasn't correct\n");
2995 *(unsigned char *)pDataBuffer = 0xcc;
2996 *pDataSize = 1;
2999 static void WINAPI TestChannelHook_ClientNotify(
3000 IChannelHook *iface,
3001 REFGUID uExtent,
3002 REFIID riid,
3003 ULONG cbDataSize,
3004 void *pDataBuffer,
3005 DWORD lDataRep,
3006 HRESULT hrFault )
3008 SChannelHookCallInfo *info = (SChannelHookCallInfo *)riid;
3009 trace("TestChannelHook_ClientNotify hrFault = 0x%08x\n", hrFault);
3010 ok(info->cbSize == sizeof(*info), "info->cbSize was %d instead of %d\n", info->cbSize, (int)sizeof(*info));
3011 ok(info->dwServerPid == GetCurrentProcessId(), "info->dwServerPid was 0x%x instead of 0x%x\n", info->dwServerPid, GetCurrentProcessId());
3012 todo_wine {
3013 ok(info->pObject != NULL, "info->pObject shouldn't be NULL\n");
3015 ok(IsEqualGUID(uExtent, &EXTENTID_WineTest), "uExtent wasn't correct\n");
3018 static void WINAPI TestChannelHook_ServerNotify(
3019 IChannelHook *iface,
3020 REFGUID uExtent,
3021 REFIID riid,
3022 ULONG cbDataSize,
3023 void *pDataBuffer,
3024 DWORD lDataRep )
3026 SChannelHookCallInfo *info = (SChannelHookCallInfo *)riid;
3027 trace("TestChannelHook_ServerNotify\n");
3028 ok(info->cbSize == sizeof(*info), "info->cbSize was %d instead of %d\n", info->cbSize, (int)sizeof(*info));
3029 ok(info->dwServerPid == GetCurrentProcessId(), "info->dwServerPid was 0x%x instead of 0x%x\n", info->dwServerPid, GetCurrentProcessId());
3030 ok(info->pObject != NULL, "info->pObject shouldn't be NULL\n");
3031 ok(cbDataSize == 1, "cbDataSize should have been 1 instead of %d\n", cbDataSize);
3032 ok(*(unsigned char *)pDataBuffer == 0xcc, "pDataBuffer should have contained 0xcc instead of 0x%x\n", *(unsigned char *)pDataBuffer);
3033 ok(IsEqualGUID(uExtent, &EXTENTID_WineTest), "uExtent wasn't correct\n");
3036 static void WINAPI TestChannelHook_ServerGetSize(
3037 IChannelHook *iface,
3038 REFGUID uExtent,
3039 REFIID riid,
3040 HRESULT hrFault,
3041 ULONG *pDataSize )
3043 SChannelHookCallInfo *info = (SChannelHookCallInfo *)riid;
3044 trace("TestChannelHook_ServerGetSize\n");
3045 trace("\t%s method %d\n", debugstr_iid(riid), info->iMethod);
3046 ok(info->cbSize == sizeof(*info), "info->cbSize was %d instead of %d\n", info->cbSize, (int)sizeof(*info));
3047 ok(info->dwServerPid == GetCurrentProcessId(), "info->dwServerPid was 0x%x instead of 0x%x\n", info->dwServerPid, GetCurrentProcessId());
3048 ok(info->pObject != NULL, "info->pObject shouldn't be NULL\n");
3049 ok(IsEqualGUID(uExtent, &EXTENTID_WineTest), "uExtent wasn't correct\n");
3050 if (hrFault != S_OK)
3051 trace("\thrFault = 0x%08x\n", hrFault);
3053 *pDataSize = 0;
3056 static void WINAPI TestChannelHook_ServerFillBuffer(
3057 IChannelHook *iface,
3058 REFGUID uExtent,
3059 REFIID riid,
3060 ULONG *pDataSize,
3061 void *pDataBuffer,
3062 HRESULT hrFault )
3064 trace("TestChannelHook_ServerFillBuffer\n");
3065 ok(0, "TestChannelHook_ServerFillBuffer shouldn't be called\n");
3068 static const IChannelHookVtbl TestChannelHookVtbl =
3070 TestChannelHook_QueryInterface,
3071 TestChannelHook_AddRef,
3072 TestChannelHook_Release,
3073 TestChannelHook_ClientGetSize,
3074 TestChannelHook_ClientFillBuffer,
3075 TestChannelHook_ClientNotify,
3076 TestChannelHook_ServerNotify,
3077 TestChannelHook_ServerGetSize,
3078 TestChannelHook_ServerFillBuffer,
3081 static IChannelHook TestChannelHook = { &TestChannelHookVtbl };
3083 static void test_channel_hook(void)
3085 IStream *pStream = NULL;
3086 IClassFactory *cf = NULL;
3087 DWORD tid;
3088 IUnknown *proxy = NULL;
3089 HANDLE thread;
3090 HRESULT hr;
3092 hr = CoRegisterChannelHook(&EXTENTID_WineTest, &TestChannelHook);
3093 ok_ole_success(hr, CoRegisterChannelHook);
3095 hr = CoRegisterMessageFilter(&MessageFilter, NULL);
3096 ok_ole_success(hr, CoRegisterMessageFilter);
3098 cLocks = 0;
3100 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
3101 ok_ole_success(hr, CreateStreamOnHGlobal);
3102 tid = start_host_object2(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &MessageFilter, &thread);
3104 ok_more_than_one_lock();
3106 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
3107 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&cf);
3108 ok_ole_success(hr, CoUnmarshalInterface);
3109 IStream_Release(pStream);
3111 ok_more_than_one_lock();
3113 hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
3114 ok_ole_success(hr, IClassFactory_CreateInstance);
3115 IUnknown_Release(proxy);
3117 IClassFactory_Release(cf);
3119 ok_no_locks();
3121 end_host_object(tid, thread);
3123 hr = CoRegisterMessageFilter(NULL, NULL);
3124 ok_ole_success(hr, CoRegisterMessageFilter);
3127 START_TEST(marshal)
3129 HMODULE hOle32 = GetModuleHandle("ole32");
3130 int argc;
3131 char **argv;
3133 if (!GetProcAddress(hOle32, "CoRegisterSurrogateEx")) {
3134 win_skip("skipping test on win9x\n");
3135 return;
3138 pCoInitializeEx = (void*)GetProcAddress(hOle32, "CoInitializeEx");
3140 argc = winetest_get_mainargs( &argv );
3141 if (argc > 2 && (!strcmp(argv[2], "-Embedding")))
3143 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
3144 test_register_local_server();
3145 CoUninitialize();
3147 return;
3150 register_test_window();
3152 test_cocreateinstance_proxy();
3154 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
3156 /* FIXME: test CoCreateInstanceEx */
3158 /* lifecycle management and marshaling tests */
3159 test_no_marshaler();
3160 test_normal_marshal_and_release();
3161 test_normal_marshal_and_unmarshal();
3162 test_marshal_and_unmarshal_invalid();
3163 test_same_apartment_unmarshal_failure();
3164 test_interthread_marshal_and_unmarshal();
3165 test_proxy_marshal_and_unmarshal();
3166 test_proxy_marshal_and_unmarshal2();
3167 test_proxy_marshal_and_unmarshal_weak();
3168 test_proxy_marshal_and_unmarshal_strong();
3169 test_marshal_stub_apartment_shutdown();
3170 test_marshal_proxy_apartment_shutdown();
3171 test_marshal_proxy_mta_apartment_shutdown();
3172 test_no_couninitialize_server();
3173 test_no_couninitialize_client();
3174 test_tableweak_marshal_and_unmarshal_twice();
3175 test_tableweak_marshal_releasedata1();
3176 test_tableweak_marshal_releasedata2();
3177 test_tableweak_and_normal_marshal_and_unmarshal();
3178 test_tablestrong_marshal_and_unmarshal_twice();
3179 test_lock_object_external();
3180 test_disconnect_stub();
3181 test_normal_marshal_and_unmarshal_twice();
3182 test_hresult_marshaling();
3183 test_proxy_used_in_wrong_thread();
3184 test_message_filter();
3185 test_bad_marshal_stream();
3186 test_proxy_interfaces();
3187 test_stubbuffer(&IID_IClassFactory);
3188 test_proxybuffer(&IID_IClassFactory);
3189 test_message_reentrancy();
3190 test_call_from_message();
3191 test_WM_QUIT_handling();
3192 test_freethreadedmarshaler();
3193 test_inproc_handler();
3194 test_handler_marshaling();
3195 test_client_security();
3197 test_local_server();
3199 test_globalinterfacetable();
3200 test_manualresetevent();
3202 /* must be last test as channel hooks can't be unregistered */
3203 test_channel_hook();
3205 CoUninitialize();