Removed superfluous WIN_ReleaseWndPtr.
[wine/wine-kai.git] / ole / moniker.c
blob719219acff76dc8ee9e5c80d3e7df86f79de0119
1 /*
2 * Monikers
4 * Copyright 1998 Marcus Meissner
5 * Copyright 1999 Noomen Hamza
6 */
8 #include <assert.h>
9 #include "winerror.h"
10 #include "wine/obj_moniker.h"
11 #include "debug.h"
12 #include "heap.h"
14 DEFAULT_DEBUG_CHANNEL(ole)
16 #define BLOCK_TAB_SIZE 20 /* represent the first size table and it's increment block size */
18 /* define the structure of the running object table elements */
19 typedef struct RunObject{
21 IUnknown* pObj; /* points on a running object*/
22 IMoniker* pmkObj; /* points on a moniker who identifies this object */
23 FILETIME lastModifObj;
24 DWORD identRegObj; /* registration key relative to this object */
25 DWORD regTypeObj; /* registration type : strong or weak */
26 }RunObject;
28 /* define de RunningObjectTableImpl structure */
29 typedef struct RunningObjectTableImpl{
31 ICOM_VTABLE(IRunningObjectTable)* lpvtbl;
32 ULONG ref;
34 RunObject* runObjTab; /* pointe to the first object in the table */
35 DWORD runObjTabSize; /* current table size */
36 DWORD runObjTabLastIndx; /* first free index element in the table. */
37 DWORD runObjTabRegister; /* registration key of the next registred object */
39 } RunningObjectTableImpl;
41 RunningObjectTableImpl* runningObjectTableInstance=0;
43 /* IRunningObjectTable prototipe functions : */
44 /* IUnknown functions*/
45 static HRESULT WINAPI RunningObjectTableImpl_QueryInterface(IRunningObjectTable* iface,REFIID riid,void** ppvObject);
46 static ULONG WINAPI RunningObjectTableImpl_AddRef(IRunningObjectTable* iface);
47 static ULONG WINAPI RunningObjectTableImpl_Release(IRunningObjectTable* iface);
48 /* IRunningObjectTable functions */
49 static HRESULT WINAPI RunningObjectTableImpl_Register(IRunningObjectTable* iface, DWORD grfFlags,IUnknown* punkObject,IMoniker* pmkObjectName,DWORD* pdwRegister);
50 static HRESULT WINAPI RunningObjectTableImpl_Revoke(IRunningObjectTable* iface, DWORD dwRegister);
51 static HRESULT WINAPI RunningObjectTableImpl_IsRunning(IRunningObjectTable* iface, IMoniker* pmkObjectName);
52 static HRESULT WINAPI RunningObjectTableImpl_GetObject(IRunningObjectTable* iface, IMoniker* pmkObjectName,IUnknown** ppunkObject);
53 static HRESULT WINAPI RunningObjectTableImpl_NoteChangeTime(IRunningObjectTable* iface, DWORD dwRegister,FILETIME* pfiletime);
54 static HRESULT WINAPI RunningObjectTableImpl_GetTimeOfLastChange(IRunningObjectTable* iface, IMoniker* pmkObjectName,FILETIME* pfiletime);
55 static HRESULT WINAPI RunningObjectTableImpl_EnumRunning(IRunningObjectTable* iface, IEnumMoniker** ppenumMoniker);
56 /* Local functions*/
57 HRESULT WINAPI RunningObjectTableImpl_Initialize();
58 HRESULT WINAPI RunningObjectTableImpl_UnInitialize();
59 HRESULT WINAPI RunningObjectTableImpl_Destroy();
60 HRESULT WINAPI RunningObjectTableImpl_GetObjectIndex(RunningObjectTableImpl* This,DWORD identReg,IMoniker* pmk,DWORD *indx);
62 /* Virtual function table for the IRunningObjectTable class. */
63 static ICOM_VTABLE(IRunningObjectTable) VT_RunningObjectTableImpl =
65 RunningObjectTableImpl_QueryInterface,
66 RunningObjectTableImpl_AddRef,
67 RunningObjectTableImpl_Release,
68 RunningObjectTableImpl_Register,
69 RunningObjectTableImpl_Revoke,
70 RunningObjectTableImpl_IsRunning,
71 RunningObjectTableImpl_GetObject,
72 RunningObjectTableImpl_NoteChangeTime,
73 RunningObjectTableImpl_GetTimeOfLastChange,
74 RunningObjectTableImpl_EnumRunning
77 /***********************************************************************
78 * RunningObjectTable_QueryInterface
80 HRESULT WINAPI RunningObjectTableImpl_QueryInterface(IRunningObjectTable* iface,REFIID riid,void** ppvObject)
82 ICOM_THIS(RunningObjectTableImpl,iface);
84 TRACE(ole,"(%p,%p,%p)\n",This,riid,ppvObject);
86 /* validate arguments*/
87 if (This==0)
88 return CO_E_NOTINITIALIZED;
90 if (ppvObject==0)
91 return E_INVALIDARG;
93 *ppvObject = 0;
95 if (IsEqualIID(&IID_IUnknown, riid))
96 *ppvObject = (IRunningObjectTable*)This;
97 else
98 if (IsEqualIID(&IID_IRunningObjectTable, riid))
99 *ppvObject = (IRunningObjectTable*)This;
101 if ((*ppvObject)==0)
102 return E_NOINTERFACE;
104 RunningObjectTableImpl_AddRef(iface);
106 return S_OK;
109 /***********************************************************************
110 * RunningObjectTable_AddRef
112 ULONG WINAPI RunningObjectTableImpl_AddRef(IRunningObjectTable* iface)
114 ICOM_THIS(RunningObjectTableImpl,iface);
116 TRACE(ole,"(%p)\n",This);
118 return ++(This->ref);
121 /***********************************************************************
122 * RunningObjectTable_Initialize
124 HRESULT WINAPI RunningObjectTableImpl_Destroy()
126 TRACE(ole,"()\n");
128 if (runningObjectTableInstance==NULL)
129 return E_INVALIDARG;
131 /* free the ROT table memory */
132 HeapFree(GetProcessHeap(),0,runningObjectTableInstance->runObjTab);
134 /* free the ROT structure memory */
135 HeapFree(GetProcessHeap(),0,runningObjectTableInstance);
137 return S_OK;
140 /***********************************************************************
141 * RunningObjectTable_Release
143 ULONG WINAPI RunningObjectTableImpl_Release(IRunningObjectTable* iface)
145 DWORD i;
146 ICOM_THIS(RunningObjectTableImpl,iface);
148 TRACE(ole,"(%p)\n",This);
150 This->ref--;
152 /* unitialize ROT structure if there's no more reference to it*/
153 if (This->ref==0){
155 /* release all registred objects */
156 for(i=0;i<This->runObjTabLastIndx;i++)
158 if (( This->runObjTab[i].regTypeObj & ROTFLAGS_REGISTRATIONKEEPSALIVE) != 0)
159 IUnknown_Release(This->runObjTab[i].pObj);
161 IMoniker_Release(This->runObjTab[i].pmkObj);
163 /* RunningObjectTable data structure will be not destroyed here ! the destruction will be done only
164 * when RunningObjectTableImpl_UnInitialize function is called
167 /* there's no more elements in the table */
168 This->runObjTabRegister=0;
169 This->runObjTabLastIndx=0;
171 return 0;
174 return This->ref;
177 /***********************************************************************
178 * RunningObjectTable_Initialize
180 HRESULT WINAPI RunningObjectTableImpl_Initialize()
182 TRACE(ole,"()\n");
184 /* create the unique instance of the RunningObjectTableImpl structure */
185 runningObjectTableInstance = HeapAlloc(GetProcessHeap(), 0, sizeof(RunningObjectTableImpl));
187 if (runningObjectTableInstance == 0)
188 return E_OUTOFMEMORY;
190 /* initialize the virtual table function */
191 runningObjectTableInstance->lpvtbl = &VT_RunningObjectTableImpl;
193 /* the initial reference is set to "1" ! because if set to "0" it will be not practis when */
194 /* the ROT refered many times not in the same time (all the objects in the ROT will */
195 /* be removed evry time the ROT is removed ) */
196 runningObjectTableInstance->ref = 1;
198 /* allocate space memory for the table witch contains all the running objects */
199 runningObjectTableInstance->runObjTab = HeapAlloc(GetProcessHeap(), 0, sizeof(RunObject[BLOCK_TAB_SIZE]));
201 if (runningObjectTableInstance->runObjTab == NULL)
202 return E_OUTOFMEMORY;
204 runningObjectTableInstance->runObjTabSize=BLOCK_TAB_SIZE;
205 runningObjectTableInstance->runObjTabRegister=0;
206 runningObjectTableInstance->runObjTabLastIndx=0;
208 return S_OK;
211 /***********************************************************************
212 * RunningObjectTable_UnInitialize
214 HRESULT WINAPI RunningObjectTableImpl_UnInitialize()
216 TRACE(ole,"()\n");
218 if (runningObjectTableInstance==NULL)
219 return E_POINTER;
221 RunningObjectTableImpl_Release((IRunningObjectTable*)runningObjectTableInstance);
223 RunningObjectTableImpl_Destroy();
225 return S_OK;
228 /***********************************************************************
229 * RunningObjectTable_Register
231 HRESULT WINAPI RunningObjectTableImpl_Register(IRunningObjectTable* iface,
232 DWORD grfFlags, /* Registration options */
233 IUnknown *punkObject, /* Pointer to the object being registered */
234 IMoniker *pmkObjectName, /* Pointer to the moniker of the object being registered */
235 DWORD *pdwRegister) /* Pointer to the value identifying the registration */
237 HRESULT res=S_OK;
238 ICOM_THIS(RunningObjectTableImpl,iface);
240 TRACE(ole,"(%p,%ld,%p,%p,%p)\n",This,grfFlags,punkObject,pmkObjectName,pdwRegister);
242 /* there's only tow types of register : strong and or weak registration (only one must be passed on parameter) */
243 if ( ( (grfFlags & ROTFLAGS_REGISTRATIONKEEPSALIVE) || !(grfFlags & ROTFLAGS_ALLOWANYCLIENT)) &&
244 (!(grfFlags & ROTFLAGS_REGISTRATIONKEEPSALIVE) || (grfFlags & ROTFLAGS_ALLOWANYCLIENT)) &&
245 (grfFlags) )
246 return E_INVALIDARG;
248 if (punkObject==NULL || pmkObjectName==NULL || pdwRegister==NULL)
249 return E_INVALIDARG;
251 /* verify if the object to be registred was registred befor */
252 if (RunningObjectTableImpl_GetObjectIndex(This,-1,pmkObjectName,NULL)==S_OK)
253 res = MK_S_MONIKERALREADYREGISTERED;
255 /* put the new registred object in the first free element in the table */
256 This->runObjTab[This->runObjTabLastIndx].pObj = punkObject;
257 This->runObjTab[This->runObjTabLastIndx].pmkObj = pmkObjectName;
258 This->runObjTab[This->runObjTabLastIndx].regTypeObj = grfFlags;
259 This->runObjTab[This->runObjTabLastIndx].identRegObj = This->runObjTabRegister;
260 CoFileTimeNow(&(This->runObjTab[This->runObjTabLastIndx].lastModifObj));
262 /* gives a registration identifier to the registred object*/
263 (*pdwRegister)= This->runObjTabRegister;
265 if (This->runObjTabRegister == 0xFFFFFFFF){
267 FIXME(ole,"runObjTabRegister: %ld is out of data limite \n",This->runObjTabRegister);
268 return E_FAIL;
270 This->runObjTabRegister++;
271 This->runObjTabLastIndx++;
273 if (This->runObjTabLastIndx == This->runObjTabSize){ /* table is full ! so it must be resized */
275 This->runObjTabSize+=BLOCK_TAB_SIZE; /* newsize table */
276 This->runObjTab=HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,This->runObjTab,
277 This->runObjTabSize * sizeof(RunObject));
278 if (!This->runObjTab)
279 return E_OUTOFMEMORY;
281 /* add a reference to the object in the strong registration case */
282 if ((grfFlags & ROTFLAGS_REGISTRATIONKEEPSALIVE) !=0 )
283 IUnknown_AddRef(punkObject);
285 IMoniker_AddRef(pmkObjectName);
287 return res;
290 /***********************************************************************
291 * RunningObjectTable_Revoke
293 HRESULT WINAPI RunningObjectTableImpl_Revoke( IRunningObjectTable* iface,
294 DWORD dwRegister) /* Value identifying registration to be revoked*/
297 DWORD index,j;
298 ICOM_THIS(RunningObjectTableImpl,iface);
300 TRACE(ole,"(%p,%ld)\n",This,dwRegister);
302 /* verify if the object to be revoked was registred befor or not */
303 if (RunningObjectTableImpl_GetObjectIndex(This,dwRegister,NULL,&index)==S_FALSE)
305 return E_INVALIDARG;
307 /* release the object if it was registred with a strong registrantion option */
308 if ((This->runObjTab[index].regTypeObj & ROTFLAGS_REGISTRATIONKEEPSALIVE)!=0)
309 IUnknown_Release(This->runObjTab[index].pObj);
311 IMoniker_Release(This->runObjTab[index].pmkObj);
313 /* remove the object from the table */
314 for(j=index; j<This->runObjTabLastIndx-1; j++)
315 This->runObjTab[j]= This->runObjTab[j+1];
317 This->runObjTabLastIndx--;
319 return S_OK;
322 /***********************************************************************
323 * RunningObjectTable_IsRunning
325 HRESULT WINAPI RunningObjectTableImpl_IsRunning( IRunningObjectTable* iface,
326 IMoniker *pmkObjectName) /* Pointer to the moniker of the object whose status is desired */
328 ICOM_THIS(RunningObjectTableImpl,iface);
330 TRACE(ole,"(%p,%p)\n",This,pmkObjectName);
332 return RunningObjectTableImpl_GetObjectIndex(This,-1,pmkObjectName,NULL);
335 /***********************************************************************
336 * RunningObjectTable_GetObject
338 HRESULT WINAPI RunningObjectTableImpl_GetObject( IRunningObjectTable* iface,
339 IMoniker *pmkObjectName,/* Pointer to the moniker on the object */
340 IUnknown **ppunkObject) /* Address of output variable that receives the IUnknown interface pointer */
342 DWORD index;
343 ICOM_THIS(RunningObjectTableImpl,iface);
345 TRACE(ole,"(%p,%p,%p)\n",This,pmkObjectName,ppunkObject);
347 if (ppunkObject==NULL)
348 return E_POINTER;
350 *ppunkObject=0;
352 /* verify if the object was registred befor or not */
353 if (RunningObjectTableImpl_GetObjectIndex(This,-1,pmkObjectName,&index)==S_FALSE)
354 return MK_E_UNAVAILABLE;
356 /* add a reference to the object then set output object argument */
357 IUnknown_AddRef(This->runObjTab[index].pObj);
358 *ppunkObject=This->runObjTab[index].pObj;
360 return S_OK;
363 /***********************************************************************
364 * RunningObjectTable_NoteChangeTime
366 HRESULT WINAPI RunningObjectTableImpl_NoteChangeTime(IRunningObjectTable* iface,
367 DWORD dwRegister, /* Value identifying registration being updated */
368 FILETIME *pfiletime) /* Pointer to structure containing object's last change time */
370 DWORD index=-1;
371 ICOM_THIS(RunningObjectTableImpl,iface);
373 TRACE(ole,"(%p,%ld,%p)\n",This,dwRegister,pfiletime);
375 /* verify if the object to be changed was registred befor or not */
376 if (RunningObjectTableImpl_GetObjectIndex(This,dwRegister,NULL,&index)==S_FALSE)
377 return E_INVALIDARG;
379 /* set the new value of the last time change */
380 This->runObjTab[index].lastModifObj= (*pfiletime);
382 return S_OK;
385 /***********************************************************************
386 * RunningObjectTable_GetTimeOfLastChange
388 HRESULT WINAPI RunningObjectTableImpl_GetTimeOfLastChange(IRunningObjectTable* iface,
389 IMoniker *pmkObjectName, /* Pointer to moniker on the object whose status is desired */
390 FILETIME *pfiletime) /* Pointer to structure that receives object's last change time */
392 DWORD index=-1;
393 ICOM_THIS(RunningObjectTableImpl,iface);
395 TRACE(ole,"(%p,%p,%p)\n",This,pmkObjectName,pfiletime);
397 if (pmkObjectName==NULL || pfiletime==NULL)
398 return E_INVALIDARG;
400 /* verify if the object was registred befor or not */
401 if (RunningObjectTableImpl_GetObjectIndex(This,-1,pmkObjectName,&index)==S_FALSE)
402 return MK_E_UNAVAILABLE;;
404 (*pfiletime)= This->runObjTab[index].lastModifObj;
406 return S_OK;
409 /***********************************************************************
410 * RunningObjectTable_EnumRunning
412 HRESULT WINAPI RunningObjectTableImpl_EnumRunning(IRunningObjectTable* iface,
413 IEnumMoniker **ppenumMoniker) /* Address of output variable that receives the IEnumMoniker interface pointer */
415 FIXME(ole,"(%p,%p) needs the IEnumMoniker implementation \n",iface,ppenumMoniker);
416 return E_NOTIMPL;
419 /***********************************************************************
420 * GetObjectIndex
422 HRESULT WINAPI RunningObjectTableImpl_GetObjectIndex(RunningObjectTableImpl* This,
423 DWORD identReg,
424 IMoniker* pmk,
425 DWORD *indx)
428 DWORD i;
430 TRACE(ole,"(%p,%ld,%p,%p)\n",This,identReg,pmk,indx);
432 if (pmk!=NULL)
433 /* search object identified by a moniker*/
434 for(i=0 ; (i < This->runObjTabLastIndx) &&(!IMoniker_IsEqual(This->runObjTab[i].pmkObj,pmk)==S_OK);i++);
435 else
436 /* search object identified by a register identifier*/
437 for(i=0;((i<This->runObjTabLastIndx)&&(This->runObjTab[i].identRegObj!=identReg));i++);
439 if (i==This->runObjTabLastIndx) return S_FALSE;
441 if (indx != NULL) *indx=i;
443 return S_OK;
446 /******************************************************************************
447 * GetRunningObjectTable16 [OLE2.30]
449 HRESULT WINAPI GetRunningObjectTable16(DWORD reserved, LPVOID *pprot)
451 FIXME(ole,"(%ld,%p),stub!\n",reserved,pprot);
452 return E_NOTIMPL;
455 /***********************************************************************
456 * GetRunningObjectTable (OLE2.73)
458 HRESULT WINAPI GetRunningObjectTable(DWORD reserved, LPVOID *pprot)
460 IID riid=IID_IRunningObjectTable;
461 HRESULT res;
463 TRACE(ole,"()\n");
465 if (reserved!=0)
466 return E_UNEXPECTED;
468 if(runningObjectTableInstance==NULL)
469 return CO_E_NOTINITIALIZED;
471 res = RunningObjectTableImpl_QueryInterface((IRunningObjectTable*)runningObjectTableInstance,&riid,(void**)pprot);
473 return res;