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