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
);
353 HeapFree(GetProcessHeap(), 0, m_sLinkPath
);
355 m_sLinkPath
= strdupW(pszFileName
);
360 DeleteFileW(pszFileName
);
361 WARN("Failed to create shortcut %s\n", debugstr_w(pszFileName
));
368 HRESULT STDMETHODCALLTYPE
CShellLink::SaveCompleted(LPCOLESTR pszFileName
)
370 FIXME("(%p)->(%s)\n", this, debugstr_w(pszFileName
));
374 HRESULT STDMETHODCALLTYPE
CShellLink::GetCurFile(LPOLESTR
*ppszFileName
)
376 *ppszFileName
= NULL
;
380 /* IPersistFile::GetCurFile called before IPersistFile::Save */
384 *ppszFileName
= (LPOLESTR
)CoTaskMemAlloc((wcslen(m_sLinkPath
) + 1) * sizeof(WCHAR
));
388 return E_OUTOFMEMORY
;
391 /* copy last saved filename */
392 wcscpy(*ppszFileName
, m_sLinkPath
);
397 static HRESULT
Stream_LoadString(IStream
* stm
, BOOL unicode
, LPWSTR
*pstr
)
403 HRESULT hr
= stm
->Read(&len
, sizeof(len
), &count
);
404 if (FAILED(hr
) || count
!= sizeof(len
))
408 len
*= sizeof(WCHAR
);
410 TRACE("reading %d\n", len
);
411 LPSTR temp
= (LPSTR
)HeapAlloc(GetProcessHeap(), 0, len
+ sizeof(WCHAR
));
413 return E_OUTOFMEMORY
;
415 hr
= stm
->Read(temp
, len
, &count
);
416 if (FAILED(hr
) || count
!= len
)
418 HeapFree(GetProcessHeap(), 0, temp
);
422 TRACE("read %s\n", debugstr_an(temp
, len
));
424 /* convert to unicode if necessary */
428 count
= MultiByteToWideChar(CP_ACP
, 0, temp
, len
, NULL
, 0);
429 str
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, (count
+ 1) * sizeof(WCHAR
));
432 HeapFree(GetProcessHeap(), 0, temp
);
433 return E_OUTOFMEMORY
;
435 MultiByteToWideChar(CP_ACP
, 0, temp
, len
, str
, count
);
436 HeapFree(GetProcessHeap(), 0, temp
);
440 count
/= sizeof(WCHAR
);
452 * NOTE: The following 5 functions are part of LINKINFO.DLL
454 static BOOL
ShellLink_GetVolumeInfo(LPCWSTR path
, CShellLink::volume_info
*volume
)
456 WCHAR drive
[4] = { path
[0], ':', '\\', 0 };
458 volume
->type
= GetDriveTypeW(drive
);
459 BOOL bRet
= GetVolumeInformationW(drive
, volume
->label
, _countof(volume
->label
), &volume
->serial
, NULL
, NULL
, NULL
, 0);
460 TRACE("ret = %d type %d serial %08x name %s\n", bRet
,
461 volume
->type
, volume
->serial
, debugstr_w(volume
->label
));
465 static HRESULT
Stream_ReadChunk(IStream
* stm
, LPVOID
*data
)
470 unsigned char data
[1];
477 HRESULT hr
= stm
->Read(&size
, sizeof(size
), &count
);
478 if (FAILED(hr
) || count
!= sizeof(size
))
481 chunk
= static_cast<sized_chunk
*>(HeapAlloc(GetProcessHeap(), 0, size
));
483 return E_OUTOFMEMORY
;
486 hr
= stm
->Read(chunk
->data
, size
- sizeof(size
), &count
);
487 if (FAILED(hr
) || count
!= (size
- sizeof(size
)))
489 HeapFree(GetProcessHeap(), 0, chunk
);
493 TRACE("Read %d bytes\n", chunk
->size
);
500 static BOOL
Stream_LoadVolume(LOCAL_VOLUME_INFO
*vol
, CShellLink::volume_info
*volume
)
502 volume
->serial
= vol
->dwVolSerial
;
503 volume
->type
= vol
->dwType
;
505 if (!vol
->dwVolLabelOfs
)
507 if (vol
->dwSize
<= vol
->dwVolLabelOfs
)
509 INT len
= vol
->dwSize
- vol
->dwVolLabelOfs
;
511 LPSTR label
= (LPSTR
)vol
;
512 label
+= vol
->dwVolLabelOfs
;
513 MultiByteToWideChar(CP_ACP
, 0, label
, len
, volume
->label
, _countof(volume
->label
));
518 static LPWSTR
Stream_LoadPath(LPCSTR p
, DWORD maxlen
)
522 while (p
[len
] && len
< maxlen
)
525 UINT wlen
= MultiByteToWideChar(CP_ACP
, 0, p
, len
, NULL
, 0);
526 LPWSTR path
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, (wlen
+ 1) * sizeof(WCHAR
));
529 MultiByteToWideChar(CP_ACP
, 0, p
, len
, path
, wlen
);
535 static HRESULT
Stream_LoadLocation(IStream
*stm
,
536 CShellLink::volume_info
*volume
, LPWSTR
*path
)
539 HRESULT hr
= Stream_ReadChunk(stm
, (LPVOID
*) &p
);
543 LOCATION_INFO
*loc
= reinterpret_cast<LOCATION_INFO
*>(p
);
544 if (loc
->dwTotalSize
< sizeof(LOCATION_INFO
))
546 HeapFree(GetProcessHeap(), 0, p
);
550 /* if there's valid local volume information, load it */
551 if (loc
->dwVolTableOfs
&&
552 ((loc
->dwVolTableOfs
+ sizeof(LOCAL_VOLUME_INFO
)) <= loc
->dwTotalSize
))
554 LOCAL_VOLUME_INFO
*volume_info
;
556 volume_info
= (LOCAL_VOLUME_INFO
*) &p
[loc
->dwVolTableOfs
];
557 Stream_LoadVolume(volume_info
, volume
);
560 /* if there's a local path, load it */
561 DWORD n
= loc
->dwLocalPathOfs
;
562 if (n
&& n
< loc
->dwTotalSize
)
563 *path
= Stream_LoadPath(&p
[n
], loc
->dwTotalSize
- n
);
565 TRACE("type %d serial %08x name %s path %s\n", volume
->type
,
566 volume
->serial
, debugstr_w(volume
->label
), debugstr_w(*path
));
568 HeapFree(GetProcessHeap(), 0, p
);
574 * The format of the advertised shortcut info is:
578 * 0 Length of the block (4 bytes, usually 0x314)
580 * 8 string data in ASCII
581 * 8+0x104 string data in UNICODE
583 * In the original Win32 implementation the buffers are not initialized
584 * to zero, so data trailing the string is random garbage.
586 HRESULT
CShellLink::GetAdvertiseInfo(LPWSTR
*str
, DWORD dwSig
)
588 LPEXP_DARWIN_LINK pInfo
;
592 pInfo
= (LPEXP_DARWIN_LINK
)SHFindDataBlock(m_pDBList
, dwSig
);
596 /* Make sure that the size of the structure is valid */
597 if (pInfo
->dbh
.cbSize
!= sizeof(*pInfo
))
599 ERR("Ooops. This structure is not as expected...\n");
603 TRACE("dwSig %08x string = '%s'\n", pInfo
->dbh
.dwSignature
, debugstr_w(pInfo
->szwDarwinID
));
605 *str
= pInfo
->szwDarwinID
;
609 /************************************************************************
610 * IPersistStream_Load (IPersistStream)
612 HRESULT STDMETHODCALLTYPE
CShellLink::Load(IStream
*stm
)
614 TRACE("%p %p\n", this, stm
);
617 return STG_E_INVALIDPOINTER
;
619 /* Free all the old stuff */
622 ULONG dwBytesRead
= 0;
623 HRESULT hr
= stm
->Read(&m_Header
, sizeof(m_Header
), &dwBytesRead
);
627 if (dwBytesRead
!= sizeof(m_Header
))
629 if (m_Header
.dwSize
!= sizeof(m_Header
))
631 if (!IsEqualIID(m_Header
.clsid
, CLSID_ShellLink
))
634 /* Load the new data in order */
638 SYSTEMTIME stCreationTime
;
639 SYSTEMTIME stLastAccessTime
;
640 SYSTEMTIME stLastWriteTime
;
641 WCHAR sTemp
[MAX_PATH
];
643 FileTimeToSystemTime(&m_Header
.ftCreationTime
, &stCreationTime
);
644 FileTimeToSystemTime(&m_Header
.ftLastAccessTime
, &stLastAccessTime
);
645 FileTimeToSystemTime(&m_Header
.ftLastWriteTime
, &stLastWriteTime
);
647 GetDateFormatW(LOCALE_USER_DEFAULT
, DATE_SHORTDATE
, &stCreationTime
,
648 NULL
, sTemp
, _countof(sTemp
));
649 TRACE("-- stCreationTime: %s\n", debugstr_w(sTemp
));
650 GetDateFormatW(LOCALE_USER_DEFAULT
, DATE_SHORTDATE
, &stLastAccessTime
,
651 NULL
, sTemp
, _countof(sTemp
));
652 TRACE("-- stLastAccessTime: %s\n", debugstr_w(sTemp
));
653 GetDateFormatW(LOCALE_USER_DEFAULT
, DATE_SHORTDATE
, &stLastWriteTime
,
654 NULL
, sTemp
, _countof(sTemp
));
655 TRACE("-- stLastWriteTime: %s\n", debugstr_w(sTemp
));
658 /* load all the new stuff */
659 if (m_Header
.dwFlags
& SLDF_HAS_ID_LIST
)
661 hr
= ILLoadFromStream(stm
, &m_pPidl
);
667 /* Load the location information... */
668 if (m_Header
.dwFlags
& SLDF_HAS_LINK_INFO
)
670 hr
= Stream_LoadLocation(stm
, &volume
, &m_sPath
);
674 /* ... but if it is required not to use it, clear it */
675 if (m_Header
.dwFlags
& SLDF_FORCE_NO_LINKINFO
)
677 HeapFree(GetProcessHeap(), 0, m_sPath
);
679 ZeroMemory(&volume
, sizeof(volume
));
682 BOOL unicode
= !!(m_Header
.dwFlags
& SLDF_UNICODE
);
684 if (m_Header
.dwFlags
& SLDF_HAS_NAME
)
686 hr
= Stream_LoadString(stm
, unicode
, &m_sDescription
);
689 TRACE("Description -> %s\n", debugstr_w(m_sDescription
));
692 if (m_Header
.dwFlags
& SLDF_HAS_RELPATH
)
694 hr
= Stream_LoadString(stm
, unicode
, &m_sPathRel
);
697 TRACE("Relative Path-> %s\n", debugstr_w(m_sPathRel
));
700 if (m_Header
.dwFlags
& SLDF_HAS_WORKINGDIR
)
702 hr
= Stream_LoadString(stm
, unicode
, &m_sWorkDir
);
705 PathRemoveBackslash(m_sWorkDir
);
706 TRACE("Working Dir -> %s\n", debugstr_w(m_sWorkDir
));
709 if (m_Header
.dwFlags
& SLDF_HAS_ARGS
)
711 hr
= Stream_LoadString(stm
, unicode
, &m_sArgs
);
714 TRACE("Arguments -> %s\n", debugstr_w(m_sArgs
));
717 if (m_Header
.dwFlags
& SLDF_HAS_ICONLOCATION
)
719 hr
= Stream_LoadString(stm
, unicode
, &m_sIcoPath
);
722 TRACE("Icon file -> %s\n", debugstr_w(m_sIcoPath
));
725 /* Now load the optional data block list */
726 hr
= SHReadDataBlockList(stm
, &m_pDBList
);
727 if (FAILED(hr
)) // FIXME: Should we fail?
732 #if (NTDDI_VERSION < NTDDI_LONGHORN)
733 if (m_Header
.dwFlags
& SLDF_HAS_LOGO3ID
)
735 hr
= GetAdvertiseInfo(&sProduct
, EXP_LOGO3_ID_SIG
);
737 TRACE("Product -> %s\n", debugstr_w(sProduct
));
740 if (m_Header
.dwFlags
& SLDF_HAS_DARWINID
)
742 hr
= GetAdvertiseInfo(&sComponent
, EXP_DARWIN_ID_SIG
);
744 TRACE("Component -> %s\n", debugstr_w(sComponent
));
748 if (m_Header
.dwFlags
& SLDF_RUNAS_USER
)
760 /************************************************************************
763 * Helper function for IPersistStream_Save. Writes a unicode string
764 * with terminating nul byte to a stream, preceded by the its length.
766 static HRESULT
Stream_WriteString(IStream
* stm
, LPCWSTR str
)
768 USHORT len
= wcslen(str
) + 1; // FIXME: Possible overflows?
771 HRESULT hr
= stm
->Write(&len
, sizeof(len
), &count
);
775 len
*= sizeof(WCHAR
);
777 hr
= stm
->Write(str
, len
, &count
);
784 /************************************************************************
785 * Stream_WriteLocationInfo
787 * Writes the location info to a stream
789 * FIXME: One day we might want to write the network volume information
790 * and the final path.
791 * Figure out how Windows deals with unicode paths here.
793 static HRESULT
Stream_WriteLocationInfo(IStream
* stm
, LPCWSTR path
,
794 CShellLink::volume_info
*volume
)
796 LOCAL_VOLUME_INFO
*vol
;
799 TRACE("%p %s %p\n", stm
, debugstr_w(path
), volume
);
801 /* figure out the size of everything */
802 DWORD label_size
= WideCharToMultiByte(CP_ACP
, 0, volume
->label
, -1,
803 NULL
, 0, NULL
, NULL
);
804 DWORD path_size
= WideCharToMultiByte(CP_ACP
, 0, path
, -1,
805 NULL
, 0, NULL
, NULL
);
806 DWORD volume_info_size
= sizeof(*vol
) + label_size
;
807 DWORD final_path_size
= 1;
808 DWORD total_size
= sizeof(*loc
) + volume_info_size
+ path_size
+ final_path_size
;
810 /* create pointers to everything */
811 loc
= static_cast<LOCATION_INFO
*>(HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, total_size
));
812 vol
= (LOCAL_VOLUME_INFO
*) &loc
[1];
813 LPSTR szLabel
= (LPSTR
) &vol
[1];
814 LPSTR szPath
= &szLabel
[label_size
];
815 LPSTR szFinalPath
= &szPath
[path_size
];
817 /* fill in the location information header */
818 loc
->dwTotalSize
= total_size
;
819 loc
->dwHeaderSize
= sizeof(*loc
);
821 loc
->dwVolTableOfs
= sizeof(*loc
);
822 loc
->dwLocalPathOfs
= sizeof(*loc
) + volume_info_size
;
823 loc
->dwNetworkVolTableOfs
= 0;
824 loc
->dwFinalPathOfs
= sizeof(*loc
) + volume_info_size
+ path_size
;
826 /* fill in the volume information */
827 vol
->dwSize
= volume_info_size
;
828 vol
->dwType
= volume
->type
;
829 vol
->dwVolSerial
= volume
->serial
;
830 vol
->dwVolLabelOfs
= sizeof(*vol
);
832 /* copy in the strings */
833 WideCharToMultiByte(CP_ACP
, 0, volume
->label
, -1,
834 szLabel
, label_size
, NULL
, NULL
);
835 WideCharToMultiByte(CP_ACP
, 0, path
, -1,
836 szPath
, path_size
, NULL
, NULL
);
840 HRESULT hr
= stm
->Write(loc
, total_size
, &count
);
841 HeapFree(GetProcessHeap(), 0, loc
);
846 /************************************************************************
847 * IPersistStream_Save (IPersistStream)
849 * FIXME: makes assumptions about byte order
851 HRESULT STDMETHODCALLTYPE
CShellLink::Save(IStream
*stm
, BOOL fClearDirty
)
853 TRACE("%p %p %x\n", this, stm
, fClearDirty
);
855 m_Header
.dwSize
= sizeof(m_Header
);
856 m_Header
.clsid
= CLSID_ShellLink
;
859 * Reset the flags: keep only the flags related to data blocks as they were
860 * already set in accordance by the different mutator member functions.
861 * The other flags will be determined now by the presence or absence of data.
863 m_Header
.dwFlags
&= (SLDF_RUN_WITH_SHIMLAYER
| SLDF_RUNAS_USER
|
864 SLDF_RUN_IN_SEPARATE
| SLDF_HAS_DARWINID
|
865 #if (NTDDI_VERSION < NTDDI_LONGHORN)
868 SLDF_HAS_EXP_ICON_SZ
| SLDF_HAS_EXP_SZ
);
869 // TODO: When we will support Vista+ functionality, add other flags to this list.
871 /* The stored strings are in UNICODE */
872 m_Header
.dwFlags
|= SLDF_UNICODE
;
875 m_Header
.dwFlags
|= SLDF_HAS_ID_LIST
;
877 m_Header
.dwFlags
|= SLDF_HAS_LINK_INFO
;
878 if (m_sDescription
&& *m_sDescription
)
879 m_Header
.dwFlags
|= SLDF_HAS_NAME
;
880 if (m_sPathRel
&& *m_sPathRel
)
881 m_Header
.dwFlags
|= SLDF_HAS_RELPATH
;
882 if (m_sWorkDir
&& *m_sWorkDir
)
883 m_Header
.dwFlags
|= SLDF_HAS_WORKINGDIR
;
884 if (m_sArgs
&& *m_sArgs
)
885 m_Header
.dwFlags
|= SLDF_HAS_ARGS
;
886 if (m_sIcoPath
&& *m_sIcoPath
)
887 m_Header
.dwFlags
|= SLDF_HAS_ICONLOCATION
;
889 m_Header
.dwFlags
|= SLDF_RUNAS_USER
;
891 /* Write the shortcut header */
893 HRESULT hr
= stm
->Write(&m_Header
, sizeof(m_Header
), &count
);
896 ERR("Write failed\n");
900 /* Save the data in order */
904 hr
= ILSaveToStream(stm
, m_pPidl
);
907 ERR("Failed to write PIDL\n");
914 hr
= Stream_WriteLocationInfo(stm
, m_sPath
, &volume
);
919 if (m_Header
.dwFlags
& SLDF_HAS_NAME
)
921 hr
= Stream_WriteString(stm
, m_sDescription
);
926 if (m_Header
.dwFlags
& SLDF_HAS_RELPATH
)
928 hr
= Stream_WriteString(stm
, m_sPathRel
);
933 if (m_Header
.dwFlags
& SLDF_HAS_WORKINGDIR
)
935 hr
= Stream_WriteString(stm
, m_sWorkDir
);
940 if (m_Header
.dwFlags
& SLDF_HAS_ARGS
)
942 hr
= Stream_WriteString(stm
, m_sArgs
);
947 if (m_Header
.dwFlags
& SLDF_HAS_ICONLOCATION
)
949 hr
= Stream_WriteString(stm
, m_sIcoPath
);
955 * Now save the data block list.
957 * NOTE that both advertised Product and Component are already saved
958 * inside Logo3 and Darwin data blocks in the m_pDBList list, and the
959 * m_Header.dwFlags is suitably initialized.
961 hr
= SHWriteDataBlockList(stm
, m_pDBList
);
965 /* Clear the dirty bit if requested */
972 /************************************************************************
973 * IPersistStream_GetSizeMax (IPersistStream)
975 HRESULT STDMETHODCALLTYPE
CShellLink::GetSizeMax(ULARGE_INTEGER
*pcbSize
)
977 TRACE("(%p)\n", this);
981 static BOOL
SHELL_ExistsFileW(LPCWSTR path
)
983 if (INVALID_FILE_ATTRIBUTES
== GetFileAttributesW(path
))
989 /**************************************************************************
990 * ShellLink_UpdatePath
991 * update absolute path in sPath using relative path in sPathRel
993 static HRESULT
ShellLink_UpdatePath(LPCWSTR sPathRel
, LPCWSTR path
, LPCWSTR sWorkDir
, LPWSTR
* psPath
)
995 if (!path
|| !psPath
)
998 if (!*psPath
&& sPathRel
)
1000 WCHAR buffer
[2*MAX_PATH
], abs_path
[2*MAX_PATH
];
1001 LPWSTR final
= NULL
;
1003 /* first try if [directory of link file] + [relative path] finds an existing file */
1005 GetFullPathNameW(path
, MAX_PATH
* 2, buffer
, &final
);
1008 wcscpy(final
, sPathRel
);
1012 if (SHELL_ExistsFileW(buffer
))
1014 if (!GetFullPathNameW(buffer
, MAX_PATH
, abs_path
, &final
))
1015 wcscpy(abs_path
, buffer
);
1019 /* try if [working directory] + [relative path] finds an existing file */
1022 wcscpy(buffer
, sWorkDir
);
1023 wcscpy(PathAddBackslashW(buffer
), sPathRel
);
1025 if (SHELL_ExistsFileW(buffer
))
1026 if (!GetFullPathNameW(buffer
, MAX_PATH
, abs_path
, &final
))
1027 wcscpy(abs_path
, buffer
);
1031 /* FIXME: This is even not enough - not all shell links can be resolved using this algorithm. */
1033 wcscpy(abs_path
, sPathRel
);
1035 *psPath
= strdupW(abs_path
);
1037 return E_OUTOFMEMORY
;
1043 HRESULT STDMETHODCALLTYPE
CShellLink::GetPath(LPSTR pszFile
, INT cchMaxPath
, WIN32_FIND_DATAA
*pfd
, DWORD fFlags
)
1047 WIN32_FIND_DATAW wfd
;
1049 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%u)(%s)\n",
1050 this, pszFile
, cchMaxPath
, pfd
, fFlags
, debugstr_w(m_sPath
));
1052 /* Allocate a temporary UNICODE buffer */
1053 pszFileW
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, cchMaxPath
* sizeof(WCHAR
));
1055 return E_OUTOFMEMORY
;
1057 /* Call the UNICODE function */
1058 hr
= GetPath(pszFileW
, cchMaxPath
, &wfd
, fFlags
);
1060 /* Convert the file path back to ANSI */
1061 WideCharToMultiByte(CP_ACP
, 0, pszFileW
, -1,
1062 pszFile
, cchMaxPath
, NULL
, NULL
);
1064 /* Free the temporary buffer */
1065 HeapFree(GetProcessHeap(), 0, pszFileW
);
1069 ZeroMemory(pfd
, sizeof(*pfd
));
1071 /* Copy the file data if a file path was returned */
1076 /* Copy the fixed part */
1077 CopyMemory(pfd
, &wfd
, FIELD_OFFSET(WIN32_FIND_DATAA
, cFileName
));
1079 /* Convert the file names to ANSI */
1080 len
= lstrlenW(wfd
.cFileName
);
1081 WideCharToMultiByte(CP_ACP
, 0, wfd
.cFileName
, len
+ 1,
1082 pfd
->cFileName
, sizeof(pfd
->cFileName
), NULL
, NULL
);
1083 len
= lstrlenW(wfd
.cAlternateFileName
);
1084 WideCharToMultiByte(CP_ACP
, 0, wfd
.cAlternateFileName
, len
+ 1,
1085 pfd
->cAlternateFileName
, sizeof(pfd
->cAlternateFileName
), NULL
, NULL
);
1092 HRESULT STDMETHODCALLTYPE
CShellLink::GetIDList(LPITEMIDLIST
*ppidl
)
1094 TRACE("(%p)->(ppidl=%p)\n", this, ppidl
);
1102 *ppidl
= ILClone(m_pPidl
);
1106 HRESULT STDMETHODCALLTYPE
CShellLink::SetIDList(LPCITEMIDLIST pidl
)
1108 TRACE("(%p)->(pidl=%p)\n", this, pidl
);
1109 return SetTargetFromPIDLOrPath(pidl
, NULL
);
1112 HRESULT STDMETHODCALLTYPE
CShellLink::GetDescription(LPSTR pszName
, INT cchMaxName
)
1114 TRACE("(%p)->(%p len=%u)\n", this, pszName
, cchMaxName
);
1120 WideCharToMultiByte(CP_ACP
, 0, m_sDescription
, -1,
1121 pszName
, cchMaxName
, NULL
, NULL
);
1126 HRESULT STDMETHODCALLTYPE
CShellLink::SetDescription(LPCSTR pszName
)
1128 TRACE("(%p)->(pName=%s)\n", this, pszName
);
1130 HeapFree(GetProcessHeap(), 0, m_sDescription
);
1131 m_sDescription
= NULL
;
1135 m_sDescription
= HEAP_strdupAtoW(GetProcessHeap(), 0, pszName
);
1136 if (!m_sDescription
)
1137 return E_OUTOFMEMORY
;
1144 HRESULT STDMETHODCALLTYPE
CShellLink::GetWorkingDirectory(LPSTR pszDir
, INT cchMaxPath
)
1146 TRACE("(%p)->(%p len=%u)\n", this, pszDir
, cchMaxPath
);
1152 WideCharToMultiByte(CP_ACP
, 0, m_sWorkDir
, -1,
1153 pszDir
, cchMaxPath
, NULL
, NULL
);
1158 HRESULT STDMETHODCALLTYPE
CShellLink::SetWorkingDirectory(LPCSTR pszDir
)
1160 TRACE("(%p)->(dir=%s)\n", this, pszDir
);
1162 HeapFree(GetProcessHeap(), 0, m_sWorkDir
);
1167 m_sWorkDir
= HEAP_strdupAtoW(GetProcessHeap(), 0, pszDir
);
1169 return E_OUTOFMEMORY
;
1176 HRESULT STDMETHODCALLTYPE
CShellLink::GetArguments(LPSTR pszArgs
, INT cchMaxPath
)
1178 TRACE("(%p)->(%p len=%u)\n", this, pszArgs
, cchMaxPath
);
1184 WideCharToMultiByte(CP_ACP
, 0, m_sArgs
, -1,
1185 pszArgs
, cchMaxPath
, NULL
, NULL
);
1190 HRESULT STDMETHODCALLTYPE
CShellLink::SetArguments(LPCSTR pszArgs
)
1192 TRACE("(%p)->(args=%s)\n", this, pszArgs
);
1194 HeapFree(GetProcessHeap(), 0, m_sArgs
);
1199 m_sArgs
= HEAP_strdupAtoW(GetProcessHeap(), 0, pszArgs
);
1201 return E_OUTOFMEMORY
;
1209 HRESULT STDMETHODCALLTYPE
CShellLink::GetHotkey(WORD
*pwHotkey
)
1211 TRACE("(%p)->(%p)(0x%08x)\n", this, pwHotkey
, m_Header
.wHotKey
);
1212 *pwHotkey
= m_Header
.wHotKey
;
1216 HRESULT STDMETHODCALLTYPE
CShellLink::SetHotkey(WORD wHotkey
)
1218 TRACE("(%p)->(hotkey=%x)\n", this, wHotkey
);
1220 m_Header
.wHotKey
= wHotkey
;
1226 HRESULT STDMETHODCALLTYPE
CShellLink::GetShowCmd(INT
*piShowCmd
)
1228 TRACE("(%p)->(%p) %d\n", this, piShowCmd
, m_Header
.nShowCommand
);
1229 *piShowCmd
= m_Header
.nShowCommand
;
1233 HRESULT STDMETHODCALLTYPE
CShellLink::SetShowCmd(INT iShowCmd
)
1235 TRACE("(%p) %d\n", this, iShowCmd
);
1237 m_Header
.nShowCommand
= iShowCmd
;
1243 HRESULT STDMETHODCALLTYPE
CShellLink::GetIconLocation(LPSTR pszIconPath
, INT cchIconPath
, INT
*piIcon
)
1246 LPWSTR pszIconPathW
;
1248 TRACE("(%p)->(%p len=%u iicon=%p)\n", this, pszIconPath
, cchIconPath
, piIcon
);
1250 /* Allocate a temporary UNICODE buffer */
1251 pszIconPathW
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, cchIconPath
* sizeof(WCHAR
));
1253 return E_OUTOFMEMORY
;
1255 /* Call the UNICODE function */
1256 hr
= GetIconLocation(pszIconPathW
, cchIconPath
, piIcon
);
1258 /* Convert the file path back to ANSI */
1259 WideCharToMultiByte(CP_ACP
, 0, pszIconPathW
, -1,
1260 pszIconPath
, cchIconPath
, NULL
, NULL
);
1262 /* Free the temporary buffer */
1263 HeapFree(GetProcessHeap(), 0, pszIconPathW
);
1268 HRESULT STDMETHODCALLTYPE
CShellLink::GetIconLocation(UINT uFlags
, PSTR pszIconFile
, UINT cchMax
, int *piIndex
, UINT
*pwFlags
)
1271 LPWSTR pszIconFileW
;
1273 TRACE("(%p)->(%u %p len=%u piIndex=%p pwFlags=%p)\n", this, uFlags
, pszIconFile
, cchMax
, piIndex
, pwFlags
);
1275 /* Allocate a temporary UNICODE buffer */
1276 pszIconFileW
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, cchMax
* sizeof(WCHAR
));
1278 return E_OUTOFMEMORY
;
1280 /* Call the UNICODE function */
1281 hr
= GetIconLocation(uFlags
, pszIconFileW
, cchMax
, piIndex
, pwFlags
);
1283 /* Convert the file path back to ANSI */
1284 WideCharToMultiByte(CP_ACP
, 0, pszIconFileW
, -1,
1285 pszIconFile
, cchMax
, NULL
, NULL
);
1287 /* Free the temporary buffer */
1288 HeapFree(GetProcessHeap(), 0, pszIconFileW
);
1293 HRESULT STDMETHODCALLTYPE
CShellLink::Extract(PCSTR pszFile
, UINT nIconIndex
, HICON
*phiconLarge
, HICON
*phiconSmall
, UINT nIconSize
)
1295 TRACE("(%p)->(path=%s iicon=%u)\n", this, pszFile
, nIconIndex
);
1300 str
= HEAP_strdupAtoW(GetProcessHeap(), 0, pszFile
);
1302 return E_OUTOFMEMORY
;
1305 HRESULT hr
= Extract(str
, nIconIndex
, phiconLarge
, phiconSmall
, nIconSize
);
1308 HeapFree(GetProcessHeap(), 0, str
);
1313 HRESULT STDMETHODCALLTYPE
CShellLink::SetIconLocation(LPCSTR pszIconPath
, INT iIcon
)
1315 TRACE("(%p)->(path=%s iicon=%u)\n", this, pszIconPath
, iIcon
);
1320 str
= HEAP_strdupAtoW(GetProcessHeap(), 0, pszIconPath
);
1322 return E_OUTOFMEMORY
;
1325 HRESULT hr
= SetIconLocation(str
, iIcon
);
1328 HeapFree(GetProcessHeap(), 0, str
);
1333 HRESULT STDMETHODCALLTYPE
CShellLink::SetRelativePath(LPCSTR pszPathRel
, DWORD dwReserved
)
1335 TRACE("(%p)->(path=%s %x)\n", this, pszPathRel
, dwReserved
);
1337 HeapFree(GetProcessHeap(), 0, m_sPathRel
);
1342 m_sPathRel
= HEAP_strdupAtoW(GetProcessHeap(), 0, pszPathRel
);
1346 return ShellLink_UpdatePath(m_sPathRel
, m_sPath
, m_sWorkDir
, &m_sPath
);
1350 shelllink_get_msi_component_path(LPWSTR component
)
1352 DWORD Result
, sz
= 0;
1354 Result
= CommandLineFromMsiDescriptor(component
, NULL
, &sz
);
1355 if (Result
!= ERROR_SUCCESS
)
1359 LPWSTR path
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, sz
* sizeof(WCHAR
));
1360 Result
= CommandLineFromMsiDescriptor(component
, path
, &sz
);
1361 if (Result
!= ERROR_SUCCESS
)
1363 HeapFree(GetProcessHeap(), 0, path
);
1367 TRACE("returning %s\n", debugstr_w(path
));
1372 HRESULT STDMETHODCALLTYPE
CShellLink::Resolve(HWND hwnd
, DWORD fFlags
)
1377 TRACE("(%p)->(hwnd=%p flags=%x)\n", this, hwnd
, fFlags
);
1379 /* FIXME: use IResolveShellLink interface? */
1381 // FIXME: See InvokeCommand().
1383 #if (NTDDI_VERSION < NTDDI_LONGHORN)
1384 // NOTE: For Logo3 (EXP_LOGO3_ID_SIG), check also for SHRestricted(REST_NOLOGO3CHANNELNOTIFY)
1385 if (m_Header
.dwFlags
& SLDF_HAS_LOGO3ID
)
1387 FIXME("Logo3 links are not supported yet!\n");
1392 /* Resolve Darwin (MSI) target */
1393 if (m_Header
.dwFlags
& SLDF_HAS_DARWINID
)
1395 LPWSTR component
= NULL
;
1396 hr
= GetAdvertiseInfo(&component
, EXP_DARWIN_ID_SIG
);
1400 /* Clear the cached path */
1401 HeapFree(GetProcessHeap(), 0, m_sPath
);
1403 m_sPath
= shelllink_get_msi_component_path(component
);
1408 if (!m_sPath
&& m_pPidl
)
1410 WCHAR buffer
[MAX_PATH
];
1412 bSuccess
= SHGetPathFromIDListW(m_pPidl
, buffer
);
1413 if (bSuccess
&& *buffer
)
1415 m_sPath
= strdupW(buffer
);
1417 return E_OUTOFMEMORY
;
1423 hr
= S_OK
; /* don't report an error occurred while just caching information */
1427 // FIXME: Strange to do that here...
1428 if (!m_sIcoPath
&& m_sPath
)
1430 m_sIcoPath
= strdupW(m_sPath
);
1432 return E_OUTOFMEMORY
;
1434 m_Header
.nIconIndex
= 0;
1442 HRESULT STDMETHODCALLTYPE
CShellLink::SetPath(LPCSTR pszFile
)
1444 TRACE("(%p)->(path=%s)\n", this, pszFile
);
1447 return E_INVALIDARG
;
1449 LPWSTR str
= HEAP_strdupAtoW(GetProcessHeap(), 0, pszFile
);
1451 return E_OUTOFMEMORY
;
1453 HRESULT hr
= SetPath(str
);
1454 HeapFree(GetProcessHeap(), 0, str
);
1459 HRESULT STDMETHODCALLTYPE
CShellLink::GetPath(LPWSTR pszFile
, INT cchMaxPath
, WIN32_FIND_DATAW
*pfd
, DWORD fFlags
)
1461 WCHAR buffer
[MAX_PATH
];
1463 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%u)(%s)\n",
1464 this, pszFile
, cchMaxPath
, pfd
, fFlags
, debugstr_w(m_sPath
));
1468 // FIXME: What if cchMaxPath == 0 , or pszFile == NULL ??
1470 // FIXME: What about Darwin??
1473 * Retrieve the path to the target from the PIDL (if we have one).
1474 * NOTE: Do NOT use the cached path (m_sPath from link info).
1476 if (m_pPidl
&& SHGetPathFromIDListW(m_pPidl
, buffer
))
1478 if (fFlags
& SLGP_SHORTPATH
)
1479 GetShortPathNameW(buffer
, buffer
, _countof(buffer
));
1480 // FIXME: Add support for SLGP_UNCPRIORITY
1487 /* If we have a FindData structure, initialize it */
1490 ZeroMemory(pfd
, sizeof(*pfd
));
1492 /* Copy the file data if the target is a file path */
1495 pfd
->dwFileAttributes
= m_Header
.dwFileAttributes
;
1496 pfd
->ftCreationTime
= m_Header
.ftCreationTime
;
1497 pfd
->ftLastAccessTime
= m_Header
.ftLastAccessTime
;
1498 pfd
->ftLastWriteTime
= m_Header
.ftLastWriteTime
;
1499 pfd
->nFileSizeHigh
= 0;
1500 pfd
->nFileSizeLow
= m_Header
.nFileSizeLow
;
1503 * Build temporarily a short path in pfd->cFileName (of size MAX_PATH),
1504 * then extract and store the short file name in pfd->cAlternateFileName.
1506 GetShortPathNameW(buffer
, pfd
->cFileName
, _countof(pfd
->cFileName
));
1507 lstrcpynW(pfd
->cAlternateFileName
,
1508 PathFindFileNameW(pfd
->cFileName
),
1509 _countof(pfd
->cAlternateFileName
));
1511 /* Now extract and store the long file name in pfd->cFileName */
1512 lstrcpynW(pfd
->cFileName
,
1513 PathFindFileNameW(buffer
),
1514 _countof(pfd
->cFileName
));
1518 /* Finally check if we have a raw path the user actually wants to retrieve */
1519 if ((fFlags
& SLGP_RAWPATH
) && (m_Header
.dwFlags
& SLDF_HAS_EXP_SZ
))
1521 /* Search for a target environment block */
1522 LPEXP_SZ_LINK pInfo
;
1523 pInfo
= (LPEXP_SZ_LINK
)SHFindDataBlock(m_pDBList
, EXP_SZ_LINK_SIG
);
1524 if (pInfo
&& (pInfo
->cbSize
== sizeof(*pInfo
)))
1525 lstrcpynW(buffer
, pInfo
->szwTarget
, cchMaxPath
);
1528 /* For diagnostics purposes only... */
1529 // NOTE: SLGP_UNCPRIORITY is unsupported
1530 fFlags
&= ~(SLGP_RAWPATH
| SLGP_SHORTPATH
);
1531 if (fFlags
) FIXME("(%p): Unsupported flags %lu\n", this, fFlags
);
1533 /* Copy the data back to the user */
1535 lstrcpynW(pszFile
, buffer
, cchMaxPath
);
1537 return (*buffer
? S_OK
: S_FALSE
);
1540 HRESULT STDMETHODCALLTYPE
CShellLink::GetDescription(LPWSTR pszName
, INT cchMaxName
)
1542 TRACE("(%p)->(%p len=%u)\n", this, pszName
, cchMaxName
);
1546 lstrcpynW(pszName
, m_sDescription
, cchMaxName
);
1551 HRESULT STDMETHODCALLTYPE
CShellLink::SetDescription(LPCWSTR pszName
)
1553 TRACE("(%p)->(desc=%s)\n", this, debugstr_w(pszName
));
1555 HeapFree(GetProcessHeap(), 0, m_sDescription
);
1558 m_sDescription
= strdupW(pszName
);
1559 if (!m_sDescription
)
1560 return E_OUTOFMEMORY
;
1563 m_sDescription
= NULL
;
1570 HRESULT STDMETHODCALLTYPE
CShellLink::GetWorkingDirectory(LPWSTR pszDir
, INT cchMaxPath
)
1572 TRACE("(%p)->(%p len %u)\n", this, pszDir
, cchMaxPath
);
1578 lstrcpynW(pszDir
, m_sWorkDir
, cchMaxPath
);
1583 HRESULT STDMETHODCALLTYPE
CShellLink::SetWorkingDirectory(LPCWSTR pszDir
)
1585 TRACE("(%p)->(dir=%s)\n", this, debugstr_w(pszDir
));
1587 HeapFree(GetProcessHeap(), 0, m_sWorkDir
);
1590 m_sWorkDir
= strdupW(pszDir
);
1592 return E_OUTOFMEMORY
;
1602 HRESULT STDMETHODCALLTYPE
CShellLink::GetArguments(LPWSTR pszArgs
, INT cchMaxPath
)
1604 TRACE("(%p)->(%p len=%u)\n", this, pszArgs
, cchMaxPath
);
1610 lstrcpynW(pszArgs
, m_sArgs
, cchMaxPath
);
1615 HRESULT STDMETHODCALLTYPE
CShellLink::SetArguments(LPCWSTR pszArgs
)
1617 TRACE("(%p)->(args=%s)\n", this, debugstr_w(pszArgs
));
1619 HeapFree(GetProcessHeap(), 0, m_sArgs
);
1622 m_sArgs
= strdupW(pszArgs
);
1624 return E_OUTOFMEMORY
;
1634 HRESULT STDMETHODCALLTYPE
CShellLink::GetIconLocation(LPWSTR pszIconPath
, INT cchIconPath
, INT
*piIcon
)
1636 TRACE("(%p)->(%p len=%u iicon=%p)\n", this, pszIconPath
, cchIconPath
, piIcon
);
1643 /* Update the original icon path location */
1644 if (m_Header
.dwFlags
& SLDF_HAS_EXP_ICON_SZ
)
1646 WCHAR szPath
[MAX_PATH
];
1648 /* Search for an icon environment block */
1649 LPEXP_SZ_LINK pInfo
;
1650 pInfo
= (LPEXP_SZ_LINK
)SHFindDataBlock(m_pDBList
, EXP_SZ_ICON_SIG
);
1651 if (pInfo
&& (pInfo
->cbSize
== sizeof(*pInfo
)))
1653 m_Header
.dwFlags
&= ~SLDF_HAS_ICONLOCATION
;
1654 HeapFree(GetProcessHeap(), 0, m_sIcoPath
);
1657 SHExpandEnvironmentStringsW(pInfo
->szwTarget
, szPath
, _countof(szPath
));
1659 m_sIcoPath
= strdupW(szPath
);
1661 return E_OUTOFMEMORY
;
1663 m_Header
.dwFlags
|= SLDF_HAS_ICONLOCATION
;
1669 *piIcon
= m_Header
.nIconIndex
;
1672 lstrcpynW(pszIconPath
, m_sIcoPath
, cchIconPath
);
1677 static HRESULT
SHELL_PidlGetIconLocationW(PCIDLIST_ABSOLUTE pidl
,
1678 UINT uFlags
, PWSTR pszIconFile
, UINT cchMax
, int *piIndex
, UINT
*pwFlags
)
1680 LPCITEMIDLIST pidlLast
;
1681 CComPtr
<IShellFolder
> psf
;
1683 HRESULT hr
= SHBindToParent(pidl
, IID_PPV_ARG(IShellFolder
, &psf
), &pidlLast
);
1684 if (FAILED_UNEXPECTEDLY(hr
))
1687 CComPtr
<IExtractIconW
> pei
;
1688 hr
= psf
->GetUIObjectOf(0, 1, &pidlLast
, IID_NULL_PPV_ARG(IExtractIconW
, &pei
));
1689 if (FAILED_UNEXPECTEDLY(hr
))
1692 hr
= pei
->GetIconLocation(uFlags
, pszIconFile
, cchMax
, piIndex
, pwFlags
);
1693 if (FAILED_UNEXPECTEDLY(hr
))
1699 HRESULT STDMETHODCALLTYPE
CShellLink::GetIconLocation(UINT uFlags
, PWSTR pszIconFile
, UINT cchMax
, int *piIndex
, UINT
*pwFlags
)
1703 pszIconFile
[0] = UNICODE_NULL
;
1706 * It is possible for a shell link to point to another shell link,
1707 * and in particular there is the possibility to point to itself.
1708 * Now, suppose we ask such a link to retrieve its associated icon.
1709 * This function would be called, and due to COM would be called again
1710 * recursively. To solve this issue, we forbid calling GetIconLocation()
1711 * with GIL_FORSHORTCUT set in uFlags, as done by Windows (shown by tests).
1713 if (uFlags
& GIL_FORSHORTCUT
)
1714 return E_INVALIDARG
;
1717 * Now, we set GIL_FORSHORTCUT so that: i) we allow the icon extractor
1718 * of the target to give us a suited icon, and ii) we protect ourselves
1719 * against recursive call.
1721 uFlags
|= GIL_FORSHORTCUT
;
1723 if (uFlags
& GIL_DEFAULTICON
)
1726 hr
= GetIconLocation(pszIconFile
, cchMax
, piIndex
);
1727 if (FAILED(hr
) || pszIconFile
[0] == UNICODE_NULL
)
1729 hr
= SHELL_PidlGetIconLocationW(m_pPidl
, uFlags
, pszIconFile
, cchMax
, piIndex
, pwFlags
);
1733 *pwFlags
= GIL_NOTFILENAME
| GIL_PERCLASS
;
1739 HRESULT STDMETHODCALLTYPE
1740 CShellLink::Extract(PCWSTR pszFile
, UINT nIconIndex
, HICON
*phiconLarge
, HICON
*phiconSmall
, UINT nIconSize
)
1742 HRESULT hr
= NOERROR
;
1743 UINT cxyLarge
= LOWORD(nIconSize
), cxySmall
= HIWORD(nIconSize
);
1747 *phiconLarge
= NULL
;
1748 PrivateExtractIconsW(pszFile
, nIconIndex
, cxyLarge
, cxyLarge
, phiconLarge
, NULL
, 1, 0);
1750 if (*phiconLarge
== NULL
)
1756 *phiconSmall
= NULL
;
1757 PrivateExtractIconsW(pszFile
, nIconIndex
, cxySmall
, cxySmall
, phiconSmall
, NULL
, 1, 0);
1759 if (*phiconSmall
== NULL
)
1765 if (phiconLarge
&& *phiconLarge
)
1767 DestroyIcon(*phiconLarge
);
1768 *phiconLarge
= NULL
;
1770 if (phiconSmall
&& *phiconSmall
)
1772 DestroyIcon(*phiconSmall
);
1773 *phiconSmall
= NULL
;
1781 /* Extends the functionality of PathUnExpandEnvStringsW */
1782 BOOL
PathFullyUnExpandEnvStringsW(
1783 _In_ LPCWSTR pszPath
,
1784 _Out_ LPWSTR pszBuf
,
1787 BOOL Ret
= FALSE
; // Set to TRUE as soon as PathUnExpandEnvStrings starts unexpanding.
1792 while (*pszPath
&& cchBuf
> 0)
1794 /* Attempt unexpanding the path */
1795 res
= PathUnExpandEnvStringsW(pszPath
, pszBuf
, cchBuf
);
1798 /* The unexpansion failed. Try to find a path delimiter. */
1799 p
= wcspbrk(pszPath
, L
" /\\:*?\"<>|%");
1800 if (!p
) /* None found, we will copy the remaining path */
1801 p
= pszPath
+ wcslen(pszPath
);
1802 else /* Found one, we will copy the delimiter and skip it */
1804 /* If we overflow, we cannot unexpand more, so return FALSE */
1805 if (p
- pszPath
>= cchBuf
)
1806 return FALSE
; // *pszBuf = L'\0';
1808 /* Copy the untouched portion of path up to the delimiter, included */
1809 wcsncpy(pszBuf
, pszPath
, p
- pszPath
);
1810 pszBuf
[p
- pszPath
] = L
'\0'; // NULL-terminate
1812 /* Advance the pointers and decrease the remaining buffer size */
1813 cchBuf
-= (p
- pszPath
);
1814 pszBuf
+= (p
- pszPath
);
1815 pszPath
+= (p
- pszPath
);
1820 * The unexpansion succeeded. Skip the unexpanded part by trying
1821 * to find where the original path and the unexpanded string
1823 * NOTE: An alternative(?) would be to stop also at the last
1824 * path delimiter encountered in the loop (i.e. would be the
1825 * first path delimiter in the strings).
1830 * The algorithm starts at the end of the strings and loops back
1831 * while the characters are equal, until it finds a discrepancy.
1833 p
= pszPath
+ wcslen(pszPath
);
1834 q
= pszBuf
+ wcslen(pszBuf
); // This wcslen should be < cchBuf
1835 while ((*p
== *q
) && (p
> pszPath
) && (q
> pszBuf
))
1839 /* Skip discrepancy */
1842 /* Advance the pointers and decrease the remaining buffer size */
1843 cchBuf
-= (q
- pszBuf
);
1855 HRESULT STDMETHODCALLTYPE
CShellLink::SetIconLocation(LPCWSTR pszIconPath
, INT iIcon
)
1857 HRESULT hr
= E_FAIL
;
1858 WCHAR szUnExpIconPath
[MAX_PATH
];
1860 TRACE("(%p)->(path=%s iicon=%u)\n", this, debugstr_w(pszIconPath
), iIcon
);
1864 /* Try to fully unexpand the icon path */
1867 * Check whether the user-given file path contains unexpanded
1868 * environment variables. If so, create a target environment block.
1869 * Note that in this block we will store the user-given path.
1870 * It will contain the unexpanded environment variables, but
1871 * it can also contain already expanded path that the user does
1872 * not want to see them unexpanded (e.g. so that they always
1873 * refer to the same place even if the would-be corresponding
1874 * environment variable could change).
1876 // FIXME: http://stackoverflow.com/questions/2976489/ishelllinkseticonlocation-translates-my-icon-path-into-program-files-which-i
1877 // if (PathFullyUnExpandEnvStringsW(pszIconPath, szUnExpIconPath, _countof(szUnExpIconPath)))
1878 PathUnExpandEnvStringsW(pszIconPath
, szUnExpIconPath
, _countof(szUnExpIconPath
));
1879 if (wcscmp(pszIconPath
, szUnExpIconPath
) != 0)
1881 /* Unexpansion succeeded, so we need an icon environment block */
1883 LPEXP_SZ_LINK pInfo
;
1885 pszIconPath
= szUnExpIconPath
;
1886 pInfo
= (LPEXP_SZ_LINK
)SHFindDataBlock(m_pDBList
, EXP_SZ_ICON_SIG
);
1889 /* Make sure that the size of the structure is valid */
1890 if (pInfo
->cbSize
!= sizeof(*pInfo
))
1892 ERR("Ooops. This structure is not as expected...\n");
1894 /* Invalid structure, remove it altogether */
1895 m_Header
.dwFlags
&= ~SLDF_HAS_EXP_ICON_SZ
;
1896 RemoveDataBlock(EXP_SZ_ICON_SIG
);
1898 /* Reset the pointer and go use the static buffer */
1904 /* Use the static buffer */
1906 buffer
.cbSize
= sizeof(buffer
);
1907 buffer
.dwSignature
= EXP_SZ_ICON_SIG
;
1910 lstrcpynW(pInfo
->szwTarget
, szUnExpIconPath
, _countof(pInfo
->szwTarget
));
1911 WideCharToMultiByte(CP_ACP
, 0, szUnExpIconPath
, -1,
1912 pInfo
->szTarget
, _countof(pInfo
->szTarget
), NULL
, NULL
);
1915 if (pInfo
== &buffer
)
1916 hr
= AddDataBlock(pInfo
);
1918 m_Header
.dwFlags
|= SLDF_HAS_EXP_ICON_SZ
;
1922 /* Unexpansion failed, so we need to remove any icon environment block */
1923 m_Header
.dwFlags
&= ~SLDF_HAS_EXP_ICON_SZ
;
1924 RemoveDataBlock(EXP_SZ_ICON_SIG
);
1928 /* Store the original icon path location (this one may contain unexpanded environment strings) */
1931 m_Header
.dwFlags
&= ~SLDF_HAS_ICONLOCATION
;
1932 HeapFree(GetProcessHeap(), 0, m_sIcoPath
);
1935 m_sIcoPath
= strdupW(pszIconPath
);
1937 return E_OUTOFMEMORY
;
1939 m_Header
.dwFlags
|= SLDF_HAS_ICONLOCATION
;
1944 m_Header
.nIconIndex
= iIcon
;
1950 HRESULT STDMETHODCALLTYPE
CShellLink::SetRelativePath(LPCWSTR pszPathRel
, DWORD dwReserved
)
1952 TRACE("(%p)->(path=%s %x)\n", this, debugstr_w(pszPathRel
), dwReserved
);
1954 HeapFree(GetProcessHeap(), 0, m_sPathRel
);
1957 m_sPathRel
= strdupW(pszPathRel
);
1959 return E_OUTOFMEMORY
;
1966 return ShellLink_UpdatePath(m_sPathRel
, m_sPath
, m_sWorkDir
, &m_sPath
);
1969 static LPWSTR
GetAdvertisedArg(LPCWSTR str
)
1974 LPCWSTR p
= wcschr(str
, L
':');
1978 DWORD len
= p
- str
;
1979 LPWSTR ret
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR
) * (len
+ 1));
1983 memcpy(ret
, str
, sizeof(WCHAR
)*len
);
1988 HRESULT
CShellLink::WriteAdvertiseInfo(LPCWSTR string
, DWORD dwSig
)
1990 EXP_DARWIN_LINK buffer
;
1991 LPEXP_DARWIN_LINK pInfo
;
1993 if ( (dwSig
!= EXP_DARWIN_ID_SIG
)
1994 #if (NTDDI_VERSION < NTDDI_LONGHORN)
1995 && (dwSig
!= EXP_LOGO3_ID_SIG
)
1999 return E_INVALIDARG
;
2005 pInfo
= (LPEXP_DARWIN_LINK
)SHFindDataBlock(m_pDBList
, dwSig
);
2008 /* Make sure that the size of the structure is valid */
2009 if (pInfo
->dbh
.cbSize
!= sizeof(*pInfo
))
2011 ERR("Ooops. This structure is not as expected...\n");
2013 /* Invalid structure, remove it altogether */
2014 if (dwSig
== EXP_DARWIN_ID_SIG
)
2015 m_Header
.dwFlags
&= ~SLDF_HAS_DARWINID
;
2016 #if (NTDDI_VERSION < NTDDI_LONGHORN)
2017 else if (dwSig
== EXP_LOGO3_ID_SIG
)
2018 m_Header
.dwFlags
&= ~SLDF_HAS_LOGO3ID
;
2020 RemoveDataBlock(dwSig
);
2022 /* Reset the pointer and go use the static buffer */
2028 /* Use the static buffer */
2030 buffer
.dbh
.cbSize
= sizeof(buffer
);
2031 buffer
.dbh
.dwSignature
= dwSig
;
2034 lstrcpynW(pInfo
->szwDarwinID
, string
, _countof(pInfo
->szwDarwinID
));
2035 WideCharToMultiByte(CP_ACP
, 0, string
, -1,
2036 pInfo
->szDarwinID
, _countof(pInfo
->szDarwinID
), NULL
, NULL
);
2039 if (pInfo
== &buffer
)
2040 hr
= AddDataBlock(pInfo
);
2043 if (dwSig
== EXP_DARWIN_ID_SIG
)
2044 m_Header
.dwFlags
|= SLDF_HAS_DARWINID
;
2045 #if (NTDDI_VERSION < NTDDI_LONGHORN)
2046 else if (dwSig
== EXP_LOGO3_ID_SIG
)
2047 m_Header
.dwFlags
|= SLDF_HAS_LOGO3ID
;
2054 HRESULT
CShellLink::SetAdvertiseInfo(LPCWSTR str
)
2057 LPCWSTR szComponent
= NULL
, szProduct
= NULL
, p
;
2062 /**/sProduct
= sComponent
= NULL
;/**/
2066 /* each segment must start with two colons */
2067 if (str
[0] != ':' || str
[1] != ':')
2070 /* the last segment is just two colons */
2075 /* there must be a colon straight after a guid */
2076 p
= wcschr(str
, L
':');
2083 /* get the guid, and check if it's validly formatted */
2084 memcpy(szGuid
, str
, sizeof(WCHAR
)*len
);
2087 hr
= CLSIDFromString(szGuid
, &guid
);
2092 /* match it up to a guid that we care about */
2093 if (IsEqualGUID(guid
, SHELL32_AdvtShortcutComponent
) && !szComponent
)
2094 szComponent
= str
; /* Darwin */
2095 else if (IsEqualGUID(guid
, SHELL32_AdvtShortcutProduct
) && !szProduct
)
2096 szProduct
= str
; /* Logo3 */
2100 /* skip to the next field */
2101 str
= wcschr(str
, L
':');
2106 /* we have to have a component for an advertised shortcut */
2110 szComponent
= GetAdvertisedArg(szComponent
);
2111 szProduct
= GetAdvertisedArg(szProduct
);
2113 hr
= WriteAdvertiseInfo(szComponent
, EXP_DARWIN_ID_SIG
);
2116 #if (NTDDI_VERSION < NTDDI_LONGHORN)
2117 hr
= WriteAdvertiseInfo(szProduct
, EXP_LOGO3_ID_SIG
);
2122 HeapFree(GetProcessHeap(), 0, (PVOID
)szComponent
);
2123 HeapFree(GetProcessHeap(), 0, (PVOID
)szProduct
);
2125 if (TRACE_ON(shell
))
2127 GetAdvertiseInfo(&sComponent
, EXP_DARWIN_ID_SIG
);
2128 TRACE("Component = %s\n", debugstr_w(sComponent
));
2129 #if (NTDDI_VERSION < NTDDI_LONGHORN)
2130 GetAdvertiseInfo(&sProduct
, EXP_LOGO3_ID_SIG
);
2131 TRACE("Product = %s\n", debugstr_w(sProduct
));
2139 * Since the real PathResolve (from Wine) is unimplemented at the moment,
2140 * we use this local implementation, until a better one is written (using
2141 * code parts of the SHELL_xxx helpers in Wine's shellpath.c).
2143 static BOOL
HACKISH_PathResolve(
2144 IN OUT PWSTR pszPath
,
2145 IN PZPCWSTR dirs OPTIONAL
,
2148 // FIXME: This is unimplemented!!!
2150 return PathResolve(pszPath
, dirs
, fFlags
);
2152 BOOL Success
= FALSE
;
2154 LPWSTR fname
= NULL
;
2155 WCHAR szPath
[MAX_PATH
];
2157 /* First, search for a valid existing path */
2159 // NOTE: See also: SHELL_FindExecutable()
2162 * List of extensions searched for, by PathResolve with the flag
2163 * PRF_TRYPROGRAMEXTENSIONS == PRF_EXECUTABLE | PRF_VERIFYEXISTS set,
2164 * according to MSDN: https://msdn.microsoft.com/en-us/library/windows/desktop/bb776478(v=vs.85).aspx
2166 static PCWSTR Extensions
[] = {L
".pif", L
".com", L
".bat", L
".cmd", L
".lnk", L
".exe", NULL
};
2167 #define LNK_EXT_INDEX 4 // ".lnk" has index 4 in the array above
2170 * Start at the beginning of the list if PRF_EXECUTABLE is set, otherwise
2171 * just use the last element 'NULL' (no extension checking).
2173 i
= ((fFlags
& PRF_EXECUTABLE
) ? 0 : _countof(Extensions
) - 1);
2174 for (; i
< _countof(Extensions
); ++i
)
2176 /* Ignore shell links ".lnk" if needed */
2177 if ((fFlags
& PRF_DONTFINDLNK
) && (i
== LNK_EXT_INDEX
))
2180 Success
= (SearchPathW(NULL
, pszPath
, Extensions
[i
],
2181 _countof(szPath
), szPath
, NULL
) != 0);
2184 ERR("SearchPathW(pszPath = '%S') failed\n", pszPath
);
2188 ERR("SearchPathW(pszPath = '%S', szPath = '%S') succeeded\n", pszPath
, szPath
);
2195 ERR("SearchPathW(pszPath = '%S') failed\n", pszPath
);
2197 /* We failed, try with PathFindOnPath, as explained by MSDN */
2198 // Success = PathFindOnPathW(pszPath, dirs);
2199 StringCchCopyW(szPath
, _countof(szPath
), pszPath
);
2200 Success
= PathFindOnPathW(szPath
, dirs
);
2203 ERR("PathFindOnPathW(pszPath = '%S') failed\n", pszPath
);
2205 /* We failed again, fall back to building a possible non-existing path */
2206 if (!GetFullPathNameW(pszPath
, _countof(szPath
), szPath
, &fname
))
2208 ERR("GetFullPathNameW(pszPath = '%S') failed\n", pszPath
);
2212 Success
= PathFileExistsW(szPath
);
2214 ERR("PathFileExistsW(szPath = '%S') failed\n", szPath
);
2216 /******************************************************/
2217 /* Question: Why this line is needed only for files?? */
2218 if (fname
&& (_wcsicmp(pszPath
, fname
) == 0))
2220 /******************************************************/
2224 ERR("PathFindOnPathW(pszPath = '%S' ==> '%S') succeeded\n", pszPath
, szPath
);
2228 /* Copy back the results to the caller */
2229 StringCchCopyW(pszPath
, MAX_PATH
, szPath
);
2232 * Since the called functions always checked whether the file path existed,
2233 * we do not need to redo a final check: we can use instead the cached
2234 * result in 'Success'.
2236 return ((fFlags
& PRF_VERIFYEXISTS
) ? Success
: TRUE
);
2240 HRESULT
CShellLink::SetTargetFromPIDLOrPath(LPCITEMIDLIST pidl
, LPCWSTR pszFile
)
2243 LPITEMIDLIST pidlNew
= NULL
;
2244 WCHAR szPath
[MAX_PATH
];
2247 * Not both 'pidl' and 'pszFile' should be set.
2248 * But either one or both can be NULL.
2250 if (pidl
&& pszFile
)
2255 /* Clone the PIDL */
2256 pidlNew
= ILClone(pidl
);
2262 /* Build a PIDL for this path target */
2263 hr
= SHILCreateFromPathW(pszFile
, &pidlNew
, NULL
);
2266 /* This failed, try to resolve the path, then create a simple PIDL */
2268 StringCchCopyW(szPath
, _countof(szPath
), pszFile
);
2269 // FIXME: Because PathResolve is unimplemented, we use our hackish implementation!
2270 HACKISH_PathResolve(szPath
, NULL
, PRF_TRYPROGRAMEXTENSIONS
);
2272 pidlNew
= SHSimpleIDListFromPathW(szPath
);
2273 /******************************************************/
2274 /* Question: Why this line is needed only for files?? */
2275 hr
= (*szPath
? S_OK
: E_INVALIDARG
); // S_FALSE
2276 /******************************************************/
2279 // else if (!pidl && !pszFile) { pidlNew = NULL; hr = S_OK; }
2286 if (SHGetPathFromIDListW(pidlNew
, szPath
))
2290 // TODO: Fully update link info, tracker, file attribs...
2299 /* Update the cached path (for link info) */
2300 ShellLink_GetVolumeInfo(pszFile
, &volume
);
2303 HeapFree(GetProcessHeap(), 0, m_sPath
);
2305 m_sPath
= strdupW(pszFile
);
2307 return E_OUTOFMEMORY
;
2313 HRESULT STDMETHODCALLTYPE
CShellLink::SetPath(LPCWSTR pszFile
)
2315 LPWSTR unquoted
= NULL
;
2318 TRACE("(%p)->(path=%s)\n", this, debugstr_w(pszFile
));
2321 return E_INVALIDARG
;
2324 * Allow upgrading Logo3 shortcuts (m_Header.dwFlags & SLDF_HAS_LOGO3ID),
2325 * but forbid upgrading Darwin ones.
2327 if (m_Header
.dwFlags
& SLDF_HAS_DARWINID
)
2330 /* quotes at the ends of the string are stripped */
2331 SIZE_T len
= wcslen(pszFile
);
2332 if (pszFile
[0] == L
'"' && pszFile
[len
-1] == L
'"')
2334 unquoted
= strdupW(pszFile
);
2335 PathUnquoteSpacesW(unquoted
);
2339 /* any other quote marks are invalid */
2340 if (wcschr(pszFile
, L
'"'))
2346 /* Clear the cached path */
2347 HeapFree(GetProcessHeap(), 0, m_sPath
);
2350 /* Check for an advertised target (Logo3 or Darwin) */
2351 if (SetAdvertiseInfo(pszFile
) != S_OK
)
2353 /* This is not an advertised target, but a regular path */
2354 WCHAR szPath
[MAX_PATH
];
2357 * Check whether the user-given file path contains unexpanded
2358 * environment variables. If so, create a target environment block.
2359 * Note that in this block we will store the user-given path.
2360 * It will contain the unexpanded environment variables, but
2361 * it can also contain already expanded path that the user does
2362 * not want to see them unexpanded (e.g. so that they always
2363 * refer to the same place even if the would-be corresponding
2364 * environment variable could change).
2367 SHExpandEnvironmentStringsW(pszFile
, szPath
, _countof(szPath
));
2371 if (*pszFile
&& (wcscmp(pszFile
, szPath
) != 0))
2374 * The user-given file path contains unexpanded environment
2375 * variables, so we need a target environment block.
2378 LPEXP_SZ_LINK pInfo
;
2380 pInfo
= (LPEXP_SZ_LINK
)SHFindDataBlock(m_pDBList
, EXP_SZ_LINK_SIG
);
2383 /* Make sure that the size of the structure is valid */
2384 if (pInfo
->cbSize
!= sizeof(*pInfo
))
2386 ERR("Ooops. This structure is not as expected...\n");
2388 /* Invalid structure, remove it altogether */
2389 m_Header
.dwFlags
&= ~SLDF_HAS_EXP_SZ
;
2390 RemoveDataBlock(EXP_SZ_LINK_SIG
);
2392 /* Reset the pointer and go use the static buffer */
2398 /* Use the static buffer */
2400 buffer
.cbSize
= sizeof(buffer
);
2401 buffer
.dwSignature
= EXP_SZ_LINK_SIG
;
2404 lstrcpynW(pInfo
->szwTarget
, pszFile
, _countof(pInfo
->szwTarget
));
2405 WideCharToMultiByte(CP_ACP
, 0, pszFile
, -1,
2406 pInfo
->szTarget
, _countof(pInfo
->szTarget
), NULL
, NULL
);
2409 if (pInfo
== &buffer
)
2410 hr
= AddDataBlock(pInfo
);
2412 m_Header
.dwFlags
|= SLDF_HAS_EXP_SZ
;
2414 /* Now, make pszFile point to the expanded buffer */
2420 * The user-given file path does not contain unexpanded environment
2421 * variables, so we need to remove any target environment block.
2423 m_Header
.dwFlags
&= ~SLDF_HAS_EXP_SZ
;
2424 RemoveDataBlock(EXP_SZ_LINK_SIG
);
2426 /* pszFile points to the user path */
2429 /* Set the target */
2430 hr
= SetTargetFromPIDLOrPath(NULL
, pszFile
);
2436 HeapFree(GetProcessHeap(), 0, unquoted
);
2440 HRESULT STDMETHODCALLTYPE
CShellLink::AddDataBlock(void* pDataBlock
)
2442 if (SHAddDataBlock(&m_pDBList
, (DATABLOCK_HEADER
*)pDataBlock
))
2450 HRESULT STDMETHODCALLTYPE
CShellLink::CopyDataBlock(DWORD dwSig
, void** ppDataBlock
)
2452 DATABLOCK_HEADER
* pBlock
;
2455 TRACE("%p %08x %p\n", this, dwSig
, ppDataBlock
);
2457 *ppDataBlock
= NULL
;
2459 pBlock
= SHFindDataBlock(m_pDBList
, dwSig
);
2462 ERR("unknown datablock %08x (not found)\n", dwSig
);
2466 pDataBlock
= LocalAlloc(LMEM_ZEROINIT
, pBlock
->cbSize
);
2468 return E_OUTOFMEMORY
;
2470 CopyMemory(pDataBlock
, pBlock
, pBlock
->cbSize
);
2472 *ppDataBlock
= pDataBlock
;
2476 HRESULT STDMETHODCALLTYPE
CShellLink::RemoveDataBlock(DWORD dwSig
)
2478 if (SHRemoveDataBlock(&m_pDBList
, dwSig
))
2486 HRESULT STDMETHODCALLTYPE
CShellLink::GetFlags(DWORD
*pdwFlags
)
2488 TRACE("%p %p\n", this, pdwFlags
);
2489 *pdwFlags
= m_Header
.dwFlags
;
2493 HRESULT STDMETHODCALLTYPE
CShellLink::SetFlags(DWORD dwFlags
)
2496 m_Header
.dwFlags
= dwFlags
;
2505 /**************************************************************************
2506 * CShellLink implementation of IShellExtInit::Initialize()
2508 * Loads the shelllink from the dataobject the shell is pointing to.
2510 HRESULT STDMETHODCALLTYPE
CShellLink::Initialize(LPCITEMIDLIST pidlFolder
, IDataObject
*pdtobj
, HKEY hkeyProgID
)
2512 TRACE("%p %p %p %p\n", this, pidlFolder
, pdtobj
, hkeyProgID
);
2518 format
.cfFormat
= CF_HDROP
;
2520 format
.dwAspect
= DVASPECT_CONTENT
;
2522 format
.tymed
= TYMED_HGLOBAL
;
2525 HRESULT hr
= pdtobj
->GetData(&format
, &stgm
);
2529 UINT count
= DragQueryFileW((HDROP
)stgm
.hGlobal
, -1, NULL
, 0);
2532 count
= DragQueryFileW((HDROP
)stgm
.hGlobal
, 0, NULL
, 0);
2534 LPWSTR path
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, count
* sizeof(WCHAR
));
2537 count
= DragQueryFileW((HDROP
)stgm
.hGlobal
, 0, path
, count
);
2539 HeapFree(GetProcessHeap(), 0, path
);
2542 ReleaseStgMedium(&stgm
);
2547 HRESULT STDMETHODCALLTYPE
CShellLink::QueryContextMenu(HMENU hMenu
, UINT indexMenu
, UINT idCmdFirst
, UINT idCmdLast
, UINT uFlags
)
2551 TRACE("%p %p %u %u %u %u\n", this,
2552 hMenu
, indexMenu
, idCmdFirst
, idCmdLast
, uFlags
);
2555 return E_INVALIDARG
;
2558 if (!LoadStringW(shell32_hInstance
, IDS_OPEN_VERB
, wszOpen
, _countof(wszOpen
)))
2562 ZeroMemory(&mii
, sizeof(mii
));
2563 mii
.cbSize
= sizeof(mii
);
2564 mii
.fMask
= MIIM_TYPE
| MIIM_ID
| MIIM_STATE
;
2565 mii
.dwTypeData
= wszOpen
;
2566 mii
.cch
= wcslen(mii
.dwTypeData
);
2567 mii
.wID
= idCmdFirst
+ id
++;
2568 mii
.fState
= MFS_DEFAULT
| MFS_ENABLED
;
2569 mii
.fType
= MFT_STRING
;
2570 if (!InsertMenuItemW(hMenu
, indexMenu
, TRUE
, &mii
))
2574 return MAKE_HRESULT(SEVERITY_SUCCESS
, 0, id
);
2577 HRESULT STDMETHODCALLTYPE
CShellLink::InvokeCommand(LPCMINVOKECOMMANDINFO lpici
)
2582 TRACE("%p %p\n", this, lpici
);
2584 if (lpici
->cbSize
< sizeof(CMINVOKECOMMANDINFO
))
2585 return E_INVALIDARG
;
2587 // NOTE: We could use lpici->hwnd (certainly in case lpici->fMask doesn't contain CMIC_MASK_FLAG_NO_UI)
2588 // as the parent window handle... ?
2589 /* FIXME: get using interface set from IObjectWithSite?? */
2590 // NOTE: We might need an extended version of Resolve that provides us with paths...
2591 HRESULT hr
= Resolve(lpici
->hwnd
, 0);
2594 TRACE("failed to resolve component with error 0x%08x", hr
);
2598 path
= strdupW(m_sPath
);
2600 if ( lpici
->cbSize
== sizeof(CMINVOKECOMMANDINFOEX
) &&
2601 (lpici
->fMask
& CMIC_MASK_UNICODE
) )
2603 LPCMINVOKECOMMANDINFOEX iciex
= (LPCMINVOKECOMMANDINFOEX
)lpici
;
2607 len
+= wcslen(m_sArgs
);
2608 if (iciex
->lpParametersW
)
2609 len
+= wcslen(iciex
->lpParametersW
);
2611 args
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
2614 wcscat(args
, m_sArgs
);
2615 if (iciex
->lpParametersW
)
2618 wcscat(args
, iciex
->lpParametersW
);
2621 else if (m_sArgs
!= NULL
)
2623 args
= strdupW(m_sArgs
);
2626 SHELLEXECUTEINFOW sei
;
2627 ZeroMemory(&sei
, sizeof(sei
));
2628 sei
.cbSize
= sizeof(sei
);
2629 sei
.fMask
= SEE_MASK_HASLINKNAME
| SEE_MASK_UNICODE
|
2630 (lpici
->fMask
& (SEE_MASK_NOASYNC
| SEE_MASK_ASYNCOK
| SEE_MASK_FLAG_NO_UI
));
2632 sei
.lpClass
= m_sLinkPath
;
2633 sei
.nShow
= m_Header
.nShowCommand
;
2634 sei
.lpDirectory
= m_sWorkDir
;
2635 sei
.lpParameters
= args
;
2636 sei
.lpVerb
= L
"open";
2638 // HACK for ShellExecuteExW
2639 if (m_sPath
&& wcsstr(m_sPath
, L
".cpl"))
2640 sei
.lpVerb
= L
"cplopen";
2642 if (ShellExecuteExW(&sei
))
2647 HeapFree(GetProcessHeap(), 0, args
);
2648 HeapFree(GetProcessHeap(), 0, path
);
2653 HRESULT STDMETHODCALLTYPE
CShellLink::GetCommandString(UINT_PTR idCmd
, UINT uType
, UINT
* pwReserved
, LPSTR pszName
, UINT cchMax
)
2655 FIXME("%p %lu %u %p %p %u\n", this, idCmd
, uType
, pwReserved
, pszName
, cchMax
);
2659 INT_PTR CALLBACK
ExtendedShortcutProc(HWND hwndDlg
, UINT uMsg
,
2660 WPARAM wParam
, LPARAM lParam
)
2667 HWND hDlgCtrl
= GetDlgItem(hwndDlg
, IDC_SHORTEX_RUN_DIFFERENT
);
2668 SendMessage(hDlgCtrl
, BM_SETCHECK
, BST_CHECKED
, 0);
2673 HWND hDlgCtrl
= GetDlgItem(hwndDlg
, IDC_SHORTEX_RUN_DIFFERENT
);
2674 if (LOWORD(wParam
) == IDOK
)
2676 if (SendMessage(hDlgCtrl
, BM_GETCHECK
, 0, 0) == BST_CHECKED
)
2677 EndDialog(hwndDlg
, 1);
2679 EndDialog(hwndDlg
, 0);
2681 else if (LOWORD(wParam
) == IDCANCEL
)
2683 EndDialog(hwndDlg
, -1);
2685 else if (LOWORD(wParam
) == IDC_SHORTEX_RUN_DIFFERENT
)
2687 if (SendMessage(hDlgCtrl
, BM_GETCHECK
, 0, 0) == BST_CHECKED
)
2688 SendMessage(hDlgCtrl
, BM_SETCHECK
, BST_UNCHECKED
, 0);
2690 SendMessage(hDlgCtrl
, BM_SETCHECK
, BST_CHECKED
, 0);
2699 SHOpenFolderAndSelectItems(LPITEMIDLIST pidlFolder
,
2701 PCUITEMID_CHILD_ARRAY apidl
,
2704 /**************************************************************************
2705 * SH_GetTargetTypeByPath
2707 * Function to get target type by passing full path to it
2709 LPWSTR
SH_GetTargetTypeByPath(LPCWSTR lpcwFullPath
)
2712 static WCHAR wszBuf
[MAX_PATH
];
2714 /* Get file information */
2716 if (!SHGetFileInfoW(lpcwFullPath
, 0, &fi
, sizeof(fi
), SHGFI_TYPENAME
| SHGFI_USEFILEATTRIBUTES
))
2718 ERR("SHGetFileInfoW failed for %ls (%lu)\n", lpcwFullPath
, GetLastError());
2719 fi
.szTypeName
[0] = L
'\0';
2723 pwszExt
= PathFindExtensionW(lpcwFullPath
);
2726 if (!fi
.szTypeName
[0])
2728 /* The file type is unknown, so default to string "FileExtension File" */
2729 size_t cchRemaining
= 0;
2730 LPWSTR pwszEnd
= NULL
;
2732 StringCchPrintfExW(wszBuf
, _countof(wszBuf
), &pwszEnd
, &cchRemaining
, 0, L
"%s ", pwszExt
+ 1);
2736 /* Update file type */
2737 StringCbPrintfW(wszBuf
, sizeof(wszBuf
), L
"%s (%s)", fi
.szTypeName
, pwszExt
);
2744 BOOL
CShellLink::OnInitDialog(HWND hwndDlg
, HWND hwndFocus
, LPARAM lParam
)
2746 TRACE("CShellLink::OnInitDialog(hwnd %p hwndFocus %p lParam %p)\n", hwndDlg
, hwndFocus
, lParam
);
2748 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
,
2749 m_sIcoPath
, m_sPath
, m_sPathRel
, sProduct
, m_sWorkDir
);
2753 /* Get file information */
2754 // FIXME! FIXME! Shouldn't we use m_sIcoPath, m_Header.nIconIndex instead???
2756 if (!SHGetFileInfoW(m_sLinkPath
, 0, &fi
, sizeof(fi
), SHGFI_TYPENAME
| SHGFI_ICON
))
2758 ERR("SHGetFileInfoW failed for %ls (%lu)\n", m_sLinkPath
, GetLastError());
2759 fi
.szTypeName
[0] = L
'\0';
2766 DestroyIcon(m_hIcon
);
2768 SendDlgItemMessageW(hwndDlg
, IDC_SHORTCUT_ICON
, STM_SETICON
, (WPARAM
)m_hIcon
, 0);
2771 ERR("ExtractIconW failed %ls %u\n", m_sIcoPath
, m_Header
.nIconIndex
);
2775 SetDlgItemTextW(hwndDlg
, IDC_SHORTCUT_TYPE_EDIT
, SH_GetTargetTypeByPath(m_sPath
));
2777 /* Target location */
2780 WCHAR target
[MAX_PATH
];
2781 StringCchCopyW(target
, _countof(target
), m_sPath
);
2782 PathRemoveFileSpecW(target
);
2783 SetDlgItemTextW(hwndDlg
, IDC_SHORTCUT_LOCATION_EDIT
, PathFindFileNameW(target
));
2789 WCHAR newpath
[2*MAX_PATH
] = L
"\0";
2790 if (wcschr(m_sPath
, ' '))
2791 StringCchPrintfExW(newpath
, _countof(newpath
), NULL
, NULL
, 0, L
"\"%ls\"", m_sPath
);
2793 StringCchCopyExW(newpath
, _countof(newpath
), m_sPath
, NULL
, NULL
, 0);
2795 if (m_sArgs
&& m_sArgs
[0])
2797 StringCchCatW(newpath
, _countof(newpath
), L
" ");
2798 StringCchCatW(newpath
, _countof(newpath
), m_sArgs
);
2800 SetDlgItemTextW(hwndDlg
, IDC_SHORTCUT_TARGET_TEXT
, newpath
);
2805 SetDlgItemTextW(hwndDlg
, IDC_SHORTCUT_START_IN_EDIT
, m_sWorkDir
);
2809 SetDlgItemTextW(hwndDlg
, IDC_SHORTCUT_COMMENT_EDIT
, m_sDescription
);
2816 void CShellLink::OnCommand(HWND hwndDlg
, int id
, HWND hwndCtl
, UINT codeNotify
)
2820 case IDC_SHORTCUT_FIND
:
2821 SHOpenFolderAndSelectItems(m_pPidl
, 0, NULL
, 0);
2824 /// open target directory
2828 case IDC_SHORTCUT_CHANGE_ICON
:
2830 WCHAR wszPath
[MAX_PATH
] = L
"";
2833 wcscpy(wszPath
, m_sIcoPath
);
2835 FindExecutableW(m_sPath
, NULL
, wszPath
);
2837 INT IconIndex
= m_Header
.nIconIndex
;
2838 if (PickIconDlg(hwndDlg
, wszPath
, _countof(wszPath
), &IconIndex
))
2840 SetIconLocation(wszPath
, IconIndex
);
2841 PropSheet_Changed(GetParent(hwndDlg
), hwndDlg
);
2843 HICON hIconLarge
= CreateShortcutIcon(wszPath
, IconIndex
);
2847 DestroyIcon(m_hIcon
);
2848 m_hIcon
= hIconLarge
;
2849 SendDlgItemMessageW(hwndDlg
, IDC_SHORTCUT_ICON
, STM_SETICON
, (WPARAM
)m_hIcon
, 0);
2855 case IDC_SHORTCUT_ADVANCED
:
2857 INT_PTR result
= DialogBoxParamW(shell32_hInstance
, MAKEINTRESOURCEW(IDD_SHORTCUT_EXTENDED_PROPERTIES
), hwndDlg
, ExtendedShortcutProc
, (LPARAM
)m_bRunAs
);
2858 if (result
== 1 || result
== 0)
2860 if (m_bRunAs
!= result
)
2862 PropSheet_Changed(GetParent(hwndDlg
), hwndDlg
);
2870 if (codeNotify
== EN_CHANGE
)
2873 PropSheet_Changed(GetParent(hwndDlg
), hwndDlg
);
2877 LRESULT
CShellLink::OnNotify(HWND hwndDlg
, int idFrom
, LPNMHDR pnmhdr
)
2879 WCHAR wszBuf
[MAX_PATH
];
2880 LPPSHNOTIFY lppsn
= (LPPSHNOTIFY
)pnmhdr
;
2882 if (lppsn
->hdr
.code
== PSN_APPLY
)
2884 /* set working directory */
2885 GetDlgItemTextW(hwndDlg
, IDC_SHORTCUT_START_IN_EDIT
, wszBuf
, _countof(wszBuf
));
2886 SetWorkingDirectory(wszBuf
);
2888 /* set link destination */
2889 GetDlgItemTextW(hwndDlg
, IDC_SHORTCUT_TARGET_TEXT
, wszBuf
, _countof(wszBuf
));
2890 LPWSTR lpszArgs
= NULL
;
2891 LPWSTR unquoted
= strdupW(wszBuf
);
2892 StrTrimW(unquoted
, L
" ");
2894 if (!PathFileExistsW(unquoted
))
2896 lpszArgs
= PathGetArgsW(unquoted
);
2897 PathRemoveArgsW(unquoted
);
2898 StrTrimW(lpszArgs
, L
" ");
2900 if (unquoted
[0] == '"' && unquoted
[wcslen(unquoted
) - 1] == '"')
2901 PathUnquoteSpacesW(unquoted
);
2903 WCHAR
*pwszExt
= PathFindExtensionW(unquoted
);
2904 if (!wcsicmp(pwszExt
, L
".lnk"))
2906 // FIXME load localized error msg
2907 MessageBoxW(hwndDlg
, L
"You cannot create a link to a shortcut", L
"Error", MB_ICONERROR
);
2908 SetWindowLongPtr(hwndDlg
, DWLP_MSGRESULT
, PSNRET_INVALID_NOCHANGEPAGE
);
2912 if (!PathFileExistsW(unquoted
))
2914 // FIXME load localized error msg
2915 MessageBoxW(hwndDlg
, L
"The specified file name in the target box is invalid", L
"Error", MB_ICONERROR
);
2916 SetWindowLongPtr(hwndDlg
, DWLP_MSGRESULT
, PSNRET_INVALID_NOCHANGEPAGE
);
2922 SetArguments(lpszArgs
);
2924 SetArguments(L
"\0");
2926 HeapFree(GetProcessHeap(), 0, unquoted
);
2928 TRACE("This %p m_sLinkPath %S\n", this, m_sLinkPath
);
2929 Save(m_sLinkPath
, TRUE
);
2930 SHChangeNotify(SHCNE_UPDATEITEM
, SHCNF_PATHW
, m_sLinkPath
, NULL
);
2931 SetWindowLongPtr(hwndDlg
, DWLP_MSGRESULT
, PSNRET_NOERROR
);
2937 void CShellLink::OnDestroy(HWND hwndDlg
)
2941 DestroyIcon(m_hIcon
);
2946 /**************************************************************************
2947 * SH_ShellLinkDlgProc
2949 * dialog proc of the shortcut property dialog
2953 CShellLink::SH_ShellLinkDlgProc(HWND hwndDlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
2955 LPPROPSHEETPAGEW ppsp
;
2956 CShellLink
*pThis
= reinterpret_cast<CShellLink
*>(GetWindowLongPtr(hwndDlg
, DWLP_USER
));
2961 ppsp
= (LPPROPSHEETPAGEW
)lParam
;
2965 pThis
= reinterpret_cast<CShellLink
*>(ppsp
->lParam
);
2966 SetWindowLongPtr(hwndDlg
, DWLP_USER
, (LONG_PTR
)pThis
);
2967 return pThis
->OnInitDialog(hwndDlg
, (HWND
)(wParam
), lParam
);
2970 return pThis
->OnNotify(hwndDlg
, (int)wParam
, (NMHDR
*)lParam
);
2973 pThis
->OnCommand(hwndDlg
, LOWORD(wParam
), (HWND
)lParam
, HIWORD(wParam
));
2977 pThis
->OnDestroy(hwndDlg
);
2987 /**************************************************************************
2988 * ShellLink_IShellPropSheetExt interface
2991 HRESULT STDMETHODCALLTYPE
CShellLink::AddPages(LPFNADDPROPSHEETPAGE pfnAddPage
, LPARAM lParam
)
2993 HPROPSHEETPAGE hPage
= SH_CreatePropertySheetPage(IDD_SHORTCUT_PROPERTIES
, SH_ShellLinkDlgProc
, (LPARAM
)this, NULL
);
2996 ERR("failed to create property sheet page\n");
3000 if (!pfnAddPage(hPage
, lParam
))
3006 HRESULT STDMETHODCALLTYPE
CShellLink::ReplacePage(UINT uPageID
, LPFNADDPROPSHEETPAGE pfnReplacePage
, LPARAM lParam
)
3008 TRACE("(%p) (uPageID %u, pfnReplacePage %p lParam %p\n", this, uPageID
, pfnReplacePage
, lParam
);
3012 HRESULT STDMETHODCALLTYPE
CShellLink::SetSite(IUnknown
*punk
)
3014 TRACE("%p %p\n", this, punk
);
3021 HRESULT STDMETHODCALLTYPE
CShellLink::GetSite(REFIID iid
, void ** ppvSite
)
3023 TRACE("%p %s %p\n", this, debugstr_guid(&iid
), ppvSite
);
3028 return m_site
->QueryInterface(iid
, ppvSite
);
3031 HRESULT STDMETHODCALLTYPE
CShellLink::DragEnter(IDataObject
*pDataObject
,
3032 DWORD dwKeyState
, POINTL pt
, DWORD
*pdwEffect
)
3034 TRACE("(%p)->(DataObject=%p)\n", this, pDataObject
);
3035 LPCITEMIDLIST pidlLast
;
3036 CComPtr
<IShellFolder
> psf
;
3038 HRESULT hr
= SHBindToParent(m_pPidl
, IID_PPV_ARG(IShellFolder
, &psf
), &pidlLast
);
3042 hr
= psf
->GetUIObjectOf(0, 1, &pidlLast
, IID_NULL_PPV_ARG(IDropTarget
, &m_DropTarget
));
3045 hr
= m_DropTarget
->DragEnter(pDataObject
, dwKeyState
, pt
, pdwEffect
);
3047 *pdwEffect
= DROPEFFECT_NONE
;
3050 *pdwEffect
= DROPEFFECT_NONE
;
3055 HRESULT STDMETHODCALLTYPE
CShellLink::DragOver(DWORD dwKeyState
, POINTL pt
,
3058 TRACE("(%p)\n", this);
3061 hr
= m_DropTarget
->DragOver(dwKeyState
, pt
, pdwEffect
);
3065 HRESULT STDMETHODCALLTYPE
CShellLink::DragLeave()
3067 TRACE("(%p)\n", this);
3071 hr
= m_DropTarget
->DragLeave();
3072 m_DropTarget
.Release();
3078 HRESULT STDMETHODCALLTYPE
CShellLink::Drop(IDataObject
*pDataObject
,
3079 DWORD dwKeyState
, POINTL pt
, DWORD
*pdwEffect
)
3081 TRACE("(%p)\n", this);
3084 hr
= m_DropTarget
->Drop(pDataObject
, dwKeyState
, pt
, pdwEffect
);
3089 /**************************************************************************
3090 * IShellLink_ConstructFromFile
3092 HRESULT WINAPI
IShellLink_ConstructFromPath(WCHAR
*path
, REFIID riid
, LPVOID
*ppv
)
3094 CComPtr
<IPersistFile
> ppf
;
3095 HRESULT hr
= CShellLink::_CreatorClass::CreateInstance(NULL
, IID_PPV_ARG(IPersistFile
, &ppf
));
3099 hr
= ppf
->Load(path
, 0);
3103 return ppf
->QueryInterface(riid
, ppv
);
3106 HRESULT WINAPI
IShellLink_ConstructFromFile(IShellFolder
* psf
, LPCITEMIDLIST pidl
, REFIID riid
, LPVOID
*ppv
)
3108 WCHAR path
[MAX_PATH
];
3109 if (!ILGetDisplayNameExW(psf
, pidl
, path
, 0))
3112 return IShellLink_ConstructFromPath(path
, riid
, ppv
);
3115 HICON
CShellLink::CreateShortcutIcon(LPCWSTR wszIconPath
, INT IconIndex
)
3117 const INT cx
= GetSystemMetrics(SM_CXICON
), cy
= GetSystemMetrics(SM_CYICON
);
3118 const COLORREF crMask
= GetSysColor(COLOR_3DFACE
);
3120 HIMAGELIST himl
= ImageList_Create(cx
, cy
, ILC_COLOR32
| ILC_MASK
, 1, 1);
3121 HICON hIcon
= NULL
, hNewIcon
= NULL
;
3122 HICON hShortcut
= (HICON
)LoadImageW(shell32_hInstance
, MAKEINTRESOURCE(IDI_SHELL_SHORTCUT
),
3123 IMAGE_ICON
, cx
, cy
, 0);
3125 ::ExtractIconExW(wszIconPath
, IconIndex
, &hIcon
, NULL
, 1);
3126 if (!hIcon
|| !hShortcut
|| !himl
)
3129 hDC
= CreateCompatibleDC(NULL
);
3132 // create 32bpp bitmap
3134 ZeroMemory(&bi
, sizeof(bi
));
3135 bi
.bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
3136 bi
.bmiHeader
.biWidth
= cx
;
3137 bi
.bmiHeader
.biHeight
= cy
;
3138 bi
.bmiHeader
.biPlanes
= 1;
3139 bi
.bmiHeader
.biBitCount
= 32;
3141 HBITMAP hbm
= CreateDIBSection(hDC
, &bi
, DIB_RGB_COLORS
, &pvBits
, NULL
, 0);
3144 // draw the icon image
3145 HGDIOBJ hbmOld
= SelectObject(hDC
, hbm
);
3147 HBRUSH hbr
= CreateSolidBrush(crMask
);
3148 RECT rc
= { 0, 0, cx
, cy
};
3149 FillRect(hDC
, &rc
, hbr
);
3152 DrawIconEx(hDC
, 0, 0, hIcon
, cx
, cy
, 0, NULL
, DI_NORMAL
);
3153 DrawIconEx(hDC
, 0, 0, hShortcut
, cx
, cy
, 0, NULL
, DI_NORMAL
);
3155 SelectObject(hDC
, hbmOld
);
3157 INT iAdded
= ImageList_AddMasked(himl
, hbm
, crMask
);
3158 hNewIcon
= ImageList_GetIcon(himl
, iAdded
, ILD_NORMAL
| ILD_TRANSPARENT
);
3169 DestroyIcon(hShortcut
);
3171 ImageList_Destroy(himl
);