📄 oleproxy.c
字号:
/*
* OLE32 proxy/stub handler
*
* Copyright 2002 Marcus Meissner
* Copyright 2001 Ove K鍁en, TransGaming Technologies
*
* 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
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
/* Documentation on MSDN:
*
* (Top level COM documentation)
* http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnanchor/html/componentdevelopmentank.asp
*
* (COM Proxy)
* http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/htm/comext_1q0p.asp
*
* (COM Stub)
* http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/htm/comext_1lia.asp
*
* (Marshal)
* http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/htm/comext_1gfn.asp
*
*/
#include "config.h"
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#define COBJMACROS
#define NONAMELESSUNION
#define NONAMELESSSTRUCT
#include "windef.h"
#include "winbase.h"
#include "winuser.h"
#include "objbase.h"
#include "ole2.h"
#include "rpc.h"
#include "winerror.h"
#include "wtypes.h"
#include "compobj_private.h"
#include "moniker.h"
#include "wine/debug.h"
WINE_DEFAULT_DEBUG_CHANNEL(ole);
static ULONG WINAPI RURpcProxyBufferImpl_Release(LPRPCPROXYBUFFER iface);
/* From: http://msdn.microsoft.com/library/en-us/com/cmi_m_4lda.asp
*
* The first time a client requests a pointer to an interface on a
* particular object, COM loads an IClassFactory stub in the server
* process and uses it to marshal the first pointer back to the
* client. In the client process, COM loads the generic proxy for the
* class factory object and calls its implementation of IMarshal to
* unmarshal that first pointer. COM then creates the first interface
* proxy and hands it a pointer to the RPC channel. Finally, COM returns
* the IClassFactory pointer to the client, which uses it to call
* IClassFactory::CreateInstance, passing it a reference to the interface.
*
* Back in the server process, COM now creates a new instance of the
* object, along with a stub for the requested interface. This stub marshals
* the interface pointer back to the client process, where another object
* proxy is created, this time for the object itself. Also created is a
* proxy for the requested interface, a pointer to which is returned to
* the client. With subsequent calls to other interfaces on the object,
* COM will load the appropriate interface stubs and proxies as needed.
*/
typedef struct _CFStub {
const IRpcStubBufferVtbl *lpvtbl;
LONG ref;
LPUNKNOWN pUnkServer;
} CFStub;
static HRESULT WINAPI
CFStub_QueryInterface(LPRPCSTUBBUFFER iface, REFIID riid, LPVOID *ppv) {
if (IsEqualIID(&IID_IUnknown,riid)||IsEqualIID(&IID_IRpcStubBuffer,riid)) {
*ppv = (LPVOID)iface;
IUnknown_AddRef(iface);
return S_OK;
}
FIXME("(%s), interface not supported.\n",debugstr_guid(riid));
return E_NOINTERFACE;
}
static ULONG WINAPI
CFStub_AddRef(LPRPCSTUBBUFFER iface) {
CFStub *This = (CFStub *)iface;
return InterlockedIncrement(&This->ref);
}
static ULONG WINAPI
CFStub_Release(LPRPCSTUBBUFFER iface) {
CFStub *This = (CFStub *)iface;
ULONG ref;
ref = InterlockedDecrement(&This->ref);
if (!ref) {
IRpcStubBuffer_Disconnect(iface);
HeapFree(GetProcessHeap(),0,This);
}
return ref;
}
static HRESULT WINAPI
CFStub_Connect(LPRPCSTUBBUFFER iface, IUnknown *pUnkServer) {
CFStub *This = (CFStub *)iface;
This->pUnkServer = pUnkServer;
IUnknown_AddRef(pUnkServer);
return S_OK;
}
static void WINAPI
CFStub_Disconnect(LPRPCSTUBBUFFER iface) {
CFStub *This = (CFStub *)iface;
if (This->pUnkServer) {
IUnknown_Release(This->pUnkServer);
This->pUnkServer = NULL;
}
}
static HRESULT WINAPI
CFStub_Invoke(
LPRPCSTUBBUFFER iface,RPCOLEMESSAGE* msg,IRpcChannelBuffer* chanbuf
) {
CFStub *This = (CFStub *)iface;
HRESULT hres;
if (msg->iMethod == 3) { /* CreateInstance */
IID iid;
IClassFactory *classfac;
IUnknown *ppv;
IStream *pStm;
STATSTG ststg;
ULARGE_INTEGER newpos;
LARGE_INTEGER seekto;
ULONG res;
if (msg->cbBuffer < sizeof(IID)) {
FIXME("Not enough bytes in buffer (%d)?\n",msg->cbBuffer);
return E_FAIL;
}
memcpy(&iid,msg->Buffer,sizeof(iid));
TRACE("->CreateInstance(%s)\n",debugstr_guid(&iid));
hres = IUnknown_QueryInterface(This->pUnkServer,&IID_IClassFactory,(LPVOID*)&classfac);
if (hres) {
FIXME("Ole server does not provide an IClassFactory?\n");
return hres;
}
hres = IClassFactory_CreateInstance(classfac,NULL,&iid,(LPVOID*)&ppv);
IClassFactory_Release(classfac);
msg->cbBuffer = 0;
if (hres) {
FIXME("Failed to create an instance of %s\n",debugstr_guid(&iid));
goto getbuffer;
}
hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
if (hres) {
FIXME("Failed to create stream on hglobal\n");
goto getbuffer;
}
hres = IStream_Write(pStm, &ppv, sizeof(ppv), NULL);
if (hres) {
ERR("IStream_Write failed, 0x%08x\n", hres);
goto getbuffer;
}
if (ppv) {
hres = CoMarshalInterface(pStm,&iid,ppv,0,NULL,0);
IUnknown_Release(ppv);
if (hres) {
FIXME("CoMarshalInterface failed, %x!\n",hres);
goto getbuffer;
}
}
hres = IStream_Stat(pStm,&ststg,0);
if (hres) {
FIXME("Stat failed.\n");
goto getbuffer;
}
msg->cbBuffer = ststg.cbSize.u.LowPart;
getbuffer:
IRpcChannelBuffer_GetBuffer(chanbuf, msg, &IID_IClassFactory);
if (hres) return hres;
seekto.u.LowPart = 0;seekto.u.HighPart = 0;
hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
if (hres) {
FIXME("IStream_Seek failed, %x\n",hres);
return hres;
}
hres = IStream_Read(pStm,msg->Buffer,msg->cbBuffer,&res);
if (hres) {
FIXME("Stream Read failed, %x\n",hres);
return hres;
}
IStream_Release(pStm);
return S_OK;
}
FIXME("(%p,%p), stub!\n",msg,chanbuf);
FIXME("iMethod is %d\n",msg->iMethod);
FIXME("cbBuffer is %d\n",msg->cbBuffer);
return E_FAIL;
}
static LPRPCSTUBBUFFER WINAPI
CFStub_IsIIDSupported(LPRPCSTUBBUFFER iface,REFIID riid) {
FIXME("(%s), stub!\n",debugstr_guid(riid));
return NULL;
}
static ULONG WINAPI
CFStub_CountRefs(LPRPCSTUBBUFFER iface) {
FIXME("(), stub!\n");
return 1;
}
static HRESULT WINAPI
CFStub_DebugServerQueryInterface(LPRPCSTUBBUFFER iface,void** ppv) {
FIXME("(%p), stub!\n",ppv);
return E_FAIL;
}
static void WINAPI
CFStub_DebugServerRelease(LPRPCSTUBBUFFER iface,void *pv) {
FIXME("(%p), stub!\n",pv);
}
static const IRpcStubBufferVtbl cfstubvt = {
CFStub_QueryInterface,
CFStub_AddRef,
CFStub_Release,
CFStub_Connect,
CFStub_Disconnect,
CFStub_Invoke,
CFStub_IsIIDSupported,
CFStub_CountRefs,
CFStub_DebugServerQueryInterface,
CFStub_DebugServerRelease
};
static HRESULT
CFStub_Construct(LPRPCSTUBBUFFER *ppv) {
CFStub *cfstub;
cfstub = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(CFStub));
if (!cfstub)
return E_OUTOFMEMORY;
*ppv = (LPRPCSTUBBUFFER)cfstub;
cfstub->lpvtbl = &cfstubvt;
cfstub->ref = 1;
return S_OK;
}
/* Since we create proxy buffers and classfactory in a pair, there is
* no need for 2 separate structs. Just put them in one, but remember
* the refcount.
*/
typedef struct _CFProxy {
const IClassFactoryVtbl *lpvtbl_cf;
const IRpcProxyBufferVtbl *lpvtbl_proxy;
LONG ref;
IRpcChannelBuffer *chanbuf;
IUnknown *outer_unknown;
} CFProxy;
static HRESULT WINAPI IRpcProxyBufferImpl_QueryInterface(LPRPCPROXYBUFFER iface,REFIID riid,LPVOID *ppv) {
*ppv = NULL;
if (IsEqualIID(riid,&IID_IRpcProxyBuffer)||IsEqualIID(riid,&IID_IUnknown)) {
IRpcProxyBuffer_AddRef(iface);
*ppv = (LPVOID)iface;
return S_OK;
}
FIXME("(%s), no interface.\n",debugstr_guid(riid));
return E_NOINTERFACE;
}
static ULONG WINAPI IRpcProxyBufferImpl_AddRef(LPRPCPROXYBUFFER iface) {
ICOM_THIS_MULTI(CFProxy,lpvtbl_proxy,iface);
return InterlockedIncrement(&This->ref);
}
static ULONG WINAPI IRpcProxyBufferImpl_Release(LPRPCPROXYBUFFER iface) {
ICOM_THIS_MULTI(CFProxy,lpvtbl_proxy,iface);
ULONG ref = InterlockedDecrement(&This->ref);
if (!ref) {
IRpcProxyBuffer_Disconnect(iface);
HeapFree(GetProcessHeap(),0,This);
}
return ref;
}
static HRESULT WINAPI IRpcProxyBufferImpl_Connect(LPRPCPROXYBUFFER iface,IRpcChannelBuffer* pRpcChannelBuffer) {
ICOM_THIS_MULTI(CFProxy,lpvtbl_proxy,iface);
This->chanbuf = pRpcChannelBuffer;
IRpcChannelBuffer_AddRef(This->chanbuf);
return S_OK;
}
static void WINAPI IRpcProxyBufferImpl_Disconnect(LPRPCPROXYBUFFER iface) {
ICOM_THIS_MULTI(CFProxy,lpvtbl_proxy,iface);
if (This->chanbuf) {
IRpcChannelBuffer_Release(This->chanbuf);
This->chanbuf = NULL;
}
}
static HRESULT WINAPI
CFProxy_QueryInterface(LPCLASSFACTORY iface,REFIID riid, LPVOID *ppv) {
ICOM_THIS_MULTI(CFProxy,lpvtbl_cf,iface);
if (This->outer_unknown) return IUnknown_QueryInterface(This->outer_unknown, riid, ppv);
*ppv = NULL;
if (IsEqualIID(&IID_IClassFactory,riid) || IsEqualIID(&IID_IUnknown,riid)) {
*ppv = (LPVOID)iface;
IClassFactory_AddRef(iface);
return S_OK;
}
if (IsEqualIID(riid,&IID_IMarshal)) /* just to avoid debug output */
return E_NOINTERFACE;
FIXME("Unhandled interface: %s\n",debugstr_guid(riid));
return E_NOINTERFACE;
}
static ULONG WINAPI CFProxy_AddRef(LPCLASSFACTORY iface) {
ICOM_THIS_MULTI(CFProxy,lpvtbl_cf,iface);
if (This->outer_unknown) return IUnknown_AddRef(This->outer_unknown);
return InterlockedIncrement(&This->ref);
}
static ULONG WINAPI CFProxy_Release(LPCLASSFACTORY iface) {
ICOM_THIS_MULTI(CFProxy,lpvtbl_cf,iface);
if (This->outer_unknown)
return IUnknown_Release(This->outer_unknown);
else
return IRpcProxyBufferImpl_Release((IRpcProxyBuffer *)&This->lpvtbl_proxy);
}
static HRESULT WINAPI CFProxy_CreateInstance(
LPCLASSFACTORY iface,
LPUNKNOWN pUnkOuter,/* [in] */
REFIID riid, /* [in] */
LPVOID *ppv /* [out] */
) {
ICOM_THIS_MULTI(CFProxy,lpvtbl_cf,iface);
HRESULT hres;
LPSTREAM pStream;
HGLOBAL hGlobal;
ULONG srstatus;
RPCOLEMESSAGE msg;
TRACE("(%p,%s,%p)\n",pUnkOuter,debugstr_guid(riid),ppv);
/* Send CreateInstance to the remote classfactory.
*
* Data: Only the 'IID'.
*/
msg.iMethod = 3;
msg.cbBuffer = sizeof(*riid);
msg.Buffer = NULL;
hres = IRpcChannelBuffer_GetBuffer(This->chanbuf,&msg,&IID_IClassFactory);
if (hres) {
FIXME("IRpcChannelBuffer_GetBuffer failed with %x?\n",hres);
return hres;
}
memcpy(msg.Buffer,riid,sizeof(*riid));
hres = IRpcChannelBuffer_SendReceive(This->chanbuf,&msg,&srstatus);
if (hres) {
FIXME("IRpcChannelBuffer_SendReceive failed with %x?\n",hres);
IRpcChannelBuffer_FreeBuffer(This->chanbuf,&msg);
return hres;
}
if (!msg.cbBuffer) { /* interface not found on remote */
IRpcChannelBuffer_FreeBuffer(This->chanbuf,&msg);
return srstatus;
}
/* We got back: [Marshalled Interface data] */
TRACE("got %d bytes data.\n",msg.cbBuffer);
hGlobal = GlobalAlloc(GMEM_MOVEABLE|GMEM_NODISCARD|GMEM_SHARE,msg.cbBuffer);
memcpy(GlobalLock(hGlobal),msg.Buffer,msg.cbBuffer);
hres = CreateStreamOnHGlobal(hGlobal,TRUE,&pStream);
if (hres) {
FIXME("CreateStreamOnHGlobal failed with %x\n",hres);
IRpcChannelBuffer_FreeBuffer(This->chanbuf,&msg);
return hres;
}
hres = IStream_Read(pStream, ppv, sizeof(*ppv), NULL);
if (hres != S_OK)
hres = E_FAIL;
else if (*ppv) {
hres = CoUnmarshalInterface(
pStream,
riid,
ppv
);
}
IStream_Release(pStream); /* Does GlobalFree hGlobal too. */
IRpcChannelBuffer_FreeBuffer(This->chanbuf,&msg);
if (hres) {
FIXME("CoMarshalInterface failed, %x\n",hres);
return hres;
}
return S_OK;
}
static HRESULT WINAPI CFProxy_LockServer(LPCLASSFACTORY iface,BOOL fLock) {
/*ICOM_THIS_MULTI(CFProxy,lpvtbl_cf,iface);*/
FIXME("(%d), stub!\n",fLock);
/* basically: write BOOL, read empty */
return S_OK;
}
static const IRpcProxyBufferVtbl pspbvtbl = {
IRpcProxyBufferImpl_QueryInterface,
IRpcProxyBufferImpl_AddRef,
IRpcProxyBufferImpl_Release,
IRpcProxyBufferImpl_Connect,
IRpcProxyBufferImpl_Disconnect
};
static const IClassFactoryVtbl cfproxyvt = {
CFProxy_QueryInterface,
CFProxy_AddRef,
CFProxy_Release,
CFProxy_CreateInstance,
CFProxy_LockServer
};
static HRESULT
CFProxy_Construct(IUnknown *pUnkOuter, LPVOID *ppv,LPVOID *ppProxy) {
CFProxy *cf;
cf = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(CFProxy));
if (!cf)
return E_OUTOFMEMORY;
cf->lpvtbl_cf = &cfproxyvt;
cf->lpvtbl_proxy = &pspbvtbl;
/* one reference for the proxy buffer */
cf->ref = 1;
cf->outer_unknown = pUnkOuter;
*ppv = &(cf->lpvtbl_cf);
*ppProxy = &(cf->lpvtbl_proxy);
/* and one reference for the object */
IUnknown_AddRef((IUnknown *)*ppv);
return S_OK;
}
/********************* IRemUnknown Proxy/Stub ********************************/
typedef struct
{
const IRpcStubBufferVtbl *lpVtbl;
LONG refs;
IRemUnknown *iface;
} RemUnkStub;
static HRESULT WINAPI RemUnkStub_QueryInterface(LPRPCSTUBBUFFER iface,
REFIID riid,
LPVOID *obj)
{
RemUnkStub *This = (RemUnkStub *)iface;
TRACE("(%p)->QueryInterface(%s,%p)\n",This,debugstr_guid(riid),obj);
if (IsEqualGUID(&IID_IUnknown,riid) ||
IsEqualGUID(&IID_IRpcStubBuffer,riid)) {
*obj = This;
return S_OK;
}
return E_NOINTERFACE;
}
static ULONG WINAPI RemUnkStub_AddRef(LPRPCSTUBBUFFER iface)
{
RemUnkStub *This = (RemUnkStub *)iface;
TRACE("(%p)->AddRef()\n",This);
return InterlockedIncrement(&This->refs);
}
static ULONG WINAPI RemUnkStub_Release(LPRPCSTUBBUFFER iface)
{
RemUnkStub *This = (RemUnkStub *)iface;
ULONG refs;
TRACE("(%p)->Release()\n",This);
refs = InterlockedDecrement(&This->refs);
if (!refs)
HeapFree(GetProcessHeap(), 0, This);
return refs;
}
static HRESULT WINAPI RemUnkStub_Connect(LPRPCSTUBBUFFER iface,
LPUNKNOWN lpUnkServer)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -