Don't define BEGIN_INTERFACE in unknwn.h.
[wine] / dlls / ole32 / oleproxy.c
1 /*
2  *      OLE32 proxy/stub handler
3  *
4  *  Copyright 2002  Marcus Meissner
5  *
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.
10  *
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.
15  *
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
19  */
20
21 /* Documentation on MSDN:
22  *
23  * (Top level COM documentation)
24  * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnanchor/html/componentdevelopmentank.asp
25  *
26  * (COM Proxy)
27  * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/htm/comext_1q0p.asp
28  *
29  * (COM Stub)
30  * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/htm/comext_1lia.asp
31  *
32  * (Marshal)
33  * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/htm/comext_1gfn.asp
34  *
35  */
36
37 #include "config.h"
38
39 #include <stdlib.h>
40 #include <stdarg.h>
41 #include <stdio.h>
42 #include <string.h>
43
44 #define NONAMELESSUNION
45 #define NONAMELESSSTRUCT
46 #include "windef.h"
47 #include "winbase.h"
48 #include "winuser.h"
49 #include "objbase.h"
50 #include "ole2.h"
51 #include "rpc.h"
52 #include "winerror.h"
53 #include "winreg.h"
54 #include "wtypes.h"
55
56 #include "compobj_private.h"
57
58 #include "wine/debug.h"
59
60 WINE_DEFAULT_DEBUG_CHANNEL(ole);
61
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} };
64
65 /* From: http://msdn.microsoft.com/library/en-us/com/cmi_m_4lda.asp
66  *
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.
76  *
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.
84  */
85 typedef struct _CFStub {
86     IRpcStubBufferVtbl  *lpvtbl;
87     DWORD                       ref;
88
89     LPUNKNOWN                   pUnkServer;
90 } CFStub;
91
92 static HRESULT WINAPI
93 CFStub_QueryInterface(LPRPCSTUBBUFFER iface, REFIID riid, LPVOID *ppv) {
94     if (IsEqualIID(&IID_IUnknown,riid)||IsEqualIID(&IID_IRpcStubBuffer,riid)) {
95         *ppv = (LPVOID)iface;
96         IUnknown_AddRef(iface);
97         return S_OK;
98     }
99     FIXME("(%s), interface not supported.\n",debugstr_guid(riid));
100     return E_NOINTERFACE;
101 }
102
103 static ULONG WINAPI
104 CFStub_AddRef(LPRPCSTUBBUFFER iface) {
105     ICOM_THIS(CFStub,iface);
106
107     This->ref++;
108     return This->ref;
109 }
110
111 static ULONG WINAPI
112 CFStub_Release(LPRPCSTUBBUFFER iface) {
113     ICOM_THIS(CFStub,iface);
114
115     This->ref--;
116     if (This->ref)
117         return This->ref;
118     HeapFree(GetProcessHeap(),0,This);
119     return 0;
120 }
121
122 static HRESULT WINAPI
123 CFStub_Connect(LPRPCSTUBBUFFER iface, IUnknown *pUnkServer) {
124     ICOM_THIS(CFStub,iface);
125
126     This->pUnkServer = pUnkServer;
127     IUnknown_AddRef(pUnkServer);
128     return S_OK;
129 }
130
131 static void WINAPI
132 CFStub_Disconnect(LPRPCSTUBBUFFER iface) {
133     ICOM_THIS(CFStub,iface);
134
135     IUnknown_Release(This->pUnkServer);
136     This->pUnkServer = NULL;
137 }
138 static HRESULT WINAPI
139 CFStub_Invoke(
140     LPRPCSTUBBUFFER iface,RPCOLEMESSAGE* msg,IRpcChannelBuffer* chanbuf
141 ) {
142     ICOM_THIS(CFStub,iface);
143     HRESULT hres;
144
145     if (msg->iMethod == 3) { /* CreateInstance */
146         IID iid;
147         IClassFactory   *classfac;
148         IUnknown        *ppv;
149         IStream         *pStm;
150         STATSTG         ststg;
151         ULARGE_INTEGER  newpos;
152         LARGE_INTEGER   seekto;
153         ULONG           res;
154
155         if (msg->cbBuffer < sizeof(IID)) {
156             FIXME("Not enough bytes in buffer (%ld instead of %d)?\n",msg->cbBuffer,sizeof(IID));
157             return E_FAIL;
158         }
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);
162         if (hres) {
163             FIXME("Ole server does not provide a IClassFactory?\n");
164             return hres;
165         }
166         hres = IClassFactory_CreateInstance(classfac,NULL,&iid,(LPVOID*)&ppv);
167         IClassFactory_Release(classfac);
168         if (hres) {
169             msg->cbBuffer = 0;
170             FIXME("Failed to create an instance of %s\n",debugstr_guid(&iid));
171             return hres;
172         }
173         hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
174         if (hres) {
175             FIXME("Failed to create stream on hglobal\n");
176             return hres;
177         }
178         hres = CoMarshalInterface(pStm,&iid,ppv,0,NULL,0);
179         if (hres) {
180             FIXME("CoMarshalInterface failed, %lx!\n",hres);
181             msg->cbBuffer = 0;
182             return hres;
183         }
184         hres = IStream_Stat(pStm,&ststg,0);
185         if (hres) {
186             FIXME("Stat failed.\n");
187             return hres;
188         }
189
190         msg->cbBuffer = ststg.cbSize.u.LowPart;
191
192         if (msg->Buffer)
193             msg->Buffer = HeapReAlloc(GetProcessHeap(),0,msg->Buffer,ststg.cbSize.u.LowPart);
194         else
195             msg->Buffer = HeapAlloc(GetProcessHeap(),0,ststg.cbSize.u.LowPart);
196
197         seekto.u.LowPart = 0;seekto.u.HighPart = 0;
198         hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
199         if (hres) {
200             FIXME("IStream_Seek failed, %lx\n",hres);
201             return hres;
202         }
203         hres = IStream_Read(pStm,msg->Buffer,msg->cbBuffer,&res);
204         if (hres) {
205             FIXME("Stream Read failed, %lx\n",hres);
206             return hres;
207         }
208         IStream_Release(pStm);
209         return S_OK;
210     }
211     FIXME("(%p,%p), stub!\n",msg,chanbuf);
212     FIXME("iMethod is %ld\n",msg->iMethod);
213     FIXME("cbBuffer is %ld\n",msg->cbBuffer);
214     return E_FAIL;
215 }
216
217 static LPRPCSTUBBUFFER WINAPI
218 CFStub_IsIIDSupported(LPRPCSTUBBUFFER iface,REFIID riid) {
219     FIXME("(%s), stub!\n",debugstr_guid(riid));
220     return NULL;
221 }
222
223 static ULONG WINAPI
224 CFStub_CountRefs(LPRPCSTUBBUFFER iface) {
225     FIXME("(), stub!\n");
226     return 1;
227 }
228
229 static HRESULT WINAPI
230 CFStub_DebugServerQueryInterface(LPRPCSTUBBUFFER iface,void** ppv) {
231     FIXME("(%p), stub!\n",ppv);
232     return E_FAIL;
233 }
234 static void    WINAPI
235 CFStub_DebugServerRelease(LPRPCSTUBBUFFER iface,void *pv) {
236     FIXME("(%p), stub!\n",pv);
237 }
238
239 static IRpcStubBufferVtbl cfstubvt = {
240     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
241     CFStub_QueryInterface,
242     CFStub_AddRef,
243     CFStub_Release,
244     CFStub_Connect,
245     CFStub_Disconnect,
246     CFStub_Invoke,
247     CFStub_IsIIDSupported,
248     CFStub_CountRefs,
249     CFStub_DebugServerQueryInterface,
250     CFStub_DebugServerRelease
251 };
252
253 static HRESULT
254 CFStub_Construct(LPRPCSTUBBUFFER *ppv) {
255     CFStub *cfstub;
256     cfstub = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(CFStub));
257     if (!cfstub)
258         return E_OUTOFMEMORY;
259     *ppv = (LPRPCSTUBBUFFER)cfstub;
260     cfstub->lpvtbl      = &cfstubvt;
261     cfstub->ref         = 1;
262     return S_OK;
263 }
264
265 /* Since we create proxy buffers and classfactory in a pair, there is
266  * no need for 2 separate structs. Just put them in one, but remember
267  * the refcount.
268  */
269 typedef struct _CFProxy {
270     IClassFactoryVtbl           *lpvtbl_cf;
271     IRpcProxyBufferVtbl *lpvtbl_proxy;
272     DWORD                               ref;
273
274     IRpcChannelBuffer                   *chanbuf;
275 } CFProxy;
276
277 static HRESULT WINAPI IRpcProxyBufferImpl_QueryInterface(LPRPCPROXYBUFFER iface,REFIID riid,LPVOID *ppv) {
278     *ppv = NULL;
279     if (IsEqualIID(riid,&IID_IRpcProxyBuffer)||IsEqualIID(riid,&IID_IUnknown)) {
280         IRpcProxyBuffer_AddRef(iface);
281         *ppv = (LPVOID)iface;
282         return S_OK;
283     }
284     FIXME("(%s), no interface.\n",debugstr_guid(riid));
285     return E_NOINTERFACE;
286 }
287
288 static ULONG WINAPI IRpcProxyBufferImpl_AddRef(LPRPCPROXYBUFFER iface) {
289     ICOM_THIS_MULTI(CFProxy,lpvtbl_proxy,iface);
290     return ++(This->ref);
291 }
292
293 static ULONG WINAPI IRpcProxyBufferImpl_Release(LPRPCPROXYBUFFER iface) {
294     ICOM_THIS_MULTI(CFProxy,lpvtbl_proxy,iface);
295
296     if (!--(This->ref)) {
297         IRpcChannelBuffer_Release(This->chanbuf);This->chanbuf = NULL;
298         HeapFree(GetProcessHeap(),0,This);
299         return 0;
300     }
301     return This->ref;
302 }
303
304 static HRESULT WINAPI IRpcProxyBufferImpl_Connect(LPRPCPROXYBUFFER iface,IRpcChannelBuffer* pRpcChannelBuffer) {
305     ICOM_THIS_MULTI(CFProxy,lpvtbl_proxy,iface);
306
307     This->chanbuf = pRpcChannelBuffer;
308     IRpcChannelBuffer_AddRef(This->chanbuf);
309     return S_OK;
310 }
311 static void WINAPI IRpcProxyBufferImpl_Disconnect(LPRPCPROXYBUFFER iface) {
312     ICOM_THIS_MULTI(CFProxy,lpvtbl_proxy,iface);
313     if (This->chanbuf) {
314         IRpcChannelBuffer_Release(This->chanbuf);
315         This->chanbuf = NULL;
316     }
317 }
318
319 static HRESULT WINAPI
320 CFProxy_QueryInterface(LPCLASSFACTORY iface,REFIID riid, LPVOID *ppv) {
321     *ppv = NULL;
322     if (IsEqualIID(&IID_IClassFactory,riid) || IsEqualIID(&IID_IUnknown,riid)) {
323         *ppv = (LPVOID)iface;
324         IClassFactory_AddRef(iface);
325         return S_OK;
326     }
327     if (IsEqualIID(riid,&IID_IMarshal)) /* just to avoid debug output */
328         return E_NOINTERFACE;
329     FIXME("Unhandled interface: %s\n",debugstr_guid(riid));
330     return E_NOINTERFACE;
331 }
332
333 static ULONG   WINAPI CFProxy_AddRef(LPCLASSFACTORY iface) {
334     ICOM_THIS_MULTI(CFProxy,lpvtbl_cf,iface);
335     This->ref++;
336     return This->ref;
337 }
338
339 static ULONG   WINAPI CFProxy_Release(LPCLASSFACTORY iface) {
340     ICOM_THIS_MULTI(CFProxy,lpvtbl_cf,iface);
341     This->ref--;
342     if (This->ref)
343         return This->ref;
344     HeapFree(GetProcessHeap(),0,This);
345     return 0;
346 }
347
348 static HRESULT WINAPI CFProxy_CreateInstance(
349     LPCLASSFACTORY iface,
350     LPUNKNOWN pUnkOuter,/* [in] */
351     REFIID riid,        /* [in] */
352     LPVOID *ppv         /* [out] */
353 ) {
354     ICOM_THIS_MULTI(CFProxy,lpvtbl_cf,iface);
355     HRESULT             hres;
356     LPSTREAM            pStream;
357     HGLOBAL             hGlobal;
358     ULONG               srstatus;
359     RPCOLEMESSAGE       msg;
360
361     TRACE("(%p,%s,%p)\n",pUnkOuter,debugstr_guid(riid),ppv);
362
363     /* Send CreateInstance to the remote classfactory.
364      *
365      * Data: Only the 'IID'.
366      */
367     msg.iMethod  = 3;
368     msg.cbBuffer = sizeof(*riid);
369     msg.Buffer   = NULL;
370     hres = IRpcChannelBuffer_GetBuffer(This->chanbuf,&msg,&IID_IClassFactory);
371     if (hres) {
372         FIXME("IRpcChannelBuffer_GetBuffer failed with %lx?\n",hres);
373         return hres;
374     }
375     memcpy(msg.Buffer,riid,sizeof(*riid));
376     hres = IRpcChannelBuffer_SendReceive(This->chanbuf,&msg,&srstatus);
377     if (hres) {
378         FIXME("IRpcChannelBuffer_SendReceive failed with %lx?\n",hres);
379         return hres;
380     }
381
382     if (!msg.cbBuffer) /* interface not found on remote */
383         return srstatus;
384
385     /* We got back: [Marshalled Interface data] */
386     TRACE("got %ld bytes data.\n",msg.cbBuffer);
387     hGlobal = GlobalAlloc(GMEM_MOVEABLE|GMEM_NODISCARD|GMEM_SHARE,msg.cbBuffer);
388     memcpy(GlobalLock(hGlobal),msg.Buffer,msg.cbBuffer);
389     hres = CreateStreamOnHGlobal(hGlobal,TRUE,&pStream);
390     if (hres) {
391         FIXME("CreateStreamOnHGlobal failed with %lx\n",hres);
392         return hres;
393     }
394     hres = CoUnmarshalInterface(
395             pStream,
396             riid,
397             ppv
398     );
399     IStream_Release(pStream); /* Does GlobalFree hGlobal too. */
400     if (hres) {
401         FIXME("CoMarshalInterface failed, %lx\n",hres);
402         return hres;
403     }
404     return S_OK;
405 }
406
407 static HRESULT WINAPI CFProxy_LockServer(LPCLASSFACTORY iface,BOOL fLock) {
408     /*ICOM_THIS_MULTI(CFProxy,lpvtbl_cf,iface);*/
409     FIXME("(%d), stub!\n",fLock);
410     /* basically: write BOOL, read empty */
411     return S_OK;
412 }
413
414 static IRpcProxyBufferVtbl pspbvtbl = {
415     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
416     IRpcProxyBufferImpl_QueryInterface,
417     IRpcProxyBufferImpl_AddRef,
418     IRpcProxyBufferImpl_Release,
419     IRpcProxyBufferImpl_Connect,
420     IRpcProxyBufferImpl_Disconnect
421 };
422 static IClassFactoryVtbl cfproxyvt = {
423     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
424     CFProxy_QueryInterface,
425     CFProxy_AddRef,
426     CFProxy_Release,
427     CFProxy_CreateInstance,
428     CFProxy_LockServer
429 };
430
431 static HRESULT
432 CFProxy_Construct(LPVOID *ppv,LPVOID *ppProxy) {
433     CFProxy *cf;
434
435     cf = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(CFProxy));
436     if (!cf)
437         return E_OUTOFMEMORY;
438
439     cf->lpvtbl_cf       = &cfproxyvt;
440     cf->lpvtbl_proxy    = &pspbvtbl;
441     /* 1 reference for the proxy and 1 for the object */
442     cf->ref             = 2;
443     *ppv                = &(cf->lpvtbl_cf);
444     *ppProxy            = &(cf->lpvtbl_proxy);
445     return S_OK;
446 }
447
448
449 /********************* OLE Proxy/Stub Factory ********************************/
450 static HRESULT WINAPI
451 PSFacBuf_QueryInterface(LPPSFACTORYBUFFER iface, REFIID iid, LPVOID *ppv) {
452     if (IsEqualIID(iid,&IID_IPSFactoryBuffer)||IsEqualIID(iid,&IID_IUnknown)) {
453         *ppv = (LPVOID)iface;
454         /* No ref counting, static class */
455         return S_OK;
456     }
457     FIXME("(%s) unknown IID?\n",debugstr_guid(iid));
458     return E_NOINTERFACE;
459 }
460
461 static ULONG WINAPI PSFacBuf_AddRef(LPPSFACTORYBUFFER iface) { return 2; }
462 static ULONG WINAPI PSFacBuf_Release(LPPSFACTORYBUFFER iface) { return 1; }
463
464 static HRESULT WINAPI
465 PSFacBuf_CreateProxy(
466     LPPSFACTORYBUFFER iface, IUnknown* pUnkOuter, REFIID riid,
467     IRpcProxyBuffer **ppProxy, LPVOID *ppv
468 ) {
469     if (IsEqualIID(&IID_IClassFactory,riid) ||
470         IsEqualIID(&IID_IUnknown,riid)
471     )
472         return CFProxy_Construct(ppv,(LPVOID*)ppProxy);
473     FIXME("proxying not implemented for (%s) yet!\n",debugstr_guid(riid));
474     return E_FAIL;
475 }
476
477 static HRESULT WINAPI
478 PSFacBuf_CreateStub(
479     LPPSFACTORYBUFFER iface, REFIID riid,IUnknown *pUnkServer,
480     IRpcStubBuffer** ppStub
481 ) {
482     HRESULT hres;
483
484     TRACE("(%s,%p,%p)\n",debugstr_guid(riid),pUnkServer,ppStub);
485
486     if (IsEqualIID(&IID_IClassFactory,riid) ||
487         IsEqualIID(&IID_IUnknown,riid)
488     ) {
489         hres = CFStub_Construct(ppStub);
490         if (!hres)
491             IRpcStubBuffer_Connect((*ppStub),pUnkServer);
492         return hres;
493     }
494     FIXME("stubbing not implemented for (%s) yet!\n",debugstr_guid(riid));
495     return E_FAIL;
496 }
497
498 static IPSFactoryBufferVtbl psfacbufvtbl = {
499     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
500     PSFacBuf_QueryInterface,
501     PSFacBuf_AddRef,
502     PSFacBuf_Release,
503     PSFacBuf_CreateProxy,
504     PSFacBuf_CreateStub
505 };
506
507 /* This is the whole PSFactoryBuffer object, just the vtableptr */
508 static IPSFactoryBufferVtbl *lppsfac = &psfacbufvtbl;
509
510 /***********************************************************************
511  *           DllGetClassObject [OLE32.@]
512  */
513 HRESULT WINAPI OLE32_DllGetClassObject(REFCLSID rclsid, REFIID iid,LPVOID *ppv)
514 {
515     *ppv = NULL;
516     if (IsEqualIID(rclsid,&CLSID_PSFactoryBuffer)) {
517         *ppv = &lppsfac;
518         return S_OK;
519     }
520     if (IsEqualIID(rclsid,&CLSID_DfMarshal)&&(
521                 IsEqualIID(iid,&IID_IClassFactory) ||
522                 IsEqualIID(iid,&IID_IUnknown)
523         )
524     )
525         return MARSHAL_GetStandardMarshalCF(ppv);
526     if (IsEqualIID(rclsid,&CLSID_StdGlobalInterfaceTable) && (IsEqualIID(iid,&IID_IClassFactory) || IsEqualIID(iid,&IID_IUnknown)))
527         return StdGlobalInterfaceTable_GetFactory(ppv);
528
529     FIXME("\n\tCLSID:\t%s,\n\tIID:\t%s\n",debugstr_guid(rclsid),debugstr_guid(iid));
530     return CLASS_E_CLASSNOTAVAILABLE;
531 }