jscript: No longer support per-statement compilation.
[wine/multimedia.git] / dlls / hlink / link.c
bloba9297823ebf4c60dfc0db5f7c53841a79a921596
1 /*
2 * Implementation of hyperlinking (hlink.dll)
4 * Copyright 2005 Aric Stewart for CodeWeavers
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.
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.
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
21 #include "hlink_private.h"
23 #include "shellapi.h"
24 #include "hlguids.h"
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(hlink);
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)
40 typedef struct
42 IHlink IHlink_iface;
43 LONG ref;
45 IPersistStream IPersistStream_iface;
46 IDataObject IDataObject_iface;
48 LPWSTR FriendlyName;
49 LPWSTR Location;
50 LPWSTR TargetFrameName;
51 IMoniker *Moniker;
52 IHlinkSite *Site;
53 DWORD SiteData;
54 BOOL absolute;
55 } HlinkImpl;
57 static inline HlinkImpl *impl_from_IHlink(IHlink *iface)
59 return CONTAINING_RECORD(iface, HlinkImpl, IHlink_iface);
63 static inline HlinkImpl* impl_from_IPersistStream( IPersistStream* iface)
65 return CONTAINING_RECORD(iface, HlinkImpl, IPersistStream_iface);
68 static inline HlinkImpl* impl_from_IDataObject( IDataObject* iface)
70 return CONTAINING_RECORD(iface, HlinkImpl, IDataObject_iface);
73 static HRESULT __GetMoniker(HlinkImpl* This, IMoniker** moniker,
74 DWORD ref_type)
76 HRESULT hres;
78 if (ref_type == HLINKGETREF_DEFAULT)
79 ref_type = HLINKGETREF_RELATIVE;
81 if (ref_type == HLINKGETREF_ABSOLUTE && This->Site)
83 IMoniker *hls_moniker;
85 hres = IHlinkSite_GetMoniker(This->Site, This->SiteData,
86 OLEGETMONIKER_FORCEASSIGN, OLEWHICHMK_CONTAINER, &hls_moniker);
87 if (FAILED(hres))
88 return hres;
90 if (This->Moniker)
92 hres = IMoniker_ComposeWith(hls_moniker, This->Moniker, FALSE,
93 moniker);
94 IMoniker_Release(hls_moniker);
95 return hres;
98 *moniker = hls_moniker;
99 return S_OK;
102 *moniker = This->Moniker;
103 if (*moniker)
104 IMoniker_AddRef(*moniker);
106 return S_OK;
109 static HRESULT WINAPI IHlink_fnQueryInterface(IHlink* iface, REFIID riid,
110 LPVOID *ppvObj)
112 HlinkImpl *This = impl_from_IHlink(iface);
114 TRACE ("(%p)->(%s,%p)\n", This, debugstr_guid (riid), ppvObj);
116 *ppvObj = NULL;
118 if (IsEqualIID(riid, &IID_IUnknown) || (IsEqualIID(riid, &IID_IHlink)))
119 *ppvObj = This;
120 else if (IsEqualIID(riid, &IID_IPersistStream))
121 *ppvObj = &This->IPersistStream_iface;
122 else if (IsEqualIID(riid, &IID_IDataObject))
123 *ppvObj = &This->IDataObject_iface;
125 if (*ppvObj)
127 IUnknown_AddRef((IUnknown*)(*ppvObj));
128 return S_OK;
130 return E_NOINTERFACE;
133 static ULONG WINAPI IHlink_fnAddRef (IHlink* iface)
135 HlinkImpl *This = impl_from_IHlink(iface);
136 ULONG refCount = InterlockedIncrement(&This->ref);
138 TRACE("(%p)->(count=%u)\n", This, refCount - 1);
140 return refCount;
143 static ULONG WINAPI IHlink_fnRelease (IHlink* iface)
145 HlinkImpl *This = impl_from_IHlink(iface);
146 ULONG refCount = InterlockedDecrement(&This->ref);
148 TRACE("(%p)->(count=%u)\n", This, refCount + 1);
149 if (refCount)
150 return refCount;
152 TRACE("-- destroying IHlink (%p)\n", This);
153 heap_free(This->FriendlyName);
154 heap_free(This->TargetFrameName);
155 heap_free(This->Location);
156 if (This->Moniker)
157 IMoniker_Release(This->Moniker);
158 if (This->Site)
159 IHlinkSite_Release(This->Site);
160 heap_free(This);
161 return 0;
164 static HRESULT WINAPI IHlink_fnSetHlinkSite( IHlink* iface,
165 IHlinkSite* pihlSite, DWORD dwSiteData)
167 HlinkImpl *This = impl_from_IHlink(iface);
169 TRACE("(%p)->(%p %i)\n", This, pihlSite, dwSiteData);
171 if (This->Site)
172 IHlinkSite_Release(This->Site);
174 This->Site = pihlSite;
175 if (This->Site)
176 IHlinkSite_AddRef(This->Site);
178 This->SiteData = dwSiteData;
180 return S_OK;
183 static HRESULT WINAPI IHlink_fnGetHlinkSite( IHlink* iface,
184 IHlinkSite** ppihlSite, DWORD *pdwSiteData)
186 HlinkImpl *This = impl_from_IHlink(iface);
188 TRACE("(%p)->(%p %p)\n", This, ppihlSite, pdwSiteData);
190 *ppihlSite = This->Site;
192 if (This->Site) {
193 IHlinkSite_AddRef(This->Site);
194 *pdwSiteData = This->SiteData;
197 return S_OK;
200 static HRESULT WINAPI IHlink_fnSetMonikerReference( IHlink* iface,
201 DWORD rfHLSETF, IMoniker *pmkTarget, LPCWSTR pwzLocation)
203 HlinkImpl *This = impl_from_IHlink(iface);
205 TRACE("(%p)->(%i %p %s)\n", This, rfHLSETF, pmkTarget,
206 debugstr_w(pwzLocation));
208 if(rfHLSETF == 0)
209 return E_INVALIDARG;
210 if(!(rfHLSETF & (HLINKSETF_TARGET | HLINKSETF_LOCATION)))
211 return rfHLSETF;
213 if(rfHLSETF & HLINKSETF_TARGET){
214 if (This->Moniker)
215 IMoniker_Release(This->Moniker);
217 This->Moniker = pmkTarget;
218 if (This->Moniker)
220 LPOLESTR display_name;
221 IMoniker_AddRef(This->Moniker);
222 IMoniker_GetDisplayName(This->Moniker, NULL, NULL, &display_name);
223 This->absolute = display_name && strchrW(display_name, ':');
224 CoTaskMemFree(display_name);
228 if(rfHLSETF & HLINKSETF_LOCATION){
229 heap_free(This->Location);
230 This->Location = hlink_strdupW( pwzLocation );
233 return S_OK;
236 static HRESULT WINAPI IHlink_fnSetStringReference(IHlink* iface,
237 DWORD grfHLSETF, LPCWSTR pwzTarget, LPCWSTR pwzLocation)
239 HlinkImpl *This = impl_from_IHlink(iface);
241 TRACE("(%p)->(%i %s %s)\n", This, grfHLSETF, debugstr_w(pwzTarget),
242 debugstr_w(pwzLocation));
244 if(grfHLSETF > (HLINKSETF_TARGET | HLINKSETF_LOCATION) &&
245 grfHLSETF < -(HLINKSETF_TARGET | HLINKSETF_LOCATION))
246 return grfHLSETF;
248 if (grfHLSETF & HLINKSETF_TARGET)
250 if (This->Moniker)
252 IMoniker_Release(This->Moniker);
253 This->Moniker = NULL;
255 if (pwzTarget && *pwzTarget)
257 IMoniker *pMon;
258 IBindCtx *pbc = NULL;
259 ULONG eaten;
260 HRESULT r;
262 r = CreateBindCtx(0, &pbc);
263 if (FAILED(r))
264 return E_OUTOFMEMORY;
266 r = MkParseDisplayName(pbc, pwzTarget, &eaten, &pMon);
267 IBindCtx_Release(pbc);
269 if (FAILED(r))
271 LPCWSTR p = strchrW(pwzTarget, ':');
272 if (p && (p - pwzTarget > 1))
273 r = CreateURLMoniker(NULL, pwzTarget, &pMon);
274 else
275 r = CreateFileMoniker(pwzTarget, &pMon);
276 if (FAILED(r))
278 ERR("couldn't create moniker for %s, failed with error 0x%08x\n",
279 debugstr_w(pwzTarget), r);
280 return r;
284 IHlink_SetMonikerReference(iface, HLINKSETF_TARGET, pMon, NULL);
285 IMoniker_Release(pMon);
289 if (grfHLSETF & HLINKSETF_LOCATION)
291 heap_free(This->Location);
292 This->Location = NULL;
293 if (pwzLocation && *pwzLocation)
294 This->Location = hlink_strdupW( pwzLocation );
297 return S_OK;
300 static HRESULT WINAPI IHlink_fnGetMonikerReference(IHlink* iface,
301 DWORD dwWhichRef, IMoniker **ppimkTarget, LPWSTR *ppwzLocation)
303 HlinkImpl *This = impl_from_IHlink(iface);
305 TRACE("(%p) -> (%i %p %p)\n", This, dwWhichRef, ppimkTarget,
306 ppwzLocation);
308 if (ppimkTarget)
310 HRESULT hres = __GetMoniker(This, ppimkTarget, dwWhichRef);
311 if (FAILED(hres))
313 if (ppwzLocation)
314 *ppwzLocation = NULL;
315 return hres;
319 if (ppwzLocation)
320 IHlink_GetStringReference(iface, dwWhichRef, NULL, ppwzLocation);
322 return S_OK;
325 static HRESULT WINAPI IHlink_fnGetStringReference (IHlink* iface,
326 DWORD dwWhichRef, LPWSTR *ppwzTarget, LPWSTR *ppwzLocation)
328 HlinkImpl *This = impl_from_IHlink(iface);
330 TRACE("(%p) -> (%i %p %p)\n", This, dwWhichRef, ppwzTarget, ppwzLocation);
332 if(dwWhichRef != -1 && dwWhichRef & ~(HLINKGETREF_DEFAULT | HLINKGETREF_ABSOLUTE | HLINKGETREF_RELATIVE))
334 if(ppwzTarget)
335 *ppwzTarget = NULL;
336 if(ppwzLocation)
337 *ppwzLocation = NULL;
338 return E_INVALIDARG;
341 if (ppwzTarget)
343 IMoniker* mon;
344 HRESULT hres = __GetMoniker(This, &mon, dwWhichRef);
345 if (FAILED(hres))
347 if (ppwzLocation)
348 *ppwzLocation = NULL;
349 return hres;
351 if (mon)
353 IBindCtx *pbc;
355 CreateBindCtx( 0, &pbc);
356 IMoniker_GetDisplayName(mon, pbc, NULL, ppwzTarget);
357 IBindCtx_Release(pbc);
358 IMoniker_Release(mon);
360 else
361 *ppwzTarget = NULL;
363 if (ppwzLocation)
364 *ppwzLocation = hlink_co_strdupW( This->Location );
366 TRACE("(Target: %s Location: %s)\n",
367 (ppwzTarget)?debugstr_w(*ppwzTarget):"<NULL>",
368 (ppwzLocation)?debugstr_w(*ppwzLocation):"<NULL>");
370 return S_OK;
373 static HRESULT WINAPI IHlink_fnSetFriendlyName (IHlink *iface,
374 LPCWSTR pwzFriendlyName)
376 HlinkImpl *This = impl_from_IHlink(iface);
378 TRACE("(%p) -> (%s)\n", This, debugstr_w(pwzFriendlyName));
380 heap_free(This->FriendlyName);
381 This->FriendlyName = hlink_strdupW( pwzFriendlyName );
383 return S_OK;
386 static HRESULT WINAPI IHlink_fnGetFriendlyName (IHlink* iface,
387 DWORD grfHLFNAMEF, LPWSTR* ppwzFriendlyName)
389 HlinkImpl *This = impl_from_IHlink(iface);
391 TRACE("(%p) -> (%i %p)\n", This, grfHLFNAMEF, ppwzFriendlyName);
393 /* FIXME: Only using explicitly set and cached friendly names */
395 if (This->FriendlyName)
396 *ppwzFriendlyName = hlink_co_strdupW( This->FriendlyName );
397 else
399 IMoniker *moniker;
400 HRESULT hres = __GetMoniker(This, &moniker, HLINKGETREF_DEFAULT);
401 if (FAILED(hres))
403 *ppwzFriendlyName = NULL;
404 return hres;
406 if (moniker)
408 IBindCtx *bcxt;
409 CreateBindCtx(0, &bcxt);
411 IMoniker_GetDisplayName(moniker, bcxt, NULL, ppwzFriendlyName);
412 IBindCtx_Release(bcxt);
413 IMoniker_Release(moniker);
415 else
416 *ppwzFriendlyName = NULL;
419 return S_OK;
422 static HRESULT WINAPI IHlink_fnSetTargetFrameName(IHlink* iface,
423 LPCWSTR pwzTargetFramename)
425 HlinkImpl *This = impl_from_IHlink(iface);
426 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzTargetFramename));
428 heap_free(This->TargetFrameName);
429 This->TargetFrameName = hlink_strdupW( pwzTargetFramename );
431 return S_OK;
434 static HRESULT WINAPI IHlink_fnGetTargetFrameName(IHlink* iface,
435 LPWSTR *ppwzTargetFrameName)
437 HlinkImpl *This = impl_from_IHlink(iface);
439 TRACE("(%p)->(%p)\n", This, ppwzTargetFrameName);
441 if(!This->TargetFrameName) {
442 *ppwzTargetFrameName = NULL;
443 return S_FALSE;
446 *ppwzTargetFrameName = hlink_co_strdupW( This->TargetFrameName );
447 if(!*ppwzTargetFrameName)
448 return E_OUTOFMEMORY;
450 return S_OK;
453 static HRESULT WINAPI IHlink_fnGetMiscStatus(IHlink* iface, DWORD* pdwStatus)
455 FIXME("\n");
456 return E_NOTIMPL;
459 static HRESULT WINAPI IHlink_fnNavigate(IHlink* iface, DWORD grfHLNF, LPBC pbc,
460 IBindStatusCallback *pbsc, IHlinkBrowseContext *phbc)
462 HlinkImpl *This = impl_from_IHlink(iface);
463 IMoniker *mon = NULL;
464 HRESULT r;
466 FIXME("Semi-Stub:(%p)->(%i %p %p %p)\n", This, grfHLNF, pbc, pbsc, phbc);
468 r = __GetMoniker(This, &mon, HLINKGETREF_ABSOLUTE);
469 TRACE("Moniker %p\n", mon);
471 if (SUCCEEDED(r))
473 IBindCtx *bcxt;
474 IHlinkTarget *target = NULL;
476 CreateBindCtx(0, &bcxt);
478 RegisterBindStatusCallback(bcxt, pbsc, NULL, 0);
480 r = IMoniker_BindToObject(mon, bcxt, NULL, &IID_IHlinkTarget,
481 (LPVOID*)&target);
482 TRACE("IHlinkTarget returned 0x%x\n", r);
483 if (r == S_OK)
485 IHlinkTarget_SetBrowseContext(target, phbc);
486 IHlinkTarget_Navigate(target, grfHLNF, This->Location);
487 IHlinkTarget_Release(target);
489 else
491 static const WCHAR szOpen[] = {'o','p','e','n',0};
492 LPWSTR target = NULL;
494 r = IHlink_GetStringReference(iface, HLINKGETREF_DEFAULT, &target, NULL);
495 if (SUCCEEDED(r) && target)
497 ShellExecuteW(NULL, szOpen, target, NULL, NULL, SW_SHOW);
498 CoTaskMemFree(target);
502 RevokeBindStatusCallback(bcxt, pbsc);
504 IBindCtx_Release(bcxt);
505 IMoniker_Release(mon);
508 if (This->Site)
509 IHlinkSite_OnNavigationComplete(This->Site, This->SiteData, 0, r, NULL);
511 TRACE("Finished Navigation\n");
512 return r;
515 static HRESULT WINAPI IHlink_fnSetAdditonalParams(IHlink* iface,
516 LPCWSTR pwzAdditionalParams)
518 TRACE("Not implemented in native IHlink\n");
519 return E_NOTIMPL;
522 static HRESULT WINAPI IHlink_fnGetAdditionalParams(IHlink* iface,
523 LPWSTR* ppwzAdditionalParams)
525 TRACE("Not implemented in native IHlink\n");
526 return E_NOTIMPL;
529 static const IHlinkVtbl hlvt =
531 IHlink_fnQueryInterface,
532 IHlink_fnAddRef,
533 IHlink_fnRelease,
534 IHlink_fnSetHlinkSite,
535 IHlink_fnGetHlinkSite,
536 IHlink_fnSetMonikerReference,
537 IHlink_fnGetMonikerReference,
538 IHlink_fnSetStringReference,
539 IHlink_fnGetStringReference,
540 IHlink_fnSetFriendlyName,
541 IHlink_fnGetFriendlyName,
542 IHlink_fnSetTargetFrameName,
543 IHlink_fnGetTargetFrameName,
544 IHlink_fnGetMiscStatus,
545 IHlink_fnNavigate,
546 IHlink_fnSetAdditonalParams,
547 IHlink_fnGetAdditionalParams
550 static HRESULT WINAPI IDataObject_fnQueryInterface(IDataObject* iface,
551 REFIID riid, LPVOID *ppvObj)
553 HlinkImpl *This = impl_from_IDataObject(iface);
554 TRACE("%p\n", This);
555 return IHlink_QueryInterface(&This->IHlink_iface, riid, ppvObj);
558 static ULONG WINAPI IDataObject_fnAddRef (IDataObject* iface)
560 HlinkImpl *This = impl_from_IDataObject(iface);
561 TRACE("%p\n", This);
562 return IHlink_AddRef(&This->IHlink_iface);
565 static ULONG WINAPI IDataObject_fnRelease (IDataObject* iface)
567 HlinkImpl *This = impl_from_IDataObject(iface);
568 TRACE("%p\n", This);
569 return IHlink_Release(&This->IHlink_iface);
572 static HRESULT WINAPI IDataObject_fnGetData(IDataObject* iface,
573 FORMATETC* pformatetcIn, STGMEDIUM* pmedium)
575 FIXME("\n");
576 return E_NOTIMPL;
579 static HRESULT WINAPI IDataObject_fnGetDataHere(IDataObject* iface,
580 FORMATETC* pformatetc, STGMEDIUM* pmedium)
582 FIXME("\n");
583 return E_NOTIMPL;
586 static HRESULT WINAPI IDataObject_fnQueryGetData(IDataObject* iface,
587 FORMATETC* pformatetc)
589 FIXME("\n");
590 return E_NOTIMPL;
593 static HRESULT WINAPI IDataObject_fnGetConicalFormatEtc(IDataObject* iface,
594 FORMATETC* pformatetcIn, FORMATETC* pformatetcOut)
596 FIXME("\n");
597 return E_NOTIMPL;
600 static HRESULT WINAPI IDataObject_fnSetData(IDataObject* iface,
601 FORMATETC* pformatetc, STGMEDIUM* pmedium, BOOL fRelease)
603 FIXME("\n");
604 return E_NOTIMPL;
607 static HRESULT WINAPI IDataObject_fnEnumFormatEtc(IDataObject* iface,
608 DWORD dwDirection, IEnumFORMATETC** ppenumFormatEtc)
610 FIXME("\n");
611 return E_NOTIMPL;
614 static HRESULT WINAPI IDataObject_fnDAdvise(IDataObject* iface,
615 FORMATETC* pformatetc, DWORD advf, IAdviseSink* pAdvSink,
616 DWORD* pdwConnection)
618 FIXME("\n");
619 return E_NOTIMPL;
622 static HRESULT WINAPI IDataObject_fnDUnadvise(IDataObject* iface,
623 DWORD dwConnection)
625 FIXME("\n");
626 return E_NOTIMPL;
629 static HRESULT WINAPI IDataObject_fnEnumDAdvise(IDataObject* iface,
630 IEnumSTATDATA** ppenumAdvise)
632 FIXME("\n");
633 return E_NOTIMPL;
636 static const IDataObjectVtbl dovt =
638 IDataObject_fnQueryInterface,
639 IDataObject_fnAddRef,
640 IDataObject_fnRelease,
641 IDataObject_fnGetData,
642 IDataObject_fnGetDataHere,
643 IDataObject_fnQueryGetData,
644 IDataObject_fnGetConicalFormatEtc,
645 IDataObject_fnSetData,
646 IDataObject_fnEnumFormatEtc,
647 IDataObject_fnDAdvise,
648 IDataObject_fnDUnadvise,
649 IDataObject_fnEnumDAdvise
652 static HRESULT WINAPI IPersistStream_fnQueryInterface(IPersistStream* iface,
653 REFIID riid, LPVOID *ppvObj)
655 HlinkImpl *This = impl_from_IPersistStream(iface);
656 TRACE("(%p)\n", This);
657 return IHlink_QueryInterface(&This->IHlink_iface, riid, ppvObj);
660 static ULONG WINAPI IPersistStream_fnAddRef (IPersistStream* iface)
662 HlinkImpl *This = impl_from_IPersistStream(iface);
663 TRACE("(%p)\n", This);
664 return IHlink_AddRef(&This->IHlink_iface);
667 static ULONG WINAPI IPersistStream_fnRelease (IPersistStream* iface)
669 HlinkImpl *This = impl_from_IPersistStream(iface);
670 TRACE("(%p)\n", This);
671 return IHlink_Release(&This->IHlink_iface);
674 static HRESULT WINAPI IPersistStream_fnGetClassID(IPersistStream* iface,
675 CLSID* pClassID)
677 HlinkImpl *This = impl_from_IPersistStream(iface);
678 TRACE("(%p)\n", This);
679 *pClassID = CLSID_StdHlink;
680 return S_OK;
683 static HRESULT WINAPI IPersistStream_fnIsDirty(IPersistStream* iface)
685 FIXME("\n");
686 return E_NOTIMPL;
689 static HRESULT write_hlink_string(IStream *pStm, LPCWSTR str)
691 DWORD len;
692 HRESULT hr;
694 TRACE("(%p, %s)\n", pStm, debugstr_w(str));
696 len = strlenW(str) + 1;
698 hr = IStream_Write(pStm, &len, sizeof(len), NULL);
699 if (FAILED(hr)) return hr;
701 hr = IStream_Write(pStm, str, len * sizeof(WCHAR), NULL);
702 if (FAILED(hr)) return hr;
704 return S_OK;
707 static inline ULONG size_hlink_string(LPCWSTR str)
709 return sizeof(DWORD) + (strlenW(str) + 1) * sizeof(WCHAR);
712 static HRESULT read_hlink_string(IStream *pStm, LPWSTR *out_str)
714 LPWSTR str;
715 DWORD len;
716 ULONG read;
717 HRESULT hr;
719 hr = IStream_Read(pStm, &len, sizeof(len), &read);
720 if (FAILED(hr)) return hr;
721 if (read != sizeof(len)) return STG_E_READFAULT;
723 TRACE("read len %d\n", len);
725 str = heap_alloc(len * sizeof(WCHAR));
726 if (!str) return E_OUTOFMEMORY;
728 hr = IStream_Read(pStm, str, len * sizeof(WCHAR), &read);
729 if (FAILED(hr))
731 heap_free(str);
732 return hr;
734 if (read != len * sizeof(WCHAR))
736 heap_free(str);
737 return STG_E_READFAULT;
739 TRACE("read string %s\n", debugstr_w(str));
741 *out_str = str;
742 return S_OK;
745 static HRESULT WINAPI IPersistStream_fnLoad(IPersistStream* iface,
746 IStream* pStm)
748 HRESULT r;
749 DWORD hdr[2];
750 DWORD read;
751 HlinkImpl *This = impl_from_IPersistStream(iface);
753 r = IStream_Read(pStm, hdr, sizeof(hdr), &read);
754 if (read != sizeof(hdr) || (hdr[0] != HLINK_SAVE_MAGIC))
756 r = E_FAIL;
757 goto end;
759 if (hdr[1] & ~HLINK_SAVE_ALL)
760 FIXME("unknown flag(s) 0x%x\n", hdr[1] & ~HLINK_SAVE_ALL);
762 if (hdr[1] & HLINK_SAVE_TARGET_FRAME_PRESENT)
764 TRACE("loading target frame name\n");
765 r = read_hlink_string(pStm, &This->TargetFrameName);
766 if (FAILED(r)) goto end;
769 if (hdr[1] & HLINK_SAVE_FRIENDLY_PRESENT)
771 TRACE("loading target friendly name\n");
772 if (!(hdr[1] & 0x4))
773 FIXME("0x4 flag not present with friendly name flag - not sure what this means\n");
774 r = read_hlink_string(pStm, &This->FriendlyName);
775 if (FAILED(r)) goto end;
778 if (hdr[1] & HLINK_SAVE_MONIKER_PRESENT)
780 TRACE("loading moniker\n");
781 r = OleLoadFromStream(pStm, &IID_IMoniker, (LPVOID*)&(This->Moniker));
782 if (FAILED(r))
783 goto end;
784 This->absolute = hdr[1] & HLINK_SAVE_MONIKER_IS_ABSOLUTE ? TRUE : FALSE;
787 if (hdr[1] & HLINK_SAVE_LOCATION_PRESENT)
789 TRACE("loading location\n");
790 r = read_hlink_string(pStm, &This->Location);
791 if (FAILED(r)) goto end;
794 end:
795 TRACE("Load Result 0x%x (%p)\n", r, This->Moniker);
797 return r;
800 static HRESULT WINAPI IPersistStream_fnSave(IPersistStream* iface,
801 IStream* pStm, BOOL fClearDirty)
803 HRESULT r;
804 HlinkImpl *This = impl_from_IPersistStream(iface);
805 DWORD hdr[2];
806 IMoniker *moniker;
808 TRACE("(%p) Moniker(%p)\n", This, This->Moniker);
810 r = __GetMoniker(This, &moniker, HLINKGETREF_DEFAULT);
811 if (FAILED(r))
812 return r;
813 r = E_FAIL;
815 hdr[0] = HLINK_SAVE_MAGIC;
816 hdr[1] = 0;
818 if (moniker)
819 hdr[1] |= HLINK_SAVE_MONIKER_PRESENT;
820 if (This->absolute)
821 hdr[1] |= HLINK_SAVE_MONIKER_IS_ABSOLUTE;
822 if (This->Location)
823 hdr[1] |= HLINK_SAVE_LOCATION_PRESENT;
824 if (This->FriendlyName)
825 hdr[1] |= HLINK_SAVE_FRIENDLY_PRESENT | 4 /* FIXME */;
826 if (This->TargetFrameName)
827 hdr[1] |= HLINK_SAVE_TARGET_FRAME_PRESENT;
829 IStream_Write(pStm, hdr, sizeof(hdr), NULL);
831 if (This->TargetFrameName)
833 r = write_hlink_string(pStm, This->TargetFrameName);
834 if (FAILED(r)) goto end;
837 if (This->FriendlyName)
839 r = write_hlink_string(pStm, This->FriendlyName);
840 if (FAILED(r)) goto end;
843 if (moniker)
845 IPersistStream* monstream;
847 monstream = NULL;
848 IMoniker_QueryInterface(moniker, &IID_IPersistStream,
849 (LPVOID*)&monstream);
850 if (monstream)
852 r = OleSaveToStream(monstream, pStm);
853 IPersistStream_Release(monstream);
855 if (FAILED(r)) goto end;
858 if (This->Location)
860 r = write_hlink_string(pStm, This->Location);
861 if (FAILED(r)) goto end;
864 end:
865 if (moniker) IMoniker_Release(moniker);
866 TRACE("Save Result 0x%x\n", r);
868 return r;
871 static HRESULT WINAPI IPersistStream_fnGetSizeMax(IPersistStream* iface,
872 ULARGE_INTEGER* pcbSize)
874 HRESULT r;
875 HlinkImpl *This = impl_from_IPersistStream(iface);
876 IMoniker *moniker;
878 TRACE("(%p) Moniker(%p)\n", This, This->Moniker);
880 pcbSize->QuadPart = sizeof(DWORD)*2;
882 if (This->TargetFrameName)
883 pcbSize->QuadPart += size_hlink_string(This->TargetFrameName);
885 if (This->FriendlyName)
886 pcbSize->QuadPart += size_hlink_string(This->FriendlyName);
888 r = __GetMoniker(This, &moniker, HLINKGETREF_DEFAULT);
889 if (FAILED(r))
890 return r;
891 r = E_FAIL;
893 if (moniker)
895 IPersistStream* monstream = NULL;
896 IMoniker_QueryInterface(moniker, &IID_IPersistStream,
897 (LPVOID*)&monstream);
898 if (monstream)
900 ULARGE_INTEGER mon_size;
901 r = IPersistStream_GetSizeMax(monstream, &mon_size);
902 pcbSize->QuadPart += mon_size.QuadPart;
903 IPersistStream_Release(monstream);
905 IMoniker_Release(moniker);
908 if (This->Location)
909 pcbSize->QuadPart += size_hlink_string(This->Location);
911 return r;
914 static const IPersistStreamVtbl psvt =
916 IPersistStream_fnQueryInterface,
917 IPersistStream_fnAddRef,
918 IPersistStream_fnRelease,
919 IPersistStream_fnGetClassID,
920 IPersistStream_fnIsDirty,
921 IPersistStream_fnLoad,
922 IPersistStream_fnSave,
923 IPersistStream_fnGetSizeMax,
926 HRESULT HLink_Constructor(IUnknown *pUnkOuter, REFIID riid, void **ppv)
928 HlinkImpl * hl;
930 TRACE("unkOut=%p riid=%s\n", pUnkOuter, debugstr_guid(riid));
931 *ppv = NULL;
933 if (pUnkOuter)
934 return CLASS_E_NOAGGREGATION;
936 hl = heap_alloc_zero(sizeof(HlinkImpl));
937 if (!hl)
938 return E_OUTOFMEMORY;
940 hl->ref = 1;
941 hl->IHlink_iface.lpVtbl = &hlvt;
942 hl->IPersistStream_iface.lpVtbl = &psvt;
943 hl->IDataObject_iface.lpVtbl = &dovt;
945 *ppv = hl;
946 return S_OK;