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