4 * Copyright 1998 Marcus Meissner
5 * Copyright 1999 Noomen Hamza
6 * Copyright 2005 Robert Shearman (for CodeWeavers)
7 * Copyright 2007 Robert Shearman
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 * - 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.
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"
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 (GetExceptionCode())
60 case EXCEPTION_ACCESS_VIOLATION:
61 case EXCEPTION_ILLEGAL_INSTRUCTION:
62 return EXCEPTION_CONTINUE_SEARCH;
64 return EXCEPTION_EXECUTE_HANDLER;
68 /* define the structure of the running object table elements */
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;
78 /* define the RunningObjectTableImpl structure */
79 typedef struct RunningObjectTableImpl
81 const IRunningObjectTableVtbl *lpVtbl;
84 struct list rot; /* list of ROT entries */
85 CRITICAL_SECTION lock;
86 } RunningObjectTableImpl;
88 static RunningObjectTableImpl* runningObjectTableInstance = NULL;
89 static IrotHandle irot_handle;
91 /* define the EnumMonikerImpl structure */
92 typedef struct EnumMonikerImpl
94 const IEnumMonikerVtbl *lpVtbl;
97 InterfaceData **monikers;
103 /* IEnumMoniker Local functions*/
104 static HRESULT WINAPI EnumMonikerImpl_CreateEnumROTMoniker(InterfaceData **monikers,
105 ULONG moniker_count, ULONG pos, IEnumMoniker **ppenumMoniker);
107 static IrotHandle get_irot_handle(void)
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)
124 if (InterlockedCompareExchangePointer(&irot_handle, new_handle, NULL))
125 /* another thread beat us to it */
126 RpcBindingFree(&new_handle);
131 static BOOL start_rpcss(void)
133 PROCESS_INFORMATION pi;
136 static const WCHAR rpcss[] = {'r','p','c','s','s',0};
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 */
162 CloseHandle(pi.hProcess);
163 CloseHandle(pi.hThread);
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;
187 IrotRevoke(get_irot_handle(), rot_entry->cookie, &object, &moniker);
193 MIDL_user_free(object);
198 hr = create_stream_on_mip_ro(moniker, &stream);
201 CoReleaseMarshalData(stream);
202 IUnknown_Release(stream);
205 MIDL_user_free(moniker);
207 if (rot_entry->object)
211 hr = create_stream_on_mip_ro(rot_entry->object, &stream);
214 CoReleaseMarshalData(stream);
215 IUnknown_Release(stream);
218 HeapFree(GetProcessHeap(), 0, rot_entry->object);
219 HeapFree(GetProcessHeap(), 0, rot_entry->moniker_data);
220 HeapFree(GetProcessHeap(), 0, rot_entry);
223 /* moniker_data must be freed with HeapFree when no longer in use */
224 static HRESULT get_moniker_comparison_data(IMoniker *pMoniker, MonikerComparisonData **moniker_data)
227 IROTData *pROTData = NULL;
228 hr = IMoniker_QueryInterface(pMoniker, &IID_IROTData, (void *)&pROTData);
231 ULONG size = MAX_COMPARISON_DATA;
232 *moniker_data = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(MonikerComparisonData, abData[size]));
235 IROTData_Release(pROTData);
236 return E_OUTOFMEMORY;
238 hr = IROTData_GetComparisonData(pROTData, (*moniker_data)->abData, size, &size);
239 IROTData_Release(pROTData);
242 ERR("Failed to copy comparison data into buffer, hr = 0x%08x\n", hr);
243 HeapFree(GetProcessHeap(), 0, *moniker_data);
246 (*moniker_data)->ulCntData = size;
251 LPOLESTR pszDisplayName;
255 TRACE("generating comparison data from display name\n");
257 hr = CreateBindCtx(0, &pbc);
260 hr = IMoniker_GetDisplayName(pMoniker, pbc, NULL, &pszDisplayName);
261 IBindCtx_Release(pbc);
264 hr = IMoniker_GetClassID(pMoniker, &clsid);
267 CoTaskMemFree(pszDisplayName);
271 len = strlenW(pszDisplayName);
272 *moniker_data = HeapAlloc(GetProcessHeap(), 0,
273 FIELD_OFFSET(MonikerComparisonData, abData[sizeof(CLSID) + (len+1)*sizeof(WCHAR)]));
276 CoTaskMemFree(pszDisplayName);
277 return E_OUTOFMEMORY;
279 (*moniker_data)->ulCntData = sizeof(CLSID) + (len+1)*sizeof(WCHAR);
281 memcpy(&(*moniker_data)->abData[0], &clsid, sizeof(clsid));
282 memcpy(&(*moniker_data)->abData[sizeof(clsid)], pszDisplayName, (len+1)*sizeof(WCHAR));
287 static HRESULT reduce_moniker(IMoniker *pmk, IBindCtx *pbc, IMoniker **pmkReduced)
289 IBindCtx *pbcNew = NULL;
293 hr = CreateBindCtx(0, &pbcNew);
298 hr = IMoniker_Reduce(pmk, pbc, MKRREDUCE_ALL, NULL, pmkReduced);
300 ERR("reducing moniker failed with error 0x%08x\n", hr);
301 if (pbcNew) IBindCtx_Release(pbcNew);
305 /***********************************************************************
306 * RunningObjectTable_QueryInterface
308 static HRESULT WINAPI
309 RunningObjectTableImpl_QueryInterface(IRunningObjectTable* iface,
310 REFIID riid,void** ppvObject)
312 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
314 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
316 /* validate arguments */
323 if (IsEqualIID(&IID_IUnknown, riid) ||
324 IsEqualIID(&IID_IRunningObjectTable, riid))
325 *ppvObject = (IRunningObjectTable*)This;
328 return E_NOINTERFACE;
330 IRunningObjectTable_AddRef(iface);
335 /***********************************************************************
336 * RunningObjectTable_AddRef
339 RunningObjectTableImpl_AddRef(IRunningObjectTable* iface)
341 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
343 TRACE("(%p)\n",This);
345 return InterlockedIncrement(&This->ref);
348 /***********************************************************************
349 * RunningObjectTable_Initialize
351 static HRESULT WINAPI
352 RunningObjectTableImpl_Destroy(void)
354 struct list *cursor, *cursor2;
355 IrotHandle old_handle;
359 if (runningObjectTableInstance==NULL)
362 /* free the ROT table memory */
363 LIST_FOR_EACH_SAFE(cursor, cursor2, &runningObjectTableInstance->rot)
365 struct rot_entry *rot_entry = LIST_ENTRY(cursor, struct rot_entry, entry);
366 list_remove(&rot_entry->entry);
367 rot_entry_delete(rot_entry);
370 DEBUG_CLEAR_CRITSEC_NAME(&runningObjectTableInstance->lock);
371 DeleteCriticalSection(&runningObjectTableInstance->lock);
373 /* free the ROT structure memory */
374 HeapFree(GetProcessHeap(),0,runningObjectTableInstance);
375 runningObjectTableInstance = NULL;
377 old_handle = irot_handle;
380 RpcBindingFree(&old_handle);
385 /***********************************************************************
386 * RunningObjectTable_Release
389 RunningObjectTableImpl_Release(IRunningObjectTable* iface)
391 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
394 TRACE("(%p)\n",This);
396 ref = InterlockedDecrement(&This->ref);
398 /* uninitialize ROT structure if there's no more references to it */
401 struct list *cursor, *cursor2;
402 LIST_FOR_EACH_SAFE(cursor, cursor2, &This->rot)
404 struct rot_entry *rot_entry = LIST_ENTRY(cursor, struct rot_entry, entry);
405 list_remove(&rot_entry->entry);
406 rot_entry_delete(rot_entry);
408 /* RunningObjectTable data structure will be not destroyed here ! the destruction will be done only
409 * when RunningObjectTableImpl_UnInitialize function is called
416 /***********************************************************************
417 * RunningObjectTable_Register
420 * grfFlags [in] Registration options
421 * punkObject [in] the object being registered
422 * pmkObjectName [in] the moniker of the object being registered
423 * pdwRegister [in] the value identifying the registration
425 static HRESULT WINAPI
426 RunningObjectTableImpl_Register(IRunningObjectTable* iface, DWORD grfFlags,
427 IUnknown *punkObject, IMoniker *pmkObjectName, DWORD *pdwRegister)
429 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
430 struct rot_entry *rot_entry;
432 IStream *pStream = NULL;
435 InterfaceData *moniker = NULL;
437 TRACE("(%p,%d,%p,%p,%p)\n",This,grfFlags,punkObject,pmkObjectName,pdwRegister);
439 if (grfFlags & ~(ROTFLAGS_REGISTRATIONKEEPSALIVE|ROTFLAGS_ALLOWANYCLIENT))
441 ERR("Invalid grfFlags: 0x%08x\n", grfFlags & ~(ROTFLAGS_REGISTRATIONKEEPSALIVE|ROTFLAGS_ALLOWANYCLIENT));
445 if (punkObject==NULL || pmkObjectName==NULL || pdwRegister==NULL)
448 rot_entry = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*rot_entry));
450 return E_OUTOFMEMORY;
453 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
456 rot_entry_delete(rot_entry);
459 mshlflags = (grfFlags & ROTFLAGS_REGISTRATIONKEEPSALIVE) ? MSHLFLAGS_TABLESTRONG : MSHLFLAGS_TABLEWEAK;
460 hr = CoMarshalInterface(pStream, &IID_IUnknown, punkObject, MSHCTX_LOCAL | MSHCTX_NOSHAREDMEM, NULL, mshlflags);
461 /* FIXME: a cleaner way would be to create an IStream class that writes
462 * directly to an MInterfacePointer */
466 hr = GetHGlobalFromStream(pStream, &hglobal);
469 SIZE_T size = GlobalSize(hglobal);
470 const void *pv = GlobalLock(hglobal);
471 rot_entry->object = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(MInterfacePointer, abData[size]));
472 rot_entry->object->ulCntData = size;
473 memcpy(&rot_entry->object->abData, pv, size);
474 GlobalUnlock(hglobal);
477 IStream_Release(pStream);
480 rot_entry_delete(rot_entry);
484 hr = CreateBindCtx(0, &pbc);
487 rot_entry_delete(rot_entry);
491 hr = reduce_moniker(pmkObjectName, pbc, &pmkObjectName);
494 rot_entry_delete(rot_entry);
495 IBindCtx_Release(pbc);
499 hr = IMoniker_GetTimeOfLastChange(pmkObjectName, pbc, NULL,
500 &rot_entry->last_modified);
501 IBindCtx_Release(pbc);
504 CoFileTimeNow(&rot_entry->last_modified);
508 hr = get_moniker_comparison_data(pmkObjectName,
509 &rot_entry->moniker_data);
512 rot_entry_delete(rot_entry);
513 IMoniker_Release(pmkObjectName);
517 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
520 rot_entry_delete(rot_entry);
521 IMoniker_Release(pmkObjectName);
524 /* marshal moniker */
525 hr = CoMarshalInterface(pStream, &IID_IMoniker, (IUnknown *)pmkObjectName,
526 MSHCTX_LOCAL | MSHCTX_NOSHAREDMEM, NULL, MSHLFLAGS_TABLESTRONG);
527 /* FIXME: a cleaner way would be to create an IStream class that writes
528 * directly to an MInterfacePointer */
532 hr = GetHGlobalFromStream(pStream, &hglobal);
535 SIZE_T size = GlobalSize(hglobal);
536 const void *pv = GlobalLock(hglobal);
537 moniker = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(InterfaceData, abData[size]));
538 moniker->ulCntData = size;
539 memcpy(&moniker->abData, pv, size);
540 GlobalUnlock(hglobal);
543 IStream_Release(pStream);
544 IMoniker_Release(pmkObjectName);
547 rot_entry_delete(rot_entry);
556 hr = IrotRegister(get_irot_handle(), rot_entry->moniker_data, rot_entry->object, moniker, &rot_entry->last_modified, grfFlags, &rot_entry->cookie);
560 hr = HRESULT_FROM_WIN32(GetExceptionCode());
563 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
572 rot_entry_delete(rot_entry);
576 /* gives a registration identifier to the registered object*/
577 *pdwRegister = rot_entry->cookie;
579 EnterCriticalSection(&This->lock);
580 list_add_tail(&This->rot, &rot_entry->entry);
581 LeaveCriticalSection(&This->lock);
586 /***********************************************************************
587 * RunningObjectTable_Revoke
590 * dwRegister [in] Value identifying registration to be revoked
592 static HRESULT WINAPI
593 RunningObjectTableImpl_Revoke( IRunningObjectTable* iface, DWORD dwRegister)
595 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
596 struct rot_entry *rot_entry;
598 TRACE("(%p,%d)\n",This,dwRegister);
600 EnterCriticalSection(&This->lock);
601 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, struct rot_entry, entry)
603 if (rot_entry->cookie == dwRegister)
605 list_remove(&rot_entry->entry);
606 LeaveCriticalSection(&This->lock);
608 rot_entry_delete(rot_entry);
612 LeaveCriticalSection(&This->lock);
617 /***********************************************************************
618 * RunningObjectTable_IsRunning
621 * pmkObjectName [in] moniker of the object whose status is desired
623 static HRESULT WINAPI
624 RunningObjectTableImpl_IsRunning( IRunningObjectTable* iface, IMoniker *pmkObjectName)
626 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
627 MonikerComparisonData *moniker_data;
629 const struct rot_entry *rot_entry;
631 TRACE("(%p,%p)\n",This,pmkObjectName);
633 hr = reduce_moniker(pmkObjectName, NULL, &pmkObjectName);
636 hr = get_moniker_comparison_data(pmkObjectName, &moniker_data);
637 IMoniker_Release(pmkObjectName);
642 EnterCriticalSection(&This->lock);
643 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, const struct rot_entry, entry)
645 if ((rot_entry->moniker_data->ulCntData == moniker_data->ulCntData) &&
646 !memcmp(&moniker_data->abData, &rot_entry->moniker_data->abData, moniker_data->ulCntData))
652 LeaveCriticalSection(&This->lock);
660 hr = IrotIsRunning(get_irot_handle(), moniker_data);
664 hr = HRESULT_FROM_WIN32(GetExceptionCode());
667 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
676 HeapFree(GetProcessHeap(), 0, moniker_data);
681 /***********************************************************************
682 * RunningObjectTable_GetObject
685 * pmkObjectName [in] Pointer to the moniker on the object
686 * ppunkObject [out] variable that receives the IUnknown interface pointer
688 static HRESULT WINAPI
689 RunningObjectTableImpl_GetObject( IRunningObjectTable* iface,
690 IMoniker *pmkObjectName, IUnknown **ppunkObject)
692 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
693 MonikerComparisonData *moniker_data;
694 InterfaceData *object;
697 struct rot_entry *rot_entry;
699 TRACE("(%p,%p,%p)\n",This,pmkObjectName,ppunkObject);
701 if (ppunkObject == NULL)
706 hr = reduce_moniker(pmkObjectName, NULL, &pmkObjectName);
709 hr = get_moniker_comparison_data(pmkObjectName, &moniker_data);
710 IMoniker_Release(pmkObjectName);
714 EnterCriticalSection(&This->lock);
715 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, struct rot_entry, entry)
717 if ((rot_entry->moniker_data->ulCntData == moniker_data->ulCntData) &&
718 !memcmp(&moniker_data->abData, &rot_entry->moniker_data->abData, moniker_data->ulCntData))
721 hr = create_stream_on_mip_ro(rot_entry->object, &pStream);
724 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)ppunkObject);
725 IStream_Release(pStream);
728 LeaveCriticalSection(&This->lock);
729 HeapFree(GetProcessHeap(), 0, moniker_data);
734 LeaveCriticalSection(&This->lock);
736 TRACE("moniker unavailable locally, calling SCM\n");
742 hr = IrotGetObject(get_irot_handle(), moniker_data, &object, &cookie);
746 hr = HRESULT_FROM_WIN32(GetExceptionCode());
749 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
760 hr = create_stream_on_mip_ro(object, &pStream);
763 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)ppunkObject);
764 IStream_Release(pStream);
768 WARN("Moniker unavailable, IrotGetObject returned 0x%08x\n", hr);
770 HeapFree(GetProcessHeap(), 0, moniker_data);
775 /***********************************************************************
776 * RunningObjectTable_NoteChangeTime
779 * dwRegister [in] Value identifying registration being updated
780 * pfiletime [in] Pointer to structure containing object's last change time
782 static HRESULT WINAPI
783 RunningObjectTableImpl_NoteChangeTime(IRunningObjectTable* iface,
784 DWORD dwRegister, FILETIME *pfiletime)
786 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
787 struct rot_entry *rot_entry;
788 HRESULT hr = E_INVALIDARG;
790 TRACE("(%p,%d,%p)\n",This,dwRegister,pfiletime);
792 EnterCriticalSection(&This->lock);
793 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, struct rot_entry, entry)
795 if (rot_entry->cookie == dwRegister)
797 rot_entry->last_modified = *pfiletime;
798 LeaveCriticalSection(&This->lock);
804 hr = IrotNoteChangeTime(get_irot_handle(), dwRegister, pfiletime);
808 hr = HRESULT_FROM_WIN32(GetExceptionCode());
811 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
821 LeaveCriticalSection(&This->lock);
823 TRACE("-- 0x08%x\n", hr);
827 /***********************************************************************
828 * RunningObjectTable_GetTimeOfLastChange
831 * pmkObjectName [in] moniker of the object whose status is desired
832 * pfiletime [out] structure that receives object's last change time
834 static HRESULT WINAPI
835 RunningObjectTableImpl_GetTimeOfLastChange(IRunningObjectTable* iface,
836 IMoniker *pmkObjectName, FILETIME *pfiletime)
838 HRESULT hr = MK_E_UNAVAILABLE;
839 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
840 MonikerComparisonData *moniker_data;
841 const struct rot_entry *rot_entry;
843 TRACE("(%p,%p,%p)\n",This,pmkObjectName,pfiletime);
845 if (pmkObjectName==NULL || pfiletime==NULL)
848 hr = reduce_moniker(pmkObjectName, NULL, &pmkObjectName);
851 hr = get_moniker_comparison_data(pmkObjectName, &moniker_data);
852 IMoniker_Release(pmkObjectName);
856 hr = MK_E_UNAVAILABLE;
858 EnterCriticalSection(&This->lock);
859 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, const struct rot_entry, entry)
861 if ((rot_entry->moniker_data->ulCntData == moniker_data->ulCntData) &&
862 !memcmp(&moniker_data->abData, &rot_entry->moniker_data->abData, moniker_data->ulCntData))
864 *pfiletime = rot_entry->last_modified;
869 LeaveCriticalSection(&This->lock);
877 hr = IrotGetTimeOfLastChange(get_irot_handle(), moniker_data, pfiletime);
881 hr = HRESULT_FROM_WIN32(GetExceptionCode());
884 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
893 HeapFree(GetProcessHeap(), 0, moniker_data);
895 TRACE("-- 0x%08x\n", hr);
899 /***********************************************************************
900 * RunningObjectTable_EnumRunning
903 * ppenumMoniker [out] receives the IEnumMoniker interface pointer
905 static HRESULT WINAPI
906 RunningObjectTableImpl_EnumRunning(IRunningObjectTable* iface,
907 IEnumMoniker **ppenumMoniker)
909 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
910 InterfaceList *interface_list = NULL;
913 TRACE("(%p, %p)\n", This, ppenumMoniker);
915 *ppenumMoniker = NULL;
921 hr = IrotEnumRunning(get_irot_handle(), &interface_list);
925 hr = HRESULT_FROM_WIN32(GetExceptionCode());
928 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
937 hr = EnumMonikerImpl_CreateEnumROTMoniker(interface_list->interfaces,
938 interface_list->size,
944 /* Virtual function table for the IRunningObjectTable class. */
945 static const IRunningObjectTableVtbl VT_RunningObjectTableImpl =
947 RunningObjectTableImpl_QueryInterface,
948 RunningObjectTableImpl_AddRef,
949 RunningObjectTableImpl_Release,
950 RunningObjectTableImpl_Register,
951 RunningObjectTableImpl_Revoke,
952 RunningObjectTableImpl_IsRunning,
953 RunningObjectTableImpl_GetObject,
954 RunningObjectTableImpl_NoteChangeTime,
955 RunningObjectTableImpl_GetTimeOfLastChange,
956 RunningObjectTableImpl_EnumRunning
959 /***********************************************************************
960 * RunningObjectTable_Initialize
962 HRESULT WINAPI RunningObjectTableImpl_Initialize(void)
966 /* create the unique instance of the RunningObjectTableImpl structure */
967 runningObjectTableInstance = HeapAlloc(GetProcessHeap(), 0, sizeof(RunningObjectTableImpl));
969 if (!runningObjectTableInstance)
970 return E_OUTOFMEMORY;
972 /* initialize the virtual table function */
973 runningObjectTableInstance->lpVtbl = &VT_RunningObjectTableImpl;
975 /* the initial reference is set to "1" ! because if set to "0" it will be not practis when */
976 /* the ROT referred many times not in the same time (all the objects in the ROT will */
977 /* be removed every time the ROT is removed ) */
978 runningObjectTableInstance->ref = 1;
980 list_init(&runningObjectTableInstance->rot);
981 InitializeCriticalSection(&runningObjectTableInstance->lock);
982 DEBUG_SET_CRITSEC_NAME(&runningObjectTableInstance->lock, "RunningObjectTableImpl.lock");
987 /***********************************************************************
988 * RunningObjectTable_UnInitialize
990 HRESULT WINAPI RunningObjectTableImpl_UnInitialize(void)
994 if (runningObjectTableInstance==NULL)
997 RunningObjectTableImpl_Release((IRunningObjectTable*)runningObjectTableInstance);
999 RunningObjectTableImpl_Destroy();
1004 /***********************************************************************
1005 * GetRunningObjectTable (OLE32.@)
1007 * Retrieves the global running object table.
1010 * reserved [I] Reserved. Set to 0.
1011 * pprot [O] Address that receives the pointer to the running object table.
1015 * Failure: Any HRESULT code.
1018 GetRunningObjectTable(DWORD reserved, LPRUNNINGOBJECTTABLE *pprot)
1020 IID riid=IID_IRunningObjectTable;
1026 return E_UNEXPECTED;
1028 if(runningObjectTableInstance==NULL)
1029 return CO_E_NOTINITIALIZED;
1031 res = IRunningObjectTable_QueryInterface((IRunningObjectTable*)runningObjectTableInstance,&riid,(void**)pprot);
1036 static HRESULT get_moniker_for_progid_display_name(LPBC pbc,
1037 LPCOLESTR szDisplayName,
1044 LPCWSTR start = szDisplayName;
1047 IMoniker *class_moniker;
1052 /* find end delimiter */
1053 for (end = start; *end; end++)
1059 /* must start with '@' or have a ':' somewhere and mustn't be one character
1060 * long (since that looks like an absolute path) */
1061 if (((start == szDisplayName) && (*end == '\0')) || (len <= 1))
1064 progid = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1067 memcpy(progid, start, len * sizeof(WCHAR));
1070 hr = CLSIDFromProgID(progid, &clsid);
1071 HeapFree(GetProcessHeap(), 0, progid);
1075 hr = CreateClassMoniker(&clsid, &class_moniker);
1078 IParseDisplayName *pdn;
1079 hr = IMoniker_BindToObject(class_moniker, pbc, NULL,
1080 &IID_IParseDisplayName, (void **)&pdn);
1081 IMoniker_Release(class_moniker);
1084 hr = IParseDisplayName_ParseDisplayName(pdn, pbc,
1085 (LPOLESTR)szDisplayName,
1087 IParseDisplayName_Release(pdn);
1093 /******************************************************************************
1094 * MkParseDisplayName [OLE32.@]
1096 HRESULT WINAPI MkParseDisplayName(LPBC pbc, LPCOLESTR szDisplayName,
1097 LPDWORD pchEaten, LPMONIKER *ppmk)
1099 HRESULT hr = MK_E_SYNTAX;
1100 static const WCHAR wszClsidColon[] = {'c','l','s','i','d',':'};
1104 TRACE("(%p, %s, %p, %p)\n", pbc, debugstr_w(szDisplayName), pchEaten, ppmk);
1106 if (!(IsValidInterface((LPUNKNOWN) pbc)))
1107 return E_INVALIDARG;
1112 if (!strncmpiW(szDisplayName, wszClsidColon, sizeof(wszClsidColon)/sizeof(wszClsidColon[0])))
1114 hr = ClassMoniker_CreateFromDisplayName(pbc, szDisplayName, &chEaten, &moniker);
1115 if (FAILED(hr) && (hr != MK_E_SYNTAX))
1120 hr = get_moniker_for_progid_display_name(pbc, szDisplayName, &chEaten, &moniker);
1121 if (FAILED(hr) && (hr != MK_E_SYNTAX))
1127 hr = FileMoniker_CreateFromDisplayName(pbc, szDisplayName, &chEaten, &moniker);
1128 if (FAILED(hr) && (hr != MK_E_SYNTAX))
1136 IMoniker *next_moniker;
1137 *pchEaten += chEaten;
1138 szDisplayName += chEaten;
1139 if (!*szDisplayName)
1145 hr = IMoniker_ParseDisplayName(moniker, pbc, NULL,
1146 (LPOLESTR)szDisplayName, &chEaten,
1148 IMoniker_Release(moniker);
1154 moniker = next_moniker;
1161 /***********************************************************************
1162 * GetClassFile (OLE32.@)
1164 * Retrieves the class ID associated with the given filename.
1167 * filePathName [I] Filename to retrieve the class ID for.
1168 * pclsid [O] Address that receives the class ID for the file.
1172 * Failure: Any HRESULT code.
1174 HRESULT WINAPI GetClassFile(LPCOLESTR filePathName,CLSID *pclsid)
1178 int nbElm, length, i;
1180 LPOLESTR *pathDec=0,absFile=0,progId=0;
1182 static const WCHAR bkslashW[] = {'\\',0};
1183 static const WCHAR dotW[] = {'.',0};
1185 TRACE("%s, %p\n", debugstr_w(filePathName), pclsid);
1187 /* if the file contain a storage object the return the CLSID written by IStorage_SetClass method*/
1188 if((StgIsStorageFile(filePathName))==S_OK){
1190 res=StgOpenStorage(filePathName,NULL,STGM_READ | STGM_SHARE_DENY_WRITE,NULL,0,&pstg);
1193 res=ReadClassStg(pstg,pclsid);
1195 IStorage_Release(pstg);
1199 /* if the file is not a storage object then attemps to match various bits in the file against a
1200 pattern in the registry. this case is not frequently used ! so I present only the psodocode for
1203 for(i=0;i<nFileTypes;i++)
1205 for(i=0;j<nPatternsForType;j++){
1210 pat=ReadPatternFromRegistry(i,j);
1211 hFile=CreateFileW(filePathName,,,,,,hFile);
1212 SetFilePosition(hFile,pat.offset);
1213 ReadFile(hFile,buf,pat.size,&r,NULL);
1214 if (memcmp(buf&pat.mask,pat.pattern.pat.size)==0){
1216 *pclsid=ReadCLSIDFromRegistry(i);
1222 /* if the above strategies fail then search for the extension key in the registry */
1224 /* get the last element (absolute file) in the path name */
1225 nbElm=FileMonikerImpl_DecomposePath(filePathName,&pathDec);
1226 absFile=pathDec[nbElm-1];
1228 /* failed if the path represente a directory and not an absolute file name*/
1229 if (!lstrcmpW(absFile, bkslashW))
1230 return MK_E_INVALIDEXTENSION;
1232 /* get the extension of the file */
1234 length=lstrlenW(absFile);
1235 for(i = length-1; (i >= 0) && *(extension = &absFile[i]) != '.'; i--)
1238 if (!extension || !lstrcmpW(extension, dotW))
1239 return MK_E_INVALIDEXTENSION;
1241 res=RegQueryValueW(HKEY_CLASSES_ROOT, extension, NULL, &sizeProgId);
1243 /* get the progId associated to the extension */
1244 progId = CoTaskMemAlloc(sizeProgId);
1245 res = RegQueryValueW(HKEY_CLASSES_ROOT, extension, progId, &sizeProgId);
1247 if (res==ERROR_SUCCESS)
1248 /* return the clsid associated to the progId */
1249 res= CLSIDFromProgID(progId,pclsid);
1251 for(i=0; pathDec[i]!=NULL;i++)
1252 CoTaskMemFree(pathDec[i]);
1253 CoTaskMemFree(pathDec);
1255 CoTaskMemFree(progId);
1257 if (res==ERROR_SUCCESS)
1260 return MK_E_INVALIDEXTENSION;
1263 /***********************************************************************
1264 * EnumMoniker_QueryInterface
1266 static HRESULT WINAPI EnumMonikerImpl_QueryInterface(IEnumMoniker* iface,REFIID riid,void** ppvObject)
1268 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1270 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
1272 /* validate arguments */
1273 if (ppvObject == NULL)
1274 return E_INVALIDARG;
1278 if (IsEqualIID(&IID_IUnknown, riid))
1279 *ppvObject = (IEnumMoniker*)This;
1281 if (IsEqualIID(&IID_IEnumMoniker, riid))
1282 *ppvObject = (IEnumMoniker*)This;
1284 if ((*ppvObject)==NULL)
1285 return E_NOINTERFACE;
1287 IEnumMoniker_AddRef(iface);
1292 /***********************************************************************
1293 * EnumMoniker_AddRef
1295 static ULONG WINAPI EnumMonikerImpl_AddRef(IEnumMoniker* iface)
1297 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1299 TRACE("(%p)\n",This);
1301 return InterlockedIncrement(&This->ref);
1304 /***********************************************************************
1305 * EnumMoniker_release
1307 static ULONG WINAPI EnumMonikerImpl_Release(IEnumMoniker* iface)
1309 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1312 TRACE("(%p)\n",This);
1314 ref = InterlockedDecrement(&This->ref);
1316 /* uninitialize rot structure if there's no more reference to it*/
1321 TRACE("(%p) Deleting\n",This);
1323 for (i = 0; i < This->moniker_count; i++)
1324 HeapFree(GetProcessHeap(), 0, This->monikers[i]);
1325 HeapFree(GetProcessHeap(), 0, This->monikers);
1326 HeapFree(GetProcessHeap(), 0, This);
1331 /***********************************************************************
1334 static HRESULT WINAPI EnumMonikerImpl_Next(IEnumMoniker* iface, ULONG celt, IMoniker** rgelt, ULONG * pceltFetched)
1337 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1340 TRACE("(%p) TabCurrentPos %d Tablastindx %d\n", This, This->pos, This->moniker_count);
1342 /* retrieve the requested number of moniker from the current position */
1343 for(i = 0; (This->pos < This->moniker_count) && (i < celt); i++)
1346 hr = create_stream_on_mip_ro(This->monikers[This->pos++], &stream);
1347 if (hr != S_OK) break;
1348 hr = CoUnmarshalInterface(stream, &IID_IMoniker, (void **)&rgelt[i]);
1349 IStream_Release(stream);
1350 if (hr != S_OK) break;
1353 if (pceltFetched != NULL)
1366 /***********************************************************************
1369 static HRESULT WINAPI EnumMonikerImpl_Skip(IEnumMoniker* iface, ULONG celt)
1371 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1373 TRACE("(%p)\n",This);
1375 if (This->pos + celt >= This->moniker_count)
1383 /***********************************************************************
1384 * EnmumMoniker_Reset
1386 static HRESULT WINAPI EnumMonikerImpl_Reset(IEnumMoniker* iface)
1388 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1390 This->pos = 0; /* set back to start of list */
1392 TRACE("(%p)\n",This);
1397 /***********************************************************************
1398 * EnmumMoniker_Clone
1400 static HRESULT WINAPI EnumMonikerImpl_Clone(IEnumMoniker* iface, IEnumMoniker ** ppenum)
1402 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1403 InterfaceData **monikers;
1406 TRACE("(%p)\n",This);
1410 monikers = HeapAlloc(GetProcessHeap(), 0, sizeof(*monikers)*This->moniker_count);
1412 return E_OUTOFMEMORY;
1414 for (i = 0; i < This->moniker_count; i++)
1416 SIZE_T size = FIELD_OFFSET(InterfaceData, abData[This->monikers[i]->ulCntData]);
1417 monikers[i] = HeapAlloc(GetProcessHeap(), 0, size);
1421 for (i = 0; i < end; i++)
1422 HeapFree(GetProcessHeap(), 0, monikers[i]);
1423 HeapFree(GetProcessHeap(), 0, monikers);
1424 return E_OUTOFMEMORY;
1426 memcpy(monikers[i], This->monikers[i], size);
1429 /* copy the enum structure */
1430 return EnumMonikerImpl_CreateEnumROTMoniker(monikers, This->moniker_count,
1434 /* Virtual function table for the IEnumMoniker class. */
1435 static const IEnumMonikerVtbl VT_EnumMonikerImpl =
1437 EnumMonikerImpl_QueryInterface,
1438 EnumMonikerImpl_AddRef,
1439 EnumMonikerImpl_Release,
1440 EnumMonikerImpl_Next,
1441 EnumMonikerImpl_Skip,
1442 EnumMonikerImpl_Reset,
1443 EnumMonikerImpl_Clone
1446 /***********************************************************************
1447 * EnumMonikerImpl_CreateEnumROTMoniker
1448 * Used by EnumRunning to create the structure and EnumClone
1449 * to copy the structure
1451 static HRESULT WINAPI EnumMonikerImpl_CreateEnumROTMoniker(InterfaceData **monikers,
1452 ULONG moniker_count,
1454 IEnumMoniker **ppenumMoniker)
1456 EnumMonikerImpl* This = NULL;
1459 return E_INVALIDARG;
1461 This = HeapAlloc(GetProcessHeap(), 0, sizeof(EnumMonikerImpl));
1462 if (!This) return E_OUTOFMEMORY;
1464 TRACE("(%p)\n", This);
1466 /* initialize the virtual table function */
1467 This->lpVtbl = &VT_EnumMonikerImpl;
1469 /* the initial reference is set to "1" */
1470 This->ref = 1; /* set the ref count to one */
1471 This->pos = current_pos; /* Set the list start posn */
1472 This->moniker_count = moniker_count; /* Need the same size table as ROT */
1473 This->monikers = monikers;
1475 *ppenumMoniker = (IEnumMoniker*)This;
1481 /* Shared implementation of moniker marshaler based on saving and loading of
1484 typedef struct MonikerMarshal
1486 const IUnknownVtbl *lpVtbl;
1487 const IMarshalVtbl *lpVtblMarshal;
1493 static inline MonikerMarshal *impl_from_IMarshal( IMarshal *iface )
1495 return (MonikerMarshal *)((char*)iface - FIELD_OFFSET(MonikerMarshal, lpVtblMarshal));
1498 static HRESULT WINAPI MonikerMarshalInner_QueryInterface(IUnknown *iface, REFIID riid, LPVOID *ppv)
1500 MonikerMarshal *This = (MonikerMarshal *)iface;
1501 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
1503 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IMarshal, riid))
1505 *ppv = &This->lpVtblMarshal;
1506 IUnknown_AddRef((IUnknown *)&This->lpVtblMarshal);
1509 FIXME("No interface for %s\n", debugstr_guid(riid));
1510 return E_NOINTERFACE;
1513 static ULONG WINAPI MonikerMarshalInner_AddRef(IUnknown *iface)
1515 MonikerMarshal *This = (MonikerMarshal *)iface;
1516 return InterlockedIncrement(&This->ref);
1519 static ULONG WINAPI MonikerMarshalInner_Release(IUnknown *iface)
1521 MonikerMarshal *This = (MonikerMarshal *)iface;
1522 ULONG ref = InterlockedDecrement(&This->ref);
1524 if (!ref) HeapFree(GetProcessHeap(), 0, This);
1528 static const IUnknownVtbl VT_MonikerMarshalInner =
1530 MonikerMarshalInner_QueryInterface,
1531 MonikerMarshalInner_AddRef,
1532 MonikerMarshalInner_Release
1535 static HRESULT WINAPI MonikerMarshal_QueryInterface(IMarshal *iface, REFIID riid, LPVOID *ppv)
1537 MonikerMarshal *This = impl_from_IMarshal(iface);
1538 return IMoniker_QueryInterface(This->moniker, riid, ppv);
1541 static ULONG WINAPI MonikerMarshal_AddRef(IMarshal *iface)
1543 MonikerMarshal *This = impl_from_IMarshal(iface);
1544 return IMoniker_AddRef(This->moniker);
1547 static ULONG WINAPI MonikerMarshal_Release(IMarshal *iface)
1549 MonikerMarshal *This = impl_from_IMarshal(iface);
1550 return IMoniker_Release(This->moniker);
1553 static HRESULT WINAPI MonikerMarshal_GetUnmarshalClass(
1554 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
1555 void* pvDestContext, DWORD mshlflags, CLSID* pCid)
1557 MonikerMarshal *This = impl_from_IMarshal(iface);
1559 TRACE("(%s, %p, %x, %p, %x, %p)\n", debugstr_guid(riid), pv,
1560 dwDestContext, pvDestContext, mshlflags, pCid);
1562 return IMoniker_GetClassID(This->moniker, pCid);
1565 static HRESULT WINAPI MonikerMarshal_GetMarshalSizeMax(
1566 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
1567 void* pvDestContext, DWORD mshlflags, DWORD* pSize)
1569 MonikerMarshal *This = impl_from_IMarshal(iface);
1571 ULARGE_INTEGER size;
1573 TRACE("(%s, %p, %x, %p, %x, %p)\n", debugstr_guid(riid), pv,
1574 dwDestContext, pvDestContext, mshlflags, pSize);
1576 hr = IMoniker_GetSizeMax(This->moniker, &size);
1578 *pSize = (DWORD)size.QuadPart;
1582 static HRESULT WINAPI MonikerMarshal_MarshalInterface(LPMARSHAL iface, IStream *pStm,
1583 REFIID riid, void* pv, DWORD dwDestContext,
1584 void* pvDestContext, DWORD mshlflags)
1586 MonikerMarshal *This = impl_from_IMarshal(iface);
1588 TRACE("(%p, %s, %p, %x, %p, %x)\n", pStm, debugstr_guid(riid), pv,
1589 dwDestContext, pvDestContext, mshlflags);
1591 return IMoniker_Save(This->moniker, pStm, FALSE);
1594 static HRESULT WINAPI MonikerMarshal_UnmarshalInterface(LPMARSHAL iface, IStream *pStm, REFIID riid, void **ppv)
1596 MonikerMarshal *This = impl_from_IMarshal(iface);
1599 TRACE("(%p, %s, %p)\n", pStm, debugstr_guid(riid), ppv);
1601 hr = IMoniker_Load(This->moniker, pStm);
1603 hr = IMoniker_QueryInterface(This->moniker, riid, ppv);
1607 static HRESULT WINAPI MonikerMarshal_ReleaseMarshalData(LPMARSHAL iface, IStream *pStm)
1610 /* can't release a state-based marshal as nothing on server side to
1615 static HRESULT WINAPI MonikerMarshal_DisconnectObject(LPMARSHAL iface, DWORD dwReserved)
1618 /* can't disconnect a state-based marshal as nothing on server side to
1619 * disconnect from */
1623 static const IMarshalVtbl VT_MonikerMarshal =
1625 MonikerMarshal_QueryInterface,
1626 MonikerMarshal_AddRef,
1627 MonikerMarshal_Release,
1628 MonikerMarshal_GetUnmarshalClass,
1629 MonikerMarshal_GetMarshalSizeMax,
1630 MonikerMarshal_MarshalInterface,
1631 MonikerMarshal_UnmarshalInterface,
1632 MonikerMarshal_ReleaseMarshalData,
1633 MonikerMarshal_DisconnectObject
1636 HRESULT MonikerMarshal_Create(IMoniker *inner, IUnknown **outer)
1638 MonikerMarshal *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1639 if (!This) return E_OUTOFMEMORY;
1641 This->lpVtbl = &VT_MonikerMarshalInner;
1642 This->lpVtblMarshal = &VT_MonikerMarshal;
1644 This->moniker = inner;
1646 *outer = (IUnknown *)&This->lpVtbl;
1650 void * __RPC_USER MIDL_user_allocate(size_t size)
1652 return HeapAlloc(GetProcessHeap(), 0, size);
1655 void __RPC_USER MIDL_user_free(void *p)
1657 HeapFree(GetProcessHeap(), 0, p);