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