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