hlink/tests: Fix interface leak (Valgrind).
[wine] / dlls / hlink / link.c
1 /*
2  * Implementation of hyperlinking (hlink.dll)
3  *
4  * Copyright 2005 Aric Stewart for CodeWeavers
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 "hlink_private.h"
22
23 #include "shellapi.h"
24 #include "hlguids.h"
25
26 #include "wine/debug.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(hlink);
29
30 #define HLINK_SAVE_MAGIC    0x00000002
31 #define HLINK_SAVE_MONIKER_PRESENT      0x01
32 #define HLINK_SAVE_MONIKER_IS_ABSOLUTE  0x02
33 #define HLINK_SAVE_LOCATION_PRESENT     0x08
34 #define HLINK_SAVE_FRIENDLY_PRESENT     0x10
35 /* 0x20, 0x40 unknown */
36 #define HLINK_SAVE_TARGET_FRAME_PRESENT 0x80
37 /* known flags */
38 #define HLINK_SAVE_ALL (HLINK_SAVE_TARGET_FRAME_PRESENT|HLINK_SAVE_FRIENDLY_PRESENT|HLINK_SAVE_LOCATION_PRESENT|0x04|HLINK_SAVE_MONIKER_IS_ABSOLUTE|HLINK_SAVE_MONIKER_PRESENT)
39
40 static const IHlinkVtbl              hlvt;
41 static const IPersistStreamVtbl      psvt;
42 static const IDataObjectVtbl         dovt;
43
44 typedef struct
45 {
46     IHlink              IHlink_iface;
47     LONG                ref;
48
49     IPersistStream      IPersistStream_iface;
50     IDataObject         IDataObject_iface;
51
52     LPWSTR              FriendlyName;
53     LPWSTR              Location;
54     LPWSTR              TargetFrameName;
55     IMoniker            *Moniker;
56     IHlinkSite          *Site;
57     DWORD               SiteData;
58     BOOL                absolute;
59 } HlinkImpl;
60
61 static inline HlinkImpl *impl_from_IHlink(IHlink *iface)
62 {
63     return CONTAINING_RECORD(iface, HlinkImpl, IHlink_iface);
64 }
65
66
67 static inline HlinkImpl* impl_from_IPersistStream( IPersistStream* iface)
68 {
69     return CONTAINING_RECORD(iface, HlinkImpl, IPersistStream_iface);
70 }
71
72 static inline HlinkImpl* impl_from_IDataObject( IDataObject* iface)
73 {
74     return CONTAINING_RECORD(iface, HlinkImpl, IDataObject_iface);
75 }
76
77 static HRESULT __GetMoniker(HlinkImpl* This, IMoniker** moniker,
78         DWORD ref_type)
79 {
80     HRESULT hres;
81
82     if (ref_type == HLINKGETREF_DEFAULT)
83         ref_type = HLINKGETREF_RELATIVE;
84
85     if (ref_type == HLINKGETREF_ABSOLUTE && This->Site)
86     {
87         IMoniker *hls_moniker;
88
89         hres = IHlinkSite_GetMoniker(This->Site, This->SiteData,
90                 OLEGETMONIKER_FORCEASSIGN, OLEWHICHMK_CONTAINER, &hls_moniker);
91         if (FAILED(hres))
92             return hres;
93
94         if (This->Moniker)
95         {
96             hres = IMoniker_ComposeWith(hls_moniker, This->Moniker, FALSE,
97                     moniker);
98             IMoniker_Release(hls_moniker);
99             return hres;
100         }
101
102         *moniker = hls_moniker;
103         return S_OK;
104     }
105
106     *moniker = This->Moniker;
107     if (*moniker)
108         IMoniker_AddRef(*moniker);
109
110     return S_OK;
111 }
112
113 HRESULT WINAPI HLink_Constructor(IUnknown *pUnkOuter, REFIID riid,
114         LPVOID *ppv)
115 {
116     HlinkImpl * hl;
117
118     TRACE("unkOut=%p riid=%s\n", pUnkOuter, debugstr_guid(riid));
119     *ppv = NULL;
120
121     if (pUnkOuter)
122         return CLASS_E_NOAGGREGATION;
123
124     hl = heap_alloc_zero(sizeof(HlinkImpl));
125     if (!hl)
126         return E_OUTOFMEMORY;
127
128     hl->ref = 1;
129     hl->IHlink_iface.lpVtbl = &hlvt;
130     hl->IPersistStream_iface.lpVtbl = &psvt;
131     hl->IDataObject_iface.lpVtbl = &dovt;
132
133     *ppv = hl;
134     return S_OK;
135 }
136
137 static HRESULT WINAPI IHlink_fnQueryInterface(IHlink* iface, REFIID riid,
138         LPVOID *ppvObj)
139 {
140     HlinkImpl  *This = impl_from_IHlink(iface);
141
142     TRACE ("(%p)->(%s,%p)\n", This, debugstr_guid (riid), ppvObj);
143
144     *ppvObj = NULL;
145
146     if (IsEqualIID(riid, &IID_IUnknown) || (IsEqualIID(riid, &IID_IHlink)))
147         *ppvObj = This;
148     else if (IsEqualIID(riid, &IID_IPersistStream))
149         *ppvObj = &This->IPersistStream_iface;
150     else if (IsEqualIID(riid, &IID_IDataObject))
151         *ppvObj = &This->IDataObject_iface;
152
153     if (*ppvObj)
154     {
155         IUnknown_AddRef((IUnknown*)(*ppvObj));
156         return S_OK;
157     }
158     return E_NOINTERFACE;
159 }
160
161 static ULONG WINAPI IHlink_fnAddRef (IHlink* iface)
162 {
163     HlinkImpl  *This = impl_from_IHlink(iface);
164     ULONG refCount = InterlockedIncrement(&This->ref);
165
166     TRACE("(%p)->(count=%u)\n", This, refCount - 1);
167
168     return refCount;
169 }
170
171 static ULONG WINAPI IHlink_fnRelease (IHlink* iface)
172 {
173     HlinkImpl  *This = impl_from_IHlink(iface);
174     ULONG refCount = InterlockedDecrement(&This->ref);
175
176     TRACE("(%p)->(count=%u)\n", This, refCount + 1);
177     if (refCount)
178         return refCount;
179
180     TRACE("-- destroying IHlink (%p)\n", This);
181     heap_free(This->FriendlyName);
182     heap_free(This->TargetFrameName);
183     heap_free(This->Location);
184     if (This->Moniker)
185         IMoniker_Release(This->Moniker);
186     if (This->Site)
187         IHlinkSite_Release(This->Site);
188     heap_free(This);
189     return 0;
190 }
191
192 static HRESULT WINAPI IHlink_fnSetHlinkSite( IHlink* iface,
193         IHlinkSite* pihlSite, DWORD dwSiteData)
194 {
195     HlinkImpl  *This = impl_from_IHlink(iface);
196
197     TRACE("(%p)->(%p %i)\n", This, pihlSite, dwSiteData);
198
199     if (This->Site)
200         IHlinkSite_Release(This->Site);
201
202     This->Site = pihlSite;
203     if (This->Site)
204         IHlinkSite_AddRef(This->Site);
205
206     This->SiteData = dwSiteData;
207
208     return S_OK;
209 }
210
211 static HRESULT WINAPI IHlink_fnGetHlinkSite( IHlink* iface,
212         IHlinkSite** ppihlSite, DWORD *pdwSiteData)
213 {
214     HlinkImpl  *This = impl_from_IHlink(iface);
215
216     TRACE("(%p)->(%p %p)\n", This, ppihlSite, pdwSiteData);
217
218     *ppihlSite = This->Site;
219
220     if (This->Site) {
221         IHlinkSite_AddRef(This->Site);
222         *pdwSiteData = This->SiteData;
223     }
224
225     return S_OK;
226 }
227
228 static HRESULT WINAPI IHlink_fnSetMonikerReference( IHlink* iface,
229         DWORD rfHLSETF, IMoniker *pmkTarget, LPCWSTR pwzLocation)
230 {
231     HlinkImpl  *This = impl_from_IHlink(iface);
232
233     TRACE("(%p)->(%i %p %s)\n", This, rfHLSETF, pmkTarget,
234             debugstr_w(pwzLocation));
235
236     if(rfHLSETF == 0)
237         return E_INVALIDARG;
238     if(!(rfHLSETF & (HLINKSETF_TARGET | HLINKSETF_LOCATION)))
239         return rfHLSETF;
240
241     if(rfHLSETF & HLINKSETF_TARGET){
242         if (This->Moniker)
243             IMoniker_Release(This->Moniker);
244
245         This->Moniker = pmkTarget;
246         if (This->Moniker)
247         {
248             LPOLESTR display_name;
249             IMoniker_AddRef(This->Moniker);
250             IMoniker_GetDisplayName(This->Moniker, NULL, NULL, &display_name);
251             This->absolute = display_name && strchrW(display_name, ':');
252             CoTaskMemFree(display_name);
253         }
254     }
255
256     if(rfHLSETF & HLINKSETF_LOCATION){
257         heap_free(This->Location);
258         This->Location = hlink_strdupW( pwzLocation );
259     }
260
261     return S_OK;
262 }
263
264 static HRESULT WINAPI IHlink_fnSetStringReference(IHlink* iface,
265         DWORD grfHLSETF, LPCWSTR pwzTarget, LPCWSTR pwzLocation)
266 {
267     HlinkImpl  *This = impl_from_IHlink(iface);
268
269     TRACE("(%p)->(%i %s %s)\n", This, grfHLSETF, debugstr_w(pwzTarget),
270             debugstr_w(pwzLocation));
271
272     if(grfHLSETF > (HLINKSETF_TARGET | HLINKSETF_LOCATION) &&
273             grfHLSETF < -(HLINKSETF_TARGET | HLINKSETF_LOCATION))
274         return grfHLSETF;
275
276     if (grfHLSETF & HLINKSETF_TARGET)
277     {
278         if (This->Moniker)
279         {
280             IMoniker_Release(This->Moniker);
281             This->Moniker = NULL;
282         }
283         if (pwzTarget && *pwzTarget)
284         {
285             IMoniker *pMon;
286             IBindCtx *pbc = NULL;
287             ULONG eaten;
288             HRESULT r;
289
290             r = CreateBindCtx(0, &pbc);
291             if (FAILED(r))
292                 return E_OUTOFMEMORY;
293
294             r = MkParseDisplayName(pbc, pwzTarget, &eaten, &pMon);
295             IBindCtx_Release(pbc);
296
297             if (FAILED(r))
298             {
299                 LPCWSTR p = strchrW(pwzTarget, ':');
300                 if (p && (p - pwzTarget > 1))
301                     r = CreateURLMoniker(NULL, pwzTarget, &pMon);
302                 else
303                     r = CreateFileMoniker(pwzTarget, &pMon);
304                 if (FAILED(r))
305                 {
306                     ERR("couldn't create moniker for %s, failed with error 0x%08x\n",
307                         debugstr_w(pwzTarget), r);
308                     return r;
309                 }
310             }
311
312             IHlink_SetMonikerReference(iface, HLINKSETF_TARGET, pMon, NULL);
313             IMoniker_Release(pMon);
314         }
315     }
316
317     if (grfHLSETF & HLINKSETF_LOCATION)
318     {
319         heap_free(This->Location);
320         This->Location = NULL;
321         if (pwzLocation && *pwzLocation)
322             This->Location = hlink_strdupW( pwzLocation );
323     }
324
325     return S_OK;
326 }
327
328 static HRESULT WINAPI IHlink_fnGetMonikerReference(IHlink* iface,
329         DWORD dwWhichRef, IMoniker **ppimkTarget, LPWSTR *ppwzLocation)
330 {
331     HlinkImpl  *This = impl_from_IHlink(iface);
332
333     TRACE("(%p) -> (%i %p %p)\n", This, dwWhichRef, ppimkTarget,
334             ppwzLocation);
335
336     if (ppimkTarget)
337     {
338         HRESULT hres = __GetMoniker(This, ppimkTarget, dwWhichRef);
339         if (FAILED(hres))
340         {
341             if (ppwzLocation)
342                 *ppwzLocation = NULL;
343             return hres;
344         }
345     }
346
347     if (ppwzLocation)
348         IHlink_GetStringReference(iface, dwWhichRef, NULL, ppwzLocation);
349
350     return S_OK;
351 }
352
353 static HRESULT WINAPI IHlink_fnGetStringReference (IHlink* iface,
354         DWORD dwWhichRef, LPWSTR *ppwzTarget, LPWSTR *ppwzLocation)
355 {
356     HlinkImpl  *This = impl_from_IHlink(iface);
357
358     TRACE("(%p) -> (%i %p %p)\n", This, dwWhichRef, ppwzTarget, ppwzLocation);
359
360     if(dwWhichRef != -1 && dwWhichRef & ~(HLINKGETREF_DEFAULT | HLINKGETREF_ABSOLUTE | HLINKGETREF_RELATIVE))
361     {
362         if(ppwzTarget)
363             *ppwzTarget = NULL;
364         if(ppwzLocation)
365             *ppwzLocation = NULL;
366         return E_INVALIDARG;
367     }
368
369     if (ppwzTarget)
370     {
371         IMoniker* mon;
372         HRESULT hres = __GetMoniker(This, &mon, dwWhichRef);
373         if (FAILED(hres))
374         {
375             if (ppwzLocation)
376                 *ppwzLocation = NULL;
377             return hres;
378         }
379         if (mon)
380         {
381             IBindCtx *pbc;
382
383             CreateBindCtx( 0, &pbc);
384             IMoniker_GetDisplayName(mon, pbc, NULL, ppwzTarget);
385             IBindCtx_Release(pbc);
386             IMoniker_Release(mon);
387         }
388         else
389             *ppwzTarget = NULL;
390     }
391     if (ppwzLocation)
392         *ppwzLocation = hlink_co_strdupW( This->Location );
393
394     TRACE("(Target: %s Location: %s)\n",
395             (ppwzTarget)?debugstr_w(*ppwzTarget):"<NULL>",
396             (ppwzLocation)?debugstr_w(*ppwzLocation):"<NULL>");
397
398     return S_OK;
399 }
400
401 static HRESULT WINAPI IHlink_fnSetFriendlyName (IHlink *iface,
402         LPCWSTR pwzFriendlyName)
403 {
404     HlinkImpl  *This = impl_from_IHlink(iface);
405
406     TRACE("(%p) -> (%s)\n", This, debugstr_w(pwzFriendlyName));
407
408     heap_free(This->FriendlyName);
409     This->FriendlyName = hlink_strdupW( pwzFriendlyName );
410
411     return S_OK;
412 }
413
414 static HRESULT WINAPI IHlink_fnGetFriendlyName (IHlink* iface,
415         DWORD grfHLFNAMEF, LPWSTR* ppwzFriendlyName)
416 {
417     HlinkImpl  *This = impl_from_IHlink(iface);
418
419     TRACE("(%p) -> (%i %p)\n", This, grfHLFNAMEF, ppwzFriendlyName);
420
421     /* FIXME: Only using explicitly set and cached friendly names */
422
423     if (This->FriendlyName)
424         *ppwzFriendlyName = hlink_co_strdupW( This->FriendlyName );
425     else
426     {
427         IMoniker *moniker;
428         HRESULT hres = __GetMoniker(This, &moniker, HLINKGETREF_DEFAULT);
429         if (FAILED(hres))
430         {
431             *ppwzFriendlyName = NULL;
432             return hres;
433         }
434         if (moniker)
435         {
436             IBindCtx *bcxt;
437             CreateBindCtx(0, &bcxt);
438
439             IMoniker_GetDisplayName(moniker, bcxt, NULL, ppwzFriendlyName);
440             IBindCtx_Release(bcxt);
441             IMoniker_Release(moniker);
442         }
443         else
444             *ppwzFriendlyName = NULL;
445     }
446
447     return S_OK;
448 }
449
450 static HRESULT WINAPI IHlink_fnSetTargetFrameName(IHlink* iface,
451         LPCWSTR pwzTargetFramename)
452 {
453     HlinkImpl  *This = impl_from_IHlink(iface);
454     TRACE("(%p)->(%s)\n", This, debugstr_w(pwzTargetFramename));
455
456     heap_free(This->TargetFrameName);
457     This->TargetFrameName = hlink_strdupW( pwzTargetFramename );
458
459     return S_OK;
460 }
461
462 static HRESULT WINAPI IHlink_fnGetTargetFrameName(IHlink* iface,
463         LPWSTR *ppwzTargetFrameName)
464 {
465     HlinkImpl  *This = impl_from_IHlink(iface);
466
467     TRACE("(%p)->(%p)\n", This, ppwzTargetFrameName);
468     *ppwzTargetFrameName = hlink_co_strdupW( This->TargetFrameName );
469
470     return S_OK;
471 }
472
473 static HRESULT WINAPI IHlink_fnGetMiscStatus(IHlink* iface, DWORD* pdwStatus)
474 {
475     FIXME("\n");
476     return E_NOTIMPL;
477 }
478
479 static HRESULT WINAPI IHlink_fnNavigate(IHlink* iface, DWORD grfHLNF, LPBC pbc,
480         IBindStatusCallback *pbsc, IHlinkBrowseContext *phbc)
481 {
482     HlinkImpl  *This = impl_from_IHlink(iface);
483     IMoniker *mon = NULL;
484     HRESULT r;
485
486     FIXME("Semi-Stub:(%p)->(%i %p %p %p)\n", This, grfHLNF, pbc, pbsc, phbc);
487
488     r = __GetMoniker(This, &mon, HLINKGETREF_ABSOLUTE);
489     TRACE("Moniker %p\n", mon);
490
491     if (SUCCEEDED(r))
492     {
493         IBindCtx *bcxt;
494         IHlinkTarget *target = NULL;
495
496         CreateBindCtx(0, &bcxt);
497
498         RegisterBindStatusCallback(bcxt, pbsc, NULL, 0);
499
500         r = IMoniker_BindToObject(mon, bcxt, NULL, &IID_IHlinkTarget,
501                 (LPVOID*)&target);
502         TRACE("IHlinkTarget returned 0x%x\n", r);
503         if (r == S_OK)
504         {
505             IHlinkTarget_SetBrowseContext(target, phbc);
506             IHlinkTarget_Navigate(target, grfHLNF, This->Location);
507             IHlinkTarget_Release(target);
508         }
509         else
510         {
511             static const WCHAR szOpen[] = {'o','p','e','n',0};
512             LPWSTR target = NULL;
513
514             r = IHlink_GetStringReference(iface, HLINKGETREF_DEFAULT, &target, NULL);
515             if (SUCCEEDED(r) && target)
516             {
517                 ShellExecuteW(NULL, szOpen, target, NULL, NULL, SW_SHOW);
518                 CoTaskMemFree(target);
519             }
520         }
521
522         RevokeBindStatusCallback(bcxt, pbsc);
523
524         IBindCtx_Release(bcxt);
525         IMoniker_Release(mon);
526     }
527
528     if (This->Site)
529         IHlinkSite_OnNavigationComplete(This->Site, This->SiteData, 0, r, NULL);
530
531     TRACE("Finished Navigation\n");
532     return r;
533 }
534
535 static HRESULT WINAPI IHlink_fnSetAdditonalParams(IHlink* iface,
536         LPCWSTR pwzAdditionalParams)
537 {
538     TRACE("Not implemented in native IHlink\n");
539     return E_NOTIMPL;
540 }
541
542 static HRESULT WINAPI IHlink_fnGetAdditionalParams(IHlink* iface,
543         LPWSTR* ppwzAdditionalParams)
544 {
545     TRACE("Not implemented in native IHlink\n");
546     return E_NOTIMPL;
547 }
548
549 static const IHlinkVtbl hlvt =
550 {
551     IHlink_fnQueryInterface,
552     IHlink_fnAddRef,
553     IHlink_fnRelease,
554     IHlink_fnSetHlinkSite,
555     IHlink_fnGetHlinkSite,
556     IHlink_fnSetMonikerReference,
557     IHlink_fnGetMonikerReference,
558     IHlink_fnSetStringReference,
559     IHlink_fnGetStringReference,
560     IHlink_fnSetFriendlyName,
561     IHlink_fnGetFriendlyName,
562     IHlink_fnSetTargetFrameName,
563     IHlink_fnGetTargetFrameName,
564     IHlink_fnGetMiscStatus,
565     IHlink_fnNavigate,
566     IHlink_fnSetAdditonalParams,
567     IHlink_fnGetAdditionalParams
568 };
569
570 static HRESULT WINAPI IDataObject_fnQueryInterface(IDataObject* iface,
571         REFIID riid, LPVOID *ppvObj)
572 {
573     HlinkImpl *This = impl_from_IDataObject(iface);
574     TRACE("%p\n", This);
575     return IHlink_QueryInterface(&This->IHlink_iface, riid, ppvObj);
576 }
577
578 static ULONG WINAPI IDataObject_fnAddRef (IDataObject* iface)
579 {
580     HlinkImpl *This = impl_from_IDataObject(iface);
581     TRACE("%p\n", This);
582     return IHlink_AddRef(&This->IHlink_iface);
583 }
584
585 static ULONG WINAPI IDataObject_fnRelease (IDataObject* iface)
586 {
587     HlinkImpl *This = impl_from_IDataObject(iface);
588     TRACE("%p\n", This);
589     return IHlink_Release(&This->IHlink_iface);
590 }
591
592 static HRESULT WINAPI IDataObject_fnGetData(IDataObject* iface,
593         FORMATETC* pformatetcIn, STGMEDIUM* pmedium)
594 {
595     FIXME("\n");
596     return E_NOTIMPL;
597 }
598
599 static HRESULT WINAPI IDataObject_fnGetDataHere(IDataObject* iface,
600         FORMATETC* pformatetc, STGMEDIUM* pmedium)
601 {
602     FIXME("\n");
603     return E_NOTIMPL;
604 }
605
606 static HRESULT WINAPI IDataObject_fnQueryGetData(IDataObject* iface,
607         FORMATETC* pformatetc)
608 {
609     FIXME("\n");
610     return E_NOTIMPL;
611 }
612
613 static HRESULT WINAPI IDataObject_fnGetConicalFormatEtc(IDataObject* iface,
614         FORMATETC* pformatetcIn, FORMATETC* pformatetcOut)
615 {
616     FIXME("\n");
617     return E_NOTIMPL;
618 }
619
620 static HRESULT WINAPI IDataObject_fnSetData(IDataObject* iface,
621         FORMATETC* pformatetc, STGMEDIUM* pmedium, BOOL fRelease)
622 {
623     FIXME("\n");
624     return E_NOTIMPL;
625 }
626
627 static HRESULT WINAPI IDataObject_fnEnumFormatEtc(IDataObject* iface,
628         DWORD dwDirection, IEnumFORMATETC** ppenumFormatEtc)
629 {
630     FIXME("\n");
631     return E_NOTIMPL;
632 }
633
634 static HRESULT WINAPI IDataObject_fnDAdvise(IDataObject* iface,
635         FORMATETC* pformatetc, DWORD advf, IAdviseSink* pAdvSink,
636         DWORD* pdwConnection)
637 {
638     FIXME("\n");
639     return E_NOTIMPL;
640 }
641
642 static HRESULT WINAPI IDataObject_fnDUnadvise(IDataObject* iface,
643         DWORD dwConnection)
644 {
645     FIXME("\n");
646     return E_NOTIMPL;
647 }
648
649 static HRESULT WINAPI IDataObject_fnEnumDAdvise(IDataObject* iface,
650         IEnumSTATDATA** ppenumAdvise)
651 {
652     FIXME("\n");
653     return E_NOTIMPL;
654 }
655
656 static const IDataObjectVtbl dovt =
657 {
658     IDataObject_fnQueryInterface,
659     IDataObject_fnAddRef,
660     IDataObject_fnRelease,
661     IDataObject_fnGetData,
662     IDataObject_fnGetDataHere,
663     IDataObject_fnQueryGetData,
664     IDataObject_fnGetConicalFormatEtc,
665     IDataObject_fnSetData,
666     IDataObject_fnEnumFormatEtc,
667     IDataObject_fnDAdvise,
668     IDataObject_fnDUnadvise,
669     IDataObject_fnEnumDAdvise
670 };
671
672 static HRESULT WINAPI IPersistStream_fnQueryInterface(IPersistStream* iface,
673         REFIID riid, LPVOID *ppvObj)
674 {
675     HlinkImpl *This = impl_from_IPersistStream(iface);
676     TRACE("(%p)\n", This);
677     return IHlink_QueryInterface(&This->IHlink_iface, riid, ppvObj);
678 }
679
680 static ULONG WINAPI IPersistStream_fnAddRef (IPersistStream* iface)
681 {
682     HlinkImpl *This = impl_from_IPersistStream(iface);
683     TRACE("(%p)\n", This);
684     return IHlink_AddRef(&This->IHlink_iface);
685 }
686
687 static ULONG WINAPI IPersistStream_fnRelease (IPersistStream* iface)
688 {
689     HlinkImpl *This = impl_from_IPersistStream(iface);
690     TRACE("(%p)\n", This);
691     return IHlink_Release(&This->IHlink_iface);
692 }
693
694 static HRESULT WINAPI IPersistStream_fnGetClassID(IPersistStream* iface,
695         CLSID* pClassID)
696 {
697     HlinkImpl *This = impl_from_IPersistStream(iface);
698     TRACE("(%p)\n", This);
699     *pClassID = CLSID_StdHlink;
700     return S_OK;
701 }
702
703 static HRESULT WINAPI IPersistStream_fnIsDirty(IPersistStream* iface)
704 {
705     FIXME("\n");
706     return E_NOTIMPL;
707 }
708
709 static HRESULT write_hlink_string(IStream *pStm, LPCWSTR str)
710 {
711     DWORD len;
712     HRESULT hr;
713
714     TRACE("(%p, %s)\n", pStm, debugstr_w(str));
715
716     len = strlenW(str) + 1;
717
718     hr = IStream_Write(pStm, &len, sizeof(len), NULL);
719     if (FAILED(hr)) return hr;
720
721     hr = IStream_Write(pStm, str, len * sizeof(WCHAR), NULL);
722     if (FAILED(hr)) return hr;
723
724     return S_OK;
725 }
726
727 static inline ULONG size_hlink_string(LPCWSTR str)
728 {
729     return sizeof(DWORD) + (strlenW(str) + 1) * sizeof(WCHAR);
730 }
731
732 static HRESULT read_hlink_string(IStream *pStm, LPWSTR *out_str)
733 {
734     LPWSTR str;
735     DWORD len;
736     ULONG read;
737     HRESULT hr;
738
739     hr = IStream_Read(pStm, &len, sizeof(len), &read);
740     if (FAILED(hr)) return hr;
741     if (read != sizeof(len)) return STG_E_READFAULT;
742
743     TRACE("read len %d\n", len);
744
745     str = heap_alloc(len * sizeof(WCHAR));
746     if (!str) return E_OUTOFMEMORY;
747
748     hr = IStream_Read(pStm, str, len * sizeof(WCHAR), &read);
749     if (FAILED(hr))
750     {
751         heap_free(str);
752         return hr;
753     }
754     if (read != len * sizeof(WCHAR))
755     {
756         heap_free(str);
757         return STG_E_READFAULT;
758     }
759     TRACE("read string %s\n", debugstr_w(str));
760
761     *out_str = str;
762     return S_OK;
763 }
764
765 static HRESULT WINAPI IPersistStream_fnLoad(IPersistStream* iface,
766         IStream* pStm)
767 {
768     HRESULT r;
769     DWORD hdr[2];
770     DWORD read;
771     HlinkImpl *This = impl_from_IPersistStream(iface);
772
773     r = IStream_Read(pStm, hdr, sizeof(hdr), &read);
774     if (read != sizeof(hdr) || (hdr[0] != HLINK_SAVE_MAGIC))
775     {
776         r = E_FAIL;
777         goto end;
778     }
779     if (hdr[1] & ~HLINK_SAVE_ALL)
780         FIXME("unknown flag(s) 0x%x\n", hdr[1] & ~HLINK_SAVE_ALL);
781
782     if (hdr[1] & HLINK_SAVE_TARGET_FRAME_PRESENT)
783     {
784         TRACE("loading target frame name\n");
785         r = read_hlink_string(pStm, &This->TargetFrameName);
786         if (FAILED(r)) goto end;
787     }
788
789     if (hdr[1] & HLINK_SAVE_FRIENDLY_PRESENT)
790     {
791         TRACE("loading target friendly name\n");
792         if (!(hdr[1] & 0x4))
793             FIXME("0x4 flag not present with friendly name flag - not sure what this means\n");
794         r = read_hlink_string(pStm, &This->FriendlyName);
795         if (FAILED(r)) goto end;
796     }
797
798     if (hdr[1] & HLINK_SAVE_MONIKER_PRESENT)
799     {
800         TRACE("loading moniker\n");
801         r = OleLoadFromStream(pStm, &IID_IMoniker, (LPVOID*)&(This->Moniker));
802         if (FAILED(r))
803             goto end;
804         This->absolute = hdr[1] & HLINK_SAVE_MONIKER_IS_ABSOLUTE ? TRUE : FALSE;
805     }
806
807     if (hdr[1] & HLINK_SAVE_LOCATION_PRESENT)
808     {
809         TRACE("loading location\n");
810         r = read_hlink_string(pStm, &This->Location);
811         if (FAILED(r)) goto end;
812     }
813
814 end:
815     TRACE("Load Result 0x%x (%p)\n", r, This->Moniker);
816
817     return r;
818 }
819
820 static HRESULT WINAPI IPersistStream_fnSave(IPersistStream* iface,
821         IStream* pStm, BOOL fClearDirty)
822 {
823     HRESULT r;
824     HlinkImpl *This = impl_from_IPersistStream(iface);
825     DWORD hdr[2];
826     IMoniker *moniker;
827
828     TRACE("(%p) Moniker(%p)\n", This, This->Moniker);
829
830     r = __GetMoniker(This, &moniker, HLINKGETREF_DEFAULT);
831     if (FAILED(r))
832         return r;
833     r = E_FAIL;
834
835     hdr[0] = HLINK_SAVE_MAGIC;
836     hdr[1] = 0;
837
838     if (moniker)
839         hdr[1] |= HLINK_SAVE_MONIKER_PRESENT;
840     if (This->absolute)
841         hdr[1] |= HLINK_SAVE_MONIKER_IS_ABSOLUTE;
842     if (This->Location)
843         hdr[1] |= HLINK_SAVE_LOCATION_PRESENT;
844     if (This->FriendlyName)
845         hdr[1] |= HLINK_SAVE_FRIENDLY_PRESENT | 4 /* FIXME */;
846     if (This->TargetFrameName)
847         hdr[1] |= HLINK_SAVE_TARGET_FRAME_PRESENT;
848
849     IStream_Write(pStm, hdr, sizeof(hdr), NULL);
850
851     if (This->TargetFrameName)
852     {
853         r = write_hlink_string(pStm, This->TargetFrameName);
854         if (FAILED(r)) goto end;
855     }
856
857     if (This->FriendlyName)
858     {
859         r = write_hlink_string(pStm, This->FriendlyName);
860         if (FAILED(r)) goto end;
861     }
862
863     if (moniker)
864     {
865         IPersistStream* monstream;
866
867         monstream = NULL;
868         IMoniker_QueryInterface(moniker, &IID_IPersistStream,
869                 (LPVOID*)&monstream);
870         if (monstream)
871         {
872             r = OleSaveToStream(monstream, pStm);
873             IPersistStream_Release(monstream);
874         }
875         if (FAILED(r)) goto end;
876     }
877
878     if (This->Location)
879     {
880         r = write_hlink_string(pStm, This->Location);
881         if (FAILED(r)) goto end;
882     }
883
884 end:
885     if (moniker) IMoniker_Release(moniker);
886     TRACE("Save Result 0x%x\n", r);
887
888     return r;
889 }
890
891 static HRESULT WINAPI IPersistStream_fnGetSizeMax(IPersistStream* iface,
892         ULARGE_INTEGER* pcbSize)
893 {
894     HRESULT r;
895     HlinkImpl *This = impl_from_IPersistStream(iface);
896     IMoniker *moniker;
897
898     TRACE("(%p) Moniker(%p)\n", This, This->Moniker);
899
900     pcbSize->QuadPart = sizeof(DWORD)*2;
901
902     if (This->TargetFrameName)
903         pcbSize->QuadPart += size_hlink_string(This->TargetFrameName);
904
905     if (This->FriendlyName)
906         pcbSize->QuadPart += size_hlink_string(This->FriendlyName);
907
908     r = __GetMoniker(This, &moniker, HLINKGETREF_DEFAULT);
909     if (FAILED(r))
910         return r;
911     r = E_FAIL;
912
913     if (moniker)
914     {
915         IPersistStream* monstream = NULL;
916         IMoniker_QueryInterface(moniker, &IID_IPersistStream,
917                 (LPVOID*)&monstream);
918         if (monstream)
919         {
920             ULARGE_INTEGER mon_size;
921             r = IPersistStream_GetSizeMax(monstream, &mon_size);
922             pcbSize->QuadPart += mon_size.QuadPart;
923             IPersistStream_Release(monstream);
924         }
925         IMoniker_Release(moniker);
926     }
927
928     if (This->Location)
929         pcbSize->QuadPart += size_hlink_string(This->Location);
930
931     return r;
932 }
933
934 static const IPersistStreamVtbl psvt =
935 {
936     IPersistStream_fnQueryInterface,
937     IPersistStream_fnAddRef,
938     IPersistStream_fnRelease,
939     IPersistStream_fnGetClassID,
940     IPersistStream_fnIsDirty,
941     IPersistStream_fnLoad,
942     IPersistStream_fnSave,
943     IPersistStream_fnGetSizeMax,
944 };