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
52 #include "compobj_private.h"
54 #include "wine/debug.h"
56 WINE_DEFAULT_DEBUG_CHANNEL(ole);
58 const CLSID CLSID_DfMarshal = { 0x0000030b, 0, 0, {0xc0, 0, 0, 0, 0, 0, 0, 0x46} };
59 const CLSID CLSID_PSFactoryBuffer = { 0x00000320, 0, 0, {0xc0, 0, 0, 0, 0, 0, 0, 0x46} };
61 /* From: http://msdn.microsoft.com/library/en-us/com/cmi_m_4lda.asp
63 * The first time a client requests a pointer to an interface on a
64 * particular object, COM loads an IClassFactory stub in the server
65 * process and uses it to marshal the first pointer back to the
66 * client. In the client process, COM loads the generic proxy for the
67 * class factory object and calls its implementation of IMarshal to
68 * unmarshal that first pointer. COM then creates the first interface
69 * proxy and hands it a pointer to the RPC channel. Finally, COM returns
70 * the IClassFactory pointer to the client, which uses it to call
71 * IClassFactory::CreateInstance, passing it a reference to the interface.
73 * Back in the server process, COM now creates a new instance of the
74 * object, along with a stub for the requested interface. This stub marshals
75 * the interface pointer back to the client process, where another object
76 * proxy is created, this time for the object itself. Also created is a
77 * proxy for the requested interface, a pointer to which is returned to
78 * the client. With subsequent calls to other interfaces on the object,
79 * COM will load the appropriate interface stubs and proxies as needed.
81 typedef struct _CFStub {
82 ICOM_VTABLE(IRpcStubBuffer) *lpvtbl;
89 CFStub_QueryInterface(LPRPCSTUBBUFFER iface, REFIID riid, LPVOID *ppv) {
90 if (IsEqualIID(&IID_IUnknown,riid)||IsEqualIID(&IID_IRpcStubBuffer,riid)) {
92 IUnknown_AddRef(iface);
95 FIXME("(%s), interface not supported.\n",debugstr_guid(riid));
100 CFStub_AddRef(LPRPCSTUBBUFFER iface) {
101 ICOM_THIS(CFStub,iface);
108 CFStub_Release(LPRPCSTUBBUFFER iface) {
109 ICOM_THIS(CFStub,iface);
114 HeapFree(GetProcessHeap(),0,This);
118 static HRESULT WINAPI
119 CFStub_Connect(LPRPCSTUBBUFFER iface, IUnknown *pUnkServer) {
120 ICOM_THIS(CFStub,iface);
122 This->pUnkServer = pUnkServer;
123 IUnknown_AddRef(pUnkServer);
128 CFStub_Disconnect(LPRPCSTUBBUFFER iface) {
129 ICOM_THIS(CFStub,iface);
131 IUnknown_Release(This->pUnkServer);
132 This->pUnkServer = NULL;
134 static HRESULT WINAPI
136 LPRPCSTUBBUFFER iface,RPCOLEMESSAGE* msg,IRpcChannelBuffer* chanbuf
138 ICOM_THIS(CFStub,iface);
141 if (msg->iMethod == 3) { /* CreateInstance */
143 IClassFactory *classfac;
147 ULARGE_INTEGER newpos;
148 LARGE_INTEGER seekto;
151 if (msg->cbBuffer < sizeof(IID)) {
152 FIXME("Not enough bytes in buffer (%ld instead of %d)?\n",msg->cbBuffer,sizeof(IID));
155 memcpy(&iid,msg->Buffer,sizeof(iid));
156 TRACE("->CreateInstance(%s)\n",debugstr_guid(&iid));
157 hres = IUnknown_QueryInterface(This->pUnkServer,&IID_IClassFactory,(LPVOID*)&classfac);
159 FIXME("Ole server does not provide a IClassFactory?\n");
162 hres = IClassFactory_CreateInstance(classfac,NULL,&iid,(LPVOID*)&ppv);
163 IClassFactory_Release(classfac);
166 FIXME("Failed to create an instance of %s\n",debugstr_guid(&iid));
169 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
171 FIXME("Failed to create stream on hglobal\n");
174 hres = CoMarshalInterface(pStm,&iid,ppv,0,NULL,0);
176 FIXME("CoMarshalInterface failed, %lx!\n",hres);
180 hres = IStream_Stat(pStm,&ststg,0);
182 FIXME("Stat failed.\n");
186 msg->cbBuffer = ststg.cbSize.s.LowPart;
187 msg->Buffer = HeapReAlloc(GetProcessHeap(),0,msg->Buffer,ststg.cbSize.s.LowPart);
188 seekto.s.LowPart = 0;seekto.s.HighPart = 0;
189 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
191 FIXME("IStream_Seek failed, %lx\n",hres);
194 hres = IStream_Read(pStm,msg->Buffer,msg->cbBuffer,&res);
196 FIXME("Stream Read failed, %lx\n",hres);
199 IStream_Release(pStm);
202 FIXME("(%p,%p), stub!\n",msg,chanbuf);
203 FIXME("iMethod is %ld\n",msg->iMethod);
204 FIXME("cbBuffer is %ld\n",msg->cbBuffer);
208 static LPRPCSTUBBUFFER WINAPI
209 CFStub_IsIIDSupported(LPRPCSTUBBUFFER iface,REFIID riid) {
210 FIXME("(%s), stub!\n",debugstr_guid(riid));
215 CFStub_CountRefs(LPRPCSTUBBUFFER iface) {
216 FIXME("(), stub!\n");
220 static HRESULT WINAPI
221 CFStub_DebugServerQueryInterface(LPRPCSTUBBUFFER iface,void** ppv) {
222 FIXME("(%p), stub!\n",ppv);
226 CFStub_DebugServerRelease(LPRPCSTUBBUFFER iface,void *pv) {
227 FIXME("(%p), stub!\n",pv);
230 static ICOM_VTABLE(IRpcStubBuffer) cfstubvt = {
231 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
232 CFStub_QueryInterface,
238 CFStub_IsIIDSupported,
240 CFStub_DebugServerQueryInterface,
241 CFStub_DebugServerRelease
245 CFStub_Construct(LPRPCSTUBBUFFER *ppv) {
247 cfstub = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(CFStub));
249 return E_OUTOFMEMORY;
250 *ppv = (LPRPCSTUBBUFFER)cfstub;
251 cfstub->lpvtbl = &cfstubvt;
256 /* Since we create proxy buffers and classfactory in a pair, there is
257 * no need for 2 separate structs. Just put them in one, but remember
260 typedef struct _CFProxy {
261 ICOM_VTABLE(IClassFactory) *lpvtbl_cf;
262 ICOM_VTABLE(IRpcProxyBuffer) *lpvtbl_proxy;
265 IRpcChannelBuffer *chanbuf;
268 static HRESULT WINAPI IRpcProxyBufferImpl_QueryInterface(LPRPCPROXYBUFFER iface,REFIID riid,LPVOID *ppv) {
270 if (IsEqualIID(riid,&IID_IRpcProxyBuffer)||IsEqualIID(riid,&IID_IUnknown)) {
271 IRpcProxyBuffer_AddRef(iface);
272 *ppv = (LPVOID)iface;
275 FIXME("(%s), no interface.\n",debugstr_guid(riid));
276 return E_NOINTERFACE;
279 static ULONG WINAPI IRpcProxyBufferImpl_AddRef(LPRPCPROXYBUFFER iface) {
280 ICOM_THIS_MULTI(CFProxy,lpvtbl_proxy,iface);
281 return ++(This->ref);
284 static ULONG WINAPI IRpcProxyBufferImpl_Release(LPRPCPROXYBUFFER iface) {
285 ICOM_THIS_MULTI(CFProxy,lpvtbl_proxy,iface);
287 if (!--(This->ref)) {
288 IRpcChannelBuffer_Release(This->chanbuf);This->chanbuf = NULL;
289 HeapFree(GetProcessHeap(),0,This);
295 static HRESULT WINAPI IRpcProxyBufferImpl_Connect(LPRPCPROXYBUFFER iface,IRpcChannelBuffer* pRpcChannelBuffer) {
296 ICOM_THIS_MULTI(CFProxy,lpvtbl_proxy,iface);
298 This->chanbuf = pRpcChannelBuffer;
299 IRpcChannelBuffer_AddRef(This->chanbuf);
302 static void WINAPI IRpcProxyBufferImpl_Disconnect(LPRPCPROXYBUFFER iface) {
303 ICOM_THIS_MULTI(CFProxy,lpvtbl_proxy,iface);
305 IRpcChannelBuffer_Release(This->chanbuf);
306 This->chanbuf = NULL;
310 static HRESULT WINAPI
311 CFProxy_QueryInterface(LPCLASSFACTORY iface,REFIID riid, LPVOID *ppv) {
313 if (IsEqualIID(&IID_IClassFactory,riid) || IsEqualIID(&IID_IUnknown,riid)) {
314 *ppv = (LPVOID)iface;
315 IClassFactory_AddRef(iface);
318 if (IsEqualIID(riid,&IID_IMarshal)) /* just to avoid debug output */
319 return E_NOINTERFACE;
320 FIXME("Unhandled interface: %s\n",debugstr_guid(riid));
321 return E_NOINTERFACE;
324 static ULONG WINAPI CFProxy_AddRef(LPCLASSFACTORY iface) {
325 ICOM_THIS_MULTI(CFProxy,lpvtbl_cf,iface);
330 static ULONG WINAPI CFProxy_Release(LPCLASSFACTORY iface) {
331 ICOM_THIS_MULTI(CFProxy,lpvtbl_cf,iface);
335 HeapFree(GetProcessHeap(),0,This);
339 static HRESULT WINAPI CFProxy_CreateInstance(
340 LPCLASSFACTORY iface,
341 LPUNKNOWN pUnkOuter,/* [in] */
342 REFIID riid, /* [in] */
343 LPVOID *ppv /* [out] */
345 ICOM_THIS_MULTI(CFProxy,lpvtbl_cf,iface);
352 TRACE("(%p,%s,%p)\n",pUnkOuter,debugstr_guid(riid),ppv);
354 /* Send CreateInstance to the remote classfactory.
356 * Data: Only the 'IID'.
359 msg.cbBuffer = sizeof(*riid);
361 hres = IRpcChannelBuffer_GetBuffer(This->chanbuf,&msg,&IID_IClassFactory);
363 FIXME("IRpcChannelBuffer_GetBuffer failed with %lx?\n",hres);
366 memcpy(msg.Buffer,riid,sizeof(*riid));
367 hres = IRpcChannelBuffer_SendReceive(This->chanbuf,&msg,&srstatus);
369 FIXME("IRpcChannelBuffer_SendReceive failed with %lx?\n",hres);
373 if (!msg.cbBuffer) /* interface not found on remote */
376 /* We got back: [Marshalled Interface data] */
377 TRACE("got %ld bytes data.\n",msg.cbBuffer);
378 hGlobal = GlobalAlloc(GMEM_MOVEABLE|GMEM_NODISCARD|GMEM_SHARE,msg.cbBuffer);
379 memcpy(GlobalLock(hGlobal),msg.Buffer,msg.cbBuffer);
380 hres = CreateStreamOnHGlobal(hGlobal,TRUE,&pStream);
382 FIXME("CreateStreamOnHGlobal failed with %lx\n",hres);
385 hres = CoUnmarshalInterface(
390 IStream_Release(pStream); /* Does GlobalFree hGlobal too. */
392 FIXME("CoMarshalInterface failed, %lx\n",hres);
398 static HRESULT WINAPI CFProxy_LockServer(LPCLASSFACTORY iface,BOOL fLock) {
399 /*ICOM_THIS_MULTI(CFProxy,lpvtbl_cf,iface);*/
400 FIXME("(%d), stub!\n",fLock);
401 /* basically: write BOOL, read empty */
405 static ICOM_VTABLE(IRpcProxyBuffer) pspbvtbl = {
406 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
407 IRpcProxyBufferImpl_QueryInterface,
408 IRpcProxyBufferImpl_AddRef,
409 IRpcProxyBufferImpl_Release,
410 IRpcProxyBufferImpl_Connect,
411 IRpcProxyBufferImpl_Disconnect
413 static ICOM_VTABLE(IClassFactory) cfproxyvt = {
414 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
415 CFProxy_QueryInterface,
418 CFProxy_CreateInstance,
423 CFProxy_Construct(LPVOID *ppv,LPVOID *ppProxy) {
426 cf = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(CFProxy));
428 return E_OUTOFMEMORY;
430 cf->lpvtbl_cf = &cfproxyvt;
431 cf->lpvtbl_proxy = &pspbvtbl;
432 cf->ref = 2; /* we return 2 references to the object! */
433 *ppv = &(cf->lpvtbl_cf);
434 *ppProxy = &(cf->lpvtbl_proxy);
439 /********************* OLE Proxy/Stub Factory ********************************/
440 static HRESULT WINAPI
441 PSFacBuf_QueryInterface(LPPSFACTORYBUFFER iface, REFIID iid, LPVOID *ppv) {
442 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)||IsEqualIID(iid,&IID_IUnknown)) {
443 *ppv = (LPVOID)iface;
444 /* No ref counting, static class */
447 FIXME("(%s) unknown IID?\n",debugstr_guid(iid));
448 return E_NOINTERFACE;
451 static ULONG WINAPI PSFacBuf_AddRef(LPPSFACTORYBUFFER iface) { return 2; }
452 static ULONG WINAPI PSFacBuf_Release(LPPSFACTORYBUFFER iface) { return 1; }
454 static HRESULT WINAPI
455 PSFacBuf_CreateProxy(
456 LPPSFACTORYBUFFER iface, IUnknown* pUnkOuter, REFIID riid,
457 IRpcProxyBuffer **ppProxy, LPVOID *ppv
459 if (IsEqualIID(&IID_IClassFactory,riid) ||
460 IsEqualIID(&IID_IUnknown,riid)
462 return CFProxy_Construct(ppv,(LPVOID*)ppProxy);
463 FIXME("proxying not implemented for (%s) yet!\n",debugstr_guid(riid));
467 static HRESULT WINAPI
469 LPPSFACTORYBUFFER iface, REFIID riid,IUnknown *pUnkServer,
470 IRpcStubBuffer** ppStub
474 TRACE("(%s,%p,%p)\n",debugstr_guid(riid),pUnkServer,ppStub);
476 if (IsEqualIID(&IID_IClassFactory,riid) ||
477 IsEqualIID(&IID_IUnknown,riid)
479 hres = CFStub_Construct(ppStub);
481 IRpcStubBuffer_Connect((*ppStub),pUnkServer);
484 FIXME("stubbing not implemented for (%s) yet!\n",debugstr_guid(riid));
488 static ICOM_VTABLE(IPSFactoryBuffer) psfacbufvtbl = {
489 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
490 PSFacBuf_QueryInterface,
493 PSFacBuf_CreateProxy,
497 /* This is the whole PSFactoryBuffer object, just the vtableptr */
498 static ICOM_VTABLE(IPSFactoryBuffer) *lppsfac = &psfacbufvtbl;
500 /***********************************************************************
501 * DllGetClassObject [OLE32.63]
503 HRESULT WINAPI OLE32_DllGetClassObject(REFCLSID rclsid, REFIID iid,LPVOID *ppv)
506 if (IsEqualIID(rclsid,&CLSID_PSFactoryBuffer)) {
508 /* If we create a ps factory, we might need a stub manager later
514 if (IsEqualIID(rclsid,&CLSID_DfMarshal)&&(
515 IsEqualIID(iid,&IID_IClassFactory) ||
516 IsEqualIID(iid,&IID_IUnknown)
519 return MARSHAL_GetStandardMarshalCF(ppv);
520 if (IsEqualIID(rclsid,&CLSID_StdGlobalInterfaceTable) && (IsEqualIID(iid,&IID_IClassFactory) || IsEqualIID(iid,&IID_IUnknown)))
521 return StdGlobalInterfaceTable_GetFactory(ppv);
523 FIXME("\n\tCLSID:\t%s,\n\tIID:\t%s\n",debugstr_guid(rclsid),debugstr_guid(iid));
524 return CLASS_E_CLASSNOTAVAILABLE;