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 */
81 /* flag1: lnk elements: simple link has 0x0B */
83 #define SCF_LOCATION 2
84 #define SCF_DESCRIPTION 4
85 #define SCF_RELATIVE 8
86 #define SCF_WORKDIR 0x10
88 #define SCF_CUSTOMICON 0x40
89 #define SCF_UNICODE 0x80
90 #define SCF_PRODUCT 0x800
91 #define SCF_COMPONENT 0x1000
95 typedef struct _LINK_HEADER
97 DWORD dwSize; /* 0x00 size of the header - 0x4c */
98 GUID MagicGuid; /* 0x04 is CLSID_ShellLink */
99 DWORD dwFlags; /* 0x14 describes elements following */
100 DWORD dwFileAttr; /* 0x18 attributes of the target file */
101 FILETIME Time1; /* 0x1c */
102 FILETIME Time2; /* 0x24 */
103 FILETIME Time3; /* 0x2c */
104 DWORD dwFileLength; /* 0x34 File length */
105 DWORD nIcon; /* 0x38 icon number */
106 DWORD fStartup; /* 0x3c startup type */
107 DWORD wHotKey; /* 0x40 hotkey */
108 DWORD Unknown5; /* 0x44 */
109 DWORD Unknown6; /* 0x48 */
110 } LINK_HEADER, * PLINK_HEADER;
112 #define SHLINK_LOCAL 0
113 #define SHLINK_REMOTE 1
115 typedef struct _LOCATION_INFO
121 DWORD dwLocalPathOfs;
122 DWORD dwNetworkVolTableOfs;
123 DWORD dwFinalPathOfs;
126 typedef struct _LOCAL_VOLUME_INFO
134 typedef struct tagLINK_ADVERTISEINFO
139 WCHAR bufW[MAX_PATH];
140 } LINK_ADVERTISEINFO;
142 typedef struct volume_info_t
146 WCHAR label[12]; /* assume 8.3 */
151 static IShellLinkAVtbl slvt;
152 static IShellLinkWVtbl slvtw;
153 static IPersistFileVtbl pfvt;
154 static IPersistStreamVtbl psvt;
156 /* IShellLink Implementation */
160 IShellLinkAVtbl *lpVtbl;
163 IShellLinkWVtbl *lpvtblw;
164 IPersistFileVtbl *lpvtblPersistFile;
165 IPersistStreamVtbl *lpvtblPersistStream;
167 /* data structures according to the informations in the link */
189 #define _IShellLinkW_Offset ((int)(&(((IShellLinkImpl*)0)->lpvtblw)))
190 #define _ICOM_THIS_From_IShellLinkW(class, name) class* This = (class*)(((char*)name)-_IShellLinkW_Offset)
192 #define _IPersistFile_Offset ((int)(&(((IShellLinkImpl*)0)->lpvtblPersistFile)))
193 #define _ICOM_THIS_From_IPersistFile(class, name) class* This = (class*)(((char*)name)-_IPersistFile_Offset)
195 #define _IPersistStream_Offset ((int)(&(((IShellLinkImpl*)0)->lpvtblPersistStream)))
196 #define _ICOM_THIS_From_IPersistStream(class, name) class* This = (class*)(((char*)name)-_IPersistStream_Offset)
198 static HRESULT ShellLink_UpdatePath(LPWSTR sPathRel, LPCWSTR path, LPCWSTR sWorkDir, LPWSTR* psPath);
200 /* strdup on the process heap */
201 inline static LPWSTR HEAP_strdupAtoW( HANDLE heap, DWORD flags, LPCSTR str)
203 INT len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
204 LPWSTR p = HeapAlloc( heap, flags, len*sizeof (WCHAR) );
207 MultiByteToWideChar( CP_ACP, 0, str, -1, p, len );
211 /**************************************************************************
212 * IPersistFile_QueryInterface
214 static HRESULT WINAPI IPersistFile_fnQueryInterface(
219 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
221 TRACE("(%p)\n",This);
223 return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvObj);
226 /******************************************************************************
227 * IPersistFile_AddRef
229 static ULONG WINAPI IPersistFile_fnAddRef(IPersistFile* iface)
231 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
233 TRACE("(%p)->(count=%lu)\n",This,This->ref);
235 return IShellLinkA_AddRef((IShellLinkA*)This);
237 /******************************************************************************
238 * IPersistFile_Release
240 static ULONG WINAPI IPersistFile_fnRelease(IPersistFile* iface)
242 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
244 TRACE("(%p)->(count=%lu)\n",This,This->ref);
246 return IShellLinkA_Release((IShellLinkA*)This);
249 static HRESULT WINAPI IPersistFile_fnGetClassID(IPersistFile* iface, CLSID *pClassID)
251 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
252 FIXME("(%p)\n",This);
255 static HRESULT WINAPI IPersistFile_fnIsDirty(IPersistFile* iface)
257 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
259 TRACE("(%p)\n",This);
266 static HRESULT WINAPI IPersistFile_fnLoad(IPersistFile* iface, LPCOLESTR pszFileName, DWORD dwMode)
268 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
269 IPersistStream *StreamThis = (IPersistStream *)&This->lpvtblPersistStream;
273 TRACE("(%p, %s)\n",This, debugstr_w(pszFileName));
275 r = CreateStreamOnFile(pszFileName, dwMode, &stm);
278 r = IPersistStream_Load(StreamThis, stm);
279 ShellLink_UpdatePath(This->sPathRel, pszFileName, This->sWorkDir, &This->sPath);
280 IStream_Release( stm );
281 This->bDirty = FALSE;
287 static BOOL StartLinkProcessor( LPCOLESTR szLink )
289 static const WCHAR szFormat[] = {
290 'w','i','n','e','m','e','n','u','b','u','i','l','d','e','r','.','e','x','e',
291 ' ','-','r',' ','"','%','s','"',0 };
295 PROCESS_INFORMATION pi;
297 len = sizeof(szFormat) + lstrlenW( szLink ) * sizeof(WCHAR);
298 buffer = HeapAlloc( GetProcessHeap(), 0, len );
302 wsprintfW( buffer, szFormat, szLink );
304 TRACE("starting %s\n",debugstr_w(buffer));
306 memset(&si, 0, sizeof(si));
308 if (!CreateProcessW( NULL, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) return FALSE;
310 /* wait for a while to throttle the creation of linker processes */
311 if( WAIT_OBJECT_0 != WaitForSingleObject( pi.hProcess, 10000 ) )
312 WARN("Timed out waiting for shell linker\n");
314 CloseHandle( pi.hProcess );
315 CloseHandle( pi.hThread );
320 static HRESULT WINAPI IPersistFile_fnSave(IPersistFile* iface, LPCOLESTR pszFileName, BOOL fRemember)
322 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
323 IPersistStream *StreamThis = (IPersistStream *)&This->lpvtblPersistStream;
327 TRACE("(%p)->(%s)\n",This,debugstr_w(pszFileName));
332 r = CreateStreamOnFile(pszFileName, STGM_READWRITE | STGM_CREATE, &stm);
335 r = IPersistStream_Save(StreamThis, stm, FALSE);
336 IStream_Release( stm );
340 StartLinkProcessor( pszFileName );
342 This->bDirty = FALSE;
346 DeleteFileW( pszFileName );
347 WARN("Failed to create shortcut %s\n", debugstr_w(pszFileName) );
354 static HRESULT WINAPI IPersistFile_fnSaveCompleted(IPersistFile* iface, LPCOLESTR pszFileName)
356 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
357 FIXME("(%p)->(%s)\n",This,debugstr_w(pszFileName));
360 static HRESULT WINAPI IPersistFile_fnGetCurFile(IPersistFile* iface, LPOLESTR *ppszFileName)
362 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
363 FIXME("(%p)\n",This);
367 static IPersistFileVtbl pfvt =
369 IPersistFile_fnQueryInterface,
370 IPersistFile_fnAddRef,
371 IPersistFile_fnRelease,
372 IPersistFile_fnGetClassID,
373 IPersistFile_fnIsDirty,
376 IPersistFile_fnSaveCompleted,
377 IPersistFile_fnGetCurFile
380 /************************************************************************
381 * IPersistStream_QueryInterface
383 static HRESULT WINAPI IPersistStream_fnQueryInterface(
384 IPersistStream* iface,
388 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
390 TRACE("(%p)\n",This);
392 return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvoid);
395 /************************************************************************
396 * IPersistStream_Release
398 static ULONG WINAPI IPersistStream_fnRelease(
399 IPersistStream* iface)
401 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
403 TRACE("(%p)\n",This);
405 return IShellLinkA_Release((IShellLinkA*)This);
408 /************************************************************************
409 * IPersistStream_AddRef
411 static ULONG WINAPI IPersistStream_fnAddRef(
412 IPersistStream* iface)
414 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
416 TRACE("(%p)\n",This);
418 return IShellLinkA_AddRef((IShellLinkA*)This);
421 /************************************************************************
422 * IPersistStream_GetClassID
425 static HRESULT WINAPI IPersistStream_fnGetClassID(
426 IPersistStream* iface,
429 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
431 TRACE("(%p)\n", This);
436 /* memcpy(pClassID, &CLSID_???, sizeof(CLSID_???)); */
441 /************************************************************************
442 * IPersistStream_IsDirty (IPersistStream)
444 static HRESULT WINAPI IPersistStream_fnIsDirty(
445 IPersistStream* iface)
447 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
449 TRACE("(%p)\n", This);
455 static HRESULT Stream_LoadString( IStream* stm, BOOL unicode, LPWSTR *pstr )
466 r = IStream_Read(stm, &len, sizeof(len), &count);
467 if ( FAILED (r) || ( count != sizeof(len) ) )
471 len *= sizeof (WCHAR);
473 TRACE("reading %d\n", len);
474 temp = HeapAlloc(GetProcessHeap(), 0, len+sizeof(WCHAR));
476 return E_OUTOFMEMORY;
478 r = IStream_Read(stm, temp, len, &count);
479 if( FAILED (r) || ( count != len ) )
481 HeapFree( GetProcessHeap(), 0, temp );
485 TRACE("read %s\n", debugstr_an(temp,len));
487 /* convert to unicode if necessary */
490 count = MultiByteToWideChar( CP_ACP, 0, (LPSTR) temp, len, NULL, 0 );
491 str = HeapAlloc( GetProcessHeap(), 0, (count+1)*sizeof (WCHAR) );
493 MultiByteToWideChar( CP_ACP, 0, (LPSTR) temp, len, str, count );
494 HeapFree( GetProcessHeap(), 0, temp );
508 static HRESULT Stream_ReadChunk( IStream* stm, LPVOID *data )
515 unsigned char data[1];
520 r = IStream_Read( stm, &size, sizeof(size), &count );
521 if( FAILED( r ) || count != sizeof(size) )
524 chunk = HeapAlloc( GetProcessHeap(), 0, size );
526 return E_OUTOFMEMORY;
529 r = IStream_Read( stm, chunk->data, size - sizeof(size), &count );
530 if( FAILED( r ) || count != (size - sizeof(size)) )
532 HeapFree( GetProcessHeap(), 0, chunk );
536 TRACE("Read %ld bytes\n",chunk->size);
538 *data = (LPVOID) chunk;
543 static BOOL Stream_LoadVolume( LOCAL_VOLUME_INFO *vol, volume_info *volume )
545 const int label_sz = sizeof volume->label/sizeof volume->label[0];
549 volume->serial = vol->dwVolSerial;
550 volume->type = vol->dwType;
552 if( !vol->dwVolLabelOfs )
554 if( vol->dwSize <= vol->dwVolLabelOfs )
556 len = vol->dwSize - vol->dwVolLabelOfs;
559 label += vol->dwVolLabelOfs;
560 MultiByteToWideChar( CP_ACP, 0, label, len, volume->label, label_sz-1);
565 static LPWSTR Stream_LoadPath( LPSTR p, DWORD maxlen )
570 while( p[len] && (len < maxlen) )
573 wlen = MultiByteToWideChar(CP_ACP, 0, p, len, NULL, 0);
574 path = HeapAlloc(GetProcessHeap(), 0, (wlen+1)*sizeof(WCHAR));
575 MultiByteToWideChar(CP_ACP, 0, p, len, path, wlen);
581 static HRESULT Stream_LoadLocation( IStream *stm,
582 volume_info *volume, LPWSTR *path )
584 unsigned char *p = NULL;
589 r = Stream_ReadChunk( stm, (LPVOID*) &p );
593 loc = (LOCATION_INFO*) p;
594 if (loc->dwTotalSize < sizeof(LOCATION_INFO))
596 HeapFree( GetProcessHeap(), 0, p );
600 /* if there's valid local volume information, load it */
601 if( loc->dwVolTableOfs &&
602 ((loc->dwVolTableOfs + sizeof(LOCAL_VOLUME_INFO)) <= loc->dwTotalSize) )
604 LOCAL_VOLUME_INFO *volume_info;
606 volume_info = (LOCAL_VOLUME_INFO*) &p[loc->dwVolTableOfs];
607 Stream_LoadVolume( volume_info, volume );
610 /* if there's a local path, load it */
611 n = loc->dwLocalPathOfs;
612 if( n && (n < loc->dwTotalSize) )
613 *path = Stream_LoadPath( &p[n], loc->dwTotalSize - n );
615 TRACE("type %ld serial %08lx name %s path %s\n", volume->type,
616 volume->serial, debugstr_w(volume->label), debugstr_w(*path));
618 HeapFree( GetProcessHeap(), 0, p );
623 * The format of the advertised shortcut info seems to be:
628 * 0 Length of the block (4 bytes, usually 0x314)
630 * 8 string data in ASCII
631 * 8+0x104 string data in UNICODE
633 * In the original Win32 implementation the buffers are not initialized
634 * to zero, so data trailing the string is random garbage.
636 static HRESULT Stream_LoadAdvertiseInfo( IStream* stm, LPWSTR *str )
641 LINK_ADVERTISEINFO buffer;
645 r = IStream_Read( stm, &buffer.size, sizeof (DWORD), &count );
649 /* make sure that we read the size of the structure even on error */
650 size = sizeof buffer - sizeof (DWORD);
651 if( buffer.size != sizeof buffer )
653 ERR("Ooops. This structure is not as expected...\n");
657 r = IStream_Read( stm, &buffer.magic, size, &count );
664 TRACE("magic %08lx string = %s\n", buffer.magic, debugstr_w(buffer.bufW));
666 if( (buffer.magic&0xffff0000) != 0xa0000000 )
668 ERR("Unknown magic number %08lx in advertised shortcut\n", buffer.magic);
672 *str = HeapAlloc( GetProcessHeap(), 0,
673 (strlenW(buffer.bufW)+1) * sizeof(WCHAR) );
674 strcpyW( *str, buffer.bufW );
679 /************************************************************************
680 * IPersistStream_Load (IPersistStream)
682 static HRESULT WINAPI IPersistStream_fnLoad(
683 IPersistStream* iface,
692 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
694 TRACE("%p %p\n", This, stm);
697 return STG_E_INVALIDPOINTER;
700 r = IStream_Read(stm, &hdr, sizeof(hdr), &dwBytesRead);
704 if( dwBytesRead != sizeof(hdr))
706 if( hdr.dwSize != sizeof(hdr))
708 if( !IsEqualIID(&hdr.MagicGuid, &CLSID_ShellLink) )
711 /* free all the old stuff */
714 memset( &This->volume, 0, sizeof This->volume );
715 HeapFree(GetProcessHeap(), 0, This->sPath);
717 HeapFree(GetProcessHeap(), 0, This->sDescription);
718 This->sDescription = NULL;
719 HeapFree(GetProcessHeap(), 0, This->sPathRel);
720 This->sPathRel = NULL;
721 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
722 This->sWorkDir = NULL;
723 HeapFree(GetProcessHeap(), 0, This->sArgs);
725 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
726 This->sIcoPath = NULL;
727 HeapFree(GetProcessHeap(), 0, This->sProduct);
728 This->sProduct = NULL;
729 HeapFree(GetProcessHeap(), 0, This->sComponent);
730 This->sComponent = NULL;
732 This->wHotKey = (WORD)hdr.wHotKey;
733 This->iIcoNdx = hdr.nIcon;
734 FileTimeToSystemTime (&hdr.Time1, &This->time1);
735 FileTimeToSystemTime (&hdr.Time2, &This->time2);
736 FileTimeToSystemTime (&hdr.Time3, &This->time3);
739 WCHAR sTemp[MAX_PATH];
740 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE, &This->time1,
741 NULL, sTemp, sizeof(sTemp)/sizeof(*sTemp));
742 TRACE("-- time1: %s\n", debugstr_w(sTemp) );
743 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE, &This->time2,
744 NULL, sTemp, sizeof(sTemp)/sizeof(*sTemp));
745 TRACE("-- time2: %s\n", debugstr_w(sTemp) );
746 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE, &This->time3,
747 NULL, sTemp, sizeof(sTemp)/sizeof(*sTemp));
748 TRACE("-- time3: %s\n", debugstr_w(sTemp) );
751 /* load all the new stuff */
752 if( hdr.dwFlags & SCF_PIDL )
754 r = ILLoadFromStream( stm, &This->pPidl );
760 /* load the location information */
761 if( hdr.dwFlags & SCF_LOCATION )
762 r = Stream_LoadLocation( stm, &This->volume, &This->sPath );
766 unicode = hdr.dwFlags & SCF_UNICODE;
767 if( hdr.dwFlags & SCF_DESCRIPTION )
769 r = Stream_LoadString( stm, unicode, &This->sDescription );
770 TRACE("Description -> %s\n",debugstr_w(This->sDescription));
775 if( hdr.dwFlags & SCF_RELATIVE )
777 r = Stream_LoadString( stm, unicode, &This->sPathRel );
778 TRACE("Relative Path-> %s\n",debugstr_w(This->sPathRel));
783 if( hdr.dwFlags & SCF_WORKDIR )
785 r = Stream_LoadString( stm, unicode, &This->sWorkDir );
786 TRACE("Working Dir -> %s\n",debugstr_w(This->sWorkDir));
791 if( hdr.dwFlags & SCF_ARGS )
793 r = Stream_LoadString( stm, unicode, &This->sArgs );
794 TRACE("Working Dir -> %s\n",debugstr_w(This->sArgs));
799 if( hdr.dwFlags & SCF_CUSTOMICON )
801 r = Stream_LoadString( stm, unicode, &This->sIcoPath );
802 TRACE("Icon file -> %s\n",debugstr_w(This->sIcoPath));
807 if( hdr.dwFlags & SCF_PRODUCT )
809 r = Stream_LoadAdvertiseInfo( stm, &This->sProduct );
810 TRACE("Product -> %s\n",debugstr_w(This->sProduct));
815 if( hdr.dwFlags & SCF_COMPONENT )
817 r = Stream_LoadAdvertiseInfo( stm, &This->sComponent );
818 TRACE("Component -> %s\n",debugstr_w(This->sComponent));
823 r = IStream_Read(stm, &zero, sizeof zero, &dwBytesRead);
824 if( FAILED( r ) || zero || dwBytesRead != sizeof zero )
825 ERR("Last word was not zero\n");
836 /************************************************************************
839 * Helper function for IPersistStream_Save. Writes a unicode string
840 * with terminating nul byte to a stream, preceded by the its length.
842 static HRESULT Stream_WriteString( IStream* stm, LPCWSTR str )
844 USHORT len = lstrlenW( str ) + 1;
848 r = IStream_Write( stm, &len, sizeof(len), &count );
852 len *= sizeof(WCHAR);
854 r = IStream_Write( stm, str, len, &count );
861 /************************************************************************
862 * Stream_WriteLocationInfo
864 * Writes the location info to a stream
866 * FIXME: One day we might want to write the network volume information
867 * and the final path.
868 * Figure out how Windows deals with unicode paths here.
870 static HRESULT Stream_WriteLocationInfo( IStream* stm, LPCWSTR path,
871 volume_info *volume )
873 DWORD total_size, path_size, volume_info_size, label_size, final_path_size;
874 LOCAL_VOLUME_INFO *vol;
876 LPSTR szLabel, szPath, szFinalPath;
879 TRACE("%p %s %p\n", stm, debugstr_w(path), volume);
881 /* figure out the size of everything */
882 label_size = WideCharToMultiByte( CP_ACP, 0, volume->label, -1,
883 NULL, 0, NULL, NULL );
884 path_size = WideCharToMultiByte( CP_ACP, 0, path, -1,
885 NULL, 0, NULL, NULL );
886 volume_info_size = sizeof *vol + label_size;
888 total_size = sizeof *loc + volume_info_size + path_size + final_path_size;
890 /* create pointers to everything */
891 loc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, total_size);
892 vol = (LOCAL_VOLUME_INFO*) &loc[1];
893 szLabel = (LPSTR) &vol[1];
894 szPath = &szLabel[label_size];
895 szFinalPath = &szPath[path_size];
897 /* fill in the location information header */
898 loc->dwTotalSize = total_size;
899 loc->dwHeaderSize = sizeof (*loc);
901 loc->dwVolTableOfs = sizeof (*loc);
902 loc->dwLocalPathOfs = sizeof (*loc) + volume_info_size;
903 loc->dwNetworkVolTableOfs = 0;
904 loc->dwFinalPathOfs = sizeof (*loc) + volume_info_size + path_size;
906 /* fill in the volume information */
907 vol->dwSize = volume_info_size;
908 vol->dwType = volume->type;
909 vol->dwVolSerial = volume->serial;
910 vol->dwVolLabelOfs = sizeof (*vol);
912 /* copy in the strings */
913 WideCharToMultiByte( CP_ACP, 0, volume->label, -1,
914 szLabel, label_size, NULL, NULL );
915 WideCharToMultiByte( CP_ACP, 0, path, -1,
916 szPath, path_size, NULL, NULL );
919 return IStream_Write( stm, loc, total_size, &count );
922 static HRESULT Stream_WriteAdvertiseInfo( IStream* stm, LPCWSTR string, DWORD magic )
925 LINK_ADVERTISEINFO buffer;
929 memset( &buffer, 0, sizeof buffer );
930 buffer.size = sizeof buffer;
931 buffer.magic = magic;
932 strncpyW( buffer.bufW, string, MAX_PATH );
933 WideCharToMultiByte(CP_ACP, 0, string, -1, buffer.bufA, MAX_PATH, NULL, NULL );
935 return IStream_Write( stm, &buffer, buffer.size, &count );
938 /************************************************************************
939 * IPersistStream_Save (IPersistStream)
941 * FIXME: makes assumptions about byte order
943 static HRESULT WINAPI IPersistStream_fnSave(
944 IPersistStream* iface,
948 static const WCHAR wOpen[] = {'o','p','e','n',0};
951 WCHAR exePath[MAX_PATH];
956 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
958 TRACE("%p %p %x\n", This, stm, fClearDirty);
964 SHELL_FindExecutable(NULL, This->sPath, wOpen, exePath, MAX_PATH,
965 NULL, NULL, NULL, NULL);
967 * windows can create lnk files to executables that do not exist yet
968 * so if the executable does not exist the just trust the path they
971 if (!*exePath) strcpyW(exePath,This->sPath);
974 memset(&header, 0, sizeof(header));
975 header.dwSize = sizeof(header);
976 header.fStartup = This->iShowCmd;
977 memcpy(&header.MagicGuid, &CLSID_ShellLink, sizeof(header.MagicGuid) );
979 header.wHotKey = This->wHotKey;
980 header.nIcon = This->iIcoNdx;
981 header.dwFlags = SCF_UNICODE; /* strings are in unicode */
983 header.dwFlags |= SCF_PIDL;
985 header.dwFlags |= SCF_LOCATION;
986 if( This->sDescription )
987 header.dwFlags |= SCF_DESCRIPTION;
989 header.dwFlags |= SCF_WORKDIR;
991 header.dwFlags |= SCF_ARGS;
993 header.dwFlags |= SCF_CUSTOMICON;
995 header.dwFlags |= SCF_PRODUCT;
996 if( This->sComponent )
997 header.dwFlags |= SCF_COMPONENT;
999 SystemTimeToFileTime ( &This->time1, &header.Time1 );
1000 SystemTimeToFileTime ( &This->time2, &header.Time2 );
1001 SystemTimeToFileTime ( &This->time3, &header.Time3 );
1003 /* write the Shortcut header */
1004 r = IStream_Write( stm, &header, sizeof(header), &count );
1007 ERR("Write failed at %d\n",__LINE__);
1011 TRACE("Writing pidl \n");
1013 /* write the PIDL to the shortcut */
1016 r = ILSaveToStream( stm, This->pPidl );
1019 ERR("Failed to write PIDL at %d\n",__LINE__);
1025 Stream_WriteLocationInfo( stm, exePath, &This->volume );
1027 if( This->sDescription )
1028 r = Stream_WriteString( stm, This->sDescription );
1030 if( This->sPathRel )
1031 r = Stream_WriteString( stm, This->sPathRel );
1033 if( This->sWorkDir )
1034 r = Stream_WriteString( stm, This->sWorkDir );
1037 r = Stream_WriteString( stm, This->sArgs );
1039 if( This->sIcoPath )
1040 r = Stream_WriteString( stm, This->sIcoPath );
1042 if( This->sProduct )
1043 r = Stream_WriteAdvertiseInfo( stm, This->sProduct, 0xa0000007 );
1045 if( This->sComponent )
1046 r = Stream_WriteAdvertiseInfo( stm, This->sComponent, 0xa0000006 );
1048 /* the last field is a single zero dword */
1050 r = IStream_Write( stm, &zero, sizeof zero, &count );
1055 /************************************************************************
1056 * IPersistStream_GetSizeMax (IPersistStream)
1058 static HRESULT WINAPI IPersistStream_fnGetSizeMax(
1059 IPersistStream* iface,
1060 ULARGE_INTEGER* pcbSize)
1062 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
1064 TRACE("(%p)\n", This);
1069 static IPersistStreamVtbl psvt =
1071 IPersistStream_fnQueryInterface,
1072 IPersistStream_fnAddRef,
1073 IPersistStream_fnRelease,
1074 IPersistStream_fnGetClassID,
1075 IPersistStream_fnIsDirty,
1076 IPersistStream_fnLoad,
1077 IPersistStream_fnSave,
1078 IPersistStream_fnGetSizeMax
1081 /**************************************************************************
1082 * IShellLink_Constructor
1084 HRESULT WINAPI IShellLink_Constructor (
1085 IUnknown * pUnkOuter,
1089 IShellLinkImpl * sl;
1091 TRACE("unkOut=%p riid=%s\n",pUnkOuter, debugstr_guid(riid));
1095 if(pUnkOuter) return CLASS_E_NOAGGREGATION;
1096 sl = (IShellLinkImpl *) LocalAlloc(LMEM_ZEROINIT,sizeof(IShellLinkImpl));
1097 if (!sl) return E_OUTOFMEMORY;
1101 sl->lpvtblw = &slvtw;
1102 sl->lpvtblPersistFile = &pfvt;
1103 sl->lpvtblPersistStream = &psvt;
1104 sl->iShowCmd = SW_SHOWNORMAL;
1107 TRACE("(%p)->()\n",sl);
1109 if (IsEqualIID(riid, &IID_IUnknown) ||
1110 IsEqualIID(riid, &IID_IShellLinkA))
1112 else if (IsEqualIID(riid, &IID_IShellLinkW))
1113 *ppv = &(sl->lpvtblw);
1115 LocalFree((HLOCAL)sl);
1116 ERR("E_NOINTERFACE\n");
1117 return E_NOINTERFACE;
1124 static BOOL SHELL_ExistsFileW(LPCWSTR path)
1126 if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW(path))
1131 /**************************************************************************
1132 * ShellLink_UpdatePath
1133 * update absolute path in sPath using relative path in sPathRel
1135 static HRESULT ShellLink_UpdatePath(LPWSTR sPathRel, LPCWSTR path, LPCWSTR sWorkDir, LPWSTR* psPath)
1137 if (!path || !psPath)
1138 return E_INVALIDARG;
1140 if (!*psPath && sPathRel) {
1141 WCHAR buffer[2*MAX_PATH], abs_path[2*MAX_PATH];
1142 LPWSTR final = NULL;
1144 /* first try if [directory of link file] + [relative path] finds an existing file */
1146 GetFullPathNameW( path, MAX_PATH*2, buffer, &final );
1149 lstrcpyW(final, sPathRel);
1153 if (SHELL_ExistsFileW(buffer)) {
1154 if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
1155 lstrcpyW(abs_path, buffer);
1157 /* try if [working directory] + [relative path] finds an existing file */
1159 lstrcpyW(buffer, sWorkDir);
1160 lstrcpyW(PathAddBackslashW(buffer), sPathRel);
1162 if (SHELL_ExistsFileW(buffer))
1163 if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
1164 lstrcpyW(abs_path, buffer);
1168 /* FIXME: This is even not enough - not all shell links can be resolved using this algorithm. */
1170 lstrcpyW(abs_path, sPathRel);
1172 *psPath = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(abs_path)+1)*sizeof(WCHAR));
1174 return E_OUTOFMEMORY;
1176 lstrcpyW(*psPath, abs_path);
1182 /**************************************************************************
1183 * IShellLink_ConstructFromFile
1185 HRESULT WINAPI IShellLink_ConstructFromFile (
1186 IUnknown* pUnkOuter,
1194 HRESULT hr = IShellLink_Constructor(NULL, riid, (LPVOID*)&psl);
1196 if (SUCCEEDED(hr)) {
1201 hr = IShellLinkW_QueryInterface(psl, &IID_IPersistFile, (LPVOID*)&ppf);
1203 if (SUCCEEDED(hr)) {
1204 WCHAR path[MAX_PATH];
1206 if (SHGetPathFromIDListW(pidl, path))
1207 hr = IPersistFile_Load(ppf, path, 0);
1212 *ppv = (IUnknown*) psl;
1214 IPersistFile_Release(ppf);
1218 IShellLinkW_Release(psl);
1224 /**************************************************************************
1225 * IShellLinkA_QueryInterface
1227 static HRESULT WINAPI IShellLinkA_fnQueryInterface( IShellLinkA * iface, REFIID riid, LPVOID *ppvObj)
1229 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1231 TRACE("(%p)->(\n\tIID:\t%s)\n",This,debugstr_guid(riid));
1235 if(IsEqualIID(riid, &IID_IUnknown) ||
1236 IsEqualIID(riid, &IID_IShellLinkA))
1240 else if(IsEqualIID(riid, &IID_IShellLinkW))
1242 *ppvObj = (IShellLinkW *)&(This->lpvtblw);
1244 else if(IsEqualIID(riid, &IID_IPersistFile))
1246 *ppvObj = (IPersistFile *)&(This->lpvtblPersistFile);
1248 else if(IsEqualIID(riid, &IID_IPersistStream))
1250 *ppvObj = (IPersistStream *)&(This->lpvtblPersistStream);
1255 IUnknown_AddRef((IUnknown*)(*ppvObj));
1256 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
1259 TRACE("-- Interface: E_NOINTERFACE\n");
1260 return E_NOINTERFACE;
1262 /******************************************************************************
1263 * IShellLinkA_AddRef
1265 static ULONG WINAPI IShellLinkA_fnAddRef(IShellLinkA * iface)
1267 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1268 ULONG refCount = InterlockedIncrement(&This->ref);
1270 TRACE("(%p)->(count=%lu)\n", This, refCount - 1);
1274 /******************************************************************************
1275 * IShellLinkA_Release
1277 static ULONG WINAPI IShellLinkA_fnRelease(IShellLinkA * iface)
1279 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1280 ULONG refCount = InterlockedDecrement(&This->ref);
1282 TRACE("(%p)->(count=%lu)\n", This, refCount + 1);
1287 TRACE("-- destroying IShellLink(%p)\n",This);
1289 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
1290 HeapFree(GetProcessHeap(), 0, This->sArgs);
1291 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1292 HeapFree(GetProcessHeap(), 0, This->sDescription);
1293 HeapFree(GetProcessHeap(),0,This->sPath);
1296 ILFree(This->pPidl);
1298 LocalFree((HANDLE)This);
1303 static HRESULT WINAPI IShellLinkA_fnGetPath(IShellLinkA * iface, LPSTR pszFile,
1304 INT cchMaxPath, WIN32_FIND_DATAA *pfd, DWORD fFlags)
1306 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1308 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%lu)(%s)\n",
1309 This, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(This->sPath));
1311 if (This->sComponent || This->sProduct)
1317 WideCharToMultiByte( CP_ACP, 0, This->sPath, -1,
1318 pszFile, cchMaxPath, NULL, NULL);
1320 if (pfd) FIXME("(%p): WIN32_FIND_DATA is not yet filled.\n", This);
1325 static HRESULT WINAPI IShellLinkA_fnGetIDList(IShellLinkA * iface, LPITEMIDLIST * ppidl)
1327 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1329 TRACE("(%p)->(ppidl=%p)\n",This, ppidl);
1331 return IShellLinkW_GetIDList((IShellLinkW*)&(This->lpvtblw), ppidl);
1334 static HRESULT WINAPI IShellLinkA_fnSetIDList(IShellLinkA * iface, LPCITEMIDLIST pidl)
1336 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1338 TRACE("(%p)->(pidl=%p)\n",This, pidl);
1341 ILFree(This->pPidl);
1342 This->pPidl = ILClone (pidl);
1343 This->bDirty = TRUE;
1348 static HRESULT WINAPI IShellLinkA_fnGetDescription(IShellLinkA * iface, LPSTR pszName,INT cchMaxName)
1350 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1352 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1356 if( This->sDescription )
1357 WideCharToMultiByte( CP_ACP, 0, This->sDescription, -1,
1358 pszName, cchMaxName, NULL, NULL);
1362 static HRESULT WINAPI IShellLinkA_fnSetDescription(IShellLinkA * iface, LPCSTR pszName)
1364 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1366 TRACE("(%p)->(pName=%s)\n", This, pszName);
1368 HeapFree(GetProcessHeap(), 0, This->sDescription);
1369 This->sDescription = HEAP_strdupAtoW( GetProcessHeap(), 0, pszName);
1370 if ( !This->sDescription )
1371 return E_OUTOFMEMORY;
1373 This->bDirty = TRUE;
1378 static HRESULT WINAPI IShellLinkA_fnGetWorkingDirectory(IShellLinkA * iface, LPSTR pszDir,INT cchMaxPath)
1380 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1382 TRACE("(%p)->(%p len=%u)\n", This, pszDir, cchMaxPath);
1386 if( This->sWorkDir )
1387 WideCharToMultiByte( CP_ACP, 0, This->sWorkDir, -1,
1388 pszDir, cchMaxPath, NULL, NULL);
1393 static HRESULT WINAPI IShellLinkA_fnSetWorkingDirectory(IShellLinkA * iface, LPCSTR pszDir)
1395 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1397 TRACE("(%p)->(dir=%s)\n",This, pszDir);
1399 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1400 This->sWorkDir = HEAP_strdupAtoW( GetProcessHeap(), 0, pszDir);
1401 if ( !This->sWorkDir )
1402 return E_OUTOFMEMORY;
1404 This->bDirty = TRUE;
1409 static HRESULT WINAPI IShellLinkA_fnGetArguments(IShellLinkA * iface, LPSTR pszArgs,INT cchMaxPath)
1411 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1413 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1418 WideCharToMultiByte( CP_ACP, 0, This->sArgs, -1,
1419 pszArgs, cchMaxPath, NULL, NULL);
1424 static HRESULT WINAPI IShellLinkA_fnSetArguments(IShellLinkA * iface, LPCSTR pszArgs)
1426 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1428 TRACE("(%p)->(args=%s)\n",This, pszArgs);
1430 HeapFree(GetProcessHeap(), 0, This->sArgs);
1431 This->sArgs = HEAP_strdupAtoW( GetProcessHeap(), 0, pszArgs);
1433 return E_OUTOFMEMORY;
1435 This->bDirty = TRUE;
1440 static HRESULT WINAPI IShellLinkA_fnGetHotkey(IShellLinkA * iface, WORD *pwHotkey)
1442 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1444 TRACE("(%p)->(%p)(0x%08x)\n",This, pwHotkey, This->wHotKey);
1446 *pwHotkey = This->wHotKey;
1451 static HRESULT WINAPI IShellLinkA_fnSetHotkey(IShellLinkA * iface, WORD wHotkey)
1453 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1455 TRACE("(%p)->(hotkey=%x)\n",This, wHotkey);
1457 This->wHotKey = wHotkey;
1458 This->bDirty = TRUE;
1463 static HRESULT WINAPI IShellLinkA_fnGetShowCmd(IShellLinkA * iface, INT *piShowCmd)
1465 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1467 TRACE("(%p)->(%p)\n",This, piShowCmd);
1468 *piShowCmd = This->iShowCmd;
1472 static HRESULT WINAPI IShellLinkA_fnSetShowCmd(IShellLinkA * iface, INT iShowCmd)
1474 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1476 TRACE("(%p) %d\n",This, iShowCmd);
1478 This->iShowCmd = iShowCmd;
1479 This->bDirty = TRUE;
1484 static HRESULT SHELL_PidlGeticonLocationA(IShellFolder* psf, LPITEMIDLIST pidl, LPSTR pszIconPath, int cchIconPath, int* piIcon)
1486 LPCITEMIDLIST pidlLast;
1488 HRESULT hr = SHBindToParent(pidl, &IID_IShellFolder, (LPVOID*)&psf, &pidlLast);
1490 if (SUCCEEDED(hr)) {
1493 hr = IShellFolder_GetUIObjectOf(psf, 0, 1, (LPCITEMIDLIST*)&pidlLast, &IID_IExtractIconA, NULL, (LPVOID*)&pei);
1495 if (SUCCEEDED(hr)) {
1496 hr = IExtractIconA_GetIconLocation(pei, 0, pszIconPath, MAX_PATH, piIcon, NULL);
1498 IExtractIconA_Release(pei);
1501 IShellFolder_Release(psf);
1507 static HRESULT WINAPI IShellLinkA_fnGetIconLocation(IShellLinkA * iface, LPSTR pszIconPath,INT cchIconPath,INT *piIcon)
1509 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1511 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1514 *piIcon = This->iIcoNdx;
1518 WideCharToMultiByte(CP_ACP, 0, This->sIcoPath, -1, pszIconPath, cchIconPath, NULL, NULL);
1522 if (This->pPidl || This->sPath)
1526 HRESULT hr = SHGetDesktopFolder(&pdsk);
1530 /* first look for an icon using the PIDL (if present) */
1532 hr = SHELL_PidlGeticonLocationA(pdsk, This->pPidl, pszIconPath, cchIconPath, piIcon);
1536 /* if we couldn't find an icon yet, look for it using the file system path */
1537 if (FAILED(hr) && This->sPath)
1541 hr = IShellFolder_ParseDisplayName(pdsk, 0, NULL, This->sPath, NULL, &pidl, NULL);
1543 if (SUCCEEDED(hr)) {
1544 hr = SHELL_PidlGeticonLocationA(pdsk, pidl, pszIconPath, cchIconPath, piIcon);
1550 IShellFolder_Release(pdsk);
1558 static HRESULT WINAPI IShellLinkA_fnSetIconLocation(IShellLinkA * iface, LPCSTR pszIconPath,INT iIcon)
1560 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1562 TRACE("(%p)->(path=%s iicon=%u)\n",This, pszIconPath, iIcon);
1564 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
1565 This->sIcoPath = HEAP_strdupAtoW(GetProcessHeap(), 0, pszIconPath);
1566 if ( !This->sIcoPath )
1567 return E_OUTOFMEMORY;
1569 This->iIcoNdx = iIcon;
1570 This->bDirty = TRUE;
1575 static HRESULT WINAPI IShellLinkA_fnSetRelativePath(IShellLinkA * iface, LPCSTR pszPathRel, DWORD dwReserved)
1577 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1579 TRACE("(%p)->(path=%s %lx)\n",This, pszPathRel, dwReserved);
1581 HeapFree(GetProcessHeap(), 0, This->sPathRel);
1582 This->sPathRel = HEAP_strdupAtoW(GetProcessHeap(), 0, pszPathRel);
1583 This->bDirty = TRUE;
1585 return ShellLink_UpdatePath(This->sPathRel, This->sPath, This->sWorkDir, &This->sPath);
1588 static HRESULT WINAPI IShellLinkA_fnResolve(IShellLinkA * iface, HWND hwnd, DWORD fFlags)
1592 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1594 TRACE("(%p)->(hwnd=%p flags=%lx)\n",This, hwnd, fFlags);
1596 /*FIXME: use IResolveShellLink interface */
1598 if (!This->sPath && This->pPidl) {
1599 WCHAR buffer[MAX_PATH];
1601 hr = SHELL_GetPathFromIDListW(This->pPidl, buffer, MAX_PATH);
1603 if (SUCCEEDED(hr) && *buffer) {
1604 This->sPath = (LPWSTR) HeapAlloc(GetProcessHeap(), 0, (lstrlenW(buffer)+1)*sizeof(WCHAR));
1606 return E_OUTOFMEMORY;
1608 lstrcpyW(This->sPath, buffer);
1610 This->bDirty = TRUE;
1612 hr = S_OK; /* don't report an error occurred while just caching information */
1615 if (!This->sIcoPath && This->sPath) {
1616 This->sIcoPath = (LPWSTR) HeapAlloc(GetProcessHeap(), 0, (lstrlenW(This->sPath)+1)*sizeof(WCHAR));
1617 if (!This->sIcoPath)
1618 return E_OUTOFMEMORY;
1620 lstrcpyW(This->sIcoPath, This->sPath);
1623 This->bDirty = TRUE;
1629 static HRESULT WINAPI IShellLinkA_fnSetPath(IShellLinkA * iface, LPCSTR pszFile)
1633 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1635 TRACE("(%p)->(path=%s)\n",This, pszFile);
1637 str = HEAP_strdupAtoW(GetProcessHeap(), 0, pszFile);
1639 return E_OUTOFMEMORY;
1641 r = IShellLinkW_SetPath((IShellLinkW*)&(This->lpvtblw), str);
1642 HeapFree( GetProcessHeap(), 0, str );
1647 /**************************************************************************
1648 * IShellLink Implementation
1651 static IShellLinkAVtbl slvt =
1653 IShellLinkA_fnQueryInterface,
1654 IShellLinkA_fnAddRef,
1655 IShellLinkA_fnRelease,
1656 IShellLinkA_fnGetPath,
1657 IShellLinkA_fnGetIDList,
1658 IShellLinkA_fnSetIDList,
1659 IShellLinkA_fnGetDescription,
1660 IShellLinkA_fnSetDescription,
1661 IShellLinkA_fnGetWorkingDirectory,
1662 IShellLinkA_fnSetWorkingDirectory,
1663 IShellLinkA_fnGetArguments,
1664 IShellLinkA_fnSetArguments,
1665 IShellLinkA_fnGetHotkey,
1666 IShellLinkA_fnSetHotkey,
1667 IShellLinkA_fnGetShowCmd,
1668 IShellLinkA_fnSetShowCmd,
1669 IShellLinkA_fnGetIconLocation,
1670 IShellLinkA_fnSetIconLocation,
1671 IShellLinkA_fnSetRelativePath,
1672 IShellLinkA_fnResolve,
1673 IShellLinkA_fnSetPath
1677 /**************************************************************************
1678 * IShellLinkW_fnQueryInterface
1680 static HRESULT WINAPI IShellLinkW_fnQueryInterface(
1681 IShellLinkW * iface, REFIID riid, LPVOID *ppvObj)
1683 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1685 return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvObj);
1688 /******************************************************************************
1689 * IShellLinkW_fnAddRef
1691 static ULONG WINAPI IShellLinkW_fnAddRef(IShellLinkW * iface)
1693 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1695 TRACE("(%p)->(count=%lu)\n",This,This->ref);
1697 return IShellLinkA_AddRef((IShellLinkA*)This);
1699 /******************************************************************************
1700 * IShellLinkW_fnRelease
1703 static ULONG WINAPI IShellLinkW_fnRelease(IShellLinkW * iface)
1705 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1707 TRACE("(%p)->(count=%lu)\n",This,This->ref);
1709 return IShellLinkA_Release((IShellLinkA*)This);
1712 static HRESULT WINAPI IShellLinkW_fnGetPath(IShellLinkW * iface, LPWSTR pszFile,INT cchMaxPath, WIN32_FIND_DATAW *pfd, DWORD fFlags)
1714 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1716 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%lu)(%s)\n",
1717 This, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(This->sPath));
1719 if (This->sComponent || This->sProduct)
1725 lstrcpynW( pszFile, This->sPath, cchMaxPath );
1727 if (pfd) FIXME("(%p): WIN32_FIND_DATA is not yet filled.\n", This);
1732 static HRESULT WINAPI IShellLinkW_fnGetIDList(IShellLinkW * iface, LPITEMIDLIST * ppidl)
1734 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1736 TRACE("(%p)->(ppidl=%p)\n",This, ppidl);
1740 *ppidl = ILClone(This->pPidl);
1744 static HRESULT WINAPI IShellLinkW_fnSetIDList(IShellLinkW * iface, LPCITEMIDLIST pidl)
1746 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1748 TRACE("(%p)->(pidl=%p)\n",This, pidl);
1751 ILFree( This->pPidl );
1752 This->pPidl = ILClone( pidl );
1756 This->bDirty = TRUE;
1761 static HRESULT WINAPI IShellLinkW_fnGetDescription(IShellLinkW * iface, LPWSTR pszName,INT cchMaxName)
1763 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1765 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1768 if( This->sDescription )
1769 lstrcpynW( pszName, This->sDescription, cchMaxName );
1774 static HRESULT WINAPI IShellLinkW_fnSetDescription(IShellLinkW * iface, LPCWSTR pszName)
1776 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1778 TRACE("(%p)->(desc=%s)\n",This, debugstr_w(pszName));
1780 HeapFree(GetProcessHeap(), 0, This->sDescription);
1781 This->sDescription = HeapAlloc( GetProcessHeap(), 0,
1782 (lstrlenW( pszName )+1)*sizeof(WCHAR) );
1783 if ( !This->sDescription )
1784 return E_OUTOFMEMORY;
1786 lstrcpyW( This->sDescription, pszName );
1787 This->bDirty = TRUE;
1792 static HRESULT WINAPI IShellLinkW_fnGetWorkingDirectory(IShellLinkW * iface, LPWSTR pszDir,INT cchMaxPath)
1794 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1796 TRACE("(%p)->(%p len %u)\n", This, pszDir, cchMaxPath);
1800 if( This->sWorkDir )
1801 lstrcpynW( pszDir, This->sWorkDir, cchMaxPath );
1806 static HRESULT WINAPI IShellLinkW_fnSetWorkingDirectory(IShellLinkW * iface, LPCWSTR pszDir)
1808 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1810 TRACE("(%p)->(dir=%s)\n",This, debugstr_w(pszDir));
1812 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1813 This->sWorkDir = HeapAlloc( GetProcessHeap(), 0,
1814 (lstrlenW( pszDir )+1)*sizeof (WCHAR) );
1815 if ( !This->sWorkDir )
1816 return E_OUTOFMEMORY;
1817 lstrcpyW( This->sWorkDir, pszDir );
1818 This->bDirty = TRUE;
1823 static HRESULT WINAPI IShellLinkW_fnGetArguments(IShellLinkW * iface, LPWSTR pszArgs,INT cchMaxPath)
1825 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1827 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1832 lstrcpynW( pszArgs, This->sArgs, cchMaxPath );
1837 static HRESULT WINAPI IShellLinkW_fnSetArguments(IShellLinkW * iface, LPCWSTR pszArgs)
1839 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1841 TRACE("(%p)->(args=%s)\n",This, debugstr_w(pszArgs));
1843 HeapFree(GetProcessHeap(), 0, This->sArgs);
1844 This->sArgs = HeapAlloc( GetProcessHeap(), 0,
1845 (lstrlenW( pszArgs )+1)*sizeof (WCHAR) );
1847 return E_OUTOFMEMORY;
1848 lstrcpyW( This->sArgs, pszArgs );
1849 This->bDirty = TRUE;
1854 static HRESULT WINAPI IShellLinkW_fnGetHotkey(IShellLinkW * iface, WORD *pwHotkey)
1856 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1858 TRACE("(%p)->(%p)\n",This, pwHotkey);
1860 *pwHotkey=This->wHotKey;
1865 static HRESULT WINAPI IShellLinkW_fnSetHotkey(IShellLinkW * iface, WORD wHotkey)
1867 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1869 TRACE("(%p)->(hotkey=%x)\n",This, wHotkey);
1871 This->wHotKey = wHotkey;
1872 This->bDirty = TRUE;
1877 static HRESULT WINAPI IShellLinkW_fnGetShowCmd(IShellLinkW * iface, INT *piShowCmd)
1879 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1881 TRACE("(%p)->(%p)\n",This, piShowCmd);
1883 *piShowCmd = This->iShowCmd;
1888 static HRESULT WINAPI IShellLinkW_fnSetShowCmd(IShellLinkW * iface, INT iShowCmd)
1890 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1892 This->iShowCmd = iShowCmd;
1893 This->bDirty = TRUE;
1898 static HRESULT SHELL_PidlGeticonLocationW(IShellFolder* psf, LPITEMIDLIST pidl, LPWSTR pszIconPath, int cchIconPath, int* piIcon)
1900 LPCITEMIDLIST pidlLast;
1902 HRESULT hr = SHBindToParent(pidl, &IID_IShellFolder, (LPVOID*)&psf, &pidlLast);
1904 if (SUCCEEDED(hr)) {
1907 hr = IShellFolder_GetUIObjectOf(psf, 0, 1, (LPCITEMIDLIST*)&pidlLast, &IID_IExtractIconW, NULL, (LPVOID*)&pei);
1909 if (SUCCEEDED(hr)) {
1910 hr = IExtractIconW_GetIconLocation(pei, 0, pszIconPath, MAX_PATH, piIcon, NULL);
1912 IExtractIconW_Release(pei);
1915 IShellFolder_Release(psf);
1921 static HRESULT WINAPI IShellLinkW_fnGetIconLocation(IShellLinkW * iface, LPWSTR pszIconPath,INT cchIconPath,INT *piIcon)
1923 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1925 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1928 *piIcon = This->iIcoNdx;
1932 lstrcpynW(pszIconPath, This->sIcoPath, cchIconPath);
1936 if (This->pPidl || This->sPath)
1940 HRESULT hr = SHGetDesktopFolder(&pdsk);
1944 /* first look for an icon using the PIDL (if present) */
1946 hr = SHELL_PidlGeticonLocationW(pdsk, This->pPidl, pszIconPath, cchIconPath, piIcon);
1950 /* if we couldn't find an icon yet, look for it using the file system path */
1951 if (FAILED(hr) && This->sPath)
1955 hr = IShellFolder_ParseDisplayName(pdsk, 0, NULL, This->sPath, NULL, &pidl, NULL);
1959 hr = SHELL_PidlGeticonLocationW(pdsk, pidl, pszIconPath, cchIconPath, piIcon);
1965 IShellFolder_Release(pdsk);
1972 static HRESULT WINAPI IShellLinkW_fnSetIconLocation(IShellLinkW * iface, LPCWSTR pszIconPath,INT iIcon)
1974 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1976 TRACE("(%p)->(path=%s iicon=%u)\n",This, debugstr_w(pszIconPath), iIcon);
1978 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
1979 This->sIcoPath = HeapAlloc( GetProcessHeap(), 0,
1980 (lstrlenW( pszIconPath )+1)*sizeof (WCHAR) );
1981 if ( !This->sIcoPath )
1982 return E_OUTOFMEMORY;
1983 lstrcpyW( This->sIcoPath, pszIconPath );
1985 This->iIcoNdx = iIcon;
1986 This->bDirty = TRUE;
1991 static HRESULT WINAPI IShellLinkW_fnSetRelativePath(IShellLinkW * iface, LPCWSTR pszPathRel, DWORD dwReserved)
1993 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1995 TRACE("(%p)->(path=%s %lx)\n",This, debugstr_w(pszPathRel), dwReserved);
1997 HeapFree(GetProcessHeap(), 0, This->sPathRel);
1998 This->sPathRel = HeapAlloc( GetProcessHeap(), 0,
1999 (lstrlenW( pszPathRel )+1) * sizeof (WCHAR) );
2000 if ( !This->sPathRel )
2001 return E_OUTOFMEMORY;
2002 lstrcpyW( This->sPathRel, pszPathRel );
2003 This->bDirty = TRUE;
2005 return ShellLink_UpdatePath(This->sPathRel, This->sPath, This->sWorkDir, &This->sPath);
2008 static HRESULT WINAPI IShellLinkW_fnResolve(IShellLinkW * iface, HWND hwnd, DWORD fFlags)
2012 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
2014 TRACE("(%p)->(hwnd=%p flags=%lx)\n",This, hwnd, fFlags);
2016 /*FIXME: use IResolveShellLink interface */
2018 if (!This->sPath && This->pPidl) {
2019 WCHAR buffer[MAX_PATH];
2021 hr = SHELL_GetPathFromIDListW(This->pPidl, buffer, MAX_PATH);
2023 if (SUCCEEDED(hr) && *buffer) {
2024 This->sPath = (LPWSTR) HeapAlloc(GetProcessHeap(), 0, (lstrlenW(buffer)+1)*sizeof(WCHAR));
2026 return E_OUTOFMEMORY;
2028 lstrcpyW(This->sPath, buffer);
2030 This->bDirty = TRUE;
2032 hr = S_OK; /* don't report an error occurred while just caching information */
2035 if (!This->sIcoPath && This->sPath) {
2036 This->sIcoPath = (LPWSTR) HeapAlloc(GetProcessHeap(), 0, (lstrlenW(This->sPath)+1)*sizeof(WCHAR));
2037 if (!This->sIcoPath)
2038 return E_OUTOFMEMORY;
2040 lstrcpyW(This->sIcoPath, This->sPath);
2043 This->bDirty = TRUE;
2049 static LPWSTR ShellLink_GetAdvertisedArg(LPCWSTR str)
2055 p = strchrW( str, ':' );
2059 ret = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR)*(len+1));
2062 memcpy( ret, str, sizeof(WCHAR)*len );
2067 static HRESULT ShellLink_SetAdvertiseInfo(IShellLinkImpl *This, LPCWSTR str)
2069 LPCWSTR szComponent = NULL, szProduct = NULL, p;
2077 /* each segment must start with two colons */
2078 if( str[0] != ':' || str[1] != ':' )
2081 /* the last segment is just two colons */
2086 /* there must be a colon straight after a guid */
2087 p = strchrW( str, ':' );
2094 /* get the guid, and check it's validly formatted */
2095 memcpy( szGuid, str, sizeof(WCHAR)*len );
2097 r = CLSIDFromString( szGuid, &guid );
2102 /* match it up to a guid that we care about */
2103 if( IsEqualGUID( &guid, &SHELL32_AdvtShortcutComponent ) && !szComponent )
2105 else if( IsEqualGUID( &guid, &SHELL32_AdvtShortcutProduct ) && !szProduct )
2110 /* skip to the next field */
2111 str = strchrW( str, ':' );
2116 /* we have to have at least one of these two for an advertised shortcut */
2117 if( !szComponent && !szProduct )
2120 This->sComponent = ShellLink_GetAdvertisedArg( szComponent );
2121 This->sProduct = ShellLink_GetAdvertisedArg( szProduct );
2123 TRACE("Component = %s\n", debugstr_w(This->sComponent));
2124 TRACE("Product = %s\n", debugstr_w(This->sProduct));
2129 static BOOL ShellLink_GetVolumeInfo(LPWSTR path, volume_info *volume)
2131 const int label_sz = sizeof volume->label/sizeof volume->label[0];
2132 WCHAR drive[4] = { path[0], ':', '\\', 0 };
2135 volume->type = GetDriveTypeW(drive);
2136 r = GetVolumeInformationW(drive, volume->label, label_sz,
2137 &volume->serial, NULL, NULL, NULL, 0);
2138 TRACE("r = %d type %ld serial %08lx name %s\n", r,
2139 volume->type, volume->serial, debugstr_w(volume->label));
2143 static HRESULT WINAPI IShellLinkW_fnSetPath(IShellLinkW * iface, LPCWSTR pszFile)
2145 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
2146 WCHAR buffer[MAX_PATH];
2150 TRACE("(%p)->(path=%s)\n",This, debugstr_w(pszFile));
2152 HeapFree(GetProcessHeap(), 0, This->sPath);
2155 HeapFree(GetProcessHeap(), 0, This->sComponent);
2156 This->sComponent = NULL;
2159 ILFree(This->pPidl);
2162 if (S_OK != ShellLink_SetAdvertiseInfo( This, pszFile ))
2164 if (*pszFile == '\0')
2166 else if (!GetFullPathNameW(pszFile, MAX_PATH, buffer, &fname))
2168 else if(!PathFileExistsW(buffer))
2171 This->pPidl = SHSimpleIDListFromPathW(pszFile);
2172 ShellLink_GetVolumeInfo(buffer, &This->volume);
2174 This->sPath = HeapAlloc( GetProcessHeap(), 0,
2175 (lstrlenW( buffer )+1) * sizeof (WCHAR) );
2177 return E_OUTOFMEMORY;
2179 lstrcpyW(This->sPath, buffer);
2181 This->bDirty = TRUE;
2186 /**************************************************************************
2187 * IShellLinkW Implementation
2190 static IShellLinkWVtbl slvtw =
2192 IShellLinkW_fnQueryInterface,
2193 IShellLinkW_fnAddRef,
2194 IShellLinkW_fnRelease,
2195 IShellLinkW_fnGetPath,
2196 IShellLinkW_fnGetIDList,
2197 IShellLinkW_fnSetIDList,
2198 IShellLinkW_fnGetDescription,
2199 IShellLinkW_fnSetDescription,
2200 IShellLinkW_fnGetWorkingDirectory,
2201 IShellLinkW_fnSetWorkingDirectory,
2202 IShellLinkW_fnGetArguments,
2203 IShellLinkW_fnSetArguments,
2204 IShellLinkW_fnGetHotkey,
2205 IShellLinkW_fnSetHotkey,
2206 IShellLinkW_fnGetShowCmd,
2207 IShellLinkW_fnSetShowCmd,
2208 IShellLinkW_fnGetIconLocation,
2209 IShellLinkW_fnSetIconLocation,
2210 IShellLinkW_fnSetRelativePath,
2211 IShellLinkW_fnResolve,
2212 IShellLinkW_fnSetPath