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