kernel32: Add the directory the executable was loaded from to the module search path...
[wine] / dlls / ole32 / oleproxy.c
1 /*
2  *      OLE32 proxy/stub handler
3  *
4  *  Copyright 2002  Marcus Meissner
5  *  Copyright 2001  Ove Kåven, TransGaming Technologies
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 /* Documentation on MSDN:
23  *
24  * (Top level COM documentation)
25  * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnanchor/html/componentdevelopmentank.asp
26  *
27  * (COM Proxy)
28  * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/htm/comext_1q0p.asp
29  *
30  * (COM Stub)
31  * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/htm/comext_1lia.asp
32  *
33  * (Marshal)
34  * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/htm/comext_1gfn.asp
35  *
36  */
37
38 #include "config.h"
39
40 #include <stdlib.h>
41 #include <stdarg.h>
42 #include <stdio.h>
43 #include <string.h>
44
45 #define COBJMACROS
46 #define NONAMELESSUNION
47 #define NONAMELESSSTRUCT
48
49 #include "windef.h"
50 #include "winbase.h"
51 #include "winuser.h"
52 #include "objbase.h"
53 #include "ole2.h"
54 #include "rpc.h"
55 #include "winerror.h"
56 #include "winreg.h"
57 #include "wtypes.h"
58
59 #include "compobj_private.h"
60 #include "moniker.h"
61
62 #include "wine/debug.h"
63
64 WINE_DEFAULT_DEBUG_CHANNEL(ole);
65
66 static ULONG WINAPI RURpcProxyBufferImpl_Release(LPRPCPROXYBUFFER iface);
67
68 /* From: http://msdn.microsoft.com/library/en-us/com/cmi_m_4lda.asp
69  *
70  * The first time a client requests a pointer to an interface on a
71  * particular object, COM loads an IClassFactory stub in the server
72  * process and uses it to marshal the first pointer back to the
73  * client. In the client process, COM loads the generic proxy for the
74  * class factory object and calls its implementation of IMarshal to
75  * unmarshal that first pointer. COM then creates the first interface
76  * proxy and hands it a pointer to the RPC channel. Finally, COM returns
77  * the IClassFactory pointer to the client, which uses it to call
78  * IClassFactory::CreateInstance, passing it a reference to the interface.
79  *
80  * Back in the server process, COM now creates a new instance of the
81  * object, along with a stub for the requested interface. This stub marshals
82  * the interface pointer back to the client process, where another object
83  * proxy is created, this time for the object itself. Also created is a
84  * proxy for the requested interface, a pointer to which is returned to
85  * the client. With subsequent calls to other interfaces on the object,
86  * COM will load the appropriate interface stubs and proxies as needed.
87  */
88 typedef struct _CFStub {
89     const IRpcStubBufferVtbl   *lpvtbl;
90     LONG                        ref;
91
92     LPUNKNOWN                   pUnkServer;
93 } CFStub;
94
95 static HRESULT WINAPI
96 CFStub_QueryInterface(LPRPCSTUBBUFFER iface, REFIID riid, LPVOID *ppv) {
97     if (IsEqualIID(&IID_IUnknown,riid)||IsEqualIID(&IID_IRpcStubBuffer,riid)) {
98         *ppv = (LPVOID)iface;
99         IUnknown_AddRef(iface);
100         return S_OK;
101     }
102     FIXME("(%s), interface not supported.\n",debugstr_guid(riid));
103     return E_NOINTERFACE;
104 }
105
106 static ULONG WINAPI
107 CFStub_AddRef(LPRPCSTUBBUFFER iface) {
108     CFStub *This = (CFStub *)iface;
109     return InterlockedIncrement(&This->ref);
110 }
111
112 static ULONG WINAPI
113 CFStub_Release(LPRPCSTUBBUFFER iface) {
114     CFStub *This = (CFStub *)iface;
115     ULONG ref;
116
117     ref = InterlockedDecrement(&This->ref);
118     if (!ref) {
119         IRpcStubBuffer_Disconnect(iface);
120         HeapFree(GetProcessHeap(),0,This);
121     }
122     return ref;
123 }
124
125 static HRESULT WINAPI
126 CFStub_Connect(LPRPCSTUBBUFFER iface, IUnknown *pUnkServer) {
127     CFStub *This = (CFStub *)iface;
128
129     This->pUnkServer = pUnkServer;
130     IUnknown_AddRef(pUnkServer);
131     return S_OK;
132 }
133
134 static void WINAPI
135 CFStub_Disconnect(LPRPCSTUBBUFFER iface) {
136     CFStub *This = (CFStub *)iface;
137
138     if (This->pUnkServer) {
139         IUnknown_Release(This->pUnkServer);
140         This->pUnkServer = NULL;
141     }
142 }
143
144 static HRESULT WINAPI
145 CFStub_Invoke(
146     LPRPCSTUBBUFFER iface,RPCOLEMESSAGE* msg,IRpcChannelBuffer* chanbuf
147 ) {
148     CFStub *This = (CFStub *)iface;
149     HRESULT hres;
150
151     if (msg->iMethod == 3) { /* CreateInstance */
152         IID iid;
153         IClassFactory   *classfac;
154         IUnknown        *ppv;
155         IStream         *pStm;
156         STATSTG         ststg;
157         ULARGE_INTEGER  newpos;
158         LARGE_INTEGER   seekto;
159         ULONG           res;
160
161         if (msg->cbBuffer < sizeof(IID)) {
162             FIXME("Not enough bytes in buffer (%d)?\n",msg->cbBuffer);
163             return E_FAIL;
164         }
165         memcpy(&iid,msg->Buffer,sizeof(iid));
166         TRACE("->CreateInstance(%s)\n",debugstr_guid(&iid));
167         hres = IUnknown_QueryInterface(This->pUnkServer,&IID_IClassFactory,(LPVOID*)&classfac);
168         if (hres) {
169             FIXME("Ole server does not provide an IClassFactory?\n");
170             return hres;
171         }
172         hres = IClassFactory_CreateInstance(classfac,NULL,&iid,(LPVOID*)&ppv);
173         IClassFactory_Release(classfac);
174         msg->cbBuffer = 0;
175         if (hres) {
176             FIXME("Failed to create an instance of %s\n",debugstr_guid(&iid));
177             goto getbuffer;
178         }
179         hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
180         if (hres) {
181             FIXME("Failed to create stream on hglobal\n");
182             goto getbuffer;
183         }
184         hres = IStream_Write(pStm, &ppv, sizeof(ppv), NULL);
185         if (hres) {
186            ERR("IStream_Write failed, 0x%08x\n", hres);
187            goto getbuffer;
188         }
189         if (ppv) {
190         hres = CoMarshalInterface(pStm,&iid,ppv,0,NULL,0);
191         IUnknown_Release(ppv);
192         if (hres) {
193             FIXME("CoMarshalInterface failed, %x!\n",hres);
194             goto getbuffer;
195         }
196     }
197         hres = IStream_Stat(pStm,&ststg,0);
198         if (hres) {
199             FIXME("Stat failed.\n");
200             goto getbuffer;
201         }
202
203         msg->cbBuffer = ststg.cbSize.u.LowPart;
204
205 getbuffer:
206         IRpcChannelBuffer_GetBuffer(chanbuf, msg, &IID_IClassFactory);
207         if (hres) return hres;
208
209         seekto.u.LowPart = 0;seekto.u.HighPart = 0;
210         hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
211         if (hres) {
212             FIXME("IStream_Seek failed, %x\n",hres);
213             return hres;
214         }
215         hres = IStream_Read(pStm,msg->Buffer,msg->cbBuffer,&res);
216         if (hres) {
217             FIXME("Stream Read failed, %x\n",hres);
218             return hres;
219         }
220         IStream_Release(pStm);
221         return S_OK;
222     }
223     FIXME("(%p,%p), stub!\n",msg,chanbuf);
224     FIXME("iMethod is %d\n",msg->iMethod);
225     FIXME("cbBuffer is %d\n",msg->cbBuffer);
226     return E_FAIL;
227 }
228
229 static LPRPCSTUBBUFFER WINAPI
230 CFStub_IsIIDSupported(LPRPCSTUBBUFFER iface,REFIID riid) {
231     FIXME("(%s), stub!\n",debugstr_guid(riid));
232     return NULL;
233 }
234
235 static ULONG WINAPI
236 CFStub_CountRefs(LPRPCSTUBBUFFER iface) {
237     FIXME("(), stub!\n");
238     return 1;
239 }
240
241 static HRESULT WINAPI
242 CFStub_DebugServerQueryInterface(LPRPCSTUBBUFFER iface,void** ppv) {
243     FIXME("(%p), stub!\n",ppv);
244     return E_FAIL;
245 }
246 static void    WINAPI
247 CFStub_DebugServerRelease(LPRPCSTUBBUFFER iface,void *pv) {
248     FIXME("(%p), stub!\n",pv);
249 }
250
251 static const IRpcStubBufferVtbl cfstubvt = {
252     CFStub_QueryInterface,
253     CFStub_AddRef,
254     CFStub_Release,
255     CFStub_Connect,
256     CFStub_Disconnect,
257     CFStub_Invoke,
258     CFStub_IsIIDSupported,
259     CFStub_CountRefs,
260     CFStub_DebugServerQueryInterface,
261     CFStub_DebugServerRelease
262 };
263
264 static HRESULT
265 CFStub_Construct(LPRPCSTUBBUFFER *ppv) {
266     CFStub *cfstub;
267     cfstub = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(CFStub));
268     if (!cfstub)
269         return E_OUTOFMEMORY;
270     *ppv = (LPRPCSTUBBUFFER)cfstub;
271     cfstub->lpvtbl      = &cfstubvt;
272     cfstub->ref         = 1;
273     return S_OK;
274 }
275
276 /* Since we create proxy buffers and classfactory in a pair, there is
277  * no need for 2 separate structs. Just put them in one, but remember
278  * the refcount.
279  */
280 typedef struct _CFProxy {
281     const IClassFactoryVtbl             *lpvtbl_cf;
282     const IRpcProxyBufferVtbl   *lpvtbl_proxy;
283     LONG                                ref;
284
285     IRpcChannelBuffer                   *chanbuf;
286     IUnknown *outer_unknown;
287 } CFProxy;
288
289 static HRESULT WINAPI IRpcProxyBufferImpl_QueryInterface(LPRPCPROXYBUFFER iface,REFIID riid,LPVOID *ppv) {
290     *ppv = NULL;
291     if (IsEqualIID(riid,&IID_IRpcProxyBuffer)||IsEqualIID(riid,&IID_IUnknown)) {
292         IRpcProxyBuffer_AddRef(iface);
293         *ppv = (LPVOID)iface;
294         return S_OK;
295     }
296     FIXME("(%s), no interface.\n",debugstr_guid(riid));
297     return E_NOINTERFACE;
298 }
299
300 static ULONG WINAPI IRpcProxyBufferImpl_AddRef(LPRPCPROXYBUFFER iface) {
301     ICOM_THIS_MULTI(CFProxy,lpvtbl_proxy,iface);
302     return InterlockedIncrement(&This->ref);
303 }
304
305 static ULONG WINAPI IRpcProxyBufferImpl_Release(LPRPCPROXYBUFFER iface) {
306     ICOM_THIS_MULTI(CFProxy,lpvtbl_proxy,iface);
307     ULONG ref = InterlockedDecrement(&This->ref);
308
309     if (!ref) {
310         IRpcProxyBuffer_Disconnect(iface);
311         HeapFree(GetProcessHeap(),0,This);
312     }
313     return ref;
314 }
315
316 static HRESULT WINAPI IRpcProxyBufferImpl_Connect(LPRPCPROXYBUFFER iface,IRpcChannelBuffer* pRpcChannelBuffer) {
317     ICOM_THIS_MULTI(CFProxy,lpvtbl_proxy,iface);
318
319     This->chanbuf = pRpcChannelBuffer;
320     IRpcChannelBuffer_AddRef(This->chanbuf);
321     return S_OK;
322 }
323 static void WINAPI IRpcProxyBufferImpl_Disconnect(LPRPCPROXYBUFFER iface) {
324     ICOM_THIS_MULTI(CFProxy,lpvtbl_proxy,iface);
325     if (This->chanbuf) {
326         IRpcChannelBuffer_Release(This->chanbuf);
327         This->chanbuf = NULL;
328     }
329 }
330
331 static HRESULT WINAPI
332 CFProxy_QueryInterface(LPCLASSFACTORY iface,REFIID riid, LPVOID *ppv) {
333     ICOM_THIS_MULTI(CFProxy,lpvtbl_cf,iface);
334     if (This->outer_unknown) return IUnknown_QueryInterface(This->outer_unknown, riid, ppv);
335     *ppv = NULL;
336     if (IsEqualIID(&IID_IClassFactory,riid) || IsEqualIID(&IID_IUnknown,riid)) {
337         *ppv = (LPVOID)iface;
338         IClassFactory_AddRef(iface);
339         return S_OK;
340     }
341     if (IsEqualIID(riid,&IID_IMarshal)) /* just to avoid debug output */
342         return E_NOINTERFACE;
343     FIXME("Unhandled interface: %s\n",debugstr_guid(riid));
344     return E_NOINTERFACE;
345 }
346
347 static ULONG   WINAPI CFProxy_AddRef(LPCLASSFACTORY iface) {
348     ICOM_THIS_MULTI(CFProxy,lpvtbl_cf,iface);
349     if (This->outer_unknown) return IUnknown_AddRef(This->outer_unknown);
350     return InterlockedIncrement(&This->ref);
351 }
352
353 static ULONG   WINAPI CFProxy_Release(LPCLASSFACTORY iface) {
354     ICOM_THIS_MULTI(CFProxy,lpvtbl_cf,iface);
355     if (This->outer_unknown)
356         return IUnknown_Release(This->outer_unknown);
357     else
358         return IRpcProxyBufferImpl_Release((IRpcProxyBuffer *)&This->lpvtbl_proxy);
359 }
360
361 static HRESULT WINAPI CFProxy_CreateInstance(
362     LPCLASSFACTORY iface,
363     LPUNKNOWN pUnkOuter,/* [in] */
364     REFIID riid,        /* [in] */
365     LPVOID *ppv         /* [out] */
366 ) {
367     ICOM_THIS_MULTI(CFProxy,lpvtbl_cf,iface);
368     HRESULT             hres;
369     LPSTREAM            pStream;
370     HGLOBAL             hGlobal;
371     ULONG               srstatus;
372     RPCOLEMESSAGE       msg;
373
374     TRACE("(%p,%s,%p)\n",pUnkOuter,debugstr_guid(riid),ppv);
375
376     /* Send CreateInstance to the remote classfactory.
377      *
378      * Data: Only the 'IID'.
379      */
380     msg.iMethod  = 3;
381     msg.cbBuffer = sizeof(*riid);
382     msg.Buffer   = NULL;
383     hres = IRpcChannelBuffer_GetBuffer(This->chanbuf,&msg,&IID_IClassFactory);
384     if (hres) {
385         FIXME("IRpcChannelBuffer_GetBuffer failed with %x?\n",hres);
386         return hres;
387     }
388     memcpy(msg.Buffer,riid,sizeof(*riid));
389     hres = IRpcChannelBuffer_SendReceive(This->chanbuf,&msg,&srstatus);
390     if (hres) {
391         FIXME("IRpcChannelBuffer_SendReceive failed with %x?\n",hres);
392         IRpcChannelBuffer_FreeBuffer(This->chanbuf,&msg);
393         return hres;
394     }
395
396     if (!msg.cbBuffer) { /* interface not found on remote */
397         IRpcChannelBuffer_FreeBuffer(This->chanbuf,&msg);
398         return srstatus;
399     }
400
401     /* We got back: [Marshalled Interface data] */
402     TRACE("got %d bytes data.\n",msg.cbBuffer);
403     hGlobal = GlobalAlloc(GMEM_MOVEABLE|GMEM_NODISCARD|GMEM_SHARE,msg.cbBuffer);
404     memcpy(GlobalLock(hGlobal),msg.Buffer,msg.cbBuffer);
405     hres = CreateStreamOnHGlobal(hGlobal,TRUE,&pStream);
406     if (hres) {
407         FIXME("CreateStreamOnHGlobal failed with %x\n",hres);
408         IRpcChannelBuffer_FreeBuffer(This->chanbuf,&msg);
409         return hres;
410     }
411     hres = IStream_Read(pStream, ppv, sizeof(*ppv), NULL);
412     if (hres != S_OK)
413         hres = E_FAIL;
414     else if (*ppv) {
415         hres = CoUnmarshalInterface(
416                pStream,
417                riid,
418                ppv
419         );
420     }
421     IStream_Release(pStream); /* Does GlobalFree hGlobal too. */
422
423     IRpcChannelBuffer_FreeBuffer(This->chanbuf,&msg);
424
425     if (hres) {
426         FIXME("CoMarshalInterface failed, %x\n",hres);
427         return hres;
428     }
429     return S_OK;
430 }
431
432 static HRESULT WINAPI CFProxy_LockServer(LPCLASSFACTORY iface,BOOL fLock) {
433     /*ICOM_THIS_MULTI(CFProxy,lpvtbl_cf,iface);*/
434     FIXME("(%d), stub!\n",fLock);
435     /* basically: write BOOL, read empty */
436     return S_OK;
437 }
438
439 static const IRpcProxyBufferVtbl pspbvtbl = {
440     IRpcProxyBufferImpl_QueryInterface,
441     IRpcProxyBufferImpl_AddRef,
442     IRpcProxyBufferImpl_Release,
443     IRpcProxyBufferImpl_Connect,
444     IRpcProxyBufferImpl_Disconnect
445 };
446 static const IClassFactoryVtbl cfproxyvt = {
447     CFProxy_QueryInterface,
448     CFProxy_AddRef,
449     CFProxy_Release,
450     CFProxy_CreateInstance,
451     CFProxy_LockServer
452 };
453
454 static HRESULT
455 CFProxy_Construct(IUnknown *pUnkOuter, LPVOID *ppv,LPVOID *ppProxy) {
456     CFProxy *cf;
457
458     cf = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(CFProxy));
459     if (!cf)
460         return E_OUTOFMEMORY;
461
462     cf->lpvtbl_cf       = &cfproxyvt;
463     cf->lpvtbl_proxy    = &pspbvtbl;
464     /* one reference for the proxy buffer */
465     cf->ref             = 1;
466     cf->outer_unknown = pUnkOuter;
467     *ppv                = &(cf->lpvtbl_cf);
468     *ppProxy            = &(cf->lpvtbl_proxy);
469     /* and one reference for the object */
470     IUnknown_AddRef((IUnknown *)*ppv);
471     return S_OK;
472 }
473
474
475 /********************* IRemUnknown Proxy/Stub ********************************/
476
477 typedef struct
478 {
479     const IRpcStubBufferVtbl *lpVtbl;
480     LONG refs;
481     IRemUnknown *iface;
482 } RemUnkStub;
483
484 static HRESULT WINAPI RemUnkStub_QueryInterface(LPRPCSTUBBUFFER iface,
485                                              REFIID riid,
486                                              LPVOID *obj)
487 {
488   RemUnkStub *This = (RemUnkStub *)iface;
489   TRACE("(%p)->QueryInterface(%s,%p)\n",This,debugstr_guid(riid),obj);
490   if (IsEqualGUID(&IID_IUnknown,riid) ||
491       IsEqualGUID(&IID_IRpcStubBuffer,riid)) {
492     *obj = This;
493     return S_OK;
494   }
495   return E_NOINTERFACE;
496 }
497
498 static ULONG WINAPI RemUnkStub_AddRef(LPRPCSTUBBUFFER iface)
499 {
500   RemUnkStub *This = (RemUnkStub *)iface;
501   TRACE("(%p)->AddRef()\n",This);
502   return InterlockedIncrement(&This->refs);
503 }
504
505 static ULONG WINAPI RemUnkStub_Release(LPRPCSTUBBUFFER iface)
506 {
507   RemUnkStub *This = (RemUnkStub *)iface;
508   ULONG refs;
509   TRACE("(%p)->Release()\n",This);
510   refs = InterlockedDecrement(&This->refs);
511   if (!refs)
512     HeapFree(GetProcessHeap(), 0, This);
513   return refs;
514 }
515
516 static HRESULT WINAPI RemUnkStub_Connect(LPRPCSTUBBUFFER iface,
517                                       LPUNKNOWN lpUnkServer)
518 {
519   RemUnkStub *This = (RemUnkStub *)iface;
520   TRACE("(%p)->Connect(%p)\n",This,lpUnkServer);
521   This->iface = (IRemUnknown*)lpUnkServer;
522   IRemUnknown_AddRef(This->iface);
523   return S_OK;
524 }
525
526 static void WINAPI RemUnkStub_Disconnect(LPRPCSTUBBUFFER iface)
527 {
528   RemUnkStub *This = (RemUnkStub *)iface;
529   TRACE("(%p)->Disconnect()\n",This);
530   IUnknown_Release(This->iface);
531   This->iface = NULL;
532 }
533
534 static HRESULT WINAPI RemUnkStub_Invoke(LPRPCSTUBBUFFER iface,
535                                      PRPCOLEMESSAGE pMsg,
536                                      LPRPCCHANNELBUFFER pChannel)
537 {
538   RemUnkStub *This = (RemUnkStub *)iface;
539   ULONG iMethod = pMsg->iMethod;
540   LPBYTE buf = pMsg->Buffer;
541   HRESULT hr = RPC_E_INVALIDMETHOD;
542
543   TRACE("(%p)->Invoke(%p,%p) method %d\n", This, pMsg, pChannel, iMethod);
544   switch (iMethod)
545   {
546   case 3: /* RemQueryInterface */
547   {
548     IPID ipid;
549     ULONG cRefs;
550     USHORT cIids;
551     IID *iids;
552     REMQIRESULT *pQIResults = NULL;
553
554     /* in */
555     memcpy(&ipid, buf, sizeof(ipid));
556     buf += sizeof(ipid);
557     memcpy(&cRefs, buf, sizeof(cRefs));
558     buf += sizeof(cRefs);
559     memcpy(&cIids, buf, sizeof(cIids));
560     buf += sizeof(cIids);
561     iids = (IID *)buf;
562
563     hr = IRemUnknown_RemQueryInterface(This->iface, &ipid, cRefs, cIids, iids, &pQIResults);
564
565     /* out */
566     pMsg->cbBuffer = cIids * sizeof(REMQIRESULT) + sizeof(HRESULT);
567
568     IRpcChannelBuffer_GetBuffer(pChannel, pMsg, &IID_IRemUnknown);
569
570     buf = pMsg->Buffer;
571     *(HRESULT *)buf = hr;
572     buf += sizeof(HRESULT);
573     
574     if (hr) return hr;
575     /* FIXME: pQIResults is a unique pointer so pQIResults can be NULL! */
576     memcpy(buf, pQIResults, cIids * sizeof(REMQIRESULT));
577
578     break;
579   }
580   case 4: /* RemAddRef */
581   {
582     USHORT cIids;
583     REMINTERFACEREF *ir;
584     HRESULT *pResults;
585
586     /* in */
587     memcpy(&cIids, buf, sizeof(USHORT));
588     buf += sizeof(USHORT);
589     ir = (REMINTERFACEREF*)buf;
590     pResults = CoTaskMemAlloc(cIids * sizeof(HRESULT));
591     if (!pResults) return E_OUTOFMEMORY;
592
593     hr = IRemUnknown_RemAddRef(This->iface, cIids, ir, pResults);
594
595     /* out */
596     pMsg->cbBuffer = cIids * sizeof(HRESULT);
597
598     IRpcChannelBuffer_GetBuffer(pChannel, pMsg, &IID_IRemUnknown);
599     if (!hr)
600     {
601         buf = pMsg->Buffer;
602         memcpy(buf, pResults, cIids * sizeof(HRESULT));
603     }
604
605     CoTaskMemFree(pResults);
606
607     break;
608   }
609   case 5: /* RemRelease */
610   {
611     USHORT cIids;
612     REMINTERFACEREF *ir;
613
614     /* in */
615     memcpy(&cIids, buf, sizeof(USHORT));
616     buf += sizeof(USHORT);
617     ir = (REMINTERFACEREF*)buf;
618
619     hr = IRemUnknown_RemRelease(This->iface, cIids, ir);
620
621     /* out */
622     pMsg->cbBuffer = 0;
623     IRpcChannelBuffer_GetBuffer(pChannel, pMsg, &IID_IRemUnknown);
624     break;
625   }
626   }
627   return hr;
628 }
629
630 static LPRPCSTUBBUFFER WINAPI RemUnkStub_IsIIDSupported(LPRPCSTUBBUFFER iface,
631                                                      REFIID riid)
632 {
633   RemUnkStub *This = (RemUnkStub *)iface;
634   TRACE("(%p)->IsIIDSupported(%s)\n", This, debugstr_guid(riid));
635   return IsEqualGUID(&IID_IRemUnknown, riid) ? iface : NULL;
636 }
637
638 static ULONG WINAPI RemUnkStub_CountRefs(LPRPCSTUBBUFFER iface)
639 {
640   RemUnkStub *This = (RemUnkStub *)iface;
641   FIXME("(%p)->CountRefs()\n", This);
642   return 1;
643 }
644
645 static HRESULT WINAPI RemUnkStub_DebugServerQueryInterface(LPRPCSTUBBUFFER iface,
646                                                         LPVOID *ppv)
647 {
648   RemUnkStub *This = (RemUnkStub *)iface;
649   FIXME("(%p)->DebugServerQueryInterface(%p)\n",This,ppv);
650   return E_NOINTERFACE;
651 }
652
653 static void WINAPI RemUnkStub_DebugServerRelease(LPRPCSTUBBUFFER iface,
654                                               LPVOID pv)
655 {
656   RemUnkStub *This = (RemUnkStub *)iface;
657   FIXME("(%p)->DebugServerRelease(%p)\n", This, pv);
658 }
659
660 static const IRpcStubBufferVtbl RemUnkStub_VTable =
661 {
662   RemUnkStub_QueryInterface,
663   RemUnkStub_AddRef,
664   RemUnkStub_Release,
665   RemUnkStub_Connect,
666   RemUnkStub_Disconnect,
667   RemUnkStub_Invoke,
668   RemUnkStub_IsIIDSupported,
669   RemUnkStub_CountRefs,
670   RemUnkStub_DebugServerQueryInterface,
671   RemUnkStub_DebugServerRelease
672 };
673
674 static HRESULT RemUnkStub_Construct(IRpcStubBuffer **ppStub)
675 {
676     RemUnkStub *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
677     if (!This) return E_OUTOFMEMORY;
678     This->lpVtbl = &RemUnkStub_VTable;
679     This->refs = 1;
680     This->iface = NULL;
681     *ppStub = (IRpcStubBuffer*)This;
682     return S_OK;
683 }
684
685
686 typedef struct _RemUnkProxy {
687     const IRemUnknownVtbl               *lpvtbl_remunk;
688     const IRpcProxyBufferVtbl   *lpvtbl_proxy;
689     LONG                                refs;
690
691     IRpcChannelBuffer                   *chan;
692     IUnknown *outer_unknown;
693 } RemUnkProxy;
694
695 static HRESULT WINAPI RemUnkProxy_QueryInterface(LPREMUNKNOWN iface, REFIID riid, void **ppv)
696 {
697     RemUnkProxy *This = (RemUnkProxy *)iface;
698     if (This->outer_unknown)
699         return IUnknown_QueryInterface(This->outer_unknown, riid, ppv);
700     if (IsEqualIID(riid, &IID_IUnknown) ||
701         IsEqualIID(riid, &IID_IRemUnknown))
702     {
703         IRemUnknown_AddRef(iface);
704         *ppv = (LPVOID)iface;
705         return S_OK;
706     }
707     return E_NOINTERFACE;
708 }
709
710 static ULONG WINAPI RemUnkProxy_AddRef(LPREMUNKNOWN iface)
711 {
712   RemUnkProxy *This = (RemUnkProxy *)iface;
713   ULONG refs;
714
715   TRACE("(%p)->AddRef()\n",This);
716
717   if (This->outer_unknown)
718       refs = IUnknown_AddRef(This->outer_unknown);
719   else
720       refs = InterlockedIncrement(&This->refs);
721   return refs;
722 }
723
724 static ULONG WINAPI RemUnkProxy_Release(LPREMUNKNOWN iface)
725 {
726   RemUnkProxy *This = (RemUnkProxy *)iface;
727
728   TRACE("(%p)->Release()\n",This);
729   if (This->outer_unknown)
730       return IUnknown_Release(This->outer_unknown);
731   else
732       return IRpcProxyBufferImpl_Release((IRpcProxyBuffer *)&This->lpvtbl_proxy);
733 }
734
735 static HRESULT WINAPI RemUnkProxy_RemQueryInterface(LPREMUNKNOWN iface,
736                                                  REFIPID ripid,
737                                                  ULONG cRefs,
738                                                  USHORT cIids,
739                                                  IID* iids,
740                                                  REMQIRESULT** ppQIResults)
741 {
742   RemUnkProxy *This = (RemUnkProxy *)iface;
743   RPCOLEMESSAGE msg;
744   HRESULT hr = S_OK;
745   ULONG status;
746
747   TRACE("(%p)->(%s,%d,%d,%p,%p)\n",This,
748         debugstr_guid(ripid),cRefs,cIids,iids,ppQIResults);
749
750   *ppQIResults = NULL;
751   memset(&msg, 0, sizeof(msg));
752   msg.iMethod = 3;
753   msg.cbBuffer = sizeof(IPID) + sizeof(ULONG) +
754     sizeof(USHORT) + cIids*sizeof(IID);
755   hr = IRpcChannelBuffer_GetBuffer(This->chan, &msg, &IID_IRemUnknown);
756   if (SUCCEEDED(hr)) {
757     LPBYTE buf = msg.Buffer;
758     memcpy(buf, ripid, sizeof(IPID));
759     buf += sizeof(IPID);
760     memcpy(buf, &cRefs, sizeof(ULONG));
761     buf += sizeof(ULONG);
762     memcpy(buf, &cIids, sizeof(USHORT));
763     buf += sizeof(USHORT);
764     memcpy(buf, iids, cIids*sizeof(IID));
765
766     hr = IRpcChannelBuffer_SendReceive(This->chan, &msg, &status);
767
768     buf = msg.Buffer;
769
770     if (SUCCEEDED(hr)) {
771         hr = *(HRESULT *)buf;
772         buf += sizeof(HRESULT);
773     }
774
775     if (SUCCEEDED(hr)) {
776       *ppQIResults = CoTaskMemAlloc(cIids*sizeof(REMQIRESULT));
777       memcpy(*ppQIResults, buf, cIids*sizeof(REMQIRESULT));
778     }
779
780     IRpcChannelBuffer_FreeBuffer(This->chan, &msg);
781   }
782
783   return hr;
784 }
785
786 static HRESULT WINAPI RemUnkProxy_RemAddRef(LPREMUNKNOWN iface,
787                                          USHORT cInterfaceRefs,
788                                          REMINTERFACEREF* InterfaceRefs,
789                                          HRESULT* pResults)
790 {
791   RemUnkProxy *This = (RemUnkProxy *)iface;
792   RPCOLEMESSAGE msg;
793   HRESULT hr = S_OK;
794   ULONG status;
795
796   TRACE("(%p)->(%d,%p,%p)\n",This,
797         cInterfaceRefs,InterfaceRefs,pResults);
798
799   memset(&msg, 0, sizeof(msg));
800   msg.iMethod = 4;
801   msg.cbBuffer = sizeof(USHORT) + cInterfaceRefs*sizeof(REMINTERFACEREF);
802   hr = IRpcChannelBuffer_GetBuffer(This->chan, &msg, &IID_IRemUnknown);
803   if (SUCCEEDED(hr)) {
804     LPBYTE buf = msg.Buffer;
805     memcpy(buf, &cInterfaceRefs, sizeof(USHORT));
806     buf += sizeof(USHORT);
807     memcpy(buf, InterfaceRefs, cInterfaceRefs*sizeof(REMINTERFACEREF));
808
809     hr = IRpcChannelBuffer_SendReceive(This->chan, &msg, &status);
810
811     if (SUCCEEDED(hr)) {
812       buf = msg.Buffer;
813       memcpy(pResults, buf, cInterfaceRefs*sizeof(HRESULT));
814     }
815
816     IRpcChannelBuffer_FreeBuffer(This->chan, &msg);
817   }
818
819   return hr;
820 }
821
822 static HRESULT WINAPI RemUnkProxy_RemRelease(LPREMUNKNOWN iface,
823                                           USHORT cInterfaceRefs,
824                                           REMINTERFACEREF* InterfaceRefs)
825 {
826   RemUnkProxy *This = (RemUnkProxy *)iface;
827   RPCOLEMESSAGE msg;
828   HRESULT hr = S_OK;
829   ULONG status;
830
831   TRACE("(%p)->(%d,%p)\n",This,
832         cInterfaceRefs,InterfaceRefs);
833
834   memset(&msg, 0, sizeof(msg));
835   msg.iMethod = 5;
836   msg.cbBuffer = sizeof(USHORT) + cInterfaceRefs*sizeof(REMINTERFACEREF);
837   hr = IRpcChannelBuffer_GetBuffer(This->chan, &msg, &IID_IRemUnknown);
838   if (SUCCEEDED(hr)) {
839     LPBYTE buf = msg.Buffer;
840     memcpy(buf, &cInterfaceRefs, sizeof(USHORT));
841     buf += sizeof(USHORT);
842     memcpy(buf, InterfaceRefs, cInterfaceRefs*sizeof(REMINTERFACEREF));
843
844     hr = IRpcChannelBuffer_SendReceive(This->chan, &msg, &status);
845
846     IRpcChannelBuffer_FreeBuffer(This->chan, &msg);
847   }
848
849   return hr;
850 }
851
852 static const IRemUnknownVtbl RemUnkProxy_VTable =
853 {
854   RemUnkProxy_QueryInterface,
855   RemUnkProxy_AddRef,
856   RemUnkProxy_Release,
857   RemUnkProxy_RemQueryInterface,
858   RemUnkProxy_RemAddRef,
859   RemUnkProxy_RemRelease
860 };
861
862
863 static HRESULT WINAPI RURpcProxyBufferImpl_QueryInterface(LPRPCPROXYBUFFER iface,REFIID riid,LPVOID *ppv) {
864     *ppv = NULL;
865     if (IsEqualIID(riid,&IID_IRpcProxyBuffer)||IsEqualIID(riid,&IID_IUnknown)) {
866         IRpcProxyBuffer_AddRef(iface);
867         *ppv = (LPVOID)iface;
868         return S_OK;
869     }
870     FIXME("(%s), no interface.\n",debugstr_guid(riid));
871     return E_NOINTERFACE;
872 }
873
874 static ULONG WINAPI RURpcProxyBufferImpl_AddRef(LPRPCPROXYBUFFER iface) {
875     ICOM_THIS_MULTI(RemUnkProxy,lpvtbl_proxy,iface);
876     TRACE("%p, %d\n", iface, This->refs + 1);
877     return InterlockedIncrement(&This->refs);
878 }
879
880 static ULONG WINAPI RURpcProxyBufferImpl_Release(LPRPCPROXYBUFFER iface) {
881     ICOM_THIS_MULTI(RemUnkProxy,lpvtbl_proxy,iface);
882     ULONG ref = InterlockedDecrement(&This->refs);
883     TRACE("%p, %d\n", iface, ref);
884     if (!ref) {
885         IRpcProxyBuffer_Disconnect(iface);
886         HeapFree(GetProcessHeap(),0,This);
887     }
888     return ref;
889 }
890
891 static HRESULT WINAPI RURpcProxyBufferImpl_Connect(LPRPCPROXYBUFFER iface,IRpcChannelBuffer* pRpcChannelBuffer) {
892     ICOM_THIS_MULTI(RemUnkProxy,lpvtbl_proxy,iface);
893
894     TRACE("%p, %p\n", iface, pRpcChannelBuffer);
895     This->chan = pRpcChannelBuffer;
896     IRpcChannelBuffer_AddRef(This->chan);
897     return S_OK;
898 }
899 static void WINAPI RURpcProxyBufferImpl_Disconnect(LPRPCPROXYBUFFER iface) {
900     ICOM_THIS_MULTI(RemUnkProxy,lpvtbl_proxy,iface);
901     TRACE("%p, %p\n", iface, This->chan);
902     if (This->chan) {
903         IRpcChannelBuffer_Release(This->chan);
904         This->chan = NULL;
905     }
906 }
907
908
909 static const IRpcProxyBufferVtbl RURpcProxyBuffer_VTable = {
910     RURpcProxyBufferImpl_QueryInterface,
911     RURpcProxyBufferImpl_AddRef,
912     RURpcProxyBufferImpl_Release,
913     RURpcProxyBufferImpl_Connect,
914     RURpcProxyBufferImpl_Disconnect
915 };
916
917 static HRESULT
918 RemUnkProxy_Construct(IUnknown *pUnkOuter, LPVOID *ppv,LPVOID *ppProxy) {
919     RemUnkProxy *This;
920
921     This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*This));
922     if (!This)
923         return E_OUTOFMEMORY;
924
925     This->lpvtbl_remunk = &RemUnkProxy_VTable;
926     This->lpvtbl_proxy  = &RURpcProxyBuffer_VTable;
927     /* only one reference for the proxy buffer */
928     This->refs          = 1;
929     This->outer_unknown = pUnkOuter;
930     *ppv                = &(This->lpvtbl_remunk);
931     *ppProxy            = &(This->lpvtbl_proxy);
932     /* and one reference for the object */
933     IUnknown_AddRef((IUnknown *)*ppv);
934     return S_OK;
935 }
936
937
938 /********************* OLE Proxy/Stub Factory ********************************/
939 static HRESULT WINAPI
940 PSFacBuf_QueryInterface(LPPSFACTORYBUFFER iface, REFIID iid, LPVOID *ppv) {
941     if (IsEqualIID(iid,&IID_IPSFactoryBuffer)||IsEqualIID(iid,&IID_IUnknown)) {
942         *ppv = (LPVOID)iface;
943         /* No ref counting, static class */
944         return S_OK;
945     }
946     FIXME("(%s) unknown IID?\n",debugstr_guid(iid));
947     return E_NOINTERFACE;
948 }
949
950 static ULONG WINAPI PSFacBuf_AddRef(LPPSFACTORYBUFFER iface) { return 2; }
951 static ULONG WINAPI PSFacBuf_Release(LPPSFACTORYBUFFER iface) { return 1; }
952
953 static HRESULT WINAPI
954 PSFacBuf_CreateProxy(
955     LPPSFACTORYBUFFER iface, IUnknown* pUnkOuter, REFIID riid,
956     IRpcProxyBuffer **ppProxy, LPVOID *ppv
957 ) {
958     if (IsEqualIID(&IID_IClassFactory,riid))
959         return CFProxy_Construct(pUnkOuter, ppv,(LPVOID*)ppProxy);
960     else if (IsEqualIID(&IID_IRemUnknown,riid))
961         return RemUnkProxy_Construct(pUnkOuter, ppv,(LPVOID*)ppProxy);
962     FIXME("proxying not implemented for (%s) yet!\n",debugstr_guid(riid));
963     return E_FAIL;
964 }
965
966 static HRESULT WINAPI
967 PSFacBuf_CreateStub(
968     LPPSFACTORYBUFFER iface, REFIID riid,IUnknown *pUnkServer,
969     IRpcStubBuffer** ppStub
970 ) {
971     HRESULT hres;
972
973     TRACE("(%s,%p,%p)\n",debugstr_guid(riid),pUnkServer,ppStub);
974
975     if (IsEqualIID(&IID_IClassFactory, riid) ||
976         IsEqualIID(&IID_IUnknown, riid) /* FIXME: fixup stub manager and remove this*/) {
977         hres = CFStub_Construct(ppStub);
978         if (!hres)
979             IRpcStubBuffer_Connect((*ppStub),pUnkServer);
980         return hres;
981     } else if (IsEqualIID(&IID_IRemUnknown,riid)) {
982         hres = RemUnkStub_Construct(ppStub);
983         if (!hres)
984             IRpcStubBuffer_Connect((*ppStub),pUnkServer);
985         return hres;
986     }
987     FIXME("stubbing not implemented for (%s) yet!\n",debugstr_guid(riid));
988     return E_FAIL;
989 }
990
991 static const IPSFactoryBufferVtbl psfacbufvtbl = {
992     PSFacBuf_QueryInterface,
993     PSFacBuf_AddRef,
994     PSFacBuf_Release,
995     PSFacBuf_CreateProxy,
996     PSFacBuf_CreateStub
997 };
998
999 /* This is the whole PSFactoryBuffer object, just the vtableptr */
1000 static const IPSFactoryBufferVtbl *lppsfac = &psfacbufvtbl;
1001
1002 /***********************************************************************
1003  *           DllGetClassObject [OLE32.@]
1004  */
1005 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID iid,LPVOID *ppv)
1006 {
1007     *ppv = NULL;
1008     if (IsEqualIID(rclsid, &CLSID_PSFactoryBuffer))
1009         return IPSFactoryBuffer_QueryInterface((IPSFactoryBuffer *)&lppsfac, iid, ppv);
1010     if (IsEqualIID(rclsid,&CLSID_DfMarshal)&&(
1011                 IsEqualIID(iid,&IID_IClassFactory) ||
1012                 IsEqualIID(iid,&IID_IUnknown)
1013         )
1014     )
1015         return MARSHAL_GetStandardMarshalCF(ppv);
1016     if (IsEqualIID(rclsid,&CLSID_StdGlobalInterfaceTable) && (IsEqualIID(iid,&IID_IClassFactory) || IsEqualIID(iid,&IID_IUnknown)))
1017         return StdGlobalInterfaceTable_GetFactory(ppv);
1018     if (IsEqualCLSID(rclsid, &CLSID_FileMoniker))
1019         return FileMonikerCF_Create(iid, ppv);
1020     if (IsEqualCLSID(rclsid, &CLSID_ItemMoniker))
1021         return ItemMonikerCF_Create(iid, ppv);
1022     if (IsEqualCLSID(rclsid, &CLSID_AntiMoniker))
1023         return AntiMonikerCF_Create(iid, ppv);
1024     if (IsEqualCLSID(rclsid, &CLSID_CompositeMoniker))
1025         return CompositeMonikerCF_Create(iid, ppv);
1026     if (IsEqualCLSID(rclsid, &CLSID_ClassMoniker))
1027         return ClassMonikerCF_Create(iid, ppv);
1028
1029     FIXME("\n\tCLSID:\t%s,\n\tIID:\t%s\n",debugstr_guid(rclsid),debugstr_guid(iid));
1030     return CLASS_E_CLASSNOTAVAILABLE;
1031 }