IEnum::Clone shouldn't do a Reset.
[wine.git] / dlls / ole32 / moniker.c
blobf83b2fdad4aa7f742a521ea072c576ba37ed4fd5
1 /*
2 * Monikers
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * TODO:
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.
28 #include <assert.h>
29 #include <stdarg.h>
30 #include <string.h>
32 #define COBJMACROS
34 #include "winerror.h"
35 #include "windef.h"
36 #include "winbase.h"
37 #include "winuser.h"
38 #include "wtypes.h"
39 #include "ole2.h"
41 #include "wine/list.h"
42 #include "wine/debug.h"
44 #include "compobj_private.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(ole);
48 /* define the structure of the running object table elements */
49 struct rot_entry
51 struct list entry;
52 MInterfacePointer* object; /* marshaled running object*/
53 MInterfacePointer* moniker; /* marshaled moniker that identifies this object */
54 MInterfacePointer* moniker_data; /* moniker comparison data that identifies this object */
55 DWORD cookie; /* cookie identifying this object */
56 FILETIME last_modified;
59 /* define the RunningObjectTableImpl structure */
60 typedef struct RunningObjectTableImpl
62 const IRunningObjectTableVtbl *lpVtbl;
63 ULONG ref;
65 struct list rot; /* list of ROT entries */
66 CRITICAL_SECTION lock;
67 } RunningObjectTableImpl;
69 static RunningObjectTableImpl* runningObjectTableInstance = NULL;
73 static inline HRESULT WINAPI
74 IrotRegister(DWORD *cookie)
76 static DWORD last_cookie = 1;
77 *cookie = InterlockedIncrement(&last_cookie);
78 return S_OK;
81 /* define the EnumMonikerImpl structure */
82 typedef struct EnumMonikerImpl
84 const IEnumMonikerVtbl *lpVtbl;
85 ULONG ref;
87 MInterfacePointer **monikers;
88 ULONG moniker_count;
89 ULONG pos;
90 } EnumMonikerImpl;
93 /* IEnumMoniker Local functions*/
94 static HRESULT WINAPI EnumMonikerImpl_CreateEnumROTMoniker(MInterfacePointer **monikers,
95 ULONG moniker_count, ULONG pos, IEnumMoniker **ppenumMoniker);
97 static HRESULT create_stream_on_mip_ro(const MInterfacePointer *mip, IStream **stream)
99 HGLOBAL hglobal = GlobalAlloc(0, mip->ulCntData);
100 void *pv = GlobalLock(hglobal);
101 memcpy(pv, mip->abData, mip->ulCntData);
102 GlobalUnlock(hglobal);
103 return CreateStreamOnHGlobal(hglobal, TRUE, stream);
106 static inline void rot_entry_delete(struct rot_entry *rot_entry)
108 /* FIXME: revoke entry from rpcss's copy of the ROT */
109 if (rot_entry->object)
111 IStream *stream;
112 HRESULT hr;
113 hr = create_stream_on_mip_ro(rot_entry->object, &stream);
114 if (hr == S_OK)
116 CoReleaseMarshalData(stream);
117 IUnknown_Release(stream);
120 if (rot_entry->moniker)
122 IStream *stream;
123 HRESULT hr;
124 hr = create_stream_on_mip_ro(rot_entry->moniker, &stream);
125 if (hr == S_OK)
127 CoReleaseMarshalData(stream);
128 IUnknown_Release(stream);
131 HeapFree(GetProcessHeap(), 0, rot_entry->object);
132 HeapFree(GetProcessHeap(), 0, rot_entry->moniker);
133 HeapFree(GetProcessHeap(), 0, rot_entry->moniker_data);
134 HeapFree(GetProcessHeap(), 0, rot_entry);
137 /* moniker_data must be freed with HeapFree when no longer in use */
138 static HRESULT get_moniker_comparison_data(IMoniker *pMoniker, MInterfacePointer **moniker_data)
140 HRESULT hr;
141 IROTData *pROTData = NULL;
142 ULONG size = 0;
143 hr = IMoniker_QueryInterface(pMoniker, &IID_IROTData, (void *)&pROTData);
144 if (hr != S_OK)
146 ERR("Failed to query moniker for IROTData interface, hr = 0x%08lx\n", hr);
147 return hr;
149 IROTData_GetComparisonData(pROTData, NULL, 0, &size);
150 *moniker_data = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(MInterfacePointer, abData[size]));
151 (*moniker_data)->ulCntData = size;
152 hr = IROTData_GetComparisonData(pROTData, (*moniker_data)->abData, size, &size);
153 if (hr != S_OK)
155 ERR("Failed to copy comparison data into buffer, hr = 0x%08lx\n", hr);
156 HeapFree(GetProcessHeap(), 0, *moniker_data);
157 return hr;
159 return S_OK;
162 /***********************************************************************
163 * RunningObjectTable_QueryInterface
165 static HRESULT WINAPI
166 RunningObjectTableImpl_QueryInterface(IRunningObjectTable* iface,
167 REFIID riid,void** ppvObject)
169 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
171 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
173 /* validate arguments */
175 if (ppvObject==0)
176 return E_INVALIDARG;
178 *ppvObject = 0;
180 if (IsEqualIID(&IID_IUnknown, riid) ||
181 IsEqualIID(&IID_IRunningObjectTable, riid))
182 *ppvObject = (IRunningObjectTable*)This;
184 if ((*ppvObject)==0)
185 return E_NOINTERFACE;
187 IRunningObjectTable_AddRef(iface);
189 return S_OK;
192 /***********************************************************************
193 * RunningObjectTable_AddRef
195 static ULONG WINAPI
196 RunningObjectTableImpl_AddRef(IRunningObjectTable* iface)
198 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
200 TRACE("(%p)\n",This);
202 return InterlockedIncrement(&This->ref);
205 /***********************************************************************
206 * RunningObjectTable_Initialize
208 static HRESULT WINAPI
209 RunningObjectTableImpl_Destroy(void)
211 struct list *cursor, *cursor2;
213 TRACE("()\n");
215 if (runningObjectTableInstance==NULL)
216 return E_INVALIDARG;
218 /* free the ROT table memory */
219 LIST_FOR_EACH_SAFE(cursor, cursor2, &runningObjectTableInstance->rot)
221 struct rot_entry *rot_entry = LIST_ENTRY(cursor, struct rot_entry, entry);
222 list_remove(&rot_entry->entry);
223 rot_entry_delete(rot_entry);
226 /* free the ROT structure memory */
227 HeapFree(GetProcessHeap(),0,runningObjectTableInstance);
228 runningObjectTableInstance = NULL;
230 return S_OK;
233 /***********************************************************************
234 * RunningObjectTable_Release
236 static ULONG WINAPI
237 RunningObjectTableImpl_Release(IRunningObjectTable* iface)
239 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
240 ULONG ref;
242 TRACE("(%p)\n",This);
244 ref = InterlockedDecrement(&This->ref);
246 /* uninitialize ROT structure if there's no more references to it */
247 if (ref == 0)
249 struct list *cursor, *cursor2;
250 LIST_FOR_EACH_SAFE(cursor, cursor2, &This->rot)
252 struct rot_entry *rot_entry = LIST_ENTRY(cursor, struct rot_entry, entry);
253 list_remove(&rot_entry->entry);
254 rot_entry_delete(rot_entry);
256 /* RunningObjectTable data structure will be not destroyed here ! the destruction will be done only
257 * when RunningObjectTableImpl_UnInitialize function is called
261 return ref;
264 /***********************************************************************
265 * RunningObjectTable_Register
267 * PARAMS
268 * grfFlags [in] Registration options
269 * punkObject [in] the object being registered
270 * pmkObjectName [in] the moniker of the object being registered
271 * pdwRegister [in] the value identifying the registration
273 static HRESULT WINAPI
274 RunningObjectTableImpl_Register(IRunningObjectTable* iface, DWORD grfFlags,
275 IUnknown *punkObject, IMoniker *pmkObjectName, DWORD *pdwRegister)
277 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
278 struct rot_entry *rot_entry;
279 HRESULT hr = S_OK;
280 IStream *pStream = NULL;
281 DWORD mshlflags;
283 TRACE("(%p,%ld,%p,%p,%p)\n",This,grfFlags,punkObject,pmkObjectName,pdwRegister);
286 * there's only two types of register : strong and or weak registration
287 * (only one must be passed on parameter)
289 if ( ( (grfFlags & ROTFLAGS_REGISTRATIONKEEPSALIVE) || !(grfFlags & ROTFLAGS_ALLOWANYCLIENT)) &&
290 (!(grfFlags & ROTFLAGS_REGISTRATIONKEEPSALIVE) || (grfFlags & ROTFLAGS_ALLOWANYCLIENT)) &&
291 (grfFlags) )
293 ERR("Invalid combination of ROTFLAGS: %lx\n", grfFlags);
294 return E_INVALIDARG;
297 if (punkObject==NULL || pmkObjectName==NULL || pdwRegister==NULL)
298 return E_INVALIDARG;
300 rot_entry = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*rot_entry));
301 if (!rot_entry)
302 return E_OUTOFMEMORY;
304 CoFileTimeNow(&rot_entry->last_modified);
306 /* marshal object */
307 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
308 if (hr != S_OK)
310 rot_entry_delete(rot_entry);
311 return hr;
313 mshlflags = (grfFlags & ROTFLAGS_REGISTRATIONKEEPSALIVE) ? MSHLFLAGS_TABLESTRONG : MSHLFLAGS_TABLEWEAK;
314 hr = CoMarshalInterface(pStream, &IID_IUnknown, punkObject, MSHCTX_LOCAL | MSHCTX_NOSHAREDMEM, NULL, mshlflags);
315 /* FIXME: a cleaner way would be to create an IStream class that writes
316 * directly to an MInterfacePointer */
317 if (hr == S_OK)
319 HGLOBAL hglobal;
320 hr = GetHGlobalFromStream(pStream, &hglobal);
321 if (hr == S_OK)
323 SIZE_T size = GlobalSize(hglobal);
324 const void *pv = GlobalLock(hglobal);
325 rot_entry->object = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(MInterfacePointer, abData[size]));
326 rot_entry->object->ulCntData = size;
327 memcpy(&rot_entry->object->abData, pv, size);
328 GlobalUnlock(hglobal);
331 IStream_Release(pStream);
332 if (hr != S_OK)
334 rot_entry_delete(rot_entry);
335 return hr;
338 hr = get_moniker_comparison_data(pmkObjectName, &rot_entry->moniker_data);
339 if (hr != S_OK)
341 rot_entry_delete(rot_entry);
342 return hr;
345 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
346 if (hr != S_OK)
348 rot_entry_delete(rot_entry);
349 return hr;
351 /* marshal moniker */
352 hr = CoMarshalInterface(pStream, &IID_IMoniker, (IUnknown *)pmkObjectName, MSHCTX_LOCAL | MSHCTX_NOSHAREDMEM, NULL, MSHLFLAGS_TABLESTRONG);
353 /* FIXME: a cleaner way would be to create an IStream class that writes
354 * directly to an MInterfacePointer */
355 if (hr == S_OK)
357 HGLOBAL hglobal;
358 hr = GetHGlobalFromStream(pStream, &hglobal);
359 if (hr == S_OK)
361 SIZE_T size = GlobalSize(hglobal);
362 const void *pv = GlobalLock(hglobal);
363 rot_entry->moniker = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(MInterfacePointer, abData[size]));
364 rot_entry->moniker->ulCntData = size;
365 memcpy(&rot_entry->moniker->abData, pv, size);
366 GlobalUnlock(hglobal);
369 IStream_Release(pStream);
370 if (hr != S_OK)
372 rot_entry_delete(rot_entry);
373 return hr;
376 /* FIXME: not the right signature of IrotRegister function */
377 hr = IrotRegister(&rot_entry->cookie);
378 if (hr != S_OK)
380 rot_entry_delete(rot_entry);
381 return hr;
384 /* gives a registration identifier to the registered object*/
385 *pdwRegister = rot_entry->cookie;
387 EnterCriticalSection(&This->lock);
388 /* FIXME: see if object was registered before and return MK_S_MONIKERALREADYREGISTERED */
389 list_add_tail(&This->rot, &rot_entry->entry);
390 LeaveCriticalSection(&This->lock);
392 return hr;
395 /***********************************************************************
396 * RunningObjectTable_Revoke
398 * PARAMS
399 * dwRegister [in] Value identifying registration to be revoked
401 static HRESULT WINAPI
402 RunningObjectTableImpl_Revoke( IRunningObjectTable* iface, DWORD dwRegister)
404 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
405 struct rot_entry *rot_entry;
407 TRACE("(%p,%ld)\n",This,dwRegister);
409 EnterCriticalSection(&This->lock);
410 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, struct rot_entry, entry)
412 if (rot_entry->cookie == dwRegister)
414 list_remove(&rot_entry->entry);
415 LeaveCriticalSection(&This->lock);
417 rot_entry_delete(rot_entry);
418 return S_OK;
421 LeaveCriticalSection(&This->lock);
423 return E_INVALIDARG;
426 /***********************************************************************
427 * RunningObjectTable_IsRunning
429 * PARAMS
430 * pmkObjectName [in] moniker of the object whose status is desired
432 static HRESULT WINAPI
433 RunningObjectTableImpl_IsRunning( IRunningObjectTable* iface, IMoniker *pmkObjectName)
435 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
436 MInterfacePointer *moniker_data;
437 HRESULT hr;
438 struct rot_entry *rot_entry;
440 TRACE("(%p,%p)\n",This,pmkObjectName);
442 hr = get_moniker_comparison_data(pmkObjectName, &moniker_data);
443 if (hr != S_OK)
444 return hr;
446 hr = S_FALSE;
447 EnterCriticalSection(&This->lock);
448 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, struct rot_entry, entry)
450 if ((rot_entry->moniker_data->ulCntData == moniker_data->ulCntData) &&
451 !memcmp(moniker_data, rot_entry->moniker_data, moniker_data->ulCntData))
453 hr = S_OK;
454 break;
457 LeaveCriticalSection(&This->lock);
459 /* FIXME: call IrotIsRunning */
461 HeapFree(GetProcessHeap(), 0, moniker_data);
463 return hr;
466 /***********************************************************************
467 * RunningObjectTable_GetObject
469 * PARAMS
470 * pmkObjectName [in] Pointer to the moniker on the object
471 * ppunkObject [out] variable that receives the IUnknown interface pointer
473 static HRESULT WINAPI
474 RunningObjectTableImpl_GetObject( IRunningObjectTable* iface,
475 IMoniker *pmkObjectName, IUnknown **ppunkObject)
477 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
478 MInterfacePointer *moniker_data;
479 HRESULT hr;
480 struct rot_entry *rot_entry;
482 TRACE("(%p,%p,%p)\n",This,pmkObjectName,ppunkObject);
484 if (ppunkObject == NULL)
485 return E_POINTER;
487 *ppunkObject = NULL;
489 hr = get_moniker_comparison_data(pmkObjectName, &moniker_data);
490 if (hr != S_OK)
491 return hr;
493 EnterCriticalSection(&This->lock);
494 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, struct rot_entry, entry)
496 if ((rot_entry->moniker_data->ulCntData == moniker_data->ulCntData) &&
497 !memcmp(moniker_data, rot_entry->moniker_data, moniker_data->ulCntData))
499 IStream *pStream;
500 hr = create_stream_on_mip_ro(rot_entry->object, &pStream);
501 if (hr == S_OK)
503 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)ppunkObject);
504 IStream_Release(pStream);
507 LeaveCriticalSection(&This->lock);
508 HeapFree(GetProcessHeap(), 0, moniker_data);
510 return hr;
513 LeaveCriticalSection(&This->lock);
515 /* FIXME: call IrotGetObject */
516 WARN("Moniker unavailable - app may require interprocess running object table\n");
517 hr = MK_E_UNAVAILABLE;
519 HeapFree(GetProcessHeap(), 0, moniker_data);
521 return hr;
524 /***********************************************************************
525 * RunningObjectTable_NoteChangeTime
527 * PARAMS
528 * dwRegister [in] Value identifying registration being updated
529 * pfiletime [in] Pointer to structure containing object's last change time
531 static HRESULT WINAPI
532 RunningObjectTableImpl_NoteChangeTime(IRunningObjectTable* iface,
533 DWORD dwRegister, FILETIME *pfiletime)
535 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
536 struct rot_entry *rot_entry;
538 TRACE("(%p,%ld,%p)\n",This,dwRegister,pfiletime);
540 EnterCriticalSection(&This->lock);
541 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, struct rot_entry, entry)
543 if (rot_entry->cookie == dwRegister)
545 rot_entry->last_modified = *pfiletime;
546 LeaveCriticalSection(&This->lock);
547 return S_OK;
550 LeaveCriticalSection(&This->lock);
552 /* FIXME: call IrotNoteChangeTime */
554 return E_INVALIDARG;
557 /***********************************************************************
558 * RunningObjectTable_GetTimeOfLastChange
560 * PARAMS
561 * pmkObjectName [in] moniker of the object whose status is desired
562 * pfiletime [out] structure that receives object's last change time
564 static HRESULT WINAPI
565 RunningObjectTableImpl_GetTimeOfLastChange(IRunningObjectTable* iface,
566 IMoniker *pmkObjectName, FILETIME *pfiletime)
568 HRESULT hr = MK_E_UNAVAILABLE;
569 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
570 MInterfacePointer *moniker_data;
571 struct rot_entry *rot_entry;
573 TRACE("(%p,%p,%p)\n",This,pmkObjectName,pfiletime);
575 if (pmkObjectName==NULL || pfiletime==NULL)
576 return E_INVALIDARG;
578 hr = get_moniker_comparison_data(pmkObjectName, &moniker_data);
579 if (hr != S_OK)
580 return hr;
582 hr = MK_E_UNAVAILABLE;
584 EnterCriticalSection(&This->lock);
585 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, struct rot_entry, entry)
587 if ((rot_entry->moniker_data->ulCntData == moniker_data->ulCntData) &&
588 !memcmp(moniker_data, rot_entry->moniker_data, moniker_data->ulCntData))
590 *pfiletime = rot_entry->last_modified;
591 hr = S_OK;
592 break;
595 LeaveCriticalSection(&This->lock);
597 /* FIXME: if (hr != S_OK) call IrotGetTimeOfLastChange */
599 HeapFree(GetProcessHeap(), 0, moniker_data);
600 return hr;
603 /***********************************************************************
604 * RunningObjectTable_EnumRunning
606 * PARAMS
607 * ppenumMoniker [out] receives the IEnumMoniker interface pointer
609 static HRESULT WINAPI
610 RunningObjectTableImpl_EnumRunning(IRunningObjectTable* iface,
611 IEnumMoniker **ppenumMoniker)
613 HRESULT hr;
614 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
615 MInterfacePointer **monikers;
616 ULONG moniker_count = 0;
617 const struct rot_entry *rot_entry;
618 ULONG i = 0;
620 EnterCriticalSection(&This->lock);
622 LIST_FOR_EACH_ENTRY( rot_entry, &This->rot, const struct rot_entry, entry )
623 moniker_count++;
625 monikers = HeapAlloc(GetProcessHeap(), 0, moniker_count * sizeof(*monikers));
627 LIST_FOR_EACH_ENTRY( rot_entry, &This->rot, const struct rot_entry, entry )
629 SIZE_T size = FIELD_OFFSET(MInterfacePointer, abData[rot_entry->moniker->ulCntData]);
630 monikers[i] = HeapAlloc(GetProcessHeap(), 0, size);
631 memcpy(monikers[i], rot_entry->moniker, size);
632 i++;
635 LeaveCriticalSection(&This->lock);
637 /* FIXME: call IrotEnumRunning and append data */
639 hr = EnumMonikerImpl_CreateEnumROTMoniker(monikers, moniker_count, 0, ppenumMoniker);
641 return hr;
644 /******************************************************************************
645 * GetRunningObjectTable (OLE2.30)
647 HRESULT WINAPI
648 GetRunningObjectTable16(DWORD reserved, LPRUNNINGOBJECTTABLE *pprot)
650 FIXME("(%ld,%p),stub!\n",reserved,pprot);
651 return E_NOTIMPL;
654 /***********************************************************************
655 * GetRunningObjectTable (OLE32.@)
657 HRESULT WINAPI
658 GetRunningObjectTable(DWORD reserved, LPRUNNINGOBJECTTABLE *pprot)
660 IID riid=IID_IRunningObjectTable;
661 HRESULT res;
663 TRACE("()\n");
665 if (reserved!=0)
666 return E_UNEXPECTED;
668 if(runningObjectTableInstance==NULL)
669 return CO_E_NOTINITIALIZED;
671 res = IRunningObjectTable_QueryInterface((IRunningObjectTable*)runningObjectTableInstance,&riid,(void**)pprot);
673 return res;
676 /******************************************************************************
677 * OleRun [OLE32.@]
679 HRESULT WINAPI OleRun(LPUNKNOWN pUnknown)
681 IRunnableObject *runable;
682 IRunnableObject *This = (IRunnableObject *)pUnknown;
683 LRESULT ret;
685 ret = IRunnableObject_QueryInterface(This,&IID_IRunnableObject,(LPVOID*)&runable);
686 if (ret)
687 return 0; /* Appears to return no error. */
688 ret = IRunnableObject_Run(runable,NULL);
689 IRunnableObject_Release(runable);
690 return ret;
693 /******************************************************************************
694 * MkParseDisplayName [OLE32.@]
696 HRESULT WINAPI MkParseDisplayName(LPBC pbc, LPCOLESTR szUserName,
697 LPDWORD pchEaten, LPMONIKER *ppmk)
699 FIXME("(%p, %s, %p, %p): stub.\n", pbc, debugstr_w(szUserName), pchEaten, *ppmk);
701 if (!(IsValidInterface((LPUNKNOWN) pbc)))
702 return E_INVALIDARG;
704 return MK_E_SYNTAX;
707 /******************************************************************************
708 * CreateClassMoniker [OLE32.@]
710 HRESULT WINAPI CreateClassMoniker(REFCLSID rclsid, IMoniker ** ppmk)
712 FIXME("%s\n", debugstr_guid( rclsid ));
713 if( ppmk )
714 *ppmk = NULL;
715 return E_NOTIMPL;
718 /* Virtual function table for the IRunningObjectTable class. */
719 static IRunningObjectTableVtbl VT_RunningObjectTableImpl =
721 RunningObjectTableImpl_QueryInterface,
722 RunningObjectTableImpl_AddRef,
723 RunningObjectTableImpl_Release,
724 RunningObjectTableImpl_Register,
725 RunningObjectTableImpl_Revoke,
726 RunningObjectTableImpl_IsRunning,
727 RunningObjectTableImpl_GetObject,
728 RunningObjectTableImpl_NoteChangeTime,
729 RunningObjectTableImpl_GetTimeOfLastChange,
730 RunningObjectTableImpl_EnumRunning
733 /***********************************************************************
734 * RunningObjectTable_Initialize
736 HRESULT WINAPI RunningObjectTableImpl_Initialize(void)
738 TRACE("\n");
740 /* create the unique instance of the RunningObjectTableImpl structure */
741 runningObjectTableInstance = HeapAlloc(GetProcessHeap(), 0, sizeof(RunningObjectTableImpl));
743 if (!runningObjectTableInstance)
744 return E_OUTOFMEMORY;
746 /* initialize the virtual table function */
747 runningObjectTableInstance->lpVtbl = &VT_RunningObjectTableImpl;
749 /* the initial reference is set to "1" ! because if set to "0" it will be not practis when */
750 /* the ROT referred many times not in the same time (all the objects in the ROT will */
751 /* be removed every time the ROT is removed ) */
752 runningObjectTableInstance->ref = 1;
754 list_init(&runningObjectTableInstance->rot);
755 InitializeCriticalSection(&runningObjectTableInstance->lock);
757 return S_OK;
760 /***********************************************************************
761 * RunningObjectTable_UnInitialize
763 HRESULT WINAPI RunningObjectTableImpl_UnInitialize()
765 TRACE("\n");
767 if (runningObjectTableInstance==NULL)
768 return E_POINTER;
770 RunningObjectTableImpl_Release((IRunningObjectTable*)runningObjectTableInstance);
772 RunningObjectTableImpl_Destroy();
774 return S_OK;
777 /***********************************************************************
778 * EnumMoniker_QueryInterface
780 static HRESULT WINAPI EnumMonikerImpl_QueryInterface(IEnumMoniker* iface,REFIID riid,void** ppvObject)
782 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
784 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
786 /* validate arguments */
787 if (ppvObject == NULL)
788 return E_INVALIDARG;
790 *ppvObject = NULL;
792 if (IsEqualIID(&IID_IUnknown, riid))
793 *ppvObject = (IEnumMoniker*)This;
794 else
795 if (IsEqualIID(&IID_IEnumMoniker, riid))
796 *ppvObject = (IEnumMoniker*)This;
798 if ((*ppvObject)==NULL)
799 return E_NOINTERFACE;
801 IEnumMoniker_AddRef(iface);
803 return S_OK;
806 /***********************************************************************
807 * EnumMoniker_AddRef
809 static ULONG WINAPI EnumMonikerImpl_AddRef(IEnumMoniker* iface)
811 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
813 TRACE("(%p)\n",This);
815 return InterlockedIncrement(&This->ref);
818 /***********************************************************************
819 * EnumMoniker_release
821 static ULONG WINAPI EnumMonikerImpl_Release(IEnumMoniker* iface)
823 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
824 ULONG ref;
826 TRACE("(%p)\n",This);
828 ref = InterlockedDecrement(&This->ref);
830 /* unitialize rot structure if there's no more reference to it*/
831 if (ref == 0)
833 ULONG i;
835 TRACE("(%p) Deleting\n",This);
837 for (i = 0; i < This->moniker_count; i++)
838 HeapFree(GetProcessHeap(), 0, This->monikers[i]);
839 HeapFree(GetProcessHeap(), 0, This->monikers);
840 HeapFree(GetProcessHeap(), 0, This);
843 return ref;
845 /***********************************************************************
846 * EnmumMoniker_Next
848 static HRESULT WINAPI EnumMonikerImpl_Next(IEnumMoniker* iface, ULONG celt, IMoniker** rgelt, ULONG * pceltFetched)
850 ULONG i;
851 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
852 HRESULT hr = S_OK;
854 TRACE("(%p) TabCurrentPos %ld Tablastindx %ld\n", This, This->pos, This->moniker_count);
856 /* retrieve the requested number of moniker from the current position */
857 for(i = 0; (This->pos < This->moniker_count) && (i < celt); i++)
859 IStream *stream;
860 hr = create_stream_on_mip_ro(This->monikers[This->pos++], &stream);
861 if (hr != S_OK) break;
862 hr = CoUnmarshalInterface(stream, &IID_IMoniker, (void **)&rgelt[i]);
863 IStream_Release(stream);
864 if (hr != S_OK) break;
867 if (pceltFetched != NULL)
868 *pceltFetched= i;
870 if (hr != S_OK)
871 return hr;
873 if (i == celt)
874 return S_OK;
875 else
876 return S_FALSE;
880 /***********************************************************************
881 * EnmumMoniker_Skip
883 static HRESULT WINAPI EnumMonikerImpl_Skip(IEnumMoniker* iface, ULONG celt)
885 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
887 TRACE("(%p)\n",This);
889 if (This->pos + celt >= This->moniker_count)
890 return S_FALSE;
892 This->pos += celt;
894 return S_OK;
897 /***********************************************************************
898 * EnmumMoniker_Reset
900 static HRESULT WINAPI EnumMonikerImpl_Reset(IEnumMoniker* iface)
902 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
904 This->pos = 0; /* set back to start of list */
906 TRACE("(%p)\n",This);
908 return S_OK;
911 /***********************************************************************
912 * EnmumMoniker_Clone
914 static HRESULT WINAPI EnumMonikerImpl_Clone(IEnumMoniker* iface, IEnumMoniker ** ppenum)
916 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
917 MInterfacePointer **monikers = HeapAlloc(GetProcessHeap(), 0, sizeof(*monikers)*This->moniker_count);
918 ULONG i;
920 TRACE("(%p)\n",This);
922 for (i = 0; i < This->moniker_count; i++)
924 SIZE_T size = FIELD_OFFSET(MInterfacePointer, abData[This->monikers[i]->ulCntData]);
925 monikers[i] = HeapAlloc(GetProcessHeap(), 0, size);
926 memcpy(monikers[i], This->monikers[i], size);
929 /* copy the enum structure */
930 return EnumMonikerImpl_CreateEnumROTMoniker(monikers, This->moniker_count,
931 This->pos, ppenum);
934 /* Virtual function table for the IEnumMoniker class. */
935 static const IEnumMonikerVtbl VT_EnumMonikerImpl =
937 EnumMonikerImpl_QueryInterface,
938 EnumMonikerImpl_AddRef,
939 EnumMonikerImpl_Release,
940 EnumMonikerImpl_Next,
941 EnumMonikerImpl_Skip,
942 EnumMonikerImpl_Reset,
943 EnumMonikerImpl_Clone
946 /***********************************************************************
947 * EnumMonikerImpl_CreateEnumROTMoniker
948 * Used by EnumRunning to create the structure and EnumClone
949 * to copy the structure
951 static HRESULT WINAPI EnumMonikerImpl_CreateEnumROTMoniker(MInterfacePointer **monikers,
952 ULONG moniker_count,
953 ULONG current_pos,
954 IEnumMoniker **ppenumMoniker)
956 EnumMonikerImpl* This = NULL;
958 if (!ppenumMoniker)
959 return E_INVALIDARG;
961 This = HeapAlloc(GetProcessHeap(), 0, sizeof(EnumMonikerImpl));
962 if (!This) return E_OUTOFMEMORY;
964 TRACE("(%p)\n", This);
966 /* initialize the virtual table function */
967 This->lpVtbl = &VT_EnumMonikerImpl;
969 /* the initial reference is set to "1" */
970 This->ref = 1; /* set the ref count to one */
971 This->pos = current_pos; /* Set the list start posn */
972 This->moniker_count = moniker_count; /* Need the same size table as ROT */
973 This->monikers = monikers;
975 *ppenumMoniker = (IEnumMoniker*)This;
977 return S_OK;
981 /* Shared implementation of moniker marshaler based on saving and loading of
982 * monikers */
984 #define ICOM_THIS_From_IMoniker(class, name) class* This = (class*)(((char*)name)-FIELD_OFFSET(class, lpVtblMarshal))
986 typedef struct MonikerMarshal
988 const IUnknownVtbl *lpVtbl;
989 const IMarshalVtbl *lpVtblMarshal;
991 ULONG ref;
992 IMoniker *moniker;
993 } MonikerMarshal;
995 static HRESULT WINAPI MonikerMarshalInner_QueryInterface(IUnknown *iface, REFIID riid, LPVOID *ppv)
997 MonikerMarshal *This = (MonikerMarshal *)iface;
998 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
999 *ppv = NULL;
1000 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IMarshal, riid))
1002 *ppv = &This->lpVtblMarshal;
1003 IUnknown_AddRef((IUnknown *)&This->lpVtblMarshal);
1004 return S_OK;
1006 FIXME("No interface for %s\n", debugstr_guid(riid));
1007 return E_NOINTERFACE;
1010 static ULONG WINAPI MonikerMarshalInner_AddRef(IUnknown *iface)
1012 MonikerMarshal *This = (MonikerMarshal *)iface;
1013 return InterlockedIncrement(&This->ref);
1016 static ULONG WINAPI MonikerMarshalInner_Release(IUnknown *iface)
1018 MonikerMarshal *This = (MonikerMarshal *)iface;
1019 ULONG ref = InterlockedDecrement(&This->ref);
1021 if (!ref) HeapFree(GetProcessHeap(), 0, This);
1022 return ref;
1025 static const IUnknownVtbl VT_MonikerMarshalInner =
1027 MonikerMarshalInner_QueryInterface,
1028 MonikerMarshalInner_AddRef,
1029 MonikerMarshalInner_Release
1032 static HRESULT WINAPI MonikerMarshal_QueryInterface(IMarshal *iface, REFIID riid, LPVOID *ppv)
1034 ICOM_THIS_From_IMoniker(MonikerMarshal, iface);
1035 return IMoniker_QueryInterface(This->moniker, riid, ppv);
1038 static ULONG WINAPI MonikerMarshal_AddRef(IMarshal *iface)
1040 ICOM_THIS_From_IMoniker(MonikerMarshal, iface);
1041 return IMoniker_AddRef(This->moniker);
1044 static ULONG WINAPI MonikerMarshal_Release(IMarshal *iface)
1046 ICOM_THIS_From_IMoniker(MonikerMarshal, iface);
1047 return IMoniker_Release(This->moniker);
1050 static HRESULT WINAPI MonikerMarshal_GetUnmarshalClass(
1051 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
1052 void* pvDestContext, DWORD mshlflags, CLSID* pCid)
1054 ICOM_THIS_From_IMoniker(MonikerMarshal, iface);
1056 TRACE("(%s, %p, %lx, %p, %lx, %p)\n", debugstr_guid(riid), pv,
1057 dwDestContext, pvDestContext, mshlflags, pCid);
1059 return IMoniker_GetClassID(This->moniker, pCid);
1062 static HRESULT WINAPI MonikerMarshal_GetMarshalSizeMax(
1063 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
1064 void* pvDestContext, DWORD mshlflags, DWORD* pSize)
1066 ICOM_THIS_From_IMoniker(MonikerMarshal, iface);
1067 HRESULT hr;
1068 ULARGE_INTEGER size;
1070 TRACE("(%s, %p, %lx, %p, %lx, %p)\n", debugstr_guid(riid), pv,
1071 dwDestContext, pvDestContext, mshlflags, pSize);
1073 hr = IMoniker_GetSizeMax(This->moniker, &size);
1074 if (hr == S_OK)
1075 *pSize = (DWORD)size.QuadPart;
1076 return hr;
1079 static HRESULT WINAPI MonikerMarshal_MarshalInterface(LPMARSHAL iface, IStream *pStm,
1080 REFIID riid, void* pv, DWORD dwDestContext,
1081 void* pvDestContext, DWORD mshlflags)
1083 ICOM_THIS_From_IMoniker(MonikerMarshal, iface);
1085 TRACE("(%p, %s, %p, %lx, %p, %lx)\n", pStm, debugstr_guid(riid), pv,
1086 dwDestContext, pvDestContext, mshlflags);
1088 return IMoniker_Save(This->moniker, pStm, FALSE);
1091 static HRESULT WINAPI MonikerMarshal_UnmarshalInterface(LPMARSHAL iface, IStream *pStm, REFIID riid, void **ppv)
1093 ICOM_THIS_From_IMoniker(MonikerMarshal, iface);
1094 HRESULT hr;
1096 TRACE("(%p, %s, %p)\n", pStm, debugstr_guid(riid), ppv);
1098 hr = IMoniker_Load(This->moniker, pStm);
1099 if (hr == S_OK)
1100 hr = IMoniker_QueryInterface(This->moniker, riid, ppv);
1101 return hr;
1104 static HRESULT WINAPI MonikerMarshal_ReleaseMarshalData(LPMARSHAL iface, IStream *pStm)
1106 TRACE("()\n");
1107 /* can't release a state-based marshal as nothing on server side to
1108 * release */
1109 return S_OK;
1112 static HRESULT WINAPI MonikerMarshal_DisconnectObject(LPMARSHAL iface, DWORD dwReserved)
1114 TRACE("()\n");
1115 /* can't disconnect a state-based marshal as nothing on server side to
1116 * disconnect from */
1117 return S_OK;
1120 static const IMarshalVtbl VT_MonikerMarshal =
1122 MonikerMarshal_QueryInterface,
1123 MonikerMarshal_AddRef,
1124 MonikerMarshal_Release,
1125 MonikerMarshal_GetUnmarshalClass,
1126 MonikerMarshal_GetMarshalSizeMax,
1127 MonikerMarshal_MarshalInterface,
1128 MonikerMarshal_UnmarshalInterface,
1129 MonikerMarshal_ReleaseMarshalData,
1130 MonikerMarshal_DisconnectObject
1133 HRESULT MonikerMarshal_Create(IMoniker *inner, IUnknown **outer)
1135 MonikerMarshal *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1136 if (!This) return E_OUTOFMEMORY;
1138 This->lpVtbl = &VT_MonikerMarshalInner;
1139 This->lpVtblMarshal = &VT_MonikerMarshal;
1140 This->ref = 1;
1141 This->moniker = inner;
1143 *outer = (IUnknown *)&This->lpVtbl;
1144 return S_OK;