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