Get rid of W->A call.
[wine] / dlls / shell32 / shelllink.c
1 /*
2  *
3  *      Copyright 1997  Marcus Meissner
4  *      Copyright 1998  Juergen Schmied
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * NOTES
21  *   Nearly complete informations about the binary formats 
22  *   of .lnk files available at http://www.wotsit.org
23  *
24  */
25
26 #include "config.h"
27 #include "wine/port.h"
28
29 #include <ctype.h>
30 #include <string.h>
31 #include <stdarg.h>
32 #include <stdio.h>
33 #ifdef HAVE_UNISTD_H
34 # include <unistd.h>
35 #endif
36 #include <errno.h>
37 #include <limits.h>
38 #ifdef HAVE_SYS_WAIT_H
39 # include <sys/wait.h>
40 #endif
41
42 #define COBJMACROS
43
44 #include "wine/debug.h"
45 #include "winerror.h"
46 #include "windef.h"
47 #include "winbase.h"
48 #include "winnls.h"
49 #include "winreg.h"
50
51 #include "winuser.h"
52 #include "wingdi.h"
53 #include "shlobj.h"
54 #include "undocshell.h"
55
56 #include "pidl.h"
57 #include "shell32_main.h"
58 #include "shlguid.h"
59 #include "shlwapi.h"
60
61 WINE_DEFAULT_DEBUG_CHANNEL(shell);
62
63 /* link file formats */
64
65 /* flag1: lnk elements: simple link has 0x0B */
66 #define SCF_PIDL   1
67 #define SCF_NORMAL 2
68 #define SCF_DESCRIPTION 4
69 #define SCF_RELATIVE 8
70 #define SCF_WORKDIR 0x10
71 #define SCF_ARGS 0x20
72 #define SCF_CUSTOMICON 0x40
73 #define SCF_UNICODE 0x80
74
75 #include "pshpack1.h"
76
77 typedef struct _LINK_HEADER
78 {
79         DWORD    dwSize;        /* 0x00 size of the header - 0x4c */
80         GUID     MagicGuid;     /* 0x04 is CLSID_ShellLink */
81         DWORD    dwFlags;       /* 0x14 describes elements following */
82         DWORD    dwFileAttr;    /* 0x18 attributes of the target file */
83         FILETIME Time1;         /* 0x1c */
84         FILETIME Time2;         /* 0x24 */
85         FILETIME Time3;         /* 0x2c */
86         DWORD    dwFileLength;  /* 0x34 File length */
87         DWORD    nIcon;         /* 0x38 icon number */
88         DWORD   fStartup;       /* 0x3c startup type */
89         DWORD   wHotKey;        /* 0x40 hotkey */
90         DWORD   Unknown5;       /* 0x44 */
91         DWORD   Unknown6;       /* 0x48 */
92 } LINK_HEADER, * PLINK_HEADER;
93
94 #define SHLINK_LOCAL  0
95 #define SHLINK_REMOTE 1
96
97 typedef struct _LOCATION_INFO
98 {
99     DWORD  dwTotalSize;
100     DWORD  dwHeaderSize;
101     DWORD  dwFlags;
102     DWORD  dwVolTableOfs;
103     DWORD  dwLocalPathOfs;
104     DWORD  dwNetworkVolTableOfs;
105     DWORD  dwFinalPathOfs;
106 } LOCATION_INFO;
107
108 typedef struct _LOCAL_VOLUME_INFO
109 {
110     DWORD dwSize;
111     DWORD dwType;
112     DWORD dwVolSerial;
113     DWORD dwVolLabelOfs;
114 } LOCAL_VOLUME_INFO;
115
116 #include "poppack.h"
117
118 static IShellLinkAVtbl          slvt;
119 static IShellLinkWVtbl          slvtw;
120 static IPersistFileVtbl pfvt;
121 static IPersistStreamVtbl       psvt;
122
123 /* IShellLink Implementation */
124
125 typedef struct
126 {
127         IShellLinkAVtbl    *lpVtbl;
128         DWORD               ref;
129
130         IShellLinkWVtbl    *lpvtblw;
131         IPersistFileVtbl   *lpvtblPersistFile;
132         IPersistStreamVtbl *lpvtblPersistStream;
133
134         /* data structures according to the informations in the link */
135         LPITEMIDLIST    pPidl;
136         WORD            wHotKey;
137         SYSTEMTIME      time1;
138         SYSTEMTIME      time2;
139         SYSTEMTIME      time3;
140
141         DWORD         iShowCmd;
142         LPWSTR        sIcoPath;
143         INT           iIcoNdx;
144         LPWSTR        sPath;
145         LPWSTR        sArgs;
146         LPWSTR        sWorkDir;
147         LPWSTR        sDescription;
148         LPWSTR        sPathRel;
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 /**************************************************************************
177  *  IPersistFile_QueryInterface
178  */
179 static HRESULT WINAPI IPersistFile_fnQueryInterface(
180         IPersistFile* iface,
181         REFIID riid,
182         LPVOID *ppvObj)
183 {
184         _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
185
186         TRACE("(%p)\n",This);
187
188         return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvObj);
189 }
190
191 /******************************************************************************
192  * IPersistFile_AddRef
193  */
194 static ULONG WINAPI IPersistFile_fnAddRef(IPersistFile* iface)
195 {
196         _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
197
198         TRACE("(%p)->(count=%lu)\n",This,This->ref);
199
200         return IShellLinkA_AddRef((IShellLinkA*)This);
201 }
202 /******************************************************************************
203  * IPersistFile_Release
204  */
205 static ULONG WINAPI IPersistFile_fnRelease(IPersistFile* iface)
206 {
207         _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
208
209         TRACE("(%p)->(count=%lu)\n",This,This->ref);
210
211         return IShellLinkA_Release((IShellLinkA*)This);
212 }
213
214 static HRESULT WINAPI IPersistFile_fnGetClassID(IPersistFile* iface, CLSID *pClassID)
215 {
216         _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
217         FIXME("(%p)\n",This);
218         return NOERROR;
219 }
220 static HRESULT WINAPI IPersistFile_fnIsDirty(IPersistFile* iface)
221 {
222         _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
223
224         TRACE("(%p)\n",This);
225
226         if (This->bDirty)
227             return S_OK;
228
229         return S_FALSE;
230 }
231 static HRESULT WINAPI IPersistFile_fnLoad(IPersistFile* iface, LPCOLESTR pszFileName, DWORD dwMode)
232 {
233         _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
234         IPersistStream *StreamThis = (IPersistStream *)&This->lpvtblPersistStream;
235         HRESULT r;
236         IStream *stm;
237
238         TRACE("(%p, %s)\n",This, debugstr_w(pszFileName));
239
240         r = CreateStreamOnFile(pszFileName, dwMode, &stm);
241         if( SUCCEEDED( r ) )
242         {
243             r = IPersistStream_Load(StreamThis, stm);
244             ShellLink_UpdatePath(This->sPathRel, pszFileName, This->sWorkDir, &This->sPath);
245             IStream_Release( stm );
246             This->bDirty = FALSE;
247         }
248
249         return r;
250 }
251
252 static BOOL StartLinkProcessor( LPCOLESTR szLink )
253 {
254     static const WCHAR szFormat[] = {'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 || !This->sPath)
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 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_LoadLocation( IStream* stm )
473 {
474     DWORD size;
475     ULONG count;
476     HRESULT r;
477     LOCATION_INFO *loc;
478
479     TRACE("%p\n",stm);
480
481     r = IStream_Read( stm, &size, sizeof(size), &count );
482     if( FAILED( r ) )
483         return r;
484     if( count != sizeof(loc->dwTotalSize) )
485         return E_FAIL;
486
487     loc = HeapAlloc( GetProcessHeap(), 0, size );
488     if( ! loc )
489         return E_OUTOFMEMORY;
490
491     r = IStream_Read( stm, &loc->dwHeaderSize, size-sizeof(size), &count );
492     if( FAILED( r ) )
493         goto end;
494     if( count != (size - sizeof(size)) )
495     {
496         r = E_FAIL;
497         goto end;
498     }
499     loc->dwTotalSize = size;
500
501     TRACE("Read %ld bytes\n",count);
502
503     /* FIXME: do something useful with it */
504     HeapFree( GetProcessHeap(), 0, loc );
505
506     return S_OK;
507 end:
508     HeapFree( GetProcessHeap(), 0, loc );
509     return r;
510 }
511
512 /************************************************************************
513  * IPersistStream_Load (IPersistStream)
514  */
515 static HRESULT WINAPI IPersistStream_fnLoad(
516         IPersistStream*  iface,
517     IStream*         stm)
518 {
519     LINK_HEADER hdr;
520     ULONG    dwBytesRead;
521     BOOL     unicode;
522     WCHAR    sTemp[MAX_PATH];
523     HRESULT  r;
524
525     _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
526
527     TRACE("(%p)(%p)\n", This, stm);
528
529     if( !stm )
530           return STG_E_INVALIDPOINTER;
531
532     dwBytesRead = 0;
533     r = IStream_Read(stm, &hdr, sizeof(hdr), &dwBytesRead);
534     if( FAILED( r ) )
535         return r;
536
537     if( dwBytesRead != sizeof(hdr))
538         return E_FAIL;
539     if( hdr.dwSize != sizeof(hdr))
540         return E_FAIL;
541     if( !IsEqualIID(&hdr.MagicGuid, &CLSID_ShellLink) )
542         return E_FAIL;
543
544     /* if( hdr.dwFlags & SCF_PIDL ) */  /* FIXME: seems to always have a PIDL */
545     {
546         r = ILLoadFromStream( stm, &This->pPidl );
547         if( FAILED( r ) )
548             return r;
549     }
550     This->wHotKey = (WORD)hdr.wHotKey;
551     This->iIcoNdx = hdr.nIcon;
552     FileTimeToSystemTime (&hdr.Time1, &This->time1);
553     FileTimeToSystemTime (&hdr.Time2, &This->time2);
554     FileTimeToSystemTime (&hdr.Time3, &This->time3);
555 #if 1
556     GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE,&This->time1, NULL, sTemp, 256);
557     TRACE("-- time1: %s\n", debugstr_w(sTemp) );
558     GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE,&This->time2, NULL, sTemp, 256);
559     TRACE("-- time1: %s\n", debugstr_w(sTemp) );
560     GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE,&This->time3, NULL, sTemp, 256);
561     TRACE("-- time1: %s\n", debugstr_w(sTemp) );
562     pdump (This->pPidl);
563 #endif
564     if( hdr.dwFlags & SCF_NORMAL )
565         r = Stream_LoadLocation( stm );
566     if( FAILED( r ) )
567         goto end;
568     unicode = hdr.dwFlags & SCF_UNICODE;
569     if( hdr.dwFlags & SCF_DESCRIPTION )
570     {
571         r = Stream_LoadString( stm, unicode, &This->sDescription );
572         TRACE("Description  -> %s\n",debugstr_w(This->sDescription));
573     }
574     if( FAILED( r ) )
575         goto end;
576
577     if( hdr.dwFlags & SCF_RELATIVE )
578     {
579         r = Stream_LoadString( stm, unicode, &This->sPathRel );
580         TRACE("Relative Path-> %s\n",debugstr_w(This->sPathRel));
581     }
582     if( FAILED( r ) )
583         goto end;
584
585     if( hdr.dwFlags & SCF_WORKDIR )
586           {
587         r = Stream_LoadString( stm, unicode, &This->sWorkDir );
588         TRACE("Working Dir  -> %s\n",debugstr_w(This->sWorkDir));
589     }
590     if( FAILED( r ) )
591         goto end;
592
593     if( hdr.dwFlags & SCF_ARGS )
594     {
595         r = Stream_LoadString( stm, unicode, &This->sArgs );
596         TRACE("Working Dir  -> %s\n",debugstr_w(This->sArgs));
597     }
598     if( FAILED( r ) )
599         goto end;
600
601     if( hdr.dwFlags & SCF_CUSTOMICON )
602     {
603         r = Stream_LoadString( stm, unicode, &This->sIcoPath );
604         TRACE("Icon file    -> %s\n",debugstr_w(This->sIcoPath));
605     }
606     if( FAILED( r ) )
607         goto end;
608
609     TRACE("OK\n");
610
611     pdump (This->pPidl);
612
613     return S_OK;
614 end:
615     return r;
616 }
617
618 /************************************************************************
619  * Stream_WriteString
620  *
621  * Helper function for IPersistStream_Save. Writes a unicode string 
622  *  with terminating nul byte to a stream, preceded by the its length.
623  */
624 static HRESULT Stream_WriteString( IStream* stm, LPCWSTR str )
625 {
626     USHORT len = lstrlenW( str ) + 1;
627     DWORD count;
628     HRESULT r;
629
630     r = IStream_Write( stm, &len, sizeof(len), &count );
631     if( FAILED( r ) )
632         return r;
633
634     len *= sizeof(WCHAR);
635
636     r = IStream_Write( stm, str, len, &count );
637     if( FAILED( r ) )
638         return r;
639
640     return S_OK;
641 }
642
643 static HRESULT Stream_WriteLocationInfo( IStream* stm, LPCWSTR filename )
644 {
645     LOCATION_INFO loc;
646     ULONG count;
647
648     FIXME("writing empty location info\n");
649
650     memset( &loc, 0, sizeof(loc) );
651     loc.dwTotalSize = sizeof(loc) - sizeof(loc.dwTotalSize);
652
653     /* FIXME: fill this in */
654
655     return IStream_Write( stm, &loc, loc.dwTotalSize, &count );
656 }
657
658 /************************************************************************
659  * IPersistStream_Save (IPersistStream)
660  *
661  * FIXME: makes assumptions about byte order
662  */
663 static HRESULT WINAPI IPersistStream_fnSave(
664         IPersistStream*  iface,
665         IStream*         stm,
666         BOOL             fClearDirty)
667 {
668     static const WCHAR wOpen[] = {'o','p','e','n',0};
669
670     LINK_HEADER header;
671     WCHAR   exePath[MAX_PATH];
672     ULONG   count;
673     HRESULT r;
674
675     _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
676
677     TRACE("(%p) %p %x\n", This, stm, fClearDirty);
678
679     *exePath = '\0';
680
681     if (This->sPath)
682     {
683         SHELL_FindExecutable(NULL, This->sPath, wOpen, exePath, MAX_PATH, NULL, NULL, NULL, NULL);
684         /*
685          * windows can create lnk files to executables that do not exist yet
686          * so if the executable does not exist the just trust the path they
687          * gave us
688          */
689         if( !*exePath ) strcpyW(exePath,This->sPath);
690     }
691
692     /* if there's no PIDL, generate one */
693     if( ! This->pPidl ) This->pPidl = ILCreateFromPathW(exePath);
694
695     memset(&header, 0, sizeof(header));
696     header.dwSize = sizeof(header);
697     memcpy(&header.MagicGuid, &CLSID_ShellLink, sizeof(header.MagicGuid) );
698
699     header.wHotKey = This->wHotKey;
700     header.nIcon = This->iIcoNdx;
701     header.dwFlags = SCF_UNICODE;   /* strings are in unicode */
702     header.dwFlags |= SCF_NORMAL;   /* how do we determine this ? */
703     if( This->pPidl )
704         header.dwFlags |= SCF_PIDL;
705     if( This->sDescription )
706         header.dwFlags |= SCF_DESCRIPTION;
707     if( This->sWorkDir )
708         header.dwFlags |= SCF_WORKDIR;
709     if( This->sArgs )
710         header.dwFlags |= SCF_ARGS;
711     if( This->sIcoPath )
712         header.dwFlags |= SCF_CUSTOMICON;
713
714     SystemTimeToFileTime ( &This->time1, &header.Time1 );
715     SystemTimeToFileTime ( &This->time2, &header.Time2 );
716     SystemTimeToFileTime ( &This->time3, &header.Time3 );
717
718     /* write the Shortcut header */
719     r = IStream_Write( stm, &header, sizeof(header), &count );
720     if( FAILED( r ) )
721     {
722         ERR("Write failed at %d\n",__LINE__);
723         return r;
724     }
725
726     TRACE("Writing pidl \n");
727
728     /* write the PIDL to the shortcut */
729     if( This->pPidl )
730     {
731         r = ILSaveToStream( stm, This->pPidl );
732         if( FAILED( r ) )
733         {
734             ERR("Failed to write PIDL at %d\n",__LINE__);
735             return r;
736         }
737     }
738
739     Stream_WriteLocationInfo( stm, exePath );
740
741     TRACE("Description = %s\n", debugstr_w(This->sDescription));
742     if( This->sDescription )
743         r = Stream_WriteString( stm, This->sDescription );
744
745     if( This->sPathRel )
746         r = Stream_WriteString( stm, This->sPathRel );
747
748     if( This->sWorkDir )
749         r = Stream_WriteString( stm, This->sWorkDir );
750
751     if( This->sArgs )
752         r = Stream_WriteString( stm, This->sArgs );
753
754     if( This->sIcoPath )
755         r = Stream_WriteString( stm, This->sIcoPath );
756
757     return S_OK;
758 }
759
760 /************************************************************************
761  * IPersistStream_GetSizeMax (IPersistStream)
762  */
763 static HRESULT WINAPI IPersistStream_fnGetSizeMax(
764         IPersistStream*  iface,
765         ULARGE_INTEGER*  pcbSize)
766 {
767         _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
768
769         TRACE("(%p)\n", This);
770
771         return E_NOTIMPL;
772 }
773
774 static IPersistStreamVtbl psvt =
775 {
776         IPersistStream_fnQueryInterface,
777         IPersistStream_fnAddRef,
778         IPersistStream_fnRelease,
779         IPersistStream_fnGetClassID,
780         IPersistStream_fnIsDirty,
781         IPersistStream_fnLoad,
782         IPersistStream_fnSave,
783         IPersistStream_fnGetSizeMax
784 };
785
786 /**************************************************************************
787  *        IShellLink_Constructor
788  */
789 HRESULT WINAPI IShellLink_Constructor (
790         IUnknown * pUnkOuter,
791         REFIID riid,
792         LPVOID * ppv)
793 {
794         IShellLinkImpl * sl;
795
796         TRACE("unkOut=%p riid=%s\n",pUnkOuter, debugstr_guid(riid));
797
798         *ppv = NULL;
799
800         if(pUnkOuter) return CLASS_E_NOAGGREGATION;
801         sl = (IShellLinkImpl *) LocalAlloc(GMEM_ZEROINIT,sizeof(IShellLinkImpl));
802         if (!sl) return E_OUTOFMEMORY;
803
804         sl->ref = 1;
805         sl->lpVtbl = &slvt;
806         sl->lpvtblw = &slvtw;
807         sl->lpvtblPersistFile = &pfvt;
808         sl->lpvtblPersistStream = &psvt;
809         sl->iShowCmd = SW_SHOWNORMAL;
810         sl->bDirty = FALSE;
811
812         TRACE("(%p)->()\n",sl);
813
814         if (IsEqualIID(riid, &IID_IUnknown) ||
815             IsEqualIID(riid, &IID_IShellLinkA))
816             *ppv = sl;
817         else if (IsEqualIID(riid, &IID_IShellLinkW))
818             *ppv = &(sl->lpvtblw);
819         else {
820             LocalFree((HLOCAL)sl);
821             ERR("E_NOINTERFACE\n");
822             return E_NOINTERFACE;
823         }
824
825         return S_OK;
826 }
827
828
829 static BOOL SHELL_ExistsFileW(LPCWSTR path)
830 {
831     HANDLE hfile = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
832
833     if (hfile != INVALID_HANDLE_VALUE) {
834         CloseHandle(hfile);
835         return TRUE;
836     } else
837         return FALSE;
838 }
839
840 /**************************************************************************
841  *  ShellLink_UpdatePath
842  *      update absolute path in sPath using relative path in sPathRel
843  */
844 static HRESULT ShellLink_UpdatePath(LPWSTR sPathRel, LPCWSTR path, LPCWSTR sWorkDir, LPWSTR* psPath)
845 {
846     if (!path || !psPath)
847         return E_INVALIDARG;
848
849     if (!*psPath && sPathRel) {
850         WCHAR buffer[2*MAX_PATH], abs_path[2*MAX_PATH];
851         LPWSTR final = NULL;
852
853         /* first try if [directory of link file] + [relative path] finds an existing file */
854
855         GetFullPathNameW( path, MAX_PATH*2, buffer, &final );
856         if( !final )
857             final = buffer;
858         lstrcpyW(final, sPathRel);
859
860         *abs_path = '\0';
861
862         if (SHELL_ExistsFileW(buffer)) {
863             if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
864                 lstrcpyW(abs_path, buffer);
865         } else {
866             /* try if [working directory] + [relative path] finds an existing file */
867             if (sWorkDir) {
868                 lstrcpyW(buffer, sWorkDir);
869                 lstrcpyW(PathAddBackslashW(buffer), sPathRel);
870
871                 if (SHELL_ExistsFileW(buffer))
872                     if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
873                         lstrcpyW(abs_path, buffer);
874             }
875         }
876
877         /* FIXME: This is even not enough - not all shell links can be resolved using this algorithm. */
878         if (!*abs_path)
879             lstrcpyW(abs_path, sPathRel);
880
881         *psPath = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(abs_path)+1)*sizeof(WCHAR));
882         if (!*psPath)
883             return E_OUTOFMEMORY;
884
885         lstrcpyW(*psPath, abs_path);
886     }
887
888     return S_OK;
889 }
890
891 /**************************************************************************
892  *        IShellLink_ConstructFromFile
893  */
894 HRESULT WINAPI IShellLink_ConstructFromFile (
895         IUnknown* pUnkOuter,
896         REFIID riid,
897         LPCITEMIDLIST pidl,
898         LPVOID* ppv
899 )
900 {
901     IShellLinkW* psl;
902
903     HRESULT hr = IShellLink_Constructor(NULL, riid, (LPVOID*)&psl);
904
905     if (SUCCEEDED(hr)) {
906         IPersistFile* ppf;
907
908         *ppv = NULL;
909
910         hr = IShellLinkW_QueryInterface(psl, &IID_IPersistFile, (LPVOID*)&ppf);
911
912         if (SUCCEEDED(hr)) {
913             WCHAR path[MAX_PATH];
914
915             if (SHGetPathFromIDListW(pidl, path)) 
916                 hr = IPersistFile_Load(ppf, path, 0);
917             else
918                 hr = E_FAIL;
919
920             if (SUCCEEDED(hr))
921                 *ppv = (IUnknown*) psl;
922
923             IPersistFile_Release(ppf);
924         }
925
926         if (!*ppv)
927             IShellLinkW_Release(psl);
928     }
929
930     return hr;
931 }
932
933 /**************************************************************************
934  *  IShellLinkA_QueryInterface
935  */
936 static HRESULT WINAPI IShellLinkA_fnQueryInterface( IShellLinkA * iface, REFIID riid,  LPVOID *ppvObj)
937 {
938         IShellLinkImpl *This = (IShellLinkImpl *)iface;
939
940         TRACE("(%p)->(\n\tIID:\t%s)\n",This,debugstr_guid(riid));
941
942         *ppvObj = NULL;
943
944         if(IsEqualIID(riid, &IID_IUnknown) ||
945            IsEqualIID(riid, &IID_IShellLinkA))
946         {
947           *ppvObj = This;
948         }
949         else if(IsEqualIID(riid, &IID_IShellLinkW))
950         {
951           *ppvObj = (IShellLinkW *)&(This->lpvtblw);
952         }
953         else if(IsEqualIID(riid, &IID_IPersistFile))
954         {
955           *ppvObj = (IPersistFile *)&(This->lpvtblPersistFile);
956         }
957         else if(IsEqualIID(riid, &IID_IPersistStream))
958         {
959           *ppvObj = (IPersistStream *)&(This->lpvtblPersistStream);
960         }
961
962         if(*ppvObj)
963         {
964           IUnknown_AddRef((IUnknown*)(*ppvObj));
965           TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
966           return S_OK;
967         }
968         TRACE("-- Interface: E_NOINTERFACE\n");
969         return E_NOINTERFACE;
970 }
971 /******************************************************************************
972  * IShellLinkA_AddRef
973  */
974 static ULONG WINAPI IShellLinkA_fnAddRef(IShellLinkA * iface)
975 {
976         IShellLinkImpl *This = (IShellLinkImpl *)iface;
977
978         TRACE("(%p)->(count=%lu)\n",This,This->ref);
979
980         return ++(This->ref);
981 }
982 /******************************************************************************
983  *      IShellLinkA_Release
984  */
985 static ULONG WINAPI IShellLinkA_fnRelease(IShellLinkA * iface)
986 {
987     IShellLinkImpl *This = (IShellLinkImpl *)iface;
988
989     TRACE("(%p)->(count=%lu)\n",This,This->ref);
990
991     if (--(This->ref))
992         return This->ref;
993
994     TRACE("-- destroying IShellLink(%p)\n",This);
995
996     HeapFree(GetProcessHeap(), 0, This->sIcoPath);
997     HeapFree(GetProcessHeap(), 0, This->sArgs);
998     HeapFree(GetProcessHeap(), 0, This->sWorkDir);
999     HeapFree(GetProcessHeap(), 0, This->sDescription);
1000     HeapFree(GetProcessHeap(),0,This->sPath);
1001
1002     if (This->pPidl)
1003         ILFree(This->pPidl);
1004
1005     LocalFree((HANDLE)This);
1006
1007     return 0;
1008 }
1009
1010 static HRESULT WINAPI IShellLinkA_fnGetPath(IShellLinkA * iface, LPSTR pszFile,
1011                   INT cchMaxPath, WIN32_FIND_DATAA *pfd, DWORD fFlags)
1012 {
1013     IShellLinkImpl *This = (IShellLinkImpl *)iface;
1014
1015     TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%lu)(%s)\n",
1016           This, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(This->sPath));
1017
1018     if( cchMaxPath )
1019         pszFile[0] = 0;
1020     if (This->sPath)
1021         WideCharToMultiByte( CP_ACP, 0, This->sPath, -1,
1022                              pszFile, cchMaxPath, NULL, NULL);
1023
1024     if (pfd) FIXME("(%p): WIN32_FIND_DATA is not yet filled.\n", This);
1025
1026     return NOERROR;
1027 }
1028
1029 static HRESULT WINAPI IShellLinkA_fnGetIDList(IShellLinkA * iface, LPITEMIDLIST * ppidl)
1030 {
1031         IShellLinkImpl *This = (IShellLinkImpl *)iface;
1032
1033         TRACE("(%p)->(ppidl=%p)\n",This, ppidl);
1034
1035         *ppidl = ILClone(This->pPidl);
1036
1037         return NOERROR;
1038 }
1039
1040 static HRESULT WINAPI IShellLinkA_fnSetIDList(IShellLinkA * iface, LPCITEMIDLIST pidl)
1041 {
1042     IShellLinkImpl *This = (IShellLinkImpl *)iface;
1043
1044     TRACE("(%p)->(pidl=%p)\n",This, pidl);
1045
1046     if (This->pPidl)
1047         ILFree(This->pPidl);
1048     This->pPidl = ILClone (pidl);
1049     This->bDirty = TRUE;
1050
1051     return S_OK;
1052 }
1053
1054 static HRESULT WINAPI IShellLinkA_fnGetDescription(IShellLinkA * iface, LPSTR pszName,INT cchMaxName)
1055 {
1056     IShellLinkImpl *This = (IShellLinkImpl *)iface;
1057
1058     TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1059
1060     if( cchMaxName )
1061         pszName[0] = 0;
1062     if( This->sDescription )
1063         WideCharToMultiByte( CP_ACP, 0, This->sDescription, -1,
1064             pszName, cchMaxName, NULL, NULL);
1065
1066     return S_OK;
1067 }
1068 static HRESULT WINAPI IShellLinkA_fnSetDescription(IShellLinkA * iface, LPCSTR pszName)
1069 {
1070     IShellLinkImpl *This = (IShellLinkImpl *)iface;
1071
1072     TRACE("(%p)->(pName=%s)\n", This, pszName);
1073
1074     HeapFree(GetProcessHeap(), 0, This->sDescription);
1075     This->sDescription = HEAP_strdupAtoW( GetProcessHeap(), 0, pszName);
1076     if ( !This->sDescription )
1077         return E_OUTOFMEMORY;
1078
1079     This->bDirty = TRUE;
1080
1081     return S_OK;
1082 }
1083
1084 static HRESULT WINAPI IShellLinkA_fnGetWorkingDirectory(IShellLinkA * iface, LPSTR pszDir,INT cchMaxPath)
1085 {
1086     IShellLinkImpl *This = (IShellLinkImpl *)iface;
1087
1088     TRACE("(%p)->(%p len=%u)\n", This, pszDir, cchMaxPath);
1089
1090     if( cchMaxPath )
1091         pszDir[0] = 0;
1092     if( This->sWorkDir )
1093         WideCharToMultiByte( CP_ACP, 0, This->sWorkDir, -1,
1094                              pszDir, cchMaxPath, NULL, NULL);
1095
1096     return S_OK;
1097 }
1098
1099 static HRESULT WINAPI IShellLinkA_fnSetWorkingDirectory(IShellLinkA * iface, LPCSTR pszDir)
1100 {
1101     IShellLinkImpl *This = (IShellLinkImpl *)iface;
1102
1103     TRACE("(%p)->(dir=%s)\n",This, pszDir);
1104
1105     HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1106     This->sWorkDir = HEAP_strdupAtoW( GetProcessHeap(), 0, pszDir);
1107     if ( !This->sWorkDir )
1108         return E_OUTOFMEMORY;
1109
1110     This->bDirty = TRUE;
1111
1112     return S_OK;
1113 }
1114
1115 static HRESULT WINAPI IShellLinkA_fnGetArguments(IShellLinkA * iface, LPSTR pszArgs,INT cchMaxPath)
1116 {
1117     IShellLinkImpl *This = (IShellLinkImpl *)iface;
1118
1119     TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1120
1121     if( cchMaxPath )
1122         pszArgs[0] = 0;
1123     if( This->sArgs )
1124         WideCharToMultiByte( CP_ACP, 0, This->sArgs, -1,
1125                              pszArgs, cchMaxPath, NULL, NULL);
1126
1127     return S_OK;
1128 }
1129
1130 static HRESULT WINAPI IShellLinkA_fnSetArguments(IShellLinkA * iface, LPCSTR pszArgs)
1131 {
1132     IShellLinkImpl *This = (IShellLinkImpl *)iface;
1133
1134     TRACE("(%p)->(args=%s)\n",This, pszArgs);
1135
1136     HeapFree(GetProcessHeap(), 0, This->sArgs);
1137     This->sArgs = HEAP_strdupAtoW( GetProcessHeap(), 0, pszArgs);
1138     if( !This->sArgs )
1139         return E_OUTOFMEMORY;
1140
1141     This->bDirty = TRUE;
1142
1143     return S_OK;
1144 }
1145
1146 static HRESULT WINAPI IShellLinkA_fnGetHotkey(IShellLinkA * iface, WORD *pwHotkey)
1147 {
1148         IShellLinkImpl *This = (IShellLinkImpl *)iface;
1149
1150         TRACE("(%p)->(%p)(0x%08x)\n",This, pwHotkey, This->wHotKey);
1151
1152         *pwHotkey = This->wHotKey;
1153
1154         return S_OK;
1155 }
1156
1157 static HRESULT WINAPI IShellLinkA_fnSetHotkey(IShellLinkA * iface, WORD wHotkey)
1158 {
1159         IShellLinkImpl *This = (IShellLinkImpl *)iface;
1160
1161         TRACE("(%p)->(hotkey=%x)\n",This, wHotkey);
1162
1163         This->wHotKey = wHotkey;
1164         This->bDirty = TRUE;
1165
1166         return S_OK;
1167 }
1168
1169 static HRESULT WINAPI IShellLinkA_fnGetShowCmd(IShellLinkA * iface, INT *piShowCmd)
1170 {
1171     IShellLinkImpl *This = (IShellLinkImpl *)iface;
1172
1173     TRACE("(%p)->(%p)\n",This, piShowCmd);
1174     *piShowCmd = This->iShowCmd;
1175     return S_OK;
1176 }
1177
1178 static HRESULT WINAPI IShellLinkA_fnSetShowCmd(IShellLinkA * iface, INT iShowCmd)
1179 {
1180     IShellLinkImpl *This = (IShellLinkImpl *)iface;
1181
1182     TRACE("(%p) %d\n",This, iShowCmd);
1183
1184     This->iShowCmd = iShowCmd;
1185     This->bDirty = TRUE;
1186
1187     return NOERROR;
1188 }
1189
1190 static HRESULT SHELL_PidlGeticonLocationA(IShellFolder* psf, LPITEMIDLIST pidl, LPSTR pszIconPath, int cchIconPath, int* piIcon)
1191 {
1192     LPCITEMIDLIST pidlLast;
1193
1194     HRESULT hr = SHBindToParent(pidl, &IID_IShellFolder, (LPVOID*)&psf, &pidlLast);
1195
1196     if (SUCCEEDED(hr)) {
1197         IExtractIconA* pei;
1198
1199         hr = IShellFolder_GetUIObjectOf(psf, 0, 1, (LPCITEMIDLIST*)&pidlLast, &IID_IExtractIconA, NULL, (LPVOID*)&pei);
1200
1201         if (SUCCEEDED(hr)) {
1202             hr = IExtractIconA_GetIconLocation(pei, 0, pszIconPath, MAX_PATH, piIcon, NULL);
1203
1204             IExtractIconA_Release(pei);
1205         }
1206
1207         IShellFolder_Release(psf);
1208     }
1209
1210     return hr;
1211 }
1212
1213 static HRESULT WINAPI IShellLinkA_fnGetIconLocation(IShellLinkA * iface, LPSTR pszIconPath,INT cchIconPath,INT *piIcon)
1214 {
1215     IShellLinkImpl *This = (IShellLinkImpl *)iface;
1216
1217     TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1218
1219     if (cchIconPath)
1220         pszIconPath[0] = 0;
1221
1222     if (This->sIcoPath) {
1223         WideCharToMultiByte(CP_ACP, 0, This->sIcoPath, -1, pszIconPath, cchIconPath, NULL, NULL);
1224         *piIcon = This->iIcoNdx;
1225         return S_OK;
1226     }
1227
1228     if (This->pPidl || This->sPath) {
1229         IShellFolder* pdsk;
1230
1231         HRESULT hr = SHGetDesktopFolder(&pdsk);
1232
1233         if (SUCCEEDED(hr)) {
1234             /* first look for an icon using the PIDL (if present) */
1235             if (This->pPidl)
1236                 hr = SHELL_PidlGeticonLocationA(pdsk, This->pPidl, pszIconPath, cchIconPath, piIcon);
1237             else
1238                 hr = E_FAIL;
1239
1240             /* if we couldn't find an icon yet, look for it using the file system path */
1241             if (FAILED(hr) && This->sPath) {
1242                 LPITEMIDLIST pidl;
1243
1244                 hr = IShellFolder_ParseDisplayName(pdsk, 0, NULL, This->sPath, NULL, &pidl, NULL);
1245
1246                 if (SUCCEEDED(hr)) {
1247                     hr = SHELL_PidlGeticonLocationA(pdsk, pidl, pszIconPath, cchIconPath, piIcon);
1248
1249                     SHFree(pidl);
1250                 }
1251             }
1252
1253             IShellFolder_Release(pdsk);
1254         }
1255
1256         return hr;
1257     } else
1258         return E_FAIL;
1259 }
1260
1261 static HRESULT WINAPI IShellLinkA_fnSetIconLocation(IShellLinkA * iface, LPCSTR pszIconPath,INT iIcon)
1262 {
1263     IShellLinkImpl *This = (IShellLinkImpl *)iface;
1264
1265     TRACE("(%p)->(path=%s iicon=%u)\n",This, pszIconPath, iIcon);
1266
1267     HeapFree(GetProcessHeap(), 0, This->sIcoPath);
1268     This->sIcoPath = HEAP_strdupAtoW(GetProcessHeap(), 0, pszIconPath);
1269     if ( !This->sIcoPath )
1270         return E_OUTOFMEMORY;
1271
1272     This->iIcoNdx = iIcon;
1273     This->bDirty = TRUE;
1274
1275     return S_OK;
1276 }
1277
1278 static HRESULT WINAPI IShellLinkA_fnSetRelativePath(IShellLinkA * iface, LPCSTR pszPathRel, DWORD dwReserved)
1279 {
1280     IShellLinkImpl *This = (IShellLinkImpl *)iface;
1281
1282     FIXME("(%p)->(path=%s %lx)\n",This, pszPathRel, dwReserved);
1283
1284     HeapFree(GetProcessHeap(), 0, This->sPathRel);
1285     This->sPathRel = HEAP_strdupAtoW(GetProcessHeap(), 0, pszPathRel);
1286     This->bDirty = TRUE;
1287
1288     return ShellLink_UpdatePath(This->sPathRel, This->sPath, This->sWorkDir, &This->sPath);
1289 }
1290
1291 static HRESULT WINAPI IShellLinkA_fnResolve(IShellLinkA * iface, HWND hwnd, DWORD fFlags)
1292 {
1293     HRESULT hr = S_OK;
1294
1295     IShellLinkImpl *This = (IShellLinkImpl *)iface;
1296
1297     FIXME("(%p)->(hwnd=%p flags=%lx)\n",This, hwnd, fFlags);
1298
1299     /*FIXME: use IResolveShellLink interface */
1300
1301     if (!This->sPath && This->pPidl) {
1302         WCHAR buffer[MAX_PATH];
1303
1304         hr = SHELL_GetPathFromIDListW(This->pPidl, buffer, MAX_PATH);
1305
1306         if (SUCCEEDED(hr) && *buffer) {
1307             This->sPath = (LPWSTR) HeapAlloc(GetProcessHeap(), 0, (lstrlenW(buffer)+1)*sizeof(WCHAR));
1308             if (!This->sPath)
1309                 return E_OUTOFMEMORY;
1310
1311             lstrcpyW(This->sPath, buffer);
1312
1313             This->bDirty = TRUE;
1314         } else
1315             hr = S_OK;    /* don't report an error occurred while just caching information */
1316     }
1317
1318     if (!This->sIcoPath && This->sPath) {
1319         This->sIcoPath = (LPWSTR) HeapAlloc(GetProcessHeap(), 0, (lstrlenW(This->sPath)+1)*sizeof(WCHAR));
1320         if (!This->sIcoPath)
1321             return E_OUTOFMEMORY;
1322
1323         lstrcpyW(This->sIcoPath, This->sPath);
1324         This->iIcoNdx = 0;
1325
1326         This->bDirty = TRUE;
1327     }
1328
1329     return hr;
1330 }
1331
1332 static HRESULT WINAPI IShellLinkA_fnSetPath(IShellLinkA * iface, LPCSTR pszFile)
1333 {
1334     IShellLinkImpl *This = (IShellLinkImpl *)iface;
1335     char buffer[MAX_PATH];
1336     LPSTR fname;
1337
1338     TRACE("(%p)->(path=%s)\n",This, pszFile);
1339
1340     if (!GetFullPathNameA(pszFile, MAX_PATH, buffer, &fname))
1341         return E_FAIL;
1342
1343     HeapFree(GetProcessHeap(), 0, This->sPath);
1344     This->sPath = HEAP_strdupAtoW(GetProcessHeap(), 0, buffer);
1345     if( !This->sPath )
1346         return E_OUTOFMEMORY;
1347
1348     This->bDirty = TRUE;
1349
1350     return S_OK;
1351 }
1352
1353 /**************************************************************************
1354 * IShellLink Implementation
1355 */
1356
1357 static IShellLinkAVtbl slvt =
1358 {
1359         IShellLinkA_fnQueryInterface,
1360         IShellLinkA_fnAddRef,
1361         IShellLinkA_fnRelease,
1362         IShellLinkA_fnGetPath,
1363         IShellLinkA_fnGetIDList,
1364         IShellLinkA_fnSetIDList,
1365         IShellLinkA_fnGetDescription,
1366         IShellLinkA_fnSetDescription,
1367         IShellLinkA_fnGetWorkingDirectory,
1368         IShellLinkA_fnSetWorkingDirectory,
1369         IShellLinkA_fnGetArguments,
1370         IShellLinkA_fnSetArguments,
1371         IShellLinkA_fnGetHotkey,
1372         IShellLinkA_fnSetHotkey,
1373         IShellLinkA_fnGetShowCmd,
1374         IShellLinkA_fnSetShowCmd,
1375         IShellLinkA_fnGetIconLocation,
1376         IShellLinkA_fnSetIconLocation,
1377         IShellLinkA_fnSetRelativePath,
1378         IShellLinkA_fnResolve,
1379         IShellLinkA_fnSetPath
1380 };
1381
1382
1383 /**************************************************************************
1384  *  IShellLinkW_fnQueryInterface
1385  */
1386 static HRESULT WINAPI IShellLinkW_fnQueryInterface(
1387   IShellLinkW * iface, REFIID riid, LPVOID *ppvObj)
1388 {
1389         _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1390
1391         return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvObj);
1392 }
1393
1394 /******************************************************************************
1395  * IShellLinkW_fnAddRef
1396  */
1397 static ULONG WINAPI IShellLinkW_fnAddRef(IShellLinkW * iface)
1398 {
1399         _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1400
1401         TRACE("(%p)->(count=%lu)\n",This,This->ref);
1402
1403         return IShellLinkA_AddRef((IShellLinkA*)This);
1404 }
1405 /******************************************************************************
1406  * IShellLinkW_fnRelease
1407  */
1408
1409 static ULONG WINAPI IShellLinkW_fnRelease(IShellLinkW * iface)
1410 {
1411         _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1412
1413         TRACE("(%p)->(count=%lu)\n",This,This->ref);
1414
1415         return IShellLinkA_Release((IShellLinkA*)This);
1416 }
1417
1418 static HRESULT WINAPI IShellLinkW_fnGetPath(IShellLinkW * iface, LPWSTR pszFile,INT cchMaxPath, WIN32_FIND_DATAW *pfd, DWORD fFlags)
1419 {
1420     _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1421
1422     TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%lu)\n",
1423                   This, pszFile, cchMaxPath, pfd, fFlags);
1424
1425     if( cchMaxPath )
1426         pszFile[0] = 0;
1427     if( This->sPath )
1428         lstrcpynW( pszFile, This->sPath, cchMaxPath );
1429
1430     if (pfd) FIXME("(%p): WIN32_FIND_DATA is not yet filled.\n", This);
1431
1432     return NOERROR;
1433 }
1434
1435 static HRESULT WINAPI IShellLinkW_fnGetIDList(IShellLinkW * iface, LPITEMIDLIST * ppidl)
1436 {
1437     _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1438
1439     TRACE("(%p)->(ppidl=%p)\n",This, ppidl);
1440
1441     if( This->pPidl)
1442         *ppidl = ILClone( This->pPidl );
1443     else
1444         *ppidl = NULL;
1445
1446     return S_OK;
1447 }
1448
1449 static HRESULT WINAPI IShellLinkW_fnSetIDList(IShellLinkW * iface, LPCITEMIDLIST pidl)
1450 {
1451     _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1452
1453     TRACE("(%p)->(pidl=%p)\n",This, pidl);
1454
1455     if( This->pPidl )
1456         ILFree( This->pPidl );
1457     This->pPidl = ILClone( pidl );
1458     if( !This->pPidl )
1459         return E_FAIL;
1460
1461     This->bDirty = TRUE;
1462
1463     return S_OK;
1464 }
1465
1466 static HRESULT WINAPI IShellLinkW_fnGetDescription(IShellLinkW * iface, LPWSTR pszName,INT cchMaxName)
1467 {
1468     _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1469
1470     TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1471
1472     if( cchMaxName )
1473         pszName[0] = 0;
1474     if( This->sDescription )
1475         lstrcpynW( pszName, This->sDescription, cchMaxName );
1476
1477     return S_OK;
1478 }
1479
1480 static HRESULT WINAPI IShellLinkW_fnSetDescription(IShellLinkW * iface, LPCWSTR pszName)
1481 {
1482     _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1483
1484     TRACE("(%p)->(desc=%s)\n",This, debugstr_w(pszName));
1485
1486     HeapFree(GetProcessHeap(), 0, This->sDescription);
1487     This->sDescription = HeapAlloc( GetProcessHeap(), 0,
1488                                     (lstrlenW( pszName )+1)*sizeof(WCHAR) );
1489     if ( !This->sDescription )
1490         return E_OUTOFMEMORY;
1491
1492     lstrcpyW( This->sDescription, pszName );
1493     This->bDirty = TRUE;
1494
1495     return S_OK;
1496 }
1497
1498 static HRESULT WINAPI IShellLinkW_fnGetWorkingDirectory(IShellLinkW * iface, LPWSTR pszDir,INT cchMaxPath)
1499 {
1500     _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1501
1502     TRACE("(%p)->(%p len %u)\n", This, pszDir, cchMaxPath);
1503
1504     if( cchMaxPath )
1505         pszDir[0] = 0;
1506     if( This->sWorkDir )
1507         lstrcpynW( pszDir, This->sWorkDir, cchMaxPath );
1508
1509     return S_OK;
1510 }
1511
1512 static HRESULT WINAPI IShellLinkW_fnSetWorkingDirectory(IShellLinkW * iface, LPCWSTR pszDir)
1513 {
1514     _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1515
1516     TRACE("(%p)->(dir=%s)\n",This, debugstr_w(pszDir));
1517
1518     HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1519     This->sWorkDir = HeapAlloc( GetProcessHeap(), 0,
1520                                 (lstrlenW( pszDir )+1)*sizeof (WCHAR) );
1521     if ( !This->sWorkDir )
1522         return E_OUTOFMEMORY;
1523     lstrcpyW( This->sWorkDir, pszDir );
1524     This->bDirty = TRUE;
1525
1526     return S_OK;
1527 }
1528
1529 static HRESULT WINAPI IShellLinkW_fnGetArguments(IShellLinkW * iface, LPWSTR pszArgs,INT cchMaxPath)
1530 {
1531     _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1532
1533     TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1534
1535     if( cchMaxPath )
1536         pszArgs[0] = 0;
1537     if( This->sArgs )
1538         lstrcpynW( pszArgs, This->sArgs, cchMaxPath );
1539
1540     return NOERROR;
1541 }
1542
1543 static HRESULT WINAPI IShellLinkW_fnSetArguments(IShellLinkW * iface, LPCWSTR pszArgs)
1544 {
1545     _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1546
1547     TRACE("(%p)->(args=%s)\n",This, debugstr_w(pszArgs));
1548
1549     HeapFree(GetProcessHeap(), 0, This->sArgs);
1550     This->sArgs = HeapAlloc( GetProcessHeap(), 0,
1551                              (lstrlenW( pszArgs )+1)*sizeof (WCHAR) );
1552     if ( !This->sArgs )
1553         return E_OUTOFMEMORY;
1554     lstrcpyW( This->sArgs, pszArgs );
1555     This->bDirty = TRUE;
1556
1557     return S_OK;
1558 }
1559
1560 static HRESULT WINAPI IShellLinkW_fnGetHotkey(IShellLinkW * iface, WORD *pwHotkey)
1561 {
1562     _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1563
1564     TRACE("(%p)->(%p)\n",This, pwHotkey);
1565
1566     *pwHotkey=This->wHotKey;
1567
1568     return S_OK;
1569 }
1570
1571 static HRESULT WINAPI IShellLinkW_fnSetHotkey(IShellLinkW * iface, WORD wHotkey)
1572 {
1573     _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1574
1575     TRACE("(%p)->(hotkey=%x)\n",This, wHotkey);
1576
1577     This->wHotKey = wHotkey;
1578     This->bDirty = TRUE;
1579
1580     return S_OK;
1581 }
1582
1583 static HRESULT WINAPI IShellLinkW_fnGetShowCmd(IShellLinkW * iface, INT *piShowCmd)
1584 {
1585     _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1586
1587     TRACE("(%p)->(%p)\n",This, piShowCmd);
1588
1589     *piShowCmd = This->iShowCmd;
1590
1591     return S_OK;
1592 }
1593
1594 static HRESULT WINAPI IShellLinkW_fnSetShowCmd(IShellLinkW * iface, INT iShowCmd)
1595 {
1596     _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1597
1598     This->iShowCmd = iShowCmd;
1599     This->bDirty = TRUE;
1600
1601     return S_OK;
1602 }
1603
1604 static HRESULT SHELL_PidlGeticonLocationW(IShellFolder* psf, LPITEMIDLIST pidl, LPWSTR pszIconPath, int cchIconPath, int* piIcon)
1605 {
1606     LPCITEMIDLIST pidlLast;
1607
1608     HRESULT hr = SHBindToParent(pidl, &IID_IShellFolder, (LPVOID*)&psf, &pidlLast);
1609
1610     if (SUCCEEDED(hr)) {
1611         IExtractIconW* pei;
1612
1613         hr = IShellFolder_GetUIObjectOf(psf, 0, 1, (LPCITEMIDLIST*)&pidlLast, &IID_IExtractIconW, NULL, (LPVOID*)&pei);
1614
1615         if (SUCCEEDED(hr)) {
1616             hr = IExtractIconW_GetIconLocation(pei, 0, pszIconPath, MAX_PATH, piIcon, NULL);
1617
1618             IExtractIconW_Release(pei);
1619         }
1620
1621         IShellFolder_Release(psf);
1622     }
1623
1624     return hr;
1625 }
1626
1627 static HRESULT WINAPI IShellLinkW_fnGetIconLocation(IShellLinkW * iface, LPWSTR pszIconPath,INT cchIconPath,INT *piIcon)
1628 {
1629     _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1630
1631     TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1632
1633     if (cchIconPath)
1634         pszIconPath[0] = 0;
1635
1636     if (This->sIcoPath) {
1637         lstrcpynW(pszIconPath, This->sIcoPath, cchIconPath);
1638         *piIcon = This->iIcoNdx;
1639         return S_OK;
1640     }
1641
1642     if (This->pPidl || This->sPath) {
1643         IShellFolder* pdsk;
1644
1645         HRESULT hr = SHGetDesktopFolder(&pdsk);
1646
1647         if (SUCCEEDED(hr)) {
1648             /* first look for an icon using the PIDL (if present) */
1649             if (This->pPidl)
1650                 hr = SHELL_PidlGeticonLocationW(pdsk, This->pPidl, pszIconPath, cchIconPath, piIcon);
1651             else
1652                 hr = E_FAIL;
1653
1654             /* if we couldn't find an icon yet, look for it using the file system path */
1655             if (FAILED(hr) && This->sPath) {
1656                 LPITEMIDLIST pidl;
1657
1658                 hr = IShellFolder_ParseDisplayName(pdsk, 0, NULL, This->sPath, NULL, &pidl, NULL);
1659
1660                 if (SUCCEEDED(hr)) {
1661                     hr = SHELL_PidlGeticonLocationW(pdsk, pidl, pszIconPath, cchIconPath, piIcon);
1662
1663                     SHFree(pidl);
1664                 }
1665             }
1666
1667             IShellFolder_Release(pdsk);
1668         }
1669
1670         return hr;
1671     } else
1672         return E_FAIL;
1673 }
1674
1675 static HRESULT WINAPI IShellLinkW_fnSetIconLocation(IShellLinkW * iface, LPCWSTR pszIconPath,INT iIcon)
1676 {
1677     _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1678
1679     TRACE("(%p)->(path=%s iicon=%u)\n",This, debugstr_w(pszIconPath), iIcon);
1680
1681     HeapFree(GetProcessHeap(), 0, This->sIcoPath);
1682     This->sIcoPath = HeapAlloc( GetProcessHeap(), 0,
1683                                 (lstrlenW( pszIconPath )+1)*sizeof (WCHAR) );
1684     if ( !This->sIcoPath )
1685         return E_OUTOFMEMORY;
1686     lstrcpyW( This->sIcoPath, pszIconPath );
1687
1688     This->iIcoNdx = iIcon;
1689     This->bDirty = TRUE;
1690
1691     return S_OK;
1692 }
1693
1694 static HRESULT WINAPI IShellLinkW_fnSetRelativePath(IShellLinkW * iface, LPCWSTR pszPathRel, DWORD dwReserved)
1695 {
1696     _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1697
1698     TRACE("(%p)->(path=%s %lx)\n",This, debugstr_w(pszPathRel), dwReserved);
1699
1700     HeapFree(GetProcessHeap(), 0, This->sPathRel);
1701     This->sPathRel = HeapAlloc( GetProcessHeap(), 0,
1702                                 (lstrlenW( pszPathRel )+1) * sizeof (WCHAR) );
1703     if ( !This->sPathRel )
1704         return E_OUTOFMEMORY;
1705     lstrcpyW( This->sPathRel, pszPathRel );
1706     This->bDirty = TRUE;
1707
1708     return ShellLink_UpdatePath(This->sPathRel, This->sPath, This->sWorkDir, &This->sPath);
1709 }
1710
1711 static HRESULT WINAPI IShellLinkW_fnResolve(IShellLinkW * iface, HWND hwnd, DWORD fFlags)
1712 {
1713     HRESULT hr = S_OK;
1714
1715     _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1716
1717     FIXME("(%p)->(hwnd=%p flags=%lx)\n",This, hwnd, fFlags);
1718
1719     /*FIXME: use IResolveShellLink interface */
1720
1721     if (!This->sPath && This->pPidl) {
1722         WCHAR buffer[MAX_PATH];
1723
1724         hr = SHELL_GetPathFromIDListW(This->pPidl, buffer, MAX_PATH);
1725
1726         if (SUCCEEDED(hr) && *buffer) {
1727             This->sPath = (LPWSTR) HeapAlloc(GetProcessHeap(), 0, (lstrlenW(buffer)+1)*sizeof(WCHAR));
1728             if (!This->sPath)
1729                 return E_OUTOFMEMORY;
1730
1731             lstrcpyW(This->sPath, buffer);
1732
1733             This->bDirty = TRUE;
1734         } else
1735             hr = S_OK;    /* don't report an error occurred while just caching information */
1736     }
1737
1738     if (!This->sIcoPath && This->sPath) {
1739         This->sIcoPath = (LPWSTR) HeapAlloc(GetProcessHeap(), 0, (lstrlenW(This->sPath)+1)*sizeof(WCHAR));
1740         if (!This->sIcoPath)
1741             return E_OUTOFMEMORY;
1742
1743         lstrcpyW(This->sIcoPath, This->sPath);
1744         This->iIcoNdx = 0;
1745
1746         This->bDirty = TRUE;
1747     }
1748
1749     return hr;
1750 }
1751
1752 static HRESULT WINAPI IShellLinkW_fnSetPath(IShellLinkW * iface, LPCWSTR pszFile)
1753 {
1754     _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1755     WCHAR buffer[MAX_PATH];
1756     LPWSTR fname;
1757
1758     TRACE("(%p)->(path=%s)\n",This, debugstr_w(pszFile));
1759
1760     if (!GetFullPathNameW(pszFile, MAX_PATH, buffer, &fname))
1761         return E_FAIL;
1762
1763     HeapFree(GetProcessHeap(), 0, This->sPath);
1764     This->sPath = HeapAlloc( GetProcessHeap(), 0,
1765                              (lstrlenW( buffer )+1) * sizeof (WCHAR) );
1766     if (!This->sPath)
1767         return E_OUTOFMEMORY;
1768
1769     lstrcpyW(This->sPath, buffer);
1770     This->bDirty = TRUE;
1771
1772     return S_OK;
1773 }
1774
1775 /**************************************************************************
1776 * IShellLinkW Implementation
1777 */
1778
1779 static IShellLinkWVtbl slvtw =
1780 {
1781         IShellLinkW_fnQueryInterface,
1782         IShellLinkW_fnAddRef,
1783         IShellLinkW_fnRelease,
1784         IShellLinkW_fnGetPath,
1785         IShellLinkW_fnGetIDList,
1786         IShellLinkW_fnSetIDList,
1787         IShellLinkW_fnGetDescription,
1788         IShellLinkW_fnSetDescription,
1789         IShellLinkW_fnGetWorkingDirectory,
1790         IShellLinkW_fnSetWorkingDirectory,
1791         IShellLinkW_fnGetArguments,
1792         IShellLinkW_fnSetArguments,
1793         IShellLinkW_fnGetHotkey,
1794         IShellLinkW_fnSetHotkey,
1795         IShellLinkW_fnGetShowCmd,
1796         IShellLinkW_fnSetShowCmd,
1797         IShellLinkW_fnGetIconLocation,
1798         IShellLinkW_fnSetIconLocation,
1799         IShellLinkW_fnSetRelativePath,
1800         IShellLinkW_fnResolve,
1801         IShellLinkW_fnSetPath
1802 };