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