mshtml: Added IHTMLWindow2::focus implementation.
[wine] / dlls / ole32 / tests / marshal.c
1 /*
2  * Marshaling Tests
3  *
4  * Copyright 2004 Robert Shearman
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #define _WIN32_DCOM
22 #define COBJMACROS
23 #define CONST_VTABLE
24
25 #include <stdarg.h>
26 #include <stdio.h>
27
28 #include "windef.h"
29 #include "winbase.h"
30 #include "objbase.h"
31 #include "olectl.h"
32 #include "shlguid.h"
33 #include "shobjidl.h"
34 #include "initguid.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 static 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 = 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 = 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 = 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     ok( !WaitForSingleObject(marshal_event, 10000), "wait timed out\n" );
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     ok( !WaitForSingleObject(event, 10000), "wait timed out\n" );
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     ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
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 = 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     ok( !WaitForSingleObject(ncu_params->unmarshal_event, 10000), "wait timed out\n" );
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     ok( !WaitForSingleObject(ncu_params.marshal_event, 10000), "wait timed out\n" );
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     ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
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 = 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     ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
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, 10000, 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     ok( !WaitForSingleObject(data.hReadyEvent, 10000), "wait timed out\n" );
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     ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
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_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
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     /* test CoLockObjectExternal releases reference to object with
1273      * fLastUnlockReleases as TRUE and there are only strong references on
1274      * the object */
1275     CoLockObjectExternal((IUnknown*)&Test_ClassFactory, TRUE, FALSE);
1276
1277     ok_more_than_one_lock();
1278
1279     CoLockObjectExternal((IUnknown*)&Test_ClassFactory, FALSE, FALSE);
1280
1281     ok_no_locks();
1282
1283     /* test CoLockObjectExternal doesn't release the last reference to an
1284      * object with fLastUnlockReleases as TRUE and there is a weak reference
1285      * on the object */
1286     hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_TABLEWEAK);
1287     ok_ole_success(hr, CoMarshalInterface);
1288
1289     ok_more_than_one_lock();
1290
1291     CoLockObjectExternal((IUnknown*)&Test_ClassFactory, TRUE, FALSE);
1292
1293     ok_more_than_one_lock();
1294
1295     CoLockObjectExternal((IUnknown*)&Test_ClassFactory, FALSE, FALSE);
1296
1297     ok_more_than_one_lock();
1298
1299     CoDisconnectObject((IUnknown*)&Test_ClassFactory, 0);
1300
1301     ok_no_locks();
1302
1303     IStream_Release(pStream);
1304 }
1305
1306 /* tests disconnecting stubs */
1307 static void test_disconnect_stub(void)
1308 {
1309     HRESULT hr;
1310     IStream *pStream = NULL;
1311
1312     cLocks = 0;
1313
1314     hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1315     ok_ole_success(hr, CreateStreamOnHGlobal);
1316     hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1317     ok_ole_success(hr, CoMarshalInterface);
1318
1319     CoLockObjectExternal((IUnknown*)&Test_ClassFactory, TRUE, TRUE);
1320
1321     ok_more_than_one_lock();
1322     
1323     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1324     hr = CoReleaseMarshalData(pStream);
1325     ok_ole_success(hr, CoReleaseMarshalData);
1326     IStream_Release(pStream);
1327
1328     ok_more_than_one_lock();
1329
1330     CoDisconnectObject((IUnknown*)&Test_ClassFactory, 0);
1331
1332     ok_no_locks();
1333 }
1334
1335 /* tests failure case of a same-thread marshal and unmarshal twice */
1336 static void test_normal_marshal_and_unmarshal_twice(void)
1337 {
1338     HRESULT hr;
1339     IStream *pStream = NULL;
1340     IUnknown *pProxy1 = NULL;
1341     IUnknown *pProxy2 = NULL;
1342
1343     cLocks = 0;
1344
1345     hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1346     ok_ole_success(hr, CreateStreamOnHGlobal);
1347     hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1348     ok_ole_success(hr, CoMarshalInterface);
1349
1350     ok_more_than_one_lock();
1351     
1352     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1353     hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy1);
1354     ok_ole_success(hr, CoUnmarshalInterface);
1355
1356     ok_more_than_one_lock();
1357
1358     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1359     hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy2);
1360     ok(hr == CO_E_OBJNOTCONNECTED,
1361         "CoUnmarshalInterface should have failed with error CO_E_OBJNOTCONNECTED for double unmarshal, instead of 0x%08x\n", hr);
1362
1363     IStream_Release(pStream);
1364
1365     ok_more_than_one_lock();
1366
1367     IUnknown_Release(pProxy1);
1368
1369     ok_no_locks();
1370 }
1371
1372 /* tests success case of marshaling and unmarshaling an HRESULT */
1373 static void test_hresult_marshaling(void)
1374 {
1375     HRESULT hr;
1376     HRESULT hr_marshaled = 0;
1377     IStream *pStream = NULL;
1378     static const HRESULT E_DEADBEEF = 0xdeadbeef;
1379
1380     hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1381     ok_ole_success(hr, CreateStreamOnHGlobal);
1382
1383     hr = CoMarshalHresult(pStream, E_DEADBEEF);
1384     ok_ole_success(hr, CoMarshalHresult);
1385
1386     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1387     hr = IStream_Read(pStream, &hr_marshaled, sizeof(HRESULT), NULL);
1388     ok_ole_success(hr, IStream_Read);
1389
1390     ok(hr_marshaled == E_DEADBEEF, "Didn't marshal HRESULT as expected: got value 0x%08x instead\n", hr_marshaled);
1391
1392     hr_marshaled = 0;
1393     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1394     hr = CoUnmarshalHresult(pStream, &hr_marshaled);
1395     ok_ole_success(hr, CoUnmarshalHresult);
1396
1397     ok(hr_marshaled == E_DEADBEEF, "Didn't marshal HRESULT as expected: got value 0x%08x instead\n", hr_marshaled);
1398
1399     IStream_Release(pStream);
1400 }
1401
1402
1403 /* helper for test_proxy_used_in_wrong_thread */
1404 static DWORD CALLBACK bad_thread_proc(LPVOID p)
1405 {
1406     IClassFactory * cf = p;
1407     HRESULT hr;
1408     IUnknown * proxy = NULL;
1409
1410     hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
1411     todo_wine
1412     ok(hr == CO_E_NOTINITIALIZED,
1413        "COM should have failed with CO_E_NOTINITIALIZED on using proxy without apartment, but instead returned 0x%08x\n",
1414        hr);
1415
1416     hr = IClassFactory_QueryInterface(cf, &IID_IMultiQI, (LPVOID *)&proxy);
1417     /* Win9x returns S_OK, whilst NT returns RPC_E_WRONG_THREAD */
1418     trace("call to proxy's QueryInterface for local interface without apartment returned 0x%08x\n", hr);
1419     if (SUCCEEDED(hr))
1420         IUnknown_Release(proxy);
1421
1422     hr = IClassFactory_QueryInterface(cf, &IID_IStream, (LPVOID *)&proxy);
1423     /* Win9x returns E_NOINTERFACE, whilst NT returns RPC_E_WRONG_THREAD */
1424     trace("call to proxy's QueryInterface without apartment returned 0x%08x\n", hr);
1425     if (SUCCEEDED(hr))
1426         IUnknown_Release(proxy);
1427
1428     pCoInitializeEx(NULL, COINIT_MULTITHREADED);
1429
1430     hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
1431     if (proxy) IUnknown_Release(proxy);
1432     ok(hr == RPC_E_WRONG_THREAD,
1433         "COM should have failed with RPC_E_WRONG_THREAD on using proxy from wrong apartment, but instead returned 0x%08x\n",
1434         hr);
1435
1436     hr = IClassFactory_QueryInterface(cf, &IID_IStream, (LPVOID *)&proxy);
1437     /* Win9x returns E_NOINTERFACE, whilst NT returns RPC_E_WRONG_THREAD */
1438     trace("call to proxy's QueryInterface from wrong apartment returned 0x%08x\n", hr);
1439
1440     /* now be really bad and release the proxy from the wrong apartment */
1441     IUnknown_Release(cf);
1442
1443     CoUninitialize();
1444
1445     return 0;
1446 }
1447
1448 /* tests failure case of a using a proxy in the wrong apartment */
1449 static void test_proxy_used_in_wrong_thread(void)
1450 {
1451     HRESULT hr;
1452     IStream *pStream = NULL;
1453     IUnknown *pProxy = NULL;
1454     DWORD tid, tid2;
1455     HANDLE thread;
1456     HANDLE host_thread;
1457
1458     cLocks = 0;
1459
1460     hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1461     ok_ole_success(hr, CreateStreamOnHGlobal);
1462     tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &host_thread);
1463
1464     ok_more_than_one_lock();
1465     
1466     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1467     hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
1468     ok_ole_success(hr, CoUnmarshalInterface);
1469     IStream_Release(pStream);
1470
1471     ok_more_than_one_lock();
1472
1473     /* do a call that will fail, but result in IRemUnknown being used by the proxy */
1474     IClassFactory_QueryInterface(pProxy, &IID_IStream, (LPVOID *)&pStream);
1475
1476     /* create a thread that we can misbehave in */
1477     thread = CreateThread(NULL, 0, bad_thread_proc, pProxy, 0, &tid2);
1478
1479     ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
1480     CloseHandle(thread);
1481
1482     /* do release statement on Win9x that we should have done above */
1483     if (!GetProcAddress(GetModuleHandle("ole32"), "CoRegisterSurrogateEx"))
1484         IUnknown_Release(pProxy);
1485
1486     ok_no_locks();
1487
1488     end_host_object(tid, host_thread);
1489 }
1490
1491 static HRESULT WINAPI MessageFilter_QueryInterface(IMessageFilter *iface, REFIID riid, void ** ppvObj)
1492 {
1493     if (ppvObj == NULL) return E_POINTER;
1494
1495     if (IsEqualGUID(riid, &IID_IUnknown) ||
1496         IsEqualGUID(riid, &IID_IClassFactory))
1497     {
1498         *ppvObj = iface;
1499         IClassFactory_AddRef(iface);
1500         return S_OK;
1501     }
1502
1503     return E_NOINTERFACE;
1504 }
1505
1506 static ULONG WINAPI MessageFilter_AddRef(IMessageFilter *iface)
1507 {
1508     return 2; /* non-heap object */
1509 }
1510
1511 static ULONG WINAPI MessageFilter_Release(IMessageFilter *iface)
1512 {
1513     return 1; /* non-heap object */
1514 }
1515
1516 static DWORD WINAPI MessageFilter_HandleInComingCall(
1517   IMessageFilter *iface,
1518   DWORD dwCallType,
1519   HTASK threadIDCaller,
1520   DWORD dwTickCount,
1521   LPINTERFACEINFO lpInterfaceInfo)
1522 {
1523     static int callcount = 0;
1524     DWORD ret;
1525     trace("HandleInComingCall\n");
1526     switch (callcount)
1527     {
1528     case 0:
1529         ret = SERVERCALL_REJECTED;
1530         break;
1531     case 1:
1532         ret = SERVERCALL_RETRYLATER;
1533         break;
1534     default:
1535         ret = SERVERCALL_ISHANDLED;
1536         break;
1537     }
1538     callcount++;
1539     return ret;
1540 }
1541
1542 static DWORD WINAPI MessageFilter_RetryRejectedCall(
1543   IMessageFilter *iface,
1544   HTASK threadIDCallee,
1545   DWORD dwTickCount,
1546   DWORD dwRejectType)
1547 {
1548     trace("RetryRejectedCall\n");
1549     return 0;
1550 }
1551
1552 static DWORD WINAPI MessageFilter_MessagePending(
1553   IMessageFilter *iface,
1554   HTASK threadIDCallee,
1555   DWORD dwTickCount,
1556   DWORD dwPendingType)
1557 {
1558     trace("MessagePending\n");
1559     return PENDINGMSG_WAITNOPROCESS;
1560 }
1561
1562 static const IMessageFilterVtbl MessageFilter_Vtbl =
1563 {
1564     MessageFilter_QueryInterface,
1565     MessageFilter_AddRef,
1566     MessageFilter_Release,
1567     MessageFilter_HandleInComingCall,
1568     MessageFilter_RetryRejectedCall,
1569     MessageFilter_MessagePending
1570 };
1571
1572 static IMessageFilter MessageFilter = { &MessageFilter_Vtbl };
1573
1574 static void test_message_filter(void)
1575 {
1576     HRESULT hr;
1577     IStream *pStream = NULL;
1578     IClassFactory *cf = NULL;
1579     DWORD tid;
1580     IUnknown *proxy = NULL;
1581     IMessageFilter *prev_filter = NULL;
1582     HANDLE thread;
1583
1584     cLocks = 0;
1585
1586     hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1587     ok_ole_success(hr, CreateStreamOnHGlobal);
1588     tid = start_host_object2(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &MessageFilter, &thread);
1589
1590     ok_more_than_one_lock();
1591
1592     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1593     hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&cf);
1594     ok_ole_success(hr, CoUnmarshalInterface);
1595     IStream_Release(pStream);
1596
1597     ok_more_than_one_lock();
1598
1599     hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
1600     ok(hr == RPC_E_CALL_REJECTED, "Call should have returned RPC_E_CALL_REJECTED, but return 0x%08x instead\n", hr);
1601     if (proxy) IUnknown_Release(proxy);
1602     proxy = NULL;
1603
1604     hr = CoRegisterMessageFilter(&MessageFilter, &prev_filter);
1605     ok_ole_success(hr, CoRegisterMessageFilter);
1606
1607     hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
1608     ok_ole_success(hr, IClassFactory_CreateInstance);
1609
1610     IUnknown_Release(proxy);
1611
1612     IClassFactory_Release(cf);
1613
1614     ok_no_locks();
1615
1616     end_host_object(tid, thread);
1617
1618     hr = CoRegisterMessageFilter(prev_filter, NULL);
1619     ok_ole_success(hr, CoRegisterMessageFilter);
1620 }
1621
1622 /* test failure case of trying to unmarshal from bad stream */
1623 static void test_bad_marshal_stream(void)
1624 {
1625     HRESULT hr;
1626     IStream *pStream = NULL;
1627
1628     hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1629     ok_ole_success(hr, CreateStreamOnHGlobal);
1630     hr = CoMarshalInterface(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1631     ok_ole_success(hr, CoMarshalInterface);
1632
1633     ok_more_than_one_lock();
1634
1635     /* try to read beyond end of stream */
1636     hr = CoReleaseMarshalData(pStream);
1637     ok(hr == STG_E_READFAULT, "Should have failed with STG_E_READFAULT, but returned 0x%08x instead\n", hr);
1638
1639     /* now release for real */
1640     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1641     hr = CoReleaseMarshalData(pStream);
1642     ok_ole_success(hr, CoReleaseMarshalData);
1643
1644     IStream_Release(pStream);
1645 }
1646
1647 /* tests that proxies implement certain interfaces */
1648 static void test_proxy_interfaces(void)
1649 {
1650     HRESULT hr;
1651     IStream *pStream = NULL;
1652     IUnknown *pProxy = NULL;
1653     IUnknown *pOtherUnknown = NULL;
1654     DWORD tid;
1655     HANDLE thread;
1656
1657     cLocks = 0;
1658
1659     hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1660     ok_ole_success(hr, CreateStreamOnHGlobal);
1661     tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
1662
1663     ok_more_than_one_lock();
1664         
1665     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1666     hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)&pProxy);
1667     ok_ole_success(hr, CoUnmarshalInterface);
1668     IStream_Release(pStream);
1669
1670     ok_more_than_one_lock();
1671
1672     hr = IUnknown_QueryInterface(pProxy, &IID_IUnknown, (LPVOID*)&pOtherUnknown);
1673     ok_ole_success(hr, IUnknown_QueryInterface IID_IUnknown);
1674     if (hr == S_OK) IUnknown_Release(pOtherUnknown);
1675
1676     hr = IUnknown_QueryInterface(pProxy, &IID_IClientSecurity, (LPVOID*)&pOtherUnknown);
1677     ok_ole_success(hr, IUnknown_QueryInterface IID_IClientSecurity);
1678     if (hr == S_OK) IUnknown_Release(pOtherUnknown);
1679
1680     hr = IUnknown_QueryInterface(pProxy, &IID_IMultiQI, (LPVOID*)&pOtherUnknown);
1681     ok_ole_success(hr, IUnknown_QueryInterface IID_IMultiQI);
1682     if (hr == S_OK) IUnknown_Release(pOtherUnknown);
1683
1684     hr = IUnknown_QueryInterface(pProxy, &IID_IMarshal, (LPVOID*)&pOtherUnknown);
1685     ok_ole_success(hr, IUnknown_QueryInterface IID_IMarshal);
1686     if (hr == S_OK) IUnknown_Release(pOtherUnknown);
1687
1688     /* IMarshal2 is also supported on NT-based systems, but is pretty much
1689      * useless as it has no more methods over IMarshal that it inherits from. */
1690
1691     IUnknown_Release(pProxy);
1692
1693     ok_no_locks();
1694
1695     end_host_object(tid, thread);
1696 }
1697
1698 typedef struct
1699 {
1700     IUnknown IUnknown_iface;
1701     ULONG refs;
1702 } HeapUnknown;
1703
1704 static inline HeapUnknown *impl_from_IUnknown(IUnknown *iface)
1705 {
1706     return CONTAINING_RECORD(iface, HeapUnknown, IUnknown_iface);
1707 }
1708
1709 static HRESULT WINAPI HeapUnknown_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
1710 {
1711     if (IsEqualIID(riid, &IID_IUnknown))
1712     {
1713         IUnknown_AddRef(iface);
1714         *ppv = iface;
1715         return S_OK;
1716     }
1717     *ppv = NULL;
1718     return E_NOINTERFACE;
1719 }
1720
1721 static ULONG WINAPI HeapUnknown_AddRef(IUnknown *iface)
1722 {
1723     HeapUnknown *This = impl_from_IUnknown(iface);
1724     return InterlockedIncrement((LONG*)&This->refs);
1725 }
1726
1727 static ULONG WINAPI HeapUnknown_Release(IUnknown *iface)
1728 {
1729     HeapUnknown *This = impl_from_IUnknown(iface);
1730     ULONG refs = InterlockedDecrement((LONG*)&This->refs);
1731     if (!refs) HeapFree(GetProcessHeap(), 0, This);
1732     return refs;
1733 }
1734
1735 static const IUnknownVtbl HeapUnknown_Vtbl =
1736 {
1737     HeapUnknown_QueryInterface,
1738     HeapUnknown_AddRef,
1739     HeapUnknown_Release
1740 };
1741
1742 static void test_proxybuffer(REFIID riid)
1743 {
1744     HRESULT hr;
1745     IPSFactoryBuffer *psfb;
1746     IRpcProxyBuffer *proxy;
1747     LPVOID lpvtbl;
1748     ULONG refs;
1749     CLSID clsid;
1750     HeapUnknown *pUnkOuter = HeapAlloc(GetProcessHeap(), 0, sizeof(*pUnkOuter));
1751
1752     pUnkOuter->IUnknown_iface.lpVtbl = &HeapUnknown_Vtbl;
1753     pUnkOuter->refs = 1;
1754
1755     hr = CoGetPSClsid(riid, &clsid);
1756     ok_ole_success(hr, CoGetPSClsid);
1757
1758     hr = CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL, &IID_IPSFactoryBuffer, (LPVOID*)&psfb);
1759     ok_ole_success(hr, CoGetClassObject);
1760
1761     hr = IPSFactoryBuffer_CreateProxy(psfb, &pUnkOuter->IUnknown_iface, riid, &proxy, &lpvtbl);
1762     ok_ole_success(hr, IPSFactoryBuffer_CreateProxy);
1763     ok(lpvtbl != NULL, "IPSFactoryBuffer_CreateProxy succeeded, but returned a NULL vtable!\n");
1764
1765     /* release our reference to the outer unknown object - the PS factory
1766      * buffer will have AddRef's it in the CreateProxy call */
1767     refs = IUnknown_Release(&pUnkOuter->IUnknown_iface);
1768     ok(refs == 1, "Ref count of outer unknown should have been 1 instead of %d\n", refs);
1769
1770     refs = IPSFactoryBuffer_Release(psfb);
1771     if (0)
1772     {
1773     /* not reliable on native. maybe it leaks references! */
1774     ok(refs == 0, "Ref-count leak of %d on IPSFactoryBuffer\n", refs);
1775     }
1776
1777     refs = IUnknown_Release((IUnknown *)lpvtbl);
1778     ok(refs == 0, "Ref-count leak of %d on IRpcProxyBuffer\n", refs);
1779
1780     refs = IRpcProxyBuffer_Release(proxy);
1781     ok(refs == 0, "Ref-count leak of %d on IRpcProxyBuffer\n", refs);
1782 }
1783
1784 static void test_stubbuffer(REFIID riid)
1785 {
1786     HRESULT hr;
1787     IPSFactoryBuffer *psfb;
1788     IRpcStubBuffer *stub;
1789     ULONG refs;
1790     CLSID clsid;
1791
1792     cLocks = 0;
1793
1794     hr = CoGetPSClsid(riid, &clsid);
1795     ok_ole_success(hr, CoGetPSClsid);
1796
1797     hr = CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL, &IID_IPSFactoryBuffer, (LPVOID*)&psfb);
1798     ok_ole_success(hr, CoGetClassObject);
1799
1800     hr = IPSFactoryBuffer_CreateStub(psfb, riid, (IUnknown*)&Test_ClassFactory, &stub);
1801     ok_ole_success(hr, IPSFactoryBuffer_CreateStub);
1802
1803     refs = IPSFactoryBuffer_Release(psfb);
1804     if (0)
1805     {
1806     /* not reliable on native. maybe it leaks references */
1807     ok(refs == 0, "Ref-count leak of %d on IPSFactoryBuffer\n", refs);
1808     }
1809
1810     ok_more_than_one_lock();
1811
1812     IRpcStubBuffer_Disconnect(stub);
1813
1814     ok_no_locks();
1815
1816     refs = IRpcStubBuffer_Release(stub);
1817     ok(refs == 0, "Ref-count leak of %d on IRpcProxyBuffer\n", refs);
1818 }
1819
1820 static HWND hwnd_app;
1821
1822 static HRESULT WINAPI TestRE_IClassFactory_CreateInstance(
1823     LPCLASSFACTORY iface,
1824     LPUNKNOWN pUnkOuter,
1825     REFIID riid,
1826     LPVOID *ppvObj)
1827 {
1828     DWORD_PTR res;
1829     if (IsEqualIID(riid, &IID_IWineTest))
1830     {
1831         BOOL ret = SendMessageTimeout(hwnd_app, WM_NULL, 0, 0, SMTO_BLOCK, 5000, &res);
1832         ok(ret, "Timed out sending a message to originating window during RPC call\n");
1833     }
1834     *ppvObj = NULL;
1835     return S_FALSE;
1836 }
1837
1838 static const IClassFactoryVtbl TestREClassFactory_Vtbl =
1839 {
1840     Test_IClassFactory_QueryInterface,
1841     Test_IClassFactory_AddRef,
1842     Test_IClassFactory_Release,
1843     TestRE_IClassFactory_CreateInstance,
1844     Test_IClassFactory_LockServer
1845 };
1846
1847 static IClassFactory TestRE_ClassFactory = { &TestREClassFactory_Vtbl };
1848
1849 static LRESULT CALLBACK window_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1850 {
1851     switch (msg)
1852     {
1853     case WM_USER:
1854     {
1855         HRESULT hr;
1856         IStream *pStream = NULL;
1857         IClassFactory *proxy = NULL;
1858         IUnknown *object;
1859         DWORD tid;
1860         HANDLE thread;
1861
1862         cLocks = 0;
1863
1864         hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1865         ok_ole_success(hr, CreateStreamOnHGlobal);
1866         tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&TestRE_ClassFactory, MSHLFLAGS_NORMAL, &thread);
1867
1868         ok_more_than_one_lock();
1869
1870         IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1871         hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&proxy);
1872         ok_ole_success(hr, CoReleaseMarshalData);
1873         IStream_Release(pStream);
1874
1875         ok_more_than_one_lock();
1876
1877         /* note the use of the magic IID_IWineTest value to tell remote thread
1878          * to try to send a message back to us */
1879         hr = IClassFactory_CreateInstance(proxy, NULL, &IID_IWineTest, (void **)&object);
1880
1881         IClassFactory_Release(proxy);
1882
1883         ok_no_locks();
1884
1885         end_host_object(tid, thread);
1886
1887         PostMessage(hwnd, WM_QUIT, 0, 0);
1888
1889         return 0;
1890     }
1891     case WM_USER+1:
1892     {
1893         HRESULT hr;
1894         IStream *pStream = NULL;
1895         IClassFactory *proxy = NULL;
1896         IUnknown *object;
1897         DWORD tid;
1898         HANDLE thread;
1899
1900         cLocks = 0;
1901
1902         hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1903         ok_ole_success(hr, CreateStreamOnHGlobal);
1904         tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&TestRE_ClassFactory, MSHLFLAGS_NORMAL, &thread);
1905
1906         ok_more_than_one_lock();
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         ok_more_than_one_lock();
1914
1915         /* post quit message before a doing a COM call to show that a pending
1916         * WM_QUIT message doesn't stop the call from succeeding */
1917         PostMessage(hwnd, WM_QUIT, 0, 0);
1918         hr = IClassFactory_CreateInstance(proxy, NULL, &IID_IUnknown, (void **)&object);
1919
1920         IClassFactory_Release(proxy);
1921
1922         ok_no_locks();
1923
1924         end_host_object(tid, thread);
1925
1926         return 0;
1927     }
1928     case WM_USER+2:
1929     {
1930         HRESULT hr;
1931         IStream *pStream = NULL;
1932         IClassFactory *proxy = NULL;
1933         IUnknown *object;
1934         DWORD tid;
1935         HANDLE thread;
1936
1937         hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1938         ok_ole_success(hr, CreateStreamOnHGlobal);
1939         tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
1940
1941         IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1942         hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&proxy);
1943         ok_ole_success(hr, CoReleaseMarshalData);
1944         IStream_Release(pStream);
1945
1946         /* shows that COM calls executed during the processing of sent
1947          * messages should fail */
1948         hr = IClassFactory_CreateInstance(proxy, NULL, &IID_IUnknown, (void **)&object);
1949         ok(hr == RPC_E_CANTCALLOUT_ININPUTSYNCCALL,
1950            "COM call during processing of sent message should return RPC_E_CANTCALLOUT_ININPUTSYNCCALL instead of 0x%08x\n", hr);
1951
1952         IClassFactory_Release(proxy);
1953
1954         end_host_object(tid, thread);
1955
1956         PostQuitMessage(0);
1957
1958         return 0;
1959     }
1960     default:
1961         return DefWindowProc(hwnd, msg, wparam, lparam);
1962     }
1963 }
1964
1965 static void register_test_window(void)
1966 {
1967     WNDCLASS wndclass;
1968
1969     memset(&wndclass, 0, sizeof(wndclass));
1970     wndclass.lpfnWndProc = window_proc;
1971     wndclass.lpszClassName = "WineCOMTest";
1972     RegisterClass(&wndclass);
1973 }
1974
1975 static void test_message_reentrancy(void)
1976 {
1977     MSG msg;
1978
1979     hwnd_app = CreateWindow("WineCOMTest", NULL, 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, NULL, 0);
1980     ok(hwnd_app != NULL, "Window creation failed\n");
1981
1982     /* start message re-entrancy test */
1983     PostMessage(hwnd_app, WM_USER, 0, 0);
1984
1985     while (GetMessage(&msg, NULL, 0, 0))
1986     {
1987         TranslateMessage(&msg);
1988         DispatchMessage(&msg);
1989     }
1990     DestroyWindow(hwnd_app);
1991 }
1992
1993 static HRESULT WINAPI TestMsg_IClassFactory_CreateInstance(
1994     LPCLASSFACTORY iface,
1995     LPUNKNOWN pUnkOuter,
1996     REFIID riid,
1997     LPVOID *ppvObj)
1998 {
1999     *ppvObj = NULL;
2000     SendMessage(hwnd_app, WM_USER+2, 0, 0);
2001     return S_OK;
2002 }
2003
2004 static IClassFactoryVtbl TestMsgClassFactory_Vtbl =
2005 {
2006     Test_IClassFactory_QueryInterface,
2007     Test_IClassFactory_AddRef,
2008     Test_IClassFactory_Release,
2009     TestMsg_IClassFactory_CreateInstance,
2010     Test_IClassFactory_LockServer
2011 };
2012
2013 static IClassFactory TestMsg_ClassFactory = { &TestMsgClassFactory_Vtbl };
2014
2015 static void test_call_from_message(void)
2016 {
2017     MSG msg;
2018     IStream *pStream;
2019     HRESULT hr;
2020     IClassFactory *proxy;
2021     DWORD tid;
2022     HANDLE thread;
2023     IUnknown *object;
2024
2025     hwnd_app = CreateWindow("WineCOMTest", NULL, 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, NULL, 0);
2026     ok(hwnd_app != NULL, "Window creation failed\n");
2027
2028     hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
2029     ok_ole_success(hr, CreateStreamOnHGlobal);
2030     tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&TestMsg_ClassFactory, MSHLFLAGS_NORMAL, &thread);
2031
2032     ok_more_than_one_lock();
2033
2034     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
2035     hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&proxy);
2036     ok_ole_success(hr, CoReleaseMarshalData);
2037     IStream_Release(pStream);
2038
2039     ok_more_than_one_lock();
2040
2041     /* start message re-entrancy test */
2042     hr = IClassFactory_CreateInstance(proxy, NULL, &IID_IUnknown, (void **)&object);
2043     ok_ole_success(hr, IClassFactory_CreateInstance);
2044
2045     IClassFactory_Release(proxy);
2046
2047     ok_no_locks();
2048
2049     end_host_object(tid, thread);
2050
2051     while (GetMessage(&msg, NULL, 0, 0))
2052     {
2053         TranslateMessage(&msg);
2054         DispatchMessage(&msg);
2055     }
2056     DestroyWindow(hwnd_app);
2057 }
2058
2059 static void test_WM_QUIT_handling(void)
2060 {
2061     MSG msg;
2062
2063     hwnd_app = CreateWindow("WineCOMTest", NULL, 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, NULL, 0);
2064     ok(hwnd_app != NULL, "Window creation failed\n");
2065
2066     /* start WM_QUIT handling test */
2067     PostMessage(hwnd_app, WM_USER+1, 0, 0);
2068
2069     while (GetMessage(&msg, NULL, 0, 0))
2070     {
2071         TranslateMessage(&msg);
2072         DispatchMessage(&msg);
2073     }
2074 }
2075
2076 static SIZE_T round_global_size(SIZE_T size)
2077 {
2078     static SIZE_T global_size_alignment = -1;
2079     if (global_size_alignment == -1)
2080     {
2081         void *p = GlobalAlloc(GMEM_FIXED, 1);
2082         global_size_alignment = GlobalSize(p);
2083         GlobalFree(p);
2084     }
2085
2086     return ((size + global_size_alignment - 1) & ~(global_size_alignment - 1));
2087 }
2088
2089 static void test_freethreadedmarshaldata(IStream *pStream, MSHCTX mshctx, void *ptr, DWORD mshlflags)
2090 {
2091     HGLOBAL hglobal;
2092     DWORD size;
2093     char *marshal_data;
2094     HRESULT hr;
2095
2096     hr = GetHGlobalFromStream(pStream, &hglobal);
2097     ok_ole_success(hr, GetHGlobalFromStream);
2098
2099     size = GlobalSize(hglobal);
2100
2101     marshal_data = GlobalLock(hglobal);
2102
2103     if (mshctx == MSHCTX_INPROC)
2104     {
2105         DWORD expected_size = round_global_size(3*sizeof(DWORD) + sizeof(GUID));
2106         ok(size == expected_size ||
2107            broken(size == (2*sizeof(DWORD))) /* Win9x & NT4 */,
2108            "size should have been %d instead of %d\n", expected_size, size);
2109
2110         ok(*(DWORD *)marshal_data == mshlflags, "expected 0x%x, but got 0x%x for mshctx\n", mshlflags, *(DWORD *)marshal_data);
2111         marshal_data += sizeof(DWORD);
2112         ok(*(void **)marshal_data == ptr, "expected %p, but got %p for mshctx\n", ptr, *(void **)marshal_data);
2113         marshal_data += sizeof(void *);
2114         if (sizeof(void*) == 4 && size >= 3*sizeof(DWORD))
2115         {
2116             ok(*(DWORD *)marshal_data == 0, "expected 0x0, but got 0x%x\n", *(DWORD *)marshal_data);
2117             marshal_data += sizeof(DWORD);
2118         }
2119         if (size >= 3*sizeof(DWORD) + sizeof(GUID))
2120         {
2121             trace("got guid data: {%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\n",
2122                 ((GUID *)marshal_data)->Data1, ((GUID *)marshal_data)->Data2, ((GUID *)marshal_data)->Data3,
2123                 ((GUID *)marshal_data)->Data4[0], ((GUID *)marshal_data)->Data4[1], ((GUID *)marshal_data)->Data4[2], ((GUID *)marshal_data)->Data4[3],
2124                 ((GUID *)marshal_data)->Data4[4], ((GUID *)marshal_data)->Data4[5], ((GUID *)marshal_data)->Data4[6], ((GUID *)marshal_data)->Data4[7]);
2125         }
2126     }
2127     else
2128     {
2129         ok(size > sizeof(DWORD), "size should have been > sizeof(DWORD), not %d\n", size);
2130         ok(*(DWORD *)marshal_data == 0x574f454d /* MEOW */,
2131             "marshal data should be filled by standard marshal and start with MEOW signature\n");
2132     }
2133
2134     GlobalUnlock(hglobal);
2135 }
2136
2137 static void test_freethreadedmarshaler(void)
2138 {
2139     HRESULT hr;
2140     IUnknown *pFTUnknown;
2141     IMarshal *pFTMarshal;
2142     IStream *pStream;
2143     IUnknown *pProxy;
2144     static const LARGE_INTEGER llZero;
2145
2146     cLocks = 0;
2147     hr = CoCreateFreeThreadedMarshaler(NULL, &pFTUnknown);
2148     ok_ole_success(hr, CoCreateFreeThreadedMarshaler);
2149     hr = IUnknown_QueryInterface(pFTUnknown, &IID_IMarshal, (void **)&pFTMarshal);
2150     ok_ole_success(hr, IUnknown_QueryInterface);
2151     IUnknown_Release(pFTUnknown);
2152
2153     hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
2154     ok_ole_success(hr, CreateStreamOnHGlobal);
2155
2156     /* inproc normal marshaling */
2157
2158     hr = IMarshal_MarshalInterface(pFTMarshal, pStream, &IID_IClassFactory,
2159         &Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
2160     ok_ole_success(hr, IMarshal_MarshalInterface);
2161
2162     ok_more_than_one_lock();
2163
2164     test_freethreadedmarshaldata(pStream, MSHCTX_INPROC, &Test_ClassFactory, MSHLFLAGS_NORMAL);
2165
2166     IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2167     hr = IMarshal_UnmarshalInterface(pFTMarshal, pStream, &IID_IUnknown, (void **)&pProxy);
2168     ok_ole_success(hr, IMarshal_UnmarshalInterface);
2169
2170     IUnknown_Release(pProxy);
2171
2172     ok_no_locks();
2173
2174 /* native doesn't allow us to unmarshal or release the stream data,
2175  * presumably because it wants us to call CoMarshalInterface instead */
2176     if (0)
2177     {
2178     /* local normal marshaling */
2179
2180     IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2181     hr = IMarshal_MarshalInterface(pFTMarshal, pStream, &IID_IClassFactory, &Test_ClassFactory, MSHCTX_LOCAL, NULL, MSHLFLAGS_NORMAL);
2182     ok_ole_success(hr, IMarshal_MarshalInterface);
2183
2184     ok_more_than_one_lock();
2185
2186     test_freethreadedmarshaldata(pStream, MSHCTX_LOCAL, &Test_ClassFactory, MSHLFLAGS_NORMAL);
2187
2188     IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2189     hr = IMarshal_ReleaseMarshalData(pFTMarshal, pStream);
2190     ok_ole_success(hr, IMarshal_ReleaseMarshalData);
2191
2192     ok_no_locks();
2193     }
2194
2195     /* inproc table-strong marshaling */
2196
2197     IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2198     hr = IMarshal_MarshalInterface(pFTMarshal, pStream, &IID_IClassFactory,
2199         (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, (void *)0xdeadbeef,
2200         MSHLFLAGS_TABLESTRONG);
2201     ok_ole_success(hr, IMarshal_MarshalInterface);
2202
2203     ok_more_than_one_lock();
2204
2205     test_freethreadedmarshaldata(pStream, MSHCTX_INPROC, &Test_ClassFactory, MSHLFLAGS_TABLESTRONG);
2206
2207     IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2208     hr = IMarshal_UnmarshalInterface(pFTMarshal, pStream, &IID_IUnknown, (void **)&pProxy);
2209     ok_ole_success(hr, IMarshal_UnmarshalInterface);
2210
2211     IUnknown_Release(pProxy);
2212
2213     ok_more_than_one_lock();
2214
2215     IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2216     hr = IMarshal_ReleaseMarshalData(pFTMarshal, pStream);
2217     ok_ole_success(hr, IMarshal_ReleaseMarshalData);
2218
2219     ok_no_locks();
2220
2221     /* inproc table-weak marshaling */
2222
2223     IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2224     hr = IMarshal_MarshalInterface(pFTMarshal, pStream, &IID_IClassFactory,
2225         (IUnknown*)&Test_ClassFactory, MSHCTX_INPROC, (void *)0xdeadbeef,
2226         MSHLFLAGS_TABLEWEAK);
2227     ok_ole_success(hr, IMarshal_MarshalInterface);
2228
2229     ok_no_locks();
2230
2231     test_freethreadedmarshaldata(pStream, MSHCTX_INPROC, &Test_ClassFactory, MSHLFLAGS_TABLEWEAK);
2232
2233     IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2234     hr = IMarshal_UnmarshalInterface(pFTMarshal, pStream, &IID_IUnknown, (void **)&pProxy);
2235     ok_ole_success(hr, IMarshal_UnmarshalInterface);
2236
2237     ok_more_than_one_lock();
2238
2239     IUnknown_Release(pProxy);
2240
2241     ok_no_locks();
2242
2243     /* inproc normal marshaling (for extraordinary cases) */
2244
2245     IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2246     hr = IMarshal_MarshalInterface(pFTMarshal, pStream, &IID_IClassFactory,
2247         &Test_ClassFactory, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
2248     ok_ole_success(hr, IMarshal_MarshalInterface);
2249
2250     ok_more_than_one_lock();
2251
2252     /* this call shows that DisconnectObject does nothing */
2253     hr = IMarshal_DisconnectObject(pFTMarshal, 0);
2254     ok_ole_success(hr, IMarshal_DisconnectObject);
2255
2256     ok_more_than_one_lock();
2257
2258     IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2259     hr = IMarshal_ReleaseMarshalData(pFTMarshal, pStream);
2260     ok_ole_success(hr, IMarshal_ReleaseMarshalData);
2261
2262     ok_no_locks();
2263
2264     /* doesn't enforce marshaling rules here and allows us to unmarshal the
2265      * interface, even though it was freed above */
2266     IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
2267     hr = IMarshal_UnmarshalInterface(pFTMarshal, pStream, &IID_IUnknown, (void **)&pProxy);
2268     ok_ole_success(hr, IMarshal_UnmarshalInterface);
2269
2270     ok_no_locks();
2271
2272     IStream_Release(pStream);
2273     IMarshal_Release(pFTMarshal);
2274 }
2275
2276 static void reg_unreg_wine_test_class(BOOL Register)
2277 {
2278     HRESULT hr;
2279     char buffer[256];
2280     LPOLESTR pszClsid;
2281     HKEY hkey;
2282     DWORD dwDisposition;
2283     DWORD error;
2284
2285     hr = StringFromCLSID(&CLSID_WineTest, &pszClsid);
2286     ok_ole_success(hr, "StringFromCLSID");
2287     strcpy(buffer, "CLSID\\");
2288     WideCharToMultiByte(CP_ACP, 0, pszClsid, -1, buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), NULL, NULL);
2289     CoTaskMemFree(pszClsid);
2290     strcat(buffer, "\\InprocHandler32");
2291     if (Register)
2292     {
2293         error = RegCreateKeyEx(HKEY_CLASSES_ROOT, buffer, 0, NULL, 0, KEY_SET_VALUE, NULL, &hkey, &dwDisposition);
2294         ok(error == ERROR_SUCCESS, "RegCreateKeyEx failed with error %d\n", error);
2295         error = RegSetValueEx(hkey, NULL, 0, REG_SZ, (const unsigned char *)"\"ole32.dll\"", strlen("\"ole32.dll\"") + 1);
2296         ok(error == ERROR_SUCCESS, "RegSetValueEx failed with error %d\n", error);
2297         RegCloseKey(hkey);
2298     }
2299     else
2300     {
2301         RegDeleteKey(HKEY_CLASSES_ROOT, buffer);
2302         *strrchr(buffer, '\\') = '\0';
2303         RegDeleteKey(HKEY_CLASSES_ROOT, buffer);
2304     }
2305 }
2306
2307 static void test_inproc_handler(void)
2308 {
2309     HRESULT hr;
2310     IUnknown *pObject;
2311     IUnknown *pObject2;
2312
2313     reg_unreg_wine_test_class(TRUE);
2314
2315     hr = CoCreateInstance(&CLSID_WineTest, NULL, CLSCTX_INPROC_HANDLER, &IID_IUnknown, (void **)&pObject);
2316     ok_ole_success(hr, "CoCreateInstance");
2317
2318     if (SUCCEEDED(hr))
2319     {
2320         hr = IUnknown_QueryInterface(pObject, &IID_IWineTest, (void **)&pObject2);
2321         ok(hr == E_NOINTERFACE, "IUnknown_QueryInterface on handler for invalid interface returned 0x%08x instead of E_NOINTERFACE\n", hr);
2322
2323         /* it's a handler as it supports IOleObject */
2324         hr = IUnknown_QueryInterface(pObject, &IID_IOleObject, (void **)&pObject2);
2325         ok_ole_success(hr, "IUnknown_QueryInterface(&IID_IOleObject)");
2326         IUnknown_Release(pObject2);
2327
2328         IUnknown_Release(pObject);
2329     }
2330
2331     reg_unreg_wine_test_class(FALSE);
2332 }
2333
2334 static HRESULT WINAPI Test_SMI_QueryInterface(
2335     IStdMarshalInfo *iface,
2336     REFIID riid,
2337     LPVOID *ppvObj)
2338 {
2339     if (ppvObj == NULL) return E_POINTER;
2340
2341     if (IsEqualGUID(riid, &IID_IUnknown) ||
2342         IsEqualGUID(riid, &IID_IStdMarshalInfo))
2343     {
2344         *ppvObj = iface;
2345         IClassFactory_AddRef(iface);
2346         return S_OK;
2347     }
2348
2349     return E_NOINTERFACE;
2350 }
2351
2352 static ULONG WINAPI Test_SMI_AddRef(IStdMarshalInfo *iface)
2353 {
2354     LockModule();
2355     return 2; /* non-heap-based object */
2356 }
2357
2358 static ULONG WINAPI Test_SMI_Release(IStdMarshalInfo *iface)
2359 {
2360     UnlockModule();
2361     return 1; /* non-heap-based object */
2362 }
2363
2364 static HRESULT WINAPI Test_SMI_GetClassForHandler(
2365     IStdMarshalInfo *iface,
2366     DWORD dwDestContext,
2367     void *pvDestContext,
2368     CLSID *pClsid)
2369 {
2370     *pClsid = CLSID_WineTest;
2371     return S_OK;
2372 }
2373
2374 static const IStdMarshalInfoVtbl Test_SMI_Vtbl =
2375 {
2376     Test_SMI_QueryInterface,
2377     Test_SMI_AddRef,
2378     Test_SMI_Release,
2379     Test_SMI_GetClassForHandler
2380 };
2381
2382 static IStdMarshalInfo Test_SMI = {&Test_SMI_Vtbl};
2383
2384 static void test_handler_marshaling(void)
2385 {
2386     HRESULT hr;
2387     IStream *pStream = NULL;
2388     IUnknown *pProxy = NULL;
2389     IUnknown *pObject;
2390     DWORD tid;
2391     HANDLE thread;
2392     static const LARGE_INTEGER ullZero;
2393
2394     reg_unreg_wine_test_class(TRUE);
2395     cLocks = 0;
2396
2397     hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
2398     ok_ole_success(hr, "CreateStreamOnHGlobal");
2399     tid = start_host_object(pStream, &IID_IUnknown, (IUnknown*)&Test_SMI, MSHLFLAGS_NORMAL, &thread);
2400
2401     ok_more_than_one_lock();
2402
2403     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
2404     hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)&pProxy);
2405     ok_ole_success(hr, "CoUnmarshalInterface");
2406     IStream_Release(pStream);
2407
2408     if(hr == S_OK)
2409     {
2410         ok_more_than_one_lock();
2411
2412         hr = IUnknown_QueryInterface(pProxy, &IID_IWineTest, (void **)&pObject);
2413         ok(hr == E_NOINTERFACE, "IUnknown_QueryInterface with unknown IID should have returned E_NOINTERFACE instead of 0x%08x\n", hr);
2414
2415         /* it's a handler as it supports IOleObject */
2416         hr = IUnknown_QueryInterface(pProxy, &IID_IOleObject, (void **)&pObject);
2417         todo_wine
2418         ok_ole_success(hr, "IUnknown_QueryInterface(&IID_IOleObject)");
2419         if (SUCCEEDED(hr)) IUnknown_Release(pObject);
2420
2421         IUnknown_Release(pProxy);
2422
2423         ok_no_locks();
2424     }
2425
2426     end_host_object(tid, thread);
2427     reg_unreg_wine_test_class(FALSE);
2428
2429     /* FIXME: test IPersist interface has the same effect as IStdMarshalInfo */
2430 }
2431
2432
2433 static void test_client_security(void)
2434 {
2435     HRESULT hr;
2436     IStream *pStream = NULL;
2437     IClassFactory *pProxy = NULL;
2438     IUnknown *pProxy2 = NULL;
2439     IUnknown *pUnknown1 = NULL;
2440     IUnknown *pUnknown2 = NULL;
2441     IClientSecurity *pCliSec = NULL;
2442     IMarshal *pMarshal;
2443     DWORD tid;
2444     HANDLE thread;
2445     static const LARGE_INTEGER ullZero;
2446     DWORD dwAuthnSvc;
2447     DWORD dwAuthzSvc;
2448     OLECHAR *pServerPrincName;
2449     DWORD dwAuthnLevel;
2450     DWORD dwImpLevel;
2451     void *pAuthInfo;
2452     DWORD dwCapabilities;
2453     void *pv;
2454
2455     cLocks = 0;
2456
2457     hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
2458     ok_ole_success(hr, "CreateStreamOnHGlobal");
2459     tid = start_host_object(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &thread);
2460
2461     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
2462     hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&pProxy);
2463     ok_ole_success(hr, "CoUnmarshalInterface");
2464     IStream_Release(pStream);
2465
2466     hr = IUnknown_QueryInterface(pProxy, &IID_IUnknown, (LPVOID*)&pUnknown1);
2467     ok_ole_success(hr, "IUnknown_QueryInterface IID_IUnknown");
2468
2469     hr = IUnknown_QueryInterface(pProxy, &IID_IRemUnknown, (LPVOID*)&pProxy2);
2470     ok_ole_success(hr, "IUnknown_QueryInterface IID_IStream");
2471
2472     hr = IUnknown_QueryInterface(pProxy2, &IID_IUnknown, (LPVOID*)&pUnknown2);
2473     ok_ole_success(hr, "IUnknown_QueryInterface IID_IUnknown");
2474
2475     ok(pUnknown1 == pUnknown2, "both proxy's IUnknowns should be the same - %p, %p\n", pUnknown1, pUnknown2);
2476
2477     hr = IUnknown_QueryInterface(pProxy, &IID_IMarshal, (LPVOID*)&pMarshal);
2478     ok_ole_success(hr, "IUnknown_QueryInterface IID_IMarshal");
2479
2480     hr = IUnknown_QueryInterface(pProxy, &IID_IClientSecurity, (LPVOID*)&pCliSec);
2481     ok_ole_success(hr, "IUnknown_QueryInterface IID_IClientSecurity");
2482
2483     hr = IClientSecurity_QueryBlanket(pCliSec, (IUnknown *)pProxy, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
2484     todo_wine ok_ole_success(hr, "IClientSecurity_QueryBlanket (all NULLs)");
2485
2486     hr = IClientSecurity_QueryBlanket(pCliSec, (IUnknown *)pMarshal, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
2487     todo_wine ok(hr == E_NOINTERFACE, "IClientSecurity_QueryBlanket with local interface should have returned E_NOINTERFACE instead of 0x%08x\n", hr);
2488
2489     hr = IClientSecurity_QueryBlanket(pCliSec, (IUnknown *)pProxy, &dwAuthnSvc, &dwAuthzSvc, &pServerPrincName, &dwAuthnLevel, &dwImpLevel, &pAuthInfo, &dwCapabilities);
2490     todo_wine ok_ole_success(hr, "IClientSecurity_QueryBlanket");
2491
2492     hr = IClientSecurity_SetBlanket(pCliSec, (IUnknown *)pProxy, dwAuthnSvc, dwAuthzSvc, pServerPrincName, dwAuthnLevel, RPC_C_IMP_LEVEL_IMPERSONATE, pAuthInfo, dwCapabilities);
2493     todo_wine ok_ole_success(hr, "IClientSecurity_SetBlanket");
2494
2495     hr = IClassFactory_CreateInstance(pProxy, NULL, &IID_IWineTest, &pv);
2496     ok(hr == E_NOINTERFACE, "COM call should have succeeded instead of returning 0x%08x\n", hr);
2497
2498     hr = IClientSecurity_SetBlanket(pCliSec, (IUnknown *)pMarshal, dwAuthnSvc, dwAuthzSvc, pServerPrincName, dwAuthnLevel, dwImpLevel, pAuthInfo, dwCapabilities);
2499     todo_wine ok(hr == E_NOINTERFACE, "IClientSecurity_SetBlanket with local interface should have returned E_NOINTERFACE instead of 0x%08x\n", hr);
2500
2501     hr = IClientSecurity_SetBlanket(pCliSec, (IUnknown *)pProxy, 0xdeadbeef, dwAuthzSvc, pServerPrincName, dwAuthnLevel, dwImpLevel, pAuthInfo, dwCapabilities);
2502     todo_wine ok(hr == E_INVALIDARG, "IClientSecurity_SetBlanke with invalid dwAuthnSvc should have returned E_INVALIDARG instead of 0x%08x\n", hr);
2503
2504     CoTaskMemFree(pServerPrincName);
2505
2506     hr = IClientSecurity_QueryBlanket(pCliSec, pUnknown1, &dwAuthnSvc, &dwAuthzSvc, &pServerPrincName, &dwAuthnLevel, &dwImpLevel, &pAuthInfo, &dwCapabilities);
2507     todo_wine ok_ole_success(hr, "IClientSecurity_QueryBlanket(IUnknown)");
2508
2509     CoTaskMemFree(pServerPrincName);
2510
2511     IClassFactory_Release(pProxy);
2512     IUnknown_Release(pProxy2);
2513     IUnknown_Release(pUnknown1);
2514     IUnknown_Release(pUnknown2);
2515     IMarshal_Release(pMarshal);
2516     IClientSecurity_Release(pCliSec);
2517
2518     end_host_object(tid, thread);
2519 }
2520
2521 static HANDLE heventShutdown;
2522
2523 static void LockModuleOOP(void)
2524 {
2525     InterlockedIncrement(&cLocks); /* for test purposes only */
2526     CoAddRefServerProcess();
2527 }
2528
2529 static void UnlockModuleOOP(void)
2530 {
2531     InterlockedDecrement(&cLocks); /* for test purposes only */
2532     if (!CoReleaseServerProcess())
2533         SetEvent(heventShutdown);
2534 }
2535
2536 static HWND hwnd_app;
2537
2538 static HRESULT WINAPI TestOOP_IClassFactory_QueryInterface(
2539     LPCLASSFACTORY iface,
2540     REFIID riid,
2541     LPVOID *ppvObj)
2542 {
2543     if (ppvObj == NULL) return E_POINTER;
2544
2545     if (IsEqualGUID(riid, &IID_IUnknown) ||
2546         IsEqualGUID(riid, &IID_IClassFactory))
2547     {
2548         *ppvObj = iface;
2549         IClassFactory_AddRef(iface);
2550         return S_OK;
2551     }
2552
2553     return E_NOINTERFACE;
2554 }
2555
2556 static ULONG WINAPI TestOOP_IClassFactory_AddRef(LPCLASSFACTORY iface)
2557 {
2558     return 2; /* non-heap-based object */
2559 }
2560
2561 static ULONG WINAPI TestOOP_IClassFactory_Release(LPCLASSFACTORY iface)
2562 {
2563     return 1; /* non-heap-based object */
2564 }
2565
2566 static HRESULT WINAPI TestOOP_IClassFactory_CreateInstance(
2567     LPCLASSFACTORY iface,
2568     LPUNKNOWN pUnkOuter,
2569     REFIID riid,
2570     LPVOID *ppvObj)
2571 {
2572     if (IsEqualIID(riid, &IID_IClassFactory) || IsEqualIID(riid, &IID_IUnknown))
2573     {
2574         *ppvObj = iface;
2575         return S_OK;
2576     }
2577     return CLASS_E_CLASSNOTAVAILABLE;
2578 }
2579
2580 static HRESULT WINAPI TestOOP_IClassFactory_LockServer(
2581     LPCLASSFACTORY iface,
2582     BOOL fLock)
2583 {
2584     if (fLock)
2585         LockModuleOOP();
2586     else
2587         UnlockModuleOOP();
2588     return S_OK;
2589 }
2590
2591 static const IClassFactoryVtbl TestClassFactoryOOP_Vtbl =
2592 {
2593     TestOOP_IClassFactory_QueryInterface,
2594     TestOOP_IClassFactory_AddRef,
2595     TestOOP_IClassFactory_Release,
2596     TestOOP_IClassFactory_CreateInstance,
2597     TestOOP_IClassFactory_LockServer
2598 };
2599
2600 static IClassFactory TestOOP_ClassFactory = { &TestClassFactoryOOP_Vtbl };
2601
2602 static void test_register_local_server(void)
2603 {
2604     DWORD cookie;
2605     HRESULT hr;
2606     HANDLE ready_event;
2607     HANDLE quit_event;
2608     DWORD wait;
2609
2610     heventShutdown = CreateEvent(NULL, TRUE, FALSE, NULL);
2611
2612     hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&TestOOP_ClassFactory,
2613                                CLSCTX_LOCAL_SERVER, REGCLS_SINGLEUSE, &cookie);
2614     ok_ole_success(hr, CoRegisterClassObject);
2615
2616     ready_event = CreateEvent(NULL, FALSE, FALSE, "Wine COM Test Ready Event");
2617     SetEvent(ready_event);
2618
2619     quit_event = CreateEvent(NULL, FALSE, FALSE, "Wine COM Test Quit Event");
2620
2621     do
2622     {
2623         wait = MsgWaitForMultipleObjects(1, &quit_event, FALSE, 30000, QS_ALLINPUT);
2624         if (wait == WAIT_OBJECT_0+1)
2625         {
2626             MSG msg;
2627             BOOL ret = PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
2628             if (ret)
2629             {
2630                 trace("Message 0x%x\n", msg.message);
2631                 TranslateMessage(&msg);
2632                 DispatchMessage(&msg);
2633             }
2634         }
2635     }
2636     while (wait == WAIT_OBJECT_0+1);
2637
2638     ok( wait == WAIT_OBJECT_0, "quit event wait timed out\n" );
2639     hr = CoRevokeClassObject(cookie);
2640     ok_ole_success(hr, CoRevokeClassObject);
2641 }
2642
2643 static HANDLE create_target_process(const char *arg)
2644 {
2645     char **argv;
2646     char cmdline[MAX_PATH];
2647     BOOL ret;
2648     PROCESS_INFORMATION pi;
2649     STARTUPINFO si = { 0 };
2650     si.cb = sizeof(si);
2651
2652     pi.hThread = NULL;
2653     pi.hProcess = NULL;
2654     winetest_get_mainargs( &argv );
2655     sprintf(cmdline, "%s %s %s", argv[0], argv[1], arg);
2656     ret = CreateProcess(argv[0], cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
2657     ok(ret, "CreateProcess failed with error: %u\n", GetLastError());
2658     if (pi.hThread) CloseHandle(pi.hThread);
2659     return pi.hProcess;
2660 }
2661
2662 /* tests functions commonly used by out of process COM servers */
2663 static void test_local_server(void)
2664 {
2665     DWORD cookie;
2666     HRESULT hr;
2667     IClassFactory * cf;
2668     DWORD ret;
2669     HANDLE process;
2670     HANDLE quit_event;
2671     HANDLE ready_event;
2672
2673     heventShutdown = CreateEvent(NULL, TRUE, FALSE, NULL);
2674
2675     cLocks = 0;
2676
2677     /* Start the object suspended */
2678     hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&TestOOP_ClassFactory,
2679         CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE | REGCLS_SUSPENDED, &cookie);
2680     ok_ole_success(hr, CoRegisterClassObject);
2681
2682     /* ... and CoGetClassObject does not find it and fails when it looks for the
2683      * class in the registry */
2684     hr = CoGetClassObject(&CLSID_WineOOPTest, CLSCTX_INPROC_SERVER,
2685         NULL, &IID_IClassFactory, (LPVOID*)&cf);
2686     ok(hr == REGDB_E_CLASSNOTREG || /* NT */
2687        hr == S_OK /* Win9x */,
2688         "CoGetClassObject should have returned REGDB_E_CLASSNOTREG instead of 0x%08x\n", hr);
2689
2690     /* Resume the object suspended above ... */
2691     hr = CoResumeClassObjects();
2692     ok_ole_success(hr, CoResumeClassObjects);
2693
2694     /* ... and now it should succeed */
2695     hr = CoGetClassObject(&CLSID_WineOOPTest, CLSCTX_INPROC_SERVER,
2696         NULL, &IID_IClassFactory, (LPVOID*)&cf);
2697     ok_ole_success(hr, CoGetClassObject);
2698
2699     /* Now check the locking is working */
2700     /* NOTE: we are accessing the class directly, not through a proxy */
2701
2702     ok_no_locks();
2703
2704     hr = IClassFactory_LockServer(cf, TRUE);
2705     ok_ole_success(hr, IClassFactory_LockServer);
2706
2707     ok_more_than_one_lock();
2708     
2709     IClassFactory_LockServer(cf, FALSE);
2710     ok_ole_success(hr, IClassFactory_LockServer);
2711
2712     ok_no_locks();
2713
2714     IClassFactory_Release(cf);
2715
2716     /* wait for shutdown signal */
2717     ret = WaitForSingleObject(heventShutdown, 0);
2718     ok(ret != WAIT_TIMEOUT, "Server didn't shut down\n");
2719
2720     /* try to connect again after SCM has suspended registered class objects */
2721     hr = CoGetClassObject(&CLSID_WineOOPTest, CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER, NULL,
2722         &IID_IClassFactory, (LPVOID*)&cf);
2723     ok(hr == CO_E_SERVER_STOPPING || /* NT */
2724        hr == REGDB_E_CLASSNOTREG || /* win2k */
2725        hr == S_OK /* Win9x */,
2726         "CoGetClassObject should have returned CO_E_SERVER_STOPPING or REGDB_E_CLASSNOTREG instead of 0x%08x\n", hr);
2727
2728     hr = CoRevokeClassObject(cookie);
2729     ok_ole_success(hr, CoRevokeClassObject);
2730
2731     CloseHandle(heventShutdown);
2732
2733     process = create_target_process("-Embedding");
2734     ok(process != NULL, "couldn't start local server process, error was %d\n", GetLastError());
2735
2736     ready_event = CreateEvent(NULL, FALSE, FALSE, "Wine COM Test Ready Event");
2737     ok( !WaitForSingleObject(ready_event, 10000), "wait timed out\n" );
2738     CloseHandle(ready_event);
2739
2740     hr = CoCreateInstance(&CLSID_WineOOPTest, NULL, CLSCTX_LOCAL_SERVER, &IID_IClassFactory, (void **)&cf);
2741     ok_ole_success(hr, CoCreateInstance);
2742
2743     IClassFactory_Release(cf);
2744
2745     hr = CoCreateInstance(&CLSID_WineOOPTest, NULL, CLSCTX_LOCAL_SERVER, &IID_IClassFactory, (void **)&cf);
2746     ok(hr == REGDB_E_CLASSNOTREG, "Second CoCreateInstance on REGCLS_SINGLEUSE object should have failed\n");
2747
2748     quit_event = CreateEvent(NULL, FALSE, FALSE, "Wine COM Test Quit Event");
2749     SetEvent(quit_event);
2750
2751     winetest_wait_child_process( process );
2752     CloseHandle(quit_event);
2753     CloseHandle(process);
2754 }
2755
2756 struct git_params
2757 {
2758         DWORD cookie;
2759         IGlobalInterfaceTable *git;
2760 };
2761
2762 static DWORD CALLBACK get_global_interface_proc(LPVOID pv)
2763 {
2764         HRESULT hr;
2765         struct git_params *params = pv;
2766         IClassFactory *cf;
2767
2768         hr = IGlobalInterfaceTable_GetInterfaceFromGlobal(params->git, params->cookie, &IID_IClassFactory, (void **)&cf);
2769         ok(hr == CO_E_NOTINITIALIZED ||
2770                 broken(hr == E_UNEXPECTED) /* win2k */ ||
2771                 broken(hr == S_OK) /* NT 4 */,
2772                 "IGlobalInterfaceTable_GetInterfaceFromGlobal should have failed with error CO_E_NOTINITIALIZED or E_UNEXPECTED instead of 0x%08x\n",
2773                 hr);
2774         if (hr == S_OK)
2775                 IClassFactory_Release(cf);
2776
2777         CoInitialize(NULL);
2778
2779         hr = IGlobalInterfaceTable_GetInterfaceFromGlobal(params->git, params->cookie, &IID_IClassFactory, (void **)&cf);
2780         ok_ole_success(hr, IGlobalInterfaceTable_GetInterfaceFromGlobal);
2781
2782         IClassFactory_Release(cf);
2783
2784         CoUninitialize();
2785
2786         return hr;
2787 }
2788
2789 static void test_globalinterfacetable(void)
2790 {
2791         HRESULT hr;
2792         IGlobalInterfaceTable *git;
2793         DWORD cookie;
2794         HANDLE thread;
2795         DWORD tid;
2796         struct git_params params;
2797         DWORD ret;
2798         IUnknown *object;
2799
2800         trace("test_globalinterfacetable\n");
2801         cLocks = 0;
2802
2803         hr = CoCreateInstance(&CLSID_StdGlobalInterfaceTable, NULL, CLSCTX_INPROC_SERVER, &IID_IGlobalInterfaceTable, (void **)&git);
2804         ok_ole_success(hr, CoCreateInstance);
2805
2806         hr = IGlobalInterfaceTable_RegisterInterfaceInGlobal(git, (IUnknown *)&Test_ClassFactory, &IID_IClassFactory, &cookie);
2807         ok_ole_success(hr, IGlobalInterfaceTable_RegisterInterfaceInGlobal);
2808
2809         ok_more_than_one_lock();
2810
2811         params.cookie = cookie;
2812         params.git = git;
2813         /* note: params is on stack so we MUST wait for get_global_interface_proc
2814          * to exit before we can return */
2815         thread = CreateThread(NULL, 0, get_global_interface_proc, &params, 0, &tid);
2816
2817         ret = MsgWaitForMultipleObjects(1, &thread, FALSE, 10000, QS_ALLINPUT);
2818         while (ret == WAIT_OBJECT_0 + 1)
2819         {
2820                 MSG msg;
2821                 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
2822                         DispatchMessage(&msg);
2823                 ret = MsgWaitForMultipleObjects(1, &thread, FALSE, 10000, QS_ALLINPUT);
2824         }
2825
2826         CloseHandle(thread);
2827
2828         /* test getting interface from global with different iid */
2829         hr = IGlobalInterfaceTable_GetInterfaceFromGlobal(git, cookie, &IID_IUnknown, (void **)&object);
2830         ok_ole_success(hr, IGlobalInterfaceTable_GetInterfaceFromGlobal);
2831         IUnknown_Release(object);
2832
2833         /* test getting interface from global with same iid */
2834         hr = IGlobalInterfaceTable_GetInterfaceFromGlobal(git, cookie, &IID_IClassFactory, (void **)&object);
2835         ok_ole_success(hr, IGlobalInterfaceTable_GetInterfaceFromGlobal);
2836         IUnknown_Release(object);
2837
2838         hr = IGlobalInterfaceTable_RevokeInterfaceFromGlobal(git, cookie);
2839         ok_ole_success(hr, IGlobalInterfaceTable_RevokeInterfaceFromGlobal);
2840
2841         ok_no_locks();
2842
2843         IGlobalInterfaceTable_Release(git);
2844 }
2845
2846 static const char *debugstr_iid(REFIID riid)
2847 {
2848     static char name[256];
2849     HKEY hkeyInterface;
2850     WCHAR bufferW[39];
2851     char buffer[39];
2852     LONG name_size = sizeof(name);
2853     StringFromGUID2(riid, bufferW, sizeof(bufferW)/sizeof(bufferW[0]));
2854     WideCharToMultiByte(CP_ACP, 0, bufferW, sizeof(bufferW)/sizeof(bufferW[0]), buffer, sizeof(buffer), NULL, NULL);
2855     if (RegOpenKeyEx(HKEY_CLASSES_ROOT, "Interface", 0, KEY_QUERY_VALUE, &hkeyInterface) != ERROR_SUCCESS)
2856     {
2857         memcpy(name, buffer, sizeof(buffer));
2858         goto done;
2859     }
2860     if (RegQueryValue(hkeyInterface, buffer, name, &name_size) != ERROR_SUCCESS)
2861     {
2862         memcpy(name, buffer, sizeof(buffer));
2863         goto done;
2864     }
2865     RegCloseKey(hkeyInterface);
2866 done:
2867     return name;
2868 }
2869
2870 static HRESULT WINAPI TestChannelHook_QueryInterface(IChannelHook *iface, REFIID riid, void **ppv)
2871 {
2872     if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IChannelHook))
2873     {
2874         *ppv = iface;
2875         IUnknown_AddRef(iface);
2876         return S_OK;
2877     }
2878
2879     *ppv = NULL;
2880     return E_NOINTERFACE;
2881 }
2882
2883 static ULONG WINAPI TestChannelHook_AddRef(IChannelHook *iface)
2884 {
2885     return 2;
2886 }
2887
2888 static ULONG WINAPI TestChannelHook_Release(IChannelHook *iface)
2889 {
2890     return 1;
2891 }
2892
2893 static void WINAPI TestChannelHook_ClientGetSize(
2894     IChannelHook *iface,
2895     REFGUID uExtent,
2896     REFIID  riid,
2897     ULONG  *pDataSize )
2898 {
2899     SChannelHookCallInfo *info = (SChannelHookCallInfo *)riid;
2900     trace("TestChannelHook_ClientGetBuffer\n");
2901     trace("\t%s method %d\n", debugstr_iid(riid), info->iMethod);
2902     trace("\tcid: %s\n", debugstr_iid(&info->uCausality));
2903     ok(info->cbSize == sizeof(*info), "info->cbSize was %d instead of %d\n", info->cbSize, (int)sizeof(*info));
2904     ok(info->dwServerPid == GetCurrentProcessId(), "info->dwServerPid was 0x%x instead of 0x%x\n", info->dwServerPid, GetCurrentProcessId());
2905     ok(!info->pObject, "info->pObject should be NULL\n");
2906     ok(IsEqualGUID(uExtent, &EXTENTID_WineTest), "uExtent wasn't correct\n");
2907
2908     *pDataSize = 1;
2909 }
2910
2911 static void WINAPI TestChannelHook_ClientFillBuffer(
2912     IChannelHook *iface,
2913     REFGUID uExtent,
2914     REFIID  riid,
2915     ULONG  *pDataSize,
2916     void   *pDataBuffer )
2917 {
2918     SChannelHookCallInfo *info = (SChannelHookCallInfo *)riid;
2919     trace("TestChannelHook_ClientFillBuffer\n");
2920     ok(info->cbSize == sizeof(*info), "info->cbSize was %d instead of %d\n", info->cbSize, (int)sizeof(*info));
2921     ok(info->dwServerPid == GetCurrentProcessId(), "info->dwServerPid was 0x%x instead of 0x%x\n", info->dwServerPid, GetCurrentProcessId());
2922     ok(!info->pObject, "info->pObject should be NULL\n");
2923     ok(IsEqualGUID(uExtent, &EXTENTID_WineTest), "uExtent wasn't correct\n");
2924
2925     *(unsigned char *)pDataBuffer = 0xcc;
2926     *pDataSize = 1;
2927 }
2928
2929 static void WINAPI TestChannelHook_ClientNotify(
2930     IChannelHook *iface,
2931     REFGUID uExtent,
2932     REFIID  riid,
2933     ULONG   cbDataSize,
2934     void   *pDataBuffer,
2935     DWORD   lDataRep,
2936     HRESULT hrFault )
2937 {
2938     SChannelHookCallInfo *info = (SChannelHookCallInfo *)riid;
2939     trace("TestChannelHook_ClientNotify hrFault = 0x%08x\n", hrFault);
2940     ok(info->cbSize == sizeof(*info), "info->cbSize was %d instead of %d\n", info->cbSize, (int)sizeof(*info));
2941     ok(info->dwServerPid == GetCurrentProcessId(), "info->dwServerPid was 0x%x instead of 0x%x\n", info->dwServerPid, GetCurrentProcessId());
2942     todo_wine {
2943     ok(info->pObject != NULL, "info->pObject shouldn't be NULL\n");
2944     }
2945     ok(IsEqualGUID(uExtent, &EXTENTID_WineTest), "uExtent wasn't correct\n");
2946 }
2947
2948 static void WINAPI TestChannelHook_ServerNotify(
2949     IChannelHook *iface,
2950     REFGUID uExtent,
2951     REFIID  riid,
2952     ULONG   cbDataSize,
2953     void   *pDataBuffer,
2954     DWORD   lDataRep )
2955 {
2956     SChannelHookCallInfo *info = (SChannelHookCallInfo *)riid;
2957     trace("TestChannelHook_ServerNotify\n");
2958     ok(info->cbSize == sizeof(*info), "info->cbSize was %d instead of %d\n", info->cbSize, (int)sizeof(*info));
2959     ok(info->dwServerPid == GetCurrentProcessId(), "info->dwServerPid was 0x%x instead of 0x%x\n", info->dwServerPid, GetCurrentProcessId());
2960     ok(info->pObject != NULL, "info->pObject shouldn't be NULL\n");
2961     ok(cbDataSize == 1, "cbDataSize should have been 1 instead of %d\n", cbDataSize);
2962     ok(*(unsigned char *)pDataBuffer == 0xcc, "pDataBuffer should have contained 0xcc instead of 0x%x\n", *(unsigned char *)pDataBuffer);
2963     ok(IsEqualGUID(uExtent, &EXTENTID_WineTest), "uExtent wasn't correct\n");
2964 }
2965
2966 static void WINAPI TestChannelHook_ServerGetSize(
2967     IChannelHook *iface,
2968     REFGUID uExtent,
2969     REFIID  riid,
2970     HRESULT hrFault,
2971     ULONG  *pDataSize )
2972 {
2973     SChannelHookCallInfo *info = (SChannelHookCallInfo *)riid;
2974     trace("TestChannelHook_ServerGetSize\n");
2975     trace("\t%s method %d\n", debugstr_iid(riid), info->iMethod);
2976     ok(info->cbSize == sizeof(*info), "info->cbSize was %d instead of %d\n", info->cbSize, (int)sizeof(*info));
2977     ok(info->dwServerPid == GetCurrentProcessId(), "info->dwServerPid was 0x%x instead of 0x%x\n", info->dwServerPid, GetCurrentProcessId());
2978     ok(info->pObject != NULL, "info->pObject shouldn't be NULL\n");
2979     ok(IsEqualGUID(uExtent, &EXTENTID_WineTest), "uExtent wasn't correct\n");
2980     if (hrFault != S_OK)
2981         trace("\thrFault = 0x%08x\n", hrFault);
2982
2983     *pDataSize = 0;
2984 }
2985
2986 static void WINAPI TestChannelHook_ServerFillBuffer(
2987     IChannelHook *iface,
2988     REFGUID uExtent,
2989     REFIID  riid,
2990     ULONG  *pDataSize,
2991     void   *pDataBuffer,
2992     HRESULT hrFault )
2993 {
2994     trace("TestChannelHook_ServerFillBuffer\n");
2995     ok(0, "TestChannelHook_ServerFillBuffer shouldn't be called\n");
2996 }
2997
2998 static const IChannelHookVtbl TestChannelHookVtbl =
2999 {
3000     TestChannelHook_QueryInterface,
3001     TestChannelHook_AddRef,
3002     TestChannelHook_Release,
3003     TestChannelHook_ClientGetSize,
3004     TestChannelHook_ClientFillBuffer,
3005     TestChannelHook_ClientNotify,
3006     TestChannelHook_ServerNotify,
3007     TestChannelHook_ServerGetSize,
3008     TestChannelHook_ServerFillBuffer,
3009 };
3010
3011 static IChannelHook TestChannelHook = { &TestChannelHookVtbl };
3012
3013 static void test_channel_hook(void)
3014 {
3015     IStream *pStream = NULL;
3016     IClassFactory *cf = NULL;
3017     DWORD tid;
3018     IUnknown *proxy = NULL;
3019     HANDLE thread;
3020     HRESULT hr;
3021
3022     hr = CoRegisterChannelHook(&EXTENTID_WineTest, &TestChannelHook);
3023     ok_ole_success(hr, CoRegisterChannelHook);
3024
3025     hr = CoRegisterMessageFilter(&MessageFilter, NULL);
3026     ok_ole_success(hr, CoRegisterMessageFilter);
3027
3028     cLocks = 0;
3029
3030     hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
3031     ok_ole_success(hr, CreateStreamOnHGlobal);
3032     tid = start_host_object2(pStream, &IID_IClassFactory, (IUnknown*)&Test_ClassFactory, MSHLFLAGS_NORMAL, &MessageFilter, &thread);
3033
3034     ok_more_than_one_lock();
3035
3036     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
3037     hr = CoUnmarshalInterface(pStream, &IID_IClassFactory, (void **)&cf);
3038     ok_ole_success(hr, CoUnmarshalInterface);
3039     IStream_Release(pStream);
3040
3041     ok_more_than_one_lock();
3042
3043     hr = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (LPVOID*)&proxy);
3044     ok_ole_success(hr, IClassFactory_CreateInstance);
3045     IUnknown_Release(proxy);
3046
3047     IClassFactory_Release(cf);
3048
3049     ok_no_locks();
3050
3051     end_host_object(tid, thread);
3052
3053     hr = CoRegisterMessageFilter(NULL, NULL);
3054     ok_ole_success(hr, CoRegisterMessageFilter);
3055 }
3056
3057 START_TEST(marshal)
3058 {
3059     HMODULE hOle32 = GetModuleHandle("ole32");
3060     int argc;
3061     char **argv;
3062
3063     if (!GetProcAddress(hOle32, "CoRegisterSurrogateEx")) {
3064         win_skip("skipping test on win9x\n");
3065         return;
3066     }
3067
3068     pCoInitializeEx = (void*)GetProcAddress(hOle32, "CoInitializeEx");
3069
3070     argc = winetest_get_mainargs( &argv );
3071     if (argc > 2 && (!strcmp(argv[2], "-Embedding")))
3072     {
3073         pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
3074         test_register_local_server();
3075         CoUninitialize();
3076
3077         return;
3078     }
3079
3080     register_test_window();
3081
3082     test_cocreateinstance_proxy();
3083
3084     pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
3085
3086     /* FIXME: test CoCreateInstanceEx */
3087
3088     /* lifecycle management and marshaling tests */
3089     test_no_marshaler();
3090     test_normal_marshal_and_release();
3091     test_normal_marshal_and_unmarshal();
3092     test_marshal_and_unmarshal_invalid();
3093     test_same_apartment_unmarshal_failure();
3094     test_interthread_marshal_and_unmarshal();
3095     test_proxy_marshal_and_unmarshal();
3096     test_proxy_marshal_and_unmarshal2();
3097     test_proxy_marshal_and_unmarshal_weak();
3098     test_proxy_marshal_and_unmarshal_strong();
3099     test_marshal_stub_apartment_shutdown();
3100     test_marshal_proxy_apartment_shutdown();
3101     test_marshal_proxy_mta_apartment_shutdown();
3102     test_no_couninitialize_server();
3103     test_no_couninitialize_client();
3104     test_tableweak_marshal_and_unmarshal_twice();
3105     test_tableweak_marshal_releasedata1();
3106     test_tableweak_marshal_releasedata2();
3107     test_tableweak_and_normal_marshal_and_unmarshal();
3108     test_tablestrong_marshal_and_unmarshal_twice();
3109     test_lock_object_external();
3110     test_disconnect_stub();
3111     test_normal_marshal_and_unmarshal_twice();
3112     test_hresult_marshaling();
3113     test_proxy_used_in_wrong_thread();
3114     test_message_filter();
3115     test_bad_marshal_stream();
3116     test_proxy_interfaces();
3117     test_stubbuffer(&IID_IClassFactory);
3118     test_proxybuffer(&IID_IClassFactory);
3119     test_message_reentrancy();
3120     test_call_from_message();
3121     test_WM_QUIT_handling();
3122     test_freethreadedmarshaler();
3123     test_inproc_handler();
3124     test_handler_marshaling();
3125     test_client_security();
3126
3127     test_local_server();
3128
3129     test_globalinterfacetable();
3130
3131     /* must be last test as channel hooks can't be unregistered */
3132     test_channel_hook();
3133
3134     CoUninitialize();
3135 }