Revert the sync.
[reactos.git] / dll / win32 / ole32 / moniker.c
1 /*
2 * Monikers
3 *
4 * Copyright 1998 Marcus Meissner
5 * Copyright 1999 Noomen Hamza
6 * Copyright 2005 Robert Shearman (for CodeWeavers)
7 * Copyright 2007 Robert Shearman
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24 #include "config.h"
25 #include "wine/port.h"
26
27 #include <stdarg.h>
28 #include <string.h>
29
30 #define COBJMACROS
31
32 #include "winerror.h"
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winuser.h"
36 #include "wtypes.h"
37 #include "ole2.h"
38
39 #include "wine/list.h"
40 #include "wine/debug.h"
41 #include "wine/unicode.h"
42 #include "wine/exception.h"
43
44 #include "compobj_private.h"
45 #include "moniker.h"
46 #include "irot.h"
47
48 WINE_DEFAULT_DEBUG_CHANNEL(ole);
49
50 /* see MSDN docs for IROTData::GetComparisonData, which states what this
51 * constant is
52 */
53 #define MAX_COMPARISON_DATA 2048
54
55 static LONG WINAPI rpc_filter(EXCEPTION_POINTERS *eptr)
56 {
57 return I_RpcExceptionFilter(eptr->ExceptionRecord->ExceptionCode);
58 }
59
60 /* define the structure of the running object table elements */
61 struct rot_entry
62 {
63 struct list entry;
64 InterfaceData* object; /* marshaled running object*/
65 MonikerComparisonData* moniker_data; /* moniker comparison data that identifies this object */
66 DWORD cookie; /* cookie identifying this object */
67 FILETIME last_modified;
68 IrotContextHandle ctxt_handle;
69 };
70
71 /* define the RunningObjectTableImpl structure */
72 typedef struct RunningObjectTableImpl
73 {
74 const IRunningObjectTableVtbl *lpVtbl;
75 LONG ref;
76
77 struct list rot; /* list of ROT entries */
78 CRITICAL_SECTION lock;
79 } RunningObjectTableImpl;
80
81 static RunningObjectTableImpl* runningObjectTableInstance = NULL;
82 static IrotHandle irot_handle;
83
84 /* define the EnumMonikerImpl structure */
85 typedef struct EnumMonikerImpl
86 {
87 const IEnumMonikerVtbl *lpVtbl;
88 LONG ref;
89
90 InterfaceList *moniker_list;
91 ULONG pos;
92 } EnumMonikerImpl;
93
94
95 /* IEnumMoniker Local functions*/
96 static HRESULT EnumMonikerImpl_CreateEnumROTMoniker(InterfaceList *moniker_list,
97 ULONG pos, IEnumMoniker **ppenumMoniker);
98
99 static IrotHandle get_irot_handle(void)
100 {
101 if (!irot_handle)
102 {
103 RPC_STATUS status;
104 RPC_WSTR binding;
105 IrotHandle new_handle;
106 unsigned short ncacn_np[] = IROT_PROTSEQ;
107 unsigned short endpoint[] = IROT_ENDPOINT;
108 status = RpcStringBindingComposeW(NULL, ncacn_np, NULL, endpoint, NULL, &binding);
109 if (status == RPC_S_OK)
110 {
111 status = RpcBindingFromStringBindingW(binding, &new_handle);
112 RpcStringFreeW(&binding);
113 }
114 if (status != RPC_S_OK)
115 return NULL;
116 if (InterlockedCompareExchangePointer(&irot_handle, new_handle, NULL))
117 /* another thread beat us to it */
118 RpcBindingFree(&new_handle);
119 }
120 return irot_handle;
121 }
122
123 static BOOL start_rpcss(void)
124 {
125 PROCESS_INFORMATION pi;
126 STARTUPINFOW si;
127 WCHAR cmd[MAX_PATH];
128 static const WCHAR rpcss[] = {'\\','r','p','c','s','s','.','e','x','e',0};
129 BOOL rslt;
130
131 TRACE("\n");
132
133 ZeroMemory(&si, sizeof(STARTUPINFOA));
134 si.cb = sizeof(STARTUPINFOA);
135 GetSystemDirectoryW( cmd, MAX_PATH - sizeof(rpcss)/sizeof(WCHAR) );
136 strcatW( cmd, rpcss );
137
138 rslt = CreateProcessW( cmd, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi );
139
140 if (rslt)
141 {
142 CloseHandle(pi.hProcess);
143 CloseHandle(pi.hThread);
144 Sleep(100);
145 }
146
147 return rslt;
148 }
149
150 static HRESULT create_stream_on_mip_ro(const InterfaceData *mip, IStream **stream)
151 {
152 HGLOBAL hglobal = GlobalAlloc(0, mip->ulCntData);
153 void *pv = GlobalLock(hglobal);
154 memcpy(pv, mip->abData, mip->ulCntData);
155 GlobalUnlock(hglobal);
156 return CreateStreamOnHGlobal(hglobal, TRUE, stream);
157 }
158
159 static inline void rot_entry_delete(struct rot_entry *rot_entry)
160 {
161 if (rot_entry->cookie)
162 {
163 InterfaceData *object = NULL;
164 InterfaceData *moniker = NULL;
165 __TRY
166 {
167 IrotRevoke(get_irot_handle(), rot_entry->cookie,
168 &rot_entry->ctxt_handle, &object, &moniker);
169 }
170 __EXCEPT(rpc_filter)
171 {
172 }
173 __ENDTRY
174 MIDL_user_free(object);
175 if (moniker)
176 {
177 IStream *stream;
178 HRESULT hr;
179 hr = create_stream_on_mip_ro(moniker, &stream);
180 if (hr == S_OK)
181 {
182 CoReleaseMarshalData(stream);
183 IUnknown_Release(stream);
184 }
185 }
186 MIDL_user_free(moniker);
187 }
188 if (rot_entry->object)
189 {
190 IStream *stream;
191 HRESULT hr;
192 hr = create_stream_on_mip_ro(rot_entry->object, &stream);
193 if (hr == S_OK)
194 {
195 CoReleaseMarshalData(stream);
196 IUnknown_Release(stream);
197 }
198 }
199 HeapFree(GetProcessHeap(), 0, rot_entry->object);
200 HeapFree(GetProcessHeap(), 0, rot_entry->moniker_data);
201 HeapFree(GetProcessHeap(), 0, rot_entry);
202 }
203
204 /* moniker_data must be freed with HeapFree when no longer in use */
205 static HRESULT get_moniker_comparison_data(IMoniker *pMoniker, MonikerComparisonData **moniker_data)
206 {
207 HRESULT hr;
208 IROTData *pROTData = NULL;
209 hr = IMoniker_QueryInterface(pMoniker, &IID_IROTData, (void *)&pROTData);
210 if (SUCCEEDED(hr))
211 {
212 ULONG size = MAX_COMPARISON_DATA;
213 *moniker_data = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(MonikerComparisonData, abData[size]));
214 if (!*moniker_data)
215 {
216 IROTData_Release(pROTData);
217 return E_OUTOFMEMORY;
218 }
219 hr = IROTData_GetComparisonData(pROTData, (*moniker_data)->abData, size, &size);
220 IROTData_Release(pROTData);
221 if (hr != S_OK)
222 {
223 ERR("Failed to copy comparison data into buffer, hr = 0x%08x\n", hr);
224 HeapFree(GetProcessHeap(), 0, *moniker_data);
225 return hr;
226 }
227 (*moniker_data)->ulCntData = size;
228 }
229 else
230 {
231 IBindCtx *pbc;
232 LPOLESTR pszDisplayName;
233 CLSID clsid;
234 int len;
235
236 TRACE("generating comparison data from display name\n");
237
238 hr = CreateBindCtx(0, &pbc);
239 if (FAILED(hr))
240 return hr;
241 hr = IMoniker_GetDisplayName(pMoniker, pbc, NULL, &pszDisplayName);
242 IBindCtx_Release(pbc);
243 if (FAILED(hr))
244 return hr;
245 hr = IMoniker_GetClassID(pMoniker, &clsid);
246 if (FAILED(hr))
247 {
248 CoTaskMemFree(pszDisplayName);
249 return hr;
250 }
251
252 len = strlenW(pszDisplayName);
253 *moniker_data = HeapAlloc(GetProcessHeap(), 0,
254 FIELD_OFFSET(MonikerComparisonData, abData[sizeof(CLSID) + (len+1)*sizeof(WCHAR)]));
255 if (!*moniker_data)
256 {
257 CoTaskMemFree(pszDisplayName);
258 return E_OUTOFMEMORY;
259 }
260 (*moniker_data)->ulCntData = sizeof(CLSID) + (len+1)*sizeof(WCHAR);
261
262 memcpy(&(*moniker_data)->abData[0], &clsid, sizeof(clsid));
263 memcpy(&(*moniker_data)->abData[sizeof(clsid)], pszDisplayName, (len+1)*sizeof(WCHAR));
264 CoTaskMemFree(pszDisplayName);
265 }
266 return S_OK;
267 }
268
269 static HRESULT reduce_moniker(IMoniker *pmk, IBindCtx *pbc, IMoniker **pmkReduced)
270 {
271 IBindCtx *pbcNew = NULL;
272 HRESULT hr;
273 if (!pbc)
274 {
275 hr = CreateBindCtx(0, &pbcNew);
276 if (FAILED(hr))
277 return hr;
278 pbc = pbcNew;
279 }
280 hr = IMoniker_Reduce(pmk, pbc, MKRREDUCE_ALL, NULL, pmkReduced);
281 if (FAILED(hr))
282 ERR("reducing moniker failed with error 0x%08x\n", hr);
283 if (pbcNew) IBindCtx_Release(pbcNew);
284 return hr;
285 }
286
287 /***********************************************************************
288 * RunningObjectTable_QueryInterface
289 */
290 static HRESULT WINAPI
291 RunningObjectTableImpl_QueryInterface(IRunningObjectTable* iface,
292 REFIID riid,void** ppvObject)
293 {
294 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
295
296 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
297
298 /* validate arguments */
299
300 if (ppvObject==0)
301 return E_INVALIDARG;
302
303 *ppvObject = 0;
304
305 if (IsEqualIID(&IID_IUnknown, riid) ||
306 IsEqualIID(&IID_IRunningObjectTable, riid))
307 *ppvObject = This;
308
309 if ((*ppvObject)==0)
310 return E_NOINTERFACE;
311
312 IRunningObjectTable_AddRef(iface);
313
314 return S_OK;
315 }
316
317 /***********************************************************************
318 * RunningObjectTable_AddRef
319 */
320 static ULONG WINAPI
321 RunningObjectTableImpl_AddRef(IRunningObjectTable* iface)
322 {
323 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
324
325 TRACE("(%p)\n",This);
326
327 return InterlockedIncrement(&This->ref);
328 }
329
330 /***********************************************************************
331 * RunningObjectTable_Destroy
332 */
333 static HRESULT
334 RunningObjectTableImpl_Destroy(void)
335 {
336 struct list *cursor, *cursor2;
337 IrotHandle old_handle;
338
339 TRACE("()\n");
340
341 if (runningObjectTableInstance==NULL)
342 return E_INVALIDARG;
343
344 /* free the ROT table memory */
345 LIST_FOR_EACH_SAFE(cursor, cursor2, &runningObjectTableInstance->rot)
346 {
347 struct rot_entry *rot_entry = LIST_ENTRY(cursor, struct rot_entry, entry);
348 list_remove(&rot_entry->entry);
349 rot_entry_delete(rot_entry);
350 }
351
352 DEBUG_CLEAR_CRITSEC_NAME(&runningObjectTableInstance->lock);
353 DeleteCriticalSection(&runningObjectTableInstance->lock);
354
355 /* free the ROT structure memory */
356 HeapFree(GetProcessHeap(),0,runningObjectTableInstance);
357 runningObjectTableInstance = NULL;
358
359 old_handle = irot_handle;
360 irot_handle = NULL;
361 if (old_handle)
362 RpcBindingFree(&old_handle);
363
364 return S_OK;
365 }
366
367 /***********************************************************************
368 * RunningObjectTable_Release
369 */
370 static ULONG WINAPI
371 RunningObjectTableImpl_Release(IRunningObjectTable* iface)
372 {
373 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
374 ULONG ref;
375
376 TRACE("(%p)\n",This);
377
378 ref = InterlockedDecrement(&This->ref);
379
380 /* uninitialize ROT structure if there's no more references to it */
381 if (ref == 0)
382 {
383 struct list *cursor, *cursor2;
384 LIST_FOR_EACH_SAFE(cursor, cursor2, &This->rot)
385 {
386 struct rot_entry *rot_entry = LIST_ENTRY(cursor, struct rot_entry, entry);
387 list_remove(&rot_entry->entry);
388 rot_entry_delete(rot_entry);
389 }
390 /* RunningObjectTable data structure will be not destroyed here ! the destruction will be done only
391 * when RunningObjectTableImpl_UnInitialize function is called
392 */
393 }
394
395 return ref;
396 }
397
398 /***********************************************************************
399 * RunningObjectTable_Register
400 *
401 * PARAMS
402 * grfFlags [in] Registration options
403 * punkObject [in] the object being registered
404 * pmkObjectName [in] the moniker of the object being registered
405 * pdwRegister [out] the value identifying the registration
406 */
407 static HRESULT WINAPI
408 RunningObjectTableImpl_Register(IRunningObjectTable* iface, DWORD grfFlags,
409 IUnknown *punkObject, IMoniker *pmkObjectName, DWORD *pdwRegister)
410 {
411 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
412 struct rot_entry *rot_entry;
413 HRESULT hr = S_OK;
414 IStream *pStream = NULL;
415 DWORD mshlflags;
416 IBindCtx *pbc;
417 InterfaceData *moniker = NULL;
418
419 TRACE("(%p,%d,%p,%p,%p)\n",This,grfFlags,punkObject,pmkObjectName,pdwRegister);
420
421 if (grfFlags & ~(ROTFLAGS_REGISTRATIONKEEPSALIVE|ROTFLAGS_ALLOWANYCLIENT))
422 {
423 ERR("Invalid grfFlags: 0x%08x\n", grfFlags & ~(ROTFLAGS_REGISTRATIONKEEPSALIVE|ROTFLAGS_ALLOWANYCLIENT));
424 return E_INVALIDARG;
425 }
426
427 if (punkObject==NULL || pmkObjectName==NULL || pdwRegister==NULL)
428 return E_INVALIDARG;
429
430 rot_entry = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*rot_entry));
431 if (!rot_entry)
432 return E_OUTOFMEMORY;
433
434 /* marshal object */
435 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
436 if (hr != S_OK)
437 {
438 rot_entry_delete(rot_entry);
439 return hr;
440 }
441 mshlflags = (grfFlags & ROTFLAGS_REGISTRATIONKEEPSALIVE) ? MSHLFLAGS_TABLESTRONG : MSHLFLAGS_TABLEWEAK;
442 hr = CoMarshalInterface(pStream, &IID_IUnknown, punkObject, MSHCTX_LOCAL | MSHCTX_NOSHAREDMEM, NULL, mshlflags);
443 /* FIXME: a cleaner way would be to create an IStream class that writes
444 * directly to an MInterfacePointer */
445 if (hr == S_OK)
446 {
447 HGLOBAL hglobal;
448 hr = GetHGlobalFromStream(pStream, &hglobal);
449 if (hr == S_OK)
450 {
451 SIZE_T size = GlobalSize(hglobal);
452 const void *pv = GlobalLock(hglobal);
453 rot_entry->object = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(MInterfacePointer, abData[size]));
454 rot_entry->object->ulCntData = size;
455 memcpy(rot_entry->object->abData, pv, size);
456 GlobalUnlock(hglobal);
457 }
458 }
459 IStream_Release(pStream);
460 if (hr != S_OK)
461 {
462 rot_entry_delete(rot_entry);
463 return hr;
464 }
465
466 hr = CreateBindCtx(0, &pbc);
467 if (FAILED(hr))
468 {
469 rot_entry_delete(rot_entry);
470 return hr;
471 }
472
473 hr = reduce_moniker(pmkObjectName, pbc, &pmkObjectName);
474 if (FAILED(hr))
475 {
476 rot_entry_delete(rot_entry);
477 IBindCtx_Release(pbc);
478 return hr;
479 }
480
481 hr = IMoniker_GetTimeOfLastChange(pmkObjectName, pbc, NULL,
482 &rot_entry->last_modified);
483 IBindCtx_Release(pbc);
484 if (FAILED(hr))
485 {
486 CoFileTimeNow(&rot_entry->last_modified);
487 hr = S_OK;
488 }
489
490 hr = get_moniker_comparison_data(pmkObjectName,
491 &rot_entry->moniker_data);
492 if (hr != S_OK)
493 {
494 rot_entry_delete(rot_entry);
495 IMoniker_Release(pmkObjectName);
496 return hr;
497 }
498
499 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
500 if (hr != S_OK)
501 {
502 rot_entry_delete(rot_entry);
503 IMoniker_Release(pmkObjectName);
504 return hr;
505 }
506 /* marshal moniker */
507 hr = CoMarshalInterface(pStream, &IID_IMoniker, (IUnknown *)pmkObjectName,
508 MSHCTX_LOCAL | MSHCTX_NOSHAREDMEM, NULL, MSHLFLAGS_TABLESTRONG);
509 /* FIXME: a cleaner way would be to create an IStream class that writes
510 * directly to an MInterfacePointer */
511 if (hr == S_OK)
512 {
513 HGLOBAL hglobal;
514 hr = GetHGlobalFromStream(pStream, &hglobal);
515 if (hr == S_OK)
516 {
517 SIZE_T size = GlobalSize(hglobal);
518 const void *pv = GlobalLock(hglobal);
519 moniker = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(InterfaceData, abData[size]));
520 moniker->ulCntData = size;
521 memcpy(moniker->abData, pv, size);
522 GlobalUnlock(hglobal);
523 }
524 }
525 IStream_Release(pStream);
526 IMoniker_Release(pmkObjectName);
527 if (hr != S_OK)
528 {
529 HeapFree(GetProcessHeap(), 0, moniker);
530 rot_entry_delete(rot_entry);
531 return hr;
532 }
533
534
535 while (TRUE)
536 {
537 __TRY
538 {
539 hr = IrotRegister(get_irot_handle(), rot_entry->moniker_data,
540 rot_entry->object, moniker,
541 &rot_entry->last_modified, grfFlags,
542 &rot_entry->cookie, &rot_entry->ctxt_handle);
543 }
544 __EXCEPT(rpc_filter)
545 {
546 hr = HRESULT_FROM_WIN32(GetExceptionCode());
547 }
548 __ENDTRY
549 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
550 {
551 if (start_rpcss())
552 continue;
553 }
554 break;
555 }
556 HeapFree(GetProcessHeap(), 0, moniker);
557 if (FAILED(hr))
558 {
559 rot_entry_delete(rot_entry);
560 return hr;
561 }
562
563 /* gives a registration identifier to the registered object*/
564 *pdwRegister = rot_entry->cookie;
565
566 EnterCriticalSection(&This->lock);
567 list_add_tail(&This->rot, &rot_entry->entry);
568 LeaveCriticalSection(&This->lock);
569
570 return hr;
571 }
572
573 /***********************************************************************
574 * RunningObjectTable_Revoke
575 *
576 * PARAMS
577 * dwRegister [in] Value identifying registration to be revoked
578 */
579 static HRESULT WINAPI
580 RunningObjectTableImpl_Revoke( IRunningObjectTable* iface, DWORD dwRegister)
581 {
582 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
583 struct rot_entry *rot_entry;
584
585 TRACE("(%p,%d)\n",This,dwRegister);
586
587 EnterCriticalSection(&This->lock);
588 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, struct rot_entry, entry)
589 {
590 if (rot_entry->cookie == dwRegister)
591 {
592 list_remove(&rot_entry->entry);
593 LeaveCriticalSection(&This->lock);
594
595 rot_entry_delete(rot_entry);
596 return S_OK;
597 }
598 }
599 LeaveCriticalSection(&This->lock);
600
601 return E_INVALIDARG;
602 }
603
604 /***********************************************************************
605 * RunningObjectTable_IsRunning
606 *
607 * PARAMS
608 * pmkObjectName [in] moniker of the object whose status is desired
609 */
610 static HRESULT WINAPI
611 RunningObjectTableImpl_IsRunning( IRunningObjectTable* iface, IMoniker *pmkObjectName)
612 {
613 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
614 MonikerComparisonData *moniker_data;
615 HRESULT hr;
616 const struct rot_entry *rot_entry;
617
618 TRACE("(%p,%p)\n",This,pmkObjectName);
619
620 hr = reduce_moniker(pmkObjectName, NULL, &pmkObjectName);
621 if (FAILED(hr))
622 return hr;
623 hr = get_moniker_comparison_data(pmkObjectName, &moniker_data);
624 IMoniker_Release(pmkObjectName);
625 if (hr != S_OK)
626 return hr;
627
628 hr = S_FALSE;
629 EnterCriticalSection(&This->lock);
630 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, const struct rot_entry, entry)
631 {
632 if ((rot_entry->moniker_data->ulCntData == moniker_data->ulCntData) &&
633 !memcmp(moniker_data->abData, rot_entry->moniker_data->abData, moniker_data->ulCntData))
634 {
635 hr = S_OK;
636 break;
637 }
638 }
639 LeaveCriticalSection(&This->lock);
640
641 if (hr == S_FALSE)
642 {
643 while (TRUE)
644 {
645 __TRY
646 {
647 hr = IrotIsRunning(get_irot_handle(), moniker_data);
648 }
649 __EXCEPT(rpc_filter)
650 {
651 hr = HRESULT_FROM_WIN32(GetExceptionCode());
652 }
653 __ENDTRY
654 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
655 {
656 if (start_rpcss())
657 continue;
658 }
659 break;
660 }
661 }
662
663 HeapFree(GetProcessHeap(), 0, moniker_data);
664
665 return hr;
666 }
667
668 /***********************************************************************
669 * RunningObjectTable_GetObject
670 *
671 * PARAMS
672 * pmkObjectName [in] Pointer to the moniker on the object
673 * ppunkObject [out] variable that receives the IUnknown interface pointer
674 */
675 static HRESULT WINAPI
676 RunningObjectTableImpl_GetObject( IRunningObjectTable* iface,
677 IMoniker *pmkObjectName, IUnknown **ppunkObject)
678 {
679 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
680 MonikerComparisonData *moniker_data;
681 InterfaceData *object = NULL;
682 IrotCookie cookie;
683 HRESULT hr;
684 struct rot_entry *rot_entry;
685
686 TRACE("(%p,%p,%p)\n",This,pmkObjectName,ppunkObject);
687
688 if (ppunkObject == NULL)
689 return E_POINTER;
690
691 *ppunkObject = NULL;
692
693 hr = reduce_moniker(pmkObjectName, NULL, &pmkObjectName);
694 if (FAILED(hr))
695 return hr;
696 hr = get_moniker_comparison_data(pmkObjectName, &moniker_data);
697 IMoniker_Release(pmkObjectName);
698 if (hr != S_OK)
699 return hr;
700
701 EnterCriticalSection(&This->lock);
702 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, struct rot_entry, entry)
703 {
704 if ((rot_entry->moniker_data->ulCntData == moniker_data->ulCntData) &&
705 !memcmp(moniker_data->abData, rot_entry->moniker_data->abData, moniker_data->ulCntData))
706 {
707 IStream *pStream;
708 hr = create_stream_on_mip_ro(rot_entry->object, &pStream);
709 if (hr == S_OK)
710 {
711 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)ppunkObject);
712 IStream_Release(pStream);
713 }
714
715 LeaveCriticalSection(&This->lock);
716 HeapFree(GetProcessHeap(), 0, moniker_data);
717
718 return hr;
719 }
720 }
721 LeaveCriticalSection(&This->lock);
722
723 TRACE("moniker unavailable locally, calling SCM\n");
724
725 while (TRUE)
726 {
727 __TRY
728 {
729 hr = IrotGetObject(get_irot_handle(), moniker_data, &object, &cookie);
730 }
731 __EXCEPT(rpc_filter)
732 {
733 hr = HRESULT_FROM_WIN32(GetExceptionCode());
734 }
735 __ENDTRY
736 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
737 {
738 if (start_rpcss())
739 continue;
740 }
741 break;
742 }
743
744 if (SUCCEEDED(hr))
745 {
746 IStream *pStream;
747 hr = create_stream_on_mip_ro(object, &pStream);
748 if (hr == S_OK)
749 {
750 hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)ppunkObject);
751 IStream_Release(pStream);
752 }
753 }
754 else
755 WARN("Moniker unavailable, IrotGetObject returned 0x%08x\n", hr);
756
757 HeapFree(GetProcessHeap(), 0, moniker_data);
758
759 return hr;
760 }
761
762 /***********************************************************************
763 * RunningObjectTable_NoteChangeTime
764 *
765 * PARAMS
766 * dwRegister [in] Value identifying registration being updated
767 * pfiletime [in] Pointer to structure containing object's last change time
768 */
769 static HRESULT WINAPI
770 RunningObjectTableImpl_NoteChangeTime(IRunningObjectTable* iface,
771 DWORD dwRegister, FILETIME *pfiletime)
772 {
773 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
774 struct rot_entry *rot_entry;
775 HRESULT hr = E_INVALIDARG;
776
777 TRACE("(%p,%d,%p)\n",This,dwRegister,pfiletime);
778
779 EnterCriticalSection(&This->lock);
780 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, struct rot_entry, entry)
781 {
782 if (rot_entry->cookie == dwRegister)
783 {
784 rot_entry->last_modified = *pfiletime;
785 LeaveCriticalSection(&This->lock);
786
787 while (TRUE)
788 {
789 __TRY
790 {
791 hr = IrotNoteChangeTime(get_irot_handle(), dwRegister, pfiletime);
792 }
793 __EXCEPT(rpc_filter)
794 {
795 hr = HRESULT_FROM_WIN32(GetExceptionCode());
796 }
797 __ENDTRY
798 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
799 {
800 if (start_rpcss())
801 continue;
802 }
803 break;
804 }
805
806 goto done;
807 }
808 }
809 LeaveCriticalSection(&This->lock);
810
811 done:
812 TRACE("-- 0x08%x\n", hr);
813 return hr;
814 }
815
816 /***********************************************************************
817 * RunningObjectTable_GetTimeOfLastChange
818 *
819 * PARAMS
820 * pmkObjectName [in] moniker of the object whose status is desired
821 * pfiletime [out] structure that receives object's last change time
822 */
823 static HRESULT WINAPI
824 RunningObjectTableImpl_GetTimeOfLastChange(IRunningObjectTable* iface,
825 IMoniker *pmkObjectName, FILETIME *pfiletime)
826 {
827 HRESULT hr = MK_E_UNAVAILABLE;
828 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
829 MonikerComparisonData *moniker_data;
830 const struct rot_entry *rot_entry;
831
832 TRACE("(%p,%p,%p)\n",This,pmkObjectName,pfiletime);
833
834 if (pmkObjectName==NULL || pfiletime==NULL)
835 return E_INVALIDARG;
836
837 hr = reduce_moniker(pmkObjectName, NULL, &pmkObjectName);
838 if (FAILED(hr))
839 return hr;
840 hr = get_moniker_comparison_data(pmkObjectName, &moniker_data);
841 IMoniker_Release(pmkObjectName);
842 if (hr != S_OK)
843 return hr;
844
845 hr = MK_E_UNAVAILABLE;
846
847 EnterCriticalSection(&This->lock);
848 LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, const struct rot_entry, entry)
849 {
850 if ((rot_entry->moniker_data->ulCntData == moniker_data->ulCntData) &&
851 !memcmp(moniker_data->abData, rot_entry->moniker_data->abData, moniker_data->ulCntData))
852 {
853 *pfiletime = rot_entry->last_modified;
854 hr = S_OK;
855 break;
856 }
857 }
858 LeaveCriticalSection(&This->lock);
859
860 if (hr != S_OK)
861 {
862 while (TRUE)
863 {
864 __TRY
865 {
866 hr = IrotGetTimeOfLastChange(get_irot_handle(), moniker_data, pfiletime);
867 }
868 __EXCEPT(rpc_filter)
869 {
870 hr = HRESULT_FROM_WIN32(GetExceptionCode());
871 }
872 __ENDTRY
873 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
874 {
875 if (start_rpcss())
876 continue;
877 }
878 break;
879 }
880 }
881
882 HeapFree(GetProcessHeap(), 0, moniker_data);
883
884 TRACE("-- 0x%08x\n", hr);
885 return hr;
886 }
887
888 /***********************************************************************
889 * RunningObjectTable_EnumRunning
890 *
891 * PARAMS
892 * ppenumMoniker [out] receives the IEnumMoniker interface pointer
893 */
894 static HRESULT WINAPI
895 RunningObjectTableImpl_EnumRunning(IRunningObjectTable* iface,
896 IEnumMoniker **ppenumMoniker)
897 {
898 RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
899 InterfaceList *interface_list = NULL;
900 HRESULT hr;
901
902 TRACE("(%p, %p)\n", This, ppenumMoniker);
903
904 *ppenumMoniker = NULL;
905
906 while (TRUE)
907 {
908 __TRY
909 {
910 hr = IrotEnumRunning(get_irot_handle(), &interface_list);
911 }
912 __EXCEPT(rpc_filter)
913 {
914 hr = HRESULT_FROM_WIN32(GetExceptionCode());
915 }
916 __ENDTRY
917 if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
918 {
919 if (start_rpcss())
920 continue;
921 }
922 break;
923 }
924
925 if (SUCCEEDED(hr))
926 hr = EnumMonikerImpl_CreateEnumROTMoniker(interface_list,
927 0, ppenumMoniker);
928
929 return hr;
930 }
931
932 /* Virtual function table for the IRunningObjectTable class. */
933 static const IRunningObjectTableVtbl VT_RunningObjectTableImpl =
934 {
935 RunningObjectTableImpl_QueryInterface,
936 RunningObjectTableImpl_AddRef,
937 RunningObjectTableImpl_Release,
938 RunningObjectTableImpl_Register,
939 RunningObjectTableImpl_Revoke,
940 RunningObjectTableImpl_IsRunning,
941 RunningObjectTableImpl_GetObject,
942 RunningObjectTableImpl_NoteChangeTime,
943 RunningObjectTableImpl_GetTimeOfLastChange,
944 RunningObjectTableImpl_EnumRunning
945 };
946
947 /***********************************************************************
948 * RunningObjectTable_Initialize
949 */
950 HRESULT WINAPI RunningObjectTableImpl_Initialize(void)
951 {
952 TRACE("\n");
953
954 /* create the unique instance of the RunningObjectTableImpl structure */
955 runningObjectTableInstance = HeapAlloc(GetProcessHeap(), 0, sizeof(RunningObjectTableImpl));
956
957 if (!runningObjectTableInstance)
958 return E_OUTOFMEMORY;
959
960 /* initialize the virtual table function */
961 runningObjectTableInstance->lpVtbl = &VT_RunningObjectTableImpl;
962
963 /* the initial reference is set to "1" so that it isn't destroyed after its
964 * first use until the process is destroyed, as the running object table is
965 * a process-wide cache of a global table */
966 runningObjectTableInstance->ref = 1;
967
968 list_init(&runningObjectTableInstance->rot);
969 InitializeCriticalSection(&runningObjectTableInstance->lock);
970 DEBUG_SET_CRITSEC_NAME(&runningObjectTableInstance->lock, "RunningObjectTableImpl.lock");
971
972 return S_OK;
973 }
974
975 /***********************************************************************
976 * RunningObjectTable_UnInitialize
977 */
978 HRESULT WINAPI RunningObjectTableImpl_UnInitialize(void)
979 {
980 TRACE("\n");
981
982 if (runningObjectTableInstance==NULL)
983 return E_POINTER;
984
985 RunningObjectTableImpl_Release((IRunningObjectTable*)runningObjectTableInstance);
986
987 RunningObjectTableImpl_Destroy();
988
989 return S_OK;
990 }
991
992 /***********************************************************************
993 * GetRunningObjectTable (OLE32.@)
994 *
995 * Retrieves the global running object table.
996 *
997 * PARAMS
998 * reserved [I] Reserved. Set to 0.
999 * pprot [O] Address that receives the pointer to the running object table.
1000 *
1001 * RETURNS
1002 * Success: S_OK.
1003 * Failure: Any HRESULT code.
1004 */
1005 HRESULT WINAPI
1006 GetRunningObjectTable(DWORD reserved, LPRUNNINGOBJECTTABLE *pprot)
1007 {
1008 IID riid=IID_IRunningObjectTable;
1009 HRESULT res;
1010
1011 TRACE("()\n");
1012
1013 if (reserved!=0)
1014 return E_UNEXPECTED;
1015
1016 if(runningObjectTableInstance==NULL)
1017 return CO_E_NOTINITIALIZED;
1018
1019 res = IRunningObjectTable_QueryInterface((IRunningObjectTable*)runningObjectTableInstance,&riid,(void**)pprot);
1020
1021 return res;
1022 }
1023
1024 static HRESULT get_moniker_for_progid_display_name(LPBC pbc,
1025 LPCOLESTR szDisplayName,
1026 LPDWORD pchEaten,
1027 LPMONIKER *ppmk)
1028 {
1029 CLSID clsid;
1030 HRESULT hr;
1031 LPWSTR progid;
1032 LPCWSTR start = szDisplayName;
1033 LPCWSTR end;
1034 int len;
1035 IMoniker *class_moniker;
1036
1037 if (*start == '@')
1038 start++;
1039
1040 /* find end delimiter */
1041 for (end = start; *end; end++)
1042 if (*end == ':')
1043 break;
1044
1045 len = end - start;
1046
1047 /* must start with '@' or have a ':' somewhere and mustn't be one character
1048 * long (since that looks like an absolute path) */
1049 if (((start == szDisplayName) && (*end == '\0')) || (len <= 1))
1050 return MK_E_SYNTAX;
1051
1052 progid = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1053 if (progid)
1054 {
1055 memcpy(progid, start, len * sizeof(WCHAR));
1056 progid[len] = '\0';
1057 }
1058 hr = CLSIDFromProgID(progid, &clsid);
1059 HeapFree(GetProcessHeap(), 0, progid);
1060 if (FAILED(hr))
1061 return MK_E_SYNTAX;
1062
1063 hr = CreateClassMoniker(&clsid, &class_moniker);
1064 if (SUCCEEDED(hr))
1065 {
1066 IParseDisplayName *pdn;
1067 hr = IMoniker_BindToObject(class_moniker, pbc, NULL,
1068 &IID_IParseDisplayName, (void **)&pdn);
1069 /* fallback to using IClassFactory to get IParseDisplayName -
1070 * adsldp.dll depends on this */
1071 if (FAILED(hr))
1072 {
1073 IClassFactory *pcf;
1074 hr = IMoniker_BindToObject(class_moniker, pbc, NULL,
1075 &IID_IClassFactory, (void **)&pcf);
1076 if (SUCCEEDED(hr))
1077 {
1078 hr = IClassFactory_CreateInstance(pcf, NULL,
1079 &IID_IParseDisplayName,
1080 (void **)&pdn);
1081 IClassFactory_Release(pcf);
1082 }
1083 }
1084 IMoniker_Release(class_moniker);
1085 if (SUCCEEDED(hr))
1086 {
1087 hr = IParseDisplayName_ParseDisplayName(pdn, pbc,
1088 (LPOLESTR)szDisplayName,
1089 pchEaten, ppmk);
1090 IParseDisplayName_Release(pdn);
1091 }
1092 }
1093 return hr;
1094 }
1095
1096 /******************************************************************************
1097 * MkParseDisplayName [OLE32.@]
1098 */
1099 HRESULT WINAPI MkParseDisplayName(LPBC pbc, LPCOLESTR szDisplayName,
1100 LPDWORD pchEaten, LPMONIKER *ppmk)
1101 {
1102 HRESULT hr = MK_E_SYNTAX;
1103 static const WCHAR wszClsidColon[] = {'c','l','s','i','d',':'};
1104 IMoniker *moniker;
1105 DWORD chEaten;
1106
1107 TRACE("(%p, %s, %p, %p)\n", pbc, debugstr_w(szDisplayName), pchEaten, ppmk);
1108
1109 if (!(IsValidInterface((LPUNKNOWN) pbc)))
1110 return E_INVALIDARG;
1111
1112 *pchEaten = 0;
1113 *ppmk = NULL;
1114
1115 if (!strncmpiW(szDisplayName, wszClsidColon, sizeof(wszClsidColon)/sizeof(wszClsidColon[0])))
1116 {
1117 hr = ClassMoniker_CreateFromDisplayName(pbc, szDisplayName, &chEaten, &moniker);
1118 if (FAILED(hr) && (hr != MK_E_SYNTAX))
1119 return hr;
1120 }
1121 else
1122 {
1123 hr = get_moniker_for_progid_display_name(pbc, szDisplayName, &chEaten, &moniker);
1124 if (FAILED(hr) && (hr != MK_E_SYNTAX))
1125 return hr;
1126 }
1127
1128 if (FAILED(hr))
1129 {
1130 hr = FileMoniker_CreateFromDisplayName(pbc, szDisplayName, &chEaten, &moniker);
1131 if (FAILED(hr) && (hr != MK_E_SYNTAX))
1132 return hr;
1133 }
1134
1135 if (SUCCEEDED(hr))
1136 {
1137 while (TRUE)
1138 {
1139 IMoniker *next_moniker;
1140 *pchEaten += chEaten;
1141 szDisplayName += chEaten;
1142 if (!*szDisplayName)
1143 {
1144 *ppmk = moniker;
1145 return S_OK;
1146 }
1147 chEaten = 0;
1148 hr = IMoniker_ParseDisplayName(moniker, pbc, NULL,
1149 (LPOLESTR)szDisplayName, &chEaten,
1150 &next_moniker);
1151 IMoniker_Release(moniker);
1152 if (FAILED(hr))
1153 {
1154 *pchEaten = 0;
1155 break;
1156 }
1157 moniker = next_moniker;
1158 }
1159 }
1160
1161 return hr;
1162 }
1163
1164 /***********************************************************************
1165 * GetClassFile (OLE32.@)
1166 *
1167 * Retrieves the class ID associated with the given filename.
1168 *
1169 * PARAMS
1170 * filePathName [I] Filename to retrieve the class ID for.
1171 * pclsid [O] Address that receives the class ID for the file.
1172 *
1173 * RETURNS
1174 * Success: S_OK.
1175 * Failure: Any HRESULT code.
1176 */
1177 HRESULT WINAPI GetClassFile(LPCOLESTR filePathName,CLSID *pclsid)
1178 {
1179 IStorage *pstg=0;
1180 HRESULT res;
1181 int nbElm, length, i;
1182 LONG sizeProgId;
1183 LPOLESTR *pathDec=0,absFile=0,progId=0;
1184 LPWSTR extension;
1185 static const WCHAR bkslashW[] = {'\\',0};
1186 static const WCHAR dotW[] = {'.',0};
1187
1188 TRACE("%s, %p\n", debugstr_w(filePathName), pclsid);
1189
1190 /* if the file contain a storage object the return the CLSID written by IStorage_SetClass method*/
1191 if((StgIsStorageFile(filePathName))==S_OK){
1192
1193 res=StgOpenStorage(filePathName,NULL,STGM_READ | STGM_SHARE_DENY_WRITE,NULL,0,&pstg);
1194
1195 if (SUCCEEDED(res))
1196 res=ReadClassStg(pstg,pclsid);
1197
1198 IStorage_Release(pstg);
1199
1200 return res;
1201 }
1202 /* If the file is not a storage object then attempt to match various bits in the file against a
1203 pattern in the registry. This case is not frequently used, so I present only the pseudocode for
1204 this case.
1205
1206 for(i=0;i<nFileTypes;i++)
1207
1208 for(i=0;j<nPatternsForType;j++){
1209
1210 PATTERN pat;
1211 HANDLE hFile;
1212
1213 pat=ReadPatternFromRegistry(i,j);
1214 hFile=CreateFileW(filePathName,,,,,,hFile);
1215 SetFilePosition(hFile,pat.offset);
1216 ReadFile(hFile,buf,pat.size,&r,NULL);
1217 if (memcmp(buf&pat.mask,pat.pattern.pat.size)==0){
1218
1219 *pclsid=ReadCLSIDFromRegistry(i);
1220 return S_OK;
1221 }
1222 }
1223 */
1224
1225 /* if the above strategies fail then search for the extension key in the registry */
1226
1227 /* get the last element (absolute file) in the path name */
1228 nbElm=FileMonikerImpl_DecomposePath(filePathName,&pathDec);
1229 absFile=pathDec[nbElm-1];
1230
1231 /* failed if the path represents a directory and not an absolute file name*/
1232 if (!lstrcmpW(absFile, bkslashW))
1233 return MK_E_INVALIDEXTENSION;
1234
1235 /* get the extension of the file */
1236 extension = NULL;
1237 length=lstrlenW(absFile);
1238 for(i = length-1; (i >= 0) && *(extension = &absFile[i]) != '.'; i--)
1239 /* nothing */;
1240
1241 if (!extension || !lstrcmpW(extension, dotW))
1242 return MK_E_INVALIDEXTENSION;
1243
1244 res=RegQueryValueW(HKEY_CLASSES_ROOT, extension, NULL, &sizeProgId);
1245
1246 /* get the progId associated to the extension */
1247 progId = CoTaskMemAlloc(sizeProgId);
1248 res = RegQueryValueW(HKEY_CLASSES_ROOT, extension, progId, &sizeProgId);
1249
1250 if (res==ERROR_SUCCESS)
1251 /* return the clsid associated to the progId */
1252 res= CLSIDFromProgID(progId,pclsid);
1253
1254 for(i=0; pathDec[i]!=NULL;i++)
1255 CoTaskMemFree(pathDec[i]);
1256 CoTaskMemFree(pathDec);
1257
1258 CoTaskMemFree(progId);
1259
1260 if (res==ERROR_SUCCESS)
1261 return res;
1262
1263 return MK_E_INVALIDEXTENSION;
1264 }
1265
1266 /***********************************************************************
1267 * EnumMoniker_QueryInterface
1268 */
1269 static HRESULT WINAPI EnumMonikerImpl_QueryInterface(IEnumMoniker* iface,REFIID riid,void** ppvObject)
1270 {
1271 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1272
1273 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
1274
1275 /* validate arguments */
1276 if (ppvObject == NULL)
1277 return E_INVALIDARG;
1278
1279 *ppvObject = NULL;
1280
1281 if (IsEqualIID(&IID_IUnknown, riid))
1282 *ppvObject = This;
1283 else
1284 if (IsEqualIID(&IID_IEnumMoniker, riid))
1285 *ppvObject = This;
1286
1287 if ((*ppvObject)==NULL)
1288 return E_NOINTERFACE;
1289
1290 IEnumMoniker_AddRef(iface);
1291
1292 return S_OK;
1293 }
1294
1295 /***********************************************************************
1296 * EnumMoniker_AddRef
1297 */
1298 static ULONG WINAPI EnumMonikerImpl_AddRef(IEnumMoniker* iface)
1299 {
1300 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1301
1302 TRACE("(%p)\n",This);
1303
1304 return InterlockedIncrement(&This->ref);
1305 }
1306
1307 /***********************************************************************
1308 * EnumMoniker_release
1309 */
1310 static ULONG WINAPI EnumMonikerImpl_Release(IEnumMoniker* iface)
1311 {
1312 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1313 ULONG ref;
1314
1315 TRACE("(%p)\n",This);
1316
1317 ref = InterlockedDecrement(&This->ref);
1318
1319 /* uninitialize rot structure if there's no more reference to it*/
1320 if (ref == 0)
1321 {
1322 ULONG i;
1323
1324 TRACE("(%p) Deleting\n",This);
1325
1326 for (i = 0; i < This->moniker_list->size; i++)
1327 HeapFree(GetProcessHeap(), 0, This->moniker_list->interfaces[i]);
1328 HeapFree(GetProcessHeap(), 0, This->moniker_list);
1329 HeapFree(GetProcessHeap(), 0, This);
1330 }
1331
1332 return ref;
1333 }
1334 /***********************************************************************
1335 * EnumMoniker_Next
1336 */
1337 static HRESULT WINAPI EnumMonikerImpl_Next(IEnumMoniker* iface, ULONG celt, IMoniker** rgelt, ULONG * pceltFetched)
1338 {
1339 ULONG i;
1340 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1341 HRESULT hr = S_OK;
1342
1343 TRACE("(%p) TabCurrentPos %d Tablastindx %d\n", This, This->pos, This->moniker_list->size);
1344
1345 /* retrieve the requested number of moniker from the current position */
1346 for(i = 0; (This->pos < This->moniker_list->size) && (i < celt); i++)
1347 {
1348 IStream *stream;
1349 hr = create_stream_on_mip_ro(This->moniker_list->interfaces[This->pos++], &stream);
1350 if (hr != S_OK) break;
1351 hr = CoUnmarshalInterface(stream, &IID_IMoniker, (void **)&rgelt[i]);
1352 IStream_Release(stream);
1353 if (hr != S_OK) break;
1354 }
1355
1356 if (pceltFetched != NULL)
1357 *pceltFetched= i;
1358
1359 if (hr != S_OK)
1360 return hr;
1361
1362 if (i == celt)
1363 return S_OK;
1364 else
1365 return S_FALSE;
1366
1367 }
1368
1369 /***********************************************************************
1370 * EnumMoniker_Skip
1371 */
1372 static HRESULT WINAPI EnumMonikerImpl_Skip(IEnumMoniker* iface, ULONG celt)
1373 {
1374 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1375
1376 TRACE("(%p)\n",This);
1377
1378 if (This->pos + celt >= This->moniker_list->size)
1379 return S_FALSE;
1380
1381 This->pos += celt;
1382
1383 return S_OK;
1384 }
1385
1386 /***********************************************************************
1387 * EnumMoniker_Reset
1388 */
1389 static HRESULT WINAPI EnumMonikerImpl_Reset(IEnumMoniker* iface)
1390 {
1391 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1392
1393 This->pos = 0; /* set back to start of list */
1394
1395 TRACE("(%p)\n",This);
1396
1397 return S_OK;
1398 }
1399
1400 /***********************************************************************
1401 * EnumMoniker_Clone
1402 */
1403 static HRESULT WINAPI EnumMonikerImpl_Clone(IEnumMoniker* iface, IEnumMoniker ** ppenum)
1404 {
1405 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1406 InterfaceList *moniker_list;
1407 ULONG i;
1408
1409 TRACE("(%p)\n",This);
1410
1411 *ppenum = NULL;
1412
1413 moniker_list = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(InterfaceList, interfaces[This->moniker_list->size]));
1414 if (!moniker_list)
1415 return E_OUTOFMEMORY;
1416
1417 moniker_list->size = This->moniker_list->size;
1418 for (i = 0; i < This->moniker_list->size; i++)
1419 {
1420 SIZE_T size = FIELD_OFFSET(InterfaceData, abData[This->moniker_list->interfaces[i]->ulCntData]);
1421 moniker_list->interfaces[i] = HeapAlloc(GetProcessHeap(), 0, size);
1422 if (!moniker_list->interfaces[i])
1423 {
1424 ULONG end = i;
1425 for (i = 0; i < end; i++)
1426 HeapFree(GetProcessHeap(), 0, moniker_list->interfaces[i]);
1427 HeapFree(GetProcessHeap(), 0, moniker_list);
1428 return E_OUTOFMEMORY;
1429 }
1430 memcpy(moniker_list->interfaces[i], This->moniker_list->interfaces[i], size);
1431 }
1432
1433 /* copy the enum structure */
1434 return EnumMonikerImpl_CreateEnumROTMoniker(moniker_list, This->pos, ppenum);
1435 }
1436
1437 /* Virtual function table for the IEnumMoniker class. */
1438 static const IEnumMonikerVtbl VT_EnumMonikerImpl =
1439 {
1440 EnumMonikerImpl_QueryInterface,
1441 EnumMonikerImpl_AddRef,
1442 EnumMonikerImpl_Release,
1443 EnumMonikerImpl_Next,
1444 EnumMonikerImpl_Skip,
1445 EnumMonikerImpl_Reset,
1446 EnumMonikerImpl_Clone
1447 };
1448
1449 /***********************************************************************
1450 * EnumMonikerImpl_CreateEnumROTMoniker
1451 * Used by EnumRunning to create the structure and EnumClone
1452 * to copy the structure
1453 */
1454 static HRESULT EnumMonikerImpl_CreateEnumROTMoniker(InterfaceList *moniker_list,
1455 ULONG current_pos,
1456 IEnumMoniker **ppenumMoniker)
1457 {
1458 EnumMonikerImpl* This = NULL;
1459
1460 if (!ppenumMoniker)
1461 return E_INVALIDARG;
1462
1463 This = HeapAlloc(GetProcessHeap(), 0, sizeof(EnumMonikerImpl));
1464 if (!This) return E_OUTOFMEMORY;
1465
1466 TRACE("(%p)\n", This);
1467
1468 /* initialize the virtual table function */
1469 This->lpVtbl = &VT_EnumMonikerImpl;
1470
1471 /* the initial reference is set to "1" */
1472 This->ref = 1; /* set the ref count to one */
1473 This->pos = current_pos; /* Set the list start posn */
1474 This->moniker_list = moniker_list;
1475
1476 *ppenumMoniker = (IEnumMoniker*)This;
1477
1478 return S_OK;
1479 }
1480
1481
1482 /* Shared implementation of moniker marshaler based on saving and loading of
1483 * monikers */
1484
1485 typedef struct MonikerMarshal
1486 {
1487 const IUnknownVtbl *lpVtbl;
1488 const IMarshalVtbl *lpVtblMarshal;
1489
1490 LONG ref;
1491 IMoniker *moniker;
1492 } MonikerMarshal;
1493
1494 static inline MonikerMarshal *impl_from_IMarshal( IMarshal *iface )
1495 {
1496 return (MonikerMarshal *)((char*)iface - FIELD_OFFSET(MonikerMarshal, lpVtblMarshal));
1497 }
1498
1499 static HRESULT WINAPI MonikerMarshalInner_QueryInterface(IUnknown *iface, REFIID riid, LPVOID *ppv)
1500 {
1501 MonikerMarshal *This = (MonikerMarshal *)iface;
1502 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
1503 *ppv = NULL;
1504 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IMarshal, riid))
1505 {
1506 *ppv = &This->lpVtblMarshal;
1507 IUnknown_AddRef((IUnknown *)&This->lpVtblMarshal);
1508 return S_OK;
1509 }
1510 FIXME("No interface for %s\n", debugstr_guid(riid));
1511 return E_NOINTERFACE;
1512 }
1513
1514 static ULONG WINAPI MonikerMarshalInner_AddRef(IUnknown *iface)
1515 {
1516 MonikerMarshal *This = (MonikerMarshal *)iface;
1517 return InterlockedIncrement(&This->ref);
1518 }
1519
1520 static ULONG WINAPI MonikerMarshalInner_Release(IUnknown *iface)
1521 {
1522 MonikerMarshal *This = (MonikerMarshal *)iface;
1523 ULONG ref = InterlockedDecrement(&This->ref);
1524
1525 if (!ref) HeapFree(GetProcessHeap(), 0, This);
1526 return ref;
1527 }
1528
1529 static const IUnknownVtbl VT_MonikerMarshalInner =
1530 {
1531 MonikerMarshalInner_QueryInterface,
1532 MonikerMarshalInner_AddRef,
1533 MonikerMarshalInner_Release
1534 };
1535
1536 static HRESULT WINAPI MonikerMarshal_QueryInterface(IMarshal *iface, REFIID riid, LPVOID *ppv)
1537 {
1538 MonikerMarshal *This = impl_from_IMarshal(iface);
1539 return IMoniker_QueryInterface(This->moniker, riid, ppv);
1540 }
1541
1542 static ULONG WINAPI MonikerMarshal_AddRef(IMarshal *iface)
1543 {
1544 MonikerMarshal *This = impl_from_IMarshal(iface);
1545 return IMoniker_AddRef(This->moniker);
1546 }
1547
1548 static ULONG WINAPI MonikerMarshal_Release(IMarshal *iface)
1549 {
1550 MonikerMarshal *This = impl_from_IMarshal(iface);
1551 return IMoniker_Release(This->moniker);
1552 }
1553
1554 static HRESULT WINAPI MonikerMarshal_GetUnmarshalClass(
1555 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
1556 void* pvDestContext, DWORD mshlflags, CLSID* pCid)
1557 {
1558 MonikerMarshal *This = impl_from_IMarshal(iface);
1559
1560 TRACE("(%s, %p, %x, %p, %x, %p)\n", debugstr_guid(riid), pv,
1561 dwDestContext, pvDestContext, mshlflags, pCid);
1562
1563 return IMoniker_GetClassID(This->moniker, pCid);
1564 }
1565
1566 static HRESULT WINAPI MonikerMarshal_GetMarshalSizeMax(
1567 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
1568 void* pvDestContext, DWORD mshlflags, DWORD* pSize)
1569 {
1570 MonikerMarshal *This = impl_from_IMarshal(iface);
1571 HRESULT hr;
1572 ULARGE_INTEGER size;
1573
1574 TRACE("(%s, %p, %x, %p, %x, %p)\n", debugstr_guid(riid), pv,
1575 dwDestContext, pvDestContext, mshlflags, pSize);
1576
1577 hr = IMoniker_GetSizeMax(This->moniker, &size);
1578 if (hr == S_OK)
1579 *pSize = (DWORD)size.QuadPart;
1580 return hr;
1581 }
1582
1583 static HRESULT WINAPI MonikerMarshal_MarshalInterface(LPMARSHAL iface, IStream *pStm,
1584 REFIID riid, void* pv, DWORD dwDestContext,
1585 void* pvDestContext, DWORD mshlflags)
1586 {
1587 MonikerMarshal *This = impl_from_IMarshal(iface);
1588
1589 TRACE("(%p, %s, %p, %x, %p, %x)\n", pStm, debugstr_guid(riid), pv,
1590 dwDestContext, pvDestContext, mshlflags);
1591
1592 return IMoniker_Save(This->moniker, pStm, FALSE);
1593 }
1594
1595 static HRESULT WINAPI MonikerMarshal_UnmarshalInterface(LPMARSHAL iface, IStream *pStm, REFIID riid, void **ppv)
1596 {
1597 MonikerMarshal *This = impl_from_IMarshal(iface);
1598 HRESULT hr;
1599
1600 TRACE("(%p, %s, %p)\n", pStm, debugstr_guid(riid), ppv);
1601
1602 hr = IMoniker_Load(This->moniker, pStm);
1603 if (hr == S_OK)
1604 hr = IMoniker_QueryInterface(This->moniker, riid, ppv);
1605 return hr;
1606 }
1607
1608 static HRESULT WINAPI MonikerMarshal_ReleaseMarshalData(LPMARSHAL iface, IStream *pStm)
1609 {
1610 TRACE("()\n");
1611 /* can't release a state-based marshal as nothing on server side to
1612 * release */
1613 return S_OK;
1614 }
1615
1616 static HRESULT WINAPI MonikerMarshal_DisconnectObject(LPMARSHAL iface, DWORD dwReserved)
1617 {
1618 TRACE("()\n");
1619 /* can't disconnect a state-based marshal as nothing on server side to
1620 * disconnect from */
1621 return S_OK;
1622 }
1623
1624 static const IMarshalVtbl VT_MonikerMarshal =
1625 {
1626 MonikerMarshal_QueryInterface,
1627 MonikerMarshal_AddRef,
1628 MonikerMarshal_Release,
1629 MonikerMarshal_GetUnmarshalClass,
1630 MonikerMarshal_GetMarshalSizeMax,
1631 MonikerMarshal_MarshalInterface,
1632 MonikerMarshal_UnmarshalInterface,
1633 MonikerMarshal_ReleaseMarshalData,
1634 MonikerMarshal_DisconnectObject
1635 };
1636
1637 HRESULT MonikerMarshal_Create(IMoniker *inner, IUnknown **outer)
1638 {
1639 MonikerMarshal *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1640 if (!This) return E_OUTOFMEMORY;
1641
1642 This->lpVtbl = &VT_MonikerMarshalInner;
1643 This->lpVtblMarshal = &VT_MonikerMarshal;
1644 This->ref = 1;
1645 This->moniker = inner;
1646
1647 *outer = (IUnknown *)&This->lpVtbl;
1648 return S_OK;
1649 }
1650
1651 void * __RPC_USER MIDL_user_allocate(SIZE_T size)
1652 {
1653 return HeapAlloc(GetProcessHeap(), 0, size);
1654 }
1655
1656 void __RPC_USER MIDL_user_free(void *p)
1657 {
1658 HeapFree(GetProcessHeap(), 0, p);
1659 }