Initialize a bunch of dc values to their default before enumerating an
[wine] / dlls / ole32 / stubmanager.c
1 /*
2  * A stub manager is an object that controls interface stubs. It is
3  * identified by an OID (object identifier) and acts as the network
4  * identity of the object. There can be many stub managers in a
5  * process or apartment.
6  *
7  * Copyright 2002 Marcus Meissner
8  * Copyright 2004 Mike Hearn for CodeWeavers
9  * Copyright 2004 Robert Shearman (for CodeWeavers)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with this library; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24  */
25
26 #define COBJMACROS
27 #define NONAMELESSUNION
28 #define NONAMELESSSTRUCT
29
30 #include <assert.h>
31 #include <stdarg.h>
32 #include <limits.h>
33
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winuser.h"
37 #include "objbase.h"
38 #include "ole2.h"
39 #include "ole2ver.h"
40 #include "rpc.h"
41 #include "wine/debug.h"
42 #include "compobj_private.h"
43
44 WINE_DEFAULT_DEBUG_CHANNEL(ole);
45
46 static void stub_manager_delete_ifstub(struct stub_manager *m, struct ifstub *ifstub);
47 static struct ifstub *stub_manager_ipid_to_ifstub(struct stub_manager *m, const IPID *ipid);
48
49 /* creates a new stub manager and adds it into the apartment. caller must
50  * release stub manager when it is no longer required. the apartment and
51  * external refs together take one implicit ref */
52 struct stub_manager *new_stub_manager(APARTMENT *apt, IUnknown *object)
53 {
54     struct stub_manager *sm;
55
56     assert( apt );
57     
58     sm = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct stub_manager));
59     if (!sm) return NULL;
60
61     list_init(&sm->ifstubs);
62
63     InitializeCriticalSection(&sm->lock);
64     DEBUG_SET_CRITSEC_NAME(&sm->lock, "stub_manager");
65
66     IUnknown_AddRef(object);
67     sm->object = object;
68     sm->apt    = apt;
69
70     /* start off with 2 references because the stub is in the apartment
71      * and the caller will also hold a reference */
72     sm->refs   = 2;
73
74     /* yes, that's right, this starts at zero. that's zero EXTERNAL
75      * refs, ie nobody has unmarshalled anything yet. we can't have
76      * negative refs because the stub manager cannot be explicitly
77      * killed, it has to die by somebody unmarshalling then releasing
78      * the marshalled ifptr.
79      */
80     sm->extrefs = 0;
81
82     EnterCriticalSection(&apt->cs);
83     sm->oid = apt->oidc++;
84     list_add_head(&apt->stubmgrs, &sm->entry);
85     LeaveCriticalSection(&apt->cs);
86
87     TRACE("Created new stub manager (oid=%s) at %p for object with IUnknown %p\n", wine_dbgstr_longlong(sm->oid), sm, object);
88     
89     return sm;
90 }
91
92 /* caller must remove stub manager from apartment prior to calling this function */
93 static void stub_manager_delete(struct stub_manager *m)
94 {
95     struct list *cursor;
96
97     TRACE("destroying %p (oid=%s)\n", m, wine_dbgstr_longlong(m->oid));
98
99     /* release every ifstub */
100     while ((cursor = list_head(&m->ifstubs)))
101     {
102         struct ifstub *ifstub = LIST_ENTRY(cursor, struct ifstub, entry);
103         stub_manager_delete_ifstub(m, ifstub);
104     }
105
106     IUnknown_Release(m->object);
107
108     DEBUG_CLEAR_CRITSEC_NAME(&m->lock);
109     DeleteCriticalSection(&m->lock);
110
111     HeapFree(GetProcessHeap(), 0, m);
112 }
113
114 /* gets the stub manager associated with an object - caller must have
115  * a reference to the apartment while a reference to the stub manager is held.
116  * it must also call release on the stub manager when it is no longer needed */
117 struct stub_manager *get_stub_manager_from_object(APARTMENT *apt, void *object)
118 {
119     struct stub_manager *result = NULL;
120     struct list         *cursor;
121
122     EnterCriticalSection(&apt->cs);
123     LIST_FOR_EACH( cursor, &apt->stubmgrs )
124     {
125         struct stub_manager *m = LIST_ENTRY( cursor, struct stub_manager, entry );
126
127         if (m->object == object)
128         {
129             result = m;
130             stub_manager_int_addref(result);
131             break;
132         }
133     }
134     LeaveCriticalSection(&apt->cs);
135
136     if (result)
137         TRACE("found %p for object %p\n", result, object);
138     else
139         TRACE("not found for object %p\n", object);
140
141     return result;    
142 }
143
144 /* removes the apartment reference to an object, destroying it when no other
145  * threads have a reference to it */
146 void apartment_disconnectobject(struct apartment *apt, void *object)
147 {
148     int found = FALSE;
149     struct stub_manager *stubmgr;
150
151     EnterCriticalSection(&apt->cs);
152     LIST_FOR_EACH_ENTRY( stubmgr, &apt->stubmgrs, struct stub_manager, entry )
153     {
154         if (stubmgr->object == object)
155         {
156             found = TRUE;
157             stub_manager_int_release(stubmgr);
158             break;
159         }
160     }
161     LeaveCriticalSection(&apt->cs);
162
163     if (found)
164         TRACE("disconnect object %p\n", object);
165     else
166         WARN("couldn't find object %p\n", object);
167 }
168
169 /* gets the stub manager associated with an object id - caller must have
170  * a reference to the apartment while a reference to the stub manager is held.
171  * it must also call release on the stub manager when it is no longer needed */
172 struct stub_manager *get_stub_manager(APARTMENT *apt, OID oid)
173 {
174     struct stub_manager *result = NULL;
175     struct list         *cursor;
176
177     EnterCriticalSection(&apt->cs);
178     LIST_FOR_EACH( cursor, &apt->stubmgrs )
179     {
180         struct stub_manager *m = LIST_ENTRY( cursor, struct stub_manager, entry );
181
182         if (m->oid == oid)
183         {
184             result = m;
185             stub_manager_int_addref(result);
186             break;
187         }
188     }
189     LeaveCriticalSection(&apt->cs);
190
191     if (result)
192         TRACE("found %p for oid %s\n", result, wine_dbgstr_longlong(oid));
193     else
194         TRACE("not found for oid %s\n", wine_dbgstr_longlong(oid));
195
196     return result;
197 }
198
199 /* increments the internal refcount */
200 ULONG stub_manager_int_addref(struct stub_manager *This)
201 {
202     ULONG refs;
203
204     EnterCriticalSection(&This->apt->cs);
205     refs = ++This->refs;
206     LeaveCriticalSection(&This->apt->cs);
207
208     TRACE("before %ld\n", refs - 1);
209
210     return refs;
211 }
212
213 /* decrements the internal refcount */
214 ULONG stub_manager_int_release(struct stub_manager *This)
215 {
216     ULONG refs;
217     APARTMENT *apt = This->apt;
218
219     EnterCriticalSection(&apt->cs);
220     refs = --This->refs;
221
222     TRACE("after %ld\n", refs);
223
224     /* remove from apartment so no other thread can access it... */
225     if (!refs)
226         list_remove(&This->entry);
227
228     LeaveCriticalSection(&apt->cs);
229
230     /* ... so now we can delete it without being inside the apartment critsec */
231     if (!refs)
232         stub_manager_delete(This);
233
234     return refs;
235 }
236
237 /* add some external references (ie from a client that unmarshaled an ifptr) */
238 ULONG stub_manager_ext_addref(struct stub_manager *m, ULONG refs)
239 {
240     ULONG rc;
241
242     EnterCriticalSection(&m->lock);
243     
244     /* make sure we don't overflow extrefs */
245     refs = min(refs, (ULONG_MAX-1 - m->extrefs));
246     rc = (m->extrefs += refs);
247
248     LeaveCriticalSection(&m->lock);
249     
250     TRACE("added %lu refs to %p (oid %s), rc is now %lu\n", refs, m, wine_dbgstr_longlong(m->oid), rc);
251
252     return rc;
253 }
254
255 /* remove some external references */
256 ULONG stub_manager_ext_release(struct stub_manager *m, ULONG refs)
257 {
258     ULONG rc;
259
260     EnterCriticalSection(&m->lock);
261
262     /* make sure we don't underflow extrefs */
263     refs = min(refs, m->extrefs);
264     rc = (m->extrefs -= refs);
265
266     LeaveCriticalSection(&m->lock);
267     
268     TRACE("removed %lu refs from %p (oid %s), rc is now %lu\n", refs, m, wine_dbgstr_longlong(m->oid), rc);
269
270     if (rc == 0)
271         stub_manager_int_release(m);
272
273     return rc;
274 }
275
276 static struct ifstub *stub_manager_ipid_to_ifstub(struct stub_manager *m, const IPID *ipid)
277 {
278     struct list    *cursor;
279     struct ifstub  *result = NULL;
280     
281     EnterCriticalSection(&m->lock);
282     LIST_FOR_EACH( cursor, &m->ifstubs )
283     {
284         struct ifstub *ifstub = LIST_ENTRY( cursor, struct ifstub, entry );
285
286         if (IsEqualGUID(ipid, &ifstub->ipid))
287         {
288             result = ifstub;
289             break;
290         }
291     }
292     LeaveCriticalSection(&m->lock);
293
294     return result;
295 }
296
297 struct ifstub *stub_manager_find_ifstub(struct stub_manager *m, REFIID iid, MSHLFLAGS flags)
298 {
299     struct ifstub  *result = NULL;
300     struct ifstub  *ifstub;
301
302     EnterCriticalSection(&m->lock);
303     LIST_FOR_EACH_ENTRY( ifstub, &m->ifstubs, struct ifstub, entry )
304     {
305         if (IsEqualIID(iid, &ifstub->iid) && (ifstub->flags == flags))
306         {
307             result = ifstub;
308             break;
309         }
310     }
311     LeaveCriticalSection(&m->lock);
312
313     return result;
314 }
315
316 /* gets the stub manager associated with an ipid - caller must have
317  * a reference to the apartment while a reference to the stub manager is held.
318  * it must also call release on the stub manager when it is no longer needed */
319 static struct stub_manager *get_stub_manager_from_ipid(APARTMENT *apt, const IPID *ipid)
320 {
321     struct stub_manager *result = NULL;
322     struct list         *cursor;
323
324     EnterCriticalSection(&apt->cs);
325     LIST_FOR_EACH( cursor, &apt->stubmgrs )
326     {
327         struct stub_manager *m = LIST_ENTRY( cursor, struct stub_manager, entry );
328
329         if (stub_manager_ipid_to_ifstub(m, ipid))
330         {
331             result = m;
332             stub_manager_int_addref(result);
333             break;
334         }
335     }
336     LeaveCriticalSection(&apt->cs);
337
338     if (result)
339         TRACE("found %p for ipid %s\n", result, debugstr_guid(ipid));
340     else
341         ERR("not found for ipid %s\n", debugstr_guid(ipid));
342
343     return result;
344 }
345
346 HRESULT ipid_to_stub_manager(const IPID *ipid, APARTMENT **stub_apt, struct stub_manager **stubmgr_ret)
347 {
348     /* FIXME: hack for IRemUnknown */
349     if (ipid->Data2 == 0xffff)
350         *stub_apt = apartment_findfromoxid(*(OXID *)ipid->Data4, TRUE);
351     else
352         *stub_apt = apartment_findfromtid(ipid->Data2);
353     if (!*stub_apt)
354     {
355         TRACE("Couldn't find apartment corresponding to TID 0x%04x\n", ipid->Data2);
356         return RPC_E_INVALID_OBJECT;
357     }
358     *stubmgr_ret = get_stub_manager_from_ipid(*stub_apt, ipid);
359     if (!*stubmgr_ret)
360     {
361         apartment_release(*stub_apt);
362         *stub_apt = NULL;
363         return RPC_E_INVALID_OBJECT;
364     }
365     return S_OK;
366 }
367
368 /* gets the apartment and IRpcStubBuffer from an object. the caller must
369  * release the references to both objects */
370 IRpcStubBuffer *ipid_to_apt_and_stubbuffer(const IPID *ipid, APARTMENT **stub_apt)
371 {
372     IRpcStubBuffer *ret = NULL;
373     struct stub_manager *stubmgr;
374     struct ifstub *ifstub;
375     HRESULT hr;
376
377     *stub_apt = NULL;
378
379     hr = ipid_to_stub_manager(ipid, stub_apt, &stubmgr);
380     if (hr != S_OK) return NULL;
381
382     ifstub = stub_manager_ipid_to_ifstub(stubmgr, ipid);
383     if (ifstub)
384         ret = ifstub->stubbuffer;
385
386     if (ret) IRpcStubBuffer_AddRef(ret);
387
388     stub_manager_int_release(stubmgr);
389
390     return ret;
391 }
392
393 /* generates an ipid in the following format (similar to native version):
394  * Data1 = apartment-local ipid counter
395  * Data2 = apartment creator thread ID
396  * Data3 = process ID
397  * Data4 = random value
398  */
399 static inline HRESULT generate_ipid(struct stub_manager *m, IPID *ipid)
400 {
401     HRESULT hr;
402     hr = UuidCreate(ipid);
403     if (FAILED(hr))
404     {
405         ERR("couldn't create IPID for stub manager %p\n", m);
406         UuidCreateNil(ipid);
407         return hr;
408     }
409
410     ipid->Data1 = InterlockedIncrement(&m->apt->ipidc);
411     ipid->Data2 = (USHORT)m->apt->tid;
412     ipid->Data3 = (USHORT)GetCurrentProcessId();
413     return S_OK;
414 }
415
416 /* registers a new interface stub COM object with the stub manager and returns registration record */
417 struct ifstub *stub_manager_new_ifstub(struct stub_manager *m, IRpcStubBuffer *sb, IUnknown *iptr, REFIID iid, MSHLFLAGS flags)
418 {
419     struct ifstub *stub;
420
421     TRACE("oid=%s, stubbuffer=%p, iptr=%p, iid=%s\n",
422           wine_dbgstr_longlong(m->oid), sb, iptr, debugstr_guid(iid));
423
424     stub = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct ifstub));
425     if (!stub) return NULL;
426
427     stub->stubbuffer = sb;
428     if (sb) IRpcStubBuffer_AddRef(sb);
429
430     IUnknown_AddRef(iptr);
431     stub->iface = iptr;
432     stub->flags = flags;
433     stub->iid = *iid;
434
435     /* 
436      * FIXME: this is a hack for marshalling IRemUnknown. In real
437      * DCOM, the IPID of the IRemUnknown interface is generated like
438      * any other and passed to the OXID resolver which then returns it
439      * when queried. We don't have an OXID resolver yet so instead we
440      * use a magic IPID reserved for IRemUnknown.
441      */
442     if (IsEqualIID(iid, &IID_IRemUnknown))
443     {
444         stub->ipid.Data1 = 0xffffffff;
445         stub->ipid.Data2 = 0xffff;
446         stub->ipid.Data3 = 0xffff;
447         assert(sizeof(stub->ipid.Data4) == sizeof(m->apt->oxid));
448         memcpy(&stub->ipid.Data4, &m->apt->oxid, sizeof(OXID));
449     }
450     else
451         generate_ipid(m, &stub->ipid);
452
453     EnterCriticalSection(&m->lock);
454     list_add_head(&m->ifstubs, &stub->entry);
455     /* every normal marshal is counted so we don't allow more than we should */
456     if (flags & MSHLFLAGS_NORMAL) m->norm_refs++;
457     LeaveCriticalSection(&m->lock);
458
459     TRACE("ifstub %p created with ipid %s\n", stub, debugstr_guid(&stub->ipid));
460
461     return stub;
462 }
463
464 static void stub_manager_delete_ifstub(struct stub_manager *m, struct ifstub *ifstub)
465 {
466     TRACE("m=%p, m->oid=%s, ipid=%s\n", m, wine_dbgstr_longlong(m->oid), debugstr_guid(&ifstub->ipid));
467
468     list_remove(&ifstub->entry);
469
470     RPC_UnregisterInterface(&ifstub->iid);
471         
472     if (ifstub->stubbuffer) IUnknown_Release(ifstub->stubbuffer);
473     IUnknown_Release(ifstub->iface);
474
475     HeapFree(GetProcessHeap(), 0, ifstub);
476 }
477
478 /* returns TRUE if it is possible to unmarshal, FALSE otherwise. */
479 BOOL stub_manager_notify_unmarshal(struct stub_manager *m, const IPID *ipid)
480 {
481     BOOL ret = TRUE;
482     struct ifstub *ifstub;
483
484     if (!(ifstub = stub_manager_ipid_to_ifstub(m, ipid)))
485     {
486         ERR("attempted unmarshal of unknown IPID %s\n", debugstr_guid(ipid));
487         return FALSE;
488     }
489
490     EnterCriticalSection(&m->lock);
491
492     /* track normal marshals so we can enforce rules whilst in-process */
493     if (ifstub->flags & MSHLFLAGS_NORMAL)
494     {
495         if (m->norm_refs)
496             m->norm_refs--;
497         else
498         {
499             ERR("attempted invalid normal unmarshal, norm_refs is zero\n");
500             ret = FALSE;
501         }
502     }
503
504     LeaveCriticalSection(&m->lock);
505
506     return ret;
507 }
508
509 /* handles refcounting for CoReleaseMarshalData */
510 void stub_manager_release_marshal_data(struct stub_manager *m, ULONG refs, const IPID *ipid)
511 {
512     struct ifstub *ifstub;
513  
514     if (!(ifstub = stub_manager_ipid_to_ifstub(m, ipid)))
515         return;
516  
517     if (ifstub->flags & MSHLFLAGS_TABLEWEAK)
518         refs = 0;
519     else
520         refs = 1;
521
522     stub_manager_ext_release(m, refs);
523 }
524
525 /* is an ifstub table marshaled? */
526 BOOL stub_manager_is_table_marshaled(struct stub_manager *m, const IPID *ipid)
527 {
528     struct ifstub *ifstub = stub_manager_ipid_to_ifstub(m, ipid);
529  
530     assert( ifstub );
531     
532     return ifstub->flags & (MSHLFLAGS_TABLESTRONG | MSHLFLAGS_TABLEWEAK);
533 }
534
535
536 /*****************************************************************************
537  *
538  * IRemUnknown implementation
539  *
540  *
541  * Note: this object is not related to the lifetime of a stub_manager, but it
542  * interacts with stub managers.
543  */
544
545 const IID IID_IRemUnknown = { 0x00000131, 0, 0, {0xc0, 0, 0, 0, 0, 0, 0, 0x46} };
546
547 typedef struct rem_unknown
548 {
549     const IRemUnknownVtbl *lpVtbl;
550     LONG refs;
551 } RemUnknown;
552
553 static const IRemUnknownVtbl RemUnknown_Vtbl;
554
555
556 /* construct an IRemUnknown object with one outstanding reference */
557 static HRESULT RemUnknown_Construct(IRemUnknown **ppRemUnknown)
558 {
559     RemUnknown *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
560
561     if (!This) return E_OUTOFMEMORY;
562
563     This->lpVtbl = &RemUnknown_Vtbl;
564     This->refs = 1;
565
566     *ppRemUnknown = (IRemUnknown *)This;
567     return S_OK;
568 }
569
570 static HRESULT WINAPI RemUnknown_QueryInterface(IRemUnknown *iface, REFIID riid, void **ppv)
571 {
572     TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
573
574     if (IsEqualIID(riid, &IID_IUnknown) ||
575         IsEqualIID(riid, &IID_IRemUnknown))
576     {
577         *ppv = (LPVOID)iface;
578         IRemUnknown_AddRef(iface);
579         return S_OK;
580     }
581
582     FIXME("No interface for iid %s\n", debugstr_guid(riid));
583
584     *ppv = NULL;
585     return E_NOINTERFACE;
586 }
587
588 static ULONG WINAPI RemUnknown_AddRef(IRemUnknown *iface)
589 {
590     ULONG refs;
591     RemUnknown *This = (RemUnknown *)iface;
592
593     refs = InterlockedIncrement(&This->refs);
594
595     TRACE("%p before: %ld\n", iface, refs-1);
596     return refs;
597 }
598
599 static ULONG WINAPI RemUnknown_Release(IRemUnknown *iface)
600 {
601     ULONG refs;
602     RemUnknown *This = (RemUnknown *)iface;
603
604     refs = InterlockedDecrement(&This->refs);
605     if (!refs)
606         HeapFree(GetProcessHeap(), 0, This);
607
608     TRACE("%p after: %ld\n", iface, refs);
609     return refs;
610 }
611
612 static HRESULT WINAPI RemUnknown_RemQueryInterface(IRemUnknown *iface,
613     REFIPID ripid, ULONG cRefs, USHORT cIids, IID *iids /* [size_is(cIids)] */,
614     REMQIRESULT **ppQIResults /* [size_is(,cIids)] */)
615 {
616     HRESULT hr;
617     USHORT i;
618     USHORT successful_qis = 0;
619     APARTMENT *apt;
620     struct stub_manager *stubmgr;
621
622     TRACE("(%p)->(%s, %ld, %d, %p, %p)\n", iface, debugstr_guid(ripid), cRefs, cIids, iids, ppQIResults);
623
624     hr = ipid_to_stub_manager(ripid, &apt, &stubmgr);
625     if (hr != S_OK) return hr;
626
627     *ppQIResults = CoTaskMemAlloc(sizeof(REMQIRESULT) * cIids);
628
629     for (i = 0; i < cIids; i++)
630     {
631         HRESULT hrobj = marshal_object(apt, &(*ppQIResults)[i].std, &iids[i],
632                                        stubmgr->object, MSHLFLAGS_NORMAL);
633         if (hrobj == S_OK)
634             successful_qis++;
635         (*ppQIResults)[i].hResult = hrobj;
636     }
637
638     stub_manager_int_release(stubmgr);
639     apartment_release(apt);
640
641     if (successful_qis == cIids)
642         return S_OK; /* we got all requested interfaces */
643     else if (successful_qis == 0)
644         return E_NOINTERFACE; /* we didn't get any interfaces */
645     else
646         return S_FALSE; /* we got some interfaces */
647 }
648
649 static HRESULT WINAPI RemUnknown_RemAddRef(IRemUnknown *iface,
650     USHORT cInterfaceRefs,
651     REMINTERFACEREF* InterfaceRefs /* [size_is(cInterfaceRefs)] */,
652     HRESULT *pResults /* [size_is(cInterfaceRefs)] */)
653 {
654     HRESULT hr = S_OK;
655     USHORT i;
656
657     TRACE("(%p)->(%d, %p, %p)\n", iface, cInterfaceRefs, InterfaceRefs, pResults);
658
659     for (i = 0; i < cInterfaceRefs; i++)
660     {
661         APARTMENT *apt;
662         struct stub_manager *stubmgr;
663
664         pResults[i] = ipid_to_stub_manager(&InterfaceRefs[i].ipid, &apt, &stubmgr);
665         if (pResults[i] != S_OK)
666         {
667             hr = S_FALSE;
668             continue;
669         }
670
671         stub_manager_ext_addref(stubmgr, InterfaceRefs[i].cPublicRefs);
672         if (InterfaceRefs[i].cPrivateRefs)
673             FIXME("Adding %ld refs securely not implemented\n", InterfaceRefs[i].cPrivateRefs);
674
675         stub_manager_int_release(stubmgr);
676         apartment_release(apt);
677     }
678
679     return hr;
680 }
681
682 static HRESULT WINAPI RemUnknown_RemRelease(IRemUnknown *iface,
683     USHORT cInterfaceRefs,
684     REMINTERFACEREF* InterfaceRefs /* [size_is(cInterfaceRefs)] */)
685 {
686     HRESULT hr = S_OK;
687     USHORT i;
688
689     TRACE("(%p)->(%d, %p)\n", iface, cInterfaceRefs, InterfaceRefs);
690
691     for (i = 0; i < cInterfaceRefs; i++)
692     {
693         APARTMENT *apt;
694         struct stub_manager *stubmgr;
695
696         hr = ipid_to_stub_manager(&InterfaceRefs[i].ipid, &apt, &stubmgr);
697         if (hr != S_OK)
698         {
699             hr = E_INVALIDARG;
700             /* FIXME: we should undo any changes already made in this function */
701             break;
702         }
703
704         stub_manager_ext_release(stubmgr, InterfaceRefs[i].cPublicRefs);
705         if (InterfaceRefs[i].cPrivateRefs)
706             FIXME("Releasing %ld refs securely not implemented\n", InterfaceRefs[i].cPrivateRefs);
707
708         stub_manager_int_release(stubmgr);
709         apartment_release(apt);
710     }
711
712     return hr;
713 }
714
715 static const IRemUnknownVtbl RemUnknown_Vtbl =
716 {
717     RemUnknown_QueryInterface,
718     RemUnknown_AddRef,
719     RemUnknown_Release,
720     RemUnknown_RemQueryInterface,
721     RemUnknown_RemAddRef,
722     RemUnknown_RemRelease
723 };
724
725 /* starts the IRemUnknown listener for the current apartment */
726 HRESULT start_apartment_remote_unknown()
727 {
728     IRemUnknown *pRemUnknown;
729     HRESULT hr = S_OK;
730     APARTMENT *apt = COM_CurrentApt();
731
732     EnterCriticalSection(&apt->cs);
733     if (!apt->remunk_exported)
734     {
735         /* create the IRemUnknown object */
736         hr = RemUnknown_Construct(&pRemUnknown);
737         if (hr == S_OK)
738         {
739             STDOBJREF stdobjref; /* dummy - not used */
740             /* register it with the stub manager */
741             hr = marshal_object(apt, &stdobjref, &IID_IRemUnknown, (IUnknown *)pRemUnknown, MSHLFLAGS_NORMAL);
742             /* release our reference to the object as the stub manager will manage the life cycle for us */
743             IRemUnknown_Release(pRemUnknown);
744             if (hr == S_OK)
745                 apt->remunk_exported = TRUE;
746         }
747     }
748     LeaveCriticalSection(&apt->cs);
749     return hr;
750 }