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