winedbg: Don't dereference possibly NULL thread pointer.
[wine/zf.git] / dlls / hlink / hlink_main.c
bloba9f907fa071d22f64504f32649294c2b90066393
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 "winreg.h"
24 #include "rpcproxy.h"
25 #include "hlguids.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(hlink);
31 static HINSTANCE instance;
33 typedef HRESULT (*LPFNCREATEINSTANCE)(IUnknown*, REFIID, LPVOID*);
35 typedef struct
37 IClassFactory IClassFactory_iface;
38 LPFNCREATEINSTANCE lpfnCI;
39 } CFImpl;
41 static inline CFImpl *impl_from_IClassFactory(IClassFactory *iface)
43 return CONTAINING_RECORD(iface, CFImpl, IClassFactory_iface);
46 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
48 TRACE("%p %d %p\n", hinstDLL, fdwReason, lpvReserved);
50 switch (fdwReason)
52 case DLL_PROCESS_ATTACH:
53 instance = hinstDLL;
54 DisableThreadLibraryCalls(hinstDLL);
55 break;
57 return TRUE;
60 /***********************************************************************
61 * DllCanUnloadNow (HLINK.@)
63 HRESULT WINAPI DllCanUnloadNow( void )
65 return S_FALSE;
68 /***********************************************************************
69 * HlinkCreateFromMoniker (HLINK.@)
71 HRESULT WINAPI HlinkCreateFromMoniker( IMoniker *pimkTrgt, LPCWSTR pwzLocation,
72 LPCWSTR pwzFriendlyName, IHlinkSite* pihlsite, DWORD dwSiteData,
73 IUnknown* piunkOuter, REFIID riid, void** ppvObj)
75 IHlink *hl = NULL;
76 HRESULT r;
78 TRACE("%p %s %s %p %i %p %s %p\n", pimkTrgt, debugstr_w(pwzLocation),
79 debugstr_w(pwzFriendlyName), pihlsite, dwSiteData, piunkOuter,
80 debugstr_guid(riid), ppvObj);
82 r = CoCreateInstance(&CLSID_StdHlink, piunkOuter, CLSCTX_INPROC_SERVER, riid, (LPVOID*)&hl);
83 if (FAILED(r))
84 return r;
86 IHlink_SetMonikerReference(hl, HLINKSETF_LOCATION | HLINKSETF_TARGET, pimkTrgt, pwzLocation);
88 if (pwzFriendlyName)
89 IHlink_SetFriendlyName(hl, pwzFriendlyName);
90 if (pihlsite)
91 IHlink_SetHlinkSite(hl, pihlsite, dwSiteData);
93 *ppvObj = hl;
95 TRACE("Returning %i\n",r);
97 return r;
100 /***********************************************************************
101 * HlinkCreateFromString (HLINK.@)
103 HRESULT WINAPI HlinkCreateFromString( LPCWSTR pwzTarget, LPCWSTR pwzLocation,
104 LPCWSTR pwzFriendlyName, IHlinkSite* pihlsite, DWORD dwSiteData,
105 IUnknown* piunkOuter, REFIID riid, void** ppvObj)
107 IHlink *hl = NULL;
108 HRESULT r;
109 WCHAR *hash, *tgt;
110 const WCHAR *loc;
112 TRACE("%s %s %s %p %i %p %s %p\n", debugstr_w(pwzTarget),
113 debugstr_w(pwzLocation), debugstr_w(pwzFriendlyName), pihlsite,
114 dwSiteData, piunkOuter, debugstr_guid(riid), ppvObj);
116 r = CoCreateInstance(&CLSID_StdHlink, piunkOuter, CLSCTX_INPROC_SERVER, riid, (LPVOID*)&hl);
117 if (FAILED(r))
118 return r;
120 if (pwzTarget)
122 hash = wcschr(pwzTarget, '#');
123 if (hash)
125 if (hash == pwzTarget)
126 tgt = NULL;
127 else
129 int tgt_len = hash - pwzTarget;
130 tgt = heap_alloc((tgt_len + 1) * sizeof(WCHAR));
131 if (!tgt)
132 return E_OUTOFMEMORY;
133 memcpy(tgt, pwzTarget, tgt_len * sizeof(WCHAR));
134 tgt[tgt_len] = 0;
136 if (!pwzLocation)
137 loc = hash + 1;
138 else
139 loc = pwzLocation;
141 else
143 tgt = hlink_strdupW(pwzTarget);
144 if (!tgt)
145 return E_OUTOFMEMORY;
146 loc = pwzLocation;
149 else
151 tgt = NULL;
152 loc = pwzLocation;
155 IHlink_SetStringReference(hl, HLINKSETF_TARGET | HLINKSETF_LOCATION, tgt, loc);
157 heap_free(tgt);
159 if (pwzFriendlyName)
160 IHlink_SetFriendlyName(hl, pwzFriendlyName);
162 if (pihlsite)
163 IHlink_SetHlinkSite(hl, pihlsite, dwSiteData);
165 TRACE("Returning %i\n",r);
166 *ppvObj = hl;
168 return r;
172 /***********************************************************************
173 * HlinkCreateBrowseContext (HLINK.@)
175 HRESULT WINAPI HlinkCreateBrowseContext( IUnknown* piunkOuter, REFIID riid, void** ppvObj)
177 TRACE("%p %s %p\n", piunkOuter, debugstr_guid(riid), ppvObj);
178 return CoCreateInstance(&CLSID_StdHlinkBrowseContext, piunkOuter, CLSCTX_INPROC_SERVER, riid, ppvObj);
181 /***********************************************************************
182 * HlinkNavigate (HLINK.@)
184 HRESULT WINAPI HlinkNavigate(IHlink *phl, IHlinkFrame *phlFrame,
185 DWORD grfHLNF, LPBC pbc, IBindStatusCallback *pbsc,
186 IHlinkBrowseContext *phlbc)
188 HRESULT r = S_OK;
190 TRACE("%p %p %i %p %p %p\n", phl, phlFrame, grfHLNF, pbc, pbsc, phlbc);
192 if (phlFrame)
193 r = IHlinkFrame_Navigate(phlFrame, grfHLNF, pbc, pbsc, phl);
194 else if (phl)
195 r = IHlink_Navigate(phl, grfHLNF, pbc, pbsc, phlbc);
197 return r;
200 /***********************************************************************
201 * HlinkOnNavigate (HLINK.@)
203 HRESULT WINAPI HlinkOnNavigate( IHlinkFrame *phlFrame,
204 IHlinkBrowseContext* phlbc, DWORD grfHLNF, IMoniker *pmkTarget,
205 LPCWSTR pwzLocation, LPCWSTR pwzFriendlyName, ULONG* puHLID)
207 HRESULT r;
209 TRACE("%p %p %i %p %s %s %p\n",phlFrame, phlbc, grfHLNF, pmkTarget,
210 debugstr_w(pwzLocation), debugstr_w(pwzFriendlyName), puHLID);
212 r = IHlinkBrowseContext_OnNavigateHlink(phlbc, grfHLNF, pmkTarget,
213 pwzLocation, pwzFriendlyName, puHLID);
215 if (phlFrame)
216 r = IHlinkFrame_OnNavigate(phlFrame,grfHLNF,pmkTarget, pwzLocation,
217 pwzFriendlyName, 0);
219 return r;
222 /***********************************************************************
223 * HlinkCreateFromData (HLINK.@)
225 HRESULT WINAPI HlinkCreateFromData(IDataObject *piDataObj,
226 IHlinkSite *pihlsite, DWORD dwSiteData, IUnknown *piunkOuter,
227 REFIID riid, void **ppvObj)
229 FIXME("%p, %p, %d, %p, %s, %p\n", piDataObj, pihlsite, dwSiteData,
230 piunkOuter, debugstr_guid(riid), ppvObj);
231 *ppvObj = NULL;
232 return E_NOTIMPL;
235 /***********************************************************************
236 * HlinkQueryCreateFromData (HLINK.@)
238 HRESULT WINAPI HlinkQueryCreateFromData(IDataObject* piDataObj)
240 FIXME("%p\n", piDataObj);
241 return E_NOTIMPL;
244 /***********************************************************************
245 * HlinkNavigateToStringReference (HLINK.@)
247 HRESULT WINAPI HlinkNavigateToStringReference( LPCWSTR pwzTarget,
248 LPCWSTR pwzLocation, IHlinkSite *pihlsite, DWORD dwSiteData,
249 IHlinkFrame *pihlframe, DWORD grfHLNF, LPBC pibc,
250 IBindStatusCallback *pibsc, IHlinkBrowseContext *pihlbc)
252 HRESULT r;
253 IHlink *hlink = NULL;
255 TRACE("%s %s %p %08x %p %08x %p %p %p\n",
256 debugstr_w(pwzTarget), debugstr_w(pwzLocation), pihlsite,
257 dwSiteData, pihlframe, grfHLNF, pibc, pibsc, pihlbc);
259 r = HlinkCreateFromString( pwzTarget, pwzLocation, NULL, pihlsite,
260 dwSiteData, NULL, &IID_IHlink, (LPVOID*) &hlink );
261 if (SUCCEEDED(r)) {
262 r = HlinkNavigate(hlink, pihlframe, grfHLNF, pibc, pibsc, pihlbc);
263 IHlink_Release(hlink);
266 return r;
269 /***********************************************************************
270 * HlinkIsShortcut (HLINK.@)
272 HRESULT WINAPI HlinkIsShortcut(LPCWSTR pwzFileName)
274 int len;
276 TRACE("(%s)\n", debugstr_w(pwzFileName));
278 if(!pwzFileName)
279 return E_INVALIDARG;
281 len = lstrlenW(pwzFileName)-4;
282 if(len < 0)
283 return S_FALSE;
285 return wcsicmp(pwzFileName+len, L".url") ? S_FALSE : S_OK;
288 /***********************************************************************
289 * HlinkGetSpecialReference (HLINK.@)
291 HRESULT WINAPI HlinkGetSpecialReference(ULONG uReference, LPWSTR *ppwzReference)
293 DWORD res, type, size = 100;
294 LPCWSTR value_name;
295 WCHAR *buf;
296 HKEY hkey;
298 TRACE("(%u %p)\n", uReference, ppwzReference);
300 *ppwzReference = NULL;
302 switch(uReference) {
303 case HLSR_HOME:
304 value_name = L"Start Page";
305 break;
306 case HLSR_SEARCHPAGE:
307 value_name = L"Search Page";
308 break;
309 case HLSR_HISTORYFOLDER:
310 return E_NOTIMPL;
311 default:
312 return E_INVALIDARG;
315 res = RegOpenKeyW(HKEY_CURRENT_USER, L"Software\\Microsoft\\Internet Explorer\\Main", &hkey);
316 if(res != ERROR_SUCCESS) {
317 WARN("Could not open key: %u\n", res);
318 return HRESULT_FROM_WIN32(res);
321 buf = CoTaskMemAlloc(size);
322 res = RegQueryValueExW(hkey, value_name, NULL, &type, (PBYTE)buf, &size);
323 buf = CoTaskMemRealloc(buf, size);
324 if(res == ERROR_MORE_DATA)
325 res = RegQueryValueExW(hkey, value_name, NULL, &type, (PBYTE)buf, &size);
326 RegCloseKey(hkey);
327 if(res != ERROR_SUCCESS) {
328 WARN("Could not query value %s: %u\n", debugstr_w(value_name), res);
329 CoTaskMemFree(buf);
330 return HRESULT_FROM_WIN32(res);
333 *ppwzReference = buf;
334 return S_OK;
337 /***********************************************************************
338 * HlinkTranslateURL (HLINK.@)
340 HRESULT WINAPI HlinkTranslateURL(LPCWSTR pwzURL, DWORD grfFlags, LPWSTR *ppwzTranslatedURL)
342 FIXME("(%s %08x %p)\n", debugstr_w(pwzURL), grfFlags, ppwzTranslatedURL);
343 return E_NOTIMPL;
346 /***********************************************************************
347 * HlinkUpdateStackItem (HLINK.@)
349 HRESULT WINAPI HlinkUpdateStackItem(IHlinkFrame *frame, IHlinkBrowseContext *bc,
350 ULONG hlid, IMoniker *target, LPCWSTR location, LPCWSTR friendly_name)
352 HRESULT hr;
354 TRACE("(%p %p 0x%x %p %s %s)\n", frame, bc, hlid, target, debugstr_w(location), debugstr_w(friendly_name));
356 if (!frame && !bc) return E_INVALIDARG;
358 if (frame)
359 hr = IHlinkFrame_UpdateHlink(frame, hlid, target, location, friendly_name);
360 else
361 hr = IHlinkBrowseContext_UpdateHlink(bc, hlid, target, location, friendly_name);
363 return hr;
366 /***********************************************************************
367 * HlinkParseDisplayName (HLINK.@)
369 HRESULT WINAPI HlinkParseDisplayName(LPBC pibc, LPCWSTR pwzDisplayName, BOOL fNoForceAbs,
370 ULONG *pcchEaten, IMoniker **ppimk)
372 ULONG eaten = 0, len;
373 HRESULT hres;
375 TRACE("(%p %s %x %p %p)\n", pibc, debugstr_w(pwzDisplayName), fNoForceAbs, pcchEaten, ppimk);
377 if(fNoForceAbs)
378 FIXME("Unsupported fNoForceAbs\n");
380 len = ARRAY_SIZE(L"file:") - 1;
381 if(!wcsnicmp(pwzDisplayName, L"file:", len)) {
382 pwzDisplayName += len;
383 eaten += len;
385 while(*pwzDisplayName == '/') {
386 pwzDisplayName++;
387 eaten++;
389 }else {
390 hres = MkParseDisplayNameEx(pibc, pwzDisplayName, pcchEaten, ppimk);
391 if(SUCCEEDED(hres))
392 return hres;
394 hres = MkParseDisplayName(pibc, pwzDisplayName, pcchEaten, ppimk);
395 if(SUCCEEDED(hres))
396 return hres;
399 hres = CreateFileMoniker(pwzDisplayName, ppimk);
400 if(SUCCEEDED(hres))
401 *pcchEaten = eaten + lstrlenW(pwzDisplayName);
403 return hres;
406 /***********************************************************************
407 * HlinkResolveMonikerForData (HLINK.@)
409 HRESULT WINAPI HlinkResolveMonikerForData(LPMONIKER pimkReference, DWORD reserved, LPBC pibc,
410 ULONG cFmtetc, FORMATETC *rgFmtetc, IBindStatusCallback *pibsc, LPMONIKER pimkBase)
412 LPOLESTR name = NULL;
413 IBindCtx *bctx;
414 DWORD mksys = 0;
415 void *obj = NULL;
416 HRESULT hres;
418 TRACE("(%p %x %p %d %p %p %p)\n", pimkReference, reserved, pibc, cFmtetc, rgFmtetc, pibsc, pimkBase);
420 if(cFmtetc || rgFmtetc || pimkBase)
421 FIXME("Unsupported args\n");
423 hres = RegisterBindStatusCallback(pibc, pibsc, NULL /* FIXME */, 0);
424 if(FAILED(hres))
425 return hres;
427 hres = IMoniker_IsSystemMoniker(pimkReference, &mksys);
428 if(SUCCEEDED(hres) && mksys != MKSYS_URLMONIKER)
429 WARN("sysmk = %x\n", mksys);
431 /* FIXME: What is it for? */
432 CreateBindCtx(0, &bctx);
433 hres = IMoniker_GetDisplayName(pimkReference, bctx, NULL, &name);
434 IBindCtx_Release(bctx);
435 if(SUCCEEDED(hres)) {
436 TRACE("got display name %s\n", debugstr_w(name));
437 CoTaskMemFree(name);
440 return IMoniker_BindToStorage(pimkReference, pibc, NULL, &IID_IUnknown, &obj);
443 /***********************************************************************
444 * HlinkClone (HLINK.@)
446 HRESULT WINAPI HlinkClone(IHlink *hlink, REFIID riid, IHlinkSite *hls,
447 DWORD site_data, void **obj)
449 IMoniker *mk, *clone_mk = NULL;
450 WCHAR *loc, *name = NULL;
451 HRESULT hres;
453 if(!hlink || !riid || !obj)
454 return E_INVALIDARG;
456 *obj = NULL;
458 hres = IHlink_GetMonikerReference(hlink, HLINKGETREF_DEFAULT, &mk, &loc);
459 if(FAILED(hres))
460 return hres;
462 if(mk) {
463 IStream *strm;
464 LARGE_INTEGER lgint;
466 hres = CreateStreamOnHGlobal(NULL, TRUE, &strm);
467 if(FAILED(hres)) {
468 IMoniker_Release(mk);
469 goto cleanup;
472 hres = OleSaveToStream((IPersistStream*)mk, strm);
473 if(FAILED(hres)) {
474 IStream_Release(strm);
475 IMoniker_Release(mk);
476 goto cleanup;
478 IMoniker_Release(mk);
480 lgint.QuadPart = 0;
481 hres = IStream_Seek(strm, lgint, STREAM_SEEK_SET, NULL);
482 if(FAILED(hres)) {
483 IStream_Release(strm);
484 goto cleanup;
487 hres = OleLoadFromStream(strm, &IID_IMoniker, (void**)&clone_mk);
488 IStream_Release(strm);
489 if(FAILED(hres))
490 goto cleanup;
493 hres = IHlink_GetFriendlyName(hlink, HLFNAMEF_DEFAULT, &name);
494 if(FAILED(hres))
495 goto cleanup;
497 hres = HlinkCreateFromMoniker(clone_mk, loc, name, hls, site_data, NULL,
498 &IID_IHlink, obj);
500 cleanup:
501 if(clone_mk)
502 IMoniker_Release(clone_mk);
503 CoTaskMemFree(loc);
504 CoTaskMemFree(name);
505 return hres;
508 static HRESULT WINAPI HLinkCF_fnQueryInterface ( LPCLASSFACTORY iface,
509 REFIID riid, LPVOID *ppvObj)
511 CFImpl *This = impl_from_IClassFactory(iface);
513 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppvObj);
515 *ppvObj = NULL;
517 if (IsEqualIID(riid, &IID_IUnknown) ||
518 IsEqualIID(riid, &IID_IClassFactory))
520 *ppvObj = &This->IClassFactory_iface;
521 return S_OK;
524 TRACE("-- E_NOINTERFACE\n");
525 return E_NOINTERFACE;
528 static ULONG WINAPI HLinkCF_fnAddRef (LPCLASSFACTORY iface)
530 return 2;
533 static ULONG WINAPI HLinkCF_fnRelease(LPCLASSFACTORY iface)
535 return 1;
538 static HRESULT WINAPI HLinkCF_fnCreateInstance( LPCLASSFACTORY iface,
539 LPUNKNOWN pUnkOuter, REFIID riid, LPVOID *ppvObject)
541 CFImpl *This = impl_from_IClassFactory(iface);
543 TRACE("%p->(%p,%s,%p)\n", This, pUnkOuter, debugstr_guid(riid), ppvObject);
545 *ppvObject = NULL;
547 return This->lpfnCI(pUnkOuter, riid, ppvObject);
550 static HRESULT WINAPI HLinkCF_fnLockServer(LPCLASSFACTORY iface, BOOL fLock)
552 FIXME("%p %d\n", iface, fLock);
553 return E_NOTIMPL;
556 static const IClassFactoryVtbl hlcfvt =
558 HLinkCF_fnQueryInterface,
559 HLinkCF_fnAddRef,
560 HLinkCF_fnRelease,
561 HLinkCF_fnCreateInstance,
562 HLinkCF_fnLockServer
565 static CFImpl HLink_cf = { { &hlcfvt }, HLink_Constructor };
566 static CFImpl HLinkBrowseContext_cf = { { &hlcfvt }, HLinkBrowseContext_Constructor };
568 /***********************************************************************
569 * DllGetClassObject (HLINK.@)
571 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv)
573 IClassFactory *pcf = NULL;
575 TRACE("%s %s %p\n", debugstr_guid(rclsid), debugstr_guid(iid), ppv);
577 if (!ppv)
578 return E_INVALIDARG;
579 *ppv = NULL;
581 if (IsEqualIID(rclsid, &CLSID_StdHlink))
582 pcf = &HLink_cf.IClassFactory_iface;
583 else if (IsEqualIID(rclsid, &CLSID_StdHlinkBrowseContext))
584 pcf = &HLinkBrowseContext_cf.IClassFactory_iface;
585 else
586 return CLASS_E_CLASSNOTAVAILABLE;
588 return IClassFactory_QueryInterface(pcf, iid, ppv);
591 /***********************************************************************
592 * DllRegisterServer (HLINK.@)
594 HRESULT WINAPI DllRegisterServer(void)
596 return __wine_register_resources( instance );
599 /***********************************************************************
600 * DllUnregisterServer (HLINK.@)
602 HRESULT WINAPI DllUnregisterServer(void)
604 return __wine_unregister_resources( instance );