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