92f62961cf8097a397d38fa6a74e5fdfc2747ab1
[reactos.git] / dll / win32 / shell32 / CShellLink.cpp
1 /*
2 *
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>
10 *
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.
15 *
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.
20 *
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
24 *
25 * NOTES
26 * Nearly complete information about the binary formats
27 * of .lnk files available at http://www.wotsit.org
28 *
29 * You can use winedump to examine the contents of a link file:
30 * winedump lnk sc.lnk
31 *
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.
37 */
38 /*
39 * In the following is listed more documentation about the Shell Link file format,
40 * as well as its interface.
41 *
42 * General introduction about "Shell Links" (MSDN):
43 * https://msdn.microsoft.com/en-us/library/windows/desktop/bb776891(v=vs.85).aspx
44 *
45 *
46 * Details of the file format:
47 *
48 * - Official MSDN documentation "[MS-SHLLINK]: Shell Link (.LNK) Binary File Format":
49 * https://msdn.microsoft.com/en-us/library/dd871305.aspx
50 *
51 * - Forensics:
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
56 *
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
60 *
61 *
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:
66 *
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 .
71 *
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
79 *
80 *
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
88 *
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.
99 *
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.
102 *
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
113 *
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
126 *
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
131 * New Thing" blog:
132 * https://blogs.msdn.microsoft.com/oldnewthing/20121210-00/?p=5883#comment-1025083
133 *
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.
137 *
138 */
139
140 #include "precomp.h"
141
142 #include <appmgmt.h>
143
144 WINE_DEFAULT_DEBUG_CHANNEL(shell);
145
146 #define SHLINK_LOCAL 0
147 #define SHLINK_REMOTE 1
148
149 /* link file formats */
150
151 #include "pshpack1.h"
152
153 struct LOCATION_INFO
154 {
155 DWORD dwTotalSize;
156 DWORD dwHeaderSize;
157 DWORD dwFlags;
158 DWORD dwVolTableOfs;
159 DWORD dwLocalPathOfs;
160 DWORD dwNetworkVolTableOfs;
161 DWORD dwFinalPathOfs;
162 };
163
164 struct LOCAL_VOLUME_INFO
165 {
166 DWORD dwSize;
167 DWORD dwType;
168 DWORD dwVolSerial;
169 DWORD dwVolLabelOfs;
170 };
171
172 struct volume_info
173 {
174 DWORD type;
175 DWORD serial;
176 WCHAR label[12]; /* assume 8.3 */
177 };
178
179 #include "poppack.h"
180
181 /* IShellLink Implementation */
182
183 static HRESULT ShellLink_UpdatePath(LPCWSTR sPathRel, LPCWSTR path, LPCWSTR sWorkDir, LPWSTR* psPath);
184
185 /* strdup on the process heap */
186 static LPWSTR __inline HEAP_strdupAtoW(HANDLE heap, DWORD flags, LPCSTR str)
187 {
188 INT len;
189 LPWSTR p;
190
191 assert(str);
192
193 len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
194 p = (LPWSTR)HeapAlloc(heap, flags, len * sizeof(WCHAR));
195 if (!p)
196 return p;
197 MultiByteToWideChar(CP_ACP, 0, str, -1, p, len);
198 return p;
199 }
200
201 static LPWSTR __inline strdupW(LPCWSTR src)
202 {
203 LPWSTR dest;
204 if (!src) return NULL;
205 dest = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, (wcslen(src) + 1) * sizeof(WCHAR));
206 if (dest)
207 wcscpy(dest, src);
208 return dest;
209 }
210
211 // TODO: Use it for constructor & destructor too
212 VOID CShellLink::Reset()
213 {
214 ILFree(m_pPidl);
215 m_pPidl = NULL;
216
217 HeapFree(GetProcessHeap(), 0, m_sPath);
218 m_sPath = NULL;
219 ZeroMemory(&volume, sizeof(volume));
220
221 HeapFree(GetProcessHeap(), 0, m_sDescription);
222 m_sDescription = NULL;
223 HeapFree(GetProcessHeap(), 0, m_sPathRel);
224 m_sPathRel = NULL;
225 HeapFree(GetProcessHeap(), 0, m_sWorkDir);
226 m_sWorkDir = NULL;
227 HeapFree(GetProcessHeap(), 0, m_sArgs);
228 m_sArgs = NULL;
229 HeapFree(GetProcessHeap(), 0, m_sIcoPath);
230 m_sIcoPath = NULL;
231
232 m_bRunAs = FALSE;
233 m_bDirty = FALSE;
234
235 if (m_pDBList)
236 SHFreeDataBlockList(m_pDBList);
237 m_pDBList = NULL;
238
239 /**/sProduct = sComponent = NULL;/**/
240 }
241
242 CShellLink::CShellLink()
243 {
244 m_Header.dwSize = sizeof(m_Header);
245 m_Header.clsid = CLSID_ShellLink;
246 m_Header.dwFlags = 0;
247
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;
253
254 m_Header.nIconIndex = 0;
255 m_Header.nShowCommand = SW_SHOWNORMAL;
256 m_Header.wHotKey = 0;
257
258 m_pPidl = NULL;
259
260 m_sPath = NULL;
261 ZeroMemory(&volume, sizeof(volume));
262
263 m_sDescription = NULL;
264 m_sPathRel = NULL;
265 m_sWorkDir = NULL;
266 m_sArgs = NULL;
267 m_sIcoPath = NULL;
268 m_bRunAs = FALSE;
269 m_bDirty = FALSE;
270 m_pDBList = NULL;
271 m_bInInit = FALSE;
272 m_hIcon = NULL;
273
274 m_sLinkPath = NULL;
275 m_iIdOpen = -1;
276
277 /**/sProduct = sComponent = NULL;/**/
278 }
279
280 CShellLink::~CShellLink()
281 {
282 TRACE("-- destroying IShellLink(%p)\n", this);
283
284 ILFree(m_pPidl);
285
286 HeapFree(GetProcessHeap(), 0, m_sPath);
287
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);
295 }
296
297 HRESULT STDMETHODCALLTYPE CShellLink::GetClassID(CLSID *pclsid)
298 {
299 TRACE("%p %p\n", this, pclsid);
300
301 if (pclsid == NULL)
302 return E_POINTER;
303 *pclsid = CLSID_ShellLink;
304 return S_OK;
305 }
306
307 /************************************************************************
308 * IPersistStream_IsDirty (IPersistStream)
309 */
310 HRESULT STDMETHODCALLTYPE CShellLink::IsDirty()
311 {
312 TRACE("(%p)\n", this);
313 return (m_bDirty ? S_OK : S_FALSE);
314 }
315
316 HRESULT STDMETHODCALLTYPE CShellLink::Load(LPCOLESTR pszFileName, DWORD dwMode)
317 {
318 TRACE("(%p, %s, %x)\n", this, debugstr_w(pszFileName), dwMode);
319
320 if (dwMode == 0)
321 dwMode = STGM_READ | STGM_SHARE_DENY_WRITE;
322
323 CComPtr<IStream> stm;
324 HRESULT hr = SHCreateStreamOnFileW(pszFileName, dwMode, &stm);
325 if (SUCCEEDED(hr))
326 {
327 HeapFree(GetProcessHeap(), 0, m_sLinkPath);
328 m_sLinkPath = strdupW(pszFileName);
329 hr = Load(stm);
330 ShellLink_UpdatePath(m_sPathRel, pszFileName, m_sWorkDir, &m_sPath);
331 m_bDirty = FALSE;
332 }
333 TRACE("-- returning hr %08x\n", hr);
334 return hr;
335 }
336
337 HRESULT STDMETHODCALLTYPE CShellLink::Save(LPCOLESTR pszFileName, BOOL fRemember)
338 {
339 TRACE("(%p)->(%s)\n", this, debugstr_w(pszFileName));
340
341 if (!pszFileName)
342 return E_FAIL;
343
344 CComPtr<IStream> stm;
345 HRESULT hr = SHCreateStreamOnFileW(pszFileName, STGM_READWRITE | STGM_CREATE | STGM_SHARE_EXCLUSIVE, &stm);
346 if (SUCCEEDED(hr))
347 {
348 hr = Save(stm, FALSE);
349
350 if (SUCCEEDED(hr))
351 {
352 if (m_sLinkPath)
353 HeapFree(GetProcessHeap(), 0, m_sLinkPath);
354
355 m_sLinkPath = strdupW(pszFileName);
356 m_bDirty = FALSE;
357 }
358 else
359 {
360 DeleteFileW(pszFileName);
361 WARN("Failed to create shortcut %s\n", debugstr_w(pszFileName));
362 }
363 }
364
365 return hr;
366 }
367
368 HRESULT STDMETHODCALLTYPE CShellLink::SaveCompleted(LPCOLESTR pszFileName)
369 {
370 FIXME("(%p)->(%s)\n", this, debugstr_w(pszFileName));
371 return S_OK;
372 }
373
374 HRESULT STDMETHODCALLTYPE CShellLink::GetCurFile(LPOLESTR *ppszFileName)
375 {
376 *ppszFileName = NULL;
377
378 if (!m_sLinkPath)
379 {
380 /* IPersistFile::GetCurFile called before IPersistFile::Save */
381 return S_FALSE;
382 }
383
384 *ppszFileName = (LPOLESTR)CoTaskMemAlloc((wcslen(m_sLinkPath) + 1) * sizeof(WCHAR));
385 if (!*ppszFileName)
386 {
387 /* out of memory */
388 return E_OUTOFMEMORY;
389 }
390
391 /* copy last saved filename */
392 wcscpy(*ppszFileName, m_sLinkPath);
393
394 return S_OK;
395 }
396
397 static HRESULT Stream_LoadString(IStream* stm, BOOL unicode, LPWSTR *pstr)
398 {
399 TRACE("%p\n", stm);
400
401 USHORT len;
402 DWORD count = 0;
403 HRESULT hr = stm->Read(&len, sizeof(len), &count);
404 if (FAILED(hr) || count != sizeof(len))
405 return E_FAIL;
406
407 if (unicode)
408 len *= sizeof(WCHAR);
409
410 TRACE("reading %d\n", len);
411 LPSTR temp = (LPSTR)HeapAlloc(GetProcessHeap(), 0, len + sizeof(WCHAR));
412 if (!temp)
413 return E_OUTOFMEMORY;
414 count = 0;
415 hr = stm->Read(temp, len, &count);
416 if (FAILED(hr) || count != len)
417 {
418 HeapFree(GetProcessHeap(), 0, temp);
419 return E_FAIL;
420 }
421
422 TRACE("read %s\n", debugstr_an(temp, len));
423
424 /* convert to unicode if necessary */
425 LPWSTR str;
426 if (!unicode)
427 {
428 count = MultiByteToWideChar(CP_ACP, 0, temp, len, NULL, 0);
429 str = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, (count + 1) * sizeof(WCHAR));
430 if (!str)
431 {
432 HeapFree(GetProcessHeap(), 0, temp);
433 return E_OUTOFMEMORY;
434 }
435 MultiByteToWideChar(CP_ACP, 0, temp, len, str, count);
436 HeapFree(GetProcessHeap(), 0, temp);
437 }
438 else
439 {
440 count /= sizeof(WCHAR);
441 str = (LPWSTR)temp;
442 }
443 str[count] = 0;
444
445 *pstr = str;
446
447 return S_OK;
448 }
449
450
451 /*
452 * NOTE: The following 5 functions are part of LINKINFO.DLL
453 */
454 static BOOL ShellLink_GetVolumeInfo(LPCWSTR path, CShellLink::volume_info *volume)
455 {
456 WCHAR drive[4] = { path[0], ':', '\\', 0 };
457
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));
462 return bRet;
463 }
464
465 static HRESULT Stream_ReadChunk(IStream* stm, LPVOID *data)
466 {
467 struct sized_chunk
468 {
469 DWORD size;
470 unsigned char data[1];
471 } *chunk;
472
473 TRACE("%p\n", stm);
474
475 DWORD size;
476 ULONG count;
477 HRESULT hr = stm->Read(&size, sizeof(size), &count);
478 if (FAILED(hr) || count != sizeof(size))
479 return E_FAIL;
480
481 chunk = static_cast<sized_chunk *>(HeapAlloc(GetProcessHeap(), 0, size));
482 if (!chunk)
483 return E_OUTOFMEMORY;
484
485 chunk->size = size;
486 hr = stm->Read(chunk->data, size - sizeof(size), &count);
487 if (FAILED(hr) || count != (size - sizeof(size)))
488 {
489 HeapFree(GetProcessHeap(), 0, chunk);
490 return E_FAIL;
491 }
492
493 TRACE("Read %d bytes\n", chunk->size);
494
495 *data = chunk;
496
497 return S_OK;
498 }
499
500 static BOOL Stream_LoadVolume(LOCAL_VOLUME_INFO *vol, CShellLink::volume_info *volume)
501 {
502 volume->serial = vol->dwVolSerial;
503 volume->type = vol->dwType;
504
505 if (!vol->dwVolLabelOfs)
506 return FALSE;
507 if (vol->dwSize <= vol->dwVolLabelOfs)
508 return FALSE;
509 INT len = vol->dwSize - vol->dwVolLabelOfs;
510
511 LPSTR label = (LPSTR)vol;
512 label += vol->dwVolLabelOfs;
513 MultiByteToWideChar(CP_ACP, 0, label, len, volume->label, _countof(volume->label));
514
515 return TRUE;
516 }
517
518 static LPWSTR Stream_LoadPath(LPCSTR p, DWORD maxlen)
519 {
520 UINT len = 0;
521
522 while (len < maxlen && p[len])
523 len++;
524
525 UINT wlen = MultiByteToWideChar(CP_ACP, 0, p, len, NULL, 0);
526 LPWSTR path = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, (wlen + 1) * sizeof(WCHAR));
527 if (!path)
528 return NULL;
529 MultiByteToWideChar(CP_ACP, 0, p, len, path, wlen);
530 path[wlen] = 0;
531
532 return path;
533 }
534
535 static HRESULT Stream_LoadLocation(IStream *stm,
536 CShellLink::volume_info *volume, LPWSTR *path)
537 {
538 char *p = NULL;
539 HRESULT hr = Stream_ReadChunk(stm, (LPVOID*) &p);
540 if (FAILED(hr))
541 return hr;
542
543 LOCATION_INFO *loc = reinterpret_cast<LOCATION_INFO *>(p);
544 if (loc->dwTotalSize < sizeof(LOCATION_INFO))
545 {
546 HeapFree(GetProcessHeap(), 0, p);
547 return E_FAIL;
548 }
549
550 /* if there's valid local volume information, load it */
551 if (loc->dwVolTableOfs &&
552 ((loc->dwVolTableOfs + sizeof(LOCAL_VOLUME_INFO)) <= loc->dwTotalSize))
553 {
554 LOCAL_VOLUME_INFO *volume_info;
555
556 volume_info = (LOCAL_VOLUME_INFO*) &p[loc->dwVolTableOfs];
557 Stream_LoadVolume(volume_info, volume);
558 }
559
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);
564
565 TRACE("type %d serial %08x name %s path %s\n", volume->type,
566 volume->serial, debugstr_w(volume->label), debugstr_w(*path));
567
568 HeapFree(GetProcessHeap(), 0, p);
569 return S_OK;
570 }
571
572
573 /*
574 * The format of the advertised shortcut info is:
575 *
576 * Offset Description
577 * ------ -----------
578 * 0 Length of the block (4 bytes, usually 0x314)
579 * 4 tag (dword)
580 * 8 string data in ASCII
581 * 8+0x104 string data in UNICODE
582 *
583 * In the original Win32 implementation the buffers are not initialized
584 * to zero, so data trailing the string is random garbage.
585 */
586 HRESULT CShellLink::GetAdvertiseInfo(LPWSTR *str, DWORD dwSig)
587 {
588 LPEXP_DARWIN_LINK pInfo;
589
590 *str = NULL;
591
592 pInfo = (LPEXP_DARWIN_LINK)SHFindDataBlock(m_pDBList, dwSig);
593 if (!pInfo)
594 return E_FAIL;
595
596 /* Make sure that the size of the structure is valid */
597 if (pInfo->dbh.cbSize != sizeof(*pInfo))
598 {
599 ERR("Ooops. This structure is not as expected...\n");
600 return E_FAIL;
601 }
602
603 TRACE("dwSig %08x string = '%s'\n", pInfo->dbh.dwSignature, debugstr_w(pInfo->szwDarwinID));
604
605 *str = pInfo->szwDarwinID;
606 return S_OK;
607 }
608
609 /************************************************************************
610 * IPersistStream_Load (IPersistStream)
611 */
612 HRESULT STDMETHODCALLTYPE CShellLink::Load(IStream *stm)
613 {
614 TRACE("%p %p\n", this, stm);
615
616 if (!stm)
617 return STG_E_INVALIDPOINTER;
618
619 /* Free all the old stuff */
620 Reset();
621
622 ULONG dwBytesRead = 0;
623 HRESULT hr = stm->Read(&m_Header, sizeof(m_Header), &dwBytesRead);
624 if (FAILED(hr))
625 return hr;
626
627 if (dwBytesRead != sizeof(m_Header))
628 return E_FAIL;
629 if (m_Header.dwSize != sizeof(m_Header))
630 return E_FAIL;
631 if (!IsEqualIID(m_Header.clsid, CLSID_ShellLink))
632 return E_FAIL;
633
634 /* Load the new data in order */
635
636 if (TRACE_ON(shell))
637 {
638 SYSTEMTIME stCreationTime;
639 SYSTEMTIME stLastAccessTime;
640 SYSTEMTIME stLastWriteTime;
641 WCHAR sTemp[MAX_PATH];
642
643 FileTimeToSystemTime(&m_Header.ftCreationTime, &stCreationTime);
644 FileTimeToSystemTime(&m_Header.ftLastAccessTime, &stLastAccessTime);
645 FileTimeToSystemTime(&m_Header.ftLastWriteTime, &stLastWriteTime);
646
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));
656 }
657
658 /* load all the new stuff */
659 if (m_Header.dwFlags & SLDF_HAS_ID_LIST)
660 {
661 hr = ILLoadFromStream(stm, &m_pPidl);
662 if (FAILED(hr))
663 return hr;
664 }
665 pdump(m_pPidl);
666
667 /* Load the location information... */
668 if (m_Header.dwFlags & SLDF_HAS_LINK_INFO)
669 {
670 hr = Stream_LoadLocation(stm, &volume, &m_sPath);
671 if (FAILED(hr))
672 return hr;
673 }
674 /* ... but if it is required not to use it, clear it */
675 if (m_Header.dwFlags & SLDF_FORCE_NO_LINKINFO)
676 {
677 HeapFree(GetProcessHeap(), 0, m_sPath);
678 m_sPath = NULL;
679 ZeroMemory(&volume, sizeof(volume));
680 }
681
682 BOOL unicode = !!(m_Header.dwFlags & SLDF_UNICODE);
683
684 if (m_Header.dwFlags & SLDF_HAS_NAME)
685 {
686 hr = Stream_LoadString(stm, unicode, &m_sDescription);
687 if (FAILED(hr))
688 return hr;
689 TRACE("Description -> %s\n", debugstr_w(m_sDescription));
690 }
691
692 if (m_Header.dwFlags & SLDF_HAS_RELPATH)
693 {
694 hr = Stream_LoadString(stm, unicode, &m_sPathRel);
695 if (FAILED(hr))
696 return hr;
697 TRACE("Relative Path-> %s\n", debugstr_w(m_sPathRel));
698 }
699
700 if (m_Header.dwFlags & SLDF_HAS_WORKINGDIR)
701 {
702 hr = Stream_LoadString(stm, unicode, &m_sWorkDir);
703 if (FAILED(hr))
704 return hr;
705 PathRemoveBackslash(m_sWorkDir);
706 TRACE("Working Dir -> %s\n", debugstr_w(m_sWorkDir));
707 }
708
709 if (m_Header.dwFlags & SLDF_HAS_ARGS)
710 {
711 hr = Stream_LoadString(stm, unicode, &m_sArgs);
712 if (FAILED(hr))
713 return hr;
714 TRACE("Arguments -> %s\n", debugstr_w(m_sArgs));
715 }
716
717 if (m_Header.dwFlags & SLDF_HAS_ICONLOCATION)
718 {
719 hr = Stream_LoadString(stm, unicode, &m_sIcoPath);
720 if (FAILED(hr))
721 return hr;
722 TRACE("Icon file -> %s\n", debugstr_w(m_sIcoPath));
723 }
724
725 /* Now load the optional data block list */
726 hr = SHReadDataBlockList(stm, &m_pDBList);
727 if (FAILED(hr)) // FIXME: Should we fail?
728 return hr;
729
730 if (TRACE_ON(shell))
731 {
732 #if (NTDDI_VERSION < NTDDI_LONGHORN)
733 if (m_Header.dwFlags & SLDF_HAS_LOGO3ID)
734 {
735 hr = GetAdvertiseInfo(&sProduct, EXP_LOGO3_ID_SIG);
736 if (SUCCEEDED(hr))
737 TRACE("Product -> %s\n", debugstr_w(sProduct));
738 }
739 #endif
740 if (m_Header.dwFlags & SLDF_HAS_DARWINID)
741 {
742 hr = GetAdvertiseInfo(&sComponent, EXP_DARWIN_ID_SIG);
743 if (SUCCEEDED(hr))
744 TRACE("Component -> %s\n", debugstr_w(sComponent));
745 }
746 }
747
748 if (m_Header.dwFlags & SLDF_RUNAS_USER)
749 m_bRunAs = TRUE;
750 else
751 m_bRunAs = FALSE;
752
753 TRACE("OK\n");
754
755 pdump(m_pPidl);
756
757 return S_OK;
758 }
759
760 /************************************************************************
761 * Stream_WriteString
762 *
763 * Helper function for IPersistStream_Save. Writes a unicode string
764 * with terminating nul byte to a stream, preceded by the its length.
765 */
766 static HRESULT Stream_WriteString(IStream* stm, LPCWSTR str)
767 {
768 USHORT len = wcslen(str) + 1; // FIXME: Possible overflows?
769 DWORD count;
770
771 HRESULT hr = stm->Write(&len, sizeof(len), &count);
772 if (FAILED(hr))
773 return hr;
774
775 len *= sizeof(WCHAR);
776
777 hr = stm->Write(str, len, &count);
778 if (FAILED(hr))
779 return hr;
780
781 return S_OK;
782 }
783
784 /************************************************************************
785 * Stream_WriteLocationInfo
786 *
787 * Writes the location info to a stream
788 *
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.
792 */
793 static HRESULT Stream_WriteLocationInfo(IStream* stm, LPCWSTR path,
794 CShellLink::volume_info *volume)
795 {
796 LOCAL_VOLUME_INFO *vol;
797 LOCATION_INFO *loc;
798
799 TRACE("%p %s %p\n", stm, debugstr_w(path), volume);
800
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;
809
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];
816
817 /* fill in the location information header */
818 loc->dwTotalSize = total_size;
819 loc->dwHeaderSize = sizeof(*loc);
820 loc->dwFlags = 1;
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;
825
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);
831
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);
837 *szFinalPath = 0;
838
839 ULONG count = 0;
840 HRESULT hr = stm->Write(loc, total_size, &count);
841 HeapFree(GetProcessHeap(), 0, loc);
842
843 return hr;
844 }
845
846 /************************************************************************
847 * IPersistStream_Save (IPersistStream)
848 *
849 * FIXME: makes assumptions about byte order
850 */
851 HRESULT STDMETHODCALLTYPE CShellLink::Save(IStream *stm, BOOL fClearDirty)
852 {
853 TRACE("%p %p %x\n", this, stm, fClearDirty);
854
855 m_Header.dwSize = sizeof(m_Header);
856 m_Header.clsid = CLSID_ShellLink;
857
858 /*
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.
862 */
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)
866 SLDF_HAS_LOGO3ID |
867 #endif
868 SLDF_HAS_EXP_ICON_SZ | SLDF_HAS_EXP_SZ);
869 // TODO: When we will support Vista+ functionality, add other flags to this list.
870
871 /* The stored strings are in UNICODE */
872 m_Header.dwFlags |= SLDF_UNICODE;
873
874 if (m_pPidl)
875 m_Header.dwFlags |= SLDF_HAS_ID_LIST;
876 if (m_sPath)
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;
888 if (m_bRunAs)
889 m_Header.dwFlags |= SLDF_RUNAS_USER;
890
891 /* Write the shortcut header */
892 ULONG count;
893 HRESULT hr = stm->Write(&m_Header, sizeof(m_Header), &count);
894 if (FAILED(hr))
895 {
896 ERR("Write failed\n");
897 return hr;
898 }
899
900 /* Save the data in order */
901
902 if (m_pPidl)
903 {
904 hr = ILSaveToStream(stm, m_pPidl);
905 if (FAILED(hr))
906 {
907 ERR("Failed to write PIDL\n");
908 return hr;
909 }
910 }
911
912 if (m_sPath)
913 {
914 hr = Stream_WriteLocationInfo(stm, m_sPath, &volume);
915 if (FAILED(hr))
916 return hr;
917 }
918
919 if (m_Header.dwFlags & SLDF_HAS_NAME)
920 {
921 hr = Stream_WriteString(stm, m_sDescription);
922 if (FAILED(hr))
923 return hr;
924 }
925
926 if (m_Header.dwFlags & SLDF_HAS_RELPATH)
927 {
928 hr = Stream_WriteString(stm, m_sPathRel);
929 if (FAILED(hr))
930 return hr;
931 }
932
933 if (m_Header.dwFlags & SLDF_HAS_WORKINGDIR)
934 {
935 hr = Stream_WriteString(stm, m_sWorkDir);
936 if (FAILED(hr))
937 return hr;
938 }
939
940 if (m_Header.dwFlags & SLDF_HAS_ARGS)
941 {
942 hr = Stream_WriteString(stm, m_sArgs);
943 if (FAILED(hr))
944 return hr;
945 }
946
947 if (m_Header.dwFlags & SLDF_HAS_ICONLOCATION)
948 {
949 hr = Stream_WriteString(stm, m_sIcoPath);
950 if (FAILED(hr))
951 return hr;
952 }
953
954 /*
955 * Now save the data block list.
956 *
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.
960 */
961 hr = SHWriteDataBlockList(stm, m_pDBList);
962 if (FAILED(hr))
963 return hr;
964
965 /* Clear the dirty bit if requested */
966 if (fClearDirty)
967 m_bDirty = FALSE;
968
969 return hr;
970 }
971
972 /************************************************************************
973 * IPersistStream_GetSizeMax (IPersistStream)
974 */
975 HRESULT STDMETHODCALLTYPE CShellLink::GetSizeMax(ULARGE_INTEGER *pcbSize)
976 {
977 TRACE("(%p)\n", this);
978 return E_NOTIMPL;
979 }
980
981 static BOOL SHELL_ExistsFileW(LPCWSTR path)
982 {
983 if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW(path))
984 return FALSE;
985
986 return TRUE;
987 }
988
989 /**************************************************************************
990 * ShellLink_UpdatePath
991 * update absolute path in sPath using relative path in sPathRel
992 */
993 static HRESULT ShellLink_UpdatePath(LPCWSTR sPathRel, LPCWSTR path, LPCWSTR sWorkDir, LPWSTR* psPath)
994 {
995 if (!path || !psPath)
996 return E_INVALIDARG;
997
998 if (!*psPath && sPathRel)
999 {
1000 WCHAR buffer[2*MAX_PATH], abs_path[2*MAX_PATH];
1001 LPWSTR final = NULL;
1002
1003 /* first try if [directory of link file] + [relative path] finds an existing file */
1004
1005 GetFullPathNameW(path, MAX_PATH * 2, buffer, &final);
1006 if (!final)
1007 final = buffer;
1008 wcscpy(final, sPathRel);
1009
1010 *abs_path = '\0';
1011
1012 if (SHELL_ExistsFileW(buffer))
1013 {
1014 if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
1015 wcscpy(abs_path, buffer);
1016 }
1017 else
1018 {
1019 /* try if [working directory] + [relative path] finds an existing file */
1020 if (sWorkDir)
1021 {
1022 wcscpy(buffer, sWorkDir);
1023 wcscpy(PathAddBackslashW(buffer), sPathRel);
1024
1025 if (SHELL_ExistsFileW(buffer))
1026 if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
1027 wcscpy(abs_path, buffer);
1028 }
1029 }
1030
1031 /* FIXME: This is even not enough - not all shell links can be resolved using this algorithm. */
1032 if (!*abs_path)
1033 wcscpy(abs_path, sPathRel);
1034
1035 *psPath = strdupW(abs_path);
1036 if (!*psPath)
1037 return E_OUTOFMEMORY;
1038 }
1039
1040 return S_OK;
1041 }
1042
1043 HRESULT STDMETHODCALLTYPE CShellLink::GetPath(LPSTR pszFile, INT cchMaxPath, WIN32_FIND_DATAA *pfd, DWORD fFlags)
1044 {
1045 HRESULT hr;
1046 LPWSTR pszFileW;
1047 WIN32_FIND_DATAW wfd;
1048
1049 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%u)(%s)\n",
1050 this, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(m_sPath));
1051
1052 /* Allocate a temporary UNICODE buffer */
1053 pszFileW = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, cchMaxPath * sizeof(WCHAR));
1054 if (!pszFileW)
1055 return E_OUTOFMEMORY;
1056
1057 /* Call the UNICODE function */
1058 hr = GetPath(pszFileW, cchMaxPath, &wfd, fFlags);
1059
1060 /* Convert the file path back to ANSI */
1061 WideCharToMultiByte(CP_ACP, 0, pszFileW, -1,
1062 pszFile, cchMaxPath, NULL, NULL);
1063
1064 /* Free the temporary buffer */
1065 HeapFree(GetProcessHeap(), 0, pszFileW);
1066
1067 if (pfd)
1068 {
1069 ZeroMemory(pfd, sizeof(*pfd));
1070
1071 /* Copy the file data if a file path was returned */
1072 if (*pszFile)
1073 {
1074 DWORD len;
1075
1076 /* Copy the fixed part */
1077 CopyMemory(pfd, &wfd, FIELD_OFFSET(WIN32_FIND_DATAA, cFileName));
1078
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);
1086 }
1087 }
1088
1089 return hr;
1090 }
1091
1092 HRESULT STDMETHODCALLTYPE CShellLink::GetIDList(LPITEMIDLIST *ppidl)
1093 {
1094 TRACE("(%p)->(ppidl=%p)\n", this, ppidl);
1095
1096 if (!m_pPidl)
1097 {
1098 *ppidl = NULL;
1099 return S_FALSE;
1100 }
1101
1102 *ppidl = ILClone(m_pPidl);
1103 return S_OK;
1104 }
1105
1106 HRESULT STDMETHODCALLTYPE CShellLink::SetIDList(LPCITEMIDLIST pidl)
1107 {
1108 TRACE("(%p)->(pidl=%p)\n", this, pidl);
1109 return SetTargetFromPIDLOrPath(pidl, NULL);
1110 }
1111
1112 HRESULT STDMETHODCALLTYPE CShellLink::GetDescription(LPSTR pszName, INT cchMaxName)
1113 {
1114 TRACE("(%p)->(%p len=%u)\n", this, pszName, cchMaxName);
1115
1116 if (cchMaxName)
1117 *pszName = 0;
1118
1119 if (m_sDescription)
1120 WideCharToMultiByte(CP_ACP, 0, m_sDescription, -1,
1121 pszName, cchMaxName, NULL, NULL);
1122
1123 return S_OK;
1124 }
1125
1126 HRESULT STDMETHODCALLTYPE CShellLink::SetDescription(LPCSTR pszName)
1127 {
1128 TRACE("(%p)->(pName=%s)\n", this, pszName);
1129
1130 HeapFree(GetProcessHeap(), 0, m_sDescription);
1131 m_sDescription = NULL;
1132
1133 if (pszName)
1134 {
1135 m_sDescription = HEAP_strdupAtoW(GetProcessHeap(), 0, pszName);
1136 if (!m_sDescription)
1137 return E_OUTOFMEMORY;
1138 }
1139 m_bDirty = TRUE;
1140
1141 return S_OK;
1142 }
1143
1144 HRESULT STDMETHODCALLTYPE CShellLink::GetWorkingDirectory(LPSTR pszDir, INT cchMaxPath)
1145 {
1146 TRACE("(%p)->(%p len=%u)\n", this, pszDir, cchMaxPath);
1147
1148 if (cchMaxPath)
1149 *pszDir = 0;
1150
1151 if (m_sWorkDir)
1152 WideCharToMultiByte(CP_ACP, 0, m_sWorkDir, -1,
1153 pszDir, cchMaxPath, NULL, NULL);
1154
1155 return S_OK;
1156 }
1157
1158 HRESULT STDMETHODCALLTYPE CShellLink::SetWorkingDirectory(LPCSTR pszDir)
1159 {
1160 TRACE("(%p)->(dir=%s)\n", this, pszDir);
1161
1162 HeapFree(GetProcessHeap(), 0, m_sWorkDir);
1163 m_sWorkDir = NULL;
1164
1165 if (pszDir)
1166 {
1167 m_sWorkDir = HEAP_strdupAtoW(GetProcessHeap(), 0, pszDir);
1168 if (!m_sWorkDir)
1169 return E_OUTOFMEMORY;
1170 }
1171 m_bDirty = TRUE;
1172
1173 return S_OK;
1174 }
1175
1176 HRESULT STDMETHODCALLTYPE CShellLink::GetArguments(LPSTR pszArgs, INT cchMaxPath)
1177 {
1178 TRACE("(%p)->(%p len=%u)\n", this, pszArgs, cchMaxPath);
1179
1180 if (cchMaxPath)
1181 *pszArgs = 0;
1182
1183 if (m_sArgs)
1184 WideCharToMultiByte(CP_ACP, 0, m_sArgs, -1,
1185 pszArgs, cchMaxPath, NULL, NULL);
1186
1187 return S_OK;
1188 }
1189
1190 HRESULT STDMETHODCALLTYPE CShellLink::SetArguments(LPCSTR pszArgs)
1191 {
1192 TRACE("(%p)->(args=%s)\n", this, pszArgs);
1193
1194 HeapFree(GetProcessHeap(), 0, m_sArgs);
1195 m_sArgs = NULL;
1196
1197 if (pszArgs)
1198 {
1199 m_sArgs = HEAP_strdupAtoW(GetProcessHeap(), 0, pszArgs);
1200 if (!m_sArgs)
1201 return E_OUTOFMEMORY;
1202 }
1203 m_bDirty = TRUE;
1204
1205 return S_OK;
1206 }
1207
1208 HRESULT STDMETHODCALLTYPE CShellLink::GetHotkey(WORD *pwHotkey)
1209 {
1210 TRACE("(%p)->(%p)(0x%08x)\n", this, pwHotkey, m_Header.wHotKey);
1211 *pwHotkey = m_Header.wHotKey;
1212 return S_OK;
1213 }
1214
1215 HRESULT STDMETHODCALLTYPE CShellLink::SetHotkey(WORD wHotkey)
1216 {
1217 TRACE("(%p)->(hotkey=%x)\n", this, wHotkey);
1218
1219 m_Header.wHotKey = wHotkey;
1220 m_bDirty = TRUE;
1221
1222 return S_OK;
1223 }
1224
1225 HRESULT STDMETHODCALLTYPE CShellLink::GetShowCmd(INT *piShowCmd)
1226 {
1227 TRACE("(%p)->(%p) %d\n", this, piShowCmd, m_Header.nShowCommand);
1228 *piShowCmd = m_Header.nShowCommand;
1229 return S_OK;
1230 }
1231
1232 HRESULT STDMETHODCALLTYPE CShellLink::SetShowCmd(INT iShowCmd)
1233 {
1234 TRACE("(%p) %d\n", this, iShowCmd);
1235
1236 m_Header.nShowCommand = iShowCmd;
1237 m_bDirty = TRUE;
1238
1239 return S_OK;
1240 }
1241
1242 HRESULT STDMETHODCALLTYPE CShellLink::GetIconLocation(LPSTR pszIconPath, INT cchIconPath, INT *piIcon)
1243 {
1244 HRESULT hr;
1245 LPWSTR pszIconPathW;
1246
1247 TRACE("(%p)->(%p len=%u iicon=%p)\n", this, pszIconPath, cchIconPath, piIcon);
1248
1249 /* Allocate a temporary UNICODE buffer */
1250 pszIconPathW = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, cchIconPath * sizeof(WCHAR));
1251 if (!pszIconPathW)
1252 return E_OUTOFMEMORY;
1253
1254 /* Call the UNICODE function */
1255 hr = GetIconLocation(pszIconPathW, cchIconPath, piIcon);
1256
1257 /* Convert the file path back to ANSI */
1258 WideCharToMultiByte(CP_ACP, 0, pszIconPathW, -1,
1259 pszIconPath, cchIconPath, NULL, NULL);
1260
1261 /* Free the temporary buffer */
1262 HeapFree(GetProcessHeap(), 0, pszIconPathW);
1263
1264 return hr;
1265 }
1266
1267 HRESULT STDMETHODCALLTYPE CShellLink::GetIconLocation(UINT uFlags, PSTR pszIconFile, UINT cchMax, int *piIndex, UINT *pwFlags)
1268 {
1269 HRESULT hr;
1270 LPWSTR pszIconFileW;
1271
1272 TRACE("(%p)->(%u %p len=%u piIndex=%p pwFlags=%p)\n", this, uFlags, pszIconFile, cchMax, piIndex, pwFlags);
1273
1274 /* Allocate a temporary UNICODE buffer */
1275 pszIconFileW = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, cchMax * sizeof(WCHAR));
1276 if (!pszIconFileW)
1277 return E_OUTOFMEMORY;
1278
1279 /* Call the UNICODE function */
1280 hr = GetIconLocation(uFlags, pszIconFileW, cchMax, piIndex, pwFlags);
1281
1282 /* Convert the file path back to ANSI */
1283 WideCharToMultiByte(CP_ACP, 0, pszIconFileW, -1,
1284 pszIconFile, cchMax, NULL, NULL);
1285
1286 /* Free the temporary buffer */
1287 HeapFree(GetProcessHeap(), 0, pszIconFileW);
1288
1289 return hr;
1290 }
1291
1292 HRESULT STDMETHODCALLTYPE CShellLink::Extract(PCSTR pszFile, UINT nIconIndex, HICON *phiconLarge, HICON *phiconSmall, UINT nIconSize)
1293 {
1294 TRACE("(%p)->(path=%s iicon=%u)\n", this, pszFile, nIconIndex);
1295
1296 LPWSTR str = NULL;
1297 if (pszFile)
1298 {
1299 str = HEAP_strdupAtoW(GetProcessHeap(), 0, pszFile);
1300 if (!str)
1301 return E_OUTOFMEMORY;
1302 }
1303
1304 HRESULT hr = Extract(str, nIconIndex, phiconLarge, phiconSmall, nIconSize);
1305
1306 if (str)
1307 HeapFree(GetProcessHeap(), 0, str);
1308
1309 return hr;
1310 }
1311
1312 HRESULT STDMETHODCALLTYPE CShellLink::SetIconLocation(LPCSTR pszIconPath, INT iIcon)
1313 {
1314 TRACE("(%p)->(path=%s iicon=%u)\n", this, pszIconPath, iIcon);
1315
1316 LPWSTR str = NULL;
1317 if (pszIconPath)
1318 {
1319 str = HEAP_strdupAtoW(GetProcessHeap(), 0, pszIconPath);
1320 if (!str)
1321 return E_OUTOFMEMORY;
1322 }
1323
1324 HRESULT hr = SetIconLocation(str, iIcon);
1325
1326 if (str)
1327 HeapFree(GetProcessHeap(), 0, str);
1328
1329 return hr;
1330 }
1331
1332 HRESULT STDMETHODCALLTYPE CShellLink::SetRelativePath(LPCSTR pszPathRel, DWORD dwReserved)
1333 {
1334 TRACE("(%p)->(path=%s %x)\n", this, pszPathRel, dwReserved);
1335
1336 HeapFree(GetProcessHeap(), 0, m_sPathRel);
1337 m_sPathRel = NULL;
1338
1339 if (pszPathRel)
1340 {
1341 m_sPathRel = HEAP_strdupAtoW(GetProcessHeap(), 0, pszPathRel);
1342 m_bDirty = TRUE;
1343 }
1344
1345 return ShellLink_UpdatePath(m_sPathRel, m_sPath, m_sWorkDir, &m_sPath);
1346 }
1347
1348 static LPWSTR
1349 shelllink_get_msi_component_path(LPWSTR component)
1350 {
1351 DWORD Result, sz = 0;
1352
1353 Result = CommandLineFromMsiDescriptor(component, NULL, &sz);
1354 if (Result != ERROR_SUCCESS)
1355 return NULL;
1356
1357 sz++;
1358 LPWSTR path = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, sz * sizeof(WCHAR));
1359 Result = CommandLineFromMsiDescriptor(component, path, &sz);
1360 if (Result != ERROR_SUCCESS)
1361 {
1362 HeapFree(GetProcessHeap(), 0, path);
1363 path = NULL;
1364 }
1365
1366 TRACE("returning %s\n", debugstr_w(path));
1367
1368 return path;
1369 }
1370
1371 HRESULT STDMETHODCALLTYPE CShellLink::Resolve(HWND hwnd, DWORD fFlags)
1372 {
1373 HRESULT hr = S_OK;
1374 BOOL bSuccess;
1375
1376 TRACE("(%p)->(hwnd=%p flags=%x)\n", this, hwnd, fFlags);
1377
1378 /* FIXME: use IResolveShellLink interface? */
1379
1380 // FIXME: See InvokeCommand().
1381
1382 #if (NTDDI_VERSION < NTDDI_LONGHORN)
1383 // NOTE: For Logo3 (EXP_LOGO3_ID_SIG), check also for SHRestricted(REST_NOLOGO3CHANNELNOTIFY)
1384 if (m_Header.dwFlags & SLDF_HAS_LOGO3ID)
1385 {
1386 FIXME("Logo3 links are not supported yet!\n");
1387 return E_FAIL;
1388 }
1389 #endif
1390
1391 /* Resolve Darwin (MSI) target */
1392 if (m_Header.dwFlags & SLDF_HAS_DARWINID)
1393 {
1394 LPWSTR component = NULL;
1395 hr = GetAdvertiseInfo(&component, EXP_DARWIN_ID_SIG);
1396 if (FAILED(hr))
1397 return E_FAIL;
1398
1399 /* Clear the cached path */
1400 HeapFree(GetProcessHeap(), 0, m_sPath);
1401 m_sPath = shelllink_get_msi_component_path(component);
1402 if (!m_sPath)
1403 return E_FAIL;
1404 }
1405
1406 if (!m_sPath && m_pPidl)
1407 {
1408 WCHAR buffer[MAX_PATH];
1409
1410 bSuccess = SHGetPathFromIDListW(m_pPidl, buffer);
1411 if (bSuccess && *buffer)
1412 {
1413 m_sPath = strdupW(buffer);
1414 if (!m_sPath)
1415 return E_OUTOFMEMORY;
1416
1417 m_bDirty = TRUE;
1418 }
1419 else
1420 {
1421 hr = S_OK; /* don't report an error occurred while just caching information */
1422 }
1423 }
1424
1425 // FIXME: Strange to do that here...
1426 if (!m_sIcoPath && m_sPath)
1427 {
1428 m_sIcoPath = strdupW(m_sPath);
1429 if (!m_sIcoPath)
1430 return E_OUTOFMEMORY;
1431
1432 m_Header.nIconIndex = 0;
1433
1434 m_bDirty = TRUE;
1435 }
1436
1437 return hr;
1438 }
1439
1440 HRESULT STDMETHODCALLTYPE CShellLink::SetPath(LPCSTR pszFile)
1441 {
1442 TRACE("(%p)->(path=%s)\n", this, pszFile);
1443
1444 if (!pszFile)
1445 return E_INVALIDARG;
1446
1447 LPWSTR str = HEAP_strdupAtoW(GetProcessHeap(), 0, pszFile);
1448 if (!str)
1449 return E_OUTOFMEMORY;
1450
1451 HRESULT hr = SetPath(str);
1452 HeapFree(GetProcessHeap(), 0, str);
1453
1454 return hr;
1455 }
1456
1457 HRESULT STDMETHODCALLTYPE CShellLink::GetPath(LPWSTR pszFile, INT cchMaxPath, WIN32_FIND_DATAW *pfd, DWORD fFlags)
1458 {
1459 WCHAR buffer[MAX_PATH];
1460
1461 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%u)(%s)\n",
1462 this, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(m_sPath));
1463
1464 if (cchMaxPath)
1465 *pszFile = 0;
1466 // FIXME: What if cchMaxPath == 0 , or pszFile == NULL ??
1467
1468 // FIXME: What about Darwin??
1469
1470 /*
1471 * Retrieve the path to the target from the PIDL (if we have one).
1472 * NOTE: Do NOT use the cached path (m_sPath from link info).
1473 */
1474 if (m_pPidl && SHGetPathFromIDListW(m_pPidl, buffer))
1475 {
1476 if (fFlags & SLGP_SHORTPATH)
1477 GetShortPathNameW(buffer, buffer, _countof(buffer));
1478 // FIXME: Add support for SLGP_UNCPRIORITY
1479 }
1480 else
1481 {
1482 *buffer = 0;
1483 }
1484
1485 /* If we have a FindData structure, initialize it */
1486 if (pfd)
1487 {
1488 ZeroMemory(pfd, sizeof(*pfd));
1489
1490 /* Copy the file data if the target is a file path */
1491 if (*buffer)
1492 {
1493 pfd->dwFileAttributes = m_Header.dwFileAttributes;
1494 pfd->ftCreationTime = m_Header.ftCreationTime;
1495 pfd->ftLastAccessTime = m_Header.ftLastAccessTime;
1496 pfd->ftLastWriteTime = m_Header.ftLastWriteTime;
1497 pfd->nFileSizeHigh = 0;
1498 pfd->nFileSizeLow = m_Header.nFileSizeLow;
1499
1500 /*
1501 * Build temporarily a short path in pfd->cFileName (of size MAX_PATH),
1502 * then extract and store the short file name in pfd->cAlternateFileName.
1503 */
1504 GetShortPathNameW(buffer, pfd->cFileName, _countof(pfd->cFileName));
1505 lstrcpynW(pfd->cAlternateFileName,
1506 PathFindFileNameW(pfd->cFileName),
1507 _countof(pfd->cAlternateFileName));
1508
1509 /* Now extract and store the long file name in pfd->cFileName */
1510 lstrcpynW(pfd->cFileName,
1511 PathFindFileNameW(buffer),
1512 _countof(pfd->cFileName));
1513 }
1514 }
1515
1516 /* Finally check if we have a raw path the user actually wants to retrieve */
1517 if ((fFlags & SLGP_RAWPATH) && (m_Header.dwFlags & SLDF_HAS_EXP_SZ))
1518 {
1519 /* Search for a target environment block */
1520 LPEXP_SZ_LINK pInfo;
1521 pInfo = (LPEXP_SZ_LINK)SHFindDataBlock(m_pDBList, EXP_SZ_LINK_SIG);
1522 if (pInfo && (pInfo->cbSize == sizeof(*pInfo)))
1523 lstrcpynW(buffer, pInfo->szwTarget, cchMaxPath);
1524 }
1525
1526 /* For diagnostics purposes only... */
1527 // NOTE: SLGP_UNCPRIORITY is unsupported
1528 fFlags &= ~(SLGP_RAWPATH | SLGP_SHORTPATH);
1529 if (fFlags) FIXME("(%p): Unsupported flags %lu\n", this, fFlags);
1530
1531 /* Copy the data back to the user */
1532 if (*buffer)
1533 lstrcpynW(pszFile, buffer, cchMaxPath);
1534
1535 return (*buffer ? S_OK : S_FALSE);
1536 }
1537
1538 HRESULT STDMETHODCALLTYPE CShellLink::GetDescription(LPWSTR pszName, INT cchMaxName)
1539 {
1540 TRACE("(%p)->(%p len=%u)\n", this, pszName, cchMaxName);
1541
1542 *pszName = 0;
1543 if (m_sDescription)
1544 lstrcpynW(pszName, m_sDescription, cchMaxName);
1545
1546 return S_OK;
1547 }
1548
1549 HRESULT STDMETHODCALLTYPE CShellLink::SetDescription(LPCWSTR pszName)
1550 {
1551 TRACE("(%p)->(desc=%s)\n", this, debugstr_w(pszName));
1552
1553 HeapFree(GetProcessHeap(), 0, m_sDescription);
1554 m_sDescription = NULL;
1555
1556 if (pszName)
1557 {
1558 m_sDescription = strdupW(pszName);
1559 if (!m_sDescription)
1560 return E_OUTOFMEMORY;
1561 }
1562 m_bDirty = TRUE;
1563
1564 return S_OK;
1565 }
1566
1567 HRESULT STDMETHODCALLTYPE CShellLink::GetWorkingDirectory(LPWSTR pszDir, INT cchMaxPath)
1568 {
1569 TRACE("(%p)->(%p len %u)\n", this, pszDir, cchMaxPath);
1570
1571 if (cchMaxPath)
1572 *pszDir = 0;
1573
1574 if (m_sWorkDir)
1575 lstrcpynW(pszDir, m_sWorkDir, cchMaxPath);
1576
1577 return S_OK;
1578 }
1579
1580 HRESULT STDMETHODCALLTYPE CShellLink::SetWorkingDirectory(LPCWSTR pszDir)
1581 {
1582 TRACE("(%p)->(dir=%s)\n", this, debugstr_w(pszDir));
1583
1584 HeapFree(GetProcessHeap(), 0, m_sWorkDir);
1585 m_sWorkDir = NULL;
1586
1587 if (pszDir)
1588 {
1589 m_sWorkDir = strdupW(pszDir);
1590 if (!m_sWorkDir)
1591 return E_OUTOFMEMORY;
1592 }
1593 m_bDirty = TRUE;
1594
1595 return S_OK;
1596 }
1597
1598 HRESULT STDMETHODCALLTYPE CShellLink::GetArguments(LPWSTR pszArgs, INT cchMaxPath)
1599 {
1600 TRACE("(%p)->(%p len=%u)\n", this, pszArgs, cchMaxPath);
1601
1602 if (cchMaxPath)
1603 *pszArgs = 0;
1604
1605 if (m_sArgs)
1606 lstrcpynW(pszArgs, m_sArgs, cchMaxPath);
1607
1608 return S_OK;
1609 }
1610
1611 HRESULT STDMETHODCALLTYPE CShellLink::SetArguments(LPCWSTR pszArgs)
1612 {
1613 TRACE("(%p)->(args=%s)\n", this, debugstr_w(pszArgs));
1614
1615 HeapFree(GetProcessHeap(), 0, m_sArgs);
1616 m_sArgs = NULL;
1617
1618 if (pszArgs)
1619 {
1620 m_sArgs = strdupW(pszArgs);
1621 if (!m_sArgs)
1622 return E_OUTOFMEMORY;
1623 }
1624 m_bDirty = TRUE;
1625
1626 return S_OK;
1627 }
1628
1629 HRESULT STDMETHODCALLTYPE CShellLink::GetIconLocation(LPWSTR pszIconPath, INT cchIconPath, INT *piIcon)
1630 {
1631 TRACE("(%p)->(%p len=%u iicon=%p)\n", this, pszIconPath, cchIconPath, piIcon);
1632
1633 if (cchIconPath)
1634 *pszIconPath = 0;
1635
1636 *piIcon = 0;
1637
1638 /* Update the original icon path location */
1639 if (m_Header.dwFlags & SLDF_HAS_EXP_ICON_SZ)
1640 {
1641 WCHAR szPath[MAX_PATH];
1642
1643 /* Search for an icon environment block */
1644 LPEXP_SZ_LINK pInfo;
1645 pInfo = (LPEXP_SZ_LINK)SHFindDataBlock(m_pDBList, EXP_SZ_ICON_SIG);
1646 if (pInfo && (pInfo->cbSize == sizeof(*pInfo)))
1647 {
1648 SHExpandEnvironmentStringsW(pInfo->szwTarget, szPath, _countof(szPath));
1649
1650 m_Header.dwFlags &= ~SLDF_HAS_ICONLOCATION;
1651 HeapFree(GetProcessHeap(), 0, m_sIcoPath);
1652
1653 m_sIcoPath = strdupW(szPath);
1654 if (!m_sIcoPath)
1655 return E_OUTOFMEMORY;
1656
1657 m_Header.dwFlags |= SLDF_HAS_ICONLOCATION;
1658
1659 m_bDirty = TRUE;
1660 }
1661 }
1662
1663 *piIcon = m_Header.nIconIndex;
1664
1665 if (m_sIcoPath)
1666 lstrcpynW(pszIconPath, m_sIcoPath, cchIconPath);
1667
1668 return S_OK;
1669 }
1670
1671 static HRESULT SHELL_PidlGetIconLocationW(PCIDLIST_ABSOLUTE pidl,
1672 UINT uFlags, PWSTR pszIconFile, UINT cchMax, int *piIndex, UINT *pwFlags)
1673 {
1674 LPCITEMIDLIST pidlLast;
1675 CComPtr<IShellFolder> psf;
1676
1677 HRESULT hr = SHBindToParent(pidl, IID_PPV_ARG(IShellFolder, &psf), &pidlLast);
1678 if (FAILED_UNEXPECTEDLY(hr))
1679 return hr;
1680
1681 CComPtr<IExtractIconW> pei;
1682 hr = psf->GetUIObjectOf(0, 1, &pidlLast, IID_NULL_PPV_ARG(IExtractIconW, &pei));
1683 if (FAILED_UNEXPECTEDLY(hr))
1684 return hr;
1685
1686 hr = pei->GetIconLocation(uFlags, pszIconFile, cchMax, piIndex, pwFlags);
1687 if (FAILED_UNEXPECTEDLY(hr))
1688 return hr;
1689
1690 return S_OK;
1691 }
1692
1693 HRESULT STDMETHODCALLTYPE CShellLink::GetIconLocation(UINT uFlags, PWSTR pszIconFile, UINT cchMax, int *piIndex, UINT *pwFlags)
1694 {
1695 HRESULT hr;
1696
1697 pszIconFile[0] = UNICODE_NULL;
1698
1699 /*
1700 * It is possible for a shell link to point to another shell link,
1701 * and in particular there is the possibility to point to itself.
1702 * Now, suppose we ask such a link to retrieve its associated icon.
1703 * This function would be called, and due to COM would be called again
1704 * recursively. To solve this issue, we forbid calling GetIconLocation()
1705 * with GIL_FORSHORTCUT set in uFlags, as done by Windows (shown by tests).
1706 */
1707 if (uFlags & GIL_FORSHORTCUT)
1708 return E_INVALIDARG;
1709
1710 /*
1711 * Now, we set GIL_FORSHORTCUT so that: i) we allow the icon extractor
1712 * of the target to give us a suited icon, and ii) we protect ourselves
1713 * against recursive call.
1714 */
1715 uFlags |= GIL_FORSHORTCUT;
1716
1717 if (uFlags & GIL_DEFAULTICON)
1718 return E_FAIL;
1719
1720 hr = GetIconLocation(pszIconFile, cchMax, piIndex);
1721 if (FAILED(hr) || pszIconFile[0] == UNICODE_NULL)
1722 {
1723 hr = SHELL_PidlGetIconLocationW(m_pPidl, uFlags, pszIconFile, cchMax, piIndex, pwFlags);
1724 }
1725 else
1726 {
1727 *pwFlags = GIL_NOTFILENAME | GIL_PERCLASS;
1728 }
1729
1730 return hr;
1731 }
1732
1733 HRESULT STDMETHODCALLTYPE
1734 CShellLink::Extract(PCWSTR pszFile, UINT nIconIndex, HICON *phiconLarge, HICON *phiconSmall, UINT nIconSize)
1735 {
1736 HRESULT hr = NOERROR;
1737 UINT cxyLarge = LOWORD(nIconSize), cxySmall = HIWORD(nIconSize);
1738
1739 if (phiconLarge)
1740 {
1741 *phiconLarge = NULL;
1742 PrivateExtractIconsW(pszFile, nIconIndex, cxyLarge, cxyLarge, phiconLarge, NULL, 1, 0);
1743
1744 if (*phiconLarge == NULL)
1745 hr = S_FALSE;
1746 }
1747
1748 if (phiconSmall)
1749 {
1750 *phiconSmall = NULL;
1751 PrivateExtractIconsW(pszFile, nIconIndex, cxySmall, cxySmall, phiconSmall, NULL, 1, 0);
1752
1753 if (*phiconSmall == NULL)
1754 hr = S_FALSE;
1755 }
1756
1757 if (hr == S_FALSE)
1758 {
1759 if (phiconLarge && *phiconLarge)
1760 {
1761 DestroyIcon(*phiconLarge);
1762 *phiconLarge = NULL;
1763 }
1764 if (phiconSmall && *phiconSmall)
1765 {
1766 DestroyIcon(*phiconSmall);
1767 *phiconSmall = NULL;
1768 }
1769 }
1770
1771 return hr;
1772 }
1773
1774 #if 0
1775 /* Extends the functionality of PathUnExpandEnvStringsW */
1776 BOOL PathFullyUnExpandEnvStringsW(
1777 _In_ LPCWSTR pszPath,
1778 _Out_ LPWSTR pszBuf,
1779 _In_ UINT cchBuf)
1780 {
1781 BOOL Ret = FALSE; // Set to TRUE as soon as PathUnExpandEnvStrings starts unexpanding.
1782 BOOL res;
1783 LPCWSTR p;
1784
1785 // *pszBuf = L'\0';
1786 while (*pszPath && cchBuf > 0)
1787 {
1788 /* Attempt unexpanding the path */
1789 res = PathUnExpandEnvStringsW(pszPath, pszBuf, cchBuf);
1790 if (!res)
1791 {
1792 /* The unexpansion failed. Try to find a path delimiter. */
1793 p = wcspbrk(pszPath, L" /\\:*?\"<>|%");
1794 if (!p) /* None found, we will copy the remaining path */
1795 p = pszPath + wcslen(pszPath);
1796 else /* Found one, we will copy the delimiter and skip it */
1797 ++p;
1798 /* If we overflow, we cannot unexpand more, so return FALSE */
1799 if (p - pszPath >= cchBuf)
1800 return FALSE; // *pszBuf = L'\0';
1801
1802 /* Copy the untouched portion of path up to the delimiter, included */
1803 wcsncpy(pszBuf, pszPath, p - pszPath);
1804 pszBuf[p - pszPath] = L'\0'; // NULL-terminate
1805
1806 /* Advance the pointers and decrease the remaining buffer size */
1807 cchBuf -= (p - pszPath);
1808 pszBuf += (p - pszPath);
1809 pszPath += (p - pszPath);
1810 }
1811 else
1812 {
1813 /*
1814 * The unexpansion succeeded. Skip the unexpanded part by trying
1815 * to find where the original path and the unexpanded string
1816 * become different.
1817 * NOTE: An alternative(?) would be to stop also at the last
1818 * path delimiter encountered in the loop (i.e. would be the
1819 * first path delimiter in the strings).
1820 */
1821 LPWSTR q;
1822
1823 /*
1824 * The algorithm starts at the end of the strings and loops back
1825 * while the characters are equal, until it finds a discrepancy.
1826 */
1827 p = pszPath + wcslen(pszPath);
1828 q = pszBuf + wcslen(pszBuf); // This wcslen should be < cchBuf
1829 while ((*p == *q) && (p > pszPath) && (q > pszBuf))
1830 {
1831 --p; --q;
1832 }
1833 /* Skip discrepancy */
1834 ++p; ++q;
1835
1836 /* Advance the pointers and decrease the remaining buffer size */
1837 cchBuf -= (q - pszBuf);
1838 pszBuf = q;
1839 pszPath = p;
1840
1841 Ret = TRUE;
1842 }
1843 }
1844
1845 return Ret;
1846 }
1847 #endif
1848
1849 HRESULT STDMETHODCALLTYPE CShellLink::SetIconLocation(LPCWSTR pszIconPath, INT iIcon)
1850 {
1851 HRESULT hr = E_FAIL;
1852 WCHAR szUnExpIconPath[MAX_PATH];
1853 BOOL bSuccess;
1854
1855 TRACE("(%p)->(path=%s iicon=%u)\n", this, debugstr_w(pszIconPath), iIcon);
1856
1857 if (pszIconPath)
1858 {
1859 /* Try to fully unexpand the icon path */
1860
1861 /*
1862 * Check whether the user-given file path contains unexpanded
1863 * environment variables. If so, create a target environment block.
1864 * Note that in this block we will store the user-given path.
1865 * It will contain the unexpanded environment variables, but
1866 * it can also contain already expanded path that the user does
1867 * not want to see them unexpanded (e.g. so that they always
1868 * refer to the same place even if the would-be corresponding
1869 * environment variable could change).
1870 */
1871 // FIXME: http://stackoverflow.com/questions/2976489/ishelllinkseticonlocation-translates-my-icon-path-into-program-files-which-i
1872 // if (PathFullyUnExpandEnvStringsW(pszIconPath, szUnExpIconPath, _countof(szUnExpIconPath)))
1873 bSuccess = PathUnExpandEnvStringsW(pszIconPath, szUnExpIconPath, _countof(szUnExpIconPath));
1874 if (bSuccess && wcscmp(pszIconPath, szUnExpIconPath) != 0)
1875 {
1876 /* Unexpansion succeeded, so we need an icon environment block */
1877 EXP_SZ_LINK buffer;
1878 LPEXP_SZ_LINK pInfo;
1879
1880 pszIconPath = szUnExpIconPath;
1881 pInfo = (LPEXP_SZ_LINK)SHFindDataBlock(m_pDBList, EXP_SZ_ICON_SIG);
1882 if (pInfo)
1883 {
1884 /* Make sure that the size of the structure is valid */
1885 if (pInfo->cbSize != sizeof(*pInfo))
1886 {
1887 ERR("Ooops. This structure is not as expected...\n");
1888
1889 /* Invalid structure, remove it altogether */
1890 m_Header.dwFlags &= ~SLDF_HAS_EXP_ICON_SZ;
1891 RemoveDataBlock(EXP_SZ_ICON_SIG);
1892
1893 /* Reset the pointer and go use the static buffer */
1894 pInfo = NULL;
1895 }
1896 }
1897 if (!pInfo)
1898 {
1899 /* Use the static buffer */
1900 pInfo = &buffer;
1901 buffer.cbSize = sizeof(buffer);
1902 buffer.dwSignature = EXP_SZ_ICON_SIG;
1903 }
1904
1905 lstrcpynW(pInfo->szwTarget, szUnExpIconPath, _countof(pInfo->szwTarget));
1906 WideCharToMultiByte(CP_ACP, 0, szUnExpIconPath, -1,
1907 pInfo->szTarget, _countof(pInfo->szTarget), NULL, NULL);
1908
1909 hr = S_OK;
1910 if (pInfo == &buffer)
1911 hr = AddDataBlock(pInfo);
1912 if (hr == S_OK)
1913 m_Header.dwFlags |= SLDF_HAS_EXP_ICON_SZ;
1914 }
1915 else
1916 {
1917 /* Unexpansion failed, so we need to remove any icon environment block */
1918 m_Header.dwFlags &= ~SLDF_HAS_EXP_ICON_SZ;
1919 RemoveDataBlock(EXP_SZ_ICON_SIG);
1920 }
1921 }
1922
1923 /* Store the original icon path location (this one may contain unexpanded environment strings) */
1924 if (pszIconPath)
1925 {
1926 m_Header.dwFlags &= ~SLDF_HAS_ICONLOCATION;
1927 HeapFree(GetProcessHeap(), 0, m_sIcoPath);
1928
1929 m_sIcoPath = strdupW(pszIconPath);
1930 if (!m_sIcoPath)
1931 return E_OUTOFMEMORY;
1932
1933 m_Header.dwFlags |= SLDF_HAS_ICONLOCATION;
1934 }
1935
1936 hr = S_OK;
1937
1938 m_Header.nIconIndex = iIcon;
1939 m_bDirty = TRUE;
1940
1941 return hr;
1942 }
1943
1944 HRESULT STDMETHODCALLTYPE CShellLink::SetRelativePath(LPCWSTR pszPathRel, DWORD dwReserved)
1945 {
1946 TRACE("(%p)->(path=%s %x)\n", this, debugstr_w(pszPathRel), dwReserved);
1947
1948 HeapFree(GetProcessHeap(), 0, m_sPathRel);
1949 m_sPathRel = NULL;
1950
1951 if (pszPathRel)
1952 {
1953 m_sPathRel = strdupW(pszPathRel);
1954 if (!m_sPathRel)
1955 return E_OUTOFMEMORY;
1956 }
1957 m_bDirty = TRUE;
1958
1959 return ShellLink_UpdatePath(m_sPathRel, m_sPath, m_sWorkDir, &m_sPath);
1960 }
1961
1962 static LPWSTR GetAdvertisedArg(LPCWSTR str)
1963 {
1964 if (!str)
1965 return NULL;
1966
1967 LPCWSTR p = wcschr(str, L':');
1968 if (!p)
1969 return NULL;
1970
1971 DWORD len = p - str;
1972 LPWSTR ret = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * (len + 1));
1973 if (!ret)
1974 return ret;
1975
1976 memcpy(ret, str, sizeof(WCHAR)*len);
1977 ret[len] = 0;
1978 return ret;
1979 }
1980
1981 HRESULT CShellLink::WriteAdvertiseInfo(LPCWSTR string, DWORD dwSig)
1982 {
1983 EXP_DARWIN_LINK buffer;
1984 LPEXP_DARWIN_LINK pInfo;
1985
1986 if ( (dwSig != EXP_DARWIN_ID_SIG)
1987 #if (NTDDI_VERSION < NTDDI_LONGHORN)
1988 && (dwSig != EXP_LOGO3_ID_SIG)
1989 #endif
1990 )
1991 {
1992 return E_INVALIDARG;
1993 }
1994
1995 if (!string)
1996 return S_FALSE;
1997
1998 pInfo = (LPEXP_DARWIN_LINK)SHFindDataBlock(m_pDBList, dwSig);
1999 if (pInfo)
2000 {
2001 /* Make sure that the size of the structure is valid */
2002 if (pInfo->dbh.cbSize != sizeof(*pInfo))
2003 {
2004 ERR("Ooops. This structure is not as expected...\n");
2005
2006 /* Invalid structure, remove it altogether */
2007 if (dwSig == EXP_DARWIN_ID_SIG)
2008 m_Header.dwFlags &= ~SLDF_HAS_DARWINID;
2009 #if (NTDDI_VERSION < NTDDI_LONGHORN)
2010 else if (dwSig == EXP_LOGO3_ID_SIG)
2011 m_Header.dwFlags &= ~SLDF_HAS_LOGO3ID;
2012 #endif
2013 RemoveDataBlock(dwSig);
2014
2015 /* Reset the pointer and go use the static buffer */
2016 pInfo = NULL;
2017 }
2018 }
2019 if (!pInfo)
2020 {
2021 /* Use the static buffer */
2022 pInfo = &buffer;
2023 buffer.dbh.cbSize = sizeof(buffer);
2024 buffer.dbh.dwSignature = dwSig;
2025 }
2026
2027 lstrcpynW(pInfo->szwDarwinID, string, _countof(pInfo->szwDarwinID));
2028 WideCharToMultiByte(CP_ACP, 0, string, -1,
2029 pInfo->szDarwinID, _countof(pInfo->szDarwinID), NULL, NULL);
2030
2031 HRESULT hr = S_OK;
2032 if (pInfo == &buffer)
2033 hr = AddDataBlock(pInfo);
2034 if (hr == S_OK)
2035 {
2036 if (dwSig == EXP_DARWIN_ID_SIG)
2037 m_Header.dwFlags |= SLDF_HAS_DARWINID;
2038 #if (NTDDI_VERSION < NTDDI_LONGHORN)
2039 else if (dwSig == EXP_LOGO3_ID_SIG)
2040 m_Header.dwFlags |= SLDF_HAS_LOGO3ID;
2041 #endif
2042 }
2043
2044 return hr;
2045 }
2046
2047 HRESULT CShellLink::SetAdvertiseInfo(LPCWSTR str)
2048 {
2049 HRESULT hr;
2050 LPCWSTR szComponent = NULL, szProduct = NULL, p;
2051 INT len;
2052 GUID guid;
2053 WCHAR szGuid[38+1];
2054
2055 /**/sProduct = sComponent = NULL;/**/
2056
2057 while (str[0])
2058 {
2059 /* each segment must start with two colons */
2060 if (str[0] != ':' || str[1] != ':')
2061 return E_FAIL;
2062
2063 /* the last segment is just two colons */
2064 if (!str[2])
2065 break;
2066 str += 2;
2067
2068 /* there must be a colon straight after a guid */
2069 p = wcschr(str, L':');
2070 if (!p)
2071 return E_FAIL;
2072 len = p - str;
2073 if (len != 38)
2074 return E_FAIL;
2075
2076 /* get the guid, and check if it's validly formatted */
2077 memcpy(szGuid, str, sizeof(WCHAR)*len);
2078 szGuid[len] = 0;
2079
2080 hr = CLSIDFromString(szGuid, &guid);
2081 if (hr != S_OK)
2082 return hr;
2083 str = p + 1;
2084
2085 /* match it up to a guid that we care about */
2086 if (IsEqualGUID(guid, SHELL32_AdvtShortcutComponent) && !szComponent)
2087 szComponent = str; /* Darwin */
2088 else if (IsEqualGUID(guid, SHELL32_AdvtShortcutProduct) && !szProduct)
2089 szProduct = str; /* Logo3 */
2090 else
2091 return E_FAIL;
2092
2093 /* skip to the next field */
2094 str = wcschr(str, L':');
2095 if (!str)
2096 return E_FAIL;
2097 }
2098
2099 /* we have to have a component for an advertised shortcut */
2100 if (!szComponent)
2101 return E_FAIL;
2102
2103 szComponent = GetAdvertisedArg(szComponent);
2104 szProduct = GetAdvertisedArg(szProduct);
2105
2106 hr = WriteAdvertiseInfo(szComponent, EXP_DARWIN_ID_SIG);
2107 // if (FAILED(hr))
2108 // return hr;
2109 #if (NTDDI_VERSION < NTDDI_LONGHORN)
2110 hr = WriteAdvertiseInfo(szProduct, EXP_LOGO3_ID_SIG);
2111 // if (FAILED(hr))
2112 // return hr;
2113 #endif
2114
2115 HeapFree(GetProcessHeap(), 0, (PVOID)szComponent);
2116 HeapFree(GetProcessHeap(), 0, (PVOID)szProduct);
2117
2118 if (TRACE_ON(shell))
2119 {
2120 GetAdvertiseInfo(&sComponent, EXP_DARWIN_ID_SIG);
2121 TRACE("Component = %s\n", debugstr_w(sComponent));
2122 #if (NTDDI_VERSION < NTDDI_LONGHORN)
2123 GetAdvertiseInfo(&sProduct, EXP_LOGO3_ID_SIG);
2124 TRACE("Product = %s\n", debugstr_w(sProduct));
2125 #endif
2126 }
2127
2128 return S_OK;
2129 }
2130
2131 /*
2132 * Since the real PathResolve (from Wine) is unimplemented at the moment,
2133 * we use this local implementation, until a better one is written (using
2134 * code parts of the SHELL_xxx helpers in Wine's shellpath.c).
2135 */
2136 static BOOL HACKISH_PathResolve(
2137 IN OUT PWSTR pszPath,
2138 IN PZPCWSTR dirs OPTIONAL,
2139 IN UINT fFlags)
2140 {
2141 // FIXME: This is unimplemented!!!
2142 #if 0
2143 return PathResolve(pszPath, dirs, fFlags);
2144 #else
2145 BOOL Success = FALSE;
2146 USHORT i;
2147 LPWSTR fname = NULL;
2148 WCHAR szPath[MAX_PATH];
2149
2150 /* First, search for a valid existing path */
2151
2152 // NOTE: See also: SHELL_FindExecutable()
2153
2154 /*
2155 * List of extensions searched for, by PathResolve with the flag
2156 * PRF_TRYPROGRAMEXTENSIONS == PRF_EXECUTABLE | PRF_VERIFYEXISTS set,
2157 * according to MSDN: https://msdn.microsoft.com/en-us/library/windows/desktop/bb776478(v=vs.85).aspx
2158 */
2159 static PCWSTR Extensions[] = {L".pif", L".com", L".bat", L".cmd", L".lnk", L".exe", NULL};
2160 #define LNK_EXT_INDEX 4 // ".lnk" has index 4 in the array above
2161
2162 /*
2163 * Start at the beginning of the list if PRF_EXECUTABLE is set, otherwise
2164 * just use the last element 'NULL' (no extension checking).
2165 */
2166 i = ((fFlags & PRF_EXECUTABLE) ? 0 : _countof(Extensions) - 1);
2167 for (; i < _countof(Extensions); ++i)
2168 {
2169 /* Ignore shell links ".lnk" if needed */
2170 if ((fFlags & PRF_DONTFINDLNK) && (i == LNK_EXT_INDEX))
2171 continue;
2172
2173 Success = (SearchPathW(NULL, pszPath, Extensions[i],
2174 _countof(szPath), szPath, NULL) != 0);
2175 if (!Success)
2176 {
2177 ERR("SearchPathW(pszPath = '%S') failed\n", pszPath);
2178 }
2179 else
2180 {
2181 ERR("SearchPathW(pszPath = '%S', szPath = '%S') succeeded\n", pszPath, szPath);
2182 break;
2183 }
2184 }
2185
2186 if (!Success)
2187 {
2188 ERR("SearchPathW(pszPath = '%S') failed\n", pszPath);
2189
2190 /* We failed, try with PathFindOnPath, as explained by MSDN */
2191 // Success = PathFindOnPathW(pszPath, dirs);
2192 StringCchCopyW(szPath, _countof(szPath), pszPath);
2193 Success = PathFindOnPathW(szPath, dirs);
2194 if (!Success)
2195 {
2196 ERR("PathFindOnPathW(pszPath = '%S') failed\n", pszPath);
2197
2198 /* We failed again, fall back to building a possible non-existing path */
2199 if (!GetFullPathNameW(pszPath, _countof(szPath), szPath, &fname))
2200 {
2201 ERR("GetFullPathNameW(pszPath = '%S') failed\n", pszPath);
2202 return FALSE;
2203 }
2204
2205 Success = PathFileExistsW(szPath);
2206 if (!Success)
2207 ERR("PathFileExistsW(szPath = '%S') failed\n", szPath);
2208
2209 /******************************************************/
2210 /* Question: Why this line is needed only for files?? */
2211 if (fname && (_wcsicmp(pszPath, fname) == 0))
2212 *szPath = L'\0';
2213 /******************************************************/
2214 }
2215 else
2216 {
2217 ERR("PathFindOnPathW(pszPath = '%S' ==> '%S') succeeded\n", pszPath, szPath);
2218 }
2219 }
2220
2221 /* Copy back the results to the caller */
2222 StringCchCopyW(pszPath, MAX_PATH, szPath);
2223
2224 /*
2225 * Since the called functions always checked whether the file path existed,
2226 * we do not need to redo a final check: we can use instead the cached
2227 * result in 'Success'.
2228 */
2229 return ((fFlags & PRF_VERIFYEXISTS) ? Success : TRUE);
2230 #endif
2231 }
2232
2233 HRESULT CShellLink::SetTargetFromPIDLOrPath(LPCITEMIDLIST pidl, LPCWSTR pszFile)
2234 {
2235 HRESULT hr = S_OK;
2236 LPITEMIDLIST pidlNew = NULL;
2237 WCHAR szPath[MAX_PATH];
2238
2239 /*
2240 * Not both 'pidl' and 'pszFile' should be set.
2241 * But either one or both can be NULL.
2242 */
2243 if (pidl && pszFile)
2244 return E_FAIL;
2245
2246 if (pidl)
2247 {
2248 /* Clone the PIDL */
2249 pidlNew = ILClone(pidl);
2250 if (!pidlNew)
2251 return E_FAIL;
2252 }
2253 else if (pszFile)
2254 {
2255 /* Build a PIDL for this path target */
2256 hr = SHILCreateFromPathW(pszFile, &pidlNew, NULL);
2257 if (FAILED(hr))
2258 {
2259 /* This failed, try to resolve the path, then create a simple PIDL */
2260
2261 StringCchCopyW(szPath, _countof(szPath), pszFile);
2262 // FIXME: Because PathResolve is unimplemented, we use our hackish implementation!
2263 HACKISH_PathResolve(szPath, NULL, PRF_TRYPROGRAMEXTENSIONS);
2264
2265 pidlNew = SHSimpleIDListFromPathW(szPath);
2266 /******************************************************/
2267 /* Question: Why this line is needed only for files?? */
2268 hr = (*szPath ? S_OK : E_INVALIDARG); // S_FALSE
2269 /******************************************************/
2270 }
2271 }
2272 // else if (!pidl && !pszFile) { pidlNew = NULL; hr = S_OK; }
2273
2274 ILFree(m_pPidl);
2275 m_pPidl = pidlNew;
2276
2277 if (!pszFile)
2278 {
2279 if (SHGetPathFromIDListW(pidlNew, szPath))
2280 pszFile = szPath;
2281 }
2282
2283 // TODO: Fully update link info, tracker, file attribs...
2284
2285 // if (pszFile)
2286 if (!pszFile)
2287 {
2288 *szPath = L'\0';
2289 pszFile = szPath;
2290 }
2291
2292 /* Update the cached path (for link info) */
2293 ShellLink_GetVolumeInfo(pszFile, &volume);
2294
2295 if (m_sPath)
2296 HeapFree(GetProcessHeap(), 0, m_sPath);
2297
2298 m_sPath = strdupW(pszFile);
2299 if (!m_sPath)
2300 return E_OUTOFMEMORY;
2301
2302 m_bDirty = TRUE;
2303 return hr;
2304 }
2305
2306 HRESULT STDMETHODCALLTYPE CShellLink::SetPath(LPCWSTR pszFile)
2307 {
2308 LPWSTR unquoted = NULL;
2309 HRESULT hr = S_OK;
2310
2311 TRACE("(%p)->(path=%s)\n", this, debugstr_w(pszFile));
2312
2313 if (!pszFile)
2314 return E_INVALIDARG;
2315
2316 /*
2317 * Allow upgrading Logo3 shortcuts (m_Header.dwFlags & SLDF_HAS_LOGO3ID),
2318 * but forbid upgrading Darwin ones.
2319 */
2320 if (m_Header.dwFlags & SLDF_HAS_DARWINID)
2321 return S_FALSE;
2322
2323 /* quotes at the ends of the string are stripped */
2324 SIZE_T len = wcslen(pszFile);
2325 if (pszFile[0] == L'"' && pszFile[len-1] == L'"')
2326 {
2327 unquoted = strdupW(pszFile);
2328 PathUnquoteSpacesW(unquoted);
2329 pszFile = unquoted;
2330 }
2331
2332 /* any other quote marks are invalid */
2333 if (wcschr(pszFile, L'"'))
2334 {
2335 hr = S_FALSE;
2336 goto end;
2337 }
2338
2339 /* Clear the cached path */
2340 HeapFree(GetProcessHeap(), 0, m_sPath);
2341 m_sPath = NULL;
2342
2343 /* Check for an advertised target (Logo3 or Darwin) */
2344 if (SetAdvertiseInfo(pszFile) != S_OK)
2345 {
2346 /* This is not an advertised target, but a regular path */
2347 WCHAR szPath[MAX_PATH];
2348
2349 /*
2350 * Check whether the user-given file path contains unexpanded
2351 * environment variables. If so, create a target environment block.
2352 * Note that in this block we will store the user-given path.
2353 * It will contain the unexpanded environment variables, but
2354 * it can also contain already expanded path that the user does
2355 * not want to see them unexpanded (e.g. so that they always
2356 * refer to the same place even if the would-be corresponding
2357 * environment variable could change).
2358 */
2359 if (*pszFile)
2360 SHExpandEnvironmentStringsW(pszFile, szPath, _countof(szPath));
2361 else
2362 *szPath = L'\0';
2363
2364 if (*pszFile && (wcscmp(pszFile, szPath) != 0))
2365 {
2366 /*
2367 * The user-given file path contains unexpanded environment
2368 * variables, so we need a target environment block.
2369 */
2370 EXP_SZ_LINK buffer;
2371 LPEXP_SZ_LINK pInfo;
2372
2373 pInfo = (LPEXP_SZ_LINK)SHFindDataBlock(m_pDBList, EXP_SZ_LINK_SIG);
2374 if (pInfo)
2375 {
2376 /* Make sure that the size of the structure is valid */
2377 if (pInfo->cbSize != sizeof(*pInfo))
2378 {
2379 ERR("Ooops. This structure is not as expected...\n");
2380
2381 /* Invalid structure, remove it altogether */
2382 m_Header.dwFlags &= ~SLDF_HAS_EXP_SZ;
2383 RemoveDataBlock(EXP_SZ_LINK_SIG);
2384
2385 /* Reset the pointer and go use the static buffer */
2386 pInfo = NULL;
2387 }
2388 }
2389 if (!pInfo)
2390 {
2391 /* Use the static buffer */
2392 pInfo = &buffer;
2393 buffer.cbSize = sizeof(buffer);
2394 buffer.dwSignature = EXP_SZ_LINK_SIG;
2395 }
2396
2397 lstrcpynW(pInfo->szwTarget, pszFile, _countof(pInfo->szwTarget));
2398 WideCharToMultiByte(CP_ACP, 0, pszFile, -1,
2399 pInfo->szTarget, _countof(pInfo->szTarget), NULL, NULL);
2400
2401 hr = S_OK;
2402 if (pInfo == &buffer)
2403 hr = AddDataBlock(pInfo);
2404 if (hr == S_OK)
2405 m_Header.dwFlags |= SLDF_HAS_EXP_SZ;
2406
2407 /* Now, make pszFile point to the expanded buffer */
2408 pszFile = szPath;
2409 }
2410 else
2411 {
2412 /*
2413 * The user-given file path does not contain unexpanded environment
2414 * variables, so we need to remove any target environment block.
2415 */
2416 m_Header.dwFlags &= ~SLDF_HAS_EXP_SZ;
2417 RemoveDataBlock(EXP_SZ_LINK_SIG);
2418
2419 /* pszFile points to the user path */
2420 }
2421
2422 /* Set the target */
2423 hr = SetTargetFromPIDLOrPath(NULL, pszFile);
2424 }
2425
2426 m_bDirty = TRUE;
2427
2428 end:
2429 HeapFree(GetProcessHeap(), 0, unquoted);
2430 return hr;
2431 }
2432
2433 HRESULT STDMETHODCALLTYPE CShellLink::AddDataBlock(void* pDataBlock)
2434 {
2435 if (SHAddDataBlock(&m_pDBList, (DATABLOCK_HEADER*)pDataBlock))
2436 {
2437 m_bDirty = TRUE;
2438 return S_OK;
2439 }
2440 return S_FALSE;
2441 }
2442
2443 HRESULT STDMETHODCALLTYPE CShellLink::CopyDataBlock(DWORD dwSig, void** ppDataBlock)
2444 {
2445 DATABLOCK_HEADER* pBlock;
2446 PVOID pDataBlock;
2447
2448 TRACE("%p %08x %p\n", this, dwSig, ppDataBlock);
2449
2450 *ppDataBlock = NULL;
2451
2452 pBlock = SHFindDataBlock(m_pDBList, dwSig);
2453 if (!pBlock)
2454 {
2455 ERR("unknown datablock %08x (not found)\n", dwSig);
2456 return E_FAIL;
2457 }
2458
2459 pDataBlock = LocalAlloc(LMEM_ZEROINIT, pBlock->cbSize);
2460 if (!pDataBlock)
2461 return E_OUTOFMEMORY;
2462
2463 CopyMemory(pDataBlock, pBlock, pBlock->cbSize);
2464
2465 *ppDataBlock = pDataBlock;
2466 return S_OK;
2467 }
2468
2469 HRESULT STDMETHODCALLTYPE CShellLink::RemoveDataBlock(DWORD dwSig)
2470 {
2471 if (SHRemoveDataBlock(&m_pDBList, dwSig))
2472 {
2473 m_bDirty = TRUE;
2474 return S_OK;
2475 }
2476 return S_FALSE;
2477 }
2478
2479 HRESULT STDMETHODCALLTYPE CShellLink::GetFlags(DWORD *pdwFlags)
2480 {
2481 TRACE("%p %p\n", this, pdwFlags);
2482 *pdwFlags = m_Header.dwFlags;
2483 return S_OK;
2484 }
2485
2486 HRESULT STDMETHODCALLTYPE CShellLink::SetFlags(DWORD dwFlags)
2487 {
2488 #if 0 // FIXME!
2489 m_Header.dwFlags = dwFlags;
2490 m_bDirty = TRUE;
2491 return S_OK;
2492 #else
2493 FIXME("\n");
2494 return E_NOTIMPL;
2495 #endif
2496 }
2497
2498 /**************************************************************************
2499 * CShellLink implementation of IShellExtInit::Initialize()
2500 *
2501 * Loads the shelllink from the dataobject the shell is pointing to.
2502 */
2503 HRESULT STDMETHODCALLTYPE CShellLink::Initialize(LPCITEMIDLIST pidlFolder, IDataObject *pdtobj, HKEY hkeyProgID)
2504 {
2505 TRACE("%p %p %p %p\n", this, pidlFolder, pdtobj, hkeyProgID);
2506
2507 if (!pdtobj)
2508 return E_FAIL;
2509
2510 FORMATETC format;
2511 format.cfFormat = CF_HDROP;
2512 format.ptd = NULL;
2513 format.dwAspect = DVASPECT_CONTENT;
2514 format.lindex = -1;
2515 format.tymed = TYMED_HGLOBAL;
2516
2517 STGMEDIUM stgm;
2518 HRESULT hr = pdtobj->GetData(&format, &stgm);
2519 if (FAILED(hr))
2520 return hr;
2521
2522 UINT count = DragQueryFileW((HDROP)stgm.hGlobal, -1, NULL, 0);
2523 if (count == 1)
2524 {
2525 count = DragQueryFileW((HDROP)stgm.hGlobal, 0, NULL, 0);
2526 count++;
2527 LPWSTR path = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, count * sizeof(WCHAR));
2528 if (path)
2529 {
2530 count = DragQueryFileW((HDROP)stgm.hGlobal, 0, path, count);
2531 hr = Load(path, 0);
2532 HeapFree(GetProcessHeap(), 0, path);
2533 }
2534 }
2535 ReleaseStgMedium(&stgm);
2536
2537 return S_OK;
2538 }
2539
2540 HRESULT STDMETHODCALLTYPE CShellLink::QueryContextMenu(HMENU hMenu, UINT indexMenu, UINT idCmdFirst, UINT idCmdLast, UINT uFlags)
2541 {
2542 int id = 1;
2543
2544 TRACE("%p %p %u %u %u %u\n", this,
2545 hMenu, indexMenu, idCmdFirst, idCmdLast, uFlags);
2546
2547 if (!hMenu)
2548 return E_INVALIDARG;
2549
2550 WCHAR wszOpen[20];
2551 if (!LoadStringW(shell32_hInstance, IDS_OPEN_VERB, wszOpen, _countof(wszOpen)))
2552 *wszOpen = L'\0';
2553
2554 MENUITEMINFOW mii;
2555 ZeroMemory(&mii, sizeof(mii));
2556 mii.cbSize = sizeof(mii);
2557 mii.fMask = MIIM_TYPE | MIIM_ID | MIIM_STATE;
2558 mii.dwTypeData = wszOpen;
2559 mii.cch = wcslen(mii.dwTypeData);
2560 mii.wID = idCmdFirst + id++;
2561 mii.fState = MFS_DEFAULT | MFS_ENABLED;
2562 mii.fType = MFT_STRING;
2563 if (!InsertMenuItemW(hMenu, indexMenu, TRUE, &mii))
2564 return E_FAIL;
2565 m_iIdOpen = 1;
2566
2567 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, id);
2568 }
2569
2570 HRESULT STDMETHODCALLTYPE CShellLink::InvokeCommand(LPCMINVOKECOMMANDINFO lpici)
2571 {
2572 LPWSTR args = NULL;
2573 LPWSTR path = NULL;
2574
2575 TRACE("%p %p\n", this, lpici);
2576
2577 if (lpici->cbSize < sizeof(CMINVOKECOMMANDINFO))
2578 return E_INVALIDARG;
2579
2580 // NOTE: We could use lpici->hwnd (certainly in case lpici->fMask doesn't contain CMIC_MASK_FLAG_NO_UI)
2581 // as the parent window handle... ?
2582 /* FIXME: get using interface set from IObjectWithSite?? */
2583 // NOTE: We might need an extended version of Resolve that provides us with paths...
2584 HRESULT hr = Resolve(lpici->hwnd, 0);
2585 if (FAILED(hr))
2586 {
2587 TRACE("failed to resolve component with error 0x%08x", hr);
2588 return hr;
2589 }
2590
2591 path = strdupW(m_sPath);
2592
2593 if ( lpici->cbSize == sizeof(CMINVOKECOMMANDINFOEX) &&
2594 (lpici->fMask & CMIC_MASK_UNICODE) )
2595 {
2596 LPCMINVOKECOMMANDINFOEX iciex = (LPCMINVOKECOMMANDINFOEX)lpici;
2597 SIZE_T len = 2;
2598
2599 if (m_sArgs)
2600 len += wcslen(m_sArgs);
2601 if (iciex->lpParametersW)
2602 len += wcslen(iciex->lpParametersW);
2603
2604 args = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
2605 *args = 0;
2606 if (m_sArgs)
2607 wcscat(args, m_sArgs);
2608 if (iciex->lpParametersW)
2609 {
2610 wcscat(args, L" ");
2611 wcscat(args, iciex->lpParametersW);
2612 }
2613 }
2614 else if (m_sArgs != NULL)
2615 {
2616 args = strdupW(m_sArgs);
2617 }
2618
2619 SHELLEXECUTEINFOW sei;
2620 ZeroMemory(&sei, sizeof(sei));
2621 sei.cbSize = sizeof(sei);
2622 sei.fMask = SEE_MASK_HASLINKNAME | SEE_MASK_UNICODE |
2623 (lpici->fMask & (SEE_MASK_NOASYNC | SEE_MASK_ASYNCOK | SEE_MASK_FLAG_NO_UI));
2624 sei.lpFile = path;
2625 sei.lpClass = m_sLinkPath;
2626 sei.nShow = m_Header.nShowCommand;
2627 sei.lpDirectory = m_sWorkDir;
2628 sei.lpParameters = args;
2629 sei.lpVerb = L"open";
2630
2631 // HACK for ShellExecuteExW
2632 if (m_sPath && wcsstr(m_sPath, L".cpl"))
2633 sei.lpVerb = L"cplopen";
2634
2635 if (ShellExecuteExW(&sei))
2636 hr = S_OK;
2637 else
2638 hr = E_FAIL;
2639
2640 HeapFree(GetProcessHeap(), 0, args);
2641 HeapFree(GetProcessHeap(), 0, path);
2642
2643 return hr;
2644 }
2645
2646 HRESULT STDMETHODCALLTYPE CShellLink::GetCommandString(UINT_PTR idCmd, UINT uType, UINT* pwReserved, LPSTR pszName, UINT cchMax)
2647 {
2648 FIXME("%p %lu %u %p %p %u\n", this, idCmd, uType, pwReserved, pszName, cchMax);
2649 return E_NOTIMPL;
2650 }
2651
2652 INT_PTR CALLBACK ExtendedShortcutProc(HWND hwndDlg, UINT uMsg,
2653 WPARAM wParam, LPARAM lParam)
2654 {
2655 switch(uMsg)
2656 {
2657 case WM_INITDIALOG:
2658 if (lParam)
2659 {
2660 HWND hDlgCtrl = GetDlgItem(hwndDlg, IDC_SHORTEX_RUN_DIFFERENT);
2661 SendMessage(hDlgCtrl, BM_SETCHECK, BST_CHECKED, 0);
2662 }
2663 return TRUE;
2664 case WM_COMMAND:
2665 {
2666 HWND hDlgCtrl = GetDlgItem(hwndDlg, IDC_SHORTEX_RUN_DIFFERENT);
2667 if (LOWORD(wParam) == IDOK)
2668 {
2669 if (SendMessage(hDlgCtrl, BM_GETCHECK, 0, 0) == BST_CHECKED)
2670 EndDialog(hwndDlg, 1);
2671 else
2672 EndDialog(hwndDlg, 0);
2673 }
2674 else if (LOWORD(wParam) == IDCANCEL)
2675 {
2676 EndDialog(hwndDlg, -1);
2677 }
2678 else if (LOWORD(wParam) == IDC_SHORTEX_RUN_DIFFERENT)
2679 {
2680 if (SendMessage(hDlgCtrl, BM_GETCHECK, 0, 0) == BST_CHECKED)
2681 SendMessage(hDlgCtrl, BM_SETCHECK, BST_UNCHECKED, 0);
2682 else
2683 SendMessage(hDlgCtrl, BM_SETCHECK, BST_CHECKED, 0);
2684 }
2685 }
2686 }
2687 return FALSE;
2688 }
2689
2690 EXTERN_C HRESULT
2691 WINAPI
2692 SHOpenFolderAndSelectItems(LPITEMIDLIST pidlFolder,
2693 UINT cidl,
2694 PCUITEMID_CHILD_ARRAY apidl,
2695 DWORD dwFlags);
2696
2697 /**************************************************************************
2698 * SH_GetTargetTypeByPath
2699 *
2700 * Function to get target type by passing full path to it
2701 */
2702 LPWSTR SH_GetTargetTypeByPath(LPCWSTR lpcwFullPath)
2703 {
2704 LPCWSTR pwszExt;
2705 static WCHAR wszBuf[MAX_PATH];
2706
2707 /* Get file information */
2708 SHFILEINFOW fi;
2709 if (!SHGetFileInfoW(lpcwFullPath, 0, &fi, sizeof(fi), SHGFI_TYPENAME | SHGFI_USEFILEATTRIBUTES))
2710 {
2711 ERR("SHGetFileInfoW failed for %ls (%lu)\n", lpcwFullPath, GetLastError());
2712 fi.szTypeName[0] = L'\0';
2713 fi.hIcon = NULL;
2714 }
2715
2716 pwszExt = PathFindExtensionW(lpcwFullPath);
2717 if (pwszExt[0])
2718 {
2719 if (!fi.szTypeName[0])
2720 {
2721 /* The file type is unknown, so default to string "FileExtension File" */
2722 size_t cchRemaining = 0;
2723 LPWSTR pwszEnd = NULL;
2724
2725 StringCchPrintfExW(wszBuf, _countof(wszBuf), &pwszEnd, &cchRemaining, 0, L"%s ", pwszExt + 1);
2726 }
2727 else
2728 {
2729 /* Update file type */
2730 StringCbPrintfW(wszBuf, sizeof(wszBuf), L"%s (%s)", fi.szTypeName, pwszExt);
2731 }
2732 }
2733
2734 return wszBuf;
2735 }
2736
2737 BOOL CShellLink::OnInitDialog(HWND hwndDlg, HWND hwndFocus, LPARAM lParam)
2738 {
2739 TRACE("CShellLink::OnInitDialog(hwnd %p hwndFocus %p lParam %p)\n", hwndDlg, hwndFocus, lParam);
2740
2741 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,
2742 m_sIcoPath, m_sPath, m_sPathRel, sProduct, m_sWorkDir);
2743
2744 m_bInInit = TRUE;
2745
2746 /* Get file information */
2747 // FIXME! FIXME! Shouldn't we use m_sIcoPath, m_Header.nIconIndex instead???
2748 SHFILEINFOW fi;
2749 if (!SHGetFileInfoW(m_sLinkPath, 0, &fi, sizeof(fi), SHGFI_TYPENAME | SHGFI_ICON))
2750 {
2751 ERR("SHGetFileInfoW failed for %ls (%lu)\n", m_sLinkPath, GetLastError());
2752 fi.szTypeName[0] = L'\0';
2753 fi.hIcon = NULL;
2754 }
2755
2756 if (fi.hIcon)
2757 {
2758 if (m_hIcon)
2759 DestroyIcon(m_hIcon);
2760 m_hIcon = fi.hIcon;
2761 SendDlgItemMessageW(hwndDlg, IDC_SHORTCUT_ICON, STM_SETICON, (WPARAM)m_hIcon, 0);
2762 }
2763 else
2764 ERR("ExtractIconW failed %ls %u\n", m_sIcoPath, m_Header.nIconIndex);
2765
2766 /* Target type */
2767 if (m_sPath)
2768 SetDlgItemTextW(hwndDlg, IDC_SHORTCUT_TYPE_EDIT, SH_GetTargetTypeByPath(m_sPath));
2769
2770 /* Target location */
2771 if (m_sPath)
2772 {
2773 WCHAR target[MAX_PATH];
2774 StringCchCopyW(target, _countof(target), m_sPath);
2775 PathRemoveFileSpecW(target);
2776 SetDlgItemTextW(hwndDlg, IDC_SHORTCUT_LOCATION_EDIT, PathFindFileNameW(target));
2777 }
2778
2779 /* Target path */
2780 if (m_sPath)
2781 {
2782 WCHAR newpath[2*MAX_PATH] = L"\0";
2783 if (wcschr(m_sPath, ' '))
2784 StringCchPrintfExW(newpath, _countof(newpath), NULL, NULL, 0, L"\"%ls\"", m_sPath);
2785 else
2786 StringCchCopyExW(newpath, _countof(newpath), m_sPath, NULL, NULL, 0);
2787
2788 if (m_sArgs && m_sArgs[0])
2789 {
2790 StringCchCatW(newpath, _countof(newpath), L" ");
2791 StringCchCatW(newpath, _countof(newpath), m_sArgs);
2792 }
2793 SetDlgItemTextW(hwndDlg, IDC_SHORTCUT_TARGET_TEXT, newpath);
2794 }
2795
2796 /* Working dir */
2797 if (m_sWorkDir)
2798 SetDlgItemTextW(hwndDlg, IDC_SHORTCUT_START_IN_EDIT, m_sWorkDir);
2799
2800 /* Description */
2801 if (m_sDescription)
2802 SetDlgItemTextW(hwndDlg, IDC_SHORTCUT_COMMENT_EDIT, m_sDescription);
2803
2804 m_bInInit = FALSE;
2805
2806 return TRUE;
2807 }
2808
2809 void CShellLink::OnCommand(HWND hwndDlg, int id, HWND hwndCtl, UINT codeNotify)
2810 {
2811 switch (id)
2812 {
2813 case IDC_SHORTCUT_FIND:
2814 SHOpenFolderAndSelectItems(m_pPidl, 0, NULL, 0);
2815 ///
2816 /// FIXME
2817 /// open target directory
2818 ///
2819 return;
2820
2821 case IDC_SHORTCUT_CHANGE_ICON:
2822 {
2823 WCHAR wszPath[MAX_PATH] = L"";
2824
2825 if (m_sIcoPath)
2826 wcscpy(wszPath, m_sIcoPath);
2827 else
2828 FindExecutableW(m_sPath, NULL, wszPath);
2829
2830 INT IconIndex = m_Header.nIconIndex;
2831 if (PickIconDlg(hwndDlg, wszPath, _countof(wszPath), &IconIndex))
2832 {
2833 SetIconLocation(wszPath, IconIndex);
2834 PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
2835
2836 HICON hIconLarge = CreateShortcutIcon(wszPath, IconIndex);
2837 if (hIconLarge)
2838 {
2839 if (m_hIcon)
2840 DestroyIcon(m_hIcon);
2841 m_hIcon = hIconLarge;
2842 SendDlgItemMessageW(hwndDlg, IDC_SHORTCUT_ICON, STM_SETICON, (WPARAM)m_hIcon, 0);
2843 }
2844 }
2845 return;
2846 }
2847
2848 case IDC_SHORTCUT_ADVANCED:
2849 {
2850 INT_PTR result = DialogBoxParamW(shell32_hInstance, MAKEINTRESOURCEW(IDD_SHORTCUT_EXTENDED_PROPERTIES), hwndDlg, ExtendedShortcutProc, (LPARAM)m_bRunAs);
2851 if (result == 1 || result == 0)
2852 {
2853 if (m_bRunAs != result)
2854 {
2855 PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
2856 }
2857
2858 m_bRunAs = result;
2859 }
2860 return;
2861 }
2862 }
2863 if (codeNotify == EN_CHANGE)
2864 {
2865 if (!m_bInInit)
2866 PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
2867 }
2868 }
2869
2870 LRESULT CShellLink::OnNotify(HWND hwndDlg, int idFrom, LPNMHDR pnmhdr)
2871 {
2872 WCHAR wszBuf[MAX_PATH];
2873 LPPSHNOTIFY lppsn = (LPPSHNOTIFY)pnmhdr;
2874
2875 if (lppsn->hdr.code == PSN_APPLY)
2876 {
2877 /* set working directory */
2878 GetDlgItemTextW(hwndDlg, IDC_SHORTCUT_START_IN_EDIT, wszBuf, _countof(wszBuf));
2879 SetWorkingDirectory(wszBuf);
2880
2881 /* set link destination */
2882 GetDlgItemTextW(hwndDlg, IDC_SHORTCUT_TARGET_TEXT, wszBuf, _countof(wszBuf));
2883 LPWSTR lpszArgs = NULL;
2884 LPWSTR unquoted = strdupW(wszBuf);
2885 StrTrimW(unquoted, L" ");
2886
2887 if (!PathFileExistsW(unquoted))
2888 {
2889 lpszArgs = PathGetArgsW(unquoted);
2890 PathRemoveArgsW(unquoted);
2891 StrTrimW(lpszArgs, L" ");
2892 }
2893 if (unquoted[0] == '"' && unquoted[wcslen(unquoted) - 1] == '"')
2894 PathUnquoteSpacesW(unquoted);
2895
2896 WCHAR *pwszExt = PathFindExtensionW(unquoted);
2897 if (!wcsicmp(pwszExt, L".lnk"))
2898 {
2899 // FIXME load localized error msg
2900 MessageBoxW(hwndDlg, L"You cannot create a link to a shortcut", L"Error", MB_ICONERROR);
2901 SetWindowLongPtr(hwndDlg, DWLP_MSGRESULT, PSNRET_INVALID_NOCHANGEPAGE);
2902 return TRUE;
2903 }
2904
2905 if (!PathFileExistsW(unquoted))
2906 {
2907 // FIXME load localized error msg
2908 MessageBoxW(hwndDlg, L"The specified file name in the target box is invalid", L"Error", MB_ICONERROR);
2909 SetWindowLongPtr(hwndDlg, DWLP_MSGRESULT, PSNRET_INVALID_NOCHANGEPAGE);
2910 return TRUE;
2911 }
2912
2913 SetPath(unquoted);
2914 if (lpszArgs)
2915 SetArguments(lpszArgs);
2916 else
2917 SetArguments(L"\0");
2918
2919 HeapFree(GetProcessHeap(), 0, unquoted);
2920
2921 TRACE("This %p m_sLinkPath %S\n", this, m_sLinkPath);
2922 Save(m_sLinkPath, TRUE);
2923 SHChangeNotify(SHCNE_UPDATEITEM, SHCNF_PATHW, m_sLinkPath, NULL);
2924 SetWindowLongPtr(hwndDlg, DWLP_MSGRESULT, PSNRET_NOERROR);
2925 return TRUE;
2926 }
2927 return FALSE;
2928 }
2929
2930 void CShellLink::OnDestroy(HWND hwndDlg)
2931 {
2932 if (m_hIcon)
2933 {
2934 DestroyIcon(m_hIcon);
2935 m_hIcon = NULL;
2936 }
2937 }
2938
2939 /**************************************************************************
2940 * SH_ShellLinkDlgProc
2941 *
2942 * dialog proc of the shortcut property dialog
2943 */
2944
2945 INT_PTR CALLBACK
2946 CShellLink::SH_ShellLinkDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
2947 {
2948 LPPROPSHEETPAGEW ppsp;
2949 CShellLink *pThis = reinterpret_cast<CShellLink *>(GetWindowLongPtr(hwndDlg, DWLP_USER));
2950
2951 switch (uMsg)
2952 {
2953 case WM_INITDIALOG:
2954 ppsp = (LPPROPSHEETPAGEW)lParam;
2955 if (ppsp == NULL)
2956 break;
2957
2958 pThis = reinterpret_cast<CShellLink *>(ppsp->lParam);
2959 SetWindowLongPtr(hwndDlg, DWLP_USER, (LONG_PTR)pThis);
2960 return pThis->OnInitDialog(hwndDlg, (HWND)(wParam), lParam);
2961
2962 case WM_NOTIFY:
2963 return pThis->OnNotify(hwndDlg, (int)wParam, (NMHDR *)lParam);
2964
2965 case WM_COMMAND:
2966 pThis->OnCommand(hwndDlg, LOWORD(wParam), (HWND)lParam, HIWORD(wParam));
2967 break;
2968
2969 case WM_DESTROY:
2970 pThis->OnDestroy(hwndDlg);
2971 break;
2972
2973 default:
2974 break;
2975 }
2976
2977 return FALSE;
2978 }
2979
2980 /**************************************************************************
2981 * ShellLink_IShellPropSheetExt interface
2982 */
2983
2984 HRESULT STDMETHODCALLTYPE CShellLink::AddPages(LPFNADDPROPSHEETPAGE pfnAddPage, LPARAM lParam)
2985 {
2986 HPROPSHEETPAGE hPage = SH_CreatePropertySheetPage(IDD_SHORTCUT_PROPERTIES, SH_ShellLinkDlgProc, (LPARAM)this, NULL);
2987 if (hPage == NULL)
2988 {
2989 ERR("failed to create property sheet page\n");
2990 return E_FAIL;
2991 }
2992
2993 if (!pfnAddPage(hPage, lParam))
2994 return E_FAIL;
2995
2996 return S_OK;
2997 }
2998
2999 HRESULT STDMETHODCALLTYPE CShellLink::ReplacePage(UINT uPageID, LPFNADDPROPSHEETPAGE pfnReplacePage, LPARAM lParam)
3000 {
3001 TRACE("(%p) (uPageID %u, pfnReplacePage %p lParam %p\n", this, uPageID, pfnReplacePage, lParam);
3002 return E_NOTIMPL;
3003 }
3004
3005 HRESULT STDMETHODCALLTYPE CShellLink::SetSite(IUnknown *punk)
3006 {
3007 TRACE("%p %p\n", this, punk);
3008
3009 m_site = punk;
3010
3011 return S_OK;
3012 }
3013
3014 HRESULT STDMETHODCALLTYPE CShellLink::GetSite(REFIID iid, void ** ppvSite)
3015 {
3016 TRACE("%p %s %p\n", this, debugstr_guid(&iid), ppvSite);
3017
3018 if (m_site == NULL)
3019 return E_FAIL;
3020
3021 return m_site->QueryInterface(iid, ppvSite);
3022 }
3023
3024 HRESULT STDMETHODCALLTYPE CShellLink::DragEnter(IDataObject *pDataObject,
3025 DWORD dwKeyState, POINTL pt, DWORD *pdwEffect)
3026 {
3027 TRACE("(%p)->(DataObject=%p)\n", this, pDataObject);
3028 LPCITEMIDLIST pidlLast;
3029 CComPtr<IShellFolder> psf;
3030
3031 HRESULT hr = SHBindToParent(m_pPidl, IID_PPV_ARG(IShellFolder, &psf), &pidlLast);
3032
3033 if (SUCCEEDED(hr))
3034 {
3035 hr = psf->GetUIObjectOf(0, 1, &pidlLast, IID_NULL_PPV_ARG(IDropTarget, &m_DropTarget));
3036
3037 if (SUCCEEDED(hr))
3038 hr = m_DropTarget->DragEnter(pDataObject, dwKeyState, pt, pdwEffect);
3039 else
3040 *pdwEffect = DROPEFFECT_NONE;
3041 }
3042 else
3043 *pdwEffect = DROPEFFECT_NONE;
3044
3045 return S_OK;
3046 }
3047
3048 HRESULT STDMETHODCALLTYPE CShellLink::DragOver(DWORD dwKeyState, POINTL pt,
3049 DWORD *pdwEffect)
3050 {
3051 TRACE("(%p)\n", this);
3052 HRESULT hr = S_OK;
3053 if (m_DropTarget)
3054 hr = m_DropTarget->DragOver(dwKeyState, pt, pdwEffect);
3055 return hr;
3056 }
3057
3058 HRESULT STDMETHODCALLTYPE CShellLink::DragLeave()
3059 {
3060 TRACE("(%p)\n", this);
3061 HRESULT hr = S_OK;
3062 if (m_DropTarget)
3063 {
3064 hr = m_DropTarget->DragLeave();
3065 m_DropTarget.Release();
3066 }
3067
3068 return hr;
3069 }
3070
3071 HRESULT STDMETHODCALLTYPE CShellLink::Drop(IDataObject *pDataObject,
3072 DWORD dwKeyState, POINTL pt, DWORD *pdwEffect)
3073 {
3074 TRACE("(%p)\n", this);
3075 HRESULT hr = S_OK;
3076 if (m_DropTarget)
3077 hr = m_DropTarget->Drop(pDataObject, dwKeyState, pt, pdwEffect);
3078
3079 return hr;
3080 }
3081
3082 /**************************************************************************
3083 * IShellLink_ConstructFromFile
3084 */
3085 HRESULT WINAPI IShellLink_ConstructFromPath(WCHAR *path, REFIID riid, LPVOID *ppv)
3086 {
3087 CComPtr<IPersistFile> ppf;
3088 HRESULT hr = CShellLink::_CreatorClass::CreateInstance(NULL, IID_PPV_ARG(IPersistFile, &ppf));
3089 if (FAILED(hr))
3090 return hr;
3091
3092 hr = ppf->Load(path, 0);
3093 if (FAILED(hr))
3094 return hr;
3095
3096 return ppf->QueryInterface(riid, ppv);
3097 }
3098
3099 HRESULT WINAPI IShellLink_ConstructFromFile(IShellFolder * psf, LPCITEMIDLIST pidl, REFIID riid, LPVOID *ppv)
3100 {
3101 WCHAR path[MAX_PATH];
3102 if (!ILGetDisplayNameExW(psf, pidl, path, 0))
3103 return E_FAIL;
3104
3105 return IShellLink_ConstructFromPath(path, riid, ppv);
3106 }
3107
3108 HICON CShellLink::CreateShortcutIcon(LPCWSTR wszIconPath, INT IconIndex)
3109 {
3110 const INT cx = GetSystemMetrics(SM_CXICON), cy = GetSystemMetrics(SM_CYICON);
3111 const COLORREF crMask = GetSysColor(COLOR_3DFACE);
3112 HDC hDC;
3113 HIMAGELIST himl = ImageList_Create(cx, cy, ILC_COLOR32 | ILC_MASK, 1, 1);
3114 HICON hIcon = NULL, hNewIcon = NULL;
3115 HICON hShortcut = (HICON)LoadImageW(shell32_hInstance, MAKEINTRESOURCE(IDI_SHELL_SHORTCUT),
3116 IMAGE_ICON, cx, cy, 0);
3117
3118 ::ExtractIconExW(wszIconPath, IconIndex, &hIcon, NULL, 1);
3119 if (!hIcon || !hShortcut || !himl)
3120 goto cleanup;
3121
3122 hDC = CreateCompatibleDC(NULL);
3123 if (hDC)
3124 {
3125 // create 32bpp bitmap
3126 BITMAPINFO bi;
3127 ZeroMemory(&bi, sizeof(bi));
3128 bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
3129 bi.bmiHeader.biWidth = cx;
3130 bi.bmiHeader.biHeight = cy;
3131 bi.bmiHeader.biPlanes = 1;
3132 bi.bmiHeader.biBitCount = 32;
3133 LPVOID pvBits;
3134 HBITMAP hbm = CreateDIBSection(hDC, &bi, DIB_RGB_COLORS, &pvBits, NULL, 0);
3135 if (hbm)
3136 {
3137 // draw the icon image
3138 HGDIOBJ hbmOld = SelectObject(hDC, hbm);
3139 {
3140 HBRUSH hbr = CreateSolidBrush(crMask);
3141 RECT rc = { 0, 0, cx, cy };
3142 FillRect(hDC, &rc, hbr);
3143 DeleteObject(hbr);
3144
3145 DrawIconEx(hDC, 0, 0, hIcon, cx, cy, 0, NULL, DI_NORMAL);
3146 DrawIconEx(hDC, 0, 0, hShortcut, cx, cy, 0, NULL, DI_NORMAL);
3147 }
3148 SelectObject(hDC, hbmOld);
3149
3150 INT iAdded = ImageList_AddMasked(himl, hbm, crMask);
3151 hNewIcon = ImageList_GetIcon(himl, iAdded, ILD_NORMAL | ILD_TRANSPARENT);
3152
3153 DeleteObject(hbm);
3154 }
3155 DeleteDC(hDC);
3156 }
3157
3158 cleanup:
3159 if (hIcon)
3160 DestroyIcon(hIcon);
3161 if (hShortcut)
3162 DestroyIcon(hShortcut);
3163 if (himl)
3164 ImageList_Destroy(himl);
3165
3166 return hNewIcon;
3167 }