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