2 * Copyright 2008 Damjan Jovanovic
4 * ShellLink's barely documented cousin that handles URLs.
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
23 * Implement the IShellLinkA/W interfaces
24 * Handle the SetURL flags
25 * Implement any other interfaces? Does any software actually use them?
27 * The installer for the Zuma Deluxe Popcap game is good for testing.
33 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(shdocvw);
46 IUniformResourceLocatorA uniformResourceLocatorA;
47 IUniformResourceLocatorW uniformResourceLocatorW;
48 IPersistFile persistFile;
57 /* utility functions */
59 static inline InternetShortcut* impl_from_IUniformResourceLocatorA(IUniformResourceLocatorA *iface)
61 return CONTAINING_RECORD(iface, InternetShortcut, uniformResourceLocatorA);
64 static inline InternetShortcut* impl_from_IUniformResourceLocatorW(IUniformResourceLocatorW *iface)
66 return CONTAINING_RECORD(iface, InternetShortcut, uniformResourceLocatorW);
69 static inline InternetShortcut* impl_from_IPersistFile(IPersistFile *iface)
71 return CONTAINING_RECORD(iface, InternetShortcut, persistFile);
74 static BOOL run_winemenubuilder( const WCHAR *args )
76 static const WCHAR menubuilder[] = {'\\','w','i','n','e','m','e','n','u','b','u','i','l','d','e','r','.','e','x','e',0};
80 PROCESS_INFORMATION pi;
85 GetSystemDirectoryW( app, MAX_PATH - sizeof(menubuilder)/sizeof(WCHAR) );
86 strcatW( app, menubuilder );
88 len = (strlenW( app ) + strlenW( args ) + 1) * sizeof(WCHAR);
89 buffer = heap_alloc( len );
93 strcpyW( buffer, app );
94 strcatW( buffer, args );
96 TRACE("starting %s\n",debugstr_w(buffer));
98 memset(&si, 0, sizeof(si));
101 Wow64DisableWow64FsRedirection( &redir );
102 ret = CreateProcessW( app, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi );
103 Wow64RevertWow64FsRedirection( redir );
109 CloseHandle( pi.hProcess );
110 CloseHandle( pi.hThread );
116 static BOOL StartLinkProcessor( LPCOLESTR szLink )
118 static const WCHAR szFormat[] = { ' ','-','w',' ','-','u',' ','"','%','s','"',0 };
123 len = sizeof(szFormat) + lstrlenW( szLink ) * sizeof(WCHAR);
124 buffer = heap_alloc( len );
128 wsprintfW( buffer, szFormat, szLink );
129 ret = run_winemenubuilder( buffer );
134 /* interface functions */
136 static HRESULT Unknown_QueryInterface(InternetShortcut *This, REFIID riid, PVOID *ppvObject)
138 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppvObject);
140 if (IsEqualGUID(&IID_IUnknown, riid))
141 *ppvObject = &This->uniformResourceLocatorA;
142 else if (IsEqualGUID(&IID_IUniformResourceLocatorA, riid))
143 *ppvObject = &This->uniformResourceLocatorA;
144 else if (IsEqualGUID(&IID_IUniformResourceLocatorW, riid))
145 *ppvObject = &This->uniformResourceLocatorW;
146 else if (IsEqualGUID(&IID_IPersistFile, riid))
147 *ppvObject = &This->persistFile;
148 else if (IsEqualGUID(&IID_IShellLinkA, riid))
150 FIXME("The IShellLinkA interface is not yet supported by InternetShortcut\n");
151 return E_NOINTERFACE;
153 else if (IsEqualGUID(&IID_IShellLinkW, riid))
155 FIXME("The IShellLinkW interface is not yet supported by InternetShortcut\n");
156 return E_NOINTERFACE;
160 FIXME("Interface with GUID %s not yet implemented by InternetShortcut\n", debugstr_guid(riid));
161 return E_NOINTERFACE;
163 IUnknown_AddRef((IUnknown*)*ppvObject);
167 static ULONG Unknown_AddRef(InternetShortcut *This)
169 TRACE("(%p)\n", This);
170 return InterlockedIncrement(&This->refCount);
173 static ULONG Unknown_Release(InternetShortcut *This)
176 TRACE("(%p)\n", This);
177 count = InterlockedDecrement(&This->refCount);
180 CoTaskMemFree(This->url);
181 CoTaskMemFree(This->currentFile);
183 SHDOCVW_UnlockModule();
188 static HRESULT WINAPI UniformResourceLocatorW_QueryInterface(IUniformResourceLocatorW *url, REFIID riid, PVOID *ppvObject)
190 InternetShortcut *This = impl_from_IUniformResourceLocatorW(url);
191 TRACE("(%p, %s, %p)\n", url, debugstr_guid(riid), ppvObject);
192 return Unknown_QueryInterface(This, riid, ppvObject);
195 static ULONG WINAPI UniformResourceLocatorW_AddRef(IUniformResourceLocatorW *url)
197 InternetShortcut *This = impl_from_IUniformResourceLocatorW(url);
198 TRACE("(%p)\n", url);
199 return Unknown_AddRef(This);
202 static ULONG WINAPI UniformResourceLocatorW_Release(IUniformResourceLocatorW *url)
204 InternetShortcut *This = impl_from_IUniformResourceLocatorW(url);
205 TRACE("(%p)\n", url);
206 return Unknown_Release(This);
209 static HRESULT WINAPI UniformResourceLocatorW_SetUrl(IUniformResourceLocatorW *url, LPCWSTR pcszURL, DWORD dwInFlags)
211 WCHAR *newURL = NULL;
212 InternetShortcut *This = impl_from_IUniformResourceLocatorW(url);
213 TRACE("(%p, %s, 0x%x)\n", url, debugstr_w(pcszURL), dwInFlags);
215 FIXME("ignoring unsupported flags 0x%x\n", dwInFlags);
218 newURL = co_strdupW(pcszURL);
220 return E_OUTOFMEMORY;
222 CoTaskMemFree(This->url);
224 This->isDirty = TRUE;
228 static HRESULT WINAPI UniformResourceLocatorW_GetUrl(IUniformResourceLocatorW *url, LPWSTR *ppszURL)
231 InternetShortcut *This = impl_from_IUniformResourceLocatorW(url);
232 TRACE("(%p, %p)\n", url, ppszURL);
233 if (This->url == NULL)
237 *ppszURL = co_strdupW(This->url);
238 if (*ppszURL == NULL)
244 static HRESULT WINAPI UniformResourceLocatorW_InvokeCommand(IUniformResourceLocatorW *url, PURLINVOKECOMMANDINFOW pCommandInfo)
246 InternetShortcut *This = impl_from_IUniformResourceLocatorW(url);
249 static const WCHAR wszURLProtocol[] = {'U','R','L',' ','P','r','o','t','o','c','o','l',0};
250 SHELLEXECUTEINFOW sei;
254 TRACE("%p %p\n", This, pCommandInfo );
256 if (pCommandInfo->dwcbSize < sizeof (URLINVOKECOMMANDINFOW))
259 if (pCommandInfo->dwFlags != IURL_INVOKECOMMAND_FL_USE_DEFAULT_VERB)
261 FIXME("(%p, %p): non-default verbs not implemented\n", url, pCommandInfo);
265 hres = CoInternetParseUrl(This->url, PARSE_SCHEMA, 0, app, sizeof(app)/sizeof(WCHAR), NULL, 0);
269 res = RegOpenKeyW(HKEY_CLASSES_ROOT, app, &hkey);
270 if(res != ERROR_SUCCESS)
273 res = RegQueryValueExW(hkey, wszURLProtocol, NULL, &type, NULL, NULL);
275 if(res != ERROR_SUCCESS || type != REG_SZ)
278 memset(&sei, 0, sizeof(sei));
279 sei.cbSize = sizeof(sei);
280 sei.lpFile = This->url;
283 if( ShellExecuteExW(&sei) )
289 static HRESULT WINAPI UniformResourceLocatorA_QueryInterface(IUniformResourceLocatorA *url, REFIID riid, PVOID *ppvObject)
291 InternetShortcut *This = impl_from_IUniformResourceLocatorA(url);
292 TRACE("(%p, %s, %p)\n", url, debugstr_guid(riid), ppvObject);
293 return Unknown_QueryInterface(This, riid, ppvObject);
296 static ULONG WINAPI UniformResourceLocatorA_AddRef(IUniformResourceLocatorA *url)
298 InternetShortcut *This = impl_from_IUniformResourceLocatorA(url);
299 TRACE("(%p)\n", url);
300 return Unknown_AddRef(This);
303 static ULONG WINAPI UniformResourceLocatorA_Release(IUniformResourceLocatorA *url)
305 InternetShortcut *This = impl_from_IUniformResourceLocatorA(url);
306 TRACE("(%p)\n", url);
307 return Unknown_Release(This);
310 static HRESULT WINAPI UniformResourceLocatorA_SetUrl(IUniformResourceLocatorA *url, LPCSTR pcszURL, DWORD dwInFlags)
312 WCHAR *newURL = NULL;
313 InternetShortcut *This = impl_from_IUniformResourceLocatorA(url);
314 TRACE("(%p, %s, 0x%x)\n", url, debugstr_a(pcszURL), dwInFlags);
316 FIXME("ignoring unsupported flags 0x%x\n", dwInFlags);
319 newURL = co_strdupAtoW(pcszURL);
321 return E_OUTOFMEMORY;
323 CoTaskMemFree(This->url);
325 This->isDirty = TRUE;
329 static HRESULT WINAPI UniformResourceLocatorA_GetUrl(IUniformResourceLocatorA *url, LPSTR *ppszURL)
332 InternetShortcut *This = impl_from_IUniformResourceLocatorA(url);
333 TRACE("(%p, %p)\n", url, ppszURL);
334 if (This->url == NULL)
338 *ppszURL = co_strdupWtoA(This->url);
339 if (*ppszURL == NULL)
345 static HRESULT WINAPI UniformResourceLocatorA_InvokeCommand(IUniformResourceLocatorA *url, PURLINVOKECOMMANDINFOA pCommandInfo)
347 URLINVOKECOMMANDINFOW wideCommandInfo;
351 InternetShortcut *This = impl_from_IUniformResourceLocatorA(url);
353 wideCommandInfo.dwcbSize = sizeof wideCommandInfo;
354 wideCommandInfo.dwFlags = pCommandInfo->dwFlags;
355 wideCommandInfo.hwndParent = pCommandInfo->hwndParent;
357 len = MultiByteToWideChar(CP_ACP, 0, pCommandInfo->pcszVerb, -1, NULL, 0);
358 wideVerb = heap_alloc(len * sizeof(WCHAR));
359 MultiByteToWideChar(CP_ACP, 0, pCommandInfo->pcszVerb, -1, wideVerb, len);
361 wideCommandInfo.pcszVerb = wideVerb;
363 res = UniformResourceLocatorW_InvokeCommand(&This->uniformResourceLocatorW, &wideCommandInfo);
369 static HRESULT WINAPI PersistFile_QueryInterface(IPersistFile *pFile, REFIID riid, PVOID *ppvObject)
371 InternetShortcut *This = impl_from_IPersistFile(pFile);
372 TRACE("(%p, %s, %p)\n", pFile, debugstr_guid(riid), ppvObject);
373 return Unknown_QueryInterface(This, riid, ppvObject);
376 static ULONG WINAPI PersistFile_AddRef(IPersistFile *pFile)
378 InternetShortcut *This = impl_from_IPersistFile(pFile);
379 TRACE("(%p)\n", pFile);
380 return Unknown_AddRef(This);
383 static ULONG WINAPI PersistFile_Release(IPersistFile *pFile)
385 InternetShortcut *This = impl_from_IPersistFile(pFile);
386 TRACE("(%p)\n", pFile);
387 return Unknown_Release(This);
390 static HRESULT WINAPI PersistFile_GetClassID(IPersistFile *pFile, CLSID *pClassID)
392 TRACE("(%p, %p)\n", pFile, pClassID);
393 *pClassID = CLSID_InternetShortcut;
397 static HRESULT WINAPI PersistFile_IsDirty(IPersistFile *pFile)
399 InternetShortcut *This = impl_from_IPersistFile(pFile);
400 TRACE("(%p)\n", pFile);
401 return This->isDirty ? S_OK : S_FALSE;
404 static HRESULT WINAPI PersistFile_Load(IPersistFile *pFile, LPCOLESTR pszFileName, DWORD dwMode)
406 WCHAR str_header[] = {'I','n','t','e','r','n','e','t','S','h','o','r','t','c','u','t',0};
407 WCHAR str_URL[] = {'U','R','L',0};
408 WCHAR *filename = NULL;
410 InternetShortcut *This = impl_from_IPersistFile(pFile);
411 TRACE("(%p, %s, 0x%x)\n", pFile, debugstr_w(pszFileName), dwMode);
413 FIXME("ignoring unimplemented mode 0x%x\n", dwMode);
414 filename = co_strdupW(pszFileName);
415 if (filename != NULL)
419 WCHAR *url = CoTaskMemAlloc(len*sizeof(WCHAR));
422 r = GetPrivateProfileStringW(str_header, str_URL, NULL, url, len, pszFileName);
427 url = CoTaskMemAlloc(len*sizeof(WCHAR));
430 r = GetPrivateProfileStringW(str_header, str_URL, NULL, url, len, pszFileName);
434 else if (url != NULL)
436 CoTaskMemFree(This->currentFile);
437 This->currentFile = filename;
438 CoTaskMemFree(This->url);
440 This->isDirty = FALSE;
449 CoTaskMemFree(filename);
456 static HRESULT WINAPI PersistFile_Save(IPersistFile *pFile, LPCOLESTR pszFileName, BOOL fRemember)
461 InternetShortcut *This = impl_from_IPersistFile(pFile);
463 TRACE("(%p, %s, %d)\n", pFile, debugstr_w(pszFileName), fRemember);
465 if (pszFileName != NULL && fRemember)
467 LPOLESTR oldFile = This->currentFile;
468 This->currentFile = co_strdupW(pszFileName);
469 if (This->currentFile == NULL)
471 This->currentFile = oldFile;
472 return E_OUTOFMEMORY;
474 CoTaskMemFree(oldFile);
476 if (This->url == NULL)
479 /* Windows seems to always write:
480 * ASCII "[InternetShortcut]" headers
481 * ASCII names in "name=value" pairs
482 * An ASCII (probably UTF8?) value in "URL=..."
484 len = WideCharToMultiByte(CP_UTF8, 0, This->url, -1, NULL, 0, 0, 0);
485 url = heap_alloc(len);
489 WideCharToMultiByte(CP_UTF8, 0, This->url, -1, url, len, 0, 0);
490 file = CreateFileW(pszFileName, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
491 if (file != INVALID_HANDLE_VALUE)
494 char str_header[] = "[InternetShortcut]";
495 char str_URL[] = "URL=";
496 char str_eol[] = "\r\n";
498 WriteFile(file, str_header, lstrlenA(str_header), &bytesWritten, NULL);
499 WriteFile(file, str_eol, lstrlenA(str_eol), &bytesWritten, NULL);
500 WriteFile(file, str_URL, lstrlenA(str_URL), &bytesWritten, NULL);
501 WriteFile(file, url, lstrlenA(url), &bytesWritten, NULL);
502 WriteFile(file, str_eol, lstrlenA(str_eol), &bytesWritten, NULL);
504 if (pszFileName == NULL || fRemember)
505 This->isDirty = FALSE;
506 StartLinkProcessor(pszFileName);
518 static HRESULT WINAPI PersistFile_SaveCompleted(IPersistFile *pFile, LPCOLESTR pszFileName)
520 FIXME("(%p, %p): stub\n", pFile, pszFileName);
524 static HRESULT WINAPI PersistFile_GetCurFile(IPersistFile *pFile, LPOLESTR *ppszFileName)
527 InternetShortcut *This = impl_from_IPersistFile(pFile);
528 TRACE("(%p, %p)\n", pFile, ppszFileName);
529 if (This->currentFile == NULL)
530 *ppszFileName = NULL;
533 *ppszFileName = co_strdupW(This->currentFile);
534 if (*ppszFileName == NULL)
542 static const IUniformResourceLocatorWVtbl uniformResourceLocatorWVtbl = {
543 UniformResourceLocatorW_QueryInterface,
544 UniformResourceLocatorW_AddRef,
545 UniformResourceLocatorW_Release,
546 UniformResourceLocatorW_SetUrl,
547 UniformResourceLocatorW_GetUrl,
548 UniformResourceLocatorW_InvokeCommand
551 static const IUniformResourceLocatorAVtbl uniformResourceLocatorAVtbl = {
552 UniformResourceLocatorA_QueryInterface,
553 UniformResourceLocatorA_AddRef,
554 UniformResourceLocatorA_Release,
555 UniformResourceLocatorA_SetUrl,
556 UniformResourceLocatorA_GetUrl,
557 UniformResourceLocatorA_InvokeCommand
560 static const IPersistFileVtbl persistFileVtbl = {
561 PersistFile_QueryInterface,
564 PersistFile_GetClassID,
568 PersistFile_SaveCompleted,
569 PersistFile_GetCurFile
572 static InternetShortcut *create_shortcut(void)
574 InternetShortcut *newshortcut;
576 newshortcut = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(InternetShortcut));
579 newshortcut->uniformResourceLocatorA.lpVtbl = &uniformResourceLocatorAVtbl;
580 newshortcut->uniformResourceLocatorW.lpVtbl = &uniformResourceLocatorWVtbl;
581 newshortcut->persistFile.lpVtbl = &persistFileVtbl;
582 newshortcut->refCount = 0;
588 HRESULT InternetShortcut_Create(IUnknown *pOuter, REFIID riid, void **ppv)
590 InternetShortcut *This;
593 TRACE("(%p, %s, %p)\n", pOuter, debugstr_guid(riid), ppv);
598 return CLASS_E_NOAGGREGATION;
600 This = create_shortcut();
603 hr = Unknown_QueryInterface(This, riid, ppv);
605 SHDOCVW_LockModule();
611 return E_OUTOFMEMORY;
615 /**********************************************************************
616 * OpenURL (SHDOCVW.@)
618 void WINAPI OpenURL(HWND hWnd, HINSTANCE hInst, LPCSTR lpcstrUrl, int nShowCmd)
620 InternetShortcut *shortcut;
621 WCHAR* urlfilepath = NULL;
622 shortcut = create_shortcut();
628 len = MultiByteToWideChar(CP_ACP, 0, lpcstrUrl, -1, NULL, 0);
629 urlfilepath = heap_alloc(len * sizeof(WCHAR));
630 MultiByteToWideChar(CP_ACP, 0, lpcstrUrl, -1, urlfilepath, len);
632 if(SUCCEEDED(IPersistFile_Load(&shortcut->persistFile, urlfilepath, 0)))
634 URLINVOKECOMMANDINFOW ici;
636 memset( &ici, 0, sizeof ici );
637 ici.dwcbSize = sizeof ici;
638 ici.dwFlags = IURL_INVOKECOMMAND_FL_USE_DEFAULT_VERB;
639 ici.hwndParent = hWnd;
641 if FAILED(UniformResourceLocatorW_InvokeCommand(&shortcut->uniformResourceLocatorW, (PURLINVOKECOMMANDINFOW) &ici))
642 TRACE("failed to open URL: %s\n.",debugstr_a(lpcstrUrl));
646 heap_free(urlfilepath);