ntdll: Add support for file I/O on device files.
[wine.git] / dlls / ole32 / stubmanager.c
blobfda494448c95449a08a3e795bc1d205dcccfbac6
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.
7 * Copyright 2002 Marcus Meissner
8 * Copyright 2004 Mike Hearn for CodeWeavers
9 * Copyright 2004 Robert Shearman (for CodeWeavers)
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.
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.
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
26 #define COBJMACROS
28 #include <assert.h>
29 #include <stdarg.h>
30 #include <limits.h>
32 #include "windef.h"
33 #include "winbase.h"
34 #include "winuser.h"
35 #include "objbase.h"
36 #include "rpc.h"
38 #include "wine/debug.h"
39 #include "compobj_private.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(ole);
44 /* generates an ipid in the following format (similar to native version):
45 * Data1 = apartment-local ipid counter
46 * Data2 = apartment creator thread ID
47 * Data3 = process ID
48 * Data4 = random value
50 static inline HRESULT generate_ipid(struct stub_manager *m, IPID *ipid)
52 HRESULT hr;
53 hr = UuidCreate(ipid);
54 if (FAILED(hr))
56 ERR("couldn't create IPID for stub manager %p\n", m);
57 UuidCreateNil(ipid);
58 return hr;
61 ipid->Data1 = InterlockedIncrement(&m->apt->ipidc);
62 ipid->Data2 = (USHORT)m->apt->tid;
63 ipid->Data3 = (USHORT)GetCurrentProcessId();
64 return S_OK;
67 /* registers a new interface stub COM object with the stub manager and returns registration record */
68 struct ifstub *stub_manager_new_ifstub(struct stub_manager *m, IRpcStubBuffer *sb, IUnknown *iptr, REFIID iid, DWORD dest_context,
69 void *dest_context_data, MSHLFLAGS flags)
71 struct ifstub *stub;
72 HRESULT hr;
74 TRACE("oid=%s, stubbuffer=%p, iptr=%p, iid=%s\n",
75 wine_dbgstr_longlong(m->oid), sb, iptr, debugstr_guid(iid));
77 stub = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct ifstub));
78 if (!stub) return NULL;
80 hr = RPC_CreateServerChannel(dest_context, dest_context_data, &stub->chan);
81 if (hr != S_OK)
83 HeapFree(GetProcessHeap(), 0, stub);
84 return NULL;
87 stub->stubbuffer = sb;
88 if (sb) IRpcStubBuffer_AddRef(sb);
90 IUnknown_AddRef(iptr);
91 stub->iface = iptr;
92 stub->flags = flags;
93 stub->iid = *iid;
95 /* FIXME: find a cleaner way of identifying that we are creating an ifstub
96 * for the remunknown interface */
97 if (flags & MSHLFLAGSP_REMUNKNOWN)
98 stub->ipid = m->oxid_info.ipidRemUnknown;
99 else
100 generate_ipid(m, &stub->ipid);
102 EnterCriticalSection(&m->lock);
103 list_add_head(&m->ifstubs, &stub->entry);
104 /* every normal marshal is counted so we don't allow more than we should */
105 if (flags & MSHLFLAGS_NORMAL) m->norm_refs++;
106 LeaveCriticalSection(&m->lock);
108 TRACE("ifstub %p created with ipid %s\n", stub, debugstr_guid(&stub->ipid));
110 return stub;
113 static void stub_manager_delete_ifstub(struct stub_manager *m, struct ifstub *ifstub)
115 TRACE("m=%p, m->oid=%s, ipid=%s\n", m, wine_dbgstr_longlong(m->oid), debugstr_guid(&ifstub->ipid));
117 list_remove(&ifstub->entry);
119 RPC_UnregisterInterface(&ifstub->iid);
121 if (ifstub->stubbuffer) IRpcStubBuffer_Release(ifstub->stubbuffer);
122 IUnknown_Release(ifstub->iface);
123 IRpcChannelBuffer_Release(ifstub->chan);
125 HeapFree(GetProcessHeap(), 0, ifstub);
128 static struct ifstub *stub_manager_ipid_to_ifstub(struct stub_manager *m, const IPID *ipid)
130 struct list *cursor;
131 struct ifstub *result = NULL;
133 EnterCriticalSection(&m->lock);
134 LIST_FOR_EACH( cursor, &m->ifstubs )
136 struct ifstub *ifstub = LIST_ENTRY( cursor, struct ifstub, entry );
138 if (IsEqualGUID(ipid, &ifstub->ipid))
140 result = ifstub;
141 break;
144 LeaveCriticalSection(&m->lock);
146 return result;
149 struct ifstub *stub_manager_find_ifstub(struct stub_manager *m, REFIID iid, MSHLFLAGS flags)
151 struct ifstub *result = NULL;
152 struct ifstub *ifstub;
154 EnterCriticalSection(&m->lock);
155 LIST_FOR_EACH_ENTRY( ifstub, &m->ifstubs, struct ifstub, entry )
157 if (IsEqualIID(iid, &ifstub->iid) && (ifstub->flags == flags))
159 result = ifstub;
160 break;
163 LeaveCriticalSection(&m->lock);
165 return result;
168 /* creates a new stub manager and adds it into the apartment. caller must
169 * release stub manager when it is no longer required. the apartment and
170 * external refs together take one implicit ref */
171 struct stub_manager *new_stub_manager(APARTMENT *apt, IUnknown *object)
173 struct stub_manager *sm;
174 HRESULT hres;
176 assert( apt );
178 sm = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct stub_manager));
179 if (!sm) return NULL;
181 list_init(&sm->ifstubs);
183 InitializeCriticalSection(&sm->lock);
184 DEBUG_SET_CRITSEC_NAME(&sm->lock, "stub_manager");
186 IUnknown_AddRef(object);
187 sm->object = object;
188 sm->apt = apt;
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;
195 sm->oxid_info.dwPid = GetCurrentProcessId();
196 sm->oxid_info.dwTid = GetCurrentThreadId();
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.
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 */;
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.
218 sm->extrefs = 0;
220 hres = IUnknown_QueryInterface(object, &IID_IExternalConnection, (void**)&sm->extern_conn);
221 if(FAILED(hres))
222 sm->extern_conn = NULL;
224 EnterCriticalSection(&apt->cs);
225 sm->oid = apt->oidc++;
226 list_add_head(&apt->stubmgrs, &sm->entry);
227 LeaveCriticalSection(&apt->cs);
229 TRACE("Created new stub manager (oid=%s) at %p for object with IUnknown %p\n", wine_dbgstr_longlong(sm->oid), sm, object);
231 return sm;
234 /* caller must remove stub manager from apartment prior to calling this function */
235 static void stub_manager_delete(struct stub_manager *m)
237 struct list *cursor;
239 TRACE("destroying %p (oid=%s)\n", m, wine_dbgstr_longlong(m->oid));
241 /* release every ifstub */
242 while ((cursor = list_head(&m->ifstubs)))
244 struct ifstub *ifstub = LIST_ENTRY(cursor, struct ifstub, entry);
245 stub_manager_delete_ifstub(m, ifstub);
248 if(m->extern_conn)
249 IExternalConnection_Release(m->extern_conn);
251 CoTaskMemFree(m->oxid_info.psa);
252 IUnknown_Release(m->object);
254 DEBUG_CLEAR_CRITSEC_NAME(&m->lock);
255 DeleteCriticalSection(&m->lock);
257 HeapFree(GetProcessHeap(), 0, m);
260 /* increments the internal refcount */
261 static ULONG stub_manager_int_addref(struct stub_manager *This)
263 ULONG refs;
265 EnterCriticalSection(&This->apt->cs);
266 refs = ++This->refs;
267 LeaveCriticalSection(&This->apt->cs);
269 TRACE("before %d\n", refs - 1);
271 return refs;
274 /* decrements the internal refcount */
275 ULONG stub_manager_int_release(struct stub_manager *This)
277 ULONG refs;
278 APARTMENT *apt = This->apt;
280 EnterCriticalSection(&apt->cs);
281 refs = --This->refs;
283 TRACE("after %d\n", refs);
285 /* remove from apartment so no other thread can access it... */
286 if (!refs)
287 list_remove(&This->entry);
289 LeaveCriticalSection(&apt->cs);
291 /* ... so now we can delete it without being inside the apartment critsec */
292 if (!refs)
293 stub_manager_delete(This);
295 return refs;
298 /* gets the stub manager associated with an object - caller must have
299 * a reference to the apartment while a reference to the stub manager is held.
300 * it must also call release on the stub manager when it is no longer needed */
301 struct stub_manager *get_stub_manager_from_object(APARTMENT *apt, void *object)
303 struct stub_manager *result = NULL;
304 struct list *cursor;
306 EnterCriticalSection(&apt->cs);
307 LIST_FOR_EACH( cursor, &apt->stubmgrs )
309 struct stub_manager *m = LIST_ENTRY( cursor, struct stub_manager, entry );
311 if (m->object == object)
313 result = m;
314 stub_manager_int_addref(result);
315 break;
318 LeaveCriticalSection(&apt->cs);
320 if (result)
321 TRACE("found %p for object %p\n", result, object);
322 else
323 TRACE("not found for object %p\n", object);
325 return result;
328 /* removes the apartment reference to an object, destroying it when no other
329 * threads have a reference to it */
330 void apartment_disconnectobject(struct apartment *apt, void *object)
332 BOOL found = FALSE;
333 struct stub_manager *stubmgr;
335 EnterCriticalSection(&apt->cs);
336 LIST_FOR_EACH_ENTRY( stubmgr, &apt->stubmgrs, struct stub_manager, entry )
338 if (stubmgr->object == object)
340 found = TRUE;
341 stub_manager_int_release(stubmgr);
342 break;
345 LeaveCriticalSection(&apt->cs);
347 if (found)
348 TRACE("disconnect object %p\n", object);
349 else
350 WARN("couldn't find object %p\n", object);
353 /* gets the stub manager associated with an object id - caller must have
354 * a reference to the apartment while a reference to the stub manager is held.
355 * it must also call release on the stub manager when it is no longer needed */
356 struct stub_manager *get_stub_manager(APARTMENT *apt, OID oid)
358 struct stub_manager *result = NULL;
359 struct list *cursor;
361 EnterCriticalSection(&apt->cs);
362 LIST_FOR_EACH( cursor, &apt->stubmgrs )
364 struct stub_manager *m = LIST_ENTRY( cursor, struct stub_manager, entry );
366 if (m->oid == oid)
368 result = m;
369 stub_manager_int_addref(result);
370 break;
373 LeaveCriticalSection(&apt->cs);
375 if (result)
376 TRACE("found %p for oid %s\n", result, wine_dbgstr_longlong(oid));
377 else
378 TRACE("not found for oid %s\n", wine_dbgstr_longlong(oid));
380 return result;
383 /* add some external references (ie from a client that unmarshaled an ifptr) */
384 ULONG stub_manager_ext_addref(struct stub_manager *m, ULONG refs, BOOL tableweak)
386 BOOL first_extern_ref;
387 ULONG rc;
389 EnterCriticalSection(&m->lock);
391 first_extern_ref = refs && !m->extrefs;
393 /* make sure we don't overflow extrefs */
394 refs = min(refs, (ULONG_MAX-1 - m->extrefs));
395 rc = (m->extrefs += refs);
397 if (tableweak)
398 rc += ++m->weakrefs;
400 LeaveCriticalSection(&m->lock);
402 TRACE("added %u refs to %p (oid %s), rc is now %u\n", refs, m, wine_dbgstr_longlong(m->oid), rc);
405 * NOTE: According to tests, creating a stub causes two AddConnection calls followed by
406 * one ReleaseConnection call (with fLastReleaseCloses=FALSE).
408 if(first_extern_ref && m->extern_conn)
409 IExternalConnection_AddConnection(m->extern_conn, EXTCONN_STRONG, 0);
411 return rc;
414 /* remove some external references */
415 ULONG stub_manager_ext_release(struct stub_manager *m, ULONG refs, BOOL tableweak, BOOL last_unlock_releases)
417 BOOL last_extern_ref;
418 ULONG rc;
420 EnterCriticalSection(&m->lock);
422 /* make sure we don't underflow extrefs */
423 refs = min(refs, m->extrefs);
424 rc = (m->extrefs -= refs);
426 if (tableweak)
427 --m->weakrefs;
428 if (!last_unlock_releases)
429 rc += m->weakrefs;
431 last_extern_ref = refs && !m->extrefs;
433 LeaveCriticalSection(&m->lock);
435 TRACE("removed %u refs from %p (oid %s), rc is now %u\n", refs, m, wine_dbgstr_longlong(m->oid), rc);
437 if (last_extern_ref && m->extern_conn)
438 IExternalConnection_ReleaseConnection(m->extern_conn, EXTCONN_STRONG, 0, last_unlock_releases);
440 if (rc == 0)
441 if (!(m->extern_conn && last_unlock_releases && m->weakrefs))
442 stub_manager_int_release(m);
444 return rc;
447 /* gets the stub manager associated with an ipid - caller must have
448 * a reference to the apartment while a reference to the stub manager is held.
449 * it must also call release on the stub manager when it is no longer needed */
450 static struct stub_manager *get_stub_manager_from_ipid(APARTMENT *apt, const IPID *ipid)
452 struct stub_manager *result = NULL;
453 struct list *cursor;
455 EnterCriticalSection(&apt->cs);
456 LIST_FOR_EACH( cursor, &apt->stubmgrs )
458 struct stub_manager *m = LIST_ENTRY( cursor, struct stub_manager, entry );
460 if (stub_manager_ipid_to_ifstub(m, ipid))
462 result = m;
463 stub_manager_int_addref(result);
464 break;
467 LeaveCriticalSection(&apt->cs);
469 if (result)
470 TRACE("found %p for ipid %s\n", result, debugstr_guid(ipid));
471 else
472 ERR("not found for ipid %s\n", debugstr_guid(ipid));
474 return result;
477 static HRESULT ipid_to_stub_manager(const IPID *ipid, APARTMENT **stub_apt, struct stub_manager **stubmgr_ret)
479 /* FIXME: hack for IRemUnknown */
480 if (ipid->Data2 == 0xffff)
481 *stub_apt = apartment_findfromoxid(*(const OXID *)ipid->Data4, TRUE);
482 else
483 *stub_apt = apartment_findfromtid(ipid->Data2);
484 if (!*stub_apt)
486 TRACE("Couldn't find apartment corresponding to TID 0x%04x\n", ipid->Data2);
487 return RPC_E_INVALID_OBJECT;
489 *stubmgr_ret = get_stub_manager_from_ipid(*stub_apt, ipid);
490 if (!*stubmgr_ret)
492 apartment_release(*stub_apt);
493 *stub_apt = NULL;
494 return RPC_E_INVALID_OBJECT;
496 return S_OK;
499 /* gets the apartment, stub and channel of an object. the caller must
500 * release the references to all objects (except iface) if the function
501 * returned success, otherwise no references are returned. */
502 HRESULT ipid_get_dispatch_params(const IPID *ipid, APARTMENT **stub_apt,
503 IRpcStubBuffer **stub, IRpcChannelBuffer **chan,
504 IID *iid, IUnknown **iface)
506 struct stub_manager *stubmgr;
507 struct ifstub *ifstub;
508 APARTMENT *apt;
509 HRESULT hr;
511 hr = ipid_to_stub_manager(ipid, &apt, &stubmgr);
512 if (hr != S_OK) return RPC_E_DISCONNECTED;
514 ifstub = stub_manager_ipid_to_ifstub(stubmgr, ipid);
515 if (ifstub)
517 *stub = ifstub->stubbuffer;
518 IRpcStubBuffer_AddRef(*stub);
519 *chan = ifstub->chan;
520 IRpcChannelBuffer_AddRef(*chan);
521 *stub_apt = apt;
522 *iid = ifstub->iid;
523 *iface = ifstub->iface;
525 stub_manager_int_release(stubmgr);
526 return S_OK;
528 else
530 stub_manager_int_release(stubmgr);
531 apartment_release(apt);
532 return RPC_E_DISCONNECTED;
536 /* returns TRUE if it is possible to unmarshal, FALSE otherwise. */
537 BOOL stub_manager_notify_unmarshal(struct stub_manager *m, const IPID *ipid)
539 BOOL ret = TRUE;
540 struct ifstub *ifstub;
542 if (!(ifstub = stub_manager_ipid_to_ifstub(m, ipid)))
544 ERR("attempted unmarshal of unknown IPID %s\n", debugstr_guid(ipid));
545 return FALSE;
548 EnterCriticalSection(&m->lock);
550 /* track normal marshals so we can enforce rules whilst in-process */
551 if (ifstub->flags & MSHLFLAGS_NORMAL)
553 if (m->norm_refs)
554 m->norm_refs--;
555 else
557 ERR("attempted invalid normal unmarshal, norm_refs is zero\n");
558 ret = FALSE;
562 LeaveCriticalSection(&m->lock);
564 return ret;
567 /* handles refcounting for CoReleaseMarshalData */
568 void stub_manager_release_marshal_data(struct stub_manager *m, ULONG refs, const IPID *ipid, BOOL tableweak)
570 struct ifstub *ifstub;
572 if (!(ifstub = stub_manager_ipid_to_ifstub(m, ipid)))
573 return;
575 if (ifstub->flags & MSHLFLAGS_TABLEWEAK)
576 refs = 0;
577 else if (ifstub->flags & MSHLFLAGS_TABLESTRONG)
578 refs = 1;
580 stub_manager_ext_release(m, refs, tableweak, !tableweak);
583 /* is an ifstub table marshaled? */
584 BOOL stub_manager_is_table_marshaled(struct stub_manager *m, const IPID *ipid)
586 struct ifstub *ifstub = stub_manager_ipid_to_ifstub(m, ipid);
588 assert( ifstub );
590 return ifstub->flags & (MSHLFLAGS_TABLESTRONG | MSHLFLAGS_TABLEWEAK);
594 /*****************************************************************************
596 * IRemUnknown implementation
599 * Note: this object is not related to the lifetime of a stub_manager, but it
600 * interacts with stub managers.
603 typedef struct rem_unknown
605 IRemUnknown IRemUnknown_iface;
606 LONG refs;
607 } RemUnknown;
609 static const IRemUnknownVtbl RemUnknown_Vtbl;
611 static inline RemUnknown *impl_from_IRemUnknown(IRemUnknown *iface)
613 return CONTAINING_RECORD(iface, RemUnknown, IRemUnknown_iface);
617 /* construct an IRemUnknown object with one outstanding reference */
618 static HRESULT RemUnknown_Construct(IRemUnknown **ppRemUnknown)
620 RemUnknown *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
622 if (!This) return E_OUTOFMEMORY;
624 This->IRemUnknown_iface.lpVtbl = &RemUnknown_Vtbl;
625 This->refs = 1;
627 *ppRemUnknown = &This->IRemUnknown_iface;
628 return S_OK;
631 static HRESULT WINAPI RemUnknown_QueryInterface(IRemUnknown *iface, REFIID riid, void **ppv)
633 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
635 if (IsEqualIID(riid, &IID_IUnknown) ||
636 IsEqualIID(riid, &IID_IRemUnknown))
638 *ppv = iface;
639 IRemUnknown_AddRef(iface);
640 return S_OK;
643 FIXME("No interface for iid %s\n", debugstr_guid(riid));
645 *ppv = NULL;
646 return E_NOINTERFACE;
649 static ULONG WINAPI RemUnknown_AddRef(IRemUnknown *iface)
651 ULONG refs;
652 RemUnknown *This = impl_from_IRemUnknown(iface);
654 refs = InterlockedIncrement(&This->refs);
656 TRACE("%p before: %d\n", iface, refs-1);
657 return refs;
660 static ULONG WINAPI RemUnknown_Release(IRemUnknown *iface)
662 ULONG refs;
663 RemUnknown *This = impl_from_IRemUnknown(iface);
665 refs = InterlockedDecrement(&This->refs);
666 if (!refs)
667 HeapFree(GetProcessHeap(), 0, This);
669 TRACE("%p after: %d\n", iface, refs);
670 return refs;
673 static HRESULT WINAPI RemUnknown_RemQueryInterface(IRemUnknown *iface,
674 REFIPID ripid, ULONG cRefs, USHORT cIids, IID *iids /* [size_is(cIids)] */,
675 REMQIRESULT **ppQIResults /* [size_is(,cIids)] */)
677 HRESULT hr;
678 USHORT i;
679 USHORT successful_qis = 0;
680 APARTMENT *apt;
681 struct stub_manager *stubmgr;
683 TRACE("(%p)->(%s, %d, %d, %p, %p)\n", iface, debugstr_guid(ripid), cRefs, cIids, iids, ppQIResults);
685 hr = ipid_to_stub_manager(ripid, &apt, &stubmgr);
686 if (hr != S_OK) return hr;
688 *ppQIResults = CoTaskMemAlloc(sizeof(REMQIRESULT) * cIids);
690 for (i = 0; i < cIids; i++)
692 HRESULT hrobj = marshal_object(apt, &(*ppQIResults)[i].std, &iids[i],
693 stubmgr->object, MSHCTX_DIFFERENTMACHINE, NULL, MSHLFLAGS_NORMAL);
694 if (hrobj == S_OK)
695 successful_qis++;
696 (*ppQIResults)[i].hResult = hrobj;
699 stub_manager_int_release(stubmgr);
700 apartment_release(apt);
702 if (successful_qis == cIids)
703 return S_OK; /* we got all requested interfaces */
704 else if (successful_qis == 0)
705 return E_NOINTERFACE; /* we didn't get any interfaces */
706 else
707 return S_FALSE; /* we got some interfaces */
710 static HRESULT WINAPI RemUnknown_RemAddRef(IRemUnknown *iface,
711 USHORT cInterfaceRefs,
712 REMINTERFACEREF* InterfaceRefs /* [size_is(cInterfaceRefs)] */,
713 HRESULT *pResults /* [size_is(cInterfaceRefs)] */)
715 HRESULT hr = S_OK;
716 USHORT i;
718 TRACE("(%p)->(%d, %p, %p)\n", iface, cInterfaceRefs, InterfaceRefs, pResults);
720 for (i = 0; i < cInterfaceRefs; i++)
722 APARTMENT *apt;
723 struct stub_manager *stubmgr;
725 pResults[i] = ipid_to_stub_manager(&InterfaceRefs[i].ipid, &apt, &stubmgr);
726 if (pResults[i] != S_OK)
728 hr = S_FALSE;
729 continue;
732 stub_manager_ext_addref(stubmgr, InterfaceRefs[i].cPublicRefs, FALSE);
733 if (InterfaceRefs[i].cPrivateRefs)
734 FIXME("Adding %d refs securely not implemented\n", InterfaceRefs[i].cPrivateRefs);
736 stub_manager_int_release(stubmgr);
737 apartment_release(apt);
740 return hr;
743 static HRESULT WINAPI RemUnknown_RemRelease(IRemUnknown *iface,
744 USHORT cInterfaceRefs,
745 REMINTERFACEREF* InterfaceRefs /* [size_is(cInterfaceRefs)] */)
747 HRESULT hr = S_OK;
748 USHORT i;
750 TRACE("(%p)->(%d, %p)\n", iface, cInterfaceRefs, InterfaceRefs);
752 for (i = 0; i < cInterfaceRefs; i++)
754 APARTMENT *apt;
755 struct stub_manager *stubmgr;
757 hr = ipid_to_stub_manager(&InterfaceRefs[i].ipid, &apt, &stubmgr);
758 if (hr != S_OK)
760 hr = E_INVALIDARG;
761 /* FIXME: we should undo any changes already made in this function */
762 break;
765 stub_manager_ext_release(stubmgr, InterfaceRefs[i].cPublicRefs, FALSE, TRUE);
766 if (InterfaceRefs[i].cPrivateRefs)
767 FIXME("Releasing %d refs securely not implemented\n", InterfaceRefs[i].cPrivateRefs);
769 stub_manager_int_release(stubmgr);
770 apartment_release(apt);
773 return hr;
776 static const IRemUnknownVtbl RemUnknown_Vtbl =
778 RemUnknown_QueryInterface,
779 RemUnknown_AddRef,
780 RemUnknown_Release,
781 RemUnknown_RemQueryInterface,
782 RemUnknown_RemAddRef,
783 RemUnknown_RemRelease
786 /* starts the IRemUnknown listener for the current apartment */
787 HRESULT start_apartment_remote_unknown(void)
789 IRemUnknown *pRemUnknown;
790 HRESULT hr = S_OK;
791 APARTMENT *apt = COM_CurrentApt();
793 EnterCriticalSection(&apt->cs);
794 if (!apt->remunk_exported)
796 /* create the IRemUnknown object */
797 hr = RemUnknown_Construct(&pRemUnknown);
798 if (hr == S_OK)
800 STDOBJREF stdobjref; /* dummy - not used */
801 /* register it with the stub manager */
802 hr = marshal_object(apt, &stdobjref, &IID_IRemUnknown, (IUnknown *)pRemUnknown, MSHCTX_DIFFERENTMACHINE, NULL, MSHLFLAGS_NORMAL|MSHLFLAGSP_REMUNKNOWN);
803 /* release our reference to the object as the stub manager will manage the life cycle for us */
804 IRemUnknown_Release(pRemUnknown);
805 if (hr == S_OK)
806 apt->remunk_exported = TRUE;
809 LeaveCriticalSection(&apt->cs);
810 return hr;