- Correct the ulAssemblyRosterIndex member of ACTCTX_SECTION_KEYED_DATA -- it is...
[reactos.git] / reactos / 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 = (void *)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:\n\tflags = %04lx\n\tcPublicRefs = %ld\n\toxid = %s\n\toid = %s\n\tipid = %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,
557 AuthzSvc, debugstr_w(pServerPrincName), AuthnLevel, ImpLevel,
558 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 (void)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 = (void *)&This->lpVtbl;
841 IUnknown_AddRef((IUnknown *)*ppv);
842 return S_OK;
843 }
844 if (IsEqualIID(riid, &IID_IMarshal))
845 {
846 *ppv = (void *)&This->lpVtblMarshal;
847 IUnknown_AddRef((IUnknown *)*ppv);
848 return S_OK;
849 }
850 if (IsEqualIID(riid, &IID_IClientSecurity))
851 {
852 *ppv = (void *)&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 = (void *)&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 %lu 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, (IUnknown *)pv, mshlflags);
1217 if (hres)
1218 {
1219 ERR("Failed to create ifstub, hres=0x%x\n", hres);
1220 return hres;
1221 }
1222
1223 hres = IStream_Write(pStm, &stdobjref, sizeof(stdobjref), &res);
1224 if (hres) return hres;
1225
1226 return S_OK;
1227 }
1228
1229 /* helper for StdMarshalImpl_UnmarshalInterface - does the unmarshaling with
1230 * no questions asked about the rules surrounding same-apartment unmarshals
1231 * and table marshaling */
1232 static HRESULT unmarshal_object(const STDOBJREF *stdobjref, APARTMENT *apt,
1233 MSHCTX dest_context, void *dest_context_data,
1234 REFIID riid, const OXID_INFO *oxid_info,
1235 void **object)
1236 {
1237 struct proxy_manager *proxy_manager = NULL;
1238 HRESULT hr = S_OK;
1239
1240 assert(apt);
1241
1242 TRACE("stdobjref:\n\tflags = %04lx\n\tcPublicRefs = %ld\n\toxid = %s\n\toid = %s\n\tipid = %s\n",
1243 stdobjref->flags, stdobjref->cPublicRefs,
1244 wine_dbgstr_longlong(stdobjref->oxid),
1245 wine_dbgstr_longlong(stdobjref->oid),
1246 debugstr_guid(&stdobjref->ipid));
1247
1248 /* create a new proxy manager if one doesn't already exist for the
1249 * object */
1250 if (!find_proxy_manager(apt, stdobjref->oxid, stdobjref->oid, &proxy_manager))
1251 {
1252 hr = proxy_manager_construct(apt, stdobjref->flags,
1253 stdobjref->oxid, stdobjref->oid, oxid_info,
1254 &proxy_manager);
1255 }
1256 else
1257 TRACE("proxy manager already created, using\n");
1258
1259 if (hr == S_OK)
1260 {
1261 struct ifproxy * ifproxy;
1262
1263 proxy_manager_set_context(proxy_manager, dest_context, dest_context_data);
1264
1265 hr = proxy_manager_find_ifproxy(proxy_manager, riid, &ifproxy);
1266 if (hr == E_NOINTERFACE)
1267 {
1268 IRpcChannelBuffer *chanbuf;
1269 hr = RPC_CreateClientChannel(&stdobjref->oxid, &stdobjref->ipid,
1270 &proxy_manager->oxid_info,
1271 proxy_manager->dest_context,
1272 proxy_manager->dest_context_data,
1273 &chanbuf);
1274 if (hr == S_OK)
1275 hr = proxy_manager_create_ifproxy(proxy_manager, stdobjref,
1276 riid, chanbuf, &ifproxy);
1277 }
1278 else
1279 IUnknown_AddRef((IUnknown *)ifproxy->iface);
1280
1281 if (hr == S_OK)
1282 {
1283 InterlockedExchangeAdd((LONG *)&ifproxy->refs, stdobjref->cPublicRefs);
1284 /* get at least one external reference to the object to keep it alive */
1285 hr = ifproxy_get_public_ref(ifproxy);
1286 if (FAILED(hr))
1287 ifproxy_destroy(ifproxy);
1288 }
1289
1290 if (hr == S_OK)
1291 *object = ifproxy->iface;
1292 }
1293
1294 /* release our reference to the proxy manager - the client/apartment
1295 * will hold on to the remaining reference for us */
1296 if (proxy_manager) ClientIdentity_Release((IMultiQI*)&proxy_manager->lpVtbl);
1297
1298 return hr;
1299 }
1300
1301 static HRESULT WINAPI
1302 StdMarshalImpl_UnmarshalInterface(LPMARSHAL iface, IStream *pStm, REFIID riid, void **ppv)
1303 {
1304 StdMarshalImpl *This = (StdMarshalImpl *)iface;
1305 struct stub_manager *stubmgr = NULL;
1306 STDOBJREF stdobjref;
1307 ULONG res;
1308 HRESULT hres;
1309 APARTMENT *apt = COM_CurrentApt();
1310 APARTMENT *stub_apt;
1311 OXID oxid;
1312
1313 TRACE("(...,%s,....)\n", debugstr_guid(riid));
1314
1315 /* we need an apartment to unmarshal into */
1316 if (!apt)
1317 {
1318 ERR("Apartment not initialized\n");
1319 return CO_E_NOTINITIALIZED;
1320 }
1321
1322 /* read STDOBJREF from wire */
1323 hres = IStream_Read(pStm, &stdobjref, sizeof(stdobjref), &res);
1324 if (hres) return STG_E_READFAULT;
1325
1326 hres = apartment_getoxid(apt, &oxid);
1327 if (hres) return hres;
1328
1329 /* check if we're marshalling back to ourselves */
1330 if ((oxid == stdobjref.oxid) && (stubmgr = get_stub_manager(apt, stdobjref.oid)))
1331 {
1332 TRACE("Unmarshalling object marshalled in same apartment for iid %s, "
1333 "returning original object %p\n", debugstr_guid(riid), stubmgr->object);
1334
1335 hres = IUnknown_QueryInterface(stubmgr->object, riid, ppv);
1336
1337 /* unref the ifstub. FIXME: only do this on success? */
1338 if (!stub_manager_is_table_marshaled(stubmgr, &stdobjref.ipid))
1339 stub_manager_ext_release(stubmgr, stdobjref.cPublicRefs, stdobjref.flags & SORFP_TABLEWEAK, TRUE);
1340
1341 stub_manager_int_release(stubmgr);
1342 return hres;
1343 }
1344
1345 /* notify stub manager about unmarshal if process-local object.
1346 * note: if the oxid is not found then we and native will quite happily
1347 * ignore table marshaling and normal marshaling rules regarding number of
1348 * unmarshals, etc, but if you abuse these rules then your proxy could end
1349 * up returning RPC_E_DISCONNECTED. */
1350 if ((stub_apt = apartment_findfromoxid(stdobjref.oxid, TRUE)))
1351 {
1352 if ((stubmgr = get_stub_manager(stub_apt, stdobjref.oid)))
1353 {
1354 if (!stub_manager_notify_unmarshal(stubmgr, &stdobjref.ipid))
1355 hres = CO_E_OBJNOTCONNECTED;
1356 }
1357 else
1358 {
1359 WARN("Couldn't find object for OXID %s, OID %s, assuming disconnected\n",
1360 wine_dbgstr_longlong(stdobjref.oxid),
1361 wine_dbgstr_longlong(stdobjref.oid));
1362 hres = CO_E_OBJNOTCONNECTED;
1363 }
1364 }
1365 else
1366 TRACE("Treating unmarshal from OXID %s as inter-process\n",
1367 wine_dbgstr_longlong(stdobjref.oxid));
1368
1369 if (hres == S_OK)
1370 hres = unmarshal_object(&stdobjref, apt, This->dwDestContext,
1371 This->pvDestContext, riid,
1372 stubmgr ? &stubmgr->oxid_info : NULL, ppv);
1373
1374 if (stubmgr) stub_manager_int_release(stubmgr);
1375 if (stub_apt) apartment_release(stub_apt);
1376
1377 if (hres) WARN("Failed with error 0x%08x\n", hres);
1378 else TRACE("Successfully created proxy %p\n", *ppv);
1379
1380 return hres;
1381 }
1382
1383 static HRESULT WINAPI
1384 StdMarshalImpl_ReleaseMarshalData(LPMARSHAL iface, IStream *pStm)
1385 {
1386 STDOBJREF stdobjref;
1387 ULONG res;
1388 HRESULT hres;
1389 struct stub_manager *stubmgr;
1390 APARTMENT *apt;
1391
1392 TRACE("iface=%p, pStm=%p\n", iface, pStm);
1393
1394 hres = IStream_Read(pStm, &stdobjref, sizeof(stdobjref), &res);
1395 if (hres) return STG_E_READFAULT;
1396
1397 TRACE("oxid = %s, oid = %s, ipid = %s\n",
1398 wine_dbgstr_longlong(stdobjref.oxid),
1399 wine_dbgstr_longlong(stdobjref.oid),
1400 wine_dbgstr_guid(&stdobjref.ipid));
1401
1402 if (!(apt = apartment_findfromoxid(stdobjref.oxid, TRUE)))
1403 {
1404 WARN("Could not map OXID %s to apartment object\n",
1405 wine_dbgstr_longlong(stdobjref.oxid));
1406 return RPC_E_INVALID_OBJREF;
1407 }
1408
1409 if (!(stubmgr = get_stub_manager(apt, stdobjref.oid)))
1410 {
1411 ERR("could not map object ID to stub manager, oxid=%s, oid=%s\n",
1412 wine_dbgstr_longlong(stdobjref.oxid), wine_dbgstr_longlong(stdobjref.oid));
1413 return RPC_E_INVALID_OBJREF;
1414 }
1415
1416 stub_manager_release_marshal_data(stubmgr, stdobjref.cPublicRefs, &stdobjref.ipid, stdobjref.flags & SORFP_TABLEWEAK);
1417
1418 stub_manager_int_release(stubmgr);
1419 apartment_release(apt);
1420
1421 return S_OK;
1422 }
1423
1424 static HRESULT WINAPI
1425 StdMarshalImpl_DisconnectObject(LPMARSHAL iface, DWORD dwReserved)
1426 {
1427 FIXME("(), stub!\n");
1428 return S_OK;
1429 }
1430
1431 static const IMarshalVtbl VT_StdMarshal =
1432 {
1433 StdMarshalImpl_QueryInterface,
1434 StdMarshalImpl_AddRef,
1435 StdMarshalImpl_Release,
1436 StdMarshalImpl_GetUnmarshalClass,
1437 StdMarshalImpl_GetMarshalSizeMax,
1438 StdMarshalImpl_MarshalInterface,
1439 StdMarshalImpl_UnmarshalInterface,
1440 StdMarshalImpl_ReleaseMarshalData,
1441 StdMarshalImpl_DisconnectObject
1442 };
1443
1444 static HRESULT StdMarshalImpl_Construct(REFIID riid, void** ppvObject)
1445 {
1446 StdMarshalImpl * pStdMarshal =
1447 HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(StdMarshalImpl));
1448 if (!pStdMarshal)
1449 return E_OUTOFMEMORY;
1450 pStdMarshal->lpvtbl = &VT_StdMarshal;
1451 pStdMarshal->ref = 0;
1452 return IMarshal_QueryInterface((IMarshal*)pStdMarshal, riid, ppvObject);
1453 }
1454
1455 /***********************************************************************
1456 * CoGetStandardMarshal [OLE32.@]
1457 *
1458 * Gets or creates a standard marshal object.
1459 *
1460 * PARAMS
1461 * riid [I] Interface identifier of the pUnk object.
1462 * pUnk [I] Optional. Object to get the marshal object for.
1463 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1464 * pvDestContext [I] Reserved. Must be NULL.
1465 * mshlflags [I] Flags affecting the marshaling process.
1466 * ppMarshal [O] Address where marshal object will be stored.
1467 *
1468 * RETURNS
1469 * Success: S_OK.
1470 * Failure: HRESULT code.
1471 *
1472 * NOTES
1473 *
1474 * The function retrieves the IMarshal object associated with an object if
1475 * that object is currently an active stub, otherwise a new marshal object is
1476 * created.
1477 */
1478 HRESULT WINAPI CoGetStandardMarshal(REFIID riid, IUnknown *pUnk,
1479 DWORD dwDestContext, LPVOID pvDestContext,
1480 DWORD mshlflags, LPMARSHAL *ppMarshal)
1481 {
1482 StdMarshalImpl *dm;
1483
1484 if (pUnk == NULL)
1485 {
1486 FIXME("(%s,NULL,%x,%p,%x,%p), unimplemented yet.\n",
1487 debugstr_guid(riid),dwDestContext,pvDestContext,mshlflags,ppMarshal);
1488 return E_NOTIMPL;
1489 }
1490 TRACE("(%s,%p,%x,%p,%x,%p)\n",
1491 debugstr_guid(riid),pUnk,dwDestContext,pvDestContext,mshlflags,ppMarshal);
1492 *ppMarshal = HeapAlloc(GetProcessHeap(),0,sizeof(StdMarshalImpl));
1493 dm = (StdMarshalImpl*) *ppMarshal;
1494 if (!dm) return E_FAIL;
1495 dm->lpvtbl = &VT_StdMarshal;
1496 dm->ref = 1;
1497
1498 dm->iid = *riid;
1499 dm->dwDestContext = dwDestContext;
1500 dm->pvDestContext = pvDestContext;
1501 dm->mshlflags = mshlflags;
1502 return S_OK;
1503 }
1504
1505 /***********************************************************************
1506 * get_marshaler [internal]
1507 *
1508 * Retrieves an IMarshal interface for an object.
1509 */
1510 static HRESULT get_marshaler(REFIID riid, IUnknown *pUnk, DWORD dwDestContext,
1511 void *pvDestContext, DWORD mshlFlags,
1512 LPMARSHAL *pMarshal)
1513 {
1514 HRESULT hr;
1515
1516 if (!pUnk)
1517 return E_POINTER;
1518 hr = IUnknown_QueryInterface(pUnk, &IID_IMarshal, (LPVOID*)pMarshal);
1519 if (hr)
1520 hr = CoGetStandardMarshal(riid, pUnk, dwDestContext, pvDestContext,
1521 mshlFlags, pMarshal);
1522 return hr;
1523 }
1524
1525 /***********************************************************************
1526 * get_unmarshaler_from_stream [internal]
1527 *
1528 * Creates an IMarshal* object according to the data marshaled to the stream.
1529 * The function leaves the stream pointer at the start of the data written
1530 * to the stream by the IMarshal* object.
1531 */
1532 static HRESULT get_unmarshaler_from_stream(IStream *stream, IMarshal **marshal, IID *iid)
1533 {
1534 HRESULT hr;
1535 ULONG res;
1536 OBJREF objref;
1537
1538 /* read common OBJREF header */
1539 hr = IStream_Read(stream, &objref, FIELD_OFFSET(OBJREF, u_objref), &res);
1540 if (hr || (res != FIELD_OFFSET(OBJREF, u_objref)))
1541 {
1542 ERR("Failed to read common OBJREF header, 0x%08x\n", hr);
1543 return STG_E_READFAULT;
1544 }
1545
1546 /* sanity check on header */
1547 if (objref.signature != OBJREF_SIGNATURE)
1548 {
1549 ERR("Bad OBJREF signature 0x%08lx\n", objref.signature);
1550 return RPC_E_INVALID_OBJREF;
1551 }
1552
1553 if (iid) *iid = objref.iid;
1554
1555 /* FIXME: handler marshaling */
1556 if (objref.flags & OBJREF_STANDARD)
1557 {
1558 TRACE("Using standard unmarshaling\n");
1559 hr = StdMarshalImpl_Construct(&IID_IMarshal, (LPVOID*)marshal);
1560 }
1561 else if (objref.flags & OBJREF_CUSTOM)
1562 {
1563 ULONG custom_header_size = FIELD_OFFSET(OBJREF, u_objref.u_custom.pData) -
1564 FIELD_OFFSET(OBJREF, u_objref.u_custom);
1565 TRACE("Using custom unmarshaling\n");
1566 /* read constant sized OR_CUSTOM data from stream */
1567 hr = IStream_Read(stream, &objref.u_objref.u_custom,
1568 custom_header_size, &res);
1569 if (hr || (res != custom_header_size))
1570 {
1571 ERR("Failed to read OR_CUSTOM header, 0x%08x\n", hr);
1572 return STG_E_READFAULT;
1573 }
1574 /* now create the marshaler specified in the stream */
1575 hr = CoCreateInstance(&objref.u_objref.u_custom.clsid, NULL,
1576 CLSCTX_INPROC_SERVER, &IID_IMarshal,
1577 (LPVOID*)marshal);
1578 }
1579 else
1580 {
1581 FIXME("Invalid or unimplemented marshaling type specified: %lx\n",
1582 objref.flags);
1583 return RPC_E_INVALID_OBJREF;
1584 }
1585
1586 if (hr)
1587 ERR("Failed to create marshal, 0x%08x\n", hr);
1588
1589 return hr;
1590 }
1591
1592 /***********************************************************************
1593 * CoGetMarshalSizeMax [OLE32.@]
1594 *
1595 * Gets the maximum amount of data that will be needed by a marshal.
1596 *
1597 * PARAMS
1598 * pulSize [O] Address where maximum marshal size will be stored.
1599 * riid [I] Identifier of the interface to marshal.
1600 * pUnk [I] Pointer to the object to marshal.
1601 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1602 * pvDestContext [I] Reserved. Must be NULL.
1603 * mshlFlags [I] Flags that affect the marshaling. See CoMarshalInterface().
1604 *
1605 * RETURNS
1606 * Success: S_OK.
1607 * Failure: HRESULT code.
1608 *
1609 * SEE ALSO
1610 * CoMarshalInterface().
1611 */
1612 HRESULT WINAPI CoGetMarshalSizeMax(ULONG *pulSize, REFIID riid, IUnknown *pUnk,
1613 DWORD dwDestContext, void *pvDestContext,
1614 DWORD mshlFlags)
1615 {
1616 HRESULT hr;
1617 LPMARSHAL pMarshal;
1618 CLSID marshaler_clsid;
1619
1620 hr = get_marshaler(riid, pUnk, dwDestContext, pvDestContext, mshlFlags, &pMarshal);
1621 if (hr)
1622 return hr;
1623
1624 hr = IMarshal_GetUnmarshalClass(pMarshal, riid, pUnk, dwDestContext,
1625 pvDestContext, mshlFlags, &marshaler_clsid);
1626 if (hr)
1627 {
1628 ERR("IMarshal::GetUnmarshalClass failed, 0x%08x\n", hr);
1629 IMarshal_Release(pMarshal);
1630 return hr;
1631 }
1632
1633 hr = IMarshal_GetMarshalSizeMax(pMarshal, riid, pUnk, dwDestContext,
1634 pvDestContext, mshlFlags, pulSize);
1635 if (IsEqualCLSID(&marshaler_clsid, &CLSID_DfMarshal))
1636 /* add on the size of the common header */
1637 *pulSize += FIELD_OFFSET(OBJREF, u_objref);
1638 else
1639 /* custom marshaling: add on the size of the whole OBJREF structure
1640 * like native does */
1641 *pulSize += sizeof(OBJREF);
1642
1643 IMarshal_Release(pMarshal);
1644 return hr;
1645 }
1646
1647
1648 static void dump_MSHLFLAGS(MSHLFLAGS flags)
1649 {
1650 if (flags & MSHLFLAGS_TABLESTRONG)
1651 TRACE(" MSHLFLAGS_TABLESTRONG");
1652 if (flags & MSHLFLAGS_TABLEWEAK)
1653 TRACE(" MSHLFLAGS_TABLEWEAK");
1654 if (!(flags & (MSHLFLAGS_TABLESTRONG|MSHLFLAGS_TABLEWEAK)))
1655 TRACE(" MSHLFLAGS_NORMAL");
1656 if (flags & MSHLFLAGS_NOPING)
1657 TRACE(" MSHLFLAGS_NOPING");
1658 }
1659
1660 /***********************************************************************
1661 * CoMarshalInterface [OLE32.@]
1662 *
1663 * Marshals an interface into a stream so that the object can then be
1664 * unmarshaled from another COM apartment and used remotely.
1665 *
1666 * PARAMS
1667 * pStream [I] Stream the object will be marshaled into.
1668 * riid [I] Identifier of the interface to marshal.
1669 * pUnk [I] Pointer to the object to marshal.
1670 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1671 * pvDestContext [I] Reserved. Must be NULL.
1672 * mshlFlags [I] Flags that affect the marshaling. See notes.
1673 *
1674 * RETURNS
1675 * Success: S_OK.
1676 * Failure: HRESULT code.
1677 *
1678 * NOTES
1679 *
1680 * The mshlFlags parameter can take one or more of the following flags:
1681 *| MSHLFLAGS_NORMAL - Unmarshal once, releases stub on last proxy release.
1682 *| MSHLFLAGS_TABLESTRONG - Unmarshal many, release when CoReleaseMarshalData() called.
1683 *| MSHLFLAGS_TABLEWEAK - Unmarshal many, releases stub on last proxy release.
1684 *| MSHLFLAGS_NOPING - No automatic garbage collection (and so reduces network traffic).
1685 *
1686 * If a marshaled object is not unmarshaled, then CoReleaseMarshalData() must
1687 * be called in order to release the resources used in the marshaling.
1688 *
1689 * SEE ALSO
1690 * CoUnmarshalInterface(), CoReleaseMarshalData().
1691 */
1692 HRESULT WINAPI CoMarshalInterface(IStream *pStream, REFIID riid, IUnknown *pUnk,
1693 DWORD dwDestContext, void *pvDestContext,
1694 DWORD mshlFlags)
1695 {
1696 HRESULT hr;
1697 CLSID marshaler_clsid;
1698 OBJREF objref;
1699 LPMARSHAL pMarshal;
1700
1701 TRACE("(%p, %s, %p, %x, %p,", pStream, debugstr_guid(riid), pUnk,
1702 dwDestContext, pvDestContext);
1703 dump_MSHLFLAGS(mshlFlags);
1704 TRACE(")\n");
1705
1706 if (!pUnk || !pStream)
1707 return E_INVALIDARG;
1708
1709 objref.signature = OBJREF_SIGNATURE;
1710 objref.iid = *riid;
1711
1712 /* get the marshaler for the specified interface */
1713 hr = get_marshaler(riid, pUnk, dwDestContext, pvDestContext, mshlFlags, &pMarshal);
1714 if (hr)
1715 {
1716 ERR("Failed to get marshaller, 0x%08x\n", hr);
1717 return hr;
1718 }
1719
1720 hr = IMarshal_GetUnmarshalClass(pMarshal, riid, pUnk, dwDestContext,
1721 pvDestContext, mshlFlags, &marshaler_clsid);
1722 if (hr)
1723 {
1724 ERR("IMarshal::GetUnmarshalClass failed, 0x%08x\n", hr);
1725 goto cleanup;
1726 }
1727
1728 /* FIXME: implement handler marshaling too */
1729 if (IsEqualCLSID(&marshaler_clsid, &CLSID_DfMarshal))
1730 {
1731 TRACE("Using standard marshaling\n");
1732 objref.flags = OBJREF_STANDARD;
1733
1734 /* write the common OBJREF header to the stream */
1735 hr = IStream_Write(pStream, &objref, FIELD_OFFSET(OBJREF, u_objref), NULL);
1736 if (hr)
1737 {
1738 ERR("Failed to write OBJREF header to stream, 0x%08x\n", hr);
1739 goto cleanup;
1740 }
1741 }
1742 else
1743 {
1744 TRACE("Using custom marshaling\n");
1745 objref.flags = OBJREF_CUSTOM;
1746 objref.u_objref.u_custom.clsid = marshaler_clsid;
1747 objref.u_objref.u_custom.cbExtension = 0;
1748 objref.u_objref.u_custom.size = 0;
1749 hr = IMarshal_GetMarshalSizeMax(pMarshal, riid, pUnk, dwDestContext,
1750 pvDestContext, mshlFlags,
1751 &objref.u_objref.u_custom.size);
1752 if (hr)
1753 {
1754 ERR("Failed to get max size of marshal data, error 0x%08x\n", hr);
1755 goto cleanup;
1756 }
1757 /* write constant sized common header and OR_CUSTOM data into stream */
1758 hr = IStream_Write(pStream, &objref,
1759 FIELD_OFFSET(OBJREF, u_objref.u_custom.pData), NULL);
1760 if (hr)
1761 {
1762 ERR("Failed to write OR_CUSTOM header to stream with 0x%08x\n", hr);
1763 goto cleanup;
1764 }
1765 }
1766
1767 TRACE("Calling IMarshal::MarshalInterace\n");
1768 /* call helper object to do the actual marshaling */
1769 hr = IMarshal_MarshalInterface(pMarshal, pStream, riid, pUnk, dwDestContext,
1770 pvDestContext, mshlFlags);
1771
1772 if (hr)
1773 {
1774 ERR("Failed to marshal the interface %s, %x\n", debugstr_guid(riid), hr);
1775 goto cleanup;
1776 }
1777
1778 cleanup:
1779 IMarshal_Release(pMarshal);
1780
1781 TRACE("completed with hr 0x%08x\n", hr);
1782
1783 return hr;
1784 }
1785
1786 /***********************************************************************
1787 * CoUnmarshalInterface [OLE32.@]
1788 *
1789 * Unmarshals an object from a stream by creating a proxy to the remote
1790 * object, if necessary.
1791 *
1792 * PARAMS
1793 *
1794 * pStream [I] Stream containing the marshaled object.
1795 * riid [I] Interface identifier of the object to create a proxy to.
1796 * ppv [O] Address where proxy will be stored.
1797 *
1798 * RETURNS
1799 *
1800 * Success: S_OK.
1801 * Failure: HRESULT code.
1802 *
1803 * SEE ALSO
1804 * CoMarshalInterface().
1805 */
1806 HRESULT WINAPI CoUnmarshalInterface(IStream *pStream, REFIID riid, LPVOID *ppv)
1807 {
1808 HRESULT hr;
1809 LPMARSHAL pMarshal;
1810 IID iid;
1811 IUnknown *object;
1812
1813 TRACE("(%p, %s, %p)\n", pStream, debugstr_guid(riid), ppv);
1814
1815 if (!pStream || !ppv)
1816 return E_INVALIDARG;
1817
1818 hr = get_unmarshaler_from_stream(pStream, &pMarshal, &iid);
1819 if (hr != S_OK)
1820 return hr;
1821
1822 /* call the helper object to do the actual unmarshaling */
1823 hr = IMarshal_UnmarshalInterface(pMarshal, pStream, &iid, (LPVOID*)&object);
1824 if (hr)
1825 ERR("IMarshal::UnmarshalInterface failed, 0x%08x\n", hr);
1826
1827 if (hr == S_OK)
1828 {
1829 /* IID_NULL means use the interface ID of the marshaled object */
1830 if (!IsEqualIID(riid, &IID_NULL) && !IsEqualIID(riid, &iid))
1831 {
1832 TRACE("requested interface != marshalled interface, additional QI needed\n");
1833 hr = IUnknown_QueryInterface(object, riid, ppv);
1834 if (hr)
1835 ERR("Couldn't query for interface %s, hr = 0x%08x\n",
1836 debugstr_guid(riid), hr);
1837 IUnknown_Release(object);
1838 }
1839 else
1840 {
1841 *ppv = object;
1842 }
1843 }
1844
1845 IMarshal_Release(pMarshal);
1846
1847 TRACE("completed with hr 0x%x\n", hr);
1848
1849 return hr;
1850 }
1851
1852 /***********************************************************************
1853 * CoReleaseMarshalData [OLE32.@]
1854 *
1855 * Releases resources associated with an object that has been marshaled into
1856 * a stream.
1857 *
1858 * PARAMS
1859 *
1860 * pStream [I] The stream that the object has been marshaled into.
1861 *
1862 * RETURNS
1863 * Success: S_OK.
1864 * Failure: HRESULT error code.
1865 *
1866 * NOTES
1867 *
1868 * Call this function to release resources associated with a normal or
1869 * table-weak marshal that will not be unmarshaled, and all table-strong
1870 * marshals when they are no longer needed.
1871 *
1872 * SEE ALSO
1873 * CoMarshalInterface(), CoUnmarshalInterface().
1874 */
1875 HRESULT WINAPI CoReleaseMarshalData(IStream *pStream)
1876 {
1877 HRESULT hr;
1878 LPMARSHAL pMarshal;
1879
1880 TRACE("(%p)\n", pStream);
1881
1882 hr = get_unmarshaler_from_stream(pStream, &pMarshal, NULL);
1883 if (hr != S_OK)
1884 return hr;
1885
1886 /* call the helper object to do the releasing of marshal data */
1887 hr = IMarshal_ReleaseMarshalData(pMarshal, pStream);
1888 if (hr)
1889 ERR("IMarshal::ReleaseMarshalData failed with error 0x%08x\n", hr);
1890
1891 IMarshal_Release(pMarshal);
1892 return hr;
1893 }
1894
1895
1896 /***********************************************************************
1897 * CoMarshalInterThreadInterfaceInStream [OLE32.@]
1898 *
1899 * Marshal an interface across threads in the same process.
1900 *
1901 * PARAMS
1902 * riid [I] Identifier of the interface to be marshalled.
1903 * pUnk [I] Pointer to IUnknown-derived interface that will be marshalled.
1904 * ppStm [O] Pointer to IStream object that is created and then used to store the marshalled interface.
1905 *
1906 * RETURNS
1907 * Success: S_OK
1908 * Failure: E_OUTOFMEMORY and other COM error codes
1909 *
1910 * SEE ALSO
1911 * CoMarshalInterface(), CoUnmarshalInterface() and CoGetInterfaceAndReleaseStream()
1912 */
1913 HRESULT WINAPI CoMarshalInterThreadInterfaceInStream(
1914 REFIID riid, LPUNKNOWN pUnk, LPSTREAM * ppStm)
1915 {
1916 ULARGE_INTEGER xpos;
1917 LARGE_INTEGER seekto;
1918 HRESULT hres;
1919
1920 TRACE("(%s, %p, %p)\n",debugstr_guid(riid), pUnk, ppStm);
1921
1922 hres = CreateStreamOnHGlobal(NULL, TRUE, ppStm);
1923 if (FAILED(hres)) return hres;
1924 hres = CoMarshalInterface(*ppStm, riid, pUnk, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1925
1926 if (SUCCEEDED(hres))
1927 {
1928 memset(&seekto, 0, sizeof(seekto));
1929 IStream_Seek(*ppStm, seekto, STREAM_SEEK_SET, &xpos);
1930 }
1931 else
1932 {
1933 IStream_Release(*ppStm);
1934 *ppStm = NULL;
1935 }
1936
1937 return hres;
1938 }
1939
1940 /***********************************************************************
1941 * CoGetInterfaceAndReleaseStream [OLE32.@]
1942 *
1943 * Unmarshalls an interface from a stream and then releases the stream.
1944 *
1945 * PARAMS
1946 * pStm [I] Stream that contains the marshalled interface.
1947 * riid [I] Interface identifier of the object to unmarshall.
1948 * ppv [O] Address of pointer where the requested interface object will be stored.
1949 *
1950 * RETURNS
1951 * Success: S_OK
1952 * Failure: A COM error code
1953 *
1954 * SEE ALSO
1955 * CoMarshalInterThreadInterfaceInStream() and CoUnmarshalInterface()
1956 */
1957 HRESULT WINAPI CoGetInterfaceAndReleaseStream(LPSTREAM pStm, REFIID riid,
1958 LPVOID *ppv)
1959 {
1960 HRESULT hres;
1961
1962 TRACE("(%p, %s, %p)\n", pStm, debugstr_guid(riid), ppv);
1963
1964 if(!pStm) return E_INVALIDARG;
1965 hres = CoUnmarshalInterface(pStm, riid, ppv);
1966 IStream_Release(pStm);
1967 return hres;
1968 }
1969
1970 static HRESULT WINAPI StdMarshalCF_QueryInterface(LPCLASSFACTORY iface,
1971 REFIID riid, LPVOID *ppv)
1972 {
1973 *ppv = NULL;
1974 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory))
1975 {
1976 *ppv = (LPVOID)iface;
1977 return S_OK;
1978 }
1979 return E_NOINTERFACE;
1980 }
1981
1982 static ULONG WINAPI StdMarshalCF_AddRef(LPCLASSFACTORY iface)
1983 {
1984 return 2; /* non-heap based object */
1985 }
1986
1987 static ULONG WINAPI StdMarshalCF_Release(LPCLASSFACTORY iface)
1988 {
1989 return 1; /* non-heap based object */
1990 }
1991
1992 static HRESULT WINAPI StdMarshalCF_CreateInstance(LPCLASSFACTORY iface,
1993 LPUNKNOWN pUnk, REFIID riid, LPVOID *ppv)
1994 {
1995 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IMarshal))
1996 return StdMarshalImpl_Construct(riid, ppv);
1997
1998 FIXME("(%s), not supported.\n",debugstr_guid(riid));
1999 return E_NOINTERFACE;
2000 }
2001
2002 static HRESULT WINAPI StdMarshalCF_LockServer(LPCLASSFACTORY iface, BOOL fLock)
2003 {
2004 FIXME("(%d), stub!\n",fLock);
2005 return S_OK;
2006 }
2007
2008 static const IClassFactoryVtbl StdMarshalCFVtbl =
2009 {
2010 StdMarshalCF_QueryInterface,
2011 StdMarshalCF_AddRef,
2012 StdMarshalCF_Release,
2013 StdMarshalCF_CreateInstance,
2014 StdMarshalCF_LockServer
2015 };
2016 static const IClassFactoryVtbl *StdMarshalCF = &StdMarshalCFVtbl;
2017
2018 HRESULT MARSHAL_GetStandardMarshalCF(LPVOID *ppv)
2019 {
2020 *ppv = &StdMarshalCF;
2021 return S_OK;
2022 }
2023
2024 /***********************************************************************
2025 * CoMarshalHresult [OLE32.@]
2026 *
2027 * Marshals an HRESULT value into a stream.
2028 *
2029 * PARAMS
2030 * pStm [I] Stream that hresult will be marshalled into.
2031 * hresult [I] HRESULT to be marshalled.
2032 *
2033 * RETURNS
2034 * Success: S_OK
2035 * Failure: A COM error code
2036 *
2037 * SEE ALSO
2038 * CoUnmarshalHresult().
2039 */
2040 HRESULT WINAPI CoMarshalHresult(LPSTREAM pStm, HRESULT hresult)
2041 {
2042 return IStream_Write(pStm, &hresult, sizeof(hresult), NULL);
2043 }
2044
2045 /***********************************************************************
2046 * CoUnmarshalHresult [OLE32.@]
2047 *
2048 * Unmarshals an HRESULT value from a stream.
2049 *
2050 * PARAMS
2051 * pStm [I] Stream that hresult will be unmarshalled from.
2052 * phresult [I] Pointer to HRESULT where the value will be unmarshalled to.
2053 *
2054 * RETURNS
2055 * Success: S_OK
2056 * Failure: A COM error code
2057 *
2058 * SEE ALSO
2059 * CoMarshalHresult().
2060 */
2061 HRESULT WINAPI CoUnmarshalHresult(LPSTREAM pStm, HRESULT * phresult)
2062 {
2063 return IStream_Read(pStm, phresult, sizeof(*phresult), NULL);
2064 }