[SHELL32]
[reactos.git] / reactos / 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 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 *
23 * NOTES
24 * Nearly complete information about the binary formats
25 * of .lnk files available at http://www.wotsit.org
26 *
27 * You can use winedump to examine the contents of a link file:
28 * winedump lnk sc.lnk
29 *
30 * MSI advertised shortcuts are totally undocumented. They provide an
31 * icon for a program that is not yet installed, and invoke MSI to
32 * install the program when the shortcut is clicked on. They are
33 * created by passing a special string to SetPath, and the information
34 * in that string is parsed an stored.
35 */
36
37 #include "precomp.h"
38
39 #include <appmgmt.h>
40
41 WINE_DEFAULT_DEBUG_CHANNEL(shell);
42
43 #define SHLINK_LOCAL 0
44 #define SHLINK_REMOTE 1
45 #define MAX_PROPERTY_SHEET_PAGE 32
46
47 /* link file formats */
48
49 #include "pshpack1.h"
50
51 struct LOCATION_INFO
52 {
53 DWORD dwTotalSize;
54 DWORD dwHeaderSize;
55 DWORD dwFlags;
56 DWORD dwVolTableOfs;
57 DWORD dwLocalPathOfs;
58 DWORD dwNetworkVolTableOfs;
59 DWORD dwFinalPathOfs;
60 };
61
62 struct LOCAL_VOLUME_INFO
63 {
64 DWORD dwSize;
65 DWORD dwType;
66 DWORD dwVolSerial;
67 DWORD dwVolLabelOfs;
68 };
69
70 struct volume_info
71 {
72 DWORD type;
73 DWORD serial;
74 WCHAR label[12]; /* assume 8.3 */
75 };
76
77 #include "poppack.h"
78
79 /**************************************************************************
80 * SH_GetTargetTypeByPath
81 *
82 * Function to get target type by passing full path to it
83 */
84 LPWSTR SH_GetTargetTypeByPath(LPCWSTR lpcwFullPath)
85 {
86 LPCWSTR pwszExt;
87 static WCHAR wszBuf[MAX_PATH];
88
89 /* Get file information */
90 SHFILEINFO fi;
91 if (!SHGetFileInfoW(lpcwFullPath, 0, &fi, sizeof(fi), SHGFI_TYPENAME | SHGFI_USEFILEATTRIBUTES ))
92 {
93 ERR("SHGetFileInfoW failed for %ls (%lu)\n", lpcwFullPath, GetLastError());
94 fi.szTypeName[0] = L'\0';
95 fi.hIcon = NULL;
96 }
97
98 pwszExt = PathFindExtensionW(lpcwFullPath);
99 if (pwszExt[0])
100 {
101 if (!fi.szTypeName[0])
102 {
103 /* The file type is unknown, so default to string "FileExtension File" */
104 size_t cchRemaining = 0;
105 LPWSTR pwszEnd = NULL;
106
107 StringCchPrintfExW(wszBuf, _countof(wszBuf), &pwszEnd, &cchRemaining, 0, L"%s ", pwszExt + 1);
108 }
109 else
110 StringCbPrintfW(wszBuf, sizeof(wszBuf), L"%s (%s)", fi.szTypeName, pwszExt); /* Update file type */
111 }
112
113 return wszBuf;
114 }
115
116 /* IShellLink Implementation */
117
118 static HRESULT ShellLink_UpdatePath(LPCWSTR sPathRel, LPCWSTR path, LPCWSTR sWorkDir, LPWSTR* psPath);
119
120 /* strdup on the process heap */
121 static LPWSTR __inline HEAP_strdupAtoW(HANDLE heap, DWORD flags, LPCSTR str)
122 {
123 INT len;
124 LPWSTR p;
125
126 assert(str);
127
128 len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
129 p = (LPWSTR)HeapAlloc(heap, flags, len * sizeof(WCHAR));
130 if (!p)
131 return p;
132 MultiByteToWideChar(CP_ACP, 0, str, -1, p, len);
133 return p;
134 }
135
136 static LPWSTR __inline strdupW(LPCWSTR src)
137 {
138 LPWSTR dest;
139 if (!src) return NULL;
140 dest = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, (wcslen(src) + 1) * sizeof(WCHAR));
141 if (dest)
142 wcscpy(dest, src);
143 return dest;
144 }
145
146 CShellLink::CShellLink()
147 {
148 pPidl = NULL;
149 wHotKey = 0;
150 memset(&time1, 0, sizeof(time1));
151 memset(&time2, 0, sizeof(time2));
152 memset(&time3, 0, sizeof(time3));
153 iShowCmd = SW_SHOWNORMAL;
154 sIcoPath = NULL;
155 iIcoNdx = 0;
156 sPath = NULL;
157 sArgs = NULL;
158 sWorkDir = NULL;
159 sDescription = NULL;
160 sPathRel = NULL;
161 sProduct = NULL;
162 sComponent = NULL;
163 memset(&volume, 0, sizeof(volume));
164 sLinkPath = NULL;
165 bRunAs = FALSE;
166 bDirty = FALSE;
167 iIdOpen = -1;
168 }
169
170 CShellLink::~CShellLink()
171 {
172 TRACE("-- destroying IShellLink(%p)\n", this);
173
174 HeapFree(GetProcessHeap(), 0, sIcoPath);
175 HeapFree(GetProcessHeap(), 0, sArgs);
176 HeapFree(GetProcessHeap(), 0, sWorkDir);
177 HeapFree(GetProcessHeap(), 0, sDescription);
178 HeapFree(GetProcessHeap(), 0, sPath);
179 HeapFree(GetProcessHeap(), 0, sLinkPath);
180
181 if (pPidl)
182 ILFree(pPidl);
183 }
184
185 HRESULT WINAPI CShellLink::GetClassID(CLSID *pclsid)
186 {
187 TRACE("%p %p\n", this, pclsid);
188
189 if (pclsid == NULL)
190 return E_POINTER;
191 *pclsid = CLSID_ShellLink;
192 return S_OK;
193 }
194
195 HRESULT WINAPI CShellLink::IsDirty()
196 {
197 TRACE("(%p)\n", this);
198
199 if (bDirty)
200 return S_OK;
201
202 return S_FALSE;
203 }
204
205 HRESULT WINAPI CShellLink::Load(LPCOLESTR pszFileName, DWORD dwMode)
206 {
207 TRACE("(%p, %s, %x)\n", this, debugstr_w(pszFileName), dwMode);
208
209 if (dwMode == 0)
210 dwMode = STGM_READ | STGM_SHARE_DENY_WRITE;
211
212 CComPtr<IStream> stm;
213 HRESULT hr = SHCreateStreamOnFileW(pszFileName, dwMode, &stm);
214 if (SUCCEEDED(hr))
215 {
216 HeapFree(GetProcessHeap(), 0, sLinkPath);
217 sLinkPath = strdupW(pszFileName);
218 hr = Load(stm);
219 ShellLink_UpdatePath(sPathRel, pszFileName, sWorkDir, &sPath);
220 bDirty = FALSE;
221 }
222 TRACE("-- returning hr %08x\n", hr);
223 return hr;
224 }
225
226 HRESULT WINAPI CShellLink::Save(LPCOLESTR pszFileName, BOOL fRemember)
227 {
228 TRACE("(%p)->(%s)\n", this, debugstr_w(pszFileName));
229
230 if (!pszFileName)
231 return E_FAIL;
232
233 CComPtr<IStream> stm;
234 HRESULT hr = SHCreateStreamOnFileW(pszFileName, STGM_READWRITE | STGM_CREATE | STGM_SHARE_EXCLUSIVE, &stm);
235 if (SUCCEEDED(hr))
236 {
237 hr = Save(stm, FALSE);
238
239 if (SUCCEEDED(hr))
240 {
241 if (sLinkPath)
242 HeapFree(GetProcessHeap(), 0, sLinkPath);
243
244 sLinkPath = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, (wcslen(pszFileName) + 1) * sizeof(WCHAR));
245 if (sLinkPath)
246 wcscpy(sLinkPath, pszFileName);
247
248 bDirty = FALSE;
249 }
250 else
251 {
252 DeleteFileW(pszFileName);
253 WARN("Failed to create shortcut %s\n", debugstr_w(pszFileName));
254 }
255 }
256
257 return hr;
258 }
259
260 HRESULT WINAPI CShellLink::SaveCompleted(LPCOLESTR pszFileName)
261 {
262 FIXME("(%p)->(%s)\n", this, debugstr_w(pszFileName));
263 return S_OK;
264 }
265
266 HRESULT WINAPI CShellLink::GetCurFile(LPOLESTR *ppszFileName)
267 {
268 *ppszFileName = NULL;
269
270 if (!sLinkPath)
271 {
272 /* IPersistFile::GetCurFile called before IPersistFile::Save */
273 return S_FALSE;
274 }
275
276 *ppszFileName = (LPOLESTR)CoTaskMemAlloc((wcslen(sLinkPath) + 1) * sizeof(WCHAR));
277 if (!*ppszFileName)
278 {
279 /* out of memory */
280 return E_OUTOFMEMORY;
281 }
282
283 /* copy last saved filename */
284 wcscpy(*ppszFileName, sLinkPath);
285
286 return S_OK;
287 }
288
289 /************************************************************************
290 * IPersistStream_IsDirty (IPersistStream)
291 */
292
293 static HRESULT Stream_LoadString(IStream* stm, BOOL unicode, LPWSTR *pstr)
294 {
295 TRACE("%p\n", stm);
296
297 USHORT len;
298 DWORD count = 0;
299 HRESULT hr = stm->Read(&len, sizeof(len), &count);
300 if (FAILED(hr) || count != sizeof(len))
301 return E_FAIL;
302
303 if (unicode)
304 len *= sizeof (WCHAR);
305
306 TRACE("reading %d\n", len);
307 LPSTR temp = (LPSTR)HeapAlloc(GetProcessHeap(), 0, len + sizeof(WCHAR));
308 if (!temp)
309 return E_OUTOFMEMORY;
310 count = 0;
311 hr = stm->Read(temp, len, &count);
312 if(FAILED(hr) || count != len)
313 {
314 HeapFree(GetProcessHeap(), 0, temp);
315 return E_FAIL;
316 }
317
318 TRACE("read %s\n", debugstr_an(temp, len));
319
320 /* convert to unicode if necessary */
321 LPWSTR str;
322 if (!unicode)
323 {
324 count = MultiByteToWideChar(CP_ACP, 0, temp, len, NULL, 0);
325 str = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, (count + 1) * sizeof (WCHAR));
326 if (!str)
327 {
328 HeapFree(GetProcessHeap(), 0, temp);
329 return E_OUTOFMEMORY;
330 }
331 MultiByteToWideChar(CP_ACP, 0, temp, len, str, count);
332 HeapFree(GetProcessHeap(), 0, temp);
333 }
334 else
335 {
336 count /= 2;
337 str = (LPWSTR)temp;
338 }
339 str[count] = 0;
340
341 *pstr = str;
342
343 return S_OK;
344 }
345
346 static HRESULT Stream_ReadChunk(IStream* stm, LPVOID *data)
347 {
348 struct sized_chunk
349 {
350 DWORD size;
351 unsigned char data[1];
352 } *chunk;
353
354 TRACE("%p\n", stm);
355
356 DWORD size;
357 ULONG count;
358 HRESULT hr = stm->Read(&size, sizeof(size), &count);
359 if (FAILED(hr) || count != sizeof(size))
360 return E_FAIL;
361
362 chunk = static_cast<sized_chunk *>(HeapAlloc(GetProcessHeap(), 0, size));
363 if (!chunk)
364 return E_OUTOFMEMORY;
365
366 chunk->size = size;
367 hr = stm->Read(chunk->data, size - sizeof(size), &count);
368 if (FAILED(hr) || count != (size - sizeof(size)))
369 {
370 HeapFree(GetProcessHeap(), 0, chunk);
371 return E_FAIL;
372 }
373
374 TRACE("Read %d bytes\n", chunk->size);
375
376 *data = chunk;
377
378 return S_OK;
379 }
380
381 static BOOL Stream_LoadVolume(LOCAL_VOLUME_INFO *vol, CShellLink::volume_info *volume)
382 {
383 volume->serial = vol->dwVolSerial;
384 volume->type = vol->dwType;
385
386 if (!vol->dwVolLabelOfs)
387 return FALSE;
388 if (vol->dwSize <= vol->dwVolLabelOfs)
389 return FALSE;
390 INT len = vol->dwSize - vol->dwVolLabelOfs;
391
392 LPSTR label = (LPSTR)vol;
393 label += vol->dwVolLabelOfs;
394 MultiByteToWideChar(CP_ACP, 0, label, len, volume->label, _countof(volume->label));
395
396 return TRUE;
397 }
398
399 static LPWSTR Stream_LoadPath(LPCSTR p, DWORD maxlen)
400 {
401 UINT len = 0;
402
403 while (p[len] && len < maxlen)
404 len++;
405
406 UINT wlen = MultiByteToWideChar(CP_ACP, 0, p, len, NULL, 0);
407 LPWSTR path = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, (wlen + 1) * sizeof(WCHAR));
408 if (!path)
409 return NULL;
410 MultiByteToWideChar(CP_ACP, 0, p, len, path, wlen);
411 path[wlen] = 0;
412
413 return path;
414 }
415
416 static HRESULT Stream_LoadLocation(IStream *stm,
417 CShellLink::volume_info *volume, LPWSTR *path)
418 {
419 char *p = NULL;
420 HRESULT hr = Stream_ReadChunk(stm, (LPVOID*) &p);
421 if (FAILED(hr))
422 return hr;
423
424 LOCATION_INFO *loc = reinterpret_cast<LOCATION_INFO *>(p);
425 if (loc->dwTotalSize < sizeof(LOCATION_INFO))
426 {
427 HeapFree(GetProcessHeap(), 0, p);
428 return E_FAIL;
429 }
430
431 /* if there's valid local volume information, load it */
432 if (loc->dwVolTableOfs &&
433 ((loc->dwVolTableOfs + sizeof(LOCAL_VOLUME_INFO)) <= loc->dwTotalSize))
434 {
435 LOCAL_VOLUME_INFO *volume_info;
436
437 volume_info = (LOCAL_VOLUME_INFO*) &p[loc->dwVolTableOfs];
438 Stream_LoadVolume(volume_info, volume);
439 }
440
441 /* if there's a local path, load it */
442 DWORD n = loc->dwLocalPathOfs;
443 if (n && n < loc->dwTotalSize)
444 *path = Stream_LoadPath(&p[n], loc->dwTotalSize - n);
445
446 TRACE("type %d serial %08x name %s path %s\n", volume->type,
447 volume->serial, debugstr_w(volume->label), debugstr_w(*path));
448
449 HeapFree(GetProcessHeap(), 0, p);
450 return S_OK;
451 }
452
453 /*
454 * The format of the advertised shortcut info seems to be:
455 *
456 * Offset Description
457 * ------ -----------
458 *
459 * 0 Length of the block (4 bytes, usually 0x314)
460 * 4 tag (dword)
461 * 8 string data in ASCII
462 * 8+0x104 string data in UNICODE
463 *
464 * In the original Win32 implementation the buffers are not initialized
465 * to zero, so data trailing the string is random garbage.
466 */
467 static HRESULT Stream_LoadAdvertiseInfo(IStream* stm, LPWSTR *str)
468 {
469 TRACE("%p\n", stm);
470
471 ULONG count;
472 EXP_DARWIN_LINK buffer;
473 HRESULT hr = stm->Read(&buffer.dbh.cbSize, sizeof (DWORD), &count);
474 if (FAILED(hr))
475 return hr;
476
477 /* make sure that we read the size of the structure even on error */
478 DWORD size = sizeof buffer - sizeof (DWORD);
479 if (buffer.dbh.cbSize != sizeof buffer)
480 {
481 ERR("Ooops. This structure is not as expected...\n");
482 return E_FAIL;
483 }
484
485 hr = stm->Read(&buffer.dbh.dwSignature, size, &count);
486 if (FAILED(hr))
487 return hr;
488
489 if (count != size)
490 return E_FAIL;
491
492 TRACE("magic %08x string = %s\n", buffer.dbh.dwSignature, debugstr_w(buffer.szwDarwinID));
493
494 if ((buffer.dbh.dwSignature & 0xffff0000) != 0xa0000000)
495 {
496 ERR("Unknown magic number %08x in advertised shortcut\n", buffer.dbh.dwSignature);
497 return E_FAIL;
498 }
499
500 *str = (LPWSTR)HeapAlloc(GetProcessHeap(), 0,
501 (wcslen(buffer.szwDarwinID) + 1) * sizeof(WCHAR));
502 wcscpy(*str, buffer.szwDarwinID);
503
504 return S_OK;
505 }
506
507 /************************************************************************
508 * IPersistStream_Load (IPersistStream)
509 */
510 HRESULT WINAPI CShellLink::Load(IStream *stm)
511 {
512 TRACE("%p %p\n", this, stm);
513
514 if (!stm)
515 return STG_E_INVALIDPOINTER;
516
517 SHELL_LINK_HEADER ShlLnkHeader;
518 ULONG dwBytesRead = 0;
519 HRESULT hr = stm->Read(&ShlLnkHeader, sizeof(ShlLnkHeader), &dwBytesRead);
520 if (FAILED(hr))
521 return hr;
522
523 if (dwBytesRead != sizeof(ShlLnkHeader))
524 return E_FAIL;
525 if (ShlLnkHeader.dwSize != sizeof(ShlLnkHeader))
526 return E_FAIL;
527 if (!IsEqualIID(ShlLnkHeader.clsid, CLSID_ShellLink))
528 return E_FAIL;
529
530 /* free all the old stuff */
531 ILFree(pPidl);
532 pPidl = NULL;
533 memset(&volume, 0, sizeof volume);
534 HeapFree(GetProcessHeap(), 0, sPath);
535 sPath = NULL;
536 HeapFree(GetProcessHeap(), 0, sDescription);
537 sDescription = NULL;
538 HeapFree(GetProcessHeap(), 0, sPathRel);
539 sPathRel = NULL;
540 HeapFree(GetProcessHeap(), 0, sWorkDir);
541 sWorkDir = NULL;
542 HeapFree(GetProcessHeap(), 0, sArgs);
543 sArgs = NULL;
544 HeapFree(GetProcessHeap(), 0, sIcoPath);
545 sIcoPath = NULL;
546 HeapFree(GetProcessHeap(), 0, sProduct);
547 sProduct = NULL;
548 HeapFree(GetProcessHeap(), 0, sComponent);
549 sComponent = NULL;
550
551 BOOL unicode = FALSE;
552 iShowCmd = ShlLnkHeader.nShowCommand;
553 wHotKey = ShlLnkHeader.wHotKey;
554 iIcoNdx = ShlLnkHeader.nIconIndex;
555 FileTimeToSystemTime (&ShlLnkHeader.ftCreationTime, &time1);
556 FileTimeToSystemTime (&ShlLnkHeader.ftLastAccessTime, &time2);
557 FileTimeToSystemTime (&ShlLnkHeader.ftLastWriteTime, &time3);
558 if (TRACE_ON(shell))
559 {
560 WCHAR sTemp[MAX_PATH];
561 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &time1,
562 NULL, sTemp, sizeof(sTemp) / sizeof(*sTemp));
563 TRACE("-- time1: %s\n", debugstr_w(sTemp));
564 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &time2,
565 NULL, sTemp, sizeof(sTemp) / sizeof(*sTemp));
566 TRACE("-- time2: %s\n", debugstr_w(sTemp));
567 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &time3,
568 NULL, sTemp, sizeof(sTemp) / sizeof(*sTemp));
569 TRACE("-- time3: %s\n", debugstr_w(sTemp));
570 }
571
572 /* load all the new stuff */
573 if (ShlLnkHeader.dwFlags & SLDF_HAS_ID_LIST)
574 {
575 hr = ILLoadFromStream(stm, &pPidl);
576 if (FAILED(hr))
577 return hr;
578 }
579 pdump(pPidl);
580
581 /* load the location information */
582 if (ShlLnkHeader.dwFlags & SLDF_HAS_LINK_INFO)
583 hr = Stream_LoadLocation(stm, &volume, &sPath);
584 if (FAILED(hr))
585 goto end;
586
587 if (ShlLnkHeader.dwFlags & SLDF_UNICODE)
588 unicode = TRUE;
589
590 if (ShlLnkHeader.dwFlags & SLDF_HAS_NAME)
591 {
592 hr = Stream_LoadString(stm, unicode, &sDescription);
593 TRACE("Description -> %s\n", debugstr_w(sDescription));
594 }
595 if (FAILED(hr))
596 goto end;
597
598 if (ShlLnkHeader.dwFlags & SLDF_HAS_RELPATH)
599 {
600 hr = Stream_LoadString(stm, unicode, &sPathRel);
601 TRACE("Relative Path-> %s\n", debugstr_w(sPathRel));
602 }
603 if (FAILED(hr))
604 goto end;
605
606 if (ShlLnkHeader.dwFlags & SLDF_HAS_WORKINGDIR)
607 {
608 hr = Stream_LoadString(stm, unicode, &sWorkDir);
609 PathRemoveBackslash(sWorkDir);
610 TRACE("Working Dir -> %s\n", debugstr_w(sWorkDir));
611 }
612 if (FAILED(hr))
613 goto end;
614
615 if (ShlLnkHeader.dwFlags & SLDF_HAS_ARGS)
616 {
617 hr = Stream_LoadString(stm, unicode, &sArgs);
618 TRACE("Arguments -> %s\n", debugstr_w(sArgs));
619 }
620 if (FAILED(hr))
621 goto end;
622
623 if (ShlLnkHeader.dwFlags & SLDF_HAS_ICONLOCATION)
624 {
625 hr = Stream_LoadString(stm, unicode, &sIcoPath);
626 TRACE("Icon file -> %s\n", debugstr_w(sIcoPath));
627 }
628 if (FAILED(hr))
629 goto end;
630
631 #if (NTDDI_VERSION < NTDDI_LONGHORN)
632 if (ShlLnkHeader.dwFlags & SLDF_HAS_LOGO3ID)
633 {
634 hr = Stream_LoadAdvertiseInfo(stm, &sProduct);
635 TRACE("Product -> %s\n", debugstr_w(sProduct));
636 }
637 if (FAILED(hr))
638 goto end;
639 #endif
640
641 if (ShlLnkHeader.dwFlags & SLDF_HAS_DARWINID)
642 {
643 hr = Stream_LoadAdvertiseInfo(stm, &sComponent);
644 TRACE("Component -> %s\n", debugstr_w(sComponent));
645 }
646 if (ShlLnkHeader.dwFlags & SLDF_RUNAS_USER)
647 {
648 bRunAs = TRUE;
649 }
650 else
651 {
652 bRunAs = FALSE;
653 }
654
655 if (FAILED(hr))
656 goto end;
657
658 DWORD dwZero;
659 hr = stm->Read(&dwZero, sizeof(dwZero), &dwBytesRead);
660 if (FAILED(hr) || dwZero || dwBytesRead != sizeof(dwZero))
661 ERR("Last word was not zero\n");
662
663 TRACE("OK\n");
664
665 pdump (pPidl);
666
667 return S_OK;
668
669 end:
670 return hr;
671 }
672
673 /************************************************************************
674 * Stream_WriteString
675 *
676 * Helper function for IPersistStream_Save. Writes a unicode string
677 * with terminating nul byte to a stream, preceded by the its length.
678 */
679 static HRESULT Stream_WriteString(IStream* stm, LPCWSTR str)
680 {
681 USHORT len = wcslen(str) + 1;
682 DWORD count;
683
684 HRESULT hr = stm->Write(&len, sizeof(len), &count);
685 if (FAILED(hr))
686 return hr;
687
688 len *= sizeof(WCHAR);
689
690 hr = stm->Write(str, len, &count);
691 if (FAILED(hr))
692 return hr;
693
694 return S_OK;
695 }
696
697 /************************************************************************
698 * Stream_WriteLocationInfo
699 *
700 * Writes the location info to a stream
701 *
702 * FIXME: One day we might want to write the network volume information
703 * and the final path.
704 * Figure out how Windows deals with unicode paths here.
705 */
706 static HRESULT Stream_WriteLocationInfo(IStream* stm, LPCWSTR path,
707 CShellLink::volume_info *volume)
708 {
709 LOCAL_VOLUME_INFO *vol;
710 LOCATION_INFO *loc;
711
712 TRACE("%p %s %p\n", stm, debugstr_w(path), volume);
713
714 /* figure out the size of everything */
715 DWORD label_size = WideCharToMultiByte(CP_ACP, 0, volume->label, -1,
716 NULL, 0, NULL, NULL);
717 DWORD path_size = WideCharToMultiByte(CP_ACP, 0, path, -1,
718 NULL, 0, NULL, NULL);
719 DWORD volume_info_size = sizeof(*vol) + label_size;
720 DWORD final_path_size = 1;
721 DWORD total_size = sizeof(*loc) + volume_info_size + path_size + final_path_size;
722
723 /* create pointers to everything */
724 loc = static_cast<LOCATION_INFO *>(HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, total_size));
725 vol = (LOCAL_VOLUME_INFO*) &loc[1];
726 LPSTR szLabel = (LPSTR) &vol[1];
727 LPSTR szPath = &szLabel[label_size];
728 LPSTR szFinalPath = &szPath[path_size];
729
730 /* fill in the location information header */
731 loc->dwTotalSize = total_size;
732 loc->dwHeaderSize = sizeof (*loc);
733 loc->dwFlags = 1;
734 loc->dwVolTableOfs = sizeof (*loc);
735 loc->dwLocalPathOfs = sizeof (*loc) + volume_info_size;
736 loc->dwNetworkVolTableOfs = 0;
737 loc->dwFinalPathOfs = sizeof (*loc) + volume_info_size + path_size;
738
739 /* fill in the volume information */
740 vol->dwSize = volume_info_size;
741 vol->dwType = volume->type;
742 vol->dwVolSerial = volume->serial;
743 vol->dwVolLabelOfs = sizeof (*vol);
744
745 /* copy in the strings */
746 WideCharToMultiByte(CP_ACP, 0, volume->label, -1,
747 szLabel, label_size, NULL, NULL);
748 WideCharToMultiByte(CP_ACP, 0, path, -1,
749 szPath, path_size, NULL, NULL);
750 szFinalPath[0] = 0;
751
752 ULONG count = 0;
753 HRESULT hr = stm->Write(loc, total_size, &count);
754 HeapFree(GetProcessHeap(), 0, loc);
755
756 return hr;
757 }
758
759 static EXP_DARWIN_LINK* shelllink_build_darwinid(LPCWSTR string, DWORD magic)
760 {
761 EXP_DARWIN_LINK *buffer = (EXP_DARWIN_LINK *)LocalAlloc(LMEM_ZEROINIT, sizeof * buffer);
762 buffer->dbh.cbSize = sizeof * buffer;
763 buffer->dbh.dwSignature = magic;
764 lstrcpynW(buffer->szwDarwinID, string, MAX_PATH);
765 WideCharToMultiByte(CP_ACP, 0, string, -1, buffer->szDarwinID, MAX_PATH, NULL, NULL);
766
767 return buffer;
768 }
769
770 static HRESULT Stream_WriteAdvertiseInfo(IStream* stm, LPCWSTR string, DWORD magic)
771 {
772 TRACE("%p\n", stm);
773
774 EXP_DARWIN_LINK *buffer = shelllink_build_darwinid(string, magic);
775
776 ULONG count;
777 return stm->Write(buffer, buffer->dbh.cbSize, &count);
778 }
779
780 /************************************************************************
781 * IPersistStream_Save (IPersistStream)
782 *
783 * FIXME: makes assumptions about byte order
784 */
785 HRESULT WINAPI CShellLink::Save(IStream *stm, BOOL fClearDirty)
786 {
787 TRACE("%p %p %x\n", this, stm, fClearDirty);
788
789 SHELL_LINK_HEADER ShlLnkHeader;
790 memset(&ShlLnkHeader, 0, sizeof(ShlLnkHeader));
791 ShlLnkHeader.dwSize = sizeof(ShlLnkHeader);
792 ShlLnkHeader.nShowCommand = iShowCmd;
793 ShlLnkHeader.clsid = CLSID_ShellLink;
794
795 ShlLnkHeader.wHotKey = wHotKey;
796 ShlLnkHeader.nIconIndex = iIcoNdx;
797 ShlLnkHeader.dwFlags = SLDF_UNICODE; /* strings are in unicode */
798 if (pPidl)
799 ShlLnkHeader.dwFlags |= SLDF_HAS_ID_LIST;
800 if (sPath)
801 ShlLnkHeader.dwFlags |= SLDF_HAS_LINK_INFO;
802 if (sDescription)
803 ShlLnkHeader.dwFlags |= SLDF_HAS_NAME;
804 if (sWorkDir)
805 ShlLnkHeader.dwFlags |= SLDF_HAS_WORKINGDIR;
806 if (sArgs)
807 ShlLnkHeader.dwFlags |= SLDF_HAS_ARGS;
808 if (sIcoPath)
809 ShlLnkHeader.dwFlags |= SLDF_HAS_ICONLOCATION;
810 #if (NTDDI_VERSION < NTDDI_LONGHORN)
811 if (sProduct)
812 ShlLnkHeader.dwFlags |= SLDF_HAS_LOGO3ID;
813 #endif
814 if (sComponent)
815 ShlLnkHeader.dwFlags |= SLDF_HAS_DARWINID;
816 if (bRunAs)
817 ShlLnkHeader.dwFlags |= SLDF_RUNAS_USER;
818
819 SystemTimeToFileTime (&time1, &ShlLnkHeader.ftCreationTime);
820 SystemTimeToFileTime (&time2, &ShlLnkHeader.ftLastAccessTime);
821 SystemTimeToFileTime (&time3, &ShlLnkHeader.ftLastWriteTime);
822
823 /* write the Shortcut header */
824 ULONG count;
825 HRESULT hr = stm->Write(&ShlLnkHeader, sizeof(ShlLnkHeader), &count);
826 if (FAILED(hr))
827 {
828 ERR("Write failed\n");
829 return hr;
830 }
831
832 TRACE("Writing pidl\n");
833
834 /* write the PIDL to the shortcut */
835 if (pPidl)
836 {
837 hr = ILSaveToStream(stm, pPidl);
838 if (FAILED(hr))
839 {
840 ERR("Failed to write PIDL\n");
841 return hr;
842 }
843 }
844
845 if (sPath)
846 Stream_WriteLocationInfo(stm, sPath, &volume);
847
848 if (sDescription)
849 hr = Stream_WriteString(stm, sDescription);
850
851 if (sPathRel)
852 hr = Stream_WriteString(stm, sPathRel);
853
854 if (sWorkDir)
855 hr = Stream_WriteString(stm, sWorkDir);
856
857 if (sArgs)
858 hr = Stream_WriteString(stm, sArgs);
859
860 if (sIcoPath)
861 hr = Stream_WriteString(stm, sIcoPath);
862
863 if (sProduct)
864 hr = Stream_WriteAdvertiseInfo(stm, sProduct, EXP_SZ_ICON_SIG);
865
866 if (sComponent)
867 hr = Stream_WriteAdvertiseInfo(stm, sComponent, EXP_DARWIN_ID_SIG);
868
869 /* the last field is a single zero dword */
870 DWORD zero = 0;
871 hr = stm->Write(&zero, sizeof zero, &count);
872
873 return S_OK;
874 }
875
876 /************************************************************************
877 * IPersistStream_GetSizeMax (IPersistStream)
878 */
879 HRESULT WINAPI CShellLink::GetSizeMax(ULARGE_INTEGER *pcbSize)
880 {
881 TRACE("(%p)\n", this);
882
883 return E_NOTIMPL;
884 }
885
886 static BOOL SHELL_ExistsFileW(LPCWSTR path)
887 {
888 if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW(path))
889 return FALSE;
890
891 return TRUE;
892 }
893
894 /**************************************************************************
895 * ShellLink_UpdatePath
896 * update absolute path in sPath using relative path in sPathRel
897 */
898 static HRESULT ShellLink_UpdatePath(LPCWSTR sPathRel, LPCWSTR path, LPCWSTR sWorkDir, LPWSTR* psPath)
899 {
900 if (!path || !psPath)
901 return E_INVALIDARG;
902
903 if (!*psPath && sPathRel)
904 {
905 WCHAR buffer[2*MAX_PATH], abs_path[2*MAX_PATH];
906 LPWSTR final = NULL;
907
908 /* first try if [directory of link file] + [relative path] finds an existing file */
909
910 GetFullPathNameW(path, MAX_PATH * 2, buffer, &final);
911 if (!final)
912 final = buffer;
913 wcscpy(final, sPathRel);
914
915 *abs_path = '\0';
916
917 if (SHELL_ExistsFileW(buffer))
918 {
919 if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
920 wcscpy(abs_path, buffer);
921 }
922 else
923 {
924 /* try if [working directory] + [relative path] finds an existing file */
925 if (sWorkDir)
926 {
927 wcscpy(buffer, sWorkDir);
928 wcscpy(PathAddBackslashW(buffer), sPathRel);
929
930 if (SHELL_ExistsFileW(buffer))
931 if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
932 wcscpy(abs_path, buffer);
933 }
934 }
935
936 /* FIXME: This is even not enough - not all shell links can be resolved using this algorithm. */
937 if (!*abs_path)
938 wcscpy(abs_path, sPathRel);
939
940 *psPath = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, (wcslen(abs_path) + 1) * sizeof(WCHAR));
941 if (!*psPath)
942 return E_OUTOFMEMORY;
943
944 wcscpy(*psPath, abs_path);
945 }
946
947 return S_OK;
948 }
949
950 HRESULT WINAPI CShellLink::GetPath(LPSTR pszFile, INT cchMaxPath, WIN32_FIND_DATAA *pfd, DWORD fFlags)
951 {
952 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%u)(%s)\n",
953 this, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(sPath));
954
955 if (sComponent || sProduct)
956 return S_FALSE;
957
958 if (cchMaxPath)
959 pszFile[0] = 0;
960
961 if (sPath)
962 WideCharToMultiByte(CP_ACP, 0, sPath, -1,
963 pszFile, cchMaxPath, NULL, NULL);
964
965 if (pfd) FIXME("(%p): WIN32_FIND_DATA is not yet filled.\n", this);
966
967 return S_OK;
968 }
969
970 HRESULT WINAPI CShellLink::GetIDList(LPITEMIDLIST * ppidl)
971 {
972 TRACE("(%p)->(ppidl=%p)\n", this, ppidl);
973
974 if (!pPidl)
975 {
976 *ppidl = NULL;
977 return S_FALSE;
978 }
979
980 *ppidl = ILClone(pPidl);
981 return S_OK;
982 }
983
984 HRESULT WINAPI CShellLink::SetIDList(LPCITEMIDLIST pidl)
985 {
986 TRACE("(%p)->(pidl=%p)\n", this, pidl);
987
988 if (pPidl)
989 ILFree(pPidl);
990
991 pPidl = ILClone(pidl);
992 if (!pPidl)
993 return E_FAIL;
994
995 bDirty = TRUE;
996
997 return S_OK;
998 }
999
1000 HRESULT WINAPI CShellLink::GetDescription(LPSTR pszName, INT cchMaxName)
1001 {
1002 TRACE("(%p)->(%p len=%u)\n", this, pszName, cchMaxName);
1003
1004 if (cchMaxName)
1005 pszName[0] = 0;
1006
1007 if (sDescription)
1008 WideCharToMultiByte(CP_ACP, 0, sDescription, -1,
1009 pszName, cchMaxName, NULL, NULL);
1010
1011 return S_OK;
1012 }
1013
1014 HRESULT WINAPI CShellLink::SetDescription(LPCSTR pszName)
1015 {
1016 TRACE("(%p)->(pName=%s)\n", this, pszName);
1017
1018 HeapFree(GetProcessHeap(), 0, sDescription);
1019 sDescription = NULL;
1020
1021 if (pszName)
1022 {
1023 sDescription = HEAP_strdupAtoW(GetProcessHeap(), 0, pszName);
1024 if (!sDescription)
1025 return E_OUTOFMEMORY;
1026 }
1027 bDirty = TRUE;
1028
1029 return S_OK;
1030 }
1031
1032 HRESULT WINAPI CShellLink::GetWorkingDirectory(LPSTR pszDir, INT cchMaxPath)
1033 {
1034 TRACE("(%p)->(%p len=%u)\n", this, pszDir, cchMaxPath);
1035
1036 if (cchMaxPath)
1037 pszDir[0] = 0;
1038
1039 if (sWorkDir)
1040 WideCharToMultiByte(CP_ACP, 0, sWorkDir, -1,
1041 pszDir, cchMaxPath, NULL, NULL);
1042
1043 return S_OK;
1044 }
1045
1046 HRESULT WINAPI CShellLink::SetWorkingDirectory(LPCSTR pszDir)
1047 {
1048 TRACE("(%p)->(dir=%s)\n", this, pszDir);
1049
1050 HeapFree(GetProcessHeap(), 0, sWorkDir);
1051 sWorkDir = NULL;
1052
1053 if (pszDir)
1054 {
1055 sWorkDir = HEAP_strdupAtoW(GetProcessHeap(), 0, pszDir);
1056 if (!sWorkDir)
1057 return E_OUTOFMEMORY;
1058 }
1059 bDirty = TRUE;
1060
1061 return S_OK;
1062 }
1063
1064 HRESULT WINAPI CShellLink::GetArguments(LPSTR pszArgs, INT cchMaxPath)
1065 {
1066 TRACE("(%p)->(%p len=%u)\n", this, pszArgs, cchMaxPath);
1067
1068 if (cchMaxPath)
1069 pszArgs[0] = 0;
1070 if (sArgs)
1071 WideCharToMultiByte(CP_ACP, 0, sArgs, -1,
1072 pszArgs, cchMaxPath, NULL, NULL);
1073
1074 return S_OK;
1075 }
1076
1077 HRESULT WINAPI CShellLink::SetArguments(LPCSTR pszArgs)
1078 {
1079 TRACE("(%p)->(args=%s)\n", this, pszArgs);
1080
1081 HeapFree(GetProcessHeap(), 0, sArgs);
1082 sArgs = NULL;
1083
1084 if (pszArgs)
1085 {
1086 sArgs = HEAP_strdupAtoW(GetProcessHeap(), 0, pszArgs);
1087 if (!sArgs)
1088 return E_OUTOFMEMORY;
1089 }
1090
1091 bDirty = TRUE;
1092
1093 return S_OK;
1094 }
1095
1096 HRESULT WINAPI CShellLink::GetHotkey(WORD *pwHotkey)
1097 {
1098 TRACE("(%p)->(%p)(0x%08x)\n", this, pwHotkey, wHotKey);
1099
1100 *pwHotkey = wHotKey;
1101
1102 return S_OK;
1103 }
1104
1105 HRESULT WINAPI CShellLink::SetHotkey(WORD wHotkey)
1106 {
1107 TRACE("(%p)->(hotkey=%x)\n", this, wHotkey);
1108
1109 wHotKey = wHotkey;
1110 bDirty = TRUE;
1111
1112 return S_OK;
1113 }
1114
1115 HRESULT WINAPI CShellLink::GetShowCmd(INT *piShowCmd)
1116 {
1117 TRACE("(%p)->(%p)\n", this, piShowCmd);
1118 *piShowCmd = iShowCmd;
1119 return S_OK;
1120 }
1121
1122 HRESULT WINAPI CShellLink::SetShowCmd(INT iShowCmd)
1123 {
1124 TRACE("(%p) %d\n", this, iShowCmd);
1125
1126 this->iShowCmd = iShowCmd;
1127 bDirty = TRUE;
1128
1129 return S_OK;
1130 }
1131
1132 static HRESULT SHELL_PidlGeticonLocationA(IShellFolder* psf, LPCITEMIDLIST pidl,
1133 LPSTR pszIconPath, int cchIconPath, int* piIcon)
1134 {
1135 LPCITEMIDLIST pidlLast;
1136
1137 HRESULT hr = SHBindToParent(pidl, IID_PPV_ARG(IShellFolder, &psf), &pidlLast);
1138
1139 if (SUCCEEDED(hr))
1140 {
1141 CComPtr<IExtractIconA> pei;
1142
1143 hr = psf->GetUIObjectOf(0, 1, &pidlLast, IID_NULL_PPV_ARG(IExtractIconA, &pei));
1144
1145 if (SUCCEEDED(hr))
1146 hr = pei->GetIconLocation(0, pszIconPath, MAX_PATH, piIcon, NULL);
1147
1148 psf->Release();
1149 }
1150
1151 return hr;
1152 }
1153
1154 HRESULT WINAPI CShellLink::GetIconLocation(LPSTR pszIconPath, INT cchIconPath, INT *piIcon)
1155 {
1156 TRACE("(%p)->(%p len=%u iicon=%p)\n", this, pszIconPath, cchIconPath, piIcon);
1157
1158 pszIconPath[0] = 0;
1159 *piIcon = iIcoNdx;
1160
1161 if (sIcoPath)
1162 {
1163 WideCharToMultiByte(CP_ACP, 0, sIcoPath, -1, pszIconPath, cchIconPath, NULL, NULL);
1164 return S_OK;
1165 }
1166
1167 if (pPidl || sPath)
1168 {
1169 CComPtr<IShellFolder> pdsk;
1170
1171 HRESULT hr = SHGetDesktopFolder(&pdsk);
1172
1173 if (SUCCEEDED(hr))
1174 {
1175 /* first look for an icon using the PIDL (if present) */
1176 if (pPidl)
1177 hr = SHELL_PidlGeticonLocationA(pdsk, pPidl, pszIconPath, cchIconPath, piIcon);
1178 else
1179 hr = E_FAIL;
1180
1181 /* if we couldn't find an icon yet, look for it using the file system path */
1182 if (FAILED(hr) && sPath)
1183 {
1184 LPITEMIDLIST pidl;
1185
1186 hr = pdsk->ParseDisplayName(0, NULL, sPath, NULL, &pidl, NULL);
1187
1188 if (SUCCEEDED(hr))
1189 {
1190 hr = SHELL_PidlGeticonLocationA(pdsk, pidl, pszIconPath, cchIconPath, piIcon);
1191
1192 SHFree(pidl);
1193 }
1194 }
1195 }
1196
1197 return hr;
1198 }
1199 return S_OK;
1200 }
1201
1202 HRESULT WINAPI CShellLink::SetIconLocation(LPCSTR pszIconPath, INT iIcon)
1203 {
1204 TRACE("(%p)->(path=%s iicon=%u)\n", this, pszIconPath, iIcon);
1205
1206 HeapFree(GetProcessHeap(), 0, sIcoPath);
1207 sIcoPath = NULL;
1208
1209 if (pszIconPath)
1210 {
1211 sIcoPath = HEAP_strdupAtoW(GetProcessHeap(), 0, pszIconPath);
1212 if (!sIcoPath)
1213 return E_OUTOFMEMORY;
1214 }
1215
1216 iIcoNdx = iIcon;
1217 bDirty = TRUE;
1218
1219 return S_OK;
1220 }
1221
1222 HRESULT WINAPI CShellLink::SetRelativePath(LPCSTR pszPathRel, DWORD dwReserved)
1223 {
1224 TRACE("(%p)->(path=%s %x)\n", this, pszPathRel, dwReserved);
1225
1226 HeapFree(GetProcessHeap(), 0, sPathRel);
1227 sPathRel = NULL;
1228
1229 if (pszPathRel)
1230 {
1231 sPathRel = HEAP_strdupAtoW(GetProcessHeap(), 0, pszPathRel);
1232 bDirty = TRUE;
1233 }
1234
1235 return ShellLink_UpdatePath(sPathRel, sPath, sWorkDir, &sPath);
1236 }
1237
1238 HRESULT WINAPI CShellLink::Resolve(HWND hwnd, DWORD fFlags)
1239 {
1240 HRESULT hr = S_OK;
1241 BOOL bSuccess;
1242
1243 TRACE("(%p)->(hwnd=%p flags=%x)\n", this, hwnd, fFlags);
1244
1245 /*FIXME: use IResolveShellLink interface */
1246
1247 if (!sPath && pPidl)
1248 {
1249 WCHAR buffer[MAX_PATH];
1250
1251 bSuccess = SHGetPathFromIDListW(pPidl, buffer);
1252
1253 if (bSuccess && *buffer)
1254 {
1255 sPath = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, (wcslen(buffer) + 1) * sizeof(WCHAR));
1256
1257 if (!sPath)
1258 return E_OUTOFMEMORY;
1259
1260 wcscpy(sPath, buffer);
1261
1262 bDirty = TRUE;
1263 }
1264 else
1265 hr = S_OK; /* don't report an error occurred while just caching information */
1266 }
1267
1268 if (!sIcoPath && sPath)
1269 {
1270 sIcoPath = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, (wcslen(sPath) + 1) * sizeof(WCHAR));
1271
1272 if (!sIcoPath)
1273 return E_OUTOFMEMORY;
1274
1275 wcscpy(sIcoPath, sPath);
1276 iIcoNdx = 0;
1277
1278 bDirty = TRUE;
1279 }
1280
1281 return hr;
1282 }
1283
1284 HRESULT WINAPI CShellLink::SetPath(LPCSTR pszFile)
1285 {
1286 TRACE("(%p)->(path=%s)\n", this, pszFile);
1287 if (pszFile == NULL)
1288 return E_INVALIDARG;
1289
1290 LPWSTR str = HEAP_strdupAtoW(GetProcessHeap(), 0, pszFile);
1291 if (!str)
1292 return E_OUTOFMEMORY;
1293
1294 HRESULT hr = SetPath(str);
1295 HeapFree(GetProcessHeap(), 0, str);
1296
1297 return hr;
1298 }
1299
1300 HRESULT WINAPI CShellLink::GetPath(LPWSTR pszFile, INT cchMaxPath, WIN32_FIND_DATAW *pfd, DWORD fFlags)
1301 {
1302 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%u)(%s)\n",
1303 this, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(sPath));
1304
1305 if (sComponent || sProduct)
1306 return S_FALSE;
1307
1308 if (cchMaxPath)
1309 pszFile[0] = 0;
1310
1311 if (sPath)
1312 lstrcpynW(pszFile, sPath, cchMaxPath);
1313
1314 if (pfd) FIXME("(%p): WIN32_FIND_DATA is not yet filled.\n", this);
1315
1316 return S_OK;
1317 }
1318
1319 HRESULT WINAPI CShellLink::GetDescription(LPWSTR pszName, INT cchMaxName)
1320 {
1321 TRACE("(%p)->(%p len=%u)\n", this, pszName, cchMaxName);
1322
1323 pszName[0] = 0;
1324 if (sDescription)
1325 lstrcpynW(pszName, sDescription, cchMaxName);
1326
1327 return S_OK;
1328 }
1329
1330 HRESULT WINAPI CShellLink::SetDescription(LPCWSTR pszName)
1331 {
1332 TRACE("(%p)->(desc=%s)\n", this, debugstr_w(pszName));
1333
1334 HeapFree(GetProcessHeap(), 0, sDescription);
1335 if (pszName)
1336 {
1337 sDescription = (LPWSTR)HeapAlloc(GetProcessHeap(), 0,
1338 (wcslen(pszName) + 1) * sizeof(WCHAR));
1339 if (!sDescription)
1340 return E_OUTOFMEMORY;
1341
1342 wcscpy(sDescription, pszName);
1343 }
1344 else
1345 sDescription = NULL;
1346
1347 bDirty = TRUE;
1348
1349 return S_OK;
1350 }
1351
1352 HRESULT WINAPI CShellLink::GetWorkingDirectory(LPWSTR pszDir, INT cchMaxPath)
1353 {
1354 TRACE("(%p)->(%p len %u)\n", this, pszDir, cchMaxPath);
1355
1356 if (cchMaxPath)
1357 pszDir[0] = 0;
1358 if (sWorkDir)
1359 lstrcpynW(pszDir, sWorkDir, cchMaxPath);
1360
1361 return S_OK;
1362 }
1363
1364 HRESULT WINAPI CShellLink::SetWorkingDirectory(LPCWSTR pszDir)
1365 {
1366 TRACE("(%p)->(dir=%s)\n", this, debugstr_w(pszDir));
1367
1368 HeapFree(GetProcessHeap(), 0, sWorkDir);
1369 if (pszDir)
1370 {
1371 sWorkDir = (LPWSTR)HeapAlloc(GetProcessHeap(), 0,
1372 (wcslen(pszDir) + 1) * sizeof (WCHAR));
1373 if (!sWorkDir)
1374 return E_OUTOFMEMORY;
1375 wcscpy(sWorkDir, pszDir);
1376 }
1377 else
1378 sWorkDir = NULL;
1379
1380 bDirty = TRUE;
1381
1382 return S_OK;
1383 }
1384
1385 HRESULT WINAPI CShellLink::GetArguments(LPWSTR pszArgs, INT cchMaxPath)
1386 {
1387 TRACE("(%p)->(%p len=%u)\n", this, pszArgs, cchMaxPath);
1388
1389 if (cchMaxPath)
1390 pszArgs[0] = 0;
1391 if (sArgs)
1392 lstrcpynW(pszArgs, sArgs, cchMaxPath);
1393
1394 return S_OK;
1395 }
1396
1397 HRESULT WINAPI CShellLink::SetArguments(LPCWSTR pszArgs)
1398 {
1399 TRACE("(%p)->(args=%s)\n", this, debugstr_w(pszArgs));
1400
1401 HeapFree(GetProcessHeap(), 0, sArgs);
1402 if (pszArgs)
1403 {
1404 sArgs = (LPWSTR)HeapAlloc(GetProcessHeap(), 0,
1405 (wcslen(pszArgs) + 1) * sizeof (WCHAR));
1406 if (!sArgs)
1407 return E_OUTOFMEMORY;
1408
1409 wcscpy(sArgs, pszArgs);
1410 }
1411 else
1412 sArgs = NULL;
1413
1414 bDirty = TRUE;
1415
1416 return S_OK;
1417 }
1418
1419 static HRESULT SHELL_PidlGeticonLocationW(IShellFolder* psf, LPCITEMIDLIST pidl,
1420 LPWSTR pszIconPath, int cchIconPath, int* piIcon)
1421 {
1422 LPCITEMIDLIST pidlLast;
1423 UINT wFlags;
1424
1425 HRESULT hr = SHBindToParent(pidl, IID_PPV_ARG(IShellFolder, &psf), &pidlLast);
1426
1427 if (SUCCEEDED(hr))
1428 {
1429 CComPtr<IExtractIconW> pei;
1430
1431 hr = psf->GetUIObjectOf(0, 1, &pidlLast, IID_NULL_PPV_ARG(IExtractIconW, &pei));
1432
1433 if (SUCCEEDED(hr))
1434 hr = pei->GetIconLocation(0, pszIconPath, MAX_PATH, piIcon, &wFlags);
1435
1436 psf->Release();
1437 }
1438
1439 return hr;
1440 }
1441
1442 HRESULT WINAPI CShellLink::GetIconLocation(LPWSTR pszIconPath, INT cchIconPath, INT *piIcon)
1443 {
1444 TRACE("(%p)->(%p len=%u iicon=%p)\n", this, pszIconPath, cchIconPath, piIcon);
1445
1446 pszIconPath[0] = 0;
1447 *piIcon = iIcoNdx;
1448
1449 if (sIcoPath)
1450 {
1451 lstrcpynW(pszIconPath, sIcoPath, cchIconPath);
1452 return S_OK;
1453 }
1454
1455 if (pPidl || sPath)
1456 {
1457 CComPtr<IShellFolder> pdsk;
1458
1459 HRESULT hr = SHGetDesktopFolder(&pdsk);
1460
1461 if (SUCCEEDED(hr))
1462 {
1463 /* first look for an icon using the PIDL (if present) */
1464 if (pPidl)
1465 hr = SHELL_PidlGeticonLocationW(pdsk, pPidl, pszIconPath, cchIconPath, piIcon);
1466 else
1467 hr = E_FAIL;
1468
1469 /* if we couldn't find an icon yet, look for it using the file system path */
1470 if (FAILED(hr) && sPath)
1471 {
1472 LPITEMIDLIST pidl;
1473
1474 hr = pdsk->ParseDisplayName(0, NULL, sPath, NULL, &pidl, NULL);
1475
1476 if (SUCCEEDED(hr))
1477 {
1478 hr = SHELL_PidlGeticonLocationW(pdsk, pidl, pszIconPath, cchIconPath, piIcon);
1479
1480 SHFree(pidl);
1481 }
1482 }
1483 }
1484 return hr;
1485 }
1486 return S_OK;
1487 }
1488
1489 HRESULT WINAPI CShellLink::SetIconLocation(LPCWSTR pszIconPath, INT iIcon)
1490 {
1491 TRACE("(%p)->(path=%s iicon=%u)\n", this, debugstr_w(pszIconPath), iIcon);
1492
1493 HeapFree(GetProcessHeap(), 0, sIcoPath);
1494 if (pszIconPath)
1495 {
1496 sIcoPath = (LPWSTR)HeapAlloc(GetProcessHeap(), 0,
1497 (wcslen(pszIconPath) + 1) * sizeof (WCHAR));
1498 if (!sIcoPath)
1499 return E_OUTOFMEMORY;
1500 wcscpy(sIcoPath, pszIconPath);
1501 }
1502 else
1503 sIcoPath = NULL;
1504
1505 iIcoNdx = iIcon;
1506 bDirty = TRUE;
1507
1508 return S_OK;
1509 }
1510
1511 HRESULT WINAPI CShellLink::SetRelativePath(LPCWSTR pszPathRel, DWORD dwReserved)
1512 {
1513 TRACE("(%p)->(path=%s %x)\n", this, debugstr_w(pszPathRel), dwReserved);
1514
1515 HeapFree(GetProcessHeap(), 0, sPathRel);
1516 if (pszPathRel)
1517 {
1518 sPathRel = (LPWSTR)HeapAlloc(GetProcessHeap(), 0,
1519 (wcslen(pszPathRel) + 1) * sizeof (WCHAR));
1520 if (!sPathRel)
1521 return E_OUTOFMEMORY;
1522 wcscpy(sPathRel, pszPathRel);
1523 }
1524 else
1525 sPathRel = NULL;
1526
1527 bDirty = TRUE;
1528
1529 return ShellLink_UpdatePath(sPathRel, sPath, sWorkDir, &sPath);
1530 }
1531
1532 LPWSTR CShellLink::ShellLink_GetAdvertisedArg(LPCWSTR str)
1533 {
1534 if (!str)
1535 return NULL;
1536
1537 LPCWSTR p = wcschr(str, L':');
1538 if (!p)
1539 return NULL;
1540 DWORD len = p - str;
1541 LPWSTR ret = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * (len + 1));
1542 if (!ret)
1543 return ret;
1544 memcpy(ret, str, sizeof(WCHAR)*len);
1545 ret[len] = 0;
1546 return ret;
1547 }
1548
1549 HRESULT CShellLink::ShellLink_SetAdvertiseInfo(LPCWSTR str)
1550 {
1551 LPCWSTR szComponent = NULL, szProduct = NULL;
1552
1553 while (str[0])
1554 {
1555 /* each segment must start with two colons */
1556 if (str[0] != ':' || str[1] != ':')
1557 return E_FAIL;
1558
1559 /* the last segment is just two colons */
1560 if (!str[2])
1561 break;
1562 str += 2;
1563
1564 /* there must be a colon straight after a guid */
1565 LPCWSTR p = wcschr(str, L':');
1566 if (!p)
1567 return E_FAIL;
1568 INT len = p - str;
1569 if (len != 38)
1570 return E_FAIL;
1571
1572 /* get the guid, and check it's validly formatted */
1573 WCHAR szGuid[39];
1574 memcpy(szGuid, str, sizeof(WCHAR)*len);
1575 szGuid[len] = 0;
1576
1577 GUID guid;
1578 HRESULT hr = CLSIDFromString(szGuid, &guid);
1579 if (hr != S_OK)
1580 return hr;
1581 str = p + 1;
1582
1583 /* match it up to a guid that we care about */
1584 if (IsEqualGUID(guid, SHELL32_AdvtShortcutComponent) && !szComponent)
1585 szComponent = str;
1586 else if (IsEqualGUID(guid, SHELL32_AdvtShortcutProduct) && !szProduct)
1587 szProduct = str;
1588 else
1589 return E_FAIL;
1590
1591 /* skip to the next field */
1592 str = wcschr(str, L':');
1593 if (!str)
1594 return E_FAIL;
1595 }
1596
1597 /* we have to have a component for an advertised shortcut */
1598 if (!szComponent)
1599 return E_FAIL;
1600
1601 sComponent = ShellLink_GetAdvertisedArg(szComponent);
1602 sProduct = ShellLink_GetAdvertisedArg(szProduct);
1603
1604 TRACE("Component = %s\n", debugstr_w(sComponent));
1605 TRACE("Product = %s\n", debugstr_w(sProduct));
1606
1607 return S_OK;
1608 }
1609
1610 static BOOL ShellLink_GetVolumeInfo(LPCWSTR path, CShellLink::volume_info *volume)
1611 {
1612 WCHAR drive[4] = { path[0], ':', '\\', 0 };
1613
1614 volume->type = GetDriveTypeW(drive);
1615 BOOL bRet = GetVolumeInformationW(drive, volume->label, _countof(volume->label), &volume->serial, NULL, NULL, NULL, 0);
1616 TRACE("ret = %d type %d serial %08x name %s\n", bRet,
1617 volume->type, volume->serial, debugstr_w(volume->label));
1618 return bRet;
1619 }
1620
1621 HRESULT WINAPI CShellLink::SetPath(LPCWSTR pszFile)
1622 {
1623 LPWSTR unquoted = NULL;
1624 HRESULT hr = S_OK;
1625
1626 TRACE("(%p)->(path=%s)\n", this, debugstr_w(pszFile));
1627
1628 if (!pszFile)
1629 return E_INVALIDARG;
1630
1631 /* quotes at the ends of the string are stripped */
1632 UINT len = wcslen(pszFile);
1633 if (pszFile[0] == '"' && pszFile[len-1] == '"')
1634 {
1635 unquoted = strdupW(pszFile);
1636 PathUnquoteSpacesW(unquoted);
1637 pszFile = unquoted;
1638 }
1639
1640 /* any other quote marks are invalid */
1641 if (wcschr(pszFile, '"'))
1642 {
1643 HeapFree(GetProcessHeap(), 0, unquoted);
1644 return S_FALSE;
1645 }
1646
1647 HeapFree(GetProcessHeap(), 0, sPath);
1648 sPath = NULL;
1649
1650 HeapFree(GetProcessHeap(), 0, sComponent);
1651 sComponent = NULL;
1652
1653 if (pPidl)
1654 ILFree(pPidl);
1655 pPidl = NULL;
1656
1657 if (S_OK != ShellLink_SetAdvertiseInfo(pszFile))
1658 {
1659 WCHAR buffer[MAX_PATH];
1660 LPWSTR fname;
1661
1662 if (*pszFile == '\0')
1663 *buffer = '\0';
1664 else if (!GetFullPathNameW(pszFile, MAX_PATH, buffer, &fname))
1665 return E_FAIL;
1666 else if(!PathFileExistsW(buffer) &&
1667 !SearchPathW(NULL, pszFile, NULL, MAX_PATH, buffer, NULL))
1668 hr = S_FALSE;
1669
1670 pPidl = SHSimpleIDListFromPathW(pszFile);
1671 ShellLink_GetVolumeInfo(buffer, &volume);
1672
1673 sPath = (LPWSTR)HeapAlloc(GetProcessHeap(), 0,
1674 (wcslen(buffer) + 1) * sizeof (WCHAR));
1675 if (!sPath)
1676 return E_OUTOFMEMORY;
1677
1678 wcscpy(sPath, buffer);
1679 }
1680
1681 bDirty = TRUE;
1682 HeapFree(GetProcessHeap(), 0, unquoted);
1683
1684 return hr;
1685 }
1686
1687 HRESULT WINAPI CShellLink::AddDataBlock(void* pDataBlock)
1688 {
1689 FIXME("\n");
1690 return E_NOTIMPL;
1691 }
1692
1693 HRESULT WINAPI CShellLink::CopyDataBlock(DWORD dwSig, void** ppDataBlock)
1694 {
1695 LPVOID block = NULL;
1696 HRESULT hr = E_FAIL;
1697
1698 TRACE("%p %08x %p\n", this, dwSig, ppDataBlock);
1699
1700 switch (dwSig)
1701 {
1702 case EXP_DARWIN_ID_SIG:
1703 if (!sComponent)
1704 break;
1705 block = shelllink_build_darwinid(sComponent, dwSig);
1706 hr = S_OK;
1707 break;
1708 case EXP_SZ_LINK_SIG:
1709 case NT_CONSOLE_PROPS_SIG:
1710 case NT_FE_CONSOLE_PROPS_SIG:
1711 case EXP_SPECIAL_FOLDER_SIG:
1712 case EXP_SZ_ICON_SIG:
1713 FIXME("valid but unhandled datablock %08x\n", dwSig);
1714 break;
1715 default:
1716 ERR("unknown datablock %08x\n", dwSig);
1717 }
1718 *ppDataBlock = block;
1719 return hr;
1720 }
1721
1722 HRESULT WINAPI CShellLink::RemoveDataBlock(DWORD dwSig)
1723 {
1724 FIXME("\n");
1725 return E_NOTIMPL;
1726 }
1727
1728 HRESULT WINAPI CShellLink::GetFlags(DWORD *pdwFlags)
1729 {
1730 DWORD flags = 0;
1731
1732 FIXME("%p %p\n", this, pdwFlags);
1733
1734 /* FIXME: add more */
1735 if (sArgs)
1736 flags |= SLDF_HAS_ARGS;
1737 if (sComponent)
1738 flags |= SLDF_HAS_DARWINID;
1739 if (sIcoPath)
1740 flags |= SLDF_HAS_ICONLOCATION;
1741 #if (NTDDI_VERSION < NTDDI_LONGHORN)
1742 if (sProduct)
1743 flags |= SLDF_HAS_LOGO3ID;
1744 #endif
1745 if (pPidl)
1746 flags |= SLDF_HAS_ID_LIST;
1747
1748 *pdwFlags = flags;
1749
1750 return S_OK;
1751 }
1752
1753 HRESULT WINAPI CShellLink::SetFlags(DWORD dwFlags)
1754 {
1755 FIXME("\n");
1756 return E_NOTIMPL;
1757 }
1758
1759 /**************************************************************************
1760 * CShellLink implementation of IShellExtInit::Initialize()
1761 *
1762 * Loads the shelllink from the dataobject the shell is pointing to.
1763 */
1764 HRESULT WINAPI CShellLink::Initialize(LPCITEMIDLIST pidlFolder, IDataObject *pdtobj, HKEY hkeyProgID)
1765 {
1766 TRACE("%p %p %p %p\n", this, pidlFolder, pdtobj, hkeyProgID);
1767
1768 if (!pdtobj)
1769 return E_FAIL;
1770
1771 FORMATETC format;
1772 format.cfFormat = CF_HDROP;
1773 format.ptd = NULL;
1774 format.dwAspect = DVASPECT_CONTENT;
1775 format.lindex = -1;
1776 format.tymed = TYMED_HGLOBAL;
1777
1778 STGMEDIUM stgm;
1779 HRESULT hr = pdtobj->GetData(&format, &stgm);
1780 if (FAILED(hr))
1781 return hr;
1782
1783 UINT count = DragQueryFileW((HDROP)stgm.hGlobal, -1, NULL, 0);
1784 if (count == 1)
1785 {
1786 count = DragQueryFileW((HDROP)stgm.hGlobal, 0, NULL, 0);
1787 count++;
1788 LPWSTR path = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, count * sizeof(WCHAR));
1789 if (path)
1790 {
1791 count = DragQueryFileW((HDROP)stgm.hGlobal, 0, path, count);
1792 hr = Load(path, 0);
1793 HeapFree(GetProcessHeap(), 0, path);
1794 }
1795 }
1796 ReleaseStgMedium(&stgm);
1797
1798 return S_OK;
1799 }
1800
1801 HRESULT WINAPI CShellLink::QueryContextMenu(HMENU hMenu, UINT indexMenu, UINT idCmdFirst, UINT idCmdLast, UINT uFlags)
1802 {
1803 int id = 1;
1804
1805 TRACE("%p %p %u %u %u %u\n", this,
1806 hMenu, indexMenu, idCmdFirst, idCmdLast, uFlags);
1807
1808 if (!hMenu)
1809 return E_INVALIDARG;
1810
1811 WCHAR wszOpen[20];
1812 if (!LoadStringW(shell32_hInstance, IDS_OPEN_VERB, wszOpen, _countof(wszOpen)))
1813 wszOpen[0] = L'\0';
1814
1815 MENUITEMINFOW mii;
1816 memset(&mii, 0, sizeof(mii));
1817 mii.cbSize = sizeof (mii);
1818 mii.fMask = MIIM_TYPE | MIIM_ID | MIIM_STATE;
1819 mii.dwTypeData = wszOpen;
1820 mii.cch = wcslen(mii.dwTypeData);
1821 mii.wID = idCmdFirst + id++;
1822 mii.fState = MFS_DEFAULT | MFS_ENABLED;
1823 mii.fType = MFT_STRING;
1824 if (!InsertMenuItemW(hMenu, indexMenu, TRUE, &mii))
1825 return E_FAIL;
1826 iIdOpen = 1;
1827
1828 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, id);
1829 }
1830
1831 static LPWSTR
1832 shelllink_get_msi_component_path(LPWSTR component)
1833 {
1834 DWORD Result, sz = 0;
1835
1836 Result = CommandLineFromMsiDescriptor(component, NULL, &sz);
1837 if (Result != ERROR_SUCCESS)
1838 return NULL;
1839
1840 sz++;
1841 LPWSTR path = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, sz * sizeof(WCHAR));
1842 Result = CommandLineFromMsiDescriptor(component, path, &sz);
1843 if (Result != ERROR_SUCCESS)
1844 {
1845 HeapFree(GetProcessHeap(), 0, path);
1846 path = NULL;
1847 }
1848
1849 TRACE("returning %s\n", debugstr_w(path));
1850
1851 return path;
1852 }
1853
1854 HRESULT WINAPI CShellLink::InvokeCommand(LPCMINVOKECOMMANDINFO lpici)
1855 {
1856 HWND hwnd = NULL; /* FIXME: get using interface set from IObjectWithSite */
1857 LPWSTR args = NULL;
1858 LPWSTR path = NULL;
1859
1860 TRACE("%p %p\n", this, lpici);
1861
1862 if (lpici->cbSize < sizeof (CMINVOKECOMMANDINFO))
1863 return E_INVALIDARG;
1864
1865 HRESULT hr = Resolve(hwnd, 0);
1866 if (FAILED(hr))
1867 {
1868 TRACE("failed to resolve component with error 0x%08x", hr);
1869 return hr;
1870 }
1871 if (sComponent)
1872 {
1873 path = shelllink_get_msi_component_path(sComponent);
1874 if (!path)
1875 return E_FAIL;
1876 }
1877 else
1878 path = strdupW(sPath);
1879
1880 if ( lpici->cbSize == sizeof(CMINVOKECOMMANDINFOEX) &&
1881 (lpici->fMask & CMIC_MASK_UNICODE) )
1882 {
1883 LPCMINVOKECOMMANDINFOEX iciex = (LPCMINVOKECOMMANDINFOEX)lpici;
1884 DWORD len = 2;
1885
1886 if (sArgs)
1887 len += wcslen(sArgs);
1888 if (iciex->lpParametersW)
1889 len += wcslen(iciex->lpParametersW);
1890
1891 args = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1892 args[0] = 0;
1893 if (sArgs)
1894 wcscat(args, sArgs);
1895 if (iciex->lpParametersW)
1896 {
1897 wcscat(args, L" ");
1898 wcscat(args, iciex->lpParametersW);
1899 }
1900 }
1901 else if (sArgs != NULL)
1902 {
1903 args = strdupW(sArgs);
1904 }
1905
1906 SHELLEXECUTEINFOW sei;
1907 memset(&sei, 0, sizeof sei);
1908 sei.cbSize = sizeof sei;
1909 sei.fMask = SEE_MASK_HASLINKNAME | SEE_MASK_UNICODE |
1910 (lpici->fMask & (SEE_MASK_NOASYNC | SEE_MASK_ASYNCOK | SEE_MASK_FLAG_NO_UI));
1911 sei.lpFile = path;
1912 sei.lpClass = sLinkPath;
1913 sei.nShow = iShowCmd;
1914 sei.lpDirectory = sWorkDir;
1915 sei.lpParameters = args;
1916 sei.lpVerb = L"open";
1917
1918 // HACK for ShellExecuteExW
1919 if (wcsstr(sPath, L".cpl"))
1920 sei.lpVerb = L"cplopen";
1921
1922 if (ShellExecuteExW(&sei))
1923 hr = S_OK;
1924 else
1925 hr = E_FAIL;
1926
1927 HeapFree(GetProcessHeap(), 0, args);
1928 HeapFree(GetProcessHeap(), 0, path);
1929
1930 return hr;
1931 }
1932
1933 HRESULT WINAPI CShellLink::GetCommandString(UINT_PTR idCmd, UINT uType, UINT* pwReserved, LPSTR pszName, UINT cchMax)
1934 {
1935 FIXME("%p %lu %u %p %p %u\n", this, idCmd, uType, pwReserved, pszName, cchMax);
1936
1937 return E_NOTIMPL;
1938 }
1939
1940 INT_PTR CALLBACK ExtendedShortcutProc(HWND hwndDlg, UINT uMsg,
1941 WPARAM wParam, LPARAM lParam)
1942 {
1943 switch(uMsg)
1944 {
1945 case WM_INITDIALOG:
1946 if (lParam)
1947 {
1948 HWND hDlgCtrl = GetDlgItem(hwndDlg, 14000);
1949 SendMessage(hDlgCtrl, BM_SETCHECK, BST_CHECKED, 0);
1950 }
1951 return TRUE;
1952 case WM_COMMAND:
1953 {
1954 HWND hDlgCtrl = GetDlgItem(hwndDlg, 14000);
1955 if (LOWORD(wParam) == IDOK)
1956 {
1957 if (SendMessage(hDlgCtrl, BM_GETCHECK, 0, 0) == BST_CHECKED)
1958 EndDialog(hwndDlg, 1);
1959 else
1960 EndDialog(hwndDlg, 0);
1961 }
1962 else if (LOWORD(wParam) == IDCANCEL)
1963 {
1964 EndDialog(hwndDlg, -1);
1965 }
1966 else if (LOWORD(wParam) == 14000)
1967 {
1968 if (SendMessage(hDlgCtrl, BM_GETCHECK, 0, 0) == BST_CHECKED)
1969 SendMessage(hDlgCtrl, BM_SETCHECK, BST_UNCHECKED, 0);
1970 else
1971 SendMessage(hDlgCtrl, BM_SETCHECK, BST_CHECKED, 0);
1972 }
1973 }
1974 }
1975 return FALSE;
1976 }
1977
1978 EXTERN_C HRESULT
1979 WINAPI
1980 SHOpenFolderAndSelectItems(LPITEMIDLIST pidlFolder,
1981 UINT cidl,
1982 PCUITEMID_CHILD_ARRAY apidl,
1983 DWORD dwFlags);
1984
1985 /**************************************************************************
1986 * SH_ShellLinkDlgProc
1987 *
1988 * dialog proc of the shortcut property dialog
1989 */
1990
1991 INT_PTR CALLBACK CShellLink::SH_ShellLinkDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
1992 {
1993 CShellLink *pThis = reinterpret_cast<CShellLink *>(GetWindowLongPtr(hwndDlg, DWLP_USER));
1994
1995 switch(uMsg)
1996 {
1997 case WM_INITDIALOG:
1998 {
1999 LPPROPSHEETPAGEW ppsp = (LPPROPSHEETPAGEW)lParam;
2000 if (ppsp == NULL)
2001 break;
2002
2003 TRACE("ShellLink_DlgProc (WM_INITDIALOG hwnd %p lParam %p ppsplParam %x)\n", hwndDlg, lParam, ppsp->lParam);
2004
2005 pThis = reinterpret_cast<CShellLink *>(ppsp->lParam);
2006 SetWindowLongPtr(hwndDlg, DWLP_USER, (LONG_PTR)pThis);
2007
2008 TRACE("sArgs: %S sComponent: %S sDescription: %S sIcoPath: %S sPath: %S sPathRel: %S sProduct: %S sWorkDir: %S\n", pThis->sArgs, pThis->sComponent, pThis->sDescription,
2009 pThis->sIcoPath, pThis->sPath, pThis->sPathRel, pThis->sProduct, pThis->sWorkDir);
2010
2011 /* Get file information */
2012 SHFILEINFO fi;
2013 if (!SHGetFileInfoW(pThis->sLinkPath, 0, &fi, sizeof(fi), SHGFI_TYPENAME|SHGFI_ICON))
2014 {
2015 ERR("SHGetFileInfoW failed for %ls (%lu)\n", pThis->sLinkPath, GetLastError());
2016 fi.szTypeName[0] = L'\0';
2017 fi.hIcon = NULL;
2018 }
2019
2020 if (fi.hIcon) // TODO: destroy icon
2021 SendDlgItemMessageW(hwndDlg, 14000, STM_SETICON, (WPARAM)fi.hIcon, 0);
2022 else
2023 ERR("ExtractIconW failed %ls %u\n", pThis->sIcoPath, pThis->iIcoNdx);
2024
2025 /* target type */
2026 if (pThis->sPath)
2027 SetDlgItemTextW(hwndDlg, 14005, SH_GetTargetTypeByPath(pThis->sPath));
2028
2029 /* target location */
2030 if (pThis->sWorkDir)
2031 SetDlgItemTextW(hwndDlg, 14007, PathFindFileName(pThis->sWorkDir));
2032
2033 /* target path */
2034 if (pThis->sPath)
2035 {
2036 WCHAR newpath[2*MAX_PATH] = L"\0";
2037 if (wcschr(pThis->sPath, ' '))
2038 StringCchPrintfExW(newpath, 2*MAX_PATH, NULL, NULL, 0, L"\"%ls\"", pThis->sPath);
2039 else
2040 StringCchCopyExW(newpath, 2*MAX_PATH, pThis->sPath, NULL, NULL, 0);
2041
2042 if (pThis->sArgs && pThis->sArgs[0])
2043 {
2044 StringCchCatW(newpath, 2*MAX_PATH, L" ");
2045 StringCchCatW(newpath, 2*MAX_PATH, pThis->sArgs);
2046 }
2047 SetDlgItemTextW(hwndDlg, 14009, newpath);
2048 }
2049 /* working dir */
2050 if (pThis->sWorkDir)
2051 SetDlgItemTextW(hwndDlg, 14011, pThis->sWorkDir);
2052
2053 /* description */
2054 if (pThis->sDescription)
2055 SetDlgItemTextW(hwndDlg, 14019, pThis->sDescription);
2056
2057 return TRUE;
2058 }
2059 case WM_NOTIFY:
2060 {
2061 LPPSHNOTIFY lppsn = (LPPSHNOTIFY)lParam;
2062 if (lppsn->hdr.code == PSN_APPLY)
2063 {
2064 WCHAR wszBuf[MAX_PATH];
2065 /* set working directory */
2066 GetDlgItemTextW(hwndDlg, 14011, wszBuf, MAX_PATH);
2067 pThis->SetWorkingDirectory(wszBuf);
2068 /* set link destination */
2069 GetDlgItemTextW(hwndDlg, 14009, wszBuf, MAX_PATH);
2070 LPWSTR lpszArgs = NULL;
2071 LPWSTR unquoted = strdupW(wszBuf);
2072 StrTrimW(unquoted, L" ");
2073 if (!PathFileExistsW(unquoted))
2074 {
2075 lpszArgs = PathGetArgsW(unquoted);
2076 PathRemoveArgsW(unquoted);
2077 StrTrimW(lpszArgs, L" ");
2078 }
2079 if (unquoted[0] == '"' && unquoted[wcslen(unquoted)-1] == '"')
2080 PathUnquoteSpacesW(unquoted);
2081
2082
2083 WCHAR *pwszExt = PathFindExtensionW(unquoted);
2084 if (!wcsicmp(pwszExt, L".lnk"))
2085 {
2086 // FIXME load localized error msg
2087 MessageBoxW(hwndDlg, L"You cannot create a link to a shortcut", L"Error", MB_ICONERROR);
2088 SetWindowLongPtr(hwndDlg, DWL_MSGRESULT, PSNRET_INVALID_NOCHANGEPAGE);
2089 return TRUE;
2090 }
2091
2092 if (!PathFileExistsW(unquoted))
2093 {
2094 //FIXME load localized error msg
2095 MessageBoxW(hwndDlg, L"The specified file name in the target box is invalid", L"Error", MB_ICONERROR);
2096 SetWindowLongPtr(hwndDlg, DWL_MSGRESULT, PSNRET_INVALID_NOCHANGEPAGE);
2097 return TRUE;
2098 }
2099
2100 pThis->SetPath(unquoted);
2101 if (lpszArgs)
2102 pThis->SetArguments(lpszArgs);
2103 else
2104 pThis->SetArguments(L"\0");
2105
2106 HeapFree(GetProcessHeap(), 0, unquoted);
2107
2108 TRACE("This %p sLinkPath %S\n", pThis, pThis->sLinkPath);
2109 pThis->Save(pThis->sLinkPath, TRUE);
2110 SetWindowLongPtr(hwndDlg, DWL_MSGRESULT, PSNRET_NOERROR); return TRUE;
2111 }
2112 break;
2113 }
2114 case WM_COMMAND:
2115 switch(LOWORD(wParam))
2116 {
2117 case 14020:
2118 SHOpenFolderAndSelectItems(pThis->pPidl, 0, NULL, 0);
2119 ///
2120 /// FIXME
2121 /// open target directory
2122 ///
2123 return TRUE;
2124 case 14021:
2125 {
2126 WCHAR wszPath[MAX_PATH] = L"";
2127
2128 if (pThis->sIcoPath)
2129 wcscpy(wszPath, pThis->sIcoPath);
2130 INT IconIndex = pThis->iIcoNdx;
2131 if (PickIconDlg(hwndDlg, wszPath, MAX_PATH, &IconIndex))
2132 {
2133 pThis->SetIconLocation(wszPath, IconIndex);
2134 ///
2135 /// FIXME redraw icon
2136 }
2137 return TRUE;
2138 }
2139
2140 case 14022:
2141 {
2142 INT_PTR result = DialogBoxParamW(shell32_hInstance, MAKEINTRESOURCEW(IDD_SHORTCUT_EXTENDED_PROPERTIES), hwndDlg, ExtendedShortcutProc, (LPARAM)pThis->bRunAs);
2143 if (result == 1 || result == 0)
2144 {
2145 if (pThis->bRunAs != result)
2146 {
2147 PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
2148 }
2149
2150 pThis->bRunAs = result;
2151 }
2152 return TRUE;
2153 }
2154 }
2155 if(HIWORD(wParam) == EN_CHANGE)
2156 PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
2157 break;
2158 default:
2159 break;
2160 }
2161 return FALSE;
2162 }
2163
2164 /**************************************************************************
2165 * ShellLink_IShellPropSheetExt interface
2166 */
2167
2168 HRESULT WINAPI CShellLink::AddPages(LPFNADDPROPSHEETPAGE pfnAddPage, LPARAM lParam)
2169 {
2170 HPROPSHEETPAGE hPage = SH_CreatePropertySheetPage(IDD_SHORTCUT_PROPERTIES, SH_ShellLinkDlgProc, (LPARAM)this, NULL);
2171 if (hPage == NULL)
2172 {
2173 ERR("failed to create property sheet page\n");
2174 return E_FAIL;
2175 }
2176
2177 if (!pfnAddPage(hPage, lParam))
2178 return E_FAIL;
2179
2180 return S_OK;
2181 }
2182
2183 HRESULT WINAPI CShellLink::ReplacePage(UINT uPageID, LPFNADDPROPSHEETPAGE pfnReplacePage, LPARAM lParam)
2184 {
2185 TRACE("(%p) (uPageID %u, pfnReplacePage %p lParam %p\n", this, uPageID, pfnReplacePage, lParam);
2186 return E_NOTIMPL;
2187 }
2188
2189 HRESULT WINAPI CShellLink::SetSite(IUnknown *punk)
2190 {
2191 TRACE("%p %p\n", this, punk);
2192
2193 site = punk;
2194
2195 return S_OK;
2196 }
2197
2198 HRESULT WINAPI CShellLink::GetSite(REFIID iid, void ** ppvSite)
2199 {
2200 TRACE("%p %s %p\n", this, debugstr_guid(&iid), ppvSite);
2201
2202 if (site == NULL)
2203 return E_FAIL;
2204
2205 return site->QueryInterface(iid, ppvSite);
2206 }
2207
2208 HRESULT WINAPI CShellLink::DragEnter(IDataObject *pDataObject,
2209 DWORD dwKeyState, POINTL pt, DWORD *pdwEffect)
2210 {
2211 TRACE("(%p)->(DataObject=%p)\n", this, pDataObject);
2212 LPCITEMIDLIST pidlLast;
2213 CComPtr<IShellFolder> psf;
2214
2215 HRESULT hr = SHBindToParent(pPidl, IID_PPV_ARG(IShellFolder, &psf), &pidlLast);
2216
2217 if (SUCCEEDED(hr))
2218 {
2219 hr = psf->GetUIObjectOf(0, 1, &pidlLast, IID_NULL_PPV_ARG(IDropTarget, &mDropTarget));
2220
2221 if (SUCCEEDED(hr))
2222 hr = mDropTarget->DragEnter(pDataObject, dwKeyState, pt, pdwEffect);
2223 else
2224 *pdwEffect = DROPEFFECT_NONE;
2225 }
2226 else
2227 *pdwEffect = DROPEFFECT_NONE;
2228
2229 return S_OK;
2230 }
2231
2232
2233
2234 HRESULT WINAPI CShellLink::DragOver(DWORD dwKeyState, POINTL pt,
2235 DWORD *pdwEffect)
2236 {
2237 TRACE("(%p)\n", this);
2238 HRESULT hr = S_OK;
2239 if (mDropTarget)
2240 hr = mDropTarget->DragOver(dwKeyState, pt, pdwEffect);
2241 return hr;
2242 }
2243
2244 HRESULT WINAPI CShellLink::DragLeave()
2245 {
2246 TRACE("(%p)\n", this);
2247 HRESULT hr = S_OK;
2248 if (mDropTarget)
2249 {
2250 hr = mDropTarget->DragLeave();
2251 mDropTarget->Release();
2252 }
2253
2254 return hr;
2255 }
2256
2257 HRESULT WINAPI CShellLink::Drop(IDataObject *pDataObject,
2258 DWORD dwKeyState, POINTL pt, DWORD *pdwEffect)
2259 {
2260 TRACE("(%p)\n", this);
2261 HRESULT hr = S_OK;
2262 if (mDropTarget)
2263 hr = mDropTarget->Drop(pDataObject, dwKeyState, pt, pdwEffect);
2264
2265 return hr;
2266 }
2267
2268 /**************************************************************************
2269 * IShellLink_ConstructFromFile
2270 */
2271 HRESULT WINAPI IShellLink_ConstructFromPath(WCHAR *path, REFIID riid, LPVOID *ppv)
2272 {
2273 CComPtr<IPersistFile> ppf;
2274 HRESULT hr = CShellLink::_CreatorClass::CreateInstance(NULL, IID_PPV_ARG(IPersistFile, &ppf));
2275 if (FAILED(hr))
2276 return hr;
2277
2278 hr = ppf->Load(path, 0);
2279 if (FAILED(hr))
2280 return hr;
2281
2282 return ppf->QueryInterface(riid, ppv);
2283 }
2284
2285 HRESULT WINAPI IShellLink_ConstructFromFile(IShellFolder * psf, LPCITEMIDLIST pidl, REFIID riid, LPVOID *ppv)
2286 {
2287 WCHAR path[MAX_PATH];
2288 if (!ILGetDisplayNameExW(psf, pidl, path, 0))
2289 return E_FAIL;
2290
2291 return IShellLink_ConstructFromPath(path, riid, ppv);
2292 }