msi: Add more tests for MsiSourceListGetInfo.
[wine.git] / dlls / ole32 / moniker.c
blobb1918213f9af31f33727f90f5a5d87e5ddfb560d
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 (http://msdn2.microsoft.com/en-us/library/ms693773.aspx) */
54 #define MAX_COMPARISON_DATA 2048
56 static LONG WINAPI rpc_filter(EXCEPTION_POINTERS *eptr)
58 switch (eptr->ExceptionRecord->ExceptionCode)
60 case EXCEPTION_ACCESS_VIOLATION:
61 case EXCEPTION_ILLEGAL_INSTRUCTION:
62 return EXCEPTION_CONTINUE_SEARCH;
63 default:
64 return EXCEPTION_EXECUTE_HANDLER;
68 /* define the structure of the running object table elements */
69 struct rot_entry
71 struct list entry;
72 InterfaceData* object; /* marshaled running object*/
73 MonikerComparisonData* moniker_data; /* moniker comparison data that identifies this object */
74 DWORD cookie; /* cookie identifying this object */
75 FILETIME last_modified;
76 IrotContextHandle ctxt_handle;
79 /* define the RunningObjectTableImpl structure */
80 typedef struct RunningObjectTableImpl
82 const IRunningObjectTableVtbl *lpVtbl;
83 LONG ref;
85 struct list rot; /* list of ROT entries */
86 CRITICAL_SECTION lock;
87 } RunningObjectTableImpl;
89 static RunningObjectTableImpl* runningObjectTableInstance = NULL;
90 static IrotHandle irot_handle;
92 /* define the EnumMonikerImpl structure */
93 typedef struct EnumMonikerImpl
95 const IEnumMonikerVtbl *lpVtbl;
96 LONG ref;
98 InterfaceList *moniker_list;
99 ULONG pos;
100 } EnumMonikerImpl;
103 /* IEnumMoniker Local functions*/
104 static HRESULT WINAPI EnumMonikerImpl_CreateEnumROTMoniker(InterfaceList *moniker_list,
105 ULONG pos, IEnumMoniker **ppenumMoniker);
107 static IrotHandle get_irot_handle(void)
109 if (!irot_handle)
111 RPC_STATUS status;
112 RPC_WSTR binding;
113 IrotHandle new_handle;
114 unsigned short ncacn_np[] = IROT_PROTSEQ;
115 unsigned short endpoint[] = IROT_ENDPOINT;
116 status = RpcStringBindingComposeW(NULL, ncacn_np, NULL, endpoint, NULL, &binding);
117 if (status == RPC_S_OK)
119 status = RpcBindingFromStringBindingW(binding, &new_handle);
120 RpcStringFreeW(&binding);
122 if (status != RPC_S_OK)
123 return NULL;
124 if (InterlockedCompareExchangePointer(&irot_handle, new_handle, NULL))
125 /* another thread beat us to it */
126 RpcBindingFree(&new_handle);
128 return irot_handle;
131 static BOOL start_rpcss(void)
133 PROCESS_INFORMATION pi;
134 STARTUPINFOW si;
135 static WCHAR cmd[6];
136 static const WCHAR rpcss[] = {'r','p','c','s','s',0};
137 BOOL rslt;
139 TRACE("\n");
141 ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
142 ZeroMemory(&si, sizeof(STARTUPINFOA));
143 si.cb = sizeof(STARTUPINFOA);
145 memcpy(cmd, rpcss, sizeof(rpcss));
147 rslt = CreateProcessW(
148 NULL, /* executable */
149 cmd, /* command line */
150 NULL, /* process security attributes */
151 NULL, /* primary thread security attributes */
152 FALSE, /* inherit handles */
153 0, /* creation flags */
154 NULL, /* use parent's environment */
155 NULL, /* use parent's current directory */
156 &si, /* STARTUPINFO pointer */
157 &pi /* PROCESS_INFORMATION */
160 if (rslt)
162 CloseHandle(pi.hProcess);
163 CloseHandle(pi.hThread);
164 Sleep(100);
167 return rslt;
170 static HRESULT create_stream_on_mip_ro(const InterfaceData *mip, IStream **stream)
172 HGLOBAL hglobal = GlobalAlloc(0, mip->ulCntData);
173 void *pv = GlobalLock(hglobal);
174 memcpy(pv, mip->abData, mip->ulCntData);
175 GlobalUnlock(hglobal);
176 return CreateStreamOnHGlobal(hglobal, TRUE, stream);
179 static inline void rot_entry_delete(struct rot_entry *rot_entry)
181 if (rot_entry->cookie)
183 InterfaceData *object = NULL;
184 InterfaceData *moniker = NULL;
185 __TRY
187 IrotRevoke(get_irot_handle(), rot_entry->cookie,
188 &rot_entry->ctxt_handle, &object, &moniker);
190 __EXCEPT(rpc_filter)
193 __ENDTRY
194 MIDL_user_free(object);
195 if (moniker)
197 IStream *stream;
198 HRESULT hr;
199 hr = create_stream_on_mip_ro(moniker, &stream);
200 if (hr == S_OK)
202 CoReleaseMarshalData(stream);
203 IUnknown_Release(stream);
206 MIDL_user_free(moniker);
208 if (rot_entry->object)
210 IStream *stream;
211 HRESULT hr;
212 hr = create_stream_on_mip_ro(rot_entry->object, &stream);
213 if (hr == S_OK)
215 CoReleaseMarshalData(stream);
216 IUnknown_Release(stream);
219 HeapFree(GetProcessHeap(), 0, rot_entry->object);
220 HeapFree(GetProcessHeap(), 0, rot_entry->moniker_data);
221 HeapFree(GetProcessHeap(), 0, rot_entry);
224 /* moniker_data must be freed with HeapFree when no longer in use */
225 static HRESULT get_moniker_comparison_data(IMoniker *pMoniker, MonikerComparisonData **moniker_data)
227 HRESULT hr;
228 IROTData *pROTData = NULL;
229 hr = IMoniker_QueryInterface(pMoniker, &IID_IROTData, (void *)&pROTData);
230 if (SUCCEEDED(hr))
232 ULONG size = MAX_COMPARISON_DATA;
233 *moniker_data = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(MonikerComparisonData, abData[size]));
234 if (!*moniker_data)
236 IROTData_Release(pROTData);
237 return E_OUTOFMEMORY;
239 hr = IROTData_GetComparisonData(pROTData, (*moniker_data)->abData, size, &size);
240 IROTData_Release(pROTData);
241 if (hr != S_OK)
243 ERR("Failed to copy comparison data into buffer, hr = 0x%08x\n", hr);
244 HeapFree(GetProcessHeap(), 0, *moniker_data);
245 return hr;
247 (*moniker_data)->ulCntData = size;
249 else
251 IBindCtx *pbc;
252 LPOLESTR pszDisplayName;
253 CLSID clsid;
254 int len;
256 TRACE("generating comparison data from display name\n");
258 hr = CreateBindCtx(0, &pbc);
259 if (FAILED(hr))
260 return hr;
261 hr = IMoniker_GetDisplayName(pMoniker, pbc, NULL, &pszDisplayName);
262 IBindCtx_Release(pbc);
263 if (FAILED(hr))
264 return hr;
265 hr = IMoniker_GetClassID(pMoniker, &clsid);
266 if (FAILED(hr))
268 CoTaskMemFree(pszDisplayName);
269 return hr;
272 len = strlenW(pszDisplayName);
273 *moniker_data = HeapAlloc(GetProcessHeap(), 0,
274 FIELD_OFFSET(MonikerComparisonData, abData[sizeof(CLSID) + (len+1)*sizeof(WCHAR)]));
275 if (!*moniker_data)
277 CoTaskMemFree(pszDisplayName);
278 return E_OUTOFMEMORY;
280 (*moniker_data)->ulCntData = sizeof(CLSID) + (len+1)*sizeof(WCHAR);
282 memcpy(&(*moniker_data)->abData[0], &clsid, sizeof(clsid));
283 memcpy(&(*moniker_data)->abData[sizeof(clsid)], pszDisplayName, (len+1)*sizeof(WCHAR));
284 CoTaskMemFree(pszDisplayName);
286 return S_OK;
289 static HRESULT reduce_moniker(IMoniker *pmk, IBindCtx *pbc, IMoniker **pmkReduced)
291 IBindCtx *pbcNew = NULL;
292 HRESULT hr;
293 if (!pbc)
295 hr = CreateBindCtx(0, &pbcNew);
296 if (FAILED(hr))
297 return hr;
298 pbc = pbcNew;
300 hr = IMoniker_Reduce(pmk, pbc, MKRREDUCE_ALL, NULL, pmkReduced);
301 if (FAILED(hr))
302 ERR("reducing moniker failed with error 0x%08x\n", hr);
303 if (pbcNew) IBindCtx_Release(pbcNew);
304 return hr;
307 /***********************************************************************
308 * RunningObjectTable_QueryInterface
310 static HRESULT WINAPI
311 RunningObjectTableImpl_QueryInterface(IRunningObjectTable* iface,
312 REFIID riid,void** ppvObject)
314 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
316 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
318 /* validate arguments */
320 if (ppvObject==0)
321 return E_INVALIDARG;
323 *ppvObject = 0;
325 if (IsEqualIID(&IID_IUnknown, riid) ||
326 IsEqualIID(&IID_IRunningObjectTable, riid))
327 *ppvObject = (IRunningObjectTable*)This;
329 if ((*ppvObject)==0)
330 return E_NOINTERFACE;
332 IRunningObjectTable_AddRef(iface);
334 return S_OK;
337 /***********************************************************************
338 * RunningObjectTable_AddRef
340 static ULONG WINAPI
341 RunningObjectTableImpl_AddRef(IRunningObjectTable* iface)
343 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
345 TRACE("(%p)\n",This);
347 return InterlockedIncrement(&This->ref);
350 /***********************************************************************
351 * RunningObjectTable_Initialize
353 static HRESULT WINAPI
354 RunningObjectTableImpl_Destroy(void)
356 struct list *cursor, *cursor2;
357 IrotHandle old_handle;
359 TRACE("()\n");
361 if (runningObjectTableInstance==NULL)
362 return E_INVALIDARG;
364 /* free the ROT table memory */
365 LIST_FOR_EACH_SAFE(cursor, cursor2, &runningObjectTableInstance->rot)
367 struct rot_entry *rot_entry = LIST_ENTRY(cursor, struct rot_entry, entry);
368 list_remove(&rot_entry->entry);
369 rot_entry_delete(rot_entry);
372 DEBUG_CLEAR_CRITSEC_NAME(&runningObjectTableInstance->lock);
373 DeleteCriticalSection(&runningObjectTableInstance->lock);
375 /* free the ROT structure memory */
376 HeapFree(GetProcessHeap(),0,runningObjectTableInstance);
377 runningObjectTableInstance = NULL;
379 old_handle = irot_handle;
380 irot_handle = NULL;
381 if (old_handle)
382 RpcBindingFree(&old_handle);
384 return S_OK;
387 /***********************************************************************
388 * RunningObjectTable_Release
390 static ULONG WINAPI
391 RunningObjectTableImpl_Release(IRunningObjectTable* iface)
393 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
394 ULONG ref;
396 TRACE("(%p)\n",This);
398 ref = InterlockedDecrement(&This->ref);
400 /* uninitialize ROT structure if there's no more references to it */
401 if (ref == 0)
403 struct list *cursor, *cursor2;
404 LIST_FOR_EACH_SAFE(cursor, cursor2, &This->rot)
406 struct rot_entry *rot_entry = LIST_ENTRY(cursor, struct rot_entry, entry);
407 list_remove(&rot_entry->entry);
408 rot_entry_delete(rot_entry);
410 /* RunningObjectTable data structure will be not destroyed here ! the destruction will be done only
411 * when RunningObjectTableImpl_UnInitialize function is called
415 return ref;
418 /***********************************************************************
419 * RunningObjectTable_Register
421 * PARAMS
422 * grfFlags [in] Registration options
423 * punkObject [in] the object being registered
424 * pmkObjectName [in] the moniker of the object being registered
425 * pdwRegister [in] the value identifying the registration
427 static HRESULT WINAPI
428 RunningObjectTableImpl_Register(IRunningObjectTable* iface, DWORD grfFlags,
429 IUnknown *punkObject, IMoniker *pmkObjectName, DWORD *pdwRegister)
431 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
432 struct rot_entry *rot_entry;
433 HRESULT hr = S_OK;
434 IStream *pStream = NULL;
435 DWORD mshlflags;
436 IBindCtx *pbc;
437 InterfaceData *moniker = NULL;
439 TRACE("(%p,%d,%p,%p,%p)\n",This,grfFlags,punkObject,pmkObjectName,pdwRegister);
441 if (grfFlags & ~(ROTFLAGS_REGISTRATIONKEEPSALIVE|ROTFLAGS_ALLOWANYCLIENT))
443 ERR("Invalid grfFlags: 0x%08x\n", grfFlags & ~(ROTFLAGS_REGISTRATIONKEEPSALIVE|ROTFLAGS_ALLOWANYCLIENT));
444 return E_INVALIDARG;
447 if (punkObject==NULL || pmkObjectName==NULL || pdwRegister==NULL)
448 return E_INVALIDARG;
450 rot_entry = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*rot_entry));
451 if (!rot_entry)
452 return E_OUTOFMEMORY;
454 /* marshal object */
455 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
456 if (hr != S_OK)
458 rot_entry_delete(rot_entry);
459 return hr;
461 mshlflags = (grfFlags & ROTFLAGS_REGISTRATIONKEEPSALIVE) ? MSHLFLAGS_TABLESTRONG : MSHLFLAGS_TABLEWEAK;
462 hr = CoMarshalInterface(pStream, &IID_IUnknown, punkObject, MSHCTX_LOCAL | MSHCTX_NOSHAREDMEM, NULL, mshlflags);
463 /* FIXME: a cleaner way would be to create an IStream class that writes
464 * directly to an MInterfacePointer */
465 if (hr == S_OK)
467 HGLOBAL hglobal;
468 hr = GetHGlobalFromStream(pStream, &hglobal);
469 if (hr == S_OK)
471 SIZE_T size = GlobalSize(hglobal);
472 const void *pv = GlobalLock(hglobal);
473 rot_entry->object = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(MInterfacePointer, abData[size]));
474 rot_entry->object->ulCntData = size;
475 memcpy(&rot_entry->object->abData, pv, size);
476 GlobalUnlock(hglobal);
479 IStream_Release(pStream);
480 if (hr != S_OK)
482 rot_entry_delete(rot_entry);
483 return hr;
486 hr = CreateBindCtx(0, &pbc);
487 if (FAILED(hr))
489 rot_entry_delete(rot_entry);
490 return hr;
493 hr = reduce_moniker(pmkObjectName, pbc, &pmkObjectName);
494 if (FAILED(hr))
496 rot_entry_delete(rot_entry);
497 IBindCtx_Release(pbc);
498 return hr;
501 hr = IMoniker_GetTimeOfLastChange(pmkObjectName, pbc, NULL,
502 &rot_entry->last_modified);
503 IBindCtx_Release(pbc);
504 if (FAILED(hr))
506 CoFileTimeNow(&rot_entry->last_modified);
507 hr = S_OK;
510 hr = get_moniker_comparison_data(pmkObjectName,
511 &rot_entry->moniker_data);
512 if (hr != S_OK)
514 rot_entry_delete(rot_entry);
515 IMoniker_Release(pmkObjectName);
516 return hr;
519 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
520 if (hr != S_OK)
522 rot_entry_delete(rot_entry);
523 IMoniker_Release(pmkObjectName);
524 return hr;
526 /* marshal moniker */
527 hr = CoMarshalInterface(pStream, &IID_IMoniker, (IUnknown *)pmkObjectName,
528 MSHCTX_LOCAL | MSHCTX_NOSHAREDMEM, NULL, MSHLFLAGS_TABLESTRONG);
529 /* FIXME: a cleaner way would be to create an IStream class that writes
530 * directly to an MInterfacePointer */
531 if (hr == S_OK)
533 HGLOBAL hglobal;
534 hr = GetHGlobalFromStream(pStream, &hglobal);
535 if (hr == S_OK)
537 SIZE_T size = GlobalSize(hglobal);
538 const void *pv = GlobalLock(hglobal);
539 moniker = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(InterfaceData, abData[size]));
540 moniker->ulCntData = size;
541 memcpy(&moniker->abData, pv, size);
542 GlobalUnlock(hglobal);
545 IStream_Release(pStream);
546 IMoniker_Release(pmkObjectName);
547 if (hr != S_OK)
549 HeapFree(GetProcessHeap(), 0, moniker);
550 rot_entry_delete(rot_entry);
551 return hr;
555 while (TRUE)
557 __TRY
559 hr = IrotRegister(get_irot_handle(), rot_entry->moniker_data,
560 rot_entry->object, moniker,
561 &rot_entry->last_modified, grfFlags,
562 &rot_entry->cookie, &rot_entry->ctxt_handle);
564 __EXCEPT(rpc_filter)
566 hr = HRESULT_FROM_WIN32(GetExceptionCode());
568 __ENDTRY
569 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
571 if (start_rpcss())
572 continue;
574 break;
576 HeapFree(GetProcessHeap(), 0, moniker);
577 if (FAILED(hr))
579 rot_entry_delete(rot_entry);
580 return hr;
583 /* gives a registration identifier to the registered object*/
584 *pdwRegister = rot_entry->cookie;
586 EnterCriticalSection(&This->lock);
587 list_add_tail(&This->rot, &rot_entry->entry);
588 LeaveCriticalSection(&This->lock);
590 return hr;
593 /***********************************************************************
594 * RunningObjectTable_Revoke
596 * PARAMS
597 * dwRegister [in] Value identifying registration to be revoked
599 static HRESULT WINAPI
600 RunningObjectTableImpl_Revoke( IRunningObjectTable* iface, DWORD dwRegister)
602 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
603 struct rot_entry *rot_entry;
605 TRACE("(%p,%d)\n",This,dwRegister);
607 EnterCriticalSection(&This->lock);
608 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, struct rot_entry, entry)
610 if (rot_entry->cookie == dwRegister)
612 list_remove(&rot_entry->entry);
613 LeaveCriticalSection(&This->lock);
615 rot_entry_delete(rot_entry);
616 return S_OK;
619 LeaveCriticalSection(&This->lock);
621 return E_INVALIDARG;
624 /***********************************************************************
625 * RunningObjectTable_IsRunning
627 * PARAMS
628 * pmkObjectName [in] moniker of the object whose status is desired
630 static HRESULT WINAPI
631 RunningObjectTableImpl_IsRunning( IRunningObjectTable* iface, IMoniker *pmkObjectName)
633 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
634 MonikerComparisonData *moniker_data;
635 HRESULT hr;
636 const struct rot_entry *rot_entry;
638 TRACE("(%p,%p)\n",This,pmkObjectName);
640 hr = reduce_moniker(pmkObjectName, NULL, &pmkObjectName);
641 if (FAILED(hr))
642 return hr;
643 hr = get_moniker_comparison_data(pmkObjectName, &moniker_data);
644 IMoniker_Release(pmkObjectName);
645 if (hr != S_OK)
646 return hr;
648 hr = S_FALSE;
649 EnterCriticalSection(&This->lock);
650 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, const struct rot_entry, entry)
652 if ((rot_entry->moniker_data->ulCntData == moniker_data->ulCntData) &&
653 !memcmp(&moniker_data->abData, &rot_entry->moniker_data->abData, moniker_data->ulCntData))
655 hr = S_OK;
656 break;
659 LeaveCriticalSection(&This->lock);
661 if (hr == S_FALSE)
663 while (TRUE)
665 __TRY
667 hr = IrotIsRunning(get_irot_handle(), moniker_data);
669 __EXCEPT(rpc_filter)
671 hr = HRESULT_FROM_WIN32(GetExceptionCode());
673 __ENDTRY
674 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
676 if (start_rpcss())
677 continue;
679 break;
683 HeapFree(GetProcessHeap(), 0, moniker_data);
685 return hr;
688 /***********************************************************************
689 * RunningObjectTable_GetObject
691 * PARAMS
692 * pmkObjectName [in] Pointer to the moniker on the object
693 * ppunkObject [out] variable that receives the IUnknown interface pointer
695 static HRESULT WINAPI
696 RunningObjectTableImpl_GetObject( IRunningObjectTable* iface,
697 IMoniker *pmkObjectName, IUnknown **ppunkObject)
699 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
700 MonikerComparisonData *moniker_data;
701 InterfaceData *object = NULL;
702 IrotCookie cookie;
703 HRESULT hr;
704 struct rot_entry *rot_entry;
706 TRACE("(%p,%p,%p)\n",This,pmkObjectName,ppunkObject);
708 if (ppunkObject == NULL)
709 return E_POINTER;
711 *ppunkObject = NULL;
713 hr = reduce_moniker(pmkObjectName, NULL, &pmkObjectName);
714 if (FAILED(hr))
715 return hr;
716 hr = get_moniker_comparison_data(pmkObjectName, &moniker_data);
717 IMoniker_Release(pmkObjectName);
718 if (hr != S_OK)
719 return hr;
721 EnterCriticalSection(&This->lock);
722 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, struct rot_entry, entry)
724 if ((rot_entry->moniker_data->ulCntData == moniker_data->ulCntData) &&
725 !memcmp(&moniker_data->abData, &rot_entry->moniker_data->abData, moniker_data->ulCntData))
727 IStream *pStream;
728 hr = create_stream_on_mip_ro(rot_entry->object, &pStream);
729 if (hr == S_OK)
731 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)ppunkObject);
732 IStream_Release(pStream);
735 LeaveCriticalSection(&This->lock);
736 HeapFree(GetProcessHeap(), 0, moniker_data);
738 return hr;
741 LeaveCriticalSection(&This->lock);
743 TRACE("moniker unavailable locally, calling SCM\n");
745 while (TRUE)
747 __TRY
749 hr = IrotGetObject(get_irot_handle(), moniker_data, &object, &cookie);
751 __EXCEPT(rpc_filter)
753 hr = HRESULT_FROM_WIN32(GetExceptionCode());
755 __ENDTRY
756 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
758 if (start_rpcss())
759 continue;
761 break;
764 if (SUCCEEDED(hr))
766 IStream *pStream;
767 hr = create_stream_on_mip_ro(object, &pStream);
768 if (hr == S_OK)
770 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)ppunkObject);
771 IStream_Release(pStream);
774 else
775 WARN("Moniker unavailable, IrotGetObject returned 0x%08x\n", hr);
777 HeapFree(GetProcessHeap(), 0, moniker_data);
779 return hr;
782 /***********************************************************************
783 * RunningObjectTable_NoteChangeTime
785 * PARAMS
786 * dwRegister [in] Value identifying registration being updated
787 * pfiletime [in] Pointer to structure containing object's last change time
789 static HRESULT WINAPI
790 RunningObjectTableImpl_NoteChangeTime(IRunningObjectTable* iface,
791 DWORD dwRegister, FILETIME *pfiletime)
793 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
794 struct rot_entry *rot_entry;
795 HRESULT hr = E_INVALIDARG;
797 TRACE("(%p,%d,%p)\n",This,dwRegister,pfiletime);
799 EnterCriticalSection(&This->lock);
800 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, struct rot_entry, entry)
802 if (rot_entry->cookie == dwRegister)
804 rot_entry->last_modified = *pfiletime;
805 LeaveCriticalSection(&This->lock);
807 while (TRUE)
809 __TRY
811 hr = IrotNoteChangeTime(get_irot_handle(), dwRegister, pfiletime);
813 __EXCEPT(rpc_filter)
815 hr = HRESULT_FROM_WIN32(GetExceptionCode());
817 __ENDTRY
818 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
820 if (start_rpcss())
821 continue;
823 break;
826 goto done;
829 LeaveCriticalSection(&This->lock);
831 done:
832 TRACE("-- 0x08%x\n", hr);
833 return hr;
836 /***********************************************************************
837 * RunningObjectTable_GetTimeOfLastChange
839 * PARAMS
840 * pmkObjectName [in] moniker of the object whose status is desired
841 * pfiletime [out] structure that receives object's last change time
843 static HRESULT WINAPI
844 RunningObjectTableImpl_GetTimeOfLastChange(IRunningObjectTable* iface,
845 IMoniker *pmkObjectName, FILETIME *pfiletime)
847 HRESULT hr = MK_E_UNAVAILABLE;
848 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
849 MonikerComparisonData *moniker_data;
850 const struct rot_entry *rot_entry;
852 TRACE("(%p,%p,%p)\n",This,pmkObjectName,pfiletime);
854 if (pmkObjectName==NULL || pfiletime==NULL)
855 return E_INVALIDARG;
857 hr = reduce_moniker(pmkObjectName, NULL, &pmkObjectName);
858 if (FAILED(hr))
859 return hr;
860 hr = get_moniker_comparison_data(pmkObjectName, &moniker_data);
861 IMoniker_Release(pmkObjectName);
862 if (hr != S_OK)
863 return hr;
865 hr = MK_E_UNAVAILABLE;
867 EnterCriticalSection(&This->lock);
868 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, const struct rot_entry, entry)
870 if ((rot_entry->moniker_data->ulCntData == moniker_data->ulCntData) &&
871 !memcmp(&moniker_data->abData, &rot_entry->moniker_data->abData, moniker_data->ulCntData))
873 *pfiletime = rot_entry->last_modified;
874 hr = S_OK;
875 break;
878 LeaveCriticalSection(&This->lock);
880 if (hr != S_OK)
882 while (TRUE)
884 __TRY
886 hr = IrotGetTimeOfLastChange(get_irot_handle(), moniker_data, pfiletime);
888 __EXCEPT(rpc_filter)
890 hr = HRESULT_FROM_WIN32(GetExceptionCode());
892 __ENDTRY
893 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
895 if (start_rpcss())
896 continue;
898 break;
902 HeapFree(GetProcessHeap(), 0, moniker_data);
904 TRACE("-- 0x%08x\n", hr);
905 return hr;
908 /***********************************************************************
909 * RunningObjectTable_EnumRunning
911 * PARAMS
912 * ppenumMoniker [out] receives the IEnumMoniker interface pointer
914 static HRESULT WINAPI
915 RunningObjectTableImpl_EnumRunning(IRunningObjectTable* iface,
916 IEnumMoniker **ppenumMoniker)
918 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
919 InterfaceList *interface_list = NULL;
920 HRESULT hr;
922 TRACE("(%p, %p)\n", This, ppenumMoniker);
924 *ppenumMoniker = NULL;
926 while (TRUE)
928 __TRY
930 hr = IrotEnumRunning(get_irot_handle(), &interface_list);
932 __EXCEPT(rpc_filter)
934 hr = HRESULT_FROM_WIN32(GetExceptionCode());
936 __ENDTRY
937 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
939 if (start_rpcss())
940 continue;
942 break;
945 if (SUCCEEDED(hr))
946 hr = EnumMonikerImpl_CreateEnumROTMoniker(interface_list,
947 0, ppenumMoniker);
949 return hr;
952 /* Virtual function table for the IRunningObjectTable class. */
953 static const IRunningObjectTableVtbl VT_RunningObjectTableImpl =
955 RunningObjectTableImpl_QueryInterface,
956 RunningObjectTableImpl_AddRef,
957 RunningObjectTableImpl_Release,
958 RunningObjectTableImpl_Register,
959 RunningObjectTableImpl_Revoke,
960 RunningObjectTableImpl_IsRunning,
961 RunningObjectTableImpl_GetObject,
962 RunningObjectTableImpl_NoteChangeTime,
963 RunningObjectTableImpl_GetTimeOfLastChange,
964 RunningObjectTableImpl_EnumRunning
967 /***********************************************************************
968 * RunningObjectTable_Initialize
970 HRESULT WINAPI RunningObjectTableImpl_Initialize(void)
972 TRACE("\n");
974 /* create the unique instance of the RunningObjectTableImpl structure */
975 runningObjectTableInstance = HeapAlloc(GetProcessHeap(), 0, sizeof(RunningObjectTableImpl));
977 if (!runningObjectTableInstance)
978 return E_OUTOFMEMORY;
980 /* initialize the virtual table function */
981 runningObjectTableInstance->lpVtbl = &VT_RunningObjectTableImpl;
983 /* the initial reference is set to "1" ! because if set to "0" it will be not practis when */
984 /* the ROT referred many times not in the same time (all the objects in the ROT will */
985 /* be removed every time the ROT is removed ) */
986 runningObjectTableInstance->ref = 1;
988 list_init(&runningObjectTableInstance->rot);
989 InitializeCriticalSection(&runningObjectTableInstance->lock);
990 DEBUG_SET_CRITSEC_NAME(&runningObjectTableInstance->lock, "RunningObjectTableImpl.lock");
992 return S_OK;
995 /***********************************************************************
996 * RunningObjectTable_UnInitialize
998 HRESULT WINAPI RunningObjectTableImpl_UnInitialize(void)
1000 TRACE("\n");
1002 if (runningObjectTableInstance==NULL)
1003 return E_POINTER;
1005 RunningObjectTableImpl_Release((IRunningObjectTable*)runningObjectTableInstance);
1007 RunningObjectTableImpl_Destroy();
1009 return S_OK;
1012 /***********************************************************************
1013 * GetRunningObjectTable (OLE32.@)
1015 * Retrieves the global running object table.
1017 * PARAMS
1018 * reserved [I] Reserved. Set to 0.
1019 * pprot [O] Address that receives the pointer to the running object table.
1021 * RETURNS
1022 * Success: S_OK.
1023 * Failure: Any HRESULT code.
1025 HRESULT WINAPI
1026 GetRunningObjectTable(DWORD reserved, LPRUNNINGOBJECTTABLE *pprot)
1028 IID riid=IID_IRunningObjectTable;
1029 HRESULT res;
1031 TRACE("()\n");
1033 if (reserved!=0)
1034 return E_UNEXPECTED;
1036 if(runningObjectTableInstance==NULL)
1037 return CO_E_NOTINITIALIZED;
1039 res = IRunningObjectTable_QueryInterface((IRunningObjectTable*)runningObjectTableInstance,&riid,(void**)pprot);
1041 return res;
1044 static HRESULT get_moniker_for_progid_display_name(LPBC pbc,
1045 LPCOLESTR szDisplayName,
1046 LPDWORD pchEaten,
1047 LPMONIKER *ppmk)
1049 CLSID clsid;
1050 HRESULT hr;
1051 LPWSTR progid;
1052 LPCWSTR start = szDisplayName;
1053 LPCWSTR end;
1054 int len;
1055 IMoniker *class_moniker;
1057 if (*start == '@')
1058 start++;
1060 /* find end delimiter */
1061 for (end = start; *end; end++)
1062 if (*end == ':')
1063 break;
1065 len = end - start;
1067 /* must start with '@' or have a ':' somewhere and mustn't be one character
1068 * long (since that looks like an absolute path) */
1069 if (((start == szDisplayName) && (*end == '\0')) || (len <= 1))
1070 return MK_E_SYNTAX;
1072 progid = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1073 if (progid)
1075 memcpy(progid, start, len * sizeof(WCHAR));
1076 progid[len] = '\0';
1078 hr = CLSIDFromProgID(progid, &clsid);
1079 HeapFree(GetProcessHeap(), 0, progid);
1080 if (FAILED(hr))
1081 return MK_E_SYNTAX;
1083 hr = CreateClassMoniker(&clsid, &class_moniker);
1084 if (SUCCEEDED(hr))
1086 IParseDisplayName *pdn;
1087 hr = IMoniker_BindToObject(class_moniker, pbc, NULL,
1088 &IID_IParseDisplayName, (void **)&pdn);
1089 /* fallback to using IClassFactory to get IParseDisplayName -
1090 * adsldp.dll depends on this */
1091 if (FAILED(hr))
1093 IClassFactory *pcf;
1094 hr = IMoniker_BindToObject(class_moniker, pbc, NULL,
1095 &IID_IClassFactory, (void **)&pcf);
1096 if (SUCCEEDED(hr))
1098 hr = IClassFactory_CreateInstance(pcf, NULL,
1099 &IID_IParseDisplayName,
1100 (void **)&pdn);
1101 IClassFactory_Release(pcf);
1104 IMoniker_Release(class_moniker);
1105 if (SUCCEEDED(hr))
1107 hr = IParseDisplayName_ParseDisplayName(pdn, pbc,
1108 (LPOLESTR)szDisplayName,
1109 pchEaten, ppmk);
1110 IParseDisplayName_Release(pdn);
1113 return hr;
1116 /******************************************************************************
1117 * MkParseDisplayName [OLE32.@]
1119 HRESULT WINAPI MkParseDisplayName(LPBC pbc, LPCOLESTR szDisplayName,
1120 LPDWORD pchEaten, LPMONIKER *ppmk)
1122 HRESULT hr = MK_E_SYNTAX;
1123 static const WCHAR wszClsidColon[] = {'c','l','s','i','d',':'};
1124 IMoniker *moniker;
1125 DWORD chEaten;
1127 TRACE("(%p, %s, %p, %p)\n", pbc, debugstr_w(szDisplayName), pchEaten, ppmk);
1129 if (!(IsValidInterface((LPUNKNOWN) pbc)))
1130 return E_INVALIDARG;
1132 *pchEaten = 0;
1133 *ppmk = NULL;
1135 if (!strncmpiW(szDisplayName, wszClsidColon, sizeof(wszClsidColon)/sizeof(wszClsidColon[0])))
1137 hr = ClassMoniker_CreateFromDisplayName(pbc, szDisplayName, &chEaten, &moniker);
1138 if (FAILED(hr) && (hr != MK_E_SYNTAX))
1139 return hr;
1141 else
1143 hr = get_moniker_for_progid_display_name(pbc, szDisplayName, &chEaten, &moniker);
1144 if (FAILED(hr) && (hr != MK_E_SYNTAX))
1145 return hr;
1148 if (FAILED(hr))
1150 hr = FileMoniker_CreateFromDisplayName(pbc, szDisplayName, &chEaten, &moniker);
1151 if (FAILED(hr) && (hr != MK_E_SYNTAX))
1152 return hr;
1155 if (SUCCEEDED(hr))
1157 while (TRUE)
1159 IMoniker *next_moniker;
1160 *pchEaten += chEaten;
1161 szDisplayName += chEaten;
1162 if (!*szDisplayName)
1164 *ppmk = moniker;
1165 return S_OK;
1167 chEaten = 0;
1168 hr = IMoniker_ParseDisplayName(moniker, pbc, NULL,
1169 (LPOLESTR)szDisplayName, &chEaten,
1170 &next_moniker);
1171 IMoniker_Release(moniker);
1172 if (FAILED(hr))
1174 *pchEaten = 0;
1175 break;
1177 moniker = next_moniker;
1181 return hr;
1184 /***********************************************************************
1185 * GetClassFile (OLE32.@)
1187 * Retrieves the class ID associated with the given filename.
1189 * PARAMS
1190 * filePathName [I] Filename to retrieve the class ID for.
1191 * pclsid [O] Address that receives the class ID for the file.
1193 * RETURNS
1194 * Success: S_OK.
1195 * Failure: Any HRESULT code.
1197 HRESULT WINAPI GetClassFile(LPCOLESTR filePathName,CLSID *pclsid)
1199 IStorage *pstg=0;
1200 HRESULT res;
1201 int nbElm, length, i;
1202 LONG sizeProgId;
1203 LPOLESTR *pathDec=0,absFile=0,progId=0;
1204 LPWSTR extension;
1205 static const WCHAR bkslashW[] = {'\\',0};
1206 static const WCHAR dotW[] = {'.',0};
1208 TRACE("%s, %p\n", debugstr_w(filePathName), pclsid);
1210 /* if the file contain a storage object the return the CLSID written by IStorage_SetClass method*/
1211 if((StgIsStorageFile(filePathName))==S_OK){
1213 res=StgOpenStorage(filePathName,NULL,STGM_READ | STGM_SHARE_DENY_WRITE,NULL,0,&pstg);
1215 if (SUCCEEDED(res))
1216 res=ReadClassStg(pstg,pclsid);
1218 IStorage_Release(pstg);
1220 return res;
1222 /* if the file is not a storage object then attemps to match various bits in the file against a
1223 pattern in the registry. this case is not frequently used ! so I present only the psodocode for
1224 this case
1226 for(i=0;i<nFileTypes;i++)
1228 for(i=0;j<nPatternsForType;j++){
1230 PATTERN pat;
1231 HANDLE hFile;
1233 pat=ReadPatternFromRegistry(i,j);
1234 hFile=CreateFileW(filePathName,,,,,,hFile);
1235 SetFilePosition(hFile,pat.offset);
1236 ReadFile(hFile,buf,pat.size,&r,NULL);
1237 if (memcmp(buf&pat.mask,pat.pattern.pat.size)==0){
1239 *pclsid=ReadCLSIDFromRegistry(i);
1240 return S_OK;
1245 /* if the above strategies fail then search for the extension key in the registry */
1247 /* get the last element (absolute file) in the path name */
1248 nbElm=FileMonikerImpl_DecomposePath(filePathName,&pathDec);
1249 absFile=pathDec[nbElm-1];
1251 /* failed if the path represente a directory and not an absolute file name*/
1252 if (!lstrcmpW(absFile, bkslashW))
1253 return MK_E_INVALIDEXTENSION;
1255 /* get the extension of the file */
1256 extension = NULL;
1257 length=lstrlenW(absFile);
1258 for(i = length-1; (i >= 0) && *(extension = &absFile[i]) != '.'; i--)
1259 /* nothing */;
1261 if (!extension || !lstrcmpW(extension, dotW))
1262 return MK_E_INVALIDEXTENSION;
1264 res=RegQueryValueW(HKEY_CLASSES_ROOT, extension, NULL, &sizeProgId);
1266 /* get the progId associated to the extension */
1267 progId = CoTaskMemAlloc(sizeProgId);
1268 res = RegQueryValueW(HKEY_CLASSES_ROOT, extension, progId, &sizeProgId);
1270 if (res==ERROR_SUCCESS)
1271 /* return the clsid associated to the progId */
1272 res= CLSIDFromProgID(progId,pclsid);
1274 for(i=0; pathDec[i]!=NULL;i++)
1275 CoTaskMemFree(pathDec[i]);
1276 CoTaskMemFree(pathDec);
1278 CoTaskMemFree(progId);
1280 if (res==ERROR_SUCCESS)
1281 return res;
1283 return MK_E_INVALIDEXTENSION;
1286 /***********************************************************************
1287 * EnumMoniker_QueryInterface
1289 static HRESULT WINAPI EnumMonikerImpl_QueryInterface(IEnumMoniker* iface,REFIID riid,void** ppvObject)
1291 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1293 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
1295 /* validate arguments */
1296 if (ppvObject == NULL)
1297 return E_INVALIDARG;
1299 *ppvObject = NULL;
1301 if (IsEqualIID(&IID_IUnknown, riid))
1302 *ppvObject = (IEnumMoniker*)This;
1303 else
1304 if (IsEqualIID(&IID_IEnumMoniker, riid))
1305 *ppvObject = (IEnumMoniker*)This;
1307 if ((*ppvObject)==NULL)
1308 return E_NOINTERFACE;
1310 IEnumMoniker_AddRef(iface);
1312 return S_OK;
1315 /***********************************************************************
1316 * EnumMoniker_AddRef
1318 static ULONG WINAPI EnumMonikerImpl_AddRef(IEnumMoniker* iface)
1320 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1322 TRACE("(%p)\n",This);
1324 return InterlockedIncrement(&This->ref);
1327 /***********************************************************************
1328 * EnumMoniker_release
1330 static ULONG WINAPI EnumMonikerImpl_Release(IEnumMoniker* iface)
1332 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1333 ULONG ref;
1335 TRACE("(%p)\n",This);
1337 ref = InterlockedDecrement(&This->ref);
1339 /* uninitialize rot structure if there's no more reference to it*/
1340 if (ref == 0)
1342 ULONG i;
1344 TRACE("(%p) Deleting\n",This);
1346 for (i = 0; i < This->moniker_list->size; i++)
1347 HeapFree(GetProcessHeap(), 0, This->moniker_list->interfaces[i]);
1348 HeapFree(GetProcessHeap(), 0, This->moniker_list);
1349 HeapFree(GetProcessHeap(), 0, This);
1352 return ref;
1354 /***********************************************************************
1355 * EnmumMoniker_Next
1357 static HRESULT WINAPI EnumMonikerImpl_Next(IEnumMoniker* iface, ULONG celt, IMoniker** rgelt, ULONG * pceltFetched)
1359 ULONG i;
1360 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1361 HRESULT hr = S_OK;
1363 TRACE("(%p) TabCurrentPos %d Tablastindx %d\n", This, This->pos, This->moniker_list->size);
1365 /* retrieve the requested number of moniker from the current position */
1366 for(i = 0; (This->pos < This->moniker_list->size) && (i < celt); i++)
1368 IStream *stream;
1369 hr = create_stream_on_mip_ro(This->moniker_list->interfaces[This->pos++], &stream);
1370 if (hr != S_OK) break;
1371 hr = CoUnmarshalInterface(stream, &IID_IMoniker, (void **)&rgelt[i]);
1372 IStream_Release(stream);
1373 if (hr != S_OK) break;
1376 if (pceltFetched != NULL)
1377 *pceltFetched= i;
1379 if (hr != S_OK)
1380 return hr;
1382 if (i == celt)
1383 return S_OK;
1384 else
1385 return S_FALSE;
1389 /***********************************************************************
1390 * EnmumMoniker_Skip
1392 static HRESULT WINAPI EnumMonikerImpl_Skip(IEnumMoniker* iface, ULONG celt)
1394 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1396 TRACE("(%p)\n",This);
1398 if (This->pos + celt >= This->moniker_list->size)
1399 return S_FALSE;
1401 This->pos += celt;
1403 return S_OK;
1406 /***********************************************************************
1407 * EnmumMoniker_Reset
1409 static HRESULT WINAPI EnumMonikerImpl_Reset(IEnumMoniker* iface)
1411 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1413 This->pos = 0; /* set back to start of list */
1415 TRACE("(%p)\n",This);
1417 return S_OK;
1420 /***********************************************************************
1421 * EnmumMoniker_Clone
1423 static HRESULT WINAPI EnumMonikerImpl_Clone(IEnumMoniker* iface, IEnumMoniker ** ppenum)
1425 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1426 InterfaceList *moniker_list;
1427 ULONG i;
1429 TRACE("(%p)\n",This);
1431 *ppenum = NULL;
1433 moniker_list = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(InterfaceList, interfaces[This->moniker_list->size]));
1434 if (!moniker_list)
1435 return E_OUTOFMEMORY;
1437 moniker_list->size = This->moniker_list->size;
1438 for (i = 0; i < This->moniker_list->size; i++)
1440 SIZE_T size = FIELD_OFFSET(InterfaceData, abData[This->moniker_list->interfaces[i]->ulCntData]);
1441 moniker_list->interfaces[i] = HeapAlloc(GetProcessHeap(), 0, size);
1442 if (!moniker_list->interfaces[i])
1444 ULONG end = i;
1445 for (i = 0; i < end; i++)
1446 HeapFree(GetProcessHeap(), 0, moniker_list->interfaces[i]);
1447 HeapFree(GetProcessHeap(), 0, moniker_list);
1448 return E_OUTOFMEMORY;
1450 memcpy(moniker_list->interfaces[i], This->moniker_list->interfaces[i], size);
1453 /* copy the enum structure */
1454 return EnumMonikerImpl_CreateEnumROTMoniker(moniker_list, This->pos, ppenum);
1457 /* Virtual function table for the IEnumMoniker class. */
1458 static const IEnumMonikerVtbl VT_EnumMonikerImpl =
1460 EnumMonikerImpl_QueryInterface,
1461 EnumMonikerImpl_AddRef,
1462 EnumMonikerImpl_Release,
1463 EnumMonikerImpl_Next,
1464 EnumMonikerImpl_Skip,
1465 EnumMonikerImpl_Reset,
1466 EnumMonikerImpl_Clone
1469 /***********************************************************************
1470 * EnumMonikerImpl_CreateEnumROTMoniker
1471 * Used by EnumRunning to create the structure and EnumClone
1472 * to copy the structure
1474 static HRESULT WINAPI EnumMonikerImpl_CreateEnumROTMoniker(InterfaceList *moniker_list,
1475 ULONG current_pos,
1476 IEnumMoniker **ppenumMoniker)
1478 EnumMonikerImpl* This = NULL;
1480 if (!ppenumMoniker)
1481 return E_INVALIDARG;
1483 This = HeapAlloc(GetProcessHeap(), 0, sizeof(EnumMonikerImpl));
1484 if (!This) return E_OUTOFMEMORY;
1486 TRACE("(%p)\n", This);
1488 /* initialize the virtual table function */
1489 This->lpVtbl = &VT_EnumMonikerImpl;
1491 /* the initial reference is set to "1" */
1492 This->ref = 1; /* set the ref count to one */
1493 This->pos = current_pos; /* Set the list start posn */
1494 This->moniker_list = moniker_list;
1496 *ppenumMoniker = (IEnumMoniker*)This;
1498 return S_OK;
1502 /* Shared implementation of moniker marshaler based on saving and loading of
1503 * monikers */
1505 typedef struct MonikerMarshal
1507 const IUnknownVtbl *lpVtbl;
1508 const IMarshalVtbl *lpVtblMarshal;
1510 LONG ref;
1511 IMoniker *moniker;
1512 } MonikerMarshal;
1514 static inline MonikerMarshal *impl_from_IMarshal( IMarshal *iface )
1516 return (MonikerMarshal *)((char*)iface - FIELD_OFFSET(MonikerMarshal, lpVtblMarshal));
1519 static HRESULT WINAPI MonikerMarshalInner_QueryInterface(IUnknown *iface, REFIID riid, LPVOID *ppv)
1521 MonikerMarshal *This = (MonikerMarshal *)iface;
1522 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
1523 *ppv = NULL;
1524 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IMarshal, riid))
1526 *ppv = &This->lpVtblMarshal;
1527 IUnknown_AddRef((IUnknown *)&This->lpVtblMarshal);
1528 return S_OK;
1530 FIXME("No interface for %s\n", debugstr_guid(riid));
1531 return E_NOINTERFACE;
1534 static ULONG WINAPI MonikerMarshalInner_AddRef(IUnknown *iface)
1536 MonikerMarshal *This = (MonikerMarshal *)iface;
1537 return InterlockedIncrement(&This->ref);
1540 static ULONG WINAPI MonikerMarshalInner_Release(IUnknown *iface)
1542 MonikerMarshal *This = (MonikerMarshal *)iface;
1543 ULONG ref = InterlockedDecrement(&This->ref);
1545 if (!ref) HeapFree(GetProcessHeap(), 0, This);
1546 return ref;
1549 static const IUnknownVtbl VT_MonikerMarshalInner =
1551 MonikerMarshalInner_QueryInterface,
1552 MonikerMarshalInner_AddRef,
1553 MonikerMarshalInner_Release
1556 static HRESULT WINAPI MonikerMarshal_QueryInterface(IMarshal *iface, REFIID riid, LPVOID *ppv)
1558 MonikerMarshal *This = impl_from_IMarshal(iface);
1559 return IMoniker_QueryInterface(This->moniker, riid, ppv);
1562 static ULONG WINAPI MonikerMarshal_AddRef(IMarshal *iface)
1564 MonikerMarshal *This = impl_from_IMarshal(iface);
1565 return IMoniker_AddRef(This->moniker);
1568 static ULONG WINAPI MonikerMarshal_Release(IMarshal *iface)
1570 MonikerMarshal *This = impl_from_IMarshal(iface);
1571 return IMoniker_Release(This->moniker);
1574 static HRESULT WINAPI MonikerMarshal_GetUnmarshalClass(
1575 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
1576 void* pvDestContext, DWORD mshlflags, CLSID* pCid)
1578 MonikerMarshal *This = impl_from_IMarshal(iface);
1580 TRACE("(%s, %p, %x, %p, %x, %p)\n", debugstr_guid(riid), pv,
1581 dwDestContext, pvDestContext, mshlflags, pCid);
1583 return IMoniker_GetClassID(This->moniker, pCid);
1586 static HRESULT WINAPI MonikerMarshal_GetMarshalSizeMax(
1587 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
1588 void* pvDestContext, DWORD mshlflags, DWORD* pSize)
1590 MonikerMarshal *This = impl_from_IMarshal(iface);
1591 HRESULT hr;
1592 ULARGE_INTEGER size;
1594 TRACE("(%s, %p, %x, %p, %x, %p)\n", debugstr_guid(riid), pv,
1595 dwDestContext, pvDestContext, mshlflags, pSize);
1597 hr = IMoniker_GetSizeMax(This->moniker, &size);
1598 if (hr == S_OK)
1599 *pSize = (DWORD)size.QuadPart;
1600 return hr;
1603 static HRESULT WINAPI MonikerMarshal_MarshalInterface(LPMARSHAL iface, IStream *pStm,
1604 REFIID riid, void* pv, DWORD dwDestContext,
1605 void* pvDestContext, DWORD mshlflags)
1607 MonikerMarshal *This = impl_from_IMarshal(iface);
1609 TRACE("(%p, %s, %p, %x, %p, %x)\n", pStm, debugstr_guid(riid), pv,
1610 dwDestContext, pvDestContext, mshlflags);
1612 return IMoniker_Save(This->moniker, pStm, FALSE);
1615 static HRESULT WINAPI MonikerMarshal_UnmarshalInterface(LPMARSHAL iface, IStream *pStm, REFIID riid, void **ppv)
1617 MonikerMarshal *This = impl_from_IMarshal(iface);
1618 HRESULT hr;
1620 TRACE("(%p, %s, %p)\n", pStm, debugstr_guid(riid), ppv);
1622 hr = IMoniker_Load(This->moniker, pStm);
1623 if (hr == S_OK)
1624 hr = IMoniker_QueryInterface(This->moniker, riid, ppv);
1625 return hr;
1628 static HRESULT WINAPI MonikerMarshal_ReleaseMarshalData(LPMARSHAL iface, IStream *pStm)
1630 TRACE("()\n");
1631 /* can't release a state-based marshal as nothing on server side to
1632 * release */
1633 return S_OK;
1636 static HRESULT WINAPI MonikerMarshal_DisconnectObject(LPMARSHAL iface, DWORD dwReserved)
1638 TRACE("()\n");
1639 /* can't disconnect a state-based marshal as nothing on server side to
1640 * disconnect from */
1641 return S_OK;
1644 static const IMarshalVtbl VT_MonikerMarshal =
1646 MonikerMarshal_QueryInterface,
1647 MonikerMarshal_AddRef,
1648 MonikerMarshal_Release,
1649 MonikerMarshal_GetUnmarshalClass,
1650 MonikerMarshal_GetMarshalSizeMax,
1651 MonikerMarshal_MarshalInterface,
1652 MonikerMarshal_UnmarshalInterface,
1653 MonikerMarshal_ReleaseMarshalData,
1654 MonikerMarshal_DisconnectObject
1657 HRESULT MonikerMarshal_Create(IMoniker *inner, IUnknown **outer)
1659 MonikerMarshal *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1660 if (!This) return E_OUTOFMEMORY;
1662 This->lpVtbl = &VT_MonikerMarshalInner;
1663 This->lpVtblMarshal = &VT_MonikerMarshal;
1664 This->ref = 1;
1665 This->moniker = inner;
1667 *outer = (IUnknown *)&This->lpVtbl;
1668 return S_OK;
1671 void * __RPC_USER MIDL_user_allocate(size_t size)
1673 return HeapAlloc(GetProcessHeap(), 0, size);
1676 void __RPC_USER MIDL_user_free(void *p)
1678 HeapFree(GetProcessHeap(), 0, p);