ole32: Add more tests for global interface table functions.
[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
439 /* tests success case of an interthread marshal */
440 static void test_interthread_marshal_and_unmarshal(void)
441 {
442     HRESULT hr;
443     IStream *pStream = NULL;
444     IUnknown *pProxy = NULL;
445     DWORD tid;
446     HANDLE thread;
447
448     cLocks = 0;
449
450     hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
451     ok_ole_success(hr, CreateStreamOnHGlobal);
452     tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
453
454     ok_more_than_one_lock();
455     
456     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
457     hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
458     ok_ole_success(hr, CoUnmarshalInterface);
459     IStream_Release(pStream);
460
461     ok_more_than_one_lock();
462
463     IUnknown_Release(pProxy);
464
465     ok_no_locks();
466
467     end_host_object(tid, thread);
468 }
469
470 /* the number of external references that Wine's proxy manager normally gives
471  * out, so we can test the border case of running out of references */
472 #define NORMALEXTREFS 5
473
474 /* tests success case of an interthread marshal and then marshaling the proxy */
475 static void test_proxy_marshal_and_unmarshal(void)
476 {
477     HRESULT hr;
478     IStream *pStream = NULL;
479     IUnknown *pProxy = NULL;
480     IUnknown *pProxy2 = NULL;
481     DWORD tid;
482     HANDLE thread;
483     int i;
484
485     cLocks = 0;
486
487     hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
488     ok_ole_success(hr, CreateStreamOnHGlobal);
489     tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
490
491     ok_more_than_one_lock();
492     
493     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
494     hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
495     ok_ole_success(hr, CoUnmarshalInterface);
496
497     ok_more_than_one_lock();
498
499     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
500     /* marshal the proxy */
501     hr = CoMarshalInterface(pStream, &IID_IClassFactory, pProxy, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
502     ok_ole_success(hr, CoMarshalInterface);
503
504     ok_more_than_one_lock();
505
506     /* marshal 5 more times to exhaust the normal external references of 5 */
507     for (i = 0; i < NORMALEXTREFS; i++)
508     {
509         hr = CoMarshalInterface(pStream, &IID_IClassFactory, pProxy, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
510         ok_ole_success(hr, CoMarshalInterface);
511     }
512
513     ok_more_than_one_lock();
514
515     /* release the original proxy to test that we successfully keep the
516      * original object alive */
517     IUnknown_Release(pProxy);
518
519     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
520     hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
521     ok_ole_success(hr, CoUnmarshalInterface);
522
523     ok_more_than_one_lock();
524
525     IUnknown_Release(pProxy2);
526
527     /* unmarshal all of the proxies to check that the object stub still exists */
528     for (i = 0; i < NORMALEXTREFS; i++)
529     {
530         hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
531         ok_ole_success(hr, CoUnmarshalInterface);
532
533         IUnknown_Release(pProxy2);
534     }
535
536     ok_no_locks();
537
538     IStream_Release(pStream);
539
540     end_host_object(tid, thread);
541 }
542
543 /* tests success case of an interthread marshal and then marshaling the proxy
544  * using an iid that hasn't previously been unmarshaled */
545 static void test_proxy_marshal_and_unmarshal2(void)
546 {
547     HRESULT hr;
548     IStream *pStream = NULL;
549     IUnknown *pProxy = NULL;
550     IUnknown *pProxy2 = NULL;
551     DWORD tid;
552     HANDLE thread;
553
554     cLocks = 0;
555
556     hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
557     ok_ole_success(hr, CreateStreamOnHGlobal);
558     tid = start_host_object(pStream, &IID_IUnknown, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
559
560     ok_more_than_one_lock();
561
562     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
563     hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)&pProxy);
564     ok_ole_success(hr, CoUnmarshalInterface);
565
566     ok_more_than_one_lock();
567
568     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
569     /* marshal the proxy */
570     hr = CoMarshalInterface(pStream, &IID_IClassFactory, pProxy, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
571     ok_ole_success(hr, CoMarshalInterface);
572
573     ok_more_than_one_lock();
574
575     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
576     /* unmarshal the second proxy to the object */
577     hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
578     ok_ole_success(hr, CoUnmarshalInterface);
579     IStream_Release(pStream);
580
581     /* now the proxies should be as follows:
582      *  pProxy -> &Test_ClassFactory
583      *  pProxy2 -> &Test_ClassFactory
584      * they should NOT be as follows:
585      *  pProxy -> &Test_ClassFactory
586      *  pProxy2 -> pProxy
587      * the above can only really be tested by looking in +ole traces
588      */
589
590     ok_more_than_one_lock();
591
592     IUnknown_Release(pProxy);
593
594     ok_more_than_one_lock();
595
596     IUnknown_Release(pProxy2);
597
598     ok_no_locks();
599
600     end_host_object(tid, thread);
601 }
602
603 /* tests success case of an interthread marshal and then table-weak-marshaling the proxy */
604 static void test_proxy_marshal_and_unmarshal_weak(void)
605 {
606     HRESULT hr;
607     IStream *pStream = NULL;
608     IUnknown *pProxy = NULL;
609     IUnknown *pProxy2 = NULL;
610     DWORD tid;
611     HANDLE thread;
612
613     cLocks = 0;
614
615     hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
616     ok_ole_success(hr, CreateStreamOnHGlobal);
617     tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
618
619     ok_more_than_one_lock();
620
621     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
622     hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
623     ok_ole_success(hr, CoUnmarshalInterface);
624
625     ok_more_than_one_lock();
626
627     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
628     /* marshal the proxy */
629     hr = CoMarshalInterface(pStream, &IID_IClassFactory, pProxy, MSHCTX_INPROC, NULL, MSHLFLAGS_TABLEWEAK);
630     ok_ole_success(hr, CoMarshalInterface);
631
632     ok_more_than_one_lock();
633
634     /* release the original proxy to test that we successfully keep the
635      * original object alive */
636     IUnknown_Release(pProxy);
637
638     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
639     hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
640     todo_wine
641     ok(hr == CO_E_OBJNOTREG, "CoUnmarshalInterface should return CO_E_OBJNOTREG instead of 0x%08x\n", hr);
642
643     ok_no_locks();
644
645     IStream_Release(pStream);
646
647     end_host_object(tid, thread);
648 }
649
650 /* tests success case of an interthread marshal and then table-strong-marshaling the proxy */
651 static void test_proxy_marshal_and_unmarshal_strong(void)
652 {
653     HRESULT hr;
654     IStream *pStream = NULL;
655     IUnknown *pProxy = NULL;
656     IUnknown *pProxy2 = NULL;
657     DWORD tid;
658     HANDLE thread;
659
660     cLocks = 0;
661
662     hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
663     ok_ole_success(hr, CreateStreamOnHGlobal);
664     tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
665
666     ok_more_than_one_lock();
667
668     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
669     hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
670     ok_ole_success(hr, CoUnmarshalInterface);
671
672     ok_more_than_one_lock();
673
674     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
675     /* marshal the proxy */
676     hr = CoMarshalInterface(pStream, &IID_IClassFactory, pProxy, MSHCTX_INPROC, NULL, MSHLFLAGS_TABLESTRONG);
677     ok(hr == S_OK /* WinNT */ || hr == E_INVALIDARG /* Win9x */,
678         "CoMarshalInterface should have return S_OK or E_INVALIDARG instead of 0x%08x\n", hr);
679     if (FAILED(hr))
680     {
681         IUnknown_Release(pProxy);
682         goto end;
683     }
684
685     ok_more_than_one_lock();
686
687     /* release the original proxy to test that we successfully keep the
688      * original object alive */
689     IUnknown_Release(pProxy);
690
691     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
692     hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
693     ok_ole_success(hr, CoUnmarshalInterface);
694
695     ok_more_than_one_lock();
696
697     IUnknown_Release(pProxy2);
698
699     ok_more_than_one_lock();
700
701 end:
702     IStream_Release(pStream);
703
704     end_host_object(tid, thread);
705
706     ok_no_locks();
707 }
708
709 /* tests that stubs are released when the containing apartment is destroyed */
710 static void test_marshal_stub_apartment_shutdown(void)
711 {
712     HRESULT hr;
713     IStream *pStream = NULL;
714     IUnknown *pProxy = NULL;
715     DWORD tid;
716     HANDLE thread;
717
718     cLocks = 0;
719
720     hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
721     ok_ole_success(hr, CreateStreamOnHGlobal);
722     tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
723
724     ok_more_than_one_lock();
725     
726     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
727     hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
728     ok_ole_success(hr, CoUnmarshalInterface);
729     IStream_Release(pStream);
730
731     ok_more_than_one_lock();
732
733     end_host_object(tid, thread);
734
735     ok_no_locks();
736
737     IUnknown_Release(pProxy);
738
739     ok_no_locks();
740 }
741
742 /* tests that proxies are released when the containing apartment is destroyed */
743 static void test_marshal_proxy_apartment_shutdown(void)
744 {
745     HRESULT hr;
746     IStream *pStream = NULL;
747     IUnknown *pProxy = NULL;
748     DWORD tid;
749     HANDLE thread;
750
751     cLocks = 0;
752
753     hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
754     ok_ole_success(hr, CreateStreamOnHGlobal);
755     tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
756
757     ok_more_than_one_lock();
758     
759     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
760     hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
761     ok_ole_success(hr, CoUnmarshalInterface);
762     IStream_Release(pStream);
763
764     ok_more_than_one_lock();
765
766     CoUninitialize();
767
768     ok_no_locks();
769
770     IUnknown_Release(pProxy);
771
772     ok_no_locks();
773
774     end_host_object(tid, thread);
775
776     pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
777 }
778
779 /* tests that proxies are released when the containing mta apartment is destroyed */
780 static void test_marshal_proxy_mta_apartment_shutdown(void)
781 {
782     HRESULT hr;
783     IStream *pStream = NULL;
784     IUnknown *pProxy = NULL;
785     DWORD tid;
786     HANDLE thread;
787
788     CoUninitialize();
789     pCoInitializeEx(NULL, COINIT_MULTITHREADED);
790
791     cLocks = 0;
792
793     hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
794     ok_ole_success(hr, CreateStreamOnHGlobal);
795     tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
796
797     ok_more_than_one_lock();
798
799     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
800     hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
801     ok_ole_success(hr, CoUnmarshalInterface);
802     IStream_Release(pStream);
803
804     ok_more_than_one_lock();
805
806     CoUninitialize();
807
808     ok_no_locks();
809
810     IUnknown_Release(pProxy);
811
812     ok_no_locks();
813
814     end_host_object(tid, thread);
815
816     pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
817 }
818
819 struct ncu_params
820 {
821     LPSTREAM stream;
822     HANDLE marshal_event;
823     HANDLE unmarshal_event;
824 };
825
826 /* helper for test_no_couninitialize_server */
827 static DWORD CALLBACK no_couninitialize_server_proc(LPVOID p)
828 {
829     struct ncu_params *ncu_params = (struct ncu_params *)p;
830     HRESULT hr;
831
832     pCoInitializeEx(NULL, COINIT_MULTITHREADED);
833
834     hr = CoMarshalInterface(ncu_params->stream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
835     ok_ole_success(hr, CoMarshalInterface);
836
837     SetEvent(ncu_params->marshal_event);
838
839     WaitForSingleObject(ncu_params->unmarshal_event, INFINITE);
840
841     /* die without calling CoUninitialize */
842
843     return 0;
844 }
845
846 /* tests apartment that an apartment with a stub is released without deadlock
847  * if the owning thread exits */
848 static void test_no_couninitialize_server(void)
849 {
850     HRESULT hr;
851     IStream *pStream = NULL;
852     IUnknown *pProxy = NULL;
853     DWORD tid;
854     HANDLE thread;
855     struct ncu_params ncu_params;
856
857     cLocks = 0;
858
859     ncu_params.marshal_event = CreateEvent(NULL, TRUE, FALSE, NULL);
860     ncu_params.unmarshal_event = CreateEvent(NULL, TRUE, FALSE, NULL);
861
862     hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
863     ok_ole_success(hr, CreateStreamOnHGlobal);
864     ncu_params.stream = pStream;
865
866     thread = CreateThread(NULL, 0, no_couninitialize_server_proc, &ncu_params, 0, &tid);
867
868     WaitForSingleObject(ncu_params.marshal_event, INFINITE);
869     ok_more_than_one_lock();
870
871     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
872     hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
873     ok_ole_success(hr, CoUnmarshalInterface);
874     IStream_Release(pStream);
875
876     ok_more_than_one_lock();
877
878     SetEvent(ncu_params.unmarshal_event);
879     WaitForSingleObject(thread, INFINITE);
880
881     ok_no_locks();
882
883     CloseHandle(thread);
884     CloseHandle(ncu_params.marshal_event);
885     CloseHandle(ncu_params.unmarshal_event);
886
887     IUnknown_Release(pProxy);
888
889     ok_no_locks();
890 }
891
892 /* STA -> STA call during DLL_THREAD_DETACH */
893 static DWORD CALLBACK no_couninitialize_client_proc(LPVOID p)
894 {
895     struct ncu_params *ncu_params = (struct ncu_params *)p;
896     HRESULT hr;
897     IUnknown *pProxy = NULL;
898
899     pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
900
901     hr = CoUnmarshalInterface(ncu_params->stream, &IID_IClassFactory, (void **)&pProxy);
902     ok_ole_success(hr, CoUnmarshalInterface);
903     IStream_Release(ncu_params->stream);
904
905     ok_more_than_one_lock();
906
907     /* die without calling CoUninitialize */
908
909     return 0;
910 }
911
912 /* tests STA -> STA call during DLL_THREAD_DETACH doesn't deadlock */
913 static void test_no_couninitialize_client(void)
914 {
915     HRESULT hr;
916     IStream *pStream = NULL;
917     DWORD tid;
918     DWORD host_tid;
919     HANDLE thread;
920     HANDLE host_thread;
921     struct ncu_params ncu_params;
922
923     cLocks = 0;
924
925     hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
926     ok_ole_success(hr, CreateStreamOnHGlobal);
927     ncu_params.stream = pStream;
928
929     /* NOTE: assumes start_host_object uses an STA to host the object, as MTAs
930      * always deadlock when called from within DllMain */
931     host_tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown *)&Test_ClassFactory, MSHLFLAGS_NORMAL, &host_thread);
932     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
933
934     ok_more_than_one_lock();
935
936     thread = CreateThread(NULL, 0, no_couninitialize_client_proc, &ncu_params, 0, &tid);
937
938     WaitForSingleObject(thread, INFINITE);
939     CloseHandle(thread);
940
941     ok_no_locks();
942
943     end_host_object(host_tid, host_thread);
944 }
945
946 /* tests success case of a same-thread table-weak marshal, unmarshal, unmarshal */
947 static void test_tableweak_marshal_and_unmarshal_twice(void)
948 {
949     HRESULT hr;
950     IStream *pStream = NULL;
951     IUnknown *pProxy1 = NULL;
952     IUnknown *pProxy2 = NULL;
953     DWORD tid;
954     HANDLE thread;
955
956     cLocks = 0;
957
958     hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
959     ok_ole_success(hr, CreateStreamOnHGlobal);
960     tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_TABLEWEAK, &thread);
961
962     ok_more_than_one_lock();
963
964     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
965     hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy1);
966     ok_ole_success(hr, CoUnmarshalInterface);
967
968     ok_more_than_one_lock();
969
970     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
971     hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
972     IStream_Release(pStream);
973     ok_ole_success(hr, CoUnmarshalInterface);
974
975     ok_more_than_one_lock();
976
977     IUnknown_Release(pProxy1);
978     IUnknown_Release(pProxy2);
979
980     /* this line is shows the difference between weak and strong table marshaling:
981      *  weak has cLocks == 0
982      *  strong has cLocks > 0 */
983     ok_no_locks();
984
985     end_host_object(tid, thread);
986 }
987
988 /* tests releasing after unmarshaling one object */
989 static void test_tableweak_marshal_releasedata1(void)
990 {
991     HRESULT hr;
992     IStream *pStream = NULL;
993     IUnknown *pProxy1 = NULL;
994     IUnknown *pProxy2 = NULL;
995     DWORD tid;
996     HANDLE thread;
997
998     cLocks = 0;
999
1000     hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1001     ok_ole_success(hr, CreateStreamOnHGlobal);
1002     tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_TABLEWEAK, &thread);
1003
1004     ok_more_than_one_lock();
1005
1006     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1007     hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy1);
1008     ok_ole_success(hr, CoUnmarshalInterface);
1009
1010     ok_more_than_one_lock();
1011
1012     /* release the remaining reference on the object by calling
1013      * CoReleaseMarshalData in the hosting thread */
1014     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1015     release_host_object(tid);
1016
1017     ok_more_than_one_lock();
1018
1019     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1020     hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
1021     ok_ole_success(hr, CoUnmarshalInterface);
1022     IStream_Release(pStream);
1023
1024     ok_more_than_one_lock();
1025
1026     IUnknown_Release(pProxy1);
1027     if (pProxy2)
1028         IUnknown_Release(pProxy2);
1029
1030     /* this line is shows the difference between weak and strong table marshaling:
1031      *  weak has cLocks == 0
1032      *  strong has cLocks > 0 */
1033     ok_no_locks();
1034
1035     end_host_object(tid, thread);
1036 }
1037
1038 /* tests releasing after unmarshaling one object */
1039 static void test_tableweak_marshal_releasedata2(void)
1040 {
1041     HRESULT hr;
1042     IStream *pStream = NULL;
1043     IUnknown *pProxy = NULL;
1044     DWORD tid;
1045     HANDLE thread;
1046
1047     cLocks = 0;
1048
1049     hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1050     ok_ole_success(hr, CreateStreamOnHGlobal);
1051     tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_TABLEWEAK, &thread);
1052
1053     ok_more_than_one_lock();
1054
1055     /* release the remaining reference on the object by calling
1056      * CoReleaseMarshalData in the hosting thread */
1057     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1058     release_host_object(tid);
1059
1060     ok_no_locks();
1061
1062     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1063     hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
1064     todo_wine
1065     {
1066     ok(hr == CO_E_OBJNOTREG,
1067        "CoUnmarshalInterface should have failed with CO_E_OBJNOTREG, but returned 0x%08x instead\n",
1068        hr);
1069     }
1070     IStream_Release(pStream);
1071
1072     ok_no_locks();
1073
1074     end_host_object(tid, thread);
1075 }
1076
1077 /* tests success case of a same-thread table-strong marshal, unmarshal, unmarshal */
1078 static void test_tablestrong_marshal_and_unmarshal_twice(void)
1079 {
1080     HRESULT hr;
1081     IStream *pStream = NULL;
1082     IUnknown *pProxy1 = NULL;
1083     IUnknown *pProxy2 = NULL;
1084     DWORD tid;
1085     HANDLE thread;
1086
1087     cLocks = 0;
1088
1089     hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1090     ok_ole_success(hr, CreateStreamOnHGlobal);
1091     tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_TABLESTRONG, &thread);
1092
1093     ok_more_than_one_lock();
1094
1095     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1096     hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy1);
1097     ok_ole_success(hr, CoUnmarshalInterface);
1098
1099     ok_more_than_one_lock();
1100
1101     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1102     hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
1103     ok_ole_success(hr, CoUnmarshalInterface);
1104
1105     ok_more_than_one_lock();
1106
1107     if (pProxy1) IUnknown_Release(pProxy1);
1108     if (pProxy2) IUnknown_Release(pProxy2);
1109
1110     /* this line is shows the difference between weak and strong table marshaling:
1111      *  weak has cLocks == 0
1112      *  strong has cLocks > 0 */
1113     ok_more_than_one_lock();
1114
1115     /* release the remaining reference on the object by calling
1116      * CoReleaseMarshalData in the hosting thread */
1117     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1118     release_host_object(tid);
1119     IStream_Release(pStream);
1120
1121     ok_no_locks();
1122
1123     end_host_object(tid, thread);
1124 }
1125
1126 /* tests CoLockObjectExternal */
1127 static void test_lock_object_external(void)
1128 {
1129     HRESULT hr;
1130     IStream *pStream = NULL;
1131
1132     cLocks = 0;
1133
1134     /* test the stub manager creation aspect of CoLockObjectExternal when the
1135      * object hasn't been marshaled yet */
1136     CoLockObjectExternal((IUnknown*)&Test_ClassFactory, TRUE, TRUE);
1137
1138     ok_more_than_one_lock();
1139
1140     CoDisconnectObject((IUnknown*)&Test_ClassFactory, 0);
1141
1142     ok_no_locks();
1143
1144     /* test our empty stub manager being handled correctly in
1145      * CoMarshalInterface */
1146     CoLockObjectExternal((IUnknown*)&Test_ClassFactory, TRUE, TRUE);
1147
1148     hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1149     ok_ole_success(hr, CreateStreamOnHGlobal);
1150     hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1151     ok_ole_success(hr, CoMarshalInterface);
1152
1153     CoLockObjectExternal((IUnknown*)&Test_ClassFactory, TRUE, TRUE);
1154
1155     ok_more_than_one_lock();
1156
1157     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1158     hr = CoReleaseMarshalData(pStream);
1159     ok_ole_success(hr, CoReleaseMarshalData);
1160     IStream_Release(pStream);
1161
1162     ok_more_than_one_lock();
1163
1164     CoLockObjectExternal((IUnknown*)&Test_ClassFactory, FALSE, TRUE);
1165
1166     ok_more_than_one_lock();
1167
1168     CoLockObjectExternal((IUnknown*)&Test_ClassFactory, FALSE, TRUE);
1169
1170     ok_no_locks();
1171 }
1172
1173 /* tests disconnecting stubs */
1174 static void test_disconnect_stub(void)
1175 {
1176     HRESULT hr;
1177     IStream *pStream = NULL;
1178
1179     cLocks = 0;
1180
1181     hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1182     ok_ole_success(hr, CreateStreamOnHGlobal);
1183     hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1184     ok_ole_success(hr, CoMarshalInterface);
1185
1186     CoLockObjectExternal((IUnknown*)&Test_ClassFactory, TRUE, TRUE);
1187
1188     ok_more_than_one_lock();
1189     
1190     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1191     hr = CoReleaseMarshalData(pStream);
1192     ok_ole_success(hr, CoReleaseMarshalData);
1193     IStream_Release(pStream);
1194
1195     ok_more_than_one_lock();
1196
1197     CoDisconnectObject((IUnknown*)&Test_ClassFactory, 0);
1198
1199     ok_no_locks();
1200 }
1201
1202 /* tests failure case of a same-thread marshal and unmarshal twice */
1203 static void test_normal_marshal_and_unmarshal_twice(void)
1204 {
1205     HRESULT hr;
1206     IStream *pStream = NULL;
1207     IUnknown *pProxy1 = NULL;
1208     IUnknown *pProxy2 = NULL;
1209
1210     cLocks = 0;
1211
1212     hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1213     ok_ole_success(hr, CreateStreamOnHGlobal);
1214     hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1215     ok_ole_success(hr, CoMarshalInterface);
1216
1217     ok_more_than_one_lock();
1218     
1219     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1220     hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy1);
1221     ok_ole_success(hr, CoUnmarshalInterface);
1222
1223     ok_more_than_one_lock();
1224
1225     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1226     hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
1227     ok(hr == CO_E_OBJNOTCONNECTED,
1228         "CoUnmarshalInterface should have failed with error CO_E_OBJNOTCONNECTED for double unmarshal, instead of 0x%08x\n", hr);
1229
1230     IStream_Release(pStream);
1231
1232     ok_more_than_one_lock();
1233
1234     IUnknown_Release(pProxy1);
1235
1236     ok_no_locks();
1237 }
1238
1239 /* tests success case of marshaling and unmarshaling an HRESULT */
1240 static void test_hresult_marshaling(void)
1241 {
1242     HRESULT hr;
1243     HRESULT hr_marshaled = 0;
1244     IStream *pStream = NULL;
1245     static const HRESULT E_DEADBEEF = 0xdeadbeef;
1246
1247     hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1248     ok_ole_success(hr, CreateStreamOnHGlobal);
1249
1250     hr = CoMarshalHresult(pStream, E_DEADBEEF);
1251     ok_ole_success(hr, CoMarshalHresult);
1252
1253     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1254     hr = IStream_Read(pStream, &hr_marshaled, sizeof(HRESULT), NULL);
1255     ok_ole_success(hr, IStream_Read);
1256
1257     ok(hr_marshaled == E_DEADBEEF, "Didn't marshal HRESULT as expected: got value 0x%08x instead\n", hr_marshaled);
1258
1259     hr_marshaled = 0;
1260     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1261     hr = CoUnmarshalHresult(pStream, &hr_marshaled);
1262     ok_ole_success(hr, CoUnmarshalHresult);
1263
1264     ok(hr_marshaled == E_DEADBEEF, "Didn't marshal HRESULT as expected: got value 0x%08x instead\n", hr_marshaled);
1265
1266     IStream_Release(pStream);
1267 }
1268
1269
1270 /* helper for test_proxy_used_in_wrong_thread */
1271 static DWORD CALLBACK bad_thread_proc(LPVOID p)
1272 {
1273     IClassFactory * cf = (IClassFactory *)p;
1274     HRESULT hr;
1275     IUnknown * proxy = NULL;
1276
1277     hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
1278     todo_wine
1279     ok(hr == CO_E_NOTINITIALIZED,
1280        "COM should have failed with CO_E_NOTINITIALIZED on using proxy without apartment, but instead returned 0x%08x\n",
1281        hr);
1282
1283     hr = IClassFactory_QueryInterface(cf, &IID_IMultiQI, (LPVOID *)&proxy);
1284     /* Win9x returns S_OK, whilst NT returns RPC_E_WRONG_THREAD */
1285     trace("call to proxy's QueryInterface for local interface without apartment returned 0x%08x\n", hr);
1286     if (SUCCEEDED(hr))
1287         IUnknown_Release(proxy);
1288
1289     hr = IClassFactory_QueryInterface(cf, &IID_IStream, (LPVOID *)&proxy);
1290     /* Win9x returns E_NOINTERFACE, whilst NT returns RPC_E_WRONG_THREAD */
1291     trace("call to proxy's QueryInterface without apartment returned 0x%08x\n", hr);
1292     if (SUCCEEDED(hr))
1293         IUnknown_Release(proxy);
1294
1295     pCoInitializeEx(NULL, COINIT_MULTITHREADED);
1296
1297     hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
1298     if (proxy) IUnknown_Release(proxy);
1299     ok(hr == RPC_E_WRONG_THREAD,
1300         "COM should have failed with RPC_E_WRONG_THREAD on using proxy from wrong apartment, but instead returned 0x%08x\n",
1301         hr);
1302
1303     hr = IClassFactory_QueryInterface(cf, &IID_IStream, (LPVOID *)&proxy);
1304     /* Win9x returns E_NOINTERFACE, whilst NT returns RPC_E_WRONG_THREAD */
1305     trace("call to proxy's QueryInterface from wrong apartment returned 0x%08x\n", hr);
1306
1307     /* this statement causes Win9x DCOM to crash during CoUninitialize of
1308      * other apartment, so don't test this on Win9x (signified by NT-only
1309      * export of CoRegisterSurrogateEx) */
1310     if (GetProcAddress(GetModuleHandle("ole32"), "CoRegisterSurrogateEx"))
1311         /* now be really bad and release the proxy from the wrong apartment */
1312         IUnknown_Release(cf);
1313     else
1314         skip("skipping test for releasing proxy from wrong apartment that will succeed, but cause a crash during CoUninitialize\n");
1315
1316     CoUninitialize();
1317
1318     return 0;
1319 }
1320
1321 /* tests failure case of a using a proxy in the wrong apartment */
1322 static void test_proxy_used_in_wrong_thread(void)
1323 {
1324     HRESULT hr;
1325     IStream *pStream = NULL;
1326     IUnknown *pProxy = NULL;
1327     DWORD tid, tid2;
1328     HANDLE thread;
1329     HANDLE host_thread;
1330
1331     cLocks = 0;
1332
1333     hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1334     ok_ole_success(hr, CreateStreamOnHGlobal);
1335     tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &host_thread);
1336
1337     ok_more_than_one_lock();
1338     
1339     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1340     hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
1341     ok_ole_success(hr, CoUnmarshalInterface);
1342     IStream_Release(pStream);
1343
1344     ok_more_than_one_lock();
1345
1346     /* do a call that will fail, but result in IRemUnknown being used by the proxy */
1347     IClassFactory_QueryInterface(pProxy, &IID_IStream, (LPVOID *)&pStream);
1348
1349     /* create a thread that we can misbehave in */
1350     thread = CreateThread(NULL, 0, bad_thread_proc, (LPVOID)pProxy, 0, &tid2);
1351
1352     WaitForSingleObject(thread, INFINITE);
1353     CloseHandle(thread);
1354
1355     /* do release statement on Win9x that we should have done above */
1356     if (!GetProcAddress(GetModuleHandle("ole32"), "CoRegisterSurrogateEx"))
1357         IUnknown_Release(pProxy);
1358
1359     ok_no_locks();
1360
1361     end_host_object(tid, host_thread);
1362 }
1363
1364 static HRESULT WINAPI MessageFilter_QueryInterface(IMessageFilter *iface, REFIID riid, void ** ppvObj)
1365 {
1366     if (ppvObj == NULL) return E_POINTER;
1367
1368     if (IsEqualGUID(riid, &IID_IUnknown) ||
1369         IsEqualGUID(riid, &IID_IClassFactory))
1370     {
1371         *ppvObj = (LPVOID)iface;
1372         IClassFactory_AddRef(iface);
1373         return S_OK;
1374     }
1375
1376     return E_NOINTERFACE;
1377 }
1378
1379 static ULONG WINAPI MessageFilter_AddRef(IMessageFilter *iface)
1380 {
1381     return 2; /* non-heap object */
1382 }
1383
1384 static ULONG WINAPI MessageFilter_Release(IMessageFilter *iface)
1385 {
1386     return 1; /* non-heap object */
1387 }
1388
1389 static DWORD WINAPI MessageFilter_HandleInComingCall(
1390   IMessageFilter *iface,
1391   DWORD dwCallType,
1392   HTASK threadIDCaller,
1393   DWORD dwTickCount,
1394   LPINTERFACEINFO lpInterfaceInfo)
1395 {
1396     static int callcount = 0;
1397     DWORD ret;
1398     trace("HandleInComingCall\n");
1399     switch (callcount)
1400     {
1401     case 0:
1402         ret = SERVERCALL_REJECTED;
1403         break;
1404     case 1:
1405         ret = SERVERCALL_RETRYLATER;
1406         break;
1407     default:
1408         ret = SERVERCALL_ISHANDLED;
1409         break;
1410     }
1411     callcount++;
1412     return ret;
1413 }
1414
1415 static DWORD WINAPI MessageFilter_RetryRejectedCall(
1416   IMessageFilter *iface,
1417   HTASK threadIDCallee,
1418   DWORD dwTickCount,
1419   DWORD dwRejectType)
1420 {
1421     trace("RetryRejectedCall\n");
1422     return 0;
1423 }
1424
1425 static DWORD WINAPI MessageFilter_MessagePending(
1426   IMessageFilter *iface,
1427   HTASK threadIDCallee,
1428   DWORD dwTickCount,
1429   DWORD dwPendingType)
1430 {
1431     trace("MessagePending\n");
1432     return PENDINGMSG_WAITNOPROCESS;
1433 }
1434
1435 static const IMessageFilterVtbl MessageFilter_Vtbl =
1436 {
1437     MessageFilter_QueryInterface,
1438     MessageFilter_AddRef,
1439     MessageFilter_Release,
1440     MessageFilter_HandleInComingCall,
1441     MessageFilter_RetryRejectedCall,
1442     MessageFilter_MessagePending
1443 };
1444
1445 static IMessageFilter MessageFilter = { &MessageFilter_Vtbl };
1446
1447 static void test_message_filter(void)
1448 {
1449     HRESULT hr;
1450     IStream *pStream = NULL;
1451     IClassFactory *cf = NULL;
1452     DWORD tid;
1453     IUnknown *proxy = NULL;
1454     IMessageFilter *prev_filter = NULL;
1455     HANDLE thread;
1456
1457     cLocks = 0;
1458
1459     hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1460     ok_ole_success(hr, CreateStreamOnHGlobal);
1461     tid = start_host_object2(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &MessageFilter, &thread);
1462
1463     ok_more_than_one_lock();
1464
1465     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1466     hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&cf);
1467     ok_ole_success(hr, CoUnmarshalInterface);
1468     IStream_Release(pStream);
1469
1470     ok_more_than_one_lock();
1471
1472     hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
1473     ok(hr == RPC_E_CALL_REJECTED, "Call should have returned RPC_E_CALL_REJECTED, but return 0x%08x instead\n", hr);
1474     if (proxy) IUnknown_Release(proxy);
1475     proxy = NULL;
1476
1477     hr = CoRegisterMessageFilter(&MessageFilter, &prev_filter);
1478     ok_ole_success(hr, CoRegisterMessageFilter);
1479
1480     hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
1481     ok_ole_success(hr, IClassFactory_CreateInstance);
1482
1483     IUnknown_Release(proxy);
1484
1485     IClassFactory_Release(cf);
1486
1487     ok_no_locks();
1488
1489     end_host_object(tid, thread);
1490
1491     hr = CoRegisterMessageFilter(prev_filter, NULL);
1492     ok_ole_success(hr, CoRegisterMessageFilter);
1493 }
1494
1495 /* test failure case of trying to unmarshal from bad stream */
1496 static void test_bad_marshal_stream(void)
1497 {
1498     HRESULT hr;
1499     IStream *pStream = NULL;
1500
1501     hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1502     ok_ole_success(hr, CreateStreamOnHGlobal);
1503     hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1504     ok_ole_success(hr, CoMarshalInterface);
1505
1506     ok_more_than_one_lock();
1507
1508     /* try to read beyond end of stream */
1509     hr = CoReleaseMarshalData(pStream);
1510     ok(hr == STG_E_READFAULT, "Should have failed with STG_E_READFAULT, but returned 0x%08x instead\n", hr);
1511
1512     /* now release for real */
1513     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1514     hr = CoReleaseMarshalData(pStream);
1515     ok_ole_success(hr, CoReleaseMarshalData);
1516
1517     IStream_Release(pStream);
1518 }
1519
1520 /* tests that proxies implement certain interfaces */
1521 static void test_proxy_interfaces(void)
1522 {
1523     HRESULT hr;
1524     IStream *pStream = NULL;
1525     IUnknown *pProxy = NULL;
1526     IUnknown *pOtherUnknown = NULL;
1527     DWORD tid;
1528     HANDLE thread;
1529
1530     cLocks = 0;
1531
1532     hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1533     ok_ole_success(hr, CreateStreamOnHGlobal);
1534     tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
1535
1536     ok_more_than_one_lock();
1537         
1538     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1539     hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)&pProxy);
1540     ok_ole_success(hr, CoUnmarshalInterface);
1541     IStream_Release(pStream);
1542
1543     ok_more_than_one_lock();
1544
1545     hr = IUnknown_QueryInterface(pProxy, &IID_IUnknown, (LPVOID*)&pOtherUnknown);
1546     ok_ole_success(hr, IUnknown_QueryInterface IID_IUnknown);
1547     if (hr == S_OK) IUnknown_Release(pOtherUnknown);
1548
1549     hr = IUnknown_QueryInterface(pProxy, &IID_IClientSecurity, (LPVOID*)&pOtherUnknown);
1550     ok_ole_success(hr, IUnknown_QueryInterface IID_IClientSecurity);
1551     if (hr == S_OK) IUnknown_Release(pOtherUnknown);
1552
1553     hr = IUnknown_QueryInterface(pProxy, &IID_IMultiQI, (LPVOID*)&pOtherUnknown);
1554     ok_ole_success(hr, IUnknown_QueryInterface IID_IMultiQI);
1555     if (hr == S_OK) IUnknown_Release(pOtherUnknown);
1556
1557     hr = IUnknown_QueryInterface(pProxy, &IID_IMarshal, (LPVOID*)&pOtherUnknown);
1558     ok_ole_success(hr, IUnknown_QueryInterface IID_IMarshal);
1559     if (hr == S_OK) IUnknown_Release(pOtherUnknown);
1560
1561     /* IMarshal2 is also supported on NT-based systems, but is pretty much
1562      * useless as it has no more methods over IMarshal that it inherits from. */
1563
1564     IUnknown_Release(pProxy);
1565
1566     ok_no_locks();
1567
1568     end_host_object(tid, thread);
1569 }
1570
1571 typedef struct
1572 {
1573     const IUnknownVtbl *lpVtbl;
1574     ULONG refs;
1575 } HeapUnknown;
1576
1577 static HRESULT WINAPI HeapUnknown_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
1578 {
1579     if (IsEqualIID(riid, &IID_IUnknown))
1580     {
1581         IUnknown_AddRef(iface);
1582         *ppv = (LPVOID)iface;
1583         return S_OK;
1584     }
1585     *ppv = NULL;
1586     return E_NOINTERFACE;
1587 }
1588
1589 static ULONG WINAPI HeapUnknown_AddRef(IUnknown *iface)
1590 {
1591     HeapUnknown *This = (HeapUnknown *)iface;
1592     return InterlockedIncrement((LONG*)&This->refs);
1593 }
1594
1595 static ULONG WINAPI HeapUnknown_Release(IUnknown *iface)
1596 {
1597     HeapUnknown *This = (HeapUnknown *)iface;
1598     ULONG refs = InterlockedDecrement((LONG*)&This->refs);
1599     if (!refs) HeapFree(GetProcessHeap(), 0, This);
1600     return refs;
1601 }
1602
1603 static const IUnknownVtbl HeapUnknown_Vtbl =
1604 {
1605     HeapUnknown_QueryInterface,
1606     HeapUnknown_AddRef,
1607     HeapUnknown_Release
1608 };
1609
1610 static void test_proxybuffer(REFIID riid)
1611 {
1612     HRESULT hr;
1613     IPSFactoryBuffer *psfb;
1614     IRpcProxyBuffer *proxy;
1615     LPVOID lpvtbl;
1616     ULONG refs;
1617     CLSID clsid;
1618     HeapUnknown *pUnkOuter = (HeapUnknown *)HeapAlloc(GetProcessHeap(), 0, sizeof(*pUnkOuter));
1619
1620     pUnkOuter->lpVtbl = &HeapUnknown_Vtbl;
1621     pUnkOuter->refs = 1;
1622
1623     hr = CoGetPSClsid(riid, &clsid);
1624     ok_ole_success(hr, CoGetPSClsid);
1625
1626     hr = CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL, &IID_IPSFactoryBuffer, (LPVOID*)&psfb);
1627     ok_ole_success(hr, CoGetClassObject);
1628
1629     hr = IPSFactoryBuffer_CreateProxy(psfb, (IUnknown*)pUnkOuter, riid, &proxy, &lpvtbl);
1630     ok_ole_success(hr, IPSFactoryBuffer_CreateProxy);
1631     ok(lpvtbl != NULL, "IPSFactoryBuffer_CreateProxy succeeded, but returned a NULL vtable!\n");
1632
1633     /* release our reference to the outer unknown object - the PS factory
1634      * buffer will have AddRef's it in the CreateProxy call */
1635     refs = IUnknown_Release((IUnknown *)pUnkOuter);
1636     ok(refs == 1, "Ref count of outer unknown should have been 1 instead of %d\n", refs);
1637
1638     refs = IPSFactoryBuffer_Release(psfb);
1639     if (0)
1640     {
1641     /* not reliable on native. maybe it leaks references! */
1642     ok(refs == 0, "Ref-count leak of %d on IPSFactoryBuffer\n", refs);
1643     }
1644
1645     refs = IUnknown_Release((IUnknown *)lpvtbl);
1646     ok(refs == 0, "Ref-count leak of %d on IRpcProxyBuffer\n", refs);
1647
1648     refs = IRpcProxyBuffer_Release(proxy);
1649     ok(refs == 0, "Ref-count leak of %d on IRpcProxyBuffer\n", refs);
1650 }
1651
1652 static void test_stubbuffer(REFIID riid)
1653 {
1654     HRESULT hr;
1655     IPSFactoryBuffer *psfb;
1656     IRpcStubBuffer *stub;
1657     ULONG refs;
1658     CLSID clsid;
1659
1660     cLocks = 0;
1661
1662     hr = CoGetPSClsid(riid, &clsid);
1663     ok_ole_success(hr, CoGetPSClsid);
1664
1665     hr = CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL, &IID_IPSFactoryBuffer, (LPVOID*)&psfb);
1666     ok_ole_success(hr, CoGetClassObject);
1667
1668     hr = IPSFactoryBuffer_CreateStub(psfb, riid, (IUnknown*)&Test_ClassFactory, &stub);
1669     ok_ole_success(hr, IPSFactoryBuffer_CreateStub);
1670
1671     refs = IPSFactoryBuffer_Release(psfb);
1672     if (0)
1673     {
1674     /* not reliable on native. maybe it leaks references */
1675     ok(refs == 0, "Ref-count leak of %d on IPSFactoryBuffer\n", refs);
1676     }
1677
1678     ok_more_than_one_lock();
1679
1680     IRpcStubBuffer_Disconnect(stub);
1681
1682     ok_no_locks();
1683
1684     refs = IRpcStubBuffer_Release(stub);
1685     ok(refs == 0, "Ref-count leak of %d on IRpcProxyBuffer\n", refs);
1686 }
1687
1688 static HWND hwnd_app;
1689
1690 static HRESULT WINAPI TestRE_IClassFactory_CreateInstance(
1691     LPCLASSFACTORY iface,
1692     LPUNKNOWN pUnkOuter,
1693     REFIID riid,
1694     LPVOID *ppvObj)
1695 {
1696     DWORD_PTR res;
1697     if (IsEqualIID(riid, &IID_IWineTest))
1698     {
1699         BOOL ret = SendMessageTimeout(hwnd_app, WM_NULL, 0, 0, SMTO_BLOCK, 5000, &res);
1700         ok(ret, "Timed out sending a message to originating window during RPC call\n");
1701     }
1702     return S_FALSE;
1703 }
1704
1705 static const IClassFactoryVtbl TestREClassFactory_Vtbl =
1706 {
1707     Test_IClassFactory_QueryInterface,
1708     Test_IClassFactory_AddRef,
1709     Test_IClassFactory_Release,
1710     TestRE_IClassFactory_CreateInstance,
1711     Test_IClassFactory_LockServer
1712 };
1713
1714 IClassFactory TestRE_ClassFactory = { &TestREClassFactory_Vtbl };
1715
1716 static LRESULT CALLBACK window_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1717 {
1718     switch (msg)
1719     {
1720     case WM_USER:
1721     {
1722         HRESULT hr;
1723         IStream *pStream = NULL;
1724         IClassFactory *proxy = NULL;
1725         IUnknown *object;
1726         DWORD tid;
1727         HANDLE thread;
1728
1729         cLocks = 0;
1730
1731         hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1732         ok_ole_success(hr, CreateStreamOnHGlobal);
1733         tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&TestRE_ClassFactory, MSHLFLAGS_NORMAL, &thread);
1734
1735         ok_more_than_one_lock();
1736
1737         IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1738         hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&proxy);
1739         ok_ole_success(hr, CoReleaseMarshalData);
1740         IStream_Release(pStream);
1741
1742         ok_more_than_one_lock();
1743
1744         /* note the use of the magic IID_IWineTest value to tell remote thread
1745          * to try to send a message back to us */
1746         hr = IClassFactory_CreateInstance(proxy, NULL, &IID_IWineTest, (void **)&object);
1747
1748         IClassFactory_Release(proxy);
1749
1750         ok_no_locks();
1751
1752         end_host_object(tid, thread);
1753
1754         PostMessage(hwnd, WM_QUIT, 0, 0);
1755
1756         return 0;
1757     }
1758     case WM_USER+1:
1759     {
1760         HRESULT hr;
1761         IStream *pStream = NULL;
1762         IClassFactory *proxy = NULL;
1763         IUnknown *object;
1764         DWORD tid;
1765         HANDLE thread;
1766
1767         cLocks = 0;
1768
1769         hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1770         ok_ole_success(hr, CreateStreamOnHGlobal);
1771         tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&TestRE_ClassFactory, MSHLFLAGS_NORMAL, &thread);
1772
1773         ok_more_than_one_lock();
1774
1775         IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1776         hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&proxy);
1777         ok_ole_success(hr, CoReleaseMarshalData);
1778         IStream_Release(pStream);
1779
1780         ok_more_than_one_lock();
1781
1782         /* post quit message before a doing a COM call to show that a pending
1783         * WM_QUIT message doesn't stop the call from succeeding */
1784         PostMessage(hwnd, WM_QUIT, 0, 0);
1785         hr = IClassFactory_CreateInstance(proxy, NULL, &IID_IUnknown, (void **)&object);
1786
1787         IClassFactory_Release(proxy);
1788
1789         ok_no_locks();
1790
1791         end_host_object(tid, thread);
1792
1793         return 0;
1794     }
1795     case WM_USER+2:
1796     {
1797         HRESULT hr;
1798         IStream *pStream = NULL;
1799         IClassFactory *proxy = NULL;
1800         IUnknown *object;
1801         DWORD tid;
1802         HANDLE thread;
1803
1804         hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1805         ok_ole_success(hr, CreateStreamOnHGlobal);
1806         tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
1807
1808         IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1809         hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&proxy);
1810         ok_ole_success(hr, CoReleaseMarshalData);
1811         IStream_Release(pStream);
1812
1813         /* shows that COM calls executed during the processing of sent
1814          * messages should fail */
1815         hr = IClassFactory_CreateInstance(proxy, NULL, &IID_IUnknown, (void **)&object);
1816         ok(hr == RPC_E_CANTCALLOUT_ININPUTSYNCCALL,
1817            "COM call during processing of sent message should return RPC_E_CANTCALLOUT_ININPUTSYNCCALL instead of 0x%08x\n", hr);
1818
1819         IClassFactory_Release(proxy);
1820
1821         end_host_object(tid, thread);
1822
1823         PostQuitMessage(0);
1824
1825         return 0;
1826     }
1827     default:
1828         return DefWindowProc(hwnd, msg, wparam, lparam);
1829     }
1830 }
1831
1832 static void test_message_reentrancy(void)
1833 {
1834     WNDCLASS wndclass;
1835     MSG msg;
1836
1837     memset(&wndclass, 0, sizeof(wndclass));
1838     wndclass.lpfnWndProc = window_proc;
1839     wndclass.lpszClassName = "WineCOMTest";
1840     RegisterClass(&wndclass);
1841
1842     hwnd_app = CreateWindow("WineCOMTest", NULL, 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, NULL, 0);
1843     ok(hwnd_app != NULL, "Window creation failed\n");
1844
1845     /* start message re-entrancy test */
1846     PostMessage(hwnd_app, WM_USER, 0, 0);
1847
1848     while (GetMessage(&msg, NULL, 0, 0))
1849     {
1850         TranslateMessage(&msg);
1851         DispatchMessage(&msg);
1852     }
1853 }
1854
1855 static HRESULT WINAPI TestMsg_IClassFactory_CreateInstance(
1856     LPCLASSFACTORY iface,
1857     LPUNKNOWN pUnkOuter,
1858     REFIID riid,
1859     LPVOID *ppvObj)
1860 {
1861     *ppvObj = NULL;
1862     SendMessage(hwnd_app, WM_USER+2, 0, 0);
1863     return S_OK;
1864 }
1865
1866 static IClassFactoryVtbl TestMsgClassFactory_Vtbl =
1867 {
1868     Test_IClassFactory_QueryInterface,
1869     Test_IClassFactory_AddRef,
1870     Test_IClassFactory_Release,
1871     TestMsg_IClassFactory_CreateInstance,
1872     Test_IClassFactory_LockServer
1873 };
1874
1875 IClassFactory TestMsg_ClassFactory = { &TestMsgClassFactory_Vtbl };
1876
1877 static void test_call_from_message(void)
1878 {
1879     MSG msg;
1880     IStream *pStream;
1881     HRESULT hr;
1882     IClassFactory *proxy;
1883     DWORD tid;
1884     HANDLE thread;
1885     IUnknown *object;
1886
1887     hwnd_app = CreateWindow("WineCOMTest", NULL, 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, NULL, 0);
1888     ok(hwnd_app != NULL, "Window creation failed\n");
1889
1890     hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1891     ok_ole_success(hr, CreateStreamOnHGlobal);
1892     tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&TestMsg_ClassFactory, MSHLFLAGS_NORMAL, &thread);
1893
1894     ok_more_than_one_lock();
1895
1896     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1897     hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&proxy);
1898     ok_ole_success(hr, CoReleaseMarshalData);
1899     IStream_Release(pStream);
1900
1901     ok_more_than_one_lock();
1902
1903     /* start message re-entrancy test */
1904     hr = IClassFactory_CreateInstance(proxy, NULL, &IID_IUnknown, (void **)&object);
1905     ok_ole_success(hr, IClassFactory_CreateInstance);
1906
1907     IClassFactory_Release(proxy);
1908
1909     ok_no_locks();
1910
1911     end_host_object(tid, thread);
1912
1913     while (GetMessage(&msg, NULL, 0, 0))
1914     {
1915         TranslateMessage(&msg);
1916         DispatchMessage(&msg);
1917     }
1918 }
1919
1920 static void test_WM_QUIT_handling(void)
1921 {
1922     MSG msg;
1923
1924     hwnd_app = CreateWindow("WineCOMTest", NULL, 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, NULL, 0);
1925     ok(hwnd_app != NULL, "Window creation failed\n");
1926
1927     /* start WM_QUIT handling test */
1928     PostMessage(hwnd_app, WM_USER+1, 0, 0);
1929
1930     while (GetMessage(&msg, NULL, 0, 0))
1931     {
1932         TranslateMessage(&msg);
1933         DispatchMessage(&msg);
1934     }
1935 }
1936
1937 static void test_freethreadedmarshaldata(IStream *pStream, MSHCTX mshctx, void *ptr, DWORD mshlflags)
1938 {
1939     HGLOBAL hglobal;
1940     DWORD size;
1941     char *marshal_data;
1942     HRESULT hr;
1943
1944     hr = GetHGlobalFromStream(pStream, &hglobal);
1945     ok_ole_success(hr, GetHGlobalFromStream);
1946
1947     size = GlobalSize(hglobal);
1948
1949     marshal_data = (char *)GlobalLock(hglobal);
1950
1951     if (mshctx == MSHCTX_INPROC)
1952     {
1953         DWORD expected_size = sizeof(DWORD) + sizeof(void *) + sizeof(DWORD) + sizeof(GUID);
1954         ok(size == expected_size, "size should have been %d instead of %d\n", expected_size, size);
1955
1956         ok(*(DWORD *)marshal_data == mshlflags, "expected 0x%x, but got 0x%x for mshctx\n", mshlflags, *(DWORD *)marshal_data);
1957         marshal_data += sizeof(DWORD);
1958         ok(*(void **)marshal_data == ptr, "expected %p, but got %p for mshctx\n", ptr, *(void **)marshal_data);
1959         marshal_data += sizeof(void *);
1960         ok(*(DWORD *)marshal_data == 0, "expected 0x0, but got 0x%x\n", *(DWORD *)marshal_data);
1961         marshal_data += sizeof(DWORD);
1962         trace("got guid data: {%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\n",
1963             ((GUID *)marshal_data)->Data1, ((GUID *)marshal_data)->Data2, ((GUID *)marshal_data)->Data3,
1964             ((GUID *)marshal_data)->Data4[0], ((GUID *)marshal_data)->Data4[1], ((GUID *)marshal_data)->Data4[2], ((GUID *)marshal_data)->Data4[3],
1965             ((GUID *)marshal_data)->Data4[4], ((GUID *)marshal_data)->Data4[5], ((GUID *)marshal_data)->Data4[6], ((GUID *)marshal_data)->Data4[7]);
1966     }
1967     else
1968     {
1969         ok(size > sizeof(DWORD), "size should have been > sizeof(DWORD), not %d\n", size);
1970         ok(*(DWORD *)marshal_data == 0x574f454d /* MEOW */,
1971             "marshal data should be filled by standard marshal and start with MEOW signature\n");
1972     }
1973
1974     GlobalUnlock(hglobal);
1975 }
1976
1977 static void test_freethreadedmarshaler(void)
1978 {
1979     HRESULT hr;
1980     IUnknown *pFTUnknown;
1981     IMarshal *pFTMarshal;
1982     IStream *pStream;
1983     IUnknown *pProxy;
1984     static const LARGE_INTEGER llZero;
1985
1986     cLocks = 0;
1987     hr = CoCreateFreeThreadedMarshaler(NULL, &pFTUnknown);
1988     ok_ole_success(hr, CoCreateFreeThreadedMarshaler);
1989     hr = IUnknown_QueryInterface(pFTUnknown, &IID_IMarshal, (void **)&pFTMarshal);
1990     ok_ole_success(hr, IUnknown_QueryInterface);
1991     IUnknown_Release(pFTUnknown);
1992
1993     hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1994     ok_ole_success(hr, CreateStreamOnHGlobal);
1995
1996     /* inproc normal marshaling */
1997
1998     hr = IMarshal_MarshalInterface(pFTMarshal, pStream, &IID_IClassFactory,
1999         (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
2000     ok_ole_success(hr, IMarshal_MarshalInterface);
2001
2002     ok_more_than_one_lock();
2003
2004     test_freethreadedmarshaldata(pStream, MSHCTX_INPROC, &Test_ClassFactory, MSHLFLAGS_NORMAL);
2005
2006     IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2007     hr = IMarshal_UnmarshalInterface(pFTMarshal, pStream, &IID_IUnknown, (void **)&pProxy);
2008     ok_ole_success(hr, IMarshal_UnmarshalInterface);
2009
2010     IUnknown_Release(pProxy);
2011
2012     ok_no_locks();
2013
2014 /* native doesn't allow us to unmarshal or release the stream data,
2015  * presumably because it wants us to call CoMarshalInterface instead */
2016     if (0)
2017     {
2018     /* local normal marshaling */
2019
2020     IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2021     hr = IMarshal_MarshalInterface(pFTMarshal, pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_LOCAL, NULL, MSHLFLAGS_NORMAL);
2022     ok_ole_success(hr, IMarshal_MarshalInterface);
2023
2024     ok_more_than_one_lock();
2025
2026     test_freethreadedmarshaldata(pStream, MSHCTX_LOCAL, &Test_ClassFactory, MSHLFLAGS_NORMAL);
2027
2028     IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2029     hr = IMarshal_ReleaseMarshalData(pFTMarshal, pStream);
2030     ok_ole_success(hr, IMarshal_ReleaseMarshalData);
2031
2032     ok_no_locks();
2033     }
2034
2035     /* inproc table-strong marshaling */
2036
2037     IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2038     hr = IMarshal_MarshalInterface(pFTMarshal, pStream, &IID_IClassFactory,
2039         (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, (void *)0xdeadbeef,
2040         MSHLFLAGS_TABLESTRONG);
2041     ok_ole_success(hr, IMarshal_MarshalInterface);
2042
2043     ok_more_than_one_lock();
2044
2045     test_freethreadedmarshaldata(pStream, MSHCTX_INPROC, &Test_ClassFactory, MSHLFLAGS_TABLESTRONG);
2046
2047     IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2048     hr = IMarshal_UnmarshalInterface(pFTMarshal, pStream, &IID_IUnknown, (void **)&pProxy);
2049     ok_ole_success(hr, IMarshal_UnmarshalInterface);
2050
2051     IUnknown_Release(pProxy);
2052
2053     ok_more_than_one_lock();
2054
2055     IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2056     hr = IMarshal_ReleaseMarshalData(pFTMarshal, pStream);
2057     ok_ole_success(hr, IMarshal_ReleaseMarshalData);
2058
2059     ok_no_locks();
2060
2061     /* inproc table-weak marshaling */
2062
2063     IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2064     hr = IMarshal_MarshalInterface(pFTMarshal, pStream, &IID_IClassFactory,
2065         (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, (void *)0xdeadbeef,
2066         MSHLFLAGS_TABLEWEAK);
2067     ok_ole_success(hr, IMarshal_MarshalInterface);
2068
2069     ok_no_locks();
2070
2071     test_freethreadedmarshaldata(pStream, MSHCTX_INPROC, &Test_ClassFactory, MSHLFLAGS_TABLEWEAK);
2072
2073     IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2074     hr = IMarshal_UnmarshalInterface(pFTMarshal, pStream, &IID_IUnknown, (void **)&pProxy);
2075     ok_ole_success(hr, IMarshal_UnmarshalInterface);
2076
2077     ok_more_than_one_lock();
2078
2079     IUnknown_Release(pProxy);
2080
2081     ok_no_locks();
2082
2083     /* inproc normal marshaling (for extraordinary cases) */
2084
2085     IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2086     hr = IMarshal_MarshalInterface(pFTMarshal, pStream, &IID_IClassFactory,
2087         (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
2088     ok_ole_success(hr, IMarshal_MarshalInterface);
2089
2090     ok_more_than_one_lock();
2091
2092     /* this call shows that DisconnectObject does nothing */
2093     hr = IMarshal_DisconnectObject(pFTMarshal, 0);
2094     ok_ole_success(hr, IMarshal_DisconnectObject);
2095
2096     ok_more_than_one_lock();
2097
2098     IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2099     hr = IMarshal_ReleaseMarshalData(pFTMarshal, pStream);
2100     ok_ole_success(hr, IMarshal_ReleaseMarshalData);
2101
2102     ok_no_locks();
2103
2104     /* doesn't enforce marshaling rules here and allows us to unmarshal the
2105      * interface, even though it was freed above */
2106     IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2107     hr = IMarshal_UnmarshalInterface(pFTMarshal, pStream, &IID_IUnknown, (void **)&pProxy);
2108     ok_ole_success(hr, IMarshal_UnmarshalInterface);
2109
2110     ok_no_locks();
2111
2112     IStream_Release(pStream);
2113     IMarshal_Release(pFTMarshal);
2114 }
2115
2116 static void test_inproc_handler(void)
2117 {
2118     HRESULT hr;
2119     IUnknown *pObject;
2120     IUnknown *pObject2;
2121     char buffer[256];
2122     LPOLESTR pszClsid;
2123     HKEY hkey;
2124     DWORD dwDisposition;
2125     DWORD error;
2126
2127     hr = StringFromCLSID(&CLSID_WineTest, &pszClsid);
2128     ok_ole_success(hr, "StringFromCLSID");
2129     strcpy(buffer, "CLSID\\");
2130     WideCharToMultiByte(CP_ACP, 0, pszClsid, -1, buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), NULL, NULL);
2131     CoTaskMemFree(pszClsid);
2132     strcat(buffer, "\\InprocHandler32");
2133     error = RegCreateKeyEx(HKEY_CLASSES_ROOT, buffer, 0, NULL, 0, KEY_SET_VALUE, NULL, &hkey, &dwDisposition);
2134     ok(error == ERROR_SUCCESS, "RegCreateKeyEx failed with error %d\n", error);
2135     error = RegSetValueEx(hkey, NULL, 0, REG_SZ, (const unsigned char *)"ole32.dll", strlen("ole32.dll") + 1);
2136     ok(error == ERROR_SUCCESS, "RegSetValueEx failed with error %d\n", error);
2137     RegCloseKey(hkey);
2138
2139     hr = CoCreateInstance(&CLSID_WineTest, NULL, CLSCTX_INPROC_HANDLER, &IID_IUnknown, (void **)&pObject);
2140     todo_wine
2141     ok_ole_success(hr, "CoCreateInstance");
2142
2143     if (SUCCEEDED(hr))
2144     {
2145         hr = IUnknown_QueryInterface(pObject, &IID_IWineTest, (void **)&pObject2);
2146         ok(hr == E_NOINTERFACE, "IUnknown_QueryInterface on handler for invalid interface returned 0x%08x instead of E_NOINTERFACE\n", hr);
2147
2148         /* it's a handler as it supports IOleObject */
2149         hr = IUnknown_QueryInterface(pObject, &IID_IOleObject, (void **)&pObject2);
2150         ok_ole_success(hr, "IUnknown_QueryInterface(&IID_IOleObject)");
2151         IUnknown_Release(pObject2);
2152
2153         IUnknown_Release(pObject);
2154     }
2155
2156     RegDeleteKey(HKEY_CLASSES_ROOT, buffer);
2157     *strrchr(buffer, '\\') = '\0';
2158     RegDeleteKey(HKEY_CLASSES_ROOT, buffer);
2159 }
2160
2161 static HRESULT WINAPI Test_SMI_QueryInterface(
2162     IStdMarshalInfo *iface,
2163     REFIID riid,
2164     LPVOID *ppvObj)
2165 {
2166     if (ppvObj == NULL) return E_POINTER;
2167
2168     if (IsEqualGUID(riid, &IID_IUnknown) ||
2169         IsEqualGUID(riid, &IID_IStdMarshalInfo))
2170     {
2171         *ppvObj = (LPVOID)iface;
2172         IClassFactory_AddRef(iface);
2173         return S_OK;
2174     }
2175
2176     return E_NOINTERFACE;
2177 }
2178
2179 static ULONG WINAPI Test_SMI_AddRef(IStdMarshalInfo *iface)
2180 {
2181     LockModule();
2182     return 2; /* non-heap-based object */
2183 }
2184
2185 static ULONG WINAPI Test_SMI_Release(IStdMarshalInfo *iface)
2186 {
2187     UnlockModule();
2188     return 1; /* non-heap-based object */
2189 }
2190
2191 static HRESULT WINAPI Test_SMI_GetClassForHandler(
2192     IStdMarshalInfo *iface,
2193     DWORD dwDestContext,
2194     void *pvDestContext,
2195     CLSID *pClsid)
2196 {
2197     *pClsid = CLSID_WineTest;
2198     return S_OK;
2199 }
2200
2201 static const IStdMarshalInfoVtbl Test_SMI_Vtbl =
2202 {
2203     Test_SMI_QueryInterface,
2204     Test_SMI_AddRef,
2205     Test_SMI_Release,
2206     Test_SMI_GetClassForHandler
2207 };
2208
2209 static IStdMarshalInfo Test_SMI = {&Test_SMI_Vtbl};
2210
2211 static void test_handler_marshaling(void)
2212 {
2213     HRESULT hr;
2214     IStream *pStream = NULL;
2215     IUnknown *pProxy = NULL;
2216     IUnknown *pObject;
2217     DWORD tid;
2218     HANDLE thread;
2219     static const LARGE_INTEGER ullZero;
2220
2221     cLocks = 0;
2222
2223     hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
2224     ok_ole_success(hr, "CreateStreamOnHGlobal");
2225     tid = start_host_object(pStream, &IID_IUnknown, (IUnknown*)&Test_SMI, MSHLFLAGS_NORMAL, &thread);
2226
2227     ok_more_than_one_lock();
2228
2229     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
2230     hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)&pProxy);
2231     ok_ole_success(hr, "CoUnmarshalInterface");
2232     IStream_Release(pStream);
2233
2234     ok_more_than_one_lock();
2235
2236     hr = IUnknown_QueryInterface(pProxy, &IID_IWineTest, (void **)&pObject);
2237     ok(hr == E_NOINTERFACE, "IUnknown_QueryInterface with unknown IID should have returned E_NOINTERFACE instead of 0x%08x\n", hr);
2238
2239     /* it's a handler as it supports IOleObject */
2240     hr = IUnknown_QueryInterface(pProxy, &IID_IOleObject, (void **)&pObject);
2241     todo_wine
2242     ok_ole_success(hr, "IUnknown_QueryInterface(&IID_IOleObject)");
2243     if (SUCCEEDED(hr)) IUnknown_Release(pObject);
2244
2245     IUnknown_Release(pProxy);
2246
2247     ok_no_locks();
2248
2249     end_host_object(tid, thread);
2250
2251     /* FIXME: test IPersist interface has the same effect as IStdMarshalInfo */
2252 }
2253
2254
2255 static void test_client_security(void)
2256 {
2257     HRESULT hr;
2258     IStream *pStream = NULL;
2259     IClassFactory *pProxy = NULL;
2260     IUnknown *pProxy2 = NULL;
2261     IUnknown *pUnknown1 = NULL;
2262     IUnknown *pUnknown2 = NULL;
2263     IClientSecurity *pCliSec = NULL;
2264     IMarshal *pMarshal;
2265     DWORD tid;
2266     HANDLE thread;
2267     static const LARGE_INTEGER ullZero;
2268     DWORD dwAuthnSvc;
2269     DWORD dwAuthzSvc;
2270     OLECHAR *pServerPrincName;
2271     DWORD dwAuthnLevel;
2272     DWORD dwImpLevel;
2273     void *pAuthInfo;
2274     DWORD dwCapabilities;
2275     void *pv;
2276
2277     cLocks = 0;
2278
2279     hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
2280     ok_ole_success(hr, "CreateStreamOnHGlobal");
2281     tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
2282
2283     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
2284     hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
2285     ok_ole_success(hr, "CoUnmarshalInterface");
2286     IStream_Release(pStream);
2287
2288     hr = IUnknown_QueryInterface(pProxy, &IID_IUnknown, (LPVOID*)&pUnknown1);
2289     ok_ole_success(hr, "IUnknown_QueryInterface IID_IUnknown");
2290
2291     hr = IUnknown_QueryInterface(pProxy, &IID_IRemUnknown, (LPVOID*)&pProxy2);
2292     ok_ole_success(hr, "IUnknown_QueryInterface IID_IStream");
2293
2294     hr = IUnknown_QueryInterface(pProxy2, &IID_IUnknown, (LPVOID*)&pUnknown2);
2295     ok_ole_success(hr, "IUnknown_QueryInterface IID_IUnknown");
2296
2297     ok(pUnknown1 == pUnknown2, "both proxy's IUnknowns should be the same - %p, %p\n", pUnknown1, pUnknown2);
2298
2299     hr = IUnknown_QueryInterface(pProxy, &IID_IMarshal, (LPVOID*)&pMarshal);
2300     ok_ole_success(hr, "IUnknown_QueryInterface IID_IMarshal");
2301
2302     hr = IUnknown_QueryInterface(pProxy, &IID_IClientSecurity, (LPVOID*)&pCliSec);
2303     ok_ole_success(hr, "IUnknown_QueryInterface IID_IClientSecurity");
2304
2305     hr = IClientSecurity_QueryBlanket(pCliSec, (IUnknown *)pProxy, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
2306     todo_wine ok_ole_success(hr, "IClientSecurity_QueryBlanket (all NULLs)");
2307
2308     hr = IClientSecurity_QueryBlanket(pCliSec, (IUnknown *)pMarshal, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
2309     todo_wine ok(hr == E_NOINTERFACE, "IClientSecurity_QueryBlanket with local interface should have returned E_NOINTERFACE instead of 0x%08x\n", hr);
2310
2311     hr = IClientSecurity_QueryBlanket(pCliSec, (IUnknown *)pProxy, &dwAuthnSvc, &dwAuthzSvc, &pServerPrincName, &dwAuthnLevel, &dwImpLevel, &pAuthInfo, &dwCapabilities);
2312     todo_wine ok_ole_success(hr, "IClientSecurity_QueryBlanket");
2313
2314     hr = IClientSecurity_SetBlanket(pCliSec, (IUnknown *)pProxy, dwAuthnSvc, dwAuthzSvc, pServerPrincName, dwAuthnLevel, RPC_C_IMP_LEVEL_IMPERSONATE, pAuthInfo, dwCapabilities);
2315     todo_wine ok_ole_success(hr, "IClientSecurity_SetBlanket");
2316
2317     hr = IClassFactory_CreateInstance(pProxy, NULL, &IID_IWineTest, &pv);
2318     ok(hr == E_NOINTERFACE, "COM call should have succeeded instead of returning 0x%08x\n", hr);
2319
2320     hr = IClientSecurity_SetBlanket(pCliSec, (IUnknown *)pMarshal, dwAuthnSvc, dwAuthzSvc, pServerPrincName, dwAuthnLevel, dwImpLevel, pAuthInfo, dwCapabilities);
2321     todo_wine ok(hr == E_NOINTERFACE, "IClientSecurity_SetBlanket with local interface should have returned E_NOINTERFACE instead of 0x%08x\n", hr);
2322
2323     hr = IClientSecurity_SetBlanket(pCliSec, (IUnknown *)pProxy, 0xdeadbeef, dwAuthzSvc, pServerPrincName, dwAuthnLevel, dwImpLevel, pAuthInfo, dwCapabilities);
2324     todo_wine ok(hr == E_INVALIDARG, "IClientSecurity_SetBlanke with invalid dwAuthnSvc should have returned E_INVALIDARG instead of 0x%08x\n", hr);
2325
2326     CoTaskMemFree(pServerPrincName);
2327
2328     hr = IClientSecurity_QueryBlanket(pCliSec, (IUnknown *)pUnknown1, &dwAuthnSvc, &dwAuthzSvc, &pServerPrincName, &dwAuthnLevel, &dwImpLevel, &pAuthInfo, &dwCapabilities);
2329     todo_wine ok_ole_success(hr, "IClientSecurity_QueryBlanket(IUnknown)");
2330
2331     CoTaskMemFree(pServerPrincName);
2332
2333     IClassFactory_Release(pProxy);
2334     IUnknown_Release(pProxy2);
2335     IUnknown_Release(pUnknown1);
2336     IUnknown_Release(pUnknown2);
2337     IMarshal_Release(pMarshal);
2338     IClientSecurity_Release(pCliSec);
2339
2340     end_host_object(tid, thread);
2341 }
2342
2343 static HANDLE heventShutdown;
2344
2345 static void LockModuleOOP(void)
2346 {
2347     InterlockedIncrement(&cLocks); /* for test purposes only */
2348     CoAddRefServerProcess();
2349 }
2350
2351 static void UnlockModuleOOP(void)
2352 {
2353     InterlockedDecrement(&cLocks); /* for test purposes only */
2354     if (!CoReleaseServerProcess())
2355         SetEvent(heventShutdown);
2356 }
2357
2358 static HWND hwnd_app;
2359
2360 static HRESULT WINAPI TestOOP_IClassFactory_QueryInterface(
2361     LPCLASSFACTORY iface,
2362     REFIID riid,
2363     LPVOID *ppvObj)
2364 {
2365     if (ppvObj == NULL) return E_POINTER;
2366
2367     if (IsEqualGUID(riid, &IID_IUnknown) ||
2368         IsEqualGUID(riid, &IID_IClassFactory))
2369     {
2370         *ppvObj = (LPVOID)iface;
2371         IClassFactory_AddRef(iface);
2372         return S_OK;
2373     }
2374
2375     return E_NOINTERFACE;
2376 }
2377
2378 static ULONG WINAPI TestOOP_IClassFactory_AddRef(LPCLASSFACTORY iface)
2379 {
2380     return 2; /* non-heap-based object */
2381 }
2382
2383 static ULONG WINAPI TestOOP_IClassFactory_Release(LPCLASSFACTORY iface)
2384 {
2385     return 1; /* non-heap-based object */
2386 }
2387
2388 static HRESULT WINAPI TestOOP_IClassFactory_CreateInstance(
2389     LPCLASSFACTORY iface,
2390     LPUNKNOWN pUnkOuter,
2391     REFIID riid,
2392     LPVOID *ppvObj)
2393 {
2394     if (IsEqualIID(riid, &IID_IClassFactory) || IsEqualIID(riid, &IID_IUnknown))
2395     {
2396         *ppvObj = iface;
2397         return S_OK;
2398     }
2399     return CLASS_E_CLASSNOTAVAILABLE;
2400 }
2401
2402 static HRESULT WINAPI TestOOP_IClassFactory_LockServer(
2403     LPCLASSFACTORY iface,
2404     BOOL fLock)
2405 {
2406     if (fLock)
2407         LockModuleOOP();
2408     else
2409         UnlockModuleOOP();
2410     return S_OK;
2411 }
2412
2413 static const IClassFactoryVtbl TestClassFactoryOOP_Vtbl =
2414 {
2415     TestOOP_IClassFactory_QueryInterface,
2416     TestOOP_IClassFactory_AddRef,
2417     TestOOP_IClassFactory_Release,
2418     TestOOP_IClassFactory_CreateInstance,
2419     TestOOP_IClassFactory_LockServer
2420 };
2421
2422 static IClassFactory TestOOP_ClassFactory = { &TestClassFactoryOOP_Vtbl };
2423
2424 static void test_register_local_server(void)
2425 {
2426     DWORD cookie;
2427     HRESULT hr;
2428     HANDLE ready_event;
2429     HANDLE quit_event;
2430     DWORD wait;
2431
2432     heventShutdown = CreateEvent(NULL, TRUE, FALSE, NULL);
2433
2434     hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&TestOOP_ClassFactory,
2435                                CLSCTX_LOCAL_SERVER, REGCLS_SINGLEUSE, &cookie);
2436     ok_ole_success(hr, CoRegisterClassObject);
2437
2438     ready_event = CreateEvent(NULL, FALSE, FALSE, "Wine COM Test Ready Event");
2439     SetEvent(ready_event);
2440
2441     quit_event = CreateEvent(NULL, FALSE, FALSE, "Wine COM Test Quit Event");
2442
2443     do
2444     {
2445         wait = MsgWaitForMultipleObjects(1, &quit_event, FALSE, INFINITE, QS_ALLINPUT);
2446         if (wait == WAIT_OBJECT_0+1)
2447         {
2448             MSG msg;
2449             BOOL ret = PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
2450             if (ret)
2451             {
2452                 trace("Message 0x%x\n", msg.message);
2453                 TranslateMessage(&msg);
2454                 DispatchMessage(&msg);
2455             }
2456         }
2457     }
2458     while (wait == WAIT_OBJECT_0+1);
2459
2460     hr = CoRevokeClassObject(cookie);
2461     ok_ole_success(hr, CoRevokeClassObject);
2462 }
2463
2464 static HANDLE create_target_process(const char *arg)
2465 {
2466     char **argv;
2467     char cmdline[MAX_PATH];
2468     PROCESS_INFORMATION pi;
2469     STARTUPINFO si = { 0 };
2470     si.cb = sizeof(si);
2471
2472     winetest_get_mainargs( &argv );
2473     sprintf(cmdline, "%s %s %s", argv[0], argv[1], arg);
2474     ok(CreateProcess(argv[0], cmdline, NULL, NULL, FALSE, 0, NULL, NULL,
2475                      &si, &pi) != 0, "error: %u\n", GetLastError());
2476     ok(CloseHandle(pi.hThread) != 0, "error %u\n", GetLastError());
2477     return pi.hProcess;
2478 }
2479
2480 /* tests functions commonly used by out of process COM servers */
2481 static void test_local_server(void)
2482 {
2483     DWORD cookie;
2484     HRESULT hr;
2485     IClassFactory * cf;
2486     DWORD ret;
2487     HANDLE process;
2488     HANDLE quit_event;
2489     HANDLE ready_event;
2490
2491     heventShutdown = CreateEvent(NULL, TRUE, FALSE, NULL);
2492
2493     cLocks = 0;
2494
2495     /* Start the object suspended */
2496     hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&TestOOP_ClassFactory,
2497         CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE | REGCLS_SUSPENDED, &cookie);
2498     ok_ole_success(hr, CoRegisterClassObject);
2499
2500     /* ... and CoGetClassObject does not find it and fails when it looks for the
2501      * class in the registry */
2502     hr = CoGetClassObject(&CLSID_WineOOPTest, CLSCTX_INPROC_SERVER,
2503         NULL, &IID_IClassFactory, (LPVOID*)&cf);
2504     ok(hr == REGDB_E_CLASSNOTREG || /* NT */
2505        hr == S_OK /* Win9x */,
2506         "CoGetClassObject should have returned REGDB_E_CLASSNOTREG instead of 0x%08x\n", hr);
2507
2508     /* Resume the object suspended above ... */
2509     hr = CoResumeClassObjects();
2510     ok_ole_success(hr, CoResumeClassObjects);
2511
2512     /* ... and now it should succeed */
2513     hr = CoGetClassObject(&CLSID_WineOOPTest, CLSCTX_INPROC_SERVER,
2514         NULL, &IID_IClassFactory, (LPVOID*)&cf);
2515     ok_ole_success(hr, CoGetClassObject);
2516
2517     /* Now check the locking is working */
2518     /* NOTE: we are accessing the class directly, not through a proxy */
2519
2520     ok_no_locks();
2521
2522     hr = IClassFactory_LockServer(cf, TRUE);
2523     ok_ole_success(hr, IClassFactory_LockServer);
2524
2525     ok_more_than_one_lock();
2526     
2527     IClassFactory_LockServer(cf, FALSE);
2528     ok_ole_success(hr, IClassFactory_LockServer);
2529
2530     ok_no_locks();
2531
2532     IClassFactory_Release(cf);
2533
2534     /* wait for shutdown signal */
2535     ret = WaitForSingleObject(heventShutdown, 0);
2536     ok(ret != WAIT_TIMEOUT, "Server didn't shut down\n");
2537
2538     /* try to connect again after SCM has suspended registered class objects */
2539     hr = CoGetClassObject(&CLSID_WineOOPTest, CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER, NULL,
2540         &IID_IClassFactory, (LPVOID*)&cf);
2541     ok(hr == CO_E_SERVER_STOPPING || /* NT */
2542        hr == S_OK /* Win9x */,
2543         "CoGetClassObject should have returned CO_E_SERVER_STOPPING instead of 0x%08x\n", hr);
2544
2545     hr = CoRevokeClassObject(cookie);
2546     ok_ole_success(hr, CoRevokeClassObject);
2547
2548     CloseHandle(heventShutdown);
2549
2550     process = create_target_process("-Embedding");
2551     ok(process != NULL, "couldn't start local server process, error was %d\n", GetLastError());
2552
2553     ready_event = CreateEvent(NULL, FALSE, FALSE, "Wine COM Test Ready Event");
2554     WaitForSingleObject(ready_event, 1000);
2555     CloseHandle(ready_event);
2556
2557     hr = CoCreateInstance(&CLSID_WineOOPTest, NULL, CLSCTX_LOCAL_SERVER, &IID_IClassFactory, (void **)&cf);
2558     ok_ole_success(hr, CoCreateInstance);
2559
2560     IClassFactory_Release(cf);
2561
2562     hr = CoCreateInstance(&CLSID_WineOOPTest, NULL, CLSCTX_LOCAL_SERVER, &IID_IClassFactory, (void **)&cf);
2563     ok(hr == REGDB_E_CLASSNOTREG, "Second CoCreateInstance on REGCLS_SINGLEUSE object should have failed\n");
2564
2565     quit_event = CreateEvent(NULL, FALSE, FALSE, "Wine COM Test Quit Event");
2566     SetEvent(quit_event);
2567
2568     WaitForSingleObject(process, INFINITE);
2569     CloseHandle(quit_event);
2570     CloseHandle(process);
2571 }
2572
2573 struct git_params
2574 {
2575         DWORD cookie;
2576         IGlobalInterfaceTable *git;
2577 };
2578
2579 static DWORD CALLBACK get_global_interface_proc(LPVOID pv)
2580 {
2581         HRESULT hr;
2582         struct git_params *params = (struct git_params *)pv;
2583         IClassFactory *cf;
2584
2585         hr = IGlobalInterfaceTable_GetInterfaceFromGlobal(params->git, params->cookie, &IID_IClassFactory, (void **)&cf);
2586         ok(hr == CO_E_NOTINITIALIZED,
2587                 "IGlobalInterfaceTable_GetInterfaceFromGlobal should have failed with error CO_E_NOTINITIALIZED instead of 0x%08x\n",
2588                 hr);
2589
2590         CoInitialize(NULL);
2591
2592         hr = IGlobalInterfaceTable_GetInterfaceFromGlobal(params->git, params->cookie, &IID_IClassFactory, (void **)&cf);
2593         ok_ole_success(hr, IGlobalInterfaceTable_GetInterfaceFromGlobal);
2594
2595         IClassFactory_Release(cf);
2596
2597         CoUninitialize();
2598
2599         return hr;
2600 }
2601
2602 static void test_globalinterfacetable(void)
2603 {
2604         HRESULT hr;
2605         IGlobalInterfaceTable *git;
2606         DWORD cookie;
2607         HANDLE thread;
2608         DWORD tid;
2609         struct git_params params;
2610         DWORD ret;
2611         IUnknown *object;
2612
2613         trace("test_globalinterfacetable\n");
2614         cLocks = 0;
2615
2616         hr = CoCreateInstance(&CLSID_StdGlobalInterfaceTable, NULL, CLSCTX_INPROC_SERVER, &IID_IGlobalInterfaceTable, (void **)&git);
2617         ok_ole_success(hr, CoCreateInstance);
2618
2619         hr = IGlobalInterfaceTable_RegisterInterfaceInGlobal(git, (IUnknown *)&Test_ClassFactory, &IID_IClassFactory, &cookie);
2620         ok_ole_success(hr, IGlobalInterfaceTable_RegisterInterfaceInGlobal);
2621
2622         ok_more_than_one_lock();
2623
2624         params.cookie = cookie;
2625         params.git = git;
2626         /* note: params is on stack so we MUST wait for get_global_interface_proc
2627          * to exit before we can return */
2628         thread = CreateThread(NULL, 0, get_global_interface_proc, &params, 0, &tid);
2629
2630         ret = MsgWaitForMultipleObjects(1, &thread, FALSE, INFINITE, QS_ALLINPUT);
2631         while (ret == WAIT_OBJECT_0 + 1)
2632         {
2633                 MSG msg;
2634                 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
2635                         DispatchMessage(&msg);
2636                 ret = MsgWaitForMultipleObjects(1, &thread, FALSE, INFINITE, QS_ALLINPUT);
2637         }
2638
2639         CloseHandle(thread);
2640
2641         /* test getting interface from global with different iid */
2642         hr = IGlobalInterfaceTable_GetInterfaceFromGlobal(git, cookie, &IID_IUnknown, (void **)&object);
2643         todo_wine
2644         ok_ole_success(hr, IGlobalInterfaceTable_GetInterfaceFromGlobal);
2645         if (SUCCEEDED(hr)) IUnknown_Release(object);
2646
2647         /* test getting interface from global with same iid */
2648         hr = IGlobalInterfaceTable_GetInterfaceFromGlobal(git, cookie, &IID_IClassFactory, (void **)&object);
2649         ok_ole_success(hr, IGlobalInterfaceTable_GetInterfaceFromGlobal);
2650         IUnknown_Release(object);
2651
2652         hr = IGlobalInterfaceTable_RevokeInterfaceFromGlobal(git, cookie);
2653         ok_ole_success(hr, IGlobalInterfaceTable_RevokeInterfaceFromGlobal);
2654
2655         todo_wine
2656         ok_no_locks();
2657
2658         IGlobalInterfaceTable_Release(git);
2659 }
2660
2661 static const char *debugstr_iid(REFIID riid)
2662 {
2663     static char name[256];
2664     HKEY hkeyInterface;
2665     WCHAR bufferW[39];
2666     char buffer[39];
2667     LONG name_size = sizeof(name);
2668     StringFromGUID2(riid, bufferW, sizeof(bufferW)/sizeof(bufferW[0]));
2669     WideCharToMultiByte(CP_ACP, 0, bufferW, sizeof(bufferW)/sizeof(bufferW[0]), buffer, sizeof(buffer), NULL, NULL);
2670     if (RegOpenKeyEx(HKEY_CLASSES_ROOT, "Interface", 0, KEY_QUERY_VALUE, &hkeyInterface) != ERROR_SUCCESS)
2671     {
2672         memcpy(name, buffer, sizeof(buffer));
2673         goto done;
2674     }
2675     if (RegQueryValue(hkeyInterface, buffer, name, &name_size) != ERROR_SUCCESS)
2676     {
2677         memcpy(name, buffer, sizeof(buffer));
2678         goto done;
2679     }
2680     RegCloseKey(hkeyInterface);
2681 done:
2682     return name;
2683 }
2684
2685 static HRESULT WINAPI TestChannelHook_QueryInterface(IChannelHook *iface, REFIID riid, void **ppv)
2686 {
2687     if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IChannelHook))
2688     {
2689         *ppv = iface;
2690         IUnknown_AddRef(iface);
2691         return S_OK;
2692     }
2693
2694     *ppv = NULL;
2695     return E_NOINTERFACE;
2696 }
2697
2698 static ULONG WINAPI TestChannelHook_AddRef(IChannelHook *iface)
2699 {
2700     return 2;
2701 }
2702
2703 static ULONG WINAPI TestChannelHook_Release(IChannelHook *iface)
2704 {
2705     return 1;
2706 }
2707
2708 static void WINAPI TestChannelHook_ClientGetSize(
2709     IChannelHook *iface,
2710     REFGUID uExtent,
2711     REFIID  riid,
2712     ULONG  *pDataSize )
2713 {
2714     SChannelHookCallInfo *info = (SChannelHookCallInfo *)riid;
2715     trace("TestChannelHook_ClientGetBuffer\n");
2716     trace("\t%s method %d\n", debugstr_iid(riid), info->iMethod);
2717     trace("\tcid: %s\n", debugstr_iid(&info->uCausality));
2718     ok(info->cbSize == sizeof(*info), "info->cbSize was %d instead of %d\n", info->cbSize, (int)sizeof(*info));
2719     ok(info->dwServerPid == GetCurrentProcessId(), "info->dwServerPid was 0x%x instead of 0x%x\n", info->dwServerPid, GetCurrentProcessId());
2720     ok(!info->pObject, "info->pObject should be NULL\n");
2721     ok(IsEqualGUID(uExtent, &EXTENTID_WineTest), "uExtent wasn't correct\n");
2722
2723     *pDataSize = 1;
2724 }
2725
2726 static void WINAPI TestChannelHook_ClientFillBuffer(
2727     IChannelHook *iface,
2728     REFGUID uExtent,
2729     REFIID  riid,
2730     ULONG  *pDataSize,
2731     void   *pDataBuffer )
2732 {
2733     SChannelHookCallInfo *info = (SChannelHookCallInfo *)riid;
2734     trace("TestChannelHook_ClientFillBuffer\n");
2735     ok(info->cbSize == sizeof(*info), "info->cbSize was %d instead of %d\n", info->cbSize, (int)sizeof(*info));
2736     ok(info->dwServerPid == GetCurrentProcessId(), "info->dwServerPid was 0x%x instead of 0x%x\n", info->dwServerPid, GetCurrentProcessId());
2737     ok(!info->pObject, "info->pObject should be NULL\n");
2738     ok(IsEqualGUID(uExtent, &EXTENTID_WineTest), "uExtent wasn't correct\n");
2739
2740     *(unsigned char *)pDataBuffer = 0xcc;
2741     *pDataSize = 1;
2742 }
2743
2744 static void WINAPI TestChannelHook_ClientNotify(
2745     IChannelHook *iface,
2746     REFGUID uExtent,
2747     REFIID  riid,
2748     ULONG   cbDataSize,
2749     void   *pDataBuffer,
2750     DWORD   lDataRep,
2751     HRESULT hrFault )
2752 {
2753     SChannelHookCallInfo *info = (SChannelHookCallInfo *)riid;
2754     trace("TestChannelHook_ClientNotify hrFault = 0x%08x\n", hrFault);
2755     ok(info->cbSize == sizeof(*info), "info->cbSize was %d instead of %d\n", info->cbSize, (int)sizeof(*info));
2756     ok(info->dwServerPid == GetCurrentProcessId(), "info->dwServerPid was 0x%x instead of 0x%x\n", info->dwServerPid, GetCurrentProcessId());
2757     todo_wine {
2758     ok(info->pObject != NULL, "info->pObject shouldn't be NULL\n");
2759     }
2760     ok(IsEqualGUID(uExtent, &EXTENTID_WineTest), "uExtent wasn't correct\n");
2761 }
2762
2763 static void WINAPI TestChannelHook_ServerNotify(
2764     IChannelHook *iface,
2765     REFGUID uExtent,
2766     REFIID  riid,
2767     ULONG   cbDataSize,
2768     void   *pDataBuffer,
2769     DWORD   lDataRep )
2770 {
2771     SChannelHookCallInfo *info = (SChannelHookCallInfo *)riid;
2772     trace("TestChannelHook_ServerNotify\n");
2773     ok(info->cbSize == sizeof(*info), "info->cbSize was %d instead of %d\n", info->cbSize, (int)sizeof(*info));
2774     ok(info->dwServerPid == GetCurrentProcessId(), "info->dwServerPid was 0x%x instead of 0x%x\n", info->dwServerPid, GetCurrentProcessId());
2775     ok(info->pObject != NULL, "info->pObject shouldn't be NULL\n");
2776     ok(cbDataSize == 1, "cbDataSize should have been 1 instead of %d\n", cbDataSize);
2777     ok(*(unsigned char *)pDataBuffer == 0xcc, "pDataBuffer should have contained 0xcc instead of 0x%x\n", *(unsigned char *)pDataBuffer);
2778     ok(IsEqualGUID(uExtent, &EXTENTID_WineTest), "uExtent wasn't correct\n");
2779 }
2780
2781 static void WINAPI TestChannelHook_ServerGetSize(
2782     IChannelHook *iface,
2783     REFGUID uExtent,
2784     REFIID  riid,
2785     HRESULT hrFault,
2786     ULONG  *pDataSize )
2787 {
2788     SChannelHookCallInfo *info = (SChannelHookCallInfo *)riid;
2789     trace("TestChannelHook_ServerGetSize\n");
2790     trace("\t%s method %d\n", debugstr_iid(riid), info->iMethod);
2791     ok(info->cbSize == sizeof(*info), "info->cbSize was %d instead of %d\n", info->cbSize, (int)sizeof(*info));
2792     ok(info->dwServerPid == GetCurrentProcessId(), "info->dwServerPid was 0x%x instead of 0x%x\n", info->dwServerPid, GetCurrentProcessId());
2793     ok(info->pObject != NULL, "info->pObject shouldn't be NULL\n");
2794     ok(IsEqualGUID(uExtent, &EXTENTID_WineTest), "uExtent wasn't correct\n");
2795     if (hrFault != S_OK)
2796         trace("\thrFault = 0x%08x\n", hrFault);
2797
2798     *pDataSize = 0;
2799 }
2800
2801 static void WINAPI TestChannelHook_ServerFillBuffer(
2802     IChannelHook *iface,
2803     REFGUID uExtent,
2804     REFIID  riid,
2805     ULONG  *pDataSize,
2806     void   *pDataBuffer,
2807     HRESULT hrFault )
2808 {
2809     trace("TestChannelHook_ServerFillBuffer\n");
2810     ok(0, "TestChannelHook_ServerFillBuffer shouldn't be called\n");
2811 }
2812
2813 static const IChannelHookVtbl TestChannelHookVtbl =
2814 {
2815     TestChannelHook_QueryInterface,
2816     TestChannelHook_AddRef,
2817     TestChannelHook_Release,
2818     TestChannelHook_ClientGetSize,
2819     TestChannelHook_ClientFillBuffer,
2820     TestChannelHook_ClientNotify,
2821     TestChannelHook_ServerNotify,
2822     TestChannelHook_ServerGetSize,
2823     TestChannelHook_ServerFillBuffer,
2824 };
2825
2826 static IChannelHook TestChannelHook = { &TestChannelHookVtbl };
2827
2828 static void test_channel_hook(void)
2829 {
2830     IStream *pStream = NULL;
2831     IClassFactory *cf = NULL;
2832     DWORD tid;
2833     IUnknown *proxy = NULL;
2834     HANDLE thread;
2835     HRESULT hr;
2836
2837     hr = CoRegisterChannelHook(&EXTENTID_WineTest, &TestChannelHook);
2838     ok_ole_success(hr, CoRegisterChannelHook);
2839
2840     hr = CoRegisterMessageFilter(&MessageFilter, NULL);
2841     ok_ole_success(hr, CoRegisterMessageFilter);
2842
2843     cLocks = 0;
2844
2845     hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
2846     ok_ole_success(hr, CreateStreamOnHGlobal);
2847     tid = start_host_object2(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &MessageFilter, &thread);
2848
2849     ok_more_than_one_lock();
2850
2851     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
2852     hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&cf);
2853     ok_ole_success(hr, CoUnmarshalInterface);
2854     IStream_Release(pStream);
2855
2856     ok_more_than_one_lock();
2857
2858     hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
2859     ok_ole_success(hr, IClassFactory_CreateInstance);
2860     IUnknown_Release(proxy);
2861
2862     IClassFactory_Release(cf);
2863
2864     ok_no_locks();
2865
2866     end_host_object(tid, thread);
2867
2868     hr = CoRegisterMessageFilter(NULL, NULL);
2869     ok_ole_success(hr, CoRegisterMessageFilter);
2870 }
2871
2872 START_TEST(marshal)
2873 {
2874     WNDCLASS wndclass;
2875     HMODULE hOle32 = GetModuleHandle("ole32");
2876     int argc;
2877     char **argv;
2878
2879     if (!(pCoInitializeEx = (void*)GetProcAddress(hOle32, "CoInitializeEx"))) goto no_test;
2880
2881     argc = winetest_get_mainargs( &argv );
2882     if (argc > 2 && (!strcmp(argv[2], "-Embedding")))
2883     {
2884         pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
2885         test_register_local_server();
2886         CoUninitialize();
2887
2888         return;
2889     }
2890
2891     /* register a window class used in several tests */
2892     memset(&wndclass, 0, sizeof(wndclass));
2893     wndclass.lpfnWndProc = window_proc;
2894     wndclass.lpszClassName = "WineCOMTest";
2895     RegisterClass(&wndclass);
2896
2897     test_cocreateinstance_proxy();
2898
2899     pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
2900
2901     /* FIXME: test CoCreateInstanceEx */
2902
2903     /* lifecycle management and marshaling tests */
2904     test_no_marshaler();
2905     test_normal_marshal_and_release();
2906     test_normal_marshal_and_unmarshal();
2907     test_marshal_and_unmarshal_invalid();
2908     test_same_apartment_unmarshal_failure();
2909     test_interthread_marshal_and_unmarshal();
2910     test_proxy_marshal_and_unmarshal();
2911     test_proxy_marshal_and_unmarshal2();
2912     test_proxy_marshal_and_unmarshal_weak();
2913     test_proxy_marshal_and_unmarshal_strong();
2914     test_marshal_stub_apartment_shutdown();
2915     test_marshal_proxy_apartment_shutdown();
2916     test_marshal_proxy_mta_apartment_shutdown();
2917     test_no_couninitialize_server();
2918     test_no_couninitialize_client();
2919     test_tableweak_marshal_and_unmarshal_twice();
2920     test_tableweak_marshal_releasedata1();
2921     test_tableweak_marshal_releasedata2();
2922     test_tablestrong_marshal_and_unmarshal_twice();
2923     test_lock_object_external();
2924     test_disconnect_stub();
2925     test_normal_marshal_and_unmarshal_twice();
2926     test_hresult_marshaling();
2927     test_proxy_used_in_wrong_thread();
2928     test_message_filter();
2929     test_bad_marshal_stream();
2930     test_proxy_interfaces();
2931     test_stubbuffer(&IID_IClassFactory);
2932     test_proxybuffer(&IID_IClassFactory);
2933     test_message_reentrancy();
2934     test_call_from_message();
2935     test_WM_QUIT_handling();
2936     test_freethreadedmarshaler();
2937     test_inproc_handler();
2938     test_handler_marshaling();
2939     test_client_security();
2940
2941     test_local_server();
2942
2943     test_globalinterfacetable();
2944
2945     /* must be last test as channel hooks can't be unregistered */
2946     test_channel_hook();
2947
2948     CoUninitialize();
2949     return;
2950
2951 no_test:
2952     trace("You need DCOM95 installed to run this test\n");
2953     return;
2954 }