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