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