3 * Copyright 1997 Marcus Meissner
4 * Copyright 1998 Juergen Schmied
5 * Copyright 2005 Mike McCormack
6 * Copyright 2009 Andrew Hill
7 * Copyright 2013 Dominik Hornung
8 * Copyright 2017 Hermes Belusca-Maito
9 * Copyright 2018 Katayama Hirofumi MZ <katayama.hirofumi.mz@gmail.com>
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 * Nearly complete information about the binary formats
27 * of .lnk files available at http://www.wotsit.org
29 * You can use winedump to examine the contents of a link file:
32 * MSI advertised shortcuts are totally undocumented. They provide an
33 * icon for a program that is not yet installed, and invoke MSI to
34 * install the program when the shortcut is clicked on. They are
35 * created by passing a special string to SetPath, and the information
36 * in that string is parsed an stored.
39 * In the following is listed more documentation about the Shell Link file format,
40 * as well as its interface.
42 * General introduction about "Shell Links" (MSDN):
43 * https://msdn.microsoft.com/en-us/library/windows/desktop/bb776891(v=vs.85).aspx
46 * Details of the file format:
48 * - Official MSDN documentation "[MS-SHLLINK]: Shell Link (.LNK) Binary File Format":
49 * https://msdn.microsoft.com/en-us/library/dd871305.aspx
52 * http://forensicswiki.org/wiki/LNK
53 * http://computerforensics.parsonage.co.uk/downloads/TheMeaningofLIFE.pdf
54 * https://ithreats.files.wordpress.com/2009/05/lnk_the_windows_shortcut_file_format.pdf
55 * https://github.com/libyal/liblnk/blob/master/documentation/Windows%20Shortcut%20File%20(LNK)%20format.asciidoc
57 * - List of possible shell link header flags (SHELL_LINK_DATA_FLAGS enumeration):
58 * https://msdn.microsoft.com/en-us/library/windows/desktop/bb762540(v=vs.85).aspx
59 * https://msdn.microsoft.com/en-us/library/dd891314.aspx
62 * In addition to storing its target by using a PIDL, a shell link file also
63 * stores metadata to make the shell able to track the link target, in situations
64 * where the link target is moved amongst local or network directories, or moved
65 * to different volumes. For this, two structures are used:
67 * - The first and oldest one (from NewShell/WinNT4) is the "LinkInfo" structure,
68 * stored in a serialized manner at the beginning of the shell link file:
69 * https://msdn.microsoft.com/en-us/library/dd871404.aspx
70 * The official API for manipulating this is located in LINKINFO.DLL .
72 * - The second, more recent one, is an extra binary block appended to the
73 * extra-data list of the shell link file: this is the "TrackerDataBlock":
74 * https://msdn.microsoft.com/en-us/library/dd891376.aspx
75 * Its purpose is for link tracking, and works in coordination with the
76 * "Distributed Link Tracking" service ('TrkWks' client, 'TrkSvr' server).
77 * See a detailed explanation at:
78 * http://www.serverwatch.com/tutorials/article.php/1476701/Searching-for-the-Missing-Link-Distributed-Link-Tracking.htm
81 * MSI installations most of the time create so-called "advertised shortcuts".
82 * They provide an icon for a program that may not be installed yet, and invoke
83 * MSI to install the program when the shortcut is opened (resolved).
84 * The philosophy of this approach is explained in detail inside the MSDN article
85 * "Application Resiliency: Unlock the Hidden Features of Windows Installer"
86 * (by Michael Sanford), here:
87 * https://msdn.microsoft.com/en-us/library/aa302344.aspx
89 * This functionality is implemented by adding a binary "Darwin" data block
90 * of type "EXP_DARWIN_LINK", signature EXP_DARWIN_ID_SIG == 0xA0000006,
91 * to the shell link file:
92 * https://msdn.microsoft.com/en-us/library/dd871369.aspx
93 * or, this could be done more simply by specifying a special link target path
94 * with the IShellLink::SetPath() function. Defining the following GUID:
95 * SHELL32_AdvtShortcutComponent = "::{9db1186e-40df-11d1-aa8c-00c04fb67863}:"
96 * setting a target of the form:
97 * "::{SHELL32_AdvtShortcutComponent}:<MSI_App_ID>"
98 * would automatically create the necessary binary block.
100 * With that, the target of the shortcut now becomes the MSI data. The latter
101 * is parsed from MSI and retrieved by the shell that then can run the program.
103 * This MSI functionality, dubbed "link blessing", actually originates from an
104 * older technology introduced in Internet Explorer 3 (and now obsolete since
105 * Internet Explorer 7), called "MS Internet Component Download (MSICD)", see
106 * this MSDN introductory article:
107 * https://msdn.microsoft.com/en-us/library/aa741198(v=vs.85).aspx
108 * and leveraged in Internet Explorer 4 with "Software Update Channels", see:
109 * https://msdn.microsoft.com/en-us/library/aa740931(v=vs.85).aspx
110 * Applications supporting this technology could present shell links having
111 * a special target, see subsection "Modifying the Shortcut" in the article:
112 * https://msdn.microsoft.com/en-us/library/aa741201(v=vs.85).aspx#pub_shor
114 * Similarly as for the MSI shortcuts, these MSICD shortcuts are created by
115 * specifying a special link target path with the IShellLink::SetPath() function,
116 * defining the following GUID:
117 * SHELL32_AdvtShortcutProduct = "::{9db1186f-40df-11d1-aa8c-00c04fb67863}:"
118 * and setting a target of the form:
119 * "::{SHELL32_AdvtShortcutProduct}:<AppName>::<Path>" .
120 * A tool, called "blesslnk.exe", was also provided for automatizing the process;
121 * its ReadMe can be found in the (now outdated) MS "Internet Client SDK" (INetSDK,
122 * for MS Windows 95 and NT), whose contents can be read at:
123 * http://www.msfn.org/board/topic/145352-new-windows-lnk-vulnerability/?page=4#comment-944223
124 * The MS INetSDK can be found at:
125 * https://web.archive.org/web/20100924000013/http://support.microsoft.com/kb/177877
127 * Internally the shell link target of these MSICD shortcuts is converted into
128 * a binary data block of a type similar to Darwin / "EXP_DARWIN_LINK", but with
129 * a different signature EXP_LOGO3_ID_SIG == 0xA0000007 . Such shell links are
130 * called "Logo3" shortcuts. They were evoked in this user comment in "The Old
132 * https://blogs.msdn.microsoft.com/oldnewthing/20121210-00/?p=5883#comment-1025083
134 * The shell exports the API 'SoftwareUpdateMessageBox' (in shdocvw.dll) that
135 * displays a message when an update for an application supporting this
136 * technology is available.
144 WINE_DEFAULT_DEBUG_CHANNEL(shell
);
146 #define SHLINK_LOCAL 0
147 #define SHLINK_REMOTE 1
149 /* link file formats */
151 #include "pshpack1.h"
159 DWORD dwLocalPathOfs
;
160 DWORD dwNetworkVolTableOfs
;
161 DWORD dwFinalPathOfs
;
164 struct LOCAL_VOLUME_INFO
176 WCHAR label
[12]; /* assume 8.3 */
181 /* IShellLink Implementation */
183 static HRESULT
ShellLink_UpdatePath(LPCWSTR sPathRel
, LPCWSTR path
, LPCWSTR sWorkDir
, LPWSTR
* psPath
);
185 /* strdup on the process heap */
186 static LPWSTR __inline
HEAP_strdupAtoW(HANDLE heap
, DWORD flags
, LPCSTR str
)
193 len
= MultiByteToWideChar(CP_ACP
, 0, str
, -1, NULL
, 0);
194 p
= (LPWSTR
)HeapAlloc(heap
, flags
, len
* sizeof(WCHAR
));
197 MultiByteToWideChar(CP_ACP
, 0, str
, -1, p
, len
);
201 static LPWSTR __inline
strdupW(LPCWSTR src
)
204 if (!src
) return NULL
;
205 dest
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, (wcslen(src
) + 1) * sizeof(WCHAR
));
211 // TODO: Use it for constructor & destructor too
212 VOID
CShellLink::Reset()
217 HeapFree(GetProcessHeap(), 0, m_sPath
);
219 ZeroMemory(&volume
, sizeof(volume
));
221 HeapFree(GetProcessHeap(), 0, m_sDescription
);
222 m_sDescription
= NULL
;
223 HeapFree(GetProcessHeap(), 0, m_sPathRel
);
225 HeapFree(GetProcessHeap(), 0, m_sWorkDir
);
227 HeapFree(GetProcessHeap(), 0, m_sArgs
);
229 HeapFree(GetProcessHeap(), 0, m_sIcoPath
);
236 SHFreeDataBlockList(m_pDBList
);
239 /**/sProduct
= sComponent
= NULL
;/**/
242 CShellLink::CShellLink()
244 m_Header
.dwSize
= sizeof(m_Header
);
245 m_Header
.clsid
= CLSID_ShellLink
;
246 m_Header
.dwFlags
= 0;
248 m_Header
.dwFileAttributes
= 0;
249 ZeroMemory(&m_Header
.ftCreationTime
, sizeof(m_Header
.ftCreationTime
));
250 ZeroMemory(&m_Header
.ftLastAccessTime
, sizeof(m_Header
.ftLastAccessTime
));
251 ZeroMemory(&m_Header
.ftLastWriteTime
, sizeof(m_Header
.ftLastWriteTime
));
252 m_Header
.nFileSizeLow
= 0;
254 m_Header
.nIconIndex
= 0;
255 m_Header
.nShowCommand
= SW_SHOWNORMAL
;
256 m_Header
.wHotKey
= 0;
261 ZeroMemory(&volume
, sizeof(volume
));
263 m_sDescription
= NULL
;
277 /**/sProduct
= sComponent
= NULL
;/**/
280 CShellLink::~CShellLink()
282 TRACE("-- destroying IShellLink(%p)\n", this);
286 HeapFree(GetProcessHeap(), 0, m_sPath
);
288 HeapFree(GetProcessHeap(), 0, m_sDescription
);
289 HeapFree(GetProcessHeap(), 0, m_sPathRel
);
290 HeapFree(GetProcessHeap(), 0, m_sWorkDir
);
291 HeapFree(GetProcessHeap(), 0, m_sArgs
);
292 HeapFree(GetProcessHeap(), 0, m_sIcoPath
);
293 HeapFree(GetProcessHeap(), 0, m_sLinkPath
);
294 SHFreeDataBlockList(m_pDBList
);
297 HRESULT STDMETHODCALLTYPE
CShellLink::GetClassID(CLSID
*pclsid
)
299 TRACE("%p %p\n", this, pclsid
);
303 *pclsid
= CLSID_ShellLink
;
307 /************************************************************************
308 * IPersistStream_IsDirty (IPersistStream)
310 HRESULT STDMETHODCALLTYPE
CShellLink::IsDirty()
312 TRACE("(%p)\n", this);
313 return (m_bDirty
? S_OK
: S_FALSE
);
316 HRESULT STDMETHODCALLTYPE
CShellLink::Load(LPCOLESTR pszFileName
, DWORD dwMode
)
318 TRACE("(%p, %s, %x)\n", this, debugstr_w(pszFileName
), dwMode
);
321 dwMode
= STGM_READ
| STGM_SHARE_DENY_WRITE
;
323 CComPtr
<IStream
> stm
;
324 HRESULT hr
= SHCreateStreamOnFileW(pszFileName
, dwMode
, &stm
);
327 HeapFree(GetProcessHeap(), 0, m_sLinkPath
);
328 m_sLinkPath
= strdupW(pszFileName
);
330 ShellLink_UpdatePath(m_sPathRel
, pszFileName
, m_sWorkDir
, &m_sPath
);
333 TRACE("-- returning hr %08x\n", hr
);
337 HRESULT STDMETHODCALLTYPE
CShellLink::Save(LPCOLESTR pszFileName
, BOOL fRemember
)
339 TRACE("(%p)->(%s)\n", this, debugstr_w(pszFileName
));
344 CComPtr
<IStream
> stm
;
345 HRESULT hr
= SHCreateStreamOnFileW(pszFileName
, STGM_READWRITE
| STGM_CREATE
| STGM_SHARE_EXCLUSIVE
, &stm
);
348 hr
= Save(stm
, FALSE
);
352 if (pszFileName
!= m_sLinkPath
)
355 HeapFree(GetProcessHeap(), 0, m_sLinkPath
);
357 m_sLinkPath
= strdupW(pszFileName
);
364 DeleteFileW(pszFileName
);
365 WARN("Failed to create shortcut %s\n", debugstr_w(pszFileName
));
372 HRESULT STDMETHODCALLTYPE
CShellLink::SaveCompleted(LPCOLESTR pszFileName
)
374 FIXME("(%p)->(%s)\n", this, debugstr_w(pszFileName
));
378 HRESULT STDMETHODCALLTYPE
CShellLink::GetCurFile(LPOLESTR
*ppszFileName
)
380 *ppszFileName
= NULL
;
384 /* IPersistFile::GetCurFile called before IPersistFile::Save */
388 *ppszFileName
= (LPOLESTR
)CoTaskMemAlloc((wcslen(m_sLinkPath
) + 1) * sizeof(WCHAR
));
392 return E_OUTOFMEMORY
;
395 /* copy last saved filename */
396 wcscpy(*ppszFileName
, m_sLinkPath
);
401 static HRESULT
Stream_LoadString(IStream
* stm
, BOOL unicode
, LPWSTR
*pstr
)
407 HRESULT hr
= stm
->Read(&len
, sizeof(len
), &count
);
408 if (FAILED(hr
) || count
!= sizeof(len
))
412 len
*= sizeof(WCHAR
);
414 TRACE("reading %d\n", len
);
415 LPSTR temp
= (LPSTR
)HeapAlloc(GetProcessHeap(), 0, len
+ sizeof(WCHAR
));
417 return E_OUTOFMEMORY
;
419 hr
= stm
->Read(temp
, len
, &count
);
420 if (FAILED(hr
) || count
!= len
)
422 HeapFree(GetProcessHeap(), 0, temp
);
426 TRACE("read %s\n", debugstr_an(temp
, len
));
428 /* convert to unicode if necessary */
432 count
= MultiByteToWideChar(CP_ACP
, 0, temp
, len
, NULL
, 0);
433 str
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, (count
+ 1) * sizeof(WCHAR
));
436 HeapFree(GetProcessHeap(), 0, temp
);
437 return E_OUTOFMEMORY
;
439 MultiByteToWideChar(CP_ACP
, 0, temp
, len
, str
, count
);
440 HeapFree(GetProcessHeap(), 0, temp
);
444 count
/= sizeof(WCHAR
);
456 * NOTE: The following 5 functions are part of LINKINFO.DLL
458 static BOOL
ShellLink_GetVolumeInfo(LPCWSTR path
, CShellLink::volume_info
*volume
)
460 WCHAR drive
[4] = { path
[0], ':', '\\', 0 };
462 volume
->type
= GetDriveTypeW(drive
);
463 BOOL bRet
= GetVolumeInformationW(drive
, volume
->label
, _countof(volume
->label
), &volume
->serial
, NULL
, NULL
, NULL
, 0);
464 TRACE("ret = %d type %d serial %08x name %s\n", bRet
,
465 volume
->type
, volume
->serial
, debugstr_w(volume
->label
));
469 static HRESULT
Stream_ReadChunk(IStream
* stm
, LPVOID
*data
)
474 unsigned char data
[1];
481 HRESULT hr
= stm
->Read(&size
, sizeof(size
), &count
);
482 if (FAILED(hr
) || count
!= sizeof(size
))
485 chunk
= static_cast<sized_chunk
*>(HeapAlloc(GetProcessHeap(), 0, size
));
487 return E_OUTOFMEMORY
;
490 hr
= stm
->Read(chunk
->data
, size
- sizeof(size
), &count
);
491 if (FAILED(hr
) || count
!= (size
- sizeof(size
)))
493 HeapFree(GetProcessHeap(), 0, chunk
);
497 TRACE("Read %d bytes\n", chunk
->size
);
504 static BOOL
Stream_LoadVolume(LOCAL_VOLUME_INFO
*vol
, CShellLink::volume_info
*volume
)
506 volume
->serial
= vol
->dwVolSerial
;
507 volume
->type
= vol
->dwType
;
509 if (!vol
->dwVolLabelOfs
)
511 if (vol
->dwSize
<= vol
->dwVolLabelOfs
)
513 INT len
= vol
->dwSize
- vol
->dwVolLabelOfs
;
515 LPSTR label
= (LPSTR
)vol
;
516 label
+= vol
->dwVolLabelOfs
;
517 MultiByteToWideChar(CP_ACP
, 0, label
, len
, volume
->label
, _countof(volume
->label
));
522 static LPWSTR
Stream_LoadPath(LPCSTR p
, DWORD maxlen
)
526 while (p
[len
] && len
< maxlen
)
529 UINT wlen
= MultiByteToWideChar(CP_ACP
, 0, p
, len
, NULL
, 0);
530 LPWSTR path
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, (wlen
+ 1) * sizeof(WCHAR
));
533 MultiByteToWideChar(CP_ACP
, 0, p
, len
, path
, wlen
);
539 static HRESULT
Stream_LoadLocation(IStream
*stm
,
540 CShellLink::volume_info
*volume
, LPWSTR
*path
)
543 HRESULT hr
= Stream_ReadChunk(stm
, (LPVOID
*) &p
);
547 LOCATION_INFO
*loc
= reinterpret_cast<LOCATION_INFO
*>(p
);
548 if (loc
->dwTotalSize
< sizeof(LOCATION_INFO
))
550 HeapFree(GetProcessHeap(), 0, p
);
554 /* if there's valid local volume information, load it */
555 if (loc
->dwVolTableOfs
&&
556 ((loc
->dwVolTableOfs
+ sizeof(LOCAL_VOLUME_INFO
)) <= loc
->dwTotalSize
))
558 LOCAL_VOLUME_INFO
*volume_info
;
560 volume_info
= (LOCAL_VOLUME_INFO
*) &p
[loc
->dwVolTableOfs
];
561 Stream_LoadVolume(volume_info
, volume
);
564 /* if there's a local path, load it */
565 DWORD n
= loc
->dwLocalPathOfs
;
566 if (n
&& n
< loc
->dwTotalSize
)
567 *path
= Stream_LoadPath(&p
[n
], loc
->dwTotalSize
- n
);
569 TRACE("type %d serial %08x name %s path %s\n", volume
->type
,
570 volume
->serial
, debugstr_w(volume
->label
), debugstr_w(*path
));
572 HeapFree(GetProcessHeap(), 0, p
);
578 * The format of the advertised shortcut info is:
582 * 0 Length of the block (4 bytes, usually 0x314)
584 * 8 string data in ASCII
585 * 8+0x104 string data in UNICODE
587 * In the original Win32 implementation the buffers are not initialized
588 * to zero, so data trailing the string is random garbage.
590 HRESULT
CShellLink::GetAdvertiseInfo(LPWSTR
*str
, DWORD dwSig
)
592 LPEXP_DARWIN_LINK pInfo
;
596 pInfo
= (LPEXP_DARWIN_LINK
)SHFindDataBlock(m_pDBList
, dwSig
);
600 /* Make sure that the size of the structure is valid */
601 if (pInfo
->dbh
.cbSize
!= sizeof(*pInfo
))
603 ERR("Ooops. This structure is not as expected...\n");
607 TRACE("dwSig %08x string = '%s'\n", pInfo
->dbh
.dwSignature
, debugstr_w(pInfo
->szwDarwinID
));
609 *str
= pInfo
->szwDarwinID
;
613 /************************************************************************
614 * IPersistStream_Load (IPersistStream)
616 HRESULT STDMETHODCALLTYPE
CShellLink::Load(IStream
*stm
)
618 TRACE("%p %p\n", this, stm
);
621 return STG_E_INVALIDPOINTER
;
623 /* Free all the old stuff */
626 ULONG dwBytesRead
= 0;
627 HRESULT hr
= stm
->Read(&m_Header
, sizeof(m_Header
), &dwBytesRead
);
631 if (dwBytesRead
!= sizeof(m_Header
))
633 if (m_Header
.dwSize
!= sizeof(m_Header
))
635 if (!IsEqualIID(m_Header
.clsid
, CLSID_ShellLink
))
638 /* Load the new data in order */
642 SYSTEMTIME stCreationTime
;
643 SYSTEMTIME stLastAccessTime
;
644 SYSTEMTIME stLastWriteTime
;
645 WCHAR sTemp
[MAX_PATH
];
647 FileTimeToSystemTime(&m_Header
.ftCreationTime
, &stCreationTime
);
648 FileTimeToSystemTime(&m_Header
.ftLastAccessTime
, &stLastAccessTime
);
649 FileTimeToSystemTime(&m_Header
.ftLastWriteTime
, &stLastWriteTime
);
651 GetDateFormatW(LOCALE_USER_DEFAULT
, DATE_SHORTDATE
, &stCreationTime
,
652 NULL
, sTemp
, _countof(sTemp
));
653 TRACE("-- stCreationTime: %s\n", debugstr_w(sTemp
));
654 GetDateFormatW(LOCALE_USER_DEFAULT
, DATE_SHORTDATE
, &stLastAccessTime
,
655 NULL
, sTemp
, _countof(sTemp
));
656 TRACE("-- stLastAccessTime: %s\n", debugstr_w(sTemp
));
657 GetDateFormatW(LOCALE_USER_DEFAULT
, DATE_SHORTDATE
, &stLastWriteTime
,
658 NULL
, sTemp
, _countof(sTemp
));
659 TRACE("-- stLastWriteTime: %s\n", debugstr_w(sTemp
));
662 /* load all the new stuff */
663 if (m_Header
.dwFlags
& SLDF_HAS_ID_LIST
)
665 hr
= ILLoadFromStream(stm
, &m_pPidl
);
671 /* Load the location information... */
672 if (m_Header
.dwFlags
& SLDF_HAS_LINK_INFO
)
674 hr
= Stream_LoadLocation(stm
, &volume
, &m_sPath
);
678 /* ... but if it is required not to use it, clear it */
679 if (m_Header
.dwFlags
& SLDF_FORCE_NO_LINKINFO
)
681 HeapFree(GetProcessHeap(), 0, m_sPath
);
683 ZeroMemory(&volume
, sizeof(volume
));
686 BOOL unicode
= !!(m_Header
.dwFlags
& SLDF_UNICODE
);
688 if (m_Header
.dwFlags
& SLDF_HAS_NAME
)
690 hr
= Stream_LoadString(stm
, unicode
, &m_sDescription
);
693 TRACE("Description -> %s\n", debugstr_w(m_sDescription
));
696 if (m_Header
.dwFlags
& SLDF_HAS_RELPATH
)
698 hr
= Stream_LoadString(stm
, unicode
, &m_sPathRel
);
701 TRACE("Relative Path-> %s\n", debugstr_w(m_sPathRel
));
704 if (m_Header
.dwFlags
& SLDF_HAS_WORKINGDIR
)
706 hr
= Stream_LoadString(stm
, unicode
, &m_sWorkDir
);
709 PathRemoveBackslash(m_sWorkDir
);
710 TRACE("Working Dir -> %s\n", debugstr_w(m_sWorkDir
));
713 if (m_Header
.dwFlags
& SLDF_HAS_ARGS
)
715 hr
= Stream_LoadString(stm
, unicode
, &m_sArgs
);
718 TRACE("Arguments -> %s\n", debugstr_w(m_sArgs
));
721 if (m_Header
.dwFlags
& SLDF_HAS_ICONLOCATION
)
723 hr
= Stream_LoadString(stm
, unicode
, &m_sIcoPath
);
726 TRACE("Icon file -> %s\n", debugstr_w(m_sIcoPath
));
729 /* Now load the optional data block list */
730 hr
= SHReadDataBlockList(stm
, &m_pDBList
);
731 if (FAILED(hr
)) // FIXME: Should we fail?
736 #if (NTDDI_VERSION < NTDDI_LONGHORN)
737 if (m_Header
.dwFlags
& SLDF_HAS_LOGO3ID
)
739 hr
= GetAdvertiseInfo(&sProduct
, EXP_LOGO3_ID_SIG
);
741 TRACE("Product -> %s\n", debugstr_w(sProduct
));
744 if (m_Header
.dwFlags
& SLDF_HAS_DARWINID
)
746 hr
= GetAdvertiseInfo(&sComponent
, EXP_DARWIN_ID_SIG
);
748 TRACE("Component -> %s\n", debugstr_w(sComponent
));
752 if (m_Header
.dwFlags
& SLDF_RUNAS_USER
)
764 /************************************************************************
767 * Helper function for IPersistStream_Save. Writes a unicode string
768 * with terminating nul byte to a stream, preceded by the its length.
770 static HRESULT
Stream_WriteString(IStream
* stm
, LPCWSTR str
)
772 USHORT len
= wcslen(str
) + 1; // FIXME: Possible overflows?
775 HRESULT hr
= stm
->Write(&len
, sizeof(len
), &count
);
779 len
*= sizeof(WCHAR
);
781 hr
= stm
->Write(str
, len
, &count
);
788 /************************************************************************
789 * Stream_WriteLocationInfo
791 * Writes the location info to a stream
793 * FIXME: One day we might want to write the network volume information
794 * and the final path.
795 * Figure out how Windows deals with unicode paths here.
797 static HRESULT
Stream_WriteLocationInfo(IStream
* stm
, LPCWSTR path
,
798 CShellLink::volume_info
*volume
)
800 LOCAL_VOLUME_INFO
*vol
;
803 TRACE("%p %s %p\n", stm
, debugstr_w(path
), volume
);
805 /* figure out the size of everything */
806 DWORD label_size
= WideCharToMultiByte(CP_ACP
, 0, volume
->label
, -1,
807 NULL
, 0, NULL
, NULL
);
808 DWORD path_size
= WideCharToMultiByte(CP_ACP
, 0, path
, -1,
809 NULL
, 0, NULL
, NULL
);
810 DWORD volume_info_size
= sizeof(*vol
) + label_size
;
811 DWORD final_path_size
= 1;
812 DWORD total_size
= sizeof(*loc
) + volume_info_size
+ path_size
+ final_path_size
;
814 /* create pointers to everything */
815 loc
= static_cast<LOCATION_INFO
*>(HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, total_size
));
816 vol
= (LOCAL_VOLUME_INFO
*) &loc
[1];
817 LPSTR szLabel
= (LPSTR
) &vol
[1];
818 LPSTR szPath
= &szLabel
[label_size
];
819 LPSTR szFinalPath
= &szPath
[path_size
];
821 /* fill in the location information header */
822 loc
->dwTotalSize
= total_size
;
823 loc
->dwHeaderSize
= sizeof(*loc
);
825 loc
->dwVolTableOfs
= sizeof(*loc
);
826 loc
->dwLocalPathOfs
= sizeof(*loc
) + volume_info_size
;
827 loc
->dwNetworkVolTableOfs
= 0;
828 loc
->dwFinalPathOfs
= sizeof(*loc
) + volume_info_size
+ path_size
;
830 /* fill in the volume information */
831 vol
->dwSize
= volume_info_size
;
832 vol
->dwType
= volume
->type
;
833 vol
->dwVolSerial
= volume
->serial
;
834 vol
->dwVolLabelOfs
= sizeof(*vol
);
836 /* copy in the strings */
837 WideCharToMultiByte(CP_ACP
, 0, volume
->label
, -1,
838 szLabel
, label_size
, NULL
, NULL
);
839 WideCharToMultiByte(CP_ACP
, 0, path
, -1,
840 szPath
, path_size
, NULL
, NULL
);
844 HRESULT hr
= stm
->Write(loc
, total_size
, &count
);
845 HeapFree(GetProcessHeap(), 0, loc
);
850 /************************************************************************
851 * IPersistStream_Save (IPersistStream)
853 * FIXME: makes assumptions about byte order
855 HRESULT STDMETHODCALLTYPE
CShellLink::Save(IStream
*stm
, BOOL fClearDirty
)
857 TRACE("%p %p %x\n", this, stm
, fClearDirty
);
859 m_Header
.dwSize
= sizeof(m_Header
);
860 m_Header
.clsid
= CLSID_ShellLink
;
863 * Reset the flags: keep only the flags related to data blocks as they were
864 * already set in accordance by the different mutator member functions.
865 * The other flags will be determined now by the presence or absence of data.
867 m_Header
.dwFlags
&= (SLDF_RUN_WITH_SHIMLAYER
| SLDF_RUNAS_USER
|
868 SLDF_RUN_IN_SEPARATE
| SLDF_HAS_DARWINID
|
869 #if (NTDDI_VERSION < NTDDI_LONGHORN)
872 SLDF_HAS_EXP_ICON_SZ
| SLDF_HAS_EXP_SZ
);
873 // TODO: When we will support Vista+ functionality, add other flags to this list.
875 /* The stored strings are in UNICODE */
876 m_Header
.dwFlags
|= SLDF_UNICODE
;
879 m_Header
.dwFlags
|= SLDF_HAS_ID_LIST
;
881 m_Header
.dwFlags
|= SLDF_HAS_LINK_INFO
;
882 if (m_sDescription
&& *m_sDescription
)
883 m_Header
.dwFlags
|= SLDF_HAS_NAME
;
884 if (m_sPathRel
&& *m_sPathRel
)
885 m_Header
.dwFlags
|= SLDF_HAS_RELPATH
;
886 if (m_sWorkDir
&& *m_sWorkDir
)
887 m_Header
.dwFlags
|= SLDF_HAS_WORKINGDIR
;
888 if (m_sArgs
&& *m_sArgs
)
889 m_Header
.dwFlags
|= SLDF_HAS_ARGS
;
890 if (m_sIcoPath
&& *m_sIcoPath
)
891 m_Header
.dwFlags
|= SLDF_HAS_ICONLOCATION
;
893 m_Header
.dwFlags
|= SLDF_RUNAS_USER
;
895 /* Write the shortcut header */
897 HRESULT hr
= stm
->Write(&m_Header
, sizeof(m_Header
), &count
);
900 ERR("Write failed\n");
904 /* Save the data in order */
908 hr
= ILSaveToStream(stm
, m_pPidl
);
911 ERR("Failed to write PIDL\n");
918 hr
= Stream_WriteLocationInfo(stm
, m_sPath
, &volume
);
923 if (m_Header
.dwFlags
& SLDF_HAS_NAME
)
925 hr
= Stream_WriteString(stm
, m_sDescription
);
930 if (m_Header
.dwFlags
& SLDF_HAS_RELPATH
)
932 hr
= Stream_WriteString(stm
, m_sPathRel
);
937 if (m_Header
.dwFlags
& SLDF_HAS_WORKINGDIR
)
939 hr
= Stream_WriteString(stm
, m_sWorkDir
);
944 if (m_Header
.dwFlags
& SLDF_HAS_ARGS
)
946 hr
= Stream_WriteString(stm
, m_sArgs
);
951 if (m_Header
.dwFlags
& SLDF_HAS_ICONLOCATION
)
953 hr
= Stream_WriteString(stm
, m_sIcoPath
);
959 * Now save the data block list.
961 * NOTE that both advertised Product and Component are already saved
962 * inside Logo3 and Darwin data blocks in the m_pDBList list, and the
963 * m_Header.dwFlags is suitably initialized.
965 hr
= SHWriteDataBlockList(stm
, m_pDBList
);
969 /* Clear the dirty bit if requested */
976 /************************************************************************
977 * IPersistStream_GetSizeMax (IPersistStream)
979 HRESULT STDMETHODCALLTYPE
CShellLink::GetSizeMax(ULARGE_INTEGER
*pcbSize
)
981 TRACE("(%p)\n", this);
985 static BOOL
SHELL_ExistsFileW(LPCWSTR path
)
987 if (INVALID_FILE_ATTRIBUTES
== GetFileAttributesW(path
))
993 /**************************************************************************
994 * ShellLink_UpdatePath
995 * update absolute path in sPath using relative path in sPathRel
997 static HRESULT
ShellLink_UpdatePath(LPCWSTR sPathRel
, LPCWSTR path
, LPCWSTR sWorkDir
, LPWSTR
* psPath
)
999 if (!path
|| !psPath
)
1000 return E_INVALIDARG
;
1002 if (!*psPath
&& sPathRel
)
1004 WCHAR buffer
[2*MAX_PATH
], abs_path
[2*MAX_PATH
];
1005 LPWSTR final
= NULL
;
1007 /* first try if [directory of link file] + [relative path] finds an existing file */
1009 GetFullPathNameW(path
, MAX_PATH
* 2, buffer
, &final
);
1012 wcscpy(final
, sPathRel
);
1016 if (SHELL_ExistsFileW(buffer
))
1018 if (!GetFullPathNameW(buffer
, MAX_PATH
, abs_path
, &final
))
1019 wcscpy(abs_path
, buffer
);
1023 /* try if [working directory] + [relative path] finds an existing file */
1026 wcscpy(buffer
, sWorkDir
);
1027 wcscpy(PathAddBackslashW(buffer
), sPathRel
);
1029 if (SHELL_ExistsFileW(buffer
))
1030 if (!GetFullPathNameW(buffer
, MAX_PATH
, abs_path
, &final
))
1031 wcscpy(abs_path
, buffer
);
1035 /* FIXME: This is even not enough - not all shell links can be resolved using this algorithm. */
1037 wcscpy(abs_path
, sPathRel
);
1039 *psPath
= strdupW(abs_path
);
1041 return E_OUTOFMEMORY
;
1047 HRESULT STDMETHODCALLTYPE
CShellLink::GetPath(LPSTR pszFile
, INT cchMaxPath
, WIN32_FIND_DATAA
*pfd
, DWORD fFlags
)
1051 WIN32_FIND_DATAW wfd
;
1053 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%u)(%s)\n",
1054 this, pszFile
, cchMaxPath
, pfd
, fFlags
, debugstr_w(m_sPath
));
1056 /* Allocate a temporary UNICODE buffer */
1057 pszFileW
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, cchMaxPath
* sizeof(WCHAR
));
1059 return E_OUTOFMEMORY
;
1061 /* Call the UNICODE function */
1062 hr
= GetPath(pszFileW
, cchMaxPath
, &wfd
, fFlags
);
1064 /* Convert the file path back to ANSI */
1065 WideCharToMultiByte(CP_ACP
, 0, pszFileW
, -1,
1066 pszFile
, cchMaxPath
, NULL
, NULL
);
1068 /* Free the temporary buffer */
1069 HeapFree(GetProcessHeap(), 0, pszFileW
);
1073 ZeroMemory(pfd
, sizeof(*pfd
));
1075 /* Copy the file data if a file path was returned */
1080 /* Copy the fixed part */
1081 CopyMemory(pfd
, &wfd
, FIELD_OFFSET(WIN32_FIND_DATAA
, cFileName
));
1083 /* Convert the file names to ANSI */
1084 len
= lstrlenW(wfd
.cFileName
);
1085 WideCharToMultiByte(CP_ACP
, 0, wfd
.cFileName
, len
+ 1,
1086 pfd
->cFileName
, sizeof(pfd
->cFileName
), NULL
, NULL
);
1087 len
= lstrlenW(wfd
.cAlternateFileName
);
1088 WideCharToMultiByte(CP_ACP
, 0, wfd
.cAlternateFileName
, len
+ 1,
1089 pfd
->cAlternateFileName
, sizeof(pfd
->cAlternateFileName
), NULL
, NULL
);
1096 HRESULT STDMETHODCALLTYPE
CShellLink::GetIDList(LPITEMIDLIST
*ppidl
)
1098 TRACE("(%p)->(ppidl=%p)\n", this, ppidl
);
1106 *ppidl
= ILClone(m_pPidl
);
1110 HRESULT STDMETHODCALLTYPE
CShellLink::SetIDList(LPCITEMIDLIST pidl
)
1112 TRACE("(%p)->(pidl=%p)\n", this, pidl
);
1113 return SetTargetFromPIDLOrPath(pidl
, NULL
);
1116 HRESULT STDMETHODCALLTYPE
CShellLink::GetDescription(LPSTR pszName
, INT cchMaxName
)
1118 TRACE("(%p)->(%p len=%u)\n", this, pszName
, cchMaxName
);
1124 WideCharToMultiByte(CP_ACP
, 0, m_sDescription
, -1,
1125 pszName
, cchMaxName
, NULL
, NULL
);
1130 HRESULT STDMETHODCALLTYPE
CShellLink::SetDescription(LPCSTR pszName
)
1132 TRACE("(%p)->(pName=%s)\n", this, pszName
);
1134 HeapFree(GetProcessHeap(), 0, m_sDescription
);
1135 m_sDescription
= NULL
;
1139 m_sDescription
= HEAP_strdupAtoW(GetProcessHeap(), 0, pszName
);
1140 if (!m_sDescription
)
1141 return E_OUTOFMEMORY
;
1148 HRESULT STDMETHODCALLTYPE
CShellLink::GetWorkingDirectory(LPSTR pszDir
, INT cchMaxPath
)
1150 TRACE("(%p)->(%p len=%u)\n", this, pszDir
, cchMaxPath
);
1156 WideCharToMultiByte(CP_ACP
, 0, m_sWorkDir
, -1,
1157 pszDir
, cchMaxPath
, NULL
, NULL
);
1162 HRESULT STDMETHODCALLTYPE
CShellLink::SetWorkingDirectory(LPCSTR pszDir
)
1164 TRACE("(%p)->(dir=%s)\n", this, pszDir
);
1166 HeapFree(GetProcessHeap(), 0, m_sWorkDir
);
1171 m_sWorkDir
= HEAP_strdupAtoW(GetProcessHeap(), 0, pszDir
);
1173 return E_OUTOFMEMORY
;
1180 HRESULT STDMETHODCALLTYPE
CShellLink::GetArguments(LPSTR pszArgs
, INT cchMaxPath
)
1182 TRACE("(%p)->(%p len=%u)\n", this, pszArgs
, cchMaxPath
);
1188 WideCharToMultiByte(CP_ACP
, 0, m_sArgs
, -1,
1189 pszArgs
, cchMaxPath
, NULL
, NULL
);
1194 HRESULT STDMETHODCALLTYPE
CShellLink::SetArguments(LPCSTR pszArgs
)
1196 TRACE("(%p)->(args=%s)\n", this, pszArgs
);
1198 HeapFree(GetProcessHeap(), 0, m_sArgs
);
1203 m_sArgs
= HEAP_strdupAtoW(GetProcessHeap(), 0, pszArgs
);
1205 return E_OUTOFMEMORY
;
1213 HRESULT STDMETHODCALLTYPE
CShellLink::GetHotkey(WORD
*pwHotkey
)
1215 TRACE("(%p)->(%p)(0x%08x)\n", this, pwHotkey
, m_Header
.wHotKey
);
1216 *pwHotkey
= m_Header
.wHotKey
;
1220 HRESULT STDMETHODCALLTYPE
CShellLink::SetHotkey(WORD wHotkey
)
1222 TRACE("(%p)->(hotkey=%x)\n", this, wHotkey
);
1224 m_Header
.wHotKey
= wHotkey
;
1230 HRESULT STDMETHODCALLTYPE
CShellLink::GetShowCmd(INT
*piShowCmd
)
1232 TRACE("(%p)->(%p) %d\n", this, piShowCmd
, m_Header
.nShowCommand
);
1233 *piShowCmd
= m_Header
.nShowCommand
;
1237 HRESULT STDMETHODCALLTYPE
CShellLink::SetShowCmd(INT iShowCmd
)
1239 TRACE("(%p) %d\n", this, iShowCmd
);
1241 m_Header
.nShowCommand
= iShowCmd
;
1247 HRESULT STDMETHODCALLTYPE
CShellLink::GetIconLocation(LPSTR pszIconPath
, INT cchIconPath
, INT
*piIcon
)
1250 LPWSTR pszIconPathW
;
1252 TRACE("(%p)->(%p len=%u iicon=%p)\n", this, pszIconPath
, cchIconPath
, piIcon
);
1254 /* Allocate a temporary UNICODE buffer */
1255 pszIconPathW
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, cchIconPath
* sizeof(WCHAR
));
1257 return E_OUTOFMEMORY
;
1259 /* Call the UNICODE function */
1260 hr
= GetIconLocation(pszIconPathW
, cchIconPath
, piIcon
);
1262 /* Convert the file path back to ANSI */
1263 WideCharToMultiByte(CP_ACP
, 0, pszIconPathW
, -1,
1264 pszIconPath
, cchIconPath
, NULL
, NULL
);
1266 /* Free the temporary buffer */
1267 HeapFree(GetProcessHeap(), 0, pszIconPathW
);
1272 HRESULT STDMETHODCALLTYPE
CShellLink::GetIconLocation(UINT uFlags
, PSTR pszIconFile
, UINT cchMax
, int *piIndex
, UINT
*pwFlags
)
1275 LPWSTR pszIconFileW
;
1277 TRACE("(%p)->(%u %p len=%u piIndex=%p pwFlags=%p)\n", this, uFlags
, pszIconFile
, cchMax
, piIndex
, pwFlags
);
1279 /* Allocate a temporary UNICODE buffer */
1280 pszIconFileW
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, cchMax
* sizeof(WCHAR
));
1282 return E_OUTOFMEMORY
;
1284 /* Call the UNICODE function */
1285 hr
= GetIconLocation(uFlags
, pszIconFileW
, cchMax
, piIndex
, pwFlags
);
1287 /* Convert the file path back to ANSI */
1288 WideCharToMultiByte(CP_ACP
, 0, pszIconFileW
, -1,
1289 pszIconFile
, cchMax
, NULL
, NULL
);
1291 /* Free the temporary buffer */
1292 HeapFree(GetProcessHeap(), 0, pszIconFileW
);
1297 HRESULT STDMETHODCALLTYPE
CShellLink::Extract(PCSTR pszFile
, UINT nIconIndex
, HICON
*phiconLarge
, HICON
*phiconSmall
, UINT nIconSize
)
1299 TRACE("(%p)->(path=%s iicon=%u)\n", this, pszFile
, nIconIndex
);
1304 str
= HEAP_strdupAtoW(GetProcessHeap(), 0, pszFile
);
1306 return E_OUTOFMEMORY
;
1309 HRESULT hr
= Extract(str
, nIconIndex
, phiconLarge
, phiconSmall
, nIconSize
);
1312 HeapFree(GetProcessHeap(), 0, str
);
1317 HRESULT STDMETHODCALLTYPE
CShellLink::SetIconLocation(LPCSTR pszIconPath
, INT iIcon
)
1319 TRACE("(%p)->(path=%s iicon=%u)\n", this, pszIconPath
, iIcon
);
1324 str
= HEAP_strdupAtoW(GetProcessHeap(), 0, pszIconPath
);
1326 return E_OUTOFMEMORY
;
1329 HRESULT hr
= SetIconLocation(str
, iIcon
);
1332 HeapFree(GetProcessHeap(), 0, str
);
1337 HRESULT STDMETHODCALLTYPE
CShellLink::SetRelativePath(LPCSTR pszPathRel
, DWORD dwReserved
)
1339 TRACE("(%p)->(path=%s %x)\n", this, pszPathRel
, dwReserved
);
1341 HeapFree(GetProcessHeap(), 0, m_sPathRel
);
1346 m_sPathRel
= HEAP_strdupAtoW(GetProcessHeap(), 0, pszPathRel
);
1350 return ShellLink_UpdatePath(m_sPathRel
, m_sPath
, m_sWorkDir
, &m_sPath
);
1354 shelllink_get_msi_component_path(LPWSTR component
)
1356 DWORD Result
, sz
= 0;
1358 Result
= CommandLineFromMsiDescriptor(component
, NULL
, &sz
);
1359 if (Result
!= ERROR_SUCCESS
)
1363 LPWSTR path
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, sz
* sizeof(WCHAR
));
1364 Result
= CommandLineFromMsiDescriptor(component
, path
, &sz
);
1365 if (Result
!= ERROR_SUCCESS
)
1367 HeapFree(GetProcessHeap(), 0, path
);
1371 TRACE("returning %s\n", debugstr_w(path
));
1376 HRESULT STDMETHODCALLTYPE
CShellLink::Resolve(HWND hwnd
, DWORD fFlags
)
1381 TRACE("(%p)->(hwnd=%p flags=%x)\n", this, hwnd
, fFlags
);
1383 /* FIXME: use IResolveShellLink interface? */
1385 // FIXME: See InvokeCommand().
1387 #if (NTDDI_VERSION < NTDDI_LONGHORN)
1388 // NOTE: For Logo3 (EXP_LOGO3_ID_SIG), check also for SHRestricted(REST_NOLOGO3CHANNELNOTIFY)
1389 if (m_Header
.dwFlags
& SLDF_HAS_LOGO3ID
)
1391 FIXME("Logo3 links are not supported yet!\n");
1396 /* Resolve Darwin (MSI) target */
1397 if (m_Header
.dwFlags
& SLDF_HAS_DARWINID
)
1399 LPWSTR component
= NULL
;
1400 hr
= GetAdvertiseInfo(&component
, EXP_DARWIN_ID_SIG
);
1404 /* Clear the cached path */
1405 HeapFree(GetProcessHeap(), 0, m_sPath
);
1407 m_sPath
= shelllink_get_msi_component_path(component
);
1412 if (!m_sPath
&& m_pPidl
)
1414 WCHAR buffer
[MAX_PATH
];
1416 bSuccess
= SHGetPathFromIDListW(m_pPidl
, buffer
);
1417 if (bSuccess
&& *buffer
)
1419 if (buffer
!= m_sPath
)
1421 m_sPath
= strdupW(buffer
);
1423 return E_OUTOFMEMORY
;
1430 hr
= S_OK
; /* don't report an error occurred while just caching information */
1434 // FIXME: Strange to do that here...
1435 if (!m_sIcoPath
&& m_sPath
)
1437 if (m_sIcoPath
!= m_sPath
)
1439 m_sIcoPath
= strdupW(m_sPath
);
1441 return E_OUTOFMEMORY
;
1444 m_Header
.nIconIndex
= 0;
1452 HRESULT STDMETHODCALLTYPE
CShellLink::SetPath(LPCSTR pszFile
)
1454 TRACE("(%p)->(path=%s)\n", this, pszFile
);
1457 return E_INVALIDARG
;
1459 LPWSTR str
= HEAP_strdupAtoW(GetProcessHeap(), 0, pszFile
);
1461 return E_OUTOFMEMORY
;
1463 HRESULT hr
= SetPath(str
);
1464 HeapFree(GetProcessHeap(), 0, str
);
1469 HRESULT STDMETHODCALLTYPE
CShellLink::GetPath(LPWSTR pszFile
, INT cchMaxPath
, WIN32_FIND_DATAW
*pfd
, DWORD fFlags
)
1471 WCHAR buffer
[MAX_PATH
];
1473 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%u)(%s)\n",
1474 this, pszFile
, cchMaxPath
, pfd
, fFlags
, debugstr_w(m_sPath
));
1478 // FIXME: What if cchMaxPath == 0 , or pszFile == NULL ??
1480 // FIXME: What about Darwin??
1483 * Retrieve the path to the target from the PIDL (if we have one).
1484 * NOTE: Do NOT use the cached path (m_sPath from link info).
1486 if (m_pPidl
&& SHGetPathFromIDListW(m_pPidl
, buffer
))
1488 if (fFlags
& SLGP_SHORTPATH
)
1489 GetShortPathNameW(buffer
, buffer
, _countof(buffer
));
1490 // FIXME: Add support for SLGP_UNCPRIORITY
1497 /* If we have a FindData structure, initialize it */
1500 ZeroMemory(pfd
, sizeof(*pfd
));
1502 /* Copy the file data if the target is a file path */
1505 pfd
->dwFileAttributes
= m_Header
.dwFileAttributes
;
1506 pfd
->ftCreationTime
= m_Header
.ftCreationTime
;
1507 pfd
->ftLastAccessTime
= m_Header
.ftLastAccessTime
;
1508 pfd
->ftLastWriteTime
= m_Header
.ftLastWriteTime
;
1509 pfd
->nFileSizeHigh
= 0;
1510 pfd
->nFileSizeLow
= m_Header
.nFileSizeLow
;
1513 * Build temporarily a short path in pfd->cFileName (of size MAX_PATH),
1514 * then extract and store the short file name in pfd->cAlternateFileName.
1516 GetShortPathNameW(buffer
, pfd
->cFileName
, _countof(pfd
->cFileName
));
1517 lstrcpynW(pfd
->cAlternateFileName
,
1518 PathFindFileNameW(pfd
->cFileName
),
1519 _countof(pfd
->cAlternateFileName
));
1521 /* Now extract and store the long file name in pfd->cFileName */
1522 lstrcpynW(pfd
->cFileName
,
1523 PathFindFileNameW(buffer
),
1524 _countof(pfd
->cFileName
));
1528 /* Finally check if we have a raw path the user actually wants to retrieve */
1529 if ((fFlags
& SLGP_RAWPATH
) && (m_Header
.dwFlags
& SLDF_HAS_EXP_SZ
))
1531 /* Search for a target environment block */
1532 LPEXP_SZ_LINK pInfo
;
1533 pInfo
= (LPEXP_SZ_LINK
)SHFindDataBlock(m_pDBList
, EXP_SZ_LINK_SIG
);
1534 if (pInfo
&& (pInfo
->cbSize
== sizeof(*pInfo
)))
1535 lstrcpynW(buffer
, pInfo
->szwTarget
, cchMaxPath
);
1538 /* For diagnostics purposes only... */
1539 // NOTE: SLGP_UNCPRIORITY is unsupported
1540 fFlags
&= ~(SLGP_RAWPATH
| SLGP_SHORTPATH
);
1541 if (fFlags
) FIXME("(%p): Unsupported flags %lu\n", this, fFlags
);
1543 /* Copy the data back to the user */
1545 lstrcpynW(pszFile
, buffer
, cchMaxPath
);
1547 return (*buffer
? S_OK
: S_FALSE
);
1550 HRESULT STDMETHODCALLTYPE
CShellLink::GetDescription(LPWSTR pszName
, INT cchMaxName
)
1552 TRACE("(%p)->(%p len=%u)\n", this, pszName
, cchMaxName
);
1556 lstrcpynW(pszName
, m_sDescription
, cchMaxName
);
1561 HRESULT STDMETHODCALLTYPE
CShellLink::SetDescription(LPCWSTR pszName
)
1563 TRACE("(%p)->(desc=%s)\n", this, debugstr_w(pszName
));
1565 HeapFree(GetProcessHeap(), 0, m_sDescription
);
1568 if (m_sDescription
!= pszName
)
1570 m_sDescription
= strdupW(pszName
);
1571 if (!m_sDescription
)
1572 return E_OUTOFMEMORY
;
1576 m_sDescription
= NULL
;
1583 HRESULT STDMETHODCALLTYPE
CShellLink::GetWorkingDirectory(LPWSTR pszDir
, INT cchMaxPath
)
1585 TRACE("(%p)->(%p len %u)\n", this, pszDir
, cchMaxPath
);
1591 lstrcpynW(pszDir
, m_sWorkDir
, cchMaxPath
);
1596 HRESULT STDMETHODCALLTYPE
CShellLink::SetWorkingDirectory(LPCWSTR pszDir
)
1598 TRACE("(%p)->(dir=%s)\n", this, debugstr_w(pszDir
));
1600 HeapFree(GetProcessHeap(), 0, m_sWorkDir
);
1603 if (m_sWorkDir
!= pszDir
)
1605 m_sWorkDir
= strdupW(pszDir
);
1607 return E_OUTOFMEMORY
;
1618 HRESULT STDMETHODCALLTYPE
CShellLink::GetArguments(LPWSTR pszArgs
, INT cchMaxPath
)
1620 TRACE("(%p)->(%p len=%u)\n", this, pszArgs
, cchMaxPath
);
1626 lstrcpynW(pszArgs
, m_sArgs
, cchMaxPath
);
1631 HRESULT STDMETHODCALLTYPE
CShellLink::SetArguments(LPCWSTR pszArgs
)
1633 TRACE("(%p)->(args=%s)\n", this, debugstr_w(pszArgs
));
1635 HeapFree(GetProcessHeap(), 0, m_sArgs
);
1638 if (m_sArgs
!= pszArgs
)
1640 m_sArgs
= strdupW(pszArgs
);
1642 return E_OUTOFMEMORY
;
1653 HRESULT STDMETHODCALLTYPE
CShellLink::GetIconLocation(LPWSTR pszIconPath
, INT cchIconPath
, INT
*piIcon
)
1655 TRACE("(%p)->(%p len=%u iicon=%p)\n", this, pszIconPath
, cchIconPath
, piIcon
);
1662 /* Update the original icon path location */
1663 if (m_Header
.dwFlags
& SLDF_HAS_EXP_ICON_SZ
)
1665 WCHAR szPath
[MAX_PATH
];
1667 /* Search for an icon environment block */
1668 LPEXP_SZ_LINK pInfo
;
1669 pInfo
= (LPEXP_SZ_LINK
)SHFindDataBlock(m_pDBList
, EXP_SZ_ICON_SIG
);
1670 if (pInfo
&& (pInfo
->cbSize
== sizeof(*pInfo
)))
1672 m_Header
.dwFlags
&= ~SLDF_HAS_ICONLOCATION
;
1673 HeapFree(GetProcessHeap(), 0, m_sIcoPath
);
1676 SHExpandEnvironmentStringsW(pInfo
->szwTarget
, szPath
, _countof(szPath
));
1678 if (m_sIcoPath
!= szPath
)
1680 m_sIcoPath
= strdupW(szPath
);
1682 return E_OUTOFMEMORY
;
1685 m_Header
.dwFlags
|= SLDF_HAS_ICONLOCATION
;
1691 *piIcon
= m_Header
.nIconIndex
;
1694 lstrcpynW(pszIconPath
, m_sIcoPath
, cchIconPath
);
1699 static HRESULT
SHELL_PidlGetIconLocationW(PCIDLIST_ABSOLUTE pidl
,
1700 UINT uFlags
, PWSTR pszIconFile
, UINT cchMax
, int *piIndex
, UINT
*pwFlags
)
1702 LPCITEMIDLIST pidlLast
;
1703 CComPtr
<IShellFolder
> psf
;
1705 HRESULT hr
= SHBindToParent(pidl
, IID_PPV_ARG(IShellFolder
, &psf
), &pidlLast
);
1706 if (FAILED_UNEXPECTEDLY(hr
))
1709 CComPtr
<IExtractIconW
> pei
;
1710 hr
= psf
->GetUIObjectOf(0, 1, &pidlLast
, IID_NULL_PPV_ARG(IExtractIconW
, &pei
));
1711 if (FAILED_UNEXPECTEDLY(hr
))
1714 hr
= pei
->GetIconLocation(uFlags
, pszIconFile
, cchMax
, piIndex
, pwFlags
);
1715 if (FAILED_UNEXPECTEDLY(hr
))
1721 HRESULT STDMETHODCALLTYPE
CShellLink::GetIconLocation(UINT uFlags
, PWSTR pszIconFile
, UINT cchMax
, int *piIndex
, UINT
*pwFlags
)
1725 pszIconFile
[0] = UNICODE_NULL
;
1728 * It is possible for a shell link to point to another shell link,
1729 * and in particular there is the possibility to point to itself.
1730 * Now, suppose we ask such a link to retrieve its associated icon.
1731 * This function would be called, and due to COM would be called again
1732 * recursively. To solve this issue, we forbid calling GetIconLocation()
1733 * with GIL_FORSHORTCUT set in uFlags, as done by Windows (shown by tests).
1735 if (uFlags
& GIL_FORSHORTCUT
)
1736 return E_INVALIDARG
;
1739 * Now, we set GIL_FORSHORTCUT so that: i) we allow the icon extractor
1740 * of the target to give us a suited icon, and ii) we protect ourselves
1741 * against recursive call.
1743 uFlags
|= GIL_FORSHORTCUT
;
1745 if (uFlags
& GIL_DEFAULTICON
)
1748 hr
= GetIconLocation(pszIconFile
, cchMax
, piIndex
);
1749 if (FAILED(hr
) || pszIconFile
[0] == UNICODE_NULL
)
1751 hr
= SHELL_PidlGetIconLocationW(m_pPidl
, uFlags
, pszIconFile
, cchMax
, piIndex
, pwFlags
);
1755 *pwFlags
= GIL_NOTFILENAME
| GIL_PERCLASS
;
1761 HRESULT STDMETHODCALLTYPE
1762 CShellLink::Extract(PCWSTR pszFile
, UINT nIconIndex
, HICON
*phiconLarge
, HICON
*phiconSmall
, UINT nIconSize
)
1764 HRESULT hr
= NOERROR
;
1765 UINT cxyLarge
= LOWORD(nIconSize
), cxySmall
= HIWORD(nIconSize
);
1769 *phiconLarge
= NULL
;
1770 PrivateExtractIconsW(pszFile
, nIconIndex
, cxyLarge
, cxyLarge
, phiconLarge
, NULL
, 1, 0);
1772 if (*phiconLarge
== NULL
)
1778 *phiconSmall
= NULL
;
1779 PrivateExtractIconsW(pszFile
, nIconIndex
, cxySmall
, cxySmall
, phiconSmall
, NULL
, 1, 0);
1781 if (*phiconSmall
== NULL
)
1787 if (phiconLarge
&& *phiconLarge
)
1789 DestroyIcon(*phiconLarge
);
1790 *phiconLarge
= NULL
;
1792 if (phiconSmall
&& *phiconSmall
)
1794 DestroyIcon(*phiconSmall
);
1795 *phiconSmall
= NULL
;
1803 /* Extends the functionality of PathUnExpandEnvStringsW */
1804 BOOL
PathFullyUnExpandEnvStringsW(
1805 _In_ LPCWSTR pszPath
,
1806 _Out_ LPWSTR pszBuf
,
1809 BOOL Ret
= FALSE
; // Set to TRUE as soon as PathUnExpandEnvStrings starts unexpanding.
1814 while (*pszPath
&& cchBuf
> 0)
1816 /* Attempt unexpanding the path */
1817 res
= PathUnExpandEnvStringsW(pszPath
, pszBuf
, cchBuf
);
1820 /* The unexpansion failed. Try to find a path delimiter. */
1821 p
= wcspbrk(pszPath
, L
" /\\:*?\"<>|%");
1822 if (!p
) /* None found, we will copy the remaining path */
1823 p
= pszPath
+ wcslen(pszPath
);
1824 else /* Found one, we will copy the delimiter and skip it */
1826 /* If we overflow, we cannot unexpand more, so return FALSE */
1827 if (p
- pszPath
>= cchBuf
)
1828 return FALSE
; // *pszBuf = L'\0';
1830 /* Copy the untouched portion of path up to the delimiter, included */
1831 wcsncpy(pszBuf
, pszPath
, p
- pszPath
);
1832 pszBuf
[p
- pszPath
] = L
'\0'; // NULL-terminate
1834 /* Advance the pointers and decrease the remaining buffer size */
1835 cchBuf
-= (p
- pszPath
);
1836 pszBuf
+= (p
- pszPath
);
1837 pszPath
+= (p
- pszPath
);
1842 * The unexpansion succeeded. Skip the unexpanded part by trying
1843 * to find where the original path and the unexpanded string
1845 * NOTE: An alternative(?) would be to stop also at the last
1846 * path delimiter encountered in the loop (i.e. would be the
1847 * first path delimiter in the strings).
1852 * The algorithm starts at the end of the strings and loops back
1853 * while the characters are equal, until it finds a discrepancy.
1855 p
= pszPath
+ wcslen(pszPath
);
1856 q
= pszBuf
+ wcslen(pszBuf
); // This wcslen should be < cchBuf
1857 while ((*p
== *q
) && (p
> pszPath
) && (q
> pszBuf
))
1861 /* Skip discrepancy */
1864 /* Advance the pointers and decrease the remaining buffer size */
1865 cchBuf
-= (q
- pszBuf
);
1877 HRESULT STDMETHODCALLTYPE
CShellLink::SetIconLocation(LPCWSTR pszIconPath
, INT iIcon
)
1879 HRESULT hr
= E_FAIL
;
1880 WCHAR szUnExpIconPath
[MAX_PATH
];
1882 TRACE("(%p)->(path=%s iicon=%u)\n", this, debugstr_w(pszIconPath
), iIcon
);
1886 /* Try to fully unexpand the icon path */
1889 * Check whether the user-given file path contains unexpanded
1890 * environment variables. If so, create a target environment block.
1891 * Note that in this block we will store the user-given path.
1892 * It will contain the unexpanded environment variables, but
1893 * it can also contain already expanded path that the user does
1894 * not want to see them unexpanded (e.g. so that they always
1895 * refer to the same place even if the would-be corresponding
1896 * environment variable could change).
1898 // FIXME: http://stackoverflow.com/questions/2976489/ishelllinkseticonlocation-translates-my-icon-path-into-program-files-which-i
1899 // if (PathFullyUnExpandEnvStringsW(pszIconPath, szUnExpIconPath, _countof(szUnExpIconPath)))
1900 PathUnExpandEnvStringsW(pszIconPath
, szUnExpIconPath
, _countof(szUnExpIconPath
));
1901 if (wcscmp(pszIconPath
, szUnExpIconPath
) != 0)
1903 /* Unexpansion succeeded, so we need an icon environment block */
1905 LPEXP_SZ_LINK pInfo
;
1907 pszIconPath
= szUnExpIconPath
;
1908 pInfo
= (LPEXP_SZ_LINK
)SHFindDataBlock(m_pDBList
, EXP_SZ_ICON_SIG
);
1911 /* Make sure that the size of the structure is valid */
1912 if (pInfo
->cbSize
!= sizeof(*pInfo
))
1914 ERR("Ooops. This structure is not as expected...\n");
1916 /* Invalid structure, remove it altogether */
1917 m_Header
.dwFlags
&= ~SLDF_HAS_EXP_ICON_SZ
;
1918 RemoveDataBlock(EXP_SZ_ICON_SIG
);
1920 /* Reset the pointer and go use the static buffer */
1926 /* Use the static buffer */
1928 buffer
.cbSize
= sizeof(buffer
);
1929 buffer
.dwSignature
= EXP_SZ_ICON_SIG
;
1932 lstrcpynW(pInfo
->szwTarget
, szUnExpIconPath
, _countof(pInfo
->szwTarget
));
1933 WideCharToMultiByte(CP_ACP
, 0, szUnExpIconPath
, -1,
1934 pInfo
->szTarget
, _countof(pInfo
->szTarget
), NULL
, NULL
);
1937 if (pInfo
== &buffer
)
1938 hr
= AddDataBlock(pInfo
);
1940 m_Header
.dwFlags
|= SLDF_HAS_EXP_ICON_SZ
;
1944 /* Unexpansion failed, so we need to remove any icon environment block */
1945 m_Header
.dwFlags
&= ~SLDF_HAS_EXP_ICON_SZ
;
1946 RemoveDataBlock(EXP_SZ_ICON_SIG
);
1950 /* Store the original icon path location (this one may contain unexpanded environment strings) */
1953 m_Header
.dwFlags
&= ~SLDF_HAS_ICONLOCATION
;
1954 HeapFree(GetProcessHeap(), 0, m_sIcoPath
);
1957 if (m_sIcoPath
!= pszIconPath
)
1959 m_sIcoPath
= strdupW(pszIconPath
);
1961 return E_OUTOFMEMORY
;
1964 m_Header
.dwFlags
|= SLDF_HAS_ICONLOCATION
;
1969 m_Header
.nIconIndex
= iIcon
;
1975 HRESULT STDMETHODCALLTYPE
CShellLink::SetRelativePath(LPCWSTR pszPathRel
, DWORD dwReserved
)
1977 TRACE("(%p)->(path=%s %x)\n", this, debugstr_w(pszPathRel
), dwReserved
);
1979 HeapFree(GetProcessHeap(), 0, m_sPathRel
);
1982 if (m_sPathRel
!= pszPathRel
)
1984 m_sPathRel
= strdupW(pszPathRel
);
1986 return E_OUTOFMEMORY
;
1994 return ShellLink_UpdatePath(m_sPathRel
, m_sPath
, m_sWorkDir
, &m_sPath
);
1997 static LPWSTR
GetAdvertisedArg(LPCWSTR str
)
2002 LPCWSTR p
= wcschr(str
, L
':');
2006 DWORD len
= p
- str
;
2007 LPWSTR ret
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR
) * (len
+ 1));
2011 memcpy(ret
, str
, sizeof(WCHAR
)*len
);
2016 HRESULT
CShellLink::WriteAdvertiseInfo(LPCWSTR string
, DWORD dwSig
)
2018 EXP_DARWIN_LINK buffer
;
2019 LPEXP_DARWIN_LINK pInfo
;
2021 if ( (dwSig
!= EXP_DARWIN_ID_SIG
)
2022 #if (NTDDI_VERSION < NTDDI_LONGHORN)
2023 && (dwSig
!= EXP_LOGO3_ID_SIG
)
2027 return E_INVALIDARG
;
2033 pInfo
= (LPEXP_DARWIN_LINK
)SHFindDataBlock(m_pDBList
, dwSig
);
2036 /* Make sure that the size of the structure is valid */
2037 if (pInfo
->dbh
.cbSize
!= sizeof(*pInfo
))
2039 ERR("Ooops. This structure is not as expected...\n");
2041 /* Invalid structure, remove it altogether */
2042 if (dwSig
== EXP_DARWIN_ID_SIG
)
2043 m_Header
.dwFlags
&= ~SLDF_HAS_DARWINID
;
2044 #if (NTDDI_VERSION < NTDDI_LONGHORN)
2045 else if (dwSig
== EXP_LOGO3_ID_SIG
)
2046 m_Header
.dwFlags
&= ~SLDF_HAS_LOGO3ID
;
2048 RemoveDataBlock(dwSig
);
2050 /* Reset the pointer and go use the static buffer */
2056 /* Use the static buffer */
2058 buffer
.dbh
.cbSize
= sizeof(buffer
);
2059 buffer
.dbh
.dwSignature
= dwSig
;
2062 lstrcpynW(pInfo
->szwDarwinID
, string
, _countof(pInfo
->szwDarwinID
));
2063 WideCharToMultiByte(CP_ACP
, 0, string
, -1,
2064 pInfo
->szDarwinID
, _countof(pInfo
->szDarwinID
), NULL
, NULL
);
2067 if (pInfo
== &buffer
)
2068 hr
= AddDataBlock(pInfo
);
2071 if (dwSig
== EXP_DARWIN_ID_SIG
)
2072 m_Header
.dwFlags
|= SLDF_HAS_DARWINID
;
2073 #if (NTDDI_VERSION < NTDDI_LONGHORN)
2074 else if (dwSig
== EXP_LOGO3_ID_SIG
)
2075 m_Header
.dwFlags
|= SLDF_HAS_LOGO3ID
;
2082 HRESULT
CShellLink::SetAdvertiseInfo(LPCWSTR str
)
2085 LPCWSTR szComponent
= NULL
, szProduct
= NULL
, p
;
2090 /**/sProduct
= sComponent
= NULL
;/**/
2094 /* each segment must start with two colons */
2095 if (str
[0] != ':' || str
[1] != ':')
2098 /* the last segment is just two colons */
2103 /* there must be a colon straight after a guid */
2104 p
= wcschr(str
, L
':');
2111 /* get the guid, and check if it's validly formatted */
2112 memcpy(szGuid
, str
, sizeof(WCHAR
)*len
);
2115 hr
= CLSIDFromString(szGuid
, &guid
);
2120 /* match it up to a guid that we care about */
2121 if (IsEqualGUID(guid
, SHELL32_AdvtShortcutComponent
) && !szComponent
)
2122 szComponent
= str
; /* Darwin */
2123 else if (IsEqualGUID(guid
, SHELL32_AdvtShortcutProduct
) && !szProduct
)
2124 szProduct
= str
; /* Logo3 */
2128 /* skip to the next field */
2129 str
= wcschr(str
, L
':');
2134 /* we have to have a component for an advertised shortcut */
2138 szComponent
= GetAdvertisedArg(szComponent
);
2139 szProduct
= GetAdvertisedArg(szProduct
);
2141 hr
= WriteAdvertiseInfo(szComponent
, EXP_DARWIN_ID_SIG
);
2144 #if (NTDDI_VERSION < NTDDI_LONGHORN)
2145 hr
= WriteAdvertiseInfo(szProduct
, EXP_LOGO3_ID_SIG
);
2150 HeapFree(GetProcessHeap(), 0, (PVOID
)szComponent
);
2151 HeapFree(GetProcessHeap(), 0, (PVOID
)szProduct
);
2153 if (TRACE_ON(shell
))
2155 GetAdvertiseInfo(&sComponent
, EXP_DARWIN_ID_SIG
);
2156 TRACE("Component = %s\n", debugstr_w(sComponent
));
2157 #if (NTDDI_VERSION < NTDDI_LONGHORN)
2158 GetAdvertiseInfo(&sProduct
, EXP_LOGO3_ID_SIG
);
2159 TRACE("Product = %s\n", debugstr_w(sProduct
));
2167 * Since the real PathResolve (from Wine) is unimplemented at the moment,
2168 * we use this local implementation, until a better one is written (using
2169 * code parts of the SHELL_xxx helpers in Wine's shellpath.c).
2171 static BOOL
HACKISH_PathResolve(
2172 IN OUT PWSTR pszPath
,
2173 IN PZPCWSTR dirs OPTIONAL
,
2176 // FIXME: This is unimplemented!!!
2178 return PathResolve(pszPath
, dirs
, fFlags
);
2180 BOOL Success
= FALSE
;
2182 LPWSTR fname
= NULL
;
2183 WCHAR szPath
[MAX_PATH
];
2185 /* First, search for a valid existing path */
2187 // NOTE: See also: SHELL_FindExecutable()
2190 * List of extensions searched for, by PathResolve with the flag
2191 * PRF_TRYPROGRAMEXTENSIONS == PRF_EXECUTABLE | PRF_VERIFYEXISTS set,
2192 * according to MSDN: https://msdn.microsoft.com/en-us/library/windows/desktop/bb776478(v=vs.85).aspx
2194 static PCWSTR Extensions
[] = {L
".pif", L
".com", L
".bat", L
".cmd", L
".lnk", L
".exe", NULL
};
2195 #define LNK_EXT_INDEX 4 // ".lnk" has index 4 in the array above
2198 * Start at the beginning of the list if PRF_EXECUTABLE is set, otherwise
2199 * just use the last element 'NULL' (no extension checking).
2201 i
= ((fFlags
& PRF_EXECUTABLE
) ? 0 : _countof(Extensions
) - 1);
2202 for (; i
< _countof(Extensions
); ++i
)
2204 /* Ignore shell links ".lnk" if needed */
2205 if ((fFlags
& PRF_DONTFINDLNK
) && (i
== LNK_EXT_INDEX
))
2208 Success
= (SearchPathW(NULL
, pszPath
, Extensions
[i
],
2209 _countof(szPath
), szPath
, NULL
) != 0);
2212 ERR("SearchPathW(pszPath = '%S') failed\n", pszPath
);
2216 ERR("SearchPathW(pszPath = '%S', szPath = '%S') succeeded\n", pszPath
, szPath
);
2223 ERR("SearchPathW(pszPath = '%S') failed\n", pszPath
);
2225 /* We failed, try with PathFindOnPath, as explained by MSDN */
2226 // Success = PathFindOnPathW(pszPath, dirs);
2227 StringCchCopyW(szPath
, _countof(szPath
), pszPath
);
2228 Success
= PathFindOnPathW(szPath
, dirs
);
2231 ERR("PathFindOnPathW(pszPath = '%S') failed\n", pszPath
);
2233 /* We failed again, fall back to building a possible non-existing path */
2234 if (!GetFullPathNameW(pszPath
, _countof(szPath
), szPath
, &fname
))
2236 ERR("GetFullPathNameW(pszPath = '%S') failed\n", pszPath
);
2240 Success
= PathFileExistsW(szPath
);
2242 ERR("PathFileExistsW(szPath = '%S') failed\n", szPath
);
2244 /******************************************************/
2245 /* Question: Why this line is needed only for files?? */
2246 if (fname
&& (_wcsicmp(pszPath
, fname
) == 0))
2248 /******************************************************/
2252 ERR("PathFindOnPathW(pszPath = '%S' ==> '%S') succeeded\n", pszPath
, szPath
);
2256 /* Copy back the results to the caller */
2257 StringCchCopyW(pszPath
, MAX_PATH
, szPath
);
2260 * Since the called functions always checked whether the file path existed,
2261 * we do not need to redo a final check: we can use instead the cached
2262 * result in 'Success'.
2264 return ((fFlags
& PRF_VERIFYEXISTS
) ? Success
: TRUE
);
2268 HRESULT
CShellLink::SetTargetFromPIDLOrPath(LPCITEMIDLIST pidl
, LPCWSTR pszFile
)
2271 LPITEMIDLIST pidlNew
= NULL
;
2272 WCHAR szPath
[MAX_PATH
];
2275 * Not both 'pidl' and 'pszFile' should be set.
2276 * But either one or both can be NULL.
2278 if (pidl
&& pszFile
)
2283 /* Clone the PIDL */
2284 pidlNew
= ILClone(pidl
);
2290 /* Build a PIDL for this path target */
2291 hr
= SHILCreateFromPathW(pszFile
, &pidlNew
, NULL
);
2294 /* This failed, try to resolve the path, then create a simple PIDL */
2296 StringCchCopyW(szPath
, _countof(szPath
), pszFile
);
2297 // FIXME: Because PathResolve is unimplemented, we use our hackish implementation!
2298 HACKISH_PathResolve(szPath
, NULL
, PRF_TRYPROGRAMEXTENSIONS
);
2300 pidlNew
= SHSimpleIDListFromPathW(szPath
);
2301 /******************************************************/
2302 /* Question: Why this line is needed only for files?? */
2303 hr
= (*szPath
? S_OK
: E_INVALIDARG
); // S_FALSE
2304 /******************************************************/
2307 // else if (!pidl && !pszFile) { pidlNew = NULL; hr = S_OK; }
2314 if (SHGetPathFromIDListW(pidlNew
, szPath
))
2318 // TODO: Fully update link info, tracker, file attribs...
2327 /* Update the cached path (for link info) */
2328 ShellLink_GetVolumeInfo(pszFile
, &volume
);
2330 if (m_sPath
!= pszFile
)
2332 m_sPath
= strdupW(pszFile
);
2334 return E_OUTOFMEMORY
;
2341 HRESULT STDMETHODCALLTYPE
CShellLink::SetPath(LPCWSTR pszFile
)
2343 LPWSTR unquoted
= NULL
;
2346 TRACE("(%p)->(path=%s)\n", this, debugstr_w(pszFile
));
2349 return E_INVALIDARG
;
2352 * Allow upgrading Logo3 shortcuts (m_Header.dwFlags & SLDF_HAS_LOGO3ID),
2353 * but forbid upgrading Darwin ones.
2355 if (m_Header
.dwFlags
& SLDF_HAS_DARWINID
)
2358 /* quotes at the ends of the string are stripped */
2359 SIZE_T len
= wcslen(pszFile
);
2360 if (pszFile
[0] == L
'"' && pszFile
[len
-1] == L
'"')
2362 unquoted
= strdupW(pszFile
);
2363 PathUnquoteSpacesW(unquoted
);
2367 /* any other quote marks are invalid */
2368 if (wcschr(pszFile
, L
'"'))
2374 /* Clear the cached path */
2375 HeapFree(GetProcessHeap(), 0, m_sPath
);
2378 /* Check for an advertised target (Logo3 or Darwin) */
2379 if (SetAdvertiseInfo(pszFile
) != S_OK
)
2381 /* This is not an advertised target, but a regular path */
2382 WCHAR szPath
[MAX_PATH
];
2385 * Check whether the user-given file path contains unexpanded
2386 * environment variables. If so, create a target environment block.
2387 * Note that in this block we will store the user-given path.
2388 * It will contain the unexpanded environment variables, but
2389 * it can also contain already expanded path that the user does
2390 * not want to see them unexpanded (e.g. so that they always
2391 * refer to the same place even if the would-be corresponding
2392 * environment variable could change).
2395 SHExpandEnvironmentStringsW(pszFile
, szPath
, _countof(szPath
));
2399 if (*pszFile
&& (wcscmp(pszFile
, szPath
) != 0))
2402 * The user-given file path contains unexpanded environment
2403 * variables, so we need a target environment block.
2406 LPEXP_SZ_LINK pInfo
;
2408 pInfo
= (LPEXP_SZ_LINK
)SHFindDataBlock(m_pDBList
, EXP_SZ_LINK_SIG
);
2411 /* Make sure that the size of the structure is valid */
2412 if (pInfo
->cbSize
!= sizeof(*pInfo
))
2414 ERR("Ooops. This structure is not as expected...\n");
2416 /* Invalid structure, remove it altogether */
2417 m_Header
.dwFlags
&= ~SLDF_HAS_EXP_SZ
;
2418 RemoveDataBlock(EXP_SZ_LINK_SIG
);
2420 /* Reset the pointer and go use the static buffer */
2426 /* Use the static buffer */
2428 buffer
.cbSize
= sizeof(buffer
);
2429 buffer
.dwSignature
= EXP_SZ_LINK_SIG
;
2432 lstrcpynW(pInfo
->szwTarget
, pszFile
, _countof(pInfo
->szwTarget
));
2433 WideCharToMultiByte(CP_ACP
, 0, pszFile
, -1,
2434 pInfo
->szTarget
, _countof(pInfo
->szTarget
), NULL
, NULL
);
2437 if (pInfo
== &buffer
)
2438 hr
= AddDataBlock(pInfo
);
2440 m_Header
.dwFlags
|= SLDF_HAS_EXP_SZ
;
2442 /* Now, make pszFile point to the expanded buffer */
2448 * The user-given file path does not contain unexpanded environment
2449 * variables, so we need to remove any target environment block.
2451 m_Header
.dwFlags
&= ~SLDF_HAS_EXP_SZ
;
2452 RemoveDataBlock(EXP_SZ_LINK_SIG
);
2454 /* pszFile points to the user path */
2457 /* Set the target */
2458 hr
= SetTargetFromPIDLOrPath(NULL
, pszFile
);
2464 HeapFree(GetProcessHeap(), 0, unquoted
);
2468 HRESULT STDMETHODCALLTYPE
CShellLink::AddDataBlock(void* pDataBlock
)
2470 if (SHAddDataBlock(&m_pDBList
, (DATABLOCK_HEADER
*)pDataBlock
))
2478 HRESULT STDMETHODCALLTYPE
CShellLink::CopyDataBlock(DWORD dwSig
, void** ppDataBlock
)
2480 DATABLOCK_HEADER
* pBlock
;
2483 TRACE("%p %08x %p\n", this, dwSig
, ppDataBlock
);
2485 *ppDataBlock
= NULL
;
2487 pBlock
= SHFindDataBlock(m_pDBList
, dwSig
);
2490 ERR("unknown datablock %08x (not found)\n", dwSig
);
2494 pDataBlock
= LocalAlloc(LMEM_ZEROINIT
, pBlock
->cbSize
);
2496 return E_OUTOFMEMORY
;
2498 CopyMemory(pDataBlock
, pBlock
, pBlock
->cbSize
);
2500 *ppDataBlock
= pDataBlock
;
2504 HRESULT STDMETHODCALLTYPE
CShellLink::RemoveDataBlock(DWORD dwSig
)
2506 if (SHRemoveDataBlock(&m_pDBList
, dwSig
))
2514 HRESULT STDMETHODCALLTYPE
CShellLink::GetFlags(DWORD
*pdwFlags
)
2516 TRACE("%p %p\n", this, pdwFlags
);
2517 *pdwFlags
= m_Header
.dwFlags
;
2521 HRESULT STDMETHODCALLTYPE
CShellLink::SetFlags(DWORD dwFlags
)
2524 m_Header
.dwFlags
= dwFlags
;
2533 /**************************************************************************
2534 * CShellLink implementation of IShellExtInit::Initialize()
2536 * Loads the shelllink from the dataobject the shell is pointing to.
2538 HRESULT STDMETHODCALLTYPE
CShellLink::Initialize(LPCITEMIDLIST pidlFolder
, IDataObject
*pdtobj
, HKEY hkeyProgID
)
2540 TRACE("%p %p %p %p\n", this, pidlFolder
, pdtobj
, hkeyProgID
);
2546 format
.cfFormat
= CF_HDROP
;
2548 format
.dwAspect
= DVASPECT_CONTENT
;
2550 format
.tymed
= TYMED_HGLOBAL
;
2553 HRESULT hr
= pdtobj
->GetData(&format
, &stgm
);
2557 UINT count
= DragQueryFileW((HDROP
)stgm
.hGlobal
, -1, NULL
, 0);
2560 count
= DragQueryFileW((HDROP
)stgm
.hGlobal
, 0, NULL
, 0);
2562 LPWSTR path
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, count
* sizeof(WCHAR
));
2565 count
= DragQueryFileW((HDROP
)stgm
.hGlobal
, 0, path
, count
);
2567 HeapFree(GetProcessHeap(), 0, path
);
2570 ReleaseStgMedium(&stgm
);
2575 HRESULT STDMETHODCALLTYPE
CShellLink::QueryContextMenu(HMENU hMenu
, UINT indexMenu
, UINT idCmdFirst
, UINT idCmdLast
, UINT uFlags
)
2579 TRACE("%p %p %u %u %u %u\n", this,
2580 hMenu
, indexMenu
, idCmdFirst
, idCmdLast
, uFlags
);
2583 return E_INVALIDARG
;
2586 if (!LoadStringW(shell32_hInstance
, IDS_OPEN_VERB
, wszOpen
, _countof(wszOpen
)))
2590 ZeroMemory(&mii
, sizeof(mii
));
2591 mii
.cbSize
= sizeof(mii
);
2592 mii
.fMask
= MIIM_TYPE
| MIIM_ID
| MIIM_STATE
;
2593 mii
.dwTypeData
= wszOpen
;
2594 mii
.cch
= wcslen(mii
.dwTypeData
);
2595 mii
.wID
= idCmdFirst
+ id
++;
2596 mii
.fState
= MFS_DEFAULT
| MFS_ENABLED
;
2597 mii
.fType
= MFT_STRING
;
2598 if (!InsertMenuItemW(hMenu
, indexMenu
, TRUE
, &mii
))
2602 return MAKE_HRESULT(SEVERITY_SUCCESS
, 0, id
);
2605 HRESULT STDMETHODCALLTYPE
CShellLink::InvokeCommand(LPCMINVOKECOMMANDINFO lpici
)
2610 TRACE("%p %p\n", this, lpici
);
2612 if (lpici
->cbSize
< sizeof(CMINVOKECOMMANDINFO
))
2613 return E_INVALIDARG
;
2615 // NOTE: We could use lpici->hwnd (certainly in case lpici->fMask doesn't contain CMIC_MASK_FLAG_NO_UI)
2616 // as the parent window handle... ?
2617 /* FIXME: get using interface set from IObjectWithSite?? */
2618 // NOTE: We might need an extended version of Resolve that provides us with paths...
2619 HRESULT hr
= Resolve(lpici
->hwnd
, 0);
2622 TRACE("failed to resolve component with error 0x%08x", hr
);
2626 path
= strdupW(m_sPath
);
2628 if ( lpici
->cbSize
== sizeof(CMINVOKECOMMANDINFOEX
) &&
2629 (lpici
->fMask
& CMIC_MASK_UNICODE
) )
2631 LPCMINVOKECOMMANDINFOEX iciex
= (LPCMINVOKECOMMANDINFOEX
)lpici
;
2635 len
+= wcslen(m_sArgs
);
2636 if (iciex
->lpParametersW
)
2637 len
+= wcslen(iciex
->lpParametersW
);
2639 args
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
2642 wcscat(args
, m_sArgs
);
2643 if (iciex
->lpParametersW
)
2646 wcscat(args
, iciex
->lpParametersW
);
2649 else if (m_sArgs
!= NULL
)
2651 args
= strdupW(m_sArgs
);
2654 SHELLEXECUTEINFOW sei
;
2655 ZeroMemory(&sei
, sizeof(sei
));
2656 sei
.cbSize
= sizeof(sei
);
2657 sei
.fMask
= SEE_MASK_HASLINKNAME
| SEE_MASK_UNICODE
|
2658 (lpici
->fMask
& (SEE_MASK_NOASYNC
| SEE_MASK_ASYNCOK
| SEE_MASK_FLAG_NO_UI
));
2660 sei
.lpClass
= m_sLinkPath
;
2661 sei
.nShow
= m_Header
.nShowCommand
;
2662 sei
.lpDirectory
= m_sWorkDir
;
2663 sei
.lpParameters
= args
;
2664 sei
.lpVerb
= L
"open";
2666 // HACK for ShellExecuteExW
2667 if (m_sPath
&& wcsstr(m_sPath
, L
".cpl"))
2668 sei
.lpVerb
= L
"cplopen";
2670 if (ShellExecuteExW(&sei
))
2675 HeapFree(GetProcessHeap(), 0, args
);
2676 HeapFree(GetProcessHeap(), 0, path
);
2681 HRESULT STDMETHODCALLTYPE
CShellLink::GetCommandString(UINT_PTR idCmd
, UINT uType
, UINT
* pwReserved
, LPSTR pszName
, UINT cchMax
)
2683 FIXME("%p %lu %u %p %p %u\n", this, idCmd
, uType
, pwReserved
, pszName
, cchMax
);
2687 INT_PTR CALLBACK
ExtendedShortcutProc(HWND hwndDlg
, UINT uMsg
,
2688 WPARAM wParam
, LPARAM lParam
)
2695 HWND hDlgCtrl
= GetDlgItem(hwndDlg
, IDC_SHORTEX_RUN_DIFFERENT
);
2696 SendMessage(hDlgCtrl
, BM_SETCHECK
, BST_CHECKED
, 0);
2701 HWND hDlgCtrl
= GetDlgItem(hwndDlg
, IDC_SHORTEX_RUN_DIFFERENT
);
2702 if (LOWORD(wParam
) == IDOK
)
2704 if (SendMessage(hDlgCtrl
, BM_GETCHECK
, 0, 0) == BST_CHECKED
)
2705 EndDialog(hwndDlg
, 1);
2707 EndDialog(hwndDlg
, 0);
2709 else if (LOWORD(wParam
) == IDCANCEL
)
2711 EndDialog(hwndDlg
, -1);
2713 else if (LOWORD(wParam
) == IDC_SHORTEX_RUN_DIFFERENT
)
2715 if (SendMessage(hDlgCtrl
, BM_GETCHECK
, 0, 0) == BST_CHECKED
)
2716 SendMessage(hDlgCtrl
, BM_SETCHECK
, BST_UNCHECKED
, 0);
2718 SendMessage(hDlgCtrl
, BM_SETCHECK
, BST_CHECKED
, 0);
2727 SHOpenFolderAndSelectItems(LPITEMIDLIST pidlFolder
,
2729 PCUITEMID_CHILD_ARRAY apidl
,
2732 /**************************************************************************
2733 * SH_GetTargetTypeByPath
2735 * Function to get target type by passing full path to it
2737 LPWSTR
SH_GetTargetTypeByPath(LPCWSTR lpcwFullPath
)
2740 static WCHAR wszBuf
[MAX_PATH
];
2742 /* Get file information */
2744 if (!SHGetFileInfoW(lpcwFullPath
, 0, &fi
, sizeof(fi
), SHGFI_TYPENAME
| SHGFI_USEFILEATTRIBUTES
))
2746 ERR("SHGetFileInfoW failed for %ls (%lu)\n", lpcwFullPath
, GetLastError());
2747 fi
.szTypeName
[0] = L
'\0';
2751 pwszExt
= PathFindExtensionW(lpcwFullPath
);
2754 if (!fi
.szTypeName
[0])
2756 /* The file type is unknown, so default to string "FileExtension File" */
2757 size_t cchRemaining
= 0;
2758 LPWSTR pwszEnd
= NULL
;
2760 StringCchPrintfExW(wszBuf
, _countof(wszBuf
), &pwszEnd
, &cchRemaining
, 0, L
"%s ", pwszExt
+ 1);
2764 /* Update file type */
2765 StringCbPrintfW(wszBuf
, sizeof(wszBuf
), L
"%s (%s)", fi
.szTypeName
, pwszExt
);
2772 BOOL
CShellLink::OnInitDialog(HWND hwndDlg
, HWND hwndFocus
, LPARAM lParam
)
2774 TRACE("CShellLink::OnInitDialog(hwnd %p hwndFocus %p lParam %p)\n", hwndDlg
, hwndFocus
, lParam
);
2776 TRACE("m_sArgs: %S sComponent: %S m_sDescription: %S m_sIcoPath: %S m_sPath: %S m_sPathRel: %S sProduct: %S m_sWorkDir: %S\n", m_sArgs
, sComponent
, m_sDescription
,
2777 m_sIcoPath
, m_sPath
, m_sPathRel
, sProduct
, m_sWorkDir
);
2781 /* Get file information */
2782 // FIXME! FIXME! Shouldn't we use m_sIcoPath, m_Header.nIconIndex instead???
2784 if (!SHGetFileInfoW(m_sLinkPath
, 0, &fi
, sizeof(fi
), SHGFI_TYPENAME
| SHGFI_ICON
))
2786 ERR("SHGetFileInfoW failed for %ls (%lu)\n", m_sLinkPath
, GetLastError());
2787 fi
.szTypeName
[0] = L
'\0';
2794 DestroyIcon(m_hIcon
);
2796 SendDlgItemMessageW(hwndDlg
, IDC_SHORTCUT_ICON
, STM_SETICON
, (WPARAM
)m_hIcon
, 0);
2799 ERR("ExtractIconW failed %ls %u\n", m_sIcoPath
, m_Header
.nIconIndex
);
2803 SetDlgItemTextW(hwndDlg
, IDC_SHORTCUT_TYPE_EDIT
, SH_GetTargetTypeByPath(m_sPath
));
2805 /* Target location */
2808 WCHAR target
[MAX_PATH
];
2809 StringCchCopyW(target
, _countof(target
), m_sPath
);
2810 PathRemoveFileSpecW(target
);
2811 SetDlgItemTextW(hwndDlg
, IDC_SHORTCUT_LOCATION_EDIT
, PathFindFileNameW(target
));
2817 WCHAR newpath
[2*MAX_PATH
] = L
"\0";
2818 if (wcschr(m_sPath
, ' '))
2819 StringCchPrintfExW(newpath
, _countof(newpath
), NULL
, NULL
, 0, L
"\"%ls\"", m_sPath
);
2821 StringCchCopyExW(newpath
, _countof(newpath
), m_sPath
, NULL
, NULL
, 0);
2823 if (m_sArgs
&& m_sArgs
[0])
2825 StringCchCatW(newpath
, _countof(newpath
), L
" ");
2826 StringCchCatW(newpath
, _countof(newpath
), m_sArgs
);
2828 SetDlgItemTextW(hwndDlg
, IDC_SHORTCUT_TARGET_TEXT
, newpath
);
2833 SetDlgItemTextW(hwndDlg
, IDC_SHORTCUT_START_IN_EDIT
, m_sWorkDir
);
2837 SetDlgItemTextW(hwndDlg
, IDC_SHORTCUT_COMMENT_EDIT
, m_sDescription
);
2844 void CShellLink::OnCommand(HWND hwndDlg
, int id
, HWND hwndCtl
, UINT codeNotify
)
2848 case IDC_SHORTCUT_FIND
:
2849 SHOpenFolderAndSelectItems(m_pPidl
, 0, NULL
, 0);
2852 /// open target directory
2856 case IDC_SHORTCUT_CHANGE_ICON
:
2858 WCHAR wszPath
[MAX_PATH
] = L
"";
2861 wcscpy(wszPath
, m_sIcoPath
);
2863 FindExecutableW(m_sPath
, NULL
, wszPath
);
2865 INT IconIndex
= m_Header
.nIconIndex
;
2866 if (PickIconDlg(hwndDlg
, wszPath
, _countof(wszPath
), &IconIndex
))
2868 SetIconLocation(wszPath
, IconIndex
);
2869 PropSheet_Changed(GetParent(hwndDlg
), hwndDlg
);
2871 HICON hIconLarge
= CreateShortcutIcon(wszPath
, IconIndex
);
2875 DestroyIcon(m_hIcon
);
2876 m_hIcon
= hIconLarge
;
2877 SendDlgItemMessageW(hwndDlg
, IDC_SHORTCUT_ICON
, STM_SETICON
, (WPARAM
)m_hIcon
, 0);
2883 case IDC_SHORTCUT_ADVANCED
:
2885 INT_PTR result
= DialogBoxParamW(shell32_hInstance
, MAKEINTRESOURCEW(IDD_SHORTCUT_EXTENDED_PROPERTIES
), hwndDlg
, ExtendedShortcutProc
, (LPARAM
)m_bRunAs
);
2886 if (result
== 1 || result
== 0)
2888 if (m_bRunAs
!= result
)
2890 PropSheet_Changed(GetParent(hwndDlg
), hwndDlg
);
2898 if (codeNotify
== EN_CHANGE
)
2901 PropSheet_Changed(GetParent(hwndDlg
), hwndDlg
);
2905 LRESULT
CShellLink::OnNotify(HWND hwndDlg
, int idFrom
, LPNMHDR pnmhdr
)
2907 WCHAR wszBuf
[MAX_PATH
];
2908 LPPSHNOTIFY lppsn
= (LPPSHNOTIFY
)pnmhdr
;
2910 if (lppsn
->hdr
.code
== PSN_APPLY
)
2912 /* set working directory */
2913 GetDlgItemTextW(hwndDlg
, IDC_SHORTCUT_START_IN_EDIT
, wszBuf
, _countof(wszBuf
));
2914 SetWorkingDirectory(wszBuf
);
2916 /* set link destination */
2917 GetDlgItemTextW(hwndDlg
, IDC_SHORTCUT_TARGET_TEXT
, wszBuf
, _countof(wszBuf
));
2918 LPWSTR lpszArgs
= NULL
;
2919 LPWSTR unquoted
= strdupW(wszBuf
);
2920 StrTrimW(unquoted
, L
" ");
2922 if (!PathFileExistsW(unquoted
))
2924 lpszArgs
= PathGetArgsW(unquoted
);
2925 PathRemoveArgsW(unquoted
);
2926 StrTrimW(lpszArgs
, L
" ");
2928 if (unquoted
[0] == '"' && unquoted
[wcslen(unquoted
) - 1] == '"')
2929 PathUnquoteSpacesW(unquoted
);
2931 WCHAR
*pwszExt
= PathFindExtensionW(unquoted
);
2932 if (!wcsicmp(pwszExt
, L
".lnk"))
2934 // FIXME load localized error msg
2935 MessageBoxW(hwndDlg
, L
"You cannot create a link to a shortcut", L
"Error", MB_ICONERROR
);
2936 SetWindowLongPtr(hwndDlg
, DWLP_MSGRESULT
, PSNRET_INVALID_NOCHANGEPAGE
);
2940 if (!PathFileExistsW(unquoted
))
2942 // FIXME load localized error msg
2943 MessageBoxW(hwndDlg
, L
"The specified file name in the target box is invalid", L
"Error", MB_ICONERROR
);
2944 SetWindowLongPtr(hwndDlg
, DWLP_MSGRESULT
, PSNRET_INVALID_NOCHANGEPAGE
);
2950 SetArguments(lpszArgs
);
2952 SetArguments(L
"\0");
2954 HeapFree(GetProcessHeap(), 0, unquoted
);
2956 TRACE("This %p m_sLinkPath %S\n", this, m_sLinkPath
);
2957 Save(m_sLinkPath
, TRUE
);
2958 SHChangeNotify(SHCNE_UPDATEITEM
, SHCNF_PATHW
, m_sLinkPath
, NULL
);
2959 SetWindowLongPtr(hwndDlg
, DWLP_MSGRESULT
, PSNRET_NOERROR
);
2965 void CShellLink::OnDestroy(HWND hwndDlg
)
2969 DestroyIcon(m_hIcon
);
2974 /**************************************************************************
2975 * SH_ShellLinkDlgProc
2977 * dialog proc of the shortcut property dialog
2981 CShellLink::SH_ShellLinkDlgProc(HWND hwndDlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
2983 LPPROPSHEETPAGEW ppsp
;
2984 CShellLink
*pThis
= reinterpret_cast<CShellLink
*>(GetWindowLongPtr(hwndDlg
, DWLP_USER
));
2989 ppsp
= (LPPROPSHEETPAGEW
)lParam
;
2993 pThis
= reinterpret_cast<CShellLink
*>(ppsp
->lParam
);
2994 SetWindowLongPtr(hwndDlg
, DWLP_USER
, (LONG_PTR
)pThis
);
2995 return pThis
->OnInitDialog(hwndDlg
, (HWND
)(wParam
), lParam
);
2998 return pThis
->OnNotify(hwndDlg
, (int)wParam
, (NMHDR
*)lParam
);
3001 pThis
->OnCommand(hwndDlg
, LOWORD(wParam
), (HWND
)lParam
, HIWORD(wParam
));
3005 pThis
->OnDestroy(hwndDlg
);
3015 /**************************************************************************
3016 * ShellLink_IShellPropSheetExt interface
3019 HRESULT STDMETHODCALLTYPE
CShellLink::AddPages(LPFNADDPROPSHEETPAGE pfnAddPage
, LPARAM lParam
)
3021 HPROPSHEETPAGE hPage
= SH_CreatePropertySheetPage(IDD_SHORTCUT_PROPERTIES
, SH_ShellLinkDlgProc
, (LPARAM
)this, NULL
);
3024 ERR("failed to create property sheet page\n");
3028 if (!pfnAddPage(hPage
, lParam
))
3034 HRESULT STDMETHODCALLTYPE
CShellLink::ReplacePage(UINT uPageID
, LPFNADDPROPSHEETPAGE pfnReplacePage
, LPARAM lParam
)
3036 TRACE("(%p) (uPageID %u, pfnReplacePage %p lParam %p\n", this, uPageID
, pfnReplacePage
, lParam
);
3040 HRESULT STDMETHODCALLTYPE
CShellLink::SetSite(IUnknown
*punk
)
3042 TRACE("%p %p\n", this, punk
);
3049 HRESULT STDMETHODCALLTYPE
CShellLink::GetSite(REFIID iid
, void ** ppvSite
)
3051 TRACE("%p %s %p\n", this, debugstr_guid(&iid
), ppvSite
);
3056 return m_site
->QueryInterface(iid
, ppvSite
);
3059 HRESULT STDMETHODCALLTYPE
CShellLink::DragEnter(IDataObject
*pDataObject
,
3060 DWORD dwKeyState
, POINTL pt
, DWORD
*pdwEffect
)
3062 TRACE("(%p)->(DataObject=%p)\n", this, pDataObject
);
3063 LPCITEMIDLIST pidlLast
;
3064 CComPtr
<IShellFolder
> psf
;
3066 HRESULT hr
= SHBindToParent(m_pPidl
, IID_PPV_ARG(IShellFolder
, &psf
), &pidlLast
);
3070 hr
= psf
->GetUIObjectOf(0, 1, &pidlLast
, IID_NULL_PPV_ARG(IDropTarget
, &m_DropTarget
));
3073 hr
= m_DropTarget
->DragEnter(pDataObject
, dwKeyState
, pt
, pdwEffect
);
3075 *pdwEffect
= DROPEFFECT_NONE
;
3078 *pdwEffect
= DROPEFFECT_NONE
;
3083 HRESULT STDMETHODCALLTYPE
CShellLink::DragOver(DWORD dwKeyState
, POINTL pt
,
3086 TRACE("(%p)\n", this);
3089 hr
= m_DropTarget
->DragOver(dwKeyState
, pt
, pdwEffect
);
3093 HRESULT STDMETHODCALLTYPE
CShellLink::DragLeave()
3095 TRACE("(%p)\n", this);
3099 hr
= m_DropTarget
->DragLeave();
3100 m_DropTarget
.Release();
3106 HRESULT STDMETHODCALLTYPE
CShellLink::Drop(IDataObject
*pDataObject
,
3107 DWORD dwKeyState
, POINTL pt
, DWORD
*pdwEffect
)
3109 TRACE("(%p)\n", this);
3112 hr
= m_DropTarget
->Drop(pDataObject
, dwKeyState
, pt
, pdwEffect
);
3117 /**************************************************************************
3118 * IShellLink_ConstructFromFile
3120 HRESULT WINAPI
IShellLink_ConstructFromPath(WCHAR
*path
, REFIID riid
, LPVOID
*ppv
)
3122 CComPtr
<IPersistFile
> ppf
;
3123 HRESULT hr
= CShellLink::_CreatorClass::CreateInstance(NULL
, IID_PPV_ARG(IPersistFile
, &ppf
));
3127 hr
= ppf
->Load(path
, 0);
3131 return ppf
->QueryInterface(riid
, ppv
);
3134 HRESULT WINAPI
IShellLink_ConstructFromFile(IShellFolder
* psf
, LPCITEMIDLIST pidl
, REFIID riid
, LPVOID
*ppv
)
3136 WCHAR path
[MAX_PATH
];
3137 if (!ILGetDisplayNameExW(psf
, pidl
, path
, 0))
3140 return IShellLink_ConstructFromPath(path
, riid
, ppv
);
3143 HICON
CShellLink::CreateShortcutIcon(LPCWSTR wszIconPath
, INT IconIndex
)
3145 const INT cx
= GetSystemMetrics(SM_CXICON
), cy
= GetSystemMetrics(SM_CYICON
);
3146 const COLORREF crMask
= GetSysColor(COLOR_3DFACE
);
3148 HIMAGELIST himl
= ImageList_Create(cx
, cy
, ILC_COLOR32
| ILC_MASK
, 1, 1);
3149 HICON hIcon
= NULL
, hNewIcon
= NULL
;
3150 HICON hShortcut
= (HICON
)LoadImageW(shell32_hInstance
, MAKEINTRESOURCE(IDI_SHELL_SHORTCUT
),
3151 IMAGE_ICON
, cx
, cy
, 0);
3153 ::ExtractIconExW(wszIconPath
, IconIndex
, &hIcon
, NULL
, 1);
3154 if (!hIcon
|| !hShortcut
|| !himl
)
3157 hDC
= CreateCompatibleDC(NULL
);
3160 // create 32bpp bitmap
3162 ZeroMemory(&bi
, sizeof(bi
));
3163 bi
.bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
3164 bi
.bmiHeader
.biWidth
= cx
;
3165 bi
.bmiHeader
.biHeight
= cy
;
3166 bi
.bmiHeader
.biPlanes
= 1;
3167 bi
.bmiHeader
.biBitCount
= 32;
3169 HBITMAP hbm
= CreateDIBSection(hDC
, &bi
, DIB_RGB_COLORS
, &pvBits
, NULL
, 0);
3172 // draw the icon image
3173 HGDIOBJ hbmOld
= SelectObject(hDC
, hbm
);
3175 HBRUSH hbr
= CreateSolidBrush(crMask
);
3176 RECT rc
= { 0, 0, cx
, cy
};
3177 FillRect(hDC
, &rc
, hbr
);
3180 DrawIconEx(hDC
, 0, 0, hIcon
, cx
, cy
, 0, NULL
, DI_NORMAL
);
3181 DrawIconEx(hDC
, 0, 0, hShortcut
, cx
, cy
, 0, NULL
, DI_NORMAL
);
3183 SelectObject(hDC
, hbmOld
);
3185 INT iAdded
= ImageList_AddMasked(himl
, hbm
, crMask
);
3186 hNewIcon
= ImageList_GetIcon(himl
, iAdded
, ILD_NORMAL
| ILD_TRANSPARENT
);
3197 DestroyIcon(hShortcut
);
3199 ImageList_Destroy(himl
);