riched20: Use ME_PointFromChar to calculate the caret position.
[wine] / dlls / ole32 / stubmanager.c
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.
6  *
7  * Copyright 2002 Marcus Meissner
8  * Copyright 2004 Mike Hearn for CodeWeavers
9  * Copyright 2004 Robert Shearman (for CodeWeavers)
10  *
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.
15  *
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.
20  *
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
24  */
25
26 #define COBJMACROS
27 #define NONAMELESSUNION
28 #define NONAMELESSSTRUCT
29
30 #include <assert.h>
31 #include <stdarg.h>
32 #include <limits.h>
33
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winuser.h"
37 #include "objbase.h"
38 #include "rpc.h"
39
40 #include "wine/debug.h"
41 #include "compobj_private.h"
42
43 WINE_DEFAULT_DEBUG_CHANNEL(ole);
44
45
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
51  */
52 static inline HRESULT generate_ipid(struct stub_manager *m, IPID *ipid)
53 {
54     HRESULT hr;
55     hr = UuidCreate(ipid);
56     if (FAILED(hr))
57     {
58         ERR("couldn't create IPID for stub manager %p\n", m);
59         UuidCreateNil(ipid);
60         return hr;
61     }
62
63     ipid->Data1 = InterlockedIncrement(&m->apt->ipidc);
64     ipid->Data2 = (USHORT)m->apt->tid;
65     ipid->Data3 = (USHORT)GetCurrentProcessId();
66     return S_OK;
67 }
68
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)
72 {
73     struct ifstub *stub;
74     HRESULT hr;
75
76     TRACE("oid=%s, stubbuffer=%p, iptr=%p, iid=%s\n",
77           wine_dbgstr_longlong(m->oid), sb, iptr, debugstr_guid(iid));
78
79     stub = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct ifstub));
80     if (!stub) return NULL;
81
82     hr = RPC_CreateServerChannel(dest_context, dest_context_data, &stub->chan);
83     if (hr != S_OK)
84     {
85         HeapFree(GetProcessHeap(), 0, stub);
86         return NULL;
87     }
88
89     stub->stubbuffer = sb;
90     if (sb) IRpcStubBuffer_AddRef(sb);
91
92     IUnknown_AddRef(iptr);
93     stub->iface = iptr;
94     stub->flags = flags;
95     stub->iid = *iid;
96
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);
103
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);
109
110     TRACE("ifstub %p created with ipid %s\n", stub, debugstr_guid(&stub->ipid));
111
112     return stub;
113 }
114
115 static void stub_manager_delete_ifstub(struct stub_manager *m, struct ifstub *ifstub)
116 {
117     TRACE("m=%p, m->oid=%s, ipid=%s\n", m, wine_dbgstr_longlong(m->oid), debugstr_guid(&ifstub->ipid));
118
119     list_remove(&ifstub->entry);
120
121     RPC_UnregisterInterface(&ifstub->iid);
122
123     if (ifstub->stubbuffer) IRpcStubBuffer_Release(ifstub->stubbuffer);
124     IUnknown_Release(ifstub->iface);
125     IRpcChannelBuffer_Release(ifstub->chan);
126
127     HeapFree(GetProcessHeap(), 0, ifstub);
128 }
129
130 static struct ifstub *stub_manager_ipid_to_ifstub(struct stub_manager *m, const IPID *ipid)
131 {
132     struct list    *cursor;
133     struct ifstub  *result = NULL;
134
135     EnterCriticalSection(&m->lock);
136     LIST_FOR_EACH( cursor, &m->ifstubs )
137     {
138         struct ifstub *ifstub = LIST_ENTRY( cursor, struct ifstub, entry );
139
140         if (IsEqualGUID(ipid, &ifstub->ipid))
141         {
142             result = ifstub;
143             break;
144         }
145     }
146     LeaveCriticalSection(&m->lock);
147
148     return result;
149 }
150
151 struct ifstub *stub_manager_find_ifstub(struct stub_manager *m, REFIID iid, MSHLFLAGS flags)
152 {
153     struct ifstub  *result = NULL;
154     struct ifstub  *ifstub;
155
156     EnterCriticalSection(&m->lock);
157     LIST_FOR_EACH_ENTRY( ifstub, &m->ifstubs, struct ifstub, entry )
158     {
159         if (IsEqualIID(iid, &ifstub->iid) && (ifstub->flags == flags))
160         {
161             result = ifstub;
162             break;
163         }
164     }
165     LeaveCriticalSection(&m->lock);
166
167     return result;
168 }
169
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)
174 {
175     struct stub_manager *sm;
176
177     assert( apt );
178     
179     sm = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct stub_manager));
180     if (!sm) return NULL;
181
182     list_init(&sm->ifstubs);
183
184     InitializeCriticalSection(&sm->lock);
185     DEBUG_SET_CRITSEC_NAME(&sm->lock, "stub_manager");
186
187     IUnknown_AddRef(object);
188     sm->object = object;
189     sm->apt    = apt;
190
191     /* start off with 2 references because the stub is in the apartment
192      * and the caller will also hold a reference */
193     sm->refs   = 2;
194     sm->weakrefs = 0;
195
196     sm->oxid_info.dwPid = GetCurrentProcessId();
197     sm->oxid_info.dwTid = GetCurrentThreadId();
198     /*
199      * FIXME: this is a hack for marshalling IRemUnknown. In real
200      * DCOM, the IPID of the IRemUnknown interface is generated like
201      * any other and passed to the OXID resolver which then returns it
202      * when queried. We don't have an OXID resolver yet so instead we
203      * use a magic IPID reserved for IRemUnknown.
204      */
205     sm->oxid_info.ipidRemUnknown.Data1 = 0xffffffff;
206     sm->oxid_info.ipidRemUnknown.Data2 = 0xffff;
207     sm->oxid_info.ipidRemUnknown.Data3 = 0xffff;
208     assert(sizeof(sm->oxid_info.ipidRemUnknown.Data4) == sizeof(apt->oxid));
209     memcpy(sm->oxid_info.ipidRemUnknown.Data4, &apt->oxid, sizeof(OXID));
210     sm->oxid_info.dwAuthnHint = RPC_C_AUTHN_LEVEL_NONE;
211     sm->oxid_info.psa = NULL /* FIXME */;
212
213     /* Yes, that's right, this starts at zero. that's zero EXTERNAL
214      * refs, i.e., nobody has unmarshalled anything yet. We can't have
215      * negative refs because the stub manager cannot be explicitly
216      * killed, it has to die by somebody unmarshalling then releasing
217      * the marshalled ifptr.
218      */
219     sm->extrefs = 0;
220
221     EnterCriticalSection(&apt->cs);
222     sm->oid = apt->oidc++;
223     list_add_head(&apt->stubmgrs, &sm->entry);
224     LeaveCriticalSection(&apt->cs);
225
226     TRACE("Created new stub manager (oid=%s) at %p for object with IUnknown %p\n", wine_dbgstr_longlong(sm->oid), sm, object);
227     
228     return sm;
229 }
230
231 /* caller must remove stub manager from apartment prior to calling this function */
232 static void stub_manager_delete(struct stub_manager *m)
233 {
234     struct list *cursor;
235
236     TRACE("destroying %p (oid=%s)\n", m, wine_dbgstr_longlong(m->oid));
237
238     /* release every ifstub */
239     while ((cursor = list_head(&m->ifstubs)))
240     {
241         struct ifstub *ifstub = LIST_ENTRY(cursor, struct ifstub, entry);
242         stub_manager_delete_ifstub(m, ifstub);
243     }
244
245     CoTaskMemFree(m->oxid_info.psa);
246     IUnknown_Release(m->object);
247
248     DEBUG_CLEAR_CRITSEC_NAME(&m->lock);
249     DeleteCriticalSection(&m->lock);
250
251     HeapFree(GetProcessHeap(), 0, m);
252 }
253
254 /* increments the internal refcount */
255 static ULONG stub_manager_int_addref(struct stub_manager *This)
256 {
257     ULONG refs;
258
259     EnterCriticalSection(&This->apt->cs);
260     refs = ++This->refs;
261     LeaveCriticalSection(&This->apt->cs);
262
263     TRACE("before %d\n", refs - 1);
264
265     return refs;
266 }
267
268 /* decrements the internal refcount */
269 ULONG stub_manager_int_release(struct stub_manager *This)
270 {
271     ULONG refs;
272     APARTMENT *apt = This->apt;
273
274     EnterCriticalSection(&apt->cs);
275     refs = --This->refs;
276
277     TRACE("after %d\n", refs);
278
279     /* remove from apartment so no other thread can access it... */
280     if (!refs)
281         list_remove(&This->entry);
282
283     LeaveCriticalSection(&apt->cs);
284
285     /* ... so now we can delete it without being inside the apartment critsec */
286     if (!refs)
287         stub_manager_delete(This);
288
289     return refs;
290 }
291
292 /* gets the stub manager associated with an object - caller must have
293  * a reference to the apartment while a reference to the stub manager is held.
294  * it must also call release on the stub manager when it is no longer needed */
295 struct stub_manager *get_stub_manager_from_object(APARTMENT *apt, void *object)
296 {
297     struct stub_manager *result = NULL;
298     struct list         *cursor;
299
300     EnterCriticalSection(&apt->cs);
301     LIST_FOR_EACH( cursor, &apt->stubmgrs )
302     {
303         struct stub_manager *m = LIST_ENTRY( cursor, struct stub_manager, entry );
304
305         if (m->object == object)
306         {
307             result = m;
308             stub_manager_int_addref(result);
309             break;
310         }
311     }
312     LeaveCriticalSection(&apt->cs);
313
314     if (result)
315         TRACE("found %p for object %p\n", result, object);
316     else
317         TRACE("not found for object %p\n", object);
318
319     return result;    
320 }
321
322 /* removes the apartment reference to an object, destroying it when no other
323  * threads have a reference to it */
324 void apartment_disconnectobject(struct apartment *apt, void *object)
325 {
326     int found = FALSE;
327     struct stub_manager *stubmgr;
328
329     EnterCriticalSection(&apt->cs);
330     LIST_FOR_EACH_ENTRY( stubmgr, &apt->stubmgrs, struct stub_manager, entry )
331     {
332         if (stubmgr->object == object)
333         {
334             found = TRUE;
335             stub_manager_int_release(stubmgr);
336             break;
337         }
338     }
339     LeaveCriticalSection(&apt->cs);
340
341     if (found)
342         TRACE("disconnect object %p\n", object);
343     else
344         WARN("couldn't find object %p\n", object);
345 }
346
347 /* gets the stub manager associated with an object id - caller must have
348  * a reference to the apartment while a reference to the stub manager is held.
349  * it must also call release on the stub manager when it is no longer needed */
350 struct stub_manager *get_stub_manager(APARTMENT *apt, OID oid)
351 {
352     struct stub_manager *result = NULL;
353     struct list         *cursor;
354
355     EnterCriticalSection(&apt->cs);
356     LIST_FOR_EACH( cursor, &apt->stubmgrs )
357     {
358         struct stub_manager *m = LIST_ENTRY( cursor, struct stub_manager, entry );
359
360         if (m->oid == oid)
361         {
362             result = m;
363             stub_manager_int_addref(result);
364             break;
365         }
366     }
367     LeaveCriticalSection(&apt->cs);
368
369     if (result)
370         TRACE("found %p for oid %s\n", result, wine_dbgstr_longlong(oid));
371     else
372         TRACE("not found for oid %s\n", wine_dbgstr_longlong(oid));
373
374     return result;
375 }
376
377 /* add some external references (ie from a client that unmarshaled an ifptr) */
378 ULONG stub_manager_ext_addref(struct stub_manager *m, ULONG refs, BOOL tableweak)
379 {
380     ULONG rc;
381
382     EnterCriticalSection(&m->lock);
383
384     /* make sure we don't overflow extrefs */
385     refs = min(refs, (ULONG_MAX-1 - m->extrefs));
386     rc = (m->extrefs += refs);
387
388     if (tableweak)
389         rc += ++m->weakrefs;
390
391     LeaveCriticalSection(&m->lock);
392     
393     TRACE("added %u refs to %p (oid %s), rc is now %u\n", refs, m, wine_dbgstr_longlong(m->oid), rc);
394
395     return rc;
396 }
397
398 /* remove some external references */
399 ULONG stub_manager_ext_release(struct stub_manager *m, ULONG refs, BOOL tableweak, BOOL last_unlock_releases)
400 {
401     ULONG rc;
402
403     EnterCriticalSection(&m->lock);
404
405     /* make sure we don't underflow extrefs */
406     refs = min(refs, m->extrefs);
407     rc = (m->extrefs -= refs);
408
409     if (tableweak)
410         --m->weakrefs;
411     if (!last_unlock_releases)
412         rc += m->weakrefs;
413
414     LeaveCriticalSection(&m->lock);
415     
416     TRACE("removed %u refs from %p (oid %s), rc is now %u\n", refs, m, wine_dbgstr_longlong(m->oid), rc);
417
418     if (rc == 0)
419         stub_manager_int_release(m);
420
421     return rc;
422 }
423
424 /* gets the stub manager associated with an ipid - caller must have
425  * a reference to the apartment while a reference to the stub manager is held.
426  * it must also call release on the stub manager when it is no longer needed */
427 static struct stub_manager *get_stub_manager_from_ipid(APARTMENT *apt, const IPID *ipid)
428 {
429     struct stub_manager *result = NULL;
430     struct list         *cursor;
431
432     EnterCriticalSection(&apt->cs);
433     LIST_FOR_EACH( cursor, &apt->stubmgrs )
434     {
435         struct stub_manager *m = LIST_ENTRY( cursor, struct stub_manager, entry );
436
437         if (stub_manager_ipid_to_ifstub(m, ipid))
438         {
439             result = m;
440             stub_manager_int_addref(result);
441             break;
442         }
443     }
444     LeaveCriticalSection(&apt->cs);
445
446     if (result)
447         TRACE("found %p for ipid %s\n", result, debugstr_guid(ipid));
448     else
449         ERR("not found for ipid %s\n", debugstr_guid(ipid));
450
451     return result;
452 }
453
454 static HRESULT ipid_to_stub_manager(const IPID *ipid, APARTMENT **stub_apt, struct stub_manager **stubmgr_ret)
455 {
456     /* FIXME: hack for IRemUnknown */
457     if (ipid->Data2 == 0xffff)
458         *stub_apt = apartment_findfromoxid(*(const OXID *)ipid->Data4, TRUE);
459     else
460         *stub_apt = apartment_findfromtid(ipid->Data2);
461     if (!*stub_apt)
462     {
463         TRACE("Couldn't find apartment corresponding to TID 0x%04x\n", ipid->Data2);
464         return RPC_E_INVALID_OBJECT;
465     }
466     *stubmgr_ret = get_stub_manager_from_ipid(*stub_apt, ipid);
467     if (!*stubmgr_ret)
468     {
469         apartment_release(*stub_apt);
470         *stub_apt = NULL;
471         return RPC_E_INVALID_OBJECT;
472     }
473     return S_OK;
474 }
475
476 /* gets the apartment, stub and channel of an object. the caller must
477  * release the references to all objects (except iface) if the function
478  * returned success, otherwise no references are returned. */
479 HRESULT ipid_get_dispatch_params(const IPID *ipid, APARTMENT **stub_apt,
480                                  IRpcStubBuffer **stub, IRpcChannelBuffer **chan,
481                                  IID *iid, IUnknown **iface)
482 {
483     struct stub_manager *stubmgr;
484     struct ifstub *ifstub;
485     APARTMENT *apt;
486     HRESULT hr;
487
488     hr = ipid_to_stub_manager(ipid, &apt, &stubmgr);
489     if (hr != S_OK) return RPC_E_DISCONNECTED;
490
491     ifstub = stub_manager_ipid_to_ifstub(stubmgr, ipid);
492     if (ifstub)
493     {
494         *stub = ifstub->stubbuffer;
495         IRpcStubBuffer_AddRef(*stub);
496         *chan = ifstub->chan;
497         IRpcChannelBuffer_AddRef(*chan);
498         *stub_apt = apt;
499         *iid = ifstub->iid;
500         *iface = ifstub->iface;
501
502         stub_manager_int_release(stubmgr);
503         return S_OK;
504     }
505     else
506     {
507         stub_manager_int_release(stubmgr);
508         apartment_release(apt);
509         return RPC_E_DISCONNECTED;
510     }
511 }
512
513 /* returns TRUE if it is possible to unmarshal, FALSE otherwise. */
514 BOOL stub_manager_notify_unmarshal(struct stub_manager *m, const IPID *ipid)
515 {
516     BOOL ret = TRUE;
517     struct ifstub *ifstub;
518
519     if (!(ifstub = stub_manager_ipid_to_ifstub(m, ipid)))
520     {
521         ERR("attempted unmarshal of unknown IPID %s\n", debugstr_guid(ipid));
522         return FALSE;
523     }
524
525     EnterCriticalSection(&m->lock);
526
527     /* track normal marshals so we can enforce rules whilst in-process */
528     if (ifstub->flags & MSHLFLAGS_NORMAL)
529     {
530         if (m->norm_refs)
531             m->norm_refs--;
532         else
533         {
534             ERR("attempted invalid normal unmarshal, norm_refs is zero\n");
535             ret = FALSE;
536         }
537     }
538
539     LeaveCriticalSection(&m->lock);
540
541     return ret;
542 }
543
544 /* handles refcounting for CoReleaseMarshalData */
545 void stub_manager_release_marshal_data(struct stub_manager *m, ULONG refs, const IPID *ipid, BOOL tableweak)
546 {
547     struct ifstub *ifstub;
548  
549     if (!(ifstub = stub_manager_ipid_to_ifstub(m, ipid)))
550         return;
551  
552     if (ifstub->flags & MSHLFLAGS_TABLEWEAK)
553         refs = 0;
554     else if (ifstub->flags & MSHLFLAGS_TABLESTRONG)
555         refs = 1;
556
557     stub_manager_ext_release(m, refs, tableweak, FALSE);
558 }
559
560 /* is an ifstub table marshaled? */
561 BOOL stub_manager_is_table_marshaled(struct stub_manager *m, const IPID *ipid)
562 {
563     struct ifstub *ifstub = stub_manager_ipid_to_ifstub(m, ipid);
564  
565     assert( ifstub );
566     
567     return ifstub->flags & (MSHLFLAGS_TABLESTRONG | MSHLFLAGS_TABLEWEAK);
568 }
569
570
571 /*****************************************************************************
572  *
573  * IRemUnknown implementation
574  *
575  *
576  * Note: this object is not related to the lifetime of a stub_manager, but it
577  * interacts with stub managers.
578  */
579
580 typedef struct rem_unknown
581 {
582     IRemUnknown IRemUnknown_iface;
583     LONG refs;
584 } RemUnknown;
585
586 static const IRemUnknownVtbl RemUnknown_Vtbl;
587
588 static inline RemUnknown *impl_from_IRemUnknown(IRemUnknown *iface)
589 {
590     return CONTAINING_RECORD(iface, RemUnknown, IRemUnknown_iface);
591 }
592
593
594 /* construct an IRemUnknown object with one outstanding reference */
595 static HRESULT RemUnknown_Construct(IRemUnknown **ppRemUnknown)
596 {
597     RemUnknown *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
598
599     if (!This) return E_OUTOFMEMORY;
600
601     This->IRemUnknown_iface.lpVtbl = &RemUnknown_Vtbl;
602     This->refs = 1;
603
604     *ppRemUnknown = &This->IRemUnknown_iface;
605     return S_OK;
606 }
607
608 static HRESULT WINAPI RemUnknown_QueryInterface(IRemUnknown *iface, REFIID riid, void **ppv)
609 {
610     TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
611
612     if (IsEqualIID(riid, &IID_IUnknown) ||
613         IsEqualIID(riid, &IID_IRemUnknown))
614     {
615         *ppv = iface;
616         IRemUnknown_AddRef(iface);
617         return S_OK;
618     }
619
620     FIXME("No interface for iid %s\n", debugstr_guid(riid));
621
622     *ppv = NULL;
623     return E_NOINTERFACE;
624 }
625
626 static ULONG WINAPI RemUnknown_AddRef(IRemUnknown *iface)
627 {
628     ULONG refs;
629     RemUnknown *This = impl_from_IRemUnknown(iface);
630
631     refs = InterlockedIncrement(&This->refs);
632
633     TRACE("%p before: %d\n", iface, refs-1);
634     return refs;
635 }
636
637 static ULONG WINAPI RemUnknown_Release(IRemUnknown *iface)
638 {
639     ULONG refs;
640     RemUnknown *This = impl_from_IRemUnknown(iface);
641
642     refs = InterlockedDecrement(&This->refs);
643     if (!refs)
644         HeapFree(GetProcessHeap(), 0, This);
645
646     TRACE("%p after: %d\n", iface, refs);
647     return refs;
648 }
649
650 static HRESULT WINAPI RemUnknown_RemQueryInterface(IRemUnknown *iface,
651     REFIPID ripid, ULONG cRefs, USHORT cIids, IID *iids /* [size_is(cIids)] */,
652     REMQIRESULT **ppQIResults /* [size_is(,cIids)] */)
653 {
654     HRESULT hr;
655     USHORT i;
656     USHORT successful_qis = 0;
657     APARTMENT *apt;
658     struct stub_manager *stubmgr;
659
660     TRACE("(%p)->(%s, %d, %d, %p, %p)\n", iface, debugstr_guid(ripid), cRefs, cIids, iids, ppQIResults);
661
662     hr = ipid_to_stub_manager(ripid, &apt, &stubmgr);
663     if (hr != S_OK) return hr;
664
665     *ppQIResults = CoTaskMemAlloc(sizeof(REMQIRESULT) * cIids);
666
667     for (i = 0; i < cIids; i++)
668     {
669         HRESULT hrobj = marshal_object(apt, &(*ppQIResults)[i].std, &iids[i],
670                                        stubmgr->object, MSHCTX_DIFFERENTMACHINE, NULL, MSHLFLAGS_NORMAL);
671         if (hrobj == S_OK)
672             successful_qis++;
673         (*ppQIResults)[i].hResult = hrobj;
674     }
675
676     stub_manager_int_release(stubmgr);
677     apartment_release(apt);
678
679     if (successful_qis == cIids)
680         return S_OK; /* we got all requested interfaces */
681     else if (successful_qis == 0)
682         return E_NOINTERFACE; /* we didn't get any interfaces */
683     else
684         return S_FALSE; /* we got some interfaces */
685 }
686
687 static HRESULT WINAPI RemUnknown_RemAddRef(IRemUnknown *iface,
688     USHORT cInterfaceRefs,
689     REMINTERFACEREF* InterfaceRefs /* [size_is(cInterfaceRefs)] */,
690     HRESULT *pResults /* [size_is(cInterfaceRefs)] */)
691 {
692     HRESULT hr = S_OK;
693     USHORT i;
694
695     TRACE("(%p)->(%d, %p, %p)\n", iface, cInterfaceRefs, InterfaceRefs, pResults);
696
697     for (i = 0; i < cInterfaceRefs; i++)
698     {
699         APARTMENT *apt;
700         struct stub_manager *stubmgr;
701
702         pResults[i] = ipid_to_stub_manager(&InterfaceRefs[i].ipid, &apt, &stubmgr);
703         if (pResults[i] != S_OK)
704         {
705             hr = S_FALSE;
706             continue;
707         }
708
709         stub_manager_ext_addref(stubmgr, InterfaceRefs[i].cPublicRefs, FALSE);
710         if (InterfaceRefs[i].cPrivateRefs)
711             FIXME("Adding %d refs securely not implemented\n", InterfaceRefs[i].cPrivateRefs);
712
713         stub_manager_int_release(stubmgr);
714         apartment_release(apt);
715     }
716
717     return hr;
718 }
719
720 static HRESULT WINAPI RemUnknown_RemRelease(IRemUnknown *iface,
721     USHORT cInterfaceRefs,
722     REMINTERFACEREF* InterfaceRefs /* [size_is(cInterfaceRefs)] */)
723 {
724     HRESULT hr = S_OK;
725     USHORT i;
726
727     TRACE("(%p)->(%d, %p)\n", iface, cInterfaceRefs, InterfaceRefs);
728
729     for (i = 0; i < cInterfaceRefs; i++)
730     {
731         APARTMENT *apt;
732         struct stub_manager *stubmgr;
733
734         hr = ipid_to_stub_manager(&InterfaceRefs[i].ipid, &apt, &stubmgr);
735         if (hr != S_OK)
736         {
737             hr = E_INVALIDARG;
738             /* FIXME: we should undo any changes already made in this function */
739             break;
740         }
741
742         stub_manager_ext_release(stubmgr, InterfaceRefs[i].cPublicRefs, FALSE, TRUE);
743         if (InterfaceRefs[i].cPrivateRefs)
744             FIXME("Releasing %d refs securely not implemented\n", InterfaceRefs[i].cPrivateRefs);
745
746         stub_manager_int_release(stubmgr);
747         apartment_release(apt);
748     }
749
750     return hr;
751 }
752
753 static const IRemUnknownVtbl RemUnknown_Vtbl =
754 {
755     RemUnknown_QueryInterface,
756     RemUnknown_AddRef,
757     RemUnknown_Release,
758     RemUnknown_RemQueryInterface,
759     RemUnknown_RemAddRef,
760     RemUnknown_RemRelease
761 };
762
763 /* starts the IRemUnknown listener for the current apartment */
764 HRESULT start_apartment_remote_unknown(void)
765 {
766     IRemUnknown *pRemUnknown;
767     HRESULT hr = S_OK;
768     APARTMENT *apt = COM_CurrentApt();
769
770     EnterCriticalSection(&apt->cs);
771     if (!apt->remunk_exported)
772     {
773         /* create the IRemUnknown object */
774         hr = RemUnknown_Construct(&pRemUnknown);
775         if (hr == S_OK)
776         {
777             STDOBJREF stdobjref; /* dummy - not used */
778             /* register it with the stub manager */
779             hr = marshal_object(apt, &stdobjref, &IID_IRemUnknown, (IUnknown *)pRemUnknown, MSHCTX_DIFFERENTMACHINE, NULL, MSHLFLAGS_NORMAL|MSHLFLAGSP_REMUNKNOWN);
780             /* release our reference to the object as the stub manager will manage the life cycle for us */
781             IRemUnknown_Release(pRemUnknown);
782             if (hr == S_OK)
783                 apt->remunk_exported = TRUE;
784         }
785     }
786     LeaveCriticalSection(&apt->cs);
787     return hr;
788 }