krnl386.exe16: Make a couple of functions static.
[wine.git] / dlls / hlink / link.c
blob25ee45d7c7e26a018cf821eb230a7a6712103cef
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 (This->Moniker)
83 DWORD mktype = MKSYS_NONE;
85 hres = IMoniker_IsSystemMoniker(This->Moniker, &mktype);
86 if (hres == S_OK && mktype != MKSYS_NONE)
88 *moniker = This->Moniker;
89 IMoniker_AddRef(*moniker);
90 return S_OK;
94 if (ref_type == HLINKGETREF_ABSOLUTE && This->Site)
96 IMoniker *hls_moniker;
98 hres = IHlinkSite_GetMoniker(This->Site, This->SiteData,
99 OLEGETMONIKER_FORCEASSIGN, OLEWHICHMK_CONTAINER, &hls_moniker);
100 if (FAILED(hres))
101 return hres;
103 if (This->Moniker)
105 hres = IMoniker_ComposeWith(hls_moniker, This->Moniker, FALSE,
106 moniker);
107 IMoniker_Release(hls_moniker);
108 return hres;
111 *moniker = hls_moniker;
112 return S_OK;
115 *moniker = This->Moniker;
116 if (*moniker)
117 IMoniker_AddRef(*moniker);
119 return S_OK;
122 static HRESULT WINAPI IHlink_fnQueryInterface(IHlink* iface, REFIID riid,
123 LPVOID *ppvObj)
125 HlinkImpl *This = impl_from_IHlink(iface);
127 TRACE ("(%p)->(%s,%p)\n", This, debugstr_guid (riid), ppvObj);
129 *ppvObj = NULL;
131 if (IsEqualIID(riid, &IID_IUnknown) || (IsEqualIID(riid, &IID_IHlink)))
132 *ppvObj = &This->IHlink_iface;
133 else if (IsEqualIID(riid, &IID_IPersistStream))
134 *ppvObj = &This->IPersistStream_iface;
135 else if (IsEqualIID(riid, &IID_IDataObject))
136 *ppvObj = &This->IDataObject_iface;
138 if (*ppvObj)
140 IUnknown_AddRef((IUnknown*)(*ppvObj));
141 return S_OK;
143 return E_NOINTERFACE;
146 static ULONG WINAPI IHlink_fnAddRef (IHlink* iface)
148 HlinkImpl *This = impl_from_IHlink(iface);
149 ULONG refCount = InterlockedIncrement(&This->ref);
151 TRACE("(%p)->(count=%u)\n", This, refCount - 1);
153 return refCount;
156 static ULONG WINAPI IHlink_fnRelease (IHlink* iface)
158 HlinkImpl *This = impl_from_IHlink(iface);
159 ULONG refCount = InterlockedDecrement(&This->ref);
161 TRACE("(%p)->(count=%u)\n", This, refCount + 1);
162 if (refCount)
163 return refCount;
165 TRACE("-- destroying IHlink (%p)\n", This);
166 heap_free(This->FriendlyName);
167 heap_free(This->TargetFrameName);
168 heap_free(This->Location);
169 if (This->Moniker)
170 IMoniker_Release(This->Moniker);
171 if (This->Site)
172 IHlinkSite_Release(This->Site);
173 heap_free(This);
174 return 0;
177 static HRESULT WINAPI IHlink_fnSetHlinkSite( IHlink* iface,
178 IHlinkSite* pihlSite, DWORD dwSiteData)
180 HlinkImpl *This = impl_from_IHlink(iface);
182 TRACE("(%p)->(%p %i)\n", This, pihlSite, dwSiteData);
184 if (This->Site)
185 IHlinkSite_Release(This->Site);
187 This->Site = pihlSite;
188 if (This->Site)
189 IHlinkSite_AddRef(This->Site);
191 This->SiteData = dwSiteData;
193 return S_OK;
196 static HRESULT WINAPI IHlink_fnGetHlinkSite( IHlink* iface,
197 IHlinkSite** ppihlSite, DWORD *pdwSiteData)
199 HlinkImpl *This = impl_from_IHlink(iface);
201 TRACE("(%p)->(%p %p)\n", This, ppihlSite, pdwSiteData);
203 *ppihlSite = This->Site;
205 if (This->Site) {
206 IHlinkSite_AddRef(This->Site);
207 *pdwSiteData = This->SiteData;
210 return S_OK;
213 static HRESULT WINAPI IHlink_fnSetMonikerReference( IHlink* iface,
214 DWORD rfHLSETF, IMoniker *pmkTarget, LPCWSTR pwzLocation)
216 HlinkImpl *This = impl_from_IHlink(iface);
218 TRACE("(%p)->(%i %p %s)\n", This, rfHLSETF, pmkTarget,
219 debugstr_w(pwzLocation));
221 if(rfHLSETF == 0)
222 return E_INVALIDARG;
223 if(!(rfHLSETF & (HLINKSETF_TARGET | HLINKSETF_LOCATION)))
224 return rfHLSETF;
226 if(rfHLSETF & HLINKSETF_TARGET){
227 if (This->Moniker)
228 IMoniker_Release(This->Moniker);
230 This->Moniker = pmkTarget;
231 if (This->Moniker)
233 IBindCtx *pbc;
234 LPOLESTR display_name;
235 IMoniker_AddRef(This->Moniker);
236 CreateBindCtx( 0, &pbc);
237 IMoniker_GetDisplayName(This->Moniker, pbc, NULL, &display_name);
238 IBindCtx_Release(pbc);
239 This->absolute = display_name && strchrW(display_name, ':');
240 CoTaskMemFree(display_name);
244 if(rfHLSETF & HLINKSETF_LOCATION){
245 heap_free(This->Location);
246 This->Location = hlink_strdupW( pwzLocation );
249 return S_OK;
252 static HRESULT WINAPI IHlink_fnSetStringReference(IHlink* iface,
253 DWORD grfHLSETF, LPCWSTR pwzTarget, LPCWSTR pwzLocation)
255 HlinkImpl *This = impl_from_IHlink(iface);
257 TRACE("(%p)->(%i %s %s)\n", This, grfHLSETF, debugstr_w(pwzTarget),
258 debugstr_w(pwzLocation));
260 if(grfHLSETF > (HLINKSETF_TARGET | HLINKSETF_LOCATION) &&
261 grfHLSETF < -(HLINKSETF_TARGET | HLINKSETF_LOCATION))
262 return grfHLSETF;
264 if (grfHLSETF & HLINKSETF_TARGET)
266 if (This->Moniker)
268 IMoniker_Release(This->Moniker);
269 This->Moniker = NULL;
271 if (pwzTarget && *pwzTarget)
273 IMoniker *pMon;
274 IBindCtx *pbc = NULL;
275 ULONG eaten;
276 HRESULT r;
278 r = CreateBindCtx(0, &pbc);
279 if (FAILED(r))
280 return E_OUTOFMEMORY;
282 r = MkParseDisplayName(pbc, pwzTarget, &eaten, &pMon);
283 IBindCtx_Release(pbc);
285 if (FAILED(r))
287 LPCWSTR p = strchrW(pwzTarget, ':');
288 if (p && (p - pwzTarget > 1))
289 r = CreateURLMoniker(NULL, pwzTarget, &pMon);
290 else
291 r = CreateFileMoniker(pwzTarget, &pMon);
292 if (FAILED(r))
294 ERR("couldn't create moniker for %s, failed with error 0x%08x\n",
295 debugstr_w(pwzTarget), r);
296 return r;
300 IHlink_SetMonikerReference(iface, HLINKSETF_TARGET, pMon, NULL);
301 IMoniker_Release(pMon);
305 if (grfHLSETF & HLINKSETF_LOCATION)
307 heap_free(This->Location);
308 This->Location = NULL;
309 if (pwzLocation && *pwzLocation)
310 This->Location = hlink_strdupW( pwzLocation );
313 return S_OK;
316 static HRESULT WINAPI IHlink_fnGetMonikerReference(IHlink* iface,
317 DWORD dwWhichRef, IMoniker **ppimkTarget, LPWSTR *ppwzLocation)
319 HlinkImpl *This = impl_from_IHlink(iface);
321 TRACE("(%p) -> (%i %p %p)\n", This, dwWhichRef, ppimkTarget,
322 ppwzLocation);
324 if (ppimkTarget)
326 HRESULT hres = __GetMoniker(This, ppimkTarget, dwWhichRef);
327 if (FAILED(hres))
329 if (ppwzLocation)
330 *ppwzLocation = NULL;
331 return hres;
335 if (ppwzLocation)
336 IHlink_GetStringReference(iface, dwWhichRef, NULL, ppwzLocation);
338 return S_OK;
341 static HRESULT WINAPI IHlink_fnGetStringReference (IHlink* iface,
342 DWORD dwWhichRef, LPWSTR *ppwzTarget, LPWSTR *ppwzLocation)
344 HlinkImpl *This = impl_from_IHlink(iface);
346 TRACE("(%p) -> (%i %p %p)\n", This, dwWhichRef, ppwzTarget, ppwzLocation);
348 if(dwWhichRef != -1 && dwWhichRef & ~(HLINKGETREF_DEFAULT | HLINKGETREF_ABSOLUTE | HLINKGETREF_RELATIVE))
350 if(ppwzTarget)
351 *ppwzTarget = NULL;
352 if(ppwzLocation)
353 *ppwzLocation = NULL;
354 return E_INVALIDARG;
357 if (ppwzTarget)
359 IMoniker* mon;
360 HRESULT hres = __GetMoniker(This, &mon, dwWhichRef);
361 if (FAILED(hres))
363 if (ppwzLocation)
364 *ppwzLocation = NULL;
365 return hres;
367 if (mon)
369 IBindCtx *pbc;
371 CreateBindCtx( 0, &pbc);
372 IMoniker_GetDisplayName(mon, pbc, NULL, ppwzTarget);
373 IBindCtx_Release(pbc);
374 IMoniker_Release(mon);
376 else
377 *ppwzTarget = NULL;
379 if (ppwzLocation)
380 *ppwzLocation = hlink_co_strdupW( This->Location );
382 TRACE("(Target: %s Location: %s)\n",
383 (ppwzTarget)?debugstr_w(*ppwzTarget):"<NULL>",
384 (ppwzLocation)?debugstr_w(*ppwzLocation):"<NULL>");
386 return S_OK;
389 static HRESULT WINAPI IHlink_fnSetFriendlyName (IHlink *iface,
390 LPCWSTR pwzFriendlyName)
392 HlinkImpl *This = impl_from_IHlink(iface);
394 TRACE("(%p) -> (%s)\n", This, debugstr_w(pwzFriendlyName));
396 heap_free(This->FriendlyName);
397 This->FriendlyName = hlink_strdupW( pwzFriendlyName );
399 return S_OK;
402 static HRESULT WINAPI IHlink_fnGetFriendlyName (IHlink* iface,
403 DWORD grfHLFNAMEF, LPWSTR* ppwzFriendlyName)
405 HlinkImpl *This = impl_from_IHlink(iface);
407 TRACE("(%p) -> (%i %p)\n", This, grfHLFNAMEF, ppwzFriendlyName);
409 /* FIXME: Only using explicitly set and cached friendly names */
411 if (This->FriendlyName)
412 *ppwzFriendlyName = hlink_co_strdupW( This->FriendlyName );
413 else
415 IMoniker *moniker;
416 HRESULT hres = __GetMoniker(This, &moniker, HLINKGETREF_DEFAULT);
417 if (FAILED(hres))
419 *ppwzFriendlyName = NULL;
420 return hres;
422 if (moniker)
424 IBindCtx *bcxt;
425 CreateBindCtx(0, &bcxt);
427 IMoniker_GetDisplayName(moniker, bcxt, NULL, ppwzFriendlyName);
428 IBindCtx_Release(bcxt);
429 IMoniker_Release(moniker);
431 else
432 *ppwzFriendlyName = NULL;
435 return S_OK;
438 static HRESULT WINAPI IHlink_fnSetTargetFrameName(IHlink* iface,
439 LPCWSTR pwzTargetFramename)
441 HlinkImpl *This = impl_from_IHlink(iface);
442 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzTargetFramename));
444 heap_free(This->TargetFrameName);
445 This->TargetFrameName = hlink_strdupW( pwzTargetFramename );
447 return S_OK;
450 static HRESULT WINAPI IHlink_fnGetTargetFrameName(IHlink* iface,
451 LPWSTR *ppwzTargetFrameName)
453 HlinkImpl *This = impl_from_IHlink(iface);
455 TRACE("(%p)->(%p)\n", This, ppwzTargetFrameName);
457 if(!This->TargetFrameName) {
458 *ppwzTargetFrameName = NULL;
459 return S_FALSE;
462 *ppwzTargetFrameName = hlink_co_strdupW( This->TargetFrameName );
463 if(!*ppwzTargetFrameName)
464 return E_OUTOFMEMORY;
466 return S_OK;
469 static HRESULT WINAPI IHlink_fnGetMiscStatus(IHlink* iface, DWORD* pdwStatus)
471 FIXME("\n");
472 return E_NOTIMPL;
475 static HRESULT WINAPI IHlink_fnNavigate(IHlink* iface, DWORD grfHLNF, LPBC pbc,
476 IBindStatusCallback *pbsc, IHlinkBrowseContext *phbc)
478 HlinkImpl *This = impl_from_IHlink(iface);
479 IMoniker *mon = NULL;
480 HRESULT r;
482 FIXME("Semi-Stub:(%p)->(%i %p %p %p)\n", This, grfHLNF, pbc, pbsc, phbc);
484 r = __GetMoniker(This, &mon, HLINKGETREF_ABSOLUTE);
485 TRACE("Moniker %p\n", mon);
487 if (SUCCEEDED(r))
489 IBindCtx *bcxt = NULL;
490 IUnknown *unk = NULL;
491 IHlinkTarget *target;
493 if (phbc)
495 r = IHlinkBrowseContext_GetObject(phbc, mon, TRUE, &unk);
496 if (r != S_OK)
498 CreateBindCtx(0, &bcxt);
499 RegisterBindStatusCallback(bcxt, pbsc, NULL, 0);
500 r = IMoniker_BindToObject(mon, bcxt, NULL, &IID_IUnknown, (void**)&unk);
502 if (r == S_OK)
504 r = IUnknown_QueryInterface(unk, &IID_IHlinkTarget, (void **)&target);
505 IUnknown_Release(unk);
507 if (r == S_OK)
509 if (bcxt) IHlinkTarget_SetBrowseContext(target, phbc);
510 r = IHlinkTarget_Navigate(target, grfHLNF, This->Location);
511 IHlinkTarget_Release(target);
514 RevokeBindStatusCallback(bcxt, pbsc);
515 if (bcxt) IBindCtx_Release(bcxt);
517 else
519 static const WCHAR szOpen[] = {'o','p','e','n',0};
520 LPWSTR target = NULL;
522 r = IHlink_GetStringReference(iface, HLINKGETREF_DEFAULT, &target, NULL);
523 if (SUCCEEDED(r) && target)
525 ShellExecuteW(NULL, szOpen, target, NULL, NULL, SW_SHOW);
526 CoTaskMemFree(target);
527 r = DRAGDROP_S_DROP;
530 IMoniker_Release(mon);
533 if (This->Site)
534 IHlinkSite_OnNavigationComplete(This->Site, This->SiteData, 0, r, NULL);
536 TRACE("Finished Navigation\n");
537 return r;
540 static HRESULT WINAPI IHlink_fnSetAdditionalParams(IHlink* iface,
541 LPCWSTR pwzAdditionalParams)
543 TRACE("Not implemented in native IHlink\n");
544 return E_NOTIMPL;
547 static HRESULT WINAPI IHlink_fnGetAdditionalParams(IHlink* iface,
548 LPWSTR* ppwzAdditionalParams)
550 TRACE("Not implemented in native IHlink\n");
551 return E_NOTIMPL;
554 static const IHlinkVtbl hlvt =
556 IHlink_fnQueryInterface,
557 IHlink_fnAddRef,
558 IHlink_fnRelease,
559 IHlink_fnSetHlinkSite,
560 IHlink_fnGetHlinkSite,
561 IHlink_fnSetMonikerReference,
562 IHlink_fnGetMonikerReference,
563 IHlink_fnSetStringReference,
564 IHlink_fnGetStringReference,
565 IHlink_fnSetFriendlyName,
566 IHlink_fnGetFriendlyName,
567 IHlink_fnSetTargetFrameName,
568 IHlink_fnGetTargetFrameName,
569 IHlink_fnGetMiscStatus,
570 IHlink_fnNavigate,
571 IHlink_fnSetAdditionalParams,
572 IHlink_fnGetAdditionalParams
575 static HRESULT WINAPI IDataObject_fnQueryInterface(IDataObject* iface,
576 REFIID riid, LPVOID *ppvObj)
578 HlinkImpl *This = impl_from_IDataObject(iface);
579 TRACE("%p\n", This);
580 return IHlink_QueryInterface(&This->IHlink_iface, riid, ppvObj);
583 static ULONG WINAPI IDataObject_fnAddRef (IDataObject* iface)
585 HlinkImpl *This = impl_from_IDataObject(iface);
586 TRACE("%p\n", This);
587 return IHlink_AddRef(&This->IHlink_iface);
590 static ULONG WINAPI IDataObject_fnRelease (IDataObject* iface)
592 HlinkImpl *This = impl_from_IDataObject(iface);
593 TRACE("%p\n", This);
594 return IHlink_Release(&This->IHlink_iface);
597 static HRESULT WINAPI IDataObject_fnGetData(IDataObject* iface,
598 FORMATETC* pformatetcIn, STGMEDIUM* pmedium)
600 FIXME("\n");
601 return E_NOTIMPL;
604 static HRESULT WINAPI IDataObject_fnGetDataHere(IDataObject* iface,
605 FORMATETC* pformatetc, STGMEDIUM* pmedium)
607 FIXME("\n");
608 return E_NOTIMPL;
611 static HRESULT WINAPI IDataObject_fnQueryGetData(IDataObject* iface,
612 FORMATETC* pformatetc)
614 FIXME("\n");
615 return E_NOTIMPL;
618 static HRESULT WINAPI IDataObject_fnGetConicalFormatEtc(IDataObject* iface,
619 FORMATETC* pformatetcIn, FORMATETC* pformatetcOut)
621 FIXME("\n");
622 return E_NOTIMPL;
625 static HRESULT WINAPI IDataObject_fnSetData(IDataObject* iface,
626 FORMATETC* pformatetc, STGMEDIUM* pmedium, BOOL fRelease)
628 FIXME("\n");
629 return E_NOTIMPL;
632 static HRESULT WINAPI IDataObject_fnEnumFormatEtc(IDataObject* iface,
633 DWORD dwDirection, IEnumFORMATETC** ppenumFormatEtc)
635 FIXME("\n");
636 return E_NOTIMPL;
639 static HRESULT WINAPI IDataObject_fnDAdvise(IDataObject* iface,
640 FORMATETC* pformatetc, DWORD advf, IAdviseSink* pAdvSink,
641 DWORD* pdwConnection)
643 FIXME("\n");
644 return E_NOTIMPL;
647 static HRESULT WINAPI IDataObject_fnDUnadvise(IDataObject* iface,
648 DWORD dwConnection)
650 FIXME("\n");
651 return E_NOTIMPL;
654 static HRESULT WINAPI IDataObject_fnEnumDAdvise(IDataObject* iface,
655 IEnumSTATDATA** ppenumAdvise)
657 FIXME("\n");
658 return E_NOTIMPL;
661 static const IDataObjectVtbl dovt =
663 IDataObject_fnQueryInterface,
664 IDataObject_fnAddRef,
665 IDataObject_fnRelease,
666 IDataObject_fnGetData,
667 IDataObject_fnGetDataHere,
668 IDataObject_fnQueryGetData,
669 IDataObject_fnGetConicalFormatEtc,
670 IDataObject_fnSetData,
671 IDataObject_fnEnumFormatEtc,
672 IDataObject_fnDAdvise,
673 IDataObject_fnDUnadvise,
674 IDataObject_fnEnumDAdvise
677 static HRESULT WINAPI IPersistStream_fnQueryInterface(IPersistStream* iface,
678 REFIID riid, LPVOID *ppvObj)
680 HlinkImpl *This = impl_from_IPersistStream(iface);
681 TRACE("(%p)\n", This);
682 return IHlink_QueryInterface(&This->IHlink_iface, riid, ppvObj);
685 static ULONG WINAPI IPersistStream_fnAddRef (IPersistStream* iface)
687 HlinkImpl *This = impl_from_IPersistStream(iface);
688 TRACE("(%p)\n", This);
689 return IHlink_AddRef(&This->IHlink_iface);
692 static ULONG WINAPI IPersistStream_fnRelease (IPersistStream* iface)
694 HlinkImpl *This = impl_from_IPersistStream(iface);
695 TRACE("(%p)\n", This);
696 return IHlink_Release(&This->IHlink_iface);
699 static HRESULT WINAPI IPersistStream_fnGetClassID(IPersistStream* iface,
700 CLSID* pClassID)
702 HlinkImpl *This = impl_from_IPersistStream(iface);
703 TRACE("(%p)\n", This);
704 *pClassID = CLSID_StdHlink;
705 return S_OK;
708 static HRESULT WINAPI IPersistStream_fnIsDirty(IPersistStream* iface)
710 FIXME("\n");
711 return E_NOTIMPL;
714 static HRESULT write_hlink_string(IStream *pStm, LPCWSTR str)
716 DWORD len;
717 HRESULT hr;
719 TRACE("(%p, %s)\n", pStm, debugstr_w(str));
721 len = strlenW(str) + 1;
723 hr = IStream_Write(pStm, &len, sizeof(len), NULL);
724 if (FAILED(hr)) return hr;
726 hr = IStream_Write(pStm, str, len * sizeof(WCHAR), NULL);
727 if (FAILED(hr)) return hr;
729 return S_OK;
732 static inline ULONG size_hlink_string(LPCWSTR str)
734 return sizeof(DWORD) + (strlenW(str) + 1) * sizeof(WCHAR);
737 static HRESULT read_hlink_string(IStream *pStm, LPWSTR *out_str)
739 LPWSTR str;
740 DWORD len;
741 ULONG read;
742 HRESULT hr;
744 hr = IStream_Read(pStm, &len, sizeof(len), &read);
745 if (FAILED(hr)) return hr;
746 if (read != sizeof(len)) return STG_E_READFAULT;
748 TRACE("read len %d\n", len);
750 str = heap_alloc(len * sizeof(WCHAR));
751 if (!str) return E_OUTOFMEMORY;
753 hr = IStream_Read(pStm, str, len * sizeof(WCHAR), &read);
754 if (FAILED(hr))
756 heap_free(str);
757 return hr;
759 if (read != len * sizeof(WCHAR))
761 heap_free(str);
762 return STG_E_READFAULT;
764 TRACE("read string %s\n", debugstr_w(str));
766 *out_str = str;
767 return S_OK;
770 static HRESULT WINAPI IPersistStream_fnLoad(IPersistStream* iface,
771 IStream* pStm)
773 HRESULT r;
774 DWORD hdr[2];
775 DWORD read;
776 HlinkImpl *This = impl_from_IPersistStream(iface);
778 r = IStream_Read(pStm, hdr, sizeof(hdr), &read);
779 if (read != sizeof(hdr) || (hdr[0] != HLINK_SAVE_MAGIC))
781 r = E_FAIL;
782 goto end;
784 if (hdr[1] & ~HLINK_SAVE_ALL)
785 FIXME("unknown flag(s) 0x%x\n", hdr[1] & ~HLINK_SAVE_ALL);
787 if (hdr[1] & HLINK_SAVE_TARGET_FRAME_PRESENT)
789 TRACE("loading target frame name\n");
790 r = read_hlink_string(pStm, &This->TargetFrameName);
791 if (FAILED(r)) goto end;
794 if (hdr[1] & HLINK_SAVE_FRIENDLY_PRESENT)
796 TRACE("loading target friendly name\n");
797 if (!(hdr[1] & 0x4))
798 FIXME("0x4 flag not present with friendly name flag - not sure what this means\n");
799 r = read_hlink_string(pStm, &This->FriendlyName);
800 if (FAILED(r)) goto end;
803 if (hdr[1] & HLINK_SAVE_MONIKER_PRESENT)
805 TRACE("loading moniker\n");
806 r = OleLoadFromStream(pStm, &IID_IMoniker, (LPVOID*)&(This->Moniker));
807 if (FAILED(r))
808 goto end;
809 This->absolute = (hdr[1] & HLINK_SAVE_MONIKER_IS_ABSOLUTE) != 0;
812 if (hdr[1] & HLINK_SAVE_LOCATION_PRESENT)
814 TRACE("loading location\n");
815 r = read_hlink_string(pStm, &This->Location);
816 if (FAILED(r)) goto end;
819 end:
820 TRACE("Load Result 0x%x (%p)\n", r, This->Moniker);
822 return r;
825 static HRESULT WINAPI IPersistStream_fnSave(IPersistStream* iface,
826 IStream* pStm, BOOL fClearDirty)
828 HRESULT r;
829 HlinkImpl *This = impl_from_IPersistStream(iface);
830 DWORD hdr[2];
831 IMoniker *moniker;
833 TRACE("(%p) Moniker(%p)\n", This, This->Moniker);
835 r = __GetMoniker(This, &moniker, HLINKGETREF_DEFAULT);
836 if (FAILED(r))
837 return r;
838 r = E_FAIL;
840 hdr[0] = HLINK_SAVE_MAGIC;
841 hdr[1] = 0;
843 if (moniker)
844 hdr[1] |= HLINK_SAVE_MONIKER_PRESENT;
845 if (This->absolute)
846 hdr[1] |= HLINK_SAVE_MONIKER_IS_ABSOLUTE;
847 if (This->Location)
848 hdr[1] |= HLINK_SAVE_LOCATION_PRESENT;
849 if (This->FriendlyName)
850 hdr[1] |= HLINK_SAVE_FRIENDLY_PRESENT | 4 /* FIXME */;
851 if (This->TargetFrameName)
852 hdr[1] |= HLINK_SAVE_TARGET_FRAME_PRESENT;
854 IStream_Write(pStm, hdr, sizeof(hdr), NULL);
856 if (This->TargetFrameName)
858 r = write_hlink_string(pStm, This->TargetFrameName);
859 if (FAILED(r)) goto end;
862 if (This->FriendlyName)
864 r = write_hlink_string(pStm, This->FriendlyName);
865 if (FAILED(r)) goto end;
868 if (moniker)
870 IPersistStream* monstream;
872 monstream = NULL;
873 IMoniker_QueryInterface(moniker, &IID_IPersistStream,
874 (LPVOID*)&monstream);
875 if (monstream)
877 r = OleSaveToStream(monstream, pStm);
878 IPersistStream_Release(monstream);
880 if (FAILED(r)) goto end;
883 if (This->Location)
885 r = write_hlink_string(pStm, This->Location);
886 if (FAILED(r)) goto end;
889 end:
890 if (moniker) IMoniker_Release(moniker);
891 TRACE("Save Result 0x%x\n", r);
893 return r;
896 static HRESULT WINAPI IPersistStream_fnGetSizeMax(IPersistStream* iface,
897 ULARGE_INTEGER* pcbSize)
899 HRESULT r;
900 HlinkImpl *This = impl_from_IPersistStream(iface);
901 IMoniker *moniker;
903 TRACE("(%p) Moniker(%p)\n", This, This->Moniker);
905 pcbSize->QuadPart = sizeof(DWORD)*2;
907 if (This->TargetFrameName)
908 pcbSize->QuadPart += size_hlink_string(This->TargetFrameName);
910 if (This->FriendlyName)
911 pcbSize->QuadPart += size_hlink_string(This->FriendlyName);
913 r = __GetMoniker(This, &moniker, HLINKGETREF_DEFAULT);
914 if (FAILED(r))
915 return r;
916 r = E_FAIL;
918 if (moniker)
920 IPersistStream* monstream = NULL;
921 IMoniker_QueryInterface(moniker, &IID_IPersistStream,
922 (LPVOID*)&monstream);
923 if (monstream)
925 ULARGE_INTEGER mon_size;
926 r = IPersistStream_GetSizeMax(monstream, &mon_size);
927 pcbSize->QuadPart += mon_size.QuadPart;
928 IPersistStream_Release(monstream);
930 IMoniker_Release(moniker);
933 if (This->Location)
934 pcbSize->QuadPart += size_hlink_string(This->Location);
936 return r;
939 static const IPersistStreamVtbl psvt =
941 IPersistStream_fnQueryInterface,
942 IPersistStream_fnAddRef,
943 IPersistStream_fnRelease,
944 IPersistStream_fnGetClassID,
945 IPersistStream_fnIsDirty,
946 IPersistStream_fnLoad,
947 IPersistStream_fnSave,
948 IPersistStream_fnGetSizeMax,
951 HRESULT HLink_Constructor(IUnknown *pUnkOuter, REFIID riid, void **ppv)
953 HlinkImpl * hl;
955 TRACE("unkOut=%p riid=%s\n", pUnkOuter, debugstr_guid(riid));
956 *ppv = NULL;
958 if (pUnkOuter)
959 return CLASS_E_NOAGGREGATION;
961 hl = heap_alloc_zero(sizeof(HlinkImpl));
962 if (!hl)
963 return E_OUTOFMEMORY;
965 hl->ref = 1;
966 hl->IHlink_iface.lpVtbl = &hlvt;
967 hl->IPersistStream_iface.lpVtbl = &psvt;
968 hl->IDataObject_iface.lpVtbl = &dovt;
970 *ppv = hl;
971 return S_OK;