ole32: Keep track of the number of weak references to an object.
[wine.git] / dlls / ole32 / stubmanager.c
blob8d7b3821de545dbce574eead1c45ae9da18ac8a5
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
27 #define NONAMELESSUNION
28 #define NONAMELESSSTRUCT
30 #include <assert.h>
31 #include <stdarg.h>
32 #include <limits.h>
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winuser.h"
37 #include "objbase.h"
38 #include "rpc.h"
40 #include "wine/debug.h"
41 #include "compobj_private.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(ole);
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);
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)
53 struct stub_manager *sm;
55 assert( apt );
57 sm = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct stub_manager));
58 if (!sm) return NULL;
60 list_init(&sm->ifstubs);
62 InitializeCriticalSection(&sm->lock);
63 DEBUG_SET_CRITSEC_NAME(&sm->lock, "stub_manager");
65 IUnknown_AddRef(object);
66 sm->object = object;
67 sm->apt = apt;
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 sm->weakrefs = 0;
74 sm->oxid_info.dwPid = GetCurrentProcessId();
75 sm->oxid_info.dwTid = GetCurrentThreadId();
77 * FIXME: this is a hack for marshalling IRemUnknown. In real
78 * DCOM, the IPID of the IRemUnknown interface is generated like
79 * any other and passed to the OXID resolver which then returns it
80 * when queried. We don't have an OXID resolver yet so instead we
81 * use a magic IPID reserved for IRemUnknown.
83 sm->oxid_info.ipidRemUnknown.Data1 = 0xffffffff;
84 sm->oxid_info.ipidRemUnknown.Data2 = 0xffff;
85 sm->oxid_info.ipidRemUnknown.Data3 = 0xffff;
86 assert(sizeof(sm->oxid_info.ipidRemUnknown.Data4) == sizeof(apt->oxid));
87 memcpy(&sm->oxid_info.ipidRemUnknown.Data4, &apt->oxid, sizeof(OXID));
88 sm->oxid_info.dwAuthnHint = RPC_C_AUTHN_LEVEL_NONE;
89 sm->oxid_info.psa = NULL /* FIXME */;
91 /* Yes, that's right, this starts at zero. that's zero EXTERNAL
92 * refs, i.e., nobody has unmarshalled anything yet. We can't have
93 * negative refs because the stub manager cannot be explicitly
94 * killed, it has to die by somebody unmarshalling then releasing
95 * the marshalled ifptr.
97 sm->extrefs = 0;
99 EnterCriticalSection(&apt->cs);
100 sm->oid = apt->oidc++;
101 list_add_head(&apt->stubmgrs, &sm->entry);
102 LeaveCriticalSection(&apt->cs);
104 TRACE("Created new stub manager (oid=%s) at %p for object with IUnknown %p\n", wine_dbgstr_longlong(sm->oid), sm, object);
106 return sm;
109 /* caller must remove stub manager from apartment prior to calling this function */
110 static void stub_manager_delete(struct stub_manager *m)
112 struct list *cursor;
114 TRACE("destroying %p (oid=%s)\n", m, wine_dbgstr_longlong(m->oid));
116 /* release every ifstub */
117 while ((cursor = list_head(&m->ifstubs)))
119 struct ifstub *ifstub = LIST_ENTRY(cursor, struct ifstub, entry);
120 stub_manager_delete_ifstub(m, ifstub);
123 CoTaskMemFree(m->oxid_info.psa);
124 IUnknown_Release(m->object);
126 DEBUG_CLEAR_CRITSEC_NAME(&m->lock);
127 DeleteCriticalSection(&m->lock);
129 HeapFree(GetProcessHeap(), 0, m);
132 /* gets the stub manager associated with an object - caller must have
133 * a reference to the apartment while a reference to the stub manager is held.
134 * it must also call release on the stub manager when it is no longer needed */
135 struct stub_manager *get_stub_manager_from_object(APARTMENT *apt, void *object)
137 struct stub_manager *result = NULL;
138 struct list *cursor;
140 EnterCriticalSection(&apt->cs);
141 LIST_FOR_EACH( cursor, &apt->stubmgrs )
143 struct stub_manager *m = LIST_ENTRY( cursor, struct stub_manager, entry );
145 if (m->object == object)
147 result = m;
148 stub_manager_int_addref(result);
149 break;
152 LeaveCriticalSection(&apt->cs);
154 if (result)
155 TRACE("found %p for object %p\n", result, object);
156 else
157 TRACE("not found for object %p\n", object);
159 return result;
162 /* removes the apartment reference to an object, destroying it when no other
163 * threads have a reference to it */
164 void apartment_disconnectobject(struct apartment *apt, void *object)
166 int found = FALSE;
167 struct stub_manager *stubmgr;
169 EnterCriticalSection(&apt->cs);
170 LIST_FOR_EACH_ENTRY( stubmgr, &apt->stubmgrs, struct stub_manager, entry )
172 if (stubmgr->object == object)
174 found = TRUE;
175 stub_manager_int_release(stubmgr);
176 break;
179 LeaveCriticalSection(&apt->cs);
181 if (found)
182 TRACE("disconnect object %p\n", object);
183 else
184 WARN("couldn't find object %p\n", object);
187 /* gets the stub manager associated with an object id - caller must have
188 * a reference to the apartment while a reference to the stub manager is held.
189 * it must also call release on the stub manager when it is no longer needed */
190 struct stub_manager *get_stub_manager(APARTMENT *apt, OID oid)
192 struct stub_manager *result = NULL;
193 struct list *cursor;
195 EnterCriticalSection(&apt->cs);
196 LIST_FOR_EACH( cursor, &apt->stubmgrs )
198 struct stub_manager *m = LIST_ENTRY( cursor, struct stub_manager, entry );
200 if (m->oid == oid)
202 result = m;
203 stub_manager_int_addref(result);
204 break;
207 LeaveCriticalSection(&apt->cs);
209 if (result)
210 TRACE("found %p for oid %s\n", result, wine_dbgstr_longlong(oid));
211 else
212 TRACE("not found for oid %s\n", wine_dbgstr_longlong(oid));
214 return result;
217 /* increments the internal refcount */
218 ULONG stub_manager_int_addref(struct stub_manager *This)
220 ULONG refs;
222 EnterCriticalSection(&This->apt->cs);
223 refs = ++This->refs;
224 LeaveCriticalSection(&This->apt->cs);
226 TRACE("before %d\n", refs - 1);
228 return refs;
231 /* decrements the internal refcount */
232 ULONG stub_manager_int_release(struct stub_manager *This)
234 ULONG refs;
235 APARTMENT *apt = This->apt;
237 EnterCriticalSection(&apt->cs);
238 refs = --This->refs;
240 TRACE("after %d\n", refs);
242 /* remove from apartment so no other thread can access it... */
243 if (!refs)
244 list_remove(&This->entry);
246 LeaveCriticalSection(&apt->cs);
248 /* ... so now we can delete it without being inside the apartment critsec */
249 if (!refs)
250 stub_manager_delete(This);
252 return refs;
255 /* add some external references (ie from a client that unmarshaled an ifptr) */
256 ULONG stub_manager_ext_addref(struct stub_manager *m, ULONG refs, BOOL tableweak)
258 ULONG rc;
260 EnterCriticalSection(&m->lock);
262 /* make sure we don't overflow extrefs */
263 refs = min(refs, (ULONG_MAX-1 - m->extrefs));
264 rc = (m->extrefs += refs);
266 if (tableweak)
267 rc += ++m->weakrefs;
269 LeaveCriticalSection(&m->lock);
271 TRACE("added %u refs to %p (oid %s), rc is now %u\n", refs, m, wine_dbgstr_longlong(m->oid), rc);
273 return rc;
276 /* remove some external references */
277 ULONG stub_manager_ext_release(struct stub_manager *m, ULONG refs, BOOL tableweak, BOOL last_unlock_releases)
279 ULONG rc;
281 EnterCriticalSection(&m->lock);
283 /* make sure we don't underflow extrefs */
284 refs = min(refs, m->extrefs);
285 rc = (m->extrefs -= refs);
287 if (tableweak)
288 rc += --m->weakrefs;
290 LeaveCriticalSection(&m->lock);
292 TRACE("removed %u refs from %p (oid %s), rc is now %u\n", refs, m, wine_dbgstr_longlong(m->oid), rc);
294 if (rc == 0 && last_unlock_releases)
295 stub_manager_int_release(m);
297 return rc;
300 static struct ifstub *stub_manager_ipid_to_ifstub(struct stub_manager *m, const IPID *ipid)
302 struct list *cursor;
303 struct ifstub *result = NULL;
305 EnterCriticalSection(&m->lock);
306 LIST_FOR_EACH( cursor, &m->ifstubs )
308 struct ifstub *ifstub = LIST_ENTRY( cursor, struct ifstub, entry );
310 if (IsEqualGUID(ipid, &ifstub->ipid))
312 result = ifstub;
313 break;
316 LeaveCriticalSection(&m->lock);
318 return result;
321 struct ifstub *stub_manager_find_ifstub(struct stub_manager *m, REFIID iid, MSHLFLAGS flags)
323 struct ifstub *result = NULL;
324 struct ifstub *ifstub;
326 EnterCriticalSection(&m->lock);
327 LIST_FOR_EACH_ENTRY( ifstub, &m->ifstubs, struct ifstub, entry )
329 if (IsEqualIID(iid, &ifstub->iid) && (ifstub->flags == flags))
331 result = ifstub;
332 break;
335 LeaveCriticalSection(&m->lock);
337 return result;
340 /* gets the stub manager associated with an ipid - caller must have
341 * a reference to the apartment while a reference to the stub manager is held.
342 * it must also call release on the stub manager when it is no longer needed */
343 static struct stub_manager *get_stub_manager_from_ipid(APARTMENT *apt, const IPID *ipid)
345 struct stub_manager *result = NULL;
346 struct list *cursor;
348 EnterCriticalSection(&apt->cs);
349 LIST_FOR_EACH( cursor, &apt->stubmgrs )
351 struct stub_manager *m = LIST_ENTRY( cursor, struct stub_manager, entry );
353 if (stub_manager_ipid_to_ifstub(m, ipid))
355 result = m;
356 stub_manager_int_addref(result);
357 break;
360 LeaveCriticalSection(&apt->cs);
362 if (result)
363 TRACE("found %p for ipid %s\n", result, debugstr_guid(ipid));
364 else
365 ERR("not found for ipid %s\n", debugstr_guid(ipid));
367 return result;
370 HRESULT ipid_to_stub_manager(const IPID *ipid, APARTMENT **stub_apt, struct stub_manager **stubmgr_ret)
372 /* FIXME: hack for IRemUnknown */
373 if (ipid->Data2 == 0xffff)
374 *stub_apt = apartment_findfromoxid(*(const OXID *)ipid->Data4, TRUE);
375 else
376 *stub_apt = apartment_findfromtid(ipid->Data2);
377 if (!*stub_apt)
379 TRACE("Couldn't find apartment corresponding to TID 0x%04x\n", ipid->Data2);
380 return RPC_E_INVALID_OBJECT;
382 *stubmgr_ret = get_stub_manager_from_ipid(*stub_apt, ipid);
383 if (!*stubmgr_ret)
385 apartment_release(*stub_apt);
386 *stub_apt = NULL;
387 return RPC_E_INVALID_OBJECT;
389 return S_OK;
392 /* gets the apartment, stub and channel of an object. the caller must
393 * release the references to all objects (except iface) if the function
394 * returned success, otherwise no references are returned. */
395 HRESULT ipid_get_dispatch_params(const IPID *ipid, APARTMENT **stub_apt,
396 IRpcStubBuffer **stub, IRpcChannelBuffer **chan,
397 IID *iid, IUnknown **iface)
399 struct stub_manager *stubmgr;
400 struct ifstub *ifstub;
401 APARTMENT *apt;
402 HRESULT hr;
404 hr = ipid_to_stub_manager(ipid, &apt, &stubmgr);
405 if (hr != S_OK) return RPC_E_DISCONNECTED;
407 ifstub = stub_manager_ipid_to_ifstub(stubmgr, ipid);
408 if (ifstub)
410 *stub = ifstub->stubbuffer;
411 IRpcStubBuffer_AddRef(*stub);
412 *chan = ifstub->chan;
413 IRpcChannelBuffer_AddRef(*chan);
414 *stub_apt = apt;
415 *iid = ifstub->iid;
416 *iface = ifstub->iface;
418 stub_manager_int_release(stubmgr);
419 return S_OK;
421 else
423 stub_manager_int_release(stubmgr);
424 apartment_release(apt);
425 return RPC_E_DISCONNECTED;
429 /* generates an ipid in the following format (similar to native version):
430 * Data1 = apartment-local ipid counter
431 * Data2 = apartment creator thread ID
432 * Data3 = process ID
433 * Data4 = random value
435 static inline HRESULT generate_ipid(struct stub_manager *m, IPID *ipid)
437 HRESULT hr;
438 hr = UuidCreate(ipid);
439 if (FAILED(hr))
441 ERR("couldn't create IPID for stub manager %p\n", m);
442 UuidCreateNil(ipid);
443 return hr;
446 ipid->Data1 = InterlockedIncrement(&m->apt->ipidc);
447 ipid->Data2 = (USHORT)m->apt->tid;
448 ipid->Data3 = (USHORT)GetCurrentProcessId();
449 return S_OK;
452 /* registers a new interface stub COM object with the stub manager and returns registration record */
453 struct ifstub *stub_manager_new_ifstub(struct stub_manager *m, IRpcStubBuffer *sb, IUnknown *iptr, REFIID iid, MSHLFLAGS flags)
455 struct ifstub *stub;
456 HRESULT hr;
458 TRACE("oid=%s, stubbuffer=%p, iptr=%p, iid=%s\n",
459 wine_dbgstr_longlong(m->oid), sb, iptr, debugstr_guid(iid));
461 stub = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct ifstub));
462 if (!stub) return NULL;
464 hr = RPC_CreateServerChannel(&stub->chan);
465 if (hr != S_OK)
467 HeapFree(GetProcessHeap(), 0, stub);
468 return NULL;
471 stub->stubbuffer = sb;
472 if (sb) IRpcStubBuffer_AddRef(sb);
474 IUnknown_AddRef(iptr);
475 stub->iface = iptr;
476 stub->flags = flags;
477 stub->iid = *iid;
479 /* FIXME: find a cleaner way of identifying that we are creating an ifstub
480 * for the remunknown interface */
481 if (flags & MSHLFLAGSP_REMUNKNOWN)
482 stub->ipid = m->oxid_info.ipidRemUnknown;
483 else
484 generate_ipid(m, &stub->ipid);
486 EnterCriticalSection(&m->lock);
487 list_add_head(&m->ifstubs, &stub->entry);
488 /* every normal marshal is counted so we don't allow more than we should */
489 if (flags & MSHLFLAGS_NORMAL) m->norm_refs++;
490 LeaveCriticalSection(&m->lock);
492 TRACE("ifstub %p created with ipid %s\n", stub, debugstr_guid(&stub->ipid));
494 return stub;
497 static void stub_manager_delete_ifstub(struct stub_manager *m, struct ifstub *ifstub)
499 TRACE("m=%p, m->oid=%s, ipid=%s\n", m, wine_dbgstr_longlong(m->oid), debugstr_guid(&ifstub->ipid));
501 list_remove(&ifstub->entry);
503 RPC_UnregisterInterface(&ifstub->iid);
505 if (ifstub->stubbuffer) IUnknown_Release(ifstub->stubbuffer);
506 IUnknown_Release(ifstub->iface);
507 IRpcChannelBuffer_Release(ifstub->chan);
509 HeapFree(GetProcessHeap(), 0, ifstub);
512 /* returns TRUE if it is possible to unmarshal, FALSE otherwise. */
513 BOOL stub_manager_notify_unmarshal(struct stub_manager *m, const IPID *ipid)
515 BOOL ret = TRUE;
516 struct ifstub *ifstub;
518 if (!(ifstub = stub_manager_ipid_to_ifstub(m, ipid)))
520 ERR("attempted unmarshal of unknown IPID %s\n", debugstr_guid(ipid));
521 return FALSE;
524 EnterCriticalSection(&m->lock);
526 /* track normal marshals so we can enforce rules whilst in-process */
527 if (ifstub->flags & MSHLFLAGS_NORMAL)
529 if (m->norm_refs)
530 m->norm_refs--;
531 else
533 ERR("attempted invalid normal unmarshal, norm_refs is zero\n");
534 ret = FALSE;
538 LeaveCriticalSection(&m->lock);
540 return ret;
543 /* handles refcounting for CoReleaseMarshalData */
544 void stub_manager_release_marshal_data(struct stub_manager *m, ULONG refs, const IPID *ipid, BOOL tableweak)
546 struct ifstub *ifstub;
548 if (!(ifstub = stub_manager_ipid_to_ifstub(m, ipid)))
549 return;
551 if (ifstub->flags & MSHLFLAGS_TABLEWEAK)
552 refs = 0;
553 else if (ifstub->flags & MSHLFLAGS_TABLESTRONG)
554 refs = 1;
556 stub_manager_ext_release(m, refs, tableweak, TRUE);
559 /* is an ifstub table marshaled? */
560 BOOL stub_manager_is_table_marshaled(struct stub_manager *m, const IPID *ipid)
562 struct ifstub *ifstub = stub_manager_ipid_to_ifstub(m, ipid);
564 assert( ifstub );
566 return ifstub->flags & (MSHLFLAGS_TABLESTRONG | MSHLFLAGS_TABLEWEAK);
570 /*****************************************************************************
572 * IRemUnknown implementation
575 * Note: this object is not related to the lifetime of a stub_manager, but it
576 * interacts with stub managers.
579 typedef struct rem_unknown
581 const IRemUnknownVtbl *lpVtbl;
582 LONG refs;
583 } RemUnknown;
585 static const IRemUnknownVtbl RemUnknown_Vtbl;
588 /* construct an IRemUnknown object with one outstanding reference */
589 static HRESULT RemUnknown_Construct(IRemUnknown **ppRemUnknown)
591 RemUnknown *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
593 if (!This) return E_OUTOFMEMORY;
595 This->lpVtbl = &RemUnknown_Vtbl;
596 This->refs = 1;
598 *ppRemUnknown = (IRemUnknown *)This;
599 return S_OK;
602 static HRESULT WINAPI RemUnknown_QueryInterface(IRemUnknown *iface, REFIID riid, void **ppv)
604 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
606 if (IsEqualIID(riid, &IID_IUnknown) ||
607 IsEqualIID(riid, &IID_IRemUnknown))
609 *ppv = (LPVOID)iface;
610 IRemUnknown_AddRef(iface);
611 return S_OK;
614 FIXME("No interface for iid %s\n", debugstr_guid(riid));
616 *ppv = NULL;
617 return E_NOINTERFACE;
620 static ULONG WINAPI RemUnknown_AddRef(IRemUnknown *iface)
622 ULONG refs;
623 RemUnknown *This = (RemUnknown *)iface;
625 refs = InterlockedIncrement(&This->refs);
627 TRACE("%p before: %d\n", iface, refs-1);
628 return refs;
631 static ULONG WINAPI RemUnknown_Release(IRemUnknown *iface)
633 ULONG refs;
634 RemUnknown *This = (RemUnknown *)iface;
636 refs = InterlockedDecrement(&This->refs);
637 if (!refs)
638 HeapFree(GetProcessHeap(), 0, This);
640 TRACE("%p after: %d\n", iface, refs);
641 return refs;
644 static HRESULT WINAPI RemUnknown_RemQueryInterface(IRemUnknown *iface,
645 REFIPID ripid, ULONG cRefs, USHORT cIids, IID *iids /* [size_is(cIids)] */,
646 REMQIRESULT **ppQIResults /* [size_is(,cIids)] */)
648 HRESULT hr;
649 USHORT i;
650 USHORT successful_qis = 0;
651 APARTMENT *apt;
652 struct stub_manager *stubmgr;
654 TRACE("(%p)->(%s, %d, %d, %p, %p)\n", iface, debugstr_guid(ripid), cRefs, cIids, iids, ppQIResults);
656 hr = ipid_to_stub_manager(ripid, &apt, &stubmgr);
657 if (hr != S_OK) return hr;
659 *ppQIResults = CoTaskMemAlloc(sizeof(REMQIRESULT) * cIids);
661 for (i = 0; i < cIids; i++)
663 HRESULT hrobj = marshal_object(apt, &(*ppQIResults)[i].std, &iids[i],
664 stubmgr->object, MSHLFLAGS_NORMAL);
665 if (hrobj == S_OK)
666 successful_qis++;
667 (*ppQIResults)[i].hResult = hrobj;
670 stub_manager_int_release(stubmgr);
671 apartment_release(apt);
673 if (successful_qis == cIids)
674 return S_OK; /* we got all requested interfaces */
675 else if (successful_qis == 0)
676 return E_NOINTERFACE; /* we didn't get any interfaces */
677 else
678 return S_FALSE; /* we got some interfaces */
681 static HRESULT WINAPI RemUnknown_RemAddRef(IRemUnknown *iface,
682 USHORT cInterfaceRefs,
683 REMINTERFACEREF* InterfaceRefs /* [size_is(cInterfaceRefs)] */,
684 HRESULT *pResults /* [size_is(cInterfaceRefs)] */)
686 HRESULT hr = S_OK;
687 USHORT i;
689 TRACE("(%p)->(%d, %p, %p)\n", iface, cInterfaceRefs, InterfaceRefs, pResults);
691 for (i = 0; i < cInterfaceRefs; i++)
693 APARTMENT *apt;
694 struct stub_manager *stubmgr;
696 pResults[i] = ipid_to_stub_manager(&InterfaceRefs[i].ipid, &apt, &stubmgr);
697 if (pResults[i] != S_OK)
699 hr = S_FALSE;
700 continue;
703 stub_manager_ext_addref(stubmgr, InterfaceRefs[i].cPublicRefs, FALSE);
704 if (InterfaceRefs[i].cPrivateRefs)
705 FIXME("Adding %ld refs securely not implemented\n", InterfaceRefs[i].cPrivateRefs);
707 stub_manager_int_release(stubmgr);
708 apartment_release(apt);
711 return hr;
714 static HRESULT WINAPI RemUnknown_RemRelease(IRemUnknown *iface,
715 USHORT cInterfaceRefs,
716 REMINTERFACEREF* InterfaceRefs /* [size_is(cInterfaceRefs)] */)
718 HRESULT hr = S_OK;
719 USHORT i;
721 TRACE("(%p)->(%d, %p)\n", iface, cInterfaceRefs, InterfaceRefs);
723 for (i = 0; i < cInterfaceRefs; i++)
725 APARTMENT *apt;
726 struct stub_manager *stubmgr;
728 hr = ipid_to_stub_manager(&InterfaceRefs[i].ipid, &apt, &stubmgr);
729 if (hr != S_OK)
731 hr = E_INVALIDARG;
732 /* FIXME: we should undo any changes already made in this function */
733 break;
736 stub_manager_ext_release(stubmgr, InterfaceRefs[i].cPublicRefs, FALSE, TRUE);
737 if (InterfaceRefs[i].cPrivateRefs)
738 FIXME("Releasing %ld refs securely not implemented\n", InterfaceRefs[i].cPrivateRefs);
740 stub_manager_int_release(stubmgr);
741 apartment_release(apt);
744 return hr;
747 static const IRemUnknownVtbl RemUnknown_Vtbl =
749 RemUnknown_QueryInterface,
750 RemUnknown_AddRef,
751 RemUnknown_Release,
752 RemUnknown_RemQueryInterface,
753 RemUnknown_RemAddRef,
754 RemUnknown_RemRelease
757 /* starts the IRemUnknown listener for the current apartment */
758 HRESULT start_apartment_remote_unknown(void)
760 IRemUnknown *pRemUnknown;
761 HRESULT hr = S_OK;
762 APARTMENT *apt = COM_CurrentApt();
764 EnterCriticalSection(&apt->cs);
765 if (!apt->remunk_exported)
767 /* create the IRemUnknown object */
768 hr = RemUnknown_Construct(&pRemUnknown);
769 if (hr == S_OK)
771 STDOBJREF stdobjref; /* dummy - not used */
772 /* register it with the stub manager */
773 hr = marshal_object(apt, &stdobjref, &IID_IRemUnknown, (IUnknown *)pRemUnknown, MSHLFLAGS_NORMAL|MSHLFLAGSP_REMUNKNOWN);
774 /* release our reference to the object as the stub manager will manage the life cycle for us */
775 IRemUnknown_Release(pRemUnknown);
776 if (hr == S_OK)
777 apt->remunk_exported = TRUE;
780 LeaveCriticalSection(&apt->cs);
781 return hr;