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:
23 * (Top level COM documentation)
24 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnanchor/html/componentdevelopmentank.asp
27 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/htm/comext_1q0p.asp
30 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/htm/comext_1lia.asp
33 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/htm/comext_1gfn.asp
44 #define NONAMELESSUNION
45 #define NONAMELESSSTRUCT
56 #include "compobj_private.h"
58 #include "wine/debug.h"
60 WINE_DEFAULT_DEBUG_CHANNEL(ole
);
62 const CLSID CLSID_DfMarshal
= { 0x0000030b, 0, 0, {0xc0, 0, 0, 0, 0, 0, 0, 0x46} };
63 const CLSID CLSID_PSFactoryBuffer
= { 0x00000320, 0, 0, {0xc0, 0, 0, 0, 0, 0, 0, 0x46} };
65 /* From: http://msdn.microsoft.com/library/en-us/com/cmi_m_4lda.asp
67 * The first time a client requests a pointer to an interface on a
68 * particular object, COM loads an IClassFactory stub in the server
69 * process and uses it to marshal the first pointer back to the
70 * client. In the client process, COM loads the generic proxy for the
71 * class factory object and calls its implementation of IMarshal to
72 * unmarshal that first pointer. COM then creates the first interface
73 * proxy and hands it a pointer to the RPC channel. Finally, COM returns
74 * the IClassFactory pointer to the client, which uses it to call
75 * IClassFactory::CreateInstance, passing it a reference to the interface.
77 * Back in the server process, COM now creates a new instance of the
78 * object, along with a stub for the requested interface. This stub marshals
79 * the interface pointer back to the client process, where another object
80 * proxy is created, this time for the object itself. Also created is a
81 * proxy for the requested interface, a pointer to which is returned to
82 * the client. With subsequent calls to other interfaces on the object,
83 * COM will load the appropriate interface stubs and proxies as needed.
85 typedef struct _CFStub
{
86 IRpcStubBufferVtbl
*lpvtbl
;
93 CFStub_QueryInterface(LPRPCSTUBBUFFER iface
, REFIID riid
, LPVOID
*ppv
) {
94 if (IsEqualIID(&IID_IUnknown
,riid
)||IsEqualIID(&IID_IRpcStubBuffer
,riid
)) {
96 IUnknown_AddRef(iface
);
99 FIXME("(%s), interface not supported.\n",debugstr_guid(riid
));
100 return E_NOINTERFACE
;
104 CFStub_AddRef(LPRPCSTUBBUFFER iface
) {
105 ICOM_THIS(CFStub
,iface
);
112 CFStub_Release(LPRPCSTUBBUFFER iface
) {
113 ICOM_THIS(CFStub
,iface
);
118 HeapFree(GetProcessHeap(),0,This
);
122 static HRESULT WINAPI
123 CFStub_Connect(LPRPCSTUBBUFFER iface
, IUnknown
*pUnkServer
) {
124 ICOM_THIS(CFStub
,iface
);
126 This
->pUnkServer
= pUnkServer
;
127 IUnknown_AddRef(pUnkServer
);
132 CFStub_Disconnect(LPRPCSTUBBUFFER iface
) {
133 ICOM_THIS(CFStub
,iface
);
135 IUnknown_Release(This
->pUnkServer
);
136 This
->pUnkServer
= NULL
;
138 static HRESULT WINAPI
140 LPRPCSTUBBUFFER iface
,RPCOLEMESSAGE
* msg
,IRpcChannelBuffer
* chanbuf
142 ICOM_THIS(CFStub
,iface
);
145 if (msg
->iMethod
== 3) { /* CreateInstance */
147 IClassFactory
*classfac
;
151 ULARGE_INTEGER newpos
;
152 LARGE_INTEGER seekto
;
155 if (msg
->cbBuffer
< sizeof(IID
)) {
156 FIXME("Not enough bytes in buffer (%ld instead of %d)?\n",msg
->cbBuffer
,sizeof(IID
));
159 memcpy(&iid
,msg
->Buffer
,sizeof(iid
));
160 TRACE("->CreateInstance(%s)\n",debugstr_guid(&iid
));
161 hres
= IUnknown_QueryInterface(This
->pUnkServer
,&IID_IClassFactory
,(LPVOID
*)&classfac
);
163 FIXME("Ole server does not provide a IClassFactory?\n");
166 hres
= IClassFactory_CreateInstance(classfac
,NULL
,&iid
,(LPVOID
*)&ppv
);
167 IClassFactory_Release(classfac
);
170 FIXME("Failed to create an instance of %s\n",debugstr_guid(&iid
));
173 hres
= CreateStreamOnHGlobal(0,TRUE
,&pStm
);
175 FIXME("Failed to create stream on hglobal\n");
178 hres
= CoMarshalInterface(pStm
,&iid
,ppv
,0,NULL
,0);
180 FIXME("CoMarshalInterface failed, %lx!\n",hres
);
184 hres
= IStream_Stat(pStm
,&ststg
,0);
186 FIXME("Stat failed.\n");
190 msg
->cbBuffer
= ststg
.cbSize
.u
.LowPart
;
193 msg
->Buffer
= HeapReAlloc(GetProcessHeap(),0,msg
->Buffer
,ststg
.cbSize
.u
.LowPart
);
195 msg
->Buffer
= HeapAlloc(GetProcessHeap(),0,ststg
.cbSize
.u
.LowPart
);
197 seekto
.u
.LowPart
= 0;seekto
.u
.HighPart
= 0;
198 hres
= IStream_Seek(pStm
,seekto
,SEEK_SET
,&newpos
);
200 FIXME("IStream_Seek failed, %lx\n",hres
);
203 hres
= IStream_Read(pStm
,msg
->Buffer
,msg
->cbBuffer
,&res
);
205 FIXME("Stream Read failed, %lx\n",hres
);
208 IStream_Release(pStm
);
211 FIXME("(%p,%p), stub!\n",msg
,chanbuf
);
212 FIXME("iMethod is %ld\n",msg
->iMethod
);
213 FIXME("cbBuffer is %ld\n",msg
->cbBuffer
);
217 static LPRPCSTUBBUFFER WINAPI
218 CFStub_IsIIDSupported(LPRPCSTUBBUFFER iface
,REFIID riid
) {
219 FIXME("(%s), stub!\n",debugstr_guid(riid
));
224 CFStub_CountRefs(LPRPCSTUBBUFFER iface
) {
225 FIXME("(), stub!\n");
229 static HRESULT WINAPI
230 CFStub_DebugServerQueryInterface(LPRPCSTUBBUFFER iface
,void** ppv
) {
231 FIXME("(%p), stub!\n",ppv
);
235 CFStub_DebugServerRelease(LPRPCSTUBBUFFER iface
,void *pv
) {
236 FIXME("(%p), stub!\n",pv
);
239 static IRpcStubBufferVtbl cfstubvt
= {
240 CFStub_QueryInterface
,
246 CFStub_IsIIDSupported
,
248 CFStub_DebugServerQueryInterface
,
249 CFStub_DebugServerRelease
253 CFStub_Construct(LPRPCSTUBBUFFER
*ppv
) {
255 cfstub
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(CFStub
));
257 return E_OUTOFMEMORY
;
258 *ppv
= (LPRPCSTUBBUFFER
)cfstub
;
259 cfstub
->lpvtbl
= &cfstubvt
;
264 /* Since we create proxy buffers and classfactory in a pair, there is
265 * no need for 2 separate structs. Just put them in one, but remember
268 typedef struct _CFProxy
{
269 IClassFactoryVtbl
*lpvtbl_cf
;
270 IRpcProxyBufferVtbl
*lpvtbl_proxy
;
273 IRpcChannelBuffer
*chanbuf
;
276 static HRESULT WINAPI
IRpcProxyBufferImpl_QueryInterface(LPRPCPROXYBUFFER iface
,REFIID riid
,LPVOID
*ppv
) {
278 if (IsEqualIID(riid
,&IID_IRpcProxyBuffer
)||IsEqualIID(riid
,&IID_IUnknown
)) {
279 IRpcProxyBuffer_AddRef(iface
);
280 *ppv
= (LPVOID
)iface
;
283 FIXME("(%s), no interface.\n",debugstr_guid(riid
));
284 return E_NOINTERFACE
;
287 static ULONG WINAPI
IRpcProxyBufferImpl_AddRef(LPRPCPROXYBUFFER iface
) {
288 ICOM_THIS_MULTI(CFProxy
,lpvtbl_proxy
,iface
);
289 return ++(This
->ref
);
292 static ULONG WINAPI
IRpcProxyBufferImpl_Release(LPRPCPROXYBUFFER iface
) {
293 ICOM_THIS_MULTI(CFProxy
,lpvtbl_proxy
,iface
);
295 if (!--(This
->ref
)) {
296 IRpcChannelBuffer_Release(This
->chanbuf
);This
->chanbuf
= NULL
;
297 HeapFree(GetProcessHeap(),0,This
);
303 static HRESULT WINAPI
IRpcProxyBufferImpl_Connect(LPRPCPROXYBUFFER iface
,IRpcChannelBuffer
* pRpcChannelBuffer
) {
304 ICOM_THIS_MULTI(CFProxy
,lpvtbl_proxy
,iface
);
306 This
->chanbuf
= pRpcChannelBuffer
;
307 IRpcChannelBuffer_AddRef(This
->chanbuf
);
310 static void WINAPI
IRpcProxyBufferImpl_Disconnect(LPRPCPROXYBUFFER iface
) {
311 ICOM_THIS_MULTI(CFProxy
,lpvtbl_proxy
,iface
);
313 IRpcChannelBuffer_Release(This
->chanbuf
);
314 This
->chanbuf
= NULL
;
318 static HRESULT WINAPI
319 CFProxy_QueryInterface(LPCLASSFACTORY iface
,REFIID riid
, LPVOID
*ppv
) {
321 if (IsEqualIID(&IID_IClassFactory
,riid
) || IsEqualIID(&IID_IUnknown
,riid
)) {
322 *ppv
= (LPVOID
)iface
;
323 IClassFactory_AddRef(iface
);
326 if (IsEqualIID(riid
,&IID_IMarshal
)) /* just to avoid debug output */
327 return E_NOINTERFACE
;
328 FIXME("Unhandled interface: %s\n",debugstr_guid(riid
));
329 return E_NOINTERFACE
;
332 static ULONG WINAPI
CFProxy_AddRef(LPCLASSFACTORY iface
) {
333 ICOM_THIS_MULTI(CFProxy
,lpvtbl_cf
,iface
);
338 static ULONG WINAPI
CFProxy_Release(LPCLASSFACTORY iface
) {
339 ICOM_THIS_MULTI(CFProxy
,lpvtbl_cf
,iface
);
343 HeapFree(GetProcessHeap(),0,This
);
347 static HRESULT WINAPI
CFProxy_CreateInstance(
348 LPCLASSFACTORY iface
,
349 LPUNKNOWN pUnkOuter
,/* [in] */
350 REFIID riid
, /* [in] */
351 LPVOID
*ppv
/* [out] */
353 ICOM_THIS_MULTI(CFProxy
,lpvtbl_cf
,iface
);
360 TRACE("(%p,%s,%p)\n",pUnkOuter
,debugstr_guid(riid
),ppv
);
362 /* Send CreateInstance to the remote classfactory.
364 * Data: Only the 'IID'.
367 msg
.cbBuffer
= sizeof(*riid
);
369 hres
= IRpcChannelBuffer_GetBuffer(This
->chanbuf
,&msg
,&IID_IClassFactory
);
371 FIXME("IRpcChannelBuffer_GetBuffer failed with %lx?\n",hres
);
374 memcpy(msg
.Buffer
,riid
,sizeof(*riid
));
375 hres
= IRpcChannelBuffer_SendReceive(This
->chanbuf
,&msg
,&srstatus
);
377 FIXME("IRpcChannelBuffer_SendReceive failed with %lx?\n",hres
);
381 if (!msg
.cbBuffer
) /* interface not found on remote */
384 /* We got back: [Marshalled Interface data] */
385 TRACE("got %ld bytes data.\n",msg
.cbBuffer
);
386 hGlobal
= GlobalAlloc(GMEM_MOVEABLE
|GMEM_NODISCARD
|GMEM_SHARE
,msg
.cbBuffer
);
387 memcpy(GlobalLock(hGlobal
),msg
.Buffer
,msg
.cbBuffer
);
388 hres
= CreateStreamOnHGlobal(hGlobal
,TRUE
,&pStream
);
390 FIXME("CreateStreamOnHGlobal failed with %lx\n",hres
);
393 hres
= CoUnmarshalInterface(
398 IStream_Release(pStream
); /* Does GlobalFree hGlobal too. */
400 FIXME("CoMarshalInterface failed, %lx\n",hres
);
406 static HRESULT WINAPI
CFProxy_LockServer(LPCLASSFACTORY iface
,BOOL fLock
) {
407 /*ICOM_THIS_MULTI(CFProxy,lpvtbl_cf,iface);*/
408 FIXME("(%d), stub!\n",fLock
);
409 /* basically: write BOOL, read empty */
413 static IRpcProxyBufferVtbl pspbvtbl
= {
414 IRpcProxyBufferImpl_QueryInterface
,
415 IRpcProxyBufferImpl_AddRef
,
416 IRpcProxyBufferImpl_Release
,
417 IRpcProxyBufferImpl_Connect
,
418 IRpcProxyBufferImpl_Disconnect
420 static IClassFactoryVtbl cfproxyvt
= {
421 CFProxy_QueryInterface
,
424 CFProxy_CreateInstance
,
429 CFProxy_Construct(LPVOID
*ppv
,LPVOID
*ppProxy
) {
432 cf
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(CFProxy
));
434 return E_OUTOFMEMORY
;
436 cf
->lpvtbl_cf
= &cfproxyvt
;
437 cf
->lpvtbl_proxy
= &pspbvtbl
;
438 /* 1 reference for the proxy and 1 for the object */
440 *ppv
= &(cf
->lpvtbl_cf
);
441 *ppProxy
= &(cf
->lpvtbl_proxy
);
446 /********************* OLE Proxy/Stub Factory ********************************/
447 static HRESULT WINAPI
448 PSFacBuf_QueryInterface(LPPSFACTORYBUFFER iface
, REFIID iid
, LPVOID
*ppv
) {
449 if (IsEqualIID(iid
,&IID_IPSFactoryBuffer
)||IsEqualIID(iid
,&IID_IUnknown
)) {
450 *ppv
= (LPVOID
)iface
;
451 /* No ref counting, static class */
454 FIXME("(%s) unknown IID?\n",debugstr_guid(iid
));
455 return E_NOINTERFACE
;
458 static ULONG WINAPI
PSFacBuf_AddRef(LPPSFACTORYBUFFER iface
) { return 2; }
459 static ULONG WINAPI
PSFacBuf_Release(LPPSFACTORYBUFFER iface
) { return 1; }
461 static HRESULT WINAPI
462 PSFacBuf_CreateProxy(
463 LPPSFACTORYBUFFER iface
, IUnknown
* pUnkOuter
, REFIID riid
,
464 IRpcProxyBuffer
**ppProxy
, LPVOID
*ppv
466 if (IsEqualIID(&IID_IClassFactory
,riid
) ||
467 IsEqualIID(&IID_IUnknown
,riid
)
469 return CFProxy_Construct(ppv
,(LPVOID
*)ppProxy
);
470 FIXME("proxying not implemented for (%s) yet!\n",debugstr_guid(riid
));
474 static HRESULT WINAPI
476 LPPSFACTORYBUFFER iface
, REFIID riid
,IUnknown
*pUnkServer
,
477 IRpcStubBuffer
** ppStub
481 TRACE("(%s,%p,%p)\n",debugstr_guid(riid
),pUnkServer
,ppStub
);
483 if (IsEqualIID(&IID_IClassFactory
,riid
) ||
484 IsEqualIID(&IID_IUnknown
,riid
)
486 hres
= CFStub_Construct(ppStub
);
488 IRpcStubBuffer_Connect((*ppStub
),pUnkServer
);
491 FIXME("stubbing not implemented for (%s) yet!\n",debugstr_guid(riid
));
495 static IPSFactoryBufferVtbl psfacbufvtbl
= {
496 PSFacBuf_QueryInterface
,
499 PSFacBuf_CreateProxy
,
503 /* This is the whole PSFactoryBuffer object, just the vtableptr */
504 static IPSFactoryBufferVtbl
*lppsfac
= &psfacbufvtbl
;
506 /***********************************************************************
507 * DllGetClassObject [OLE32.@]
509 HRESULT WINAPI
OLE32_DllGetClassObject(REFCLSID rclsid
, REFIID iid
,LPVOID
*ppv
)
512 if (IsEqualIID(rclsid
,&CLSID_PSFactoryBuffer
)) {
516 if (IsEqualIID(rclsid
,&CLSID_DfMarshal
)&&(
517 IsEqualIID(iid
,&IID_IClassFactory
) ||
518 IsEqualIID(iid
,&IID_IUnknown
)
521 return MARSHAL_GetStandardMarshalCF(ppv
);
522 if (IsEqualIID(rclsid
,&CLSID_StdGlobalInterfaceTable
) && (IsEqualIID(iid
,&IID_IClassFactory
) || IsEqualIID(iid
,&IID_IUnknown
)))
523 return StdGlobalInterfaceTable_GetFactory(ppv
);
525 FIXME("\n\tCLSID:\t%s,\n\tIID:\t%s\n",debugstr_guid(rclsid
),debugstr_guid(iid
));
526 return CLASS_E_CLASSNOTAVAILABLE
;