mfreadwrite/reader: Add missing allocation check (Coverity).
[wine/zf.git] / dlls / combase / stubmanager.c
blobe9f20962e3bd48f52a679af81e8ed09659f7a7f9
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 "wine/exception.h"
41 #include "initguid.h"
42 #include "dcom.h"
43 #include "combase_private.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(ole);
47 /* generates an ipid in the following format (similar to native version):
48 * Data1 = apartment-local ipid counter
49 * Data2 = apartment creator thread ID
50 * Data3 = process ID
51 * Data4 = random value
53 static inline HRESULT generate_ipid(struct stub_manager *m, IPID *ipid)
55 HRESULT hr;
56 hr = UuidCreate(ipid);
57 if (FAILED(hr))
59 ERR("couldn't create IPID for stub manager %p\n", m);
60 UuidCreateNil(ipid);
61 return hr;
64 ipid->Data1 = InterlockedIncrement(&m->apt->ipidc);
65 ipid->Data2 = (USHORT)m->apt->tid;
66 ipid->Data3 = (USHORT)GetCurrentProcessId();
67 return S_OK;
70 /* registers a new interface stub COM object with the stub manager and returns registration record */
71 struct ifstub * stub_manager_new_ifstub(struct stub_manager *m, IRpcStubBuffer *sb, REFIID iid, DWORD dest_context,
72 void *dest_context_data, MSHLFLAGS flags)
74 struct ifstub *stub;
75 HRESULT hr;
77 TRACE("oid=%s, stubbuffer=%p, iid=%s, dest_context=%x\n", wine_dbgstr_longlong(m->oid), sb,
78 debugstr_guid(iid), dest_context);
80 stub = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct ifstub));
81 if (!stub) return NULL;
83 hr = IUnknown_QueryInterface(m->object, iid, (void **)&stub->iface);
84 if (hr != S_OK)
86 HeapFree(GetProcessHeap(), 0, stub);
87 return NULL;
90 hr = rpc_create_serverchannel(dest_context, dest_context_data, &stub->chan);
91 if (hr != S_OK)
93 IUnknown_Release(stub->iface);
94 HeapFree(GetProcessHeap(), 0, stub);
95 return NULL;
98 stub->stubbuffer = sb;
99 if (sb) IRpcStubBuffer_AddRef(sb);
101 stub->flags = flags;
102 stub->iid = *iid;
104 /* FIXME: find a cleaner way of identifying that we are creating an ifstub
105 * for the remunknown interface */
106 if (flags & MSHLFLAGSP_REMUNKNOWN)
107 stub->ipid = m->oxid_info.ipidRemUnknown;
108 else
109 generate_ipid(m, &stub->ipid);
111 EnterCriticalSection(&m->lock);
112 list_add_head(&m->ifstubs, &stub->entry);
113 /* every normal marshal is counted so we don't allow more than we should */
114 if (flags & MSHLFLAGS_NORMAL) m->norm_refs++;
115 LeaveCriticalSection(&m->lock);
117 TRACE("ifstub %p created with ipid %s\n", stub, debugstr_guid(&stub->ipid));
119 return stub;
122 static void stub_manager_delete_ifstub(struct stub_manager *m, struct ifstub *ifstub)
124 TRACE("m=%p, m->oid=%s, ipid=%s\n", m, wine_dbgstr_longlong(m->oid), debugstr_guid(&ifstub->ipid));
126 list_remove(&ifstub->entry);
128 if (!m->disconnected)
129 rpc_unregister_interface(&ifstub->iid, TRUE);
131 if (ifstub->stubbuffer) IRpcStubBuffer_Release(ifstub->stubbuffer);
132 IUnknown_Release(ifstub->iface);
133 IRpcChannelBuffer_Release(ifstub->chan);
135 HeapFree(GetProcessHeap(), 0, ifstub);
138 static struct ifstub *stub_manager_ipid_to_ifstub(struct stub_manager *m, const IPID *ipid)
140 struct ifstub *result = NULL, *ifstub;
142 EnterCriticalSection(&m->lock);
143 LIST_FOR_EACH_ENTRY(ifstub, &m->ifstubs, struct ifstub, entry)
145 if (IsEqualGUID(ipid, &ifstub->ipid))
147 result = ifstub;
148 break;
151 LeaveCriticalSection(&m->lock);
153 return result;
156 struct ifstub * stub_manager_find_ifstub(struct stub_manager *m, REFIID iid, MSHLFLAGS flags)
158 struct ifstub *result = NULL;
159 struct ifstub *ifstub;
161 EnterCriticalSection(&m->lock);
162 LIST_FOR_EACH_ENTRY( ifstub, &m->ifstubs, struct ifstub, entry )
164 if (IsEqualIID(iid, &ifstub->iid) && (ifstub->flags == flags))
166 result = ifstub;
167 break;
170 LeaveCriticalSection(&m->lock);
172 return result;
175 /* creates a new stub manager and adds it into the apartment. caller must
176 * release stub manager when it is no longer required. the apartment and
177 * external refs together take one implicit ref */
178 static struct stub_manager *new_stub_manager(struct apartment *apt, IUnknown *object)
180 struct stub_manager *sm;
181 HRESULT hres;
183 assert(apt);
185 sm = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct stub_manager));
186 if (!sm) return NULL;
188 list_init(&sm->ifstubs);
190 InitializeCriticalSection(&sm->lock);
191 sm->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": stub_manager");
193 IUnknown_AddRef(object);
194 sm->object = object;
195 sm->apt = apt;
197 /* start off with 2 references because the stub is in the apartment
198 * and the caller will also hold a reference */
199 sm->refs = 2;
200 sm->weakrefs = 0;
202 sm->oxid_info.dwPid = GetCurrentProcessId();
203 sm->oxid_info.dwTid = GetCurrentThreadId();
205 * FIXME: this is a hack for marshalling IRemUnknown. In real
206 * DCOM, the IPID of the IRemUnknown interface is generated like
207 * any other and passed to the OXID resolver which then returns it
208 * when queried. We don't have an OXID resolver yet so instead we
209 * use a magic IPID reserved for IRemUnknown.
211 sm->oxid_info.ipidRemUnknown.Data1 = 0xffffffff;
212 sm->oxid_info.ipidRemUnknown.Data2 = 0xffff;
213 sm->oxid_info.ipidRemUnknown.Data3 = 0xffff;
214 assert(sizeof(sm->oxid_info.ipidRemUnknown.Data4) == sizeof(apt->oxid));
215 memcpy(sm->oxid_info.ipidRemUnknown.Data4, &apt->oxid, sizeof(OXID));
216 sm->oxid_info.dwAuthnHint = RPC_C_AUTHN_LEVEL_NONE;
217 sm->oxid_info.psa = NULL /* FIXME */;
219 /* Yes, that's right, this starts at zero. that's zero EXTERNAL
220 * refs, i.e., nobody has unmarshalled anything yet. We can't have
221 * negative refs because the stub manager cannot be explicitly
222 * killed, it has to die by somebody unmarshalling then releasing
223 * the marshalled ifptr.
225 sm->extrefs = 0;
226 sm->disconnected = FALSE;
228 hres = IUnknown_QueryInterface(object, &IID_IExternalConnection, (void**)&sm->extern_conn);
229 if(FAILED(hres))
230 sm->extern_conn = NULL;
232 EnterCriticalSection(&apt->cs);
233 sm->oid = apt->oidc++;
234 list_add_head(&apt->stubmgrs, &sm->entry);
235 LeaveCriticalSection(&apt->cs);
237 TRACE("Created new stub manager (oid=%s) at %p for object with IUnknown %p\n", wine_dbgstr_longlong(sm->oid), sm, object);
239 return sm;
242 void stub_manager_disconnect(struct stub_manager *m)
244 struct ifstub *ifstub;
246 EnterCriticalSection(&m->lock);
247 if (!m->disconnected)
249 LIST_FOR_EACH_ENTRY(ifstub, &m->ifstubs, struct ifstub, entry)
250 rpc_unregister_interface(&ifstub->iid, FALSE);
252 m->disconnected = TRUE;
254 LeaveCriticalSection(&m->lock);
257 /* caller must remove stub manager from apartment prior to calling this function */
258 static void stub_manager_delete(struct stub_manager *m)
260 struct list *cursor;
262 TRACE("destroying %p (oid=%s)\n", m, wine_dbgstr_longlong(m->oid));
264 /* release every ifstub */
265 while ((cursor = list_head(&m->ifstubs)))
267 struct ifstub *ifstub = LIST_ENTRY(cursor, struct ifstub, entry);
268 stub_manager_delete_ifstub(m, ifstub);
271 if(m->extern_conn)
272 IExternalConnection_Release(m->extern_conn);
274 CoTaskMemFree(m->oxid_info.psa);
276 /* Some broken apps crash in object destructors. We have a test showing
277 * that on winxp+ those crashes are caught and ignored. */
278 __TRY
280 IUnknown_Release(m->object);
282 __EXCEPT_PAGE_FAULT
284 ERR("Got page fault when releasing stub!\n");
286 __ENDTRY
288 m->lock.DebugInfo->Spare[0] = 0;
289 DeleteCriticalSection(&m->lock);
291 HeapFree(GetProcessHeap(), 0, m);
294 /* increments the internal refcount */
295 static ULONG stub_manager_int_addref(struct stub_manager *m)
297 ULONG refs;
299 EnterCriticalSection(&m->apt->cs);
300 refs = ++m->refs;
301 LeaveCriticalSection(&m->apt->cs);
303 TRACE("before %d\n", refs - 1);
305 return refs;
308 /* decrements the internal refcount */
309 ULONG stub_manager_int_release(struct stub_manager *m)
311 ULONG refs;
312 struct apartment *apt = m->apt;
314 EnterCriticalSection(&apt->cs);
315 refs = --m->refs;
317 TRACE("after %d\n", refs);
319 /* remove from apartment so no other thread can access it... */
320 if (!refs)
321 list_remove(&m->entry);
323 LeaveCriticalSection(&apt->cs);
325 /* ... so now we can delete it without being inside the apartment critsec */
326 if (!refs)
327 stub_manager_delete(m);
329 return refs;
332 /* gets the stub manager associated with an object - caller must have
333 * a reference to the apartment while a reference to the stub manager is held.
334 * it must also call release on the stub manager when it is no longer needed */
335 struct stub_manager * get_stub_manager_from_object(struct apartment *apt, IUnknown *obj, BOOL alloc)
337 struct stub_manager *result = NULL, *m;
338 IUnknown *object;
339 HRESULT hres;
341 hres = IUnknown_QueryInterface(obj, &IID_IUnknown, (void**)&object);
342 if (FAILED(hres))
344 ERR("QueryInterface(IID_IUnknown failed): %08x\n", hres);
345 return NULL;
348 EnterCriticalSection(&apt->cs);
349 LIST_FOR_EACH_ENTRY(m, &apt->stubmgrs, struct stub_manager, entry)
351 if (m->object == object)
353 result = m;
354 stub_manager_int_addref(result);
355 break;
358 LeaveCriticalSection(&apt->cs);
360 if (result)
362 TRACE("found %p for object %p\n", result, object);
364 else if (alloc)
366 TRACE("not found, creating new stub manager...\n");
367 result = new_stub_manager(apt, object);
369 else
371 TRACE("not found for object %p\n", object);
374 IUnknown_Release(object);
375 return result;
378 /* gets the stub manager associated with an object id - caller must have
379 * a reference to the apartment while a reference to the stub manager is held.
380 * it must also call release on the stub manager when it is no longer needed */
381 struct stub_manager * get_stub_manager(struct apartment *apt, OID oid)
383 struct stub_manager *result = NULL, *m;
385 EnterCriticalSection(&apt->cs);
386 LIST_FOR_EACH_ENTRY(m, &apt->stubmgrs, struct stub_manager, entry)
388 if (m->oid == oid)
390 result = m;
391 stub_manager_int_addref(result);
392 break;
395 LeaveCriticalSection(&apt->cs);
397 if (result)
398 TRACE("found %p for oid %s\n", result, wine_dbgstr_longlong(oid));
399 else
400 TRACE("not found for oid %s\n", wine_dbgstr_longlong(oid));
402 return result;
405 /* add some external references (ie from a client that unmarshaled an ifptr) */
406 ULONG stub_manager_ext_addref(struct stub_manager *m, ULONG refs, BOOL tableweak)
408 BOOL first_extern_ref;
409 ULONG rc;
411 EnterCriticalSection(&m->lock);
413 first_extern_ref = refs && !m->extrefs;
415 /* make sure we don't overflow extrefs */
416 refs = min(refs, (ULONG_MAX-1 - m->extrefs));
417 rc = (m->extrefs += refs);
419 if (tableweak)
420 rc += ++m->weakrefs;
422 LeaveCriticalSection(&m->lock);
424 TRACE("added %u refs to %p (oid %s), rc is now %u\n", refs, m, wine_dbgstr_longlong(m->oid), rc);
427 * NOTE: According to tests, creating a stub causes two AddConnection calls followed by
428 * one ReleaseConnection call (with fLastReleaseCloses=FALSE).
430 if(first_extern_ref && m->extern_conn)
431 IExternalConnection_AddConnection(m->extern_conn, EXTCONN_STRONG, 0);
433 return rc;
436 /* remove some external references */
437 ULONG stub_manager_ext_release(struct stub_manager *m, ULONG refs, BOOL tableweak, BOOL last_unlock_releases)
439 BOOL last_extern_ref;
440 ULONG rc;
442 EnterCriticalSection(&m->lock);
444 /* make sure we don't underflow extrefs */
445 refs = min(refs, m->extrefs);
446 rc = (m->extrefs -= refs);
448 if (tableweak)
449 --m->weakrefs;
450 if (!last_unlock_releases)
451 rc += m->weakrefs;
453 last_extern_ref = refs && !m->extrefs;
455 LeaveCriticalSection(&m->lock);
457 TRACE("removed %u refs from %p (oid %s), rc is now %u\n", refs, m, wine_dbgstr_longlong(m->oid), rc);
459 if (last_extern_ref && m->extern_conn)
460 IExternalConnection_ReleaseConnection(m->extern_conn, EXTCONN_STRONG, 0, last_unlock_releases);
462 if (rc == 0)
463 if (!(m->extern_conn && last_unlock_releases && m->weakrefs))
464 stub_manager_int_release(m);
466 return rc;
469 /* gets the stub manager associated with an ipid - caller must have
470 * a reference to the apartment while a reference to the stub manager is held.
471 * it must also call release on the stub manager when it is no longer needed */
472 static struct stub_manager *get_stub_manager_from_ipid(struct apartment *apt, const IPID *ipid, struct ifstub **ifstub)
474 struct stub_manager *result = NULL, *m;
476 EnterCriticalSection(&apt->cs);
477 LIST_FOR_EACH_ENTRY(m, &apt->stubmgrs, struct stub_manager, entry)
479 if ((*ifstub = stub_manager_ipid_to_ifstub(m, ipid)))
481 result = m;
482 stub_manager_int_addref(result);
483 break;
486 LeaveCriticalSection(&apt->cs);
488 if (result)
489 TRACE("found %p for ipid %s\n", result, debugstr_guid(ipid));
490 else
491 ERR("not found for ipid %s\n", debugstr_guid(ipid));
493 return result;
496 static HRESULT ipid_to_ifstub(const IPID *ipid, struct apartment **stub_apt,
497 struct stub_manager **stubmgr_ret, struct ifstub **ifstub)
499 /* FIXME: hack for IRemUnknown */
500 if (ipid->Data2 == 0xffff)
501 *stub_apt = apartment_findfromoxid(*(const OXID *)ipid->Data4);
502 else
503 *stub_apt = apartment_findfromtid(ipid->Data2);
504 if (!*stub_apt)
506 TRACE("Couldn't find apartment corresponding to TID 0x%04x\n", ipid->Data2);
507 return RPC_E_INVALID_OBJECT;
509 *stubmgr_ret = get_stub_manager_from_ipid(*stub_apt, ipid, ifstub);
510 if (!*stubmgr_ret)
512 apartment_release(*stub_apt);
513 *stub_apt = NULL;
514 return RPC_E_INVALID_OBJECT;
516 return S_OK;
519 static HRESULT ipid_to_stub_manager(const IPID *ipid, struct apartment **stub_apt, struct stub_manager **stub)
521 struct ifstub *ifstub;
522 return ipid_to_ifstub(ipid, stub_apt, stub, &ifstub);
525 /* gets the apartment, stub and channel of an object. the caller must
526 * release the references to all objects (except iface) if the function
527 * returned success, otherwise no references are returned. */
528 HRESULT ipid_get_dispatch_params(const IPID *ipid, struct apartment **stub_apt,
529 struct stub_manager **manager, IRpcStubBuffer **stub, IRpcChannelBuffer **chan,
530 IID *iid, IUnknown **iface)
532 struct stub_manager *stubmgr;
533 struct ifstub *ifstub;
534 struct apartment *apt;
535 HRESULT hr;
537 hr = ipid_to_ifstub(ipid, &apt, &stubmgr, &ifstub);
538 if (hr != S_OK) return RPC_E_DISCONNECTED;
540 *stub = ifstub->stubbuffer;
541 IRpcStubBuffer_AddRef(*stub);
542 *chan = ifstub->chan;
543 IRpcChannelBuffer_AddRef(*chan);
544 *stub_apt = apt;
545 *iid = ifstub->iid;
546 *iface = ifstub->iface;
548 if (manager)
549 *manager = stubmgr;
550 else
551 stub_manager_int_release(stubmgr);
552 return S_OK;
555 /* returns TRUE if it is possible to unmarshal, FALSE otherwise. */
556 BOOL stub_manager_notify_unmarshal(struct stub_manager *m, const IPID *ipid)
558 BOOL ret = TRUE;
559 struct ifstub *ifstub;
561 if (!(ifstub = stub_manager_ipid_to_ifstub(m, ipid)))
563 ERR("attempted unmarshal of unknown IPID %s\n", debugstr_guid(ipid));
564 return FALSE;
567 EnterCriticalSection(&m->lock);
569 /* track normal marshals so we can enforce rules whilst in-process */
570 if (ifstub->flags & MSHLFLAGS_NORMAL)
572 if (m->norm_refs)
573 m->norm_refs--;
574 else
576 ERR("attempted invalid normal unmarshal, norm_refs is zero\n");
577 ret = FALSE;
581 LeaveCriticalSection(&m->lock);
583 return ret;
586 /* handles refcounting for CoReleaseMarshalData */
587 void stub_manager_release_marshal_data(struct stub_manager *m, ULONG refs, const IPID *ipid, BOOL tableweak)
589 struct ifstub *ifstub;
591 if (!(ifstub = stub_manager_ipid_to_ifstub(m, ipid)))
592 return;
594 if (ifstub->flags & MSHLFLAGS_TABLEWEAK)
595 refs = 0;
596 else if (ifstub->flags & MSHLFLAGS_TABLESTRONG)
597 refs = 1;
599 stub_manager_ext_release(m, refs, tableweak, !tableweak);
602 /* is an ifstub table marshaled? */
603 BOOL stub_manager_is_table_marshaled(struct stub_manager *m, const IPID *ipid)
605 struct ifstub *ifstub = stub_manager_ipid_to_ifstub(m, ipid);
607 assert(ifstub);
609 return ifstub->flags & (MSHLFLAGS_TABLESTRONG | MSHLFLAGS_TABLEWEAK);
612 /*****************************************************************************
614 * IRemUnknown implementation
617 * Note: this object is not related to the lifetime of a stub_manager, but it
618 * interacts with stub managers.
621 typedef struct rem_unknown
623 IRemUnknown IRemUnknown_iface;
624 LONG refs;
625 } RemUnknown;
627 static const IRemUnknownVtbl RemUnknown_Vtbl;
629 static inline RemUnknown *impl_from_IRemUnknown(IRemUnknown *iface)
631 return CONTAINING_RECORD(iface, RemUnknown, IRemUnknown_iface);
634 /* construct an IRemUnknown object with one outstanding reference */
635 static HRESULT RemUnknown_Construct(IRemUnknown **ppRemUnknown)
637 RemUnknown *object = HeapAlloc(GetProcessHeap(), 0, sizeof(*object));
639 if (!object)
640 return E_OUTOFMEMORY;
642 object->IRemUnknown_iface.lpVtbl = &RemUnknown_Vtbl;
643 object->refs = 1;
645 *ppRemUnknown = &object->IRemUnknown_iface;
646 return S_OK;
649 static HRESULT WINAPI RemUnknown_QueryInterface(IRemUnknown *iface, REFIID riid, void **ppv)
651 TRACE("%p, %s, %p\n", iface, debugstr_guid(riid), ppv);
653 if (IsEqualIID(riid, &IID_IUnknown) ||
654 IsEqualIID(riid, &IID_IRemUnknown))
656 *ppv = iface;
657 IRemUnknown_AddRef(iface);
658 return S_OK;
661 if (!IsEqualIID(riid, &IID_IExternalConnection))
662 FIXME("No interface for iid %s\n", debugstr_guid(riid));
664 *ppv = NULL;
665 return E_NOINTERFACE;
668 static ULONG WINAPI RemUnknown_AddRef(IRemUnknown *iface)
670 ULONG refs;
671 RemUnknown *remunk = impl_from_IRemUnknown(iface);
673 refs = InterlockedIncrement(&remunk->refs);
675 TRACE("%p before: %d\n", iface, refs-1);
676 return refs;
679 static ULONG WINAPI RemUnknown_Release(IRemUnknown *iface)
681 ULONG refs;
682 RemUnknown *remunk = impl_from_IRemUnknown(iface);
684 refs = InterlockedDecrement(&remunk->refs);
685 if (!refs)
686 HeapFree(GetProcessHeap(), 0, remunk);
688 TRACE("%p after: %d\n", iface, refs);
689 return refs;
692 static HRESULT WINAPI RemUnknown_RemQueryInterface(IRemUnknown *iface,
693 REFIPID ripid, ULONG cRefs, USHORT cIids, IID *iids /* [size_is(cIids)] */,
694 REMQIRESULT **ppQIResults /* [size_is(,cIids)] */)
696 HRESULT hr;
697 USHORT i;
698 USHORT successful_qis = 0;
699 struct apartment *apt;
700 struct stub_manager *stubmgr;
701 struct ifstub *ifstub;
702 DWORD dest_context;
703 void *dest_context_data;
705 TRACE("%p, %s, %d, %d, %p, %p\n", iface, debugstr_guid(ripid), cRefs, cIids, iids, ppQIResults);
707 hr = ipid_to_ifstub(ripid, &apt, &stubmgr, &ifstub);
708 if (hr != S_OK) return hr;
710 IRpcChannelBuffer_GetDestCtx(ifstub->chan, &dest_context, &dest_context_data);
712 *ppQIResults = CoTaskMemAlloc(sizeof(REMQIRESULT) * cIids);
714 for (i = 0; i < cIids; i++)
716 HRESULT hrobj = marshal_object(apt, &(*ppQIResults)[i].std, &iids[i],
717 stubmgr->object, dest_context, dest_context_data, MSHLFLAGS_NORMAL);
718 if (hrobj == S_OK)
719 successful_qis++;
720 (*ppQIResults)[i].hResult = hrobj;
723 stub_manager_int_release(stubmgr);
724 apartment_release(apt);
726 if (successful_qis == cIids)
727 return S_OK; /* we got all requested interfaces */
728 else if (successful_qis == 0)
729 return E_NOINTERFACE; /* we didn't get any interfaces */
730 else
731 return S_FALSE; /* we got some interfaces */
734 static HRESULT WINAPI RemUnknown_RemAddRef(IRemUnknown *iface,
735 USHORT cInterfaceRefs,
736 REMINTERFACEREF* InterfaceRefs /* [size_is(cInterfaceRefs)] */,
737 HRESULT *pResults /* [size_is(cInterfaceRefs)] */)
739 HRESULT hr = S_OK;
740 USHORT i;
742 TRACE("%p, %d, %p, %p\n", iface, cInterfaceRefs, InterfaceRefs, pResults);
744 for (i = 0; i < cInterfaceRefs; i++)
746 struct apartment *apt;
747 struct stub_manager *stubmgr;
749 pResults[i] = ipid_to_stub_manager(&InterfaceRefs[i].ipid, &apt, &stubmgr);
750 if (pResults[i] != S_OK)
752 hr = S_FALSE;
753 continue;
756 stub_manager_ext_addref(stubmgr, InterfaceRefs[i].cPublicRefs, FALSE);
757 if (InterfaceRefs[i].cPrivateRefs)
758 FIXME("Adding %d refs securely not implemented\n", InterfaceRefs[i].cPrivateRefs);
760 stub_manager_int_release(stubmgr);
761 apartment_release(apt);
764 return hr;
767 static HRESULT WINAPI RemUnknown_RemRelease(IRemUnknown *iface,
768 USHORT cInterfaceRefs,
769 REMINTERFACEREF* InterfaceRefs /* [size_is(cInterfaceRefs)] */)
771 HRESULT hr = S_OK;
772 USHORT i;
774 TRACE("%p, %d, %p\n", iface, cInterfaceRefs, InterfaceRefs);
776 for (i = 0; i < cInterfaceRefs; i++)
778 struct apartment *apt;
779 struct stub_manager *stubmgr;
781 hr = ipid_to_stub_manager(&InterfaceRefs[i].ipid, &apt, &stubmgr);
782 if (hr != S_OK)
784 hr = E_INVALIDARG;
785 /* FIXME: we should undo any changes already made in this function */
786 break;
789 stub_manager_ext_release(stubmgr, InterfaceRefs[i].cPublicRefs, FALSE, TRUE);
790 if (InterfaceRefs[i].cPrivateRefs)
791 FIXME("Releasing %d refs securely not implemented\n", InterfaceRefs[i].cPrivateRefs);
793 stub_manager_int_release(stubmgr);
794 apartment_release(apt);
797 return hr;
800 static const IRemUnknownVtbl RemUnknown_Vtbl =
802 RemUnknown_QueryInterface,
803 RemUnknown_AddRef,
804 RemUnknown_Release,
805 RemUnknown_RemQueryInterface,
806 RemUnknown_RemAddRef,
807 RemUnknown_RemRelease
810 /* starts the IRemUnknown listener for the current apartment */
811 HRESULT start_apartment_remote_unknown(struct apartment *apt)
813 IRemUnknown *pRemUnknown;
814 HRESULT hr = S_OK;
816 EnterCriticalSection(&apt->cs);
817 if (!apt->remunk_exported)
819 /* create the IRemUnknown object */
820 hr = RemUnknown_Construct(&pRemUnknown);
821 if (hr == S_OK)
823 STDOBJREF stdobjref; /* dummy - not used */
824 /* register it with the stub manager */
825 hr = marshal_object(apt, &stdobjref, &IID_IRemUnknown, (IUnknown *)pRemUnknown,
826 MSHCTX_DIFFERENTMACHINE, NULL, MSHLFLAGS_NORMAL|MSHLFLAGSP_REMUNKNOWN);
827 /* release our reference to the object as the stub manager will manage the life cycle for us */
828 IRemUnknown_Release(pRemUnknown);
829 if (hr == S_OK)
830 apt->remunk_exported = TRUE;
833 LeaveCriticalSection(&apt->cs);
834 return hr;