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