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