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"
37 WINE_DEFAULT_DEBUG_CHANNEL(ole
);
40 BOOL WINAPI
InternalIsProcessInitialized(void);
41 HRESULT WINAPI
InternalIrotRegister(const MonikerComparisonData
*moniker_data
,
42 const InterfaceData
*object
, const InterfaceData
*moniker
,
43 const FILETIME
*time
, DWORD flags
, IrotCookie
*cookie
, IrotContextHandle
*ctxt_handle
);
44 HRESULT WINAPI
InternalIrotIsRunning(const MonikerComparisonData
*moniker_data
);
45 HRESULT WINAPI
InternalIrotGetObject(const MonikerComparisonData
*moniker_data
, PInterfaceData
*obj
,
47 HRESULT WINAPI
InternalIrotNoteChangeTime(IrotCookie cookie
, const FILETIME
*time
);
48 HRESULT WINAPI
InternalIrotGetTimeOfLastChange(const MonikerComparisonData
*moniker_data
, FILETIME
*time
);
49 HRESULT WINAPI
InternalIrotEnumRunning(PInterfaceList
*list
);
50 HRESULT WINAPI
InternalIrotRevoke(IrotCookie cookie
, IrotContextHandle
*ctxt_handle
, PInterfaceData
*object
,
51 PInterfaceData
*moniker
);
53 /* define the structure of the running object table elements */
57 InterfaceData
* object
; /* marshaled running object*/
58 MonikerComparisonData
* moniker_data
; /* moniker comparison data that identifies this object */
59 DWORD cookie
; /* cookie identifying this object */
60 FILETIME last_modified
;
61 IrotContextHandle ctxt_handle
;
64 /* define the RunningObjectTableImpl structure */
65 typedef struct RunningObjectTableImpl
67 IRunningObjectTable IRunningObjectTable_iface
;
68 struct list rot
; /* list of ROT entries */
69 CRITICAL_SECTION lock
;
70 } RunningObjectTableImpl
;
72 /* define the EnumMonikerImpl structure */
73 typedef struct EnumMonikerImpl
75 IEnumMoniker IEnumMoniker_iface
;
78 InterfaceList
*moniker_list
;
82 static inline RunningObjectTableImpl
*impl_from_IRunningObjectTable(IRunningObjectTable
*iface
)
84 return CONTAINING_RECORD(iface
, RunningObjectTableImpl
, IRunningObjectTable_iface
);
87 static inline EnumMonikerImpl
*impl_from_IEnumMoniker(IEnumMoniker
*iface
)
89 return CONTAINING_RECORD(iface
, EnumMonikerImpl
, IEnumMoniker_iface
);
92 /* IEnumMoniker Local functions*/
93 static HRESULT
EnumMonikerImpl_CreateEnumROTMoniker(InterfaceList
*moniker_list
,
94 ULONG pos
, IEnumMoniker
**ppenumMoniker
);
96 static HRESULT
create_stream_on_mip_ro(const InterfaceData
*mip
, IStream
**stream
)
98 HGLOBAL hglobal
= GlobalAlloc(0, mip
->ulCntData
);
99 void *pv
= GlobalLock(hglobal
);
100 memcpy(pv
, mip
->abData
, mip
->ulCntData
);
101 GlobalUnlock(hglobal
);
102 return CreateStreamOnHGlobal(hglobal
, TRUE
, stream
);
105 static void rot_entry_delete(struct rot_entry
*rot_entry
)
107 if (rot_entry
->cookie
)
109 InterfaceData
*object
= NULL
;
110 InterfaceData
*moniker
= NULL
;
112 InternalIrotRevoke(rot_entry
->cookie
, &rot_entry
->ctxt_handle
, &object
, &moniker
);
113 MIDL_user_free(object
);
118 hr
= create_stream_on_mip_ro(moniker
, &stream
);
121 CoReleaseMarshalData(stream
);
122 IStream_Release(stream
);
125 MIDL_user_free(moniker
);
127 if (rot_entry
->object
)
131 hr
= create_stream_on_mip_ro(rot_entry
->object
, &stream
);
134 CoReleaseMarshalData(stream
);
135 IStream_Release(stream
);
138 HeapFree(GetProcessHeap(), 0, rot_entry
->object
);
139 HeapFree(GetProcessHeap(), 0, rot_entry
->moniker_data
);
140 HeapFree(GetProcessHeap(), 0, rot_entry
);
143 /* moniker_data must be freed with HeapFree when no longer in use */
144 static HRESULT
get_moniker_comparison_data(IMoniker
*pMoniker
, MonikerComparisonData
**moniker_data
)
147 IROTData
*pROTData
= NULL
;
148 hr
= IMoniker_QueryInterface(pMoniker
, &IID_IROTData
, (void *)&pROTData
);
151 ULONG size
= ROT_COMPARE_MAX
;
152 *moniker_data
= HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(MonikerComparisonData
, abData
[size
]));
155 IROTData_Release(pROTData
);
156 return E_OUTOFMEMORY
;
158 hr
= IROTData_GetComparisonData(pROTData
, (*moniker_data
)->abData
, size
, &size
);
159 IROTData_Release(pROTData
);
162 ERR("Failed to copy comparison data into buffer, hr = %#lx\n", hr
);
163 HeapFree(GetProcessHeap(), 0, *moniker_data
);
164 *moniker_data
= NULL
;
167 (*moniker_data
)->ulCntData
= size
;
172 LPOLESTR pszDisplayName
;
176 TRACE("generating comparison data from display name\n");
178 hr
= CreateBindCtx(0, &pbc
);
181 hr
= IMoniker_GetDisplayName(pMoniker
, pbc
, NULL
, &pszDisplayName
);
182 IBindCtx_Release(pbc
);
185 hr
= IMoniker_GetClassID(pMoniker
, &clsid
);
188 CoTaskMemFree(pszDisplayName
);
192 len
= lstrlenW(pszDisplayName
);
193 *moniker_data
= HeapAlloc(GetProcessHeap(), 0,
194 FIELD_OFFSET(MonikerComparisonData
, abData
[sizeof(CLSID
) + (len
+1)*sizeof(WCHAR
)]));
197 CoTaskMemFree(pszDisplayName
);
198 return E_OUTOFMEMORY
;
200 (*moniker_data
)->ulCntData
= sizeof(CLSID
) + (len
+1)*sizeof(WCHAR
);
202 memcpy(&(*moniker_data
)->abData
[0], &clsid
, sizeof(clsid
));
203 memcpy(&(*moniker_data
)->abData
[sizeof(clsid
)], pszDisplayName
, (len
+1)*sizeof(WCHAR
));
204 CoTaskMemFree(pszDisplayName
);
209 static HRESULT
reduce_moniker(IMoniker
*pmk
, IBindCtx
*pbc
, IMoniker
**pmkReduced
)
211 IBindCtx
*pbcNew
= NULL
;
215 hr
= CreateBindCtx(0, &pbcNew
);
220 hr
= IMoniker_Reduce(pmk
, pbc
, MKRREDUCE_ALL
, NULL
, pmkReduced
);
222 ERR("reducing moniker failed with error %#lx.\n", hr
);
223 if (pbcNew
) IBindCtx_Release(pbcNew
);
227 /***********************************************************************
228 * RunningObjectTable_QueryInterface
230 static HRESULT WINAPI
231 RunningObjectTableImpl_QueryInterface(IRunningObjectTable
* iface
,
232 REFIID riid
,void** ppvObject
)
234 RunningObjectTableImpl
*This
= impl_from_IRunningObjectTable(iface
);
236 TRACE("(%p,%s,%p)\n",This
,debugstr_guid(riid
),ppvObject
);
238 /* validate arguments */
245 if (IsEqualIID(&IID_IUnknown
, riid
) ||
246 IsEqualIID(&IID_IRunningObjectTable
, riid
))
247 *ppvObject
= &This
->IRunningObjectTable_iface
;
250 return E_NOINTERFACE
;
252 IRunningObjectTable_AddRef(iface
);
257 static ULONG WINAPI
RunningObjectTableImpl_AddRef(IRunningObjectTable
*iface
)
259 TRACE("%p\n", iface
);
264 static ULONG WINAPI
RunningObjectTableImpl_Release(IRunningObjectTable
*iface
)
266 TRACE("%p\n", iface
);
271 /***********************************************************************
272 * RunningObjectTable_Register
275 * grfFlags [in] Registration options
276 * punkObject [in] the object being registered
277 * pmkObjectName [in] the moniker of the object being registered
278 * pdwRegister [out] the value identifying the registration
280 static HRESULT WINAPI
281 RunningObjectTableImpl_Register(IRunningObjectTable
* iface
, DWORD flags
,
282 IUnknown
*punkObject
, IMoniker
*pmkObjectName
, DWORD
*pdwRegister
)
284 RunningObjectTableImpl
*This
= impl_from_IRunningObjectTable(iface
);
285 struct rot_entry
*rot_entry
;
287 IStream
*pStream
= NULL
;
290 InterfaceData
*moniker
= NULL
;
292 TRACE("%p, %#lx, %p, %p, %p\n", iface
, flags
, punkObject
, pmkObjectName
, pdwRegister
);
294 if (flags
& ~(ROTFLAGS_REGISTRATIONKEEPSALIVE
|ROTFLAGS_ALLOWANYCLIENT
))
296 ERR("Invalid flags: %#lx\n", flags
& ~(ROTFLAGS_REGISTRATIONKEEPSALIVE
|ROTFLAGS_ALLOWANYCLIENT
));
300 if (punkObject
==NULL
|| pmkObjectName
==NULL
|| pdwRegister
==NULL
)
303 rot_entry
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*rot_entry
));
305 return E_OUTOFMEMORY
;
308 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
311 rot_entry_delete(rot_entry
);
314 mshlflags
= flags
& ROTFLAGS_REGISTRATIONKEEPSALIVE
? MSHLFLAGS_TABLESTRONG
: MSHLFLAGS_TABLEWEAK
;
315 hr
= CoMarshalInterface(pStream
, &IID_IUnknown
, punkObject
, MSHCTX_LOCAL
| MSHCTX_NOSHAREDMEM
, NULL
, mshlflags
);
316 /* FIXME: a cleaner way would be to create an IStream class that writes
317 * directly to an MInterfacePointer */
321 hr
= GetHGlobalFromStream(pStream
, &hglobal
);
324 SIZE_T size
= GlobalSize(hglobal
);
325 const void *pv
= GlobalLock(hglobal
);
326 rot_entry
->object
= HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(MInterfacePointer
, abData
[size
]));
327 rot_entry
->object
->ulCntData
= size
;
328 memcpy(rot_entry
->object
->abData
, pv
, size
);
329 GlobalUnlock(hglobal
);
332 IStream_Release(pStream
);
335 rot_entry_delete(rot_entry
);
339 hr
= CreateBindCtx(0, &pbc
);
342 rot_entry_delete(rot_entry
);
346 hr
= reduce_moniker(pmkObjectName
, pbc
, &pmkObjectName
);
349 rot_entry_delete(rot_entry
);
350 IBindCtx_Release(pbc
);
354 hr
= IMoniker_GetTimeOfLastChange(pmkObjectName
, pbc
, NULL
,
355 &rot_entry
->last_modified
);
356 IBindCtx_Release(pbc
);
359 CoFileTimeNow(&rot_entry
->last_modified
);
363 hr
= get_moniker_comparison_data(pmkObjectName
,
364 &rot_entry
->moniker_data
);
367 rot_entry_delete(rot_entry
);
368 IMoniker_Release(pmkObjectName
);
372 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
375 rot_entry_delete(rot_entry
);
376 IMoniker_Release(pmkObjectName
);
379 /* marshal moniker */
380 hr
= CoMarshalInterface(pStream
, &IID_IMoniker
, (IUnknown
*)pmkObjectName
,
381 MSHCTX_LOCAL
| MSHCTX_NOSHAREDMEM
, NULL
, MSHLFLAGS_TABLESTRONG
);
382 /* FIXME: a cleaner way would be to create an IStream class that writes
383 * directly to an MInterfacePointer */
387 hr
= GetHGlobalFromStream(pStream
, &hglobal
);
390 SIZE_T size
= GlobalSize(hglobal
);
391 const void *pv
= GlobalLock(hglobal
);
392 moniker
= HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(InterfaceData
, abData
[size
]));
393 moniker
->ulCntData
= size
;
394 memcpy(moniker
->abData
, pv
, size
);
395 GlobalUnlock(hglobal
);
398 IStream_Release(pStream
);
399 IMoniker_Release(pmkObjectName
);
402 HeapFree(GetProcessHeap(), 0, moniker
);
403 rot_entry_delete(rot_entry
);
407 hr
= InternalIrotRegister(rot_entry
->moniker_data
, rot_entry
->object
, moniker
,
408 &rot_entry
->last_modified
, flags
, &rot_entry
->cookie
, &rot_entry
->ctxt_handle
);
410 HeapFree(GetProcessHeap(), 0, moniker
);
413 rot_entry_delete(rot_entry
);
417 /* gives a registration identifier to the registered object*/
418 *pdwRegister
= rot_entry
->cookie
;
420 EnterCriticalSection(&This
->lock
);
421 list_add_tail(&This
->rot
, &rot_entry
->entry
);
422 LeaveCriticalSection(&This
->lock
);
427 /***********************************************************************
428 * RunningObjectTable_Revoke
431 * dwRegister [in] Value identifying registration to be revoked
433 static HRESULT WINAPI
434 RunningObjectTableImpl_Revoke( IRunningObjectTable
* iface
, DWORD dwRegister
)
436 RunningObjectTableImpl
*This
= impl_from_IRunningObjectTable(iface
);
437 struct rot_entry
*rot_entry
;
439 TRACE("%p, %ld.\n", iface
, dwRegister
);
441 EnterCriticalSection(&This
->lock
);
442 LIST_FOR_EACH_ENTRY(rot_entry
, &This
->rot
, struct rot_entry
, entry
)
444 if (rot_entry
->cookie
== dwRegister
)
446 list_remove(&rot_entry
->entry
);
447 LeaveCriticalSection(&This
->lock
);
449 rot_entry_delete(rot_entry
);
453 LeaveCriticalSection(&This
->lock
);
458 /***********************************************************************
459 * RunningObjectTable_IsRunning
462 * pmkObjectName [in] moniker of the object whose status is desired
464 static HRESULT WINAPI
465 RunningObjectTableImpl_IsRunning( IRunningObjectTable
* iface
, IMoniker
*pmkObjectName
)
467 RunningObjectTableImpl
*This
= impl_from_IRunningObjectTable(iface
);
468 MonikerComparisonData
*moniker_data
;
470 const struct rot_entry
*rot_entry
;
472 TRACE("(%p,%p)\n",This
,pmkObjectName
);
474 hr
= reduce_moniker(pmkObjectName
, NULL
, &pmkObjectName
);
477 hr
= get_moniker_comparison_data(pmkObjectName
, &moniker_data
);
478 IMoniker_Release(pmkObjectName
);
483 EnterCriticalSection(&This
->lock
);
484 LIST_FOR_EACH_ENTRY(rot_entry
, &This
->rot
, const struct rot_entry
, entry
)
486 if ((rot_entry
->moniker_data
->ulCntData
== moniker_data
->ulCntData
) &&
487 !memcmp(moniker_data
->abData
, rot_entry
->moniker_data
->abData
, moniker_data
->ulCntData
))
493 LeaveCriticalSection(&This
->lock
);
496 hr
= InternalIrotIsRunning(moniker_data
);
498 HeapFree(GetProcessHeap(), 0, moniker_data
);
503 /***********************************************************************
504 * RunningObjectTable_GetObject
507 * pmkObjectName [in] Pointer to the moniker on the object
508 * ppunkObject [out] variable that receives the IUnknown interface pointer
510 static HRESULT WINAPI
511 RunningObjectTableImpl_GetObject( IRunningObjectTable
* iface
,
512 IMoniker
*pmkObjectName
, IUnknown
**ppunkObject
)
514 RunningObjectTableImpl
*This
= impl_from_IRunningObjectTable(iface
);
515 MonikerComparisonData
*moniker_data
;
516 InterfaceData
*object
= NULL
;
519 struct rot_entry
*rot_entry
;
521 TRACE("(%p,%p,%p)\n",This
,pmkObjectName
,ppunkObject
);
523 if (ppunkObject
== NULL
)
528 hr
= reduce_moniker(pmkObjectName
, NULL
, &pmkObjectName
);
531 hr
= get_moniker_comparison_data(pmkObjectName
, &moniker_data
);
532 IMoniker_Release(pmkObjectName
);
536 EnterCriticalSection(&This
->lock
);
537 LIST_FOR_EACH_ENTRY(rot_entry
, &This
->rot
, struct rot_entry
, entry
)
539 if ((rot_entry
->moniker_data
->ulCntData
== moniker_data
->ulCntData
) &&
540 !memcmp(moniker_data
->abData
, rot_entry
->moniker_data
->abData
, moniker_data
->ulCntData
))
543 hr
= create_stream_on_mip_ro(rot_entry
->object
, &pStream
);
544 LeaveCriticalSection(&This
->lock
);
548 hr
= CoUnmarshalInterface(pStream
, &IID_IUnknown
, (void **)ppunkObject
);
549 IStream_Release(pStream
);
552 HeapFree(GetProcessHeap(), 0, moniker_data
);
557 LeaveCriticalSection(&This
->lock
);
559 TRACE("moniker unavailable locally, calling SCM\n");
561 hr
= InternalIrotGetObject(moniker_data
, &object
, &cookie
);
565 hr
= create_stream_on_mip_ro(object
, &pStream
);
568 hr
= CoUnmarshalInterface(pStream
, &IID_IUnknown
, (void **)ppunkObject
);
569 IStream_Release(pStream
);
573 WARN("Moniker unavailable, IrotGetObject returned %#lx\n", hr
);
575 HeapFree(GetProcessHeap(), 0, moniker_data
);
580 /***********************************************************************
581 * RunningObjectTable_NoteChangeTime
584 * dwRegister [in] Value identifying registration being updated
585 * pfiletime [in] Pointer to structure containing object's last change time
587 static HRESULT WINAPI
588 RunningObjectTableImpl_NoteChangeTime(IRunningObjectTable
* iface
,
589 DWORD dwRegister
, FILETIME
*pfiletime
)
591 RunningObjectTableImpl
*This
= impl_from_IRunningObjectTable(iface
);
592 struct rot_entry
*rot_entry
;
593 HRESULT hr
= E_INVALIDARG
;
595 TRACE("%p, %ld, %p.\n", iface
, dwRegister
, pfiletime
);
597 EnterCriticalSection(&This
->lock
);
598 LIST_FOR_EACH_ENTRY(rot_entry
, &This
->rot
, struct rot_entry
, entry
)
600 if (rot_entry
->cookie
== dwRegister
)
602 rot_entry
->last_modified
= *pfiletime
;
603 LeaveCriticalSection(&This
->lock
);
605 hr
= InternalIrotNoteChangeTime(dwRegister
, pfiletime
);
610 LeaveCriticalSection(&This
->lock
);
613 TRACE("-- %#lx\n", hr
);
617 /***********************************************************************
618 * RunningObjectTable_GetTimeOfLastChange
621 * pmkObjectName [in] moniker of the object whose status is desired
622 * pfiletime [out] structure that receives object's last change time
624 static HRESULT WINAPI
625 RunningObjectTableImpl_GetTimeOfLastChange(IRunningObjectTable
* iface
,
626 IMoniker
*pmkObjectName
, FILETIME
*pfiletime
)
628 HRESULT hr
= MK_E_UNAVAILABLE
;
629 RunningObjectTableImpl
*This
= impl_from_IRunningObjectTable(iface
);
630 MonikerComparisonData
*moniker_data
;
631 const struct rot_entry
*rot_entry
;
633 TRACE("(%p,%p,%p)\n",This
,pmkObjectName
,pfiletime
);
635 if (pmkObjectName
==NULL
|| pfiletime
==NULL
)
638 hr
= reduce_moniker(pmkObjectName
, NULL
, &pmkObjectName
);
641 hr
= get_moniker_comparison_data(pmkObjectName
, &moniker_data
);
642 IMoniker_Release(pmkObjectName
);
646 hr
= MK_E_UNAVAILABLE
;
648 EnterCriticalSection(&This
->lock
);
649 LIST_FOR_EACH_ENTRY(rot_entry
, &This
->rot
, const struct rot_entry
, entry
)
651 if ((rot_entry
->moniker_data
->ulCntData
== moniker_data
->ulCntData
) &&
652 !memcmp(moniker_data
->abData
, rot_entry
->moniker_data
->abData
, moniker_data
->ulCntData
))
654 *pfiletime
= rot_entry
->last_modified
;
659 LeaveCriticalSection(&This
->lock
);
662 hr
= InternalIrotGetTimeOfLastChange(moniker_data
, pfiletime
);
664 HeapFree(GetProcessHeap(), 0, moniker_data
);
666 TRACE("-- %#lx\n", hr
);
670 /***********************************************************************
671 * RunningObjectTable_EnumRunning
674 * ppenumMoniker [out] receives the IEnumMoniker interface pointer
676 static HRESULT WINAPI
677 RunningObjectTableImpl_EnumRunning(IRunningObjectTable
* iface
,
678 IEnumMoniker
**ppenumMoniker
)
680 RunningObjectTableImpl
*This
= impl_from_IRunningObjectTable(iface
);
681 InterfaceList
*interface_list
= NULL
;
684 TRACE("(%p, %p)\n", This
, ppenumMoniker
);
686 *ppenumMoniker
= NULL
;
688 hr
= InternalIrotEnumRunning(&interface_list
);
690 hr
= EnumMonikerImpl_CreateEnumROTMoniker(interface_list
, 0, ppenumMoniker
);
695 /* Virtual function table for the IRunningObjectTable class. */
696 static const IRunningObjectTableVtbl VT_RunningObjectTableImpl
=
698 RunningObjectTableImpl_QueryInterface
,
699 RunningObjectTableImpl_AddRef
,
700 RunningObjectTableImpl_Release
,
701 RunningObjectTableImpl_Register
,
702 RunningObjectTableImpl_Revoke
,
703 RunningObjectTableImpl_IsRunning
,
704 RunningObjectTableImpl_GetObject
,
705 RunningObjectTableImpl_NoteChangeTime
,
706 RunningObjectTableImpl_GetTimeOfLastChange
,
707 RunningObjectTableImpl_EnumRunning
710 static RunningObjectTableImpl rot
;
712 static RTL_CRITICAL_SECTION_DEBUG critsect_debug
=
715 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
716 0, 0, { (DWORD_PTR
)(__FILE__
": RunningObjectTable_section") }
719 static RunningObjectTableImpl rot
=
721 .IRunningObjectTable_iface
.lpVtbl
= &VT_RunningObjectTableImpl
,
722 .lock
= { &critsect_debug
, -1, 0, 0, 0, 0 },
723 .rot
= LIST_INIT(rot
.rot
),
726 /***********************************************************************
727 * GetRunningObjectTable (OLE32.@)
729 HRESULT WINAPI
GetRunningObjectTable(DWORD reserved
, IRunningObjectTable
**ret
)
731 TRACE("%#lx, %p\n", reserved
, ret
);
736 if (!InternalIsProcessInitialized())
737 return CO_E_NOTINITIALIZED
;
739 *ret
= &rot
.IRunningObjectTable_iface
;
740 IRunningObjectTable_AddRef(*ret
);
745 /***********************************************************************
746 * DestroyRunningObjectTable (ole32.@)
748 void WINAPI
DestroyRunningObjectTable(void)
750 struct rot_entry
*rot_entry
, *cursor2
;
754 EnterCriticalSection(&rot
.lock
);
755 LIST_FOR_EACH_ENTRY_SAFE(rot_entry
, cursor2
, &rot
.rot
, struct rot_entry
, entry
)
757 list_remove(&rot_entry
->entry
);
758 rot_entry_delete(rot_entry
);
760 LeaveCriticalSection(&rot
.lock
);
763 static HRESULT
get_moniker_for_progid_display_name(LPBC pbc
,
764 LPCOLESTR szDisplayName
,
771 LPCWSTR start
= szDisplayName
;
774 IMoniker
*class_moniker
;
779 /* find end delimiter */
780 for (end
= start
; *end
; end
++)
786 /* must start with '@' or have a ':' somewhere and mustn't be one character
787 * long (since that looks like an absolute path) */
788 if (((start
== szDisplayName
) && (*end
== '\0')) || (len
<= 1))
791 progid
= HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
));
794 memcpy(progid
, start
, len
* sizeof(WCHAR
));
797 hr
= CLSIDFromProgID(progid
, &clsid
);
798 HeapFree(GetProcessHeap(), 0, progid
);
802 hr
= CreateClassMoniker(&clsid
, &class_moniker
);
805 IParseDisplayName
*pdn
;
806 hr
= IMoniker_BindToObject(class_moniker
, pbc
, NULL
,
807 &IID_IParseDisplayName
, (void **)&pdn
);
808 /* fallback to using IClassFactory to get IParseDisplayName -
809 * adsldp.dll depends on this */
813 hr
= IMoniker_BindToObject(class_moniker
, pbc
, NULL
,
814 &IID_IClassFactory
, (void **)&pcf
);
817 hr
= IClassFactory_CreateInstance(pcf
, NULL
,
818 &IID_IParseDisplayName
,
820 IClassFactory_Release(pcf
);
823 IMoniker_Release(class_moniker
);
826 hr
= IParseDisplayName_ParseDisplayName(pdn
, pbc
,
827 (LPOLESTR
)szDisplayName
,
829 IParseDisplayName_Release(pdn
);
835 /******************************************************************************
836 * MkParseDisplayName [OLE32.@]
838 HRESULT WINAPI
MkParseDisplayName(LPBC pbc
, LPCOLESTR szDisplayName
,
839 LPDWORD pchEaten
, LPMONIKER
*ppmk
)
841 HRESULT hr
= MK_E_SYNTAX
;
845 TRACE("(%p, %s, %p, %p)\n", pbc
, debugstr_w(szDisplayName
), pchEaten
, ppmk
);
847 if (!pbc
|| !IsValidInterface((LPUNKNOWN
) pbc
))
850 if (!szDisplayName
|| !*szDisplayName
)
853 if (!pchEaten
|| !ppmk
)
859 if (!wcsnicmp(szDisplayName
, L
"clsid:", 6))
861 hr
= ClassMoniker_CreateFromDisplayName(pbc
, szDisplayName
, &chEaten
, &moniker
);
862 if (FAILED(hr
) && (hr
!= MK_E_SYNTAX
))
867 hr
= get_moniker_for_progid_display_name(pbc
, szDisplayName
, &chEaten
, &moniker
);
868 if (FAILED(hr
) && (hr
!= MK_E_SYNTAX
))
874 hr
= FileMoniker_CreateFromDisplayName(pbc
, szDisplayName
, &chEaten
, &moniker
);
875 if (FAILED(hr
) && (hr
!= MK_E_SYNTAX
))
883 IMoniker
*next_moniker
;
884 *pchEaten
+= chEaten
;
885 szDisplayName
+= chEaten
;
892 hr
= IMoniker_ParseDisplayName(moniker
, pbc
, NULL
,
893 (LPOLESTR
)szDisplayName
, &chEaten
,
895 IMoniker_Release(moniker
);
901 moniker
= next_moniker
;
908 /***********************************************************************
909 * GetClassFile (OLE32.@)
911 * Retrieves the class ID associated with the given filename.
914 * filePathName [I] Filename to retrieve the class ID for.
915 * pclsid [O] Address that receives the class ID for the file.
919 * Failure: Any HRESULT code.
921 HRESULT WINAPI
GetClassFile(LPCOLESTR filePathName
,CLSID
*pclsid
)
925 LONG sizeProgId
, ret
;
927 const WCHAR
*extension
;
929 TRACE("%s, %p\n", debugstr_w(filePathName
), pclsid
);
931 /* if the file contain a storage object the return the CLSID written by IStorage_SetClass method*/
932 if((StgIsStorageFile(filePathName
))==S_OK
){
934 res
=StgOpenStorage(filePathName
,NULL
,STGM_READ
| STGM_SHARE_DENY_WRITE
,NULL
,0,&pstg
);
936 if (SUCCEEDED(res
)) {
937 res
=ReadClassStg(pstg
,pclsid
);
938 IStorage_Release(pstg
);
943 /* If the file is not a storage object then attempt to match various bits in the file against a
944 pattern in the registry. This case is not frequently used, so I present only the pseudocode for
947 for(i=0;i<nFileTypes;i++)
949 for(i=0;j<nPatternsForType;j++){
954 pat=ReadPatternFromRegistry(i,j);
955 hFile=CreateFileW(filePathName,,,,,,hFile);
956 SetFilePosition(hFile,pat.offset);
957 ReadFile(hFile,buf,pat.size,&r,NULL);
958 if (memcmp(buf&pat.mask,pat.pattern.pat.size)==0){
960 *pclsid=ReadCLSIDFromRegistry(i);
966 /* if the above strategies fail then search for the extension key in the registry */
968 res
= PathCchFindExtension(filePathName
, PATHCCH_MAX_CCH
, &extension
);
969 if (FAILED(res
) || !extension
|| !*extension
|| !wcscmp(extension
, L
"."))
970 return MK_E_INVALIDEXTENSION
;
972 ret
= RegQueryValueW(HKEY_CLASSES_ROOT
, extension
, NULL
, &sizeProgId
);
974 /* get the progId associated to the extension */
975 progId
= CoTaskMemAlloc(sizeProgId
);
976 ret
= RegQueryValueW(HKEY_CLASSES_ROOT
, extension
, progId
, &sizeProgId
);
978 /* return the clsid associated to the progId */
979 res
= CLSIDFromProgID(progId
, pclsid
);
981 res
= HRESULT_FROM_WIN32(ret
);
982 CoTaskMemFree(progId
);
985 res
= HRESULT_FROM_WIN32(ret
);
987 return res
!= S_OK
? MK_E_INVALIDEXTENSION
: res
;
990 /***********************************************************************
991 * EnumMoniker_QueryInterface
993 static HRESULT WINAPI
EnumMonikerImpl_QueryInterface(IEnumMoniker
* iface
,REFIID riid
,void** ppvObject
)
995 EnumMonikerImpl
*This
= impl_from_IEnumMoniker(iface
);
997 TRACE("(%p,%s,%p)\n",This
,debugstr_guid(riid
),ppvObject
);
999 /* validate arguments */
1000 if (ppvObject
== NULL
)
1001 return E_INVALIDARG
;
1005 if (IsEqualIID(&IID_IUnknown
, riid
) || IsEqualIID(&IID_IEnumMoniker
, riid
))
1006 *ppvObject
= &This
->IEnumMoniker_iface
;
1008 return E_NOINTERFACE
;
1010 IEnumMoniker_AddRef(iface
);
1014 /***********************************************************************
1015 * EnumMoniker_AddRef
1017 static ULONG WINAPI
EnumMonikerImpl_AddRef(IEnumMoniker
* iface
)
1019 EnumMonikerImpl
*This
= impl_from_IEnumMoniker(iface
);
1021 TRACE("(%p)\n",This
);
1023 return InterlockedIncrement(&This
->ref
);
1026 /***********************************************************************
1027 * EnumMoniker_release
1029 static ULONG WINAPI
EnumMonikerImpl_Release(IEnumMoniker
* iface
)
1031 EnumMonikerImpl
*This
= impl_from_IEnumMoniker(iface
);
1034 TRACE("(%p)\n",This
);
1036 ref
= InterlockedDecrement(&This
->ref
);
1038 /* uninitialize ROT structure if there are no more references to it */
1043 TRACE("(%p) Deleting\n",This
);
1045 for (i
= 0; i
< This
->moniker_list
->size
; i
++)
1046 free(This
->moniker_list
->interfaces
[i
]);
1047 free(This
->moniker_list
);
1048 HeapFree(GetProcessHeap(), 0, This
);
1053 /***********************************************************************
1056 static HRESULT WINAPI
EnumMonikerImpl_Next(IEnumMoniker
* iface
, ULONG celt
, IMoniker
** rgelt
, ULONG
* pceltFetched
)
1059 EnumMonikerImpl
*This
= impl_from_IEnumMoniker(iface
);
1062 TRACE("%p, %lu, %p, %p.\n", iface
, celt
, rgelt
, pceltFetched
);
1064 /* retrieve the requested number of moniker from the current position */
1065 for(i
= 0; (This
->pos
< This
->moniker_list
->size
) && (i
< celt
); i
++)
1068 hr
= create_stream_on_mip_ro(This
->moniker_list
->interfaces
[This
->pos
++], &stream
);
1069 if (hr
!= S_OK
) break;
1070 hr
= CoUnmarshalInterface(stream
, &IID_IMoniker
, (void **)&rgelt
[i
]);
1071 IStream_Release(stream
);
1072 if (hr
!= S_OK
) break;
1075 if (pceltFetched
!= NULL
)
1088 /***********************************************************************
1091 static HRESULT WINAPI
EnumMonikerImpl_Skip(IEnumMoniker
* iface
, ULONG celt
)
1093 EnumMonikerImpl
*This
= impl_from_IEnumMoniker(iface
);
1095 TRACE("(%p)\n",This
);
1097 if (This
->pos
+ celt
>= This
->moniker_list
->size
)
1105 /***********************************************************************
1108 static HRESULT WINAPI
EnumMonikerImpl_Reset(IEnumMoniker
* iface
)
1110 EnumMonikerImpl
*This
= impl_from_IEnumMoniker(iface
);
1112 This
->pos
= 0; /* set back to start of list */
1114 TRACE("(%p)\n",This
);
1119 /***********************************************************************
1122 static HRESULT WINAPI
EnumMonikerImpl_Clone(IEnumMoniker
* iface
, IEnumMoniker
** ppenum
)
1124 EnumMonikerImpl
*This
= impl_from_IEnumMoniker(iface
);
1125 InterfaceList
*moniker_list
;
1128 TRACE("(%p)\n",This
);
1132 moniker_list
= malloc(FIELD_OFFSET(InterfaceList
, interfaces
[This
->moniker_list
->size
]));
1134 return E_OUTOFMEMORY
;
1136 moniker_list
->size
= This
->moniker_list
->size
;
1137 for (i
= 0; i
< This
->moniker_list
->size
; i
++)
1139 SIZE_T size
= FIELD_OFFSET(InterfaceData
, abData
[This
->moniker_list
->interfaces
[i
]->ulCntData
]);
1140 moniker_list
->interfaces
[i
] = malloc(size
);
1141 if (!moniker_list
->interfaces
[i
])
1144 for (i
= 0; i
< end
; i
++)
1145 free(moniker_list
->interfaces
[i
]);
1147 return E_OUTOFMEMORY
;
1149 memcpy(moniker_list
->interfaces
[i
], This
->moniker_list
->interfaces
[i
], size
);
1152 /* copy the enum structure */
1153 return EnumMonikerImpl_CreateEnumROTMoniker(moniker_list
, This
->pos
, ppenum
);
1156 /* Virtual function table for the IEnumMoniker class. */
1157 static const IEnumMonikerVtbl VT_EnumMonikerImpl
=
1159 EnumMonikerImpl_QueryInterface
,
1160 EnumMonikerImpl_AddRef
,
1161 EnumMonikerImpl_Release
,
1162 EnumMonikerImpl_Next
,
1163 EnumMonikerImpl_Skip
,
1164 EnumMonikerImpl_Reset
,
1165 EnumMonikerImpl_Clone
1168 /***********************************************************************
1169 * EnumMonikerImpl_CreateEnumROTMoniker
1170 * Used by EnumRunning to create the structure and EnumClone
1171 * to copy the structure
1173 static HRESULT
EnumMonikerImpl_CreateEnumROTMoniker(InterfaceList
*moniker_list
,
1175 IEnumMoniker
**ppenumMoniker
)
1177 EnumMonikerImpl
* This
= NULL
;
1180 return E_INVALIDARG
;
1182 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(EnumMonikerImpl
));
1183 if (!This
) return E_OUTOFMEMORY
;
1185 TRACE("(%p)\n", This
);
1187 /* initialize the virtual table function */
1188 This
->IEnumMoniker_iface
.lpVtbl
= &VT_EnumMonikerImpl
;
1190 /* the initial reference is set to "1" */
1191 This
->ref
= 1; /* set the ref count to one */
1192 This
->pos
= current_pos
; /* Set the list start posn */
1193 This
->moniker_list
= moniker_list
;
1195 *ppenumMoniker
= &This
->IEnumMoniker_iface
;
1201 /* Shared implementation of moniker marshaler based on saving and loading of
1204 typedef struct MonikerMarshal
1206 IUnknown IUnknown_iface
;
1207 IMarshal IMarshal_iface
;
1213 static inline MonikerMarshal
*impl_from_IUnknown(IUnknown
*iface
)
1215 return CONTAINING_RECORD(iface
, MonikerMarshal
, IUnknown_iface
);
1218 static inline MonikerMarshal
*impl_from_IMarshal( IMarshal
*iface
)
1220 return CONTAINING_RECORD(iface
, MonikerMarshal
, IMarshal_iface
);
1223 static HRESULT WINAPI
MonikerMarshalInner_QueryInterface(IUnknown
*iface
, REFIID riid
, LPVOID
*ppv
)
1225 MonikerMarshal
*This
= impl_from_IUnknown(iface
);
1226 TRACE("(%s, %p)\n", debugstr_guid(riid
), ppv
);
1228 if (IsEqualIID(&IID_IUnknown
, riid
) || IsEqualIID(&IID_IMarshal
, riid
))
1230 *ppv
= &This
->IMarshal_iface
;
1231 IMarshal_AddRef(&This
->IMarshal_iface
);
1234 FIXME("No interface for %s\n", debugstr_guid(riid
));
1235 return E_NOINTERFACE
;
1238 static ULONG WINAPI
MonikerMarshalInner_AddRef(IUnknown
*iface
)
1240 MonikerMarshal
*This
= impl_from_IUnknown(iface
);
1241 return InterlockedIncrement(&This
->ref
);
1244 static ULONG WINAPI
MonikerMarshalInner_Release(IUnknown
*iface
)
1246 MonikerMarshal
*This
= impl_from_IUnknown(iface
);
1247 ULONG ref
= InterlockedDecrement(&This
->ref
);
1249 if (!ref
) HeapFree(GetProcessHeap(), 0, This
);
1253 static const IUnknownVtbl VT_MonikerMarshalInner
=
1255 MonikerMarshalInner_QueryInterface
,
1256 MonikerMarshalInner_AddRef
,
1257 MonikerMarshalInner_Release
1260 static HRESULT WINAPI
MonikerMarshal_QueryInterface(IMarshal
*iface
, REFIID riid
, LPVOID
*ppv
)
1262 MonikerMarshal
*This
= impl_from_IMarshal(iface
);
1263 return IMoniker_QueryInterface(This
->moniker
, riid
, ppv
);
1266 static ULONG WINAPI
MonikerMarshal_AddRef(IMarshal
*iface
)
1268 MonikerMarshal
*This
= impl_from_IMarshal(iface
);
1269 return IMoniker_AddRef(This
->moniker
);
1272 static ULONG WINAPI
MonikerMarshal_Release(IMarshal
*iface
)
1274 MonikerMarshal
*This
= impl_from_IMarshal(iface
);
1275 return IMoniker_Release(This
->moniker
);
1278 static HRESULT WINAPI
MonikerMarshal_GetUnmarshalClass(
1279 LPMARSHAL iface
, REFIID riid
, void* pv
, DWORD dwDestContext
,
1280 void* pvDestContext
, DWORD mshlflags
, CLSID
* pCid
)
1282 MonikerMarshal
*This
= impl_from_IMarshal(iface
);
1284 TRACE("%s, %p, %lx, %p, %lx, %p.\n", debugstr_guid(riid
), pv
,
1285 dwDestContext
, pvDestContext
, mshlflags
, pCid
);
1287 return IMoniker_GetClassID(This
->moniker
, pCid
);
1290 static HRESULT WINAPI
MonikerMarshal_GetMarshalSizeMax(
1291 LPMARSHAL iface
, REFIID riid
, void* pv
, DWORD dwDestContext
,
1292 void* pvDestContext
, DWORD mshlflags
, DWORD
* pSize
)
1294 MonikerMarshal
*This
= impl_from_IMarshal(iface
);
1296 ULARGE_INTEGER size
;
1298 TRACE("%s, %p, %lx, %p, %lx, %p.\n", debugstr_guid(riid
), pv
,
1299 dwDestContext
, pvDestContext
, mshlflags
, pSize
);
1301 hr
= IMoniker_GetSizeMax(This
->moniker
, &size
);
1303 *pSize
= (DWORD
)size
.QuadPart
;
1307 static HRESULT WINAPI
MonikerMarshal_MarshalInterface(LPMARSHAL iface
, IStream
*pStm
,
1308 REFIID riid
, void* pv
, DWORD dwDestContext
,
1309 void* pvDestContext
, DWORD mshlflags
)
1311 MonikerMarshal
*This
= impl_from_IMarshal(iface
);
1313 TRACE("%p, %s, %p, %lx, %p, %lx.\n", pStm
, debugstr_guid(riid
), pv
,
1314 dwDestContext
, pvDestContext
, mshlflags
);
1316 return IMoniker_Save(This
->moniker
, pStm
, FALSE
);
1319 static HRESULT WINAPI
MonikerMarshal_UnmarshalInterface(LPMARSHAL iface
, IStream
*pStm
, REFIID riid
, void **ppv
)
1321 MonikerMarshal
*This
= impl_from_IMarshal(iface
);
1324 TRACE("(%p, %s, %p)\n", pStm
, debugstr_guid(riid
), ppv
);
1326 hr
= IMoniker_Load(This
->moniker
, pStm
);
1328 hr
= IMoniker_QueryInterface(This
->moniker
, riid
, ppv
);
1332 static HRESULT WINAPI
MonikerMarshal_ReleaseMarshalData(LPMARSHAL iface
, IStream
*pStm
)
1335 /* can't release a state-based marshal as nothing on server side to
1340 static HRESULT WINAPI
MonikerMarshal_DisconnectObject(LPMARSHAL iface
, DWORD dwReserved
)
1343 /* can't disconnect a state-based marshal as nothing on server side to
1344 * disconnect from */
1348 static const IMarshalVtbl VT_MonikerMarshal
=
1350 MonikerMarshal_QueryInterface
,
1351 MonikerMarshal_AddRef
,
1352 MonikerMarshal_Release
,
1353 MonikerMarshal_GetUnmarshalClass
,
1354 MonikerMarshal_GetMarshalSizeMax
,
1355 MonikerMarshal_MarshalInterface
,
1356 MonikerMarshal_UnmarshalInterface
,
1357 MonikerMarshal_ReleaseMarshalData
,
1358 MonikerMarshal_DisconnectObject
1361 HRESULT
MonikerMarshal_Create(IMoniker
*inner
, IUnknown
**outer
)
1363 MonikerMarshal
*This
= HeapAlloc(GetProcessHeap(), 0, sizeof(*This
));
1364 if (!This
) return E_OUTOFMEMORY
;
1366 This
->IUnknown_iface
.lpVtbl
= &VT_MonikerMarshalInner
;
1367 This
->IMarshal_iface
.lpVtbl
= &VT_MonikerMarshal
;
1369 This
->moniker
= inner
;
1371 *outer
= &This
->IUnknown_iface
;
1375 void * __RPC_USER
MIDL_user_allocate(SIZE_T size
)
1377 return malloc(size
);
1380 void __RPC_USER
MIDL_user_free(void *p
)