734e47f9e7af3e11c678ba7a9d3888110759a887
[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 #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((HANDLE)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 memcpy( pclsid, &CLSID_ShellLink, sizeof (CLSID) );
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.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.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.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.dwSignature, debugstr_w(buffer.szwDarwinID));
757
758 if( (buffer.dwSignature&0xffff0000) != 0xa0000000 )
759 {
760 ERR("Unknown magic number %08x in advertised shortcut\n", buffer.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->cbSize = sizeof *buffer;
1035 buffer->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->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 str = HEAP_strdupAtoW(GetProcessHeap(), 0, pszFile);
1655 if( !str )
1656 return E_OUTOFMEMORY;
1657
1658 r = IShellLinkW_SetPath((IShellLinkW*)&(This->lpvtblw), str);
1659 HeapFree( GetProcessHeap(), 0, str );
1660
1661 return r;
1662 }
1663
1664 /**************************************************************************
1665 * IShellLink Implementation
1666 */
1667
1668 static const IShellLinkAVtbl slvt =
1669 {
1670 IShellLinkA_fnQueryInterface,
1671 IShellLinkA_fnAddRef,
1672 IShellLinkA_fnRelease,
1673 IShellLinkA_fnGetPath,
1674 IShellLinkA_fnGetIDList,
1675 IShellLinkA_fnSetIDList,
1676 IShellLinkA_fnGetDescription,
1677 IShellLinkA_fnSetDescription,
1678 IShellLinkA_fnGetWorkingDirectory,
1679 IShellLinkA_fnSetWorkingDirectory,
1680 IShellLinkA_fnGetArguments,
1681 IShellLinkA_fnSetArguments,
1682 IShellLinkA_fnGetHotkey,
1683 IShellLinkA_fnSetHotkey,
1684 IShellLinkA_fnGetShowCmd,
1685 IShellLinkA_fnSetShowCmd,
1686 IShellLinkA_fnGetIconLocation,
1687 IShellLinkA_fnSetIconLocation,
1688 IShellLinkA_fnSetRelativePath,
1689 IShellLinkA_fnResolve,
1690 IShellLinkA_fnSetPath
1691 };
1692
1693
1694 /**************************************************************************
1695 * IShellLinkW_fnQueryInterface
1696 */
1697 static HRESULT WINAPI IShellLinkW_fnQueryInterface(
1698 IShellLinkW * iface, REFIID riid, LPVOID *ppvObj)
1699 {
1700 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1701 return ShellLink_QueryInterface( This, riid, ppvObj );
1702 }
1703
1704 /******************************************************************************
1705 * IShellLinkW_fnAddRef
1706 */
1707 static ULONG WINAPI IShellLinkW_fnAddRef(IShellLinkW * iface)
1708 {
1709 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1710 return ShellLink_AddRef( This );
1711 }
1712
1713 /******************************************************************************
1714 * IShellLinkW_fnRelease
1715 */
1716 static ULONG WINAPI IShellLinkW_fnRelease(IShellLinkW * iface)
1717 {
1718 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1719 return ShellLink_Release( This );
1720 }
1721
1722 static HRESULT WINAPI IShellLinkW_fnGetPath(IShellLinkW * iface, LPWSTR pszFile,INT cchMaxPath, WIN32_FIND_DATAW *pfd, DWORD fFlags)
1723 {
1724 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1725
1726 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%u)(%s)\n",
1727 This, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(This->sPath));
1728
1729 if (This->sComponent || This->sProduct)
1730 return S_FALSE;
1731
1732 if (cchMaxPath)
1733 pszFile[0] = 0;
1734 if (This->sPath)
1735 lstrcpynW( pszFile, This->sPath, cchMaxPath );
1736
1737 if (pfd) FIXME("(%p): WIN32_FIND_DATA is not yet filled.\n", This);
1738
1739 return S_OK;
1740 }
1741
1742 static HRESULT WINAPI IShellLinkW_fnGetIDList(IShellLinkW * iface, LPITEMIDLIST * ppidl)
1743 {
1744 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1745
1746 TRACE("(%p)->(ppidl=%p)\n",This, ppidl);
1747
1748 if (!This->pPidl)
1749 {
1750 *ppidl = NULL;
1751 return S_FALSE;
1752 }
1753 *ppidl = ILClone(This->pPidl);
1754 return S_OK;
1755 }
1756
1757 static HRESULT WINAPI IShellLinkW_fnSetIDList(IShellLinkW * iface, LPCITEMIDLIST pidl)
1758 {
1759 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1760
1761 TRACE("(%p)->(pidl=%p)\n",This, pidl);
1762
1763 if( This->pPidl )
1764 ILFree( This->pPidl );
1765 This->pPidl = ILClone( pidl );
1766 if( !This->pPidl )
1767 return E_FAIL;
1768
1769 This->bDirty = TRUE;
1770
1771 return S_OK;
1772 }
1773
1774 static HRESULT WINAPI IShellLinkW_fnGetDescription(IShellLinkW * iface, LPWSTR pszName,INT cchMaxName)
1775 {
1776 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1777
1778 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1779
1780 pszName[0] = 0;
1781 if( This->sDescription )
1782 lstrcpynW( pszName, This->sDescription, cchMaxName );
1783
1784 return S_OK;
1785 }
1786
1787 static HRESULT WINAPI IShellLinkW_fnSetDescription(IShellLinkW * iface, LPCWSTR pszName)
1788 {
1789 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1790
1791 TRACE("(%p)->(desc=%s)\n",This, debugstr_w(pszName));
1792
1793 HeapFree(GetProcessHeap(), 0, This->sDescription);
1794 This->sDescription = HeapAlloc( GetProcessHeap(), 0,
1795 (wcslen( pszName )+1)*sizeof(WCHAR) );
1796 if ( !This->sDescription )
1797 return E_OUTOFMEMORY;
1798
1799 wcscpy( This->sDescription, pszName );
1800 This->bDirty = TRUE;
1801
1802 return S_OK;
1803 }
1804
1805 static HRESULT WINAPI IShellLinkW_fnGetWorkingDirectory(IShellLinkW * iface, LPWSTR pszDir,INT cchMaxPath)
1806 {
1807 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1808
1809 TRACE("(%p)->(%p len %u)\n", This, pszDir, cchMaxPath);
1810
1811 if( cchMaxPath )
1812 pszDir[0] = 0;
1813 if( This->sWorkDir )
1814 lstrcpynW( pszDir, This->sWorkDir, cchMaxPath );
1815
1816 return S_OK;
1817 }
1818
1819 static HRESULT WINAPI IShellLinkW_fnSetWorkingDirectory(IShellLinkW * iface, LPCWSTR pszDir)
1820 {
1821 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1822
1823 TRACE("(%p)->(dir=%s)\n",This, debugstr_w(pszDir));
1824
1825 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1826 This->sWorkDir = HeapAlloc( GetProcessHeap(), 0,
1827 (wcslen( pszDir )+1)*sizeof (WCHAR) );
1828 if ( !This->sWorkDir )
1829 return E_OUTOFMEMORY;
1830 wcscpy( This->sWorkDir, pszDir );
1831 This->bDirty = TRUE;
1832
1833 return S_OK;
1834 }
1835
1836 static HRESULT WINAPI IShellLinkW_fnGetArguments(IShellLinkW * iface, LPWSTR pszArgs,INT cchMaxPath)
1837 {
1838 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1839
1840 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1841
1842 if( cchMaxPath )
1843 pszArgs[0] = 0;
1844 if( This->sArgs )
1845 lstrcpynW( pszArgs, This->sArgs, cchMaxPath );
1846
1847 return NOERROR;
1848 }
1849
1850 static HRESULT WINAPI IShellLinkW_fnSetArguments(IShellLinkW * iface, LPCWSTR pszArgs)
1851 {
1852 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1853
1854 TRACE("(%p)->(args=%s)\n",This, debugstr_w(pszArgs));
1855
1856 HeapFree(GetProcessHeap(), 0, This->sArgs);
1857 This->sArgs = HeapAlloc( GetProcessHeap(), 0,
1858 (wcslen( pszArgs )+1)*sizeof (WCHAR) );
1859 if ( !This->sArgs )
1860 return E_OUTOFMEMORY;
1861 wcscpy( This->sArgs, pszArgs );
1862 This->bDirty = TRUE;
1863
1864 return S_OK;
1865 }
1866
1867 static HRESULT WINAPI IShellLinkW_fnGetHotkey(IShellLinkW * iface, WORD *pwHotkey)
1868 {
1869 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1870
1871 TRACE("(%p)->(%p)\n",This, pwHotkey);
1872
1873 *pwHotkey=This->wHotKey;
1874
1875 return S_OK;
1876 }
1877
1878 static HRESULT WINAPI IShellLinkW_fnSetHotkey(IShellLinkW * iface, WORD wHotkey)
1879 {
1880 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1881
1882 TRACE("(%p)->(hotkey=%x)\n",This, wHotkey);
1883
1884 This->wHotKey = wHotkey;
1885 This->bDirty = TRUE;
1886
1887 return S_OK;
1888 }
1889
1890 static HRESULT WINAPI IShellLinkW_fnGetShowCmd(IShellLinkW * iface, INT *piShowCmd)
1891 {
1892 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1893
1894 TRACE("(%p)->(%p)\n",This, piShowCmd);
1895
1896 *piShowCmd = This->iShowCmd;
1897
1898 return S_OK;
1899 }
1900
1901 static HRESULT WINAPI IShellLinkW_fnSetShowCmd(IShellLinkW * iface, INT iShowCmd)
1902 {
1903 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1904
1905 This->iShowCmd = iShowCmd;
1906 This->bDirty = TRUE;
1907
1908 return S_OK;
1909 }
1910
1911 static HRESULT SHELL_PidlGeticonLocationW(IShellFolder* psf, LPCITEMIDLIST pidl,
1912 LPWSTR pszIconPath, int cchIconPath, int* piIcon)
1913 {
1914 LPCITEMIDLIST pidlLast;
1915 UINT wFlags;
1916
1917 HRESULT hr = SHBindToParent(pidl, &IID_IShellFolder, (LPVOID*)&psf, &pidlLast);
1918
1919 if (SUCCEEDED(hr)) {
1920 IExtractIconW* pei;
1921
1922 hr = IShellFolder_GetUIObjectOf(psf, 0, 1, &pidlLast, &IID_IExtractIconW, NULL, (LPVOID*)&pei);
1923
1924 if (SUCCEEDED(hr)) {
1925 hr = IExtractIconW_GetIconLocation(pei, 0, pszIconPath, MAX_PATH, piIcon, &wFlags);
1926
1927 IExtractIconW_Release(pei);
1928 }
1929
1930 IShellFolder_Release(psf);
1931 }
1932
1933 return hr;
1934 }
1935
1936 static HRESULT WINAPI IShellLinkW_fnGetIconLocation(IShellLinkW * iface, LPWSTR pszIconPath,INT cchIconPath,INT *piIcon)
1937 {
1938 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1939
1940 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1941
1942 pszIconPath[0] = 0;
1943 *piIcon = This->iIcoNdx;
1944
1945 if (This->sIcoPath)
1946 {
1947 lstrcpynW(pszIconPath, This->sIcoPath, cchIconPath);
1948 return S_OK;
1949 }
1950
1951 if (This->pPidl || This->sPath)
1952 {
1953 IShellFolder* pdsk;
1954
1955 HRESULT hr = SHGetDesktopFolder(&pdsk);
1956
1957 if (SUCCEEDED(hr))
1958 {
1959 /* first look for an icon using the PIDL (if present) */
1960 if (This->pPidl)
1961 hr = SHELL_PidlGeticonLocationW(pdsk, This->pPidl, pszIconPath, cchIconPath, piIcon);
1962 else
1963 hr = E_FAIL;
1964
1965 /* if we couldn't find an icon yet, look for it using the file system path */
1966 if (FAILED(hr) && This->sPath)
1967 {
1968 LPITEMIDLIST pidl;
1969
1970 hr = IShellFolder_ParseDisplayName(pdsk, 0, NULL, This->sPath, NULL, &pidl, NULL);
1971
1972 if (SUCCEEDED(hr))
1973 {
1974 hr = SHELL_PidlGeticonLocationW(pdsk, pidl, pszIconPath, cchIconPath, piIcon);
1975
1976 SHFree(pidl);
1977 }
1978 }
1979
1980 IShellFolder_Release(pdsk);
1981 }
1982 return hr;
1983 }
1984 return S_OK;
1985 }
1986
1987 static HRESULT WINAPI IShellLinkW_fnSetIconLocation(IShellLinkW * iface, LPCWSTR pszIconPath,INT iIcon)
1988 {
1989 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1990
1991 TRACE("(%p)->(path=%s iicon=%u)\n",This, debugstr_w(pszIconPath), iIcon);
1992
1993 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
1994 This->sIcoPath = HeapAlloc( GetProcessHeap(), 0,
1995 (wcslen( pszIconPath )+1)*sizeof (WCHAR) );
1996 if ( !This->sIcoPath )
1997 return E_OUTOFMEMORY;
1998 wcscpy( This->sIcoPath, pszIconPath );
1999
2000 This->iIcoNdx = iIcon;
2001 This->bDirty = TRUE;
2002
2003 return S_OK;
2004 }
2005
2006 static HRESULT WINAPI IShellLinkW_fnSetRelativePath(IShellLinkW * iface, LPCWSTR pszPathRel, DWORD dwReserved)
2007 {
2008 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
2009
2010 TRACE("(%p)->(path=%s %x)\n",This, debugstr_w(pszPathRel), dwReserved);
2011
2012 HeapFree(GetProcessHeap(), 0, This->sPathRel);
2013 This->sPathRel = HeapAlloc( GetProcessHeap(), 0,
2014 (wcslen( pszPathRel )+1) * sizeof (WCHAR) );
2015 if ( !This->sPathRel )
2016 return E_OUTOFMEMORY;
2017 wcscpy( This->sPathRel, pszPathRel );
2018 This->bDirty = TRUE;
2019
2020 return ShellLink_UpdatePath(This->sPathRel, This->sPath, This->sWorkDir, &This->sPath);
2021 }
2022
2023 static HRESULT WINAPI IShellLinkW_fnResolve(IShellLinkW * iface, HWND hwnd, DWORD fFlags)
2024 {
2025 HRESULT hr = S_OK;
2026 BOOL bSuccess;
2027
2028 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
2029
2030 TRACE("(%p)->(hwnd=%p flags=%x)\n",This, hwnd, fFlags);
2031
2032 /*FIXME: use IResolveShellLink interface */
2033
2034 if (!This->sPath && This->pPidl) {
2035 WCHAR buffer[MAX_PATH];
2036
2037 bSuccess = SHGetPathFromIDListW(This->pPidl, buffer);
2038
2039 if (bSuccess && *buffer) {
2040 This->sPath = HeapAlloc(GetProcessHeap(), 0, (wcslen(buffer)+1)*sizeof(WCHAR));
2041 if (!This->sPath)
2042 return E_OUTOFMEMORY;
2043
2044 wcscpy(This->sPath, buffer);
2045
2046 This->bDirty = TRUE;
2047 } else
2048 hr = S_OK; /* don't report an error occurred while just caching information */
2049 }
2050
2051 if (!This->sIcoPath && This->sPath) {
2052 This->sIcoPath = HeapAlloc(GetProcessHeap(), 0, (wcslen(This->sPath)+1)*sizeof(WCHAR));
2053 if (!This->sIcoPath)
2054 return E_OUTOFMEMORY;
2055
2056 wcscpy(This->sIcoPath, This->sPath);
2057 This->iIcoNdx = 0;
2058
2059 This->bDirty = TRUE;
2060 }
2061
2062 return hr;
2063 }
2064
2065 static LPWSTR ShellLink_GetAdvertisedArg(LPCWSTR str)
2066 {
2067 LPWSTR ret;
2068 LPCWSTR p;
2069 DWORD len;
2070
2071 if( !str )
2072 return NULL;
2073
2074 p = wcschr( str, ':' );
2075 if( !p )
2076 return NULL;
2077 len = p - str;
2078 ret = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR)*(len+1));
2079 if( !ret )
2080 return ret;
2081 memcpy( ret, str, sizeof(WCHAR)*len );
2082 ret[len] = 0;
2083 return ret;
2084 }
2085
2086 static HRESULT ShellLink_SetAdvertiseInfo(IShellLinkImpl *This, LPCWSTR str)
2087 {
2088 LPCWSTR szComponent = NULL, szProduct = NULL, p;
2089 WCHAR szGuid[39];
2090 HRESULT r;
2091 GUID guid;
2092 int len;
2093
2094 while( str[0] )
2095 {
2096 /* each segment must start with two colons */
2097 if( str[0] != ':' || str[1] != ':' )
2098 return E_FAIL;
2099
2100 /* the last segment is just two colons */
2101 if( !str[2] )
2102 break;
2103 str += 2;
2104
2105 /* there must be a colon straight after a guid */
2106 p = wcschr( str, ':' );
2107 if( !p )
2108 return E_FAIL;
2109 len = p - str;
2110 if( len != 38 )
2111 return E_FAIL;
2112
2113 /* get the guid, and check it's validly formatted */
2114 memcpy( szGuid, str, sizeof(WCHAR)*len );
2115 szGuid[len] = 0;
2116 r = CLSIDFromString( szGuid, &guid );
2117 if( r != S_OK )
2118 return r;
2119 str = p + 1;
2120
2121 /* match it up to a guid that we care about */
2122 if( IsEqualGUID( &guid, &SHELL32_AdvtShortcutComponent ) && !szComponent )
2123 szComponent = str;
2124 else if( IsEqualGUID( &guid, &SHELL32_AdvtShortcutProduct ) && !szProduct )
2125 szProduct = str;
2126 else
2127 return E_FAIL;
2128
2129 /* skip to the next field */
2130 str = wcschr( str, ':' );
2131 if( !str )
2132 return E_FAIL;
2133 }
2134
2135 /* we have to have a component for an advertised shortcut */
2136 if( !szComponent )
2137 return E_FAIL;
2138
2139 This->sComponent = ShellLink_GetAdvertisedArg( szComponent );
2140 This->sProduct = ShellLink_GetAdvertisedArg( szProduct );
2141
2142 TRACE("Component = %s\n", debugstr_w(This->sComponent));
2143 TRACE("Product = %s\n", debugstr_w(This->sProduct));
2144
2145 return S_OK;
2146 }
2147
2148 static BOOL ShellLink_GetVolumeInfo(LPCWSTR path, volume_info *volume)
2149 {
2150 const int label_sz = sizeof volume->label/sizeof volume->label[0];
2151 WCHAR drive[4] = { path[0], ':', '\\', 0 };
2152 BOOL r;
2153
2154 volume->type = GetDriveTypeW(drive);
2155 r = GetVolumeInformationW(drive, volume->label, label_sz,
2156 &volume->serial, NULL, NULL, NULL, 0);
2157 TRACE("r = %d type %d serial %08x name %s\n", r,
2158 volume->type, volume->serial, debugstr_w(volume->label));
2159 return r;
2160 }
2161
2162 static HRESULT WINAPI IShellLinkW_fnSetPath(IShellLinkW * iface, LPCWSTR pszFile)
2163 {
2164 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
2165 WCHAR buffer[MAX_PATH];
2166 LPWSTR fname, unquoted = NULL;
2167 HRESULT hr = S_OK;
2168 UINT len;
2169
2170 TRACE("(%p)->(path=%s)\n",This, debugstr_w(pszFile));
2171
2172 /* quotes at the ends of the string are stripped */
2173 len = wcslen(pszFile);
2174 if (pszFile[0] == '"' && pszFile[len-1] == '"')
2175 {
2176 unquoted = strdupW(pszFile);
2177 PathUnquoteSpacesW(unquoted);
2178 pszFile = unquoted;
2179 }
2180
2181 /* any other quote marks are invalid */
2182 if (wcschr(pszFile, '"'))
2183 return S_FALSE;
2184
2185 HeapFree(GetProcessHeap(), 0, This->sPath);
2186 This->sPath = NULL;
2187
2188 HeapFree(GetProcessHeap(), 0, This->sComponent);
2189 This->sComponent = NULL;
2190
2191 if (This->pPidl)
2192 ILFree(This->pPidl);
2193 This->pPidl = NULL;
2194
2195 if (S_OK != ShellLink_SetAdvertiseInfo( This, pszFile ))
2196 {
2197 if (*pszFile == '\0')
2198 *buffer = '\0';
2199 else if (!GetFullPathNameW(pszFile, MAX_PATH, buffer, &fname))
2200 return E_FAIL;
2201 else if(!PathFileExistsW(buffer) &&
2202 !SearchPathW(NULL, pszFile, NULL, MAX_PATH, buffer, NULL))
2203 hr = S_FALSE;
2204
2205 This->pPidl = SHSimpleIDListFromPathW(pszFile);
2206 ShellLink_GetVolumeInfo(buffer, &This->volume);
2207
2208 This->sPath = HeapAlloc( GetProcessHeap(), 0,
2209 (wcslen( buffer )+1) * sizeof (WCHAR) );
2210 if (!This->sPath)
2211 return E_OUTOFMEMORY;
2212
2213 wcscpy(This->sPath, buffer);
2214 }
2215 This->bDirty = TRUE;
2216 HeapFree(GetProcessHeap(), 0, unquoted);
2217
2218 return hr;
2219 }
2220
2221 /**************************************************************************
2222 * IShellLinkW Implementation
2223 */
2224
2225 static const IShellLinkWVtbl slvtw =
2226 {
2227 IShellLinkW_fnQueryInterface,
2228 IShellLinkW_fnAddRef,
2229 IShellLinkW_fnRelease,
2230 IShellLinkW_fnGetPath,
2231 IShellLinkW_fnGetIDList,
2232 IShellLinkW_fnSetIDList,
2233 IShellLinkW_fnGetDescription,
2234 IShellLinkW_fnSetDescription,
2235 IShellLinkW_fnGetWorkingDirectory,
2236 IShellLinkW_fnSetWorkingDirectory,
2237 IShellLinkW_fnGetArguments,
2238 IShellLinkW_fnSetArguments,
2239 IShellLinkW_fnGetHotkey,
2240 IShellLinkW_fnSetHotkey,
2241 IShellLinkW_fnGetShowCmd,
2242 IShellLinkW_fnSetShowCmd,
2243 IShellLinkW_fnGetIconLocation,
2244 IShellLinkW_fnSetIconLocation,
2245 IShellLinkW_fnSetRelativePath,
2246 IShellLinkW_fnResolve,
2247 IShellLinkW_fnSetPath
2248 };
2249
2250 static HRESULT WINAPI
2251 ShellLink_DataList_QueryInterface( IShellLinkDataList* iface, REFIID riid, void** ppvObject)
2252 {
2253 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2254 return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvObject);
2255 }
2256
2257 static ULONG WINAPI
2258 ShellLink_DataList_AddRef( IShellLinkDataList* iface )
2259 {
2260 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2261 return IShellLinkA_AddRef((IShellLinkA*)This);
2262 }
2263
2264 static ULONG WINAPI
2265 ShellLink_DataList_Release( IShellLinkDataList* iface )
2266 {
2267 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2268 return ShellLink_Release( This );
2269 }
2270
2271 static HRESULT WINAPI
2272 ShellLink_AddDataBlock( IShellLinkDataList* iface, void* pDataBlock )
2273 {
2274 FIXME("\n");
2275 return E_NOTIMPL;
2276 }
2277
2278 static HRESULT WINAPI
2279 ShellLink_CopyDataBlock( IShellLinkDataList* iface, DWORD dwSig, void** ppDataBlock )
2280 {
2281 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2282 LPVOID block = NULL;
2283 HRESULT r = E_FAIL;
2284
2285 TRACE("%p %08x %p\n", iface, dwSig, ppDataBlock );
2286
2287 switch (dwSig)
2288 {
2289 case EXP_DARWIN_ID_SIG:
2290 if (!This->sComponent)
2291 break;
2292 block = shelllink_build_darwinid( This->sComponent, dwSig );
2293 r = S_OK;
2294 break;
2295 case EXP_SZ_LINK_SIG:
2296 case NT_CONSOLE_PROPS_SIG:
2297 case NT_FE_CONSOLE_PROPS_SIG:
2298 case EXP_SPECIAL_FOLDER_SIG:
2299 case EXP_SZ_ICON_SIG:
2300 FIXME("valid but unhandled datablock %08x\n", dwSig);
2301 break;
2302 default:
2303 ERR("unknown datablock %08x\n", dwSig);
2304 }
2305 *ppDataBlock = block;
2306 return r;
2307 }
2308
2309 static HRESULT WINAPI
2310 ShellLink_RemoveDataBlock( IShellLinkDataList* iface, DWORD dwSig )
2311 {
2312 FIXME("\n");
2313 return E_NOTIMPL;
2314 }
2315
2316 static HRESULT WINAPI
2317 ShellLink_GetFlags( IShellLinkDataList* iface, DWORD* pdwFlags )
2318 {
2319 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2320 DWORD flags = 0;
2321
2322 FIXME("%p %p\n", This, pdwFlags );
2323
2324 /* FIXME: add more */
2325 if (This->sArgs)
2326 flags |= SLDF_HAS_ARGS;
2327 if (This->sComponent)
2328 flags |= SLDF_HAS_DARWINID;
2329 if (This->sIcoPath)
2330 flags |= SLDF_HAS_ICONLOCATION;
2331 #if (NTDDI_VERSION < NTDDI_LONGHORN)
2332 if (This->sProduct)
2333 flags |= SLDF_HAS_LOGO3ID;
2334 #endif
2335 if (This->pPidl)
2336 flags |= SLDF_HAS_ID_LIST;
2337
2338 *pdwFlags = flags;
2339
2340 return S_OK;
2341 }
2342
2343 static HRESULT WINAPI
2344 ShellLink_SetFlags( IShellLinkDataList* iface, DWORD dwFlags )
2345 {
2346 FIXME("\n");
2347 return E_NOTIMPL;
2348 }
2349
2350 static const IShellLinkDataListVtbl dlvt =
2351 {
2352 ShellLink_DataList_QueryInterface,
2353 ShellLink_DataList_AddRef,
2354 ShellLink_DataList_Release,
2355 ShellLink_AddDataBlock,
2356 ShellLink_CopyDataBlock,
2357 ShellLink_RemoveDataBlock,
2358 ShellLink_GetFlags,
2359 ShellLink_SetFlags
2360 };
2361
2362 static HRESULT WINAPI
2363 ShellLink_ExtInit_QueryInterface( IShellExtInit* iface, REFIID riid, void** ppvObject )
2364 {
2365 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2366 return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvObject);
2367 }
2368
2369 static ULONG WINAPI
2370 ShellLink_ExtInit_AddRef( IShellExtInit* iface )
2371 {
2372 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2373 return IShellLinkA_AddRef((IShellLinkA*)This);
2374 }
2375
2376 static ULONG WINAPI
2377 ShellLink_ExtInit_Release( IShellExtInit* iface )
2378 {
2379 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2380 return ShellLink_Release( This );
2381 }
2382
2383 /**************************************************************************
2384 * ShellLink implementation of IShellExtInit::Initialize()
2385 *
2386 * Loads the shelllink from the dataobject the shell is pointing to.
2387 */
2388 static HRESULT WINAPI
2389 ShellLink_ExtInit_Initialize( IShellExtInit* iface, LPCITEMIDLIST pidlFolder,
2390 IDataObject *pdtobj, HKEY hkeyProgID )
2391 {
2392 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2393 FORMATETC format;
2394 STGMEDIUM stgm;
2395 UINT count;
2396 HRESULT r = E_FAIL;
2397
2398 TRACE("%p %p %p %p\n", This, pidlFolder, pdtobj, hkeyProgID );
2399
2400 if( !pdtobj )
2401 return r;
2402
2403 format.cfFormat = CF_HDROP;
2404 format.ptd = NULL;
2405 format.dwAspect = DVASPECT_CONTENT;
2406 format.lindex = -1;
2407 format.tymed = TYMED_HGLOBAL;
2408
2409 if( FAILED( IDataObject_GetData( pdtobj, &format, &stgm ) ) )
2410 return r;
2411
2412 count = DragQueryFileW( stgm.u.hGlobal, -1, NULL, 0 );
2413 if( count == 1 )
2414 {
2415 LPWSTR path;
2416
2417 count = DragQueryFileW( stgm.u.hGlobal, 0, NULL, 0 );
2418 count++;
2419 path = HeapAlloc( GetProcessHeap(), 0, count*sizeof(WCHAR) );
2420 if( path )
2421 {
2422 IPersistFile *pf = (IPersistFile*) &This->lpvtblPersistFile;
2423
2424 count = DragQueryFileW( stgm.u.hGlobal, 0, path, count );
2425 r = IPersistFile_Load( pf, path, 0 );
2426 HeapFree( GetProcessHeap(), 0, path );
2427 }
2428 }
2429 ReleaseStgMedium( &stgm );
2430
2431 return r;
2432 }
2433
2434 static const IShellExtInitVtbl eivt =
2435 {
2436 ShellLink_ExtInit_QueryInterface,
2437 ShellLink_ExtInit_AddRef,
2438 ShellLink_ExtInit_Release,
2439 ShellLink_ExtInit_Initialize
2440 };
2441
2442 static HRESULT WINAPI
2443 ShellLink_ContextMenu_QueryInterface( IContextMenu* iface, REFIID riid, void** ppvObject )
2444 {
2445 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2446 return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvObject);
2447 }
2448
2449 static ULONG WINAPI
2450 ShellLink_ContextMenu_AddRef( IContextMenu* iface )
2451 {
2452 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2453 return IShellLinkA_AddRef((IShellLinkA*)This);
2454 }
2455
2456 static ULONG WINAPI
2457 ShellLink_ContextMenu_Release( IContextMenu* iface )
2458 {
2459 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2460 return ShellLink_Release( This );
2461 }
2462
2463 static HRESULT WINAPI
2464 ShellLink_QueryContextMenu( IContextMenu* iface, HMENU hmenu, UINT indexMenu,
2465 UINT idCmdFirst, UINT idCmdLast, UINT uFlags )
2466 {
2467 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2468 WCHAR szOpen[20];
2469 MENUITEMINFOW mii;
2470 int id = 1;
2471
2472 TRACE("%p %p %u %u %u %u\n", This,
2473 hmenu, indexMenu, idCmdFirst, idCmdLast, uFlags );
2474
2475 if ( !hmenu )
2476 return E_INVALIDARG;
2477
2478 if (!LoadStringW(shell32_hInstance, IDS_OPEN_VERB, szOpen, sizeof(szOpen)/sizeof(WCHAR)))
2479 szOpen[0] = L'\0';
2480 else
2481 szOpen[(sizeof(szOpen)/sizeof(WCHAR))-1] = L'\0';
2482
2483 memset( &mii, 0, sizeof(mii) );
2484 mii.cbSize = sizeof (mii);
2485 mii.fMask = MIIM_TYPE | MIIM_ID | MIIM_STATE;
2486 mii.dwTypeData = (LPWSTR)szOpen;
2487 mii.cch = wcslen( mii.dwTypeData );
2488 mii.wID = idCmdFirst + id++;
2489 mii.fState = MFS_DEFAULT | MFS_ENABLED;
2490 mii.fType = MFT_STRING;
2491 if (!InsertMenuItemW( hmenu, indexMenu, TRUE, &mii ))
2492 return E_FAIL;
2493 This->iIdOpen = 1;
2494
2495 return MAKE_HRESULT( SEVERITY_SUCCESS, 0, id );
2496 }
2497
2498 static LPWSTR
2499 shelllink_get_msi_component_path( LPWSTR component )
2500 {
2501 LPWSTR path;
2502 DWORD r, sz = 0;
2503
2504 r = CommandLineFromMsiDescriptor( component, NULL, &sz );
2505 if (r != ERROR_SUCCESS)
2506 return NULL;
2507
2508 sz++;
2509 path = HeapAlloc( GetProcessHeap(), 0, sz*sizeof(WCHAR) );
2510 r = CommandLineFromMsiDescriptor( component, path, &sz );
2511 if (r != ERROR_SUCCESS)
2512 {
2513 HeapFree( GetProcessHeap(), 0, path );
2514 path = NULL;
2515 }
2516
2517 TRACE("returning %s\n", debugstr_w( path ) );
2518
2519 return path;
2520 }
2521
2522 INT_PTR CALLBACK ExtendedShortcutProc(
2523 HWND hwndDlg,
2524 UINT uMsg,
2525 WPARAM wParam,
2526 LPARAM lParam
2527 )
2528 {
2529 HWND hDlgCtrl;
2530
2531 switch(uMsg)
2532 {
2533 case WM_INITDIALOG:
2534 if (lParam)
2535 {
2536 hDlgCtrl = GetDlgItem(hwndDlg, 14000);
2537 SendMessage(hDlgCtrl, BM_SETCHECK, BST_CHECKED, 0);
2538 }
2539 return TRUE;
2540 case WM_COMMAND:
2541 hDlgCtrl = GetDlgItem(hwndDlg, 14000);
2542 if (LOWORD(wParam) == IDOK)
2543 {
2544 if ( SendMessage(hDlgCtrl, BM_GETCHECK, 0, 0) == BST_CHECKED )
2545 EndDialog(hwndDlg, 1);
2546 else
2547 EndDialog(hwndDlg, 0);
2548 }
2549 else if (LOWORD(wParam) == IDCANCEL)
2550 {
2551 EndDialog(hwndDlg, -1);
2552 }
2553 else if (LOWORD(wParam) == 14000)
2554 {
2555 if ( SendMessage(hDlgCtrl, BM_GETCHECK, 0, 0) == BST_CHECKED)
2556 SendMessage(hDlgCtrl, BM_SETCHECK, BST_UNCHECKED, 0);
2557 else
2558 SendMessage(hDlgCtrl, BM_SETCHECK, BST_CHECKED, 0);
2559
2560 }
2561 }
2562 return FALSE;
2563 }
2564
2565 /**************************************************************************
2566 * SH_ShellLinkDlgProc
2567 *
2568 * dialog proc of the shortcut property dialog
2569 */
2570
2571 INT_PTR
2572 CALLBACK
2573 SH_ShellLinkDlgProc(
2574 HWND hwndDlg,
2575 UINT uMsg,
2576 WPARAM wParam,
2577 LPARAM lParam
2578 )
2579 {
2580 LPPROPSHEETPAGEW ppsp;
2581 LPPSHNOTIFY lppsn;
2582 IShellLinkImpl *This;
2583 HWND hDlgCtrl;
2584 WCHAR szBuffer[MAX_PATH];
2585 WCHAR * ptr;
2586 int IconIndex;
2587 INT_PTR result;
2588
2589 This = (IShellLinkImpl *)GetWindowLongPtr(hwndDlg, DWLP_USER);
2590
2591 switch(uMsg)
2592 {
2593 case WM_INITDIALOG:
2594 ppsp = (LPPROPSHEETPAGEW)lParam;
2595 if (ppsp == NULL)
2596 break;
2597
2598 TRACE("ShellLink_DlgProc (WM_INITDIALOG hwnd %p lParam %p ppsplParam %x)\n",hwndDlg, lParam, ppsp->lParam);
2599
2600 This = (IShellLinkImpl *)ppsp->lParam;
2601 SetWindowLongPtr(hwndDlg, DWLP_USER, (LONG_PTR)This);
2602
2603 TRACE("sArgs: %S sComponent: %S sDescription: %S sIcoPath: %S sPath: %S sPathRel: %S sProduct: %S sWorkDir: %S\n", This->sArgs, This->sComponent ,This->sDescription,
2604 This->sIcoPath, This->sPath, This->sPathRel, This->sProduct, This->sWorkDir);
2605
2606 /* target path */
2607 hDlgCtrl = GetDlgItem( hwndDlg, 14009 );
2608 if ( hDlgCtrl != NULL )
2609 SendMessageW( hDlgCtrl, WM_SETTEXT, (WPARAM)NULL, (LPARAM)This->sPath );
2610
2611 /* working dir */
2612 hDlgCtrl = GetDlgItem( hwndDlg, 14011 );
2613 if ( hDlgCtrl != NULL )
2614 SendMessageW( hDlgCtrl, WM_SETTEXT, (WPARAM)NULL, (LPARAM)This->sWorkDir );
2615
2616 /* description */
2617 hDlgCtrl = GetDlgItem( hwndDlg, 14019 );
2618 if ( hDlgCtrl != NULL )
2619 SendMessageW( hDlgCtrl, WM_SETTEXT, (WPARAM)NULL, (LPARAM)This->sDescription );
2620 return TRUE;
2621 case WM_NOTIFY:
2622 lppsn = (LPPSHNOTIFY) lParam;
2623 if ( lppsn->hdr.code == PSN_APPLY )
2624 {
2625 /* set working directory */
2626 hDlgCtrl = GetDlgItem( hwndDlg, 14011 );
2627 SendMessageW( hDlgCtrl, WM_GETTEXT, (WPARAM)MAX_PATH, (LPARAM)szBuffer );
2628 IShellLinkW_fnSetWorkingDirectory((IShellLinkW*)&This->lpvtblw, szBuffer);
2629 /* set link destination */
2630 hDlgCtrl = GetDlgItem( hwndDlg, 14009 );
2631 SendMessageW( hDlgCtrl, WM_GETTEXT, (WPARAM)MAX_PATH, (LPARAM)szBuffer);
2632 if ( !SHELL_ExistsFileW(szBuffer) )
2633 {
2634 //FIXME load localized error msg
2635 MessageBoxW( hwndDlg, L"file not existing", szBuffer, MB_OK );
2636 SetWindowLongPtr( hwndDlg, DWL_MSGRESULT, PSNRET_INVALID_NOCHANGEPAGE );
2637 return TRUE;
2638 }
2639 ptr = wcsrchr(szBuffer, L'.');
2640 if (ptr && !_wcsnicmp(ptr, L".lnk", 4))
2641 {
2642 // FIXME load localized error msg
2643 MessageBoxW( hwndDlg, L"You cannot create a link to a shortcut", L"Error", MB_ICONERROR );
2644 SetWindowLongPtr( hwndDlg, DWL_MSGRESULT, PSNRET_INVALID_NOCHANGEPAGE );
2645 return TRUE;
2646 }
2647
2648 IShellLinkW_fnSetPath((IShellLinkW*)&This->lpvtblw, szBuffer);
2649
2650 TRACE("This %p sLinkPath %S\n", This, This->sLinkPath);
2651 IPersistFile_fnSave( (IPersistFile*)&This->lpvtblPersistFile, This->sLinkPath, TRUE );
2652 SetWindowLongPtr( hwndDlg, DWL_MSGRESULT, PSNRET_NOERROR );
2653 return TRUE;
2654 }
2655 break;
2656 case WM_COMMAND:
2657 switch(LOWORD(wParam))
2658 {
2659 case 14020:
2660 ///
2661 /// FIXME
2662 /// open target directory
2663 ///
2664 return TRUE;
2665 case 14021:
2666 if (This->sIcoPath)
2667 wcscpy(szBuffer, This->sIcoPath);
2668 IconIndex = This->iIcoNdx;
2669 if (PickIconDlg(hwndDlg, szBuffer, MAX_PATH, &IconIndex))
2670 {
2671 IShellLinkW_fnSetIconLocation((IShellLinkW*)&This->lpvtblw, szBuffer, IconIndex);
2672 ///
2673 /// FIXME redraw icon
2674 }
2675 return TRUE;
2676 case 14022:
2677 result = DialogBoxParamW(shell32_hInstance, MAKEINTRESOURCEW(SHELL_EXTENDED_SHORTCUT_DLG), hwndDlg, ExtendedShortcutProc, (LPARAM)This->bRunAs);
2678 if (result == 1 || result == 0)
2679 {
2680 if ( This->bRunAs != result )
2681 {
2682 PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
2683 }
2684
2685 This->bRunAs = result;
2686 }
2687 return TRUE;
2688 }
2689 switch(HIWORD(wParam))
2690 {
2691 case EN_CHANGE:
2692 PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
2693 break;
2694 }
2695 break;
2696 default:
2697 break;
2698 }
2699 return FALSE;
2700 }
2701
2702 /**************************************************************************
2703 * ShellLink_IShellPropSheetExt interface
2704 */
2705
2706 static HRESULT WINAPI
2707 ShellLink_IShellPropSheetExt_QueryInterface( IShellPropSheetExt* iface, REFIID riid, void** ppvObject )
2708 {
2709 IShellLinkImpl *This = impl_from_IShellPropSheetExt(iface);
2710 return ShellLink_QueryInterface( This, riid, ppvObject );
2711 }
2712
2713 static ULONG WINAPI
2714 ShellLink_IShellPropSheetExt_AddRef( IShellPropSheetExt* iface )
2715 {
2716 IShellLinkImpl *This = impl_from_IShellPropSheetExt(iface);
2717 return ShellLink_AddRef( This );
2718 }
2719
2720 static ULONG WINAPI
2721 ShellLink_IShellPropSheetExt_Release( IShellPropSheetExt* iface )
2722 {
2723 IShellLinkImpl *This = impl_from_IShellPropSheetExt(iface);
2724 return ShellLink_Release( This );
2725 }
2726
2727 static HRESULT WINAPI
2728 ShellLink_IShellPropSheetExt_AddPages( IShellPropSheetExt *iface, LPFNADDPROPSHEETPAGE pfnAddPage, LPARAM lParam)
2729 {
2730 HPROPSHEETPAGE hPage;
2731 BOOL bRet;
2732 IShellLinkImpl *This = impl_from_IShellPropSheetExt(iface);
2733
2734 hPage = SH_CreatePropertySheetPage("SHELL_GENERAL_SHORTCUT_DLG", SH_ShellLinkDlgProc, (LPARAM)This, NULL);
2735 if (hPage == NULL)
2736 {
2737 ERR("failed to create property sheet page\n");
2738 return E_FAIL;
2739 }
2740
2741 bRet = pfnAddPage(hPage, lParam);
2742 if (bRet)
2743 return S_OK;
2744 else
2745 return E_FAIL;
2746 }
2747
2748 static HRESULT WINAPI
2749 ShellLink_IShellPropSheetExt_ReplacePages( IShellPropSheetExt *iface, UINT uPageID, LPFNADDPROPSHEETPAGE pfnReplacePage, LPARAM lParam)
2750 {
2751 IShellLinkImpl *This = impl_from_IShellPropSheetExt(iface);
2752 TRACE("(%p) (uPageID %u, pfnReplacePage %p lParam %p\n", This, uPageID, pfnReplacePage, lParam);
2753 return E_NOTIMPL;
2754 }
2755
2756 static const IShellPropSheetExtVtbl pse =
2757 {
2758 ShellLink_IShellPropSheetExt_QueryInterface,
2759 ShellLink_IShellPropSheetExt_AddRef,
2760 ShellLink_IShellPropSheetExt_Release,
2761 ShellLink_IShellPropSheetExt_AddPages,
2762 ShellLink_IShellPropSheetExt_ReplacePages
2763 };
2764
2765 static HRESULT WINAPI
2766 ShellLink_InvokeCommand( IContextMenu* iface, LPCMINVOKECOMMANDINFO lpici )
2767 {
2768 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2769 static const WCHAR szOpen[] = { 'o','p','e','n',0 };
2770 static const WCHAR szCplOpen[] = { 'c','p','l','o','p','e','n',0 };
2771 SHELLEXECUTEINFOW sei;
2772 HWND hwnd = NULL; /* FIXME: get using interface set from IObjectWithSite */
2773 LPWSTR args = NULL;
2774 LPWSTR path = NULL;
2775 HRESULT r;
2776
2777 TRACE("%p %p\n", This, lpici );
2778
2779 if ( lpici->cbSize < sizeof (CMINVOKECOMMANDINFO) )
2780 return E_INVALIDARG;
2781
2782 r = IShellLinkW_Resolve( (IShellLinkW*)&(This->lpvtblw), hwnd, 0 );
2783 if ( FAILED( r ) )
2784 {
2785 TRACE("failed to resolve component with error 0x%08x", r);
2786 return r;
2787 }
2788 if ( This->sComponent )
2789 {
2790 path = shelllink_get_msi_component_path( This->sComponent );
2791 if (!path)
2792 return E_FAIL;
2793 }
2794 else
2795 path = strdupW( This->sPath );
2796
2797 if ( lpici->cbSize == sizeof (CMINVOKECOMMANDINFOEX) &&
2798 ( lpici->fMask & CMIC_MASK_UNICODE ) )
2799 {
2800 LPCMINVOKECOMMANDINFOEX iciex = (LPCMINVOKECOMMANDINFOEX) lpici;
2801 DWORD len = 2;
2802
2803 if ( This->sArgs )
2804 len += wcslen( This->sArgs );
2805 if ( iciex->lpParametersW )
2806 len += wcslen( iciex->lpParametersW );
2807
2808 args = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
2809 args[0] = 0;
2810 if ( This->sArgs )
2811 wcscat( args, This->sArgs );
2812 if ( iciex->lpParametersW )
2813 {
2814 static const WCHAR space[] = { ' ', 0 };
2815 wcscat( args, space );
2816 wcscat( args, iciex->lpParametersW );
2817 }
2818 }
2819
2820 memset( &sei, 0, sizeof sei );
2821 sei.cbSize = sizeof sei;
2822 sei.fMask = SEE_MASK_UNICODE | (lpici->fMask & (SEE_MASK_NOASYNC|SEE_MASK_ASYNCOK|SEE_MASK_FLAG_NO_UI));
2823 sei.lpFile = path;
2824 sei.nShow = This->iShowCmd;
2825 sei.lpDirectory = This->sWorkDir;
2826 sei.lpParameters = args;
2827 sei.lpVerb = szOpen;
2828
2829 // HACK for ShellExecuteExW
2830 if (!wcsstr(This->sPath, L".cpl"))
2831 sei.lpVerb = szOpen;
2832 else
2833 sei.lpVerb = szCplOpen;
2834
2835 if( ShellExecuteExW( &sei ) )
2836 r = S_OK;
2837 else
2838 r = E_FAIL;
2839
2840 HeapFree( GetProcessHeap(), 0, args );
2841 HeapFree( GetProcessHeap(), 0, path );
2842
2843 return r;
2844 }
2845
2846 static HRESULT WINAPI
2847 ShellLink_GetCommandString( IContextMenu* iface, UINT_PTR idCmd, UINT uType,
2848 UINT* pwReserved, LPSTR pszName, UINT cchMax )
2849 {
2850 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2851
2852 FIXME("%p %lu %u %p %p %u\n", This,
2853 idCmd, uType, pwReserved, pszName, cchMax );
2854
2855 return E_NOTIMPL;
2856 }
2857
2858 static const IContextMenuVtbl cmvt =
2859 {
2860 ShellLink_ContextMenu_QueryInterface,
2861 ShellLink_ContextMenu_AddRef,
2862 ShellLink_ContextMenu_Release,
2863 ShellLink_QueryContextMenu,
2864 ShellLink_InvokeCommand,
2865 ShellLink_GetCommandString
2866 };
2867
2868 static HRESULT WINAPI
2869 ShellLink_ObjectWithSite_QueryInterface( IObjectWithSite* iface, REFIID riid, void** ppvObject )
2870 {
2871 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2872 return ShellLink_QueryInterface( This, riid, ppvObject );
2873 }
2874
2875 static ULONG WINAPI
2876 ShellLink_ObjectWithSite_AddRef( IObjectWithSite* iface )
2877 {
2878 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2879 return ShellLink_AddRef( This );
2880 }
2881
2882 static ULONG WINAPI
2883 ShellLink_ObjectWithSite_Release( IObjectWithSite* iface )
2884 {
2885 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2886 return ShellLink_Release( This );
2887 }
2888
2889 static HRESULT WINAPI
2890 ShellLink_GetSite( IObjectWithSite *iface, REFIID iid, void ** ppvSite )
2891 {
2892 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2893
2894 TRACE("%p %s %p\n", This, debugstr_guid( iid ), ppvSite );
2895
2896 if ( !This->site )
2897 return E_FAIL;
2898 return IUnknown_QueryInterface( This->site, iid, ppvSite );
2899 }
2900
2901 static HRESULT WINAPI
2902 ShellLink_SetSite( IObjectWithSite *iface, IUnknown *punk )
2903 {
2904 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2905
2906 TRACE("%p %p\n", iface, punk);
2907
2908 if ( punk )
2909 IUnknown_AddRef( punk );
2910 This->site = punk;
2911
2912 return S_OK;
2913 }
2914
2915 static const IObjectWithSiteVtbl owsvt =
2916 {
2917 ShellLink_ObjectWithSite_QueryInterface,
2918 ShellLink_ObjectWithSite_AddRef,
2919 ShellLink_ObjectWithSite_Release,
2920 ShellLink_SetSite,
2921 ShellLink_GetSite,
2922 };