Sync trunk.
[reactos.git] / dll / win32 / ole32 / marshal.c
1 /*
2 * Marshalling library
3 *
4 * Copyright 2002 Marcus Meissner
5 * Copyright 2004 Mike Hearn, for CodeWeavers
6 * Copyright 2004 Rob Shearman, for CodeWeavers
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 */
22
23 #include <stdarg.h>
24 #include <string.h>
25 #include <assert.h>
26
27 #define COBJMACROS
28
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winuser.h"
32 #include "objbase.h"
33 #include "ole2.h"
34 #include "winerror.h"
35 #include "wine/unicode.h"
36
37 #include "compobj_private.h"
38
39 #include "wine/debug.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(ole);
42
43 extern const CLSID CLSID_DfMarshal;
44
45 /* number of refs given out for normal marshaling */
46 #define NORMALEXTREFS 5
47
48
49 /* private flag indicating that the object was marshaled as table-weak */
50 #define SORFP_TABLEWEAK SORF_OXRES1
51 /* private flag indicating that the caller does not want to notify the stub
52 * when the proxy disconnects or is destroyed */
53 #define SORFP_NOLIFETIMEMGMT SORF_OXRES2
54
55 static HRESULT unmarshal_object(const STDOBJREF *stdobjref, APARTMENT *apt,
56 MSHCTX dest_context, void *dest_context_data,
57 REFIID riid, const OXID_INFO *oxid_info,
58 void **object);
59
60 /* Marshalling just passes a unique identifier to the remote client,
61 * that makes it possible to find the passed interface again.
62 *
63 * So basically we need a set of values that make it unique.
64 *
65 * Note that the IUnknown_QI(ob,xiid,&ppv) always returns the SAME ppv value!
66 *
67 * A triple is used: OXID (apt id), OID (stub manager id),
68 * IPID (interface ptr/stub id).
69 *
70 * OXIDs identify an apartment and are network scoped
71 * OIDs identify a stub manager and are apartment scoped
72 * IPIDs identify an interface stub and are apartment scoped
73 */
74
75 static inline HRESULT get_facbuf_for_iid(REFIID riid, IPSFactoryBuffer **facbuf)
76 {
77 HRESULT hr;
78 CLSID clsid;
79
80 if ((hr = CoGetPSClsid(riid, &clsid)))
81 return hr;
82 return CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER | WINE_CLSCTX_DONT_HOST,
83 NULL, &IID_IPSFactoryBuffer, (LPVOID*)facbuf);
84 }
85
86 /* marshals an object into a STDOBJREF structure */
87 HRESULT marshal_object(APARTMENT *apt, STDOBJREF *stdobjref, REFIID riid, IUnknown *object, MSHLFLAGS mshlflags)
88 {
89 struct stub_manager *manager;
90 struct ifstub *ifstub;
91 BOOL tablemarshal;
92 IRpcStubBuffer *stub = NULL;
93 HRESULT hr;
94 IUnknown *iobject = NULL; /* object of type riid */
95
96 hr = apartment_getoxid(apt, &stdobjref->oxid);
97 if (hr != S_OK)
98 return hr;
99
100 hr = apartment_createwindowifneeded(apt);
101 if (hr != S_OK)
102 return hr;
103
104 hr = IUnknown_QueryInterface(object, riid, (void **)&iobject);
105 if (hr != S_OK)
106 {
107 ERR("object doesn't expose interface %s, failing with error 0x%08x\n",
108 debugstr_guid(riid), hr);
109 return E_NOINTERFACE;
110 }
111
112 /* IUnknown doesn't require a stub buffer, because it never goes out on
113 * the wire */
114 if (!IsEqualIID(riid, &IID_IUnknown))
115 {
116 IPSFactoryBuffer *psfb;
117
118 hr = get_facbuf_for_iid(riid, &psfb);
119 if (hr != S_OK)
120 {
121 ERR("couldn't get IPSFactory buffer for interface %s\n", debugstr_guid(riid));
122 IUnknown_Release(iobject);
123 return hr;
124 }
125
126 hr = IPSFactoryBuffer_CreateStub(psfb, riid, iobject, &stub);
127 IPSFactoryBuffer_Release(psfb);
128 if (hr != S_OK)
129 {
130 ERR("Failed to create an IRpcStubBuffer from IPSFactory for %s with error 0x%08x\n",
131 debugstr_guid(riid), hr);
132 IUnknown_Release(iobject);
133 return hr;
134 }
135 }
136
137 stdobjref->flags = SORF_NULL;
138 if (mshlflags & MSHLFLAGS_TABLEWEAK)
139 stdobjref->flags |= SORFP_TABLEWEAK;
140 if (mshlflags & MSHLFLAGS_NOPING)
141 stdobjref->flags |= SORF_NOPING;
142
143 if ((manager = get_stub_manager_from_object(apt, object)))
144 TRACE("registering new ifstub on pre-existing manager\n");
145 else
146 {
147 TRACE("constructing new stub manager\n");
148
149 manager = new_stub_manager(apt, object);
150 if (!manager)
151 {
152 if (stub) IRpcStubBuffer_Release(stub);
153 IUnknown_Release(iobject);
154 return E_OUTOFMEMORY;
155 }
156 }
157 stdobjref->oid = manager->oid;
158
159 tablemarshal = ((mshlflags & MSHLFLAGS_TABLESTRONG) || (mshlflags & MSHLFLAGS_TABLEWEAK));
160
161 /* make sure ifstub that we are creating is unique */
162 ifstub = stub_manager_find_ifstub(manager, riid, mshlflags);
163 if (!ifstub)
164 ifstub = stub_manager_new_ifstub(manager, stub, iobject, riid, mshlflags);
165
166 if (stub) IRpcStubBuffer_Release(stub);
167 IUnknown_Release(iobject);
168
169 if (!ifstub)
170 {
171 stub_manager_int_release(manager);
172 /* destroy the stub manager if it has no ifstubs by releasing
173 * zero external references */
174 stub_manager_ext_release(manager, 0, FALSE, TRUE);
175 return E_OUTOFMEMORY;
176 }
177
178 if (!tablemarshal)
179 {
180 stdobjref->cPublicRefs = NORMALEXTREFS;
181 stub_manager_ext_addref(manager, stdobjref->cPublicRefs, FALSE);
182 }
183 else
184 {
185 stdobjref->cPublicRefs = 0;
186 if (mshlflags & MSHLFLAGS_TABLESTRONG)
187 stub_manager_ext_addref(manager, 1, FALSE);
188 else
189 stub_manager_ext_addref(manager, 0, TRUE);
190 }
191
192 /* FIXME: check return value */
193 RPC_RegisterInterface(riid);
194
195 stdobjref->ipid = ifstub->ipid;
196
197 stub_manager_int_release(manager);
198 return S_OK;
199 }
200
201
202
203 /* Client-side identity of the server object */
204
205 static HRESULT proxy_manager_get_remunknown(struct proxy_manager * This, IRemUnknown **remunk);
206 static void proxy_manager_destroy(struct proxy_manager * This);
207 static HRESULT proxy_manager_find_ifproxy(struct proxy_manager * This, REFIID riid, struct ifproxy ** ifproxy_found);
208 static HRESULT proxy_manager_query_local_interface(struct proxy_manager * This, REFIID riid, void ** ppv);
209
210 static HRESULT WINAPI ClientIdentity_QueryInterface(IMultiQI * iface, REFIID riid, void ** ppv)
211 {
212 HRESULT hr;
213 MULTI_QI mqi;
214
215 TRACE("%s\n", debugstr_guid(riid));
216
217 mqi.pIID = riid;
218 hr = IMultiQI_QueryMultipleInterfaces(iface, 1, &mqi);
219 *ppv = mqi.pItf;
220
221 return hr;
222 }
223
224 static ULONG WINAPI ClientIdentity_AddRef(IMultiQI * iface)
225 {
226 struct proxy_manager * This = (struct proxy_manager *)iface;
227 TRACE("%p - before %d\n", iface, This->refs);
228 return InterlockedIncrement(&This->refs);
229 }
230
231 static ULONG WINAPI ClientIdentity_Release(IMultiQI * iface)
232 {
233 struct proxy_manager * This = (struct proxy_manager *)iface;
234 ULONG refs = InterlockedDecrement(&This->refs);
235 TRACE("%p - after %d\n", iface, refs);
236 if (!refs)
237 proxy_manager_destroy(This);
238 return refs;
239 }
240
241 static HRESULT WINAPI ClientIdentity_QueryMultipleInterfaces(IMultiQI *iface, ULONG cMQIs, MULTI_QI *pMQIs)
242 {
243 struct proxy_manager * This = (struct proxy_manager *)iface;
244 REMQIRESULT *qiresults = NULL;
245 ULONG nonlocal_mqis = 0;
246 ULONG i;
247 ULONG successful_mqis = 0;
248 IID *iids = HeapAlloc(GetProcessHeap(), 0, cMQIs * sizeof(*iids));
249 /* mapping of RemQueryInterface index to QueryMultipleInterfaces index */
250 ULONG *mapping = HeapAlloc(GetProcessHeap(), 0, cMQIs * sizeof(*mapping));
251
252 TRACE("cMQIs: %d\n", cMQIs);
253
254 /* try to get a local interface - this includes already active proxy
255 * interfaces and also interfaces exposed by the proxy manager */
256 for (i = 0; i < cMQIs; i++)
257 {
258 TRACE("iid[%d] = %s\n", i, debugstr_guid(pMQIs[i].pIID));
259 pMQIs[i].hr = proxy_manager_query_local_interface(This, pMQIs[i].pIID, (void **)&pMQIs[i].pItf);
260 if (pMQIs[i].hr == S_OK)
261 successful_mqis++;
262 else
263 {
264 iids[nonlocal_mqis] = *pMQIs[i].pIID;
265 mapping[nonlocal_mqis] = i;
266 nonlocal_mqis++;
267 }
268 }
269
270 TRACE("%d interfaces not found locally\n", nonlocal_mqis);
271
272 /* if we have more than one interface not found locally then we must try
273 * to query the remote object for it */
274 if (nonlocal_mqis != 0)
275 {
276 IRemUnknown *remunk;
277 HRESULT hr;
278 IPID *ipid;
279
280 /* get the ipid of the first entry */
281 /* FIXME: should we implement ClientIdentity on the ifproxies instead
282 * of the proxy_manager so we use the correct ipid here? */
283 ipid = &LIST_ENTRY(list_head(&This->interfaces), struct ifproxy, entry)->stdobjref.ipid;
284
285 /* get IRemUnknown proxy so we can communicate with the remote object */
286 hr = proxy_manager_get_remunknown(This, &remunk);
287
288 if (SUCCEEDED(hr))
289 {
290 hr = IRemUnknown_RemQueryInterface(remunk, ipid, NORMALEXTREFS,
291 nonlocal_mqis, iids, &qiresults);
292 IRemUnknown_Release(remunk);
293 if (FAILED(hr))
294 ERR("IRemUnknown_RemQueryInterface failed with error 0x%08x\n", hr);
295 }
296
297 /* IRemUnknown_RemQueryInterface can return S_FALSE if only some of
298 * the interfaces were returned */
299 if (SUCCEEDED(hr))
300 {
301 /* try to unmarshal each object returned to us */
302 for (i = 0; i < nonlocal_mqis; i++)
303 {
304 ULONG index = mapping[i];
305 HRESULT hrobj = qiresults[i].hResult;
306 if (hrobj == S_OK)
307 hrobj = unmarshal_object(&qiresults[i].std, COM_CurrentApt(),
308 This->dest_context,
309 This->dest_context_data,
310 pMQIs[index].pIID, &This->oxid_info,
311 (void **)&pMQIs[index].pItf);
312
313 if (hrobj == S_OK)
314 successful_mqis++;
315 else
316 ERR("Failed to get pointer to interface %s\n", debugstr_guid(pMQIs[index].pIID));
317 pMQIs[index].hr = hrobj;
318 }
319 }
320
321 /* free the memory allocated by the proxy */
322 CoTaskMemFree(qiresults);
323 }
324
325 TRACE("%d/%d successfully queried\n", successful_mqis, cMQIs);
326
327 HeapFree(GetProcessHeap(), 0, iids);
328 HeapFree(GetProcessHeap(), 0, mapping);
329
330 if (successful_mqis == cMQIs)
331 return S_OK; /* we got all requested interfaces */
332 else if (successful_mqis == 0)
333 return E_NOINTERFACE; /* we didn't get any interfaces */
334 else
335 return S_FALSE; /* we got some interfaces */
336 }
337
338 static const IMultiQIVtbl ClientIdentity_Vtbl =
339 {
340 ClientIdentity_QueryInterface,
341 ClientIdentity_AddRef,
342 ClientIdentity_Release,
343 ClientIdentity_QueryMultipleInterfaces
344 };
345
346 /* FIXME: remove these */
347 static HRESULT WINAPI StdMarshalImpl_GetUnmarshalClass(LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext, void* pvDestContext, DWORD mshlflags, CLSID* pCid);
348 static HRESULT WINAPI StdMarshalImpl_GetMarshalSizeMax(LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext, void* pvDestContext, DWORD mshlflags, DWORD* pSize);
349 static HRESULT WINAPI StdMarshalImpl_UnmarshalInterface(LPMARSHAL iface, IStream *pStm, REFIID riid, void **ppv);
350 static HRESULT WINAPI StdMarshalImpl_ReleaseMarshalData(LPMARSHAL iface, IStream *pStm);
351 static HRESULT WINAPI StdMarshalImpl_DisconnectObject(LPMARSHAL iface, DWORD dwReserved);
352
353 static HRESULT WINAPI Proxy_QueryInterface(IMarshal *iface, REFIID riid, void **ppvObject)
354 {
355 ICOM_THIS_MULTI(struct proxy_manager, lpVtblMarshal, iface);
356 return IMultiQI_QueryInterface((IMultiQI *)&This->lpVtbl, riid, ppvObject);
357 }
358
359 static ULONG WINAPI Proxy_AddRef(IMarshal *iface)
360 {
361 ICOM_THIS_MULTI(struct proxy_manager, lpVtblMarshal, iface);
362 return IMultiQI_AddRef((IMultiQI *)&This->lpVtbl);
363 }
364
365 static ULONG WINAPI Proxy_Release(IMarshal *iface)
366 {
367 ICOM_THIS_MULTI(struct proxy_manager, lpVtblMarshal, iface);
368 return IMultiQI_Release((IMultiQI *)&This->lpVtbl);
369 }
370
371 static HRESULT WINAPI Proxy_MarshalInterface(
372 LPMARSHAL iface, IStream *pStm, REFIID riid, void* pv, DWORD dwDestContext,
373 void* pvDestContext, DWORD mshlflags)
374 {
375 ICOM_THIS_MULTI(struct proxy_manager, lpVtblMarshal, iface);
376 HRESULT hr;
377 struct ifproxy *ifproxy;
378
379 TRACE("(...,%s,...)\n", debugstr_guid(riid));
380
381 hr = proxy_manager_find_ifproxy(This, riid, &ifproxy);
382 if (SUCCEEDED(hr))
383 {
384 STDOBJREF stdobjref = ifproxy->stdobjref;
385
386 stdobjref.cPublicRefs = 0;
387
388 if ((mshlflags != MSHLFLAGS_TABLEWEAK) &&
389 (mshlflags != MSHLFLAGS_TABLESTRONG))
390 {
391 ULONG cPublicRefs = ifproxy->refs;
392 ULONG cPublicRefsOld;
393 /* optimization - share out proxy's public references if possible
394 * instead of making new proxy do a roundtrip through the server */
395 do
396 {
397 ULONG cPublicRefsNew;
398 cPublicRefsOld = cPublicRefs;
399 stdobjref.cPublicRefs = cPublicRefs / 2;
400 cPublicRefsNew = cPublicRefs - stdobjref.cPublicRefs;
401 cPublicRefs = InterlockedCompareExchange(
402 (LONG *)&ifproxy->refs, cPublicRefsNew, cPublicRefsOld);
403 } while (cPublicRefs != cPublicRefsOld);
404 }
405
406 /* normal and table-strong marshaling need at least one reference */
407 if (!stdobjref.cPublicRefs && (mshlflags != MSHLFLAGS_TABLEWEAK))
408 {
409 IRemUnknown *remunk;
410 hr = proxy_manager_get_remunknown(This, &remunk);
411 if (hr == S_OK)
412 {
413 HRESULT hrref = S_OK;
414 REMINTERFACEREF rif;
415 rif.ipid = ifproxy->stdobjref.ipid;
416 rif.cPublicRefs = (mshlflags == MSHLFLAGS_TABLESTRONG) ? 1 : NORMALEXTREFS;
417 rif.cPrivateRefs = 0;
418 hr = IRemUnknown_RemAddRef(remunk, 1, &rif, &hrref);
419 IRemUnknown_Release(remunk);
420 if (hr == S_OK && hrref == S_OK)
421 {
422 /* table-strong marshaling doesn't give the refs to the
423 * client that unmarshals the STDOBJREF */
424 if (mshlflags != MSHLFLAGS_TABLESTRONG)
425 stdobjref.cPublicRefs = rif.cPublicRefs;
426 }
427 else
428 ERR("IRemUnknown_RemAddRef returned with 0x%08x, hrref = 0x%08x\n", hr, hrref);
429 }
430 }
431
432 if (SUCCEEDED(hr))
433 {
434 TRACE("writing stdobjref: flags = %04x cPublicRefs = %d oxid = %s oid = %s ipid = %s\n",
435 stdobjref.flags, stdobjref.cPublicRefs,
436 wine_dbgstr_longlong(stdobjref.oxid),
437 wine_dbgstr_longlong(stdobjref.oid),
438 debugstr_guid(&stdobjref.ipid));
439 hr = IStream_Write(pStm, &stdobjref, sizeof(stdobjref), NULL);
440 }
441 }
442 else
443 {
444 /* we don't have the interface already unmarshaled so we have to
445 * request the object from the server */
446 IRemUnknown *remunk;
447 IPID *ipid;
448 REMQIRESULT *qiresults = NULL;
449 IID iid = *riid;
450
451 /* get the ipid of the first entry */
452 /* FIXME: should we implement ClientIdentity on the ifproxies instead
453 * of the proxy_manager so we use the correct ipid here? */
454 ipid = &LIST_ENTRY(list_head(&This->interfaces), struct ifproxy, entry)->stdobjref.ipid;
455
456 /* get IRemUnknown proxy so we can communicate with the remote object */
457 hr = proxy_manager_get_remunknown(This, &remunk);
458
459 if (hr == S_OK)
460 {
461 hr = IRemUnknown_RemQueryInterface(remunk, ipid, NORMALEXTREFS,
462 1, &iid, &qiresults);
463 if (SUCCEEDED(hr))
464 {
465 hr = IStream_Write(pStm, &qiresults->std, sizeof(qiresults->std), NULL);
466 if (FAILED(hr))
467 {
468 REMINTERFACEREF rif;
469 rif.ipid = qiresults->std.ipid;
470 rif.cPublicRefs = qiresults->std.cPublicRefs;
471 rif.cPrivateRefs = 0;
472 IRemUnknown_RemRelease(remunk, 1, &rif);
473 }
474 CoTaskMemFree(qiresults);
475 }
476 else
477 ERR("IRemUnknown_RemQueryInterface failed with error 0x%08x\n", hr);
478 IRemUnknown_Release(remunk);
479 }
480 }
481
482 return hr;
483 }
484
485 static const IMarshalVtbl ProxyMarshal_Vtbl =
486 {
487 Proxy_QueryInterface,
488 Proxy_AddRef,
489 Proxy_Release,
490 StdMarshalImpl_GetUnmarshalClass,
491 StdMarshalImpl_GetMarshalSizeMax,
492 Proxy_MarshalInterface,
493 StdMarshalImpl_UnmarshalInterface,
494 StdMarshalImpl_ReleaseMarshalData,
495 StdMarshalImpl_DisconnectObject
496 };
497
498 static HRESULT WINAPI ProxyCliSec_QueryInterface(IClientSecurity *iface, REFIID riid, void **ppvObject)
499 {
500 ICOM_THIS_MULTI(struct proxy_manager, lpVtblCliSec, iface);
501 return IMultiQI_QueryInterface((IMultiQI *)&This->lpVtbl, riid, ppvObject);
502 }
503
504 static ULONG WINAPI ProxyCliSec_AddRef(IClientSecurity *iface)
505 {
506 ICOM_THIS_MULTI(struct proxy_manager, lpVtblCliSec, iface);
507 return IMultiQI_AddRef((IMultiQI *)&This->lpVtbl);
508 }
509
510 static ULONG WINAPI ProxyCliSec_Release(IClientSecurity *iface)
511 {
512 ICOM_THIS_MULTI(struct proxy_manager, lpVtblCliSec, iface);
513 return IMultiQI_Release((IMultiQI *)&This->lpVtbl);
514 }
515
516 static HRESULT WINAPI ProxyCliSec_QueryBlanket(IClientSecurity *iface,
517 IUnknown *pProxy,
518 DWORD *pAuthnSvc,
519 DWORD *pAuthzSvc,
520 OLECHAR **ppServerPrincName,
521 DWORD *pAuthnLevel,
522 DWORD *pImpLevel,
523 void **pAuthInfo,
524 DWORD *pCapabilities)
525 {
526 FIXME("(%p, %p, %p, %p, %p, %p, %p, %p): stub\n", pProxy, pAuthnSvc,
527 pAuthzSvc, ppServerPrincName, pAuthnLevel, pImpLevel, pAuthInfo,
528 pCapabilities);
529
530 if (pAuthnSvc)
531 *pAuthnSvc = 0;
532 if (pAuthzSvc)
533 *pAuthzSvc = 0;
534 if (ppServerPrincName)
535 *ppServerPrincName = NULL;
536 if (pAuthnLevel)
537 *pAuthnLevel = RPC_C_AUTHN_LEVEL_DEFAULT;
538 if (pImpLevel)
539 *pImpLevel = RPC_C_IMP_LEVEL_DEFAULT;
540 if (pAuthInfo)
541 *pAuthInfo = NULL;
542 if (pCapabilities)
543 *pCapabilities = EOAC_NONE;
544
545 return E_NOTIMPL;
546 }
547
548 static HRESULT WINAPI ProxyCliSec_SetBlanket(IClientSecurity *iface,
549 IUnknown *pProxy, DWORD AuthnSvc,
550 DWORD AuthzSvc,
551 OLECHAR *pServerPrincName,
552 DWORD AuthnLevel, DWORD ImpLevel,
553 void *pAuthInfo,
554 DWORD Capabilities)
555 {
556 FIXME("(%p, %d, %d, %s, %d, %d, %p, 0x%x): stub\n", pProxy, AuthnSvc, AuthzSvc,
557 pServerPrincName == COLE_DEFAULT_PRINCIPAL ? "<default principal>" : debugstr_w(pServerPrincName),
558 AuthnLevel, ImpLevel, pAuthInfo, Capabilities);
559 return E_NOTIMPL;
560 }
561
562 static HRESULT WINAPI ProxyCliSec_CopyProxy(IClientSecurity *iface,
563 IUnknown *pProxy, IUnknown **ppCopy)
564 {
565 FIXME("(%p, %p): stub\n", pProxy, ppCopy);
566 *ppCopy = NULL;
567 return E_NOTIMPL;
568 }
569
570 static const IClientSecurityVtbl ProxyCliSec_Vtbl =
571 {
572 ProxyCliSec_QueryInterface,
573 ProxyCliSec_AddRef,
574 ProxyCliSec_Release,
575 ProxyCliSec_QueryBlanket,
576 ProxyCliSec_SetBlanket,
577 ProxyCliSec_CopyProxy
578 };
579
580 static HRESULT ifproxy_get_public_ref(struct ifproxy * This)
581 {
582 HRESULT hr = S_OK;
583
584 if (WAIT_OBJECT_0 != WaitForSingleObject(This->parent->remoting_mutex, INFINITE))
585 {
586 ERR("Wait failed for ifproxy %p\n", This);
587 return E_UNEXPECTED;
588 }
589
590 if (This->refs == 0)
591 {
592 IRemUnknown *remunk = NULL;
593
594 TRACE("getting public ref for ifproxy %p\n", This);
595
596 hr = proxy_manager_get_remunknown(This->parent, &remunk);
597 if (hr == S_OK)
598 {
599 HRESULT hrref = S_OK;
600 REMINTERFACEREF rif;
601 rif.ipid = This->stdobjref.ipid;
602 rif.cPublicRefs = NORMALEXTREFS;
603 rif.cPrivateRefs = 0;
604 hr = IRemUnknown_RemAddRef(remunk, 1, &rif, &hrref);
605 IRemUnknown_Release(remunk);
606 if (hr == S_OK && hrref == S_OK)
607 InterlockedExchangeAdd((LONG *)&This->refs, NORMALEXTREFS);
608 else
609 ERR("IRemUnknown_RemAddRef returned with 0x%08x, hrref = 0x%08x\n", hr, hrref);
610 }
611 }
612 ReleaseMutex(This->parent->remoting_mutex);
613
614 return hr;
615 }
616
617 static HRESULT ifproxy_release_public_refs(struct ifproxy * This)
618 {
619 HRESULT hr = S_OK;
620 LONG public_refs;
621
622 if (WAIT_OBJECT_0 != WaitForSingleObject(This->parent->remoting_mutex, INFINITE))
623 {
624 ERR("Wait failed for ifproxy %p\n", This);
625 return E_UNEXPECTED;
626 }
627
628 public_refs = This->refs;
629 if (public_refs > 0)
630 {
631 IRemUnknown *remunk = NULL;
632
633 TRACE("releasing %d refs\n", public_refs);
634
635 hr = proxy_manager_get_remunknown(This->parent, &remunk);
636 if (hr == S_OK)
637 {
638 REMINTERFACEREF rif;
639 rif.ipid = This->stdobjref.ipid;
640 rif.cPublicRefs = public_refs;
641 rif.cPrivateRefs = 0;
642 hr = IRemUnknown_RemRelease(remunk, 1, &rif);
643 IRemUnknown_Release(remunk);
644 if (hr == S_OK)
645 InterlockedExchangeAdd((LONG *)&This->refs, -public_refs);
646 else if (hr == RPC_E_DISCONNECTED)
647 WARN("couldn't release references because object was "
648 "disconnected: oxid = %s, oid = %s\n",
649 wine_dbgstr_longlong(This->parent->oxid),
650 wine_dbgstr_longlong(This->parent->oid));
651 else
652 ERR("IRemUnknown_RemRelease failed with error 0x%08x\n", hr);
653 }
654 }
655 ReleaseMutex(This->parent->remoting_mutex);
656
657 return hr;
658 }
659
660 /* should be called inside This->parent->cs critical section */
661 static void ifproxy_disconnect(struct ifproxy * This)
662 {
663 ifproxy_release_public_refs(This);
664 if (This->proxy) IRpcProxyBuffer_Disconnect(This->proxy);
665
666 IRpcChannelBuffer_Release(This->chan);
667 This->chan = NULL;
668 }
669
670 /* should be called in This->parent->cs critical section if it is an entry in parent's list */
671 static void ifproxy_destroy(struct ifproxy * This)
672 {
673 TRACE("%p\n", This);
674
675 /* release public references to this object so that the stub can know
676 * when to destroy itself */
677 ifproxy_release_public_refs(This);
678
679 list_remove(&This->entry);
680
681 if (This->chan)
682 {
683 IRpcChannelBuffer_Release(This->chan);
684 This->chan = NULL;
685 }
686
687 if (This->proxy) IRpcProxyBuffer_Release(This->proxy);
688
689 HeapFree(GetProcessHeap(), 0, This);
690 }
691
692 static HRESULT proxy_manager_construct(
693 APARTMENT * apt, ULONG sorflags, OXID oxid, OID oid,
694 const OXID_INFO *oxid_info, struct proxy_manager ** proxy_manager)
695 {
696 struct proxy_manager * This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
697 if (!This) return E_OUTOFMEMORY;
698
699 This->remoting_mutex = CreateMutexW(NULL, FALSE, NULL);
700 if (!This->remoting_mutex)
701 {
702 HeapFree(GetProcessHeap(), 0, This);
703 return HRESULT_FROM_WIN32(GetLastError());
704 }
705
706 if (oxid_info)
707 {
708 This->oxid_info.dwPid = oxid_info->dwPid;
709 This->oxid_info.dwTid = oxid_info->dwTid;
710 This->oxid_info.ipidRemUnknown = oxid_info->ipidRemUnknown;
711 This->oxid_info.dwAuthnHint = oxid_info->dwAuthnHint;
712 This->oxid_info.psa = NULL /* FIXME: copy from oxid_info */;
713 }
714 else
715 {
716 HRESULT hr = RPC_ResolveOxid(oxid, &This->oxid_info);
717 if (FAILED(hr))
718 {
719 CloseHandle(This->remoting_mutex);
720 HeapFree(GetProcessHeap(), 0, This);
721 return hr;
722 }
723 }
724
725 This->lpVtbl = &ClientIdentity_Vtbl;
726 This->lpVtblMarshal = &ProxyMarshal_Vtbl;
727 This->lpVtblCliSec = &ProxyCliSec_Vtbl;
728
729 list_init(&This->entry);
730 list_init(&This->interfaces);
731
732 InitializeCriticalSection(&This->cs);
733 DEBUG_SET_CRITSEC_NAME(&This->cs, "proxy_manager");
734
735 /* the apartment the object was unmarshaled into */
736 This->parent = apt;
737
738 /* the source apartment and id of the object */
739 This->oxid = oxid;
740 This->oid = oid;
741
742 This->refs = 1;
743
744 /* the DCOM draft specification states that the SORF_NOPING flag is
745 * proxy manager specific, not ifproxy specific, so this implies that we
746 * should store the STDOBJREF flags here in the proxy manager. */
747 This->sorflags = sorflags;
748
749 /* we create the IRemUnknown proxy on demand */
750 This->remunk = NULL;
751
752 /* initialise these values to the weakest values and they will be
753 * overwritten in proxy_manager_set_context */
754 This->dest_context = MSHCTX_INPROC;
755 This->dest_context_data = NULL;
756
757 EnterCriticalSection(&apt->cs);
758 /* FIXME: we are dependent on the ordering in here to make sure a proxy's
759 * IRemUnknown proxy doesn't get destroyed before the regual proxy does
760 * because we need the IRemUnknown proxy during the destruction of the
761 * regular proxy. Ideally, we should maintain a separate list for the
762 * IRemUnknown proxies that need late destruction */
763 list_add_tail(&apt->proxies, &This->entry);
764 LeaveCriticalSection(&apt->cs);
765
766 TRACE("%p created for OXID %s, OID %s\n", This,
767 wine_dbgstr_longlong(oxid), wine_dbgstr_longlong(oid));
768
769 *proxy_manager = This;
770 return S_OK;
771 }
772
773 static inline void proxy_manager_set_context(struct proxy_manager *This, MSHCTX dest_context, void *dest_context_data)
774 {
775 MSHCTX old_dest_context = This->dest_context;
776 MSHCTX new_dest_context;
777
778 do
779 {
780 new_dest_context = old_dest_context;
781 /* "stronger" values overwrite "weaker" values. stronger values are
782 * ones that disable more optimisations */
783 switch (old_dest_context)
784 {
785 case MSHCTX_INPROC:
786 new_dest_context = dest_context;
787 break;
788 case MSHCTX_CROSSCTX:
789 switch (dest_context)
790 {
791 case MSHCTX_INPROC:
792 break;
793 default:
794 new_dest_context = dest_context;
795 }
796 break;
797 case MSHCTX_LOCAL:
798 switch (dest_context)
799 {
800 case MSHCTX_INPROC:
801 case MSHCTX_CROSSCTX:
802 break;
803 default:
804 new_dest_context = dest_context;
805 }
806 break;
807 case MSHCTX_NOSHAREDMEM:
808 switch (dest_context)
809 {
810 case MSHCTX_DIFFERENTMACHINE:
811 new_dest_context = dest_context;
812 break;
813 default:
814 break;
815 }
816 break;
817 default:
818 break;
819 }
820
821 if (old_dest_context == new_dest_context) break;
822
823 old_dest_context = InterlockedCompareExchange((PLONG)&This->dest_context, new_dest_context, old_dest_context);
824 } while (new_dest_context != old_dest_context);
825
826 if (dest_context_data)
827 InterlockedExchangePointer(&This->dest_context_data, dest_context_data);
828 }
829
830 static HRESULT proxy_manager_query_local_interface(struct proxy_manager * This, REFIID riid, void ** ppv)
831 {
832 HRESULT hr;
833 struct ifproxy * ifproxy;
834
835 TRACE("%s\n", debugstr_guid(riid));
836
837 if (IsEqualIID(riid, &IID_IUnknown) ||
838 IsEqualIID(riid, &IID_IMultiQI))
839 {
840 *ppv = &This->lpVtbl;
841 IUnknown_AddRef((IUnknown *)*ppv);
842 return S_OK;
843 }
844 if (IsEqualIID(riid, &IID_IMarshal))
845 {
846 *ppv = &This->lpVtblMarshal;
847 IUnknown_AddRef((IUnknown *)*ppv);
848 return S_OK;
849 }
850 if (IsEqualIID(riid, &IID_IClientSecurity))
851 {
852 *ppv = &This->lpVtblCliSec;
853 IUnknown_AddRef((IUnknown *)*ppv);
854 return S_OK;
855 }
856
857 hr = proxy_manager_find_ifproxy(This, riid, &ifproxy);
858 if (hr == S_OK)
859 {
860 *ppv = ifproxy->iface;
861 IUnknown_AddRef((IUnknown *)*ppv);
862 return S_OK;
863 }
864
865 *ppv = NULL;
866 return E_NOINTERFACE;
867 }
868
869 static HRESULT proxy_manager_create_ifproxy(
870 struct proxy_manager * This, const STDOBJREF *stdobjref, REFIID riid,
871 IRpcChannelBuffer * channel, struct ifproxy ** iif_out)
872 {
873 HRESULT hr;
874 IPSFactoryBuffer * psfb;
875 struct ifproxy * ifproxy = HeapAlloc(GetProcessHeap(), 0, sizeof(*ifproxy));
876 if (!ifproxy) return E_OUTOFMEMORY;
877
878 list_init(&ifproxy->entry);
879
880 ifproxy->parent = This;
881 ifproxy->stdobjref = *stdobjref;
882 ifproxy->iid = *riid;
883 ifproxy->refs = 0;
884 ifproxy->proxy = NULL;
885
886 assert(channel);
887 ifproxy->chan = channel; /* FIXME: we should take the binding strings and construct the channel in this function */
888
889 /* the IUnknown interface is special because it does not have a
890 * proxy associated with the ifproxy as we handle IUnknown ourselves */
891 if (IsEqualIID(riid, &IID_IUnknown))
892 {
893 ifproxy->iface = &This->lpVtbl;
894 IMultiQI_AddRef((IMultiQI *)&This->lpVtbl);
895 hr = S_OK;
896 }
897 else
898 {
899 hr = get_facbuf_for_iid(riid, &psfb);
900 if (hr == S_OK)
901 {
902 /* important note: the outer unknown is set to the proxy manager.
903 * This ensures the COM identity rules are not violated, by having a
904 * one-to-one mapping of objects on the proxy side to objects on the
905 * stub side, no matter which interface you view the object through */
906 hr = IPSFactoryBuffer_CreateProxy(psfb, (IUnknown *)&This->lpVtbl, riid,
907 &ifproxy->proxy, &ifproxy->iface);
908 IPSFactoryBuffer_Release(psfb);
909 if (hr != S_OK)
910 ERR("Could not create proxy for interface %s, error 0x%08x\n",
911 debugstr_guid(riid), hr);
912 }
913 else
914 ERR("Could not get IPSFactoryBuffer for interface %s, error 0x%08x\n",
915 debugstr_guid(riid), hr);
916
917 if (hr == S_OK)
918 hr = IRpcProxyBuffer_Connect(ifproxy->proxy, ifproxy->chan);
919 }
920
921 if (hr == S_OK)
922 {
923 EnterCriticalSection(&This->cs);
924 list_add_tail(&This->interfaces, &ifproxy->entry);
925 LeaveCriticalSection(&This->cs);
926
927 *iif_out = ifproxy;
928 TRACE("ifproxy %p created for IPID %s, interface %s with %u public refs\n",
929 ifproxy, debugstr_guid(&stdobjref->ipid), debugstr_guid(riid), stdobjref->cPublicRefs);
930 }
931 else
932 ifproxy_destroy(ifproxy);
933
934 return hr;
935 }
936
937 static HRESULT proxy_manager_find_ifproxy(struct proxy_manager * This, REFIID riid, struct ifproxy ** ifproxy_found)
938 {
939 HRESULT hr = E_NOINTERFACE; /* assume not found */
940 struct list * cursor;
941
942 EnterCriticalSection(&This->cs);
943 LIST_FOR_EACH(cursor, &This->interfaces)
944 {
945 struct ifproxy * ifproxy = LIST_ENTRY(cursor, struct ifproxy, entry);
946 if (IsEqualIID(riid, &ifproxy->iid))
947 {
948 *ifproxy_found = ifproxy;
949 hr = S_OK;
950 break;
951 }
952 }
953 LeaveCriticalSection(&This->cs);
954
955 return hr;
956 }
957
958 static void proxy_manager_disconnect(struct proxy_manager * This)
959 {
960 struct list * cursor;
961
962 TRACE("oxid = %s, oid = %s\n", wine_dbgstr_longlong(This->oxid),
963 wine_dbgstr_longlong(This->oid));
964
965 EnterCriticalSection(&This->cs);
966
967 /* SORFP_NOLIFTIMEMGMT proxies (for IRemUnknown) shouldn't be
968 * disconnected - it won't do anything anyway, except cause
969 * problems for other objects that depend on this proxy always
970 * working */
971 if (!(This->sorflags & SORFP_NOLIFETIMEMGMT))
972 {
973 LIST_FOR_EACH(cursor, &This->interfaces)
974 {
975 struct ifproxy * ifproxy = LIST_ENTRY(cursor, struct ifproxy, entry);
976 ifproxy_disconnect(ifproxy);
977 }
978 }
979
980 /* apartment is being destroyed so don't keep a pointer around to it */
981 This->parent = NULL;
982
983 LeaveCriticalSection(&This->cs);
984 }
985
986 static HRESULT proxy_manager_get_remunknown(struct proxy_manager * This, IRemUnknown **remunk)
987 {
988 HRESULT hr = S_OK;
989 struct apartment *apt;
990 BOOL called_in_original_apt;
991
992 /* we don't want to try and unmarshal or use IRemUnknown if we don't want
993 * lifetime management */
994 if (This->sorflags & SORFP_NOLIFETIMEMGMT)
995 return S_FALSE;
996
997 apt = COM_CurrentApt();
998 if (!apt)
999 return CO_E_NOTINITIALIZED;
1000
1001 called_in_original_apt = This->parent && (This->parent->oxid == apt->oxid);
1002
1003 EnterCriticalSection(&This->cs);
1004 /* only return the cached object if called from the original apartment.
1005 * in future, we might want to make the IRemUnknown proxy callable from any
1006 * apartment to avoid these checks */
1007 if (This->remunk && called_in_original_apt)
1008 {
1009 /* already created - return existing object */
1010 *remunk = This->remunk;
1011 IRemUnknown_AddRef(*remunk);
1012 }
1013 else if (!This->parent)
1014 /* disconnected - we can't create IRemUnknown */
1015 hr = S_FALSE;
1016 else
1017 {
1018 STDOBJREF stdobjref;
1019 /* Don't want IRemUnknown lifetime management as this is IRemUnknown!
1020 * We also don't care about whether or not the stub is still alive */
1021 stdobjref.flags = SORFP_NOLIFETIMEMGMT | SORF_NOPING;
1022 stdobjref.cPublicRefs = 1;
1023 /* oxid of destination object */
1024 stdobjref.oxid = This->oxid;
1025 /* FIXME: what should be used for the oid? The DCOM draft doesn't say */
1026 stdobjref.oid = (OID)-1;
1027 stdobjref.ipid = This->oxid_info.ipidRemUnknown;
1028
1029 /* do the unmarshal */
1030 hr = unmarshal_object(&stdobjref, COM_CurrentApt(), This->dest_context,
1031 This->dest_context_data, &IID_IRemUnknown,
1032 &This->oxid_info, (void**)remunk);
1033 if (hr == S_OK && called_in_original_apt)
1034 {
1035 This->remunk = *remunk;
1036 IRemUnknown_AddRef(This->remunk);
1037 }
1038 }
1039 LeaveCriticalSection(&This->cs);
1040
1041 TRACE("got IRemUnknown* pointer %p, hr = 0x%08x\n", *remunk, hr);
1042
1043 return hr;
1044 }
1045
1046 /* destroys a proxy manager, freeing the memory it used.
1047 * Note: this function should not be called from a list iteration in the
1048 * apartment, due to the fact that it removes itself from the apartment and
1049 * it could add a proxy to IRemUnknown into the apartment. */
1050 static void proxy_manager_destroy(struct proxy_manager * This)
1051 {
1052 struct list * cursor;
1053
1054 TRACE("oxid = %s, oid = %s\n", wine_dbgstr_longlong(This->oxid),
1055 wine_dbgstr_longlong(This->oid));
1056
1057 if (This->parent)
1058 {
1059 EnterCriticalSection(&This->parent->cs);
1060
1061 /* remove ourself from the list of proxy objects in the apartment */
1062 LIST_FOR_EACH(cursor, &This->parent->proxies)
1063 {
1064 if (cursor == &This->entry)
1065 {
1066 list_remove(&This->entry);
1067 break;
1068 }
1069 }
1070
1071 LeaveCriticalSection(&This->parent->cs);
1072 }
1073
1074 /* destroy all of the interface proxies */
1075 while ((cursor = list_head(&This->interfaces)))
1076 {
1077 struct ifproxy * ifproxy = LIST_ENTRY(cursor, struct ifproxy, entry);
1078 ifproxy_destroy(ifproxy);
1079 }
1080
1081 if (This->remunk) IRemUnknown_Release(This->remunk);
1082 CoTaskMemFree(This->oxid_info.psa);
1083
1084 DEBUG_CLEAR_CRITSEC_NAME(&This->cs);
1085 DeleteCriticalSection(&This->cs);
1086
1087 CloseHandle(This->remoting_mutex);
1088
1089 HeapFree(GetProcessHeap(), 0, This);
1090 }
1091
1092 /* finds the proxy manager corresponding to a given OXID and OID that has
1093 * been unmarshaled in the specified apartment. The caller must release the
1094 * reference to the proxy_manager when the object is no longer used. */
1095 static BOOL find_proxy_manager(APARTMENT * apt, OXID oxid, OID oid, struct proxy_manager ** proxy_found)
1096 {
1097 BOOL found = FALSE;
1098 struct list * cursor;
1099
1100 EnterCriticalSection(&apt->cs);
1101 LIST_FOR_EACH(cursor, &apt->proxies)
1102 {
1103 struct proxy_manager * proxy = LIST_ENTRY(cursor, struct proxy_manager, entry);
1104 if ((oxid == proxy->oxid) && (oid == proxy->oid))
1105 {
1106 /* be careful of a race with ClientIdentity_Release, which would
1107 * cause us to return a proxy which is in the process of being
1108 * destroyed */
1109 if (ClientIdentity_AddRef((IMultiQI *)&proxy->lpVtbl) != 0)
1110 {
1111 *proxy_found = proxy;
1112 found = TRUE;
1113 break;
1114 }
1115 }
1116 }
1117 LeaveCriticalSection(&apt->cs);
1118 return found;
1119 }
1120
1121 HRESULT apartment_disconnectproxies(struct apartment *apt)
1122 {
1123 struct list * cursor;
1124
1125 LIST_FOR_EACH(cursor, &apt->proxies)
1126 {
1127 struct proxy_manager * proxy = LIST_ENTRY(cursor, struct proxy_manager, entry);
1128 proxy_manager_disconnect(proxy);
1129 }
1130
1131 return S_OK;
1132 }
1133
1134 /********************** StdMarshal implementation ****************************/
1135 typedef struct _StdMarshalImpl
1136 {
1137 const IMarshalVtbl *lpvtbl;
1138 LONG ref;
1139
1140 IID iid;
1141 DWORD dwDestContext;
1142 LPVOID pvDestContext;
1143 DWORD mshlflags;
1144 } StdMarshalImpl;
1145
1146 static HRESULT WINAPI
1147 StdMarshalImpl_QueryInterface(LPMARSHAL iface, REFIID riid, LPVOID *ppv)
1148 {
1149 *ppv = NULL;
1150 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IMarshal, riid))
1151 {
1152 *ppv = iface;
1153 IUnknown_AddRef(iface);
1154 return S_OK;
1155 }
1156 FIXME("No interface for %s.\n", debugstr_guid(riid));
1157 return E_NOINTERFACE;
1158 }
1159
1160 static ULONG WINAPI
1161 StdMarshalImpl_AddRef(LPMARSHAL iface)
1162 {
1163 StdMarshalImpl *This = (StdMarshalImpl *)iface;
1164 return InterlockedIncrement(&This->ref);
1165 }
1166
1167 static ULONG WINAPI
1168 StdMarshalImpl_Release(LPMARSHAL iface)
1169 {
1170 StdMarshalImpl *This = (StdMarshalImpl *)iface;
1171 ULONG ref = InterlockedDecrement(&This->ref);
1172
1173 if (!ref) HeapFree(GetProcessHeap(),0,This);
1174 return ref;
1175 }
1176
1177 static HRESULT WINAPI
1178 StdMarshalImpl_GetUnmarshalClass(
1179 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
1180 void* pvDestContext, DWORD mshlflags, CLSID* pCid)
1181 {
1182 *pCid = CLSID_DfMarshal;
1183 return S_OK;
1184 }
1185
1186 static HRESULT WINAPI
1187 StdMarshalImpl_GetMarshalSizeMax(
1188 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
1189 void* pvDestContext, DWORD mshlflags, DWORD* pSize)
1190 {
1191 *pSize = sizeof(STDOBJREF);
1192 return S_OK;
1193 }
1194
1195 static HRESULT WINAPI
1196 StdMarshalImpl_MarshalInterface(
1197 LPMARSHAL iface, IStream *pStm,REFIID riid, void* pv, DWORD dwDestContext,
1198 void* pvDestContext, DWORD mshlflags)
1199 {
1200 STDOBJREF stdobjref;
1201 ULONG res;
1202 HRESULT hres;
1203 APARTMENT *apt = COM_CurrentApt();
1204
1205 TRACE("(...,%s,...)\n", debugstr_guid(riid));
1206
1207 if (!apt)
1208 {
1209 ERR("Apartment not initialized\n");
1210 return CO_E_NOTINITIALIZED;
1211 }
1212
1213 /* make sure this apartment can be reached from other threads / processes */
1214 RPC_StartRemoting(apt);
1215
1216 hres = marshal_object(apt, &stdobjref, riid, pv, mshlflags);
1217 if (hres != S_OK)
1218 {
1219 ERR("Failed to create ifstub, hres=0x%x\n", hres);
1220 return hres;
1221 }
1222
1223 return IStream_Write(pStm, &stdobjref, sizeof(stdobjref), &res);
1224 }
1225
1226 /* helper for StdMarshalImpl_UnmarshalInterface - does the unmarshaling with
1227 * no questions asked about the rules surrounding same-apartment unmarshals
1228 * and table marshaling */
1229 static HRESULT unmarshal_object(const STDOBJREF *stdobjref, APARTMENT *apt,
1230 MSHCTX dest_context, void *dest_context_data,
1231 REFIID riid, const OXID_INFO *oxid_info,
1232 void **object)
1233 {
1234 struct proxy_manager *proxy_manager = NULL;
1235 HRESULT hr = S_OK;
1236
1237 assert(apt);
1238
1239 TRACE("stdobjref: flags = %04x cPublicRefs = %d oxid = %s oid = %s ipid = %s\n",
1240 stdobjref->flags, stdobjref->cPublicRefs,
1241 wine_dbgstr_longlong(stdobjref->oxid),
1242 wine_dbgstr_longlong(stdobjref->oid),
1243 debugstr_guid(&stdobjref->ipid));
1244
1245 /* create a new proxy manager if one doesn't already exist for the
1246 * object */
1247 if (!find_proxy_manager(apt, stdobjref->oxid, stdobjref->oid, &proxy_manager))
1248 {
1249 hr = proxy_manager_construct(apt, stdobjref->flags,
1250 stdobjref->oxid, stdobjref->oid, oxid_info,
1251 &proxy_manager);
1252 }
1253 else
1254 TRACE("proxy manager already created, using\n");
1255
1256 if (hr == S_OK)
1257 {
1258 struct ifproxy * ifproxy;
1259
1260 proxy_manager_set_context(proxy_manager, dest_context, dest_context_data);
1261
1262 hr = proxy_manager_find_ifproxy(proxy_manager, riid, &ifproxy);
1263 if (hr == E_NOINTERFACE)
1264 {
1265 IRpcChannelBuffer *chanbuf;
1266 hr = RPC_CreateClientChannel(&stdobjref->oxid, &stdobjref->ipid,
1267 &proxy_manager->oxid_info,
1268 proxy_manager->dest_context,
1269 proxy_manager->dest_context_data,
1270 &chanbuf);
1271 if (hr == S_OK)
1272 hr = proxy_manager_create_ifproxy(proxy_manager, stdobjref,
1273 riid, chanbuf, &ifproxy);
1274 }
1275 else
1276 IUnknown_AddRef((IUnknown *)ifproxy->iface);
1277
1278 if (hr == S_OK)
1279 {
1280 InterlockedExchangeAdd((LONG *)&ifproxy->refs, stdobjref->cPublicRefs);
1281 /* get at least one external reference to the object to keep it alive */
1282 hr = ifproxy_get_public_ref(ifproxy);
1283 if (FAILED(hr))
1284 ifproxy_destroy(ifproxy);
1285 }
1286
1287 if (hr == S_OK)
1288 *object = ifproxy->iface;
1289 }
1290
1291 /* release our reference to the proxy manager - the client/apartment
1292 * will hold on to the remaining reference for us */
1293 if (proxy_manager) ClientIdentity_Release((IMultiQI*)&proxy_manager->lpVtbl);
1294
1295 return hr;
1296 }
1297
1298 static HRESULT WINAPI
1299 StdMarshalImpl_UnmarshalInterface(LPMARSHAL iface, IStream *pStm, REFIID riid, void **ppv)
1300 {
1301 StdMarshalImpl *This = (StdMarshalImpl *)iface;
1302 struct stub_manager *stubmgr = NULL;
1303 STDOBJREF stdobjref;
1304 ULONG res;
1305 HRESULT hres;
1306 APARTMENT *apt = COM_CurrentApt();
1307 APARTMENT *stub_apt;
1308 OXID oxid;
1309
1310 TRACE("(...,%s,....)\n", debugstr_guid(riid));
1311
1312 /* we need an apartment to unmarshal into */
1313 if (!apt)
1314 {
1315 ERR("Apartment not initialized\n");
1316 return CO_E_NOTINITIALIZED;
1317 }
1318
1319 /* read STDOBJREF from wire */
1320 hres = IStream_Read(pStm, &stdobjref, sizeof(stdobjref), &res);
1321 if (hres != S_OK) return STG_E_READFAULT;
1322
1323 hres = apartment_getoxid(apt, &oxid);
1324 if (hres != S_OK) return hres;
1325
1326 /* check if we're marshalling back to ourselves */
1327 if ((oxid == stdobjref.oxid) && (stubmgr = get_stub_manager(apt, stdobjref.oid)))
1328 {
1329 TRACE("Unmarshalling object marshalled in same apartment for iid %s, "
1330 "returning original object %p\n", debugstr_guid(riid), stubmgr->object);
1331
1332 hres = IUnknown_QueryInterface(stubmgr->object, riid, ppv);
1333
1334 /* unref the ifstub. FIXME: only do this on success? */
1335 if (!stub_manager_is_table_marshaled(stubmgr, &stdobjref.ipid))
1336 stub_manager_ext_release(stubmgr, stdobjref.cPublicRefs, stdobjref.flags & SORFP_TABLEWEAK, FALSE);
1337
1338 stub_manager_int_release(stubmgr);
1339 return hres;
1340 }
1341
1342 /* notify stub manager about unmarshal if process-local object.
1343 * note: if the oxid is not found then we and native will quite happily
1344 * ignore table marshaling and normal marshaling rules regarding number of
1345 * unmarshals, etc, but if you abuse these rules then your proxy could end
1346 * up returning RPC_E_DISCONNECTED. */
1347 if ((stub_apt = apartment_findfromoxid(stdobjref.oxid, TRUE)))
1348 {
1349 if ((stubmgr = get_stub_manager(stub_apt, stdobjref.oid)))
1350 {
1351 if (!stub_manager_notify_unmarshal(stubmgr, &stdobjref.ipid))
1352 hres = CO_E_OBJNOTCONNECTED;
1353 }
1354 else
1355 {
1356 WARN("Couldn't find object for OXID %s, OID %s, assuming disconnected\n",
1357 wine_dbgstr_longlong(stdobjref.oxid),
1358 wine_dbgstr_longlong(stdobjref.oid));
1359 hres = CO_E_OBJNOTCONNECTED;
1360 }
1361 }
1362 else
1363 TRACE("Treating unmarshal from OXID %s as inter-process\n",
1364 wine_dbgstr_longlong(stdobjref.oxid));
1365
1366 if (hres == S_OK)
1367 hres = unmarshal_object(&stdobjref, apt, This->dwDestContext,
1368 This->pvDestContext, riid,
1369 stubmgr ? &stubmgr->oxid_info : NULL, ppv);
1370
1371 if (stubmgr) stub_manager_int_release(stubmgr);
1372 if (stub_apt) apartment_release(stub_apt);
1373
1374 if (hres != S_OK) WARN("Failed with error 0x%08x\n", hres);
1375 else TRACE("Successfully created proxy %p\n", *ppv);
1376
1377 return hres;
1378 }
1379
1380 static HRESULT WINAPI
1381 StdMarshalImpl_ReleaseMarshalData(LPMARSHAL iface, IStream *pStm)
1382 {
1383 STDOBJREF stdobjref;
1384 ULONG res;
1385 HRESULT hres;
1386 struct stub_manager *stubmgr;
1387 APARTMENT *apt;
1388
1389 TRACE("iface=%p, pStm=%p\n", iface, pStm);
1390
1391 hres = IStream_Read(pStm, &stdobjref, sizeof(stdobjref), &res);
1392 if (hres != S_OK) return STG_E_READFAULT;
1393
1394 TRACE("oxid = %s, oid = %s, ipid = %s\n",
1395 wine_dbgstr_longlong(stdobjref.oxid),
1396 wine_dbgstr_longlong(stdobjref.oid),
1397 wine_dbgstr_guid(&stdobjref.ipid));
1398
1399 if (!(apt = apartment_findfromoxid(stdobjref.oxid, TRUE)))
1400 {
1401 WARN("Could not map OXID %s to apartment object\n",
1402 wine_dbgstr_longlong(stdobjref.oxid));
1403 return RPC_E_INVALID_OBJREF;
1404 }
1405
1406 if (!(stubmgr = get_stub_manager(apt, stdobjref.oid)))
1407 {
1408 ERR("could not map object ID to stub manager, oxid=%s, oid=%s\n",
1409 wine_dbgstr_longlong(stdobjref.oxid), wine_dbgstr_longlong(stdobjref.oid));
1410 return RPC_E_INVALID_OBJREF;
1411 }
1412
1413 stub_manager_release_marshal_data(stubmgr, stdobjref.cPublicRefs, &stdobjref.ipid, stdobjref.flags & SORFP_TABLEWEAK);
1414
1415 stub_manager_int_release(stubmgr);
1416 apartment_release(apt);
1417
1418 return S_OK;
1419 }
1420
1421 static HRESULT WINAPI
1422 StdMarshalImpl_DisconnectObject(LPMARSHAL iface, DWORD dwReserved)
1423 {
1424 FIXME("(), stub!\n");
1425 return S_OK;
1426 }
1427
1428 static const IMarshalVtbl VT_StdMarshal =
1429 {
1430 StdMarshalImpl_QueryInterface,
1431 StdMarshalImpl_AddRef,
1432 StdMarshalImpl_Release,
1433 StdMarshalImpl_GetUnmarshalClass,
1434 StdMarshalImpl_GetMarshalSizeMax,
1435 StdMarshalImpl_MarshalInterface,
1436 StdMarshalImpl_UnmarshalInterface,
1437 StdMarshalImpl_ReleaseMarshalData,
1438 StdMarshalImpl_DisconnectObject
1439 };
1440
1441 static HRESULT StdMarshalImpl_Construct(REFIID riid, void** ppvObject)
1442 {
1443 StdMarshalImpl * pStdMarshal =
1444 HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(StdMarshalImpl));
1445 if (!pStdMarshal)
1446 return E_OUTOFMEMORY;
1447 pStdMarshal->lpvtbl = &VT_StdMarshal;
1448 pStdMarshal->ref = 0;
1449 return IMarshal_QueryInterface((IMarshal*)pStdMarshal, riid, ppvObject);
1450 }
1451
1452 /***********************************************************************
1453 * CoGetStandardMarshal [OLE32.@]
1454 *
1455 * Gets or creates a standard marshal object.
1456 *
1457 * PARAMS
1458 * riid [I] Interface identifier of the pUnk object.
1459 * pUnk [I] Optional. Object to get the marshal object for.
1460 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1461 * pvDestContext [I] Reserved. Must be NULL.
1462 * mshlflags [I] Flags affecting the marshaling process.
1463 * ppMarshal [O] Address where marshal object will be stored.
1464 *
1465 * RETURNS
1466 * Success: S_OK.
1467 * Failure: HRESULT code.
1468 *
1469 * NOTES
1470 *
1471 * The function retrieves the IMarshal object associated with an object if
1472 * that object is currently an active stub, otherwise a new marshal object is
1473 * created.
1474 */
1475 HRESULT WINAPI CoGetStandardMarshal(REFIID riid, IUnknown *pUnk,
1476 DWORD dwDestContext, LPVOID pvDestContext,
1477 DWORD mshlflags, LPMARSHAL *ppMarshal)
1478 {
1479 StdMarshalImpl *dm;
1480
1481 if (pUnk == NULL)
1482 {
1483 FIXME("(%s,NULL,%x,%p,%x,%p), unimplemented yet.\n",
1484 debugstr_guid(riid),dwDestContext,pvDestContext,mshlflags,ppMarshal);
1485 return E_NOTIMPL;
1486 }
1487 TRACE("(%s,%p,%x,%p,%x,%p)\n",
1488 debugstr_guid(riid),pUnk,dwDestContext,pvDestContext,mshlflags,ppMarshal);
1489 *ppMarshal = HeapAlloc(GetProcessHeap(),0,sizeof(StdMarshalImpl));
1490 dm = (StdMarshalImpl*) *ppMarshal;
1491 if (!dm) return E_FAIL;
1492 dm->lpvtbl = &VT_StdMarshal;
1493 dm->ref = 1;
1494
1495 dm->iid = *riid;
1496 dm->dwDestContext = dwDestContext;
1497 dm->pvDestContext = pvDestContext;
1498 dm->mshlflags = mshlflags;
1499 return S_OK;
1500 }
1501
1502 /***********************************************************************
1503 * get_marshaler [internal]
1504 *
1505 * Retrieves an IMarshal interface for an object.
1506 */
1507 static HRESULT get_marshaler(REFIID riid, IUnknown *pUnk, DWORD dwDestContext,
1508 void *pvDestContext, DWORD mshlFlags,
1509 LPMARSHAL *pMarshal)
1510 {
1511 HRESULT hr;
1512
1513 if (!pUnk)
1514 return E_POINTER;
1515 hr = IUnknown_QueryInterface(pUnk, &IID_IMarshal, (LPVOID*)pMarshal);
1516 if (hr != S_OK)
1517 hr = CoGetStandardMarshal(riid, pUnk, dwDestContext, pvDestContext,
1518 mshlFlags, pMarshal);
1519 return hr;
1520 }
1521
1522 /***********************************************************************
1523 * get_unmarshaler_from_stream [internal]
1524 *
1525 * Creates an IMarshal* object according to the data marshaled to the stream.
1526 * The function leaves the stream pointer at the start of the data written
1527 * to the stream by the IMarshal* object.
1528 */
1529 static HRESULT get_unmarshaler_from_stream(IStream *stream, IMarshal **marshal, IID *iid)
1530 {
1531 HRESULT hr;
1532 ULONG res;
1533 OBJREF objref;
1534
1535 /* read common OBJREF header */
1536 hr = IStream_Read(stream, &objref, FIELD_OFFSET(OBJREF, u_objref), &res);
1537 if (hr != S_OK || (res != FIELD_OFFSET(OBJREF, u_objref)))
1538 {
1539 ERR("Failed to read common OBJREF header, 0x%08x\n", hr);
1540 return STG_E_READFAULT;
1541 }
1542
1543 /* sanity check on header */
1544 if (objref.signature != OBJREF_SIGNATURE)
1545 {
1546 ERR("Bad OBJREF signature 0x%08x\n", objref.signature);
1547 return RPC_E_INVALID_OBJREF;
1548 }
1549
1550 if (iid) *iid = objref.iid;
1551
1552 /* FIXME: handler marshaling */
1553 if (objref.flags & OBJREF_STANDARD)
1554 {
1555 TRACE("Using standard unmarshaling\n");
1556 hr = StdMarshalImpl_Construct(&IID_IMarshal, (LPVOID*)marshal);
1557 }
1558 else if (objref.flags & OBJREF_CUSTOM)
1559 {
1560 ULONG custom_header_size = FIELD_OFFSET(OBJREF, u_objref.u_custom.pData) -
1561 FIELD_OFFSET(OBJREF, u_objref.u_custom);
1562 TRACE("Using custom unmarshaling\n");
1563 /* read constant sized OR_CUSTOM data from stream */
1564 hr = IStream_Read(stream, &objref.u_objref.u_custom,
1565 custom_header_size, &res);
1566 if (hr != S_OK || (res != custom_header_size))
1567 {
1568 ERR("Failed to read OR_CUSTOM header, 0x%08x\n", hr);
1569 return STG_E_READFAULT;
1570 }
1571 /* now create the marshaler specified in the stream */
1572 hr = CoCreateInstance(&objref.u_objref.u_custom.clsid, NULL,
1573 CLSCTX_INPROC_SERVER, &IID_IMarshal,
1574 (LPVOID*)marshal);
1575 }
1576 else
1577 {
1578 FIXME("Invalid or unimplemented marshaling type specified: %x\n",
1579 objref.flags);
1580 return RPC_E_INVALID_OBJREF;
1581 }
1582
1583 if (hr != S_OK)
1584 ERR("Failed to create marshal, 0x%08x\n", hr);
1585
1586 return hr;
1587 }
1588
1589 /***********************************************************************
1590 * CoGetMarshalSizeMax [OLE32.@]
1591 *
1592 * Gets the maximum amount of data that will be needed by a marshal.
1593 *
1594 * PARAMS
1595 * pulSize [O] Address where maximum marshal size will be stored.
1596 * riid [I] Identifier of the interface to marshal.
1597 * pUnk [I] Pointer to the object to marshal.
1598 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1599 * pvDestContext [I] Reserved. Must be NULL.
1600 * mshlFlags [I] Flags that affect the marshaling. See CoMarshalInterface().
1601 *
1602 * RETURNS
1603 * Success: S_OK.
1604 * Failure: HRESULT code.
1605 *
1606 * SEE ALSO
1607 * CoMarshalInterface().
1608 */
1609 HRESULT WINAPI CoGetMarshalSizeMax(ULONG *pulSize, REFIID riid, IUnknown *pUnk,
1610 DWORD dwDestContext, void *pvDestContext,
1611 DWORD mshlFlags)
1612 {
1613 HRESULT hr;
1614 LPMARSHAL pMarshal;
1615 CLSID marshaler_clsid;
1616
1617 hr = get_marshaler(riid, pUnk, dwDestContext, pvDestContext, mshlFlags, &pMarshal);
1618 if (hr != S_OK)
1619 return hr;
1620
1621 hr = IMarshal_GetUnmarshalClass(pMarshal, riid, pUnk, dwDestContext,
1622 pvDestContext, mshlFlags, &marshaler_clsid);
1623 if (hr != S_OK)
1624 {
1625 ERR("IMarshal::GetUnmarshalClass failed, 0x%08x\n", hr);
1626 IMarshal_Release(pMarshal);
1627 return hr;
1628 }
1629
1630 hr = IMarshal_GetMarshalSizeMax(pMarshal, riid, pUnk, dwDestContext,
1631 pvDestContext, mshlFlags, pulSize);
1632 if (IsEqualCLSID(&marshaler_clsid, &CLSID_DfMarshal))
1633 /* add on the size of the common header */
1634 *pulSize += FIELD_OFFSET(OBJREF, u_objref);
1635 else
1636 /* custom marshaling: add on the size of the whole OBJREF structure
1637 * like native does */
1638 *pulSize += sizeof(OBJREF);
1639
1640 IMarshal_Release(pMarshal);
1641 return hr;
1642 }
1643
1644
1645 static void dump_MSHLFLAGS(MSHLFLAGS flags)
1646 {
1647 if (flags & MSHLFLAGS_TABLESTRONG)
1648 TRACE(" MSHLFLAGS_TABLESTRONG");
1649 if (flags & MSHLFLAGS_TABLEWEAK)
1650 TRACE(" MSHLFLAGS_TABLEWEAK");
1651 if (!(flags & (MSHLFLAGS_TABLESTRONG|MSHLFLAGS_TABLEWEAK)))
1652 TRACE(" MSHLFLAGS_NORMAL");
1653 if (flags & MSHLFLAGS_NOPING)
1654 TRACE(" MSHLFLAGS_NOPING");
1655 }
1656
1657 /***********************************************************************
1658 * CoMarshalInterface [OLE32.@]
1659 *
1660 * Marshals an interface into a stream so that the object can then be
1661 * unmarshaled from another COM apartment and used remotely.
1662 *
1663 * PARAMS
1664 * pStream [I] Stream the object will be marshaled into.
1665 * riid [I] Identifier of the interface to marshal.
1666 * pUnk [I] Pointer to the object to marshal.
1667 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1668 * pvDestContext [I] Reserved. Must be NULL.
1669 * mshlFlags [I] Flags that affect the marshaling. See notes.
1670 *
1671 * RETURNS
1672 * Success: S_OK.
1673 * Failure: HRESULT code.
1674 *
1675 * NOTES
1676 *
1677 * The mshlFlags parameter can take one or more of the following flags:
1678 *| MSHLFLAGS_NORMAL - Unmarshal once, releases stub on last proxy release.
1679 *| MSHLFLAGS_TABLESTRONG - Unmarshal many, release when CoReleaseMarshalData() called.
1680 *| MSHLFLAGS_TABLEWEAK - Unmarshal many, releases stub on last proxy release.
1681 *| MSHLFLAGS_NOPING - No automatic garbage collection (and so reduces network traffic).
1682 *
1683 * If a marshaled object is not unmarshaled, then CoReleaseMarshalData() must
1684 * be called in order to release the resources used in the marshaling.
1685 *
1686 * SEE ALSO
1687 * CoUnmarshalInterface(), CoReleaseMarshalData().
1688 */
1689 HRESULT WINAPI CoMarshalInterface(IStream *pStream, REFIID riid, IUnknown *pUnk,
1690 DWORD dwDestContext, void *pvDestContext,
1691 DWORD mshlFlags)
1692 {
1693 HRESULT hr;
1694 CLSID marshaler_clsid;
1695 OBJREF objref;
1696 LPMARSHAL pMarshal;
1697
1698 TRACE("(%p, %s, %p, %x, %p,", pStream, debugstr_guid(riid), pUnk,
1699 dwDestContext, pvDestContext);
1700 dump_MSHLFLAGS(mshlFlags);
1701 TRACE(")\n");
1702
1703 if (!pUnk || !pStream)
1704 return E_INVALIDARG;
1705
1706 objref.signature = OBJREF_SIGNATURE;
1707 objref.iid = *riid;
1708
1709 /* get the marshaler for the specified interface */
1710 hr = get_marshaler(riid, pUnk, dwDestContext, pvDestContext, mshlFlags, &pMarshal);
1711 if (hr != S_OK)
1712 {
1713 ERR("Failed to get marshaller, 0x%08x\n", hr);
1714 return hr;
1715 }
1716
1717 hr = IMarshal_GetUnmarshalClass(pMarshal, riid, pUnk, dwDestContext,
1718 pvDestContext, mshlFlags, &marshaler_clsid);
1719 if (hr != S_OK)
1720 {
1721 ERR("IMarshal::GetUnmarshalClass failed, 0x%08x\n", hr);
1722 goto cleanup;
1723 }
1724
1725 /* FIXME: implement handler marshaling too */
1726 if (IsEqualCLSID(&marshaler_clsid, &CLSID_DfMarshal))
1727 {
1728 TRACE("Using standard marshaling\n");
1729 objref.flags = OBJREF_STANDARD;
1730
1731 /* write the common OBJREF header to the stream */
1732 hr = IStream_Write(pStream, &objref, FIELD_OFFSET(OBJREF, u_objref), NULL);
1733 if (hr != S_OK)
1734 {
1735 ERR("Failed to write OBJREF header to stream, 0x%08x\n", hr);
1736 goto cleanup;
1737 }
1738 }
1739 else
1740 {
1741 TRACE("Using custom marshaling\n");
1742 objref.flags = OBJREF_CUSTOM;
1743 objref.u_objref.u_custom.clsid = marshaler_clsid;
1744 objref.u_objref.u_custom.cbExtension = 0;
1745 objref.u_objref.u_custom.size = 0;
1746 hr = IMarshal_GetMarshalSizeMax(pMarshal, riid, pUnk, dwDestContext,
1747 pvDestContext, mshlFlags,
1748 &objref.u_objref.u_custom.size);
1749 if (hr != S_OK)
1750 {
1751 ERR("Failed to get max size of marshal data, error 0x%08x\n", hr);
1752 goto cleanup;
1753 }
1754 /* write constant sized common header and OR_CUSTOM data into stream */
1755 hr = IStream_Write(pStream, &objref,
1756 FIELD_OFFSET(OBJREF, u_objref.u_custom.pData), NULL);
1757 if (hr != S_OK)
1758 {
1759 ERR("Failed to write OR_CUSTOM header to stream with 0x%08x\n", hr);
1760 goto cleanup;
1761 }
1762 }
1763
1764 TRACE("Calling IMarshal::MarshalInterace\n");
1765 /* call helper object to do the actual marshaling */
1766 hr = IMarshal_MarshalInterface(pMarshal, pStream, riid, pUnk, dwDestContext,
1767 pvDestContext, mshlFlags);
1768
1769 if (hr != S_OK)
1770 {
1771 ERR("Failed to marshal the interface %s, %x\n", debugstr_guid(riid), hr);
1772 goto cleanup;
1773 }
1774
1775 cleanup:
1776 IMarshal_Release(pMarshal);
1777
1778 TRACE("completed with hr 0x%08x\n", hr);
1779
1780 return hr;
1781 }
1782
1783 /***********************************************************************
1784 * CoUnmarshalInterface [OLE32.@]
1785 *
1786 * Unmarshals an object from a stream by creating a proxy to the remote
1787 * object, if necessary.
1788 *
1789 * PARAMS
1790 *
1791 * pStream [I] Stream containing the marshaled object.
1792 * riid [I] Interface identifier of the object to create a proxy to.
1793 * ppv [O] Address where proxy will be stored.
1794 *
1795 * RETURNS
1796 *
1797 * Success: S_OK.
1798 * Failure: HRESULT code.
1799 *
1800 * SEE ALSO
1801 * CoMarshalInterface().
1802 */
1803 HRESULT WINAPI CoUnmarshalInterface(IStream *pStream, REFIID riid, LPVOID *ppv)
1804 {
1805 HRESULT hr;
1806 LPMARSHAL pMarshal;
1807 IID iid;
1808 IUnknown *object;
1809
1810 TRACE("(%p, %s, %p)\n", pStream, debugstr_guid(riid), ppv);
1811
1812 if (!pStream || !ppv)
1813 return E_INVALIDARG;
1814
1815 hr = get_unmarshaler_from_stream(pStream, &pMarshal, &iid);
1816 if (hr != S_OK)
1817 return hr;
1818
1819 /* call the helper object to do the actual unmarshaling */
1820 hr = IMarshal_UnmarshalInterface(pMarshal, pStream, &iid, (LPVOID*)&object);
1821 if (hr != S_OK)
1822 ERR("IMarshal::UnmarshalInterface failed, 0x%08x\n", hr);
1823
1824 if (hr == S_OK)
1825 {
1826 /* IID_NULL means use the interface ID of the marshaled object */
1827 if (!IsEqualIID(riid, &IID_NULL) && !IsEqualIID(riid, &iid))
1828 {
1829 TRACE("requested interface != marshalled interface, additional QI needed\n");
1830 hr = IUnknown_QueryInterface(object, riid, ppv);
1831 if (hr != S_OK)
1832 ERR("Couldn't query for interface %s, hr = 0x%08x\n",
1833 debugstr_guid(riid), hr);
1834 IUnknown_Release(object);
1835 }
1836 else
1837 {
1838 *ppv = object;
1839 }
1840 }
1841
1842 IMarshal_Release(pMarshal);
1843
1844 TRACE("completed with hr 0x%x\n", hr);
1845
1846 return hr;
1847 }
1848
1849 /***********************************************************************
1850 * CoReleaseMarshalData [OLE32.@]
1851 *
1852 * Releases resources associated with an object that has been marshaled into
1853 * a stream.
1854 *
1855 * PARAMS
1856 *
1857 * pStream [I] The stream that the object has been marshaled into.
1858 *
1859 * RETURNS
1860 * Success: S_OK.
1861 * Failure: HRESULT error code.
1862 *
1863 * NOTES
1864 *
1865 * Call this function to release resources associated with a normal or
1866 * table-weak marshal that will not be unmarshaled, and all table-strong
1867 * marshals when they are no longer needed.
1868 *
1869 * SEE ALSO
1870 * CoMarshalInterface(), CoUnmarshalInterface().
1871 */
1872 HRESULT WINAPI CoReleaseMarshalData(IStream *pStream)
1873 {
1874 HRESULT hr;
1875 LPMARSHAL pMarshal;
1876
1877 TRACE("(%p)\n", pStream);
1878
1879 hr = get_unmarshaler_from_stream(pStream, &pMarshal, NULL);
1880 if (hr != S_OK)
1881 return hr;
1882
1883 /* call the helper object to do the releasing of marshal data */
1884 hr = IMarshal_ReleaseMarshalData(pMarshal, pStream);
1885 if (hr != S_OK)
1886 ERR("IMarshal::ReleaseMarshalData failed with error 0x%08x\n", hr);
1887
1888 IMarshal_Release(pMarshal);
1889 return hr;
1890 }
1891
1892
1893 /***********************************************************************
1894 * CoMarshalInterThreadInterfaceInStream [OLE32.@]
1895 *
1896 * Marshal an interface across threads in the same process.
1897 *
1898 * PARAMS
1899 * riid [I] Identifier of the interface to be marshalled.
1900 * pUnk [I] Pointer to IUnknown-derived interface that will be marshalled.
1901 * ppStm [O] Pointer to IStream object that is created and then used to store the marshalled interface.
1902 *
1903 * RETURNS
1904 * Success: S_OK
1905 * Failure: E_OUTOFMEMORY and other COM error codes
1906 *
1907 * SEE ALSO
1908 * CoMarshalInterface(), CoUnmarshalInterface() and CoGetInterfaceAndReleaseStream()
1909 */
1910 HRESULT WINAPI CoMarshalInterThreadInterfaceInStream(
1911 REFIID riid, LPUNKNOWN pUnk, LPSTREAM * ppStm)
1912 {
1913 ULARGE_INTEGER xpos;
1914 LARGE_INTEGER seekto;
1915 HRESULT hres;
1916
1917 TRACE("(%s, %p, %p)\n",debugstr_guid(riid), pUnk, ppStm);
1918
1919 hres = CreateStreamOnHGlobal(NULL, TRUE, ppStm);
1920 if (FAILED(hres)) return hres;
1921 hres = CoMarshalInterface(*ppStm, riid, pUnk, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1922
1923 if (SUCCEEDED(hres))
1924 {
1925 memset(&seekto, 0, sizeof(seekto));
1926 IStream_Seek(*ppStm, seekto, STREAM_SEEK_SET, &xpos);
1927 }
1928 else
1929 {
1930 IStream_Release(*ppStm);
1931 *ppStm = NULL;
1932 }
1933
1934 return hres;
1935 }
1936
1937 /***********************************************************************
1938 * CoGetInterfaceAndReleaseStream [OLE32.@]
1939 *
1940 * Unmarshalls an interface from a stream and then releases the stream.
1941 *
1942 * PARAMS
1943 * pStm [I] Stream that contains the marshalled interface.
1944 * riid [I] Interface identifier of the object to unmarshall.
1945 * ppv [O] Address of pointer where the requested interface object will be stored.
1946 *
1947 * RETURNS
1948 * Success: S_OK
1949 * Failure: A COM error code
1950 *
1951 * SEE ALSO
1952 * CoMarshalInterThreadInterfaceInStream() and CoUnmarshalInterface()
1953 */
1954 HRESULT WINAPI CoGetInterfaceAndReleaseStream(LPSTREAM pStm, REFIID riid,
1955 LPVOID *ppv)
1956 {
1957 HRESULT hres;
1958
1959 TRACE("(%p, %s, %p)\n", pStm, debugstr_guid(riid), ppv);
1960
1961 if(!pStm) return E_INVALIDARG;
1962 hres = CoUnmarshalInterface(pStm, riid, ppv);
1963 IStream_Release(pStm);
1964 return hres;
1965 }
1966
1967 static HRESULT WINAPI StdMarshalCF_QueryInterface(LPCLASSFACTORY iface,
1968 REFIID riid, LPVOID *ppv)
1969 {
1970 *ppv = NULL;
1971 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory))
1972 {
1973 *ppv = iface;
1974 return S_OK;
1975 }
1976 return E_NOINTERFACE;
1977 }
1978
1979 static ULONG WINAPI StdMarshalCF_AddRef(LPCLASSFACTORY iface)
1980 {
1981 return 2; /* non-heap based object */
1982 }
1983
1984 static ULONG WINAPI StdMarshalCF_Release(LPCLASSFACTORY iface)
1985 {
1986 return 1; /* non-heap based object */
1987 }
1988
1989 static HRESULT WINAPI StdMarshalCF_CreateInstance(LPCLASSFACTORY iface,
1990 LPUNKNOWN pUnk, REFIID riid, LPVOID *ppv)
1991 {
1992 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IMarshal))
1993 return StdMarshalImpl_Construct(riid, ppv);
1994
1995 FIXME("(%s), not supported.\n",debugstr_guid(riid));
1996 return E_NOINTERFACE;
1997 }
1998
1999 static HRESULT WINAPI StdMarshalCF_LockServer(LPCLASSFACTORY iface, BOOL fLock)
2000 {
2001 FIXME("(%d), stub!\n",fLock);
2002 return S_OK;
2003 }
2004
2005 static const IClassFactoryVtbl StdMarshalCFVtbl =
2006 {
2007 StdMarshalCF_QueryInterface,
2008 StdMarshalCF_AddRef,
2009 StdMarshalCF_Release,
2010 StdMarshalCF_CreateInstance,
2011 StdMarshalCF_LockServer
2012 };
2013 static const IClassFactoryVtbl *StdMarshalCF = &StdMarshalCFVtbl;
2014
2015 HRESULT MARSHAL_GetStandardMarshalCF(LPVOID *ppv)
2016 {
2017 *ppv = &StdMarshalCF;
2018 return S_OK;
2019 }
2020
2021 /***********************************************************************
2022 * CoMarshalHresult [OLE32.@]
2023 *
2024 * Marshals an HRESULT value into a stream.
2025 *
2026 * PARAMS
2027 * pStm [I] Stream that hresult will be marshalled into.
2028 * hresult [I] HRESULT to be marshalled.
2029 *
2030 * RETURNS
2031 * Success: S_OK
2032 * Failure: A COM error code
2033 *
2034 * SEE ALSO
2035 * CoUnmarshalHresult().
2036 */
2037 HRESULT WINAPI CoMarshalHresult(LPSTREAM pStm, HRESULT hresult)
2038 {
2039 return IStream_Write(pStm, &hresult, sizeof(hresult), NULL);
2040 }
2041
2042 /***********************************************************************
2043 * CoUnmarshalHresult [OLE32.@]
2044 *
2045 * Unmarshals an HRESULT value from a stream.
2046 *
2047 * PARAMS
2048 * pStm [I] Stream that hresult will be unmarshalled from.
2049 * phresult [I] Pointer to HRESULT where the value will be unmarshalled to.
2050 *
2051 * RETURNS
2052 * Success: S_OK
2053 * Failure: A COM error code
2054 *
2055 * SEE ALSO
2056 * CoMarshalHresult().
2057 */
2058 HRESULT WINAPI CoUnmarshalHresult(LPSTREAM pStm, HRESULT * phresult)
2059 {
2060 return IStream_Read(pStm, phresult, sizeof(*phresult), NULL);
2061 }