Define NONAMELESS{STRUCT,UNION} explicitly in the files that need them.
[wine/wine-kai.git] / dlls / ole32 / oleproxy.c
blob2302b8cb503ac01549d6f0b065c4a8aec27a310a
1 /*
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 * (COM Proxy)
24 * http://msdn.microsoft.com/library/en-us/com/comext_1q0p.asp
26 * (COM Stub)
27 * http://msdn.microsoft.com/library/en-us/com/comext_1lia.asp
29 * (Marshal)
30 * http://msdn.microsoft.com/library/en-us/com/comext_1gfn.asp
34 #include "config.h"
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <string.h>
40 #define NONAMELESSUNION
41 #define NONAMELESSSTRUCT
42 #include "windef.h"
43 #include "objbase.h"
44 #include "ole2.h"
45 #include "rpc.h"
46 #include "winerror.h"
47 #include "winreg.h"
48 #include "wtypes.h"
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;
81 DWORD ref;
83 LPUNKNOWN pUnkServer;
84 } CFStub;
86 static HRESULT WINAPI
87 CFStub_QueryInterface(LPRPCSTUBBUFFER iface, REFIID riid, LPVOID *ppv) {
88 if (IsEqualIID(&IID_IUnknown,riid)||IsEqualIID(&IID_IRpcStubBuffer,riid)) {
89 *ppv = (LPVOID)iface;
90 IUnknown_AddRef(iface);
91 return S_OK;
93 FIXME("(%s), interface not supported.\n",debugstr_guid(riid));
94 return E_NOINTERFACE;
97 static ULONG WINAPI
98 CFStub_AddRef(LPRPCSTUBBUFFER iface) {
99 ICOM_THIS(CFStub,iface);
101 This->ref++;
102 return This->ref;
105 static ULONG WINAPI
106 CFStub_Release(LPRPCSTUBBUFFER iface) {
107 ICOM_THIS(CFStub,iface);
109 This->ref--;
110 if (This->ref)
111 return This->ref;
112 HeapFree(GetProcessHeap(),0,This);
113 return 0;
116 static HRESULT WINAPI
117 CFStub_Connect(LPRPCSTUBBUFFER iface, IUnknown *pUnkServer) {
118 ICOM_THIS(CFStub,iface);
120 This->pUnkServer = pUnkServer;
121 IUnknown_AddRef(pUnkServer);
122 return S_OK;
125 static void WINAPI
126 CFStub_Disconnect(LPRPCSTUBBUFFER iface) {
127 ICOM_THIS(CFStub,iface);
129 IUnknown_Release(This->pUnkServer);
130 This->pUnkServer = NULL;
132 static HRESULT WINAPI
133 CFStub_Invoke(
134 LPRPCSTUBBUFFER iface,RPCOLEMESSAGE* msg,IRpcChannelBuffer* chanbuf
136 ICOM_THIS(CFStub,iface);
137 HRESULT hres;
139 if (msg->iMethod == 3) { /* CreateInstance */
140 IID iid;
141 IClassFactory *classfac;
142 IUnknown *ppv;
143 IStream *pStm;
144 STATSTG ststg;
145 ULARGE_INTEGER newpos;
146 LARGE_INTEGER seekto;
147 ULONG res;
149 if (msg->cbBuffer < sizeof(IID)) {
150 FIXME("Not enough bytes in buffer (%ld instead of %d)?\n",msg->cbBuffer,sizeof(IID));
151 return E_FAIL;
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);
156 if (hres) {
157 FIXME("Ole server does not provide a IClassFactory?\n");
158 return hres;
160 hres = IClassFactory_CreateInstance(classfac,NULL,&iid,(LPVOID*)&ppv);
161 IClassFactory_Release(classfac);
162 if (hres) {
163 msg->cbBuffer = 0;
164 FIXME("Failed to create an instance of %s\n",debugstr_guid(&iid));
165 return hres;
167 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
168 if (hres) {
169 FIXME("Failed to create stream on hglobal\n");
170 return hres;
172 hres = CoMarshalInterface(pStm,&iid,ppv,0,NULL,0);
173 if (hres) {
174 FIXME("CoMarshalInterface failed, %lx!\n",hres);
175 msg->cbBuffer = 0;
176 return hres;
178 hres = IStream_Stat(pStm,&ststg,0);
179 if (hres) {
180 FIXME("Stat failed.\n");
181 return hres;
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);
188 if (hres) {
189 FIXME("IStream_Seek failed, %lx\n",hres);
190 return hres;
192 hres = IStream_Read(pStm,msg->Buffer,msg->cbBuffer,&res);
193 if (hres) {
194 FIXME("Stream Read failed, %lx\n",hres);
195 return hres;
197 IStream_Release(pStm);
198 return S_OK;
200 FIXME("(%p,%p), stub!\n",msg,chanbuf);
201 FIXME("iMethod is %ld\n",msg->iMethod);
202 FIXME("cbBuffer is %ld\n",msg->cbBuffer);
203 return E_FAIL;
206 static LPRPCSTUBBUFFER WINAPI
207 CFStub_IsIIDSupported(LPRPCSTUBBUFFER iface,REFIID riid) {
208 FIXME("(%s), stub!\n",debugstr_guid(riid));
209 return NULL;
212 static ULONG WINAPI
213 CFStub_CountRefs(LPRPCSTUBBUFFER iface) {
214 FIXME("(), stub!\n");
215 return 1;
218 static HRESULT WINAPI
219 CFStub_DebugServerQueryInterface(LPRPCSTUBBUFFER iface,void** ppv) {
220 FIXME("(%p), stub!\n",ppv);
221 return E_FAIL;
223 static void WINAPI
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,
231 CFStub_AddRef,
232 CFStub_Release,
233 CFStub_Connect,
234 CFStub_Disconnect,
235 CFStub_Invoke,
236 CFStub_IsIIDSupported,
237 CFStub_CountRefs,
238 CFStub_DebugServerQueryInterface,
239 CFStub_DebugServerRelease
242 static HRESULT
243 CFStub_Construct(LPRPCSTUBBUFFER *ppv) {
244 CFStub *cfstub;
245 cfstub = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(CFStub));
246 if (!cfstub)
247 return E_OUTOFMEMORY;
248 *ppv = (LPRPCSTUBBUFFER)cfstub;
249 cfstub->lpvtbl = &cfstubvt;
250 cfstub->ref = 1;
251 return S_OK;
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
256 * the refcount.
258 typedef struct _CFProxy {
259 ICOM_VTABLE(IClassFactory) *lpvtbl_cf;
260 ICOM_VTABLE(IRpcProxyBuffer) *lpvtbl_proxy;
261 DWORD ref;
263 IRpcChannelBuffer *chanbuf;
264 } CFProxy;
266 static HRESULT WINAPI IRpcProxyBufferImpl_QueryInterface(LPRPCPROXYBUFFER iface,REFIID riid,LPVOID *ppv) {
267 *ppv = NULL;
268 if (IsEqualIID(riid,&IID_IRpcProxyBuffer)||IsEqualIID(riid,&IID_IUnknown)) {
269 IRpcProxyBuffer_AddRef(iface);
270 *ppv = (LPVOID)iface;
271 return S_OK;
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);
288 return 0;
290 return This->ref;
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);
298 return S_OK;
300 static void WINAPI IRpcProxyBufferImpl_Disconnect(LPRPCPROXYBUFFER iface) {
301 ICOM_THIS_MULTI(CFProxy,lpvtbl_proxy,iface);
302 if (This->chanbuf) {
303 IRpcChannelBuffer_Release(This->chanbuf);
304 This->chanbuf = NULL;
308 static HRESULT WINAPI
309 CFProxy_QueryInterface(LPCLASSFACTORY iface,REFIID riid, LPVOID *ppv) {
310 *ppv = NULL;
311 if (IsEqualIID(&IID_IClassFactory,riid) || IsEqualIID(&IID_IUnknown,riid)) {
312 *ppv = (LPVOID)iface;
313 IClassFactory_AddRef(iface);
314 return S_OK;
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);
324 This->ref++;
325 return This->ref;
328 static ULONG WINAPI CFProxy_Release(LPCLASSFACTORY iface) {
329 ICOM_THIS_MULTI(CFProxy,lpvtbl_cf,iface);
330 This->ref--;
331 if (This->ref)
332 return This->ref;
333 HeapFree(GetProcessHeap(),0,This);
334 return 0;
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);
344 HRESULT hres;
345 LPSTREAM pStream;
346 HGLOBAL hGlobal;
347 ULONG srstatus;
348 RPCOLEMESSAGE msg;
350 TRACE("(%p,%s,%p)\n",pUnkOuter,debugstr_guid(riid),ppv);
352 /* Send CreateInstance to the remote classfactory.
354 * Data: Only the 'IID'.
356 msg.iMethod = 3;
357 msg.cbBuffer = sizeof(*riid);
358 msg.Buffer = NULL;
359 hres = IRpcChannelBuffer_GetBuffer(This->chanbuf,&msg,&IID_IClassFactory);
360 if (hres) {
361 FIXME("IRpcChannelBuffer_GetBuffer failed with %lx?\n",hres);
362 return hres;
364 memcpy(msg.Buffer,riid,sizeof(*riid));
365 hres = IRpcChannelBuffer_SendReceive(This->chanbuf,&msg,&srstatus);
366 if (hres) {
367 FIXME("IRpcChannelBuffer_SendReceive failed with %lx?\n",hres);
368 return hres;
371 if (!msg.cbBuffer) /* interface not found on remote */
372 return srstatus;
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);
379 if (hres) {
380 FIXME("CreateStreamOnHGlobal failed with %lx\n",hres);
381 return hres;
383 hres = CoUnmarshalInterface(
384 pStream,
385 riid,
388 IStream_Release(pStream); /* Does GlobalFree hGlobal too. */
389 if (hres) {
390 FIXME("CoMarshalInterface failed, %lx\n",hres);
391 return hres;
393 return S_OK;
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 */
400 return S_OK;
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,
414 CFProxy_AddRef,
415 CFProxy_Release,
416 CFProxy_CreateInstance,
417 CFProxy_LockServer
420 static HRESULT
421 CFProxy_Construct(LPVOID *ppv,LPVOID *ppProxy) {
422 CFProxy *cf;
424 cf = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(CFProxy));
425 if (!cf)
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);
433 return S_OK;
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 */
443 return S_OK;
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));
462 return E_FAIL;
465 static HRESULT WINAPI
466 PSFacBuf_CreateStub(
467 LPPSFACTORYBUFFER iface, REFIID riid,IUnknown *pUnkServer,
468 IRpcStubBuffer** ppStub
470 HRESULT hres;
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);
478 if (!hres)
479 IRpcStubBuffer_Connect((*ppStub),pUnkServer);
480 return hres;
482 FIXME("stubbing not implemented for (%s) yet!\n",debugstr_guid(riid));
483 return E_FAIL;
486 static ICOM_VTABLE(IPSFactoryBuffer) psfacbufvtbl = {
487 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
488 PSFacBuf_QueryInterface,
489 PSFacBuf_AddRef,
490 PSFacBuf_Release,
491 PSFacBuf_CreateProxy,
492 PSFacBuf_CreateStub
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)
503 *ppv = NULL;
504 if (IsEqualIID(rclsid,&CLSID_PSFactoryBuffer)) {
505 *ppv = &lppsfac;
506 /* If we create a ps factory, we might need a stub manager later
507 * anyway
509 STUBMGR_Start();
510 return S_OK;
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;