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
29 #include "wine/list.h"
30 #include "wine/debug.h"
32 #include "compobj_private.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(ole
);
39 BOOL WINAPI
InternalIsProcessInitialized(void);
40 HRESULT WINAPI
InternalIrotRegister(const MonikerComparisonData
*moniker_data
,
41 const InterfaceData
*object
, const InterfaceData
*moniker
,
42 const FILETIME
*time
, DWORD flags
, IrotCookie
*cookie
, IrotContextHandle
*ctxt_handle
);
43 HRESULT WINAPI
InternalIrotIsRunning(const MonikerComparisonData
*moniker_data
);
44 HRESULT WINAPI
InternalIrotGetObject(const MonikerComparisonData
*moniker_data
, PInterfaceData
*obj
,
46 HRESULT WINAPI
InternalIrotNoteChangeTime(IrotCookie cookie
, const FILETIME
*time
);
47 HRESULT WINAPI
InternalIrotGetTimeOfLastChange(const MonikerComparisonData
*moniker_data
, FILETIME
*time
);
48 HRESULT WINAPI
InternalIrotEnumRunning(PInterfaceList
*list
);
49 HRESULT WINAPI
InternalIrotRevoke(IrotCookie cookie
, IrotContextHandle
*ctxt_handle
, PInterfaceData
*object
,
50 PInterfaceData
*moniker
);
52 /* define the structure of the running object table elements */
56 InterfaceData
* object
; /* marshaled running object*/
57 MonikerComparisonData
* moniker_data
; /* moniker comparison data that identifies this object */
58 DWORD cookie
; /* cookie identifying this object */
59 FILETIME last_modified
;
60 IrotContextHandle ctxt_handle
;
63 /* define the RunningObjectTableImpl structure */
64 typedef struct RunningObjectTableImpl
66 IRunningObjectTable IRunningObjectTable_iface
;
67 struct list rot
; /* list of ROT entries */
68 CRITICAL_SECTION lock
;
69 } RunningObjectTableImpl
;
71 /* define the EnumMonikerImpl structure */
72 typedef struct EnumMonikerImpl
74 IEnumMoniker IEnumMoniker_iface
;
77 InterfaceList
*moniker_list
;
81 static inline RunningObjectTableImpl
*impl_from_IRunningObjectTable(IRunningObjectTable
*iface
)
83 return CONTAINING_RECORD(iface
, RunningObjectTableImpl
, IRunningObjectTable_iface
);
86 static inline EnumMonikerImpl
*impl_from_IEnumMoniker(IEnumMoniker
*iface
)
88 return CONTAINING_RECORD(iface
, EnumMonikerImpl
, IEnumMoniker_iface
);
91 /* IEnumMoniker Local functions*/
92 static HRESULT
EnumMonikerImpl_CreateEnumROTMoniker(InterfaceList
*moniker_list
,
93 ULONG pos
, IEnumMoniker
**ppenumMoniker
);
95 static HRESULT
create_stream_on_mip_ro(const InterfaceData
*mip
, IStream
**stream
)
97 HGLOBAL hglobal
= GlobalAlloc(0, mip
->ulCntData
);
98 void *pv
= GlobalLock(hglobal
);
99 memcpy(pv
, mip
->abData
, mip
->ulCntData
);
100 GlobalUnlock(hglobal
);
101 return CreateStreamOnHGlobal(hglobal
, TRUE
, stream
);
104 static void rot_entry_delete(struct rot_entry
*rot_entry
)
106 if (rot_entry
->cookie
)
108 InterfaceData
*object
= NULL
;
109 InterfaceData
*moniker
= NULL
;
111 InternalIrotRevoke(rot_entry
->cookie
, &rot_entry
->ctxt_handle
, &object
, &moniker
);
112 MIDL_user_free(object
);
117 hr
= create_stream_on_mip_ro(moniker
, &stream
);
120 CoReleaseMarshalData(stream
);
121 IStream_Release(stream
);
124 MIDL_user_free(moniker
);
126 if (rot_entry
->object
)
130 hr
= create_stream_on_mip_ro(rot_entry
->object
, &stream
);
133 CoReleaseMarshalData(stream
);
134 IStream_Release(stream
);
137 HeapFree(GetProcessHeap(), 0, rot_entry
->object
);
138 HeapFree(GetProcessHeap(), 0, rot_entry
->moniker_data
);
139 HeapFree(GetProcessHeap(), 0, rot_entry
);
142 /* moniker_data must be freed with HeapFree when no longer in use */
143 static HRESULT
get_moniker_comparison_data(IMoniker
*pMoniker
, MonikerComparisonData
**moniker_data
)
146 IROTData
*pROTData
= NULL
;
147 hr
= IMoniker_QueryInterface(pMoniker
, &IID_IROTData
, (void *)&pROTData
);
150 ULONG size
= ROT_COMPARE_MAX
;
151 *moniker_data
= HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(MonikerComparisonData
, abData
[size
]));
154 IROTData_Release(pROTData
);
155 return E_OUTOFMEMORY
;
157 hr
= IROTData_GetComparisonData(pROTData
, (*moniker_data
)->abData
, size
, &size
);
158 IROTData_Release(pROTData
);
161 ERR("Failed to copy comparison data into buffer, hr = 0x%08x\n", hr
);
162 HeapFree(GetProcessHeap(), 0, *moniker_data
);
165 (*moniker_data
)->ulCntData
= size
;
170 LPOLESTR pszDisplayName
;
174 TRACE("generating comparison data from display name\n");
176 hr
= CreateBindCtx(0, &pbc
);
179 hr
= IMoniker_GetDisplayName(pMoniker
, pbc
, NULL
, &pszDisplayName
);
180 IBindCtx_Release(pbc
);
183 hr
= IMoniker_GetClassID(pMoniker
, &clsid
);
186 CoTaskMemFree(pszDisplayName
);
190 len
= lstrlenW(pszDisplayName
);
191 *moniker_data
= HeapAlloc(GetProcessHeap(), 0,
192 FIELD_OFFSET(MonikerComparisonData
, abData
[sizeof(CLSID
) + (len
+1)*sizeof(WCHAR
)]));
195 CoTaskMemFree(pszDisplayName
);
196 return E_OUTOFMEMORY
;
198 (*moniker_data
)->ulCntData
= sizeof(CLSID
) + (len
+1)*sizeof(WCHAR
);
200 memcpy(&(*moniker_data
)->abData
[0], &clsid
, sizeof(clsid
));
201 memcpy(&(*moniker_data
)->abData
[sizeof(clsid
)], pszDisplayName
, (len
+1)*sizeof(WCHAR
));
202 CoTaskMemFree(pszDisplayName
);
207 static HRESULT
reduce_moniker(IMoniker
*pmk
, IBindCtx
*pbc
, IMoniker
**pmkReduced
)
209 IBindCtx
*pbcNew
= NULL
;
213 hr
= CreateBindCtx(0, &pbcNew
);
218 hr
= IMoniker_Reduce(pmk
, pbc
, MKRREDUCE_ALL
, NULL
, pmkReduced
);
220 ERR("reducing moniker failed with error 0x%08x\n", hr
);
221 if (pbcNew
) IBindCtx_Release(pbcNew
);
225 /***********************************************************************
226 * RunningObjectTable_QueryInterface
228 static HRESULT WINAPI
229 RunningObjectTableImpl_QueryInterface(IRunningObjectTable
* iface
,
230 REFIID riid
,void** ppvObject
)
232 RunningObjectTableImpl
*This
= impl_from_IRunningObjectTable(iface
);
234 TRACE("(%p,%s,%p)\n",This
,debugstr_guid(riid
),ppvObject
);
236 /* validate arguments */
243 if (IsEqualIID(&IID_IUnknown
, riid
) ||
244 IsEqualIID(&IID_IRunningObjectTable
, riid
))
245 *ppvObject
= &This
->IRunningObjectTable_iface
;
248 return E_NOINTERFACE
;
250 IRunningObjectTable_AddRef(iface
);
255 static ULONG WINAPI
RunningObjectTableImpl_AddRef(IRunningObjectTable
*iface
)
257 TRACE("%p\n", iface
);
262 static ULONG WINAPI
RunningObjectTableImpl_Release(IRunningObjectTable
*iface
)
264 TRACE("%p\n", iface
);
269 /***********************************************************************
270 * RunningObjectTable_Register
273 * grfFlags [in] Registration options
274 * punkObject [in] the object being registered
275 * pmkObjectName [in] the moniker of the object being registered
276 * pdwRegister [out] the value identifying the registration
278 static HRESULT WINAPI
279 RunningObjectTableImpl_Register(IRunningObjectTable
* iface
, DWORD flags
,
280 IUnknown
*punkObject
, IMoniker
*pmkObjectName
, DWORD
*pdwRegister
)
282 RunningObjectTableImpl
*This
= impl_from_IRunningObjectTable(iface
);
283 struct rot_entry
*rot_entry
;
285 IStream
*pStream
= NULL
;
288 InterfaceData
*moniker
= NULL
;
290 TRACE("%p, %#x, %p, %p, %p\n", This
, flags
, punkObject
, pmkObjectName
, pdwRegister
);
292 if (flags
& ~(ROTFLAGS_REGISTRATIONKEEPSALIVE
|ROTFLAGS_ALLOWANYCLIENT
))
294 ERR("Invalid flags: 0x%08x\n", flags
& ~(ROTFLAGS_REGISTRATIONKEEPSALIVE
|ROTFLAGS_ALLOWANYCLIENT
));
298 if (punkObject
==NULL
|| pmkObjectName
==NULL
|| pdwRegister
==NULL
)
301 rot_entry
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*rot_entry
));
303 return E_OUTOFMEMORY
;
306 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
309 rot_entry_delete(rot_entry
);
312 mshlflags
= flags
& ROTFLAGS_REGISTRATIONKEEPSALIVE
? MSHLFLAGS_TABLESTRONG
: MSHLFLAGS_TABLEWEAK
;
313 hr
= CoMarshalInterface(pStream
, &IID_IUnknown
, punkObject
, MSHCTX_LOCAL
| MSHCTX_NOSHAREDMEM
, NULL
, mshlflags
);
314 /* FIXME: a cleaner way would be to create an IStream class that writes
315 * directly to an MInterfacePointer */
319 hr
= GetHGlobalFromStream(pStream
, &hglobal
);
322 SIZE_T size
= GlobalSize(hglobal
);
323 const void *pv
= GlobalLock(hglobal
);
324 rot_entry
->object
= HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(MInterfacePointer
, abData
[size
]));
325 rot_entry
->object
->ulCntData
= size
;
326 memcpy(rot_entry
->object
->abData
, pv
, size
);
327 GlobalUnlock(hglobal
);
330 IStream_Release(pStream
);
333 rot_entry_delete(rot_entry
);
337 hr
= CreateBindCtx(0, &pbc
);
340 rot_entry_delete(rot_entry
);
344 hr
= reduce_moniker(pmkObjectName
, pbc
, &pmkObjectName
);
347 rot_entry_delete(rot_entry
);
348 IBindCtx_Release(pbc
);
352 hr
= IMoniker_GetTimeOfLastChange(pmkObjectName
, pbc
, NULL
,
353 &rot_entry
->last_modified
);
354 IBindCtx_Release(pbc
);
357 CoFileTimeNow(&rot_entry
->last_modified
);
361 hr
= get_moniker_comparison_data(pmkObjectName
,
362 &rot_entry
->moniker_data
);
365 rot_entry_delete(rot_entry
);
366 IMoniker_Release(pmkObjectName
);
370 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
373 rot_entry_delete(rot_entry
);
374 IMoniker_Release(pmkObjectName
);
377 /* marshal moniker */
378 hr
= CoMarshalInterface(pStream
, &IID_IMoniker
, (IUnknown
*)pmkObjectName
,
379 MSHCTX_LOCAL
| MSHCTX_NOSHAREDMEM
, NULL
, MSHLFLAGS_TABLESTRONG
);
380 /* FIXME: a cleaner way would be to create an IStream class that writes
381 * directly to an MInterfacePointer */
385 hr
= GetHGlobalFromStream(pStream
, &hglobal
);
388 SIZE_T size
= GlobalSize(hglobal
);
389 const void *pv
= GlobalLock(hglobal
);
390 moniker
= HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(InterfaceData
, abData
[size
]));
391 moniker
->ulCntData
= size
;
392 memcpy(moniker
->abData
, pv
, size
);
393 GlobalUnlock(hglobal
);
396 IStream_Release(pStream
);
397 IMoniker_Release(pmkObjectName
);
400 HeapFree(GetProcessHeap(), 0, moniker
);
401 rot_entry_delete(rot_entry
);
405 hr
= InternalIrotRegister(rot_entry
->moniker_data
, rot_entry
->object
, moniker
,
406 &rot_entry
->last_modified
, flags
, &rot_entry
->cookie
, &rot_entry
->ctxt_handle
);
408 HeapFree(GetProcessHeap(), 0, moniker
);
411 rot_entry_delete(rot_entry
);
415 /* gives a registration identifier to the registered object*/
416 *pdwRegister
= rot_entry
->cookie
;
418 EnterCriticalSection(&This
->lock
);
419 list_add_tail(&This
->rot
, &rot_entry
->entry
);
420 LeaveCriticalSection(&This
->lock
);
425 /***********************************************************************
426 * RunningObjectTable_Revoke
429 * dwRegister [in] Value identifying registration to be revoked
431 static HRESULT WINAPI
432 RunningObjectTableImpl_Revoke( IRunningObjectTable
* iface
, DWORD dwRegister
)
434 RunningObjectTableImpl
*This
= impl_from_IRunningObjectTable(iface
);
435 struct rot_entry
*rot_entry
;
437 TRACE("(%p,%d)\n",This
,dwRegister
);
439 EnterCriticalSection(&This
->lock
);
440 LIST_FOR_EACH_ENTRY(rot_entry
, &This
->rot
, struct rot_entry
, entry
)
442 if (rot_entry
->cookie
== dwRegister
)
444 list_remove(&rot_entry
->entry
);
445 LeaveCriticalSection(&This
->lock
);
447 rot_entry_delete(rot_entry
);
451 LeaveCriticalSection(&This
->lock
);
456 /***********************************************************************
457 * RunningObjectTable_IsRunning
460 * pmkObjectName [in] moniker of the object whose status is desired
462 static HRESULT WINAPI
463 RunningObjectTableImpl_IsRunning( IRunningObjectTable
* iface
, IMoniker
*pmkObjectName
)
465 RunningObjectTableImpl
*This
= impl_from_IRunningObjectTable(iface
);
466 MonikerComparisonData
*moniker_data
;
468 const struct rot_entry
*rot_entry
;
470 TRACE("(%p,%p)\n",This
,pmkObjectName
);
472 hr
= reduce_moniker(pmkObjectName
, NULL
, &pmkObjectName
);
475 hr
= get_moniker_comparison_data(pmkObjectName
, &moniker_data
);
476 IMoniker_Release(pmkObjectName
);
481 EnterCriticalSection(&This
->lock
);
482 LIST_FOR_EACH_ENTRY(rot_entry
, &This
->rot
, const struct rot_entry
, entry
)
484 if ((rot_entry
->moniker_data
->ulCntData
== moniker_data
->ulCntData
) &&
485 !memcmp(moniker_data
->abData
, rot_entry
->moniker_data
->abData
, moniker_data
->ulCntData
))
491 LeaveCriticalSection(&This
->lock
);
494 hr
= InternalIrotIsRunning(moniker_data
);
496 HeapFree(GetProcessHeap(), 0, moniker_data
);
501 /***********************************************************************
502 * RunningObjectTable_GetObject
505 * pmkObjectName [in] Pointer to the moniker on the object
506 * ppunkObject [out] variable that receives the IUnknown interface pointer
508 static HRESULT WINAPI
509 RunningObjectTableImpl_GetObject( IRunningObjectTable
* iface
,
510 IMoniker
*pmkObjectName
, IUnknown
**ppunkObject
)
512 RunningObjectTableImpl
*This
= impl_from_IRunningObjectTable(iface
);
513 MonikerComparisonData
*moniker_data
;
514 InterfaceData
*object
= NULL
;
517 struct rot_entry
*rot_entry
;
519 TRACE("(%p,%p,%p)\n",This
,pmkObjectName
,ppunkObject
);
521 if (ppunkObject
== NULL
)
526 hr
= reduce_moniker(pmkObjectName
, NULL
, &pmkObjectName
);
529 hr
= get_moniker_comparison_data(pmkObjectName
, &moniker_data
);
530 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
))
541 hr
= create_stream_on_mip_ro(rot_entry
->object
, &pStream
);
544 hr
= CoUnmarshalInterface(pStream
, &IID_IUnknown
, (void **)ppunkObject
);
545 IStream_Release(pStream
);
548 LeaveCriticalSection(&This
->lock
);
549 HeapFree(GetProcessHeap(), 0, moniker_data
);
554 LeaveCriticalSection(&This
->lock
);
556 TRACE("moniker unavailable locally, calling SCM\n");
558 hr
= InternalIrotGetObject(moniker_data
, &object
, &cookie
);
562 hr
= create_stream_on_mip_ro(object
, &pStream
);
565 hr
= CoUnmarshalInterface(pStream
, &IID_IUnknown
, (void **)ppunkObject
);
566 IStream_Release(pStream
);
570 WARN("Moniker unavailable, IrotGetObject returned 0x%08x\n", hr
);
572 HeapFree(GetProcessHeap(), 0, moniker_data
);
577 /***********************************************************************
578 * RunningObjectTable_NoteChangeTime
581 * dwRegister [in] Value identifying registration being updated
582 * pfiletime [in] Pointer to structure containing object's last change time
584 static HRESULT WINAPI
585 RunningObjectTableImpl_NoteChangeTime(IRunningObjectTable
* iface
,
586 DWORD dwRegister
, FILETIME
*pfiletime
)
588 RunningObjectTableImpl
*This
= impl_from_IRunningObjectTable(iface
);
589 struct rot_entry
*rot_entry
;
590 HRESULT hr
= E_INVALIDARG
;
592 TRACE("(%p,%d,%p)\n",This
,dwRegister
,pfiletime
);
594 EnterCriticalSection(&This
->lock
);
595 LIST_FOR_EACH_ENTRY(rot_entry
, &This
->rot
, struct rot_entry
, entry
)
597 if (rot_entry
->cookie
== dwRegister
)
599 rot_entry
->last_modified
= *pfiletime
;
600 LeaveCriticalSection(&This
->lock
);
602 hr
= InternalIrotNoteChangeTime(dwRegister
, pfiletime
);
607 LeaveCriticalSection(&This
->lock
);
610 TRACE("-- 0x08%x\n", hr
);
614 /***********************************************************************
615 * RunningObjectTable_GetTimeOfLastChange
618 * pmkObjectName [in] moniker of the object whose status is desired
619 * pfiletime [out] structure that receives object's last change time
621 static HRESULT WINAPI
622 RunningObjectTableImpl_GetTimeOfLastChange(IRunningObjectTable
* iface
,
623 IMoniker
*pmkObjectName
, FILETIME
*pfiletime
)
625 HRESULT hr
= MK_E_UNAVAILABLE
;
626 RunningObjectTableImpl
*This
= impl_from_IRunningObjectTable(iface
);
627 MonikerComparisonData
*moniker_data
;
628 const struct rot_entry
*rot_entry
;
630 TRACE("(%p,%p,%p)\n",This
,pmkObjectName
,pfiletime
);
632 if (pmkObjectName
==NULL
|| pfiletime
==NULL
)
635 hr
= reduce_moniker(pmkObjectName
, NULL
, &pmkObjectName
);
638 hr
= get_moniker_comparison_data(pmkObjectName
, &moniker_data
);
639 IMoniker_Release(pmkObjectName
);
643 hr
= MK_E_UNAVAILABLE
;
645 EnterCriticalSection(&This
->lock
);
646 LIST_FOR_EACH_ENTRY(rot_entry
, &This
->rot
, const struct rot_entry
, entry
)
648 if ((rot_entry
->moniker_data
->ulCntData
== moniker_data
->ulCntData
) &&
649 !memcmp(moniker_data
->abData
, rot_entry
->moniker_data
->abData
, moniker_data
->ulCntData
))
651 *pfiletime
= rot_entry
->last_modified
;
656 LeaveCriticalSection(&This
->lock
);
659 hr
= InternalIrotGetTimeOfLastChange(moniker_data
, pfiletime
);
661 HeapFree(GetProcessHeap(), 0, moniker_data
);
663 TRACE("-- 0x%08x\n", hr
);
667 /***********************************************************************
668 * RunningObjectTable_EnumRunning
671 * ppenumMoniker [out] receives the IEnumMoniker interface pointer
673 static HRESULT WINAPI
674 RunningObjectTableImpl_EnumRunning(IRunningObjectTable
* iface
,
675 IEnumMoniker
**ppenumMoniker
)
677 RunningObjectTableImpl
*This
= impl_from_IRunningObjectTable(iface
);
678 InterfaceList
*interface_list
= NULL
;
681 TRACE("(%p, %p)\n", This
, ppenumMoniker
);
683 *ppenumMoniker
= NULL
;
685 hr
= InternalIrotEnumRunning(&interface_list
);
687 hr
= EnumMonikerImpl_CreateEnumROTMoniker(interface_list
, 0, ppenumMoniker
);
692 /* Virtual function table for the IRunningObjectTable class. */
693 static const IRunningObjectTableVtbl VT_RunningObjectTableImpl
=
695 RunningObjectTableImpl_QueryInterface
,
696 RunningObjectTableImpl_AddRef
,
697 RunningObjectTableImpl_Release
,
698 RunningObjectTableImpl_Register
,
699 RunningObjectTableImpl_Revoke
,
700 RunningObjectTableImpl_IsRunning
,
701 RunningObjectTableImpl_GetObject
,
702 RunningObjectTableImpl_NoteChangeTime
,
703 RunningObjectTableImpl_GetTimeOfLastChange
,
704 RunningObjectTableImpl_EnumRunning
707 static RunningObjectTableImpl rot
=
709 .IRunningObjectTable_iface
.lpVtbl
= &VT_RunningObjectTableImpl
,
710 .lock
.LockCount
= -1,
711 .rot
= LIST_INIT(rot
.rot
),
714 /***********************************************************************
715 * GetRunningObjectTable (OLE32.@)
717 HRESULT WINAPI
GetRunningObjectTable(DWORD reserved
, IRunningObjectTable
**ret
)
719 TRACE("%#x, %p\n", reserved
, ret
);
724 if (!InternalIsProcessInitialized())
725 return CO_E_NOTINITIALIZED
;
727 *ret
= &rot
.IRunningObjectTable_iface
;
728 IRunningObjectTable_AddRef(*ret
);
733 /***********************************************************************
734 * DestroyRunningObjectTable (ole32.@)
736 void WINAPI
DestroyRunningObjectTable(void)
738 struct rot_entry
*rot_entry
, *cursor2
;
742 EnterCriticalSection(&rot
.lock
);
743 LIST_FOR_EACH_ENTRY_SAFE(rot_entry
, cursor2
, &rot
.rot
, struct rot_entry
, entry
)
745 list_remove(&rot_entry
->entry
);
746 rot_entry_delete(rot_entry
);
748 LeaveCriticalSection(&rot
.lock
);
751 static HRESULT
get_moniker_for_progid_display_name(LPBC pbc
,
752 LPCOLESTR szDisplayName
,
759 LPCWSTR start
= szDisplayName
;
762 IMoniker
*class_moniker
;
767 /* find end delimiter */
768 for (end
= start
; *end
; end
++)
774 /* must start with '@' or have a ':' somewhere and mustn't be one character
775 * long (since that looks like an absolute path) */
776 if (((start
== szDisplayName
) && (*end
== '\0')) || (len
<= 1))
779 progid
= HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
));
782 memcpy(progid
, start
, len
* sizeof(WCHAR
));
785 hr
= CLSIDFromProgID(progid
, &clsid
);
786 HeapFree(GetProcessHeap(), 0, progid
);
790 hr
= CreateClassMoniker(&clsid
, &class_moniker
);
793 IParseDisplayName
*pdn
;
794 hr
= IMoniker_BindToObject(class_moniker
, pbc
, NULL
,
795 &IID_IParseDisplayName
, (void **)&pdn
);
796 /* fallback to using IClassFactory to get IParseDisplayName -
797 * adsldp.dll depends on this */
801 hr
= IMoniker_BindToObject(class_moniker
, pbc
, NULL
,
802 &IID_IClassFactory
, (void **)&pcf
);
805 hr
= IClassFactory_CreateInstance(pcf
, NULL
,
806 &IID_IParseDisplayName
,
808 IClassFactory_Release(pcf
);
811 IMoniker_Release(class_moniker
);
814 hr
= IParseDisplayName_ParseDisplayName(pdn
, pbc
,
815 (LPOLESTR
)szDisplayName
,
817 IParseDisplayName_Release(pdn
);
823 /******************************************************************************
824 * MkParseDisplayName [OLE32.@]
826 HRESULT WINAPI
MkParseDisplayName(LPBC pbc
, LPCOLESTR szDisplayName
,
827 LPDWORD pchEaten
, LPMONIKER
*ppmk
)
829 HRESULT hr
= MK_E_SYNTAX
;
830 static const WCHAR wszClsidColon
[] = {'c','l','s','i','d',':'};
834 TRACE("(%p, %s, %p, %p)\n", pbc
, debugstr_w(szDisplayName
), pchEaten
, ppmk
);
836 if (!pbc
|| !IsValidInterface((LPUNKNOWN
) pbc
))
839 if (!szDisplayName
|| !*szDisplayName
)
842 if (!pchEaten
|| !ppmk
)
848 if (!wcsnicmp(szDisplayName
, wszClsidColon
, ARRAY_SIZE(wszClsidColon
)))
850 hr
= ClassMoniker_CreateFromDisplayName(pbc
, szDisplayName
, &chEaten
, &moniker
);
851 if (FAILED(hr
) && (hr
!= MK_E_SYNTAX
))
856 hr
= get_moniker_for_progid_display_name(pbc
, szDisplayName
, &chEaten
, &moniker
);
857 if (FAILED(hr
) && (hr
!= MK_E_SYNTAX
))
863 hr
= FileMoniker_CreateFromDisplayName(pbc
, szDisplayName
, &chEaten
, &moniker
);
864 if (FAILED(hr
) && (hr
!= MK_E_SYNTAX
))
872 IMoniker
*next_moniker
;
873 *pchEaten
+= chEaten
;
874 szDisplayName
+= chEaten
;
881 hr
= IMoniker_ParseDisplayName(moniker
, pbc
, NULL
,
882 (LPOLESTR
)szDisplayName
, &chEaten
,
884 IMoniker_Release(moniker
);
890 moniker
= next_moniker
;
897 /***********************************************************************
898 * GetClassFile (OLE32.@)
900 * Retrieves the class ID associated with the given filename.
903 * filePathName [I] Filename to retrieve the class ID for.
904 * pclsid [O] Address that receives the class ID for the file.
908 * Failure: Any HRESULT code.
910 HRESULT WINAPI
GetClassFile(LPCOLESTR filePathName
,CLSID
*pclsid
)
914 int nbElm
, length
, i
;
915 LONG sizeProgId
, ret
;
916 LPOLESTR
*pathDec
=0,absFile
=0,progId
=0;
919 TRACE("%s, %p\n", debugstr_w(filePathName
), pclsid
);
921 /* if the file contain a storage object the return the CLSID written by IStorage_SetClass method*/
922 if((StgIsStorageFile(filePathName
))==S_OK
){
924 res
=StgOpenStorage(filePathName
,NULL
,STGM_READ
| STGM_SHARE_DENY_WRITE
,NULL
,0,&pstg
);
926 if (SUCCEEDED(res
)) {
927 res
=ReadClassStg(pstg
,pclsid
);
928 IStorage_Release(pstg
);
933 /* If the file is not a storage object then attempt to match various bits in the file against a
934 pattern in the registry. This case is not frequently used, so I present only the pseudocode for
937 for(i=0;i<nFileTypes;i++)
939 for(i=0;j<nPatternsForType;j++){
944 pat=ReadPatternFromRegistry(i,j);
945 hFile=CreateFileW(filePathName,,,,,,hFile);
946 SetFilePosition(hFile,pat.offset);
947 ReadFile(hFile,buf,pat.size,&r,NULL);
948 if (memcmp(buf&pat.mask,pat.pattern.pat.size)==0){
950 *pclsid=ReadCLSIDFromRegistry(i);
956 /* if the above strategies fail then search for the extension key in the registry */
958 /* get the last element (absolute file) in the path name */
959 nbElm
=FileMonikerImpl_DecomposePath(filePathName
,&pathDec
);
960 absFile
=pathDec
[nbElm
-1];
962 /* failed if the path represents a directory and not an absolute file name*/
963 if (!wcscmp(absFile
, L
"\\")) {
964 CoTaskMemFree(pathDec
);
965 return MK_E_INVALIDEXTENSION
;
968 /* get the extension of the file */
970 length
=lstrlenW(absFile
);
971 for(i
= length
-1; (i
>= 0) && *(extension
= &absFile
[i
]) != '.'; i
--)
974 if (!extension
|| !wcscmp(extension
, L
".")) {
975 CoTaskMemFree(pathDec
);
976 return MK_E_INVALIDEXTENSION
;
979 ret
= RegQueryValueW(HKEY_CLASSES_ROOT
, extension
, NULL
, &sizeProgId
);
981 /* get the progId associated to the extension */
982 progId
= CoTaskMemAlloc(sizeProgId
);
983 ret
= RegQueryValueW(HKEY_CLASSES_ROOT
, extension
, progId
, &sizeProgId
);
985 /* return the clsid associated to the progId */
986 res
= CLSIDFromProgID(progId
, pclsid
);
988 res
= HRESULT_FROM_WIN32(ret
);
989 CoTaskMemFree(progId
);
992 res
= HRESULT_FROM_WIN32(ret
);
994 for(i
=0; pathDec
[i
]!=NULL
;i
++)
995 CoTaskMemFree(pathDec
[i
]);
996 CoTaskMemFree(pathDec
);
998 return res
!= S_OK
? MK_E_INVALIDEXTENSION
: res
;
1001 /***********************************************************************
1002 * EnumMoniker_QueryInterface
1004 static HRESULT WINAPI
EnumMonikerImpl_QueryInterface(IEnumMoniker
* iface
,REFIID riid
,void** ppvObject
)
1006 EnumMonikerImpl
*This
= impl_from_IEnumMoniker(iface
);
1008 TRACE("(%p,%s,%p)\n",This
,debugstr_guid(riid
),ppvObject
);
1010 /* validate arguments */
1011 if (ppvObject
== NULL
)
1012 return E_INVALIDARG
;
1016 if (IsEqualIID(&IID_IUnknown
, riid
) || IsEqualIID(&IID_IEnumMoniker
, riid
))
1017 *ppvObject
= &This
->IEnumMoniker_iface
;
1019 return E_NOINTERFACE
;
1021 IEnumMoniker_AddRef(iface
);
1025 /***********************************************************************
1026 * EnumMoniker_AddRef
1028 static ULONG WINAPI
EnumMonikerImpl_AddRef(IEnumMoniker
* iface
)
1030 EnumMonikerImpl
*This
= impl_from_IEnumMoniker(iface
);
1032 TRACE("(%p)\n",This
);
1034 return InterlockedIncrement(&This
->ref
);
1037 /***********************************************************************
1038 * EnumMoniker_release
1040 static ULONG WINAPI
EnumMonikerImpl_Release(IEnumMoniker
* iface
)
1042 EnumMonikerImpl
*This
= impl_from_IEnumMoniker(iface
);
1045 TRACE("(%p)\n",This
);
1047 ref
= InterlockedDecrement(&This
->ref
);
1049 /* uninitialize ROT structure if there are no more references to it */
1054 TRACE("(%p) Deleting\n",This
);
1056 for (i
= 0; i
< This
->moniker_list
->size
; i
++)
1057 HeapFree(GetProcessHeap(), 0, This
->moniker_list
->interfaces
[i
]);
1058 HeapFree(GetProcessHeap(), 0, This
->moniker_list
);
1059 HeapFree(GetProcessHeap(), 0, This
);
1064 /***********************************************************************
1067 static HRESULT WINAPI
EnumMonikerImpl_Next(IEnumMoniker
* iface
, ULONG celt
, IMoniker
** rgelt
, ULONG
* pceltFetched
)
1070 EnumMonikerImpl
*This
= impl_from_IEnumMoniker(iface
);
1073 TRACE("(%p) TabCurrentPos %d Tablastindx %d\n", This
, This
->pos
, This
->moniker_list
->size
);
1075 /* retrieve the requested number of moniker from the current position */
1076 for(i
= 0; (This
->pos
< This
->moniker_list
->size
) && (i
< celt
); i
++)
1079 hr
= create_stream_on_mip_ro(This
->moniker_list
->interfaces
[This
->pos
++], &stream
);
1080 if (hr
!= S_OK
) break;
1081 hr
= CoUnmarshalInterface(stream
, &IID_IMoniker
, (void **)&rgelt
[i
]);
1082 IStream_Release(stream
);
1083 if (hr
!= S_OK
) break;
1086 if (pceltFetched
!= NULL
)
1099 /***********************************************************************
1102 static HRESULT WINAPI
EnumMonikerImpl_Skip(IEnumMoniker
* iface
, ULONG celt
)
1104 EnumMonikerImpl
*This
= impl_from_IEnumMoniker(iface
);
1106 TRACE("(%p)\n",This
);
1108 if (This
->pos
+ celt
>= This
->moniker_list
->size
)
1116 /***********************************************************************
1119 static HRESULT WINAPI
EnumMonikerImpl_Reset(IEnumMoniker
* iface
)
1121 EnumMonikerImpl
*This
= impl_from_IEnumMoniker(iface
);
1123 This
->pos
= 0; /* set back to start of list */
1125 TRACE("(%p)\n",This
);
1130 /***********************************************************************
1133 static HRESULT WINAPI
EnumMonikerImpl_Clone(IEnumMoniker
* iface
, IEnumMoniker
** ppenum
)
1135 EnumMonikerImpl
*This
= impl_from_IEnumMoniker(iface
);
1136 InterfaceList
*moniker_list
;
1139 TRACE("(%p)\n",This
);
1143 moniker_list
= HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(InterfaceList
, interfaces
[This
->moniker_list
->size
]));
1145 return E_OUTOFMEMORY
;
1147 moniker_list
->size
= This
->moniker_list
->size
;
1148 for (i
= 0; i
< This
->moniker_list
->size
; i
++)
1150 SIZE_T size
= FIELD_OFFSET(InterfaceData
, abData
[This
->moniker_list
->interfaces
[i
]->ulCntData
]);
1151 moniker_list
->interfaces
[i
] = HeapAlloc(GetProcessHeap(), 0, size
);
1152 if (!moniker_list
->interfaces
[i
])
1155 for (i
= 0; i
< end
; i
++)
1156 HeapFree(GetProcessHeap(), 0, moniker_list
->interfaces
[i
]);
1157 HeapFree(GetProcessHeap(), 0, moniker_list
);
1158 return E_OUTOFMEMORY
;
1160 memcpy(moniker_list
->interfaces
[i
], This
->moniker_list
->interfaces
[i
], size
);
1163 /* copy the enum structure */
1164 return EnumMonikerImpl_CreateEnumROTMoniker(moniker_list
, This
->pos
, ppenum
);
1167 /* Virtual function table for the IEnumMoniker class. */
1168 static const IEnumMonikerVtbl VT_EnumMonikerImpl
=
1170 EnumMonikerImpl_QueryInterface
,
1171 EnumMonikerImpl_AddRef
,
1172 EnumMonikerImpl_Release
,
1173 EnumMonikerImpl_Next
,
1174 EnumMonikerImpl_Skip
,
1175 EnumMonikerImpl_Reset
,
1176 EnumMonikerImpl_Clone
1179 /***********************************************************************
1180 * EnumMonikerImpl_CreateEnumROTMoniker
1181 * Used by EnumRunning to create the structure and EnumClone
1182 * to copy the structure
1184 static HRESULT
EnumMonikerImpl_CreateEnumROTMoniker(InterfaceList
*moniker_list
,
1186 IEnumMoniker
**ppenumMoniker
)
1188 EnumMonikerImpl
* This
= NULL
;
1191 return E_INVALIDARG
;
1193 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(EnumMonikerImpl
));
1194 if (!This
) return E_OUTOFMEMORY
;
1196 TRACE("(%p)\n", This
);
1198 /* initialize the virtual table function */
1199 This
->IEnumMoniker_iface
.lpVtbl
= &VT_EnumMonikerImpl
;
1201 /* the initial reference is set to "1" */
1202 This
->ref
= 1; /* set the ref count to one */
1203 This
->pos
= current_pos
; /* Set the list start posn */
1204 This
->moniker_list
= moniker_list
;
1206 *ppenumMoniker
= &This
->IEnumMoniker_iface
;
1212 /* Shared implementation of moniker marshaler based on saving and loading of
1215 typedef struct MonikerMarshal
1217 IUnknown IUnknown_iface
;
1218 IMarshal IMarshal_iface
;
1224 static inline MonikerMarshal
*impl_from_IUnknown(IUnknown
*iface
)
1226 return CONTAINING_RECORD(iface
, MonikerMarshal
, IUnknown_iface
);
1229 static inline MonikerMarshal
*impl_from_IMarshal( IMarshal
*iface
)
1231 return CONTAINING_RECORD(iface
, MonikerMarshal
, IMarshal_iface
);
1234 static HRESULT WINAPI
MonikerMarshalInner_QueryInterface(IUnknown
*iface
, REFIID riid
, LPVOID
*ppv
)
1236 MonikerMarshal
*This
= impl_from_IUnknown(iface
);
1237 TRACE("(%s, %p)\n", debugstr_guid(riid
), ppv
);
1239 if (IsEqualIID(&IID_IUnknown
, riid
) || IsEqualIID(&IID_IMarshal
, riid
))
1241 *ppv
= &This
->IMarshal_iface
;
1242 IMarshal_AddRef(&This
->IMarshal_iface
);
1245 FIXME("No interface for %s\n", debugstr_guid(riid
));
1246 return E_NOINTERFACE
;
1249 static ULONG WINAPI
MonikerMarshalInner_AddRef(IUnknown
*iface
)
1251 MonikerMarshal
*This
= impl_from_IUnknown(iface
);
1252 return InterlockedIncrement(&This
->ref
);
1255 static ULONG WINAPI
MonikerMarshalInner_Release(IUnknown
*iface
)
1257 MonikerMarshal
*This
= impl_from_IUnknown(iface
);
1258 ULONG ref
= InterlockedDecrement(&This
->ref
);
1260 if (!ref
) HeapFree(GetProcessHeap(), 0, This
);
1264 static const IUnknownVtbl VT_MonikerMarshalInner
=
1266 MonikerMarshalInner_QueryInterface
,
1267 MonikerMarshalInner_AddRef
,
1268 MonikerMarshalInner_Release
1271 static HRESULT WINAPI
MonikerMarshal_QueryInterface(IMarshal
*iface
, REFIID riid
, LPVOID
*ppv
)
1273 MonikerMarshal
*This
= impl_from_IMarshal(iface
);
1274 return IMoniker_QueryInterface(This
->moniker
, riid
, ppv
);
1277 static ULONG WINAPI
MonikerMarshal_AddRef(IMarshal
*iface
)
1279 MonikerMarshal
*This
= impl_from_IMarshal(iface
);
1280 return IMoniker_AddRef(This
->moniker
);
1283 static ULONG WINAPI
MonikerMarshal_Release(IMarshal
*iface
)
1285 MonikerMarshal
*This
= impl_from_IMarshal(iface
);
1286 return IMoniker_Release(This
->moniker
);
1289 static HRESULT WINAPI
MonikerMarshal_GetUnmarshalClass(
1290 LPMARSHAL iface
, REFIID riid
, void* pv
, DWORD dwDestContext
,
1291 void* pvDestContext
, DWORD mshlflags
, CLSID
* pCid
)
1293 MonikerMarshal
*This
= impl_from_IMarshal(iface
);
1295 TRACE("(%s, %p, %x, %p, %x, %p)\n", debugstr_guid(riid
), pv
,
1296 dwDestContext
, pvDestContext
, mshlflags
, pCid
);
1298 return IMoniker_GetClassID(This
->moniker
, pCid
);
1301 static HRESULT WINAPI
MonikerMarshal_GetMarshalSizeMax(
1302 LPMARSHAL iface
, REFIID riid
, void* pv
, DWORD dwDestContext
,
1303 void* pvDestContext
, DWORD mshlflags
, DWORD
* pSize
)
1305 MonikerMarshal
*This
= impl_from_IMarshal(iface
);
1307 ULARGE_INTEGER size
;
1309 TRACE("(%s, %p, %x, %p, %x, %p)\n", debugstr_guid(riid
), pv
,
1310 dwDestContext
, pvDestContext
, mshlflags
, pSize
);
1312 hr
= IMoniker_GetSizeMax(This
->moniker
, &size
);
1314 *pSize
= (DWORD
)size
.QuadPart
;
1318 static HRESULT WINAPI
MonikerMarshal_MarshalInterface(LPMARSHAL iface
, IStream
*pStm
,
1319 REFIID riid
, void* pv
, DWORD dwDestContext
,
1320 void* pvDestContext
, DWORD mshlflags
)
1322 MonikerMarshal
*This
= impl_from_IMarshal(iface
);
1324 TRACE("(%p, %s, %p, %x, %p, %x)\n", pStm
, debugstr_guid(riid
), pv
,
1325 dwDestContext
, pvDestContext
, mshlflags
);
1327 return IMoniker_Save(This
->moniker
, pStm
, FALSE
);
1330 static HRESULT WINAPI
MonikerMarshal_UnmarshalInterface(LPMARSHAL iface
, IStream
*pStm
, REFIID riid
, void **ppv
)
1332 MonikerMarshal
*This
= impl_from_IMarshal(iface
);
1335 TRACE("(%p, %s, %p)\n", pStm
, debugstr_guid(riid
), ppv
);
1337 hr
= IMoniker_Load(This
->moniker
, pStm
);
1339 hr
= IMoniker_QueryInterface(This
->moniker
, riid
, ppv
);
1343 static HRESULT WINAPI
MonikerMarshal_ReleaseMarshalData(LPMARSHAL iface
, IStream
*pStm
)
1346 /* can't release a state-based marshal as nothing on server side to
1351 static HRESULT WINAPI
MonikerMarshal_DisconnectObject(LPMARSHAL iface
, DWORD dwReserved
)
1354 /* can't disconnect a state-based marshal as nothing on server side to
1355 * disconnect from */
1359 static const IMarshalVtbl VT_MonikerMarshal
=
1361 MonikerMarshal_QueryInterface
,
1362 MonikerMarshal_AddRef
,
1363 MonikerMarshal_Release
,
1364 MonikerMarshal_GetUnmarshalClass
,
1365 MonikerMarshal_GetMarshalSizeMax
,
1366 MonikerMarshal_MarshalInterface
,
1367 MonikerMarshal_UnmarshalInterface
,
1368 MonikerMarshal_ReleaseMarshalData
,
1369 MonikerMarshal_DisconnectObject
1372 HRESULT
MonikerMarshal_Create(IMoniker
*inner
, IUnknown
**outer
)
1374 MonikerMarshal
*This
= HeapAlloc(GetProcessHeap(), 0, sizeof(*This
));
1375 if (!This
) return E_OUTOFMEMORY
;
1377 This
->IUnknown_iface
.lpVtbl
= &VT_MonikerMarshalInner
;
1378 This
->IMarshal_iface
.lpVtbl
= &VT_MonikerMarshal
;
1380 This
->moniker
= inner
;
1382 *outer
= &This
->IUnknown_iface
;
1386 void * __RPC_USER
MIDL_user_allocate(SIZE_T size
)
1388 return HeapAlloc(GetProcessHeap(), 0, size
);
1391 void __RPC_USER
MIDL_user_free(void *p
)
1393 HeapFree(GetProcessHeap(), 0, p
);