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