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;
76 IrotContextHandle ctxt_handle;
79 /* define the RunningObjectTableImpl structure */
80 typedef struct RunningObjectTableImpl
82 const IRunningObjectTableVtbl *lpVtbl;
85 struct list rot; /* list of ROT entries */
86 CRITICAL_SECTION lock;
87 } RunningObjectTableImpl;
89 static RunningObjectTableImpl* runningObjectTableInstance = NULL;
90 static IrotHandle irot_handle;
92 /* define the EnumMonikerImpl structure */
93 typedef struct EnumMonikerImpl
95 const IEnumMonikerVtbl *lpVtbl;
98 InterfaceList *moniker_list;
103 /* IEnumMoniker Local functions*/
104 static HRESULT WINAPI EnumMonikerImpl_CreateEnumROTMoniker(InterfaceList *moniker_list,
105 ULONG pos, IEnumMoniker **ppenumMoniker);
107 static IrotHandle get_irot_handle(void)
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,
188 &rot_entry->ctxt_handle, &object, &moniker);
194 MIDL_user_free(object);
199 hr = create_stream_on_mip_ro(moniker, &stream);
202 CoReleaseMarshalData(stream);
203 IUnknown_Release(stream);
206 MIDL_user_free(moniker);
208 if (rot_entry->object)
212 hr = create_stream_on_mip_ro(rot_entry->object, &stream);
215 CoReleaseMarshalData(stream);
216 IUnknown_Release(stream);
219 HeapFree(GetProcessHeap(), 0, rot_entry->object);
220 HeapFree(GetProcessHeap(), 0, rot_entry->moniker_data);
221 HeapFree(GetProcessHeap(), 0, rot_entry);
224 /* moniker_data must be freed with HeapFree when no longer in use */
225 static HRESULT get_moniker_comparison_data(IMoniker *pMoniker, MonikerComparisonData **moniker_data)
228 IROTData *pROTData = NULL;
229 hr = IMoniker_QueryInterface(pMoniker, &IID_IROTData, (void *)&pROTData);
232 ULONG size = MAX_COMPARISON_DATA;
233 *moniker_data = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(MonikerComparisonData, abData[size]));
236 IROTData_Release(pROTData);
237 return E_OUTOFMEMORY;
239 hr = IROTData_GetComparisonData(pROTData, (*moniker_data)->abData, size, &size);
240 IROTData_Release(pROTData);
243 ERR("Failed to copy comparison data into buffer, hr = 0x%08x\n", hr);
244 HeapFree(GetProcessHeap(), 0, *moniker_data);
247 (*moniker_data)->ulCntData = size;
252 LPOLESTR pszDisplayName;
256 TRACE("generating comparison data from display name\n");
258 hr = CreateBindCtx(0, &pbc);
261 hr = IMoniker_GetDisplayName(pMoniker, pbc, NULL, &pszDisplayName);
262 IBindCtx_Release(pbc);
265 hr = IMoniker_GetClassID(pMoniker, &clsid);
268 CoTaskMemFree(pszDisplayName);
272 len = strlenW(pszDisplayName);
273 *moniker_data = HeapAlloc(GetProcessHeap(), 0,
274 FIELD_OFFSET(MonikerComparisonData, abData[sizeof(CLSID) + (len+1)*sizeof(WCHAR)]));
277 CoTaskMemFree(pszDisplayName);
278 return E_OUTOFMEMORY;
280 (*moniker_data)->ulCntData = sizeof(CLSID) + (len+1)*sizeof(WCHAR);
282 memcpy(&(*moniker_data)->abData[0], &clsid, sizeof(clsid));
283 memcpy(&(*moniker_data)->abData[sizeof(clsid)], pszDisplayName, (len+1)*sizeof(WCHAR));
284 CoTaskMemFree(pszDisplayName);
289 static HRESULT reduce_moniker(IMoniker *pmk, IBindCtx *pbc, IMoniker **pmkReduced)
291 IBindCtx *pbcNew = NULL;
295 hr = CreateBindCtx(0, &pbcNew);
300 hr = IMoniker_Reduce(pmk, pbc, MKRREDUCE_ALL, NULL, pmkReduced);
302 ERR("reducing moniker failed with error 0x%08x\n", hr);
303 if (pbcNew) IBindCtx_Release(pbcNew);
307 /***********************************************************************
308 * RunningObjectTable_QueryInterface
310 static HRESULT WINAPI
311 RunningObjectTableImpl_QueryInterface(IRunningObjectTable* iface,
312 REFIID riid,void** ppvObject)
314 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
316 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
318 /* validate arguments */
325 if (IsEqualIID(&IID_IUnknown, riid) ||
326 IsEqualIID(&IID_IRunningObjectTable, riid))
327 *ppvObject = (IRunningObjectTable*)This;
330 return E_NOINTERFACE;
332 IRunningObjectTable_AddRef(iface);
337 /***********************************************************************
338 * RunningObjectTable_AddRef
341 RunningObjectTableImpl_AddRef(IRunningObjectTable* iface)
343 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
345 TRACE("(%p)\n",This);
347 return InterlockedIncrement(&This->ref);
350 /***********************************************************************
351 * RunningObjectTable_Initialize
353 static HRESULT WINAPI
354 RunningObjectTableImpl_Destroy(void)
356 struct list *cursor, *cursor2;
357 IrotHandle old_handle;
361 if (runningObjectTableInstance==NULL)
364 /* free the ROT table memory */
365 LIST_FOR_EACH_SAFE(cursor, cursor2, &runningObjectTableInstance->rot)
367 struct rot_entry *rot_entry = LIST_ENTRY(cursor, struct rot_entry, entry);
368 list_remove(&rot_entry->entry);
369 rot_entry_delete(rot_entry);
372 DEBUG_CLEAR_CRITSEC_NAME(&runningObjectTableInstance->lock);
373 DeleteCriticalSection(&runningObjectTableInstance->lock);
375 /* free the ROT structure memory */
376 HeapFree(GetProcessHeap(),0,runningObjectTableInstance);
377 runningObjectTableInstance = NULL;
379 old_handle = irot_handle;
382 RpcBindingFree(&old_handle);
387 /***********************************************************************
388 * RunningObjectTable_Release
391 RunningObjectTableImpl_Release(IRunningObjectTable* iface)
393 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
396 TRACE("(%p)\n",This);
398 ref = InterlockedDecrement(&This->ref);
400 /* uninitialize ROT structure if there's no more references to it */
403 struct list *cursor, *cursor2;
404 LIST_FOR_EACH_SAFE(cursor, cursor2, &This->rot)
406 struct rot_entry *rot_entry = LIST_ENTRY(cursor, struct rot_entry, entry);
407 list_remove(&rot_entry->entry);
408 rot_entry_delete(rot_entry);
410 /* RunningObjectTable data structure will be not destroyed here ! the destruction will be done only
411 * when RunningObjectTableImpl_UnInitialize function is called
418 /***********************************************************************
419 * RunningObjectTable_Register
422 * grfFlags [in] Registration options
423 * punkObject [in] the object being registered
424 * pmkObjectName [in] the moniker of the object being registered
425 * pdwRegister [in] the value identifying the registration
427 static HRESULT WINAPI
428 RunningObjectTableImpl_Register(IRunningObjectTable* iface, DWORD grfFlags,
429 IUnknown *punkObject, IMoniker *pmkObjectName, DWORD *pdwRegister)
431 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
432 struct rot_entry *rot_entry;
434 IStream *pStream = NULL;
437 InterfaceData *moniker = NULL;
439 TRACE("(%p,%d,%p,%p,%p)\n",This,grfFlags,punkObject,pmkObjectName,pdwRegister);
441 if (grfFlags & ~(ROTFLAGS_REGISTRATIONKEEPSALIVE|ROTFLAGS_ALLOWANYCLIENT))
443 ERR("Invalid grfFlags: 0x%08x\n", grfFlags & ~(ROTFLAGS_REGISTRATIONKEEPSALIVE|ROTFLAGS_ALLOWANYCLIENT));
447 if (punkObject==NULL || pmkObjectName==NULL || pdwRegister==NULL)
450 rot_entry = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*rot_entry));
452 return E_OUTOFMEMORY;
455 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
458 rot_entry_delete(rot_entry);
461 mshlflags = (grfFlags & ROTFLAGS_REGISTRATIONKEEPSALIVE) ? MSHLFLAGS_TABLESTRONG : MSHLFLAGS_TABLEWEAK;
462 hr = CoMarshalInterface(pStream, &IID_IUnknown, punkObject, MSHCTX_LOCAL | MSHCTX_NOSHAREDMEM, NULL, mshlflags);
463 /* FIXME: a cleaner way would be to create an IStream class that writes
464 * directly to an MInterfacePointer */
468 hr = GetHGlobalFromStream(pStream, &hglobal);
471 SIZE_T size = GlobalSize(hglobal);
472 const void *pv = GlobalLock(hglobal);
473 rot_entry->object = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(MInterfacePointer, abData[size]));
474 rot_entry->object->ulCntData = size;
475 memcpy(&rot_entry->object->abData, pv, size);
476 GlobalUnlock(hglobal);
479 IStream_Release(pStream);
482 rot_entry_delete(rot_entry);
486 hr = CreateBindCtx(0, &pbc);
489 rot_entry_delete(rot_entry);
493 hr = reduce_moniker(pmkObjectName, pbc, &pmkObjectName);
496 rot_entry_delete(rot_entry);
497 IBindCtx_Release(pbc);
501 hr = IMoniker_GetTimeOfLastChange(pmkObjectName, pbc, NULL,
502 &rot_entry->last_modified);
503 IBindCtx_Release(pbc);
506 CoFileTimeNow(&rot_entry->last_modified);
510 hr = get_moniker_comparison_data(pmkObjectName,
511 &rot_entry->moniker_data);
514 rot_entry_delete(rot_entry);
515 IMoniker_Release(pmkObjectName);
519 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
522 rot_entry_delete(rot_entry);
523 IMoniker_Release(pmkObjectName);
526 /* marshal moniker */
527 hr = CoMarshalInterface(pStream, &IID_IMoniker, (IUnknown *)pmkObjectName,
528 MSHCTX_LOCAL | MSHCTX_NOSHAREDMEM, NULL, MSHLFLAGS_TABLESTRONG);
529 /* FIXME: a cleaner way would be to create an IStream class that writes
530 * directly to an MInterfacePointer */
534 hr = GetHGlobalFromStream(pStream, &hglobal);
537 SIZE_T size = GlobalSize(hglobal);
538 const void *pv = GlobalLock(hglobal);
539 moniker = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(InterfaceData, abData[size]));
540 moniker->ulCntData = size;
541 memcpy(&moniker->abData, pv, size);
542 GlobalUnlock(hglobal);
545 IStream_Release(pStream);
546 IMoniker_Release(pmkObjectName);
549 HeapFree(GetProcessHeap(), 0, moniker);
550 rot_entry_delete(rot_entry);
559 hr = IrotRegister(get_irot_handle(), rot_entry->moniker_data,
560 rot_entry->object, moniker,
561 &rot_entry->last_modified, grfFlags,
562 &rot_entry->cookie, &rot_entry->ctxt_handle);
566 hr = HRESULT_FROM_WIN32(GetExceptionCode());
569 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
576 HeapFree(GetProcessHeap(), 0, moniker);
579 rot_entry_delete(rot_entry);
583 /* gives a registration identifier to the registered object*/
584 *pdwRegister = rot_entry->cookie;
586 EnterCriticalSection(&This->lock);
587 list_add_tail(&This->rot, &rot_entry->entry);
588 LeaveCriticalSection(&This->lock);
593 /***********************************************************************
594 * RunningObjectTable_Revoke
597 * dwRegister [in] Value identifying registration to be revoked
599 static HRESULT WINAPI
600 RunningObjectTableImpl_Revoke( IRunningObjectTable* iface, DWORD dwRegister)
602 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
603 struct rot_entry *rot_entry;
605 TRACE("(%p,%d)\n",This,dwRegister);
607 EnterCriticalSection(&This->lock);
608 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, struct rot_entry, entry)
610 if (rot_entry->cookie == dwRegister)
612 list_remove(&rot_entry->entry);
613 LeaveCriticalSection(&This->lock);
615 rot_entry_delete(rot_entry);
619 LeaveCriticalSection(&This->lock);
624 /***********************************************************************
625 * RunningObjectTable_IsRunning
628 * pmkObjectName [in] moniker of the object whose status is desired
630 static HRESULT WINAPI
631 RunningObjectTableImpl_IsRunning( IRunningObjectTable* iface, IMoniker *pmkObjectName)
633 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
634 MonikerComparisonData *moniker_data;
636 const struct rot_entry *rot_entry;
638 TRACE("(%p,%p)\n",This,pmkObjectName);
640 hr = reduce_moniker(pmkObjectName, NULL, &pmkObjectName);
643 hr = get_moniker_comparison_data(pmkObjectName, &moniker_data);
644 IMoniker_Release(pmkObjectName);
649 EnterCriticalSection(&This->lock);
650 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, const struct rot_entry, entry)
652 if ((rot_entry->moniker_data->ulCntData == moniker_data->ulCntData) &&
653 !memcmp(&moniker_data->abData, &rot_entry->moniker_data->abData, moniker_data->ulCntData))
659 LeaveCriticalSection(&This->lock);
667 hr = IrotIsRunning(get_irot_handle(), moniker_data);
671 hr = HRESULT_FROM_WIN32(GetExceptionCode());
674 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
683 HeapFree(GetProcessHeap(), 0, moniker_data);
688 /***********************************************************************
689 * RunningObjectTable_GetObject
692 * pmkObjectName [in] Pointer to the moniker on the object
693 * ppunkObject [out] variable that receives the IUnknown interface pointer
695 static HRESULT WINAPI
696 RunningObjectTableImpl_GetObject( IRunningObjectTable* iface,
697 IMoniker *pmkObjectName, IUnknown **ppunkObject)
699 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
700 MonikerComparisonData *moniker_data;
701 InterfaceData *object = NULL;
704 struct rot_entry *rot_entry;
706 TRACE("(%p,%p,%p)\n",This,pmkObjectName,ppunkObject);
708 if (ppunkObject == NULL)
713 hr = reduce_moniker(pmkObjectName, NULL, &pmkObjectName);
716 hr = get_moniker_comparison_data(pmkObjectName, &moniker_data);
717 IMoniker_Release(pmkObjectName);
721 EnterCriticalSection(&This->lock);
722 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, struct rot_entry, entry)
724 if ((rot_entry->moniker_data->ulCntData == moniker_data->ulCntData) &&
725 !memcmp(&moniker_data->abData, &rot_entry->moniker_data->abData, moniker_data->ulCntData))
728 hr = create_stream_on_mip_ro(rot_entry->object, &pStream);
731 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)ppunkObject);
732 IStream_Release(pStream);
735 LeaveCriticalSection(&This->lock);
736 HeapFree(GetProcessHeap(), 0, moniker_data);
741 LeaveCriticalSection(&This->lock);
743 TRACE("moniker unavailable locally, calling SCM\n");
749 hr = IrotGetObject(get_irot_handle(), moniker_data, &object, &cookie);
753 hr = HRESULT_FROM_WIN32(GetExceptionCode());
756 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
767 hr = create_stream_on_mip_ro(object, &pStream);
770 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)ppunkObject);
771 IStream_Release(pStream);
775 WARN("Moniker unavailable, IrotGetObject returned 0x%08x\n", hr);
777 HeapFree(GetProcessHeap(), 0, moniker_data);
782 /***********************************************************************
783 * RunningObjectTable_NoteChangeTime
786 * dwRegister [in] Value identifying registration being updated
787 * pfiletime [in] Pointer to structure containing object's last change time
789 static HRESULT WINAPI
790 RunningObjectTableImpl_NoteChangeTime(IRunningObjectTable* iface,
791 DWORD dwRegister, FILETIME *pfiletime)
793 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
794 struct rot_entry *rot_entry;
795 HRESULT hr = E_INVALIDARG;
797 TRACE("(%p,%d,%p)\n",This,dwRegister,pfiletime);
799 EnterCriticalSection(&This->lock);
800 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, struct rot_entry, entry)
802 if (rot_entry->cookie == dwRegister)
804 rot_entry->last_modified = *pfiletime;
805 LeaveCriticalSection(&This->lock);
811 hr = IrotNoteChangeTime(get_irot_handle(), dwRegister, pfiletime);
815 hr = HRESULT_FROM_WIN32(GetExceptionCode());
818 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
829 LeaveCriticalSection(&This->lock);
832 TRACE("-- 0x08%x\n", hr);
836 /***********************************************************************
837 * RunningObjectTable_GetTimeOfLastChange
840 * pmkObjectName [in] moniker of the object whose status is desired
841 * pfiletime [out] structure that receives object's last change time
843 static HRESULT WINAPI
844 RunningObjectTableImpl_GetTimeOfLastChange(IRunningObjectTable* iface,
845 IMoniker *pmkObjectName, FILETIME *pfiletime)
847 HRESULT hr = MK_E_UNAVAILABLE;
848 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
849 MonikerComparisonData *moniker_data;
850 const struct rot_entry *rot_entry;
852 TRACE("(%p,%p,%p)\n",This,pmkObjectName,pfiletime);
854 if (pmkObjectName==NULL || pfiletime==NULL)
857 hr = reduce_moniker(pmkObjectName, NULL, &pmkObjectName);
860 hr = get_moniker_comparison_data(pmkObjectName, &moniker_data);
861 IMoniker_Release(pmkObjectName);
865 hr = MK_E_UNAVAILABLE;
867 EnterCriticalSection(&This->lock);
868 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, const struct rot_entry, entry)
870 if ((rot_entry->moniker_data->ulCntData == moniker_data->ulCntData) &&
871 !memcmp(&moniker_data->abData, &rot_entry->moniker_data->abData, moniker_data->ulCntData))
873 *pfiletime = rot_entry->last_modified;
878 LeaveCriticalSection(&This->lock);
886 hr = IrotGetTimeOfLastChange(get_irot_handle(), moniker_data, pfiletime);
890 hr = HRESULT_FROM_WIN32(GetExceptionCode());
893 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
902 HeapFree(GetProcessHeap(), 0, moniker_data);
904 TRACE("-- 0x%08x\n", hr);
908 /***********************************************************************
909 * RunningObjectTable_EnumRunning
912 * ppenumMoniker [out] receives the IEnumMoniker interface pointer
914 static HRESULT WINAPI
915 RunningObjectTableImpl_EnumRunning(IRunningObjectTable* iface,
916 IEnumMoniker **ppenumMoniker)
918 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
919 InterfaceList *interface_list = NULL;
922 TRACE("(%p, %p)\n", This, ppenumMoniker);
924 *ppenumMoniker = NULL;
930 hr = IrotEnumRunning(get_irot_handle(), &interface_list);
934 hr = HRESULT_FROM_WIN32(GetExceptionCode());
937 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
946 hr = EnumMonikerImpl_CreateEnumROTMoniker(interface_list,
952 /* Virtual function table for the IRunningObjectTable class. */
953 static const IRunningObjectTableVtbl VT_RunningObjectTableImpl =
955 RunningObjectTableImpl_QueryInterface,
956 RunningObjectTableImpl_AddRef,
957 RunningObjectTableImpl_Release,
958 RunningObjectTableImpl_Register,
959 RunningObjectTableImpl_Revoke,
960 RunningObjectTableImpl_IsRunning,
961 RunningObjectTableImpl_GetObject,
962 RunningObjectTableImpl_NoteChangeTime,
963 RunningObjectTableImpl_GetTimeOfLastChange,
964 RunningObjectTableImpl_EnumRunning
967 /***********************************************************************
968 * RunningObjectTable_Initialize
970 HRESULT WINAPI RunningObjectTableImpl_Initialize(void)
974 /* create the unique instance of the RunningObjectTableImpl structure */
975 runningObjectTableInstance = HeapAlloc(GetProcessHeap(), 0, sizeof(RunningObjectTableImpl));
977 if (!runningObjectTableInstance)
978 return E_OUTOFMEMORY;
980 /* initialize the virtual table function */
981 runningObjectTableInstance->lpVtbl = &VT_RunningObjectTableImpl;
983 /* the initial reference is set to "1" ! because if set to "0" it will be not practis when */
984 /* the ROT referred many times not in the same time (all the objects in the ROT will */
985 /* be removed every time the ROT is removed ) */
986 runningObjectTableInstance->ref = 1;
988 list_init(&runningObjectTableInstance->rot);
989 InitializeCriticalSection(&runningObjectTableInstance->lock);
990 DEBUG_SET_CRITSEC_NAME(&runningObjectTableInstance->lock, "RunningObjectTableImpl.lock");
995 /***********************************************************************
996 * RunningObjectTable_UnInitialize
998 HRESULT WINAPI RunningObjectTableImpl_UnInitialize(void)
1002 if (runningObjectTableInstance==NULL)
1005 RunningObjectTableImpl_Release((IRunningObjectTable*)runningObjectTableInstance);
1007 RunningObjectTableImpl_Destroy();
1012 /***********************************************************************
1013 * GetRunningObjectTable (OLE32.@)
1015 * Retrieves the global running object table.
1018 * reserved [I] Reserved. Set to 0.
1019 * pprot [O] Address that receives the pointer to the running object table.
1023 * Failure: Any HRESULT code.
1026 GetRunningObjectTable(DWORD reserved, LPRUNNINGOBJECTTABLE *pprot)
1028 IID riid=IID_IRunningObjectTable;
1034 return E_UNEXPECTED;
1036 if(runningObjectTableInstance==NULL)
1037 return CO_E_NOTINITIALIZED;
1039 res = IRunningObjectTable_QueryInterface((IRunningObjectTable*)runningObjectTableInstance,&riid,(void**)pprot);
1044 static HRESULT get_moniker_for_progid_display_name(LPBC pbc,
1045 LPCOLESTR szDisplayName,
1052 LPCWSTR start = szDisplayName;
1055 IMoniker *class_moniker;
1060 /* find end delimiter */
1061 for (end = start; *end; end++)
1067 /* must start with '@' or have a ':' somewhere and mustn't be one character
1068 * long (since that looks like an absolute path) */
1069 if (((start == szDisplayName) && (*end == '\0')) || (len <= 1))
1072 progid = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1075 memcpy(progid, start, len * sizeof(WCHAR));
1078 hr = CLSIDFromProgID(progid, &clsid);
1079 HeapFree(GetProcessHeap(), 0, progid);
1083 hr = CreateClassMoniker(&clsid, &class_moniker);
1086 IParseDisplayName *pdn;
1087 hr = IMoniker_BindToObject(class_moniker, pbc, NULL,
1088 &IID_IParseDisplayName, (void **)&pdn);
1089 IMoniker_Release(class_moniker);
1092 hr = IParseDisplayName_ParseDisplayName(pdn, pbc,
1093 (LPOLESTR)szDisplayName,
1095 IParseDisplayName_Release(pdn);
1101 /******************************************************************************
1102 * MkParseDisplayName [OLE32.@]
1104 HRESULT WINAPI MkParseDisplayName(LPBC pbc, LPCOLESTR szDisplayName,
1105 LPDWORD pchEaten, LPMONIKER *ppmk)
1107 HRESULT hr = MK_E_SYNTAX;
1108 static const WCHAR wszClsidColon[] = {'c','l','s','i','d',':'};
1112 TRACE("(%p, %s, %p, %p)\n", pbc, debugstr_w(szDisplayName), pchEaten, ppmk);
1114 if (!(IsValidInterface((LPUNKNOWN) pbc)))
1115 return E_INVALIDARG;
1120 if (!strncmpiW(szDisplayName, wszClsidColon, sizeof(wszClsidColon)/sizeof(wszClsidColon[0])))
1122 hr = ClassMoniker_CreateFromDisplayName(pbc, szDisplayName, &chEaten, &moniker);
1123 if (FAILED(hr) && (hr != MK_E_SYNTAX))
1128 hr = get_moniker_for_progid_display_name(pbc, szDisplayName, &chEaten, &moniker);
1129 if (FAILED(hr) && (hr != MK_E_SYNTAX))
1135 hr = FileMoniker_CreateFromDisplayName(pbc, szDisplayName, &chEaten, &moniker);
1136 if (FAILED(hr) && (hr != MK_E_SYNTAX))
1144 IMoniker *next_moniker;
1145 *pchEaten += chEaten;
1146 szDisplayName += chEaten;
1147 if (!*szDisplayName)
1153 hr = IMoniker_ParseDisplayName(moniker, pbc, NULL,
1154 (LPOLESTR)szDisplayName, &chEaten,
1156 IMoniker_Release(moniker);
1162 moniker = next_moniker;
1169 /***********************************************************************
1170 * GetClassFile (OLE32.@)
1172 * Retrieves the class ID associated with the given filename.
1175 * filePathName [I] Filename to retrieve the class ID for.
1176 * pclsid [O] Address that receives the class ID for the file.
1180 * Failure: Any HRESULT code.
1182 HRESULT WINAPI GetClassFile(LPCOLESTR filePathName,CLSID *pclsid)
1186 int nbElm, length, i;
1188 LPOLESTR *pathDec=0,absFile=0,progId=0;
1190 static const WCHAR bkslashW[] = {'\\',0};
1191 static const WCHAR dotW[] = {'.',0};
1193 TRACE("%s, %p\n", debugstr_w(filePathName), pclsid);
1195 /* if the file contain a storage object the return the CLSID written by IStorage_SetClass method*/
1196 if((StgIsStorageFile(filePathName))==S_OK){
1198 res=StgOpenStorage(filePathName,NULL,STGM_READ | STGM_SHARE_DENY_WRITE,NULL,0,&pstg);
1201 res=ReadClassStg(pstg,pclsid);
1203 IStorage_Release(pstg);
1207 /* if the file is not a storage object then attemps to match various bits in the file against a
1208 pattern in the registry. this case is not frequently used ! so I present only the psodocode for
1211 for(i=0;i<nFileTypes;i++)
1213 for(i=0;j<nPatternsForType;j++){
1218 pat=ReadPatternFromRegistry(i,j);
1219 hFile=CreateFileW(filePathName,,,,,,hFile);
1220 SetFilePosition(hFile,pat.offset);
1221 ReadFile(hFile,buf,pat.size,&r,NULL);
1222 if (memcmp(buf&pat.mask,pat.pattern.pat.size)==0){
1224 *pclsid=ReadCLSIDFromRegistry(i);
1230 /* if the above strategies fail then search for the extension key in the registry */
1232 /* get the last element (absolute file) in the path name */
1233 nbElm=FileMonikerImpl_DecomposePath(filePathName,&pathDec);
1234 absFile=pathDec[nbElm-1];
1236 /* failed if the path represente a directory and not an absolute file name*/
1237 if (!lstrcmpW(absFile, bkslashW))
1238 return MK_E_INVALIDEXTENSION;
1240 /* get the extension of the file */
1242 length=lstrlenW(absFile);
1243 for(i = length-1; (i >= 0) && *(extension = &absFile[i]) != '.'; i--)
1246 if (!extension || !lstrcmpW(extension, dotW))
1247 return MK_E_INVALIDEXTENSION;
1249 res=RegQueryValueW(HKEY_CLASSES_ROOT, extension, NULL, &sizeProgId);
1251 /* get the progId associated to the extension */
1252 progId = CoTaskMemAlloc(sizeProgId);
1253 res = RegQueryValueW(HKEY_CLASSES_ROOT, extension, progId, &sizeProgId);
1255 if (res==ERROR_SUCCESS)
1256 /* return the clsid associated to the progId */
1257 res= CLSIDFromProgID(progId,pclsid);
1259 for(i=0; pathDec[i]!=NULL;i++)
1260 CoTaskMemFree(pathDec[i]);
1261 CoTaskMemFree(pathDec);
1263 CoTaskMemFree(progId);
1265 if (res==ERROR_SUCCESS)
1268 return MK_E_INVALIDEXTENSION;
1271 /***********************************************************************
1272 * EnumMoniker_QueryInterface
1274 static HRESULT WINAPI EnumMonikerImpl_QueryInterface(IEnumMoniker* iface,REFIID riid,void** ppvObject)
1276 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1278 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
1280 /* validate arguments */
1281 if (ppvObject == NULL)
1282 return E_INVALIDARG;
1286 if (IsEqualIID(&IID_IUnknown, riid))
1287 *ppvObject = (IEnumMoniker*)This;
1289 if (IsEqualIID(&IID_IEnumMoniker, riid))
1290 *ppvObject = (IEnumMoniker*)This;
1292 if ((*ppvObject)==NULL)
1293 return E_NOINTERFACE;
1295 IEnumMoniker_AddRef(iface);
1300 /***********************************************************************
1301 * EnumMoniker_AddRef
1303 static ULONG WINAPI EnumMonikerImpl_AddRef(IEnumMoniker* iface)
1305 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1307 TRACE("(%p)\n",This);
1309 return InterlockedIncrement(&This->ref);
1312 /***********************************************************************
1313 * EnumMoniker_release
1315 static ULONG WINAPI EnumMonikerImpl_Release(IEnumMoniker* iface)
1317 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1320 TRACE("(%p)\n",This);
1322 ref = InterlockedDecrement(&This->ref);
1324 /* uninitialize rot structure if there's no more reference to it*/
1329 TRACE("(%p) Deleting\n",This);
1331 for (i = 0; i < This->moniker_list->size; i++)
1332 HeapFree(GetProcessHeap(), 0, This->moniker_list->interfaces[i]);
1333 HeapFree(GetProcessHeap(), 0, This->moniker_list);
1334 HeapFree(GetProcessHeap(), 0, This);
1339 /***********************************************************************
1342 static HRESULT WINAPI EnumMonikerImpl_Next(IEnumMoniker* iface, ULONG celt, IMoniker** rgelt, ULONG * pceltFetched)
1345 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1348 TRACE("(%p) TabCurrentPos %d Tablastindx %d\n", This, This->pos, This->moniker_list->size);
1350 /* retrieve the requested number of moniker from the current position */
1351 for(i = 0; (This->pos < This->moniker_list->size) && (i < celt); i++)
1354 hr = create_stream_on_mip_ro(This->moniker_list->interfaces[This->pos++], &stream);
1355 if (hr != S_OK) break;
1356 hr = CoUnmarshalInterface(stream, &IID_IMoniker, (void **)&rgelt[i]);
1357 IStream_Release(stream);
1358 if (hr != S_OK) break;
1361 if (pceltFetched != NULL)
1374 /***********************************************************************
1377 static HRESULT WINAPI EnumMonikerImpl_Skip(IEnumMoniker* iface, ULONG celt)
1379 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1381 TRACE("(%p)\n",This);
1383 if (This->pos + celt >= This->moniker_list->size)
1391 /***********************************************************************
1392 * EnmumMoniker_Reset
1394 static HRESULT WINAPI EnumMonikerImpl_Reset(IEnumMoniker* iface)
1396 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1398 This->pos = 0; /* set back to start of list */
1400 TRACE("(%p)\n",This);
1405 /***********************************************************************
1406 * EnmumMoniker_Clone
1408 static HRESULT WINAPI EnumMonikerImpl_Clone(IEnumMoniker* iface, IEnumMoniker ** ppenum)
1410 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1411 InterfaceList *moniker_list;
1414 TRACE("(%p)\n",This);
1418 moniker_list = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(InterfaceList, interfaces[This->moniker_list->size]));
1420 return E_OUTOFMEMORY;
1422 moniker_list->size = This->moniker_list->size;
1423 for (i = 0; i < This->moniker_list->size; i++)
1425 SIZE_T size = FIELD_OFFSET(InterfaceData, abData[This->moniker_list->interfaces[i]->ulCntData]);
1426 moniker_list->interfaces[i] = HeapAlloc(GetProcessHeap(), 0, size);
1427 if (!moniker_list->interfaces[i])
1430 for (i = 0; i < end; i++)
1431 HeapFree(GetProcessHeap(), 0, moniker_list->interfaces[i]);
1432 HeapFree(GetProcessHeap(), 0, moniker_list);
1433 return E_OUTOFMEMORY;
1435 memcpy(moniker_list->interfaces[i], This->moniker_list->interfaces[i], size);
1438 /* copy the enum structure */
1439 return EnumMonikerImpl_CreateEnumROTMoniker(moniker_list, This->pos, ppenum);
1442 /* Virtual function table for the IEnumMoniker class. */
1443 static const IEnumMonikerVtbl VT_EnumMonikerImpl =
1445 EnumMonikerImpl_QueryInterface,
1446 EnumMonikerImpl_AddRef,
1447 EnumMonikerImpl_Release,
1448 EnumMonikerImpl_Next,
1449 EnumMonikerImpl_Skip,
1450 EnumMonikerImpl_Reset,
1451 EnumMonikerImpl_Clone
1454 /***********************************************************************
1455 * EnumMonikerImpl_CreateEnumROTMoniker
1456 * Used by EnumRunning to create the structure and EnumClone
1457 * to copy the structure
1459 static HRESULT WINAPI EnumMonikerImpl_CreateEnumROTMoniker(InterfaceList *moniker_list,
1461 IEnumMoniker **ppenumMoniker)
1463 EnumMonikerImpl* This = NULL;
1466 return E_INVALIDARG;
1468 This = HeapAlloc(GetProcessHeap(), 0, sizeof(EnumMonikerImpl));
1469 if (!This) return E_OUTOFMEMORY;
1471 TRACE("(%p)\n", This);
1473 /* initialize the virtual table function */
1474 This->lpVtbl = &VT_EnumMonikerImpl;
1476 /* the initial reference is set to "1" */
1477 This->ref = 1; /* set the ref count to one */
1478 This->pos = current_pos; /* Set the list start posn */
1479 This->moniker_list = moniker_list;
1481 *ppenumMoniker = (IEnumMoniker*)This;
1487 /* Shared implementation of moniker marshaler based on saving and loading of
1490 typedef struct MonikerMarshal
1492 const IUnknownVtbl *lpVtbl;
1493 const IMarshalVtbl *lpVtblMarshal;
1499 static inline MonikerMarshal *impl_from_IMarshal( IMarshal *iface )
1501 return (MonikerMarshal *)((char*)iface - FIELD_OFFSET(MonikerMarshal, lpVtblMarshal));
1504 static HRESULT WINAPI MonikerMarshalInner_QueryInterface(IUnknown *iface, REFIID riid, LPVOID *ppv)
1506 MonikerMarshal *This = (MonikerMarshal *)iface;
1507 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
1509 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IMarshal, riid))
1511 *ppv = &This->lpVtblMarshal;
1512 IUnknown_AddRef((IUnknown *)&This->lpVtblMarshal);
1515 FIXME("No interface for %s\n", debugstr_guid(riid));
1516 return E_NOINTERFACE;
1519 static ULONG WINAPI MonikerMarshalInner_AddRef(IUnknown *iface)
1521 MonikerMarshal *This = (MonikerMarshal *)iface;
1522 return InterlockedIncrement(&This->ref);
1525 static ULONG WINAPI MonikerMarshalInner_Release(IUnknown *iface)
1527 MonikerMarshal *This = (MonikerMarshal *)iface;
1528 ULONG ref = InterlockedDecrement(&This->ref);
1530 if (!ref) HeapFree(GetProcessHeap(), 0, This);
1534 static const IUnknownVtbl VT_MonikerMarshalInner =
1536 MonikerMarshalInner_QueryInterface,
1537 MonikerMarshalInner_AddRef,
1538 MonikerMarshalInner_Release
1541 static HRESULT WINAPI MonikerMarshal_QueryInterface(IMarshal *iface, REFIID riid, LPVOID *ppv)
1543 MonikerMarshal *This = impl_from_IMarshal(iface);
1544 return IMoniker_QueryInterface(This->moniker, riid, ppv);
1547 static ULONG WINAPI MonikerMarshal_AddRef(IMarshal *iface)
1549 MonikerMarshal *This = impl_from_IMarshal(iface);
1550 return IMoniker_AddRef(This->moniker);
1553 static ULONG WINAPI MonikerMarshal_Release(IMarshal *iface)
1555 MonikerMarshal *This = impl_from_IMarshal(iface);
1556 return IMoniker_Release(This->moniker);
1559 static HRESULT WINAPI MonikerMarshal_GetUnmarshalClass(
1560 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
1561 void* pvDestContext, DWORD mshlflags, CLSID* pCid)
1563 MonikerMarshal *This = impl_from_IMarshal(iface);
1565 TRACE("(%s, %p, %x, %p, %x, %p)\n", debugstr_guid(riid), pv,
1566 dwDestContext, pvDestContext, mshlflags, pCid);
1568 return IMoniker_GetClassID(This->moniker, pCid);
1571 static HRESULT WINAPI MonikerMarshal_GetMarshalSizeMax(
1572 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
1573 void* pvDestContext, DWORD mshlflags, DWORD* pSize)
1575 MonikerMarshal *This = impl_from_IMarshal(iface);
1577 ULARGE_INTEGER size;
1579 TRACE("(%s, %p, %x, %p, %x, %p)\n", debugstr_guid(riid), pv,
1580 dwDestContext, pvDestContext, mshlflags, pSize);
1582 hr = IMoniker_GetSizeMax(This->moniker, &size);
1584 *pSize = (DWORD)size.QuadPart;
1588 static HRESULT WINAPI MonikerMarshal_MarshalInterface(LPMARSHAL iface, IStream *pStm,
1589 REFIID riid, void* pv, DWORD dwDestContext,
1590 void* pvDestContext, DWORD mshlflags)
1592 MonikerMarshal *This = impl_from_IMarshal(iface);
1594 TRACE("(%p, %s, %p, %x, %p, %x)\n", pStm, debugstr_guid(riid), pv,
1595 dwDestContext, pvDestContext, mshlflags);
1597 return IMoniker_Save(This->moniker, pStm, FALSE);
1600 static HRESULT WINAPI MonikerMarshal_UnmarshalInterface(LPMARSHAL iface, IStream *pStm, REFIID riid, void **ppv)
1602 MonikerMarshal *This = impl_from_IMarshal(iface);
1605 TRACE("(%p, %s, %p)\n", pStm, debugstr_guid(riid), ppv);
1607 hr = IMoniker_Load(This->moniker, pStm);
1609 hr = IMoniker_QueryInterface(This->moniker, riid, ppv);
1613 static HRESULT WINAPI MonikerMarshal_ReleaseMarshalData(LPMARSHAL iface, IStream *pStm)
1616 /* can't release a state-based marshal as nothing on server side to
1621 static HRESULT WINAPI MonikerMarshal_DisconnectObject(LPMARSHAL iface, DWORD dwReserved)
1624 /* can't disconnect a state-based marshal as nothing on server side to
1625 * disconnect from */
1629 static const IMarshalVtbl VT_MonikerMarshal =
1631 MonikerMarshal_QueryInterface,
1632 MonikerMarshal_AddRef,
1633 MonikerMarshal_Release,
1634 MonikerMarshal_GetUnmarshalClass,
1635 MonikerMarshal_GetMarshalSizeMax,
1636 MonikerMarshal_MarshalInterface,
1637 MonikerMarshal_UnmarshalInterface,
1638 MonikerMarshal_ReleaseMarshalData,
1639 MonikerMarshal_DisconnectObject
1642 HRESULT MonikerMarshal_Create(IMoniker *inner, IUnknown **outer)
1644 MonikerMarshal *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1645 if (!This) return E_OUTOFMEMORY;
1647 This->lpVtbl = &VT_MonikerMarshalInner;
1648 This->lpVtblMarshal = &VT_MonikerMarshal;
1650 This->moniker = inner;
1652 *outer = (IUnknown *)&This->lpVtbl;
1656 void * __RPC_USER MIDL_user_allocate(size_t size)
1658 return HeapAlloc(GetProcessHeap(), 0, size);
1661 void __RPC_USER MIDL_user_free(void *p)
1663 HeapFree(GetProcessHeap(), 0, p);