wintrust: Return error directly from SOFTPUB_LoadCertMessage.
[wine/multimedia.git] / dlls / ole32 / moniker.c
bloba7c5a11c98b901d8f38e420afd13f342e34eb109
1 /*
2 * Monikers
4 * Copyright 1998 Marcus Meissner
5 * Copyright 1999 Noomen Hamza
6 * Copyright 2005 Robert Shearman (for CodeWeavers)
7 * Copyright 2007 Robert Shearman
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "config.h"
25 #include "wine/port.h"
27 #include <stdarg.h>
28 #include <string.h>
30 #define COBJMACROS
32 #include "winerror.h"
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winuser.h"
36 #include "wtypes.h"
37 #include "ole2.h"
39 #include "wine/list.h"
40 #include "wine/debug.h"
41 #include "wine/unicode.h"
42 #include "wine/exception.h"
44 #include "compobj_private.h"
45 #include "moniker.h"
46 #include "irot.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(ole);
50 /* see MSDN docs for IROTData::GetComparisonData, which states what this
51 * constant is
53 #define MAX_COMPARISON_DATA 2048
55 static LONG WINAPI rpc_filter(EXCEPTION_POINTERS *eptr)
57 return I_RpcExceptionFilter(eptr->ExceptionRecord->ExceptionCode);
60 /* define the structure of the running object table elements */
61 struct rot_entry
63 struct list entry;
64 InterfaceData* object; /* marshaled running object*/
65 MonikerComparisonData* moniker_data; /* moniker comparison data that identifies this object */
66 DWORD cookie; /* cookie identifying this object */
67 FILETIME last_modified;
68 IrotContextHandle ctxt_handle;
71 /* define the RunningObjectTableImpl structure */
72 typedef struct RunningObjectTableImpl
74 const IRunningObjectTableVtbl *lpVtbl;
75 LONG ref;
77 struct list rot; /* list of ROT entries */
78 CRITICAL_SECTION lock;
79 } RunningObjectTableImpl;
81 static RunningObjectTableImpl* runningObjectTableInstance = NULL;
82 static IrotHandle irot_handle;
84 /* define the EnumMonikerImpl structure */
85 typedef struct EnumMonikerImpl
87 const IEnumMonikerVtbl *lpVtbl;
88 LONG ref;
90 InterfaceList *moniker_list;
91 ULONG pos;
92 } EnumMonikerImpl;
95 /* IEnumMoniker Local functions*/
96 static HRESULT EnumMonikerImpl_CreateEnumROTMoniker(InterfaceList *moniker_list,
97 ULONG pos, IEnumMoniker **ppenumMoniker);
99 static IrotHandle get_irot_handle(void)
101 if (!irot_handle)
103 RPC_STATUS status;
104 RPC_WSTR binding;
105 IrotHandle new_handle;
106 unsigned short ncacn_np[] = IROT_PROTSEQ;
107 unsigned short endpoint[] = IROT_ENDPOINT;
108 status = RpcStringBindingComposeW(NULL, ncacn_np, NULL, endpoint, NULL, &binding);
109 if (status == RPC_S_OK)
111 status = RpcBindingFromStringBindingW(binding, &new_handle);
112 RpcStringFreeW(&binding);
114 if (status != RPC_S_OK)
115 return NULL;
116 if (InterlockedCompareExchangePointer(&irot_handle, new_handle, NULL))
117 /* another thread beat us to it */
118 RpcBindingFree(&new_handle);
120 return irot_handle;
123 static BOOL start_rpcss(void)
125 PROCESS_INFORMATION pi;
126 STARTUPINFOW si;
127 WCHAR cmd[MAX_PATH];
128 static const WCHAR rpcss[] = {'\\','r','p','c','s','s','.','e','x','e',0};
129 BOOL rslt;
131 TRACE("\n");
133 ZeroMemory(&si, sizeof(STARTUPINFOA));
134 si.cb = sizeof(STARTUPINFOA);
135 GetSystemDirectoryW( cmd, MAX_PATH - sizeof(rpcss)/sizeof(WCHAR) );
136 strcatW( cmd, rpcss );
138 rslt = CreateProcessW( cmd, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi );
140 if (rslt)
142 CloseHandle(pi.hProcess);
143 CloseHandle(pi.hThread);
144 Sleep(100);
147 return rslt;
150 static HRESULT create_stream_on_mip_ro(const InterfaceData *mip, IStream **stream)
152 HGLOBAL hglobal = GlobalAlloc(0, mip->ulCntData);
153 void *pv = GlobalLock(hglobal);
154 memcpy(pv, mip->abData, mip->ulCntData);
155 GlobalUnlock(hglobal);
156 return CreateStreamOnHGlobal(hglobal, TRUE, stream);
159 static inline void rot_entry_delete(struct rot_entry *rot_entry)
161 if (rot_entry->cookie)
163 InterfaceData *object = NULL;
164 InterfaceData *moniker = NULL;
165 __TRY
167 IrotRevoke(get_irot_handle(), rot_entry->cookie,
168 &rot_entry->ctxt_handle, &object, &moniker);
170 __EXCEPT(rpc_filter)
173 __ENDTRY
174 MIDL_user_free(object);
175 if (moniker)
177 IStream *stream;
178 HRESULT hr;
179 hr = create_stream_on_mip_ro(moniker, &stream);
180 if (hr == S_OK)
182 CoReleaseMarshalData(stream);
183 IUnknown_Release(stream);
186 MIDL_user_free(moniker);
188 if (rot_entry->object)
190 IStream *stream;
191 HRESULT hr;
192 hr = create_stream_on_mip_ro(rot_entry->object, &stream);
193 if (hr == S_OK)
195 CoReleaseMarshalData(stream);
196 IUnknown_Release(stream);
199 HeapFree(GetProcessHeap(), 0, rot_entry->object);
200 HeapFree(GetProcessHeap(), 0, rot_entry->moniker_data);
201 HeapFree(GetProcessHeap(), 0, rot_entry);
204 /* moniker_data must be freed with HeapFree when no longer in use */
205 static HRESULT get_moniker_comparison_data(IMoniker *pMoniker, MonikerComparisonData **moniker_data)
207 HRESULT hr;
208 IROTData *pROTData = NULL;
209 hr = IMoniker_QueryInterface(pMoniker, &IID_IROTData, (void *)&pROTData);
210 if (SUCCEEDED(hr))
212 ULONG size = MAX_COMPARISON_DATA;
213 *moniker_data = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(MonikerComparisonData, abData[size]));
214 if (!*moniker_data)
216 IROTData_Release(pROTData);
217 return E_OUTOFMEMORY;
219 hr = IROTData_GetComparisonData(pROTData, (*moniker_data)->abData, size, &size);
220 IROTData_Release(pROTData);
221 if (hr != S_OK)
223 ERR("Failed to copy comparison data into buffer, hr = 0x%08x\n", hr);
224 HeapFree(GetProcessHeap(), 0, *moniker_data);
225 return hr;
227 (*moniker_data)->ulCntData = size;
229 else
231 IBindCtx *pbc;
232 LPOLESTR pszDisplayName;
233 CLSID clsid;
234 int len;
236 TRACE("generating comparison data from display name\n");
238 hr = CreateBindCtx(0, &pbc);
239 if (FAILED(hr))
240 return hr;
241 hr = IMoniker_GetDisplayName(pMoniker, pbc, NULL, &pszDisplayName);
242 IBindCtx_Release(pbc);
243 if (FAILED(hr))
244 return hr;
245 hr = IMoniker_GetClassID(pMoniker, &clsid);
246 if (FAILED(hr))
248 CoTaskMemFree(pszDisplayName);
249 return hr;
252 len = strlenW(pszDisplayName);
253 *moniker_data = HeapAlloc(GetProcessHeap(), 0,
254 FIELD_OFFSET(MonikerComparisonData, abData[sizeof(CLSID) + (len+1)*sizeof(WCHAR)]));
255 if (!*moniker_data)
257 CoTaskMemFree(pszDisplayName);
258 return E_OUTOFMEMORY;
260 (*moniker_data)->ulCntData = sizeof(CLSID) + (len+1)*sizeof(WCHAR);
262 memcpy(&(*moniker_data)->abData[0], &clsid, sizeof(clsid));
263 memcpy(&(*moniker_data)->abData[sizeof(clsid)], pszDisplayName, (len+1)*sizeof(WCHAR));
264 CoTaskMemFree(pszDisplayName);
266 return S_OK;
269 static HRESULT reduce_moniker(IMoniker *pmk, IBindCtx *pbc, IMoniker **pmkReduced)
271 IBindCtx *pbcNew = NULL;
272 HRESULT hr;
273 if (!pbc)
275 hr = CreateBindCtx(0, &pbcNew);
276 if (FAILED(hr))
277 return hr;
278 pbc = pbcNew;
280 hr = IMoniker_Reduce(pmk, pbc, MKRREDUCE_ALL, NULL, pmkReduced);
281 if (FAILED(hr))
282 ERR("reducing moniker failed with error 0x%08x\n", hr);
283 if (pbcNew) IBindCtx_Release(pbcNew);
284 return hr;
287 /***********************************************************************
288 * RunningObjectTable_QueryInterface
290 static HRESULT WINAPI
291 RunningObjectTableImpl_QueryInterface(IRunningObjectTable* iface,
292 REFIID riid,void** ppvObject)
294 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
296 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
298 /* validate arguments */
300 if (ppvObject==0)
301 return E_INVALIDARG;
303 *ppvObject = 0;
305 if (IsEqualIID(&IID_IUnknown, riid) ||
306 IsEqualIID(&IID_IRunningObjectTable, riid))
307 *ppvObject = This;
309 if ((*ppvObject)==0)
310 return E_NOINTERFACE;
312 IRunningObjectTable_AddRef(iface);
314 return S_OK;
317 /***********************************************************************
318 * RunningObjectTable_AddRef
320 static ULONG WINAPI
321 RunningObjectTableImpl_AddRef(IRunningObjectTable* iface)
323 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
325 TRACE("(%p)\n",This);
327 return InterlockedIncrement(&This->ref);
330 /***********************************************************************
331 * RunningObjectTable_Destroy
333 static HRESULT
334 RunningObjectTableImpl_Destroy(void)
336 struct list *cursor, *cursor2;
337 IrotHandle old_handle;
339 TRACE("()\n");
341 if (runningObjectTableInstance==NULL)
342 return E_INVALIDARG;
344 /* free the ROT table memory */
345 LIST_FOR_EACH_SAFE(cursor, cursor2, &runningObjectTableInstance->rot)
347 struct rot_entry *rot_entry = LIST_ENTRY(cursor, struct rot_entry, entry);
348 list_remove(&rot_entry->entry);
349 rot_entry_delete(rot_entry);
352 DEBUG_CLEAR_CRITSEC_NAME(&runningObjectTableInstance->lock);
353 DeleteCriticalSection(&runningObjectTableInstance->lock);
355 /* free the ROT structure memory */
356 HeapFree(GetProcessHeap(),0,runningObjectTableInstance);
357 runningObjectTableInstance = NULL;
359 old_handle = irot_handle;
360 irot_handle = NULL;
361 if (old_handle)
362 RpcBindingFree(&old_handle);
364 return S_OK;
367 /***********************************************************************
368 * RunningObjectTable_Release
370 static ULONG WINAPI
371 RunningObjectTableImpl_Release(IRunningObjectTable* iface)
373 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
374 ULONG ref;
376 TRACE("(%p)\n",This);
378 ref = InterlockedDecrement(&This->ref);
380 /* uninitialize ROT structure if there's no more references to it */
381 if (ref == 0)
383 struct list *cursor, *cursor2;
384 LIST_FOR_EACH_SAFE(cursor, cursor2, &This->rot)
386 struct rot_entry *rot_entry = LIST_ENTRY(cursor, struct rot_entry, entry);
387 list_remove(&rot_entry->entry);
388 rot_entry_delete(rot_entry);
390 /* RunningObjectTable data structure will be not destroyed here ! the destruction will be done only
391 * when RunningObjectTableImpl_UnInitialize function is called
395 return ref;
398 /***********************************************************************
399 * RunningObjectTable_Register
401 * PARAMS
402 * grfFlags [in] Registration options
403 * punkObject [in] the object being registered
404 * pmkObjectName [in] the moniker of the object being registered
405 * pdwRegister [out] the value identifying the registration
407 static HRESULT WINAPI
408 RunningObjectTableImpl_Register(IRunningObjectTable* iface, DWORD grfFlags,
409 IUnknown *punkObject, IMoniker *pmkObjectName, DWORD *pdwRegister)
411 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
412 struct rot_entry *rot_entry;
413 HRESULT hr = S_OK;
414 IStream *pStream = NULL;
415 DWORD mshlflags;
416 IBindCtx *pbc;
417 InterfaceData *moniker = NULL;
419 TRACE("(%p,%d,%p,%p,%p)\n",This,grfFlags,punkObject,pmkObjectName,pdwRegister);
421 if (grfFlags & ~(ROTFLAGS_REGISTRATIONKEEPSALIVE|ROTFLAGS_ALLOWANYCLIENT))
423 ERR("Invalid grfFlags: 0x%08x\n", grfFlags & ~(ROTFLAGS_REGISTRATIONKEEPSALIVE|ROTFLAGS_ALLOWANYCLIENT));
424 return E_INVALIDARG;
427 if (punkObject==NULL || pmkObjectName==NULL || pdwRegister==NULL)
428 return E_INVALIDARG;
430 rot_entry = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*rot_entry));
431 if (!rot_entry)
432 return E_OUTOFMEMORY;
434 /* marshal object */
435 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
436 if (hr != S_OK)
438 rot_entry_delete(rot_entry);
439 return hr;
441 mshlflags = (grfFlags & ROTFLAGS_REGISTRATIONKEEPSALIVE) ? MSHLFLAGS_TABLESTRONG : MSHLFLAGS_TABLEWEAK;
442 hr = CoMarshalInterface(pStream, &IID_IUnknown, punkObject, MSHCTX_LOCAL | MSHCTX_NOSHAREDMEM, NULL, mshlflags);
443 /* FIXME: a cleaner way would be to create an IStream class that writes
444 * directly to an MInterfacePointer */
445 if (hr == S_OK)
447 HGLOBAL hglobal;
448 hr = GetHGlobalFromStream(pStream, &hglobal);
449 if (hr == S_OK)
451 SIZE_T size = GlobalSize(hglobal);
452 const void *pv = GlobalLock(hglobal);
453 rot_entry->object = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(MInterfacePointer, abData[size]));
454 rot_entry->object->ulCntData = size;
455 memcpy(rot_entry->object->abData, pv, size);
456 GlobalUnlock(hglobal);
459 IStream_Release(pStream);
460 if (hr != S_OK)
462 rot_entry_delete(rot_entry);
463 return hr;
466 hr = CreateBindCtx(0, &pbc);
467 if (FAILED(hr))
469 rot_entry_delete(rot_entry);
470 return hr;
473 hr = reduce_moniker(pmkObjectName, pbc, &pmkObjectName);
474 if (FAILED(hr))
476 rot_entry_delete(rot_entry);
477 IBindCtx_Release(pbc);
478 return hr;
481 hr = IMoniker_GetTimeOfLastChange(pmkObjectName, pbc, NULL,
482 &rot_entry->last_modified);
483 IBindCtx_Release(pbc);
484 if (FAILED(hr))
486 CoFileTimeNow(&rot_entry->last_modified);
487 hr = S_OK;
490 hr = get_moniker_comparison_data(pmkObjectName,
491 &rot_entry->moniker_data);
492 if (hr != S_OK)
494 rot_entry_delete(rot_entry);
495 IMoniker_Release(pmkObjectName);
496 return hr;
499 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
500 if (hr != S_OK)
502 rot_entry_delete(rot_entry);
503 IMoniker_Release(pmkObjectName);
504 return hr;
506 /* marshal moniker */
507 hr = CoMarshalInterface(pStream, &IID_IMoniker, (IUnknown *)pmkObjectName,
508 MSHCTX_LOCAL | MSHCTX_NOSHAREDMEM, NULL, MSHLFLAGS_TABLESTRONG);
509 /* FIXME: a cleaner way would be to create an IStream class that writes
510 * directly to an MInterfacePointer */
511 if (hr == S_OK)
513 HGLOBAL hglobal;
514 hr = GetHGlobalFromStream(pStream, &hglobal);
515 if (hr == S_OK)
517 SIZE_T size = GlobalSize(hglobal);
518 const void *pv = GlobalLock(hglobal);
519 moniker = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(InterfaceData, abData[size]));
520 moniker->ulCntData = size;
521 memcpy(moniker->abData, pv, size);
522 GlobalUnlock(hglobal);
525 IStream_Release(pStream);
526 IMoniker_Release(pmkObjectName);
527 if (hr != S_OK)
529 HeapFree(GetProcessHeap(), 0, moniker);
530 rot_entry_delete(rot_entry);
531 return hr;
535 while (TRUE)
537 __TRY
539 hr = IrotRegister(get_irot_handle(), rot_entry->moniker_data,
540 rot_entry->object, moniker,
541 &rot_entry->last_modified, grfFlags,
542 &rot_entry->cookie, &rot_entry->ctxt_handle);
544 __EXCEPT(rpc_filter)
546 hr = HRESULT_FROM_WIN32(GetExceptionCode());
548 __ENDTRY
549 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
551 if (start_rpcss())
552 continue;
554 break;
556 HeapFree(GetProcessHeap(), 0, moniker);
557 if (FAILED(hr))
559 rot_entry_delete(rot_entry);
560 return hr;
563 /* gives a registration identifier to the registered object*/
564 *pdwRegister = rot_entry->cookie;
566 EnterCriticalSection(&This->lock);
567 list_add_tail(&This->rot, &rot_entry->entry);
568 LeaveCriticalSection(&This->lock);
570 return hr;
573 /***********************************************************************
574 * RunningObjectTable_Revoke
576 * PARAMS
577 * dwRegister [in] Value identifying registration to be revoked
579 static HRESULT WINAPI
580 RunningObjectTableImpl_Revoke( IRunningObjectTable* iface, DWORD dwRegister)
582 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
583 struct rot_entry *rot_entry;
585 TRACE("(%p,%d)\n",This,dwRegister);
587 EnterCriticalSection(&This->lock);
588 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, struct rot_entry, entry)
590 if (rot_entry->cookie == dwRegister)
592 list_remove(&rot_entry->entry);
593 LeaveCriticalSection(&This->lock);
595 rot_entry_delete(rot_entry);
596 return S_OK;
599 LeaveCriticalSection(&This->lock);
601 return E_INVALIDARG;
604 /***********************************************************************
605 * RunningObjectTable_IsRunning
607 * PARAMS
608 * pmkObjectName [in] moniker of the object whose status is desired
610 static HRESULT WINAPI
611 RunningObjectTableImpl_IsRunning( IRunningObjectTable* iface, IMoniker *pmkObjectName)
613 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
614 MonikerComparisonData *moniker_data;
615 HRESULT hr;
616 const struct rot_entry *rot_entry;
618 TRACE("(%p,%p)\n",This,pmkObjectName);
620 hr = reduce_moniker(pmkObjectName, NULL, &pmkObjectName);
621 if (FAILED(hr))
622 return hr;
623 hr = get_moniker_comparison_data(pmkObjectName, &moniker_data);
624 IMoniker_Release(pmkObjectName);
625 if (hr != S_OK)
626 return hr;
628 hr = S_FALSE;
629 EnterCriticalSection(&This->lock);
630 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, const struct rot_entry, entry)
632 if ((rot_entry->moniker_data->ulCntData == moniker_data->ulCntData) &&
633 !memcmp(moniker_data->abData, rot_entry->moniker_data->abData, moniker_data->ulCntData))
635 hr = S_OK;
636 break;
639 LeaveCriticalSection(&This->lock);
641 if (hr == S_FALSE)
643 while (TRUE)
645 __TRY
647 hr = IrotIsRunning(get_irot_handle(), moniker_data);
649 __EXCEPT(rpc_filter)
651 hr = HRESULT_FROM_WIN32(GetExceptionCode());
653 __ENDTRY
654 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
656 if (start_rpcss())
657 continue;
659 break;
663 HeapFree(GetProcessHeap(), 0, moniker_data);
665 return hr;
668 /***********************************************************************
669 * RunningObjectTable_GetObject
671 * PARAMS
672 * pmkObjectName [in] Pointer to the moniker on the object
673 * ppunkObject [out] variable that receives the IUnknown interface pointer
675 static HRESULT WINAPI
676 RunningObjectTableImpl_GetObject( IRunningObjectTable* iface,
677 IMoniker *pmkObjectName, IUnknown **ppunkObject)
679 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
680 MonikerComparisonData *moniker_data;
681 InterfaceData *object = NULL;
682 IrotCookie cookie;
683 HRESULT hr;
684 struct rot_entry *rot_entry;
686 TRACE("(%p,%p,%p)\n",This,pmkObjectName,ppunkObject);
688 if (ppunkObject == NULL)
689 return E_POINTER;
691 *ppunkObject = NULL;
693 hr = reduce_moniker(pmkObjectName, NULL, &pmkObjectName);
694 if (FAILED(hr))
695 return hr;
696 hr = get_moniker_comparison_data(pmkObjectName, &moniker_data);
697 IMoniker_Release(pmkObjectName);
698 if (hr != S_OK)
699 return hr;
701 EnterCriticalSection(&This->lock);
702 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, struct rot_entry, entry)
704 if ((rot_entry->moniker_data->ulCntData == moniker_data->ulCntData) &&
705 !memcmp(moniker_data->abData, rot_entry->moniker_data->abData, moniker_data->ulCntData))
707 IStream *pStream;
708 hr = create_stream_on_mip_ro(rot_entry->object, &pStream);
709 if (hr == S_OK)
711 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)ppunkObject);
712 IStream_Release(pStream);
715 LeaveCriticalSection(&This->lock);
716 HeapFree(GetProcessHeap(), 0, moniker_data);
718 return hr;
721 LeaveCriticalSection(&This->lock);
723 TRACE("moniker unavailable locally, calling SCM\n");
725 while (TRUE)
727 __TRY
729 hr = IrotGetObject(get_irot_handle(), moniker_data, &object, &cookie);
731 __EXCEPT(rpc_filter)
733 hr = HRESULT_FROM_WIN32(GetExceptionCode());
735 __ENDTRY
736 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
738 if (start_rpcss())
739 continue;
741 break;
744 if (SUCCEEDED(hr))
746 IStream *pStream;
747 hr = create_stream_on_mip_ro(object, &pStream);
748 if (hr == S_OK)
750 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)ppunkObject);
751 IStream_Release(pStream);
754 else
755 WARN("Moniker unavailable, IrotGetObject returned 0x%08x\n", hr);
757 HeapFree(GetProcessHeap(), 0, moniker_data);
759 return hr;
762 /***********************************************************************
763 * RunningObjectTable_NoteChangeTime
765 * PARAMS
766 * dwRegister [in] Value identifying registration being updated
767 * pfiletime [in] Pointer to structure containing object's last change time
769 static HRESULT WINAPI
770 RunningObjectTableImpl_NoteChangeTime(IRunningObjectTable* iface,
771 DWORD dwRegister, FILETIME *pfiletime)
773 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
774 struct rot_entry *rot_entry;
775 HRESULT hr = E_INVALIDARG;
777 TRACE("(%p,%d,%p)\n",This,dwRegister,pfiletime);
779 EnterCriticalSection(&This->lock);
780 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, struct rot_entry, entry)
782 if (rot_entry->cookie == dwRegister)
784 rot_entry->last_modified = *pfiletime;
785 LeaveCriticalSection(&This->lock);
787 while (TRUE)
789 __TRY
791 hr = IrotNoteChangeTime(get_irot_handle(), dwRegister, pfiletime);
793 __EXCEPT(rpc_filter)
795 hr = HRESULT_FROM_WIN32(GetExceptionCode());
797 __ENDTRY
798 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
800 if (start_rpcss())
801 continue;
803 break;
806 goto done;
809 LeaveCriticalSection(&This->lock);
811 done:
812 TRACE("-- 0x08%x\n", hr);
813 return hr;
816 /***********************************************************************
817 * RunningObjectTable_GetTimeOfLastChange
819 * PARAMS
820 * pmkObjectName [in] moniker of the object whose status is desired
821 * pfiletime [out] structure that receives object's last change time
823 static HRESULT WINAPI
824 RunningObjectTableImpl_GetTimeOfLastChange(IRunningObjectTable* iface,
825 IMoniker *pmkObjectName, FILETIME *pfiletime)
827 HRESULT hr = MK_E_UNAVAILABLE;
828 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
829 MonikerComparisonData *moniker_data;
830 const struct rot_entry *rot_entry;
832 TRACE("(%p,%p,%p)\n",This,pmkObjectName,pfiletime);
834 if (pmkObjectName==NULL || pfiletime==NULL)
835 return E_INVALIDARG;
837 hr = reduce_moniker(pmkObjectName, NULL, &pmkObjectName);
838 if (FAILED(hr))
839 return hr;
840 hr = get_moniker_comparison_data(pmkObjectName, &moniker_data);
841 IMoniker_Release(pmkObjectName);
842 if (hr != S_OK)
843 return hr;
845 hr = MK_E_UNAVAILABLE;
847 EnterCriticalSection(&This->lock);
848 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, const struct rot_entry, entry)
850 if ((rot_entry->moniker_data->ulCntData == moniker_data->ulCntData) &&
851 !memcmp(moniker_data->abData, rot_entry->moniker_data->abData, moniker_data->ulCntData))
853 *pfiletime = rot_entry->last_modified;
854 hr = S_OK;
855 break;
858 LeaveCriticalSection(&This->lock);
860 if (hr != S_OK)
862 while (TRUE)
864 __TRY
866 hr = IrotGetTimeOfLastChange(get_irot_handle(), moniker_data, pfiletime);
868 __EXCEPT(rpc_filter)
870 hr = HRESULT_FROM_WIN32(GetExceptionCode());
872 __ENDTRY
873 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
875 if (start_rpcss())
876 continue;
878 break;
882 HeapFree(GetProcessHeap(), 0, moniker_data);
884 TRACE("-- 0x%08x\n", hr);
885 return hr;
888 /***********************************************************************
889 * RunningObjectTable_EnumRunning
891 * PARAMS
892 * ppenumMoniker [out] receives the IEnumMoniker interface pointer
894 static HRESULT WINAPI
895 RunningObjectTableImpl_EnumRunning(IRunningObjectTable* iface,
896 IEnumMoniker **ppenumMoniker)
898 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
899 InterfaceList *interface_list = NULL;
900 HRESULT hr;
902 TRACE("(%p, %p)\n", This, ppenumMoniker);
904 *ppenumMoniker = NULL;
906 while (TRUE)
908 __TRY
910 hr = IrotEnumRunning(get_irot_handle(), &interface_list);
912 __EXCEPT(rpc_filter)
914 hr = HRESULT_FROM_WIN32(GetExceptionCode());
916 __ENDTRY
917 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
919 if (start_rpcss())
920 continue;
922 break;
925 if (SUCCEEDED(hr))
926 hr = EnumMonikerImpl_CreateEnumROTMoniker(interface_list,
927 0, ppenumMoniker);
929 return hr;
932 /* Virtual function table for the IRunningObjectTable class. */
933 static const IRunningObjectTableVtbl VT_RunningObjectTableImpl =
935 RunningObjectTableImpl_QueryInterface,
936 RunningObjectTableImpl_AddRef,
937 RunningObjectTableImpl_Release,
938 RunningObjectTableImpl_Register,
939 RunningObjectTableImpl_Revoke,
940 RunningObjectTableImpl_IsRunning,
941 RunningObjectTableImpl_GetObject,
942 RunningObjectTableImpl_NoteChangeTime,
943 RunningObjectTableImpl_GetTimeOfLastChange,
944 RunningObjectTableImpl_EnumRunning
947 /***********************************************************************
948 * RunningObjectTable_Initialize
950 HRESULT WINAPI RunningObjectTableImpl_Initialize(void)
952 TRACE("\n");
954 /* create the unique instance of the RunningObjectTableImpl structure */
955 runningObjectTableInstance = HeapAlloc(GetProcessHeap(), 0, sizeof(RunningObjectTableImpl));
957 if (!runningObjectTableInstance)
958 return E_OUTOFMEMORY;
960 /* initialize the virtual table function */
961 runningObjectTableInstance->lpVtbl = &VT_RunningObjectTableImpl;
963 /* the initial reference is set to "1" so that it isn't destroyed after its
964 * first use until the process is destroyed, as the running object table is
965 * a process-wide cache of a global table */
966 runningObjectTableInstance->ref = 1;
968 list_init(&runningObjectTableInstance->rot);
969 InitializeCriticalSection(&runningObjectTableInstance->lock);
970 DEBUG_SET_CRITSEC_NAME(&runningObjectTableInstance->lock, "RunningObjectTableImpl.lock");
972 return S_OK;
975 /***********************************************************************
976 * RunningObjectTable_UnInitialize
978 HRESULT WINAPI RunningObjectTableImpl_UnInitialize(void)
980 TRACE("\n");
982 if (runningObjectTableInstance==NULL)
983 return E_POINTER;
985 RunningObjectTableImpl_Release((IRunningObjectTable*)runningObjectTableInstance);
987 RunningObjectTableImpl_Destroy();
989 return S_OK;
992 /***********************************************************************
993 * GetRunningObjectTable (OLE32.@)
995 * Retrieves the global running object table.
997 * PARAMS
998 * reserved [I] Reserved. Set to 0.
999 * pprot [O] Address that receives the pointer to the running object table.
1001 * RETURNS
1002 * Success: S_OK.
1003 * Failure: Any HRESULT code.
1005 HRESULT WINAPI
1006 GetRunningObjectTable(DWORD reserved, LPRUNNINGOBJECTTABLE *pprot)
1008 IID riid=IID_IRunningObjectTable;
1009 HRESULT res;
1011 TRACE("()\n");
1013 if (reserved!=0)
1014 return E_UNEXPECTED;
1016 if(runningObjectTableInstance==NULL)
1017 return CO_E_NOTINITIALIZED;
1019 res = IRunningObjectTable_QueryInterface((IRunningObjectTable*)runningObjectTableInstance,&riid,(void**)pprot);
1021 return res;
1024 static HRESULT get_moniker_for_progid_display_name(LPBC pbc,
1025 LPCOLESTR szDisplayName,
1026 LPDWORD pchEaten,
1027 LPMONIKER *ppmk)
1029 CLSID clsid;
1030 HRESULT hr;
1031 LPWSTR progid;
1032 LPCWSTR start = szDisplayName;
1033 LPCWSTR end;
1034 int len;
1035 IMoniker *class_moniker;
1037 if (*start == '@')
1038 start++;
1040 /* find end delimiter */
1041 for (end = start; *end; end++)
1042 if (*end == ':')
1043 break;
1045 len = end - start;
1047 /* must start with '@' or have a ':' somewhere and mustn't be one character
1048 * long (since that looks like an absolute path) */
1049 if (((start == szDisplayName) && (*end == '\0')) || (len <= 1))
1050 return MK_E_SYNTAX;
1052 progid = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1053 if (progid)
1055 memcpy(progid, start, len * sizeof(WCHAR));
1056 progid[len] = '\0';
1058 hr = CLSIDFromProgID(progid, &clsid);
1059 HeapFree(GetProcessHeap(), 0, progid);
1060 if (FAILED(hr))
1061 return MK_E_SYNTAX;
1063 hr = CreateClassMoniker(&clsid, &class_moniker);
1064 if (SUCCEEDED(hr))
1066 IParseDisplayName *pdn;
1067 hr = IMoniker_BindToObject(class_moniker, pbc, NULL,
1068 &IID_IParseDisplayName, (void **)&pdn);
1069 /* fallback to using IClassFactory to get IParseDisplayName -
1070 * adsldp.dll depends on this */
1071 if (FAILED(hr))
1073 IClassFactory *pcf;
1074 hr = IMoniker_BindToObject(class_moniker, pbc, NULL,
1075 &IID_IClassFactory, (void **)&pcf);
1076 if (SUCCEEDED(hr))
1078 hr = IClassFactory_CreateInstance(pcf, NULL,
1079 &IID_IParseDisplayName,
1080 (void **)&pdn);
1081 IClassFactory_Release(pcf);
1084 IMoniker_Release(class_moniker);
1085 if (SUCCEEDED(hr))
1087 hr = IParseDisplayName_ParseDisplayName(pdn, pbc,
1088 (LPOLESTR)szDisplayName,
1089 pchEaten, ppmk);
1090 IParseDisplayName_Release(pdn);
1093 return hr;
1096 /******************************************************************************
1097 * MkParseDisplayName [OLE32.@]
1099 HRESULT WINAPI MkParseDisplayName(LPBC pbc, LPCOLESTR szDisplayName,
1100 LPDWORD pchEaten, LPMONIKER *ppmk)
1102 HRESULT hr = MK_E_SYNTAX;
1103 static const WCHAR wszClsidColon[] = {'c','l','s','i','d',':'};
1104 IMoniker *moniker;
1105 DWORD chEaten;
1107 TRACE("(%p, %s, %p, %p)\n", pbc, debugstr_w(szDisplayName), pchEaten, ppmk);
1109 if (!(IsValidInterface((LPUNKNOWN) pbc)))
1110 return E_INVALIDARG;
1112 *pchEaten = 0;
1113 *ppmk = NULL;
1115 if (!strncmpiW(szDisplayName, wszClsidColon, sizeof(wszClsidColon)/sizeof(wszClsidColon[0])))
1117 hr = ClassMoniker_CreateFromDisplayName(pbc, szDisplayName, &chEaten, &moniker);
1118 if (FAILED(hr) && (hr != MK_E_SYNTAX))
1119 return hr;
1121 else
1123 hr = get_moniker_for_progid_display_name(pbc, szDisplayName, &chEaten, &moniker);
1124 if (FAILED(hr) && (hr != MK_E_SYNTAX))
1125 return hr;
1128 if (FAILED(hr))
1130 hr = FileMoniker_CreateFromDisplayName(pbc, szDisplayName, &chEaten, &moniker);
1131 if (FAILED(hr) && (hr != MK_E_SYNTAX))
1132 return hr;
1135 if (SUCCEEDED(hr))
1137 while (TRUE)
1139 IMoniker *next_moniker;
1140 *pchEaten += chEaten;
1141 szDisplayName += chEaten;
1142 if (!*szDisplayName)
1144 *ppmk = moniker;
1145 return S_OK;
1147 chEaten = 0;
1148 hr = IMoniker_ParseDisplayName(moniker, pbc, NULL,
1149 (LPOLESTR)szDisplayName, &chEaten,
1150 &next_moniker);
1151 IMoniker_Release(moniker);
1152 if (FAILED(hr))
1154 *pchEaten = 0;
1155 break;
1157 moniker = next_moniker;
1161 return hr;
1164 /***********************************************************************
1165 * GetClassFile (OLE32.@)
1167 * Retrieves the class ID associated with the given filename.
1169 * PARAMS
1170 * filePathName [I] Filename to retrieve the class ID for.
1171 * pclsid [O] Address that receives the class ID for the file.
1173 * RETURNS
1174 * Success: S_OK.
1175 * Failure: Any HRESULT code.
1177 HRESULT WINAPI GetClassFile(LPCOLESTR filePathName,CLSID *pclsid)
1179 IStorage *pstg=0;
1180 HRESULT res;
1181 int nbElm, length, i;
1182 LONG sizeProgId;
1183 LPOLESTR *pathDec=0,absFile=0,progId=0;
1184 LPWSTR extension;
1185 static const WCHAR bkslashW[] = {'\\',0};
1186 static const WCHAR dotW[] = {'.',0};
1188 TRACE("%s, %p\n", debugstr_w(filePathName), pclsid);
1190 /* if the file contain a storage object the return the CLSID written by IStorage_SetClass method*/
1191 if((StgIsStorageFile(filePathName))==S_OK){
1193 res=StgOpenStorage(filePathName,NULL,STGM_READ | STGM_SHARE_DENY_WRITE,NULL,0,&pstg);
1195 if (SUCCEEDED(res))
1196 res=ReadClassStg(pstg,pclsid);
1198 IStorage_Release(pstg);
1200 return res;
1202 /* If the file is not a storage object then attempt to match various bits in the file against a
1203 pattern in the registry. This case is not frequently used, so I present only the pseudocode for
1204 this case.
1206 for(i=0;i<nFileTypes;i++)
1208 for(i=0;j<nPatternsForType;j++){
1210 PATTERN pat;
1211 HANDLE hFile;
1213 pat=ReadPatternFromRegistry(i,j);
1214 hFile=CreateFileW(filePathName,,,,,,hFile);
1215 SetFilePosition(hFile,pat.offset);
1216 ReadFile(hFile,buf,pat.size,&r,NULL);
1217 if (memcmp(buf&pat.mask,pat.pattern.pat.size)==0){
1219 *pclsid=ReadCLSIDFromRegistry(i);
1220 return S_OK;
1225 /* if the above strategies fail then search for the extension key in the registry */
1227 /* get the last element (absolute file) in the path name */
1228 nbElm=FileMonikerImpl_DecomposePath(filePathName,&pathDec);
1229 absFile=pathDec[nbElm-1];
1231 /* failed if the path represents a directory and not an absolute file name*/
1232 if (!lstrcmpW(absFile, bkslashW))
1233 return MK_E_INVALIDEXTENSION;
1235 /* get the extension of the file */
1236 extension = NULL;
1237 length=lstrlenW(absFile);
1238 for(i = length-1; (i >= 0) && *(extension = &absFile[i]) != '.'; i--)
1239 /* nothing */;
1241 if (!extension || !lstrcmpW(extension, dotW))
1242 return MK_E_INVALIDEXTENSION;
1244 res=RegQueryValueW(HKEY_CLASSES_ROOT, extension, NULL, &sizeProgId);
1246 /* get the progId associated to the extension */
1247 progId = CoTaskMemAlloc(sizeProgId);
1248 res = RegQueryValueW(HKEY_CLASSES_ROOT, extension, progId, &sizeProgId);
1250 if (res==ERROR_SUCCESS)
1251 /* return the clsid associated to the progId */
1252 res= CLSIDFromProgID(progId,pclsid);
1254 for(i=0; pathDec[i]!=NULL;i++)
1255 CoTaskMemFree(pathDec[i]);
1256 CoTaskMemFree(pathDec);
1258 CoTaskMemFree(progId);
1260 if (res==ERROR_SUCCESS)
1261 return res;
1263 return MK_E_INVALIDEXTENSION;
1266 /***********************************************************************
1267 * EnumMoniker_QueryInterface
1269 static HRESULT WINAPI EnumMonikerImpl_QueryInterface(IEnumMoniker* iface,REFIID riid,void** ppvObject)
1271 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1273 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
1275 /* validate arguments */
1276 if (ppvObject == NULL)
1277 return E_INVALIDARG;
1279 *ppvObject = NULL;
1281 if (IsEqualIID(&IID_IUnknown, riid))
1282 *ppvObject = This;
1283 else
1284 if (IsEqualIID(&IID_IEnumMoniker, riid))
1285 *ppvObject = This;
1287 if ((*ppvObject)==NULL)
1288 return E_NOINTERFACE;
1290 IEnumMoniker_AddRef(iface);
1292 return S_OK;
1295 /***********************************************************************
1296 * EnumMoniker_AddRef
1298 static ULONG WINAPI EnumMonikerImpl_AddRef(IEnumMoniker* iface)
1300 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1302 TRACE("(%p)\n",This);
1304 return InterlockedIncrement(&This->ref);
1307 /***********************************************************************
1308 * EnumMoniker_release
1310 static ULONG WINAPI EnumMonikerImpl_Release(IEnumMoniker* iface)
1312 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1313 ULONG ref;
1315 TRACE("(%p)\n",This);
1317 ref = InterlockedDecrement(&This->ref);
1319 /* uninitialize rot structure if there's no more reference to it*/
1320 if (ref == 0)
1322 ULONG i;
1324 TRACE("(%p) Deleting\n",This);
1326 for (i = 0; i < This->moniker_list->size; i++)
1327 HeapFree(GetProcessHeap(), 0, This->moniker_list->interfaces[i]);
1328 HeapFree(GetProcessHeap(), 0, This->moniker_list);
1329 HeapFree(GetProcessHeap(), 0, This);
1332 return ref;
1334 /***********************************************************************
1335 * EnumMoniker_Next
1337 static HRESULT WINAPI EnumMonikerImpl_Next(IEnumMoniker* iface, ULONG celt, IMoniker** rgelt, ULONG * pceltFetched)
1339 ULONG i;
1340 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1341 HRESULT hr = S_OK;
1343 TRACE("(%p) TabCurrentPos %d Tablastindx %d\n", This, This->pos, This->moniker_list->size);
1345 /* retrieve the requested number of moniker from the current position */
1346 for(i = 0; (This->pos < This->moniker_list->size) && (i < celt); i++)
1348 IStream *stream;
1349 hr = create_stream_on_mip_ro(This->moniker_list->interfaces[This->pos++], &stream);
1350 if (hr != S_OK) break;
1351 hr = CoUnmarshalInterface(stream, &IID_IMoniker, (void **)&rgelt[i]);
1352 IStream_Release(stream);
1353 if (hr != S_OK) break;
1356 if (pceltFetched != NULL)
1357 *pceltFetched= i;
1359 if (hr != S_OK)
1360 return hr;
1362 if (i == celt)
1363 return S_OK;
1364 else
1365 return S_FALSE;
1369 /***********************************************************************
1370 * EnumMoniker_Skip
1372 static HRESULT WINAPI EnumMonikerImpl_Skip(IEnumMoniker* iface, ULONG celt)
1374 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1376 TRACE("(%p)\n",This);
1378 if (This->pos + celt >= This->moniker_list->size)
1379 return S_FALSE;
1381 This->pos += celt;
1383 return S_OK;
1386 /***********************************************************************
1387 * EnumMoniker_Reset
1389 static HRESULT WINAPI EnumMonikerImpl_Reset(IEnumMoniker* iface)
1391 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1393 This->pos = 0; /* set back to start of list */
1395 TRACE("(%p)\n",This);
1397 return S_OK;
1400 /***********************************************************************
1401 * EnumMoniker_Clone
1403 static HRESULT WINAPI EnumMonikerImpl_Clone(IEnumMoniker* iface, IEnumMoniker ** ppenum)
1405 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1406 InterfaceList *moniker_list;
1407 ULONG i;
1409 TRACE("(%p)\n",This);
1411 *ppenum = NULL;
1413 moniker_list = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(InterfaceList, interfaces[This->moniker_list->size]));
1414 if (!moniker_list)
1415 return E_OUTOFMEMORY;
1417 moniker_list->size = This->moniker_list->size;
1418 for (i = 0; i < This->moniker_list->size; i++)
1420 SIZE_T size = FIELD_OFFSET(InterfaceData, abData[This->moniker_list->interfaces[i]->ulCntData]);
1421 moniker_list->interfaces[i] = HeapAlloc(GetProcessHeap(), 0, size);
1422 if (!moniker_list->interfaces[i])
1424 ULONG end = i;
1425 for (i = 0; i < end; i++)
1426 HeapFree(GetProcessHeap(), 0, moniker_list->interfaces[i]);
1427 HeapFree(GetProcessHeap(), 0, moniker_list);
1428 return E_OUTOFMEMORY;
1430 memcpy(moniker_list->interfaces[i], This->moniker_list->interfaces[i], size);
1433 /* copy the enum structure */
1434 return EnumMonikerImpl_CreateEnumROTMoniker(moniker_list, This->pos, ppenum);
1437 /* Virtual function table for the IEnumMoniker class. */
1438 static const IEnumMonikerVtbl VT_EnumMonikerImpl =
1440 EnumMonikerImpl_QueryInterface,
1441 EnumMonikerImpl_AddRef,
1442 EnumMonikerImpl_Release,
1443 EnumMonikerImpl_Next,
1444 EnumMonikerImpl_Skip,
1445 EnumMonikerImpl_Reset,
1446 EnumMonikerImpl_Clone
1449 /***********************************************************************
1450 * EnumMonikerImpl_CreateEnumROTMoniker
1451 * Used by EnumRunning to create the structure and EnumClone
1452 * to copy the structure
1454 static HRESULT EnumMonikerImpl_CreateEnumROTMoniker(InterfaceList *moniker_list,
1455 ULONG current_pos,
1456 IEnumMoniker **ppenumMoniker)
1458 EnumMonikerImpl* This = NULL;
1460 if (!ppenumMoniker)
1461 return E_INVALIDARG;
1463 This = HeapAlloc(GetProcessHeap(), 0, sizeof(EnumMonikerImpl));
1464 if (!This) return E_OUTOFMEMORY;
1466 TRACE("(%p)\n", This);
1468 /* initialize the virtual table function */
1469 This->lpVtbl = &VT_EnumMonikerImpl;
1471 /* the initial reference is set to "1" */
1472 This->ref = 1; /* set the ref count to one */
1473 This->pos = current_pos; /* Set the list start posn */
1474 This->moniker_list = moniker_list;
1476 *ppenumMoniker = (IEnumMoniker*)This;
1478 return S_OK;
1482 /* Shared implementation of moniker marshaler based on saving and loading of
1483 * monikers */
1485 typedef struct MonikerMarshal
1487 const IUnknownVtbl *lpVtbl;
1488 const IMarshalVtbl *lpVtblMarshal;
1490 LONG ref;
1491 IMoniker *moniker;
1492 } MonikerMarshal;
1494 static inline MonikerMarshal *impl_from_IMarshal( IMarshal *iface )
1496 return (MonikerMarshal *)((char*)iface - FIELD_OFFSET(MonikerMarshal, lpVtblMarshal));
1499 static HRESULT WINAPI MonikerMarshalInner_QueryInterface(IUnknown *iface, REFIID riid, LPVOID *ppv)
1501 MonikerMarshal *This = (MonikerMarshal *)iface;
1502 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
1503 *ppv = NULL;
1504 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IMarshal, riid))
1506 *ppv = &This->lpVtblMarshal;
1507 IUnknown_AddRef((IUnknown *)&This->lpVtblMarshal);
1508 return S_OK;
1510 FIXME("No interface for %s\n", debugstr_guid(riid));
1511 return E_NOINTERFACE;
1514 static ULONG WINAPI MonikerMarshalInner_AddRef(IUnknown *iface)
1516 MonikerMarshal *This = (MonikerMarshal *)iface;
1517 return InterlockedIncrement(&This->ref);
1520 static ULONG WINAPI MonikerMarshalInner_Release(IUnknown *iface)
1522 MonikerMarshal *This = (MonikerMarshal *)iface;
1523 ULONG ref = InterlockedDecrement(&This->ref);
1525 if (!ref) HeapFree(GetProcessHeap(), 0, This);
1526 return ref;
1529 static const IUnknownVtbl VT_MonikerMarshalInner =
1531 MonikerMarshalInner_QueryInterface,
1532 MonikerMarshalInner_AddRef,
1533 MonikerMarshalInner_Release
1536 static HRESULT WINAPI MonikerMarshal_QueryInterface(IMarshal *iface, REFIID riid, LPVOID *ppv)
1538 MonikerMarshal *This = impl_from_IMarshal(iface);
1539 return IMoniker_QueryInterface(This->moniker, riid, ppv);
1542 static ULONG WINAPI MonikerMarshal_AddRef(IMarshal *iface)
1544 MonikerMarshal *This = impl_from_IMarshal(iface);
1545 return IMoniker_AddRef(This->moniker);
1548 static ULONG WINAPI MonikerMarshal_Release(IMarshal *iface)
1550 MonikerMarshal *This = impl_from_IMarshal(iface);
1551 return IMoniker_Release(This->moniker);
1554 static HRESULT WINAPI MonikerMarshal_GetUnmarshalClass(
1555 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
1556 void* pvDestContext, DWORD mshlflags, CLSID* pCid)
1558 MonikerMarshal *This = impl_from_IMarshal(iface);
1560 TRACE("(%s, %p, %x, %p, %x, %p)\n", debugstr_guid(riid), pv,
1561 dwDestContext, pvDestContext, mshlflags, pCid);
1563 return IMoniker_GetClassID(This->moniker, pCid);
1566 static HRESULT WINAPI MonikerMarshal_GetMarshalSizeMax(
1567 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
1568 void* pvDestContext, DWORD mshlflags, DWORD* pSize)
1570 MonikerMarshal *This = impl_from_IMarshal(iface);
1571 HRESULT hr;
1572 ULARGE_INTEGER size;
1574 TRACE("(%s, %p, %x, %p, %x, %p)\n", debugstr_guid(riid), pv,
1575 dwDestContext, pvDestContext, mshlflags, pSize);
1577 hr = IMoniker_GetSizeMax(This->moniker, &size);
1578 if (hr == S_OK)
1579 *pSize = (DWORD)size.QuadPart;
1580 return hr;
1583 static HRESULT WINAPI MonikerMarshal_MarshalInterface(LPMARSHAL iface, IStream *pStm,
1584 REFIID riid, void* pv, DWORD dwDestContext,
1585 void* pvDestContext, DWORD mshlflags)
1587 MonikerMarshal *This = impl_from_IMarshal(iface);
1589 TRACE("(%p, %s, %p, %x, %p, %x)\n", pStm, debugstr_guid(riid), pv,
1590 dwDestContext, pvDestContext, mshlflags);
1592 return IMoniker_Save(This->moniker, pStm, FALSE);
1595 static HRESULT WINAPI MonikerMarshal_UnmarshalInterface(LPMARSHAL iface, IStream *pStm, REFIID riid, void **ppv)
1597 MonikerMarshal *This = impl_from_IMarshal(iface);
1598 HRESULT hr;
1600 TRACE("(%p, %s, %p)\n", pStm, debugstr_guid(riid), ppv);
1602 hr = IMoniker_Load(This->moniker, pStm);
1603 if (hr == S_OK)
1604 hr = IMoniker_QueryInterface(This->moniker, riid, ppv);
1605 return hr;
1608 static HRESULT WINAPI MonikerMarshal_ReleaseMarshalData(LPMARSHAL iface, IStream *pStm)
1610 TRACE("()\n");
1611 /* can't release a state-based marshal as nothing on server side to
1612 * release */
1613 return S_OK;
1616 static HRESULT WINAPI MonikerMarshal_DisconnectObject(LPMARSHAL iface, DWORD dwReserved)
1618 TRACE("()\n");
1619 /* can't disconnect a state-based marshal as nothing on server side to
1620 * disconnect from */
1621 return S_OK;
1624 static const IMarshalVtbl VT_MonikerMarshal =
1626 MonikerMarshal_QueryInterface,
1627 MonikerMarshal_AddRef,
1628 MonikerMarshal_Release,
1629 MonikerMarshal_GetUnmarshalClass,
1630 MonikerMarshal_GetMarshalSizeMax,
1631 MonikerMarshal_MarshalInterface,
1632 MonikerMarshal_UnmarshalInterface,
1633 MonikerMarshal_ReleaseMarshalData,
1634 MonikerMarshal_DisconnectObject
1637 HRESULT MonikerMarshal_Create(IMoniker *inner, IUnknown **outer)
1639 MonikerMarshal *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1640 if (!This) return E_OUTOFMEMORY;
1642 This->lpVtbl = &VT_MonikerMarshalInner;
1643 This->lpVtblMarshal = &VT_MonikerMarshal;
1644 This->ref = 1;
1645 This->moniker = inner;
1647 *outer = (IUnknown *)&This->lpVtbl;
1648 return S_OK;
1651 void * __RPC_USER MIDL_user_allocate(SIZE_T size)
1653 return HeapAlloc(GetProcessHeap(), 0, size);
1656 void __RPC_USER MIDL_user_free(void *p)
1658 HeapFree(GetProcessHeap(), 0, p);