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
25 #include "wine/port.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"
48 WINE_DEFAULT_DEBUG_CHANNEL(ole);
50 /* see MSDN docs for IROTData::GetComparisonData, which states what this
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 */
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;
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;
90 InterfaceList *moniker_list;
95 /* IEnumMoniker Local functions*/
96 static HRESULT EnumMonikerImpl_CreateEnumROTMoniker(InterfaceList *moniker_list,
97 ULONG pos, IEnumMoniker **ppenumMoniker);
99 static IrotHandle get_irot_handle(void)
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)
116 if (InterlockedCompareExchangePointer(&irot_handle, new_handle, NULL))
117 /* another thread beat us to it */
118 RpcBindingFree(&new_handle);
123 static BOOL start_rpcss(void)
125 PROCESS_INFORMATION pi;
128 static const WCHAR rpcss[] = {'\\','r','p','c','s','s','.','e','x','e',0};
134 ZeroMemory(&si, sizeof(STARTUPINFOA));
135 si.cb = sizeof(STARTUPINFOA);
136 GetSystemDirectoryW( cmd, MAX_PATH - sizeof(rpcss)/sizeof(WCHAR) );
137 strcatW( cmd, rpcss );
139 Wow64DisableWow64FsRedirection( &redir );
140 rslt = CreateProcessW( cmd, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi );
141 Wow64RevertWow64FsRedirection( redir );
145 CloseHandle(pi.hProcess);
146 CloseHandle(pi.hThread);
153 static HRESULT create_stream_on_mip_ro(const InterfaceData *mip, IStream **stream)
155 HGLOBAL hglobal = GlobalAlloc(0, mip->ulCntData);
156 void *pv = GlobalLock(hglobal);
157 memcpy(pv, mip->abData, mip->ulCntData);
158 GlobalUnlock(hglobal);
159 return CreateStreamOnHGlobal(hglobal, TRUE, stream);
162 static inline void rot_entry_delete(struct rot_entry *rot_entry)
164 if (rot_entry->cookie)
166 InterfaceData *object = NULL;
167 InterfaceData *moniker = NULL;
170 IrotRevoke(get_irot_handle(), rot_entry->cookie,
171 &rot_entry->ctxt_handle, &object, &moniker);
177 MIDL_user_free(object);
182 hr = create_stream_on_mip_ro(moniker, &stream);
185 CoReleaseMarshalData(stream);
186 IUnknown_Release(stream);
189 MIDL_user_free(moniker);
191 if (rot_entry->object)
195 hr = create_stream_on_mip_ro(rot_entry->object, &stream);
198 CoReleaseMarshalData(stream);
199 IUnknown_Release(stream);
202 HeapFree(GetProcessHeap(), 0, rot_entry->object);
203 HeapFree(GetProcessHeap(), 0, rot_entry->moniker_data);
204 HeapFree(GetProcessHeap(), 0, rot_entry);
207 /* moniker_data must be freed with HeapFree when no longer in use */
208 static HRESULT get_moniker_comparison_data(IMoniker *pMoniker, MonikerComparisonData **moniker_data)
211 IROTData *pROTData = NULL;
212 hr = IMoniker_QueryInterface(pMoniker, &IID_IROTData, (void *)&pROTData);
215 ULONG size = MAX_COMPARISON_DATA;
216 *moniker_data = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(MonikerComparisonData, abData[size]));
219 IROTData_Release(pROTData);
220 return E_OUTOFMEMORY;
222 hr = IROTData_GetComparisonData(pROTData, (*moniker_data)->abData, size, &size);
223 IROTData_Release(pROTData);
226 ERR("Failed to copy comparison data into buffer, hr = 0x%08x\n", hr);
227 HeapFree(GetProcessHeap(), 0, *moniker_data);
230 (*moniker_data)->ulCntData = size;
235 LPOLESTR pszDisplayName;
239 TRACE("generating comparison data from display name\n");
241 hr = CreateBindCtx(0, &pbc);
244 hr = IMoniker_GetDisplayName(pMoniker, pbc, NULL, &pszDisplayName);
245 IBindCtx_Release(pbc);
248 hr = IMoniker_GetClassID(pMoniker, &clsid);
251 CoTaskMemFree(pszDisplayName);
255 len = strlenW(pszDisplayName);
256 *moniker_data = HeapAlloc(GetProcessHeap(), 0,
257 FIELD_OFFSET(MonikerComparisonData, abData[sizeof(CLSID) + (len+1)*sizeof(WCHAR)]));
260 CoTaskMemFree(pszDisplayName);
261 return E_OUTOFMEMORY;
263 (*moniker_data)->ulCntData = sizeof(CLSID) + (len+1)*sizeof(WCHAR);
265 memcpy(&(*moniker_data)->abData[0], &clsid, sizeof(clsid));
266 memcpy(&(*moniker_data)->abData[sizeof(clsid)], pszDisplayName, (len+1)*sizeof(WCHAR));
267 CoTaskMemFree(pszDisplayName);
272 static HRESULT reduce_moniker(IMoniker *pmk, IBindCtx *pbc, IMoniker **pmkReduced)
274 IBindCtx *pbcNew = NULL;
278 hr = CreateBindCtx(0, &pbcNew);
283 hr = IMoniker_Reduce(pmk, pbc, MKRREDUCE_ALL, NULL, pmkReduced);
285 ERR("reducing moniker failed with error 0x%08x\n", hr);
286 if (pbcNew) IBindCtx_Release(pbcNew);
290 /***********************************************************************
291 * RunningObjectTable_QueryInterface
293 static HRESULT WINAPI
294 RunningObjectTableImpl_QueryInterface(IRunningObjectTable* iface,
295 REFIID riid,void** ppvObject)
297 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
299 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
301 /* validate arguments */
308 if (IsEqualIID(&IID_IUnknown, riid) ||
309 IsEqualIID(&IID_IRunningObjectTable, riid))
313 return E_NOINTERFACE;
315 IRunningObjectTable_AddRef(iface);
320 /***********************************************************************
321 * RunningObjectTable_AddRef
324 RunningObjectTableImpl_AddRef(IRunningObjectTable* iface)
326 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
328 TRACE("(%p)\n",This);
330 return InterlockedIncrement(&This->ref);
333 /***********************************************************************
334 * RunningObjectTable_Destroy
337 RunningObjectTableImpl_Destroy(void)
339 struct list *cursor, *cursor2;
340 IrotHandle old_handle;
344 if (runningObjectTableInstance==NULL)
347 /* free the ROT table memory */
348 LIST_FOR_EACH_SAFE(cursor, cursor2, &runningObjectTableInstance->rot)
350 struct rot_entry *rot_entry = LIST_ENTRY(cursor, struct rot_entry, entry);
351 list_remove(&rot_entry->entry);
352 rot_entry_delete(rot_entry);
355 DEBUG_CLEAR_CRITSEC_NAME(&runningObjectTableInstance->lock);
356 DeleteCriticalSection(&runningObjectTableInstance->lock);
358 /* free the ROT structure memory */
359 HeapFree(GetProcessHeap(),0,runningObjectTableInstance);
360 runningObjectTableInstance = NULL;
362 old_handle = irot_handle;
365 RpcBindingFree(&old_handle);
370 /***********************************************************************
371 * RunningObjectTable_Release
374 RunningObjectTableImpl_Release(IRunningObjectTable* iface)
376 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
379 TRACE("(%p)\n",This);
381 ref = InterlockedDecrement(&This->ref);
383 /* uninitialize ROT structure if there's no more references to it */
386 struct list *cursor, *cursor2;
387 LIST_FOR_EACH_SAFE(cursor, cursor2, &This->rot)
389 struct rot_entry *rot_entry = LIST_ENTRY(cursor, struct rot_entry, entry);
390 list_remove(&rot_entry->entry);
391 rot_entry_delete(rot_entry);
393 /* RunningObjectTable data structure will be not destroyed here ! the destruction will be done only
394 * when RunningObjectTableImpl_UnInitialize function is called
401 /***********************************************************************
402 * RunningObjectTable_Register
405 * grfFlags [in] Registration options
406 * punkObject [in] the object being registered
407 * pmkObjectName [in] the moniker of the object being registered
408 * pdwRegister [out] the value identifying the registration
410 static HRESULT WINAPI
411 RunningObjectTableImpl_Register(IRunningObjectTable* iface, DWORD grfFlags,
412 IUnknown *punkObject, IMoniker *pmkObjectName, DWORD *pdwRegister)
414 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
415 struct rot_entry *rot_entry;
417 IStream *pStream = NULL;
420 InterfaceData *moniker = NULL;
422 TRACE("(%p,%d,%p,%p,%p)\n",This,grfFlags,punkObject,pmkObjectName,pdwRegister);
424 if (grfFlags & ~(ROTFLAGS_REGISTRATIONKEEPSALIVE|ROTFLAGS_ALLOWANYCLIENT))
426 ERR("Invalid grfFlags: 0x%08x\n", grfFlags & ~(ROTFLAGS_REGISTRATIONKEEPSALIVE|ROTFLAGS_ALLOWANYCLIENT));
430 if (punkObject==NULL || pmkObjectName==NULL || pdwRegister==NULL)
433 rot_entry = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*rot_entry));
435 return E_OUTOFMEMORY;
438 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
441 rot_entry_delete(rot_entry);
444 mshlflags = (grfFlags & ROTFLAGS_REGISTRATIONKEEPSALIVE) ? MSHLFLAGS_TABLESTRONG : MSHLFLAGS_TABLEWEAK;
445 hr = CoMarshalInterface(pStream, &IID_IUnknown, punkObject, MSHCTX_LOCAL | MSHCTX_NOSHAREDMEM, NULL, mshlflags);
446 /* FIXME: a cleaner way would be to create an IStream class that writes
447 * directly to an MInterfacePointer */
451 hr = GetHGlobalFromStream(pStream, &hglobal);
454 SIZE_T size = GlobalSize(hglobal);
455 const void *pv = GlobalLock(hglobal);
456 rot_entry->object = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(MInterfacePointer, abData[size]));
457 rot_entry->object->ulCntData = size;
458 memcpy(rot_entry->object->abData, pv, size);
459 GlobalUnlock(hglobal);
462 IStream_Release(pStream);
465 rot_entry_delete(rot_entry);
469 hr = CreateBindCtx(0, &pbc);
472 rot_entry_delete(rot_entry);
476 hr = reduce_moniker(pmkObjectName, pbc, &pmkObjectName);
479 rot_entry_delete(rot_entry);
480 IBindCtx_Release(pbc);
484 hr = IMoniker_GetTimeOfLastChange(pmkObjectName, pbc, NULL,
485 &rot_entry->last_modified);
486 IBindCtx_Release(pbc);
489 CoFileTimeNow(&rot_entry->last_modified);
493 hr = get_moniker_comparison_data(pmkObjectName,
494 &rot_entry->moniker_data);
497 rot_entry_delete(rot_entry);
498 IMoniker_Release(pmkObjectName);
502 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
505 rot_entry_delete(rot_entry);
506 IMoniker_Release(pmkObjectName);
509 /* marshal moniker */
510 hr = CoMarshalInterface(pStream, &IID_IMoniker, (IUnknown *)pmkObjectName,
511 MSHCTX_LOCAL | MSHCTX_NOSHAREDMEM, NULL, MSHLFLAGS_TABLESTRONG);
512 /* FIXME: a cleaner way would be to create an IStream class that writes
513 * directly to an MInterfacePointer */
517 hr = GetHGlobalFromStream(pStream, &hglobal);
520 SIZE_T size = GlobalSize(hglobal);
521 const void *pv = GlobalLock(hglobal);
522 moniker = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(InterfaceData, abData[size]));
523 moniker->ulCntData = size;
524 memcpy(moniker->abData, pv, size);
525 GlobalUnlock(hglobal);
528 IStream_Release(pStream);
529 IMoniker_Release(pmkObjectName);
532 HeapFree(GetProcessHeap(), 0, moniker);
533 rot_entry_delete(rot_entry);
542 hr = IrotRegister(get_irot_handle(), rot_entry->moniker_data,
543 rot_entry->object, moniker,
544 &rot_entry->last_modified, grfFlags,
545 &rot_entry->cookie, &rot_entry->ctxt_handle);
549 hr = HRESULT_FROM_WIN32(GetExceptionCode());
552 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
559 HeapFree(GetProcessHeap(), 0, moniker);
562 rot_entry_delete(rot_entry);
566 /* gives a registration identifier to the registered object*/
567 *pdwRegister = rot_entry->cookie;
569 EnterCriticalSection(&This->lock);
570 list_add_tail(&This->rot, &rot_entry->entry);
571 LeaveCriticalSection(&This->lock);
576 /***********************************************************************
577 * RunningObjectTable_Revoke
580 * dwRegister [in] Value identifying registration to be revoked
582 static HRESULT WINAPI
583 RunningObjectTableImpl_Revoke( IRunningObjectTable* iface, DWORD dwRegister)
585 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
586 struct rot_entry *rot_entry;
588 TRACE("(%p,%d)\n",This,dwRegister);
590 EnterCriticalSection(&This->lock);
591 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, struct rot_entry, entry)
593 if (rot_entry->cookie == dwRegister)
595 list_remove(&rot_entry->entry);
596 LeaveCriticalSection(&This->lock);
598 rot_entry_delete(rot_entry);
602 LeaveCriticalSection(&This->lock);
607 /***********************************************************************
608 * RunningObjectTable_IsRunning
611 * pmkObjectName [in] moniker of the object whose status is desired
613 static HRESULT WINAPI
614 RunningObjectTableImpl_IsRunning( IRunningObjectTable* iface, IMoniker *pmkObjectName)
616 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
617 MonikerComparisonData *moniker_data;
619 const struct rot_entry *rot_entry;
621 TRACE("(%p,%p)\n",This,pmkObjectName);
623 hr = reduce_moniker(pmkObjectName, NULL, &pmkObjectName);
626 hr = get_moniker_comparison_data(pmkObjectName, &moniker_data);
627 IMoniker_Release(pmkObjectName);
632 EnterCriticalSection(&This->lock);
633 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, const struct rot_entry, entry)
635 if ((rot_entry->moniker_data->ulCntData == moniker_data->ulCntData) &&
636 !memcmp(moniker_data->abData, rot_entry->moniker_data->abData, moniker_data->ulCntData))
642 LeaveCriticalSection(&This->lock);
650 hr = IrotIsRunning(get_irot_handle(), moniker_data);
654 hr = HRESULT_FROM_WIN32(GetExceptionCode());
657 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
666 HeapFree(GetProcessHeap(), 0, moniker_data);
671 /***********************************************************************
672 * RunningObjectTable_GetObject
675 * pmkObjectName [in] Pointer to the moniker on the object
676 * ppunkObject [out] variable that receives the IUnknown interface pointer
678 static HRESULT WINAPI
679 RunningObjectTableImpl_GetObject( IRunningObjectTable* iface,
680 IMoniker *pmkObjectName, IUnknown **ppunkObject)
682 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
683 MonikerComparisonData *moniker_data;
684 InterfaceData *object = NULL;
687 struct rot_entry *rot_entry;
689 TRACE("(%p,%p,%p)\n",This,pmkObjectName,ppunkObject);
691 if (ppunkObject == NULL)
696 hr = reduce_moniker(pmkObjectName, NULL, &pmkObjectName);
699 hr = get_moniker_comparison_data(pmkObjectName, &moniker_data);
700 IMoniker_Release(pmkObjectName);
704 EnterCriticalSection(&This->lock);
705 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, struct rot_entry, entry)
707 if ((rot_entry->moniker_data->ulCntData == moniker_data->ulCntData) &&
708 !memcmp(moniker_data->abData, rot_entry->moniker_data->abData, moniker_data->ulCntData))
711 hr = create_stream_on_mip_ro(rot_entry->object, &pStream);
714 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)ppunkObject);
715 IStream_Release(pStream);
718 LeaveCriticalSection(&This->lock);
719 HeapFree(GetProcessHeap(), 0, moniker_data);
724 LeaveCriticalSection(&This->lock);
726 TRACE("moniker unavailable locally, calling SCM\n");
732 hr = IrotGetObject(get_irot_handle(), moniker_data, &object, &cookie);
736 hr = HRESULT_FROM_WIN32(GetExceptionCode());
739 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
750 hr = create_stream_on_mip_ro(object, &pStream);
753 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)ppunkObject);
754 IStream_Release(pStream);
758 WARN("Moniker unavailable, IrotGetObject returned 0x%08x\n", hr);
760 HeapFree(GetProcessHeap(), 0, moniker_data);
765 /***********************************************************************
766 * RunningObjectTable_NoteChangeTime
769 * dwRegister [in] Value identifying registration being updated
770 * pfiletime [in] Pointer to structure containing object's last change time
772 static HRESULT WINAPI
773 RunningObjectTableImpl_NoteChangeTime(IRunningObjectTable* iface,
774 DWORD dwRegister, FILETIME *pfiletime)
776 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
777 struct rot_entry *rot_entry;
778 HRESULT hr = E_INVALIDARG;
780 TRACE("(%p,%d,%p)\n",This,dwRegister,pfiletime);
782 EnterCriticalSection(&This->lock);
783 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, struct rot_entry, entry)
785 if (rot_entry->cookie == dwRegister)
787 rot_entry->last_modified = *pfiletime;
788 LeaveCriticalSection(&This->lock);
794 hr = IrotNoteChangeTime(get_irot_handle(), dwRegister, pfiletime);
798 hr = HRESULT_FROM_WIN32(GetExceptionCode());
801 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
812 LeaveCriticalSection(&This->lock);
815 TRACE("-- 0x08%x\n", hr);
819 /***********************************************************************
820 * RunningObjectTable_GetTimeOfLastChange
823 * pmkObjectName [in] moniker of the object whose status is desired
824 * pfiletime [out] structure that receives object's last change time
826 static HRESULT WINAPI
827 RunningObjectTableImpl_GetTimeOfLastChange(IRunningObjectTable* iface,
828 IMoniker *pmkObjectName, FILETIME *pfiletime)
830 HRESULT hr = MK_E_UNAVAILABLE;
831 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
832 MonikerComparisonData *moniker_data;
833 const struct rot_entry *rot_entry;
835 TRACE("(%p,%p,%p)\n",This,pmkObjectName,pfiletime);
837 if (pmkObjectName==NULL || pfiletime==NULL)
840 hr = reduce_moniker(pmkObjectName, NULL, &pmkObjectName);
843 hr = get_moniker_comparison_data(pmkObjectName, &moniker_data);
844 IMoniker_Release(pmkObjectName);
848 hr = MK_E_UNAVAILABLE;
850 EnterCriticalSection(&This->lock);
851 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, const struct rot_entry, entry)
853 if ((rot_entry->moniker_data->ulCntData == moniker_data->ulCntData) &&
854 !memcmp(moniker_data->abData, rot_entry->moniker_data->abData, moniker_data->ulCntData))
856 *pfiletime = rot_entry->last_modified;
861 LeaveCriticalSection(&This->lock);
869 hr = IrotGetTimeOfLastChange(get_irot_handle(), moniker_data, pfiletime);
873 hr = HRESULT_FROM_WIN32(GetExceptionCode());
876 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
885 HeapFree(GetProcessHeap(), 0, moniker_data);
887 TRACE("-- 0x%08x\n", hr);
891 /***********************************************************************
892 * RunningObjectTable_EnumRunning
895 * ppenumMoniker [out] receives the IEnumMoniker interface pointer
897 static HRESULT WINAPI
898 RunningObjectTableImpl_EnumRunning(IRunningObjectTable* iface,
899 IEnumMoniker **ppenumMoniker)
901 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
902 InterfaceList *interface_list = NULL;
905 TRACE("(%p, %p)\n", This, ppenumMoniker);
907 *ppenumMoniker = NULL;
913 hr = IrotEnumRunning(get_irot_handle(), &interface_list);
917 hr = HRESULT_FROM_WIN32(GetExceptionCode());
920 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
929 hr = EnumMonikerImpl_CreateEnumROTMoniker(interface_list,
935 /* Virtual function table for the IRunningObjectTable class. */
936 static const IRunningObjectTableVtbl VT_RunningObjectTableImpl =
938 RunningObjectTableImpl_QueryInterface,
939 RunningObjectTableImpl_AddRef,
940 RunningObjectTableImpl_Release,
941 RunningObjectTableImpl_Register,
942 RunningObjectTableImpl_Revoke,
943 RunningObjectTableImpl_IsRunning,
944 RunningObjectTableImpl_GetObject,
945 RunningObjectTableImpl_NoteChangeTime,
946 RunningObjectTableImpl_GetTimeOfLastChange,
947 RunningObjectTableImpl_EnumRunning
950 /***********************************************************************
951 * RunningObjectTable_Initialize
953 HRESULT WINAPI RunningObjectTableImpl_Initialize(void)
957 /* create the unique instance of the RunningObjectTableImpl structure */
958 runningObjectTableInstance = HeapAlloc(GetProcessHeap(), 0, sizeof(RunningObjectTableImpl));
960 if (!runningObjectTableInstance)
961 return E_OUTOFMEMORY;
963 /* initialize the virtual table function */
964 runningObjectTableInstance->lpVtbl = &VT_RunningObjectTableImpl;
966 /* the initial reference is set to "1" so that it isn't destroyed after its
967 * first use until the process is destroyed, as the running object table is
968 * a process-wide cache of a global table */
969 runningObjectTableInstance->ref = 1;
971 list_init(&runningObjectTableInstance->rot);
972 InitializeCriticalSection(&runningObjectTableInstance->lock);
973 DEBUG_SET_CRITSEC_NAME(&runningObjectTableInstance->lock, "RunningObjectTableImpl.lock");
978 /***********************************************************************
979 * RunningObjectTable_UnInitialize
981 HRESULT WINAPI RunningObjectTableImpl_UnInitialize(void)
985 if (runningObjectTableInstance==NULL)
988 RunningObjectTableImpl_Release((IRunningObjectTable*)runningObjectTableInstance);
990 RunningObjectTableImpl_Destroy();
995 /***********************************************************************
996 * GetRunningObjectTable (OLE32.@)
998 * Retrieves the global running object table.
1001 * reserved [I] Reserved. Set to 0.
1002 * pprot [O] Address that receives the pointer to the running object table.
1006 * Failure: Any HRESULT code.
1009 GetRunningObjectTable(DWORD reserved, LPRUNNINGOBJECTTABLE *pprot)
1011 IID riid=IID_IRunningObjectTable;
1017 return E_UNEXPECTED;
1019 if(runningObjectTableInstance==NULL)
1020 return CO_E_NOTINITIALIZED;
1022 res = IRunningObjectTable_QueryInterface((IRunningObjectTable*)runningObjectTableInstance,&riid,(void**)pprot);
1027 static HRESULT get_moniker_for_progid_display_name(LPBC pbc,
1028 LPCOLESTR szDisplayName,
1035 LPCWSTR start = szDisplayName;
1038 IMoniker *class_moniker;
1043 /* find end delimiter */
1044 for (end = start; *end; end++)
1050 /* must start with '@' or have a ':' somewhere and mustn't be one character
1051 * long (since that looks like an absolute path) */
1052 if (((start == szDisplayName) && (*end == '\0')) || (len <= 1))
1055 progid = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1058 memcpy(progid, start, len * sizeof(WCHAR));
1061 hr = CLSIDFromProgID(progid, &clsid);
1062 HeapFree(GetProcessHeap(), 0, progid);
1066 hr = CreateClassMoniker(&clsid, &class_moniker);
1069 IParseDisplayName *pdn;
1070 hr = IMoniker_BindToObject(class_moniker, pbc, NULL,
1071 &IID_IParseDisplayName, (void **)&pdn);
1072 /* fallback to using IClassFactory to get IParseDisplayName -
1073 * adsldp.dll depends on this */
1077 hr = IMoniker_BindToObject(class_moniker, pbc, NULL,
1078 &IID_IClassFactory, (void **)&pcf);
1081 hr = IClassFactory_CreateInstance(pcf, NULL,
1082 &IID_IParseDisplayName,
1084 IClassFactory_Release(pcf);
1087 IMoniker_Release(class_moniker);
1090 hr = IParseDisplayName_ParseDisplayName(pdn, pbc,
1091 (LPOLESTR)szDisplayName,
1093 IParseDisplayName_Release(pdn);
1099 /******************************************************************************
1100 * MkParseDisplayName [OLE32.@]
1102 HRESULT WINAPI MkParseDisplayName(LPBC pbc, LPCOLESTR szDisplayName,
1103 LPDWORD pchEaten, LPMONIKER *ppmk)
1105 HRESULT hr = MK_E_SYNTAX;
1106 static const WCHAR wszClsidColon[] = {'c','l','s','i','d',':'};
1110 TRACE("(%p, %s, %p, %p)\n", pbc, debugstr_w(szDisplayName), pchEaten, ppmk);
1112 if (!(IsValidInterface((LPUNKNOWN) pbc)))
1113 return E_INVALIDARG;
1118 if (!strncmpiW(szDisplayName, wszClsidColon, sizeof(wszClsidColon)/sizeof(wszClsidColon[0])))
1120 hr = ClassMoniker_CreateFromDisplayName(pbc, szDisplayName, &chEaten, &moniker);
1121 if (FAILED(hr) && (hr != MK_E_SYNTAX))
1126 hr = get_moniker_for_progid_display_name(pbc, szDisplayName, &chEaten, &moniker);
1127 if (FAILED(hr) && (hr != MK_E_SYNTAX))
1133 hr = FileMoniker_CreateFromDisplayName(pbc, szDisplayName, &chEaten, &moniker);
1134 if (FAILED(hr) && (hr != MK_E_SYNTAX))
1142 IMoniker *next_moniker;
1143 *pchEaten += chEaten;
1144 szDisplayName += chEaten;
1145 if (!*szDisplayName)
1151 hr = IMoniker_ParseDisplayName(moniker, pbc, NULL,
1152 (LPOLESTR)szDisplayName, &chEaten,
1154 IMoniker_Release(moniker);
1160 moniker = next_moniker;
1167 /***********************************************************************
1168 * GetClassFile (OLE32.@)
1170 * Retrieves the class ID associated with the given filename.
1173 * filePathName [I] Filename to retrieve the class ID for.
1174 * pclsid [O] Address that receives the class ID for the file.
1178 * Failure: Any HRESULT code.
1180 HRESULT WINAPI GetClassFile(LPCOLESTR filePathName,CLSID *pclsid)
1184 int nbElm, length, i;
1186 LPOLESTR *pathDec=0,absFile=0,progId=0;
1188 static const WCHAR bkslashW[] = {'\\',0};
1189 static const WCHAR dotW[] = {'.',0};
1191 TRACE("%s, %p\n", debugstr_w(filePathName), pclsid);
1193 /* if the file contain a storage object the return the CLSID written by IStorage_SetClass method*/
1194 if((StgIsStorageFile(filePathName))==S_OK){
1196 res=StgOpenStorage(filePathName,NULL,STGM_READ | STGM_SHARE_DENY_WRITE,NULL,0,&pstg);
1199 res=ReadClassStg(pstg,pclsid);
1201 IStorage_Release(pstg);
1205 /* If the file is not a storage object then attempt to match various bits in the file against a
1206 pattern in the registry. This case is not frequently used, so I present only the pseudocode for
1209 for(i=0;i<nFileTypes;i++)
1211 for(i=0;j<nPatternsForType;j++){
1216 pat=ReadPatternFromRegistry(i,j);
1217 hFile=CreateFileW(filePathName,,,,,,hFile);
1218 SetFilePosition(hFile,pat.offset);
1219 ReadFile(hFile,buf,pat.size,&r,NULL);
1220 if (memcmp(buf&pat.mask,pat.pattern.pat.size)==0){
1222 *pclsid=ReadCLSIDFromRegistry(i);
1228 /* if the above strategies fail then search for the extension key in the registry */
1230 /* get the last element (absolute file) in the path name */
1231 nbElm=FileMonikerImpl_DecomposePath(filePathName,&pathDec);
1232 absFile=pathDec[nbElm-1];
1234 /* failed if the path represents a directory and not an absolute file name*/
1235 if (!lstrcmpW(absFile, bkslashW))
1236 return MK_E_INVALIDEXTENSION;
1238 /* get the extension of the file */
1240 length=lstrlenW(absFile);
1241 for(i = length-1; (i >= 0) && *(extension = &absFile[i]) != '.'; i--)
1244 if (!extension || !lstrcmpW(extension, dotW))
1245 return MK_E_INVALIDEXTENSION;
1247 res=RegQueryValueW(HKEY_CLASSES_ROOT, extension, NULL, &sizeProgId);
1249 /* get the progId associated to the extension */
1250 progId = CoTaskMemAlloc(sizeProgId);
1251 res = RegQueryValueW(HKEY_CLASSES_ROOT, extension, progId, &sizeProgId);
1253 if (res==ERROR_SUCCESS)
1254 /* return the clsid associated to the progId */
1255 res= CLSIDFromProgID(progId,pclsid);
1257 for(i=0; pathDec[i]!=NULL;i++)
1258 CoTaskMemFree(pathDec[i]);
1259 CoTaskMemFree(pathDec);
1261 CoTaskMemFree(progId);
1263 if (res==ERROR_SUCCESS)
1266 return MK_E_INVALIDEXTENSION;
1269 /***********************************************************************
1270 * EnumMoniker_QueryInterface
1272 static HRESULT WINAPI EnumMonikerImpl_QueryInterface(IEnumMoniker* iface,REFIID riid,void** ppvObject)
1274 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1276 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
1278 /* validate arguments */
1279 if (ppvObject == NULL)
1280 return E_INVALIDARG;
1284 if (IsEqualIID(&IID_IUnknown, riid))
1287 if (IsEqualIID(&IID_IEnumMoniker, riid))
1290 if ((*ppvObject)==NULL)
1291 return E_NOINTERFACE;
1293 IEnumMoniker_AddRef(iface);
1298 /***********************************************************************
1299 * EnumMoniker_AddRef
1301 static ULONG WINAPI EnumMonikerImpl_AddRef(IEnumMoniker* iface)
1303 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1305 TRACE("(%p)\n",This);
1307 return InterlockedIncrement(&This->ref);
1310 /***********************************************************************
1311 * EnumMoniker_release
1313 static ULONG WINAPI EnumMonikerImpl_Release(IEnumMoniker* iface)
1315 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1318 TRACE("(%p)\n",This);
1320 ref = InterlockedDecrement(&This->ref);
1322 /* uninitialize rot structure if there's no more reference to it*/
1327 TRACE("(%p) Deleting\n",This);
1329 for (i = 0; i < This->moniker_list->size; i++)
1330 HeapFree(GetProcessHeap(), 0, This->moniker_list->interfaces[i]);
1331 HeapFree(GetProcessHeap(), 0, This->moniker_list);
1332 HeapFree(GetProcessHeap(), 0, This);
1337 /***********************************************************************
1340 static HRESULT WINAPI EnumMonikerImpl_Next(IEnumMoniker* iface, ULONG celt, IMoniker** rgelt, ULONG * pceltFetched)
1343 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1346 TRACE("(%p) TabCurrentPos %d Tablastindx %d\n", This, This->pos, This->moniker_list->size);
1348 /* retrieve the requested number of moniker from the current position */
1349 for(i = 0; (This->pos < This->moniker_list->size) && (i < celt); i++)
1352 hr = create_stream_on_mip_ro(This->moniker_list->interfaces[This->pos++], &stream);
1353 if (hr != S_OK) break;
1354 hr = CoUnmarshalInterface(stream, &IID_IMoniker, (void **)&rgelt[i]);
1355 IStream_Release(stream);
1356 if (hr != S_OK) break;
1359 if (pceltFetched != NULL)
1372 /***********************************************************************
1375 static HRESULT WINAPI EnumMonikerImpl_Skip(IEnumMoniker* iface, ULONG celt)
1377 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1379 TRACE("(%p)\n",This);
1381 if (This->pos + celt >= This->moniker_list->size)
1389 /***********************************************************************
1392 static HRESULT WINAPI EnumMonikerImpl_Reset(IEnumMoniker* iface)
1394 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1396 This->pos = 0; /* set back to start of list */
1398 TRACE("(%p)\n",This);
1403 /***********************************************************************
1406 static HRESULT WINAPI EnumMonikerImpl_Clone(IEnumMoniker* iface, IEnumMoniker ** ppenum)
1408 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1409 InterfaceList *moniker_list;
1412 TRACE("(%p)\n",This);
1416 moniker_list = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(InterfaceList, interfaces[This->moniker_list->size]));
1418 return E_OUTOFMEMORY;
1420 moniker_list->size = This->moniker_list->size;
1421 for (i = 0; i < This->moniker_list->size; i++)
1423 SIZE_T size = FIELD_OFFSET(InterfaceData, abData[This->moniker_list->interfaces[i]->ulCntData]);
1424 moniker_list->interfaces[i] = HeapAlloc(GetProcessHeap(), 0, size);
1425 if (!moniker_list->interfaces[i])
1428 for (i = 0; i < end; i++)
1429 HeapFree(GetProcessHeap(), 0, moniker_list->interfaces[i]);
1430 HeapFree(GetProcessHeap(), 0, moniker_list);
1431 return E_OUTOFMEMORY;
1433 memcpy(moniker_list->interfaces[i], This->moniker_list->interfaces[i], size);
1436 /* copy the enum structure */
1437 return EnumMonikerImpl_CreateEnumROTMoniker(moniker_list, This->pos, ppenum);
1440 /* Virtual function table for the IEnumMoniker class. */
1441 static const IEnumMonikerVtbl VT_EnumMonikerImpl =
1443 EnumMonikerImpl_QueryInterface,
1444 EnumMonikerImpl_AddRef,
1445 EnumMonikerImpl_Release,
1446 EnumMonikerImpl_Next,
1447 EnumMonikerImpl_Skip,
1448 EnumMonikerImpl_Reset,
1449 EnumMonikerImpl_Clone
1452 /***********************************************************************
1453 * EnumMonikerImpl_CreateEnumROTMoniker
1454 * Used by EnumRunning to create the structure and EnumClone
1455 * to copy the structure
1457 static HRESULT EnumMonikerImpl_CreateEnumROTMoniker(InterfaceList *moniker_list,
1459 IEnumMoniker **ppenumMoniker)
1461 EnumMonikerImpl* This = NULL;
1464 return E_INVALIDARG;
1466 This = HeapAlloc(GetProcessHeap(), 0, sizeof(EnumMonikerImpl));
1467 if (!This) return E_OUTOFMEMORY;
1469 TRACE("(%p)\n", This);
1471 /* initialize the virtual table function */
1472 This->lpVtbl = &VT_EnumMonikerImpl;
1474 /* the initial reference is set to "1" */
1475 This->ref = 1; /* set the ref count to one */
1476 This->pos = current_pos; /* Set the list start posn */
1477 This->moniker_list = moniker_list;
1479 *ppenumMoniker = (IEnumMoniker*)This;
1485 /* Shared implementation of moniker marshaler based on saving and loading of
1488 typedef struct MonikerMarshal
1490 const IUnknownVtbl *lpVtbl;
1491 const IMarshalVtbl *lpVtblMarshal;
1497 static inline MonikerMarshal *impl_from_IMarshal( IMarshal *iface )
1499 return (MonikerMarshal *)((char*)iface - FIELD_OFFSET(MonikerMarshal, lpVtblMarshal));
1502 static HRESULT WINAPI MonikerMarshalInner_QueryInterface(IUnknown *iface, REFIID riid, LPVOID *ppv)
1504 MonikerMarshal *This = (MonikerMarshal *)iface;
1505 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
1507 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IMarshal, riid))
1509 *ppv = &This->lpVtblMarshal;
1510 IUnknown_AddRef((IUnknown *)&This->lpVtblMarshal);
1513 FIXME("No interface for %s\n", debugstr_guid(riid));
1514 return E_NOINTERFACE;
1517 static ULONG WINAPI MonikerMarshalInner_AddRef(IUnknown *iface)
1519 MonikerMarshal *This = (MonikerMarshal *)iface;
1520 return InterlockedIncrement(&This->ref);
1523 static ULONG WINAPI MonikerMarshalInner_Release(IUnknown *iface)
1525 MonikerMarshal *This = (MonikerMarshal *)iface;
1526 ULONG ref = InterlockedDecrement(&This->ref);
1528 if (!ref) HeapFree(GetProcessHeap(), 0, This);
1532 static const IUnknownVtbl VT_MonikerMarshalInner =
1534 MonikerMarshalInner_QueryInterface,
1535 MonikerMarshalInner_AddRef,
1536 MonikerMarshalInner_Release
1539 static HRESULT WINAPI MonikerMarshal_QueryInterface(IMarshal *iface, REFIID riid, LPVOID *ppv)
1541 MonikerMarshal *This = impl_from_IMarshal(iface);
1542 return IMoniker_QueryInterface(This->moniker, riid, ppv);
1545 static ULONG WINAPI MonikerMarshal_AddRef(IMarshal *iface)
1547 MonikerMarshal *This = impl_from_IMarshal(iface);
1548 return IMoniker_AddRef(This->moniker);
1551 static ULONG WINAPI MonikerMarshal_Release(IMarshal *iface)
1553 MonikerMarshal *This = impl_from_IMarshal(iface);
1554 return IMoniker_Release(This->moniker);
1557 static HRESULT WINAPI MonikerMarshal_GetUnmarshalClass(
1558 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
1559 void* pvDestContext, DWORD mshlflags, CLSID* pCid)
1561 MonikerMarshal *This = impl_from_IMarshal(iface);
1563 TRACE("(%s, %p, %x, %p, %x, %p)\n", debugstr_guid(riid), pv,
1564 dwDestContext, pvDestContext, mshlflags, pCid);
1566 return IMoniker_GetClassID(This->moniker, pCid);
1569 static HRESULT WINAPI MonikerMarshal_GetMarshalSizeMax(
1570 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
1571 void* pvDestContext, DWORD mshlflags, DWORD* pSize)
1573 MonikerMarshal *This = impl_from_IMarshal(iface);
1575 ULARGE_INTEGER size;
1577 TRACE("(%s, %p, %x, %p, %x, %p)\n", debugstr_guid(riid), pv,
1578 dwDestContext, pvDestContext, mshlflags, pSize);
1580 hr = IMoniker_GetSizeMax(This->moniker, &size);
1582 *pSize = (DWORD)size.QuadPart;
1586 static HRESULT WINAPI MonikerMarshal_MarshalInterface(LPMARSHAL iface, IStream *pStm,
1587 REFIID riid, void* pv, DWORD dwDestContext,
1588 void* pvDestContext, DWORD mshlflags)
1590 MonikerMarshal *This = impl_from_IMarshal(iface);
1592 TRACE("(%p, %s, %p, %x, %p, %x)\n", pStm, debugstr_guid(riid), pv,
1593 dwDestContext, pvDestContext, mshlflags);
1595 return IMoniker_Save(This->moniker, pStm, FALSE);
1598 static HRESULT WINAPI MonikerMarshal_UnmarshalInterface(LPMARSHAL iface, IStream *pStm, REFIID riid, void **ppv)
1600 MonikerMarshal *This = impl_from_IMarshal(iface);
1603 TRACE("(%p, %s, %p)\n", pStm, debugstr_guid(riid), ppv);
1605 hr = IMoniker_Load(This->moniker, pStm);
1607 hr = IMoniker_QueryInterface(This->moniker, riid, ppv);
1611 static HRESULT WINAPI MonikerMarshal_ReleaseMarshalData(LPMARSHAL iface, IStream *pStm)
1614 /* can't release a state-based marshal as nothing on server side to
1619 static HRESULT WINAPI MonikerMarshal_DisconnectObject(LPMARSHAL iface, DWORD dwReserved)
1622 /* can't disconnect a state-based marshal as nothing on server side to
1623 * disconnect from */
1627 static const IMarshalVtbl VT_MonikerMarshal =
1629 MonikerMarshal_QueryInterface,
1630 MonikerMarshal_AddRef,
1631 MonikerMarshal_Release,
1632 MonikerMarshal_GetUnmarshalClass,
1633 MonikerMarshal_GetMarshalSizeMax,
1634 MonikerMarshal_MarshalInterface,
1635 MonikerMarshal_UnmarshalInterface,
1636 MonikerMarshal_ReleaseMarshalData,
1637 MonikerMarshal_DisconnectObject
1640 HRESULT MonikerMarshal_Create(IMoniker *inner, IUnknown **outer)
1642 MonikerMarshal *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1643 if (!This) return E_OUTOFMEMORY;
1645 This->lpVtbl = &VT_MonikerMarshalInner;
1646 This->lpVtblMarshal = &VT_MonikerMarshal;
1648 This->moniker = inner;
1650 *outer = (IUnknown *)&This->lpVtbl;
1654 void * __RPC_USER MIDL_user_allocate(SIZE_T size)
1656 return HeapAlloc(GetProcessHeap(), 0, size);
1659 void __RPC_USER MIDL_user_free(void *p)
1661 HeapFree(GetProcessHeap(), 0, p);