shell32: Test links to executable filenames lacking the extension.
[wine.git] / dlls / shell32 / tests / shelllink.c
blob29b0dda13abd5096d263482f8b2d714df8b4622d
1 /*
2 * Unit tests for shelllinks
4 * Copyright 2004 Mike McCormack
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.
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.
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).
25 #define COBJMACROS
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"
34 #include "shell32_test.h"
36 #ifndef SLDF_HAS_LOGO3ID
37 # define SLDF_HAS_LOGO3ID 0x00000800 /* not available in the Vista SDK */
38 #endif
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);
45 static fnILFree pILFree;
46 static fnILIsEqual pILIsEqual;
47 static fnSHILCreateFromPath pSHILCreateFromPath;
48 static fnSHDefExtractIconA pSHDefExtractIconA;
50 static DWORD (WINAPI *pGetLongPathNameA)(LPCSTR, LPSTR, DWORD);
51 static DWORD (WINAPI *pGetShortPathNameA)(LPCSTR, LPSTR, DWORD);
53 static const GUID _IID_IShellLinkDataList = {
54 0x45e2b4ae, 0xb1c3, 0x11d0,
55 { 0xb9, 0x2f, 0x00, 0xa0, 0xc9, 0x03, 0x12, 0xe1 }
58 static const WCHAR notafile[]= { 'C',':','\\','n','o','n','e','x','i','s','t','e','n','t','\\','f','i','l','e',0 };
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.
65 static LPITEMIDLIST (WINAPI *pSHSimpleIDListFromPathAW)(LPCVOID);
67 static LPITEMIDLIST path_to_pidl(const char* path)
69 LPITEMIDLIST pidl;
71 if (!pSHSimpleIDListFromPathAW)
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");
79 pidl=NULL;
80 /* pSHSimpleIDListFromPathAW maps to A on non NT platforms */
81 if (pSHSimpleIDListFromPathAW && (GetVersion() & 0x80000000))
82 pidl=pSHSimpleIDListFromPathAW(path);
84 if (!pidl)
86 WCHAR* pathW;
87 HRESULT r;
88 int len;
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);
94 r=pSHILCreateFromPath(pathW, &pidl, NULL);
95 ok(r == S_OK, "SHILCreateFromPath failed (0x%08x)\n", r);
96 HeapFree(GetProcessHeap(), 0, pathW);
98 return pidl;
103 * Test manipulation of an IShellLink's properties.
106 static void test_get_set(void)
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;
118 r = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
119 &IID_IShellLinkA, (LPVOID*)&sl);
120 ok(r == S_OK, "no IID_IShellLinkA (0x%08x)\n", r);
121 if (r != S_OK)
122 return;
124 /* Test Getting / Setting the description */
125 strcpy(buffer,"garbage");
126 r = IShellLinkA_GetDescription(sl, buffer, sizeof(buffer));
127 ok(r == S_OK, "GetDescription failed (0x%08x)\n", r);
128 ok(*buffer=='\0', "GetDescription returned '%s'\n", buffer);
130 str="Some description";
131 r = IShellLinkA_SetDescription(sl, str);
132 ok(r == S_OK, "SetDescription failed (0x%08x)\n", r);
134 strcpy(buffer,"garbage");
135 r = IShellLinkA_GetDescription(sl, buffer, sizeof(buffer));
136 ok(r == S_OK, "GetDescription failed (0x%08x)\n", r);
137 ok(lstrcmp(buffer,str)==0, "GetDescription returned '%s'\n", buffer);
139 r = IShellLinkA_SetDescription(sl, NULL);
140 ok(r == S_OK, "SetDescription failed (0x%08x)\n", r);
142 strcpy(buffer,"garbage");
143 r = IShellLinkA_GetDescription(sl, buffer, sizeof(buffer));
144 ok(r == S_OK, "GetDescription failed (0x%08x)\n", r);
145 ok(*buffer=='\0' || broken(lstrcmp(buffer,str)==0), "GetDescription returned '%s'\n", buffer); /* NT4 */
148 /* Test Getting / Setting the work directory */
149 strcpy(buffer,"garbage");
150 r = IShellLinkA_GetWorkingDirectory(sl, buffer, sizeof(buffer));
151 ok(r == S_OK, "GetWorkingDirectory failed (0x%08x)\n", r);
152 ok(*buffer=='\0', "GetWorkingDirectory returned '%s'\n", buffer);
154 str="c:\\nonexistent\\directory";
155 r = IShellLinkA_SetWorkingDirectory(sl, str);
156 ok(r == S_OK, "SetWorkingDirectory failed (0x%08x)\n", r);
158 strcpy(buffer,"garbage");
159 r = IShellLinkA_GetWorkingDirectory(sl, buffer, sizeof(buffer));
160 ok(r == S_OK, "GetWorkingDirectory failed (0x%08x)\n", r);
161 ok(lstrcmpi(buffer,str)==0, "GetWorkingDirectory returned '%s'\n", buffer);
163 /* Test Getting / Setting the path */
164 strcpy(buffer,"garbage");
165 r = IShellLinkA_GetPath(sl, buffer, sizeof(buffer), NULL, SLGP_RAWPATH);
166 todo_wine ok(r == S_FALSE || broken(r == S_OK) /* NT4/W2K */, "GetPath failed (0x%08x)\n", r);
167 ok(*buffer=='\0', "GetPath returned '%s'\n", buffer);
169 CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
170 &IID_IShellLinkW, (LPVOID*)&slW);
171 if (!slW /* Win9x */ || !pGetLongPathNameA /* NT4 */)
172 skip("SetPath with NULL parameter crashes on Win9x and some NT4\n");
173 else
175 IShellLinkW_Release(slW);
176 r = IShellLinkA_SetPath(sl, NULL);
177 ok(r==E_INVALIDARG ||
178 broken(r==S_OK), /* Some Win95 and NT4 */
179 "SetPath returned wrong error (0x%08x)\n", r);
182 r = IShellLinkA_SetPath(sl, "");
183 ok(r==S_OK, "SetPath failed (0x%08x)\n", r);
185 strcpy(buffer,"garbage");
186 r = IShellLinkA_GetPath(sl, buffer, sizeof(buffer), NULL, SLGP_RAWPATH);
187 todo_wine ok(r == S_FALSE, "GetPath failed (0x%08x)\n", r);
188 ok(*buffer=='\0', "GetPath returned '%s'\n", buffer);
190 /* Win98 returns S_FALSE, but WinXP returns S_OK */
191 str="c:\\nonexistent\\file";
192 r = IShellLinkA_SetPath(sl, str);
193 ok(r==S_FALSE || r==S_OK, "SetPath failed (0x%08x)\n", r);
195 strcpy(buffer,"garbage");
196 r = IShellLinkA_GetPath(sl, buffer, sizeof(buffer), NULL, SLGP_RAWPATH);
197 ok(r == S_OK, "GetPath failed (0x%08x)\n", r);
198 ok(lstrcmpi(buffer,str)==0, "GetPath returned '%s'\n", buffer);
200 /* Get some real path to play with */
201 GetWindowsDirectoryA( mypath, sizeof(mypath)-12 );
202 strcat(mypath, "\\regedit.exe");
204 /* Test the interaction of SetPath and SetIDList */
205 tmp_pidl=NULL;
206 r = IShellLinkA_GetIDList(sl, &tmp_pidl);
207 ok(r == S_OK, "GetIDList failed (0x%08x)\n", r);
208 if (r == S_OK)
210 BOOL ret;
212 strcpy(buffer,"garbage");
213 ret = SHGetPathFromIDListA(tmp_pidl, buffer);
214 ok(ret, "SHGetPathFromIDListA failed\n");
215 if (ret)
216 ok(lstrcmpi(buffer,str)==0, "GetIDList returned '%s'\n", buffer);
217 pILFree(tmp_pidl);
220 pidl=path_to_pidl(mypath);
221 ok(pidl!=NULL, "path_to_pidl returned a NULL pidl\n");
223 if (pidl)
225 LPITEMIDLIST second_pidl;
227 r = IShellLinkA_SetIDList(sl, pidl);
228 ok(r == S_OK, "SetIDList failed (0x%08x)\n", r);
230 tmp_pidl=NULL;
231 r = IShellLinkA_GetIDList(sl, &tmp_pidl);
232 ok(r == S_OK, "GetIDList failed (0x%08x)\n", r);
233 ok(tmp_pidl && pILIsEqual(pidl, tmp_pidl),
234 "GetIDList returned an incorrect pidl\n");
236 r = IShellLinkA_GetIDList(sl, &second_pidl);
237 ok(r == S_OK, "GetIDList failed (0x%08x)\n", r);
238 ok(second_pidl && pILIsEqual(pidl, second_pidl),
239 "GetIDList returned an incorrect pidl\n");
240 ok(second_pidl != tmp_pidl, "pidls are the same\n");
242 pILFree(second_pidl);
243 pILFree(tmp_pidl);
244 pILFree(pidl);
246 strcpy(buffer,"garbage");
247 r = IShellLinkA_GetPath(sl, buffer, sizeof(buffer), NULL, SLGP_RAWPATH);
248 ok(r == S_OK, "GetPath failed (0x%08x)\n", r);
249 todo_wine
250 ok(lstrcmpi(buffer, mypath)==0, "GetPath returned '%s'\n", buffer);
254 /* test path with quotes (IShellLinkA_SetPath returns S_FALSE on W2K and below and S_OK on XP and above */
255 r = IShellLinkA_SetPath(sl, "\"c:\\nonexistent\\file\"");
256 ok(r==S_FALSE || r == S_OK, "SetPath failed (0x%08x)\n", r);
258 strcpy(buffer,"garbage");
259 r = IShellLinkA_GetPath(sl, buffer, sizeof(buffer), NULL, SLGP_RAWPATH);
260 ok(r==S_OK, "GetPath failed (0x%08x)\n", r);
261 ok(!lstrcmp(buffer, "C:\\nonexistent\\file") ||
262 broken(!lstrcmp(buffer, "C:\\\"c:\\nonexistent\\file\"")), /* NT4 */
263 "case doesn't match\n");
265 r = IShellLinkA_SetPath(sl, "\"c:\\foo");
266 ok(r==S_FALSE || r == S_OK || r == E_INVALIDARG /* Vista */, "SetPath failed (0x%08x)\n", r);
268 r = IShellLinkA_SetPath(sl, "\"\"c:\\foo");
269 ok(r==S_FALSE || r == S_OK || r == E_INVALIDARG /* Vista */, "SetPath failed (0x%08x)\n", r);
271 r = IShellLinkA_SetPath(sl, "c:\\foo\"");
272 ok(r==S_FALSE || r == S_OK || r == E_INVALIDARG /* Vista */, "SetPath failed (0x%08x)\n", r);
274 r = IShellLinkA_SetPath(sl, "\"\"c:\\foo\"");
275 ok(r==S_FALSE || r == S_OK || r == E_INVALIDARG /* Vista */, "SetPath failed (0x%08x)\n", r);
277 r = IShellLinkA_SetPath(sl, "\"\"c:\\foo\"\"");
278 ok(r==S_FALSE || r == S_OK || r == E_INVALIDARG /* Vista */, "SetPath failed (0x%08x)\n", r);
280 /* Test Getting / Setting the arguments */
281 strcpy(buffer,"garbage");
282 r = IShellLinkA_GetArguments(sl, buffer, sizeof(buffer));
283 ok(r == S_OK, "GetArguments failed (0x%08x)\n", r);
284 ok(*buffer=='\0', "GetArguments returned '%s'\n", buffer);
286 str="param1 \"spaced param2\"";
287 r = IShellLinkA_SetArguments(sl, str);
288 ok(r == S_OK, "SetArguments failed (0x%08x)\n", r);
290 strcpy(buffer,"garbage");
291 r = IShellLinkA_GetArguments(sl, buffer, sizeof(buffer));
292 ok(r == S_OK, "GetArguments failed (0x%08x)\n", r);
293 ok(lstrcmp(buffer,str)==0, "GetArguments returned '%s'\n", buffer);
295 strcpy(buffer,"garbage");
296 r = IShellLinkA_SetArguments(sl, NULL);
297 ok(r == S_OK, "SetArguments failed (0x%08x)\n", r);
298 r = IShellLinkA_GetArguments(sl, buffer, sizeof(buffer));
299 ok(r == S_OK, "GetArguments failed (0x%08x)\n", r);
300 ok(!buffer[0] || lstrcmp(buffer,str)==0, "GetArguments returned '%s'\n", buffer);
302 strcpy(buffer,"garbage");
303 r = IShellLinkA_SetArguments(sl, "");
304 ok(r == S_OK, "SetArguments failed (0x%08x)\n", r);
305 r = IShellLinkA_GetArguments(sl, buffer, sizeof(buffer));
306 ok(r == S_OK, "GetArguments failed (0x%08x)\n", r);
307 ok(!buffer[0], "GetArguments returned '%s'\n", buffer);
309 /* Test Getting / Setting showcmd */
310 i=0xdeadbeef;
311 r = IShellLinkA_GetShowCmd(sl, &i);
312 ok(r == S_OK, "GetShowCmd failed (0x%08x)\n", r);
313 ok(i==SW_SHOWNORMAL, "GetShowCmd returned %d\n", i);
315 r = IShellLinkA_SetShowCmd(sl, SW_SHOWMAXIMIZED);
316 ok(r == S_OK, "SetShowCmd failed (0x%08x)\n", r);
318 i=0xdeadbeef;
319 r = IShellLinkA_GetShowCmd(sl, &i);
320 ok(r == S_OK, "GetShowCmd failed (0x%08x)\n", r);
321 ok(i==SW_SHOWMAXIMIZED, "GetShowCmd returned %d'\n", i);
323 /* Test Getting / Setting the icon */
324 i=0xdeadbeef;
325 strcpy(buffer,"garbage");
326 r = IShellLinkA_GetIconLocation(sl, buffer, sizeof(buffer), &i);
327 ok(r == S_OK, "GetIconLocation failed (0x%08x)\n", r);
328 ok(*buffer=='\0', "GetIconLocation returned '%s'\n", buffer);
329 ok(i==0, "GetIconLocation returned %d\n", i);
331 str="c:\\nonexistent\\file";
332 r = IShellLinkA_SetIconLocation(sl, str, 0xbabecafe);
333 ok(r == S_OK, "SetIconLocation failed (0x%08x)\n", r);
335 i=0xdeadbeef;
336 r = IShellLinkA_GetIconLocation(sl, buffer, sizeof(buffer), &i);
337 ok(r == S_OK, "GetIconLocation failed (0x%08x)\n", r);
338 ok(lstrcmpi(buffer,str)==0, "GetIconLocation returned '%s'\n", buffer);
339 ok(i==0xbabecafe, "GetIconLocation returned %d'\n", i);
341 /* Test Getting / Setting the hot key */
342 w=0xbeef;
343 r = IShellLinkA_GetHotkey(sl, &w);
344 ok(r == S_OK, "GetHotkey failed (0x%08x)\n", r);
345 ok(w==0, "GetHotkey returned %d\n", w);
347 r = IShellLinkA_SetHotkey(sl, 0x5678);
348 ok(r == S_OK, "SetHotkey failed (0x%08x)\n", r);
350 w=0xbeef;
351 r = IShellLinkA_GetHotkey(sl, &w);
352 ok(r == S_OK, "GetHotkey failed (0x%08x)\n", r);
353 ok(w==0x5678, "GetHotkey returned %d'\n", w);
355 IShellLinkA_Release(sl);
360 * Test saving and loading .lnk files
363 #define lok ok_(__FILE__, line)
364 #define lok_todo_4(todo_flag,a,b,c,d) \
365 if ((todo & todo_flag) == 0) lok((a), (b), (c), (d)); \
366 else todo_wine lok((a), (b), (c), (d));
367 #define lok_todo_2(todo_flag,a,b) \
368 if ((todo & todo_flag) == 0) lok((a), (b)); \
369 else todo_wine lok((a), (b));
370 #define check_lnk(a,b,c) check_lnk_(__LINE__, (a), (b), (c))
372 void create_lnk_(int line, const WCHAR* path, lnk_desc_t* desc, int save_fails)
374 HRESULT r;
375 IShellLinkA *sl;
376 IPersistFile *pf;
378 r = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
379 &IID_IShellLinkA, (LPVOID*)&sl);
380 lok(r == S_OK, "no IID_IShellLinkA (0x%08x)\n", r);
381 if (r != S_OK)
382 return;
384 if (desc->description)
386 r = IShellLinkA_SetDescription(sl, desc->description);
387 lok(r == S_OK, "SetDescription failed (0x%08x)\n", r);
389 if (desc->workdir)
391 r = IShellLinkA_SetWorkingDirectory(sl, desc->workdir);
392 lok(r == S_OK, "SetWorkingDirectory failed (0x%08x)\n", r);
394 if (desc->path)
396 r = IShellLinkA_SetPath(sl, desc->path);
397 lok(SUCCEEDED(r), "SetPath failed (0x%08x)\n", r);
399 if (desc->pidl)
401 r = IShellLinkA_SetIDList(sl, desc->pidl);
402 lok(r == S_OK, "SetIDList failed (0x%08x)\n", r);
404 if (desc->arguments)
406 r = IShellLinkA_SetArguments(sl, desc->arguments);
407 lok(r == S_OK, "SetArguments failed (0x%08x)\n", r);
409 if (desc->showcmd)
411 r = IShellLinkA_SetShowCmd(sl, desc->showcmd);
412 lok(r == S_OK, "SetShowCmd failed (0x%08x)\n", r);
414 if (desc->icon)
416 r = IShellLinkA_SetIconLocation(sl, desc->icon, desc->icon_id);
417 lok(r == S_OK, "SetIconLocation failed (0x%08x)\n", r);
419 if (desc->hotkey)
421 r = IShellLinkA_SetHotkey(sl, desc->hotkey);
422 lok(r == S_OK, "SetHotkey failed (0x%08x)\n", r);
425 r = IShellLinkW_QueryInterface(sl, &IID_IPersistFile, (LPVOID*)&pf);
426 lok(r == S_OK, "no IID_IPersistFile (0x%08x)\n", r);
427 if (r == S_OK)
429 LPOLESTR str;
431 if (0)
433 /* crashes on XP */
434 r = IPersistFile_GetCurFile(pf, NULL);
437 /* test GetCurFile before ::Save */
438 str = (LPWSTR)0xdeadbeef;
439 r = IPersistFile_GetCurFile(pf, &str);
440 lok(r == S_FALSE ||
441 broken(r == S_OK), /* shell32 < 5.0 */
442 "got 0x%08x\n", r);
443 lok(str == NULL, "got %p\n", str);
445 r = IPersistFile_Save(pf, path, TRUE);
446 if (save_fails)
448 todo_wine {
449 lok(r == S_OK, "save failed (0x%08x)\n", r);
452 else
454 lok(r == S_OK, "save failed (0x%08x)\n", r);
457 /* test GetCurFile after ::Save */
458 r = IPersistFile_GetCurFile(pf, &str);
459 lok(r == S_OK, "got 0x%08x\n", r);
460 lok(str != NULL ||
461 broken(str == NULL), /* shell32 < 5.0 */
462 "Didn't expect NULL\n");
463 if (str != NULL)
465 IMalloc *pmalloc;
467 lok(!winetest_strcmpW(path, str), "Expected %s, got %s\n",
468 wine_dbgstr_w(path), wine_dbgstr_w(str));
470 SHGetMalloc(&pmalloc);
471 IMalloc_Free(pmalloc, str);
473 else
474 win_skip("GetCurFile fails on shell32 < 5.0\n");
476 IPersistFile_Release(pf);
479 IShellLinkA_Release(sl);
482 static void check_lnk_(int line, const WCHAR* path, lnk_desc_t* desc, int todo)
484 HRESULT r;
485 IShellLinkA *sl;
486 IPersistFile *pf;
487 char buffer[INFOTIPSIZE];
488 LPOLESTR str;
490 r = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
491 &IID_IShellLinkA, (LPVOID*)&sl);
492 lok(r == S_OK, "no IID_IShellLinkA (0x%08x)\n", r);
493 if (r != S_OK)
494 return;
496 r = IShellLinkA_QueryInterface(sl, &IID_IPersistFile, (LPVOID*)&pf);
497 lok(r == S_OK, "no IID_IPersistFile (0x%08x)\n", r);
498 if (r != S_OK)
500 IShellLinkA_Release(sl);
501 return;
504 /* test GetCurFile before ::Load */
505 str = (LPWSTR)0xdeadbeef;
506 r = IPersistFile_GetCurFile(pf, &str);
507 lok(r == S_FALSE ||
508 broken(r == S_OK), /* shell32 < 5.0 */
509 "got 0x%08x\n", r);
510 lok(str == NULL, "got %p\n", str);
512 r = IPersistFile_Load(pf, path, STGM_READ);
513 lok(r == S_OK, "load failed (0x%08x)\n", r);
515 /* test GetCurFile after ::Save */
516 r = IPersistFile_GetCurFile(pf, &str);
517 lok(r == S_OK, "got 0x%08x\n", r);
518 lok(str != NULL ||
519 broken(str == NULL), /* shell32 < 5.0 */
520 "Didn't expect NULL\n");
521 if (str != NULL)
523 IMalloc *pmalloc;
525 lok(!winetest_strcmpW(path, str), "Expected %s, got %s\n",
526 wine_dbgstr_w(path), wine_dbgstr_w(str));
528 SHGetMalloc(&pmalloc);
529 IMalloc_Free(pmalloc, str);
531 else
532 win_skip("GetCurFile fails on shell32 < 5.0\n");
534 IPersistFile_Release(pf);
535 if (r != S_OK)
537 IShellLinkA_Release(sl);
538 return;
541 if (desc->description)
543 strcpy(buffer,"garbage");
544 r = IShellLinkA_GetDescription(sl, buffer, sizeof(buffer));
545 lok(r == S_OK, "GetDescription failed (0x%08x)\n", r);
546 lok_todo_4(0x1, lstrcmp(buffer, desc->description)==0,
547 "GetDescription returned '%s' instead of '%s'\n",
548 buffer, desc->description);
550 if (desc->workdir)
552 strcpy(buffer,"garbage");
553 r = IShellLinkA_GetWorkingDirectory(sl, buffer, sizeof(buffer));
554 lok(r == S_OK, "GetWorkingDirectory failed (0x%08x)\n", r);
555 lok_todo_4(0x2, lstrcmpi(buffer, desc->workdir)==0,
556 "GetWorkingDirectory returned '%s' instead of '%s'\n",
557 buffer, desc->workdir);
559 if (desc->path)
561 strcpy(buffer,"garbage");
562 r = IShellLinkA_GetPath(sl, buffer, sizeof(buffer), NULL, SLGP_RAWPATH);
563 lok(SUCCEEDED(r), "GetPath failed (0x%08x)\n", r);
564 lok_todo_4(0x4, lstrcmpi(buffer, desc->path)==0,
565 "GetPath returned '%s' instead of '%s'\n",
566 buffer, desc->path);
568 if (desc->pidl)
570 LPITEMIDLIST pidl=NULL;
571 r = IShellLinkA_GetIDList(sl, &pidl);
572 lok(r == S_OK, "GetIDList failed (0x%08x)\n", r);
573 lok_todo_2(0x8, pILIsEqual(pidl, desc->pidl),
574 "GetIDList returned an incorrect pidl\n");
576 if (desc->showcmd)
578 int i=0xdeadbeef;
579 r = IShellLinkA_GetShowCmd(sl, &i);
580 lok(r == S_OK, "GetShowCmd failed (0x%08x)\n", r);
581 lok_todo_4(0x10, i==desc->showcmd,
582 "GetShowCmd returned 0x%0x instead of 0x%0x\n",
583 i, desc->showcmd);
585 if (desc->icon)
587 int i=0xdeadbeef;
588 strcpy(buffer,"garbage");
589 r = IShellLinkA_GetIconLocation(sl, buffer, sizeof(buffer), &i);
590 lok(r == S_OK, "GetIconLocation failed (0x%08x)\n", r);
591 lok_todo_4(0x20, lstrcmpi(buffer, desc->icon)==0,
592 "GetIconLocation returned '%s' instead of '%s'\n",
593 buffer, desc->icon);
594 lok_todo_4(0x20, i==desc->icon_id,
595 "GetIconLocation returned 0x%0x instead of 0x%0x\n",
596 i, desc->icon_id);
598 if (desc->hotkey)
600 WORD i=0xbeef;
601 r = IShellLinkA_GetHotkey(sl, &i);
602 lok(r == S_OK, "GetHotkey failed (0x%08x)\n", r);
603 lok_todo_4(0x40, i==desc->hotkey,
604 "GetHotkey returned 0x%04x instead of 0x%04x\n",
605 i, desc->hotkey);
608 IShellLinkA_Release(sl);
611 static void test_load_save(void)
613 WCHAR lnkfile[MAX_PATH];
614 char lnkfileA[MAX_PATH];
615 static const char lnkfileA_name[] = "\\test.lnk";
617 lnk_desc_t desc;
618 char mypath[MAX_PATH];
619 char mydir[MAX_PATH];
620 char realpath[MAX_PATH];
621 char* p;
622 HANDLE hf;
623 DWORD r;
625 if (!pGetLongPathNameA)
627 win_skip("GetLongPathNameA is not available\n");
628 return;
631 /* Don't used a fixed path for the test.lnk file */
632 GetTempPathA(MAX_PATH, lnkfileA);
633 lstrcatA(lnkfileA, lnkfileA_name);
634 MultiByteToWideChar(CP_ACP, 0, lnkfileA, -1, lnkfile, MAX_PATH);
636 /* Save an empty .lnk file */
637 memset(&desc, 0, sizeof(desc));
638 create_lnk(lnkfile, &desc, 0);
640 /* It should come back as a bunch of empty strings */
641 desc.description="";
642 desc.workdir="";
643 desc.path="";
644 desc.arguments="";
645 desc.icon="";
646 check_lnk(lnkfile, &desc, 0x0);
648 /* Point a .lnk file to nonexistent files */
649 desc.description="";
650 desc.workdir="c:\\Nonexitent\\work\\directory";
651 desc.path="c:\\nonexistent\\path";
652 desc.pidl=NULL;
653 desc.arguments="";
654 desc.showcmd=0;
655 desc.icon="c:\\nonexistent\\icon\\file";
656 desc.icon_id=1234;
657 desc.hotkey=0;
658 create_lnk(lnkfile, &desc, 0);
659 check_lnk(lnkfile, &desc, 0x0);
661 r=GetModuleFileName(NULL, mypath, sizeof(mypath));
662 ok(r<sizeof(mypath), "GetModuleFileName failed (%d)\n", r);
663 strcpy(mydir, mypath);
664 p=strrchr(mydir, '\\');
665 if (p)
666 *p='\0';
668 /* IShellLink returns path in long form */
669 if (!pGetLongPathNameA(mypath, realpath, MAX_PATH)) strcpy( realpath, mypath );
671 /* Overwrite the existing lnk file and point it to existing files */
672 desc.description="test 2";
673 desc.workdir=mydir;
674 desc.path=realpath;
675 desc.pidl=NULL;
676 desc.arguments="/option1 /option2 \"Some string\"";
677 desc.showcmd=SW_SHOWNORMAL;
678 desc.icon=mypath;
679 desc.icon_id=0;
680 desc.hotkey=0x1234;
681 create_lnk(lnkfile, &desc, 0);
682 check_lnk(lnkfile, &desc, 0x0);
684 /* Test omitting .exe from an absolute path */
685 p=strrchr(mypath, '.');
686 if (p)
687 *p='\0';
689 desc.description="absolute path without .exe";
690 desc.workdir=mydir;
691 desc.path=mypath;
692 desc.pidl=NULL;
693 desc.arguments="/option1 /option2 \"Some string\"";
694 desc.showcmd=SW_SHOWNORMAL;
695 desc.icon=mypath;
696 desc.icon_id=0;
697 desc.hotkey=0x1234;
698 create_lnk(lnkfile, &desc, 0);
699 desc.path = realpath;
700 check_lnk(lnkfile, &desc, 0x4);
702 /* Overwrite the existing lnk file and test link to a command on the path */
703 desc.description="command on path";
704 desc.workdir=mypath;
705 desc.path="rundll32.exe";
706 desc.pidl=NULL;
707 desc.arguments="/option1 /option2 \"Some string\"";
708 desc.showcmd=SW_SHOWNORMAL;
709 desc.icon=mypath;
710 desc.icon_id=0;
711 desc.hotkey=0x1234;
712 create_lnk(lnkfile, &desc, 0);
713 /* Check that link is created to proper location */
714 SearchPathA( NULL, desc.path, NULL, MAX_PATH, realpath, NULL);
715 desc.path=realpath;
716 check_lnk(lnkfile, &desc, 0x0);
718 /* Test omitting .exe from a command on the path */
719 desc.description="command on path without .exe";
720 desc.workdir=mypath;
721 desc.path="rundll32";
722 desc.pidl=NULL;
723 desc.arguments="/option1 /option2 \"Some string\"";
724 desc.showcmd=SW_SHOWNORMAL;
725 desc.icon=mypath;
726 desc.icon_id=0;
727 desc.hotkey=0x1234;
728 create_lnk(lnkfile, &desc, 0);
729 /* Check that link is created to proper location */
730 SearchPathA( NULL, "rundll32", NULL, MAX_PATH, realpath, NULL);
731 desc.path=realpath;
732 check_lnk(lnkfile, &desc, 0x4);
734 /* Create a temporary non-executable file */
735 r=GetTempPath(sizeof(mypath), mypath);
736 ok(r<sizeof(mypath), "GetTempPath failed (%d), err %d\n", r, GetLastError());
737 r=pGetLongPathNameA(mypath, mydir, sizeof(mydir));
738 ok(r<sizeof(mydir), "GetLongPathName failed (%d), err %d\n", r, GetLastError());
739 p=strrchr(mydir, '\\');
740 if (p)
741 *p='\0';
743 strcpy(mypath, mydir);
744 strcat(mypath, "\\test.txt");
745 hf = CreateFile(mypath, GENERIC_WRITE, 0, NULL,
746 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
747 CloseHandle(hf);
749 /* Overwrite the existing lnk file and test link to an existing non-executable file */
750 desc.description="non-executable file";
751 desc.workdir=mydir;
752 desc.path=mypath;
753 desc.pidl=NULL;
754 desc.arguments="";
755 desc.showcmd=SW_SHOWNORMAL;
756 desc.icon=mypath;
757 desc.icon_id=0;
758 desc.hotkey=0x1234;
759 create_lnk(lnkfile, &desc, 0);
760 check_lnk(lnkfile, &desc, 0x0);
762 r=pGetShortPathNameA(mydir, mypath, sizeof(mypath));
763 strcpy(realpath, mypath);
764 strcat(realpath, "\\test.txt");
765 strcat(mypath, "\\\\test.txt");
767 /* Overwrite the existing lnk file and test link to a short path with double backslashes */
768 desc.description="non-executable file";
769 desc.workdir=mydir;
770 desc.path=mypath;
771 desc.pidl=NULL;
772 desc.arguments="";
773 desc.showcmd=SW_SHOWNORMAL;
774 desc.icon=mypath;
775 desc.icon_id=0;
776 desc.hotkey=0x1234;
777 create_lnk(lnkfile, &desc, 0);
778 desc.path=realpath;
779 check_lnk(lnkfile, &desc, 0x0);
781 r = DeleteFileA(mypath);
782 ok(r, "failed to delete file %s (%d)\n", mypath, GetLastError());
784 /* Create a temporary .bat file */
785 strcpy(mypath, mydir);
786 strcat(mypath, "\\test.bat");
787 hf = CreateFile(mypath, GENERIC_WRITE, 0, NULL,
788 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
789 CloseHandle(hf);
791 strcpy(realpath, mypath);
793 p=strrchr(mypath, '.');
794 if (p)
795 *p='\0';
797 /* Try linking to the .bat file without the extension */
798 desc.description="batch file";
799 desc.workdir=mydir;
800 desc.path=mypath;
801 desc.pidl=NULL;
802 desc.arguments="";
803 desc.showcmd=SW_SHOWNORMAL;
804 desc.icon=mypath;
805 desc.icon_id=0;
806 desc.hotkey=0x1234;
807 create_lnk(lnkfile, &desc, 0);
808 desc.path = realpath;
809 check_lnk(lnkfile, &desc, 0x4);
811 r = DeleteFileA(realpath);
813 /* FIXME: Also test saving a .lnk pointing to a pidl that cannot be
814 * represented as a path.
817 /* DeleteFileW is not implemented on Win9x */
818 r=DeleteFileA(lnkfileA);
819 ok(r, "failed to delete link '%s' (%d)\n", lnkfileA, GetLastError());
822 static void test_datalink(void)
824 static const WCHAR lnk[] = {
825 ':',':','{','9','d','b','1','1','8','6','e','-','4','0','d','f','-','1',
826 '1','d','1','-','a','a','8','c','-','0','0','c','0','4','f','b','6','7',
827 '8','6','3','}',':','2','6',',','!','!','g','x','s','f','(','N','g',']',
828 'q','F','`','H','{','L','s','A','C','C','E','S','S','F','i','l','e','s',
829 '>','p','l','T',']','j','I','{','j','f','(','=','1','&','L','[','-','8',
830 '1','-',']',':',':',0 };
831 static const WCHAR comp[] = {
832 '2','6',',','!','!','g','x','s','f','(','N','g',']','q','F','`','H','{',
833 'L','s','A','C','C','E','S','S','F','i','l','e','s','>','p','l','T',']',
834 'j','I','{','j','f','(','=','1','&','L','[','-','8','1','-',']',0 };
835 IShellLinkDataList *dl = NULL;
836 IShellLinkW *sl = NULL;
837 HRESULT r;
838 DWORD flags = 0;
839 EXP_DARWIN_LINK *dar;
841 r = CoCreateInstance( &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
842 &IID_IShellLinkW, (LPVOID*)&sl );
843 ok( r == S_OK ||
844 broken(r == E_NOINTERFACE), /* Win9x */
845 "CoCreateInstance failed (0x%08x)\n", r);
846 if (!sl)
848 win_skip("no shelllink\n");
849 return;
852 r = IShellLinkW_QueryInterface( sl, &_IID_IShellLinkDataList, (LPVOID*) &dl );
853 ok( r == S_OK ||
854 broken(r == E_NOINTERFACE), /* NT4 */
855 "IShellLinkW_QueryInterface failed (0x%08x)\n", r);
857 if (!dl)
859 win_skip("no datalink interface\n");
860 IShellLinkW_Release( sl );
861 return;
864 flags = 0;
865 r = IShellLinkDataList_GetFlags( dl, &flags );
866 ok( r == S_OK, "GetFlags failed\n");
867 ok( flags == 0, "GetFlags returned wrong flags\n");
869 dar = (void*)-1;
870 r = IShellLinkDataList_CopyDataBlock( dl, EXP_DARWIN_ID_SIG, (LPVOID*) &dar );
871 ok( r == E_FAIL, "CopyDataBlock failed\n");
872 ok( dar == NULL, "should be null\n");
874 if (!pGetLongPathNameA /* NT4 */)
875 skip("SetPath with NULL parameter crashes on NT4\n");
876 else
878 r = IShellLinkW_SetPath(sl, NULL);
879 ok(r == E_INVALIDARG, "SetPath returned wrong error (0x%08x)\n", r);
882 r = IShellLinkW_SetPath(sl, lnk);
883 ok(r == S_OK, "SetPath failed\n");
885 if (0)
887 /* the following crashes */
888 r = IShellLinkDataList_GetFlags( dl, NULL );
891 flags = 0;
892 r = IShellLinkDataList_GetFlags( dl, &flags );
893 ok( r == S_OK, "GetFlags failed\n");
894 /* SLDF_HAS_LOGO3ID is no longer supported on Vista+, filter it out */
895 ok( (flags & (~ SLDF_HAS_LOGO3ID)) == SLDF_HAS_DARWINID,
896 "GetFlags returned wrong flags\n");
898 dar = NULL;
899 r = IShellLinkDataList_CopyDataBlock( dl, EXP_DARWIN_ID_SIG, (LPVOID*) &dar );
900 ok( r == S_OK, "CopyDataBlock failed\n");
902 ok( dar && ((DATABLOCK_HEADER*)dar)->dwSignature == EXP_DARWIN_ID_SIG, "signature wrong\n");
903 ok( dar && 0==lstrcmpW(dar->szwDarwinID, comp ), "signature wrong\n");
905 LocalFree( dar );
907 IUnknown_Release( dl );
908 IShellLinkW_Release( sl );
911 static void test_shdefextracticon(void)
913 HICON hiconlarge=NULL, hiconsmall=NULL;
914 HRESULT res;
916 if (!pSHDefExtractIconA)
918 win_skip("SHDefExtractIconA is unavailable\n");
919 return;
922 res = pSHDefExtractIconA("shell32.dll", 0, 0, &hiconlarge, &hiconsmall, MAKELONG(16,24));
923 ok(SUCCEEDED(res), "SHDefExtractIconA failed, res=%x\n", res);
924 ok(hiconlarge != NULL, "got null hiconlarge\n");
925 ok(hiconsmall != NULL, "got null hiconsmall\n");
926 DestroyIcon(hiconlarge);
927 DestroyIcon(hiconsmall);
929 hiconsmall = NULL;
930 res = pSHDefExtractIconA("shell32.dll", 0, 0, NULL, &hiconsmall, MAKELONG(16,24));
931 ok(SUCCEEDED(res), "SHDefExtractIconA failed, res=%x\n", res);
932 ok(hiconsmall != NULL, "got null hiconsmall\n");
933 DestroyIcon(hiconsmall);
935 res = pSHDefExtractIconA("shell32.dll", 0, 0, NULL, NULL, MAKELONG(16,24));
936 ok(SUCCEEDED(res), "SHDefExtractIconA failed, res=%x\n", res);
939 static void test_GetIconLocation(void)
941 IShellLinkA *sl;
942 const char *str;
943 char buffer[INFOTIPSIZE], mypath[MAX_PATH];
944 int i;
945 HRESULT r;
946 LPITEMIDLIST pidl;
948 r = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
949 &IID_IShellLinkA, (LPVOID*)&sl);
950 ok(r == S_OK, "no IID_IShellLinkA (0x%08x)\n", r);
951 if(r != S_OK)
952 return;
954 i = 0xdeadbeef;
955 strcpy(buffer, "garbage");
956 r = IShellLinkA_GetIconLocation(sl, buffer, sizeof(buffer), &i);
957 ok(r == S_OK, "GetIconLocation failed (0x%08x)\n", r);
958 ok(*buffer == '\0', "GetIconLocation returned '%s'\n", buffer);
959 ok(i == 0, "GetIconLocation returned %d\n", i);
961 str = "c:\\some\\path";
962 r = IShellLinkA_SetPath(sl, str);
963 ok(r == S_FALSE || r == S_OK, "SetPath failed (0x%08x)\n", r);
965 i = 0xdeadbeef;
966 strcpy(buffer, "garbage");
967 r = IShellLinkA_GetIconLocation(sl, buffer, sizeof(buffer), &i);
968 ok(r == S_OK, "GetIconLocation failed (0x%08x)\n", r);
969 ok(*buffer == '\0', "GetIconLocation returned '%s'\n", buffer);
970 ok(i == 0, "GetIconLocation returned %d\n", i);
972 GetWindowsDirectoryA(mypath, sizeof(mypath) - 12);
973 strcat(mypath, "\\regedit.exe");
974 pidl = path_to_pidl(mypath);
975 r = IShellLinkA_SetIDList(sl, pidl);
976 ok(r == S_OK, "SetPath failed (0x%08x)\n", r);
977 pILFree(pidl);
979 i = 0xdeadbeef;
980 strcpy(buffer, "garbage");
981 r = IShellLinkA_GetIconLocation(sl, buffer, sizeof(buffer), &i);
982 ok(r == S_OK, "GetIconLocation failed (0x%08x)\n", r);
983 ok(*buffer == '\0', "GetIconLocation returned '%s'\n", buffer);
984 ok(i == 0, "GetIconLocation returned %d\n", i);
986 str = "c:\\nonexistent\\file";
987 r = IShellLinkA_SetIconLocation(sl, str, 0xbabecafe);
988 ok(r == S_OK, "SetIconLocation failed (0x%08x)\n", r);
990 i = 0xdeadbeef;
991 r = IShellLinkA_GetIconLocation(sl, buffer, sizeof(buffer), &i);
992 ok(r == S_OK, "GetIconLocation failed (0x%08x)\n", r);
993 ok(lstrcmpi(buffer,str) == 0, "GetIconLocation returned '%s'\n", buffer);
994 ok(i == 0xbabecafe, "GetIconLocation returned %d'\n", i);
996 IShellLinkA_Release(sl);
999 START_TEST(shelllink)
1001 HRESULT r;
1002 HMODULE hmod = GetModuleHandleA("shell32.dll");
1003 HMODULE hkernel32 = GetModuleHandleA("kernel32.dll");
1005 pILFree = (fnILFree) GetProcAddress(hmod, (LPSTR)155);
1006 pILIsEqual = (fnILIsEqual) GetProcAddress(hmod, (LPSTR)21);
1007 pSHILCreateFromPath = (fnSHILCreateFromPath) GetProcAddress(hmod, (LPSTR)28);
1008 pSHDefExtractIconA = (fnSHDefExtractIconA) GetProcAddress(hmod, "SHDefExtractIconA");
1010 pGetLongPathNameA = (void *)GetProcAddress(hkernel32, "GetLongPathNameA");
1011 pGetShortPathNameA = (void *)GetProcAddress(hkernel32, "GetShortPathNameA");
1013 r = CoInitialize(NULL);
1014 ok(r == S_OK, "CoInitialize failed (0x%08x)\n", r);
1015 if (r != S_OK)
1016 return;
1018 test_get_set();
1019 test_load_save();
1020 test_datalink();
1021 test_shdefextracticon();
1022 test_GetIconLocation();
1024 CoUninitialize();