wined3d: Check the results of SetPixelFormat.
[wine.git] / dlls / ole32 / oleproxy.c
blobe8079411c8f6cecb8b9f7fa1de98a58072e0c525
1 /*
2 * OLE32 proxy/stub handler
4 * Copyright 2002 Marcus Meissner
5 * Copyright 2001 Ove Kåven, TransGaming Technologies
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.
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.
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
22 /* Documentation on MSDN:
24 * (Top level COM documentation)
25 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnanchor/html/componentdevelopmentank.asp
27 * (COM Proxy)
28 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/htm/comext_1q0p.asp
30 * (COM Stub)
31 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/htm/comext_1lia.asp
33 * (Marshal)
34 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/htm/comext_1gfn.asp
38 #include "config.h"
40 #include <stdlib.h>
41 #include <stdarg.h>
42 #include <stdio.h>
43 #include <string.h>
45 #define COBJMACROS
46 #define NONAMELESSUNION
47 #define NONAMELESSSTRUCT
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"
58 #include "compobj_private.h"
59 #include "moniker.h"
61 #include "wine/debug.h"
63 WINE_DEFAULT_DEBUG_CHANNEL(ole);
65 static ULONG WINAPI RURpcProxyBufferImpl_Release(LPRPCPROXYBUFFER iface);
67 /* From: http://msdn.microsoft.com/library/en-us/com/cmi_m_4lda.asp
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.
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.
87 typedef struct _CFStub {
88 const IRpcStubBufferVtbl *lpvtbl;
89 LONG ref;
91 LPUNKNOWN pUnkServer;
92 } CFStub;
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;
101 FIXME("(%s), interface not supported.\n",debugstr_guid(riid));
102 return E_NOINTERFACE;
105 static ULONG WINAPI
106 CFStub_AddRef(LPRPCSTUBBUFFER iface) {
107 CFStub *This = (CFStub *)iface;
108 return InterlockedIncrement(&This->ref);
111 static ULONG WINAPI
112 CFStub_Release(LPRPCSTUBBUFFER iface) {
113 CFStub *This = (CFStub *)iface;
114 ULONG ref;
116 ref = InterlockedDecrement(&This->ref);
117 if (!ref) {
118 IRpcStubBuffer_Disconnect(iface);
119 HeapFree(GetProcessHeap(),0,This);
121 return ref;
124 static HRESULT WINAPI
125 CFStub_Connect(LPRPCSTUBBUFFER iface, IUnknown *pUnkServer) {
126 CFStub *This = (CFStub *)iface;
128 This->pUnkServer = pUnkServer;
129 IUnknown_AddRef(pUnkServer);
130 return S_OK;
133 static void WINAPI
134 CFStub_Disconnect(LPRPCSTUBBUFFER iface) {
135 CFStub *This = (CFStub *)iface;
137 if (This->pUnkServer) {
138 IUnknown_Release(This->pUnkServer);
139 This->pUnkServer = NULL;
143 static HRESULT WINAPI
144 CFStub_Invoke(
145 LPRPCSTUBBUFFER iface,RPCOLEMESSAGE* msg,IRpcChannelBuffer* chanbuf
147 CFStub *This = (CFStub *)iface;
148 HRESULT hres;
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;
160 if (msg->cbBuffer < sizeof(IID)) {
161 FIXME("Not enough bytes in buffer (%d)?\n",msg->cbBuffer);
162 return E_FAIL;
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;
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;
178 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
179 if (hres) {
180 FIXME("Failed to create stream on hglobal\n");
181 goto getbuffer;
183 hres = IStream_Write(pStm, &ppv, sizeof(ppv), NULL);
184 if (hres) {
185 ERR("IStream_Write failed, 0x%08x\n", hres);
186 goto getbuffer;
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;
196 hres = IStream_Stat(pStm,&ststg,0);
197 if (hres) {
198 FIXME("Stat failed.\n");
199 goto getbuffer;
202 msg->cbBuffer = ststg.cbSize.u.LowPart;
204 getbuffer:
205 IRpcChannelBuffer_GetBuffer(chanbuf, msg, &IID_IClassFactory);
206 if (hres) return hres;
208 seekto.u.LowPart = 0;seekto.u.HighPart = 0;
209 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
210 if (hres) {
211 FIXME("IStream_Seek failed, %x\n",hres);
212 return hres;
214 hres = IStream_Read(pStm,msg->Buffer,msg->cbBuffer,&res);
215 if (hres) {
216 FIXME("Stream Read failed, %x\n",hres);
217 return hres;
219 IStream_Release(pStm);
220 return S_OK;
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;
228 static LPRPCSTUBBUFFER WINAPI
229 CFStub_IsIIDSupported(LPRPCSTUBBUFFER iface,REFIID riid) {
230 FIXME("(%s), stub!\n",debugstr_guid(riid));
231 return NULL;
234 static ULONG WINAPI
235 CFStub_CountRefs(LPRPCSTUBBUFFER iface) {
236 FIXME("(), stub!\n");
237 return 1;
240 static HRESULT WINAPI
241 CFStub_DebugServerQueryInterface(LPRPCSTUBBUFFER iface,void** ppv) {
242 FIXME("(%p), stub!\n",ppv);
243 return E_FAIL;
245 static void WINAPI
246 CFStub_DebugServerRelease(LPRPCSTUBBUFFER iface,void *pv) {
247 FIXME("(%p), stub!\n",pv);
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
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;
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.
279 typedef struct _CFProxy {
280 const IClassFactoryVtbl *lpvtbl_cf;
281 const IRpcProxyBufferVtbl *lpvtbl_proxy;
282 LONG ref;
284 IRpcChannelBuffer *chanbuf;
285 IUnknown *outer_unknown;
286 } CFProxy;
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;
295 FIXME("(%s), no interface.\n",debugstr_guid(riid));
296 return E_NOINTERFACE;
299 static ULONG WINAPI IRpcProxyBufferImpl_AddRef(LPRPCPROXYBUFFER iface) {
300 ICOM_THIS_MULTI(CFProxy,lpvtbl_proxy,iface);
301 return InterlockedIncrement(&This->ref);
304 static ULONG WINAPI IRpcProxyBufferImpl_Release(LPRPCPROXYBUFFER iface) {
305 ICOM_THIS_MULTI(CFProxy,lpvtbl_proxy,iface);
306 ULONG ref = InterlockedDecrement(&This->ref);
308 if (!ref) {
309 IRpcProxyBuffer_Disconnect(iface);
310 HeapFree(GetProcessHeap(),0,This);
312 return ref;
315 static HRESULT WINAPI IRpcProxyBufferImpl_Connect(LPRPCPROXYBUFFER iface,IRpcChannelBuffer* pRpcChannelBuffer) {
316 ICOM_THIS_MULTI(CFProxy,lpvtbl_proxy,iface);
318 This->chanbuf = pRpcChannelBuffer;
319 IRpcChannelBuffer_AddRef(This->chanbuf);
320 return S_OK;
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;
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;
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;
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);
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);
360 static HRESULT WINAPI CFProxy_CreateInstance(
361 LPCLASSFACTORY iface,
362 LPUNKNOWN pUnkOuter,/* [in] */
363 REFIID riid, /* [in] */
364 LPVOID *ppv /* [out] */
366 ICOM_THIS_MULTI(CFProxy,lpvtbl_cf,iface);
367 HRESULT hres;
368 LPSTREAM pStream;
369 HGLOBAL hGlobal;
370 ULONG srstatus;
371 RPCOLEMESSAGE msg;
373 TRACE("(%p,%s,%p)\n",pUnkOuter,debugstr_guid(riid),ppv);
375 /* Send CreateInstance to the remote classfactory.
377 * Data: Only the 'IID'.
379 msg.iMethod = 3;
380 msg.cbBuffer = sizeof(*riid);
381 msg.Buffer = NULL;
382 hres = IRpcChannelBuffer_GetBuffer(This->chanbuf,&msg,&IID_IClassFactory);
383 if (hres) {
384 FIXME("IRpcChannelBuffer_GetBuffer failed with %x?\n",hres);
385 return hres;
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;
395 if (!msg.cbBuffer) { /* interface not found on remote */
396 IRpcChannelBuffer_FreeBuffer(This->chanbuf,&msg);
397 return srstatus;
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) {
406 FIXME("CreateStreamOnHGlobal failed with %x\n",hres);
407 IRpcChannelBuffer_FreeBuffer(This->chanbuf,&msg);
408 return hres;
410 hres = IStream_Read(pStream, ppv, sizeof(*ppv), NULL);
411 if (hres != S_OK)
412 hres = E_FAIL;
413 else if (*ppv) {
414 hres = CoUnmarshalInterface(
415 pStream,
416 riid,
420 IStream_Release(pStream); /* Does GlobalFree hGlobal too. */
422 IRpcChannelBuffer_FreeBuffer(This->chanbuf,&msg);
424 if (hres) {
425 FIXME("CoMarshalInterface failed, %x\n",hres);
426 return hres;
428 return S_OK;
431 static HRESULT WINAPI CFProxy_LockServer(LPCLASSFACTORY iface,BOOL fLock) {
432 /*ICOM_THIS_MULTI(CFProxy,lpvtbl_cf,iface);*/
433 FIXME("(%d), stub!\n",fLock);
434 /* basically: write BOOL, read empty */
435 return S_OK;
438 static const IRpcProxyBufferVtbl pspbvtbl = {
439 IRpcProxyBufferImpl_QueryInterface,
440 IRpcProxyBufferImpl_AddRef,
441 IRpcProxyBufferImpl_Release,
442 IRpcProxyBufferImpl_Connect,
443 IRpcProxyBufferImpl_Disconnect
445 static const IClassFactoryVtbl cfproxyvt = {
446 CFProxy_QueryInterface,
447 CFProxy_AddRef,
448 CFProxy_Release,
449 CFProxy_CreateInstance,
450 CFProxy_LockServer
453 static HRESULT
454 CFProxy_Construct(IUnknown *pUnkOuter, LPVOID *ppv,LPVOID *ppProxy) {
455 CFProxy *cf;
457 cf = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(CFProxy));
458 if (!cf)
459 return E_OUTOFMEMORY;
461 cf->lpvtbl_cf = &cfproxyvt;
462 cf->lpvtbl_proxy = &pspbvtbl;
463 /* one reference for the proxy buffer */
464 cf->ref = 1;
465 cf->outer_unknown = pUnkOuter;
466 *ppv = &(cf->lpvtbl_cf);
467 *ppProxy = &(cf->lpvtbl_proxy);
468 /* and one reference for the object */
469 IUnknown_AddRef((IUnknown *)*ppv);
470 return S_OK;
474 /********************* IRemUnknown Proxy/Stub ********************************/
476 typedef struct
478 const IRpcStubBufferVtbl *lpVtbl;
479 LONG refs;
480 IRemUnknown *iface;
481 } RemUnkStub;
483 static HRESULT WINAPI RemUnkStub_QueryInterface(LPRPCSTUBBUFFER iface,
484 REFIID riid,
485 LPVOID *obj)
487 RemUnkStub *This = (RemUnkStub *)iface;
488 TRACE("(%p)->QueryInterface(%s,%p)\n",This,debugstr_guid(riid),obj);
489 if (IsEqualGUID(&IID_IUnknown,riid) ||
490 IsEqualGUID(&IID_IRpcStubBuffer,riid)) {
491 *obj = This;
492 return S_OK;
494 return E_NOINTERFACE;
497 static ULONG WINAPI RemUnkStub_AddRef(LPRPCSTUBBUFFER iface)
499 RemUnkStub *This = (RemUnkStub *)iface;
500 TRACE("(%p)->AddRef()\n",This);
501 return InterlockedIncrement(&This->refs);
504 static ULONG WINAPI RemUnkStub_Release(LPRPCSTUBBUFFER iface)
506 RemUnkStub *This = (RemUnkStub *)iface;
507 ULONG refs;
508 TRACE("(%p)->Release()\n",This);
509 refs = InterlockedDecrement(&This->refs);
510 if (!refs)
511 HeapFree(GetProcessHeap(), 0, This);
512 return refs;
515 static HRESULT WINAPI RemUnkStub_Connect(LPRPCSTUBBUFFER iface,
516 LPUNKNOWN lpUnkServer)
518 RemUnkStub *This = (RemUnkStub *)iface;
519 TRACE("(%p)->Connect(%p)\n",This,lpUnkServer);
520 This->iface = (IRemUnknown*)lpUnkServer;
521 IRemUnknown_AddRef(This->iface);
522 return S_OK;
525 static void WINAPI RemUnkStub_Disconnect(LPRPCSTUBBUFFER iface)
527 RemUnkStub *This = (RemUnkStub *)iface;
528 TRACE("(%p)->Disconnect()\n",This);
529 IUnknown_Release(This->iface);
530 This->iface = NULL;
533 static HRESULT WINAPI RemUnkStub_Invoke(LPRPCSTUBBUFFER iface,
534 PRPCOLEMESSAGE pMsg,
535 LPRPCCHANNELBUFFER pChannel)
537 RemUnkStub *This = (RemUnkStub *)iface;
538 ULONG iMethod = pMsg->iMethod;
539 LPBYTE buf = pMsg->Buffer;
540 HRESULT hr = RPC_E_INVALIDMETHOD;
542 TRACE("(%p)->Invoke(%p,%p) method %d\n", This, pMsg, pChannel, iMethod);
543 switch (iMethod)
545 case 3: /* RemQueryInterface */
547 IPID ipid;
548 ULONG cRefs;
549 USHORT cIids;
550 IID *iids;
551 REMQIRESULT *pQIResults = NULL;
553 /* in */
554 memcpy(&ipid, buf, sizeof(ipid));
555 buf += sizeof(ipid);
556 memcpy(&cRefs, buf, sizeof(cRefs));
557 buf += sizeof(cRefs);
558 memcpy(&cIids, buf, sizeof(cIids));
559 buf += sizeof(cIids);
560 iids = (IID *)buf;
562 hr = IRemUnknown_RemQueryInterface(This->iface, &ipid, cRefs, cIids, iids, &pQIResults);
564 /* out */
565 pMsg->cbBuffer = cIids * sizeof(REMQIRESULT) + sizeof(HRESULT);
567 IRpcChannelBuffer_GetBuffer(pChannel, pMsg, &IID_IRemUnknown);
569 buf = pMsg->Buffer;
570 *(HRESULT *)buf = hr;
571 buf += sizeof(HRESULT);
573 if (hr) return hr;
574 /* FIXME: pQIResults is a unique pointer so pQIResults can be NULL! */
575 memcpy(buf, pQIResults, cIids * sizeof(REMQIRESULT));
577 break;
579 case 4: /* RemAddRef */
581 USHORT cIids;
582 REMINTERFACEREF *ir;
583 HRESULT *pResults;
585 /* in */
586 memcpy(&cIids, buf, sizeof(USHORT));
587 buf += sizeof(USHORT);
588 ir = (REMINTERFACEREF*)buf;
589 pResults = CoTaskMemAlloc(cIids * sizeof(HRESULT));
590 if (!pResults) return E_OUTOFMEMORY;
592 hr = IRemUnknown_RemAddRef(This->iface, cIids, ir, pResults);
594 /* out */
595 pMsg->cbBuffer = cIids * sizeof(HRESULT);
597 IRpcChannelBuffer_GetBuffer(pChannel, pMsg, &IID_IRemUnknown);
598 if (!hr)
600 buf = pMsg->Buffer;
601 memcpy(buf, pResults, cIids * sizeof(HRESULT));
604 CoTaskMemFree(pResults);
606 break;
608 case 5: /* RemRelease */
610 USHORT cIids;
611 REMINTERFACEREF *ir;
613 /* in */
614 memcpy(&cIids, buf, sizeof(USHORT));
615 buf += sizeof(USHORT);
616 ir = (REMINTERFACEREF*)buf;
618 hr = IRemUnknown_RemRelease(This->iface, cIids, ir);
620 /* out */
621 pMsg->cbBuffer = 0;
622 IRpcChannelBuffer_GetBuffer(pChannel, pMsg, &IID_IRemUnknown);
623 break;
626 return hr;
629 static LPRPCSTUBBUFFER WINAPI RemUnkStub_IsIIDSupported(LPRPCSTUBBUFFER iface,
630 REFIID riid)
632 RemUnkStub *This = (RemUnkStub *)iface;
633 TRACE("(%p)->IsIIDSupported(%s)\n", This, debugstr_guid(riid));
634 return IsEqualGUID(&IID_IRemUnknown, riid) ? iface : NULL;
637 static ULONG WINAPI RemUnkStub_CountRefs(LPRPCSTUBBUFFER iface)
639 RemUnkStub *This = (RemUnkStub *)iface;
640 FIXME("(%p)->CountRefs()\n", This);
641 return 1;
644 static HRESULT WINAPI RemUnkStub_DebugServerQueryInterface(LPRPCSTUBBUFFER iface,
645 LPVOID *ppv)
647 RemUnkStub *This = (RemUnkStub *)iface;
648 FIXME("(%p)->DebugServerQueryInterface(%p)\n",This,ppv);
649 return E_NOINTERFACE;
652 static void WINAPI RemUnkStub_DebugServerRelease(LPRPCSTUBBUFFER iface,
653 LPVOID pv)
655 RemUnkStub *This = (RemUnkStub *)iface;
656 FIXME("(%p)->DebugServerRelease(%p)\n", This, pv);
659 static const IRpcStubBufferVtbl RemUnkStub_VTable =
661 RemUnkStub_QueryInterface,
662 RemUnkStub_AddRef,
663 RemUnkStub_Release,
664 RemUnkStub_Connect,
665 RemUnkStub_Disconnect,
666 RemUnkStub_Invoke,
667 RemUnkStub_IsIIDSupported,
668 RemUnkStub_CountRefs,
669 RemUnkStub_DebugServerQueryInterface,
670 RemUnkStub_DebugServerRelease
673 static HRESULT RemUnkStub_Construct(IRpcStubBuffer **ppStub)
675 RemUnkStub *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
676 if (!This) return E_OUTOFMEMORY;
677 This->lpVtbl = &RemUnkStub_VTable;
678 This->refs = 1;
679 This->iface = NULL;
680 *ppStub = (IRpcStubBuffer*)This;
681 return S_OK;
685 typedef struct _RemUnkProxy {
686 const IRemUnknownVtbl *lpvtbl_remunk;
687 const IRpcProxyBufferVtbl *lpvtbl_proxy;
688 LONG refs;
690 IRpcChannelBuffer *chan;
691 IUnknown *outer_unknown;
692 } RemUnkProxy;
694 static HRESULT WINAPI RemUnkProxy_QueryInterface(LPREMUNKNOWN iface, REFIID riid, void **ppv)
696 RemUnkProxy *This = (RemUnkProxy *)iface;
697 if (This->outer_unknown)
698 return IUnknown_QueryInterface(This->outer_unknown, riid, ppv);
699 if (IsEqualIID(riid, &IID_IUnknown) ||
700 IsEqualIID(riid, &IID_IRemUnknown))
702 IRemUnknown_AddRef(iface);
703 *ppv = (LPVOID)iface;
704 return S_OK;
706 return E_NOINTERFACE;
709 static ULONG WINAPI RemUnkProxy_AddRef(LPREMUNKNOWN iface)
711 RemUnkProxy *This = (RemUnkProxy *)iface;
712 ULONG refs;
714 TRACE("(%p)->AddRef()\n",This);
716 if (This->outer_unknown)
717 refs = IUnknown_AddRef(This->outer_unknown);
718 else
719 refs = InterlockedIncrement(&This->refs);
720 return refs;
723 static ULONG WINAPI RemUnkProxy_Release(LPREMUNKNOWN iface)
725 RemUnkProxy *This = (RemUnkProxy *)iface;
727 TRACE("(%p)->Release()\n",This);
728 if (This->outer_unknown)
729 return IUnknown_Release(This->outer_unknown);
730 else
731 return IRpcProxyBufferImpl_Release((IRpcProxyBuffer *)&This->lpvtbl_proxy);
734 static HRESULT WINAPI RemUnkProxy_RemQueryInterface(LPREMUNKNOWN iface,
735 REFIPID ripid,
736 ULONG cRefs,
737 USHORT cIids,
738 IID* iids,
739 REMQIRESULT** ppQIResults)
741 RemUnkProxy *This = (RemUnkProxy *)iface;
742 RPCOLEMESSAGE msg;
743 HRESULT hr = S_OK;
744 ULONG status;
746 TRACE("(%p)->(%s,%d,%d,%p,%p)\n",This,
747 debugstr_guid(ripid),cRefs,cIids,iids,ppQIResults);
749 *ppQIResults = NULL;
750 memset(&msg, 0, sizeof(msg));
751 msg.iMethod = 3;
752 msg.cbBuffer = sizeof(IPID) + sizeof(ULONG) +
753 sizeof(USHORT) + cIids*sizeof(IID);
754 hr = IRpcChannelBuffer_GetBuffer(This->chan, &msg, &IID_IRemUnknown);
755 if (SUCCEEDED(hr)) {
756 LPBYTE buf = msg.Buffer;
757 memcpy(buf, ripid, sizeof(IPID));
758 buf += sizeof(IPID);
759 memcpy(buf, &cRefs, sizeof(ULONG));
760 buf += sizeof(ULONG);
761 memcpy(buf, &cIids, sizeof(USHORT));
762 buf += sizeof(USHORT);
763 memcpy(buf, iids, cIids*sizeof(IID));
765 hr = IRpcChannelBuffer_SendReceive(This->chan, &msg, &status);
767 buf = msg.Buffer;
769 if (SUCCEEDED(hr)) {
770 hr = *(HRESULT *)buf;
771 buf += sizeof(HRESULT);
774 if (SUCCEEDED(hr)) {
775 *ppQIResults = CoTaskMemAlloc(cIids*sizeof(REMQIRESULT));
776 memcpy(*ppQIResults, buf, cIids*sizeof(REMQIRESULT));
779 IRpcChannelBuffer_FreeBuffer(This->chan, &msg);
782 return hr;
785 static HRESULT WINAPI RemUnkProxy_RemAddRef(LPREMUNKNOWN iface,
786 USHORT cInterfaceRefs,
787 REMINTERFACEREF* InterfaceRefs,
788 HRESULT* pResults)
790 RemUnkProxy *This = (RemUnkProxy *)iface;
791 RPCOLEMESSAGE msg;
792 HRESULT hr = S_OK;
793 ULONG status;
795 TRACE("(%p)->(%d,%p,%p)\n",This,
796 cInterfaceRefs,InterfaceRefs,pResults);
798 memset(&msg, 0, sizeof(msg));
799 msg.iMethod = 4;
800 msg.cbBuffer = sizeof(USHORT) + cInterfaceRefs*sizeof(REMINTERFACEREF);
801 hr = IRpcChannelBuffer_GetBuffer(This->chan, &msg, &IID_IRemUnknown);
802 if (SUCCEEDED(hr)) {
803 LPBYTE buf = msg.Buffer;
804 memcpy(buf, &cInterfaceRefs, sizeof(USHORT));
805 buf += sizeof(USHORT);
806 memcpy(buf, InterfaceRefs, cInterfaceRefs*sizeof(REMINTERFACEREF));
808 hr = IRpcChannelBuffer_SendReceive(This->chan, &msg, &status);
810 if (SUCCEEDED(hr)) {
811 buf = msg.Buffer;
812 memcpy(pResults, buf, cInterfaceRefs*sizeof(HRESULT));
815 IRpcChannelBuffer_FreeBuffer(This->chan, &msg);
818 return hr;
821 static HRESULT WINAPI RemUnkProxy_RemRelease(LPREMUNKNOWN iface,
822 USHORT cInterfaceRefs,
823 REMINTERFACEREF* InterfaceRefs)
825 RemUnkProxy *This = (RemUnkProxy *)iface;
826 RPCOLEMESSAGE msg;
827 HRESULT hr = S_OK;
828 ULONG status;
830 TRACE("(%p)->(%d,%p)\n",This,
831 cInterfaceRefs,InterfaceRefs);
833 memset(&msg, 0, sizeof(msg));
834 msg.iMethod = 5;
835 msg.cbBuffer = sizeof(USHORT) + cInterfaceRefs*sizeof(REMINTERFACEREF);
836 hr = IRpcChannelBuffer_GetBuffer(This->chan, &msg, &IID_IRemUnknown);
837 if (SUCCEEDED(hr)) {
838 LPBYTE buf = msg.Buffer;
839 memcpy(buf, &cInterfaceRefs, sizeof(USHORT));
840 buf += sizeof(USHORT);
841 memcpy(buf, InterfaceRefs, cInterfaceRefs*sizeof(REMINTERFACEREF));
843 hr = IRpcChannelBuffer_SendReceive(This->chan, &msg, &status);
845 IRpcChannelBuffer_FreeBuffer(This->chan, &msg);
848 return hr;
851 static const IRemUnknownVtbl RemUnkProxy_VTable =
853 RemUnkProxy_QueryInterface,
854 RemUnkProxy_AddRef,
855 RemUnkProxy_Release,
856 RemUnkProxy_RemQueryInterface,
857 RemUnkProxy_RemAddRef,
858 RemUnkProxy_RemRelease
862 static HRESULT WINAPI RURpcProxyBufferImpl_QueryInterface(LPRPCPROXYBUFFER iface,REFIID riid,LPVOID *ppv) {
863 *ppv = NULL;
864 if (IsEqualIID(riid,&IID_IRpcProxyBuffer)||IsEqualIID(riid,&IID_IUnknown)) {
865 IRpcProxyBuffer_AddRef(iface);
866 *ppv = (LPVOID)iface;
867 return S_OK;
869 FIXME("(%s), no interface.\n",debugstr_guid(riid));
870 return E_NOINTERFACE;
873 static ULONG WINAPI RURpcProxyBufferImpl_AddRef(LPRPCPROXYBUFFER iface) {
874 ICOM_THIS_MULTI(RemUnkProxy,lpvtbl_proxy,iface);
875 TRACE("%p, %d\n", iface, This->refs + 1);
876 return InterlockedIncrement(&This->refs);
879 static ULONG WINAPI RURpcProxyBufferImpl_Release(LPRPCPROXYBUFFER iface) {
880 ICOM_THIS_MULTI(RemUnkProxy,lpvtbl_proxy,iface);
881 ULONG ref = InterlockedDecrement(&This->refs);
882 TRACE("%p, %d\n", iface, ref);
883 if (!ref) {
884 IRpcProxyBuffer_Disconnect(iface);
885 HeapFree(GetProcessHeap(),0,This);
887 return ref;
890 static HRESULT WINAPI RURpcProxyBufferImpl_Connect(LPRPCPROXYBUFFER iface,IRpcChannelBuffer* pRpcChannelBuffer) {
891 ICOM_THIS_MULTI(RemUnkProxy,lpvtbl_proxy,iface);
893 TRACE("%p, %p\n", iface, pRpcChannelBuffer);
894 This->chan = pRpcChannelBuffer;
895 IRpcChannelBuffer_AddRef(This->chan);
896 return S_OK;
898 static void WINAPI RURpcProxyBufferImpl_Disconnect(LPRPCPROXYBUFFER iface) {
899 ICOM_THIS_MULTI(RemUnkProxy,lpvtbl_proxy,iface);
900 TRACE("%p, %p\n", iface, This->chan);
901 if (This->chan) {
902 IRpcChannelBuffer_Release(This->chan);
903 This->chan = NULL;
908 static const IRpcProxyBufferVtbl RURpcProxyBuffer_VTable = {
909 RURpcProxyBufferImpl_QueryInterface,
910 RURpcProxyBufferImpl_AddRef,
911 RURpcProxyBufferImpl_Release,
912 RURpcProxyBufferImpl_Connect,
913 RURpcProxyBufferImpl_Disconnect
916 static HRESULT
917 RemUnkProxy_Construct(IUnknown *pUnkOuter, LPVOID *ppv,LPVOID *ppProxy) {
918 RemUnkProxy *This;
920 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*This));
921 if (!This)
922 return E_OUTOFMEMORY;
924 This->lpvtbl_remunk = &RemUnkProxy_VTable;
925 This->lpvtbl_proxy = &RURpcProxyBuffer_VTable;
926 /* only one reference for the proxy buffer */
927 This->refs = 1;
928 This->outer_unknown = pUnkOuter;
929 *ppv = &(This->lpvtbl_remunk);
930 *ppProxy = &(This->lpvtbl_proxy);
931 /* and one reference for the object */
932 IUnknown_AddRef((IUnknown *)*ppv);
933 return S_OK;
937 /********************* OLE Proxy/Stub Factory ********************************/
938 static HRESULT WINAPI
939 PSFacBuf_QueryInterface(LPPSFACTORYBUFFER iface, REFIID iid, LPVOID *ppv) {
940 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)||IsEqualIID(iid,&IID_IUnknown)) {
941 *ppv = (LPVOID)iface;
942 /* No ref counting, static class */
943 return S_OK;
945 FIXME("(%s) unknown IID?\n",debugstr_guid(iid));
946 return E_NOINTERFACE;
949 static ULONG WINAPI PSFacBuf_AddRef(LPPSFACTORYBUFFER iface) { return 2; }
950 static ULONG WINAPI PSFacBuf_Release(LPPSFACTORYBUFFER iface) { return 1; }
952 static HRESULT WINAPI
953 PSFacBuf_CreateProxy(
954 LPPSFACTORYBUFFER iface, IUnknown* pUnkOuter, REFIID riid,
955 IRpcProxyBuffer **ppProxy, LPVOID *ppv
957 if (IsEqualIID(&IID_IClassFactory,riid))
958 return CFProxy_Construct(pUnkOuter, ppv,(LPVOID*)ppProxy);
959 else if (IsEqualIID(&IID_IRemUnknown,riid))
960 return RemUnkProxy_Construct(pUnkOuter, ppv,(LPVOID*)ppProxy);
961 FIXME("proxying not implemented for (%s) yet!\n",debugstr_guid(riid));
962 return E_FAIL;
965 static HRESULT WINAPI
966 PSFacBuf_CreateStub(
967 LPPSFACTORYBUFFER iface, REFIID riid,IUnknown *pUnkServer,
968 IRpcStubBuffer** ppStub
970 HRESULT hres;
972 TRACE("(%s,%p,%p)\n",debugstr_guid(riid),pUnkServer,ppStub);
974 if (IsEqualIID(&IID_IClassFactory, riid) ||
975 IsEqualIID(&IID_IUnknown, riid) /* FIXME: fixup stub manager and remove this*/) {
976 hres = CFStub_Construct(ppStub);
977 if (!hres)
978 IRpcStubBuffer_Connect((*ppStub),pUnkServer);
979 return hres;
980 } else if (IsEqualIID(&IID_IRemUnknown,riid)) {
981 hres = RemUnkStub_Construct(ppStub);
982 if (!hres)
983 IRpcStubBuffer_Connect((*ppStub),pUnkServer);
984 return hres;
986 FIXME("stubbing not implemented for (%s) yet!\n",debugstr_guid(riid));
987 return E_FAIL;
990 static const IPSFactoryBufferVtbl psfacbufvtbl = {
991 PSFacBuf_QueryInterface,
992 PSFacBuf_AddRef,
993 PSFacBuf_Release,
994 PSFacBuf_CreateProxy,
995 PSFacBuf_CreateStub
998 /* This is the whole PSFactoryBuffer object, just the vtableptr */
999 static const IPSFactoryBufferVtbl *lppsfac = &psfacbufvtbl;
1001 /***********************************************************************
1002 * DllGetClassObject [OLE32.@]
1004 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID iid,LPVOID *ppv)
1006 *ppv = NULL;
1007 if (IsEqualIID(rclsid, &CLSID_PSFactoryBuffer))
1008 return IPSFactoryBuffer_QueryInterface((IPSFactoryBuffer *)&lppsfac, iid, ppv);
1009 if (IsEqualIID(rclsid,&CLSID_DfMarshal)&&(
1010 IsEqualIID(iid,&IID_IClassFactory) ||
1011 IsEqualIID(iid,&IID_IUnknown)
1014 return MARSHAL_GetStandardMarshalCF(ppv);
1015 if (IsEqualIID(rclsid,&CLSID_StdGlobalInterfaceTable) && (IsEqualIID(iid,&IID_IClassFactory) || IsEqualIID(iid,&IID_IUnknown)))
1016 return StdGlobalInterfaceTable_GetFactory(ppv);
1017 if (IsEqualCLSID(rclsid, &CLSID_FileMoniker))
1018 return FileMonikerCF_Create(iid, ppv);
1019 if (IsEqualCLSID(rclsid, &CLSID_ItemMoniker))
1020 return ItemMonikerCF_Create(iid, ppv);
1021 if (IsEqualCLSID(rclsid, &CLSID_AntiMoniker))
1022 return AntiMonikerCF_Create(iid, ppv);
1023 if (IsEqualCLSID(rclsid, &CLSID_CompositeMoniker))
1024 return CompositeMonikerCF_Create(iid, ppv);
1025 if (IsEqualCLSID(rclsid, &CLSID_ClassMoniker))
1026 return ClassMonikerCF_Create(iid, ppv);
1028 FIXME("\n\tCLSID:\t%s,\n\tIID:\t%s\n",debugstr_guid(rclsid),debugstr_guid(iid));
1029 return CLASS_E_CLASSNOTAVAILABLE;