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