made functions and variables static in some testcases.
[wine/hacks.git] / dlls / ole32 / oleproxy.c
blob889e03c914ec6a4cb312a797065f64d78461f197
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 "winreg.h"
57 #include "wtypes.h"
59 #include "compobj_private.h"
60 #include "moniker.h"
62 #include "wine/debug.h"
64 WINE_DEFAULT_DEBUG_CHANNEL(ole);
66 static ULONG WINAPI RURpcProxyBufferImpl_Release(LPRPCPROXYBUFFER iface);
68 /* From: http://msdn.microsoft.com/library/en-us/com/cmi_m_4lda.asp
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.
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.
88 typedef struct _CFStub {
89 const IRpcStubBufferVtbl *lpvtbl;
90 LONG ref;
92 LPUNKNOWN pUnkServer;
93 } CFStub;
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;
102 FIXME("(%s), interface not supported.\n",debugstr_guid(riid));
103 return E_NOINTERFACE;
106 static ULONG WINAPI
107 CFStub_AddRef(LPRPCSTUBBUFFER iface) {
108 CFStub *This = (CFStub *)iface;
109 return InterlockedIncrement(&This->ref);
112 static ULONG WINAPI
113 CFStub_Release(LPRPCSTUBBUFFER iface) {
114 CFStub *This = (CFStub *)iface;
115 ULONG ref;
117 ref = InterlockedDecrement(&This->ref);
118 if (!ref) {
119 IRpcStubBuffer_Disconnect(iface);
120 HeapFree(GetProcessHeap(),0,This);
122 return ref;
125 static HRESULT WINAPI
126 CFStub_Connect(LPRPCSTUBBUFFER iface, IUnknown *pUnkServer) {
127 CFStub *This = (CFStub *)iface;
129 This->pUnkServer = pUnkServer;
130 IUnknown_AddRef(pUnkServer);
131 return S_OK;
134 static void WINAPI
135 CFStub_Disconnect(LPRPCSTUBBUFFER iface) {
136 CFStub *This = (CFStub *)iface;
138 if (This->pUnkServer) {
139 IUnknown_Release(This->pUnkServer);
140 This->pUnkServer = NULL;
144 static HRESULT WINAPI
145 CFStub_Invoke(
146 LPRPCSTUBBUFFER iface,RPCOLEMESSAGE* msg,IRpcChannelBuffer* chanbuf
148 CFStub *This = (CFStub *)iface;
149 HRESULT hres;
151 if (msg->iMethod == 3) { /* CreateInstance */
152 IID iid;
153 IClassFactory *classfac;
154 IUnknown *ppv;
155 IStream *pStm;
156 STATSTG ststg;
157 ULARGE_INTEGER newpos;
158 LARGE_INTEGER seekto;
159 ULONG res;
161 if (msg->cbBuffer < sizeof(IID)) {
162 FIXME("Not enough bytes in buffer (%d)?\n",msg->cbBuffer);
163 return E_FAIL;
165 memcpy(&iid,msg->Buffer,sizeof(iid));
166 TRACE("->CreateInstance(%s)\n",debugstr_guid(&iid));
167 hres = IUnknown_QueryInterface(This->pUnkServer,&IID_IClassFactory,(LPVOID*)&classfac);
168 if (hres) {
169 FIXME("Ole server does not provide an IClassFactory?\n");
170 return hres;
172 hres = IClassFactory_CreateInstance(classfac,NULL,&iid,(LPVOID*)&ppv);
173 IClassFactory_Release(classfac);
174 msg->cbBuffer = 0;
175 if (hres) {
176 FIXME("Failed to create an instance of %s\n",debugstr_guid(&iid));
177 goto getbuffer;
179 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
180 if (hres) {
181 FIXME("Failed to create stream on hglobal\n");
182 goto getbuffer;
184 hres = CoMarshalInterface(pStm,&iid,ppv,0,NULL,0);
185 IUnknown_Release((IUnknown*)ppv);
186 if (hres) {
187 FIXME("CoMarshalInterface failed, %x!\n",hres);
188 goto getbuffer;
190 hres = IStream_Stat(pStm,&ststg,0);
191 if (hres) {
192 FIXME("Stat failed.\n");
193 goto getbuffer;
196 msg->cbBuffer = ststg.cbSize.u.LowPart;
198 getbuffer:
199 IRpcChannelBuffer_GetBuffer(chanbuf, msg, &IID_IClassFactory);
200 if (hres) return hres;
202 seekto.u.LowPart = 0;seekto.u.HighPart = 0;
203 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
204 if (hres) {
205 FIXME("IStream_Seek failed, %x\n",hres);
206 return hres;
208 hres = IStream_Read(pStm,msg->Buffer,msg->cbBuffer,&res);
209 if (hres) {
210 FIXME("Stream Read failed, %x\n",hres);
211 return hres;
213 IStream_Release(pStm);
214 return S_OK;
216 FIXME("(%p,%p), stub!\n",msg,chanbuf);
217 FIXME("iMethod is %d\n",msg->iMethod);
218 FIXME("cbBuffer is %d\n",msg->cbBuffer);
219 return E_FAIL;
222 static LPRPCSTUBBUFFER WINAPI
223 CFStub_IsIIDSupported(LPRPCSTUBBUFFER iface,REFIID riid) {
224 FIXME("(%s), stub!\n",debugstr_guid(riid));
225 return NULL;
228 static ULONG WINAPI
229 CFStub_CountRefs(LPRPCSTUBBUFFER iface) {
230 FIXME("(), stub!\n");
231 return 1;
234 static HRESULT WINAPI
235 CFStub_DebugServerQueryInterface(LPRPCSTUBBUFFER iface,void** ppv) {
236 FIXME("(%p), stub!\n",ppv);
237 return E_FAIL;
239 static void WINAPI
240 CFStub_DebugServerRelease(LPRPCSTUBBUFFER iface,void *pv) {
241 FIXME("(%p), stub!\n",pv);
244 static const IRpcStubBufferVtbl cfstubvt = {
245 CFStub_QueryInterface,
246 CFStub_AddRef,
247 CFStub_Release,
248 CFStub_Connect,
249 CFStub_Disconnect,
250 CFStub_Invoke,
251 CFStub_IsIIDSupported,
252 CFStub_CountRefs,
253 CFStub_DebugServerQueryInterface,
254 CFStub_DebugServerRelease
257 static HRESULT
258 CFStub_Construct(LPRPCSTUBBUFFER *ppv) {
259 CFStub *cfstub;
260 cfstub = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(CFStub));
261 if (!cfstub)
262 return E_OUTOFMEMORY;
263 *ppv = (LPRPCSTUBBUFFER)cfstub;
264 cfstub->lpvtbl = &cfstubvt;
265 cfstub->ref = 1;
266 return S_OK;
269 /* Since we create proxy buffers and classfactory in a pair, there is
270 * no need for 2 separate structs. Just put them in one, but remember
271 * the refcount.
273 typedef struct _CFProxy {
274 const IClassFactoryVtbl *lpvtbl_cf;
275 const IRpcProxyBufferVtbl *lpvtbl_proxy;
276 LONG ref;
278 IRpcChannelBuffer *chanbuf;
279 IUnknown *outer_unknown;
280 } CFProxy;
282 static HRESULT WINAPI IRpcProxyBufferImpl_QueryInterface(LPRPCPROXYBUFFER iface,REFIID riid,LPVOID *ppv) {
283 *ppv = NULL;
284 if (IsEqualIID(riid,&IID_IRpcProxyBuffer)||IsEqualIID(riid,&IID_IUnknown)) {
285 IRpcProxyBuffer_AddRef(iface);
286 *ppv = (LPVOID)iface;
287 return S_OK;
289 FIXME("(%s), no interface.\n",debugstr_guid(riid));
290 return E_NOINTERFACE;
293 static ULONG WINAPI IRpcProxyBufferImpl_AddRef(LPRPCPROXYBUFFER iface) {
294 ICOM_THIS_MULTI(CFProxy,lpvtbl_proxy,iface);
295 return InterlockedIncrement(&This->ref);
298 static ULONG WINAPI IRpcProxyBufferImpl_Release(LPRPCPROXYBUFFER iface) {
299 ICOM_THIS_MULTI(CFProxy,lpvtbl_proxy,iface);
300 ULONG ref = InterlockedDecrement(&This->ref);
302 if (!ref) {
303 IRpcProxyBuffer_Disconnect(iface);
304 HeapFree(GetProcessHeap(),0,This);
306 return ref;
309 static HRESULT WINAPI IRpcProxyBufferImpl_Connect(LPRPCPROXYBUFFER iface,IRpcChannelBuffer* pRpcChannelBuffer) {
310 ICOM_THIS_MULTI(CFProxy,lpvtbl_proxy,iface);
312 This->chanbuf = pRpcChannelBuffer;
313 IRpcChannelBuffer_AddRef(This->chanbuf);
314 return S_OK;
316 static void WINAPI IRpcProxyBufferImpl_Disconnect(LPRPCPROXYBUFFER iface) {
317 ICOM_THIS_MULTI(CFProxy,lpvtbl_proxy,iface);
318 if (This->chanbuf) {
319 IRpcChannelBuffer_Release(This->chanbuf);
320 This->chanbuf = NULL;
324 static HRESULT WINAPI
325 CFProxy_QueryInterface(LPCLASSFACTORY iface,REFIID riid, LPVOID *ppv) {
326 ICOM_THIS_MULTI(CFProxy,lpvtbl_cf,iface);
327 if (This->outer_unknown) return IUnknown_QueryInterface(This->outer_unknown, riid, ppv);
328 *ppv = NULL;
329 if (IsEqualIID(&IID_IClassFactory,riid) || IsEqualIID(&IID_IUnknown,riid)) {
330 *ppv = (LPVOID)iface;
331 IClassFactory_AddRef(iface);
332 return S_OK;
334 if (IsEqualIID(riid,&IID_IMarshal)) /* just to avoid debug output */
335 return E_NOINTERFACE;
336 FIXME("Unhandled interface: %s\n",debugstr_guid(riid));
337 return E_NOINTERFACE;
340 static ULONG WINAPI CFProxy_AddRef(LPCLASSFACTORY iface) {
341 ICOM_THIS_MULTI(CFProxy,lpvtbl_cf,iface);
342 if (This->outer_unknown) return IUnknown_AddRef(This->outer_unknown);
343 return InterlockedIncrement(&This->ref);
346 static ULONG WINAPI CFProxy_Release(LPCLASSFACTORY iface) {
347 ICOM_THIS_MULTI(CFProxy,lpvtbl_cf,iface);
348 if (This->outer_unknown)
349 return IUnknown_Release(This->outer_unknown);
350 else
351 return IRpcProxyBufferImpl_Release((IRpcProxyBuffer *)&This->lpvtbl_proxy);
354 static HRESULT WINAPI CFProxy_CreateInstance(
355 LPCLASSFACTORY iface,
356 LPUNKNOWN pUnkOuter,/* [in] */
357 REFIID riid, /* [in] */
358 LPVOID *ppv /* [out] */
360 ICOM_THIS_MULTI(CFProxy,lpvtbl_cf,iface);
361 HRESULT hres;
362 LPSTREAM pStream;
363 HGLOBAL hGlobal;
364 ULONG srstatus;
365 RPCOLEMESSAGE msg;
367 TRACE("(%p,%s,%p)\n",pUnkOuter,debugstr_guid(riid),ppv);
369 /* Send CreateInstance to the remote classfactory.
371 * Data: Only the 'IID'.
373 msg.iMethod = 3;
374 msg.cbBuffer = sizeof(*riid);
375 msg.Buffer = NULL;
376 hres = IRpcChannelBuffer_GetBuffer(This->chanbuf,&msg,&IID_IClassFactory);
377 if (hres) {
378 FIXME("IRpcChannelBuffer_GetBuffer failed with %x?\n",hres);
379 return hres;
381 memcpy(msg.Buffer,riid,sizeof(*riid));
382 hres = IRpcChannelBuffer_SendReceive(This->chanbuf,&msg,&srstatus);
383 if (hres) {
384 FIXME("IRpcChannelBuffer_SendReceive failed with %x?\n",hres);
385 IRpcChannelBuffer_FreeBuffer(This->chanbuf,&msg);
386 return hres;
389 if (!msg.cbBuffer) { /* interface not found on remote */
390 IRpcChannelBuffer_FreeBuffer(This->chanbuf,&msg);
391 return srstatus;
394 /* We got back: [Marshalled Interface data] */
395 TRACE("got %d bytes data.\n",msg.cbBuffer);
396 hGlobal = GlobalAlloc(GMEM_MOVEABLE|GMEM_NODISCARD|GMEM_SHARE,msg.cbBuffer);
397 memcpy(GlobalLock(hGlobal),msg.Buffer,msg.cbBuffer);
398 hres = CreateStreamOnHGlobal(hGlobal,TRUE,&pStream);
399 if (hres) {
400 FIXME("CreateStreamOnHGlobal failed with %x\n",hres);
401 IRpcChannelBuffer_FreeBuffer(This->chanbuf,&msg);
402 return hres;
404 hres = CoUnmarshalInterface(
405 pStream,
406 riid,
409 IStream_Release(pStream); /* Does GlobalFree hGlobal too. */
411 IRpcChannelBuffer_FreeBuffer(This->chanbuf,&msg);
413 if (hres) {
414 FIXME("CoMarshalInterface failed, %x\n",hres);
415 return hres;
417 return S_OK;
420 static HRESULT WINAPI CFProxy_LockServer(LPCLASSFACTORY iface,BOOL fLock) {
421 /*ICOM_THIS_MULTI(CFProxy,lpvtbl_cf,iface);*/
422 FIXME("(%d), stub!\n",fLock);
423 /* basically: write BOOL, read empty */
424 return S_OK;
427 static const IRpcProxyBufferVtbl pspbvtbl = {
428 IRpcProxyBufferImpl_QueryInterface,
429 IRpcProxyBufferImpl_AddRef,
430 IRpcProxyBufferImpl_Release,
431 IRpcProxyBufferImpl_Connect,
432 IRpcProxyBufferImpl_Disconnect
434 static const IClassFactoryVtbl cfproxyvt = {
435 CFProxy_QueryInterface,
436 CFProxy_AddRef,
437 CFProxy_Release,
438 CFProxy_CreateInstance,
439 CFProxy_LockServer
442 static HRESULT
443 CFProxy_Construct(IUnknown *pUnkOuter, LPVOID *ppv,LPVOID *ppProxy) {
444 CFProxy *cf;
446 cf = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(CFProxy));
447 if (!cf)
448 return E_OUTOFMEMORY;
450 cf->lpvtbl_cf = &cfproxyvt;
451 cf->lpvtbl_proxy = &pspbvtbl;
452 /* one reference for the proxy buffer */
453 cf->ref = 1;
454 cf->outer_unknown = pUnkOuter;
455 *ppv = &(cf->lpvtbl_cf);
456 *ppProxy = &(cf->lpvtbl_proxy);
457 /* and one reference for the object */
458 IUnknown_AddRef((IUnknown *)*ppv);
459 return S_OK;
463 /********************* IRemUnknown Proxy/Stub ********************************/
465 typedef struct
467 const IRpcStubBufferVtbl *lpVtbl;
468 LONG refs;
469 IRemUnknown *iface;
470 } RemUnkStub;
472 static HRESULT WINAPI RemUnkStub_QueryInterface(LPRPCSTUBBUFFER iface,
473 REFIID riid,
474 LPVOID *obj)
476 RemUnkStub *This = (RemUnkStub *)iface;
477 TRACE("(%p)->QueryInterface(%s,%p)\n",This,debugstr_guid(riid),obj);
478 if (IsEqualGUID(&IID_IUnknown,riid) ||
479 IsEqualGUID(&IID_IRpcStubBuffer,riid)) {
480 *obj = This;
481 return S_OK;
483 return E_NOINTERFACE;
486 static ULONG WINAPI RemUnkStub_AddRef(LPRPCSTUBBUFFER iface)
488 RemUnkStub *This = (RemUnkStub *)iface;
489 TRACE("(%p)->AddRef()\n",This);
490 return InterlockedIncrement(&This->refs);
493 static ULONG WINAPI RemUnkStub_Release(LPRPCSTUBBUFFER iface)
495 RemUnkStub *This = (RemUnkStub *)iface;
496 ULONG refs;
497 TRACE("(%p)->Release()\n",This);
498 refs = InterlockedDecrement(&This->refs);
499 if (!refs)
500 HeapFree(GetProcessHeap(), 0, This);
501 return refs;
504 static HRESULT WINAPI RemUnkStub_Connect(LPRPCSTUBBUFFER iface,
505 LPUNKNOWN lpUnkServer)
507 RemUnkStub *This = (RemUnkStub *)iface;
508 TRACE("(%p)->Connect(%p)\n",This,lpUnkServer);
509 This->iface = (IRemUnknown*)lpUnkServer;
510 IRemUnknown_AddRef(This->iface);
511 return S_OK;
514 static void WINAPI RemUnkStub_Disconnect(LPRPCSTUBBUFFER iface)
516 RemUnkStub *This = (RemUnkStub *)iface;
517 TRACE("(%p)->Disconnect()\n",This);
518 IUnknown_Release(This->iface);
519 This->iface = NULL;
522 static HRESULT WINAPI RemUnkStub_Invoke(LPRPCSTUBBUFFER iface,
523 PRPCOLEMESSAGE pMsg,
524 LPRPCCHANNELBUFFER pChannel)
526 RemUnkStub *This = (RemUnkStub *)iface;
527 ULONG iMethod = pMsg->iMethod;
528 LPBYTE buf = pMsg->Buffer;
529 HRESULT hr = RPC_E_INVALIDMETHOD;
531 TRACE("(%p)->Invoke(%p,%p) method %d\n", This, pMsg, pChannel, iMethod);
532 switch (iMethod)
534 case 3: /* RemQueryInterface */
536 IPID ipid;
537 ULONG cRefs;
538 USHORT cIids;
539 IID *iids;
540 REMQIRESULT *pQIResults = NULL;
542 /* in */
543 memcpy(&ipid, buf, sizeof(ipid));
544 buf += sizeof(ipid);
545 memcpy(&cRefs, buf, sizeof(cRefs));
546 buf += sizeof(cRefs);
547 memcpy(&cIids, buf, sizeof(cIids));
548 buf += sizeof(cIids);
549 iids = (IID *)buf;
551 hr = IRemUnknown_RemQueryInterface(This->iface, &ipid, cRefs, cIids, iids, &pQIResults);
553 /* out */
554 pMsg->cbBuffer = cIids * sizeof(REMQIRESULT) + sizeof(HRESULT);
556 IRpcChannelBuffer_GetBuffer(pChannel, pMsg, &IID_IRemUnknown);
558 buf = pMsg->Buffer;
559 *(HRESULT *)buf = hr;
560 buf += sizeof(HRESULT);
562 if (hr) return hr;
563 /* FIXME: pQIResults is a unique pointer so pQIResults can be NULL! */
564 memcpy(buf, pQIResults, cIids * sizeof(REMQIRESULT));
566 break;
568 case 4: /* RemAddRef */
570 USHORT cIids;
571 REMINTERFACEREF *ir;
572 HRESULT *pResults;
574 /* in */
575 memcpy(&cIids, buf, sizeof(USHORT));
576 buf += sizeof(USHORT);
577 ir = (REMINTERFACEREF*)buf;
578 pResults = CoTaskMemAlloc(cIids * sizeof(HRESULT));
579 if (!pResults) return E_OUTOFMEMORY;
581 hr = IRemUnknown_RemAddRef(This->iface, cIids, ir, pResults);
583 /* out */
584 pMsg->cbBuffer = cIids * sizeof(HRESULT);
586 IRpcChannelBuffer_GetBuffer(pChannel, pMsg, &IID_IRemUnknown);
587 if (!hr)
589 buf = pMsg->Buffer;
590 memcpy(buf, pResults, cIids * sizeof(HRESULT));
593 CoTaskMemFree(pResults);
595 break;
597 case 5: /* RemRelease */
599 USHORT cIids;
600 REMINTERFACEREF *ir;
602 /* in */
603 memcpy(&cIids, buf, sizeof(USHORT));
604 buf += sizeof(USHORT);
605 ir = (REMINTERFACEREF*)buf;
607 hr = IRemUnknown_RemRelease(This->iface, cIids, ir);
609 /* out */
610 pMsg->cbBuffer = 0;
611 IRpcChannelBuffer_GetBuffer(pChannel, pMsg, &IID_IRemUnknown);
612 break;
615 return hr;
618 static LPRPCSTUBBUFFER WINAPI RemUnkStub_IsIIDSupported(LPRPCSTUBBUFFER iface,
619 REFIID riid)
621 RemUnkStub *This = (RemUnkStub *)iface;
622 TRACE("(%p)->IsIIDSupported(%s)\n", This, debugstr_guid(riid));
623 return IsEqualGUID(&IID_IRemUnknown, riid) ? iface : NULL;
626 static ULONG WINAPI RemUnkStub_CountRefs(LPRPCSTUBBUFFER iface)
628 RemUnkStub *This = (RemUnkStub *)iface;
629 FIXME("(%p)->CountRefs()\n", This);
630 return 1;
633 static HRESULT WINAPI RemUnkStub_DebugServerQueryInterface(LPRPCSTUBBUFFER iface,
634 LPVOID *ppv)
636 RemUnkStub *This = (RemUnkStub *)iface;
637 FIXME("(%p)->DebugServerQueryInterface(%p)\n",This,ppv);
638 return E_NOINTERFACE;
641 static void WINAPI RemUnkStub_DebugServerRelease(LPRPCSTUBBUFFER iface,
642 LPVOID pv)
644 RemUnkStub *This = (RemUnkStub *)iface;
645 FIXME("(%p)->DebugServerRelease(%p)\n", This, pv);
648 static const IRpcStubBufferVtbl RemUnkStub_VTable =
650 RemUnkStub_QueryInterface,
651 RemUnkStub_AddRef,
652 RemUnkStub_Release,
653 RemUnkStub_Connect,
654 RemUnkStub_Disconnect,
655 RemUnkStub_Invoke,
656 RemUnkStub_IsIIDSupported,
657 RemUnkStub_CountRefs,
658 RemUnkStub_DebugServerQueryInterface,
659 RemUnkStub_DebugServerRelease
662 static HRESULT RemUnkStub_Construct(IRpcStubBuffer **ppStub)
664 RemUnkStub *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
665 if (!This) return E_OUTOFMEMORY;
666 This->lpVtbl = &RemUnkStub_VTable;
667 This->refs = 1;
668 This->iface = NULL;
669 *ppStub = (IRpcStubBuffer*)This;
670 return S_OK;
674 typedef struct _RemUnkProxy {
675 const IRemUnknownVtbl *lpvtbl_remunk;
676 const IRpcProxyBufferVtbl *lpvtbl_proxy;
677 LONG refs;
679 IRpcChannelBuffer *chan;
680 IUnknown *outer_unknown;
681 } RemUnkProxy;
683 static HRESULT WINAPI RemUnkProxy_QueryInterface(LPREMUNKNOWN iface, REFIID riid, void **ppv)
685 RemUnkProxy *This = (RemUnkProxy *)iface;
686 if (This->outer_unknown)
687 return IUnknown_QueryInterface(This->outer_unknown, riid, ppv);
688 if (IsEqualIID(riid, &IID_IUnknown) ||
689 IsEqualIID(riid, &IID_IRemUnknown))
691 IRemUnknown_AddRef(iface);
692 *ppv = (LPVOID)iface;
693 return S_OK;
695 return E_NOINTERFACE;
698 static ULONG WINAPI RemUnkProxy_AddRef(LPREMUNKNOWN iface)
700 RemUnkProxy *This = (RemUnkProxy *)iface;
701 ULONG refs;
703 TRACE("(%p)->AddRef()\n",This);
705 if (This->outer_unknown)
706 refs = IUnknown_AddRef(This->outer_unknown);
707 else
708 refs = InterlockedIncrement(&This->refs);
709 return refs;
712 static ULONG WINAPI RemUnkProxy_Release(LPREMUNKNOWN iface)
714 RemUnkProxy *This = (RemUnkProxy *)iface;
716 TRACE("(%p)->Release()\n",This);
717 if (This->outer_unknown)
718 return IUnknown_Release(This->outer_unknown);
719 else
720 return IRpcProxyBufferImpl_Release((IRpcProxyBuffer *)&This->lpvtbl_proxy);
723 static HRESULT WINAPI RemUnkProxy_RemQueryInterface(LPREMUNKNOWN iface,
724 REFIPID ripid,
725 ULONG cRefs,
726 USHORT cIids,
727 IID* iids,
728 REMQIRESULT** ppQIResults)
730 RemUnkProxy *This = (RemUnkProxy *)iface;
731 RPCOLEMESSAGE msg;
732 HRESULT hr = S_OK;
733 ULONG status;
735 TRACE("(%p)->(%s,%d,%d,%p,%p)\n",This,
736 debugstr_guid(ripid),cRefs,cIids,iids,ppQIResults);
738 *ppQIResults = NULL;
739 memset(&msg, 0, sizeof(msg));
740 msg.iMethod = 3;
741 msg.cbBuffer = sizeof(IPID) + sizeof(ULONG) +
742 sizeof(USHORT) + cIids*sizeof(IID);
743 hr = IRpcChannelBuffer_GetBuffer(This->chan, &msg, &IID_IRemUnknown);
744 if (SUCCEEDED(hr)) {
745 LPBYTE buf = msg.Buffer;
746 memcpy(buf, ripid, sizeof(IPID));
747 buf += sizeof(IPID);
748 memcpy(buf, &cRefs, sizeof(ULONG));
749 buf += sizeof(ULONG);
750 memcpy(buf, &cIids, sizeof(USHORT));
751 buf += sizeof(USHORT);
752 memcpy(buf, iids, cIids*sizeof(IID));
754 hr = IRpcChannelBuffer_SendReceive(This->chan, &msg, &status);
756 buf = msg.Buffer;
758 if (SUCCEEDED(hr)) {
759 hr = *(HRESULT *)buf;
760 buf += sizeof(HRESULT);
763 if (SUCCEEDED(hr)) {
764 *ppQIResults = CoTaskMemAlloc(cIids*sizeof(REMQIRESULT));
765 memcpy(*ppQIResults, buf, cIids*sizeof(REMQIRESULT));
768 IRpcChannelBuffer_FreeBuffer(This->chan, &msg);
771 return hr;
774 static HRESULT WINAPI RemUnkProxy_RemAddRef(LPREMUNKNOWN iface,
775 USHORT cInterfaceRefs,
776 REMINTERFACEREF* InterfaceRefs,
777 HRESULT* pResults)
779 RemUnkProxy *This = (RemUnkProxy *)iface;
780 RPCOLEMESSAGE msg;
781 HRESULT hr = S_OK;
782 ULONG status;
784 TRACE("(%p)->(%d,%p,%p)\n",This,
785 cInterfaceRefs,InterfaceRefs,pResults);
787 memset(&msg, 0, sizeof(msg));
788 msg.iMethod = 4;
789 msg.cbBuffer = sizeof(USHORT) + cInterfaceRefs*sizeof(REMINTERFACEREF);
790 hr = IRpcChannelBuffer_GetBuffer(This->chan, &msg, &IID_IRemUnknown);
791 if (SUCCEEDED(hr)) {
792 LPBYTE buf = msg.Buffer;
793 memcpy(buf, &cInterfaceRefs, sizeof(USHORT));
794 buf += sizeof(USHORT);
795 memcpy(buf, InterfaceRefs, cInterfaceRefs*sizeof(REMINTERFACEREF));
797 hr = IRpcChannelBuffer_SendReceive(This->chan, &msg, &status);
799 if (SUCCEEDED(hr)) {
800 buf = msg.Buffer;
801 memcpy(pResults, buf, cInterfaceRefs*sizeof(HRESULT));
804 IRpcChannelBuffer_FreeBuffer(This->chan, &msg);
807 return hr;
810 static HRESULT WINAPI RemUnkProxy_RemRelease(LPREMUNKNOWN iface,
811 USHORT cInterfaceRefs,
812 REMINTERFACEREF* InterfaceRefs)
814 RemUnkProxy *This = (RemUnkProxy *)iface;
815 RPCOLEMESSAGE msg;
816 HRESULT hr = S_OK;
817 ULONG status;
819 TRACE("(%p)->(%d,%p)\n",This,
820 cInterfaceRefs,InterfaceRefs);
822 memset(&msg, 0, sizeof(msg));
823 msg.iMethod = 5;
824 msg.cbBuffer = sizeof(USHORT) + cInterfaceRefs*sizeof(REMINTERFACEREF);
825 hr = IRpcChannelBuffer_GetBuffer(This->chan, &msg, &IID_IRemUnknown);
826 if (SUCCEEDED(hr)) {
827 LPBYTE buf = msg.Buffer;
828 memcpy(buf, &cInterfaceRefs, sizeof(USHORT));
829 buf += sizeof(USHORT);
830 memcpy(buf, InterfaceRefs, cInterfaceRefs*sizeof(REMINTERFACEREF));
832 hr = IRpcChannelBuffer_SendReceive(This->chan, &msg, &status);
834 IRpcChannelBuffer_FreeBuffer(This->chan, &msg);
837 return hr;
840 static const IRemUnknownVtbl RemUnkProxy_VTable =
842 RemUnkProxy_QueryInterface,
843 RemUnkProxy_AddRef,
844 RemUnkProxy_Release,
845 RemUnkProxy_RemQueryInterface,
846 RemUnkProxy_RemAddRef,
847 RemUnkProxy_RemRelease
851 static HRESULT WINAPI RURpcProxyBufferImpl_QueryInterface(LPRPCPROXYBUFFER iface,REFIID riid,LPVOID *ppv) {
852 *ppv = NULL;
853 if (IsEqualIID(riid,&IID_IRpcProxyBuffer)||IsEqualIID(riid,&IID_IUnknown)) {
854 IRpcProxyBuffer_AddRef(iface);
855 *ppv = (LPVOID)iface;
856 return S_OK;
858 FIXME("(%s), no interface.\n",debugstr_guid(riid));
859 return E_NOINTERFACE;
862 static ULONG WINAPI RURpcProxyBufferImpl_AddRef(LPRPCPROXYBUFFER iface) {
863 ICOM_THIS_MULTI(RemUnkProxy,lpvtbl_proxy,iface);
864 TRACE("%p, %d\n", iface, This->refs + 1);
865 return InterlockedIncrement(&This->refs);
868 static ULONG WINAPI RURpcProxyBufferImpl_Release(LPRPCPROXYBUFFER iface) {
869 ICOM_THIS_MULTI(RemUnkProxy,lpvtbl_proxy,iface);
870 ULONG ref = InterlockedDecrement(&This->refs);
871 TRACE("%p, %d\n", iface, ref);
872 if (!ref) {
873 IRpcProxyBuffer_Disconnect(iface);
874 HeapFree(GetProcessHeap(),0,This);
876 return ref;
879 static HRESULT WINAPI RURpcProxyBufferImpl_Connect(LPRPCPROXYBUFFER iface,IRpcChannelBuffer* pRpcChannelBuffer) {
880 ICOM_THIS_MULTI(RemUnkProxy,lpvtbl_proxy,iface);
882 TRACE("%p, %p\n", iface, pRpcChannelBuffer);
883 This->chan = pRpcChannelBuffer;
884 IRpcChannelBuffer_AddRef(This->chan);
885 return S_OK;
887 static void WINAPI RURpcProxyBufferImpl_Disconnect(LPRPCPROXYBUFFER iface) {
888 ICOM_THIS_MULTI(RemUnkProxy,lpvtbl_proxy,iface);
889 TRACE("%p, %p\n", iface, This->chan);
890 if (This->chan) {
891 IRpcChannelBuffer_Release(This->chan);
892 This->chan = NULL;
897 static const IRpcProxyBufferVtbl RURpcProxyBuffer_VTable = {
898 RURpcProxyBufferImpl_QueryInterface,
899 RURpcProxyBufferImpl_AddRef,
900 RURpcProxyBufferImpl_Release,
901 RURpcProxyBufferImpl_Connect,
902 RURpcProxyBufferImpl_Disconnect
905 static HRESULT
906 RemUnkProxy_Construct(IUnknown *pUnkOuter, LPVOID *ppv,LPVOID *ppProxy) {
907 RemUnkProxy *This;
909 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*This));
910 if (!This)
911 return E_OUTOFMEMORY;
913 This->lpvtbl_remunk = &RemUnkProxy_VTable;
914 This->lpvtbl_proxy = &RURpcProxyBuffer_VTable;
915 /* only one reference for the proxy buffer */
916 This->refs = 1;
917 This->outer_unknown = pUnkOuter;
918 *ppv = &(This->lpvtbl_remunk);
919 *ppProxy = &(This->lpvtbl_proxy);
920 /* and one reference for the object */
921 IUnknown_AddRef((IUnknown *)*ppv);
922 return S_OK;
926 /********************* OLE Proxy/Stub Factory ********************************/
927 static HRESULT WINAPI
928 PSFacBuf_QueryInterface(LPPSFACTORYBUFFER iface, REFIID iid, LPVOID *ppv) {
929 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)||IsEqualIID(iid,&IID_IUnknown)) {
930 *ppv = (LPVOID)iface;
931 /* No ref counting, static class */
932 return S_OK;
934 FIXME("(%s) unknown IID?\n",debugstr_guid(iid));
935 return E_NOINTERFACE;
938 static ULONG WINAPI PSFacBuf_AddRef(LPPSFACTORYBUFFER iface) { return 2; }
939 static ULONG WINAPI PSFacBuf_Release(LPPSFACTORYBUFFER iface) { return 1; }
941 static HRESULT WINAPI
942 PSFacBuf_CreateProxy(
943 LPPSFACTORYBUFFER iface, IUnknown* pUnkOuter, REFIID riid,
944 IRpcProxyBuffer **ppProxy, LPVOID *ppv
946 if (IsEqualIID(&IID_IClassFactory,riid))
947 return CFProxy_Construct(pUnkOuter, ppv,(LPVOID*)ppProxy);
948 else if (IsEqualIID(&IID_IRemUnknown,riid))
949 return RemUnkProxy_Construct(pUnkOuter, ppv,(LPVOID*)ppProxy);
950 FIXME("proxying not implemented for (%s) yet!\n",debugstr_guid(riid));
951 return E_FAIL;
954 static HRESULT WINAPI
955 PSFacBuf_CreateStub(
956 LPPSFACTORYBUFFER iface, REFIID riid,IUnknown *pUnkServer,
957 IRpcStubBuffer** ppStub
959 HRESULT hres;
961 TRACE("(%s,%p,%p)\n",debugstr_guid(riid),pUnkServer,ppStub);
963 if (IsEqualIID(&IID_IClassFactory, riid) ||
964 IsEqualIID(&IID_IUnknown, riid) /* FIXME: fixup stub manager and remove this*/) {
965 hres = CFStub_Construct(ppStub);
966 if (!hres)
967 IRpcStubBuffer_Connect((*ppStub),pUnkServer);
968 return hres;
969 } else if (IsEqualIID(&IID_IRemUnknown,riid)) {
970 hres = RemUnkStub_Construct(ppStub);
971 if (!hres)
972 IRpcStubBuffer_Connect((*ppStub),pUnkServer);
973 return hres;
975 FIXME("stubbing not implemented for (%s) yet!\n",debugstr_guid(riid));
976 return E_FAIL;
979 static const IPSFactoryBufferVtbl psfacbufvtbl = {
980 PSFacBuf_QueryInterface,
981 PSFacBuf_AddRef,
982 PSFacBuf_Release,
983 PSFacBuf_CreateProxy,
984 PSFacBuf_CreateStub
987 /* This is the whole PSFactoryBuffer object, just the vtableptr */
988 static const IPSFactoryBufferVtbl *lppsfac = &psfacbufvtbl;
990 /***********************************************************************
991 * DllGetClassObject [OLE32.@]
993 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID iid,LPVOID *ppv)
995 *ppv = NULL;
996 if (IsEqualIID(rclsid, &CLSID_PSFactoryBuffer))
997 return IPSFactoryBuffer_QueryInterface((IPSFactoryBuffer *)&lppsfac, iid, ppv);
998 if (IsEqualIID(rclsid,&CLSID_DfMarshal)&&(
999 IsEqualIID(iid,&IID_IClassFactory) ||
1000 IsEqualIID(iid,&IID_IUnknown)
1003 return MARSHAL_GetStandardMarshalCF(ppv);
1004 if (IsEqualIID(rclsid,&CLSID_StdGlobalInterfaceTable) && (IsEqualIID(iid,&IID_IClassFactory) || IsEqualIID(iid,&IID_IUnknown)))
1005 return StdGlobalInterfaceTable_GetFactory(ppv);
1006 if (IsEqualCLSID(rclsid, &CLSID_FileMoniker))
1007 return FileMonikerCF_Create(iid, ppv);
1008 if (IsEqualCLSID(rclsid, &CLSID_ItemMoniker))
1009 return ItemMonikerCF_Create(iid, ppv);
1010 if (IsEqualCLSID(rclsid, &CLSID_AntiMoniker))
1011 return AntiMonikerCF_Create(iid, ppv);
1012 if (IsEqualCLSID(rclsid, &CLSID_CompositeMoniker))
1013 return CompositeMonikerCF_Create(iid, ppv);
1014 if (IsEqualCLSID(rclsid, &CLSID_ClassMoniker))
1015 return ClassMonikerCF_Create(iid, ppv);
1017 FIXME("\n\tCLSID:\t%s,\n\tIID:\t%s\n",debugstr_guid(rclsid),debugstr_guid(iid));
1018 return CLASS_E_CLASSNOTAVAILABLE;