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