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.
42 #include "wine/list.h"
43 #include "wine/debug.h"
44 #include "wine/unicode.h"
46 #include "compobj_private.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(ole);
51 /* see MSDN docs for IROTData::GetComparisonData, which states what this
52 * constant is (http://msdn2.microsoft.com/en-us/library/ms693773.aspx) */
53 #define MAX_COMPARISON_DATA 2048
55 /* define the structure of the running object table elements */
59 MInterfacePointer* object; /* marshaled running object*/
60 MInterfacePointer* moniker; /* marshaled moniker that identifies this object */
61 MInterfacePointer* moniker_data; /* moniker comparison data that identifies this object */
62 DWORD cookie; /* cookie identifying this object */
63 FILETIME last_modified;
66 /* define the RunningObjectTableImpl structure */
67 typedef struct RunningObjectTableImpl
69 const IRunningObjectTableVtbl *lpVtbl;
72 struct list rot; /* list of ROT entries */
73 CRITICAL_SECTION lock;
74 } RunningObjectTableImpl;
76 static RunningObjectTableImpl* runningObjectTableInstance = NULL;
80 static inline HRESULT WINAPI
81 IrotRegister(DWORD *cookie)
83 static LONG last_cookie = 1;
84 *cookie = InterlockedIncrement(&last_cookie);
88 /* define the EnumMonikerImpl structure */
89 typedef struct EnumMonikerImpl
91 const IEnumMonikerVtbl *lpVtbl;
94 MInterfacePointer **monikers;
100 /* IEnumMoniker Local functions*/
101 static HRESULT WINAPI EnumMonikerImpl_CreateEnumROTMoniker(MInterfacePointer **monikers,
102 ULONG moniker_count, ULONG pos, IEnumMoniker **ppenumMoniker);
104 static HRESULT create_stream_on_mip_ro(const MInterfacePointer *mip, IStream **stream)
106 HGLOBAL hglobal = GlobalAlloc(0, mip->ulCntData);
107 void *pv = GlobalLock(hglobal);
108 memcpy(pv, mip->abData, mip->ulCntData);
109 GlobalUnlock(hglobal);
110 return CreateStreamOnHGlobal(hglobal, TRUE, stream);
113 static inline void rot_entry_delete(struct rot_entry *rot_entry)
115 /* FIXME: revoke entry from rpcss's copy of the ROT */
116 if (rot_entry->object)
120 hr = create_stream_on_mip_ro(rot_entry->object, &stream);
123 CoReleaseMarshalData(stream);
124 IUnknown_Release(stream);
127 if (rot_entry->moniker)
131 hr = create_stream_on_mip_ro(rot_entry->moniker, &stream);
134 CoReleaseMarshalData(stream);
135 IUnknown_Release(stream);
138 HeapFree(GetProcessHeap(), 0, rot_entry->object);
139 HeapFree(GetProcessHeap(), 0, rot_entry->moniker);
140 HeapFree(GetProcessHeap(), 0, rot_entry->moniker_data);
141 HeapFree(GetProcessHeap(), 0, rot_entry);
144 /* moniker_data must be freed with HeapFree when no longer in use */
145 static HRESULT get_moniker_comparison_data(IMoniker *pMoniker, MInterfacePointer **moniker_data)
148 IROTData *pROTData = NULL;
149 hr = IMoniker_QueryInterface(pMoniker, &IID_IROTData, (void *)&pROTData);
152 ULONG size = MAX_COMPARISON_DATA;
153 *moniker_data = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(MInterfacePointer, abData[size]));
154 hr = IROTData_GetComparisonData(pROTData, (*moniker_data)->abData, size, &size);
157 ERR("Failed to copy comparison data into buffer, hr = 0x%08x\n", hr);
158 HeapFree(GetProcessHeap(), 0, *moniker_data);
161 (*moniker_data)->ulCntData = size;
166 LPOLESTR pszDisplayName;
170 TRACE("generating comparison data from display name\n");
172 hr = CreateBindCtx(0, &pbc);
175 hr = IMoniker_GetDisplayName(pMoniker, pbc, NULL, &pszDisplayName);
176 IBindCtx_Release(pbc);
179 hr = IMoniker_GetClassID(pMoniker, &clsid);
182 CoTaskMemFree(pszDisplayName);
186 len = strlenW(pszDisplayName);
187 *moniker_data = HeapAlloc(GetProcessHeap(), 0,
188 FIELD_OFFSET(MInterfacePointer, abData[sizeof(CLSID) + (len+1)*sizeof(WCHAR)]));
191 CoTaskMemFree(pszDisplayName);
192 return E_OUTOFMEMORY;
194 (*moniker_data)->ulCntData = sizeof(CLSID) + (len+1)*sizeof(WCHAR);
196 memcpy(&(*moniker_data)->abData[0], &clsid, sizeof(clsid));
197 memcpy(&(*moniker_data)->abData[sizeof(clsid)], pszDisplayName, (len+1)*sizeof(WCHAR));
202 static HRESULT reduce_moniker(IMoniker *pmk, IBindCtx *pbc, IMoniker **pmkReduced)
204 IBindCtx *pbcNew = NULL;
208 hr = CreateBindCtx(0, &pbcNew);
213 hr = IMoniker_Reduce(pmk, pbc, MKRREDUCE_ALL, NULL, pmkReduced);
215 ERR("reducing moniker failed with error 0x%08x\n", hr);
216 if (pbcNew) IBindCtx_Release(pbcNew);
220 /***********************************************************************
221 * RunningObjectTable_QueryInterface
223 static HRESULT WINAPI
224 RunningObjectTableImpl_QueryInterface(IRunningObjectTable* iface,
225 REFIID riid,void** ppvObject)
227 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
229 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
231 /* validate arguments */
238 if (IsEqualIID(&IID_IUnknown, riid) ||
239 IsEqualIID(&IID_IRunningObjectTable, riid))
240 *ppvObject = (IRunningObjectTable*)This;
243 return E_NOINTERFACE;
245 IRunningObjectTable_AddRef(iface);
250 /***********************************************************************
251 * RunningObjectTable_AddRef
254 RunningObjectTableImpl_AddRef(IRunningObjectTable* iface)
256 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
258 TRACE("(%p)\n",This);
260 return InterlockedIncrement(&This->ref);
263 /***********************************************************************
264 * RunningObjectTable_Initialize
266 static HRESULT WINAPI
267 RunningObjectTableImpl_Destroy(void)
269 struct list *cursor, *cursor2;
273 if (runningObjectTableInstance==NULL)
276 /* free the ROT table memory */
277 LIST_FOR_EACH_SAFE(cursor, cursor2, &runningObjectTableInstance->rot)
279 struct rot_entry *rot_entry = LIST_ENTRY(cursor, struct rot_entry, entry);
280 list_remove(&rot_entry->entry);
281 rot_entry_delete(rot_entry);
284 DEBUG_CLEAR_CRITSEC_NAME(&runningObjectTableInstance->lock);
285 DeleteCriticalSection(&runningObjectTableInstance->lock);
287 /* free the ROT structure memory */
288 HeapFree(GetProcessHeap(),0,runningObjectTableInstance);
289 runningObjectTableInstance = NULL;
294 /***********************************************************************
295 * RunningObjectTable_Release
298 RunningObjectTableImpl_Release(IRunningObjectTable* iface)
300 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
303 TRACE("(%p)\n",This);
305 ref = InterlockedDecrement(&This->ref);
307 /* uninitialize ROT structure if there's no more references to it */
310 struct list *cursor, *cursor2;
311 LIST_FOR_EACH_SAFE(cursor, cursor2, &This->rot)
313 struct rot_entry *rot_entry = LIST_ENTRY(cursor, struct rot_entry, entry);
314 list_remove(&rot_entry->entry);
315 rot_entry_delete(rot_entry);
317 /* RunningObjectTable data structure will be not destroyed here ! the destruction will be done only
318 * when RunningObjectTableImpl_UnInitialize function is called
325 /***********************************************************************
326 * RunningObjectTable_Register
329 * grfFlags [in] Registration options
330 * punkObject [in] the object being registered
331 * pmkObjectName [in] the moniker of the object being registered
332 * pdwRegister [in] the value identifying the registration
334 static HRESULT WINAPI
335 RunningObjectTableImpl_Register(IRunningObjectTable* iface, DWORD grfFlags,
336 IUnknown *punkObject, IMoniker *pmkObjectName, DWORD *pdwRegister)
338 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
339 struct rot_entry *rot_entry;
341 IStream *pStream = NULL;
345 TRACE("(%p,%d,%p,%p,%p)\n",This,grfFlags,punkObject,pmkObjectName,pdwRegister);
347 if (grfFlags & ~(ROTFLAGS_REGISTRATIONKEEPSALIVE|ROTFLAGS_ALLOWANYCLIENT))
349 ERR("Invalid grfFlags: 0x%08x\n", grfFlags & ~(ROTFLAGS_REGISTRATIONKEEPSALIVE|ROTFLAGS_ALLOWANYCLIENT));
353 if (punkObject==NULL || pmkObjectName==NULL || pdwRegister==NULL)
356 rot_entry = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*rot_entry));
358 return E_OUTOFMEMORY;
361 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
364 rot_entry_delete(rot_entry);
367 mshlflags = (grfFlags & ROTFLAGS_REGISTRATIONKEEPSALIVE) ? MSHLFLAGS_TABLESTRONG : MSHLFLAGS_TABLEWEAK;
368 hr = CoMarshalInterface(pStream, &IID_IUnknown, punkObject, MSHCTX_LOCAL | MSHCTX_NOSHAREDMEM, NULL, mshlflags);
369 /* FIXME: a cleaner way would be to create an IStream class that writes
370 * directly to an MInterfacePointer */
374 hr = GetHGlobalFromStream(pStream, &hglobal);
377 SIZE_T size = GlobalSize(hglobal);
378 const void *pv = GlobalLock(hglobal);
379 rot_entry->object = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(MInterfacePointer, abData[size]));
380 rot_entry->object->ulCntData = size;
381 memcpy(&rot_entry->object->abData, pv, size);
382 GlobalUnlock(hglobal);
385 IStream_Release(pStream);
388 rot_entry_delete(rot_entry);
392 hr = CreateBindCtx(0, &pbc);
395 rot_entry_delete(rot_entry);
399 hr = reduce_moniker(pmkObjectName, pbc, &pmkObjectName);
402 rot_entry_delete(rot_entry);
403 IBindCtx_Release(pbc);
407 hr = IMoniker_GetTimeOfLastChange(pmkObjectName, pbc, NULL,
408 &rot_entry->last_modified);
409 IBindCtx_Release(pbc);
412 CoFileTimeNow(&rot_entry->last_modified);
416 hr = get_moniker_comparison_data(pmkObjectName,
417 &rot_entry->moniker_data);
420 rot_entry_delete(rot_entry);
421 IMoniker_Release(pmkObjectName);
425 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
428 rot_entry_delete(rot_entry);
429 IMoniker_Release(pmkObjectName);
432 /* marshal moniker */
433 hr = CoMarshalInterface(pStream, &IID_IMoniker, (IUnknown *)pmkObjectName,
434 MSHCTX_LOCAL | MSHCTX_NOSHAREDMEM, NULL, MSHLFLAGS_TABLESTRONG);
435 /* FIXME: a cleaner way would be to create an IStream class that writes
436 * directly to an MInterfacePointer */
440 hr = GetHGlobalFromStream(pStream, &hglobal);
443 SIZE_T size = GlobalSize(hglobal);
444 const void *pv = GlobalLock(hglobal);
445 rot_entry->moniker = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(MInterfacePointer, abData[size]));
446 rot_entry->moniker->ulCntData = size;
447 memcpy(&rot_entry->moniker->abData, pv, size);
448 GlobalUnlock(hglobal);
451 IStream_Release(pStream);
452 IMoniker_Release(pmkObjectName);
455 rot_entry_delete(rot_entry);
459 /* FIXME: not the right signature of IrotRegister function */
460 hr = IrotRegister(&rot_entry->cookie);
463 rot_entry_delete(rot_entry);
467 /* gives a registration identifier to the registered object*/
468 *pdwRegister = rot_entry->cookie;
470 EnterCriticalSection(&This->lock);
471 /* FIXME: see if object was registered before and return MK_S_MONIKERALREADYREGISTERED */
472 list_add_tail(&This->rot, &rot_entry->entry);
473 LeaveCriticalSection(&This->lock);
478 /***********************************************************************
479 * RunningObjectTable_Revoke
482 * dwRegister [in] Value identifying registration to be revoked
484 static HRESULT WINAPI
485 RunningObjectTableImpl_Revoke( IRunningObjectTable* iface, DWORD dwRegister)
487 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
488 struct rot_entry *rot_entry;
490 TRACE("(%p,%d)\n",This,dwRegister);
492 EnterCriticalSection(&This->lock);
493 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, struct rot_entry, entry)
495 if (rot_entry->cookie == dwRegister)
497 list_remove(&rot_entry->entry);
498 LeaveCriticalSection(&This->lock);
500 rot_entry_delete(rot_entry);
504 LeaveCriticalSection(&This->lock);
509 /***********************************************************************
510 * RunningObjectTable_IsRunning
513 * pmkObjectName [in] moniker of the object whose status is desired
515 static HRESULT WINAPI
516 RunningObjectTableImpl_IsRunning( IRunningObjectTable* iface, IMoniker *pmkObjectName)
518 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
519 MInterfacePointer *moniker_data;
521 struct rot_entry *rot_entry;
523 TRACE("(%p,%p)\n",This,pmkObjectName);
525 hr = reduce_moniker(pmkObjectName, NULL, &pmkObjectName);
528 hr = get_moniker_comparison_data(pmkObjectName, &moniker_data);
529 IMoniker_Release(pmkObjectName);
534 EnterCriticalSection(&This->lock);
535 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, struct rot_entry, entry)
537 if ((rot_entry->moniker_data->ulCntData == moniker_data->ulCntData) &&
538 !memcmp(&moniker_data->abData, &rot_entry->moniker_data->abData, moniker_data->ulCntData))
544 LeaveCriticalSection(&This->lock);
546 /* FIXME: call IrotIsRunning */
548 HeapFree(GetProcessHeap(), 0, moniker_data);
553 /***********************************************************************
554 * RunningObjectTable_GetObject
557 * pmkObjectName [in] Pointer to the moniker on the object
558 * ppunkObject [out] variable that receives the IUnknown interface pointer
560 static HRESULT WINAPI
561 RunningObjectTableImpl_GetObject( IRunningObjectTable* iface,
562 IMoniker *pmkObjectName, IUnknown **ppunkObject)
564 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
565 MInterfacePointer *moniker_data;
567 struct rot_entry *rot_entry;
569 TRACE("(%p,%p,%p)\n",This,pmkObjectName,ppunkObject);
571 if (ppunkObject == NULL)
576 hr = reduce_moniker(pmkObjectName, NULL, &pmkObjectName);
579 hr = get_moniker_comparison_data(pmkObjectName, &moniker_data);
580 IMoniker_Release(pmkObjectName);
584 EnterCriticalSection(&This->lock);
585 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, struct rot_entry, entry)
587 if ((rot_entry->moniker_data->ulCntData == moniker_data->ulCntData) &&
588 !memcmp(&moniker_data->abData, &rot_entry->moniker_data->abData, moniker_data->ulCntData))
591 hr = create_stream_on_mip_ro(rot_entry->object, &pStream);
594 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)ppunkObject);
595 IStream_Release(pStream);
598 LeaveCriticalSection(&This->lock);
599 HeapFree(GetProcessHeap(), 0, moniker_data);
604 LeaveCriticalSection(&This->lock);
606 /* FIXME: call IrotGetObject */
607 WARN("Moniker unavailable - app may require interprocess running object table\n");
608 hr = MK_E_UNAVAILABLE;
610 HeapFree(GetProcessHeap(), 0, moniker_data);
615 /***********************************************************************
616 * RunningObjectTable_NoteChangeTime
619 * dwRegister [in] Value identifying registration being updated
620 * pfiletime [in] Pointer to structure containing object's last change time
622 static HRESULT WINAPI
623 RunningObjectTableImpl_NoteChangeTime(IRunningObjectTable* iface,
624 DWORD dwRegister, FILETIME *pfiletime)
626 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
627 struct rot_entry *rot_entry;
629 TRACE("(%p,%d,%p)\n",This,dwRegister,pfiletime);
631 EnterCriticalSection(&This->lock);
632 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, struct rot_entry, entry)
634 if (rot_entry->cookie == dwRegister)
636 rot_entry->last_modified = *pfiletime;
637 LeaveCriticalSection(&This->lock);
641 LeaveCriticalSection(&This->lock);
643 /* FIXME: call IrotNoteChangeTime */
648 /***********************************************************************
649 * RunningObjectTable_GetTimeOfLastChange
652 * pmkObjectName [in] moniker of the object whose status is desired
653 * pfiletime [out] structure that receives object's last change time
655 static HRESULT WINAPI
656 RunningObjectTableImpl_GetTimeOfLastChange(IRunningObjectTable* iface,
657 IMoniker *pmkObjectName, FILETIME *pfiletime)
659 HRESULT hr = MK_E_UNAVAILABLE;
660 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
661 MInterfacePointer *moniker_data;
662 struct rot_entry *rot_entry;
664 TRACE("(%p,%p,%p)\n",This,pmkObjectName,pfiletime);
666 if (pmkObjectName==NULL || pfiletime==NULL)
669 hr = reduce_moniker(pmkObjectName, NULL, &pmkObjectName);
672 hr = get_moniker_comparison_data(pmkObjectName, &moniker_data);
673 IMoniker_Release(pmkObjectName);
677 hr = MK_E_UNAVAILABLE;
679 EnterCriticalSection(&This->lock);
680 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, struct rot_entry, entry)
682 if ((rot_entry->moniker_data->ulCntData == moniker_data->ulCntData) &&
683 !memcmp(&moniker_data->abData, &rot_entry->moniker_data->abData, moniker_data->ulCntData))
685 *pfiletime = rot_entry->last_modified;
690 LeaveCriticalSection(&This->lock);
692 /* FIXME: if (hr != S_OK) call IrotGetTimeOfLastChange */
694 HeapFree(GetProcessHeap(), 0, moniker_data);
698 /***********************************************************************
699 * RunningObjectTable_EnumRunning
702 * ppenumMoniker [out] receives the IEnumMoniker interface pointer
704 static HRESULT WINAPI
705 RunningObjectTableImpl_EnumRunning(IRunningObjectTable* iface,
706 IEnumMoniker **ppenumMoniker)
709 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
710 MInterfacePointer **monikers;
711 ULONG moniker_count = 0;
712 const struct rot_entry *rot_entry;
715 EnterCriticalSection(&This->lock);
717 LIST_FOR_EACH_ENTRY( rot_entry, &This->rot, const struct rot_entry, entry )
720 monikers = HeapAlloc(GetProcessHeap(), 0, moniker_count * sizeof(*monikers));
722 LIST_FOR_EACH_ENTRY( rot_entry, &This->rot, const struct rot_entry, entry )
724 SIZE_T size = FIELD_OFFSET(MInterfacePointer, abData[rot_entry->moniker->ulCntData]);
725 monikers[i] = HeapAlloc(GetProcessHeap(), 0, size);
726 memcpy(monikers[i], rot_entry->moniker, size);
730 LeaveCriticalSection(&This->lock);
732 /* FIXME: call IrotEnumRunning and append data */
734 hr = EnumMonikerImpl_CreateEnumROTMoniker(monikers, moniker_count, 0, ppenumMoniker);
739 /* Virtual function table for the IRunningObjectTable class. */
740 static const IRunningObjectTableVtbl VT_RunningObjectTableImpl =
742 RunningObjectTableImpl_QueryInterface,
743 RunningObjectTableImpl_AddRef,
744 RunningObjectTableImpl_Release,
745 RunningObjectTableImpl_Register,
746 RunningObjectTableImpl_Revoke,
747 RunningObjectTableImpl_IsRunning,
748 RunningObjectTableImpl_GetObject,
749 RunningObjectTableImpl_NoteChangeTime,
750 RunningObjectTableImpl_GetTimeOfLastChange,
751 RunningObjectTableImpl_EnumRunning
754 /***********************************************************************
755 * RunningObjectTable_Initialize
757 HRESULT WINAPI RunningObjectTableImpl_Initialize(void)
761 /* create the unique instance of the RunningObjectTableImpl structure */
762 runningObjectTableInstance = HeapAlloc(GetProcessHeap(), 0, sizeof(RunningObjectTableImpl));
764 if (!runningObjectTableInstance)
765 return E_OUTOFMEMORY;
767 /* initialize the virtual table function */
768 runningObjectTableInstance->lpVtbl = &VT_RunningObjectTableImpl;
770 /* the initial reference is set to "1" ! because if set to "0" it will be not practis when */
771 /* the ROT referred many times not in the same time (all the objects in the ROT will */
772 /* be removed every time the ROT is removed ) */
773 runningObjectTableInstance->ref = 1;
775 list_init(&runningObjectTableInstance->rot);
776 InitializeCriticalSection(&runningObjectTableInstance->lock);
777 DEBUG_SET_CRITSEC_NAME(&runningObjectTableInstance->lock, "RunningObjectTableImpl.lock");
782 /***********************************************************************
783 * RunningObjectTable_UnInitialize
785 HRESULT WINAPI RunningObjectTableImpl_UnInitialize(void)
789 if (runningObjectTableInstance==NULL)
792 RunningObjectTableImpl_Release((IRunningObjectTable*)runningObjectTableInstance);
794 RunningObjectTableImpl_Destroy();
799 /***********************************************************************
800 * GetRunningObjectTable (OLE32.@)
802 * Retrieves the global running object table.
805 * reserved [I] Reserved. Set to 0.
806 * pprot [O] Address that receives the pointer to the running object table.
810 * Failure: Any HRESULT code.
813 GetRunningObjectTable(DWORD reserved, LPRUNNINGOBJECTTABLE *pprot)
815 IID riid=IID_IRunningObjectTable;
823 if(runningObjectTableInstance==NULL)
824 return CO_E_NOTINITIALIZED;
826 res = IRunningObjectTable_QueryInterface((IRunningObjectTable*)runningObjectTableInstance,&riid,(void**)pprot);
831 static HRESULT get_moniker_for_progid_display_name(LPBC pbc,
832 LPCOLESTR szDisplayName,
839 LPCWSTR start = szDisplayName;
842 IMoniker *class_moniker;
847 /* find end delimiter */
848 for (end = start; *end; end++)
854 /* must start with '@' or have a ':' somewhere and mustn't be one character
855 * long (since that looks like an absolute path) */
856 if (((start == szDisplayName) && (*end == '\0')) || (len <= 1))
859 progid = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
862 memcpy(progid, start, len * sizeof(WCHAR));
865 hr = CLSIDFromProgID(progid, &clsid);
866 HeapFree(GetProcessHeap(), 0, progid);
870 hr = CreateClassMoniker(&clsid, &class_moniker);
873 IParseDisplayName *pdn;
874 hr = IMoniker_BindToObject(class_moniker, pbc, NULL,
875 &IID_IParseDisplayName, (void **)&pdn);
876 IMoniker_Release(class_moniker);
879 hr = IParseDisplayName_ParseDisplayName(pdn, pbc,
880 (LPOLESTR)szDisplayName,
882 IParseDisplayName_Release(pdn);
888 /******************************************************************************
889 * MkParseDisplayName [OLE32.@]
891 HRESULT WINAPI MkParseDisplayName(LPBC pbc, LPCOLESTR szDisplayName,
892 LPDWORD pchEaten, LPMONIKER *ppmk)
894 HRESULT hr = MK_E_SYNTAX;
895 static const WCHAR wszClsidColon[] = {'c','l','s','i','d',':'};
899 TRACE("(%p, %s, %p, %p)\n", pbc, debugstr_w(szDisplayName), pchEaten, ppmk);
901 if (!(IsValidInterface((LPUNKNOWN) pbc)))
907 if (!strncmpiW(szDisplayName, wszClsidColon, sizeof(wszClsidColon)/sizeof(wszClsidColon[0])))
909 hr = ClassMoniker_CreateFromDisplayName(pbc, szDisplayName, &chEaten, &moniker);
910 if (FAILED(hr) && (hr != MK_E_SYNTAX))
915 hr = get_moniker_for_progid_display_name(pbc, szDisplayName, &chEaten, &moniker);
916 if (FAILED(hr) && (hr != MK_E_SYNTAX))
922 hr = FileMoniker_CreateFromDisplayName(pbc, szDisplayName, &chEaten, &moniker);
923 if (FAILED(hr) && (hr != MK_E_SYNTAX))
931 IMoniker *next_moniker;
932 *pchEaten += chEaten;
933 szDisplayName += chEaten;
940 hr = IMoniker_ParseDisplayName(moniker, pbc, NULL,
941 (LPOLESTR)szDisplayName, &chEaten,
943 IMoniker_Release(moniker);
949 moniker = next_moniker;
956 /***********************************************************************
957 * GetClassFile (OLE32.@)
959 * Retrieves the class ID associated with the given filename.
962 * filePathName [I] Filename to retrieve the class ID for.
963 * pclsid [O] Address that receives the class ID for the file.
967 * Failure: Any HRESULT code.
969 HRESULT WINAPI GetClassFile(LPCOLESTR filePathName,CLSID *pclsid)
973 int nbElm, length, i;
975 LPOLESTR *pathDec=0,absFile=0,progId=0;
977 static const WCHAR bkslashW[] = {'\\',0};
978 static const WCHAR dotW[] = {'.',0};
980 TRACE("%s, %p\n", debugstr_w(filePathName), pclsid);
982 /* if the file contain a storage object the return the CLSID written by IStorage_SetClass method*/
983 if((StgIsStorageFile(filePathName))==S_OK){
985 res=StgOpenStorage(filePathName,NULL,STGM_READ | STGM_SHARE_DENY_WRITE,NULL,0,&pstg);
988 res=ReadClassStg(pstg,pclsid);
990 IStorage_Release(pstg);
994 /* if the file is not a storage object then attemps to match various bits in the file against a
995 pattern in the registry. this case is not frequently used ! so I present only the psodocode for
998 for(i=0;i<nFileTypes;i++)
1000 for(i=0;j<nPatternsForType;j++){
1005 pat=ReadPatternFromRegistry(i,j);
1006 hFile=CreateFileW(filePathName,,,,,,hFile);
1007 SetFilePosition(hFile,pat.offset);
1008 ReadFile(hFile,buf,pat.size,&r,NULL);
1009 if (memcmp(buf&pat.mask,pat.pattern.pat.size)==0){
1011 *pclsid=ReadCLSIDFromRegistry(i);
1017 /* if the above strategies fail then search for the extension key in the registry */
1019 /* get the last element (absolute file) in the path name */
1020 nbElm=FileMonikerImpl_DecomposePath(filePathName,&pathDec);
1021 absFile=pathDec[nbElm-1];
1023 /* failed if the path represente a directory and not an absolute file name*/
1024 if (!lstrcmpW(absFile, bkslashW))
1025 return MK_E_INVALIDEXTENSION;
1027 /* get the extension of the file */
1029 length=lstrlenW(absFile);
1030 for(i = length-1; (i >= 0) && *(extension = &absFile[i]) != '.'; i--)
1033 if (!extension || !lstrcmpW(extension, dotW))
1034 return MK_E_INVALIDEXTENSION;
1036 res=RegQueryValueW(HKEY_CLASSES_ROOT, extension, NULL, &sizeProgId);
1038 /* get the progId associated to the extension */
1039 progId = CoTaskMemAlloc(sizeProgId);
1040 res = RegQueryValueW(HKEY_CLASSES_ROOT, extension, progId, &sizeProgId);
1042 if (res==ERROR_SUCCESS)
1043 /* return the clsid associated to the progId */
1044 res= CLSIDFromProgID(progId,pclsid);
1046 for(i=0; pathDec[i]!=NULL;i++)
1047 CoTaskMemFree(pathDec[i]);
1048 CoTaskMemFree(pathDec);
1050 CoTaskMemFree(progId);
1052 if (res==ERROR_SUCCESS)
1055 return MK_E_INVALIDEXTENSION;
1058 /***********************************************************************
1059 * EnumMoniker_QueryInterface
1061 static HRESULT WINAPI EnumMonikerImpl_QueryInterface(IEnumMoniker* iface,REFIID riid,void** ppvObject)
1063 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1065 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
1067 /* validate arguments */
1068 if (ppvObject == NULL)
1069 return E_INVALIDARG;
1073 if (IsEqualIID(&IID_IUnknown, riid))
1074 *ppvObject = (IEnumMoniker*)This;
1076 if (IsEqualIID(&IID_IEnumMoniker, riid))
1077 *ppvObject = (IEnumMoniker*)This;
1079 if ((*ppvObject)==NULL)
1080 return E_NOINTERFACE;
1082 IEnumMoniker_AddRef(iface);
1087 /***********************************************************************
1088 * EnumMoniker_AddRef
1090 static ULONG WINAPI EnumMonikerImpl_AddRef(IEnumMoniker* iface)
1092 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1094 TRACE("(%p)\n",This);
1096 return InterlockedIncrement(&This->ref);
1099 /***********************************************************************
1100 * EnumMoniker_release
1102 static ULONG WINAPI EnumMonikerImpl_Release(IEnumMoniker* iface)
1104 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1107 TRACE("(%p)\n",This);
1109 ref = InterlockedDecrement(&This->ref);
1111 /* uninitialize rot structure if there's no more reference to it*/
1116 TRACE("(%p) Deleting\n",This);
1118 for (i = 0; i < This->moniker_count; i++)
1119 HeapFree(GetProcessHeap(), 0, This->monikers[i]);
1120 HeapFree(GetProcessHeap(), 0, This->monikers);
1121 HeapFree(GetProcessHeap(), 0, This);
1126 /***********************************************************************
1129 static HRESULT WINAPI EnumMonikerImpl_Next(IEnumMoniker* iface, ULONG celt, IMoniker** rgelt, ULONG * pceltFetched)
1132 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1135 TRACE("(%p) TabCurrentPos %d Tablastindx %d\n", This, This->pos, This->moniker_count);
1137 /* retrieve the requested number of moniker from the current position */
1138 for(i = 0; (This->pos < This->moniker_count) && (i < celt); i++)
1141 hr = create_stream_on_mip_ro(This->monikers[This->pos++], &stream);
1142 if (hr != S_OK) break;
1143 hr = CoUnmarshalInterface(stream, &IID_IMoniker, (void **)&rgelt[i]);
1144 IStream_Release(stream);
1145 if (hr != S_OK) break;
1148 if (pceltFetched != NULL)
1161 /***********************************************************************
1164 static HRESULT WINAPI EnumMonikerImpl_Skip(IEnumMoniker* iface, ULONG celt)
1166 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1168 TRACE("(%p)\n",This);
1170 if (This->pos + celt >= This->moniker_count)
1178 /***********************************************************************
1179 * EnmumMoniker_Reset
1181 static HRESULT WINAPI EnumMonikerImpl_Reset(IEnumMoniker* iface)
1183 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1185 This->pos = 0; /* set back to start of list */
1187 TRACE("(%p)\n",This);
1192 /***********************************************************************
1193 * EnmumMoniker_Clone
1195 static HRESULT WINAPI EnumMonikerImpl_Clone(IEnumMoniker* iface, IEnumMoniker ** ppenum)
1197 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1198 MInterfacePointer **monikers = HeapAlloc(GetProcessHeap(), 0, sizeof(*monikers)*This->moniker_count);
1201 TRACE("(%p)\n",This);
1203 for (i = 0; i < This->moniker_count; i++)
1205 SIZE_T size = FIELD_OFFSET(MInterfacePointer, abData[This->monikers[i]->ulCntData]);
1206 monikers[i] = HeapAlloc(GetProcessHeap(), 0, size);
1207 memcpy(monikers[i], This->monikers[i], size);
1210 /* copy the enum structure */
1211 return EnumMonikerImpl_CreateEnumROTMoniker(monikers, This->moniker_count,
1215 /* Virtual function table for the IEnumMoniker class. */
1216 static const IEnumMonikerVtbl VT_EnumMonikerImpl =
1218 EnumMonikerImpl_QueryInterface,
1219 EnumMonikerImpl_AddRef,
1220 EnumMonikerImpl_Release,
1221 EnumMonikerImpl_Next,
1222 EnumMonikerImpl_Skip,
1223 EnumMonikerImpl_Reset,
1224 EnumMonikerImpl_Clone
1227 /***********************************************************************
1228 * EnumMonikerImpl_CreateEnumROTMoniker
1229 * Used by EnumRunning to create the structure and EnumClone
1230 * to copy the structure
1232 static HRESULT WINAPI EnumMonikerImpl_CreateEnumROTMoniker(MInterfacePointer **monikers,
1233 ULONG moniker_count,
1235 IEnumMoniker **ppenumMoniker)
1237 EnumMonikerImpl* This = NULL;
1240 return E_INVALIDARG;
1242 This = HeapAlloc(GetProcessHeap(), 0, sizeof(EnumMonikerImpl));
1243 if (!This) return E_OUTOFMEMORY;
1245 TRACE("(%p)\n", This);
1247 /* initialize the virtual table function */
1248 This->lpVtbl = &VT_EnumMonikerImpl;
1250 /* the initial reference is set to "1" */
1251 This->ref = 1; /* set the ref count to one */
1252 This->pos = current_pos; /* Set the list start posn */
1253 This->moniker_count = moniker_count; /* Need the same size table as ROT */
1254 This->monikers = monikers;
1256 *ppenumMoniker = (IEnumMoniker*)This;
1262 /* Shared implementation of moniker marshaler based on saving and loading of
1265 typedef struct MonikerMarshal
1267 const IUnknownVtbl *lpVtbl;
1268 const IMarshalVtbl *lpVtblMarshal;
1274 static inline MonikerMarshal *impl_from_IMarshal( IMarshal *iface )
1276 return (MonikerMarshal *)((char*)iface - FIELD_OFFSET(MonikerMarshal, lpVtblMarshal));
1279 static HRESULT WINAPI MonikerMarshalInner_QueryInterface(IUnknown *iface, REFIID riid, LPVOID *ppv)
1281 MonikerMarshal *This = (MonikerMarshal *)iface;
1282 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
1284 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IMarshal, riid))
1286 *ppv = &This->lpVtblMarshal;
1287 IUnknown_AddRef((IUnknown *)&This->lpVtblMarshal);
1290 FIXME("No interface for %s\n", debugstr_guid(riid));
1291 return E_NOINTERFACE;
1294 static ULONG WINAPI MonikerMarshalInner_AddRef(IUnknown *iface)
1296 MonikerMarshal *This = (MonikerMarshal *)iface;
1297 return InterlockedIncrement(&This->ref);
1300 static ULONG WINAPI MonikerMarshalInner_Release(IUnknown *iface)
1302 MonikerMarshal *This = (MonikerMarshal *)iface;
1303 ULONG ref = InterlockedDecrement(&This->ref);
1305 if (!ref) HeapFree(GetProcessHeap(), 0, This);
1309 static const IUnknownVtbl VT_MonikerMarshalInner =
1311 MonikerMarshalInner_QueryInterface,
1312 MonikerMarshalInner_AddRef,
1313 MonikerMarshalInner_Release
1316 static HRESULT WINAPI MonikerMarshal_QueryInterface(IMarshal *iface, REFIID riid, LPVOID *ppv)
1318 MonikerMarshal *This = impl_from_IMarshal(iface);
1319 return IMoniker_QueryInterface(This->moniker, riid, ppv);
1322 static ULONG WINAPI MonikerMarshal_AddRef(IMarshal *iface)
1324 MonikerMarshal *This = impl_from_IMarshal(iface);
1325 return IMoniker_AddRef(This->moniker);
1328 static ULONG WINAPI MonikerMarshal_Release(IMarshal *iface)
1330 MonikerMarshal *This = impl_from_IMarshal(iface);
1331 return IMoniker_Release(This->moniker);
1334 static HRESULT WINAPI MonikerMarshal_GetUnmarshalClass(
1335 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
1336 void* pvDestContext, DWORD mshlflags, CLSID* pCid)
1338 MonikerMarshal *This = impl_from_IMarshal(iface);
1340 TRACE("(%s, %p, %x, %p, %x, %p)\n", debugstr_guid(riid), pv,
1341 dwDestContext, pvDestContext, mshlflags, pCid);
1343 return IMoniker_GetClassID(This->moniker, pCid);
1346 static HRESULT WINAPI MonikerMarshal_GetMarshalSizeMax(
1347 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
1348 void* pvDestContext, DWORD mshlflags, DWORD* pSize)
1350 MonikerMarshal *This = impl_from_IMarshal(iface);
1352 ULARGE_INTEGER size;
1354 TRACE("(%s, %p, %x, %p, %x, %p)\n", debugstr_guid(riid), pv,
1355 dwDestContext, pvDestContext, mshlflags, pSize);
1357 hr = IMoniker_GetSizeMax(This->moniker, &size);
1359 *pSize = (DWORD)size.QuadPart;
1363 static HRESULT WINAPI MonikerMarshal_MarshalInterface(LPMARSHAL iface, IStream *pStm,
1364 REFIID riid, void* pv, DWORD dwDestContext,
1365 void* pvDestContext, DWORD mshlflags)
1367 MonikerMarshal *This = impl_from_IMarshal(iface);
1369 TRACE("(%p, %s, %p, %x, %p, %x)\n", pStm, debugstr_guid(riid), pv,
1370 dwDestContext, pvDestContext, mshlflags);
1372 return IMoniker_Save(This->moniker, pStm, FALSE);
1375 static HRESULT WINAPI MonikerMarshal_UnmarshalInterface(LPMARSHAL iface, IStream *pStm, REFIID riid, void **ppv)
1377 MonikerMarshal *This = impl_from_IMarshal(iface);
1380 TRACE("(%p, %s, %p)\n", pStm, debugstr_guid(riid), ppv);
1382 hr = IMoniker_Load(This->moniker, pStm);
1384 hr = IMoniker_QueryInterface(This->moniker, riid, ppv);
1388 static HRESULT WINAPI MonikerMarshal_ReleaseMarshalData(LPMARSHAL iface, IStream *pStm)
1391 /* can't release a state-based marshal as nothing on server side to
1396 static HRESULT WINAPI MonikerMarshal_DisconnectObject(LPMARSHAL iface, DWORD dwReserved)
1399 /* can't disconnect a state-based marshal as nothing on server side to
1400 * disconnect from */
1404 static const IMarshalVtbl VT_MonikerMarshal =
1406 MonikerMarshal_QueryInterface,
1407 MonikerMarshal_AddRef,
1408 MonikerMarshal_Release,
1409 MonikerMarshal_GetUnmarshalClass,
1410 MonikerMarshal_GetMarshalSizeMax,
1411 MonikerMarshal_MarshalInterface,
1412 MonikerMarshal_UnmarshalInterface,
1413 MonikerMarshal_ReleaseMarshalData,
1414 MonikerMarshal_DisconnectObject
1417 HRESULT MonikerMarshal_Create(IMoniker *inner, IUnknown **outer)
1419 MonikerMarshal *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1420 if (!This) return E_OUTOFMEMORY;
1422 This->lpVtbl = &VT_MonikerMarshalInner;
1423 This->lpVtblMarshal = &VT_MonikerMarshal;
1425 This->moniker = inner;
1427 *outer = (IUnknown *)&This->lpVtbl;