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