shell32/tests: Fix a test failure on NT4.
[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
44 static fnILFree pILFree;
45 static fnILIsEqual pILIsEqual;
46 static fnSHILCreateFromPath pSHILCreateFromPath;
47
48 static DWORD (WINAPI *pGetLongPathNameA)(LPCSTR, LPSTR, DWORD);
49
50 static const GUID _IID_IShellLinkDataList = {
51     0x45e2b4ae, 0xb1c3, 0x11d0,
52     { 0xb9, 0x2f, 0x00, 0xa0, 0xc9, 0x03, 0x12, 0xe1 }
53 };
54
55 static const WCHAR notafile[]= { 'C',':','\\','n','o','n','e','x','i','s','t','e','n','t','\\','f','i','l','e',0 };
56
57
58 /* For some reason SHILCreateFromPath does not work on Win98 and
59  * SHSimpleIDListFromPathA does not work on NT4. But if we call both we
60  * get what we want on all platforms.
61  */
62 static LPITEMIDLIST (WINAPI *pSHSimpleIDListFromPathAW)(LPCVOID);
63
64 static LPITEMIDLIST path_to_pidl(const char* path)
65 {
66     LPITEMIDLIST pidl;
67
68     if (!pSHSimpleIDListFromPathAW)
69     {
70         HMODULE hdll=GetModuleHandleA("shell32.dll");
71         pSHSimpleIDListFromPathAW=(void*)GetProcAddress(hdll, (char*)162);
72         if (!pSHSimpleIDListFromPathAW)
73             win_skip("SHSimpleIDListFromPathAW not found in shell32.dll\n");
74     }
75
76     pidl=NULL;
77     /* pSHSimpleIDListFromPathAW maps to A on non NT platforms */
78     if (pSHSimpleIDListFromPathAW && (GetVersion() & 0x80000000))
79         pidl=pSHSimpleIDListFromPathAW(path);
80
81     if (!pidl)
82     {
83         WCHAR* pathW;
84         HRESULT r;
85         int len;
86
87         len=MultiByteToWideChar(CP_ACP, 0, path, -1, NULL, 0);
88         pathW=HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
89         MultiByteToWideChar(CP_ACP, 0, path, -1, pathW, len);
90
91         r=pSHILCreateFromPath(pathW, &pidl, NULL);
92         ok(SUCCEEDED(r), "SHILCreateFromPath failed (0x%08x)\n", r);
93         HeapFree(GetProcessHeap(), 0, pathW);
94     }
95     return pidl;
96 }
97
98
99 /*
100  * Test manipulation of an IShellLink's properties.
101  */
102
103 static void test_get_set(void)
104 {
105     HRESULT r;
106     IShellLinkA *sl;
107     char mypath[MAX_PATH];
108     char buffer[INFOTIPSIZE];
109     LPITEMIDLIST pidl, tmp_pidl;
110     const char * str;
111     int i;
112     WORD w;
113
114     r = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
115                          &IID_IShellLinkA, (LPVOID*)&sl);
116     ok(SUCCEEDED(r), "no IID_IShellLinkA (0x%08x)\n", r);
117     if (FAILED(r))
118         return;
119
120     /* Test Getting / Setting the description */
121     strcpy(buffer,"garbage");
122     r = IShellLinkA_GetDescription(sl, buffer, sizeof(buffer));
123     ok(SUCCEEDED(r), "GetDescription failed (0x%08x)\n", r);
124     ok(*buffer=='\0', "GetDescription returned '%s'\n", buffer);
125
126     str="Some description";
127     r = IShellLinkA_SetDescription(sl, str);
128     ok(SUCCEEDED(r), "SetDescription failed (0x%08x)\n", r);
129
130     strcpy(buffer,"garbage");
131     r = IShellLinkA_GetDescription(sl, buffer, sizeof(buffer));
132     ok(SUCCEEDED(r), "GetDescription failed (0x%08x)\n", r);
133     ok(lstrcmp(buffer,str)==0, "GetDescription returned '%s'\n", buffer);
134
135     /* Test Getting / Setting the work directory */
136     strcpy(buffer,"garbage");
137     r = IShellLinkA_GetWorkingDirectory(sl, buffer, sizeof(buffer));
138     ok(SUCCEEDED(r), "GetWorkingDirectory failed (0x%08x)\n", r);
139     ok(*buffer=='\0', "GetWorkingDirectory returned '%s'\n", buffer);
140
141     str="c:\\nonexistent\\directory";
142     r = IShellLinkA_SetWorkingDirectory(sl, str);
143     ok(SUCCEEDED(r), "SetWorkingDirectory failed (0x%08x)\n", r);
144
145     strcpy(buffer,"garbage");
146     r = IShellLinkA_GetWorkingDirectory(sl, buffer, sizeof(buffer));
147     ok(SUCCEEDED(r), "GetWorkingDirectory failed (0x%08x)\n", r);
148     ok(lstrcmpi(buffer,str)==0, "GetWorkingDirectory returned '%s'\n", buffer);
149
150     /* Test Getting / Setting the path */
151     strcpy(buffer,"garbage");
152     r = IShellLinkA_GetPath(sl, buffer, sizeof(buffer), NULL, SLGP_RAWPATH);
153     ok(SUCCEEDED(r), "GetPath failed (0x%08x)\n", r);
154     ok(*buffer=='\0', "GetPath returned '%s'\n", buffer);
155
156     r = IShellLinkA_SetPath(sl, "");
157     ok(r==S_OK, "SetPath failed (0x%08x)\n", r);
158
159     strcpy(buffer,"garbage");
160     r = IShellLinkA_GetPath(sl, buffer, sizeof(buffer), NULL, SLGP_RAWPATH);
161     ok(SUCCEEDED(r), "GetPath failed (0x%08x)\n", r);
162     ok(*buffer=='\0', "GetPath returned '%s'\n", buffer);
163
164     /* Win98 returns S_FALSE, but WinXP returns S_OK */
165     str="c:\\nonexistent\\file";
166     r = IShellLinkA_SetPath(sl, str);
167     ok(r==S_FALSE || r==S_OK, "SetPath failed (0x%08x)\n", r);
168
169     strcpy(buffer,"garbage");
170     r = IShellLinkA_GetPath(sl, buffer, sizeof(buffer), NULL, SLGP_RAWPATH);
171     ok(SUCCEEDED(r), "GetPath failed (0x%08x)\n", r);
172     ok(lstrcmpi(buffer,str)==0, "GetPath returned '%s'\n", buffer);
173
174     /* Get some real path to play with */
175     GetWindowsDirectoryA( mypath, sizeof(mypath)-12 );
176     strcat(mypath, "\\regedit.exe");
177
178     /* Test the interaction of SetPath and SetIDList */
179     tmp_pidl=NULL;
180     r = IShellLinkA_GetIDList(sl, &tmp_pidl);
181     ok(SUCCEEDED(r), "GetIDList failed (0x%08x)\n", r);
182     if (SUCCEEDED(r))
183     {
184         BOOL ret;
185
186         strcpy(buffer,"garbage");
187         ret = SHGetPathFromIDListA(tmp_pidl, buffer);
188         todo_wine {
189         ok(ret, "SHGetPathFromIDListA failed\n");
190         }
191         if (ret)
192             ok(lstrcmpi(buffer,str)==0, "GetIDList returned '%s'\n", buffer);
193     }
194
195     pidl=path_to_pidl(mypath);
196     ok(pidl!=NULL, "path_to_pidl returned a NULL pidl\n");
197
198     if (pidl)
199     {
200         r = IShellLinkA_SetIDList(sl, pidl);
201         ok(SUCCEEDED(r), "SetIDList failed (0x%08x)\n", r);
202
203         tmp_pidl=NULL;
204         r = IShellLinkA_GetIDList(sl, &tmp_pidl);
205         ok(SUCCEEDED(r), "GetIDList failed (0x%08x)\n", r);
206         ok(tmp_pidl && pILIsEqual(pidl, tmp_pidl),
207            "GetIDList returned an incorrect pidl\n");
208
209         /* tmp_pidl is owned by IShellLink so we don't free it */
210         pILFree(pidl);
211
212         strcpy(buffer,"garbage");
213         r = IShellLinkA_GetPath(sl, buffer, sizeof(buffer), NULL, SLGP_RAWPATH);
214         ok(SUCCEEDED(r), "GetPath failed (0x%08x)\n", r);
215         todo_wine
216         ok(lstrcmpi(buffer, mypath)==0, "GetPath returned '%s'\n", buffer);
217
218     }
219
220     /* test path with quotes (IShellLinkA_SetPath returns S_FALSE on W2K and below and S_OK on XP and above */
221     r = IShellLinkA_SetPath(sl, "\"c:\\nonexistent\\file\"");
222     ok(r==S_FALSE || r == S_OK, "SetPath failed (0x%08x)\n", r);
223
224     r = IShellLinkA_GetPath(sl, buffer, sizeof(buffer), NULL, SLGP_RAWPATH);
225     ok(r==S_OK, "GetPath failed (0x%08x)\n", r);
226     ok(!lstrcmp(buffer, "C:\\nonexistent\\file") ||
227        broken(!lstrcmp(buffer, "C:\\\"c:\\nonexistent\\file\"")), /* NT4 */
228        "case doesn't match\n");
229
230     r = IShellLinkA_SetPath(sl, "\"c:\\foo");
231     ok(r==S_FALSE || r == S_OK || r == E_INVALIDARG /* Vista */, "SetPath failed (0x%08x)\n", r);
232
233     r = IShellLinkA_SetPath(sl, "\"\"c:\\foo");
234     ok(r==S_FALSE || r == S_OK || r == E_INVALIDARG /* Vista */, "SetPath failed (0x%08x)\n", r);
235
236     r = IShellLinkA_SetPath(sl, "c:\\foo\"");
237     ok(r==S_FALSE || r == S_OK || r == E_INVALIDARG /* Vista */, "SetPath failed (0x%08x)\n", r);
238
239     r = IShellLinkA_SetPath(sl, "\"\"c:\\foo\"");
240     ok(r==S_FALSE || r == S_OK || r == E_INVALIDARG /* Vista */, "SetPath failed (0x%08x)\n", r);
241
242     r = IShellLinkA_SetPath(sl, "\"\"c:\\foo\"\"");
243     ok(r==S_FALSE || r == S_OK || r == E_INVALIDARG /* Vista */, "SetPath failed (0x%08x)\n", r);
244
245     /* Test Getting / Setting the arguments */
246     strcpy(buffer,"garbage");
247     r = IShellLinkA_GetArguments(sl, buffer, sizeof(buffer));
248     ok(SUCCEEDED(r), "GetArguments failed (0x%08x)\n", r);
249     ok(*buffer=='\0', "GetArguments returned '%s'\n", buffer);
250
251     str="param1 \"spaced param2\"";
252     r = IShellLinkA_SetArguments(sl, str);
253     ok(SUCCEEDED(r), "SetArguments failed (0x%08x)\n", r);
254
255     strcpy(buffer,"garbage");
256     r = IShellLinkA_GetArguments(sl, buffer, sizeof(buffer));
257     ok(SUCCEEDED(r), "GetArguments failed (0x%08x)\n", r);
258     ok(lstrcmp(buffer,str)==0, "GetArguments returned '%s'\n", buffer);
259
260     /* Test Getting / Setting showcmd */
261     i=0xdeadbeef;
262     r = IShellLinkA_GetShowCmd(sl, &i);
263     ok(SUCCEEDED(r), "GetShowCmd failed (0x%08x)\n", r);
264     ok(i==SW_SHOWNORMAL, "GetShowCmd returned %d\n", i);
265
266     r = IShellLinkA_SetShowCmd(sl, SW_SHOWMAXIMIZED);
267     ok(SUCCEEDED(r), "SetShowCmd failed (0x%08x)\n", r);
268
269     i=0xdeadbeef;
270     r = IShellLinkA_GetShowCmd(sl, &i);
271     ok(SUCCEEDED(r), "GetShowCmd failed (0x%08x)\n", r);
272     ok(i==SW_SHOWMAXIMIZED, "GetShowCmd returned %d'\n", i);
273
274     /* Test Getting / Setting the icon */
275     i=0xdeadbeef;
276     strcpy(buffer,"garbage");
277     r = IShellLinkA_GetIconLocation(sl, buffer, sizeof(buffer), &i);
278     todo_wine {
279     ok(SUCCEEDED(r), "GetIconLocation failed (0x%08x)\n", r);
280     }
281     ok(*buffer=='\0', "GetIconLocation returned '%s'\n", buffer);
282     ok(i==0, "GetIconLocation returned %d\n", i);
283
284     str="c:\\nonexistent\\file";
285     r = IShellLinkA_SetIconLocation(sl, str, 0xbabecafe);
286     ok(SUCCEEDED(r), "SetIconLocation failed (0x%08x)\n", r);
287
288     i=0xdeadbeef;
289     r = IShellLinkA_GetIconLocation(sl, buffer, sizeof(buffer), &i);
290     ok(SUCCEEDED(r), "GetIconLocation failed (0x%08x)\n", r);
291     ok(lstrcmpi(buffer,str)==0, "GetIconLocation returned '%s'\n", buffer);
292     ok(i==0xbabecafe, "GetIconLocation returned %d'\n", i);
293
294     /* Test Getting / Setting the hot key */
295     w=0xbeef;
296     r = IShellLinkA_GetHotkey(sl, &w);
297     ok(SUCCEEDED(r), "GetHotkey failed (0x%08x)\n", r);
298     ok(w==0, "GetHotkey returned %d\n", w);
299
300     r = IShellLinkA_SetHotkey(sl, 0x5678);
301     ok(SUCCEEDED(r), "SetHotkey failed (0x%08x)\n", r);
302
303     w=0xbeef;
304     r = IShellLinkA_GetHotkey(sl, &w);
305     ok(SUCCEEDED(r), "GetHotkey failed (0x%08x)\n", r);
306     ok(w==0x5678, "GetHotkey returned %d'\n", w);
307
308     IShellLinkA_Release(sl);
309 }
310
311
312 /*
313  * Test saving and loading .lnk files
314  */
315
316 #define lok                   ok_(__FILE__, line)
317 #define lok_todo_4(todo_flag,a,b,c,d) \
318     if ((todo & todo_flag) == 0) lok((a), (b), (c), (d)); \
319     else todo_wine lok((a), (b), (c), (d));
320 #define lok_todo_2(todo_flag,a,b) \
321     if ((todo & todo_flag) == 0) lok((a), (b)); \
322     else todo_wine lok((a), (b));
323 #define check_lnk(a,b,c)        check_lnk_(__LINE__, (a), (b), (c))
324
325 void create_lnk_(int line, const WCHAR* path, lnk_desc_t* desc, int save_fails)
326 {
327     HRESULT r;
328     IShellLinkA *sl;
329     IPersistFile *pf;
330
331     r = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
332                          &IID_IShellLinkA, (LPVOID*)&sl);
333     lok(SUCCEEDED(r), "no IID_IShellLinkA (0x%08x)\n", r);
334     if (FAILED(r))
335         return;
336
337     if (desc->description)
338     {
339         r = IShellLinkA_SetDescription(sl, desc->description);
340         lok(SUCCEEDED(r), "SetDescription failed (0x%08x)\n", r);
341     }
342     if (desc->workdir)
343     {
344         r = IShellLinkA_SetWorkingDirectory(sl, desc->workdir);
345         lok(SUCCEEDED(r), "SetWorkingDirectory failed (0x%08x)\n", r);
346     }
347     if (desc->path)
348     {
349         r = IShellLinkA_SetPath(sl, desc->path);
350         lok(SUCCEEDED(r), "SetPath failed (0x%08x)\n", r);
351     }
352     if (desc->pidl)
353     {
354         r = IShellLinkA_SetIDList(sl, desc->pidl);
355         lok(SUCCEEDED(r), "SetIDList failed (0x%08x)\n", r);
356     }
357     if (desc->arguments)
358     {
359         r = IShellLinkA_SetArguments(sl, desc->arguments);
360         lok(SUCCEEDED(r), "SetArguments failed (0x%08x)\n", r);
361     }
362     if (desc->showcmd)
363     {
364         r = IShellLinkA_SetShowCmd(sl, desc->showcmd);
365         lok(SUCCEEDED(r), "SetShowCmd failed (0x%08x)\n", r);
366     }
367     if (desc->icon)
368     {
369         r = IShellLinkA_SetIconLocation(sl, desc->icon, desc->icon_id);
370         lok(SUCCEEDED(r), "SetIconLocation failed (0x%08x)\n", r);
371     }
372     if (desc->hotkey)
373     {
374         r = IShellLinkA_SetHotkey(sl, desc->hotkey);
375         lok(SUCCEEDED(r), "SetHotkey failed (0x%08x)\n", r);
376     }
377
378     r = IShellLinkW_QueryInterface(sl, &IID_IPersistFile, (LPVOID*)&pf);
379     lok(SUCCEEDED(r), "no IID_IPersistFile (0x%08x)\n", r);
380     if (SUCCEEDED(r))
381     {
382         r = IPersistFile_Save(pf, path, TRUE);
383         if (save_fails)
384         {
385             todo_wine {
386             lok(SUCCEEDED(r), "save failed (0x%08x)\n", r);
387             }
388         }
389         else
390         {
391             lok(SUCCEEDED(r), "save failed (0x%08x)\n", r);
392         }
393         IPersistFile_Release(pf);
394     }
395
396     IShellLinkA_Release(sl);
397 }
398
399 static void check_lnk_(int line, const WCHAR* path, lnk_desc_t* desc, int todo)
400 {
401     HRESULT r;
402     IShellLinkA *sl;
403     IPersistFile *pf;
404     char buffer[INFOTIPSIZE];
405
406     r = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
407                          &IID_IShellLinkA, (LPVOID*)&sl);
408     lok(SUCCEEDED(r), "no IID_IShellLinkA (0x%08x)\n", r);
409     if (FAILED(r))
410         return;
411
412     r = IShellLinkA_QueryInterface(sl, &IID_IPersistFile, (LPVOID*)&pf);
413     lok(SUCCEEDED(r), "no IID_IPersistFile (0x%08x)\n", r);
414     if (FAILED(r))
415     {
416         IShellLinkA_Release(sl);
417         return;
418     }
419
420     r = IPersistFile_Load(pf, path, STGM_READ);
421     lok(SUCCEEDED(r), "load failed (0x%08x)\n", r);
422     IPersistFile_Release(pf);
423     if (FAILED(r))
424     {
425         IShellLinkA_Release(sl);
426         return;
427     }
428
429     if (desc->description)
430     {
431         strcpy(buffer,"garbage");
432         r = IShellLinkA_GetDescription(sl, buffer, sizeof(buffer));
433         lok(SUCCEEDED(r), "GetDescription failed (0x%08x)\n", r);
434         lok_todo_4(0x1, lstrcmp(buffer, desc->description)==0,
435            "GetDescription returned '%s' instead of '%s'\n",
436            buffer, desc->description);
437     }
438     if (desc->workdir)
439     {
440         strcpy(buffer,"garbage");
441         r = IShellLinkA_GetWorkingDirectory(sl, buffer, sizeof(buffer));
442         lok(SUCCEEDED(r), "GetWorkingDirectory failed (0x%08x)\n", r);
443         lok_todo_4(0x2, lstrcmpi(buffer, desc->workdir)==0,
444            "GetWorkingDirectory returned '%s' instead of '%s'\n",
445            buffer, desc->workdir);
446     }
447     if (desc->path)
448     {
449         strcpy(buffer,"garbage");
450         r = IShellLinkA_GetPath(sl, buffer, sizeof(buffer), NULL, SLGP_RAWPATH);
451         lok(SUCCEEDED(r), "GetPath failed (0x%08x)\n", r);
452         lok_todo_4(0x4, lstrcmpi(buffer, desc->path)==0,
453            "GetPath returned '%s' instead of '%s'\n",
454            buffer, desc->path);
455     }
456     if (desc->pidl)
457     {
458         LPITEMIDLIST pidl=NULL;
459         r = IShellLinkA_GetIDList(sl, &pidl);
460         lok(SUCCEEDED(r), "GetIDList failed (0x%08x)\n", r);
461         lok_todo_2(0x8, pILIsEqual(pidl, desc->pidl),
462            "GetIDList returned an incorrect pidl\n");
463     }
464     if (desc->showcmd)
465     {
466         int i=0xdeadbeef;
467         r = IShellLinkA_GetShowCmd(sl, &i);
468         lok(SUCCEEDED(r), "GetShowCmd failed (0x%08x)\n", r);
469         lok_todo_4(0x10, i==desc->showcmd,
470            "GetShowCmd returned 0x%0x instead of 0x%0x\n",
471            i, desc->showcmd);
472     }
473     if (desc->icon)
474     {
475         int i=0xdeadbeef;
476         strcpy(buffer,"garbage");
477         r = IShellLinkA_GetIconLocation(sl, buffer, sizeof(buffer), &i);
478         lok(SUCCEEDED(r), "GetIconLocation failed (0x%08x)\n", r);
479         lok_todo_4(0x20, lstrcmpi(buffer, desc->icon)==0,
480            "GetIconLocation returned '%s' instead of '%s'\n",
481            buffer, desc->icon);
482         lok_todo_4(0x20, i==desc->icon_id,
483            "GetIconLocation returned 0x%0x instead of 0x%0x\n",
484            i, desc->icon_id);
485     }
486     if (desc->hotkey)
487     {
488         WORD i=0xbeef;
489         r = IShellLinkA_GetHotkey(sl, &i);
490         lok(SUCCEEDED(r), "GetHotkey failed (0x%08x)\n", r);
491         lok_todo_4(0x40, i==desc->hotkey,
492            "GetHotkey returned 0x%04x instead of 0x%04x\n",
493            i, desc->hotkey);
494     }
495
496     IShellLinkA_Release(sl);
497 }
498
499 static void test_load_save(void)
500 {
501     WCHAR lnkfile[MAX_PATH];
502     char lnkfileA[MAX_PATH];
503     static const char lnkfileA_name[] = "\\test.lnk";
504
505     lnk_desc_t desc;
506     char mypath[MAX_PATH];
507     char mydir[MAX_PATH];
508     char realpath[MAX_PATH];
509     char* p;
510     HANDLE hf;
511     DWORD r;
512
513     if (!pGetLongPathNameA)
514     {
515         skip("GetLongPathNameA is not available\n");
516         return;
517     }
518
519     /* Don't used a fixed path for the test.lnk file */
520     GetTempPathA(MAX_PATH, lnkfileA);
521     lstrcatA(lnkfileA, lnkfileA_name);
522     MultiByteToWideChar(CP_ACP, 0, lnkfileA, -1, lnkfile, MAX_PATH);
523
524     /* Save an empty .lnk file */
525     memset(&desc, 0, sizeof(desc));
526     create_lnk(lnkfile, &desc, 0);
527
528     /* It should come back as a bunch of empty strings */
529     desc.description="";
530     desc.workdir="";
531     desc.path="";
532     desc.arguments="";
533     desc.icon="";
534     check_lnk(lnkfile, &desc, 0x0);
535
536     /* Point a .lnk file to nonexistent files */
537     desc.description="";
538     desc.workdir="c:\\Nonexitent\\work\\directory";
539     desc.path="c:\\nonexistent\\path";
540     desc.pidl=NULL;
541     desc.arguments="";
542     desc.showcmd=0;
543     desc.icon="c:\\nonexistent\\icon\\file";
544     desc.icon_id=1234;
545     desc.hotkey=0;
546     create_lnk(lnkfile, &desc, 0);
547     check_lnk(lnkfile, &desc, 0x0);
548
549     r=GetModuleFileName(NULL, mypath, sizeof(mypath));
550     ok(r<sizeof(mypath), "GetModuleFileName failed (%d)\n", r);
551     strcpy(mydir, mypath);
552     p=strrchr(mydir, '\\');
553     if (p)
554         *p='\0';
555
556     /* IShellLink returns path in long form */
557     if (!pGetLongPathNameA(mypath, realpath, MAX_PATH)) strcpy( realpath, mypath );
558
559     /* Overwrite the existing lnk file and point it to existing files */
560     desc.description="test 2";
561     desc.workdir=mydir;
562     desc.path=realpath;
563     desc.pidl=NULL;
564     desc.arguments="/option1 /option2 \"Some string\"";
565     desc.showcmd=SW_SHOWNORMAL;
566     desc.icon=mypath;
567     desc.icon_id=0;
568     desc.hotkey=0x1234;
569     create_lnk(lnkfile, &desc, 0);
570     check_lnk(lnkfile, &desc, 0x0);
571
572     /* Overwrite the existing lnk file and test link to a command on the path */
573     desc.description="command on path";
574     desc.workdir=mypath;
575     desc.path="rundll32.exe";
576     desc.pidl=NULL;
577     desc.arguments="/option1 /option2 \"Some string\"";
578     desc.showcmd=SW_SHOWNORMAL;
579     desc.icon=mypath;
580     desc.icon_id=0;
581     desc.hotkey=0x1234;
582     create_lnk(lnkfile, &desc, 0);
583     /* Check that link is created to proper location */
584     SearchPathA( NULL, desc.path, NULL, MAX_PATH, realpath, NULL);
585     desc.path=realpath;
586     check_lnk(lnkfile, &desc, 0x0);
587
588     /* Create a temporary non-executable file */
589     r=GetTempPath(sizeof(mypath), mypath);
590     ok(r<sizeof(mypath), "GetTempPath failed (%d), err %d\n", r, GetLastError());
591     r=pGetLongPathNameA(mypath, mydir, sizeof(mydir));
592     ok(r<sizeof(mydir), "GetLongPathName failed (%d), err %d\n", r, GetLastError());
593     p=strrchr(mydir, '\\');
594     if (p)
595         *p='\0';
596
597     strcpy(mypath, mydir);
598     strcat(mypath, "\\test.txt");
599     hf = CreateFile(mypath, GENERIC_WRITE, 0, NULL,
600                     CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
601     CloseHandle(hf);
602
603     /* Overwrite the existing lnk file and test link to an existing non-executable file */
604     desc.description="non-executable file";
605     desc.workdir=mydir;
606     desc.path=mypath;
607     desc.pidl=NULL;
608     desc.arguments="";
609     desc.showcmd=SW_SHOWNORMAL;
610     desc.icon=mypath;
611     desc.icon_id=0;
612     desc.hotkey=0x1234;
613     create_lnk(lnkfile, &desc, 0);
614     check_lnk(lnkfile, &desc, 0x0);
615
616     r = DeleteFileA(mypath);
617     ok(r, "failed to delete file %s (%d)\n", mypath, GetLastError());
618
619     /* FIXME: Also test saving a .lnk pointing to a pidl that cannot be
620      * represented as a path.
621      */
622
623     /* DeleteFileW is not implemented on Win9x */
624     r=DeleteFileA(lnkfileA);
625     ok(r, "failed to delete link '%s' (%d)\n", lnkfileA, GetLastError());
626 }
627
628 static void test_datalink(void)
629 {
630     static const WCHAR lnk[] = {
631       ':',':','{','9','d','b','1','1','8','6','f','-','4','0','d','f','-','1',
632       '1','d','1','-','a','a','8','c','-','0','0','c','0','4','f','b','6','7',
633       '8','6','3','}',':','{','0','0','0','1','0','4','0','9','-','7','8','E',
634       '1','-','1','1','D','2','-','B','6','0','F','-','0','0','6','0','9','7',
635       'C','9','9','8','E','7','}',':',':','{','9','d','b','1','1','8','6','e',
636       '-','4','0','d','f','-','1','1','d','1','-','a','a','8','c','-','0','0',
637       'c','0','4','f','b','6','7','8','6','3','}',':','2','6',',','!','!','g',
638       'x','s','f','(','N','g',']','q','F','`','H','{','L','s','A','C','C','E',
639       'S','S','F','i','l','e','s','>','p','l','T',']','j','I','{','j','f','(',
640       '=','1','&','L','[','-','8','1','-',']',':',':',0 };
641     static const WCHAR comp[] = {
642       '2','6',',','!','!','g','x','s','f','(','N','g',']','q','F','`','H','{',
643       'L','s','A','C','C','E','S','S','F','i','l','e','s','>','p','l','T',']',
644       'j','I','{','j','f','(','=','1','&','L','[','-','8','1','-',']',0 };
645     IShellLinkDataList *dl = NULL;
646     IShellLinkW *sl = NULL;
647     HRESULT r;
648     DWORD flags = 0;
649     EXP_DARWIN_LINK *dar;
650
651     r = CoCreateInstance( &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
652                             &IID_IShellLinkW, (LPVOID*)&sl );
653     ok( r == S_OK ||
654         broken(r == E_NOINTERFACE), /* Win9x */
655         "CoCreateInstance failed (0x%08x)\n", r);
656     if (!sl)
657     {
658         skip("no shelllink\n");
659         return;
660     }
661
662     r = IShellLinkW_QueryInterface( sl, &_IID_IShellLinkDataList, (LPVOID*) &dl );
663     ok( r == S_OK ||
664         broken(r == E_NOINTERFACE), /* NT4 */
665         "IShellLinkW_QueryInterface failed (0x%08x)\n", r);
666
667     if (!dl)
668     {
669         skip("no datalink interface\n");
670         IShellLinkW_Release( sl );
671         return;
672     }
673
674     flags = 0;
675     r = IShellLinkDataList_GetFlags( dl, &flags );
676     ok( r == S_OK, "GetFlags failed\n");
677     ok( flags == 0, "GetFlags returned wrong flags\n");
678
679     dar = (void*)-1;
680     r = IShellLinkDataList_CopyDataBlock( dl, EXP_DARWIN_ID_SIG, (LPVOID*) &dar );
681     ok( r == E_FAIL, "CopyDataBlock failed\n");
682     ok( dar == NULL, "should be null\n");
683
684     r = IShellLinkW_SetPath(sl, lnk);
685     ok(r == S_OK, "set path failed\n");
686
687     /*
688      * The following crashes:
689      * r = IShellLinkDataList_GetFlags( dl, NULL );
690      */
691
692     flags = 0;
693     r = IShellLinkDataList_GetFlags( dl, &flags );
694     ok( r == S_OK, "GetFlags failed\n");
695     ok( flags == (SLDF_HAS_DARWINID|SLDF_HAS_LOGO3ID),
696         "GetFlags returned wrong flags\n");
697
698     dar = NULL;
699     r = IShellLinkDataList_CopyDataBlock( dl, EXP_DARWIN_ID_SIG, (LPVOID*) &dar );
700     ok( r == S_OK, "CopyDataBlock failed\n");
701
702     ok( dar && ((DATABLOCK_HEADER*)dar)->dwSignature == EXP_DARWIN_ID_SIG, "signature wrong\n");
703     ok( dar && 0==lstrcmpW(dar->szwDarwinID, comp ), "signature wrong\n");
704
705     LocalFree( dar );
706
707     IUnknown_Release( dl );
708     IShellLinkW_Release( sl );
709 }
710
711 START_TEST(shelllink)
712 {
713     HRESULT r;
714     HMODULE hmod = GetModuleHandleA("shell32.dll");
715     HMODULE hkernel32 = GetModuleHandleA("kernel32.dll");
716
717     pILFree = (fnILFree) GetProcAddress(hmod, (LPSTR)155);
718     pILIsEqual = (fnILIsEqual) GetProcAddress(hmod, (LPSTR)21);
719     pSHILCreateFromPath = (fnSHILCreateFromPath) GetProcAddress(hmod, (LPSTR)28);
720
721     pGetLongPathNameA = (void *)GetProcAddress(hkernel32, "GetLongPathNameA");
722
723     r = CoInitialize(NULL);
724     ok(SUCCEEDED(r), "CoInitialize failed (0x%08x)\n", r);
725     if (FAILED(r))
726         return;
727
728     test_get_set();
729     test_load_save();
730     test_datalink();
731
732     CoUninitialize();
733 }