2004-12-27 20:21:47 +01:00
|
|
|
/*
|
|
|
|
* A stub manager is an object that controls interface stubs. It is
|
|
|
|
* identified by an OID (object identifier) and acts as the network
|
|
|
|
* identity of the object. There can be many stub managers in a
|
|
|
|
* process or apartment.
|
|
|
|
*
|
|
|
|
* Copyright 2002 Marcus Meissner
|
|
|
|
* Copyright 2004 Mike Hearn for CodeWeavers
|
2005-01-11 11:45:34 +01:00
|
|
|
* Copyright 2004 Robert Shearman (for CodeWeavers)
|
2004-12-27 20:21:47 +01:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2004-12-27 20:21:47 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define COBJMACROS
|
|
|
|
#define NONAMELESSUNION
|
|
|
|
#define NONAMELESSSTRUCT
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdarg.h>
|
2005-02-18 21:03:23 +01:00
|
|
|
#include <limits.h>
|
2004-12-27 20:21:47 +01:00
|
|
|
|
|
|
|
#include "windef.h"
|
|
|
|
#include "winbase.h"
|
|
|
|
#include "winuser.h"
|
|
|
|
#include "objbase.h"
|
|
|
|
#include "rpc.h"
|
2007-01-09 18:15:59 +01:00
|
|
|
|
2004-12-27 20:21:47 +01:00
|
|
|
#include "wine/debug.h"
|
|
|
|
#include "compobj_private.h"
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(ole);
|
|
|
|
|
2005-01-11 11:45:34 +01:00
|
|
|
static void stub_manager_delete_ifstub(struct stub_manager *m, struct ifstub *ifstub);
|
|
|
|
static struct ifstub *stub_manager_ipid_to_ifstub(struct stub_manager *m, const IPID *ipid);
|
|
|
|
|
|
|
|
/* creates a new stub manager and adds it into the apartment. caller must
|
|
|
|
* release stub manager when it is no longer required. the apartment and
|
|
|
|
* external refs together take one implicit ref */
|
2005-09-02 13:18:45 +02:00
|
|
|
struct stub_manager *new_stub_manager(APARTMENT *apt, IUnknown *object)
|
2004-12-27 20:21:47 +01:00
|
|
|
{
|
|
|
|
struct stub_manager *sm;
|
|
|
|
|
|
|
|
assert( apt );
|
|
|
|
|
|
|
|
sm = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct stub_manager));
|
|
|
|
if (!sm) return NULL;
|
|
|
|
|
|
|
|
list_init(&sm->ifstubs);
|
2005-03-17 11:26:20 +01:00
|
|
|
|
2004-12-27 20:21:47 +01:00
|
|
|
InitializeCriticalSection(&sm->lock);
|
2005-03-17 11:26:20 +01:00
|
|
|
DEBUG_SET_CRITSEC_NAME(&sm->lock, "stub_manager");
|
|
|
|
|
2004-12-27 20:21:47 +01:00
|
|
|
IUnknown_AddRef(object);
|
|
|
|
sm->object = object;
|
|
|
|
sm->apt = apt;
|
2005-01-11 11:45:34 +01:00
|
|
|
|
|
|
|
/* start off with 2 references because the stub is in the apartment
|
|
|
|
* and the caller will also hold a reference */
|
|
|
|
sm->refs = 2;
|
|
|
|
|
2007-03-26 19:23:16 +02:00
|
|
|
sm->oxid_info.dwPid = GetCurrentProcessId();
|
|
|
|
sm->oxid_info.dwTid = GetCurrentThreadId();
|
|
|
|
/*
|
|
|
|
* FIXME: this is a hack for marshalling IRemUnknown. In real
|
|
|
|
* DCOM, the IPID of the IRemUnknown interface is generated like
|
|
|
|
* any other and passed to the OXID resolver which then returns it
|
|
|
|
* when queried. We don't have an OXID resolver yet so instead we
|
|
|
|
* use a magic IPID reserved for IRemUnknown.
|
|
|
|
*/
|
|
|
|
sm->oxid_info.ipidRemUnknown.Data1 = 0xffffffff;
|
|
|
|
sm->oxid_info.ipidRemUnknown.Data2 = 0xffff;
|
|
|
|
sm->oxid_info.ipidRemUnknown.Data3 = 0xffff;
|
|
|
|
assert(sizeof(sm->oxid_info.ipidRemUnknown.Data4) == sizeof(apt->oxid));
|
|
|
|
memcpy(&sm->oxid_info.ipidRemUnknown.Data4, &apt->oxid, sizeof(OXID));
|
|
|
|
sm->oxid_info.dwAuthnHint = RPC_C_AUTHN_LEVEL_NONE;
|
|
|
|
sm->oxid_info.psa = NULL /* FIXME */;
|
|
|
|
|
2004-12-27 20:21:47 +01:00
|
|
|
/* yes, that's right, this starts at zero. that's zero EXTERNAL
|
|
|
|
* refs, ie nobody has unmarshalled anything yet. we can't have
|
|
|
|
* negative refs because the stub manager cannot be explicitly
|
|
|
|
* killed, it has to die by somebody unmarshalling then releasing
|
|
|
|
* the marshalled ifptr.
|
|
|
|
*/
|
2005-01-11 11:45:34 +01:00
|
|
|
sm->extrefs = 0;
|
2005-02-18 21:03:23 +01:00
|
|
|
|
2004-12-27 20:21:47 +01:00
|
|
|
EnterCriticalSection(&apt->cs);
|
2005-09-02 13:18:45 +02:00
|
|
|
sm->oid = apt->oidc++;
|
2004-12-27 20:21:47 +01:00
|
|
|
list_add_head(&apt->stubmgrs, &sm->entry);
|
|
|
|
LeaveCriticalSection(&apt->cs);
|
|
|
|
|
|
|
|
TRACE("Created new stub manager (oid=%s) at %p for object with IUnknown %p\n", wine_dbgstr_longlong(sm->oid), sm, object);
|
|
|
|
|
|
|
|
return sm;
|
|
|
|
}
|
|
|
|
|
2005-07-19 21:12:24 +02:00
|
|
|
/* caller must remove stub manager from apartment prior to calling this function */
|
2005-01-11 11:45:34 +01:00
|
|
|
static void stub_manager_delete(struct stub_manager *m)
|
|
|
|
{
|
|
|
|
struct list *cursor;
|
|
|
|
|
|
|
|
TRACE("destroying %p (oid=%s)\n", m, wine_dbgstr_longlong(m->oid));
|
|
|
|
|
2005-02-08 13:55:26 +01:00
|
|
|
/* release every ifstub */
|
2005-01-11 11:45:34 +01:00
|
|
|
while ((cursor = list_head(&m->ifstubs)))
|
|
|
|
{
|
|
|
|
struct ifstub *ifstub = LIST_ENTRY(cursor, struct ifstub, entry);
|
|
|
|
stub_manager_delete_ifstub(m, ifstub);
|
|
|
|
}
|
|
|
|
|
2007-03-26 19:23:16 +02:00
|
|
|
CoTaskMemFree(m->oxid_info.psa);
|
2005-01-11 11:45:34 +01:00
|
|
|
IUnknown_Release(m->object);
|
|
|
|
|
2005-03-17 11:26:20 +01:00
|
|
|
DEBUG_CLEAR_CRITSEC_NAME(&m->lock);
|
2005-01-11 11:45:34 +01:00
|
|
|
DeleteCriticalSection(&m->lock);
|
|
|
|
|
|
|
|
HeapFree(GetProcessHeap(), 0, m);
|
|
|
|
}
|
|
|
|
|
2005-01-14 17:48:34 +01:00
|
|
|
/* gets the stub manager associated with an object - caller must have
|
|
|
|
* a reference to the apartment while a reference to the stub manager is held.
|
|
|
|
* it must also call release on the stub manager when it is no longer needed */
|
|
|
|
struct stub_manager *get_stub_manager_from_object(APARTMENT *apt, void *object)
|
2004-12-27 20:21:47 +01:00
|
|
|
{
|
|
|
|
struct stub_manager *result = NULL;
|
|
|
|
struct list *cursor;
|
|
|
|
|
2005-01-14 17:48:34 +01:00
|
|
|
EnterCriticalSection(&apt->cs);
|
|
|
|
LIST_FOR_EACH( cursor, &apt->stubmgrs )
|
2004-12-27 20:21:47 +01:00
|
|
|
{
|
2005-01-14 17:48:34 +01:00
|
|
|
struct stub_manager *m = LIST_ENTRY( cursor, struct stub_manager, entry );
|
|
|
|
|
|
|
|
if (m->object == object)
|
|
|
|
{
|
|
|
|
result = m;
|
|
|
|
stub_manager_int_addref(result);
|
|
|
|
break;
|
|
|
|
}
|
2004-12-27 20:21:47 +01:00
|
|
|
}
|
2005-01-14 17:48:34 +01:00
|
|
|
LeaveCriticalSection(&apt->cs);
|
|
|
|
|
|
|
|
if (result)
|
|
|
|
TRACE("found %p for object %p\n", result, object);
|
|
|
|
else
|
|
|
|
TRACE("not found for object %p\n", object);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2005-02-08 17:51:22 +01:00
|
|
|
/* removes the apartment reference to an object, destroying it when no other
|
|
|
|
* threads have a reference to it */
|
2005-03-11 11:19:10 +01:00
|
|
|
void apartment_disconnectobject(struct apartment *apt, void *object)
|
2005-02-08 17:51:22 +01:00
|
|
|
{
|
|
|
|
int found = FALSE;
|
|
|
|
struct stub_manager *stubmgr;
|
|
|
|
|
|
|
|
EnterCriticalSection(&apt->cs);
|
|
|
|
LIST_FOR_EACH_ENTRY( stubmgr, &apt->stubmgrs, struct stub_manager, entry )
|
|
|
|
{
|
|
|
|
if (stubmgr->object == object)
|
|
|
|
{
|
|
|
|
found = TRUE;
|
|
|
|
stub_manager_int_release(stubmgr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LeaveCriticalSection(&apt->cs);
|
|
|
|
|
|
|
|
if (found)
|
|
|
|
TRACE("disconnect object %p\n", object);
|
|
|
|
else
|
|
|
|
WARN("couldn't find object %p\n", object);
|
|
|
|
}
|
|
|
|
|
2005-01-14 17:48:34 +01:00
|
|
|
/* gets the stub manager associated with an object id - caller must have
|
|
|
|
* a reference to the apartment while a reference to the stub manager is held.
|
|
|
|
* it must also call release on the stub manager when it is no longer needed */
|
|
|
|
struct stub_manager *get_stub_manager(APARTMENT *apt, OID oid)
|
|
|
|
{
|
|
|
|
struct stub_manager *result = NULL;
|
|
|
|
struct list *cursor;
|
2004-12-27 20:21:47 +01:00
|
|
|
|
|
|
|
EnterCriticalSection(&apt->cs);
|
|
|
|
LIST_FOR_EACH( cursor, &apt->stubmgrs )
|
|
|
|
{
|
|
|
|
struct stub_manager *m = LIST_ENTRY( cursor, struct stub_manager, entry );
|
|
|
|
|
2005-01-14 17:48:34 +01:00
|
|
|
if (m->oid == oid)
|
2004-12-27 20:21:47 +01:00
|
|
|
{
|
|
|
|
result = m;
|
2005-01-11 11:45:34 +01:00
|
|
|
stub_manager_int_addref(result);
|
2004-12-27 20:21:47 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LeaveCriticalSection(&apt->cs);
|
|
|
|
|
2005-01-14 17:48:34 +01:00
|
|
|
if (result)
|
|
|
|
TRACE("found %p for oid %s\n", result, wine_dbgstr_longlong(oid));
|
|
|
|
else
|
|
|
|
TRACE("not found for oid %s\n", wine_dbgstr_longlong(oid));
|
2004-12-27 20:21:47 +01:00
|
|
|
|
2005-01-14 17:48:34 +01:00
|
|
|
return result;
|
2004-12-27 20:21:47 +01:00
|
|
|
}
|
|
|
|
|
2005-01-11 11:45:34 +01:00
|
|
|
/* increments the internal refcount */
|
|
|
|
ULONG stub_manager_int_addref(struct stub_manager *This)
|
|
|
|
{
|
|
|
|
ULONG refs;
|
|
|
|
|
|
|
|
EnterCriticalSection(&This->apt->cs);
|
|
|
|
refs = ++This->refs;
|
|
|
|
LeaveCriticalSection(&This->apt->cs);
|
|
|
|
|
2006-10-15 12:28:08 +02:00
|
|
|
TRACE("before %d\n", refs - 1);
|
2005-01-11 11:45:34 +01:00
|
|
|
|
|
|
|
return refs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* decrements the internal refcount */
|
|
|
|
ULONG stub_manager_int_release(struct stub_manager *This)
|
|
|
|
{
|
|
|
|
ULONG refs;
|
|
|
|
APARTMENT *apt = This->apt;
|
|
|
|
|
|
|
|
EnterCriticalSection(&apt->cs);
|
|
|
|
refs = --This->refs;
|
|
|
|
|
2006-10-15 12:28:08 +02:00
|
|
|
TRACE("after %d\n", refs);
|
2005-01-11 11:45:34 +01:00
|
|
|
|
2005-07-19 21:12:24 +02:00
|
|
|
/* remove from apartment so no other thread can access it... */
|
2005-01-11 11:45:34 +01:00
|
|
|
if (!refs)
|
2005-07-19 21:12:24 +02:00
|
|
|
list_remove(&This->entry);
|
|
|
|
|
2005-01-11 11:45:34 +01:00
|
|
|
LeaveCriticalSection(&apt->cs);
|
|
|
|
|
2005-07-19 21:12:24 +02:00
|
|
|
/* ... so now we can delete it without being inside the apartment critsec */
|
|
|
|
if (!refs)
|
|
|
|
stub_manager_delete(This);
|
|
|
|
|
2005-01-11 11:45:34 +01:00
|
|
|
return refs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add some external references (ie from a client that unmarshaled an ifptr) */
|
|
|
|
ULONG stub_manager_ext_addref(struct stub_manager *m, ULONG refs)
|
2004-12-27 20:21:47 +01:00
|
|
|
{
|
2005-02-18 21:03:23 +01:00
|
|
|
ULONG rc;
|
|
|
|
|
|
|
|
EnterCriticalSection(&m->lock);
|
|
|
|
|
|
|
|
/* make sure we don't overflow extrefs */
|
|
|
|
refs = min(refs, (ULONG_MAX-1 - m->extrefs));
|
|
|
|
rc = (m->extrefs += refs);
|
2005-01-11 11:45:34 +01:00
|
|
|
|
2005-02-18 21:03:23 +01:00
|
|
|
LeaveCriticalSection(&m->lock);
|
|
|
|
|
2006-10-15 12:28:08 +02:00
|
|
|
TRACE("added %u refs to %p (oid %s), rc is now %u\n", refs, m, wine_dbgstr_longlong(m->oid), rc);
|
2005-01-11 11:45:34 +01:00
|
|
|
|
2004-12-27 20:21:47 +01:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove some external references */
|
2006-08-03 21:24:57 +02:00
|
|
|
ULONG stub_manager_ext_release(struct stub_manager *m, ULONG refs, BOOL last_unlock_releases)
|
2004-12-27 20:21:47 +01:00
|
|
|
{
|
2005-02-18 21:03:23 +01:00
|
|
|
ULONG rc;
|
|
|
|
|
|
|
|
EnterCriticalSection(&m->lock);
|
|
|
|
|
|
|
|
/* make sure we don't underflow extrefs */
|
|
|
|
refs = min(refs, m->extrefs);
|
|
|
|
rc = (m->extrefs -= refs);
|
|
|
|
|
|
|
|
LeaveCriticalSection(&m->lock);
|
2004-12-27 20:21:47 +01:00
|
|
|
|
2006-10-15 12:28:08 +02:00
|
|
|
TRACE("removed %u refs from %p (oid %s), rc is now %u\n", refs, m, wine_dbgstr_longlong(m->oid), rc);
|
2004-12-27 20:21:47 +01:00
|
|
|
|
2006-08-03 21:24:57 +02:00
|
|
|
if (rc == 0 && last_unlock_releases)
|
2005-01-11 11:45:34 +01:00
|
|
|
stub_manager_int_release(m);
|
2004-12-27 20:21:47 +01:00
|
|
|
|
2005-01-11 11:45:34 +01:00
|
|
|
return rc;
|
2004-12-27 20:21:47 +01:00
|
|
|
}
|
|
|
|
|
2005-01-11 11:45:34 +01:00
|
|
|
static struct ifstub *stub_manager_ipid_to_ifstub(struct stub_manager *m, const IPID *ipid)
|
2004-12-27 20:21:47 +01:00
|
|
|
{
|
|
|
|
struct list *cursor;
|
|
|
|
struct ifstub *result = NULL;
|
|
|
|
|
|
|
|
EnterCriticalSection(&m->lock);
|
|
|
|
LIST_FOR_EACH( cursor, &m->ifstubs )
|
|
|
|
{
|
|
|
|
struct ifstub *ifstub = LIST_ENTRY( cursor, struct ifstub, entry );
|
|
|
|
|
2005-01-07 16:33:41 +01:00
|
|
|
if (IsEqualGUID(ipid, &ifstub->ipid))
|
2004-12-27 20:21:47 +01:00
|
|
|
{
|
|
|
|
result = ifstub;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LeaveCriticalSection(&m->lock);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2005-09-02 13:18:45 +02:00
|
|
|
struct ifstub *stub_manager_find_ifstub(struct stub_manager *m, REFIID iid, MSHLFLAGS flags)
|
|
|
|
{
|
|
|
|
struct ifstub *result = NULL;
|
|
|
|
struct ifstub *ifstub;
|
|
|
|
|
|
|
|
EnterCriticalSection(&m->lock);
|
|
|
|
LIST_FOR_EACH_ENTRY( ifstub, &m->ifstubs, struct ifstub, entry )
|
|
|
|
{
|
|
|
|
if (IsEqualIID(iid, &ifstub->iid) && (ifstub->flags == flags))
|
|
|
|
{
|
|
|
|
result = ifstub;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LeaveCriticalSection(&m->lock);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2005-01-25 11:57:24 +01:00
|
|
|
/* gets the stub manager associated with an ipid - caller must have
|
|
|
|
* a reference to the apartment while a reference to the stub manager is held.
|
|
|
|
* it must also call release on the stub manager when it is no longer needed */
|
|
|
|
static struct stub_manager *get_stub_manager_from_ipid(APARTMENT *apt, const IPID *ipid)
|
2004-12-27 20:21:47 +01:00
|
|
|
{
|
2005-01-25 11:57:24 +01:00
|
|
|
struct stub_manager *result = NULL;
|
|
|
|
struct list *cursor;
|
|
|
|
|
|
|
|
EnterCriticalSection(&apt->cs);
|
|
|
|
LIST_FOR_EACH( cursor, &apt->stubmgrs )
|
|
|
|
{
|
|
|
|
struct stub_manager *m = LIST_ENTRY( cursor, struct stub_manager, entry );
|
|
|
|
|
|
|
|
if (stub_manager_ipid_to_ifstub(m, ipid))
|
|
|
|
{
|
|
|
|
result = m;
|
|
|
|
stub_manager_int_addref(result);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LeaveCriticalSection(&apt->cs);
|
|
|
|
|
|
|
|
if (result)
|
|
|
|
TRACE("found %p for ipid %s\n", result, debugstr_guid(ipid));
|
|
|
|
else
|
|
|
|
ERR("not found for ipid %s\n", debugstr_guid(ipid));
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT ipid_to_stub_manager(const IPID *ipid, APARTMENT **stub_apt, struct stub_manager **stubmgr_ret)
|
|
|
|
{
|
|
|
|
/* FIXME: hack for IRemUnknown */
|
|
|
|
if (ipid->Data2 == 0xffff)
|
2005-03-11 11:19:10 +01:00
|
|
|
*stub_apt = apartment_findfromoxid(*(OXID *)ipid->Data4, TRUE);
|
2005-01-25 11:57:24 +01:00
|
|
|
else
|
2005-03-11 11:19:10 +01:00
|
|
|
*stub_apt = apartment_findfromtid(ipid->Data2);
|
2005-01-25 11:57:24 +01:00
|
|
|
if (!*stub_apt)
|
|
|
|
{
|
2005-07-19 21:42:33 +02:00
|
|
|
TRACE("Couldn't find apartment corresponding to TID 0x%04x\n", ipid->Data2);
|
2005-01-25 11:57:24 +01:00
|
|
|
return RPC_E_INVALID_OBJECT;
|
|
|
|
}
|
|
|
|
*stubmgr_ret = get_stub_manager_from_ipid(*stub_apt, ipid);
|
|
|
|
if (!*stubmgr_ret)
|
|
|
|
{
|
2005-03-11 11:19:10 +01:00
|
|
|
apartment_release(*stub_apt);
|
2005-01-25 11:57:24 +01:00
|
|
|
*stub_apt = NULL;
|
|
|
|
return RPC_E_INVALID_OBJECT;
|
|
|
|
}
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2006-01-10 20:08:02 +01:00
|
|
|
/* gets the apartment, stub and channel of an object. the caller must
|
2006-12-19 20:37:15 +01:00
|
|
|
* release the references to all objects (except iface) if the function
|
|
|
|
* returned success, otherwise no references are returned. */
|
2006-01-10 20:08:02 +01:00
|
|
|
HRESULT ipid_get_dispatch_params(const IPID *ipid, APARTMENT **stub_apt,
|
2006-12-19 20:37:15 +01:00
|
|
|
IRpcStubBuffer **stub, IRpcChannelBuffer **chan,
|
|
|
|
IID *iid, IUnknown **iface)
|
2005-01-25 11:57:24 +01:00
|
|
|
{
|
|
|
|
struct stub_manager *stubmgr;
|
|
|
|
struct ifstub *ifstub;
|
2006-01-10 20:08:02 +01:00
|
|
|
APARTMENT *apt;
|
2005-01-25 11:57:24 +01:00
|
|
|
HRESULT hr;
|
|
|
|
|
2006-01-10 20:08:02 +01:00
|
|
|
hr = ipid_to_stub_manager(ipid, &apt, &stubmgr);
|
|
|
|
if (hr != S_OK) return RPC_E_DISCONNECTED;
|
2005-01-25 11:57:24 +01:00
|
|
|
|
|
|
|
ifstub = stub_manager_ipid_to_ifstub(stubmgr, ipid);
|
|
|
|
if (ifstub)
|
2006-01-10 20:08:02 +01:00
|
|
|
{
|
|
|
|
*stub = ifstub->stubbuffer;
|
|
|
|
IRpcStubBuffer_AddRef(*stub);
|
|
|
|
*chan = ifstub->chan;
|
|
|
|
IRpcChannelBuffer_AddRef(*chan);
|
|
|
|
*stub_apt = apt;
|
2006-12-19 20:37:15 +01:00
|
|
|
*iid = ifstub->iid;
|
|
|
|
*iface = ifstub->iface;
|
2006-01-10 20:08:02 +01:00
|
|
|
|
|
|
|
stub_manager_int_release(stubmgr);
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
stub_manager_int_release(stubmgr);
|
|
|
|
apartment_release(apt);
|
|
|
|
return RPC_E_DISCONNECTED;
|
|
|
|
}
|
2005-01-25 11:57:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* generates an ipid in the following format (similar to native version):
|
|
|
|
* Data1 = apartment-local ipid counter
|
|
|
|
* Data2 = apartment creator thread ID
|
|
|
|
* Data3 = process ID
|
|
|
|
* Data4 = random value
|
|
|
|
*/
|
|
|
|
static inline HRESULT generate_ipid(struct stub_manager *m, IPID *ipid)
|
|
|
|
{
|
|
|
|
HRESULT hr;
|
|
|
|
hr = UuidCreate(ipid);
|
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
|
|
|
ERR("couldn't create IPID for stub manager %p\n", m);
|
|
|
|
UuidCreateNil(ipid);
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
2005-01-26 21:53:06 +01:00
|
|
|
ipid->Data1 = InterlockedIncrement(&m->apt->ipidc);
|
2005-01-25 11:57:24 +01:00
|
|
|
ipid->Data2 = (USHORT)m->apt->tid;
|
|
|
|
ipid->Data3 = (USHORT)GetCurrentProcessId();
|
|
|
|
return S_OK;
|
2004-12-27 20:21:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* registers a new interface stub COM object with the stub manager and returns registration record */
|
2005-09-02 13:18:45 +02:00
|
|
|
struct ifstub *stub_manager_new_ifstub(struct stub_manager *m, IRpcStubBuffer *sb, IUnknown *iptr, REFIID iid, MSHLFLAGS flags)
|
2004-12-27 20:21:47 +01:00
|
|
|
{
|
|
|
|
struct ifstub *stub;
|
2006-01-10 20:08:02 +01:00
|
|
|
HRESULT hr;
|
2004-12-27 20:21:47 +01:00
|
|
|
|
2005-02-18 21:03:23 +01:00
|
|
|
TRACE("oid=%s, stubbuffer=%p, iptr=%p, iid=%s\n",
|
|
|
|
wine_dbgstr_longlong(m->oid), sb, iptr, debugstr_guid(iid));
|
2004-12-27 20:21:47 +01:00
|
|
|
|
|
|
|
stub = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct ifstub));
|
|
|
|
if (!stub) return NULL;
|
|
|
|
|
2006-01-10 20:08:02 +01:00
|
|
|
hr = RPC_CreateServerChannel(&stub->chan);
|
|
|
|
if (hr != S_OK)
|
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(), 0, stub);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2004-12-27 20:21:47 +01:00
|
|
|
stub->stubbuffer = sb;
|
2005-06-09 11:45:59 +02:00
|
|
|
if (sb) IRpcStubBuffer_AddRef(sb);
|
2004-12-27 20:21:47 +01:00
|
|
|
|
2005-07-19 21:59:41 +02:00
|
|
|
IUnknown_AddRef(iptr);
|
2004-12-27 20:21:47 +01:00
|
|
|
stub->iface = iptr;
|
2005-09-02 13:18:45 +02:00
|
|
|
stub->flags = flags;
|
2004-12-27 20:21:47 +01:00
|
|
|
stub->iid = *iid;
|
2005-01-25 11:57:24 +01:00
|
|
|
|
2007-03-26 19:23:16 +02:00
|
|
|
/* FIXME: find a cleaner way of identifying that we are creating an ifstub
|
|
|
|
* for the remunknown interface */
|
2005-01-25 11:57:24 +01:00
|
|
|
if (IsEqualIID(iid, &IID_IRemUnknown))
|
2007-03-26 19:23:16 +02:00
|
|
|
stub->ipid = m->oxid_info.ipidRemUnknown;
|
2005-01-25 11:57:24 +01:00
|
|
|
else
|
|
|
|
generate_ipid(m, &stub->ipid);
|
2005-01-11 11:45:34 +01:00
|
|
|
|
2004-12-27 20:21:47 +01:00
|
|
|
EnterCriticalSection(&m->lock);
|
|
|
|
list_add_head(&m->ifstubs, &stub->entry);
|
2005-09-02 13:18:45 +02:00
|
|
|
/* every normal marshal is counted so we don't allow more than we should */
|
|
|
|
if (flags & MSHLFLAGS_NORMAL) m->norm_refs++;
|
2004-12-27 20:21:47 +01:00
|
|
|
LeaveCriticalSection(&m->lock);
|
|
|
|
|
2005-01-25 11:57:24 +01:00
|
|
|
TRACE("ifstub %p created with ipid %s\n", stub, debugstr_guid(&stub->ipid));
|
|
|
|
|
2004-12-27 20:21:47 +01:00
|
|
|
return stub;
|
|
|
|
}
|
|
|
|
|
2005-01-11 11:45:34 +01:00
|
|
|
static void stub_manager_delete_ifstub(struct stub_manager *m, struct ifstub *ifstub)
|
2004-12-27 20:21:47 +01:00
|
|
|
{
|
2005-01-11 11:45:34 +01:00
|
|
|
TRACE("m=%p, m->oid=%s, ipid=%s\n", m, wine_dbgstr_longlong(m->oid), debugstr_guid(&ifstub->ipid));
|
2005-02-15 16:02:49 +01:00
|
|
|
|
2005-01-11 11:45:34 +01:00
|
|
|
list_remove(&ifstub->entry);
|
2005-02-15 16:02:49 +01:00
|
|
|
|
|
|
|
RPC_UnregisterInterface(&ifstub->iid);
|
2006-01-10 20:08:02 +01:00
|
|
|
|
2005-06-09 11:45:59 +02:00
|
|
|
if (ifstub->stubbuffer) IUnknown_Release(ifstub->stubbuffer);
|
2005-01-11 11:45:34 +01:00
|
|
|
IUnknown_Release(ifstub->iface);
|
2006-01-10 20:08:02 +01:00
|
|
|
IRpcChannelBuffer_Release(ifstub->chan);
|
2005-01-11 11:45:34 +01:00
|
|
|
|
|
|
|
HeapFree(GetProcessHeap(), 0, ifstub);
|
2004-12-27 20:21:47 +01:00
|
|
|
}
|
2005-01-17 14:39:40 +01:00
|
|
|
|
|
|
|
/* returns TRUE if it is possible to unmarshal, FALSE otherwise. */
|
2005-09-02 13:18:45 +02:00
|
|
|
BOOL stub_manager_notify_unmarshal(struct stub_manager *m, const IPID *ipid)
|
2005-01-17 14:39:40 +01:00
|
|
|
{
|
2005-09-02 13:18:45 +02:00
|
|
|
BOOL ret = TRUE;
|
|
|
|
struct ifstub *ifstub;
|
|
|
|
|
|
|
|
if (!(ifstub = stub_manager_ipid_to_ifstub(m, ipid)))
|
|
|
|
{
|
|
|
|
ERR("attempted unmarshal of unknown IPID %s\n", debugstr_guid(ipid));
|
|
|
|
return FALSE;
|
|
|
|
}
|
2005-01-17 14:39:40 +01:00
|
|
|
|
|
|
|
EnterCriticalSection(&m->lock);
|
|
|
|
|
2005-09-02 13:18:45 +02:00
|
|
|
/* track normal marshals so we can enforce rules whilst in-process */
|
|
|
|
if (ifstub->flags & MSHLFLAGS_NORMAL)
|
2005-01-17 14:39:40 +01:00
|
|
|
{
|
2005-09-02 13:18:45 +02:00
|
|
|
if (m->norm_refs)
|
|
|
|
m->norm_refs--;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ERR("attempted invalid normal unmarshal, norm_refs is zero\n");
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
2005-01-17 14:39:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
LeaveCriticalSection(&m->lock);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-09-02 13:18:45 +02:00
|
|
|
/* handles refcounting for CoReleaseMarshalData */
|
|
|
|
void stub_manager_release_marshal_data(struct stub_manager *m, ULONG refs, const IPID *ipid)
|
2005-01-17 14:39:40 +01:00
|
|
|
{
|
2005-09-02 13:18:45 +02:00
|
|
|
struct ifstub *ifstub;
|
|
|
|
|
|
|
|
if (!(ifstub = stub_manager_ipid_to_ifstub(m, ipid)))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (ifstub->flags & MSHLFLAGS_TABLEWEAK)
|
|
|
|
refs = 0;
|
2006-03-01 13:18:33 +01:00
|
|
|
else if (ifstub->flags & MSHLFLAGS_TABLESTRONG)
|
2005-02-18 21:03:23 +01:00
|
|
|
refs = 1;
|
2005-07-19 21:12:24 +02:00
|
|
|
|
2006-08-03 21:24:57 +02:00
|
|
|
stub_manager_ext_release(m, refs, TRUE);
|
2005-02-18 21:03:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* is an ifstub table marshaled? */
|
2005-09-02 13:18:45 +02:00
|
|
|
BOOL stub_manager_is_table_marshaled(struct stub_manager *m, const IPID *ipid)
|
2005-02-18 21:03:23 +01:00
|
|
|
{
|
2005-09-02 13:18:45 +02:00
|
|
|
struct ifstub *ifstub = stub_manager_ipid_to_ifstub(m, ipid);
|
|
|
|
|
|
|
|
assert( ifstub );
|
|
|
|
|
|
|
|
return ifstub->flags & (MSHLFLAGS_TABLESTRONG | MSHLFLAGS_TABLEWEAK);
|
2005-01-17 14:39:40 +01:00
|
|
|
}
|
2005-01-25 11:57:24 +01:00
|
|
|
|
2005-01-26 21:42:30 +01:00
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* IRemUnknown implementation
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Note: this object is not related to the lifetime of a stub_manager, but it
|
|
|
|
* interacts with stub managers.
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct rem_unknown
|
|
|
|
{
|
|
|
|
const IRemUnknownVtbl *lpVtbl;
|
2005-07-05 13:02:54 +02:00
|
|
|
LONG refs;
|
2005-01-26 21:42:30 +01:00
|
|
|
} RemUnknown;
|
|
|
|
|
|
|
|
static const IRemUnknownVtbl RemUnknown_Vtbl;
|
|
|
|
|
|
|
|
|
|
|
|
/* construct an IRemUnknown object with one outstanding reference */
|
|
|
|
static HRESULT RemUnknown_Construct(IRemUnknown **ppRemUnknown)
|
|
|
|
{
|
|
|
|
RemUnknown *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
|
|
|
|
|
|
|
|
if (!This) return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
This->lpVtbl = &RemUnknown_Vtbl;
|
|
|
|
This->refs = 1;
|
|
|
|
|
|
|
|
*ppRemUnknown = (IRemUnknown *)This;
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT WINAPI RemUnknown_QueryInterface(IRemUnknown *iface, REFIID riid, void **ppv)
|
|
|
|
{
|
|
|
|
TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
|
|
|
|
|
|
|
|
if (IsEqualIID(riid, &IID_IUnknown) ||
|
|
|
|
IsEqualIID(riid, &IID_IRemUnknown))
|
|
|
|
{
|
|
|
|
*ppv = (LPVOID)iface;
|
|
|
|
IRemUnknown_AddRef(iface);
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
FIXME("No interface for iid %s\n", debugstr_guid(riid));
|
|
|
|
|
|
|
|
*ppv = NULL;
|
|
|
|
return E_NOINTERFACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ULONG WINAPI RemUnknown_AddRef(IRemUnknown *iface)
|
|
|
|
{
|
|
|
|
ULONG refs;
|
|
|
|
RemUnknown *This = (RemUnknown *)iface;
|
|
|
|
|
|
|
|
refs = InterlockedIncrement(&This->refs);
|
|
|
|
|
2006-10-15 12:28:08 +02:00
|
|
|
TRACE("%p before: %d\n", iface, refs-1);
|
2005-01-26 21:42:30 +01:00
|
|
|
return refs;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ULONG WINAPI RemUnknown_Release(IRemUnknown *iface)
|
|
|
|
{
|
|
|
|
ULONG refs;
|
|
|
|
RemUnknown *This = (RemUnknown *)iface;
|
|
|
|
|
|
|
|
refs = InterlockedDecrement(&This->refs);
|
|
|
|
if (!refs)
|
|
|
|
HeapFree(GetProcessHeap(), 0, This);
|
|
|
|
|
2006-10-15 12:28:08 +02:00
|
|
|
TRACE("%p after: %d\n", iface, refs);
|
2005-01-26 21:42:30 +01:00
|
|
|
return refs;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT WINAPI RemUnknown_RemQueryInterface(IRemUnknown *iface,
|
|
|
|
REFIPID ripid, ULONG cRefs, USHORT cIids, IID *iids /* [size_is(cIids)] */,
|
|
|
|
REMQIRESULT **ppQIResults /* [size_is(,cIids)] */)
|
|
|
|
{
|
|
|
|
HRESULT hr;
|
|
|
|
USHORT i;
|
2005-01-26 22:07:05 +01:00
|
|
|
USHORT successful_qis = 0;
|
2005-01-26 21:42:30 +01:00
|
|
|
APARTMENT *apt;
|
|
|
|
struct stub_manager *stubmgr;
|
|
|
|
|
2006-10-15 12:28:08 +02:00
|
|
|
TRACE("(%p)->(%s, %d, %d, %p, %p)\n", iface, debugstr_guid(ripid), cRefs, cIids, iids, ppQIResults);
|
2005-01-26 21:42:30 +01:00
|
|
|
|
|
|
|
hr = ipid_to_stub_manager(ripid, &apt, &stubmgr);
|
|
|
|
if (hr != S_OK) return hr;
|
|
|
|
|
|
|
|
*ppQIResults = CoTaskMemAlloc(sizeof(REMQIRESULT) * cIids);
|
|
|
|
|
|
|
|
for (i = 0; i < cIids; i++)
|
|
|
|
{
|
2005-03-11 11:19:10 +01:00
|
|
|
HRESULT hrobj = marshal_object(apt, &(*ppQIResults)[i].std, &iids[i],
|
|
|
|
stubmgr->object, MSHLFLAGS_NORMAL);
|
2005-01-26 22:07:05 +01:00
|
|
|
if (hrobj == S_OK)
|
|
|
|
successful_qis++;
|
|
|
|
(*ppQIResults)[i].hResult = hrobj;
|
2005-01-26 21:42:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
stub_manager_int_release(stubmgr);
|
2005-03-11 11:19:10 +01:00
|
|
|
apartment_release(apt);
|
2005-01-26 21:42:30 +01:00
|
|
|
|
2005-01-26 22:07:05 +01:00
|
|
|
if (successful_qis == cIids)
|
|
|
|
return S_OK; /* we got all requested interfaces */
|
|
|
|
else if (successful_qis == 0)
|
|
|
|
return E_NOINTERFACE; /* we didn't get any interfaces */
|
|
|
|
else
|
|
|
|
return S_FALSE; /* we got some interfaces */
|
2005-01-26 21:42:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT WINAPI RemUnknown_RemAddRef(IRemUnknown *iface,
|
|
|
|
USHORT cInterfaceRefs,
|
|
|
|
REMINTERFACEREF* InterfaceRefs /* [size_is(cInterfaceRefs)] */,
|
|
|
|
HRESULT *pResults /* [size_is(cInterfaceRefs)] */)
|
|
|
|
{
|
|
|
|
HRESULT hr = S_OK;
|
|
|
|
USHORT i;
|
|
|
|
|
|
|
|
TRACE("(%p)->(%d, %p, %p)\n", iface, cInterfaceRefs, InterfaceRefs, pResults);
|
|
|
|
|
|
|
|
for (i = 0; i < cInterfaceRefs; i++)
|
|
|
|
{
|
|
|
|
APARTMENT *apt;
|
|
|
|
struct stub_manager *stubmgr;
|
|
|
|
|
|
|
|
pResults[i] = ipid_to_stub_manager(&InterfaceRefs[i].ipid, &apt, &stubmgr);
|
|
|
|
if (pResults[i] != S_OK)
|
|
|
|
{
|
|
|
|
hr = S_FALSE;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
stub_manager_ext_addref(stubmgr, InterfaceRefs[i].cPublicRefs);
|
|
|
|
if (InterfaceRefs[i].cPrivateRefs)
|
|
|
|
FIXME("Adding %ld refs securely not implemented\n", InterfaceRefs[i].cPrivateRefs);
|
|
|
|
|
|
|
|
stub_manager_int_release(stubmgr);
|
2005-03-11 11:19:10 +01:00
|
|
|
apartment_release(apt);
|
2005-01-26 21:42:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT WINAPI RemUnknown_RemRelease(IRemUnknown *iface,
|
|
|
|
USHORT cInterfaceRefs,
|
|
|
|
REMINTERFACEREF* InterfaceRefs /* [size_is(cInterfaceRefs)] */)
|
|
|
|
{
|
|
|
|
HRESULT hr = S_OK;
|
|
|
|
USHORT i;
|
|
|
|
|
|
|
|
TRACE("(%p)->(%d, %p)\n", iface, cInterfaceRefs, InterfaceRefs);
|
|
|
|
|
|
|
|
for (i = 0; i < cInterfaceRefs; i++)
|
|
|
|
{
|
|
|
|
APARTMENT *apt;
|
|
|
|
struct stub_manager *stubmgr;
|
|
|
|
|
|
|
|
hr = ipid_to_stub_manager(&InterfaceRefs[i].ipid, &apt, &stubmgr);
|
|
|
|
if (hr != S_OK)
|
|
|
|
{
|
|
|
|
hr = E_INVALIDARG;
|
|
|
|
/* FIXME: we should undo any changes already made in this function */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2006-08-03 21:24:57 +02:00
|
|
|
stub_manager_ext_release(stubmgr, InterfaceRefs[i].cPublicRefs, TRUE);
|
2005-01-26 21:42:30 +01:00
|
|
|
if (InterfaceRefs[i].cPrivateRefs)
|
|
|
|
FIXME("Releasing %ld refs securely not implemented\n", InterfaceRefs[i].cPrivateRefs);
|
|
|
|
|
|
|
|
stub_manager_int_release(stubmgr);
|
2005-03-11 11:19:10 +01:00
|
|
|
apartment_release(apt);
|
2005-01-26 21:42:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const IRemUnknownVtbl RemUnknown_Vtbl =
|
|
|
|
{
|
|
|
|
RemUnknown_QueryInterface,
|
|
|
|
RemUnknown_AddRef,
|
|
|
|
RemUnknown_Release,
|
|
|
|
RemUnknown_RemQueryInterface,
|
|
|
|
RemUnknown_RemAddRef,
|
|
|
|
RemUnknown_RemRelease
|
|
|
|
};
|
|
|
|
|
|
|
|
/* starts the IRemUnknown listener for the current apartment */
|
2006-08-28 15:18:15 +02:00
|
|
|
HRESULT start_apartment_remote_unknown(void)
|
2005-01-26 21:42:30 +01:00
|
|
|
{
|
|
|
|
IRemUnknown *pRemUnknown;
|
|
|
|
HRESULT hr = S_OK;
|
|
|
|
APARTMENT *apt = COM_CurrentApt();
|
|
|
|
|
|
|
|
EnterCriticalSection(&apt->cs);
|
|
|
|
if (!apt->remunk_exported)
|
|
|
|
{
|
|
|
|
/* create the IRemUnknown object */
|
|
|
|
hr = RemUnknown_Construct(&pRemUnknown);
|
|
|
|
if (hr == S_OK)
|
|
|
|
{
|
|
|
|
STDOBJREF stdobjref; /* dummy - not used */
|
|
|
|
/* register it with the stub manager */
|
2005-03-11 11:19:10 +01:00
|
|
|
hr = marshal_object(apt, &stdobjref, &IID_IRemUnknown, (IUnknown *)pRemUnknown, MSHLFLAGS_NORMAL);
|
2005-01-26 21:42:30 +01:00
|
|
|
/* release our reference to the object as the stub manager will manage the life cycle for us */
|
|
|
|
IRemUnknown_Release(pRemUnknown);
|
|
|
|
if (hr == S_OK)
|
|
|
|
apt->remunk_exported = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LeaveCriticalSection(&apt->cs);
|
|
|
|
return hr;
|
|
|
|
}
|