push 88671f85dcf7a7cd89c63d6e9f34ad6b6ad2ae64
[wine/hacks.git] / dlls / ole32 / tests / marshal.c
blobc7f79961bcfe73c91cb78f0e4975a0e80fc432cc
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"
33 #include "wine/test.h"
35 /* functions that are not present on all versions of Windows */
36 HRESULT (WINAPI * pCoInitializeEx)(LPVOID lpReserved, DWORD dwCoInit);
38 /* helper macros to make tests a bit leaner */
39 #define ok_more_than_one_lock() ok(cLocks > 0, "Number of locks should be > 0, but actually is %d\n", cLocks)
40 #define ok_no_locks() ok(cLocks == 0, "Number of locks should be 0, but actually is %d\n", cLocks)
41 #define ok_ole_success(hr, func) ok(hr == S_OK, #func " failed with error 0x%08x\n", hr)
43 static const IID IID_IWineTest =
45 0x5201163f,
46 0x8164,
47 0x4fd0,
48 {0xa1, 0xa2, 0x5d, 0x5a, 0x36, 0x54, 0xd3, 0xbd}
49 }; /* 5201163f-8164-4fd0-a1a2-5d5a3654d3bd */
51 static const IID IID_IRemUnknown =
53 0x00000131,
54 0x0000,
55 0x0000,
56 {0xc0,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46}
59 #define EXTENTID_WineTest IID_IWineTest
60 #define CLSID_WineTest IID_IWineTest
62 static const CLSID CLSID_WineOOPTest =
64 0x5201163f,
65 0x8164,
66 0x4fd0,
67 {0xa1, 0xa2, 0x5d, 0x5a, 0x36, 0x54, 0xd3, 0xbd}
68 }; /* 5201163f-8164-4fd0-a1a2-5d5a3654d3bd */
70 static void test_cocreateinstance_proxy(void)
72 IUnknown *pProxy;
73 IMultiQI *pMQI;
74 HRESULT hr;
76 pCoInitializeEx(NULL, COINIT_MULTITHREADED);
78 hr = CoCreateInstance(&CLSID_ShellDesktop, NULL, CLSCTX_INPROC, &IID_IUnknown, (void **)&pProxy);
79 ok_ole_success(hr, CoCreateInstance);
80 hr = IUnknown_QueryInterface(pProxy, &IID_IMultiQI, (void **)&pMQI);
81 ok(hr == S_OK, "created object is not a proxy, so was created in the wrong apartment\n");
82 if (hr == S_OK)
83 IMultiQI_Release(pMQI);
84 IUnknown_Release(pProxy);
86 CoUninitialize();
89 static const LARGE_INTEGER ullZero;
90 static LONG cLocks;
92 static void LockModule(void)
94 InterlockedIncrement(&cLocks);
97 static void UnlockModule(void)
99 InterlockedDecrement(&cLocks);
103 static HRESULT WINAPI Test_IUnknown_QueryInterface(
104 LPUNKNOWN iface,
105 REFIID riid,
106 LPVOID *ppvObj)
108 if (ppvObj == NULL) return E_POINTER;
110 if (IsEqualGUID(riid, &IID_IUnknown))
112 *ppvObj = (LPVOID)iface;
113 IUnknown_AddRef(iface);
114 return S_OK;
117 *ppvObj = NULL;
118 return E_NOINTERFACE;
121 static ULONG WINAPI Test_IUnknown_AddRef(LPUNKNOWN iface)
123 LockModule();
124 return 2; /* non-heap-based object */
127 static ULONG WINAPI Test_IUnknown_Release(LPUNKNOWN iface)
129 UnlockModule();
130 return 1; /* non-heap-based object */
133 static const IUnknownVtbl TestUnknown_Vtbl =
135 Test_IUnknown_QueryInterface,
136 Test_IUnknown_AddRef,
137 Test_IUnknown_Release,
140 static IUnknown Test_Unknown = { &TestUnknown_Vtbl };
143 static HRESULT WINAPI Test_IClassFactory_QueryInterface(
144 LPCLASSFACTORY iface,
145 REFIID riid,
146 LPVOID *ppvObj)
148 if (ppvObj == NULL) return E_POINTER;
150 if (IsEqualGUID(riid, &IID_IUnknown) ||
151 IsEqualGUID(riid, &IID_IClassFactory) ||
152 /* the only other interface Wine is currently able to marshal (for testing two proxies) */
153 IsEqualGUID(riid, &IID_IRemUnknown))
155 *ppvObj = (LPVOID)iface;
156 IClassFactory_AddRef(iface);
157 return S_OK;
160 *ppvObj = NULL;
161 return E_NOINTERFACE;
164 static ULONG WINAPI Test_IClassFactory_AddRef(LPCLASSFACTORY iface)
166 LockModule();
167 return 2; /* non-heap-based object */
170 static ULONG WINAPI Test_IClassFactory_Release(LPCLASSFACTORY iface)
172 UnlockModule();
173 return 1; /* non-heap-based object */
176 static HRESULT WINAPI Test_IClassFactory_CreateInstance(
177 LPCLASSFACTORY iface,
178 LPUNKNOWN pUnkOuter,
179 REFIID riid,
180 LPVOID *ppvObj)
182 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
183 return IUnknown_QueryInterface((IUnknown*)&Test_Unknown, riid, ppvObj);
186 static HRESULT WINAPI Test_IClassFactory_LockServer(
187 LPCLASSFACTORY iface,
188 BOOL fLock)
190 return S_OK;
193 static const IClassFactoryVtbl TestClassFactory_Vtbl =
195 Test_IClassFactory_QueryInterface,
196 Test_IClassFactory_AddRef,
197 Test_IClassFactory_Release,
198 Test_IClassFactory_CreateInstance,
199 Test_IClassFactory_LockServer
202 static IClassFactory Test_ClassFactory = { &TestClassFactory_Vtbl };
204 #define RELEASEMARSHALDATA WM_USER
206 struct host_object_data
208 IStream *stream;
209 IID iid;
210 IUnknown *object;
211 MSHLFLAGS marshal_flags;
212 HANDLE marshal_event;
213 IMessageFilter *filter;
216 static DWORD CALLBACK host_object_proc(LPVOID p)
218 struct host_object_data *data = (struct host_object_data *)p;
219 HRESULT hr;
220 MSG msg;
222 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
224 if (data->filter)
226 IMessageFilter * prev_filter = NULL;
227 hr = CoRegisterMessageFilter(data->filter, &prev_filter);
228 if (prev_filter) IMessageFilter_Release(prev_filter);
229 ok_ole_success(hr, CoRegisterMessageFilter);
232 hr = CoMarshalInterface(data->stream, &data->iid, data->object, MSHCTX_INPROC, NULL, data->marshal_flags);
233 ok_ole_success(hr, CoMarshalInterface);
235 /* force the message queue to be created before signaling parent thread */
236 PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
238 SetEvent(data->marshal_event);
240 while (GetMessage(&msg, NULL, 0, 0))
242 if (msg.hwnd == NULL && msg.message == RELEASEMARSHALDATA)
244 CoReleaseMarshalData(data->stream);
245 SetEvent((HANDLE)msg.lParam);
247 else
248 DispatchMessage(&msg);
251 HeapFree(GetProcessHeap(), 0, data);
253 CoUninitialize();
255 return hr;
258 static DWORD start_host_object2(IStream *stream, REFIID riid, IUnknown *object, MSHLFLAGS marshal_flags, IMessageFilter *filter, HANDLE *thread)
260 DWORD tid = 0;
261 HANDLE marshal_event = CreateEvent(NULL, FALSE, FALSE, NULL);
262 struct host_object_data *data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data));
264 data->stream = stream;
265 data->iid = *riid;
266 data->object = object;
267 data->marshal_flags = marshal_flags;
268 data->marshal_event = marshal_event;
269 data->filter = filter;
271 *thread = CreateThread(NULL, 0, host_object_proc, data, 0, &tid);
273 /* wait for marshaling to complete before returning */
274 WaitForSingleObject(marshal_event, INFINITE);
275 CloseHandle(marshal_event);
277 return tid;
280 static DWORD start_host_object(IStream *stream, REFIID riid, IUnknown *object, MSHLFLAGS marshal_flags, HANDLE *thread)
282 return start_host_object2(stream, riid, object, marshal_flags, NULL, thread);
285 /* asks thread to release the marshal data because it has to be done by the
286 * same thread that marshaled the interface in the first place. */
287 static void release_host_object(DWORD tid)
289 HANDLE event = CreateEvent(NULL, FALSE, FALSE, NULL);
290 PostThreadMessage(tid, RELEASEMARSHALDATA, 0, (LPARAM)event);
291 WaitForSingleObject(event, INFINITE);
292 CloseHandle(event);
295 static void end_host_object(DWORD tid, HANDLE thread)
297 BOOL ret = PostThreadMessage(tid, WM_QUIT, 0, 0);
298 ok(ret, "PostThreadMessage failed with error %d\n", GetLastError());
299 /* be careful of races - don't return until hosting thread has terminated */
300 WaitForSingleObject(thread, INFINITE);
301 CloseHandle(thread);
304 /* tests failure case of interface not having a marshaler specified in the
305 * registry */
306 static void test_no_marshaler(void)
308 IStream *pStream;
309 HRESULT hr;
311 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
312 ok_ole_success(hr, CreateStreamOnHGlobal);
313 hr = CoMarshalInterface(pStream, &IID_IWineTest, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
314 ok(hr == E_NOINTERFACE, "CoMarshalInterface should have returned E_NOINTERFACE instead of 0x%08x\n", hr);
316 IStream_Release(pStream);
319 /* tests normal marshal and then release without unmarshaling */
320 static void test_normal_marshal_and_release(void)
322 HRESULT hr;
323 IStream *pStream = NULL;
325 cLocks = 0;
327 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
328 ok_ole_success(hr, CreateStreamOnHGlobal);
329 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
330 ok_ole_success(hr, CoMarshalInterface);
332 ok_more_than_one_lock();
334 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
335 hr = CoReleaseMarshalData(pStream);
336 ok_ole_success(hr, CoReleaseMarshalData);
337 IStream_Release(pStream);
339 ok_no_locks();
342 /* tests success case of a same-thread marshal and unmarshal */
343 static void test_normal_marshal_and_unmarshal(void)
345 HRESULT hr;
346 IStream *pStream = NULL;
347 IUnknown *pProxy = NULL;
349 cLocks = 0;
351 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
352 ok_ole_success(hr, CreateStreamOnHGlobal);
353 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
354 ok_ole_success(hr, CoMarshalInterface);
356 ok_more_than_one_lock();
358 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
359 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
360 ok_ole_success(hr, CoUnmarshalInterface);
361 IStream_Release(pStream);
363 ok_more_than_one_lock();
365 IUnknown_Release(pProxy);
367 ok_no_locks();
370 /* tests failure case of unmarshaling a freed object */
371 static void test_marshal_and_unmarshal_invalid(void)
373 HRESULT hr;
374 IStream *pStream = NULL;
375 IClassFactory *pProxy = NULL;
376 DWORD tid;
377 void * dummy;
378 HANDLE thread;
380 cLocks = 0;
382 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
383 ok_ole_success(hr, CreateStreamOnHGlobal);
384 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
386 ok_more_than_one_lock();
388 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
389 hr = CoReleaseMarshalData(pStream);
390 ok_ole_success(hr, CoReleaseMarshalData);
392 ok_no_locks();
394 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
395 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
396 todo_wine { ok_ole_success(hr, CoUnmarshalInterface); }
398 ok_no_locks();
400 if (pProxy)
402 hr = IClassFactory_CreateInstance(pProxy, NULL, &IID_IUnknown, &dummy);
403 ok(hr == RPC_E_DISCONNECTED, "Remote call should have returned RPC_E_DISCONNECTED, instead of 0x%08x\n", hr);
405 IClassFactory_Release(pProxy);
408 IStream_Release(pStream);
410 end_host_object(tid, thread);
413 static void test_same_apartment_unmarshal_failure(void)
415 HRESULT hr;
416 IStream *pStream;
417 IUnknown *pProxy;
418 static const LARGE_INTEGER llZero;
420 cLocks = 0;
422 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
423 ok_ole_success(hr, CreateStreamOnHGlobal);
425 hr = CoMarshalInterface(pStream, &IID_IUnknown, (IUnknown *)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
426 ok_ole_success(hr, CoMarshalInterface);
428 ok_more_than_one_lock();
430 hr = IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
431 ok_ole_success(hr, IStream_Seek);
433 hr = CoUnmarshalInterface(pStream, &IID_IParseDisplayName, (void **)&pProxy);
434 ok(hr == E_NOINTERFACE, "CoUnmarshalInterface should have returned E_NOINTERFACE instead of 0x%08x\n", hr);
436 ok_no_locks();
438 IStream_Release(pStream);
441 /* tests success case of an interthread marshal */
442 static void test_interthread_marshal_and_unmarshal(void)
444 HRESULT hr;
445 IStream *pStream = NULL;
446 IUnknown *pProxy = NULL;
447 DWORD tid;
448 HANDLE thread;
450 cLocks = 0;
452 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
453 ok_ole_success(hr, CreateStreamOnHGlobal);
454 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
456 ok_more_than_one_lock();
458 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
459 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
460 ok_ole_success(hr, CoUnmarshalInterface);
461 IStream_Release(pStream);
463 ok_more_than_one_lock();
465 IUnknown_Release(pProxy);
467 ok_no_locks();
469 end_host_object(tid, thread);
472 /* the number of external references that Wine's proxy manager normally gives
473 * out, so we can test the border case of running out of references */
474 #define NORMALEXTREFS 5
476 /* tests success case of an interthread marshal and then marshaling the proxy */
477 static void test_proxy_marshal_and_unmarshal(void)
479 HRESULT hr;
480 IStream *pStream = NULL;
481 IUnknown *pProxy = NULL;
482 IUnknown *pProxy2 = NULL;
483 DWORD tid;
484 HANDLE thread;
485 int i;
487 cLocks = 0;
489 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
490 ok_ole_success(hr, CreateStreamOnHGlobal);
491 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
493 ok_more_than_one_lock();
495 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
496 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
497 ok_ole_success(hr, CoUnmarshalInterface);
499 ok_more_than_one_lock();
501 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
502 /* marshal the proxy */
503 hr = CoMarshalInterface(pStream, &IID_IClassFactory, pProxy, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
504 ok_ole_success(hr, CoMarshalInterface);
506 ok_more_than_one_lock();
508 /* marshal 5 more times to exhaust the normal external references of 5 */
509 for (i = 0; i < NORMALEXTREFS; i++)
511 hr = CoMarshalInterface(pStream, &IID_IClassFactory, pProxy, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
512 ok_ole_success(hr, CoMarshalInterface);
515 ok_more_than_one_lock();
517 /* release the original proxy to test that we successfully keep the
518 * original object alive */
519 IUnknown_Release(pProxy);
521 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
522 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
523 ok_ole_success(hr, CoUnmarshalInterface);
525 ok_more_than_one_lock();
527 IUnknown_Release(pProxy2);
529 /* unmarshal all of the proxies to check that the object stub still exists */
530 for (i = 0; i < NORMALEXTREFS; i++)
532 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
533 ok_ole_success(hr, CoUnmarshalInterface);
535 IUnknown_Release(pProxy2);
538 ok_no_locks();
540 IStream_Release(pStream);
542 end_host_object(tid, thread);
545 /* tests success case of an interthread marshal and then marshaling the proxy
546 * using an iid that hasn't previously been unmarshaled */
547 static void test_proxy_marshal_and_unmarshal2(void)
549 HRESULT hr;
550 IStream *pStream = NULL;
551 IUnknown *pProxy = NULL;
552 IUnknown *pProxy2 = NULL;
553 DWORD tid;
554 HANDLE thread;
556 cLocks = 0;
558 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
559 ok_ole_success(hr, CreateStreamOnHGlobal);
560 tid = start_host_object(pStream, &IID_IUnknown, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
562 ok_more_than_one_lock();
564 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
565 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)&pProxy);
566 ok_ole_success(hr, CoUnmarshalInterface);
568 ok_more_than_one_lock();
570 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
571 /* marshal the proxy */
572 hr = CoMarshalInterface(pStream, &IID_IClassFactory, pProxy, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
573 ok_ole_success(hr, CoMarshalInterface);
575 ok_more_than_one_lock();
577 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
578 /* unmarshal the second proxy to the object */
579 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
580 ok_ole_success(hr, CoUnmarshalInterface);
581 IStream_Release(pStream);
583 /* now the proxies should be as follows:
584 * pProxy -> &Test_ClassFactory
585 * pProxy2 -> &Test_ClassFactory
586 * they should NOT be as follows:
587 * pProxy -> &Test_ClassFactory
588 * pProxy2 -> pProxy
589 * the above can only really be tested by looking in +ole traces
592 ok_more_than_one_lock();
594 IUnknown_Release(pProxy);
596 ok_more_than_one_lock();
598 IUnknown_Release(pProxy2);
600 ok_no_locks();
602 end_host_object(tid, thread);
605 /* tests success case of an interthread marshal and then table-weak-marshaling the proxy */
606 static void test_proxy_marshal_and_unmarshal_weak(void)
608 HRESULT hr;
609 IStream *pStream = NULL;
610 IUnknown *pProxy = NULL;
611 IUnknown *pProxy2 = NULL;
612 DWORD tid;
613 HANDLE thread;
615 cLocks = 0;
617 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
618 ok_ole_success(hr, CreateStreamOnHGlobal);
619 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
621 ok_more_than_one_lock();
623 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
624 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
625 ok_ole_success(hr, CoUnmarshalInterface);
627 ok_more_than_one_lock();
629 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
630 /* marshal the proxy */
631 hr = CoMarshalInterface(pStream, &IID_IClassFactory, pProxy, MSHCTX_INPROC, NULL, MSHLFLAGS_TABLEWEAK);
632 ok_ole_success(hr, CoMarshalInterface);
634 ok_more_than_one_lock();
636 /* release the original proxy to test that we successfully keep the
637 * original object alive */
638 IUnknown_Release(pProxy);
640 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
641 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
642 todo_wine
643 ok(hr == CO_E_OBJNOTREG, "CoUnmarshalInterface should return CO_E_OBJNOTREG instead of 0x%08x\n", hr);
645 ok_no_locks();
647 IStream_Release(pStream);
649 end_host_object(tid, thread);
652 /* tests success case of an interthread marshal and then table-strong-marshaling the proxy */
653 static void test_proxy_marshal_and_unmarshal_strong(void)
655 HRESULT hr;
656 IStream *pStream = NULL;
657 IUnknown *pProxy = NULL;
658 IUnknown *pProxy2 = NULL;
659 DWORD tid;
660 HANDLE thread;
662 cLocks = 0;
664 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
665 ok_ole_success(hr, CreateStreamOnHGlobal);
666 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
668 ok_more_than_one_lock();
670 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
671 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
672 ok_ole_success(hr, CoUnmarshalInterface);
674 ok_more_than_one_lock();
676 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
677 /* marshal the proxy */
678 hr = CoMarshalInterface(pStream, &IID_IClassFactory, pProxy, MSHCTX_INPROC, NULL, MSHLFLAGS_TABLESTRONG);
679 ok(hr == S_OK /* WinNT */ || hr == E_INVALIDARG /* Win9x */,
680 "CoMarshalInterface should have return S_OK or E_INVALIDARG instead of 0x%08x\n", hr);
681 if (FAILED(hr))
683 IUnknown_Release(pProxy);
684 goto end;
687 ok_more_than_one_lock();
689 /* release the original proxy to test that we successfully keep the
690 * original object alive */
691 IUnknown_Release(pProxy);
693 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
694 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
695 ok_ole_success(hr, CoUnmarshalInterface);
697 ok_more_than_one_lock();
699 IUnknown_Release(pProxy2);
701 ok_more_than_one_lock();
703 end:
704 IStream_Release(pStream);
706 end_host_object(tid, thread);
708 ok_no_locks();
711 /* tests that stubs are released when the containing apartment is destroyed */
712 static void test_marshal_stub_apartment_shutdown(void)
714 HRESULT hr;
715 IStream *pStream = NULL;
716 IUnknown *pProxy = NULL;
717 DWORD tid;
718 HANDLE thread;
720 cLocks = 0;
722 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
723 ok_ole_success(hr, CreateStreamOnHGlobal);
724 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
726 ok_more_than_one_lock();
728 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
729 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
730 ok_ole_success(hr, CoUnmarshalInterface);
731 IStream_Release(pStream);
733 ok_more_than_one_lock();
735 end_host_object(tid, thread);
737 ok_no_locks();
739 IUnknown_Release(pProxy);
741 ok_no_locks();
744 /* tests that proxies are released when the containing apartment is destroyed */
745 static void test_marshal_proxy_apartment_shutdown(void)
747 HRESULT hr;
748 IStream *pStream = NULL;
749 IUnknown *pProxy = NULL;
750 DWORD tid;
751 HANDLE thread;
753 cLocks = 0;
755 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
756 ok_ole_success(hr, CreateStreamOnHGlobal);
757 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
759 ok_more_than_one_lock();
761 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
762 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
763 ok_ole_success(hr, CoUnmarshalInterface);
764 IStream_Release(pStream);
766 ok_more_than_one_lock();
768 CoUninitialize();
770 ok_no_locks();
772 IUnknown_Release(pProxy);
774 ok_no_locks();
776 end_host_object(tid, thread);
778 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
781 /* tests that proxies are released when the containing mta apartment is destroyed */
782 static void test_marshal_proxy_mta_apartment_shutdown(void)
784 HRESULT hr;
785 IStream *pStream = NULL;
786 IUnknown *pProxy = NULL;
787 DWORD tid;
788 HANDLE thread;
790 CoUninitialize();
791 pCoInitializeEx(NULL, COINIT_MULTITHREADED);
793 cLocks = 0;
795 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
796 ok_ole_success(hr, CreateStreamOnHGlobal);
797 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
799 ok_more_than_one_lock();
801 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
802 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
803 ok_ole_success(hr, CoUnmarshalInterface);
804 IStream_Release(pStream);
806 ok_more_than_one_lock();
808 CoUninitialize();
810 ok_no_locks();
812 IUnknown_Release(pProxy);
814 ok_no_locks();
816 end_host_object(tid, thread);
818 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
821 struct ncu_params
823 LPSTREAM stream;
824 HANDLE marshal_event;
825 HANDLE unmarshal_event;
828 /* helper for test_no_couninitialize_server */
829 static DWORD CALLBACK no_couninitialize_server_proc(LPVOID p)
831 struct ncu_params *ncu_params = (struct ncu_params *)p;
832 HRESULT hr;
834 pCoInitializeEx(NULL, COINIT_MULTITHREADED);
836 hr = CoMarshalInterface(ncu_params->stream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
837 ok_ole_success(hr, CoMarshalInterface);
839 SetEvent(ncu_params->marshal_event);
841 WaitForSingleObject(ncu_params->unmarshal_event, INFINITE);
843 /* die without calling CoUninitialize */
845 return 0;
848 /* tests apartment that an apartment with a stub is released without deadlock
849 * if the owning thread exits */
850 static void test_no_couninitialize_server(void)
852 HRESULT hr;
853 IStream *pStream = NULL;
854 IUnknown *pProxy = NULL;
855 DWORD tid;
856 HANDLE thread;
857 struct ncu_params ncu_params;
859 cLocks = 0;
861 ncu_params.marshal_event = CreateEvent(NULL, TRUE, FALSE, NULL);
862 ncu_params.unmarshal_event = CreateEvent(NULL, TRUE, FALSE, NULL);
864 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
865 ok_ole_success(hr, CreateStreamOnHGlobal);
866 ncu_params.stream = pStream;
868 thread = CreateThread(NULL, 0, no_couninitialize_server_proc, &ncu_params, 0, &tid);
870 WaitForSingleObject(ncu_params.marshal_event, INFINITE);
871 ok_more_than_one_lock();
873 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
874 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
875 ok_ole_success(hr, CoUnmarshalInterface);
876 IStream_Release(pStream);
878 ok_more_than_one_lock();
880 SetEvent(ncu_params.unmarshal_event);
881 WaitForSingleObject(thread, INFINITE);
883 ok_no_locks();
885 CloseHandle(thread);
886 CloseHandle(ncu_params.marshal_event);
887 CloseHandle(ncu_params.unmarshal_event);
889 IUnknown_Release(pProxy);
891 ok_no_locks();
894 /* STA -> STA call during DLL_THREAD_DETACH */
895 static DWORD CALLBACK no_couninitialize_client_proc(LPVOID p)
897 struct ncu_params *ncu_params = (struct ncu_params *)p;
898 HRESULT hr;
899 IUnknown *pProxy = NULL;
901 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
903 hr = CoUnmarshalInterface(ncu_params->stream, &IID_IClassFactory, (void **)&pProxy);
904 ok_ole_success(hr, CoUnmarshalInterface);
905 IStream_Release(ncu_params->stream);
907 ok_more_than_one_lock();
909 /* die without calling CoUninitialize */
911 return 0;
914 /* tests STA -> STA call during DLL_THREAD_DETACH doesn't deadlock */
915 static void test_no_couninitialize_client(void)
917 HRESULT hr;
918 IStream *pStream = NULL;
919 DWORD tid;
920 DWORD host_tid;
921 HANDLE thread;
922 HANDLE host_thread;
923 struct ncu_params ncu_params;
925 cLocks = 0;
927 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
928 ok_ole_success(hr, CreateStreamOnHGlobal);
929 ncu_params.stream = pStream;
931 /* NOTE: assumes start_host_object uses an STA to host the object, as MTAs
932 * always deadlock when called from within DllMain */
933 host_tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown *)&Test_ClassFactory, MSHLFLAGS_NORMAL, &host_thread);
934 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
936 ok_more_than_one_lock();
938 thread = CreateThread(NULL, 0, no_couninitialize_client_proc, &ncu_params, 0, &tid);
940 WaitForSingleObject(thread, INFINITE);
941 CloseHandle(thread);
943 ok_no_locks();
945 end_host_object(host_tid, host_thread);
948 /* tests success case of a same-thread table-weak marshal, unmarshal, unmarshal */
949 static void test_tableweak_marshal_and_unmarshal_twice(void)
951 HRESULT hr;
952 IStream *pStream = NULL;
953 IUnknown *pProxy1 = NULL;
954 IUnknown *pProxy2 = NULL;
955 DWORD tid;
956 HANDLE thread;
958 cLocks = 0;
960 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
961 ok_ole_success(hr, CreateStreamOnHGlobal);
962 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_TABLEWEAK, &thread);
964 ok_more_than_one_lock();
966 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
967 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy1);
968 ok_ole_success(hr, CoUnmarshalInterface);
970 ok_more_than_one_lock();
972 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
973 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
974 IStream_Release(pStream);
975 ok_ole_success(hr, CoUnmarshalInterface);
977 ok_more_than_one_lock();
979 IUnknown_Release(pProxy1);
980 IUnknown_Release(pProxy2);
982 /* this line is shows the difference between weak and strong table marshaling:
983 * weak has cLocks == 0
984 * strong has cLocks > 0 */
985 ok_no_locks();
987 end_host_object(tid, thread);
990 /* tests releasing after unmarshaling one object */
991 static void test_tableweak_marshal_releasedata1(void)
993 HRESULT hr;
994 IStream *pStream = NULL;
995 IUnknown *pProxy1 = NULL;
996 IUnknown *pProxy2 = NULL;
997 DWORD tid;
998 HANDLE thread;
1000 cLocks = 0;
1002 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1003 ok_ole_success(hr, CreateStreamOnHGlobal);
1004 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_TABLEWEAK, &thread);
1006 ok_more_than_one_lock();
1008 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1009 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy1);
1010 ok_ole_success(hr, CoUnmarshalInterface);
1012 ok_more_than_one_lock();
1014 /* release the remaining reference on the object by calling
1015 * CoReleaseMarshalData in the hosting thread */
1016 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1017 release_host_object(tid);
1019 ok_more_than_one_lock();
1021 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1022 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
1023 ok_ole_success(hr, CoUnmarshalInterface);
1024 IStream_Release(pStream);
1026 ok_more_than_one_lock();
1028 IUnknown_Release(pProxy1);
1029 if (pProxy2)
1030 IUnknown_Release(pProxy2);
1032 /* this line is shows the difference between weak and strong table marshaling:
1033 * weak has cLocks == 0
1034 * strong has cLocks > 0 */
1035 ok_no_locks();
1037 end_host_object(tid, thread);
1040 /* tests releasing after unmarshaling one object */
1041 static void test_tableweak_marshal_releasedata2(void)
1043 HRESULT hr;
1044 IStream *pStream = NULL;
1045 IUnknown *pProxy = NULL;
1046 DWORD tid;
1047 HANDLE thread;
1049 cLocks = 0;
1051 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1052 ok_ole_success(hr, CreateStreamOnHGlobal);
1053 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_TABLEWEAK, &thread);
1055 ok_more_than_one_lock();
1057 /* release the remaining reference on the object by calling
1058 * CoReleaseMarshalData in the hosting thread */
1059 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1060 release_host_object(tid);
1062 ok_no_locks();
1064 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1065 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
1066 todo_wine
1068 ok(hr == CO_E_OBJNOTREG,
1069 "CoUnmarshalInterface should have failed with CO_E_OBJNOTREG, but returned 0x%08x instead\n",
1070 hr);
1072 IStream_Release(pStream);
1074 ok_no_locks();
1076 end_host_object(tid, thread);
1079 /* tests success case of a same-thread table-strong marshal, unmarshal, unmarshal */
1080 static void test_tablestrong_marshal_and_unmarshal_twice(void)
1082 HRESULT hr;
1083 IStream *pStream = NULL;
1084 IUnknown *pProxy1 = NULL;
1085 IUnknown *pProxy2 = NULL;
1086 DWORD tid;
1087 HANDLE thread;
1089 cLocks = 0;
1091 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1092 ok_ole_success(hr, CreateStreamOnHGlobal);
1093 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_TABLESTRONG, &thread);
1095 ok_more_than_one_lock();
1097 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1098 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy1);
1099 ok_ole_success(hr, CoUnmarshalInterface);
1101 ok_more_than_one_lock();
1103 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1104 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
1105 ok_ole_success(hr, CoUnmarshalInterface);
1107 ok_more_than_one_lock();
1109 if (pProxy1) IUnknown_Release(pProxy1);
1110 if (pProxy2) IUnknown_Release(pProxy2);
1112 /* this line is shows the difference between weak and strong table marshaling:
1113 * weak has cLocks == 0
1114 * strong has cLocks > 0 */
1115 ok_more_than_one_lock();
1117 /* release the remaining reference on the object by calling
1118 * CoReleaseMarshalData in the hosting thread */
1119 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1120 release_host_object(tid);
1121 IStream_Release(pStream);
1123 ok_no_locks();
1125 end_host_object(tid, thread);
1128 /* tests CoLockObjectExternal */
1129 static void test_lock_object_external(void)
1131 HRESULT hr;
1132 IStream *pStream = NULL;
1134 cLocks = 0;
1136 /* test the stub manager creation aspect of CoLockObjectExternal when the
1137 * object hasn't been marshaled yet */
1138 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, TRUE, TRUE);
1140 ok_more_than_one_lock();
1142 CoDisconnectObject((IUnknown*)&Test_ClassFactory, 0);
1144 ok_no_locks();
1146 /* test our empty stub manager being handled correctly in
1147 * CoMarshalInterface */
1148 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, TRUE, TRUE);
1150 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1151 ok_ole_success(hr, CreateStreamOnHGlobal);
1152 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1153 ok_ole_success(hr, CoMarshalInterface);
1155 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, TRUE, TRUE);
1157 ok_more_than_one_lock();
1159 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1160 hr = CoReleaseMarshalData(pStream);
1161 ok_ole_success(hr, CoReleaseMarshalData);
1162 IStream_Release(pStream);
1164 ok_more_than_one_lock();
1166 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, FALSE, TRUE);
1168 ok_more_than_one_lock();
1170 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, FALSE, TRUE);
1172 ok_no_locks();
1175 /* tests disconnecting stubs */
1176 static void test_disconnect_stub(void)
1178 HRESULT hr;
1179 IStream *pStream = NULL;
1181 cLocks = 0;
1183 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1184 ok_ole_success(hr, CreateStreamOnHGlobal);
1185 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1186 ok_ole_success(hr, CoMarshalInterface);
1188 CoLockObjectExternal((IUnknown*)&Test_ClassFactory, TRUE, TRUE);
1190 ok_more_than_one_lock();
1192 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1193 hr = CoReleaseMarshalData(pStream);
1194 ok_ole_success(hr, CoReleaseMarshalData);
1195 IStream_Release(pStream);
1197 ok_more_than_one_lock();
1199 CoDisconnectObject((IUnknown*)&Test_ClassFactory, 0);
1201 ok_no_locks();
1204 /* tests failure case of a same-thread marshal and unmarshal twice */
1205 static void test_normal_marshal_and_unmarshal_twice(void)
1207 HRESULT hr;
1208 IStream *pStream = NULL;
1209 IUnknown *pProxy1 = NULL;
1210 IUnknown *pProxy2 = NULL;
1212 cLocks = 0;
1214 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1215 ok_ole_success(hr, CreateStreamOnHGlobal);
1216 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1217 ok_ole_success(hr, CoMarshalInterface);
1219 ok_more_than_one_lock();
1221 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1222 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy1);
1223 ok_ole_success(hr, CoUnmarshalInterface);
1225 ok_more_than_one_lock();
1227 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1228 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
1229 ok(hr == CO_E_OBJNOTCONNECTED,
1230 "CoUnmarshalInterface should have failed with error CO_E_OBJNOTCONNECTED for double unmarshal, instead of 0x%08x\n", hr);
1232 IStream_Release(pStream);
1234 ok_more_than_one_lock();
1236 IUnknown_Release(pProxy1);
1238 ok_no_locks();
1241 /* tests success case of marshaling and unmarshaling an HRESULT */
1242 static void test_hresult_marshaling(void)
1244 HRESULT hr;
1245 HRESULT hr_marshaled = 0;
1246 IStream *pStream = NULL;
1247 static const HRESULT E_DEADBEEF = 0xdeadbeef;
1249 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1250 ok_ole_success(hr, CreateStreamOnHGlobal);
1252 hr = CoMarshalHresult(pStream, E_DEADBEEF);
1253 ok_ole_success(hr, CoMarshalHresult);
1255 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1256 hr = IStream_Read(pStream, &hr_marshaled, sizeof(HRESULT), NULL);
1257 ok_ole_success(hr, IStream_Read);
1259 ok(hr_marshaled == E_DEADBEEF, "Didn't marshal HRESULT as expected: got value 0x%08x instead\n", hr_marshaled);
1261 hr_marshaled = 0;
1262 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1263 hr = CoUnmarshalHresult(pStream, &hr_marshaled);
1264 ok_ole_success(hr, CoUnmarshalHresult);
1266 ok(hr_marshaled == E_DEADBEEF, "Didn't marshal HRESULT as expected: got value 0x%08x instead\n", hr_marshaled);
1268 IStream_Release(pStream);
1272 /* helper for test_proxy_used_in_wrong_thread */
1273 static DWORD CALLBACK bad_thread_proc(LPVOID p)
1275 IClassFactory * cf = (IClassFactory *)p;
1276 HRESULT hr;
1277 IUnknown * proxy = NULL;
1279 hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
1280 todo_wine
1281 ok(hr == CO_E_NOTINITIALIZED,
1282 "COM should have failed with CO_E_NOTINITIALIZED on using proxy without apartment, but instead returned 0x%08x\n",
1283 hr);
1285 hr = IClassFactory_QueryInterface(cf, &IID_IMultiQI, (LPVOID *)&proxy);
1286 /* Win9x returns S_OK, whilst NT returns RPC_E_WRONG_THREAD */
1287 trace("call to proxy's QueryInterface for local interface without apartment returned 0x%08x\n", hr);
1288 if (SUCCEEDED(hr))
1289 IUnknown_Release(proxy);
1291 hr = IClassFactory_QueryInterface(cf, &IID_IStream, (LPVOID *)&proxy);
1292 /* Win9x returns E_NOINTERFACE, whilst NT returns RPC_E_WRONG_THREAD */
1293 trace("call to proxy's QueryInterface without apartment returned 0x%08x\n", hr);
1294 if (SUCCEEDED(hr))
1295 IUnknown_Release(proxy);
1297 pCoInitializeEx(NULL, COINIT_MULTITHREADED);
1299 hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
1300 if (proxy) IUnknown_Release(proxy);
1301 ok(hr == RPC_E_WRONG_THREAD,
1302 "COM should have failed with RPC_E_WRONG_THREAD on using proxy from wrong apartment, but instead returned 0x%08x\n",
1303 hr);
1305 hr = IClassFactory_QueryInterface(cf, &IID_IStream, (LPVOID *)&proxy);
1306 /* Win9x returns E_NOINTERFACE, whilst NT returns RPC_E_WRONG_THREAD */
1307 trace("call to proxy's QueryInterface from wrong apartment returned 0x%08x\n", hr);
1309 /* this statement causes Win9x DCOM to crash during CoUninitialize of
1310 * other apartment, so don't test this on Win9x (signified by NT-only
1311 * export of CoRegisterSurrogateEx) */
1312 if (GetProcAddress(GetModuleHandle("ole32"), "CoRegisterSurrogateEx"))
1313 /* now be really bad and release the proxy from the wrong apartment */
1314 IUnknown_Release(cf);
1315 else
1316 skip("skipping test for releasing proxy from wrong apartment that will succeed, but cause a crash during CoUninitialize\n");
1318 CoUninitialize();
1320 return 0;
1323 /* tests failure case of a using a proxy in the wrong apartment */
1324 static void test_proxy_used_in_wrong_thread(void)
1326 HRESULT hr;
1327 IStream *pStream = NULL;
1328 IUnknown *pProxy = NULL;
1329 DWORD tid, tid2;
1330 HANDLE thread;
1331 HANDLE host_thread;
1333 cLocks = 0;
1335 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1336 ok_ole_success(hr, CreateStreamOnHGlobal);
1337 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &host_thread);
1339 ok_more_than_one_lock();
1341 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1342 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
1343 ok_ole_success(hr, CoUnmarshalInterface);
1344 IStream_Release(pStream);
1346 ok_more_than_one_lock();
1348 /* do a call that will fail, but result in IRemUnknown being used by the proxy */
1349 IClassFactory_QueryInterface(pProxy, &IID_IStream, (LPVOID *)&pStream);
1351 /* create a thread that we can misbehave in */
1352 thread = CreateThread(NULL, 0, bad_thread_proc, (LPVOID)pProxy, 0, &tid2);
1354 WaitForSingleObject(thread, INFINITE);
1355 CloseHandle(thread);
1357 /* do release statement on Win9x that we should have done above */
1358 if (!GetProcAddress(GetModuleHandle("ole32"), "CoRegisterSurrogateEx"))
1359 IUnknown_Release(pProxy);
1361 ok_no_locks();
1363 end_host_object(tid, host_thread);
1366 static HRESULT WINAPI MessageFilter_QueryInterface(IMessageFilter *iface, REFIID riid, void ** ppvObj)
1368 if (ppvObj == NULL) return E_POINTER;
1370 if (IsEqualGUID(riid, &IID_IUnknown) ||
1371 IsEqualGUID(riid, &IID_IClassFactory))
1373 *ppvObj = (LPVOID)iface;
1374 IClassFactory_AddRef(iface);
1375 return S_OK;
1378 return E_NOINTERFACE;
1381 static ULONG WINAPI MessageFilter_AddRef(IMessageFilter *iface)
1383 return 2; /* non-heap object */
1386 static ULONG WINAPI MessageFilter_Release(IMessageFilter *iface)
1388 return 1; /* non-heap object */
1391 static DWORD WINAPI MessageFilter_HandleInComingCall(
1392 IMessageFilter *iface,
1393 DWORD dwCallType,
1394 HTASK threadIDCaller,
1395 DWORD dwTickCount,
1396 LPINTERFACEINFO lpInterfaceInfo)
1398 static int callcount = 0;
1399 DWORD ret;
1400 trace("HandleInComingCall\n");
1401 switch (callcount)
1403 case 0:
1404 ret = SERVERCALL_REJECTED;
1405 break;
1406 case 1:
1407 ret = SERVERCALL_RETRYLATER;
1408 break;
1409 default:
1410 ret = SERVERCALL_ISHANDLED;
1411 break;
1413 callcount++;
1414 return ret;
1417 static DWORD WINAPI MessageFilter_RetryRejectedCall(
1418 IMessageFilter *iface,
1419 HTASK threadIDCallee,
1420 DWORD dwTickCount,
1421 DWORD dwRejectType)
1423 trace("RetryRejectedCall\n");
1424 return 0;
1427 static DWORD WINAPI MessageFilter_MessagePending(
1428 IMessageFilter *iface,
1429 HTASK threadIDCallee,
1430 DWORD dwTickCount,
1431 DWORD dwPendingType)
1433 trace("MessagePending\n");
1434 return PENDINGMSG_WAITNOPROCESS;
1437 static const IMessageFilterVtbl MessageFilter_Vtbl =
1439 MessageFilter_QueryInterface,
1440 MessageFilter_AddRef,
1441 MessageFilter_Release,
1442 MessageFilter_HandleInComingCall,
1443 MessageFilter_RetryRejectedCall,
1444 MessageFilter_MessagePending
1447 static IMessageFilter MessageFilter = { &MessageFilter_Vtbl };
1449 static void test_message_filter(void)
1451 HRESULT hr;
1452 IStream *pStream = NULL;
1453 IClassFactory *cf = NULL;
1454 DWORD tid;
1455 IUnknown *proxy = NULL;
1456 IMessageFilter *prev_filter = NULL;
1457 HANDLE thread;
1459 cLocks = 0;
1461 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1462 ok_ole_success(hr, CreateStreamOnHGlobal);
1463 tid = start_host_object2(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &MessageFilter, &thread);
1465 ok_more_than_one_lock();
1467 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1468 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&cf);
1469 ok_ole_success(hr, CoUnmarshalInterface);
1470 IStream_Release(pStream);
1472 ok_more_than_one_lock();
1474 hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
1475 ok(hr == RPC_E_CALL_REJECTED, "Call should have returned RPC_E_CALL_REJECTED, but return 0x%08x instead\n", hr);
1476 if (proxy) IUnknown_Release(proxy);
1477 proxy = NULL;
1479 hr = CoRegisterMessageFilter(&MessageFilter, &prev_filter);
1480 ok_ole_success(hr, CoRegisterMessageFilter);
1482 hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
1483 ok_ole_success(hr, IClassFactory_CreateInstance);
1485 IUnknown_Release(proxy);
1487 IClassFactory_Release(cf);
1489 ok_no_locks();
1491 end_host_object(tid, thread);
1493 hr = CoRegisterMessageFilter(prev_filter, NULL);
1494 ok_ole_success(hr, CoRegisterMessageFilter);
1497 /* test failure case of trying to unmarshal from bad stream */
1498 static void test_bad_marshal_stream(void)
1500 HRESULT hr;
1501 IStream *pStream = NULL;
1503 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1504 ok_ole_success(hr, CreateStreamOnHGlobal);
1505 hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1506 ok_ole_success(hr, CoMarshalInterface);
1508 ok_more_than_one_lock();
1510 /* try to read beyond end of stream */
1511 hr = CoReleaseMarshalData(pStream);
1512 ok(hr == STG_E_READFAULT, "Should have failed with STG_E_READFAULT, but returned 0x%08x instead\n", hr);
1514 /* now release for real */
1515 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1516 hr = CoReleaseMarshalData(pStream);
1517 ok_ole_success(hr, CoReleaseMarshalData);
1519 IStream_Release(pStream);
1522 /* tests that proxies implement certain interfaces */
1523 static void test_proxy_interfaces(void)
1525 HRESULT hr;
1526 IStream *pStream = NULL;
1527 IUnknown *pProxy = NULL;
1528 IUnknown *pOtherUnknown = NULL;
1529 DWORD tid;
1530 HANDLE thread;
1532 cLocks = 0;
1534 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1535 ok_ole_success(hr, CreateStreamOnHGlobal);
1536 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
1538 ok_more_than_one_lock();
1540 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1541 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)&pProxy);
1542 ok_ole_success(hr, CoUnmarshalInterface);
1543 IStream_Release(pStream);
1545 ok_more_than_one_lock();
1547 hr = IUnknown_QueryInterface(pProxy, &IID_IUnknown, (LPVOID*)&pOtherUnknown);
1548 ok_ole_success(hr, IUnknown_QueryInterface IID_IUnknown);
1549 if (hr == S_OK) IUnknown_Release(pOtherUnknown);
1551 hr = IUnknown_QueryInterface(pProxy, &IID_IClientSecurity, (LPVOID*)&pOtherUnknown);
1552 ok_ole_success(hr, IUnknown_QueryInterface IID_IClientSecurity);
1553 if (hr == S_OK) IUnknown_Release(pOtherUnknown);
1555 hr = IUnknown_QueryInterface(pProxy, &IID_IMultiQI, (LPVOID*)&pOtherUnknown);
1556 ok_ole_success(hr, IUnknown_QueryInterface IID_IMultiQI);
1557 if (hr == S_OK) IUnknown_Release(pOtherUnknown);
1559 hr = IUnknown_QueryInterface(pProxy, &IID_IMarshal, (LPVOID*)&pOtherUnknown);
1560 ok_ole_success(hr, IUnknown_QueryInterface IID_IMarshal);
1561 if (hr == S_OK) IUnknown_Release(pOtherUnknown);
1563 /* IMarshal2 is also supported on NT-based systems, but is pretty much
1564 * useless as it has no more methods over IMarshal that it inherits from. */
1566 IUnknown_Release(pProxy);
1568 ok_no_locks();
1570 end_host_object(tid, thread);
1573 typedef struct
1575 const IUnknownVtbl *lpVtbl;
1576 ULONG refs;
1577 } HeapUnknown;
1579 static HRESULT WINAPI HeapUnknown_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
1581 if (IsEqualIID(riid, &IID_IUnknown))
1583 IUnknown_AddRef(iface);
1584 *ppv = (LPVOID)iface;
1585 return S_OK;
1587 *ppv = NULL;
1588 return E_NOINTERFACE;
1591 static ULONG WINAPI HeapUnknown_AddRef(IUnknown *iface)
1593 HeapUnknown *This = (HeapUnknown *)iface;
1594 return InterlockedIncrement((LONG*)&This->refs);
1597 static ULONG WINAPI HeapUnknown_Release(IUnknown *iface)
1599 HeapUnknown *This = (HeapUnknown *)iface;
1600 ULONG refs = InterlockedDecrement((LONG*)&This->refs);
1601 if (!refs) HeapFree(GetProcessHeap(), 0, This);
1602 return refs;
1605 static const IUnknownVtbl HeapUnknown_Vtbl =
1607 HeapUnknown_QueryInterface,
1608 HeapUnknown_AddRef,
1609 HeapUnknown_Release
1612 static void test_proxybuffer(REFIID riid)
1614 HRESULT hr;
1615 IPSFactoryBuffer *psfb;
1616 IRpcProxyBuffer *proxy;
1617 LPVOID lpvtbl;
1618 ULONG refs;
1619 CLSID clsid;
1620 HeapUnknown *pUnkOuter = HeapAlloc(GetProcessHeap(), 0, sizeof(*pUnkOuter));
1622 pUnkOuter->lpVtbl = &HeapUnknown_Vtbl;
1623 pUnkOuter->refs = 1;
1625 hr = CoGetPSClsid(riid, &clsid);
1626 ok_ole_success(hr, CoGetPSClsid);
1628 hr = CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL, &IID_IPSFactoryBuffer, (LPVOID*)&psfb);
1629 ok_ole_success(hr, CoGetClassObject);
1631 hr = IPSFactoryBuffer_CreateProxy(psfb, (IUnknown*)pUnkOuter, riid, &proxy, &lpvtbl);
1632 ok_ole_success(hr, IPSFactoryBuffer_CreateProxy);
1633 ok(lpvtbl != NULL, "IPSFactoryBuffer_CreateProxy succeeded, but returned a NULL vtable!\n");
1635 /* release our reference to the outer unknown object - the PS factory
1636 * buffer will have AddRef's it in the CreateProxy call */
1637 refs = IUnknown_Release((IUnknown *)pUnkOuter);
1638 ok(refs == 1, "Ref count of outer unknown should have been 1 instead of %d\n", refs);
1640 refs = IPSFactoryBuffer_Release(psfb);
1641 if (0)
1643 /* not reliable on native. maybe it leaks references! */
1644 ok(refs == 0, "Ref-count leak of %d on IPSFactoryBuffer\n", refs);
1647 refs = IUnknown_Release((IUnknown *)lpvtbl);
1648 ok(refs == 0, "Ref-count leak of %d on IRpcProxyBuffer\n", refs);
1650 refs = IRpcProxyBuffer_Release(proxy);
1651 ok(refs == 0, "Ref-count leak of %d on IRpcProxyBuffer\n", refs);
1654 static void test_stubbuffer(REFIID riid)
1656 HRESULT hr;
1657 IPSFactoryBuffer *psfb;
1658 IRpcStubBuffer *stub;
1659 ULONG refs;
1660 CLSID clsid;
1662 cLocks = 0;
1664 hr = CoGetPSClsid(riid, &clsid);
1665 ok_ole_success(hr, CoGetPSClsid);
1667 hr = CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL, &IID_IPSFactoryBuffer, (LPVOID*)&psfb);
1668 ok_ole_success(hr, CoGetClassObject);
1670 hr = IPSFactoryBuffer_CreateStub(psfb, riid, (IUnknown*)&Test_ClassFactory, &stub);
1671 ok_ole_success(hr, IPSFactoryBuffer_CreateStub);
1673 refs = IPSFactoryBuffer_Release(psfb);
1674 if (0)
1676 /* not reliable on native. maybe it leaks references */
1677 ok(refs == 0, "Ref-count leak of %d on IPSFactoryBuffer\n", refs);
1680 ok_more_than_one_lock();
1682 IRpcStubBuffer_Disconnect(stub);
1684 ok_no_locks();
1686 refs = IRpcStubBuffer_Release(stub);
1687 ok(refs == 0, "Ref-count leak of %d on IRpcProxyBuffer\n", refs);
1690 static HWND hwnd_app;
1692 static HRESULT WINAPI TestRE_IClassFactory_CreateInstance(
1693 LPCLASSFACTORY iface,
1694 LPUNKNOWN pUnkOuter,
1695 REFIID riid,
1696 LPVOID *ppvObj)
1698 DWORD_PTR res;
1699 if (IsEqualIID(riid, &IID_IWineTest))
1701 BOOL ret = SendMessageTimeout(hwnd_app, WM_NULL, 0, 0, SMTO_BLOCK, 5000, &res);
1702 ok(ret, "Timed out sending a message to originating window during RPC call\n");
1704 return S_FALSE;
1707 static const IClassFactoryVtbl TestREClassFactory_Vtbl =
1709 Test_IClassFactory_QueryInterface,
1710 Test_IClassFactory_AddRef,
1711 Test_IClassFactory_Release,
1712 TestRE_IClassFactory_CreateInstance,
1713 Test_IClassFactory_LockServer
1716 IClassFactory TestRE_ClassFactory = { &TestREClassFactory_Vtbl };
1718 static LRESULT CALLBACK window_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1720 switch (msg)
1722 case WM_USER:
1724 HRESULT hr;
1725 IStream *pStream = NULL;
1726 IClassFactory *proxy = NULL;
1727 IUnknown *object;
1728 DWORD tid;
1729 HANDLE thread;
1731 cLocks = 0;
1733 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1734 ok_ole_success(hr, CreateStreamOnHGlobal);
1735 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&TestRE_ClassFactory, MSHLFLAGS_NORMAL, &thread);
1737 ok_more_than_one_lock();
1739 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1740 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&proxy);
1741 ok_ole_success(hr, CoReleaseMarshalData);
1742 IStream_Release(pStream);
1744 ok_more_than_one_lock();
1746 /* note the use of the magic IID_IWineTest value to tell remote thread
1747 * to try to send a message back to us */
1748 hr = IClassFactory_CreateInstance(proxy, NULL, &IID_IWineTest, (void **)&object);
1750 IClassFactory_Release(proxy);
1752 ok_no_locks();
1754 end_host_object(tid, thread);
1756 PostMessage(hwnd, WM_QUIT, 0, 0);
1758 return 0;
1760 case WM_USER+1:
1762 HRESULT hr;
1763 IStream *pStream = NULL;
1764 IClassFactory *proxy = NULL;
1765 IUnknown *object;
1766 DWORD tid;
1767 HANDLE thread;
1769 cLocks = 0;
1771 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1772 ok_ole_success(hr, CreateStreamOnHGlobal);
1773 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&TestRE_ClassFactory, MSHLFLAGS_NORMAL, &thread);
1775 ok_more_than_one_lock();
1777 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1778 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&proxy);
1779 ok_ole_success(hr, CoReleaseMarshalData);
1780 IStream_Release(pStream);
1782 ok_more_than_one_lock();
1784 /* post quit message before a doing a COM call to show that a pending
1785 * WM_QUIT message doesn't stop the call from succeeding */
1786 PostMessage(hwnd, WM_QUIT, 0, 0);
1787 hr = IClassFactory_CreateInstance(proxy, NULL, &IID_IUnknown, (void **)&object);
1789 IClassFactory_Release(proxy);
1791 ok_no_locks();
1793 end_host_object(tid, thread);
1795 return 0;
1797 case WM_USER+2:
1799 HRESULT hr;
1800 IStream *pStream = NULL;
1801 IClassFactory *proxy = NULL;
1802 IUnknown *object;
1803 DWORD tid;
1804 HANDLE thread;
1806 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1807 ok_ole_success(hr, CreateStreamOnHGlobal);
1808 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
1810 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1811 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&proxy);
1812 ok_ole_success(hr, CoReleaseMarshalData);
1813 IStream_Release(pStream);
1815 /* shows that COM calls executed during the processing of sent
1816 * messages should fail */
1817 hr = IClassFactory_CreateInstance(proxy, NULL, &IID_IUnknown, (void **)&object);
1818 ok(hr == RPC_E_CANTCALLOUT_ININPUTSYNCCALL,
1819 "COM call during processing of sent message should return RPC_E_CANTCALLOUT_ININPUTSYNCCALL instead of 0x%08x\n", hr);
1821 IClassFactory_Release(proxy);
1823 end_host_object(tid, thread);
1825 PostQuitMessage(0);
1827 return 0;
1829 default:
1830 return DefWindowProc(hwnd, msg, wparam, lparam);
1834 static void register_test_window(void)
1836 WNDCLASS wndclass;
1838 memset(&wndclass, 0, sizeof(wndclass));
1839 wndclass.lpfnWndProc = window_proc;
1840 wndclass.lpszClassName = "WineCOMTest";
1841 RegisterClass(&wndclass);
1844 static void test_message_reentrancy(void)
1846 MSG msg;
1848 hwnd_app = CreateWindow("WineCOMTest", NULL, 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, NULL, 0);
1849 ok(hwnd_app != NULL, "Window creation failed\n");
1851 /* start message re-entrancy test */
1852 PostMessage(hwnd_app, WM_USER, 0, 0);
1854 while (GetMessage(&msg, NULL, 0, 0))
1856 TranslateMessage(&msg);
1857 DispatchMessage(&msg);
1859 DestroyWindow(hwnd_app);
1862 static HRESULT WINAPI TestMsg_IClassFactory_CreateInstance(
1863 LPCLASSFACTORY iface,
1864 LPUNKNOWN pUnkOuter,
1865 REFIID riid,
1866 LPVOID *ppvObj)
1868 *ppvObj = NULL;
1869 SendMessage(hwnd_app, WM_USER+2, 0, 0);
1870 return S_OK;
1873 static IClassFactoryVtbl TestMsgClassFactory_Vtbl =
1875 Test_IClassFactory_QueryInterface,
1876 Test_IClassFactory_AddRef,
1877 Test_IClassFactory_Release,
1878 TestMsg_IClassFactory_CreateInstance,
1879 Test_IClassFactory_LockServer
1882 IClassFactory TestMsg_ClassFactory = { &TestMsgClassFactory_Vtbl };
1884 static void test_call_from_message(void)
1886 MSG msg;
1887 IStream *pStream;
1888 HRESULT hr;
1889 IClassFactory *proxy;
1890 DWORD tid;
1891 HANDLE thread;
1892 IUnknown *object;
1894 hwnd_app = CreateWindow("WineCOMTest", NULL, 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, NULL, 0);
1895 ok(hwnd_app != NULL, "Window creation failed\n");
1897 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1898 ok_ole_success(hr, CreateStreamOnHGlobal);
1899 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&TestMsg_ClassFactory, MSHLFLAGS_NORMAL, &thread);
1901 ok_more_than_one_lock();
1903 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1904 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&proxy);
1905 ok_ole_success(hr, CoReleaseMarshalData);
1906 IStream_Release(pStream);
1908 ok_more_than_one_lock();
1910 /* start message re-entrancy test */
1911 hr = IClassFactory_CreateInstance(proxy, NULL, &IID_IUnknown, (void **)&object);
1912 ok_ole_success(hr, IClassFactory_CreateInstance);
1914 IClassFactory_Release(proxy);
1916 ok_no_locks();
1918 end_host_object(tid, thread);
1920 while (GetMessage(&msg, NULL, 0, 0))
1922 TranslateMessage(&msg);
1923 DispatchMessage(&msg);
1925 DestroyWindow(hwnd_app);
1928 static void test_WM_QUIT_handling(void)
1930 MSG msg;
1932 hwnd_app = CreateWindow("WineCOMTest", NULL, 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, NULL, 0);
1933 ok(hwnd_app != NULL, "Window creation failed\n");
1935 /* start WM_QUIT handling test */
1936 PostMessage(hwnd_app, WM_USER+1, 0, 0);
1938 while (GetMessage(&msg, NULL, 0, 0))
1940 TranslateMessage(&msg);
1941 DispatchMessage(&msg);
1945 static void test_freethreadedmarshaldata(IStream *pStream, MSHCTX mshctx, void *ptr, DWORD mshlflags)
1947 HGLOBAL hglobal;
1948 DWORD size;
1949 char *marshal_data;
1950 HRESULT hr;
1952 hr = GetHGlobalFromStream(pStream, &hglobal);
1953 ok_ole_success(hr, GetHGlobalFromStream);
1955 size = GlobalSize(hglobal);
1957 marshal_data = (char *)GlobalLock(hglobal);
1959 if (mshctx == MSHCTX_INPROC)
1961 DWORD expected_size = sizeof(DWORD) + sizeof(void *) + sizeof(DWORD) + sizeof(GUID);
1962 ok(size == expected_size, "size should have been %d instead of %d\n", expected_size, size);
1964 ok(*(DWORD *)marshal_data == mshlflags, "expected 0x%x, but got 0x%x for mshctx\n", mshlflags, *(DWORD *)marshal_data);
1965 marshal_data += sizeof(DWORD);
1966 ok(*(void **)marshal_data == ptr, "expected %p, but got %p for mshctx\n", ptr, *(void **)marshal_data);
1967 marshal_data += sizeof(void *);
1968 ok(*(DWORD *)marshal_data == 0, "expected 0x0, but got 0x%x\n", *(DWORD *)marshal_data);
1969 marshal_data += sizeof(DWORD);
1970 trace("got guid data: {%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\n",
1971 ((GUID *)marshal_data)->Data1, ((GUID *)marshal_data)->Data2, ((GUID *)marshal_data)->Data3,
1972 ((GUID *)marshal_data)->Data4[0], ((GUID *)marshal_data)->Data4[1], ((GUID *)marshal_data)->Data4[2], ((GUID *)marshal_data)->Data4[3],
1973 ((GUID *)marshal_data)->Data4[4], ((GUID *)marshal_data)->Data4[5], ((GUID *)marshal_data)->Data4[6], ((GUID *)marshal_data)->Data4[7]);
1975 else
1977 ok(size > sizeof(DWORD), "size should have been > sizeof(DWORD), not %d\n", size);
1978 ok(*(DWORD *)marshal_data == 0x574f454d /* MEOW */,
1979 "marshal data should be filled by standard marshal and start with MEOW signature\n");
1982 GlobalUnlock(hglobal);
1985 static void test_freethreadedmarshaler(void)
1987 HRESULT hr;
1988 IUnknown *pFTUnknown;
1989 IMarshal *pFTMarshal;
1990 IStream *pStream;
1991 IUnknown *pProxy;
1992 static const LARGE_INTEGER llZero;
1994 cLocks = 0;
1995 hr = CoCreateFreeThreadedMarshaler(NULL, &pFTUnknown);
1996 ok_ole_success(hr, CoCreateFreeThreadedMarshaler);
1997 hr = IUnknown_QueryInterface(pFTUnknown, &IID_IMarshal, (void **)&pFTMarshal);
1998 ok_ole_success(hr, IUnknown_QueryInterface);
1999 IUnknown_Release(pFTUnknown);
2001 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
2002 ok_ole_success(hr, CreateStreamOnHGlobal);
2004 /* inproc normal marshaling */
2006 hr = IMarshal_MarshalInterface(pFTMarshal, pStream, &IID_IClassFactory,
2007 (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
2008 ok_ole_success(hr, IMarshal_MarshalInterface);
2010 ok_more_than_one_lock();
2012 test_freethreadedmarshaldata(pStream, MSHCTX_INPROC, &Test_ClassFactory, MSHLFLAGS_NORMAL);
2014 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2015 hr = IMarshal_UnmarshalInterface(pFTMarshal, pStream, &IID_IUnknown, (void **)&pProxy);
2016 ok_ole_success(hr, IMarshal_UnmarshalInterface);
2018 IUnknown_Release(pProxy);
2020 ok_no_locks();
2022 /* native doesn't allow us to unmarshal or release the stream data,
2023 * presumably because it wants us to call CoMarshalInterface instead */
2024 if (0)
2026 /* local normal marshaling */
2028 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2029 hr = IMarshal_MarshalInterface(pFTMarshal, pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_LOCAL, NULL, MSHLFLAGS_NORMAL);
2030 ok_ole_success(hr, IMarshal_MarshalInterface);
2032 ok_more_than_one_lock();
2034 test_freethreadedmarshaldata(pStream, MSHCTX_LOCAL, &Test_ClassFactory, MSHLFLAGS_NORMAL);
2036 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2037 hr = IMarshal_ReleaseMarshalData(pFTMarshal, pStream);
2038 ok_ole_success(hr, IMarshal_ReleaseMarshalData);
2040 ok_no_locks();
2043 /* inproc table-strong marshaling */
2045 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2046 hr = IMarshal_MarshalInterface(pFTMarshal, pStream, &IID_IClassFactory,
2047 (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, (void *)0xdeadbeef,
2048 MSHLFLAGS_TABLESTRONG);
2049 ok_ole_success(hr, IMarshal_MarshalInterface);
2051 ok_more_than_one_lock();
2053 test_freethreadedmarshaldata(pStream, MSHCTX_INPROC, &Test_ClassFactory, MSHLFLAGS_TABLESTRONG);
2055 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2056 hr = IMarshal_UnmarshalInterface(pFTMarshal, pStream, &IID_IUnknown, (void **)&pProxy);
2057 ok_ole_success(hr, IMarshal_UnmarshalInterface);
2059 IUnknown_Release(pProxy);
2061 ok_more_than_one_lock();
2063 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2064 hr = IMarshal_ReleaseMarshalData(pFTMarshal, pStream);
2065 ok_ole_success(hr, IMarshal_ReleaseMarshalData);
2067 ok_no_locks();
2069 /* inproc table-weak marshaling */
2071 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2072 hr = IMarshal_MarshalInterface(pFTMarshal, pStream, &IID_IClassFactory,
2073 (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, (void *)0xdeadbeef,
2074 MSHLFLAGS_TABLEWEAK);
2075 ok_ole_success(hr, IMarshal_MarshalInterface);
2077 ok_no_locks();
2079 test_freethreadedmarshaldata(pStream, MSHCTX_INPROC, &Test_ClassFactory, MSHLFLAGS_TABLEWEAK);
2081 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2082 hr = IMarshal_UnmarshalInterface(pFTMarshal, pStream, &IID_IUnknown, (void **)&pProxy);
2083 ok_ole_success(hr, IMarshal_UnmarshalInterface);
2085 ok_more_than_one_lock();
2087 IUnknown_Release(pProxy);
2089 ok_no_locks();
2091 /* inproc normal marshaling (for extraordinary cases) */
2093 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2094 hr = IMarshal_MarshalInterface(pFTMarshal, pStream, &IID_IClassFactory,
2095 (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
2096 ok_ole_success(hr, IMarshal_MarshalInterface);
2098 ok_more_than_one_lock();
2100 /* this call shows that DisconnectObject does nothing */
2101 hr = IMarshal_DisconnectObject(pFTMarshal, 0);
2102 ok_ole_success(hr, IMarshal_DisconnectObject);
2104 ok_more_than_one_lock();
2106 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2107 hr = IMarshal_ReleaseMarshalData(pFTMarshal, pStream);
2108 ok_ole_success(hr, IMarshal_ReleaseMarshalData);
2110 ok_no_locks();
2112 /* doesn't enforce marshaling rules here and allows us to unmarshal the
2113 * interface, even though it was freed above */
2114 IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2115 hr = IMarshal_UnmarshalInterface(pFTMarshal, pStream, &IID_IUnknown, (void **)&pProxy);
2116 ok_ole_success(hr, IMarshal_UnmarshalInterface);
2118 ok_no_locks();
2120 IStream_Release(pStream);
2121 IMarshal_Release(pFTMarshal);
2124 static void test_inproc_handler(void)
2126 HRESULT hr;
2127 IUnknown *pObject;
2128 IUnknown *pObject2;
2129 char buffer[256];
2130 LPOLESTR pszClsid;
2131 HKEY hkey;
2132 DWORD dwDisposition;
2133 DWORD error;
2135 hr = StringFromCLSID(&CLSID_WineTest, &pszClsid);
2136 ok_ole_success(hr, "StringFromCLSID");
2137 strcpy(buffer, "CLSID\\");
2138 WideCharToMultiByte(CP_ACP, 0, pszClsid, -1, buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), NULL, NULL);
2139 CoTaskMemFree(pszClsid);
2140 strcat(buffer, "\\InprocHandler32");
2141 error = RegCreateKeyEx(HKEY_CLASSES_ROOT, buffer, 0, NULL, 0, KEY_SET_VALUE, NULL, &hkey, &dwDisposition);
2142 ok(error == ERROR_SUCCESS, "RegCreateKeyEx failed with error %d\n", error);
2143 error = RegSetValueEx(hkey, NULL, 0, REG_SZ, (const unsigned char *)"ole32.dll", strlen("ole32.dll") + 1);
2144 ok(error == ERROR_SUCCESS, "RegSetValueEx failed with error %d\n", error);
2145 RegCloseKey(hkey);
2147 hr = CoCreateInstance(&CLSID_WineTest, NULL, CLSCTX_INPROC_HANDLER, &IID_IUnknown, (void **)&pObject);
2148 todo_wine
2149 ok_ole_success(hr, "CoCreateInstance");
2151 if (SUCCEEDED(hr))
2153 hr = IUnknown_QueryInterface(pObject, &IID_IWineTest, (void **)&pObject2);
2154 ok(hr == E_NOINTERFACE, "IUnknown_QueryInterface on handler for invalid interface returned 0x%08x instead of E_NOINTERFACE\n", hr);
2156 /* it's a handler as it supports IOleObject */
2157 hr = IUnknown_QueryInterface(pObject, &IID_IOleObject, (void **)&pObject2);
2158 ok_ole_success(hr, "IUnknown_QueryInterface(&IID_IOleObject)");
2159 IUnknown_Release(pObject2);
2161 IUnknown_Release(pObject);
2164 RegDeleteKey(HKEY_CLASSES_ROOT, buffer);
2165 *strrchr(buffer, '\\') = '\0';
2166 RegDeleteKey(HKEY_CLASSES_ROOT, buffer);
2169 static HRESULT WINAPI Test_SMI_QueryInterface(
2170 IStdMarshalInfo *iface,
2171 REFIID riid,
2172 LPVOID *ppvObj)
2174 if (ppvObj == NULL) return E_POINTER;
2176 if (IsEqualGUID(riid, &IID_IUnknown) ||
2177 IsEqualGUID(riid, &IID_IStdMarshalInfo))
2179 *ppvObj = (LPVOID)iface;
2180 IClassFactory_AddRef(iface);
2181 return S_OK;
2184 return E_NOINTERFACE;
2187 static ULONG WINAPI Test_SMI_AddRef(IStdMarshalInfo *iface)
2189 LockModule();
2190 return 2; /* non-heap-based object */
2193 static ULONG WINAPI Test_SMI_Release(IStdMarshalInfo *iface)
2195 UnlockModule();
2196 return 1; /* non-heap-based object */
2199 static HRESULT WINAPI Test_SMI_GetClassForHandler(
2200 IStdMarshalInfo *iface,
2201 DWORD dwDestContext,
2202 void *pvDestContext,
2203 CLSID *pClsid)
2205 *pClsid = CLSID_WineTest;
2206 return S_OK;
2209 static const IStdMarshalInfoVtbl Test_SMI_Vtbl =
2211 Test_SMI_QueryInterface,
2212 Test_SMI_AddRef,
2213 Test_SMI_Release,
2214 Test_SMI_GetClassForHandler
2217 static IStdMarshalInfo Test_SMI = {&Test_SMI_Vtbl};
2219 static void test_handler_marshaling(void)
2221 HRESULT hr;
2222 IStream *pStream = NULL;
2223 IUnknown *pProxy = NULL;
2224 IUnknown *pObject;
2225 DWORD tid;
2226 HANDLE thread;
2227 static const LARGE_INTEGER ullZero;
2229 cLocks = 0;
2231 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
2232 ok_ole_success(hr, "CreateStreamOnHGlobal");
2233 tid = start_host_object(pStream, &IID_IUnknown, (IUnknown*)&Test_SMI, MSHLFLAGS_NORMAL, &thread);
2235 ok_more_than_one_lock();
2237 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
2238 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)&pProxy);
2239 ok_ole_success(hr, "CoUnmarshalInterface");
2240 IStream_Release(pStream);
2242 ok_more_than_one_lock();
2244 hr = IUnknown_QueryInterface(pProxy, &IID_IWineTest, (void **)&pObject);
2245 ok(hr == E_NOINTERFACE, "IUnknown_QueryInterface with unknown IID should have returned E_NOINTERFACE instead of 0x%08x\n", hr);
2247 /* it's a handler as it supports IOleObject */
2248 hr = IUnknown_QueryInterface(pProxy, &IID_IOleObject, (void **)&pObject);
2249 todo_wine
2250 ok_ole_success(hr, "IUnknown_QueryInterface(&IID_IOleObject)");
2251 if (SUCCEEDED(hr)) IUnknown_Release(pObject);
2253 IUnknown_Release(pProxy);
2255 ok_no_locks();
2257 end_host_object(tid, thread);
2259 /* FIXME: test IPersist interface has the same effect as IStdMarshalInfo */
2263 static void test_client_security(void)
2265 HRESULT hr;
2266 IStream *pStream = NULL;
2267 IClassFactory *pProxy = NULL;
2268 IUnknown *pProxy2 = NULL;
2269 IUnknown *pUnknown1 = NULL;
2270 IUnknown *pUnknown2 = NULL;
2271 IClientSecurity *pCliSec = NULL;
2272 IMarshal *pMarshal;
2273 DWORD tid;
2274 HANDLE thread;
2275 static const LARGE_INTEGER ullZero;
2276 DWORD dwAuthnSvc;
2277 DWORD dwAuthzSvc;
2278 OLECHAR *pServerPrincName;
2279 DWORD dwAuthnLevel;
2280 DWORD dwImpLevel;
2281 void *pAuthInfo;
2282 DWORD dwCapabilities;
2283 void *pv;
2285 cLocks = 0;
2287 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
2288 ok_ole_success(hr, "CreateStreamOnHGlobal");
2289 tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
2291 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
2292 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
2293 ok_ole_success(hr, "CoUnmarshalInterface");
2294 IStream_Release(pStream);
2296 hr = IUnknown_QueryInterface(pProxy, &IID_IUnknown, (LPVOID*)&pUnknown1);
2297 ok_ole_success(hr, "IUnknown_QueryInterface IID_IUnknown");
2299 hr = IUnknown_QueryInterface(pProxy, &IID_IRemUnknown, (LPVOID*)&pProxy2);
2300 ok_ole_success(hr, "IUnknown_QueryInterface IID_IStream");
2302 hr = IUnknown_QueryInterface(pProxy2, &IID_IUnknown, (LPVOID*)&pUnknown2);
2303 ok_ole_success(hr, "IUnknown_QueryInterface IID_IUnknown");
2305 ok(pUnknown1 == pUnknown2, "both proxy's IUnknowns should be the same - %p, %p\n", pUnknown1, pUnknown2);
2307 hr = IUnknown_QueryInterface(pProxy, &IID_IMarshal, (LPVOID*)&pMarshal);
2308 ok_ole_success(hr, "IUnknown_QueryInterface IID_IMarshal");
2310 hr = IUnknown_QueryInterface(pProxy, &IID_IClientSecurity, (LPVOID*)&pCliSec);
2311 ok_ole_success(hr, "IUnknown_QueryInterface IID_IClientSecurity");
2313 hr = IClientSecurity_QueryBlanket(pCliSec, (IUnknown *)pProxy, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
2314 todo_wine ok_ole_success(hr, "IClientSecurity_QueryBlanket (all NULLs)");
2316 hr = IClientSecurity_QueryBlanket(pCliSec, (IUnknown *)pMarshal, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
2317 todo_wine ok(hr == E_NOINTERFACE, "IClientSecurity_QueryBlanket with local interface should have returned E_NOINTERFACE instead of 0x%08x\n", hr);
2319 hr = IClientSecurity_QueryBlanket(pCliSec, (IUnknown *)pProxy, &dwAuthnSvc, &dwAuthzSvc, &pServerPrincName, &dwAuthnLevel, &dwImpLevel, &pAuthInfo, &dwCapabilities);
2320 todo_wine ok_ole_success(hr, "IClientSecurity_QueryBlanket");
2322 hr = IClientSecurity_SetBlanket(pCliSec, (IUnknown *)pProxy, dwAuthnSvc, dwAuthzSvc, pServerPrincName, dwAuthnLevel, RPC_C_IMP_LEVEL_IMPERSONATE, pAuthInfo, dwCapabilities);
2323 todo_wine ok_ole_success(hr, "IClientSecurity_SetBlanket");
2325 hr = IClassFactory_CreateInstance(pProxy, NULL, &IID_IWineTest, &pv);
2326 ok(hr == E_NOINTERFACE, "COM call should have succeeded instead of returning 0x%08x\n", hr);
2328 hr = IClientSecurity_SetBlanket(pCliSec, (IUnknown *)pMarshal, dwAuthnSvc, dwAuthzSvc, pServerPrincName, dwAuthnLevel, dwImpLevel, pAuthInfo, dwCapabilities);
2329 todo_wine ok(hr == E_NOINTERFACE, "IClientSecurity_SetBlanket with local interface should have returned E_NOINTERFACE instead of 0x%08x\n", hr);
2331 hr = IClientSecurity_SetBlanket(pCliSec, (IUnknown *)pProxy, 0xdeadbeef, dwAuthzSvc, pServerPrincName, dwAuthnLevel, dwImpLevel, pAuthInfo, dwCapabilities);
2332 todo_wine ok(hr == E_INVALIDARG, "IClientSecurity_SetBlanke with invalid dwAuthnSvc should have returned E_INVALIDARG instead of 0x%08x\n", hr);
2334 CoTaskMemFree(pServerPrincName);
2336 hr = IClientSecurity_QueryBlanket(pCliSec, (IUnknown *)pUnknown1, &dwAuthnSvc, &dwAuthzSvc, &pServerPrincName, &dwAuthnLevel, &dwImpLevel, &pAuthInfo, &dwCapabilities);
2337 todo_wine ok_ole_success(hr, "IClientSecurity_QueryBlanket(IUnknown)");
2339 CoTaskMemFree(pServerPrincName);
2341 IClassFactory_Release(pProxy);
2342 IUnknown_Release(pProxy2);
2343 IUnknown_Release(pUnknown1);
2344 IUnknown_Release(pUnknown2);
2345 IMarshal_Release(pMarshal);
2346 IClientSecurity_Release(pCliSec);
2348 end_host_object(tid, thread);
2351 static HANDLE heventShutdown;
2353 static void LockModuleOOP(void)
2355 InterlockedIncrement(&cLocks); /* for test purposes only */
2356 CoAddRefServerProcess();
2359 static void UnlockModuleOOP(void)
2361 InterlockedDecrement(&cLocks); /* for test purposes only */
2362 if (!CoReleaseServerProcess())
2363 SetEvent(heventShutdown);
2366 static HWND hwnd_app;
2368 static HRESULT WINAPI TestOOP_IClassFactory_QueryInterface(
2369 LPCLASSFACTORY iface,
2370 REFIID riid,
2371 LPVOID *ppvObj)
2373 if (ppvObj == NULL) return E_POINTER;
2375 if (IsEqualGUID(riid, &IID_IUnknown) ||
2376 IsEqualGUID(riid, &IID_IClassFactory))
2378 *ppvObj = (LPVOID)iface;
2379 IClassFactory_AddRef(iface);
2380 return S_OK;
2383 return E_NOINTERFACE;
2386 static ULONG WINAPI TestOOP_IClassFactory_AddRef(LPCLASSFACTORY iface)
2388 return 2; /* non-heap-based object */
2391 static ULONG WINAPI TestOOP_IClassFactory_Release(LPCLASSFACTORY iface)
2393 return 1; /* non-heap-based object */
2396 static HRESULT WINAPI TestOOP_IClassFactory_CreateInstance(
2397 LPCLASSFACTORY iface,
2398 LPUNKNOWN pUnkOuter,
2399 REFIID riid,
2400 LPVOID *ppvObj)
2402 if (IsEqualIID(riid, &IID_IClassFactory) || IsEqualIID(riid, &IID_IUnknown))
2404 *ppvObj = iface;
2405 return S_OK;
2407 return CLASS_E_CLASSNOTAVAILABLE;
2410 static HRESULT WINAPI TestOOP_IClassFactory_LockServer(
2411 LPCLASSFACTORY iface,
2412 BOOL fLock)
2414 if (fLock)
2415 LockModuleOOP();
2416 else
2417 UnlockModuleOOP();
2418 return S_OK;
2421 static const IClassFactoryVtbl TestClassFactoryOOP_Vtbl =
2423 TestOOP_IClassFactory_QueryInterface,
2424 TestOOP_IClassFactory_AddRef,
2425 TestOOP_IClassFactory_Release,
2426 TestOOP_IClassFactory_CreateInstance,
2427 TestOOP_IClassFactory_LockServer
2430 static IClassFactory TestOOP_ClassFactory = { &TestClassFactoryOOP_Vtbl };
2432 static void test_register_local_server(void)
2434 DWORD cookie;
2435 HRESULT hr;
2436 HANDLE ready_event;
2437 HANDLE quit_event;
2438 DWORD wait;
2440 heventShutdown = CreateEvent(NULL, TRUE, FALSE, NULL);
2442 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&TestOOP_ClassFactory,
2443 CLSCTX_LOCAL_SERVER, REGCLS_SINGLEUSE, &cookie);
2444 ok_ole_success(hr, CoRegisterClassObject);
2446 ready_event = CreateEvent(NULL, FALSE, FALSE, "Wine COM Test Ready Event");
2447 SetEvent(ready_event);
2449 quit_event = CreateEvent(NULL, FALSE, FALSE, "Wine COM Test Quit Event");
2453 wait = MsgWaitForMultipleObjects(1, &quit_event, FALSE, INFINITE, QS_ALLINPUT);
2454 if (wait == WAIT_OBJECT_0+1)
2456 MSG msg;
2457 BOOL ret = PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
2458 if (ret)
2460 trace("Message 0x%x\n", msg.message);
2461 TranslateMessage(&msg);
2462 DispatchMessage(&msg);
2466 while (wait == WAIT_OBJECT_0+1);
2468 hr = CoRevokeClassObject(cookie);
2469 ok_ole_success(hr, CoRevokeClassObject);
2472 static HANDLE create_target_process(const char *arg)
2474 char **argv;
2475 char cmdline[MAX_PATH];
2476 PROCESS_INFORMATION pi;
2477 STARTUPINFO si = { 0 };
2478 si.cb = sizeof(si);
2480 pi.hThread = NULL;
2481 pi.hProcess = NULL;
2482 winetest_get_mainargs( &argv );
2483 sprintf(cmdline, "%s %s %s", argv[0], argv[1], arg);
2484 ok(CreateProcess(argv[0], cmdline, NULL, NULL, FALSE, 0, NULL, NULL,
2485 &si, &pi) != 0, "CreateProcess failed with error: %u\n", GetLastError());
2486 if (pi.hThread) CloseHandle(pi.hThread);
2487 return pi.hProcess;
2490 /* tests functions commonly used by out of process COM servers */
2491 static void test_local_server(void)
2493 DWORD cookie;
2494 HRESULT hr;
2495 IClassFactory * cf;
2496 DWORD ret;
2497 HANDLE process;
2498 HANDLE quit_event;
2499 HANDLE ready_event;
2501 heventShutdown = CreateEvent(NULL, TRUE, FALSE, NULL);
2503 cLocks = 0;
2505 /* Start the object suspended */
2506 hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&TestOOP_ClassFactory,
2507 CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE | REGCLS_SUSPENDED, &cookie);
2508 ok_ole_success(hr, CoRegisterClassObject);
2510 /* ... and CoGetClassObject does not find it and fails when it looks for the
2511 * class in the registry */
2512 hr = CoGetClassObject(&CLSID_WineOOPTest, CLSCTX_INPROC_SERVER,
2513 NULL, &IID_IClassFactory, (LPVOID*)&cf);
2514 ok(hr == REGDB_E_CLASSNOTREG || /* NT */
2515 hr == S_OK /* Win9x */,
2516 "CoGetClassObject should have returned REGDB_E_CLASSNOTREG instead of 0x%08x\n", hr);
2518 /* Resume the object suspended above ... */
2519 hr = CoResumeClassObjects();
2520 ok_ole_success(hr, CoResumeClassObjects);
2522 /* ... and now it should succeed */
2523 hr = CoGetClassObject(&CLSID_WineOOPTest, CLSCTX_INPROC_SERVER,
2524 NULL, &IID_IClassFactory, (LPVOID*)&cf);
2525 ok_ole_success(hr, CoGetClassObject);
2527 /* Now check the locking is working */
2528 /* NOTE: we are accessing the class directly, not through a proxy */
2530 ok_no_locks();
2532 hr = IClassFactory_LockServer(cf, TRUE);
2533 ok_ole_success(hr, IClassFactory_LockServer);
2535 ok_more_than_one_lock();
2537 IClassFactory_LockServer(cf, FALSE);
2538 ok_ole_success(hr, IClassFactory_LockServer);
2540 ok_no_locks();
2542 IClassFactory_Release(cf);
2544 /* wait for shutdown signal */
2545 ret = WaitForSingleObject(heventShutdown, 0);
2546 ok(ret != WAIT_TIMEOUT, "Server didn't shut down\n");
2548 /* try to connect again after SCM has suspended registered class objects */
2549 hr = CoGetClassObject(&CLSID_WineOOPTest, CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER, NULL,
2550 &IID_IClassFactory, (LPVOID*)&cf);
2551 ok(hr == CO_E_SERVER_STOPPING || /* NT */
2552 hr == S_OK /* Win9x */,
2553 "CoGetClassObject should have returned CO_E_SERVER_STOPPING instead of 0x%08x\n", hr);
2555 hr = CoRevokeClassObject(cookie);
2556 ok_ole_success(hr, CoRevokeClassObject);
2558 CloseHandle(heventShutdown);
2560 process = create_target_process("-Embedding");
2561 ok(process != NULL, "couldn't start local server process, error was %d\n", GetLastError());
2563 ready_event = CreateEvent(NULL, FALSE, FALSE, "Wine COM Test Ready Event");
2564 WaitForSingleObject(ready_event, INFINITE);
2565 CloseHandle(ready_event);
2567 hr = CoCreateInstance(&CLSID_WineOOPTest, NULL, CLSCTX_LOCAL_SERVER, &IID_IClassFactory, (void **)&cf);
2568 ok_ole_success(hr, CoCreateInstance);
2570 IClassFactory_Release(cf);
2572 hr = CoCreateInstance(&CLSID_WineOOPTest, NULL, CLSCTX_LOCAL_SERVER, &IID_IClassFactory, (void **)&cf);
2573 ok(hr == REGDB_E_CLASSNOTREG, "Second CoCreateInstance on REGCLS_SINGLEUSE object should have failed\n");
2575 quit_event = CreateEvent(NULL, FALSE, FALSE, "Wine COM Test Quit Event");
2576 SetEvent(quit_event);
2578 winetest_wait_child_process( process );
2579 CloseHandle(quit_event);
2580 CloseHandle(process);
2583 struct git_params
2585 DWORD cookie;
2586 IGlobalInterfaceTable *git;
2589 static DWORD CALLBACK get_global_interface_proc(LPVOID pv)
2591 HRESULT hr;
2592 struct git_params *params = (struct git_params *)pv;
2593 IClassFactory *cf;
2595 hr = IGlobalInterfaceTable_GetInterfaceFromGlobal(params->git, params->cookie, &IID_IClassFactory, (void **)&cf);
2596 ok(hr == CO_E_NOTINITIALIZED,
2597 "IGlobalInterfaceTable_GetInterfaceFromGlobal should have failed with error CO_E_NOTINITIALIZED instead of 0x%08x\n",
2598 hr);
2600 CoInitialize(NULL);
2602 hr = IGlobalInterfaceTable_GetInterfaceFromGlobal(params->git, params->cookie, &IID_IClassFactory, (void **)&cf);
2603 ok_ole_success(hr, IGlobalInterfaceTable_GetInterfaceFromGlobal);
2605 IClassFactory_Release(cf);
2607 CoUninitialize();
2609 return hr;
2612 static void test_globalinterfacetable(void)
2614 HRESULT hr;
2615 IGlobalInterfaceTable *git;
2616 DWORD cookie;
2617 HANDLE thread;
2618 DWORD tid;
2619 struct git_params params;
2620 DWORD ret;
2621 IUnknown *object;
2623 trace("test_globalinterfacetable\n");
2624 cLocks = 0;
2626 hr = CoCreateInstance(&CLSID_StdGlobalInterfaceTable, NULL, CLSCTX_INPROC_SERVER, &IID_IGlobalInterfaceTable, (void **)&git);
2627 ok_ole_success(hr, CoCreateInstance);
2629 hr = IGlobalInterfaceTable_RegisterInterfaceInGlobal(git, (IUnknown *)&Test_ClassFactory, &IID_IClassFactory, &cookie);
2630 ok_ole_success(hr, IGlobalInterfaceTable_RegisterInterfaceInGlobal);
2632 ok_more_than_one_lock();
2634 params.cookie = cookie;
2635 params.git = git;
2636 /* note: params is on stack so we MUST wait for get_global_interface_proc
2637 * to exit before we can return */
2638 thread = CreateThread(NULL, 0, get_global_interface_proc, &params, 0, &tid);
2640 ret = MsgWaitForMultipleObjects(1, &thread, FALSE, INFINITE, QS_ALLINPUT);
2641 while (ret == WAIT_OBJECT_0 + 1)
2643 MSG msg;
2644 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
2645 DispatchMessage(&msg);
2646 ret = MsgWaitForMultipleObjects(1, &thread, FALSE, INFINITE, QS_ALLINPUT);
2649 CloseHandle(thread);
2651 /* test getting interface from global with different iid */
2652 hr = IGlobalInterfaceTable_GetInterfaceFromGlobal(git, cookie, &IID_IUnknown, (void **)&object);
2653 ok_ole_success(hr, IGlobalInterfaceTable_GetInterfaceFromGlobal);
2654 IUnknown_Release(object);
2656 /* test getting interface from global with same iid */
2657 hr = IGlobalInterfaceTable_GetInterfaceFromGlobal(git, cookie, &IID_IClassFactory, (void **)&object);
2658 ok_ole_success(hr, IGlobalInterfaceTable_GetInterfaceFromGlobal);
2659 IUnknown_Release(object);
2661 hr = IGlobalInterfaceTable_RevokeInterfaceFromGlobal(git, cookie);
2662 ok_ole_success(hr, IGlobalInterfaceTable_RevokeInterfaceFromGlobal);
2664 ok_no_locks();
2666 IGlobalInterfaceTable_Release(git);
2669 static const char *debugstr_iid(REFIID riid)
2671 static char name[256];
2672 HKEY hkeyInterface;
2673 WCHAR bufferW[39];
2674 char buffer[39];
2675 LONG name_size = sizeof(name);
2676 StringFromGUID2(riid, bufferW, sizeof(bufferW)/sizeof(bufferW[0]));
2677 WideCharToMultiByte(CP_ACP, 0, bufferW, sizeof(bufferW)/sizeof(bufferW[0]), buffer, sizeof(buffer), NULL, NULL);
2678 if (RegOpenKeyEx(HKEY_CLASSES_ROOT, "Interface", 0, KEY_QUERY_VALUE, &hkeyInterface) != ERROR_SUCCESS)
2680 memcpy(name, buffer, sizeof(buffer));
2681 goto done;
2683 if (RegQueryValue(hkeyInterface, buffer, name, &name_size) != ERROR_SUCCESS)
2685 memcpy(name, buffer, sizeof(buffer));
2686 goto done;
2688 RegCloseKey(hkeyInterface);
2689 done:
2690 return name;
2693 static HRESULT WINAPI TestChannelHook_QueryInterface(IChannelHook *iface, REFIID riid, void **ppv)
2695 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IChannelHook))
2697 *ppv = iface;
2698 IUnknown_AddRef(iface);
2699 return S_OK;
2702 *ppv = NULL;
2703 return E_NOINTERFACE;
2706 static ULONG WINAPI TestChannelHook_AddRef(IChannelHook *iface)
2708 return 2;
2711 static ULONG WINAPI TestChannelHook_Release(IChannelHook *iface)
2713 return 1;
2716 static void WINAPI TestChannelHook_ClientGetSize(
2717 IChannelHook *iface,
2718 REFGUID uExtent,
2719 REFIID riid,
2720 ULONG *pDataSize )
2722 SChannelHookCallInfo *info = (SChannelHookCallInfo *)riid;
2723 trace("TestChannelHook_ClientGetBuffer\n");
2724 trace("\t%s method %d\n", debugstr_iid(riid), info->iMethod);
2725 trace("\tcid: %s\n", debugstr_iid(&info->uCausality));
2726 ok(info->cbSize == sizeof(*info), "info->cbSize was %d instead of %d\n", info->cbSize, (int)sizeof(*info));
2727 ok(info->dwServerPid == GetCurrentProcessId(), "info->dwServerPid was 0x%x instead of 0x%x\n", info->dwServerPid, GetCurrentProcessId());
2728 ok(!info->pObject, "info->pObject should be NULL\n");
2729 ok(IsEqualGUID(uExtent, &EXTENTID_WineTest), "uExtent wasn't correct\n");
2731 *pDataSize = 1;
2734 static void WINAPI TestChannelHook_ClientFillBuffer(
2735 IChannelHook *iface,
2736 REFGUID uExtent,
2737 REFIID riid,
2738 ULONG *pDataSize,
2739 void *pDataBuffer )
2741 SChannelHookCallInfo *info = (SChannelHookCallInfo *)riid;
2742 trace("TestChannelHook_ClientFillBuffer\n");
2743 ok(info->cbSize == sizeof(*info), "info->cbSize was %d instead of %d\n", info->cbSize, (int)sizeof(*info));
2744 ok(info->dwServerPid == GetCurrentProcessId(), "info->dwServerPid was 0x%x instead of 0x%x\n", info->dwServerPid, GetCurrentProcessId());
2745 ok(!info->pObject, "info->pObject should be NULL\n");
2746 ok(IsEqualGUID(uExtent, &EXTENTID_WineTest), "uExtent wasn't correct\n");
2748 *(unsigned char *)pDataBuffer = 0xcc;
2749 *pDataSize = 1;
2752 static void WINAPI TestChannelHook_ClientNotify(
2753 IChannelHook *iface,
2754 REFGUID uExtent,
2755 REFIID riid,
2756 ULONG cbDataSize,
2757 void *pDataBuffer,
2758 DWORD lDataRep,
2759 HRESULT hrFault )
2761 SChannelHookCallInfo *info = (SChannelHookCallInfo *)riid;
2762 trace("TestChannelHook_ClientNotify hrFault = 0x%08x\n", hrFault);
2763 ok(info->cbSize == sizeof(*info), "info->cbSize was %d instead of %d\n", info->cbSize, (int)sizeof(*info));
2764 ok(info->dwServerPid == GetCurrentProcessId(), "info->dwServerPid was 0x%x instead of 0x%x\n", info->dwServerPid, GetCurrentProcessId());
2765 todo_wine {
2766 ok(info->pObject != NULL, "info->pObject shouldn't be NULL\n");
2768 ok(IsEqualGUID(uExtent, &EXTENTID_WineTest), "uExtent wasn't correct\n");
2771 static void WINAPI TestChannelHook_ServerNotify(
2772 IChannelHook *iface,
2773 REFGUID uExtent,
2774 REFIID riid,
2775 ULONG cbDataSize,
2776 void *pDataBuffer,
2777 DWORD lDataRep )
2779 SChannelHookCallInfo *info = (SChannelHookCallInfo *)riid;
2780 trace("TestChannelHook_ServerNotify\n");
2781 ok(info->cbSize == sizeof(*info), "info->cbSize was %d instead of %d\n", info->cbSize, (int)sizeof(*info));
2782 ok(info->dwServerPid == GetCurrentProcessId(), "info->dwServerPid was 0x%x instead of 0x%x\n", info->dwServerPid, GetCurrentProcessId());
2783 ok(info->pObject != NULL, "info->pObject shouldn't be NULL\n");
2784 ok(cbDataSize == 1, "cbDataSize should have been 1 instead of %d\n", cbDataSize);
2785 ok(*(unsigned char *)pDataBuffer == 0xcc, "pDataBuffer should have contained 0xcc instead of 0x%x\n", *(unsigned char *)pDataBuffer);
2786 ok(IsEqualGUID(uExtent, &EXTENTID_WineTest), "uExtent wasn't correct\n");
2789 static void WINAPI TestChannelHook_ServerGetSize(
2790 IChannelHook *iface,
2791 REFGUID uExtent,
2792 REFIID riid,
2793 HRESULT hrFault,
2794 ULONG *pDataSize )
2796 SChannelHookCallInfo *info = (SChannelHookCallInfo *)riid;
2797 trace("TestChannelHook_ServerGetSize\n");
2798 trace("\t%s method %d\n", debugstr_iid(riid), info->iMethod);
2799 ok(info->cbSize == sizeof(*info), "info->cbSize was %d instead of %d\n", info->cbSize, (int)sizeof(*info));
2800 ok(info->dwServerPid == GetCurrentProcessId(), "info->dwServerPid was 0x%x instead of 0x%x\n", info->dwServerPid, GetCurrentProcessId());
2801 ok(info->pObject != NULL, "info->pObject shouldn't be NULL\n");
2802 ok(IsEqualGUID(uExtent, &EXTENTID_WineTest), "uExtent wasn't correct\n");
2803 if (hrFault != S_OK)
2804 trace("\thrFault = 0x%08x\n", hrFault);
2806 *pDataSize = 0;
2809 static void WINAPI TestChannelHook_ServerFillBuffer(
2810 IChannelHook *iface,
2811 REFGUID uExtent,
2812 REFIID riid,
2813 ULONG *pDataSize,
2814 void *pDataBuffer,
2815 HRESULT hrFault )
2817 trace("TestChannelHook_ServerFillBuffer\n");
2818 ok(0, "TestChannelHook_ServerFillBuffer shouldn't be called\n");
2821 static const IChannelHookVtbl TestChannelHookVtbl =
2823 TestChannelHook_QueryInterface,
2824 TestChannelHook_AddRef,
2825 TestChannelHook_Release,
2826 TestChannelHook_ClientGetSize,
2827 TestChannelHook_ClientFillBuffer,
2828 TestChannelHook_ClientNotify,
2829 TestChannelHook_ServerNotify,
2830 TestChannelHook_ServerGetSize,
2831 TestChannelHook_ServerFillBuffer,
2834 static IChannelHook TestChannelHook = { &TestChannelHookVtbl };
2836 static void test_channel_hook(void)
2838 IStream *pStream = NULL;
2839 IClassFactory *cf = NULL;
2840 DWORD tid;
2841 IUnknown *proxy = NULL;
2842 HANDLE thread;
2843 HRESULT hr;
2845 hr = CoRegisterChannelHook(&EXTENTID_WineTest, &TestChannelHook);
2846 ok_ole_success(hr, CoRegisterChannelHook);
2848 hr = CoRegisterMessageFilter(&MessageFilter, NULL);
2849 ok_ole_success(hr, CoRegisterMessageFilter);
2851 cLocks = 0;
2853 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
2854 ok_ole_success(hr, CreateStreamOnHGlobal);
2855 tid = start_host_object2(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &MessageFilter, &thread);
2857 ok_more_than_one_lock();
2859 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
2860 hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&cf);
2861 ok_ole_success(hr, CoUnmarshalInterface);
2862 IStream_Release(pStream);
2864 ok_more_than_one_lock();
2866 hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
2867 ok_ole_success(hr, IClassFactory_CreateInstance);
2868 IUnknown_Release(proxy);
2870 IClassFactory_Release(cf);
2872 ok_no_locks();
2874 end_host_object(tid, thread);
2876 hr = CoRegisterMessageFilter(NULL, NULL);
2877 ok_ole_success(hr, CoRegisterMessageFilter);
2880 START_TEST(marshal)
2882 HMODULE hOle32 = GetModuleHandle("ole32");
2883 int argc;
2884 char **argv;
2886 if (!(pCoInitializeEx = (void*)GetProcAddress(hOle32, "CoInitializeEx"))) goto no_test;
2888 argc = winetest_get_mainargs( &argv );
2889 if (argc > 2 && (!strcmp(argv[2], "-Embedding")))
2891 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
2892 test_register_local_server();
2893 CoUninitialize();
2895 return;
2898 register_test_window();
2900 test_cocreateinstance_proxy();
2902 pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
2904 /* FIXME: test CoCreateInstanceEx */
2906 /* lifecycle management and marshaling tests */
2907 test_no_marshaler();
2908 test_normal_marshal_and_release();
2909 test_normal_marshal_and_unmarshal();
2910 test_marshal_and_unmarshal_invalid();
2911 test_same_apartment_unmarshal_failure();
2912 test_interthread_marshal_and_unmarshal();
2913 test_proxy_marshal_and_unmarshal();
2914 test_proxy_marshal_and_unmarshal2();
2915 test_proxy_marshal_and_unmarshal_weak();
2916 test_proxy_marshal_and_unmarshal_strong();
2917 test_marshal_stub_apartment_shutdown();
2918 test_marshal_proxy_apartment_shutdown();
2919 test_marshal_proxy_mta_apartment_shutdown();
2920 test_no_couninitialize_server();
2921 test_no_couninitialize_client();
2922 test_tableweak_marshal_and_unmarshal_twice();
2923 test_tableweak_marshal_releasedata1();
2924 test_tableweak_marshal_releasedata2();
2925 test_tablestrong_marshal_and_unmarshal_twice();
2926 test_lock_object_external();
2927 test_disconnect_stub();
2928 test_normal_marshal_and_unmarshal_twice();
2929 test_hresult_marshaling();
2930 test_proxy_used_in_wrong_thread();
2931 test_message_filter();
2932 test_bad_marshal_stream();
2933 test_proxy_interfaces();
2934 test_stubbuffer(&IID_IClassFactory);
2935 test_proxybuffer(&IID_IClassFactory);
2936 test_message_reentrancy();
2937 test_call_from_message();
2938 test_WM_QUIT_handling();
2939 test_freethreadedmarshaler();
2940 test_inproc_handler();
2941 test_handler_marshaling();
2942 test_client_security();
2944 test_local_server();
2946 test_globalinterfacetable();
2948 /* must be last test as channel hooks can't be unregistered */
2949 test_channel_hook();
2951 CoUninitialize();
2952 return;
2954 no_test:
2955 trace("You need DCOM95 installed to run this test\n");
2956 return;