shelllink.c 48.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Unit tests for shelllinks
 *
 * Copyright 2004 Mike McCormack
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 20 21 22 23
 *
 */

#define COBJMACROS

24 25
#include "initguid.h"
#include "windows.h"
26 27
#include "shlguid.h"
#include "shobjidl.h"
28
#include "shlobj.h"
29
#include "shellapi.h"
30
#include "commoncontrols.h"
31 32
#include "wine/test.h"

33 34
#include "shell32_test.h"

35 36 37
#ifndef SLDF_HAS_LOGO3ID
#  define SLDF_HAS_LOGO3ID 0x00000800 /* not available in the Vista SDK */
#endif
38

39 40 41 42
static void (WINAPI *pILFree)(LPITEMIDLIST);
static BOOL (WINAPI *pILIsEqual)(LPCITEMIDLIST, LPCITEMIDLIST);
static HRESULT (WINAPI *pSHILCreateFromPath)(LPCWSTR, LPITEMIDLIST *,DWORD*);
static HRESULT (WINAPI *pSHDefExtractIconA)(LPCSTR, int, UINT, HICON*, HICON*, UINT);
43
static HRESULT (WINAPI *pSHGetStockIconInfo)(SHSTOCKICONID, UINT, SHSTOCKICONINFO *);
44
static DWORD (WINAPI *pGetLongPathNameA)(LPCSTR, LPSTR, DWORD);
45
static DWORD (WINAPI *pGetShortPathNameA)(LPCSTR, LPSTR, DWORD);
46
static UINT (WINAPI *pSHExtractIconsW)(LPCWSTR, int, int, int, HICON *, UINT *, UINT, UINT);
47
static BOOL (WINAPI *pIsProcessDPIAware)(void);
48

49 50 51 52 53
static const GUID _IID_IShellLinkDataList = {
    0x45e2b4ae, 0xb1c3, 0x11d0,
    { 0xb9, 0x2f, 0x00, 0xa0, 0xc9, 0x03, 0x12, 0xe1 }
};

54 55 56 57 58

/* For some reason SHILCreateFromPath does not work on Win98 and
 * SHSimpleIDListFromPathA does not work on NT4. But if we call both we
 * get what we want on all platforms.
 */
59
static LPITEMIDLIST (WINAPI *pSHSimpleIDListFromPathAW)(LPCVOID);
60 61 62 63 64

static LPITEMIDLIST path_to_pidl(const char* path)
{
    LPITEMIDLIST pidl;

65
    if (!pSHSimpleIDListFromPathAW)
66
    {
67
        HMODULE hdll=GetModuleHandleA("shell32.dll");
68 69
        pSHSimpleIDListFromPathAW=(void*)GetProcAddress(hdll, (char*)162);
        if (!pSHSimpleIDListFromPathAW)
70
            win_skip("SHSimpleIDListFromPathAW not found in shell32.dll\n");
71 72 73
    }

    pidl=NULL;
74 75 76
    /* pSHSimpleIDListFromPathAW maps to A on non NT platforms */
    if (pSHSimpleIDListFromPathAW && (GetVersion() & 0x80000000))
        pidl=pSHSimpleIDListFromPathAW(path);
77 78 79 80 81 82 83 84 85 86 87

    if (!pidl)
    {
        WCHAR* pathW;
        HRESULT r;
        int len;

        len=MultiByteToWideChar(CP_ACP, 0, path, -1, NULL, 0);
        pathW=HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
        MultiByteToWideChar(CP_ACP, 0, path, -1, pathW, len);

88
        r=pSHILCreateFromPath(pathW, &pidl, NULL);
89
        ok(r == S_OK, "SHILCreateFromPath failed (0x%08x)\n", r);
90 91 92 93 94 95 96 97 98 99
        HeapFree(GetProcessHeap(), 0, pathW);
    }
    return pidl;
}


/*
 * Test manipulation of an IShellLink's properties.
 */

100
static void test_get_set(void)
101 102
{
    HRESULT r;
103
    IShellLinkA *sl;
104
    IShellLinkW *slW = NULL;
105 106 107
    char mypath[MAX_PATH];
    char buffer[INFOTIPSIZE];
    LPITEMIDLIST pidl, tmp_pidl;
108
    const char * str;
109 110 111 112 113
    int i;
    WORD w;

    r = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
                         &IID_IShellLinkA, (LPVOID*)&sl);
114 115
    ok(r == S_OK, "no IID_IShellLinkA (0x%08x)\n", r);
    if (r != S_OK)
116 117 118 119 120
        return;

    /* Test Getting / Setting the description */
    strcpy(buffer,"garbage");
    r = IShellLinkA_GetDescription(sl, buffer, sizeof(buffer));
121
    ok(r == S_OK, "GetDescription failed (0x%08x)\n", r);
122 123 124 125
    ok(*buffer=='\0', "GetDescription returned '%s'\n", buffer);

    str="Some description";
    r = IShellLinkA_SetDescription(sl, str);
126
    ok(r == S_OK, "SetDescription failed (0x%08x)\n", r);
127 128 129

    strcpy(buffer,"garbage");
    r = IShellLinkA_GetDescription(sl, buffer, sizeof(buffer));
130
    ok(r == S_OK, "GetDescription failed (0x%08x)\n", r);
131
    ok(strcmp(buffer,str)==0, "GetDescription returned '%s'\n", buffer);
132

133 134 135 136 137 138
    r = IShellLinkA_SetDescription(sl, NULL);
    ok(r == S_OK, "SetDescription failed (0x%08x)\n", r);

    strcpy(buffer,"garbage");
    r = IShellLinkA_GetDescription(sl, buffer, sizeof(buffer));
    ok(r == S_OK, "GetDescription failed (0x%08x)\n", r);
139
    ok(*buffer=='\0' || broken(strcmp(buffer,str)==0), "GetDescription returned '%s'\n", buffer); /* NT4 */
140

141 142 143
    /* Test Getting / Setting the work directory */
    strcpy(buffer,"garbage");
    r = IShellLinkA_GetWorkingDirectory(sl, buffer, sizeof(buffer));
144
    ok(r == S_OK, "GetWorkingDirectory failed (0x%08x)\n", r);
145
    ok(*buffer=='\0', "GetWorkingDirectory returned '%s'\n", buffer);
146

147 148
    str="c:\\nonexistent\\directory";
    r = IShellLinkA_SetWorkingDirectory(sl, str);
149
    ok(r == S_OK, "SetWorkingDirectory failed (0x%08x)\n", r);
150 151 152

    strcpy(buffer,"garbage");
    r = IShellLinkA_GetWorkingDirectory(sl, buffer, sizeof(buffer));
153
    ok(r == S_OK, "GetWorkingDirectory failed (0x%08x)\n", r);
154
    ok(lstrcmpiA(buffer,str)==0, "GetWorkingDirectory returned '%s'\n", buffer);
155

156
    /* Test Getting / Setting the path */
157 158
    strcpy(buffer,"garbage");
    r = IShellLinkA_GetPath(sl, buffer, sizeof(buffer), NULL, SLGP_RAWPATH);
159
    todo_wine ok(r == S_FALSE || broken(r == S_OK) /* NT4/W2K */, "GetPath failed (0x%08x)\n", r);
160 161
    ok(*buffer=='\0', "GetPath returned '%s'\n", buffer);

162 163
    CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
                     &IID_IShellLinkW, (LPVOID*)&slW);
164 165
    if (!slW /* Win9x */ || !pGetLongPathNameA /* NT4 */)
        skip("SetPath with NULL parameter crashes on Win9x and some NT4\n");
166 167 168 169
    else
    {
        IShellLinkW_Release(slW);
        r = IShellLinkA_SetPath(sl, NULL);
170 171
        ok(r==E_INVALIDARG ||
           broken(r==S_OK), /* Some Win95 and NT4 */
172
           "SetPath returned wrong error (0x%08x)\n", r);
173
    }
174

175
    r = IShellLinkA_SetPath(sl, "");
176
    ok(r==S_OK, "SetPath failed (0x%08x)\n", r);
177 178 179

    strcpy(buffer,"garbage");
    r = IShellLinkA_GetPath(sl, buffer, sizeof(buffer), NULL, SLGP_RAWPATH);
180
    todo_wine ok(r == S_FALSE, "GetPath failed (0x%08x)\n", r);
181 182
    ok(*buffer=='\0', "GetPath returned '%s'\n", buffer);

183
    /* Win98 returns S_FALSE, but WinXP returns S_OK */
184 185
    str="c:\\nonexistent\\file";
    r = IShellLinkA_SetPath(sl, str);
186
    ok(r==S_FALSE || r==S_OK, "SetPath failed (0x%08x)\n", r);
187 188 189

    strcpy(buffer,"garbage");
    r = IShellLinkA_GetPath(sl, buffer, sizeof(buffer), NULL, SLGP_RAWPATH);
190
    ok(r == S_OK, "GetPath failed (0x%08x)\n", r);
191
    ok(lstrcmpiA(buffer,str)==0, "GetPath returned '%s'\n", buffer);
192

193 194 195
    /* Get some real path to play with */
    GetWindowsDirectoryA( mypath, sizeof(mypath)-12 );
    strcat(mypath, "\\regedit.exe");
196 197 198 199

    /* Test the interaction of SetPath and SetIDList */
    tmp_pidl=NULL;
    r = IShellLinkA_GetIDList(sl, &tmp_pidl);
200
    ok(r == S_OK, "GetIDList failed (0x%08x)\n", r);
201
    if (r == S_OK)
202
    {
203 204
        BOOL ret;

205
        strcpy(buffer,"garbage");
206 207 208
        ret = SHGetPathFromIDListA(tmp_pidl, buffer);
        ok(ret, "SHGetPathFromIDListA failed\n");
        if (ret)
209
            ok(lstrcmpiA(buffer,str)==0, "GetIDList returned '%s'\n", buffer);
210
        pILFree(tmp_pidl);
211
    }
212

213 214 215 216 217
    pidl=path_to_pidl(mypath);
    ok(pidl!=NULL, "path_to_pidl returned a NULL pidl\n");

    if (pidl)
    {
218 219
        LPITEMIDLIST second_pidl;

220
        r = IShellLinkA_SetIDList(sl, pidl);
221
        ok(r == S_OK, "SetIDList failed (0x%08x)\n", r);
222 223 224

        tmp_pidl=NULL;
        r = IShellLinkA_GetIDList(sl, &tmp_pidl);
225
        ok(r == S_OK, "GetIDList failed (0x%08x)\n", r);
226
        ok(tmp_pidl && pILIsEqual(pidl, tmp_pidl),
227 228
           "GetIDList returned an incorrect pidl\n");

229
        r = IShellLinkA_GetIDList(sl, &second_pidl);
230
        ok(r == S_OK, "GetIDList failed (0x%08x)\n", r);
231 232 233 234 235 236
        ok(second_pidl && pILIsEqual(pidl, second_pidl),
           "GetIDList returned an incorrect pidl\n");
        ok(second_pidl != tmp_pidl, "pidls are the same\n");

        pILFree(second_pidl);
        pILFree(tmp_pidl);
237
        pILFree(pidl);
238 239 240

        strcpy(buffer,"garbage");
        r = IShellLinkA_GetPath(sl, buffer, sizeof(buffer), NULL, SLGP_RAWPATH);
241
        ok(r == S_OK, "GetPath failed (0x%08x)\n", r);
242
        todo_wine
243
        ok(lstrcmpiA(buffer, mypath)==0, "GetPath returned '%s'\n", buffer);
244 245
    }

246
    /* test path with quotes (IShellLinkA_SetPath returns S_FALSE on W2K and below and S_OK on XP and above */
247
    r = IShellLinkA_SetPath(sl, "\"c:\\nonexistent\\file\"");
248
    ok(r==S_FALSE || r == S_OK, "SetPath failed (0x%08x)\n", r);
249

250
    strcpy(buffer,"garbage");
251
    r = IShellLinkA_GetPath(sl, buffer, sizeof(buffer), NULL, SLGP_RAWPATH);
252
    ok(r==S_OK, "GetPath failed (0x%08x)\n", r);
253
    todo_wine ok(!strcmp(buffer, "C:\\nonexistent\\file") ||
254
       broken(!strcmp(buffer, "C:\\\"c:\\nonexistent\\file\"")), /* NT4 */
255
       "case doesn't match\n");
256 257

    r = IShellLinkA_SetPath(sl, "\"c:\\foo");
258
    ok(r==S_FALSE || r == S_OK || r == E_INVALIDARG /* Vista */, "SetPath failed (0x%08x)\n", r);
259 260

    r = IShellLinkA_SetPath(sl, "\"\"c:\\foo");
261
    ok(r==S_FALSE || r == S_OK || r == E_INVALIDARG /* Vista */, "SetPath failed (0x%08x)\n", r);
262 263

    r = IShellLinkA_SetPath(sl, "c:\\foo\"");
264
    ok(r==S_FALSE || r == S_OK || r == E_INVALIDARG /* Vista */, "SetPath failed (0x%08x)\n", r);
265 266

    r = IShellLinkA_SetPath(sl, "\"\"c:\\foo\"");
267
    ok(r==S_FALSE || r == S_OK || r == E_INVALIDARG /* Vista */, "SetPath failed (0x%08x)\n", r);
268 269

    r = IShellLinkA_SetPath(sl, "\"\"c:\\foo\"\"");
270
    ok(r==S_FALSE || r == S_OK || r == E_INVALIDARG /* Vista */, "SetPath failed (0x%08x)\n", r);
271

272 273 274
    /* Test Getting / Setting the arguments */
    strcpy(buffer,"garbage");
    r = IShellLinkA_GetArguments(sl, buffer, sizeof(buffer));
275
    ok(r == S_OK, "GetArguments failed (0x%08x)\n", r);
276 277 278 279
    ok(*buffer=='\0', "GetArguments returned '%s'\n", buffer);

    str="param1 \"spaced param2\"";
    r = IShellLinkA_SetArguments(sl, str);
280
    ok(r == S_OK, "SetArguments failed (0x%08x)\n", r);
281 282 283

    strcpy(buffer,"garbage");
    r = IShellLinkA_GetArguments(sl, buffer, sizeof(buffer));
284
    ok(r == S_OK, "GetArguments failed (0x%08x)\n", r);
285
    ok(strcmp(buffer,str)==0, "GetArguments returned '%s'\n", buffer);
286

287 288
    strcpy(buffer,"garbage");
    r = IShellLinkA_SetArguments(sl, NULL);
289
    ok(r == S_OK, "SetArguments failed (0x%08x)\n", r);
290
    r = IShellLinkA_GetArguments(sl, buffer, sizeof(buffer));
291
    ok(r == S_OK, "GetArguments failed (0x%08x)\n", r);
292
    ok(!buffer[0] || strcmp(buffer,str)==0, "GetArguments returned '%s'\n", buffer);
293 294 295

    strcpy(buffer,"garbage");
    r = IShellLinkA_SetArguments(sl, "");
296
    ok(r == S_OK, "SetArguments failed (0x%08x)\n", r);
297
    r = IShellLinkA_GetArguments(sl, buffer, sizeof(buffer));
298
    ok(r == S_OK, "GetArguments failed (0x%08x)\n", r);
299 300
    ok(!buffer[0], "GetArguments returned '%s'\n", buffer);

301 302 303
    /* Test Getting / Setting showcmd */
    i=0xdeadbeef;
    r = IShellLinkA_GetShowCmd(sl, &i);
304
    ok(r == S_OK, "GetShowCmd failed (0x%08x)\n", r);
305 306 307
    ok(i==SW_SHOWNORMAL, "GetShowCmd returned %d\n", i);

    r = IShellLinkA_SetShowCmd(sl, SW_SHOWMAXIMIZED);
308
    ok(r == S_OK, "SetShowCmd failed (0x%08x)\n", r);
309 310 311

    i=0xdeadbeef;
    r = IShellLinkA_GetShowCmd(sl, &i);
312
    ok(r == S_OK, "GetShowCmd failed (0x%08x)\n", r);
313 314 315 316 317 318
    ok(i==SW_SHOWMAXIMIZED, "GetShowCmd returned %d'\n", i);

    /* Test Getting / Setting the icon */
    i=0xdeadbeef;
    strcpy(buffer,"garbage");
    r = IShellLinkA_GetIconLocation(sl, buffer, sizeof(buffer), &i);
319
    ok(r == S_OK, "GetIconLocation failed (0x%08x)\n", r);
320 321
    ok(*buffer=='\0', "GetIconLocation returned '%s'\n", buffer);
    ok(i==0, "GetIconLocation returned %d\n", i);
322 323 324

    str="c:\\nonexistent\\file";
    r = IShellLinkA_SetIconLocation(sl, str, 0xbabecafe);
325
    ok(r == S_OK, "SetIconLocation failed (0x%08x)\n", r);
326

327 328
    i=0xdeadbeef;
    r = IShellLinkA_GetIconLocation(sl, buffer, sizeof(buffer), &i);
329
    ok(r == S_OK, "GetIconLocation failed (0x%08x)\n", r);
330
    ok(lstrcmpiA(buffer,str)==0, "GetIconLocation returned '%s'\n", buffer);
331 332 333 334 335
    ok(i==0xbabecafe, "GetIconLocation returned %d'\n", i);

    /* Test Getting / Setting the hot key */
    w=0xbeef;
    r = IShellLinkA_GetHotkey(sl, &w);
336
    ok(r == S_OK, "GetHotkey failed (0x%08x)\n", r);
337 338 339
    ok(w==0, "GetHotkey returned %d\n", w);

    r = IShellLinkA_SetHotkey(sl, 0x5678);
340
    ok(r == S_OK, "SetHotkey failed (0x%08x)\n", r);
341 342 343

    w=0xbeef;
    r = IShellLinkA_GetHotkey(sl, &w);
344
    ok(r == S_OK, "GetHotkey failed (0x%08x)\n", r);
345 346 347 348 349 350 351 352 353 354 355
    ok(w==0x5678, "GetHotkey returned %d'\n", w);

    IShellLinkA_Release(sl);
}


/*
 * Test saving and loading .lnk files
 */

#define lok                   ok_(__FILE__, line)
356
#define check_lnk(a,b,c)        check_lnk_(__LINE__, (a), (b), (c))
357

358
void create_lnk_(int line, const WCHAR* path, lnk_desc_t* desc, int save_fails)
359 360 361 362 363 364 365
{
    HRESULT r;
    IShellLinkA *sl;
    IPersistFile *pf;

    r = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
                         &IID_IShellLinkA, (LPVOID*)&sl);
366 367
    lok(r == S_OK, "no IID_IShellLinkA (0x%08x)\n", r);
    if (r != S_OK)
368 369 370 371 372
        return;

    if (desc->description)
    {
        r = IShellLinkA_SetDescription(sl, desc->description);
373
        lok(r == S_OK, "SetDescription failed (0x%08x)\n", r);
374 375 376 377
    }
    if (desc->workdir)
    {
        r = IShellLinkA_SetWorkingDirectory(sl, desc->workdir);
378
        lok(r == S_OK, "SetWorkingDirectory failed (0x%08x)\n", r);
379 380 381 382
    }
    if (desc->path)
    {
        r = IShellLinkA_SetPath(sl, desc->path);
383
        lok(SUCCEEDED(r), "SetPath failed (0x%08x)\n", r);
384 385 386 387
    }
    if (desc->pidl)
    {
        r = IShellLinkA_SetIDList(sl, desc->pidl);
388
        lok(r == S_OK, "SetIDList failed (0x%08x)\n", r);
389 390 391 392
    }
    if (desc->arguments)
    {
        r = IShellLinkA_SetArguments(sl, desc->arguments);
393
        lok(r == S_OK, "SetArguments failed (0x%08x)\n", r);
394 395 396 397
    }
    if (desc->showcmd)
    {
        r = IShellLinkA_SetShowCmd(sl, desc->showcmd);
398
        lok(r == S_OK, "SetShowCmd failed (0x%08x)\n", r);
399 400 401 402
    }
    if (desc->icon)
    {
        r = IShellLinkA_SetIconLocation(sl, desc->icon, desc->icon_id);
403
        lok(r == S_OK, "SetIconLocation failed (0x%08x)\n", r);
404 405 406 407
    }
    if (desc->hotkey)
    {
        r = IShellLinkA_SetHotkey(sl, desc->hotkey);
408
        lok(r == S_OK, "SetHotkey failed (0x%08x)\n", r);
409 410
    }

411
    r = IShellLinkA_QueryInterface(sl, &IID_IPersistFile, (void**)&pf);
412 413
    lok(r == S_OK, "no IID_IPersistFile (0x%08x)\n", r);
    if (r == S_OK)
414
    {
415 416 417 418 419
        LPOLESTR str;

    if (0)
    {
        /* crashes on XP */
420
        IPersistFile_GetCurFile(pf, NULL);
421 422 423 424 425
    }

        /* test GetCurFile before ::Save */
        str = (LPWSTR)0xdeadbeef;
        r = IPersistFile_GetCurFile(pf, &str);
426 427 428 429
        lok(r == S_FALSE ||
            broken(r == S_OK), /* shell32 < 5.0 */
            "got 0x%08x\n", r);
        lok(str == NULL, "got %p\n", str);
430

431
        r = IPersistFile_Save(pf, path, TRUE);
432
        todo_wine_if (save_fails)
433
            lok(r == S_OK, "save failed (0x%08x)\n", r);
434 435 436 437

        /* test GetCurFile after ::Save */
        r = IPersistFile_GetCurFile(pf, &str);
        lok(r == S_OK, "got 0x%08x\n", r);
438 439 440 441 442 443
        lok(str != NULL ||
            broken(str == NULL), /* shell32 < 5.0 */
            "Didn't expect NULL\n");
        if (str != NULL)
        {
            IMalloc *pmalloc;
444

445 446
            lok(!winetest_strcmpW(path, str), "Expected %s, got %s\n",
                wine_dbgstr_w(path), wine_dbgstr_w(str));
447

448 449 450 451 452
            SHGetMalloc(&pmalloc);
            IMalloc_Free(pmalloc, str);
        }
        else
            win_skip("GetCurFile fails on shell32 < 5.0\n");
453

454 455
        IPersistFile_Release(pf);
    }
456

457 458
    IShellLinkA_Release(sl);
}
459

460
static void check_lnk_(int line, const WCHAR* path, lnk_desc_t* desc, int todo)
461 462 463 464 465
{
    HRESULT r;
    IShellLinkA *sl;
    IPersistFile *pf;
    char buffer[INFOTIPSIZE];
466
    LPOLESTR str;
467 468 469

    r = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
                         &IID_IShellLinkA, (LPVOID*)&sl);
470 471
    lok(r == S_OK, "no IID_IShellLinkA (0x%08x)\n", r);
    if (r != S_OK)
472 473 474
        return;

    r = IShellLinkA_QueryInterface(sl, &IID_IPersistFile, (LPVOID*)&pf);
475 476
    lok(r == S_OK, "no IID_IPersistFile (0x%08x)\n", r);
    if (r != S_OK)
477 478 479 480 481
    {
        IShellLinkA_Release(sl);
        return;
    }

482 483 484
    /* test GetCurFile before ::Load */
    str = (LPWSTR)0xdeadbeef;
    r = IPersistFile_GetCurFile(pf, &str);
485 486 487
    lok(r == S_FALSE ||
        broken(r == S_OK), /* shell32 < 5.0 */
        "got 0x%08x\n", r);
488 489
    lok(str == NULL, "got %p\n", str);

490
    r = IPersistFile_Load(pf, path, STGM_READ);
491
    lok(r == S_OK, "load failed (0x%08x)\n", r);
492 493 494 495

    /* test GetCurFile after ::Save */
    r = IPersistFile_GetCurFile(pf, &str);
    lok(r == S_OK, "got 0x%08x\n", r);
496 497 498 499 500 501
    lok(str != NULL ||
        broken(str == NULL), /* shell32 < 5.0 */
        "Didn't expect NULL\n");
    if (str != NULL)
    {
        IMalloc *pmalloc;
502

503 504
        lok(!winetest_strcmpW(path, str), "Expected %s, got %s\n",
            wine_dbgstr_w(path), wine_dbgstr_w(str));
505

506 507 508 509 510
        SHGetMalloc(&pmalloc);
        IMalloc_Free(pmalloc, str);
    }
    else
        win_skip("GetCurFile fails on shell32 < 5.0\n");
511

512
    IPersistFile_Release(pf);
513
    if (r != S_OK)
514 515 516
    {
        IShellLinkA_Release(sl);
        return;
517
    }
518 519 520 521 522

    if (desc->description)
    {
        strcpy(buffer,"garbage");
        r = IShellLinkA_GetDescription(sl, buffer, sizeof(buffer));
523
        lok(r == S_OK, "GetDescription failed (0x%08x)\n", r);
524 525 526
        todo_wine_if ((todo & 0x1) != 0)
            lok(strcmp(buffer, desc->description)==0, "GetDescription returned '%s' instead of '%s'\n",
                buffer, desc->description);
527 528 529 530 531
    }
    if (desc->workdir)
    {
        strcpy(buffer,"garbage");
        r = IShellLinkA_GetWorkingDirectory(sl, buffer, sizeof(buffer));
532
        lok(r == S_OK, "GetWorkingDirectory failed (0x%08x)\n", r);
533 534 535
        todo_wine_if ((todo & 0x2) != 0)
            lok(lstrcmpiA(buffer, desc->workdir)==0, "GetWorkingDirectory returned '%s' instead of '%s'\n",
                buffer, desc->workdir);
536 537 538 539 540
    }
    if (desc->path)
    {
        strcpy(buffer,"garbage");
        r = IShellLinkA_GetPath(sl, buffer, sizeof(buffer), NULL, SLGP_RAWPATH);
541
        lok(SUCCEEDED(r), "GetPath failed (0x%08x)\n", r);
542 543 544
        todo_wine_if ((todo & 0x4) != 0)
            lok(lstrcmpiA(buffer, desc->path)==0, "GetPath returned '%s' instead of '%s'\n",
                buffer, desc->path);
545 546 547 548 549
    }
    if (desc->pidl)
    {
        LPITEMIDLIST pidl=NULL;
        r = IShellLinkA_GetIDList(sl, &pidl);
550
        lok(r == S_OK, "GetIDList failed (0x%08x)\n", r);
551 552
        todo_wine_if ((todo & 0x8) != 0)
            lok(pILIsEqual(pidl, desc->pidl), "GetIDList returned an incorrect pidl\n");
553 554 555 556 557
    }
    if (desc->showcmd)
    {
        int i=0xdeadbeef;
        r = IShellLinkA_GetShowCmd(sl, &i);
558
        lok(r == S_OK, "GetShowCmd failed (0x%08x)\n", r);
559 560 561
        todo_wine_if ((todo & 0x10) != 0)
            lok(i==desc->showcmd, "GetShowCmd returned 0x%0x instead of 0x%0x\n",
                i, desc->showcmd);
562 563 564 565 566 567
    }
    if (desc->icon)
    {
        int i=0xdeadbeef;
        strcpy(buffer,"garbage");
        r = IShellLinkA_GetIconLocation(sl, buffer, sizeof(buffer), &i);
568
        lok(r == S_OK, "GetIconLocation failed (0x%08x)\n", r);
569 570 571 572 573 574
        todo_wine_if ((todo & 0x20) != 0) {
            lok(lstrcmpiA(buffer, desc->icon)==0, "GetIconLocation returned '%s' instead of '%s'\n",
                buffer, desc->icon);
            lok(i==desc->icon_id, "GetIconLocation returned 0x%0x instead of 0x%0x\n",
                i, desc->icon_id);
        }
575 576 577 578 579
    }
    if (desc->hotkey)
    {
        WORD i=0xbeef;
        r = IShellLinkA_GetHotkey(sl, &i);
580
        lok(r == S_OK, "GetHotkey failed (0x%08x)\n", r);
581 582 583
        todo_wine_if ((todo & 0x40) != 0)
            lok(i==desc->hotkey, "GetHotkey returned 0x%04x instead of 0x%04x\n",
                i, desc->hotkey);
584 585 586 587 588
    }

    IShellLinkA_Release(sl);
}

589
static void test_load_save(void)
590
{
591 592 593 594
    WCHAR lnkfile[MAX_PATH];
    char lnkfileA[MAX_PATH];
    static const char lnkfileA_name[] = "\\test.lnk";

595 596 597
    lnk_desc_t desc;
    char mypath[MAX_PATH];
    char mydir[MAX_PATH];
598
    char realpath[MAX_PATH];
599
    char* p;
600
    HANDLE hf;
601 602
    DWORD r;

603 604
    if (!pGetLongPathNameA)
    {
605
        win_skip("GetLongPathNameA is not available\n");
606 607 608
        return;
    }

609 610 611
    /* Don't used a fixed path for the test.lnk file */
    GetTempPathA(MAX_PATH, lnkfileA);
    lstrcatA(lnkfileA, lnkfileA_name);
612
    MultiByteToWideChar(CP_ACP, 0, lnkfileA, -1, lnkfile, MAX_PATH);
613

614 615
    /* Save an empty .lnk file */
    memset(&desc, 0, sizeof(desc));
616
    create_lnk(lnkfile, &desc, 0);
617 618 619 620 621 622 623

    /* It should come back as a bunch of empty strings */
    desc.description="";
    desc.workdir="";
    desc.path="";
    desc.arguments="";
    desc.icon="";
624
    check_lnk(lnkfile, &desc, 0x0);
625 626 627 628 629 630 631 632 633 634 635 636

    /* Point a .lnk file to nonexistent files */
    desc.description="";
    desc.workdir="c:\\Nonexitent\\work\\directory";
    desc.path="c:\\nonexistent\\path";
    desc.pidl=NULL;
    desc.arguments="";
    desc.showcmd=0;
    desc.icon="c:\\nonexistent\\icon\\file";
    desc.icon_id=1234;
    desc.hotkey=0;
    create_lnk(lnkfile, &desc, 0);
637
    check_lnk(lnkfile, &desc, 0x0);
638

639
    r=GetModuleFileNameA(NULL, mypath, sizeof(mypath));
640
    ok(r<sizeof(mypath), "GetModuleFileName failed (%d)\n", r);
641 642 643 644 645
    strcpy(mydir, mypath);
    p=strrchr(mydir, '\\');
    if (p)
        *p='\0';

646
    /* IShellLink returns path in long form */
647
    if (!pGetLongPathNameA(mypath, realpath, MAX_PATH)) strcpy( realpath, mypath );
648

649 650 651
    /* Overwrite the existing lnk file and point it to existing files */
    desc.description="test 2";
    desc.workdir=mydir;
652
    desc.path=realpath;
653 654 655 656 657 658 659
    desc.pidl=NULL;
    desc.arguments="/option1 /option2 \"Some string\"";
    desc.showcmd=SW_SHOWNORMAL;
    desc.icon=mypath;
    desc.icon_id=0;
    desc.hotkey=0x1234;
    create_lnk(lnkfile, &desc, 0);
660
    check_lnk(lnkfile, &desc, 0x0);
661

662
    /* Test omitting .exe from an absolute path */
663
    p=strrchr(realpath, '.');
664 665 666 667 668
    if (p)
        *p='\0';

    desc.description="absolute path without .exe";
    desc.workdir=mydir;
669
    desc.path=realpath;
670 671 672 673 674 675 676
    desc.pidl=NULL;
    desc.arguments="/option1 /option2 \"Some string\"";
    desc.showcmd=SW_SHOWNORMAL;
    desc.icon=mypath;
    desc.icon_id=0;
    desc.hotkey=0x1234;
    create_lnk(lnkfile, &desc, 0);
677
    strcat(realpath, ".exe");
678 679
    check_lnk(lnkfile, &desc, 0x4);

680 681 682 683 684 685 686 687 688 689 690 691 692 693
    /* Overwrite the existing lnk file and test link to a command on the path */
    desc.description="command on path";
    desc.workdir=mypath;
    desc.path="rundll32.exe";
    desc.pidl=NULL;
    desc.arguments="/option1 /option2 \"Some string\"";
    desc.showcmd=SW_SHOWNORMAL;
    desc.icon=mypath;
    desc.icon_id=0;
    desc.hotkey=0x1234;
    create_lnk(lnkfile, &desc, 0);
    /* Check that link is created to proper location */
    SearchPathA( NULL, desc.path, NULL, MAX_PATH, realpath, NULL);
    desc.path=realpath;
694
    check_lnk(lnkfile, &desc, 0x0);
695

696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
    /* Test omitting .exe from a command on the path */
    desc.description="command on path without .exe";
    desc.workdir=mypath;
    desc.path="rundll32";
    desc.pidl=NULL;
    desc.arguments="/option1 /option2 \"Some string\"";
    desc.showcmd=SW_SHOWNORMAL;
    desc.icon=mypath;
    desc.icon_id=0;
    desc.hotkey=0x1234;
    create_lnk(lnkfile, &desc, 0);
    /* Check that link is created to proper location */
    SearchPathA( NULL, "rundll32", NULL, MAX_PATH, realpath, NULL);
    desc.path=realpath;
    check_lnk(lnkfile, &desc, 0x4);

712
    /* Create a temporary non-executable file */
713
    r=GetTempPathA(sizeof(mypath), mypath);
714
    ok(r<sizeof(mypath), "GetTempPath failed (%d), err %d\n", r, GetLastError());
715
    r=pGetLongPathNameA(mypath, mydir, sizeof(mydir));
716
    ok(r<sizeof(mydir), "GetLongPathName failed (%d), err %d\n", r, GetLastError());
717 718 719 720 721 722
    p=strrchr(mydir, '\\');
    if (p)
        *p='\0';

    strcpy(mypath, mydir);
    strcat(mypath, "\\test.txt");
723 724
    hf = CreateFileA(mypath, GENERIC_WRITE, 0, NULL,
                     CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
725 726 727 728 729 730 731 732 733 734 735 736 737
    CloseHandle(hf);

    /* Overwrite the existing lnk file and test link to an existing non-executable file */
    desc.description="non-executable file";
    desc.workdir=mydir;
    desc.path=mypath;
    desc.pidl=NULL;
    desc.arguments="";
    desc.showcmd=SW_SHOWNORMAL;
    desc.icon=mypath;
    desc.icon_id=0;
    desc.hotkey=0x1234;
    create_lnk(lnkfile, &desc, 0);
738
    check_lnk(lnkfile, &desc, 0x0);
739

740
    r=pGetShortPathNameA(mydir, mypath, sizeof(mypath));
741 742
    ok(r<sizeof(mypath), "GetShortPathName failed (%d), err %d\n", r, GetLastError());

743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
    strcpy(realpath, mypath);
    strcat(realpath, "\\test.txt");
    strcat(mypath, "\\\\test.txt");

    /* Overwrite the existing lnk file and test link to a short path with double backslashes */
    desc.description="non-executable file";
    desc.workdir=mydir;
    desc.path=mypath;
    desc.pidl=NULL;
    desc.arguments="";
    desc.showcmd=SW_SHOWNORMAL;
    desc.icon=mypath;
    desc.icon_id=0;
    desc.hotkey=0x1234;
    create_lnk(lnkfile, &desc, 0);
    desc.path=realpath;
    check_lnk(lnkfile, &desc, 0x0);

761 762 763
    r = DeleteFileA(mypath);
    ok(r, "failed to delete file %s (%d)\n", mypath, GetLastError());

764 765 766
    /* Create a temporary .bat file */
    strcpy(mypath, mydir);
    strcat(mypath, "\\test.bat");
767 768
    hf = CreateFileA(mypath, GENERIC_WRITE, 0, NULL,
                     CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
    CloseHandle(hf);

    strcpy(realpath, mypath);

    p=strrchr(mypath, '.');
    if (p)
        *p='\0';

    /* Try linking to the .bat file without the extension */
    desc.description="batch file";
    desc.workdir=mydir;
    desc.path=mypath;
    desc.pidl=NULL;
    desc.arguments="";
    desc.showcmd=SW_SHOWNORMAL;
    desc.icon=mypath;
    desc.icon_id=0;
    desc.hotkey=0x1234;
    create_lnk(lnkfile, &desc, 0);
    desc.path = realpath;
    check_lnk(lnkfile, &desc, 0x4);

    r = DeleteFileA(realpath);
792
    ok(r, "failed to delete file %s (%d)\n", realpath, GetLastError());
793

794 795 796 797 798
    /* FIXME: Also test saving a .lnk pointing to a pidl that cannot be
     * represented as a path.
     */

    /* DeleteFileW is not implemented on Win9x */
799 800
    r=DeleteFileA(lnkfileA);
    ok(r, "failed to delete link '%s' (%d)\n", lnkfileA, GetLastError());
801 802
}

803 804 805
static void test_datalink(void)
{
    static const WCHAR lnk[] = {
806
      ':',':','{','9','d','b','1','1','8','6','e','-','4','0','d','f','-','1',
807
      '1','d','1','-','a','a','8','c','-','0','0','c','0','4','f','b','6','7',
808 809 810 811
      '8','6','3','}',':','2','6',',','!','!','g','x','s','f','(','N','g',']',
      'q','F','`','H','{','L','s','A','C','C','E','S','S','F','i','l','e','s',
      '>','p','l','T',']','j','I','{','j','f','(','=','1','&','L','[','-','8',
      '1','-',']',':',':',0 };
812 813 814 815 816 817 818 819 820 821 822 823
    static const WCHAR comp[] = {
      '2','6',',','!','!','g','x','s','f','(','N','g',']','q','F','`','H','{',
      'L','s','A','C','C','E','S','S','F','i','l','e','s','>','p','l','T',']',
      'j','I','{','j','f','(','=','1','&','L','[','-','8','1','-',']',0 };
    IShellLinkDataList *dl = NULL;
    IShellLinkW *sl = NULL;
    HRESULT r;
    DWORD flags = 0;
    EXP_DARWIN_LINK *dar;

    r = CoCreateInstance( &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
                            &IID_IShellLinkW, (LPVOID*)&sl );
824 825 826
    ok( r == S_OK ||
        broken(r == E_NOINTERFACE), /* Win9x */
        "CoCreateInstance failed (0x%08x)\n", r);
827
    if (!sl)
828
    {
829
        win_skip("no shelllink\n");
830
        return;
831
    }
832

833
    r = IShellLinkW_QueryInterface( sl, &_IID_IShellLinkDataList, (LPVOID*) &dl );
834 835 836
    ok( r == S_OK ||
        broken(r == E_NOINTERFACE), /* NT4 */
        "IShellLinkW_QueryInterface failed (0x%08x)\n", r);
837 838

    if (!dl)
839
    {
840
        win_skip("no datalink interface\n");
841
        IShellLinkW_Release( sl );
842
        return;
843
    }
844 845

    flags = 0;
846
    r = IShellLinkDataList_GetFlags( dl, &flags );
847 848 849 850
    ok( r == S_OK, "GetFlags failed\n");
    ok( flags == 0, "GetFlags returned wrong flags\n");

    dar = (void*)-1;
851
    r = IShellLinkDataList_CopyDataBlock( dl, EXP_DARWIN_ID_SIG, (LPVOID*) &dar );
852 853 854
    ok( r == E_FAIL, "CopyDataBlock failed\n");
    ok( dar == NULL, "should be null\n");

855 856 857 858 859 860 861
    if (!pGetLongPathNameA /* NT4 */)
        skip("SetPath with NULL parameter crashes on NT4\n");
    else
    {
        r = IShellLinkW_SetPath(sl, NULL);
        ok(r == E_INVALIDARG, "SetPath returned wrong error (0x%08x)\n", r);
    }
862

863
    r = IShellLinkW_SetPath(sl, lnk);
864
    ok(r == S_OK, "SetPath failed\n");
865

866 867 868
if (0)
{
    /* the following crashes */
869
    IShellLinkDataList_GetFlags( dl, NULL );
870
}
871 872

    flags = 0;
873
    r = IShellLinkDataList_GetFlags( dl, &flags );
874
    ok( r == S_OK, "GetFlags failed\n");
875 876
    /* SLDF_HAS_LOGO3ID is no longer supported on Vista+, filter it out */
    ok( (flags & (~ SLDF_HAS_LOGO3ID)) == SLDF_HAS_DARWINID,
877 878 879
        "GetFlags returned wrong flags\n");

    dar = NULL;
880
    r = IShellLinkDataList_CopyDataBlock( dl, EXP_DARWIN_ID_SIG, (LPVOID*) &dar );
881 882
    ok( r == S_OK, "CopyDataBlock failed\n");

883
    ok( dar && ((DATABLOCK_HEADER*)dar)->dwSignature == EXP_DARWIN_ID_SIG, "signature wrong\n");
884 885 886 887
    ok( dar && 0==lstrcmpW(dar->szwDarwinID, comp ), "signature wrong\n");

    LocalFree( dar );

888
    IShellLinkDataList_Release( dl );
889 890 891
    IShellLinkW_Release( sl );
}

892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
static void test_shdefextracticon(void)
{
    HICON hiconlarge=NULL, hiconsmall=NULL;
    HRESULT res;

    if (!pSHDefExtractIconA)
    {
        win_skip("SHDefExtractIconA is unavailable\n");
        return;
    }

    res = pSHDefExtractIconA("shell32.dll", 0, 0, &hiconlarge, &hiconsmall, MAKELONG(16,24));
    ok(SUCCEEDED(res), "SHDefExtractIconA failed, res=%x\n", res);
    ok(hiconlarge != NULL, "got null hiconlarge\n");
    ok(hiconsmall != NULL, "got null hiconsmall\n");
    DestroyIcon(hiconlarge);
    DestroyIcon(hiconsmall);

    hiconsmall = NULL;
    res = pSHDefExtractIconA("shell32.dll", 0, 0, NULL, &hiconsmall, MAKELONG(16,24));
    ok(SUCCEEDED(res), "SHDefExtractIconA failed, res=%x\n", res);
    ok(hiconsmall != NULL, "got null hiconsmall\n");
    DestroyIcon(hiconsmall);

    res = pSHDefExtractIconA("shell32.dll", 0, 0, NULL, NULL, MAKELONG(16,24));
    ok(SUCCEEDED(res), "SHDefExtractIconA failed, res=%x\n", res);
}

920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
static void test_GetIconLocation(void)
{
    IShellLinkA *sl;
    const char *str;
    char buffer[INFOTIPSIZE], mypath[MAX_PATH];
    int i;
    HRESULT r;
    LPITEMIDLIST pidl;

    r = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
            &IID_IShellLinkA, (LPVOID*)&sl);
    ok(r == S_OK, "no IID_IShellLinkA (0x%08x)\n", r);
    if(r != S_OK)
        return;

    i = 0xdeadbeef;
    strcpy(buffer, "garbage");
    r = IShellLinkA_GetIconLocation(sl, buffer, sizeof(buffer), &i);
    ok(r == S_OK, "GetIconLocation failed (0x%08x)\n", r);
    ok(*buffer == '\0', "GetIconLocation returned '%s'\n", buffer);
    ok(i == 0, "GetIconLocation returned %d\n", i);

    str = "c:\\some\\path";
    r = IShellLinkA_SetPath(sl, str);
    ok(r == S_FALSE || r == S_OK, "SetPath failed (0x%08x)\n", r);

    i = 0xdeadbeef;
    strcpy(buffer, "garbage");
    r = IShellLinkA_GetIconLocation(sl, buffer, sizeof(buffer), &i);
    ok(r == S_OK, "GetIconLocation failed (0x%08x)\n", r);
    ok(*buffer == '\0', "GetIconLocation returned '%s'\n", buffer);
    ok(i == 0, "GetIconLocation returned %d\n", i);

    GetWindowsDirectoryA(mypath, sizeof(mypath) - 12);
    strcat(mypath, "\\regedit.exe");
    pidl = path_to_pidl(mypath);
    r = IShellLinkA_SetIDList(sl, pidl);
    ok(r == S_OK, "SetPath failed (0x%08x)\n", r);
958
    pILFree(pidl);
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973

    i = 0xdeadbeef;
    strcpy(buffer, "garbage");
    r = IShellLinkA_GetIconLocation(sl, buffer, sizeof(buffer), &i);
    ok(r == S_OK, "GetIconLocation failed (0x%08x)\n", r);
    ok(*buffer == '\0', "GetIconLocation returned '%s'\n", buffer);
    ok(i == 0, "GetIconLocation returned %d\n", i);

    str = "c:\\nonexistent\\file";
    r = IShellLinkA_SetIconLocation(sl, str, 0xbabecafe);
    ok(r == S_OK, "SetIconLocation failed (0x%08x)\n", r);

    i = 0xdeadbeef;
    r = IShellLinkA_GetIconLocation(sl, buffer, sizeof(buffer), &i);
    ok(r == S_OK, "GetIconLocation failed (0x%08x)\n", r);
974
    ok(lstrcmpiA(buffer,str) == 0, "GetIconLocation returned '%s'\n", buffer);
975 976 977 978 979
    ok(i == 0xbabecafe, "GetIconLocation returned %d'\n", i);

    IShellLinkA_Release(sl);
}

980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
static void test_SHGetStockIconInfo(void)
{
    BYTE buffer[sizeof(SHSTOCKICONINFO) + 16];
    SHSTOCKICONINFO *sii = (SHSTOCKICONINFO *) buffer;
    HRESULT hr;
    INT i;

    /* not present before vista */
    if (!pSHGetStockIconInfo)
    {
        win_skip("SHGetStockIconInfo not available\n");
        return;
    }

    /* negative values are handled */
    memset(buffer, '#', sizeof(buffer));
    sii->cbSize = sizeof(SHSTOCKICONINFO);
997
    hr = pSHGetStockIconInfo(SIID_INVALID, SHGSI_ICONLOCATION, sii);
998 999 1000
    ok(hr == E_INVALIDARG, "-1: got 0x%x (expected E_INVALIDARG)\n", hr);

    /* max. id for vista is 140 (no definition exists for this value) */
1001
    for (i = SIID_DOCNOASSOC; i <= SIID_CLUSTEREDDRIVE; i++)
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
    {
        memset(buffer, '#', sizeof(buffer));
        sii->cbSize = sizeof(SHSTOCKICONINFO);
        hr = pSHGetStockIconInfo(i, SHGSI_ICONLOCATION, sii);

        ok(hr == S_OK,
            "%3d: got 0x%x, iSysImageIndex: 0x%x, iIcon: 0x%x (expected S_OK)\n",
            i, hr, sii->iSysImageIndex, sii->iIcon);

        if ((hr == S_OK) && (winetest_debug > 1))
            trace("%3d: got iSysImageIndex %3d, iIcon %3d and %s\n", i, sii->iSysImageIndex,
                  sii->iIcon, wine_dbgstr_w(sii->szPath));
    }

1016 1017
    /* test invalid icons indices that are invalid for all platforms */
    for (i = SIID_MAX_ICONS; i < (SIID_MAX_ICONS + 25) ; i++)
1018 1019 1020 1021
    {
        memset(buffer, '#', sizeof(buffer));
        sii->cbSize = sizeof(SHSTOCKICONINFO);
        hr = pSHGetStockIconInfo(i, SHGSI_ICONLOCATION, sii);
1022 1023 1024 1025 1026
        ok(hr == E_INVALIDARG, "%3d: got 0x%x (expected E_INVALIDARG)\n", i, hr);
    todo_wine {
        ok(sii->iSysImageIndex == -1, "%d: got iSysImageIndex %d\n", i, sii->iSysImageIndex);
        ok(sii->iIcon == -1, "%d: got iIcon %d\n", i, sii->iIcon);
    }
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
    }

    /* test more returned SHSTOCKICONINFO elements without extra flags */
    memset(buffer, '#', sizeof(buffer));
    sii->cbSize = sizeof(SHSTOCKICONINFO);
    hr = pSHGetStockIconInfo(SIID_FOLDER, SHGSI_ICONLOCATION, sii);
    ok(hr == S_OK, "got 0x%x (expected S_OK)\n", hr);
    ok(!sii->hIcon, "got %p (expected NULL)\n", sii->hIcon);
    ok(sii->iSysImageIndex == -1, "got %d (expected -1)\n", sii->iSysImageIndex);

    /* the exact size is required of the struct */
    memset(buffer, '#', sizeof(buffer));
    sii->cbSize = sizeof(SHSTOCKICONINFO) + 2;
    hr = pSHGetStockIconInfo(SIID_FOLDER, SHGSI_ICONLOCATION, sii);
    ok(hr == E_INVALIDARG, "+2: got 0x%x, iSysImageIndex: 0x%x, iIcon: 0x%x\n", hr, sii->iSysImageIndex, sii->iIcon);

    memset(buffer, '#', sizeof(buffer));
    sii->cbSize = sizeof(SHSTOCKICONINFO) + 1;
    hr = pSHGetStockIconInfo(SIID_FOLDER, SHGSI_ICONLOCATION, sii);
    ok(hr == E_INVALIDARG, "+1: got 0x%x, iSysImageIndex: 0x%x, iIcon: 0x%x\n", hr, sii->iSysImageIndex, sii->iIcon);

    memset(buffer, '#', sizeof(buffer));
    sii->cbSize = sizeof(SHSTOCKICONINFO) - 1;
    hr = pSHGetStockIconInfo(SIID_FOLDER, SHGSI_ICONLOCATION, sii);
    ok(hr == E_INVALIDARG, "-1: got 0x%x, iSysImageIndex: 0x%x, iIcon: 0x%x\n", hr, sii->iSysImageIndex, sii->iIcon);

    memset(buffer, '#', sizeof(buffer));
    sii->cbSize = sizeof(SHSTOCKICONINFO) - 2;
    hr = pSHGetStockIconInfo(SIID_FOLDER, SHGSI_ICONLOCATION, sii);
    ok(hr == E_INVALIDARG, "-2: got 0x%x, iSysImageIndex: 0x%x, iIcon: 0x%x\n", hr, sii->iSysImageIndex, sii->iIcon);

    /* there is a NULL check for the struct  */
    hr = pSHGetStockIconInfo(SIID_FOLDER, SHGSI_ICONLOCATION, NULL);
    ok(hr == E_INVALIDARG, "NULL: got 0x%x\n", hr);
}

1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
static void test_SHExtractIcons(void)
{
    static const WCHAR notepadW[] = {'n','o','t','e','p','a','d','.','e','x','e',0};
    static const WCHAR shell32W[] = {'s','h','e','l','l','3','2','.','d','l','l',0};
    static const WCHAR emptyW[] = {0};
    UINT ret, ret2;
    HICON icons[256];
    UINT ids[256], i;

    if (!pSHExtractIconsW)
    {
        win_skip("SHExtractIconsW not available\n");
        return;
    }

    ret = pSHExtractIconsW(emptyW, 0, 16, 16, icons, ids, 1, 0);
    ok(ret == ~0u, "got %u\n", ret);

    ret = pSHExtractIconsW(notepadW, 0, 16, 16, NULL, NULL, 1, 0);
    ok(ret == 1 || broken(ret == 2) /* win2k */, "got %u\n", ret);

    icons[0] = (HICON)0xdeadbeef;
    ret = pSHExtractIconsW(notepadW, 0, 16, 16, icons, NULL, 1, 0);
    ok(ret == 1, "got %u\n", ret);
    ok(icons[0] != (HICON)0xdeadbeef, "icon not set\n");
    DestroyIcon(icons[0]);

    icons[0] = (HICON)0xdeadbeef;
    ids[0] = 0xdeadbeef;
    ret = pSHExtractIconsW(notepadW, 0, 16, 16, icons, ids, 1, 0);
    ok(ret == 1, "got %u\n", ret);
    ok(icons[0] != (HICON)0xdeadbeef, "icon not set\n");
    ok(ids[0] != 0xdeadbeef, "id not set\n");
    DestroyIcon(icons[0]);

    ret = pSHExtractIconsW(shell32W, 0, 16, 16, NULL, NULL, 0, 0);
    ret2 = pSHExtractIconsW(shell32W, 4, MAKELONG(32,16), MAKELONG(32,16), NULL, NULL, 256, 0);
    ok(ret && ret == ret2,
       "icon count should be independent of requested icon sizes and base icon index\n");

    ret = pSHExtractIconsW(shell32W, 0, 16, 16, icons, ids, 0, 0);
    ok(ret == ~0u || !ret /* < vista */, "got %u\n", ret);

    ret = pSHExtractIconsW(shell32W, 0, 16, 16, icons, ids, 3, 0);
    ok(ret == 3, "got %u\n", ret);
    for (i = 0; i < ret; i++) DestroyIcon(icons[i]);

    /* count must be a multiple of two when getting two sizes */
    ret = pSHExtractIconsW(shell32W, 0, MAKELONG(16,32), MAKELONG(16,32), icons, ids, 3, 0);
    ok(!ret /* vista */ || ret == 4, "got %u\n", ret);
    for (i = 0; i < ret; i++) DestroyIcon(icons[i]);

    ret = pSHExtractIconsW(shell32W, 0, MAKELONG(16,32), MAKELONG(16,32), icons, ids, 4, 0);
    ok(ret == 4, "got %u\n", ret);
    for (i = 0; i < ret; i++) DestroyIcon(icons[i]);
}

1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
static void test_propertystore(void)
{
    IShellLinkA *linkA;
    IShellLinkW *linkW;
    IPropertyStore *ps;
    HRESULT hr;

    hr = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
                         &IID_IShellLinkA, (void**)&linkA);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    hr = IShellLinkA_QueryInterface(linkA, &IID_IShellLinkW, (void**)&linkW);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    hr = IShellLinkA_QueryInterface(linkA, &IID_IPropertyStore, (void**)&ps);
    if (hr == S_OK) {
        IPropertyStoreCache *pscache;

        IPropertyStore_Release(ps);

        hr = IShellLinkW_QueryInterface(linkW, &IID_IPropertyStore, (void**)&ps);
        ok(hr == S_OK, "got 0x%08x\n", hr);

        hr = IPropertyStore_QueryInterface(ps, &IID_IPropertyStoreCache, (void**)&pscache);
        ok(hr == E_NOINTERFACE, "got 0x%08x\n", hr);

        IPropertyStore_Release(ps);
    }
    else
        win_skip("IShellLink doesn't support IPropertyStore.\n");

    IShellLinkA_Release(linkA);
    IShellLinkW_Release(linkW);
}

1155 1156 1157 1158 1159 1160 1161 1162 1163
static void test_ExtractIcon(void)
{
    static const WCHAR nameW[] = {'\\','e','x','t','r','a','c','t','i','c','o','n','_','t','e','s','t','.','t','x','t',0};
    static const WCHAR shell32W[] = {'s','h','e','l','l','3','2','.','d','l','l',0};
    WCHAR pathW[MAX_PATH];
    HICON hicon, hicon2;
    char path[MAX_PATH];
    HANDLE file;
    int r;
1164 1165
    ICONINFO info;
    BITMAP bm;
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220

    /* specified instance handle */
    hicon = ExtractIconA(GetModuleHandleA("shell32.dll"), NULL, 0);
todo_wine
    ok(hicon == NULL, "Got icon %p\n", hicon);
    hicon2 = ExtractIconA(GetModuleHandleA("shell32.dll"), "shell32.dll", -1);
    ok(hicon2 != NULL, "Got icon %p\n", hicon2);

    /* existing index */
    hicon = ExtractIconA(NULL, "shell32.dll", 0);
    ok(hicon != NULL && HandleToLong(hicon) != -1, "Got icon %p\n", hicon);
    DestroyIcon(hicon);

    /* returns number of resources */
    hicon = ExtractIconA(NULL, "shell32.dll", -1);
    ok(HandleToLong(hicon) > 1 && hicon == hicon2, "Got icon %p\n", hicon);

    /* invalid index, valid dll name */
    hicon = ExtractIconA(NULL, "shell32.dll", 3000);
    ok(hicon == NULL, "Got icon %p\n", hicon);

    /* Create a temporary non-executable file */
    GetTempPathA(sizeof(path), path);
    strcat(path, "\\extracticon_test.txt");
    file = CreateFileA(path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    ok(file != INVALID_HANDLE_VALUE, "Failed to create a test file\n");
    CloseHandle(file);

    hicon = ExtractIconA(NULL, path, 0);
todo_wine
    ok(hicon == NULL, "Got icon %p\n", hicon);

    hicon = ExtractIconA(NULL, path, -1);
    ok(hicon == NULL, "Got icon %p\n", hicon);

    hicon = ExtractIconA(NULL, path, 1);
todo_wine
    ok(hicon == NULL, "Got icon %p\n", hicon);

    r = DeleteFileA(path);
    ok(r, "failed to delete file %s (%d)\n", path, GetLastError());

    /* same for W variant */
if (0)
{
    /* specified instance handle, crashes on XP, 2k3 */
    hicon = ExtractIconW(GetModuleHandleA("shell32.dll"), NULL, 0);
    ok(hicon == NULL, "Got icon %p\n", hicon);
}
    hicon2 = ExtractIconW(GetModuleHandleA("shell32.dll"), shell32W, -1);
    ok(hicon2 != NULL, "Got icon %p\n", hicon2);

    /* existing index */
    hicon = ExtractIconW(NULL, shell32W, 0);
    ok(hicon != NULL && HandleToLong(hicon) != -1, "Got icon %p\n", hicon);
1221 1222 1223 1224
    GetIconInfo(hicon, &info);
    GetObjectW(info.hbmColor, sizeof(bm), &bm);
    ok(bm.bmWidth == GetSystemMetrics(SM_CXICON), "got %d\n", bm.bmWidth);
    ok(bm.bmHeight == GetSystemMetrics(SM_CYICON), "got %d\n", bm.bmHeight);
1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256
    DestroyIcon(hicon);

    /* returns number of resources */
    hicon = ExtractIconW(NULL, shell32W, -1);
    ok(HandleToLong(hicon) > 1 && hicon == hicon2, "Got icon %p\n", hicon);

    /* invalid index, valid dll name */
    hicon = ExtractIconW(NULL, shell32W, 3000);
    ok(hicon == NULL, "Got icon %p\n", hicon);

    /* Create a temporary non-executable file */
    GetTempPathW(sizeof(pathW)/sizeof(pathW[0]), pathW);
    lstrcatW(pathW, nameW);
    file = CreateFileW(pathW, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    ok(file != INVALID_HANDLE_VALUE, "Failed to create a test file\n");
    CloseHandle(file);

    hicon = ExtractIconW(NULL, pathW, 0);
todo_wine
    ok(hicon == NULL, "Got icon %p\n", hicon);

    hicon = ExtractIconW(NULL, pathW, -1);
    ok(hicon == NULL, "Got icon %p\n", hicon);

    hicon = ExtractIconW(NULL, pathW, 1);
todo_wine
    ok(hicon == NULL, "Got icon %p\n", hicon);

    r = DeleteFileW(pathW);
    ok(r, "failed to delete file %s (%d)\n", path, GetLastError());
}

1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286
static void test_ExtractAssociatedIcon(void)
{
    char pathA[MAX_PATH];
    HICON hicon;
    WORD index;

    /* empty path */
    index = 0;
    *pathA = 0;
    hicon = ExtractAssociatedIconA(NULL, pathA, &index);
todo_wine {
    ok(hicon != NULL, "Got icon %p\n", hicon);
    ok(!*pathA, "Unexpected path %s\n", pathA);
    ok(index == 0, "Unexpected index %u\n", index);
}
    DestroyIcon(hicon);

    /* by index */
    index = 0;
    strcpy(pathA, "shell32.dll");
    hicon = ExtractAssociatedIconA(NULL, pathA, &index);
    ok(hicon != NULL, "Got icon %p\n", hicon);
    ok(!strcmp(pathA, "shell32.dll"), "Unexpected path %s\n", pathA);
    ok(index == 0, "Unexpected index %u\n", index);
    DestroyIcon(hicon);

    /* valid dll name, invalid index */
    index = 5000;
    strcpy(pathA, "user32.dll");
    hicon = ExtractAssociatedIconA(NULL, pathA, &index);
1287
    CharLowerBuffA(pathA, strlen(pathA));
1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
todo_wine {
    ok(hicon != NULL, "Got icon %p\n", hicon);
    ok(!!strstr(pathA, "shell32.dll"), "Unexpected path %s\n", pathA);
}
    ok(index != 5000, "Unexpected index %u\n", index);
    DestroyIcon(hicon);

    /* associated icon */
    index = 0xcaca;
    strcpy(pathA, "dummy.exe");
    hicon = ExtractAssociatedIconA(NULL, pathA, &index);
1299
    CharLowerBuffA(pathA, strlen(pathA));
1300 1301 1302 1303 1304 1305 1306 1307
todo_wine {
    ok(hicon != NULL, "Got icon %p\n", hicon);
    ok(!!strstr(pathA, "shell32.dll"), "Unexpected path %s\n", pathA);
}
    ok(index != 0xcaca, "Unexpected index %u\n", index);
    DestroyIcon(hicon);
}

1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322
static int get_shell_icon_size(void)
{
    char buf[10];
    DWORD value = 32, size = sizeof(buf), type;
    HKEY key;

    if (!RegOpenKeyA( HKEY_CURRENT_USER, "Control Panel\\Desktop\\WindowMetrics", &key ))
    {
        if (!RegQueryValueExA( key, "Shell Icon Size", NULL, &type, (BYTE *)buf, &size ) && type == REG_SZ)
            value = atoi( buf );
        RegCloseKey( key );
    }
    return value;
}

1323 1324 1325 1326 1327 1328 1329
static void test_SHGetImageList(void)
{
    HRESULT hr;
    IImageList *list, *list2;
    BOOL ret;
    HIMAGELIST lg, sm;
    ULONG start_refs, refs;
1330 1331
    int i, width, height, expect;
    BOOL dpi_aware = pIsProcessDPIAware && pIsProcessDPIAware();
1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360

    hr = SHGetImageList( SHIL_LARGE, &IID_IImageList, (void **)&list );
    ok( hr == S_OK, "got %08x\n", hr );
    start_refs = IImageList_AddRef( list );
    IImageList_Release( list );

    hr = SHGetImageList( SHIL_LARGE, &IID_IImageList, (void **)&list2 );
    ok( hr == S_OK, "got %08x\n", hr );
    ok( list == list2, "lists differ\n" );
    refs = IImageList_AddRef( list );
    IImageList_Release( list );
    ok( refs == start_refs + 1, "got %d, start_refs %d\n", refs, start_refs );
    IImageList_Release( list2 );

    hr = SHGetImageList( SHIL_SMALL, &IID_IImageList, (void **)&list2 );
    ok( hr == S_OK, "got %08x\n", hr );

    ret = Shell_GetImageLists( &lg, &sm );
    ok( ret, "got %d\n", ret );
    ok( lg == (HIMAGELIST)list, "mismatch\n" );
    ok( sm == (HIMAGELIST)list2, "mismatch\n" );

    /* Shell_GetImageLists doesn't take a reference */
    refs = IImageList_AddRef( list );
    IImageList_Release( list );
    ok( refs == start_refs, "got %d, start_refs %d\n", refs, start_refs );

    IImageList_Release( list2 );
    IImageList_Release( list );
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398

    /* Test the icon sizes */
    for (i = 0; i <= SHIL_LAST; i++)
    {
        hr = SHGetImageList( i, &IID_IImageList, (void **)&list );
        todo_wine_if(i == SHIL_EXTRALARGE || i == SHIL_JUMBO)
            ok( hr == S_OK ||
                broken( i == SHIL_JUMBO && hr == E_INVALIDARG ), /* XP and 2003 */
                "%d: got %08x\n", i, hr );
        if (FAILED(hr)) continue;
        IImageList_GetIconSize( list, &width, &height );
        switch (i)
        {
        case SHIL_LARGE:
            if (dpi_aware) expect = GetSystemMetrics( SM_CXICON );
            else expect = get_shell_icon_size();
            break;
        case SHIL_SMALL:
            if (dpi_aware) expect = GetSystemMetrics( SM_CXICON ) / 2;
            else expect = GetSystemMetrics( SM_CXSMICON );
            break;
        case SHIL_EXTRALARGE:
            expect = (GetSystemMetrics( SM_CXICON ) * 3) / 2;
            break;
        case SHIL_SYSSMALL:
            expect = GetSystemMetrics( SM_CXSMICON );
            break;
        case SHIL_JUMBO:
            expect = 256;
            break;
        }
        todo_wine_if(i == SHIL_SYSSMALL && dpi_aware && expect != GetSystemMetrics( SM_CXICON ) / 2)
        {
            ok( width == expect, "%d: got %d expect %d\n", i, width, expect );
            ok( height == expect, "%d: got %d expect %d\n", i, height, expect );
        }
        IImageList_Release( list );
    }
1399 1400
}

1401 1402
START_TEST(shelllink)
{
1403
    HRESULT r;
1404 1405
    HMODULE hmod = GetModuleHandleA("shell32.dll");
    HMODULE hkernel32 = GetModuleHandleA("kernel32.dll");
1406
    HMODULE huser32 = GetModuleHandleA("user32.dll");
1407

1408 1409 1410 1411
    pILFree = (void *)GetProcAddress(hmod, (LPSTR)155);
    pILIsEqual = (void *)GetProcAddress(hmod, (LPSTR)21);
    pSHILCreateFromPath = (void *)GetProcAddress(hmod, (LPSTR)28);
    pSHDefExtractIconA = (void *)GetProcAddress(hmod, "SHDefExtractIconA");
1412
    pSHGetStockIconInfo = (void *)GetProcAddress(hmod, "SHGetStockIconInfo");
1413
    pGetLongPathNameA = (void *)GetProcAddress(hkernel32, "GetLongPathNameA");
1414
    pGetShortPathNameA = (void *)GetProcAddress(hkernel32, "GetShortPathNameA");
1415
    pSHExtractIconsW = (void *)GetProcAddress(hmod, "SHExtractIconsW");
1416
    pIsProcessDPIAware = (void *)GetProcAddress(huser32, "IsProcessDPIAware");
1417

1418
    r = CoInitialize(NULL);
1419 1420
    ok(r == S_OK, "CoInitialize failed (0x%08x)\n", r);
    if (r != S_OK)
1421 1422 1423 1424
        return;

    test_get_set();
    test_load_save();
1425
    test_datalink();
1426
    test_shdefextracticon();
1427
    test_GetIconLocation();
1428
    test_SHGetStockIconInfo();
1429
    test_SHExtractIcons();
1430
    test_propertystore();
1431
    test_ExtractIcon();
1432
    test_ExtractAssociatedIcon();
1433
    test_SHGetImageList();
1434 1435

    CoUninitialize();
1436
}