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