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