xref: /reactos/dll/win32/hlink/hlink_main.c (revision 84ccccab)
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 <winreg.h>
24 #include <rpcproxy.h>
25 
26 static HINSTANCE instance;
27 
28 typedef HRESULT (*LPFNCREATEINSTANCE)(IUnknown*, REFIID, LPVOID*);
29 
30 typedef struct
31 {
32     IClassFactory      IClassFactory_iface;
33     LPFNCREATEINSTANCE lpfnCI;
34 } CFImpl;
35 
36 static inline CFImpl *impl_from_IClassFactory(IClassFactory *iface)
37 {
38     return CONTAINING_RECORD(iface, CFImpl, IClassFactory_iface);
39 }
40 
41 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
42 {
43     TRACE("%p %d %p\n", hinstDLL, fdwReason, lpvReserved);
44 
45     switch (fdwReason)
46     {
47     case DLL_PROCESS_ATTACH:
48         instance = hinstDLL;
49         DisableThreadLibraryCalls(hinstDLL);
50         break;
51     }
52     return TRUE;
53 }
54 
55 /***********************************************************************
56  *             DllCanUnloadNow (HLINK.@)
57  */
58 HRESULT WINAPI DllCanUnloadNow( void )
59 {
60     return S_FALSE;
61 }
62 
63 /***********************************************************************
64  *             HlinkCreateFromMoniker (HLINK.@)
65  */
66 HRESULT WINAPI HlinkCreateFromMoniker( IMoniker *pimkTrgt, LPCWSTR pwzLocation,
67         LPCWSTR pwzFriendlyName, IHlinkSite* pihlsite, DWORD dwSiteData,
68         IUnknown* piunkOuter, REFIID riid, void** ppvObj)
69 {
70     IHlink *hl = NULL;
71     HRESULT r;
72 
73     TRACE("%p %s %s %p %i %p %s %p\n", pimkTrgt, debugstr_w(pwzLocation),
74             debugstr_w(pwzFriendlyName), pihlsite, dwSiteData, piunkOuter,
75             debugstr_guid(riid), ppvObj);
76 
77     r = CoCreateInstance(&CLSID_StdHlink, piunkOuter, CLSCTX_INPROC_SERVER, riid, (LPVOID*)&hl);
78     if (FAILED(r))
79         return r;
80 
81     IHlink_SetMonikerReference(hl, HLINKSETF_LOCATION | HLINKSETF_TARGET, pimkTrgt, pwzLocation);
82 
83     if (pwzFriendlyName)
84         IHlink_SetFriendlyName(hl, pwzFriendlyName);
85     if (pihlsite)
86         IHlink_SetHlinkSite(hl, pihlsite, dwSiteData);
87 
88     *ppvObj = hl;
89 
90     TRACE("Returning %i\n",r);
91 
92     return r;
93 }
94 
95 /***********************************************************************
96  *             HlinkCreateFromString (HLINK.@)
97  */
98 HRESULT WINAPI HlinkCreateFromString( LPCWSTR pwzTarget, LPCWSTR pwzLocation,
99         LPCWSTR pwzFriendlyName, IHlinkSite* pihlsite, DWORD dwSiteData,
100         IUnknown* piunkOuter, REFIID riid, void** ppvObj)
101 {
102     IHlink *hl = NULL;
103     HRESULT r;
104     WCHAR *hash, *tgt;
105     const WCHAR *loc;
106 
107     TRACE("%s %s %s %p %i %p %s %p\n", debugstr_w(pwzTarget),
108             debugstr_w(pwzLocation), debugstr_w(pwzFriendlyName), pihlsite,
109             dwSiteData, piunkOuter, debugstr_guid(riid), ppvObj);
110 
111     r = CoCreateInstance(&CLSID_StdHlink, piunkOuter, CLSCTX_INPROC_SERVER, riid, (LPVOID*)&hl);
112     if (FAILED(r))
113         return r;
114 
115     if (pwzTarget)
116     {
117         hash = strchrW(pwzTarget, '#');
118         if (hash)
119         {
120             if (hash == pwzTarget)
121                 tgt = NULL;
122             else
123             {
124                 int tgt_len = hash - pwzTarget;
125                 tgt = heap_alloc((tgt_len + 1) * sizeof(WCHAR));
126                 if (!tgt)
127                     return E_OUTOFMEMORY;
128                 memcpy(tgt, pwzTarget, tgt_len * sizeof(WCHAR));
129                 tgt[tgt_len] = 0;
130             }
131             if (!pwzLocation)
132                 loc = hash + 1;
133             else
134                 loc = pwzLocation;
135         }
136         else
137         {
138             tgt = hlink_strdupW(pwzTarget);
139             if (!tgt)
140                 return E_OUTOFMEMORY;
141             loc = pwzLocation;
142         }
143     }
144     else
145     {
146         tgt = NULL;
147         loc = pwzLocation;
148     }
149 
150     IHlink_SetStringReference(hl, HLINKSETF_TARGET | HLINKSETF_LOCATION, tgt, loc);
151 
152     heap_free(tgt);
153 
154     if (pwzFriendlyName)
155         IHlink_SetFriendlyName(hl, pwzFriendlyName);
156 
157     if (pihlsite)
158         IHlink_SetHlinkSite(hl, pihlsite, dwSiteData);
159 
160     TRACE("Returning %i\n",r);
161     *ppvObj = hl;
162 
163     return r;
164 }
165 
166 
167 /***********************************************************************
168  *             HlinkCreateBrowseContext (HLINK.@)
169  */
170 HRESULT WINAPI HlinkCreateBrowseContext( IUnknown* piunkOuter, REFIID riid, void** ppvObj)
171 {
172     TRACE("%p %s %p\n", piunkOuter, debugstr_guid(riid), ppvObj);
173     return CoCreateInstance(&CLSID_StdHlinkBrowseContext, piunkOuter, CLSCTX_INPROC_SERVER, riid, ppvObj);
174 }
175 
176 /***********************************************************************
177  *             HlinkNavigate (HLINK.@)
178  */
179 HRESULT WINAPI HlinkNavigate(IHlink *phl, IHlinkFrame *phlFrame,
180         DWORD grfHLNF, LPBC pbc, IBindStatusCallback *pbsc,
181         IHlinkBrowseContext *phlbc)
182 {
183     HRESULT r = S_OK;
184 
185     TRACE("%p %p %i %p %p %p\n", phl, phlFrame, grfHLNF, pbc, pbsc, phlbc);
186 
187     if (phlFrame)
188         r = IHlinkFrame_Navigate(phlFrame, grfHLNF, pbc, pbsc, phl);
189     else if (phl)
190         r = IHlink_Navigate(phl, grfHLNF, pbc, pbsc, phlbc);
191 
192     return r;
193 }
194 
195 /***********************************************************************
196  *             HlinkOnNavigate (HLINK.@)
197  */
198 HRESULT WINAPI HlinkOnNavigate( IHlinkFrame *phlFrame,
199         IHlinkBrowseContext* phlbc, DWORD grfHLNF, IMoniker *pmkTarget,
200         LPCWSTR pwzLocation, LPCWSTR pwzFriendlyName, ULONG* puHLID)
201 {
202     HRESULT r;
203 
204     TRACE("%p %p %i %p %s %s %p\n",phlFrame, phlbc, grfHLNF, pmkTarget,
205             debugstr_w(pwzLocation), debugstr_w(pwzFriendlyName), puHLID);
206 
207     r = IHlinkBrowseContext_OnNavigateHlink(phlbc, grfHLNF, pmkTarget,
208             pwzLocation, pwzFriendlyName, puHLID);
209 
210     if (phlFrame)
211         r = IHlinkFrame_OnNavigate(phlFrame,grfHLNF,pmkTarget, pwzLocation,
212                 pwzFriendlyName, 0);
213 
214     return r;
215 }
216 
217 /***********************************************************************
218  *             HlinkCreateFromData (HLINK.@)
219  */
220 HRESULT WINAPI HlinkCreateFromData(IDataObject *piDataObj,
221         IHlinkSite *pihlsite, DWORD dwSiteData, IUnknown *piunkOuter,
222         REFIID riid, void **ppvObj)
223 {
224     FIXME("%p, %p, %d, %p, %s, %p\n", piDataObj, pihlsite, dwSiteData,
225            piunkOuter, debugstr_guid(riid), ppvObj);
226     *ppvObj = NULL;
227     return E_NOTIMPL;
228 }
229 
230 /***********************************************************************
231  *             HlinkQueryCreateFromData (HLINK.@)
232  */
233 HRESULT WINAPI HlinkQueryCreateFromData(IDataObject* piDataObj)
234 {
235     FIXME("%p\n", piDataObj);
236     return E_NOTIMPL;
237 }
238 
239 /***********************************************************************
240  *             HlinkNavigateToStringReference (HLINK.@)
241  */
242 HRESULT WINAPI HlinkNavigateToStringReference( LPCWSTR pwzTarget,
243         LPCWSTR pwzLocation, IHlinkSite *pihlsite, DWORD dwSiteData,
244         IHlinkFrame *pihlframe, DWORD grfHLNF, LPBC pibc,
245         IBindStatusCallback *pibsc, IHlinkBrowseContext *pihlbc)
246 {
247     HRESULT r;
248     IHlink *hlink = NULL;
249 
250     TRACE("%s %s %p %08x %p %08x %p %p %p\n",
251           debugstr_w(pwzTarget), debugstr_w(pwzLocation), pihlsite,
252           dwSiteData, pihlframe, grfHLNF, pibc, pibsc, pihlbc);
253 
254     r = HlinkCreateFromString( pwzTarget, pwzLocation, NULL, pihlsite,
255                                dwSiteData, NULL, &IID_IHlink, (LPVOID*) &hlink );
256     if (SUCCEEDED(r)) {
257         r = HlinkNavigate(hlink, pihlframe, grfHLNF, pibc, pibsc, pihlbc);
258         IHlink_Release(hlink);
259     }
260 
261     return r;
262 }
263 
264 /***********************************************************************
265  *             HlinkIsShortcut (HLINK.@)
266  */
267 HRESULT WINAPI HlinkIsShortcut(LPCWSTR pwzFileName)
268 {
269     int len;
270 
271     static const WCHAR url_ext[] = {'.','u','r','l',0};
272 
273     TRACE("(%s)\n", debugstr_w(pwzFileName));
274 
275     if(!pwzFileName)
276         return E_INVALIDARG;
277 
278     len = strlenW(pwzFileName)-4;
279     if(len < 0)
280         return S_FALSE;
281 
282     return strcmpiW(pwzFileName+len, url_ext) ? S_FALSE : S_OK;
283 }
284 
285 /***********************************************************************
286  *             HlinkGetSpecialReference (HLINK.@)
287  */
288 HRESULT WINAPI HlinkGetSpecialReference(ULONG uReference, LPWSTR *ppwzReference)
289 {
290     DWORD res, type, size = 100;
291     LPCWSTR value_name;
292     WCHAR *buf;
293     HKEY hkey;
294 
295     static const WCHAR start_pageW[] = {'S','t','a','r','t',' ','P','a','g','e',0};
296     static const WCHAR search_pageW[] = {'S','e','a','r','c','h',' ','P','a','g','e',0};
297 
298     static const WCHAR ie_main_keyW[] =
299         {'S','o','f','t','w','a','r','e',
300          '\\','M','i','c','r','o','s','o','f','t','\\',
301          'I','n','t','e','r','n','e','t',' ','E','x','p','l','o','r','e','r',
302          '\\','M','a','i','n',0};
303 
304     TRACE("(%u %p)\n", uReference, ppwzReference);
305 
306     *ppwzReference = NULL;
307 
308     switch(uReference) {
309     case HLSR_HOME:
310         value_name = start_pageW;
311         break;
312     case HLSR_SEARCHPAGE:
313         value_name = search_pageW;
314         break;
315     case HLSR_HISTORYFOLDER:
316         return E_NOTIMPL;
317     default:
318         return E_INVALIDARG;
319     }
320 
321     res = RegOpenKeyW(HKEY_CURRENT_USER, ie_main_keyW, &hkey);
322     if(res != ERROR_SUCCESS) {
323         WARN("Could not open key: %u\n", res);
324         return HRESULT_FROM_WIN32(res);
325     }
326 
327     buf = CoTaskMemAlloc(size);
328     res = RegQueryValueExW(hkey, value_name, NULL, &type, (PBYTE)buf, &size);
329     buf = CoTaskMemRealloc(buf, size);
330     if(res == ERROR_MORE_DATA)
331         res = RegQueryValueExW(hkey, value_name, NULL, &type, (PBYTE)buf, &size);
332     RegCloseKey(hkey);
333     if(res != ERROR_SUCCESS) {
334         WARN("Could not query value %s: %u\n", debugstr_w(value_name), res);
335         CoTaskMemFree(buf);
336         return HRESULT_FROM_WIN32(res);
337     }
338 
339     *ppwzReference = buf;
340     return S_OK;
341 }
342 
343 /***********************************************************************
344  *             HlinkTranslateURL (HLINK.@)
345  */
346 HRESULT WINAPI HlinkTranslateURL(LPCWSTR pwzURL, DWORD grfFlags, LPWSTR *ppwzTranslatedURL)
347 {
348     FIXME("(%s %08x %p)\n", debugstr_w(pwzURL), grfFlags, ppwzTranslatedURL);
349     return E_NOTIMPL;
350 }
351 
352 /***********************************************************************
353  *             HlinkUpdateStackItem (HLINK.@)
354  */
355 HRESULT WINAPI HlinkUpdateStackItem(IHlinkFrame *frame, IHlinkBrowseContext *bc,
356         ULONG hlid, IMoniker *target, LPCWSTR location, LPCWSTR friendly_name)
357 {
358     HRESULT hr;
359 
360     TRACE("(%p %p 0x%x %p %s %s)\n", frame, bc, hlid, target, debugstr_w(location), debugstr_w(friendly_name));
361 
362     if (!frame && !bc) return E_INVALIDARG;
363 
364     if (frame)
365         hr = IHlinkFrame_UpdateHlink(frame, hlid, target, location, friendly_name);
366     else
367         hr = IHlinkBrowseContext_UpdateHlink(bc, hlid, target, location, friendly_name);
368 
369     return hr;
370 }
371 
372 /***********************************************************************
373  *             HlinkParseDisplayName (HLINK.@)
374  */
375 HRESULT WINAPI HlinkParseDisplayName(LPBC pibc, LPCWSTR pwzDisplayName, BOOL fNoForceAbs,
376         ULONG *pcchEaten, IMoniker **ppimk)
377 {
378     static const WCHAR file_colonW[] = {'f','i','l','e',':'};
379     ULONG eaten = 0;
380     HRESULT hres;
381 
382     TRACE("(%p %s %x %p %p)\n", pibc, debugstr_w(pwzDisplayName), fNoForceAbs, pcchEaten, ppimk);
383 
384     if(fNoForceAbs)
385         FIXME("Unsupported fNoForceAbs\n");
386 
387     if(!strncmpiW(pwzDisplayName, file_colonW, sizeof(file_colonW)/sizeof(WCHAR))) {
388         pwzDisplayName += sizeof(file_colonW)/sizeof(WCHAR);
389         eaten += sizeof(file_colonW)/sizeof(WCHAR);
390 
391         while(*pwzDisplayName == '/') {
392             pwzDisplayName++;
393             eaten++;
394         }
395     }else {
396         hres = MkParseDisplayNameEx(pibc, pwzDisplayName, pcchEaten, ppimk);
397         if(SUCCEEDED(hres))
398             return hres;
399 
400         hres = MkParseDisplayName(pibc, pwzDisplayName, pcchEaten, ppimk);
401         if(SUCCEEDED(hres))
402             return hres;
403     }
404 
405     hres = CreateFileMoniker(pwzDisplayName, ppimk);
406     if(SUCCEEDED(hres))
407         *pcchEaten = eaten + strlenW(pwzDisplayName);
408 
409     return hres;
410 }
411 
412 /***********************************************************************
413  *             HlinkResolveMonikerForData (HLINK.@)
414  */
415 HRESULT WINAPI HlinkResolveMonikerForData(LPMONIKER pimkReference, DWORD reserved, LPBC pibc,
416         ULONG cFmtetc, FORMATETC *rgFmtetc, IBindStatusCallback *pibsc, LPMONIKER pimkBase)
417 {
418     LPOLESTR name = NULL;
419     IBindCtx *bctx;
420     DWORD mksys = 0;
421     void *obj = NULL;
422     HRESULT hres;
423 
424     TRACE("(%p %x %p %d %p %p %p)\n", pimkReference, reserved, pibc, cFmtetc, rgFmtetc, pibsc, pimkBase);
425 
426     if(cFmtetc || rgFmtetc || pimkBase)
427         FIXME("Unsupported args\n");
428 
429     hres = RegisterBindStatusCallback(pibc, pibsc, NULL /* FIXME */, 0);
430     if(FAILED(hres))
431         return hres;
432 
433     hres = IMoniker_IsSystemMoniker(pimkReference, &mksys);
434     if(SUCCEEDED(hres) && mksys != MKSYS_URLMONIKER)
435         WARN("sysmk = %x\n", mksys);
436 
437     /* FIXME: What is it for? */
438     CreateBindCtx(0, &bctx);
439     hres = IMoniker_GetDisplayName(pimkReference, bctx, NULL, &name);
440     IBindCtx_Release(bctx);
441     if(SUCCEEDED(hres)) {
442         TRACE("got display name %s\n", debugstr_w(name));
443         CoTaskMemFree(name);
444     }
445 
446     return IMoniker_BindToStorage(pimkReference, pibc, NULL, &IID_IUnknown, &obj);
447 }
448 
449 /***********************************************************************
450  *             HlinkClone (HLINK.@)
451  */
452 HRESULT WINAPI HlinkClone(IHlink *hlink, REFIID riid, IHlinkSite *hls,
453         DWORD site_data, void **obj)
454 {
455     IMoniker *mk, *clone_mk = NULL;
456     WCHAR *loc, *name = NULL;
457     HRESULT hres;
458 
459     if(!hlink || !riid || !obj)
460         return E_INVALIDARG;
461 
462     *obj = NULL;
463 
464     hres = IHlink_GetMonikerReference(hlink, HLINKGETREF_DEFAULT, &mk, &loc);
465     if(FAILED(hres))
466         return hres;
467 
468     if(mk) {
469         IStream *strm;
470         LARGE_INTEGER lgint;
471 
472         hres = CreateStreamOnHGlobal(NULL, TRUE, &strm);
473         if(FAILED(hres)) {
474             IMoniker_Release(mk);
475             goto cleanup;
476         }
477 
478         hres = OleSaveToStream((IPersistStream*)mk, strm);
479         if(FAILED(hres)) {
480             IStream_Release(strm);
481             IMoniker_Release(mk);
482             goto cleanup;
483         }
484         IMoniker_Release(mk);
485 
486         lgint.QuadPart = 0;
487         hres = IStream_Seek(strm, lgint, STREAM_SEEK_SET, NULL);
488         if(FAILED(hres)) {
489             IStream_Release(strm);
490             goto cleanup;
491         }
492 
493         hres = OleLoadFromStream(strm, &IID_IMoniker, (void**)&clone_mk);
494         IStream_Release(strm);
495         if(FAILED(hres))
496             goto cleanup;
497     }
498 
499     hres = IHlink_GetFriendlyName(hlink, HLFNAMEF_DEFAULT, &name);
500     if(FAILED(hres))
501         goto cleanup;
502 
503     hres = HlinkCreateFromMoniker(clone_mk, loc, name, hls, site_data, NULL,
504             &IID_IHlink, obj);
505 
506 cleanup:
507     if(clone_mk)
508         IMoniker_Release(clone_mk);
509     CoTaskMemFree(loc);
510     CoTaskMemFree(name);
511     return hres;
512 }
513 
514 static HRESULT WINAPI HLinkCF_fnQueryInterface ( LPCLASSFACTORY iface,
515         REFIID riid, LPVOID *ppvObj)
516 {
517     CFImpl *This = impl_from_IClassFactory(iface);
518 
519     TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppvObj);
520 
521     *ppvObj = NULL;
522 
523     if (IsEqualIID(riid, &IID_IUnknown) ||
524         IsEqualIID(riid, &IID_IClassFactory))
525     {
526         *ppvObj = &This->IClassFactory_iface;
527         return S_OK;
528     }
529 
530     TRACE("-- E_NOINTERFACE\n");
531     return E_NOINTERFACE;
532 }
533 
534 static ULONG WINAPI HLinkCF_fnAddRef (LPCLASSFACTORY iface)
535 {
536     return 2;
537 }
538 
539 static ULONG WINAPI HLinkCF_fnRelease(LPCLASSFACTORY iface)
540 {
541     return 1;
542 }
543 
544 static HRESULT WINAPI HLinkCF_fnCreateInstance( LPCLASSFACTORY iface,
545         LPUNKNOWN pUnkOuter, REFIID riid, LPVOID *ppvObject)
546 {
547     CFImpl *This = impl_from_IClassFactory(iface);
548 
549     TRACE("%p->(%p,%s,%p)\n", This, pUnkOuter, debugstr_guid(riid), ppvObject);
550 
551     *ppvObject = NULL;
552 
553     return This->lpfnCI(pUnkOuter, riid, ppvObject);
554 }
555 
556 static HRESULT WINAPI HLinkCF_fnLockServer(LPCLASSFACTORY iface, BOOL fLock)
557 {
558     FIXME("%p %d\n", iface, fLock);
559     return E_NOTIMPL;
560 }
561 
562 static const IClassFactoryVtbl hlcfvt =
563 {
564     HLinkCF_fnQueryInterface,
565     HLinkCF_fnAddRef,
566     HLinkCF_fnRelease,
567     HLinkCF_fnCreateInstance,
568     HLinkCF_fnLockServer
569 };
570 
571 static CFImpl HLink_cf = { { &hlcfvt }, HLink_Constructor };
572 static CFImpl HLinkBrowseContext_cf = { { &hlcfvt }, HLinkBrowseContext_Constructor };
573 
574 /***********************************************************************
575  *             DllGetClassObject (HLINK.@)
576  */
577 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv)
578 {
579     IClassFactory   *pcf = NULL;
580 
581     TRACE("%s %s %p\n", debugstr_guid(rclsid), debugstr_guid(iid), ppv);
582 
583     if (!ppv)
584         return E_INVALIDARG;
585     *ppv = NULL;
586 
587     if (IsEqualIID(rclsid, &CLSID_StdHlink))
588         pcf = &HLink_cf.IClassFactory_iface;
589     else if (IsEqualIID(rclsid, &CLSID_StdHlinkBrowseContext))
590         pcf = &HLinkBrowseContext_cf.IClassFactory_iface;
591     else
592         return CLASS_E_CLASSNOTAVAILABLE;
593 
594     return IClassFactory_QueryInterface(pcf, iid, ppv);
595 }
596 
597 /***********************************************************************
598  *		DllRegisterServer (HLINK.@)
599  */
600 HRESULT WINAPI DllRegisterServer(void)
601 {
602     return __wine_register_resources( instance );
603 }
604 
605 /***********************************************************************
606  *		DllUnregisterServer (HLINK.@)
607  */
608 HRESULT WINAPI DllUnregisterServer(void)
609 {
610     return __wine_unregister_resources( instance );
611 }
612