shlfolder.c 175 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Unit test of the IShellFolder functions.
 *
 * Copyright 2004 Vitaliy Margolen
 *
 * 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
 */

#include <stdarg.h>
#include <stdio.h>

24
#define COBJMACROS
25
#define CONST_VTABLE
26

27 28 29 30 31 32 33 34 35
#include "windef.h"
#include "winbase.h"
#include "wtypes.h"
#include "shellapi.h"


#include "shlguid.h"
#include "shlobj.h"
#include "shobjidl.h"
36
#include "shlwapi.h"
37 38
#include "ocidl.h"
#include "oleauto.h"
39 40 41

#include "wine/test.h"

42 43
#include <initguid.h>
DEFINE_GUID(IID_IParentAndItem, 0xB3A4B685, 0xB685, 0x4805, 0x99,0xD9, 0x5D,0xEA,0xD2,0x87,0x32,0x36);
44
DEFINE_GUID(CLSID_ShellDocObjView, 0xe7e4bc40, 0xe76a, 0x11ce, 0xa9,0xbb, 0x00,0xaa,0x00,0x4a,0xe8,0x37);
45

46
static IMalloc *ppM;
47

48
static HRESULT (WINAPI *pSHBindToParent)(LPCITEMIDLIST, REFIID, LPVOID*, LPCITEMIDLIST*);
49 50
static HRESULT (WINAPI *pSHGetFolderPathA)(HWND, int, HANDLE, DWORD, LPSTR);
static HRESULT (WINAPI *pSHGetFolderPathAndSubDirA)(HWND, int, HANDLE, DWORD, LPCSTR, LPSTR);
51
static BOOL (WINAPI *pSHGetPathFromIDListW)(LPCITEMIDLIST,LPWSTR);
52
static HRESULT (WINAPI *pSHGetSpecialFolderLocation)(HWND, int, LPITEMIDLIST *);
53
static BOOL (WINAPI *pSHGetSpecialFolderPathA)(HWND, LPSTR, int, BOOL);
54
static BOOL (WINAPI *pSHGetSpecialFolderPathW)(HWND, LPWSTR, int, BOOL);
55
static HRESULT (WINAPI *pStrRetToBufW)(STRRET*,LPCITEMIDLIST,LPWSTR,UINT);
56
static LPITEMIDLIST (WINAPI *pILFindLastID)(LPCITEMIDLIST);
57 58
static void (WINAPI *pILFree)(LPITEMIDLIST);
static BOOL (WINAPI *pILIsEqual)(LPCITEMIDLIST, LPCITEMIDLIST);
59
static HRESULT (WINAPI *pSHCreateItemFromIDList)(PCIDLIST_ABSOLUTE pidl, REFIID riid, void **ppv);
60
static HRESULT (WINAPI *pSHCreateItemFromParsingName)(PCWSTR,IBindCtx*,REFIID,void**);
61
static HRESULT (WINAPI *pSHCreateShellItem)(LPCITEMIDLIST,IShellFolder*,LPCITEMIDLIST,IShellItem**);
62
static HRESULT (WINAPI *pSHCreateShellItemArray)(LPCITEMIDLIST,IShellFolder*,UINT,LPCITEMIDLIST*,IShellItemArray**);
63
static HRESULT (WINAPI *pSHCreateShellItemArrayFromDataObject)(IDataObject*, REFIID, void **);
64
static HRESULT (WINAPI *pSHCreateShellItemArrayFromShellItem)(IShellItem*, REFIID, void **);
65
static LPITEMIDLIST (WINAPI *pILCombine)(LPCITEMIDLIST,LPCITEMIDLIST);
66
static HRESULT (WINAPI *pSHParseDisplayName)(LPCWSTR,IBindCtx*,LPITEMIDLIST*,SFGAOF,SFGAOF*);
67
static LPITEMIDLIST (WINAPI *pSHSimpleIDListFromPathAW)(LPCVOID);
68
static HRESULT (WINAPI *pSHGetNameFromIDList)(PCIDLIST_ABSOLUTE,SIGDN,PWSTR*);
69
static HRESULT (WINAPI *pSHGetItemFromDataObject)(IDataObject*,DATAOBJ_GET_ITEM_FLAGS,REFIID,void**);
70
static HRESULT (WINAPI *pSHGetIDListFromObject)(IUnknown*, PIDLIST_ABSOLUTE*);
71
static HRESULT (WINAPI *pSHGetItemFromObject)(IUnknown*,REFIID,void**);
72
static BOOL (WINAPI *pIsWow64Process)(HANDLE, PBOOL);
73
static UINT (WINAPI *pGetSystemWow64DirectoryW)(LPWSTR, UINT);
74
static HRESULT (WINAPI *pSHCreateDefaultContextMenu)(const DEFCONTEXTMENU*,REFIID,void**);
75

76 77 78 79 80 81 82 83 84 85 86 87
static const char *debugstr_guid(REFIID riid)
{
    static char buf[50];

    sprintf(buf, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
            riid->Data1, riid->Data2, riid->Data3, riid->Data4[0],
            riid->Data4[1], riid->Data4[2], riid->Data4[3], riid->Data4[4],
            riid->Data4[5], riid->Data4[6], riid->Data4[7]);

    return buf;
}

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
static WCHAR *make_wstr(const char *str)
{
    WCHAR *ret;
    int len;

    if(!str || strlen(str) == 0)
        return NULL;

    len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
    if(!len || len < 0)
        return NULL;

    ret = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
    if(!ret)
        return NULL;

    MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len);
    return ret;
}

108 109 110 111 112 113 114
static int strcmp_wa(LPCWSTR strw, const char *stra)
{
    CHAR buf[512];
    WideCharToMultiByte(CP_ACP, 0, strw, -1, buf, sizeof(buf), NULL, NULL);
    return lstrcmpA(stra, buf);
}

115 116
static void init_function_pointers(void)
{
117
    HMODULE hmod;
118
    HRESULT hr;
119
    void *ptr;
120

121
    hmod = GetModuleHandleA("shell32.dll");
122 123 124

#define MAKEFUNC(f) (p##f = (void*)GetProcAddress(hmod, #f))
    MAKEFUNC(SHBindToParent);
125
    MAKEFUNC(SHCreateItemFromIDList);
126
    MAKEFUNC(SHCreateItemFromParsingName);
127
    MAKEFUNC(SHCreateShellItem);
128
    MAKEFUNC(SHCreateShellItemArray);
129
    MAKEFUNC(SHCreateShellItemArrayFromDataObject);
130
    MAKEFUNC(SHCreateShellItemArrayFromShellItem);
131 132 133 134 135
    MAKEFUNC(SHGetFolderPathA);
    MAKEFUNC(SHGetFolderPathAndSubDirA);
    MAKEFUNC(SHGetPathFromIDListW);
    MAKEFUNC(SHGetSpecialFolderPathA);
    MAKEFUNC(SHGetSpecialFolderPathW);
136
    MAKEFUNC(SHGetSpecialFolderLocation);
137
    MAKEFUNC(SHParseDisplayName);
138
    MAKEFUNC(SHGetNameFromIDList);
139
    MAKEFUNC(SHGetItemFromDataObject);
140
    MAKEFUNC(SHGetIDListFromObject);
141
    MAKEFUNC(SHGetItemFromObject);
142
    MAKEFUNC(SHCreateDefaultContextMenu);
143 144 145 146 147 148 149
#undef MAKEFUNC

#define MAKEFUNC_ORD(f, ord) (p##f = (void*)GetProcAddress(hmod, (LPSTR)(ord)))
    MAKEFUNC_ORD(ILFindLastID, 16);
    MAKEFUNC_ORD(ILIsEqual, 21);
    MAKEFUNC_ORD(ILCombine, 25);
    MAKEFUNC_ORD(ILFree, 155);
150
    MAKEFUNC_ORD(SHSimpleIDListFromPathAW, 162);
151
#undef MAKEFUNC_ORD
152

153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
    /* test named exports */
    ptr = GetProcAddress(hmod, "ILFree");
    ok(broken(ptr == 0) || ptr != 0, "expected named export for ILFree\n");
    if (ptr)
    {
#define TESTNAMED(f) \
    ptr = (void*)GetProcAddress(hmod, #f); \
    ok(ptr != 0, "expected named export for " #f "\n");

        TESTNAMED(ILAppendID);
        TESTNAMED(ILClone);
        TESTNAMED(ILCloneFirst);
        TESTNAMED(ILCombine);
        TESTNAMED(ILCreateFromPath);
        TESTNAMED(ILCreateFromPathA);
        TESTNAMED(ILCreateFromPathW);
        TESTNAMED(ILFindChild);
        TESTNAMED(ILFindLastID);
        TESTNAMED(ILGetNext);
        TESTNAMED(ILGetSize);
        TESTNAMED(ILIsEqual);
        TESTNAMED(ILIsParent);
        TESTNAMED(ILRemoveLastID);
        TESTNAMED(ILSaveToStream);
#undef TESTNAMED
    }

180
    hmod = GetModuleHandleA("shlwapi.dll");
181
    pStrRetToBufW = (void*)GetProcAddress(hmod, "StrRetToBufW");
182

183 184 185
    hmod = GetModuleHandleA("kernel32.dll");
    pIsWow64Process = (void*)GetProcAddress(hmod, "IsWow64Process");
    pGetSystemWow64DirectoryW = (void*)GetProcAddress(hmod, "GetSystemWow64DirectoryW");
186

187
    hr = SHGetMalloc(&ppM);
188
    ok(hr == S_OK, "SHGetMalloc failed %08x\n", hr);
189 190
}

191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
/* Based on PathAddBackslashW from dlls/shlwapi/path.c */
static LPWSTR myPathAddBackslashW( LPWSTR lpszPath )
{
  size_t iLen;

  if (!lpszPath || (iLen = lstrlenW(lpszPath)) >= MAX_PATH)
    return NULL;

  if (iLen)
  {
    lpszPath += iLen;
    if (lpszPath[-1] != '\\')
    {
      *lpszPath++ = '\\';
      *lpszPath = '\0';
    }
  }
  return lpszPath;
}

211 212 213 214 215 216
static void test_ParseDisplayName(void)
{
    HRESULT hr;
    IShellFolder *IDesktopFolder;
    static const char *cNonExistDir1A = "c:\\nonexist_subdir";
    static const char *cNonExistDir2A = "c:\\\\nonexist_subdir";
217 218
    static const char *cInetTestA = "http:\\yyy";
    static const char *cInetTest2A = "xx:yyy";
219 220 221
    DWORD res;
    WCHAR cTestDirW [MAX_PATH] = {0};
    ITEMIDLIST *newPIDL;
222
    BOOL bRes;
223 224

    hr = SHGetDesktopFolder(&IDesktopFolder);
225
    ok(hr == S_OK, "Expected SHGetDesktopFolder to return S_OK, got 0x%08x\n", hr);
226 227
    if(hr != S_OK) return;

228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
    /* Tests crash on W2K and below (SHCreateShellItem available as of XP) */
    if (pSHCreateShellItem)
    {
        /* null name and pidl */
        hr = IShellFolder_ParseDisplayName(IDesktopFolder,
            NULL, NULL, NULL, NULL, NULL, 0);
        ok(hr == E_INVALIDARG, "returned %08x, expected E_INVALIDARG\n", hr);

        /* null name */
        newPIDL = (ITEMIDLIST*)0xdeadbeef;
        hr = IShellFolder_ParseDisplayName(IDesktopFolder,
            NULL, NULL, NULL, NULL, &newPIDL, 0);
        ok(newPIDL == 0, "expected null, got %p\n", newPIDL);
        ok(hr == E_INVALIDARG, "returned %08x, expected E_INVALIDARG\n", hr);
    }
    else
        win_skip("Tests would crash on W2K and below\n");
245

246 247 248
    MultiByteToWideChar(CP_ACP, 0, cInetTestA, -1, cTestDirW, MAX_PATH);
    hr = IShellFolder_ParseDisplayName(IDesktopFolder,
        NULL, NULL, cTestDirW, NULL, &newPIDL, 0);
249
    todo_wine ok(hr == S_OK || broken(hr == E_FAIL) /* NT4 */,
250
        "ParseDisplayName returned %08x, expected SUCCESS or E_FAIL\n", hr);
251
    if (hr == S_OK)
252 253 254 255 256 257 258 259 260
    {
        ok(pILFindLastID(newPIDL)->mkid.abID[0] == 0x61, "Last pidl should be of type "
           "PT_IESPECIAL1, but is: %02x\n", pILFindLastID(newPIDL)->mkid.abID[0]);
        IMalloc_Free(ppM, newPIDL);
    }

    MultiByteToWideChar(CP_ACP, 0, cInetTest2A, -1, cTestDirW, MAX_PATH);
    hr = IShellFolder_ParseDisplayName(IDesktopFolder,
        NULL, NULL, cTestDirW, NULL, &newPIDL, 0);
261
    todo_wine ok(hr == S_OK || broken(hr == E_FAIL) /* NT4 */,
262
        "ParseDisplayName returned %08x, expected SUCCESS or E_FAIL\n", hr);
263
    if (hr == S_OK)
264 265 266 267 268 269
    {
        ok(pILFindLastID(newPIDL)->mkid.abID[0] == 0x61, "Last pidl should be of type "
           "PT_IESPECIAL1, but is: %02x\n", pILFindLastID(newPIDL)->mkid.abID[0]);
        IMalloc_Free(ppM, newPIDL);
    }

270
    res = GetFileAttributesA(cNonExistDir1A);
271 272 273 274 275
    if(res != INVALID_FILE_ATTRIBUTES)
    {
        skip("Test directory unexpectedly exists\n");
        goto finished;
    }
276 277 278 279

    MultiByteToWideChar(CP_ACP, 0, cNonExistDir1A, -1, cTestDirW, MAX_PATH);
    hr = IShellFolder_ParseDisplayName(IDesktopFolder, 
        NULL, NULL, cTestDirW, NULL, &newPIDL, 0);
280
    ok((hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) || (hr == E_FAIL), 
281
        "ParseDisplayName returned %08x, expected 80070002 or E_FAIL\n", hr);
282 283

    res = GetFileAttributesA(cNonExistDir2A);
284 285 286 287 288
    if(res != INVALID_FILE_ATTRIBUTES)
    {
        skip("Test directory unexpectedly exists\n");
        goto finished;
    }
289 290 291 292

    MultiByteToWideChar(CP_ACP, 0, cNonExistDir2A, -1, cTestDirW, MAX_PATH);
    hr = IShellFolder_ParseDisplayName(IDesktopFolder, 
        NULL, NULL, cTestDirW, NULL, &newPIDL, 0);
293
    ok((hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) || (hr == E_FAIL) || (hr == E_INVALIDARG), 
294
        "ParseDisplayName returned %08x, expected 80070002, E_FAIL or E_INVALIDARG\n", hr);
295

296 297 298
    /* I thought that perhaps the DesktopFolder's ParseDisplayName would recognize the
     * path corresponding to CSIDL_PERSONAL and return a CLSID_MyDocuments PIDL. Turns
     * out it doesn't. The magic seems to happen in the file dialogs, then. */
299 300 301 302 303 304
    if (!pSHGetSpecialFolderPathW || !pILFindLastID)
    {
        win_skip("SHGetSpecialFolderPathW and/or ILFindLastID are not available\n");
        goto finished;
    }

305
    bRes = pSHGetSpecialFolderPathW(NULL, cTestDirW, CSIDL_PERSONAL, FALSE);
306
    ok(bRes, "SHGetSpecialFolderPath(CSIDL_PERSONAL) failed! %u\n", GetLastError());
307 308 309
    if (!bRes) goto finished;

    hr = IShellFolder_ParseDisplayName(IDesktopFolder, NULL, NULL, cTestDirW, NULL, &newPIDL, 0);
310 311
    ok(hr == S_OK, "DesktopFolder->ParseDisplayName failed. hr = %08x.\n", hr);
    if (hr != S_OK) goto finished;
312

313 314 315 316
    ok(pILFindLastID(newPIDL)->mkid.abID[0] == 0x31 ||
       pILFindLastID(newPIDL)->mkid.abID[0] == 0xb1, /* Win98 */
       "Last pidl should be of type PT_FOLDER or PT_IESPECIAL2, but is: %02x\n",
       pILFindLastID(newPIDL)->mkid.abID[0]);
317 318 319 320
    IMalloc_Free(ppM, newPIDL);
    
finished:
    IShellFolder_Release(IDesktopFolder);
321 322
}

323
/* creates a file with the specified name for tests */
324
static void CreateTestFile(const CHAR *name)
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
{
    HANDLE file;
    DWORD written;

    file = CreateFileA(name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
    if (file != INVALID_HANDLE_VALUE)
    {
	WriteFile(file, name, strlen(name), &written, NULL);
	WriteFile(file, "\n", strlen("\n"), &written, NULL);
	CloseHandle(file);
    }
}


/* initializes the tests */
340
static void CreateFilesFolders(void)
341 342 343 344 345 346 347
{
    CreateDirectoryA(".\\testdir", NULL);
    CreateDirectoryA(".\\testdir\\test.txt", NULL);
    CreateTestFile  (".\\testdir\\test1.txt ");
    CreateTestFile  (".\\testdir\\test2.txt ");
    CreateTestFile  (".\\testdir\\test3.txt ");
    CreateDirectoryA(".\\testdir\\testdir2 ", NULL);
348
    CreateDirectoryA(".\\testdir\\testdir2\\subdir", NULL);
349 350 351
}

/* cleans after tests */
352
static void Cleanup(void)
353 354 355 356 357
{
    DeleteFileA(".\\testdir\\test1.txt");
    DeleteFileA(".\\testdir\\test2.txt");
    DeleteFileA(".\\testdir\\test3.txt");
    RemoveDirectoryA(".\\testdir\\test.txt");
358
    RemoveDirectoryA(".\\testdir\\testdir2\\subdir");
359 360 361 362 363 364
    RemoveDirectoryA(".\\testdir\\testdir2");
    RemoveDirectoryA(".\\testdir");
}


/* perform test */
365
static void test_EnumObjects(IShellFolder *iFolder)
366 367
{
    IEnumIDList *iEnumList;
368
    LPITEMIDLIST newPIDL, idlArr[10];
369 370
    ULONG NumPIDLs;
    int i=0, j;
371
    HRESULT hr;
372 373 374 375

    static const WORD iResults [5][5] =
    {
	{ 0,-1,-1,-1,-1},
376 377 378 379
	{ 1, 0,-1,-1,-1},
	{ 1, 1, 0,-1,-1},
	{ 1, 1, 1, 0,-1},
	{ 1, 1, 1, 1, 0}
380 381
    };

382 383
#define SFGAO_testfor SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR | SFGAO_CAPABILITYMASK
    /* Don't test for SFGAO_HASSUBFOLDER since we return real state and native cached */
384
    static const ULONG attrs[5] =
385
    {
386 387 388 389 390
        SFGAO_CAPABILITYMASK | SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR,
        SFGAO_CAPABILITYMASK | SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR,
        SFGAO_CAPABILITYMASK | SFGAO_FILESYSTEM,
        SFGAO_CAPABILITYMASK | SFGAO_FILESYSTEM,
        SFGAO_CAPABILITYMASK | SFGAO_FILESYSTEM,
391 392 393
    };

    hr = IShellFolder_EnumObjects(iFolder, NULL, SHCONTF_FOLDERS | SHCONTF_NONFOLDERS | SHCONTF_INCLUDEHIDDEN, &iEnumList);
394
    ok(hr == S_OK, "EnumObjects failed %08x\n", hr);
395

396 397
    /* This is to show that, contrary to what is said on MSDN, on IEnumIDList::Next,
     * the filesystem shellfolders return S_OK even if less than 'celt' items are
398 399 400 401 402
     * returned (in contrast to S_FALSE). We have to do it in a loop since WinXP
     * only ever returns a single entry per call. */
    while (IEnumIDList_Next(iEnumList, 10-i, &idlArr[i], &NumPIDLs) == S_OK) 
        i += NumPIDLs;
    ok (i == 5, "i: %d\n", i);
403 404

    hr = IEnumIDList_Release(iEnumList);
405
    ok(hr == S_OK, "IEnumIDList_Release failed %08x\n", hr);
406
    
407 408 409
    /* Sort them first in case of wrong order from system */
    for (i=0;i<5;i++) for (j=0;j<5;j++)
        if ((SHORT)IShellFolder_CompareIDs(iFolder, 0, idlArr[i], idlArr[j]) < 0)
410
	{
411 412 413 414 415 416 417 418
            newPIDL = idlArr[i];
            idlArr[i] = idlArr[j];
            idlArr[j] = newPIDL;
        }
	    
    for (i=0;i<5;i++) for (j=0;j<5;j++)
    {
        hr = IShellFolder_CompareIDs(iFolder, 0, idlArr[i], idlArr[j]);
419
        ok(hr == iResults[i][j], "Got %x expected [%d]-[%d]=%x\n", hr, i, j, iResults[i][j]);
420 421
    }

422

423 424 425
    for (i = 0; i < 5; i++)
    {
        SFGAOF flags;
426
#define SFGAO_VISTA SFGAO_DROPTARGET | SFGAO_CANLINK | SFGAO_CANCOPY
427 428
        /* Native returns all flags no matter what we ask for */
        flags = SFGAO_CANCOPY;
429
        hr = IShellFolder_GetAttributesOf(iFolder, 1, (LPCITEMIDLIST*)(idlArr + i), &flags);
430
        flags &= SFGAO_testfor;
431
        ok(hr == S_OK, "GetAttributesOf returns %08x\n", hr);
432
        ok(flags == (attrs[i]) ||
433 434
           flags == (attrs[i] & ~SFGAO_FILESYSANCESTOR) || /* Win9x, NT4 */
           flags == ((attrs[i] & ~SFGAO_CAPABILITYMASK) | SFGAO_VISTA), /* Vista and higher */
435
           "GetAttributesOf[%i] got %08x, expected %08x\n", i, flags, attrs[i]);
436 437 438 439

        flags = SFGAO_testfor;
        hr = IShellFolder_GetAttributesOf(iFolder, 1, (LPCITEMIDLIST*)(idlArr + i), &flags);
        flags &= SFGAO_testfor;
440
        ok(hr == S_OK, "GetAttributesOf returns %08x\n", hr);
441 442 443
        ok(flags == attrs[i] ||
           flags == (attrs[i] & ~SFGAO_FILESYSANCESTOR), /* Win9x, NT4 */
           "GetAttributesOf[%i] got %08x, expected %08x\n", i, flags, attrs[i]);
444
    }
445 446 447

    for (i=0;i<5;i++)
        IMalloc_Free(ppM, idlArr[i]);
448 449
}

450
static void test_BindToObject(void)
451 452 453 454 455
{
    HRESULT hr;
    UINT cChars;
    IShellFolder *psfDesktop, *psfChild, *psfMyComputer, *psfSystemDir;
    SHITEMID emptyitem = { 0, { 0 } };
456
    LPITEMIDLIST pidlMyComputer, pidlSystemDir, pidl, pidlEmpty = (LPITEMIDLIST)&emptyitem;
457
    WCHAR wszSystemDir[MAX_PATH];
458
    char szSystemDir[MAX_PATH];
459 460
    char buf[MAX_PATH];
    WCHAR path[MAX_PATH];
461
    CHAR pathA[MAX_PATH];
462
    HANDLE hfile;
463 464 465
    WCHAR wszMyComputer[] = { 
        ':',':','{','2','0','D','0','4','F','E','0','-','3','A','E','A','-','1','0','6','9','-',
        'A','2','D','8','-','0','8','0','0','2','B','3','0','3','0','9','D','}',0 };
466 467 468
    static const CHAR filename_html[] = "winetest.html";
    static const CHAR filename_txt[] = "winetest.txt";
    static const CHAR filename_foo[] = "winetest.foo";
469 470 471 472 473

    /* The following tests shows that BindToObject should fail with E_INVALIDARG if called
     * with an empty pidl. This is tested for Desktop, MyComputer and the FS ShellFolder
     */
    hr = SHGetDesktopFolder(&psfDesktop);
474 475
    ok (hr == S_OK, "SHGetDesktopFolder failed! hr = %08x\n", hr);
    if (hr != S_OK) return;
476 477
    
    hr = IShellFolder_BindToObject(psfDesktop, pidlEmpty, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
478
    ok (hr == E_INVALIDARG, "Desktop's BindToObject should fail, when called with empty pidl! hr = %08x\n", hr);
479

480
    hr = IShellFolder_BindToObject(psfDesktop, NULL, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
481
    ok (hr == E_INVALIDARG, "Desktop's BindToObject should fail, when called with NULL pidl! hr = %08x\n", hr);
482

483
    hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszMyComputer, NULL, &pidlMyComputer, NULL);
484 485
    ok (hr == S_OK, "Desktop's ParseDisplayName failed to parse MyComputer's CLSID! hr = %08x\n", hr);
    if (hr != S_OK) {
486 487 488 489 490
        IShellFolder_Release(psfDesktop);
        return;
    }
    
    hr = IShellFolder_BindToObject(psfDesktop, pidlMyComputer, NULL, &IID_IShellFolder, (LPVOID*)&psfMyComputer);
491
    ok (hr == S_OK, "Desktop failed to bind to MyComputer object! hr = %08x\n", hr);
492
    IShellFolder_Release(psfDesktop);
493
    IMalloc_Free(ppM, pidlMyComputer);
494
    if (hr != S_OK) return;
495 496

    hr = IShellFolder_BindToObject(psfMyComputer, pidlEmpty, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
497
    ok (hr == E_INVALIDARG, "MyComputers's BindToObject should fail, when called with empty pidl! hr = %08x\n", hr);
498

499 500
if (0)
{
501
    /* this call segfaults on 98SE */
502
    hr = IShellFolder_BindToObject(psfMyComputer, NULL, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
503
    ok (hr == E_INVALIDARG, "MyComputers's BindToObject should fail, when called with NULL pidl! hr = %08x\n", hr);
504
}
505

506
    cChars = GetSystemDirectoryA(szSystemDir, MAX_PATH);
507
    ok (cChars > 0 && cChars < MAX_PATH, "GetSystemDirectoryA failed! LastError: %u\n", GetLastError());
508 509 510 511
    if (cChars == 0 || cChars >= MAX_PATH) {
        IShellFolder_Release(psfMyComputer);
        return;
    }
512
    MultiByteToWideChar(CP_ACP, 0, szSystemDir, -1, wszSystemDir, MAX_PATH);
513 514
    
    hr = IShellFolder_ParseDisplayName(psfMyComputer, NULL, NULL, wszSystemDir, NULL, &pidlSystemDir, NULL);
515 516
    ok (hr == S_OK, "MyComputers's ParseDisplayName failed to parse the SystemDirectory! hr = %08x\n", hr);
    if (hr != S_OK) {
517 518 519 520 521
        IShellFolder_Release(psfMyComputer);
        return;
    }

    hr = IShellFolder_BindToObject(psfMyComputer, pidlSystemDir, NULL, &IID_IShellFolder, (LPVOID*)&psfSystemDir);
522
    ok (hr == S_OK, "MyComputer failed to bind to a FileSystem ShellFolder! hr = %08x\n", hr);
523
    IShellFolder_Release(psfMyComputer);
524
    IMalloc_Free(ppM, pidlSystemDir);
525
    if (hr != S_OK) return;
526 527 528

    hr = IShellFolder_BindToObject(psfSystemDir, pidlEmpty, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
    ok (hr == E_INVALIDARG, 
529
        "FileSystem ShellFolder's BindToObject should fail, when called with empty pidl! hr = %08x\n", hr);
530 531 532

if (0)
{
533
    /* this call segfaults on 98SE */
534
    hr = IShellFolder_BindToObject(psfSystemDir, NULL, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
535
    ok (hr == E_INVALIDARG,
536
        "FileSystem ShellFolder's BindToObject should fail, when called with NULL pidl! hr = %08x\n", hr);
537
}
538

539
    IShellFolder_Release(psfSystemDir);
540

541 542 543 544 545 546
    GetCurrentDirectoryA(MAX_PATH, buf);
    if(!lstrlenA(buf))
    {
        skip("Failed to get current directory, skipping tests.\n");
        return;
    }
547

548
    SHGetDesktopFolder(&psfDesktop);
549

550 551 552
    /* Attempt BindToObject on files. */

    /* .html */
553 554 555 556 557
    lstrcpyA(pathA, buf);
    lstrcatA(pathA, "\\");
    lstrcatA(pathA, filename_html);
    hfile = CreateFileA(pathA, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
    if(hfile != INVALID_HANDLE_VALUE)
558
    {
559
        CloseHandle(hfile);
560
        MultiByteToWideChar(CP_ACP, 0, pathA, -1, path, MAX_PATH);
561
        hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, path, NULL, &pidl, NULL);
562
        ok(hr == S_OK, "Got 0x%08x\n", hr);
563 564 565
        if(SUCCEEDED(hr))
        {
            hr = IShellFolder_BindToObject(psfDesktop, pidl, NULL, &IID_IShellFolder, (void**)&psfChild);
566 567
            ok(hr == S_OK ||
               hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), /* XP, W2K3 */
568 569 570 571 572
               "Got 0x%08x\n", hr);
            if(SUCCEEDED(hr))
            {
                IPersist *pp;
                hr = IShellFolder_QueryInterface(psfChild, &IID_IPersist, (void**)&pp);
573 574 575
                ok(hr == S_OK ||
                   broken(hr == E_NOINTERFACE), /* Win9x, NT4, W2K */
                   "Got 0x%08x\n", hr);
576 577 578 579 580
                if(SUCCEEDED(hr))
                {
                    CLSID id;
                    hr = IPersist_GetClassID(pp, &id);
                    ok(hr == S_OK, "Got 0x%08x\n", hr);
581 582 583
                    /* CLSID_ShellFSFolder on some w2k systems */
                    ok(IsEqualIID(&id, &CLSID_ShellDocObjView) || broken(IsEqualIID(&id, &CLSID_ShellFSFolder)),
                        "Unexpected classid %s\n", debugstr_guid(&id));
584 585 586 587 588 589 590
                    IPersist_Release(pp);
                }

                IShellFolder_Release(psfChild);
            }
            pILFree(pidl);
        }
591
        DeleteFileA(pathA);
592
    }
593 594 595 596
    else
        win_skip("Failed to create .html testfile.\n");

    /* .txt */
597 598 599 600 601
    lstrcpyA(pathA, buf);
    lstrcatA(pathA, "\\");
    lstrcatA(pathA, filename_txt);
    hfile = CreateFileA(pathA, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
    if(hfile != INVALID_HANDLE_VALUE)
602 603
    {
        CloseHandle(hfile);
604
        MultiByteToWideChar(CP_ACP, 0, pathA, -1, path, MAX_PATH);
605
        hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, path, NULL, &pidl, NULL);
606
        ok(hr == S_OK, "Got 0x%08x\n", hr);
607 608 609
        if(SUCCEEDED(hr))
        {
            hr = IShellFolder_BindToObject(psfDesktop, pidl, NULL, &IID_IShellFolder, (void**)&psfChild);
610 611 612
            ok(hr == E_FAIL || /* Vista+ */
               hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) || /* XP, W2K3 */
               broken(hr == S_OK), /* Win9x, NT4, W2K */
613 614 615 616
               "Got 0x%08x\n", hr);
            if(SUCCEEDED(hr)) IShellFolder_Release(psfChild);
            pILFree(pidl);
        }
617
        DeleteFileA(pathA);
618 619 620 621 622
    }
    else
        win_skip("Failed to create .txt testfile.\n");

    /* .foo */
623 624 625 626 627
    lstrcpyA(pathA, buf);
    lstrcatA(pathA, "\\");
    lstrcatA(pathA, filename_foo);
    hfile = CreateFileA(pathA, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
    if(hfile != INVALID_HANDLE_VALUE)
628 629
    {
        CloseHandle(hfile);
630
        MultiByteToWideChar(CP_ACP, 0, pathA, -1, path, MAX_PATH);
631
        hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, path, NULL, &pidl, NULL);
632
        ok(hr == S_OK, "Got 0x%08x\n", hr);
633 634 635
        if(SUCCEEDED(hr))
        {
            hr = IShellFolder_BindToObject(psfDesktop, pidl, NULL, &IID_IShellFolder, (void**)&psfChild);
636 637 638
            ok(hr == E_FAIL || /* Vista+ */
               hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) || /* XP, W2K3 */
               broken(hr == S_OK), /* Win9x, NT4, W2K */
639 640 641 642
               "Got 0x%08x\n", hr);
            if(SUCCEEDED(hr)) IShellFolder_Release(psfChild);
            pILFree(pidl);
        }
643
        DeleteFileA(pathA);
644 645 646 647 648
    }
    else
        win_skip("Failed to create .foo testfile.\n");

    /* And on the desktop */
649
    if(pSHGetSpecialFolderPathA)
650
    {
651 652 653 654 655
        pSHGetSpecialFolderPathA(NULL, pathA, CSIDL_DESKTOP, FALSE);
        lstrcatA(pathA, "\\");
        lstrcatA(pathA, filename_html);
        hfile = CreateFileA(pathA, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
        if(hfile != INVALID_HANDLE_VALUE)
656 657
        {
            CloseHandle(hfile);
658
            MultiByteToWideChar(CP_ACP, 0, pathA, -1, path, MAX_PATH);
659
            hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, path, NULL, &pidl, NULL);
660
            ok(hr == S_OK, "Got 0x%08x\n", hr);
661 662 663
            if(SUCCEEDED(hr))
            {
                hr = IShellFolder_BindToObject(psfDesktop, pidl, NULL, &IID_IShellFolder, (void**)&psfChild);
664 665
                ok(hr == S_OK ||
                   hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), /* XP, W2K3 */
666 667 668 669
                   "Got 0x%08x\n", hr);
                if(SUCCEEDED(hr)) IShellFolder_Release(psfChild);
                pILFree(pidl);
            }
670
            if(!DeleteFileA(pathA))
671 672 673 674 675 676
                trace("Failed to delete: %d\n", GetLastError());

        }
        else
            win_skip("Failed to create .html testfile.\n");

677 678 679 680 681
        pSHGetSpecialFolderPathA(NULL, pathA, CSIDL_DESKTOP, FALSE);
        lstrcatA(pathA, "\\");
        lstrcatA(pathA, filename_foo);
        hfile = CreateFileA(pathA, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
        if(hfile != INVALID_HANDLE_VALUE)
682 683
        {
            CloseHandle(hfile);
684
            MultiByteToWideChar(CP_ACP, 0, pathA, -1, path, MAX_PATH);
685
            hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, path, NULL, &pidl, NULL);
686
            ok(hr == S_OK, "Got 0x%08x\n", hr);
687 688 689
            if(SUCCEEDED(hr))
            {
                hr = IShellFolder_BindToObject(psfDesktop, pidl, NULL, &IID_IShellFolder, (void**)&psfChild);
690 691 692
                ok(hr == E_FAIL || /* Vista+ */
                   hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) || /* XP, W2K3 */
                   broken(hr == S_OK), /* Win9x, NT4, W2K */
693 694 695 696
                   "Got 0x%08x\n", hr);
                if(SUCCEEDED(hr)) IShellFolder_Release(psfChild);
                pILFree(pidl);
            }
697
            DeleteFileA(pathA);
698 699 700 701 702 703
        }
        else
            win_skip("Failed to create .foo testfile.\n");
    }

    IShellFolder_Release(psfDesktop);
704 705
}

706 707 708 709 710
static void test_GetDisplayName(void)
{
    BOOL result;
    HRESULT hr;
    HANDLE hTestFile;
711
    WCHAR wszTestFile[MAX_PATH], wszTestFile2[MAX_PATH];
712
    char szTestFile[MAX_PATH], szTestDir[MAX_PATH];
713
    DWORD attr;
714 715
    STRRET strret;
    LPSHELLFOLDER psfDesktop, psfPersonal;
716
    IUnknown *psfFile;
717 718
    SHITEMID emptyitem = { 0, { 0 } };
    LPITEMIDLIST pidlTestFile, pidlEmpty = (LPITEMIDLIST)&emptyitem;
719
    LPCITEMIDLIST pidlLast;
720
    static const CHAR szFileName[] = "winetest.foo";
721 722 723 724
    static const WCHAR wszFileName[] = { 'w','i','n','e','t','e','s','t','.','f','o','o',0 };
    static const WCHAR wszDirName[] = { 'w','i','n','e','t','e','s','t',0 };

    /* I'm trying to figure if there is a functional difference between calling
725
     * SHGetPathFromIDListW and calling GetDisplayNameOf(SHGDN_FORPARSING) after
726
     * binding to the shellfolder. One thing I thought of was that perhaps 
727 728
     * SHGetPathFromIDListW would be able to get the path to a file, which does
     * not exist anymore, while the other method wouldn't. It turns out there's
729 730 731
     * no functional difference in this respect.
     */

732 733 734 735
    if(!pSHGetSpecialFolderPathA) {
        win_skip("SHGetSpecialFolderPathA is not available\n");
        return;
    }
736

737
    /* First creating a directory in MyDocuments and a file in this directory. */
738 739
    result = pSHGetSpecialFolderPathA(NULL, szTestDir, CSIDL_PERSONAL, FALSE);
    ok(result, "SHGetSpecialFolderPathA failed! Last error: %u\n", GetLastError());
740 741
    if (!result) return;

742
    /* Use ANSI file functions so this works on Windows 9x */
743
    lstrcatA(szTestDir, "\\winetest");
744 745 746 747 748 749 750
    CreateDirectoryA(szTestDir, NULL);
    attr=GetFileAttributesA(szTestDir);
    if (attr == INVALID_FILE_ATTRIBUTES || !(attr & FILE_ATTRIBUTE_DIRECTORY))
    {
        ok(0, "unable to create the '%s' directory\n", szTestDir);
        return;
    }
751

752 753 754
    lstrcpyA(szTestFile, szTestDir);
    lstrcatA(szTestFile, "\\");
    lstrcatA(szTestFile, szFileName);
755
    hTestFile = CreateFileA(szTestFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
756
    ok((hTestFile != INVALID_HANDLE_VALUE), "CreateFileA failed! Last error: %u\n", GetLastError());
757 758 759
    if (hTestFile == INVALID_HANDLE_VALUE) return;
    CloseHandle(hTestFile);

760
    /* Getting an itemidlist for the file. */
761
    hr = SHGetDesktopFolder(&psfDesktop);
762 763
    ok(hr == S_OK, "SHGetDesktopFolder failed! hr = %08x\n", hr);
    if (hr != S_OK) return;
764

765 766
    MultiByteToWideChar(CP_ACP, 0, szTestFile, -1, wszTestFile, MAX_PATH);

767
    hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszTestFile, NULL, &pidlTestFile, NULL);
768 769
    ok(hr == S_OK, "Desktop->ParseDisplayName failed! hr = %08x\n", hr);
    if (hr != S_OK) {
770 771 772 773
        IShellFolder_Release(psfDesktop);
        return;
    }

774
    pidlLast = pILFindLastID(pidlTestFile);
775 776 777 778 779 780 781 782 783
    ok(pidlLast->mkid.cb >=76 ||
        broken(pidlLast->mkid.cb == 28) || /* W2K */
        broken(pidlLast->mkid.cb == 40), /* Win9x, WinME */
        "Expected pidl length of at least 76, got %d.\n", pidlLast->mkid.cb);
    if (pidlLast->mkid.cb >= 28) {
        ok(!lstrcmpA((CHAR*)&pidlLast->mkid.abID[12], szFileName),
            "Filename should be stored as ansi-string at this position!\n");
    }
    /* WinXP and up store the filenames as both ANSI and UNICODE in the pidls */
784
    if (pidlLast->mkid.cb >= 76) {
785
        ok(!lstrcmpW((WCHAR*)&pidlLast->mkid.abID[46], wszFileName) ||
786 787
            (pidlLast->mkid.cb >= 94 && !lstrcmpW((WCHAR*)&pidlLast->mkid.abID[64], wszFileName)) ||  /* Vista */
            (pidlLast->mkid.cb >= 98 && !lstrcmpW((WCHAR*)&pidlLast->mkid.abID[68], wszFileName)), /* Win7 */
788
            "Filename should be stored as wchar-string at this position!\n");
789 790
    }
    
791
    /* It seems as if we cannot bind to regular files on windows, but only directories. 
792 793
     */
    hr = IShellFolder_BindToObject(psfDesktop, pidlTestFile, NULL, &IID_IUnknown, (VOID**)&psfFile);
794
    ok (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) ||
795
        hr == E_NOTIMPL || /* Vista */
796
        broken(hr == S_OK), /* Win9x, W2K */
797
        "hr = %08x\n", hr);
798
    if (hr == S_OK) {
799
        IUnknown_Release(psfFile);
800
    }
801

802
    if (!pSHBindToParent)
803
    {
804 805 806
        win_skip("SHBindToParent is missing\n");
        DeleteFileA(szTestFile);
        RemoveDirectoryA(szTestDir);
807 808
        return;
    }
Michael Jung's avatar
Michael Jung committed
809 810
  
    /* Some tests for IShellFolder::SetNameOf */
811 812 813
    if (pSHGetFolderPathAndSubDirA)
    {
        hr = pSHBindToParent(pidlTestFile, &IID_IShellFolder, (VOID**)&psfPersonal, &pidlLast);
814 815
        ok(hr == S_OK, "SHBindToParent failed! hr = %08x\n", hr);
        if (hr == S_OK) {
816 817 818 819 820 821
            /* It's ok to use this fixed path. Call will fail anyway. */
            WCHAR wszAbsoluteFilename[] = { 'C',':','\\','w','i','n','e','t','e','s','t', 0 };
            LPITEMIDLIST pidlNew;

            /* The pidl returned through the last parameter of SetNameOf is a simple one. */
            hr = IShellFolder_SetNameOf(psfPersonal, NULL, pidlLast, wszDirName, SHGDN_NORMAL, &pidlNew);
822
            ok (hr == S_OK, "SetNameOf failed! hr = %08x\n", hr);
823 824 825 826 827 828 829 830 831 832 833 834 835 836
            if (hr == S_OK)
            {
                ok (((LPITEMIDLIST)((LPBYTE)pidlNew+pidlNew->mkid.cb))->mkid.cb == 0,
                    "pidl returned from SetNameOf should be simple!\n");

                /* Passing an absolute path to SetNameOf fails. The HRESULT code indicates that SetNameOf
                 * is implemented on top of SHFileOperation in WinXP. */
                hr = IShellFolder_SetNameOf(psfPersonal, NULL, pidlNew, wszAbsoluteFilename,
                        SHGDN_FORPARSING, NULL);
                ok (hr == HRESULT_FROM_WIN32(ERROR_CANCELLED), "SetNameOf succeeded! hr = %08x\n", hr);

                /* Rename the file back to its original name. SetNameOf ignores the fact, that the
                 * SHGDN flags specify an absolute path. */
                hr = IShellFolder_SetNameOf(psfPersonal, NULL, pidlNew, wszFileName, SHGDN_FORPARSING, NULL);
837
                ok (hr == S_OK, "SetNameOf failed! hr = %08x\n", hr);
838 839 840 841 842

                pILFree(pidlNew);
            }

            IShellFolder_Release(psfPersonal);
843
        }
Michael Jung's avatar
Michael Jung committed
844
    }
845 846
    else
        win_skip("Avoid needs of interaction on Win2k\n");
847

848
    /* Deleting the file and the directory */
849 850
    DeleteFileA(szTestFile);
    RemoveDirectoryA(szTestDir);
851 852

    /* SHGetPathFromIDListW still works, although the file is not present anymore. */
853 854 855 856 857 858
    if (pSHGetPathFromIDListW)
    {
        result = pSHGetPathFromIDListW(pidlTestFile, wszTestFile2);
        ok (result, "SHGetPathFromIDListW failed! Last error: %u\n", GetLastError());
        ok (!lstrcmpiW(wszTestFile, wszTestFile2), "SHGetPathFromIDListW returns incorrect path!\n");
    }
859

860 861
    /* SHBindToParent fails, if called with a NULL PIDL. */
    hr = pSHBindToParent(NULL, &IID_IShellFolder, (VOID**)&psfPersonal, &pidlLast);
862
    ok (hr != S_OK, "SHBindToParent(NULL) should fail!\n");
863 864 865

    /* But it succeeds with an empty PIDL. */
    hr = pSHBindToParent(pidlEmpty, &IID_IShellFolder, (VOID**)&psfPersonal, &pidlLast);
866
    ok (hr == S_OK, "SHBindToParent(empty PIDL) should succeed! hr = %08x\n", hr);
867
    ok (pidlLast == pidlEmpty, "The last element of an empty PIDL should be the PIDL itself!\n");
868
    if (hr == S_OK)
869 870
        IShellFolder_Release(psfPersonal);
    
871
    /* Binding to the folder and querying the display name of the file also works. */
872
    hr = pSHBindToParent(pidlTestFile, &IID_IShellFolder, (VOID**)&psfPersonal, &pidlLast); 
873 874
    ok (hr == S_OK, "SHBindToParent failed! hr = %08x\n", hr);
    if (hr != S_OK) {
875 876 877 878
        IShellFolder_Release(psfDesktop);
        return;
    }

879 880
    /* This test shows that Windows doesn't allocate a new pidlLast, but returns a pointer into 
     * pidlTestFile (In accordance with MSDN). */
881 882
    ok (pILFindLastID(pidlTestFile) == pidlLast, 
                                "SHBindToParent doesn't return the last id of the pidl param!\n");
883
    
884
    hr = IShellFolder_GetDisplayNameOf(psfPersonal, pidlLast, SHGDN_FORPARSING, &strret);
885 886
    ok (hr == S_OK, "Personal->GetDisplayNameOf failed! hr = %08x\n", hr);
    if (hr != S_OK) {
887 888 889 890
        IShellFolder_Release(psfDesktop);
        IShellFolder_Release(psfPersonal);
        return;
    }
891 892 893 894

    if (pStrRetToBufW)
    {
        hr = pStrRetToBufW(&strret, pidlLast, wszTestFile2, MAX_PATH);
895
        ok (hr == S_OK, "StrRetToBufW failed! hr = %08x\n", hr);
896 897
        ok (!lstrcmpiW(wszTestFile, wszTestFile2), "GetDisplayNameOf returns incorrect path!\n");
    }
898
    
899
    ILFree(pidlTestFile);
900 901 902 903
    IShellFolder_Release(psfDesktop);
    IShellFolder_Release(psfPersonal);
}

904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933
static void test_CallForAttributes(void)
{
    HKEY hKey;
    LONG lResult;
    HRESULT hr;
    DWORD dwSize;
    LPSHELLFOLDER psfDesktop;
    LPITEMIDLIST pidlMyDocuments;
    DWORD dwAttributes, dwCallForAttributes, dwOrigAttributes, dwOrigCallForAttributes;
    static const WCHAR wszAttributes[] = { 'A','t','t','r','i','b','u','t','e','s',0 };
    static const WCHAR wszCallForAttributes[] = { 
        'C','a','l','l','F','o','r','A','t','t','r','i','b','u','t','e','s',0 };
    static const WCHAR wszMyDocumentsKey[] = {
        'C','L','S','I','D','\\','{','4','5','0','D','8','F','B','A','-','A','D','2','5','-',
        '1','1','D','0','-','9','8','A','8','-','0','8','0','0','3','6','1','B','1','1','0','3','}',
        '\\','S','h','e','l','l','F','o','l','d','e','r',0 };
    WCHAR wszMyDocuments[] = {
        ':',':','{','4','5','0','D','8','F','B','A','-','A','D','2','5','-','1','1','D','0','-',
        '9','8','A','8','-','0','8','0','0','3','6','1','B','1','1','0','3','}',0 };
    
    /* For the root of a namespace extension, the attributes are not queried by binding
     * to the object and calling GetAttributesOf. Instead, the attributes are read from 
     * the registry value HKCR/CLSID/{...}/ShellFolder/Attributes. This is documented on MSDN.
     *
     * The MyDocuments shellfolder on WinXP has a HKCR/CLSID/{...}/ShellFolder/CallForAttributes
     * value. It seems that if the folder is queried for one of the flags set in CallForAttributes,
     * the shell does bind to the folder object and calls GetAttributesOf. This is not documented
     * on MSDN. This test is meant to document the observed behaviour on WinXP SP2.
     */
    hr = SHGetDesktopFolder(&psfDesktop);
934 935
    ok (hr == S_OK, "SHGetDesktopFolder failed! hr = %08x\n", hr);
    if (hr != S_OK) return;
936 937 938
    
    hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszMyDocuments, NULL, 
                                       &pidlMyDocuments, NULL);
939
    ok (hr == S_OK ||
940
        broken(hr == E_INVALIDARG), /* Win95, NT4 */
941
        "Desktop's ParseDisplayName failed to parse MyDocuments's CLSID! hr = %08x\n", hr);
942
    if (hr != S_OK) {
943 944 945 946 947 948 949
        IShellFolder_Release(psfDesktop);
        return;
    }

    dwAttributes = 0xffffffff;
    hr = IShellFolder_GetAttributesOf(psfDesktop, 1, 
                                      (LPCITEMIDLIST*)&pidlMyDocuments, &dwAttributes);
950
    ok (hr == S_OK, "Desktop->GetAttributesOf(MyDocuments) failed! hr = %08x\n", hr);
951 952

    /* We need the following setup (as observed on WinXP SP2), for the tests to make sense. */
953
    ok (dwAttributes & SFGAO_FILESYSTEM, "SFGAO_FILESYSTEM attribute is not set for MyDocuments!\n");
954 955 956 957 958 959 960
    ok (!(dwAttributes & SFGAO_ISSLOW), "SFGAO_ISSLOW attribute is set for MyDocuments!\n");
    ok (!(dwAttributes & SFGAO_GHOSTED), "SFGAO_GHOSTED attribute is set for MyDocuments!\n");

    /* We don't have the MyDocuments shellfolder in wine yet, and thus we don't have the registry
     * key. So the test will return at this point, if run on wine. 
     */
    lResult = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszMyDocumentsKey, 0, KEY_WRITE|KEY_READ, &hKey);
961 962 963
    ok (lResult == ERROR_SUCCESS ||
        lResult == ERROR_ACCESS_DENIED,
        "RegOpenKeyEx failed! result: %08x\n", lResult);
964
    if (lResult != ERROR_SUCCESS) {
965 966
        if (lResult == ERROR_ACCESS_DENIED)
            skip("Not enough rights to open the registry key\n");
967
        IMalloc_Free(ppM, pidlMyDocuments);
968 969 970 971 972 973 974
        IShellFolder_Release(psfDesktop);
        return;
    }
    
    /* Query MyDocuments' Attributes value, to be able to restore it later. */
    dwSize = sizeof(DWORD);
    lResult = RegQueryValueExW(hKey, wszAttributes, NULL, NULL, (LPBYTE)&dwOrigAttributes, &dwSize);
975
    ok (lResult == ERROR_SUCCESS, "RegQueryValueEx failed! result: %08x\n", lResult);
976 977
    if (lResult != ERROR_SUCCESS) {
        RegCloseKey(hKey);
978
        IMalloc_Free(ppM, pidlMyDocuments);
979 980 981 982 983 984 985 986
        IShellFolder_Release(psfDesktop);
        return;
    }

    /* Query MyDocuments' CallForAttributes value, to be able to restore it later. */
    dwSize = sizeof(DWORD);
    lResult = RegQueryValueExW(hKey, wszCallForAttributes, NULL, NULL, 
                              (LPBYTE)&dwOrigCallForAttributes, &dwSize);
987
    ok (lResult == ERROR_SUCCESS, "RegQueryValueEx failed! result: %08x\n", lResult);
988 989
    if (lResult != ERROR_SUCCESS) {
        RegCloseKey(hKey);
990
        IMalloc_Free(ppM, pidlMyDocuments);
991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
        IShellFolder_Release(psfDesktop);
        return;
    }
    
    /* Define via the Attributes value that MyDocuments attributes are SFGAO_ISSLOW and 
     * SFGAO_GHOSTED and that MyDocuments should be called for the SFGAO_ISSLOW and
     * SFGAO_FILESYSTEM attributes. */
    dwAttributes = SFGAO_ISSLOW|SFGAO_GHOSTED;
    RegSetValueExW(hKey, wszAttributes, 0, REG_DWORD, (LPBYTE)&dwAttributes, sizeof(DWORD));
    dwCallForAttributes = SFGAO_ISSLOW|SFGAO_FILESYSTEM;
    RegSetValueExW(hKey, wszCallForAttributes, 0, REG_DWORD, 
                   (LPBYTE)&dwCallForAttributes, sizeof(DWORD));

    /* Although it is not set in CallForAttributes, the SFGAO_GHOSTED flag is reset by 
     * GetAttributesOf. It seems that once there is a single attribute queried, for which
     * CallForAttributes is set, all flags are taken from the GetAttributesOf call and
     * the flags in Attributes are ignored. 
     */
    dwAttributes = SFGAO_ISSLOW|SFGAO_GHOSTED|SFGAO_FILESYSTEM;
    hr = IShellFolder_GetAttributesOf(psfDesktop, 1, 
                                      (LPCITEMIDLIST*)&pidlMyDocuments, &dwAttributes);
1012 1013
    ok (hr == S_OK, "Desktop->GetAttributesOf(MyDocuments) failed! hr = %08x\n", hr);
    if (hr == S_OK)
1014
        ok (dwAttributes == SFGAO_FILESYSTEM, 
1015
            "Desktop->GetAttributes(MyDocuments) returned unexpected attributes: %08x\n", 
1016 1017 1018 1019 1020 1021 1022
            dwAttributes);

    /* Restore MyDocuments' original Attributes and CallForAttributes registry values */
    RegSetValueExW(hKey, wszAttributes, 0, REG_DWORD, (LPBYTE)&dwOrigAttributes, sizeof(DWORD));
    RegSetValueExW(hKey, wszCallForAttributes, 0, REG_DWORD, 
                   (LPBYTE)&dwOrigCallForAttributes, sizeof(DWORD));
    RegCloseKey(hKey);
1023
    IMalloc_Free(ppM, pidlMyDocuments);
1024 1025 1026
    IShellFolder_Release(psfDesktop);
}

1027 1028 1029
static void test_GetAttributesOf(void) 
{
    HRESULT hr;
1030
    LPSHELLFOLDER psfDesktop, psfMyComputer;
1031 1032
    SHITEMID emptyitem = { 0, { 0 } };
    LPCITEMIDLIST pidlEmpty = (LPCITEMIDLIST)&emptyitem;
1033
    LPITEMIDLIST pidlMyComputer;
1034
    DWORD dwFlags;
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
    static const DWORD desktopFlags[] = {
        /* WinXP */
        SFGAO_STORAGE | SFGAO_HASPROPSHEET | SFGAO_STORAGEANCESTOR | SFGAO_FILESYSANCESTOR |
        SFGAO_FOLDER | SFGAO_FILESYSTEM | SFGAO_HASSUBFOLDER,
        /* Win2k */
        SFGAO_CANRENAME | SFGAO_HASPROPSHEET | SFGAO_STREAM | SFGAO_FILESYSANCESTOR |
        SFGAO_FOLDER | SFGAO_FILESYSTEM | SFGAO_HASSUBFOLDER,
        /* WinMe, Win9x, WinNT*/
        SFGAO_CANRENAME | SFGAO_HASPROPSHEET | SFGAO_FILESYSANCESTOR |
        SFGAO_FOLDER | SFGAO_FILESYSTEM | SFGAO_HASSUBFOLDER
    };
    static const DWORD myComputerFlags[] = {
        /* WinXP */
        SFGAO_CANRENAME | SFGAO_CANDELETE | SFGAO_HASPROPSHEET | SFGAO_DROPTARGET |
        SFGAO_FILESYSANCESTOR | SFGAO_FOLDER | SFGAO_HASSUBFOLDER,
        /* Win2k */
        SFGAO_CANRENAME | SFGAO_HASPROPSHEET | SFGAO_DROPTARGET | SFGAO_STREAM |
        SFGAO_FILESYSANCESTOR | SFGAO_FOLDER | SFGAO_HASSUBFOLDER,
        /* WinMe, Win9x, WinNT */
        SFGAO_CANRENAME | SFGAO_HASPROPSHEET | SFGAO_DROPTARGET | SFGAO_FILESYSANCESTOR |
        SFGAO_FOLDER | SFGAO_HASSUBFOLDER,
        /* Win95, WinNT when queried directly */
1057
        SFGAO_CANLINK | SFGAO_HASPROPSHEET | SFGAO_DROPTARGET | SFGAO_FILESYSANCESTOR |
1058 1059
        SFGAO_FOLDER | SFGAO_FILESYSTEM | SFGAO_HASSUBFOLDER
    };
1060 1061 1062
    WCHAR wszMyComputer[] = { 
        ':',':','{','2','0','D','0','4','F','E','0','-','3','A','E','A','-','1','0','6','9','-',
        'A','2','D','8','-','0','8','0','0','2','B','3','0','3','0','9','D','}',0 };
1063 1064
    char  cCurrDirA [MAX_PATH] = {0};
    WCHAR cCurrDirW [MAX_PATH];
1065
    static WCHAR cTestDirW[] = {'t','e','s','t','d','i','r',0};
1066 1067
    IShellFolder *IDesktopFolder, *testIShellFolder;
    ITEMIDLIST *newPIDL;
1068 1069
    int len, i;
    BOOL foundFlagsMatch;
1070

1071
    hr = SHGetDesktopFolder(&psfDesktop);
1072 1073
    ok (hr == S_OK, "SHGetDesktopFolder failed! hr = %08x\n", hr);
    if (hr != S_OK) return;
1074 1075 1076 1077

    /* The Desktop attributes can be queried with a single empty itemidlist, .. */
    dwFlags = 0xffffffff;
    hr = IShellFolder_GetAttributesOf(psfDesktop, 1, &pidlEmpty, &dwFlags);
1078
    ok (hr == S_OK, "Desktop->GetAttributesOf(empty pidl) failed! hr = %08x\n", hr);
1079 1080 1081 1082 1083 1084 1085
    for (i = 0, foundFlagsMatch = FALSE; !foundFlagsMatch &&
         i < sizeof(desktopFlags) / sizeof(desktopFlags[0]); i++)
    {
        if (desktopFlags[i] == dwFlags)
            foundFlagsMatch = TRUE;
    }
    ok (foundFlagsMatch, "Wrong Desktop attributes: %08x\n", dwFlags);
1086 1087 1088 1089

    /* .. or with no itemidlist at all. */
    dwFlags = 0xffffffff;
    hr = IShellFolder_GetAttributesOf(psfDesktop, 0, NULL, &dwFlags);
1090
    ok (hr == S_OK, "Desktop->GetAttributesOf(NULL) failed! hr = %08x\n", hr);
1091 1092 1093 1094 1095 1096 1097
    for (i = 0, foundFlagsMatch = FALSE; !foundFlagsMatch &&
         i < sizeof(desktopFlags) / sizeof(desktopFlags[0]); i++)
    {
        if (desktopFlags[i] == dwFlags)
            foundFlagsMatch = TRUE;
    }
    ok (foundFlagsMatch, "Wrong Desktop attributes: %08x\n", dwFlags);
1098 1099 1100
   
    /* Testing the attributes of the MyComputer shellfolder */
    hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszMyComputer, NULL, &pidlMyComputer, NULL);
1101 1102
    ok (hr == S_OK, "Desktop's ParseDisplayName failed to parse MyComputer's CLSID! hr = %08x\n", hr);
    if (hr != S_OK) {
1103 1104 1105 1106
        IShellFolder_Release(psfDesktop);
        return;
    }

1107
    /* Windows sets the SFGAO_CANLINK flag, when MyComputer is queried via the Desktop
1108 1109 1110 1111
     * folder object. It doesn't do this, if MyComputer is queried directly (see below).
     */
    dwFlags = 0xffffffff;
    hr = IShellFolder_GetAttributesOf(psfDesktop, 1, (LPCITEMIDLIST*)&pidlMyComputer, &dwFlags);
1112
    ok (hr == S_OK, "Desktop->GetAttributesOf(MyComputer) failed! hr = %08x\n", hr);
1113 1114 1115
    for (i = 0, foundFlagsMatch = FALSE; !foundFlagsMatch &&
         i < sizeof(myComputerFlags) / sizeof(myComputerFlags[0]); i++)
    {
1116
        if ((myComputerFlags[i] | SFGAO_CANLINK) == dwFlags)
1117 1118
            foundFlagsMatch = TRUE;
    }
1119
    todo_wine
1120
    ok (foundFlagsMatch, "Wrong MyComputer attributes: %08x\n", dwFlags);
1121 1122

    hr = IShellFolder_BindToObject(psfDesktop, pidlMyComputer, NULL, &IID_IShellFolder, (LPVOID*)&psfMyComputer);
1123
    ok (hr == S_OK, "Desktop failed to bind to MyComputer object! hr = %08x\n", hr);
1124
    IShellFolder_Release(psfDesktop);
1125
    IMalloc_Free(ppM, pidlMyComputer);
1126
    if (hr != S_OK) return;
1127 1128

    hr = IShellFolder_GetAttributesOf(psfMyComputer, 1, &pidlEmpty, &dwFlags);
1129 1130
    todo_wine
    ok (hr == E_INVALIDARG ||
1131
        broken(hr == S_OK), /* W2K and earlier */
1132
        "MyComputer->GetAttributesOf(emtpy pidl) should fail! hr = %08x\n", hr);
1133 1134 1135

    dwFlags = 0xffffffff;
    hr = IShellFolder_GetAttributesOf(psfMyComputer, 0, NULL, &dwFlags);
1136
    ok (hr == S_OK, "MyComputer->GetAttributesOf(NULL) failed! hr = %08x\n", hr);
1137 1138 1139 1140 1141 1142 1143 1144
    for (i = 0, foundFlagsMatch = FALSE; !foundFlagsMatch &&
         i < sizeof(myComputerFlags) / sizeof(myComputerFlags[0]); i++)
    {
        if (myComputerFlags[i] == dwFlags)
            foundFlagsMatch = TRUE;
    }
    todo_wine
    ok (foundFlagsMatch, "Wrong MyComputer attributes: %08x\n", dwFlags);
1145 1146

    IShellFolder_Release(psfMyComputer);
1147 1148 1149 1150 1151

    GetCurrentDirectoryA(MAX_PATH, cCurrDirA);
    len = lstrlenA(cCurrDirA);

    if (len == 0) {
1152 1153
        win_skip("GetCurrentDirectoryA returned empty string. Skipping test_GetAttributesOf\n");
        return;
1154
    }
1155 1156
    if (len > 3 && cCurrDirA[len-1] == '\\')
        cCurrDirA[len-1] = 0;
1157

1158 1159 1160
    /* create test directory */
    CreateFilesFolders();

1161 1162 1163
    MultiByteToWideChar(CP_ACP, 0, cCurrDirA, -1, cCurrDirW, MAX_PATH);
 
    hr = SHGetDesktopFolder(&IDesktopFolder);
1164
    ok(hr == S_OK, "SHGetDesktopfolder failed %08x\n", hr);
1165 1166

    hr = IShellFolder_ParseDisplayName(IDesktopFolder, NULL, NULL, cCurrDirW, NULL, &newPIDL, 0);
1167
    ok(hr == S_OK, "ParseDisplayName failed %08x\n", hr);
1168 1169

    hr = IShellFolder_BindToObject(IDesktopFolder, newPIDL, NULL, (REFIID)&IID_IShellFolder, (LPVOID *)&testIShellFolder);
1170
    ok(hr == S_OK, "BindToObject failed %08x\n", hr);
1171 1172 1173 1174

    IMalloc_Free(ppM, newPIDL);

    /* get relative PIDL */
1175
    hr = IShellFolder_ParseDisplayName(testIShellFolder, NULL, NULL, cTestDirW, NULL, &newPIDL, 0);
1176
    ok(hr == S_OK, "ParseDisplayName failed %08x\n", hr);
1177 1178 1179 1180

    /* test the shell attributes of the test directory using the relative PIDL */
    dwFlags = SFGAO_FOLDER;
    hr = IShellFolder_GetAttributesOf(testIShellFolder, 1, (LPCITEMIDLIST*)&newPIDL, &dwFlags);
1181
    ok (hr == S_OK, "Desktop->GetAttributesOf() failed! hr = %08x\n", hr);
1182
    ok ((dwFlags&SFGAO_FOLDER), "Wrong directory attribute for relative PIDL: %08x\n", dwFlags);
1183 1184 1185 1186 1187

    /* free memory */
    IMalloc_Free(ppM, newPIDL);

    /* append testdirectory name to path */
1188 1189
    if (cCurrDirA[len-1] == '\\')
        cCurrDirA[len-1] = 0;
1190 1191
    lstrcatA(cCurrDirA, "\\testdir");
    MultiByteToWideChar(CP_ACP, 0, cCurrDirA, -1, cCurrDirW, MAX_PATH);
1192 1193

    hr = IShellFolder_ParseDisplayName(IDesktopFolder, NULL, NULL, cCurrDirW, NULL, &newPIDL, 0);
1194
    ok(hr == S_OK, "ParseDisplayName failed %08x\n", hr);
1195 1196 1197 1198

    /* test the shell attributes of the test directory using the absolute PIDL */
    dwFlags = SFGAO_FOLDER;
    hr = IShellFolder_GetAttributesOf(IDesktopFolder, 1, (LPCITEMIDLIST*)&newPIDL, &dwFlags);
1199
    ok (hr == S_OK, "Desktop->GetAttributesOf() failed! hr = %08x\n", hr);
1200
    ok ((dwFlags&SFGAO_FOLDER), "Wrong directory attribute for absolute PIDL: %08x\n", dwFlags);
1201

1202 1203 1204
    /* free memory */
    IMalloc_Free(ppM, newPIDL);

1205
    IShellFolder_Release(testIShellFolder);
1206 1207 1208

    Cleanup();

1209
    IShellFolder_Release(IDesktopFolder);
1210
}
1211

1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223
static void test_SHGetPathFromIDList(void)
{
    SHITEMID emptyitem = { 0, { 0 } };
    LPCITEMIDLIST pidlEmpty = (LPCITEMIDLIST)&emptyitem;
    LPITEMIDLIST pidlMyComputer;
    WCHAR wszPath[MAX_PATH], wszDesktop[MAX_PATH];
    BOOL result;
    HRESULT hr;
    LPSHELLFOLDER psfDesktop;
    WCHAR wszMyComputer[] = { 
        ':',':','{','2','0','D','0','4','F','E','0','-','3','A','E','A','-','1','0','6','9','-',
        'A','2','D','8','-','0','8','0','0','2','B','3','0','3','0','9','D','}',0 };
1224 1225 1226 1227 1228 1229
    WCHAR wszFileName[MAX_PATH];
    LPITEMIDLIST pidlTestFile;
    HANDLE hTestFile;
    STRRET strret;
    static WCHAR wszTestFile[] = {
        'w','i','n','e','t','e','s','t','.','f','o','o',0 };
1230
    LPITEMIDLIST pidlPrograms;
1231

1232 1233
    if(!pSHGetPathFromIDListW || !pSHGetSpecialFolderPathW)
    {
1234
        win_skip("SHGetPathFromIDListW() or SHGetSpecialFolderPathW() is missing\n");
1235 1236
        return;
    }
1237

1238
    /* Calling SHGetPathFromIDListW with no pidl should return the empty string */
1239 1240
    wszPath[0] = 'a';
    wszPath[1] = '\0';
1241
    result = pSHGetPathFromIDListW(NULL, wszPath);
1242 1243 1244
    ok(!result, "Expected failure\n");
    ok(!wszPath[0], "Expected empty string\n");

1245
    /* Calling SHGetPathFromIDListW with an empty pidl should return the desktop folder's path. */
1246
    result = pSHGetSpecialFolderPathW(NULL, wszDesktop, CSIDL_DESKTOP, FALSE);
1247
    ok(result, "SHGetSpecialFolderPathW(CSIDL_DESKTOP) failed! Last error: %u\n", GetLastError());
1248
    if (!result) return;
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258

    /* Check if we are on Win9x */
    SetLastError(0xdeadbeef);
    lstrcmpiW(wszDesktop, wszDesktop);
    if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
    {
        win_skip("Most W-calls are not implemented\n");
        return;
    }

1259
    result = pSHGetPathFromIDListW(pidlEmpty, wszPath);
1260
    ok(result, "SHGetPathFromIDListW failed! Last error: %u\n", GetLastError());
1261
    if (!result) return;
1262
    ok(!lstrcmpiW(wszDesktop, wszPath), "SHGetPathFromIDListW didn't return desktop path for empty pidl!\n");
1263

1264
    /* MyComputer does not map to a filesystem path. SHGetPathFromIDListW should fail. */
1265
    hr = SHGetDesktopFolder(&psfDesktop);
1266 1267
    ok (hr == S_OK, "SHGetDesktopFolder failed! hr = %08x\n", hr);
    if (hr != S_OK) return;
1268 1269

    hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszMyComputer, NULL, &pidlMyComputer, NULL);
1270 1271
    ok (hr == S_OK, "Desktop's ParseDisplayName failed to parse MyComputer's CLSID! hr = %08x\n", hr);
    if (hr != S_OK) {
1272 1273 1274
        IShellFolder_Release(psfDesktop);
        return;
    }
1275 1276

    SetLastError(0xdeadbeef);
1277 1278
    wszPath[0] = 'a';
    wszPath[1] = '\0';
1279 1280
    result = pSHGetPathFromIDListW(pidlMyComputer, wszPath);
    ok (!result, "SHGetPathFromIDListW succeeded where it shouldn't!\n");
1281 1282 1283
    ok (GetLastError()==0xdeadbeef ||
        GetLastError()==ERROR_SUCCESS, /* Vista and higher */
        "Unexpected last error from SHGetPathFromIDListW: %u\n", GetLastError());
1284
    ok (!wszPath[0], "Expected empty path\n");
1285 1286 1287 1288
    if (result) {
        IShellFolder_Release(psfDesktop);
        return;
    }
1289

1290
    IMalloc_Free(ppM, pidlMyComputer);
1291 1292

    result = pSHGetSpecialFolderPathW(NULL, wszFileName, CSIDL_DESKTOPDIRECTORY, FALSE);
1293
    ok(result, "SHGetSpecialFolderPathW failed! Last error: %u\n", GetLastError());
1294 1295 1296 1297
    if (!result) {
        IShellFolder_Release(psfDesktop);
        return;
    }
1298
    myPathAddBackslashW(wszFileName);
1299 1300
    lstrcatW(wszFileName, wszTestFile);
    hTestFile = CreateFileW(wszFileName, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, NULL);
1301
    ok(hTestFile != INVALID_HANDLE_VALUE, "CreateFileW failed! Last error: %u\n", GetLastError());
1302 1303 1304 1305 1306 1307 1308
    if (hTestFile == INVALID_HANDLE_VALUE) {
        IShellFolder_Release(psfDesktop);
        return;
    }
    CloseHandle(hTestFile);

    hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszTestFile, NULL, &pidlTestFile, NULL);
1309 1310
    ok (hr == S_OK, "Desktop's ParseDisplayName failed to parse filename hr = %08x\n", hr);
    if (hr != S_OK) {
1311 1312 1313 1314 1315 1316 1317 1318 1319
        IShellFolder_Release(psfDesktop);
        DeleteFileW(wszFileName);
        IMalloc_Free(ppM, pidlTestFile);
        return;
    }

    /* This test is to show that the Desktop shellfolder prepends the CSIDL_DESKTOPDIRECTORY
     * path for files placed on the desktop, if called with SHGDN_FORPARSING. */
    hr = IShellFolder_GetDisplayNameOf(psfDesktop, pidlTestFile, SHGDN_FORPARSING, &strret);
1320
    ok (hr == S_OK, "Desktop's GetDisplayNamfOf failed! hr = %08x\n", hr);
1321 1322
    IShellFolder_Release(psfDesktop);
    DeleteFileW(wszFileName);
1323
    if (hr != S_OK) {
1324 1325 1326
        IMalloc_Free(ppM, pidlTestFile);
        return;
    }
1327 1328 1329 1330 1331 1332 1333
    if (pStrRetToBufW)
    {
        pStrRetToBufW(&strret, pidlTestFile, wszPath, MAX_PATH);
        ok(0 == lstrcmpW(wszFileName, wszPath), 
           "Desktop->GetDisplayNameOf(pidlTestFile, SHGDN_FORPARSING) "
           "returned incorrect path for file placed on desktop\n");
    }
1334

1335
    result = pSHGetPathFromIDListW(pidlTestFile, wszPath);
1336
    ok(result, "SHGetPathFromIDListW failed! Last error: %u\n", GetLastError());
1337 1338 1339
    IMalloc_Free(ppM, pidlTestFile);
    if (!result) return;
    ok(0 == lstrcmpW(wszFileName, wszPath), "SHGetPathFromIDListW returned incorrect path for file placed on desktop\n");
1340 1341


1342
    /* Test if we can get the path from the start menu "program files" PIDL. */
1343
    hr = pSHGetSpecialFolderLocation(NULL, CSIDL_PROGRAM_FILES, &pidlPrograms);
1344
    ok(hr == S_OK, "SHGetFolderLocation failed: 0x%08x\n", hr);
1345 1346

    SetLastError(0xdeadbeef);
1347
    result = pSHGetPathFromIDListW(pidlPrograms, wszPath);
1348
	IMalloc_Free(ppM, pidlPrograms);
1349
    ok(result, "SHGetPathFromIDListW failed\n");
1350 1351
}

1352
static void test_EnumObjects_and_CompareIDs(void)
1353 1354 1355
{
    ITEMIDLIST *newPIDL;
    IShellFolder *IDesktopFolder, *testIShellFolder;
1356
    char  cCurrDirA [MAX_PATH] = {0};
1357 1358
    static const CHAR cTestDirA[] = "\\testdir";
    WCHAR cTestDirW[MAX_PATH];
1359
    int len;
1360
    HRESULT hr;
1361

1362
    GetCurrentDirectoryA(MAX_PATH, cCurrDirA);
1363 1364 1365
    len = lstrlenA(cCurrDirA);

    if(len == 0) {
1366
        win_skip("GetCurrentDirectoryA returned empty string. Skipping test_EnumObjects_and_CompareIDs\n");
1367 1368 1369 1370 1371
        return;
    }
    if(cCurrDirA[len-1] == '\\')
        cCurrDirA[len-1] = 0;

1372 1373
    lstrcatA(cCurrDirA, cTestDirA);
    MultiByteToWideChar(CP_ACP, 0, cCurrDirA, -1, cTestDirW, MAX_PATH);
1374

1375
    hr = SHGetDesktopFolder(&IDesktopFolder);
1376
    ok(hr == S_OK, "SHGetDesktopfolder failed %08x\n", hr);
1377

1378 1379
    CreateFilesFolders();

1380
    hr = IShellFolder_ParseDisplayName(IDesktopFolder, NULL, NULL, cTestDirW, NULL, &newPIDL, 0);
1381
    ok(hr == S_OK, "ParseDisplayName failed %08x\n", hr);
1382

1383
    hr = IShellFolder_BindToObject(IDesktopFolder, newPIDL, NULL, (REFIID)&IID_IShellFolder, (LPVOID *)&testIShellFolder);
1384
    ok(hr == S_OK, "BindToObject failed %08x\n", hr);
1385

1386 1387
    test_EnumObjects(testIShellFolder);

1388
    IShellFolder_Release(testIShellFolder);
1389

1390
    Cleanup();
1391 1392

    IMalloc_Free(ppM, newPIDL);
1393

1394
    IShellFolder_Release(IDesktopFolder);
1395
}
1396

1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435
/* A simple implementation of an IPropertyBag, which returns fixed values for
 * 'Target' and 'Attributes' properties.
 */
static HRESULT WINAPI InitPropertyBag_IPropertyBag_QueryInterface(IPropertyBag *iface, REFIID riid,
    void **ppvObject) 
{
    if (!ppvObject)
        return E_INVALIDARG;

    if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IPropertyBag, riid)) {
        *ppvObject = iface;
    } else {
        ok (FALSE, "InitPropertyBag asked for unknown interface!\n");
        return E_NOINTERFACE;
    }

    IPropertyBag_AddRef(iface);
    return S_OK;
}

static ULONG WINAPI InitPropertyBag_IPropertyBag_AddRef(IPropertyBag *iface) {
    return 2;
}

static ULONG WINAPI InitPropertyBag_IPropertyBag_Release(IPropertyBag *iface) {
    return 1;
}

static HRESULT WINAPI InitPropertyBag_IPropertyBag_Read(IPropertyBag *iface, LPCOLESTR pszPropName,
    VARIANT *pVar, IErrorLog *pErrorLog)
{
    static const WCHAR wszTargetSpecialFolder[] = {
        'T','a','r','g','e','t','S','p','e','c','i','a','l','F','o','l','d','e','r',0 };
    static const WCHAR wszTarget[] = {
        'T','a','r','g','e','t',0 };
    static const WCHAR wszAttributes[] = {
        'A','t','t','r','i','b','u','t','e','s',0 };
    static const WCHAR wszResolveLinkFlags[] = {
        'R','e','s','o','l','v','e','L','i','n','k','F','l','a','g','s',0 };
1436 1437 1438 1439
    static const WCHAR wszTargetKnownFolder[] = {
        'T','a','r','g','e','t','K','n','o','w','n','F','o','l','d','e','r',0 };
    static const WCHAR wszCLSID[] = {
        'C','L','S','I','D',0 };
1440 1441
       
    if (!lstrcmpW(pszPropName, wszTargetSpecialFolder)) {
1442 1443 1444
        ok(V_VT(pVar) == VT_I4 ||
           broken(V_VT(pVar) == VT_BSTR),   /* Win2k */
           "Wrong variant type for 'TargetSpecialFolder' property!\n");
1445 1446 1447 1448
        return E_INVALIDARG;
    }
    
    if (!lstrcmpW(pszPropName, wszResolveLinkFlags)) 
1449
    {
1450
        ok(V_VT(pVar) == VT_UI4, "Wrong variant type for 'ResolveLinkFlags' property!\n");
1451 1452 1453 1454 1455 1456 1457
        return E_INVALIDARG;
    }

    if (!lstrcmpW(pszPropName, wszTarget)) {
        WCHAR wszPath[MAX_PATH];
        BOOL result;
        
1458 1459 1460
        ok(V_VT(pVar) == VT_BSTR ||
           broken(V_VT(pVar) == VT_EMPTY),  /* Win2k */
           "Wrong variant type for 'Target' property!\n");
1461 1462 1463
        if (V_VT(pVar) != VT_BSTR) return E_INVALIDARG;

        result = pSHGetSpecialFolderPathW(NULL, wszPath, CSIDL_DESKTOPDIRECTORY, FALSE);
1464
        ok(result, "SHGetSpecialFolderPathW(DESKTOPDIRECTORY) failed! %u\n", GetLastError());
1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478
        if (!result) return E_INVALIDARG;

        V_BSTR(pVar) = SysAllocString(wszPath);
        return S_OK;
    }

    if (!lstrcmpW(pszPropName, wszAttributes)) {
        ok(V_VT(pVar) == VT_UI4, "Wrong variant type for 'Attributes' property!\n");
        if (V_VT(pVar) != VT_UI4) return E_INVALIDARG;
        V_UI4(pVar) = SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR|
                      SFGAO_CANRENAME|SFGAO_FILESYSTEM;
        return S_OK;
    }

1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491
    if (!lstrcmpW(pszPropName, wszTargetKnownFolder)) {
        ok(V_VT(pVar) == VT_BSTR, "Wrong variant type for 'TargetKnownFolder' property!\n");
        /* TODO */
        return E_INVALIDARG;
    }

    if (!lstrcmpW(pszPropName, wszCLSID)) {
        ok(V_VT(pVar) == VT_EMPTY, "Wrong variant type for 'CLSID' property!\n");
        /* TODO */
        return E_INVALIDARG;
    }

    ok(FALSE, "PropertyBag was asked for unknown property %s (vt=%d)!\n", wine_dbgstr_w(pszPropName), V_VT(pVar));
1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509
    return E_INVALIDARG;
}

static HRESULT WINAPI InitPropertyBag_IPropertyBag_Write(IPropertyBag *iface, LPCOLESTR pszPropName,
    VARIANT *pVar)
{
    ok(FALSE, "Unexpected call to IPropertyBag_Write\n");
    return E_NOTIMPL;
}
    
static const IPropertyBagVtbl InitPropertyBag_IPropertyBagVtbl = {
    InitPropertyBag_IPropertyBag_QueryInterface,
    InitPropertyBag_IPropertyBag_AddRef,
    InitPropertyBag_IPropertyBag_Release,
    InitPropertyBag_IPropertyBag_Read,
    InitPropertyBag_IPropertyBag_Write
};

1510
static struct IPropertyBag InitPropertyBag = {
1511 1512 1513
    &InitPropertyBag_IPropertyBagVtbl
};

1514
static void test_FolderShortcut(void) {
1515
    IPersistPropertyBag *pPersistPropertyBag;
1516
    IShellFolder *pShellFolder, *pDesktopFolder;
1517 1518 1519
    IPersistFolder3 *pPersistFolder3;
    HRESULT hr;
    STRRET strret;
1520
    WCHAR wszDesktopPath[MAX_PATH], wszBuffer[MAX_PATH];
1521 1522
    BOOL result;
    CLSID clsid;
1523
    LPITEMIDLIST pidlCurrentFolder, pidlWineTestFolder, pidlSubFolder;
1524
    HKEY hShellExtKey;
1525 1526 1527
    WCHAR wszWineTestFolder[] = {
        ':',':','{','9','B','3','5','2','E','B','F','-','2','7','6','5','-','4','5','C','1','-',
        'B','4','C','6','-','8','5','C','C','7','F','7','A','B','C','6','4','}',0 };
1528 1529 1530 1531 1532 1533 1534 1535
    WCHAR wszShellExtKey[] = { 'S','o','f','t','w','a','r','e','\\',
        'M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\',
        'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
        'E','x','p','l','o','r','e','r','\\','D','e','s','k','t','o','p','\\',
        'N','a','m','e','S','p','a','c','e','\\',
        '{','9','b','3','5','2','e','b','f','-','2','7','6','5','-','4','5','c','1','-',
        'b','4','c','6','-','8','5','c','c','7','f','7','a','b','c','6','4','}',0 };
    
1536
    WCHAR wszSomeSubFolder[] = { 'S','u','b','F','o','l','d','e','r', 0};
1537 1538 1539
    static const GUID CLSID_UnixDosFolder = 
        {0x9d20aae8, 0x0625, 0x44b0, {0x9c, 0xa7, 0x71, 0x88, 0x9c, 0x22, 0x54, 0xd9}};

1540 1541 1542 1543
    if (!pSHGetSpecialFolderPathW || !pStrRetToBufW) {
        win_skip("SHGetSpecialFolderPathW and/or StrRetToBufW are not available\n");
        return;
    }
1544 1545 1546 1547 1548 1549 1550

    if (!pSHGetFolderPathAndSubDirA)
    {
        win_skip("FolderShortcut test doesn't work on Win2k\n");
        return;
    }

1551 1552 1553 1554 1555 1556
    /* These tests basically show, that CLSID_FolderShortcuts are initialized
     * via their IPersistPropertyBag interface. And that the target folder
     * is taken from the IPropertyBag's 'Target' property.
     */
    hr = CoCreateInstance(&CLSID_FolderShortcut, NULL, CLSCTX_INPROC_SERVER, 
                          &IID_IPersistPropertyBag, (LPVOID*)&pPersistPropertyBag);
1557 1558 1559 1560
    if (hr == REGDB_E_CLASSNOTREG) {
        win_skip("CLSID_FolderShortcut is not implemented\n");
        return;
    }
1561 1562
    ok (hr == S_OK, "CoCreateInstance failed! hr = 0x%08x\n", hr);
    if (hr != S_OK) return;
1563 1564

    hr = IPersistPropertyBag_Load(pPersistPropertyBag, &InitPropertyBag, NULL);
1565 1566
    ok(hr == S_OK, "IPersistPropertyBag_Load failed! hr = %08x\n", hr);
    if (hr != S_OK) {
1567 1568 1569
        IPersistPropertyBag_Release(pPersistPropertyBag);
        return;
    }
1570

1571 1572 1573
    hr = IPersistPropertyBag_QueryInterface(pPersistPropertyBag, &IID_IShellFolder, 
                                            (LPVOID*)&pShellFolder);
    IPersistPropertyBag_Release(pPersistPropertyBag);
1574 1575
    ok(hr == S_OK, "IPersistPropertyBag_QueryInterface(IShellFolder) failed! hr = %08x\n", hr);
    if (hr != S_OK) return;
1576 1577

    hr = IShellFolder_GetDisplayNameOf(pShellFolder, NULL, SHGDN_FORPARSING, &strret);
1578 1579
    ok(hr == S_OK, "IShellFolder_GetDisplayNameOf(NULL) failed! hr = %08x\n", hr);
    if (hr != S_OK) {
1580 1581 1582 1583
        IShellFolder_Release(pShellFolder);
        return;
    }

1584
    result = pSHGetSpecialFolderPathW(NULL, wszDesktopPath, CSIDL_DESKTOPDIRECTORY, FALSE);
1585
    ok(result, "SHGetSpecialFolderPathW(CSIDL_DESKTOPDIRECTORY) failed! %u\n", GetLastError());
1586 1587 1588
    if (!result) return;

    pStrRetToBufW(&strret, NULL, wszBuffer, MAX_PATH);
1589
    ok(!lstrcmpiW(wszDesktopPath, wszBuffer), "FolderShortcut returned incorrect folder!\n");
1590 1591 1592

    hr = IShellFolder_QueryInterface(pShellFolder, &IID_IPersistFolder3, (LPVOID*)&pPersistFolder3);
    IShellFolder_Release(pShellFolder);
1593 1594
    ok(hr == S_OK, "IShellFolder_QueryInterface(IID_IPersistFolder3 failed! hr = 0x%08x\n", hr);
    if (hr != S_OK) return;
1595 1596

    hr = IPersistFolder3_GetClassID(pPersistFolder3, &clsid);
1597
    ok(hr == S_OK, "IPersistFolder3_GetClassID failed! hr=0x%08x\n", hr);
1598 1599 1600
    ok(IsEqualCLSID(&clsid, &CLSID_FolderShortcut), "Unexpected CLSID!\n");

    hr = IPersistFolder3_GetCurFolder(pPersistFolder3, &pidlCurrentFolder);
1601
    todo_wine ok(hr == S_FALSE, "IPersistFolder3_GetCurFolder failed! hr=0x%08x\n", hr);
1602 1603
    ok(!pidlCurrentFolder, "IPersistFolder3_GetCurFolder should return a NULL pidl!\n");
                    
1604 1605 1606 1607 1608 1609 1610
    /* For FolderShortcut objects, the Initialize method initialized the folder's position in the
     * shell namespace. The target folder, read from the property bag above, remains untouched. 
     * The following tests show this: The itemidlist for some imaginary shellfolder object
     * is created and the FolderShortcut is initialized with it. GetCurFolder now returns this
     * itemidlist, but GetDisplayNameOf still returns the path from above.
     */
    hr = SHGetDesktopFolder(&pDesktopFolder);
1611 1612
    ok (hr == S_OK, "SHGetDesktopFolder failed! hr = %08x\n", hr);
    if (hr != S_OK) return;
1613

1614 1615 1616 1617
    /* Temporarily register WineTestFolder as a shell namespace extension at the Desktop. 
     * Otherwise ParseDisplayName fails on WinXP with E_INVALIDARG */
    RegCreateKeyW(HKEY_CURRENT_USER, wszShellExtKey, &hShellExtKey);
    RegCloseKey(hShellExtKey);
1618 1619
    hr = IShellFolder_ParseDisplayName(pDesktopFolder, NULL, NULL, wszWineTestFolder, NULL,
                                       &pidlWineTestFolder, NULL);
1620
    RegDeleteKeyW(HKEY_CURRENT_USER, wszShellExtKey);
1621
    IShellFolder_Release(pDesktopFolder);
1622 1623
    ok (hr == S_OK, "IShellFolder::ParseDisplayName failed! hr = %08x\n", hr);
    if (hr != S_OK) return;
1624 1625

    hr = IPersistFolder3_Initialize(pPersistFolder3, pidlWineTestFolder);
1626 1627
    ok (hr == S_OK, "IPersistFolder3::Initialize failed! hr = %08x\n", hr);
    if (hr != S_OK) {
1628
        IPersistFolder3_Release(pPersistFolder3);
1629
        pILFree(pidlWineTestFolder);
1630 1631
        return;
    }
1632

1633
    hr = IPersistFolder3_GetCurFolder(pPersistFolder3, &pidlCurrentFolder);
1634
    ok(hr == S_OK, "IPersistFolder3_GetCurFolder failed! hr=0x%08x\n", hr);
1635
    ok(pILIsEqual(pidlCurrentFolder, pidlWineTestFolder),
1636
        "IPersistFolder3_GetCurFolder should return pidlWineTestFolder!\n");
1637 1638 1639
    pILFree(pidlCurrentFolder);
    pILFree(pidlWineTestFolder);

1640 1641
    hr = IPersistFolder3_QueryInterface(pPersistFolder3, &IID_IShellFolder, (LPVOID*)&pShellFolder);
    IPersistFolder3_Release(pPersistFolder3);
1642 1643
    ok(hr == S_OK, "IPersistFolder3_QueryInterface(IShellFolder) failed! hr = %08x\n", hr);
    if (hr != S_OK) return;
1644 1645

    hr = IShellFolder_GetDisplayNameOf(pShellFolder, NULL, SHGDN_FORPARSING, &strret);
1646 1647
    ok(hr == S_OK, "IShellFolder_GetDisplayNameOf(NULL) failed! hr = %08x\n", hr);
    if (hr != S_OK) {
1648 1649 1650 1651 1652 1653 1654 1655 1656
        IShellFolder_Release(pShellFolder);
        return;
    }

    pStrRetToBufW(&strret, NULL, wszBuffer, MAX_PATH);
    ok(!lstrcmpiW(wszDesktopPath, wszBuffer), "FolderShortcut returned incorrect folder!\n");

    /* Next few lines are meant to show that children of FolderShortcuts are not FolderShortcuts,
     * but ShellFSFolders. */
1657
    myPathAddBackslashW(wszDesktopPath);
1658 1659 1660 1661 1662 1663 1664 1665
    lstrcatW(wszDesktopPath, wszSomeSubFolder);
    if (!CreateDirectoryW(wszDesktopPath, NULL)) {
        IShellFolder_Release(pShellFolder);
        return;
    }
    
    hr = IShellFolder_ParseDisplayName(pShellFolder, NULL, NULL, wszSomeSubFolder, NULL, 
                                       &pidlSubFolder, NULL);
1666
    RemoveDirectoryW(wszDesktopPath);
1667 1668
    ok (hr == S_OK, "IShellFolder::ParseDisplayName failed! hr = %08x\n", hr);
    if (hr != S_OK) {
1669 1670 1671 1672
        IShellFolder_Release(pShellFolder);
        return;
    }

1673
    hr = IShellFolder_BindToObject(pShellFolder, pidlSubFolder, NULL, &IID_IPersistFolder3,
1674 1675
                                   (LPVOID*)&pPersistFolder3);
    IShellFolder_Release(pShellFolder);
1676
    pILFree(pidlSubFolder);
1677 1678
    ok (hr == S_OK, "IShellFolder::BindToObject failed! hr = %08x\n", hr);
    if (hr != S_OK)
1679 1680
        return;

1681 1682
    /* On windows, we expect CLSID_ShellFSFolder. On wine we relax this constraint
     * a little bit and also allow CLSID_UnixDosFolder. */
1683
    hr = IPersistFolder3_GetClassID(pPersistFolder3, &clsid);
1684
    ok(hr == S_OK, "IPersistFolder3_GetClassID failed! hr=0x%08x\n", hr);
1685 1686
    ok(IsEqualCLSID(&clsid, &CLSID_ShellFSFolder) || IsEqualCLSID(&clsid, &CLSID_UnixDosFolder),
        "IPersistFolder3::GetClassID returned unexpected CLSID!\n");
1687

1688 1689 1690
    IPersistFolder3_Release(pPersistFolder3);
}

1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716
#include "pshpack1.h"
struct FileStructA {
    BYTE  type;
    BYTE  dummy;
    DWORD dwFileSize;
    WORD  uFileDate;    /* In our current implementation this is */
    WORD  uFileTime;    /* FileTimeToDosDate(WIN32_FIND_DATA->ftLastWriteTime) */
    WORD  uFileAttribs;
    CHAR  szName[1];
};

struct FileStructW {
    WORD  cbLen;        /* Length of this element. */
    BYTE  abFooBar1[6]; /* Beyond any recognition. */
    WORD  uDate;        /* FileTimeToDosDate(WIN32_FIND_DATA->ftCreationTime)? */
    WORD  uTime;        /* (this is currently speculation) */
    WORD  uDate2;       /* FileTimeToDosDate(WIN32_FIND_DATA->ftLastAccessTime)? */
    WORD  uTime2;       /* (this is currently speculation) */
    BYTE  abFooBar2[4]; /* Beyond any recognition. */
    WCHAR wszName[1];   /* The long filename in unicode. */
    /* Just for documentation: Right after the unicode string: */
    WORD  cbOffset;     /* FileStructW's offset from the beginning of the SHITMEID. 
                         * SHITEMID->cb == uOffset + cbLen */
};
#include "poppack.h"

1717
static void test_ITEMIDLIST_format(void) {
1718 1719 1720 1721 1722 1723
    WCHAR wszPersonal[MAX_PATH];
    LPSHELLFOLDER psfDesktop, psfPersonal;
    LPITEMIDLIST pidlPersonal, pidlFile;
    HANDLE hFile;
    HRESULT hr;
    BOOL bResult;
1724
    WCHAR wszFile[3][17] = { { 'e','v','e','n','_',0 }, { 'o','d','d','_',0 },
1725 1726
        { 'l','o','n','g','e','r','_','t','h','a','n','.','8','_','3',0 } };
    int i;
1727 1728

    if (!pSHGetSpecialFolderPathW) return;
1729 1730

    bResult = pSHGetSpecialFolderPathW(NULL, wszPersonal, CSIDL_PERSONAL, FALSE);
1731
    ok(bResult, "SHGetSpecialFolderPathW failed! Last error: %u\n", GetLastError());
1732 1733
    if (!bResult) return;

1734
    SetLastError(0xdeadbeef);
1735
    bResult = SetCurrentDirectoryW(wszPersonal);
1736 1737 1738 1739
    if (!bResult && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) {
        win_skip("Most W-calls are not implemented\n");
        return;
    }
1740
    ok(bResult, "SetCurrentDirectory failed! Last error: %u\n", GetLastError());
1741 1742 1743
    if (!bResult) return;

    hr = SHGetDesktopFolder(&psfDesktop);
1744 1745
    ok(hr == S_OK, "SHGetDesktopFolder failed! hr: %08x\n", hr);
    if (hr != S_OK) return;
1746 1747

    hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszPersonal, NULL, &pidlPersonal, NULL);
1748 1749
    ok(hr == S_OK, "psfDesktop->ParseDisplayName failed! hr = %08x\n", hr);
    if (hr != S_OK) {
1750 1751 1752 1753
        IShellFolder_Release(psfDesktop);
        return;
    }

1754
    hr = IShellFolder_BindToObject(psfDesktop, pidlPersonal, NULL, &IID_IShellFolder,
1755 1756
        (LPVOID*)&psfPersonal);
    IShellFolder_Release(psfDesktop);
1757
    pILFree(pidlPersonal);
1758 1759
    ok(hr == S_OK, "psfDesktop->BindToObject failed! hr = %08x\n", hr);
    if (hr != S_OK) return;
1760 1761 1762 1763 1764

    for (i=0; i<3; i++) {
        CHAR szFile[MAX_PATH];
        struct FileStructA *pFileStructA;
        WORD cbOffset;
1765

1766
        WideCharToMultiByte(CP_ACP, 0, wszFile[i], -1, szFile, MAX_PATH, NULL, NULL);
1767

1768
        hFile = CreateFileW(wszFile[i], GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_FLAG_WRITE_THROUGH, NULL);
1769
        ok(hFile != INVALID_HANDLE_VALUE, "CreateFile failed! (%u)\n", GetLastError());
1770 1771 1772 1773 1774 1775 1776 1777
        if (hFile == INVALID_HANDLE_VALUE) {
            IShellFolder_Release(psfPersonal);
            return;
        }
        CloseHandle(hFile);

        hr = IShellFolder_ParseDisplayName(psfPersonal, NULL, NULL, wszFile[i], NULL, &pidlFile, NULL);
        DeleteFileW(wszFile[i]);
1778 1779
        ok(hr == S_OK, "psfPersonal->ParseDisplayName failed! hr: %08x\n", hr);
        if (hr != S_OK) {
1780 1781 1782 1783 1784 1785 1786
            IShellFolder_Release(psfPersonal);
            return;
        }

        pFileStructA = (struct FileStructA *)pidlFile->mkid.abID;
        ok(pFileStructA->type == 0x32, "PIDLTYPE should be 0x32!\n");
        ok(pFileStructA->dummy == 0x00, "Dummy Byte should be 0x00!\n");
1787
        ok(pFileStructA->dwFileSize == 0, "Filesize should be zero!\n");
1788

1789
        if (i < 2) /* First two file names are already in valid 8.3 format */
1790
            ok(!strcmp(szFile, (CHAR*)&pidlFile->mkid.abID[12]), "Wrong file name!\n");
1791
        else
1792 1793 1794
            /* WinXP stores a derived 8.3 dos name (LONGER~1.8_3) here. We probably
             * can't implement this correctly, since unix filesystems don't support
             * this nasty short/long filename stuff. So we'll probably stay with our
1795
             * current habit of storing the long filename here, which seems to work
1796
             * just fine. */
1797 1798 1799 1800
            todo_wine
            ok(pidlFile->mkid.abID[18] == '~' ||
               broken(pidlFile->mkid.abID[34] == '~'),  /* Win2k */
               "Should be derived 8.3 name!\n");
1801 1802

        if (i == 0) /* First file name has an even number of chars. No need for alignment. */
1803 1804 1805 1806
            ok(pidlFile->mkid.abID[12 + strlen(szFile) + 1] != '\0' ||
               broken(pidlFile->mkid.cb == 2 + 12 + strlen(szFile) + 1 + 1),    /* Win2k */
                "Alignment byte, where there shouldn't be!\n");

1807
        if (i == 1) /* Second file name has an uneven number of chars => alignment byte */
1808
            ok(pidlFile->mkid.abID[12 + strlen(szFile) + 1] == '\0',
1809 1810 1811 1812
                "There should be an alignment byte, but isn't!\n");

        /* The offset of the FileStructW member is stored as a WORD at the end of the pidl. */
        cbOffset = *(WORD*)(((LPBYTE)pidlFile)+pidlFile->mkid.cb-sizeof(WORD));
1813 1814 1815 1816
        ok ((cbOffset >= sizeof(struct FileStructA) &&
            cbOffset <= pidlFile->mkid.cb - sizeof(struct FileStructW)) ||
            broken(pidlFile->mkid.cb == 2 + 12 + strlen(szFile) + 1 + 1) ||     /* Win2k on short names */
            broken(pidlFile->mkid.cb == 2 + 12 + strlen(szFile) + 1 + 12 + 1),  /* Win2k on long names */
1817
            "Wrong offset value (%d) stored at the end of the PIDL\n", cbOffset);
1818 1819

        if (cbOffset >= sizeof(struct FileStructA) &&
1820
            cbOffset <= pidlFile->mkid.cb - sizeof(struct FileStructW))
1821 1822
        {
            struct FileStructW *pFileStructW = (struct FileStructW *)(((LPBYTE)pidlFile)+cbOffset);
1823
            WCHAR *name = pFileStructW->wszName;
1824

1825
            ok(pidlFile->mkid.cb == cbOffset + pFileStructW->cbLen,
1826 1827 1828 1829
                "FileStructW's offset and length should add up to the PIDL's length!\n");

            if (pidlFile->mkid.cb == cbOffset + pFileStructW->cbLen) {
                /* Since we just created the file, time of creation,
1830 1831
                 * time of last access and time of last write access just be the same.
                 * These tests seem to fail sometimes (on WinXP), if the test is run again shortly
1832 1833 1834 1835 1836 1837
                 * after the first run. I do remember something with NTFS keeping the creation time
                 * if a file is deleted and then created again within a couple of seconds or so.
                 * Might be the reason. */
                ok (pFileStructA->uFileDate == pFileStructW->uDate &&
                    pFileStructA->uFileTime == pFileStructW->uTime,
                    "Last write time should match creation time!\n");
1838

1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858
                /* On FAT filesystems the last access time is midnight
                   local time, so the values of uDate2 and uTime2 will
                   depend on the local timezone.  If the times are exactly
                   equal then the dates should be identical for both FAT
                   and NTFS as no timezone is more than 1 day away from UTC.
                */
                if (pFileStructA->uFileTime == pFileStructW->uTime2)
                {
                    ok (pFileStructA->uFileDate == pFileStructW->uDate2,
                        "Last write date and time should match last access date and time!\n");
                }
                else
                {
                    /* Filesystem may be FAT. Check date within 1 day
                       and seconds are zero. */
                    trace ("Filesystem may be FAT. Performing less strict atime test.\n");
                    ok ((pFileStructW->uTime2 & 0x1F) == 0,
                        "Last access time on FAT filesystems should have zero seconds.\n");
                    /* TODO: Perform check for date being within one day.*/
                }
1859

1860 1861 1862
                ok (!lstrcmpW(wszFile[i], name) ||
                    !lstrcmpW(wszFile[i], name + 9) || /* Vista */
                    !lstrcmpW(wszFile[i], name + 11), /* Win7 */
1863 1864 1865 1866
                    "The filename should be stored in unicode at this position!\n");
            }
        }

1867
        pILFree(pidlFile);
1868
    }
1869 1870

    IShellFolder_Release(psfPersonal);
1871 1872
}

1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959
static void test_SHGetFolderPathA(void)
{
    static const BOOL is_win64 = sizeof(void *) > sizeof(int);
    BOOL is_wow64;
    char path[MAX_PATH];
    char path_x86[MAX_PATH];
    char path_key[MAX_PATH];
    HRESULT hr;
    HKEY key;

    if (!pSHGetFolderPathA)
    {
        win_skip("SHGetFolderPathA not present\n");
        return;
    }
    if (!pIsWow64Process || !pIsWow64Process( GetCurrentProcess(), &is_wow64 )) is_wow64 = FALSE;

    hr = pSHGetFolderPathA( 0, CSIDL_PROGRAM_FILES, 0, SHGFP_TYPE_CURRENT, path );
    ok( !hr, "SHGetFolderPathA failed %x\n", hr );
    hr = pSHGetFolderPathA( 0, CSIDL_PROGRAM_FILESX86, 0, SHGFP_TYPE_CURRENT, path_x86 );
    if (hr == E_FAIL)
    {
        win_skip( "Program Files (x86) not supported\n" );
        return;
    }
    ok( !hr, "SHGetFolderPathA failed %x\n", hr );
    if (is_win64)
    {
        ok( lstrcmpiA( path, path_x86 ), "paths are identical '%s'\n", path );
        ok( strstr( path, "x86" ) == NULL, "64-bit path '%s' contains x86\n", path );
        ok( strstr( path_x86, "x86" ) != NULL, "32-bit path '%s' doesn't contain x86\n", path_x86 );
    }
    else
    {
        ok( !lstrcmpiA( path, path_x86 ), "paths differ '%s' != '%s'\n", path, path_x86 );
        if (is_wow64)
            ok( strstr( path, "x86" ) != NULL, "32-bit path '%s' doesn't contain x86\n", path );
        else
            ok( strstr( path, "x86" ) == NULL, "32-bit path '%s' contains x86\n", path );
    }
    if (!RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows\\CurrentVersion", &key ))
    {
        DWORD type, count = sizeof(path_x86);
        if (!RegQueryValueExA( key, "ProgramFilesDir (x86)", NULL, &type, (BYTE *)path_key, &count ))
        {
            ok( is_win64 || is_wow64, "ProgramFilesDir (x86) exists on 32-bit setup\n" );
            ok( !lstrcmpiA( path_key, path_x86 ), "paths differ '%s' != '%s'\n", path_key, path_x86 );
        }
        else ok( !is_win64 && !is_wow64, "ProgramFilesDir (x86) should exist on 64-bit setup\n" );
        RegCloseKey( key );
    }

    hr = pSHGetFolderPathA( 0, CSIDL_PROGRAM_FILES_COMMON, 0, SHGFP_TYPE_CURRENT, path );
    ok( !hr, "SHGetFolderPathA failed %x\n", hr );
    hr = pSHGetFolderPathA( 0, CSIDL_PROGRAM_FILES_COMMONX86, 0, SHGFP_TYPE_CURRENT, path_x86 );
    if (hr == E_FAIL)
    {
        win_skip( "Common Files (x86) not supported\n" );
        return;
    }
    ok( !hr, "SHGetFolderPathA failed %x\n", hr );
    if (is_win64)
    {
        ok( lstrcmpiA( path, path_x86 ), "paths are identical '%s'\n", path );
        ok( strstr( path, "x86" ) == NULL, "64-bit path '%s' contains x86\n", path );
        ok( strstr( path_x86, "x86" ) != NULL, "32-bit path '%s' doesn't contain x86\n", path_x86 );
    }
    else
    {
        ok( !lstrcmpiA( path, path_x86 ), "paths differ '%s' != '%s'\n", path, path_x86 );
        if (is_wow64)
            ok( strstr( path, "x86" ) != NULL, "32-bit path '%s' doesn't contain x86\n", path );
        else
            ok( strstr( path, "x86" ) == NULL, "32-bit path '%s' contains x86\n", path );
    }
    if (!RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows\\CurrentVersion", &key ))
    {
        DWORD type, count = sizeof(path_x86);
        if (!RegQueryValueExA( key, "CommonFilesDir (x86)", NULL, &type, (BYTE *)path_key, &count ))
        {
            ok( is_win64 || is_wow64, "CommonFilesDir (x86) exists on 32-bit setup\n" );
            ok( !lstrcmpiA( path_key, path_x86 ), "paths differ '%s' != '%s'\n", path_key, path_x86 );
        }
        else ok( !is_win64 && !is_wow64, "CommonFilesDir (x86) should exist on 64-bit setup\n" );
    }
}

1960
static void test_SHGetFolderPathAndSubDirA(void)
1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971
{
    HRESULT ret;
    BOOL delret;
    DWORD dwret;
    int i;
    static char wine[] = "wine";
    static char winetemp[] = "wine\\temp";
    static char appdata[MAX_PATH];
    static char testpath[MAX_PATH];
    static char toolongpath[MAX_PATH+1];

1972 1973 1974 1975 1976 1977
    if(!pSHGetFolderPathAndSubDirA)
    {
        win_skip("SHGetFolderPathAndSubDirA not present!\n");
        return;
    }

1978
    if(!pSHGetFolderPathA) {
1979
        win_skip("SHGetFolderPathA not present!\n");
1980 1981
        return;
    }
1982
    if(FAILED(pSHGetFolderPathA(NULL, CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, appdata)))
1983
    {
1984
        win_skip("SHGetFolderPathA failed for CSIDL_LOCAL_APPDATA!\n");
1985 1986 1987 1988 1989 1990
        return;
    }

    sprintf(testpath, "%s\\%s", appdata, winetemp);
    delret = RemoveDirectoryA(testpath);
    if(!delret && (ERROR_PATH_NOT_FOUND != GetLastError()) ) {
1991
        win_skip("RemoveDirectoryA(%s) failed with error %u\n", testpath, GetLastError());
1992 1993 1994 1995 1996 1997
        return;
    }

    sprintf(testpath, "%s\\%s", appdata, wine);
    delret = RemoveDirectoryA(testpath);
    if(!delret && (ERROR_PATH_NOT_FOUND != GetLastError()) && (ERROR_FILE_NOT_FOUND != GetLastError())) {
1998
        win_skip("RemoveDirectoryA(%s) failed with error %u\n", testpath, GetLastError());
1999 2000 2001 2002 2003 2004 2005
        return;
    }

    /* test invalid second parameter */
    ret = pSHGetFolderPathAndSubDirA(NULL, CSIDL_FLAG_DONT_VERIFY | 0xff, NULL, SHGFP_TYPE_CURRENT, wine, testpath);
    ok(E_INVALIDARG == ret, "expected E_INVALIDARG, got  %x\n", ret);

2006 2007
    /* test fourth parameter */
    ret = pSHGetFolderPathAndSubDirA(NULL, CSIDL_FLAG_DONT_VERIFY | CSIDL_LOCAL_APPDATA, NULL, 2, winetemp, testpath);
2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019
    switch(ret) {
        case S_OK: /* winvista */
            ok(!strncmp(appdata, testpath, strlen(appdata)),
                "expected %s to start with %s\n", testpath, appdata);
            ok(!lstrcmpA(&testpath[1 + strlen(appdata)], winetemp),
                "expected %s to end with %s\n", testpath, winetemp);
            break;
        case E_INVALIDARG: /* winxp, win2k3 */
            break;
        default:
            ok(0, "expected S_OK or E_INVALIDARG, got  %x\n", ret);
    }
2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036

    /* test fifth parameter */
    testpath[0] = '\0';
    ret = pSHGetFolderPathAndSubDirA(NULL, CSIDL_FLAG_DONT_VERIFY | CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, NULL, testpath);
    ok(S_OK == ret, "expected S_OK, got %x\n", ret);
    ok(!lstrcmpA(appdata, testpath), "expected %s, got %s\n", appdata, testpath);

    testpath[0] = '\0';
    ret = pSHGetFolderPathAndSubDirA(NULL, CSIDL_FLAG_DONT_VERIFY | CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, "", testpath);
    ok(S_OK == ret, "expected S_OK, got %x\n", ret);
    ok(!lstrcmpA(appdata, testpath), "expected %s, got %s\n", appdata, testpath);

    testpath[0] = '\0';
    ret = pSHGetFolderPathAndSubDirA(NULL, CSIDL_FLAG_DONT_VERIFY | CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, "\\", testpath);
    ok(S_OK == ret, "expected S_OK, got %x\n", ret);
    ok(!lstrcmpA(appdata, testpath), "expected %s, got %s\n", appdata, testpath);

2037 2038 2039
    for(i=0; i< MAX_PATH; i++)
        toolongpath[i] = '0' + i % 10;
    toolongpath[MAX_PATH] = '\0';
2040 2041 2042 2043 2044 2045
    ret = pSHGetFolderPathAndSubDirA(NULL, CSIDL_FLAG_DONT_VERIFY | CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, toolongpath, testpath);
    ok(HRESULT_FROM_WIN32(ERROR_FILENAME_EXCED_RANGE) == ret,
        "expected %x, got %x\n", HRESULT_FROM_WIN32(ERROR_FILENAME_EXCED_RANGE), ret);

    testpath[0] = '\0';
    ret = pSHGetFolderPathAndSubDirA(NULL, CSIDL_FLAG_DONT_VERIFY | CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, wine, NULL);
2046
    ok((S_OK == ret) || (E_INVALIDARG == ret), "expected S_OK or E_INVALIDARG, got %x\n", ret);
2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071

    /* test a not existing path */
    testpath[0] = '\0';
    ret = pSHGetFolderPathAndSubDirA(NULL, CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, winetemp, testpath);
    ok(HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND) == ret,
        "expected %x, got %x\n", HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND), ret);

    /* create a directory inside a not existing directory */
    testpath[0] = '\0';
    ret = pSHGetFolderPathAndSubDirA(NULL, CSIDL_FLAG_CREATE | CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, winetemp, testpath);
    ok(S_OK == ret, "expected S_OK, got %x\n", ret);
    ok(!strncmp(appdata, testpath, strlen(appdata)),
        "expected %s to start with %s\n", testpath, appdata);
    ok(!lstrcmpA(&testpath[1 + strlen(appdata)], winetemp),
        "expected %s to end with %s\n", testpath, winetemp);
    dwret = GetFileAttributes(testpath);
    ok(FILE_ATTRIBUTE_DIRECTORY | dwret, "expected %x to contain FILE_ATTRIBUTE_DIRECTORY\n", dwret);

    /* cleanup */
    sprintf(testpath, "%s\\%s", appdata, winetemp);
    RemoveDirectoryA(testpath);
    sprintf(testpath, "%s\\%s", appdata, wine);
    RemoveDirectoryA(testpath);
}

2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083
static void test_LocalizedNames(void)
{
    static char cCurrDirA[MAX_PATH];
    WCHAR cCurrDirW[MAX_PATH], tempbufW[25];
    IShellFolder *IDesktopFolder, *testIShellFolder;
    ITEMIDLIST *newPIDL;
    int len;
    HRESULT hr;
    static char resourcefile[MAX_PATH];
    DWORD res;
    HANDLE file;
    STRRET strret;
2084
    BOOL ret;
2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103

    static const char desktopini_contents1[] =
        "[.ShellClassInfo]\r\n"
        "LocalizedResourceName=@";
    static const char desktopini_contents2[] =
        ",-1\r\n";
    static WCHAR foldernameW[] = {'t','e','s','t','f','o','l','d','e','r',0};
    static const WCHAR folderdisplayW[] = {'F','o','l','d','e','r',' ','N','a','m','e',' ','R','e','s','o','u','r','c','e',0};

    /* create folder with desktop.ini and localized name in GetModuleFileNameA(NULL) */
    CreateDirectoryA(".\\testfolder", NULL);

    SetFileAttributesA(".\\testfolder", GetFileAttributesA(".\\testfolder")|FILE_ATTRIBUTE_SYSTEM);

    GetModuleFileNameA(NULL, resourcefile, MAX_PATH);

    file = CreateFileA(".\\testfolder\\desktop.ini", GENERIC_WRITE, 0, NULL,
                         CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    ok(file != INVALID_HANDLE_VALUE, "CreateFileA failed %i\n", GetLastError());
2104 2105 2106 2107
    ret = WriteFile(file, desktopini_contents1, strlen(desktopini_contents1), &res, NULL) &&
          WriteFile(file, resourcefile, strlen(resourcefile), &res, NULL) &&
          WriteFile(file, desktopini_contents2, strlen(desktopini_contents2), &res, NULL);
    ok(ret, "WriteFile failed %i\n", GetLastError());
2108 2109 2110 2111 2112 2113 2114
    CloseHandle(file);

    /* get IShellFolder for parent */
    GetCurrentDirectoryA(MAX_PATH, cCurrDirA);
    len = lstrlenA(cCurrDirA);

    if (len == 0) {
2115
        win_skip("GetCurrentDirectoryA returned empty string. Skipping test_LocalizedNames\n");
2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138
        goto cleanup;
    }
    if(cCurrDirA[len-1] == '\\')
        cCurrDirA[len-1] = 0;

    MultiByteToWideChar(CP_ACP, 0, cCurrDirA, -1, cCurrDirW, MAX_PATH);

    hr = SHGetDesktopFolder(&IDesktopFolder);
    ok(hr == S_OK, "SHGetDesktopfolder failed %08x\n", hr);

    hr = IShellFolder_ParseDisplayName(IDesktopFolder, NULL, NULL, cCurrDirW, NULL, &newPIDL, 0);
    ok(hr == S_OK, "ParseDisplayName failed %08x\n", hr);

    hr = IShellFolder_BindToObject(IDesktopFolder, newPIDL, NULL, (REFIID)&IID_IShellFolder, (LPVOID *)&testIShellFolder);
    ok(hr == S_OK, "BindToObject failed %08x\n", hr);

    IMalloc_Free(ppM, newPIDL);

    /* windows reads the display name from the resource */
    hr = IShellFolder_ParseDisplayName(testIShellFolder, NULL, NULL, foldernameW, NULL, &newPIDL, 0);
    ok(hr == S_OK, "ParseDisplayName failed %08x\n", hr);

    hr = IShellFolder_GetDisplayNameOf(testIShellFolder, newPIDL, SHGDN_INFOLDER, &strret);
2139
    ok(hr == S_OK, "GetDisplayNameOf failed %08x\n", hr);
2140

2141
    if (hr == S_OK && pStrRetToBufW)
2142 2143
    {
        hr = pStrRetToBufW(&strret, newPIDL, tempbufW, sizeof(tempbufW)/sizeof(WCHAR));
2144
        ok (hr == S_OK, "StrRetToBufW failed! hr = %08x\n", hr);
2145 2146 2147 2148
        todo_wine
        ok (!lstrcmpiW(tempbufW, folderdisplayW) ||
            broken(!lstrcmpiW(tempbufW, foldernameW)), /* W2K */
            "GetDisplayNameOf returned %s\n", wine_dbgstr_w(tempbufW));
2149 2150 2151 2152
    }

    /* editing name is also read from the resource */
    hr = IShellFolder_GetDisplayNameOf(testIShellFolder, newPIDL, SHGDN_INFOLDER|SHGDN_FOREDITING, &strret);
2153
    ok(hr == S_OK, "GetDisplayNameOf failed %08x\n", hr);
2154

2155
    if (hr == S_OK && pStrRetToBufW)
2156 2157
    {
        hr = pStrRetToBufW(&strret, newPIDL, tempbufW, sizeof(tempbufW)/sizeof(WCHAR));
2158
        ok (hr == S_OK, "StrRetToBufW failed! hr = %08x\n", hr);
2159 2160 2161 2162
        todo_wine
        ok (!lstrcmpiW(tempbufW, folderdisplayW) ||
            broken(!lstrcmpiW(tempbufW, foldernameW)), /* W2K */
            "GetDisplayNameOf returned %s\n", wine_dbgstr_w(tempbufW));
2163 2164 2165 2166
    }

    /* parsing name is unchanged */
    hr = IShellFolder_GetDisplayNameOf(testIShellFolder, newPIDL, SHGDN_INFOLDER|SHGDN_FORPARSING, &strret);
2167
    ok(hr == S_OK, "GetDisplayNameOf failed %08x\n", hr);
2168

2169
    if (hr == S_OK && pStrRetToBufW)
2170 2171
    {
        hr = pStrRetToBufW(&strret, newPIDL, tempbufW, sizeof(tempbufW)/sizeof(WCHAR));
2172
        ok (hr == S_OK, "StrRetToBufW failed! hr = %08x\n", hr);
2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186
        ok (!lstrcmpiW(tempbufW, foldernameW), "GetDisplayNameOf returned %s\n", wine_dbgstr_w(tempbufW));
    }

    IShellFolder_Release(IDesktopFolder);
    IShellFolder_Release(testIShellFolder);

    IMalloc_Free(ppM, newPIDL);

cleanup:
    DeleteFileA(".\\testfolder\\desktop.ini");
    SetFileAttributesA(".\\testfolder", GetFileAttributesA(".\\testfolder")&~FILE_ATTRIBUTE_SYSTEM);
    RemoveDirectoryA(".\\testfolder");
}

2187 2188
static void test_SHCreateShellItem(void)
{
2189
    IShellItem *shellitem, *shellitem2;
2190
    IPersistIDList *persistidl;
2191
    LPITEMIDLIST pidl_cwd=NULL, pidl_testfile, pidl_abstestfile, pidl_test, pidl_desktop;
2192 2193 2194
    HRESULT ret;
    char curdirA[MAX_PATH];
    WCHAR curdirW[MAX_PATH];
2195
    WCHAR fnbufW[MAX_PATH];
2196 2197 2198 2199 2200
    IShellFolder *desktopfolder=NULL, *currentfolder=NULL;
    static WCHAR testfileW[] = {'t','e','s','t','f','i','l','e',0};

    GetCurrentDirectoryA(MAX_PATH, curdirA);

2201 2202 2203 2204 2205 2206
    if (!pSHCreateShellItem)
    {
        win_skip("SHCreateShellItem isn't available\n");
        return;
    }

2207 2208 2209 2210 2211 2212
    if (!lstrlenA(curdirA))
    {
        win_skip("GetCurrentDirectoryA returned empty string, skipping test_SHCreateShellItem\n");
        return;
    }

2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223
    if(pSHGetSpecialFolderLocation)
    {
        ret = pSHGetSpecialFolderLocation(NULL, CSIDL_DESKTOP, &pidl_desktop);
        ok(ret == S_OK, "Got 0x%08x\n", ret);
    }
    else
    {
        win_skip("pSHGetSpecialFolderLocation missing.\n");
        pidl_desktop = NULL;
    }

2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242
    MultiByteToWideChar(CP_ACP, 0, curdirA, -1, curdirW, MAX_PATH);

    ret = SHGetDesktopFolder(&desktopfolder);
    ok(SUCCEEDED(ret), "SHGetShellFolder returned %x\n", ret);

    ret = IShellFolder_ParseDisplayName(desktopfolder, NULL, NULL, curdirW, NULL, &pidl_cwd, NULL);
    ok(SUCCEEDED(ret), "ParseDisplayName returned %x\n", ret);

    ret = IShellFolder_BindToObject(desktopfolder, pidl_cwd, NULL, &IID_IShellFolder, (void**)&currentfolder);
    ok(SUCCEEDED(ret), "BindToObject returned %x\n", ret);

    CreateTestFile(".\\testfile");

    ret = IShellFolder_ParseDisplayName(currentfolder, NULL, NULL, testfileW, NULL, &pidl_testfile, NULL);
    ok(SUCCEEDED(ret), "ParseDisplayName returned %x\n", ret);

    pidl_abstestfile = pILCombine(pidl_cwd, pidl_testfile);

    ret = pSHCreateShellItem(NULL, NULL, NULL, &shellitem);
2243
    ok(ret == E_INVALIDARG, "SHCreateShellItem returned %x\n", ret);
2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273

    if (0) /* crashes on Windows XP */
    {
        pSHCreateShellItem(NULL, NULL, pidl_cwd, NULL);
        pSHCreateShellItem(pidl_cwd, NULL, NULL, &shellitem);
        pSHCreateShellItem(NULL, currentfolder, NULL, &shellitem);
        pSHCreateShellItem(pidl_cwd, currentfolder, NULL, &shellitem);
    }

    ret = pSHCreateShellItem(NULL, NULL, pidl_cwd, &shellitem);
    ok(SUCCEEDED(ret), "SHCreateShellItem returned %x\n", ret);
    if (SUCCEEDED(ret))
    {
        ret = IShellItem_QueryInterface(shellitem, &IID_IPersistIDList, (void**)&persistidl);
        ok(SUCCEEDED(ret), "QueryInterface returned %x\n", ret);
        if (SUCCEEDED(ret))
        {
            ret = IPersistIDList_GetIDList(persistidl, &pidl_test);
            ok(SUCCEEDED(ret), "GetIDList returned %x\n", ret);
            if (SUCCEEDED(ret))
            {
                ok(ILIsEqual(pidl_cwd, pidl_test), "id lists are not equal\n");
                pILFree(pidl_test);
            }
            IPersistIDList_Release(persistidl);
        }
        IShellItem_Release(shellitem);
    }

    ret = pSHCreateShellItem(pidl_cwd, NULL, pidl_testfile, &shellitem);
2274
    ok(SUCCEEDED(ret), "SHCreateShellItem returned %x\n", ret);
2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289
    if (SUCCEEDED(ret))
    {
        ret = IShellItem_QueryInterface(shellitem, &IID_IPersistIDList, (void**)&persistidl);
        ok(SUCCEEDED(ret), "QueryInterface returned %x\n", ret);
        if (SUCCEEDED(ret))
        {
            ret = IPersistIDList_GetIDList(persistidl, &pidl_test);
            ok(SUCCEEDED(ret), "GetIDList returned %x\n", ret);
            if (SUCCEEDED(ret))
            {
                ok(ILIsEqual(pidl_abstestfile, pidl_test), "id lists are not equal\n");
                pILFree(pidl_test);
            }
            IPersistIDList_Release(persistidl);
        }
2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310

        ret = IShellItem_GetParent(shellitem, &shellitem2);
        ok(SUCCEEDED(ret), "GetParent returned %x\n", ret);
        if (SUCCEEDED(ret))
        {
            ret = IShellItem_QueryInterface(shellitem2, &IID_IPersistIDList, (void**)&persistidl);
            ok(SUCCEEDED(ret), "QueryInterface returned %x\n", ret);
            if (SUCCEEDED(ret))
            {
                ret = IPersistIDList_GetIDList(persistidl, &pidl_test);
                ok(SUCCEEDED(ret), "GetIDList returned %x\n", ret);
                if (SUCCEEDED(ret))
                {
                    ok(ILIsEqual(pidl_cwd, pidl_test), "id lists are not equal\n");
                    pILFree(pidl_test);
                }
                IPersistIDList_Release(persistidl);
            }
            IShellItem_Release(shellitem2);
        }

2311 2312 2313 2314
        IShellItem_Release(shellitem);
    }

    ret = pSHCreateShellItem(NULL, currentfolder, pidl_testfile, &shellitem);
2315
    ok(SUCCEEDED(ret), "SHCreateShellItem returned %x\n", ret);
2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335
    if (SUCCEEDED(ret))
    {
        ret = IShellItem_QueryInterface(shellitem, &IID_IPersistIDList, (void**)&persistidl);
        ok(SUCCEEDED(ret), "QueryInterface returned %x\n", ret);
        if (SUCCEEDED(ret))
        {
            ret = IPersistIDList_GetIDList(persistidl, &pidl_test);
            ok(SUCCEEDED(ret), "GetIDList returned %x\n", ret);
            if (SUCCEEDED(ret))
            {
                ok(ILIsEqual(pidl_abstestfile, pidl_test), "id lists are not equal\n");
                pILFree(pidl_test);
            }
            IPersistIDList_Release(persistidl);
        }
        IShellItem_Release(shellitem);
    }

    /* if a parent pidl and shellfolder are specified, the shellfolder is ignored */
    ret = pSHCreateShellItem(pidl_cwd, desktopfolder, pidl_testfile, &shellitem);
2336
    ok(SUCCEEDED(ret), "SHCreateShellItem returned %x\n", ret);
2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354
    if (SUCCEEDED(ret))
    {
        ret = IShellItem_QueryInterface(shellitem, &IID_IPersistIDList, (void**)&persistidl);
        ok(SUCCEEDED(ret), "QueryInterface returned %x\n", ret);
        if (SUCCEEDED(ret))
        {
            ret = IPersistIDList_GetIDList(persistidl, &pidl_test);
            ok(SUCCEEDED(ret), "GetIDList returned %x\n", ret);
            if (SUCCEEDED(ret))
            {
                ok(ILIsEqual(pidl_abstestfile, pidl_test), "id lists are not equal\n");
                pILFree(pidl_test);
            }
            IPersistIDList_Release(persistidl);
        }
        IShellItem_Release(shellitem);
    }

2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371
    ret = pSHCreateShellItem(NULL, desktopfolder, pidl_testfile, &shellitem);
    ok(SUCCEEDED(ret), "SHCreateShellItem returned %x\n", ret);
    if (SUCCEEDED(ret))
    {
        ret = IShellItem_QueryInterface(shellitem, &IID_IPersistIDList, (void**)&persistidl);
        ok(SUCCEEDED(ret), "QueryInterface returned %x\n", ret);
        if (SUCCEEDED(ret))
        {
            ret = IPersistIDList_GetIDList(persistidl, &pidl_test);
            ok(SUCCEEDED(ret), "GetIDList returned %x\n", ret);
            if (SUCCEEDED(ret))
            {
                ok(ILIsEqual(pidl_testfile, pidl_test), "id lists are not equal\n");
                pILFree(pidl_test);
            }
            IPersistIDList_Release(persistidl);
        }
2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382

        IShellItem_Release(shellitem);
    }

    ret = pSHCreateShellItem(NULL, NULL, pidl_desktop, &shellitem);
    ok(SUCCEEDED(ret), "SHCreateShellItem returned %x\n", ret);
    if (SUCCEEDED(ret))
    {
        ret = IShellItem_GetParent(shellitem, &shellitem2);
        ok(FAILED(ret), "Got 0x%08x\n", ret);
        if(SUCCEEDED(ret)) IShellItem_Release(shellitem2);
2383 2384 2385
        IShellItem_Release(shellitem);
    }

2386 2387 2388 2389 2390 2391
    /* SHCreateItemFromParsingName */
    if(pSHCreateItemFromParsingName)
    {
        if(0)
        {
            /* Crashes under windows 7 */
2392
            pSHCreateItemFromParsingName(NULL, NULL, &IID_IShellItem, NULL);
2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426
        }

        shellitem = (void*)0xdeadbeef;
        ret = pSHCreateItemFromParsingName(NULL, NULL, &IID_IShellItem, (void**)&shellitem);
        ok(ret == E_INVALIDARG, "SHCreateItemFromParsingName returned %x\n", ret);
        ok(shellitem == NULL, "shellitem was %p.\n", shellitem);

        ret = pSHCreateItemFromParsingName(testfileW, NULL, &IID_IShellItem, (void**)&shellitem);
        ok(ret == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND),
           "SHCreateItemFromParsingName returned %x\n", ret);
        if(SUCCEEDED(ret)) IShellItem_Release(shellitem);

        lstrcpyW(fnbufW, curdirW);
        myPathAddBackslashW(fnbufW);
        lstrcatW(fnbufW, testfileW);

        ret = pSHCreateItemFromParsingName(fnbufW, NULL, &IID_IShellItem, (void**)&shellitem);
        ok(ret == S_OK, "SHCreateItemFromParsingName returned %x\n", ret);
        if(SUCCEEDED(ret))
        {
            LPWSTR tmp_fname;
            ret = IShellItem_GetDisplayName(shellitem, SIGDN_FILESYSPATH, &tmp_fname);
            ok(ret == S_OK, "GetDisplayName returned %x\n", ret);
            if(SUCCEEDED(ret))
            {
                ok(!lstrcmpW(fnbufW, tmp_fname), "strings not equal\n");
                CoTaskMemFree(tmp_fname);
            }
            IShellItem_Release(shellitem);
        }
    }
    else
        win_skip("No SHCreateItemFromParsingName\n");

2427 2428 2429 2430 2431 2432 2433

    /* SHCreateItemFromIDList */
    if(pSHCreateItemFromIDList)
    {
        if(0)
        {
            /* Crashes under win7 */
2434
            pSHCreateItemFromIDList(NULL, &IID_IShellItem, NULL);
2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482
        }

        ret = pSHCreateItemFromIDList(NULL, &IID_IShellItem, (void**)&shellitem);
        ok(ret == E_INVALIDARG, "SHCreateItemFromIDList returned %x\n", ret);

        ret = pSHCreateItemFromIDList(pidl_cwd, &IID_IShellItem, (void**)&shellitem);
        ok(ret == S_OK, "SHCreateItemFromIDList returned %x\n", ret);
        if (SUCCEEDED(ret))
        {
            ret = IShellItem_QueryInterface(shellitem, &IID_IPersistIDList, (void**)&persistidl);
            ok(ret == S_OK, "QueryInterface returned %x\n", ret);
            if (SUCCEEDED(ret))
            {
                ret = IPersistIDList_GetIDList(persistidl, &pidl_test);
                ok(ret == S_OK, "GetIDList returned %x\n", ret);
                if (SUCCEEDED(ret))
                {
                    ok(ILIsEqual(pidl_cwd, pidl_test), "id lists are not equal\n");
                    pILFree(pidl_test);
                }
                IPersistIDList_Release(persistidl);
            }
            IShellItem_Release(shellitem);
        }

        ret = pSHCreateItemFromIDList(pidl_testfile, &IID_IShellItem, (void**)&shellitem);
        ok(ret == S_OK, "SHCreateItemFromIDList returned %x\n", ret);
        if (SUCCEEDED(ret))
        {
            ret = IShellItem_QueryInterface(shellitem, &IID_IPersistIDList, (void**)&persistidl);
            ok(ret == S_OK, "QueryInterface returned %x\n", ret);
            if (SUCCEEDED(ret))
            {
                ret = IPersistIDList_GetIDList(persistidl, &pidl_test);
                ok(ret == S_OK, "GetIDList returned %x\n", ret);
                if (SUCCEEDED(ret))
                {
                    ok(ILIsEqual(pidl_testfile, pidl_test), "id lists are not equal\n");
                    pILFree(pidl_test);
                }
                IPersistIDList_Release(persistidl);
            }
            IShellItem_Release(shellitem);
        }
    }
    else
        win_skip("No SHCreateItemFromIDList\n");

2483 2484 2485
    DeleteFileA(".\\testfile");
    pILFree(pidl_abstestfile);
    pILFree(pidl_testfile);
2486
    pILFree(pidl_desktop);
2487 2488 2489 2490
    pILFree(pidl_cwd);
    IShellFolder_Release(currentfolder);
    IShellFolder_Release(desktopfolder);
}
2491

2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519
static void test_SHGetNameFromIDList(void)
{
    IShellItem *shellitem;
    LPITEMIDLIST pidl;
    LPWSTR name_string;
    HRESULT hres;
    UINT i;
    static const DWORD flags[] = {
        SIGDN_NORMALDISPLAY, SIGDN_PARENTRELATIVEPARSING,
        SIGDN_DESKTOPABSOLUTEPARSING,SIGDN_PARENTRELATIVEEDITING,
        SIGDN_DESKTOPABSOLUTEEDITING, /*SIGDN_FILESYSPATH, SIGDN_URL, */
        SIGDN_PARENTRELATIVEFORADDRESSBAR,SIGDN_PARENTRELATIVE, -1234};

    if(!pSHGetNameFromIDList)
    {
        win_skip("SHGetNameFromIDList missing.\n");
        return;
    }

    /* These should be available on any platform that passed the above test. */
    ok(pSHCreateShellItem != NULL, "SHCreateShellItem missing.\n");
    ok(pSHBindToParent != NULL, "SHBindToParent missing.\n");
    ok(pSHGetSpecialFolderLocation != NULL, "SHGetSpecialFolderLocation missing.\n");
    ok(pStrRetToBufW != NULL, "StrRetToBufW missing.\n");

    if(0)
    {
        /* Crashes under win7 */
2520
        pSHGetNameFromIDList(NULL, 0, NULL);
2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565
    }

    hres = pSHGetNameFromIDList(NULL, 0, &name_string);
    ok(hres == E_INVALIDARG, "Got 0x%08x\n", hres);

    /* Test the desktop */
    hres = pSHGetSpecialFolderLocation(NULL, CSIDL_DESKTOP, &pidl);
    ok(hres == S_OK, "Got 0x%08x\n", hres);
    hres = pSHCreateShellItem(NULL, NULL, pidl, &shellitem);
    ok(hres == S_OK, "Got 0x%08x\n", hres);
    if(SUCCEEDED(hres))
    {
        WCHAR *nameSI, *nameSH;
        WCHAR buf[MAX_PATH];
        HRESULT hrSI, hrSH, hrSF;
        STRRET strret;
        IShellFolder *psf;
        BOOL res;

        SHGetDesktopFolder(&psf);
        for(i = 0; flags[i] != -1234; i++)
        {
            hrSI = IShellItem_GetDisplayName(shellitem, flags[i], &nameSI);
            ok(hrSI == S_OK, "Got 0x%08x\n", hrSI);
            hrSH = pSHGetNameFromIDList(pidl, flags[i], &nameSH);
            ok(hrSH == S_OK, "Got 0x%08x\n", hrSH);
            hrSF = IShellFolder_GetDisplayNameOf(psf, pidl, flags[i] & 0xffff, &strret);
            ok(hrSF == S_OK, "Got 0x%08x\n", hrSF);

            if(SUCCEEDED(hrSI) && SUCCEEDED(hrSH))
                ok(!lstrcmpW(nameSI, nameSH), "Strings differ.\n");

            if(SUCCEEDED(hrSF))
            {
                pStrRetToBufW(&strret, NULL, buf, MAX_PATH);
                if(SUCCEEDED(hrSI))
                    ok(!lstrcmpW(nameSI, buf), "Strings differ.\n");
                if(SUCCEEDED(hrSF))
                    ok(!lstrcmpW(nameSI, buf), "Strings differ.\n");
            }
            if(SUCCEEDED(hrSI)) CoTaskMemFree(nameSI);
            if(SUCCEEDED(hrSH)) CoTaskMemFree(nameSH);
        }
        IShellFolder_Release(psf);

2566 2567 2568 2569 2570 2571 2572 2573 2574 2575
        if(pSHGetPathFromIDListW){
            hrSI = pSHGetNameFromIDList(pidl, SIGDN_FILESYSPATH, &nameSI);
            ok(hrSI == S_OK, "Got 0x%08x\n", hrSI);
            res = pSHGetPathFromIDListW(pidl, buf);
            ok(res == TRUE, "Got %d\n", res);
            if(SUCCEEDED(hrSI) && res)
                ok(!lstrcmpW(nameSI, buf), "Strings differ.\n");
            if(SUCCEEDED(hrSI)) CoTaskMemFree(nameSI);
        }else
            win_skip("pSHGetPathFromIDListW not available\n");
2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624

        hres = pSHGetNameFromIDList(pidl, SIGDN_URL, &name_string);
        todo_wine ok(hres == S_OK, "Got 0x%08x\n", hres);
        if(SUCCEEDED(hres)) CoTaskMemFree(name_string);

        IShellItem_Release(shellitem);
    }
    pILFree(pidl);

    /* Test the control panel */
    hres = pSHGetSpecialFolderLocation(NULL, CSIDL_CONTROLS, &pidl);
    ok(hres == S_OK, "Got 0x%08x\n", hres);
    hres = pSHCreateShellItem(NULL, NULL, pidl, &shellitem);
    ok(hres == S_OK, "Got 0x%08x\n", hres);
    if(SUCCEEDED(hres))
    {
        WCHAR *nameSI, *nameSH;
        WCHAR buf[MAX_PATH];
        HRESULT hrSI, hrSH, hrSF;
        STRRET strret;
        IShellFolder *psf;
        BOOL res;

        SHGetDesktopFolder(&psf);
        for(i = 0; flags[i] != -1234; i++)
        {
            hrSI = IShellItem_GetDisplayName(shellitem, flags[i], &nameSI);
            ok(hrSI == S_OK, "Got 0x%08x\n", hrSI);
            hrSH = pSHGetNameFromIDList(pidl, flags[i], &nameSH);
            ok(hrSH == S_OK, "Got 0x%08x\n", hrSH);
            hrSF = IShellFolder_GetDisplayNameOf(psf, pidl, flags[i] & 0xffff, &strret);
            ok(hrSF == S_OK, "Got 0x%08x\n", hrSF);

            if(SUCCEEDED(hrSI) && SUCCEEDED(hrSH))
                ok(!lstrcmpW(nameSI, nameSH), "Strings differ.\n");

            if(SUCCEEDED(hrSF))
            {
                pStrRetToBufW(&strret, NULL, buf, MAX_PATH);
                if(SUCCEEDED(hrSI))
                    ok(!lstrcmpW(nameSI, buf), "Strings differ.\n");
                if(SUCCEEDED(hrSF))
                    ok(!lstrcmpW(nameSI, buf), "Strings differ.\n");
            }
            if(SUCCEEDED(hrSI)) CoTaskMemFree(nameSI);
            if(SUCCEEDED(hrSH)) CoTaskMemFree(nameSH);
        }
        IShellFolder_Release(psf);

2625 2626 2627 2628 2629 2630 2631 2632 2633 2634
        if(pSHGetPathFromIDListW){
            hrSI = pSHGetNameFromIDList(pidl, SIGDN_FILESYSPATH, &nameSI);
            ok(hrSI == E_INVALIDARG, "Got 0x%08x\n", hrSI);
            res = pSHGetPathFromIDListW(pidl, buf);
            ok(res == FALSE, "Got %d\n", res);
            if(SUCCEEDED(hrSI) && res)
                ok(!lstrcmpW(nameSI, buf), "Strings differ.\n");
            if(SUCCEEDED(hrSI)) CoTaskMemFree(nameSI);
        }else
            win_skip("pSHGetPathFromIDListW not available\n");
2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645

        hres = pSHGetNameFromIDList(pidl, SIGDN_URL, &name_string);
        todo_wine ok(hres == E_NOTIMPL /* Win7 */ || hres == S_OK /* Vista */,
                     "Got 0x%08x\n", hres);
        if(SUCCEEDED(hres)) CoTaskMemFree(name_string);

        IShellItem_Release(shellitem);
    }
    pILFree(pidl);
}

2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661
static void test_SHGetItemFromDataObject(void)
{
    IShellFolder *psfdesktop;
    IShellItem *psi;
    IShellView *psv;
    HRESULT hres;

    if(!pSHGetItemFromDataObject)
    {
        win_skip("No SHGetItemFromDataObject.\n");
        return;
    }

    if(0)
    {
        /* Crashes under win7 */
2662
        pSHGetItemFromDataObject(NULL, 0, &IID_IShellItem, NULL);
2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740
    }

    hres = pSHGetItemFromDataObject(NULL, 0, &IID_IShellItem, (void**)&psv);
    ok(hres == E_INVALIDARG, "got 0x%08x\n", hres);

    SHGetDesktopFolder(&psfdesktop);

    hres = IShellFolder_CreateViewObject(psfdesktop, NULL, &IID_IShellView, (void**)&psv);
    ok(hres == S_OK, "got 0x%08x\n", hres);
    if(SUCCEEDED(hres))
    {
        IEnumIDList *peidl;
        IDataObject *pdo;
        SHCONTF enum_flags;

        enum_flags = SHCONTF_NONFOLDERS | SHCONTF_FOLDERS | SHCONTF_INCLUDEHIDDEN;
        hres = IShellFolder_EnumObjects(psfdesktop, NULL, enum_flags, &peidl);
        ok(hres == S_OK, "got 0x%08x\n", hres);
        if(SUCCEEDED(hres))
        {
            LPITEMIDLIST apidl[5];
            UINT count = 0, i;

            for(count = 0; count < 5; count++)
                if(IEnumIDList_Next(peidl, 1, &apidl[count], NULL) != S_OK)
                    break;

            if(count)
            {
                hres = IShellFolder_GetUIObjectOf(psfdesktop, NULL, 1, (LPCITEMIDLIST*)apidl,
                                                  &IID_IDataObject, NULL, (void**)&pdo);
                ok(hres == S_OK, "got 0x%08x\n", hres);
                if(SUCCEEDED(hres))
                {
                    hres = pSHGetItemFromDataObject(pdo, DOGIF_DEFAULT, &IID_IShellItem, (void**)&psi);
                    ok(hres == S_OK, "got 0x%08x\n", hres);
                    if(SUCCEEDED(hres)) IShellItem_Release(psi);
                    hres = pSHGetItemFromDataObject(pdo, DOGIF_TRAVERSE_LINK, &IID_IShellItem, (void**)&psi);
                    ok(hres == S_OK, "got 0x%08x\n", hres);
                    if(SUCCEEDED(hres)) IShellItem_Release(psi);
                    hres = pSHGetItemFromDataObject(pdo, DOGIF_NO_HDROP, &IID_IShellItem, (void**)&psi);
                    ok(hres == S_OK, "got 0x%08x\n", hres);
                    if(SUCCEEDED(hres)) IShellItem_Release(psi);
                    hres = pSHGetItemFromDataObject(pdo, DOGIF_NO_URL, &IID_IShellItem, (void**)&psi);
                    ok(hres == S_OK, "got 0x%08x\n", hres);
                    if(SUCCEEDED(hres)) IShellItem_Release(psi);
                    hres = pSHGetItemFromDataObject(pdo, DOGIF_ONLY_IF_ONE, &IID_IShellItem, (void**)&psi);
                    ok(hres == S_OK, "got 0x%08x\n", hres);
                    if(SUCCEEDED(hres)) IShellItem_Release(psi);

                    IDataObject_Release(pdo);
                }
            }
            else
                skip("No file(s) found - skipping single-file test.\n");

            if(count > 1)
            {
                hres = IShellFolder_GetUIObjectOf(psfdesktop, NULL, count, (LPCITEMIDLIST*)apidl,
                                                  &IID_IDataObject, NULL, (void**)&pdo);
                ok(hres == S_OK, "got 0x%08x\n", hres);
                if(SUCCEEDED(hres))
                {
                    hres = pSHGetItemFromDataObject(pdo, DOGIF_DEFAULT, &IID_IShellItem, (void**)&psi);
                    ok(hres == S_OK, "got 0x%08x\n", hres);
                    if(SUCCEEDED(hres)) IShellItem_Release(psi);
                    hres = pSHGetItemFromDataObject(pdo, DOGIF_TRAVERSE_LINK, &IID_IShellItem, (void**)&psi);
                    ok(hres == S_OK, "got 0x%08x\n", hres);
                    if(SUCCEEDED(hres)) IShellItem_Release(psi);
                    hres = pSHGetItemFromDataObject(pdo, DOGIF_NO_HDROP, &IID_IShellItem, (void**)&psi);
                    ok(hres == S_OK, "got 0x%08x\n", hres);
                    if(SUCCEEDED(hres)) IShellItem_Release(psi);
                    hres = pSHGetItemFromDataObject(pdo, DOGIF_NO_URL, &IID_IShellItem, (void**)&psi);
                    ok(hres == S_OK, "got 0x%08x\n", hres);
                    if(SUCCEEDED(hres)) IShellItem_Release(psi);
                    hres = pSHGetItemFromDataObject(pdo, DOGIF_ONLY_IF_ONE, &IID_IShellItem, (void**)&psi);
                    ok(hres == E_FAIL, "got 0x%08x\n", hres);
                    if(SUCCEEDED(hres)) IShellItem_Release(psi);
2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758
                    IDataObject_Release(pdo);
                }
            }
            else
                skip("zero or one file found - skipping multi-file test.\n");

            for(i = 0; i < count; i++)
                pILFree(apidl[i]);

            IEnumIDList_Release(peidl);
        }

        IShellView_Release(psv);
    }

    IShellFolder_Release(psfdesktop);
}

2759 2760 2761
static void test_ShellItemCompare(void)
{
    IShellItem *psi[9]; /* a\a, a\b, a\c, b\a, .. */
2762
    IShellItem *psi_a = NULL, *psi_b = NULL, *psi_c = NULL;
2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806
    IShellFolder *psf_desktop, *psf_current;
    LPITEMIDLIST pidl_cwd;
    WCHAR curdirW[MAX_PATH];
    BOOL failed;
    HRESULT hr;
    static const WCHAR filesW[][9] = {
        {'a','\\','a',0}, {'a','\\','b',0}, {'a','\\','c',0},
        {'b','\\','a',0}, {'b','\\','b',0}, {'b','\\','c',0},
        {'c','\\','a',0}, {'c','\\','b',0}, {'c','\\','c',0} };
    int order;
    UINT i;

    if(!pSHCreateShellItem)
    {
        win_skip("SHCreateShellItem missing.\n");
        return;
    }

    GetCurrentDirectoryW(MAX_PATH, curdirW);
    if(!lstrlenW(curdirW))
    {
        skip("Failed to get current directory, skipping.\n");
        return;
    }

    CreateDirectoryA(".\\a", NULL);
    CreateDirectoryA(".\\b", NULL);
    CreateDirectoryA(".\\c", NULL);
    CreateTestFile(".\\a\\a");
    CreateTestFile(".\\a\\b");
    CreateTestFile(".\\a\\c");
    CreateTestFile(".\\b\\a");
    CreateTestFile(".\\b\\b");
    CreateTestFile(".\\b\\c");
    CreateTestFile(".\\c\\a");
    CreateTestFile(".\\c\\b");
    CreateTestFile(".\\c\\c");

    SHGetDesktopFolder(&psf_desktop);
    hr = IShellFolder_ParseDisplayName(psf_desktop, NULL, NULL, curdirW, NULL, &pidl_cwd, NULL);
    ok(SUCCEEDED(hr), "ParseDisplayName returned %x\n", hr);
    hr = IShellFolder_BindToObject(psf_desktop, pidl_cwd, NULL, &IID_IShellFolder, (void**)&psf_current);
    ok(SUCCEEDED(hr), "BindToObject returned %x\n", hr);
    IShellFolder_Release(psf_desktop);
2807
    ILFree(pidl_cwd);
2808 2809

    /* Generate ShellItems for the files */
2810
    memset(&psi, 0, sizeof(psi));
2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828
    failed = FALSE;
    for(i = 0; i < 9; i++)
    {
        LPITEMIDLIST pidl_testfile = NULL;

        hr = IShellFolder_ParseDisplayName(psf_current, NULL, NULL, (LPWSTR)filesW[i],
                                           NULL, &pidl_testfile, NULL);
        ok(SUCCEEDED(hr), "ParseDisplayName returned %x\n", hr);
        if(SUCCEEDED(hr))
        {
            hr = pSHCreateShellItem(NULL, NULL, pidl_testfile, &psi[i]);
            ok(hr == S_OK, "Got 0x%08x\n", hr);
            pILFree(pidl_testfile);
        }
        if(FAILED(hr)) failed = TRUE;
    }
    if(failed)
    {
2829
        skip("Failed to create all shellitems.\n");
2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845
        goto cleanup;
    }

    /* Generate ShellItems for the folders */
    hr = IShellItem_GetParent(psi[0], &psi_a);
    ok(hr == S_OK, "Got 0x%08x\n", hr);
    if(FAILED(hr)) failed = TRUE;
    hr = IShellItem_GetParent(psi[3], &psi_b);
    ok(hr == S_OK, "Got 0x%08x\n", hr);
    if(FAILED(hr)) failed = TRUE;
    hr = IShellItem_GetParent(psi[6], &psi_c);
    ok(hr == S_OK, "Got 0x%08x\n", hr);
    if(FAILED(hr)) failed = TRUE;

    if(failed)
    {
2846
        skip("Failed to create shellitems.\n");
2847 2848 2849 2850 2851 2852
        goto cleanup;
    }

    if(0)
    {
        /* Crashes on native (win7, winxp) */
2853 2854 2855
        IShellItem_Compare(psi_a, NULL, 0, NULL);
        IShellItem_Compare(psi_a, psi_b, 0, NULL);
        IShellItem_Compare(psi_a, NULL, 0, &order);
2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984
    }

    /* Basics */
    for(i = 0; i < 9; i++)
    {
        hr = IShellItem_Compare(psi[i], psi[i], SICHINT_DISPLAY, &order);
        ok(hr == S_OK, "Got 0x%08x\n", hr);
        ok(order == 0, "Got order %d\n", order);
        hr = IShellItem_Compare(psi[i], psi[i], SICHINT_CANONICAL, &order);
        ok(hr == S_OK, "Got 0x%08x\n", hr);
        ok(order == 0, "Got order %d\n", order);
        hr = IShellItem_Compare(psi[i], psi[i], SICHINT_ALLFIELDS, &order);
        ok(hr == S_OK, "Got 0x%08x\n", hr);
        ok(order == 0, "Got order %d\n", order);
    }

    /* Order */
    /* a\b:a\a , a\b:a\c, a\b:a\b */
    hr = IShellItem_Compare(psi[1], psi[0], SICHINT_DISPLAY, &order);
    ok(hr == S_FALSE, "Got 0x%08x\n", hr);
    ok(order == 1, "Got order %d\n", order);
    hr = IShellItem_Compare(psi[1], psi[2], SICHINT_DISPLAY, &order);
    ok(hr == S_FALSE, "Got 0x%08x\n", hr);
    ok(order == -1, "Got order %d\n", order);
    hr = IShellItem_Compare(psi[1], psi[1], SICHINT_DISPLAY, &order);
    ok(hr == S_OK, "Got 0x%08x\n", hr);
    ok(order == 0, "Got order %d\n", order);

    /* b\b:a\b, b\b:c\b, b\b:c\b */
    hr = IShellItem_Compare(psi[4], psi[1], SICHINT_DISPLAY, &order);
    ok(hr == S_FALSE, "Got 0x%08x\n", hr);
    ok(order == 1, "Got order %d\n", order);
    hr = IShellItem_Compare(psi[4], psi[7], SICHINT_DISPLAY, &order);
    ok(hr == S_FALSE, "Got 0x%08x\n", hr);
    ok(order == -1, "Got order %d\n", order);
    hr = IShellItem_Compare(psi[4], psi[4], SICHINT_DISPLAY, &order);
    ok(hr == S_OK, "Got 0x%08x\n", hr);
    ok(order == 0, "Got order %d\n", order);

    /* b:a\a, b:a\c, b:a\b */
    hr = IShellItem_Compare(psi_b, psi[0], SICHINT_DISPLAY, &order);
    ok(hr == S_FALSE, "Got 0x%08x\n", hr);
    todo_wine ok(order == 1, "Got order %d\n", order);
    hr = IShellItem_Compare(psi_b, psi[2], SICHINT_DISPLAY, &order);
    ok(hr == S_FALSE, "Got 0x%08x\n", hr);
    todo_wine ok(order == 1, "Got order %d\n", order);
    hr = IShellItem_Compare(psi_b, psi[1], SICHINT_DISPLAY, &order);
    ok(hr == S_FALSE, "Got 0x%08x\n", hr);
    todo_wine ok(order == 1, "Got order %d\n", order);

    /* b:c\a, b:c\c, b:c\b */
    hr = IShellItem_Compare(psi_b, psi[6], SICHINT_DISPLAY, &order);
    ok(hr == S_FALSE, "Got 0x%08x\n", hr);
    ok(order == -1, "Got order %d\n", order);
    hr = IShellItem_Compare(psi_b, psi[8], SICHINT_DISPLAY, &order);
    ok(hr == S_FALSE, "Got 0x%08x\n", hr);
    ok(order == -1, "Got order %d\n", order);
    hr = IShellItem_Compare(psi_b, psi[7], SICHINT_DISPLAY, &order);
    ok(hr == S_FALSE, "Got 0x%08x\n", hr);
    ok(order == -1, "Got order %d\n", order);

    /* a\b:a\a , a\b:a\c, a\b:a\b */
    hr = IShellItem_Compare(psi[1], psi[0], SICHINT_CANONICAL, &order);
    ok(hr == S_FALSE, "Got 0x%08x\n", hr);
    ok(order == 1, "Got order %d\n", order);
    hr = IShellItem_Compare(psi[1], psi[2], SICHINT_CANONICAL, &order);
    ok(hr == S_FALSE, "Got 0x%08x\n", hr);
    ok(order == -1, "Got order %d\n", order);
    hr = IShellItem_Compare(psi[1], psi[1], SICHINT_CANONICAL, &order);
    ok(hr == S_OK, "Got 0x%08x\n", hr);
    ok(order == 0, "Got order %d\n", order);

    /* b\b:a\b, b\b:c\b, b\b:c\b */
    hr = IShellItem_Compare(psi[4], psi[1], SICHINT_CANONICAL, &order);
    ok(hr == S_FALSE, "Got 0x%08x\n", hr);
    ok(order == 1, "Got order %d\n", order);
    hr = IShellItem_Compare(psi[4], psi[7], SICHINT_CANONICAL, &order);
    ok(hr == S_FALSE, "Got 0x%08x\n", hr);
    ok(order == -1, "Got order %d\n", order);
    hr = IShellItem_Compare(psi[4], psi[4], SICHINT_CANONICAL, &order);
    ok(hr == S_OK, "Got 0x%08x\n", hr);
    ok(order == 0, "Got order %d\n", order);

    /* b:a\a, b:a\c, b:a\b */
    hr = IShellItem_Compare(psi_b, psi[0], SICHINT_CANONICAL, &order);
    ok(hr == S_FALSE, "Got 0x%08x\n", hr);
    todo_wine ok(order == 1, "Got order %d\n", order);
    hr = IShellItem_Compare(psi_b, psi[2], SICHINT_CANONICAL, &order);
    ok(hr == S_FALSE, "Got 0x%08x\n", hr);
    todo_wine ok(order == 1, "Got order %d\n", order);
    hr = IShellItem_Compare(psi_b, psi[1], SICHINT_CANONICAL, &order);
    ok(hr == S_FALSE, "Got 0x%08x\n", hr);
    todo_wine ok(order == 1, "Got order %d\n", order);

    /* b:c\a, b:c\c, b:c\b */
    hr = IShellItem_Compare(psi_b, psi[6], SICHINT_CANONICAL, &order);
    ok(hr == S_FALSE, "Got 0x%08x\n", hr);
    ok(order == -1, "Got order %d\n", order);
    hr = IShellItem_Compare(psi_b, psi[8], SICHINT_CANONICAL, &order);
    ok(hr == S_FALSE, "Got 0x%08x\n", hr);
    ok(order == -1, "Got order %d\n", order);
    hr = IShellItem_Compare(psi_b, psi[7], SICHINT_CANONICAL, &order);
    ok(hr == S_FALSE, "Got 0x%08x\n", hr);
    ok(order == -1, "Got order %d\n", order);

cleanup:
    IShellFolder_Release(psf_current);

    DeleteFileA(".\\a\\a");
    DeleteFileA(".\\a\\b");
    DeleteFileA(".\\a\\c");
    DeleteFileA(".\\b\\a");
    DeleteFileA(".\\b\\b");
    DeleteFileA(".\\b\\c");
    DeleteFileA(".\\c\\a");
    DeleteFileA(".\\c\\b");
    DeleteFileA(".\\c\\c");
    RemoveDirectoryA(".\\a");
    RemoveDirectoryA(".\\b");
    RemoveDirectoryA(".\\c");

    if(psi_a) IShellItem_Release(psi_a);
    if(psi_b) IShellItem_Release(psi_b);
    if(psi_c) IShellItem_Release(psi_c);

    for(i = 0; i < 9; i++)
        if(psi[i]) IShellItem_Release(psi[i]);
}

2985 2986 2987
/**************************************************************/
/* IUnknown implementation for counting QueryInterface calls. */
typedef struct {
2988
    IUnknown IUnknown_iface;
2989 2990 2991 2992 2993 2994 2995
    struct if_count {
        REFIID id;
        LONG count;
    } *ifaces;
    LONG unknown;
} IUnknownImpl;

2996 2997 2998 2999 3000
static inline IUnknownImpl *impl_from_IUnknown(IUnknown *iface)
{
    return CONTAINING_RECORD(iface, IUnknownImpl, IUnknown_iface);
}

3001 3002
static HRESULT WINAPI unk_fnQueryInterface(IUnknown *iunk, REFIID riid, void** punk)
{
3003
    IUnknownImpl *This = impl_from_IUnknown(iunk);
3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028
    UINT i, found;
    for(i = found = 0; This->ifaces[i].id != NULL; i++)
    {
        if(IsEqualIID(This->ifaces[i].id, riid))
        {
            This->ifaces[i].count++;
            found = 1;
            break;
        }
    }
    if(!found)
        This->unknown++;
    return E_NOINTERFACE;
}

static ULONG WINAPI unk_fnAddRef(IUnknown *iunk)
{
    return 2;
}

static ULONG WINAPI unk_fnRelease(IUnknown *iunk)
{
    return 1;
}

3029
static const IUnknownVtbl vt_IUnknown = {
3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069
    unk_fnQueryInterface,
    unk_fnAddRef,
    unk_fnRelease
};

static void test_SHGetIDListFromObject(void)
{
    IUnknownImpl *punkimpl;
    IShellFolder *psfdesktop;
    IShellView *psv;
    LPITEMIDLIST pidl, pidl_desktop;
    HRESULT hres;
    UINT i;
    struct if_count ifaces[] =
        { {&IID_IPersistIDList, 0},
          {&IID_IPersistFolder2, 0},
          {&IID_IDataObject, 0},
          {&IID_IParentAndItem, 0},
          {&IID_IFolderView, 0},
          {NULL, 0} };

    if(!pSHGetIDListFromObject)
    {
        win_skip("SHGetIDListFromObject missing.\n");
        return;
    }

    ok(pSHGetSpecialFolderLocation != NULL, "SHGetSpecialFolderLocation missing.\n");

    if(0)
    {
        /* Crashes native */
        pSHGetIDListFromObject(NULL, NULL);
        pSHGetIDListFromObject((void*)0xDEADBEEF, NULL);
    }

    hres = pSHGetIDListFromObject(NULL, &pidl);
    ok(hres == E_NOINTERFACE, "Got %x\n", hres);

    punkimpl = HeapAlloc(GetProcessHeap(), 0, sizeof(IUnknownImpl));
3070
    punkimpl->IUnknown_iface.lpVtbl = &vt_IUnknown;
3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184
    punkimpl->ifaces = ifaces;
    punkimpl->unknown = 0;

    hres = pSHGetIDListFromObject((IUnknown*)punkimpl, &pidl);
    ok(hres == E_NOINTERFACE, "Got %x\n", hres);
    ok(ifaces[0].count, "interface not requested.\n");
    ok(ifaces[1].count, "interface not requested.\n");
    ok(ifaces[2].count, "interface not requested.\n");
    todo_wine
        ok(ifaces[3].count || broken(!ifaces[3].count /*vista*/),
           "interface not requested.\n");
    ok(ifaces[4].count || broken(!ifaces[4].count /*vista*/),
       "interface not requested.\n");

    ok(!punkimpl->unknown, "Got %d unknown.\n", punkimpl->unknown);
    HeapFree(GetProcessHeap(), 0, punkimpl);

    pidl_desktop = NULL;
    pSHGetSpecialFolderLocation(NULL, CSIDL_DESKTOP, &pidl_desktop);
    ok(pidl_desktop != NULL, "Failed to get desktop pidl.\n");

    SHGetDesktopFolder(&psfdesktop);

    /* Test IShellItem */
    if(pSHCreateShellItem)
    {
        IShellItem *shellitem;
        hres = pSHCreateShellItem(NULL, NULL, pidl_desktop, &shellitem);
        ok(hres == S_OK, "got 0x%08x\n", hres);
        if(SUCCEEDED(hres))
        {
            hres = pSHGetIDListFromObject((IUnknown*)shellitem, &pidl);
            ok(hres == S_OK, "got 0x%08x\n", hres);
            if(SUCCEEDED(hres))
            {
                ok(ILIsEqual(pidl_desktop, pidl), "pidl not equal.\n");
                pILFree(pidl);
            }
            IShellItem_Release(shellitem);
        }
    }
    else
        skip("no SHCreateShellItem.\n");

    /* Test IShellFolder */
    hres = pSHGetIDListFromObject((IUnknown*)psfdesktop, &pidl);
    ok(hres == S_OK, "got 0x%08x\n", hres);
    if(SUCCEEDED(hres))
    {
        ok(ILIsEqual(pidl_desktop, pidl), "pidl not equal.\n");
        pILFree(pidl);
    }

    hres = IShellFolder_CreateViewObject(psfdesktop, NULL, &IID_IShellView, (void**)&psv);
    ok(hres == S_OK, "got 0x%08x\n", hres);
    if(SUCCEEDED(hres))
    {
        IEnumIDList *peidl;
        IDataObject *pdo;
        SHCONTF enum_flags;

        /* Test IFolderView */
        hres = pSHGetIDListFromObject((IUnknown*)psv, &pidl);
        ok(hres == S_OK, "got 0x%08x\n", hres);
        if(SUCCEEDED(hres))
        {
            ok(ILIsEqual(pidl_desktop, pidl), "pidl not equal.\n");
            pILFree(pidl);
        }

        /* Test IDataObject */
        enum_flags = SHCONTF_NONFOLDERS | SHCONTF_FOLDERS | SHCONTF_INCLUDEHIDDEN;
        hres = IShellFolder_EnumObjects(psfdesktop, NULL, enum_flags, &peidl);
        ok(hres == S_OK, "got 0x%08x\n", hres);
        if(SUCCEEDED(hres))
        {
            LPITEMIDLIST apidl[5];
            UINT count = 0;
            for(count = 0; count < 5; count++)
                if(IEnumIDList_Next(peidl, 1, &apidl[count], NULL) != S_OK)
                    break;

            if(count)
            {
                hres = IShellFolder_GetUIObjectOf(psfdesktop, NULL, 1, (LPCITEMIDLIST*)apidl,
                                                  &IID_IDataObject, NULL, (void**)&pdo);
                ok(hres == S_OK, "got 0x%08x\n", hres);
                if(SUCCEEDED(hres))
                {
                    pidl = (void*)0xDEADBEEF;
                    hres = pSHGetIDListFromObject((IUnknown*)pdo, &pidl);
                    ok(hres == S_OK, "got 0x%08x\n", hres);
                    ok(pidl != NULL, "pidl is NULL.\n");
                    ok(ILIsEqual(pidl, apidl[0]), "pidl not equal.\n");
                    pILFree(pidl);

                    IDataObject_Release(pdo);
                }
            }
            else
                skip("No files found - skipping single-file test.\n");

            if(count > 1)
            {
                hres = IShellFolder_GetUIObjectOf(psfdesktop, NULL, count, (LPCITEMIDLIST*)apidl,
                                                  &IID_IDataObject, NULL, (void**)&pdo);
                ok(hres == S_OK, "got 0x%08x\n", hres);
                if(SUCCEEDED(hres))
                {
                    pidl = (void*)0xDEADBEEF;
                    hres = pSHGetIDListFromObject((IUnknown*)pdo, &pidl);
                    ok(hres == E_NOINTERFACE || hres == E_FAIL /*Vista*/,
                       "got 0x%08x\n", hres);
                    ok(pidl == NULL, "pidl is not NULL.\n");
3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201

                    IDataObject_Release(pdo);
                }
            }
            else
                skip("zero or one file found - skipping multi-file test.\n");

            for(i = 0; i < count; i++)
                pILFree(apidl[i]);

            IEnumIDList_Release(peidl);
        }

        IShellView_Release(psv);
    }

    IShellFolder_Release(psfdesktop);
3202
    pILFree(pidl_desktop);
3203 3204
}

3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231
static void test_SHGetItemFromObject(void)
{
    IUnknownImpl *punkimpl;
    IShellFolder *psfdesktop;
    LPITEMIDLIST pidl;
    IShellItem *psi;
    IUnknown *punk;
    HRESULT hres;
    struct if_count ifaces[] =
        { {&IID_IPersistIDList, 0},
          {&IID_IPersistFolder2, 0},
          {&IID_IDataObject, 0},
          {&IID_IParentAndItem, 0},
          {&IID_IFolderView, 0},
          {NULL, 0} };

    if(!pSHGetItemFromObject)
    {
        skip("No SHGetItemFromObject.\n");
        return;
    }

    SHGetDesktopFolder(&psfdesktop);

    if(0)
    {
        /* Crashes with Windows 7 */
3232 3233 3234
        pSHGetItemFromObject((IUnknown*)psfdesktop, &IID_IUnknown, NULL);
        pSHGetItemFromObject(NULL, &IID_IUnknown, NULL);
        pSHGetItemFromObject((IUnknown*)psfdesktop, NULL, (void**)&punk);
3235 3236 3237 3238 3239 3240
    }

    hres = pSHGetItemFromObject(NULL, &IID_IUnknown, (void**)&punk);
    ok(hres == E_NOINTERFACE, "Got 0x%08x\n", hres);

    punkimpl = HeapAlloc(GetProcessHeap(), 0, sizeof(IUnknownImpl));
3241
    punkimpl->IUnknown_iface.lpVtbl = &vt_IUnknown;
3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279
    punkimpl->ifaces = ifaces;
    punkimpl->unknown = 0;

    /* The same as SHGetIDListFromObject */
    hres = pSHGetIDListFromObject((IUnknown*)punkimpl, &pidl);
    ok(hres == E_NOINTERFACE, "Got %x\n", hres);
    ok(ifaces[0].count, "interface not requested.\n");
    ok(ifaces[1].count, "interface not requested.\n");
    ok(ifaces[2].count, "interface not requested.\n");
    todo_wine
        ok(ifaces[3].count || broken(!ifaces[3].count /*vista*/),
           "interface not requested.\n");
    ok(ifaces[4].count || broken(!ifaces[4].count /*vista*/),
       "interface not requested.\n");

    ok(!punkimpl->unknown, "Got %d unknown.\n", punkimpl->unknown);
    HeapFree(GetProcessHeap(), 0, punkimpl);

    /* Test IShellItem */
    hres = pSHGetItemFromObject((IUnknown*)psfdesktop, &IID_IShellItem, (void**)&psi);
    ok(hres == S_OK, "Got 0x%08x\n", hres);
    if(SUCCEEDED(hres))
    {
        IShellItem *psi2;
        hres = pSHGetItemFromObject((IUnknown*)psi, &IID_IShellItem, (void**)&psi2);
        ok(hres == S_OK, "Got 0x%08x\n", hres);
        if(SUCCEEDED(hres))
        {
            todo_wine
                ok(psi == psi2, "Different instances (%p != %p).\n", psi, psi2);
            IShellItem_Release(psi2);
        }
        IShellItem_Release(psi);
    }

    IShellFolder_Release(psfdesktop);
}

3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367
static void test_SHCreateShellItemArray(void)
{
    IShellFolder *pdesktopsf, *psf;
    IShellItemArray *psia;
    IEnumIDList *peidl;
    HRESULT hr;
    WCHAR cTestDirW[MAX_PATH];
    LPITEMIDLIST pidl_testdir, pidl;
    static const WCHAR testdirW[] = {'t','e','s','t','d','i','r',0};

    if(!pSHCreateShellItemArray) {
        skip("No pSHCreateShellItemArray!\n");
        return;
    }

    ok(pSHGetSpecialFolderLocation != NULL, "SHGetSpecialFolderLocation missing.\n");

    if(0)
    {
        /* Crashes under native */
        pSHCreateShellItemArray(NULL, NULL, 0, NULL, NULL);
        pSHCreateShellItemArray(NULL, NULL, 1, NULL, NULL);
        pSHCreateShellItemArray(NULL, pdesktopsf, 0, NULL, NULL);
        pSHCreateShellItemArray(pidl, NULL, 0, NULL, NULL);
    }

    hr = pSHCreateShellItemArray(NULL, NULL, 0, NULL, &psia);
    ok(hr == E_POINTER, "got 0x%08x\n", hr);

    SHGetDesktopFolder(&pdesktopsf);
    hr = pSHCreateShellItemArray(NULL, pdesktopsf, 0, NULL, &psia);
    ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);

    hr = pSHCreateShellItemArray(NULL, pdesktopsf, 1, NULL, &psia);
    ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);

    pSHGetSpecialFolderLocation(NULL, CSIDL_DESKTOP, &pidl);
    hr = pSHCreateShellItemArray(pidl, NULL, 0, NULL, &psia);
    ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
    pILFree(pidl);

    GetCurrentDirectoryW(MAX_PATH, cTestDirW);
    myPathAddBackslashW(cTestDirW);
    lstrcatW(cTestDirW, testdirW);

    CreateFilesFolders();

    hr = IShellFolder_ParseDisplayName(pdesktopsf, NULL, NULL, cTestDirW, NULL, &pidl_testdir, 0);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    if(SUCCEEDED(hr))
    {
        hr = IShellFolder_BindToObject(pdesktopsf, pidl_testdir, NULL, (REFIID)&IID_IShellFolder,
                                       (void**)&psf);
        ok(hr == S_OK, "Got 0x%08x\n", hr);
    }
    IShellFolder_Release(pdesktopsf);

    if(FAILED(hr))
    {
        skip("Failed to set up environment for SHCreateShellItemArray tests.\n");
        pILFree(pidl_testdir);
        Cleanup();
        return;
    }

    hr = IShellFolder_EnumObjects(psf, NULL, SHCONTF_FOLDERS | SHCONTF_NONFOLDERS, &peidl);
    ok(hr == S_OK, "Got %08x\n", hr);
    if(SUCCEEDED(hr))
    {
        LPITEMIDLIST apidl[5];
        UINT done, numitems, i;

        for(done = 0; done < 5; done++)
            if(IEnumIDList_Next(peidl, 1, &apidl[done], NULL) != S_OK)
                break;
        ok(done == 5, "Got %d pidls\n", done);
        IEnumIDList_Release(peidl);

        /* Create a ShellItemArray */
        hr = pSHCreateShellItemArray(NULL, psf, done, (LPCITEMIDLIST*)apidl, &psia);
        ok(hr == S_OK, "Got 0x%08x\n", hr);
        if(SUCCEEDED(hr))
        {
            IShellItem *psi;

            if(0)
            {
                /* Crashes in Windows 7 */
3368
                IShellItemArray_GetCount(psia, NULL);
3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403
            }

            IShellItemArray_GetCount(psia, &numitems);
            ok(numitems == done, "Got %d, expected %d\n", numitems, done);

            hr = IShellItemArray_GetItemAt(psia, numitems, &psi);
            ok(hr == E_FAIL, "Got 0x%08x\n", hr);

            /* Compare all the items */
            for(i = 0; i < numitems; i++)
            {
                LPITEMIDLIST pidl_abs;
                pidl_abs = ILCombine(pidl_testdir, apidl[i]);

                hr = IShellItemArray_GetItemAt(psia, i, &psi);
                ok(hr == S_OK, "(%d) Failed with 0x%08x\n", i, hr);
                if(SUCCEEDED(hr))
                {
                    hr = pSHGetIDListFromObject((IUnknown*)psi, &pidl);
                    ok(hr == S_OK, "Got 0x%08x\n", hr);
                    if(SUCCEEDED(hr))
                    {
                        ok(ILIsEqual(pidl_abs, pidl), "Pidl not equal.\n");
                        pILFree(pidl);
                    }
                    IShellItem_Release(psi);
                }
                pILFree(pidl_abs);
            }
            for(i = 0; i < done; i++)
                pILFree(apidl[i]);
            IShellItemArray_Release(psia);
        }
    }

3404 3405 3406 3407 3408 3409 3410 3411
    /* SHCreateShellItemArrayFromShellItem */
    if(pSHCreateShellItemArrayFromShellItem)
    {
        IShellItem *psi;

        if(0)
        {
            /* Crashes under Windows 7 */
3412 3413 3414
            pSHCreateShellItemArrayFromShellItem(NULL, &IID_IShellItemArray, NULL);
            pSHCreateShellItemArrayFromShellItem(NULL, &IID_IShellItemArray, (void**)&psia);
            pSHCreateShellItemArrayFromShellItem(psi, &IID_IShellItemArray, NULL);
3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457
        }

        hr = pSHCreateItemFromIDList(pidl_testdir, &IID_IShellItem, (void**)&psi);
        ok(hr == S_OK, "Got 0x%08x\n", hr);
        if(SUCCEEDED(hr))
        {
            hr = pSHCreateShellItemArrayFromShellItem(psi, &IID_IShellItemArray, (void**)&psia);
            ok(hr == S_OK, "Got 0x%08x\n", hr);
            if(SUCCEEDED(hr))
            {
                IShellItem *psi2;
                UINT count;
                hr = IShellItemArray_GetCount(psia, &count);
                ok(hr == S_OK, "Got 0x%08x\n", hr);
                ok(count == 1, "Got count %d\n", count);
                hr = IShellItemArray_GetItemAt(psia, 0, &psi2);
                ok(hr == S_OK, "Got 0x%08x\n", hr);
                todo_wine
                    ok(psi != psi2, "ShellItems are of the same instance.\n");
                if(SUCCEEDED(hr))
                {
                    LPITEMIDLIST pidl1, pidl2;
                    hr = pSHGetIDListFromObject((IUnknown*)psi, &pidl1);
                    ok(hr == S_OK, "Got 0x%08x\n", hr);
                    ok(pidl1 != NULL, "pidl1 was null.\n");
                    hr = pSHGetIDListFromObject((IUnknown*)psi2, &pidl2);
                    ok(hr == S_OK, "Got 0x%08x\n", hr);
                    ok(pidl2 != NULL, "pidl2 was null.\n");
                    ok(ILIsEqual(pidl1, pidl2), "pidls not equal.\n");
                    pILFree(pidl1);
                    pILFree(pidl2);
                    IShellItem_Release(psi2);
                }
                hr = IShellItemArray_GetItemAt(psia, 1, &psi2);
                ok(hr == E_FAIL, "Got 0x%08x\n", hr);
                IShellItemArray_Release(psia);
            }
            IShellItem_Release(psi);
        }
    }
    else
        skip("No SHCreateShellItemArrayFromShellItem.\n");

3458 3459 3460 3461 3462 3463 3464
    if(pSHCreateShellItemArrayFromDataObject)
    {
        IShellView *psv;

        if(0)
        {
            /* Crashes under Windows 7 */
3465
            pSHCreateShellItemArrayFromDataObject(NULL, &IID_IShellItemArray, NULL);
3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504
        }
        hr = pSHCreateShellItemArrayFromDataObject(NULL, &IID_IShellItemArray, (void**)&psia);
        ok(hr == E_INVALIDARG, "Got 0x%08x\n", hr);

        hr = IShellFolder_CreateViewObject(psf, NULL, &IID_IShellView, (void**)&psv);
        ok(hr == S_OK, "got 0x%08x\n", hr);
        if(SUCCEEDED(hr))
        {
            IEnumIDList *peidl;
            IDataObject *pdo;
            SHCONTF enum_flags;

            enum_flags = SHCONTF_NONFOLDERS | SHCONTF_FOLDERS | SHCONTF_INCLUDEHIDDEN;
            hr = IShellFolder_EnumObjects(psf, NULL, enum_flags, &peidl);
            ok(hr == S_OK, "got 0x%08x\n", hr);
            if(SUCCEEDED(hr))
            {
                LPITEMIDLIST apidl[5];
                UINT count, i;

                for(count = 0; count < 5; count++)
                    if(IEnumIDList_Next(peidl, 1, &apidl[count], NULL) != S_OK)
                        break;
                ok(count == 5, "Got %d\n", count);

                if(count)
                {
                    hr = IShellFolder_GetUIObjectOf(psf, NULL, count, (LPCITEMIDLIST*)apidl,
                                                    &IID_IDataObject, NULL, (void**)&pdo);
                    ok(hr == S_OK, "Got 0x%08x\n", hr);
                    if(SUCCEEDED(hr))
                    {
                        hr = pSHCreateShellItemArrayFromDataObject(pdo, &IID_IShellItemArray,
                                                                   (void**)&psia);
                        ok(hr == S_OK, "Got 0x%08x\n", hr);
                        if(SUCCEEDED(hr))
                        {
                            UINT count_sia, i;
                            hr = IShellItemArray_GetCount(psia, &count_sia);
3505
                            ok(hr == S_OK, "Got 0x%08x\n", hr);
3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543
                            ok(count_sia == count, "Counts differ (%d, %d)\n", count, count_sia);
                            for(i = 0; i < count_sia; i++)
                            {
                                LPITEMIDLIST pidl_abs = ILCombine(pidl_testdir, apidl[i]);
                                IShellItem *psi;
                                hr = IShellItemArray_GetItemAt(psia, i, &psi);
                                ok(hr == S_OK, "Got 0x%08x\n", hr);
                                if(SUCCEEDED(hr))
                                {
                                    LPITEMIDLIST pidl;
                                    hr = pSHGetIDListFromObject((IUnknown*)psi, &pidl);
                                    ok(hr == S_OK, "Got 0x%08x\n", hr);
                                    ok(pidl != NULL, "pidl as NULL.\n");
                                    ok(ILIsEqual(pidl, pidl_abs), "pidls differ.\n");
                                    pILFree(pidl);
                                    IShellItem_Release(psi);
                                }
                                pILFree(pidl_abs);
                            }

                            IShellItemArray_Release(psia);
                        }

                        IDataObject_Release(pdo);
                    }
                    for(i = 0; i < count; i++)
                        pILFree(apidl[i]);
                }
                else
                    skip("No files found - skipping test.\n");

                IEnumIDList_Release(peidl);
            }
            IShellView_Release(psv);
        }
    }
    else
        skip("No SHCreateShellItemArrayFromDataObject.\n");
3544

3545 3546 3547 3548 3549
    IShellFolder_Release(psf);
    pILFree(pidl_testdir);
    Cleanup();
}

3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576
static void test_ShellItemBindToHandler(void)
{
    IShellItem *psi;
    LPITEMIDLIST pidl_desktop;
    HRESULT hr;

    if(!pSHCreateShellItem)
    {
        skip("SHCreateShellItem missing.\n");
        return;
    }

    hr = pSHGetSpecialFolderLocation(NULL, CSIDL_DESKTOP, &pidl_desktop);
    ok(hr == S_OK, "Got 0x%08x\n", hr);
    if(SUCCEEDED(hr))
    {
        hr = pSHCreateShellItem(NULL, NULL, pidl_desktop, &psi);
        ok(hr == S_OK, "Got 0x%08x\n", hr);
    }
    if(SUCCEEDED(hr))
    {
        IPersistFolder2 *ppf2;
        IUnknown *punk;

        if(0)
        {
            /* Crashes under Windows 7 */
3577 3578
            IShellItem_BindToHandler(psi, NULL, NULL, NULL, NULL);
            IShellItem_BindToHandler(psi, NULL, &IID_IUnknown, &IID_IUnknown, NULL);
3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601
        }
        hr = IShellItem_BindToHandler(psi, NULL, &IID_IUnknown, &IID_IUnknown, (void**)&punk);
        ok(hr == MK_E_NOOBJECT, "Got 0x%08x\n", hr);

        /* BHID_SFObject */
        hr = IShellItem_BindToHandler(psi, NULL, &BHID_SFObject, &IID_IShellFolder, (void**)&punk);
        ok(hr == S_OK, "Got 0x%08x\n", hr);
        if(SUCCEEDED(hr)) IUnknown_Release(punk);
        hr = IShellItem_BindToHandler(psi, NULL, &BHID_SFObject, &IID_IPersistFolder2, (void**)&ppf2);
        ok(hr == S_OK, "Got 0x%08x\n", hr);
        if(SUCCEEDED(hr))
        {
            LPITEMIDLIST pidl_tmp;
            hr = IPersistFolder2_GetCurFolder(ppf2, &pidl_tmp);
            ok(hr == S_OK, "Got 0x%08x\n", hr);
            if(SUCCEEDED(hr))
            {
                ok(ILIsEqual(pidl_desktop, pidl_tmp), "Pidl not equal (%p, %p)\n", pidl_desktop, pidl_tmp);
                pILFree(pidl_tmp);
            }
            IPersistFolder2_Release(ppf2);
        }

3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614
        /* BHID_SFUIObject */
        hr = IShellItem_BindToHandler(psi, NULL, &BHID_SFUIObject, &IID_IDataObject, (void**)&punk);
        ok(hr == S_OK || broken(hr == E_NOINTERFACE /* XP */), "Got 0x%08x\n", hr);
        if(SUCCEEDED(hr)) IUnknown_Release(punk);
        hr = IShellItem_BindToHandler(psi, NULL, &BHID_SFUIObject, &IID_IContextMenu, (void**)&punk);
        ok(hr == S_OK || broken(hr == E_NOINTERFACE /* XP */), "Got 0x%08x\n", hr);
        if(SUCCEEDED(hr)) IUnknown_Release(punk);

        /* BHID_DataObject */
        hr = IShellItem_BindToHandler(psi, NULL, &BHID_DataObject, &IID_IDataObject, (void**)&punk);
        ok(hr == S_OK || broken(hr == MK_E_NOOBJECT /* XP */), "Got 0x%08x\n", hr);
        if(SUCCEEDED(hr)) IUnknown_Release(punk);

3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700
        todo_wine
        {
            /* BHID_SFViewObject */
            hr = IShellItem_BindToHandler(psi, NULL, &BHID_SFViewObject, &IID_IShellView, (void**)&punk);
            ok(hr == S_OK, "Got 0x%08x\n", hr);
            if(SUCCEEDED(hr)) IUnknown_Release(punk);
            hr = IShellItem_BindToHandler(psi, NULL, &BHID_SFViewObject, &IID_IShellFolderView, (void**)&punk);
            ok(hr == E_NOINTERFACE, "Got 0x%08x\n", hr);
            if(SUCCEEDED(hr)) IUnknown_Release(punk);

            /* BHID_Storage */
            hr = IShellItem_BindToHandler(psi, NULL, &BHID_Storage, &IID_IStream, (void**)&punk);
            ok(hr == E_NOINTERFACE, "Got 0x%08x\n", hr);
            if(SUCCEEDED(hr)) IUnknown_Release(punk);
            hr = IShellItem_BindToHandler(psi, NULL, &BHID_Storage, &IID_IUnknown, (void**)&punk);
            ok(hr == S_OK, "Got 0x%08x\n", hr);
            if(SUCCEEDED(hr)) IUnknown_Release(punk);

            /* BHID_Stream */
            hr = IShellItem_BindToHandler(psi, NULL, &BHID_Stream, &IID_IStream, (void**)&punk);
            ok(hr == E_NOINTERFACE, "Got 0x%08x\n", hr);
            if(SUCCEEDED(hr)) IUnknown_Release(punk);
            hr = IShellItem_BindToHandler(psi, NULL, &BHID_Stream, &IID_IUnknown, (void**)&punk);
            ok(hr == S_OK, "Got 0x%08x\n", hr);
            if(SUCCEEDED(hr)) IUnknown_Release(punk);

            /* BHID_StorageEnum */
            hr = IShellItem_BindToHandler(psi, NULL, &BHID_StorageEnum, &IID_IEnumShellItems, (void**)&punk);
            ok(hr == S_OK, "Got 0x%08x\n", hr);
            if(SUCCEEDED(hr)) IUnknown_Release(punk);

            /* BHID_Transfer */
            hr = IShellItem_BindToHandler(psi, NULL, &BHID_Transfer, &IID_IUnknown, (void**)&punk);
            ok(hr == E_NOINTERFACE || broken(hr == MK_E_NOOBJECT /* XP */), "Got 0x%08x\n", hr);
            if(SUCCEEDED(hr)) IUnknown_Release(punk);

            /* BHID_EnumItems */
            hr = IShellItem_BindToHandler(psi, NULL, &BHID_EnumItems, &IID_IEnumShellItems, (void**)&punk);
            ok(hr == S_OK || broken(hr == MK_E_NOOBJECT /* XP */), "Got 0x%08x\n", hr);
            if(SUCCEEDED(hr)) IUnknown_Release(punk);

            /* BHID_Filter */
            hr = IShellItem_BindToHandler(psi, NULL, &BHID_Filter, &IID_IUnknown, (void**)&punk);
            ok(hr == S_OK || broken(hr == MK_E_NOOBJECT /* XP */), "Got 0x%08x\n", hr);
            if(SUCCEEDED(hr)) IUnknown_Release(punk);

            /* BHID_LinkTargetItem */
            hr = IShellItem_BindToHandler(psi, NULL, &BHID_LinkTargetItem, &IID_IShellItem, (void**)&punk);
            ok(hr == E_NOINTERFACE || broken(hr == E_INVALIDARG /* XP */), "Got 0x%08x\n", hr);
            if(SUCCEEDED(hr)) IUnknown_Release(punk);
            hr = IShellItem_BindToHandler(psi, NULL, &BHID_LinkTargetItem, &IID_IUnknown, (void**)&punk);
            ok(hr == E_NOINTERFACE || broken(hr == E_INVALIDARG /* XP */), "Got 0x%08x\n", hr);
            if(SUCCEEDED(hr)) IUnknown_Release(punk);

            /* BHID_PropertyStore */
            hr = IShellItem_BindToHandler(psi, NULL, &BHID_PropertyStore, &IID_IPropertyStore, (void**)&punk);
            ok(hr == E_NOINTERFACE || broken(hr == MK_E_NOOBJECT /* XP */), "Got 0x%08x\n", hr);
            if(SUCCEEDED(hr)) IUnknown_Release(punk);
            hr = IShellItem_BindToHandler(psi, NULL, &BHID_PropertyStore, &IID_IPropertyStoreFactory, (void**)&punk);
            ok(hr == E_NOINTERFACE || broken(hr == MK_E_NOOBJECT /* XP */), "Got 0x%08x\n", hr);
            if(SUCCEEDED(hr)) IUnknown_Release(punk);

            /* BHID_ThumbnailHandler */
            hr = IShellItem_BindToHandler(psi, NULL, &BHID_ThumbnailHandler, &IID_IUnknown, (void**)&punk);
            ok(hr == E_INVALIDARG || broken(hr == MK_E_NOOBJECT /* XP */), "Got 0x%08x\n", hr);
            if(SUCCEEDED(hr)) IUnknown_Release(punk);

            /* BHID_AssociationArray */
            hr = IShellItem_BindToHandler(psi, NULL, &BHID_AssociationArray, &IID_IQueryAssociations, (void**)&punk);
            ok(hr == S_OK || broken(hr == MK_E_NOOBJECT /* XP */), "Got 0x%08x\n", hr);
            if(SUCCEEDED(hr)) IUnknown_Release(punk);

            /* BHID_EnumAssocHandlers */
            hr = IShellItem_BindToHandler(psi, NULL, &BHID_EnumAssocHandlers, &IID_IUnknown, (void**)&punk);
            ok(hr == E_NOINTERFACE || broken(hr == MK_E_NOOBJECT /* XP */), "Got 0x%08x\n", hr);
            if(SUCCEEDED(hr)) IUnknown_Release(punk);
        }

        IShellItem_Release(psi);
    }
    else
        skip("Failed to create ShellItem.\n");

    pILFree(pidl_desktop);
}

3701
static void test_ShellItemGetAttributes(void)
3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723
{
    IShellItem *psi;
    LPITEMIDLIST pidl_desktop;
    SFGAOF sfgao;
    HRESULT hr;

    if(!pSHCreateShellItem)
    {
        skip("SHCreateShellItem missing.\n");
        return;
    }

    hr = pSHGetSpecialFolderLocation(NULL, CSIDL_DESKTOP, &pidl_desktop);
    ok(hr == S_OK, "Got 0x%08x\n", hr);
    if(SUCCEEDED(hr))
    {
        hr = pSHCreateShellItem(NULL, NULL, pidl_desktop, &psi);
        ok(hr == S_OK, "Got 0x%08x\n", hr);
        pILFree(pidl_desktop);
    }
    if(FAILED(hr))
    {
3724
        skip("Skipping tests.\n");
3725 3726 3727 3728 3729 3730
        return;
    }

    if(0)
    {
        /* Crashes on native (Win 7) */
3731
        IShellItem_GetAttributes(psi, 0, NULL);
3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742
    }

    /* Test GetAttributes on the desktop folder. */
    sfgao = 0xdeadbeef;
    hr = IShellItem_GetAttributes(psi, SFGAO_FOLDER, &sfgao);
    ok(hr == S_OK || broken(hr == E_FAIL) /* <Vista */, "Got 0x%08x\n", hr);
    ok(sfgao == SFGAO_FOLDER || broken(sfgao == 0) /* <Vista */, "Got 0x%08x\n", sfgao);

    IShellItem_Release(psi);
}

3743 3744 3745 3746 3747 3748 3749
static void test_SHParseDisplayName(void)
{
    LPITEMIDLIST pidl1, pidl2;
    IShellFolder *desktop;
    WCHAR dirW[MAX_PATH];
    WCHAR nameW[10];
    HRESULT hr;
3750
    BOOL ret, is_wow64;
3751 3752 3753 3754 3755 3756 3757 3758 3759 3760

    if (!pSHParseDisplayName)
    {
        win_skip("SHParseDisplayName isn't available\n");
        return;
    }

if (0)
{
    /* crashes on native */
3761
    pSHParseDisplayName(NULL, NULL, NULL, 0, NULL);
3762
    nameW[0] = 0;
3763
    pSHParseDisplayName(nameW, NULL, NULL, 0, NULL);
3764 3765 3766 3767
}

    pidl1 = (LPITEMIDLIST)0xdeadbeef;
    hr = pSHParseDisplayName(NULL, NULL, &pidl1, 0, NULL);
3768 3769
    ok(broken(hr == E_OUTOFMEMORY) /* < Vista */ ||
       hr == E_INVALIDARG, "failed %08x\n", hr);
3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785
    ok(pidl1 == 0, "expected null ptr, got %p\n", pidl1);

    /* dummy name */
    nameW[0] = 0;
    hr = pSHParseDisplayName(nameW, NULL, &pidl1, 0, NULL);
    ok(hr == S_OK, "failed %08x\n", hr);
    hr = SHGetDesktopFolder(&desktop);
    ok(hr == S_OK, "failed %08x\n", hr);
    hr = IShellFolder_ParseDisplayName(desktop, NULL, NULL, nameW, NULL, &pidl2, NULL);
    ok(hr == S_OK, "failed %08x\n", hr);
    ret = pILIsEqual(pidl1, pidl2);
    ok(ret == TRUE, "expected equal idls\n");
    pILFree(pidl1);
    pILFree(pidl2);

    /* with path */
3786
    GetWindowsDirectoryW( dirW, MAX_PATH );
3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797

    hr = pSHParseDisplayName(dirW, NULL, &pidl1, 0, NULL);
    ok(hr == S_OK, "failed %08x\n", hr);
    hr = IShellFolder_ParseDisplayName(desktop, NULL, NULL, dirW, NULL, &pidl2, NULL);
    ok(hr == S_OK, "failed %08x\n", hr);

    ret = pILIsEqual(pidl1, pidl2);
    ok(ret == TRUE, "expected equal idls\n");
    pILFree(pidl1);
    pILFree(pidl2);

3798 3799 3800 3801
    /* system32 is not redirected to syswow64 on WOW64 */
    if (!pIsWow64Process || !pIsWow64Process( GetCurrentProcess(), &is_wow64 )) is_wow64 = FALSE;
    if (is_wow64 && pGetSystemWow64DirectoryW)
    {
3802
        UINT len;
3803
        *dirW = 0;
3804 3805
        len = GetSystemDirectoryW(dirW, MAX_PATH);
        ok(len > 0, "GetSystemDirectoryW failed: %u\n", GetLastError());
3806 3807 3808
        hr = pSHParseDisplayName(dirW, NULL, &pidl1, 0, NULL);
        ok(hr == S_OK, "failed %08x\n", hr);
        *dirW = 0;
3809 3810
        len = pGetSystemWow64DirectoryW(dirW, MAX_PATH);
        ok(len > 0, "GetSystemWow64DirectoryW failed: %u\n", GetLastError());
3811 3812 3813 3814 3815 3816 3817 3818
        hr = pSHParseDisplayName(dirW, NULL, &pidl2, 0, NULL);
        ok(hr == S_OK, "failed %08x\n", hr);
        ret = pILIsEqual(pidl1, pidl2);
        ok(ret == FALSE, "expected different idls\n");
        pILFree(pidl1);
        pILFree(pidl2);
    }

3819 3820 3821
    IShellFolder_Release(desktop);
}

3822 3823 3824 3825
static void test_desktop_IPersist(void)
{
    IShellFolder *desktop;
    IPersist *persist;
3826
    IPersistFolder2 *ppf2;
3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840
    CLSID clsid;
    HRESULT hr;

    hr = SHGetDesktopFolder(&desktop);
    ok(hr == S_OK, "failed %08x\n", hr);

    hr = IShellFolder_QueryInterface(desktop, &IID_IPersist, (void**)&persist);
    ok(hr == S_OK || broken(hr == E_NOINTERFACE) /* NT4, W9X */, "failed %08x\n", hr);

    if (hr == S_OK)
    {
    if (0)
    {
        /* crashes on native */
3841
        IPersist_GetClassID(persist, NULL);
3842 3843 3844 3845 3846 3847 3848 3849
    }
        memset(&clsid, 0, sizeof(clsid));
        hr = IPersist_GetClassID(persist, &clsid);
        ok(hr == S_OK, "failed %08x\n", hr);
        ok(IsEqualIID(&CLSID_ShellDesktop, &clsid), "Expected CLSID_ShellDesktop\n");
        IPersist_Release(persist);
    }

3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874
    hr = IShellFolder_QueryInterface(desktop, &IID_IPersistFolder2, (void**)&ppf2);
    ok(hr == S_OK || broken(hr == E_NOINTERFACE) /* pre-Vista */, "failed %08x\n", hr);
    if(SUCCEEDED(hr))
    {
        IPersistFolder *ppf;
        LPITEMIDLIST pidl;
        hr = IShellFolder_QueryInterface(desktop, &IID_IPersistFolder, (void**)&ppf);
        ok(hr == S_OK, "IID_IPersistFolder2 without IID_IPersistFolder.\n");
        if(SUCCEEDED(hr))
            IPersistFolder_Release(ppf);

        todo_wine {
            hr = IPersistFolder2_Initialize(ppf2, NULL);
            ok(hr == S_OK, "got %08x\n", hr);
        }

        pidl = NULL;
        hr = IPersistFolder2_GetCurFolder(ppf2, &pidl);
        ok(hr == S_OK, "got %08x\n", hr);
        ok(pidl != NULL, "pidl was NULL.\n");
        if(SUCCEEDED(hr)) pILFree(pidl);

        IPersistFolder2_Release(ppf2);
    }

3875 3876 3877
    IShellFolder_Release(desktop);
}

3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905
static void test_GetUIObject(void)
{
    IShellFolder *psf_desktop;
    IContextMenu *pcm;
    LPITEMIDLIST pidl;
    HRESULT hr;
    WCHAR path[MAX_PATH];
    const WCHAR filename[] =
        {'\\','t','e','s','t','d','i','r','\\','t','e','s','t','1','.','t','x','t',0};

    if(!pSHBindToParent)
    {
        win_skip("SHBindToParent missing.\n");
        return;
    }

    GetCurrentDirectoryW(MAX_PATH, path);
    if(!lstrlenW(path))
    {
        skip("GetCurrentDirectoryW returned an empty string.\n");
        return;
    }
    lstrcatW(path, filename);
    SHGetDesktopFolder(&psf_desktop);

    CreateFilesFolders();

    hr = IShellFolder_ParseDisplayName(psf_desktop, NULL, NULL, path, NULL, &pidl, 0);
3906
    ok(hr == S_OK || broken(hr == E_FAIL) /* WinME */, "Got 0x%08x\n", hr);
3907 3908 3909 3910 3911 3912 3913 3914
    if(SUCCEEDED(hr))
    {
        IShellFolder *psf;
        LPCITEMIDLIST pidl_child;
        hr = pSHBindToParent(pidl, &IID_IShellFolder, (void**)&psf, &pidl_child);
        ok(hr == S_OK, "Got 0x%08x\n", hr);
        if(SUCCEEDED(hr))
        {
3915 3916
            hr = IShellFolder_GetUIObjectOf(psf, NULL, 1, &pidl_child, &IID_IContextMenu, NULL,
                                            (void**)&pcm);
3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944
            ok(hr == S_OK, "Got 0x%08x\n", hr);
            if(SUCCEEDED(hr))
            {
                HMENU hmenu = CreatePopupMenu();
                INT max_id, max_id_check;
                UINT count, i;
                const int id_upper_limit = 32767;
                hr = IContextMenu_QueryContextMenu(pcm, hmenu, 0, 0, id_upper_limit, CMF_NORMAL);
                ok(SUCCEEDED(hr), "Got 0x%08x\n", hr);
                max_id = HRESULT_CODE(hr) - 1; /* returns max_id + 1 */
                ok(max_id <= id_upper_limit, "Got %d\n", max_id);
                count = GetMenuItemCount(hmenu);
                ok(count, "Got %d\n", count);

                max_id_check = 0;
                for(i = 0; i < count; i++)
                {
                    MENUITEMINFOA mii;
                    INT res;
                    ZeroMemory(&mii, sizeof(MENUITEMINFOA));
                    mii.cbSize = sizeof(MENUITEMINFOA);
                    mii.fMask = MIIM_ID | MIIM_FTYPE;

                    SetLastError(0);
                    res = GetMenuItemInfoA(hmenu, i, TRUE, &mii);
                    ok(res, "Failed (last error: %d).\n", GetLastError());

                    ok( (mii.wID <= id_upper_limit) || (mii.fType & MFT_SEPARATOR),
3945
                        "Got non-separator ID out of range: %d (type: %x)\n", mii.wID, mii.fType);
3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960
                    if(!(mii.fType & MFT_SEPARATOR))
                        max_id_check = (mii.wID>max_id_check)?mii.wID:max_id_check;
                }
                ok((max_id_check == max_id) ||
                   (max_id_check == max_id-1 /* Win 7 */),
                   "Not equal (or near equal), got %d and %d\n", max_id_check, max_id);

#define is_win2k() (pSHGetFolderPathA && !pSHGetFolderPathAndSubDirA)

                if(count && !is_win2k())   /* Test is interactive on w2k, so skip */
                {
                    CMINVOKECOMMANDINFO cmi;
                    ZeroMemory(&cmi, sizeof(CMINVOKECOMMANDINFO));
                    cmi.cbSize = sizeof(CMINVOKECOMMANDINFO);

3961
                    /* Attempt to execute a nonexistent command */
3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985
                    cmi.lpVerb = MAKEINTRESOURCEA(9999);
                    hr = IContextMenu_InvokeCommand(pcm, &cmi);
                    ok(hr == E_INVALIDARG, "Got 0x%08x\n", hr);

                    cmi.lpVerb = "foobar_wine_test";
                    hr = IContextMenu_InvokeCommand(pcm, &cmi);
                    ok( (hr == E_INVALIDARG) || (hr == E_FAIL /* Win7 */) ||
                        (hr == HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION) /* Vista */),
                        "Got 0x%08x\n", hr);
                }
#undef is_win2k

                DestroyMenu(hmenu);
                IContextMenu_Release(pcm);
            }
            IShellFolder_Release(psf);
        }
        if(pILFree) pILFree(pidl);
    }

    IShellFolder_Release(psf_desktop);
    Cleanup();
}

3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020
#define verify_pidl(i,p) r_verify_pidl(__LINE__, i, p)
static void r_verify_pidl(unsigned l, LPCITEMIDLIST pidl, const WCHAR *path)
{
    LPCITEMIDLIST child;
    IShellFolder *parent;
    STRRET filename;
    HRESULT hr;

    if(!pSHBindToParent){
        win_skip("SHBindToParent is not available, not performing full PIDL verification\n");
        if(path)
            ok_(__FILE__,l)(pidl != NULL, "Expected PIDL to be non-NULL\n");
        else
            ok_(__FILE__,l)(pidl == NULL, "Expected PIDL to be NULL\n");
        return;
    }

    if(path){
        if(!pidl){
            ok_(__FILE__,l)(0, "didn't get expected path (%s), instead: NULL\n", wine_dbgstr_w(path));
            return;
        }

        hr = pSHBindToParent(pidl, &IID_IShellFolder, (LPVOID*)&parent, &child);
        ok_(__FILE__,l)(hr == S_OK, "SHBindToParent failed: 0x%08x\n", hr);
        if(FAILED(hr))
            return;

        hr = IShellFolder_GetDisplayNameOf(parent, child, SHGDN_FORPARSING, &filename);
        ok_(__FILE__,l)(hr == S_OK, "GetDisplayNameOf failed: 0x%08x\n", hr);
        if(FAILED(hr)){
            IShellFolder_Release(parent);
            return;
        }

4021 4022 4023
        ok_(__FILE__,l)(filename.uType == STRRET_WSTR || filename.uType == STRRET_CSTR,
                "Got unexpected string type: %d\n", filename.uType);
        if(filename.uType == STRRET_WSTR){
4024
            ok_(__FILE__,l)(lstrcmpW(path, U(filename).pOleStr) == 0,
4025
                    "didn't get expected path (%s), instead: %s\n",
4026
                     wine_dbgstr_w(path), wine_dbgstr_w(U(filename).pOleStr));
4027
            SHFree(U(filename).pOleStr);
4028
        }else if(filename.uType == STRRET_CSTR){
4029
            ok_(__FILE__,l)(strcmp_wa(path, U(filename).cStr) == 0,
4030
                    "didn't get expected path (%s), instead: %s\n",
4031
                     wine_dbgstr_w(path), U(filename).cStr);
4032
        }
4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111

        IShellFolder_Release(parent);
    }else
        ok_(__FILE__,l)(pidl == NULL, "Expected PIDL to be NULL\n");
}

static void test_SHSimpleIDListFromPath(void)
{
    const WCHAR adirW[] = {'C',':','\\','s','i','d','l','f','p','d','i','r',0};
    const CHAR adirA[] = "C:\\sidlfpdir";
    BOOL br, is_unicode = !(GetVersion() & 0x80000000);

    LPITEMIDLIST pidl = NULL;

    if(!pSHSimpleIDListFromPathAW){
        win_skip("SHSimpleIDListFromPathAW not available\n");
        return;
    }

    br = CreateDirectoryA(adirA, NULL);
    ok(br == TRUE, "CreateDirectory failed: %d\n", GetLastError());

    if(is_unicode)
        pidl = pSHSimpleIDListFromPathAW(adirW);
    else
        pidl = pSHSimpleIDListFromPathAW(adirA);
    verify_pidl(pidl, adirW);
    pILFree(pidl);

    br = RemoveDirectoryA(adirA);
    ok(br == TRUE, "RemoveDirectory failed: %d\n", GetLastError());

    if(is_unicode)
        pidl = pSHSimpleIDListFromPathAW(adirW);
    else
        pidl = pSHSimpleIDListFromPathAW(adirA);
    verify_pidl(pidl, adirW);
    pILFree(pidl);
}

/* IFileSystemBindData impl */
static HRESULT WINAPI fsbd_QueryInterface(IFileSystemBindData *fsbd,
        REFIID riid, void **ppv)
{
    if(IsEqualIID(riid, &IID_IFileSystemBindData) ||
            IsEqualIID(riid, &IID_IUnknown)){
        *ppv = fsbd;
        return S_OK;
    }
    return E_NOINTERFACE;
}

static ULONG WINAPI fsbd_AddRef(IFileSystemBindData *fsbd)
{
    return 2;
}

static ULONG WINAPI fsbd_Release(IFileSystemBindData *fsbd)
{
    return 1;
}

static HRESULT WINAPI fsbd_SetFindData(IFileSystemBindData *fsbd,
        const WIN32_FIND_DATAW *pfd)
{
    ok(0, "SetFindData called\n");
    return E_NOTIMPL;
}

static HRESULT WINAPI fsbd_GetFindData_nul(IFileSystemBindData *fsbd,
        WIN32_FIND_DATAW *pfd)
{
    memset(pfd, 0, sizeof(WIN32_FIND_DATAW));
    return S_OK;
}

static HRESULT WINAPI fsbd_GetFindData_junk(IFileSystemBindData *fsbd,
        WIN32_FIND_DATAW *pfd)
{
4112
    memset(pfd, 0xef, sizeof(WIN32_FIND_DATAW));
4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154
    return S_OK;
}

static HRESULT WINAPI fsbd_GetFindData_invalid(IFileSystemBindData *fsbd,
        WIN32_FIND_DATAW *pfd)
{
    memset(pfd, 0, sizeof(WIN32_FIND_DATAW));
    *pfd->cFileName = 'a';
    *pfd->cAlternateFileName = 'a';
    return S_OK;
}

static HRESULT WINAPI fsbd_GetFindData_valid(IFileSystemBindData *fsbd,
        WIN32_FIND_DATAW *pfd)
{
    static const WCHAR adirW[] = {'C',':','\\','f','s','b','d','d','i','r',0};
    HANDLE handle = FindFirstFileW(adirW, pfd);
    FindClose(handle);
    return S_OK;
}

static HRESULT WINAPI fsbd_GetFindData_fail(IFileSystemBindData *fsbd,
        WIN32_FIND_DATAW *pfd)
{
    return E_FAIL;
}

static IFileSystemBindDataVtbl fsbdVtbl = {
    fsbd_QueryInterface,
    fsbd_AddRef,
    fsbd_Release,
    fsbd_SetFindData,
    NULL
};

static IFileSystemBindData fsbd = { &fsbdVtbl };

static void test_ParseDisplayNamePBC(void)
{
    WCHAR wFileSystemBindData[] =
        {'F','i','l','e',' ','S','y','s','t','e','m',' ','B','i','n','d',' ','D','a','t','a',0};
    WCHAR adirW[] = {'C',':','\\','f','s','b','d','d','i','r',0};
4155
    WCHAR afileW[] = {'C',':','\\','f','s','b','d','d','i','r','\\','f','i','l','e','.','t','x','t',0};
4156
    WCHAR afile2W[] = {'C',':','\\','f','s','b','d','d','i','r','\\','s','\\','f','i','l','e','.','t','x','t',0};
4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180
    const HRESULT exp_err = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);

    IShellFolder *psf;
    IBindCtx *pbc;
    HRESULT hres;
    ITEMIDLIST *pidl;

    /* Check if we support WCHAR functions */
    SetLastError(0xdeadbeef);
    lstrcmpiW(adirW, adirW);
    if(GetLastError() == ERROR_CALL_NOT_IMPLEMENTED){
        win_skip("Most W-calls are not implemented\n");
        return;
    }

    hres = SHGetDesktopFolder(&psf);
    ok(hres == S_OK, "SHGetDesktopFolder failed: 0x%08x\n", hres);
    if(FAILED(hres)){
        win_skip("Failed to get IShellFolder, can't run tests\n");
        return;
    }

    /* fails on unknown dir with no IBindCtx */
    hres = IShellFolder_ParseDisplayName(psf, NULL, NULL, adirW, NULL, &pidl, NULL);
4181 4182 4183
    ok(hres == exp_err || broken(hres == E_FAIL) /* NT4 */,
            "ParseDisplayName failed with wrong error: 0x%08x\n", hres);
    hres = IShellFolder_ParseDisplayName(psf, NULL, NULL, afileW, NULL, &pidl, NULL);
4184 4185 4186
    ok(hres == exp_err || broken(hres == E_FAIL) /* NT4 */,
            "ParseDisplayName failed with wrong error: 0x%08x\n", hres);
    hres = IShellFolder_ParseDisplayName(psf, NULL, NULL, afile2W, NULL, &pidl, NULL);
4187 4188 4189 4190 4191 4192 4193 4194
    ok(hres == exp_err || broken(hres == E_FAIL) /* NT4 */,
            "ParseDisplayName failed with wrong error: 0x%08x\n", hres);

    /* fails on unknown dir with IBindCtx with no IFileSystemBindData */
    hres = CreateBindCtx(0, &pbc);
    ok(hres == S_OK, "CreateBindCtx failed: 0x%08x\n", hres);

    hres = IShellFolder_ParseDisplayName(psf, NULL, pbc, adirW, NULL, &pidl, NULL);
4195 4196 4197
    ok(hres == exp_err || broken(hres == E_FAIL) /* NT4 */,
            "ParseDisplayName failed with wrong error: 0x%08x\n", hres);
    hres = IShellFolder_ParseDisplayName(psf, NULL, pbc, afileW, NULL, &pidl, NULL);
4198 4199 4200
    ok(hres == exp_err || broken(hres == E_FAIL) /* NT4 */,
            "ParseDisplayName failed with wrong error: 0x%08x\n", hres);
    hres = IShellFolder_ParseDisplayName(psf, NULL, pbc, afile2W, NULL, &pidl, NULL);
4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218
    ok(hres == exp_err || broken(hres == E_FAIL) /* NT4 */,
            "ParseDisplayName failed with wrong error: 0x%08x\n", hres);

    /* unknown dir with IBindCtx with IFileSystemBindData */
    hres = IBindCtx_RegisterObjectParam(pbc, wFileSystemBindData, (IUnknown*)&fsbd);
    ok(hres == S_OK, "RegisterObjectParam failed: 0x%08x\n", hres);

    /* return E_FAIL from GetFindData */
    pidl = (ITEMIDLIST*)0xdeadbeef;
    fsbdVtbl.GetFindData = fsbd_GetFindData_fail;
    hres = IShellFolder_ParseDisplayName(psf, NULL, pbc, adirW, NULL, &pidl, NULL);
    ok(hres == S_OK || broken(hres == E_FAIL) /* NT4 */,
            "ParseDisplayName failed: 0x%08x\n", hres);
    if(SUCCEEDED(hres)){
        verify_pidl(pidl, adirW);
        ILFree(pidl);
    }

4219 4220 4221 4222 4223 4224 4225 4226
    hres = IShellFolder_ParseDisplayName(psf, NULL, pbc, afileW, NULL, &pidl, NULL);
    ok(hres == S_OK || broken(hres == E_FAIL) /* NT4 */,
            "ParseDisplayName failed: 0x%08x\n", hres);
    if(SUCCEEDED(hres)){
        verify_pidl(pidl, afileW);
        ILFree(pidl);
    }

4227 4228 4229 4230 4231 4232 4233 4234
    hres = IShellFolder_ParseDisplayName(psf, NULL, pbc, afile2W, NULL, &pidl, NULL);
    ok(hres == S_OK || broken(hres == E_FAIL) /* NT4 */,
            "ParseDisplayName failed: 0x%08x\n", hres);
    if(SUCCEEDED(hres)){
        verify_pidl(pidl, afile2W);
        ILFree(pidl);
    }

4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245
    /* set FIND_DATA struct to NULLs */
    pidl = (ITEMIDLIST*)0xdeadbeef;
    fsbdVtbl.GetFindData = fsbd_GetFindData_nul;
    hres = IShellFolder_ParseDisplayName(psf, NULL, pbc, adirW, NULL, &pidl, NULL);
    ok(hres == S_OK || broken(hres == E_FAIL) /* NT4 */,
            "ParseDisplayName failed: 0x%08x\n", hres);
    if(SUCCEEDED(hres)){
        verify_pidl(pidl, adirW);
        ILFree(pidl);
    }

4246 4247 4248 4249 4250 4251 4252 4253
    hres = IShellFolder_ParseDisplayName(psf, NULL, pbc, afileW, NULL, &pidl, NULL);
    ok(hres == S_OK || broken(hres == E_FAIL) /* NT4 */,
            "ParseDisplayName failed: 0x%08x\n", hres);
    if(SUCCEEDED(hres)){
        verify_pidl(pidl, afileW);
        ILFree(pidl);
    }

4254 4255 4256 4257 4258 4259 4260 4261
    hres = IShellFolder_ParseDisplayName(psf, NULL, pbc, afile2W, NULL, &pidl, NULL);
    ok(hres == S_OK || broken(hres == E_FAIL) /* NT4 */,
            "ParseDisplayName failed: 0x%08x\n", hres);
    if(SUCCEEDED(hres)){
        verify_pidl(pidl, afile2W);
        ILFree(pidl);
    }

4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272
    /* set FIND_DATA struct to junk */
    pidl = (ITEMIDLIST*)0xdeadbeef;
    fsbdVtbl.GetFindData = fsbd_GetFindData_junk;
    hres = IShellFolder_ParseDisplayName(psf, NULL, pbc, adirW, NULL, &pidl, NULL);
    ok(hres == S_OK || broken(hres == E_FAIL) /* NT4 */,
            "ParseDisplayName failed: 0x%08x\n", hres);
    if(SUCCEEDED(hres)){
        verify_pidl(pidl, adirW);
        ILFree(pidl);
    }

4273 4274 4275 4276 4277 4278 4279 4280
    hres = IShellFolder_ParseDisplayName(psf, NULL, pbc, afileW, NULL, &pidl, NULL);
    ok(hres == S_OK || broken(hres == E_FAIL) /* NT4 */,
            "ParseDisplayName failed: 0x%08x\n", hres);
    if(SUCCEEDED(hres)){
        verify_pidl(pidl, afileW);
        ILFree(pidl);
    }

4281 4282 4283 4284 4285 4286 4287 4288
    hres = IShellFolder_ParseDisplayName(psf, NULL, pbc, afile2W, NULL, &pidl, NULL);
    ok(hres == S_OK || broken(hres == E_FAIL) /* NT4 */,
            "ParseDisplayName failed: 0x%08x\n", hres);
    if(SUCCEEDED(hres)){
        verify_pidl(pidl, afile2W);
        ILFree(pidl);
    }

4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299
    /* set FIND_DATA struct to invalid data */
    pidl = (ITEMIDLIST*)0xdeadbeef;
    fsbdVtbl.GetFindData = fsbd_GetFindData_invalid;
    hres = IShellFolder_ParseDisplayName(psf, NULL, pbc, adirW, NULL, &pidl, NULL);
    ok(hres == S_OK || broken(hres == E_FAIL) /* NT4 */,
            "ParseDisplayName failed: 0x%08x\n", hres);
    if(SUCCEEDED(hres)){
        verify_pidl(pidl, adirW);
        ILFree(pidl);
    }

4300 4301 4302 4303 4304 4305 4306 4307
    hres = IShellFolder_ParseDisplayName(psf, NULL, pbc, afileW, NULL, &pidl, NULL);
    ok(hres == S_OK || broken(hres == E_FAIL) /* NT4 */,
            "ParseDisplayName failed: 0x%08x\n", hres);
    if(SUCCEEDED(hres)){
        verify_pidl(pidl, afileW);
        ILFree(pidl);
    }

4308 4309 4310 4311 4312 4313 4314 4315
    hres = IShellFolder_ParseDisplayName(psf, NULL, pbc, afile2W, NULL, &pidl, NULL);
    ok(hres == S_OK || broken(hres == E_FAIL) /* NT4 */,
            "ParseDisplayName failed: 0x%08x\n", hres);
    if(SUCCEEDED(hres)){
        verify_pidl(pidl, afile2W);
        ILFree(pidl);
    }

4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326
    /* set FIND_DATA struct to valid data */
    pidl = (ITEMIDLIST*)0xdeadbeef;
    fsbdVtbl.GetFindData = fsbd_GetFindData_valid;
    hres = IShellFolder_ParseDisplayName(psf, NULL, pbc, adirW, NULL, &pidl, NULL);
    ok(hres == S_OK || broken(hres == E_FAIL) /* NT4 */,
            "ParseDisplayName failed: 0x%08x\n", hres);
    if(SUCCEEDED(hres)){
        verify_pidl(pidl, adirW);
        ILFree(pidl);
    }

4327 4328 4329 4330 4331 4332 4333 4334
    hres = IShellFolder_ParseDisplayName(psf, NULL, pbc, afileW, NULL, &pidl, NULL);
    ok(hres == S_OK || broken(hres == E_FAIL) /* NT4 */,
            "ParseDisplayName failed: 0x%08x\n", hres);
    if(SUCCEEDED(hres)){
        verify_pidl(pidl, afileW);
        ILFree(pidl);
    }

4335 4336 4337 4338 4339 4340 4341 4342
    hres = IShellFolder_ParseDisplayName(psf, NULL, pbc, afile2W, NULL, &pidl, NULL);
    ok(hres == S_OK || broken(hres == E_FAIL) /* NT4 */,
            "ParseDisplayName failed: 0x%08x\n", hres);
    if(SUCCEEDED(hres)){
        verify_pidl(pidl, afile2W);
        ILFree(pidl);
    }

4343 4344 4345 4346
    IBindCtx_Release(pbc);
    IShellFolder_Release(psf);
}

4347 4348 4349
static const CHAR testwindow_class[] = "testwindow";
#define WM_USER_NOTIFY (WM_APP+1)

4350 4351 4352 4353
struct ChNotifyTest {
    const char id[256];
    const UINT notify_count;
    UINT missing_events;
4354
    UINT signal;
4355 4356 4357 4358
    const char path_1[256];
    const char path_2[256];
} chnotify_tests[] = {
    {"MKDIR", 1, 0, SHCNE_MKDIR, "C:\\shell32_cn_test\\test", ""},
4359
    {"CREATE", 1, 0, SHCNE_CREATE, "C:\\shell32_cn_test\\test\\file.txt", ""},
4360 4361 4362 4363
    {"RMDIR", 1, 0, SHCNE_RMDIR, "C:\\shell32_cn_test\\test", ""},
};

struct ChNotifyTest *exp_data;
4364
BOOL test_new_delivery_flag;
4365 4366 4367

static LRESULT CALLBACK testwindow_wndproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
4368
    LONG signal = (LONG)lparam;
4369 4370 4371

    switch(msg){
    case WM_USER_NOTIFY:
4372
        if(exp_data->missing_events > 0) {
4373
            WCHAR *path1, *path2;
4374 4375 4376 4377 4378 4379 4380
            LPITEMIDLIST *pidls = (LPITEMIDLIST*)wparam;
            HANDLE hLock = NULL;

            if(test_new_delivery_flag) {
                hLock = SHChangeNotification_Lock((HANDLE)wparam, lparam, &pidls, &signal);
                ok(hLock != NULL, "SHChangeNotification_Lock returned NULL\n");
            }
4381

4382
            ok(exp_data->signal == signal,
4383
                    "%s: expected notification type %x, got: %x\n",
4384
                    exp_data->id, exp_data->signal, signal);
4385

4386 4387 4388 4389 4390 4391 4392
            trace("verifying pidls for: %s\n", exp_data->id);
            path1 = make_wstr(exp_data->path_1);
            path2 = make_wstr(exp_data->path_2);
            verify_pidl(pidls[0], path1);
            verify_pidl(pidls[1], path2);
            HeapFree(GetProcessHeap(), 0, path1);
            HeapFree(GetProcessHeap(), 0, path2);
4393

4394
            exp_data->missing_events--;
4395 4396 4397

            if(test_new_delivery_flag)
                SHChangeNotification_Unlock(hLock);
4398
        }else
4399
            ok(0, "Didn't expect a WM_USER_NOTIFY message (event: %x)\n", signal);
4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426
        return 0;
    }
    return DefWindowProc(hwnd, msg, wparam, lparam);
}

static void register_testwindow_class(void)
{
    WNDCLASSEXA cls;
    ATOM ret;

    ZeroMemory(&cls, sizeof(cls));
    cls.cbSize = sizeof(cls);
    cls.style = 0;
    cls.lpfnWndProc = testwindow_wndproc;
    cls.hInstance = GetModuleHandleA(NULL);
    cls.lpszClassName = testwindow_class;

    SetLastError(0);
    ret = RegisterClassExA(&cls);
    ok(ret != 0, "RegisterClassExA failed: %d\n", GetLastError());
}

/* SHCNF_FLUSH doesn't seem to work as advertised for SHCNF_PATHA, so we
 * have to poll repeatedly for the message to appear */
static void do_events(void)
{
    int c = 0;
4427
    while (exp_data->missing_events && (c++ < 10)){
4428 4429 4430 4431 4432
        MSG msg;
        while(PeekMessageA(&msg, NULL, 0, 0, PM_REMOVE)){
            TranslateMessage(&msg);
            DispatchMessageA(&msg);
        }
4433
        if(exp_data->missing_events)
4434 4435
            Sleep(500);
    }
4436
    trace("%s: took %d tries\n", exp_data->id, c);
4437 4438
}

4439
static void test_SHChangeNotify(BOOL test_new_delivery)
4440 4441
{
    HWND wnd;
4442
    ULONG notifyID, i;
4443 4444 4445 4446 4447 4448
    HRESULT hr;
    BOOL br, has_unicode;
    SHChangeNotifyEntry entries[1];
    const CHAR root_dirA[] = "C:\\shell32_cn_test";
    const WCHAR root_dirW[] = {'C',':','\\','s','h','e','l','l','3','2','_','c','n','_','t','e','s','t',0};

4449 4450
    trace("SHChangeNotify tests (%x)\n", test_new_delivery);

4451 4452 4453
    CreateDirectoryW(NULL, NULL);
    has_unicode = !(GetLastError() == ERROR_CALL_NOT_IMPLEMENTED);

4454 4455 4456
    test_new_delivery_flag = test_new_delivery;
    if(!test_new_delivery)
        register_testwindow_class();
4457 4458 4459 4460 4461 4462

    wnd = CreateWindowExA(0, testwindow_class, testwindow_class, 0,
            CW_USEDEFAULT, CW_USEDEFAULT, 130, 105,
            NULL, NULL, GetModuleHandleA(NULL), 0);
    ok(wnd != NULL, "Failed to make a window\n");

4463 4464 4465
    br = CreateDirectoryA(root_dirA, NULL);
    ok(br == TRUE, "CreateDirectory failed: %d\n", GetLastError());

4466 4467 4468 4469 4470 4471 4472 4473
    entries[0].pidl = NULL;
    if(has_unicode)
        hr = SHILCreateFromPath(root_dirW, (LPITEMIDLIST*)&entries[0].pidl, 0);
    else
        hr = SHILCreateFromPath((LPCVOID)root_dirA, (LPITEMIDLIST*)&entries[0].pidl, 0);
    ok(hr == S_OK, "SHILCreateFromPath failed: 0x%08x\n", hr);
    entries[0].fRecursive = TRUE;

4474
    notifyID = SHChangeNotifyRegister(wnd, !test_new_delivery ? SHCNRF_ShellLevel : SHCNRF_ShellLevel|SHCNRF_NewDelivery,
4475 4476 4477
            SHCNE_ALLEVENTS, WM_USER_NOTIFY, 1, entries);
    ok(notifyID != 0, "Failed to register a window for change notifications\n");

4478 4479
    for(i = 0; i < sizeof(chnotify_tests) / sizeof(*chnotify_tests); ++i){
        exp_data = chnotify_tests + i;
4480

4481 4482 4483 4484
        exp_data->missing_events = exp_data->notify_count;
        SHChangeNotify(exp_data->signal, SHCNF_PATHA | SHCNF_FLUSH,
                strlen(exp_data->path_1) > 0 ? exp_data->path_1 : NULL,
                strlen(exp_data->path_2) > 0 ? exp_data->path_2 : NULL);
4485
        do_events();
4486
        ok(exp_data->missing_events == 0, "%s: Expected wndproc to be called\n", exp_data->id);
4487

4488 4489 4490 4491 4492 4493 4494 4495 4496 4497
        if(has_unicode){
            WCHAR *path1, *path2;

            path1 = make_wstr(exp_data->path_1);
            path2 = make_wstr(exp_data->path_2);

            exp_data->missing_events = exp_data->notify_count;
            SHChangeNotify(exp_data->signal, SHCNF_PATHW | SHCNF_FLUSH, path1, path2);
            do_events();
            ok(exp_data->missing_events == 0, "%s: Expected wndproc to be called\n", exp_data->id);
4498 4499 4500

            HeapFree(GetProcessHeap(), 0, path1);
            HeapFree(GetProcessHeap(), 0, path2);
4501 4502
        }
    }
4503 4504 4505 4506

    SHChangeNotifyDeregister(notifyID);
    DestroyWindow(wnd);

4507
    ILFree((LPITEMIDLIST)entries[0].pidl);
4508 4509 4510 4511
    br = RemoveDirectoryA(root_dirA);
    ok(br == TRUE, "RemoveDirectory failed: %d\n", GetLastError());
}

4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527
static void test_SHCreateDefaultContextMenu(void)
{
    HKEY keys[16];
    WCHAR path[MAX_PATH];
    IShellFolder *desktop,*folder;
    IPersistFolder2 *persist;
    IContextMenu *cmenu;
    LONG status;
    LPITEMIDLIST pidlFolder, pidl_child, pidl;
    DEFCONTEXTMENU cminfo;
    HRESULT hr;
    UINT i;
    const WCHAR filename[] =
        {'\\','t','e','s','t','d','i','r','\\','t','e','s','t','1','.','t','x','t',0};
    if(!pSHCreateDefaultContextMenu)
    {
4528
        win_skip("SHCreateDefaultContextMenu missing.\n");
4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598
        return;
    }

    if(!pSHBindToParent)
    {
        skip("SHBindToParent missing.\n");
        return;
    }

    GetCurrentDirectoryW(MAX_PATH, path);
    if(!lstrlenW(path))
    {
        skip("GetCurrentDirectoryW returned an empty string.\n");
        return;
    }
    lstrcatW(path, filename);
    SHGetDesktopFolder(&desktop);

    CreateFilesFolders();

    hr = IShellFolder_ParseDisplayName(desktop, NULL, NULL, path, NULL, &pidl, 0);
    ok(hr == S_OK || broken(hr == E_FAIL) /* WinME */, "Got 0x%08x\n", hr);
    if(SUCCEEDED(hr))
    {

        hr = pSHBindToParent(pidl, &IID_IShellFolder, (void**)&folder, (LPCITEMIDLIST*)&pidl_child);
        ok(hr == S_OK, "Got 0x%08x\n", hr);

        IShellFolder_QueryInterface(folder,&IID_IPersistFolder2,(void**)&persist);
        IPersistFolder2_GetCurFolder(persist,&pidlFolder);
        IPersistFolder2_Release(persist);
        if(SUCCEEDED(hr))
        {

            cminfo.hwnd=NULL;
            cminfo.pcmcb=NULL;
            cminfo.psf=folder;
            cminfo.pidlFolder=NULL;
            cminfo.apidl=(LPCITEMIDLIST*)&pidl_child;
            cminfo.cidl=1;
            cminfo.aKeys=NULL;
            cminfo.cKeys=0;
            cminfo.punkAssociationInfo=NULL;
            hr = pSHCreateDefaultContextMenu(&cminfo,&IID_IContextMenu,(void**)&cmenu);
            ok(hr==S_OK,"Got 0x%08x\n", hr);
            IContextMenu_Release(cmenu);
            cminfo.pidlFolder=pidlFolder;
            hr = pSHCreateDefaultContextMenu(&cminfo,&IID_IContextMenu,(void**)&cmenu);
            ok(hr==S_OK,"Got 0x%08x\n", hr);
            IContextMenu_Release(cmenu);
            status = RegOpenKeyExA(HKEY_CLASSES_ROOT,"*",0,KEY_READ,keys);
            if(status==ERROR_SUCCESS){
                for(i=1;i<16;i++)
                    keys[i]=keys[0];
                cminfo.aKeys=keys;
                cminfo.cKeys=16;
                hr = pSHCreateDefaultContextMenu(&cminfo,&IID_IContextMenu,(void**)&cmenu);
                RegCloseKey(keys[0]);
                ok(hr==S_OK,"Got 0x%08x\n", hr);
                IContextMenu_Release(cmenu);
            }
        }
        ILFree(pidlFolder);
        IShellFolder_Release(folder);
    }
    IShellFolder_Release(desktop);
    ILFree(pidl);
    Cleanup();
}

4599 4600
START_TEST(shlfolder)
{
4601
    init_function_pointers();
4602 4603 4604
    /* if OleInitialize doesn't get called, ParseDisplayName returns
       CO_E_NOTINITIALIZED for malformed directory names on win2k. */
    OleInitialize(NULL);
4605

4606
    test_ParseDisplayName();
4607
    test_SHParseDisplayName();
4608
    test_BindToObject();
4609
    test_EnumObjects_and_CompareIDs();
4610 4611 4612
    test_GetDisplayName();
    test_GetAttributesOf();
    test_SHGetPathFromIDList();
4613
    test_CallForAttributes();
4614
    test_FolderShortcut();
4615
    test_ITEMIDLIST_format();
4616
    test_SHGetFolderPathA();
4617
    test_SHGetFolderPathAndSubDirA();
4618
    test_LocalizedNames();
4619
    test_SHCreateShellItem();
4620
    test_SHCreateShellItemArray();
4621
    test_desktop_IPersist();
4622
    test_GetUIObject();
4623 4624
    test_SHSimpleIDListFromPath();
    test_ParseDisplayNamePBC();
4625
    test_SHGetNameFromIDList();
4626
    test_SHGetItemFromDataObject();
4627
    test_SHGetIDListFromObject();
4628
    test_SHGetItemFromObject();
4629
    test_ShellItemCompare();
4630 4631
    test_SHChangeNotify(FALSE);
    test_SHChangeNotify(TRUE);
4632
    test_ShellItemBindToHandler();
4633
    test_ShellItemGetAttributes();
4634
    test_SHCreateDefaultContextMenu();
4635 4636

    OleUninitialize();
4637
}