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 return I_RpcExceptionFilter(eptr->ExceptionRecord->ExceptionCode);
62 /* define the structure of the running object table elements */
66 InterfaceData* object; /* marshaled running object*/
67 MonikerComparisonData* moniker_data; /* moniker comparison data that identifies this object */
68 DWORD cookie; /* cookie identifying this object */
69 FILETIME last_modified;
70 IrotContextHandle ctxt_handle;
73 /* define the RunningObjectTableImpl structure */
74 typedef struct RunningObjectTableImpl
76 const IRunningObjectTableVtbl *lpVtbl;
79 struct list rot; /* list of ROT entries */
80 CRITICAL_SECTION lock;
81 } RunningObjectTableImpl;
83 static RunningObjectTableImpl* runningObjectTableInstance = NULL;
84 static IrotHandle irot_handle;
86 /* define the EnumMonikerImpl structure */
87 typedef struct EnumMonikerImpl
89 const IEnumMonikerVtbl *lpVtbl;
92 InterfaceList *moniker_list;
97 /* IEnumMoniker Local functions*/
98 static HRESULT WINAPI EnumMonikerImpl_CreateEnumROTMoniker(InterfaceList *moniker_list,
99 ULONG pos, IEnumMoniker **ppenumMoniker);
101 static IrotHandle get_irot_handle(void)
107 IrotHandle new_handle;
108 unsigned short ncacn_np[] = IROT_PROTSEQ;
109 unsigned short endpoint[] = IROT_ENDPOINT;
110 status = RpcStringBindingComposeW(NULL, ncacn_np, NULL, endpoint, NULL, &binding);
111 if (status == RPC_S_OK)
113 status = RpcBindingFromStringBindingW(binding, &new_handle);
114 RpcStringFreeW(&binding);
116 if (status != RPC_S_OK)
118 if (InterlockedCompareExchangePointer(&irot_handle, new_handle, NULL))
119 /* another thread beat us to it */
120 RpcBindingFree(&new_handle);
125 static BOOL start_rpcss(void)
127 PROCESS_INFORMATION pi;
130 static const WCHAR rpcss[] = {'r','p','c','s','s',0};
135 ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
136 ZeroMemory(&si, sizeof(STARTUPINFOA));
137 si.cb = sizeof(STARTUPINFOA);
139 memcpy(cmd, rpcss, sizeof(rpcss));
141 rslt = CreateProcessW(
142 NULL, /* executable */
143 cmd, /* command line */
144 NULL, /* process security attributes */
145 NULL, /* primary thread security attributes */
146 FALSE, /* inherit handles */
147 0, /* creation flags */
148 NULL, /* use parent's environment */
149 NULL, /* use parent's current directory */
150 &si, /* STARTUPINFO pointer */
151 &pi /* PROCESS_INFORMATION */
156 CloseHandle(pi.hProcess);
157 CloseHandle(pi.hThread);
164 static HRESULT create_stream_on_mip_ro(const InterfaceData *mip, IStream **stream)
166 HGLOBAL hglobal = GlobalAlloc(0, mip->ulCntData);
167 void *pv = GlobalLock(hglobal);
168 memcpy(pv, mip->abData, mip->ulCntData);
169 GlobalUnlock(hglobal);
170 return CreateStreamOnHGlobal(hglobal, TRUE, stream);
173 static inline void rot_entry_delete(struct rot_entry *rot_entry)
175 if (rot_entry->cookie)
177 InterfaceData *object = NULL;
178 InterfaceData *moniker = NULL;
181 IrotRevoke(get_irot_handle(), rot_entry->cookie,
182 &rot_entry->ctxt_handle, &object, &moniker);
188 MIDL_user_free(object);
193 hr = create_stream_on_mip_ro(moniker, &stream);
196 CoReleaseMarshalData(stream);
197 IUnknown_Release(stream);
200 MIDL_user_free(moniker);
202 if (rot_entry->object)
206 hr = create_stream_on_mip_ro(rot_entry->object, &stream);
209 CoReleaseMarshalData(stream);
210 IUnknown_Release(stream);
213 HeapFree(GetProcessHeap(), 0, rot_entry->object);
214 HeapFree(GetProcessHeap(), 0, rot_entry->moniker_data);
215 HeapFree(GetProcessHeap(), 0, rot_entry);
218 /* moniker_data must be freed with HeapFree when no longer in use */
219 static HRESULT get_moniker_comparison_data(IMoniker *pMoniker, MonikerComparisonData **moniker_data)
222 IROTData *pROTData = NULL;
223 hr = IMoniker_QueryInterface(pMoniker, &IID_IROTData, (void *)&pROTData);
226 ULONG size = MAX_COMPARISON_DATA;
227 *moniker_data = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(MonikerComparisonData, abData[size]));
230 IROTData_Release(pROTData);
231 return E_OUTOFMEMORY;
233 hr = IROTData_GetComparisonData(pROTData, (*moniker_data)->abData, size, &size);
234 IROTData_Release(pROTData);
237 ERR("Failed to copy comparison data into buffer, hr = 0x%08x\n", hr);
238 HeapFree(GetProcessHeap(), 0, *moniker_data);
241 (*moniker_data)->ulCntData = size;
246 LPOLESTR pszDisplayName;
250 TRACE("generating comparison data from display name\n");
252 hr = CreateBindCtx(0, &pbc);
255 hr = IMoniker_GetDisplayName(pMoniker, pbc, NULL, &pszDisplayName);
256 IBindCtx_Release(pbc);
259 hr = IMoniker_GetClassID(pMoniker, &clsid);
262 CoTaskMemFree(pszDisplayName);
266 len = strlenW(pszDisplayName);
267 *moniker_data = HeapAlloc(GetProcessHeap(), 0,
268 FIELD_OFFSET(MonikerComparisonData, abData[sizeof(CLSID) + (len+1)*sizeof(WCHAR)]));
271 CoTaskMemFree(pszDisplayName);
272 return E_OUTOFMEMORY;
274 (*moniker_data)->ulCntData = sizeof(CLSID) + (len+1)*sizeof(WCHAR);
276 memcpy(&(*moniker_data)->abData[0], &clsid, sizeof(clsid));
277 memcpy(&(*moniker_data)->abData[sizeof(clsid)], pszDisplayName, (len+1)*sizeof(WCHAR));
278 CoTaskMemFree(pszDisplayName);
283 static HRESULT reduce_moniker(IMoniker *pmk, IBindCtx *pbc, IMoniker **pmkReduced)
285 IBindCtx *pbcNew = NULL;
289 hr = CreateBindCtx(0, &pbcNew);
294 hr = IMoniker_Reduce(pmk, pbc, MKRREDUCE_ALL, NULL, pmkReduced);
296 ERR("reducing moniker failed with error 0x%08x\n", hr);
297 if (pbcNew) IBindCtx_Release(pbcNew);
301 /***********************************************************************
302 * RunningObjectTable_QueryInterface
304 static HRESULT WINAPI
305 RunningObjectTableImpl_QueryInterface(IRunningObjectTable* iface,
306 REFIID riid,void** ppvObject)
308 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
310 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
312 /* validate arguments */
319 if (IsEqualIID(&IID_IUnknown, riid) ||
320 IsEqualIID(&IID_IRunningObjectTable, riid))
321 *ppvObject = (IRunningObjectTable*)This;
324 return E_NOINTERFACE;
326 IRunningObjectTable_AddRef(iface);
331 /***********************************************************************
332 * RunningObjectTable_AddRef
335 RunningObjectTableImpl_AddRef(IRunningObjectTable* iface)
337 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
339 TRACE("(%p)\n",This);
341 return InterlockedIncrement(&This->ref);
344 /***********************************************************************
345 * RunningObjectTable_Initialize
347 static HRESULT WINAPI
348 RunningObjectTableImpl_Destroy(void)
350 struct list *cursor, *cursor2;
351 IrotHandle old_handle;
355 if (runningObjectTableInstance==NULL)
358 /* free the ROT table memory */
359 LIST_FOR_EACH_SAFE(cursor, cursor2, &runningObjectTableInstance->rot)
361 struct rot_entry *rot_entry = LIST_ENTRY(cursor, struct rot_entry, entry);
362 list_remove(&rot_entry->entry);
363 rot_entry_delete(rot_entry);
366 DEBUG_CLEAR_CRITSEC_NAME(&runningObjectTableInstance->lock);
367 DeleteCriticalSection(&runningObjectTableInstance->lock);
369 /* free the ROT structure memory */
370 HeapFree(GetProcessHeap(),0,runningObjectTableInstance);
371 runningObjectTableInstance = NULL;
373 old_handle = irot_handle;
376 RpcBindingFree(&old_handle);
381 /***********************************************************************
382 * RunningObjectTable_Release
385 RunningObjectTableImpl_Release(IRunningObjectTable* iface)
387 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
390 TRACE("(%p)\n",This);
392 ref = InterlockedDecrement(&This->ref);
394 /* uninitialize ROT structure if there's no more references to it */
397 struct list *cursor, *cursor2;
398 LIST_FOR_EACH_SAFE(cursor, cursor2, &This->rot)
400 struct rot_entry *rot_entry = LIST_ENTRY(cursor, struct rot_entry, entry);
401 list_remove(&rot_entry->entry);
402 rot_entry_delete(rot_entry);
404 /* RunningObjectTable data structure will be not destroyed here ! the destruction will be done only
405 * when RunningObjectTableImpl_UnInitialize function is called
412 /***********************************************************************
413 * RunningObjectTable_Register
416 * grfFlags [in] Registration options
417 * punkObject [in] the object being registered
418 * pmkObjectName [in] the moniker of the object being registered
419 * pdwRegister [in] the value identifying the registration
421 static HRESULT WINAPI
422 RunningObjectTableImpl_Register(IRunningObjectTable* iface, DWORD grfFlags,
423 IUnknown *punkObject, IMoniker *pmkObjectName, DWORD *pdwRegister)
425 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
426 struct rot_entry *rot_entry;
428 IStream *pStream = NULL;
431 InterfaceData *moniker = NULL;
433 TRACE("(%p,%d,%p,%p,%p)\n",This,grfFlags,punkObject,pmkObjectName,pdwRegister);
435 if (grfFlags & ~(ROTFLAGS_REGISTRATIONKEEPSALIVE|ROTFLAGS_ALLOWANYCLIENT))
437 ERR("Invalid grfFlags: 0x%08x\n", grfFlags & ~(ROTFLAGS_REGISTRATIONKEEPSALIVE|ROTFLAGS_ALLOWANYCLIENT));
441 if (punkObject==NULL || pmkObjectName==NULL || pdwRegister==NULL)
444 rot_entry = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*rot_entry));
446 return E_OUTOFMEMORY;
449 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
452 rot_entry_delete(rot_entry);
455 mshlflags = (grfFlags & ROTFLAGS_REGISTRATIONKEEPSALIVE) ? MSHLFLAGS_TABLESTRONG : MSHLFLAGS_TABLEWEAK;
456 hr = CoMarshalInterface(pStream, &IID_IUnknown, punkObject, MSHCTX_LOCAL | MSHCTX_NOSHAREDMEM, NULL, mshlflags);
457 /* FIXME: a cleaner way would be to create an IStream class that writes
458 * directly to an MInterfacePointer */
462 hr = GetHGlobalFromStream(pStream, &hglobal);
465 SIZE_T size = GlobalSize(hglobal);
466 const void *pv = GlobalLock(hglobal);
467 rot_entry->object = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(MInterfacePointer, abData[size]));
468 rot_entry->object->ulCntData = size;
469 memcpy(&rot_entry->object->abData, pv, size);
470 GlobalUnlock(hglobal);
473 IStream_Release(pStream);
476 rot_entry_delete(rot_entry);
480 hr = CreateBindCtx(0, &pbc);
483 rot_entry_delete(rot_entry);
487 hr = reduce_moniker(pmkObjectName, pbc, &pmkObjectName);
490 rot_entry_delete(rot_entry);
491 IBindCtx_Release(pbc);
495 hr = IMoniker_GetTimeOfLastChange(pmkObjectName, pbc, NULL,
496 &rot_entry->last_modified);
497 IBindCtx_Release(pbc);
500 CoFileTimeNow(&rot_entry->last_modified);
504 hr = get_moniker_comparison_data(pmkObjectName,
505 &rot_entry->moniker_data);
508 rot_entry_delete(rot_entry);
509 IMoniker_Release(pmkObjectName);
513 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
516 rot_entry_delete(rot_entry);
517 IMoniker_Release(pmkObjectName);
520 /* marshal moniker */
521 hr = CoMarshalInterface(pStream, &IID_IMoniker, (IUnknown *)pmkObjectName,
522 MSHCTX_LOCAL | MSHCTX_NOSHAREDMEM, NULL, MSHLFLAGS_TABLESTRONG);
523 /* FIXME: a cleaner way would be to create an IStream class that writes
524 * directly to an MInterfacePointer */
528 hr = GetHGlobalFromStream(pStream, &hglobal);
531 SIZE_T size = GlobalSize(hglobal);
532 const void *pv = GlobalLock(hglobal);
533 moniker = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(InterfaceData, abData[size]));
534 moniker->ulCntData = size;
535 memcpy(&moniker->abData, pv, size);
536 GlobalUnlock(hglobal);
539 IStream_Release(pStream);
540 IMoniker_Release(pmkObjectName);
543 HeapFree(GetProcessHeap(), 0, moniker);
544 rot_entry_delete(rot_entry);
553 hr = IrotRegister(get_irot_handle(), rot_entry->moniker_data,
554 rot_entry->object, moniker,
555 &rot_entry->last_modified, grfFlags,
556 &rot_entry->cookie, &rot_entry->ctxt_handle);
560 hr = HRESULT_FROM_WIN32(GetExceptionCode());
563 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
570 HeapFree(GetProcessHeap(), 0, moniker);
573 rot_entry_delete(rot_entry);
577 /* gives a registration identifier to the registered object*/
578 *pdwRegister = rot_entry->cookie;
580 EnterCriticalSection(&This->lock);
581 list_add_tail(&This->rot, &rot_entry->entry);
582 LeaveCriticalSection(&This->lock);
587 /***********************************************************************
588 * RunningObjectTable_Revoke
591 * dwRegister [in] Value identifying registration to be revoked
593 static HRESULT WINAPI
594 RunningObjectTableImpl_Revoke( IRunningObjectTable* iface, DWORD dwRegister)
596 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
597 struct rot_entry *rot_entry;
599 TRACE("(%p,%d)\n",This,dwRegister);
601 EnterCriticalSection(&This->lock);
602 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, struct rot_entry, entry)
604 if (rot_entry->cookie == dwRegister)
606 list_remove(&rot_entry->entry);
607 LeaveCriticalSection(&This->lock);
609 rot_entry_delete(rot_entry);
613 LeaveCriticalSection(&This->lock);
618 /***********************************************************************
619 * RunningObjectTable_IsRunning
622 * pmkObjectName [in] moniker of the object whose status is desired
624 static HRESULT WINAPI
625 RunningObjectTableImpl_IsRunning( IRunningObjectTable* iface, IMoniker *pmkObjectName)
627 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
628 MonikerComparisonData *moniker_data;
630 const struct rot_entry *rot_entry;
632 TRACE("(%p,%p)\n",This,pmkObjectName);
634 hr = reduce_moniker(pmkObjectName, NULL, &pmkObjectName);
637 hr = get_moniker_comparison_data(pmkObjectName, &moniker_data);
638 IMoniker_Release(pmkObjectName);
643 EnterCriticalSection(&This->lock);
644 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, const struct rot_entry, entry)
646 if ((rot_entry->moniker_data->ulCntData == moniker_data->ulCntData) &&
647 !memcmp(&moniker_data->abData, &rot_entry->moniker_data->abData, moniker_data->ulCntData))
653 LeaveCriticalSection(&This->lock);
661 hr = IrotIsRunning(get_irot_handle(), moniker_data);
665 hr = HRESULT_FROM_WIN32(GetExceptionCode());
668 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
677 HeapFree(GetProcessHeap(), 0, moniker_data);
682 /***********************************************************************
683 * RunningObjectTable_GetObject
686 * pmkObjectName [in] Pointer to the moniker on the object
687 * ppunkObject [out] variable that receives the IUnknown interface pointer
689 static HRESULT WINAPI
690 RunningObjectTableImpl_GetObject( IRunningObjectTable* iface,
691 IMoniker *pmkObjectName, IUnknown **ppunkObject)
693 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
694 MonikerComparisonData *moniker_data;
695 InterfaceData *object = NULL;
698 struct rot_entry *rot_entry;
700 TRACE("(%p,%p,%p)\n",This,pmkObjectName,ppunkObject);
702 if (ppunkObject == NULL)
707 hr = reduce_moniker(pmkObjectName, NULL, &pmkObjectName);
710 hr = get_moniker_comparison_data(pmkObjectName, &moniker_data);
711 IMoniker_Release(pmkObjectName);
715 EnterCriticalSection(&This->lock);
716 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, struct rot_entry, entry)
718 if ((rot_entry->moniker_data->ulCntData == moniker_data->ulCntData) &&
719 !memcmp(&moniker_data->abData, &rot_entry->moniker_data->abData, moniker_data->ulCntData))
722 hr = create_stream_on_mip_ro(rot_entry->object, &pStream);
725 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)ppunkObject);
726 IStream_Release(pStream);
729 LeaveCriticalSection(&This->lock);
730 HeapFree(GetProcessHeap(), 0, moniker_data);
735 LeaveCriticalSection(&This->lock);
737 TRACE("moniker unavailable locally, calling SCM\n");
743 hr = IrotGetObject(get_irot_handle(), moniker_data, &object, &cookie);
747 hr = HRESULT_FROM_WIN32(GetExceptionCode());
750 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
761 hr = create_stream_on_mip_ro(object, &pStream);
764 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)ppunkObject);
765 IStream_Release(pStream);
769 WARN("Moniker unavailable, IrotGetObject returned 0x%08x\n", hr);
771 HeapFree(GetProcessHeap(), 0, moniker_data);
776 /***********************************************************************
777 * RunningObjectTable_NoteChangeTime
780 * dwRegister [in] Value identifying registration being updated
781 * pfiletime [in] Pointer to structure containing object's last change time
783 static HRESULT WINAPI
784 RunningObjectTableImpl_NoteChangeTime(IRunningObjectTable* iface,
785 DWORD dwRegister, FILETIME *pfiletime)
787 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
788 struct rot_entry *rot_entry;
789 HRESULT hr = E_INVALIDARG;
791 TRACE("(%p,%d,%p)\n",This,dwRegister,pfiletime);
793 EnterCriticalSection(&This->lock);
794 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, struct rot_entry, entry)
796 if (rot_entry->cookie == dwRegister)
798 rot_entry->last_modified = *pfiletime;
799 LeaveCriticalSection(&This->lock);
805 hr = IrotNoteChangeTime(get_irot_handle(), dwRegister, pfiletime);
809 hr = HRESULT_FROM_WIN32(GetExceptionCode());
812 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
823 LeaveCriticalSection(&This->lock);
826 TRACE("-- 0x08%x\n", hr);
830 /***********************************************************************
831 * RunningObjectTable_GetTimeOfLastChange
834 * pmkObjectName [in] moniker of the object whose status is desired
835 * pfiletime [out] structure that receives object's last change time
837 static HRESULT WINAPI
838 RunningObjectTableImpl_GetTimeOfLastChange(IRunningObjectTable* iface,
839 IMoniker *pmkObjectName, FILETIME *pfiletime)
841 HRESULT hr = MK_E_UNAVAILABLE;
842 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
843 MonikerComparisonData *moniker_data;
844 const struct rot_entry *rot_entry;
846 TRACE("(%p,%p,%p)\n",This,pmkObjectName,pfiletime);
848 if (pmkObjectName==NULL || pfiletime==NULL)
851 hr = reduce_moniker(pmkObjectName, NULL, &pmkObjectName);
854 hr = get_moniker_comparison_data(pmkObjectName, &moniker_data);
855 IMoniker_Release(pmkObjectName);
859 hr = MK_E_UNAVAILABLE;
861 EnterCriticalSection(&This->lock);
862 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, const struct rot_entry, entry)
864 if ((rot_entry->moniker_data->ulCntData == moniker_data->ulCntData) &&
865 !memcmp(&moniker_data->abData, &rot_entry->moniker_data->abData, moniker_data->ulCntData))
867 *pfiletime = rot_entry->last_modified;
872 LeaveCriticalSection(&This->lock);
880 hr = IrotGetTimeOfLastChange(get_irot_handle(), moniker_data, pfiletime);
884 hr = HRESULT_FROM_WIN32(GetExceptionCode());
887 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
896 HeapFree(GetProcessHeap(), 0, moniker_data);
898 TRACE("-- 0x%08x\n", hr);
902 /***********************************************************************
903 * RunningObjectTable_EnumRunning
906 * ppenumMoniker [out] receives the IEnumMoniker interface pointer
908 static HRESULT WINAPI
909 RunningObjectTableImpl_EnumRunning(IRunningObjectTable* iface,
910 IEnumMoniker **ppenumMoniker)
912 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
913 InterfaceList *interface_list = NULL;
916 TRACE("(%p, %p)\n", This, ppenumMoniker);
918 *ppenumMoniker = NULL;
924 hr = IrotEnumRunning(get_irot_handle(), &interface_list);
928 hr = HRESULT_FROM_WIN32(GetExceptionCode());
931 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
940 hr = EnumMonikerImpl_CreateEnumROTMoniker(interface_list,
946 /* Virtual function table for the IRunningObjectTable class. */
947 static const IRunningObjectTableVtbl VT_RunningObjectTableImpl =
949 RunningObjectTableImpl_QueryInterface,
950 RunningObjectTableImpl_AddRef,
951 RunningObjectTableImpl_Release,
952 RunningObjectTableImpl_Register,
953 RunningObjectTableImpl_Revoke,
954 RunningObjectTableImpl_IsRunning,
955 RunningObjectTableImpl_GetObject,
956 RunningObjectTableImpl_NoteChangeTime,
957 RunningObjectTableImpl_GetTimeOfLastChange,
958 RunningObjectTableImpl_EnumRunning
961 /***********************************************************************
962 * RunningObjectTable_Initialize
964 HRESULT WINAPI RunningObjectTableImpl_Initialize(void)
968 /* create the unique instance of the RunningObjectTableImpl structure */
969 runningObjectTableInstance = HeapAlloc(GetProcessHeap(), 0, sizeof(RunningObjectTableImpl));
971 if (!runningObjectTableInstance)
972 return E_OUTOFMEMORY;
974 /* initialize the virtual table function */
975 runningObjectTableInstance->lpVtbl = &VT_RunningObjectTableImpl;
977 /* the initial reference is set to "1" ! because if set to "0" it will be not practis when */
978 /* the ROT referred many times not in the same time (all the objects in the ROT will */
979 /* be removed every time the ROT is removed ) */
980 runningObjectTableInstance->ref = 1;
982 list_init(&runningObjectTableInstance->rot);
983 InitializeCriticalSection(&runningObjectTableInstance->lock);
984 DEBUG_SET_CRITSEC_NAME(&runningObjectTableInstance->lock, "RunningObjectTableImpl.lock");
989 /***********************************************************************
990 * RunningObjectTable_UnInitialize
992 HRESULT WINAPI RunningObjectTableImpl_UnInitialize(void)
996 if (runningObjectTableInstance==NULL)
999 RunningObjectTableImpl_Release((IRunningObjectTable*)runningObjectTableInstance);
1001 RunningObjectTableImpl_Destroy();
1006 /***********************************************************************
1007 * GetRunningObjectTable (OLE32.@)
1009 * Retrieves the global running object table.
1012 * reserved [I] Reserved. Set to 0.
1013 * pprot [O] Address that receives the pointer to the running object table.
1017 * Failure: Any HRESULT code.
1020 GetRunningObjectTable(DWORD reserved, LPRUNNINGOBJECTTABLE *pprot)
1022 IID riid=IID_IRunningObjectTable;
1028 return E_UNEXPECTED;
1030 if(runningObjectTableInstance==NULL)
1031 return CO_E_NOTINITIALIZED;
1033 res = IRunningObjectTable_QueryInterface((IRunningObjectTable*)runningObjectTableInstance,&riid,(void**)pprot);
1038 static HRESULT get_moniker_for_progid_display_name(LPBC pbc,
1039 LPCOLESTR szDisplayName,
1046 LPCWSTR start = szDisplayName;
1049 IMoniker *class_moniker;
1054 /* find end delimiter */
1055 for (end = start; *end; end++)
1061 /* must start with '@' or have a ':' somewhere and mustn't be one character
1062 * long (since that looks like an absolute path) */
1063 if (((start == szDisplayName) && (*end == '\0')) || (len <= 1))
1066 progid = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1069 memcpy(progid, start, len * sizeof(WCHAR));
1072 hr = CLSIDFromProgID(progid, &clsid);
1073 HeapFree(GetProcessHeap(), 0, progid);
1077 hr = CreateClassMoniker(&clsid, &class_moniker);
1080 IParseDisplayName *pdn;
1081 hr = IMoniker_BindToObject(class_moniker, pbc, NULL,
1082 &IID_IParseDisplayName, (void **)&pdn);
1083 /* fallback to using IClassFactory to get IParseDisplayName -
1084 * adsldp.dll depends on this */
1088 hr = IMoniker_BindToObject(class_moniker, pbc, NULL,
1089 &IID_IClassFactory, (void **)&pcf);
1092 hr = IClassFactory_CreateInstance(pcf, NULL,
1093 &IID_IParseDisplayName,
1095 IClassFactory_Release(pcf);
1098 IMoniker_Release(class_moniker);
1101 hr = IParseDisplayName_ParseDisplayName(pdn, pbc,
1102 (LPOLESTR)szDisplayName,
1104 IParseDisplayName_Release(pdn);
1110 /******************************************************************************
1111 * MkParseDisplayName [OLE32.@]
1113 HRESULT WINAPI MkParseDisplayName(LPBC pbc, LPCOLESTR szDisplayName,
1114 LPDWORD pchEaten, LPMONIKER *ppmk)
1116 HRESULT hr = MK_E_SYNTAX;
1117 static const WCHAR wszClsidColon[] = {'c','l','s','i','d',':'};
1121 TRACE("(%p, %s, %p, %p)\n", pbc, debugstr_w(szDisplayName), pchEaten, ppmk);
1123 if (!(IsValidInterface((LPUNKNOWN) pbc)))
1124 return E_INVALIDARG;
1129 if (!strncmpiW(szDisplayName, wszClsidColon, sizeof(wszClsidColon)/sizeof(wszClsidColon[0])))
1131 hr = ClassMoniker_CreateFromDisplayName(pbc, szDisplayName, &chEaten, &moniker);
1132 if (FAILED(hr) && (hr != MK_E_SYNTAX))
1137 hr = get_moniker_for_progid_display_name(pbc, szDisplayName, &chEaten, &moniker);
1138 if (FAILED(hr) && (hr != MK_E_SYNTAX))
1144 hr = FileMoniker_CreateFromDisplayName(pbc, szDisplayName, &chEaten, &moniker);
1145 if (FAILED(hr) && (hr != MK_E_SYNTAX))
1153 IMoniker *next_moniker;
1154 *pchEaten += chEaten;
1155 szDisplayName += chEaten;
1156 if (!*szDisplayName)
1162 hr = IMoniker_ParseDisplayName(moniker, pbc, NULL,
1163 (LPOLESTR)szDisplayName, &chEaten,
1165 IMoniker_Release(moniker);
1171 moniker = next_moniker;
1178 /***********************************************************************
1179 * GetClassFile (OLE32.@)
1181 * Retrieves the class ID associated with the given filename.
1184 * filePathName [I] Filename to retrieve the class ID for.
1185 * pclsid [O] Address that receives the class ID for the file.
1189 * Failure: Any HRESULT code.
1191 HRESULT WINAPI GetClassFile(LPCOLESTR filePathName,CLSID *pclsid)
1195 int nbElm, length, i;
1197 LPOLESTR *pathDec=0,absFile=0,progId=0;
1199 static const WCHAR bkslashW[] = {'\\',0};
1200 static const WCHAR dotW[] = {'.',0};
1202 TRACE("%s, %p\n", debugstr_w(filePathName), pclsid);
1204 /* if the file contain a storage object the return the CLSID written by IStorage_SetClass method*/
1205 if((StgIsStorageFile(filePathName))==S_OK){
1207 res=StgOpenStorage(filePathName,NULL,STGM_READ | STGM_SHARE_DENY_WRITE,NULL,0,&pstg);
1210 res=ReadClassStg(pstg,pclsid);
1212 IStorage_Release(pstg);
1216 /* If the file is not a storage object then attempt to match various bits in the file against a
1217 pattern in the registry. This case is not frequently used, so I present only the psodocode for
1220 for(i=0;i<nFileTypes;i++)
1222 for(i=0;j<nPatternsForType;j++){
1227 pat=ReadPatternFromRegistry(i,j);
1228 hFile=CreateFileW(filePathName,,,,,,hFile);
1229 SetFilePosition(hFile,pat.offset);
1230 ReadFile(hFile,buf,pat.size,&r,NULL);
1231 if (memcmp(buf&pat.mask,pat.pattern.pat.size)==0){
1233 *pclsid=ReadCLSIDFromRegistry(i);
1239 /* if the above strategies fail then search for the extension key in the registry */
1241 /* get the last element (absolute file) in the path name */
1242 nbElm=FileMonikerImpl_DecomposePath(filePathName,&pathDec);
1243 absFile=pathDec[nbElm-1];
1245 /* failed if the path represents a directory and not an absolute file name*/
1246 if (!lstrcmpW(absFile, bkslashW))
1247 return MK_E_INVALIDEXTENSION;
1249 /* get the extension of the file */
1251 length=lstrlenW(absFile);
1252 for(i = length-1; (i >= 0) && *(extension = &absFile[i]) != '.'; i--)
1255 if (!extension || !lstrcmpW(extension, dotW))
1256 return MK_E_INVALIDEXTENSION;
1258 res=RegQueryValueW(HKEY_CLASSES_ROOT, extension, NULL, &sizeProgId);
1260 /* get the progId associated to the extension */
1261 progId = CoTaskMemAlloc(sizeProgId);
1262 res = RegQueryValueW(HKEY_CLASSES_ROOT, extension, progId, &sizeProgId);
1264 if (res==ERROR_SUCCESS)
1265 /* return the clsid associated to the progId */
1266 res= CLSIDFromProgID(progId,pclsid);
1268 for(i=0; pathDec[i]!=NULL;i++)
1269 CoTaskMemFree(pathDec[i]);
1270 CoTaskMemFree(pathDec);
1272 CoTaskMemFree(progId);
1274 if (res==ERROR_SUCCESS)
1277 return MK_E_INVALIDEXTENSION;
1280 /***********************************************************************
1281 * EnumMoniker_QueryInterface
1283 static HRESULT WINAPI EnumMonikerImpl_QueryInterface(IEnumMoniker* iface,REFIID riid,void** ppvObject)
1285 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1287 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
1289 /* validate arguments */
1290 if (ppvObject == NULL)
1291 return E_INVALIDARG;
1295 if (IsEqualIID(&IID_IUnknown, riid))
1296 *ppvObject = (IEnumMoniker*)This;
1298 if (IsEqualIID(&IID_IEnumMoniker, riid))
1299 *ppvObject = (IEnumMoniker*)This;
1301 if ((*ppvObject)==NULL)
1302 return E_NOINTERFACE;
1304 IEnumMoniker_AddRef(iface);
1309 /***********************************************************************
1310 * EnumMoniker_AddRef
1312 static ULONG WINAPI EnumMonikerImpl_AddRef(IEnumMoniker* iface)
1314 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1316 TRACE("(%p)\n",This);
1318 return InterlockedIncrement(&This->ref);
1321 /***********************************************************************
1322 * EnumMoniker_release
1324 static ULONG WINAPI EnumMonikerImpl_Release(IEnumMoniker* iface)
1326 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1329 TRACE("(%p)\n",This);
1331 ref = InterlockedDecrement(&This->ref);
1333 /* uninitialize rot structure if there's no more reference to it*/
1338 TRACE("(%p) Deleting\n",This);
1340 for (i = 0; i < This->moniker_list->size; i++)
1341 HeapFree(GetProcessHeap(), 0, This->moniker_list->interfaces[i]);
1342 HeapFree(GetProcessHeap(), 0, This->moniker_list);
1343 HeapFree(GetProcessHeap(), 0, This);
1348 /***********************************************************************
1351 static HRESULT WINAPI EnumMonikerImpl_Next(IEnumMoniker* iface, ULONG celt, IMoniker** rgelt, ULONG * pceltFetched)
1354 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1357 TRACE("(%p) TabCurrentPos %d Tablastindx %d\n", This, This->pos, This->moniker_list->size);
1359 /* retrieve the requested number of moniker from the current position */
1360 for(i = 0; (This->pos < This->moniker_list->size) && (i < celt); i++)
1363 hr = create_stream_on_mip_ro(This->moniker_list->interfaces[This->pos++], &stream);
1364 if (hr != S_OK) break;
1365 hr = CoUnmarshalInterface(stream, &IID_IMoniker, (void **)&rgelt[i]);
1366 IStream_Release(stream);
1367 if (hr != S_OK) break;
1370 if (pceltFetched != NULL)
1383 /***********************************************************************
1386 static HRESULT WINAPI EnumMonikerImpl_Skip(IEnumMoniker* iface, ULONG celt)
1388 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1390 TRACE("(%p)\n",This);
1392 if (This->pos + celt >= This->moniker_list->size)
1400 /***********************************************************************
1401 * EnmumMoniker_Reset
1403 static HRESULT WINAPI EnumMonikerImpl_Reset(IEnumMoniker* iface)
1405 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1407 This->pos = 0; /* set back to start of list */
1409 TRACE("(%p)\n",This);
1414 /***********************************************************************
1415 * EnmumMoniker_Clone
1417 static HRESULT WINAPI EnumMonikerImpl_Clone(IEnumMoniker* iface, IEnumMoniker ** ppenum)
1419 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1420 InterfaceList *moniker_list;
1423 TRACE("(%p)\n",This);
1427 moniker_list = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(InterfaceList, interfaces[This->moniker_list->size]));
1429 return E_OUTOFMEMORY;
1431 moniker_list->size = This->moniker_list->size;
1432 for (i = 0; i < This->moniker_list->size; i++)
1434 SIZE_T size = FIELD_OFFSET(InterfaceData, abData[This->moniker_list->interfaces[i]->ulCntData]);
1435 moniker_list->interfaces[i] = HeapAlloc(GetProcessHeap(), 0, size);
1436 if (!moniker_list->interfaces[i])
1439 for (i = 0; i < end; i++)
1440 HeapFree(GetProcessHeap(), 0, moniker_list->interfaces[i]);
1441 HeapFree(GetProcessHeap(), 0, moniker_list);
1442 return E_OUTOFMEMORY;
1444 memcpy(moniker_list->interfaces[i], This->moniker_list->interfaces[i], size);
1447 /* copy the enum structure */
1448 return EnumMonikerImpl_CreateEnumROTMoniker(moniker_list, This->pos, ppenum);
1451 /* Virtual function table for the IEnumMoniker class. */
1452 static const IEnumMonikerVtbl VT_EnumMonikerImpl =
1454 EnumMonikerImpl_QueryInterface,
1455 EnumMonikerImpl_AddRef,
1456 EnumMonikerImpl_Release,
1457 EnumMonikerImpl_Next,
1458 EnumMonikerImpl_Skip,
1459 EnumMonikerImpl_Reset,
1460 EnumMonikerImpl_Clone
1463 /***********************************************************************
1464 * EnumMonikerImpl_CreateEnumROTMoniker
1465 * Used by EnumRunning to create the structure and EnumClone
1466 * to copy the structure
1468 static HRESULT WINAPI EnumMonikerImpl_CreateEnumROTMoniker(InterfaceList *moniker_list,
1470 IEnumMoniker **ppenumMoniker)
1472 EnumMonikerImpl* This = NULL;
1475 return E_INVALIDARG;
1477 This = HeapAlloc(GetProcessHeap(), 0, sizeof(EnumMonikerImpl));
1478 if (!This) return E_OUTOFMEMORY;
1480 TRACE("(%p)\n", This);
1482 /* initialize the virtual table function */
1483 This->lpVtbl = &VT_EnumMonikerImpl;
1485 /* the initial reference is set to "1" */
1486 This->ref = 1; /* set the ref count to one */
1487 This->pos = current_pos; /* Set the list start posn */
1488 This->moniker_list = moniker_list;
1490 *ppenumMoniker = (IEnumMoniker*)This;
1496 /* Shared implementation of moniker marshaler based on saving and loading of
1499 typedef struct MonikerMarshal
1501 const IUnknownVtbl *lpVtbl;
1502 const IMarshalVtbl *lpVtblMarshal;
1508 static inline MonikerMarshal *impl_from_IMarshal( IMarshal *iface )
1510 return (MonikerMarshal *)((char*)iface - FIELD_OFFSET(MonikerMarshal, lpVtblMarshal));
1513 static HRESULT WINAPI MonikerMarshalInner_QueryInterface(IUnknown *iface, REFIID riid, LPVOID *ppv)
1515 MonikerMarshal *This = (MonikerMarshal *)iface;
1516 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
1518 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IMarshal, riid))
1520 *ppv = &This->lpVtblMarshal;
1521 IUnknown_AddRef((IUnknown *)&This->lpVtblMarshal);
1524 FIXME("No interface for %s\n", debugstr_guid(riid));
1525 return E_NOINTERFACE;
1528 static ULONG WINAPI MonikerMarshalInner_AddRef(IUnknown *iface)
1530 MonikerMarshal *This = (MonikerMarshal *)iface;
1531 return InterlockedIncrement(&This->ref);
1534 static ULONG WINAPI MonikerMarshalInner_Release(IUnknown *iface)
1536 MonikerMarshal *This = (MonikerMarshal *)iface;
1537 ULONG ref = InterlockedDecrement(&This->ref);
1539 if (!ref) HeapFree(GetProcessHeap(), 0, This);
1543 static const IUnknownVtbl VT_MonikerMarshalInner =
1545 MonikerMarshalInner_QueryInterface,
1546 MonikerMarshalInner_AddRef,
1547 MonikerMarshalInner_Release
1550 static HRESULT WINAPI MonikerMarshal_QueryInterface(IMarshal *iface, REFIID riid, LPVOID *ppv)
1552 MonikerMarshal *This = impl_from_IMarshal(iface);
1553 return IMoniker_QueryInterface(This->moniker, riid, ppv);
1556 static ULONG WINAPI MonikerMarshal_AddRef(IMarshal *iface)
1558 MonikerMarshal *This = impl_from_IMarshal(iface);
1559 return IMoniker_AddRef(This->moniker);
1562 static ULONG WINAPI MonikerMarshal_Release(IMarshal *iface)
1564 MonikerMarshal *This = impl_from_IMarshal(iface);
1565 return IMoniker_Release(This->moniker);
1568 static HRESULT WINAPI MonikerMarshal_GetUnmarshalClass(
1569 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
1570 void* pvDestContext, DWORD mshlflags, CLSID* pCid)
1572 MonikerMarshal *This = impl_from_IMarshal(iface);
1574 TRACE("(%s, %p, %x, %p, %x, %p)\n", debugstr_guid(riid), pv,
1575 dwDestContext, pvDestContext, mshlflags, pCid);
1577 return IMoniker_GetClassID(This->moniker, pCid);
1580 static HRESULT WINAPI MonikerMarshal_GetMarshalSizeMax(
1581 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
1582 void* pvDestContext, DWORD mshlflags, DWORD* pSize)
1584 MonikerMarshal *This = impl_from_IMarshal(iface);
1586 ULARGE_INTEGER size;
1588 TRACE("(%s, %p, %x, %p, %x, %p)\n", debugstr_guid(riid), pv,
1589 dwDestContext, pvDestContext, mshlflags, pSize);
1591 hr = IMoniker_GetSizeMax(This->moniker, &size);
1593 *pSize = (DWORD)size.QuadPart;
1597 static HRESULT WINAPI MonikerMarshal_MarshalInterface(LPMARSHAL iface, IStream *pStm,
1598 REFIID riid, void* pv, DWORD dwDestContext,
1599 void* pvDestContext, DWORD mshlflags)
1601 MonikerMarshal *This = impl_from_IMarshal(iface);
1603 TRACE("(%p, %s, %p, %x, %p, %x)\n", pStm, debugstr_guid(riid), pv,
1604 dwDestContext, pvDestContext, mshlflags);
1606 return IMoniker_Save(This->moniker, pStm, FALSE);
1609 static HRESULT WINAPI MonikerMarshal_UnmarshalInterface(LPMARSHAL iface, IStream *pStm, REFIID riid, void **ppv)
1611 MonikerMarshal *This = impl_from_IMarshal(iface);
1614 TRACE("(%p, %s, %p)\n", pStm, debugstr_guid(riid), ppv);
1616 hr = IMoniker_Load(This->moniker, pStm);
1618 hr = IMoniker_QueryInterface(This->moniker, riid, ppv);
1622 static HRESULT WINAPI MonikerMarshal_ReleaseMarshalData(LPMARSHAL iface, IStream *pStm)
1625 /* can't release a state-based marshal as nothing on server side to
1630 static HRESULT WINAPI MonikerMarshal_DisconnectObject(LPMARSHAL iface, DWORD dwReserved)
1633 /* can't disconnect a state-based marshal as nothing on server side to
1634 * disconnect from */
1638 static const IMarshalVtbl VT_MonikerMarshal =
1640 MonikerMarshal_QueryInterface,
1641 MonikerMarshal_AddRef,
1642 MonikerMarshal_Release,
1643 MonikerMarshal_GetUnmarshalClass,
1644 MonikerMarshal_GetMarshalSizeMax,
1645 MonikerMarshal_MarshalInterface,
1646 MonikerMarshal_UnmarshalInterface,
1647 MonikerMarshal_ReleaseMarshalData,
1648 MonikerMarshal_DisconnectObject
1651 HRESULT MonikerMarshal_Create(IMoniker *inner, IUnknown **outer)
1653 MonikerMarshal *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1654 if (!This) return E_OUTOFMEMORY;
1656 This->lpVtbl = &VT_MonikerMarshalInner;
1657 This->lpVtblMarshal = &VT_MonikerMarshal;
1659 This->moniker = inner;
1661 *outer = (IUnknown *)&This->lpVtbl;
1665 void * __RPC_USER MIDL_user_allocate(size_t size)
1667 return HeapAlloc(GetProcessHeap(), 0, size);
1670 void __RPC_USER MIDL_user_free(void *p)
1672 HeapFree(GetProcessHeap(), 0, p);