Assorted spelling fixes.
[wine.git] / dlls / ole32 / stubmanager.c
blobfbbf413ecd818e04fd5f042f4eee7d5c40c50d12
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);
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
52 static inline HRESULT generate_ipid(struct stub_manager *m, IPID *ipid)
54 HRESULT hr;
55 hr = UuidCreate(ipid);
56 if (FAILED(hr))
58 ERR("couldn't create IPID for stub manager %p\n", m);
59 UuidCreateNil(ipid);
60 return hr;
63 ipid->Data1 = InterlockedIncrement(&m->apt->ipidc);
64 ipid->Data2 = (USHORT)m->apt->tid;
65 ipid->Data3 = (USHORT)GetCurrentProcessId();
66 return S_OK;
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, DWORD dest_context,
71 void *dest_context_data, MSHLFLAGS flags)
73 struct ifstub *stub;
74 HRESULT hr;
76 TRACE("oid=%s, stubbuffer=%p, iptr=%p, iid=%s\n",
77 wine_dbgstr_longlong(m->oid), sb, iptr, debugstr_guid(iid));
79 stub = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct ifstub));
80 if (!stub) return NULL;
82 hr = RPC_CreateServerChannel(dest_context, dest_context_data, &stub->chan);
83 if (hr != S_OK)
85 HeapFree(GetProcessHeap(), 0, stub);
86 return NULL;
89 stub->stubbuffer = sb;
90 if (sb) IRpcStubBuffer_AddRef(sb);
92 IUnknown_AddRef(iptr);
93 stub->iface = iptr;
94 stub->flags = flags;
95 stub->iid = *iid;
97 /* FIXME: find a cleaner way of identifying that we are creating an ifstub
98 * for the remunknown interface */
99 if (flags & MSHLFLAGSP_REMUNKNOWN)
100 stub->ipid = m->oxid_info.ipidRemUnknown;
101 else
102 generate_ipid(m, &stub->ipid);
104 EnterCriticalSection(&m->lock);
105 list_add_head(&m->ifstubs, &stub->entry);
106 /* every normal marshal is counted so we don't allow more than we should */
107 if (flags & MSHLFLAGS_NORMAL) m->norm_refs++;
108 LeaveCriticalSection(&m->lock);
110 TRACE("ifstub %p created with ipid %s\n", stub, debugstr_guid(&stub->ipid));
112 return stub;
115 static void stub_manager_delete_ifstub(struct stub_manager *m, struct ifstub *ifstub)
117 TRACE("m=%p, m->oid=%s, ipid=%s\n", m, wine_dbgstr_longlong(m->oid), debugstr_guid(&ifstub->ipid));
119 list_remove(&ifstub->entry);
121 RPC_UnregisterInterface(&ifstub->iid);
123 if (ifstub->stubbuffer) IRpcStubBuffer_Release(ifstub->stubbuffer);
124 IUnknown_Release(ifstub->iface);
125 IRpcChannelBuffer_Release(ifstub->chan);
127 HeapFree(GetProcessHeap(), 0, ifstub);
130 static struct ifstub *stub_manager_ipid_to_ifstub(struct stub_manager *m, const IPID *ipid)
132 struct list *cursor;
133 struct ifstub *result = NULL;
135 EnterCriticalSection(&m->lock);
136 LIST_FOR_EACH( cursor, &m->ifstubs )
138 struct ifstub *ifstub = LIST_ENTRY( cursor, struct ifstub, entry );
140 if (IsEqualGUID(ipid, &ifstub->ipid))
142 result = ifstub;
143 break;
146 LeaveCriticalSection(&m->lock);
148 return result;
151 struct ifstub *stub_manager_find_ifstub(struct stub_manager *m, REFIID iid, MSHLFLAGS flags)
153 struct ifstub *result = NULL;
154 struct ifstub *ifstub;
156 EnterCriticalSection(&m->lock);
157 LIST_FOR_EACH_ENTRY( ifstub, &m->ifstubs, struct ifstub, entry )
159 if (IsEqualIID(iid, &ifstub->iid) && (ifstub->flags == flags))
161 result = ifstub;
162 break;
165 LeaveCriticalSection(&m->lock);
167 return result;
170 /* creates a new stub manager and adds it into the apartment. caller must
171 * release stub manager when it is no longer required. the apartment and
172 * external refs together take one implicit ref */
173 struct stub_manager *new_stub_manager(APARTMENT *apt, IUnknown *object)
175 struct stub_manager *sm;
176 HRESULT hres;
178 assert( apt );
180 sm = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct stub_manager));
181 if (!sm) return NULL;
183 list_init(&sm->ifstubs);
185 InitializeCriticalSection(&sm->lock);
186 DEBUG_SET_CRITSEC_NAME(&sm->lock, "stub_manager");
188 IUnknown_AddRef(object);
189 sm->object = object;
190 sm->apt = apt;
192 /* start off with 2 references because the stub is in the apartment
193 * and the caller will also hold a reference */
194 sm->refs = 2;
195 sm->weakrefs = 0;
197 sm->oxid_info.dwPid = GetCurrentProcessId();
198 sm->oxid_info.dwTid = GetCurrentThreadId();
200 * FIXME: this is a hack for marshalling IRemUnknown. In real
201 * DCOM, the IPID of the IRemUnknown interface is generated like
202 * any other and passed to the OXID resolver which then returns it
203 * when queried. We don't have an OXID resolver yet so instead we
204 * use a magic IPID reserved for IRemUnknown.
206 sm->oxid_info.ipidRemUnknown.Data1 = 0xffffffff;
207 sm->oxid_info.ipidRemUnknown.Data2 = 0xffff;
208 sm->oxid_info.ipidRemUnknown.Data3 = 0xffff;
209 assert(sizeof(sm->oxid_info.ipidRemUnknown.Data4) == sizeof(apt->oxid));
210 memcpy(sm->oxid_info.ipidRemUnknown.Data4, &apt->oxid, sizeof(OXID));
211 sm->oxid_info.dwAuthnHint = RPC_C_AUTHN_LEVEL_NONE;
212 sm->oxid_info.psa = NULL /* FIXME */;
214 /* Yes, that's right, this starts at zero. that's zero EXTERNAL
215 * refs, i.e., nobody has unmarshalled anything yet. We can't have
216 * negative refs because the stub manager cannot be explicitly
217 * killed, it has to die by somebody unmarshalling then releasing
218 * the marshalled ifptr.
220 sm->extrefs = 0;
222 hres = IUnknown_QueryInterface(object, &IID_IExternalConnection, (void**)&sm->extern_conn);
223 if(FAILED(hres))
224 sm->extern_conn = NULL;
226 EnterCriticalSection(&apt->cs);
227 sm->oid = apt->oidc++;
228 list_add_head(&apt->stubmgrs, &sm->entry);
229 LeaveCriticalSection(&apt->cs);
231 TRACE("Created new stub manager (oid=%s) at %p for object with IUnknown %p\n", wine_dbgstr_longlong(sm->oid), sm, object);
233 return sm;
236 /* caller must remove stub manager from apartment prior to calling this function */
237 static void stub_manager_delete(struct stub_manager *m)
239 struct list *cursor;
241 TRACE("destroying %p (oid=%s)\n", m, wine_dbgstr_longlong(m->oid));
243 /* release every ifstub */
244 while ((cursor = list_head(&m->ifstubs)))
246 struct ifstub *ifstub = LIST_ENTRY(cursor, struct ifstub, entry);
247 stub_manager_delete_ifstub(m, ifstub);
250 if(m->extern_conn)
251 IExternalConnection_Release(m->extern_conn);
253 CoTaskMemFree(m->oxid_info.psa);
254 IUnknown_Release(m->object);
256 DEBUG_CLEAR_CRITSEC_NAME(&m->lock);
257 DeleteCriticalSection(&m->lock);
259 HeapFree(GetProcessHeap(), 0, m);
262 /* increments the internal refcount */
263 static ULONG stub_manager_int_addref(struct stub_manager *This)
265 ULONG refs;
267 EnterCriticalSection(&This->apt->cs);
268 refs = ++This->refs;
269 LeaveCriticalSection(&This->apt->cs);
271 TRACE("before %d\n", refs - 1);
273 return refs;
276 /* decrements the internal refcount */
277 ULONG stub_manager_int_release(struct stub_manager *This)
279 ULONG refs;
280 APARTMENT *apt = This->apt;
282 EnterCriticalSection(&apt->cs);
283 refs = --This->refs;
285 TRACE("after %d\n", refs);
287 /* remove from apartment so no other thread can access it... */
288 if (!refs)
289 list_remove(&This->entry);
291 LeaveCriticalSection(&apt->cs);
293 /* ... so now we can delete it without being inside the apartment critsec */
294 if (!refs)
295 stub_manager_delete(This);
297 return refs;
300 /* gets the stub manager associated with an object - caller must have
301 * a reference to the apartment while a reference to the stub manager is held.
302 * it must also call release on the stub manager when it is no longer needed */
303 struct stub_manager *get_stub_manager_from_object(APARTMENT *apt, void *object)
305 struct stub_manager *result = NULL;
306 struct list *cursor;
308 EnterCriticalSection(&apt->cs);
309 LIST_FOR_EACH( cursor, &apt->stubmgrs )
311 struct stub_manager *m = LIST_ENTRY( cursor, struct stub_manager, entry );
313 if (m->object == object)
315 result = m;
316 stub_manager_int_addref(result);
317 break;
320 LeaveCriticalSection(&apt->cs);
322 if (result)
323 TRACE("found %p for object %p\n", result, object);
324 else
325 TRACE("not found for object %p\n", object);
327 return result;
330 /* removes the apartment reference to an object, destroying it when no other
331 * threads have a reference to it */
332 void apartment_disconnectobject(struct apartment *apt, void *object)
334 BOOL found = FALSE;
335 struct stub_manager *stubmgr;
337 EnterCriticalSection(&apt->cs);
338 LIST_FOR_EACH_ENTRY( stubmgr, &apt->stubmgrs, struct stub_manager, entry )
340 if (stubmgr->object == object)
342 found = TRUE;
343 stub_manager_int_release(stubmgr);
344 break;
347 LeaveCriticalSection(&apt->cs);
349 if (found)
350 TRACE("disconnect object %p\n", object);
351 else
352 WARN("couldn't find object %p\n", object);
355 /* gets the stub manager associated with an object id - caller must have
356 * a reference to the apartment while a reference to the stub manager is held.
357 * it must also call release on the stub manager when it is no longer needed */
358 struct stub_manager *get_stub_manager(APARTMENT *apt, OID oid)
360 struct stub_manager *result = NULL;
361 struct list *cursor;
363 EnterCriticalSection(&apt->cs);
364 LIST_FOR_EACH( cursor, &apt->stubmgrs )
366 struct stub_manager *m = LIST_ENTRY( cursor, struct stub_manager, entry );
368 if (m->oid == oid)
370 result = m;
371 stub_manager_int_addref(result);
372 break;
375 LeaveCriticalSection(&apt->cs);
377 if (result)
378 TRACE("found %p for oid %s\n", result, wine_dbgstr_longlong(oid));
379 else
380 TRACE("not found for oid %s\n", wine_dbgstr_longlong(oid));
382 return result;
385 /* add some external references (ie from a client that unmarshaled an ifptr) */
386 ULONG stub_manager_ext_addref(struct stub_manager *m, ULONG refs, BOOL tableweak)
388 BOOL first_extern_ref;
389 ULONG rc;
391 EnterCriticalSection(&m->lock);
393 first_extern_ref = refs && !m->extrefs;
395 /* make sure we don't overflow extrefs */
396 refs = min(refs, (ULONG_MAX-1 - m->extrefs));
397 rc = (m->extrefs += refs);
399 if (tableweak)
400 rc += ++m->weakrefs;
402 LeaveCriticalSection(&m->lock);
404 TRACE("added %u refs to %p (oid %s), rc is now %u\n", refs, m, wine_dbgstr_longlong(m->oid), rc);
407 * NOTE: According to tests, creating a stub causes two AddConnection calls followed by
408 * one ReleaseConnection call (with fLastReleaseCloses=FALSE).
410 if(first_extern_ref && m->extern_conn)
411 IExternalConnection_AddConnection(m->extern_conn, EXTCONN_STRONG, 0);
413 return rc;
416 /* remove some external references */
417 ULONG stub_manager_ext_release(struct stub_manager *m, ULONG refs, BOOL tableweak, BOOL last_unlock_releases)
419 BOOL last_extern_ref;
420 ULONG rc;
422 EnterCriticalSection(&m->lock);
424 /* make sure we don't underflow extrefs */
425 refs = min(refs, m->extrefs);
426 rc = (m->extrefs -= refs);
428 if (tableweak)
429 --m->weakrefs;
430 if (!last_unlock_releases)
431 rc += m->weakrefs;
433 last_extern_ref = refs && !m->extrefs;
435 LeaveCriticalSection(&m->lock);
437 TRACE("removed %u refs from %p (oid %s), rc is now %u\n", refs, m, wine_dbgstr_longlong(m->oid), rc);
439 if (last_extern_ref && m->extern_conn)
440 IExternalConnection_ReleaseConnection(m->extern_conn, EXTCONN_STRONG, 0, last_unlock_releases);
442 if (rc == 0)
443 if (!(m->extern_conn && last_unlock_releases && m->weakrefs))
444 stub_manager_int_release(m);
446 return rc;
449 /* gets the stub manager associated with an ipid - caller must have
450 * a reference to the apartment while a reference to the stub manager is held.
451 * it must also call release on the stub manager when it is no longer needed */
452 static struct stub_manager *get_stub_manager_from_ipid(APARTMENT *apt, const IPID *ipid)
454 struct stub_manager *result = NULL;
455 struct list *cursor;
457 EnterCriticalSection(&apt->cs);
458 LIST_FOR_EACH( cursor, &apt->stubmgrs )
460 struct stub_manager *m = LIST_ENTRY( cursor, struct stub_manager, entry );
462 if (stub_manager_ipid_to_ifstub(m, ipid))
464 result = m;
465 stub_manager_int_addref(result);
466 break;
469 LeaveCriticalSection(&apt->cs);
471 if (result)
472 TRACE("found %p for ipid %s\n", result, debugstr_guid(ipid));
473 else
474 ERR("not found for ipid %s\n", debugstr_guid(ipid));
476 return result;
479 static HRESULT ipid_to_stub_manager(const IPID *ipid, APARTMENT **stub_apt, struct stub_manager **stubmgr_ret)
481 /* FIXME: hack for IRemUnknown */
482 if (ipid->Data2 == 0xffff)
483 *stub_apt = apartment_findfromoxid(*(const OXID *)ipid->Data4, TRUE);
484 else
485 *stub_apt = apartment_findfromtid(ipid->Data2);
486 if (!*stub_apt)
488 TRACE("Couldn't find apartment corresponding to TID 0x%04x\n", ipid->Data2);
489 return RPC_E_INVALID_OBJECT;
491 *stubmgr_ret = get_stub_manager_from_ipid(*stub_apt, ipid);
492 if (!*stubmgr_ret)
494 apartment_release(*stub_apt);
495 *stub_apt = NULL;
496 return RPC_E_INVALID_OBJECT;
498 return S_OK;
501 /* gets the apartment, stub and channel of an object. the caller must
502 * release the references to all objects (except iface) if the function
503 * returned success, otherwise no references are returned. */
504 HRESULT ipid_get_dispatch_params(const IPID *ipid, APARTMENT **stub_apt,
505 IRpcStubBuffer **stub, IRpcChannelBuffer **chan,
506 IID *iid, IUnknown **iface)
508 struct stub_manager *stubmgr;
509 struct ifstub *ifstub;
510 APARTMENT *apt;
511 HRESULT hr;
513 hr = ipid_to_stub_manager(ipid, &apt, &stubmgr);
514 if (hr != S_OK) return RPC_E_DISCONNECTED;
516 ifstub = stub_manager_ipid_to_ifstub(stubmgr, ipid);
517 if (ifstub)
519 *stub = ifstub->stubbuffer;
520 IRpcStubBuffer_AddRef(*stub);
521 *chan = ifstub->chan;
522 IRpcChannelBuffer_AddRef(*chan);
523 *stub_apt = apt;
524 *iid = ifstub->iid;
525 *iface = ifstub->iface;
527 stub_manager_int_release(stubmgr);
528 return S_OK;
530 else
532 stub_manager_int_release(stubmgr);
533 apartment_release(apt);
534 return RPC_E_DISCONNECTED;
538 /* returns TRUE if it is possible to unmarshal, FALSE otherwise. */
539 BOOL stub_manager_notify_unmarshal(struct stub_manager *m, const IPID *ipid)
541 BOOL ret = TRUE;
542 struct ifstub *ifstub;
544 if (!(ifstub = stub_manager_ipid_to_ifstub(m, ipid)))
546 ERR("attempted unmarshal of unknown IPID %s\n", debugstr_guid(ipid));
547 return FALSE;
550 EnterCriticalSection(&m->lock);
552 /* track normal marshals so we can enforce rules whilst in-process */
553 if (ifstub->flags & MSHLFLAGS_NORMAL)
555 if (m->norm_refs)
556 m->norm_refs--;
557 else
559 ERR("attempted invalid normal unmarshal, norm_refs is zero\n");
560 ret = FALSE;
564 LeaveCriticalSection(&m->lock);
566 return ret;
569 /* handles refcounting for CoReleaseMarshalData */
570 void stub_manager_release_marshal_data(struct stub_manager *m, ULONG refs, const IPID *ipid, BOOL tableweak)
572 struct ifstub *ifstub;
574 if (!(ifstub = stub_manager_ipid_to_ifstub(m, ipid)))
575 return;
577 if (ifstub->flags & MSHLFLAGS_TABLEWEAK)
578 refs = 0;
579 else if (ifstub->flags & MSHLFLAGS_TABLESTRONG)
580 refs = 1;
582 stub_manager_ext_release(m, refs, tableweak, !tableweak);
585 /* is an ifstub table marshaled? */
586 BOOL stub_manager_is_table_marshaled(struct stub_manager *m, const IPID *ipid)
588 struct ifstub *ifstub = stub_manager_ipid_to_ifstub(m, ipid);
590 assert( ifstub );
592 return ifstub->flags & (MSHLFLAGS_TABLESTRONG | MSHLFLAGS_TABLEWEAK);
596 /*****************************************************************************
598 * IRemUnknown implementation
601 * Note: this object is not related to the lifetime of a stub_manager, but it
602 * interacts with stub managers.
605 typedef struct rem_unknown
607 IRemUnknown IRemUnknown_iface;
608 LONG refs;
609 } RemUnknown;
611 static const IRemUnknownVtbl RemUnknown_Vtbl;
613 static inline RemUnknown *impl_from_IRemUnknown(IRemUnknown *iface)
615 return CONTAINING_RECORD(iface, RemUnknown, IRemUnknown_iface);
619 /* construct an IRemUnknown object with one outstanding reference */
620 static HRESULT RemUnknown_Construct(IRemUnknown **ppRemUnknown)
622 RemUnknown *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
624 if (!This) return E_OUTOFMEMORY;
626 This->IRemUnknown_iface.lpVtbl = &RemUnknown_Vtbl;
627 This->refs = 1;
629 *ppRemUnknown = &This->IRemUnknown_iface;
630 return S_OK;
633 static HRESULT WINAPI RemUnknown_QueryInterface(IRemUnknown *iface, REFIID riid, void **ppv)
635 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
637 if (IsEqualIID(riid, &IID_IUnknown) ||
638 IsEqualIID(riid, &IID_IRemUnknown))
640 *ppv = iface;
641 IRemUnknown_AddRef(iface);
642 return S_OK;
645 FIXME("No interface for iid %s\n", debugstr_guid(riid));
647 *ppv = NULL;
648 return E_NOINTERFACE;
651 static ULONG WINAPI RemUnknown_AddRef(IRemUnknown *iface)
653 ULONG refs;
654 RemUnknown *This = impl_from_IRemUnknown(iface);
656 refs = InterlockedIncrement(&This->refs);
658 TRACE("%p before: %d\n", iface, refs-1);
659 return refs;
662 static ULONG WINAPI RemUnknown_Release(IRemUnknown *iface)
664 ULONG refs;
665 RemUnknown *This = impl_from_IRemUnknown(iface);
667 refs = InterlockedDecrement(&This->refs);
668 if (!refs)
669 HeapFree(GetProcessHeap(), 0, This);
671 TRACE("%p after: %d\n", iface, refs);
672 return refs;
675 static HRESULT WINAPI RemUnknown_RemQueryInterface(IRemUnknown *iface,
676 REFIPID ripid, ULONG cRefs, USHORT cIids, IID *iids /* [size_is(cIids)] */,
677 REMQIRESULT **ppQIResults /* [size_is(,cIids)] */)
679 HRESULT hr;
680 USHORT i;
681 USHORT successful_qis = 0;
682 APARTMENT *apt;
683 struct stub_manager *stubmgr;
685 TRACE("(%p)->(%s, %d, %d, %p, %p)\n", iface, debugstr_guid(ripid), cRefs, cIids, iids, ppQIResults);
687 hr = ipid_to_stub_manager(ripid, &apt, &stubmgr);
688 if (hr != S_OK) return hr;
690 *ppQIResults = CoTaskMemAlloc(sizeof(REMQIRESULT) * cIids);
692 for (i = 0; i < cIids; i++)
694 HRESULT hrobj = marshal_object(apt, &(*ppQIResults)[i].std, &iids[i],
695 stubmgr->object, MSHCTX_DIFFERENTMACHINE, NULL, MSHLFLAGS_NORMAL);
696 if (hrobj == S_OK)
697 successful_qis++;
698 (*ppQIResults)[i].hResult = hrobj;
701 stub_manager_int_release(stubmgr);
702 apartment_release(apt);
704 if (successful_qis == cIids)
705 return S_OK; /* we got all requested interfaces */
706 else if (successful_qis == 0)
707 return E_NOINTERFACE; /* we didn't get any interfaces */
708 else
709 return S_FALSE; /* we got some interfaces */
712 static HRESULT WINAPI RemUnknown_RemAddRef(IRemUnknown *iface,
713 USHORT cInterfaceRefs,
714 REMINTERFACEREF* InterfaceRefs /* [size_is(cInterfaceRefs)] */,
715 HRESULT *pResults /* [size_is(cInterfaceRefs)] */)
717 HRESULT hr = S_OK;
718 USHORT i;
720 TRACE("(%p)->(%d, %p, %p)\n", iface, cInterfaceRefs, InterfaceRefs, pResults);
722 for (i = 0; i < cInterfaceRefs; i++)
724 APARTMENT *apt;
725 struct stub_manager *stubmgr;
727 pResults[i] = ipid_to_stub_manager(&InterfaceRefs[i].ipid, &apt, &stubmgr);
728 if (pResults[i] != S_OK)
730 hr = S_FALSE;
731 continue;
734 stub_manager_ext_addref(stubmgr, InterfaceRefs[i].cPublicRefs, FALSE);
735 if (InterfaceRefs[i].cPrivateRefs)
736 FIXME("Adding %d refs securely not implemented\n", InterfaceRefs[i].cPrivateRefs);
738 stub_manager_int_release(stubmgr);
739 apartment_release(apt);
742 return hr;
745 static HRESULT WINAPI RemUnknown_RemRelease(IRemUnknown *iface,
746 USHORT cInterfaceRefs,
747 REMINTERFACEREF* InterfaceRefs /* [size_is(cInterfaceRefs)] */)
749 HRESULT hr = S_OK;
750 USHORT i;
752 TRACE("(%p)->(%d, %p)\n", iface, cInterfaceRefs, InterfaceRefs);
754 for (i = 0; i < cInterfaceRefs; i++)
756 APARTMENT *apt;
757 struct stub_manager *stubmgr;
759 hr = ipid_to_stub_manager(&InterfaceRefs[i].ipid, &apt, &stubmgr);
760 if (hr != S_OK)
762 hr = E_INVALIDARG;
763 /* FIXME: we should undo any changes already made in this function */
764 break;
767 stub_manager_ext_release(stubmgr, InterfaceRefs[i].cPublicRefs, FALSE, TRUE);
768 if (InterfaceRefs[i].cPrivateRefs)
769 FIXME("Releasing %d refs securely not implemented\n", InterfaceRefs[i].cPrivateRefs);
771 stub_manager_int_release(stubmgr);
772 apartment_release(apt);
775 return hr;
778 static const IRemUnknownVtbl RemUnknown_Vtbl =
780 RemUnknown_QueryInterface,
781 RemUnknown_AddRef,
782 RemUnknown_Release,
783 RemUnknown_RemQueryInterface,
784 RemUnknown_RemAddRef,
785 RemUnknown_RemRelease
788 /* starts the IRemUnknown listener for the current apartment */
789 HRESULT start_apartment_remote_unknown(void)
791 IRemUnknown *pRemUnknown;
792 HRESULT hr = S_OK;
793 APARTMENT *apt = COM_CurrentApt();
795 EnterCriticalSection(&apt->cs);
796 if (!apt->remunk_exported)
798 /* create the IRemUnknown object */
799 hr = RemUnknown_Construct(&pRemUnknown);
800 if (hr == S_OK)
802 STDOBJREF stdobjref; /* dummy - not used */
803 /* register it with the stub manager */
804 hr = marshal_object(apt, &stdobjref, &IID_IRemUnknown, (IUnknown *)pRemUnknown, MSHCTX_DIFFERENTMACHINE, NULL, MSHLFLAGS_NORMAL|MSHLFLAGSP_REMUNKNOWN);
805 /* release our reference to the object as the stub manager will manage the life cycle for us */
806 IRemUnknown_Release(pRemUnknown);
807 if (hr == S_OK)
808 apt->remunk_exported = TRUE;
811 LeaveCriticalSection(&apt->cs);
812 return hr;