ntoskrnl.exe: Change calling conventions for Interlocked* functions.
[wine/multimedia.git] / dlls / ole32 / moniker.c
blob413d11033fde93b7f009c915a65deda3efff7914
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
23 * TODO:
24 * - IRunningObjectTable should work interprocess, but currently doesn't.
25 * Native (on Win2k at least) uses an undocumented RPC interface, IROT, to
26 * communicate with RPCSS which contains the table of marshalled data.
29 #include <stdarg.h>
30 #include <string.h>
32 #define COBJMACROS
34 #include "winerror.h"
35 #include "windef.h"
36 #include "winbase.h"
37 #include "winuser.h"
38 #include "wtypes.h"
39 #include "ole2.h"
41 #include "wine/list.h"
42 #include "wine/debug.h"
43 #include "wine/unicode.h"
44 #include "wine/exception.h"
46 #include "compobj_private.h"
47 #include "moniker.h"
48 #include "irot.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(ole);
52 /* see MSDN docs for IROTData::GetComparisonData, which states what this
53 * constant is
55 #define MAX_COMPARISON_DATA 2048
57 static LONG WINAPI rpc_filter(EXCEPTION_POINTERS *eptr)
59 return I_RpcExceptionFilter(eptr->ExceptionRecord->ExceptionCode);
62 /* define the structure of the running object table elements */
63 struct rot_entry
65 struct list entry;
66 InterfaceData* object; /* marshaled running object*/
67 MonikerComparisonData* moniker_data; /* moniker comparison data that identifies this object */
68 DWORD cookie; /* cookie identifying this object */
69 FILETIME last_modified;
70 IrotContextHandle ctxt_handle;
73 /* define the RunningObjectTableImpl structure */
74 typedef struct RunningObjectTableImpl
76 const IRunningObjectTableVtbl *lpVtbl;
77 LONG ref;
79 struct list rot; /* list of ROT entries */
80 CRITICAL_SECTION lock;
81 } RunningObjectTableImpl;
83 static RunningObjectTableImpl* runningObjectTableInstance = NULL;
84 static IrotHandle irot_handle;
86 /* define the EnumMonikerImpl structure */
87 typedef struct EnumMonikerImpl
89 const IEnumMonikerVtbl *lpVtbl;
90 LONG ref;
92 InterfaceList *moniker_list;
93 ULONG pos;
94 } EnumMonikerImpl;
97 /* IEnumMoniker Local functions*/
98 static HRESULT WINAPI EnumMonikerImpl_CreateEnumROTMoniker(InterfaceList *moniker_list,
99 ULONG pos, IEnumMoniker **ppenumMoniker);
101 static IrotHandle get_irot_handle(void)
103 if (!irot_handle)
105 RPC_STATUS status;
106 RPC_WSTR binding;
107 IrotHandle new_handle;
108 unsigned short ncacn_np[] = IROT_PROTSEQ;
109 unsigned short endpoint[] = IROT_ENDPOINT;
110 status = RpcStringBindingComposeW(NULL, ncacn_np, NULL, endpoint, NULL, &binding);
111 if (status == RPC_S_OK)
113 status = RpcBindingFromStringBindingW(binding, &new_handle);
114 RpcStringFreeW(&binding);
116 if (status != RPC_S_OK)
117 return NULL;
118 if (InterlockedCompareExchangePointer(&irot_handle, new_handle, NULL))
119 /* another thread beat us to it */
120 RpcBindingFree(&new_handle);
122 return irot_handle;
125 static BOOL start_rpcss(void)
127 PROCESS_INFORMATION pi;
128 STARTUPINFOW si;
129 static WCHAR cmd[6];
130 static const WCHAR rpcss[] = {'r','p','c','s','s',0};
131 BOOL rslt;
133 TRACE("\n");
135 ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
136 ZeroMemory(&si, sizeof(STARTUPINFOA));
137 si.cb = sizeof(STARTUPINFOA);
139 memcpy(cmd, rpcss, sizeof(rpcss));
141 rslt = CreateProcessW(
142 NULL, /* executable */
143 cmd, /* command line */
144 NULL, /* process security attributes */
145 NULL, /* primary thread security attributes */
146 FALSE, /* inherit handles */
147 0, /* creation flags */
148 NULL, /* use parent's environment */
149 NULL, /* use parent's current directory */
150 &si, /* STARTUPINFO pointer */
151 &pi /* PROCESS_INFORMATION */
154 if (rslt)
156 CloseHandle(pi.hProcess);
157 CloseHandle(pi.hThread);
158 Sleep(100);
161 return rslt;
164 static HRESULT create_stream_on_mip_ro(const InterfaceData *mip, IStream **stream)
166 HGLOBAL hglobal = GlobalAlloc(0, mip->ulCntData);
167 void *pv = GlobalLock(hglobal);
168 memcpy(pv, mip->abData, mip->ulCntData);
169 GlobalUnlock(hglobal);
170 return CreateStreamOnHGlobal(hglobal, TRUE, stream);
173 static inline void rot_entry_delete(struct rot_entry *rot_entry)
175 if (rot_entry->cookie)
177 InterfaceData *object = NULL;
178 InterfaceData *moniker = NULL;
179 __TRY
181 IrotRevoke(get_irot_handle(), rot_entry->cookie,
182 &rot_entry->ctxt_handle, &object, &moniker);
184 __EXCEPT(rpc_filter)
187 __ENDTRY
188 MIDL_user_free(object);
189 if (moniker)
191 IStream *stream;
192 HRESULT hr;
193 hr = create_stream_on_mip_ro(moniker, &stream);
194 if (hr == S_OK)
196 CoReleaseMarshalData(stream);
197 IUnknown_Release(stream);
200 MIDL_user_free(moniker);
202 if (rot_entry->object)
204 IStream *stream;
205 HRESULT hr;
206 hr = create_stream_on_mip_ro(rot_entry->object, &stream);
207 if (hr == S_OK)
209 CoReleaseMarshalData(stream);
210 IUnknown_Release(stream);
213 HeapFree(GetProcessHeap(), 0, rot_entry->object);
214 HeapFree(GetProcessHeap(), 0, rot_entry->moniker_data);
215 HeapFree(GetProcessHeap(), 0, rot_entry);
218 /* moniker_data must be freed with HeapFree when no longer in use */
219 static HRESULT get_moniker_comparison_data(IMoniker *pMoniker, MonikerComparisonData **moniker_data)
221 HRESULT hr;
222 IROTData *pROTData = NULL;
223 hr = IMoniker_QueryInterface(pMoniker, &IID_IROTData, (void *)&pROTData);
224 if (SUCCEEDED(hr))
226 ULONG size = MAX_COMPARISON_DATA;
227 *moniker_data = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(MonikerComparisonData, abData[size]));
228 if (!*moniker_data)
230 IROTData_Release(pROTData);
231 return E_OUTOFMEMORY;
233 hr = IROTData_GetComparisonData(pROTData, (*moniker_data)->abData, size, &size);
234 IROTData_Release(pROTData);
235 if (hr != S_OK)
237 ERR("Failed to copy comparison data into buffer, hr = 0x%08x\n", hr);
238 HeapFree(GetProcessHeap(), 0, *moniker_data);
239 return hr;
241 (*moniker_data)->ulCntData = size;
243 else
245 IBindCtx *pbc;
246 LPOLESTR pszDisplayName;
247 CLSID clsid;
248 int len;
250 TRACE("generating comparison data from display name\n");
252 hr = CreateBindCtx(0, &pbc);
253 if (FAILED(hr))
254 return hr;
255 hr = IMoniker_GetDisplayName(pMoniker, pbc, NULL, &pszDisplayName);
256 IBindCtx_Release(pbc);
257 if (FAILED(hr))
258 return hr;
259 hr = IMoniker_GetClassID(pMoniker, &clsid);
260 if (FAILED(hr))
262 CoTaskMemFree(pszDisplayName);
263 return hr;
266 len = strlenW(pszDisplayName);
267 *moniker_data = HeapAlloc(GetProcessHeap(), 0,
268 FIELD_OFFSET(MonikerComparisonData, abData[sizeof(CLSID) + (len+1)*sizeof(WCHAR)]));
269 if (!*moniker_data)
271 CoTaskMemFree(pszDisplayName);
272 return E_OUTOFMEMORY;
274 (*moniker_data)->ulCntData = sizeof(CLSID) + (len+1)*sizeof(WCHAR);
276 memcpy(&(*moniker_data)->abData[0], &clsid, sizeof(clsid));
277 memcpy(&(*moniker_data)->abData[sizeof(clsid)], pszDisplayName, (len+1)*sizeof(WCHAR));
278 CoTaskMemFree(pszDisplayName);
280 return S_OK;
283 static HRESULT reduce_moniker(IMoniker *pmk, IBindCtx *pbc, IMoniker **pmkReduced)
285 IBindCtx *pbcNew = NULL;
286 HRESULT hr;
287 if (!pbc)
289 hr = CreateBindCtx(0, &pbcNew);
290 if (FAILED(hr))
291 return hr;
292 pbc = pbcNew;
294 hr = IMoniker_Reduce(pmk, pbc, MKRREDUCE_ALL, NULL, pmkReduced);
295 if (FAILED(hr))
296 ERR("reducing moniker failed with error 0x%08x\n", hr);
297 if (pbcNew) IBindCtx_Release(pbcNew);
298 return hr;
301 /***********************************************************************
302 * RunningObjectTable_QueryInterface
304 static HRESULT WINAPI
305 RunningObjectTableImpl_QueryInterface(IRunningObjectTable* iface,
306 REFIID riid,void** ppvObject)
308 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
310 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
312 /* validate arguments */
314 if (ppvObject==0)
315 return E_INVALIDARG;
317 *ppvObject = 0;
319 if (IsEqualIID(&IID_IUnknown, riid) ||
320 IsEqualIID(&IID_IRunningObjectTable, riid))
321 *ppvObject = (IRunningObjectTable*)This;
323 if ((*ppvObject)==0)
324 return E_NOINTERFACE;
326 IRunningObjectTable_AddRef(iface);
328 return S_OK;
331 /***********************************************************************
332 * RunningObjectTable_AddRef
334 static ULONG WINAPI
335 RunningObjectTableImpl_AddRef(IRunningObjectTable* iface)
337 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
339 TRACE("(%p)\n",This);
341 return InterlockedIncrement(&This->ref);
344 /***********************************************************************
345 * RunningObjectTable_Initialize
347 static HRESULT WINAPI
348 RunningObjectTableImpl_Destroy(void)
350 struct list *cursor, *cursor2;
351 IrotHandle old_handle;
353 TRACE("()\n");
355 if (runningObjectTableInstance==NULL)
356 return E_INVALIDARG;
358 /* free the ROT table memory */
359 LIST_FOR_EACH_SAFE(cursor, cursor2, &runningObjectTableInstance->rot)
361 struct rot_entry *rot_entry = LIST_ENTRY(cursor, struct rot_entry, entry);
362 list_remove(&rot_entry->entry);
363 rot_entry_delete(rot_entry);
366 DEBUG_CLEAR_CRITSEC_NAME(&runningObjectTableInstance->lock);
367 DeleteCriticalSection(&runningObjectTableInstance->lock);
369 /* free the ROT structure memory */
370 HeapFree(GetProcessHeap(),0,runningObjectTableInstance);
371 runningObjectTableInstance = NULL;
373 old_handle = irot_handle;
374 irot_handle = NULL;
375 if (old_handle)
376 RpcBindingFree(&old_handle);
378 return S_OK;
381 /***********************************************************************
382 * RunningObjectTable_Release
384 static ULONG WINAPI
385 RunningObjectTableImpl_Release(IRunningObjectTable* iface)
387 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
388 ULONG ref;
390 TRACE("(%p)\n",This);
392 ref = InterlockedDecrement(&This->ref);
394 /* uninitialize ROT structure if there's no more references to it */
395 if (ref == 0)
397 struct list *cursor, *cursor2;
398 LIST_FOR_EACH_SAFE(cursor, cursor2, &This->rot)
400 struct rot_entry *rot_entry = LIST_ENTRY(cursor, struct rot_entry, entry);
401 list_remove(&rot_entry->entry);
402 rot_entry_delete(rot_entry);
404 /* RunningObjectTable data structure will be not destroyed here ! the destruction will be done only
405 * when RunningObjectTableImpl_UnInitialize function is called
409 return ref;
412 /***********************************************************************
413 * RunningObjectTable_Register
415 * PARAMS
416 * grfFlags [in] Registration options
417 * punkObject [in] the object being registered
418 * pmkObjectName [in] the moniker of the object being registered
419 * pdwRegister [in] the value identifying the registration
421 static HRESULT WINAPI
422 RunningObjectTableImpl_Register(IRunningObjectTable* iface, DWORD grfFlags,
423 IUnknown *punkObject, IMoniker *pmkObjectName, DWORD *pdwRegister)
425 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
426 struct rot_entry *rot_entry;
427 HRESULT hr = S_OK;
428 IStream *pStream = NULL;
429 DWORD mshlflags;
430 IBindCtx *pbc;
431 InterfaceData *moniker = NULL;
433 TRACE("(%p,%d,%p,%p,%p)\n",This,grfFlags,punkObject,pmkObjectName,pdwRegister);
435 if (grfFlags & ~(ROTFLAGS_REGISTRATIONKEEPSALIVE|ROTFLAGS_ALLOWANYCLIENT))
437 ERR("Invalid grfFlags: 0x%08x\n", grfFlags & ~(ROTFLAGS_REGISTRATIONKEEPSALIVE|ROTFLAGS_ALLOWANYCLIENT));
438 return E_INVALIDARG;
441 if (punkObject==NULL || pmkObjectName==NULL || pdwRegister==NULL)
442 return E_INVALIDARG;
444 rot_entry = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*rot_entry));
445 if (!rot_entry)
446 return E_OUTOFMEMORY;
448 /* marshal object */
449 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
450 if (hr != S_OK)
452 rot_entry_delete(rot_entry);
453 return hr;
455 mshlflags = (grfFlags & ROTFLAGS_REGISTRATIONKEEPSALIVE) ? MSHLFLAGS_TABLESTRONG : MSHLFLAGS_TABLEWEAK;
456 hr = CoMarshalInterface(pStream, &IID_IUnknown, punkObject, MSHCTX_LOCAL | MSHCTX_NOSHAREDMEM, NULL, mshlflags);
457 /* FIXME: a cleaner way would be to create an IStream class that writes
458 * directly to an MInterfacePointer */
459 if (hr == S_OK)
461 HGLOBAL hglobal;
462 hr = GetHGlobalFromStream(pStream, &hglobal);
463 if (hr == S_OK)
465 SIZE_T size = GlobalSize(hglobal);
466 const void *pv = GlobalLock(hglobal);
467 rot_entry->object = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(MInterfacePointer, abData[size]));
468 rot_entry->object->ulCntData = size;
469 memcpy(&rot_entry->object->abData, pv, size);
470 GlobalUnlock(hglobal);
473 IStream_Release(pStream);
474 if (hr != S_OK)
476 rot_entry_delete(rot_entry);
477 return hr;
480 hr = CreateBindCtx(0, &pbc);
481 if (FAILED(hr))
483 rot_entry_delete(rot_entry);
484 return hr;
487 hr = reduce_moniker(pmkObjectName, pbc, &pmkObjectName);
488 if (FAILED(hr))
490 rot_entry_delete(rot_entry);
491 IBindCtx_Release(pbc);
492 return hr;
495 hr = IMoniker_GetTimeOfLastChange(pmkObjectName, pbc, NULL,
496 &rot_entry->last_modified);
497 IBindCtx_Release(pbc);
498 if (FAILED(hr))
500 CoFileTimeNow(&rot_entry->last_modified);
501 hr = S_OK;
504 hr = get_moniker_comparison_data(pmkObjectName,
505 &rot_entry->moniker_data);
506 if (hr != S_OK)
508 rot_entry_delete(rot_entry);
509 IMoniker_Release(pmkObjectName);
510 return hr;
513 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
514 if (hr != S_OK)
516 rot_entry_delete(rot_entry);
517 IMoniker_Release(pmkObjectName);
518 return hr;
520 /* marshal moniker */
521 hr = CoMarshalInterface(pStream, &IID_IMoniker, (IUnknown *)pmkObjectName,
522 MSHCTX_LOCAL | MSHCTX_NOSHAREDMEM, NULL, MSHLFLAGS_TABLESTRONG);
523 /* FIXME: a cleaner way would be to create an IStream class that writes
524 * directly to an MInterfacePointer */
525 if (hr == S_OK)
527 HGLOBAL hglobal;
528 hr = GetHGlobalFromStream(pStream, &hglobal);
529 if (hr == S_OK)
531 SIZE_T size = GlobalSize(hglobal);
532 const void *pv = GlobalLock(hglobal);
533 moniker = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(InterfaceData, abData[size]));
534 moniker->ulCntData = size;
535 memcpy(&moniker->abData, pv, size);
536 GlobalUnlock(hglobal);
539 IStream_Release(pStream);
540 IMoniker_Release(pmkObjectName);
541 if (hr != S_OK)
543 HeapFree(GetProcessHeap(), 0, moniker);
544 rot_entry_delete(rot_entry);
545 return hr;
549 while (TRUE)
551 __TRY
553 hr = IrotRegister(get_irot_handle(), rot_entry->moniker_data,
554 rot_entry->object, moniker,
555 &rot_entry->last_modified, grfFlags,
556 &rot_entry->cookie, &rot_entry->ctxt_handle);
558 __EXCEPT(rpc_filter)
560 hr = HRESULT_FROM_WIN32(GetExceptionCode());
562 __ENDTRY
563 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
565 if (start_rpcss())
566 continue;
568 break;
570 HeapFree(GetProcessHeap(), 0, moniker);
571 if (FAILED(hr))
573 rot_entry_delete(rot_entry);
574 return hr;
577 /* gives a registration identifier to the registered object*/
578 *pdwRegister = rot_entry->cookie;
580 EnterCriticalSection(&This->lock);
581 list_add_tail(&This->rot, &rot_entry->entry);
582 LeaveCriticalSection(&This->lock);
584 return hr;
587 /***********************************************************************
588 * RunningObjectTable_Revoke
590 * PARAMS
591 * dwRegister [in] Value identifying registration to be revoked
593 static HRESULT WINAPI
594 RunningObjectTableImpl_Revoke( IRunningObjectTable* iface, DWORD dwRegister)
596 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
597 struct rot_entry *rot_entry;
599 TRACE("(%p,%d)\n",This,dwRegister);
601 EnterCriticalSection(&This->lock);
602 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, struct rot_entry, entry)
604 if (rot_entry->cookie == dwRegister)
606 list_remove(&rot_entry->entry);
607 LeaveCriticalSection(&This->lock);
609 rot_entry_delete(rot_entry);
610 return S_OK;
613 LeaveCriticalSection(&This->lock);
615 return E_INVALIDARG;
618 /***********************************************************************
619 * RunningObjectTable_IsRunning
621 * PARAMS
622 * pmkObjectName [in] moniker of the object whose status is desired
624 static HRESULT WINAPI
625 RunningObjectTableImpl_IsRunning( IRunningObjectTable* iface, IMoniker *pmkObjectName)
627 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
628 MonikerComparisonData *moniker_data;
629 HRESULT hr;
630 const struct rot_entry *rot_entry;
632 TRACE("(%p,%p)\n",This,pmkObjectName);
634 hr = reduce_moniker(pmkObjectName, NULL, &pmkObjectName);
635 if (FAILED(hr))
636 return hr;
637 hr = get_moniker_comparison_data(pmkObjectName, &moniker_data);
638 IMoniker_Release(pmkObjectName);
639 if (hr != S_OK)
640 return hr;
642 hr = S_FALSE;
643 EnterCriticalSection(&This->lock);
644 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, const struct rot_entry, entry)
646 if ((rot_entry->moniker_data->ulCntData == moniker_data->ulCntData) &&
647 !memcmp(&moniker_data->abData, &rot_entry->moniker_data->abData, moniker_data->ulCntData))
649 hr = S_OK;
650 break;
653 LeaveCriticalSection(&This->lock);
655 if (hr == S_FALSE)
657 while (TRUE)
659 __TRY
661 hr = IrotIsRunning(get_irot_handle(), moniker_data);
663 __EXCEPT(rpc_filter)
665 hr = HRESULT_FROM_WIN32(GetExceptionCode());
667 __ENDTRY
668 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
670 if (start_rpcss())
671 continue;
673 break;
677 HeapFree(GetProcessHeap(), 0, moniker_data);
679 return hr;
682 /***********************************************************************
683 * RunningObjectTable_GetObject
685 * PARAMS
686 * pmkObjectName [in] Pointer to the moniker on the object
687 * ppunkObject [out] variable that receives the IUnknown interface pointer
689 static HRESULT WINAPI
690 RunningObjectTableImpl_GetObject( IRunningObjectTable* iface,
691 IMoniker *pmkObjectName, IUnknown **ppunkObject)
693 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
694 MonikerComparisonData *moniker_data;
695 InterfaceData *object = NULL;
696 IrotCookie cookie;
697 HRESULT hr;
698 struct rot_entry *rot_entry;
700 TRACE("(%p,%p,%p)\n",This,pmkObjectName,ppunkObject);
702 if (ppunkObject == NULL)
703 return E_POINTER;
705 *ppunkObject = NULL;
707 hr = reduce_moniker(pmkObjectName, NULL, &pmkObjectName);
708 if (FAILED(hr))
709 return hr;
710 hr = get_moniker_comparison_data(pmkObjectName, &moniker_data);
711 IMoniker_Release(pmkObjectName);
712 if (hr != S_OK)
713 return hr;
715 EnterCriticalSection(&This->lock);
716 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, struct rot_entry, entry)
718 if ((rot_entry->moniker_data->ulCntData == moniker_data->ulCntData) &&
719 !memcmp(&moniker_data->abData, &rot_entry->moniker_data->abData, moniker_data->ulCntData))
721 IStream *pStream;
722 hr = create_stream_on_mip_ro(rot_entry->object, &pStream);
723 if (hr == S_OK)
725 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)ppunkObject);
726 IStream_Release(pStream);
729 LeaveCriticalSection(&This->lock);
730 HeapFree(GetProcessHeap(), 0, moniker_data);
732 return hr;
735 LeaveCriticalSection(&This->lock);
737 TRACE("moniker unavailable locally, calling SCM\n");
739 while (TRUE)
741 __TRY
743 hr = IrotGetObject(get_irot_handle(), moniker_data, &object, &cookie);
745 __EXCEPT(rpc_filter)
747 hr = HRESULT_FROM_WIN32(GetExceptionCode());
749 __ENDTRY
750 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
752 if (start_rpcss())
753 continue;
755 break;
758 if (SUCCEEDED(hr))
760 IStream *pStream;
761 hr = create_stream_on_mip_ro(object, &pStream);
762 if (hr == S_OK)
764 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)ppunkObject);
765 IStream_Release(pStream);
768 else
769 WARN("Moniker unavailable, IrotGetObject returned 0x%08x\n", hr);
771 HeapFree(GetProcessHeap(), 0, moniker_data);
773 return hr;
776 /***********************************************************************
777 * RunningObjectTable_NoteChangeTime
779 * PARAMS
780 * dwRegister [in] Value identifying registration being updated
781 * pfiletime [in] Pointer to structure containing object's last change time
783 static HRESULT WINAPI
784 RunningObjectTableImpl_NoteChangeTime(IRunningObjectTable* iface,
785 DWORD dwRegister, FILETIME *pfiletime)
787 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
788 struct rot_entry *rot_entry;
789 HRESULT hr = E_INVALIDARG;
791 TRACE("(%p,%d,%p)\n",This,dwRegister,pfiletime);
793 EnterCriticalSection(&This->lock);
794 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, struct rot_entry, entry)
796 if (rot_entry->cookie == dwRegister)
798 rot_entry->last_modified = *pfiletime;
799 LeaveCriticalSection(&This->lock);
801 while (TRUE)
803 __TRY
805 hr = IrotNoteChangeTime(get_irot_handle(), dwRegister, pfiletime);
807 __EXCEPT(rpc_filter)
809 hr = HRESULT_FROM_WIN32(GetExceptionCode());
811 __ENDTRY
812 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
814 if (start_rpcss())
815 continue;
817 break;
820 goto done;
823 LeaveCriticalSection(&This->lock);
825 done:
826 TRACE("-- 0x08%x\n", hr);
827 return hr;
830 /***********************************************************************
831 * RunningObjectTable_GetTimeOfLastChange
833 * PARAMS
834 * pmkObjectName [in] moniker of the object whose status is desired
835 * pfiletime [out] structure that receives object's last change time
837 static HRESULT WINAPI
838 RunningObjectTableImpl_GetTimeOfLastChange(IRunningObjectTable* iface,
839 IMoniker *pmkObjectName, FILETIME *pfiletime)
841 HRESULT hr = MK_E_UNAVAILABLE;
842 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
843 MonikerComparisonData *moniker_data;
844 const struct rot_entry *rot_entry;
846 TRACE("(%p,%p,%p)\n",This,pmkObjectName,pfiletime);
848 if (pmkObjectName==NULL || pfiletime==NULL)
849 return E_INVALIDARG;
851 hr = reduce_moniker(pmkObjectName, NULL, &pmkObjectName);
852 if (FAILED(hr))
853 return hr;
854 hr = get_moniker_comparison_data(pmkObjectName, &moniker_data);
855 IMoniker_Release(pmkObjectName);
856 if (hr != S_OK)
857 return hr;
859 hr = MK_E_UNAVAILABLE;
861 EnterCriticalSection(&This->lock);
862 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, const struct rot_entry, entry)
864 if ((rot_entry->moniker_data->ulCntData == moniker_data->ulCntData) &&
865 !memcmp(&moniker_data->abData, &rot_entry->moniker_data->abData, moniker_data->ulCntData))
867 *pfiletime = rot_entry->last_modified;
868 hr = S_OK;
869 break;
872 LeaveCriticalSection(&This->lock);
874 if (hr != S_OK)
876 while (TRUE)
878 __TRY
880 hr = IrotGetTimeOfLastChange(get_irot_handle(), moniker_data, pfiletime);
882 __EXCEPT(rpc_filter)
884 hr = HRESULT_FROM_WIN32(GetExceptionCode());
886 __ENDTRY
887 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
889 if (start_rpcss())
890 continue;
892 break;
896 HeapFree(GetProcessHeap(), 0, moniker_data);
898 TRACE("-- 0x%08x\n", hr);
899 return hr;
902 /***********************************************************************
903 * RunningObjectTable_EnumRunning
905 * PARAMS
906 * ppenumMoniker [out] receives the IEnumMoniker interface pointer
908 static HRESULT WINAPI
909 RunningObjectTableImpl_EnumRunning(IRunningObjectTable* iface,
910 IEnumMoniker **ppenumMoniker)
912 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
913 InterfaceList *interface_list = NULL;
914 HRESULT hr;
916 TRACE("(%p, %p)\n", This, ppenumMoniker);
918 *ppenumMoniker = NULL;
920 while (TRUE)
922 __TRY
924 hr = IrotEnumRunning(get_irot_handle(), &interface_list);
926 __EXCEPT(rpc_filter)
928 hr = HRESULT_FROM_WIN32(GetExceptionCode());
930 __ENDTRY
931 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
933 if (start_rpcss())
934 continue;
936 break;
939 if (SUCCEEDED(hr))
940 hr = EnumMonikerImpl_CreateEnumROTMoniker(interface_list,
941 0, ppenumMoniker);
943 return hr;
946 /* Virtual function table for the IRunningObjectTable class. */
947 static const IRunningObjectTableVtbl VT_RunningObjectTableImpl =
949 RunningObjectTableImpl_QueryInterface,
950 RunningObjectTableImpl_AddRef,
951 RunningObjectTableImpl_Release,
952 RunningObjectTableImpl_Register,
953 RunningObjectTableImpl_Revoke,
954 RunningObjectTableImpl_IsRunning,
955 RunningObjectTableImpl_GetObject,
956 RunningObjectTableImpl_NoteChangeTime,
957 RunningObjectTableImpl_GetTimeOfLastChange,
958 RunningObjectTableImpl_EnumRunning
961 /***********************************************************************
962 * RunningObjectTable_Initialize
964 HRESULT WINAPI RunningObjectTableImpl_Initialize(void)
966 TRACE("\n");
968 /* create the unique instance of the RunningObjectTableImpl structure */
969 runningObjectTableInstance = HeapAlloc(GetProcessHeap(), 0, sizeof(RunningObjectTableImpl));
971 if (!runningObjectTableInstance)
972 return E_OUTOFMEMORY;
974 /* initialize the virtual table function */
975 runningObjectTableInstance->lpVtbl = &VT_RunningObjectTableImpl;
977 /* the initial reference is set to "1" ! because if set to "0" it will be not practis when */
978 /* the ROT referred many times not in the same time (all the objects in the ROT will */
979 /* be removed every time the ROT is removed ) */
980 runningObjectTableInstance->ref = 1;
982 list_init(&runningObjectTableInstance->rot);
983 InitializeCriticalSection(&runningObjectTableInstance->lock);
984 DEBUG_SET_CRITSEC_NAME(&runningObjectTableInstance->lock, "RunningObjectTableImpl.lock");
986 return S_OK;
989 /***********************************************************************
990 * RunningObjectTable_UnInitialize
992 HRESULT WINAPI RunningObjectTableImpl_UnInitialize(void)
994 TRACE("\n");
996 if (runningObjectTableInstance==NULL)
997 return E_POINTER;
999 RunningObjectTableImpl_Release((IRunningObjectTable*)runningObjectTableInstance);
1001 RunningObjectTableImpl_Destroy();
1003 return S_OK;
1006 /***********************************************************************
1007 * GetRunningObjectTable (OLE32.@)
1009 * Retrieves the global running object table.
1011 * PARAMS
1012 * reserved [I] Reserved. Set to 0.
1013 * pprot [O] Address that receives the pointer to the running object table.
1015 * RETURNS
1016 * Success: S_OK.
1017 * Failure: Any HRESULT code.
1019 HRESULT WINAPI
1020 GetRunningObjectTable(DWORD reserved, LPRUNNINGOBJECTTABLE *pprot)
1022 IID riid=IID_IRunningObjectTable;
1023 HRESULT res;
1025 TRACE("()\n");
1027 if (reserved!=0)
1028 return E_UNEXPECTED;
1030 if(runningObjectTableInstance==NULL)
1031 return CO_E_NOTINITIALIZED;
1033 res = IRunningObjectTable_QueryInterface((IRunningObjectTable*)runningObjectTableInstance,&riid,(void**)pprot);
1035 return res;
1038 static HRESULT get_moniker_for_progid_display_name(LPBC pbc,
1039 LPCOLESTR szDisplayName,
1040 LPDWORD pchEaten,
1041 LPMONIKER *ppmk)
1043 CLSID clsid;
1044 HRESULT hr;
1045 LPWSTR progid;
1046 LPCWSTR start = szDisplayName;
1047 LPCWSTR end;
1048 int len;
1049 IMoniker *class_moniker;
1051 if (*start == '@')
1052 start++;
1054 /* find end delimiter */
1055 for (end = start; *end; end++)
1056 if (*end == ':')
1057 break;
1059 len = end - start;
1061 /* must start with '@' or have a ':' somewhere and mustn't be one character
1062 * long (since that looks like an absolute path) */
1063 if (((start == szDisplayName) && (*end == '\0')) || (len <= 1))
1064 return MK_E_SYNTAX;
1066 progid = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1067 if (progid)
1069 memcpy(progid, start, len * sizeof(WCHAR));
1070 progid[len] = '\0';
1072 hr = CLSIDFromProgID(progid, &clsid);
1073 HeapFree(GetProcessHeap(), 0, progid);
1074 if (FAILED(hr))
1075 return MK_E_SYNTAX;
1077 hr = CreateClassMoniker(&clsid, &class_moniker);
1078 if (SUCCEEDED(hr))
1080 IParseDisplayName *pdn;
1081 hr = IMoniker_BindToObject(class_moniker, pbc, NULL,
1082 &IID_IParseDisplayName, (void **)&pdn);
1083 /* fallback to using IClassFactory to get IParseDisplayName -
1084 * adsldp.dll depends on this */
1085 if (FAILED(hr))
1087 IClassFactory *pcf;
1088 hr = IMoniker_BindToObject(class_moniker, pbc, NULL,
1089 &IID_IClassFactory, (void **)&pcf);
1090 if (SUCCEEDED(hr))
1092 hr = IClassFactory_CreateInstance(pcf, NULL,
1093 &IID_IParseDisplayName,
1094 (void **)&pdn);
1095 IClassFactory_Release(pcf);
1098 IMoniker_Release(class_moniker);
1099 if (SUCCEEDED(hr))
1101 hr = IParseDisplayName_ParseDisplayName(pdn, pbc,
1102 (LPOLESTR)szDisplayName,
1103 pchEaten, ppmk);
1104 IParseDisplayName_Release(pdn);
1107 return hr;
1110 /******************************************************************************
1111 * MkParseDisplayName [OLE32.@]
1113 HRESULT WINAPI MkParseDisplayName(LPBC pbc, LPCOLESTR szDisplayName,
1114 LPDWORD pchEaten, LPMONIKER *ppmk)
1116 HRESULT hr = MK_E_SYNTAX;
1117 static const WCHAR wszClsidColon[] = {'c','l','s','i','d',':'};
1118 IMoniker *moniker;
1119 DWORD chEaten;
1121 TRACE("(%p, %s, %p, %p)\n", pbc, debugstr_w(szDisplayName), pchEaten, ppmk);
1123 if (!(IsValidInterface((LPUNKNOWN) pbc)))
1124 return E_INVALIDARG;
1126 *pchEaten = 0;
1127 *ppmk = NULL;
1129 if (!strncmpiW(szDisplayName, wszClsidColon, sizeof(wszClsidColon)/sizeof(wszClsidColon[0])))
1131 hr = ClassMoniker_CreateFromDisplayName(pbc, szDisplayName, &chEaten, &moniker);
1132 if (FAILED(hr) && (hr != MK_E_SYNTAX))
1133 return hr;
1135 else
1137 hr = get_moniker_for_progid_display_name(pbc, szDisplayName, &chEaten, &moniker);
1138 if (FAILED(hr) && (hr != MK_E_SYNTAX))
1139 return hr;
1142 if (FAILED(hr))
1144 hr = FileMoniker_CreateFromDisplayName(pbc, szDisplayName, &chEaten, &moniker);
1145 if (FAILED(hr) && (hr != MK_E_SYNTAX))
1146 return hr;
1149 if (SUCCEEDED(hr))
1151 while (TRUE)
1153 IMoniker *next_moniker;
1154 *pchEaten += chEaten;
1155 szDisplayName += chEaten;
1156 if (!*szDisplayName)
1158 *ppmk = moniker;
1159 return S_OK;
1161 chEaten = 0;
1162 hr = IMoniker_ParseDisplayName(moniker, pbc, NULL,
1163 (LPOLESTR)szDisplayName, &chEaten,
1164 &next_moniker);
1165 IMoniker_Release(moniker);
1166 if (FAILED(hr))
1168 *pchEaten = 0;
1169 break;
1171 moniker = next_moniker;
1175 return hr;
1178 /***********************************************************************
1179 * GetClassFile (OLE32.@)
1181 * Retrieves the class ID associated with the given filename.
1183 * PARAMS
1184 * filePathName [I] Filename to retrieve the class ID for.
1185 * pclsid [O] Address that receives the class ID for the file.
1187 * RETURNS
1188 * Success: S_OK.
1189 * Failure: Any HRESULT code.
1191 HRESULT WINAPI GetClassFile(LPCOLESTR filePathName,CLSID *pclsid)
1193 IStorage *pstg=0;
1194 HRESULT res;
1195 int nbElm, length, i;
1196 LONG sizeProgId;
1197 LPOLESTR *pathDec=0,absFile=0,progId=0;
1198 LPWSTR extension;
1199 static const WCHAR bkslashW[] = {'\\',0};
1200 static const WCHAR dotW[] = {'.',0};
1202 TRACE("%s, %p\n", debugstr_w(filePathName), pclsid);
1204 /* if the file contain a storage object the return the CLSID written by IStorage_SetClass method*/
1205 if((StgIsStorageFile(filePathName))==S_OK){
1207 res=StgOpenStorage(filePathName,NULL,STGM_READ | STGM_SHARE_DENY_WRITE,NULL,0,&pstg);
1209 if (SUCCEEDED(res))
1210 res=ReadClassStg(pstg,pclsid);
1212 IStorage_Release(pstg);
1214 return res;
1216 /* If the file is not a storage object then attempt to match various bits in the file against a
1217 pattern in the registry. This case is not frequently used, so I present only the psodocode for
1218 this case.
1220 for(i=0;i<nFileTypes;i++)
1222 for(i=0;j<nPatternsForType;j++){
1224 PATTERN pat;
1225 HANDLE hFile;
1227 pat=ReadPatternFromRegistry(i,j);
1228 hFile=CreateFileW(filePathName,,,,,,hFile);
1229 SetFilePosition(hFile,pat.offset);
1230 ReadFile(hFile,buf,pat.size,&r,NULL);
1231 if (memcmp(buf&pat.mask,pat.pattern.pat.size)==0){
1233 *pclsid=ReadCLSIDFromRegistry(i);
1234 return S_OK;
1239 /* if the above strategies fail then search for the extension key in the registry */
1241 /* get the last element (absolute file) in the path name */
1242 nbElm=FileMonikerImpl_DecomposePath(filePathName,&pathDec);
1243 absFile=pathDec[nbElm-1];
1245 /* failed if the path represents a directory and not an absolute file name*/
1246 if (!lstrcmpW(absFile, bkslashW))
1247 return MK_E_INVALIDEXTENSION;
1249 /* get the extension of the file */
1250 extension = NULL;
1251 length=lstrlenW(absFile);
1252 for(i = length-1; (i >= 0) && *(extension = &absFile[i]) != '.'; i--)
1253 /* nothing */;
1255 if (!extension || !lstrcmpW(extension, dotW))
1256 return MK_E_INVALIDEXTENSION;
1258 res=RegQueryValueW(HKEY_CLASSES_ROOT, extension, NULL, &sizeProgId);
1260 /* get the progId associated to the extension */
1261 progId = CoTaskMemAlloc(sizeProgId);
1262 res = RegQueryValueW(HKEY_CLASSES_ROOT, extension, progId, &sizeProgId);
1264 if (res==ERROR_SUCCESS)
1265 /* return the clsid associated to the progId */
1266 res= CLSIDFromProgID(progId,pclsid);
1268 for(i=0; pathDec[i]!=NULL;i++)
1269 CoTaskMemFree(pathDec[i]);
1270 CoTaskMemFree(pathDec);
1272 CoTaskMemFree(progId);
1274 if (res==ERROR_SUCCESS)
1275 return res;
1277 return MK_E_INVALIDEXTENSION;
1280 /***********************************************************************
1281 * EnumMoniker_QueryInterface
1283 static HRESULT WINAPI EnumMonikerImpl_QueryInterface(IEnumMoniker* iface,REFIID riid,void** ppvObject)
1285 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1287 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
1289 /* validate arguments */
1290 if (ppvObject == NULL)
1291 return E_INVALIDARG;
1293 *ppvObject = NULL;
1295 if (IsEqualIID(&IID_IUnknown, riid))
1296 *ppvObject = (IEnumMoniker*)This;
1297 else
1298 if (IsEqualIID(&IID_IEnumMoniker, riid))
1299 *ppvObject = (IEnumMoniker*)This;
1301 if ((*ppvObject)==NULL)
1302 return E_NOINTERFACE;
1304 IEnumMoniker_AddRef(iface);
1306 return S_OK;
1309 /***********************************************************************
1310 * EnumMoniker_AddRef
1312 static ULONG WINAPI EnumMonikerImpl_AddRef(IEnumMoniker* iface)
1314 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1316 TRACE("(%p)\n",This);
1318 return InterlockedIncrement(&This->ref);
1321 /***********************************************************************
1322 * EnumMoniker_release
1324 static ULONG WINAPI EnumMonikerImpl_Release(IEnumMoniker* iface)
1326 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1327 ULONG ref;
1329 TRACE("(%p)\n",This);
1331 ref = InterlockedDecrement(&This->ref);
1333 /* uninitialize rot structure if there's no more reference to it*/
1334 if (ref == 0)
1336 ULONG i;
1338 TRACE("(%p) Deleting\n",This);
1340 for (i = 0; i < This->moniker_list->size; i++)
1341 HeapFree(GetProcessHeap(), 0, This->moniker_list->interfaces[i]);
1342 HeapFree(GetProcessHeap(), 0, This->moniker_list);
1343 HeapFree(GetProcessHeap(), 0, This);
1346 return ref;
1348 /***********************************************************************
1349 * EnmumMoniker_Next
1351 static HRESULT WINAPI EnumMonikerImpl_Next(IEnumMoniker* iface, ULONG celt, IMoniker** rgelt, ULONG * pceltFetched)
1353 ULONG i;
1354 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1355 HRESULT hr = S_OK;
1357 TRACE("(%p) TabCurrentPos %d Tablastindx %d\n", This, This->pos, This->moniker_list->size);
1359 /* retrieve the requested number of moniker from the current position */
1360 for(i = 0; (This->pos < This->moniker_list->size) && (i < celt); i++)
1362 IStream *stream;
1363 hr = create_stream_on_mip_ro(This->moniker_list->interfaces[This->pos++], &stream);
1364 if (hr != S_OK) break;
1365 hr = CoUnmarshalInterface(stream, &IID_IMoniker, (void **)&rgelt[i]);
1366 IStream_Release(stream);
1367 if (hr != S_OK) break;
1370 if (pceltFetched != NULL)
1371 *pceltFetched= i;
1373 if (hr != S_OK)
1374 return hr;
1376 if (i == celt)
1377 return S_OK;
1378 else
1379 return S_FALSE;
1383 /***********************************************************************
1384 * EnmumMoniker_Skip
1386 static HRESULT WINAPI EnumMonikerImpl_Skip(IEnumMoniker* iface, ULONG celt)
1388 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1390 TRACE("(%p)\n",This);
1392 if (This->pos + celt >= This->moniker_list->size)
1393 return S_FALSE;
1395 This->pos += celt;
1397 return S_OK;
1400 /***********************************************************************
1401 * EnmumMoniker_Reset
1403 static HRESULT WINAPI EnumMonikerImpl_Reset(IEnumMoniker* iface)
1405 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1407 This->pos = 0; /* set back to start of list */
1409 TRACE("(%p)\n",This);
1411 return S_OK;
1414 /***********************************************************************
1415 * EnmumMoniker_Clone
1417 static HRESULT WINAPI EnumMonikerImpl_Clone(IEnumMoniker* iface, IEnumMoniker ** ppenum)
1419 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1420 InterfaceList *moniker_list;
1421 ULONG i;
1423 TRACE("(%p)\n",This);
1425 *ppenum = NULL;
1427 moniker_list = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(InterfaceList, interfaces[This->moniker_list->size]));
1428 if (!moniker_list)
1429 return E_OUTOFMEMORY;
1431 moniker_list->size = This->moniker_list->size;
1432 for (i = 0; i < This->moniker_list->size; i++)
1434 SIZE_T size = FIELD_OFFSET(InterfaceData, abData[This->moniker_list->interfaces[i]->ulCntData]);
1435 moniker_list->interfaces[i] = HeapAlloc(GetProcessHeap(), 0, size);
1436 if (!moniker_list->interfaces[i])
1438 ULONG end = i;
1439 for (i = 0; i < end; i++)
1440 HeapFree(GetProcessHeap(), 0, moniker_list->interfaces[i]);
1441 HeapFree(GetProcessHeap(), 0, moniker_list);
1442 return E_OUTOFMEMORY;
1444 memcpy(moniker_list->interfaces[i], This->moniker_list->interfaces[i], size);
1447 /* copy the enum structure */
1448 return EnumMonikerImpl_CreateEnumROTMoniker(moniker_list, This->pos, ppenum);
1451 /* Virtual function table for the IEnumMoniker class. */
1452 static const IEnumMonikerVtbl VT_EnumMonikerImpl =
1454 EnumMonikerImpl_QueryInterface,
1455 EnumMonikerImpl_AddRef,
1456 EnumMonikerImpl_Release,
1457 EnumMonikerImpl_Next,
1458 EnumMonikerImpl_Skip,
1459 EnumMonikerImpl_Reset,
1460 EnumMonikerImpl_Clone
1463 /***********************************************************************
1464 * EnumMonikerImpl_CreateEnumROTMoniker
1465 * Used by EnumRunning to create the structure and EnumClone
1466 * to copy the structure
1468 static HRESULT WINAPI EnumMonikerImpl_CreateEnumROTMoniker(InterfaceList *moniker_list,
1469 ULONG current_pos,
1470 IEnumMoniker **ppenumMoniker)
1472 EnumMonikerImpl* This = NULL;
1474 if (!ppenumMoniker)
1475 return E_INVALIDARG;
1477 This = HeapAlloc(GetProcessHeap(), 0, sizeof(EnumMonikerImpl));
1478 if (!This) return E_OUTOFMEMORY;
1480 TRACE("(%p)\n", This);
1482 /* initialize the virtual table function */
1483 This->lpVtbl = &VT_EnumMonikerImpl;
1485 /* the initial reference is set to "1" */
1486 This->ref = 1; /* set the ref count to one */
1487 This->pos = current_pos; /* Set the list start posn */
1488 This->moniker_list = moniker_list;
1490 *ppenumMoniker = (IEnumMoniker*)This;
1492 return S_OK;
1496 /* Shared implementation of moniker marshaler based on saving and loading of
1497 * monikers */
1499 typedef struct MonikerMarshal
1501 const IUnknownVtbl *lpVtbl;
1502 const IMarshalVtbl *lpVtblMarshal;
1504 LONG ref;
1505 IMoniker *moniker;
1506 } MonikerMarshal;
1508 static inline MonikerMarshal *impl_from_IMarshal( IMarshal *iface )
1510 return (MonikerMarshal *)((char*)iface - FIELD_OFFSET(MonikerMarshal, lpVtblMarshal));
1513 static HRESULT WINAPI MonikerMarshalInner_QueryInterface(IUnknown *iface, REFIID riid, LPVOID *ppv)
1515 MonikerMarshal *This = (MonikerMarshal *)iface;
1516 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
1517 *ppv = NULL;
1518 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IMarshal, riid))
1520 *ppv = &This->lpVtblMarshal;
1521 IUnknown_AddRef((IUnknown *)&This->lpVtblMarshal);
1522 return S_OK;
1524 FIXME("No interface for %s\n", debugstr_guid(riid));
1525 return E_NOINTERFACE;
1528 static ULONG WINAPI MonikerMarshalInner_AddRef(IUnknown *iface)
1530 MonikerMarshal *This = (MonikerMarshal *)iface;
1531 return InterlockedIncrement(&This->ref);
1534 static ULONG WINAPI MonikerMarshalInner_Release(IUnknown *iface)
1536 MonikerMarshal *This = (MonikerMarshal *)iface;
1537 ULONG ref = InterlockedDecrement(&This->ref);
1539 if (!ref) HeapFree(GetProcessHeap(), 0, This);
1540 return ref;
1543 static const IUnknownVtbl VT_MonikerMarshalInner =
1545 MonikerMarshalInner_QueryInterface,
1546 MonikerMarshalInner_AddRef,
1547 MonikerMarshalInner_Release
1550 static HRESULT WINAPI MonikerMarshal_QueryInterface(IMarshal *iface, REFIID riid, LPVOID *ppv)
1552 MonikerMarshal *This = impl_from_IMarshal(iface);
1553 return IMoniker_QueryInterface(This->moniker, riid, ppv);
1556 static ULONG WINAPI MonikerMarshal_AddRef(IMarshal *iface)
1558 MonikerMarshal *This = impl_from_IMarshal(iface);
1559 return IMoniker_AddRef(This->moniker);
1562 static ULONG WINAPI MonikerMarshal_Release(IMarshal *iface)
1564 MonikerMarshal *This = impl_from_IMarshal(iface);
1565 return IMoniker_Release(This->moniker);
1568 static HRESULT WINAPI MonikerMarshal_GetUnmarshalClass(
1569 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
1570 void* pvDestContext, DWORD mshlflags, CLSID* pCid)
1572 MonikerMarshal *This = impl_from_IMarshal(iface);
1574 TRACE("(%s, %p, %x, %p, %x, %p)\n", debugstr_guid(riid), pv,
1575 dwDestContext, pvDestContext, mshlflags, pCid);
1577 return IMoniker_GetClassID(This->moniker, pCid);
1580 static HRESULT WINAPI MonikerMarshal_GetMarshalSizeMax(
1581 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
1582 void* pvDestContext, DWORD mshlflags, DWORD* pSize)
1584 MonikerMarshal *This = impl_from_IMarshal(iface);
1585 HRESULT hr;
1586 ULARGE_INTEGER size;
1588 TRACE("(%s, %p, %x, %p, %x, %p)\n", debugstr_guid(riid), pv,
1589 dwDestContext, pvDestContext, mshlflags, pSize);
1591 hr = IMoniker_GetSizeMax(This->moniker, &size);
1592 if (hr == S_OK)
1593 *pSize = (DWORD)size.QuadPart;
1594 return hr;
1597 static HRESULT WINAPI MonikerMarshal_MarshalInterface(LPMARSHAL iface, IStream *pStm,
1598 REFIID riid, void* pv, DWORD dwDestContext,
1599 void* pvDestContext, DWORD mshlflags)
1601 MonikerMarshal *This = impl_from_IMarshal(iface);
1603 TRACE("(%p, %s, %p, %x, %p, %x)\n", pStm, debugstr_guid(riid), pv,
1604 dwDestContext, pvDestContext, mshlflags);
1606 return IMoniker_Save(This->moniker, pStm, FALSE);
1609 static HRESULT WINAPI MonikerMarshal_UnmarshalInterface(LPMARSHAL iface, IStream *pStm, REFIID riid, void **ppv)
1611 MonikerMarshal *This = impl_from_IMarshal(iface);
1612 HRESULT hr;
1614 TRACE("(%p, %s, %p)\n", pStm, debugstr_guid(riid), ppv);
1616 hr = IMoniker_Load(This->moniker, pStm);
1617 if (hr == S_OK)
1618 hr = IMoniker_QueryInterface(This->moniker, riid, ppv);
1619 return hr;
1622 static HRESULT WINAPI MonikerMarshal_ReleaseMarshalData(LPMARSHAL iface, IStream *pStm)
1624 TRACE("()\n");
1625 /* can't release a state-based marshal as nothing on server side to
1626 * release */
1627 return S_OK;
1630 static HRESULT WINAPI MonikerMarshal_DisconnectObject(LPMARSHAL iface, DWORD dwReserved)
1632 TRACE("()\n");
1633 /* can't disconnect a state-based marshal as nothing on server side to
1634 * disconnect from */
1635 return S_OK;
1638 static const IMarshalVtbl VT_MonikerMarshal =
1640 MonikerMarshal_QueryInterface,
1641 MonikerMarshal_AddRef,
1642 MonikerMarshal_Release,
1643 MonikerMarshal_GetUnmarshalClass,
1644 MonikerMarshal_GetMarshalSizeMax,
1645 MonikerMarshal_MarshalInterface,
1646 MonikerMarshal_UnmarshalInterface,
1647 MonikerMarshal_ReleaseMarshalData,
1648 MonikerMarshal_DisconnectObject
1651 HRESULT MonikerMarshal_Create(IMoniker *inner, IUnknown **outer)
1653 MonikerMarshal *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1654 if (!This) return E_OUTOFMEMORY;
1656 This->lpVtbl = &VT_MonikerMarshalInner;
1657 This->lpVtblMarshal = &VT_MonikerMarshal;
1658 This->ref = 1;
1659 This->moniker = inner;
1661 *outer = (IUnknown *)&This->lpVtbl;
1662 return S_OK;
1665 void * __RPC_USER MIDL_user_allocate(size_t size)
1667 return HeapAlloc(GetProcessHeap(), 0, size);
1670 void __RPC_USER MIDL_user_free(void *p)
1672 HeapFree(GetProcessHeap(), 0, p);