urlmon: Don't create stgmed_obj for binding to object.
[wine] / dlls / ole32 / tests / marshal.c
1 /*
2  * Marshaling Tests
3  *
4  * Copyright 2004 Robert Shearman
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #define _WIN32_DCOM
22 #define COBJMACROS
23 #define CONST_VTABLE
24
25 #include <stdarg.h>
26 #include <stdio.h>
27
28 #include "windef.h"
29 #include "winbase.h"
30 #include "objbase.h"
31 #include "shlguid.h"
32
33 #include "wine/test.h"
34
35 /* functions that are not present on all versions of Windows */
36 HRESULT (WINAPI * pCoInitializeEx)(LPVOID lpReserved, DWORD dwCoInit);
37
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)
42
43 static const IID IID_IWineTest =
44 {
45     0x5201163f,
46     0x8164,
47     0x4fd0,
48     {0xa1, 0xa2, 0x5d, 0x5a, 0x36, 0x54, 0xd3, 0xbd}
49 }; /* 5201163f-8164-4fd0-a1a2-5d5a3654d3bd */
50
51 static const IID IID_IRemUnknown =
52 {
53     0x00000131,
54     0x0000,
55     0x0000,
56     {0xc0,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46}
57 };
58
59 #define EXTENTID_WineTest IID_IWineTest
60 #define CLSID_WineTest IID_IWineTest
61
62 static const CLSID CLSID_WineOOPTest =
63 {
64     0x5201163f,
65     0x8164,
66     0x4fd0,
67     {0xa1, 0xa2, 0x5d, 0x5a, 0x36, 0x54, 0xd3, 0xbd}
68 }; /* 5201163f-8164-4fd0-a1a2-5d5a3654d3bd */
69
70 static void test_cocreateinstance_proxy(void)
71 {
72     IUnknown *pProxy;
73     IMultiQI *pMQI;
74     HRESULT hr;
75
76     pCoInitializeEx(NULL, COINIT_MULTITHREADED);
77
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);
85
86     CoUninitialize();
87 }
88
89 static const LARGE_INTEGER ullZero;
90 static LONG cLocks;
91
92 static void LockModule(void)
93 {
94     InterlockedIncrement(&cLocks);
95 }
96
97 static void UnlockModule(void)
98 {
99     InterlockedDecrement(&cLocks);
100 }
101
102
103 static HRESULT WINAPI Test_IUnknown_QueryInterface(
104     LPUNKNOWN iface,
105     REFIID riid,
106     LPVOID *ppvObj)
107 {
108     if (ppvObj == NULL) return E_POINTER;
109
110     if (IsEqualGUID(riid, &IID_IUnknown))
111     {
112         *ppvObj = (LPVOID)iface;
113         IUnknown_AddRef(iface);
114         return S_OK;
115     }
116
117     *ppvObj = NULL;
118     return E_NOINTERFACE;
119 }
120
121 static ULONG WINAPI Test_IUnknown_AddRef(LPUNKNOWN iface)
122 {
123     LockModule();
124     return 2; /* non-heap-based object */
125 }
126
127 static ULONG WINAPI Test_IUnknown_Release(LPUNKNOWN iface)
128 {
129     UnlockModule();
130     return 1; /* non-heap-based object */
131 }
132
133 static const IUnknownVtbl TestUnknown_Vtbl =
134 {
135     Test_IUnknown_QueryInterface,
136     Test_IUnknown_AddRef,
137     Test_IUnknown_Release,
138 };
139
140 static IUnknown Test_Unknown = { &TestUnknown_Vtbl };
141
142
143 static HRESULT WINAPI Test_IClassFactory_QueryInterface(
144     LPCLASSFACTORY iface,
145     REFIID riid,
146     LPVOID *ppvObj)
147 {
148     if (ppvObj == NULL) return E_POINTER;
149
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))
154     {
155         *ppvObj = (LPVOID)iface;
156         IClassFactory_AddRef(iface);
157         return S_OK;
158     }
159
160     *ppvObj = NULL;
161     return E_NOINTERFACE;
162 }
163
164 static ULONG WINAPI Test_IClassFactory_AddRef(LPCLASSFACTORY iface)
165 {
166     LockModule();
167     return 2; /* non-heap-based object */
168 }
169
170 static ULONG WINAPI Test_IClassFactory_Release(LPCLASSFACTORY iface)
171 {
172     UnlockModule();
173     return 1; /* non-heap-based object */
174 }
175
176 static HRESULT WINAPI Test_IClassFactory_CreateInstance(
177     LPCLASSFACTORY iface,
178     LPUNKNOWN pUnkOuter,
179     REFIID riid,
180     LPVOID *ppvObj)
181 {
182     if (pUnkOuter) return CLASS_E_NOAGGREGATION;
183     return IUnknown_QueryInterface((IUnknown*)&Test_Unknown, riid, ppvObj);
184 }
185
186 static HRESULT WINAPI Test_IClassFactory_LockServer(
187     LPCLASSFACTORY iface,
188     BOOL fLock)
189 {
190     return S_OK;
191 }
192
193 static const IClassFactoryVtbl TestClassFactory_Vtbl =
194 {
195     Test_IClassFactory_QueryInterface,
196     Test_IClassFactory_AddRef,
197     Test_IClassFactory_Release,
198     Test_IClassFactory_CreateInstance,
199     Test_IClassFactory_LockServer
200 };
201
202 static IClassFactory Test_ClassFactory = { &TestClassFactory_Vtbl };
203
204 #define RELEASEMARSHALDATA WM_USER
205
206 struct host_object_data
207 {
208     IStream *stream;
209     IID iid;
210     IUnknown *object;
211     MSHLFLAGS marshal_flags;
212     HANDLE marshal_event;
213     IMessageFilter *filter;
214 };
215
216 static DWORD CALLBACK host_object_proc(LPVOID p)
217 {
218     struct host_object_data *data = (struct host_object_data *)p;
219     HRESULT hr;
220     MSG msg;
221
222     pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
223
224     if (data->filter)
225     {
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);
230     }
231
232     hr = CoMarshalInterface(data->stream, &data->iid, data->object, MSHCTX_INPROC, NULL, data->marshal_flags);
233     ok_ole_success(hr, CoMarshalInterface);
234
235     /* force the message queue to be created before signaling parent thread */
236     PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
237
238     SetEvent(data->marshal_event);
239
240     while (GetMessage(&msg, NULL, 0, 0))
241     {
242         if (msg.hwnd == NULL && msg.message == RELEASEMARSHALDATA)
243         {
244             CoReleaseMarshalData(data->stream);
245             SetEvent((HANDLE)msg.lParam);
246         }
247         else
248             DispatchMessage(&msg);
249     }
250
251     HeapFree(GetProcessHeap(), 0, data);
252
253     CoUninitialize();
254
255     return hr;
256 }
257
258 static DWORD start_host_object2(IStream *stream, REFIID riid, IUnknown *object, MSHLFLAGS marshal_flags, IMessageFilter *filter, HANDLE *thread)
259 {
260     DWORD tid = 0;
261     HANDLE marshal_event = CreateEvent(NULL, FALSE, FALSE, NULL);
262     struct host_object_data *data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data));
263
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;
270
271     *thread = CreateThread(NULL, 0, host_object_proc, data, 0, &tid);
272
273     /* wait for marshaling to complete before returning */
274     WaitForSingleObject(marshal_event, INFINITE);
275     CloseHandle(marshal_event);
276
277     return tid;
278 }
279
280 static DWORD start_host_object(IStream *stream, REFIID riid, IUnknown *object, MSHLFLAGS marshal_flags, HANDLE *thread)
281 {
282     return start_host_object2(stream, riid, object, marshal_flags, NULL, thread);
283 }
284
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)
288 {
289     HANDLE event = CreateEvent(NULL, FALSE, FALSE, NULL);
290     PostThreadMessage(tid, RELEASEMARSHALDATA, 0, (LPARAM)event);
291     WaitForSingleObject(event, INFINITE);
292     CloseHandle(event);
293 }
294
295 static void end_host_object(DWORD tid, HANDLE thread)
296 {
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);
302 }
303
304 /* tests failure case of interface not having a marshaler specified in the
305  * registry */
306 static void test_no_marshaler(void)
307 {
308     IStream *pStream;
309     HRESULT hr;
310
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);
315
316     IStream_Release(pStream);
317 }
318
319 /* tests normal marshal and then release without unmarshaling */
320 static void test_normal_marshal_and_release(void)
321 {
322     HRESULT hr;
323     IStream *pStream = NULL;
324
325     cLocks = 0;
326
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);
331
332     ok_more_than_one_lock();
333
334     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
335     hr = CoReleaseMarshalData(pStream);
336     ok_ole_success(hr, CoReleaseMarshalData);
337     IStream_Release(pStream);
338
339     ok_no_locks();
340 }
341
342 /* tests success case of a same-thread marshal and unmarshal */
343 static void test_normal_marshal_and_unmarshal(void)
344 {
345     HRESULT hr;
346     IStream *pStream = NULL;
347     IUnknown *pProxy = NULL;
348
349     cLocks = 0;
350
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);
355
356     ok_more_than_one_lock();
357     
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);
362
363     ok_more_than_one_lock();
364
365     IUnknown_Release(pProxy);
366
367     ok_no_locks();
368 }
369
370 /* tests failure case of unmarshaling a freed object */
371 static void test_marshal_and_unmarshal_invalid(void)
372 {
373     HRESULT hr;
374     IStream *pStream = NULL;
375     IClassFactory *pProxy = NULL;
376     DWORD tid;
377     void * dummy;
378     HANDLE thread;
379
380     cLocks = 0;
381
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);
385
386     ok_more_than_one_lock();
387         
388     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
389     hr = CoReleaseMarshalData(pStream);
390     ok_ole_success(hr, CoReleaseMarshalData);
391
392     ok_no_locks();
393
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); }
397
398     ok_no_locks();
399
400     if (pProxy)
401     {
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);
404
405         IClassFactory_Release(pProxy);
406     }
407
408     IStream_Release(pStream);
409
410     end_host_object(tid, thread);
411 }
412
413 static void test_same_apartment_unmarshal_failure(void)
414 {
415     HRESULT hr;
416     IStream *pStream;
417     IUnknown *pProxy;
418     static const LARGE_INTEGER llZero;
419
420     cLocks = 0;
421
422     hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
423     ok_ole_success(hr, CreateStreamOnHGlobal);
424
425     hr = CoMarshalInterface(pStream, &IID_IUnknown, (IUnknown *)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
426     ok_ole_success(hr, CoMarshalInterface);
427
428     ok_more_than_one_lock();
429
430     hr = IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
431     ok_ole_success(hr, IStream_Seek);
432
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);
435
436     ok_no_locks();
437
438     IStream_Release(pStream);
439 }
440
441 /* tests success case of an interthread marshal */
442 static void test_interthread_marshal_and_unmarshal(void)
443 {
444     HRESULT hr;
445     IStream *pStream = NULL;
446     IUnknown *pProxy = NULL;
447     DWORD tid;
448     HANDLE thread;
449
450     cLocks = 0;
451
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);
455
456     ok_more_than_one_lock();
457     
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);
462
463     ok_more_than_one_lock();
464
465     IUnknown_Release(pProxy);
466
467     ok_no_locks();
468
469     end_host_object(tid, thread);
470 }
471
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
475
476 /* tests success case of an interthread marshal and then marshaling the proxy */
477 static void test_proxy_marshal_and_unmarshal(void)
478 {
479     HRESULT hr;
480     IStream *pStream = NULL;
481     IUnknown *pProxy = NULL;
482     IUnknown *pProxy2 = NULL;
483     DWORD tid;
484     HANDLE thread;
485     int i;
486
487     cLocks = 0;
488
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);
492
493     ok_more_than_one_lock();
494     
495     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
496     hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
497     ok_ole_success(hr, CoUnmarshalInterface);
498
499     ok_more_than_one_lock();
500
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);
505
506     ok_more_than_one_lock();
507
508     /* marshal 5 more times to exhaust the normal external references of 5 */
509     for (i = 0; i < NORMALEXTREFS; i++)
510     {
511         hr = CoMarshalInterface(pStream, &IID_IClassFactory, pProxy, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
512         ok_ole_success(hr, CoMarshalInterface);
513     }
514
515     ok_more_than_one_lock();
516
517     /* release the original proxy to test that we successfully keep the
518      * original object alive */
519     IUnknown_Release(pProxy);
520
521     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
522     hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
523     ok_ole_success(hr, CoUnmarshalInterface);
524
525     ok_more_than_one_lock();
526
527     IUnknown_Release(pProxy2);
528
529     /* unmarshal all of the proxies to check that the object stub still exists */
530     for (i = 0; i < NORMALEXTREFS; i++)
531     {
532         hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
533         ok_ole_success(hr, CoUnmarshalInterface);
534
535         IUnknown_Release(pProxy2);
536     }
537
538     ok_no_locks();
539
540     IStream_Release(pStream);
541
542     end_host_object(tid, thread);
543 }
544
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)
548 {
549     HRESULT hr;
550     IStream *pStream = NULL;
551     IUnknown *pProxy = NULL;
552     IUnknown *pProxy2 = NULL;
553     DWORD tid;
554     HANDLE thread;
555
556     cLocks = 0;
557
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);
561
562     ok_more_than_one_lock();
563
564     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
565     hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)&pProxy);
566     ok_ole_success(hr, CoUnmarshalInterface);
567
568     ok_more_than_one_lock();
569
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);
574
575     ok_more_than_one_lock();
576
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);
582
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
590      */
591
592     ok_more_than_one_lock();
593
594     IUnknown_Release(pProxy);
595
596     ok_more_than_one_lock();
597
598     IUnknown_Release(pProxy2);
599
600     ok_no_locks();
601
602     end_host_object(tid, thread);
603 }
604
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)
607 {
608     HRESULT hr;
609     IStream *pStream = NULL;
610     IUnknown *pProxy = NULL;
611     IUnknown *pProxy2 = NULL;
612     DWORD tid;
613     HANDLE thread;
614
615     cLocks = 0;
616
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);
620
621     ok_more_than_one_lock();
622
623     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
624     hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
625     ok_ole_success(hr, CoUnmarshalInterface);
626
627     ok_more_than_one_lock();
628
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);
633
634     ok_more_than_one_lock();
635
636     /* release the original proxy to test that we successfully keep the
637      * original object alive */
638     IUnknown_Release(pProxy);
639
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);
644
645     ok_no_locks();
646
647     IStream_Release(pStream);
648
649     end_host_object(tid, thread);
650 }
651
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)
654 {
655     HRESULT hr;
656     IStream *pStream = NULL;
657     IUnknown *pProxy = NULL;
658     IUnknown *pProxy2 = NULL;
659     DWORD tid;
660     HANDLE thread;
661
662     cLocks = 0;
663
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);
667
668     ok_more_than_one_lock();
669
670     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
671     hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
672     ok_ole_success(hr, CoUnmarshalInterface);
673
674     ok_more_than_one_lock();
675
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))
682     {
683         IUnknown_Release(pProxy);
684         goto end;
685     }
686
687     ok_more_than_one_lock();
688
689     /* release the original proxy to test that we successfully keep the
690      * original object alive */
691     IUnknown_Release(pProxy);
692
693     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
694     hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
695     ok_ole_success(hr, CoUnmarshalInterface);
696
697     ok_more_than_one_lock();
698
699     IUnknown_Release(pProxy2);
700
701     ok_more_than_one_lock();
702
703 end:
704     IStream_Release(pStream);
705
706     end_host_object(tid, thread);
707
708     ok_no_locks();
709 }
710
711 /* tests that stubs are released when the containing apartment is destroyed */
712 static void test_marshal_stub_apartment_shutdown(void)
713 {
714     HRESULT hr;
715     IStream *pStream = NULL;
716     IUnknown *pProxy = NULL;
717     DWORD tid;
718     HANDLE thread;
719
720     cLocks = 0;
721
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);
725
726     ok_more_than_one_lock();
727     
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);
732
733     ok_more_than_one_lock();
734
735     end_host_object(tid, thread);
736
737     ok_no_locks();
738
739     IUnknown_Release(pProxy);
740
741     ok_no_locks();
742 }
743
744 /* tests that proxies are released when the containing apartment is destroyed */
745 static void test_marshal_proxy_apartment_shutdown(void)
746 {
747     HRESULT hr;
748     IStream *pStream = NULL;
749     IUnknown *pProxy = NULL;
750     DWORD tid;
751     HANDLE thread;
752
753     cLocks = 0;
754
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);
758
759     ok_more_than_one_lock();
760     
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);
765
766     ok_more_than_one_lock();
767
768     CoUninitialize();
769
770     ok_no_locks();
771
772     IUnknown_Release(pProxy);
773
774     ok_no_locks();
775
776     end_host_object(tid, thread);
777
778     pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
779 }
780
781 /* tests that proxies are released when the containing mta apartment is destroyed */
782 static void test_marshal_proxy_mta_apartment_shutdown(void)
783 {
784     HRESULT hr;
785     IStream *pStream = NULL;
786     IUnknown *pProxy = NULL;
787     DWORD tid;
788     HANDLE thread;
789
790     CoUninitialize();
791     pCoInitializeEx(NULL, COINIT_MULTITHREADED);
792
793     cLocks = 0;
794
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);
798
799     ok_more_than_one_lock();
800
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);
805
806     ok_more_than_one_lock();
807
808     CoUninitialize();
809
810     ok_no_locks();
811
812     IUnknown_Release(pProxy);
813
814     ok_no_locks();
815
816     end_host_object(tid, thread);
817
818     pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
819 }
820
821 struct ncu_params
822 {
823     LPSTREAM stream;
824     HANDLE marshal_event;
825     HANDLE unmarshal_event;
826 };
827
828 /* helper for test_no_couninitialize_server */
829 static DWORD CALLBACK no_couninitialize_server_proc(LPVOID p)
830 {
831     struct ncu_params *ncu_params = (struct ncu_params *)p;
832     HRESULT hr;
833
834     pCoInitializeEx(NULL, COINIT_MULTITHREADED);
835
836     hr = CoMarshalInterface(ncu_params->stream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
837     ok_ole_success(hr, CoMarshalInterface);
838
839     SetEvent(ncu_params->marshal_event);
840
841     WaitForSingleObject(ncu_params->unmarshal_event, INFINITE);
842
843     /* die without calling CoUninitialize */
844
845     return 0;
846 }
847
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)
851 {
852     HRESULT hr;
853     IStream *pStream = NULL;
854     IUnknown *pProxy = NULL;
855     DWORD tid;
856     HANDLE thread;
857     struct ncu_params ncu_params;
858
859     cLocks = 0;
860
861     ncu_params.marshal_event = CreateEvent(NULL, TRUE, FALSE, NULL);
862     ncu_params.unmarshal_event = CreateEvent(NULL, TRUE, FALSE, NULL);
863
864     hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
865     ok_ole_success(hr, CreateStreamOnHGlobal);
866     ncu_params.stream = pStream;
867
868     thread = CreateThread(NULL, 0, no_couninitialize_server_proc, &ncu_params, 0, &tid);
869
870     WaitForSingleObject(ncu_params.marshal_event, INFINITE);
871     ok_more_than_one_lock();
872
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);
877
878     ok_more_than_one_lock();
879
880     SetEvent(ncu_params.unmarshal_event);
881     WaitForSingleObject(thread, INFINITE);
882
883     ok_no_locks();
884
885     CloseHandle(thread);
886     CloseHandle(ncu_params.marshal_event);
887     CloseHandle(ncu_params.unmarshal_event);
888
889     IUnknown_Release(pProxy);
890
891     ok_no_locks();
892 }
893
894 /* STA -> STA call during DLL_THREAD_DETACH */
895 static DWORD CALLBACK no_couninitialize_client_proc(LPVOID p)
896 {
897     struct ncu_params *ncu_params = (struct ncu_params *)p;
898     HRESULT hr;
899     IUnknown *pProxy = NULL;
900
901     pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
902
903     hr = CoUnmarshalInterface(ncu_params->stream, &IID_IClassFactory, (void **)&pProxy);
904     ok_ole_success(hr, CoUnmarshalInterface);
905     IStream_Release(ncu_params->stream);
906
907     ok_more_than_one_lock();
908
909     /* die without calling CoUninitialize */
910
911     return 0;
912 }
913
914 /* tests STA -> STA call during DLL_THREAD_DETACH doesn't deadlock */
915 static void test_no_couninitialize_client(void)
916 {
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;
924
925     cLocks = 0;
926
927     hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
928     ok_ole_success(hr, CreateStreamOnHGlobal);
929     ncu_params.stream = pStream;
930
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);
935
936     ok_more_than_one_lock();
937
938     thread = CreateThread(NULL, 0, no_couninitialize_client_proc, &ncu_params, 0, &tid);
939
940     WaitForSingleObject(thread, INFINITE);
941     CloseHandle(thread);
942
943     ok_no_locks();
944
945     end_host_object(host_tid, host_thread);
946 }
947
948 /* tests success case of a same-thread table-weak marshal, unmarshal, unmarshal */
949 static void test_tableweak_marshal_and_unmarshal_twice(void)
950 {
951     HRESULT hr;
952     IStream *pStream = NULL;
953     IUnknown *pProxy1 = NULL;
954     IUnknown *pProxy2 = NULL;
955     DWORD tid;
956     HANDLE thread;
957
958     cLocks = 0;
959
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);
963
964     ok_more_than_one_lock();
965
966     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
967     hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy1);
968     ok_ole_success(hr, CoUnmarshalInterface);
969
970     ok_more_than_one_lock();
971
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);
976
977     ok_more_than_one_lock();
978
979     IUnknown_Release(pProxy1);
980     IUnknown_Release(pProxy2);
981
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();
986
987     end_host_object(tid, thread);
988 }
989
990 /* tests releasing after unmarshaling one object */
991 static void test_tableweak_marshal_releasedata1(void)
992 {
993     HRESULT hr;
994     IStream *pStream = NULL;
995     IUnknown *pProxy1 = NULL;
996     IUnknown *pProxy2 = NULL;
997     DWORD tid;
998     HANDLE thread;
999
1000     cLocks = 0;
1001
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);
1005
1006     ok_more_than_one_lock();
1007
1008     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1009     hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy1);
1010     ok_ole_success(hr, CoUnmarshalInterface);
1011
1012     ok_more_than_one_lock();
1013
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);
1018
1019     ok_more_than_one_lock();
1020
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);
1025
1026     ok_more_than_one_lock();
1027
1028     IUnknown_Release(pProxy1);
1029     if (pProxy2)
1030         IUnknown_Release(pProxy2);
1031
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();
1036
1037     end_host_object(tid, thread);
1038 }
1039
1040 /* tests releasing after unmarshaling one object */
1041 static void test_tableweak_marshal_releasedata2(void)
1042 {
1043     HRESULT hr;
1044     IStream *pStream = NULL;
1045     IUnknown *pProxy = NULL;
1046     DWORD tid;
1047     HANDLE thread;
1048
1049     cLocks = 0;
1050
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);
1054
1055     ok_more_than_one_lock();
1056
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);
1061
1062     ok_no_locks();
1063
1064     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1065     hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
1066     todo_wine
1067     {
1068     ok(hr == CO_E_OBJNOTREG,
1069        "CoUnmarshalInterface should have failed with CO_E_OBJNOTREG, but returned 0x%08x instead\n",
1070        hr);
1071     }
1072     IStream_Release(pStream);
1073
1074     ok_no_locks();
1075
1076     end_host_object(tid, thread);
1077 }
1078
1079 /* tests success case of a same-thread table-strong marshal, unmarshal, unmarshal */
1080 static void test_tablestrong_marshal_and_unmarshal_twice(void)
1081 {
1082     HRESULT hr;
1083     IStream *pStream = NULL;
1084     IUnknown *pProxy1 = NULL;
1085     IUnknown *pProxy2 = NULL;
1086     DWORD tid;
1087     HANDLE thread;
1088
1089     cLocks = 0;
1090
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);
1094
1095     ok_more_than_one_lock();
1096
1097     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1098     hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy1);
1099     ok_ole_success(hr, CoUnmarshalInterface);
1100
1101     ok_more_than_one_lock();
1102
1103     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1104     hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
1105     ok_ole_success(hr, CoUnmarshalInterface);
1106
1107     ok_more_than_one_lock();
1108
1109     if (pProxy1) IUnknown_Release(pProxy1);
1110     if (pProxy2) IUnknown_Release(pProxy2);
1111
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();
1116
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);
1122
1123     ok_no_locks();
1124
1125     end_host_object(tid, thread);
1126 }
1127
1128 /* tests CoLockObjectExternal */
1129 static void test_lock_object_external(void)
1130 {
1131     HRESULT hr;
1132     IStream *pStream = NULL;
1133
1134     cLocks = 0;
1135
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);
1139
1140     ok_more_than_one_lock();
1141
1142     CoDisconnectObject((IUnknown*)&Test_ClassFactory, 0);
1143
1144     ok_no_locks();
1145
1146     /* test our empty stub manager being handled correctly in
1147      * CoMarshalInterface */
1148     CoLockObjectExternal((IUnknown*)&Test_ClassFactory, TRUE, TRUE);
1149
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);
1154
1155     CoLockObjectExternal((IUnknown*)&Test_ClassFactory, TRUE, TRUE);
1156
1157     ok_more_than_one_lock();
1158
1159     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1160     hr = CoReleaseMarshalData(pStream);
1161     ok_ole_success(hr, CoReleaseMarshalData);
1162     IStream_Release(pStream);
1163
1164     ok_more_than_one_lock();
1165
1166     CoLockObjectExternal((IUnknown*)&Test_ClassFactory, FALSE, TRUE);
1167
1168     ok_more_than_one_lock();
1169
1170     CoLockObjectExternal((IUnknown*)&Test_ClassFactory, FALSE, TRUE);
1171
1172     ok_no_locks();
1173 }
1174
1175 /* tests disconnecting stubs */
1176 static void test_disconnect_stub(void)
1177 {
1178     HRESULT hr;
1179     IStream *pStream = NULL;
1180
1181     cLocks = 0;
1182
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);
1187
1188     CoLockObjectExternal((IUnknown*)&Test_ClassFactory, TRUE, TRUE);
1189
1190     ok_more_than_one_lock();
1191     
1192     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1193     hr = CoReleaseMarshalData(pStream);
1194     ok_ole_success(hr, CoReleaseMarshalData);
1195     IStream_Release(pStream);
1196
1197     ok_more_than_one_lock();
1198
1199     CoDisconnectObject((IUnknown*)&Test_ClassFactory, 0);
1200
1201     ok_no_locks();
1202 }
1203
1204 /* tests failure case of a same-thread marshal and unmarshal twice */
1205 static void test_normal_marshal_and_unmarshal_twice(void)
1206 {
1207     HRESULT hr;
1208     IStream *pStream = NULL;
1209     IUnknown *pProxy1 = NULL;
1210     IUnknown *pProxy2 = NULL;
1211
1212     cLocks = 0;
1213
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);
1218
1219     ok_more_than_one_lock();
1220     
1221     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1222     hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy1);
1223     ok_ole_success(hr, CoUnmarshalInterface);
1224
1225     ok_more_than_one_lock();
1226
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);
1231
1232     IStream_Release(pStream);
1233
1234     ok_more_than_one_lock();
1235
1236     IUnknown_Release(pProxy1);
1237
1238     ok_no_locks();
1239 }
1240
1241 /* tests success case of marshaling and unmarshaling an HRESULT */
1242 static void test_hresult_marshaling(void)
1243 {
1244     HRESULT hr;
1245     HRESULT hr_marshaled = 0;
1246     IStream *pStream = NULL;
1247     static const HRESULT E_DEADBEEF = 0xdeadbeef;
1248
1249     hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1250     ok_ole_success(hr, CreateStreamOnHGlobal);
1251
1252     hr = CoMarshalHresult(pStream, E_DEADBEEF);
1253     ok_ole_success(hr, CoMarshalHresult);
1254
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);
1258
1259     ok(hr_marshaled == E_DEADBEEF, "Didn't marshal HRESULT as expected: got value 0x%08x instead\n", hr_marshaled);
1260
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);
1265
1266     ok(hr_marshaled == E_DEADBEEF, "Didn't marshal HRESULT as expected: got value 0x%08x instead\n", hr_marshaled);
1267
1268     IStream_Release(pStream);
1269 }
1270
1271
1272 /* helper for test_proxy_used_in_wrong_thread */
1273 static DWORD CALLBACK bad_thread_proc(LPVOID p)
1274 {
1275     IClassFactory * cf = (IClassFactory *)p;
1276     HRESULT hr;
1277     IUnknown * proxy = NULL;
1278
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);
1284
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);
1290
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);
1296
1297     pCoInitializeEx(NULL, COINIT_MULTITHREADED);
1298
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);
1304
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);
1308
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");
1317
1318     CoUninitialize();
1319
1320     return 0;
1321 }
1322
1323 /* tests failure case of a using a proxy in the wrong apartment */
1324 static void test_proxy_used_in_wrong_thread(void)
1325 {
1326     HRESULT hr;
1327     IStream *pStream = NULL;
1328     IUnknown *pProxy = NULL;
1329     DWORD tid, tid2;
1330     HANDLE thread;
1331     HANDLE host_thread;
1332
1333     cLocks = 0;
1334
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);
1338
1339     ok_more_than_one_lock();
1340     
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);
1345
1346     ok_more_than_one_lock();
1347
1348     /* do a call that will fail, but result in IRemUnknown being used by the proxy */
1349     IClassFactory_QueryInterface(pProxy, &IID_IStream, (LPVOID *)&pStream);
1350
1351     /* create a thread that we can misbehave in */
1352     thread = CreateThread(NULL, 0, bad_thread_proc, (LPVOID)pProxy, 0, &tid2);
1353
1354     WaitForSingleObject(thread, INFINITE);
1355     CloseHandle(thread);
1356
1357     /* do release statement on Win9x that we should have done above */
1358     if (!GetProcAddress(GetModuleHandle("ole32"), "CoRegisterSurrogateEx"))
1359         IUnknown_Release(pProxy);
1360
1361     ok_no_locks();
1362
1363     end_host_object(tid, host_thread);
1364 }
1365
1366 static HRESULT WINAPI MessageFilter_QueryInterface(IMessageFilter *iface, REFIID riid, void ** ppvObj)
1367 {
1368     if (ppvObj == NULL) return E_POINTER;
1369
1370     if (IsEqualGUID(riid, &IID_IUnknown) ||
1371         IsEqualGUID(riid, &IID_IClassFactory))
1372     {
1373         *ppvObj = (LPVOID)iface;
1374         IClassFactory_AddRef(iface);
1375         return S_OK;
1376     }
1377
1378     return E_NOINTERFACE;
1379 }
1380
1381 static ULONG WINAPI MessageFilter_AddRef(IMessageFilter *iface)
1382 {
1383     return 2; /* non-heap object */
1384 }
1385
1386 static ULONG WINAPI MessageFilter_Release(IMessageFilter *iface)
1387 {
1388     return 1; /* non-heap object */
1389 }
1390
1391 static DWORD WINAPI MessageFilter_HandleInComingCall(
1392   IMessageFilter *iface,
1393   DWORD dwCallType,
1394   HTASK threadIDCaller,
1395   DWORD dwTickCount,
1396   LPINTERFACEINFO lpInterfaceInfo)
1397 {
1398     static int callcount = 0;
1399     DWORD ret;
1400     trace("HandleInComingCall\n");
1401     switch (callcount)
1402     {
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;
1412     }
1413     callcount++;
1414     return ret;
1415 }
1416
1417 static DWORD WINAPI MessageFilter_RetryRejectedCall(
1418   IMessageFilter *iface,
1419   HTASK threadIDCallee,
1420   DWORD dwTickCount,
1421   DWORD dwRejectType)
1422 {
1423     trace("RetryRejectedCall\n");
1424     return 0;
1425 }
1426
1427 static DWORD WINAPI MessageFilter_MessagePending(
1428   IMessageFilter *iface,
1429   HTASK threadIDCallee,
1430   DWORD dwTickCount,
1431   DWORD dwPendingType)
1432 {
1433     trace("MessagePending\n");
1434     return PENDINGMSG_WAITNOPROCESS;
1435 }
1436
1437 static const IMessageFilterVtbl MessageFilter_Vtbl =
1438 {
1439     MessageFilter_QueryInterface,
1440     MessageFilter_AddRef,
1441     MessageFilter_Release,
1442     MessageFilter_HandleInComingCall,
1443     MessageFilter_RetryRejectedCall,
1444     MessageFilter_MessagePending
1445 };
1446
1447 static IMessageFilter MessageFilter = { &MessageFilter_Vtbl };
1448
1449 static void test_message_filter(void)
1450 {
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;
1458
1459     cLocks = 0;
1460
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);
1464
1465     ok_more_than_one_lock();
1466
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);
1471
1472     ok_more_than_one_lock();
1473
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;
1478
1479     hr = CoRegisterMessageFilter(&MessageFilter, &prev_filter);
1480     ok_ole_success(hr, CoRegisterMessageFilter);
1481
1482     hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
1483     ok_ole_success(hr, IClassFactory_CreateInstance);
1484
1485     IUnknown_Release(proxy);
1486
1487     IClassFactory_Release(cf);
1488
1489     ok_no_locks();
1490
1491     end_host_object(tid, thread);
1492
1493     hr = CoRegisterMessageFilter(prev_filter, NULL);
1494     ok_ole_success(hr, CoRegisterMessageFilter);
1495 }
1496
1497 /* test failure case of trying to unmarshal from bad stream */
1498 static void test_bad_marshal_stream(void)
1499 {
1500     HRESULT hr;
1501     IStream *pStream = NULL;
1502
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);
1507
1508     ok_more_than_one_lock();
1509
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);
1513
1514     /* now release for real */
1515     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1516     hr = CoReleaseMarshalData(pStream);
1517     ok_ole_success(hr, CoReleaseMarshalData);
1518
1519     IStream_Release(pStream);
1520 }
1521
1522 /* tests that proxies implement certain interfaces */
1523 static void test_proxy_interfaces(void)
1524 {
1525     HRESULT hr;
1526     IStream *pStream = NULL;
1527     IUnknown *pProxy = NULL;
1528     IUnknown *pOtherUnknown = NULL;
1529     DWORD tid;
1530     HANDLE thread;
1531
1532     cLocks = 0;
1533
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);
1537
1538     ok_more_than_one_lock();
1539         
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);
1544
1545     ok_more_than_one_lock();
1546
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);
1550
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);
1554
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);
1558
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);
1562
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. */
1565
1566     IUnknown_Release(pProxy);
1567
1568     ok_no_locks();
1569
1570     end_host_object(tid, thread);
1571 }
1572
1573 typedef struct
1574 {
1575     const IUnknownVtbl *lpVtbl;
1576     ULONG refs;
1577 } HeapUnknown;
1578
1579 static HRESULT WINAPI HeapUnknown_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
1580 {
1581     if (IsEqualIID(riid, &IID_IUnknown))
1582     {
1583         IUnknown_AddRef(iface);
1584         *ppv = (LPVOID)iface;
1585         return S_OK;
1586     }
1587     *ppv = NULL;
1588     return E_NOINTERFACE;
1589 }
1590
1591 static ULONG WINAPI HeapUnknown_AddRef(IUnknown *iface)
1592 {
1593     HeapUnknown *This = (HeapUnknown *)iface;
1594     return InterlockedIncrement((LONG*)&This->refs);
1595 }
1596
1597 static ULONG WINAPI HeapUnknown_Release(IUnknown *iface)
1598 {
1599     HeapUnknown *This = (HeapUnknown *)iface;
1600     ULONG refs = InterlockedDecrement((LONG*)&This->refs);
1601     if (!refs) HeapFree(GetProcessHeap(), 0, This);
1602     return refs;
1603 }
1604
1605 static const IUnknownVtbl HeapUnknown_Vtbl =
1606 {
1607     HeapUnknown_QueryInterface,
1608     HeapUnknown_AddRef,
1609     HeapUnknown_Release
1610 };
1611
1612 static void test_proxybuffer(REFIID riid)
1613 {
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));
1621
1622     pUnkOuter->lpVtbl = &HeapUnknown_Vtbl;
1623     pUnkOuter->refs = 1;
1624
1625     hr = CoGetPSClsid(riid, &clsid);
1626     ok_ole_success(hr, CoGetPSClsid);
1627
1628     hr = CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL, &IID_IPSFactoryBuffer, (LPVOID*)&psfb);
1629     ok_ole_success(hr, CoGetClassObject);
1630
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");
1634
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);
1639
1640     refs = IPSFactoryBuffer_Release(psfb);
1641     if (0)
1642     {
1643     /* not reliable on native. maybe it leaks references! */
1644     ok(refs == 0, "Ref-count leak of %d on IPSFactoryBuffer\n", refs);
1645     }
1646
1647     refs = IUnknown_Release((IUnknown *)lpvtbl);
1648     ok(refs == 0, "Ref-count leak of %d on IRpcProxyBuffer\n", refs);
1649
1650     refs = IRpcProxyBuffer_Release(proxy);
1651     ok(refs == 0, "Ref-count leak of %d on IRpcProxyBuffer\n", refs);
1652 }
1653
1654 static void test_stubbuffer(REFIID riid)
1655 {
1656     HRESULT hr;
1657     IPSFactoryBuffer *psfb;
1658     IRpcStubBuffer *stub;
1659     ULONG refs;
1660     CLSID clsid;
1661
1662     cLocks = 0;
1663
1664     hr = CoGetPSClsid(riid, &clsid);
1665     ok_ole_success(hr, CoGetPSClsid);
1666
1667     hr = CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL, &IID_IPSFactoryBuffer, (LPVOID*)&psfb);
1668     ok_ole_success(hr, CoGetClassObject);
1669
1670     hr = IPSFactoryBuffer_CreateStub(psfb, riid, (IUnknown*)&Test_ClassFactory, &stub);
1671     ok_ole_success(hr, IPSFactoryBuffer_CreateStub);
1672
1673     refs = IPSFactoryBuffer_Release(psfb);
1674     if (0)
1675     {
1676     /* not reliable on native. maybe it leaks references */
1677     ok(refs == 0, "Ref-count leak of %d on IPSFactoryBuffer\n", refs);
1678     }
1679
1680     ok_more_than_one_lock();
1681
1682     IRpcStubBuffer_Disconnect(stub);
1683
1684     ok_no_locks();
1685
1686     refs = IRpcStubBuffer_Release(stub);
1687     ok(refs == 0, "Ref-count leak of %d on IRpcProxyBuffer\n", refs);
1688 }
1689
1690 static HWND hwnd_app;
1691
1692 static HRESULT WINAPI TestRE_IClassFactory_CreateInstance(
1693     LPCLASSFACTORY iface,
1694     LPUNKNOWN pUnkOuter,
1695     REFIID riid,
1696     LPVOID *ppvObj)
1697 {
1698     DWORD_PTR res;
1699     if (IsEqualIID(riid, &IID_IWineTest))
1700     {
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");
1703     }
1704     return S_FALSE;
1705 }
1706
1707 static const IClassFactoryVtbl TestREClassFactory_Vtbl =
1708 {
1709     Test_IClassFactory_QueryInterface,
1710     Test_IClassFactory_AddRef,
1711     Test_IClassFactory_Release,
1712     TestRE_IClassFactory_CreateInstance,
1713     Test_IClassFactory_LockServer
1714 };
1715
1716 IClassFactory TestRE_ClassFactory = { &TestREClassFactory_Vtbl };
1717
1718 static LRESULT CALLBACK window_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1719 {
1720     switch (msg)
1721     {
1722     case WM_USER:
1723     {
1724         HRESULT hr;
1725         IStream *pStream = NULL;
1726         IClassFactory *proxy = NULL;
1727         IUnknown *object;
1728         DWORD tid;
1729         HANDLE thread;
1730
1731         cLocks = 0;
1732
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);
1736
1737         ok_more_than_one_lock();
1738
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);
1743
1744         ok_more_than_one_lock();
1745
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);
1749
1750         IClassFactory_Release(proxy);
1751
1752         ok_no_locks();
1753
1754         end_host_object(tid, thread);
1755
1756         PostMessage(hwnd, WM_QUIT, 0, 0);
1757
1758         return 0;
1759     }
1760     case WM_USER+1:
1761     {
1762         HRESULT hr;
1763         IStream *pStream = NULL;
1764         IClassFactory *proxy = NULL;
1765         IUnknown *object;
1766         DWORD tid;
1767         HANDLE thread;
1768
1769         cLocks = 0;
1770
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);
1774
1775         ok_more_than_one_lock();
1776
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);
1781
1782         ok_more_than_one_lock();
1783
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);
1788
1789         IClassFactory_Release(proxy);
1790
1791         ok_no_locks();
1792
1793         end_host_object(tid, thread);
1794
1795         return 0;
1796     }
1797     case WM_USER+2:
1798     {
1799         HRESULT hr;
1800         IStream *pStream = NULL;
1801         IClassFactory *proxy = NULL;
1802         IUnknown *object;
1803         DWORD tid;
1804         HANDLE thread;
1805
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);
1809
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);
1814
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);
1820
1821         IClassFactory_Release(proxy);
1822
1823         end_host_object(tid, thread);
1824
1825         PostQuitMessage(0);
1826
1827         return 0;
1828     }
1829     default:
1830         return DefWindowProc(hwnd, msg, wparam, lparam);
1831     }
1832 }
1833
1834 static void register_test_window(void)
1835 {
1836     WNDCLASS wndclass;
1837
1838     memset(&wndclass, 0, sizeof(wndclass));
1839     wndclass.lpfnWndProc = window_proc;
1840     wndclass.lpszClassName = "WineCOMTest";
1841     RegisterClass(&wndclass);
1842 }
1843
1844 static void test_message_reentrancy(void)
1845 {
1846     MSG msg;
1847
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");
1850
1851     /* start message re-entrancy test */
1852     PostMessage(hwnd_app, WM_USER, 0, 0);
1853
1854     while (GetMessage(&msg, NULL, 0, 0))
1855     {
1856         TranslateMessage(&msg);
1857         DispatchMessage(&msg);
1858     }
1859     DestroyWindow(hwnd_app);
1860 }
1861
1862 static HRESULT WINAPI TestMsg_IClassFactory_CreateInstance(
1863     LPCLASSFACTORY iface,
1864     LPUNKNOWN pUnkOuter,
1865     REFIID riid,
1866     LPVOID *ppvObj)
1867 {
1868     *ppvObj = NULL;
1869     SendMessage(hwnd_app, WM_USER+2, 0, 0);
1870     return S_OK;
1871 }
1872
1873 static IClassFactoryVtbl TestMsgClassFactory_Vtbl =
1874 {
1875     Test_IClassFactory_QueryInterface,
1876     Test_IClassFactory_AddRef,
1877     Test_IClassFactory_Release,
1878     TestMsg_IClassFactory_CreateInstance,
1879     Test_IClassFactory_LockServer
1880 };
1881
1882 IClassFactory TestMsg_ClassFactory = { &TestMsgClassFactory_Vtbl };
1883
1884 static void test_call_from_message(void)
1885 {
1886     MSG msg;
1887     IStream *pStream;
1888     HRESULT hr;
1889     IClassFactory *proxy;
1890     DWORD tid;
1891     HANDLE thread;
1892     IUnknown *object;
1893
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");
1896
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);
1900
1901     ok_more_than_one_lock();
1902
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);
1907
1908     ok_more_than_one_lock();
1909
1910     /* start message re-entrancy test */
1911     hr = IClassFactory_CreateInstance(proxy, NULL, &IID_IUnknown, (void **)&object);
1912     ok_ole_success(hr, IClassFactory_CreateInstance);
1913
1914     IClassFactory_Release(proxy);
1915
1916     ok_no_locks();
1917
1918     end_host_object(tid, thread);
1919
1920     while (GetMessage(&msg, NULL, 0, 0))
1921     {
1922         TranslateMessage(&msg);
1923         DispatchMessage(&msg);
1924     }
1925     DestroyWindow(hwnd_app);
1926 }
1927
1928 static void test_WM_QUIT_handling(void)
1929 {
1930     MSG msg;
1931
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");
1934
1935     /* start WM_QUIT handling test */
1936     PostMessage(hwnd_app, WM_USER+1, 0, 0);
1937
1938     while (GetMessage(&msg, NULL, 0, 0))
1939     {
1940         TranslateMessage(&msg);
1941         DispatchMessage(&msg);
1942     }
1943 }
1944
1945 static void test_freethreadedmarshaldata(IStream *pStream, MSHCTX mshctx, void *ptr, DWORD mshlflags)
1946 {
1947     HGLOBAL hglobal;
1948     DWORD size;
1949     char *marshal_data;
1950     HRESULT hr;
1951
1952     hr = GetHGlobalFromStream(pStream, &hglobal);
1953     ok_ole_success(hr, GetHGlobalFromStream);
1954
1955     size = GlobalSize(hglobal);
1956
1957     marshal_data = (char *)GlobalLock(hglobal);
1958
1959     if (mshctx == MSHCTX_INPROC)
1960     {
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);
1963
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]);
1974     }
1975     else
1976     {
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");
1980     }
1981
1982     GlobalUnlock(hglobal);
1983 }
1984
1985 static void test_freethreadedmarshaler(void)
1986 {
1987     HRESULT hr;
1988     IUnknown *pFTUnknown;
1989     IMarshal *pFTMarshal;
1990     IStream *pStream;
1991     IUnknown *pProxy;
1992     static const LARGE_INTEGER llZero;
1993
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);
2000
2001     hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
2002     ok_ole_success(hr, CreateStreamOnHGlobal);
2003
2004     /* inproc normal marshaling */
2005
2006     hr = IMarshal_MarshalInterface(pFTMarshal, pStream, &IID_IClassFactory,
2007         (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
2008     ok_ole_success(hr, IMarshal_MarshalInterface);
2009
2010     ok_more_than_one_lock();
2011
2012     test_freethreadedmarshaldata(pStream, MSHCTX_INPROC, &Test_ClassFactory, MSHLFLAGS_NORMAL);
2013
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);
2017
2018     IUnknown_Release(pProxy);
2019
2020     ok_no_locks();
2021
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)
2025     {
2026     /* local normal marshaling */
2027
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);
2031
2032     ok_more_than_one_lock();
2033
2034     test_freethreadedmarshaldata(pStream, MSHCTX_LOCAL, &Test_ClassFactory, MSHLFLAGS_NORMAL);
2035
2036     IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2037     hr = IMarshal_ReleaseMarshalData(pFTMarshal, pStream);
2038     ok_ole_success(hr, IMarshal_ReleaseMarshalData);
2039
2040     ok_no_locks();
2041     }
2042
2043     /* inproc table-strong marshaling */
2044
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);
2050
2051     ok_more_than_one_lock();
2052
2053     test_freethreadedmarshaldata(pStream, MSHCTX_INPROC, &Test_ClassFactory, MSHLFLAGS_TABLESTRONG);
2054
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);
2058
2059     IUnknown_Release(pProxy);
2060
2061     ok_more_than_one_lock();
2062
2063     IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2064     hr = IMarshal_ReleaseMarshalData(pFTMarshal, pStream);
2065     ok_ole_success(hr, IMarshal_ReleaseMarshalData);
2066
2067     ok_no_locks();
2068
2069     /* inproc table-weak marshaling */
2070
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);
2076
2077     ok_no_locks();
2078
2079     test_freethreadedmarshaldata(pStream, MSHCTX_INPROC, &Test_ClassFactory, MSHLFLAGS_TABLEWEAK);
2080
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);
2084
2085     ok_more_than_one_lock();
2086
2087     IUnknown_Release(pProxy);
2088
2089     ok_no_locks();
2090
2091     /* inproc normal marshaling (for extraordinary cases) */
2092
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);
2097
2098     ok_more_than_one_lock();
2099
2100     /* this call shows that DisconnectObject does nothing */
2101     hr = IMarshal_DisconnectObject(pFTMarshal, 0);
2102     ok_ole_success(hr, IMarshal_DisconnectObject);
2103
2104     ok_more_than_one_lock();
2105
2106     IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2107     hr = IMarshal_ReleaseMarshalData(pFTMarshal, pStream);
2108     ok_ole_success(hr, IMarshal_ReleaseMarshalData);
2109
2110     ok_no_locks();
2111
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);
2117
2118     ok_no_locks();
2119
2120     IStream_Release(pStream);
2121     IMarshal_Release(pFTMarshal);
2122 }
2123
2124 static void test_inproc_handler(void)
2125 {
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;
2134
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);
2146
2147     hr = CoCreateInstance(&CLSID_WineTest, NULL, CLSCTX_INPROC_HANDLER, &IID_IUnknown, (void **)&pObject);
2148     todo_wine
2149     ok_ole_success(hr, "CoCreateInstance");
2150
2151     if (SUCCEEDED(hr))
2152     {
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);
2155
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);
2160
2161         IUnknown_Release(pObject);
2162     }
2163
2164     RegDeleteKey(HKEY_CLASSES_ROOT, buffer);
2165     *strrchr(buffer, '\\') = '\0';
2166     RegDeleteKey(HKEY_CLASSES_ROOT, buffer);
2167 }
2168
2169 static HRESULT WINAPI Test_SMI_QueryInterface(
2170     IStdMarshalInfo *iface,
2171     REFIID riid,
2172     LPVOID *ppvObj)
2173 {
2174     if (ppvObj == NULL) return E_POINTER;
2175
2176     if (IsEqualGUID(riid, &IID_IUnknown) ||
2177         IsEqualGUID(riid, &IID_IStdMarshalInfo))
2178     {
2179         *ppvObj = (LPVOID)iface;
2180         IClassFactory_AddRef(iface);
2181         return S_OK;
2182     }
2183
2184     return E_NOINTERFACE;
2185 }
2186
2187 static ULONG WINAPI Test_SMI_AddRef(IStdMarshalInfo *iface)
2188 {
2189     LockModule();
2190     return 2; /* non-heap-based object */
2191 }
2192
2193 static ULONG WINAPI Test_SMI_Release(IStdMarshalInfo *iface)
2194 {
2195     UnlockModule();
2196     return 1; /* non-heap-based object */
2197 }
2198
2199 static HRESULT WINAPI Test_SMI_GetClassForHandler(
2200     IStdMarshalInfo *iface,
2201     DWORD dwDestContext,
2202     void *pvDestContext,
2203     CLSID *pClsid)
2204 {
2205     *pClsid = CLSID_WineTest;
2206     return S_OK;
2207 }
2208
2209 static const IStdMarshalInfoVtbl Test_SMI_Vtbl =
2210 {
2211     Test_SMI_QueryInterface,
2212     Test_SMI_AddRef,
2213     Test_SMI_Release,
2214     Test_SMI_GetClassForHandler
2215 };
2216
2217 static IStdMarshalInfo Test_SMI = {&Test_SMI_Vtbl};
2218
2219 static void test_handler_marshaling(void)
2220 {
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;
2228
2229     cLocks = 0;
2230
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);
2234
2235     ok_more_than_one_lock();
2236
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);
2241
2242     ok_more_than_one_lock();
2243
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);
2246
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);
2252
2253     IUnknown_Release(pProxy);
2254
2255     ok_no_locks();
2256
2257     end_host_object(tid, thread);
2258
2259     /* FIXME: test IPersist interface has the same effect as IStdMarshalInfo */
2260 }
2261
2262
2263 static void test_client_security(void)
2264 {
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;
2284
2285     cLocks = 0;
2286
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);
2290
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);
2295
2296     hr = IUnknown_QueryInterface(pProxy, &IID_IUnknown, (LPVOID*)&pUnknown1);
2297     ok_ole_success(hr, "IUnknown_QueryInterface IID_IUnknown");
2298
2299     hr = IUnknown_QueryInterface(pProxy, &IID_IRemUnknown, (LPVOID*)&pProxy2);
2300     ok_ole_success(hr, "IUnknown_QueryInterface IID_IStream");
2301
2302     hr = IUnknown_QueryInterface(pProxy2, &IID_IUnknown, (LPVOID*)&pUnknown2);
2303     ok_ole_success(hr, "IUnknown_QueryInterface IID_IUnknown");
2304
2305     ok(pUnknown1 == pUnknown2, "both proxy's IUnknowns should be the same - %p, %p\n", pUnknown1, pUnknown2);
2306
2307     hr = IUnknown_QueryInterface(pProxy, &IID_IMarshal, (LPVOID*)&pMarshal);
2308     ok_ole_success(hr, "IUnknown_QueryInterface IID_IMarshal");
2309
2310     hr = IUnknown_QueryInterface(pProxy, &IID_IClientSecurity, (LPVOID*)&pCliSec);
2311     ok_ole_success(hr, "IUnknown_QueryInterface IID_IClientSecurity");
2312
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)");
2315
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);
2318
2319     hr = IClientSecurity_QueryBlanket(pCliSec, (IUnknown *)pProxy, &dwAuthnSvc, &dwAuthzSvc, &pServerPrincName, &dwAuthnLevel, &dwImpLevel, &pAuthInfo, &dwCapabilities);
2320     todo_wine ok_ole_success(hr, "IClientSecurity_QueryBlanket");
2321
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");
2324
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);
2327
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);
2330
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);
2333
2334     CoTaskMemFree(pServerPrincName);
2335
2336     hr = IClientSecurity_QueryBlanket(pCliSec, (IUnknown *)pUnknown1, &dwAuthnSvc, &dwAuthzSvc, &pServerPrincName, &dwAuthnLevel, &dwImpLevel, &pAuthInfo, &dwCapabilities);
2337     todo_wine ok_ole_success(hr, "IClientSecurity_QueryBlanket(IUnknown)");
2338
2339     CoTaskMemFree(pServerPrincName);
2340
2341     IClassFactory_Release(pProxy);
2342     IUnknown_Release(pProxy2);
2343     IUnknown_Release(pUnknown1);
2344     IUnknown_Release(pUnknown2);
2345     IMarshal_Release(pMarshal);
2346     IClientSecurity_Release(pCliSec);
2347
2348     end_host_object(tid, thread);
2349 }
2350
2351 static HANDLE heventShutdown;
2352
2353 static void LockModuleOOP(void)
2354 {
2355     InterlockedIncrement(&cLocks); /* for test purposes only */
2356     CoAddRefServerProcess();
2357 }
2358
2359 static void UnlockModuleOOP(void)
2360 {
2361     InterlockedDecrement(&cLocks); /* for test purposes only */
2362     if (!CoReleaseServerProcess())
2363         SetEvent(heventShutdown);
2364 }
2365
2366 static HWND hwnd_app;
2367
2368 static HRESULT WINAPI TestOOP_IClassFactory_QueryInterface(
2369     LPCLASSFACTORY iface,
2370     REFIID riid,
2371     LPVOID *ppvObj)
2372 {
2373     if (ppvObj == NULL) return E_POINTER;
2374
2375     if (IsEqualGUID(riid, &IID_IUnknown) ||
2376         IsEqualGUID(riid, &IID_IClassFactory))
2377     {
2378         *ppvObj = (LPVOID)iface;
2379         IClassFactory_AddRef(iface);
2380         return S_OK;
2381     }
2382
2383     return E_NOINTERFACE;
2384 }
2385
2386 static ULONG WINAPI TestOOP_IClassFactory_AddRef(LPCLASSFACTORY iface)
2387 {
2388     return 2; /* non-heap-based object */
2389 }
2390
2391 static ULONG WINAPI TestOOP_IClassFactory_Release(LPCLASSFACTORY iface)
2392 {
2393     return 1; /* non-heap-based object */
2394 }
2395
2396 static HRESULT WINAPI TestOOP_IClassFactory_CreateInstance(
2397     LPCLASSFACTORY iface,
2398     LPUNKNOWN pUnkOuter,
2399     REFIID riid,
2400     LPVOID *ppvObj)
2401 {
2402     if (IsEqualIID(riid, &IID_IClassFactory) || IsEqualIID(riid, &IID_IUnknown))
2403     {
2404         *ppvObj = iface;
2405         return S_OK;
2406     }
2407     return CLASS_E_CLASSNOTAVAILABLE;
2408 }
2409
2410 static HRESULT WINAPI TestOOP_IClassFactory_LockServer(
2411     LPCLASSFACTORY iface,
2412     BOOL fLock)
2413 {
2414     if (fLock)
2415         LockModuleOOP();
2416     else
2417         UnlockModuleOOP();
2418     return S_OK;
2419 }
2420
2421 static const IClassFactoryVtbl TestClassFactoryOOP_Vtbl =
2422 {
2423     TestOOP_IClassFactory_QueryInterface,
2424     TestOOP_IClassFactory_AddRef,
2425     TestOOP_IClassFactory_Release,
2426     TestOOP_IClassFactory_CreateInstance,
2427     TestOOP_IClassFactory_LockServer
2428 };
2429
2430 static IClassFactory TestOOP_ClassFactory = { &TestClassFactoryOOP_Vtbl };
2431
2432 static void test_register_local_server(void)
2433 {
2434     DWORD cookie;
2435     HRESULT hr;
2436     HANDLE ready_event;
2437     HANDLE quit_event;
2438     DWORD wait;
2439
2440     heventShutdown = CreateEvent(NULL, TRUE, FALSE, NULL);
2441
2442     hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&TestOOP_ClassFactory,
2443                                CLSCTX_LOCAL_SERVER, REGCLS_SINGLEUSE, &cookie);
2444     ok_ole_success(hr, CoRegisterClassObject);
2445
2446     ready_event = CreateEvent(NULL, FALSE, FALSE, "Wine COM Test Ready Event");
2447     SetEvent(ready_event);
2448
2449     quit_event = CreateEvent(NULL, FALSE, FALSE, "Wine COM Test Quit Event");
2450
2451     do
2452     {
2453         wait = MsgWaitForMultipleObjects(1, &quit_event, FALSE, INFINITE, QS_ALLINPUT);
2454         if (wait == WAIT_OBJECT_0+1)
2455         {
2456             MSG msg;
2457             BOOL ret = PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
2458             if (ret)
2459             {
2460                 trace("Message 0x%x\n", msg.message);
2461                 TranslateMessage(&msg);
2462                 DispatchMessage(&msg);
2463             }
2464         }
2465     }
2466     while (wait == WAIT_OBJECT_0+1);
2467
2468     hr = CoRevokeClassObject(cookie);
2469     ok_ole_success(hr, CoRevokeClassObject);
2470 }
2471
2472 static HANDLE create_target_process(const char *arg)
2473 {
2474     char **argv;
2475     char cmdline[MAX_PATH];
2476     PROCESS_INFORMATION pi;
2477     STARTUPINFO si = { 0 };
2478     si.cb = sizeof(si);
2479
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;
2488 }
2489
2490 /* tests functions commonly used by out of process COM servers */
2491 static void test_local_server(void)
2492 {
2493     DWORD cookie;
2494     HRESULT hr;
2495     IClassFactory * cf;
2496     DWORD ret;
2497     HANDLE process;
2498     HANDLE quit_event;
2499     HANDLE ready_event;
2500
2501     heventShutdown = CreateEvent(NULL, TRUE, FALSE, NULL);
2502
2503     cLocks = 0;
2504
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);
2509
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);
2517
2518     /* Resume the object suspended above ... */
2519     hr = CoResumeClassObjects();
2520     ok_ole_success(hr, CoResumeClassObjects);
2521
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);
2526
2527     /* Now check the locking is working */
2528     /* NOTE: we are accessing the class directly, not through a proxy */
2529
2530     ok_no_locks();
2531
2532     hr = IClassFactory_LockServer(cf, TRUE);
2533     ok_ole_success(hr, IClassFactory_LockServer);
2534
2535     ok_more_than_one_lock();
2536     
2537     IClassFactory_LockServer(cf, FALSE);
2538     ok_ole_success(hr, IClassFactory_LockServer);
2539
2540     ok_no_locks();
2541
2542     IClassFactory_Release(cf);
2543
2544     /* wait for shutdown signal */
2545     ret = WaitForSingleObject(heventShutdown, 0);
2546     ok(ret != WAIT_TIMEOUT, "Server didn't shut down\n");
2547
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);
2554
2555     hr = CoRevokeClassObject(cookie);
2556     ok_ole_success(hr, CoRevokeClassObject);
2557
2558     CloseHandle(heventShutdown);
2559
2560     process = create_target_process("-Embedding");
2561     ok(process != NULL, "couldn't start local server process, error was %d\n", GetLastError());
2562
2563     ready_event = CreateEvent(NULL, FALSE, FALSE, "Wine COM Test Ready Event");
2564     WaitForSingleObject(ready_event, INFINITE);
2565     CloseHandle(ready_event);
2566
2567     hr = CoCreateInstance(&CLSID_WineOOPTest, NULL, CLSCTX_LOCAL_SERVER, &IID_IClassFactory, (void **)&cf);
2568     ok_ole_success(hr, CoCreateInstance);
2569
2570     IClassFactory_Release(cf);
2571
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");
2574
2575     quit_event = CreateEvent(NULL, FALSE, FALSE, "Wine COM Test Quit Event");
2576     SetEvent(quit_event);
2577
2578     WaitForSingleObject(process, INFINITE);
2579     CloseHandle(quit_event);
2580     CloseHandle(process);
2581 }
2582
2583 struct git_params
2584 {
2585         DWORD cookie;
2586         IGlobalInterfaceTable *git;
2587 };
2588
2589 static DWORD CALLBACK get_global_interface_proc(LPVOID pv)
2590 {
2591         HRESULT hr;
2592         struct git_params *params = (struct git_params *)pv;
2593         IClassFactory *cf;
2594
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);
2599
2600         CoInitialize(NULL);
2601
2602         hr = IGlobalInterfaceTable_GetInterfaceFromGlobal(params->git, params->cookie, &IID_IClassFactory, (void **)&cf);
2603         ok_ole_success(hr, IGlobalInterfaceTable_GetInterfaceFromGlobal);
2604
2605         IClassFactory_Release(cf);
2606
2607         CoUninitialize();
2608
2609         return hr;
2610 }
2611
2612 static void test_globalinterfacetable(void)
2613 {
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;
2622
2623         trace("test_globalinterfacetable\n");
2624         cLocks = 0;
2625
2626         hr = CoCreateInstance(&CLSID_StdGlobalInterfaceTable, NULL, CLSCTX_INPROC_SERVER, &IID_IGlobalInterfaceTable, (void **)&git);
2627         ok_ole_success(hr, CoCreateInstance);
2628
2629         hr = IGlobalInterfaceTable_RegisterInterfaceInGlobal(git, (IUnknown *)&Test_ClassFactory, &IID_IClassFactory, &cookie);
2630         ok_ole_success(hr, IGlobalInterfaceTable_RegisterInterfaceInGlobal);
2631
2632         ok_more_than_one_lock();
2633
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);
2639
2640         ret = MsgWaitForMultipleObjects(1, &thread, FALSE, INFINITE, QS_ALLINPUT);
2641         while (ret == WAIT_OBJECT_0 + 1)
2642         {
2643                 MSG msg;
2644                 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
2645                         DispatchMessage(&msg);
2646                 ret = MsgWaitForMultipleObjects(1, &thread, FALSE, INFINITE, QS_ALLINPUT);
2647         }
2648
2649         CloseHandle(thread);
2650
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);
2655
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);
2660
2661         hr = IGlobalInterfaceTable_RevokeInterfaceFromGlobal(git, cookie);
2662         ok_ole_success(hr, IGlobalInterfaceTable_RevokeInterfaceFromGlobal);
2663
2664         ok_no_locks();
2665
2666         IGlobalInterfaceTable_Release(git);
2667 }
2668
2669 static const char *debugstr_iid(REFIID riid)
2670 {
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)
2679     {
2680         memcpy(name, buffer, sizeof(buffer));
2681         goto done;
2682     }
2683     if (RegQueryValue(hkeyInterface, buffer, name, &name_size) != ERROR_SUCCESS)
2684     {
2685         memcpy(name, buffer, sizeof(buffer));
2686         goto done;
2687     }
2688     RegCloseKey(hkeyInterface);
2689 done:
2690     return name;
2691 }
2692
2693 static HRESULT WINAPI TestChannelHook_QueryInterface(IChannelHook *iface, REFIID riid, void **ppv)
2694 {
2695     if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IChannelHook))
2696     {
2697         *ppv = iface;
2698         IUnknown_AddRef(iface);
2699         return S_OK;
2700     }
2701
2702     *ppv = NULL;
2703     return E_NOINTERFACE;
2704 }
2705
2706 static ULONG WINAPI TestChannelHook_AddRef(IChannelHook *iface)
2707 {
2708     return 2;
2709 }
2710
2711 static ULONG WINAPI TestChannelHook_Release(IChannelHook *iface)
2712 {
2713     return 1;
2714 }
2715
2716 static void WINAPI TestChannelHook_ClientGetSize(
2717     IChannelHook *iface,
2718     REFGUID uExtent,
2719     REFIID  riid,
2720     ULONG  *pDataSize )
2721 {
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");
2730
2731     *pDataSize = 1;
2732 }
2733
2734 static void WINAPI TestChannelHook_ClientFillBuffer(
2735     IChannelHook *iface,
2736     REFGUID uExtent,
2737     REFIID  riid,
2738     ULONG  *pDataSize,
2739     void   *pDataBuffer )
2740 {
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");
2747
2748     *(unsigned char *)pDataBuffer = 0xcc;
2749     *pDataSize = 1;
2750 }
2751
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 )
2760 {
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");
2767     }
2768     ok(IsEqualGUID(uExtent, &EXTENTID_WineTest), "uExtent wasn't correct\n");
2769 }
2770
2771 static void WINAPI TestChannelHook_ServerNotify(
2772     IChannelHook *iface,
2773     REFGUID uExtent,
2774     REFIID  riid,
2775     ULONG   cbDataSize,
2776     void   *pDataBuffer,
2777     DWORD   lDataRep )
2778 {
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");
2787 }
2788
2789 static void WINAPI TestChannelHook_ServerGetSize(
2790     IChannelHook *iface,
2791     REFGUID uExtent,
2792     REFIID  riid,
2793     HRESULT hrFault,
2794     ULONG  *pDataSize )
2795 {
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);
2805
2806     *pDataSize = 0;
2807 }
2808
2809 static void WINAPI TestChannelHook_ServerFillBuffer(
2810     IChannelHook *iface,
2811     REFGUID uExtent,
2812     REFIID  riid,
2813     ULONG  *pDataSize,
2814     void   *pDataBuffer,
2815     HRESULT hrFault )
2816 {
2817     trace("TestChannelHook_ServerFillBuffer\n");
2818     ok(0, "TestChannelHook_ServerFillBuffer shouldn't be called\n");
2819 }
2820
2821 static const IChannelHookVtbl TestChannelHookVtbl =
2822 {
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,
2832 };
2833
2834 static IChannelHook TestChannelHook = { &TestChannelHookVtbl };
2835
2836 static void test_channel_hook(void)
2837 {
2838     IStream *pStream = NULL;
2839     IClassFactory *cf = NULL;
2840     DWORD tid;
2841     IUnknown *proxy = NULL;
2842     HANDLE thread;
2843     HRESULT hr;
2844
2845     hr = CoRegisterChannelHook(&EXTENTID_WineTest, &TestChannelHook);
2846     ok_ole_success(hr, CoRegisterChannelHook);
2847
2848     hr = CoRegisterMessageFilter(&MessageFilter, NULL);
2849     ok_ole_success(hr, CoRegisterMessageFilter);
2850
2851     cLocks = 0;
2852
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);
2856
2857     ok_more_than_one_lock();
2858
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);
2863
2864     ok_more_than_one_lock();
2865
2866     hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
2867     ok_ole_success(hr, IClassFactory_CreateInstance);
2868     IUnknown_Release(proxy);
2869
2870     IClassFactory_Release(cf);
2871
2872     ok_no_locks();
2873
2874     end_host_object(tid, thread);
2875
2876     hr = CoRegisterMessageFilter(NULL, NULL);
2877     ok_ole_success(hr, CoRegisterMessageFilter);
2878 }
2879
2880 START_TEST(marshal)
2881 {
2882     HMODULE hOle32 = GetModuleHandle("ole32");
2883     int argc;
2884     char **argv;
2885
2886     if (!(pCoInitializeEx = (void*)GetProcAddress(hOle32, "CoInitializeEx"))) goto no_test;
2887
2888     argc = winetest_get_mainargs( &argv );
2889     if (argc > 2 && (!strcmp(argv[2], "-Embedding")))
2890     {
2891         pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
2892         test_register_local_server();
2893         CoUninitialize();
2894
2895         return;
2896     }
2897
2898     register_test_window();
2899
2900     test_cocreateinstance_proxy();
2901
2902     pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
2903
2904     /* FIXME: test CoCreateInstanceEx */
2905
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();
2943
2944     test_local_server();
2945
2946     test_globalinterfacetable();
2947
2948     /* must be last test as channel hooks can't be unregistered */
2949     test_channel_hook();
2950
2951     CoUninitialize();
2952     return;
2953
2954 no_test:
2955     trace("You need DCOM95 installed to run this test\n");
2956     return;
2957 }