Pass the correct hFile to PE_CreateModule.
[wine/multimedia.git] / dlls / ole32 / moniker.c
blobf75373d17e92213bbc3cec2f04fea3dc47c41396
1 /*
2 * Monikers
4 * Copyright 1998 Marcus Meissner
5 * Copyright 1999 Noomen Hamza
6 */
8 #include <assert.h>
9 #include "winerror.h"
10 #include "wtypes.h"
11 #include "winbase.h"
12 #include "wine/obj_base.h"
13 #include "wine/obj_storage.h"
14 #include "wine/obj_misc.h"
15 #include "wine/obj_moniker.h"
16 #include "debugtools.h"
17 #include "heap.h"
18 #include "ole2.h"
20 DEFAULT_DEBUG_CHANNEL(ole)
22 #define BLOCK_TAB_SIZE 20 /* represent the first size table and it's increment block size */
24 /* define the structure of the running object table elements */
25 typedef struct RunObject{
27 IUnknown* pObj; /* points on a running object*/
28 IMoniker* pmkObj; /* points on a moniker who identifies this object */
29 FILETIME lastModifObj;
30 DWORD identRegObj; /* registration key relative to this object */
31 DWORD regTypeObj; /* registration type : strong or weak */
32 }RunObject;
34 /* define the RunningObjectTableImpl structure */
35 typedef struct RunningObjectTableImpl{
37 ICOM_VFIELD(IRunningObjectTable);
38 ULONG ref;
40 RunObject* runObjTab; /* pointer to the first object in the table */
41 DWORD runObjTabSize; /* current table size */
42 DWORD runObjTabLastIndx; /* first free index element in the table. */
43 DWORD runObjTabRegister; /* registration key of the next registered object */
45 } RunningObjectTableImpl;
47 RunningObjectTableImpl* runningObjectTableInstance=0;
49 /* IRunningObjectTable prototype functions : */
50 /* IUnknown functions*/
51 static HRESULT WINAPI RunningObjectTableImpl_QueryInterface(IRunningObjectTable* iface,REFIID riid,void** ppvObject);
52 static ULONG WINAPI RunningObjectTableImpl_AddRef(IRunningObjectTable* iface);
53 static ULONG WINAPI RunningObjectTableImpl_Release(IRunningObjectTable* iface);
54 /* IRunningObjectTable functions */
55 static HRESULT WINAPI RunningObjectTableImpl_Register(IRunningObjectTable* iface, DWORD grfFlags,IUnknown* punkObject,IMoniker* pmkObjectName,DWORD* pdwRegister);
56 static HRESULT WINAPI RunningObjectTableImpl_Revoke(IRunningObjectTable* iface, DWORD dwRegister);
57 static HRESULT WINAPI RunningObjectTableImpl_IsRunning(IRunningObjectTable* iface, IMoniker* pmkObjectName);
58 static HRESULT WINAPI RunningObjectTableImpl_GetObject(IRunningObjectTable* iface, IMoniker* pmkObjectName,IUnknown** ppunkObject);
59 static HRESULT WINAPI RunningObjectTableImpl_NoteChangeTime(IRunningObjectTable* iface, DWORD dwRegister,FILETIME* pfiletime);
60 static HRESULT WINAPI RunningObjectTableImpl_GetTimeOfLastChange(IRunningObjectTable* iface, IMoniker* pmkObjectName,FILETIME* pfiletime);
61 static HRESULT WINAPI RunningObjectTableImpl_EnumRunning(IRunningObjectTable* iface, IEnumMoniker** ppenumMoniker);
62 /* Local functions*/
63 HRESULT WINAPI RunningObjectTableImpl_Initialize();
64 HRESULT WINAPI RunningObjectTableImpl_UnInitialize();
65 HRESULT WINAPI RunningObjectTableImpl_Destroy();
66 HRESULT WINAPI RunningObjectTableImpl_GetObjectIndex(RunningObjectTableImpl* This,DWORD identReg,IMoniker* pmk,DWORD *indx);
68 /* Virtual function table for the IRunningObjectTable class. */
69 static ICOM_VTABLE(IRunningObjectTable) VT_RunningObjectTableImpl =
71 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
72 RunningObjectTableImpl_QueryInterface,
73 RunningObjectTableImpl_AddRef,
74 RunningObjectTableImpl_Release,
75 RunningObjectTableImpl_Register,
76 RunningObjectTableImpl_Revoke,
77 RunningObjectTableImpl_IsRunning,
78 RunningObjectTableImpl_GetObject,
79 RunningObjectTableImpl_NoteChangeTime,
80 RunningObjectTableImpl_GetTimeOfLastChange,
81 RunningObjectTableImpl_EnumRunning
84 /***********************************************************************
85 * RunningObjectTable_QueryInterface
87 HRESULT WINAPI RunningObjectTableImpl_QueryInterface(IRunningObjectTable* iface,REFIID riid,void** ppvObject)
89 ICOM_THIS(RunningObjectTableImpl,iface);
91 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
93 /* validate arguments */
94 if (This==0)
95 return CO_E_NOTINITIALIZED;
97 if (ppvObject==0)
98 return E_INVALIDARG;
100 *ppvObject = 0;
102 if (IsEqualIID(&IID_IUnknown, riid))
103 *ppvObject = (IRunningObjectTable*)This;
104 else
105 if (IsEqualIID(&IID_IRunningObjectTable, riid))
106 *ppvObject = (IRunningObjectTable*)This;
108 if ((*ppvObject)==0)
109 return E_NOINTERFACE;
111 RunningObjectTableImpl_AddRef(iface);
113 return S_OK;
116 /***********************************************************************
117 * RunningObjectTable_AddRef
119 ULONG WINAPI RunningObjectTableImpl_AddRef(IRunningObjectTable* iface)
121 ICOM_THIS(RunningObjectTableImpl,iface);
123 TRACE("(%p)\n",This);
125 return ++(This->ref);
128 /***********************************************************************
129 * RunningObjectTable_Initialize
131 HRESULT WINAPI RunningObjectTableImpl_Destroy()
133 TRACE("()\n");
135 if (runningObjectTableInstance==NULL)
136 return E_INVALIDARG;
138 /* free the ROT table memory */
139 HeapFree(GetProcessHeap(),0,runningObjectTableInstance->runObjTab);
141 /* free the ROT structure memory */
142 HeapFree(GetProcessHeap(),0,runningObjectTableInstance);
144 return S_OK;
147 /***********************************************************************
148 * RunningObjectTable_Release
150 ULONG WINAPI RunningObjectTableImpl_Release(IRunningObjectTable* iface)
152 DWORD i;
153 ICOM_THIS(RunningObjectTableImpl,iface);
155 TRACE("(%p)\n",This);
157 This->ref--;
159 /* unitialize ROT structure if there's no more reference to it*/
160 if (This->ref==0){
162 /* release all registered objects */
163 for(i=0;i<This->runObjTabLastIndx;i++)
165 if (( This->runObjTab[i].regTypeObj & ROTFLAGS_REGISTRATIONKEEPSALIVE) != 0)
166 IUnknown_Release(This->runObjTab[i].pObj);
168 IMoniker_Release(This->runObjTab[i].pmkObj);
170 /* RunningObjectTable data structure will be not destroyed here ! the destruction will be done only
171 * when RunningObjectTableImpl_UnInitialize function is called
174 /* there's no more elements in the table */
175 This->runObjTabRegister=0;
176 This->runObjTabLastIndx=0;
178 return 0;
181 return This->ref;
184 /***********************************************************************
185 * RunningObjectTable_Initialize
187 HRESULT WINAPI RunningObjectTableImpl_Initialize()
189 TRACE("()\n");
191 /* create the unique instance of the RunningObjectTableImpl structure */
192 runningObjectTableInstance = HeapAlloc(GetProcessHeap(), 0, sizeof(RunningObjectTableImpl));
194 if (runningObjectTableInstance == 0)
195 return E_OUTOFMEMORY;
197 /* initialize the virtual table function */
198 ICOM_VTBL(runningObjectTableInstance) = &VT_RunningObjectTableImpl;
200 /* the initial reference is set to "1" ! because if set to "0" it will be not practis when */
201 /* the ROT refered many times not in the same time (all the objects in the ROT will */
202 /* be removed evry time the ROT is removed ) */
203 runningObjectTableInstance->ref = 1;
205 /* allocate space memory for the table witch contains all the running objects */
206 runningObjectTableInstance->runObjTab = HeapAlloc(GetProcessHeap(), 0, sizeof(RunObject[BLOCK_TAB_SIZE]));
208 if (runningObjectTableInstance->runObjTab == NULL)
209 return E_OUTOFMEMORY;
211 runningObjectTableInstance->runObjTabSize=BLOCK_TAB_SIZE;
212 runningObjectTableInstance->runObjTabRegister=1;
213 runningObjectTableInstance->runObjTabLastIndx=0;
215 return S_OK;
218 /***********************************************************************
219 * RunningObjectTable_UnInitialize
221 HRESULT WINAPI RunningObjectTableImpl_UnInitialize()
223 TRACE("()\n");
225 if (runningObjectTableInstance==NULL)
226 return E_POINTER;
228 RunningObjectTableImpl_Release((IRunningObjectTable*)runningObjectTableInstance);
230 RunningObjectTableImpl_Destroy();
232 return S_OK;
235 /***********************************************************************
236 * RunningObjectTable_Register
238 HRESULT WINAPI RunningObjectTableImpl_Register(IRunningObjectTable* iface,
239 DWORD grfFlags, /* Registration options */
240 IUnknown *punkObject, /* Pointer to the object being registered */
241 IMoniker *pmkObjectName, /* Pointer to the moniker of the object being registered */
242 DWORD *pdwRegister) /* Pointer to the value identifying the registration */
244 HRESULT res=S_OK;
245 ICOM_THIS(RunningObjectTableImpl,iface);
247 TRACE("(%p,%ld,%p,%p,%p)\n",This,grfFlags,punkObject,pmkObjectName,pdwRegister);
249 /* there's only tow types of register : strong and or weak registration (only one must be passed on parameter) */
250 if ( ( (grfFlags & ROTFLAGS_REGISTRATIONKEEPSALIVE) || !(grfFlags & ROTFLAGS_ALLOWANYCLIENT)) &&
251 (!(grfFlags & ROTFLAGS_REGISTRATIONKEEPSALIVE) || (grfFlags & ROTFLAGS_ALLOWANYCLIENT)) &&
252 (grfFlags) )
253 return E_INVALIDARG;
255 if (punkObject==NULL || pmkObjectName==NULL || pdwRegister==NULL)
256 return E_INVALIDARG;
258 /* verify if the object to be registered was registered before */
259 if (RunningObjectTableImpl_GetObjectIndex(This,-1,pmkObjectName,NULL)==S_OK)
260 res = MK_S_MONIKERALREADYREGISTERED;
262 /* put the new registered object in the first free element in the table */
263 This->runObjTab[This->runObjTabLastIndx].pObj = punkObject;
264 This->runObjTab[This->runObjTabLastIndx].pmkObj = pmkObjectName;
265 This->runObjTab[This->runObjTabLastIndx].regTypeObj = grfFlags;
266 This->runObjTab[This->runObjTabLastIndx].identRegObj = This->runObjTabRegister;
267 CoFileTimeNow(&(This->runObjTab[This->runObjTabLastIndx].lastModifObj));
269 /* gives a registration identifier to the registered object*/
270 (*pdwRegister)= This->runObjTabRegister;
272 if (This->runObjTabRegister == 0xFFFFFFFF){
274 FIXME("runObjTabRegister: %ld is out of data limite \n",This->runObjTabRegister);
275 return E_FAIL;
277 This->runObjTabRegister++;
278 This->runObjTabLastIndx++;
280 if (This->runObjTabLastIndx == This->runObjTabSize){ /* table is full ! so it must be resized */
282 This->runObjTabSize+=BLOCK_TAB_SIZE; /* newsize table */
283 This->runObjTab=HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,This->runObjTab,
284 This->runObjTabSize * sizeof(RunObject));
285 if (!This->runObjTab)
286 return E_OUTOFMEMORY;
288 /* add a reference to the object in the strong registration case */
289 if ((grfFlags & ROTFLAGS_REGISTRATIONKEEPSALIVE) !=0 )
290 IUnknown_AddRef(punkObject);
292 IMoniker_AddRef(pmkObjectName);
294 return res;
297 /***********************************************************************
298 * RunningObjectTable_Revoke
300 HRESULT WINAPI RunningObjectTableImpl_Revoke( IRunningObjectTable* iface,
301 DWORD dwRegister) /* Value identifying registration to be revoked*/
304 DWORD index,j;
305 ICOM_THIS(RunningObjectTableImpl,iface);
307 TRACE("(%p,%ld)\n",This,dwRegister);
309 /* verify if the object to be revoked was registered before or not */
310 if (RunningObjectTableImpl_GetObjectIndex(This,dwRegister,NULL,&index)==S_FALSE)
312 return E_INVALIDARG;
314 /* release the object if it was registered with a strong registrantion option */
315 if ((This->runObjTab[index].regTypeObj & ROTFLAGS_REGISTRATIONKEEPSALIVE)!=0)
316 IUnknown_Release(This->runObjTab[index].pObj);
318 IMoniker_Release(This->runObjTab[index].pmkObj);
320 /* remove the object from the table */
321 for(j=index; j<This->runObjTabLastIndx-1; j++)
322 This->runObjTab[j]= This->runObjTab[j+1];
324 This->runObjTabLastIndx--;
326 return S_OK;
329 /***********************************************************************
330 * RunningObjectTable_IsRunning
332 HRESULT WINAPI RunningObjectTableImpl_IsRunning( IRunningObjectTable* iface,
333 IMoniker *pmkObjectName) /* Pointer to the moniker of the object whose status is desired */
335 ICOM_THIS(RunningObjectTableImpl,iface);
337 TRACE("(%p,%p)\n",This,pmkObjectName);
339 return RunningObjectTableImpl_GetObjectIndex(This,-1,pmkObjectName,NULL);
342 /***********************************************************************
343 * RunningObjectTable_GetObject
345 HRESULT WINAPI RunningObjectTableImpl_GetObject( IRunningObjectTable* iface,
346 IMoniker *pmkObjectName,/* Pointer to the moniker on the object */
347 IUnknown **ppunkObject) /* Address of output variable that receives the IUnknown interface pointer */
349 DWORD index;
350 ICOM_THIS(RunningObjectTableImpl,iface);
352 TRACE("(%p,%p,%p)\n",This,pmkObjectName,ppunkObject);
354 if (ppunkObject==NULL)
355 return E_POINTER;
357 *ppunkObject=0;
359 /* verify if the object was registered before or not */
360 if (RunningObjectTableImpl_GetObjectIndex(This,-1,pmkObjectName,&index)==S_FALSE)
361 return MK_E_UNAVAILABLE;
363 /* add a reference to the object then set output object argument */
364 IUnknown_AddRef(This->runObjTab[index].pObj);
365 *ppunkObject=This->runObjTab[index].pObj;
367 return S_OK;
370 /***********************************************************************
371 * RunningObjectTable_NoteChangeTime
373 HRESULT WINAPI RunningObjectTableImpl_NoteChangeTime(IRunningObjectTable* iface,
374 DWORD dwRegister, /* Value identifying registration being updated */
375 FILETIME *pfiletime) /* Pointer to structure containing object's last change time */
377 DWORD index=-1;
378 ICOM_THIS(RunningObjectTableImpl,iface);
380 TRACE("(%p,%ld,%p)\n",This,dwRegister,pfiletime);
382 /* verify if the object to be changed was registered before or not */
383 if (RunningObjectTableImpl_GetObjectIndex(This,dwRegister,NULL,&index)==S_FALSE)
384 return E_INVALIDARG;
386 /* set the new value of the last time change */
387 This->runObjTab[index].lastModifObj= (*pfiletime);
389 return S_OK;
392 /***********************************************************************
393 * RunningObjectTable_GetTimeOfLastChange
395 HRESULT WINAPI RunningObjectTableImpl_GetTimeOfLastChange(IRunningObjectTable* iface,
396 IMoniker *pmkObjectName, /* Pointer to moniker on the object whose status is desired */
397 FILETIME *pfiletime) /* Pointer to structure that receives object's last change time */
399 DWORD index=-1;
400 ICOM_THIS(RunningObjectTableImpl,iface);
402 TRACE("(%p,%p,%p)\n",This,pmkObjectName,pfiletime);
404 if (pmkObjectName==NULL || pfiletime==NULL)
405 return E_INVALIDARG;
407 /* verify if the object was registered before or not */
408 if (RunningObjectTableImpl_GetObjectIndex(This,-1,pmkObjectName,&index)==S_FALSE)
409 return MK_E_UNAVAILABLE;;
411 (*pfiletime)= This->runObjTab[index].lastModifObj;
413 return S_OK;
416 /***********************************************************************
417 * RunningObjectTable_EnumRunning
419 HRESULT WINAPI RunningObjectTableImpl_EnumRunning(IRunningObjectTable* iface,
420 IEnumMoniker **ppenumMoniker) /* Address of output variable that receives the IEnumMoniker interface pointer */
422 FIXME("(%p,%p) needs the IEnumMoniker implementation \n",iface,ppenumMoniker);
423 return E_NOTIMPL;
426 /***********************************************************************
427 * GetObjectIndex
429 HRESULT WINAPI RunningObjectTableImpl_GetObjectIndex(RunningObjectTableImpl* This,
430 DWORD identReg,
431 IMoniker* pmk,
432 DWORD *indx)
435 DWORD i;
437 TRACE("(%p,%ld,%p,%p)\n",This,identReg,pmk,indx);
439 if (pmk!=NULL)
440 /* search object identified by a moniker */
441 for(i=0 ; (i < This->runObjTabLastIndx) &&(!IMoniker_IsEqual(This->runObjTab[i].pmkObj,pmk)==S_OK);i++);
442 else
443 /* search object identified by a register identifier */
444 for(i=0;((i<This->runObjTabLastIndx)&&(This->runObjTab[i].identRegObj!=identReg));i++);
446 if (i==This->runObjTabLastIndx) return S_FALSE;
448 if (indx != NULL) *indx=i;
450 return S_OK;
453 /******************************************************************************
454 * GetRunningObjectTable16 [OLE2.30]
456 HRESULT WINAPI GetRunningObjectTable16(DWORD reserved, LPRUNNINGOBJECTTABLE *pprot)
458 FIXME("(%ld,%p),stub!\n",reserved,pprot);
459 return E_NOTIMPL;
462 /***********************************************************************
463 * GetRunningObjectTable (OLE2.73)
465 HRESULT WINAPI GetRunningObjectTable(DWORD reserved, LPRUNNINGOBJECTTABLE *pprot)
467 IID riid=IID_IRunningObjectTable;
468 HRESULT res;
470 TRACE("()\n");
472 if (reserved!=0)
473 return E_UNEXPECTED;
475 if(runningObjectTableInstance==NULL)
476 return CO_E_NOTINITIALIZED;
478 res = RunningObjectTableImpl_QueryInterface((IRunningObjectTable*)runningObjectTableInstance,&riid,(void**)pprot);
480 return res;
483 /******************************************************************************
484 * OleRun [OLE32.123]
486 HRESULT WINAPI OleRun(LPUNKNOWN pUnknown)
488 IRunnableObject *runable;
489 ICOM_THIS(IRunnableObject,pUnknown);
490 LRESULT ret;
492 ret = IRunnableObject_QueryInterface(This,&IID_IRunnableObject,(LPVOID*)&runable);
493 if (ret)
494 return 0; /* Appears to return no error. */
495 ret = IRunnableObject_Run(runable,NULL);
496 IRunnableObject_Release(runable);
497 return ret;
500 /******************************************************************************
501 * MkParseDisplayName [OLE32.81]
503 HRESULT WINAPI MkParseDisplayName(LPBC pbc, LPCOLESTR szUserName,
504 LPDWORD pchEaten, LPMONIKER *ppmk)
506 FIXME("(%p, %s, %p, %p): stub.\n", pbc, debugstr_w(szUserName), pchEaten, *ppmk);
507 if (!(IsValidInterface((LPUNKNOWN) pbc)))
508 return E_INVALIDARG;
510 return MK_E_SYNTAX;