netapi32: Convert the Unix library to the __wine_unix_call interface.
[wine.git] / dlls / shell32 / tests / shelllink.c
blobedd536c3a01d7098c6541100426c6f7fa6491dd7
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
22 #define COBJMACROS
24 #include "initguid.h"
25 #include "windows.h"
26 #include "shlguid.h"
27 #include "shobjidl.h"
28 #include "shlobj.h"
29 #include "shellapi.h"
30 #include "commoncontrols.h"
32 #include "wine/heap.h"
33 #include "wine/test.h"
35 #include "shell32_test.h"
37 static HRESULT (WINAPI *pSHILCreateFromPath)(LPCWSTR, LPITEMIDLIST *,DWORD*);
38 static HRESULT (WINAPI *pSHGetFolderLocation)(HWND,INT,HANDLE,DWORD,PIDLIST_ABSOLUTE*);
39 static HRESULT (WINAPI *pSHGetStockIconInfo)(SHSTOCKICONID, UINT, SHSTOCKICONINFO *);
40 static UINT (WINAPI *pSHExtractIconsW)(LPCWSTR, int, int, int, HICON *, UINT *, UINT, UINT);
41 static BOOL (WINAPI *pIsProcessDPIAware)(void);
43 /* For some reason SHILCreateFromPath does not work on Win98 and
44 * SHSimpleIDListFromPathA does not work on NT4. But if we call both we
45 * get what we want on all platforms.
47 static LPITEMIDLIST (WINAPI *pSHSimpleIDListFromPathAW)(LPCVOID);
49 static LPITEMIDLIST path_to_pidl(const char* path)
51 LPITEMIDLIST pidl;
53 if (!pSHSimpleIDListFromPathAW)
55 HMODULE hdll=GetModuleHandleA("shell32.dll");
56 pSHSimpleIDListFromPathAW=(void*)GetProcAddress(hdll, (char*)162);
57 if (!pSHSimpleIDListFromPathAW)
58 win_skip("SHSimpleIDListFromPathAW not found in shell32.dll\n");
61 pidl=NULL;
62 /* pSHSimpleIDListFromPathAW maps to A on non NT platforms */
63 if (pSHSimpleIDListFromPathAW && (GetVersion() & 0x80000000))
64 pidl=pSHSimpleIDListFromPathAW(path);
66 if (!pidl)
68 WCHAR* pathW;
69 HRESULT r;
70 int len;
72 len=MultiByteToWideChar(CP_ACP, 0, path, -1, NULL, 0);
73 pathW = heap_alloc(len * sizeof(WCHAR));
74 MultiByteToWideChar(CP_ACP, 0, path, -1, pathW, len);
76 r=pSHILCreateFromPath(pathW, &pidl, NULL);
77 ok(r == S_OK, "SHILCreateFromPath failed (0x%08x)\n", r);
78 heap_free(pathW);
80 return pidl;
85 * Test manipulation of an IShellLink's properties.
88 static void test_get_set(void)
90 HRESULT r;
91 IShellLinkA *sl;
92 IShellLinkW *slW = NULL;
93 char mypath[MAX_PATH];
94 char buffer[INFOTIPSIZE];
95 WIN32_FIND_DATAA finddata;
96 LPITEMIDLIST pidl, tmp_pidl;
97 const char * str;
98 int i;
99 WORD w;
101 r = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
102 &IID_IShellLinkA, (LPVOID*)&sl);
103 ok(r == S_OK, "no IID_IShellLinkA (0x%08x)\n", r);
104 if (r != S_OK)
105 return;
107 /* Test Getting / Setting the description */
108 strcpy(buffer,"garbage");
109 r = IShellLinkA_GetDescription(sl, buffer, sizeof(buffer));
110 ok(r == S_OK, "GetDescription failed (0x%08x)\n", r);
111 ok(*buffer=='\0', "GetDescription returned '%s'\n", buffer);
113 str="Some description";
114 r = IShellLinkA_SetDescription(sl, str);
115 ok(r == S_OK, "SetDescription failed (0x%08x)\n", r);
117 strcpy(buffer,"garbage");
118 r = IShellLinkA_GetDescription(sl, buffer, sizeof(buffer));
119 ok(r == S_OK, "GetDescription failed (0x%08x)\n", r);
120 ok(strcmp(buffer,str)==0, "GetDescription returned '%s'\n", buffer);
122 r = IShellLinkA_SetDescription(sl, NULL);
123 ok(r == S_OK, "SetDescription failed (0x%08x)\n", r);
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, "GetDescription returned '%s'\n", buffer);
130 /* Test Getting / Setting the work directory */
131 strcpy(buffer,"garbage");
132 r = IShellLinkA_GetWorkingDirectory(sl, buffer, sizeof(buffer));
133 ok(r == S_OK, "GetWorkingDirectory failed (0x%08x)\n", r);
134 ok(*buffer=='\0', "GetWorkingDirectory returned '%s'\n", buffer);
136 str="c:\\nonexistent\\directory";
137 r = IShellLinkA_SetWorkingDirectory(sl, str);
138 ok(r == S_OK, "SetWorkingDirectory failed (0x%08x)\n", r);
140 strcpy(buffer,"garbage");
141 r = IShellLinkA_GetWorkingDirectory(sl, buffer, sizeof(buffer));
142 ok(r == S_OK, "GetWorkingDirectory failed (0x%08x)\n", r);
143 ok(lstrcmpiA(buffer,str)==0, "GetWorkingDirectory returned '%s'\n", buffer);
145 /* Test Getting / Setting the path */
146 strcpy(buffer,"garbage");
147 r = IShellLinkA_GetPath(sl, buffer, sizeof(buffer), NULL, SLGP_RAWPATH);
148 ok(r == S_FALSE, "GetPath failed (0x%08x)\n", r);
149 ok(!*buffer, "GetPath returned '%s'\n", buffer);
151 strcpy(buffer,"garbage");
152 memset(&finddata, 0xaa, sizeof(finddata));
153 r = IShellLinkA_GetPath(sl, buffer, sizeof(buffer), &finddata, SLGP_RAWPATH);
154 ok(r == S_FALSE, "GetPath failed (0x%08x)\n", r);
155 ok(!*buffer, "GetPath returned '%s'\n", buffer);
156 ok(finddata.dwFileAttributes == 0, "unexpected attributes %x\n", finddata.dwFileAttributes);
157 ok(finddata.cFileName[0] == 0, "unexpected filename '%s'\n", finddata.cFileName);
159 r = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, &IID_IShellLinkW, (void **)&slW);
160 ok(r == S_OK, "CoCreateInstance failed (0x%08x)\n", r);
161 IShellLinkW_Release(slW);
163 r = IShellLinkA_SetPath(sl, NULL);
164 ok(r == E_INVALIDARG, "Unexpected hr %#x.\n", r);
166 r = IShellLinkA_SetPath(sl, "");
167 ok(r==S_OK, "SetPath failed (0x%08x)\n", r);
169 strcpy(buffer,"garbage");
170 r = IShellLinkA_GetPath(sl, buffer, sizeof(buffer), NULL, SLGP_RAWPATH);
171 ok(r == S_FALSE, "GetPath failed (0x%08x)\n", r);
172 ok(*buffer=='\0', "GetPath returned '%s'\n", buffer);
174 /* Win98 returns S_FALSE, but WinXP returns S_OK */
175 str="c:\\nonexistent\\file";
176 r = IShellLinkA_SetPath(sl, str);
177 ok(r==S_FALSE || r==S_OK, "SetPath failed (0x%08x)\n", r);
179 strcpy(buffer,"garbage");
180 r = IShellLinkA_GetPath(sl, buffer, sizeof(buffer), NULL, SLGP_RAWPATH);
181 ok(r == S_OK, "GetPath failed (0x%08x)\n", r);
182 ok(lstrcmpiA(buffer,str)==0, "GetPath returned '%s'\n", buffer);
184 strcpy(buffer,"garbage");
185 memset(&finddata, 0xaa, sizeof(finddata));
186 r = IShellLinkA_GetPath(sl, buffer, sizeof(buffer), &finddata, SLGP_RAWPATH);
187 ok(r == S_OK, "GetPath failed (0x%08x)\n", r);
188 ok(lstrcmpiA(buffer,str)==0, "GetPath returned '%s'\n", buffer);
189 ok(finddata.dwFileAttributes == 0, "unexpected attributes %x\n", finddata.dwFileAttributes);
190 ok(lstrcmpiA(finddata.cFileName, "file") == 0, "unexpected filename '%s'\n", finddata.cFileName);
192 /* Get some real path to play with */
193 GetWindowsDirectoryA( mypath, sizeof(mypath)-12 );
194 strcat(mypath, "\\regedit.exe");
196 /* Test the interaction of SetPath and SetIDList */
197 tmp_pidl=NULL;
198 r = IShellLinkA_GetIDList(sl, &tmp_pidl);
199 ok(r == S_OK, "GetIDList failed (0x%08x)\n", r);
200 if (r == S_OK)
202 BOOL ret;
204 strcpy(buffer,"garbage");
205 ret = SHGetPathFromIDListA(tmp_pidl, buffer);
206 ok(ret, "SHGetPathFromIDListA failed\n");
207 if (ret)
208 ok(lstrcmpiA(buffer,str)==0, "GetIDList returned '%s'\n", buffer);
209 ILFree(tmp_pidl);
212 pidl=path_to_pidl(mypath);
213 ok(pidl!=NULL, "path_to_pidl returned a NULL pidl\n");
215 if (pidl)
217 LPITEMIDLIST second_pidl;
219 r = IShellLinkA_SetIDList(sl, pidl);
220 ok(r == S_OK, "SetIDList failed (0x%08x)\n", r);
222 tmp_pidl=NULL;
223 r = IShellLinkA_GetIDList(sl, &tmp_pidl);
224 ok(r == S_OK, "GetIDList failed (0x%08x)\n", r);
225 ok(tmp_pidl && ILIsEqual(pidl, tmp_pidl),
226 "GetIDList returned an incorrect pidl\n");
228 r = IShellLinkA_GetIDList(sl, &second_pidl);
229 ok(r == S_OK, "GetIDList failed (0x%08x)\n", r);
230 ok(second_pidl && ILIsEqual(pidl, second_pidl),
231 "GetIDList returned an incorrect pidl\n");
232 ok(second_pidl != tmp_pidl, "pidls are the same\n");
234 ILFree(second_pidl);
235 ILFree(tmp_pidl);
236 ILFree(pidl);
238 strcpy(buffer,"garbage");
239 r = IShellLinkA_GetPath(sl, buffer, sizeof(buffer), NULL, SLGP_RAWPATH);
240 ok(r == S_OK, "GetPath failed (0x%08x)\n", r);
241 ok(lstrcmpiA(buffer, mypath)==0, "GetPath returned '%s'\n", buffer);
243 strcpy(buffer,"garbage");
244 memset(&finddata, 0xaa, sizeof(finddata));
245 r = IShellLinkA_GetPath(sl, buffer, sizeof(buffer), &finddata, SLGP_RAWPATH);
246 ok(r == S_OK, "GetPath failed (0x%08x)\n", r);
247 ok(lstrcmpiA(buffer, mypath)==0, "GetPath returned '%s'\n", buffer);
248 ok(finddata.dwFileAttributes != 0, "unexpected attributes %x\n", finddata.dwFileAttributes);
249 ok(lstrcmpiA(finddata.cFileName, "regedit.exe") == 0, "unexpected filename '%s'\n", finddata.cFileName);
252 if (pSHGetFolderLocation)
254 LPITEMIDLIST pidl_controls;
256 r = pSHGetFolderLocation(NULL, CSIDL_CONTROLS, NULL, 0, &pidl_controls);
257 ok(r == S_OK, "SHGetFolderLocation failed (0x%08x)\n", r);
259 r = IShellLinkA_SetIDList(sl, pidl_controls);
260 ok(r == S_OK, "SetIDList failed (0x%08x)\n", r);
262 strcpy(buffer,"garbage");
263 r = IShellLinkA_GetPath(sl, buffer, sizeof(buffer), NULL, SLGP_RAWPATH);
264 ok(r == S_FALSE, "GetPath failed (0x%08x)\n", r);
265 ok(buffer[0] == 0, "GetPath returned '%s'\n", buffer);
267 strcpy(buffer,"garbage");
268 memset(&finddata, 0xaa, sizeof(finddata));
269 r = IShellLinkA_GetPath(sl, buffer, sizeof(buffer), &finddata, SLGP_RAWPATH);
270 ok(r == S_FALSE, "GetPath failed (0x%08x)\n", r);
271 ok(buffer[0] == 0, "GetPath returned '%s'\n", buffer);
272 ok(finddata.dwFileAttributes == 0, "unexpected attributes %x\n", finddata.dwFileAttributes);
273 ok(finddata.cFileName[0] == 0, "unexpected filename '%s'\n", finddata.cFileName);
275 ILFree(pidl_controls);
278 /* test path with quotes (IShellLinkA_SetPath returns S_FALSE on W2K and below and S_OK on XP and above */
279 r = IShellLinkA_SetPath(sl, "\"c:\\nonexistent\\file\"");
280 ok(r==S_FALSE || r == S_OK, "SetPath failed (0x%08x)\n", r);
282 strcpy(buffer,"garbage");
283 r = IShellLinkA_GetPath(sl, buffer, sizeof(buffer), NULL, SLGP_RAWPATH);
284 ok(r==S_OK, "GetPath failed (0x%08x)\n", r);
285 todo_wine ok(!strcmp(buffer, "C:\\nonexistent\\file"),
286 "case doesn't match\n");
288 r = IShellLinkA_SetPath(sl, "\"c:\\foo");
289 ok(r==S_FALSE || r == S_OK || r == E_INVALIDARG /* Vista */, "SetPath failed (0x%08x)\n", r);
291 r = IShellLinkA_SetPath(sl, "\"\"c:\\foo");
292 ok(r==S_FALSE || r == S_OK || r == E_INVALIDARG /* Vista */, "SetPath failed (0x%08x)\n", r);
294 r = IShellLinkA_SetPath(sl, "c:\\foo\"");
295 ok(r==S_FALSE || r == S_OK || r == E_INVALIDARG /* Vista */, "SetPath failed (0x%08x)\n", r);
297 r = IShellLinkA_SetPath(sl, "\"\"c:\\foo\"");
298 ok(r==S_FALSE || r == S_OK || r == E_INVALIDARG /* Vista */, "SetPath failed (0x%08x)\n", r);
300 r = IShellLinkA_SetPath(sl, "\"\"c:\\foo\"\"");
301 ok(r==S_FALSE || r == S_OK || r == E_INVALIDARG /* Vista */, "SetPath failed (0x%08x)\n", r);
303 /* Test Getting / Setting the arguments */
304 strcpy(buffer,"garbage");
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 str="param1 \"spaced param2\"";
310 r = IShellLinkA_SetArguments(sl, str);
311 ok(r == S_OK, "SetArguments failed (0x%08x)\n", r);
313 strcpy(buffer,"garbage");
314 r = IShellLinkA_GetArguments(sl, buffer, sizeof(buffer));
315 ok(r == S_OK, "GetArguments failed (0x%08x)\n", r);
316 ok(strcmp(buffer,str)==0, "GetArguments returned '%s'\n", buffer);
318 strcpy(buffer,"garbage");
319 r = IShellLinkA_SetArguments(sl, NULL);
320 ok(r == S_OK, "SetArguments failed (0x%08x)\n", r);
321 r = IShellLinkA_GetArguments(sl, buffer, sizeof(buffer));
322 ok(r == S_OK, "GetArguments failed (0x%08x)\n", r);
323 ok(!buffer[0] || strcmp(buffer,str)==0, "GetArguments returned '%s'\n", buffer);
325 strcpy(buffer,"garbage");
326 r = IShellLinkA_SetArguments(sl, "");
327 ok(r == S_OK, "SetArguments failed (0x%08x)\n", r);
328 r = IShellLinkA_GetArguments(sl, buffer, sizeof(buffer));
329 ok(r == S_OK, "GetArguments failed (0x%08x)\n", r);
330 ok(!buffer[0], "GetArguments returned '%s'\n", buffer);
332 /* Test Getting / Setting showcmd */
333 i=0xdeadbeef;
334 r = IShellLinkA_GetShowCmd(sl, &i);
335 ok(r == S_OK, "GetShowCmd failed (0x%08x)\n", r);
336 ok(i==SW_SHOWNORMAL, "GetShowCmd returned %d\n", i);
338 r = IShellLinkA_SetShowCmd(sl, SW_SHOWMAXIMIZED);
339 ok(r == S_OK, "SetShowCmd failed (0x%08x)\n", r);
341 i=0xdeadbeef;
342 r = IShellLinkA_GetShowCmd(sl, &i);
343 ok(r == S_OK, "GetShowCmd failed (0x%08x)\n", r);
344 ok(i==SW_SHOWMAXIMIZED, "GetShowCmd returned %d'\n", i);
346 /* Test Getting / Setting the icon */
347 i=0xdeadbeef;
348 strcpy(buffer,"garbage");
349 r = IShellLinkA_GetIconLocation(sl, buffer, sizeof(buffer), &i);
350 ok(r == S_OK, "GetIconLocation failed (0x%08x)\n", r);
351 ok(*buffer=='\0', "GetIconLocation returned '%s'\n", buffer);
352 ok(i==0, "GetIconLocation returned %d\n", i);
354 str="c:\\nonexistent\\file";
355 r = IShellLinkA_SetIconLocation(sl, str, 0xbabecafe);
356 ok(r == S_OK, "SetIconLocation failed (0x%08x)\n", r);
358 i=0xdeadbeef;
359 r = IShellLinkA_GetIconLocation(sl, buffer, sizeof(buffer), &i);
360 ok(r == S_OK, "GetIconLocation failed (0x%08x)\n", r);
361 ok(lstrcmpiA(buffer,str)==0, "GetIconLocation returned '%s'\n", buffer);
362 ok(i==0xbabecafe, "GetIconLocation returned %d'\n", i);
364 /* Test Getting / Setting the hot key */
365 w=0xbeef;
366 r = IShellLinkA_GetHotkey(sl, &w);
367 ok(r == S_OK, "GetHotkey failed (0x%08x)\n", r);
368 ok(w==0, "GetHotkey returned %d\n", w);
370 r = IShellLinkA_SetHotkey(sl, 0x5678);
371 ok(r == S_OK, "SetHotkey failed (0x%08x)\n", r);
373 w=0xbeef;
374 r = IShellLinkA_GetHotkey(sl, &w);
375 ok(r == S_OK, "GetHotkey failed (0x%08x)\n", r);
376 ok(w==0x5678, "GetHotkey returned %d'\n", w);
378 IShellLinkA_Release(sl);
383 * Test saving and loading .lnk files
386 #define lok ok_(__FILE__, line)
387 #define check_lnk(a,b,c) check_lnk_(__LINE__, (a), (b), (c))
389 void create_lnk_(int line, const WCHAR* path, lnk_desc_t* desc)
391 HRESULT r, init_dirty;
392 IShellLinkA *sl;
393 IPersistFile *pf;
395 r = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
396 &IID_IShellLinkA, (LPVOID*)&sl);
397 lok(r == S_OK, "no IID_IShellLinkA (0x%08x)\n", r);
398 if (r != S_OK)
399 return;
401 if (desc->description)
403 r = IShellLinkA_SetDescription(sl, desc->description);
404 lok(r == S_OK, "SetDescription failed (0x%08x)\n", r);
406 if (desc->workdir)
408 r = IShellLinkA_SetWorkingDirectory(sl, desc->workdir);
409 lok(r == S_OK, "SetWorkingDirectory failed (0x%08x)\n", r);
411 if (desc->path)
413 r = IShellLinkA_SetPath(sl, desc->path);
414 lok(SUCCEEDED(r), "SetPath failed (0x%08x)\n", r);
416 if (desc->pidl)
418 r = IShellLinkA_SetIDList(sl, desc->pidl);
419 lok(r == S_OK, "SetIDList failed (0x%08x)\n", r);
421 if (desc->arguments)
423 r = IShellLinkA_SetArguments(sl, desc->arguments);
424 lok(r == S_OK, "SetArguments failed (0x%08x)\n", r);
426 if (desc->showcmd)
428 r = IShellLinkA_SetShowCmd(sl, desc->showcmd);
429 lok(r == S_OK, "SetShowCmd failed (0x%08x)\n", r);
431 if (desc->icon)
433 r = IShellLinkA_SetIconLocation(sl, desc->icon, desc->icon_id);
434 lok(r == S_OK, "SetIconLocation failed (0x%08x)\n", r);
436 if (desc->hotkey)
438 r = IShellLinkA_SetHotkey(sl, desc->hotkey);
439 lok(r == S_OK, "SetHotkey failed (0x%08x)\n", r);
442 r = IShellLinkA_QueryInterface(sl, &IID_IPersistFile, (void**)&pf);
443 lok(r == S_OK, "no IID_IPersistFile (0x%08x)\n", r);
444 if (r == S_OK)
446 LPOLESTR str;
448 if (0)
450 /* crashes on XP */
451 IPersistFile_GetCurFile(pf, NULL);
454 init_dirty = IPersistFile_IsDirty(pf); /* empty links start off as clean */
455 r = IPersistFile_Save(pf, NULL, FALSE);
456 lok(r == S_OK || r == E_INVALIDARG /* before Windows 7 */, "save failed (0x%08x)\n", r);
457 r = IPersistFile_IsDirty(pf);
458 lok(r == init_dirty, "dirty (0x%08x)\n", r);
460 r = IPersistFile_Save(pf, NULL, TRUE);
461 lok(r == S_OK || r == E_INVALIDARG /* before Windows 7 */, "save failed (0x%08x)\n", r);
462 r = IPersistFile_IsDirty(pf);
463 lok(r == init_dirty, "dirty (0x%08x)\n", r);
465 /* test GetCurFile before ::Save */
466 str = (LPWSTR)0xdeadbeef;
467 r = IPersistFile_GetCurFile(pf, &str);
468 lok(r == S_FALSE, "got 0x%08x\n", r);
469 lok(str == NULL, "got %p\n", str);
471 r = IPersistFile_Save(pf, path, TRUE);
472 lok(r == S_OK, "save failed (0x%08x)\n", r);
473 r = IPersistFile_IsDirty(pf);
474 lok(r == S_FALSE, "dirty (0x%08x)\n", r);
476 /* test GetCurFile after ::Save */
477 r = IPersistFile_GetCurFile(pf, &str);
478 lok(r == S_OK, "got 0x%08x\n", r);
479 lok(str != NULL, "Didn't expect NULL\n");
480 lok(!wcscmp(path, str), "Expected %s, got %s\n", wine_dbgstr_w(path), wine_dbgstr_w(str));
481 CoTaskMemFree(str);
483 r = IPersistFile_Save(pf, NULL, TRUE);
484 lok(r == S_OK, "save failed (0x%08x)\n", r);
486 /* test GetCurFile after ::Save */
487 r = IPersistFile_GetCurFile(pf, &str);
488 lok(r == S_OK, "got 0x%08x\n", r);
489 lok(str != NULL, "Didn't expect NULL\n");
490 lok(!wcscmp(path, str), "Expected %s, got %s\n", wine_dbgstr_w(path), wine_dbgstr_w(str));
491 CoTaskMemFree(str);
493 IPersistFile_Release(pf);
496 IShellLinkA_Release(sl);
499 static void check_lnk_(int line, const WCHAR* path, lnk_desc_t* desc, int todo)
501 HRESULT r;
502 IShellLinkA *sl;
503 IPersistFile *pf;
504 char buffer[INFOTIPSIZE];
505 LPOLESTR str;
507 r = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
508 &IID_IShellLinkA, (LPVOID*)&sl);
509 lok(r == S_OK, "no IID_IShellLinkA (0x%08x)\n", r);
510 if (r != S_OK)
511 return;
513 r = IShellLinkA_QueryInterface(sl, &IID_IPersistFile, (LPVOID*)&pf);
514 lok(r == S_OK, "no IID_IPersistFile (0x%08x)\n", r);
515 if (r != S_OK)
517 IShellLinkA_Release(sl);
518 return;
521 /* test GetCurFile before ::Load */
522 str = (LPWSTR)0xdeadbeef;
523 r = IPersistFile_GetCurFile(pf, &str);
524 lok(r == S_FALSE, "got 0x%08x\n", r);
525 lok(str == NULL, "got %p\n", str);
527 r = IPersistFile_Load(pf, path, STGM_READ);
528 lok(r == S_OK, "load failed (0x%08x)\n", r);
530 /* test GetCurFile after ::Save */
531 r = IPersistFile_GetCurFile(pf, &str);
532 lok(r == S_OK, "got 0x%08x\n", r);
533 lok(str != NULL, "Didn't expect NULL\n");
534 lok(!wcscmp(path, str), "Expected %s, got %s\n", wine_dbgstr_w(path), wine_dbgstr_w(str));
535 CoTaskMemFree(str);
537 IPersistFile_Release(pf);
539 if (desc->description)
541 strcpy(buffer,"garbage");
542 r = IShellLinkA_GetDescription(sl, buffer, sizeof(buffer));
543 lok(r == S_OK, "GetDescription failed (0x%08x)\n", r);
544 todo_wine_if ((todo & 0x1) != 0)
545 lok(strcmp(buffer, desc->description)==0, "GetDescription returned '%s' instead of '%s'\n",
546 buffer, desc->description);
548 if (desc->workdir)
550 strcpy(buffer,"garbage");
551 r = IShellLinkA_GetWorkingDirectory(sl, buffer, sizeof(buffer));
552 lok(r == S_OK, "GetWorkingDirectory failed (0x%08x)\n", r);
553 todo_wine_if ((todo & 0x2) != 0)
554 lok(lstrcmpiA(buffer, desc->workdir)==0, "GetWorkingDirectory returned '%s' instead of '%s'\n",
555 buffer, desc->workdir);
557 if (desc->path)
559 strcpy(buffer,"garbage");
560 r = IShellLinkA_GetPath(sl, buffer, sizeof(buffer), NULL, SLGP_RAWPATH);
561 lok(SUCCEEDED(r), "GetPath failed (0x%08x)\n", r);
562 todo_wine_if ((todo & 0x4) != 0)
563 lok(lstrcmpiA(buffer, desc->path)==0, "GetPath returned '%s' instead of '%s'\n",
564 buffer, desc->path);
566 if (desc->pidl)
568 LPITEMIDLIST pidl=NULL;
569 r = IShellLinkA_GetIDList(sl, &pidl);
570 lok(r == S_OK, "GetIDList failed (0x%08x)\n", r);
571 todo_wine_if ((todo & 0x8) != 0)
572 lok(ILIsEqual(pidl, desc->pidl), "GetIDList returned an incorrect pidl\n");
574 if (desc->showcmd)
576 int i=0xdeadbeef;
577 r = IShellLinkA_GetShowCmd(sl, &i);
578 lok(r == S_OK, "GetShowCmd failed (0x%08x)\n", r);
579 todo_wine_if ((todo & 0x10) != 0)
580 lok(i==desc->showcmd, "GetShowCmd returned 0x%0x instead of 0x%0x\n",
581 i, desc->showcmd);
583 if (desc->icon)
585 int i=0xdeadbeef;
586 strcpy(buffer,"garbage");
587 r = IShellLinkA_GetIconLocation(sl, buffer, sizeof(buffer), &i);
588 lok(r == S_OK, "GetIconLocation failed (0x%08x)\n", r);
589 todo_wine_if ((todo & 0x20) != 0) {
590 lok(lstrcmpiA(buffer, desc->icon)==0, "GetIconLocation returned '%s' instead of '%s'\n",
591 buffer, desc->icon);
592 lok(i==desc->icon_id, "GetIconLocation returned 0x%0x instead of 0x%0x\n",
593 i, desc->icon_id);
596 if (desc->hotkey)
598 WORD i=0xbeef;
599 r = IShellLinkA_GetHotkey(sl, &i);
600 lok(r == S_OK, "GetHotkey failed (0x%08x)\n", r);
601 todo_wine_if ((todo & 0x40) != 0)
602 lok(i==desc->hotkey, "GetHotkey returned 0x%04x instead of 0x%04x\n",
603 i, desc->hotkey);
606 IShellLinkA_Release(sl);
609 static void test_load_save(void)
611 WCHAR lnkfile[MAX_PATH];
612 char lnkfileA[MAX_PATH];
613 static const char lnkfileA_name[] = "\\test.lnk";
615 lnk_desc_t desc;
616 char mypath[MAX_PATH];
617 char mydir[MAX_PATH];
618 char realpath[MAX_PATH];
619 char* p;
620 HANDLE hf;
621 DWORD r;
623 /* Don't used a fixed path for the test.lnk file */
624 GetTempPathA(MAX_PATH, lnkfileA);
625 lstrcatA(lnkfileA, lnkfileA_name);
626 MultiByteToWideChar(CP_ACP, 0, lnkfileA, -1, lnkfile, MAX_PATH);
628 /* Save an empty .lnk file */
629 memset(&desc, 0, sizeof(desc));
630 create_lnk(lnkfile, &desc);
632 /* It should come back as a bunch of empty strings */
633 desc.description="";
634 desc.workdir="";
635 desc.path="";
636 desc.arguments="";
637 desc.icon="";
638 check_lnk(lnkfile, &desc, 0x0);
640 /* Point a .lnk file to nonexistent files */
641 desc.description="";
642 desc.workdir="c:\\Nonexitent\\work\\directory";
643 desc.path="c:\\nonexistent\\path";
644 desc.pidl=NULL;
645 desc.arguments="";
646 desc.showcmd=0;
647 desc.icon="c:\\nonexistent\\icon\\file";
648 desc.icon_id=1234;
649 desc.hotkey=0;
650 create_lnk(lnkfile, &desc);
651 check_lnk(lnkfile, &desc, 0x0);
653 r=GetModuleFileNameA(NULL, mypath, sizeof(mypath));
654 ok(r<sizeof(mypath), "GetModuleFileName failed (%d)\n", r);
655 strcpy(mydir, mypath);
656 p=strrchr(mydir, '\\');
657 if (p)
658 *p='\0';
660 /* IShellLink returns path in long form */
661 if (!GetLongPathNameA(mypath, realpath, MAX_PATH))
662 strcpy( realpath, mypath );
664 /* Overwrite the existing lnk file and point it to existing files */
665 desc.description="test 2";
666 desc.workdir=mydir;
667 desc.path=realpath;
668 desc.pidl=NULL;
669 desc.arguments="/option1 /option2 \"Some string\"";
670 desc.showcmd=SW_SHOWNORMAL;
671 desc.icon=mypath;
672 desc.icon_id=0;
673 desc.hotkey=0x1234;
674 create_lnk(lnkfile, &desc);
675 check_lnk(lnkfile, &desc, 0x0);
677 /* Test omitting .exe from an absolute path */
678 p=strrchr(realpath, '.');
679 if (p)
680 *p='\0';
682 desc.description="absolute path without .exe";
683 desc.workdir=mydir;
684 desc.path=realpath;
685 desc.pidl=NULL;
686 desc.arguments="/option1 /option2 \"Some string\"";
687 desc.showcmd=SW_SHOWNORMAL;
688 desc.icon=mypath;
689 desc.icon_id=0;
690 desc.hotkey=0x1234;
691 create_lnk(lnkfile, &desc);
692 strcat(realpath, ".exe");
693 check_lnk(lnkfile, &desc, 0x4);
695 /* Overwrite the existing lnk file and test link to a command on the path */
696 desc.description="command on path";
697 desc.workdir=mypath;
698 desc.path="rundll32.exe";
699 desc.pidl=NULL;
700 desc.arguments="/option1 /option2 \"Some string\"";
701 desc.showcmd=SW_SHOWNORMAL;
702 desc.icon=mypath;
703 desc.icon_id=0;
704 desc.hotkey=0x1234;
705 create_lnk(lnkfile, &desc);
706 /* Check that link is created to proper location */
707 SearchPathA( NULL, desc.path, NULL, MAX_PATH, realpath, NULL);
708 desc.path=realpath;
709 check_lnk(lnkfile, &desc, 0x0);
711 /* Test omitting .exe from a command on the path */
712 desc.description="command on path without .exe";
713 desc.workdir=mypath;
714 desc.path="rundll32";
715 desc.pidl=NULL;
716 desc.arguments="/option1 /option2 \"Some string\"";
717 desc.showcmd=SW_SHOWNORMAL;
718 desc.icon=mypath;
719 desc.icon_id=0;
720 desc.hotkey=0x1234;
721 create_lnk(lnkfile, &desc);
722 /* Check that link is created to proper location */
723 SearchPathA( NULL, "rundll32", NULL, MAX_PATH, realpath, NULL);
724 desc.path=realpath;
725 check_lnk(lnkfile, &desc, 0x4);
727 /* Create a temporary non-executable file */
728 r=GetTempPathA(sizeof(mypath), mypath);
729 ok(r<sizeof(mypath), "GetTempPath failed (%d), err %d\n", r, GetLastError());
730 r = GetLongPathNameA(mypath, mydir, sizeof(mydir));
731 ok(r<sizeof(mydir), "GetLongPathName failed (%d), err %d\n", r, GetLastError());
732 p=strrchr(mydir, '\\');
733 if (p)
734 *p='\0';
736 strcpy(mypath, mydir);
737 strcat(mypath, "\\test.txt");
738 hf = CreateFileA(mypath, GENERIC_WRITE, 0, NULL,
739 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
740 CloseHandle(hf);
742 /* Overwrite the existing lnk file and test link to an existing non-executable file */
743 desc.description="non-executable file";
744 desc.workdir=mydir;
745 desc.path=mypath;
746 desc.pidl=NULL;
747 desc.arguments="";
748 desc.showcmd=SW_SHOWNORMAL;
749 desc.icon=mypath;
750 desc.icon_id=0;
751 desc.hotkey=0x1234;
752 create_lnk(lnkfile, &desc);
753 check_lnk(lnkfile, &desc, 0x0);
755 r = GetShortPathNameA(mydir, mypath, sizeof(mypath));
756 ok(r<sizeof(mypath), "GetShortPathName failed (%d), err %d\n", r, GetLastError());
758 strcpy(realpath, mypath);
759 strcat(realpath, "\\test.txt");
760 strcat(mypath, "\\\\test.txt");
762 /* Overwrite the existing lnk file and test link to a short path with double backslashes */
763 desc.description="non-executable file";
764 desc.workdir=mydir;
765 desc.path=mypath;
766 desc.pidl=NULL;
767 desc.arguments="";
768 desc.showcmd=SW_SHOWNORMAL;
769 desc.icon=mypath;
770 desc.icon_id=0;
771 desc.hotkey=0x1234;
772 create_lnk(lnkfile, &desc);
773 desc.path=realpath;
774 check_lnk(lnkfile, &desc, 0x0);
776 r = DeleteFileA(mypath);
777 ok(r, "failed to delete file %s (%d)\n", mypath, GetLastError());
779 /* Create a temporary .bat file */
780 strcpy(mypath, mydir);
781 strcat(mypath, "\\test.bat");
782 hf = CreateFileA(mypath, GENERIC_WRITE, 0, NULL,
783 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
784 CloseHandle(hf);
786 strcpy(realpath, mypath);
788 p=strrchr(mypath, '.');
789 if (p)
790 *p='\0';
792 /* Try linking to the .bat file without the extension */
793 desc.description="batch file";
794 desc.workdir=mydir;
795 desc.path=mypath;
796 desc.pidl=NULL;
797 desc.arguments="";
798 desc.showcmd=SW_SHOWNORMAL;
799 desc.icon=mypath;
800 desc.icon_id=0;
801 desc.hotkey=0x1234;
802 create_lnk(lnkfile, &desc);
803 desc.path = realpath;
804 check_lnk(lnkfile, &desc, 0x4);
806 r = DeleteFileA(realpath);
807 ok(r, "failed to delete file %s (%d)\n", realpath, GetLastError());
809 /* FIXME: Also test saving a .lnk pointing to a pidl that cannot be
810 * represented as a path.
813 /* DeleteFileW is not implemented on Win9x */
814 r=DeleteFileA(lnkfileA);
815 ok(r, "failed to delete link '%s' (%d)\n", lnkfileA, GetLastError());
818 static void test_datalink(void)
820 static const WCHAR lnk[] = {
821 ':',':','{','9','d','b','1','1','8','6','e','-','4','0','d','f','-','1',
822 '1','d','1','-','a','a','8','c','-','0','0','c','0','4','f','b','6','7',
823 '8','6','3','}',':','2','6',',','!','!','g','x','s','f','(','N','g',']',
824 'q','F','`','H','{','L','s','A','C','C','E','S','S','F','i','l','e','s',
825 '>','p','l','T',']','j','I','{','j','f','(','=','1','&','L','[','-','8',
826 '1','-',']',':',':',0 };
827 static const WCHAR comp[] = {
828 '2','6',',','!','!','g','x','s','f','(','N','g',']','q','F','`','H','{',
829 'L','s','A','C','C','E','S','S','F','i','l','e','s','>','p','l','T',']',
830 'j','I','{','j','f','(','=','1','&','L','[','-','8','1','-',']',0 };
831 IShellLinkDataList *dl = NULL;
832 IShellLinkW *sl = NULL;
833 HRESULT r;
834 DWORD flags = 0;
835 EXP_DARWIN_LINK *dar;
837 r = CoCreateInstance( &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
838 &IID_IShellLinkW, (LPVOID*)&sl );
839 ok( r == S_OK, "Failed to create shelllink object, hr %#x.\n", r);
841 r = IShellLinkW_QueryInterface( sl, &IID_IShellLinkDataList, (void **)&dl );
842 ok( r == S_OK, "Failed to get interface, hr %#x.\n", r);
844 flags = 0;
845 r = IShellLinkDataList_GetFlags( dl, &flags );
846 ok( r == S_OK, "GetFlags failed\n");
847 ok( flags == 0, "GetFlags returned wrong flags\n");
849 dar = (void*)-1;
850 r = IShellLinkDataList_CopyDataBlock( dl, EXP_DARWIN_ID_SIG, (LPVOID*) &dar );
851 ok( r == E_FAIL, "CopyDataBlock failed\n");
852 ok( dar == NULL, "should be null\n");
854 r = IShellLinkW_SetPath(sl, NULL);
855 ok(r == E_INVALIDARG, "Unexpected hr %#x.\n", r);
857 r = IShellLinkW_SetPath(sl, lnk);
858 ok(r == S_OK, "SetPath failed\n");
860 if (0)
862 /* the following crashes */
863 IShellLinkDataList_GetFlags( dl, NULL );
866 flags = 0;
867 r = IShellLinkDataList_GetFlags( dl, &flags );
868 ok( r == S_OK, "GetFlags failed\n");
869 /* SLDF_HAS_LOGO3ID is no longer supported on Vista+, filter it out */
870 ok( (flags & (~ SLDF_HAS_LOGO3ID)) == SLDF_HAS_DARWINID,
871 "GetFlags returned wrong flags\n");
873 dar = NULL;
874 r = IShellLinkDataList_CopyDataBlock( dl, EXP_DARWIN_ID_SIG, (LPVOID*) &dar );
875 ok( r == S_OK, "CopyDataBlock failed\n");
877 ok( dar && ((DATABLOCK_HEADER*)dar)->dwSignature == EXP_DARWIN_ID_SIG, "signature wrong\n");
878 ok( dar && 0==lstrcmpW(dar->szwDarwinID, comp ), "signature wrong\n");
880 LocalFree( dar );
882 IShellLinkDataList_Release( dl );
883 IShellLinkW_Release( sl );
886 static void test_shdefextracticon(void)
888 HICON hiconlarge=NULL, hiconsmall=NULL;
889 HRESULT res;
891 res = SHDefExtractIconA("shell32.dll", 0, 0, &hiconlarge, &hiconsmall, MAKELONG(16,24));
892 ok(SUCCEEDED(res), "SHDefExtractIconA failed, res=%x\n", res);
893 ok(hiconlarge != NULL, "got null hiconlarge\n");
894 ok(hiconsmall != NULL, "got null hiconsmall\n");
895 DestroyIcon(hiconlarge);
896 DestroyIcon(hiconsmall);
898 hiconsmall = NULL;
899 res = SHDefExtractIconA("shell32.dll", 0, 0, NULL, &hiconsmall, MAKELONG(16,24));
900 ok(SUCCEEDED(res), "SHDefExtractIconA failed, res=%x\n", res);
901 ok(hiconsmall != NULL, "got null hiconsmall\n");
902 DestroyIcon(hiconsmall);
904 res = SHDefExtractIconA("shell32.dll", 0, 0, NULL, NULL, MAKELONG(16,24));
905 ok(SUCCEEDED(res), "SHDefExtractIconA failed, res=%x\n", res);
908 static void test_GetIconLocation(void)
910 IShellLinkW *slW;
911 IShellLinkA *sl;
912 const char *str;
913 char buffer[INFOTIPSIZE], mypath[MAX_PATH];
914 int i;
915 HRESULT r;
916 LPITEMIDLIST pidl;
918 r = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
919 &IID_IShellLinkA, (LPVOID*)&sl);
920 ok(r == S_OK, "no IID_IShellLinkA (0x%08x)\n", r);
921 if(r != S_OK)
922 return;
924 i = 0xdeadbeef;
925 strcpy(buffer, "garbage");
926 r = IShellLinkA_GetIconLocation(sl, buffer, sizeof(buffer), &i);
927 ok(r == S_OK, "GetIconLocation failed (0x%08x)\n", r);
928 ok(*buffer == '\0', "GetIconLocation returned '%s'\n", buffer);
929 ok(i == 0, "GetIconLocation returned %d\n", i);
931 str = "c:\\some\\path";
932 r = IShellLinkA_SetPath(sl, str);
933 ok(r == S_FALSE || r == S_OK, "SetPath failed (0x%08x)\n", r);
935 i = 0xdeadbeef;
936 strcpy(buffer, "garbage");
937 r = IShellLinkA_GetIconLocation(sl, buffer, sizeof(buffer), &i);
938 ok(r == S_OK, "GetIconLocation failed (0x%08x)\n", r);
939 ok(*buffer == '\0', "GetIconLocation returned '%s'\n", buffer);
940 ok(i == 0, "GetIconLocation returned %d\n", i);
942 GetWindowsDirectoryA(mypath, sizeof(mypath) - 12);
943 strcat(mypath, "\\regedit.exe");
944 pidl = path_to_pidl(mypath);
945 r = IShellLinkA_SetIDList(sl, pidl);
946 ok(r == S_OK, "SetPath failed (0x%08x)\n", r);
947 ILFree(pidl);
949 i = 0xdeadbeef;
950 strcpy(buffer, "garbage");
951 r = IShellLinkA_GetIconLocation(sl, buffer, sizeof(buffer), &i);
952 ok(r == S_OK, "GetIconLocation failed (0x%08x)\n", r);
953 ok(*buffer == '\0', "GetIconLocation returned '%s'\n", buffer);
954 ok(i == 0, "GetIconLocation returned %d\n", i);
956 str = "c:\\nonexistent\\file";
957 r = IShellLinkA_SetIconLocation(sl, str, 0xbabecafe);
958 ok(r == S_OK, "SetIconLocation failed (0x%08x)\n", r);
960 i = 0xdeadbeef;
961 r = IShellLinkA_GetIconLocation(sl, buffer, sizeof(buffer), &i);
962 ok(r == S_OK, "GetIconLocation failed (0x%08x)\n", r);
963 ok(lstrcmpiA(buffer,str) == 0, "GetIconLocation returned '%s'\n", buffer);
964 ok(i == 0xbabecafe, "GetIconLocation returned %#x.\n", i);
966 r = IShellLinkA_SetIconLocation(sl, NULL, 0xcafefe);
967 ok(r == S_OK, "SetIconLocation failed (0x%08x)\n", r);
969 i = 0xdeadbeef;
970 r = IShellLinkA_GetIconLocation(sl, buffer, sizeof(buffer), &i);
971 ok(r == S_OK, "GetIconLocation failed (0x%08x)\n", r);
972 ok(!*buffer, "GetIconLocation returned '%s'\n", buffer);
973 ok(i == 0xcafefe, "GetIconLocation returned %#x.\n", i);
975 r = IShellLinkA_QueryInterface(sl, &IID_IShellLinkW, (void **)&slW);
976 ok(SUCCEEDED(r), "Failed to get IShellLinkW, hr %#x.\n", r);
978 str = "c:\\nonexistent\\file";
979 r = IShellLinkA_SetIconLocation(sl, str, 0xbabecafe);
980 ok(r == S_OK, "SetIconLocation failed (0x%08x)\n", r);
982 r = IShellLinkA_SetIconLocation(sl, NULL, 0xcafefe);
983 ok(r == S_OK, "SetIconLocation failed (0x%08x)\n", r);
985 i = 0xdeadbeef;
986 r = IShellLinkA_GetIconLocation(sl, buffer, sizeof(buffer), &i);
987 ok(r == S_OK, "GetIconLocation failed (0x%08x)\n", r);
988 ok(!*buffer, "GetIconLocation returned '%s'\n", buffer);
989 ok(i == 0xcafefe, "GetIconLocation returned %#x.\n", i);
991 IShellLinkW_Release(slW);
992 IShellLinkA_Release(sl);
995 static void test_SHGetStockIconInfo(void)
997 BYTE buffer[sizeof(SHSTOCKICONINFO) + 16];
998 SHSTOCKICONINFO *sii = (SHSTOCKICONINFO *) buffer;
999 HRESULT hr;
1000 INT i;
1002 /* not present before vista */
1003 if (!pSHGetStockIconInfo)
1005 win_skip("SHGetStockIconInfo not available\n");
1006 return;
1009 /* negative values are handled */
1010 memset(buffer, '#', sizeof(buffer));
1011 sii->cbSize = sizeof(SHSTOCKICONINFO);
1012 hr = pSHGetStockIconInfo(SIID_INVALID, SHGSI_ICONLOCATION, sii);
1013 ok(hr == E_INVALIDARG, "-1: got 0x%x (expected E_INVALIDARG)\n", hr);
1015 /* max. id for vista is 140 (no definition exists for this value) */
1016 for (i = SIID_DOCNOASSOC; i <= SIID_CLUSTEREDDRIVE; i++)
1018 memset(buffer, '#', sizeof(buffer));
1019 sii->cbSize = sizeof(SHSTOCKICONINFO);
1020 hr = pSHGetStockIconInfo(i, SHGSI_ICONLOCATION, sii);
1022 ok(hr == S_OK,
1023 "%3d: got 0x%x, iSysImageIndex: 0x%x, iIcon: 0x%x (expected S_OK)\n",
1024 i, hr, sii->iSysImageIndex, sii->iIcon);
1026 if ((hr == S_OK) && (winetest_debug > 1))
1027 trace("%3d: got iSysImageIndex %3d, iIcon %3d and %s\n", i, sii->iSysImageIndex,
1028 sii->iIcon, wine_dbgstr_w(sii->szPath));
1031 /* test invalid icons indices that are invalid for all platforms */
1032 for (i = SIID_MAX_ICONS; i < (SIID_MAX_ICONS + 25) ; i++)
1034 memset(buffer, '#', sizeof(buffer));
1035 sii->cbSize = sizeof(SHSTOCKICONINFO);
1036 hr = pSHGetStockIconInfo(i, SHGSI_ICONLOCATION, sii);
1037 ok(hr == E_INVALIDARG, "%3d: got 0x%x (expected E_INVALIDARG)\n", i, hr);
1038 todo_wine {
1039 ok(sii->iSysImageIndex == -1, "%d: got iSysImageIndex %d\n", i, sii->iSysImageIndex);
1040 ok(sii->iIcon == -1, "%d: got iIcon %d\n", i, sii->iIcon);
1044 /* test more returned SHSTOCKICONINFO elements without extra flags */
1045 memset(buffer, '#', sizeof(buffer));
1046 sii->cbSize = sizeof(SHSTOCKICONINFO);
1047 hr = pSHGetStockIconInfo(SIID_FOLDER, SHGSI_ICONLOCATION, sii);
1048 ok(hr == S_OK, "got 0x%x (expected S_OK)\n", hr);
1049 ok(!sii->hIcon, "got %p (expected NULL)\n", sii->hIcon);
1050 ok(sii->iSysImageIndex == -1, "got %d (expected -1)\n", sii->iSysImageIndex);
1052 /* the exact size is required of the struct */
1053 memset(buffer, '#', sizeof(buffer));
1054 sii->cbSize = sizeof(SHSTOCKICONINFO) + 2;
1055 hr = pSHGetStockIconInfo(SIID_FOLDER, SHGSI_ICONLOCATION, sii);
1056 ok(hr == E_INVALIDARG, "+2: got 0x%x, iSysImageIndex: 0x%x, iIcon: 0x%x\n", hr, sii->iSysImageIndex, sii->iIcon);
1058 memset(buffer, '#', sizeof(buffer));
1059 sii->cbSize = sizeof(SHSTOCKICONINFO) + 1;
1060 hr = pSHGetStockIconInfo(SIID_FOLDER, SHGSI_ICONLOCATION, sii);
1061 ok(hr == E_INVALIDARG, "+1: got 0x%x, iSysImageIndex: 0x%x, iIcon: 0x%x\n", hr, sii->iSysImageIndex, sii->iIcon);
1063 memset(buffer, '#', sizeof(buffer));
1064 sii->cbSize = sizeof(SHSTOCKICONINFO) - 1;
1065 hr = pSHGetStockIconInfo(SIID_FOLDER, SHGSI_ICONLOCATION, sii);
1066 ok(hr == E_INVALIDARG, "-1: got 0x%x, iSysImageIndex: 0x%x, iIcon: 0x%x\n", hr, sii->iSysImageIndex, sii->iIcon);
1068 memset(buffer, '#', sizeof(buffer));
1069 sii->cbSize = sizeof(SHSTOCKICONINFO) - 2;
1070 hr = pSHGetStockIconInfo(SIID_FOLDER, SHGSI_ICONLOCATION, sii);
1071 ok(hr == E_INVALIDARG, "-2: got 0x%x, iSysImageIndex: 0x%x, iIcon: 0x%x\n", hr, sii->iSysImageIndex, sii->iIcon);
1073 /* there is a NULL check for the struct */
1074 hr = pSHGetStockIconInfo(SIID_FOLDER, SHGSI_ICONLOCATION, NULL);
1075 ok(hr == E_INVALIDARG, "NULL: got 0x%x\n", hr);
1078 static void test_SHExtractIcons(void)
1080 static const WCHAR notepadW[] = {'n','o','t','e','p','a','d','.','e','x','e',0};
1081 static const WCHAR shell32W[] = {'s','h','e','l','l','3','2','.','d','l','l',0};
1082 static const WCHAR emptyW[] = {0};
1083 UINT ret, ret2;
1084 HICON icons[256];
1085 UINT ids[256], i;
1087 if (!pSHExtractIconsW)
1089 win_skip("SHExtractIconsW not available\n");
1090 return;
1093 ret = pSHExtractIconsW(emptyW, 0, 16, 16, icons, ids, 1, 0);
1094 ok(ret == ~0u, "got %u\n", ret);
1096 ret = pSHExtractIconsW(notepadW, 0, 16, 16, NULL, NULL, 1, 0);
1097 ok(ret == 1 || broken(ret == 2) /* win2k */, "got %u\n", ret);
1099 icons[0] = (HICON)0xdeadbeef;
1100 ret = pSHExtractIconsW(notepadW, 0, 16, 16, icons, NULL, 1, 0);
1101 ok(ret == 1, "got %u\n", ret);
1102 ok(icons[0] != (HICON)0xdeadbeef, "icon not set\n");
1103 DestroyIcon(icons[0]);
1105 icons[0] = (HICON)0xdeadbeef;
1106 ids[0] = 0xdeadbeef;
1107 ret = pSHExtractIconsW(notepadW, 0, 16, 16, icons, ids, 1, 0);
1108 ok(ret == 1, "got %u\n", ret);
1109 ok(icons[0] != (HICON)0xdeadbeef, "icon not set\n");
1110 ok(ids[0] != 0xdeadbeef, "id not set\n");
1111 DestroyIcon(icons[0]);
1113 ret = pSHExtractIconsW(shell32W, 0, 16, 16, NULL, NULL, 0, 0);
1114 ret2 = pSHExtractIconsW(shell32W, 4, MAKELONG(32,16), MAKELONG(32,16), NULL, NULL, 256, 0);
1115 ok(ret && ret == ret2,
1116 "icon count should be independent of requested icon sizes and base icon index\n");
1118 ret = pSHExtractIconsW(shell32W, 0, 16, 16, icons, ids, 0, 0);
1119 ok(ret == ~0u || !ret /* < vista */, "got %u\n", ret);
1121 ret = pSHExtractIconsW(shell32W, 0, 16, 16, icons, ids, 3, 0);
1122 ok(ret == 3, "got %u\n", ret);
1123 for (i = 0; i < ret; i++) DestroyIcon(icons[i]);
1125 /* count must be a multiple of two when getting two sizes */
1126 ret = pSHExtractIconsW(shell32W, 0, MAKELONG(16,32), MAKELONG(16,32), icons, ids, 3, 0);
1127 ok(!ret /* vista */ || ret == 4, "got %u\n", ret);
1128 for (i = 0; i < ret; i++) DestroyIcon(icons[i]);
1130 ret = pSHExtractIconsW(shell32W, 0, MAKELONG(16,32), MAKELONG(16,32), icons, ids, 4, 0);
1131 ok(ret == 4, "got %u\n", ret);
1132 for (i = 0; i < ret; i++) DestroyIcon(icons[i]);
1135 static void test_propertystore(void)
1137 IShellLinkA *linkA;
1138 IShellLinkW *linkW;
1139 IPropertyStore *ps;
1140 HRESULT hr;
1142 hr = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
1143 &IID_IShellLinkA, (void**)&linkA);
1144 ok(hr == S_OK, "got 0x%08x\n", hr);
1146 hr = IShellLinkA_QueryInterface(linkA, &IID_IShellLinkW, (void**)&linkW);
1147 ok(hr == S_OK, "got 0x%08x\n", hr);
1149 hr = IShellLinkA_QueryInterface(linkA, &IID_IPropertyStore, (void**)&ps);
1150 if (hr == S_OK) {
1151 IPropertyStoreCache *pscache;
1153 IPropertyStore_Release(ps);
1155 hr = IShellLinkW_QueryInterface(linkW, &IID_IPropertyStore, (void**)&ps);
1156 ok(hr == S_OK, "got 0x%08x\n", hr);
1158 hr = IPropertyStore_QueryInterface(ps, &IID_IPropertyStoreCache, (void**)&pscache);
1159 ok(hr == E_NOINTERFACE, "got 0x%08x\n", hr);
1161 IPropertyStore_Release(ps);
1163 else
1164 win_skip("IShellLink doesn't support IPropertyStore.\n");
1166 IShellLinkA_Release(linkA);
1167 IShellLinkW_Release(linkW);
1170 static void test_ExtractIcon(void)
1172 static const WCHAR nameW[] = {'\\','e','x','t','r','a','c','t','i','c','o','n','_','t','e','s','t','.','t','x','t',0};
1173 static const WCHAR shell32W[] = {'s','h','e','l','l','3','2','.','d','l','l',0};
1174 WCHAR pathW[MAX_PATH];
1175 HICON hicon, hicon2;
1176 char path[MAX_PATH];
1177 HANDLE file;
1178 int r;
1179 ICONINFO info;
1180 BITMAP bm;
1182 /* specified instance handle */
1183 hicon = ExtractIconA(GetModuleHandleA("shell32.dll"), NULL, 0);
1184 todo_wine
1185 ok(hicon == NULL, "Got icon %p\n", hicon);
1186 hicon2 = ExtractIconA(GetModuleHandleA("shell32.dll"), "shell32.dll", -1);
1187 ok(hicon2 != NULL, "Got icon %p\n", hicon2);
1189 /* existing index */
1190 hicon = ExtractIconA(NULL, "shell32.dll", 0);
1191 ok(hicon != NULL && HandleToLong(hicon) != -1, "Got icon %p\n", hicon);
1192 DestroyIcon(hicon);
1194 /* returns number of resources */
1195 hicon = ExtractIconA(NULL, "shell32.dll", -1);
1196 ok(HandleToLong(hicon) > 1 && hicon == hicon2, "Got icon %p\n", hicon);
1198 /* invalid index, valid dll name */
1199 hicon = ExtractIconA(NULL, "shell32.dll", 3000);
1200 ok(hicon == NULL, "Got icon %p\n", hicon);
1202 /* Create a temporary non-executable file */
1203 GetTempPathA(sizeof(path), path);
1204 strcat(path, "\\extracticon_test.txt");
1205 file = CreateFileA(path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1206 ok(file != INVALID_HANDLE_VALUE, "Failed to create a test file\n");
1207 CloseHandle(file);
1209 hicon = ExtractIconA(NULL, path, 0);
1210 todo_wine
1211 ok(hicon == NULL, "Got icon %p\n", hicon);
1213 hicon = ExtractIconA(NULL, path, -1);
1214 ok(hicon == NULL, "Got icon %p\n", hicon);
1216 hicon = ExtractIconA(NULL, path, 1);
1217 todo_wine
1218 ok(hicon == NULL, "Got icon %p\n", hicon);
1220 r = DeleteFileA(path);
1221 ok(r, "failed to delete file %s (%d)\n", path, GetLastError());
1223 /* same for W variant */
1224 if (0)
1226 /* specified instance handle, crashes on XP, 2k3 */
1227 hicon = ExtractIconW(GetModuleHandleA("shell32.dll"), NULL, 0);
1228 ok(hicon == NULL, "Got icon %p\n", hicon);
1230 hicon2 = ExtractIconW(GetModuleHandleA("shell32.dll"), shell32W, -1);
1231 ok(hicon2 != NULL, "Got icon %p\n", hicon2);
1233 /* existing index */
1234 hicon = ExtractIconW(NULL, shell32W, 0);
1235 ok(hicon != NULL && HandleToLong(hicon) != -1, "Got icon %p\n", hicon);
1236 GetIconInfo(hicon, &info);
1237 GetObjectW(info.hbmColor, sizeof(bm), &bm);
1238 ok(bm.bmWidth == GetSystemMetrics(SM_CXICON), "got %d\n", bm.bmWidth);
1239 ok(bm.bmHeight == GetSystemMetrics(SM_CYICON), "got %d\n", bm.bmHeight);
1240 DestroyIcon(hicon);
1242 /* returns number of resources */
1243 hicon = ExtractIconW(NULL, shell32W, -1);
1244 ok(HandleToLong(hicon) > 1 && hicon == hicon2, "Got icon %p\n", hicon);
1246 /* invalid index, valid dll name */
1247 hicon = ExtractIconW(NULL, shell32W, 3000);
1248 ok(hicon == NULL, "Got icon %p\n", hicon);
1250 /* Create a temporary non-executable file */
1251 GetTempPathW(ARRAY_SIZE(pathW), pathW);
1252 lstrcatW(pathW, nameW);
1253 file = CreateFileW(pathW, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1254 ok(file != INVALID_HANDLE_VALUE, "Failed to create a test file\n");
1255 CloseHandle(file);
1257 hicon = ExtractIconW(NULL, pathW, 0);
1258 todo_wine
1259 ok(hicon == NULL, "Got icon %p\n", hicon);
1261 hicon = ExtractIconW(NULL, pathW, -1);
1262 ok(hicon == NULL, "Got icon %p\n", hicon);
1264 hicon = ExtractIconW(NULL, pathW, 1);
1265 todo_wine
1266 ok(hicon == NULL, "Got icon %p\n", hicon);
1268 r = DeleteFileW(pathW);
1269 ok(r, "failed to delete file %s (%d)\n", path, GetLastError());
1272 static void test_ExtractAssociatedIcon(void)
1274 char pathA[MAX_PATH];
1275 HICON hicon;
1276 WORD index;
1278 /* empty path */
1279 index = 0;
1280 *pathA = 0;
1281 hicon = ExtractAssociatedIconA(NULL, pathA, &index);
1282 todo_wine {
1283 ok(hicon != NULL, "Got icon %p\n", hicon);
1284 ok(!*pathA, "Unexpected path %s\n", pathA);
1285 ok(index == 0, "Unexpected index %u\n", index);
1287 DestroyIcon(hicon);
1289 /* by index */
1290 index = 0;
1291 strcpy(pathA, "shell32.dll");
1292 hicon = ExtractAssociatedIconA(NULL, pathA, &index);
1293 ok(hicon != NULL, "Got icon %p\n", hicon);
1294 ok(!strcmp(pathA, "shell32.dll"), "Unexpected path %s\n", pathA);
1295 ok(index == 0, "Unexpected index %u\n", index);
1296 DestroyIcon(hicon);
1298 /* valid dll name, invalid index */
1299 index = 5000;
1300 strcpy(pathA, "user32.dll");
1301 hicon = ExtractAssociatedIconA(NULL, pathA, &index);
1302 CharLowerBuffA(pathA, strlen(pathA));
1303 todo_wine {
1304 ok(hicon != NULL, "Got icon %p\n", hicon);
1305 ok(!!strstr(pathA, "shell32.dll"), "Unexpected path %s\n", pathA);
1307 ok(index != 5000, "Unexpected index %u\n", index);
1308 DestroyIcon(hicon);
1310 /* associated icon */
1311 index = 0xcaca;
1312 strcpy(pathA, "dummy.exe");
1313 hicon = ExtractAssociatedIconA(NULL, pathA, &index);
1314 CharLowerBuffA(pathA, strlen(pathA));
1315 todo_wine {
1316 ok(hicon != NULL, "Got icon %p\n", hicon);
1317 ok(!!strstr(pathA, "shell32.dll"), "Unexpected path %s\n", pathA);
1319 ok(index != 0xcaca, "Unexpected index %u\n", index);
1320 DestroyIcon(hicon);
1323 static int get_shell_icon_size(void)
1325 char buf[10];
1326 DWORD value = 32, size = sizeof(buf), type;
1327 HKEY key;
1329 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Control Panel\\Desktop\\WindowMetrics", &key ))
1331 if (!RegQueryValueExA( key, "Shell Icon Size", NULL, &type, (BYTE *)buf, &size ) && type == REG_SZ)
1332 value = atoi( buf );
1333 RegCloseKey( key );
1335 return value;
1338 static void test_SHGetImageList(void)
1340 HRESULT hr;
1341 IImageList *list, *list2;
1342 BOOL ret;
1343 HIMAGELIST lg, sm;
1344 ULONG start_refs, refs;
1345 int i, width, height, expect;
1346 BOOL dpi_aware = pIsProcessDPIAware && pIsProcessDPIAware();
1348 hr = SHGetImageList( SHIL_LARGE, &IID_IImageList, (void **)&list );
1349 ok( hr == S_OK, "got %08x\n", hr );
1350 start_refs = IImageList_AddRef( list );
1351 IImageList_Release( list );
1353 hr = SHGetImageList( SHIL_LARGE, &IID_IImageList, (void **)&list2 );
1354 ok( hr == S_OK, "got %08x\n", hr );
1355 ok( list == list2, "lists differ\n" );
1356 refs = IImageList_AddRef( list );
1357 IImageList_Release( list );
1358 ok( refs == start_refs + 1, "got %d, start_refs %d\n", refs, start_refs );
1359 IImageList_Release( list2 );
1361 hr = SHGetImageList( SHIL_SMALL, &IID_IImageList, (void **)&list2 );
1362 ok( hr == S_OK, "got %08x\n", hr );
1364 ret = Shell_GetImageLists( &lg, &sm );
1365 ok( ret, "got %d\n", ret );
1366 ok( lg == (HIMAGELIST)list, "mismatch\n" );
1367 ok( sm == (HIMAGELIST)list2, "mismatch\n" );
1369 /* Shell_GetImageLists doesn't take a reference */
1370 refs = IImageList_AddRef( list );
1371 IImageList_Release( list );
1372 ok( refs == start_refs, "got %d, start_refs %d\n", refs, start_refs );
1374 IImageList_Release( list2 );
1375 IImageList_Release( list );
1377 /* Test the icon sizes */
1378 for (i = 0; i <= SHIL_LAST; i++)
1380 hr = SHGetImageList( i, &IID_IImageList, (void **)&list );
1381 ok( hr == S_OK || broken( i == SHIL_JUMBO && hr == E_INVALIDARG ), /* XP and 2003 */
1382 "%d: got %08x\n", i, hr );
1383 if (FAILED(hr)) continue;
1384 IImageList_GetIconSize( list, &width, &height );
1385 switch (i)
1387 case SHIL_LARGE:
1388 if (dpi_aware) expect = GetSystemMetrics( SM_CXICON );
1389 else expect = get_shell_icon_size();
1390 break;
1391 case SHIL_SMALL:
1392 if (dpi_aware) expect = GetSystemMetrics( SM_CXICON ) / 2;
1393 else expect = GetSystemMetrics( SM_CXSMICON );
1394 break;
1395 case SHIL_EXTRALARGE:
1396 expect = (GetSystemMetrics( SM_CXICON ) * 3) / 2;
1397 break;
1398 case SHIL_SYSSMALL:
1399 expect = GetSystemMetrics( SM_CXSMICON );
1400 break;
1401 case SHIL_JUMBO:
1402 expect = 256;
1403 break;
1405 todo_wine_if(i == SHIL_SYSSMALL && dpi_aware && expect != GetSystemMetrics( SM_CXICON ) / 2)
1407 ok( width == expect, "%d: got %d expect %d\n", i, width, expect );
1408 ok( height == expect, "%d: got %d expect %d\n", i, height, expect );
1410 IImageList_Release( list );
1414 START_TEST(shelllink)
1416 HRESULT r;
1417 HMODULE hmod = GetModuleHandleA("shell32.dll");
1418 HMODULE huser32 = GetModuleHandleA("user32.dll");
1420 pSHILCreateFromPath = (void *)GetProcAddress(hmod, (LPSTR)28);
1421 pSHGetFolderLocation = (void *)GetProcAddress(hmod, "SHGetFolderLocation");
1422 pSHGetStockIconInfo = (void *)GetProcAddress(hmod, "SHGetStockIconInfo");
1423 pSHExtractIconsW = (void *)GetProcAddress(hmod, "SHExtractIconsW");
1424 pIsProcessDPIAware = (void *)GetProcAddress(huser32, "IsProcessDPIAware");
1426 r = CoInitialize(NULL);
1427 ok(r == S_OK, "CoInitialize failed (0x%08x)\n", r);
1428 if (r != S_OK)
1429 return;
1431 test_get_set();
1432 test_load_save();
1433 test_datalink();
1434 test_shdefextracticon();
1435 test_GetIconLocation();
1436 test_SHGetStockIconInfo();
1437 test_SHExtractIcons();
1438 test_propertystore();
1439 test_ExtractIcon();
1440 test_ExtractAssociatedIcon();
1441 test_SHGetImageList();
1443 CoUninitialize();