mf/session: Forward more events to the application.
[wine/zf.git] / dlls / ole32 / moniker.c
blobbe78eb90cf320451e324bb7cb7a6479beca506ed
1 /*
2 * Monikers
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 #include <stdarg.h>
25 #include <string.h>
27 #define COBJMACROS
29 #include "wine/list.h"
30 #include "wine/debug.h"
32 #include "compobj_private.h"
33 #include "moniker.h"
34 #include "irot.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(ole);
38 /* Combase exports */
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,
45 IrotCookie *cookie);
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 */
53 struct rot_entry
55 struct list entry;
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;
75 LONG ref;
77 InterfaceList *moniker_list;
78 ULONG pos;
79 } EnumMonikerImpl;
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);
113 if (moniker)
115 IStream *stream;
116 HRESULT hr;
117 hr = create_stream_on_mip_ro(moniker, &stream);
118 if (hr == S_OK)
120 CoReleaseMarshalData(stream);
121 IStream_Release(stream);
124 MIDL_user_free(moniker);
126 if (rot_entry->object)
128 IStream *stream;
129 HRESULT hr;
130 hr = create_stream_on_mip_ro(rot_entry->object, &stream);
131 if (hr == S_OK)
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)
145 HRESULT hr;
146 IROTData *pROTData = NULL;
147 hr = IMoniker_QueryInterface(pMoniker, &IID_IROTData, (void *)&pROTData);
148 if (SUCCEEDED(hr))
150 ULONG size = ROT_COMPARE_MAX;
151 *moniker_data = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(MonikerComparisonData, abData[size]));
152 if (!*moniker_data)
154 IROTData_Release(pROTData);
155 return E_OUTOFMEMORY;
157 hr = IROTData_GetComparisonData(pROTData, (*moniker_data)->abData, size, &size);
158 IROTData_Release(pROTData);
159 if (hr != S_OK)
161 ERR("Failed to copy comparison data into buffer, hr = 0x%08x\n", hr);
162 HeapFree(GetProcessHeap(), 0, *moniker_data);
163 return hr;
165 (*moniker_data)->ulCntData = size;
167 else
169 IBindCtx *pbc;
170 LPOLESTR pszDisplayName;
171 CLSID clsid;
172 int len;
174 TRACE("generating comparison data from display name\n");
176 hr = CreateBindCtx(0, &pbc);
177 if (FAILED(hr))
178 return hr;
179 hr = IMoniker_GetDisplayName(pMoniker, pbc, NULL, &pszDisplayName);
180 IBindCtx_Release(pbc);
181 if (FAILED(hr))
182 return hr;
183 hr = IMoniker_GetClassID(pMoniker, &clsid);
184 if (FAILED(hr))
186 CoTaskMemFree(pszDisplayName);
187 return hr;
190 len = lstrlenW(pszDisplayName);
191 *moniker_data = HeapAlloc(GetProcessHeap(), 0,
192 FIELD_OFFSET(MonikerComparisonData, abData[sizeof(CLSID) + (len+1)*sizeof(WCHAR)]));
193 if (!*moniker_data)
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);
204 return S_OK;
207 static HRESULT reduce_moniker(IMoniker *pmk, IBindCtx *pbc, IMoniker **pmkReduced)
209 IBindCtx *pbcNew = NULL;
210 HRESULT hr;
211 if (!pbc)
213 hr = CreateBindCtx(0, &pbcNew);
214 if (FAILED(hr))
215 return hr;
216 pbc = pbcNew;
218 hr = IMoniker_Reduce(pmk, pbc, MKRREDUCE_ALL, NULL, pmkReduced);
219 if (FAILED(hr))
220 ERR("reducing moniker failed with error 0x%08x\n", hr);
221 if (pbcNew) IBindCtx_Release(pbcNew);
222 return hr;
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 */
238 if (ppvObject==0)
239 return E_INVALIDARG;
241 *ppvObject = 0;
243 if (IsEqualIID(&IID_IUnknown, riid) ||
244 IsEqualIID(&IID_IRunningObjectTable, riid))
245 *ppvObject = &This->IRunningObjectTable_iface;
247 if ((*ppvObject)==0)
248 return E_NOINTERFACE;
250 IRunningObjectTable_AddRef(iface);
252 return S_OK;
255 static ULONG WINAPI RunningObjectTableImpl_AddRef(IRunningObjectTable *iface)
257 TRACE("%p\n", iface);
259 return 2;
262 static ULONG WINAPI RunningObjectTableImpl_Release(IRunningObjectTable *iface)
264 TRACE("%p\n", iface);
266 return 1;
269 /***********************************************************************
270 * RunningObjectTable_Register
272 * PARAMS
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;
284 HRESULT hr = S_OK;
285 IStream *pStream = NULL;
286 DWORD mshlflags;
287 IBindCtx *pbc;
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));
295 return E_INVALIDARG;
298 if (punkObject==NULL || pmkObjectName==NULL || pdwRegister==NULL)
299 return E_INVALIDARG;
301 rot_entry = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*rot_entry));
302 if (!rot_entry)
303 return E_OUTOFMEMORY;
305 /* marshal object */
306 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
307 if (hr != S_OK)
309 rot_entry_delete(rot_entry);
310 return hr;
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 */
316 if (hr == S_OK)
318 HGLOBAL hglobal;
319 hr = GetHGlobalFromStream(pStream, &hglobal);
320 if (hr == S_OK)
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);
331 if (hr != S_OK)
333 rot_entry_delete(rot_entry);
334 return hr;
337 hr = CreateBindCtx(0, &pbc);
338 if (FAILED(hr))
340 rot_entry_delete(rot_entry);
341 return hr;
344 hr = reduce_moniker(pmkObjectName, pbc, &pmkObjectName);
345 if (FAILED(hr))
347 rot_entry_delete(rot_entry);
348 IBindCtx_Release(pbc);
349 return hr;
352 hr = IMoniker_GetTimeOfLastChange(pmkObjectName, pbc, NULL,
353 &rot_entry->last_modified);
354 IBindCtx_Release(pbc);
355 if (FAILED(hr))
357 CoFileTimeNow(&rot_entry->last_modified);
358 hr = S_OK;
361 hr = get_moniker_comparison_data(pmkObjectName,
362 &rot_entry->moniker_data);
363 if (hr != S_OK)
365 rot_entry_delete(rot_entry);
366 IMoniker_Release(pmkObjectName);
367 return hr;
370 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
371 if (hr != S_OK)
373 rot_entry_delete(rot_entry);
374 IMoniker_Release(pmkObjectName);
375 return hr;
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 */
382 if (hr == S_OK)
384 HGLOBAL hglobal;
385 hr = GetHGlobalFromStream(pStream, &hglobal);
386 if (hr == S_OK)
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);
398 if (hr != S_OK)
400 HeapFree(GetProcessHeap(), 0, moniker);
401 rot_entry_delete(rot_entry);
402 return hr;
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);
409 if (FAILED(hr))
411 rot_entry_delete(rot_entry);
412 return hr;
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);
422 return hr;
425 /***********************************************************************
426 * RunningObjectTable_Revoke
428 * PARAMS
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);
448 return S_OK;
451 LeaveCriticalSection(&This->lock);
453 return E_INVALIDARG;
456 /***********************************************************************
457 * RunningObjectTable_IsRunning
459 * PARAMS
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;
467 HRESULT hr;
468 const struct rot_entry *rot_entry;
470 TRACE("(%p,%p)\n",This,pmkObjectName);
472 hr = reduce_moniker(pmkObjectName, NULL, &pmkObjectName);
473 if (FAILED(hr))
474 return hr;
475 hr = get_moniker_comparison_data(pmkObjectName, &moniker_data);
476 IMoniker_Release(pmkObjectName);
477 if (hr != S_OK)
478 return hr;
480 hr = S_FALSE;
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))
487 hr = S_OK;
488 break;
491 LeaveCriticalSection(&This->lock);
493 if (hr == S_FALSE)
494 hr = InternalIrotIsRunning(moniker_data);
496 HeapFree(GetProcessHeap(), 0, moniker_data);
498 return hr;
501 /***********************************************************************
502 * RunningObjectTable_GetObject
504 * PARAMS
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;
515 IrotCookie cookie;
516 HRESULT hr;
517 struct rot_entry *rot_entry;
519 TRACE("(%p,%p,%p)\n",This,pmkObjectName,ppunkObject);
521 if (ppunkObject == NULL)
522 return E_POINTER;
524 *ppunkObject = NULL;
526 hr = reduce_moniker(pmkObjectName, NULL, &pmkObjectName);
527 if (FAILED(hr))
528 return hr;
529 hr = get_moniker_comparison_data(pmkObjectName, &moniker_data);
530 IMoniker_Release(pmkObjectName);
531 if (hr != S_OK)
532 return hr;
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))
540 IStream *pStream;
541 hr = create_stream_on_mip_ro(rot_entry->object, &pStream);
542 if (hr == S_OK)
544 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)ppunkObject);
545 IStream_Release(pStream);
548 LeaveCriticalSection(&This->lock);
549 HeapFree(GetProcessHeap(), 0, moniker_data);
551 return hr;
554 LeaveCriticalSection(&This->lock);
556 TRACE("moniker unavailable locally, calling SCM\n");
558 hr = InternalIrotGetObject(moniker_data, &object, &cookie);
559 if (SUCCEEDED(hr))
561 IStream *pStream;
562 hr = create_stream_on_mip_ro(object, &pStream);
563 if (hr == S_OK)
565 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)ppunkObject);
566 IStream_Release(pStream);
569 else
570 WARN("Moniker unavailable, IrotGetObject returned 0x%08x\n", hr);
572 HeapFree(GetProcessHeap(), 0, moniker_data);
574 return hr;
577 /***********************************************************************
578 * RunningObjectTable_NoteChangeTime
580 * PARAMS
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);
604 goto done;
607 LeaveCriticalSection(&This->lock);
609 done:
610 TRACE("-- 0x08%x\n", hr);
611 return hr;
614 /***********************************************************************
615 * RunningObjectTable_GetTimeOfLastChange
617 * PARAMS
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)
633 return E_INVALIDARG;
635 hr = reduce_moniker(pmkObjectName, NULL, &pmkObjectName);
636 if (FAILED(hr))
637 return hr;
638 hr = get_moniker_comparison_data(pmkObjectName, &moniker_data);
639 IMoniker_Release(pmkObjectName);
640 if (hr != S_OK)
641 return hr;
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;
652 hr = S_OK;
653 break;
656 LeaveCriticalSection(&This->lock);
658 if (hr != S_OK)
659 hr = InternalIrotGetTimeOfLastChange(moniker_data, pfiletime);
661 HeapFree(GetProcessHeap(), 0, moniker_data);
663 TRACE("-- 0x%08x\n", hr);
664 return hr;
667 /***********************************************************************
668 * RunningObjectTable_EnumRunning
670 * PARAMS
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;
679 HRESULT hr;
681 TRACE("(%p, %p)\n", This, ppenumMoniker);
683 *ppenumMoniker = NULL;
685 hr = InternalIrotEnumRunning(&interface_list);
686 if (SUCCEEDED(hr))
687 hr = EnumMonikerImpl_CreateEnumROTMoniker(interface_list, 0, ppenumMoniker);
689 return hr;
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);
721 if (reserved!=0)
722 return E_UNEXPECTED;
724 if (!InternalIsProcessInitialized())
725 return CO_E_NOTINITIALIZED;
727 *ret = &rot.IRunningObjectTable_iface;
728 IRunningObjectTable_AddRef(*ret);
730 return S_OK;
733 /***********************************************************************
734 * DestroyRunningObjectTable (ole32.@)
736 void WINAPI DestroyRunningObjectTable(void)
738 struct rot_entry *rot_entry, *cursor2;
740 TRACE("\n");
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,
753 LPDWORD pchEaten,
754 LPMONIKER *ppmk)
756 CLSID clsid;
757 HRESULT hr;
758 LPWSTR progid;
759 LPCWSTR start = szDisplayName;
760 LPCWSTR end;
761 int len;
762 IMoniker *class_moniker;
764 if (*start == '@')
765 start++;
767 /* find end delimiter */
768 for (end = start; *end; end++)
769 if (*end == ':')
770 break;
772 len = end - start;
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))
777 return MK_E_SYNTAX;
779 progid = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
780 if (progid)
782 memcpy(progid, start, len * sizeof(WCHAR));
783 progid[len] = '\0';
785 hr = CLSIDFromProgID(progid, &clsid);
786 HeapFree(GetProcessHeap(), 0, progid);
787 if (FAILED(hr))
788 return MK_E_SYNTAX;
790 hr = CreateClassMoniker(&clsid, &class_moniker);
791 if (SUCCEEDED(hr))
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 */
798 if (FAILED(hr))
800 IClassFactory *pcf;
801 hr = IMoniker_BindToObject(class_moniker, pbc, NULL,
802 &IID_IClassFactory, (void **)&pcf);
803 if (SUCCEEDED(hr))
805 hr = IClassFactory_CreateInstance(pcf, NULL,
806 &IID_IParseDisplayName,
807 (void **)&pdn);
808 IClassFactory_Release(pcf);
811 IMoniker_Release(class_moniker);
812 if (SUCCEEDED(hr))
814 hr = IParseDisplayName_ParseDisplayName(pdn, pbc,
815 (LPOLESTR)szDisplayName,
816 pchEaten, ppmk);
817 IParseDisplayName_Release(pdn);
820 return hr;
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',':'};
831 IMoniker *moniker;
832 DWORD chEaten;
834 TRACE("(%p, %s, %p, %p)\n", pbc, debugstr_w(szDisplayName), pchEaten, ppmk);
836 if (!pbc || !IsValidInterface((LPUNKNOWN) pbc))
837 return E_INVALIDARG;
839 if (!szDisplayName || !*szDisplayName)
840 return E_INVALIDARG;
842 if (!pchEaten || !ppmk)
843 return E_INVALIDARG;
845 *pchEaten = 0;
846 *ppmk = NULL;
848 if (!wcsnicmp(szDisplayName, wszClsidColon, ARRAY_SIZE(wszClsidColon)))
850 hr = ClassMoniker_CreateFromDisplayName(pbc, szDisplayName, &chEaten, &moniker);
851 if (FAILED(hr) && (hr != MK_E_SYNTAX))
852 return hr;
854 else
856 hr = get_moniker_for_progid_display_name(pbc, szDisplayName, &chEaten, &moniker);
857 if (FAILED(hr) && (hr != MK_E_SYNTAX))
858 return hr;
861 if (FAILED(hr))
863 hr = FileMoniker_CreateFromDisplayName(pbc, szDisplayName, &chEaten, &moniker);
864 if (FAILED(hr) && (hr != MK_E_SYNTAX))
865 return hr;
868 if (SUCCEEDED(hr))
870 while (TRUE)
872 IMoniker *next_moniker;
873 *pchEaten += chEaten;
874 szDisplayName += chEaten;
875 if (!*szDisplayName)
877 *ppmk = moniker;
878 return S_OK;
880 chEaten = 0;
881 hr = IMoniker_ParseDisplayName(moniker, pbc, NULL,
882 (LPOLESTR)szDisplayName, &chEaten,
883 &next_moniker);
884 IMoniker_Release(moniker);
885 if (FAILED(hr))
887 *pchEaten = 0;
888 break;
890 moniker = next_moniker;
894 return hr;
897 /***********************************************************************
898 * GetClassFile (OLE32.@)
900 * Retrieves the class ID associated with the given filename.
902 * PARAMS
903 * filePathName [I] Filename to retrieve the class ID for.
904 * pclsid [O] Address that receives the class ID for the file.
906 * RETURNS
907 * Success: S_OK.
908 * Failure: Any HRESULT code.
910 HRESULT WINAPI GetClassFile(LPCOLESTR filePathName,CLSID *pclsid)
912 IStorage *pstg=0;
913 HRESULT res;
914 int nbElm, length, i;
915 LONG sizeProgId, ret;
916 LPOLESTR *pathDec=0,absFile=0,progId=0;
917 LPWSTR extension;
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);
931 return res;
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
935 this case.
937 for(i=0;i<nFileTypes;i++)
939 for(i=0;j<nPatternsForType;j++){
941 PATTERN pat;
942 HANDLE hFile;
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);
951 return S_OK;
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 */
969 extension = NULL;
970 length=lstrlenW(absFile);
971 for(i = length-1; (i >= 0) && *(extension = &absFile[i]) != '.'; i--)
972 /* nothing */;
974 if (!extension || !wcscmp(extension, L".")) {
975 CoTaskMemFree(pathDec);
976 return MK_E_INVALIDEXTENSION;
979 ret = RegQueryValueW(HKEY_CLASSES_ROOT, extension, NULL, &sizeProgId);
980 if (!ret) {
981 /* get the progId associated to the extension */
982 progId = CoTaskMemAlloc(sizeProgId);
983 ret = RegQueryValueW(HKEY_CLASSES_ROOT, extension, progId, &sizeProgId);
984 if (!ret)
985 /* return the clsid associated to the progId */
986 res = CLSIDFromProgID(progId, pclsid);
987 else
988 res = HRESULT_FROM_WIN32(ret);
989 CoTaskMemFree(progId);
991 else
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;
1014 *ppvObject = NULL;
1016 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IEnumMoniker, riid))
1017 *ppvObject = &This->IEnumMoniker_iface;
1018 else
1019 return E_NOINTERFACE;
1021 IEnumMoniker_AddRef(iface);
1022 return S_OK;
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);
1043 ULONG ref;
1045 TRACE("(%p)\n",This);
1047 ref = InterlockedDecrement(&This->ref);
1049 /* uninitialize ROT structure if there are no more references to it */
1050 if (ref == 0)
1052 ULONG i;
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);
1062 return ref;
1064 /***********************************************************************
1065 * EnumMoniker_Next
1067 static HRESULT WINAPI EnumMonikerImpl_Next(IEnumMoniker* iface, ULONG celt, IMoniker** rgelt, ULONG * pceltFetched)
1069 ULONG i;
1070 EnumMonikerImpl *This = impl_from_IEnumMoniker(iface);
1071 HRESULT hr = S_OK;
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++)
1078 IStream *stream;
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)
1087 *pceltFetched= i;
1089 if (hr != S_OK)
1090 return hr;
1092 if (i == celt)
1093 return S_OK;
1094 else
1095 return S_FALSE;
1099 /***********************************************************************
1100 * EnumMoniker_Skip
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)
1109 return S_FALSE;
1111 This->pos += celt;
1113 return S_OK;
1116 /***********************************************************************
1117 * EnumMoniker_Reset
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);
1127 return S_OK;
1130 /***********************************************************************
1131 * EnumMoniker_Clone
1133 static HRESULT WINAPI EnumMonikerImpl_Clone(IEnumMoniker* iface, IEnumMoniker ** ppenum)
1135 EnumMonikerImpl *This = impl_from_IEnumMoniker(iface);
1136 InterfaceList *moniker_list;
1137 ULONG i;
1139 TRACE("(%p)\n",This);
1141 *ppenum = NULL;
1143 moniker_list = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(InterfaceList, interfaces[This->moniker_list->size]));
1144 if (!moniker_list)
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])
1154 ULONG end = 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,
1185 ULONG current_pos,
1186 IEnumMoniker **ppenumMoniker)
1188 EnumMonikerImpl* This = NULL;
1190 if (!ppenumMoniker)
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;
1208 return S_OK;
1212 /* Shared implementation of moniker marshaler based on saving and loading of
1213 * monikers */
1215 typedef struct MonikerMarshal
1217 IUnknown IUnknown_iface;
1218 IMarshal IMarshal_iface;
1220 LONG ref;
1221 IMoniker *moniker;
1222 } MonikerMarshal;
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);
1238 *ppv = NULL;
1239 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IMarshal, riid))
1241 *ppv = &This->IMarshal_iface;
1242 IMarshal_AddRef(&This->IMarshal_iface);
1243 return S_OK;
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);
1261 return ref;
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);
1306 HRESULT hr;
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);
1313 if (hr == S_OK)
1314 *pSize = (DWORD)size.QuadPart;
1315 return hr;
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);
1333 HRESULT hr;
1335 TRACE("(%p, %s, %p)\n", pStm, debugstr_guid(riid), ppv);
1337 hr = IMoniker_Load(This->moniker, pStm);
1338 if (hr == S_OK)
1339 hr = IMoniker_QueryInterface(This->moniker, riid, ppv);
1340 return hr;
1343 static HRESULT WINAPI MonikerMarshal_ReleaseMarshalData(LPMARSHAL iface, IStream *pStm)
1345 TRACE("()\n");
1346 /* can't release a state-based marshal as nothing on server side to
1347 * release */
1348 return S_OK;
1351 static HRESULT WINAPI MonikerMarshal_DisconnectObject(LPMARSHAL iface, DWORD dwReserved)
1353 TRACE("()\n");
1354 /* can't disconnect a state-based marshal as nothing on server side to
1355 * disconnect from */
1356 return S_OK;
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;
1379 This->ref = 1;
1380 This->moniker = inner;
1382 *outer = &This->IUnknown_iface;
1383 return S_OK;
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);