Use LMEM_ flags for LocalAlloc(), not GMEM_ (GlobalAlloc).
[wine] / dlls / 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  * NOTES
22  *   Nearly complete informations 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 "config.h"
36 #include "wine/port.h"
37
38 #include <ctype.h>
39 #include <string.h>
40 #include <stdarg.h>
41 #include <stdio.h>
42 #ifdef HAVE_UNISTD_H
43 # include <unistd.h>
44 #endif
45 #include <errno.h>
46 #include <limits.h>
47 #ifdef HAVE_SYS_WAIT_H
48 # include <sys/wait.h>
49 #endif
50
51 #define COBJMACROS
52
53 #include "wine/debug.h"
54 #include "winerror.h"
55 #include "windef.h"
56 #include "winbase.h"
57 #include "winnls.h"
58 #include "winreg.h"
59
60 #include "winuser.h"
61 #include "wingdi.h"
62 #include "shlobj.h"
63 #include "undocshell.h"
64
65 #include "pidl.h"
66 #include "shell32_main.h"
67 #include "shlguid.h"
68 #include "shlwapi.h"
69
70 #include "initguid.h"
71
72 WINE_DEFAULT_DEBUG_CHANNEL(shell);
73
74 DEFINE_GUID( SHELL32_AdvtShortcutProduct,
75        0x9db1186f,0x40df,0x11d1,0xaa,0x8c,0x00,0xc0,0x4f,0xb6,0x78,0x63);
76 DEFINE_GUID( SHELL32_AdvtShortcutComponent,
77        0x9db1186e,0x40df,0x11d1,0xaa,0x8c,0x00,0xc0,0x4f,0xb6,0x78,0x63);
78
79 /* link file formats */
80
81 /* flag1: lnk elements: simple link has 0x0B */
82 #define SCF_PIDL 1
83 #define SCF_LOCATION 2
84 #define SCF_DESCRIPTION 4
85 #define SCF_RELATIVE 8
86 #define SCF_WORKDIR 0x10
87 #define SCF_ARGS 0x20
88 #define SCF_CUSTOMICON 0x40
89 #define SCF_UNICODE 0x80
90 #define SCF_PRODUCT 0x800
91 #define SCF_COMPONENT 0x1000
92
93 #include "pshpack1.h"
94
95 typedef struct _LINK_HEADER
96 {
97         DWORD    dwSize;        /* 0x00 size of the header - 0x4c */
98         GUID     MagicGuid;     /* 0x04 is CLSID_ShellLink */
99         DWORD    dwFlags;       /* 0x14 describes elements following */
100         DWORD    dwFileAttr;    /* 0x18 attributes of the target file */
101         FILETIME Time1;         /* 0x1c */
102         FILETIME Time2;         /* 0x24 */
103         FILETIME Time3;         /* 0x2c */
104         DWORD    dwFileLength;  /* 0x34 File length */
105         DWORD    nIcon;         /* 0x38 icon number */
106         DWORD   fStartup;       /* 0x3c startup type */
107         DWORD   wHotKey;        /* 0x40 hotkey */
108         DWORD   Unknown5;       /* 0x44 */
109         DWORD   Unknown6;       /* 0x48 */
110 } LINK_HEADER, * PLINK_HEADER;
111
112 #define SHLINK_LOCAL  0
113 #define SHLINK_REMOTE 1
114
115 typedef struct _LOCATION_INFO
116 {
117     DWORD  dwTotalSize;
118     DWORD  dwHeaderSize;
119     DWORD  dwFlags;
120     DWORD  dwVolTableOfs;
121     DWORD  dwLocalPathOfs;
122     DWORD  dwNetworkVolTableOfs;
123     DWORD  dwFinalPathOfs;
124 } LOCATION_INFO;
125
126 typedef struct _LOCAL_VOLUME_INFO
127 {
128     DWORD dwSize;
129     DWORD dwType;
130     DWORD dwVolSerial;
131     DWORD dwVolLabelOfs;
132 } LOCAL_VOLUME_INFO;
133
134 typedef struct tagLINK_ADVERTISEINFO
135 {
136     DWORD size;
137     DWORD magic;
138     CHAR  bufA[MAX_PATH];
139     WCHAR bufW[MAX_PATH];
140 } LINK_ADVERTISEINFO;
141
142 typedef struct volume_info_t
143 {
144     DWORD type;
145     DWORD serial;
146     WCHAR label[12];  /* assume 8.3 */
147 } volume_info;
148
149 #include "poppack.h"
150
151 static IShellLinkAVtbl          slvt;
152 static IShellLinkWVtbl          slvtw;
153 static IPersistFileVtbl pfvt;
154 static IPersistStreamVtbl       psvt;
155
156 /* IShellLink Implementation */
157
158 typedef struct
159 {
160         IShellLinkAVtbl    *lpVtbl;
161         DWORD               ref;
162
163         IShellLinkWVtbl    *lpvtblw;
164         IPersistFileVtbl   *lpvtblPersistFile;
165         IPersistStreamVtbl *lpvtblPersistStream;
166
167         /* data structures according to the informations in the link */
168         LPITEMIDLIST    pPidl;
169         WORD            wHotKey;
170         SYSTEMTIME      time1;
171         SYSTEMTIME      time2;
172         SYSTEMTIME      time3;
173
174         DWORD         iShowCmd;
175         LPWSTR        sIcoPath;
176         INT           iIcoNdx;
177         LPWSTR        sPath;
178         LPWSTR        sArgs;
179         LPWSTR        sWorkDir;
180         LPWSTR        sDescription;
181         LPWSTR        sPathRel;
182         LPWSTR        sProduct;
183         LPWSTR        sComponent;
184         volume_info   volume;
185
186         BOOL            bDirty;
187 } IShellLinkImpl;
188
189 #define _IShellLinkW_Offset ((int)(&(((IShellLinkImpl*)0)->lpvtblw)))
190 #define _ICOM_THIS_From_IShellLinkW(class, name) class* This = (class*)(((char*)name)-_IShellLinkW_Offset)
191
192 #define _IPersistFile_Offset ((int)(&(((IShellLinkImpl*)0)->lpvtblPersistFile)))
193 #define _ICOM_THIS_From_IPersistFile(class, name) class* This = (class*)(((char*)name)-_IPersistFile_Offset)
194
195 #define _IPersistStream_Offset ((int)(&(((IShellLinkImpl*)0)->lpvtblPersistStream)))
196 #define _ICOM_THIS_From_IPersistStream(class, name) class* This = (class*)(((char*)name)-_IPersistStream_Offset)
197
198 static HRESULT ShellLink_UpdatePath(LPWSTR sPathRel, LPCWSTR path, LPCWSTR sWorkDir, LPWSTR* psPath);
199
200 /* strdup on the process heap */
201 inline static LPWSTR HEAP_strdupAtoW( HANDLE heap, DWORD flags, LPCSTR str)
202 {
203     INT len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
204     LPWSTR p = HeapAlloc( heap, flags, len*sizeof (WCHAR) );
205     if( !p )
206         return p;
207     MultiByteToWideChar( CP_ACP, 0, str, -1, p, len );
208     return p;
209 }
210
211 /**************************************************************************
212  *  IPersistFile_QueryInterface
213  */
214 static HRESULT WINAPI IPersistFile_fnQueryInterface(
215         IPersistFile* iface,
216         REFIID riid,
217         LPVOID *ppvObj)
218 {
219         _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
220
221         TRACE("(%p)\n",This);
222
223         return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvObj);
224 }
225
226 /******************************************************************************
227  * IPersistFile_AddRef
228  */
229 static ULONG WINAPI IPersistFile_fnAddRef(IPersistFile* iface)
230 {
231         _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
232
233         TRACE("(%p)->(count=%lu)\n",This,This->ref);
234
235         return IShellLinkA_AddRef((IShellLinkA*)This);
236 }
237 /******************************************************************************
238  * IPersistFile_Release
239  */
240 static ULONG WINAPI IPersistFile_fnRelease(IPersistFile* iface)
241 {
242         _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
243
244         TRACE("(%p)->(count=%lu)\n",This,This->ref);
245
246         return IShellLinkA_Release((IShellLinkA*)This);
247 }
248
249 static HRESULT WINAPI IPersistFile_fnGetClassID(IPersistFile* iface, CLSID *pClassID)
250 {
251         _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
252         FIXME("(%p)\n",This);
253         return NOERROR;
254 }
255 static HRESULT WINAPI IPersistFile_fnIsDirty(IPersistFile* iface)
256 {
257         _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
258
259         TRACE("(%p)\n",This);
260
261         if (This->bDirty)
262             return S_OK;
263
264         return S_FALSE;
265 }
266 static HRESULT WINAPI IPersistFile_fnLoad(IPersistFile* iface, LPCOLESTR pszFileName, DWORD dwMode)
267 {
268         _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
269         IPersistStream *StreamThis = (IPersistStream *)&This->lpvtblPersistStream;
270         HRESULT r;
271         IStream *stm;
272
273         TRACE("(%p, %s)\n",This, debugstr_w(pszFileName));
274
275         r = CreateStreamOnFile(pszFileName, dwMode, &stm);
276         if( SUCCEEDED( r ) )
277         {
278             r = IPersistStream_Load(StreamThis, stm);
279             ShellLink_UpdatePath(This->sPathRel, pszFileName, This->sWorkDir, &This->sPath);
280             IStream_Release( stm );
281             This->bDirty = FALSE;
282         }
283
284         return r;
285 }
286
287 static BOOL StartLinkProcessor( LPCOLESTR szLink )
288 {
289     static const WCHAR szFormat[] = {
290         'w','i','n','e','m','e','n','u','b','u','i','l','d','e','r','.','e','x','e',
291         ' ','-','r',' ','"','%','s','"',0 };
292     LONG len;
293     LPWSTR buffer;
294     STARTUPINFOW si;
295     PROCESS_INFORMATION pi;
296
297     len = sizeof(szFormat) + lstrlenW( szLink ) * sizeof(WCHAR);
298     buffer = HeapAlloc( GetProcessHeap(), 0, len );
299     if( !buffer )
300         return FALSE;
301
302     wsprintfW( buffer, szFormat, szLink );
303
304     TRACE("starting %s\n",debugstr_w(buffer));
305
306     memset(&si, 0, sizeof(si));
307     si.cb = sizeof(si);
308     if (!CreateProcessW( NULL, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) return FALSE;
309
310     /* wait for a while to throttle the creation of linker processes */
311     if( WAIT_OBJECT_0 != WaitForSingleObject( pi.hProcess, 10000 ) )
312         WARN("Timed out waiting for shell linker\n");
313
314     CloseHandle( pi.hProcess );
315     CloseHandle( pi.hThread );
316
317     return TRUE;
318 }
319
320 static HRESULT WINAPI IPersistFile_fnSave(IPersistFile* iface, LPCOLESTR pszFileName, BOOL fRemember)
321 {
322     _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
323     IPersistStream *StreamThis = (IPersistStream *)&This->lpvtblPersistStream;
324     HRESULT r;
325     IStream *stm;
326
327     TRACE("(%p)->(%s)\n",This,debugstr_w(pszFileName));
328
329     if (!pszFileName)
330         return E_FAIL;
331
332     r = CreateStreamOnFile(pszFileName, STGM_READWRITE | STGM_CREATE, &stm);
333     if( SUCCEEDED( r ) )
334     {
335         r = IPersistStream_Save(StreamThis, stm, FALSE);
336         IStream_Release( stm );
337
338         if( SUCCEEDED( r ) )
339         {
340             StartLinkProcessor( pszFileName );
341
342             This->bDirty = FALSE;
343         }
344         else
345         {
346             DeleteFileW( pszFileName );
347             WARN("Failed to create shortcut %s\n", debugstr_w(pszFileName) );
348         }
349     }
350
351     return r;
352 }
353
354 static HRESULT WINAPI IPersistFile_fnSaveCompleted(IPersistFile* iface, LPCOLESTR pszFileName)
355 {
356         _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
357         FIXME("(%p)->(%s)\n",This,debugstr_w(pszFileName));
358         return NOERROR;
359 }
360 static HRESULT WINAPI IPersistFile_fnGetCurFile(IPersistFile* iface, LPOLESTR *ppszFileName)
361 {
362         _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
363         FIXME("(%p)\n",This);
364         return NOERROR;
365 }
366
367 static IPersistFileVtbl pfvt =
368 {
369         IPersistFile_fnQueryInterface,
370         IPersistFile_fnAddRef,
371         IPersistFile_fnRelease,
372         IPersistFile_fnGetClassID,
373         IPersistFile_fnIsDirty,
374         IPersistFile_fnLoad,
375         IPersistFile_fnSave,
376         IPersistFile_fnSaveCompleted,
377         IPersistFile_fnGetCurFile
378 };
379
380 /************************************************************************
381  * IPersistStream_QueryInterface
382  */
383 static HRESULT WINAPI IPersistStream_fnQueryInterface(
384         IPersistStream* iface,
385         REFIID     riid,
386         VOID**     ppvoid)
387 {
388         _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
389
390         TRACE("(%p)\n",This);
391
392         return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvoid);
393 }
394
395 /************************************************************************
396  * IPersistStream_Release
397  */
398 static ULONG WINAPI IPersistStream_fnRelease(
399         IPersistStream* iface)
400 {
401         _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
402
403         TRACE("(%p)\n",This);
404
405         return IShellLinkA_Release((IShellLinkA*)This);
406 }
407
408 /************************************************************************
409  * IPersistStream_AddRef
410  */
411 static ULONG WINAPI IPersistStream_fnAddRef(
412         IPersistStream* iface)
413 {
414         _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
415
416         TRACE("(%p)\n",This);
417
418         return IShellLinkA_AddRef((IShellLinkA*)This);
419 }
420
421 /************************************************************************
422  * IPersistStream_GetClassID
423  *
424  */
425 static HRESULT WINAPI IPersistStream_fnGetClassID(
426         IPersistStream* iface,
427         CLSID* pClassID)
428 {
429         _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
430
431         TRACE("(%p)\n", This);
432
433         if (pClassID==0)
434           return E_POINTER;
435
436 /*      memcpy(pClassID, &CLSID_???, sizeof(CLSID_???)); */
437
438         return S_OK;
439 }
440
441 /************************************************************************
442  * IPersistStream_IsDirty (IPersistStream)
443  */
444 static HRESULT WINAPI IPersistStream_fnIsDirty(
445         IPersistStream*  iface)
446 {
447         _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
448
449         TRACE("(%p)\n", This);
450
451         return S_OK;
452 }
453
454
455 static HRESULT Stream_LoadString( IStream* stm, BOOL unicode, LPWSTR *pstr )
456 {
457     DWORD count;
458     USHORT len;
459     LPVOID temp;
460     LPWSTR str;
461     HRESULT r;
462
463     TRACE("%p\n", stm);
464
465     count = 0;
466     r = IStream_Read(stm, &len, sizeof(len), &count);
467     if ( FAILED (r) || ( count != sizeof(len) ) )
468         return E_FAIL;
469
470     if( unicode )
471         len *= sizeof (WCHAR);
472
473     TRACE("reading %d\n", len);
474     temp = HeapAlloc(GetProcessHeap(), 0, len+sizeof(WCHAR));
475     if( !temp )
476         return E_OUTOFMEMORY;
477     count = 0;
478     r = IStream_Read(stm, temp, len, &count);
479     if( FAILED (r) || ( count != len ) )
480     {
481         HeapFree( GetProcessHeap(), 0, temp );
482         return E_FAIL;
483     }
484
485     TRACE("read %s\n", debugstr_an(temp,len));
486
487     /* convert to unicode if necessary */
488     if( !unicode )
489     {
490         count = MultiByteToWideChar( CP_ACP, 0, (LPSTR) temp, len, NULL, 0 );
491         str = HeapAlloc( GetProcessHeap(), 0, (count+1)*sizeof (WCHAR) );
492         if( str )
493             MultiByteToWideChar( CP_ACP, 0, (LPSTR) temp, len, str, count );
494         HeapFree( GetProcessHeap(), 0, temp );
495     }
496     else
497     {
498         count /= 2;
499         str = (LPWSTR) temp;
500     }
501     str[count] = 0;
502
503     *pstr = str;
504
505     return S_OK;
506 }
507
508 static HRESULT Stream_ReadChunk( IStream* stm, LPVOID *data )
509 {
510     DWORD size;
511     ULONG count;
512     HRESULT r;
513     struct sized_chunk {
514         DWORD size;
515         unsigned char data[1];
516     } *chunk;
517
518     TRACE("%p\n",stm);
519
520     r = IStream_Read( stm, &size, sizeof(size), &count );
521     if( FAILED( r )  || count != sizeof(size) )
522         return E_FAIL;
523
524     chunk = HeapAlloc( GetProcessHeap(), 0, size );
525     if( !chunk )
526         return E_OUTOFMEMORY;
527
528     chunk->size = size;
529     r = IStream_Read( stm, chunk->data, size - sizeof(size), &count );
530     if( FAILED( r ) || count != (size - sizeof(size)) )
531     {
532         HeapFree( GetProcessHeap(), 0, chunk );
533         return E_FAIL;
534     }
535
536     TRACE("Read %ld bytes\n",chunk->size);
537
538     *data = (LPVOID) chunk;
539
540     return S_OK;
541 }
542
543 static BOOL Stream_LoadVolume( LOCAL_VOLUME_INFO *vol, volume_info *volume )
544 {
545     const int label_sz = sizeof volume->label/sizeof volume->label[0];
546     LPSTR label;
547     int len;
548
549     volume->serial = vol->dwVolSerial;
550     volume->type = vol->dwType;
551
552     if( !vol->dwVolLabelOfs )
553         return FALSE;
554     if( vol->dwSize <= vol->dwVolLabelOfs )
555         return FALSE;
556     len = vol->dwSize - vol->dwVolLabelOfs;
557
558     label = (LPSTR) vol;
559     label += vol->dwVolLabelOfs;
560     MultiByteToWideChar( CP_ACP, 0, label, len, volume->label, label_sz-1);
561
562     return TRUE;
563 }
564
565 static LPWSTR Stream_LoadPath( LPSTR p, DWORD maxlen )
566 {
567     int len = 0, wlen;
568     LPWSTR path;
569
570     while( p[len] && (len < maxlen) )
571         len++;
572
573     wlen = MultiByteToWideChar(CP_ACP, 0, p, len, NULL, 0);
574     path = HeapAlloc(GetProcessHeap(), 0, (wlen+1)*sizeof(WCHAR));
575     MultiByteToWideChar(CP_ACP, 0, p, len, path, wlen);
576     path[wlen] = 0;
577
578     return path;
579 }
580
581 static HRESULT Stream_LoadLocation( IStream *stm,
582                 volume_info *volume, LPWSTR *path )
583 {
584     unsigned char *p = NULL;
585     LOCATION_INFO *loc;
586     HRESULT r;
587     int n;
588
589     r = Stream_ReadChunk( stm, (LPVOID*) &p );
590     if( FAILED(r) )
591         return r;
592
593     loc = (LOCATION_INFO*) p;
594     if (loc->dwTotalSize < sizeof(LOCATION_INFO))
595     {
596         HeapFree( GetProcessHeap(), 0, p );
597         return E_FAIL;
598     }
599
600     /* if there's valid local volume information, load it */
601     if( loc->dwVolTableOfs && 
602        ((loc->dwVolTableOfs + sizeof(LOCAL_VOLUME_INFO)) <= loc->dwTotalSize) )
603     {
604         LOCAL_VOLUME_INFO *volume_info;
605
606         volume_info = (LOCAL_VOLUME_INFO*) &p[loc->dwVolTableOfs];
607         Stream_LoadVolume( volume_info, volume );
608     }
609
610     /* if there's a local path, load it */
611     n = loc->dwLocalPathOfs;
612     if( n && (n < loc->dwTotalSize) )
613         *path = Stream_LoadPath( &p[n], loc->dwTotalSize - n );
614
615     TRACE("type %ld serial %08lx name %s path %s\n", volume->type,
616           volume->serial, debugstr_w(volume->label), debugstr_w(*path));
617
618     HeapFree( GetProcessHeap(), 0, p );
619     return S_OK;
620 }
621
622 /*
623  *  The format of the advertised shortcut info seems to be:
624  *
625  *  Offset     Description
626  *  ------     -----------
627  *
628  *    0          Length of the block (4 bytes, usually 0x314)
629  *    4          tag (dword)
630  *    8          string data in ASCII
631  *    8+0x104    string data in UNICODE
632  *
633  * In the original Win32 implementation the buffers are not initialized
634  *  to zero, so data trailing the string is random garbage.
635  */
636 static HRESULT Stream_LoadAdvertiseInfo( IStream* stm, LPWSTR *str )
637 {
638     DWORD size;
639     ULONG count;
640     HRESULT r;
641     LINK_ADVERTISEINFO buffer;
642     
643     TRACE("%p\n",stm);
644
645     r = IStream_Read( stm, &buffer.size, sizeof (DWORD), &count );
646     if( FAILED( r ) )
647         return r;
648
649     /* make sure that we read the size of the structure even on error */
650     size = sizeof buffer - sizeof (DWORD);
651     if( buffer.size != sizeof buffer )
652     {
653         ERR("Ooops.  This structure is not as expected...\n");
654         return E_FAIL;
655     }
656
657     r = IStream_Read( stm, &buffer.magic, size, &count );
658     if( FAILED( r ) )
659         return r;
660
661     if( count != size )
662         return E_FAIL;
663
664     TRACE("magic %08lx  string = %s\n", buffer.magic, debugstr_w(buffer.bufW));
665
666     if( (buffer.magic&0xffff0000) != 0xa0000000 )
667     {
668         ERR("Unknown magic number %08lx in advertised shortcut\n", buffer.magic);
669         return E_FAIL;
670     }
671
672     *str = HeapAlloc( GetProcessHeap(), 0, 
673                      (strlenW(buffer.bufW)+1) * sizeof(WCHAR) );
674     strcpyW( *str, buffer.bufW );
675
676     return S_OK;
677 }
678
679 /************************************************************************
680  * IPersistStream_Load (IPersistStream)
681  */
682 static HRESULT WINAPI IPersistStream_fnLoad(
683         IPersistStream*  iface,
684     IStream*         stm)
685 {
686     LINK_HEADER hdr;
687     ULONG    dwBytesRead;
688     BOOL     unicode;
689     HRESULT  r;
690     DWORD    zero;
691
692     _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
693
694     TRACE("%p %p\n", This, stm);
695
696     if( !stm )
697           return STG_E_INVALIDPOINTER;
698
699     dwBytesRead = 0;
700     r = IStream_Read(stm, &hdr, sizeof(hdr), &dwBytesRead);
701     if( FAILED( r ) )
702         return r;
703
704     if( dwBytesRead != sizeof(hdr))
705         return E_FAIL;
706     if( hdr.dwSize != sizeof(hdr))
707         return E_FAIL;
708     if( !IsEqualIID(&hdr.MagicGuid, &CLSID_ShellLink) )
709         return E_FAIL;
710
711     /* free all the old stuff */
712     ILFree(This->pPidl);
713     This->pPidl = NULL;
714     memset( &This->volume, 0, sizeof This->volume );
715     HeapFree(GetProcessHeap(), 0, This->sPath);
716     This->sPath = NULL;
717     HeapFree(GetProcessHeap(), 0, This->sDescription);
718     This->sDescription = NULL;
719     HeapFree(GetProcessHeap(), 0, This->sPathRel);
720     This->sPathRel = NULL;
721     HeapFree(GetProcessHeap(), 0, This->sWorkDir);
722     This->sWorkDir = NULL;
723     HeapFree(GetProcessHeap(), 0, This->sArgs);
724     This->sArgs = NULL;
725     HeapFree(GetProcessHeap(), 0, This->sIcoPath);
726     This->sIcoPath = NULL;
727     HeapFree(GetProcessHeap(), 0, This->sProduct);
728     This->sProduct = NULL;
729     HeapFree(GetProcessHeap(), 0, This->sComponent);
730     This->sComponent = NULL;
731         
732     This->wHotKey = (WORD)hdr.wHotKey;
733     This->iIcoNdx = hdr.nIcon;
734     FileTimeToSystemTime (&hdr.Time1, &This->time1);
735     FileTimeToSystemTime (&hdr.Time2, &This->time2);
736     FileTimeToSystemTime (&hdr.Time3, &This->time3);
737     if (TRACE_ON(shell))
738     {
739         WCHAR sTemp[MAX_PATH];
740         GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE, &This->time1,
741                        NULL, sTemp, sizeof(sTemp)/sizeof(*sTemp));
742         TRACE("-- time1: %s\n", debugstr_w(sTemp) );
743         GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE, &This->time2,
744                        NULL, sTemp, sizeof(sTemp)/sizeof(*sTemp));
745         TRACE("-- time2: %s\n", debugstr_w(sTemp) );
746         GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE, &This->time3,
747                        NULL, sTemp, sizeof(sTemp)/sizeof(*sTemp));
748         TRACE("-- time3: %s\n", debugstr_w(sTemp) );
749     }
750
751     /* load all the new stuff */
752     if( hdr.dwFlags & SCF_PIDL )
753     {
754         r = ILLoadFromStream( stm, &This->pPidl );
755         if( FAILED( r ) )
756             return r;
757     }
758     pdump(This->pPidl);
759
760     /* load the location information */
761     if( hdr.dwFlags & SCF_LOCATION )
762         r = Stream_LoadLocation( stm, &This->volume, &This->sPath );
763     if( FAILED( r ) )
764         goto end;
765
766     unicode = hdr.dwFlags & SCF_UNICODE;
767     if( hdr.dwFlags & SCF_DESCRIPTION )
768     {
769         r = Stream_LoadString( stm, unicode, &This->sDescription );
770         TRACE("Description  -> %s\n",debugstr_w(This->sDescription));
771     }
772     if( FAILED( r ) )
773         goto end;
774
775     if( hdr.dwFlags & SCF_RELATIVE )
776     {
777         r = Stream_LoadString( stm, unicode, &This->sPathRel );
778         TRACE("Relative Path-> %s\n",debugstr_w(This->sPathRel));
779     }
780     if( FAILED( r ) )
781         goto end;
782
783     if( hdr.dwFlags & SCF_WORKDIR )
784     {
785         r = Stream_LoadString( stm, unicode, &This->sWorkDir );
786         TRACE("Working Dir  -> %s\n",debugstr_w(This->sWorkDir));
787     }
788     if( FAILED( r ) )
789         goto end;
790
791     if( hdr.dwFlags & SCF_ARGS )
792     {
793         r = Stream_LoadString( stm, unicode, &This->sArgs );
794         TRACE("Working Dir  -> %s\n",debugstr_w(This->sArgs));
795     }
796     if( FAILED( r ) )
797         goto end;
798
799     if( hdr.dwFlags & SCF_CUSTOMICON )
800     {
801         r = Stream_LoadString( stm, unicode, &This->sIcoPath );
802         TRACE("Icon file    -> %s\n",debugstr_w(This->sIcoPath));
803     }
804     if( FAILED( r ) )
805         goto end;
806
807     if( hdr.dwFlags & SCF_PRODUCT )
808     {
809         r = Stream_LoadAdvertiseInfo( stm, &This->sProduct );
810         TRACE("Product      -> %s\n",debugstr_w(This->sProduct));
811     }
812     if( FAILED( r ) )
813         goto end;
814
815     if( hdr.dwFlags & SCF_COMPONENT )
816     {
817         r = Stream_LoadAdvertiseInfo( stm, &This->sComponent );
818         TRACE("Component    -> %s\n",debugstr_w(This->sComponent));
819     }
820     if( FAILED( r ) )
821         goto end;
822
823     r = IStream_Read(stm, &zero, sizeof zero, &dwBytesRead);
824     if( FAILED( r ) || zero || dwBytesRead != sizeof zero )
825         ERR("Last word was not zero\n");
826
827     TRACE("OK\n");
828
829     pdump (This->pPidl);
830
831     return S_OK;
832 end:
833     return r;
834 }
835
836 /************************************************************************
837  * Stream_WriteString
838  *
839  * Helper function for IPersistStream_Save. Writes a unicode string 
840  *  with terminating nul byte to a stream, preceded by the its length.
841  */
842 static HRESULT Stream_WriteString( IStream* stm, LPCWSTR str )
843 {
844     USHORT len = lstrlenW( str ) + 1;
845     DWORD count;
846     HRESULT r;
847
848     r = IStream_Write( stm, &len, sizeof(len), &count );
849     if( FAILED( r ) )
850         return r;
851
852     len *= sizeof(WCHAR);
853
854     r = IStream_Write( stm, str, len, &count );
855     if( FAILED( r ) )
856         return r;
857
858     return S_OK;
859 }
860
861 /************************************************************************
862  * Stream_WriteLocationInfo
863  *
864  * Writes the location info to a stream
865  *
866  * FIXME: One day we might want to write the network volume information
867  *        and the final path.
868  *        Figure out how Windows deals with unicode paths here.
869  */
870 static HRESULT Stream_WriteLocationInfo( IStream* stm, LPCWSTR path,
871                                          volume_info *volume )
872 {
873     DWORD total_size, path_size, volume_info_size, label_size, final_path_size;
874     LOCAL_VOLUME_INFO *vol;
875     LOCATION_INFO *loc;
876     LPSTR szLabel, szPath, szFinalPath;
877     ULONG count = 0;
878
879     TRACE("%p %s %p\n", stm, debugstr_w(path), volume);
880
881     /* figure out the size of everything */
882     label_size = WideCharToMultiByte( CP_ACP, 0, volume->label, -1,
883                                       NULL, 0, NULL, NULL );
884     path_size = WideCharToMultiByte( CP_ACP, 0, path, -1,
885                                      NULL, 0, NULL, NULL );
886     volume_info_size = sizeof *vol + label_size;
887     final_path_size = 1;
888     total_size = sizeof *loc + volume_info_size + path_size + final_path_size;
889
890     /* create pointers to everything */
891     loc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, total_size);
892     vol = (LOCAL_VOLUME_INFO*) &loc[1];
893     szLabel = (LPSTR) &vol[1];
894     szPath = &szLabel[label_size];
895     szFinalPath = &szPath[path_size];
896
897     /* fill in the location information header */
898     loc->dwTotalSize = total_size;
899     loc->dwHeaderSize = sizeof (*loc);
900     loc->dwFlags = 1;
901     loc->dwVolTableOfs = sizeof (*loc);
902     loc->dwLocalPathOfs = sizeof (*loc) + volume_info_size;
903     loc->dwNetworkVolTableOfs = 0;
904     loc->dwFinalPathOfs = sizeof (*loc) + volume_info_size + path_size;
905
906     /* fill in the volume information */
907     vol->dwSize = volume_info_size;
908     vol->dwType = volume->type;
909     vol->dwVolSerial = volume->serial;
910     vol->dwVolLabelOfs = sizeof (*vol);
911
912     /* copy in the strings */
913     WideCharToMultiByte( CP_ACP, 0, volume->label, -1,
914                          szLabel, label_size, NULL, NULL );
915     WideCharToMultiByte( CP_ACP, 0, path, -1,
916                          szPath, path_size, NULL, NULL );
917     szFinalPath[0] = 0;
918
919     return IStream_Write( stm, loc, total_size, &count );
920 }
921
922 static HRESULT Stream_WriteAdvertiseInfo( IStream* stm, LPCWSTR string, DWORD magic )
923 {
924     ULONG count;
925     LINK_ADVERTISEINFO buffer;
926     
927     TRACE("%p\n",stm);
928
929     memset( &buffer, 0, sizeof buffer );
930     buffer.size = sizeof buffer;
931     buffer.magic = magic;
932     strncpyW( buffer.bufW, string, MAX_PATH );
933     WideCharToMultiByte(CP_ACP, 0, string, -1, buffer.bufA, MAX_PATH, NULL, NULL );
934
935     return IStream_Write( stm, &buffer, buffer.size, &count );
936 }
937
938 /************************************************************************
939  * IPersistStream_Save (IPersistStream)
940  *
941  * FIXME: makes assumptions about byte order
942  */
943 static HRESULT WINAPI IPersistStream_fnSave(
944         IPersistStream*  iface,
945         IStream*         stm,
946         BOOL             fClearDirty)
947 {
948     static const WCHAR wOpen[] = {'o','p','e','n',0};
949
950     LINK_HEADER header;
951     WCHAR   exePath[MAX_PATH];
952     ULONG   count;
953     DWORD   zero;
954     HRESULT r;
955
956     _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
957
958     TRACE("%p %p %x\n", This, stm, fClearDirty);
959
960     *exePath = '\0';
961
962     if (This->sPath)
963     {
964         SHELL_FindExecutable(NULL, This->sPath, wOpen, exePath, MAX_PATH,
965                              NULL, NULL, NULL, NULL);
966         /*
967          * windows can create lnk files to executables that do not exist yet
968          * so if the executable does not exist the just trust the path they
969          * gave us
970          */
971         if (!*exePath) strcpyW(exePath,This->sPath);
972     }
973
974     memset(&header, 0, sizeof(header));
975     header.dwSize = sizeof(header);
976     header.fStartup = This->iShowCmd;
977     memcpy(&header.MagicGuid, &CLSID_ShellLink, sizeof(header.MagicGuid) );
978
979     header.wHotKey = This->wHotKey;
980     header.nIcon = This->iIcoNdx;
981     header.dwFlags = SCF_UNICODE;   /* strings are in unicode */
982     if( This->pPidl )
983         header.dwFlags |= SCF_PIDL;
984     if( This->sPath )
985         header.dwFlags |= SCF_LOCATION;
986     if( This->sDescription )
987         header.dwFlags |= SCF_DESCRIPTION;
988     if( This->sWorkDir )
989         header.dwFlags |= SCF_WORKDIR;
990     if( This->sArgs )
991         header.dwFlags |= SCF_ARGS;
992     if( This->sIcoPath )
993         header.dwFlags |= SCF_CUSTOMICON;
994     if( This->sProduct )
995         header.dwFlags |= SCF_PRODUCT;
996     if( This->sComponent )
997         header.dwFlags |= SCF_COMPONENT;
998
999     SystemTimeToFileTime ( &This->time1, &header.Time1 );
1000     SystemTimeToFileTime ( &This->time2, &header.Time2 );
1001     SystemTimeToFileTime ( &This->time3, &header.Time3 );
1002
1003     /* write the Shortcut header */
1004     r = IStream_Write( stm, &header, sizeof(header), &count );
1005     if( FAILED( r ) )
1006     {
1007         ERR("Write failed at %d\n",__LINE__);
1008         return r;
1009     }
1010
1011     TRACE("Writing pidl \n");
1012
1013     /* write the PIDL to the shortcut */
1014     if( This->pPidl )
1015     {
1016         r = ILSaveToStream( stm, This->pPidl );
1017         if( FAILED( r ) )
1018         {
1019             ERR("Failed to write PIDL at %d\n",__LINE__);
1020             return r;
1021         }
1022     }
1023
1024     if( This->sPath )
1025         Stream_WriteLocationInfo( stm, exePath, &This->volume );
1026
1027     if( This->sDescription )
1028         r = Stream_WriteString( stm, This->sDescription );
1029
1030     if( This->sPathRel )
1031         r = Stream_WriteString( stm, This->sPathRel );
1032
1033     if( This->sWorkDir )
1034         r = Stream_WriteString( stm, This->sWorkDir );
1035
1036     if( This->sArgs )
1037         r = Stream_WriteString( stm, This->sArgs );
1038
1039     if( This->sIcoPath )
1040         r = Stream_WriteString( stm, This->sIcoPath );
1041
1042     if( This->sProduct )
1043         r = Stream_WriteAdvertiseInfo( stm, This->sProduct, 0xa0000007 );
1044
1045     if( This->sComponent )
1046         r = Stream_WriteAdvertiseInfo( stm, This->sComponent, 0xa0000006 );
1047
1048     /* the last field is a single zero dword */
1049     zero = 0;
1050     r = IStream_Write( stm, &zero, sizeof zero, &count );
1051
1052     return S_OK;
1053 }
1054
1055 /************************************************************************
1056  * IPersistStream_GetSizeMax (IPersistStream)
1057  */
1058 static HRESULT WINAPI IPersistStream_fnGetSizeMax(
1059         IPersistStream*  iface,
1060         ULARGE_INTEGER*  pcbSize)
1061 {
1062         _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
1063
1064         TRACE("(%p)\n", This);
1065
1066         return E_NOTIMPL;
1067 }
1068
1069 static IPersistStreamVtbl psvt =
1070 {
1071         IPersistStream_fnQueryInterface,
1072         IPersistStream_fnAddRef,
1073         IPersistStream_fnRelease,
1074         IPersistStream_fnGetClassID,
1075         IPersistStream_fnIsDirty,
1076         IPersistStream_fnLoad,
1077         IPersistStream_fnSave,
1078         IPersistStream_fnGetSizeMax
1079 };
1080
1081 /**************************************************************************
1082  *        IShellLink_Constructor
1083  */
1084 HRESULT WINAPI IShellLink_Constructor (
1085         IUnknown * pUnkOuter,
1086         REFIID riid,
1087         LPVOID * ppv)
1088 {
1089         IShellLinkImpl * sl;
1090
1091         TRACE("unkOut=%p riid=%s\n",pUnkOuter, debugstr_guid(riid));
1092
1093         *ppv = NULL;
1094
1095         if(pUnkOuter) return CLASS_E_NOAGGREGATION;
1096         sl = (IShellLinkImpl *) LocalAlloc(LMEM_ZEROINIT,sizeof(IShellLinkImpl));
1097         if (!sl) return E_OUTOFMEMORY;
1098
1099         sl->ref = 1;
1100         sl->lpVtbl = &slvt;
1101         sl->lpvtblw = &slvtw;
1102         sl->lpvtblPersistFile = &pfvt;
1103         sl->lpvtblPersistStream = &psvt;
1104         sl->iShowCmd = SW_SHOWNORMAL;
1105         sl->bDirty = FALSE;
1106
1107         TRACE("(%p)->()\n",sl);
1108
1109         if (IsEqualIID(riid, &IID_IUnknown) ||
1110             IsEqualIID(riid, &IID_IShellLinkA))
1111             *ppv = sl;
1112         else if (IsEqualIID(riid, &IID_IShellLinkW))
1113             *ppv = &(sl->lpvtblw);
1114         else {
1115             LocalFree((HLOCAL)sl);
1116             ERR("E_NOINTERFACE\n");
1117             return E_NOINTERFACE;
1118         }
1119
1120         return S_OK;
1121 }
1122
1123
1124 static BOOL SHELL_ExistsFileW(LPCWSTR path)
1125 {
1126     if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW(path))
1127         return FALSE;
1128     return TRUE;
1129 }
1130
1131 /**************************************************************************
1132  *  ShellLink_UpdatePath
1133  *      update absolute path in sPath using relative path in sPathRel
1134  */
1135 static HRESULT ShellLink_UpdatePath(LPWSTR sPathRel, LPCWSTR path, LPCWSTR sWorkDir, LPWSTR* psPath)
1136 {
1137     if (!path || !psPath)
1138         return E_INVALIDARG;
1139
1140     if (!*psPath && sPathRel) {
1141         WCHAR buffer[2*MAX_PATH], abs_path[2*MAX_PATH];
1142         LPWSTR final = NULL;
1143
1144         /* first try if [directory of link file] + [relative path] finds an existing file */
1145
1146         GetFullPathNameW( path, MAX_PATH*2, buffer, &final );
1147         if( !final )
1148             final = buffer;
1149         lstrcpyW(final, sPathRel);
1150
1151         *abs_path = '\0';
1152
1153         if (SHELL_ExistsFileW(buffer)) {
1154             if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
1155                 lstrcpyW(abs_path, buffer);
1156         } else {
1157             /* try if [working directory] + [relative path] finds an existing file */
1158             if (sWorkDir) {
1159                 lstrcpyW(buffer, sWorkDir);
1160                 lstrcpyW(PathAddBackslashW(buffer), sPathRel);
1161
1162                 if (SHELL_ExistsFileW(buffer))
1163                     if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
1164                         lstrcpyW(abs_path, buffer);
1165             }
1166         }
1167
1168         /* FIXME: This is even not enough - not all shell links can be resolved using this algorithm. */
1169         if (!*abs_path)
1170             lstrcpyW(abs_path, sPathRel);
1171
1172         *psPath = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(abs_path)+1)*sizeof(WCHAR));
1173         if (!*psPath)
1174             return E_OUTOFMEMORY;
1175
1176         lstrcpyW(*psPath, abs_path);
1177     }
1178
1179     return S_OK;
1180 }
1181
1182 /**************************************************************************
1183  *        IShellLink_ConstructFromFile
1184  */
1185 HRESULT WINAPI IShellLink_ConstructFromFile (
1186         IUnknown* pUnkOuter,
1187         REFIID riid,
1188         LPCITEMIDLIST pidl,
1189         LPVOID* ppv
1190 )
1191 {
1192     IShellLinkW* psl;
1193
1194     HRESULT hr = IShellLink_Constructor(NULL, riid, (LPVOID*)&psl);
1195
1196     if (SUCCEEDED(hr)) {
1197         IPersistFile* ppf;
1198
1199         *ppv = NULL;
1200
1201         hr = IShellLinkW_QueryInterface(psl, &IID_IPersistFile, (LPVOID*)&ppf);
1202
1203         if (SUCCEEDED(hr)) {
1204             WCHAR path[MAX_PATH];
1205
1206             if (SHGetPathFromIDListW(pidl, path)) 
1207                 hr = IPersistFile_Load(ppf, path, 0);
1208             else
1209                 hr = E_FAIL;
1210
1211             if (SUCCEEDED(hr))
1212                 *ppv = (IUnknown*) psl;
1213
1214             IPersistFile_Release(ppf);
1215         }
1216
1217         if (!*ppv)
1218             IShellLinkW_Release(psl);
1219     }
1220
1221     return hr;
1222 }
1223
1224 /**************************************************************************
1225  *  IShellLinkA_QueryInterface
1226  */
1227 static HRESULT WINAPI IShellLinkA_fnQueryInterface( IShellLinkA * iface, REFIID riid,  LPVOID *ppvObj)
1228 {
1229         IShellLinkImpl *This = (IShellLinkImpl *)iface;
1230
1231         TRACE("(%p)->(\n\tIID:\t%s)\n",This,debugstr_guid(riid));
1232
1233         *ppvObj = NULL;
1234
1235         if(IsEqualIID(riid, &IID_IUnknown) ||
1236            IsEqualIID(riid, &IID_IShellLinkA))
1237         {
1238           *ppvObj = This;
1239         }
1240         else if(IsEqualIID(riid, &IID_IShellLinkW))
1241         {
1242           *ppvObj = (IShellLinkW *)&(This->lpvtblw);
1243         }
1244         else if(IsEqualIID(riid, &IID_IPersistFile))
1245         {
1246           *ppvObj = (IPersistFile *)&(This->lpvtblPersistFile);
1247         }
1248         else if(IsEqualIID(riid, &IID_IPersistStream))
1249         {
1250           *ppvObj = (IPersistStream *)&(This->lpvtblPersistStream);
1251         }
1252
1253         if(*ppvObj)
1254         {
1255           IUnknown_AddRef((IUnknown*)(*ppvObj));
1256           TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
1257           return S_OK;
1258         }
1259         TRACE("-- Interface: E_NOINTERFACE\n");
1260         return E_NOINTERFACE;
1261 }
1262 /******************************************************************************
1263  * IShellLinkA_AddRef
1264  */
1265 static ULONG WINAPI IShellLinkA_fnAddRef(IShellLinkA * iface)
1266 {
1267         IShellLinkImpl *This = (IShellLinkImpl *)iface;
1268         ULONG refCount = InterlockedIncrement(&This->ref);
1269
1270         TRACE("(%p)->(count=%lu)\n", This, refCount - 1);
1271
1272         return refCount;
1273 }
1274 /******************************************************************************
1275  *      IShellLinkA_Release
1276  */
1277 static ULONG WINAPI IShellLinkA_fnRelease(IShellLinkA * iface)
1278 {
1279     IShellLinkImpl *This = (IShellLinkImpl *)iface;
1280     ULONG refCount = InterlockedDecrement(&This->ref);
1281
1282     TRACE("(%p)->(count=%lu)\n", This, refCount + 1);
1283
1284     if (refCount)
1285         return refCount;
1286
1287     TRACE("-- destroying IShellLink(%p)\n",This);
1288
1289     HeapFree(GetProcessHeap(), 0, This->sIcoPath);
1290     HeapFree(GetProcessHeap(), 0, This->sArgs);
1291     HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1292     HeapFree(GetProcessHeap(), 0, This->sDescription);
1293     HeapFree(GetProcessHeap(),0,This->sPath);
1294
1295     if (This->pPidl)
1296         ILFree(This->pPidl);
1297
1298     LocalFree((HANDLE)This);
1299
1300     return 0;
1301 }
1302
1303 static HRESULT WINAPI IShellLinkA_fnGetPath(IShellLinkA * iface, LPSTR pszFile,
1304                   INT cchMaxPath, WIN32_FIND_DATAA *pfd, DWORD fFlags)
1305 {
1306     IShellLinkImpl *This = (IShellLinkImpl *)iface;
1307
1308     TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%lu)(%s)\n",
1309           This, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(This->sPath));
1310
1311     if (This->sComponent || This->sProduct)
1312         return S_FALSE;
1313
1314     if (cchMaxPath)
1315         pszFile[0] = 0;
1316     if (This->sPath)
1317         WideCharToMultiByte( CP_ACP, 0, This->sPath, -1,
1318                              pszFile, cchMaxPath, NULL, NULL);
1319
1320     if (pfd) FIXME("(%p): WIN32_FIND_DATA is not yet filled.\n", This);
1321
1322     return S_OK;
1323 }
1324
1325 static HRESULT WINAPI IShellLinkA_fnGetIDList(IShellLinkA * iface, LPITEMIDLIST * ppidl)
1326 {
1327     IShellLinkImpl *This = (IShellLinkImpl *)iface;
1328
1329     TRACE("(%p)->(ppidl=%p)\n",This, ppidl);
1330
1331     return IShellLinkW_GetIDList((IShellLinkW*)&(This->lpvtblw), ppidl);
1332 }
1333
1334 static HRESULT WINAPI IShellLinkA_fnSetIDList(IShellLinkA * iface, LPCITEMIDLIST pidl)
1335 {
1336     IShellLinkImpl *This = (IShellLinkImpl *)iface;
1337
1338     TRACE("(%p)->(pidl=%p)\n",This, pidl);
1339
1340     if (This->pPidl)
1341         ILFree(This->pPidl);
1342     This->pPidl = ILClone (pidl);
1343     This->bDirty = TRUE;
1344
1345     return S_OK;
1346 }
1347
1348 static HRESULT WINAPI IShellLinkA_fnGetDescription(IShellLinkA * iface, LPSTR pszName,INT cchMaxName)
1349 {
1350     IShellLinkImpl *This = (IShellLinkImpl *)iface;
1351
1352     TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1353
1354     if( cchMaxName )
1355         pszName[0] = 0;
1356     if( This->sDescription )
1357         WideCharToMultiByte( CP_ACP, 0, This->sDescription, -1,
1358             pszName, cchMaxName, NULL, NULL);
1359
1360     return S_OK;
1361 }
1362 static HRESULT WINAPI IShellLinkA_fnSetDescription(IShellLinkA * iface, LPCSTR pszName)
1363 {
1364     IShellLinkImpl *This = (IShellLinkImpl *)iface;
1365
1366     TRACE("(%p)->(pName=%s)\n", This, pszName);
1367
1368     HeapFree(GetProcessHeap(), 0, This->sDescription);
1369     This->sDescription = HEAP_strdupAtoW( GetProcessHeap(), 0, pszName);
1370     if ( !This->sDescription )
1371         return E_OUTOFMEMORY;
1372
1373     This->bDirty = TRUE;
1374
1375     return S_OK;
1376 }
1377
1378 static HRESULT WINAPI IShellLinkA_fnGetWorkingDirectory(IShellLinkA * iface, LPSTR pszDir,INT cchMaxPath)
1379 {
1380     IShellLinkImpl *This = (IShellLinkImpl *)iface;
1381
1382     TRACE("(%p)->(%p len=%u)\n", This, pszDir, cchMaxPath);
1383
1384     if( cchMaxPath )
1385         pszDir[0] = 0;
1386     if( This->sWorkDir )
1387         WideCharToMultiByte( CP_ACP, 0, This->sWorkDir, -1,
1388                              pszDir, cchMaxPath, NULL, NULL);
1389
1390     return S_OK;
1391 }
1392
1393 static HRESULT WINAPI IShellLinkA_fnSetWorkingDirectory(IShellLinkA * iface, LPCSTR pszDir)
1394 {
1395     IShellLinkImpl *This = (IShellLinkImpl *)iface;
1396
1397     TRACE("(%p)->(dir=%s)\n",This, pszDir);
1398
1399     HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1400     This->sWorkDir = HEAP_strdupAtoW( GetProcessHeap(), 0, pszDir);
1401     if ( !This->sWorkDir )
1402         return E_OUTOFMEMORY;
1403
1404     This->bDirty = TRUE;
1405
1406     return S_OK;
1407 }
1408
1409 static HRESULT WINAPI IShellLinkA_fnGetArguments(IShellLinkA * iface, LPSTR pszArgs,INT cchMaxPath)
1410 {
1411     IShellLinkImpl *This = (IShellLinkImpl *)iface;
1412
1413     TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1414
1415     if( cchMaxPath )
1416         pszArgs[0] = 0;
1417     if( This->sArgs )
1418         WideCharToMultiByte( CP_ACP, 0, This->sArgs, -1,
1419                              pszArgs, cchMaxPath, NULL, NULL);
1420
1421     return S_OK;
1422 }
1423
1424 static HRESULT WINAPI IShellLinkA_fnSetArguments(IShellLinkA * iface, LPCSTR pszArgs)
1425 {
1426     IShellLinkImpl *This = (IShellLinkImpl *)iface;
1427
1428     TRACE("(%p)->(args=%s)\n",This, pszArgs);
1429
1430     HeapFree(GetProcessHeap(), 0, This->sArgs);
1431     This->sArgs = HEAP_strdupAtoW( GetProcessHeap(), 0, pszArgs);
1432     if( !This->sArgs )
1433         return E_OUTOFMEMORY;
1434
1435     This->bDirty = TRUE;
1436
1437     return S_OK;
1438 }
1439
1440 static HRESULT WINAPI IShellLinkA_fnGetHotkey(IShellLinkA * iface, WORD *pwHotkey)
1441 {
1442         IShellLinkImpl *This = (IShellLinkImpl *)iface;
1443
1444         TRACE("(%p)->(%p)(0x%08x)\n",This, pwHotkey, This->wHotKey);
1445
1446         *pwHotkey = This->wHotKey;
1447
1448         return S_OK;
1449 }
1450
1451 static HRESULT WINAPI IShellLinkA_fnSetHotkey(IShellLinkA * iface, WORD wHotkey)
1452 {
1453         IShellLinkImpl *This = (IShellLinkImpl *)iface;
1454
1455         TRACE("(%p)->(hotkey=%x)\n",This, wHotkey);
1456
1457         This->wHotKey = wHotkey;
1458         This->bDirty = TRUE;
1459
1460         return S_OK;
1461 }
1462
1463 static HRESULT WINAPI IShellLinkA_fnGetShowCmd(IShellLinkA * iface, INT *piShowCmd)
1464 {
1465     IShellLinkImpl *This = (IShellLinkImpl *)iface;
1466
1467     TRACE("(%p)->(%p)\n",This, piShowCmd);
1468     *piShowCmd = This->iShowCmd;
1469     return S_OK;
1470 }
1471
1472 static HRESULT WINAPI IShellLinkA_fnSetShowCmd(IShellLinkA * iface, INT iShowCmd)
1473 {
1474     IShellLinkImpl *This = (IShellLinkImpl *)iface;
1475
1476     TRACE("(%p) %d\n",This, iShowCmd);
1477
1478     This->iShowCmd = iShowCmd;
1479     This->bDirty = TRUE;
1480
1481     return NOERROR;
1482 }
1483
1484 static HRESULT SHELL_PidlGeticonLocationA(IShellFolder* psf, LPITEMIDLIST pidl, LPSTR pszIconPath, int cchIconPath, int* piIcon)
1485 {
1486     LPCITEMIDLIST pidlLast;
1487
1488     HRESULT hr = SHBindToParent(pidl, &IID_IShellFolder, (LPVOID*)&psf, &pidlLast);
1489
1490     if (SUCCEEDED(hr)) {
1491         IExtractIconA* pei;
1492
1493         hr = IShellFolder_GetUIObjectOf(psf, 0, 1, (LPCITEMIDLIST*)&pidlLast, &IID_IExtractIconA, NULL, (LPVOID*)&pei);
1494
1495         if (SUCCEEDED(hr)) {
1496             hr = IExtractIconA_GetIconLocation(pei, 0, pszIconPath, MAX_PATH, piIcon, NULL);
1497
1498             IExtractIconA_Release(pei);
1499         }
1500
1501         IShellFolder_Release(psf);
1502     }
1503
1504     return hr;
1505 }
1506
1507 static HRESULT WINAPI IShellLinkA_fnGetIconLocation(IShellLinkA * iface, LPSTR pszIconPath,INT cchIconPath,INT *piIcon)
1508 {
1509     IShellLinkImpl *This = (IShellLinkImpl *)iface;
1510
1511     TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1512
1513     pszIconPath[0] = 0;
1514     *piIcon = This->iIcoNdx;
1515
1516     if (This->sIcoPath)
1517     {
1518         WideCharToMultiByte(CP_ACP, 0, This->sIcoPath, -1, pszIconPath, cchIconPath, NULL, NULL);
1519         return S_OK;
1520     }
1521
1522     if (This->pPidl || This->sPath)
1523     {
1524         IShellFolder* pdsk;
1525
1526         HRESULT hr = SHGetDesktopFolder(&pdsk);
1527
1528         if (SUCCEEDED(hr))
1529         {
1530             /* first look for an icon using the PIDL (if present) */
1531             if (This->pPidl)
1532                 hr = SHELL_PidlGeticonLocationA(pdsk, This->pPidl, pszIconPath, cchIconPath, piIcon);
1533             else
1534                 hr = E_FAIL;
1535
1536             /* if we couldn't find an icon yet, look for it using the file system path */
1537             if (FAILED(hr) && This->sPath)
1538             {
1539                 LPITEMIDLIST pidl;
1540
1541                 hr = IShellFolder_ParseDisplayName(pdsk, 0, NULL, This->sPath, NULL, &pidl, NULL);
1542
1543                 if (SUCCEEDED(hr)) {
1544                     hr = SHELL_PidlGeticonLocationA(pdsk, pidl, pszIconPath, cchIconPath, piIcon);
1545
1546                     SHFree(pidl);
1547                 }
1548             }
1549
1550             IShellFolder_Release(pdsk);
1551         }
1552
1553         return hr;
1554     }
1555     return S_OK;
1556 }
1557
1558 static HRESULT WINAPI IShellLinkA_fnSetIconLocation(IShellLinkA * iface, LPCSTR pszIconPath,INT iIcon)
1559 {
1560     IShellLinkImpl *This = (IShellLinkImpl *)iface;
1561
1562     TRACE("(%p)->(path=%s iicon=%u)\n",This, pszIconPath, iIcon);
1563
1564     HeapFree(GetProcessHeap(), 0, This->sIcoPath);
1565     This->sIcoPath = HEAP_strdupAtoW(GetProcessHeap(), 0, pszIconPath);
1566     if ( !This->sIcoPath )
1567         return E_OUTOFMEMORY;
1568
1569     This->iIcoNdx = iIcon;
1570     This->bDirty = TRUE;
1571
1572     return S_OK;
1573 }
1574
1575 static HRESULT WINAPI IShellLinkA_fnSetRelativePath(IShellLinkA * iface, LPCSTR pszPathRel, DWORD dwReserved)
1576 {
1577     IShellLinkImpl *This = (IShellLinkImpl *)iface;
1578
1579     TRACE("(%p)->(path=%s %lx)\n",This, pszPathRel, dwReserved);
1580
1581     HeapFree(GetProcessHeap(), 0, This->sPathRel);
1582     This->sPathRel = HEAP_strdupAtoW(GetProcessHeap(), 0, pszPathRel);
1583     This->bDirty = TRUE;
1584
1585     return ShellLink_UpdatePath(This->sPathRel, This->sPath, This->sWorkDir, &This->sPath);
1586 }
1587
1588 static HRESULT WINAPI IShellLinkA_fnResolve(IShellLinkA * iface, HWND hwnd, DWORD fFlags)
1589 {
1590     HRESULT hr = S_OK;
1591
1592     IShellLinkImpl *This = (IShellLinkImpl *)iface;
1593
1594     TRACE("(%p)->(hwnd=%p flags=%lx)\n",This, hwnd, fFlags);
1595
1596     /*FIXME: use IResolveShellLink interface */
1597
1598     if (!This->sPath && This->pPidl) {
1599         WCHAR buffer[MAX_PATH];
1600
1601         hr = SHELL_GetPathFromIDListW(This->pPidl, buffer, MAX_PATH);
1602
1603         if (SUCCEEDED(hr) && *buffer) {
1604             This->sPath = (LPWSTR) HeapAlloc(GetProcessHeap(), 0, (lstrlenW(buffer)+1)*sizeof(WCHAR));
1605             if (!This->sPath)
1606                 return E_OUTOFMEMORY;
1607
1608             lstrcpyW(This->sPath, buffer);
1609
1610             This->bDirty = TRUE;
1611         } else
1612             hr = S_OK;    /* don't report an error occurred while just caching information */
1613     }
1614
1615     if (!This->sIcoPath && This->sPath) {
1616         This->sIcoPath = (LPWSTR) HeapAlloc(GetProcessHeap(), 0, (lstrlenW(This->sPath)+1)*sizeof(WCHAR));
1617         if (!This->sIcoPath)
1618             return E_OUTOFMEMORY;
1619
1620         lstrcpyW(This->sIcoPath, This->sPath);
1621         This->iIcoNdx = 0;
1622
1623         This->bDirty = TRUE;
1624     }
1625
1626     return hr;
1627 }
1628
1629 static HRESULT WINAPI IShellLinkA_fnSetPath(IShellLinkA * iface, LPCSTR pszFile)
1630 {
1631     HRESULT r;
1632     LPWSTR str;
1633     IShellLinkImpl *This = (IShellLinkImpl *)iface;
1634
1635     TRACE("(%p)->(path=%s)\n",This, pszFile);
1636
1637     str = HEAP_strdupAtoW(GetProcessHeap(), 0, pszFile);
1638     if( !str ) 
1639         return E_OUTOFMEMORY;
1640
1641     r = IShellLinkW_SetPath((IShellLinkW*)&(This->lpvtblw), str);
1642     HeapFree( GetProcessHeap(), 0, str );
1643
1644     return r;
1645 }
1646
1647 /**************************************************************************
1648 * IShellLink Implementation
1649 */
1650
1651 static IShellLinkAVtbl slvt =
1652 {
1653         IShellLinkA_fnQueryInterface,
1654         IShellLinkA_fnAddRef,
1655         IShellLinkA_fnRelease,
1656         IShellLinkA_fnGetPath,
1657         IShellLinkA_fnGetIDList,
1658         IShellLinkA_fnSetIDList,
1659         IShellLinkA_fnGetDescription,
1660         IShellLinkA_fnSetDescription,
1661         IShellLinkA_fnGetWorkingDirectory,
1662         IShellLinkA_fnSetWorkingDirectory,
1663         IShellLinkA_fnGetArguments,
1664         IShellLinkA_fnSetArguments,
1665         IShellLinkA_fnGetHotkey,
1666         IShellLinkA_fnSetHotkey,
1667         IShellLinkA_fnGetShowCmd,
1668         IShellLinkA_fnSetShowCmd,
1669         IShellLinkA_fnGetIconLocation,
1670         IShellLinkA_fnSetIconLocation,
1671         IShellLinkA_fnSetRelativePath,
1672         IShellLinkA_fnResolve,
1673         IShellLinkA_fnSetPath
1674 };
1675
1676
1677 /**************************************************************************
1678  *  IShellLinkW_fnQueryInterface
1679  */
1680 static HRESULT WINAPI IShellLinkW_fnQueryInterface(
1681   IShellLinkW * iface, REFIID riid, LPVOID *ppvObj)
1682 {
1683         _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1684
1685         return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvObj);
1686 }
1687
1688 /******************************************************************************
1689  * IShellLinkW_fnAddRef
1690  */
1691 static ULONG WINAPI IShellLinkW_fnAddRef(IShellLinkW * iface)
1692 {
1693         _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1694
1695         TRACE("(%p)->(count=%lu)\n",This,This->ref);
1696
1697         return IShellLinkA_AddRef((IShellLinkA*)This);
1698 }
1699 /******************************************************************************
1700  * IShellLinkW_fnRelease
1701  */
1702
1703 static ULONG WINAPI IShellLinkW_fnRelease(IShellLinkW * iface)
1704 {
1705         _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1706
1707         TRACE("(%p)->(count=%lu)\n",This,This->ref);
1708
1709         return IShellLinkA_Release((IShellLinkA*)This);
1710 }
1711
1712 static HRESULT WINAPI IShellLinkW_fnGetPath(IShellLinkW * iface, LPWSTR pszFile,INT cchMaxPath, WIN32_FIND_DATAW *pfd, DWORD fFlags)
1713 {
1714     _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1715
1716     TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%lu)(%s)\n",
1717           This, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(This->sPath));
1718
1719     if (This->sComponent || This->sProduct)
1720         return S_FALSE;
1721
1722     if (cchMaxPath)
1723         pszFile[0] = 0;
1724     if (This->sPath)
1725         lstrcpynW( pszFile, This->sPath, cchMaxPath );
1726
1727     if (pfd) FIXME("(%p): WIN32_FIND_DATA is not yet filled.\n", This);
1728
1729     return S_OK;
1730 }
1731
1732 static HRESULT WINAPI IShellLinkW_fnGetIDList(IShellLinkW * iface, LPITEMIDLIST * ppidl)
1733 {
1734     _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1735
1736     TRACE("(%p)->(ppidl=%p)\n",This, ppidl);
1737
1738     if (!This->pPidl)
1739         return S_FALSE;
1740     *ppidl = ILClone(This->pPidl);
1741     return S_OK;
1742 }
1743
1744 static HRESULT WINAPI IShellLinkW_fnSetIDList(IShellLinkW * iface, LPCITEMIDLIST pidl)
1745 {
1746     _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1747
1748     TRACE("(%p)->(pidl=%p)\n",This, pidl);
1749
1750     if( This->pPidl )
1751         ILFree( This->pPidl );
1752     This->pPidl = ILClone( pidl );
1753     if( !This->pPidl )
1754         return E_FAIL;
1755
1756     This->bDirty = TRUE;
1757
1758     return S_OK;
1759 }
1760
1761 static HRESULT WINAPI IShellLinkW_fnGetDescription(IShellLinkW * iface, LPWSTR pszName,INT cchMaxName)
1762 {
1763     _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1764
1765     TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1766
1767     pszName[0] = 0;
1768     if( This->sDescription )
1769         lstrcpynW( pszName, This->sDescription, cchMaxName );
1770
1771     return S_OK;
1772 }
1773
1774 static HRESULT WINAPI IShellLinkW_fnSetDescription(IShellLinkW * iface, LPCWSTR pszName)
1775 {
1776     _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1777
1778     TRACE("(%p)->(desc=%s)\n",This, debugstr_w(pszName));
1779
1780     HeapFree(GetProcessHeap(), 0, This->sDescription);
1781     This->sDescription = HeapAlloc( GetProcessHeap(), 0,
1782                                     (lstrlenW( pszName )+1)*sizeof(WCHAR) );
1783     if ( !This->sDescription )
1784         return E_OUTOFMEMORY;
1785
1786     lstrcpyW( This->sDescription, pszName );
1787     This->bDirty = TRUE;
1788
1789     return S_OK;
1790 }
1791
1792 static HRESULT WINAPI IShellLinkW_fnGetWorkingDirectory(IShellLinkW * iface, LPWSTR pszDir,INT cchMaxPath)
1793 {
1794     _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1795
1796     TRACE("(%p)->(%p len %u)\n", This, pszDir, cchMaxPath);
1797
1798     if( cchMaxPath )
1799         pszDir[0] = 0;
1800     if( This->sWorkDir )
1801         lstrcpynW( pszDir, This->sWorkDir, cchMaxPath );
1802
1803     return S_OK;
1804 }
1805
1806 static HRESULT WINAPI IShellLinkW_fnSetWorkingDirectory(IShellLinkW * iface, LPCWSTR pszDir)
1807 {
1808     _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1809
1810     TRACE("(%p)->(dir=%s)\n",This, debugstr_w(pszDir));
1811
1812     HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1813     This->sWorkDir = HeapAlloc( GetProcessHeap(), 0,
1814                                 (lstrlenW( pszDir )+1)*sizeof (WCHAR) );
1815     if ( !This->sWorkDir )
1816         return E_OUTOFMEMORY;
1817     lstrcpyW( This->sWorkDir, pszDir );
1818     This->bDirty = TRUE;
1819
1820     return S_OK;
1821 }
1822
1823 static HRESULT WINAPI IShellLinkW_fnGetArguments(IShellLinkW * iface, LPWSTR pszArgs,INT cchMaxPath)
1824 {
1825     _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1826
1827     TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1828
1829     if( cchMaxPath )
1830         pszArgs[0] = 0;
1831     if( This->sArgs )
1832         lstrcpynW( pszArgs, This->sArgs, cchMaxPath );
1833
1834     return NOERROR;
1835 }
1836
1837 static HRESULT WINAPI IShellLinkW_fnSetArguments(IShellLinkW * iface, LPCWSTR pszArgs)
1838 {
1839     _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1840
1841     TRACE("(%p)->(args=%s)\n",This, debugstr_w(pszArgs));
1842
1843     HeapFree(GetProcessHeap(), 0, This->sArgs);
1844     This->sArgs = HeapAlloc( GetProcessHeap(), 0,
1845                              (lstrlenW( pszArgs )+1)*sizeof (WCHAR) );
1846     if ( !This->sArgs )
1847         return E_OUTOFMEMORY;
1848     lstrcpyW( This->sArgs, pszArgs );
1849     This->bDirty = TRUE;
1850
1851     return S_OK;
1852 }
1853
1854 static HRESULT WINAPI IShellLinkW_fnGetHotkey(IShellLinkW * iface, WORD *pwHotkey)
1855 {
1856     _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1857
1858     TRACE("(%p)->(%p)\n",This, pwHotkey);
1859
1860     *pwHotkey=This->wHotKey;
1861
1862     return S_OK;
1863 }
1864
1865 static HRESULT WINAPI IShellLinkW_fnSetHotkey(IShellLinkW * iface, WORD wHotkey)
1866 {
1867     _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1868
1869     TRACE("(%p)->(hotkey=%x)\n",This, wHotkey);
1870
1871     This->wHotKey = wHotkey;
1872     This->bDirty = TRUE;
1873
1874     return S_OK;
1875 }
1876
1877 static HRESULT WINAPI IShellLinkW_fnGetShowCmd(IShellLinkW * iface, INT *piShowCmd)
1878 {
1879     _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1880
1881     TRACE("(%p)->(%p)\n",This, piShowCmd);
1882
1883     *piShowCmd = This->iShowCmd;
1884
1885     return S_OK;
1886 }
1887
1888 static HRESULT WINAPI IShellLinkW_fnSetShowCmd(IShellLinkW * iface, INT iShowCmd)
1889 {
1890     _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1891
1892     This->iShowCmd = iShowCmd;
1893     This->bDirty = TRUE;
1894
1895     return S_OK;
1896 }
1897
1898 static HRESULT SHELL_PidlGeticonLocationW(IShellFolder* psf, LPITEMIDLIST pidl, LPWSTR pszIconPath, int cchIconPath, int* piIcon)
1899 {
1900     LPCITEMIDLIST pidlLast;
1901
1902     HRESULT hr = SHBindToParent(pidl, &IID_IShellFolder, (LPVOID*)&psf, &pidlLast);
1903
1904     if (SUCCEEDED(hr)) {
1905         IExtractIconW* pei;
1906
1907         hr = IShellFolder_GetUIObjectOf(psf, 0, 1, (LPCITEMIDLIST*)&pidlLast, &IID_IExtractIconW, NULL, (LPVOID*)&pei);
1908
1909         if (SUCCEEDED(hr)) {
1910             hr = IExtractIconW_GetIconLocation(pei, 0, pszIconPath, MAX_PATH, piIcon, NULL);
1911
1912             IExtractIconW_Release(pei);
1913         }
1914
1915         IShellFolder_Release(psf);
1916     }
1917
1918     return hr;
1919 }
1920
1921 static HRESULT WINAPI IShellLinkW_fnGetIconLocation(IShellLinkW * iface, LPWSTR pszIconPath,INT cchIconPath,INT *piIcon)
1922 {
1923     _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1924
1925     TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1926
1927     pszIconPath[0] = 0;
1928     *piIcon = This->iIcoNdx;
1929
1930     if (This->sIcoPath)
1931     {
1932         lstrcpynW(pszIconPath, This->sIcoPath, cchIconPath);
1933         return S_OK;
1934     }
1935
1936     if (This->pPidl || This->sPath)
1937     {
1938         IShellFolder* pdsk;
1939
1940         HRESULT hr = SHGetDesktopFolder(&pdsk);
1941
1942         if (SUCCEEDED(hr))
1943         {
1944             /* first look for an icon using the PIDL (if present) */
1945             if (This->pPidl)
1946                 hr = SHELL_PidlGeticonLocationW(pdsk, This->pPidl, pszIconPath, cchIconPath, piIcon);
1947             else
1948                 hr = E_FAIL;
1949
1950             /* if we couldn't find an icon yet, look for it using the file system path */
1951             if (FAILED(hr) && This->sPath)
1952             {
1953                 LPITEMIDLIST pidl;
1954
1955                 hr = IShellFolder_ParseDisplayName(pdsk, 0, NULL, This->sPath, NULL, &pidl, NULL);
1956
1957                 if (SUCCEEDED(hr))
1958                 {
1959                     hr = SHELL_PidlGeticonLocationW(pdsk, pidl, pszIconPath, cchIconPath, piIcon);
1960
1961                     SHFree(pidl);
1962                 }
1963             }
1964
1965             IShellFolder_Release(pdsk);
1966         }
1967         return hr;
1968     }
1969     return S_OK;
1970 }
1971
1972 static HRESULT WINAPI IShellLinkW_fnSetIconLocation(IShellLinkW * iface, LPCWSTR pszIconPath,INT iIcon)
1973 {
1974     _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1975
1976     TRACE("(%p)->(path=%s iicon=%u)\n",This, debugstr_w(pszIconPath), iIcon);
1977
1978     HeapFree(GetProcessHeap(), 0, This->sIcoPath);
1979     This->sIcoPath = HeapAlloc( GetProcessHeap(), 0,
1980                                 (lstrlenW( pszIconPath )+1)*sizeof (WCHAR) );
1981     if ( !This->sIcoPath )
1982         return E_OUTOFMEMORY;
1983     lstrcpyW( This->sIcoPath, pszIconPath );
1984
1985     This->iIcoNdx = iIcon;
1986     This->bDirty = TRUE;
1987
1988     return S_OK;
1989 }
1990
1991 static HRESULT WINAPI IShellLinkW_fnSetRelativePath(IShellLinkW * iface, LPCWSTR pszPathRel, DWORD dwReserved)
1992 {
1993     _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1994
1995     TRACE("(%p)->(path=%s %lx)\n",This, debugstr_w(pszPathRel), dwReserved);
1996
1997     HeapFree(GetProcessHeap(), 0, This->sPathRel);
1998     This->sPathRel = HeapAlloc( GetProcessHeap(), 0,
1999                                 (lstrlenW( pszPathRel )+1) * sizeof (WCHAR) );
2000     if ( !This->sPathRel )
2001         return E_OUTOFMEMORY;
2002     lstrcpyW( This->sPathRel, pszPathRel );
2003     This->bDirty = TRUE;
2004
2005     return ShellLink_UpdatePath(This->sPathRel, This->sPath, This->sWorkDir, &This->sPath);
2006 }
2007
2008 static HRESULT WINAPI IShellLinkW_fnResolve(IShellLinkW * iface, HWND hwnd, DWORD fFlags)
2009 {
2010     HRESULT hr = S_OK;
2011
2012     _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
2013
2014     TRACE("(%p)->(hwnd=%p flags=%lx)\n",This, hwnd, fFlags);
2015
2016     /*FIXME: use IResolveShellLink interface */
2017
2018     if (!This->sPath && This->pPidl) {
2019         WCHAR buffer[MAX_PATH];
2020
2021         hr = SHELL_GetPathFromIDListW(This->pPidl, buffer, MAX_PATH);
2022
2023         if (SUCCEEDED(hr) && *buffer) {
2024             This->sPath = (LPWSTR) HeapAlloc(GetProcessHeap(), 0, (lstrlenW(buffer)+1)*sizeof(WCHAR));
2025             if (!This->sPath)
2026                 return E_OUTOFMEMORY;
2027
2028             lstrcpyW(This->sPath, buffer);
2029
2030             This->bDirty = TRUE;
2031         } else
2032             hr = S_OK;    /* don't report an error occurred while just caching information */
2033     }
2034
2035     if (!This->sIcoPath && This->sPath) {
2036         This->sIcoPath = (LPWSTR) HeapAlloc(GetProcessHeap(), 0, (lstrlenW(This->sPath)+1)*sizeof(WCHAR));
2037         if (!This->sIcoPath)
2038             return E_OUTOFMEMORY;
2039
2040         lstrcpyW(This->sIcoPath, This->sPath);
2041         This->iIcoNdx = 0;
2042
2043         This->bDirty = TRUE;
2044     }
2045
2046     return hr;
2047 }
2048
2049 static LPWSTR ShellLink_GetAdvertisedArg(LPCWSTR str)
2050 {
2051     LPWSTR ret;
2052     LPCWSTR p;
2053     DWORD len;
2054
2055     p = strchrW( str, ':' );
2056     if( !p )
2057         return NULL;
2058     len = p - str;
2059     ret = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR)*(len+1));
2060     if( !ret )
2061         return ret;
2062     memcpy( ret, str, sizeof(WCHAR)*len );
2063     ret[len] = 0;
2064     return ret;
2065 }
2066
2067 static HRESULT ShellLink_SetAdvertiseInfo(IShellLinkImpl *This, LPCWSTR str)
2068 {
2069     LPCWSTR szComponent = NULL, szProduct = NULL, p;
2070     WCHAR szGuid[39];
2071     HRESULT r;
2072     GUID guid;
2073     int len;
2074
2075     while( str[0] )
2076     {
2077         /* each segment must start with two colons */
2078         if( str[0] != ':' || str[1] != ':' )
2079             return E_FAIL;
2080
2081         /* the last segment is just two colons */
2082         if( !str[2] )
2083             break;
2084         str += 2;
2085
2086         /* there must be a colon straight after a guid */
2087         p = strchrW( str, ':' );
2088         if( !p )
2089             return E_FAIL;
2090         len = p - str;
2091         if( len != 38 )
2092             return E_FAIL;
2093
2094         /* get the guid, and check it's validly formatted */
2095         memcpy( szGuid, str, sizeof(WCHAR)*len );
2096         szGuid[len] = 0;
2097         r = CLSIDFromString( szGuid, &guid );
2098         if( r != S_OK )
2099             return r;
2100         str = p + 1;
2101
2102         /* match it up to a guid that we care about */
2103         if( IsEqualGUID( &guid, &SHELL32_AdvtShortcutComponent ) && !szComponent )
2104             szComponent = str;
2105         else if( IsEqualGUID( &guid, &SHELL32_AdvtShortcutProduct ) && !szProduct )
2106             szProduct = str;
2107         else
2108             return E_FAIL;
2109
2110         /* skip to the next field */
2111         str = strchrW( str, ':' );
2112         if( !str )
2113             return E_FAIL;
2114     }
2115
2116     /* we have to have at least one of these two for an advertised shortcut */
2117     if( !szComponent && !szProduct )
2118         return E_FAIL;
2119
2120     This->sComponent = ShellLink_GetAdvertisedArg( szComponent );
2121     This->sProduct = ShellLink_GetAdvertisedArg( szProduct );
2122
2123     TRACE("Component = %s\n", debugstr_w(This->sComponent));
2124     TRACE("Product = %s\n", debugstr_w(This->sProduct));
2125
2126     return S_OK;
2127 }
2128
2129 static BOOL ShellLink_GetVolumeInfo(LPWSTR path, volume_info *volume)
2130 {
2131     const int label_sz = sizeof volume->label/sizeof volume->label[0];
2132     WCHAR drive[4] = { path[0], ':', '\\', 0 };
2133     BOOL r;
2134
2135     volume->type = GetDriveTypeW(drive);
2136     r = GetVolumeInformationW(drive, volume->label, label_sz,
2137                               &volume->serial, NULL, NULL, NULL, 0);
2138     TRACE("r = %d type %ld serial %08lx name %s\n", r,
2139           volume->type, volume->serial, debugstr_w(volume->label));
2140     return r;
2141 }
2142
2143 static HRESULT WINAPI IShellLinkW_fnSetPath(IShellLinkW * iface, LPCWSTR pszFile)
2144 {
2145     _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
2146     WCHAR buffer[MAX_PATH];
2147     LPWSTR fname;
2148     HRESULT hr = S_OK;
2149
2150     TRACE("(%p)->(path=%s)\n",This, debugstr_w(pszFile));
2151
2152     HeapFree(GetProcessHeap(), 0, This->sPath);
2153     This->sPath = NULL;
2154
2155     HeapFree(GetProcessHeap(), 0, This->sComponent);
2156     This->sComponent = NULL;
2157
2158     if (This->pPidl)
2159         ILFree(This->pPidl);
2160     This->pPidl = NULL;
2161
2162     if (S_OK != ShellLink_SetAdvertiseInfo( This, pszFile ))
2163     {
2164         if (*pszFile == '\0')
2165             *buffer = '\0';
2166         else if (!GetFullPathNameW(pszFile, MAX_PATH, buffer, &fname))
2167             return E_FAIL;
2168         else if(!PathFileExistsW(buffer))
2169             hr = S_FALSE;
2170
2171         This->pPidl = SHSimpleIDListFromPathW(pszFile);
2172         ShellLink_GetVolumeInfo(buffer, &This->volume);
2173
2174         This->sPath = HeapAlloc( GetProcessHeap(), 0,
2175                              (lstrlenW( buffer )+1) * sizeof (WCHAR) );
2176         if (!This->sPath)
2177             return E_OUTOFMEMORY;
2178
2179         lstrcpyW(This->sPath, buffer);
2180     }
2181     This->bDirty = TRUE;
2182
2183     return hr;
2184 }
2185
2186 /**************************************************************************
2187 * IShellLinkW Implementation
2188 */
2189
2190 static IShellLinkWVtbl slvtw =
2191 {
2192         IShellLinkW_fnQueryInterface,
2193         IShellLinkW_fnAddRef,
2194         IShellLinkW_fnRelease,
2195         IShellLinkW_fnGetPath,
2196         IShellLinkW_fnGetIDList,
2197         IShellLinkW_fnSetIDList,
2198         IShellLinkW_fnGetDescription,
2199         IShellLinkW_fnSetDescription,
2200         IShellLinkW_fnGetWorkingDirectory,
2201         IShellLinkW_fnSetWorkingDirectory,
2202         IShellLinkW_fnGetArguments,
2203         IShellLinkW_fnSetArguments,
2204         IShellLinkW_fnGetHotkey,
2205         IShellLinkW_fnSetHotkey,
2206         IShellLinkW_fnGetShowCmd,
2207         IShellLinkW_fnSetShowCmd,
2208         IShellLinkW_fnGetIconLocation,
2209         IShellLinkW_fnSetIconLocation,
2210         IShellLinkW_fnSetRelativePath,
2211         IShellLinkW_fnResolve,
2212         IShellLinkW_fnSetPath
2213 };