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