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