[OLE32] Sync with Wine Staging 2.9. CORE-13362
[reactos.git] / reactos / dll / win32 / ole32 / antimoniker.c
1 /*
2 * AntiMonikers implementation
3 *
4 * Copyright 1999 Noomen Hamza
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #include "precomp.h"
22
23 WINE_DEFAULT_DEBUG_CHANNEL(ole);
24
25 /* AntiMoniker data structure */
26 typedef struct AntiMonikerImpl{
27 IMoniker IMoniker_iface;
28 IROTData IROTData_iface;
29 LONG ref;
30 IUnknown *pMarshal; /* custom marshaler */
31 } AntiMonikerImpl;
32
33 static inline AntiMonikerImpl *impl_from_IMoniker(IMoniker *iface)
34 {
35 return CONTAINING_RECORD(iface, AntiMonikerImpl, IMoniker_iface);
36 }
37
38 static inline AntiMonikerImpl *impl_from_IROTData(IROTData *iface)
39 {
40 return CONTAINING_RECORD(iface, AntiMonikerImpl, IROTData_iface);
41 }
42
43
44 /*******************************************************************************
45 * AntiMoniker_QueryInterface
46 *******************************************************************************/
47 static HRESULT WINAPI
48 AntiMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject)
49 {
50 AntiMonikerImpl *This = impl_from_IMoniker(iface);
51
52 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppvObject);
53
54 /* Perform a sanity check on the parameters.*/
55 if ( ppvObject==0 )
56 return E_INVALIDARG;
57
58 /* Initialize the return parameter */
59 *ppvObject = 0;
60
61 /* Compare the riid with the interface IDs implemented by this object.*/
62 if (IsEqualIID(&IID_IUnknown, riid) ||
63 IsEqualIID(&IID_IPersist, riid) ||
64 IsEqualIID(&IID_IPersistStream, riid) ||
65 IsEqualIID(&IID_IMoniker, riid))
66 *ppvObject = iface;
67 else if (IsEqualIID(&IID_IROTData, riid))
68 *ppvObject = &This->IROTData_iface;
69 else if (IsEqualIID(&IID_IMarshal, riid))
70 {
71 HRESULT hr = S_OK;
72 if (!This->pMarshal)
73 hr = MonikerMarshal_Create(iface, &This->pMarshal);
74 if (hr != S_OK)
75 return hr;
76 return IUnknown_QueryInterface(This->pMarshal, riid, ppvObject);
77 }
78
79 /* Check that we obtained an interface.*/
80 if ((*ppvObject)==0)
81 return E_NOINTERFACE;
82
83 /* always increase the reference count by one when it is successful */
84 IMoniker_AddRef(iface);
85
86 return S_OK;
87 }
88
89 /******************************************************************************
90 * AntiMoniker_AddRef
91 ******************************************************************************/
92 static ULONG WINAPI
93 AntiMonikerImpl_AddRef(IMoniker* iface)
94 {
95 AntiMonikerImpl *This = impl_from_IMoniker(iface);
96
97 TRACE("(%p)\n",This);
98
99 return InterlockedIncrement(&This->ref);
100 }
101
102 /******************************************************************************
103 * AntiMoniker_Release
104 ******************************************************************************/
105 static ULONG WINAPI
106 AntiMonikerImpl_Release(IMoniker* iface)
107 {
108 AntiMonikerImpl *This = impl_from_IMoniker(iface);
109 ULONG ref;
110
111 TRACE("(%p)\n",This);
112
113 ref = InterlockedDecrement(&This->ref);
114
115 /* destroy the object if there are no more references to it */
116 if (ref == 0)
117 {
118 if (This->pMarshal) IUnknown_Release(This->pMarshal);
119 HeapFree(GetProcessHeap(),0,This);
120 }
121
122 return ref;
123 }
124
125 /******************************************************************************
126 * AntiMoniker_GetClassID
127 ******************************************************************************/
128 static HRESULT WINAPI
129 AntiMonikerImpl_GetClassID(IMoniker* iface,CLSID *pClassID)
130 {
131 TRACE("(%p,%p)\n",iface,pClassID);
132
133 if (pClassID==NULL)
134 return E_POINTER;
135
136 *pClassID = CLSID_AntiMoniker;
137
138 return S_OK;
139 }
140
141 /******************************************************************************
142 * AntiMoniker_IsDirty
143 ******************************************************************************/
144 static HRESULT WINAPI
145 AntiMonikerImpl_IsDirty(IMoniker* iface)
146 {
147 /* Note that the OLE-provided implementations of the IPersistStream::IsDirty
148 method in the OLE-provided moniker interfaces always return S_FALSE because
149 their internal state never changes. */
150
151 TRACE("(%p)\n",iface);
152
153 return S_FALSE;
154 }
155
156 /******************************************************************************
157 * AntiMoniker_Load
158 ******************************************************************************/
159 static HRESULT WINAPI
160 AntiMonikerImpl_Load(IMoniker* iface,IStream* pStm)
161 {
162 DWORD constant=1,dwbuffer;
163 HRESULT res;
164
165 /* data read by this function is only a DWORD constant (must be 1) ! */
166 res=IStream_Read(pStm,&dwbuffer,sizeof(DWORD),NULL);
167
168 if (SUCCEEDED(res)&& dwbuffer!=constant)
169 return E_FAIL;
170
171 return res;
172 }
173
174 /******************************************************************************
175 * AntiMoniker_Save
176 ******************************************************************************/
177 static HRESULT WINAPI
178 AntiMonikerImpl_Save(IMoniker* iface,IStream* pStm,BOOL fClearDirty)
179 {
180 static const DWORD constant = 1;
181 /* data written by this function is only a DWORD constant set to 1 ! */
182 return IStream_Write(pStm,&constant,sizeof(constant),NULL);
183 }
184
185 /******************************************************************************
186 * AntiMoniker_GetSizeMax
187 *
188 * PARAMS
189 * pcbSize [out] Pointer to size of stream needed to save object
190 ******************************************************************************/
191 static HRESULT WINAPI
192 AntiMonikerImpl_GetSizeMax(IMoniker* iface, ULARGE_INTEGER* pcbSize)
193 {
194 TRACE("(%p,%p)\n",iface,pcbSize);
195
196 if (!pcbSize)
197 return E_POINTER;
198
199 /* for more details see AntiMonikerImpl_Save comments */
200
201 /*
202 * Normally the sizemax must be sizeof DWORD, but
203 * I tested this function it usually return 16 bytes
204 * more than the number of bytes used by AntiMoniker::Save function
205 */
206 pcbSize->u.LowPart = sizeof(DWORD)+16;
207
208 pcbSize->u.HighPart=0;
209
210 return S_OK;
211 }
212
213 /******************************************************************************
214 * AntiMoniker_BindToObject
215 ******************************************************************************/
216 static HRESULT WINAPI
217 AntiMonikerImpl_BindToObject(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft,
218 REFIID riid, VOID** ppvResult)
219 {
220 TRACE("(%p,%p,%p,%s,%p)\n",iface,pbc,pmkToLeft,debugstr_guid(riid),ppvResult);
221 return E_NOTIMPL;
222 }
223
224 /******************************************************************************
225 * AntiMoniker_BindToStorage
226 ******************************************************************************/
227 static HRESULT WINAPI
228 AntiMonikerImpl_BindToStorage(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft,
229 REFIID riid, VOID** ppvResult)
230 {
231 TRACE("(%p,%p,%p,%s,%p)\n",iface,pbc,pmkToLeft,debugstr_guid(riid),ppvResult);
232 return E_NOTIMPL;
233 }
234
235 /******************************************************************************
236 * AntiMoniker_Reduce
237 ******************************************************************************/
238 static HRESULT WINAPI
239 AntiMonikerImpl_Reduce(IMoniker* iface, IBindCtx* pbc, DWORD dwReduceHowFar,
240 IMoniker** ppmkToLeft, IMoniker** ppmkReduced)
241 {
242 TRACE("(%p,%p,%d,%p,%p)\n",iface,pbc,dwReduceHowFar,ppmkToLeft,ppmkReduced);
243
244 if (ppmkReduced==NULL)
245 return E_POINTER;
246
247 AntiMonikerImpl_AddRef(iface);
248
249 *ppmkReduced=iface;
250
251 return MK_S_REDUCED_TO_SELF;
252 }
253 /******************************************************************************
254 * AntiMoniker_ComposeWith
255 ******************************************************************************/
256 static HRESULT WINAPI
257 AntiMonikerImpl_ComposeWith(IMoniker* iface, IMoniker* pmkRight,
258 BOOL fOnlyIfNotGeneric, IMoniker** ppmkComposite)
259 {
260
261 TRACE("(%p,%p,%d,%p)\n",iface,pmkRight,fOnlyIfNotGeneric,ppmkComposite);
262
263 if ((ppmkComposite==NULL)||(pmkRight==NULL))
264 return E_POINTER;
265
266 *ppmkComposite=0;
267
268 if (fOnlyIfNotGeneric)
269 return MK_E_NEEDGENERIC;
270 else
271 return CreateGenericComposite(iface,pmkRight,ppmkComposite);
272 }
273
274 /******************************************************************************
275 * AntiMoniker_Enum
276 ******************************************************************************/
277 static HRESULT WINAPI
278 AntiMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker)
279 {
280 TRACE("(%p,%d,%p)\n",iface,fForward,ppenumMoniker);
281
282 if (ppenumMoniker == NULL)
283 return E_POINTER;
284
285 *ppenumMoniker = NULL;
286
287 return S_OK;
288 }
289
290 /******************************************************************************
291 * AntiMoniker_IsEqual
292 ******************************************************************************/
293 static HRESULT WINAPI
294 AntiMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker)
295 {
296 DWORD mkSys;
297
298 TRACE("(%p,%p)\n",iface,pmkOtherMoniker);
299
300 if (pmkOtherMoniker==NULL)
301 return S_FALSE;
302
303 IMoniker_IsSystemMoniker(pmkOtherMoniker,&mkSys);
304
305 if (mkSys==MKSYS_ANTIMONIKER)
306 return S_OK;
307 else
308 return S_FALSE;
309 }
310
311 /******************************************************************************
312 * AntiMoniker_Hash
313 ******************************************************************************/
314 static HRESULT WINAPI AntiMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash)
315 {
316 if (pdwHash==NULL)
317 return E_POINTER;
318
319 *pdwHash = 0x80000001;
320
321 return S_OK;
322 }
323
324 /******************************************************************************
325 * AntiMoniker_IsRunning
326 ******************************************************************************/
327 static HRESULT WINAPI
328 AntiMonikerImpl_IsRunning(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft,
329 IMoniker* pmkNewlyRunning)
330 {
331 IRunningObjectTable* rot;
332 HRESULT res;
333
334 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pmkNewlyRunning);
335
336 if (pbc==NULL)
337 return E_INVALIDARG;
338
339 res=IBindCtx_GetRunningObjectTable(pbc,&rot);
340
341 if (FAILED(res))
342 return res;
343
344 res = IRunningObjectTable_IsRunning(rot,iface);
345
346 IRunningObjectTable_Release(rot);
347
348 return res;
349 }
350
351 /******************************************************************************
352 * AntiMoniker_GetTimeOfLastChange
353 ******************************************************************************/
354 static HRESULT WINAPI AntiMonikerImpl_GetTimeOfLastChange(IMoniker* iface,
355 IBindCtx* pbc,
356 IMoniker* pmkToLeft,
357 FILETIME* pAntiTime)
358 {
359 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pAntiTime);
360 return E_NOTIMPL;
361 }
362
363 /******************************************************************************
364 * AntiMoniker_Inverse
365 ******************************************************************************/
366 static HRESULT WINAPI
367 AntiMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk)
368 {
369 TRACE("(%p,%p)\n",iface,ppmk);
370
371 if (ppmk==NULL)
372 return E_POINTER;
373
374 *ppmk=0;
375
376 return MK_E_NOINVERSE;
377 }
378
379 /******************************************************************************
380 * AntiMoniker_CommonPrefixWith
381 ******************************************************************************/
382 static HRESULT WINAPI
383 AntiMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther,IMoniker** ppmkPrefix)
384 {
385 DWORD mkSys;
386
387 IMoniker_IsSystemMoniker(pmkOther,&mkSys);
388
389 if(mkSys==MKSYS_ANTIMONIKER){
390
391 IMoniker_AddRef(iface);
392
393 *ppmkPrefix=iface;
394
395 IMoniker_AddRef(iface);
396
397 return MK_S_US;
398 }
399 else
400 return MonikerCommonPrefixWith(iface,pmkOther,ppmkPrefix);
401 }
402
403 /******************************************************************************
404 * AntiMoniker_RelativePathTo
405 ******************************************************************************/
406 static HRESULT WINAPI
407 AntiMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath)
408 {
409 TRACE("(%p,%p,%p)\n",iface,pmOther,ppmkRelPath);
410
411 if (ppmkRelPath==NULL)
412 return E_POINTER;
413
414 IMoniker_AddRef(pmOther);
415
416 *ppmkRelPath=pmOther;
417
418 return MK_S_HIM;
419 }
420
421 /******************************************************************************
422 * AntiMoniker_GetDisplayName
423 ******************************************************************************/
424 static HRESULT WINAPI
425 AntiMonikerImpl_GetDisplayName(IMoniker* iface, IBindCtx* pbc,
426 IMoniker* pmkToLeft, LPOLESTR *ppszDisplayName)
427 {
428 static const WCHAR back[]={'\\','.','.',0};
429
430 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,ppszDisplayName);
431
432 if (ppszDisplayName==NULL)
433 return E_POINTER;
434
435 if (pmkToLeft!=NULL){
436 FIXME("() pmkToLeft!=NULL not implemented\n");
437 return E_NOTIMPL;
438 }
439
440 *ppszDisplayName=CoTaskMemAlloc(sizeof(back));
441
442 if (*ppszDisplayName==NULL)
443 return E_OUTOFMEMORY;
444
445 lstrcpyW(*ppszDisplayName,back);
446
447 return S_OK;
448 }
449
450 /******************************************************************************
451 * AntiMoniker_ParseDisplayName
452 ******************************************************************************/
453 static HRESULT WINAPI
454 AntiMonikerImpl_ParseDisplayName(IMoniker* iface, IBindCtx* pbc,
455 IMoniker* pmkToLeft, LPOLESTR pszDisplayName,
456 ULONG* pchEaten, IMoniker** ppmkOut)
457 {
458 TRACE("(%p,%p,%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pszDisplayName,pchEaten,ppmkOut);
459 return E_NOTIMPL;
460 }
461
462 /******************************************************************************
463 * AntiMoniker_IsSystemMoniker
464 ******************************************************************************/
465 static HRESULT WINAPI
466 AntiMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys)
467 {
468 TRACE("(%p,%p)\n",iface,pwdMksys);
469
470 if (!pwdMksys)
471 return E_POINTER;
472
473 (*pwdMksys)=MKSYS_ANTIMONIKER;
474
475 return S_OK;
476 }
477
478 /*******************************************************************************
479 * AntiMonikerIROTData_QueryInterface
480 *******************************************************************************/
481 static HRESULT WINAPI
482 AntiMonikerROTDataImpl_QueryInterface(IROTData *iface,REFIID riid,VOID** ppvObject)
483 {
484 AntiMonikerImpl *This = impl_from_IROTData(iface);
485
486 TRACE("(%p,%s,%p)\n",iface,debugstr_guid(riid),ppvObject);
487
488 return AntiMonikerImpl_QueryInterface(&This->IMoniker_iface, riid, ppvObject);
489 }
490
491 /***********************************************************************
492 * AntiMonikerIROTData_AddRef
493 */
494 static ULONG WINAPI AntiMonikerROTDataImpl_AddRef(IROTData *iface)
495 {
496 AntiMonikerImpl *This = impl_from_IROTData(iface);
497
498 TRACE("(%p)\n",iface);
499
500 return AntiMonikerImpl_AddRef(&This->IMoniker_iface);
501 }
502
503 /***********************************************************************
504 * AntiMonikerIROTData_Release
505 */
506 static ULONG WINAPI AntiMonikerROTDataImpl_Release(IROTData* iface)
507 {
508 AntiMonikerImpl *This = impl_from_IROTData(iface);
509
510 TRACE("(%p)\n",iface);
511
512 return AntiMonikerImpl_Release(&This->IMoniker_iface);
513 }
514
515 /******************************************************************************
516 * AntiMonikerIROTData_GetComparisonData
517 ******************************************************************************/
518 static HRESULT WINAPI
519 AntiMonikerROTDataImpl_GetComparisonData(IROTData* iface, BYTE* pbData,
520 ULONG cbMax, ULONG* pcbData)
521 {
522 DWORD constant = 1;
523
524 TRACE("(%p, %u, %p)\n", pbData, cbMax, pcbData);
525
526 *pcbData = sizeof(CLSID) + sizeof(DWORD);
527 if (cbMax < *pcbData)
528 return E_OUTOFMEMORY;
529
530 memcpy(pbData, &CLSID_AntiMoniker, sizeof(CLSID));
531 memcpy(pbData+sizeof(CLSID), &constant, sizeof(DWORD));
532
533 return S_OK;
534 }
535
536 /********************************************************************************/
537 /* Virtual function table for the AntiMonikerImpl class which include IPersist,*/
538 /* IPersistStream and IMoniker functions. */
539 static const IMonikerVtbl VT_AntiMonikerImpl =
540 {
541 AntiMonikerImpl_QueryInterface,
542 AntiMonikerImpl_AddRef,
543 AntiMonikerImpl_Release,
544 AntiMonikerImpl_GetClassID,
545 AntiMonikerImpl_IsDirty,
546 AntiMonikerImpl_Load,
547 AntiMonikerImpl_Save,
548 AntiMonikerImpl_GetSizeMax,
549 AntiMonikerImpl_BindToObject,
550 AntiMonikerImpl_BindToStorage,
551 AntiMonikerImpl_Reduce,
552 AntiMonikerImpl_ComposeWith,
553 AntiMonikerImpl_Enum,
554 AntiMonikerImpl_IsEqual,
555 AntiMonikerImpl_Hash,
556 AntiMonikerImpl_IsRunning,
557 AntiMonikerImpl_GetTimeOfLastChange,
558 AntiMonikerImpl_Inverse,
559 AntiMonikerImpl_CommonPrefixWith,
560 AntiMonikerImpl_RelativePathTo,
561 AntiMonikerImpl_GetDisplayName,
562 AntiMonikerImpl_ParseDisplayName,
563 AntiMonikerImpl_IsSystemMoniker
564 };
565
566 /********************************************************************************/
567 /* Virtual function table for the IROTData class. */
568 static const IROTDataVtbl VT_ROTDataImpl =
569 {
570 AntiMonikerROTDataImpl_QueryInterface,
571 AntiMonikerROTDataImpl_AddRef,
572 AntiMonikerROTDataImpl_Release,
573 AntiMonikerROTDataImpl_GetComparisonData
574 };
575
576 /******************************************************************************
577 * AntiMoniker_Construct (local function)
578 *******************************************************************************/
579 static HRESULT AntiMonikerImpl_Construct(AntiMonikerImpl* This)
580 {
581
582 TRACE("(%p)\n",This);
583
584 /* Initialize the virtual function table. */
585 This->IMoniker_iface.lpVtbl = &VT_AntiMonikerImpl;
586 This->IROTData_iface.lpVtbl = &VT_ROTDataImpl;
587 This->ref = 0;
588 This->pMarshal = NULL;
589
590 return S_OK;
591 }
592
593 /******************************************************************************
594 * CreateAntiMoniker [OLE32.@]
595 ******************************************************************************/
596 HRESULT WINAPI CreateAntiMoniker(IMoniker **ppmk)
597 {
598 AntiMonikerImpl* newAntiMoniker;
599 HRESULT hr;
600
601 TRACE("(%p)\n",ppmk);
602
603 newAntiMoniker = HeapAlloc(GetProcessHeap(), 0, sizeof(AntiMonikerImpl));
604
605 if (newAntiMoniker == 0)
606 return STG_E_INSUFFICIENTMEMORY;
607
608 hr = AntiMonikerImpl_Construct(newAntiMoniker);
609 if (FAILED(hr))
610 {
611 HeapFree(GetProcessHeap(),0,newAntiMoniker);
612 return hr;
613 }
614
615 return AntiMonikerImpl_QueryInterface(&newAntiMoniker->IMoniker_iface, &IID_IMoniker,
616 (void**)ppmk);
617 }
618
619 HRESULT WINAPI AntiMoniker_CreateInstance(IClassFactory *iface,
620 IUnknown *pUnk, REFIID riid, void **ppv)
621 {
622 IMoniker *pMoniker;
623 HRESULT hr;
624
625 TRACE("(%p, %s, %p)\n", pUnk, debugstr_guid(riid), ppv);
626
627 *ppv = NULL;
628
629 if (pUnk)
630 return CLASS_E_NOAGGREGATION;
631
632 hr = CreateAntiMoniker(&pMoniker);
633 if (FAILED(hr))
634 return hr;
635
636 hr = IMoniker_QueryInterface(pMoniker, riid, ppv);
637
638 if (FAILED(hr))
639 IMoniker_Release(pMoniker);
640
641 return hr;
642 }