Added some more tests.
[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
21 #include "config.h"
22
23 #include <string.h>
24 #include <stdio.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #ifdef HAVE_SYS_WAIT_H
28 # include <sys/wait.h>
29 #endif
30 #include "wine/debug.h"
31 #include "winerror.h"
32 #include "winbase.h"
33 #include "winnls.h"
34 #include "winreg.h"
35
36 #include "shlobj.h"
37 #include "undocshell.h"
38 #include "bitmaps/wine.xpm"
39
40 #include "heap.h"
41 #include "pidl.h"
42 #include "shell32_main.h"
43 #include "shlguid.h"
44
45 WINE_DEFAULT_DEBUG_CHANNEL(shell);
46
47 /* link file formats */
48
49 #include "pshpack1.h"
50
51 /* flag1: lnk elements: simple link has 0x0B */
52 #define WORKDIR         0x10
53 #define ARGUMENT        0x20
54 #define ICON            0x40
55 #define UNC             0x80
56
57 /* fStartup */
58 #define NORMAL          0x01
59 #define MAXIMIZED       0x03
60 #define MINIMIZED       0x07
61
62 typedef struct _LINK_HEADER     
63 {       DWORD   MagicStr;       /* 0x00 'L','\0','\0','\0' */
64         GUID    MagicGuid;      /* 0x04 is CLSID_ShellLink */
65         DWORD   Flag1;          /* 0x14 describes elements following */
66         DWORD   Flag2;          /* 0x18 */
67         FILETIME Time1;         /* 0x1c */
68         FILETIME Time2;         /* 0x24 */
69         FILETIME Time3;         /* 0x2c */
70         DWORD   Unknown1;       /* 0x34 */
71         DWORD   Unknown2;       /* 0x38 icon number */
72         DWORD   fStartup;       /* 0x3c startup type */
73         DWORD   wHotKey;        /* 0x40 hotkey */
74         DWORD   Unknown5;       /* 0x44 */
75         DWORD   Unknown6;       /* 0x48 */
76         USHORT  PidlSize;       /* 0x4c */
77         ITEMIDLIST Pidl;        /* 0x4e */
78 } LINK_HEADER, * PLINK_HEADER;
79
80 #define LINK_HEADER_SIZE (sizeof(LINK_HEADER)-sizeof(ITEMIDLIST))
81
82 typedef struct
83 {
84         BYTE bWidth;
85         BYTE bHeight;
86         BYTE bColorCount;
87         BYTE bReserved;
88         WORD wPlanes;
89         WORD wBitCount;
90         DWORD dwBytesInRes;
91         WORD nID;
92 } GRPICONDIRENTRY;
93
94 typedef struct
95 {
96         WORD idReserved;
97         WORD idType;
98         WORD idCount;
99         GRPICONDIRENTRY idEntries[1];
100 } GRPICONDIR;
101
102 typedef struct
103 {
104         BYTE bWidth;
105         BYTE bHeight;
106         BYTE bColorCount;
107         BYTE bReserved;
108         WORD wPlanes;
109         WORD wBitCount;
110         DWORD dwBytesInRes;
111         DWORD dwImageOffset;
112 } ICONDIRENTRY;
113
114 typedef struct
115 {
116         WORD idReserved;
117         WORD idType;
118         WORD idCount;
119 } ICONDIR;
120
121
122 #include "poppack.h"
123
124 typedef struct
125 {
126         HRSRC *pResInfo;
127         int   nIndex;
128 } ENUMRESSTRUCT;
129
130 static ICOM_VTABLE(IShellLinkA)         slvt;
131 static ICOM_VTABLE(IShellLinkW)         slvtw;
132 static ICOM_VTABLE(IPersistFile)        pfvt;
133 static ICOM_VTABLE(IPersistStream)      psvt;
134
135 /* IShellLink Implementation */
136
137 typedef struct
138 {
139         ICOM_VFIELD(IShellLinkA);
140         DWORD                           ref;
141
142         ICOM_VTABLE(IShellLinkW)*       lpvtblw;
143         ICOM_VTABLE(IPersistFile)*      lpvtblPersistFile;
144         ICOM_VTABLE(IPersistStream)*    lpvtblPersistStream;
145         
146         /* internal stream of the IPersistFile interface */
147         IStream*                        lpFileStream;
148         
149         /* data structures according to the informations in the lnk */
150         LPSTR           sPath;
151         LPITEMIDLIST    pPidl;
152         WORD            wHotKey;
153         SYSTEMTIME      time1;
154         SYSTEMTIME      time2;
155         SYSTEMTIME      time3;
156
157         LPSTR           sIcoPath;
158         INT             iIcoNdx;
159         LPSTR           sArgs;
160         LPSTR           sWorkDir;
161         LPSTR           sDescription;
162 } IShellLinkImpl;
163
164 #define _IShellLinkW_Offset ((int)(&(((IShellLinkImpl*)0)->lpvtblw)))
165 #define _ICOM_THIS_From_IShellLinkW(class, name) class* This = (class*)(((char*)name)-_IShellLinkW_Offset);
166
167 #define _IPersistFile_Offset ((int)(&(((IShellLinkImpl*)0)->lpvtblPersistFile)))
168 #define _ICOM_THIS_From_IPersistFile(class, name) class* This = (class*)(((char*)name)-_IPersistFile_Offset);
169
170 #define _IPersistStream_Offset ((int)(&(((IShellLinkImpl*)0)->lpvtblPersistStream)))
171 #define _ICOM_THIS_From_IPersistStream(class, name) class* This = (class*)(((char*)name)-_IPersistStream_Offset);
172 #define _IPersistStream_From_ICOM_THIS(class, name) class* StreamThis = (class*)(((char*)name)+_IPersistStream_Offset);
173
174
175 /* strdup on the process heap */
176 inline static LPSTR heap_strdup( LPCSTR str )
177 {
178     INT len = strlen(str) + 1;
179     LPSTR p = HeapAlloc( GetProcessHeap(), 0, len );
180     if (p) memcpy( p, str, len );
181     return p;
182 }
183
184
185 /**************************************************************************
186  *  IPersistFile_QueryInterface
187  */
188 static HRESULT WINAPI IPersistFile_fnQueryInterface(
189         IPersistFile* iface,
190         REFIID riid,
191         LPVOID *ppvObj)
192 {
193         _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface)
194
195         TRACE("(%p)\n",This);
196
197         return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvObj);
198 }
199
200 /******************************************************************************
201  * IPersistFile_AddRef
202  */
203 static ULONG WINAPI IPersistFile_fnAddRef(IPersistFile* iface)
204 {
205         _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface)
206
207         TRACE("(%p)->(count=%lu)\n",This,This->ref);
208
209         return IShellLinkA_AddRef((IShellLinkA*)This);
210 }
211 /******************************************************************************
212  * IPersistFile_Release
213  */
214 static ULONG WINAPI IPersistFile_fnRelease(IPersistFile* iface)
215 {
216         _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface)
217
218         TRACE("(%p)->(count=%lu)\n",This,This->ref);
219
220         return IShellLinkA_Release((IShellLinkA*)This);
221 }
222
223 static HRESULT WINAPI IPersistFile_fnGetClassID(IPersistFile* iface, CLSID *pClassID)
224 {
225         _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface)
226         FIXME("(%p)\n",This);
227         return NOERROR;
228 }
229 static HRESULT WINAPI IPersistFile_fnIsDirty(IPersistFile* iface)
230 {
231         _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface)
232         FIXME("(%p)\n",This);
233         return NOERROR;
234 }
235 static HRESULT WINAPI IPersistFile_fnLoad(IPersistFile* iface, LPCOLESTR pszFileName, DWORD dwMode)
236 {
237         _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface)
238         _IPersistStream_From_ICOM_THIS(IPersistStream, This)
239
240         LPSTR           sFile = HEAP_strdupWtoA ( GetProcessHeap(), 0, pszFileName);
241         HRESULT         hRet = E_FAIL;
242
243         TRACE("(%p, %s)\n",This, sFile);
244         
245
246         if (This->lpFileStream)
247           IStream_Release(This->lpFileStream);
248         
249         if SUCCEEDED(CreateStreamOnFile(sFile, &(This->lpFileStream)))
250         {
251           if SUCCEEDED (IPersistStream_Load(StreamThis, This->lpFileStream))
252           {
253             return NOERROR;
254           }
255         }
256         
257         return hRet;
258 }
259
260
261 /* Icon extraction routines
262  *
263  * FIXME: should use PrivateExtractIcons and friends
264  * FIXME: should not use stdio
265  */
266
267 static BOOL SaveIconResAsXPM(const BITMAPINFO *pIcon, const char *szXPMFileName)
268 {
269     FILE *fXPMFile;
270     int nHeight;
271     int nXORWidthBytes;
272     int nANDWidthBytes;
273     BOOL b8BitColors;
274     int nColors;
275     BYTE *pXOR;
276     BYTE *pAND;
277     BOOL aColorUsed[256] = {0};
278     int nColorsUsed = 0;
279     int i,j;
280
281     if (!((pIcon->bmiHeader.biBitCount == 4) || (pIcon->bmiHeader.biBitCount == 8)))
282         return 0;
283
284     if (!(fXPMFile = fopen(szXPMFileName, "w")))
285         return 0;
286
287     nHeight = pIcon->bmiHeader.biHeight / 2;
288     nXORWidthBytes = 4 * ((pIcon->bmiHeader.biWidth * pIcon->bmiHeader.biBitCount / 32)
289                           + ((pIcon->bmiHeader.biWidth * pIcon->bmiHeader.biBitCount % 32) > 0));
290     nANDWidthBytes = 4 * ((pIcon->bmiHeader.biWidth / 32)
291                           + ((pIcon->bmiHeader.biWidth % 32) > 0));
292     b8BitColors = pIcon->bmiHeader.biBitCount == 8;
293     nColors = pIcon->bmiHeader.biClrUsed ? pIcon->bmiHeader.biClrUsed
294         : 1 << pIcon->bmiHeader.biBitCount;
295     pXOR = (BYTE*) pIcon + sizeof (BITMAPINFOHEADER) + (nColors * sizeof (RGBQUAD));
296     pAND = pXOR + nHeight * nXORWidthBytes;
297
298 #define MASK(x,y) (pAND[(x) / 8 + (nHeight - (y) - 1) * nANDWidthBytes] & (1 << (7 - (x) % 8)))
299 #define COLOR(x,y) (b8BitColors ? pXOR[(x) + (nHeight - (y) - 1) * nXORWidthBytes] : (x) % 2 ? pXOR[(x) / 2 + (nHeight - (y) - 1) * nXORWidthBytes] & 0xF : (pXOR[(x) / 2 + (nHeight - (y) - 1) * nXORWidthBytes] & 0xF0) >> 4)
300
301     for (i = 0; i < nHeight; i++)
302         for (j = 0; j < pIcon->bmiHeader.biWidth; j++)
303             if (!aColorUsed[COLOR(j,i)] && !MASK(j,i))
304             {
305                 aColorUsed[COLOR(j,i)] = TRUE;
306                 nColorsUsed++;
307             }
308
309     if (fprintf(fXPMFile, "/* XPM */\nstatic char *icon[] = {\n") <= 0)
310         goto error;
311     if (fprintf(fXPMFile, "\"%d %d %d %d\",\n",
312                 (int) pIcon->bmiHeader.biWidth, nHeight, nColorsUsed + 1, 2) <=0)
313         goto error;
314
315     for (i = 0; i < nColors; i++)
316         if (aColorUsed[i])
317             if (fprintf(fXPMFile, "\"%.2X c #%.2X%.2X%.2X\",\n", i, pIcon->bmiColors[i].rgbRed,
318                         pIcon->bmiColors[i].rgbGreen, pIcon->bmiColors[i].rgbBlue) <= 0)
319                 goto error;
320     if (fprintf(fXPMFile, "\"   c None\"") <= 0)
321         goto error;
322
323     for (i = 0; i < nHeight; i++)
324     {
325         if (fprintf(fXPMFile, ",\n\"") <= 0)
326             goto error;
327         for (j = 0; j < pIcon->bmiHeader.biWidth; j++)
328         {
329             if MASK(j,i)
330                 {
331                     if (fprintf(fXPMFile, "  ") <= 0)
332                         goto error;
333                 }
334             else
335                 if (fprintf(fXPMFile, "%.2X", COLOR(j,i)) <= 0)
336                     goto error;
337         }
338         if (fprintf(fXPMFile, "\"") <= 0)
339             goto error;
340     }
341     if (fprintf(fXPMFile, "};\n") <= 0)
342         goto error;
343
344 #undef MASK
345 #undef COLOR
346
347     fclose(fXPMFile);
348     return 1;
349
350  error:
351     fclose(fXPMFile);
352     unlink( szXPMFileName );
353     return 0;
354 }
355
356 static BOOL CALLBACK EnumResNameProc(HANDLE hModule, const char *lpszType, char *lpszName, LONG lParam)
357 {
358     ENUMRESSTRUCT *sEnumRes = (ENUMRESSTRUCT *) lParam;
359     
360     if (!sEnumRes->nIndex--)
361     {
362       *sEnumRes->pResInfo = FindResourceA(hModule, lpszName, RT_GROUP_ICONA);
363       return FALSE;
364     }
365     else
366       return TRUE;
367 }
368
369 static int ExtractFromEXEDLL(const char *szFileName, int nIndex, const char *szXPMFileName)
370 {
371     HMODULE hModule;
372     HRSRC hResInfo;
373     char *lpName = NULL;
374     HGLOBAL hResData;
375     GRPICONDIR *pIconDir;
376     BITMAPINFO *pIcon;
377     ENUMRESSTRUCT sEnumRes;
378     int nMax = 0;
379     int nMaxBits = 0;
380     int i;
381
382     if (!(hModule = LoadLibraryExA(szFileName, 0, LOAD_LIBRARY_AS_DATAFILE)))
383     {
384         TRACE("LoadLibraryExA (%s) failed, error %ld\n", szFileName, GetLastError());
385         goto error1;
386     }
387
388     if (nIndex < 0)
389     {
390         hResInfo = FindResourceA(hModule, MAKEINTRESOURCEA(-nIndex), RT_GROUP_ICONA);
391         TRACE("FindResourceA (%s) called, return 0x%x, error %ld\n", szFileName, hResInfo, GetLastError());
392     }
393     else
394     {
395         sEnumRes.pResInfo = &hResInfo;
396         sEnumRes.nIndex = nIndex;
397         if (EnumResourceNamesA(hModule, RT_GROUP_ICONA, &EnumResNameProc, (LONG) &sEnumRes))
398         {
399             TRACE("EnumResourceNamesA failed, error %ld\n", GetLastError());
400             goto error2;
401         }
402     }
403
404     if (!hResInfo)
405     {
406         TRACE("ExtractFromEXEDLL failed, error %ld\n", GetLastError());
407         goto error2;
408     }
409
410     if (!(hResData = LoadResource(hModule, hResInfo)))
411     {
412         TRACE("LoadResource failed, error %ld\n", GetLastError());
413         goto error2;
414     }
415     if (!(pIconDir = LockResource(hResData)))
416     {
417         TRACE("LockResource failed, error %ld\n", GetLastError());
418         goto error3;
419     }
420
421     for (i = 0; i < pIconDir->idCount; i++)
422         if ((pIconDir->idEntries[i].wBitCount >= nMaxBits) && (pIconDir->idEntries[i].wBitCount <= 8))
423         {
424           if (pIconDir->idEntries[i].wBitCount > nMaxBits)
425           {
426               nMaxBits = pIconDir->idEntries[i].wBitCount;
427               nMax = 0;
428           }
429           if ((pIconDir->idEntries[i].bHeight * pIconDir->idEntries[i].bWidth) > nMax)
430           {
431               lpName = MAKEINTRESOURCEA(pIconDir->idEntries[i].nID);
432               nMax = pIconDir->idEntries[i].bHeight * pIconDir->idEntries[i].bWidth;
433           }
434         }
435
436     FreeResource(hResData);
437
438     if (!(hResInfo = FindResourceA(hModule, lpName, RT_ICONA)))
439     {
440         TRACE("Second FindResourceA failed, error %ld\n", GetLastError());
441         goto error2;
442     }
443     if (!(hResData = LoadResource(hModule, hResInfo)))
444     {
445         TRACE("Second LoadResource failed, error %ld\n", GetLastError());
446         goto error2;
447     }
448     if (!(pIcon = LockResource(hResData)))
449     {
450         TRACE("Second LockResource failed, error %ld\n", GetLastError());
451         goto error3;
452     }
453
454     if(!SaveIconResAsXPM(pIcon, szXPMFileName))
455     {
456         TRACE("Failed saving icon as XPM, error %ld\n", GetLastError());
457         goto error3;
458     }
459
460     FreeResource(hResData);
461     FreeLibrary(hModule);
462
463     return 1;
464
465  error3:
466     FreeResource(hResData);
467  error2:
468     FreeLibrary(hModule);
469  error1:
470     return 0;
471 }
472
473 static int ExtractFromICO(const char *szFileName, const char *szXPMFileName)
474 {
475     FILE *fICOFile;
476     ICONDIR iconDir;
477     ICONDIRENTRY *pIconDirEntry;
478     int nMax = 0;
479     int nIndex = 0;
480     void *pIcon;
481     int i;
482
483     if (!(fICOFile = fopen(szFileName, "r")))
484         goto error1;
485
486     if (fread(&iconDir, sizeof (ICONDIR), 1, fICOFile) != 1)
487         goto error2;
488     if ((iconDir.idReserved != 0) || (iconDir.idType != 1))
489         goto error2;
490
491     if ((pIconDirEntry = malloc(iconDir.idCount * sizeof (ICONDIRENTRY))) == NULL)
492         goto error2;
493     if (fread(pIconDirEntry, sizeof (ICONDIRENTRY), iconDir.idCount, fICOFile) != iconDir.idCount)
494         goto error3;
495
496     for (i = 0; i < iconDir.idCount; i++)
497         if ((pIconDirEntry[i].bHeight * pIconDirEntry[i].bWidth) > nMax)
498         {
499             nIndex = i;
500             nMax = pIconDirEntry[i].bHeight * pIconDirEntry[i].bWidth;
501         }
502     if ((pIcon = malloc(pIconDirEntry[nIndex].dwBytesInRes)) == NULL)
503         goto error3;
504     if (fseek(fICOFile, pIconDirEntry[nIndex].dwImageOffset, SEEK_SET))
505         goto error4;
506     if (fread(pIcon, pIconDirEntry[nIndex].dwBytesInRes, 1, fICOFile) != 1)
507         goto error4;
508
509     if(!SaveIconResAsXPM(pIcon, szXPMFileName))
510         goto error4;
511
512     free(pIcon);
513     free(pIconDirEntry);
514     fclose(fICOFile);
515
516     return 1;
517
518  error4:
519     free(pIcon);
520  error3:
521     free(pIconDirEntry);
522  error2:
523     fclose(fICOFile);
524  error1:
525     return 0;
526 }
527
528 /* get the Unix file name for a given path, allocating the string */
529 inline static char *get_unix_file_name( const char *dos )
530 {
531     char buffer[MAX_PATH];
532
533     if (!wine_get_unix_file_name( dos, buffer, sizeof(buffer) )) return NULL;
534     return heap_strdup( buffer );
535 }
536
537 static BOOL create_default_icon( const char *filename )
538 {
539     FILE *fXPM;
540     int i;
541
542     if (!(fXPM = fopen(filename, "w"))) return FALSE;
543     fprintf(fXPM, "/* XPM */\nstatic char * icon[] = {");
544     for (i = 0; i < sizeof(wine_xpm)/sizeof(wine_xpm[0]); i++)
545         fprintf( fXPM, "\n\"%s\",", wine_xpm[i]);
546     fprintf( fXPM, "};\n" );
547     fclose( fXPM );
548     return TRUE;
549 }
550
551 /* extract an icon from an exe or icon file; helper for IPersistFile_fnSave */
552 static char *extract_icon( const char *path, int index)
553 {
554     int nodefault = 1;
555     char *filename = heap_strdup( tmpnam(NULL) );
556
557     /* If icon path begins with a '*' then this is a deferred call */
558     if (path[0] == '*')
559     {
560         path++;
561         nodefault = 0;
562     }
563     if (ExtractFromEXEDLL( path, index, filename )) return filename;
564     if (ExtractFromICO( path, filename )) return filename;
565     if (!nodefault)
566         if (create_default_icon( filename )) return filename;
567     HeapFree( GetProcessHeap(), 0, filename );
568     return NULL;
569 }
570
571
572 static HRESULT WINAPI IPersistFile_fnSave(IPersistFile* iface, LPCOLESTR pszFileName, BOOL fRemember)
573 {
574     HRESULT ret = NOERROR;
575     int pid, status;
576     char buffer[MAX_PATH], buff2[MAX_PATH], ascii_filename[MAX_PATH];
577     char *filename, *link_name, *p;
578     char *shell_link_app = NULL;
579     char *icon_name = NULL;
580     char *work_dir = NULL;
581     BOOL bDesktop;
582     HKEY hkey;
583
584     _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
585
586     TRACE("(%p)->(%s)\n",This,debugstr_w(pszFileName));
587
588     if (!pszFileName || !This->sPath)
589         return ERROR_UNKNOWN;
590
591     /* check for .exe extension */
592     if (!(p = strrchr( This->sPath, '.' ))) return NOERROR;
593     if (strchr( p, '\\' ) || strchr( p, '/' )) return NOERROR;
594     if (strcasecmp( p, ".exe" )) return NOERROR;
595
596     /* check if ShellLinker configured */
597     buffer[0] = 0;
598     if (!RegOpenKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\Wine",
599                         0, KEY_ALL_ACCESS, &hkey ))
600     {
601         DWORD type, count = sizeof(buffer);
602         if (RegQueryValueExA( hkey, "ShellLinker", 0, &type, buffer, &count )) buffer[0] = 0;
603         RegCloseKey( hkey );
604     }
605     if (!*buffer) return NOERROR;
606     shell_link_app = heap_strdup( buffer );
607
608     if (!WideCharToMultiByte( CP_ACP, 0, pszFileName, -1, ascii_filename, sizeof(ascii_filename), NULL, NULL))
609         return ERROR_UNKNOWN;
610     GetFullPathNameA( ascii_filename, sizeof(buff2), buff2, NULL );
611     filename = heap_strdup( buff2 );
612
613     if (SHGetSpecialFolderPathA( 0, buffer, CSIDL_STARTUP, FALSE ))
614     {
615         /* ignore startup for now */
616         if (!strncasecmp( filename, buffer, strlen(buffer) )) goto done;
617     }
618     if (SHGetSpecialFolderPathA( 0, buffer, CSIDL_DESKTOPDIRECTORY, FALSE ))
619     {
620         if (!strncasecmp( filename, buffer, strlen(buffer) ))
621         {
622             link_name = filename + strlen(buffer);
623             bDesktop = TRUE;
624             goto found;
625         }
626     }
627     if (SHGetSpecialFolderPathA( 0, buffer, CSIDL_STARTMENU, FALSE ))
628     {
629         if (!strncasecmp( filename, buffer, strlen(buffer) ))
630         {
631             link_name = filename + strlen(buffer);
632             bDesktop = FALSE;
633             goto found;
634         }
635     }
636     goto done;
637
638  found:
639     /* make link name a Unix name */
640     for (p = link_name; *p; p++) if (*p == '\\') *p = '/';
641     /* strip leading slashes */
642     while (*link_name == '/') link_name++;
643     /* remove extension */
644     if ((p = strrchr( link_name, '.' ))) *p = 0;
645
646     /* convert app working dir */
647     if (This->sWorkDir) work_dir = get_unix_file_name( This->sWorkDir );
648
649     /* extract the icon */
650     if (!(icon_name = extract_icon( This->sIcoPath && strlen(This->sIcoPath) ? 
651                                       This->sIcoPath : This->sPath,
652                                       This->iIcoNdx )))
653     {
654         /* Couldn't extract icon --  defer this menu entry to runonce. */
655         HKEY hRunOnce;
656         char* buffer = NULL;
657
658         TRACE("Deferring icon creation to reboot.\n");
659         if (RegCreateKeyExA(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows\\CurrentVersion\\RunOnce", 0,
660                 NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hRunOnce, NULL) != ERROR_SUCCESS)
661         {
662             ret = ERROR_UNKNOWN;
663             goto done;
664         }
665         buffer = HeapAlloc(GetProcessHeap(), 0, MAX_PATH * 3 + (This->sArgs ? strlen(This->sArgs) : 0) +
666                            (This->sDescription ? strlen(This->sDescription) : 0) + 200);
667         sprintf(buffer, "link:%s\xff*%s\xff%d\xff%s\xff%s\xff%s", This->sPath, This->sIcoPath, This->iIcoNdx,
668             This->sArgs ? This->sArgs : "", This->sDescription ? This->sDescription : "",
669             This->sWorkDir ? This->sWorkDir : "");
670         if (RegSetValueExA(hRunOnce, ascii_filename, 0, REG_SZ, buffer, strlen(buffer) + 1) != ERROR_SUCCESS)
671         {
672             HeapFree(GetProcessHeap(), 0, buffer);
673             RegCloseKey(hRunOnce);
674             ret = ERROR_UNKNOWN;
675             goto done;
676         }
677         HeapFree(GetProcessHeap(), 0, buffer);
678         RegCloseKey(hRunOnce);
679         goto done;
680     }
681
682     TRACE("linker app='%s' link='%s' mode=%s path='%s' args='%s' icon='%s' workdir='%s' descr='%s'\n",
683         shell_link_app, link_name, bDesktop ? "desktop" : "menu", This->sPath,
684         This->sArgs ? This->sArgs : "", icon_name, work_dir ? work_dir : "",
685         This->sDescription ? This->sDescription : "" );
686
687     if ((pid = fork()) == -1) goto done;
688     if (!pid)
689     {
690         int pos = 0;
691         char *argv[20];
692         argv[pos++] = shell_link_app;
693         argv[pos++] = "--link";
694         argv[pos++] = link_name;
695         argv[pos++] = "--path";
696         argv[pos++] = This->sPath;
697         argv[pos++] = bDesktop ? "--desktop" : "--menu";
698         if (This->sArgs && strlen(This->sArgs))
699         {
700             argv[pos++] = "--args";
701             argv[pos++] = This->sArgs;
702         }
703         if (icon_name)
704         {
705             argv[pos++] = "--icon";
706             argv[pos++] = icon_name;
707         }
708         if (This->sWorkDir && strlen(This->sWorkDir))
709         {
710             argv[pos++] = "--workdir";
711             argv[pos++] = This->sWorkDir;
712         }
713         if (This->sDescription && strlen(This->sDescription))
714         {
715             argv[pos++] = "--descr";
716             argv[pos++] = This->sDescription;
717         }
718         argv[pos] = NULL;
719         execvp( shell_link_app, argv );
720         _exit(1);
721     }
722
723     while (waitpid( pid, &status, 0 ) == -1)
724     {
725         if (errno != EINTR)
726         {
727             ret = ERROR_UNKNOWN;
728             goto done;
729         }
730     }
731     if (status) ret = E_ACCESSDENIED;
732
733  done:
734     if (icon_name) unlink( icon_name );
735     HeapFree( GetProcessHeap(), 0, shell_link_app );
736     HeapFree( GetProcessHeap(), 0, filename );
737     HeapFree( GetProcessHeap(), 0, icon_name );
738     HeapFree( GetProcessHeap(), 0, work_dir );
739     return ret;
740 }
741
742 static HRESULT WINAPI IPersistFile_fnSaveCompleted(IPersistFile* iface, LPCOLESTR pszFileName)
743 {
744         _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
745         FIXME("(%p)->(%s)\n",This,debugstr_w(pszFileName));
746         return NOERROR;
747 }
748 static HRESULT WINAPI IPersistFile_fnGetCurFile(IPersistFile* iface, LPOLESTR *ppszFileName)
749 {
750         _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
751         FIXME("(%p)\n",This);
752         return NOERROR;
753 }
754
755 static ICOM_VTABLE(IPersistFile) pfvt = 
756 {
757         ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
758         IPersistFile_fnQueryInterface,
759         IPersistFile_fnAddRef,
760         IPersistFile_fnRelease,
761         IPersistFile_fnGetClassID,
762         IPersistFile_fnIsDirty,
763         IPersistFile_fnLoad,
764         IPersistFile_fnSave,
765         IPersistFile_fnSaveCompleted,
766         IPersistFile_fnGetCurFile
767 };
768
769 /************************************************************************
770  * IPersistStream_QueryInterface
771  */
772 static HRESULT WINAPI IPersistStream_fnQueryInterface(
773         IPersistStream* iface,
774         REFIID     riid,
775         VOID**     ppvoid)
776 {
777         _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
778
779         TRACE("(%p)\n",This);
780
781         return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvoid);
782 }
783
784 /************************************************************************
785  * IPersistStream_Release
786  */
787 static ULONG WINAPI IPersistStream_fnRelease(
788         IPersistStream* iface)
789 {
790         _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
791
792         TRACE("(%p)\n",This);
793
794         return IShellLinkA_Release((IShellLinkA*)This);
795 }
796
797 /************************************************************************
798  * IPersistStream_AddRef
799  */
800 static ULONG WINAPI IPersistStream_fnAddRef(
801         IPersistStream* iface)
802 {
803         _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
804
805         TRACE("(%p)\n",This);
806
807         return IShellLinkA_AddRef((IShellLinkA*)This);
808
809
810 /************************************************************************
811  * IPersistStream_GetClassID
812  *
813  */
814 static HRESULT WINAPI IPersistStream_fnGetClassID(
815         IPersistStream* iface,
816         CLSID* pClassID)
817 {
818         _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
819
820         TRACE("(%p)\n", This);
821
822         if (pClassID==0)
823           return E_POINTER;
824
825 /*      memcpy(pClassID, &CLSID_???, sizeof(CLSID_???)); */
826
827         return S_OK;
828 }
829
830 /************************************************************************
831  * IPersistStream_IsDirty (IPersistStream)
832  */
833 static HRESULT WINAPI IPersistStream_fnIsDirty(
834         IPersistStream*  iface)
835 {
836         _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
837
838         TRACE("(%p)\n", This);
839
840         return S_OK;
841 }
842 /************************************************************************
843  * IPersistStream_Load (IPersistStream)
844  */
845
846 static HRESULT WINAPI IPersistStream_fnLoad(
847         IPersistStream*  iface,
848         IStream*         pLoadStream)
849 {
850         PLINK_HEADER lpLinkHeader = HeapAlloc(GetProcessHeap(), 0, LINK_HEADER_SIZE);
851         ULONG   dwBytesRead;
852         DWORD   ret = E_FAIL;
853         char    sTemp[MAX_PATH];
854         
855         _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
856
857         TRACE("(%p)(%p)\n", This, pLoadStream);
858
859         if ( ! pLoadStream)
860         {
861           return STG_E_INVALIDPOINTER;
862         }
863         
864         IStream_AddRef (pLoadStream);
865         if(lpLinkHeader)
866         {
867           if (SUCCEEDED(IStream_Read(pLoadStream, lpLinkHeader, LINK_HEADER_SIZE, &dwBytesRead)))
868           {
869             if ((lpLinkHeader->MagicStr == 0x0000004CL) && IsEqualIID(&lpLinkHeader->MagicGuid, &CLSID_ShellLink))
870             {
871               lpLinkHeader = HeapReAlloc(GetProcessHeap(), 0, lpLinkHeader, LINK_HEADER_SIZE+lpLinkHeader->PidlSize);
872               if (lpLinkHeader)
873               {
874                 if (SUCCEEDED(IStream_Read(pLoadStream, &(lpLinkHeader->Pidl), lpLinkHeader->PidlSize, &dwBytesRead)))
875                 {
876                   if (pcheck (&lpLinkHeader->Pidl))
877                   {     
878                     This->pPidl = ILClone (&lpLinkHeader->Pidl);
879
880                     SHGetPathFromIDListA(&lpLinkHeader->Pidl, sTemp);
881                     This->sPath = heap_strdup( sTemp );
882                   }
883                   This->wHotKey = lpLinkHeader->wHotKey;
884                   FileTimeToSystemTime (&lpLinkHeader->Time1, &This->time1);
885                   FileTimeToSystemTime (&lpLinkHeader->Time2, &This->time2);
886                   FileTimeToSystemTime (&lpLinkHeader->Time3, &This->time3);
887 #if 1
888                   GetDateFormatA(LOCALE_USER_DEFAULT,DATE_SHORTDATE,&This->time1, NULL, sTemp, 256);
889                   TRACE("-- time1: %s\n", sTemp);
890                   GetDateFormatA(LOCALE_USER_DEFAULT,DATE_SHORTDATE,&This->time2, NULL, sTemp, 256);
891                   TRACE("-- time1: %s\n", sTemp);
892                   GetDateFormatA(LOCALE_USER_DEFAULT,DATE_SHORTDATE,&This->time3, NULL, sTemp, 256);
893                   TRACE("-- time1: %s\n", sTemp);
894                   pdump (This->pPidl);
895 #endif            
896                   ret = S_OK;
897                 }
898               }
899             }
900             else
901             {
902               WARN("stream contains no link!\n");
903             }
904           }
905         }
906
907         IStream_Release (pLoadStream);
908
909         pdump(This->pPidl);
910         
911         HeapFree(GetProcessHeap(), 0, lpLinkHeader);
912
913         return ret;
914 }
915
916 /************************************************************************
917  * IPersistStream_Save (IPersistStream)
918  */
919 static HRESULT WINAPI IPersistStream_fnSave(
920         IPersistStream*  iface,
921         IStream*         pOutStream,
922         BOOL             fClearDirty)
923 {
924         _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
925         
926         TRACE("(%p) %p %x\n", This, pOutStream, fClearDirty);
927
928         return E_NOTIMPL;
929 }
930
931 /************************************************************************
932  * IPersistStream_GetSizeMax (IPersistStream)
933  */
934 static HRESULT WINAPI IPersistStream_fnGetSizeMax(
935         IPersistStream*  iface,
936         ULARGE_INTEGER*  pcbSize)
937 {
938         _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
939         
940         TRACE("(%p)\n", This);
941
942         return E_NOTIMPL;
943 }
944
945 static ICOM_VTABLE(IPersistStream) psvt =
946 {
947         ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
948         IPersistStream_fnQueryInterface,
949         IPersistStream_fnAddRef,
950         IPersistStream_fnRelease,
951         IPersistStream_fnGetClassID,
952         IPersistStream_fnIsDirty,
953         IPersistStream_fnLoad,
954         IPersistStream_fnSave,
955         IPersistStream_fnGetSizeMax
956 };
957
958 /**************************************************************************
959  *        IShellLink_Constructor
960  */
961 IShellLinkA * IShellLink_Constructor(BOOL bUnicode) 
962 {       IShellLinkImpl * sl;
963
964         sl = (IShellLinkImpl *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IShellLinkImpl));
965         sl->ref = 1;
966         ICOM_VTBL(sl) = &slvt;
967         sl->lpvtblw = &slvtw;
968         sl->lpvtblPersistFile = &pfvt;
969         sl->lpvtblPersistStream = &psvt;
970         
971         TRACE("(%p)->()\n",sl);
972         shell32_ObjCount++;
973         return bUnicode ? (IShellLinkA *) &(sl->lpvtblw) : (IShellLinkA *)sl;
974 }
975
976 /**************************************************************************
977  *  IShellLinkA_QueryInterface
978  */
979 static HRESULT WINAPI IShellLinkA_fnQueryInterface( IShellLinkA * iface, REFIID riid,  LPVOID *ppvObj)
980 {
981         ICOM_THIS(IShellLinkImpl, iface);
982         
983         TRACE("(%p)->(\n\tIID:\t%s)\n",This,debugstr_guid(riid));
984
985         *ppvObj = NULL;
986
987         if(IsEqualIID(riid, &IID_IUnknown) ||
988            IsEqualIID(riid, &IID_IShellLinkA))
989         {
990           *ppvObj = This;
991         }   
992         else if(IsEqualIID(riid, &IID_IShellLinkW))
993         {
994           *ppvObj = (IShellLinkW *)&(This->lpvtblw);
995         }   
996         else if(IsEqualIID(riid, &IID_IPersistFile))
997         {
998           *ppvObj = (IPersistFile *)&(This->lpvtblPersistFile);
999         }
1000         else if(IsEqualIID(riid, &IID_IPersistStream))
1001         {
1002           *ppvObj = (IPersistStream *)&(This->lpvtblPersistStream);
1003         }   
1004
1005         if(*ppvObj)
1006         {
1007           IUnknown_AddRef((IUnknown*)(*ppvObj));
1008           TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
1009           return S_OK;
1010         }
1011         TRACE("-- Interface: E_NOINTERFACE\n");
1012         return E_NOINTERFACE;
1013 }  
1014 /******************************************************************************
1015  * IShellLinkA_AddRef
1016  */
1017 static ULONG WINAPI IShellLinkA_fnAddRef(IShellLinkA * iface)
1018 {
1019         ICOM_THIS(IShellLinkImpl, iface);
1020         
1021         TRACE("(%p)->(count=%lu)\n",This,This->ref);
1022
1023         shell32_ObjCount++;
1024         return ++(This->ref);
1025 }
1026 /******************************************************************************
1027  *      IShellLinkA_Release
1028  */
1029 static ULONG WINAPI IShellLinkA_fnRelease(IShellLinkA * iface)
1030 {
1031         ICOM_THIS(IShellLinkImpl, iface);
1032         
1033         TRACE("(%p)->(count=%lu)\n",This,This->ref);
1034
1035         shell32_ObjCount--;
1036         if (!--(This->ref)) 
1037         { TRACE("-- destroying IShellLink(%p)\n",This);
1038         
1039           if (This->sIcoPath)
1040             HeapFree(GetProcessHeap(), 0, This->sIcoPath);
1041             
1042           if (This->sArgs)
1043             HeapFree(GetProcessHeap(), 0, This->sArgs);
1044
1045           if (This->sWorkDir)
1046             HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1047             
1048           if (This->sDescription)
1049             HeapFree(GetProcessHeap(), 0, This->sDescription);
1050
1051           if (This->sPath)
1052             HeapFree(GetProcessHeap(),0,This->sPath);
1053
1054           if (This->pPidl)
1055             SHFree(This->pPidl);
1056
1057           if (This->lpFileStream)
1058             IStream_Release(This->lpFileStream);
1059         
1060           This->iIcoNdx = 0;
1061
1062           HeapFree(GetProcessHeap(),0,This);
1063           return 0;
1064         }
1065         return This->ref;
1066 }
1067
1068 static HRESULT WINAPI IShellLinkA_fnGetPath(IShellLinkA * iface, LPSTR pszFile,INT cchMaxPath, WIN32_FIND_DATAA *pfd, DWORD fFlags)
1069 {
1070         ICOM_THIS(IShellLinkImpl, iface);
1071         
1072         TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%lu)(%s)\n",This, pszFile, cchMaxPath, pfd, fFlags, debugstr_a(This->sPath));
1073
1074         if (This->sPath)
1075           lstrcpynA(pszFile,This->sPath, cchMaxPath);
1076         else
1077           return E_FAIL;
1078
1079         return NOERROR;
1080 }
1081 static HRESULT WINAPI IShellLinkA_fnGetIDList(IShellLinkA * iface, LPITEMIDLIST * ppidl)
1082 {
1083         ICOM_THIS(IShellLinkImpl, iface);
1084         
1085         TRACE("(%p)->(ppidl=%p)\n",This, ppidl);
1086
1087         *ppidl = ILClone(This->pPidl);
1088         return NOERROR;
1089 }
1090 static HRESULT WINAPI IShellLinkA_fnSetIDList(IShellLinkA * iface, LPCITEMIDLIST pidl)
1091 {
1092         ICOM_THIS(IShellLinkImpl, iface);
1093         
1094         TRACE("(%p)->(pidl=%p)\n",This, pidl);
1095
1096         if (This->pPidl)
1097             SHFree(This->pPidl);
1098         This->pPidl = ILClone (pidl);
1099         return NOERROR;
1100 }
1101 static HRESULT WINAPI IShellLinkA_fnGetDescription(IShellLinkA * iface, LPSTR pszName,INT cchMaxName)
1102 {
1103         ICOM_THIS(IShellLinkImpl, iface);
1104         
1105         FIXME("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1106         lstrcpynA(pszName,"Description, FIXME",cchMaxName);
1107         return NOERROR;
1108 }
1109 static HRESULT WINAPI IShellLinkA_fnSetDescription(IShellLinkA * iface, LPCSTR pszName)
1110 {
1111         ICOM_THIS(IShellLinkImpl, iface);
1112         
1113         TRACE("(%p)->(pName=%s)\n", This, pszName);
1114
1115         if (This->sDescription)
1116             HeapFree(GetProcessHeap(), 0, This->sDescription);
1117         if (!(This->sDescription = heap_strdup(pszName)))
1118             return E_OUTOFMEMORY;
1119
1120         return NOERROR;
1121 }
1122 static HRESULT WINAPI IShellLinkA_fnGetWorkingDirectory(IShellLinkA * iface, LPSTR pszDir,INT cchMaxPath)
1123 {
1124         ICOM_THIS(IShellLinkImpl, iface);
1125         
1126         TRACE("(%p)->(%p len=%u)\n", This, pszDir, cchMaxPath);
1127
1128         lstrcpynA( pszDir, This->sWorkDir ? This->sWorkDir : "", cchMaxPath );
1129
1130         return NOERROR;
1131 }
1132 static HRESULT WINAPI IShellLinkA_fnSetWorkingDirectory(IShellLinkA * iface, LPCSTR pszDir)
1133 {
1134         ICOM_THIS(IShellLinkImpl, iface);
1135         
1136         TRACE("(%p)->(dir=%s)\n",This, pszDir);
1137
1138         if (This->sWorkDir)
1139             HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1140         if (!(This->sWorkDir = heap_strdup(pszDir)))
1141             return E_OUTOFMEMORY;
1142
1143         return NOERROR;
1144 }
1145 static HRESULT WINAPI IShellLinkA_fnGetArguments(IShellLinkA * iface, LPSTR pszArgs,INT cchMaxPath)
1146 {
1147         ICOM_THIS(IShellLinkImpl, iface);
1148         
1149         TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1150
1151         lstrcpynA( pszArgs, This->sArgs ? This->sArgs : "", cchMaxPath );
1152
1153         return NOERROR;
1154 }
1155 static HRESULT WINAPI IShellLinkA_fnSetArguments(IShellLinkA * iface, LPCSTR pszArgs)
1156 {
1157         ICOM_THIS(IShellLinkImpl, iface);
1158         
1159         TRACE("(%p)->(args=%s)\n",This, pszArgs);
1160
1161         if (This->sArgs)
1162             HeapFree(GetProcessHeap(), 0, This->sArgs);
1163         if (!(This->sArgs = heap_strdup(pszArgs)))
1164             return E_OUTOFMEMORY;
1165
1166         return NOERROR;
1167 }
1168 static HRESULT WINAPI IShellLinkA_fnGetHotkey(IShellLinkA * iface, WORD *pwHotkey)
1169 {
1170         ICOM_THIS(IShellLinkImpl, iface);
1171         
1172         TRACE("(%p)->(%p)(0x%08x)\n",This, pwHotkey, This->wHotKey);
1173
1174         *pwHotkey = This->wHotKey;
1175
1176         return NOERROR;
1177 }
1178 static HRESULT WINAPI IShellLinkA_fnSetHotkey(IShellLinkA * iface, WORD wHotkey)
1179 {
1180         ICOM_THIS(IShellLinkImpl, iface);
1181         
1182         TRACE("(%p)->(hotkey=%x)\n",This, wHotkey);
1183         
1184         This->wHotKey = wHotkey;
1185
1186         return NOERROR;
1187 }
1188 static HRESULT WINAPI IShellLinkA_fnGetShowCmd(IShellLinkA * iface, INT *piShowCmd)
1189 {
1190         ICOM_THIS(IShellLinkImpl, iface);
1191         
1192         FIXME("(%p)->(%p)\n",This, piShowCmd);
1193         *piShowCmd=0;
1194         return NOERROR;
1195 }
1196 static HRESULT WINAPI IShellLinkA_fnSetShowCmd(IShellLinkA * iface, INT iShowCmd)
1197 {
1198         ICOM_THIS(IShellLinkImpl, iface);
1199         
1200         FIXME("(%p)->(showcmd=%x)\n",This, iShowCmd);
1201         return NOERROR;
1202 }
1203 static HRESULT WINAPI IShellLinkA_fnGetIconLocation(IShellLinkA * iface, LPSTR pszIconPath,INT cchIconPath,INT *piIcon)
1204 {
1205         ICOM_THIS(IShellLinkImpl, iface);
1206         
1207         TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1208
1209         lstrcpynA( pszIconPath, This->sIcoPath ? This->sIcoPath : "", cchIconPath );
1210         *piIcon = This->iIcoNdx;
1211
1212         return NOERROR;
1213 }
1214 static HRESULT WINAPI IShellLinkA_fnSetIconLocation(IShellLinkA * iface, LPCSTR pszIconPath,INT iIcon)
1215 {
1216         ICOM_THIS(IShellLinkImpl, iface);
1217         
1218         TRACE("(%p)->(path=%s iicon=%u)\n",This, pszIconPath, iIcon);
1219         
1220         if (This->sIcoPath)
1221             HeapFree(GetProcessHeap(), 0, This->sIcoPath);
1222         if (!(This->sIcoPath = heap_strdup(pszIconPath)))
1223             return E_OUTOFMEMORY;       
1224         This->iIcoNdx = iIcon;
1225         
1226         return NOERROR;
1227 }
1228 static HRESULT WINAPI IShellLinkA_fnSetRelativePath(IShellLinkA * iface, LPCSTR pszPathRel, DWORD dwReserved)
1229 {
1230         ICOM_THIS(IShellLinkImpl, iface);
1231         
1232         FIXME("(%p)->(path=%s %lx)\n",This, pszPathRel, dwReserved);
1233         return NOERROR;
1234 }
1235 static HRESULT WINAPI IShellLinkA_fnResolve(IShellLinkA * iface, HWND hwnd, DWORD fFlags)
1236 {
1237         ICOM_THIS(IShellLinkImpl, iface);
1238         
1239         FIXME("(%p)->(hwnd=%x flags=%lx)\n",This, hwnd, fFlags);
1240         return NOERROR;
1241 }
1242 static HRESULT WINAPI IShellLinkA_fnSetPath(IShellLinkA * iface, LPCSTR pszFile)
1243 {
1244         ICOM_THIS(IShellLinkImpl, iface);
1245         
1246         TRACE("(%p)->(path=%s)\n",This, pszFile);
1247
1248         if (This->sPath)
1249             HeapFree(GetProcessHeap(), 0, This->sPath);
1250         if (!(This->sPath = heap_strdup(pszFile)))
1251             return E_OUTOFMEMORY;
1252         
1253         return NOERROR;
1254 }
1255
1256 /**************************************************************************
1257 * IShellLink Implementation
1258 */
1259
1260 static ICOM_VTABLE(IShellLinkA) slvt = 
1261 {       
1262         ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1263         IShellLinkA_fnQueryInterface,
1264         IShellLinkA_fnAddRef,
1265         IShellLinkA_fnRelease,
1266         IShellLinkA_fnGetPath,
1267         IShellLinkA_fnGetIDList,
1268         IShellLinkA_fnSetIDList,
1269         IShellLinkA_fnGetDescription,
1270         IShellLinkA_fnSetDescription,
1271         IShellLinkA_fnGetWorkingDirectory,
1272         IShellLinkA_fnSetWorkingDirectory,
1273         IShellLinkA_fnGetArguments,
1274         IShellLinkA_fnSetArguments,
1275         IShellLinkA_fnGetHotkey,
1276         IShellLinkA_fnSetHotkey,
1277         IShellLinkA_fnGetShowCmd,
1278         IShellLinkA_fnSetShowCmd,
1279         IShellLinkA_fnGetIconLocation,
1280         IShellLinkA_fnSetIconLocation,
1281         IShellLinkA_fnSetRelativePath,
1282         IShellLinkA_fnResolve,
1283         IShellLinkA_fnSetPath
1284 };
1285
1286
1287 /**************************************************************************
1288  *  IShellLinkW_fnQueryInterface
1289  */
1290 static HRESULT WINAPI IShellLinkW_fnQueryInterface(
1291   IShellLinkW * iface, REFIID riid, LPVOID *ppvObj)
1292 {
1293         _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1294         
1295         return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvObj);
1296 }
1297
1298 /******************************************************************************
1299  * IShellLinkW_fnAddRef
1300  */
1301 static ULONG WINAPI IShellLinkW_fnAddRef(IShellLinkW * iface)
1302 {
1303         _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1304         
1305         TRACE("(%p)->(count=%lu)\n",This,This->ref);
1306
1307         return IShellLinkA_AddRef((IShellLinkA*)This);
1308 }
1309 /******************************************************************************
1310  * IShellLinkW_fnRelease
1311  */
1312
1313 static ULONG WINAPI IShellLinkW_fnRelease(IShellLinkW * iface)
1314 {
1315         _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1316         
1317         TRACE("(%p)->(count=%lu)\n",This,This->ref);
1318
1319         return IShellLinkA_Release((IShellLinkA*)This);
1320 }
1321
1322 static HRESULT WINAPI IShellLinkW_fnGetPath(IShellLinkW * iface, LPWSTR pszFile,INT cchMaxPath, WIN32_FIND_DATAA *pfd, DWORD fFlags)
1323 {
1324         _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1325         
1326         FIXME("(%p)->(pfile=%p len=%u find_data=%p flags=%lu)\n",This, pszFile, cchMaxPath, pfd, fFlags);
1327         MultiByteToWideChar( CP_ACP, 0, "c:\\foo.bar", -1, pszFile, cchMaxPath );
1328         return NOERROR;
1329 }
1330
1331 static HRESULT WINAPI IShellLinkW_fnGetIDList(IShellLinkW * iface, LPITEMIDLIST * ppidl)
1332 {
1333         _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1334         
1335         FIXME("(%p)->(ppidl=%p)\n",This, ppidl);
1336         *ppidl = _ILCreateDesktop();
1337         return NOERROR;
1338 }
1339
1340 static HRESULT WINAPI IShellLinkW_fnSetIDList(IShellLinkW * iface, LPCITEMIDLIST pidl)
1341 {
1342         _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1343         
1344         FIXME("(%p)->(pidl=%p)\n",This, pidl);
1345         return NOERROR;
1346 }
1347
1348 static HRESULT WINAPI IShellLinkW_fnGetDescription(IShellLinkW * iface, LPWSTR pszName,INT cchMaxName)
1349 {
1350         _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1351         
1352         FIXME("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1353         MultiByteToWideChar( CP_ACP, 0, "Description, FIXME", -1, pszName, cchMaxName );
1354         return NOERROR;
1355 }
1356
1357 static HRESULT WINAPI IShellLinkW_fnSetDescription(IShellLinkW * iface, LPCWSTR pszName)
1358 {
1359         _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1360         
1361         TRACE("(%p)->(desc=%s)\n",This, debugstr_w(pszName));
1362
1363         if (This->sDescription)
1364             HeapFree(GetProcessHeap(), 0, This->sDescription);
1365         if (!(This->sDescription = HEAP_strdupWtoA(GetProcessHeap(), 0, pszName)))
1366             return E_OUTOFMEMORY;
1367                 
1368         return NOERROR;
1369 }
1370
1371 static HRESULT WINAPI IShellLinkW_fnGetWorkingDirectory(IShellLinkW * iface, LPWSTR pszDir,INT cchMaxPath)
1372 {
1373         _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1374         
1375         TRACE("(%p)->(%p len %u)\n", This, pszDir, cchMaxPath);
1376
1377         MultiByteToWideChar( CP_ACP, 0, This->sWorkDir ? This->sWorkDir : "", -1, pszDir, cchMaxPath );
1378
1379         return NOERROR;
1380 }
1381
1382 static HRESULT WINAPI IShellLinkW_fnSetWorkingDirectory(IShellLinkW * iface, LPCWSTR pszDir)
1383 {
1384         _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1385         
1386         TRACE("(%p)->(dir=%s)\n",This, debugstr_w(pszDir));
1387
1388         if (This->sWorkDir)
1389             HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1390         if (!(This->sWorkDir = HEAP_strdupWtoA(GetProcessHeap(), 0, pszDir)))
1391             return E_OUTOFMEMORY;
1392
1393         return NOERROR;
1394 }
1395
1396 static HRESULT WINAPI IShellLinkW_fnGetArguments(IShellLinkW * iface, LPWSTR pszArgs,INT cchMaxPath)
1397 {
1398         _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1399         
1400         TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1401
1402         MultiByteToWideChar( CP_ACP, 0, This->sArgs ? This->sArgs : "", -1, pszArgs, cchMaxPath );
1403
1404         return NOERROR;
1405 }
1406
1407 static HRESULT WINAPI IShellLinkW_fnSetArguments(IShellLinkW * iface, LPCWSTR pszArgs)
1408 {
1409         _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1410         
1411         TRACE("(%p)->(args=%s)\n",This, debugstr_w(pszArgs));
1412         
1413         if (This->sArgs)
1414             HeapFree(GetProcessHeap(), 0, This->sArgs);
1415         if (!(This->sArgs = HEAP_strdupWtoA(GetProcessHeap(), 0, pszArgs)))
1416             return E_OUTOFMEMORY;
1417         
1418         return NOERROR;
1419 }
1420
1421 static HRESULT WINAPI IShellLinkW_fnGetHotkey(IShellLinkW * iface, WORD *pwHotkey)
1422 {
1423         _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1424         
1425         FIXME("(%p)->(%p)\n",This, pwHotkey);
1426         *pwHotkey=0x0;
1427         return NOERROR;
1428 }
1429
1430 static HRESULT WINAPI IShellLinkW_fnSetHotkey(IShellLinkW * iface, WORD wHotkey)
1431 {
1432         _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1433         
1434         FIXME("(%p)->(hotkey=%x)\n",This, wHotkey);
1435         return NOERROR;
1436 }
1437
1438 static HRESULT WINAPI IShellLinkW_fnGetShowCmd(IShellLinkW * iface, INT *piShowCmd)
1439 {
1440         _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1441         
1442         FIXME("(%p)->(%p)\n",This, piShowCmd);
1443         *piShowCmd=0;
1444         return NOERROR;
1445 }
1446
1447 static HRESULT WINAPI IShellLinkW_fnSetShowCmd(IShellLinkW * iface, INT iShowCmd)
1448 {
1449         _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1450         
1451         FIXME("(%p)->(showcmd=%x)\n",This, iShowCmd);
1452         return NOERROR;
1453 }
1454
1455 static HRESULT WINAPI IShellLinkW_fnGetIconLocation(IShellLinkW * iface, LPWSTR pszIconPath,INT cchIconPath,INT *piIcon)
1456 {
1457         _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1458         
1459         TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1460
1461         MultiByteToWideChar( CP_ACP, 0, This->sIcoPath ? This->sIcoPath : "", -1, pszIconPath, cchIconPath );
1462         *piIcon = This->iIcoNdx;
1463
1464         return NOERROR;
1465 }
1466
1467 static HRESULT WINAPI IShellLinkW_fnSetIconLocation(IShellLinkW * iface, LPCWSTR pszIconPath,INT iIcon)
1468 {
1469         _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1470         
1471         TRACE("(%p)->(path=%s iicon=%u)\n",This, debugstr_w(pszIconPath), iIcon);
1472
1473         if (This->sIcoPath)
1474             HeapFree(GetProcessHeap(), 0, This->sIcoPath);
1475         if (!(This->sIcoPath = HEAP_strdupWtoA(GetProcessHeap(), 0, pszIconPath)))
1476             return E_OUTOFMEMORY;       
1477         This->iIcoNdx = iIcon;
1478
1479         return NOERROR;
1480 }
1481
1482 static HRESULT WINAPI IShellLinkW_fnSetRelativePath(IShellLinkW * iface, LPCWSTR pszPathRel, DWORD dwReserved)
1483 {
1484         _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1485         
1486         FIXME("(%p)->(path=%s %lx)\n",This, debugstr_w(pszPathRel), dwReserved);
1487         return NOERROR;
1488 }
1489
1490 static HRESULT WINAPI IShellLinkW_fnResolve(IShellLinkW * iface, HWND hwnd, DWORD fFlags)
1491 {
1492         _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1493         
1494         FIXME("(%p)->(hwnd=%x flags=%lx)\n",This, hwnd, fFlags);
1495         return NOERROR;
1496 }
1497
1498 static HRESULT WINAPI IShellLinkW_fnSetPath(IShellLinkW * iface, LPCWSTR pszFile)
1499 {
1500         _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1501         
1502         TRACE("(%p)->(path=%s)\n",This, debugstr_w(pszFile));
1503         
1504         if (This->sPath)
1505             HeapFree(GetProcessHeap(), 0, This->sPath);
1506         if (!(This->sPath = HEAP_strdupWtoA(GetProcessHeap(), 0, pszFile)))
1507             return E_OUTOFMEMORY;       
1508         
1509         return NOERROR;
1510 }
1511
1512 /**************************************************************************
1513 * IShellLinkW Implementation
1514 */
1515
1516 static ICOM_VTABLE(IShellLinkW) slvtw = 
1517 {       
1518         ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1519         IShellLinkW_fnQueryInterface,
1520         IShellLinkW_fnAddRef,
1521         IShellLinkW_fnRelease,
1522         IShellLinkW_fnGetPath,
1523         IShellLinkW_fnGetIDList,
1524         IShellLinkW_fnSetIDList,
1525         IShellLinkW_fnGetDescription,
1526         IShellLinkW_fnSetDescription,
1527         IShellLinkW_fnGetWorkingDirectory,
1528         IShellLinkW_fnSetWorkingDirectory,
1529         IShellLinkW_fnGetArguments,
1530         IShellLinkW_fnSetArguments,
1531         IShellLinkW_fnGetHotkey,
1532         IShellLinkW_fnSetHotkey,
1533         IShellLinkW_fnGetShowCmd,
1534         IShellLinkW_fnSetShowCmd,
1535         IShellLinkW_fnGetIconLocation,
1536         IShellLinkW_fnSetIconLocation,
1537         IShellLinkW_fnSetRelativePath,
1538         IShellLinkW_fnResolve,
1539         IShellLinkW_fnSetPath
1540 };
1541