windows.networking.hostname/tests: Add IHostNameFactory::CreateHostName() tests.
[wine.git] / dlls / shell32 / shelllink.c
blob24819fb90fb40b36682525ccfe4b09aa3cc68298
1 /*
3 * Copyright 1997 Marcus Meissner
4 * Copyright 1998 Juergen Schmied
5 * Copyright 2005 Mike McCormack
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 * NOTES
22 * Nearly complete information about the binary formats
23 * of .lnk files available at http://www.wotsit.org
25 * You can use winedump to examine the contents of a link file:
26 * winedump lnk sc.lnk
28 * MSI advertised shortcuts are totally undocumented. They provide an
29 * icon for a program that is not yet installed, and invoke MSI to
30 * install the program when the shortcut is clicked on. They are
31 * created by passing a special string to SetPath, and the information
32 * in that string is parsed an stored.
35 #define COBJMACROS
36 #include "wine/debug.h"
37 #include "winerror.h"
38 #include "windef.h"
39 #include "winbase.h"
40 #include "winnls.h"
41 #include "winreg.h"
43 #include "winuser.h"
44 #include "wingdi.h"
45 #include "shlobj.h"
47 #include "pidl.h"
48 #include "shell32_main.h"
49 #include "shlguid.h"
50 #include "shlwapi.h"
51 #include "msi.h"
52 #include "appmgmt.h"
54 #include "initguid.h"
56 WINE_DEFAULT_DEBUG_CHANNEL(shell);
58 DEFINE_GUID( SHELL32_AdvtShortcutProduct,
59 0x9db1186f,0x40df,0x11d1,0xaa,0x8c,0x00,0xc0,0x4f,0xb6,0x78,0x63);
60 DEFINE_GUID( SHELL32_AdvtShortcutComponent,
61 0x9db1186e,0x40df,0x11d1,0xaa,0x8c,0x00,0xc0,0x4f,0xb6,0x78,0x63);
63 /* link file formats */
65 #include "pshpack1.h"
67 typedef struct _LINK_HEADER
69 DWORD dwSize; /* 0x00 size of the header - 0x4c */
70 GUID MagicGuid; /* 0x04 is CLSID_ShellLink */
71 DWORD dwFlags; /* 0x14 describes elements following */
72 DWORD dwFileAttr; /* 0x18 attributes of the target file */
73 FILETIME CreationTime; /* 0x1c creation time of target file */
74 FILETIME AccessTime; /* 0x24 access time of target file */
75 FILETIME WriteTime; /* 0x2c write time of target file */
76 DWORD dwFileSize; /* 0x34 File size of target file */
77 DWORD nIcon; /* 0x38 icon number or index */
78 DWORD fStartup; /* 0x3c startup type or window state of application */
79 WORD wHotKey; /* 0x40 hotkey */
80 WORD Reserved1; /* 0x42 reserved = 0 */
81 DWORD Reserved2; /* 0x44 reserved = 0 */
82 DWORD Reserved3; /* 0x48 reserved = 0 */
83 } LINK_HEADER, * PLINK_HEADER;
85 #define SHLINK_LOCAL 0
86 #define SHLINK_REMOTE 1
88 typedef struct _LOCATION_INFO
90 DWORD dwTotalSize;
91 DWORD dwHeaderSize;
92 DWORD dwFlags;
93 DWORD dwVolTableOfs;
94 DWORD dwLocalPathOfs;
95 DWORD dwNetworkVolTableOfs;
96 DWORD dwFinalPathOfs;
97 } LOCATION_INFO;
99 typedef struct _LOCAL_VOLUME_INFO
101 DWORD dwSize;
102 DWORD dwType;
103 DWORD dwVolSerial;
104 DWORD dwVolLabelOfs;
105 } LOCAL_VOLUME_INFO;
107 typedef struct volume_info_t
109 DWORD type;
110 DWORD serial;
111 WCHAR label[12]; /* assume 8.3 */
112 } volume_info;
114 #include "poppack.h"
116 /* IShellLink Implementation */
118 typedef struct
120 IShellLinkA IShellLinkA_iface;
121 IShellLinkW IShellLinkW_iface;
122 IPersistFile IPersistFile_iface;
123 IPersistStream IPersistStream_iface;
124 IShellLinkDataList IShellLinkDataList_iface;
125 IShellExtInit IShellExtInit_iface;
126 IContextMenu IContextMenu_iface;
127 IObjectWithSite IObjectWithSite_iface;
128 IPropertyStore IPropertyStore_iface;
130 LONG ref;
132 /* data structures according to the information in the link */
133 LPITEMIDLIST pPidl;
134 WORD wHotKey;
135 SYSTEMTIME CreationTime;
136 SYSTEMTIME AccessTime;
137 SYSTEMTIME WriteTime;
139 DWORD iShowCmd;
140 LPWSTR sIcoPath;
141 INT iIcoNdx;
142 LPWSTR sPath;
143 LPWSTR sArgs;
144 LPWSTR sWorkDir;
145 LPWSTR sDescription;
146 LPWSTR sPathRel;
147 LPWSTR sProduct;
148 LPWSTR sComponent;
149 volume_info volume;
151 BOOL bDirty;
152 INT iIdOpen; /* id of the "Open" entry in the context menu */
153 IUnknown *site;
155 LPOLESTR filepath; /* file path returned by IPersistFile::GetCurFile */
156 } IShellLinkImpl;
158 static inline IShellLinkImpl *impl_from_IShellLinkA(IShellLinkA *iface)
160 return CONTAINING_RECORD(iface, IShellLinkImpl, IShellLinkA_iface);
163 static inline IShellLinkImpl *impl_from_IShellLinkW(IShellLinkW *iface)
165 return CONTAINING_RECORD(iface, IShellLinkImpl, IShellLinkW_iface);
168 static inline IShellLinkImpl *impl_from_IPersistFile(IPersistFile *iface)
170 return CONTAINING_RECORD(iface, IShellLinkImpl, IPersistFile_iface);
173 static inline IShellLinkImpl *impl_from_IPersistStream(IPersistStream *iface)
175 return CONTAINING_RECORD(iface, IShellLinkImpl, IPersistStream_iface);
178 static inline IShellLinkImpl *impl_from_IShellLinkDataList(IShellLinkDataList *iface)
180 return CONTAINING_RECORD(iface, IShellLinkImpl, IShellLinkDataList_iface);
183 static inline IShellLinkImpl *impl_from_IShellExtInit(IShellExtInit *iface)
185 return CONTAINING_RECORD(iface, IShellLinkImpl, IShellExtInit_iface);
188 static inline IShellLinkImpl *impl_from_IContextMenu(IContextMenu *iface)
190 return CONTAINING_RECORD(iface, IShellLinkImpl, IContextMenu_iface);
193 static inline IShellLinkImpl *impl_from_IObjectWithSite(IObjectWithSite *iface)
195 return CONTAINING_RECORD(iface, IShellLinkImpl, IObjectWithSite_iface);
198 static inline IShellLinkImpl *impl_from_IPropertyStore(IPropertyStore *iface)
200 return CONTAINING_RECORD(iface, IShellLinkImpl, IPropertyStore_iface);
203 static HRESULT ShellLink_UpdatePath(LPCWSTR sPathRel, LPCWSTR path, LPCWSTR sWorkDir, LPWSTR* psPath);
205 /* strdup on the process heap */
206 static inline LPWSTR heap_strdupAtoW( LPCSTR str)
208 INT len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
209 WCHAR *p = malloc( len * sizeof(WCHAR) );
210 if( !p )
211 return p;
212 MultiByteToWideChar( CP_ACP, 0, str, -1, p, len );
213 return p;
216 /**************************************************************************
217 * IPersistFile_QueryInterface
219 static HRESULT WINAPI IPersistFile_fnQueryInterface(
220 IPersistFile* iface,
221 REFIID riid,
222 LPVOID *ppvObj)
224 IShellLinkImpl *This = impl_from_IPersistFile(iface);
225 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObj);
228 /******************************************************************************
229 * IPersistFile_AddRef
231 static ULONG WINAPI IPersistFile_fnAddRef(IPersistFile* iface)
233 IShellLinkImpl *This = impl_from_IPersistFile(iface);
234 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
237 /******************************************************************************
238 * IPersistFile_Release
240 static ULONG WINAPI IPersistFile_fnRelease(IPersistFile* iface)
242 IShellLinkImpl *This = impl_from_IPersistFile(iface);
243 return IShellLinkW_Release(&This->IShellLinkW_iface);
246 static HRESULT WINAPI IPersistFile_fnGetClassID(IPersistFile* iface, CLSID *pClassID)
248 IShellLinkImpl *This = impl_from_IPersistFile(iface);
250 TRACE("(%p)->(%p)\n", This, pClassID);
252 *pClassID = CLSID_ShellLink;
254 return S_OK;
257 static HRESULT WINAPI IPersistFile_fnIsDirty(IPersistFile* iface)
259 IShellLinkImpl *This = impl_from_IPersistFile(iface);
261 TRACE("(%p)\n",This);
263 if (This->bDirty)
264 return S_OK;
266 return S_FALSE;
269 static HRESULT WINAPI IPersistFile_fnLoad(IPersistFile* iface, LPCOLESTR pszFileName, DWORD dwMode)
271 IShellLinkImpl *This = impl_from_IPersistFile(iface);
272 IPersistStream *StreamThis = &This->IPersistStream_iface;
273 HRESULT r;
274 IStream *stm;
276 TRACE("(%p, %s, %lx)\n",This, debugstr_w(pszFileName), dwMode);
278 if( dwMode == 0 ) dwMode = STGM_READ;
279 r = SHCreateStreamOnFileW(pszFileName, dwMode, &stm);
280 if( SUCCEEDED( r ) )
282 r = IPersistStream_Load(StreamThis, stm);
283 ShellLink_UpdatePath(This->sPathRel, pszFileName, This->sWorkDir, &This->sPath);
284 IStream_Release( stm );
286 /* update file path */
287 free(This->filepath);
288 This->filepath = wcsdup(pszFileName);
290 This->bDirty = FALSE;
292 TRACE("-- returning hr %08lx\n", r);
293 return r;
296 BOOL run_winemenubuilder( const WCHAR *args )
298 LONG len;
299 LPWSTR buffer;
300 STARTUPINFOW si;
301 PROCESS_INFORMATION pi;
302 BOOL ret;
303 WCHAR app[MAX_PATH];
304 void *redir;
306 GetSystemDirectoryW( app, MAX_PATH );
307 lstrcatW( app, L"\\winemenubuilder.exe" );
309 len = (lstrlenW( app ) + lstrlenW( args ) + 1) * sizeof(WCHAR);
310 buffer = malloc( len );
311 if( !buffer )
312 return FALSE;
314 lstrcpyW( buffer, app );
315 lstrcatW( buffer, args );
317 TRACE("starting %s\n",debugstr_w(buffer));
319 memset(&si, 0, sizeof(si));
320 si.cb = sizeof(si);
322 Wow64DisableWow64FsRedirection( &redir );
323 ret = CreateProcessW( app, buffer, NULL, NULL, FALSE, DETACHED_PROCESS, NULL, NULL, &si, &pi );
324 Wow64RevertWow64FsRedirection( redir );
326 free( buffer );
328 if (ret)
330 CloseHandle( pi.hProcess );
331 CloseHandle( pi.hThread );
334 return ret;
337 static BOOL StartLinkProcessor( LPCOLESTR szLink )
339 LONG len;
340 LPWSTR buffer;
341 BOOL ret;
343 len = (lstrlenW( szLink ) + 7) * sizeof(WCHAR);
344 buffer = malloc( len );
345 if( !buffer )
346 return FALSE;
348 swprintf( buffer, len, L" -w \"%s\"", szLink );
349 ret = run_winemenubuilder( buffer );
350 free( buffer );
351 return ret;
354 static HRESULT WINAPI IPersistFile_fnSave(IPersistFile* iface, LPCOLESTR pszFileName, BOOL fRemember)
356 IShellLinkImpl *This = impl_from_IPersistFile(iface);
357 IPersistStream *StreamThis = &This->IPersistStream_iface;
358 HRESULT r;
359 IStream *stm;
361 TRACE("(%p)->(%s)\n",This,debugstr_w(pszFileName));
363 if (!pszFileName)
365 if (!This->filepath) return S_OK;
367 pszFileName = This->filepath;
368 fRemember = FALSE;
371 r = SHCreateStreamOnFileW( pszFileName, STGM_READWRITE | STGM_CREATE | STGM_SHARE_DENY_WRITE, &stm );
372 if( SUCCEEDED( r ) )
374 r = IPersistStream_Save(StreamThis, stm, FALSE);
375 IStream_Release( stm );
377 if( SUCCEEDED( r ) )
379 StartLinkProcessor( pszFileName );
381 if (fRemember)
383 /* update file path */
384 free(This->filepath);
385 This->filepath = wcsdup(pszFileName);
388 This->bDirty = FALSE;
390 else
392 DeleteFileW( pszFileName );
393 WARN("Failed to create shortcut %s\n", debugstr_w(pszFileName) );
397 return r;
400 static HRESULT WINAPI IPersistFile_fnSaveCompleted(IPersistFile* iface, LPCOLESTR filename)
402 IShellLinkImpl *This = impl_from_IPersistFile(iface);
403 FIXME("(%p)->(%s): stub\n", This, debugstr_w(filename));
404 return S_OK;
407 static HRESULT WINAPI IPersistFile_fnGetCurFile(IPersistFile* iface, LPOLESTR *filename)
409 IShellLinkImpl *This = impl_from_IPersistFile(iface);
411 TRACE("(%p)->(%p)\n", This, filename);
413 if (!This->filepath)
415 *filename = NULL;
416 return S_FALSE;
419 *filename = CoTaskMemAlloc((lstrlenW(This->filepath) + 1) * sizeof(WCHAR));
420 if (!*filename) return E_OUTOFMEMORY;
422 lstrcpyW(*filename, This->filepath);
424 return S_OK;
427 static const IPersistFileVtbl pfvt =
429 IPersistFile_fnQueryInterface,
430 IPersistFile_fnAddRef,
431 IPersistFile_fnRelease,
432 IPersistFile_fnGetClassID,
433 IPersistFile_fnIsDirty,
434 IPersistFile_fnLoad,
435 IPersistFile_fnSave,
436 IPersistFile_fnSaveCompleted,
437 IPersistFile_fnGetCurFile
440 /************************************************************************
441 * IPersistStream_QueryInterface
443 static HRESULT WINAPI IPersistStream_fnQueryInterface(
444 IPersistStream* iface,
445 REFIID riid,
446 VOID** ppvObj)
448 IShellLinkImpl *This = impl_from_IPersistStream(iface);
449 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObj);
452 /************************************************************************
453 * IPersistStream_Release
455 static ULONG WINAPI IPersistStream_fnRelease(
456 IPersistStream* iface)
458 IShellLinkImpl *This = impl_from_IPersistStream(iface);
459 return IShellLinkW_Release(&This->IShellLinkW_iface);
462 /************************************************************************
463 * IPersistStream_AddRef
465 static ULONG WINAPI IPersistStream_fnAddRef(
466 IPersistStream* iface)
468 IShellLinkImpl *This = impl_from_IPersistStream(iface);
469 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
472 /************************************************************************
473 * IPersistStream_GetClassID
476 static HRESULT WINAPI IPersistStream_fnGetClassID(
477 IPersistStream* iface,
478 CLSID* pClassID)
480 IShellLinkImpl *This = impl_from_IPersistStream(iface);
481 return IPersistFile_GetClassID(&This->IPersistFile_iface, pClassID);
484 /************************************************************************
485 * IPersistStream_IsDirty (IPersistStream)
487 static HRESULT WINAPI IPersistStream_fnIsDirty(
488 IPersistStream* iface)
490 IShellLinkImpl *This = impl_from_IPersistStream(iface);
492 TRACE("(%p)\n", This);
494 return S_OK;
498 static HRESULT Stream_LoadString( IStream* stm, BOOL unicode, LPWSTR *pstr )
500 DWORD count;
501 USHORT len;
502 LPVOID temp;
503 LPWSTR str;
504 HRESULT r;
506 TRACE("%p\n", stm);
508 count = 0;
509 r = IStream_Read(stm, &len, sizeof(len), &count);
510 if ( FAILED (r) || ( count != sizeof(len) ) )
511 return E_FAIL;
513 if( unicode )
514 len *= sizeof (WCHAR);
516 TRACE("reading %d\n", len);
517 temp = malloc(len + sizeof(WCHAR));
518 if( !temp )
519 return E_OUTOFMEMORY;
520 count = 0;
521 r = IStream_Read(stm, temp, len, &count);
522 if( FAILED (r) || ( count != len ) )
524 free( temp );
525 return E_FAIL;
528 TRACE("read %s\n", debugstr_an(temp,len));
530 /* convert to unicode if necessary */
531 if( !unicode )
533 count = MultiByteToWideChar( CP_ACP, 0, temp, len, NULL, 0 );
534 str = malloc( (count + 1) * sizeof(WCHAR) );
535 if( !str )
537 free( temp );
538 return E_OUTOFMEMORY;
540 MultiByteToWideChar( CP_ACP, 0, temp, len, str, count );
541 free( temp );
543 else
545 count /= 2;
546 str = temp;
548 str[count] = 0;
550 *pstr = str;
552 return S_OK;
555 static HRESULT Stream_ReadChunk( IStream* stm, LPVOID *data )
557 DWORD size;
558 ULONG count;
559 HRESULT r;
560 struct sized_chunk {
561 DWORD size;
562 unsigned char data[1];
563 } *chunk;
565 TRACE("%p\n",stm);
567 r = IStream_Read( stm, &size, sizeof(size), &count );
568 if( FAILED( r ) || count != sizeof(size) )
569 return E_FAIL;
571 chunk = malloc( size );
572 if( !chunk )
573 return E_OUTOFMEMORY;
575 chunk->size = size;
576 r = IStream_Read( stm, chunk->data, size - sizeof(size), &count );
577 if( FAILED( r ) || count != (size - sizeof(size)) )
579 free( chunk );
580 return E_FAIL;
583 TRACE("Read %ld bytes\n",chunk->size);
585 *data = chunk;
587 return S_OK;
590 static BOOL Stream_LoadVolume( LOCAL_VOLUME_INFO *vol, volume_info *volume )
592 const int label_sz = ARRAY_SIZE(volume->label);
593 LPSTR label;
594 int len;
596 volume->serial = vol->dwVolSerial;
597 volume->type = vol->dwType;
599 if( !vol->dwVolLabelOfs )
600 return FALSE;
601 if( vol->dwSize <= vol->dwVolLabelOfs )
602 return FALSE;
603 len = vol->dwSize - vol->dwVolLabelOfs;
605 label = (LPSTR) vol;
606 label += vol->dwVolLabelOfs;
607 MultiByteToWideChar( CP_ACP, 0, label, len, volume->label, label_sz-1);
609 return TRUE;
612 static LPWSTR Stream_LoadPath( LPCSTR p, DWORD maxlen )
614 int len = 0, wlen;
615 LPWSTR path;
617 while( (len < maxlen) && p[len] )
618 len++;
620 wlen = MultiByteToWideChar(CP_ACP, 0, p, len, NULL, 0);
621 path = malloc((wlen + 1) * sizeof(WCHAR));
622 MultiByteToWideChar(CP_ACP, 0, p, len, path, wlen);
623 path[wlen] = 0;
625 return path;
628 static HRESULT Stream_LoadLocation( IStream *stm,
629 volume_info *volume, LPWSTR *path )
631 char *p = NULL;
632 LOCATION_INFO *loc;
633 HRESULT r;
634 DWORD n;
636 r = Stream_ReadChunk( stm, (LPVOID*) &p );
637 if( FAILED(r) )
638 return r;
640 loc = (LOCATION_INFO*) p;
641 if (loc->dwTotalSize < sizeof(LOCATION_INFO))
643 free( p );
644 return E_FAIL;
647 /* if there's valid local volume information, load it */
648 if( loc->dwVolTableOfs &&
649 ((loc->dwVolTableOfs + sizeof(LOCAL_VOLUME_INFO)) <= loc->dwTotalSize) )
651 LOCAL_VOLUME_INFO *volume_info;
653 volume_info = (LOCAL_VOLUME_INFO*) &p[loc->dwVolTableOfs];
654 Stream_LoadVolume( volume_info, volume );
657 /* if there's a local path, load it */
658 n = loc->dwLocalPathOfs;
659 if( n && (n < loc->dwTotalSize) )
660 *path = Stream_LoadPath( &p[n], loc->dwTotalSize - n );
662 TRACE("type %ld serial %08lx name %s path %s\n", volume->type,
663 volume->serial, debugstr_w(volume->label), debugstr_w(*path));
665 free( p );
666 return S_OK;
670 * The format of the advertised shortcut info seems to be:
672 * Offset Description
673 * ------ -----------
675 * 0 Length of the block (4 bytes, usually 0x314)
676 * 4 tag (dword)
677 * 8 string data in ANSI
678 * 8+0x104 string data in UNICODE
680 * In the original Win32 implementation the buffers are not initialized
681 * to zero, so data trailing the string is random garbage.
683 static HRESULT Stream_LoadAdvertiseInfo( IStream* stm, LPWSTR *str )
685 DWORD size;
686 ULONG count;
687 HRESULT r;
688 EXP_DARWIN_LINK buffer;
690 TRACE("%p\n",stm);
692 r = IStream_Read( stm, &buffer.dbh.cbSize, sizeof (DWORD), &count );
693 if( FAILED( r ) )
694 return r;
696 /* make sure that we read the size of the structure even on error */
697 size = sizeof buffer - sizeof (DWORD);
698 if( buffer.dbh.cbSize != sizeof buffer )
700 ERR("Ooops. This structure is not as expected...\n");
701 return E_FAIL;
704 r = IStream_Read( stm, &buffer.dbh.dwSignature, size, &count );
705 if( FAILED( r ) )
706 return r;
708 if( count != size )
709 return E_FAIL;
711 TRACE("magic %08lx string = %s\n", buffer.dbh.dwSignature, debugstr_w(buffer.szwDarwinID));
713 if( (buffer.dbh.dwSignature&0xffff0000) != 0xa0000000 )
715 ERR("Unknown magic number %08lx in advertised shortcut\n", buffer.dbh.dwSignature);
716 return E_FAIL;
719 *str = malloc( (lstrlenW(buffer.szwDarwinID) + 1) * sizeof(WCHAR) );
720 lstrcpyW( *str, buffer.szwDarwinID );
722 return S_OK;
725 /************************************************************************
726 * IPersistStream_Load (IPersistStream)
728 static HRESULT WINAPI IPersistStream_fnLoad(
729 IPersistStream* iface,
730 IStream* stm)
732 LINK_HEADER hdr;
733 ULONG dwBytesRead;
734 BOOL unicode;
735 HRESULT r;
736 DWORD zero;
738 IShellLinkImpl *This = impl_from_IPersistStream(iface);
740 TRACE("%p %p\n", This, stm);
742 if( !stm )
743 return STG_E_INVALIDPOINTER;
745 dwBytesRead = 0;
746 r = IStream_Read(stm, &hdr, sizeof(hdr), &dwBytesRead);
747 if( FAILED( r ) )
748 return r;
750 if( dwBytesRead != sizeof(hdr))
751 return E_FAIL;
752 if( hdr.dwSize != sizeof(hdr))
753 return E_FAIL;
754 if( !IsEqualIID(&hdr.MagicGuid, &CLSID_ShellLink) )
755 return E_FAIL;
757 /* free all the old stuff */
758 ILFree(This->pPidl);
759 This->pPidl = NULL;
760 memset( &This->volume, 0, sizeof This->volume );
761 free(This->sPath);
762 This->sPath = NULL;
763 free(This->sDescription);
764 This->sDescription = NULL;
765 free(This->sPathRel);
766 This->sPathRel = NULL;
767 free(This->sWorkDir);
768 This->sWorkDir = NULL;
769 free(This->sArgs);
770 This->sArgs = NULL;
771 free(This->sIcoPath);
772 This->sIcoPath = NULL;
773 free(This->sProduct);
774 This->sProduct = NULL;
775 free(This->sComponent);
776 This->sComponent = NULL;
778 This->wHotKey = hdr.wHotKey;
779 This->iIcoNdx = hdr.nIcon;
780 FileTimeToSystemTime (&hdr.CreationTime, &This->CreationTime);
781 FileTimeToSystemTime (&hdr.AccessTime, &This->AccessTime);
782 FileTimeToSystemTime (&hdr.WriteTime, &This->WriteTime);
783 if (TRACE_ON(shell))
785 WCHAR sTemp[MAX_PATH];
786 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &This->CreationTime, NULL, sTemp, ARRAY_SIZE(sTemp));
787 TRACE("-- CreationTime: %s\n", debugstr_w(sTemp) );
788 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &This->AccessTime, NULL, sTemp, ARRAY_SIZE(sTemp));
789 TRACE("-- AccessTime: %s\n", debugstr_w(sTemp) );
790 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &This->WriteTime, NULL, sTemp, ARRAY_SIZE(sTemp));
791 TRACE("-- WriteTime: %s\n", debugstr_w(sTemp) );
794 /* load all the new stuff */
795 if( hdr.dwFlags & SLDF_HAS_ID_LIST )
797 r = ILLoadFromStream( stm, &This->pPidl );
798 if( FAILED( r ) )
799 return r;
801 pdump(This->pPidl);
803 /* load the location information */
804 if( hdr.dwFlags & SLDF_HAS_LINK_INFO )
805 r = Stream_LoadLocation( stm, &This->volume, &This->sPath );
806 if( FAILED( r ) )
807 goto end;
809 unicode = hdr.dwFlags & SLDF_UNICODE;
810 if( hdr.dwFlags & SLDF_HAS_NAME )
812 r = Stream_LoadString( stm, unicode, &This->sDescription );
813 TRACE("Description -> %s\n",debugstr_w(This->sDescription));
815 if( FAILED( r ) )
816 goto end;
818 if( hdr.dwFlags & SLDF_HAS_RELPATH )
820 r = Stream_LoadString( stm, unicode, &This->sPathRel );
821 TRACE("Relative Path-> %s\n",debugstr_w(This->sPathRel));
823 if( FAILED( r ) )
824 goto end;
826 if( hdr.dwFlags & SLDF_HAS_WORKINGDIR )
828 r = Stream_LoadString( stm, unicode, &This->sWorkDir );
829 TRACE("Working Dir -> %s\n",debugstr_w(This->sWorkDir));
831 if( FAILED( r ) )
832 goto end;
834 if( hdr.dwFlags & SLDF_HAS_ARGS )
836 r = Stream_LoadString( stm, unicode, &This->sArgs );
837 TRACE("Working Dir -> %s\n",debugstr_w(This->sArgs));
839 if( FAILED( r ) )
840 goto end;
842 if( hdr.dwFlags & SLDF_HAS_ICONLOCATION )
844 r = Stream_LoadString( stm, unicode, &This->sIcoPath );
845 TRACE("Icon file -> %s\n",debugstr_w(This->sIcoPath));
847 if( FAILED( r ) )
848 goto end;
850 if( hdr.dwFlags & SLDF_HAS_LOGO3ID )
852 r = Stream_LoadAdvertiseInfo( stm, &This->sProduct );
853 TRACE("Product -> %s\n",debugstr_w(This->sProduct));
855 if( FAILED( r ) )
856 goto end;
858 if( hdr.dwFlags & SLDF_HAS_DARWINID )
860 r = Stream_LoadAdvertiseInfo( stm, &This->sComponent );
861 TRACE("Component -> %s\n",debugstr_w(This->sComponent));
863 if( FAILED( r ) )
864 goto end;
866 r = IStream_Read(stm, &zero, sizeof zero, &dwBytesRead);
867 if( FAILED( r ) || zero || dwBytesRead != sizeof zero )
869 /* Some lnk files have extra data blocks starting with a
870 * DATABLOCK_HEADER. For instance EXP_SPECIAL_FOLDER and an unknown
871 * one with a 0xa0000003 signature. However these don't seem to matter
872 * too much.
874 WARN("Last word was not zero\n");
877 TRACE("OK\n");
879 pdump (This->pPidl);
881 return S_OK;
882 end:
883 return r;
886 /************************************************************************
887 * Stream_WriteString
889 * Helper function for IPersistStream_Save. Writes a unicode string
890 * with terminating nul byte to a stream, preceded by the its length.
892 static HRESULT Stream_WriteString( IStream* stm, LPCWSTR str )
894 USHORT len = lstrlenW( str ) + 1;
895 DWORD count;
896 HRESULT r;
898 r = IStream_Write( stm, &len, sizeof(len), &count );
899 if( FAILED( r ) )
900 return r;
902 len *= sizeof(WCHAR);
904 r = IStream_Write( stm, str, len, &count );
905 if( FAILED( r ) )
906 return r;
908 return S_OK;
911 /************************************************************************
912 * Stream_WriteLocationInfo
914 * Writes the location info to a stream
916 * FIXME: One day we might want to write the network volume information
917 * and the final path.
918 * Figure out how Windows deals with unicode paths here.
920 static HRESULT Stream_WriteLocationInfo( IStream* stm, LPCWSTR path,
921 volume_info *volume )
923 DWORD total_size, path_size, volume_info_size, label_size, final_path_size;
924 LOCAL_VOLUME_INFO *vol;
925 LOCATION_INFO *loc;
926 LPSTR szLabel, szPath, szFinalPath;
927 ULONG count = 0;
928 HRESULT hr;
930 TRACE("%p %s %p\n", stm, debugstr_w(path), volume);
932 /* figure out the size of everything */
933 label_size = WideCharToMultiByte( CP_ACP, 0, volume->label, -1,
934 NULL, 0, NULL, NULL );
935 path_size = WideCharToMultiByte( CP_ACP, 0, path, -1,
936 NULL, 0, NULL, NULL );
937 volume_info_size = sizeof *vol + label_size;
938 final_path_size = 1;
939 total_size = sizeof *loc + volume_info_size + path_size + final_path_size;
941 /* create pointers to everything */
942 loc = calloc(1, total_size);
943 vol = (LOCAL_VOLUME_INFO*) &loc[1];
944 szLabel = (LPSTR) &vol[1];
945 szPath = &szLabel[label_size];
946 szFinalPath = &szPath[path_size];
948 /* fill in the location information header */
949 loc->dwTotalSize = total_size;
950 loc->dwHeaderSize = sizeof (*loc);
951 loc->dwFlags = 1;
952 loc->dwVolTableOfs = sizeof (*loc);
953 loc->dwLocalPathOfs = sizeof (*loc) + volume_info_size;
954 loc->dwNetworkVolTableOfs = 0;
955 loc->dwFinalPathOfs = sizeof (*loc) + volume_info_size + path_size;
957 /* fill in the volume information */
958 vol->dwSize = volume_info_size;
959 vol->dwType = volume->type;
960 vol->dwVolSerial = volume->serial;
961 vol->dwVolLabelOfs = sizeof (*vol);
963 /* copy in the strings */
964 WideCharToMultiByte( CP_ACP, 0, volume->label, -1,
965 szLabel, label_size, NULL, NULL );
966 WideCharToMultiByte( CP_ACP, 0, path, -1,
967 szPath, path_size, NULL, NULL );
968 szFinalPath[0] = 0;
970 hr = IStream_Write( stm, loc, total_size, &count );
971 free(loc);
973 return hr;
976 static EXP_DARWIN_LINK* shelllink_build_darwinid( LPCWSTR string, DWORD magic )
978 EXP_DARWIN_LINK *buffer;
980 buffer = LocalAlloc( LMEM_ZEROINIT, sizeof *buffer );
981 buffer->dbh.cbSize = sizeof *buffer;
982 buffer->dbh.dwSignature = magic;
983 lstrcpynW( buffer->szwDarwinID, string, MAX_PATH );
984 WideCharToMultiByte(CP_ACP, 0, string, -1, buffer->szDarwinID, MAX_PATH, NULL, NULL );
986 return buffer;
989 static HRESULT Stream_WriteAdvertiseInfo( IStream* stm, LPCWSTR string, DWORD magic )
991 EXP_DARWIN_LINK *buffer;
992 ULONG count;
994 TRACE("%p\n",stm);
996 buffer = shelllink_build_darwinid( string, magic );
998 return IStream_Write( stm, buffer, buffer->dbh.cbSize, &count );
1001 /************************************************************************
1002 * IPersistStream_Save (IPersistStream)
1004 * FIXME: makes assumptions about byte order
1006 static HRESULT WINAPI IPersistStream_fnSave(
1007 IPersistStream* iface,
1008 IStream* stm,
1009 BOOL fClearDirty)
1011 LINK_HEADER header;
1012 ULONG count;
1013 DWORD zero;
1014 HRESULT r;
1016 IShellLinkImpl *This = impl_from_IPersistStream(iface);
1018 TRACE("%p %p %x\n", This, stm, fClearDirty);
1020 memset(&header, 0, sizeof(header));
1021 header.dwSize = sizeof(header);
1022 header.fStartup = This->iShowCmd;
1023 header.MagicGuid = CLSID_ShellLink;
1025 header.wHotKey = This->wHotKey;
1026 header.nIcon = This->iIcoNdx;
1027 header.dwFlags = SLDF_UNICODE; /* strings are in unicode */
1028 if( This->pPidl )
1029 header.dwFlags |= SLDF_HAS_ID_LIST;
1030 if( This->sPath )
1031 header.dwFlags |= SLDF_HAS_LINK_INFO;
1032 if( This->sDescription )
1033 header.dwFlags |= SLDF_HAS_NAME;
1034 if( This->sWorkDir )
1035 header.dwFlags |= SLDF_HAS_WORKINGDIR;
1036 if( This->sArgs )
1037 header.dwFlags |= SLDF_HAS_ARGS;
1038 if( This->sIcoPath )
1039 header.dwFlags |= SLDF_HAS_ICONLOCATION;
1040 if( This->sProduct )
1041 header.dwFlags |= SLDF_HAS_LOGO3ID;
1042 if( This->sComponent )
1043 header.dwFlags |= SLDF_HAS_DARWINID;
1045 SystemTimeToFileTime ( &This->CreationTime, &header.CreationTime );
1046 SystemTimeToFileTime ( &This->AccessTime, &header.AccessTime );
1047 SystemTimeToFileTime ( &This->WriteTime, &header.WriteTime );
1049 /* write the Shortcut header */
1050 r = IStream_Write( stm, &header, sizeof(header), &count );
1051 if( FAILED( r ) )
1053 ERR("Write failed at %d\n",__LINE__);
1054 return r;
1057 TRACE("Writing pidl\n");
1059 /* write the PIDL to the shortcut */
1060 if( This->pPidl )
1062 r = ILSaveToStream( stm, This->pPidl );
1063 if( FAILED( r ) )
1065 ERR("Failed to write PIDL at %d\n",__LINE__);
1066 return r;
1070 if( This->sPath )
1071 Stream_WriteLocationInfo( stm, This->sPath, &This->volume );
1073 if( This->sDescription )
1074 r = Stream_WriteString( stm, This->sDescription );
1076 if( This->sPathRel )
1077 r = Stream_WriteString( stm, This->sPathRel );
1079 if( This->sWorkDir )
1080 r = Stream_WriteString( stm, This->sWorkDir );
1082 if( This->sArgs )
1083 r = Stream_WriteString( stm, This->sArgs );
1085 if( This->sIcoPath )
1086 r = Stream_WriteString( stm, This->sIcoPath );
1088 if( This->sProduct )
1089 r = Stream_WriteAdvertiseInfo( stm, This->sProduct, EXP_SZ_ICON_SIG );
1091 if( This->sComponent )
1092 r = Stream_WriteAdvertiseInfo( stm, This->sComponent, EXP_DARWIN_ID_SIG );
1094 /* the last field is a single zero dword */
1095 zero = 0;
1096 r = IStream_Write( stm, &zero, sizeof zero, &count );
1098 return S_OK;
1101 /************************************************************************
1102 * IPersistStream_GetSizeMax (IPersistStream)
1104 static HRESULT WINAPI IPersistStream_fnGetSizeMax(
1105 IPersistStream* iface,
1106 ULARGE_INTEGER* pcbSize)
1108 IShellLinkImpl *This = impl_from_IPersistStream(iface);
1110 TRACE("(%p)\n", This);
1112 return E_NOTIMPL;
1115 static const IPersistStreamVtbl psvt =
1117 IPersistStream_fnQueryInterface,
1118 IPersistStream_fnAddRef,
1119 IPersistStream_fnRelease,
1120 IPersistStream_fnGetClassID,
1121 IPersistStream_fnIsDirty,
1122 IPersistStream_fnLoad,
1123 IPersistStream_fnSave,
1124 IPersistStream_fnGetSizeMax
1127 static BOOL SHELL_ExistsFileW(LPCWSTR path)
1129 if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW(path))
1130 return FALSE;
1131 return TRUE;
1134 /**************************************************************************
1135 * ShellLink_UpdatePath
1136 * update absolute path in sPath using relative path in sPathRel
1138 static HRESULT ShellLink_UpdatePath(LPCWSTR sPathRel, LPCWSTR path, LPCWSTR sWorkDir, LPWSTR* psPath)
1140 if (!path || !psPath)
1141 return E_INVALIDARG;
1143 if (!*psPath && sPathRel) {
1144 WCHAR buffer[2*MAX_PATH], abs_path[2*MAX_PATH];
1145 LPWSTR final = NULL;
1147 /* first try if [directory of link file] + [relative path] finds an existing file */
1149 GetFullPathNameW( path, MAX_PATH*2, buffer, &final );
1150 if( !final )
1151 final = buffer;
1152 lstrcpyW(final, sPathRel);
1154 *abs_path = '\0';
1156 if (SHELL_ExistsFileW(buffer)) {
1157 if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
1158 lstrcpyW(abs_path, buffer);
1159 } else {
1160 /* try if [working directory] + [relative path] finds an existing file */
1161 if (sWorkDir) {
1162 lstrcpyW(buffer, sWorkDir);
1163 lstrcpyW(PathAddBackslashW(buffer), sPathRel);
1165 if (SHELL_ExistsFileW(buffer))
1166 if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
1167 lstrcpyW(abs_path, buffer);
1171 /* FIXME: This is even not enough - not all shell links can be resolved using this algorithm. */
1172 if (!*abs_path)
1173 lstrcpyW(abs_path, sPathRel);
1175 *psPath = malloc((lstrlenW(abs_path) + 1) * sizeof(WCHAR));
1176 if (!*psPath)
1177 return E_OUTOFMEMORY;
1179 lstrcpyW(*psPath, abs_path);
1182 return S_OK;
1185 /**************************************************************************
1186 * IShellLink_ConstructFromFile
1188 HRESULT IShellLink_ConstructFromFile( IUnknown* pUnkOuter, REFIID riid,
1189 LPCITEMIDLIST pidl, IUnknown **ppv)
1191 IShellLinkW* psl;
1193 HRESULT hr = IShellLink_Constructor(NULL, riid, (LPVOID*)&psl);
1195 if (SUCCEEDED(hr)) {
1196 IPersistFile* ppf;
1198 *ppv = NULL;
1200 hr = IShellLinkW_QueryInterface(psl, &IID_IPersistFile, (LPVOID*)&ppf);
1202 if (SUCCEEDED(hr)) {
1203 WCHAR path[MAX_PATH];
1205 if (SHGetPathFromIDListW(pidl, path))
1206 hr = IPersistFile_Load(ppf, path, 0);
1207 else
1208 hr = E_FAIL;
1210 if (SUCCEEDED(hr))
1211 *ppv = (IUnknown*)psl;
1213 IPersistFile_Release(ppf);
1216 if (!*ppv)
1217 IShellLinkW_Release(psl);
1220 return hr;
1223 /**************************************************************************
1224 * IShellLinkA_QueryInterface
1226 static HRESULT WINAPI IShellLinkA_fnQueryInterface(IShellLinkA *iface, REFIID riid, void **ppvObj)
1228 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1229 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObj);
1232 /******************************************************************************
1233 * IShellLinkA_AddRef
1235 static ULONG WINAPI IShellLinkA_fnAddRef(IShellLinkA *iface)
1237 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1238 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
1241 /******************************************************************************
1242 * IShellLinkA_Release
1244 static ULONG WINAPI IShellLinkA_fnRelease(IShellLinkA *iface)
1246 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1247 return IShellLinkW_Release(&This->IShellLinkW_iface);
1250 static HRESULT WINAPI IShellLinkA_fnGetPath(IShellLinkA *iface, LPSTR pszFile, INT cchMaxPath,
1251 WIN32_FIND_DATAA *pfd, DWORD fFlags)
1253 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1254 HRESULT res = S_OK;
1256 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%lu)(%s)\n",
1257 This, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(This->sPath));
1259 if (This->sComponent || This->sProduct)
1260 return S_FALSE;
1262 if (cchMaxPath)
1263 pszFile[0] = 0;
1264 if (This->sPath && This->sPath[0])
1265 WideCharToMultiByte( CP_ACP, 0, This->sPath, -1,
1266 pszFile, cchMaxPath, NULL, NULL);
1267 else
1268 res = S_FALSE;
1270 if (pfd)
1272 memset(pfd, 0, sizeof(*pfd));
1274 if (res == S_OK)
1276 char path[MAX_PATH];
1277 WIN32_FILE_ATTRIBUTE_DATA fad;
1279 WideCharToMultiByte(CP_ACP, 0, This->sPath, -1, path, MAX_PATH, NULL, NULL);
1281 if (GetFileAttributesExW(This->sPath, GetFileExInfoStandard, &fad))
1283 pfd->dwFileAttributes = fad.dwFileAttributes;
1284 pfd->ftCreationTime = fad.ftCreationTime;
1285 pfd->ftLastAccessTime = fad.ftLastAccessTime;
1286 pfd->ftLastWriteTime = fad.ftLastWriteTime;
1287 pfd->nFileSizeHigh = fad.nFileSizeHigh;
1288 pfd->nFileSizeLow = fad.nFileSizeLow;
1291 lstrcpyA(pfd->cFileName, PathFindFileNameA(path));
1293 if (GetShortPathNameA(path, path, MAX_PATH))
1295 lstrcpyA(pfd->cAlternateFileName, PathFindFileNameA(path));
1299 TRACE("attr 0x%08lx size 0x%08lx%08lx name %s shortname %s\n", pfd->dwFileAttributes,
1300 pfd->nFileSizeHigh, pfd->nFileSizeLow, wine_dbgstr_a(pfd->cFileName),
1301 wine_dbgstr_a(pfd->cAlternateFileName));
1304 return res;
1307 static HRESULT WINAPI IShellLinkA_fnGetIDList(IShellLinkA *iface, LPITEMIDLIST *ppidl)
1309 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1310 return IShellLinkW_GetIDList(&This->IShellLinkW_iface, ppidl);
1313 static HRESULT WINAPI IShellLinkA_fnSetIDList(IShellLinkA *iface, LPCITEMIDLIST pidl)
1315 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1316 return IShellLinkW_SetIDList(&This->IShellLinkW_iface, pidl);
1319 static HRESULT WINAPI IShellLinkA_fnGetDescription(IShellLinkA *iface, LPSTR pszName,
1320 INT cchMaxName)
1322 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1324 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1326 if( cchMaxName )
1327 pszName[0] = 0;
1328 if( This->sDescription )
1329 WideCharToMultiByte( CP_ACP, 0, This->sDescription, -1,
1330 pszName, cchMaxName, NULL, NULL);
1332 return S_OK;
1335 static HRESULT WINAPI IShellLinkA_fnSetDescription(IShellLinkA *iface, LPCSTR pszName)
1337 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1338 WCHAR *descrW;
1339 HRESULT hr;
1341 TRACE("(%p)->(pName=%s)\n", This, debugstr_a(pszName));
1343 if (pszName)
1345 descrW = heap_strdupAtoW(pszName);
1346 if (!descrW) return E_OUTOFMEMORY;
1348 else
1349 descrW = NULL;
1351 hr = IShellLinkW_SetDescription(&This->IShellLinkW_iface, descrW);
1352 free(descrW);
1354 return hr;
1357 static HRESULT WINAPI IShellLinkA_fnGetWorkingDirectory(IShellLinkA *iface, LPSTR pszDir,
1358 INT cchMaxPath)
1360 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1362 TRACE("(%p)->(%p len=%u)\n", This, pszDir, cchMaxPath);
1364 if( cchMaxPath )
1365 pszDir[0] = 0;
1366 if( This->sWorkDir )
1367 WideCharToMultiByte( CP_ACP, 0, This->sWorkDir, -1,
1368 pszDir, cchMaxPath, NULL, NULL);
1370 return S_OK;
1373 static HRESULT WINAPI IShellLinkA_fnSetWorkingDirectory(IShellLinkA *iface, LPCSTR pszDir)
1375 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1376 WCHAR *dirW;
1377 HRESULT hr;
1379 TRACE("(%p)->(dir=%s)\n",This, pszDir);
1381 dirW = heap_strdupAtoW(pszDir);
1382 if (!dirW) return E_OUTOFMEMORY;
1384 hr = IShellLinkW_SetWorkingDirectory(&This->IShellLinkW_iface, dirW);
1385 free(dirW);
1387 return hr;
1390 static HRESULT WINAPI IShellLinkA_fnGetArguments(IShellLinkA *iface, LPSTR pszArgs, INT cchMaxPath)
1392 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1394 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1396 if( cchMaxPath )
1397 pszArgs[0] = 0;
1398 if( This->sArgs )
1399 WideCharToMultiByte( CP_ACP, 0, This->sArgs, -1,
1400 pszArgs, cchMaxPath, NULL, NULL);
1402 return S_OK;
1405 static HRESULT WINAPI IShellLinkA_fnSetArguments(IShellLinkA *iface, LPCSTR pszArgs)
1407 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1408 WCHAR *argsW;
1409 HRESULT hr;
1411 TRACE("(%p)->(args=%s)\n",This, debugstr_a(pszArgs));
1413 if (pszArgs)
1415 argsW = heap_strdupAtoW(pszArgs);
1416 if (!argsW) return E_OUTOFMEMORY;
1418 else
1419 argsW = NULL;
1421 hr = IShellLinkW_SetArguments(&This->IShellLinkW_iface, argsW);
1422 free(argsW);
1424 return hr;
1427 static HRESULT WINAPI IShellLinkA_fnGetHotkey(IShellLinkA *iface, WORD *pwHotkey)
1429 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1430 return IShellLinkW_GetHotkey(&This->IShellLinkW_iface, pwHotkey);
1433 static HRESULT WINAPI IShellLinkA_fnSetHotkey(IShellLinkA *iface, WORD wHotkey)
1435 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1436 return IShellLinkW_SetHotkey(&This->IShellLinkW_iface, wHotkey);
1439 static HRESULT WINAPI IShellLinkA_fnGetShowCmd(IShellLinkA *iface, INT *piShowCmd)
1441 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1442 return IShellLinkW_GetShowCmd(&This->IShellLinkW_iface, piShowCmd);
1445 static HRESULT WINAPI IShellLinkA_fnSetShowCmd(IShellLinkA *iface, INT iShowCmd)
1447 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1448 return IShellLinkW_SetShowCmd(&This->IShellLinkW_iface, iShowCmd);
1451 static HRESULT WINAPI IShellLinkA_fnGetIconLocation(IShellLinkA *iface, LPSTR pszIconPath,
1452 INT cchIconPath, INT *piIcon)
1454 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1456 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1458 *piIcon = This->iIcoNdx;
1460 if (This->sIcoPath)
1461 WideCharToMultiByte(CP_ACP, 0, This->sIcoPath, -1, pszIconPath, cchIconPath, NULL, NULL);
1462 else
1463 pszIconPath[0] = 0;
1465 return S_OK;
1468 static HRESULT WINAPI IShellLinkA_fnSetIconLocation(IShellLinkA *iface, LPCSTR path, INT icon)
1470 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1471 WCHAR *pathW = NULL;
1472 HRESULT hr;
1474 TRACE("(%p)->(path=%s icon=%u)\n", This, debugstr_a(path), icon);
1476 if (path)
1478 pathW = heap_strdupAtoW(path);
1479 if (!pathW)
1480 return E_OUTOFMEMORY;
1483 hr = IShellLinkW_SetIconLocation(&This->IShellLinkW_iface, path ? pathW : NULL, icon);
1484 free(pathW);
1486 return hr;
1489 static HRESULT WINAPI IShellLinkA_fnSetRelativePath(IShellLinkA *iface, LPCSTR pszPathRel,
1490 DWORD dwReserved)
1492 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1493 WCHAR *pathW;
1494 HRESULT hr;
1496 TRACE("(%p)->(path=%s %lx)\n",This, pszPathRel, dwReserved);
1498 pathW = heap_strdupAtoW(pszPathRel);
1499 if (!pathW) return E_OUTOFMEMORY;
1501 hr = IShellLinkW_SetRelativePath(&This->IShellLinkW_iface, pathW, dwReserved);
1502 free(pathW);
1504 return hr;
1507 static HRESULT WINAPI IShellLinkA_fnResolve(IShellLinkA *iface, HWND hwnd, DWORD fFlags)
1509 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1511 TRACE("(%p)->(hwnd=%p flags=%lx)\n",This, hwnd, fFlags);
1513 return IShellLinkW_Resolve(&This->IShellLinkW_iface, hwnd, fFlags);
1516 static HRESULT WINAPI IShellLinkA_fnSetPath(IShellLinkA *iface, LPCSTR pszFile)
1518 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1519 HRESULT r;
1520 LPWSTR str;
1522 TRACE("(%p)->(path=%s)\n",This, debugstr_a(pszFile));
1524 if (!pszFile) return E_INVALIDARG;
1526 str = heap_strdupAtoW(pszFile);
1527 if( !str )
1528 return E_OUTOFMEMORY;
1530 r = IShellLinkW_SetPath(&This->IShellLinkW_iface, str);
1531 free( str );
1533 return r;
1536 /**************************************************************************
1537 * IShellLink Implementation
1540 static const IShellLinkAVtbl slvt =
1542 IShellLinkA_fnQueryInterface,
1543 IShellLinkA_fnAddRef,
1544 IShellLinkA_fnRelease,
1545 IShellLinkA_fnGetPath,
1546 IShellLinkA_fnGetIDList,
1547 IShellLinkA_fnSetIDList,
1548 IShellLinkA_fnGetDescription,
1549 IShellLinkA_fnSetDescription,
1550 IShellLinkA_fnGetWorkingDirectory,
1551 IShellLinkA_fnSetWorkingDirectory,
1552 IShellLinkA_fnGetArguments,
1553 IShellLinkA_fnSetArguments,
1554 IShellLinkA_fnGetHotkey,
1555 IShellLinkA_fnSetHotkey,
1556 IShellLinkA_fnGetShowCmd,
1557 IShellLinkA_fnSetShowCmd,
1558 IShellLinkA_fnGetIconLocation,
1559 IShellLinkA_fnSetIconLocation,
1560 IShellLinkA_fnSetRelativePath,
1561 IShellLinkA_fnResolve,
1562 IShellLinkA_fnSetPath
1566 /**************************************************************************
1567 * IShellLinkW_fnQueryInterface
1569 static HRESULT WINAPI IShellLinkW_fnQueryInterface(
1570 IShellLinkW * iface, REFIID riid, LPVOID *ppvObj)
1572 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1574 TRACE("(%p)->(%s)\n", This, debugstr_guid(riid));
1576 *ppvObj = NULL;
1578 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IShellLinkA))
1580 *ppvObj = &This->IShellLinkA_iface;
1582 else if(IsEqualIID(riid, &IID_IShellLinkW))
1584 *ppvObj = &This->IShellLinkW_iface;
1586 else if(IsEqualIID(riid, &IID_IPersistFile))
1588 *ppvObj = &This->IPersistFile_iface;
1590 else if(IsEqualIID(riid, &IID_IPersistStream))
1592 *ppvObj = &This->IPersistStream_iface;
1594 else if(IsEqualIID(riid, &IID_IShellLinkDataList))
1596 *ppvObj = &This->IShellLinkDataList_iface;
1598 else if(IsEqualIID(riid, &IID_IShellExtInit))
1600 *ppvObj = &This->IShellExtInit_iface;
1602 else if(IsEqualIID(riid, &IID_IContextMenu))
1604 *ppvObj = &This->IContextMenu_iface;
1606 else if(IsEqualIID(riid, &IID_IObjectWithSite))
1608 *ppvObj = &This->IObjectWithSite_iface;
1610 else if(IsEqualIID(riid, &IID_IPropertyStore))
1612 *ppvObj = &This->IPropertyStore_iface;
1615 if(*ppvObj)
1617 IUnknown_AddRef((IUnknown*)*ppvObj);
1618 TRACE("-- Interface: (%p)->(%p)\n", ppvObj, *ppvObj);
1619 return S_OK;
1621 ERR("-- Interface: E_NOINTERFACE\n");
1622 return E_NOINTERFACE;
1625 /******************************************************************************
1626 * IShellLinkW_fnAddRef
1628 static ULONG WINAPI IShellLinkW_fnAddRef(IShellLinkW * iface)
1630 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1631 ULONG ref = InterlockedIncrement(&This->ref);
1633 TRACE("(%p)->(count=%lu)\n", This, ref - 1);
1635 return ref;
1638 /******************************************************************************
1639 * IShellLinkW_fnRelease
1641 static ULONG WINAPI IShellLinkW_fnRelease(IShellLinkW * iface)
1643 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1644 ULONG refCount = InterlockedDecrement(&This->ref);
1646 TRACE("(%p)->(count=%lu)\n", This, refCount + 1);
1648 if (refCount)
1649 return refCount;
1651 TRACE("-- destroying IShellLink(%p)\n",This);
1653 free(This->sIcoPath);
1654 free(This->sArgs);
1655 free(This->sWorkDir);
1656 free(This->sDescription);
1657 free(This->sPath);
1658 free(This->sPathRel);
1659 free(This->sProduct);
1660 free(This->sComponent);
1661 free(This->filepath);
1663 if (This->site)
1664 IUnknown_Release( This->site );
1666 if (This->pPidl)
1667 ILFree(This->pPidl);
1669 LocalFree(This);
1671 return 0;
1674 static HRESULT WINAPI IShellLinkW_fnGetPath(IShellLinkW * iface, LPWSTR pszFile,INT cchMaxPath, WIN32_FIND_DATAW *pfd, DWORD fFlags)
1676 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1677 HRESULT res = S_OK;
1679 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%lu)(%s)\n",
1680 This, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(This->sPath));
1682 if (This->sComponent || This->sProduct)
1683 return S_FALSE;
1685 if (cchMaxPath)
1686 pszFile[0] = 0;
1687 if (This->sPath)
1688 lstrcpynW( pszFile, This->sPath, cchMaxPath );
1689 else
1690 res = S_FALSE;
1692 if (pfd)
1694 memset(pfd, 0, sizeof(*pfd));
1696 if (res == S_OK)
1698 WCHAR path[MAX_PATH];
1699 WIN32_FILE_ATTRIBUTE_DATA fad;
1701 if (GetFileAttributesExW(This->sPath, GetFileExInfoStandard, &fad))
1703 pfd->dwFileAttributes = fad.dwFileAttributes;
1704 pfd->ftCreationTime = fad.ftCreationTime;
1705 pfd->ftLastAccessTime = fad.ftLastAccessTime;
1706 pfd->ftLastWriteTime = fad.ftLastWriteTime;
1707 pfd->nFileSizeHigh = fad.nFileSizeHigh;
1708 pfd->nFileSizeLow = fad.nFileSizeLow;
1711 lstrcpyW(pfd->cFileName, PathFindFileNameW(This->sPath));
1713 if (GetShortPathNameW(This->sPath, path, MAX_PATH))
1715 lstrcpyW(pfd->cAlternateFileName, PathFindFileNameW(path));
1719 TRACE("attr 0x%08lx size 0x%08lx%08lx name %s shortname %s\n", pfd->dwFileAttributes,
1720 pfd->nFileSizeHigh, pfd->nFileSizeLow, wine_dbgstr_w(pfd->cFileName),
1721 wine_dbgstr_w(pfd->cAlternateFileName));
1724 return res;
1727 static HRESULT WINAPI IShellLinkW_fnGetIDList(IShellLinkW * iface, LPITEMIDLIST * ppidl)
1729 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1731 TRACE("(%p)->(ppidl=%p)\n",This, ppidl);
1733 if (!This->pPidl)
1735 *ppidl = NULL;
1736 return S_FALSE;
1738 *ppidl = ILClone(This->pPidl);
1739 return S_OK;
1742 static HRESULT WINAPI IShellLinkW_fnSetIDList(IShellLinkW * iface, LPCITEMIDLIST pidl)
1744 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1745 WCHAR path[MAX_PATH];
1747 TRACE("(%p)->(pidl=%p)\n",This, pidl);
1749 if( This->pPidl )
1750 ILFree( This->pPidl );
1751 This->pPidl = ILClone( pidl );
1752 if( !This->pPidl )
1753 return E_FAIL;
1755 free( This->sPath );
1756 This->sPath = NULL;
1758 if ( SHGetPathFromIDListW( pidl, path ) )
1760 This->sPath = malloc((lstrlenW(path) + 1) * sizeof(WCHAR));
1761 if (!This->sPath)
1762 return E_OUTOFMEMORY;
1764 lstrcpyW(This->sPath, path);
1767 This->bDirty = TRUE;
1769 return S_OK;
1772 static HRESULT WINAPI IShellLinkW_fnGetDescription(IShellLinkW * iface, LPWSTR pszName,INT cchMaxName)
1774 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1776 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1778 pszName[0] = 0;
1779 if( This->sDescription )
1780 lstrcpynW( pszName, This->sDescription, cchMaxName );
1782 return S_OK;
1785 static HRESULT WINAPI IShellLinkW_fnSetDescription(IShellLinkW * iface, LPCWSTR pszName)
1787 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1789 TRACE("(%p)->(desc=%s)\n",This, debugstr_w(pszName));
1791 free(This->sDescription);
1792 if (pszName)
1794 This->sDescription = malloc( (lstrlenW( pszName ) + 1) * sizeof(WCHAR) );
1795 if ( !This->sDescription )
1796 return E_OUTOFMEMORY;
1798 lstrcpyW( This->sDescription, pszName );
1800 else
1801 This->sDescription = NULL;
1802 This->bDirty = TRUE;
1804 return S_OK;
1807 static HRESULT WINAPI IShellLinkW_fnGetWorkingDirectory(IShellLinkW * iface, LPWSTR pszDir,INT cchMaxPath)
1809 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1811 TRACE("(%p)->(%p len %u)\n", This, pszDir, cchMaxPath);
1813 if( cchMaxPath )
1814 pszDir[0] = 0;
1815 if( This->sWorkDir )
1816 lstrcpynW( pszDir, This->sWorkDir, cchMaxPath );
1818 return S_OK;
1821 static HRESULT WINAPI IShellLinkW_fnSetWorkingDirectory(IShellLinkW * iface, LPCWSTR pszDir)
1823 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1825 TRACE("(%p)->(dir=%s)\n",This, debugstr_w(pszDir));
1827 free(This->sWorkDir);
1828 This->sWorkDir = malloc( (lstrlenW( pszDir ) + 1) * sizeof(WCHAR) );
1829 if ( !This->sWorkDir )
1830 return E_OUTOFMEMORY;
1831 lstrcpyW( This->sWorkDir, pszDir );
1832 This->bDirty = TRUE;
1834 return S_OK;
1837 static HRESULT WINAPI IShellLinkW_fnGetArguments(IShellLinkW * iface, LPWSTR pszArgs,INT cchMaxPath)
1839 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1841 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1843 if( cchMaxPath )
1844 pszArgs[0] = 0;
1845 if( This->sArgs )
1846 lstrcpynW( pszArgs, This->sArgs, cchMaxPath );
1848 return S_OK;
1851 static HRESULT WINAPI IShellLinkW_fnSetArguments(IShellLinkW * iface, LPCWSTR pszArgs)
1853 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1855 TRACE("(%p)->(args=%s)\n",This, debugstr_w(pszArgs));
1857 free(This->sArgs);
1858 if (pszArgs)
1860 This->sArgs = malloc( (lstrlenW( pszArgs ) + 1) * sizeof(WCHAR) );
1861 if ( !This->sArgs )
1862 return E_OUTOFMEMORY;
1863 lstrcpyW( This->sArgs, pszArgs );
1865 else This->sArgs = NULL;
1867 This->bDirty = TRUE;
1869 return S_OK;
1872 static HRESULT WINAPI IShellLinkW_fnGetHotkey(IShellLinkW * iface, WORD *pwHotkey)
1874 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1876 TRACE("(%p)->(%p)\n",This, pwHotkey);
1878 *pwHotkey=This->wHotKey;
1880 return S_OK;
1883 static HRESULT WINAPI IShellLinkW_fnSetHotkey(IShellLinkW * iface, WORD wHotkey)
1885 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1887 TRACE("(%p)->(hotkey=%x)\n",This, wHotkey);
1889 This->wHotKey = wHotkey;
1890 This->bDirty = TRUE;
1892 return S_OK;
1895 static HRESULT WINAPI IShellLinkW_fnGetShowCmd(IShellLinkW * iface, INT *piShowCmd)
1897 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1899 TRACE("(%p)->(%p)\n",This, piShowCmd);
1901 *piShowCmd = This->iShowCmd;
1903 return S_OK;
1906 static HRESULT WINAPI IShellLinkW_fnSetShowCmd(IShellLinkW * iface, INT iShowCmd)
1908 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1910 TRACE("(%p)->(%d)\n", This, iShowCmd);
1912 This->iShowCmd = iShowCmd;
1913 This->bDirty = TRUE;
1915 return S_OK;
1918 static HRESULT WINAPI IShellLinkW_fnGetIconLocation(IShellLinkW * iface, LPWSTR pszIconPath,INT cchIconPath,INT *piIcon)
1920 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1922 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1924 *piIcon = This->iIcoNdx;
1926 if (This->sIcoPath)
1927 lstrcpynW(pszIconPath, This->sIcoPath, cchIconPath);
1928 else
1929 pszIconPath[0] = 0;
1931 return S_OK;
1934 static HRESULT WINAPI IShellLinkW_fnSetIconLocation(IShellLinkW * iface, const WCHAR *path, INT icon)
1936 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1938 TRACE("(%p)->(path=%s icon=%u)\n", This, debugstr_w(path), icon);
1940 free(This->sIcoPath);
1941 if (path)
1943 size_t len = (lstrlenW(path) + 1) * sizeof(WCHAR);
1944 This->sIcoPath = malloc(len);
1945 if (!This->sIcoPath)
1946 return E_OUTOFMEMORY;
1947 memcpy(This->sIcoPath, path, len);
1949 else
1950 This->sIcoPath = NULL;
1951 This->iIcoNdx = icon;
1952 This->bDirty = TRUE;
1954 return S_OK;
1957 static HRESULT WINAPI IShellLinkW_fnSetRelativePath(IShellLinkW * iface, LPCWSTR pszPathRel, DWORD dwReserved)
1959 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1961 TRACE("(%p)->(path=%s %lx)\n",This, debugstr_w(pszPathRel), dwReserved);
1963 free(This->sPathRel);
1964 This->sPathRel = malloc( (lstrlenW( pszPathRel ) + 1) * sizeof(WCHAR) );
1965 if ( !This->sPathRel )
1966 return E_OUTOFMEMORY;
1967 lstrcpyW( This->sPathRel, pszPathRel );
1968 This->bDirty = TRUE;
1970 return ShellLink_UpdatePath(This->sPathRel, This->sPath, This->sWorkDir, &This->sPath);
1973 static HRESULT WINAPI IShellLinkW_fnResolve(IShellLinkW * iface, HWND hwnd, DWORD fFlags)
1975 HRESULT hr = S_OK;
1976 BOOL bSuccess;
1978 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1980 TRACE("(%p)->(hwnd=%p flags=%lx)\n",This, hwnd, fFlags);
1982 /*FIXME: use IResolveShellLink interface */
1984 if (!This->sPath && This->pPidl) {
1985 WCHAR buffer[MAX_PATH];
1987 bSuccess = SHGetPathFromIDListW(This->pPidl, buffer);
1989 if (bSuccess && *buffer) {
1990 This->sPath = malloc((lstrlenW(buffer) + 1) * sizeof(WCHAR));
1991 if (!This->sPath)
1992 return E_OUTOFMEMORY;
1994 lstrcpyW(This->sPath, buffer);
1996 This->bDirty = TRUE;
1997 } else
1998 hr = S_OK; /* don't report an error occurred while just caching information */
2001 if (!This->sIcoPath && This->sPath) {
2002 This->sIcoPath = malloc((lstrlenW(This->sPath) + 1) * sizeof(WCHAR));
2003 if (!This->sIcoPath)
2004 return E_OUTOFMEMORY;
2006 lstrcpyW(This->sIcoPath, This->sPath);
2007 This->iIcoNdx = 0;
2009 This->bDirty = TRUE;
2012 return hr;
2015 static LPWSTR ShellLink_GetAdvertisedArg(LPCWSTR str)
2017 LPWSTR ret;
2018 LPCWSTR p;
2019 DWORD len;
2021 if( !str )
2022 return NULL;
2024 p = wcschr( str, ':' );
2025 if( !p )
2026 return NULL;
2027 len = p - str;
2028 ret = malloc(sizeof(WCHAR) * (len + 1));
2029 if( !ret )
2030 return ret;
2031 memcpy( ret, str, sizeof(WCHAR)*len );
2032 ret[len] = 0;
2033 return ret;
2036 static HRESULT ShellLink_SetAdvertiseInfo(IShellLinkImpl *This, LPCWSTR str)
2038 LPCWSTR szComponent = NULL, szProduct = NULL, p;
2039 WCHAR szGuid[39];
2040 HRESULT r;
2041 GUID guid;
2042 int len;
2044 while( str[0] )
2046 /* each segment must start with two colons */
2047 if( str[0] != ':' || str[1] != ':' )
2048 return E_FAIL;
2050 /* the last segment is just two colons */
2051 if( !str[2] )
2052 break;
2053 str += 2;
2055 /* there must be a colon straight after a guid */
2056 p = wcschr( str, ':' );
2057 if( !p )
2058 return E_FAIL;
2059 len = p - str;
2060 if( len != 38 )
2061 return E_FAIL;
2063 /* get the guid, and check it's validly formatted */
2064 memcpy( szGuid, str, sizeof(WCHAR)*len );
2065 szGuid[len] = 0;
2066 r = CLSIDFromString( szGuid, &guid );
2067 if( r != S_OK )
2068 return r;
2069 str = p + 1;
2071 /* match it up to a guid that we care about */
2072 if( IsEqualGUID( &guid, &SHELL32_AdvtShortcutComponent ) && !szComponent )
2073 szComponent = str;
2074 else if( IsEqualGUID( &guid, &SHELL32_AdvtShortcutProduct ) && !szProduct )
2075 szProduct = str;
2076 else
2077 return E_FAIL;
2079 /* skip to the next field */
2080 str = wcschr( str, ':' );
2081 if( !str )
2082 return E_FAIL;
2085 /* we have to have a component for an advertised shortcut */
2086 if( !szComponent )
2087 return E_FAIL;
2089 This->sComponent = ShellLink_GetAdvertisedArg( szComponent );
2090 This->sProduct = ShellLink_GetAdvertisedArg( szProduct );
2092 TRACE("Component = %s\n", debugstr_w(This->sComponent));
2093 TRACE("Product = %s\n", debugstr_w(This->sProduct));
2095 return S_OK;
2098 static BOOL ShellLink_GetVolumeInfo(LPCWSTR path, volume_info *volume)
2100 const int label_sz = ARRAY_SIZE(volume->label);
2101 WCHAR drive[] = { path[0], ':', '\\', 0 };
2102 BOOL r;
2104 volume->type = GetDriveTypeW(drive);
2105 r = GetVolumeInformationW(drive, volume->label, label_sz,
2106 &volume->serial, NULL, NULL, NULL, 0);
2107 TRACE("r = %d type %ld serial %08lx name %s\n", r,
2108 volume->type, volume->serial, debugstr_w(volume->label));
2109 return r;
2112 static HRESULT WINAPI IShellLinkW_fnSetPath(IShellLinkW * iface, LPCWSTR pszFile)
2114 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
2115 WCHAR buffer[MAX_PATH];
2116 LPWSTR fname, unquoted = NULL;
2117 HRESULT hr = S_OK;
2118 UINT len;
2120 TRACE("(%p)->(path=%s)\n",This, debugstr_w(pszFile));
2122 if (!pszFile) return E_INVALIDARG;
2124 /* quotes at the ends of the string are stripped */
2125 len = lstrlenW(pszFile);
2126 if (pszFile[0] == '"' && pszFile[len-1] == '"')
2128 unquoted = wcsdup(pszFile);
2129 PathUnquoteSpacesW(unquoted);
2130 pszFile = unquoted;
2133 /* any other quote marks are invalid */
2134 if (wcschr(pszFile, '"'))
2136 free(unquoted);
2137 return S_FALSE;
2140 free(This->sPath);
2141 This->sPath = NULL;
2143 free(This->sComponent);
2144 This->sComponent = NULL;
2146 if (This->pPidl)
2147 ILFree(This->pPidl);
2148 This->pPidl = NULL;
2150 if (S_OK != ShellLink_SetAdvertiseInfo( This, pszFile ))
2152 if (*pszFile == '\0')
2153 *buffer = '\0';
2154 else if (!GetFullPathNameW(pszFile, MAX_PATH, buffer, &fname))
2156 free(unquoted);
2157 return E_FAIL;
2159 else if(!PathFileExistsW(buffer) &&
2160 !SearchPathW(NULL, pszFile, NULL, MAX_PATH, buffer, NULL))
2161 hr = S_FALSE;
2163 This->pPidl = SHSimpleIDListFromPathW(pszFile);
2164 ShellLink_GetVolumeInfo(buffer, &This->volume);
2166 This->sPath = malloc( (lstrlenW( buffer ) + 1) * sizeof(WCHAR) );
2167 if (!This->sPath)
2169 free(unquoted);
2170 return E_OUTOFMEMORY;
2173 lstrcpyW(This->sPath, buffer);
2175 This->bDirty = TRUE;
2176 free(unquoted);
2178 return hr;
2181 /**************************************************************************
2182 * IShellLinkW Implementation
2185 static const IShellLinkWVtbl slvtw =
2187 IShellLinkW_fnQueryInterface,
2188 IShellLinkW_fnAddRef,
2189 IShellLinkW_fnRelease,
2190 IShellLinkW_fnGetPath,
2191 IShellLinkW_fnGetIDList,
2192 IShellLinkW_fnSetIDList,
2193 IShellLinkW_fnGetDescription,
2194 IShellLinkW_fnSetDescription,
2195 IShellLinkW_fnGetWorkingDirectory,
2196 IShellLinkW_fnSetWorkingDirectory,
2197 IShellLinkW_fnGetArguments,
2198 IShellLinkW_fnSetArguments,
2199 IShellLinkW_fnGetHotkey,
2200 IShellLinkW_fnSetHotkey,
2201 IShellLinkW_fnGetShowCmd,
2202 IShellLinkW_fnSetShowCmd,
2203 IShellLinkW_fnGetIconLocation,
2204 IShellLinkW_fnSetIconLocation,
2205 IShellLinkW_fnSetRelativePath,
2206 IShellLinkW_fnResolve,
2207 IShellLinkW_fnSetPath
2210 static HRESULT WINAPI
2211 ShellLink_DataList_QueryInterface( IShellLinkDataList* iface, REFIID riid, void** ppvObject)
2213 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2214 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObject);
2217 static ULONG WINAPI
2218 ShellLink_DataList_AddRef( IShellLinkDataList* iface )
2220 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2221 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
2224 static ULONG WINAPI
2225 ShellLink_DataList_Release( IShellLinkDataList* iface )
2227 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2228 return IShellLinkW_Release(&This->IShellLinkW_iface);
2231 static HRESULT WINAPI
2232 ShellLink_AddDataBlock( IShellLinkDataList* iface, void* pDataBlock )
2234 FIXME("(%p)->(%p): stub\n", iface, pDataBlock);
2235 return E_NOTIMPL;
2238 static HRESULT WINAPI
2239 ShellLink_CopyDataBlock( IShellLinkDataList* iface, DWORD dwSig, void** ppDataBlock )
2241 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2242 LPVOID block = NULL;
2243 HRESULT r = E_FAIL;
2245 TRACE("%p %08lx %p\n", iface, dwSig, ppDataBlock );
2247 switch (dwSig)
2249 case EXP_DARWIN_ID_SIG:
2250 if (!This->sComponent)
2251 break;
2252 block = shelllink_build_darwinid( This->sComponent, dwSig );
2253 r = S_OK;
2254 break;
2255 case EXP_SZ_LINK_SIG:
2256 case NT_CONSOLE_PROPS_SIG:
2257 case NT_FE_CONSOLE_PROPS_SIG:
2258 case EXP_SPECIAL_FOLDER_SIG:
2259 case EXP_SZ_ICON_SIG:
2260 FIXME("valid but unhandled datablock %08lx\n", dwSig);
2261 break;
2262 default:
2263 ERR("unknown datablock %08lx\n", dwSig);
2265 *ppDataBlock = block;
2266 return r;
2269 static HRESULT WINAPI
2270 ShellLink_RemoveDataBlock( IShellLinkDataList* iface, DWORD dwSig )
2272 FIXME("(%p)->(%lu): stub\n", iface, dwSig);
2273 return E_NOTIMPL;
2276 static HRESULT WINAPI
2277 ShellLink_GetFlags( IShellLinkDataList* iface, DWORD* pdwFlags )
2279 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2280 DWORD flags = 0;
2282 FIXME("(%p)->(%p): partially implemented\n", This, pdwFlags);
2284 /* FIXME: add more */
2285 if (This->sArgs)
2286 flags |= SLDF_HAS_ARGS;
2287 if (This->sComponent)
2288 flags |= SLDF_HAS_DARWINID;
2289 if (This->sIcoPath)
2290 flags |= SLDF_HAS_ICONLOCATION;
2291 if (This->sProduct)
2292 flags |= SLDF_HAS_LOGO3ID;
2293 if (This->pPidl)
2294 flags |= SLDF_HAS_ID_LIST;
2296 *pdwFlags = flags;
2298 return S_OK;
2301 static HRESULT WINAPI
2302 ShellLink_SetFlags( IShellLinkDataList* iface, DWORD dwFlags )
2304 FIXME("(%p)->(%lu): stub\n", iface, dwFlags);
2305 return S_OK;
2308 static const IShellLinkDataListVtbl dlvt =
2310 ShellLink_DataList_QueryInterface,
2311 ShellLink_DataList_AddRef,
2312 ShellLink_DataList_Release,
2313 ShellLink_AddDataBlock,
2314 ShellLink_CopyDataBlock,
2315 ShellLink_RemoveDataBlock,
2316 ShellLink_GetFlags,
2317 ShellLink_SetFlags
2320 static HRESULT WINAPI
2321 ShellLink_ExtInit_QueryInterface( IShellExtInit* iface, REFIID riid, void** ppvObject )
2323 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2324 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObject);
2327 static ULONG WINAPI
2328 ShellLink_ExtInit_AddRef( IShellExtInit* iface )
2330 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2331 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
2334 static ULONG WINAPI
2335 ShellLink_ExtInit_Release( IShellExtInit* iface )
2337 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2338 return IShellLinkW_Release(&This->IShellLinkW_iface);
2341 /**************************************************************************
2342 * ShellLink implementation of IShellExtInit::Initialize()
2344 * Loads the shelllink from the dataobject the shell is pointing to.
2346 static HRESULT WINAPI
2347 ShellLink_ExtInit_Initialize( IShellExtInit* iface, LPCITEMIDLIST pidlFolder,
2348 IDataObject *pdtobj, HKEY hkeyProgID )
2350 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2351 FORMATETC format;
2352 STGMEDIUM stgm;
2353 UINT count;
2354 HRESULT r = E_FAIL;
2356 TRACE("%p %p %p %p\n", This, pidlFolder, pdtobj, hkeyProgID );
2358 if( !pdtobj )
2359 return r;
2361 format.cfFormat = CF_HDROP;
2362 format.ptd = NULL;
2363 format.dwAspect = DVASPECT_CONTENT;
2364 format.lindex = -1;
2365 format.tymed = TYMED_HGLOBAL;
2367 if( FAILED( IDataObject_GetData( pdtobj, &format, &stgm ) ) )
2368 return r;
2370 count = DragQueryFileW( stgm.hGlobal, -1, NULL, 0 );
2371 if( count == 1 )
2373 LPWSTR path;
2375 count = DragQueryFileW( stgm.hGlobal, 0, NULL, 0 );
2376 count++;
2377 path = malloc( count * sizeof(WCHAR) );
2378 if( path )
2380 IPersistFile *pf = &This->IPersistFile_iface;
2382 count = DragQueryFileW( stgm.hGlobal, 0, path, count );
2383 r = IPersistFile_Load( pf, path, 0 );
2384 free( path );
2387 ReleaseStgMedium( &stgm );
2389 return r;
2392 static const IShellExtInitVtbl eivt =
2394 ShellLink_ExtInit_QueryInterface,
2395 ShellLink_ExtInit_AddRef,
2396 ShellLink_ExtInit_Release,
2397 ShellLink_ExtInit_Initialize
2400 static HRESULT WINAPI
2401 ShellLink_ContextMenu_QueryInterface( IContextMenu* iface, REFIID riid, void** ppvObject )
2403 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2404 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObject);
2407 static ULONG WINAPI
2408 ShellLink_ContextMenu_AddRef( IContextMenu* iface )
2410 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2411 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
2414 static ULONG WINAPI
2415 ShellLink_ContextMenu_Release( IContextMenu* iface )
2417 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2418 return IShellLinkW_Release(&This->IShellLinkW_iface);
2421 static HRESULT WINAPI
2422 ShellLink_QueryContextMenu( IContextMenu* iface, HMENU hmenu, UINT indexMenu,
2423 UINT idCmdFirst, UINT idCmdLast, UINT uFlags )
2425 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2426 MENUITEMINFOW mii;
2427 int id = 1;
2429 TRACE("%p %p %u %u %u %u\n", This,
2430 hmenu, indexMenu, idCmdFirst, idCmdLast, uFlags );
2432 if ( !hmenu )
2433 return E_INVALIDARG;
2435 memset( &mii, 0, sizeof mii );
2436 mii.cbSize = sizeof mii;
2437 mii.fMask = MIIM_TYPE | MIIM_ID | MIIM_STATE;
2438 mii.dwTypeData = (LPWSTR)L"Open";
2439 mii.cch = lstrlenW( mii.dwTypeData );
2440 mii.wID = idCmdFirst + id++;
2441 mii.fState = MFS_DEFAULT | MFS_ENABLED;
2442 mii.fType = MFT_STRING;
2443 if (!InsertMenuItemW( hmenu, indexMenu, TRUE, &mii ))
2444 return E_FAIL;
2445 This->iIdOpen = 0;
2447 return MAKE_HRESULT( SEVERITY_SUCCESS, 0, id );
2450 static LPWSTR
2451 shelllink_get_msi_component_path( LPWSTR component )
2453 LPWSTR path;
2454 DWORD r, sz = 0;
2456 r = CommandLineFromMsiDescriptor( component, NULL, &sz );
2457 if (r != ERROR_SUCCESS)
2458 return NULL;
2460 sz++;
2461 path = malloc( sz * sizeof(WCHAR) );
2462 r = CommandLineFromMsiDescriptor( component, path, &sz );
2463 if (r != ERROR_SUCCESS)
2465 free( path );
2466 path = NULL;
2469 TRACE("returning %s\n", debugstr_w( path ) );
2471 return path;
2474 static HRESULT WINAPI
2475 ShellLink_InvokeCommand( IContextMenu* iface, LPCMINVOKECOMMANDINFO lpici )
2477 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2478 SHELLEXECUTEINFOW sei;
2479 HWND hwnd = NULL; /* FIXME: get using interface set from IObjectWithSite */
2480 LPWSTR args = NULL;
2481 LPWSTR path = NULL;
2482 HRESULT r;
2484 TRACE("%p %p\n", This, lpici );
2486 if ( lpici->cbSize < sizeof (CMINVOKECOMMANDINFO) )
2487 return E_INVALIDARG;
2489 if ( lpici->lpVerb != MAKEINTRESOURCEA(This->iIdOpen) )
2491 ERR("Unknown id %p != %d\n", lpici->lpVerb, This->iIdOpen );
2492 return E_INVALIDARG;
2495 r = IShellLinkW_Resolve(&This->IShellLinkW_iface, hwnd, 0);
2496 if ( FAILED( r ) )
2497 return r;
2499 if ( This->sComponent )
2501 path = shelllink_get_msi_component_path( This->sComponent );
2502 if (!path)
2503 return E_FAIL;
2505 else
2506 path = wcsdup( This->sPath );
2508 if ( lpici->cbSize == sizeof (CMINVOKECOMMANDINFOEX) &&
2509 ( lpici->fMask & CMIC_MASK_UNICODE ) )
2511 LPCMINVOKECOMMANDINFOEX iciex = (LPCMINVOKECOMMANDINFOEX) lpici;
2512 DWORD len = 2;
2514 if ( This->sArgs )
2515 len += lstrlenW( This->sArgs );
2516 if ( iciex->lpParametersW )
2517 len += lstrlenW( iciex->lpParametersW );
2519 args = malloc( len * sizeof(WCHAR) );
2520 args[0] = 0;
2521 if ( This->sArgs )
2522 lstrcatW( args, This->sArgs );
2523 if ( iciex->lpParametersW && iciex->lpParametersW[0] )
2525 lstrcatW( args, L" " );
2526 lstrcatW( args, iciex->lpParametersW );
2530 memset( &sei, 0, sizeof sei );
2531 sei.cbSize = sizeof sei;
2532 sei.fMask = SEE_MASK_UNICODE | (lpici->fMask & (SEE_MASK_NOASYNC|SEE_MASK_NO_CONSOLE|SEE_MASK_ASYNCOK|SEE_MASK_FLAG_NO_UI));
2533 sei.lpFile = path;
2534 sei.nShow = This->iShowCmd;
2535 sei.lpIDList = This->pPidl;
2536 sei.lpDirectory = This->sWorkDir;
2537 sei.lpParameters = args;
2538 sei.lpVerb = L"Open";
2540 if( ShellExecuteExW( &sei ) )
2541 r = S_OK;
2542 else
2543 r = E_FAIL;
2545 free( args );
2546 free( path );
2548 return r;
2551 static HRESULT WINAPI
2552 ShellLink_GetCommandString( IContextMenu* iface, UINT_PTR idCmd, UINT uType,
2553 UINT* pwReserved, LPSTR pszName, UINT cchMax )
2555 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2557 FIXME("(%p)->(%Iu %u %p %p %u): stub\n", This,
2558 idCmd, uType, pwReserved, pszName, cchMax );
2560 return E_NOTIMPL;
2563 static const IContextMenuVtbl cmvt =
2565 ShellLink_ContextMenu_QueryInterface,
2566 ShellLink_ContextMenu_AddRef,
2567 ShellLink_ContextMenu_Release,
2568 ShellLink_QueryContextMenu,
2569 ShellLink_InvokeCommand,
2570 ShellLink_GetCommandString
2573 static HRESULT WINAPI
2574 ShellLink_ObjectWithSite_QueryInterface( IObjectWithSite* iface, REFIID riid, void** ppvObject )
2576 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2577 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObject );
2580 static ULONG WINAPI
2581 ShellLink_ObjectWithSite_AddRef( IObjectWithSite* iface )
2583 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2584 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
2587 static ULONG WINAPI
2588 ShellLink_ObjectWithSite_Release( IObjectWithSite* iface )
2590 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2591 return IShellLinkW_Release(&This->IShellLinkW_iface);
2594 static HRESULT WINAPI
2595 ShellLink_GetSite( IObjectWithSite *iface, REFIID iid, void ** ppvSite )
2597 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2599 TRACE("%p %s %p\n", This, debugstr_guid( iid ), ppvSite );
2601 if ( !This->site )
2602 return E_FAIL;
2603 return IUnknown_QueryInterface( This->site, iid, ppvSite );
2606 static HRESULT WINAPI
2607 ShellLink_SetSite( IObjectWithSite *iface, IUnknown *punk )
2609 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2611 TRACE("%p %p\n", iface, punk);
2613 if ( punk )
2614 IUnknown_AddRef( punk );
2616 if( This->site )
2617 IUnknown_Release( This->site );
2619 This->site = punk;
2621 return S_OK;
2624 static const IObjectWithSiteVtbl owsvt =
2626 ShellLink_ObjectWithSite_QueryInterface,
2627 ShellLink_ObjectWithSite_AddRef,
2628 ShellLink_ObjectWithSite_Release,
2629 ShellLink_SetSite,
2630 ShellLink_GetSite,
2633 static HRESULT WINAPI propertystore_QueryInterface(IPropertyStore *iface, REFIID riid, void **obj)
2635 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2636 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, obj);
2639 static ULONG WINAPI propertystore_AddRef(IPropertyStore *iface)
2641 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2642 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
2645 static ULONG WINAPI propertystore_Release(IPropertyStore *iface)
2647 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2648 return IShellLinkW_Release(&This->IShellLinkW_iface);
2651 static HRESULT WINAPI propertystore_GetCount(IPropertyStore *iface, DWORD *props)
2653 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2654 FIXME("(%p)->(%p): stub\n", This, props);
2655 return E_NOTIMPL;
2658 static HRESULT WINAPI propertystore_GetAt(IPropertyStore *iface, DWORD propid, PROPERTYKEY *key)
2660 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2661 FIXME("(%p)->(%ld %p): stub\n", This, propid, key);
2662 return E_NOTIMPL;
2665 static HRESULT WINAPI propertystore_GetValue(IPropertyStore *iface, REFPROPERTYKEY key, PROPVARIANT *value)
2667 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2668 FIXME("(%p)->(%p %p): stub\n", This, key, value);
2669 return E_NOTIMPL;
2672 static HRESULT WINAPI propertystore_SetValue(IPropertyStore *iface, REFPROPERTYKEY key, REFPROPVARIANT value)
2674 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2675 FIXME("(%p)->(%p %p): stub\n", This, key, value);
2676 return S_OK;
2679 static HRESULT WINAPI propertystore_Commit(IPropertyStore *iface)
2681 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2682 FIXME("(%p): stub\n", This);
2683 return S_OK;
2686 static const IPropertyStoreVtbl propertystorevtbl = {
2687 propertystore_QueryInterface,
2688 propertystore_AddRef,
2689 propertystore_Release,
2690 propertystore_GetCount,
2691 propertystore_GetAt,
2692 propertystore_GetValue,
2693 propertystore_SetValue,
2694 propertystore_Commit
2697 HRESULT WINAPI IShellLink_Constructor(IUnknown *outer, REFIID riid, void **obj)
2699 IShellLinkImpl * sl;
2700 HRESULT r;
2702 TRACE("outer=%p riid=%s\n", outer, debugstr_guid(riid));
2704 *obj = NULL;
2706 if (outer)
2707 return CLASS_E_NOAGGREGATION;
2709 sl = LocalAlloc(LMEM_ZEROINIT,sizeof(IShellLinkImpl));
2710 if (!sl)
2711 return E_OUTOFMEMORY;
2713 sl->ref = 1;
2714 sl->IShellLinkA_iface.lpVtbl = &slvt;
2715 sl->IShellLinkW_iface.lpVtbl = &slvtw;
2716 sl->IPersistFile_iface.lpVtbl = &pfvt;
2717 sl->IPersistStream_iface.lpVtbl = &psvt;
2718 sl->IShellLinkDataList_iface.lpVtbl = &dlvt;
2719 sl->IShellExtInit_iface.lpVtbl = &eivt;
2720 sl->IContextMenu_iface.lpVtbl = &cmvt;
2721 sl->IObjectWithSite_iface.lpVtbl = &owsvt;
2722 sl->IPropertyStore_iface.lpVtbl = &propertystorevtbl;
2723 sl->iShowCmd = SW_SHOWNORMAL;
2724 sl->bDirty = FALSE;
2725 sl->iIdOpen = -1;
2726 sl->site = NULL;
2727 sl->filepath = NULL;
2729 TRACE("(%p)\n", sl);
2731 r = IShellLinkW_QueryInterface( &sl->IShellLinkW_iface, riid, obj );
2732 IShellLinkW_Release( &sl->IShellLinkW_iface );
2733 return r;