2 * OLE32 proxy/stub handler
4 * Copyright 2002 Marcus Meissner
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 /* Documentation on MSDN:
24 * http://msdn.microsoft.com/library/en-us/com/comext_1q0p.asp
27 * http://msdn.microsoft.com/library/en-us/com/comext_1lia.asp
30 * http://msdn.microsoft.com/library/en-us/com/comext_1gfn.asp
41 #define NONAMELESSUNION
42 #define NONAMELESSSTRUCT
53 #include "compobj_private.h"
55 #include "wine/debug.h"
57 WINE_DEFAULT_DEBUG_CHANNEL(ole);
59 const CLSID CLSID_DfMarshal = { 0x0000030b, 0, 0, {0xc0, 0, 0, 0, 0, 0, 0, 0x46} };
60 const CLSID CLSID_PSFactoryBuffer = { 0x00000320, 0, 0, {0xc0, 0, 0, 0, 0, 0, 0, 0x46} };
62 /* From: http://msdn.microsoft.com/library/en-us/com/cmi_m_4lda.asp
64 * The first time a client requests a pointer to an interface on a
65 * particular object, COM loads an IClassFactory stub in the server
66 * process and uses it to marshal the first pointer back to the
67 * client. In the client process, COM loads the generic proxy for the
68 * class factory object and calls its implementation of IMarshal to
69 * unmarshal that first pointer. COM then creates the first interface
70 * proxy and hands it a pointer to the RPC channel. Finally, COM returns
71 * the IClassFactory pointer to the client, which uses it to call
72 * IClassFactory::CreateInstance, passing it a reference to the interface.
74 * Back in the server process, COM now creates a new instance of the
75 * object, along with a stub for the requested interface. This stub marshals
76 * the interface pointer back to the client process, where another object
77 * proxy is created, this time for the object itself. Also created is a
78 * proxy for the requested interface, a pointer to which is returned to
79 * the client. With subsequent calls to other interfaces on the object,
80 * COM will load the appropriate interface stubs and proxies as needed.
82 typedef struct _CFStub {
83 ICOM_VTABLE(IRpcStubBuffer) *lpvtbl;
90 CFStub_QueryInterface(LPRPCSTUBBUFFER iface, REFIID riid, LPVOID *ppv) {
91 if (IsEqualIID(&IID_IUnknown,riid)||IsEqualIID(&IID_IRpcStubBuffer,riid)) {
93 IUnknown_AddRef(iface);
96 FIXME("(%s), interface not supported.\n",debugstr_guid(riid));
101 CFStub_AddRef(LPRPCSTUBBUFFER iface) {
102 ICOM_THIS(CFStub,iface);
109 CFStub_Release(LPRPCSTUBBUFFER iface) {
110 ICOM_THIS(CFStub,iface);
115 HeapFree(GetProcessHeap(),0,This);
119 static HRESULT WINAPI
120 CFStub_Connect(LPRPCSTUBBUFFER iface, IUnknown *pUnkServer) {
121 ICOM_THIS(CFStub,iface);
123 This->pUnkServer = pUnkServer;
124 IUnknown_AddRef(pUnkServer);
129 CFStub_Disconnect(LPRPCSTUBBUFFER iface) {
130 ICOM_THIS(CFStub,iface);
132 IUnknown_Release(This->pUnkServer);
133 This->pUnkServer = NULL;
135 static HRESULT WINAPI
137 LPRPCSTUBBUFFER iface,RPCOLEMESSAGE* msg,IRpcChannelBuffer* chanbuf
139 ICOM_THIS(CFStub,iface);
142 if (msg->iMethod == 3) { /* CreateInstance */
144 IClassFactory *classfac;
148 ULARGE_INTEGER newpos;
149 LARGE_INTEGER seekto;
152 if (msg->cbBuffer < sizeof(IID)) {
153 FIXME("Not enough bytes in buffer (%ld instead of %d)?\n",msg->cbBuffer,sizeof(IID));
156 memcpy(&iid,msg->Buffer,sizeof(iid));
157 TRACE("->CreateInstance(%s)\n",debugstr_guid(&iid));
158 hres = IUnknown_QueryInterface(This->pUnkServer,&IID_IClassFactory,(LPVOID*)&classfac);
160 FIXME("Ole server does not provide a IClassFactory?\n");
163 hres = IClassFactory_CreateInstance(classfac,NULL,&iid,(LPVOID*)&ppv);
164 IClassFactory_Release(classfac);
167 FIXME("Failed to create an instance of %s\n",debugstr_guid(&iid));
170 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
172 FIXME("Failed to create stream on hglobal\n");
175 hres = CoMarshalInterface(pStm,&iid,ppv,0,NULL,0);
177 FIXME("CoMarshalInterface failed, %lx!\n",hres);
181 hres = IStream_Stat(pStm,&ststg,0);
183 FIXME("Stat failed.\n");
187 msg->cbBuffer = ststg.cbSize.s.LowPart;
188 msg->Buffer = HeapReAlloc(GetProcessHeap(),0,msg->Buffer,ststg.cbSize.s.LowPart);
189 seekto.s.LowPart = 0;seekto.s.HighPart = 0;
190 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
192 FIXME("IStream_Seek failed, %lx\n",hres);
195 hres = IStream_Read(pStm,msg->Buffer,msg->cbBuffer,&res);
197 FIXME("Stream Read failed, %lx\n",hres);
200 IStream_Release(pStm);
203 FIXME("(%p,%p), stub!\n",msg,chanbuf);
204 FIXME("iMethod is %ld\n",msg->iMethod);
205 FIXME("cbBuffer is %ld\n",msg->cbBuffer);
209 static LPRPCSTUBBUFFER WINAPI
210 CFStub_IsIIDSupported(LPRPCSTUBBUFFER iface,REFIID riid) {
211 FIXME("(%s), stub!\n",debugstr_guid(riid));
216 CFStub_CountRefs(LPRPCSTUBBUFFER iface) {
217 FIXME("(), stub!\n");
221 static HRESULT WINAPI
222 CFStub_DebugServerQueryInterface(LPRPCSTUBBUFFER iface,void** ppv) {
223 FIXME("(%p), stub!\n",ppv);
227 CFStub_DebugServerRelease(LPRPCSTUBBUFFER iface,void *pv) {
228 FIXME("(%p), stub!\n",pv);
231 static ICOM_VTABLE(IRpcStubBuffer) cfstubvt = {
232 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
233 CFStub_QueryInterface,
239 CFStub_IsIIDSupported,
241 CFStub_DebugServerQueryInterface,
242 CFStub_DebugServerRelease
246 CFStub_Construct(LPRPCSTUBBUFFER *ppv) {
248 cfstub = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(CFStub));
250 return E_OUTOFMEMORY;
251 *ppv = (LPRPCSTUBBUFFER)cfstub;
252 cfstub->lpvtbl = &cfstubvt;
257 /* Since we create proxy buffers and classfactory in a pair, there is
258 * no need for 2 separate structs. Just put them in one, but remember
261 typedef struct _CFProxy {
262 ICOM_VTABLE(IClassFactory) *lpvtbl_cf;
263 ICOM_VTABLE(IRpcProxyBuffer) *lpvtbl_proxy;
266 IRpcChannelBuffer *chanbuf;
269 static HRESULT WINAPI IRpcProxyBufferImpl_QueryInterface(LPRPCPROXYBUFFER iface,REFIID riid,LPVOID *ppv) {
271 if (IsEqualIID(riid,&IID_IRpcProxyBuffer)||IsEqualIID(riid,&IID_IUnknown)) {
272 IRpcProxyBuffer_AddRef(iface);
273 *ppv = (LPVOID)iface;
276 FIXME("(%s), no interface.\n",debugstr_guid(riid));
277 return E_NOINTERFACE;
280 static ULONG WINAPI IRpcProxyBufferImpl_AddRef(LPRPCPROXYBUFFER iface) {
281 ICOM_THIS_MULTI(CFProxy,lpvtbl_proxy,iface);
282 return ++(This->ref);
285 static ULONG WINAPI IRpcProxyBufferImpl_Release(LPRPCPROXYBUFFER iface) {
286 ICOM_THIS_MULTI(CFProxy,lpvtbl_proxy,iface);
288 if (!--(This->ref)) {
289 IRpcChannelBuffer_Release(This->chanbuf);This->chanbuf = NULL;
290 HeapFree(GetProcessHeap(),0,This);
296 static HRESULT WINAPI IRpcProxyBufferImpl_Connect(LPRPCPROXYBUFFER iface,IRpcChannelBuffer* pRpcChannelBuffer) {
297 ICOM_THIS_MULTI(CFProxy,lpvtbl_proxy,iface);
299 This->chanbuf = pRpcChannelBuffer;
300 IRpcChannelBuffer_AddRef(This->chanbuf);
303 static void WINAPI IRpcProxyBufferImpl_Disconnect(LPRPCPROXYBUFFER iface) {
304 ICOM_THIS_MULTI(CFProxy,lpvtbl_proxy,iface);
306 IRpcChannelBuffer_Release(This->chanbuf);
307 This->chanbuf = NULL;
311 static HRESULT WINAPI
312 CFProxy_QueryInterface(LPCLASSFACTORY iface,REFIID riid, LPVOID *ppv) {
314 if (IsEqualIID(&IID_IClassFactory,riid) || IsEqualIID(&IID_IUnknown,riid)) {
315 *ppv = (LPVOID)iface;
316 IClassFactory_AddRef(iface);
319 if (IsEqualIID(riid,&IID_IMarshal)) /* just to avoid debug output */
320 return E_NOINTERFACE;
321 FIXME("Unhandled interface: %s\n",debugstr_guid(riid));
322 return E_NOINTERFACE;
325 static ULONG WINAPI CFProxy_AddRef(LPCLASSFACTORY iface) {
326 ICOM_THIS_MULTI(CFProxy,lpvtbl_cf,iface);
331 static ULONG WINAPI CFProxy_Release(LPCLASSFACTORY iface) {
332 ICOM_THIS_MULTI(CFProxy,lpvtbl_cf,iface);
336 HeapFree(GetProcessHeap(),0,This);
340 static HRESULT WINAPI CFProxy_CreateInstance(
341 LPCLASSFACTORY iface,
342 LPUNKNOWN pUnkOuter,/* [in] */
343 REFIID riid, /* [in] */
344 LPVOID *ppv /* [out] */
346 ICOM_THIS_MULTI(CFProxy,lpvtbl_cf,iface);
353 TRACE("(%p,%s,%p)\n",pUnkOuter,debugstr_guid(riid),ppv);
355 /* Send CreateInstance to the remote classfactory.
357 * Data: Only the 'IID'.
360 msg.cbBuffer = sizeof(*riid);
362 hres = IRpcChannelBuffer_GetBuffer(This->chanbuf,&msg,&IID_IClassFactory);
364 FIXME("IRpcChannelBuffer_GetBuffer failed with %lx?\n",hres);
367 memcpy(msg.Buffer,riid,sizeof(*riid));
368 hres = IRpcChannelBuffer_SendReceive(This->chanbuf,&msg,&srstatus);
370 FIXME("IRpcChannelBuffer_SendReceive failed with %lx?\n",hres);
374 if (!msg.cbBuffer) /* interface not found on remote */
377 /* We got back: [Marshalled Interface data] */
378 TRACE("got %ld bytes data.\n",msg.cbBuffer);
379 hGlobal = GlobalAlloc(GMEM_MOVEABLE|GMEM_NODISCARD|GMEM_SHARE,msg.cbBuffer);
380 memcpy(GlobalLock(hGlobal),msg.Buffer,msg.cbBuffer);
381 hres = CreateStreamOnHGlobal(hGlobal,TRUE,&pStream);
383 FIXME("CreateStreamOnHGlobal failed with %lx\n",hres);
386 hres = CoUnmarshalInterface(
391 IStream_Release(pStream); /* Does GlobalFree hGlobal too. */
393 FIXME("CoMarshalInterface failed, %lx\n",hres);
399 static HRESULT WINAPI CFProxy_LockServer(LPCLASSFACTORY iface,BOOL fLock) {
400 /*ICOM_THIS_MULTI(CFProxy,lpvtbl_cf,iface);*/
401 FIXME("(%d), stub!\n",fLock);
402 /* basically: write BOOL, read empty */
406 static ICOM_VTABLE(IRpcProxyBuffer) pspbvtbl = {
407 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
408 IRpcProxyBufferImpl_QueryInterface,
409 IRpcProxyBufferImpl_AddRef,
410 IRpcProxyBufferImpl_Release,
411 IRpcProxyBufferImpl_Connect,
412 IRpcProxyBufferImpl_Disconnect
414 static ICOM_VTABLE(IClassFactory) cfproxyvt = {
415 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
416 CFProxy_QueryInterface,
419 CFProxy_CreateInstance,
424 CFProxy_Construct(LPVOID *ppv,LPVOID *ppProxy) {
427 cf = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(CFProxy));
429 return E_OUTOFMEMORY;
431 cf->lpvtbl_cf = &cfproxyvt;
432 cf->lpvtbl_proxy = &pspbvtbl;
433 cf->ref = 2; /* we return 2 references to the object! */
434 *ppv = &(cf->lpvtbl_cf);
435 *ppProxy = &(cf->lpvtbl_proxy);
440 /********************* OLE Proxy/Stub Factory ********************************/
441 static HRESULT WINAPI
442 PSFacBuf_QueryInterface(LPPSFACTORYBUFFER iface, REFIID iid, LPVOID *ppv) {
443 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)||IsEqualIID(iid,&IID_IUnknown)) {
444 *ppv = (LPVOID)iface;
445 /* No ref counting, static class */
448 FIXME("(%s) unknown IID?\n",debugstr_guid(iid));
449 return E_NOINTERFACE;
452 static ULONG WINAPI PSFacBuf_AddRef(LPPSFACTORYBUFFER iface) { return 2; }
453 static ULONG WINAPI PSFacBuf_Release(LPPSFACTORYBUFFER iface) { return 1; }
455 static HRESULT WINAPI
456 PSFacBuf_CreateProxy(
457 LPPSFACTORYBUFFER iface, IUnknown* pUnkOuter, REFIID riid,
458 IRpcProxyBuffer **ppProxy, LPVOID *ppv
460 if (IsEqualIID(&IID_IClassFactory,riid) ||
461 IsEqualIID(&IID_IUnknown,riid)
463 return CFProxy_Construct(ppv,(LPVOID*)ppProxy);
464 FIXME("proxying not implemented for (%s) yet!\n",debugstr_guid(riid));
468 static HRESULT WINAPI
470 LPPSFACTORYBUFFER iface, REFIID riid,IUnknown *pUnkServer,
471 IRpcStubBuffer** ppStub
475 TRACE("(%s,%p,%p)\n",debugstr_guid(riid),pUnkServer,ppStub);
477 if (IsEqualIID(&IID_IClassFactory,riid) ||
478 IsEqualIID(&IID_IUnknown,riid)
480 hres = CFStub_Construct(ppStub);
482 IRpcStubBuffer_Connect((*ppStub),pUnkServer);
485 FIXME("stubbing not implemented for (%s) yet!\n",debugstr_guid(riid));
489 static ICOM_VTABLE(IPSFactoryBuffer) psfacbufvtbl = {
490 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
491 PSFacBuf_QueryInterface,
494 PSFacBuf_CreateProxy,
498 /* This is the whole PSFactoryBuffer object, just the vtableptr */
499 static ICOM_VTABLE(IPSFactoryBuffer) *lppsfac = &psfacbufvtbl;
501 /***********************************************************************
502 * DllGetClassObject [OLE32.@]
504 HRESULT WINAPI OLE32_DllGetClassObject(REFCLSID rclsid, REFIID iid,LPVOID *ppv)
507 if (IsEqualIID(rclsid,&CLSID_PSFactoryBuffer)) {
509 /* If we create a ps factory, we might need a stub manager later
515 if (IsEqualIID(rclsid,&CLSID_DfMarshal)&&(
516 IsEqualIID(iid,&IID_IClassFactory) ||
517 IsEqualIID(iid,&IID_IUnknown)
520 return MARSHAL_GetStandardMarshalCF(ppv);
521 if (IsEqualIID(rclsid,&CLSID_StdGlobalInterfaceTable) && (IsEqualIID(iid,&IID_IClassFactory) || IsEqualIID(iid,&IID_IUnknown)))
522 return StdGlobalInterfaceTable_GetFactory(ppv);
524 FIXME("\n\tCLSID:\t%s,\n\tIID:\t%s\n",debugstr_guid(rclsid),debugstr_guid(iid));
525 return CLASS_E_CLASSNOTAVAILABLE;