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
40 #define NONAMELESSUNION
41 #define NONAMELESSSTRUCT
50 #include "compobj_private.h"
52 #include "wine/debug.h"
54 WINE_DEFAULT_DEBUG_CHANNEL(ole
);
56 const CLSID CLSID_DfMarshal
= { 0x0000030b, 0, 0, {0xc0, 0, 0, 0, 0, 0, 0, 0x46} };
57 const CLSID CLSID_PSFactoryBuffer
= { 0x00000320, 0, 0, {0xc0, 0, 0, 0, 0, 0, 0, 0x46} };
59 /* From: http://msdn.microsoft.com/library/en-us/com/cmi_m_4lda.asp
61 * The first time a client requests a pointer to an interface on a
62 * particular object, COM loads an IClassFactory stub in the server
63 * process and uses it to marshal the first pointer back to the
64 * client. In the client process, COM loads the generic proxy for the
65 * class factory object and calls its implementation of IMarshal to
66 * unmarshal that first pointer. COM then creates the first interface
67 * proxy and hands it a pointer to the RPC channel. Finally, COM returns
68 * the IClassFactory pointer to the client, which uses it to call
69 * IClassFactory::CreateInstance, passing it a reference to the interface.
71 * Back in the server process, COM now creates a new instance of the
72 * object, along with a stub for the requested interface. This stub marshals
73 * the interface pointer back to the client process, where another object
74 * proxy is created, this time for the object itself. Also created is a
75 * proxy for the requested interface, a pointer to which is returned to
76 * the client. With subsequent calls to other interfaces on the object,
77 * COM will load the appropriate interface stubs and proxies as needed.
79 typedef struct _CFStub
{
80 ICOM_VTABLE(IRpcStubBuffer
) *lpvtbl
;
87 CFStub_QueryInterface(LPRPCSTUBBUFFER iface
, REFIID riid
, LPVOID
*ppv
) {
88 if (IsEqualIID(&IID_IUnknown
,riid
)||IsEqualIID(&IID_IRpcStubBuffer
,riid
)) {
90 IUnknown_AddRef(iface
);
93 FIXME("(%s), interface not supported.\n",debugstr_guid(riid
));
98 CFStub_AddRef(LPRPCSTUBBUFFER iface
) {
99 ICOM_THIS(CFStub
,iface
);
106 CFStub_Release(LPRPCSTUBBUFFER iface
) {
107 ICOM_THIS(CFStub
,iface
);
112 HeapFree(GetProcessHeap(),0,This
);
116 static HRESULT WINAPI
117 CFStub_Connect(LPRPCSTUBBUFFER iface
, IUnknown
*pUnkServer
) {
118 ICOM_THIS(CFStub
,iface
);
120 This
->pUnkServer
= pUnkServer
;
121 IUnknown_AddRef(pUnkServer
);
126 CFStub_Disconnect(LPRPCSTUBBUFFER iface
) {
127 ICOM_THIS(CFStub
,iface
);
129 IUnknown_Release(This
->pUnkServer
);
130 This
->pUnkServer
= NULL
;
132 static HRESULT WINAPI
134 LPRPCSTUBBUFFER iface
,RPCOLEMESSAGE
* msg
,IRpcChannelBuffer
* chanbuf
136 ICOM_THIS(CFStub
,iface
);
139 if (msg
->iMethod
== 3) { /* CreateInstance */
141 IClassFactory
*classfac
;
145 ULARGE_INTEGER newpos
;
146 LARGE_INTEGER seekto
;
149 if (msg
->cbBuffer
< sizeof(IID
)) {
150 FIXME("Not enough bytes in buffer (%ld instead of %d)?\n",msg
->cbBuffer
,sizeof(IID
));
153 memcpy(&iid
,msg
->Buffer
,sizeof(iid
));
154 TRACE("->CreateInstance(%s)\n",debugstr_guid(&iid
));
155 hres
= IUnknown_QueryInterface(This
->pUnkServer
,&IID_IClassFactory
,(LPVOID
*)&classfac
);
157 FIXME("Ole server does not provide a IClassFactory?\n");
160 hres
= IClassFactory_CreateInstance(classfac
,NULL
,&iid
,(LPVOID
*)&ppv
);
161 IClassFactory_Release(classfac
);
164 FIXME("Failed to create an instance of %s\n",debugstr_guid(&iid
));
167 hres
= CreateStreamOnHGlobal(0,TRUE
,&pStm
);
169 FIXME("Failed to create stream on hglobal\n");
172 hres
= CoMarshalInterface(pStm
,&iid
,ppv
,0,NULL
,0);
174 FIXME("CoMarshalInterface failed, %lx!\n",hres
);
178 hres
= IStream_Stat(pStm
,&ststg
,0);
180 FIXME("Stat failed.\n");
184 msg
->cbBuffer
= ststg
.cbSize
.s
.LowPart
;
185 msg
->Buffer
= HeapReAlloc(GetProcessHeap(),0,msg
->Buffer
,ststg
.cbSize
.s
.LowPart
);
186 seekto
.s
.LowPart
= 0;seekto
.s
.HighPart
= 0;
187 hres
= IStream_Seek(pStm
,seekto
,SEEK_SET
,&newpos
);
189 FIXME("IStream_Seek failed, %lx\n",hres
);
192 hres
= IStream_Read(pStm
,msg
->Buffer
,msg
->cbBuffer
,&res
);
194 FIXME("Stream Read failed, %lx\n",hres
);
197 IStream_Release(pStm
);
200 FIXME("(%p,%p), stub!\n",msg
,chanbuf
);
201 FIXME("iMethod is %ld\n",msg
->iMethod
);
202 FIXME("cbBuffer is %ld\n",msg
->cbBuffer
);
206 static LPRPCSTUBBUFFER WINAPI
207 CFStub_IsIIDSupported(LPRPCSTUBBUFFER iface
,REFIID riid
) {
208 FIXME("(%s), stub!\n",debugstr_guid(riid
));
213 CFStub_CountRefs(LPRPCSTUBBUFFER iface
) {
214 FIXME("(), stub!\n");
218 static HRESULT WINAPI
219 CFStub_DebugServerQueryInterface(LPRPCSTUBBUFFER iface
,void** ppv
) {
220 FIXME("(%p), stub!\n",ppv
);
224 CFStub_DebugServerRelease(LPRPCSTUBBUFFER iface
,void *pv
) {
225 FIXME("(%p), stub!\n",pv
);
228 static ICOM_VTABLE(IRpcStubBuffer
) cfstubvt
= {
229 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
230 CFStub_QueryInterface
,
236 CFStub_IsIIDSupported
,
238 CFStub_DebugServerQueryInterface
,
239 CFStub_DebugServerRelease
243 CFStub_Construct(LPRPCSTUBBUFFER
*ppv
) {
245 cfstub
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(CFStub
));
247 return E_OUTOFMEMORY
;
248 *ppv
= (LPRPCSTUBBUFFER
)cfstub
;
249 cfstub
->lpvtbl
= &cfstubvt
;
254 /* Since we create proxy buffers and classfactory in a pair, there is
255 * no need for 2 seperate structs. Just put them in one, but remember
258 typedef struct _CFProxy
{
259 ICOM_VTABLE(IClassFactory
) *lpvtbl_cf
;
260 ICOM_VTABLE(IRpcProxyBuffer
) *lpvtbl_proxy
;
263 IRpcChannelBuffer
*chanbuf
;
266 static HRESULT WINAPI
IRpcProxyBufferImpl_QueryInterface(LPRPCPROXYBUFFER iface
,REFIID riid
,LPVOID
*ppv
) {
268 if (IsEqualIID(riid
,&IID_IRpcProxyBuffer
)||IsEqualIID(riid
,&IID_IUnknown
)) {
269 IRpcProxyBuffer_AddRef(iface
);
270 *ppv
= (LPVOID
)iface
;
273 FIXME("(%s), no interface.\n",debugstr_guid(riid
));
274 return E_NOINTERFACE
;
277 static ULONG WINAPI
IRpcProxyBufferImpl_AddRef(LPRPCPROXYBUFFER iface
) {
278 ICOM_THIS_MULTI(CFProxy
,lpvtbl_proxy
,iface
);
279 return ++(This
->ref
);
282 static ULONG WINAPI
IRpcProxyBufferImpl_Release(LPRPCPROXYBUFFER iface
) {
283 ICOM_THIS_MULTI(CFProxy
,lpvtbl_proxy
,iface
);
285 if (!--(This
->ref
)) {
286 IRpcChannelBuffer_Release(This
->chanbuf
);This
->chanbuf
= NULL
;
287 HeapFree(GetProcessHeap(),0,This
);
293 static HRESULT WINAPI
IRpcProxyBufferImpl_Connect(LPRPCPROXYBUFFER iface
,IRpcChannelBuffer
* pRpcChannelBuffer
) {
294 ICOM_THIS_MULTI(CFProxy
,lpvtbl_proxy
,iface
);
296 This
->chanbuf
= pRpcChannelBuffer
;
297 IRpcChannelBuffer_AddRef(This
->chanbuf
);
300 static void WINAPI
IRpcProxyBufferImpl_Disconnect(LPRPCPROXYBUFFER iface
) {
301 ICOM_THIS_MULTI(CFProxy
,lpvtbl_proxy
,iface
);
303 IRpcChannelBuffer_Release(This
->chanbuf
);
304 This
->chanbuf
= NULL
;
308 static HRESULT WINAPI
309 CFProxy_QueryInterface(LPCLASSFACTORY iface
,REFIID riid
, LPVOID
*ppv
) {
311 if (IsEqualIID(&IID_IClassFactory
,riid
) || IsEqualIID(&IID_IUnknown
,riid
)) {
312 *ppv
= (LPVOID
)iface
;
313 IClassFactory_AddRef(iface
);
316 if (IsEqualIID(riid
,&IID_IMarshal
)) /* just to avoid debugoutput */
317 return E_NOINTERFACE
;
318 FIXME("Unhandled interface: %s\n",debugstr_guid(riid
));
319 return E_NOINTERFACE
;
322 static ULONG WINAPI
CFProxy_AddRef(LPCLASSFACTORY iface
) {
323 ICOM_THIS_MULTI(CFProxy
,lpvtbl_cf
,iface
);
328 static ULONG WINAPI
CFProxy_Release(LPCLASSFACTORY iface
) {
329 ICOM_THIS_MULTI(CFProxy
,lpvtbl_cf
,iface
);
333 HeapFree(GetProcessHeap(),0,This
);
337 static HRESULT WINAPI
CFProxy_CreateInstance(
338 LPCLASSFACTORY iface
,
339 LPUNKNOWN pUnkOuter
,/* [in] */
340 REFIID riid
, /* [in] */
341 LPVOID
*ppv
/* [out] */
343 ICOM_THIS_MULTI(CFProxy
,lpvtbl_cf
,iface
);
350 TRACE("(%p,%s,%p)\n",pUnkOuter
,debugstr_guid(riid
),ppv
);
352 /* Send CreateInstance to the remote classfactory.
354 * Data: Only the 'IID'.
357 msg
.cbBuffer
= sizeof(*riid
);
359 hres
= IRpcChannelBuffer_GetBuffer(This
->chanbuf
,&msg
,&IID_IClassFactory
);
361 FIXME("IRpcChannelBuffer_GetBuffer failed with %lx?\n",hres
);
364 memcpy(msg
.Buffer
,riid
,sizeof(*riid
));
365 hres
= IRpcChannelBuffer_SendReceive(This
->chanbuf
,&msg
,&srstatus
);
367 FIXME("IRpcChannelBuffer_SendReceive failed with %lx?\n",hres
);
371 if (!msg
.cbBuffer
) /* interface not found on remote */
374 /* We got back: [Marshaled Interface data] */
375 TRACE("got %ld bytes data.\n",msg
.cbBuffer
);
376 hGlobal
= GlobalAlloc(GMEM_MOVEABLE
|GMEM_NODISCARD
|GMEM_SHARE
,msg
.cbBuffer
);
377 memcpy(GlobalLock(hGlobal
),msg
.Buffer
,msg
.cbBuffer
);
378 hres
= CreateStreamOnHGlobal(hGlobal
,TRUE
,&pStream
);
380 FIXME("CreateStreamOnHGlobal failed with %lx\n",hres
);
383 hres
= CoUnmarshalInterface(
388 IStream_Release(pStream
); /* Does GlobalFree hGlobal too. */
390 FIXME("CoMarshalInterface failed, %lx\n",hres
);
396 static HRESULT WINAPI
CFProxy_LockServer(LPCLASSFACTORY iface
,BOOL fLock
) {
397 /*ICOM_THIS_MULTI(CFProxy,lpvtbl_cf,iface);*/
398 FIXME("(%d), stub!\n",fLock
);
399 /* basically: write BOOL, read empty */
403 static ICOM_VTABLE(IRpcProxyBuffer
) pspbvtbl
= {
404 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
405 IRpcProxyBufferImpl_QueryInterface
,
406 IRpcProxyBufferImpl_AddRef
,
407 IRpcProxyBufferImpl_Release
,
408 IRpcProxyBufferImpl_Connect
,
409 IRpcProxyBufferImpl_Disconnect
411 static ICOM_VTABLE(IClassFactory
) cfproxyvt
= {
412 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
413 CFProxy_QueryInterface
,
416 CFProxy_CreateInstance
,
421 CFProxy_Construct(LPVOID
*ppv
,LPVOID
*ppProxy
) {
424 cf
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(CFProxy
));
426 return E_OUTOFMEMORY
;
428 cf
->lpvtbl_cf
= &cfproxyvt
;
429 cf
->lpvtbl_proxy
= &pspbvtbl
;
430 cf
->ref
= 2; /* we return 2 references to the object! */
431 *ppv
= &(cf
->lpvtbl_cf
);
432 *ppProxy
= &(cf
->lpvtbl_proxy
);
437 /********************* OLE Proxy/Stub Factory ********************************/
438 static HRESULT WINAPI
439 PSFacBuf_QueryInterface(LPPSFACTORYBUFFER iface
, REFIID iid
, LPVOID
*ppv
) {
440 if (IsEqualIID(iid
,&IID_IPSFactoryBuffer
)||IsEqualIID(iid
,&IID_IUnknown
)) {
441 *ppv
= (LPVOID
)iface
;
442 /* No ref counting, static class */
445 FIXME("(%s) unknown IID?\n",debugstr_guid(iid
));
446 return E_NOINTERFACE
;
449 static ULONG WINAPI
PSFacBuf_AddRef(LPPSFACTORYBUFFER iface
) { return 2; }
450 static ULONG WINAPI
PSFacBuf_Release(LPPSFACTORYBUFFER iface
) { return 1; }
452 static HRESULT WINAPI
453 PSFacBuf_CreateProxy(
454 LPPSFACTORYBUFFER iface
, IUnknown
* pUnkOuter
, REFIID riid
,
455 IRpcProxyBuffer
**ppProxy
, LPVOID
*ppv
457 if (IsEqualIID(&IID_IClassFactory
,riid
) ||
458 IsEqualIID(&IID_IUnknown
,riid
)
460 return CFProxy_Construct(ppv
,(LPVOID
*)ppProxy
);
461 FIXME("proxying not implemented for (%s) yet!\n",debugstr_guid(riid
));
465 static HRESULT WINAPI
467 LPPSFACTORYBUFFER iface
, REFIID riid
,IUnknown
*pUnkServer
,
468 IRpcStubBuffer
** ppStub
472 TRACE("(%s,%p,%p)\n",debugstr_guid(riid
),pUnkServer
,ppStub
);
474 if (IsEqualIID(&IID_IClassFactory
,riid
) ||
475 IsEqualIID(&IID_IUnknown
,riid
)
477 hres
= CFStub_Construct(ppStub
);
479 IRpcStubBuffer_Connect((*ppStub
),pUnkServer
);
482 FIXME("stubbing not implemented for (%s) yet!\n",debugstr_guid(riid
));
486 static ICOM_VTABLE(IPSFactoryBuffer
) psfacbufvtbl
= {
487 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
488 PSFacBuf_QueryInterface
,
491 PSFacBuf_CreateProxy
,
495 /* This is the whole PSFactoryBuffer object, just the vtableptr */
496 static ICOM_VTABLE(IPSFactoryBuffer
) *lppsfac
= &psfacbufvtbl
;
498 /***********************************************************************
499 * DllGetClassObject [OLE32.63]
501 HRESULT WINAPI
OLE32_DllGetClassObject(REFCLSID rclsid
, REFIID iid
,LPVOID
*ppv
)
504 if (IsEqualIID(rclsid
,&CLSID_PSFactoryBuffer
)) {
506 /* If we create a ps factory, we might need a stub manager later
512 if (IsEqualIID(rclsid
,&CLSID_DfMarshal
)&&(
513 IsEqualIID(iid
,&IID_IClassFactory
) ||
514 IsEqualIID(iid
,&IID_IUnknown
)
517 return MARSHAL_GetStandardMarshalCF(ppv
);
518 FIXME("\n\tCLSID:\t%s,\n\tIID:\t%s\n",debugstr_guid(rclsid
),debugstr_guid(iid
));
519 return CLASS_E_CLASSNOTAVAILABLE
;