📄 stubmanager.c
字号:
IRpcStubBuffer **stub, IRpcChannelBuffer **chan,
IID *iid, IUnknown **iface)
{
struct stub_manager *stubmgr;
struct ifstub *ifstub;
APARTMENT *apt;
HRESULT hr;
hr = ipid_to_stub_manager(ipid, &apt, &stubmgr);
if (hr != S_OK) return RPC_E_DISCONNECTED;
ifstub = stub_manager_ipid_to_ifstub(stubmgr, ipid);
if (ifstub)
{
*stub = ifstub->stubbuffer;
IRpcStubBuffer_AddRef(*stub);
*chan = ifstub->chan;
IRpcChannelBuffer_AddRef(*chan);
*stub_apt = apt;
*iid = ifstub->iid;
*iface = ifstub->iface;
stub_manager_int_release(stubmgr);
return S_OK;
}
else
{
stub_manager_int_release(stubmgr);
apartment_release(apt);
return RPC_E_DISCONNECTED;
}
}
/* 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;
}
ipid->Data1 = InterlockedIncrement(&m->apt->ipidc);
ipid->Data2 = (USHORT)m->apt->tid;
ipid->Data3 = (USHORT)GetCurrentProcessId();
return S_OK;
}
/* registers a new interface stub COM object with the stub manager and returns registration record */
struct ifstub *stub_manager_new_ifstub(struct stub_manager *m, IRpcStubBuffer *sb, IUnknown *iptr, REFIID iid, MSHLFLAGS flags)
{
struct ifstub *stub;
HRESULT hr;
TRACE("oid=%s, stubbuffer=%p, iptr=%p, iid=%s\n",
wine_dbgstr_longlong(m->oid), sb, iptr, debugstr_guid(iid));
stub = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct ifstub));
if (!stub) return NULL;
hr = RPC_CreateServerChannel(&stub->chan);
if (hr != S_OK)
{
HeapFree(GetProcessHeap(), 0, stub);
return NULL;
}
stub->stubbuffer = sb;
if (sb) IRpcStubBuffer_AddRef(sb);
IUnknown_AddRef(iptr);
stub->iface = iptr;
stub->flags = flags;
stub->iid = *iid;
/* FIXME: find a cleaner way of identifying that we are creating an ifstub
* for the remunknown interface */
if (flags & MSHLFLAGSP_REMUNKNOWN)
stub->ipid = m->oxid_info.ipidRemUnknown;
else
generate_ipid(m, &stub->ipid);
EnterCriticalSection(&m->lock);
list_add_head(&m->ifstubs, &stub->entry);
/* every normal marshal is counted so we don't allow more than we should */
if (flags & MSHLFLAGS_NORMAL) m->norm_refs++;
LeaveCriticalSection(&m->lock);
TRACE("ifstub %p created with ipid %s\n", stub, debugstr_guid(&stub->ipid));
return stub;
}
static void stub_manager_delete_ifstub(struct stub_manager *m, struct ifstub *ifstub)
{
TRACE("m=%p, m->oid=%s, ipid=%s\n", m, wine_dbgstr_longlong(m->oid), debugstr_guid(&ifstub->ipid));
list_remove(&ifstub->entry);
RPC_UnregisterInterface(&ifstub->iid);
if (ifstub->stubbuffer) IUnknown_Release(ifstub->stubbuffer);
IUnknown_Release(ifstub->iface);
IRpcChannelBuffer_Release(ifstub->chan);
HeapFree(GetProcessHeap(), 0, ifstub);
}
/* returns TRUE if it is possible to unmarshal, FALSE otherwise. */
BOOL stub_manager_notify_unmarshal(struct stub_manager *m, const IPID *ipid)
{
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;
}
EnterCriticalSection(&m->lock);
/* track normal marshals so we can enforce rules whilst in-process */
if (ifstub->flags & MSHLFLAGS_NORMAL)
{
if (m->norm_refs)
m->norm_refs--;
else
{
ERR("attempted invalid normal unmarshal, norm_refs is zero\n");
ret = FALSE;
}
}
LeaveCriticalSection(&m->lock);
return ret;
}
/* handles refcounting for CoReleaseMarshalData */
void stub_manager_release_marshal_data(struct stub_manager *m, ULONG refs, const IPID *ipid)
{
struct ifstub *ifstub;
if (!(ifstub = stub_manager_ipid_to_ifstub(m, ipid)))
return;
if (ifstub->flags & MSHLFLAGS_TABLEWEAK)
refs = 0;
else if (ifstub->flags & MSHLFLAGS_TABLESTRONG)
refs = 1;
stub_manager_ext_release(m, refs, TRUE);
}
/* is an ifstub table marshaled? */
BOOL stub_manager_is_table_marshaled(struct stub_manager *m, const IPID *ipid)
{
struct ifstub *ifstub = stub_manager_ipid_to_ifstub(m, ipid);
assert( ifstub );
return ifstub->flags & (MSHLFLAGS_TABLESTRONG | MSHLFLAGS_TABLEWEAK);
}
/*****************************************************************************
*
* 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;
LONG refs;
} 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);
TRACE("%p before: %d\n", iface, refs-1);
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);
TRACE("%p after: %d\n", iface, refs);
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;
USHORT successful_qis = 0;
APARTMENT *apt;
struct stub_manager *stubmgr;
TRACE("(%p)->(%s, %d, %d, %p, %p)\n", iface, debugstr_guid(ripid), cRefs, cIids, iids, ppQIResults);
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++)
{
HRESULT hrobj = marshal_object(apt, &(*ppQIResults)[i].std, &iids[i],
stubmgr->object, MSHLFLAGS_NORMAL);
if (hrobj == S_OK)
successful_qis++;
(*ppQIResults)[i].hResult = hrobj;
}
stub_manager_int_release(stubmgr);
apartment_release(apt);
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 */
}
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);
apartment_release(apt);
}
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;
}
stub_manager_ext_release(stubmgr, InterfaceRefs[i].cPublicRefs, TRUE);
if (InterfaceRefs[i].cPrivateRefs)
FIXME("Releasing %ld refs securely not implemented\n", InterfaceRefs[i].cPrivateRefs);
stub_manager_int_release(stubmgr);
apartment_release(apt);
}
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 */
HRESULT start_apartment_remote_unknown(void)
{
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 */
hr = marshal_object(apt, &stdobjref, &IID_IRemUnknown, (IUnknown *)pRemUnknown, MSHLFLAGS_NORMAL|MSHLFLAGSP_REMUNKNOWN);
/* 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;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -