sync with trunk (r46275)
[reactos.git] / dll / win32 / hlink / hlink_main.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 "winreg.h"
24 #include "hlguids.h"
25
26 #include "wine/debug.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(hlink);
29
30 typedef HRESULT (CALLBACK *LPFNCREATEINSTANCE)(IUnknown*, REFIID, LPVOID*);
31
32 typedef struct
33 {
34 const IClassFactoryVtbl *lpVtbl;
35 LPFNCREATEINSTANCE lpfnCI;
36 } CFImpl;
37
38 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
39 {
40 TRACE("%p %d %p\n", hinstDLL, fdwReason, lpvReserved);
41
42 switch (fdwReason)
43 {
44 case DLL_PROCESS_ATTACH:
45 DisableThreadLibraryCalls(hinstDLL);
46 break;
47 case DLL_PROCESS_DETACH:
48 break;
49 }
50 return TRUE;
51 }
52
53 /***********************************************************************
54 * DllCanUnloadNow (HLINK.@)
55 */
56 HRESULT WINAPI DllCanUnloadNow( void )
57 {
58 return S_OK;
59 }
60
61 /***********************************************************************
62 * HlinkCreateFromMoniker (HLINK.@)
63 */
64 HRESULT WINAPI HlinkCreateFromMoniker( IMoniker *pimkTrgt, LPCWSTR pwzLocation,
65 LPCWSTR pwzFriendlyName, IHlinkSite* pihlsite, DWORD dwSiteData,
66 IUnknown* piunkOuter, REFIID riid, void** ppvObj)
67 {
68 IHlink *hl = NULL;
69 HRESULT r = S_OK;
70
71 TRACE("%p %s %s %p %i %p %s %p\n", pimkTrgt, debugstr_w(pwzLocation),
72 debugstr_w(pwzFriendlyName), pihlsite, dwSiteData, piunkOuter,
73 debugstr_guid(riid), ppvObj);
74
75 r = CoCreateInstance(&CLSID_StdHlink, piunkOuter, CLSCTX_INPROC_SERVER, riid, (LPVOID*)&hl);
76 if (FAILED(r))
77 return r;
78
79 IHlink_SetMonikerReference(hl, HLINKSETF_LOCATION | HLINKSETF_TARGET, pimkTrgt, pwzLocation);
80
81 if (pwzFriendlyName)
82 IHlink_SetFriendlyName(hl, pwzFriendlyName);
83 if (pihlsite)
84 IHlink_SetHlinkSite(hl, pihlsite, dwSiteData);
85
86 *ppvObj = hl;
87
88 TRACE("Returning %i\n",r);
89
90 return r;
91 }
92
93 /***********************************************************************
94 * HlinkCreateFromString (HLINK.@)
95 */
96 HRESULT WINAPI HlinkCreateFromString( LPCWSTR pwzTarget, LPCWSTR pwzLocation,
97 LPCWSTR pwzFriendlyName, IHlinkSite* pihlsite, DWORD dwSiteData,
98 IUnknown* piunkOuter, REFIID riid, void** ppvObj)
99 {
100 IHlink *hl = NULL;
101 HRESULT r = S_OK;
102
103 TRACE("%s %s %s %p %i %p %s %p\n", debugstr_w(pwzTarget),
104 debugstr_w(pwzLocation), debugstr_w(pwzFriendlyName), pihlsite,
105 dwSiteData, piunkOuter, debugstr_guid(riid), ppvObj);
106
107 r = CoCreateInstance(&CLSID_StdHlink, piunkOuter, CLSCTX_INPROC_SERVER, riid, (LPVOID*)&hl);
108 if (FAILED(r))
109 return r;
110
111 IHlink_SetStringReference(hl, HLINKSETF_TARGET | HLINKSETF_LOCATION,
112 pwzTarget, pwzLocation);
113
114 if (pwzFriendlyName)
115 IHlink_SetFriendlyName(hl, pwzFriendlyName);
116
117 if (pihlsite)
118 IHlink_SetHlinkSite(hl, pihlsite, dwSiteData);
119
120 TRACE("Returning %i\n",r);
121 *ppvObj = hl;
122
123 return r;
124 }
125
126
127 /***********************************************************************
128 * HlinkCreateBrowseContext (HLINK.@)
129 */
130 HRESULT WINAPI HlinkCreateBrowseContext( IUnknown* piunkOuter, REFIID riid, void** ppvObj)
131 {
132 HRESULT r = S_OK;
133
134 TRACE("%p %s %p\n", piunkOuter, debugstr_guid(riid), ppvObj);
135
136 r = CoCreateInstance(&CLSID_StdHlinkBrowseContext, piunkOuter, CLSCTX_INPROC_SERVER, riid, ppvObj);
137
138 TRACE("returning %i\n",r);
139
140 return r;
141 }
142
143 /***********************************************************************
144 * HlinkNavigate (HLINK.@)
145 */
146 HRESULT WINAPI HlinkNavigate(IHlink *phl, IHlinkFrame *phlFrame,
147 DWORD grfHLNF, LPBC pbc, IBindStatusCallback *pbsc,
148 IHlinkBrowseContext *phlbc)
149 {
150 HRESULT r = S_OK;
151
152 TRACE("%p %p %i %p %p %p\n", phl, phlFrame, grfHLNF, pbc, pbsc, phlbc);
153
154 if (phlFrame)
155 r = IHlinkFrame_Navigate(phlFrame, grfHLNF, pbc, pbsc, phl);
156 else if (phl)
157 r = IHlink_Navigate(phl, grfHLNF, pbc, pbsc, phlbc);
158
159 return r;
160 }
161
162 /***********************************************************************
163 * HlinkOnNavigate (HLINK.@)
164 */
165 HRESULT WINAPI HlinkOnNavigate( IHlinkFrame *phlFrame,
166 IHlinkBrowseContext* phlbc, DWORD grfHLNF, IMoniker *pmkTarget,
167 LPCWSTR pwzLocation, LPCWSTR pwzFriendlyName, ULONG* puHLID)
168 {
169 HRESULT r = S_OK;
170
171 TRACE("%p %p %i %p %s %s %p\n",phlFrame, phlbc, grfHLNF, pmkTarget,
172 debugstr_w(pwzLocation), debugstr_w(pwzFriendlyName), puHLID);
173
174 r = IHlinkBrowseContext_OnNavigateHlink(phlbc, grfHLNF, pmkTarget,
175 pwzLocation, pwzFriendlyName, puHLID);
176
177 if (phlFrame)
178 r = IHlinkFrame_OnNavigate(phlFrame,grfHLNF,pmkTarget, pwzLocation,
179 pwzFriendlyName, 0);
180
181 return r;
182 }
183
184 /***********************************************************************
185 * HlinkCreateFromData (HLINK.@)
186 */
187 HRESULT WINAPI HlinkCreateFromData(IDataObject *piDataObj,
188 IHlinkSite *pihlsite, DWORD dwSiteData, IUnknown *piunkOuter,
189 REFIID riid, void **ppvObj)
190 {
191 FIXME("%p %p %d %p %p %p\n",
192 piDataObj, pihlsite, dwSiteData, piunkOuter, riid, ppvObj);
193 *ppvObj = NULL;
194 return E_NOTIMPL;
195 }
196
197 /***********************************************************************
198 * HlinkQueryCreateFromData (HLINK.@)
199 */
200 HRESULT WINAPI HlinkQueryCreateFromData(IDataObject* piDataObj)
201 {
202 FIXME("%p\n", piDataObj);
203 return E_NOTIMPL;
204 }
205
206 /***********************************************************************
207 * HlinkNavigateToStringReference (HLINK.@)
208 */
209 HRESULT WINAPI HlinkNavigateToStringReference( LPCWSTR pwzTarget,
210 LPCWSTR pwzLocation, IHlinkSite *pihlsite, DWORD dwSiteData,
211 IHlinkFrame *pihlframe, DWORD grfHLNF, LPBC pibc,
212 IBindStatusCallback *pibsc, IHlinkBrowseContext *pihlbc)
213 {
214 HRESULT r;
215 IHlink *hlink = NULL;
216
217 FIXME("%s %s %p %08x %p %08x %p %p %p\n",
218 debugstr_w(pwzTarget), debugstr_w(pwzLocation), pihlsite,
219 dwSiteData, pihlframe, grfHLNF, pibc, pibsc, pihlbc);
220
221 r = HlinkCreateFromString( pwzTarget, pwzLocation, NULL, pihlsite,
222 dwSiteData, NULL, &IID_IHlink, (LPVOID*) &hlink );
223 if (SUCCEEDED(r))
224 r = HlinkNavigate(hlink, pihlframe, grfHLNF, pibc, pibsc, pihlbc);
225
226 return r;
227 }
228
229 /***********************************************************************
230 * HlinkIsShortcut (HLINK.@)
231 */
232 HRESULT WINAPI HlinkIsShortcut(LPCWSTR pwzFileName)
233 {
234 int len;
235
236 static const WCHAR url_ext[] = {'.','u','r','l',0};
237
238 TRACE("(%s)\n", debugstr_w(pwzFileName));
239
240 if(!pwzFileName)
241 return E_INVALIDARG;
242
243 len = strlenW(pwzFileName)-4;
244 if(len < 0)
245 return S_FALSE;
246
247 return strcmpiW(pwzFileName+len, url_ext) ? S_FALSE : S_OK;
248 }
249
250 /***********************************************************************
251 * HlinkGetSpecialReference (HLINK.@)
252 */
253 HRESULT WINAPI HlinkGetSpecialReference(ULONG uReference, LPWSTR *ppwzReference)
254 {
255 DWORD res, type, size = 100;
256 LPCWSTR value_name;
257 WCHAR *buf;
258 HKEY hkey;
259
260 static const WCHAR start_pageW[] = {'S','t','a','r','t',' ','P','a','g','e',0};
261 static const WCHAR search_pageW[] = {'S','e','a','r','c','h',' ','P','a','g','e',0};
262
263 static const WCHAR ie_main_keyW[] =
264 {'S','o','f','t','w','a','r','e',
265 '\\','M','i','c','r','o','s','o','f','t','\\',
266 'I','n','t','e','r','n','e','t',' ','E','x','p','l','o','r','e','r',
267 '\\','M','a','i','n',0};
268
269 TRACE("(%u %p)\n", uReference, ppwzReference);
270
271 *ppwzReference = NULL;
272
273 switch(uReference) {
274 case HLSR_HOME:
275 value_name = start_pageW;
276 break;
277 case HLSR_SEARCHPAGE:
278 value_name = search_pageW;
279 break;
280 case HLSR_HISTORYFOLDER:
281 return E_NOTIMPL;
282 default:
283 return E_INVALIDARG;
284 }
285
286 res = RegOpenKeyW(HKEY_CURRENT_USER, ie_main_keyW, &hkey);
287 if(res != ERROR_SUCCESS) {
288 WARN("Could not open key: %u\n", res);
289 return HRESULT_FROM_WIN32(res);
290 }
291
292 buf = CoTaskMemAlloc(size);
293 res = RegQueryValueExW(hkey, value_name, NULL, &type, (PBYTE)buf, &size);
294 buf = CoTaskMemRealloc(buf, size);
295 if(res == ERROR_MORE_DATA)
296 res = RegQueryValueExW(hkey, value_name, NULL, &type, (PBYTE)buf, &size);
297 RegCloseKey(hkey);
298 if(res != ERROR_SUCCESS) {
299 WARN("Could not query value %s: %u\n", debugstr_w(value_name), res);
300 CoTaskMemFree(buf);
301 return HRESULT_FROM_WIN32(res);
302 }
303
304 *ppwzReference = buf;
305 return S_OK;
306 }
307
308 /***********************************************************************
309 * HlinkTranslateURL (HLINK.@)
310 */
311 HRESULT WINAPI HlinkTranslateURL(LPCWSTR pwzURL, DWORD grfFlags, LPWSTR *ppwzTranslatedURL)
312 {
313 FIXME("(%s %08x %p)\n", debugstr_w(pwzURL), grfFlags, ppwzTranslatedURL);
314 return E_NOTIMPL;
315 }
316
317 /***********************************************************************
318 * HlinkUpdateStackItem (HLINK.@)
319 */
320 HRESULT WINAPI HlinkUpdateStackItem(IHlinkFrame *pihlframe, IHlinkBrowseContext *pihlbc,
321 ULONG uHLID, IMoniker *pimkTrgt, LPCWSTR pwzLocation, LPCWSTR pwzFriendlyName)
322 {
323 FIXME("(%p %p %u %p %s %s)\n", pihlframe, pihlbc, uHLID, pimkTrgt, debugstr_w(pwzLocation),
324 debugstr_w(pwzFriendlyName));
325 return E_NOTIMPL;
326 }
327
328 /***********************************************************************
329 * HlinkParseDisplayName (HLINK.@)
330 */
331 HRESULT WINAPI HlinkParseDisplayName(LPBC pibc, LPCWSTR pwzDisplayName, BOOL fNoForceAbs,
332 ULONG *pcchEaten, IMoniker **ppimk)
333 {
334 HRESULT hres;
335
336 TRACE("(%p %s %x %p %p)\n", pibc, debugstr_w(pwzDisplayName), fNoForceAbs, pcchEaten, ppimk);
337
338 if(fNoForceAbs)
339 FIXME("Unsupported fNoForceAbs\n");
340
341 hres = MkParseDisplayNameEx(pibc, pwzDisplayName, pcchEaten, ppimk);
342 if(SUCCEEDED(hres))
343 return hres;
344
345 hres = MkParseDisplayName(pibc, pwzDisplayName, pcchEaten, ppimk);
346 if(SUCCEEDED(hres))
347 return hres;
348
349 hres = CreateFileMoniker(pwzDisplayName, ppimk);
350 if(SUCCEEDED(hres))
351 *pcchEaten = strlenW(pwzDisplayName);
352
353 return hres;
354 }
355
356 /***********************************************************************
357 * HlinkResolveMonikerForData (HLINK.@)
358 */
359 HRESULT WINAPI HlinkResolveMonikerForData(LPMONIKER pimkReference, DWORD reserved, LPBC pibc,
360 ULONG cFmtetc, FORMATETC *rgFmtetc, IBindStatusCallback *pibsc, LPMONIKER pimkBase)
361 {
362 LPOLESTR name = NULL;
363 IBindCtx *bctx;
364 DWORD mksys = 0;
365 void *obj = NULL;
366 HRESULT hres;
367
368 TRACE("(%p %x %p %d %p %p %p)\n", pimkReference, reserved, pibc, cFmtetc, rgFmtetc, pibsc, pimkBase);
369
370 if(cFmtetc || rgFmtetc || pimkBase)
371 FIXME("Unsupported args\n");
372
373 hres = RegisterBindStatusCallback(pibc, pibsc, NULL /* FIXME */, 0);
374 if(FAILED(hres))
375 return hres;
376
377 hres = IMoniker_IsSystemMoniker(pimkReference, &mksys);
378 if(SUCCEEDED(hres) && mksys != MKSYS_URLMONIKER)
379 WARN("sysmk = %x\n", mksys);
380
381 /* FIXME: What is it for? */
382 CreateBindCtx(0, &bctx);
383 hres = IMoniker_GetDisplayName(pimkReference, bctx, NULL, &name);
384 IBindCtx_Release(bctx);
385 if(SUCCEEDED(hres)) {
386 TRACE("got display name %s\n", debugstr_w(name));
387 CoTaskMemFree(name);
388 }
389
390 return IMoniker_BindToStorage(pimkReference, pibc, NULL, &IID_IUnknown, &obj);
391 }
392
393 static HRESULT WINAPI HLinkCF_fnQueryInterface ( LPCLASSFACTORY iface,
394 REFIID riid, LPVOID *ppvObj)
395 {
396 CFImpl *This = (CFImpl *)iface;
397
398 TRACE("(%p)->(%s)\n",This,debugstr_guid(riid));
399
400 *ppvObj = NULL;
401
402 if (IsEqualIID(riid, &IID_IUnknown) ||
403 IsEqualIID(riid, &IID_IClassFactory))
404 {
405 *ppvObj = This;
406 return S_OK;
407 }
408
409 TRACE("-- E_NOINTERFACE\n");
410 return E_NOINTERFACE;
411 }
412
413 static ULONG WINAPI HLinkCF_fnAddRef (LPCLASSFACTORY iface)
414 {
415 return 2;
416 }
417
418 static ULONG WINAPI HLinkCF_fnRelease(LPCLASSFACTORY iface)
419 {
420 return 1;
421 }
422
423 static HRESULT WINAPI HLinkCF_fnCreateInstance( LPCLASSFACTORY iface,
424 LPUNKNOWN pUnkOuter, REFIID riid, LPVOID *ppvObject)
425 {
426 CFImpl *This = (CFImpl *)iface;
427
428 TRACE("%p->(%p,%s,%p)\n", This, pUnkOuter, debugstr_guid(riid), ppvObject);
429
430 *ppvObject = NULL;
431
432 return This->lpfnCI(pUnkOuter, riid, ppvObject);
433 }
434
435 static HRESULT WINAPI HLinkCF_fnLockServer(LPCLASSFACTORY iface, BOOL fLock)
436 {
437 FIXME("%p %d\n", iface, fLock);
438 return E_NOTIMPL;
439 }
440
441 static const IClassFactoryVtbl hlcfvt =
442 {
443 HLinkCF_fnQueryInterface,
444 HLinkCF_fnAddRef,
445 HLinkCF_fnRelease,
446 HLinkCF_fnCreateInstance,
447 HLinkCF_fnLockServer
448 };
449
450 static CFImpl HLink_cf = { &hlcfvt, HLink_Constructor };
451 static CFImpl HLinkBrowseContext_cf = { &hlcfvt, HLinkBrowseContext_Constructor };
452
453 /***********************************************************************
454 * DllGetClassObject (HLINK.@)
455 */
456 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv)
457 {
458 IClassFactory *pcf = NULL;
459
460 TRACE("%s %s %p\n", debugstr_guid(rclsid), debugstr_guid(iid), ppv);
461
462 if (!ppv)
463 return E_INVALIDARG;
464 *ppv = NULL;
465
466 if (IsEqualIID(rclsid, &CLSID_StdHlink))
467 pcf = (IClassFactory*) &HLink_cf;
468 else if (IsEqualIID(rclsid, &CLSID_StdHlinkBrowseContext))
469 pcf = (IClassFactory*) &HLinkBrowseContext_cf;
470 else
471 return CLASS_E_CLASSNOTAVAILABLE;
472
473 return IClassFactory_QueryInterface(pcf, iid, ppv);
474 }
475
476 static HRESULT register_clsid(LPCGUID guid)
477 {
478 static const WCHAR clsid[] =
479 {'C','L','S','I','D','\\',0};
480 static const WCHAR ips[] =
481 {'\\','I','n','p','r','o','c','S','e','r','v','e','r','3','2',0};
482 static const WCHAR hlink[] =
483 {'h','l','i','n','k','.','d','l','l',0};
484 static const WCHAR threading_model[] =
485 {'T','h','r','e','a','d','i','n','g','M','o','d','e','l',0};
486 static const WCHAR apartment[] =
487 {'A','p','a','r','t','m','e','n','t',0};
488 WCHAR path[80];
489 HKEY key = NULL;
490 LONG r;
491
492 lstrcpyW(path, clsid);
493 StringFromGUID2(guid, &path[6], 80);
494 lstrcatW(path, ips);
495 r = RegCreateKeyW(HKEY_CLASSES_ROOT, path, &key);
496 if (r != ERROR_SUCCESS)
497 return E_FAIL;
498
499 RegSetValueExW(key, NULL, 0, REG_SZ, (const BYTE *)hlink, sizeof hlink);
500 RegSetValueExW(key, threading_model, 0, REG_SZ, (const BYTE *)apartment, sizeof apartment);
501 RegCloseKey(key);
502
503 return S_OK;
504 }
505
506 /***********************************************************************
507 * DllRegisterServer (HLINK.@)
508 */
509 HRESULT WINAPI DllRegisterServer(void)
510 {
511 HRESULT r;
512
513 r = register_clsid(&CLSID_StdHlink);
514 if (SUCCEEDED(r))
515 r = register_clsid(&CLSID_StdHlinkBrowseContext);
516
517 return S_OK;
518 }