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