3 * Copyright 1997 Marcus Meissner
4 * Copyright 1998 Juergen Schmied
5 * Copyright 2005 Mike McCormack
6 * Copyright 2009 Andrew Hill
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * Nearly complete information about the binary formats
24 * of .lnk files available at http://www.wotsit.org
26 * You can use winedump to examine the contents of a link file:
29 * MSI advertised shortcuts are totally undocumented. They provide an
30 * icon for a program that is not yet installed, and invoke MSI to
31 * install the program when the shortcut is clicked on. They are
32 * created by passing a special string to SetPath, and the information
33 * in that string is parsed an stored.
38 WINE_DEFAULT_DEBUG_CHANNEL(shell
);
40 #define SHLINK_LOCAL 0
41 #define SHLINK_REMOTE 1
42 #define MAX_PROPERTY_SHEET_PAGE 32
44 /* link file formats */
50 DWORD dwSize
; /* 0x00 size of the header - 0x4c */
51 GUID MagicGuid
; /* 0x04 is CLSID_ShellLink */
52 DWORD dwFlags
; /* 0x14 describes elements following */
53 DWORD dwFileAttr
; /* 0x18 attributes of the target file */
54 FILETIME Time1
; /* 0x1c */
55 FILETIME Time2
; /* 0x24 */
56 FILETIME Time3
; /* 0x2c */
57 DWORD dwFileLength
; /* 0x34 File length */
58 DWORD nIcon
; /* 0x38 icon number */
59 DWORD fStartup
; /* 0x3c startup type */
60 DWORD wHotKey
; /* 0x40 hotkey */
61 DWORD Unknown5
; /* 0x44 */
62 DWORD Unknown6
; /* 0x48 */
72 DWORD dwNetworkVolTableOfs
;
76 struct LOCAL_VOLUME_INFO
88 WCHAR label
[12]; /* assume 8.3 */
93 /* IShellLink Implementation */
95 static HRESULT
ShellLink_UpdatePath(LPCWSTR sPathRel
, LPCWSTR path
, LPCWSTR sWorkDir
, LPWSTR
* psPath
);
97 /* strdup on the process heap */
98 static LPWSTR __inline
HEAP_strdupAtoW( HANDLE heap
, DWORD flags
, LPCSTR str
)
105 len
= MultiByteToWideChar( CP_ACP
, 0, str
, -1, NULL
, 0 );
106 p
= (LPWSTR
)HeapAlloc( heap
, flags
, len
*sizeof (WCHAR
) );
109 MultiByteToWideChar( CP_ACP
, 0, str
, -1, p
, len
);
113 static LPWSTR __inline
strdupW( LPCWSTR src
)
116 if (!src
) return NULL
;
117 dest
= (LPWSTR
)HeapAlloc( GetProcessHeap(), 0, (wcslen(src
)+1)*sizeof(WCHAR
) );
123 ShellLink::ShellLink()
127 memset(&time1
, 0, sizeof(time1
));
128 memset(&time2
, 0, sizeof(time2
));
129 memset(&time3
, 0, sizeof(time3
));
130 iShowCmd
= SW_SHOWNORMAL
;
140 memset(&volume
, 0, sizeof(volume
));
147 ShellLink::~ShellLink()
149 TRACE("-- destroying IShellLink(%p)\n", this);
151 HeapFree(GetProcessHeap(), 0, sIcoPath
);
152 HeapFree(GetProcessHeap(), 0, sArgs
);
153 HeapFree(GetProcessHeap(), 0, sWorkDir
);
154 HeapFree(GetProcessHeap(), 0, sDescription
);
155 HeapFree(GetProcessHeap(), 0, sPath
);
156 HeapFree(GetProcessHeap(), 0, sLinkPath
);
162 HRESULT WINAPI
ShellLink::GetClassID(CLSID
*pclsid
)
164 TRACE("%p %p\n", this, pclsid
);
168 *pclsid
= CLSID_ShellLink
;
172 HRESULT WINAPI
ShellLink::IsDirty()
174 TRACE("(%p)\n",this);
182 HRESULT WINAPI
ShellLink::Load(LPCOLESTR pszFileName
, DWORD dwMode
)
185 CComPtr
<IStream
> stm
;
187 TRACE("(%p, %s, %x)\n",this, debugstr_w(pszFileName
), dwMode
);
190 dwMode
= STGM_READ
| STGM_SHARE_DENY_WRITE
;
191 r
= SHCreateStreamOnFileW(pszFileName
, dwMode
, &stm
);
194 HeapFree(GetProcessHeap(), 0, sLinkPath
);
195 sLinkPath
= strdupW(pszFileName
);
197 ShellLink_UpdatePath(sPathRel
, pszFileName
, sWorkDir
, &sPath
);
200 TRACE("-- returning hr %08x\n", r
);
204 static BOOL
StartLinkProcessor( LPCOLESTR szLink
)
206 static const WCHAR szFormat
[] = {
207 'w','i','n','e','m','e','n','u','b','u','i','l','d','e','r','.','e','x','e',
208 ' ','-','w',' ','"','%','s','"',0 };
212 PROCESS_INFORMATION pi
;
215 len
= sizeof(szFormat
) + wcslen( szLink
) * sizeof(WCHAR
);
216 buffer
= (LPWSTR
)HeapAlloc( GetProcessHeap(), 0, len
);
220 swprintf( buffer
, szFormat
, szLink
);
222 TRACE("starting %s\n",debugstr_w(buffer
));
224 memset(&si
, 0, sizeof(si
));
227 ret
= CreateProcessW( NULL
, buffer
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &si
, &pi
);
229 HeapFree( GetProcessHeap(), 0, buffer
);
233 CloseHandle( pi
.hProcess
);
234 CloseHandle( pi
.hThread
);
240 HRESULT WINAPI
ShellLink::Save(LPCOLESTR pszFileName
, BOOL fRemember
)
243 CComPtr
<IStream
> stm
;
245 TRACE("(%p)->(%s)\n", this, debugstr_w(pszFileName
));
250 r
= SHCreateStreamOnFileW( pszFileName
, STGM_READWRITE
| STGM_CREATE
| STGM_SHARE_EXCLUSIVE
, &stm
);
253 r
= Save(stm
, FALSE
);
259 HeapFree(GetProcessHeap(), 0, sLinkPath
);
261 sLinkPath
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, (wcslen(pszFileName
)+1) * sizeof(WCHAR
));
264 wcscpy(sLinkPath
, pszFileName
);
267 StartLinkProcessor( pszFileName
);
273 DeleteFileW( pszFileName
);
274 WARN("Failed to create shortcut %s\n", debugstr_w(pszFileName
) );
281 HRESULT WINAPI
ShellLink::SaveCompleted(LPCOLESTR pszFileName
)
283 FIXME("(%p)->(%s)\n", this, debugstr_w(pszFileName
));
287 HRESULT WINAPI
ShellLink::GetCurFile(LPOLESTR
*ppszFileName
)
289 *ppszFileName
= NULL
;
293 /* IPersistFile::GetCurFile called before IPersistFile::Save */
297 *ppszFileName
= (LPOLESTR
)CoTaskMemAlloc((wcslen(sLinkPath
)+1) * sizeof(WCHAR
));
301 return E_OUTOFMEMORY
;
304 /* copy last saved filename */
305 wcscpy(*ppszFileName
, sLinkPath
);
310 /************************************************************************
311 * IPersistStream_IsDirty (IPersistStream)
314 static HRESULT
Stream_LoadString( IStream
* stm
, BOOL unicode
, LPWSTR
*pstr
)
325 r
= stm
->Read(&len
, sizeof(len
), &count
);
326 if ( FAILED (r
) || ( count
!= sizeof(len
) ) )
330 len
*= sizeof (WCHAR
);
332 TRACE("reading %d\n", len
);
333 temp
= (LPSTR
)HeapAlloc(GetProcessHeap(), 0, len
+sizeof(WCHAR
));
335 return E_OUTOFMEMORY
;
337 r
= stm
->Read(temp
, len
, &count
);
338 if( FAILED (r
) || ( count
!= len
) )
340 HeapFree( GetProcessHeap(), 0, temp
);
344 TRACE("read %s\n", debugstr_an(temp
,len
));
346 /* convert to unicode if necessary */
349 count
= MultiByteToWideChar( CP_ACP
, 0, temp
, len
, NULL
, 0 );
350 str
= (LPWSTR
)HeapAlloc( GetProcessHeap(), 0, (count
+1)*sizeof (WCHAR
) );
353 HeapFree( GetProcessHeap(), 0, temp
);
354 return E_OUTOFMEMORY
;
356 MultiByteToWideChar( CP_ACP
, 0, temp
, len
, str
, count
);
357 HeapFree( GetProcessHeap(), 0, temp
);
371 static HRESULT
Stream_ReadChunk( IStream
* stm
, LPVOID
*data
)
378 unsigned char data
[1];
383 r
= stm
->Read(&size
, sizeof(size
), &count
);
384 if( FAILED( r
) || count
!= sizeof(size
) )
387 chunk
= (sized_chunk
*)HeapAlloc( GetProcessHeap(), 0, size
);
389 return E_OUTOFMEMORY
;
392 r
= stm
->Read(chunk
->data
, size
- sizeof(size
), &count
);
393 if( FAILED( r
) || count
!= (size
- sizeof(size
)) )
395 HeapFree( GetProcessHeap(), 0, chunk
);
399 TRACE("Read %d bytes\n",chunk
->size
);
406 static BOOL
Stream_LoadVolume( LOCAL_VOLUME_INFO
*vol
, ShellLink::volume_info
*volume
)
408 const int label_sz
= sizeof volume
->label
/sizeof volume
->label
[0];
412 volume
->serial
= vol
->dwVolSerial
;
413 volume
->type
= vol
->dwType
;
415 if( !vol
->dwVolLabelOfs
)
417 if( vol
->dwSize
<= vol
->dwVolLabelOfs
)
419 len
= vol
->dwSize
- vol
->dwVolLabelOfs
;
422 label
+= vol
->dwVolLabelOfs
;
423 MultiByteToWideChar( CP_ACP
, 0, label
, len
, volume
->label
, label_sz
-1);
428 static LPWSTR
Stream_LoadPath( LPCSTR p
, DWORD maxlen
)
430 unsigned int len
= 0, wlen
;
433 while( p
[len
] && (len
< maxlen
) )
436 wlen
= MultiByteToWideChar(CP_ACP
, 0, p
, len
, NULL
, 0);
437 path
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, (wlen
+1)*sizeof(WCHAR
));
438 MultiByteToWideChar(CP_ACP
, 0, p
, len
, path
, wlen
);
444 static HRESULT
Stream_LoadLocation( IStream
*stm
,
445 ShellLink::volume_info
*volume
, LPWSTR
*path
)
452 r
= Stream_ReadChunk( stm
, (LPVOID
*) &p
);
456 loc
= (LOCATION_INFO
*) p
;
457 if (loc
->dwTotalSize
< sizeof(LOCATION_INFO
))
459 HeapFree( GetProcessHeap(), 0, p
);
463 /* if there's valid local volume information, load it */
464 if( loc
->dwVolTableOfs
&&
465 ((loc
->dwVolTableOfs
+ sizeof(LOCAL_VOLUME_INFO
)) <= loc
->dwTotalSize
) )
467 LOCAL_VOLUME_INFO
*volume_info
;
469 volume_info
= (LOCAL_VOLUME_INFO
*) &p
[loc
->dwVolTableOfs
];
470 Stream_LoadVolume( volume_info
, volume
);
473 /* if there's a local path, load it */
474 n
= loc
->dwLocalPathOfs
;
475 if( n
&& (n
< loc
->dwTotalSize
) )
476 *path
= Stream_LoadPath( &p
[n
], loc
->dwTotalSize
- n
);
478 TRACE("type %d serial %08x name %s path %s\n", volume
->type
,
479 volume
->serial
, debugstr_w(volume
->label
), debugstr_w(*path
));
481 HeapFree( GetProcessHeap(), 0, p
);
486 * The format of the advertised shortcut info seems to be:
491 * 0 Length of the block (4 bytes, usually 0x314)
493 * 8 string data in ASCII
494 * 8+0x104 string data in UNICODE
496 * In the original Win32 implementation the buffers are not initialized
497 * to zero, so data trailing the string is random garbage.
499 static HRESULT
Stream_LoadAdvertiseInfo( IStream
* stm
, LPWSTR
*str
)
504 EXP_DARWIN_LINK buffer
;
508 r
= stm
->Read(&buffer
.dbh
.cbSize
, sizeof (DWORD
), &count
);
512 /* make sure that we read the size of the structure even on error */
513 size
= sizeof buffer
- sizeof (DWORD
);
514 if( buffer
.dbh
.cbSize
!= sizeof buffer
)
516 ERR("Ooops. This structure is not as expected...\n");
520 r
= stm
->Read(&buffer
.dbh
.dwSignature
, size
, &count
);
527 TRACE("magic %08x string = %s\n", buffer
.dbh
.dwSignature
, debugstr_w(buffer
.szwDarwinID
));
529 if( (buffer
.dbh
.dwSignature
&0xffff0000) != 0xa0000000 )
531 ERR("Unknown magic number %08x in advertised shortcut\n", buffer
.dbh
.dwSignature
);
535 *str
= (LPWSTR
)HeapAlloc( GetProcessHeap(), 0,
536 (wcslen(buffer
.szwDarwinID
)+1) * sizeof(WCHAR
) );
537 wcscpy( *str
, buffer
.szwDarwinID
);
542 /************************************************************************
543 * IPersistStream_Load (IPersistStream)
545 HRESULT WINAPI
ShellLink::Load(IStream
*stm
)
553 TRACE("%p %p\n", this, stm
);
556 return STG_E_INVALIDPOINTER
;
559 r
= stm
->Read(&hdr
, sizeof(hdr
), &dwBytesRead
);
563 if (dwBytesRead
!= sizeof(hdr
))
565 if (hdr
.dwSize
!= sizeof(hdr
))
567 if (!IsEqualIID(hdr
.MagicGuid
, CLSID_ShellLink
))
570 /* free all the old stuff */
573 memset( &volume
, 0, sizeof volume
);
574 HeapFree(GetProcessHeap(), 0, sPath
);
576 HeapFree(GetProcessHeap(), 0, sDescription
);
578 HeapFree(GetProcessHeap(), 0, sPathRel
);
580 HeapFree(GetProcessHeap(), 0, sWorkDir
);
582 HeapFree(GetProcessHeap(), 0, sArgs
);
584 HeapFree(GetProcessHeap(), 0, sIcoPath
);
586 HeapFree(GetProcessHeap(), 0, sProduct
);
588 HeapFree(GetProcessHeap(), 0, sComponent
);
591 wHotKey
= (WORD
)hdr
.wHotKey
;
593 FileTimeToSystemTime (&hdr
.Time1
, &time1
);
594 FileTimeToSystemTime (&hdr
.Time2
, &time2
);
595 FileTimeToSystemTime (&hdr
.Time3
, &time3
);
598 WCHAR sTemp
[MAX_PATH
];
599 GetDateFormatW(LOCALE_USER_DEFAULT
,DATE_SHORTDATE
, &time1
,
600 NULL
, sTemp
, sizeof(sTemp
)/sizeof(*sTemp
));
601 TRACE("-- time1: %s\n", debugstr_w(sTemp
) );
602 GetDateFormatW(LOCALE_USER_DEFAULT
,DATE_SHORTDATE
, &time2
,
603 NULL
, sTemp
, sizeof(sTemp
)/sizeof(*sTemp
));
604 TRACE("-- time2: %s\n", debugstr_w(sTemp
) );
605 GetDateFormatW(LOCALE_USER_DEFAULT
,DATE_SHORTDATE
, &time3
,
606 NULL
, sTemp
, sizeof(sTemp
)/sizeof(*sTemp
));
607 TRACE("-- time3: %s\n", debugstr_w(sTemp
) );
610 /* load all the new stuff */
611 if( hdr
.dwFlags
& SLDF_HAS_ID_LIST
)
613 r
= ILLoadFromStream( stm
, &pPidl
);
619 /* load the location information */
620 if( hdr
.dwFlags
& SLDF_HAS_LINK_INFO
)
621 r
= Stream_LoadLocation( stm
, &volume
, &sPath
);
625 unicode
= hdr
.dwFlags
& SLDF_UNICODE
;
626 if( hdr
.dwFlags
& SLDF_HAS_NAME
)
628 r
= Stream_LoadString( stm
, unicode
, &sDescription
);
629 TRACE("Description -> %s\n",debugstr_w(sDescription
));
634 if( hdr
.dwFlags
& SLDF_HAS_RELPATH
)
636 r
= Stream_LoadString( stm
, unicode
, &sPathRel
);
637 TRACE("Relative Path-> %s\n",debugstr_w(sPathRel
));
642 if( hdr
.dwFlags
& SLDF_HAS_WORKINGDIR
)
644 r
= Stream_LoadString( stm
, unicode
, &sWorkDir
);
645 TRACE("Working Dir -> %s\n",debugstr_w(sWorkDir
));
650 if( hdr
.dwFlags
& SLDF_HAS_ARGS
)
652 r
= Stream_LoadString( stm
, unicode
, &sArgs
);
653 TRACE("Working Dir -> %s\n",debugstr_w(sArgs
));
658 if( hdr
.dwFlags
& SLDF_HAS_ICONLOCATION
)
660 r
= Stream_LoadString( stm
, unicode
, &sIcoPath
);
661 TRACE("Icon file -> %s\n",debugstr_w(sIcoPath
));
666 #if (NTDDI_VERSION < NTDDI_LONGHORN)
667 if( hdr
.dwFlags
& SLDF_HAS_LOGO3ID
)
669 r
= Stream_LoadAdvertiseInfo( stm
, &sProduct
);
670 TRACE("Product -> %s\n",debugstr_w(sProduct
));
676 if( hdr
.dwFlags
& SLDF_HAS_DARWINID
)
678 r
= Stream_LoadAdvertiseInfo( stm
, &sComponent
);
679 TRACE("Component -> %s\n",debugstr_w(sComponent
));
681 if( hdr
.dwFlags
& SLDF_RUNAS_USER
)
693 r
= stm
->Read(&zero
, sizeof zero
, &dwBytesRead
);
694 if( FAILED( r
) || zero
|| dwBytesRead
!= sizeof zero
)
695 ERR("Last word was not zero\n");
706 /************************************************************************
709 * Helper function for IPersistStream_Save. Writes a unicode string
710 * with terminating nul byte to a stream, preceded by the its length.
712 static HRESULT
Stream_WriteString( IStream
* stm
, LPCWSTR str
)
714 USHORT len
= wcslen( str
) + 1;
718 r
= stm
->Write(&len
, sizeof(len
), &count
);
722 len
*= sizeof(WCHAR
);
724 r
= stm
->Write(str
, len
, &count
);
731 /************************************************************************
732 * Stream_WriteLocationInfo
734 * Writes the location info to a stream
736 * FIXME: One day we might want to write the network volume information
737 * and the final path.
738 * Figure out how Windows deals with unicode paths here.
740 static HRESULT
Stream_WriteLocationInfo( IStream
* stm
, LPCWSTR path
,
741 ShellLink::volume_info
*volume
)
743 DWORD total_size
, path_size
, volume_info_size
, label_size
, final_path_size
;
744 LOCAL_VOLUME_INFO
*vol
;
746 LPSTR szLabel
, szPath
, szFinalPath
;
750 TRACE("%p %s %p\n", stm
, debugstr_w(path
), volume
);
752 /* figure out the size of everything */
753 label_size
= WideCharToMultiByte( CP_ACP
, 0, volume
->label
, -1,
754 NULL
, 0, NULL
, NULL
);
755 path_size
= WideCharToMultiByte( CP_ACP
, 0, path
, -1,
756 NULL
, 0, NULL
, NULL
);
757 volume_info_size
= sizeof *vol
+ label_size
;
759 total_size
= sizeof *loc
+ volume_info_size
+ path_size
+ final_path_size
;
761 /* create pointers to everything */
762 loc
= (LOCATION_INFO
*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, total_size
);
763 vol
= (LOCAL_VOLUME_INFO
*) &loc
[1];
764 szLabel
= (LPSTR
) &vol
[1];
765 szPath
= &szLabel
[label_size
];
766 szFinalPath
= &szPath
[path_size
];
768 /* fill in the location information header */
769 loc
->dwTotalSize
= total_size
;
770 loc
->dwHeaderSize
= sizeof (*loc
);
772 loc
->dwVolTableOfs
= sizeof (*loc
);
773 loc
->dwLocalPathOfs
= sizeof (*loc
) + volume_info_size
;
774 loc
->dwNetworkVolTableOfs
= 0;
775 loc
->dwFinalPathOfs
= sizeof (*loc
) + volume_info_size
+ path_size
;
777 /* fill in the volume information */
778 vol
->dwSize
= volume_info_size
;
779 vol
->dwType
= volume
->type
;
780 vol
->dwVolSerial
= volume
->serial
;
781 vol
->dwVolLabelOfs
= sizeof (*vol
);
783 /* copy in the strings */
784 WideCharToMultiByte( CP_ACP
, 0, volume
->label
, -1,
785 szLabel
, label_size
, NULL
, NULL
);
786 WideCharToMultiByte( CP_ACP
, 0, path
, -1,
787 szPath
, path_size
, NULL
, NULL
);
790 hr
= stm
->Write(loc
, total_size
, &count
);
791 HeapFree(GetProcessHeap(), 0, loc
);
796 static EXP_DARWIN_LINK
* shelllink_build_darwinid( LPCWSTR string
, DWORD magic
)
798 EXP_DARWIN_LINK
*buffer
;
800 buffer
= (EXP_DARWIN_LINK
*)LocalAlloc( LMEM_ZEROINIT
, sizeof *buffer
);
801 buffer
->dbh
.cbSize
= sizeof *buffer
;
802 buffer
->dbh
.dwSignature
= magic
;
803 lstrcpynW( buffer
->szwDarwinID
, string
, MAX_PATH
);
804 WideCharToMultiByte(CP_ACP
, 0, string
, -1, buffer
->szDarwinID
, MAX_PATH
, NULL
, NULL
);
809 static HRESULT
Stream_WriteAdvertiseInfo( IStream
* stm
, LPCWSTR string
, DWORD magic
)
811 EXP_DARWIN_LINK
*buffer
;
816 buffer
= shelllink_build_darwinid( string
, magic
);
818 return stm
->Write(buffer
, buffer
->dbh
.cbSize
, &count
);
821 /************************************************************************
822 * IPersistStream_Save (IPersistStream)
824 * FIXME: makes assumptions about byte order
826 HRESULT WINAPI
ShellLink::Save(IStream
*stm
, BOOL fClearDirty
)
833 TRACE("%p %p %x\n", this, stm
, fClearDirty
);
835 memset(&header
, 0, sizeof(header
));
836 header
.dwSize
= sizeof(header
);
837 header
.fStartup
= iShowCmd
;
838 header
.MagicGuid
= CLSID_ShellLink
;
840 header
.wHotKey
= wHotKey
;
841 header
.nIcon
= iIcoNdx
;
842 header
.dwFlags
= SLDF_UNICODE
; /* strings are in unicode */
844 header
.dwFlags
|= SLDF_HAS_ID_LIST
;
846 header
.dwFlags
|= SLDF_HAS_LINK_INFO
;
848 header
.dwFlags
|= SLDF_HAS_NAME
;
850 header
.dwFlags
|= SLDF_HAS_WORKINGDIR
;
852 header
.dwFlags
|= SLDF_HAS_ARGS
;
854 header
.dwFlags
|= SLDF_HAS_ICONLOCATION
;
855 #if (NTDDI_VERSION < NTDDI_LONGHORN)
857 header
.dwFlags
|= SLDF_HAS_LOGO3ID
;
860 header
.dwFlags
|= SLDF_HAS_DARWINID
;
862 header
.dwFlags
|= SLDF_RUNAS_USER
;
864 SystemTimeToFileTime ( &time1
, &header
.Time1
);
865 SystemTimeToFileTime ( &time2
, &header
.Time2
);
866 SystemTimeToFileTime ( &time3
, &header
.Time3
);
868 /* write the Shortcut header */
869 r
= stm
->Write(&header
, sizeof(header
), &count
);
872 ERR("Write failed at %d\n",__LINE__
);
876 TRACE("Writing pidl\n");
878 /* write the PIDL to the shortcut */
881 r
= ILSaveToStream( stm
, pPidl
);
884 ERR("Failed to write PIDL at %d\n",__LINE__
);
890 Stream_WriteLocationInfo( stm
, sPath
, &volume
);
893 r
= Stream_WriteString( stm
, sDescription
);
896 r
= Stream_WriteString( stm
, sPathRel
);
899 r
= Stream_WriteString( stm
, sWorkDir
);
902 r
= Stream_WriteString( stm
, sArgs
);
905 r
= Stream_WriteString( stm
, sIcoPath
);
908 r
= Stream_WriteAdvertiseInfo( stm
, sProduct
, EXP_SZ_ICON_SIG
);
911 r
= Stream_WriteAdvertiseInfo( stm
, sComponent
, EXP_DARWIN_ID_SIG
);
913 /* the last field is a single zero dword */
915 r
= stm
->Write(&zero
, sizeof zero
, &count
);
920 /************************************************************************
921 * IPersistStream_GetSizeMax (IPersistStream)
923 HRESULT WINAPI
ShellLink::GetSizeMax(ULARGE_INTEGER
*pcbSize
)
925 TRACE("(%p)\n", this);
930 static BOOL
SHELL_ExistsFileW(LPCWSTR path
)
932 if (INVALID_FILE_ATTRIBUTES
== GetFileAttributesW(path
))
937 /**************************************************************************
938 * ShellLink_UpdatePath
939 * update absolute path in sPath using relative path in sPathRel
941 static HRESULT
ShellLink_UpdatePath(LPCWSTR sPathRel
, LPCWSTR path
, LPCWSTR sWorkDir
, LPWSTR
* psPath
)
943 if (!path
|| !psPath
)
946 if (!*psPath
&& sPathRel
) {
947 WCHAR buffer
[2*MAX_PATH
], abs_path
[2*MAX_PATH
];
950 /* first try if [directory of link file] + [relative path] finds an existing file */
952 GetFullPathNameW( path
, MAX_PATH
*2, buffer
, &final
);
955 wcscpy(final
, sPathRel
);
959 if (SHELL_ExistsFileW(buffer
)) {
960 if (!GetFullPathNameW(buffer
, MAX_PATH
, abs_path
, &final
))
961 wcscpy(abs_path
, buffer
);
963 /* try if [working directory] + [relative path] finds an existing file */
965 wcscpy(buffer
, sWorkDir
);
966 wcscpy(PathAddBackslashW(buffer
), sPathRel
);
968 if (SHELL_ExistsFileW(buffer
))
969 if (!GetFullPathNameW(buffer
, MAX_PATH
, abs_path
, &final
))
970 wcscpy(abs_path
, buffer
);
974 /* FIXME: This is even not enough - not all shell links can be resolved using this algorithm. */
976 wcscpy(abs_path
, sPathRel
);
978 *psPath
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, (wcslen(abs_path
)+1)*sizeof(WCHAR
));
980 return E_OUTOFMEMORY
;
982 wcscpy(*psPath
, abs_path
);
988 HRESULT WINAPI
ShellLink::GetPath(LPSTR pszFile
, INT cchMaxPath
, WIN32_FIND_DATAA
*pfd
, DWORD fFlags
)
990 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%u)(%s)\n",
991 this, pszFile
, cchMaxPath
, pfd
, fFlags
, debugstr_w(sPath
));
993 if (sComponent
|| sProduct
)
999 WideCharToMultiByte( CP_ACP
, 0, sPath
, -1,
1000 pszFile
, cchMaxPath
, NULL
, NULL
);
1002 if (pfd
) FIXME("(%p): WIN32_FIND_DATA is not yet filled.\n", this);
1007 HRESULT WINAPI
ShellLink::GetIDList(LPITEMIDLIST
* ppidl
)
1009 TRACE("(%p)->(ppidl=%p)\n",this, ppidl
);
1016 *ppidl
= ILClone(pPidl
);
1020 HRESULT WINAPI
ShellLink::SetIDList(LPCITEMIDLIST pidl
)
1022 TRACE("(%p)->(pidl=%p)\n",this, pidl
);
1026 pPidl
= ILClone( pidl
);
1035 HRESULT WINAPI
ShellLink::GetDescription(LPSTR pszName
,INT cchMaxName
)
1037 TRACE("(%p)->(%p len=%u)\n",this, pszName
, cchMaxName
);
1042 WideCharToMultiByte( CP_ACP
, 0, sDescription
, -1,
1043 pszName
, cchMaxName
, NULL
, NULL
);
1048 HRESULT WINAPI
ShellLink::SetDescription(LPCSTR pszName
)
1050 TRACE("(%p)->(pName=%s)\n", this, pszName
);
1052 HeapFree(GetProcessHeap(), 0, sDescription
);
1053 sDescription
= NULL
;
1056 sDescription
= HEAP_strdupAtoW( GetProcessHeap(), 0, pszName
);
1057 if ( !sDescription
)
1058 return E_OUTOFMEMORY
;
1065 HRESULT WINAPI
ShellLink::GetWorkingDirectory(LPSTR pszDir
,INT cchMaxPath
)
1067 TRACE("(%p)->(%p len=%u)\n", this, pszDir
, cchMaxPath
);
1072 WideCharToMultiByte( CP_ACP
, 0, sWorkDir
, -1,
1073 pszDir
, cchMaxPath
, NULL
, NULL
);
1078 HRESULT WINAPI
ShellLink::SetWorkingDirectory(LPCSTR pszDir
)
1080 TRACE("(%p)->(dir=%s)\n",this, pszDir
);
1082 HeapFree(GetProcessHeap(), 0, sWorkDir
);
1086 sWorkDir
= HEAP_strdupAtoW( GetProcessHeap(), 0, pszDir
);
1088 return E_OUTOFMEMORY
;
1095 HRESULT WINAPI
ShellLink::GetArguments(LPSTR pszArgs
,INT cchMaxPath
)
1097 TRACE("(%p)->(%p len=%u)\n", this, pszArgs
, cchMaxPath
);
1102 WideCharToMultiByte( CP_ACP
, 0, sArgs
, -1,
1103 pszArgs
, cchMaxPath
, NULL
, NULL
);
1108 HRESULT WINAPI
ShellLink::SetArguments(LPCSTR pszArgs
)
1110 TRACE("(%p)->(args=%s)\n",this, pszArgs
);
1112 HeapFree(GetProcessHeap(), 0, sArgs
);
1116 sArgs
= HEAP_strdupAtoW( GetProcessHeap(), 0, pszArgs
);
1118 return E_OUTOFMEMORY
;
1126 HRESULT WINAPI
ShellLink::GetHotkey(WORD
*pwHotkey
)
1128 TRACE("(%p)->(%p)(0x%08x)\n",this, pwHotkey
, wHotKey
);
1130 *pwHotkey
= wHotKey
;
1135 HRESULT WINAPI
ShellLink::SetHotkey(WORD wHotkey
)
1137 TRACE("(%p)->(hotkey=%x)\n",this, wHotkey
);
1145 HRESULT WINAPI
ShellLink::GetShowCmd(INT
*piShowCmd
)
1147 TRACE("(%p)->(%p)\n",this, piShowCmd
);
1148 *piShowCmd
= iShowCmd
;
1152 HRESULT WINAPI
ShellLink::SetShowCmd(INT iShowCmd
)
1154 TRACE("(%p) %d\n",this, iShowCmd
);
1156 this->iShowCmd
= iShowCmd
;
1162 static HRESULT
SHELL_PidlGeticonLocationA(IShellFolder
* psf
, LPCITEMIDLIST pidl
,
1163 LPSTR pszIconPath
, int cchIconPath
, int* piIcon
)
1165 LPCITEMIDLIST pidlLast
;
1167 HRESULT hr
= SHBindToParent(pidl
, IID_IShellFolder
, (LPVOID
*)&psf
, &pidlLast
);
1169 if (SUCCEEDED(hr
)) {
1170 CComPtr
<IExtractIconA
> pei
;
1172 hr
= psf
->GetUIObjectOf(0, 1, &pidlLast
, IID_IExtractIconA
, NULL
, (LPVOID
*)&pei
);
1174 if (SUCCEEDED(hr
)) {
1175 hr
= pei
->GetIconLocation(0, pszIconPath
, MAX_PATH
, piIcon
, NULL
);
1184 HRESULT WINAPI
ShellLink::GetIconLocation(LPSTR pszIconPath
,INT cchIconPath
,INT
*piIcon
)
1186 TRACE("(%p)->(%p len=%u iicon=%p)\n", this, pszIconPath
, cchIconPath
, piIcon
);
1193 WideCharToMultiByte(CP_ACP
, 0, sIcoPath
, -1, pszIconPath
, cchIconPath
, NULL
, NULL
);
1199 CComPtr
<IShellFolder
> pdsk
;
1201 HRESULT hr
= SHGetDesktopFolder(&pdsk
);
1205 /* first look for an icon using the PIDL (if present) */
1207 hr
= SHELL_PidlGeticonLocationA(pdsk
, pPidl
, pszIconPath
, cchIconPath
, piIcon
);
1211 /* if we couldn't find an icon yet, look for it using the file system path */
1212 if (FAILED(hr
) && sPath
)
1216 hr
= pdsk
->ParseDisplayName(0, NULL
, sPath
, NULL
, &pidl
, NULL
);
1220 hr
= SHELL_PidlGeticonLocationA(pdsk
, pidl
, pszIconPath
, cchIconPath
, piIcon
);
1232 HRESULT WINAPI
ShellLink::SetIconLocation(LPCSTR pszIconPath
,INT iIcon
)
1234 TRACE("(%p)->(path=%s iicon=%u)\n",this, pszIconPath
, iIcon
);
1236 HeapFree(GetProcessHeap(), 0, sIcoPath
);
1241 sIcoPath
= HEAP_strdupAtoW(GetProcessHeap(), 0, pszIconPath
);
1243 return E_OUTOFMEMORY
;
1252 HRESULT WINAPI
ShellLink::SetRelativePath(LPCSTR pszPathRel
, DWORD dwReserved
)
1254 TRACE("(%p)->(path=%s %x)\n",this, pszPathRel
, dwReserved
);
1256 HeapFree(GetProcessHeap(), 0, sPathRel
);
1261 sPathRel
= HEAP_strdupAtoW(GetProcessHeap(), 0, pszPathRel
);
1265 return ShellLink_UpdatePath(sPathRel
, sPath
, sWorkDir
, &sPath
);
1268 HRESULT WINAPI
ShellLink::Resolve(HWND hwnd
, DWORD fFlags
)
1273 TRACE("(%p)->(hwnd=%p flags=%x)\n",this, hwnd
, fFlags
);
1275 /*FIXME: use IResolveShellLink interface */
1277 if (!sPath
&& pPidl
)
1279 WCHAR buffer
[MAX_PATH
];
1281 bSuccess
= SHGetPathFromIDListW(pPidl
, buffer
);
1283 if (bSuccess
&& *buffer
)
1285 sPath
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, (wcslen(buffer
)+1)*sizeof(WCHAR
));
1288 return E_OUTOFMEMORY
;
1290 wcscpy(sPath
, buffer
);
1295 hr
= S_OK
; /* don't report an error occurred while just caching information */
1298 if (!sIcoPath
&& sPath
)
1300 sIcoPath
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, (wcslen(sPath
)+1)*sizeof(WCHAR
));
1303 return E_OUTOFMEMORY
;
1305 wcscpy(sIcoPath
, sPath
);
1314 HRESULT WINAPI
ShellLink::SetPath(LPCSTR pszFile
)
1319 TRACE("(%p)->(path=%s)\n",this, pszFile
);
1320 if (pszFile
== NULL
)
1321 return E_INVALIDARG
;
1323 str
= HEAP_strdupAtoW(GetProcessHeap(), 0, pszFile
);
1325 return E_OUTOFMEMORY
;
1328 HeapFree( GetProcessHeap(), 0, str
);
1333 HRESULT WINAPI
ShellLink::GetPath(LPWSTR pszFile
,INT cchMaxPath
, WIN32_FIND_DATAW
*pfd
, DWORD fFlags
)
1335 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%u)(%s)\n",
1336 this, pszFile
, cchMaxPath
, pfd
, fFlags
, debugstr_w(sPath
));
1338 if (sComponent
|| sProduct
)
1345 lstrcpynW( pszFile
, sPath
, cchMaxPath
);
1347 if (pfd
) FIXME("(%p): WIN32_FIND_DATA is not yet filled.\n", this);
1352 HRESULT WINAPI
ShellLink::GetDescription(LPWSTR pszName
,INT cchMaxName
)
1354 TRACE("(%p)->(%p len=%u)\n",this, pszName
, cchMaxName
);
1358 lstrcpynW( pszName
, sDescription
, cchMaxName
);
1363 HRESULT WINAPI
ShellLink::SetDescription(LPCWSTR pszName
)
1365 TRACE("(%p)->(desc=%s)\n",this, debugstr_w(pszName
));
1367 HeapFree(GetProcessHeap(), 0, sDescription
);
1368 sDescription
= (LPWSTR
)HeapAlloc( GetProcessHeap(), 0,
1369 (wcslen( pszName
)+1)*sizeof(WCHAR
) );
1370 if ( !sDescription
)
1371 return E_OUTOFMEMORY
;
1373 wcscpy( sDescription
, pszName
);
1379 HRESULT WINAPI
ShellLink::GetWorkingDirectory(LPWSTR pszDir
,INT cchMaxPath
)
1381 TRACE("(%p)->(%p len %u)\n", this, pszDir
, cchMaxPath
);
1386 lstrcpynW( pszDir
, sWorkDir
, cchMaxPath
);
1391 HRESULT WINAPI
ShellLink::SetWorkingDirectory(LPCWSTR pszDir
)
1393 TRACE("(%p)->(dir=%s)\n",this, debugstr_w(pszDir
));
1395 HeapFree(GetProcessHeap(), 0, sWorkDir
);
1396 sWorkDir
= (LPWSTR
)HeapAlloc( GetProcessHeap(), 0,
1397 (wcslen( pszDir
)+1)*sizeof (WCHAR
) );
1399 return E_OUTOFMEMORY
;
1400 wcscpy( sWorkDir
, pszDir
);
1406 HRESULT WINAPI
ShellLink::GetArguments(LPWSTR pszArgs
,INT cchMaxPath
)
1408 TRACE("(%p)->(%p len=%u)\n", this, pszArgs
, cchMaxPath
);
1413 lstrcpynW( pszArgs
, sArgs
, cchMaxPath
);
1418 HRESULT WINAPI
ShellLink::SetArguments(LPCWSTR pszArgs
)
1420 TRACE("(%p)->(args=%s)\n",this, debugstr_w(pszArgs
));
1422 HeapFree(GetProcessHeap(), 0, sArgs
);
1423 sArgs
= (LPWSTR
)HeapAlloc( GetProcessHeap(), 0,
1424 (wcslen( pszArgs
)+1)*sizeof (WCHAR
) );
1426 return E_OUTOFMEMORY
;
1427 wcscpy( sArgs
, pszArgs
);
1433 static HRESULT
SHELL_PidlGeticonLocationW(IShellFolder
* psf
, LPCITEMIDLIST pidl
,
1434 LPWSTR pszIconPath
, int cchIconPath
, int* piIcon
)
1436 LPCITEMIDLIST pidlLast
;
1439 HRESULT hr
= SHBindToParent(pidl
, IID_IShellFolder
, (LPVOID
*)&psf
, &pidlLast
);
1441 if (SUCCEEDED(hr
)) {
1442 CComPtr
<IExtractIconW
> pei
;
1444 hr
= psf
->GetUIObjectOf(0, 1, &pidlLast
, IID_IExtractIconW
, NULL
, (LPVOID
*)&pei
);
1446 if (SUCCEEDED(hr
)) {
1447 hr
= pei
->GetIconLocation(0, pszIconPath
, MAX_PATH
, piIcon
, &wFlags
);
1456 HRESULT WINAPI
ShellLink::GetIconLocation(LPWSTR pszIconPath
,INT cchIconPath
,INT
*piIcon
)
1458 TRACE("(%p)->(%p len=%u iicon=%p)\n", this, pszIconPath
, cchIconPath
, piIcon
);
1465 lstrcpynW(pszIconPath
, sIcoPath
, cchIconPath
);
1471 CComPtr
<IShellFolder
> pdsk
;
1473 HRESULT hr
= SHGetDesktopFolder(&pdsk
);
1477 /* first look for an icon using the PIDL (if present) */
1479 hr
= SHELL_PidlGeticonLocationW(pdsk
, pPidl
, pszIconPath
, cchIconPath
, piIcon
);
1483 /* if we couldn't find an icon yet, look for it using the file system path */
1484 if (FAILED(hr
) && sPath
)
1488 hr
= pdsk
->ParseDisplayName(0, NULL
, sPath
, NULL
, &pidl
, NULL
);
1492 hr
= SHELL_PidlGeticonLocationW(pdsk
, pidl
, pszIconPath
, cchIconPath
, piIcon
);
1503 HRESULT WINAPI
ShellLink::SetIconLocation(LPCWSTR pszIconPath
,INT iIcon
)
1505 TRACE("(%p)->(path=%s iicon=%u)\n",this, debugstr_w(pszIconPath
), iIcon
);
1507 HeapFree(GetProcessHeap(), 0, sIcoPath
);
1508 sIcoPath
= (LPWSTR
)HeapAlloc( GetProcessHeap(), 0,
1509 (wcslen( pszIconPath
)+1)*sizeof (WCHAR
) );
1511 return E_OUTOFMEMORY
;
1512 wcscpy( sIcoPath
, pszIconPath
);
1520 HRESULT WINAPI
ShellLink::SetRelativePath(LPCWSTR pszPathRel
, DWORD dwReserved
)
1522 TRACE("(%p)->(path=%s %x)\n",this, debugstr_w(pszPathRel
), dwReserved
);
1524 HeapFree(GetProcessHeap(), 0, sPathRel
);
1525 sPathRel
= (LPWSTR
)HeapAlloc( GetProcessHeap(), 0,
1526 (wcslen( pszPathRel
)+1) * sizeof (WCHAR
) );
1528 return E_OUTOFMEMORY
;
1529 wcscpy( sPathRel
, pszPathRel
);
1532 return ShellLink_UpdatePath(sPathRel
, sPath
, sWorkDir
, &sPath
);
1535 LPWSTR
ShellLink::ShellLink_GetAdvertisedArg(LPCWSTR str
)
1544 p
= wcschr( str
, ':' );
1548 ret
= (LPWSTR
)HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR
)*(len
+1));
1551 memcpy( ret
, str
, sizeof(WCHAR
)*len
);
1556 HRESULT
ShellLink::ShellLink_SetAdvertiseInfo(LPCWSTR str
)
1558 LPCWSTR szComponent
= NULL
, szProduct
= NULL
, p
;
1566 /* each segment must start with two colons */
1567 if( str
[0] != ':' || str
[1] != ':' )
1570 /* the last segment is just two colons */
1575 /* there must be a colon straight after a guid */
1576 p
= wcschr( str
, ':' );
1583 /* get the guid, and check it's validly formatted */
1584 memcpy( szGuid
, str
, sizeof(WCHAR
)*len
);
1586 r
= CLSIDFromString( szGuid
, &guid
);
1591 /* match it up to a guid that we care about */
1592 if( IsEqualGUID( guid
, SHELL32_AdvtShortcutComponent
) && !szComponent
)
1594 else if( IsEqualGUID(guid
, SHELL32_AdvtShortcutProduct
) && !szProduct
)
1599 /* skip to the next field */
1600 str
= wcschr( str
, ':' );
1605 /* we have to have a component for an advertised shortcut */
1609 sComponent
= ShellLink_GetAdvertisedArg( szComponent
);
1610 sProduct
= ShellLink_GetAdvertisedArg( szProduct
);
1612 TRACE("Component = %s\n", debugstr_w(sComponent
));
1613 TRACE("Product = %s\n", debugstr_w(sProduct
));
1618 static BOOL
ShellLink_GetVolumeInfo(LPCWSTR path
, ShellLink::volume_info
*volume
)
1620 const int label_sz
= sizeof volume
->label
/sizeof volume
->label
[0];
1621 WCHAR drive
[4] = { path
[0], ':', '\\', 0 };
1624 volume
->type
= GetDriveTypeW(drive
);
1625 r
= GetVolumeInformationW(drive
, volume
->label
, label_sz
, &volume
->serial
, NULL
, NULL
, NULL
, 0);
1626 TRACE("r = %d type %d serial %08x name %s\n", r
,
1627 volume
->type
, volume
->serial
, debugstr_w(volume
->label
));
1631 HRESULT WINAPI
ShellLink::SetPath(LPCWSTR pszFile
)
1633 WCHAR buffer
[MAX_PATH
];
1634 LPWSTR fname
, unquoted
= NULL
;
1638 TRACE("(%p)->(path=%s)\n",this, debugstr_w(pszFile
));
1640 if (!pszFile
) return E_INVALIDARG
;
1642 /* quotes at the ends of the string are stripped */
1643 len
= wcslen(pszFile
);
1644 if (pszFile
[0] == '"' && pszFile
[len
-1] == '"')
1646 unquoted
= strdupW(pszFile
);
1647 PathUnquoteSpacesW(unquoted
);
1651 /* any other quote marks are invalid */
1652 if (wcschr(pszFile
, '"'))
1654 HeapFree(GetProcessHeap(), 0, unquoted
);
1658 HeapFree(GetProcessHeap(), 0, sPath
);
1661 HeapFree(GetProcessHeap(), 0, sComponent
);
1668 if (S_OK
!= ShellLink_SetAdvertiseInfo(pszFile
))
1670 if (*pszFile
== '\0')
1672 else if (!GetFullPathNameW(pszFile
, MAX_PATH
, buffer
, &fname
))
1674 else if(!PathFileExistsW(buffer
) &&
1675 !SearchPathW(NULL
, pszFile
, NULL
, MAX_PATH
, buffer
, NULL
))
1678 pPidl
= SHSimpleIDListFromPathW(pszFile
);
1679 ShellLink_GetVolumeInfo(buffer
, &volume
);
1681 sPath
= (LPWSTR
)HeapAlloc( GetProcessHeap(), 0,
1682 (wcslen( buffer
)+1) * sizeof (WCHAR
) );
1684 return E_OUTOFMEMORY
;
1686 wcscpy(sPath
, buffer
);
1689 HeapFree(GetProcessHeap(), 0, unquoted
);
1694 HRESULT WINAPI
ShellLink::AddDataBlock(void* pDataBlock
)
1700 HRESULT WINAPI
ShellLink::CopyDataBlock(DWORD dwSig
, void** ppDataBlock
)
1702 LPVOID block
= NULL
;
1705 TRACE("%p %08x %p\n", this, dwSig
, ppDataBlock
);
1709 case EXP_DARWIN_ID_SIG
:
1712 block
= shelllink_build_darwinid( sComponent
, dwSig
);
1715 case EXP_SZ_LINK_SIG
:
1716 case NT_CONSOLE_PROPS_SIG
:
1717 case NT_FE_CONSOLE_PROPS_SIG
:
1718 case EXP_SPECIAL_FOLDER_SIG
:
1719 case EXP_SZ_ICON_SIG
:
1720 FIXME("valid but unhandled datablock %08x\n", dwSig
);
1723 ERR("unknown datablock %08x\n", dwSig
);
1725 *ppDataBlock
= block
;
1729 HRESULT WINAPI
ShellLink::RemoveDataBlock(DWORD dwSig
)
1735 HRESULT WINAPI
ShellLink::GetFlags(DWORD
* pdwFlags
)
1739 FIXME("%p %p\n", this, pdwFlags
);
1741 /* FIXME: add more */
1743 flags
|= SLDF_HAS_ARGS
;
1745 flags
|= SLDF_HAS_DARWINID
;
1747 flags
|= SLDF_HAS_ICONLOCATION
;
1748 #if (NTDDI_VERSION < NTDDI_LONGHORN)
1750 flags
|= SLDF_HAS_LOGO3ID
;
1753 flags
|= SLDF_HAS_ID_LIST
;
1760 HRESULT WINAPI
ShellLink::SetFlags(DWORD dwFlags
)
1766 /**************************************************************************
1767 * ShellLink implementation of IShellExtInit::Initialize()
1769 * Loads the shelllink from the dataobject the shell is pointing to.
1771 HRESULT WINAPI
ShellLink::Initialize(LPCITEMIDLIST pidlFolder
, IDataObject
*pdtobj
, HKEY hkeyProgID
)
1778 TRACE("%p %p %p %p\n", this, pidlFolder
, pdtobj
, hkeyProgID
);
1783 format
.cfFormat
= CF_HDROP
;
1785 format
.dwAspect
= DVASPECT_CONTENT
;
1787 format
.tymed
= TYMED_HGLOBAL
;
1789 if( FAILED(pdtobj
->GetData(&format
, &stgm
) ) )
1792 count
= DragQueryFileW((HDROP
)stgm
.hGlobal
, -1, NULL
, 0 );
1797 count
= DragQueryFileW((HDROP
)stgm
.hGlobal
, 0, NULL
, 0 );
1799 path
= (LPWSTR
)HeapAlloc( GetProcessHeap(), 0, count
*sizeof(WCHAR
) );
1802 count
= DragQueryFileW((HDROP
)stgm
.hGlobal
, 0, path
, count
);
1804 HeapFree(GetProcessHeap(), 0, path
);
1807 ReleaseStgMedium(&stgm
);
1812 HRESULT WINAPI
ShellLink::QueryContextMenu(HMENU hmenu
, UINT indexMenu
, UINT idCmdFirst
, UINT idCmdLast
, UINT uFlags
)
1818 TRACE("%p %p %u %u %u %u\n", this,
1819 hmenu
, indexMenu
, idCmdFirst
, idCmdLast
, uFlags
);
1822 return E_INVALIDARG
;
1824 if (!LoadStringW(shell32_hInstance
, IDS_OPEN_VERB
, szOpen
, sizeof(szOpen
)/sizeof(WCHAR
)))
1827 szOpen
[(sizeof(szOpen
)/sizeof(WCHAR
))-1] = L
'\0';
1829 memset( &mii
, 0, sizeof(mii
) );
1830 mii
.cbSize
= sizeof (mii
);
1831 mii
.fMask
= MIIM_TYPE
| MIIM_ID
| MIIM_STATE
;
1832 mii
.dwTypeData
= (LPWSTR
)szOpen
;
1833 mii
.cch
= wcslen( mii
.dwTypeData
);
1834 mii
.wID
= idCmdFirst
+ id
++;
1835 mii
.fState
= MFS_DEFAULT
| MFS_ENABLED
;
1836 mii
.fType
= MFT_STRING
;
1837 if (!InsertMenuItemW( hmenu
, indexMenu
, TRUE
, &mii
))
1841 return MAKE_HRESULT( SEVERITY_SUCCESS
, 0, id
);
1845 shelllink_get_msi_component_path( LPWSTR component
)
1850 r
= CommandLineFromMsiDescriptor( component
, NULL
, &sz
);
1851 if (r
!= ERROR_SUCCESS
)
1855 path
= (LPWSTR
)HeapAlloc( GetProcessHeap(), 0, sz
*sizeof(WCHAR
) );
1856 r
= CommandLineFromMsiDescriptor( component
, path
, &sz
);
1857 if (r
!= ERROR_SUCCESS
)
1859 HeapFree( GetProcessHeap(), 0, path
);
1863 TRACE("returning %s\n", debugstr_w( path
) );
1868 HRESULT WINAPI
ShellLink::InvokeCommand(LPCMINVOKECOMMANDINFO lpici
)
1870 static const WCHAR szOpen
[] = { 'o','p','e','n',0 };
1871 static const WCHAR szCplOpen
[] = { 'c','p','l','o','p','e','n',0 };
1872 SHELLEXECUTEINFOW sei
;
1873 HWND hwnd
= NULL
; /* FIXME: get using interface set from IObjectWithSite */
1878 TRACE("%p %p\n", this, lpici
);
1880 if ( lpici
->cbSize
< sizeof (CMINVOKECOMMANDINFO
) )
1881 return E_INVALIDARG
;
1883 r
= Resolve(hwnd
, 0 );
1886 TRACE("failed to resolve component with error 0x%08x", r
);
1891 path
= shelllink_get_msi_component_path( sComponent
);
1896 path
= strdupW( sPath
);
1898 if ( lpici
->cbSize
== sizeof (CMINVOKECOMMANDINFOEX
) &&
1899 ( lpici
->fMask
& CMIC_MASK_UNICODE
) )
1901 LPCMINVOKECOMMANDINFOEX iciex
= (LPCMINVOKECOMMANDINFOEX
) lpici
;
1905 len
+= wcslen( sArgs
);
1906 if ( iciex
->lpParametersW
)
1907 len
+= wcslen( iciex
->lpParametersW
);
1909 args
= (LPWSTR
)HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
) );
1912 wcscat( args
, sArgs
);
1913 if ( iciex
->lpParametersW
)
1915 static const WCHAR space
[] = { ' ', 0 };
1916 wcscat( args
, space
);
1917 wcscat( args
, iciex
->lpParametersW
);
1920 else if (sArgs
!= NULL
)
1922 args
= strdupW(sArgs
);
1925 memset( &sei
, 0, sizeof sei
);
1926 sei
.cbSize
= sizeof sei
;
1927 sei
.fMask
= SEE_MASK_UNICODE
| (lpici
->fMask
& (SEE_MASK_NOASYNC
|SEE_MASK_ASYNCOK
|SEE_MASK_FLAG_NO_UI
));
1929 sei
.nShow
= iShowCmd
;
1930 sei
.lpDirectory
= sWorkDir
;
1931 sei
.lpParameters
= args
;
1932 sei
.lpVerb
= szOpen
;
1934 // HACK for ShellExecuteExW
1935 if (!wcsstr(sPath
, L
".cpl"))
1936 sei
.lpVerb
= szOpen
;
1938 sei
.lpVerb
= szCplOpen
;
1940 if( ShellExecuteExW( &sei
) )
1945 HeapFree( GetProcessHeap(), 0, args
);
1946 HeapFree( GetProcessHeap(), 0, path
);
1951 HRESULT WINAPI
ShellLink::GetCommandString(UINT_PTR idCmd
, UINT uType
, UINT
* pwReserved
, LPSTR pszName
, UINT cchMax
)
1953 FIXME("%p %lu %u %p %p %u\n", this, idCmd
, uType
, pwReserved
, pszName
, cchMax
);
1958 INT_PTR CALLBACK
ExtendedShortcutProc(HWND hwndDlg
, UINT uMsg
,
1959 WPARAM wParam
, LPARAM lParam
)
1968 hDlgCtrl
= GetDlgItem(hwndDlg
, 14000);
1969 SendMessage(hDlgCtrl
, BM_SETCHECK
, BST_CHECKED
, 0);
1973 hDlgCtrl
= GetDlgItem(hwndDlg
, 14000);
1974 if (LOWORD(wParam
) == IDOK
)
1976 if ( SendMessage(hDlgCtrl
, BM_GETCHECK
, 0, 0) == BST_CHECKED
)
1977 EndDialog(hwndDlg
, 1);
1979 EndDialog(hwndDlg
, 0);
1981 else if (LOWORD(wParam
) == IDCANCEL
)
1983 EndDialog(hwndDlg
, -1);
1985 else if (LOWORD(wParam
) == 14000)
1987 if ( SendMessage(hDlgCtrl
, BM_GETCHECK
, 0, 0) == BST_CHECKED
)
1988 SendMessage(hDlgCtrl
, BM_SETCHECK
, BST_UNCHECKED
, 0);
1990 SendMessage(hDlgCtrl
, BM_SETCHECK
, BST_CHECKED
, 0);
1996 /**************************************************************************
1997 * SH_ShellLinkDlgProc
1999 * dialog proc of the shortcut property dialog
2002 INT_PTR CALLBACK
ShellLink::SH_ShellLinkDlgProc(HWND hwndDlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
2004 LPPROPSHEETPAGEW ppsp
;
2008 WCHAR szBuffer
[MAX_PATH
];
2013 pThis
= (ShellLink
*)GetWindowLongPtr(hwndDlg
, DWLP_USER
);
2019 ppsp
= (LPPROPSHEETPAGEW
)lParam
;
2023 TRACE("ShellLink_DlgProc (WM_INITDIALOG hwnd %p lParam %p ppsplParam %x)\n",hwndDlg
, lParam
, ppsp
->lParam
);
2025 pThis
= (ShellLink
*)ppsp
->lParam
;
2026 SetWindowLongPtr(hwndDlg
, DWLP_USER
, (LONG_PTR
)pThis
);
2028 TRACE("sArgs: %S sComponent: %S sDescription: %S sIcoPath: %S sPath: %S sPathRel: %S sProduct: %S sWorkDir: %S\n", pThis
->sArgs
, pThis
->sComponent
, pThis
->sDescription
,
2029 pThis
->sIcoPath
, pThis
->sPath
, pThis
->sPathRel
, pThis
->sProduct
, pThis
->sWorkDir
);
2031 /* target location */
2032 wchar_t * wTrgtLocat
;
2033 const int ch
= '\\';
2034 wTrgtLocat
= wcsrchr(pThis
->sWorkDir
, ch
)+1;
2035 hDlgCtrl
= GetDlgItem( hwndDlg
, 14007 );
2036 SendMessageW(hDlgCtrl
, WM_SETTEXT
, (WPARAM
)NULL
, (LPARAM
)wTrgtLocat
);
2039 hDlgCtrl
= GetDlgItem( hwndDlg
, 14009 );
2040 if ( hDlgCtrl
!= NULL
)
2041 SendMessageW( hDlgCtrl
, WM_SETTEXT
, (WPARAM
)NULL
, (LPARAM
)pThis
->sPath
);
2044 hDlgCtrl
= GetDlgItem( hwndDlg
, 14011 );
2045 if ( hDlgCtrl
!= NULL
)
2046 SendMessageW( hDlgCtrl
, WM_SETTEXT
, (WPARAM
)NULL
, (LPARAM
)pThis
->sWorkDir
);
2049 hDlgCtrl
= GetDlgItem( hwndDlg
, 14019 );
2050 if ( hDlgCtrl
!= NULL
)
2051 SendMessageW( hDlgCtrl
, WM_SETTEXT
, (WPARAM
)NULL
, (LPARAM
)pThis
->sDescription
);
2056 lppsn
= (LPPSHNOTIFY
) lParam
;
2057 if ( lppsn
->hdr
.code
== PSN_APPLY
)
2059 /* set working directory */
2060 hDlgCtrl
= GetDlgItem( hwndDlg
, 14011 );
2061 SendMessageW( hDlgCtrl
, WM_GETTEXT
, (WPARAM
)MAX_PATH
, (LPARAM
)szBuffer
);
2062 pThis
->SetWorkingDirectory(szBuffer
);
2063 /* set link destination */
2064 hDlgCtrl
= GetDlgItem( hwndDlg
, 14009 );
2065 SendMessageW( hDlgCtrl
, WM_GETTEXT
, (WPARAM
)MAX_PATH
, (LPARAM
)szBuffer
);
2066 if ( !SHELL_ExistsFileW(szBuffer
) )
2068 //FIXME load localized error msg
2069 MessageBoxW( hwndDlg
, L
"file not existing", szBuffer
, MB_OK
);
2070 SetWindowLongPtr( hwndDlg
, DWL_MSGRESULT
, PSNRET_INVALID_NOCHANGEPAGE
);
2073 ptr
= wcsrchr(szBuffer
, L
'.');
2074 if (ptr
&& !_wcsnicmp(ptr
, L
".lnk", 4))
2076 // FIXME load localized error msg
2077 MessageBoxW( hwndDlg
, L
"You cannot create a link to a shortcut", L
"Error", MB_ICONERROR
);
2078 SetWindowLongPtr( hwndDlg
, DWL_MSGRESULT
, PSNRET_INVALID_NOCHANGEPAGE
);
2082 pThis
->SetPath(szBuffer
);
2084 TRACE("This %p sLinkPath %S\n", pThis
, pThis
->sLinkPath
);
2085 pThis
->Save(pThis
->sLinkPath
, TRUE
);
2086 SetWindowLongPtr( hwndDlg
, DWL_MSGRESULT
, PSNRET_NOERROR
);
2091 switch(LOWORD(wParam
))
2096 /// open target directory
2100 if (pThis
->sIcoPath
)
2101 wcscpy(szBuffer
, pThis
->sIcoPath
);
2102 IconIndex
= pThis
->iIcoNdx
;
2103 if (PickIconDlg(hwndDlg
, szBuffer
, MAX_PATH
, &IconIndex
))
2105 pThis
->SetIconLocation(szBuffer
, IconIndex
);
2107 /// FIXME redraw icon
2111 result
= DialogBoxParamW(shell32_hInstance
, MAKEINTRESOURCEW(SHELL_EXTENDED_SHORTCUT_DLG
), hwndDlg
, ExtendedShortcutProc
, (LPARAM
)pThis
->bRunAs
);
2112 if (result
== 1 || result
== 0)
2114 if (pThis
->bRunAs
!= result
)
2116 PropSheet_Changed(GetParent(hwndDlg
), hwndDlg
);
2119 pThis
->bRunAs
= result
;
2123 switch(HIWORD(wParam
))
2126 PropSheet_Changed(GetParent(hwndDlg
), hwndDlg
);
2136 /**************************************************************************
2137 * ShellLink_IShellPropSheetExt interface
2140 HRESULT WINAPI
ShellLink::AddPages(LPFNADDPROPSHEETPAGE pfnAddPage
, LPARAM lParam
)
2142 HPROPSHEETPAGE hPage
;
2145 hPage
= SH_CreatePropertySheetPage("SHELL_GENERAL_SHORTCUT_DLG", SH_ShellLinkDlgProc
, (LPARAM
)this, NULL
);
2148 ERR("failed to create property sheet page\n");
2152 bRet
= pfnAddPage(hPage
, lParam
);
2159 HRESULT WINAPI
ShellLink::ReplacePage(UINT uPageID
, LPFNADDPROPSHEETPAGE pfnReplacePage
, LPARAM lParam
)
2161 TRACE("(%p) (uPageID %u, pfnReplacePage %p lParam %p\n", this, uPageID
, pfnReplacePage
, lParam
);
2165 HRESULT WINAPI
ShellLink::SetSite(IUnknown
*punk
)
2167 TRACE("%p %p\n", this, punk
);
2174 HRESULT WINAPI
ShellLink::GetSite(REFIID iid
, void ** ppvSite
)
2176 TRACE("%p %s %p\n", this, debugstr_guid(&iid
), ppvSite
);
2180 return site
->QueryInterface(iid
, ppvSite
);
2183 /**************************************************************************
2184 * IShellLink_ConstructFromFile
2186 HRESULT WINAPI
IShellLink_ConstructFromFile(IUnknown
*pUnkOuter
, REFIID riid
, LPCITEMIDLIST pidl
, LPVOID
*ppv
)
2188 CComPtr
<IUnknown
> psl
;
2190 HRESULT hr
= ShellLink::_CreatorClass::CreateInstance(NULL
, riid
, (void**)&psl
);
2194 CComPtr
<IPersistFile
> ppf
;
2198 hr
= psl
->QueryInterface(IID_IPersistFile
, (LPVOID
*)&ppf
);
2202 WCHAR path
[MAX_PATH
];
2204 if (SHGetPathFromIDListW(pidl
, path
))
2205 hr
= ppf
->Load(path
, 0);
2210 *ppv
= psl
.Detach();