include: Add event trace flags and guid to evntrace.h.
[wine.git] / dlls / ieframe / intshcut.c
blobb2d3ba9979ba91ba06768783a18c7bf9a5159313
1 /*
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
22 * TODO:
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.
30 #include <stdio.h>
32 #define NONAMELESSUNION
34 #include "ieframe.h"
36 #include "shlobj.h"
37 #include "shobjidl.h"
38 #include "intshcut.h"
39 #include "shellapi.h"
40 #include "winreg.h"
41 #include "shlwapi.h"
42 #include "shlguid.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(ieframe);
48 typedef struct
50 IUniformResourceLocatorA IUniformResourceLocatorA_iface;
51 IUniformResourceLocatorW IUniformResourceLocatorW_iface;
52 IPersistFile IPersistFile_iface;
53 IPropertySetStorage IPropertySetStorage_iface;
55 LONG refCount;
57 IPropertySetStorage *property_set_storage;
58 WCHAR *url;
59 BOOLEAN isDirty;
60 LPOLESTR currentFile;
61 } InternetShortcut;
63 /* utility functions */
65 static inline InternetShortcut* impl_from_IUniformResourceLocatorA(IUniformResourceLocatorA *iface)
67 return CONTAINING_RECORD(iface, InternetShortcut, IUniformResourceLocatorA_iface);
70 static inline InternetShortcut* impl_from_IUniformResourceLocatorW(IUniformResourceLocatorW *iface)
72 return CONTAINING_RECORD(iface, InternetShortcut, IUniformResourceLocatorW_iface);
75 static inline InternetShortcut* impl_from_IPersistFile(IPersistFile *iface)
77 return CONTAINING_RECORD(iface, InternetShortcut, IPersistFile_iface);
80 static inline InternetShortcut* impl_from_IPropertySetStorage(IPropertySetStorage *iface)
82 return CONTAINING_RECORD(iface, InternetShortcut, IPropertySetStorage_iface);
85 static BOOL run_winemenubuilder( const WCHAR *args )
87 static const WCHAR menubuilder[] = {'\\','w','i','n','e','m','e','n','u','b','u','i','l','d','e','r','.','e','x','e',0};
88 LONG len;
89 LPWSTR buffer;
90 STARTUPINFOW si;
91 PROCESS_INFORMATION pi;
92 BOOL ret;
93 WCHAR app[MAX_PATH];
94 void *redir;
96 GetSystemDirectoryW( app, MAX_PATH - sizeof(menubuilder)/sizeof(WCHAR) );
97 strcatW( app, menubuilder );
99 len = (strlenW( app ) + strlenW( args ) + 1) * sizeof(WCHAR);
100 buffer = heap_alloc( len );
101 if( !buffer )
102 return FALSE;
104 strcpyW( buffer, app );
105 strcatW( buffer, args );
107 TRACE("starting %s\n",debugstr_w(buffer));
109 memset(&si, 0, sizeof(si));
110 si.cb = sizeof(si);
112 Wow64DisableWow64FsRedirection( &redir );
113 ret = CreateProcessW( app, buffer, NULL, NULL, FALSE, DETACHED_PROCESS, NULL, NULL, &si, &pi );
114 Wow64RevertWow64FsRedirection( redir );
116 heap_free( buffer );
118 if (ret)
120 CloseHandle( pi.hProcess );
121 CloseHandle( pi.hThread );
124 return ret;
127 static BOOL StartLinkProcessor( LPCOLESTR szLink )
129 static const WCHAR szFormat[] = { ' ','-','w',' ','-','u',' ','"','%','s','"',0 };
130 LONG len;
131 LPWSTR buffer;
132 BOOL ret;
134 len = sizeof(szFormat) + lstrlenW( szLink ) * sizeof(WCHAR);
135 buffer = heap_alloc( len );
136 if( !buffer )
137 return FALSE;
139 sprintfW( buffer, szFormat, szLink );
140 ret = run_winemenubuilder( buffer );
141 heap_free( buffer );
142 return ret;
145 /* interface functions */
147 static HRESULT Unknown_QueryInterface(InternetShortcut *This, REFIID riid, PVOID *ppvObject)
149 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppvObject);
150 *ppvObject = NULL;
151 if (IsEqualGUID(&IID_IUnknown, riid))
152 *ppvObject = &This->IUniformResourceLocatorA_iface;
153 else if (IsEqualGUID(&IID_IUniformResourceLocatorA, riid))
154 *ppvObject = &This->IUniformResourceLocatorA_iface;
155 else if (IsEqualGUID(&IID_IUniformResourceLocatorW, riid))
156 *ppvObject = &This->IUniformResourceLocatorW_iface;
157 else if (IsEqualGUID(&IID_IPersistFile, riid))
158 *ppvObject = &This->IPersistFile_iface;
159 else if (IsEqualGUID(&IID_IPropertySetStorage, riid))
160 *ppvObject = &This->IPropertySetStorage_iface;
161 else if (IsEqualGUID(&IID_IShellLinkA, riid))
163 FIXME("The IShellLinkA interface is not yet supported by InternetShortcut\n");
164 return E_NOINTERFACE;
166 else if (IsEqualGUID(&IID_IShellLinkW, riid))
168 FIXME("The IShellLinkW interface is not yet supported by InternetShortcut\n");
169 return E_NOINTERFACE;
171 else
173 FIXME("Interface with GUID %s not yet implemented by InternetShortcut\n", debugstr_guid(riid));
174 return E_NOINTERFACE;
176 IUnknown_AddRef((IUnknown*)*ppvObject);
177 return S_OK;
180 static ULONG Unknown_AddRef(InternetShortcut *This)
182 TRACE("(%p)\n", This);
183 return InterlockedIncrement(&This->refCount);
186 static ULONG Unknown_Release(InternetShortcut *This)
188 ULONG count;
189 TRACE("(%p)\n", This);
190 count = InterlockedDecrement(&This->refCount);
191 if (count == 0)
193 CoTaskMemFree(This->url);
194 CoTaskMemFree(This->currentFile);
195 IPropertySetStorage_Release(This->property_set_storage);
196 heap_free(This);
197 unlock_module();
199 return count;
202 static HRESULT WINAPI UniformResourceLocatorW_QueryInterface(IUniformResourceLocatorW *url, REFIID riid, PVOID *ppvObject)
204 InternetShortcut *This = impl_from_IUniformResourceLocatorW(url);
205 TRACE("(%p, %s, %p)\n", url, debugstr_guid(riid), ppvObject);
206 return Unknown_QueryInterface(This, riid, ppvObject);
209 static ULONG WINAPI UniformResourceLocatorW_AddRef(IUniformResourceLocatorW *url)
211 InternetShortcut *This = impl_from_IUniformResourceLocatorW(url);
212 TRACE("(%p)\n", url);
213 return Unknown_AddRef(This);
216 static ULONG WINAPI UniformResourceLocatorW_Release(IUniformResourceLocatorW *url)
218 InternetShortcut *This = impl_from_IUniformResourceLocatorW(url);
219 TRACE("(%p)\n", url);
220 return Unknown_Release(This);
223 static HRESULT WINAPI UniformResourceLocatorW_SetUrl(IUniformResourceLocatorW *url, LPCWSTR pcszURL, DWORD dwInFlags)
225 WCHAR *newURL = NULL;
226 InternetShortcut *This = impl_from_IUniformResourceLocatorW(url);
227 TRACE("(%p, %s, 0x%x)\n", url, debugstr_w(pcszURL), dwInFlags);
228 if (dwInFlags != 0)
229 FIXME("ignoring unsupported flags 0x%x\n", dwInFlags);
230 if (pcszURL != NULL)
232 newURL = co_strdupW(pcszURL);
233 if (newURL == NULL)
234 return E_OUTOFMEMORY;
236 CoTaskMemFree(This->url);
237 This->url = newURL;
238 This->isDirty = TRUE;
239 return S_OK;
242 static HRESULT WINAPI UniformResourceLocatorW_GetUrl(IUniformResourceLocatorW *url, LPWSTR *ppszURL)
244 InternetShortcut *This = impl_from_IUniformResourceLocatorW(url);
246 TRACE("(%p, %p)\n", url, ppszURL);
248 if (!This->url) {
249 *ppszURL = NULL;
250 return S_FALSE;
253 *ppszURL = co_strdupW(This->url);
254 if (!*ppszURL)
255 return E_OUTOFMEMORY;
257 return S_OK;
260 static HRESULT WINAPI UniformResourceLocatorW_InvokeCommand(IUniformResourceLocatorW *url, PURLINVOKECOMMANDINFOW pCommandInfo)
262 InternetShortcut *This = impl_from_IUniformResourceLocatorW(url);
263 WCHAR app[64];
264 HKEY hkey;
265 static const WCHAR wszURLProtocol[] = {'U','R','L',' ','P','r','o','t','o','c','o','l',0};
266 SHELLEXECUTEINFOW sei;
267 DWORD res, type;
268 HRESULT hres;
270 TRACE("%p %p\n", This, pCommandInfo );
272 if (pCommandInfo->dwcbSize < sizeof (URLINVOKECOMMANDINFOW))
273 return E_INVALIDARG;
275 if (pCommandInfo->dwFlags != IURL_INVOKECOMMAND_FL_USE_DEFAULT_VERB)
277 FIXME("(%p, %p): non-default verbs not implemented\n", url, pCommandInfo);
278 return E_NOTIMPL;
281 hres = CoInternetParseUrl(This->url, PARSE_SCHEMA, 0, app, sizeof(app)/sizeof(WCHAR), NULL, 0);
282 if(FAILED(hres))
283 return E_FAIL;
285 res = RegOpenKeyW(HKEY_CLASSES_ROOT, app, &hkey);
286 if(res != ERROR_SUCCESS)
287 return E_FAIL;
289 res = RegQueryValueExW(hkey, wszURLProtocol, NULL, &type, NULL, NULL);
290 RegCloseKey(hkey);
291 if(res != ERROR_SUCCESS || type != REG_SZ)
292 return E_FAIL;
294 memset(&sei, 0, sizeof(sei));
295 sei.cbSize = sizeof(sei);
296 sei.lpFile = This->url;
297 sei.nShow = SW_SHOW;
299 if( ShellExecuteExW(&sei) )
300 return S_OK;
301 else
302 return E_FAIL;
305 static HRESULT WINAPI UniformResourceLocatorA_QueryInterface(IUniformResourceLocatorA *url, REFIID riid, PVOID *ppvObject)
307 InternetShortcut *This = impl_from_IUniformResourceLocatorA(url);
308 TRACE("(%p, %s, %p)\n", url, debugstr_guid(riid), ppvObject);
309 return Unknown_QueryInterface(This, riid, ppvObject);
312 static ULONG WINAPI UniformResourceLocatorA_AddRef(IUniformResourceLocatorA *url)
314 InternetShortcut *This = impl_from_IUniformResourceLocatorA(url);
315 TRACE("(%p)\n", url);
316 return Unknown_AddRef(This);
319 static ULONG WINAPI UniformResourceLocatorA_Release(IUniformResourceLocatorA *url)
321 InternetShortcut *This = impl_from_IUniformResourceLocatorA(url);
322 TRACE("(%p)\n", url);
323 return Unknown_Release(This);
326 static HRESULT WINAPI UniformResourceLocatorA_SetUrl(IUniformResourceLocatorA *url, LPCSTR pcszURL, DWORD dwInFlags)
328 WCHAR *newURL = NULL;
329 InternetShortcut *This = impl_from_IUniformResourceLocatorA(url);
330 TRACE("(%p, %s, 0x%x)\n", url, debugstr_a(pcszURL), dwInFlags);
331 if (dwInFlags != 0)
332 FIXME("ignoring unsupported flags 0x%x\n", dwInFlags);
333 if (pcszURL != NULL)
335 newURL = co_strdupAtoW(pcszURL);
336 if (newURL == NULL)
337 return E_OUTOFMEMORY;
339 CoTaskMemFree(This->url);
340 This->url = newURL;
341 This->isDirty = TRUE;
342 return S_OK;
345 static HRESULT WINAPI UniformResourceLocatorA_GetUrl(IUniformResourceLocatorA *url, LPSTR *ppszURL)
347 InternetShortcut *This = impl_from_IUniformResourceLocatorA(url);
349 TRACE("(%p, %p)\n", url, ppszURL);
351 if (!This->url) {
352 *ppszURL = NULL;
353 return S_FALSE;
357 *ppszURL = co_strdupWtoA(This->url);
358 if (!*ppszURL)
359 return E_OUTOFMEMORY;
361 return S_OK;
364 static HRESULT WINAPI UniformResourceLocatorA_InvokeCommand(IUniformResourceLocatorA *url, PURLINVOKECOMMANDINFOA pCommandInfo)
366 URLINVOKECOMMANDINFOW wideCommandInfo;
367 int len;
368 WCHAR *wideVerb;
369 HRESULT res;
370 InternetShortcut *This = impl_from_IUniformResourceLocatorA(url);
372 wideCommandInfo.dwcbSize = sizeof wideCommandInfo;
373 wideCommandInfo.dwFlags = pCommandInfo->dwFlags;
374 wideCommandInfo.hwndParent = pCommandInfo->hwndParent;
376 len = MultiByteToWideChar(CP_ACP, 0, pCommandInfo->pcszVerb, -1, NULL, 0);
377 wideVerb = heap_alloc(len * sizeof(WCHAR));
378 MultiByteToWideChar(CP_ACP, 0, pCommandInfo->pcszVerb, -1, wideVerb, len);
380 wideCommandInfo.pcszVerb = wideVerb;
382 res = UniformResourceLocatorW_InvokeCommand(&This->IUniformResourceLocatorW_iface, &wideCommandInfo);
383 heap_free(wideVerb);
385 return res;
388 static HRESULT WINAPI PersistFile_QueryInterface(IPersistFile *pFile, REFIID riid, PVOID *ppvObject)
390 InternetShortcut *This = impl_from_IPersistFile(pFile);
391 TRACE("(%p, %s, %p)\n", pFile, debugstr_guid(riid), ppvObject);
392 return Unknown_QueryInterface(This, riid, ppvObject);
395 static ULONG WINAPI PersistFile_AddRef(IPersistFile *pFile)
397 InternetShortcut *This = impl_from_IPersistFile(pFile);
398 TRACE("(%p)\n", pFile);
399 return Unknown_AddRef(This);
402 static ULONG WINAPI PersistFile_Release(IPersistFile *pFile)
404 InternetShortcut *This = impl_from_IPersistFile(pFile);
405 TRACE("(%p)\n", pFile);
406 return Unknown_Release(This);
409 static HRESULT WINAPI PersistFile_GetClassID(IPersistFile *pFile, CLSID *pClassID)
411 TRACE("(%p, %p)\n", pFile, pClassID);
412 *pClassID = CLSID_InternetShortcut;
413 return S_OK;
416 static HRESULT WINAPI PersistFile_IsDirty(IPersistFile *pFile)
418 InternetShortcut *This = impl_from_IPersistFile(pFile);
419 TRACE("(%p)\n", pFile);
420 return This->isDirty ? S_OK : S_FALSE;
423 /* A helper function: Allocate and fill rString. Return number of bytes read. */
424 static DWORD get_profile_string(LPCWSTR lpAppName, LPCWSTR lpKeyName,
425 LPCWSTR lpFileName, WCHAR **rString )
427 DWORD r = 0;
428 DWORD len = 128;
429 WCHAR *buffer;
431 buffer = CoTaskMemAlloc(len * sizeof(*buffer));
432 if (buffer != NULL)
434 r = GetPrivateProfileStringW(lpAppName, lpKeyName, NULL, buffer, len, lpFileName);
435 while (r == len-1)
437 WCHAR *realloc_buf;
439 len *= 2;
440 realloc_buf = CoTaskMemRealloc(buffer, len * sizeof(*buffer));
441 if (realloc_buf == NULL)
443 CoTaskMemFree(buffer);
444 *rString = NULL;
445 return 0;
447 buffer = realloc_buf;
449 r = GetPrivateProfileStringW(lpAppName, lpKeyName, NULL, buffer, len, lpFileName);
453 *rString = buffer;
454 return r;
457 static HRESULT WINAPI PersistFile_Load(IPersistFile *pFile, LPCOLESTR pszFileName, DWORD dwMode)
459 WCHAR str_header[] = {'I','n','t','e','r','n','e','t','S','h','o','r','t','c','u','t',0};
460 WCHAR str_URL[] = {'U','R','L',0};
461 WCHAR str_iconfile[] = {'i','c','o','n','f','i','l','e',0};
462 WCHAR str_iconindex[] = {'i','c','o','n','i','n','d','e','x',0};
463 WCHAR *filename = NULL;
464 HRESULT hr;
465 InternetShortcut *This = impl_from_IPersistFile(pFile);
466 TRACE("(%p, %s, 0x%x)\n", pFile, debugstr_w(pszFileName), dwMode);
467 if (dwMode != 0)
468 FIXME("ignoring unimplemented mode 0x%x\n", dwMode);
469 filename = co_strdupW(pszFileName);
470 if (filename != NULL)
472 DWORD r;
473 WCHAR *url;
475 r = get_profile_string(str_header, str_URL, pszFileName, &url);
477 if (url == NULL)
479 hr = E_OUTOFMEMORY;
480 CoTaskMemFree(filename);
482 else if (r == 0)
484 hr = E_FAIL;
485 CoTaskMemFree(filename);
487 else
489 hr = S_OK;
490 CoTaskMemFree(This->currentFile);
491 This->currentFile = filename;
492 CoTaskMemFree(This->url);
493 This->url = url;
494 This->isDirty = FALSE;
497 /* Now we're going to read in the iconfile and iconindex.
498 If we don't find them, that's not a failure case -- it's possible
499 that they just aren't in there. */
500 if (SUCCEEDED(hr))
502 IPropertyStorage *pPropStg;
503 WCHAR *iconfile;
504 WCHAR *iconindexstring;
505 hr = IPropertySetStorage_Open(This->property_set_storage, &FMTID_Intshcut,
506 STGM_READWRITE | STGM_SHARE_EXCLUSIVE,
507 &pPropStg);
509 get_profile_string(str_header, str_iconfile, pszFileName, &iconfile);
510 if (iconfile != NULL)
512 PROPSPEC ps;
513 PROPVARIANT pv;
514 ps.ulKind = PRSPEC_PROPID;
515 ps.u.propid = PID_IS_ICONFILE;
516 pv.vt = VT_LPWSTR;
517 pv.u.pwszVal = iconfile;
518 hr = IPropertyStorage_WriteMultiple(pPropStg, 1, &ps, &pv, 0);
519 if (FAILED(hr))
521 TRACE("Failed to store the iconfile to our property storage. hr = 0x%x\n", hr);
524 CoTaskMemFree(iconfile);
527 get_profile_string(str_header, str_iconindex, pszFileName, &iconindexstring);
529 if (iconindexstring != NULL)
531 int iconindex;
532 PROPSPEC ps;
533 PROPVARIANT pv;
534 char *iconindexastring = co_strdupWtoA(iconindexstring);
535 sscanf(iconindexastring, "%d", &iconindex);
536 CoTaskMemFree(iconindexastring);
537 ps.ulKind = PRSPEC_PROPID;
538 ps.u.propid = PID_IS_ICONINDEX;
539 pv.vt = VT_I4;
540 pv.u.iVal = iconindex;
541 hr = IPropertyStorage_WriteMultiple(pPropStg, 1, &ps, &pv, 0);
542 if (FAILED(hr))
544 TRACE("Failed to store the iconindex to our property storage. hr = 0x%x\n", hr);
547 CoTaskMemFree(iconindexstring);
550 IPropertyStorage_Release(pPropStg);
552 else
553 hr = E_OUTOFMEMORY;
555 else
556 hr = E_OUTOFMEMORY;
557 return hr;
560 static HRESULT WINAPI PersistFile_Save(IPersistFile *pFile, LPCOLESTR pszFileName, BOOL fRemember)
562 HRESULT hr = S_OK;
563 INT len;
564 CHAR *url;
565 InternetShortcut *This = impl_from_IPersistFile(pFile);
567 TRACE("(%p, %s, %d)\n", pFile, debugstr_w(pszFileName), fRemember);
569 if (pszFileName != NULL && fRemember)
571 LPOLESTR oldFile = This->currentFile;
572 This->currentFile = co_strdupW(pszFileName);
573 if (This->currentFile == NULL)
575 This->currentFile = oldFile;
576 return E_OUTOFMEMORY;
578 CoTaskMemFree(oldFile);
580 if (This->url == NULL)
581 return E_FAIL;
583 /* Windows seems to always write:
584 * ASCII "[InternetShortcut]" headers
585 * ASCII names in "name=value" pairs
586 * An ASCII (probably UTF8?) value in "URL=..."
588 len = WideCharToMultiByte(CP_UTF8, 0, This->url, -1, NULL, 0, 0, 0);
589 url = heap_alloc(len);
590 if (url != NULL)
592 HANDLE file;
593 WideCharToMultiByte(CP_UTF8, 0, This->url, -1, url, len, 0, 0);
594 file = CreateFileW(pszFileName, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
595 if (file != INVALID_HANDLE_VALUE)
597 DWORD bytesWritten;
598 char *iconfile;
599 char str_header[] = "[InternetShortcut]";
600 char str_URL[] = "URL=";
601 char str_ICONFILE[] = "ICONFILE=";
602 char str_eol[] = "\r\n";
603 IPropertyStorage *pPropStgRead;
604 PROPSPEC ps[2];
605 PROPVARIANT pvread[2];
606 ps[0].ulKind = PRSPEC_PROPID;
607 ps[0].u.propid = PID_IS_ICONFILE;
608 ps[1].ulKind = PRSPEC_PROPID;
609 ps[1].u.propid = PID_IS_ICONINDEX;
611 WriteFile(file, str_header, lstrlenA(str_header), &bytesWritten, NULL);
612 WriteFile(file, str_eol, lstrlenA(str_eol), &bytesWritten, NULL);
613 WriteFile(file, str_URL, lstrlenA(str_URL), &bytesWritten, NULL);
614 WriteFile(file, url, lstrlenA(url), &bytesWritten, NULL);
615 WriteFile(file, str_eol, lstrlenA(str_eol), &bytesWritten, NULL);
617 hr = IPropertySetStorage_Open(This->property_set_storage, &FMTID_Intshcut, STGM_READ|STGM_SHARE_EXCLUSIVE, &pPropStgRead);
618 if (SUCCEEDED(hr))
620 hr = IPropertyStorage_ReadMultiple(pPropStgRead, 2, ps, pvread);
621 if (hr == S_FALSE)
623 /* None of the properties are present, that's ok */
624 hr = S_OK;
625 IPropertyStorage_Release(pPropStgRead);
627 else if (SUCCEEDED(hr))
629 char indexString[50];
630 len = WideCharToMultiByte(CP_UTF8, 0, pvread[0].u.pwszVal, -1, NULL, 0, 0, 0);
631 iconfile = heap_alloc(len);
632 if (iconfile != NULL)
634 WideCharToMultiByte(CP_UTF8, 0, pvread[0].u.pwszVal, -1, iconfile, len, 0, 0);
635 WriteFile(file, str_ICONFILE, lstrlenA(str_ICONFILE), &bytesWritten, NULL);
636 WriteFile(file, iconfile, lstrlenA(iconfile), &bytesWritten, NULL);
637 WriteFile(file, str_eol, lstrlenA(str_eol), &bytesWritten, NULL);
640 sprintf(indexString, "ICONINDEX=%d", pvread[1].u.iVal);
641 WriteFile(file, indexString, lstrlenA(indexString), &bytesWritten, NULL);
642 WriteFile(file, str_eol, lstrlenA(str_eol), &bytesWritten, NULL);
644 IPropertyStorage_Release(pPropStgRead);
645 PropVariantClear(&pvread[0]);
646 PropVariantClear(&pvread[1]);
648 else
650 TRACE("Unable to read properties.\n");
653 else
655 TRACE("Unable to get the IPropertyStorage.\n");
658 CloseHandle(file);
659 if (pszFileName == NULL || fRemember)
660 This->isDirty = FALSE;
661 StartLinkProcessor(pszFileName);
663 else
664 hr = E_FAIL;
665 heap_free(url);
667 else
668 hr = E_OUTOFMEMORY;
670 return hr;
673 static HRESULT WINAPI PersistFile_SaveCompleted(IPersistFile *pFile, LPCOLESTR pszFileName)
675 FIXME("(%p, %p): stub\n", pFile, pszFileName);
676 return E_NOTIMPL;
679 static HRESULT WINAPI PersistFile_GetCurFile(IPersistFile *pFile, LPOLESTR *ppszFileName)
681 HRESULT hr = S_OK;
682 InternetShortcut *This = impl_from_IPersistFile(pFile);
683 TRACE("(%p, %p)\n", pFile, ppszFileName);
684 if (This->currentFile == NULL)
685 *ppszFileName = NULL;
686 else
688 *ppszFileName = co_strdupW(This->currentFile);
689 if (*ppszFileName == NULL)
690 hr = E_OUTOFMEMORY;
692 return hr;
695 static HRESULT WINAPI PropertySetStorage_QueryInterface(IPropertySetStorage *iface, REFIID riid, PVOID *ppvObject)
697 InternetShortcut *This = impl_from_IPropertySetStorage(iface);
698 TRACE("(%p)\n", iface);
699 return Unknown_QueryInterface(This, riid, ppvObject);
702 static ULONG WINAPI PropertySetStorage_AddRef(IPropertySetStorage *iface)
704 InternetShortcut *This = impl_from_IPropertySetStorage(iface);
705 TRACE("(%p)\n", iface);
706 return Unknown_AddRef(This);
709 static ULONG WINAPI PropertySetStorage_Release(IPropertySetStorage *iface)
711 InternetShortcut *This = impl_from_IPropertySetStorage(iface);
712 TRACE("(%p)\n", iface);
713 return Unknown_Release(This);
716 static HRESULT WINAPI PropertySetStorage_Create(
717 IPropertySetStorage* iface,
718 REFFMTID rfmtid,
719 const CLSID *pclsid,
720 DWORD grfFlags,
721 DWORD grfMode,
722 IPropertyStorage **ppprstg)
724 InternetShortcut *This = impl_from_IPropertySetStorage(iface);
725 TRACE("(%s, %p, 0x%x, 0x%x, %p)\n", debugstr_guid(rfmtid), pclsid, grfFlags, grfMode, ppprstg);
727 return IPropertySetStorage_Create(This->property_set_storage,
728 rfmtid,
729 pclsid,
730 grfFlags,
731 grfMode,
732 ppprstg);
735 static HRESULT WINAPI PropertySetStorage_Open(
736 IPropertySetStorage* iface,
737 REFFMTID rfmtid,
738 DWORD grfMode,
739 IPropertyStorage **ppprstg)
741 InternetShortcut *This = impl_from_IPropertySetStorage(iface);
742 TRACE("(%s, 0x%x, %p)\n", debugstr_guid(rfmtid), grfMode, ppprstg);
744 /* Note: The |STGM_SHARE_EXCLUSIVE is to cope with a bug in the implementation. Should be fixed in ole32. */
745 return IPropertySetStorage_Open(This->property_set_storage,
746 rfmtid,
747 grfMode|STGM_SHARE_EXCLUSIVE,
748 ppprstg);
751 static HRESULT WINAPI PropertySetStorage_Delete(IPropertySetStorage *iface, REFFMTID rfmtid)
753 InternetShortcut *This = impl_from_IPropertySetStorage(iface);
754 TRACE("(%s)\n", debugstr_guid(rfmtid));
757 return IPropertySetStorage_Delete(This->property_set_storage,
758 rfmtid);
761 static HRESULT WINAPI PropertySetStorage_Enum(IPropertySetStorage *iface, IEnumSTATPROPSETSTG **ppenum)
763 FIXME("(%p): stub\n", ppenum);
764 return E_NOTIMPL;
767 static const IUniformResourceLocatorWVtbl uniformResourceLocatorWVtbl = {
768 UniformResourceLocatorW_QueryInterface,
769 UniformResourceLocatorW_AddRef,
770 UniformResourceLocatorW_Release,
771 UniformResourceLocatorW_SetUrl,
772 UniformResourceLocatorW_GetUrl,
773 UniformResourceLocatorW_InvokeCommand
776 static const IUniformResourceLocatorAVtbl uniformResourceLocatorAVtbl = {
777 UniformResourceLocatorA_QueryInterface,
778 UniformResourceLocatorA_AddRef,
779 UniformResourceLocatorA_Release,
780 UniformResourceLocatorA_SetUrl,
781 UniformResourceLocatorA_GetUrl,
782 UniformResourceLocatorA_InvokeCommand
785 static const IPersistFileVtbl persistFileVtbl = {
786 PersistFile_QueryInterface,
787 PersistFile_AddRef,
788 PersistFile_Release,
789 PersistFile_GetClassID,
790 PersistFile_IsDirty,
791 PersistFile_Load,
792 PersistFile_Save,
793 PersistFile_SaveCompleted,
794 PersistFile_GetCurFile
797 static const IPropertySetStorageVtbl propertySetStorageVtbl = {
798 PropertySetStorage_QueryInterface,
799 PropertySetStorage_AddRef,
800 PropertySetStorage_Release,
801 PropertySetStorage_Create,
802 PropertySetStorage_Open,
803 PropertySetStorage_Delete,
804 PropertySetStorage_Enum
807 static InternetShortcut *create_shortcut(void)
809 InternetShortcut *newshortcut;
811 newshortcut = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(InternetShortcut));
812 if (newshortcut)
814 HRESULT hr;
815 IPropertyStorage *dummy;
817 newshortcut->IUniformResourceLocatorA_iface.lpVtbl = &uniformResourceLocatorAVtbl;
818 newshortcut->IUniformResourceLocatorW_iface.lpVtbl = &uniformResourceLocatorWVtbl;
819 newshortcut->IPersistFile_iface.lpVtbl = &persistFileVtbl;
820 newshortcut->IPropertySetStorage_iface.lpVtbl = &propertySetStorageVtbl;
821 newshortcut->refCount = 1;
822 hr = StgCreateStorageEx(NULL, STGM_CREATE | STGM_READWRITE | STGM_SHARE_EXCLUSIVE | STGM_DELETEONRELEASE,
823 STGFMT_STORAGE, 0, NULL, NULL, &IID_IPropertySetStorage, (void **) &newshortcut->property_set_storage);
824 if (FAILED(hr))
826 TRACE("Failed to create the storage object needed for the shortcut.\n");
827 heap_free(newshortcut);
828 return NULL;
831 hr = IPropertySetStorage_Create(newshortcut->property_set_storage, &FMTID_Intshcut, NULL, PROPSETFLAG_DEFAULT, STGM_CREATE | STGM_READWRITE | STGM_SHARE_EXCLUSIVE, &dummy);
832 if (FAILED(hr))
834 TRACE("Failed to create the property object needed for the shortcut.\n");
835 IPropertySetStorage_Release(newshortcut->property_set_storage);
836 heap_free(newshortcut);
837 return NULL;
839 IPropertyStorage_Release(dummy);
842 return newshortcut;
845 HRESULT WINAPI InternetShortcut_Create(IClassFactory *iface, IUnknown *outer, REFIID riid, void **ppv)
847 InternetShortcut *This;
848 HRESULT hres;
850 TRACE("(%p, %s, %p)\n", outer, debugstr_guid(riid), ppv);
852 *ppv = NULL;
854 if(outer)
855 return CLASS_E_NOAGGREGATION;
857 This = create_shortcut();
858 if(!This)
859 return E_OUTOFMEMORY;
861 hres = Unknown_QueryInterface(This, riid, ppv);
862 Unknown_Release(This);
863 return hres;
867 /**********************************************************************
868 * OpenURL (ieframe.@)
870 void WINAPI OpenURL(HWND hWnd, HINSTANCE hInst, LPCSTR lpcstrUrl, int nShowCmd)
872 InternetShortcut *shortcut;
873 WCHAR* urlfilepath = NULL;
874 int len;
876 shortcut = create_shortcut();
878 if(!shortcut)
879 return;
881 len = MultiByteToWideChar(CP_ACP, 0, lpcstrUrl, -1, NULL, 0);
882 urlfilepath = heap_alloc(len * sizeof(WCHAR));
883 MultiByteToWideChar(CP_ACP, 0, lpcstrUrl, -1, urlfilepath, len);
885 if(SUCCEEDED(IPersistFile_Load(&shortcut->IPersistFile_iface, urlfilepath, 0))) {
886 URLINVOKECOMMANDINFOW ici;
888 memset( &ici, 0, sizeof ici );
889 ici.dwcbSize = sizeof ici;
890 ici.dwFlags = IURL_INVOKECOMMAND_FL_USE_DEFAULT_VERB;
891 ici.hwndParent = hWnd;
893 if(FAILED(UniformResourceLocatorW_InvokeCommand(&shortcut->IUniformResourceLocatorW_iface, (PURLINVOKECOMMANDINFOW) &ici)))
894 TRACE("failed to open URL: %s\n", debugstr_a(lpcstrUrl));
897 heap_free(urlfilepath);
898 Unknown_Release(shortcut);