ole32: Pass in the IID and IUnknown* of the object being executed to the server notif...
[wine/wine-kai.git] / dlls / ole32 / stubmanager.c
blob264a2e562142361763d16600c549d8102a0dac7d
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 "ole2.h"
39 #include "ole2ver.h"
40 #include "rpc.h"
41 #include "wine/debug.h"
42 #include "compobj_private.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(ole);
46 static void stub_manager_delete_ifstub(struct stub_manager *m, struct ifstub *ifstub);
47 static struct ifstub *stub_manager_ipid_to_ifstub(struct stub_manager *m, const IPID *ipid);
49 /* creates a new stub manager and adds it into the apartment. caller must
50 * release stub manager when it is no longer required. the apartment and
51 * external refs together take one implicit ref */
52 struct stub_manager *new_stub_manager(APARTMENT *apt, IUnknown *object)
54 struct stub_manager *sm;
56 assert( apt );
58 sm = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct stub_manager));
59 if (!sm) return NULL;
61 list_init(&sm->ifstubs);
63 InitializeCriticalSection(&sm->lock);
64 DEBUG_SET_CRITSEC_NAME(&sm->lock, "stub_manager");
66 IUnknown_AddRef(object);
67 sm->object = object;
68 sm->apt = apt;
70 /* start off with 2 references because the stub is in the apartment
71 * and the caller will also hold a reference */
72 sm->refs = 2;
74 /* yes, that's right, this starts at zero. that's zero EXTERNAL
75 * refs, ie nobody has unmarshalled anything yet. we can't have
76 * negative refs because the stub manager cannot be explicitly
77 * killed, it has to die by somebody unmarshalling then releasing
78 * the marshalled ifptr.
80 sm->extrefs = 0;
82 EnterCriticalSection(&apt->cs);
83 sm->oid = apt->oidc++;
84 list_add_head(&apt->stubmgrs, &sm->entry);
85 LeaveCriticalSection(&apt->cs);
87 TRACE("Created new stub manager (oid=%s) at %p for object with IUnknown %p\n", wine_dbgstr_longlong(sm->oid), sm, object);
89 return sm;
92 /* caller must remove stub manager from apartment prior to calling this function */
93 static void stub_manager_delete(struct stub_manager *m)
95 struct list *cursor;
97 TRACE("destroying %p (oid=%s)\n", m, wine_dbgstr_longlong(m->oid));
99 /* release every ifstub */
100 while ((cursor = list_head(&m->ifstubs)))
102 struct ifstub *ifstub = LIST_ENTRY(cursor, struct ifstub, entry);
103 stub_manager_delete_ifstub(m, ifstub);
106 IUnknown_Release(m->object);
108 DEBUG_CLEAR_CRITSEC_NAME(&m->lock);
109 DeleteCriticalSection(&m->lock);
111 HeapFree(GetProcessHeap(), 0, m);
114 /* gets the stub manager associated with an object - caller must have
115 * a reference to the apartment while a reference to the stub manager is held.
116 * it must also call release on the stub manager when it is no longer needed */
117 struct stub_manager *get_stub_manager_from_object(APARTMENT *apt, void *object)
119 struct stub_manager *result = NULL;
120 struct list *cursor;
122 EnterCriticalSection(&apt->cs);
123 LIST_FOR_EACH( cursor, &apt->stubmgrs )
125 struct stub_manager *m = LIST_ENTRY( cursor, struct stub_manager, entry );
127 if (m->object == object)
129 result = m;
130 stub_manager_int_addref(result);
131 break;
134 LeaveCriticalSection(&apt->cs);
136 if (result)
137 TRACE("found %p for object %p\n", result, object);
138 else
139 TRACE("not found for object %p\n", object);
141 return result;
144 /* removes the apartment reference to an object, destroying it when no other
145 * threads have a reference to it */
146 void apartment_disconnectobject(struct apartment *apt, void *object)
148 int found = FALSE;
149 struct stub_manager *stubmgr;
151 EnterCriticalSection(&apt->cs);
152 LIST_FOR_EACH_ENTRY( stubmgr, &apt->stubmgrs, struct stub_manager, entry )
154 if (stubmgr->object == object)
156 found = TRUE;
157 stub_manager_int_release(stubmgr);
158 break;
161 LeaveCriticalSection(&apt->cs);
163 if (found)
164 TRACE("disconnect object %p\n", object);
165 else
166 WARN("couldn't find object %p\n", object);
169 /* gets the stub manager associated with an object id - caller must have
170 * a reference to the apartment while a reference to the stub manager is held.
171 * it must also call release on the stub manager when it is no longer needed */
172 struct stub_manager *get_stub_manager(APARTMENT *apt, OID oid)
174 struct stub_manager *result = NULL;
175 struct list *cursor;
177 EnterCriticalSection(&apt->cs);
178 LIST_FOR_EACH( cursor, &apt->stubmgrs )
180 struct stub_manager *m = LIST_ENTRY( cursor, struct stub_manager, entry );
182 if (m->oid == oid)
184 result = m;
185 stub_manager_int_addref(result);
186 break;
189 LeaveCriticalSection(&apt->cs);
191 if (result)
192 TRACE("found %p for oid %s\n", result, wine_dbgstr_longlong(oid));
193 else
194 TRACE("not found for oid %s\n", wine_dbgstr_longlong(oid));
196 return result;
199 /* increments the internal refcount */
200 ULONG stub_manager_int_addref(struct stub_manager *This)
202 ULONG refs;
204 EnterCriticalSection(&This->apt->cs);
205 refs = ++This->refs;
206 LeaveCriticalSection(&This->apt->cs);
208 TRACE("before %d\n", refs - 1);
210 return refs;
213 /* decrements the internal refcount */
214 ULONG stub_manager_int_release(struct stub_manager *This)
216 ULONG refs;
217 APARTMENT *apt = This->apt;
219 EnterCriticalSection(&apt->cs);
220 refs = --This->refs;
222 TRACE("after %d\n", refs);
224 /* remove from apartment so no other thread can access it... */
225 if (!refs)
226 list_remove(&This->entry);
228 LeaveCriticalSection(&apt->cs);
230 /* ... so now we can delete it without being inside the apartment critsec */
231 if (!refs)
232 stub_manager_delete(This);
234 return refs;
237 /* add some external references (ie from a client that unmarshaled an ifptr) */
238 ULONG stub_manager_ext_addref(struct stub_manager *m, ULONG refs)
240 ULONG rc;
242 EnterCriticalSection(&m->lock);
244 /* make sure we don't overflow extrefs */
245 refs = min(refs, (ULONG_MAX-1 - m->extrefs));
246 rc = (m->extrefs += refs);
248 LeaveCriticalSection(&m->lock);
250 TRACE("added %u refs to %p (oid %s), rc is now %u\n", refs, m, wine_dbgstr_longlong(m->oid), rc);
252 return rc;
255 /* remove some external references */
256 ULONG stub_manager_ext_release(struct stub_manager *m, ULONG refs, BOOL last_unlock_releases)
258 ULONG rc;
260 EnterCriticalSection(&m->lock);
262 /* make sure we don't underflow extrefs */
263 refs = min(refs, m->extrefs);
264 rc = (m->extrefs -= refs);
266 LeaveCriticalSection(&m->lock);
268 TRACE("removed %u refs from %p (oid %s), rc is now %u\n", refs, m, wine_dbgstr_longlong(m->oid), rc);
270 if (rc == 0 && last_unlock_releases)
271 stub_manager_int_release(m);
273 return rc;
276 static struct ifstub *stub_manager_ipid_to_ifstub(struct stub_manager *m, const IPID *ipid)
278 struct list *cursor;
279 struct ifstub *result = NULL;
281 EnterCriticalSection(&m->lock);
282 LIST_FOR_EACH( cursor, &m->ifstubs )
284 struct ifstub *ifstub = LIST_ENTRY( cursor, struct ifstub, entry );
286 if (IsEqualGUID(ipid, &ifstub->ipid))
288 result = ifstub;
289 break;
292 LeaveCriticalSection(&m->lock);
294 return result;
297 struct ifstub *stub_manager_find_ifstub(struct stub_manager *m, REFIID iid, MSHLFLAGS flags)
299 struct ifstub *result = NULL;
300 struct ifstub *ifstub;
302 EnterCriticalSection(&m->lock);
303 LIST_FOR_EACH_ENTRY( ifstub, &m->ifstubs, struct ifstub, entry )
305 if (IsEqualIID(iid, &ifstub->iid) && (ifstub->flags == flags))
307 result = ifstub;
308 break;
311 LeaveCriticalSection(&m->lock);
313 return result;
316 /* gets the stub manager associated with an ipid - caller must have
317 * a reference to the apartment while a reference to the stub manager is held.
318 * it must also call release on the stub manager when it is no longer needed */
319 static struct stub_manager *get_stub_manager_from_ipid(APARTMENT *apt, const IPID *ipid)
321 struct stub_manager *result = NULL;
322 struct list *cursor;
324 EnterCriticalSection(&apt->cs);
325 LIST_FOR_EACH( cursor, &apt->stubmgrs )
327 struct stub_manager *m = LIST_ENTRY( cursor, struct stub_manager, entry );
329 if (stub_manager_ipid_to_ifstub(m, ipid))
331 result = m;
332 stub_manager_int_addref(result);
333 break;
336 LeaveCriticalSection(&apt->cs);
338 if (result)
339 TRACE("found %p for ipid %s\n", result, debugstr_guid(ipid));
340 else
341 ERR("not found for ipid %s\n", debugstr_guid(ipid));
343 return result;
346 HRESULT ipid_to_stub_manager(const IPID *ipid, APARTMENT **stub_apt, struct stub_manager **stubmgr_ret)
348 /* FIXME: hack for IRemUnknown */
349 if (ipid->Data2 == 0xffff)
350 *stub_apt = apartment_findfromoxid(*(OXID *)ipid->Data4, TRUE);
351 else
352 *stub_apt = apartment_findfromtid(ipid->Data2);
353 if (!*stub_apt)
355 TRACE("Couldn't find apartment corresponding to TID 0x%04x\n", ipid->Data2);
356 return RPC_E_INVALID_OBJECT;
358 *stubmgr_ret = get_stub_manager_from_ipid(*stub_apt, ipid);
359 if (!*stubmgr_ret)
361 apartment_release(*stub_apt);
362 *stub_apt = NULL;
363 return RPC_E_INVALID_OBJECT;
365 return S_OK;
368 /* gets the apartment, stub and channel of an object. the caller must
369 * release the references to all objects (except iface) if the function
370 * returned success, otherwise no references are returned. */
371 HRESULT ipid_get_dispatch_params(const IPID *ipid, APARTMENT **stub_apt,
372 IRpcStubBuffer **stub, IRpcChannelBuffer **chan,
373 IID *iid, IUnknown **iface)
375 struct stub_manager *stubmgr;
376 struct ifstub *ifstub;
377 APARTMENT *apt;
378 HRESULT hr;
380 hr = ipid_to_stub_manager(ipid, &apt, &stubmgr);
381 if (hr != S_OK) return RPC_E_DISCONNECTED;
383 ifstub = stub_manager_ipid_to_ifstub(stubmgr, ipid);
384 if (ifstub)
386 *stub = ifstub->stubbuffer;
387 IRpcStubBuffer_AddRef(*stub);
388 *chan = ifstub->chan;
389 IRpcChannelBuffer_AddRef(*chan);
390 *stub_apt = apt;
391 *iid = ifstub->iid;
392 *iface = ifstub->iface;
394 stub_manager_int_release(stubmgr);
395 return S_OK;
397 else
399 stub_manager_int_release(stubmgr);
400 apartment_release(apt);
401 return RPC_E_DISCONNECTED;
405 /* generates an ipid in the following format (similar to native version):
406 * Data1 = apartment-local ipid counter
407 * Data2 = apartment creator thread ID
408 * Data3 = process ID
409 * Data4 = random value
411 static inline HRESULT generate_ipid(struct stub_manager *m, IPID *ipid)
413 HRESULT hr;
414 hr = UuidCreate(ipid);
415 if (FAILED(hr))
417 ERR("couldn't create IPID for stub manager %p\n", m);
418 UuidCreateNil(ipid);
419 return hr;
422 ipid->Data1 = InterlockedIncrement(&m->apt->ipidc);
423 ipid->Data2 = (USHORT)m->apt->tid;
424 ipid->Data3 = (USHORT)GetCurrentProcessId();
425 return S_OK;
428 /* registers a new interface stub COM object with the stub manager and returns registration record */
429 struct ifstub *stub_manager_new_ifstub(struct stub_manager *m, IRpcStubBuffer *sb, IUnknown *iptr, REFIID iid, MSHLFLAGS flags)
431 struct ifstub *stub;
432 HRESULT hr;
434 TRACE("oid=%s, stubbuffer=%p, iptr=%p, iid=%s\n",
435 wine_dbgstr_longlong(m->oid), sb, iptr, debugstr_guid(iid));
437 stub = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct ifstub));
438 if (!stub) return NULL;
440 hr = RPC_CreateServerChannel(&stub->chan);
441 if (hr != S_OK)
443 HeapFree(GetProcessHeap(), 0, stub);
444 return NULL;
447 stub->stubbuffer = sb;
448 if (sb) IRpcStubBuffer_AddRef(sb);
450 IUnknown_AddRef(iptr);
451 stub->iface = iptr;
452 stub->flags = flags;
453 stub->iid = *iid;
456 * FIXME: this is a hack for marshalling IRemUnknown. In real
457 * DCOM, the IPID of the IRemUnknown interface is generated like
458 * any other and passed to the OXID resolver which then returns it
459 * when queried. We don't have an OXID resolver yet so instead we
460 * use a magic IPID reserved for IRemUnknown.
462 if (IsEqualIID(iid, &IID_IRemUnknown))
464 stub->ipid.Data1 = 0xffffffff;
465 stub->ipid.Data2 = 0xffff;
466 stub->ipid.Data3 = 0xffff;
467 assert(sizeof(stub->ipid.Data4) == sizeof(m->apt->oxid));
468 memcpy(&stub->ipid.Data4, &m->apt->oxid, sizeof(OXID));
470 else
471 generate_ipid(m, &stub->ipid);
473 EnterCriticalSection(&m->lock);
474 list_add_head(&m->ifstubs, &stub->entry);
475 /* every normal marshal is counted so we don't allow more than we should */
476 if (flags & MSHLFLAGS_NORMAL) m->norm_refs++;
477 LeaveCriticalSection(&m->lock);
479 TRACE("ifstub %p created with ipid %s\n", stub, debugstr_guid(&stub->ipid));
481 return stub;
484 static void stub_manager_delete_ifstub(struct stub_manager *m, struct ifstub *ifstub)
486 TRACE("m=%p, m->oid=%s, ipid=%s\n", m, wine_dbgstr_longlong(m->oid), debugstr_guid(&ifstub->ipid));
488 list_remove(&ifstub->entry);
490 RPC_UnregisterInterface(&ifstub->iid);
492 if (ifstub->stubbuffer) IUnknown_Release(ifstub->stubbuffer);
493 IUnknown_Release(ifstub->iface);
494 IRpcChannelBuffer_Release(ifstub->chan);
496 HeapFree(GetProcessHeap(), 0, ifstub);
499 /* returns TRUE if it is possible to unmarshal, FALSE otherwise. */
500 BOOL stub_manager_notify_unmarshal(struct stub_manager *m, const IPID *ipid)
502 BOOL ret = TRUE;
503 struct ifstub *ifstub;
505 if (!(ifstub = stub_manager_ipid_to_ifstub(m, ipid)))
507 ERR("attempted unmarshal of unknown IPID %s\n", debugstr_guid(ipid));
508 return FALSE;
511 EnterCriticalSection(&m->lock);
513 /* track normal marshals so we can enforce rules whilst in-process */
514 if (ifstub->flags & MSHLFLAGS_NORMAL)
516 if (m->norm_refs)
517 m->norm_refs--;
518 else
520 ERR("attempted invalid normal unmarshal, norm_refs is zero\n");
521 ret = FALSE;
525 LeaveCriticalSection(&m->lock);
527 return ret;
530 /* handles refcounting for CoReleaseMarshalData */
531 void stub_manager_release_marshal_data(struct stub_manager *m, ULONG refs, const IPID *ipid)
533 struct ifstub *ifstub;
535 if (!(ifstub = stub_manager_ipid_to_ifstub(m, ipid)))
536 return;
538 if (ifstub->flags & MSHLFLAGS_TABLEWEAK)
539 refs = 0;
540 else if (ifstub->flags & MSHLFLAGS_TABLESTRONG)
541 refs = 1;
543 stub_manager_ext_release(m, refs, TRUE);
546 /* is an ifstub table marshaled? */
547 BOOL stub_manager_is_table_marshaled(struct stub_manager *m, const IPID *ipid)
549 struct ifstub *ifstub = stub_manager_ipid_to_ifstub(m, ipid);
551 assert( ifstub );
553 return ifstub->flags & (MSHLFLAGS_TABLESTRONG | MSHLFLAGS_TABLEWEAK);
557 /*****************************************************************************
559 * IRemUnknown implementation
562 * Note: this object is not related to the lifetime of a stub_manager, but it
563 * interacts with stub managers.
566 typedef struct rem_unknown
568 const IRemUnknownVtbl *lpVtbl;
569 LONG refs;
570 } RemUnknown;
572 static const IRemUnknownVtbl RemUnknown_Vtbl;
575 /* construct an IRemUnknown object with one outstanding reference */
576 static HRESULT RemUnknown_Construct(IRemUnknown **ppRemUnknown)
578 RemUnknown *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
580 if (!This) return E_OUTOFMEMORY;
582 This->lpVtbl = &RemUnknown_Vtbl;
583 This->refs = 1;
585 *ppRemUnknown = (IRemUnknown *)This;
586 return S_OK;
589 static HRESULT WINAPI RemUnknown_QueryInterface(IRemUnknown *iface, REFIID riid, void **ppv)
591 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
593 if (IsEqualIID(riid, &IID_IUnknown) ||
594 IsEqualIID(riid, &IID_IRemUnknown))
596 *ppv = (LPVOID)iface;
597 IRemUnknown_AddRef(iface);
598 return S_OK;
601 FIXME("No interface for iid %s\n", debugstr_guid(riid));
603 *ppv = NULL;
604 return E_NOINTERFACE;
607 static ULONG WINAPI RemUnknown_AddRef(IRemUnknown *iface)
609 ULONG refs;
610 RemUnknown *This = (RemUnknown *)iface;
612 refs = InterlockedIncrement(&This->refs);
614 TRACE("%p before: %d\n", iface, refs-1);
615 return refs;
618 static ULONG WINAPI RemUnknown_Release(IRemUnknown *iface)
620 ULONG refs;
621 RemUnknown *This = (RemUnknown *)iface;
623 refs = InterlockedDecrement(&This->refs);
624 if (!refs)
625 HeapFree(GetProcessHeap(), 0, This);
627 TRACE("%p after: %d\n", iface, refs);
628 return refs;
631 static HRESULT WINAPI RemUnknown_RemQueryInterface(IRemUnknown *iface,
632 REFIPID ripid, ULONG cRefs, USHORT cIids, IID *iids /* [size_is(cIids)] */,
633 REMQIRESULT **ppQIResults /* [size_is(,cIids)] */)
635 HRESULT hr;
636 USHORT i;
637 USHORT successful_qis = 0;
638 APARTMENT *apt;
639 struct stub_manager *stubmgr;
641 TRACE("(%p)->(%s, %d, %d, %p, %p)\n", iface, debugstr_guid(ripid), cRefs, cIids, iids, ppQIResults);
643 hr = ipid_to_stub_manager(ripid, &apt, &stubmgr);
644 if (hr != S_OK) return hr;
646 *ppQIResults = CoTaskMemAlloc(sizeof(REMQIRESULT) * cIids);
648 for (i = 0; i < cIids; i++)
650 HRESULT hrobj = marshal_object(apt, &(*ppQIResults)[i].std, &iids[i],
651 stubmgr->object, MSHLFLAGS_NORMAL);
652 if (hrobj == S_OK)
653 successful_qis++;
654 (*ppQIResults)[i].hResult = hrobj;
657 stub_manager_int_release(stubmgr);
658 apartment_release(apt);
660 if (successful_qis == cIids)
661 return S_OK; /* we got all requested interfaces */
662 else if (successful_qis == 0)
663 return E_NOINTERFACE; /* we didn't get any interfaces */
664 else
665 return S_FALSE; /* we got some interfaces */
668 static HRESULT WINAPI RemUnknown_RemAddRef(IRemUnknown *iface,
669 USHORT cInterfaceRefs,
670 REMINTERFACEREF* InterfaceRefs /* [size_is(cInterfaceRefs)] */,
671 HRESULT *pResults /* [size_is(cInterfaceRefs)] */)
673 HRESULT hr = S_OK;
674 USHORT i;
676 TRACE("(%p)->(%d, %p, %p)\n", iface, cInterfaceRefs, InterfaceRefs, pResults);
678 for (i = 0; i < cInterfaceRefs; i++)
680 APARTMENT *apt;
681 struct stub_manager *stubmgr;
683 pResults[i] = ipid_to_stub_manager(&InterfaceRefs[i].ipid, &apt, &stubmgr);
684 if (pResults[i] != S_OK)
686 hr = S_FALSE;
687 continue;
690 stub_manager_ext_addref(stubmgr, InterfaceRefs[i].cPublicRefs);
691 if (InterfaceRefs[i].cPrivateRefs)
692 FIXME("Adding %ld refs securely not implemented\n", InterfaceRefs[i].cPrivateRefs);
694 stub_manager_int_release(stubmgr);
695 apartment_release(apt);
698 return hr;
701 static HRESULT WINAPI RemUnknown_RemRelease(IRemUnknown *iface,
702 USHORT cInterfaceRefs,
703 REMINTERFACEREF* InterfaceRefs /* [size_is(cInterfaceRefs)] */)
705 HRESULT hr = S_OK;
706 USHORT i;
708 TRACE("(%p)->(%d, %p)\n", iface, cInterfaceRefs, InterfaceRefs);
710 for (i = 0; i < cInterfaceRefs; i++)
712 APARTMENT *apt;
713 struct stub_manager *stubmgr;
715 hr = ipid_to_stub_manager(&InterfaceRefs[i].ipid, &apt, &stubmgr);
716 if (hr != S_OK)
718 hr = E_INVALIDARG;
719 /* FIXME: we should undo any changes already made in this function */
720 break;
723 stub_manager_ext_release(stubmgr, InterfaceRefs[i].cPublicRefs, TRUE);
724 if (InterfaceRefs[i].cPrivateRefs)
725 FIXME("Releasing %ld refs securely not implemented\n", InterfaceRefs[i].cPrivateRefs);
727 stub_manager_int_release(stubmgr);
728 apartment_release(apt);
731 return hr;
734 static const IRemUnknownVtbl RemUnknown_Vtbl =
736 RemUnknown_QueryInterface,
737 RemUnknown_AddRef,
738 RemUnknown_Release,
739 RemUnknown_RemQueryInterface,
740 RemUnknown_RemAddRef,
741 RemUnknown_RemRelease
744 /* starts the IRemUnknown listener for the current apartment */
745 HRESULT start_apartment_remote_unknown(void)
747 IRemUnknown *pRemUnknown;
748 HRESULT hr = S_OK;
749 APARTMENT *apt = COM_CurrentApt();
751 EnterCriticalSection(&apt->cs);
752 if (!apt->remunk_exported)
754 /* create the IRemUnknown object */
755 hr = RemUnknown_Construct(&pRemUnknown);
756 if (hr == S_OK)
758 STDOBJREF stdobjref; /* dummy - not used */
759 /* register it with the stub manager */
760 hr = marshal_object(apt, &stdobjref, &IID_IRemUnknown, (IUnknown *)pRemUnknown, MSHLFLAGS_NORMAL);
761 /* release our reference to the object as the stub manager will manage the life cycle for us */
762 IRemUnknown_Release(pRemUnknown);
763 if (hr == S_OK)
764 apt->remunk_exported = TRUE;
767 LeaveCriticalSection(&apt->cs);
768 return hr;