4 * Copyright 1998 Marcus Meissner
5 * Copyright 1999 Noomen Hamza
6 * Copyright 2005 Robert Shearman (for CodeWeavers)
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * - IRunningObjectTable should work interprocess, but currently doesn't.
24 * Native (on Win2k at least) uses an undocumented RPC interface, IROT, to
25 * communicate with RPCSS which contains the table of marshalled data.
41 #include "wine/list.h"
42 #include "wine/debug.h"
43 #include "wine/unicode.h"
45 #include "compobj_private.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(ole);
49 /* see MSDN docs for IROTData::GetComparisonData, which states what this
50 * constant is (http://msdn2.microsoft.com/en-us/library/ms693773.aspx) */
51 #define MAX_COMPARISON_DATA 2048
53 /* define the structure of the running object table elements */
57 MInterfacePointer* object; /* marshaled running object*/
58 MInterfacePointer* moniker; /* marshaled moniker that identifies this object */
59 MInterfacePointer* moniker_data; /* moniker comparison data that identifies this object */
60 DWORD cookie; /* cookie identifying this object */
61 FILETIME last_modified;
64 /* define the RunningObjectTableImpl structure */
65 typedef struct RunningObjectTableImpl
67 const IRunningObjectTableVtbl *lpVtbl;
70 struct list rot; /* list of ROT entries */
71 CRITICAL_SECTION lock;
72 } RunningObjectTableImpl;
74 static RunningObjectTableImpl* runningObjectTableInstance = NULL;
78 static inline HRESULT WINAPI
79 IrotRegister(DWORD *cookie)
81 static LONG last_cookie = 1;
82 *cookie = InterlockedIncrement(&last_cookie);
86 /* define the EnumMonikerImpl structure */
87 typedef struct EnumMonikerImpl
89 const IEnumMonikerVtbl *lpVtbl;
92 MInterfacePointer **monikers;
98 /* IEnumMoniker Local functions*/
99 static HRESULT WINAPI EnumMonikerImpl_CreateEnumROTMoniker(MInterfacePointer **monikers,
100 ULONG moniker_count, ULONG pos, IEnumMoniker **ppenumMoniker);
102 static HRESULT create_stream_on_mip_ro(const MInterfacePointer *mip, IStream **stream)
104 HGLOBAL hglobal = GlobalAlloc(0, mip->ulCntData);
105 void *pv = GlobalLock(hglobal);
106 memcpy(pv, mip->abData, mip->ulCntData);
107 GlobalUnlock(hglobal);
108 return CreateStreamOnHGlobal(hglobal, TRUE, stream);
111 static inline void rot_entry_delete(struct rot_entry *rot_entry)
113 /* FIXME: revoke entry from rpcss's copy of the ROT */
114 if (rot_entry->object)
118 hr = create_stream_on_mip_ro(rot_entry->object, &stream);
121 CoReleaseMarshalData(stream);
122 IUnknown_Release(stream);
125 if (rot_entry->moniker)
129 hr = create_stream_on_mip_ro(rot_entry->moniker, &stream);
132 CoReleaseMarshalData(stream);
133 IUnknown_Release(stream);
136 HeapFree(GetProcessHeap(), 0, rot_entry->object);
137 HeapFree(GetProcessHeap(), 0, rot_entry->moniker);
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, MInterfacePointer **moniker_data)
146 IROTData *pROTData = NULL;
147 hr = IMoniker_QueryInterface(pMoniker, &IID_IROTData, (void *)&pROTData);
150 ULONG size = MAX_COMPARISON_DATA;
151 *moniker_data = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(MInterfacePointer, abData[size]));
152 hr = IROTData_GetComparisonData(pROTData, (*moniker_data)->abData, size, &size);
155 ERR("Failed to copy comparison data into buffer, hr = 0x%08x\n", hr);
156 HeapFree(GetProcessHeap(), 0, *moniker_data);
159 (*moniker_data)->ulCntData = size;
164 LPOLESTR pszDisplayName;
168 TRACE("generating comparison data from display name\n");
170 hr = CreateBindCtx(0, &pbc);
173 hr = IMoniker_GetDisplayName(pMoniker, pbc, NULL, &pszDisplayName);
174 IBindCtx_Release(pbc);
177 hr = IMoniker_GetClassID(pMoniker, &clsid);
180 CoTaskMemFree(pszDisplayName);
184 len = strlenW(pszDisplayName);
185 *moniker_data = HeapAlloc(GetProcessHeap(), 0,
186 FIELD_OFFSET(MInterfacePointer, abData[sizeof(CLSID) + (len+1)*sizeof(WCHAR)]));
189 CoTaskMemFree(pszDisplayName);
190 return E_OUTOFMEMORY;
192 (*moniker_data)->ulCntData = sizeof(CLSID) + (len+1)*sizeof(WCHAR);
194 memcpy(&(*moniker_data)->abData[0], &clsid, sizeof(clsid));
195 memcpy(&(*moniker_data)->abData[sizeof(clsid)], pszDisplayName, (len+1)*sizeof(WCHAR));
200 static HRESULT reduce_moniker(IMoniker *pmk, IBindCtx *pbc, IMoniker **pmkReduced)
202 IBindCtx *pbcNew = NULL;
206 hr = CreateBindCtx(0, &pbcNew);
211 hr = IMoniker_Reduce(pmk, pbc, MKRREDUCE_ALL, NULL, pmkReduced);
213 ERR("reducing moniker failed with error 0x%08x\n", hr);
214 if (pbcNew) IBindCtx_Release(pbcNew);
218 /***********************************************************************
219 * RunningObjectTable_QueryInterface
221 static HRESULT WINAPI
222 RunningObjectTableImpl_QueryInterface(IRunningObjectTable* iface,
223 REFIID riid,void** ppvObject)
225 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
227 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
229 /* validate arguments */
236 if (IsEqualIID(&IID_IUnknown, riid) ||
237 IsEqualIID(&IID_IRunningObjectTable, riid))
238 *ppvObject = (IRunningObjectTable*)This;
241 return E_NOINTERFACE;
243 IRunningObjectTable_AddRef(iface);
248 /***********************************************************************
249 * RunningObjectTable_AddRef
252 RunningObjectTableImpl_AddRef(IRunningObjectTable* iface)
254 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
256 TRACE("(%p)\n",This);
258 return InterlockedIncrement(&This->ref);
261 /***********************************************************************
262 * RunningObjectTable_Initialize
264 static HRESULT WINAPI
265 RunningObjectTableImpl_Destroy(void)
267 struct list *cursor, *cursor2;
271 if (runningObjectTableInstance==NULL)
274 /* free the ROT table memory */
275 LIST_FOR_EACH_SAFE(cursor, cursor2, &runningObjectTableInstance->rot)
277 struct rot_entry *rot_entry = LIST_ENTRY(cursor, struct rot_entry, entry);
278 list_remove(&rot_entry->entry);
279 rot_entry_delete(rot_entry);
282 DEBUG_CLEAR_CRITSEC_NAME(&runningObjectTableInstance->lock);
283 DeleteCriticalSection(&runningObjectTableInstance->lock);
285 /* free the ROT structure memory */
286 HeapFree(GetProcessHeap(),0,runningObjectTableInstance);
287 runningObjectTableInstance = NULL;
292 /***********************************************************************
293 * RunningObjectTable_Release
296 RunningObjectTableImpl_Release(IRunningObjectTable* iface)
298 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
301 TRACE("(%p)\n",This);
303 ref = InterlockedDecrement(&This->ref);
305 /* uninitialize ROT structure if there's no more references to it */
308 struct list *cursor, *cursor2;
309 LIST_FOR_EACH_SAFE(cursor, cursor2, &This->rot)
311 struct rot_entry *rot_entry = LIST_ENTRY(cursor, struct rot_entry, entry);
312 list_remove(&rot_entry->entry);
313 rot_entry_delete(rot_entry);
315 /* RunningObjectTable data structure will be not destroyed here ! the destruction will be done only
316 * when RunningObjectTableImpl_UnInitialize function is called
323 /***********************************************************************
324 * RunningObjectTable_Register
327 * grfFlags [in] Registration options
328 * punkObject [in] the object being registered
329 * pmkObjectName [in] the moniker of the object being registered
330 * pdwRegister [in] the value identifying the registration
332 static HRESULT WINAPI
333 RunningObjectTableImpl_Register(IRunningObjectTable* iface, DWORD grfFlags,
334 IUnknown *punkObject, IMoniker *pmkObjectName, DWORD *pdwRegister)
336 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
337 struct rot_entry *rot_entry;
339 IStream *pStream = NULL;
343 TRACE("(%p,%d,%p,%p,%p)\n",This,grfFlags,punkObject,pmkObjectName,pdwRegister);
345 if (grfFlags & ~(ROTFLAGS_REGISTRATIONKEEPSALIVE|ROTFLAGS_ALLOWANYCLIENT))
347 ERR("Invalid grfFlags: 0x%08x\n", grfFlags & ~(ROTFLAGS_REGISTRATIONKEEPSALIVE|ROTFLAGS_ALLOWANYCLIENT));
351 if (punkObject==NULL || pmkObjectName==NULL || pdwRegister==NULL)
354 rot_entry = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*rot_entry));
356 return E_OUTOFMEMORY;
359 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
362 rot_entry_delete(rot_entry);
365 mshlflags = (grfFlags & ROTFLAGS_REGISTRATIONKEEPSALIVE) ? MSHLFLAGS_TABLESTRONG : MSHLFLAGS_TABLEWEAK;
366 hr = CoMarshalInterface(pStream, &IID_IUnknown, punkObject, MSHCTX_LOCAL | MSHCTX_NOSHAREDMEM, NULL, mshlflags);
367 /* FIXME: a cleaner way would be to create an IStream class that writes
368 * directly to an MInterfacePointer */
372 hr = GetHGlobalFromStream(pStream, &hglobal);
375 SIZE_T size = GlobalSize(hglobal);
376 const void *pv = GlobalLock(hglobal);
377 rot_entry->object = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(MInterfacePointer, abData[size]));
378 rot_entry->object->ulCntData = size;
379 memcpy(&rot_entry->object->abData, pv, size);
380 GlobalUnlock(hglobal);
383 IStream_Release(pStream);
386 rot_entry_delete(rot_entry);
390 hr = CreateBindCtx(0, &pbc);
393 rot_entry_delete(rot_entry);
397 hr = reduce_moniker(pmkObjectName, pbc, &pmkObjectName);
400 rot_entry_delete(rot_entry);
401 IBindCtx_Release(pbc);
405 hr = IMoniker_GetTimeOfLastChange(pmkObjectName, pbc, NULL,
406 &rot_entry->last_modified);
407 IBindCtx_Release(pbc);
410 CoFileTimeNow(&rot_entry->last_modified);
414 hr = get_moniker_comparison_data(pmkObjectName,
415 &rot_entry->moniker_data);
418 rot_entry_delete(rot_entry);
419 IMoniker_Release(pmkObjectName);
423 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
426 rot_entry_delete(rot_entry);
427 IMoniker_Release(pmkObjectName);
430 /* marshal moniker */
431 hr = CoMarshalInterface(pStream, &IID_IMoniker, (IUnknown *)pmkObjectName,
432 MSHCTX_LOCAL | MSHCTX_NOSHAREDMEM, NULL, MSHLFLAGS_TABLESTRONG);
433 /* FIXME: a cleaner way would be to create an IStream class that writes
434 * directly to an MInterfacePointer */
438 hr = GetHGlobalFromStream(pStream, &hglobal);
441 SIZE_T size = GlobalSize(hglobal);
442 const void *pv = GlobalLock(hglobal);
443 rot_entry->moniker = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(MInterfacePointer, abData[size]));
444 rot_entry->moniker->ulCntData = size;
445 memcpy(&rot_entry->moniker->abData, pv, size);
446 GlobalUnlock(hglobal);
449 IStream_Release(pStream);
450 IMoniker_Release(pmkObjectName);
453 rot_entry_delete(rot_entry);
457 /* FIXME: not the right signature of IrotRegister function */
458 hr = IrotRegister(&rot_entry->cookie);
461 rot_entry_delete(rot_entry);
465 /* gives a registration identifier to the registered object*/
466 *pdwRegister = rot_entry->cookie;
468 EnterCriticalSection(&This->lock);
469 /* FIXME: see if object was registered before and return MK_S_MONIKERALREADYREGISTERED */
470 list_add_tail(&This->rot, &rot_entry->entry);
471 LeaveCriticalSection(&This->lock);
476 /***********************************************************************
477 * RunningObjectTable_Revoke
480 * dwRegister [in] Value identifying registration to be revoked
482 static HRESULT WINAPI
483 RunningObjectTableImpl_Revoke( IRunningObjectTable* iface, DWORD dwRegister)
485 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
486 struct rot_entry *rot_entry;
488 TRACE("(%p,%d)\n",This,dwRegister);
490 EnterCriticalSection(&This->lock);
491 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, struct rot_entry, entry)
493 if (rot_entry->cookie == dwRegister)
495 list_remove(&rot_entry->entry);
496 LeaveCriticalSection(&This->lock);
498 rot_entry_delete(rot_entry);
502 LeaveCriticalSection(&This->lock);
507 /***********************************************************************
508 * RunningObjectTable_IsRunning
511 * pmkObjectName [in] moniker of the object whose status is desired
513 static HRESULT WINAPI
514 RunningObjectTableImpl_IsRunning( IRunningObjectTable* iface, IMoniker *pmkObjectName)
516 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
517 MInterfacePointer *moniker_data;
519 struct rot_entry *rot_entry;
521 TRACE("(%p,%p)\n",This,pmkObjectName);
523 hr = reduce_moniker(pmkObjectName, NULL, &pmkObjectName);
526 hr = get_moniker_comparison_data(pmkObjectName, &moniker_data);
527 IMoniker_Release(pmkObjectName);
532 EnterCriticalSection(&This->lock);
533 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, struct rot_entry, entry)
535 if ((rot_entry->moniker_data->ulCntData == moniker_data->ulCntData) &&
536 !memcmp(&moniker_data->abData, &rot_entry->moniker_data->abData, moniker_data->ulCntData))
542 LeaveCriticalSection(&This->lock);
544 /* FIXME: call IrotIsRunning */
546 HeapFree(GetProcessHeap(), 0, moniker_data);
551 /***********************************************************************
552 * RunningObjectTable_GetObject
555 * pmkObjectName [in] Pointer to the moniker on the object
556 * ppunkObject [out] variable that receives the IUnknown interface pointer
558 static HRESULT WINAPI
559 RunningObjectTableImpl_GetObject( IRunningObjectTable* iface,
560 IMoniker *pmkObjectName, IUnknown **ppunkObject)
562 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
563 MInterfacePointer *moniker_data;
565 struct rot_entry *rot_entry;
567 TRACE("(%p,%p,%p)\n",This,pmkObjectName,ppunkObject);
569 if (ppunkObject == NULL)
574 hr = reduce_moniker(pmkObjectName, NULL, &pmkObjectName);
577 hr = get_moniker_comparison_data(pmkObjectName, &moniker_data);
578 IMoniker_Release(pmkObjectName);
582 EnterCriticalSection(&This->lock);
583 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, struct rot_entry, entry)
585 if ((rot_entry->moniker_data->ulCntData == moniker_data->ulCntData) &&
586 !memcmp(&moniker_data->abData, &rot_entry->moniker_data->abData, moniker_data->ulCntData))
589 hr = create_stream_on_mip_ro(rot_entry->object, &pStream);
592 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)ppunkObject);
593 IStream_Release(pStream);
596 LeaveCriticalSection(&This->lock);
597 HeapFree(GetProcessHeap(), 0, moniker_data);
602 LeaveCriticalSection(&This->lock);
604 /* FIXME: call IrotGetObject */
605 WARN("Moniker unavailable - app may require interprocess running object table\n");
606 hr = MK_E_UNAVAILABLE;
608 HeapFree(GetProcessHeap(), 0, moniker_data);
613 /***********************************************************************
614 * RunningObjectTable_NoteChangeTime
617 * dwRegister [in] Value identifying registration being updated
618 * pfiletime [in] Pointer to structure containing object's last change time
620 static HRESULT WINAPI
621 RunningObjectTableImpl_NoteChangeTime(IRunningObjectTable* iface,
622 DWORD dwRegister, FILETIME *pfiletime)
624 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
625 struct rot_entry *rot_entry;
627 TRACE("(%p,%d,%p)\n",This,dwRegister,pfiletime);
629 EnterCriticalSection(&This->lock);
630 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, struct rot_entry, entry)
632 if (rot_entry->cookie == dwRegister)
634 rot_entry->last_modified = *pfiletime;
635 LeaveCriticalSection(&This->lock);
639 LeaveCriticalSection(&This->lock);
641 /* FIXME: call IrotNoteChangeTime */
646 /***********************************************************************
647 * RunningObjectTable_GetTimeOfLastChange
650 * pmkObjectName [in] moniker of the object whose status is desired
651 * pfiletime [out] structure that receives object's last change time
653 static HRESULT WINAPI
654 RunningObjectTableImpl_GetTimeOfLastChange(IRunningObjectTable* iface,
655 IMoniker *pmkObjectName, FILETIME *pfiletime)
657 HRESULT hr = MK_E_UNAVAILABLE;
658 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
659 MInterfacePointer *moniker_data;
660 struct rot_entry *rot_entry;
662 TRACE("(%p,%p,%p)\n",This,pmkObjectName,pfiletime);
664 if (pmkObjectName==NULL || pfiletime==NULL)
667 hr = reduce_moniker(pmkObjectName, NULL, &pmkObjectName);
670 hr = get_moniker_comparison_data(pmkObjectName, &moniker_data);
671 IMoniker_Release(pmkObjectName);
675 hr = MK_E_UNAVAILABLE;
677 EnterCriticalSection(&This->lock);
678 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, struct rot_entry, entry)
680 if ((rot_entry->moniker_data->ulCntData == moniker_data->ulCntData) &&
681 !memcmp(&moniker_data->abData, &rot_entry->moniker_data->abData, moniker_data->ulCntData))
683 *pfiletime = rot_entry->last_modified;
688 LeaveCriticalSection(&This->lock);
690 /* FIXME: if (hr != S_OK) call IrotGetTimeOfLastChange */
692 HeapFree(GetProcessHeap(), 0, moniker_data);
696 /***********************************************************************
697 * RunningObjectTable_EnumRunning
700 * ppenumMoniker [out] receives the IEnumMoniker interface pointer
702 static HRESULT WINAPI
703 RunningObjectTableImpl_EnumRunning(IRunningObjectTable* iface,
704 IEnumMoniker **ppenumMoniker)
707 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
708 MInterfacePointer **monikers;
709 ULONG moniker_count = 0;
710 const struct rot_entry *rot_entry;
713 EnterCriticalSection(&This->lock);
715 LIST_FOR_EACH_ENTRY( rot_entry, &This->rot, const struct rot_entry, entry )
718 monikers = HeapAlloc(GetProcessHeap(), 0, moniker_count * sizeof(*monikers));
720 LIST_FOR_EACH_ENTRY( rot_entry, &This->rot, const struct rot_entry, entry )
722 SIZE_T size = FIELD_OFFSET(MInterfacePointer, abData[rot_entry->moniker->ulCntData]);
723 monikers[i] = HeapAlloc(GetProcessHeap(), 0, size);
724 memcpy(monikers[i], rot_entry->moniker, size);
728 LeaveCriticalSection(&This->lock);
730 /* FIXME: call IrotEnumRunning and append data */
732 hr = EnumMonikerImpl_CreateEnumROTMoniker(monikers, moniker_count, 0, ppenumMoniker);
737 /***********************************************************************
738 * GetRunningObjectTable (OLE32.@)
741 GetRunningObjectTable(DWORD reserved, LPRUNNINGOBJECTTABLE *pprot)
743 IID riid=IID_IRunningObjectTable;
751 if(runningObjectTableInstance==NULL)
752 return CO_E_NOTINITIALIZED;
754 res = IRunningObjectTable_QueryInterface((IRunningObjectTable*)runningObjectTableInstance,&riid,(void**)pprot);
759 /******************************************************************************
762 HRESULT WINAPI OleRun(LPUNKNOWN pUnknown)
764 IRunnableObject *runable;
767 TRACE("(%p)\n", pUnknown);
769 hres = IUnknown_QueryInterface(pUnknown, &IID_IRunnableObject, (void**)&runable);
771 return S_OK; /* Appears to return no error. */
773 hres = IRunnableObject_Run(runable, NULL);
774 IRunnableObject_Release(runable);
778 /******************************************************************************
779 * MkParseDisplayName [OLE32.@]
781 HRESULT WINAPI MkParseDisplayName(LPBC pbc, LPCOLESTR szUserName,
782 LPDWORD pchEaten, LPMONIKER *ppmk)
784 FIXME("(%p, %s, %p, %p): stub.\n", pbc, debugstr_w(szUserName), pchEaten, *ppmk);
786 if (!(IsValidInterface((LPUNKNOWN) pbc)))
792 /* Virtual function table for the IRunningObjectTable class. */
793 static const IRunningObjectTableVtbl VT_RunningObjectTableImpl =
795 RunningObjectTableImpl_QueryInterface,
796 RunningObjectTableImpl_AddRef,
797 RunningObjectTableImpl_Release,
798 RunningObjectTableImpl_Register,
799 RunningObjectTableImpl_Revoke,
800 RunningObjectTableImpl_IsRunning,
801 RunningObjectTableImpl_GetObject,
802 RunningObjectTableImpl_NoteChangeTime,
803 RunningObjectTableImpl_GetTimeOfLastChange,
804 RunningObjectTableImpl_EnumRunning
807 /***********************************************************************
808 * RunningObjectTable_Initialize
810 HRESULT WINAPI RunningObjectTableImpl_Initialize(void)
814 /* create the unique instance of the RunningObjectTableImpl structure */
815 runningObjectTableInstance = HeapAlloc(GetProcessHeap(), 0, sizeof(RunningObjectTableImpl));
817 if (!runningObjectTableInstance)
818 return E_OUTOFMEMORY;
820 /* initialize the virtual table function */
821 runningObjectTableInstance->lpVtbl = &VT_RunningObjectTableImpl;
823 /* the initial reference is set to "1" ! because if set to "0" it will be not practis when */
824 /* the ROT referred many times not in the same time (all the objects in the ROT will */
825 /* be removed every time the ROT is removed ) */
826 runningObjectTableInstance->ref = 1;
828 list_init(&runningObjectTableInstance->rot);
829 InitializeCriticalSection(&runningObjectTableInstance->lock);
830 DEBUG_SET_CRITSEC_NAME(&runningObjectTableInstance->lock, "RunningObjectTableImpl.lock");
835 /***********************************************************************
836 * RunningObjectTable_UnInitialize
838 HRESULT WINAPI RunningObjectTableImpl_UnInitialize(void)
842 if (runningObjectTableInstance==NULL)
845 RunningObjectTableImpl_Release((IRunningObjectTable*)runningObjectTableInstance);
847 RunningObjectTableImpl_Destroy();
852 /***********************************************************************
853 * EnumMoniker_QueryInterface
855 static HRESULT WINAPI EnumMonikerImpl_QueryInterface(IEnumMoniker* iface,REFIID riid,void** ppvObject)
857 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
859 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
861 /* validate arguments */
862 if (ppvObject == NULL)
867 if (IsEqualIID(&IID_IUnknown, riid))
868 *ppvObject = (IEnumMoniker*)This;
870 if (IsEqualIID(&IID_IEnumMoniker, riid))
871 *ppvObject = (IEnumMoniker*)This;
873 if ((*ppvObject)==NULL)
874 return E_NOINTERFACE;
876 IEnumMoniker_AddRef(iface);
881 /***********************************************************************
884 static ULONG WINAPI EnumMonikerImpl_AddRef(IEnumMoniker* iface)
886 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
888 TRACE("(%p)\n",This);
890 return InterlockedIncrement(&This->ref);
893 /***********************************************************************
894 * EnumMoniker_release
896 static ULONG WINAPI EnumMonikerImpl_Release(IEnumMoniker* iface)
898 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
901 TRACE("(%p)\n",This);
903 ref = InterlockedDecrement(&This->ref);
905 /* uninitialize rot structure if there's no more reference to it*/
910 TRACE("(%p) Deleting\n",This);
912 for (i = 0; i < This->moniker_count; i++)
913 HeapFree(GetProcessHeap(), 0, This->monikers[i]);
914 HeapFree(GetProcessHeap(), 0, This->monikers);
915 HeapFree(GetProcessHeap(), 0, This);
920 /***********************************************************************
923 static HRESULT WINAPI EnumMonikerImpl_Next(IEnumMoniker* iface, ULONG celt, IMoniker** rgelt, ULONG * pceltFetched)
926 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
929 TRACE("(%p) TabCurrentPos %d Tablastindx %d\n", This, This->pos, This->moniker_count);
931 /* retrieve the requested number of moniker from the current position */
932 for(i = 0; (This->pos < This->moniker_count) && (i < celt); i++)
935 hr = create_stream_on_mip_ro(This->monikers[This->pos++], &stream);
936 if (hr != S_OK) break;
937 hr = CoUnmarshalInterface(stream, &IID_IMoniker, (void **)&rgelt[i]);
938 IStream_Release(stream);
939 if (hr != S_OK) break;
942 if (pceltFetched != NULL)
955 /***********************************************************************
958 static HRESULT WINAPI EnumMonikerImpl_Skip(IEnumMoniker* iface, ULONG celt)
960 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
962 TRACE("(%p)\n",This);
964 if (This->pos + celt >= This->moniker_count)
972 /***********************************************************************
975 static HRESULT WINAPI EnumMonikerImpl_Reset(IEnumMoniker* iface)
977 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
979 This->pos = 0; /* set back to start of list */
981 TRACE("(%p)\n",This);
986 /***********************************************************************
989 static HRESULT WINAPI EnumMonikerImpl_Clone(IEnumMoniker* iface, IEnumMoniker ** ppenum)
991 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
992 MInterfacePointer **monikers = HeapAlloc(GetProcessHeap(), 0, sizeof(*monikers)*This->moniker_count);
995 TRACE("(%p)\n",This);
997 for (i = 0; i < This->moniker_count; i++)
999 SIZE_T size = FIELD_OFFSET(MInterfacePointer, abData[This->monikers[i]->ulCntData]);
1000 monikers[i] = HeapAlloc(GetProcessHeap(), 0, size);
1001 memcpy(monikers[i], This->monikers[i], size);
1004 /* copy the enum structure */
1005 return EnumMonikerImpl_CreateEnumROTMoniker(monikers, This->moniker_count,
1009 /* Virtual function table for the IEnumMoniker class. */
1010 static const IEnumMonikerVtbl VT_EnumMonikerImpl =
1012 EnumMonikerImpl_QueryInterface,
1013 EnumMonikerImpl_AddRef,
1014 EnumMonikerImpl_Release,
1015 EnumMonikerImpl_Next,
1016 EnumMonikerImpl_Skip,
1017 EnumMonikerImpl_Reset,
1018 EnumMonikerImpl_Clone
1021 /***********************************************************************
1022 * EnumMonikerImpl_CreateEnumROTMoniker
1023 * Used by EnumRunning to create the structure and EnumClone
1024 * to copy the structure
1026 static HRESULT WINAPI EnumMonikerImpl_CreateEnumROTMoniker(MInterfacePointer **monikers,
1027 ULONG moniker_count,
1029 IEnumMoniker **ppenumMoniker)
1031 EnumMonikerImpl* This = NULL;
1034 return E_INVALIDARG;
1036 This = HeapAlloc(GetProcessHeap(), 0, sizeof(EnumMonikerImpl));
1037 if (!This) return E_OUTOFMEMORY;
1039 TRACE("(%p)\n", This);
1041 /* initialize the virtual table function */
1042 This->lpVtbl = &VT_EnumMonikerImpl;
1044 /* the initial reference is set to "1" */
1045 This->ref = 1; /* set the ref count to one */
1046 This->pos = current_pos; /* Set the list start posn */
1047 This->moniker_count = moniker_count; /* Need the same size table as ROT */
1048 This->monikers = monikers;
1050 *ppenumMoniker = (IEnumMoniker*)This;
1056 /* Shared implementation of moniker marshaler based on saving and loading of
1059 typedef struct MonikerMarshal
1061 const IUnknownVtbl *lpVtbl;
1062 const IMarshalVtbl *lpVtblMarshal;
1068 static inline MonikerMarshal *impl_from_IMarshal( IMarshal *iface )
1070 return (MonikerMarshal *)((char*)iface - FIELD_OFFSET(MonikerMarshal, lpVtblMarshal));
1073 static HRESULT WINAPI MonikerMarshalInner_QueryInterface(IUnknown *iface, REFIID riid, LPVOID *ppv)
1075 MonikerMarshal *This = (MonikerMarshal *)iface;
1076 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
1078 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IMarshal, riid))
1080 *ppv = &This->lpVtblMarshal;
1081 IUnknown_AddRef((IUnknown *)&This->lpVtblMarshal);
1084 FIXME("No interface for %s\n", debugstr_guid(riid));
1085 return E_NOINTERFACE;
1088 static ULONG WINAPI MonikerMarshalInner_AddRef(IUnknown *iface)
1090 MonikerMarshal *This = (MonikerMarshal *)iface;
1091 return InterlockedIncrement(&This->ref);
1094 static ULONG WINAPI MonikerMarshalInner_Release(IUnknown *iface)
1096 MonikerMarshal *This = (MonikerMarshal *)iface;
1097 ULONG ref = InterlockedDecrement(&This->ref);
1099 if (!ref) HeapFree(GetProcessHeap(), 0, This);
1103 static const IUnknownVtbl VT_MonikerMarshalInner =
1105 MonikerMarshalInner_QueryInterface,
1106 MonikerMarshalInner_AddRef,
1107 MonikerMarshalInner_Release
1110 static HRESULT WINAPI MonikerMarshal_QueryInterface(IMarshal *iface, REFIID riid, LPVOID *ppv)
1112 MonikerMarshal *This = impl_from_IMarshal(iface);
1113 return IMoniker_QueryInterface(This->moniker, riid, ppv);
1116 static ULONG WINAPI MonikerMarshal_AddRef(IMarshal *iface)
1118 MonikerMarshal *This = impl_from_IMarshal(iface);
1119 return IMoniker_AddRef(This->moniker);
1122 static ULONG WINAPI MonikerMarshal_Release(IMarshal *iface)
1124 MonikerMarshal *This = impl_from_IMarshal(iface);
1125 return IMoniker_Release(This->moniker);
1128 static HRESULT WINAPI MonikerMarshal_GetUnmarshalClass(
1129 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
1130 void* pvDestContext, DWORD mshlflags, CLSID* pCid)
1132 MonikerMarshal *This = impl_from_IMarshal(iface);
1134 TRACE("(%s, %p, %x, %p, %x, %p)\n", debugstr_guid(riid), pv,
1135 dwDestContext, pvDestContext, mshlflags, pCid);
1137 return IMoniker_GetClassID(This->moniker, pCid);
1140 static HRESULT WINAPI MonikerMarshal_GetMarshalSizeMax(
1141 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
1142 void* pvDestContext, DWORD mshlflags, DWORD* pSize)
1144 MonikerMarshal *This = impl_from_IMarshal(iface);
1146 ULARGE_INTEGER size;
1148 TRACE("(%s, %p, %x, %p, %x, %p)\n", debugstr_guid(riid), pv,
1149 dwDestContext, pvDestContext, mshlflags, pSize);
1151 hr = IMoniker_GetSizeMax(This->moniker, &size);
1153 *pSize = (DWORD)size.QuadPart;
1157 static HRESULT WINAPI MonikerMarshal_MarshalInterface(LPMARSHAL iface, IStream *pStm,
1158 REFIID riid, void* pv, DWORD dwDestContext,
1159 void* pvDestContext, DWORD mshlflags)
1161 MonikerMarshal *This = impl_from_IMarshal(iface);
1163 TRACE("(%p, %s, %p, %x, %p, %x)\n", pStm, debugstr_guid(riid), pv,
1164 dwDestContext, pvDestContext, mshlflags);
1166 return IMoniker_Save(This->moniker, pStm, FALSE);
1169 static HRESULT WINAPI MonikerMarshal_UnmarshalInterface(LPMARSHAL iface, IStream *pStm, REFIID riid, void **ppv)
1171 MonikerMarshal *This = impl_from_IMarshal(iface);
1174 TRACE("(%p, %s, %p)\n", pStm, debugstr_guid(riid), ppv);
1176 hr = IMoniker_Load(This->moniker, pStm);
1178 hr = IMoniker_QueryInterface(This->moniker, riid, ppv);
1182 static HRESULT WINAPI MonikerMarshal_ReleaseMarshalData(LPMARSHAL iface, IStream *pStm)
1185 /* can't release a state-based marshal as nothing on server side to
1190 static HRESULT WINAPI MonikerMarshal_DisconnectObject(LPMARSHAL iface, DWORD dwReserved)
1193 /* can't disconnect a state-based marshal as nothing on server side to
1194 * disconnect from */
1198 static const IMarshalVtbl VT_MonikerMarshal =
1200 MonikerMarshal_QueryInterface,
1201 MonikerMarshal_AddRef,
1202 MonikerMarshal_Release,
1203 MonikerMarshal_GetUnmarshalClass,
1204 MonikerMarshal_GetMarshalSizeMax,
1205 MonikerMarshal_MarshalInterface,
1206 MonikerMarshal_UnmarshalInterface,
1207 MonikerMarshal_ReleaseMarshalData,
1208 MonikerMarshal_DisconnectObject
1211 HRESULT MonikerMarshal_Create(IMoniker *inner, IUnknown **outer)
1213 MonikerMarshal *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1214 if (!This) return E_OUTOFMEMORY;
1216 This->lpVtbl = &VT_MonikerMarshalInner;
1217 This->lpVtblMarshal = &VT_MonikerMarshal;
1219 This->moniker = inner;
1221 *outer = (IUnknown *)&This->lpVtbl;