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