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