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
55 #define MAX_COMPARISON_DATA 2048
57 static LONG WINAPI rpc_filter(EXCEPTION_POINTERS *eptr)
59 switch (eptr->ExceptionRecord->ExceptionCode)
61 case EXCEPTION_ACCESS_VIOLATION:
62 case EXCEPTION_ILLEGAL_INSTRUCTION:
63 return EXCEPTION_CONTINUE_SEARCH;
65 return EXCEPTION_EXECUTE_HANDLER;
69 /* define the structure of the running object table elements */
73 InterfaceData* object; /* marshaled running object*/
74 MonikerComparisonData* moniker_data; /* moniker comparison data that identifies this object */
75 DWORD cookie; /* cookie identifying this object */
76 FILETIME last_modified;
77 IrotContextHandle ctxt_handle;
80 /* define the RunningObjectTableImpl structure */
81 typedef struct RunningObjectTableImpl
83 const IRunningObjectTableVtbl *lpVtbl;
86 struct list rot; /* list of ROT entries */
87 CRITICAL_SECTION lock;
88 } RunningObjectTableImpl;
90 static RunningObjectTableImpl* runningObjectTableInstance = NULL;
91 static IrotHandle irot_handle;
93 /* define the EnumMonikerImpl structure */
94 typedef struct EnumMonikerImpl
96 const IEnumMonikerVtbl *lpVtbl;
99 InterfaceList *moniker_list;
104 /* IEnumMoniker Local functions*/
105 static HRESULT WINAPI EnumMonikerImpl_CreateEnumROTMoniker(InterfaceList *moniker_list,
106 ULONG pos, IEnumMoniker **ppenumMoniker);
108 static IrotHandle get_irot_handle(void)
114 IrotHandle new_handle;
115 unsigned short ncacn_np[] = IROT_PROTSEQ;
116 unsigned short endpoint[] = IROT_ENDPOINT;
117 status = RpcStringBindingComposeW(NULL, ncacn_np, NULL, endpoint, NULL, &binding);
118 if (status == RPC_S_OK)
120 status = RpcBindingFromStringBindingW(binding, &new_handle);
121 RpcStringFreeW(&binding);
123 if (status != RPC_S_OK)
125 if (InterlockedCompareExchangePointer(&irot_handle, new_handle, NULL))
126 /* another thread beat us to it */
127 RpcBindingFree(&new_handle);
132 static BOOL start_rpcss(void)
134 PROCESS_INFORMATION pi;
137 static const WCHAR rpcss[] = {'r','p','c','s','s',0};
142 ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
143 ZeroMemory(&si, sizeof(STARTUPINFOA));
144 si.cb = sizeof(STARTUPINFOA);
146 memcpy(cmd, rpcss, sizeof(rpcss));
148 rslt = CreateProcessW(
149 NULL, /* executable */
150 cmd, /* command line */
151 NULL, /* process security attributes */
152 NULL, /* primary thread security attributes */
153 FALSE, /* inherit handles */
154 0, /* creation flags */
155 NULL, /* use parent's environment */
156 NULL, /* use parent's current directory */
157 &si, /* STARTUPINFO pointer */
158 &pi /* PROCESS_INFORMATION */
163 CloseHandle(pi.hProcess);
164 CloseHandle(pi.hThread);
171 static HRESULT create_stream_on_mip_ro(const InterfaceData *mip, IStream **stream)
173 HGLOBAL hglobal = GlobalAlloc(0, mip->ulCntData);
174 void *pv = GlobalLock(hglobal);
175 memcpy(pv, mip->abData, mip->ulCntData);
176 GlobalUnlock(hglobal);
177 return CreateStreamOnHGlobal(hglobal, TRUE, stream);
180 static inline void rot_entry_delete(struct rot_entry *rot_entry)
182 if (rot_entry->cookie)
184 InterfaceData *object = NULL;
185 InterfaceData *moniker = NULL;
188 IrotRevoke(get_irot_handle(), rot_entry->cookie,
189 &rot_entry->ctxt_handle, &object, &moniker);
195 MIDL_user_free(object);
200 hr = create_stream_on_mip_ro(moniker, &stream);
203 CoReleaseMarshalData(stream);
204 IUnknown_Release(stream);
207 MIDL_user_free(moniker);
209 if (rot_entry->object)
213 hr = create_stream_on_mip_ro(rot_entry->object, &stream);
216 CoReleaseMarshalData(stream);
217 IUnknown_Release(stream);
220 HeapFree(GetProcessHeap(), 0, rot_entry->object);
221 HeapFree(GetProcessHeap(), 0, rot_entry->moniker_data);
222 HeapFree(GetProcessHeap(), 0, rot_entry);
225 /* moniker_data must be freed with HeapFree when no longer in use */
226 static HRESULT get_moniker_comparison_data(IMoniker *pMoniker, MonikerComparisonData **moniker_data)
229 IROTData *pROTData = NULL;
230 hr = IMoniker_QueryInterface(pMoniker, &IID_IROTData, (void *)&pROTData);
233 ULONG size = MAX_COMPARISON_DATA;
234 *moniker_data = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(MonikerComparisonData, abData[size]));
237 IROTData_Release(pROTData);
238 return E_OUTOFMEMORY;
240 hr = IROTData_GetComparisonData(pROTData, (*moniker_data)->abData, size, &size);
241 IROTData_Release(pROTData);
244 ERR("Failed to copy comparison data into buffer, hr = 0x%08x\n", hr);
245 HeapFree(GetProcessHeap(), 0, *moniker_data);
248 (*moniker_data)->ulCntData = size;
253 LPOLESTR pszDisplayName;
257 TRACE("generating comparison data from display name\n");
259 hr = CreateBindCtx(0, &pbc);
262 hr = IMoniker_GetDisplayName(pMoniker, pbc, NULL, &pszDisplayName);
263 IBindCtx_Release(pbc);
266 hr = IMoniker_GetClassID(pMoniker, &clsid);
269 CoTaskMemFree(pszDisplayName);
273 len = strlenW(pszDisplayName);
274 *moniker_data = HeapAlloc(GetProcessHeap(), 0,
275 FIELD_OFFSET(MonikerComparisonData, abData[sizeof(CLSID) + (len+1)*sizeof(WCHAR)]));
278 CoTaskMemFree(pszDisplayName);
279 return E_OUTOFMEMORY;
281 (*moniker_data)->ulCntData = sizeof(CLSID) + (len+1)*sizeof(WCHAR);
283 memcpy(&(*moniker_data)->abData[0], &clsid, sizeof(clsid));
284 memcpy(&(*moniker_data)->abData[sizeof(clsid)], pszDisplayName, (len+1)*sizeof(WCHAR));
285 CoTaskMemFree(pszDisplayName);
290 static HRESULT reduce_moniker(IMoniker *pmk, IBindCtx *pbc, IMoniker **pmkReduced)
292 IBindCtx *pbcNew = NULL;
296 hr = CreateBindCtx(0, &pbcNew);
301 hr = IMoniker_Reduce(pmk, pbc, MKRREDUCE_ALL, NULL, pmkReduced);
303 ERR("reducing moniker failed with error 0x%08x\n", hr);
304 if (pbcNew) IBindCtx_Release(pbcNew);
308 /***********************************************************************
309 * RunningObjectTable_QueryInterface
311 static HRESULT WINAPI
312 RunningObjectTableImpl_QueryInterface(IRunningObjectTable* iface,
313 REFIID riid,void** ppvObject)
315 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
317 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
319 /* validate arguments */
326 if (IsEqualIID(&IID_IUnknown, riid) ||
327 IsEqualIID(&IID_IRunningObjectTable, riid))
328 *ppvObject = (IRunningObjectTable*)This;
331 return E_NOINTERFACE;
333 IRunningObjectTable_AddRef(iface);
338 /***********************************************************************
339 * RunningObjectTable_AddRef
342 RunningObjectTableImpl_AddRef(IRunningObjectTable* iface)
344 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
346 TRACE("(%p)\n",This);
348 return InterlockedIncrement(&This->ref);
351 /***********************************************************************
352 * RunningObjectTable_Initialize
354 static HRESULT WINAPI
355 RunningObjectTableImpl_Destroy(void)
357 struct list *cursor, *cursor2;
358 IrotHandle old_handle;
362 if (runningObjectTableInstance==NULL)
365 /* free the ROT table memory */
366 LIST_FOR_EACH_SAFE(cursor, cursor2, &runningObjectTableInstance->rot)
368 struct rot_entry *rot_entry = LIST_ENTRY(cursor, struct rot_entry, entry);
369 list_remove(&rot_entry->entry);
370 rot_entry_delete(rot_entry);
373 DEBUG_CLEAR_CRITSEC_NAME(&runningObjectTableInstance->lock);
374 DeleteCriticalSection(&runningObjectTableInstance->lock);
376 /* free the ROT structure memory */
377 HeapFree(GetProcessHeap(),0,runningObjectTableInstance);
378 runningObjectTableInstance = NULL;
380 old_handle = irot_handle;
383 RpcBindingFree(&old_handle);
388 /***********************************************************************
389 * RunningObjectTable_Release
392 RunningObjectTableImpl_Release(IRunningObjectTable* iface)
394 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
397 TRACE("(%p)\n",This);
399 ref = InterlockedDecrement(&This->ref);
401 /* uninitialize ROT structure if there's no more references to it */
404 struct list *cursor, *cursor2;
405 LIST_FOR_EACH_SAFE(cursor, cursor2, &This->rot)
407 struct rot_entry *rot_entry = LIST_ENTRY(cursor, struct rot_entry, entry);
408 list_remove(&rot_entry->entry);
409 rot_entry_delete(rot_entry);
411 /* RunningObjectTable data structure will be not destroyed here ! the destruction will be done only
412 * when RunningObjectTableImpl_UnInitialize function is called
419 /***********************************************************************
420 * RunningObjectTable_Register
423 * grfFlags [in] Registration options
424 * punkObject [in] the object being registered
425 * pmkObjectName [in] the moniker of the object being registered
426 * pdwRegister [in] the value identifying the registration
428 static HRESULT WINAPI
429 RunningObjectTableImpl_Register(IRunningObjectTable* iface, DWORD grfFlags,
430 IUnknown *punkObject, IMoniker *pmkObjectName, DWORD *pdwRegister)
432 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
433 struct rot_entry *rot_entry;
435 IStream *pStream = NULL;
438 InterfaceData *moniker = NULL;
440 TRACE("(%p,%d,%p,%p,%p)\n",This,grfFlags,punkObject,pmkObjectName,pdwRegister);
442 if (grfFlags & ~(ROTFLAGS_REGISTRATIONKEEPSALIVE|ROTFLAGS_ALLOWANYCLIENT))
444 ERR("Invalid grfFlags: 0x%08x\n", grfFlags & ~(ROTFLAGS_REGISTRATIONKEEPSALIVE|ROTFLAGS_ALLOWANYCLIENT));
448 if (punkObject==NULL || pmkObjectName==NULL || pdwRegister==NULL)
451 rot_entry = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*rot_entry));
453 return E_OUTOFMEMORY;
456 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
459 rot_entry_delete(rot_entry);
462 mshlflags = (grfFlags & ROTFLAGS_REGISTRATIONKEEPSALIVE) ? MSHLFLAGS_TABLESTRONG : MSHLFLAGS_TABLEWEAK;
463 hr = CoMarshalInterface(pStream, &IID_IUnknown, punkObject, MSHCTX_LOCAL | MSHCTX_NOSHAREDMEM, NULL, mshlflags);
464 /* FIXME: a cleaner way would be to create an IStream class that writes
465 * directly to an MInterfacePointer */
469 hr = GetHGlobalFromStream(pStream, &hglobal);
472 SIZE_T size = GlobalSize(hglobal);
473 const void *pv = GlobalLock(hglobal);
474 rot_entry->object = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(MInterfacePointer, abData[size]));
475 rot_entry->object->ulCntData = size;
476 memcpy(&rot_entry->object->abData, pv, size);
477 GlobalUnlock(hglobal);
480 IStream_Release(pStream);
483 rot_entry_delete(rot_entry);
487 hr = CreateBindCtx(0, &pbc);
490 rot_entry_delete(rot_entry);
494 hr = reduce_moniker(pmkObjectName, pbc, &pmkObjectName);
497 rot_entry_delete(rot_entry);
498 IBindCtx_Release(pbc);
502 hr = IMoniker_GetTimeOfLastChange(pmkObjectName, pbc, NULL,
503 &rot_entry->last_modified);
504 IBindCtx_Release(pbc);
507 CoFileTimeNow(&rot_entry->last_modified);
511 hr = get_moniker_comparison_data(pmkObjectName,
512 &rot_entry->moniker_data);
515 rot_entry_delete(rot_entry);
516 IMoniker_Release(pmkObjectName);
520 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
523 rot_entry_delete(rot_entry);
524 IMoniker_Release(pmkObjectName);
527 /* marshal moniker */
528 hr = CoMarshalInterface(pStream, &IID_IMoniker, (IUnknown *)pmkObjectName,
529 MSHCTX_LOCAL | MSHCTX_NOSHAREDMEM, NULL, MSHLFLAGS_TABLESTRONG);
530 /* FIXME: a cleaner way would be to create an IStream class that writes
531 * directly to an MInterfacePointer */
535 hr = GetHGlobalFromStream(pStream, &hglobal);
538 SIZE_T size = GlobalSize(hglobal);
539 const void *pv = GlobalLock(hglobal);
540 moniker = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(InterfaceData, abData[size]));
541 moniker->ulCntData = size;
542 memcpy(&moniker->abData, pv, size);
543 GlobalUnlock(hglobal);
546 IStream_Release(pStream);
547 IMoniker_Release(pmkObjectName);
550 HeapFree(GetProcessHeap(), 0, moniker);
551 rot_entry_delete(rot_entry);
560 hr = IrotRegister(get_irot_handle(), rot_entry->moniker_data,
561 rot_entry->object, moniker,
562 &rot_entry->last_modified, grfFlags,
563 &rot_entry->cookie, &rot_entry->ctxt_handle);
567 hr = HRESULT_FROM_WIN32(GetExceptionCode());
570 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
577 HeapFree(GetProcessHeap(), 0, moniker);
580 rot_entry_delete(rot_entry);
584 /* gives a registration identifier to the registered object*/
585 *pdwRegister = rot_entry->cookie;
587 EnterCriticalSection(&This->lock);
588 list_add_tail(&This->rot, &rot_entry->entry);
589 LeaveCriticalSection(&This->lock);
594 /***********************************************************************
595 * RunningObjectTable_Revoke
598 * dwRegister [in] Value identifying registration to be revoked
600 static HRESULT WINAPI
601 RunningObjectTableImpl_Revoke( IRunningObjectTable* iface, DWORD dwRegister)
603 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
604 struct rot_entry *rot_entry;
606 TRACE("(%p,%d)\n",This,dwRegister);
608 EnterCriticalSection(&This->lock);
609 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, struct rot_entry, entry)
611 if (rot_entry->cookie == dwRegister)
613 list_remove(&rot_entry->entry);
614 LeaveCriticalSection(&This->lock);
616 rot_entry_delete(rot_entry);
620 LeaveCriticalSection(&This->lock);
625 /***********************************************************************
626 * RunningObjectTable_IsRunning
629 * pmkObjectName [in] moniker of the object whose status is desired
631 static HRESULT WINAPI
632 RunningObjectTableImpl_IsRunning( IRunningObjectTable* iface, IMoniker *pmkObjectName)
634 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
635 MonikerComparisonData *moniker_data;
637 const struct rot_entry *rot_entry;
639 TRACE("(%p,%p)\n",This,pmkObjectName);
641 hr = reduce_moniker(pmkObjectName, NULL, &pmkObjectName);
644 hr = get_moniker_comparison_data(pmkObjectName, &moniker_data);
645 IMoniker_Release(pmkObjectName);
650 EnterCriticalSection(&This->lock);
651 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, const struct rot_entry, entry)
653 if ((rot_entry->moniker_data->ulCntData == moniker_data->ulCntData) &&
654 !memcmp(&moniker_data->abData, &rot_entry->moniker_data->abData, moniker_data->ulCntData))
660 LeaveCriticalSection(&This->lock);
668 hr = IrotIsRunning(get_irot_handle(), moniker_data);
672 hr = HRESULT_FROM_WIN32(GetExceptionCode());
675 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
684 HeapFree(GetProcessHeap(), 0, moniker_data);
689 /***********************************************************************
690 * RunningObjectTable_GetObject
693 * pmkObjectName [in] Pointer to the moniker on the object
694 * ppunkObject [out] variable that receives the IUnknown interface pointer
696 static HRESULT WINAPI
697 RunningObjectTableImpl_GetObject( IRunningObjectTable* iface,
698 IMoniker *pmkObjectName, IUnknown **ppunkObject)
700 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
701 MonikerComparisonData *moniker_data;
702 InterfaceData *object = NULL;
705 struct rot_entry *rot_entry;
707 TRACE("(%p,%p,%p)\n",This,pmkObjectName,ppunkObject);
709 if (ppunkObject == NULL)
714 hr = reduce_moniker(pmkObjectName, NULL, &pmkObjectName);
717 hr = get_moniker_comparison_data(pmkObjectName, &moniker_data);
718 IMoniker_Release(pmkObjectName);
722 EnterCriticalSection(&This->lock);
723 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, struct rot_entry, entry)
725 if ((rot_entry->moniker_data->ulCntData == moniker_data->ulCntData) &&
726 !memcmp(&moniker_data->abData, &rot_entry->moniker_data->abData, moniker_data->ulCntData))
729 hr = create_stream_on_mip_ro(rot_entry->object, &pStream);
732 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)ppunkObject);
733 IStream_Release(pStream);
736 LeaveCriticalSection(&This->lock);
737 HeapFree(GetProcessHeap(), 0, moniker_data);
742 LeaveCriticalSection(&This->lock);
744 TRACE("moniker unavailable locally, calling SCM\n");
750 hr = IrotGetObject(get_irot_handle(), moniker_data, &object, &cookie);
754 hr = HRESULT_FROM_WIN32(GetExceptionCode());
757 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
768 hr = create_stream_on_mip_ro(object, &pStream);
771 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)ppunkObject);
772 IStream_Release(pStream);
776 WARN("Moniker unavailable, IrotGetObject returned 0x%08x\n", hr);
778 HeapFree(GetProcessHeap(), 0, moniker_data);
783 /***********************************************************************
784 * RunningObjectTable_NoteChangeTime
787 * dwRegister [in] Value identifying registration being updated
788 * pfiletime [in] Pointer to structure containing object's last change time
790 static HRESULT WINAPI
791 RunningObjectTableImpl_NoteChangeTime(IRunningObjectTable* iface,
792 DWORD dwRegister, FILETIME *pfiletime)
794 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
795 struct rot_entry *rot_entry;
796 HRESULT hr = E_INVALIDARG;
798 TRACE("(%p,%d,%p)\n",This,dwRegister,pfiletime);
800 EnterCriticalSection(&This->lock);
801 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, struct rot_entry, entry)
803 if (rot_entry->cookie == dwRegister)
805 rot_entry->last_modified = *pfiletime;
806 LeaveCriticalSection(&This->lock);
812 hr = IrotNoteChangeTime(get_irot_handle(), dwRegister, pfiletime);
816 hr = HRESULT_FROM_WIN32(GetExceptionCode());
819 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
830 LeaveCriticalSection(&This->lock);
833 TRACE("-- 0x08%x\n", hr);
837 /***********************************************************************
838 * RunningObjectTable_GetTimeOfLastChange
841 * pmkObjectName [in] moniker of the object whose status is desired
842 * pfiletime [out] structure that receives object's last change time
844 static HRESULT WINAPI
845 RunningObjectTableImpl_GetTimeOfLastChange(IRunningObjectTable* iface,
846 IMoniker *pmkObjectName, FILETIME *pfiletime)
848 HRESULT hr = MK_E_UNAVAILABLE;
849 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
850 MonikerComparisonData *moniker_data;
851 const struct rot_entry *rot_entry;
853 TRACE("(%p,%p,%p)\n",This,pmkObjectName,pfiletime);
855 if (pmkObjectName==NULL || pfiletime==NULL)
858 hr = reduce_moniker(pmkObjectName, NULL, &pmkObjectName);
861 hr = get_moniker_comparison_data(pmkObjectName, &moniker_data);
862 IMoniker_Release(pmkObjectName);
866 hr = MK_E_UNAVAILABLE;
868 EnterCriticalSection(&This->lock);
869 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, const struct rot_entry, entry)
871 if ((rot_entry->moniker_data->ulCntData == moniker_data->ulCntData) &&
872 !memcmp(&moniker_data->abData, &rot_entry->moniker_data->abData, moniker_data->ulCntData))
874 *pfiletime = rot_entry->last_modified;
879 LeaveCriticalSection(&This->lock);
887 hr = IrotGetTimeOfLastChange(get_irot_handle(), moniker_data, pfiletime);
891 hr = HRESULT_FROM_WIN32(GetExceptionCode());
894 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
903 HeapFree(GetProcessHeap(), 0, moniker_data);
905 TRACE("-- 0x%08x\n", hr);
909 /***********************************************************************
910 * RunningObjectTable_EnumRunning
913 * ppenumMoniker [out] receives the IEnumMoniker interface pointer
915 static HRESULT WINAPI
916 RunningObjectTableImpl_EnumRunning(IRunningObjectTable* iface,
917 IEnumMoniker **ppenumMoniker)
919 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
920 InterfaceList *interface_list = NULL;
923 TRACE("(%p, %p)\n", This, ppenumMoniker);
925 *ppenumMoniker = NULL;
931 hr = IrotEnumRunning(get_irot_handle(), &interface_list);
935 hr = HRESULT_FROM_WIN32(GetExceptionCode());
938 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
947 hr = EnumMonikerImpl_CreateEnumROTMoniker(interface_list,
953 /* Virtual function table for the IRunningObjectTable class. */
954 static const IRunningObjectTableVtbl VT_RunningObjectTableImpl =
956 RunningObjectTableImpl_QueryInterface,
957 RunningObjectTableImpl_AddRef,
958 RunningObjectTableImpl_Release,
959 RunningObjectTableImpl_Register,
960 RunningObjectTableImpl_Revoke,
961 RunningObjectTableImpl_IsRunning,
962 RunningObjectTableImpl_GetObject,
963 RunningObjectTableImpl_NoteChangeTime,
964 RunningObjectTableImpl_GetTimeOfLastChange,
965 RunningObjectTableImpl_EnumRunning
968 /***********************************************************************
969 * RunningObjectTable_Initialize
971 HRESULT WINAPI RunningObjectTableImpl_Initialize(void)
975 /* create the unique instance of the RunningObjectTableImpl structure */
976 runningObjectTableInstance = HeapAlloc(GetProcessHeap(), 0, sizeof(RunningObjectTableImpl));
978 if (!runningObjectTableInstance)
979 return E_OUTOFMEMORY;
981 /* initialize the virtual table function */
982 runningObjectTableInstance->lpVtbl = &VT_RunningObjectTableImpl;
984 /* the initial reference is set to "1" ! because if set to "0" it will be not practis when */
985 /* the ROT referred many times not in the same time (all the objects in the ROT will */
986 /* be removed every time the ROT is removed ) */
987 runningObjectTableInstance->ref = 1;
989 list_init(&runningObjectTableInstance->rot);
990 InitializeCriticalSection(&runningObjectTableInstance->lock);
991 DEBUG_SET_CRITSEC_NAME(&runningObjectTableInstance->lock, "RunningObjectTableImpl.lock");
996 /***********************************************************************
997 * RunningObjectTable_UnInitialize
999 HRESULT WINAPI RunningObjectTableImpl_UnInitialize(void)
1003 if (runningObjectTableInstance==NULL)
1006 RunningObjectTableImpl_Release((IRunningObjectTable*)runningObjectTableInstance);
1008 RunningObjectTableImpl_Destroy();
1013 /***********************************************************************
1014 * GetRunningObjectTable (OLE32.@)
1016 * Retrieves the global running object table.
1019 * reserved [I] Reserved. Set to 0.
1020 * pprot [O] Address that receives the pointer to the running object table.
1024 * Failure: Any HRESULT code.
1027 GetRunningObjectTable(DWORD reserved, LPRUNNINGOBJECTTABLE *pprot)
1029 IID riid=IID_IRunningObjectTable;
1035 return E_UNEXPECTED;
1037 if(runningObjectTableInstance==NULL)
1038 return CO_E_NOTINITIALIZED;
1040 res = IRunningObjectTable_QueryInterface((IRunningObjectTable*)runningObjectTableInstance,&riid,(void**)pprot);
1045 static HRESULT get_moniker_for_progid_display_name(LPBC pbc,
1046 LPCOLESTR szDisplayName,
1053 LPCWSTR start = szDisplayName;
1056 IMoniker *class_moniker;
1061 /* find end delimiter */
1062 for (end = start; *end; end++)
1068 /* must start with '@' or have a ':' somewhere and mustn't be one character
1069 * long (since that looks like an absolute path) */
1070 if (((start == szDisplayName) && (*end == '\0')) || (len <= 1))
1073 progid = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1076 memcpy(progid, start, len * sizeof(WCHAR));
1079 hr = CLSIDFromProgID(progid, &clsid);
1080 HeapFree(GetProcessHeap(), 0, progid);
1084 hr = CreateClassMoniker(&clsid, &class_moniker);
1087 IParseDisplayName *pdn;
1088 hr = IMoniker_BindToObject(class_moniker, pbc, NULL,
1089 &IID_IParseDisplayName, (void **)&pdn);
1090 /* fallback to using IClassFactory to get IParseDisplayName -
1091 * adsldp.dll depends on this */
1095 hr = IMoniker_BindToObject(class_moniker, pbc, NULL,
1096 &IID_IClassFactory, (void **)&pcf);
1099 hr = IClassFactory_CreateInstance(pcf, NULL,
1100 &IID_IParseDisplayName,
1102 IClassFactory_Release(pcf);
1105 IMoniker_Release(class_moniker);
1108 hr = IParseDisplayName_ParseDisplayName(pdn, pbc,
1109 (LPOLESTR)szDisplayName,
1111 IParseDisplayName_Release(pdn);
1117 /******************************************************************************
1118 * MkParseDisplayName [OLE32.@]
1120 HRESULT WINAPI MkParseDisplayName(LPBC pbc, LPCOLESTR szDisplayName,
1121 LPDWORD pchEaten, LPMONIKER *ppmk)
1123 HRESULT hr = MK_E_SYNTAX;
1124 static const WCHAR wszClsidColon[] = {'c','l','s','i','d',':'};
1128 TRACE("(%p, %s, %p, %p)\n", pbc, debugstr_w(szDisplayName), pchEaten, ppmk);
1130 if (!(IsValidInterface((LPUNKNOWN) pbc)))
1131 return E_INVALIDARG;
1136 if (!strncmpiW(szDisplayName, wszClsidColon, sizeof(wszClsidColon)/sizeof(wszClsidColon[0])))
1138 hr = ClassMoniker_CreateFromDisplayName(pbc, szDisplayName, &chEaten, &moniker);
1139 if (FAILED(hr) && (hr != MK_E_SYNTAX))
1144 hr = get_moniker_for_progid_display_name(pbc, szDisplayName, &chEaten, &moniker);
1145 if (FAILED(hr) && (hr != MK_E_SYNTAX))
1151 hr = FileMoniker_CreateFromDisplayName(pbc, szDisplayName, &chEaten, &moniker);
1152 if (FAILED(hr) && (hr != MK_E_SYNTAX))
1160 IMoniker *next_moniker;
1161 *pchEaten += chEaten;
1162 szDisplayName += chEaten;
1163 if (!*szDisplayName)
1169 hr = IMoniker_ParseDisplayName(moniker, pbc, NULL,
1170 (LPOLESTR)szDisplayName, &chEaten,
1172 IMoniker_Release(moniker);
1178 moniker = next_moniker;
1185 /***********************************************************************
1186 * GetClassFile (OLE32.@)
1188 * Retrieves the class ID associated with the given filename.
1191 * filePathName [I] Filename to retrieve the class ID for.
1192 * pclsid [O] Address that receives the class ID for the file.
1196 * Failure: Any HRESULT code.
1198 HRESULT WINAPI GetClassFile(LPCOLESTR filePathName,CLSID *pclsid)
1202 int nbElm, length, i;
1204 LPOLESTR *pathDec=0,absFile=0,progId=0;
1206 static const WCHAR bkslashW[] = {'\\',0};
1207 static const WCHAR dotW[] = {'.',0};
1209 TRACE("%s, %p\n", debugstr_w(filePathName), pclsid);
1211 /* if the file contain a storage object the return the CLSID written by IStorage_SetClass method*/
1212 if((StgIsStorageFile(filePathName))==S_OK){
1214 res=StgOpenStorage(filePathName,NULL,STGM_READ | STGM_SHARE_DENY_WRITE,NULL,0,&pstg);
1217 res=ReadClassStg(pstg,pclsid);
1219 IStorage_Release(pstg);
1223 /* if the file is not a storage object then attemps to match various bits in the file against a
1224 pattern in the registry. this case is not frequently used ! so I present only the psodocode for
1227 for(i=0;i<nFileTypes;i++)
1229 for(i=0;j<nPatternsForType;j++){
1234 pat=ReadPatternFromRegistry(i,j);
1235 hFile=CreateFileW(filePathName,,,,,,hFile);
1236 SetFilePosition(hFile,pat.offset);
1237 ReadFile(hFile,buf,pat.size,&r,NULL);
1238 if (memcmp(buf&pat.mask,pat.pattern.pat.size)==0){
1240 *pclsid=ReadCLSIDFromRegistry(i);
1246 /* if the above strategies fail then search for the extension key in the registry */
1248 /* get the last element (absolute file) in the path name */
1249 nbElm=FileMonikerImpl_DecomposePath(filePathName,&pathDec);
1250 absFile=pathDec[nbElm-1];
1252 /* failed if the path represente a directory and not an absolute file name*/
1253 if (!lstrcmpW(absFile, bkslashW))
1254 return MK_E_INVALIDEXTENSION;
1256 /* get the extension of the file */
1258 length=lstrlenW(absFile);
1259 for(i = length-1; (i >= 0) && *(extension = &absFile[i]) != '.'; i--)
1262 if (!extension || !lstrcmpW(extension, dotW))
1263 return MK_E_INVALIDEXTENSION;
1265 res=RegQueryValueW(HKEY_CLASSES_ROOT, extension, NULL, &sizeProgId);
1267 /* get the progId associated to the extension */
1268 progId = CoTaskMemAlloc(sizeProgId);
1269 res = RegQueryValueW(HKEY_CLASSES_ROOT, extension, progId, &sizeProgId);
1271 if (res==ERROR_SUCCESS)
1272 /* return the clsid associated to the progId */
1273 res= CLSIDFromProgID(progId,pclsid);
1275 for(i=0; pathDec[i]!=NULL;i++)
1276 CoTaskMemFree(pathDec[i]);
1277 CoTaskMemFree(pathDec);
1279 CoTaskMemFree(progId);
1281 if (res==ERROR_SUCCESS)
1284 return MK_E_INVALIDEXTENSION;
1287 /***********************************************************************
1288 * EnumMoniker_QueryInterface
1290 static HRESULT WINAPI EnumMonikerImpl_QueryInterface(IEnumMoniker* iface,REFIID riid,void** ppvObject)
1292 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1294 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
1296 /* validate arguments */
1297 if (ppvObject == NULL)
1298 return E_INVALIDARG;
1302 if (IsEqualIID(&IID_IUnknown, riid))
1303 *ppvObject = (IEnumMoniker*)This;
1305 if (IsEqualIID(&IID_IEnumMoniker, riid))
1306 *ppvObject = (IEnumMoniker*)This;
1308 if ((*ppvObject)==NULL)
1309 return E_NOINTERFACE;
1311 IEnumMoniker_AddRef(iface);
1316 /***********************************************************************
1317 * EnumMoniker_AddRef
1319 static ULONG WINAPI EnumMonikerImpl_AddRef(IEnumMoniker* iface)
1321 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1323 TRACE("(%p)\n",This);
1325 return InterlockedIncrement(&This->ref);
1328 /***********************************************************************
1329 * EnumMoniker_release
1331 static ULONG WINAPI EnumMonikerImpl_Release(IEnumMoniker* iface)
1333 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1336 TRACE("(%p)\n",This);
1338 ref = InterlockedDecrement(&This->ref);
1340 /* uninitialize rot structure if there's no more reference to it*/
1345 TRACE("(%p) Deleting\n",This);
1347 for (i = 0; i < This->moniker_list->size; i++)
1348 HeapFree(GetProcessHeap(), 0, This->moniker_list->interfaces[i]);
1349 HeapFree(GetProcessHeap(), 0, This->moniker_list);
1350 HeapFree(GetProcessHeap(), 0, This);
1355 /***********************************************************************
1358 static HRESULT WINAPI EnumMonikerImpl_Next(IEnumMoniker* iface, ULONG celt, IMoniker** rgelt, ULONG * pceltFetched)
1361 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1364 TRACE("(%p) TabCurrentPos %d Tablastindx %d\n", This, This->pos, This->moniker_list->size);
1366 /* retrieve the requested number of moniker from the current position */
1367 for(i = 0; (This->pos < This->moniker_list->size) && (i < celt); i++)
1370 hr = create_stream_on_mip_ro(This->moniker_list->interfaces[This->pos++], &stream);
1371 if (hr != S_OK) break;
1372 hr = CoUnmarshalInterface(stream, &IID_IMoniker, (void **)&rgelt[i]);
1373 IStream_Release(stream);
1374 if (hr != S_OK) break;
1377 if (pceltFetched != NULL)
1390 /***********************************************************************
1393 static HRESULT WINAPI EnumMonikerImpl_Skip(IEnumMoniker* iface, ULONG celt)
1395 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1397 TRACE("(%p)\n",This);
1399 if (This->pos + celt >= This->moniker_list->size)
1407 /***********************************************************************
1408 * EnmumMoniker_Reset
1410 static HRESULT WINAPI EnumMonikerImpl_Reset(IEnumMoniker* iface)
1412 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1414 This->pos = 0; /* set back to start of list */
1416 TRACE("(%p)\n",This);
1421 /***********************************************************************
1422 * EnmumMoniker_Clone
1424 static HRESULT WINAPI EnumMonikerImpl_Clone(IEnumMoniker* iface, IEnumMoniker ** ppenum)
1426 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1427 InterfaceList *moniker_list;
1430 TRACE("(%p)\n",This);
1434 moniker_list = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(InterfaceList, interfaces[This->moniker_list->size]));
1436 return E_OUTOFMEMORY;
1438 moniker_list->size = This->moniker_list->size;
1439 for (i = 0; i < This->moniker_list->size; i++)
1441 SIZE_T size = FIELD_OFFSET(InterfaceData, abData[This->moniker_list->interfaces[i]->ulCntData]);
1442 moniker_list->interfaces[i] = HeapAlloc(GetProcessHeap(), 0, size);
1443 if (!moniker_list->interfaces[i])
1446 for (i = 0; i < end; i++)
1447 HeapFree(GetProcessHeap(), 0, moniker_list->interfaces[i]);
1448 HeapFree(GetProcessHeap(), 0, moniker_list);
1449 return E_OUTOFMEMORY;
1451 memcpy(moniker_list->interfaces[i], This->moniker_list->interfaces[i], size);
1454 /* copy the enum structure */
1455 return EnumMonikerImpl_CreateEnumROTMoniker(moniker_list, This->pos, ppenum);
1458 /* Virtual function table for the IEnumMoniker class. */
1459 static const IEnumMonikerVtbl VT_EnumMonikerImpl =
1461 EnumMonikerImpl_QueryInterface,
1462 EnumMonikerImpl_AddRef,
1463 EnumMonikerImpl_Release,
1464 EnumMonikerImpl_Next,
1465 EnumMonikerImpl_Skip,
1466 EnumMonikerImpl_Reset,
1467 EnumMonikerImpl_Clone
1470 /***********************************************************************
1471 * EnumMonikerImpl_CreateEnumROTMoniker
1472 * Used by EnumRunning to create the structure and EnumClone
1473 * to copy the structure
1475 static HRESULT WINAPI EnumMonikerImpl_CreateEnumROTMoniker(InterfaceList *moniker_list,
1477 IEnumMoniker **ppenumMoniker)
1479 EnumMonikerImpl* This = NULL;
1482 return E_INVALIDARG;
1484 This = HeapAlloc(GetProcessHeap(), 0, sizeof(EnumMonikerImpl));
1485 if (!This) return E_OUTOFMEMORY;
1487 TRACE("(%p)\n", This);
1489 /* initialize the virtual table function */
1490 This->lpVtbl = &VT_EnumMonikerImpl;
1492 /* the initial reference is set to "1" */
1493 This->ref = 1; /* set the ref count to one */
1494 This->pos = current_pos; /* Set the list start posn */
1495 This->moniker_list = moniker_list;
1497 *ppenumMoniker = (IEnumMoniker*)This;
1503 /* Shared implementation of moniker marshaler based on saving and loading of
1506 typedef struct MonikerMarshal
1508 const IUnknownVtbl *lpVtbl;
1509 const IMarshalVtbl *lpVtblMarshal;
1515 static inline MonikerMarshal *impl_from_IMarshal( IMarshal *iface )
1517 return (MonikerMarshal *)((char*)iface - FIELD_OFFSET(MonikerMarshal, lpVtblMarshal));
1520 static HRESULT WINAPI MonikerMarshalInner_QueryInterface(IUnknown *iface, REFIID riid, LPVOID *ppv)
1522 MonikerMarshal *This = (MonikerMarshal *)iface;
1523 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
1525 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IMarshal, riid))
1527 *ppv = &This->lpVtblMarshal;
1528 IUnknown_AddRef((IUnknown *)&This->lpVtblMarshal);
1531 FIXME("No interface for %s\n", debugstr_guid(riid));
1532 return E_NOINTERFACE;
1535 static ULONG WINAPI MonikerMarshalInner_AddRef(IUnknown *iface)
1537 MonikerMarshal *This = (MonikerMarshal *)iface;
1538 return InterlockedIncrement(&This->ref);
1541 static ULONG WINAPI MonikerMarshalInner_Release(IUnknown *iface)
1543 MonikerMarshal *This = (MonikerMarshal *)iface;
1544 ULONG ref = InterlockedDecrement(&This->ref);
1546 if (!ref) HeapFree(GetProcessHeap(), 0, This);
1550 static const IUnknownVtbl VT_MonikerMarshalInner =
1552 MonikerMarshalInner_QueryInterface,
1553 MonikerMarshalInner_AddRef,
1554 MonikerMarshalInner_Release
1557 static HRESULT WINAPI MonikerMarshal_QueryInterface(IMarshal *iface, REFIID riid, LPVOID *ppv)
1559 MonikerMarshal *This = impl_from_IMarshal(iface);
1560 return IMoniker_QueryInterface(This->moniker, riid, ppv);
1563 static ULONG WINAPI MonikerMarshal_AddRef(IMarshal *iface)
1565 MonikerMarshal *This = impl_from_IMarshal(iface);
1566 return IMoniker_AddRef(This->moniker);
1569 static ULONG WINAPI MonikerMarshal_Release(IMarshal *iface)
1571 MonikerMarshal *This = impl_from_IMarshal(iface);
1572 return IMoniker_Release(This->moniker);
1575 static HRESULT WINAPI MonikerMarshal_GetUnmarshalClass(
1576 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
1577 void* pvDestContext, DWORD mshlflags, CLSID* pCid)
1579 MonikerMarshal *This = impl_from_IMarshal(iface);
1581 TRACE("(%s, %p, %x, %p, %x, %p)\n", debugstr_guid(riid), pv,
1582 dwDestContext, pvDestContext, mshlflags, pCid);
1584 return IMoniker_GetClassID(This->moniker, pCid);
1587 static HRESULT WINAPI MonikerMarshal_GetMarshalSizeMax(
1588 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
1589 void* pvDestContext, DWORD mshlflags, DWORD* pSize)
1591 MonikerMarshal *This = impl_from_IMarshal(iface);
1593 ULARGE_INTEGER size;
1595 TRACE("(%s, %p, %x, %p, %x, %p)\n", debugstr_guid(riid), pv,
1596 dwDestContext, pvDestContext, mshlflags, pSize);
1598 hr = IMoniker_GetSizeMax(This->moniker, &size);
1600 *pSize = (DWORD)size.QuadPart;
1604 static HRESULT WINAPI MonikerMarshal_MarshalInterface(LPMARSHAL iface, IStream *pStm,
1605 REFIID riid, void* pv, DWORD dwDestContext,
1606 void* pvDestContext, DWORD mshlflags)
1608 MonikerMarshal *This = impl_from_IMarshal(iface);
1610 TRACE("(%p, %s, %p, %x, %p, %x)\n", pStm, debugstr_guid(riid), pv,
1611 dwDestContext, pvDestContext, mshlflags);
1613 return IMoniker_Save(This->moniker, pStm, FALSE);
1616 static HRESULT WINAPI MonikerMarshal_UnmarshalInterface(LPMARSHAL iface, IStream *pStm, REFIID riid, void **ppv)
1618 MonikerMarshal *This = impl_from_IMarshal(iface);
1621 TRACE("(%p, %s, %p)\n", pStm, debugstr_guid(riid), ppv);
1623 hr = IMoniker_Load(This->moniker, pStm);
1625 hr = IMoniker_QueryInterface(This->moniker, riid, ppv);
1629 static HRESULT WINAPI MonikerMarshal_ReleaseMarshalData(LPMARSHAL iface, IStream *pStm)
1632 /* can't release a state-based marshal as nothing on server side to
1637 static HRESULT WINAPI MonikerMarshal_DisconnectObject(LPMARSHAL iface, DWORD dwReserved)
1640 /* can't disconnect a state-based marshal as nothing on server side to
1641 * disconnect from */
1645 static const IMarshalVtbl VT_MonikerMarshal =
1647 MonikerMarshal_QueryInterface,
1648 MonikerMarshal_AddRef,
1649 MonikerMarshal_Release,
1650 MonikerMarshal_GetUnmarshalClass,
1651 MonikerMarshal_GetMarshalSizeMax,
1652 MonikerMarshal_MarshalInterface,
1653 MonikerMarshal_UnmarshalInterface,
1654 MonikerMarshal_ReleaseMarshalData,
1655 MonikerMarshal_DisconnectObject
1658 HRESULT MonikerMarshal_Create(IMoniker *inner, IUnknown **outer)
1660 MonikerMarshal *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1661 if (!This) return E_OUTOFMEMORY;
1663 This->lpVtbl = &VT_MonikerMarshalInner;
1664 This->lpVtblMarshal = &VT_MonikerMarshal;
1666 This->moniker = inner;
1668 *outer = (IUnknown *)&This->lpVtbl;
1672 void * __RPC_USER MIDL_user_allocate(size_t size)
1674 return HeapAlloc(GetProcessHeap(), 0, size);
1677 void __RPC_USER MIDL_user_free(void *p)
1679 HeapFree(GetProcessHeap(), 0, p);