8b5bb30182c524d4e1aa09c87818c88245f39985
[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 bDirty = TRUE;
1347
1348 return S_OK;
1349 }
1350
1351 HRESULT WINAPI CShellLink::GetWorkingDirectory(LPWSTR pszDir, INT cchMaxPath)
1352 {
1353 TRACE("(%p)->(%p len %u)\n", this, pszDir, cchMaxPath);
1354
1355 if (cchMaxPath)
1356 pszDir[0] = 0;
1357 if (sWorkDir)
1358 lstrcpynW(pszDir, sWorkDir, cchMaxPath);
1359
1360 return S_OK;
1361 }
1362
1363 HRESULT WINAPI CShellLink::SetWorkingDirectory(LPCWSTR pszDir)
1364 {
1365 TRACE("(%p)->(dir=%s)\n", this, debugstr_w(pszDir));
1366
1367 HeapFree(GetProcessHeap(), 0, sWorkDir);
1368 sWorkDir = (LPWSTR)HeapAlloc(GetProcessHeap(), 0,
1369 (wcslen(pszDir) + 1) * sizeof (WCHAR));
1370 if (!sWorkDir)
1371 return E_OUTOFMEMORY;
1372 wcscpy(sWorkDir, pszDir);
1373 bDirty = TRUE;
1374
1375 return S_OK;
1376 }
1377
1378 HRESULT WINAPI CShellLink::GetArguments(LPWSTR pszArgs, INT cchMaxPath)
1379 {
1380 TRACE("(%p)->(%p len=%u)\n", this, pszArgs, cchMaxPath);
1381
1382 if (cchMaxPath)
1383 pszArgs[0] = 0;
1384 if (sArgs)
1385 lstrcpynW(pszArgs, sArgs, cchMaxPath);
1386
1387 return S_OK;
1388 }
1389
1390 HRESULT WINAPI CShellLink::SetArguments(LPCWSTR pszArgs)
1391 {
1392 TRACE("(%p)->(args=%s)\n", this, debugstr_w(pszArgs));
1393
1394 HeapFree(GetProcessHeap(), 0, sArgs);
1395 sArgs = (LPWSTR)HeapAlloc(GetProcessHeap(), 0,
1396 (wcslen(pszArgs) + 1) * sizeof (WCHAR));
1397 if (!sArgs)
1398 return E_OUTOFMEMORY;
1399
1400 wcscpy(sArgs, pszArgs);
1401 bDirty = TRUE;
1402
1403 return S_OK;
1404 }
1405
1406 static HRESULT SHELL_PidlGeticonLocationW(IShellFolder* psf, LPCITEMIDLIST pidl,
1407 LPWSTR pszIconPath, int cchIconPath, int* piIcon)
1408 {
1409 LPCITEMIDLIST pidlLast;
1410 UINT wFlags;
1411
1412 HRESULT hr = SHBindToParent(pidl, IID_PPV_ARG(IShellFolder, &psf), &pidlLast);
1413
1414 if (SUCCEEDED(hr))
1415 {
1416 CComPtr<IExtractIconW> pei;
1417
1418 hr = psf->GetUIObjectOf(0, 1, &pidlLast, IID_NULL_PPV_ARG(IExtractIconW, &pei));
1419
1420 if (SUCCEEDED(hr))
1421 hr = pei->GetIconLocation(0, pszIconPath, MAX_PATH, piIcon, &wFlags);
1422
1423 psf->Release();
1424 }
1425
1426 return hr;
1427 }
1428
1429 HRESULT WINAPI CShellLink::GetIconLocation(LPWSTR pszIconPath, INT cchIconPath, INT *piIcon)
1430 {
1431 TRACE("(%p)->(%p len=%u iicon=%p)\n", this, pszIconPath, cchIconPath, piIcon);
1432
1433 pszIconPath[0] = 0;
1434 *piIcon = iIcoNdx;
1435
1436 if (sIcoPath)
1437 {
1438 lstrcpynW(pszIconPath, sIcoPath, cchIconPath);
1439 return S_OK;
1440 }
1441
1442 if (pPidl || sPath)
1443 {
1444 CComPtr<IShellFolder> pdsk;
1445
1446 HRESULT hr = SHGetDesktopFolder(&pdsk);
1447
1448 if (SUCCEEDED(hr))
1449 {
1450 /* first look for an icon using the PIDL (if present) */
1451 if (pPidl)
1452 hr = SHELL_PidlGeticonLocationW(pdsk, pPidl, pszIconPath, cchIconPath, piIcon);
1453 else
1454 hr = E_FAIL;
1455
1456 /* if we couldn't find an icon yet, look for it using the file system path */
1457 if (FAILED(hr) && sPath)
1458 {
1459 LPITEMIDLIST pidl;
1460
1461 hr = pdsk->ParseDisplayName(0, NULL, sPath, NULL, &pidl, NULL);
1462
1463 if (SUCCEEDED(hr))
1464 {
1465 hr = SHELL_PidlGeticonLocationW(pdsk, pidl, pszIconPath, cchIconPath, piIcon);
1466
1467 SHFree(pidl);
1468 }
1469 }
1470 }
1471 return hr;
1472 }
1473 return S_OK;
1474 }
1475
1476 HRESULT WINAPI CShellLink::SetIconLocation(LPCWSTR pszIconPath, INT iIcon)
1477 {
1478 TRACE("(%p)->(path=%s iicon=%u)\n", this, debugstr_w(pszIconPath), iIcon);
1479
1480 HeapFree(GetProcessHeap(), 0, sIcoPath);
1481 sIcoPath = (LPWSTR)HeapAlloc(GetProcessHeap(), 0,
1482 (wcslen(pszIconPath) + 1) * sizeof (WCHAR));
1483 if (!sIcoPath)
1484 return E_OUTOFMEMORY;
1485 wcscpy(sIcoPath, pszIconPath);
1486
1487 iIcoNdx = iIcon;
1488 bDirty = TRUE;
1489
1490 return S_OK;
1491 }
1492
1493 HRESULT WINAPI CShellLink::SetRelativePath(LPCWSTR pszPathRel, DWORD dwReserved)
1494 {
1495 TRACE("(%p)->(path=%s %x)\n", this, debugstr_w(pszPathRel), dwReserved);
1496
1497 HeapFree(GetProcessHeap(), 0, sPathRel);
1498 sPathRel = (LPWSTR)HeapAlloc(GetProcessHeap(), 0,
1499 (wcslen(pszPathRel) + 1) * sizeof (WCHAR));
1500 if (!sPathRel)
1501 return E_OUTOFMEMORY;
1502 wcscpy(sPathRel, pszPathRel);
1503 bDirty = TRUE;
1504
1505 return ShellLink_UpdatePath(sPathRel, sPath, sWorkDir, &sPath);
1506 }
1507
1508 LPWSTR CShellLink::ShellLink_GetAdvertisedArg(LPCWSTR str)
1509 {
1510 if (!str)
1511 return NULL;
1512
1513 LPCWSTR p = wcschr(str, L':');
1514 if (!p)
1515 return NULL;
1516 DWORD len = p - str;
1517 LPWSTR ret = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * (len + 1));
1518 if (!ret)
1519 return ret;
1520 memcpy(ret, str, sizeof(WCHAR)*len);
1521 ret[len] = 0;
1522 return ret;
1523 }
1524
1525 HRESULT CShellLink::ShellLink_SetAdvertiseInfo(LPCWSTR str)
1526 {
1527 LPCWSTR szComponent = NULL, szProduct = NULL;
1528
1529 while (str[0])
1530 {
1531 /* each segment must start with two colons */
1532 if (str[0] != ':' || str[1] != ':')
1533 return E_FAIL;
1534
1535 /* the last segment is just two colons */
1536 if (!str[2])
1537 break;
1538 str += 2;
1539
1540 /* there must be a colon straight after a guid */
1541 LPCWSTR p = wcschr(str, L':');
1542 if (!p)
1543 return E_FAIL;
1544 INT len = p - str;
1545 if (len != 38)
1546 return E_FAIL;
1547
1548 /* get the guid, and check it's validly formatted */
1549 WCHAR szGuid[39];
1550 memcpy(szGuid, str, sizeof(WCHAR)*len);
1551 szGuid[len] = 0;
1552
1553 GUID guid;
1554 HRESULT hr = CLSIDFromString(szGuid, &guid);
1555 if (hr != S_OK)
1556 return hr;
1557 str = p + 1;
1558
1559 /* match it up to a guid that we care about */
1560 if (IsEqualGUID(guid, SHELL32_AdvtShortcutComponent) && !szComponent)
1561 szComponent = str;
1562 else if (IsEqualGUID(guid, SHELL32_AdvtShortcutProduct) && !szProduct)
1563 szProduct = str;
1564 else
1565 return E_FAIL;
1566
1567 /* skip to the next field */
1568 str = wcschr(str, L':');
1569 if (!str)
1570 return E_FAIL;
1571 }
1572
1573 /* we have to have a component for an advertised shortcut */
1574 if (!szComponent)
1575 return E_FAIL;
1576
1577 sComponent = ShellLink_GetAdvertisedArg(szComponent);
1578 sProduct = ShellLink_GetAdvertisedArg(szProduct);
1579
1580 TRACE("Component = %s\n", debugstr_w(sComponent));
1581 TRACE("Product = %s\n", debugstr_w(sProduct));
1582
1583 return S_OK;
1584 }
1585
1586 static BOOL ShellLink_GetVolumeInfo(LPCWSTR path, CShellLink::volume_info *volume)
1587 {
1588 WCHAR drive[4] = { path[0], ':', '\\', 0 };
1589
1590 volume->type = GetDriveTypeW(drive);
1591 BOOL bRet = GetVolumeInformationW(drive, volume->label, _countof(volume->label), &volume->serial, NULL, NULL, NULL, 0);
1592 TRACE("ret = %d type %d serial %08x name %s\n", bRet,
1593 volume->type, volume->serial, debugstr_w(volume->label));
1594 return bRet;
1595 }
1596
1597 HRESULT WINAPI CShellLink::SetPath(LPCWSTR pszFile)
1598 {
1599 LPWSTR unquoted = NULL;
1600 HRESULT hr = S_OK;
1601
1602 TRACE("(%p)->(path=%s)\n", this, debugstr_w(pszFile));
1603
1604 if (!pszFile)
1605 return E_INVALIDARG;
1606
1607 /* quotes at the ends of the string are stripped */
1608 UINT len = wcslen(pszFile);
1609 if (pszFile[0] == '"' && pszFile[len-1] == '"')
1610 {
1611 unquoted = strdupW(pszFile);
1612 PathUnquoteSpacesW(unquoted);
1613 pszFile = unquoted;
1614 }
1615
1616 /* any other quote marks are invalid */
1617 if (wcschr(pszFile, '"'))
1618 {
1619 HeapFree(GetProcessHeap(), 0, unquoted);
1620 return S_FALSE;
1621 }
1622
1623 HeapFree(GetProcessHeap(), 0, sPath);
1624 sPath = NULL;
1625
1626 HeapFree(GetProcessHeap(), 0, sComponent);
1627 sComponent = NULL;
1628
1629 if (pPidl)
1630 ILFree(pPidl);
1631 pPidl = NULL;
1632
1633 if (S_OK != ShellLink_SetAdvertiseInfo(pszFile))
1634 {
1635 WCHAR buffer[MAX_PATH];
1636 LPWSTR fname;
1637
1638 if (*pszFile == '\0')
1639 *buffer = '\0';
1640 else if (!GetFullPathNameW(pszFile, MAX_PATH, buffer, &fname))
1641 return E_FAIL;
1642 else if(!PathFileExistsW(buffer) &&
1643 !SearchPathW(NULL, pszFile, NULL, MAX_PATH, buffer, NULL))
1644 hr = S_FALSE;
1645
1646 pPidl = SHSimpleIDListFromPathW(pszFile);
1647 ShellLink_GetVolumeInfo(buffer, &volume);
1648
1649 sPath = (LPWSTR)HeapAlloc(GetProcessHeap(), 0,
1650 (wcslen(buffer) + 1) * sizeof (WCHAR));
1651 if (!sPath)
1652 return E_OUTOFMEMORY;
1653
1654 wcscpy(sPath, buffer);
1655 }
1656
1657 bDirty = TRUE;
1658 HeapFree(GetProcessHeap(), 0, unquoted);
1659
1660 return hr;
1661 }
1662
1663 HRESULT WINAPI CShellLink::AddDataBlock(void* pDataBlock)
1664 {
1665 FIXME("\n");
1666 return E_NOTIMPL;
1667 }
1668
1669 HRESULT WINAPI CShellLink::CopyDataBlock(DWORD dwSig, void** ppDataBlock)
1670 {
1671 LPVOID block = NULL;
1672 HRESULT hr = E_FAIL;
1673
1674 TRACE("%p %08x %p\n", this, dwSig, ppDataBlock);
1675
1676 switch (dwSig)
1677 {
1678 case EXP_DARWIN_ID_SIG:
1679 if (!sComponent)
1680 break;
1681 block = shelllink_build_darwinid(sComponent, dwSig);
1682 hr = S_OK;
1683 break;
1684 case EXP_SZ_LINK_SIG:
1685 case NT_CONSOLE_PROPS_SIG:
1686 case NT_FE_CONSOLE_PROPS_SIG:
1687 case EXP_SPECIAL_FOLDER_SIG:
1688 case EXP_SZ_ICON_SIG:
1689 FIXME("valid but unhandled datablock %08x\n", dwSig);
1690 break;
1691 default:
1692 ERR("unknown datablock %08x\n", dwSig);
1693 }
1694 *ppDataBlock = block;
1695 return hr;
1696 }
1697
1698 HRESULT WINAPI CShellLink::RemoveDataBlock(DWORD dwSig)
1699 {
1700 FIXME("\n");
1701 return E_NOTIMPL;
1702 }
1703
1704 HRESULT WINAPI CShellLink::GetFlags(DWORD *pdwFlags)
1705 {
1706 DWORD flags = 0;
1707
1708 FIXME("%p %p\n", this, pdwFlags);
1709
1710 /* FIXME: add more */
1711 if (sArgs)
1712 flags |= SLDF_HAS_ARGS;
1713 if (sComponent)
1714 flags |= SLDF_HAS_DARWINID;
1715 if (sIcoPath)
1716 flags |= SLDF_HAS_ICONLOCATION;
1717 #if (NTDDI_VERSION < NTDDI_LONGHORN)
1718 if (sProduct)
1719 flags |= SLDF_HAS_LOGO3ID;
1720 #endif
1721 if (pPidl)
1722 flags |= SLDF_HAS_ID_LIST;
1723
1724 *pdwFlags = flags;
1725
1726 return S_OK;
1727 }
1728
1729 HRESULT WINAPI CShellLink::SetFlags(DWORD dwFlags)
1730 {
1731 FIXME("\n");
1732 return E_NOTIMPL;
1733 }
1734
1735 /**************************************************************************
1736 * CShellLink implementation of IShellExtInit::Initialize()
1737 *
1738 * Loads the shelllink from the dataobject the shell is pointing to.
1739 */
1740 HRESULT WINAPI CShellLink::Initialize(LPCITEMIDLIST pidlFolder, IDataObject *pdtobj, HKEY hkeyProgID)
1741 {
1742 TRACE("%p %p %p %p\n", this, pidlFolder, pdtobj, hkeyProgID);
1743
1744 if (!pdtobj)
1745 return E_FAIL;
1746
1747 FORMATETC format;
1748 format.cfFormat = CF_HDROP;
1749 format.ptd = NULL;
1750 format.dwAspect = DVASPECT_CONTENT;
1751 format.lindex = -1;
1752 format.tymed = TYMED_HGLOBAL;
1753
1754 STGMEDIUM stgm;
1755 HRESULT hr = pdtobj->GetData(&format, &stgm);
1756 if (FAILED(hr))
1757 return hr;
1758
1759 UINT count = DragQueryFileW((HDROP)stgm.hGlobal, -1, NULL, 0);
1760 if (count == 1)
1761 {
1762 count = DragQueryFileW((HDROP)stgm.hGlobal, 0, NULL, 0);
1763 count++;
1764 LPWSTR path = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, count * sizeof(WCHAR));
1765 if (path)
1766 {
1767 count = DragQueryFileW((HDROP)stgm.hGlobal, 0, path, count);
1768 hr = Load(path, 0);
1769 HeapFree(GetProcessHeap(), 0, path);
1770 }
1771 }
1772 ReleaseStgMedium(&stgm);
1773
1774 return S_OK;
1775 }
1776
1777 HRESULT WINAPI CShellLink::QueryContextMenu(HMENU hMenu, UINT indexMenu, UINT idCmdFirst, UINT idCmdLast, UINT uFlags)
1778 {
1779 int id = 1;
1780
1781 TRACE("%p %p %u %u %u %u\n", this,
1782 hMenu, indexMenu, idCmdFirst, idCmdLast, uFlags);
1783
1784 if (!hMenu)
1785 return E_INVALIDARG;
1786
1787 WCHAR wszOpen[20];
1788 if (!LoadStringW(shell32_hInstance, IDS_OPEN_VERB, wszOpen, _countof(wszOpen)))
1789 wszOpen[0] = L'\0';
1790
1791 MENUITEMINFOW mii;
1792 memset(&mii, 0, sizeof(mii));
1793 mii.cbSize = sizeof (mii);
1794 mii.fMask = MIIM_TYPE | MIIM_ID | MIIM_STATE;
1795 mii.dwTypeData = wszOpen;
1796 mii.cch = wcslen(mii.dwTypeData);
1797 mii.wID = idCmdFirst + id++;
1798 mii.fState = MFS_DEFAULT | MFS_ENABLED;
1799 mii.fType = MFT_STRING;
1800 if (!InsertMenuItemW(hMenu, indexMenu, TRUE, &mii))
1801 return E_FAIL;
1802 iIdOpen = 1;
1803
1804 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, id);
1805 }
1806
1807 static LPWSTR
1808 shelllink_get_msi_component_path(LPWSTR component)
1809 {
1810 DWORD Result, sz = 0;
1811
1812 Result = CommandLineFromMsiDescriptor(component, NULL, &sz);
1813 if (Result != ERROR_SUCCESS)
1814 return NULL;
1815
1816 sz++;
1817 LPWSTR path = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, sz * sizeof(WCHAR));
1818 Result = CommandLineFromMsiDescriptor(component, path, &sz);
1819 if (Result != ERROR_SUCCESS)
1820 {
1821 HeapFree(GetProcessHeap(), 0, path);
1822 path = NULL;
1823 }
1824
1825 TRACE("returning %s\n", debugstr_w(path));
1826
1827 return path;
1828 }
1829
1830 HRESULT WINAPI CShellLink::InvokeCommand(LPCMINVOKECOMMANDINFO lpici)
1831 {
1832 HWND hwnd = NULL; /* FIXME: get using interface set from IObjectWithSite */
1833 LPWSTR args = NULL;
1834 LPWSTR path = NULL;
1835
1836 TRACE("%p %p\n", this, lpici);
1837
1838 if (lpici->cbSize < sizeof (CMINVOKECOMMANDINFO))
1839 return E_INVALIDARG;
1840
1841 HRESULT hr = Resolve(hwnd, 0);
1842 if (FAILED(hr))
1843 {
1844 TRACE("failed to resolve component with error 0x%08x", hr);
1845 return hr;
1846 }
1847 if (sComponent)
1848 {
1849 path = shelllink_get_msi_component_path(sComponent);
1850 if (!path)
1851 return E_FAIL;
1852 }
1853 else
1854 path = strdupW(sPath);
1855
1856 if ( lpici->cbSize == sizeof(CMINVOKECOMMANDINFOEX) &&
1857 (lpici->fMask & CMIC_MASK_UNICODE) )
1858 {
1859 LPCMINVOKECOMMANDINFOEX iciex = (LPCMINVOKECOMMANDINFOEX)lpici;
1860 DWORD len = 2;
1861
1862 if (sArgs)
1863 len += wcslen(sArgs);
1864 if (iciex->lpParametersW)
1865 len += wcslen(iciex->lpParametersW);
1866
1867 args = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1868 args[0] = 0;
1869 if (sArgs)
1870 wcscat(args, sArgs);
1871 if (iciex->lpParametersW)
1872 {
1873 wcscat(args, L" ");
1874 wcscat(args, iciex->lpParametersW);
1875 }
1876 }
1877 else if (sArgs != NULL)
1878 {
1879 args = strdupW(sArgs);
1880 }
1881
1882 SHELLEXECUTEINFOW sei;
1883 memset(&sei, 0, sizeof sei);
1884 sei.cbSize = sizeof sei;
1885 sei.fMask = SEE_MASK_HASLINKNAME | SEE_MASK_UNICODE |
1886 (lpici->fMask & (SEE_MASK_NOASYNC | SEE_MASK_ASYNCOK | SEE_MASK_FLAG_NO_UI));
1887 sei.lpFile = path;
1888 sei.lpClass = sLinkPath;
1889 sei.nShow = iShowCmd;
1890 sei.lpDirectory = sWorkDir;
1891 sei.lpParameters = args;
1892 sei.lpVerb = L"open";
1893
1894 // HACK for ShellExecuteExW
1895 if (wcsstr(sPath, L".cpl"))
1896 sei.lpVerb = L"cplopen";
1897
1898 if (ShellExecuteExW(&sei))
1899 hr = S_OK;
1900 else
1901 hr = E_FAIL;
1902
1903 HeapFree(GetProcessHeap(), 0, args);
1904 HeapFree(GetProcessHeap(), 0, path);
1905
1906 return hr;
1907 }
1908
1909 HRESULT WINAPI CShellLink::GetCommandString(UINT_PTR idCmd, UINT uType, UINT* pwReserved, LPSTR pszName, UINT cchMax)
1910 {
1911 FIXME("%p %lu %u %p %p %u\n", this, idCmd, uType, pwReserved, pszName, cchMax);
1912
1913 return E_NOTIMPL;
1914 }
1915
1916 INT_PTR CALLBACK ExtendedShortcutProc(HWND hwndDlg, UINT uMsg,
1917 WPARAM wParam, LPARAM lParam)
1918 {
1919 switch(uMsg)
1920 {
1921 case WM_INITDIALOG:
1922 if (lParam)
1923 {
1924 HWND hDlgCtrl = GetDlgItem(hwndDlg, 14000);
1925 SendMessage(hDlgCtrl, BM_SETCHECK, BST_CHECKED, 0);
1926 }
1927 return TRUE;
1928 case WM_COMMAND:
1929 {
1930 HWND hDlgCtrl = GetDlgItem(hwndDlg, 14000);
1931 if (LOWORD(wParam) == IDOK)
1932 {
1933 if (SendMessage(hDlgCtrl, BM_GETCHECK, 0, 0) == BST_CHECKED)
1934 EndDialog(hwndDlg, 1);
1935 else
1936 EndDialog(hwndDlg, 0);
1937 }
1938 else if (LOWORD(wParam) == IDCANCEL)
1939 {
1940 EndDialog(hwndDlg, -1);
1941 }
1942 else if (LOWORD(wParam) == 14000)
1943 {
1944 if (SendMessage(hDlgCtrl, BM_GETCHECK, 0, 0) == BST_CHECKED)
1945 SendMessage(hDlgCtrl, BM_SETCHECK, BST_UNCHECKED, 0);
1946 else
1947 SendMessage(hDlgCtrl, BM_SETCHECK, BST_CHECKED, 0);
1948 }
1949 }
1950 }
1951 return FALSE;
1952 }
1953
1954 /**************************************************************************
1955 * SH_ShellLinkDlgProc
1956 *
1957 * dialog proc of the shortcut property dialog
1958 */
1959
1960 INT_PTR CALLBACK CShellLink::SH_ShellLinkDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
1961 {
1962 CShellLink *pThis = reinterpret_cast<CShellLink *>(GetWindowLongPtr(hwndDlg, DWLP_USER));
1963
1964 switch(uMsg)
1965 {
1966 case WM_INITDIALOG:
1967 {
1968 LPPROPSHEETPAGEW ppsp = (LPPROPSHEETPAGEW)lParam;
1969 if (ppsp == NULL)
1970 break;
1971
1972 TRACE("ShellLink_DlgProc (WM_INITDIALOG hwnd %p lParam %p ppsplParam %x)\n", hwndDlg, lParam, ppsp->lParam);
1973
1974 pThis = reinterpret_cast<CShellLink *>(ppsp->lParam);
1975 SetWindowLongPtr(hwndDlg, DWLP_USER, (LONG_PTR)pThis);
1976
1977 TRACE("sArgs: %S sComponent: %S sDescription: %S sIcoPath: %S sPath: %S sPathRel: %S sProduct: %S sWorkDir: %S\n", pThis->sArgs, pThis->sComponent, pThis->sDescription,
1978 pThis->sIcoPath, pThis->sPath, pThis->sPathRel, pThis->sProduct, pThis->sWorkDir);
1979
1980 /* Get file information */
1981 SHFILEINFO fi;
1982 if (!SHGetFileInfoW(pThis->sLinkPath, 0, &fi, sizeof(fi), SHGFI_TYPENAME|SHGFI_ICON))
1983 {
1984 ERR("SHGetFileInfoW failed for %ls (%lu)\n", pThis->sLinkPath, GetLastError());
1985 fi.szTypeName[0] = L'\0';
1986 fi.hIcon = NULL;
1987 }
1988
1989 if (fi.hIcon) // TODO: destroy icon
1990 SendDlgItemMessageW(hwndDlg, 14000, STM_SETICON, (WPARAM)fi.hIcon, 0);
1991 else
1992 ERR("ExtractIconW failed %ls %u\n", pThis->sIcoPath, pThis->iIcoNdx);
1993
1994 /* target type */
1995 if (pThis->sPath)
1996 SetDlgItemTextW(hwndDlg, 14005, SH_GetTargetTypeByPath(pThis->sPath));
1997
1998 /* target location */
1999 if (pThis->sWorkDir)
2000 SetDlgItemTextW(hwndDlg, 14007, PathFindFileName(pThis->sWorkDir));
2001
2002 /* target path */
2003 if (pThis->sPath)
2004 {
2005 WCHAR newpath[2*MAX_PATH] = L"\0";
2006 if (wcschr(pThis->sPath, ' '))
2007 StringCchPrintfExW(newpath, 2*MAX_PATH, NULL, NULL, 0, L"\"%ls\"", pThis->sPath);
2008 else
2009 StringCchCopyExW(newpath, 2*MAX_PATH, pThis->sPath, NULL, NULL, 0);
2010
2011 if (pThis->sArgs && pThis->sArgs[0])
2012 {
2013 StringCchCatW(newpath, 2*MAX_PATH, L" ");
2014 StringCchCatW(newpath, 2*MAX_PATH, pThis->sArgs);
2015 }
2016 SetDlgItemTextW(hwndDlg, 14009, newpath);
2017 }
2018 /* working dir */
2019 if (pThis->sWorkDir)
2020 SetDlgItemTextW(hwndDlg, 14011, pThis->sWorkDir);
2021
2022 /* description */
2023 if (pThis->sDescription)
2024 SetDlgItemTextW(hwndDlg, 14019, pThis->sDescription);
2025
2026 return TRUE;
2027 }
2028 case WM_NOTIFY:
2029 {
2030 LPPSHNOTIFY lppsn = (LPPSHNOTIFY)lParam;
2031 if (lppsn->hdr.code == PSN_APPLY)
2032 {
2033 WCHAR wszBuf[MAX_PATH];
2034 /* set working directory */
2035 GetDlgItemTextW(hwndDlg, 14011, wszBuf, MAX_PATH);
2036 pThis->SetWorkingDirectory(wszBuf);
2037 /* set link destination */
2038 GetDlgItemTextW(hwndDlg, 14009, wszBuf, MAX_PATH);
2039 LPWSTR lpszArgs = NULL;
2040 LPWSTR unquoted = strdupW(wszBuf);
2041 StrTrimW(unquoted, L" ");
2042 if (!PathFileExistsW(unquoted))
2043 {
2044 lpszArgs = PathGetArgsW(unquoted);
2045 PathRemoveArgsW(unquoted);
2046 StrTrimW(lpszArgs, L" ");
2047 }
2048 if (unquoted[0] == '"' && unquoted[wcslen(unquoted)-1] == '"')
2049 PathUnquoteSpacesW(unquoted);
2050
2051
2052 WCHAR *pwszExt = PathFindExtensionW(unquoted);
2053 if (!wcsicmp(pwszExt, L".lnk"))
2054 {
2055 // FIXME load localized error msg
2056 MessageBoxW(hwndDlg, L"You cannot create a link to a shortcut", L"Error", MB_ICONERROR);
2057 SetWindowLongPtr(hwndDlg, DWL_MSGRESULT, PSNRET_INVALID_NOCHANGEPAGE);
2058 return TRUE;
2059 }
2060
2061 if (!PathFileExistsW(unquoted))
2062 {
2063 //FIXME load localized error msg
2064 MessageBoxW(hwndDlg, L"The specified file name in the target box is invalid", L"Error", MB_ICONERROR);
2065 SetWindowLongPtr(hwndDlg, DWL_MSGRESULT, PSNRET_INVALID_NOCHANGEPAGE);
2066 return TRUE;
2067 }
2068
2069 pThis->SetPath(unquoted);
2070 if (lpszArgs)
2071 pThis->SetArguments(lpszArgs);
2072 else
2073 pThis->SetArguments(L"\0");
2074
2075 HeapFree(GetProcessHeap(), 0, unquoted);
2076
2077 TRACE("This %p sLinkPath %S\n", pThis, pThis->sLinkPath);
2078 pThis->Save(pThis->sLinkPath, TRUE);
2079 SetWindowLongPtr(hwndDlg, DWL_MSGRESULT, PSNRET_NOERROR); return TRUE;
2080 }
2081 break;
2082 }
2083 case WM_COMMAND:
2084 switch(LOWORD(wParam))
2085 {
2086 case 14020:
2087 ///
2088 /// FIXME
2089 /// open target directory
2090 ///
2091 return TRUE;
2092 case 14021:
2093 {
2094 WCHAR wszPath[MAX_PATH] = L"";
2095
2096 if (pThis->sIcoPath)
2097 wcscpy(wszPath, pThis->sIcoPath);
2098 INT IconIndex = pThis->iIcoNdx;
2099 if (PickIconDlg(hwndDlg, wszPath, MAX_PATH, &IconIndex))
2100 {
2101 pThis->SetIconLocation(wszPath, IconIndex);
2102 ///
2103 /// FIXME redraw icon
2104 }
2105 return TRUE;
2106 }
2107
2108 case 14022:
2109 {
2110 INT_PTR result = DialogBoxParamW(shell32_hInstance, MAKEINTRESOURCEW(IDD_SHORTCUT_EXTENDED_PROPERTIES), hwndDlg, ExtendedShortcutProc, (LPARAM)pThis->bRunAs);
2111 if (result == 1 || result == 0)
2112 {
2113 if (pThis->bRunAs != result)
2114 {
2115 PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
2116 }
2117
2118 pThis->bRunAs = result;
2119 }
2120 return TRUE;
2121 }
2122 }
2123 if(HIWORD(wParam) == EN_CHANGE)
2124 PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
2125 break;
2126 default:
2127 break;
2128 }
2129 return FALSE;
2130 }
2131
2132 /**************************************************************************
2133 * ShellLink_IShellPropSheetExt interface
2134 */
2135
2136 HRESULT WINAPI CShellLink::AddPages(LPFNADDPROPSHEETPAGE pfnAddPage, LPARAM lParam)
2137 {
2138 HPROPSHEETPAGE hPage = SH_CreatePropertySheetPage(IDD_SHORTCUT_PROPERTIES, SH_ShellLinkDlgProc, (LPARAM)this, NULL);
2139 if (hPage == NULL)
2140 {
2141 ERR("failed to create property sheet page\n");
2142 return E_FAIL;
2143 }
2144
2145 if (!pfnAddPage(hPage, lParam))
2146 return E_FAIL;
2147
2148 return S_OK;
2149 }
2150
2151 HRESULT WINAPI CShellLink::ReplacePage(UINT uPageID, LPFNADDPROPSHEETPAGE pfnReplacePage, LPARAM lParam)
2152 {
2153 TRACE("(%p) (uPageID %u, pfnReplacePage %p lParam %p\n", this, uPageID, pfnReplacePage, lParam);
2154 return E_NOTIMPL;
2155 }
2156
2157 HRESULT WINAPI CShellLink::SetSite(IUnknown *punk)
2158 {
2159 TRACE("%p %p\n", this, punk);
2160
2161 site = punk;
2162
2163 return S_OK;
2164 }
2165
2166 HRESULT WINAPI CShellLink::GetSite(REFIID iid, void ** ppvSite)
2167 {
2168 TRACE("%p %s %p\n", this, debugstr_guid(&iid), ppvSite);
2169
2170 if (site == NULL)
2171 return E_FAIL;
2172
2173 return site->QueryInterface(iid, ppvSite);
2174 }
2175
2176 HRESULT WINAPI CShellLink::DragEnter(IDataObject *pDataObject,
2177 DWORD dwKeyState, POINTL pt, DWORD *pdwEffect)
2178 {
2179 TRACE("(%p)->(DataObject=%p)\n", this, pDataObject);
2180 LPCITEMIDLIST pidlLast;
2181 CComPtr<IShellFolder> psf;
2182
2183 HRESULT hr = SHBindToParent(pPidl, IID_PPV_ARG(IShellFolder, &psf), &pidlLast);
2184
2185 if (SUCCEEDED(hr))
2186 {
2187 hr = psf->GetUIObjectOf(0, 1, &pidlLast, IID_NULL_PPV_ARG(IDropTarget, &mDropTarget));
2188
2189 if (SUCCEEDED(hr))
2190 hr = mDropTarget->DragEnter(pDataObject, dwKeyState, pt, pdwEffect);
2191 else
2192 *pdwEffect = DROPEFFECT_NONE;
2193 }
2194 else
2195 *pdwEffect = DROPEFFECT_NONE;
2196
2197 return S_OK;
2198 }
2199
2200
2201
2202 HRESULT WINAPI CShellLink::DragOver(DWORD dwKeyState, POINTL pt,
2203 DWORD *pdwEffect)
2204 {
2205 TRACE("(%p)\n", this);
2206 HRESULT hr = S_OK;
2207 if (mDropTarget)
2208 hr = mDropTarget->DragOver(dwKeyState, pt, pdwEffect);
2209 return hr;
2210 }
2211
2212 HRESULT WINAPI CShellLink::DragLeave()
2213 {
2214 TRACE("(%p)\n", this);
2215 HRESULT hr = S_OK;
2216 if (mDropTarget)
2217 {
2218 hr = mDropTarget->DragLeave();
2219 mDropTarget->Release();
2220 }
2221
2222 return hr;
2223 }
2224
2225 HRESULT WINAPI CShellLink::Drop(IDataObject *pDataObject,
2226 DWORD dwKeyState, POINTL pt, DWORD *pdwEffect)
2227 {
2228 TRACE("(%p)\n", this);
2229 HRESULT hr = S_OK;
2230 if (mDropTarget)
2231 hr = mDropTarget->Drop(pDataObject, dwKeyState, pt, pdwEffect);
2232
2233 return hr;
2234 }
2235
2236 /**************************************************************************
2237 * IShellLink_ConstructFromFile
2238 */
2239 HRESULT WINAPI IShellLink_ConstructFromPath(WCHAR *path, REFIID riid, LPVOID *ppv)
2240 {
2241 CComPtr<IPersistFile> ppf;
2242 HRESULT hr = CShellLink::_CreatorClass::CreateInstance(NULL, IID_PPV_ARG(IPersistFile, &ppf));
2243 if (FAILED(hr))
2244 return hr;
2245
2246 hr = ppf->Load(path, 0);
2247 if (FAILED(hr))
2248 return hr;
2249
2250 return ppf->QueryInterface(riid, ppv);
2251 }
2252
2253 HRESULT WINAPI IShellLink_ConstructFromFile(IShellFolder * psf, LPCITEMIDLIST pidl, REFIID riid, LPVOID *ppv)
2254 {
2255 WCHAR path[MAX_PATH];
2256 if (!ILGetDisplayNameExW(psf, pidl, path, 0))
2257 return E_FAIL;
2258
2259 return IShellLink_ConstructFromPath(path, riid, ppv);
2260 }