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