Sync with trunk head
[reactos.git] / dll / win32 / shell32 / shelllink.c
1 /*
2 *
3 * Copyright 1997 Marcus Meissner
4 * Copyright 1998 Juergen Schmied
5 * Copyright 2005 Mike McCormack
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 *
21 * NOTES
22 * Nearly complete information about the binary formats
23 * of .lnk files available at http://www.wotsit.org
24 *
25 * You can use winedump to examine the contents of a link file:
26 * winedump lnk sc.lnk
27 *
28 * MSI advertised shortcuts are totally undocumented. They provide an
29 * icon for a program that is not yet installed, and invoke MSI to
30 * install the program when the shortcut is clicked on. They are
31 * created by passing a special string to SetPath, and the information
32 * in that string is parsed an stored.
33 */
34
35 #include <precomp.h>
36
37 WINE_DEFAULT_DEBUG_CHANNEL(shell);
38
39 /* link file formats */
40
41 #include "pshpack1.h"
42
43 typedef struct _LINK_HEADER
44 {
45 DWORD dwSize; /* 0x00 size of the header - 0x4c */
46 GUID MagicGuid; /* 0x04 is CLSID_ShellLink */
47 DWORD dwFlags; /* 0x14 describes elements following */
48 DWORD dwFileAttr; /* 0x18 attributes of the target file */
49 FILETIME Time1; /* 0x1c */
50 FILETIME Time2; /* 0x24 */
51 FILETIME Time3; /* 0x2c */
52 DWORD dwFileLength; /* 0x34 File length */
53 DWORD nIcon; /* 0x38 icon number */
54 DWORD fStartup; /* 0x3c startup type */
55 DWORD wHotKey; /* 0x40 hotkey */
56 DWORD Unknown5; /* 0x44 */
57 DWORD Unknown6; /* 0x48 */
58 } LINK_HEADER, * PLINK_HEADER;
59
60 #define SHLINK_LOCAL 0
61 #define SHLINK_REMOTE 1
62 #define MAX_PROPERTY_SHEET_PAGE 32
63
64 typedef struct _LOCATION_INFO
65 {
66 DWORD dwTotalSize;
67 DWORD dwHeaderSize;
68 DWORD dwFlags;
69 DWORD dwVolTableOfs;
70 DWORD dwLocalPathOfs;
71 DWORD dwNetworkVolTableOfs;
72 DWORD dwFinalPathOfs;
73 } LOCATION_INFO;
74
75 typedef struct _LOCAL_VOLUME_INFO
76 {
77 DWORD dwSize;
78 DWORD dwType;
79 DWORD dwVolSerial;
80 DWORD dwVolLabelOfs;
81 } LOCAL_VOLUME_INFO;
82
83 typedef struct volume_info_t
84 {
85 DWORD type;
86 DWORD serial;
87 WCHAR label[12]; /* assume 8.3 */
88 } volume_info;
89
90 #include "poppack.h"
91
92 static const IShellLinkAVtbl slvt;
93 static const IShellLinkWVtbl slvtw;
94 static const IPersistFileVtbl pfvt;
95 static const IPersistStreamVtbl psvt;
96 static const IShellLinkDataListVtbl dlvt;
97 static const IShellExtInitVtbl eivt;
98 static const IContextMenuVtbl cmvt;
99 static const IObjectWithSiteVtbl owsvt;
100 static const IShellPropSheetExtVtbl pse;
101
102 /* IShellLink Implementation */
103
104 typedef struct
105 {
106 const IShellLinkAVtbl *lpVtbl;
107 const IShellLinkWVtbl *lpvtblw;
108 const IPersistFileVtbl *lpvtblPersistFile;
109 const IPersistStreamVtbl *lpvtblPersistStream;
110 const IShellLinkDataListVtbl *lpvtblShellLinkDataList;
111 const IShellExtInitVtbl *lpvtblShellExtInit;
112 const IContextMenuVtbl *lpvtblContextMenu;
113 const IObjectWithSiteVtbl *lpvtblObjectWithSite;
114 const IShellPropSheetExtVtbl * lpvtblPropSheetExt;
115
116 LONG ref;
117
118 /* data structures according to the information in the link */
119 LPITEMIDLIST pPidl;
120 WORD wHotKey;
121 SYSTEMTIME time1;
122 SYSTEMTIME time2;
123 SYSTEMTIME time3;
124
125 DWORD iShowCmd;
126 LPWSTR sIcoPath;
127 INT iIcoNdx;
128 LPWSTR sPath;
129 LPWSTR sArgs;
130 LPWSTR sWorkDir;
131 LPWSTR sDescription;
132 LPWSTR sPathRel;
133 LPWSTR sProduct;
134 LPWSTR sComponent;
135 volume_info volume;
136 LPWSTR sLinkPath;
137 BOOL bRunAs;
138 BOOL bDirty;
139 INT iIdOpen; /* id of the "Open" entry in the context menu */
140 IUnknown *site;
141 } IShellLinkImpl, *LPIShellLinkImpl;
142
143 static LPIShellLinkImpl __inline impl_from_IShellLinkW( IShellLinkW *iface )
144 {
145 return (IShellLinkImpl *)((char*)iface - FIELD_OFFSET(IShellLinkImpl, lpvtblw));
146 }
147
148 static LPIShellLinkImpl __inline impl_from_IPersistFile( IPersistFile *iface )
149 {
150 return (IShellLinkImpl *)((char*)iface - FIELD_OFFSET(IShellLinkImpl, lpvtblPersistFile));
151 }
152
153 static LPIShellLinkImpl __inline impl_from_IPersistStream( IPersistStream *iface )
154 {
155 return (IShellLinkImpl *)((char*)iface - FIELD_OFFSET(IShellLinkImpl, lpvtblPersistStream));
156 }
157
158 static LPIShellLinkImpl __inline impl_from_IShellLinkDataList( IShellLinkDataList *iface )
159 {
160 return (IShellLinkImpl *)((char*)iface - FIELD_OFFSET(IShellLinkImpl, lpvtblShellLinkDataList));
161 }
162
163 static LPIShellLinkImpl __inline impl_from_IShellExtInit( IShellExtInit *iface )
164 {
165 return (IShellLinkImpl *)((char*)iface - FIELD_OFFSET(IShellLinkImpl, lpvtblShellExtInit));
166 }
167
168 static LPIShellLinkImpl __inline impl_from_IContextMenu( IContextMenu *iface )
169 {
170 return (IShellLinkImpl *)((char*)iface - FIELD_OFFSET(IShellLinkImpl, lpvtblContextMenu));
171 }
172
173 static LPIShellLinkImpl __inline impl_from_IObjectWithSite( IObjectWithSite *iface )
174 {
175 return (IShellLinkImpl *)((char*)iface - FIELD_OFFSET(IShellLinkImpl, lpvtblObjectWithSite));
176 }
177
178 static LPIShellLinkImpl __inline impl_from_IShellPropSheetExt( IShellPropSheetExt *iface )
179 {
180 return (IShellLinkImpl *)((char*)iface - FIELD_OFFSET(IShellLinkImpl, lpvtblPropSheetExt));
181 }
182
183
184 static HRESULT ShellLink_UpdatePath(LPCWSTR sPathRel, LPCWSTR path, LPCWSTR sWorkDir, LPWSTR* psPath);
185
186 /* strdup on the process heap */
187 static LPWSTR __inline HEAP_strdupAtoW( HANDLE heap, DWORD flags, LPCSTR str)
188 {
189 INT len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
190 LPWSTR p = HeapAlloc( heap, flags, len*sizeof (WCHAR) );
191 if( !p )
192 return p;
193 MultiByteToWideChar( CP_ACP, 0, str, -1, p, len );
194 return p;
195 }
196
197 static LPWSTR __inline strdupW( LPCWSTR src )
198 {
199 LPWSTR dest;
200 if (!src) return NULL;
201 dest = HeapAlloc( GetProcessHeap(), 0, (wcslen(src)+1)*sizeof(WCHAR) );
202 if (dest)
203 wcscpy(dest, src);
204 return dest;
205 }
206
207 /**************************************************************************
208 * ShellLink::QueryInterface implementation
209 */
210 static HRESULT ShellLink_QueryInterface( IShellLinkImpl *This, REFIID riid, LPVOID *ppvObj)
211 {
212 TRACE("(%p)->(\n\tIID:\t%s)\n",This,debugstr_guid(riid));
213
214 *ppvObj = NULL;
215
216 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IShellLinkA))
217 {
218 *ppvObj = This;
219 }
220 else if(IsEqualIID(riid, &IID_IShellLinkW))
221 {
222 *ppvObj = &(This->lpvtblw);
223 }
224 else if(IsEqualIID(riid, &IID_IPersistFile))
225 {
226 *ppvObj = &(This->lpvtblPersistFile);
227 }
228 else if(IsEqualIID(riid, &IID_IPersistStream))
229 {
230 *ppvObj = &(This->lpvtblPersistStream);
231 }
232 else if(IsEqualIID(riid, &IID_IShellLinkDataList))
233 {
234 *ppvObj = &(This->lpvtblShellLinkDataList);
235 }
236 else if(IsEqualIID(riid, &IID_IShellExtInit))
237 {
238 *ppvObj = &(This->lpvtblShellExtInit);
239 }
240 else if(IsEqualIID(riid, &IID_IContextMenu))
241 {
242 *ppvObj = &(This->lpvtblContextMenu);
243 }
244 else if(IsEqualIID(riid, &IID_IObjectWithSite))
245 {
246 *ppvObj = &(This->lpvtblObjectWithSite);
247 }
248 else if(IsEqualIID(riid, &IID_IShellPropSheetExt))
249 {
250 *ppvObj = &(This->lpvtblPropSheetExt);
251 }
252
253 if(*ppvObj)
254 {
255 IUnknown_AddRef((IUnknown*)(*ppvObj));
256 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
257 return S_OK;
258 }
259 ERR("-- Interface: E_NOINTERFACE\n");
260 return E_NOINTERFACE;
261 }
262
263 /**************************************************************************
264 * ShellLink::AddRef implementation
265 */
266 static ULONG ShellLink_AddRef( IShellLinkImpl *This )
267 {
268 ULONG refCount = InterlockedIncrement(&This->ref);
269
270 TRACE("(%p)->(count=%u)\n", This, refCount - 1);
271
272 return refCount;
273 }
274
275 /**************************************************************************
276 * ShellLink::Release implementation
277 */
278 static ULONG ShellLink_Release( IShellLinkImpl *This )
279 {
280 ULONG refCount = InterlockedDecrement(&This->ref);
281
282 TRACE("(%p)->(count=%u)\n", This, refCount + 1);
283
284 if (refCount)
285 return refCount;
286
287 TRACE("-- destroying IShellLink(%p)\n",This);
288
289 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
290 HeapFree(GetProcessHeap(), 0, This->sArgs);
291 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
292 HeapFree(GetProcessHeap(), 0, This->sDescription);
293 HeapFree(GetProcessHeap(),0,This->sPath);
294 HeapFree(GetProcessHeap(),0,This->sLinkPath);
295
296 if (This->site)
297 IUnknown_Release( This->site );
298
299 if (This->pPidl)
300 ILFree(This->pPidl);
301
302 LocalFree(This);
303
304 return 0;
305 }
306
307 static HRESULT ShellLink_GetClassID( IShellLinkImpl *This, CLSID *pclsid )
308 {
309 TRACE("%p %p\n", This, pclsid);
310
311 *pclsid = CLSID_ShellLink;
312 return S_OK;
313 }
314
315 /**************************************************************************
316 * IPersistFile_QueryInterface
317 */
318 static HRESULT WINAPI IPersistFile_fnQueryInterface(
319 IPersistFile* iface,
320 REFIID riid,
321 LPVOID *ppvObj)
322 {
323 IShellLinkImpl *This = impl_from_IPersistFile(iface);
324 return ShellLink_QueryInterface( This, riid, ppvObj );
325 }
326
327 /******************************************************************************
328 * IPersistFile_AddRef
329 */
330 static ULONG WINAPI IPersistFile_fnAddRef(IPersistFile* iface)
331 {
332 IShellLinkImpl *This = impl_from_IPersistFile(iface);
333 return ShellLink_AddRef( This );
334 }
335
336 /******************************************************************************
337 * IPersistFile_Release
338 */
339 static ULONG WINAPI IPersistFile_fnRelease(IPersistFile* iface)
340 {
341 IShellLinkImpl *This = impl_from_IPersistFile(iface);
342 return IShellLinkA_Release((IShellLinkA*)This);
343 }
344
345 static HRESULT WINAPI IPersistFile_fnGetClassID(IPersistFile* iface, CLSID *pClassID)
346 {
347 IShellLinkImpl *This = impl_from_IPersistFile(iface);
348 return ShellLink_GetClassID( This, pClassID );
349 }
350
351 static HRESULT WINAPI IPersistFile_fnIsDirty(IPersistFile* iface)
352 {
353 IShellLinkImpl *This = impl_from_IPersistFile(iface);
354
355 TRACE("(%p)\n",This);
356
357 if (This->bDirty)
358 return S_OK;
359
360 return S_FALSE;
361 }
362
363 static HRESULT WINAPI IPersistFile_fnLoad(IPersistFile* iface, LPCOLESTR pszFileName, DWORD dwMode)
364 {
365 IShellLinkImpl *This = impl_from_IPersistFile(iface);
366 IPersistStream *StreamThis = (IPersistStream *)&This->lpvtblPersistStream;
367 HRESULT r;
368 IStream *stm;
369
370 TRACE("(%p, %s, %x)\n",This, debugstr_w(pszFileName), dwMode);
371
372 if( dwMode == 0 )
373 dwMode = STGM_READ | STGM_SHARE_DENY_WRITE;
374 r = SHCreateStreamOnFileW(pszFileName, dwMode, &stm);
375 if( SUCCEEDED( r ) )
376 {
377 HeapFree(GetProcessHeap(), 0, This->sLinkPath);
378 This->sLinkPath = strdupW(pszFileName);
379 r = IPersistStream_Load(StreamThis, stm);
380 ShellLink_UpdatePath(This->sPathRel, pszFileName, This->sWorkDir, &This->sPath);
381 IStream_Release( stm );
382 This->bDirty = FALSE;
383 }
384 TRACE("-- returning hr %08x\n", r);
385 return r;
386 }
387
388 static BOOL StartLinkProcessor( LPCOLESTR szLink )
389 {
390 static const WCHAR szFormat[] = {
391 'w','i','n','e','m','e','n','u','b','u','i','l','d','e','r','.','e','x','e',
392 ' ','-','w',' ','"','%','s','"',0 };
393 LONG len;
394 LPWSTR buffer;
395 STARTUPINFOW si;
396 PROCESS_INFORMATION pi;
397 BOOL ret;
398
399 len = sizeof(szFormat) + wcslen( szLink ) * sizeof(WCHAR);
400 buffer = HeapAlloc( GetProcessHeap(), 0, len );
401 if( !buffer )
402 return FALSE;
403
404 swprintf( buffer, szFormat, szLink );
405
406 TRACE("starting %s\n",debugstr_w(buffer));
407
408 memset(&si, 0, sizeof(si));
409 si.cb = sizeof(si);
410
411 ret = CreateProcessW( NULL, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi );
412
413 HeapFree( GetProcessHeap(), 0, buffer );
414
415 if (ret)
416 {
417 CloseHandle( pi.hProcess );
418 CloseHandle( pi.hThread );
419 }
420
421 return ret;
422 }
423
424 static HRESULT WINAPI IPersistFile_fnSave(IPersistFile* iface, LPCOLESTR pszFileName, BOOL fRemember)
425 {
426 IShellLinkImpl *This = impl_from_IPersistFile(iface);
427 IPersistStream *StreamThis = (IPersistStream *)&This->lpvtblPersistStream;
428 HRESULT r;
429 IStream *stm;
430
431 TRACE("(%p)->(%s)\n",This,debugstr_w(pszFileName));
432
433 if (!pszFileName)
434 return E_FAIL;
435
436 r = SHCreateStreamOnFileW( pszFileName, STGM_READWRITE | STGM_CREATE | STGM_SHARE_EXCLUSIVE, &stm );
437 if( SUCCEEDED( r ) )
438 {
439 r = IPersistStream_Save(StreamThis, stm, FALSE);
440 IStream_Release( stm );
441
442 if( SUCCEEDED( r ) )
443 {
444 StartLinkProcessor( pszFileName );
445
446 This->bDirty = FALSE;
447 }
448 else
449 {
450 DeleteFileW( pszFileName );
451 WARN("Failed to create shortcut %s\n", debugstr_w(pszFileName) );
452 }
453 }
454
455 return r;
456 }
457
458 static HRESULT WINAPI IPersistFile_fnSaveCompleted(IPersistFile* iface, LPCOLESTR pszFileName)
459 {
460 IShellLinkImpl *This = impl_from_IPersistFile(iface);
461 FIXME("(%p)->(%s)\n",This,debugstr_w(pszFileName));
462 return NOERROR;
463 }
464
465 static HRESULT WINAPI IPersistFile_fnGetCurFile(IPersistFile* iface, LPOLESTR *ppszFileName)
466 {
467 IShellLinkImpl *This = impl_from_IPersistFile(iface);
468 FIXME("(%p)\n",This);
469 return NOERROR;
470 }
471
472 static const IPersistFileVtbl pfvt =
473 {
474 IPersistFile_fnQueryInterface,
475 IPersistFile_fnAddRef,
476 IPersistFile_fnRelease,
477 IPersistFile_fnGetClassID,
478 IPersistFile_fnIsDirty,
479 IPersistFile_fnLoad,
480 IPersistFile_fnSave,
481 IPersistFile_fnSaveCompleted,
482 IPersistFile_fnGetCurFile
483 };
484
485 /************************************************************************
486 * IPersistStream_QueryInterface
487 */
488 static HRESULT WINAPI IPersistStream_fnQueryInterface(
489 IPersistStream* iface,
490 REFIID riid,
491 VOID** ppvObj)
492 {
493 IShellLinkImpl *This = impl_from_IPersistStream(iface);
494 return ShellLink_QueryInterface( This, riid, ppvObj );
495 }
496
497 /************************************************************************
498 * IPersistStream_Release
499 */
500 static ULONG WINAPI IPersistStream_fnRelease(
501 IPersistStream* iface)
502 {
503 IShellLinkImpl *This = impl_from_IPersistStream(iface);
504 return IShellLinkA_Release((IShellLinkA*)This);
505 }
506
507 /************************************************************************
508 * IPersistStream_AddRef
509 */
510 static ULONG WINAPI IPersistStream_fnAddRef(
511 IPersistStream* iface)
512 {
513 IShellLinkImpl *This = impl_from_IPersistStream(iface);
514 return ShellLink_AddRef( This );
515 }
516
517 /************************************************************************
518 * IPersistStream_GetClassID
519 *
520 */
521 static HRESULT WINAPI IPersistStream_fnGetClassID(
522 IPersistStream* iface,
523 CLSID* pClassID)
524 {
525 IShellLinkImpl *This = impl_from_IPersistStream(iface);
526 return ShellLink_GetClassID( This, pClassID );
527 }
528
529 /************************************************************************
530 * IPersistStream_IsDirty (IPersistStream)
531 */
532 static HRESULT WINAPI IPersistStream_fnIsDirty(
533 IPersistStream* iface)
534 {
535 IShellLinkImpl *This = impl_from_IPersistStream(iface);
536
537 TRACE("(%p)\n", This);
538
539 return S_OK;
540 }
541
542
543 static HRESULT Stream_LoadString( IStream* stm, BOOL unicode, LPWSTR *pstr )
544 {
545 DWORD count;
546 USHORT len;
547 LPVOID temp;
548 LPWSTR str;
549 HRESULT r;
550
551 TRACE("%p\n", stm);
552
553 count = 0;
554 r = IStream_Read(stm, &len, sizeof(len), &count);
555 if ( FAILED (r) || ( count != sizeof(len) ) )
556 return E_FAIL;
557
558 if( unicode )
559 len *= sizeof (WCHAR);
560
561 TRACE("reading %d\n", len);
562 temp = HeapAlloc(GetProcessHeap(), 0, len+sizeof(WCHAR));
563 if( !temp )
564 return E_OUTOFMEMORY;
565 count = 0;
566 r = IStream_Read(stm, temp, len, &count);
567 if( FAILED (r) || ( count != len ) )
568 {
569 HeapFree( GetProcessHeap(), 0, temp );
570 return E_FAIL;
571 }
572
573 TRACE("read %s\n", debugstr_an(temp,len));
574
575 /* convert to unicode if necessary */
576 if( !unicode )
577 {
578 count = MultiByteToWideChar( CP_ACP, 0, temp, len, NULL, 0 );
579 str = HeapAlloc( GetProcessHeap(), 0, (count+1)*sizeof (WCHAR) );
580 if( !str )
581 {
582 HeapFree( GetProcessHeap(), 0, temp );
583 return E_OUTOFMEMORY;
584 }
585 MultiByteToWideChar( CP_ACP, 0, temp, len, str, count );
586 HeapFree( GetProcessHeap(), 0, temp );
587 }
588 else
589 {
590 count /= 2;
591 str = temp;
592 }
593 str[count] = 0;
594
595 *pstr = str;
596
597 return S_OK;
598 }
599
600 static HRESULT Stream_ReadChunk( IStream* stm, LPVOID *data )
601 {
602 DWORD size;
603 ULONG count;
604 HRESULT r;
605 struct sized_chunk {
606 DWORD size;
607 unsigned char data[1];
608 } *chunk;
609
610 TRACE("%p\n",stm);
611
612 r = IStream_Read( stm, &size, sizeof(size), &count );
613 if( FAILED( r ) || count != sizeof(size) )
614 return E_FAIL;
615
616 chunk = HeapAlloc( GetProcessHeap(), 0, size );
617 if( !chunk )
618 return E_OUTOFMEMORY;
619
620 chunk->size = size;
621 r = IStream_Read( stm, chunk->data, size - sizeof(size), &count );
622 if( FAILED( r ) || count != (size - sizeof(size)) )
623 {
624 HeapFree( GetProcessHeap(), 0, chunk );
625 return E_FAIL;
626 }
627
628 TRACE("Read %d bytes\n",chunk->size);
629
630 *data = chunk;
631
632 return S_OK;
633 }
634
635 static BOOL Stream_LoadVolume( LOCAL_VOLUME_INFO *vol, volume_info *volume )
636 {
637 const int label_sz = sizeof volume->label/sizeof volume->label[0];
638 LPSTR label;
639 int len;
640
641 volume->serial = vol->dwVolSerial;
642 volume->type = vol->dwType;
643
644 if( !vol->dwVolLabelOfs )
645 return FALSE;
646 if( vol->dwSize <= vol->dwVolLabelOfs )
647 return FALSE;
648 len = vol->dwSize - vol->dwVolLabelOfs;
649
650 label = (LPSTR) vol;
651 label += vol->dwVolLabelOfs;
652 MultiByteToWideChar( CP_ACP, 0, label, len, volume->label, label_sz-1);
653
654 return TRUE;
655 }
656
657 static LPWSTR Stream_LoadPath( LPCSTR p, DWORD maxlen )
658 {
659 int len = 0, wlen;
660 LPWSTR path;
661
662 while( p[len] && (len < maxlen) )
663 len++;
664
665 wlen = MultiByteToWideChar(CP_ACP, 0, p, len, NULL, 0);
666 path = HeapAlloc(GetProcessHeap(), 0, (wlen+1)*sizeof(WCHAR));
667 MultiByteToWideChar(CP_ACP, 0, p, len, path, wlen);
668 path[wlen] = 0;
669
670 return path;
671 }
672
673 static HRESULT Stream_LoadLocation( IStream *stm,
674 volume_info *volume, LPWSTR *path )
675 {
676 char *p = NULL;
677 LOCATION_INFO *loc;
678 HRESULT r;
679 DWORD n;
680
681 r = Stream_ReadChunk( stm, (LPVOID*) &p );
682 if( FAILED(r) )
683 return r;
684
685 loc = (LOCATION_INFO*) p;
686 if (loc->dwTotalSize < sizeof(LOCATION_INFO))
687 {
688 HeapFree( GetProcessHeap(), 0, p );
689 return E_FAIL;
690 }
691
692 /* if there's valid local volume information, load it */
693 if( loc->dwVolTableOfs &&
694 ((loc->dwVolTableOfs + sizeof(LOCAL_VOLUME_INFO)) <= loc->dwTotalSize) )
695 {
696 LOCAL_VOLUME_INFO *volume_info;
697
698 volume_info = (LOCAL_VOLUME_INFO*) &p[loc->dwVolTableOfs];
699 Stream_LoadVolume( volume_info, volume );
700 }
701
702 /* if there's a local path, load it */
703 n = loc->dwLocalPathOfs;
704 if( n && (n < loc->dwTotalSize) )
705 *path = Stream_LoadPath( &p[n], loc->dwTotalSize - n );
706
707 TRACE("type %d serial %08x name %s path %s\n", volume->type,
708 volume->serial, debugstr_w(volume->label), debugstr_w(*path));
709
710 HeapFree( GetProcessHeap(), 0, p );
711 return S_OK;
712 }
713
714 /*
715 * The format of the advertised shortcut info seems to be:
716 *
717 * Offset Description
718 * ------ -----------
719 *
720 * 0 Length of the block (4 bytes, usually 0x314)
721 * 4 tag (dword)
722 * 8 string data in ASCII
723 * 8+0x104 string data in UNICODE
724 *
725 * In the original Win32 implementation the buffers are not initialized
726 * to zero, so data trailing the string is random garbage.
727 */
728 static HRESULT Stream_LoadAdvertiseInfo( IStream* stm, LPWSTR *str )
729 {
730 DWORD size;
731 ULONG count;
732 HRESULT r;
733 EXP_DARWIN_LINK buffer;
734
735 TRACE("%p\n",stm);
736
737 r = IStream_Read( stm, &buffer.dbh.cbSize, sizeof (DWORD), &count );
738 if( FAILED( r ) )
739 return r;
740
741 /* make sure that we read the size of the structure even on error */
742 size = sizeof buffer - sizeof (DWORD);
743 if( buffer.dbh.cbSize != sizeof buffer )
744 {
745 ERR("Ooops. This structure is not as expected...\n");
746 return E_FAIL;
747 }
748
749 r = IStream_Read( stm, &buffer.dbh.dwSignature, size, &count );
750 if( FAILED( r ) )
751 return r;
752
753 if( count != size )
754 return E_FAIL;
755
756 TRACE("magic %08x string = %s\n", buffer.dbh.dwSignature, debugstr_w(buffer.szwDarwinID));
757
758 if( (buffer.dbh.dwSignature&0xffff0000) != 0xa0000000 )
759 {
760 ERR("Unknown magic number %08x in advertised shortcut\n", buffer.dbh.dwSignature);
761 return E_FAIL;
762 }
763
764 *str = HeapAlloc( GetProcessHeap(), 0,
765 (wcslen(buffer.szwDarwinID)+1) * sizeof(WCHAR) );
766 wcscpy( *str, buffer.szwDarwinID );
767
768 return S_OK;
769 }
770
771 /************************************************************************
772 * IPersistStream_Load (IPersistStream)
773 */
774 static HRESULT WINAPI IPersistStream_fnLoad(
775 IPersistStream* iface,
776 IStream* stm)
777 {
778 LINK_HEADER hdr;
779 ULONG dwBytesRead;
780 BOOL unicode;
781 HRESULT r;
782 DWORD zero;
783
784 IShellLinkImpl *This = impl_from_IPersistStream(iface);
785
786 TRACE("%p %p\n", This, stm);
787
788 if( !stm )
789 return STG_E_INVALIDPOINTER;
790
791 dwBytesRead = 0;
792 r = IStream_Read(stm, &hdr, sizeof(hdr), &dwBytesRead);
793 if( FAILED( r ) )
794 return r;
795
796 if( dwBytesRead != sizeof(hdr))
797 return E_FAIL;
798 if( hdr.dwSize != sizeof(hdr))
799 return E_FAIL;
800 if( !IsEqualIID(&hdr.MagicGuid, &CLSID_ShellLink) )
801 return E_FAIL;
802
803 /* free all the old stuff */
804 ILFree(This->pPidl);
805 This->pPidl = NULL;
806 memset( &This->volume, 0, sizeof This->volume );
807 HeapFree(GetProcessHeap(), 0, This->sPath);
808 This->sPath = NULL;
809 HeapFree(GetProcessHeap(), 0, This->sDescription);
810 This->sDescription = NULL;
811 HeapFree(GetProcessHeap(), 0, This->sPathRel);
812 This->sPathRel = NULL;
813 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
814 This->sWorkDir = NULL;
815 HeapFree(GetProcessHeap(), 0, This->sArgs);
816 This->sArgs = NULL;
817 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
818 This->sIcoPath = NULL;
819 HeapFree(GetProcessHeap(), 0, This->sProduct);
820 This->sProduct = NULL;
821 HeapFree(GetProcessHeap(), 0, This->sComponent);
822 This->sComponent = NULL;
823
824 This->wHotKey = (WORD)hdr.wHotKey;
825 This->iIcoNdx = hdr.nIcon;
826 FileTimeToSystemTime (&hdr.Time1, &This->time1);
827 FileTimeToSystemTime (&hdr.Time2, &This->time2);
828 FileTimeToSystemTime (&hdr.Time3, &This->time3);
829 if (TRACE_ON(shell))
830 {
831 WCHAR sTemp[MAX_PATH];
832 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE, &This->time1,
833 NULL, sTemp, sizeof(sTemp)/sizeof(*sTemp));
834 TRACE("-- time1: %s\n", debugstr_w(sTemp) );
835 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE, &This->time2,
836 NULL, sTemp, sizeof(sTemp)/sizeof(*sTemp));
837 TRACE("-- time2: %s\n", debugstr_w(sTemp) );
838 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE, &This->time3,
839 NULL, sTemp, sizeof(sTemp)/sizeof(*sTemp));
840 TRACE("-- time3: %s\n", debugstr_w(sTemp) );
841 }
842
843 /* load all the new stuff */
844 if( hdr.dwFlags & SLDF_HAS_ID_LIST )
845 {
846 r = ILLoadFromStream( stm, &This->pPidl );
847 if( FAILED( r ) )
848 return r;
849 }
850 pdump(This->pPidl);
851
852 /* load the location information */
853 if( hdr.dwFlags & SLDF_HAS_LINK_INFO )
854 r = Stream_LoadLocation( stm, &This->volume, &This->sPath );
855 if( FAILED( r ) )
856 goto end;
857
858 unicode = hdr.dwFlags & SLDF_UNICODE;
859 if( hdr.dwFlags & SLDF_HAS_NAME )
860 {
861 r = Stream_LoadString( stm, unicode, &This->sDescription );
862 TRACE("Description -> %s\n",debugstr_w(This->sDescription));
863 }
864 if( FAILED( r ) )
865 goto end;
866
867 if( hdr.dwFlags & SLDF_HAS_RELPATH )
868 {
869 r = Stream_LoadString( stm, unicode, &This->sPathRel );
870 TRACE("Relative Path-> %s\n",debugstr_w(This->sPathRel));
871 }
872 if( FAILED( r ) )
873 goto end;
874
875 if( hdr.dwFlags & SLDF_HAS_WORKINGDIR )
876 {
877 r = Stream_LoadString( stm, unicode, &This->sWorkDir );
878 TRACE("Working Dir -> %s\n",debugstr_w(This->sWorkDir));
879 }
880 if( FAILED( r ) )
881 goto end;
882
883 if( hdr.dwFlags & SLDF_HAS_ARGS )
884 {
885 r = Stream_LoadString( stm, unicode, &This->sArgs );
886 TRACE("Working Dir -> %s\n",debugstr_w(This->sArgs));
887 }
888 if( FAILED( r ) )
889 goto end;
890
891 if( hdr.dwFlags & SLDF_HAS_ICONLOCATION )
892 {
893 r = Stream_LoadString( stm, unicode, &This->sIcoPath );
894 TRACE("Icon file -> %s\n",debugstr_w(This->sIcoPath));
895 }
896 if( FAILED( r ) )
897 goto end;
898
899 #if (NTDDI_VERSION < NTDDI_LONGHORN)
900 if( hdr.dwFlags & SLDF_HAS_LOGO3ID )
901 {
902 r = Stream_LoadAdvertiseInfo( stm, &This->sProduct );
903 TRACE("Product -> %s\n",debugstr_w(This->sProduct));
904 }
905 if( FAILED( r ) )
906 goto end;
907 #endif
908
909 if( hdr.dwFlags & SLDF_HAS_DARWINID )
910 {
911 r = Stream_LoadAdvertiseInfo( stm, &This->sComponent );
912 TRACE("Component -> %s\n",debugstr_w(This->sComponent));
913 }
914 if( hdr.dwFlags & SLDF_RUNAS_USER )
915 {
916 This->bRunAs = TRUE;
917 }
918 else
919 {
920 This->bRunAs = FALSE;
921 }
922
923 if( FAILED( r ) )
924 goto end;
925
926 r = IStream_Read(stm, &zero, sizeof zero, &dwBytesRead);
927 if( FAILED( r ) || zero || dwBytesRead != sizeof zero )
928 ERR("Last word was not zero\n");
929
930 TRACE("OK\n");
931
932 pdump (This->pPidl);
933
934 return S_OK;
935 end:
936 return r;
937 }
938
939 /************************************************************************
940 * Stream_WriteString
941 *
942 * Helper function for IPersistStream_Save. Writes a unicode string
943 * with terminating nul byte to a stream, preceded by the its length.
944 */
945 static HRESULT Stream_WriteString( IStream* stm, LPCWSTR str )
946 {
947 USHORT len = wcslen( str ) + 1;
948 DWORD count;
949 HRESULT r;
950
951 r = IStream_Write( stm, &len, sizeof(len), &count );
952 if( FAILED( r ) )
953 return r;
954
955 len *= sizeof(WCHAR);
956
957 r = IStream_Write( stm, str, len, &count );
958 if( FAILED( r ) )
959 return r;
960
961 return S_OK;
962 }
963
964 /************************************************************************
965 * Stream_WriteLocationInfo
966 *
967 * Writes the location info to a stream
968 *
969 * FIXME: One day we might want to write the network volume information
970 * and the final path.
971 * Figure out how Windows deals with unicode paths here.
972 */
973 static HRESULT Stream_WriteLocationInfo( IStream* stm, LPCWSTR path,
974 volume_info *volume )
975 {
976 DWORD total_size, path_size, volume_info_size, label_size, final_path_size;
977 LOCAL_VOLUME_INFO *vol;
978 LOCATION_INFO *loc;
979 LPSTR szLabel, szPath, szFinalPath;
980 ULONG count = 0;
981 HRESULT hr;
982
983 TRACE("%p %s %p\n", stm, debugstr_w(path), volume);
984
985 /* figure out the size of everything */
986 label_size = WideCharToMultiByte( CP_ACP, 0, volume->label, -1,
987 NULL, 0, NULL, NULL );
988 path_size = WideCharToMultiByte( CP_ACP, 0, path, -1,
989 NULL, 0, NULL, NULL );
990 volume_info_size = sizeof *vol + label_size;
991 final_path_size = 1;
992 total_size = sizeof *loc + volume_info_size + path_size + final_path_size;
993
994 /* create pointers to everything */
995 loc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, total_size);
996 vol = (LOCAL_VOLUME_INFO*) &loc[1];
997 szLabel = (LPSTR) &vol[1];
998 szPath = &szLabel[label_size];
999 szFinalPath = &szPath[path_size];
1000
1001 /* fill in the location information header */
1002 loc->dwTotalSize = total_size;
1003 loc->dwHeaderSize = sizeof (*loc);
1004 loc->dwFlags = 1;
1005 loc->dwVolTableOfs = sizeof (*loc);
1006 loc->dwLocalPathOfs = sizeof (*loc) + volume_info_size;
1007 loc->dwNetworkVolTableOfs = 0;
1008 loc->dwFinalPathOfs = sizeof (*loc) + volume_info_size + path_size;
1009
1010 /* fill in the volume information */
1011 vol->dwSize = volume_info_size;
1012 vol->dwType = volume->type;
1013 vol->dwVolSerial = volume->serial;
1014 vol->dwVolLabelOfs = sizeof (*vol);
1015
1016 /* copy in the strings */
1017 WideCharToMultiByte( CP_ACP, 0, volume->label, -1,
1018 szLabel, label_size, NULL, NULL );
1019 WideCharToMultiByte( CP_ACP, 0, path, -1,
1020 szPath, path_size, NULL, NULL );
1021 szFinalPath[0] = 0;
1022
1023 hr = IStream_Write( stm, loc, total_size, &count );
1024 HeapFree(GetProcessHeap(), 0, loc);
1025
1026 return hr;
1027 }
1028
1029 static EXP_DARWIN_LINK* shelllink_build_darwinid( LPCWSTR string, DWORD magic )
1030 {
1031 EXP_DARWIN_LINK *buffer;
1032
1033 buffer = LocalAlloc( LMEM_ZEROINIT, sizeof *buffer );
1034 buffer->dbh.cbSize = sizeof *buffer;
1035 buffer->dbh.dwSignature = magic;
1036 lstrcpynW( buffer->szwDarwinID, string, MAX_PATH );
1037 WideCharToMultiByte(CP_ACP, 0, string, -1, buffer->szDarwinID, MAX_PATH, NULL, NULL );
1038
1039 return buffer;
1040 }
1041
1042 static HRESULT Stream_WriteAdvertiseInfo( IStream* stm, LPCWSTR string, DWORD magic )
1043 {
1044 EXP_DARWIN_LINK *buffer;
1045 ULONG count;
1046
1047 TRACE("%p\n",stm);
1048
1049 buffer = shelllink_build_darwinid( string, magic );
1050
1051 return IStream_Write( stm, buffer, buffer->dbh.cbSize, &count );
1052 }
1053
1054 /************************************************************************
1055 * IPersistStream_Save (IPersistStream)
1056 *
1057 * FIXME: makes assumptions about byte order
1058 */
1059 static HRESULT WINAPI IPersistStream_fnSave(
1060 IPersistStream* iface,
1061 IStream* stm,
1062 BOOL fClearDirty)
1063 {
1064 LINK_HEADER header;
1065 ULONG count;
1066 DWORD zero;
1067 HRESULT r;
1068
1069 IShellLinkImpl *This = impl_from_IPersistStream(iface);
1070
1071 TRACE("%p %p %x\n", This, stm, fClearDirty);
1072
1073 memset(&header, 0, sizeof(header));
1074 header.dwSize = sizeof(header);
1075 header.fStartup = This->iShowCmd;
1076 header.MagicGuid = CLSID_ShellLink;
1077
1078 header.wHotKey = This->wHotKey;
1079 header.nIcon = This->iIcoNdx;
1080 header.dwFlags = SLDF_UNICODE; /* strings are in unicode */
1081 if( This->pPidl )
1082 header.dwFlags |= SLDF_HAS_ID_LIST;
1083 if( This->sPath )
1084 header.dwFlags |= SLDF_HAS_LINK_INFO;
1085 if( This->sDescription )
1086 header.dwFlags |= SLDF_HAS_NAME;
1087 if( This->sWorkDir )
1088 header.dwFlags |= SLDF_HAS_WORKINGDIR;
1089 if( This->sArgs )
1090 header.dwFlags |= SLDF_HAS_ARGS;
1091 if( This->sIcoPath )
1092 header.dwFlags |= SLDF_HAS_ICONLOCATION;
1093 #if (NTDDI_VERSION < NTDDI_LONGHORN)
1094 if( This->sProduct )
1095 header.dwFlags |= SLDF_HAS_LOGO3ID;
1096 #endif
1097 if( This->sComponent )
1098 header.dwFlags |= SLDF_HAS_DARWINID;
1099 if( This->bRunAs )
1100 header.dwFlags |= SLDF_RUNAS_USER;
1101
1102 SystemTimeToFileTime ( &This->time1, &header.Time1 );
1103 SystemTimeToFileTime ( &This->time2, &header.Time2 );
1104 SystemTimeToFileTime ( &This->time3, &header.Time3 );
1105
1106 /* write the Shortcut header */
1107 r = IStream_Write( stm, &header, sizeof(header), &count );
1108 if( FAILED( r ) )
1109 {
1110 ERR("Write failed at %d\n",__LINE__);
1111 return r;
1112 }
1113
1114 TRACE("Writing pidl\n");
1115
1116 /* write the PIDL to the shortcut */
1117 if( This->pPidl )
1118 {
1119 r = ILSaveToStream( stm, This->pPidl );
1120 if( FAILED( r ) )
1121 {
1122 ERR("Failed to write PIDL at %d\n",__LINE__);
1123 return r;
1124 }
1125 }
1126
1127 if( This->sPath )
1128 Stream_WriteLocationInfo( stm, This->sPath, &This->volume );
1129
1130 if( This->sDescription )
1131 r = Stream_WriteString( stm, This->sDescription );
1132
1133 if( This->sPathRel )
1134 r = Stream_WriteString( stm, This->sPathRel );
1135
1136 if( This->sWorkDir )
1137 r = Stream_WriteString( stm, This->sWorkDir );
1138
1139 if( This->sArgs )
1140 r = Stream_WriteString( stm, This->sArgs );
1141
1142 if( This->sIcoPath )
1143 r = Stream_WriteString( stm, This->sIcoPath );
1144
1145 if( This->sProduct )
1146 r = Stream_WriteAdvertiseInfo( stm, This->sProduct, EXP_SZ_ICON_SIG );
1147
1148 if( This->sComponent )
1149 r = Stream_WriteAdvertiseInfo( stm, This->sComponent, EXP_DARWIN_ID_SIG );
1150
1151 /* the last field is a single zero dword */
1152 zero = 0;
1153 r = IStream_Write( stm, &zero, sizeof zero, &count );
1154
1155 return S_OK;
1156 }
1157
1158 /************************************************************************
1159 * IPersistStream_GetSizeMax (IPersistStream)
1160 */
1161 static HRESULT WINAPI IPersistStream_fnGetSizeMax(
1162 IPersistStream* iface,
1163 ULARGE_INTEGER* pcbSize)
1164 {
1165 IShellLinkImpl *This = impl_from_IPersistStream(iface);
1166
1167 TRACE("(%p)\n", This);
1168
1169 return E_NOTIMPL;
1170 }
1171
1172 static const IPersistStreamVtbl psvt =
1173 {
1174 IPersistStream_fnQueryInterface,
1175 IPersistStream_fnAddRef,
1176 IPersistStream_fnRelease,
1177 IPersistStream_fnGetClassID,
1178 IPersistStream_fnIsDirty,
1179 IPersistStream_fnLoad,
1180 IPersistStream_fnSave,
1181 IPersistStream_fnGetSizeMax
1182 };
1183
1184 /**************************************************************************
1185 * IShellLink_Constructor
1186 */
1187 HRESULT WINAPI IShellLink_Constructor( IUnknown *pUnkOuter,
1188 REFIID riid, LPVOID *ppv )
1189 {
1190 IShellLinkImpl * sl;
1191 HRESULT r;
1192
1193 TRACE("unkOut=%p riid=%s\n",pUnkOuter, debugstr_guid(riid));
1194
1195 *ppv = NULL;
1196
1197 if (pUnkOuter)
1198 return CLASS_E_NOAGGREGATION;
1199 sl = LocalAlloc(LMEM_ZEROINIT,sizeof(IShellLinkImpl));
1200 if (!sl)
1201 return E_OUTOFMEMORY;
1202
1203 sl->ref = 1;
1204 sl->lpVtbl = &slvt;
1205 sl->lpvtblw = &slvtw;
1206 sl->lpvtblPersistFile = &pfvt;
1207 sl->lpvtblPersistStream = &psvt;
1208 sl->lpvtblShellLinkDataList = &dlvt;
1209 sl->lpvtblShellExtInit = &eivt;
1210 sl->lpvtblContextMenu = &cmvt;
1211 sl->lpvtblObjectWithSite = &owsvt;
1212 sl->lpvtblPropSheetExt = &pse;
1213 sl->iShowCmd = SW_SHOWNORMAL;
1214 sl->bDirty = FALSE;
1215 sl->iIdOpen = -1;
1216 sl->site = NULL;
1217 sl->bRunAs = FALSE;
1218
1219 TRACE("(%p)->()\n",sl);
1220
1221 r = ShellLink_QueryInterface( sl, riid, ppv );
1222 ShellLink_Release( sl );
1223 return r;
1224 }
1225
1226
1227 static BOOL SHELL_ExistsFileW(LPCWSTR path)
1228 {
1229 if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW(path))
1230 return FALSE;
1231 return TRUE;
1232 }
1233
1234 /**************************************************************************
1235 * ShellLink_UpdatePath
1236 * update absolute path in sPath using relative path in sPathRel
1237 */
1238 static HRESULT ShellLink_UpdatePath(LPCWSTR sPathRel, LPCWSTR path, LPCWSTR sWorkDir, LPWSTR* psPath)
1239 {
1240 if (!path || !psPath)
1241 return E_INVALIDARG;
1242
1243 if (!*psPath && sPathRel) {
1244 WCHAR buffer[2*MAX_PATH], abs_path[2*MAX_PATH];
1245 LPWSTR final = NULL;
1246
1247 /* first try if [directory of link file] + [relative path] finds an existing file */
1248
1249 GetFullPathNameW( path, MAX_PATH*2, buffer, &final );
1250 if( !final )
1251 final = buffer;
1252 wcscpy(final, sPathRel);
1253
1254 *abs_path = '\0';
1255
1256 if (SHELL_ExistsFileW(buffer)) {
1257 if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
1258 wcscpy(abs_path, buffer);
1259 } else {
1260 /* try if [working directory] + [relative path] finds an existing file */
1261 if (sWorkDir) {
1262 wcscpy(buffer, sWorkDir);
1263 wcscpy(PathAddBackslashW(buffer), sPathRel);
1264
1265 if (SHELL_ExistsFileW(buffer))
1266 if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
1267 wcscpy(abs_path, buffer);
1268 }
1269 }
1270
1271 /* FIXME: This is even not enough - not all shell links can be resolved using this algorithm. */
1272 if (!*abs_path)
1273 wcscpy(abs_path, sPathRel);
1274
1275 *psPath = HeapAlloc(GetProcessHeap(), 0, (wcslen(abs_path)+1)*sizeof(WCHAR));
1276 if (!*psPath)
1277 return E_OUTOFMEMORY;
1278
1279 wcscpy(*psPath, abs_path);
1280 }
1281
1282 return S_OK;
1283 }
1284
1285 /**************************************************************************
1286 * IShellLink_ConstructFromFile
1287 */
1288 HRESULT WINAPI IShellLink_ConstructFromFile( IUnknown* pUnkOuter, REFIID riid,
1289 LPCITEMIDLIST pidl, LPVOID* ppv)
1290 {
1291 IShellLinkW* psl;
1292
1293 HRESULT hr = IShellLink_Constructor(NULL, riid, (LPVOID*)&psl);
1294
1295 if (SUCCEEDED(hr)) {
1296 IPersistFile* ppf;
1297
1298 *ppv = NULL;
1299
1300 hr = IShellLinkW_QueryInterface(psl, &IID_IPersistFile, (LPVOID*)&ppf);
1301
1302 if (SUCCEEDED(hr)) {
1303 WCHAR path[MAX_PATH];
1304
1305 if (SHGetPathFromIDListW(pidl, path))
1306 hr = IPersistFile_Load(ppf, path, 0);
1307 else
1308 hr = E_FAIL;
1309
1310 if (SUCCEEDED(hr))
1311 *ppv = psl;
1312
1313 IPersistFile_Release(ppf);
1314 }
1315
1316 if (!*ppv)
1317 IShellLinkW_Release(psl);
1318 }
1319
1320 return hr;
1321 }
1322
1323 /**************************************************************************
1324 * IShellLinkA_QueryInterface
1325 */
1326 static HRESULT WINAPI IShellLinkA_fnQueryInterface( IShellLinkA * iface, REFIID riid, LPVOID *ppvObj)
1327 {
1328 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1329 return ShellLink_QueryInterface( This, riid, ppvObj );
1330 }
1331
1332 /******************************************************************************
1333 * IShellLinkA_AddRef
1334 */
1335 static ULONG WINAPI IShellLinkA_fnAddRef(IShellLinkA * iface)
1336 {
1337 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1338 return ShellLink_AddRef( This );
1339 }
1340
1341 /******************************************************************************
1342 * IShellLinkA_Release
1343 */
1344 static ULONG WINAPI IShellLinkA_fnRelease(IShellLinkA * iface)
1345 {
1346 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1347 return ShellLink_Release( This );
1348 }
1349
1350 static HRESULT WINAPI IShellLinkA_fnGetPath(IShellLinkA * iface, LPSTR pszFile,
1351 INT cchMaxPath, WIN32_FIND_DATAA *pfd, DWORD fFlags)
1352 {
1353 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1354
1355 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%u)(%s)\n",
1356 This, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(This->sPath));
1357
1358 if (This->sComponent || This->sProduct)
1359 return S_FALSE;
1360
1361 if (cchMaxPath)
1362 pszFile[0] = 0;
1363 if (This->sPath)
1364 WideCharToMultiByte( CP_ACP, 0, This->sPath, -1,
1365 pszFile, cchMaxPath, NULL, NULL);
1366
1367 if (pfd) FIXME("(%p): WIN32_FIND_DATA is not yet filled.\n", This);
1368
1369 return S_OK;
1370 }
1371
1372 static HRESULT WINAPI IShellLinkA_fnGetIDList(IShellLinkA * iface, LPITEMIDLIST * ppidl)
1373 {
1374 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1375
1376 TRACE("(%p)->(ppidl=%p)\n",This, ppidl);
1377
1378 return IShellLinkW_GetIDList((IShellLinkW*)&(This->lpvtblw), ppidl);
1379 }
1380
1381 static HRESULT WINAPI IShellLinkA_fnSetIDList(IShellLinkA * iface, LPCITEMIDLIST pidl)
1382 {
1383 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1384
1385 TRACE("(%p)->(pidl=%p)\n",This, pidl);
1386
1387 if (This->pPidl)
1388 ILFree(This->pPidl);
1389 This->pPidl = ILClone (pidl);
1390 This->bDirty = TRUE;
1391
1392 return S_OK;
1393 }
1394
1395 static HRESULT WINAPI IShellLinkA_fnGetDescription(IShellLinkA * iface, LPSTR pszName,INT cchMaxName)
1396 {
1397 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1398
1399 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1400
1401 if( cchMaxName )
1402 pszName[0] = 0;
1403 if( This->sDescription )
1404 WideCharToMultiByte( CP_ACP, 0, This->sDescription, -1,
1405 pszName, cchMaxName, NULL, NULL);
1406
1407 return S_OK;
1408 }
1409
1410 static HRESULT WINAPI IShellLinkA_fnSetDescription(IShellLinkA * iface, LPCSTR pszName)
1411 {
1412 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1413
1414 TRACE("(%p)->(pName=%s)\n", This, pszName);
1415
1416 HeapFree(GetProcessHeap(), 0, This->sDescription);
1417 This->sDescription = HEAP_strdupAtoW( GetProcessHeap(), 0, pszName);
1418 if ( !This->sDescription )
1419 return E_OUTOFMEMORY;
1420
1421 This->bDirty = TRUE;
1422
1423 return S_OK;
1424 }
1425
1426 static HRESULT WINAPI IShellLinkA_fnGetWorkingDirectory(IShellLinkA * iface, LPSTR pszDir,INT cchMaxPath)
1427 {
1428 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1429
1430 TRACE("(%p)->(%p len=%u)\n", This, pszDir, cchMaxPath);
1431
1432 if( cchMaxPath )
1433 pszDir[0] = 0;
1434 if( This->sWorkDir )
1435 WideCharToMultiByte( CP_ACP, 0, This->sWorkDir, -1,
1436 pszDir, cchMaxPath, NULL, NULL);
1437
1438 return S_OK;
1439 }
1440
1441 static HRESULT WINAPI IShellLinkA_fnSetWorkingDirectory(IShellLinkA * iface, LPCSTR pszDir)
1442 {
1443 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1444
1445 TRACE("(%p)->(dir=%s)\n",This, pszDir);
1446
1447 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1448 This->sWorkDir = HEAP_strdupAtoW( GetProcessHeap(), 0, pszDir);
1449 if ( !This->sWorkDir )
1450 return E_OUTOFMEMORY;
1451
1452 This->bDirty = TRUE;
1453
1454 return S_OK;
1455 }
1456
1457 static HRESULT WINAPI IShellLinkA_fnGetArguments(IShellLinkA * iface, LPSTR pszArgs,INT cchMaxPath)
1458 {
1459 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1460
1461 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1462
1463 if( cchMaxPath )
1464 pszArgs[0] = 0;
1465 if( This->sArgs )
1466 WideCharToMultiByte( CP_ACP, 0, This->sArgs, -1,
1467 pszArgs, cchMaxPath, NULL, NULL);
1468
1469 return S_OK;
1470 }
1471
1472 static HRESULT WINAPI IShellLinkA_fnSetArguments(IShellLinkA * iface, LPCSTR pszArgs)
1473 {
1474 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1475
1476 TRACE("(%p)->(args=%s)\n",This, pszArgs);
1477
1478 HeapFree(GetProcessHeap(), 0, This->sArgs);
1479 This->sArgs = HEAP_strdupAtoW( GetProcessHeap(), 0, pszArgs);
1480 if( !This->sArgs )
1481 return E_OUTOFMEMORY;
1482
1483 This->bDirty = TRUE;
1484
1485 return S_OK;
1486 }
1487
1488 static HRESULT WINAPI IShellLinkA_fnGetHotkey(IShellLinkA * iface, WORD *pwHotkey)
1489 {
1490 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1491
1492 TRACE("(%p)->(%p)(0x%08x)\n",This, pwHotkey, This->wHotKey);
1493
1494 *pwHotkey = This->wHotKey;
1495
1496 return S_OK;
1497 }
1498
1499 static HRESULT WINAPI IShellLinkA_fnSetHotkey(IShellLinkA * iface, WORD wHotkey)
1500 {
1501 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1502
1503 TRACE("(%p)->(hotkey=%x)\n",This, wHotkey);
1504
1505 This->wHotKey = wHotkey;
1506 This->bDirty = TRUE;
1507
1508 return S_OK;
1509 }
1510
1511 static HRESULT WINAPI IShellLinkA_fnGetShowCmd(IShellLinkA * iface, INT *piShowCmd)
1512 {
1513 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1514
1515 TRACE("(%p)->(%p)\n",This, piShowCmd);
1516 *piShowCmd = This->iShowCmd;
1517 return S_OK;
1518 }
1519
1520 static HRESULT WINAPI IShellLinkA_fnSetShowCmd(IShellLinkA * iface, INT iShowCmd)
1521 {
1522 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1523
1524 TRACE("(%p) %d\n",This, iShowCmd);
1525
1526 This->iShowCmd = iShowCmd;
1527 This->bDirty = TRUE;
1528
1529 return NOERROR;
1530 }
1531
1532 static HRESULT SHELL_PidlGeticonLocationA(IShellFolder* psf, LPCITEMIDLIST pidl,
1533 LPSTR pszIconPath, int cchIconPath, int* piIcon)
1534 {
1535 LPCITEMIDLIST pidlLast;
1536
1537 HRESULT hr = SHBindToParent(pidl, &IID_IShellFolder, (LPVOID*)&psf, &pidlLast);
1538
1539 if (SUCCEEDED(hr)) {
1540 IExtractIconA* pei;
1541
1542 hr = IShellFolder_GetUIObjectOf(psf, 0, 1, &pidlLast, &IID_IExtractIconA, NULL, (LPVOID*)&pei);
1543
1544 if (SUCCEEDED(hr)) {
1545 hr = IExtractIconA_GetIconLocation(pei, 0, pszIconPath, MAX_PATH, piIcon, NULL);
1546
1547 IExtractIconA_Release(pei);
1548 }
1549
1550 IShellFolder_Release(psf);
1551 }
1552
1553 return hr;
1554 }
1555
1556 static HRESULT WINAPI IShellLinkA_fnGetIconLocation(IShellLinkA * iface, LPSTR pszIconPath,INT cchIconPath,INT *piIcon)
1557 {
1558 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1559
1560 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1561
1562 pszIconPath[0] = 0;
1563 *piIcon = This->iIcoNdx;
1564
1565 if (This->sIcoPath)
1566 {
1567 WideCharToMultiByte(CP_ACP, 0, This->sIcoPath, -1, pszIconPath, cchIconPath, NULL, NULL);
1568 return S_OK;
1569 }
1570
1571 if (This->pPidl || This->sPath)
1572 {
1573 IShellFolder* pdsk;
1574
1575 HRESULT hr = SHGetDesktopFolder(&pdsk);
1576
1577 if (SUCCEEDED(hr))
1578 {
1579 /* first look for an icon using the PIDL (if present) */
1580 if (This->pPidl)
1581 hr = SHELL_PidlGeticonLocationA(pdsk, This->pPidl, pszIconPath, cchIconPath, piIcon);
1582 else
1583 hr = E_FAIL;
1584
1585 /* if we couldn't find an icon yet, look for it using the file system path */
1586 if (FAILED(hr) && This->sPath)
1587 {
1588 LPITEMIDLIST pidl;
1589
1590 hr = IShellFolder_ParseDisplayName(pdsk, 0, NULL, This->sPath, NULL, &pidl, NULL);
1591
1592 if (SUCCEEDED(hr)) {
1593 hr = SHELL_PidlGeticonLocationA(pdsk, pidl, pszIconPath, cchIconPath, piIcon);
1594
1595 SHFree(pidl);
1596 }
1597 }
1598
1599 IShellFolder_Release(pdsk);
1600 }
1601
1602 return hr;
1603 }
1604 return S_OK;
1605 }
1606
1607 static HRESULT WINAPI IShellLinkA_fnSetIconLocation(IShellLinkA * iface, LPCSTR pszIconPath,INT iIcon)
1608 {
1609 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1610
1611 TRACE("(%p)->(path=%s iicon=%u)\n",This, pszIconPath, iIcon);
1612
1613 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
1614 This->sIcoPath = HEAP_strdupAtoW(GetProcessHeap(), 0, pszIconPath);
1615 if ( !This->sIcoPath )
1616 return E_OUTOFMEMORY;
1617
1618 This->iIcoNdx = iIcon;
1619 This->bDirty = TRUE;
1620
1621 return S_OK;
1622 }
1623
1624 static HRESULT WINAPI IShellLinkA_fnSetRelativePath(IShellLinkA * iface, LPCSTR pszPathRel, DWORD dwReserved)
1625 {
1626 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1627
1628 TRACE("(%p)->(path=%s %x)\n",This, pszPathRel, dwReserved);
1629
1630 HeapFree(GetProcessHeap(), 0, This->sPathRel);
1631 This->sPathRel = HEAP_strdupAtoW(GetProcessHeap(), 0, pszPathRel);
1632 This->bDirty = TRUE;
1633
1634 return ShellLink_UpdatePath(This->sPathRel, This->sPath, This->sWorkDir, &This->sPath);
1635 }
1636
1637 static HRESULT WINAPI IShellLinkA_fnResolve(IShellLinkA * iface, HWND hwnd, DWORD fFlags)
1638 {
1639 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1640
1641 TRACE("(%p)->(hwnd=%p flags=%x)\n",This, hwnd, fFlags);
1642
1643 return IShellLinkW_Resolve( (IShellLinkW*)&(This->lpvtblw), hwnd, fFlags );
1644 }
1645
1646 static HRESULT WINAPI IShellLinkA_fnSetPath(IShellLinkA * iface, LPCSTR pszFile)
1647 {
1648 HRESULT r;
1649 LPWSTR str;
1650 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1651
1652 TRACE("(%p)->(path=%s)\n",This, pszFile);
1653
1654 if (!pszFile) return E_INVALIDARG;
1655
1656 str = HEAP_strdupAtoW(GetProcessHeap(), 0, pszFile);
1657 if( !str )
1658 return E_OUTOFMEMORY;
1659
1660 r = IShellLinkW_SetPath((IShellLinkW*)&(This->lpvtblw), str);
1661 HeapFree( GetProcessHeap(), 0, str );
1662
1663 return r;
1664 }
1665
1666 /**************************************************************************
1667 * IShellLink Implementation
1668 */
1669
1670 static const IShellLinkAVtbl slvt =
1671 {
1672 IShellLinkA_fnQueryInterface,
1673 IShellLinkA_fnAddRef,
1674 IShellLinkA_fnRelease,
1675 IShellLinkA_fnGetPath,
1676 IShellLinkA_fnGetIDList,
1677 IShellLinkA_fnSetIDList,
1678 IShellLinkA_fnGetDescription,
1679 IShellLinkA_fnSetDescription,
1680 IShellLinkA_fnGetWorkingDirectory,
1681 IShellLinkA_fnSetWorkingDirectory,
1682 IShellLinkA_fnGetArguments,
1683 IShellLinkA_fnSetArguments,
1684 IShellLinkA_fnGetHotkey,
1685 IShellLinkA_fnSetHotkey,
1686 IShellLinkA_fnGetShowCmd,
1687 IShellLinkA_fnSetShowCmd,
1688 IShellLinkA_fnGetIconLocation,
1689 IShellLinkA_fnSetIconLocation,
1690 IShellLinkA_fnSetRelativePath,
1691 IShellLinkA_fnResolve,
1692 IShellLinkA_fnSetPath
1693 };
1694
1695
1696 /**************************************************************************
1697 * IShellLinkW_fnQueryInterface
1698 */
1699 static HRESULT WINAPI IShellLinkW_fnQueryInterface(
1700 IShellLinkW * iface, REFIID riid, LPVOID *ppvObj)
1701 {
1702 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1703 return ShellLink_QueryInterface( This, riid, ppvObj );
1704 }
1705
1706 /******************************************************************************
1707 * IShellLinkW_fnAddRef
1708 */
1709 static ULONG WINAPI IShellLinkW_fnAddRef(IShellLinkW * iface)
1710 {
1711 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1712 return ShellLink_AddRef( This );
1713 }
1714
1715 /******************************************************************************
1716 * IShellLinkW_fnRelease
1717 */
1718 static ULONG WINAPI IShellLinkW_fnRelease(IShellLinkW * iface)
1719 {
1720 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1721 return ShellLink_Release( This );
1722 }
1723
1724 static HRESULT WINAPI IShellLinkW_fnGetPath(IShellLinkW * iface, LPWSTR pszFile,INT cchMaxPath, WIN32_FIND_DATAW *pfd, DWORD fFlags)
1725 {
1726 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1727
1728 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%u)(%s)\n",
1729 This, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(This->sPath));
1730
1731 if (This->sComponent || This->sProduct)
1732 return S_FALSE;
1733
1734 if (cchMaxPath)
1735 pszFile[0] = 0;
1736 if (This->sPath)
1737 lstrcpynW( pszFile, This->sPath, cchMaxPath );
1738
1739 if (pfd) FIXME("(%p): WIN32_FIND_DATA is not yet filled.\n", This);
1740
1741 return S_OK;
1742 }
1743
1744 static HRESULT WINAPI IShellLinkW_fnGetIDList(IShellLinkW * iface, LPITEMIDLIST * ppidl)
1745 {
1746 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1747
1748 TRACE("(%p)->(ppidl=%p)\n",This, ppidl);
1749
1750 if (!This->pPidl)
1751 {
1752 *ppidl = NULL;
1753 return S_FALSE;
1754 }
1755 *ppidl = ILClone(This->pPidl);
1756 return S_OK;
1757 }
1758
1759 static HRESULT WINAPI IShellLinkW_fnSetIDList(IShellLinkW * iface, LPCITEMIDLIST pidl)
1760 {
1761 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1762
1763 TRACE("(%p)->(pidl=%p)\n",This, pidl);
1764
1765 if( This->pPidl )
1766 ILFree( This->pPidl );
1767 This->pPidl = ILClone( pidl );
1768 if( !This->pPidl )
1769 return E_FAIL;
1770
1771 This->bDirty = TRUE;
1772
1773 return S_OK;
1774 }
1775
1776 static HRESULT WINAPI IShellLinkW_fnGetDescription(IShellLinkW * iface, LPWSTR pszName,INT cchMaxName)
1777 {
1778 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1779
1780 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1781
1782 pszName[0] = 0;
1783 if( This->sDescription )
1784 lstrcpynW( pszName, This->sDescription, cchMaxName );
1785
1786 return S_OK;
1787 }
1788
1789 static HRESULT WINAPI IShellLinkW_fnSetDescription(IShellLinkW * iface, LPCWSTR pszName)
1790 {
1791 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1792
1793 TRACE("(%p)->(desc=%s)\n",This, debugstr_w(pszName));
1794
1795 HeapFree(GetProcessHeap(), 0, This->sDescription);
1796 This->sDescription = HeapAlloc( GetProcessHeap(), 0,
1797 (wcslen( pszName )+1)*sizeof(WCHAR) );
1798 if ( !This->sDescription )
1799 return E_OUTOFMEMORY;
1800
1801 wcscpy( This->sDescription, pszName );
1802 This->bDirty = TRUE;
1803
1804 return S_OK;
1805 }
1806
1807 static HRESULT WINAPI IShellLinkW_fnGetWorkingDirectory(IShellLinkW * iface, LPWSTR pszDir,INT cchMaxPath)
1808 {
1809 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1810
1811 TRACE("(%p)->(%p len %u)\n", This, pszDir, cchMaxPath);
1812
1813 if( cchMaxPath )
1814 pszDir[0] = 0;
1815 if( This->sWorkDir )
1816 lstrcpynW( pszDir, This->sWorkDir, cchMaxPath );
1817
1818 return S_OK;
1819 }
1820
1821 static HRESULT WINAPI IShellLinkW_fnSetWorkingDirectory(IShellLinkW * iface, LPCWSTR pszDir)
1822 {
1823 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1824
1825 TRACE("(%p)->(dir=%s)\n",This, debugstr_w(pszDir));
1826
1827 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1828 This->sWorkDir = HeapAlloc( GetProcessHeap(), 0,
1829 (wcslen( pszDir )+1)*sizeof (WCHAR) );
1830 if ( !This->sWorkDir )
1831 return E_OUTOFMEMORY;
1832 wcscpy( This->sWorkDir, pszDir );
1833 This->bDirty = TRUE;
1834
1835 return S_OK;
1836 }
1837
1838 static HRESULT WINAPI IShellLinkW_fnGetArguments(IShellLinkW * iface, LPWSTR pszArgs,INT cchMaxPath)
1839 {
1840 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1841
1842 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1843
1844 if( cchMaxPath )
1845 pszArgs[0] = 0;
1846 if( This->sArgs )
1847 lstrcpynW( pszArgs, This->sArgs, cchMaxPath );
1848
1849 return NOERROR;
1850 }
1851
1852 static HRESULT WINAPI IShellLinkW_fnSetArguments(IShellLinkW * iface, LPCWSTR pszArgs)
1853 {
1854 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1855
1856 TRACE("(%p)->(args=%s)\n",This, debugstr_w(pszArgs));
1857
1858 HeapFree(GetProcessHeap(), 0, This->sArgs);
1859 This->sArgs = HeapAlloc( GetProcessHeap(), 0,
1860 (wcslen( pszArgs )+1)*sizeof (WCHAR) );
1861 if ( !This->sArgs )
1862 return E_OUTOFMEMORY;
1863 wcscpy( This->sArgs, pszArgs );
1864 This->bDirty = TRUE;
1865
1866 return S_OK;
1867 }
1868
1869 static HRESULT WINAPI IShellLinkW_fnGetHotkey(IShellLinkW * iface, WORD *pwHotkey)
1870 {
1871 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1872
1873 TRACE("(%p)->(%p)\n",This, pwHotkey);
1874
1875 *pwHotkey=This->wHotKey;
1876
1877 return S_OK;
1878 }
1879
1880 static HRESULT WINAPI IShellLinkW_fnSetHotkey(IShellLinkW * iface, WORD wHotkey)
1881 {
1882 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1883
1884 TRACE("(%p)->(hotkey=%x)\n",This, wHotkey);
1885
1886 This->wHotKey = wHotkey;
1887 This->bDirty = TRUE;
1888
1889 return S_OK;
1890 }
1891
1892 static HRESULT WINAPI IShellLinkW_fnGetShowCmd(IShellLinkW * iface, INT *piShowCmd)
1893 {
1894 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1895
1896 TRACE("(%p)->(%p)\n",This, piShowCmd);
1897
1898 *piShowCmd = This->iShowCmd;
1899
1900 return S_OK;
1901 }
1902
1903 static HRESULT WINAPI IShellLinkW_fnSetShowCmd(IShellLinkW * iface, INT iShowCmd)
1904 {
1905 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1906
1907 This->iShowCmd = iShowCmd;
1908 This->bDirty = TRUE;
1909
1910 return S_OK;
1911 }
1912
1913 static HRESULT SHELL_PidlGeticonLocationW(IShellFolder* psf, LPCITEMIDLIST pidl,
1914 LPWSTR pszIconPath, int cchIconPath, int* piIcon)
1915 {
1916 LPCITEMIDLIST pidlLast;
1917 UINT wFlags;
1918
1919 HRESULT hr = SHBindToParent(pidl, &IID_IShellFolder, (LPVOID*)&psf, &pidlLast);
1920
1921 if (SUCCEEDED(hr)) {
1922 IExtractIconW* pei;
1923
1924 hr = IShellFolder_GetUIObjectOf(psf, 0, 1, &pidlLast, &IID_IExtractIconW, NULL, (LPVOID*)&pei);
1925
1926 if (SUCCEEDED(hr)) {
1927 hr = IExtractIconW_GetIconLocation(pei, 0, pszIconPath, MAX_PATH, piIcon, &wFlags);
1928
1929 IExtractIconW_Release(pei);
1930 }
1931
1932 IShellFolder_Release(psf);
1933 }
1934
1935 return hr;
1936 }
1937
1938 static HRESULT WINAPI IShellLinkW_fnGetIconLocation(IShellLinkW * iface, LPWSTR pszIconPath,INT cchIconPath,INT *piIcon)
1939 {
1940 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1941
1942 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1943
1944 pszIconPath[0] = 0;
1945 *piIcon = This->iIcoNdx;
1946
1947 if (This->sIcoPath)
1948 {
1949 lstrcpynW(pszIconPath, This->sIcoPath, cchIconPath);
1950 return S_OK;
1951 }
1952
1953 if (This->pPidl || This->sPath)
1954 {
1955 IShellFolder* pdsk;
1956
1957 HRESULT hr = SHGetDesktopFolder(&pdsk);
1958
1959 if (SUCCEEDED(hr))
1960 {
1961 /* first look for an icon using the PIDL (if present) */
1962 if (This->pPidl)
1963 hr = SHELL_PidlGeticonLocationW(pdsk, This->pPidl, pszIconPath, cchIconPath, piIcon);
1964 else
1965 hr = E_FAIL;
1966
1967 /* if we couldn't find an icon yet, look for it using the file system path */
1968 if (FAILED(hr) && This->sPath)
1969 {
1970 LPITEMIDLIST pidl;
1971
1972 hr = IShellFolder_ParseDisplayName(pdsk, 0, NULL, This->sPath, NULL, &pidl, NULL);
1973
1974 if (SUCCEEDED(hr))
1975 {
1976 hr = SHELL_PidlGeticonLocationW(pdsk, pidl, pszIconPath, cchIconPath, piIcon);
1977
1978 SHFree(pidl);
1979 }
1980 }
1981
1982 IShellFolder_Release(pdsk);
1983 }
1984 return hr;
1985 }
1986 return S_OK;
1987 }
1988
1989 static HRESULT WINAPI IShellLinkW_fnSetIconLocation(IShellLinkW * iface, LPCWSTR pszIconPath,INT iIcon)
1990 {
1991 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1992
1993 TRACE("(%p)->(path=%s iicon=%u)\n",This, debugstr_w(pszIconPath), iIcon);
1994
1995 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
1996 This->sIcoPath = HeapAlloc( GetProcessHeap(), 0,
1997 (wcslen( pszIconPath )+1)*sizeof (WCHAR) );
1998 if ( !This->sIcoPath )
1999 return E_OUTOFMEMORY;
2000 wcscpy( This->sIcoPath, pszIconPath );
2001
2002 This->iIcoNdx = iIcon;
2003 This->bDirty = TRUE;
2004
2005 return S_OK;
2006 }
2007
2008 static HRESULT WINAPI IShellLinkW_fnSetRelativePath(IShellLinkW * iface, LPCWSTR pszPathRel, DWORD dwReserved)
2009 {
2010 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
2011
2012 TRACE("(%p)->(path=%s %x)\n",This, debugstr_w(pszPathRel), dwReserved);
2013
2014 HeapFree(GetProcessHeap(), 0, This->sPathRel);
2015 This->sPathRel = HeapAlloc( GetProcessHeap(), 0,
2016 (wcslen( pszPathRel )+1) * sizeof (WCHAR) );
2017 if ( !This->sPathRel )
2018 return E_OUTOFMEMORY;
2019 wcscpy( This->sPathRel, pszPathRel );
2020 This->bDirty = TRUE;
2021
2022 return ShellLink_UpdatePath(This->sPathRel, This->sPath, This->sWorkDir, &This->sPath);
2023 }
2024
2025 static HRESULT WINAPI IShellLinkW_fnResolve(IShellLinkW * iface, HWND hwnd, DWORD fFlags)
2026 {
2027 HRESULT hr = S_OK;
2028 BOOL bSuccess;
2029
2030 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
2031
2032 TRACE("(%p)->(hwnd=%p flags=%x)\n",This, hwnd, fFlags);
2033
2034 /*FIXME: use IResolveShellLink interface */
2035
2036 if (!This->sPath && This->pPidl) {
2037 WCHAR buffer[MAX_PATH];
2038
2039 bSuccess = SHGetPathFromIDListW(This->pPidl, buffer);
2040
2041 if (bSuccess && *buffer) {
2042 This->sPath = HeapAlloc(GetProcessHeap(), 0, (wcslen(buffer)+1)*sizeof(WCHAR));
2043 if (!This->sPath)
2044 return E_OUTOFMEMORY;
2045
2046 wcscpy(This->sPath, buffer);
2047
2048 This->bDirty = TRUE;
2049 } else
2050 hr = S_OK; /* don't report an error occurred while just caching information */
2051 }
2052
2053 if (!This->sIcoPath && This->sPath) {
2054 This->sIcoPath = HeapAlloc(GetProcessHeap(), 0, (wcslen(This->sPath)+1)*sizeof(WCHAR));
2055 if (!This->sIcoPath)
2056 return E_OUTOFMEMORY;
2057
2058 wcscpy(This->sIcoPath, This->sPath);
2059 This->iIcoNdx = 0;
2060
2061 This->bDirty = TRUE;
2062 }
2063
2064 return hr;
2065 }
2066
2067 static LPWSTR ShellLink_GetAdvertisedArg(LPCWSTR str)
2068 {
2069 LPWSTR ret;
2070 LPCWSTR p;
2071 DWORD len;
2072
2073 if( !str )
2074 return NULL;
2075
2076 p = wcschr( str, ':' );
2077 if( !p )
2078 return NULL;
2079 len = p - str;
2080 ret = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR)*(len+1));
2081 if( !ret )
2082 return ret;
2083 memcpy( ret, str, sizeof(WCHAR)*len );
2084 ret[len] = 0;
2085 return ret;
2086 }
2087
2088 static HRESULT ShellLink_SetAdvertiseInfo(IShellLinkImpl *This, LPCWSTR str)
2089 {
2090 LPCWSTR szComponent = NULL, szProduct = NULL, p;
2091 WCHAR szGuid[39];
2092 HRESULT r;
2093 GUID guid;
2094 int len;
2095
2096 while( str[0] )
2097 {
2098 /* each segment must start with two colons */
2099 if( str[0] != ':' || str[1] != ':' )
2100 return E_FAIL;
2101
2102 /* the last segment is just two colons */
2103 if( !str[2] )
2104 break;
2105 str += 2;
2106
2107 /* there must be a colon straight after a guid */
2108 p = wcschr( str, ':' );
2109 if( !p )
2110 return E_FAIL;
2111 len = p - str;
2112 if( len != 38 )
2113 return E_FAIL;
2114
2115 /* get the guid, and check it's validly formatted */
2116 memcpy( szGuid, str, sizeof(WCHAR)*len );
2117 szGuid[len] = 0;
2118 r = CLSIDFromString( szGuid, &guid );
2119 if( r != S_OK )
2120 return r;
2121 str = p + 1;
2122
2123 /* match it up to a guid that we care about */
2124 if( IsEqualGUID( &guid, &SHELL32_AdvtShortcutComponent ) && !szComponent )
2125 szComponent = str;
2126 else if( IsEqualGUID( &guid, &SHELL32_AdvtShortcutProduct ) && !szProduct )
2127 szProduct = str;
2128 else
2129 return E_FAIL;
2130
2131 /* skip to the next field */
2132 str = wcschr( str, ':' );
2133 if( !str )
2134 return E_FAIL;
2135 }
2136
2137 /* we have to have a component for an advertised shortcut */
2138 if( !szComponent )
2139 return E_FAIL;
2140
2141 This->sComponent = ShellLink_GetAdvertisedArg( szComponent );
2142 This->sProduct = ShellLink_GetAdvertisedArg( szProduct );
2143
2144 TRACE("Component = %s\n", debugstr_w(This->sComponent));
2145 TRACE("Product = %s\n", debugstr_w(This->sProduct));
2146
2147 return S_OK;
2148 }
2149
2150 static BOOL ShellLink_GetVolumeInfo(LPCWSTR path, volume_info *volume)
2151 {
2152 const int label_sz = sizeof volume->label/sizeof volume->label[0];
2153 WCHAR drive[4] = { path[0], ':', '\\', 0 };
2154 BOOL r;
2155
2156 volume->type = GetDriveTypeW(drive);
2157 r = GetVolumeInformationW(drive, volume->label, label_sz,
2158 &volume->serial, NULL, NULL, NULL, 0);
2159 TRACE("r = %d type %d serial %08x name %s\n", r,
2160 volume->type, volume->serial, debugstr_w(volume->label));
2161 return r;
2162 }
2163
2164 static HRESULT WINAPI IShellLinkW_fnSetPath(IShellLinkW * iface, LPCWSTR pszFile)
2165 {
2166 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
2167 WCHAR buffer[MAX_PATH];
2168 LPWSTR fname, unquoted = NULL;
2169 HRESULT hr = S_OK;
2170 UINT len;
2171
2172 TRACE("(%p)->(path=%s)\n",This, debugstr_w(pszFile));
2173
2174 if (!pszFile) return E_INVALIDARG;
2175
2176 /* quotes at the ends of the string are stripped */
2177 len = wcslen(pszFile);
2178 if (pszFile[0] == '"' && pszFile[len-1] == '"')
2179 {
2180 unquoted = strdupW(pszFile);
2181 PathUnquoteSpacesW(unquoted);
2182 pszFile = unquoted;
2183 }
2184
2185 /* any other quote marks are invalid */
2186 if (wcschr(pszFile, '"'))
2187 {
2188 HeapFree(GetProcessHeap(), 0, unquoted);
2189 return S_FALSE;
2190 }
2191
2192 HeapFree(GetProcessHeap(), 0, This->sPath);
2193 This->sPath = NULL;
2194
2195 HeapFree(GetProcessHeap(), 0, This->sComponent);
2196 This->sComponent = NULL;
2197
2198 if (This->pPidl)
2199 ILFree(This->pPidl);
2200 This->pPidl = NULL;
2201
2202 if (S_OK != ShellLink_SetAdvertiseInfo( This, pszFile ))
2203 {
2204 if (*pszFile == '\0')
2205 *buffer = '\0';
2206 else if (!GetFullPathNameW(pszFile, MAX_PATH, buffer, &fname))
2207 return E_FAIL;
2208 else if(!PathFileExistsW(buffer) &&
2209 !SearchPathW(NULL, pszFile, NULL, MAX_PATH, buffer, NULL))
2210 hr = S_FALSE;
2211
2212 This->pPidl = SHSimpleIDListFromPathW(pszFile);
2213 ShellLink_GetVolumeInfo(buffer, &This->volume);
2214
2215 This->sPath = HeapAlloc( GetProcessHeap(), 0,
2216 (wcslen( buffer )+1) * sizeof (WCHAR) );
2217 if (!This->sPath)
2218 return E_OUTOFMEMORY;
2219
2220 wcscpy(This->sPath, buffer);
2221 }
2222 This->bDirty = TRUE;
2223 HeapFree(GetProcessHeap(), 0, unquoted);
2224
2225 return hr;
2226 }
2227
2228 /**************************************************************************
2229 * IShellLinkW Implementation
2230 */
2231
2232 static const IShellLinkWVtbl slvtw =
2233 {
2234 IShellLinkW_fnQueryInterface,
2235 IShellLinkW_fnAddRef,
2236 IShellLinkW_fnRelease,
2237 IShellLinkW_fnGetPath,
2238 IShellLinkW_fnGetIDList,
2239 IShellLinkW_fnSetIDList,
2240 IShellLinkW_fnGetDescription,
2241 IShellLinkW_fnSetDescription,
2242 IShellLinkW_fnGetWorkingDirectory,
2243 IShellLinkW_fnSetWorkingDirectory,
2244 IShellLinkW_fnGetArguments,
2245 IShellLinkW_fnSetArguments,
2246 IShellLinkW_fnGetHotkey,
2247 IShellLinkW_fnSetHotkey,
2248 IShellLinkW_fnGetShowCmd,
2249 IShellLinkW_fnSetShowCmd,
2250 IShellLinkW_fnGetIconLocation,
2251 IShellLinkW_fnSetIconLocation,
2252 IShellLinkW_fnSetRelativePath,
2253 IShellLinkW_fnResolve,
2254 IShellLinkW_fnSetPath
2255 };
2256
2257 static HRESULT WINAPI
2258 ShellLink_DataList_QueryInterface( IShellLinkDataList* iface, REFIID riid, void** ppvObject)
2259 {
2260 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2261 return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvObject);
2262 }
2263
2264 static ULONG WINAPI
2265 ShellLink_DataList_AddRef( IShellLinkDataList* iface )
2266 {
2267 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2268 return IShellLinkA_AddRef((IShellLinkA*)This);
2269 }
2270
2271 static ULONG WINAPI
2272 ShellLink_DataList_Release( IShellLinkDataList* iface )
2273 {
2274 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2275 return ShellLink_Release( This );
2276 }
2277
2278 static HRESULT WINAPI
2279 ShellLink_AddDataBlock( IShellLinkDataList* iface, void* pDataBlock )
2280 {
2281 FIXME("\n");
2282 return E_NOTIMPL;
2283 }
2284
2285 static HRESULT WINAPI
2286 ShellLink_CopyDataBlock( IShellLinkDataList* iface, DWORD dwSig, void** ppDataBlock )
2287 {
2288 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2289 LPVOID block = NULL;
2290 HRESULT r = E_FAIL;
2291
2292 TRACE("%p %08x %p\n", iface, dwSig, ppDataBlock );
2293
2294 switch (dwSig)
2295 {
2296 case EXP_DARWIN_ID_SIG:
2297 if (!This->sComponent)
2298 break;
2299 block = shelllink_build_darwinid( This->sComponent, dwSig );
2300 r = S_OK;
2301 break;
2302 case EXP_SZ_LINK_SIG:
2303 case NT_CONSOLE_PROPS_SIG:
2304 case NT_FE_CONSOLE_PROPS_SIG:
2305 case EXP_SPECIAL_FOLDER_SIG:
2306 case EXP_SZ_ICON_SIG:
2307 FIXME("valid but unhandled datablock %08x\n", dwSig);
2308 break;
2309 default:
2310 ERR("unknown datablock %08x\n", dwSig);
2311 }
2312 *ppDataBlock = block;
2313 return r;
2314 }
2315
2316 static HRESULT WINAPI
2317 ShellLink_RemoveDataBlock( IShellLinkDataList* iface, DWORD dwSig )
2318 {
2319 FIXME("\n");
2320 return E_NOTIMPL;
2321 }
2322
2323 static HRESULT WINAPI
2324 ShellLink_GetFlags( IShellLinkDataList* iface, DWORD* pdwFlags )
2325 {
2326 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2327 DWORD flags = 0;
2328
2329 FIXME("%p %p\n", This, pdwFlags );
2330
2331 /* FIXME: add more */
2332 if (This->sArgs)
2333 flags |= SLDF_HAS_ARGS;
2334 if (This->sComponent)
2335 flags |= SLDF_HAS_DARWINID;
2336 if (This->sIcoPath)
2337 flags |= SLDF_HAS_ICONLOCATION;
2338 #if (NTDDI_VERSION < NTDDI_LONGHORN)
2339 if (This->sProduct)
2340 flags |= SLDF_HAS_LOGO3ID;
2341 #endif
2342 if (This->pPidl)
2343 flags |= SLDF_HAS_ID_LIST;
2344
2345 *pdwFlags = flags;
2346
2347 return S_OK;
2348 }
2349
2350 static HRESULT WINAPI
2351 ShellLink_SetFlags( IShellLinkDataList* iface, DWORD dwFlags )
2352 {
2353 FIXME("\n");
2354 return E_NOTIMPL;
2355 }
2356
2357 static const IShellLinkDataListVtbl dlvt =
2358 {
2359 ShellLink_DataList_QueryInterface,
2360 ShellLink_DataList_AddRef,
2361 ShellLink_DataList_Release,
2362 ShellLink_AddDataBlock,
2363 ShellLink_CopyDataBlock,
2364 ShellLink_RemoveDataBlock,
2365 ShellLink_GetFlags,
2366 ShellLink_SetFlags
2367 };
2368
2369 static HRESULT WINAPI
2370 ShellLink_ExtInit_QueryInterface( IShellExtInit* iface, REFIID riid, void** ppvObject )
2371 {
2372 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2373 return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvObject);
2374 }
2375
2376 static ULONG WINAPI
2377 ShellLink_ExtInit_AddRef( IShellExtInit* iface )
2378 {
2379 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2380 return IShellLinkA_AddRef((IShellLinkA*)This);
2381 }
2382
2383 static ULONG WINAPI
2384 ShellLink_ExtInit_Release( IShellExtInit* iface )
2385 {
2386 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2387 return ShellLink_Release( This );
2388 }
2389
2390 /**************************************************************************
2391 * ShellLink implementation of IShellExtInit::Initialize()
2392 *
2393 * Loads the shelllink from the dataobject the shell is pointing to.
2394 */
2395 static HRESULT WINAPI
2396 ShellLink_ExtInit_Initialize( IShellExtInit* iface, LPCITEMIDLIST pidlFolder,
2397 IDataObject *pdtobj, HKEY hkeyProgID )
2398 {
2399 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2400 FORMATETC format;
2401 STGMEDIUM stgm;
2402 UINT count;
2403 HRESULT r = E_FAIL;
2404
2405 TRACE("%p %p %p %p\n", This, pidlFolder, pdtobj, hkeyProgID );
2406
2407 if( !pdtobj )
2408 return r;
2409
2410 format.cfFormat = CF_HDROP;
2411 format.ptd = NULL;
2412 format.dwAspect = DVASPECT_CONTENT;
2413 format.lindex = -1;
2414 format.tymed = TYMED_HGLOBAL;
2415
2416 if( FAILED( IDataObject_GetData( pdtobj, &format, &stgm ) ) )
2417 return r;
2418
2419 count = DragQueryFileW( stgm.u.hGlobal, -1, NULL, 0 );
2420 if( count == 1 )
2421 {
2422 LPWSTR path;
2423
2424 count = DragQueryFileW( stgm.u.hGlobal, 0, NULL, 0 );
2425 count++;
2426 path = HeapAlloc( GetProcessHeap(), 0, count*sizeof(WCHAR) );
2427 if( path )
2428 {
2429 IPersistFile *pf = (IPersistFile*) &This->lpvtblPersistFile;
2430
2431 count = DragQueryFileW( stgm.u.hGlobal, 0, path, count );
2432 r = IPersistFile_Load( pf, path, 0 );
2433 HeapFree( GetProcessHeap(), 0, path );
2434 }
2435 }
2436 ReleaseStgMedium( &stgm );
2437
2438 return r;
2439 }
2440
2441 static const IShellExtInitVtbl eivt =
2442 {
2443 ShellLink_ExtInit_QueryInterface,
2444 ShellLink_ExtInit_AddRef,
2445 ShellLink_ExtInit_Release,
2446 ShellLink_ExtInit_Initialize
2447 };
2448
2449 static HRESULT WINAPI
2450 ShellLink_ContextMenu_QueryInterface( IContextMenu* iface, REFIID riid, void** ppvObject )
2451 {
2452 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2453 return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvObject);
2454 }
2455
2456 static ULONG WINAPI
2457 ShellLink_ContextMenu_AddRef( IContextMenu* iface )
2458 {
2459 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2460 return IShellLinkA_AddRef((IShellLinkA*)This);
2461 }
2462
2463 static ULONG WINAPI
2464 ShellLink_ContextMenu_Release( IContextMenu* iface )
2465 {
2466 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2467 return ShellLink_Release( This );
2468 }
2469
2470 static HRESULT WINAPI
2471 ShellLink_QueryContextMenu( IContextMenu* iface, HMENU hmenu, UINT indexMenu,
2472 UINT idCmdFirst, UINT idCmdLast, UINT uFlags )
2473 {
2474 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2475 WCHAR szOpen[20];
2476 MENUITEMINFOW mii;
2477 int id = 1;
2478
2479 TRACE("%p %p %u %u %u %u\n", This,
2480 hmenu, indexMenu, idCmdFirst, idCmdLast, uFlags );
2481
2482 if ( !hmenu )
2483 return E_INVALIDARG;
2484
2485 if (!LoadStringW(shell32_hInstance, IDS_OPEN_VERB, szOpen, sizeof(szOpen)/sizeof(WCHAR)))
2486 szOpen[0] = L'\0';
2487 else
2488 szOpen[(sizeof(szOpen)/sizeof(WCHAR))-1] = L'\0';
2489
2490 memset( &mii, 0, sizeof(mii) );
2491 mii.cbSize = sizeof (mii);
2492 mii.fMask = MIIM_TYPE | MIIM_ID | MIIM_STATE;
2493 mii.dwTypeData = (LPWSTR)szOpen;
2494 mii.cch = wcslen( mii.dwTypeData );
2495 mii.wID = idCmdFirst + id++;
2496 mii.fState = MFS_DEFAULT | MFS_ENABLED;
2497 mii.fType = MFT_STRING;
2498 if (!InsertMenuItemW( hmenu, indexMenu, TRUE, &mii ))
2499 return E_FAIL;
2500 This->iIdOpen = 1;
2501
2502 return MAKE_HRESULT( SEVERITY_SUCCESS, 0, id );
2503 }
2504
2505 static LPWSTR
2506 shelllink_get_msi_component_path( LPWSTR component )
2507 {
2508 LPWSTR path;
2509 DWORD r, sz = 0;
2510
2511 r = CommandLineFromMsiDescriptor( component, NULL, &sz );
2512 if (r != ERROR_SUCCESS)
2513 return NULL;
2514
2515 sz++;
2516 path = HeapAlloc( GetProcessHeap(), 0, sz*sizeof(WCHAR) );
2517 r = CommandLineFromMsiDescriptor( component, path, &sz );
2518 if (r != ERROR_SUCCESS)
2519 {
2520 HeapFree( GetProcessHeap(), 0, path );
2521 path = NULL;
2522 }
2523
2524 TRACE("returning %s\n", debugstr_w( path ) );
2525
2526 return path;
2527 }
2528
2529 INT_PTR CALLBACK ExtendedShortcutProc(
2530 HWND hwndDlg,
2531 UINT uMsg,
2532 WPARAM wParam,
2533 LPARAM lParam
2534 )
2535 {
2536 HWND hDlgCtrl;
2537
2538 switch(uMsg)
2539 {
2540 case WM_INITDIALOG:
2541 if (lParam)
2542 {
2543 hDlgCtrl = GetDlgItem(hwndDlg, 14000);
2544 SendMessage(hDlgCtrl, BM_SETCHECK, BST_CHECKED, 0);
2545 }
2546 return TRUE;
2547 case WM_COMMAND:
2548 hDlgCtrl = GetDlgItem(hwndDlg, 14000);
2549 if (LOWORD(wParam) == IDOK)
2550 {
2551 if ( SendMessage(hDlgCtrl, BM_GETCHECK, 0, 0) == BST_CHECKED )
2552 EndDialog(hwndDlg, 1);
2553 else
2554 EndDialog(hwndDlg, 0);
2555 }
2556 else if (LOWORD(wParam) == IDCANCEL)
2557 {
2558 EndDialog(hwndDlg, -1);
2559 }
2560 else if (LOWORD(wParam) == 14000)
2561 {
2562 if ( SendMessage(hDlgCtrl, BM_GETCHECK, 0, 0) == BST_CHECKED)
2563 SendMessage(hDlgCtrl, BM_SETCHECK, BST_UNCHECKED, 0);
2564 else
2565 SendMessage(hDlgCtrl, BM_SETCHECK, BST_CHECKED, 0);
2566
2567 }
2568 }
2569 return FALSE;
2570 }
2571
2572 /**************************************************************************
2573 * SH_ShellLinkDlgProc
2574 *
2575 * dialog proc of the shortcut property dialog
2576 */
2577
2578 INT_PTR
2579 CALLBACK
2580 SH_ShellLinkDlgProc(
2581 HWND hwndDlg,
2582 UINT uMsg,
2583 WPARAM wParam,
2584 LPARAM lParam
2585 )
2586 {
2587 LPPROPSHEETPAGEW ppsp;
2588 LPPSHNOTIFY lppsn;
2589 IShellLinkImpl *This;
2590 HWND hDlgCtrl;
2591 WCHAR szBuffer[MAX_PATH];
2592 WCHAR * ptr;
2593 int IconIndex;
2594 INT_PTR result;
2595
2596 This = (IShellLinkImpl *)GetWindowLongPtr(hwndDlg, DWLP_USER);
2597
2598 switch(uMsg)
2599 {
2600 case WM_INITDIALOG:
2601 ppsp = (LPPROPSHEETPAGEW)lParam;
2602 if (ppsp == NULL)
2603 break;
2604
2605 TRACE("ShellLink_DlgProc (WM_INITDIALOG hwnd %p lParam %p ppsplParam %x)\n",hwndDlg, lParam, ppsp->lParam);
2606
2607 This = (IShellLinkImpl *)ppsp->lParam;
2608 SetWindowLongPtr(hwndDlg, DWLP_USER, (LONG_PTR)This);
2609
2610 TRACE("sArgs: %S sComponent: %S sDescription: %S sIcoPath: %S sPath: %S sPathRel: %S sProduct: %S sWorkDir: %S\n", This->sArgs, This->sComponent ,This->sDescription,
2611 This->sIcoPath, This->sPath, This->sPathRel, This->sProduct, This->sWorkDir);
2612
2613 /* target path */
2614 hDlgCtrl = GetDlgItem( hwndDlg, 14009 );
2615 if ( hDlgCtrl != NULL )
2616 SendMessageW( hDlgCtrl, WM_SETTEXT, (WPARAM)NULL, (LPARAM)This->sPath );
2617
2618 /* working dir */
2619 hDlgCtrl = GetDlgItem( hwndDlg, 14011 );
2620 if ( hDlgCtrl != NULL )
2621 SendMessageW( hDlgCtrl, WM_SETTEXT, (WPARAM)NULL, (LPARAM)This->sWorkDir );
2622
2623 /* description */
2624 hDlgCtrl = GetDlgItem( hwndDlg, 14019 );
2625 if ( hDlgCtrl != NULL )
2626 SendMessageW( hDlgCtrl, WM_SETTEXT, (WPARAM)NULL, (LPARAM)This->sDescription );
2627 return TRUE;
2628 case WM_NOTIFY:
2629 lppsn = (LPPSHNOTIFY) lParam;
2630 if ( lppsn->hdr.code == PSN_APPLY )
2631 {
2632 /* set working directory */
2633 hDlgCtrl = GetDlgItem( hwndDlg, 14011 );
2634 SendMessageW( hDlgCtrl, WM_GETTEXT, (WPARAM)MAX_PATH, (LPARAM)szBuffer );
2635 IShellLinkW_fnSetWorkingDirectory((IShellLinkW*)&This->lpvtblw, szBuffer);
2636 /* set link destination */
2637 hDlgCtrl = GetDlgItem( hwndDlg, 14009 );
2638 SendMessageW( hDlgCtrl, WM_GETTEXT, (WPARAM)MAX_PATH, (LPARAM)szBuffer);
2639 if ( !SHELL_ExistsFileW(szBuffer) )
2640 {
2641 //FIXME load localized error msg
2642 MessageBoxW( hwndDlg, L"file not existing", szBuffer, MB_OK );
2643 SetWindowLongPtr( hwndDlg, DWL_MSGRESULT, PSNRET_INVALID_NOCHANGEPAGE );
2644 return TRUE;
2645 }
2646 ptr = wcsrchr(szBuffer, L'.');
2647 if (ptr && !_wcsnicmp(ptr, L".lnk", 4))
2648 {
2649 // FIXME load localized error msg
2650 MessageBoxW( hwndDlg, L"You cannot create a link to a shortcut", L"Error", MB_ICONERROR );
2651 SetWindowLongPtr( hwndDlg, DWL_MSGRESULT, PSNRET_INVALID_NOCHANGEPAGE );
2652 return TRUE;
2653 }
2654
2655 IShellLinkW_fnSetPath((IShellLinkW*)&This->lpvtblw, szBuffer);
2656
2657 TRACE("This %p sLinkPath %S\n", This, This->sLinkPath);
2658 IPersistFile_fnSave( (IPersistFile*)&This->lpvtblPersistFile, This->sLinkPath, TRUE );
2659 SetWindowLongPtr( hwndDlg, DWL_MSGRESULT, PSNRET_NOERROR );
2660 return TRUE;
2661 }
2662 break;
2663 case WM_COMMAND:
2664 switch(LOWORD(wParam))
2665 {
2666 case 14020:
2667 ///
2668 /// FIXME
2669 /// open target directory
2670 ///
2671 return TRUE;
2672 case 14021:
2673 if (This->sIcoPath)
2674 wcscpy(szBuffer, This->sIcoPath);
2675 IconIndex = This->iIcoNdx;
2676 if (PickIconDlg(hwndDlg, szBuffer, MAX_PATH, &IconIndex))
2677 {
2678 IShellLinkW_fnSetIconLocation((IShellLinkW*)&This->lpvtblw, szBuffer, IconIndex);
2679 ///
2680 /// FIXME redraw icon
2681 }
2682 return TRUE;
2683 case 14022:
2684 result = DialogBoxParamW(shell32_hInstance, MAKEINTRESOURCEW(SHELL_EXTENDED_SHORTCUT_DLG), hwndDlg, ExtendedShortcutProc, (LPARAM)This->bRunAs);
2685 if (result == 1 || result == 0)
2686 {
2687 if ( This->bRunAs != result )
2688 {
2689 PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
2690 }
2691
2692 This->bRunAs = result;
2693 }
2694 return TRUE;
2695 }
2696 switch(HIWORD(wParam))
2697 {
2698 case EN_CHANGE:
2699 PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
2700 break;
2701 }
2702 break;
2703 default:
2704 break;
2705 }
2706 return FALSE;
2707 }
2708
2709 /**************************************************************************
2710 * ShellLink_IShellPropSheetExt interface
2711 */
2712
2713 static HRESULT WINAPI
2714 ShellLink_IShellPropSheetExt_QueryInterface( IShellPropSheetExt* iface, REFIID riid, void** ppvObject )
2715 {
2716 IShellLinkImpl *This = impl_from_IShellPropSheetExt(iface);
2717 return ShellLink_QueryInterface( This, riid, ppvObject );
2718 }
2719
2720 static ULONG WINAPI
2721 ShellLink_IShellPropSheetExt_AddRef( IShellPropSheetExt* iface )
2722 {
2723 IShellLinkImpl *This = impl_from_IShellPropSheetExt(iface);
2724 return ShellLink_AddRef( This );
2725 }
2726
2727 static ULONG WINAPI
2728 ShellLink_IShellPropSheetExt_Release( IShellPropSheetExt* iface )
2729 {
2730 IShellLinkImpl *This = impl_from_IShellPropSheetExt(iface);
2731 return ShellLink_Release( This );
2732 }
2733
2734 static HRESULT WINAPI
2735 ShellLink_IShellPropSheetExt_AddPages( IShellPropSheetExt *iface, LPFNADDPROPSHEETPAGE pfnAddPage, LPARAM lParam)
2736 {
2737 HPROPSHEETPAGE hPage;
2738 BOOL bRet;
2739 IShellLinkImpl *This = impl_from_IShellPropSheetExt(iface);
2740
2741 hPage = SH_CreatePropertySheetPage("SHELL_GENERAL_SHORTCUT_DLG", SH_ShellLinkDlgProc, (LPARAM)This, NULL);
2742 if (hPage == NULL)
2743 {
2744 ERR("failed to create property sheet page\n");
2745 return E_FAIL;
2746 }
2747
2748 bRet = pfnAddPage(hPage, lParam);
2749 if (bRet)
2750 return S_OK;
2751 else
2752 return E_FAIL;
2753 }
2754
2755 static HRESULT WINAPI
2756 ShellLink_IShellPropSheetExt_ReplacePages( IShellPropSheetExt *iface, UINT uPageID, LPFNADDPROPSHEETPAGE pfnReplacePage, LPARAM lParam)
2757 {
2758 IShellLinkImpl *This = impl_from_IShellPropSheetExt(iface);
2759 TRACE("(%p) (uPageID %u, pfnReplacePage %p lParam %p\n", This, uPageID, pfnReplacePage, lParam);
2760 return E_NOTIMPL;
2761 }
2762
2763 static const IShellPropSheetExtVtbl pse =
2764 {
2765 ShellLink_IShellPropSheetExt_QueryInterface,
2766 ShellLink_IShellPropSheetExt_AddRef,
2767 ShellLink_IShellPropSheetExt_Release,
2768 ShellLink_IShellPropSheetExt_AddPages,
2769 ShellLink_IShellPropSheetExt_ReplacePages
2770 };
2771
2772 static HRESULT WINAPI
2773 ShellLink_InvokeCommand( IContextMenu* iface, LPCMINVOKECOMMANDINFO lpici )
2774 {
2775 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2776 static const WCHAR szOpen[] = { 'o','p','e','n',0 };
2777 static const WCHAR szCplOpen[] = { 'c','p','l','o','p','e','n',0 };
2778 SHELLEXECUTEINFOW sei;
2779 HWND hwnd = NULL; /* FIXME: get using interface set from IObjectWithSite */
2780 LPWSTR args = NULL;
2781 LPWSTR path = NULL;
2782 HRESULT r;
2783
2784 TRACE("%p %p\n", This, lpici );
2785
2786 if ( lpici->cbSize < sizeof (CMINVOKECOMMANDINFO) )
2787 return E_INVALIDARG;
2788
2789 r = IShellLinkW_Resolve( (IShellLinkW*)&(This->lpvtblw), hwnd, 0 );
2790 if ( FAILED( r ) )
2791 {
2792 TRACE("failed to resolve component with error 0x%08x", r);
2793 return r;
2794 }
2795 if ( This->sComponent )
2796 {
2797 path = shelllink_get_msi_component_path( This->sComponent );
2798 if (!path)
2799 return E_FAIL;
2800 }
2801 else
2802 path = strdupW( This->sPath );
2803
2804 if ( lpici->cbSize == sizeof (CMINVOKECOMMANDINFOEX) &&
2805 ( lpici->fMask & CMIC_MASK_UNICODE ) )
2806 {
2807 LPCMINVOKECOMMANDINFOEX iciex = (LPCMINVOKECOMMANDINFOEX) lpici;
2808 DWORD len = 2;
2809
2810 if ( This->sArgs )
2811 len += wcslen( This->sArgs );
2812 if ( iciex->lpParametersW )
2813 len += wcslen( iciex->lpParametersW );
2814
2815 args = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
2816 args[0] = 0;
2817 if ( This->sArgs )
2818 wcscat( args, This->sArgs );
2819 if ( iciex->lpParametersW )
2820 {
2821 static const WCHAR space[] = { ' ', 0 };
2822 wcscat( args, space );
2823 wcscat( args, iciex->lpParametersW );
2824 }
2825 }
2826 else if (This->sArgs != NULL)
2827 {
2828 args = This->sArgs;
2829 }
2830
2831 memset( &sei, 0, sizeof sei );
2832 sei.cbSize = sizeof sei;
2833 sei.fMask = SEE_MASK_UNICODE | (lpici->fMask & (SEE_MASK_NOASYNC|SEE_MASK_ASYNCOK|SEE_MASK_FLAG_NO_UI));
2834 sei.lpFile = path;
2835 sei.nShow = This->iShowCmd;
2836 sei.lpDirectory = This->sWorkDir;
2837 sei.lpParameters = args;
2838 sei.lpVerb = szOpen;
2839
2840 // HACK for ShellExecuteExW
2841 if (!wcsstr(This->sPath, L".cpl"))
2842 sei.lpVerb = szOpen;
2843 else
2844 sei.lpVerb = szCplOpen;
2845
2846 if( ShellExecuteExW( &sei ) )
2847 r = S_OK;
2848 else
2849 r = E_FAIL;
2850
2851 HeapFree( GetProcessHeap(), 0, args );
2852 HeapFree( GetProcessHeap(), 0, path );
2853
2854 return r;
2855 }
2856
2857 static HRESULT WINAPI
2858 ShellLink_GetCommandString( IContextMenu* iface, UINT_PTR idCmd, UINT uType,
2859 UINT* pwReserved, LPSTR pszName, UINT cchMax )
2860 {
2861 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2862
2863 FIXME("%p %lu %u %p %p %u\n", This,
2864 idCmd, uType, pwReserved, pszName, cchMax );
2865
2866 return E_NOTIMPL;
2867 }
2868
2869 static const IContextMenuVtbl cmvt =
2870 {
2871 ShellLink_ContextMenu_QueryInterface,
2872 ShellLink_ContextMenu_AddRef,
2873 ShellLink_ContextMenu_Release,
2874 ShellLink_QueryContextMenu,
2875 ShellLink_InvokeCommand,
2876 ShellLink_GetCommandString
2877 };
2878
2879 static HRESULT WINAPI
2880 ShellLink_ObjectWithSite_QueryInterface( IObjectWithSite* iface, REFIID riid, void** ppvObject )
2881 {
2882 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2883 return ShellLink_QueryInterface( This, riid, ppvObject );
2884 }
2885
2886 static ULONG WINAPI
2887 ShellLink_ObjectWithSite_AddRef( IObjectWithSite* iface )
2888 {
2889 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2890 return ShellLink_AddRef( This );
2891 }
2892
2893 static ULONG WINAPI
2894 ShellLink_ObjectWithSite_Release( IObjectWithSite* iface )
2895 {
2896 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2897 return ShellLink_Release( This );
2898 }
2899
2900 static HRESULT WINAPI
2901 ShellLink_GetSite( IObjectWithSite *iface, REFIID iid, void ** ppvSite )
2902 {
2903 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2904
2905 TRACE("%p %s %p\n", This, debugstr_guid( iid ), ppvSite );
2906
2907 if ( !This->site )
2908 return E_FAIL;
2909 return IUnknown_QueryInterface( This->site, iid, ppvSite );
2910 }
2911
2912 static HRESULT WINAPI
2913 ShellLink_SetSite( IObjectWithSite *iface, IUnknown *punk )
2914 {
2915 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2916
2917 TRACE("%p %p\n", iface, punk);
2918
2919 if ( punk )
2920 IUnknown_AddRef( punk );
2921 This->site = punk;
2922
2923 return S_OK;
2924 }
2925
2926 static const IObjectWithSiteVtbl owsvt =
2927 {
2928 ShellLink_ObjectWithSite_QueryInterface,
2929 ShellLink_ObjectWithSite_AddRef,
2930 ShellLink_ObjectWithSite_Release,
2931 ShellLink_SetSite,
2932 ShellLink_GetSite,
2933 };