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