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