shell32/tests: Make sure return values are used (LLVM/Clang).
[wine] / dlls / shell32 / tests / shelllink.c
1 /*
2  * Unit tests for shelllinks
3  *
4  * Copyright 2004 Mike McCormack
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  * This is a test program for the SHGet{Special}Folder{Path|Location} functions
20  * of shell32, that get either a filesystem path or a LPITEMIDLIST (shell
21  * namespace) path for a given folder (CSIDL value).
22  *
23  */
24
25 #define COBJMACROS
26
27 #include "initguid.h"
28 #include "windows.h"
29 #include "shlguid.h"
30 #include "shobjidl.h"
31 #include "shlobj.h"
32 #include "wine/test.h"
33
34 #include "shell32_test.h"
35
36 #ifndef SLDF_HAS_LOGO3ID
37 #  define SLDF_HAS_LOGO3ID 0x00000800 /* not available in the Vista SDK */
38 #endif
39
40 typedef void (WINAPI *fnILFree)(LPITEMIDLIST);
41 typedef BOOL (WINAPI *fnILIsEqual)(LPCITEMIDLIST, LPCITEMIDLIST);
42 typedef HRESULT (WINAPI *fnSHILCreateFromPath)(LPCWSTR, LPITEMIDLIST *,DWORD*);
43 typedef HRESULT (WINAPI *fnSHDefExtractIconA)(LPCSTR, int, UINT, HICON*, HICON*, UINT);
44
45 static fnILFree pILFree;
46 static fnILIsEqual pILIsEqual;
47 static fnSHILCreateFromPath pSHILCreateFromPath;
48 static fnSHDefExtractIconA pSHDefExtractIconA;
49
50 static DWORD (WINAPI *pGetLongPathNameA)(LPCSTR, LPSTR, DWORD);
51 static DWORD (WINAPI *pGetShortPathNameA)(LPCSTR, LPSTR, DWORD);
52
53 static const GUID _IID_IShellLinkDataList = {
54     0x45e2b4ae, 0xb1c3, 0x11d0,
55     { 0xb9, 0x2f, 0x00, 0xa0, 0xc9, 0x03, 0x12, 0xe1 }
56 };
57
58 static const WCHAR notafile[]= { 'C',':','\\','n','o','n','e','x','i','s','t','e','n','t','\\','f','i','l','e',0 };
59
60
61 /* For some reason SHILCreateFromPath does not work on Win98 and
62  * SHSimpleIDListFromPathA does not work on NT4. But if we call both we
63  * get what we want on all platforms.
64  */
65 static LPITEMIDLIST (WINAPI *pSHSimpleIDListFromPathAW)(LPCVOID);
66
67 static LPITEMIDLIST path_to_pidl(const char* path)
68 {
69     LPITEMIDLIST pidl;
70
71     if (!pSHSimpleIDListFromPathAW)
72     {
73         HMODULE hdll=GetModuleHandleA("shell32.dll");
74         pSHSimpleIDListFromPathAW=(void*)GetProcAddress(hdll, (char*)162);
75         if (!pSHSimpleIDListFromPathAW)
76             win_skip("SHSimpleIDListFromPathAW not found in shell32.dll\n");
77     }
78
79     pidl=NULL;
80     /* pSHSimpleIDListFromPathAW maps to A on non NT platforms */
81     if (pSHSimpleIDListFromPathAW && (GetVersion() & 0x80000000))
82         pidl=pSHSimpleIDListFromPathAW(path);
83
84     if (!pidl)
85     {
86         WCHAR* pathW;
87         HRESULT r;
88         int len;
89
90         len=MultiByteToWideChar(CP_ACP, 0, path, -1, NULL, 0);
91         pathW=HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
92         MultiByteToWideChar(CP_ACP, 0, path, -1, pathW, len);
93
94         r=pSHILCreateFromPath(pathW, &pidl, NULL);
95         ok(r == S_OK, "SHILCreateFromPath failed (0x%08x)\n", r);
96         HeapFree(GetProcessHeap(), 0, pathW);
97     }
98     return pidl;
99 }
100
101
102 /*
103  * Test manipulation of an IShellLink's properties.
104  */
105
106 static void test_get_set(void)
107 {
108     HRESULT r;
109     IShellLinkA *sl;
110     IShellLinkW *slW = NULL;
111     char mypath[MAX_PATH];
112     char buffer[INFOTIPSIZE];
113     LPITEMIDLIST pidl, tmp_pidl;
114     const char * str;
115     int i;
116     WORD w;
117
118     r = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
119                          &IID_IShellLinkA, (LPVOID*)&sl);
120     ok(r == S_OK, "no IID_IShellLinkA (0x%08x)\n", r);
121     if (r != S_OK)
122         return;
123
124     /* Test Getting / Setting the description */
125     strcpy(buffer,"garbage");
126     r = IShellLinkA_GetDescription(sl, buffer, sizeof(buffer));
127     ok(r == S_OK, "GetDescription failed (0x%08x)\n", r);
128     ok(*buffer=='\0', "GetDescription returned '%s'\n", buffer);
129
130     str="Some description";
131     r = IShellLinkA_SetDescription(sl, str);
132     ok(r == S_OK, "SetDescription failed (0x%08x)\n", r);
133
134     strcpy(buffer,"garbage");
135     r = IShellLinkA_GetDescription(sl, buffer, sizeof(buffer));
136     ok(r == S_OK, "GetDescription failed (0x%08x)\n", r);
137     ok(lstrcmp(buffer,str)==0, "GetDescription returned '%s'\n", buffer);
138
139     r = IShellLinkA_SetDescription(sl, NULL);
140     ok(r == S_OK, "SetDescription failed (0x%08x)\n", r);
141
142     strcpy(buffer,"garbage");
143     r = IShellLinkA_GetDescription(sl, buffer, sizeof(buffer));
144     ok(r == S_OK, "GetDescription failed (0x%08x)\n", r);
145     ok(*buffer=='\0' || broken(lstrcmp(buffer,str)==0), "GetDescription returned '%s'\n", buffer); /* NT4 */
146
147
148     /* Test Getting / Setting the work directory */
149     strcpy(buffer,"garbage");
150     r = IShellLinkA_GetWorkingDirectory(sl, buffer, sizeof(buffer));
151     ok(r == S_OK, "GetWorkingDirectory failed (0x%08x)\n", r);
152     ok(*buffer=='\0', "GetWorkingDirectory returned '%s'\n", buffer);
153
154     str="c:\\nonexistent\\directory";
155     r = IShellLinkA_SetWorkingDirectory(sl, str);
156     ok(r == S_OK, "SetWorkingDirectory failed (0x%08x)\n", r);
157
158     strcpy(buffer,"garbage");
159     r = IShellLinkA_GetWorkingDirectory(sl, buffer, sizeof(buffer));
160     ok(r == S_OK, "GetWorkingDirectory failed (0x%08x)\n", r);
161     ok(lstrcmpi(buffer,str)==0, "GetWorkingDirectory returned '%s'\n", buffer);
162
163     /* Test Getting / Setting the path */
164     strcpy(buffer,"garbage");
165     r = IShellLinkA_GetPath(sl, buffer, sizeof(buffer), NULL, SLGP_RAWPATH);
166     todo_wine ok(r == S_FALSE || broken(r == S_OK) /* NT4/W2K */, "GetPath failed (0x%08x)\n", r);
167     ok(*buffer=='\0', "GetPath returned '%s'\n", buffer);
168
169     CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
170                      &IID_IShellLinkW, (LPVOID*)&slW);
171     if (!slW /* Win9x */ || !pGetLongPathNameA /* NT4 */)
172         skip("SetPath with NULL parameter crashes on Win9x and some NT4\n");
173     else
174     {
175         IShellLinkW_Release(slW);
176         r = IShellLinkA_SetPath(sl, NULL);
177         ok(r==E_INVALIDARG ||
178            broken(r==S_OK), /* Some Win95 and NT4 */
179            "SetPath returned wrong error (0x%08x)\n", r);
180     }
181
182     r = IShellLinkA_SetPath(sl, "");
183     ok(r==S_OK, "SetPath failed (0x%08x)\n", r);
184
185     strcpy(buffer,"garbage");
186     r = IShellLinkA_GetPath(sl, buffer, sizeof(buffer), NULL, SLGP_RAWPATH);
187     todo_wine ok(r == S_FALSE, "GetPath failed (0x%08x)\n", r);
188     ok(*buffer=='\0', "GetPath returned '%s'\n", buffer);
189
190     /* Win98 returns S_FALSE, but WinXP returns S_OK */
191     str="c:\\nonexistent\\file";
192     r = IShellLinkA_SetPath(sl, str);
193     ok(r==S_FALSE || r==S_OK, "SetPath failed (0x%08x)\n", r);
194
195     strcpy(buffer,"garbage");
196     r = IShellLinkA_GetPath(sl, buffer, sizeof(buffer), NULL, SLGP_RAWPATH);
197     ok(r == S_OK, "GetPath failed (0x%08x)\n", r);
198     ok(lstrcmpi(buffer,str)==0, "GetPath returned '%s'\n", buffer);
199
200     /* Get some real path to play with */
201     GetWindowsDirectoryA( mypath, sizeof(mypath)-12 );
202     strcat(mypath, "\\regedit.exe");
203
204     /* Test the interaction of SetPath and SetIDList */
205     tmp_pidl=NULL;
206     r = IShellLinkA_GetIDList(sl, &tmp_pidl);
207     ok(r == S_OK, "GetIDList failed (0x%08x)\n", r);
208     if (r == S_OK)
209     {
210         BOOL ret;
211
212         strcpy(buffer,"garbage");
213         ret = SHGetPathFromIDListA(tmp_pidl, buffer);
214         ok(ret, "SHGetPathFromIDListA failed\n");
215         if (ret)
216             ok(lstrcmpi(buffer,str)==0, "GetIDList returned '%s'\n", buffer);
217         pILFree(tmp_pidl);
218     }
219
220     pidl=path_to_pidl(mypath);
221     ok(pidl!=NULL, "path_to_pidl returned a NULL pidl\n");
222
223     if (pidl)
224     {
225         LPITEMIDLIST second_pidl;
226
227         r = IShellLinkA_SetIDList(sl, pidl);
228         ok(r == S_OK, "SetIDList failed (0x%08x)\n", r);
229
230         tmp_pidl=NULL;
231         r = IShellLinkA_GetIDList(sl, &tmp_pidl);
232         ok(r == S_OK, "GetIDList failed (0x%08x)\n", r);
233         ok(tmp_pidl && pILIsEqual(pidl, tmp_pidl),
234            "GetIDList returned an incorrect pidl\n");
235
236         r = IShellLinkA_GetIDList(sl, &second_pidl);
237         ok(r == S_OK, "GetIDList failed (0x%08x)\n", r);
238         ok(second_pidl && pILIsEqual(pidl, second_pidl),
239            "GetIDList returned an incorrect pidl\n");
240         ok(second_pidl != tmp_pidl, "pidls are the same\n");
241
242         pILFree(second_pidl);
243         pILFree(tmp_pidl);
244         pILFree(pidl);
245
246         strcpy(buffer,"garbage");
247         r = IShellLinkA_GetPath(sl, buffer, sizeof(buffer), NULL, SLGP_RAWPATH);
248         ok(r == S_OK, "GetPath failed (0x%08x)\n", r);
249         todo_wine
250         ok(lstrcmpi(buffer, mypath)==0, "GetPath returned '%s'\n", buffer);
251
252     }
253
254     /* test path with quotes (IShellLinkA_SetPath returns S_FALSE on W2K and below and S_OK on XP and above */
255     r = IShellLinkA_SetPath(sl, "\"c:\\nonexistent\\file\"");
256     ok(r==S_FALSE || r == S_OK, "SetPath failed (0x%08x)\n", r);
257
258     strcpy(buffer,"garbage");
259     r = IShellLinkA_GetPath(sl, buffer, sizeof(buffer), NULL, SLGP_RAWPATH);
260     ok(r==S_OK, "GetPath failed (0x%08x)\n", r);
261     ok(!lstrcmp(buffer, "C:\\nonexistent\\file") ||
262        broken(!lstrcmp(buffer, "C:\\\"c:\\nonexistent\\file\"")), /* NT4 */
263        "case doesn't match\n");
264
265     r = IShellLinkA_SetPath(sl, "\"c:\\foo");
266     ok(r==S_FALSE || r == S_OK || r == E_INVALIDARG /* Vista */, "SetPath failed (0x%08x)\n", r);
267
268     r = IShellLinkA_SetPath(sl, "\"\"c:\\foo");
269     ok(r==S_FALSE || r == S_OK || r == E_INVALIDARG /* Vista */, "SetPath failed (0x%08x)\n", r);
270
271     r = IShellLinkA_SetPath(sl, "c:\\foo\"");
272     ok(r==S_FALSE || r == S_OK || r == E_INVALIDARG /* Vista */, "SetPath failed (0x%08x)\n", r);
273
274     r = IShellLinkA_SetPath(sl, "\"\"c:\\foo\"");
275     ok(r==S_FALSE || r == S_OK || r == E_INVALIDARG /* Vista */, "SetPath failed (0x%08x)\n", r);
276
277     r = IShellLinkA_SetPath(sl, "\"\"c:\\foo\"\"");
278     ok(r==S_FALSE || r == S_OK || r == E_INVALIDARG /* Vista */, "SetPath failed (0x%08x)\n", r);
279
280     /* Test Getting / Setting the arguments */
281     strcpy(buffer,"garbage");
282     r = IShellLinkA_GetArguments(sl, buffer, sizeof(buffer));
283     ok(r == S_OK, "GetArguments failed (0x%08x)\n", r);
284     ok(*buffer=='\0', "GetArguments returned '%s'\n", buffer);
285
286     str="param1 \"spaced param2\"";
287     r = IShellLinkA_SetArguments(sl, str);
288     ok(r == S_OK, "SetArguments failed (0x%08x)\n", r);
289
290     strcpy(buffer,"garbage");
291     r = IShellLinkA_GetArguments(sl, buffer, sizeof(buffer));
292     ok(r == S_OK, "GetArguments failed (0x%08x)\n", r);
293     ok(lstrcmp(buffer,str)==0, "GetArguments returned '%s'\n", buffer);
294
295     strcpy(buffer,"garbage");
296     r = IShellLinkA_SetArguments(sl, NULL);
297     ok(r == S_OK, "SetArguments failed (0x%08x)\n", r);
298     r = IShellLinkA_GetArguments(sl, buffer, sizeof(buffer));
299     ok(r == S_OK, "GetArguments failed (0x%08x)\n", r);
300     ok(!buffer[0] || lstrcmp(buffer,str)==0, "GetArguments returned '%s'\n", buffer);
301
302     strcpy(buffer,"garbage");
303     r = IShellLinkA_SetArguments(sl, "");
304     ok(r == S_OK, "SetArguments failed (0x%08x)\n", r);
305     r = IShellLinkA_GetArguments(sl, buffer, sizeof(buffer));
306     ok(r == S_OK, "GetArguments failed (0x%08x)\n", r);
307     ok(!buffer[0], "GetArguments returned '%s'\n", buffer);
308
309     /* Test Getting / Setting showcmd */
310     i=0xdeadbeef;
311     r = IShellLinkA_GetShowCmd(sl, &i);
312     ok(r == S_OK, "GetShowCmd failed (0x%08x)\n", r);
313     ok(i==SW_SHOWNORMAL, "GetShowCmd returned %d\n", i);
314
315     r = IShellLinkA_SetShowCmd(sl, SW_SHOWMAXIMIZED);
316     ok(r == S_OK, "SetShowCmd failed (0x%08x)\n", r);
317
318     i=0xdeadbeef;
319     r = IShellLinkA_GetShowCmd(sl, &i);
320     ok(r == S_OK, "GetShowCmd failed (0x%08x)\n", r);
321     ok(i==SW_SHOWMAXIMIZED, "GetShowCmd returned %d'\n", i);
322
323     /* Test Getting / Setting the icon */
324     i=0xdeadbeef;
325     strcpy(buffer,"garbage");
326     r = IShellLinkA_GetIconLocation(sl, buffer, sizeof(buffer), &i);
327     ok(r == S_OK, "GetIconLocation failed (0x%08x)\n", r);
328     ok(*buffer=='\0', "GetIconLocation returned '%s'\n", buffer);
329     ok(i==0, "GetIconLocation returned %d\n", i);
330
331     str="c:\\nonexistent\\file";
332     r = IShellLinkA_SetIconLocation(sl, str, 0xbabecafe);
333     ok(r == S_OK, "SetIconLocation failed (0x%08x)\n", r);
334
335     i=0xdeadbeef;
336     r = IShellLinkA_GetIconLocation(sl, buffer, sizeof(buffer), &i);
337     ok(r == S_OK, "GetIconLocation failed (0x%08x)\n", r);
338     ok(lstrcmpi(buffer,str)==0, "GetIconLocation returned '%s'\n", buffer);
339     ok(i==0xbabecafe, "GetIconLocation returned %d'\n", i);
340
341     /* Test Getting / Setting the hot key */
342     w=0xbeef;
343     r = IShellLinkA_GetHotkey(sl, &w);
344     ok(r == S_OK, "GetHotkey failed (0x%08x)\n", r);
345     ok(w==0, "GetHotkey returned %d\n", w);
346
347     r = IShellLinkA_SetHotkey(sl, 0x5678);
348     ok(r == S_OK, "SetHotkey failed (0x%08x)\n", r);
349
350     w=0xbeef;
351     r = IShellLinkA_GetHotkey(sl, &w);
352     ok(r == S_OK, "GetHotkey failed (0x%08x)\n", r);
353     ok(w==0x5678, "GetHotkey returned %d'\n", w);
354
355     IShellLinkA_Release(sl);
356 }
357
358
359 /*
360  * Test saving and loading .lnk files
361  */
362
363 #define lok                   ok_(__FILE__, line)
364 #define lok_todo_4(todo_flag,a,b,c,d) \
365     if ((todo & todo_flag) == 0) lok((a), (b), (c), (d)); \
366     else todo_wine lok((a), (b), (c), (d));
367 #define lok_todo_2(todo_flag,a,b) \
368     if ((todo & todo_flag) == 0) lok((a), (b)); \
369     else todo_wine lok((a), (b));
370 #define check_lnk(a,b,c)        check_lnk_(__LINE__, (a), (b), (c))
371
372 void create_lnk_(int line, const WCHAR* path, lnk_desc_t* desc, int save_fails)
373 {
374     HRESULT r;
375     IShellLinkA *sl;
376     IPersistFile *pf;
377
378     r = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
379                          &IID_IShellLinkA, (LPVOID*)&sl);
380     lok(r == S_OK, "no IID_IShellLinkA (0x%08x)\n", r);
381     if (r != S_OK)
382         return;
383
384     if (desc->description)
385     {
386         r = IShellLinkA_SetDescription(sl, desc->description);
387         lok(r == S_OK, "SetDescription failed (0x%08x)\n", r);
388     }
389     if (desc->workdir)
390     {
391         r = IShellLinkA_SetWorkingDirectory(sl, desc->workdir);
392         lok(r == S_OK, "SetWorkingDirectory failed (0x%08x)\n", r);
393     }
394     if (desc->path)
395     {
396         r = IShellLinkA_SetPath(sl, desc->path);
397         lok(SUCCEEDED(r), "SetPath failed (0x%08x)\n", r);
398     }
399     if (desc->pidl)
400     {
401         r = IShellLinkA_SetIDList(sl, desc->pidl);
402         lok(r == S_OK, "SetIDList failed (0x%08x)\n", r);
403     }
404     if (desc->arguments)
405     {
406         r = IShellLinkA_SetArguments(sl, desc->arguments);
407         lok(r == S_OK, "SetArguments failed (0x%08x)\n", r);
408     }
409     if (desc->showcmd)
410     {
411         r = IShellLinkA_SetShowCmd(sl, desc->showcmd);
412         lok(r == S_OK, "SetShowCmd failed (0x%08x)\n", r);
413     }
414     if (desc->icon)
415     {
416         r = IShellLinkA_SetIconLocation(sl, desc->icon, desc->icon_id);
417         lok(r == S_OK, "SetIconLocation failed (0x%08x)\n", r);
418     }
419     if (desc->hotkey)
420     {
421         r = IShellLinkA_SetHotkey(sl, desc->hotkey);
422         lok(r == S_OK, "SetHotkey failed (0x%08x)\n", r);
423     }
424
425     r = IShellLinkW_QueryInterface(sl, &IID_IPersistFile, (LPVOID*)&pf);
426     lok(r == S_OK, "no IID_IPersistFile (0x%08x)\n", r);
427     if (r == S_OK)
428     {
429         LPOLESTR str;
430
431     if (0)
432     {
433         /* crashes on XP */
434         IPersistFile_GetCurFile(pf, NULL);
435     }
436
437         /* test GetCurFile before ::Save */
438         str = (LPWSTR)0xdeadbeef;
439         r = IPersistFile_GetCurFile(pf, &str);
440         lok(r == S_FALSE ||
441             broken(r == S_OK), /* shell32 < 5.0 */
442             "got 0x%08x\n", r);
443         lok(str == NULL, "got %p\n", str);
444
445         r = IPersistFile_Save(pf, path, TRUE);
446         if (save_fails)
447         {
448             todo_wine {
449             lok(r == S_OK, "save failed (0x%08x)\n", r);
450             }
451         }
452         else
453         {
454             lok(r == S_OK, "save failed (0x%08x)\n", r);
455         }
456
457         /* test GetCurFile after ::Save */
458         r = IPersistFile_GetCurFile(pf, &str);
459         lok(r == S_OK, "got 0x%08x\n", r);
460         lok(str != NULL ||
461             broken(str == NULL), /* shell32 < 5.0 */
462             "Didn't expect NULL\n");
463         if (str != NULL)
464         {
465             IMalloc *pmalloc;
466
467             lok(!winetest_strcmpW(path, str), "Expected %s, got %s\n",
468                 wine_dbgstr_w(path), wine_dbgstr_w(str));
469
470             SHGetMalloc(&pmalloc);
471             IMalloc_Free(pmalloc, str);
472         }
473         else
474             win_skip("GetCurFile fails on shell32 < 5.0\n");
475
476         IPersistFile_Release(pf);
477     }
478
479     IShellLinkA_Release(sl);
480 }
481
482 static void check_lnk_(int line, const WCHAR* path, lnk_desc_t* desc, int todo)
483 {
484     HRESULT r;
485     IShellLinkA *sl;
486     IPersistFile *pf;
487     char buffer[INFOTIPSIZE];
488     LPOLESTR str;
489
490     r = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
491                          &IID_IShellLinkA, (LPVOID*)&sl);
492     lok(r == S_OK, "no IID_IShellLinkA (0x%08x)\n", r);
493     if (r != S_OK)
494         return;
495
496     r = IShellLinkA_QueryInterface(sl, &IID_IPersistFile, (LPVOID*)&pf);
497     lok(r == S_OK, "no IID_IPersistFile (0x%08x)\n", r);
498     if (r != S_OK)
499     {
500         IShellLinkA_Release(sl);
501         return;
502     }
503
504     /* test GetCurFile before ::Load */
505     str = (LPWSTR)0xdeadbeef;
506     r = IPersistFile_GetCurFile(pf, &str);
507     lok(r == S_FALSE ||
508         broken(r == S_OK), /* shell32 < 5.0 */
509         "got 0x%08x\n", r);
510     lok(str == NULL, "got %p\n", str);
511
512     r = IPersistFile_Load(pf, path, STGM_READ);
513     lok(r == S_OK, "load failed (0x%08x)\n", r);
514
515     /* test GetCurFile after ::Save */
516     r = IPersistFile_GetCurFile(pf, &str);
517     lok(r == S_OK, "got 0x%08x\n", r);
518     lok(str != NULL ||
519         broken(str == NULL), /* shell32 < 5.0 */
520         "Didn't expect NULL\n");
521     if (str != NULL)
522     {
523         IMalloc *pmalloc;
524
525         lok(!winetest_strcmpW(path, str), "Expected %s, got %s\n",
526             wine_dbgstr_w(path), wine_dbgstr_w(str));
527
528         SHGetMalloc(&pmalloc);
529         IMalloc_Free(pmalloc, str);
530     }
531     else
532         win_skip("GetCurFile fails on shell32 < 5.0\n");
533
534     IPersistFile_Release(pf);
535     if (r != S_OK)
536     {
537         IShellLinkA_Release(sl);
538         return;
539     }
540
541     if (desc->description)
542     {
543         strcpy(buffer,"garbage");
544         r = IShellLinkA_GetDescription(sl, buffer, sizeof(buffer));
545         lok(r == S_OK, "GetDescription failed (0x%08x)\n", r);
546         lok_todo_4(0x1, lstrcmp(buffer, desc->description)==0,
547            "GetDescription returned '%s' instead of '%s'\n",
548            buffer, desc->description);
549     }
550     if (desc->workdir)
551     {
552         strcpy(buffer,"garbage");
553         r = IShellLinkA_GetWorkingDirectory(sl, buffer, sizeof(buffer));
554         lok(r == S_OK, "GetWorkingDirectory failed (0x%08x)\n", r);
555         lok_todo_4(0x2, lstrcmpi(buffer, desc->workdir)==0,
556            "GetWorkingDirectory returned '%s' instead of '%s'\n",
557            buffer, desc->workdir);
558     }
559     if (desc->path)
560     {
561         strcpy(buffer,"garbage");
562         r = IShellLinkA_GetPath(sl, buffer, sizeof(buffer), NULL, SLGP_RAWPATH);
563         lok(SUCCEEDED(r), "GetPath failed (0x%08x)\n", r);
564         lok_todo_4(0x4, lstrcmpi(buffer, desc->path)==0,
565            "GetPath returned '%s' instead of '%s'\n",
566            buffer, desc->path);
567     }
568     if (desc->pidl)
569     {
570         LPITEMIDLIST pidl=NULL;
571         r = IShellLinkA_GetIDList(sl, &pidl);
572         lok(r == S_OK, "GetIDList failed (0x%08x)\n", r);
573         lok_todo_2(0x8, pILIsEqual(pidl, desc->pidl),
574            "GetIDList returned an incorrect pidl\n");
575     }
576     if (desc->showcmd)
577     {
578         int i=0xdeadbeef;
579         r = IShellLinkA_GetShowCmd(sl, &i);
580         lok(r == S_OK, "GetShowCmd failed (0x%08x)\n", r);
581         lok_todo_4(0x10, i==desc->showcmd,
582            "GetShowCmd returned 0x%0x instead of 0x%0x\n",
583            i, desc->showcmd);
584     }
585     if (desc->icon)
586     {
587         int i=0xdeadbeef;
588         strcpy(buffer,"garbage");
589         r = IShellLinkA_GetIconLocation(sl, buffer, sizeof(buffer), &i);
590         lok(r == S_OK, "GetIconLocation failed (0x%08x)\n", r);
591         lok_todo_4(0x20, lstrcmpi(buffer, desc->icon)==0,
592            "GetIconLocation returned '%s' instead of '%s'\n",
593            buffer, desc->icon);
594         lok_todo_4(0x20, i==desc->icon_id,
595            "GetIconLocation returned 0x%0x instead of 0x%0x\n",
596            i, desc->icon_id);
597     }
598     if (desc->hotkey)
599     {
600         WORD i=0xbeef;
601         r = IShellLinkA_GetHotkey(sl, &i);
602         lok(r == S_OK, "GetHotkey failed (0x%08x)\n", r);
603         lok_todo_4(0x40, i==desc->hotkey,
604            "GetHotkey returned 0x%04x instead of 0x%04x\n",
605            i, desc->hotkey);
606     }
607
608     IShellLinkA_Release(sl);
609 }
610
611 static void test_load_save(void)
612 {
613     WCHAR lnkfile[MAX_PATH];
614     char lnkfileA[MAX_PATH];
615     static const char lnkfileA_name[] = "\\test.lnk";
616
617     lnk_desc_t desc;
618     char mypath[MAX_PATH];
619     char mydir[MAX_PATH];
620     char realpath[MAX_PATH];
621     char* p;
622     HANDLE hf;
623     DWORD r;
624
625     if (!pGetLongPathNameA)
626     {
627         win_skip("GetLongPathNameA is not available\n");
628         return;
629     }
630
631     /* Don't used a fixed path for the test.lnk file */
632     GetTempPathA(MAX_PATH, lnkfileA);
633     lstrcatA(lnkfileA, lnkfileA_name);
634     MultiByteToWideChar(CP_ACP, 0, lnkfileA, -1, lnkfile, MAX_PATH);
635
636     /* Save an empty .lnk file */
637     memset(&desc, 0, sizeof(desc));
638     create_lnk(lnkfile, &desc, 0);
639
640     /* It should come back as a bunch of empty strings */
641     desc.description="";
642     desc.workdir="";
643     desc.path="";
644     desc.arguments="";
645     desc.icon="";
646     check_lnk(lnkfile, &desc, 0x0);
647
648     /* Point a .lnk file to nonexistent files */
649     desc.description="";
650     desc.workdir="c:\\Nonexitent\\work\\directory";
651     desc.path="c:\\nonexistent\\path";
652     desc.pidl=NULL;
653     desc.arguments="";
654     desc.showcmd=0;
655     desc.icon="c:\\nonexistent\\icon\\file";
656     desc.icon_id=1234;
657     desc.hotkey=0;
658     create_lnk(lnkfile, &desc, 0);
659     check_lnk(lnkfile, &desc, 0x0);
660
661     r=GetModuleFileName(NULL, mypath, sizeof(mypath));
662     ok(r<sizeof(mypath), "GetModuleFileName failed (%d)\n", r);
663     strcpy(mydir, mypath);
664     p=strrchr(mydir, '\\');
665     if (p)
666         *p='\0';
667
668     /* IShellLink returns path in long form */
669     if (!pGetLongPathNameA(mypath, realpath, MAX_PATH)) strcpy( realpath, mypath );
670
671     /* Overwrite the existing lnk file and point it to existing files */
672     desc.description="test 2";
673     desc.workdir=mydir;
674     desc.path=realpath;
675     desc.pidl=NULL;
676     desc.arguments="/option1 /option2 \"Some string\"";
677     desc.showcmd=SW_SHOWNORMAL;
678     desc.icon=mypath;
679     desc.icon_id=0;
680     desc.hotkey=0x1234;
681     create_lnk(lnkfile, &desc, 0);
682     check_lnk(lnkfile, &desc, 0x0);
683
684     /* Test omitting .exe from an absolute path */
685     p=strrchr(realpath, '.');
686     if (p)
687         *p='\0';
688
689     desc.description="absolute path without .exe";
690     desc.workdir=mydir;
691     desc.path=realpath;
692     desc.pidl=NULL;
693     desc.arguments="/option1 /option2 \"Some string\"";
694     desc.showcmd=SW_SHOWNORMAL;
695     desc.icon=mypath;
696     desc.icon_id=0;
697     desc.hotkey=0x1234;
698     create_lnk(lnkfile, &desc, 0);
699     strcat(realpath, ".exe");
700     check_lnk(lnkfile, &desc, 0x4);
701
702     /* Overwrite the existing lnk file and test link to a command on the path */
703     desc.description="command on path";
704     desc.workdir=mypath;
705     desc.path="rundll32.exe";
706     desc.pidl=NULL;
707     desc.arguments="/option1 /option2 \"Some string\"";
708     desc.showcmd=SW_SHOWNORMAL;
709     desc.icon=mypath;
710     desc.icon_id=0;
711     desc.hotkey=0x1234;
712     create_lnk(lnkfile, &desc, 0);
713     /* Check that link is created to proper location */
714     SearchPathA( NULL, desc.path, NULL, MAX_PATH, realpath, NULL);
715     desc.path=realpath;
716     check_lnk(lnkfile, &desc, 0x0);
717
718     /* Test omitting .exe from a command on the path */
719     desc.description="command on path without .exe";
720     desc.workdir=mypath;
721     desc.path="rundll32";
722     desc.pidl=NULL;
723     desc.arguments="/option1 /option2 \"Some string\"";
724     desc.showcmd=SW_SHOWNORMAL;
725     desc.icon=mypath;
726     desc.icon_id=0;
727     desc.hotkey=0x1234;
728     create_lnk(lnkfile, &desc, 0);
729     /* Check that link is created to proper location */
730     SearchPathA( NULL, "rundll32", NULL, MAX_PATH, realpath, NULL);
731     desc.path=realpath;
732     check_lnk(lnkfile, &desc, 0x4);
733
734     /* Create a temporary non-executable file */
735     r=GetTempPath(sizeof(mypath), mypath);
736     ok(r<sizeof(mypath), "GetTempPath failed (%d), err %d\n", r, GetLastError());
737     r=pGetLongPathNameA(mypath, mydir, sizeof(mydir));
738     ok(r<sizeof(mydir), "GetLongPathName failed (%d), err %d\n", r, GetLastError());
739     p=strrchr(mydir, '\\');
740     if (p)
741         *p='\0';
742
743     strcpy(mypath, mydir);
744     strcat(mypath, "\\test.txt");
745     hf = CreateFile(mypath, GENERIC_WRITE, 0, NULL,
746                     CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
747     CloseHandle(hf);
748
749     /* Overwrite the existing lnk file and test link to an existing non-executable file */
750     desc.description="non-executable file";
751     desc.workdir=mydir;
752     desc.path=mypath;
753     desc.pidl=NULL;
754     desc.arguments="";
755     desc.showcmd=SW_SHOWNORMAL;
756     desc.icon=mypath;
757     desc.icon_id=0;
758     desc.hotkey=0x1234;
759     create_lnk(lnkfile, &desc, 0);
760     check_lnk(lnkfile, &desc, 0x0);
761
762     r=pGetShortPathNameA(mydir, mypath, sizeof(mypath));
763     ok(r<sizeof(mypath), "GetShortPathName failed (%d), err %d\n", r, GetLastError());
764
765     strcpy(realpath, mypath);
766     strcat(realpath, "\\test.txt");
767     strcat(mypath, "\\\\test.txt");
768
769     /* Overwrite the existing lnk file and test link to a short path with double backslashes */
770     desc.description="non-executable file";
771     desc.workdir=mydir;
772     desc.path=mypath;
773     desc.pidl=NULL;
774     desc.arguments="";
775     desc.showcmd=SW_SHOWNORMAL;
776     desc.icon=mypath;
777     desc.icon_id=0;
778     desc.hotkey=0x1234;
779     create_lnk(lnkfile, &desc, 0);
780     desc.path=realpath;
781     check_lnk(lnkfile, &desc, 0x0);
782
783     r = DeleteFileA(mypath);
784     ok(r, "failed to delete file %s (%d)\n", mypath, GetLastError());
785
786     /* Create a temporary .bat file */
787     strcpy(mypath, mydir);
788     strcat(mypath, "\\test.bat");
789     hf = CreateFile(mypath, GENERIC_WRITE, 0, NULL,
790                     CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
791     CloseHandle(hf);
792
793     strcpy(realpath, mypath);
794
795     p=strrchr(mypath, '.');
796     if (p)
797         *p='\0';
798
799     /* Try linking to the .bat file without the extension */
800     desc.description="batch file";
801     desc.workdir=mydir;
802     desc.path=mypath;
803     desc.pidl=NULL;
804     desc.arguments="";
805     desc.showcmd=SW_SHOWNORMAL;
806     desc.icon=mypath;
807     desc.icon_id=0;
808     desc.hotkey=0x1234;
809     create_lnk(lnkfile, &desc, 0);
810     desc.path = realpath;
811     check_lnk(lnkfile, &desc, 0x4);
812
813     r = DeleteFileA(realpath);
814     ok(r, "failed to delete file %s (%d)\n", realpath, GetLastError());
815
816     /* FIXME: Also test saving a .lnk pointing to a pidl that cannot be
817      * represented as a path.
818      */
819
820     /* DeleteFileW is not implemented on Win9x */
821     r=DeleteFileA(lnkfileA);
822     ok(r, "failed to delete link '%s' (%d)\n", lnkfileA, GetLastError());
823 }
824
825 static void test_datalink(void)
826 {
827     static const WCHAR lnk[] = {
828       ':',':','{','9','d','b','1','1','8','6','e','-','4','0','d','f','-','1',
829       '1','d','1','-','a','a','8','c','-','0','0','c','0','4','f','b','6','7',
830       '8','6','3','}',':','2','6',',','!','!','g','x','s','f','(','N','g',']',
831       'q','F','`','H','{','L','s','A','C','C','E','S','S','F','i','l','e','s',
832       '>','p','l','T',']','j','I','{','j','f','(','=','1','&','L','[','-','8',
833       '1','-',']',':',':',0 };
834     static const WCHAR comp[] = {
835       '2','6',',','!','!','g','x','s','f','(','N','g',']','q','F','`','H','{',
836       'L','s','A','C','C','E','S','S','F','i','l','e','s','>','p','l','T',']',
837       'j','I','{','j','f','(','=','1','&','L','[','-','8','1','-',']',0 };
838     IShellLinkDataList *dl = NULL;
839     IShellLinkW *sl = NULL;
840     HRESULT r;
841     DWORD flags = 0;
842     EXP_DARWIN_LINK *dar;
843
844     r = CoCreateInstance( &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
845                             &IID_IShellLinkW, (LPVOID*)&sl );
846     ok( r == S_OK ||
847         broken(r == E_NOINTERFACE), /* Win9x */
848         "CoCreateInstance failed (0x%08x)\n", r);
849     if (!sl)
850     {
851         win_skip("no shelllink\n");
852         return;
853     }
854
855     r = IShellLinkW_QueryInterface( sl, &_IID_IShellLinkDataList, (LPVOID*) &dl );
856     ok( r == S_OK ||
857         broken(r == E_NOINTERFACE), /* NT4 */
858         "IShellLinkW_QueryInterface failed (0x%08x)\n", r);
859
860     if (!dl)
861     {
862         win_skip("no datalink interface\n");
863         IShellLinkW_Release( sl );
864         return;
865     }
866
867     flags = 0;
868     r = IShellLinkDataList_GetFlags( dl, &flags );
869     ok( r == S_OK, "GetFlags failed\n");
870     ok( flags == 0, "GetFlags returned wrong flags\n");
871
872     dar = (void*)-1;
873     r = IShellLinkDataList_CopyDataBlock( dl, EXP_DARWIN_ID_SIG, (LPVOID*) &dar );
874     ok( r == E_FAIL, "CopyDataBlock failed\n");
875     ok( dar == NULL, "should be null\n");
876
877     if (!pGetLongPathNameA /* NT4 */)
878         skip("SetPath with NULL parameter crashes on NT4\n");
879     else
880     {
881         r = IShellLinkW_SetPath(sl, NULL);
882         ok(r == E_INVALIDARG, "SetPath returned wrong error (0x%08x)\n", r);
883     }
884
885     r = IShellLinkW_SetPath(sl, lnk);
886     ok(r == S_OK, "SetPath failed\n");
887
888 if (0)
889 {
890     /* the following crashes */
891     IShellLinkDataList_GetFlags( dl, NULL );
892 }
893
894     flags = 0;
895     r = IShellLinkDataList_GetFlags( dl, &flags );
896     ok( r == S_OK, "GetFlags failed\n");
897     /* SLDF_HAS_LOGO3ID is no longer supported on Vista+, filter it out */
898     ok( (flags & (~ SLDF_HAS_LOGO3ID)) == SLDF_HAS_DARWINID,
899         "GetFlags returned wrong flags\n");
900
901     dar = NULL;
902     r = IShellLinkDataList_CopyDataBlock( dl, EXP_DARWIN_ID_SIG, (LPVOID*) &dar );
903     ok( r == S_OK, "CopyDataBlock failed\n");
904
905     ok( dar && ((DATABLOCK_HEADER*)dar)->dwSignature == EXP_DARWIN_ID_SIG, "signature wrong\n");
906     ok( dar && 0==lstrcmpW(dar->szwDarwinID, comp ), "signature wrong\n");
907
908     LocalFree( dar );
909
910     IUnknown_Release( dl );
911     IShellLinkW_Release( sl );
912 }
913
914 static void test_shdefextracticon(void)
915 {
916     HICON hiconlarge=NULL, hiconsmall=NULL;
917     HRESULT res;
918
919     if (!pSHDefExtractIconA)
920     {
921         win_skip("SHDefExtractIconA is unavailable\n");
922         return;
923     }
924
925     res = pSHDefExtractIconA("shell32.dll", 0, 0, &hiconlarge, &hiconsmall, MAKELONG(16,24));
926     ok(SUCCEEDED(res), "SHDefExtractIconA failed, res=%x\n", res);
927     ok(hiconlarge != NULL, "got null hiconlarge\n");
928     ok(hiconsmall != NULL, "got null hiconsmall\n");
929     DestroyIcon(hiconlarge);
930     DestroyIcon(hiconsmall);
931
932     hiconsmall = NULL;
933     res = pSHDefExtractIconA("shell32.dll", 0, 0, NULL, &hiconsmall, MAKELONG(16,24));
934     ok(SUCCEEDED(res), "SHDefExtractIconA failed, res=%x\n", res);
935     ok(hiconsmall != NULL, "got null hiconsmall\n");
936     DestroyIcon(hiconsmall);
937
938     res = pSHDefExtractIconA("shell32.dll", 0, 0, NULL, NULL, MAKELONG(16,24));
939     ok(SUCCEEDED(res), "SHDefExtractIconA failed, res=%x\n", res);
940 }
941
942 static void test_GetIconLocation(void)
943 {
944     IShellLinkA *sl;
945     const char *str;
946     char buffer[INFOTIPSIZE], mypath[MAX_PATH];
947     int i;
948     HRESULT r;
949     LPITEMIDLIST pidl;
950
951     r = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
952             &IID_IShellLinkA, (LPVOID*)&sl);
953     ok(r == S_OK, "no IID_IShellLinkA (0x%08x)\n", r);
954     if(r != S_OK)
955         return;
956
957     i = 0xdeadbeef;
958     strcpy(buffer, "garbage");
959     r = IShellLinkA_GetIconLocation(sl, buffer, sizeof(buffer), &i);
960     ok(r == S_OK, "GetIconLocation failed (0x%08x)\n", r);
961     ok(*buffer == '\0', "GetIconLocation returned '%s'\n", buffer);
962     ok(i == 0, "GetIconLocation returned %d\n", i);
963
964     str = "c:\\some\\path";
965     r = IShellLinkA_SetPath(sl, str);
966     ok(r == S_FALSE || r == S_OK, "SetPath failed (0x%08x)\n", r);
967
968     i = 0xdeadbeef;
969     strcpy(buffer, "garbage");
970     r = IShellLinkA_GetIconLocation(sl, buffer, sizeof(buffer), &i);
971     ok(r == S_OK, "GetIconLocation failed (0x%08x)\n", r);
972     ok(*buffer == '\0', "GetIconLocation returned '%s'\n", buffer);
973     ok(i == 0, "GetIconLocation returned %d\n", i);
974
975     GetWindowsDirectoryA(mypath, sizeof(mypath) - 12);
976     strcat(mypath, "\\regedit.exe");
977     pidl = path_to_pidl(mypath);
978     r = IShellLinkA_SetIDList(sl, pidl);
979     ok(r == S_OK, "SetPath failed (0x%08x)\n", r);
980     pILFree(pidl);
981
982     i = 0xdeadbeef;
983     strcpy(buffer, "garbage");
984     r = IShellLinkA_GetIconLocation(sl, buffer, sizeof(buffer), &i);
985     ok(r == S_OK, "GetIconLocation failed (0x%08x)\n", r);
986     ok(*buffer == '\0', "GetIconLocation returned '%s'\n", buffer);
987     ok(i == 0, "GetIconLocation returned %d\n", i);
988
989     str = "c:\\nonexistent\\file";
990     r = IShellLinkA_SetIconLocation(sl, str, 0xbabecafe);
991     ok(r == S_OK, "SetIconLocation failed (0x%08x)\n", r);
992
993     i = 0xdeadbeef;
994     r = IShellLinkA_GetIconLocation(sl, buffer, sizeof(buffer), &i);
995     ok(r == S_OK, "GetIconLocation failed (0x%08x)\n", r);
996     ok(lstrcmpi(buffer,str) == 0, "GetIconLocation returned '%s'\n", buffer);
997     ok(i == 0xbabecafe, "GetIconLocation returned %d'\n", i);
998
999     IShellLinkA_Release(sl);
1000 }
1001
1002 START_TEST(shelllink)
1003 {
1004     HRESULT r;
1005     HMODULE hmod = GetModuleHandleA("shell32.dll");
1006     HMODULE hkernel32 = GetModuleHandleA("kernel32.dll");
1007
1008     pILFree = (fnILFree) GetProcAddress(hmod, (LPSTR)155);
1009     pILIsEqual = (fnILIsEqual) GetProcAddress(hmod, (LPSTR)21);
1010     pSHILCreateFromPath = (fnSHILCreateFromPath) GetProcAddress(hmod, (LPSTR)28);
1011     pSHDefExtractIconA = (fnSHDefExtractIconA) GetProcAddress(hmod, "SHDefExtractIconA");
1012
1013     pGetLongPathNameA = (void *)GetProcAddress(hkernel32, "GetLongPathNameA");
1014     pGetShortPathNameA = (void *)GetProcAddress(hkernel32, "GetShortPathNameA");
1015
1016     r = CoInitialize(NULL);
1017     ok(r == S_OK, "CoInitialize failed (0x%08x)\n", r);
1018     if (r != S_OK)
1019         return;
1020
1021     test_get_set();
1022     test_load_save();
1023     test_datalink();
1024     test_shdefextracticon();
1025     test_GetIconLocation();
1026
1027     CoUninitialize();
1028 }