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