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.
37 #include "wine/debug.h"
47 #include "undocshell.h"
50 #include "shell32_main.h"
56 WINE_DEFAULT_DEBUG_CHANNEL(shell);
58 DEFINE_GUID( SHELL32_AdvtShortcutProduct,
59 0x9db1186f,0x40df,0x11d1,0xaa,0x8c,0x00,0xc0,0x4f,0xb6,0x78,0x63);
60 DEFINE_GUID( SHELL32_AdvtShortcutComponent,
61 0x9db1186e,0x40df,0x11d1,0xaa,0x8c,0x00,0xc0,0x4f,0xb6,0x78,0x63);
63 /* link file formats */
67 typedef struct _LINK_HEADER
69 DWORD dwSize; /* 0x00 size of the header - 0x4c */
70 GUID MagicGuid; /* 0x04 is CLSID_ShellLink */
71 DWORD dwFlags; /* 0x14 describes elements following */
72 DWORD dwFileAttr; /* 0x18 attributes of the target file */
73 FILETIME Time1; /* 0x1c */
74 FILETIME Time2; /* 0x24 */
75 FILETIME Time3; /* 0x2c */
76 DWORD dwFileLength; /* 0x34 File length */
77 DWORD nIcon; /* 0x38 icon number */
78 DWORD fStartup; /* 0x3c startup type */
79 DWORD wHotKey; /* 0x40 hotkey */
80 DWORD Unknown5; /* 0x44 */
81 DWORD Unknown6; /* 0x48 */
82 } LINK_HEADER, * PLINK_HEADER;
84 #define SHLINK_LOCAL 0
85 #define SHLINK_REMOTE 1
87 typedef struct _LOCATION_INFO
94 DWORD dwNetworkVolTableOfs;
98 typedef struct _LOCAL_VOLUME_INFO
106 typedef struct volume_info_t
110 WCHAR label[12]; /* assume 8.3 */
115 static const IShellLinkAVtbl slvt;
116 static const IShellLinkWVtbl slvtw;
117 static const IPersistFileVtbl pfvt;
118 static const IPersistStreamVtbl psvt;
120 /* IShellLink Implementation */
124 const IShellLinkAVtbl *lpVtbl;
127 const IShellLinkWVtbl *lpvtblw;
128 const IPersistFileVtbl *lpvtblPersistFile;
129 const IPersistStreamVtbl *lpvtblPersistStream;
131 /* data structures according to the informations in the link */
153 #define _IShellLinkW_Offset ((int)(&(((IShellLinkImpl*)0)->lpvtblw)))
154 #define _ICOM_THIS_From_IShellLinkW(class, name) class* This = (class*)(((char*)name)-_IShellLinkW_Offset)
156 #define _IPersistFile_Offset ((int)(&(((IShellLinkImpl*)0)->lpvtblPersistFile)))
157 #define _ICOM_THIS_From_IPersistFile(class, name) class* This = (class*)(((char*)name)-_IPersistFile_Offset)
159 #define _IPersistStream_Offset ((int)(&(((IShellLinkImpl*)0)->lpvtblPersistStream)))
160 #define _ICOM_THIS_From_IPersistStream(class, name) class* This = (class*)(((char*)name)-_IPersistStream_Offset)
162 static HRESULT ShellLink_UpdatePath(LPWSTR sPathRel, LPCWSTR path, LPCWSTR sWorkDir, LPWSTR* psPath);
164 /* strdup on the process heap */
165 inline static LPWSTR HEAP_strdupAtoW( HANDLE heap, DWORD flags, LPCSTR str)
167 INT len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
168 LPWSTR p = HeapAlloc( heap, flags, len*sizeof (WCHAR) );
171 MultiByteToWideChar( CP_ACP, 0, str, -1, p, len );
175 /**************************************************************************
176 * IPersistFile_QueryInterface
178 static HRESULT WINAPI IPersistFile_fnQueryInterface(
183 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
185 TRACE("(%p)\n",This);
187 return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvObj);
190 /******************************************************************************
191 * IPersistFile_AddRef
193 static ULONG WINAPI IPersistFile_fnAddRef(IPersistFile* iface)
195 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
197 TRACE("(%p)->(count=%lu)\n",This,This->ref);
199 return IShellLinkA_AddRef((IShellLinkA*)This);
201 /******************************************************************************
202 * IPersistFile_Release
204 static ULONG WINAPI IPersistFile_fnRelease(IPersistFile* iface)
206 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
208 TRACE("(%p)->(count=%lu)\n",This,This->ref);
210 return IShellLinkA_Release((IShellLinkA*)This);
213 static HRESULT WINAPI IPersistFile_fnGetClassID(IPersistFile* iface, CLSID *pClassID)
215 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
216 FIXME("(%p)\n",This);
219 static HRESULT WINAPI IPersistFile_fnIsDirty(IPersistFile* iface)
221 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
223 TRACE("(%p)\n",This);
230 static HRESULT WINAPI IPersistFile_fnLoad(IPersistFile* iface, LPCOLESTR pszFileName, DWORD dwMode)
232 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
233 IPersistStream *StreamThis = (IPersistStream *)&This->lpvtblPersistStream;
237 TRACE("(%p, %s, %lx)\n",This, debugstr_w(pszFileName), dwMode);
239 r = CreateStreamOnFile(pszFileName, dwMode, &stm);
242 r = IPersistStream_Load(StreamThis, stm);
243 ShellLink_UpdatePath(This->sPathRel, pszFileName, This->sWorkDir, &This->sPath);
244 IStream_Release( stm );
245 This->bDirty = FALSE;
247 TRACE("-- returning hr %08lx\n", r);
251 static BOOL StartLinkProcessor( LPCOLESTR szLink )
253 static const WCHAR szFormat[] = {
254 'w','i','n','e','m','e','n','u','b','u','i','l','d','e','r','.','e','x','e',
255 ' ','-','r',' ','"','%','s','"',0 };
259 PROCESS_INFORMATION pi;
261 len = sizeof(szFormat) + lstrlenW( szLink ) * sizeof(WCHAR);
262 buffer = HeapAlloc( GetProcessHeap(), 0, len );
266 wsprintfW( buffer, szFormat, szLink );
268 TRACE("starting %s\n",debugstr_w(buffer));
270 memset(&si, 0, sizeof(si));
272 if (!CreateProcessW( NULL, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) return FALSE;
274 /* wait for a while to throttle the creation of linker processes */
275 if( WAIT_OBJECT_0 != WaitForSingleObject( pi.hProcess, 10000 ) )
276 WARN("Timed out waiting for shell linker\n");
278 CloseHandle( pi.hProcess );
279 CloseHandle( pi.hThread );
284 static HRESULT WINAPI IPersistFile_fnSave(IPersistFile* iface, LPCOLESTR pszFileName, BOOL fRemember)
286 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
287 IPersistStream *StreamThis = (IPersistStream *)&This->lpvtblPersistStream;
291 TRACE("(%p)->(%s)\n",This,debugstr_w(pszFileName));
296 r = CreateStreamOnFile(pszFileName, STGM_READWRITE | STGM_CREATE, &stm);
299 r = IPersistStream_Save(StreamThis, stm, FALSE);
300 IStream_Release( stm );
304 StartLinkProcessor( pszFileName );
306 This->bDirty = FALSE;
310 DeleteFileW( pszFileName );
311 WARN("Failed to create shortcut %s\n", debugstr_w(pszFileName) );
318 static HRESULT WINAPI IPersistFile_fnSaveCompleted(IPersistFile* iface, LPCOLESTR pszFileName)
320 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
321 FIXME("(%p)->(%s)\n",This,debugstr_w(pszFileName));
324 static HRESULT WINAPI IPersistFile_fnGetCurFile(IPersistFile* iface, LPOLESTR *ppszFileName)
326 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
327 FIXME("(%p)\n",This);
331 static const IPersistFileVtbl pfvt =
333 IPersistFile_fnQueryInterface,
334 IPersistFile_fnAddRef,
335 IPersistFile_fnRelease,
336 IPersistFile_fnGetClassID,
337 IPersistFile_fnIsDirty,
340 IPersistFile_fnSaveCompleted,
341 IPersistFile_fnGetCurFile
344 /************************************************************************
345 * IPersistStream_QueryInterface
347 static HRESULT WINAPI IPersistStream_fnQueryInterface(
348 IPersistStream* iface,
352 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
354 TRACE("(%p)\n",This);
356 return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvoid);
359 /************************************************************************
360 * IPersistStream_Release
362 static ULONG WINAPI IPersistStream_fnRelease(
363 IPersistStream* iface)
365 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
367 TRACE("(%p)\n",This);
369 return IShellLinkA_Release((IShellLinkA*)This);
372 /************************************************************************
373 * IPersistStream_AddRef
375 static ULONG WINAPI IPersistStream_fnAddRef(
376 IPersistStream* iface)
378 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
380 TRACE("(%p)\n",This);
382 return IShellLinkA_AddRef((IShellLinkA*)This);
385 /************************************************************************
386 * IPersistStream_GetClassID
389 static HRESULT WINAPI IPersistStream_fnGetClassID(
390 IPersistStream* iface,
393 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
395 TRACE("(%p)\n", This);
400 /* memcpy(pClassID, &CLSID_???, sizeof(CLSID_???)); */
405 /************************************************************************
406 * IPersistStream_IsDirty (IPersistStream)
408 static HRESULT WINAPI IPersistStream_fnIsDirty(
409 IPersistStream* iface)
411 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
413 TRACE("(%p)\n", This);
419 static HRESULT Stream_LoadString( IStream* stm, BOOL unicode, LPWSTR *pstr )
430 r = IStream_Read(stm, &len, sizeof(len), &count);
431 if ( FAILED (r) || ( count != sizeof(len) ) )
435 len *= sizeof (WCHAR);
437 TRACE("reading %d\n", len);
438 temp = HeapAlloc(GetProcessHeap(), 0, len+sizeof(WCHAR));
440 return E_OUTOFMEMORY;
442 r = IStream_Read(stm, temp, len, &count);
443 if( FAILED (r) || ( count != len ) )
445 HeapFree( GetProcessHeap(), 0, temp );
449 TRACE("read %s\n", debugstr_an(temp,len));
451 /* convert to unicode if necessary */
454 count = MultiByteToWideChar( CP_ACP, 0, (LPSTR) temp, len, NULL, 0 );
455 str = HeapAlloc( GetProcessHeap(), 0, (count+1)*sizeof (WCHAR) );
457 MultiByteToWideChar( CP_ACP, 0, (LPSTR) temp, len, str, count );
458 HeapFree( GetProcessHeap(), 0, temp );
472 static HRESULT Stream_ReadChunk( IStream* stm, LPVOID *data )
479 unsigned char data[1];
484 r = IStream_Read( stm, &size, sizeof(size), &count );
485 if( FAILED( r ) || count != sizeof(size) )
488 chunk = HeapAlloc( GetProcessHeap(), 0, size );
490 return E_OUTOFMEMORY;
493 r = IStream_Read( stm, chunk->data, size - sizeof(size), &count );
494 if( FAILED( r ) || count != (size - sizeof(size)) )
496 HeapFree( GetProcessHeap(), 0, chunk );
500 TRACE("Read %ld bytes\n",chunk->size);
502 *data = (LPVOID) chunk;
507 static BOOL Stream_LoadVolume( LOCAL_VOLUME_INFO *vol, volume_info *volume )
509 const int label_sz = sizeof volume->label/sizeof volume->label[0];
513 volume->serial = vol->dwVolSerial;
514 volume->type = vol->dwType;
516 if( !vol->dwVolLabelOfs )
518 if( vol->dwSize <= vol->dwVolLabelOfs )
520 len = vol->dwSize - vol->dwVolLabelOfs;
523 label += vol->dwVolLabelOfs;
524 MultiByteToWideChar( CP_ACP, 0, label, len, volume->label, label_sz-1);
529 static LPWSTR Stream_LoadPath( LPSTR p, DWORD maxlen )
534 while( p[len] && (len < maxlen) )
537 wlen = MultiByteToWideChar(CP_ACP, 0, p, len, NULL, 0);
538 path = HeapAlloc(GetProcessHeap(), 0, (wlen+1)*sizeof(WCHAR));
539 MultiByteToWideChar(CP_ACP, 0, p, len, path, wlen);
545 static HRESULT Stream_LoadLocation( IStream *stm,
546 volume_info *volume, LPWSTR *path )
548 unsigned char *p = NULL;
553 r = Stream_ReadChunk( stm, (LPVOID*) &p );
557 loc = (LOCATION_INFO*) p;
558 if (loc->dwTotalSize < sizeof(LOCATION_INFO))
560 HeapFree( GetProcessHeap(), 0, p );
564 /* if there's valid local volume information, load it */
565 if( loc->dwVolTableOfs &&
566 ((loc->dwVolTableOfs + sizeof(LOCAL_VOLUME_INFO)) <= loc->dwTotalSize) )
568 LOCAL_VOLUME_INFO *volume_info;
570 volume_info = (LOCAL_VOLUME_INFO*) &p[loc->dwVolTableOfs];
571 Stream_LoadVolume( volume_info, volume );
574 /* if there's a local path, load it */
575 n = loc->dwLocalPathOfs;
576 if( n && (n < loc->dwTotalSize) )
577 *path = Stream_LoadPath( &p[n], loc->dwTotalSize - n );
579 TRACE("type %ld serial %08lx name %s path %s\n", volume->type,
580 volume->serial, debugstr_w(volume->label), debugstr_w(*path));
582 HeapFree( GetProcessHeap(), 0, p );
587 * The format of the advertised shortcut info seems to be:
592 * 0 Length of the block (4 bytes, usually 0x314)
594 * 8 string data in ASCII
595 * 8+0x104 string data in UNICODE
597 * In the original Win32 implementation the buffers are not initialized
598 * to zero, so data trailing the string is random garbage.
600 static HRESULT Stream_LoadAdvertiseInfo( IStream* stm, LPWSTR *str )
605 EXP_DARWIN_LINK buffer;
609 r = IStream_Read( stm, &buffer.dbh.cbSize, sizeof (DWORD), &count );
613 /* make sure that we read the size of the structure even on error */
614 size = sizeof buffer - sizeof (DWORD);
615 if( buffer.dbh.cbSize != sizeof buffer )
617 ERR("Ooops. This structure is not as expected...\n");
621 r = IStream_Read( stm, &buffer.dbh.dwSignature, size, &count );
628 TRACE("magic %08lx string = %s\n", buffer.dbh.dwSignature, debugstr_w(buffer.szwDarwinID));
630 if( (buffer.dbh.dwSignature&0xffff0000) != 0xa0000000 )
632 ERR("Unknown magic number %08lx in advertised shortcut\n", buffer.dbh.dwSignature);
636 *str = HeapAlloc( GetProcessHeap(), 0,
637 (strlenW(buffer.szwDarwinID)+1) * sizeof(WCHAR) );
638 strcpyW( *str, buffer.szwDarwinID );
643 /************************************************************************
644 * IPersistStream_Load (IPersistStream)
646 static HRESULT WINAPI IPersistStream_fnLoad(
647 IPersistStream* iface,
656 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
658 TRACE("%p %p\n", This, stm);
661 return STG_E_INVALIDPOINTER;
664 r = IStream_Read(stm, &hdr, sizeof(hdr), &dwBytesRead);
668 if( dwBytesRead != sizeof(hdr))
670 if( hdr.dwSize != sizeof(hdr))
672 if( !IsEqualIID(&hdr.MagicGuid, &CLSID_ShellLink) )
675 /* free all the old stuff */
678 memset( &This->volume, 0, sizeof This->volume );
679 HeapFree(GetProcessHeap(), 0, This->sPath);
681 HeapFree(GetProcessHeap(), 0, This->sDescription);
682 This->sDescription = NULL;
683 HeapFree(GetProcessHeap(), 0, This->sPathRel);
684 This->sPathRel = NULL;
685 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
686 This->sWorkDir = NULL;
687 HeapFree(GetProcessHeap(), 0, This->sArgs);
689 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
690 This->sIcoPath = NULL;
691 HeapFree(GetProcessHeap(), 0, This->sProduct);
692 This->sProduct = NULL;
693 HeapFree(GetProcessHeap(), 0, This->sComponent);
694 This->sComponent = NULL;
696 This->wHotKey = (WORD)hdr.wHotKey;
697 This->iIcoNdx = hdr.nIcon;
698 FileTimeToSystemTime (&hdr.Time1, &This->time1);
699 FileTimeToSystemTime (&hdr.Time2, &This->time2);
700 FileTimeToSystemTime (&hdr.Time3, &This->time3);
703 WCHAR sTemp[MAX_PATH];
704 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE, &This->time1,
705 NULL, sTemp, sizeof(sTemp)/sizeof(*sTemp));
706 TRACE("-- time1: %s\n", debugstr_w(sTemp) );
707 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE, &This->time2,
708 NULL, sTemp, sizeof(sTemp)/sizeof(*sTemp));
709 TRACE("-- time2: %s\n", debugstr_w(sTemp) );
710 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE, &This->time3,
711 NULL, sTemp, sizeof(sTemp)/sizeof(*sTemp));
712 TRACE("-- time3: %s\n", debugstr_w(sTemp) );
715 /* load all the new stuff */
716 if( hdr.dwFlags & SLDF_HAS_ID_LIST )
718 r = ILLoadFromStream( stm, &This->pPidl );
724 /* load the location information */
725 if( hdr.dwFlags & SLDF_HAS_LINK_INFO )
726 r = Stream_LoadLocation( stm, &This->volume, &This->sPath );
730 unicode = hdr.dwFlags & SLDF_UNICODE;
731 if( hdr.dwFlags & SLDF_HAS_NAME )
733 r = Stream_LoadString( stm, unicode, &This->sDescription );
734 TRACE("Description -> %s\n",debugstr_w(This->sDescription));
739 if( hdr.dwFlags & SLDF_HAS_RELPATH )
741 r = Stream_LoadString( stm, unicode, &This->sPathRel );
742 TRACE("Relative Path-> %s\n",debugstr_w(This->sPathRel));
747 if( hdr.dwFlags & SLDF_HAS_WORKINGDIR )
749 r = Stream_LoadString( stm, unicode, &This->sWorkDir );
750 TRACE("Working Dir -> %s\n",debugstr_w(This->sWorkDir));
755 if( hdr.dwFlags & SLDF_HAS_ARGS )
757 r = Stream_LoadString( stm, unicode, &This->sArgs );
758 TRACE("Working Dir -> %s\n",debugstr_w(This->sArgs));
763 if( hdr.dwFlags & SLDF_HAS_ICONLOCATION )
765 r = Stream_LoadString( stm, unicode, &This->sIcoPath );
766 TRACE("Icon file -> %s\n",debugstr_w(This->sIcoPath));
771 if( hdr.dwFlags & SLDF_HAS_LOGO3ID )
773 r = Stream_LoadAdvertiseInfo( stm, &This->sProduct );
774 TRACE("Product -> %s\n",debugstr_w(This->sProduct));
779 if( hdr.dwFlags & SLDF_HAS_DARWINID )
781 r = Stream_LoadAdvertiseInfo( stm, &This->sComponent );
782 TRACE("Component -> %s\n",debugstr_w(This->sComponent));
787 r = IStream_Read(stm, &zero, sizeof zero, &dwBytesRead);
788 if( FAILED( r ) || zero || dwBytesRead != sizeof zero )
789 ERR("Last word was not zero\n");
800 /************************************************************************
803 * Helper function for IPersistStream_Save. Writes a unicode string
804 * with terminating nul byte to a stream, preceded by the its length.
806 static HRESULT Stream_WriteString( IStream* stm, LPCWSTR str )
808 USHORT len = lstrlenW( str ) + 1;
812 r = IStream_Write( stm, &len, sizeof(len), &count );
816 len *= sizeof(WCHAR);
818 r = IStream_Write( stm, str, len, &count );
825 /************************************************************************
826 * Stream_WriteLocationInfo
828 * Writes the location info to a stream
830 * FIXME: One day we might want to write the network volume information
831 * and the final path.
832 * Figure out how Windows deals with unicode paths here.
834 static HRESULT Stream_WriteLocationInfo( IStream* stm, LPCWSTR path,
835 volume_info *volume )
837 DWORD total_size, path_size, volume_info_size, label_size, final_path_size;
838 LOCAL_VOLUME_INFO *vol;
840 LPSTR szLabel, szPath, szFinalPath;
843 TRACE("%p %s %p\n", stm, debugstr_w(path), volume);
845 /* figure out the size of everything */
846 label_size = WideCharToMultiByte( CP_ACP, 0, volume->label, -1,
847 NULL, 0, NULL, NULL );
848 path_size = WideCharToMultiByte( CP_ACP, 0, path, -1,
849 NULL, 0, NULL, NULL );
850 volume_info_size = sizeof *vol + label_size;
852 total_size = sizeof *loc + volume_info_size + path_size + final_path_size;
854 /* create pointers to everything */
855 loc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, total_size);
856 vol = (LOCAL_VOLUME_INFO*) &loc[1];
857 szLabel = (LPSTR) &vol[1];
858 szPath = &szLabel[label_size];
859 szFinalPath = &szPath[path_size];
861 /* fill in the location information header */
862 loc->dwTotalSize = total_size;
863 loc->dwHeaderSize = sizeof (*loc);
865 loc->dwVolTableOfs = sizeof (*loc);
866 loc->dwLocalPathOfs = sizeof (*loc) + volume_info_size;
867 loc->dwNetworkVolTableOfs = 0;
868 loc->dwFinalPathOfs = sizeof (*loc) + volume_info_size + path_size;
870 /* fill in the volume information */
871 vol->dwSize = volume_info_size;
872 vol->dwType = volume->type;
873 vol->dwVolSerial = volume->serial;
874 vol->dwVolLabelOfs = sizeof (*vol);
876 /* copy in the strings */
877 WideCharToMultiByte( CP_ACP, 0, volume->label, -1,
878 szLabel, label_size, NULL, NULL );
879 WideCharToMultiByte( CP_ACP, 0, path, -1,
880 szPath, path_size, NULL, NULL );
883 return IStream_Write( stm, loc, total_size, &count );
886 static HRESULT Stream_WriteAdvertiseInfo( IStream* stm, LPCWSTR string, DWORD magic )
889 EXP_DARWIN_LINK buffer;
893 memset( &buffer, 0, sizeof buffer );
894 buffer.dbh.cbSize = sizeof buffer;
895 buffer.dbh.dwSignature = magic;
896 lstrcpynW( buffer.szwDarwinID, string, MAX_PATH );
897 WideCharToMultiByte(CP_ACP, 0, string, -1, buffer.szDarwinID, MAX_PATH, NULL, NULL );
899 return IStream_Write( stm, &buffer, buffer.dbh.cbSize, &count );
902 /************************************************************************
903 * IPersistStream_Save (IPersistStream)
905 * FIXME: makes assumptions about byte order
907 static HRESULT WINAPI IPersistStream_fnSave(
908 IPersistStream* iface,
912 static const WCHAR wOpen[] = {'o','p','e','n',0};
915 WCHAR exePath[MAX_PATH];
920 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
922 TRACE("%p %p %x\n", This, stm, fClearDirty);
928 SHELL_FindExecutable(NULL, This->sPath, wOpen, exePath, MAX_PATH,
929 NULL, NULL, NULL, NULL);
931 * windows can create lnk files to executables that do not exist yet
932 * so if the executable does not exist the just trust the path they
935 if (!*exePath) strcpyW(exePath,This->sPath);
938 memset(&header, 0, sizeof(header));
939 header.dwSize = sizeof(header);
940 header.fStartup = This->iShowCmd;
941 memcpy(&header.MagicGuid, &CLSID_ShellLink, sizeof(header.MagicGuid) );
943 header.wHotKey = This->wHotKey;
944 header.nIcon = This->iIcoNdx;
945 header.dwFlags = SLDF_UNICODE; /* strings are in unicode */
947 header.dwFlags |= SLDF_HAS_ID_LIST;
949 header.dwFlags |= SLDF_HAS_LINK_INFO;
950 if( This->sDescription )
951 header.dwFlags |= SLDF_HAS_NAME;
953 header.dwFlags |= SLDF_HAS_WORKINGDIR;
955 header.dwFlags |= SLDF_HAS_ARGS;
957 header.dwFlags |= SLDF_HAS_ICONLOCATION;
959 header.dwFlags |= SLDF_HAS_LOGO3ID;
960 if( This->sComponent )
961 header.dwFlags |= SLDF_HAS_DARWINID;
963 SystemTimeToFileTime ( &This->time1, &header.Time1 );
964 SystemTimeToFileTime ( &This->time2, &header.Time2 );
965 SystemTimeToFileTime ( &This->time3, &header.Time3 );
967 /* write the Shortcut header */
968 r = IStream_Write( stm, &header, sizeof(header), &count );
971 ERR("Write failed at %d\n",__LINE__);
975 TRACE("Writing pidl \n");
977 /* write the PIDL to the shortcut */
980 r = ILSaveToStream( stm, This->pPidl );
983 ERR("Failed to write PIDL at %d\n",__LINE__);
989 Stream_WriteLocationInfo( stm, exePath, &This->volume );
991 if( This->sDescription )
992 r = Stream_WriteString( stm, This->sDescription );
995 r = Stream_WriteString( stm, This->sPathRel );
998 r = Stream_WriteString( stm, This->sWorkDir );
1001 r = Stream_WriteString( stm, This->sArgs );
1003 if( This->sIcoPath )
1004 r = Stream_WriteString( stm, This->sIcoPath );
1006 if( This->sProduct )
1007 r = Stream_WriteAdvertiseInfo( stm, This->sProduct, EXP_SZ_ICON_SIG );
1009 if( This->sComponent )
1010 r = Stream_WriteAdvertiseInfo( stm, This->sComponent, EXP_DARWIN_ID_SIG );
1012 /* the last field is a single zero dword */
1014 r = IStream_Write( stm, &zero, sizeof zero, &count );
1019 /************************************************************************
1020 * IPersistStream_GetSizeMax (IPersistStream)
1022 static HRESULT WINAPI IPersistStream_fnGetSizeMax(
1023 IPersistStream* iface,
1024 ULARGE_INTEGER* pcbSize)
1026 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
1028 TRACE("(%p)\n", This);
1033 static const IPersistStreamVtbl psvt =
1035 IPersistStream_fnQueryInterface,
1036 IPersistStream_fnAddRef,
1037 IPersistStream_fnRelease,
1038 IPersistStream_fnGetClassID,
1039 IPersistStream_fnIsDirty,
1040 IPersistStream_fnLoad,
1041 IPersistStream_fnSave,
1042 IPersistStream_fnGetSizeMax
1045 /**************************************************************************
1046 * IShellLink_Constructor
1048 HRESULT WINAPI IShellLink_Constructor( IUnknown *pUnkOuter,
1049 REFIID riid, LPVOID *ppv )
1051 IShellLinkImpl * sl;
1053 TRACE("unkOut=%p riid=%s\n",pUnkOuter, debugstr_guid(riid));
1058 return CLASS_E_NOAGGREGATION;
1059 sl = LocalAlloc(LMEM_ZEROINIT,sizeof(IShellLinkImpl));
1061 return E_OUTOFMEMORY;
1065 sl->lpvtblw = &slvtw;
1066 sl->lpvtblPersistFile = &pfvt;
1067 sl->lpvtblPersistStream = &psvt;
1068 sl->iShowCmd = SW_SHOWNORMAL;
1071 TRACE("(%p)->()\n",sl);
1073 if (IsEqualIID(riid, &IID_IUnknown) ||
1074 IsEqualIID(riid, &IID_IShellLinkA))
1076 else if (IsEqualIID(riid, &IID_IShellLinkW))
1077 *ppv = &(sl->lpvtblw);
1079 LocalFree((HLOCAL)sl);
1080 ERR("E_NOINTERFACE\n");
1081 return E_NOINTERFACE;
1088 static BOOL SHELL_ExistsFileW(LPCWSTR path)
1090 if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW(path))
1095 /**************************************************************************
1096 * ShellLink_UpdatePath
1097 * update absolute path in sPath using relative path in sPathRel
1099 static HRESULT ShellLink_UpdatePath(LPWSTR sPathRel, LPCWSTR path, LPCWSTR sWorkDir, LPWSTR* psPath)
1101 if (!path || !psPath)
1102 return E_INVALIDARG;
1104 if (!*psPath && sPathRel) {
1105 WCHAR buffer[2*MAX_PATH], abs_path[2*MAX_PATH];
1106 LPWSTR final = NULL;
1108 /* first try if [directory of link file] + [relative path] finds an existing file */
1110 GetFullPathNameW( path, MAX_PATH*2, buffer, &final );
1113 lstrcpyW(final, sPathRel);
1117 if (SHELL_ExistsFileW(buffer)) {
1118 if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
1119 lstrcpyW(abs_path, buffer);
1121 /* try if [working directory] + [relative path] finds an existing file */
1123 lstrcpyW(buffer, sWorkDir);
1124 lstrcpyW(PathAddBackslashW(buffer), sPathRel);
1126 if (SHELL_ExistsFileW(buffer))
1127 if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
1128 lstrcpyW(abs_path, buffer);
1132 /* FIXME: This is even not enough - not all shell links can be resolved using this algorithm. */
1134 lstrcpyW(abs_path, sPathRel);
1136 *psPath = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(abs_path)+1)*sizeof(WCHAR));
1138 return E_OUTOFMEMORY;
1140 lstrcpyW(*psPath, abs_path);
1146 /**************************************************************************
1147 * IShellLink_ConstructFromFile
1149 HRESULT WINAPI IShellLink_ConstructFromFile( IUnknown* pUnkOuter, REFIID riid,
1150 LPCITEMIDLIST pidl, LPVOID* ppv)
1154 HRESULT hr = IShellLink_Constructor(NULL, riid, (LPVOID*)&psl);
1156 if (SUCCEEDED(hr)) {
1161 hr = IShellLinkW_QueryInterface(psl, &IID_IPersistFile, (LPVOID*)&ppf);
1163 if (SUCCEEDED(hr)) {
1164 WCHAR path[MAX_PATH];
1166 if (SHGetPathFromIDListW(pidl, path))
1167 hr = IPersistFile_Load(ppf, path, 0);
1172 *ppv = (IUnknown*) psl;
1174 IPersistFile_Release(ppf);
1178 IShellLinkW_Release(psl);
1184 /**************************************************************************
1185 * IShellLinkA_QueryInterface
1187 static HRESULT WINAPI IShellLinkA_fnQueryInterface( IShellLinkA * iface, REFIID riid, LPVOID *ppvObj)
1189 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1191 TRACE("(%p)->(\n\tIID:\t%s)\n",This,debugstr_guid(riid));
1195 if(IsEqualIID(riid, &IID_IUnknown) ||
1196 IsEqualIID(riid, &IID_IShellLinkA))
1200 else if(IsEqualIID(riid, &IID_IShellLinkW))
1202 *ppvObj = (IShellLinkW *)&(This->lpvtblw);
1204 else if(IsEqualIID(riid, &IID_IPersistFile))
1206 *ppvObj = (IPersistFile *)&(This->lpvtblPersistFile);
1208 else if(IsEqualIID(riid, &IID_IPersistStream))
1210 *ppvObj = (IPersistStream *)&(This->lpvtblPersistStream);
1215 IUnknown_AddRef((IUnknown*)(*ppvObj));
1216 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
1219 TRACE("-- Interface: E_NOINTERFACE\n");
1220 return E_NOINTERFACE;
1222 /******************************************************************************
1223 * IShellLinkA_AddRef
1225 static ULONG WINAPI IShellLinkA_fnAddRef(IShellLinkA * iface)
1227 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1228 ULONG refCount = InterlockedIncrement(&This->ref);
1230 TRACE("(%p)->(count=%lu)\n", This, refCount - 1);
1234 /******************************************************************************
1235 * IShellLinkA_Release
1237 static ULONG WINAPI IShellLinkA_fnRelease(IShellLinkA * iface)
1239 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1240 ULONG refCount = InterlockedDecrement(&This->ref);
1242 TRACE("(%p)->(count=%lu)\n", This, refCount + 1);
1247 TRACE("-- destroying IShellLink(%p)\n",This);
1249 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
1250 HeapFree(GetProcessHeap(), 0, This->sArgs);
1251 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1252 HeapFree(GetProcessHeap(), 0, This->sDescription);
1253 HeapFree(GetProcessHeap(),0,This->sPath);
1256 ILFree(This->pPidl);
1258 LocalFree((HANDLE)This);
1263 static HRESULT WINAPI IShellLinkA_fnGetPath(IShellLinkA * iface, LPSTR pszFile,
1264 INT cchMaxPath, WIN32_FIND_DATAA *pfd, DWORD fFlags)
1266 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1268 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%lu)(%s)\n",
1269 This, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(This->sPath));
1271 if (This->sComponent || This->sProduct)
1277 WideCharToMultiByte( CP_ACP, 0, This->sPath, -1,
1278 pszFile, cchMaxPath, NULL, NULL);
1280 if (pfd) FIXME("(%p): WIN32_FIND_DATA is not yet filled.\n", This);
1285 static HRESULT WINAPI IShellLinkA_fnGetIDList(IShellLinkA * iface, LPITEMIDLIST * ppidl)
1287 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1289 TRACE("(%p)->(ppidl=%p)\n",This, ppidl);
1291 return IShellLinkW_GetIDList((IShellLinkW*)&(This->lpvtblw), ppidl);
1294 static HRESULT WINAPI IShellLinkA_fnSetIDList(IShellLinkA * iface, LPCITEMIDLIST pidl)
1296 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1298 TRACE("(%p)->(pidl=%p)\n",This, pidl);
1301 ILFree(This->pPidl);
1302 This->pPidl = ILClone (pidl);
1303 This->bDirty = TRUE;
1308 static HRESULT WINAPI IShellLinkA_fnGetDescription(IShellLinkA * iface, LPSTR pszName,INT cchMaxName)
1310 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1312 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1316 if( This->sDescription )
1317 WideCharToMultiByte( CP_ACP, 0, This->sDescription, -1,
1318 pszName, cchMaxName, NULL, NULL);
1322 static HRESULT WINAPI IShellLinkA_fnSetDescription(IShellLinkA * iface, LPCSTR pszName)
1324 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1326 TRACE("(%p)->(pName=%s)\n", This, pszName);
1328 HeapFree(GetProcessHeap(), 0, This->sDescription);
1329 This->sDescription = HEAP_strdupAtoW( GetProcessHeap(), 0, pszName);
1330 if ( !This->sDescription )
1331 return E_OUTOFMEMORY;
1333 This->bDirty = TRUE;
1338 static HRESULT WINAPI IShellLinkA_fnGetWorkingDirectory(IShellLinkA * iface, LPSTR pszDir,INT cchMaxPath)
1340 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1342 TRACE("(%p)->(%p len=%u)\n", This, pszDir, cchMaxPath);
1346 if( This->sWorkDir )
1347 WideCharToMultiByte( CP_ACP, 0, This->sWorkDir, -1,
1348 pszDir, cchMaxPath, NULL, NULL);
1353 static HRESULT WINAPI IShellLinkA_fnSetWorkingDirectory(IShellLinkA * iface, LPCSTR pszDir)
1355 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1357 TRACE("(%p)->(dir=%s)\n",This, pszDir);
1359 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1360 This->sWorkDir = HEAP_strdupAtoW( GetProcessHeap(), 0, pszDir);
1361 if ( !This->sWorkDir )
1362 return E_OUTOFMEMORY;
1364 This->bDirty = TRUE;
1369 static HRESULT WINAPI IShellLinkA_fnGetArguments(IShellLinkA * iface, LPSTR pszArgs,INT cchMaxPath)
1371 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1373 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1378 WideCharToMultiByte( CP_ACP, 0, This->sArgs, -1,
1379 pszArgs, cchMaxPath, NULL, NULL);
1384 static HRESULT WINAPI IShellLinkA_fnSetArguments(IShellLinkA * iface, LPCSTR pszArgs)
1386 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1388 TRACE("(%p)->(args=%s)\n",This, pszArgs);
1390 HeapFree(GetProcessHeap(), 0, This->sArgs);
1391 This->sArgs = HEAP_strdupAtoW( GetProcessHeap(), 0, pszArgs);
1393 return E_OUTOFMEMORY;
1395 This->bDirty = TRUE;
1400 static HRESULT WINAPI IShellLinkA_fnGetHotkey(IShellLinkA * iface, WORD *pwHotkey)
1402 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1404 TRACE("(%p)->(%p)(0x%08x)\n",This, pwHotkey, This->wHotKey);
1406 *pwHotkey = This->wHotKey;
1411 static HRESULT WINAPI IShellLinkA_fnSetHotkey(IShellLinkA * iface, WORD wHotkey)
1413 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1415 TRACE("(%p)->(hotkey=%x)\n",This, wHotkey);
1417 This->wHotKey = wHotkey;
1418 This->bDirty = TRUE;
1423 static HRESULT WINAPI IShellLinkA_fnGetShowCmd(IShellLinkA * iface, INT *piShowCmd)
1425 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1427 TRACE("(%p)->(%p)\n",This, piShowCmd);
1428 *piShowCmd = This->iShowCmd;
1432 static HRESULT WINAPI IShellLinkA_fnSetShowCmd(IShellLinkA * iface, INT iShowCmd)
1434 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1436 TRACE("(%p) %d\n",This, iShowCmd);
1438 This->iShowCmd = iShowCmd;
1439 This->bDirty = TRUE;
1444 static HRESULT SHELL_PidlGeticonLocationA(IShellFolder* psf, LPITEMIDLIST pidl, LPSTR pszIconPath, int cchIconPath, int* piIcon)
1446 LPCITEMIDLIST pidlLast;
1448 HRESULT hr = SHBindToParent(pidl, &IID_IShellFolder, (LPVOID*)&psf, &pidlLast);
1450 if (SUCCEEDED(hr)) {
1453 hr = IShellFolder_GetUIObjectOf(psf, 0, 1, (LPCITEMIDLIST*)&pidlLast, &IID_IExtractIconA, NULL, (LPVOID*)&pei);
1455 if (SUCCEEDED(hr)) {
1456 hr = IExtractIconA_GetIconLocation(pei, 0, pszIconPath, MAX_PATH, piIcon, NULL);
1458 IExtractIconA_Release(pei);
1461 IShellFolder_Release(psf);
1467 static HRESULT WINAPI IShellLinkA_fnGetIconLocation(IShellLinkA * iface, LPSTR pszIconPath,INT cchIconPath,INT *piIcon)
1469 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1471 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1474 *piIcon = This->iIcoNdx;
1478 WideCharToMultiByte(CP_ACP, 0, This->sIcoPath, -1, pszIconPath, cchIconPath, NULL, NULL);
1482 if (This->pPidl || This->sPath)
1486 HRESULT hr = SHGetDesktopFolder(&pdsk);
1490 /* first look for an icon using the PIDL (if present) */
1492 hr = SHELL_PidlGeticonLocationA(pdsk, This->pPidl, pszIconPath, cchIconPath, piIcon);
1496 /* if we couldn't find an icon yet, look for it using the file system path */
1497 if (FAILED(hr) && This->sPath)
1501 hr = IShellFolder_ParseDisplayName(pdsk, 0, NULL, This->sPath, NULL, &pidl, NULL);
1503 if (SUCCEEDED(hr)) {
1504 hr = SHELL_PidlGeticonLocationA(pdsk, pidl, pszIconPath, cchIconPath, piIcon);
1510 IShellFolder_Release(pdsk);
1518 static HRESULT WINAPI IShellLinkA_fnSetIconLocation(IShellLinkA * iface, LPCSTR pszIconPath,INT iIcon)
1520 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1522 TRACE("(%p)->(path=%s iicon=%u)\n",This, pszIconPath, iIcon);
1524 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
1525 This->sIcoPath = HEAP_strdupAtoW(GetProcessHeap(), 0, pszIconPath);
1526 if ( !This->sIcoPath )
1527 return E_OUTOFMEMORY;
1529 This->iIcoNdx = iIcon;
1530 This->bDirty = TRUE;
1535 static HRESULT WINAPI IShellLinkA_fnSetRelativePath(IShellLinkA * iface, LPCSTR pszPathRel, DWORD dwReserved)
1537 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1539 TRACE("(%p)->(path=%s %lx)\n",This, pszPathRel, dwReserved);
1541 HeapFree(GetProcessHeap(), 0, This->sPathRel);
1542 This->sPathRel = HEAP_strdupAtoW(GetProcessHeap(), 0, pszPathRel);
1543 This->bDirty = TRUE;
1545 return ShellLink_UpdatePath(This->sPathRel, This->sPath, This->sWorkDir, &This->sPath);
1548 static HRESULT WINAPI IShellLinkA_fnResolve(IShellLinkA * iface, HWND hwnd, DWORD fFlags)
1550 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1552 TRACE("(%p)->(hwnd=%p flags=%lx)\n",This, hwnd, fFlags);
1554 return IShellLinkW_Resolve( (IShellLinkW*)&(This->lpvtblw), hwnd, fFlags );
1557 static HRESULT WINAPI IShellLinkA_fnSetPath(IShellLinkA * iface, LPCSTR pszFile)
1561 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1563 TRACE("(%p)->(path=%s)\n",This, pszFile);
1565 str = HEAP_strdupAtoW(GetProcessHeap(), 0, pszFile);
1567 return E_OUTOFMEMORY;
1569 r = IShellLinkW_SetPath((IShellLinkW*)&(This->lpvtblw), str);
1570 HeapFree( GetProcessHeap(), 0, str );
1575 /**************************************************************************
1576 * IShellLink Implementation
1579 static const IShellLinkAVtbl slvt =
1581 IShellLinkA_fnQueryInterface,
1582 IShellLinkA_fnAddRef,
1583 IShellLinkA_fnRelease,
1584 IShellLinkA_fnGetPath,
1585 IShellLinkA_fnGetIDList,
1586 IShellLinkA_fnSetIDList,
1587 IShellLinkA_fnGetDescription,
1588 IShellLinkA_fnSetDescription,
1589 IShellLinkA_fnGetWorkingDirectory,
1590 IShellLinkA_fnSetWorkingDirectory,
1591 IShellLinkA_fnGetArguments,
1592 IShellLinkA_fnSetArguments,
1593 IShellLinkA_fnGetHotkey,
1594 IShellLinkA_fnSetHotkey,
1595 IShellLinkA_fnGetShowCmd,
1596 IShellLinkA_fnSetShowCmd,
1597 IShellLinkA_fnGetIconLocation,
1598 IShellLinkA_fnSetIconLocation,
1599 IShellLinkA_fnSetRelativePath,
1600 IShellLinkA_fnResolve,
1601 IShellLinkA_fnSetPath
1605 /**************************************************************************
1606 * IShellLinkW_fnQueryInterface
1608 static HRESULT WINAPI IShellLinkW_fnQueryInterface(
1609 IShellLinkW * iface, REFIID riid, LPVOID *ppvObj)
1611 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1613 return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvObj);
1616 /******************************************************************************
1617 * IShellLinkW_fnAddRef
1619 static ULONG WINAPI IShellLinkW_fnAddRef(IShellLinkW * iface)
1621 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1623 TRACE("(%p)->(count=%lu)\n",This,This->ref);
1625 return IShellLinkA_AddRef((IShellLinkA*)This);
1627 /******************************************************************************
1628 * IShellLinkW_fnRelease
1631 static ULONG WINAPI IShellLinkW_fnRelease(IShellLinkW * iface)
1633 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1635 TRACE("(%p)->(count=%lu)\n",This,This->ref);
1637 return IShellLinkA_Release((IShellLinkA*)This);
1640 static HRESULT WINAPI IShellLinkW_fnGetPath(IShellLinkW * iface, LPWSTR pszFile,INT cchMaxPath, WIN32_FIND_DATAW *pfd, DWORD fFlags)
1642 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1644 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%lu)(%s)\n",
1645 This, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(This->sPath));
1647 if (This->sComponent || This->sProduct)
1653 lstrcpynW( pszFile, This->sPath, cchMaxPath );
1655 if (pfd) FIXME("(%p): WIN32_FIND_DATA is not yet filled.\n", This);
1660 static HRESULT WINAPI IShellLinkW_fnGetIDList(IShellLinkW * iface, LPITEMIDLIST * ppidl)
1662 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1664 TRACE("(%p)->(ppidl=%p)\n",This, ppidl);
1668 *ppidl = ILClone(This->pPidl);
1672 static HRESULT WINAPI IShellLinkW_fnSetIDList(IShellLinkW * iface, LPCITEMIDLIST pidl)
1674 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1676 TRACE("(%p)->(pidl=%p)\n",This, pidl);
1679 ILFree( This->pPidl );
1680 This->pPidl = ILClone( pidl );
1684 This->bDirty = TRUE;
1689 static HRESULT WINAPI IShellLinkW_fnGetDescription(IShellLinkW * iface, LPWSTR pszName,INT cchMaxName)
1691 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1693 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1696 if( This->sDescription )
1697 lstrcpynW( pszName, This->sDescription, cchMaxName );
1702 static HRESULT WINAPI IShellLinkW_fnSetDescription(IShellLinkW * iface, LPCWSTR pszName)
1704 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1706 TRACE("(%p)->(desc=%s)\n",This, debugstr_w(pszName));
1708 HeapFree(GetProcessHeap(), 0, This->sDescription);
1709 This->sDescription = HeapAlloc( GetProcessHeap(), 0,
1710 (lstrlenW( pszName )+1)*sizeof(WCHAR) );
1711 if ( !This->sDescription )
1712 return E_OUTOFMEMORY;
1714 lstrcpyW( This->sDescription, pszName );
1715 This->bDirty = TRUE;
1720 static HRESULT WINAPI IShellLinkW_fnGetWorkingDirectory(IShellLinkW * iface, LPWSTR pszDir,INT cchMaxPath)
1722 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1724 TRACE("(%p)->(%p len %u)\n", This, pszDir, cchMaxPath);
1728 if( This->sWorkDir )
1729 lstrcpynW( pszDir, This->sWorkDir, cchMaxPath );
1734 static HRESULT WINAPI IShellLinkW_fnSetWorkingDirectory(IShellLinkW * iface, LPCWSTR pszDir)
1736 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1738 TRACE("(%p)->(dir=%s)\n",This, debugstr_w(pszDir));
1740 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1741 This->sWorkDir = HeapAlloc( GetProcessHeap(), 0,
1742 (lstrlenW( pszDir )+1)*sizeof (WCHAR) );
1743 if ( !This->sWorkDir )
1744 return E_OUTOFMEMORY;
1745 lstrcpyW( This->sWorkDir, pszDir );
1746 This->bDirty = TRUE;
1751 static HRESULT WINAPI IShellLinkW_fnGetArguments(IShellLinkW * iface, LPWSTR pszArgs,INT cchMaxPath)
1753 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1755 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1760 lstrcpynW( pszArgs, This->sArgs, cchMaxPath );
1765 static HRESULT WINAPI IShellLinkW_fnSetArguments(IShellLinkW * iface, LPCWSTR pszArgs)
1767 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1769 TRACE("(%p)->(args=%s)\n",This, debugstr_w(pszArgs));
1771 HeapFree(GetProcessHeap(), 0, This->sArgs);
1772 This->sArgs = HeapAlloc( GetProcessHeap(), 0,
1773 (lstrlenW( pszArgs )+1)*sizeof (WCHAR) );
1775 return E_OUTOFMEMORY;
1776 lstrcpyW( This->sArgs, pszArgs );
1777 This->bDirty = TRUE;
1782 static HRESULT WINAPI IShellLinkW_fnGetHotkey(IShellLinkW * iface, WORD *pwHotkey)
1784 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1786 TRACE("(%p)->(%p)\n",This, pwHotkey);
1788 *pwHotkey=This->wHotKey;
1793 static HRESULT WINAPI IShellLinkW_fnSetHotkey(IShellLinkW * iface, WORD wHotkey)
1795 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1797 TRACE("(%p)->(hotkey=%x)\n",This, wHotkey);
1799 This->wHotKey = wHotkey;
1800 This->bDirty = TRUE;
1805 static HRESULT WINAPI IShellLinkW_fnGetShowCmd(IShellLinkW * iface, INT *piShowCmd)
1807 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1809 TRACE("(%p)->(%p)\n",This, piShowCmd);
1811 *piShowCmd = This->iShowCmd;
1816 static HRESULT WINAPI IShellLinkW_fnSetShowCmd(IShellLinkW * iface, INT iShowCmd)
1818 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1820 This->iShowCmd = iShowCmd;
1821 This->bDirty = TRUE;
1826 static HRESULT SHELL_PidlGeticonLocationW(IShellFolder* psf, LPITEMIDLIST pidl, LPWSTR pszIconPath, int cchIconPath, int* piIcon)
1828 LPCITEMIDLIST pidlLast;
1830 HRESULT hr = SHBindToParent(pidl, &IID_IShellFolder, (LPVOID*)&psf, &pidlLast);
1832 if (SUCCEEDED(hr)) {
1835 hr = IShellFolder_GetUIObjectOf(psf, 0, 1, (LPCITEMIDLIST*)&pidlLast, &IID_IExtractIconW, NULL, (LPVOID*)&pei);
1837 if (SUCCEEDED(hr)) {
1838 hr = IExtractIconW_GetIconLocation(pei, 0, pszIconPath, MAX_PATH, piIcon, NULL);
1840 IExtractIconW_Release(pei);
1843 IShellFolder_Release(psf);
1849 static HRESULT WINAPI IShellLinkW_fnGetIconLocation(IShellLinkW * iface, LPWSTR pszIconPath,INT cchIconPath,INT *piIcon)
1851 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1853 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1856 *piIcon = This->iIcoNdx;
1860 lstrcpynW(pszIconPath, This->sIcoPath, cchIconPath);
1864 if (This->pPidl || This->sPath)
1868 HRESULT hr = SHGetDesktopFolder(&pdsk);
1872 /* first look for an icon using the PIDL (if present) */
1874 hr = SHELL_PidlGeticonLocationW(pdsk, This->pPidl, pszIconPath, cchIconPath, piIcon);
1878 /* if we couldn't find an icon yet, look for it using the file system path */
1879 if (FAILED(hr) && This->sPath)
1883 hr = IShellFolder_ParseDisplayName(pdsk, 0, NULL, This->sPath, NULL, &pidl, NULL);
1887 hr = SHELL_PidlGeticonLocationW(pdsk, pidl, pszIconPath, cchIconPath, piIcon);
1893 IShellFolder_Release(pdsk);
1900 static HRESULT WINAPI IShellLinkW_fnSetIconLocation(IShellLinkW * iface, LPCWSTR pszIconPath,INT iIcon)
1902 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1904 TRACE("(%p)->(path=%s iicon=%u)\n",This, debugstr_w(pszIconPath), iIcon);
1906 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
1907 This->sIcoPath = HeapAlloc( GetProcessHeap(), 0,
1908 (lstrlenW( pszIconPath )+1)*sizeof (WCHAR) );
1909 if ( !This->sIcoPath )
1910 return E_OUTOFMEMORY;
1911 lstrcpyW( This->sIcoPath, pszIconPath );
1913 This->iIcoNdx = iIcon;
1914 This->bDirty = TRUE;
1919 static HRESULT WINAPI IShellLinkW_fnSetRelativePath(IShellLinkW * iface, LPCWSTR pszPathRel, DWORD dwReserved)
1921 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1923 TRACE("(%p)->(path=%s %lx)\n",This, debugstr_w(pszPathRel), dwReserved);
1925 HeapFree(GetProcessHeap(), 0, This->sPathRel);
1926 This->sPathRel = HeapAlloc( GetProcessHeap(), 0,
1927 (lstrlenW( pszPathRel )+1) * sizeof (WCHAR) );
1928 if ( !This->sPathRel )
1929 return E_OUTOFMEMORY;
1930 lstrcpyW( This->sPathRel, pszPathRel );
1931 This->bDirty = TRUE;
1933 return ShellLink_UpdatePath(This->sPathRel, This->sPath, This->sWorkDir, &This->sPath);
1936 static HRESULT WINAPI IShellLinkW_fnResolve(IShellLinkW * iface, HWND hwnd, DWORD fFlags)
1940 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1942 TRACE("(%p)->(hwnd=%p flags=%lx)\n",This, hwnd, fFlags);
1944 /*FIXME: use IResolveShellLink interface */
1946 if (!This->sPath && This->pPidl) {
1947 WCHAR buffer[MAX_PATH];
1949 hr = SHELL_GetPathFromIDListW(This->pPidl, buffer, MAX_PATH);
1951 if (SUCCEEDED(hr) && *buffer) {
1952 This->sPath = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(buffer)+1)*sizeof(WCHAR));
1954 return E_OUTOFMEMORY;
1956 lstrcpyW(This->sPath, buffer);
1958 This->bDirty = TRUE;
1960 hr = S_OK; /* don't report an error occurred while just caching information */
1963 if (!This->sIcoPath && This->sPath) {
1964 This->sIcoPath = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(This->sPath)+1)*sizeof(WCHAR));
1965 if (!This->sIcoPath)
1966 return E_OUTOFMEMORY;
1968 lstrcpyW(This->sIcoPath, This->sPath);
1971 This->bDirty = TRUE;
1977 static LPWSTR ShellLink_GetAdvertisedArg(LPCWSTR str)
1986 p = strchrW( str, ':' );
1990 ret = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR)*(len+1));
1993 memcpy( ret, str, sizeof(WCHAR)*len );
1998 static HRESULT ShellLink_SetAdvertiseInfo(IShellLinkImpl *This, LPCWSTR str)
2000 LPCWSTR szComponent = NULL, szProduct = NULL, p;
2008 /* each segment must start with two colons */
2009 if( str[0] != ':' || str[1] != ':' )
2012 /* the last segment is just two colons */
2017 /* there must be a colon straight after a guid */
2018 p = strchrW( str, ':' );
2025 /* get the guid, and check it's validly formatted */
2026 memcpy( szGuid, str, sizeof(WCHAR)*len );
2028 r = CLSIDFromString( szGuid, &guid );
2033 /* match it up to a guid that we care about */
2034 if( IsEqualGUID( &guid, &SHELL32_AdvtShortcutComponent ) && !szComponent )
2036 else if( IsEqualGUID( &guid, &SHELL32_AdvtShortcutProduct ) && !szProduct )
2041 /* skip to the next field */
2042 str = strchrW( str, ':' );
2047 /* we have to have a component for an advertised shortcut */
2051 This->sComponent = ShellLink_GetAdvertisedArg( szComponent );
2052 This->sProduct = ShellLink_GetAdvertisedArg( szProduct );
2054 TRACE("Component = %s\n", debugstr_w(This->sComponent));
2055 TRACE("Product = %s\n", debugstr_w(This->sProduct));
2060 static BOOL ShellLink_GetVolumeInfo(LPWSTR path, volume_info *volume)
2062 const int label_sz = sizeof volume->label/sizeof volume->label[0];
2063 WCHAR drive[4] = { path[0], ':', '\\', 0 };
2066 volume->type = GetDriveTypeW(drive);
2067 r = GetVolumeInformationW(drive, volume->label, label_sz,
2068 &volume->serial, NULL, NULL, NULL, 0);
2069 TRACE("r = %d type %ld serial %08lx name %s\n", r,
2070 volume->type, volume->serial, debugstr_w(volume->label));
2074 static HRESULT WINAPI IShellLinkW_fnSetPath(IShellLinkW * iface, LPCWSTR pszFile)
2076 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
2077 WCHAR buffer[MAX_PATH];
2081 TRACE("(%p)->(path=%s)\n",This, debugstr_w(pszFile));
2083 HeapFree(GetProcessHeap(), 0, This->sPath);
2086 HeapFree(GetProcessHeap(), 0, This->sComponent);
2087 This->sComponent = NULL;
2090 ILFree(This->pPidl);
2093 if (S_OK != ShellLink_SetAdvertiseInfo( This, pszFile ))
2095 if (*pszFile == '\0')
2097 else if (!GetFullPathNameW(pszFile, MAX_PATH, buffer, &fname))
2099 else if(!PathFileExistsW(buffer))
2102 This->pPidl = SHSimpleIDListFromPathW(pszFile);
2103 ShellLink_GetVolumeInfo(buffer, &This->volume);
2105 This->sPath = HeapAlloc( GetProcessHeap(), 0,
2106 (lstrlenW( buffer )+1) * sizeof (WCHAR) );
2108 return E_OUTOFMEMORY;
2110 lstrcpyW(This->sPath, buffer);
2112 This->bDirty = TRUE;
2117 /**************************************************************************
2118 * IShellLinkW Implementation
2121 static const IShellLinkWVtbl slvtw =
2123 IShellLinkW_fnQueryInterface,
2124 IShellLinkW_fnAddRef,
2125 IShellLinkW_fnRelease,
2126 IShellLinkW_fnGetPath,
2127 IShellLinkW_fnGetIDList,
2128 IShellLinkW_fnSetIDList,
2129 IShellLinkW_fnGetDescription,
2130 IShellLinkW_fnSetDescription,
2131 IShellLinkW_fnGetWorkingDirectory,
2132 IShellLinkW_fnSetWorkingDirectory,
2133 IShellLinkW_fnGetArguments,
2134 IShellLinkW_fnSetArguments,
2135 IShellLinkW_fnGetHotkey,
2136 IShellLinkW_fnSetHotkey,
2137 IShellLinkW_fnGetShowCmd,
2138 IShellLinkW_fnSetShowCmd,
2139 IShellLinkW_fnGetIconLocation,
2140 IShellLinkW_fnSetIconLocation,
2141 IShellLinkW_fnSetRelativePath,
2142 IShellLinkW_fnResolve,
2143 IShellLinkW_fnSetPath