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