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
45 #define NONAMELESSUNION
46 #define NONAMELESSSTRUCT
58 #include "compobj_private.h"
60 #include "wine/debug.h"
62 WINE_DEFAULT_DEBUG_CHANNEL(ole
);
64 const CLSID CLSID_DfMarshal
= { 0x0000030b, 0, 0, {0xc0, 0, 0, 0, 0, 0, 0, 0x46} };
65 const CLSID CLSID_PSFactoryBuffer
= { 0x00000320, 0, 0, {0xc0, 0, 0, 0, 0, 0, 0, 0x46} };
67 /* From: http://msdn.microsoft.com/library/en-us/com/cmi_m_4lda.asp
69 * The first time a client requests a pointer to an interface on a
70 * particular object, COM loads an IClassFactory stub in the server
71 * process and uses it to marshal the first pointer back to the
72 * client. In the client process, COM loads the generic proxy for the
73 * class factory object and calls its implementation of IMarshal to
74 * unmarshal that first pointer. COM then creates the first interface
75 * proxy and hands it a pointer to the RPC channel. Finally, COM returns
76 * the IClassFactory pointer to the client, which uses it to call
77 * IClassFactory::CreateInstance, passing it a reference to the interface.
79 * Back in the server process, COM now creates a new instance of the
80 * object, along with a stub for the requested interface. This stub marshals
81 * the interface pointer back to the client process, where another object
82 * proxy is created, this time for the object itself. Also created is a
83 * proxy for the requested interface, a pointer to which is returned to
84 * the client. With subsequent calls to other interfaces on the object,
85 * COM will load the appropriate interface stubs and proxies as needed.
87 typedef struct _CFStub
{
88 IRpcStubBufferVtbl
*lpvtbl
;
95 CFStub_QueryInterface(LPRPCSTUBBUFFER iface
, REFIID riid
, LPVOID
*ppv
) {
96 if (IsEqualIID(&IID_IUnknown
,riid
)||IsEqualIID(&IID_IRpcStubBuffer
,riid
)) {
98 IUnknown_AddRef(iface
);
101 FIXME("(%s), interface not supported.\n",debugstr_guid(riid
));
102 return E_NOINTERFACE
;
106 CFStub_AddRef(LPRPCSTUBBUFFER iface
) {
107 CFStub
*This
= (CFStub
*)iface
;
108 return InterlockedIncrement(&This
->ref
);
112 CFStub_Release(LPRPCSTUBBUFFER iface
) {
113 CFStub
*This
= (CFStub
*)iface
;
116 ref
= InterlockedDecrement(&This
->ref
);
117 if (!ref
) HeapFree(GetProcessHeap(),0,This
);
121 static HRESULT WINAPI
122 CFStub_Connect(LPRPCSTUBBUFFER iface
, IUnknown
*pUnkServer
) {
123 CFStub
*This
= (CFStub
*)iface
;
125 This
->pUnkServer
= pUnkServer
;
126 IUnknown_AddRef(pUnkServer
);
131 CFStub_Disconnect(LPRPCSTUBBUFFER iface
) {
132 CFStub
*This
= (CFStub
*)iface
;
134 IUnknown_Release(This
->pUnkServer
);
135 This
->pUnkServer
= NULL
;
137 static HRESULT WINAPI
139 LPRPCSTUBBUFFER iface
,RPCOLEMESSAGE
* msg
,IRpcChannelBuffer
* chanbuf
141 CFStub
*This
= (CFStub
*)iface
;
144 if (msg
->iMethod
== 3) { /* CreateInstance */
146 IClassFactory
*classfac
;
150 ULARGE_INTEGER newpos
;
151 LARGE_INTEGER seekto
;
154 if (msg
->cbBuffer
< sizeof(IID
)) {
155 FIXME("Not enough bytes in buffer (%ld instead of %d)?\n",msg
->cbBuffer
,sizeof(IID
));
158 memcpy(&iid
,msg
->Buffer
,sizeof(iid
));
159 TRACE("->CreateInstance(%s)\n",debugstr_guid(&iid
));
160 hres
= IUnknown_QueryInterface(This
->pUnkServer
,&IID_IClassFactory
,(LPVOID
*)&classfac
);
162 FIXME("Ole server does not provide a IClassFactory?\n");
165 hres
= IClassFactory_CreateInstance(classfac
,NULL
,&iid
,(LPVOID
*)&ppv
);
166 IClassFactory_Release(classfac
);
169 FIXME("Failed to create an instance of %s\n",debugstr_guid(&iid
));
172 hres
= CreateStreamOnHGlobal(0,TRUE
,&pStm
);
174 FIXME("Failed to create stream on hglobal\n");
177 hres
= CoMarshalInterface(pStm
,&iid
,ppv
,0,NULL
,0);
179 FIXME("CoMarshalInterface failed, %lx!\n",hres
);
183 hres
= IStream_Stat(pStm
,&ststg
,0);
185 FIXME("Stat failed.\n");
189 msg
->cbBuffer
= ststg
.cbSize
.u
.LowPart
;
192 msg
->Buffer
= HeapReAlloc(GetProcessHeap(),0,msg
->Buffer
,ststg
.cbSize
.u
.LowPart
);
194 msg
->Buffer
= HeapAlloc(GetProcessHeap(),0,ststg
.cbSize
.u
.LowPart
);
196 seekto
.u
.LowPart
= 0;seekto
.u
.HighPart
= 0;
197 hres
= IStream_Seek(pStm
,seekto
,SEEK_SET
,&newpos
);
199 FIXME("IStream_Seek failed, %lx\n",hres
);
202 hres
= IStream_Read(pStm
,msg
->Buffer
,msg
->cbBuffer
,&res
);
204 FIXME("Stream Read failed, %lx\n",hres
);
207 IStream_Release(pStm
);
210 FIXME("(%p,%p), stub!\n",msg
,chanbuf
);
211 FIXME("iMethod is %ld\n",msg
->iMethod
);
212 FIXME("cbBuffer is %ld\n",msg
->cbBuffer
);
216 static LPRPCSTUBBUFFER WINAPI
217 CFStub_IsIIDSupported(LPRPCSTUBBUFFER iface
,REFIID riid
) {
218 FIXME("(%s), stub!\n",debugstr_guid(riid
));
223 CFStub_CountRefs(LPRPCSTUBBUFFER iface
) {
224 FIXME("(), stub!\n");
228 static HRESULT WINAPI
229 CFStub_DebugServerQueryInterface(LPRPCSTUBBUFFER iface
,void** ppv
) {
230 FIXME("(%p), stub!\n",ppv
);
234 CFStub_DebugServerRelease(LPRPCSTUBBUFFER iface
,void *pv
) {
235 FIXME("(%p), stub!\n",pv
);
238 static IRpcStubBufferVtbl cfstubvt
= {
239 CFStub_QueryInterface
,
245 CFStub_IsIIDSupported
,
247 CFStub_DebugServerQueryInterface
,
248 CFStub_DebugServerRelease
252 CFStub_Construct(LPRPCSTUBBUFFER
*ppv
) {
254 cfstub
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(CFStub
));
256 return E_OUTOFMEMORY
;
257 *ppv
= (LPRPCSTUBBUFFER
)cfstub
;
258 cfstub
->lpvtbl
= &cfstubvt
;
263 /* Since we create proxy buffers and classfactory in a pair, there is
264 * no need for 2 separate structs. Just put them in one, but remember
267 typedef struct _CFProxy
{
268 IClassFactoryVtbl
*lpvtbl_cf
;
269 IRpcProxyBufferVtbl
*lpvtbl_proxy
;
272 IRpcChannelBuffer
*chanbuf
;
273 IUnknown
*outer_unknown
;
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 InterlockedIncrement(&This
->ref
);
292 static ULONG WINAPI
IRpcProxyBufferImpl_Release(LPRPCPROXYBUFFER iface
) {
293 ICOM_THIS_MULTI(CFProxy
,lpvtbl_proxy
,iface
);
294 ULONG ref
= InterlockedDecrement(&This
->ref
);
297 IRpcChannelBuffer_Release(This
->chanbuf
);This
->chanbuf
= NULL
;
298 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
) {
320 ICOM_THIS_MULTI(CFProxy
,lpvtbl_proxy
,iface
);
321 if (This
->outer_unknown
) return IUnknown_QueryInterface(This
->outer_unknown
, riid
, ppv
);
323 if (IsEqualIID(&IID_IClassFactory
,riid
) || IsEqualIID(&IID_IUnknown
,riid
)) {
324 *ppv
= (LPVOID
)iface
;
325 IClassFactory_AddRef(iface
);
328 if (IsEqualIID(riid
,&IID_IMarshal
)) /* just to avoid debug output */
329 return E_NOINTERFACE
;
330 FIXME("Unhandled interface: %s\n",debugstr_guid(riid
));
331 return E_NOINTERFACE
;
334 static ULONG WINAPI
CFProxy_AddRef(LPCLASSFACTORY iface
) {
335 ICOM_THIS_MULTI(CFProxy
,lpvtbl_cf
,iface
);
336 if (This
->outer_unknown
) return IUnknown_AddRef(This
->outer_unknown
);
337 return InterlockedIncrement(&This
->ref
);
340 static ULONG WINAPI
CFProxy_Release(LPCLASSFACTORY iface
) {
342 ICOM_THIS_MULTI(CFProxy
,lpvtbl_cf
,iface
);
343 if (This
->outer_unknown
)
344 ref
= IUnknown_Release(This
->outer_unknown
);
346 ref
= InterlockedDecrement(&This
->ref
);
349 IRpcChannelBuffer_Release(This
->chanbuf
);
350 HeapFree(GetProcessHeap(),0,This
);
355 static HRESULT WINAPI
CFProxy_CreateInstance(
356 LPCLASSFACTORY iface
,
357 LPUNKNOWN pUnkOuter
,/* [in] */
358 REFIID riid
, /* [in] */
359 LPVOID
*ppv
/* [out] */
361 ICOM_THIS_MULTI(CFProxy
,lpvtbl_cf
,iface
);
368 TRACE("(%p,%s,%p)\n",pUnkOuter
,debugstr_guid(riid
),ppv
);
370 /* Send CreateInstance to the remote classfactory.
372 * Data: Only the 'IID'.
375 msg
.cbBuffer
= sizeof(*riid
);
377 hres
= IRpcChannelBuffer_GetBuffer(This
->chanbuf
,&msg
,&IID_IClassFactory
);
379 FIXME("IRpcChannelBuffer_GetBuffer failed with %lx?\n",hres
);
382 memcpy(msg
.Buffer
,riid
,sizeof(*riid
));
383 hres
= IRpcChannelBuffer_SendReceive(This
->chanbuf
,&msg
,&srstatus
);
385 FIXME("IRpcChannelBuffer_SendReceive failed with %lx?\n",hres
);
389 if (!msg
.cbBuffer
) /* interface not found on remote */
392 /* We got back: [Marshalled Interface data] */
393 TRACE("got %ld bytes data.\n",msg
.cbBuffer
);
394 hGlobal
= GlobalAlloc(GMEM_MOVEABLE
|GMEM_NODISCARD
|GMEM_SHARE
,msg
.cbBuffer
);
395 memcpy(GlobalLock(hGlobal
),msg
.Buffer
,msg
.cbBuffer
);
396 hres
= CreateStreamOnHGlobal(hGlobal
,TRUE
,&pStream
);
398 FIXME("CreateStreamOnHGlobal failed with %lx\n",hres
);
401 hres
= CoUnmarshalInterface(
406 IStream_Release(pStream
); /* Does GlobalFree hGlobal too. */
408 FIXME("CoMarshalInterface failed, %lx\n",hres
);
414 static HRESULT WINAPI
CFProxy_LockServer(LPCLASSFACTORY iface
,BOOL fLock
) {
415 /*ICOM_THIS_MULTI(CFProxy,lpvtbl_cf,iface);*/
416 FIXME("(%d), stub!\n",fLock
);
417 /* basically: write BOOL, read empty */
421 static IRpcProxyBufferVtbl pspbvtbl
= {
422 IRpcProxyBufferImpl_QueryInterface
,
423 IRpcProxyBufferImpl_AddRef
,
424 IRpcProxyBufferImpl_Release
,
425 IRpcProxyBufferImpl_Connect
,
426 IRpcProxyBufferImpl_Disconnect
428 static IClassFactoryVtbl cfproxyvt
= {
429 CFProxy_QueryInterface
,
432 CFProxy_CreateInstance
,
437 CFProxy_Construct(IUnknown
*pUnkOuter
, LPVOID
*ppv
,LPVOID
*ppProxy
) {
440 cf
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(CFProxy
));
442 return E_OUTOFMEMORY
;
444 cf
->lpvtbl_cf
= &cfproxyvt
;
445 cf
->lpvtbl_proxy
= &pspbvtbl
;
446 /* 1 reference for the proxy... */
448 cf
->outer_unknown
= pUnkOuter
;
449 *ppv
= &(cf
->lpvtbl_cf
);
450 *ppProxy
= &(cf
->lpvtbl_proxy
);
451 /* ...and 1 for the object */
452 IUnknown_AddRef((IUnknown
*)*ppv
);
457 /********************* OLE Proxy/Stub Factory ********************************/
458 static HRESULT WINAPI
459 PSFacBuf_QueryInterface(LPPSFACTORYBUFFER iface
, REFIID iid
, LPVOID
*ppv
) {
460 if (IsEqualIID(iid
,&IID_IPSFactoryBuffer
)||IsEqualIID(iid
,&IID_IUnknown
)) {
461 *ppv
= (LPVOID
)iface
;
462 /* No ref counting, static class */
465 FIXME("(%s) unknown IID?\n",debugstr_guid(iid
));
466 return E_NOINTERFACE
;
469 static ULONG WINAPI
PSFacBuf_AddRef(LPPSFACTORYBUFFER iface
) { return 2; }
470 static ULONG WINAPI
PSFacBuf_Release(LPPSFACTORYBUFFER iface
) { return 1; }
472 static HRESULT WINAPI
473 PSFacBuf_CreateProxy(
474 LPPSFACTORYBUFFER iface
, IUnknown
* pUnkOuter
, REFIID riid
,
475 IRpcProxyBuffer
**ppProxy
, LPVOID
*ppv
477 if (IsEqualIID(&IID_IClassFactory
,riid
) ||
478 IsEqualIID(&IID_IUnknown
,riid
)
480 return CFProxy_Construct(pUnkOuter
, ppv
,(LPVOID
*)ppProxy
);
481 FIXME("proxying not implemented for (%s) yet!\n",debugstr_guid(riid
));
485 static HRESULT WINAPI
487 LPPSFACTORYBUFFER iface
, REFIID riid
,IUnknown
*pUnkServer
,
488 IRpcStubBuffer
** ppStub
492 TRACE("(%s,%p,%p)\n",debugstr_guid(riid
),pUnkServer
,ppStub
);
494 if (IsEqualIID(&IID_IClassFactory
,riid
) ||
495 IsEqualIID(&IID_IUnknown
,riid
)
497 hres
= CFStub_Construct(ppStub
);
499 IRpcStubBuffer_Connect((*ppStub
),pUnkServer
);
502 FIXME("stubbing not implemented for (%s) yet!\n",debugstr_guid(riid
));
506 static IPSFactoryBufferVtbl psfacbufvtbl
= {
507 PSFacBuf_QueryInterface
,
510 PSFacBuf_CreateProxy
,
514 /* This is the whole PSFactoryBuffer object, just the vtableptr */
515 static IPSFactoryBufferVtbl
*lppsfac
= &psfacbufvtbl
;
517 /***********************************************************************
518 * DllGetClassObject [OLE32.@]
520 HRESULT WINAPI
OLE32_DllGetClassObject(REFCLSID rclsid
, REFIID iid
,LPVOID
*ppv
)
523 if (IsEqualIID(rclsid
,&CLSID_PSFactoryBuffer
)) {
527 if (IsEqualIID(rclsid
,&CLSID_DfMarshal
)&&(
528 IsEqualIID(iid
,&IID_IClassFactory
) ||
529 IsEqualIID(iid
,&IID_IUnknown
)
532 return MARSHAL_GetStandardMarshalCF(ppv
);
533 if (IsEqualIID(rclsid
,&CLSID_StdGlobalInterfaceTable
) && (IsEqualIID(iid
,&IID_IClassFactory
) || IsEqualIID(iid
,&IID_IUnknown
)))
534 return StdGlobalInterfaceTable_GetFactory(ppv
);
536 FIXME("\n\tCLSID:\t%s,\n\tIID:\t%s\n",debugstr_guid(rclsid
),debugstr_guid(iid
));
537 return CLASS_E_CLASSNOTAVAILABLE
;