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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * Nearly complete informations 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:
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.
36 #include "wine/port.h"
47 #ifdef HAVE_SYS_WAIT_H
48 # include <sys/wait.h>
53 #include "wine/debug.h"
63 #include "undocshell.h"
66 #include "shell32_main.h"
72 WINE_DEFAULT_DEBUG_CHANNEL(shell);
74 DEFINE_GUID( SHELL32_AdvtShortcutProduct,
75 0x9db1186f,0x40df,0x11d1,0xaa,0x8c,0x00,0xc0,0x4f,0xb6,0x78,0x63);
76 DEFINE_GUID( SHELL32_AdvtShortcutComponent,
77 0x9db1186e,0x40df,0x11d1,0xaa,0x8c,0x00,0xc0,0x4f,0xb6,0x78,0x63);
79 /* link file formats */
83 typedef struct _LINK_HEADER
85 DWORD dwSize; /* 0x00 size of the header - 0x4c */
86 GUID MagicGuid; /* 0x04 is CLSID_ShellLink */
87 DWORD dwFlags; /* 0x14 describes elements following */
88 DWORD dwFileAttr; /* 0x18 attributes of the target file */
89 FILETIME Time1; /* 0x1c */
90 FILETIME Time2; /* 0x24 */
91 FILETIME Time3; /* 0x2c */
92 DWORD dwFileLength; /* 0x34 File length */
93 DWORD nIcon; /* 0x38 icon number */
94 DWORD fStartup; /* 0x3c startup type */
95 DWORD wHotKey; /* 0x40 hotkey */
96 DWORD Unknown5; /* 0x44 */
97 DWORD Unknown6; /* 0x48 */
98 } LINK_HEADER, * PLINK_HEADER;
100 #define SHLINK_LOCAL 0
101 #define SHLINK_REMOTE 1
103 typedef struct _LOCATION_INFO
109 DWORD dwLocalPathOfs;
110 DWORD dwNetworkVolTableOfs;
111 DWORD dwFinalPathOfs;
114 typedef struct _LOCAL_VOLUME_INFO
122 typedef struct volume_info_t
126 WCHAR label[12]; /* assume 8.3 */
131 static IShellLinkAVtbl slvt;
132 static IShellLinkWVtbl slvtw;
133 static IPersistFileVtbl pfvt;
134 static IPersistStreamVtbl psvt;
136 /* IShellLink Implementation */
140 IShellLinkAVtbl *lpVtbl;
143 IShellLinkWVtbl *lpvtblw;
144 IPersistFileVtbl *lpvtblPersistFile;
145 IPersistStreamVtbl *lpvtblPersistStream;
147 /* data structures according to the informations in the link */
169 #define _IShellLinkW_Offset ((int)(&(((IShellLinkImpl*)0)->lpvtblw)))
170 #define _ICOM_THIS_From_IShellLinkW(class, name) class* This = (class*)(((char*)name)-_IShellLinkW_Offset)
172 #define _IPersistFile_Offset ((int)(&(((IShellLinkImpl*)0)->lpvtblPersistFile)))
173 #define _ICOM_THIS_From_IPersistFile(class, name) class* This = (class*)(((char*)name)-_IPersistFile_Offset)
175 #define _IPersistStream_Offset ((int)(&(((IShellLinkImpl*)0)->lpvtblPersistStream)))
176 #define _ICOM_THIS_From_IPersistStream(class, name) class* This = (class*)(((char*)name)-_IPersistStream_Offset)
178 static HRESULT ShellLink_UpdatePath(LPWSTR sPathRel, LPCWSTR path, LPCWSTR sWorkDir, LPWSTR* psPath);
180 /* strdup on the process heap */
181 inline static LPWSTR HEAP_strdupAtoW( HANDLE heap, DWORD flags, LPCSTR str)
183 INT len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
184 LPWSTR p = HeapAlloc( heap, flags, len*sizeof (WCHAR) );
187 MultiByteToWideChar( CP_ACP, 0, str, -1, p, len );
191 /**************************************************************************
192 * IPersistFile_QueryInterface
194 static HRESULT WINAPI IPersistFile_fnQueryInterface(
199 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
201 TRACE("(%p)\n",This);
203 return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvObj);
206 /******************************************************************************
207 * IPersistFile_AddRef
209 static ULONG WINAPI IPersistFile_fnAddRef(IPersistFile* iface)
211 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
213 TRACE("(%p)->(count=%lu)\n",This,This->ref);
215 return IShellLinkA_AddRef((IShellLinkA*)This);
217 /******************************************************************************
218 * IPersistFile_Release
220 static ULONG WINAPI IPersistFile_fnRelease(IPersistFile* iface)
222 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
224 TRACE("(%p)->(count=%lu)\n",This,This->ref);
226 return IShellLinkA_Release((IShellLinkA*)This);
229 static HRESULT WINAPI IPersistFile_fnGetClassID(IPersistFile* iface, CLSID *pClassID)
231 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
232 FIXME("(%p)\n",This);
235 static HRESULT WINAPI IPersistFile_fnIsDirty(IPersistFile* iface)
237 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
239 TRACE("(%p)\n",This);
246 static HRESULT WINAPI IPersistFile_fnLoad(IPersistFile* iface, LPCOLESTR pszFileName, DWORD dwMode)
248 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
249 IPersistStream *StreamThis = (IPersistStream *)&This->lpvtblPersistStream;
253 TRACE("(%p, %s, %lx)\n",This, debugstr_w(pszFileName), dwMode);
255 r = CreateStreamOnFile(pszFileName, dwMode, &stm);
258 r = IPersistStream_Load(StreamThis, stm);
259 ShellLink_UpdatePath(This->sPathRel, pszFileName, This->sWorkDir, &This->sPath);
260 IStream_Release( stm );
261 This->bDirty = FALSE;
263 TRACE("-- returning hr %08lx\n", r);
267 static BOOL StartLinkProcessor( LPCOLESTR szLink )
269 static const WCHAR szFormat[] = {
270 'w','i','n','e','m','e','n','u','b','u','i','l','d','e','r','.','e','x','e',
271 ' ','-','r',' ','"','%','s','"',0 };
275 PROCESS_INFORMATION pi;
277 len = sizeof(szFormat) + lstrlenW( szLink ) * sizeof(WCHAR);
278 buffer = HeapAlloc( GetProcessHeap(), 0, len );
282 wsprintfW( buffer, szFormat, szLink );
284 TRACE("starting %s\n",debugstr_w(buffer));
286 memset(&si, 0, sizeof(si));
288 if (!CreateProcessW( NULL, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) return FALSE;
290 /* wait for a while to throttle the creation of linker processes */
291 if( WAIT_OBJECT_0 != WaitForSingleObject( pi.hProcess, 10000 ) )
292 WARN("Timed out waiting for shell linker\n");
294 CloseHandle( pi.hProcess );
295 CloseHandle( pi.hThread );
300 static HRESULT WINAPI IPersistFile_fnSave(IPersistFile* iface, LPCOLESTR pszFileName, BOOL fRemember)
302 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
303 IPersistStream *StreamThis = (IPersistStream *)&This->lpvtblPersistStream;
307 TRACE("(%p)->(%s)\n",This,debugstr_w(pszFileName));
312 r = CreateStreamOnFile(pszFileName, STGM_READWRITE | STGM_CREATE, &stm);
315 r = IPersistStream_Save(StreamThis, stm, FALSE);
316 IStream_Release( stm );
320 StartLinkProcessor( pszFileName );
322 This->bDirty = FALSE;
326 DeleteFileW( pszFileName );
327 WARN("Failed to create shortcut %s\n", debugstr_w(pszFileName) );
334 static HRESULT WINAPI IPersistFile_fnSaveCompleted(IPersistFile* iface, LPCOLESTR pszFileName)
336 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
337 FIXME("(%p)->(%s)\n",This,debugstr_w(pszFileName));
340 static HRESULT WINAPI IPersistFile_fnGetCurFile(IPersistFile* iface, LPOLESTR *ppszFileName)
342 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
343 FIXME("(%p)\n",This);
347 static IPersistFileVtbl pfvt =
349 IPersistFile_fnQueryInterface,
350 IPersistFile_fnAddRef,
351 IPersistFile_fnRelease,
352 IPersistFile_fnGetClassID,
353 IPersistFile_fnIsDirty,
356 IPersistFile_fnSaveCompleted,
357 IPersistFile_fnGetCurFile
360 /************************************************************************
361 * IPersistStream_QueryInterface
363 static HRESULT WINAPI IPersistStream_fnQueryInterface(
364 IPersistStream* iface,
368 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
370 TRACE("(%p)\n",This);
372 return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvoid);
375 /************************************************************************
376 * IPersistStream_Release
378 static ULONG WINAPI IPersistStream_fnRelease(
379 IPersistStream* iface)
381 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
383 TRACE("(%p)\n",This);
385 return IShellLinkA_Release((IShellLinkA*)This);
388 /************************************************************************
389 * IPersistStream_AddRef
391 static ULONG WINAPI IPersistStream_fnAddRef(
392 IPersistStream* iface)
394 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
396 TRACE("(%p)\n",This);
398 return IShellLinkA_AddRef((IShellLinkA*)This);
401 /************************************************************************
402 * IPersistStream_GetClassID
405 static HRESULT WINAPI IPersistStream_fnGetClassID(
406 IPersistStream* iface,
409 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
411 TRACE("(%p)\n", This);
416 /* memcpy(pClassID, &CLSID_???, sizeof(CLSID_???)); */
421 /************************************************************************
422 * IPersistStream_IsDirty (IPersistStream)
424 static HRESULT WINAPI IPersistStream_fnIsDirty(
425 IPersistStream* iface)
427 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
429 TRACE("(%p)\n", This);
435 static HRESULT Stream_LoadString( IStream* stm, BOOL unicode, LPWSTR *pstr )
446 r = IStream_Read(stm, &len, sizeof(len), &count);
447 if ( FAILED (r) || ( count != sizeof(len) ) )
451 len *= sizeof (WCHAR);
453 TRACE("reading %d\n", len);
454 temp = HeapAlloc(GetProcessHeap(), 0, len+sizeof(WCHAR));
456 return E_OUTOFMEMORY;
458 r = IStream_Read(stm, temp, len, &count);
459 if( FAILED (r) || ( count != len ) )
461 HeapFree( GetProcessHeap(), 0, temp );
465 TRACE("read %s\n", debugstr_an(temp,len));
467 /* convert to unicode if necessary */
470 count = MultiByteToWideChar( CP_ACP, 0, (LPSTR) temp, len, NULL, 0 );
471 str = HeapAlloc( GetProcessHeap(), 0, (count+1)*sizeof (WCHAR) );
473 MultiByteToWideChar( CP_ACP, 0, (LPSTR) temp, len, str, count );
474 HeapFree( GetProcessHeap(), 0, temp );
488 static HRESULT Stream_ReadChunk( IStream* stm, LPVOID *data )
495 unsigned char data[1];
500 r = IStream_Read( stm, &size, sizeof(size), &count );
501 if( FAILED( r ) || count != sizeof(size) )
504 chunk = HeapAlloc( GetProcessHeap(), 0, size );
506 return E_OUTOFMEMORY;
509 r = IStream_Read( stm, chunk->data, size - sizeof(size), &count );
510 if( FAILED( r ) || count != (size - sizeof(size)) )
512 HeapFree( GetProcessHeap(), 0, chunk );
516 TRACE("Read %ld bytes\n",chunk->size);
518 *data = (LPVOID) chunk;
523 static BOOL Stream_LoadVolume( LOCAL_VOLUME_INFO *vol, volume_info *volume )
525 const int label_sz = sizeof volume->label/sizeof volume->label[0];
529 volume->serial = vol->dwVolSerial;
530 volume->type = vol->dwType;
532 if( !vol->dwVolLabelOfs )
534 if( vol->dwSize <= vol->dwVolLabelOfs )
536 len = vol->dwSize - vol->dwVolLabelOfs;
539 label += vol->dwVolLabelOfs;
540 MultiByteToWideChar( CP_ACP, 0, label, len, volume->label, label_sz-1);
545 static LPWSTR Stream_LoadPath( LPSTR p, DWORD maxlen )
550 while( p[len] && (len < maxlen) )
553 wlen = MultiByteToWideChar(CP_ACP, 0, p, len, NULL, 0);
554 path = HeapAlloc(GetProcessHeap(), 0, (wlen+1)*sizeof(WCHAR));
555 MultiByteToWideChar(CP_ACP, 0, p, len, path, wlen);
561 static HRESULT Stream_LoadLocation( IStream *stm,
562 volume_info *volume, LPWSTR *path )
564 unsigned char *p = NULL;
569 r = Stream_ReadChunk( stm, (LPVOID*) &p );
573 loc = (LOCATION_INFO*) p;
574 if (loc->dwTotalSize < sizeof(LOCATION_INFO))
576 HeapFree( GetProcessHeap(), 0, p );
580 /* if there's valid local volume information, load it */
581 if( loc->dwVolTableOfs &&
582 ((loc->dwVolTableOfs + sizeof(LOCAL_VOLUME_INFO)) <= loc->dwTotalSize) )
584 LOCAL_VOLUME_INFO *volume_info;
586 volume_info = (LOCAL_VOLUME_INFO*) &p[loc->dwVolTableOfs];
587 Stream_LoadVolume( volume_info, volume );
590 /* if there's a local path, load it */
591 n = loc->dwLocalPathOfs;
592 if( n && (n < loc->dwTotalSize) )
593 *path = Stream_LoadPath( &p[n], loc->dwTotalSize - n );
595 TRACE("type %ld serial %08lx name %s path %s\n", volume->type,
596 volume->serial, debugstr_w(volume->label), debugstr_w(*path));
598 HeapFree( GetProcessHeap(), 0, p );
603 * The format of the advertised shortcut info seems to be:
608 * 0 Length of the block (4 bytes, usually 0x314)
610 * 8 string data in ASCII
611 * 8+0x104 string data in UNICODE
613 * In the original Win32 implementation the buffers are not initialized
614 * to zero, so data trailing the string is random garbage.
616 static HRESULT Stream_LoadAdvertiseInfo( IStream* stm, LPWSTR *str )
621 EXP_DARWIN_LINK buffer;
625 r = IStream_Read( stm, &buffer.dbh.cbSize, sizeof (DWORD), &count );
629 /* make sure that we read the size of the structure even on error */
630 size = sizeof buffer - sizeof (DWORD);
631 if( buffer.dbh.cbSize != sizeof buffer )
633 ERR("Ooops. This structure is not as expected...\n");
637 r = IStream_Read( stm, &buffer.dbh.dwSignature, size, &count );
644 TRACE("magic %08lx string = %s\n", buffer.dbh.dwSignature, debugstr_w(buffer.szwDarwinID));
646 if( (buffer.dbh.dwSignature&0xffff0000) != 0xa0000000 )
648 ERR("Unknown magic number %08lx in advertised shortcut\n", buffer.dbh.dwSignature);
652 *str = HeapAlloc( GetProcessHeap(), 0,
653 (strlenW(buffer.szwDarwinID)+1) * sizeof(WCHAR) );
654 strcpyW( *str, buffer.szwDarwinID );
659 /************************************************************************
660 * IPersistStream_Load (IPersistStream)
662 static HRESULT WINAPI IPersistStream_fnLoad(
663 IPersistStream* iface,
672 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
674 TRACE("%p %p\n", This, stm);
677 return STG_E_INVALIDPOINTER;
680 r = IStream_Read(stm, &hdr, sizeof(hdr), &dwBytesRead);
684 if( dwBytesRead != sizeof(hdr))
686 if( hdr.dwSize != sizeof(hdr))
688 if( !IsEqualIID(&hdr.MagicGuid, &CLSID_ShellLink) )
691 /* free all the old stuff */
694 memset( &This->volume, 0, sizeof This->volume );
695 HeapFree(GetProcessHeap(), 0, This->sPath);
697 HeapFree(GetProcessHeap(), 0, This->sDescription);
698 This->sDescription = NULL;
699 HeapFree(GetProcessHeap(), 0, This->sPathRel);
700 This->sPathRel = NULL;
701 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
702 This->sWorkDir = NULL;
703 HeapFree(GetProcessHeap(), 0, This->sArgs);
705 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
706 This->sIcoPath = NULL;
707 HeapFree(GetProcessHeap(), 0, This->sProduct);
708 This->sProduct = NULL;
709 HeapFree(GetProcessHeap(), 0, This->sComponent);
710 This->sComponent = NULL;
712 This->wHotKey = (WORD)hdr.wHotKey;
713 This->iIcoNdx = hdr.nIcon;
714 FileTimeToSystemTime (&hdr.Time1, &This->time1);
715 FileTimeToSystemTime (&hdr.Time2, &This->time2);
716 FileTimeToSystemTime (&hdr.Time3, &This->time3);
719 WCHAR sTemp[MAX_PATH];
720 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE, &This->time1,
721 NULL, sTemp, sizeof(sTemp)/sizeof(*sTemp));
722 TRACE("-- time1: %s\n", debugstr_w(sTemp) );
723 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE, &This->time2,
724 NULL, sTemp, sizeof(sTemp)/sizeof(*sTemp));
725 TRACE("-- time2: %s\n", debugstr_w(sTemp) );
726 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE, &This->time3,
727 NULL, sTemp, sizeof(sTemp)/sizeof(*sTemp));
728 TRACE("-- time3: %s\n", debugstr_w(sTemp) );
731 /* load all the new stuff */
732 if( hdr.dwFlags & SLDF_HAS_ID_LIST )
734 r = ILLoadFromStream( stm, &This->pPidl );
740 /* load the location information */
741 if( hdr.dwFlags & SLDF_HAS_LINK_INFO )
742 r = Stream_LoadLocation( stm, &This->volume, &This->sPath );
746 unicode = hdr.dwFlags & SLDF_UNICODE;
747 if( hdr.dwFlags & SLDF_HAS_NAME )
749 r = Stream_LoadString( stm, unicode, &This->sDescription );
750 TRACE("Description -> %s\n",debugstr_w(This->sDescription));
755 if( hdr.dwFlags & SLDF_HAS_RELPATH )
757 r = Stream_LoadString( stm, unicode, &This->sPathRel );
758 TRACE("Relative Path-> %s\n",debugstr_w(This->sPathRel));
763 if( hdr.dwFlags & SLDF_HAS_WORKINGDIR )
765 r = Stream_LoadString( stm, unicode, &This->sWorkDir );
766 TRACE("Working Dir -> %s\n",debugstr_w(This->sWorkDir));
771 if( hdr.dwFlags & SLDF_HAS_ARGS )
773 r = Stream_LoadString( stm, unicode, &This->sArgs );
774 TRACE("Working Dir -> %s\n",debugstr_w(This->sArgs));
779 if( hdr.dwFlags & SLDF_HAS_ICONLOCATION )
781 r = Stream_LoadString( stm, unicode, &This->sIcoPath );
782 TRACE("Icon file -> %s\n",debugstr_w(This->sIcoPath));
787 if( hdr.dwFlags & SLDF_HAS_LOGO3ID )
789 r = Stream_LoadAdvertiseInfo( stm, &This->sProduct );
790 TRACE("Product -> %s\n",debugstr_w(This->sProduct));
795 if( hdr.dwFlags & SLDF_HAS_DARWINID )
797 r = Stream_LoadAdvertiseInfo( stm, &This->sComponent );
798 TRACE("Component -> %s\n",debugstr_w(This->sComponent));
803 r = IStream_Read(stm, &zero, sizeof zero, &dwBytesRead);
804 if( FAILED( r ) || zero || dwBytesRead != sizeof zero )
805 ERR("Last word was not zero\n");
816 /************************************************************************
819 * Helper function for IPersistStream_Save. Writes a unicode string
820 * with terminating nul byte to a stream, preceded by the its length.
822 static HRESULT Stream_WriteString( IStream* stm, LPCWSTR str )
824 USHORT len = lstrlenW( str ) + 1;
828 r = IStream_Write( stm, &len, sizeof(len), &count );
832 len *= sizeof(WCHAR);
834 r = IStream_Write( stm, str, len, &count );
841 /************************************************************************
842 * Stream_WriteLocationInfo
844 * Writes the location info to a stream
846 * FIXME: One day we might want to write the network volume information
847 * and the final path.
848 * Figure out how Windows deals with unicode paths here.
850 static HRESULT Stream_WriteLocationInfo( IStream* stm, LPCWSTR path,
851 volume_info *volume )
853 DWORD total_size, path_size, volume_info_size, label_size, final_path_size;
854 LOCAL_VOLUME_INFO *vol;
856 LPSTR szLabel, szPath, szFinalPath;
859 TRACE("%p %s %p\n", stm, debugstr_w(path), volume);
861 /* figure out the size of everything */
862 label_size = WideCharToMultiByte( CP_ACP, 0, volume->label, -1,
863 NULL, 0, NULL, NULL );
864 path_size = WideCharToMultiByte( CP_ACP, 0, path, -1,
865 NULL, 0, NULL, NULL );
866 volume_info_size = sizeof *vol + label_size;
868 total_size = sizeof *loc + volume_info_size + path_size + final_path_size;
870 /* create pointers to everything */
871 loc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, total_size);
872 vol = (LOCAL_VOLUME_INFO*) &loc[1];
873 szLabel = (LPSTR) &vol[1];
874 szPath = &szLabel[label_size];
875 szFinalPath = &szPath[path_size];
877 /* fill in the location information header */
878 loc->dwTotalSize = total_size;
879 loc->dwHeaderSize = sizeof (*loc);
881 loc->dwVolTableOfs = sizeof (*loc);
882 loc->dwLocalPathOfs = sizeof (*loc) + volume_info_size;
883 loc->dwNetworkVolTableOfs = 0;
884 loc->dwFinalPathOfs = sizeof (*loc) + volume_info_size + path_size;
886 /* fill in the volume information */
887 vol->dwSize = volume_info_size;
888 vol->dwType = volume->type;
889 vol->dwVolSerial = volume->serial;
890 vol->dwVolLabelOfs = sizeof (*vol);
892 /* copy in the strings */
893 WideCharToMultiByte( CP_ACP, 0, volume->label, -1,
894 szLabel, label_size, NULL, NULL );
895 WideCharToMultiByte( CP_ACP, 0, path, -1,
896 szPath, path_size, NULL, NULL );
899 return IStream_Write( stm, loc, total_size, &count );
902 static HRESULT Stream_WriteAdvertiseInfo( IStream* stm, LPCWSTR string, DWORD magic )
905 EXP_DARWIN_LINK buffer;
909 memset( &buffer, 0, sizeof buffer );
910 buffer.dbh.cbSize = sizeof buffer;
911 buffer.dbh.dwSignature = magic;
912 lstrcpynW( buffer.szwDarwinID, string, MAX_PATH );
913 WideCharToMultiByte(CP_ACP, 0, string, -1, buffer.szDarwinID, MAX_PATH, NULL, NULL );
915 return IStream_Write( stm, &buffer, buffer.dbh.cbSize, &count );
918 /************************************************************************
919 * IPersistStream_Save (IPersistStream)
921 * FIXME: makes assumptions about byte order
923 static HRESULT WINAPI IPersistStream_fnSave(
924 IPersistStream* iface,
928 static const WCHAR wOpen[] = {'o','p','e','n',0};
931 WCHAR exePath[MAX_PATH];
936 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
938 TRACE("%p %p %x\n", This, stm, fClearDirty);
944 SHELL_FindExecutable(NULL, This->sPath, wOpen, exePath, MAX_PATH,
945 NULL, NULL, NULL, NULL);
947 * windows can create lnk files to executables that do not exist yet
948 * so if the executable does not exist the just trust the path they
951 if (!*exePath) strcpyW(exePath,This->sPath);
954 memset(&header, 0, sizeof(header));
955 header.dwSize = sizeof(header);
956 header.fStartup = This->iShowCmd;
957 memcpy(&header.MagicGuid, &CLSID_ShellLink, sizeof(header.MagicGuid) );
959 header.wHotKey = This->wHotKey;
960 header.nIcon = This->iIcoNdx;
961 header.dwFlags = SLDF_UNICODE; /* strings are in unicode */
963 header.dwFlags |= SLDF_HAS_ID_LIST;
965 header.dwFlags |= SLDF_HAS_LINK_INFO;
966 if( This->sDescription )
967 header.dwFlags |= SLDF_HAS_NAME;
969 header.dwFlags |= SLDF_HAS_WORKINGDIR;
971 header.dwFlags |= SLDF_HAS_ARGS;
973 header.dwFlags |= SLDF_HAS_ICONLOCATION;
975 header.dwFlags |= SLDF_HAS_LOGO3ID;
976 if( This->sComponent )
977 header.dwFlags |= SLDF_HAS_DARWINID;
979 SystemTimeToFileTime ( &This->time1, &header.Time1 );
980 SystemTimeToFileTime ( &This->time2, &header.Time2 );
981 SystemTimeToFileTime ( &This->time3, &header.Time3 );
983 /* write the Shortcut header */
984 r = IStream_Write( stm, &header, sizeof(header), &count );
987 ERR("Write failed at %d\n",__LINE__);
991 TRACE("Writing pidl \n");
993 /* write the PIDL to the shortcut */
996 r = ILSaveToStream( stm, This->pPidl );
999 ERR("Failed to write PIDL at %d\n",__LINE__);
1005 Stream_WriteLocationInfo( stm, exePath, &This->volume );
1007 if( This->sDescription )
1008 r = Stream_WriteString( stm, This->sDescription );
1010 if( This->sPathRel )
1011 r = Stream_WriteString( stm, This->sPathRel );
1013 if( This->sWorkDir )
1014 r = Stream_WriteString( stm, This->sWorkDir );
1017 r = Stream_WriteString( stm, This->sArgs );
1019 if( This->sIcoPath )
1020 r = Stream_WriteString( stm, This->sIcoPath );
1022 if( This->sProduct )
1023 r = Stream_WriteAdvertiseInfo( stm, This->sProduct, EXP_SZ_ICON_SIG );
1025 if( This->sComponent )
1026 r = Stream_WriteAdvertiseInfo( stm, This->sComponent, EXP_DARWIN_ID_SIG );
1028 /* the last field is a single zero dword */
1030 r = IStream_Write( stm, &zero, sizeof zero, &count );
1035 /************************************************************************
1036 * IPersistStream_GetSizeMax (IPersistStream)
1038 static HRESULT WINAPI IPersistStream_fnGetSizeMax(
1039 IPersistStream* iface,
1040 ULARGE_INTEGER* pcbSize)
1042 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
1044 TRACE("(%p)\n", This);
1049 static IPersistStreamVtbl psvt =
1051 IPersistStream_fnQueryInterface,
1052 IPersistStream_fnAddRef,
1053 IPersistStream_fnRelease,
1054 IPersistStream_fnGetClassID,
1055 IPersistStream_fnIsDirty,
1056 IPersistStream_fnLoad,
1057 IPersistStream_fnSave,
1058 IPersistStream_fnGetSizeMax
1061 /**************************************************************************
1062 * IShellLink_Constructor
1064 HRESULT WINAPI IShellLink_Constructor( IUnknown *pUnkOuter,
1065 REFIID riid, LPVOID *ppv )
1067 IShellLinkImpl * sl;
1069 TRACE("unkOut=%p riid=%s\n",pUnkOuter, debugstr_guid(riid));
1074 return CLASS_E_NOAGGREGATION;
1075 sl = LocalAlloc(LMEM_ZEROINIT,sizeof(IShellLinkImpl));
1077 return E_OUTOFMEMORY;
1081 sl->lpvtblw = &slvtw;
1082 sl->lpvtblPersistFile = &pfvt;
1083 sl->lpvtblPersistStream = &psvt;
1084 sl->iShowCmd = SW_SHOWNORMAL;
1087 TRACE("(%p)->()\n",sl);
1089 if (IsEqualIID(riid, &IID_IUnknown) ||
1090 IsEqualIID(riid, &IID_IShellLinkA))
1092 else if (IsEqualIID(riid, &IID_IShellLinkW))
1093 *ppv = &(sl->lpvtblw);
1095 LocalFree((HLOCAL)sl);
1096 ERR("E_NOINTERFACE\n");
1097 return E_NOINTERFACE;
1104 static BOOL SHELL_ExistsFileW(LPCWSTR path)
1106 if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW(path))
1111 /**************************************************************************
1112 * ShellLink_UpdatePath
1113 * update absolute path in sPath using relative path in sPathRel
1115 static HRESULT ShellLink_UpdatePath(LPWSTR sPathRel, LPCWSTR path, LPCWSTR sWorkDir, LPWSTR* psPath)
1117 if (!path || !psPath)
1118 return E_INVALIDARG;
1120 if (!*psPath && sPathRel) {
1121 WCHAR buffer[2*MAX_PATH], abs_path[2*MAX_PATH];
1122 LPWSTR final = NULL;
1124 /* first try if [directory of link file] + [relative path] finds an existing file */
1126 GetFullPathNameW( path, MAX_PATH*2, buffer, &final );
1129 lstrcpyW(final, sPathRel);
1133 if (SHELL_ExistsFileW(buffer)) {
1134 if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
1135 lstrcpyW(abs_path, buffer);
1137 /* try if [working directory] + [relative path] finds an existing file */
1139 lstrcpyW(buffer, sWorkDir);
1140 lstrcpyW(PathAddBackslashW(buffer), sPathRel);
1142 if (SHELL_ExistsFileW(buffer))
1143 if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
1144 lstrcpyW(abs_path, buffer);
1148 /* FIXME: This is even not enough - not all shell links can be resolved using this algorithm. */
1150 lstrcpyW(abs_path, sPathRel);
1152 *psPath = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(abs_path)+1)*sizeof(WCHAR));
1154 return E_OUTOFMEMORY;
1156 lstrcpyW(*psPath, abs_path);
1162 /**************************************************************************
1163 * IShellLink_ConstructFromFile
1165 HRESULT WINAPI IShellLink_ConstructFromFile( IUnknown* pUnkOuter, REFIID riid,
1166 LPCITEMIDLIST pidl, LPVOID* ppv)
1170 HRESULT hr = IShellLink_Constructor(NULL, riid, (LPVOID*)&psl);
1172 if (SUCCEEDED(hr)) {
1177 hr = IShellLinkW_QueryInterface(psl, &IID_IPersistFile, (LPVOID*)&ppf);
1179 if (SUCCEEDED(hr)) {
1180 WCHAR path[MAX_PATH];
1182 if (SHGetPathFromIDListW(pidl, path))
1183 hr = IPersistFile_Load(ppf, path, 0);
1188 *ppv = (IUnknown*) psl;
1190 IPersistFile_Release(ppf);
1194 IShellLinkW_Release(psl);
1200 /**************************************************************************
1201 * IShellLinkA_QueryInterface
1203 static HRESULT WINAPI IShellLinkA_fnQueryInterface( IShellLinkA * iface, REFIID riid, LPVOID *ppvObj)
1205 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1207 TRACE("(%p)->(\n\tIID:\t%s)\n",This,debugstr_guid(riid));
1211 if(IsEqualIID(riid, &IID_IUnknown) ||
1212 IsEqualIID(riid, &IID_IShellLinkA))
1216 else if(IsEqualIID(riid, &IID_IShellLinkW))
1218 *ppvObj = (IShellLinkW *)&(This->lpvtblw);
1220 else if(IsEqualIID(riid, &IID_IPersistFile))
1222 *ppvObj = (IPersistFile *)&(This->lpvtblPersistFile);
1224 else if(IsEqualIID(riid, &IID_IPersistStream))
1226 *ppvObj = (IPersistStream *)&(This->lpvtblPersistStream);
1231 IUnknown_AddRef((IUnknown*)(*ppvObj));
1232 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
1235 TRACE("-- Interface: E_NOINTERFACE\n");
1236 return E_NOINTERFACE;
1238 /******************************************************************************
1239 * IShellLinkA_AddRef
1241 static ULONG WINAPI IShellLinkA_fnAddRef(IShellLinkA * iface)
1243 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1244 ULONG refCount = InterlockedIncrement(&This->ref);
1246 TRACE("(%p)->(count=%lu)\n", This, refCount - 1);
1250 /******************************************************************************
1251 * IShellLinkA_Release
1253 static ULONG WINAPI IShellLinkA_fnRelease(IShellLinkA * iface)
1255 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1256 ULONG refCount = InterlockedDecrement(&This->ref);
1258 TRACE("(%p)->(count=%lu)\n", This, refCount + 1);
1263 TRACE("-- destroying IShellLink(%p)\n",This);
1265 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
1266 HeapFree(GetProcessHeap(), 0, This->sArgs);
1267 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1268 HeapFree(GetProcessHeap(), 0, This->sDescription);
1269 HeapFree(GetProcessHeap(),0,This->sPath);
1272 ILFree(This->pPidl);
1274 LocalFree((HANDLE)This);
1279 static HRESULT WINAPI IShellLinkA_fnGetPath(IShellLinkA * iface, LPSTR pszFile,
1280 INT cchMaxPath, WIN32_FIND_DATAA *pfd, DWORD fFlags)
1282 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1284 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%lu)(%s)\n",
1285 This, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(This->sPath));
1287 if (This->sComponent || This->sProduct)
1293 WideCharToMultiByte( CP_ACP, 0, This->sPath, -1,
1294 pszFile, cchMaxPath, NULL, NULL);
1296 if (pfd) FIXME("(%p): WIN32_FIND_DATA is not yet filled.\n", This);
1301 static HRESULT WINAPI IShellLinkA_fnGetIDList(IShellLinkA * iface, LPITEMIDLIST * ppidl)
1303 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1305 TRACE("(%p)->(ppidl=%p)\n",This, ppidl);
1307 return IShellLinkW_GetIDList((IShellLinkW*)&(This->lpvtblw), ppidl);
1310 static HRESULT WINAPI IShellLinkA_fnSetIDList(IShellLinkA * iface, LPCITEMIDLIST pidl)
1312 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1314 TRACE("(%p)->(pidl=%p)\n",This, pidl);
1317 ILFree(This->pPidl);
1318 This->pPidl = ILClone (pidl);
1319 This->bDirty = TRUE;
1324 static HRESULT WINAPI IShellLinkA_fnGetDescription(IShellLinkA * iface, LPSTR pszName,INT cchMaxName)
1326 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1328 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1332 if( This->sDescription )
1333 WideCharToMultiByte( CP_ACP, 0, This->sDescription, -1,
1334 pszName, cchMaxName, NULL, NULL);
1338 static HRESULT WINAPI IShellLinkA_fnSetDescription(IShellLinkA * iface, LPCSTR pszName)
1340 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1342 TRACE("(%p)->(pName=%s)\n", This, pszName);
1344 HeapFree(GetProcessHeap(), 0, This->sDescription);
1345 This->sDescription = HEAP_strdupAtoW( GetProcessHeap(), 0, pszName);
1346 if ( !This->sDescription )
1347 return E_OUTOFMEMORY;
1349 This->bDirty = TRUE;
1354 static HRESULT WINAPI IShellLinkA_fnGetWorkingDirectory(IShellLinkA * iface, LPSTR pszDir,INT cchMaxPath)
1356 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1358 TRACE("(%p)->(%p len=%u)\n", This, pszDir, cchMaxPath);
1362 if( This->sWorkDir )
1363 WideCharToMultiByte( CP_ACP, 0, This->sWorkDir, -1,
1364 pszDir, cchMaxPath, NULL, NULL);
1369 static HRESULT WINAPI IShellLinkA_fnSetWorkingDirectory(IShellLinkA * iface, LPCSTR pszDir)
1371 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1373 TRACE("(%p)->(dir=%s)\n",This, pszDir);
1375 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1376 This->sWorkDir = HEAP_strdupAtoW( GetProcessHeap(), 0, pszDir);
1377 if ( !This->sWorkDir )
1378 return E_OUTOFMEMORY;
1380 This->bDirty = TRUE;
1385 static HRESULT WINAPI IShellLinkA_fnGetArguments(IShellLinkA * iface, LPSTR pszArgs,INT cchMaxPath)
1387 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1389 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1394 WideCharToMultiByte( CP_ACP, 0, This->sArgs, -1,
1395 pszArgs, cchMaxPath, NULL, NULL);
1400 static HRESULT WINAPI IShellLinkA_fnSetArguments(IShellLinkA * iface, LPCSTR pszArgs)
1402 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1404 TRACE("(%p)->(args=%s)\n",This, pszArgs);
1406 HeapFree(GetProcessHeap(), 0, This->sArgs);
1407 This->sArgs = HEAP_strdupAtoW( GetProcessHeap(), 0, pszArgs);
1409 return E_OUTOFMEMORY;
1411 This->bDirty = TRUE;
1416 static HRESULT WINAPI IShellLinkA_fnGetHotkey(IShellLinkA * iface, WORD *pwHotkey)
1418 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1420 TRACE("(%p)->(%p)(0x%08x)\n",This, pwHotkey, This->wHotKey);
1422 *pwHotkey = This->wHotKey;
1427 static HRESULT WINAPI IShellLinkA_fnSetHotkey(IShellLinkA * iface, WORD wHotkey)
1429 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1431 TRACE("(%p)->(hotkey=%x)\n",This, wHotkey);
1433 This->wHotKey = wHotkey;
1434 This->bDirty = TRUE;
1439 static HRESULT WINAPI IShellLinkA_fnGetShowCmd(IShellLinkA * iface, INT *piShowCmd)
1441 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1443 TRACE("(%p)->(%p)\n",This, piShowCmd);
1444 *piShowCmd = This->iShowCmd;
1448 static HRESULT WINAPI IShellLinkA_fnSetShowCmd(IShellLinkA * iface, INT iShowCmd)
1450 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1452 TRACE("(%p) %d\n",This, iShowCmd);
1454 This->iShowCmd = iShowCmd;
1455 This->bDirty = TRUE;
1460 static HRESULT SHELL_PidlGeticonLocationA(IShellFolder* psf, LPITEMIDLIST pidl, LPSTR pszIconPath, int cchIconPath, int* piIcon)
1462 LPCITEMIDLIST pidlLast;
1464 HRESULT hr = SHBindToParent(pidl, &IID_IShellFolder, (LPVOID*)&psf, &pidlLast);
1466 if (SUCCEEDED(hr)) {
1469 hr = IShellFolder_GetUIObjectOf(psf, 0, 1, (LPCITEMIDLIST*)&pidlLast, &IID_IExtractIconA, NULL, (LPVOID*)&pei);
1471 if (SUCCEEDED(hr)) {
1472 hr = IExtractIconA_GetIconLocation(pei, 0, pszIconPath, MAX_PATH, piIcon, NULL);
1474 IExtractIconA_Release(pei);
1477 IShellFolder_Release(psf);
1483 static HRESULT WINAPI IShellLinkA_fnGetIconLocation(IShellLinkA * iface, LPSTR pszIconPath,INT cchIconPath,INT *piIcon)
1485 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1487 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1490 *piIcon = This->iIcoNdx;
1494 WideCharToMultiByte(CP_ACP, 0, This->sIcoPath, -1, pszIconPath, cchIconPath, NULL, NULL);
1498 if (This->pPidl || This->sPath)
1502 HRESULT hr = SHGetDesktopFolder(&pdsk);
1506 /* first look for an icon using the PIDL (if present) */
1508 hr = SHELL_PidlGeticonLocationA(pdsk, This->pPidl, pszIconPath, cchIconPath, piIcon);
1512 /* if we couldn't find an icon yet, look for it using the file system path */
1513 if (FAILED(hr) && This->sPath)
1517 hr = IShellFolder_ParseDisplayName(pdsk, 0, NULL, This->sPath, NULL, &pidl, NULL);
1519 if (SUCCEEDED(hr)) {
1520 hr = SHELL_PidlGeticonLocationA(pdsk, pidl, pszIconPath, cchIconPath, piIcon);
1526 IShellFolder_Release(pdsk);
1534 static HRESULT WINAPI IShellLinkA_fnSetIconLocation(IShellLinkA * iface, LPCSTR pszIconPath,INT iIcon)
1536 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1538 TRACE("(%p)->(path=%s iicon=%u)\n",This, pszIconPath, iIcon);
1540 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
1541 This->sIcoPath = HEAP_strdupAtoW(GetProcessHeap(), 0, pszIconPath);
1542 if ( !This->sIcoPath )
1543 return E_OUTOFMEMORY;
1545 This->iIcoNdx = iIcon;
1546 This->bDirty = TRUE;
1551 static HRESULT WINAPI IShellLinkA_fnSetRelativePath(IShellLinkA * iface, LPCSTR pszPathRel, DWORD dwReserved)
1553 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1555 TRACE("(%p)->(path=%s %lx)\n",This, pszPathRel, dwReserved);
1557 HeapFree(GetProcessHeap(), 0, This->sPathRel);
1558 This->sPathRel = HEAP_strdupAtoW(GetProcessHeap(), 0, pszPathRel);
1559 This->bDirty = TRUE;
1561 return ShellLink_UpdatePath(This->sPathRel, This->sPath, This->sWorkDir, &This->sPath);
1564 static HRESULT WINAPI IShellLinkA_fnResolve(IShellLinkA * iface, HWND hwnd, DWORD fFlags)
1566 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1568 TRACE("(%p)->(hwnd=%p flags=%lx)\n",This, hwnd, fFlags);
1570 return IShellLinkW_Resolve( (IShellLinkW*)&(This->lpvtblw), hwnd, fFlags );
1573 static HRESULT WINAPI IShellLinkA_fnSetPath(IShellLinkA * iface, LPCSTR pszFile)
1577 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1579 TRACE("(%p)->(path=%s)\n",This, pszFile);
1581 str = HEAP_strdupAtoW(GetProcessHeap(), 0, pszFile);
1583 return E_OUTOFMEMORY;
1585 r = IShellLinkW_SetPath((IShellLinkW*)&(This->lpvtblw), str);
1586 HeapFree( GetProcessHeap(), 0, str );
1591 /**************************************************************************
1592 * IShellLink Implementation
1595 static IShellLinkAVtbl slvt =
1597 IShellLinkA_fnQueryInterface,
1598 IShellLinkA_fnAddRef,
1599 IShellLinkA_fnRelease,
1600 IShellLinkA_fnGetPath,
1601 IShellLinkA_fnGetIDList,
1602 IShellLinkA_fnSetIDList,
1603 IShellLinkA_fnGetDescription,
1604 IShellLinkA_fnSetDescription,
1605 IShellLinkA_fnGetWorkingDirectory,
1606 IShellLinkA_fnSetWorkingDirectory,
1607 IShellLinkA_fnGetArguments,
1608 IShellLinkA_fnSetArguments,
1609 IShellLinkA_fnGetHotkey,
1610 IShellLinkA_fnSetHotkey,
1611 IShellLinkA_fnGetShowCmd,
1612 IShellLinkA_fnSetShowCmd,
1613 IShellLinkA_fnGetIconLocation,
1614 IShellLinkA_fnSetIconLocation,
1615 IShellLinkA_fnSetRelativePath,
1616 IShellLinkA_fnResolve,
1617 IShellLinkA_fnSetPath
1621 /**************************************************************************
1622 * IShellLinkW_fnQueryInterface
1624 static HRESULT WINAPI IShellLinkW_fnQueryInterface(
1625 IShellLinkW * iface, REFIID riid, LPVOID *ppvObj)
1627 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1629 return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvObj);
1632 /******************************************************************************
1633 * IShellLinkW_fnAddRef
1635 static ULONG WINAPI IShellLinkW_fnAddRef(IShellLinkW * iface)
1637 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1639 TRACE("(%p)->(count=%lu)\n",This,This->ref);
1641 return IShellLinkA_AddRef((IShellLinkA*)This);
1643 /******************************************************************************
1644 * IShellLinkW_fnRelease
1647 static ULONG WINAPI IShellLinkW_fnRelease(IShellLinkW * iface)
1649 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1651 TRACE("(%p)->(count=%lu)\n",This,This->ref);
1653 return IShellLinkA_Release((IShellLinkA*)This);
1656 static HRESULT WINAPI IShellLinkW_fnGetPath(IShellLinkW * iface, LPWSTR pszFile,INT cchMaxPath, WIN32_FIND_DATAW *pfd, DWORD fFlags)
1658 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1660 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%lu)(%s)\n",
1661 This, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(This->sPath));
1663 if (This->sComponent || This->sProduct)
1669 lstrcpynW( pszFile, This->sPath, cchMaxPath );
1671 if (pfd) FIXME("(%p): WIN32_FIND_DATA is not yet filled.\n", This);
1676 static HRESULT WINAPI IShellLinkW_fnGetIDList(IShellLinkW * iface, LPITEMIDLIST * ppidl)
1678 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1680 TRACE("(%p)->(ppidl=%p)\n",This, ppidl);
1684 *ppidl = ILClone(This->pPidl);
1688 static HRESULT WINAPI IShellLinkW_fnSetIDList(IShellLinkW * iface, LPCITEMIDLIST pidl)
1690 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1692 TRACE("(%p)->(pidl=%p)\n",This, pidl);
1695 ILFree( This->pPidl );
1696 This->pPidl = ILClone( pidl );
1700 This->bDirty = TRUE;
1705 static HRESULT WINAPI IShellLinkW_fnGetDescription(IShellLinkW * iface, LPWSTR pszName,INT cchMaxName)
1707 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1709 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1712 if( This->sDescription )
1713 lstrcpynW( pszName, This->sDescription, cchMaxName );
1718 static HRESULT WINAPI IShellLinkW_fnSetDescription(IShellLinkW * iface, LPCWSTR pszName)
1720 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1722 TRACE("(%p)->(desc=%s)\n",This, debugstr_w(pszName));
1724 HeapFree(GetProcessHeap(), 0, This->sDescription);
1725 This->sDescription = HeapAlloc( GetProcessHeap(), 0,
1726 (lstrlenW( pszName )+1)*sizeof(WCHAR) );
1727 if ( !This->sDescription )
1728 return E_OUTOFMEMORY;
1730 lstrcpyW( This->sDescription, pszName );
1731 This->bDirty = TRUE;
1736 static HRESULT WINAPI IShellLinkW_fnGetWorkingDirectory(IShellLinkW * iface, LPWSTR pszDir,INT cchMaxPath)
1738 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1740 TRACE("(%p)->(%p len %u)\n", This, pszDir, cchMaxPath);
1744 if( This->sWorkDir )
1745 lstrcpynW( pszDir, This->sWorkDir, cchMaxPath );
1750 static HRESULT WINAPI IShellLinkW_fnSetWorkingDirectory(IShellLinkW * iface, LPCWSTR pszDir)
1752 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1754 TRACE("(%p)->(dir=%s)\n",This, debugstr_w(pszDir));
1756 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1757 This->sWorkDir = HeapAlloc( GetProcessHeap(), 0,
1758 (lstrlenW( pszDir )+1)*sizeof (WCHAR) );
1759 if ( !This->sWorkDir )
1760 return E_OUTOFMEMORY;
1761 lstrcpyW( This->sWorkDir, pszDir );
1762 This->bDirty = TRUE;
1767 static HRESULT WINAPI IShellLinkW_fnGetArguments(IShellLinkW * iface, LPWSTR pszArgs,INT cchMaxPath)
1769 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1771 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1776 lstrcpynW( pszArgs, This->sArgs, cchMaxPath );
1781 static HRESULT WINAPI IShellLinkW_fnSetArguments(IShellLinkW * iface, LPCWSTR pszArgs)
1783 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1785 TRACE("(%p)->(args=%s)\n",This, debugstr_w(pszArgs));
1787 HeapFree(GetProcessHeap(), 0, This->sArgs);
1788 This->sArgs = HeapAlloc( GetProcessHeap(), 0,
1789 (lstrlenW( pszArgs )+1)*sizeof (WCHAR) );
1791 return E_OUTOFMEMORY;
1792 lstrcpyW( This->sArgs, pszArgs );
1793 This->bDirty = TRUE;
1798 static HRESULT WINAPI IShellLinkW_fnGetHotkey(IShellLinkW * iface, WORD *pwHotkey)
1800 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1802 TRACE("(%p)->(%p)\n",This, pwHotkey);
1804 *pwHotkey=This->wHotKey;
1809 static HRESULT WINAPI IShellLinkW_fnSetHotkey(IShellLinkW * iface, WORD wHotkey)
1811 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1813 TRACE("(%p)->(hotkey=%x)\n",This, wHotkey);
1815 This->wHotKey = wHotkey;
1816 This->bDirty = TRUE;
1821 static HRESULT WINAPI IShellLinkW_fnGetShowCmd(IShellLinkW * iface, INT *piShowCmd)
1823 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1825 TRACE("(%p)->(%p)\n",This, piShowCmd);
1827 *piShowCmd = This->iShowCmd;
1832 static HRESULT WINAPI IShellLinkW_fnSetShowCmd(IShellLinkW * iface, INT iShowCmd)
1834 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1836 This->iShowCmd = iShowCmd;
1837 This->bDirty = TRUE;
1842 static HRESULT SHELL_PidlGeticonLocationW(IShellFolder* psf, LPITEMIDLIST pidl, LPWSTR pszIconPath, int cchIconPath, int* piIcon)
1844 LPCITEMIDLIST pidlLast;
1846 HRESULT hr = SHBindToParent(pidl, &IID_IShellFolder, (LPVOID*)&psf, &pidlLast);
1848 if (SUCCEEDED(hr)) {
1851 hr = IShellFolder_GetUIObjectOf(psf, 0, 1, (LPCITEMIDLIST*)&pidlLast, &IID_IExtractIconW, NULL, (LPVOID*)&pei);
1853 if (SUCCEEDED(hr)) {
1854 hr = IExtractIconW_GetIconLocation(pei, 0, pszIconPath, MAX_PATH, piIcon, NULL);
1856 IExtractIconW_Release(pei);
1859 IShellFolder_Release(psf);
1865 static HRESULT WINAPI IShellLinkW_fnGetIconLocation(IShellLinkW * iface, LPWSTR pszIconPath,INT cchIconPath,INT *piIcon)
1867 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1869 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1872 *piIcon = This->iIcoNdx;
1876 lstrcpynW(pszIconPath, This->sIcoPath, cchIconPath);
1880 if (This->pPidl || This->sPath)
1884 HRESULT hr = SHGetDesktopFolder(&pdsk);
1888 /* first look for an icon using the PIDL (if present) */
1890 hr = SHELL_PidlGeticonLocationW(pdsk, This->pPidl, pszIconPath, cchIconPath, piIcon);
1894 /* if we couldn't find an icon yet, look for it using the file system path */
1895 if (FAILED(hr) && This->sPath)
1899 hr = IShellFolder_ParseDisplayName(pdsk, 0, NULL, This->sPath, NULL, &pidl, NULL);
1903 hr = SHELL_PidlGeticonLocationW(pdsk, pidl, pszIconPath, cchIconPath, piIcon);
1909 IShellFolder_Release(pdsk);
1916 static HRESULT WINAPI IShellLinkW_fnSetIconLocation(IShellLinkW * iface, LPCWSTR pszIconPath,INT iIcon)
1918 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1920 TRACE("(%p)->(path=%s iicon=%u)\n",This, debugstr_w(pszIconPath), iIcon);
1922 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
1923 This->sIcoPath = HeapAlloc( GetProcessHeap(), 0,
1924 (lstrlenW( pszIconPath )+1)*sizeof (WCHAR) );
1925 if ( !This->sIcoPath )
1926 return E_OUTOFMEMORY;
1927 lstrcpyW( This->sIcoPath, pszIconPath );
1929 This->iIcoNdx = iIcon;
1930 This->bDirty = TRUE;
1935 static HRESULT WINAPI IShellLinkW_fnSetRelativePath(IShellLinkW * iface, LPCWSTR pszPathRel, DWORD dwReserved)
1937 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1939 TRACE("(%p)->(path=%s %lx)\n",This, debugstr_w(pszPathRel), dwReserved);
1941 HeapFree(GetProcessHeap(), 0, This->sPathRel);
1942 This->sPathRel = HeapAlloc( GetProcessHeap(), 0,
1943 (lstrlenW( pszPathRel )+1) * sizeof (WCHAR) );
1944 if ( !This->sPathRel )
1945 return E_OUTOFMEMORY;
1946 lstrcpyW( This->sPathRel, pszPathRel );
1947 This->bDirty = TRUE;
1949 return ShellLink_UpdatePath(This->sPathRel, This->sPath, This->sWorkDir, &This->sPath);
1952 static HRESULT WINAPI IShellLinkW_fnResolve(IShellLinkW * iface, HWND hwnd, DWORD fFlags)
1956 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1958 TRACE("(%p)->(hwnd=%p flags=%lx)\n",This, hwnd, fFlags);
1960 /*FIXME: use IResolveShellLink interface */
1962 if (!This->sPath && This->pPidl) {
1963 WCHAR buffer[MAX_PATH];
1965 hr = SHELL_GetPathFromIDListW(This->pPidl, buffer, MAX_PATH);
1967 if (SUCCEEDED(hr) && *buffer) {
1968 This->sPath = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(buffer)+1)*sizeof(WCHAR));
1970 return E_OUTOFMEMORY;
1972 lstrcpyW(This->sPath, buffer);
1974 This->bDirty = TRUE;
1976 hr = S_OK; /* don't report an error occurred while just caching information */
1979 if (!This->sIcoPath && This->sPath) {
1980 This->sIcoPath = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(This->sPath)+1)*sizeof(WCHAR));
1981 if (!This->sIcoPath)
1982 return E_OUTOFMEMORY;
1984 lstrcpyW(This->sIcoPath, This->sPath);
1987 This->bDirty = TRUE;
1993 static LPWSTR ShellLink_GetAdvertisedArg(LPCWSTR str)
2002 p = strchrW( str, ':' );
2006 ret = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR)*(len+1));
2009 memcpy( ret, str, sizeof(WCHAR)*len );
2014 static HRESULT ShellLink_SetAdvertiseInfo(IShellLinkImpl *This, LPCWSTR str)
2016 LPCWSTR szComponent = NULL, szProduct = NULL, p;
2024 /* each segment must start with two colons */
2025 if( str[0] != ':' || str[1] != ':' )
2028 /* the last segment is just two colons */
2033 /* there must be a colon straight after a guid */
2034 p = strchrW( str, ':' );
2041 /* get the guid, and check it's validly formatted */
2042 memcpy( szGuid, str, sizeof(WCHAR)*len );
2044 r = CLSIDFromString( szGuid, &guid );
2049 /* match it up to a guid that we care about */
2050 if( IsEqualGUID( &guid, &SHELL32_AdvtShortcutComponent ) && !szComponent )
2052 else if( IsEqualGUID( &guid, &SHELL32_AdvtShortcutProduct ) && !szProduct )
2057 /* skip to the next field */
2058 str = strchrW( str, ':' );
2063 /* we have to have a component for an advertised shortcut */
2067 This->sComponent = ShellLink_GetAdvertisedArg( szComponent );
2068 This->sProduct = ShellLink_GetAdvertisedArg( szProduct );
2070 TRACE("Component = %s\n", debugstr_w(This->sComponent));
2071 TRACE("Product = %s\n", debugstr_w(This->sProduct));
2076 static BOOL ShellLink_GetVolumeInfo(LPWSTR path, volume_info *volume)
2078 const int label_sz = sizeof volume->label/sizeof volume->label[0];
2079 WCHAR drive[4] = { path[0], ':', '\\', 0 };
2082 volume->type = GetDriveTypeW(drive);
2083 r = GetVolumeInformationW(drive, volume->label, label_sz,
2084 &volume->serial, NULL, NULL, NULL, 0);
2085 TRACE("r = %d type %ld serial %08lx name %s\n", r,
2086 volume->type, volume->serial, debugstr_w(volume->label));
2090 static HRESULT WINAPI IShellLinkW_fnSetPath(IShellLinkW * iface, LPCWSTR pszFile)
2092 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
2093 WCHAR buffer[MAX_PATH];
2097 TRACE("(%p)->(path=%s)\n",This, debugstr_w(pszFile));
2099 HeapFree(GetProcessHeap(), 0, This->sPath);
2102 HeapFree(GetProcessHeap(), 0, This->sComponent);
2103 This->sComponent = NULL;
2106 ILFree(This->pPidl);
2109 if (S_OK != ShellLink_SetAdvertiseInfo( This, pszFile ))
2111 if (*pszFile == '\0')
2113 else if (!GetFullPathNameW(pszFile, MAX_PATH, buffer, &fname))
2115 else if(!PathFileExistsW(buffer))
2118 This->pPidl = SHSimpleIDListFromPathW(pszFile);
2119 ShellLink_GetVolumeInfo(buffer, &This->volume);
2121 This->sPath = HeapAlloc( GetProcessHeap(), 0,
2122 (lstrlenW( buffer )+1) * sizeof (WCHAR) );
2124 return E_OUTOFMEMORY;
2126 lstrcpyW(This->sPath, buffer);
2128 This->bDirty = TRUE;
2133 /**************************************************************************
2134 * IShellLinkW Implementation
2137 static IShellLinkWVtbl slvtw =
2139 IShellLinkW_fnQueryInterface,
2140 IShellLinkW_fnAddRef,
2141 IShellLinkW_fnRelease,
2142 IShellLinkW_fnGetPath,
2143 IShellLinkW_fnGetIDList,
2144 IShellLinkW_fnSetIDList,
2145 IShellLinkW_fnGetDescription,
2146 IShellLinkW_fnSetDescription,
2147 IShellLinkW_fnGetWorkingDirectory,
2148 IShellLinkW_fnSetWorkingDirectory,
2149 IShellLinkW_fnGetArguments,
2150 IShellLinkW_fnSetArguments,
2151 IShellLinkW_fnGetHotkey,
2152 IShellLinkW_fnSetHotkey,
2153 IShellLinkW_fnGetShowCmd,
2154 IShellLinkW_fnSetShowCmd,
2155 IShellLinkW_fnGetIconLocation,
2156 IShellLinkW_fnSetIconLocation,
2157 IShellLinkW_fnSetRelativePath,
2158 IShellLinkW_fnResolve,
2159 IShellLinkW_fnSetPath