2 * Unit test of the IShellFolder functions.
4 * Copyright 2004 Vitaliy Margolen
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
40 #include "wine/test.h"
45 static HRESULT (WINAPI *pSHBindToParent)(LPCITEMIDLIST, REFIID, LPVOID*, LPCITEMIDLIST*);
46 static BOOL (WINAPI *pSHGetPathFromIDListW)(LPCITEMIDLIST,LPWSTR);
47 static BOOL (WINAPI *pSHGetSpecialFolderPathW)(HWND, LPWSTR, int, BOOL);
48 static HRESULT (WINAPI *pStrRetToBufW)(STRRET*,LPCITEMIDLIST,LPWSTR,UINT);
49 static LPITEMIDLIST (WINAPI *pILFindLastID)(LPCITEMIDLIST);
50 static void (WINAPI *pILFree)(LPITEMIDLIST);
51 static BOOL (WINAPI *pILIsEqual)(LPCITEMIDLIST, LPCITEMIDLIST);
53 static void init_function_pointers(void)
58 hmod = GetModuleHandleA("shell32.dll");
59 pSHBindToParent = (void*)GetProcAddress(hmod, "SHBindToParent");
60 pSHGetPathFromIDListW = (void*)GetProcAddress(hmod, "SHGetPathFromIDListW");
61 pSHGetSpecialFolderPathW = (void*)GetProcAddress(hmod, "SHGetSpecialFolderPathW");
62 pILFindLastID = (void *)GetProcAddress(hmod, (LPCSTR)16);
63 pILFree = (void*)GetProcAddress(hmod, (LPSTR)155);
64 pILIsEqual = (void*)GetProcAddress(hmod, (LPSTR)21);
66 hmod = GetModuleHandleA("shlwapi.dll");
67 pStrRetToBufW = (void*)GetProcAddress(hmod, "StrRetToBufW");
69 hr = SHGetMalloc(&ppM);
70 ok(hr == S_OK, "SHGetMalloc failed %08x\n", hr);
73 static void test_ParseDisplayName(void)
76 IShellFolder *IDesktopFolder;
77 static const char *cNonExistDir1A = "c:\\nonexist_subdir";
78 static const char *cNonExistDir2A = "c:\\\\nonexist_subdir";
80 WCHAR cTestDirW [MAX_PATH] = {0};
84 hr = SHGetDesktopFolder(&IDesktopFolder);
85 if(hr != S_OK) return;
87 res = GetFileAttributesA(cNonExistDir1A);
88 if(res != INVALID_FILE_ATTRIBUTES) return;
90 MultiByteToWideChar(CP_ACP, 0, cNonExistDir1A, -1, cTestDirW, MAX_PATH);
91 hr = IShellFolder_ParseDisplayName(IDesktopFolder,
92 NULL, NULL, cTestDirW, NULL, &newPIDL, 0);
93 ok((hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) || (hr == E_FAIL),
94 "ParseDisplayName returned %08x, expected 80070002 or E_FAIL\n", hr);
96 res = GetFileAttributesA(cNonExistDir2A);
97 if(res != INVALID_FILE_ATTRIBUTES) return;
99 MultiByteToWideChar(CP_ACP, 0, cNonExistDir2A, -1, cTestDirW, MAX_PATH);
100 hr = IShellFolder_ParseDisplayName(IDesktopFolder,
101 NULL, NULL, cTestDirW, NULL, &newPIDL, 0);
102 ok((hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) || (hr == E_FAIL) || (hr == E_INVALIDARG),
103 "ParseDisplayName returned %08x, expected 80070002, E_FAIL or E_INVALIDARG\n", hr);
105 /* I thought that perhaps the DesktopFolder's ParseDisplayName would recognize the
106 * path corresponding to CSIDL_PERSONAL and return a CLSID_MyDocuments PIDL. Turns
107 * out it doesn't. The magic seems to happen in the file dialogs, then. */
108 if (!pSHGetSpecialFolderPathW || !pILFindLastID) goto finished;
110 bRes = pSHGetSpecialFolderPathW(NULL, cTestDirW, CSIDL_PERSONAL, FALSE);
111 ok(bRes, "SHGetSpecialFolderPath(CSIDL_PERSONAL) failed! %u\n", GetLastError());
112 if (!bRes) goto finished;
114 hr = IShellFolder_ParseDisplayName(IDesktopFolder, NULL, NULL, cTestDirW, NULL, &newPIDL, 0);
115 ok(SUCCEEDED(hr), "DesktopFolder->ParseDisplayName failed. hr = %08x.\n", hr);
116 if (FAILED(hr)) goto finished;
118 ok(pILFindLastID(newPIDL)->mkid.abID[0] == 0x31, "Last pidl should be of type "
119 "PT_FOLDER, but is: %02x\n", pILFindLastID(newPIDL)->mkid.abID[0]);
120 IMalloc_Free(ppM, newPIDL);
123 IShellFolder_Release(IDesktopFolder);
126 /* creates a file with the specified name for tests */
127 static void CreateTestFile(const CHAR *name)
132 file = CreateFileA(name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
133 if (file != INVALID_HANDLE_VALUE)
135 WriteFile(file, name, strlen(name), &written, NULL);
136 WriteFile(file, "\n", strlen("\n"), &written, NULL);
142 /* initializes the tests */
143 static void CreateFilesFolders(void)
145 CreateDirectoryA(".\\testdir", NULL);
146 CreateDirectoryA(".\\testdir\\test.txt", NULL);
147 CreateTestFile (".\\testdir\\test1.txt ");
148 CreateTestFile (".\\testdir\\test2.txt ");
149 CreateTestFile (".\\testdir\\test3.txt ");
150 CreateDirectoryA(".\\testdir\\testdir2 ", NULL);
151 CreateDirectoryA(".\\testdir\\testdir2\\subdir", NULL);
154 /* cleans after tests */
155 static void Cleanup(void)
157 DeleteFileA(".\\testdir\\test1.txt");
158 DeleteFileA(".\\testdir\\test2.txt");
159 DeleteFileA(".\\testdir\\test3.txt");
160 RemoveDirectoryA(".\\testdir\\test.txt");
161 RemoveDirectoryA(".\\testdir\\testdir2\\subdir");
162 RemoveDirectoryA(".\\testdir\\testdir2");
163 RemoveDirectoryA(".\\testdir");
168 static void test_EnumObjects(IShellFolder *iFolder)
170 IEnumIDList *iEnumList;
171 LPITEMIDLIST newPIDL, idlArr[10];
176 static const WORD iResults [5][5] =
185 #define SFGAO_testfor SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR | SFGAO_CAPABILITYMASK
186 /* Don't test for SFGAO_HASSUBFOLDER since we return real state and native cached */
187 static const ULONG attrs[5] =
189 SFGAO_CAPABILITYMASK | SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR,
190 SFGAO_CAPABILITYMASK | SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR,
191 SFGAO_CAPABILITYMASK | SFGAO_FILESYSTEM,
192 SFGAO_CAPABILITYMASK | SFGAO_FILESYSTEM,
193 SFGAO_CAPABILITYMASK | SFGAO_FILESYSTEM,
196 hr = IShellFolder_EnumObjects(iFolder, NULL, SHCONTF_FOLDERS | SHCONTF_NONFOLDERS | SHCONTF_INCLUDEHIDDEN, &iEnumList);
197 ok(hr == S_OK, "EnumObjects failed %08x\n", hr);
199 /* This is to show that, contrary to what is said on MSDN, on IEnumIDList::Next,
200 * the filesystem shellfolders return S_OK even if less than 'celt' items are
201 * returned (in contrast to S_FALSE). We have to do it in a loop since WinXP
202 * only ever returns a single entry per call. */
203 while (IEnumIDList_Next(iEnumList, 10-i, &idlArr[i], &NumPIDLs) == S_OK)
205 ok (i == 5, "i: %d\n", i);
207 hr = IEnumIDList_Release(iEnumList);
208 ok(hr == S_OK, "IEnumIDList_Release failed %08x\n", hr);
210 /* Sort them first in case of wrong order from system */
211 for (i=0;i<5;i++) for (j=0;j<5;j++)
212 if ((SHORT)IShellFolder_CompareIDs(iFolder, 0, idlArr[i], idlArr[j]) < 0)
215 idlArr[i] = idlArr[j];
219 for (i=0;i<5;i++) for (j=0;j<5;j++)
221 hr = IShellFolder_CompareIDs(iFolder, 0, idlArr[i], idlArr[j]);
222 ok(hr == iResults[i][j], "Got %x expected [%d]-[%d]=%x\n", hr, i, j, iResults[i][j]);
226 for (i = 0; i < 5; i++)
229 /* Native returns all flags no matter what we ask for */
230 flags = SFGAO_CANCOPY;
231 hr = IShellFolder_GetAttributesOf(iFolder, 1, (LPCITEMIDLIST*)(idlArr + i), &flags);
232 flags &= SFGAO_testfor;
233 ok(hr == S_OK, "GetAttributesOf returns %08x\n", hr);
234 ok(flags == (attrs[i]), "GetAttributesOf[%i] got %08x, expected %08x\n", i, flags, attrs[i]);
236 flags = SFGAO_testfor;
237 hr = IShellFolder_GetAttributesOf(iFolder, 1, (LPCITEMIDLIST*)(idlArr + i), &flags);
238 flags &= SFGAO_testfor;
239 ok(hr == S_OK, "GetAttributesOf returns %08x\n", hr);
240 ok(flags == attrs[i], "GetAttributesOf[%i] got %08x, expected %08x\n", i, flags, attrs[i]);
244 IMalloc_Free(ppM, idlArr[i]);
247 static void test_BindToObject(void)
251 IShellFolder *psfDesktop, *psfChild, *psfMyComputer, *psfSystemDir;
252 SHITEMID emptyitem = { 0, { 0 } };
253 LPITEMIDLIST pidlMyComputer, pidlSystemDir, pidlEmpty = (LPITEMIDLIST)&emptyitem;
254 WCHAR wszSystemDir[MAX_PATH];
255 char szSystemDir[MAX_PATH];
256 WCHAR wszMyComputer[] = {
257 ':',':','{','2','0','D','0','4','F','E','0','-','3','A','E','A','-','1','0','6','9','-',
258 'A','2','D','8','-','0','8','0','0','2','B','3','0','3','0','9','D','}',0 };
260 /* The following tests shows that BindToObject should fail with E_INVALIDARG if called
261 * with an empty pidl. This is tested for Desktop, MyComputer and the FS ShellFolder
263 hr = SHGetDesktopFolder(&psfDesktop);
264 ok (SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08x\n", hr);
265 if (FAILED(hr)) return;
267 hr = IShellFolder_BindToObject(psfDesktop, pidlEmpty, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
268 ok (hr == E_INVALIDARG, "Desktop's BindToObject should fail, when called with empty pidl! hr = %08x\n", hr);
270 hr = IShellFolder_BindToObject(psfDesktop, NULL, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
271 ok (hr == E_INVALIDARG, "Desktop's BindToObject should fail, when called with NULL pidl! hr = %08x\n", hr);
273 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszMyComputer, NULL, &pidlMyComputer, NULL);
274 ok (SUCCEEDED(hr), "Desktop's ParseDisplayName failed to parse MyComputer's CLSID! hr = %08x\n", hr);
276 IShellFolder_Release(psfDesktop);
280 hr = IShellFolder_BindToObject(psfDesktop, pidlMyComputer, NULL, &IID_IShellFolder, (LPVOID*)&psfMyComputer);
281 ok (SUCCEEDED(hr), "Desktop failed to bind to MyComputer object! hr = %08x\n", hr);
282 IShellFolder_Release(psfDesktop);
283 IMalloc_Free(ppM, pidlMyComputer);
284 if (FAILED(hr)) return;
286 hr = IShellFolder_BindToObject(psfMyComputer, pidlEmpty, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
287 ok (hr == E_INVALIDARG, "MyComputers's BindToObject should fail, when called with empty pidl! hr = %08x\n", hr);
290 /* this call segfaults on 98SE */
291 hr = IShellFolder_BindToObject(psfMyComputer, NULL, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
292 ok (hr == E_INVALIDARG, "MyComputers's BindToObject should fail, when called with NULL pidl! hr = %08x\n", hr);
295 cChars = GetSystemDirectoryA(szSystemDir, MAX_PATH);
296 ok (cChars > 0 && cChars < MAX_PATH, "GetSystemDirectoryA failed! LastError: %u\n", GetLastError());
297 if (cChars == 0 || cChars >= MAX_PATH) {
298 IShellFolder_Release(psfMyComputer);
301 MultiByteToWideChar(CP_ACP, 0, szSystemDir, -1, wszSystemDir, MAX_PATH);
303 hr = IShellFolder_ParseDisplayName(psfMyComputer, NULL, NULL, wszSystemDir, NULL, &pidlSystemDir, NULL);
304 ok (SUCCEEDED(hr), "MyComputers's ParseDisplayName failed to parse the SystemDirectory! hr = %08x\n", hr);
306 IShellFolder_Release(psfMyComputer);
310 hr = IShellFolder_BindToObject(psfMyComputer, pidlSystemDir, NULL, &IID_IShellFolder, (LPVOID*)&psfSystemDir);
311 ok (SUCCEEDED(hr), "MyComputer failed to bind to a FileSystem ShellFolder! hr = %08x\n", hr);
312 IShellFolder_Release(psfMyComputer);
313 IMalloc_Free(ppM, pidlSystemDir);
314 if (FAILED(hr)) return;
316 hr = IShellFolder_BindToObject(psfSystemDir, pidlEmpty, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
317 ok (hr == E_INVALIDARG,
318 "FileSystem ShellFolder's BindToObject should fail, when called with empty pidl! hr = %08x\n", hr);
321 /* this call segfaults on 98SE */
322 hr = IShellFolder_BindToObject(psfSystemDir, NULL, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
323 ok (hr == E_INVALIDARG,
324 "FileSystem ShellFolder's BindToObject should fail, when called with NULL pidl! hr = %08x\n", hr);
327 IShellFolder_Release(psfSystemDir);
330 /* Based on PathAddBackslashW from dlls/shlwapi/path.c */
331 static LPWSTR myPathAddBackslashW( LPWSTR lpszPath )
335 if (!lpszPath || (iLen = lstrlenW(lpszPath)) >= MAX_PATH)
341 if (lpszPath[-1] != '\\')
350 static void test_GetDisplayName(void)
355 WCHAR wszTestFile[MAX_PATH], wszTestFile2[MAX_PATH], wszTestDir[MAX_PATH];
356 char szTestFile[MAX_PATH], szTestDir[MAX_PATH];
359 LPSHELLFOLDER psfDesktop, psfPersonal;
361 SHITEMID emptyitem = { 0, { 0 } };
362 LPITEMIDLIST pidlTestFile, pidlEmpty = (LPITEMIDLIST)&emptyitem;
363 LPCITEMIDLIST pidlLast;
364 static const WCHAR wszFileName[] = { 'w','i','n','e','t','e','s','t','.','f','o','o',0 };
365 static const WCHAR wszDirName[] = { 'w','i','n','e','t','e','s','t',0 };
367 /* I'm trying to figure if there is a functional difference between calling
368 * SHGetPathFromIDListW and calling GetDisplayNameOf(SHGDN_FORPARSING) after
369 * binding to the shellfolder. One thing I thought of was that perhaps
370 * SHGetPathFromIDListW would be able to get the path to a file, which does
371 * not exist anymore, while the other method wouldn't. It turns out there's
372 * no functional difference in this respect.
375 if(!pSHGetSpecialFolderPathW) return;
377 /* First creating a directory in MyDocuments and a file in this directory. */
378 result = pSHGetSpecialFolderPathW(NULL, wszTestDir, CSIDL_PERSONAL, FALSE);
379 ok(result, "SHGetSpecialFolderPathW failed! Last error: %u\n", GetLastError());
382 myPathAddBackslashW(wszTestDir);
383 lstrcatW(wszTestDir, wszDirName);
384 /* Use ANSI file functions so this works on Windows 9x */
385 WideCharToMultiByte(CP_ACP, 0, wszTestDir, -1, szTestDir, MAX_PATH, 0, 0);
386 CreateDirectoryA(szTestDir, NULL);
387 attr=GetFileAttributesA(szTestDir);
388 if (attr == INVALID_FILE_ATTRIBUTES || !(attr & FILE_ATTRIBUTE_DIRECTORY))
390 ok(0, "unable to create the '%s' directory\n", szTestDir);
394 lstrcpyW(wszTestFile, wszTestDir);
395 myPathAddBackslashW(wszTestFile);
396 lstrcatW(wszTestFile, wszFileName);
397 WideCharToMultiByte(CP_ACP, 0, wszTestFile, -1, szTestFile, MAX_PATH, 0, 0);
399 hTestFile = CreateFileA(szTestFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
400 ok((hTestFile != INVALID_HANDLE_VALUE), "CreateFileA failed! Last error: %u\n", GetLastError());
401 if (hTestFile == INVALID_HANDLE_VALUE) return;
402 CloseHandle(hTestFile);
404 /* Getting an itemidlist for the file. */
405 hr = SHGetDesktopFolder(&psfDesktop);
406 ok(SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08x\n", hr);
407 if (FAILED(hr)) return;
409 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszTestFile, NULL, &pidlTestFile, NULL);
410 ok(SUCCEEDED(hr), "Desktop->ParseDisplayName failed! hr = %08x\n", hr);
412 IShellFolder_Release(psfDesktop);
416 /* WinXP stores the filenames as both ANSI and UNICODE in the pidls */
417 pidlLast = pILFindLastID(pidlTestFile);
418 ok(pidlLast->mkid.cb >=76, "Expected pidl length of at least 76, got %d.\n", pidlLast->mkid.cb);
419 if (pidlLast->mkid.cb >= 76) {
420 ok(!lstrcmpW((WCHAR*)&pidlLast->mkid.abID[46], wszFileName),
421 "WinXP stores the filename as a wchar-string at this position!\n");
424 /* It seems as if we cannot bind to regular files on windows, but only directories.
426 hr = IShellFolder_BindToObject(psfDesktop, pidlTestFile, NULL, &IID_IUnknown, (VOID**)&psfFile);
427 todo_wine { ok (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), "hr = %08x\n", hr); }
429 IShellFolder_Release(psfFile);
432 /* Some tests for IShellFolder::SetNameOf */
433 hr = pSHBindToParent(pidlTestFile, &IID_IShellFolder, (VOID**)&psfPersonal, &pidlLast);
434 ok(SUCCEEDED(hr), "SHBindToParent failed! hr = %08x\n", hr);
436 /* It's ok to use this fixed path. Call will fail anyway. */
437 WCHAR wszAbsoluteFilename[] = { 'C',':','\\','w','i','n','e','t','e','s','t', 0 };
438 LPITEMIDLIST pidlNew;
440 /* The pidl returned through the last parameter of SetNameOf is a simple one. */
441 hr = IShellFolder_SetNameOf(psfPersonal, NULL, pidlLast, wszDirName, SHGDN_NORMAL, &pidlNew);
442 ok (SUCCEEDED(hr), "SetNameOf failed! hr = %08x\n", hr);
443 ok (((LPITEMIDLIST)((LPBYTE)pidlNew+pidlNew->mkid.cb))->mkid.cb == 0,
444 "pidl returned from SetNameOf should be simple!\n");
446 /* Passing an absolute path to SetNameOf fails. The HRESULT code indicates that SetNameOf
447 * is implemented on top of SHFileOperation in WinXP. */
448 hr = IShellFolder_SetNameOf(psfPersonal, NULL, pidlNew, wszAbsoluteFilename,
449 SHGDN_FORPARSING, NULL);
450 ok (hr == HRESULT_FROM_WIN32(ERROR_CANCELLED), "SetNameOf succeeded! hr = %08x\n", hr);
452 /* Rename the file back to its original name. SetNameOf ignores the fact, that the
453 * SHGDN flags specify an absolute path. */
454 hr = IShellFolder_SetNameOf(psfPersonal, NULL, pidlNew, wszFileName, SHGDN_FORPARSING, NULL);
455 ok (SUCCEEDED(hr), "SetNameOf failed! hr = %08x\n", hr);
458 IShellFolder_Release(psfPersonal);
461 /* Deleting the file and the directory */
462 DeleteFileA(szTestFile);
463 RemoveDirectoryA(szTestDir);
465 /* SHGetPathFromIDListW still works, although the file is not present anymore. */
466 if (pSHGetPathFromIDListW)
468 result = pSHGetPathFromIDListW(pidlTestFile, wszTestFile2);
469 ok (result, "SHGetPathFromIDListW failed! Last error: %u\n", GetLastError());
470 ok (!lstrcmpiW(wszTestFile, wszTestFile2), "SHGetPathFromIDListW returns incorrect path!\n");
473 if(!pSHBindToParent) return;
475 /* SHBindToParent fails, if called with a NULL PIDL. */
476 hr = pSHBindToParent(NULL, &IID_IShellFolder, (VOID**)&psfPersonal, &pidlLast);
477 ok (FAILED(hr), "SHBindToParent(NULL) should fail!\n");
479 /* But it succeeds with an empty PIDL. */
480 hr = pSHBindToParent(pidlEmpty, &IID_IShellFolder, (VOID**)&psfPersonal, &pidlLast);
481 ok (SUCCEEDED(hr), "SHBindToParent(empty PIDL) should succeed! hr = %08x\n", hr);
482 ok (pidlLast == pidlEmpty, "The last element of an empty PIDL should be the PIDL itself!\n");
484 IShellFolder_Release(psfPersonal);
486 /* Binding to the folder and querying the display name of the file also works. */
487 hr = pSHBindToParent(pidlTestFile, &IID_IShellFolder, (VOID**)&psfPersonal, &pidlLast);
488 ok (SUCCEEDED(hr), "SHBindToParent failed! hr = %08x\n", hr);
490 IShellFolder_Release(psfDesktop);
494 /* This test shows that Windows doesn't allocate a new pidlLast, but returns a pointer into
495 * pidlTestFile (In accordance with MSDN). */
496 ok (pILFindLastID(pidlTestFile) == pidlLast,
497 "SHBindToParent doesn't return the last id of the pidl param!\n");
499 hr = IShellFolder_GetDisplayNameOf(psfPersonal, pidlLast, SHGDN_FORPARSING, &strret);
500 ok (SUCCEEDED(hr), "Personal->GetDisplayNameOf failed! hr = %08x\n", hr);
502 IShellFolder_Release(psfDesktop);
503 IShellFolder_Release(psfPersonal);
509 hr = pStrRetToBufW(&strret, pidlLast, wszTestFile2, MAX_PATH);
510 ok (SUCCEEDED(hr), "StrRetToBufW failed! hr = %08x\n", hr);
511 ok (!lstrcmpiW(wszTestFile, wszTestFile2), "GetDisplayNameOf returns incorrect path!\n");
514 IShellFolder_Release(psfDesktop);
515 IShellFolder_Release(psfPersonal);
518 static void test_CallForAttributes(void)
524 LPSHELLFOLDER psfDesktop;
525 LPITEMIDLIST pidlMyDocuments;
526 DWORD dwAttributes, dwCallForAttributes, dwOrigAttributes, dwOrigCallForAttributes;
527 static const WCHAR wszAttributes[] = { 'A','t','t','r','i','b','u','t','e','s',0 };
528 static const WCHAR wszCallForAttributes[] = {
529 'C','a','l','l','F','o','r','A','t','t','r','i','b','u','t','e','s',0 };
530 static const WCHAR wszMyDocumentsKey[] = {
531 'C','L','S','I','D','\\','{','4','5','0','D','8','F','B','A','-','A','D','2','5','-',
532 '1','1','D','0','-','9','8','A','8','-','0','8','0','0','3','6','1','B','1','1','0','3','}',
533 '\\','S','h','e','l','l','F','o','l','d','e','r',0 };
534 WCHAR wszMyDocuments[] = {
535 ':',':','{','4','5','0','D','8','F','B','A','-','A','D','2','5','-','1','1','D','0','-',
536 '9','8','A','8','-','0','8','0','0','3','6','1','B','1','1','0','3','}',0 };
538 /* For the root of a namespace extension, the attributes are not queried by binding
539 * to the object and calling GetAttributesOf. Instead, the attributes are read from
540 * the registry value HKCR/CLSID/{...}/ShellFolder/Attributes. This is documented on MSDN.
542 * The MyDocuments shellfolder on WinXP has a HKCR/CLSID/{...}/ShellFolder/CallForAttributes
543 * value. It seems that if the folder is queried for one of the flags set in CallForAttributes,
544 * the shell does bind to the folder object and calls GetAttributesOf. This is not documented
545 * on MSDN. This test is meant to document the observed behaviour on WinXP SP2.
547 hr = SHGetDesktopFolder(&psfDesktop);
548 ok (SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08x\n", hr);
549 if (FAILED(hr)) return;
551 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszMyDocuments, NULL,
552 &pidlMyDocuments, NULL);
554 "Desktop's ParseDisplayName failed to parse MyDocuments's CLSID! hr = %08x\n", hr);
556 IShellFolder_Release(psfDesktop);
560 dwAttributes = 0xffffffff;
561 hr = IShellFolder_GetAttributesOf(psfDesktop, 1,
562 (LPCITEMIDLIST*)&pidlMyDocuments, &dwAttributes);
563 ok (SUCCEEDED(hr), "Desktop->GetAttributesOf(MyDocuments) failed! hr = %08x\n", hr);
565 /* We need the following setup (as observed on WinXP SP2), for the tests to make sense. */
566 ok (dwAttributes & SFGAO_FILESYSTEM, "SFGAO_FILESYSTEM attribute is not set for MyDocuments!\n");
567 ok (!(dwAttributes & SFGAO_ISSLOW), "SFGAO_ISSLOW attribute is set for MyDocuments!\n");
568 ok (!(dwAttributes & SFGAO_GHOSTED), "SFGAO_GHOSTED attribute is set for MyDocuments!\n");
570 /* We don't have the MyDocuments shellfolder in wine yet, and thus we don't have the registry
571 * key. So the test will return at this point, if run on wine.
573 lResult = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszMyDocumentsKey, 0, KEY_WRITE|KEY_READ, &hKey);
574 ok (lResult == ERROR_SUCCESS, "RegOpenKeyEx failed! result: %08x\n", lResult);
575 if (lResult != ERROR_SUCCESS) {
576 IMalloc_Free(ppM, pidlMyDocuments);
577 IShellFolder_Release(psfDesktop);
581 /* Query MyDocuments' Attributes value, to be able to restore it later. */
582 dwSize = sizeof(DWORD);
583 lResult = RegQueryValueExW(hKey, wszAttributes, NULL, NULL, (LPBYTE)&dwOrigAttributes, &dwSize);
584 ok (lResult == ERROR_SUCCESS, "RegQueryValueEx failed! result: %08x\n", lResult);
585 if (lResult != ERROR_SUCCESS) {
587 IMalloc_Free(ppM, pidlMyDocuments);
588 IShellFolder_Release(psfDesktop);
592 /* Query MyDocuments' CallForAttributes value, to be able to restore it later. */
593 dwSize = sizeof(DWORD);
594 lResult = RegQueryValueExW(hKey, wszCallForAttributes, NULL, NULL,
595 (LPBYTE)&dwOrigCallForAttributes, &dwSize);
596 ok (lResult == ERROR_SUCCESS, "RegQueryValueEx failed! result: %08x\n", lResult);
597 if (lResult != ERROR_SUCCESS) {
599 IMalloc_Free(ppM, pidlMyDocuments);
600 IShellFolder_Release(psfDesktop);
604 /* Define via the Attributes value that MyDocuments attributes are SFGAO_ISSLOW and
605 * SFGAO_GHOSTED and that MyDocuments should be called for the SFGAO_ISSLOW and
606 * SFGAO_FILESYSTEM attributes. */
607 dwAttributes = SFGAO_ISSLOW|SFGAO_GHOSTED;
608 RegSetValueExW(hKey, wszAttributes, 0, REG_DWORD, (LPBYTE)&dwAttributes, sizeof(DWORD));
609 dwCallForAttributes = SFGAO_ISSLOW|SFGAO_FILESYSTEM;
610 RegSetValueExW(hKey, wszCallForAttributes, 0, REG_DWORD,
611 (LPBYTE)&dwCallForAttributes, sizeof(DWORD));
613 /* Although it is not set in CallForAttributes, the SFGAO_GHOSTED flag is reset by
614 * GetAttributesOf. It seems that once there is a single attribute queried, for which
615 * CallForAttributes is set, all flags are taken from the GetAttributesOf call and
616 * the flags in Attributes are ignored.
618 dwAttributes = SFGAO_ISSLOW|SFGAO_GHOSTED|SFGAO_FILESYSTEM;
619 hr = IShellFolder_GetAttributesOf(psfDesktop, 1,
620 (LPCITEMIDLIST*)&pidlMyDocuments, &dwAttributes);
621 ok (SUCCEEDED(hr), "Desktop->GetAttributesOf(MyDocuments) failed! hr = %08x\n", hr);
623 ok (dwAttributes == SFGAO_FILESYSTEM,
624 "Desktop->GetAttributes(MyDocuments) returned unexpected attributes: %08x\n",
627 /* Restore MyDocuments' original Attributes and CallForAttributes registry values */
628 RegSetValueExW(hKey, wszAttributes, 0, REG_DWORD, (LPBYTE)&dwOrigAttributes, sizeof(DWORD));
629 RegSetValueExW(hKey, wszCallForAttributes, 0, REG_DWORD,
630 (LPBYTE)&dwOrigCallForAttributes, sizeof(DWORD));
632 IMalloc_Free(ppM, pidlMyDocuments);
633 IShellFolder_Release(psfDesktop);
636 static void test_GetAttributesOf(void)
639 LPSHELLFOLDER psfDesktop, psfMyComputer;
640 SHITEMID emptyitem = { 0, { 0 } };
641 LPCITEMIDLIST pidlEmpty = (LPCITEMIDLIST)&emptyitem;
642 LPITEMIDLIST pidlMyComputer;
644 static const DWORD dwDesktopFlags = /* As observed on WinXP SP2 */
645 SFGAO_STORAGE | SFGAO_HASPROPSHEET | SFGAO_STORAGEANCESTOR |
646 SFGAO_FILESYSANCESTOR | SFGAO_FOLDER | SFGAO_FILESYSTEM | SFGAO_HASSUBFOLDER;
647 static const DWORD dwMyComputerFlags = /* As observed on WinXP SP2 */
648 SFGAO_CANRENAME | SFGAO_CANDELETE | SFGAO_HASPROPSHEET |
649 SFGAO_DROPTARGET | SFGAO_FILESYSANCESTOR | SFGAO_FOLDER | SFGAO_HASSUBFOLDER;
650 WCHAR wszMyComputer[] = {
651 ':',':','{','2','0','D','0','4','F','E','0','-','3','A','E','A','-','1','0','6','9','-',
652 'A','2','D','8','-','0','8','0','0','2','B','3','0','3','0','9','D','}',0 };
653 char cCurrDirA [MAX_PATH] = {0};
654 WCHAR cCurrDirW [MAX_PATH];
655 static WCHAR cTestDirW[] = {'t','e','s','t','d','i','r',0};
656 static const WCHAR cBackSlash[] = {'\\',0};
657 IShellFolder *IDesktopFolder, *testIShellFolder;
661 hr = SHGetDesktopFolder(&psfDesktop);
662 ok (SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08x\n", hr);
663 if (FAILED(hr)) return;
665 /* The Desktop attributes can be queried with a single empty itemidlist, .. */
666 dwFlags = 0xffffffff;
667 hr = IShellFolder_GetAttributesOf(psfDesktop, 1, &pidlEmpty, &dwFlags);
668 ok (SUCCEEDED(hr), "Desktop->GetAttributesOf(empty pidl) failed! hr = %08x\n", hr);
669 ok (dwFlags == dwDesktopFlags, "Wrong Desktop attributes: %08x, expected: %08x\n",
670 dwFlags, dwDesktopFlags);
672 /* .. or with no itemidlist at all. */
673 dwFlags = 0xffffffff;
674 hr = IShellFolder_GetAttributesOf(psfDesktop, 0, NULL, &dwFlags);
675 ok (SUCCEEDED(hr), "Desktop->GetAttributesOf(NULL) failed! hr = %08x\n", hr);
676 ok (dwFlags == dwDesktopFlags, "Wrong Desktop attributes: %08x, expected: %08x\n",
677 dwFlags, dwDesktopFlags);
679 /* Testing the attributes of the MyComputer shellfolder */
680 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszMyComputer, NULL, &pidlMyComputer, NULL);
681 ok (SUCCEEDED(hr), "Desktop's ParseDisplayName failed to parse MyComputer's CLSID! hr = %08x\n", hr);
683 IShellFolder_Release(psfDesktop);
687 /* WinXP SP2 sets the SFGAO_CANLINK flag, when MyComputer is queried via the Desktop
688 * folder object. It doesn't do this, if MyComputer is queried directly (see below).
689 * SFGAO_CANLINK is the same as DROPEFFECT_LINK, which MSDN says means: "Drag source
690 * should create a link to the original data". You can't create links on MyComputer on
691 * Windows, so this flag shouldn't be set. Seems like a bug in Windows. As long as nobody
692 * depends on this bug, we probably shouldn't imitate it.
694 dwFlags = 0xffffffff;
695 hr = IShellFolder_GetAttributesOf(psfDesktop, 1, (LPCITEMIDLIST*)&pidlMyComputer, &dwFlags);
696 ok (SUCCEEDED(hr), "Desktop->GetAttributesOf(MyComputer) failed! hr = %08x\n", hr);
697 ok ((dwFlags & ~(DWORD)SFGAO_CANLINK) == dwMyComputerFlags,
698 "Wrong MyComputer attributes: %08x, expected: %08x\n", dwFlags, dwMyComputerFlags);
700 hr = IShellFolder_BindToObject(psfDesktop, pidlMyComputer, NULL, &IID_IShellFolder, (LPVOID*)&psfMyComputer);
701 ok (SUCCEEDED(hr), "Desktop failed to bind to MyComputer object! hr = %08x\n", hr);
702 IShellFolder_Release(psfDesktop);
703 IMalloc_Free(ppM, pidlMyComputer);
704 if (FAILED(hr)) return;
706 hr = IShellFolder_GetAttributesOf(psfMyComputer, 1, &pidlEmpty, &dwFlags);
707 todo_wine {ok (hr == E_INVALIDARG, "MyComputer->GetAttributesOf(emtpy pidl) should fail! hr = %08x\n", hr); }
709 dwFlags = 0xffffffff;
710 hr = IShellFolder_GetAttributesOf(psfMyComputer, 0, NULL, &dwFlags);
711 ok (SUCCEEDED(hr), "MyComputer->GetAttributesOf(NULL) failed! hr = %08x\n", hr);
712 todo_wine { ok (dwFlags == dwMyComputerFlags,
713 "Wrong MyComputer attributes: %08x, expected: %08x\n", dwFlags, dwMyComputerFlags); }
715 IShellFolder_Release(psfMyComputer);
717 /* create test directory */
718 CreateFilesFolders();
720 GetCurrentDirectoryA(MAX_PATH, cCurrDirA);
721 len = lstrlenA(cCurrDirA);
724 trace("GetCurrentDirectoryA returned empty string. Skipping test_EnumObjects_and_CompareIDs\n");
727 if(cCurrDirA[len-1] == '\\')
728 cCurrDirA[len-1] = 0;
730 MultiByteToWideChar(CP_ACP, 0, cCurrDirA, -1, cCurrDirW, MAX_PATH);
732 hr = SHGetDesktopFolder(&IDesktopFolder);
733 ok(hr == S_OK, "SHGetDesktopfolder failed %08x\n", hr);
735 hr = IShellFolder_ParseDisplayName(IDesktopFolder, NULL, NULL, cCurrDirW, NULL, &newPIDL, 0);
736 ok(hr == S_OK, "ParseDisplayName failed %08x\n", hr);
738 hr = IShellFolder_BindToObject(IDesktopFolder, newPIDL, NULL, (REFIID)&IID_IShellFolder, (LPVOID *)&testIShellFolder);
739 ok(hr == S_OK, "BindToObject failed %08x\n", hr);
741 IMalloc_Free(ppM, newPIDL);
743 /* get relative PIDL */
744 hr = IShellFolder_ParseDisplayName(testIShellFolder, NULL, NULL, cTestDirW, NULL, &newPIDL, 0);
745 ok(hr == S_OK, "ParseDisplayName failed %08x\n", hr);
747 /* test the shell attributes of the test directory using the relative PIDL */
748 dwFlags = SFGAO_FOLDER;
749 hr = IShellFolder_GetAttributesOf(testIShellFolder, 1, (LPCITEMIDLIST*)&newPIDL, &dwFlags);
750 ok (SUCCEEDED(hr), "Desktop->GetAttributesOf() failed! hr = %08x\n", hr);
751 ok ((dwFlags&SFGAO_FOLDER), "Wrong directory attribute for relative PIDL: %08x\n", dwFlags);
754 IMalloc_Free(ppM, newPIDL);
756 /* append testdirectory name to path */
757 lstrcatW(cCurrDirW, cBackSlash);
758 lstrcatW(cCurrDirW, cTestDirW);
760 hr = IShellFolder_ParseDisplayName(IDesktopFolder, NULL, NULL, cCurrDirW, NULL, &newPIDL, 0);
761 ok(hr == S_OK, "ParseDisplayName failed %08x\n", hr);
763 /* test the shell attributes of the test directory using the absolute PIDL */
764 dwFlags = SFGAO_FOLDER;
765 hr = IShellFolder_GetAttributesOf(IDesktopFolder, 1, (LPCITEMIDLIST*)&newPIDL, &dwFlags);
766 ok (SUCCEEDED(hr), "Desktop->GetAttributesOf() failed! hr = %08x\n", hr);
767 ok ((dwFlags&SFGAO_FOLDER), "Wrong directory attribute for absolute PIDL: %08x\n", dwFlags);
770 IMalloc_Free(ppM, newPIDL);
772 IShellFolder_Release(testIShellFolder);
776 IShellFolder_Release(IDesktopFolder);
779 static void test_SHGetPathFromIDList(void)
781 SHITEMID emptyitem = { 0, { 0 } };
782 LPCITEMIDLIST pidlEmpty = (LPCITEMIDLIST)&emptyitem;
783 LPITEMIDLIST pidlMyComputer;
784 WCHAR wszPath[MAX_PATH], wszDesktop[MAX_PATH];
787 LPSHELLFOLDER psfDesktop;
788 WCHAR wszMyComputer[] = {
789 ':',':','{','2','0','D','0','4','F','E','0','-','3','A','E','A','-','1','0','6','9','-',
790 'A','2','D','8','-','0','8','0','0','2','B','3','0','3','0','9','D','}',0 };
791 WCHAR wszFileName[MAX_PATH];
792 LPITEMIDLIST pidlTestFile;
795 static WCHAR wszTestFile[] = {
796 'w','i','n','e','t','e','s','t','.','f','o','o',0 };
797 HRESULT (WINAPI *pSHGetSpecialFolderLocation)(HWND, int, LPITEMIDLIST *);
799 LPITEMIDLIST pidlPrograms;
801 if(!pSHGetPathFromIDListW || !pSHGetSpecialFolderPathW)
803 skip("SHGetPathFromIDListW() or SHGetSpecialFolderPathW() is missing\n");
807 /* Calling SHGetPathFromIDListW with no pidl should return the empty string */
810 result = pSHGetPathFromIDListW(NULL, wszPath);
811 ok(!result, "Expected failure\n");
812 ok(!wszPath[0], "Expected empty string\n");
814 /* Calling SHGetPathFromIDListW with an empty pidl should return the desktop folder's path. */
815 result = pSHGetSpecialFolderPathW(NULL, wszDesktop, CSIDL_DESKTOP, FALSE);
816 ok(result, "SHGetSpecialFolderPathW(CSIDL_DESKTOP) failed! Last error: %u\n", GetLastError());
819 result = pSHGetPathFromIDListW(pidlEmpty, wszPath);
820 ok(result, "SHGetPathFromIDListW failed! Last error: %u\n", GetLastError());
822 ok(!lstrcmpiW(wszDesktop, wszPath), "SHGetPathFromIDListW didn't return desktop path for empty pidl!\n");
824 /* MyComputer does not map to a filesystem path. SHGetPathFromIDListW should fail. */
825 hr = SHGetDesktopFolder(&psfDesktop);
826 ok (SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08x\n", hr);
827 if (FAILED(hr)) return;
829 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszMyComputer, NULL, &pidlMyComputer, NULL);
830 ok (SUCCEEDED(hr), "Desktop's ParseDisplayName failed to parse MyComputer's CLSID! hr = %08x\n", hr);
832 IShellFolder_Release(psfDesktop);
836 SetLastError(0xdeadbeef);
839 result = pSHGetPathFromIDListW(pidlMyComputer, wszPath);
840 ok (!result, "SHGetPathFromIDListW succeeded where it shouldn't!\n");
841 ok (GetLastError()==0xdeadbeef, "SHGetPathFromIDListW shouldn't set last error! Last error: %u\n", GetLastError());
842 ok (!wszPath[0], "Expected empty path\n");
844 IShellFolder_Release(psfDesktop);
848 IMalloc_Free(ppM, pidlMyComputer);
850 result = pSHGetSpecialFolderPathW(NULL, wszFileName, CSIDL_DESKTOPDIRECTORY, FALSE);
851 ok(result, "SHGetSpecialFolderPathW failed! Last error: %u\n", GetLastError());
853 IShellFolder_Release(psfDesktop);
856 myPathAddBackslashW(wszFileName);
857 lstrcatW(wszFileName, wszTestFile);
858 hTestFile = CreateFileW(wszFileName, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, NULL);
859 ok(hTestFile != INVALID_HANDLE_VALUE, "CreateFileW failed! Last error: %u\n", GetLastError());
860 if (hTestFile == INVALID_HANDLE_VALUE) {
861 IShellFolder_Release(psfDesktop);
864 CloseHandle(hTestFile);
866 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszTestFile, NULL, &pidlTestFile, NULL);
867 ok (SUCCEEDED(hr), "Desktop's ParseDisplayName failed to parse filename hr = %08x\n", hr);
869 IShellFolder_Release(psfDesktop);
870 DeleteFileW(wszFileName);
871 IMalloc_Free(ppM, pidlTestFile);
875 /* This test is to show that the Desktop shellfolder prepends the CSIDL_DESKTOPDIRECTORY
876 * path for files placed on the desktop, if called with SHGDN_FORPARSING. */
877 hr = IShellFolder_GetDisplayNameOf(psfDesktop, pidlTestFile, SHGDN_FORPARSING, &strret);
878 ok (SUCCEEDED(hr), "Desktop's GetDisplayNamfOf failed! hr = %08x\n", hr);
879 IShellFolder_Release(psfDesktop);
880 DeleteFileW(wszFileName);
882 IMalloc_Free(ppM, pidlTestFile);
887 pStrRetToBufW(&strret, pidlTestFile, wszPath, MAX_PATH);
888 ok(0 == lstrcmpW(wszFileName, wszPath),
889 "Desktop->GetDisplayNameOf(pidlTestFile, SHGDN_FORPARSING) "
890 "returned incorrect path for file placed on desktop\n");
893 result = pSHGetPathFromIDListW(pidlTestFile, wszPath);
894 ok(result, "SHGetPathFromIDListW failed! Last error: %u\n", GetLastError());
895 IMalloc_Free(ppM, pidlTestFile);
897 ok(0 == lstrcmpW(wszFileName, wszPath), "SHGetPathFromIDListW returned incorrect path for file placed on desktop\n");
900 /* Test if we can get the path from the start menu "program files" PIDL. */
901 hShell32 = GetModuleHandleA("shell32");
902 pSHGetSpecialFolderLocation = (void *)GetProcAddress(hShell32, "SHGetSpecialFolderLocation");
904 hr = pSHGetSpecialFolderLocation(NULL, CSIDL_PROGRAM_FILES, &pidlPrograms);
905 ok(SUCCEEDED(hr), "SHGetFolderLocation failed: 0x%08x\n", hr);
907 SetLastError(0xdeadbeef);
908 result = pSHGetPathFromIDListW(pidlPrograms, wszPath);
909 IMalloc_Free(ppM, pidlPrograms);
910 ok(result, "SHGetPathFromIDListW failed\n");
913 static void test_EnumObjects_and_CompareIDs(void)
916 IShellFolder *IDesktopFolder, *testIShellFolder;
917 char cCurrDirA [MAX_PATH] = {0};
918 WCHAR cCurrDirW [MAX_PATH];
919 static const WCHAR cTestDirW[] = {'\\','t','e','s','t','d','i','r',0};
923 GetCurrentDirectoryA(MAX_PATH, cCurrDirA);
924 len = lstrlenA(cCurrDirA);
927 trace("GetCurrentDirectoryA returned empty string. Skipping test_EnumObjects_and_CompareIDs\n");
930 if(cCurrDirA[len-1] == '\\')
931 cCurrDirA[len-1] = 0;
933 MultiByteToWideChar(CP_ACP, 0, cCurrDirA, -1, cCurrDirW, MAX_PATH);
934 lstrcatW(cCurrDirW, cTestDirW);
936 hr = SHGetDesktopFolder(&IDesktopFolder);
937 ok(hr == S_OK, "SHGetDesktopfolder failed %08x\n", hr);
939 CreateFilesFolders();
941 hr = IShellFolder_ParseDisplayName(IDesktopFolder, NULL, NULL, cCurrDirW, NULL, &newPIDL, 0);
942 ok(hr == S_OK, "ParseDisplayName failed %08x\n", hr);
944 hr = IShellFolder_BindToObject(IDesktopFolder, newPIDL, NULL, (REFIID)&IID_IShellFolder, (LPVOID *)&testIShellFolder);
945 ok(hr == S_OK, "BindToObject failed %08x\n", hr);
947 test_EnumObjects(testIShellFolder);
949 IShellFolder_Release(testIShellFolder);
953 IMalloc_Free(ppM, newPIDL);
955 IShellFolder_Release(IDesktopFolder);
958 /* A simple implementation of an IPropertyBag, which returns fixed values for
959 * 'Target' and 'Attributes' properties.
961 static HRESULT WINAPI InitPropertyBag_IPropertyBag_QueryInterface(IPropertyBag *iface, REFIID riid,
967 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IPropertyBag, riid)) {
970 ok (FALSE, "InitPropertyBag asked for unknown interface!\n");
971 return E_NOINTERFACE;
974 IPropertyBag_AddRef(iface);
978 static ULONG WINAPI InitPropertyBag_IPropertyBag_AddRef(IPropertyBag *iface) {
982 static ULONG WINAPI InitPropertyBag_IPropertyBag_Release(IPropertyBag *iface) {
986 static HRESULT WINAPI InitPropertyBag_IPropertyBag_Read(IPropertyBag *iface, LPCOLESTR pszPropName,
987 VARIANT *pVar, IErrorLog *pErrorLog)
989 static const WCHAR wszTargetSpecialFolder[] = {
990 'T','a','r','g','e','t','S','p','e','c','i','a','l','F','o','l','d','e','r',0 };
991 static const WCHAR wszTarget[] = {
992 'T','a','r','g','e','t',0 };
993 static const WCHAR wszAttributes[] = {
994 'A','t','t','r','i','b','u','t','e','s',0 };
995 static const WCHAR wszResolveLinkFlags[] = {
996 'R','e','s','o','l','v','e','L','i','n','k','F','l','a','g','s',0 };
998 if (!lstrcmpW(pszPropName, wszTargetSpecialFolder)) {
999 ok(V_VT(pVar) == VT_I4, "Wrong variant type for 'TargetSpecialFolder' property!\n");
1000 return E_INVALIDARG;
1003 if (!lstrcmpW(pszPropName, wszResolveLinkFlags))
1005 ok(V_VT(pVar) == VT_UI4, "Wrong variant type for 'ResolveLinkFlags' property!\n");
1006 return E_INVALIDARG;
1009 if (!lstrcmpW(pszPropName, wszTarget)) {
1010 WCHAR wszPath[MAX_PATH];
1013 ok(V_VT(pVar) == VT_BSTR, "Wrong variant type for 'Target' property!\n");
1014 if (V_VT(pVar) != VT_BSTR) return E_INVALIDARG;
1016 result = pSHGetSpecialFolderPathW(NULL, wszPath, CSIDL_DESKTOPDIRECTORY, FALSE);
1017 ok(result, "SHGetSpecialFolderPathW(DESKTOPDIRECTORY) failed! %u\n", GetLastError());
1018 if (!result) return E_INVALIDARG;
1020 V_BSTR(pVar) = SysAllocString(wszPath);
1024 if (!lstrcmpW(pszPropName, wszAttributes)) {
1025 ok(V_VT(pVar) == VT_UI4, "Wrong variant type for 'Attributes' property!\n");
1026 if (V_VT(pVar) != VT_UI4) return E_INVALIDARG;
1027 V_UI4(pVar) = SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR|
1028 SFGAO_CANRENAME|SFGAO_FILESYSTEM;
1032 ok(FALSE, "PropertyBag was asked for unknown property (vt=%d)!\n", V_VT(pVar));
1033 return E_INVALIDARG;
1036 static HRESULT WINAPI InitPropertyBag_IPropertyBag_Write(IPropertyBag *iface, LPCOLESTR pszPropName,
1039 ok(FALSE, "Unexpected call to IPropertyBag_Write\n");
1043 static const IPropertyBagVtbl InitPropertyBag_IPropertyBagVtbl = {
1044 InitPropertyBag_IPropertyBag_QueryInterface,
1045 InitPropertyBag_IPropertyBag_AddRef,
1046 InitPropertyBag_IPropertyBag_Release,
1047 InitPropertyBag_IPropertyBag_Read,
1048 InitPropertyBag_IPropertyBag_Write
1051 static struct IPropertyBag InitPropertyBag = {
1052 &InitPropertyBag_IPropertyBagVtbl
1055 static void test_FolderShortcut(void) {
1056 IPersistPropertyBag *pPersistPropertyBag;
1057 IShellFolder *pShellFolder, *pDesktopFolder;
1058 IPersistFolder3 *pPersistFolder3;
1061 WCHAR wszDesktopPath[MAX_PATH], wszBuffer[MAX_PATH];
1064 LPITEMIDLIST pidlCurrentFolder, pidlWineTestFolder, pidlSubFolder;
1066 WCHAR wszWineTestFolder[] = {
1067 ':',':','{','9','B','3','5','2','E','B','F','-','2','7','6','5','-','4','5','C','1','-',
1068 'B','4','C','6','-','8','5','C','C','7','F','7','A','B','C','6','4','}',0 };
1069 WCHAR wszShellExtKey[] = { 'S','o','f','t','w','a','r','e','\\',
1070 'M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\',
1071 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
1072 'E','x','p','l','o','r','e','r','\\','D','e','s','k','t','o','p','\\',
1073 'N','a','m','e','S','p','a','c','e','\\',
1074 '{','9','b','3','5','2','e','b','f','-','2','7','6','5','-','4','5','c','1','-',
1075 'b','4','c','6','-','8','5','c','c','7','f','7','a','b','c','6','4','}',0 };
1077 WCHAR wszSomeSubFolder[] = { 'S','u','b','F','o','l','d','e','r', 0};
1078 static const GUID CLSID_UnixDosFolder =
1079 {0x9d20aae8, 0x0625, 0x44b0, {0x9c, 0xa7, 0x71, 0x88, 0x9c, 0x22, 0x54, 0xd9}};
1081 if (!pSHGetSpecialFolderPathW || !pStrRetToBufW) return;
1083 /* These tests basically show, that CLSID_FolderShortcuts are initialized
1084 * via their IPersistPropertyBag interface. And that the target folder
1085 * is taken from the IPropertyBag's 'Target' property.
1087 hr = CoCreateInstance(&CLSID_FolderShortcut, NULL, CLSCTX_INPROC_SERVER,
1088 &IID_IPersistPropertyBag, (LPVOID*)&pPersistPropertyBag);
1089 ok (SUCCEEDED(hr), "CoCreateInstance failed! hr = 0x%08x\n", hr);
1090 if (FAILED(hr)) return;
1092 hr = IPersistPropertyBag_Load(pPersistPropertyBag, &InitPropertyBag, NULL);
1093 ok(SUCCEEDED(hr), "IPersistPropertyBag_Load failed! hr = %08x\n", hr);
1095 IPersistPropertyBag_Release(pPersistPropertyBag);
1099 hr = IPersistPropertyBag_QueryInterface(pPersistPropertyBag, &IID_IShellFolder,
1100 (LPVOID*)&pShellFolder);
1101 IPersistPropertyBag_Release(pPersistPropertyBag);
1102 ok(SUCCEEDED(hr), "IPersistPropertyBag_QueryInterface(IShellFolder) failed! hr = %08x\n", hr);
1103 if (FAILED(hr)) return;
1105 hr = IShellFolder_GetDisplayNameOf(pShellFolder, NULL, SHGDN_FORPARSING, &strret);
1106 ok(SUCCEEDED(hr), "IShellFolder_GetDisplayNameOf(NULL) failed! hr = %08x\n", hr);
1108 IShellFolder_Release(pShellFolder);
1112 result = pSHGetSpecialFolderPathW(NULL, wszDesktopPath, CSIDL_DESKTOPDIRECTORY, FALSE);
1113 ok(result, "SHGetSpecialFolderPathW(CSIDL_DESKTOPDIRECTORY) failed! %u\n", GetLastError());
1114 if (!result) return;
1116 pStrRetToBufW(&strret, NULL, wszBuffer, MAX_PATH);
1117 ok(!lstrcmpiW(wszDesktopPath, wszBuffer), "FolderShortcut returned incorrect folder!\n");
1119 hr = IShellFolder_QueryInterface(pShellFolder, &IID_IPersistFolder3, (LPVOID*)&pPersistFolder3);
1120 IShellFolder_Release(pShellFolder);
1121 ok(SUCCEEDED(hr), "IShellFolder_QueryInterface(IID_IPersistFolder3 failed! hr = 0x%08x\n", hr);
1122 if (FAILED(hr)) return;
1124 hr = IPersistFolder3_GetClassID(pPersistFolder3, &clsid);
1125 ok(SUCCEEDED(hr), "IPersistFolder3_GetClassID failed! hr=0x%08x\n", hr);
1126 ok(IsEqualCLSID(&clsid, &CLSID_FolderShortcut), "Unexpected CLSID!\n");
1128 hr = IPersistFolder3_GetCurFolder(pPersistFolder3, &pidlCurrentFolder);
1129 ok(SUCCEEDED(hr), "IPersistFolder3_GetCurFolder failed! hr=0x%08x\n", hr);
1130 ok(!pidlCurrentFolder, "IPersistFolder3_GetCurFolder should return a NULL pidl!\n");
1132 /* For FolderShortcut objects, the Initialize method initialized the folder's position in the
1133 * shell namespace. The target folder, read from the property bag above, remains untouched.
1134 * The following tests show this: The itemidlist for some imaginary shellfolder object
1135 * is created and the FolderShortcut is initialized with it. GetCurFolder now returns this
1136 * itemidlist, but GetDisplayNameOf still returns the path from above.
1138 hr = SHGetDesktopFolder(&pDesktopFolder);
1139 ok (SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08x\n", hr);
1140 if (FAILED(hr)) return;
1142 /* Temporarily register WineTestFolder as a shell namespace extension at the Desktop.
1143 * Otherwise ParseDisplayName fails on WinXP with E_INVALIDARG */
1144 RegCreateKeyW(HKEY_CURRENT_USER, wszShellExtKey, &hShellExtKey);
1145 RegCloseKey(hShellExtKey);
1146 hr = IShellFolder_ParseDisplayName(pDesktopFolder, NULL, NULL, wszWineTestFolder, NULL,
1147 &pidlWineTestFolder, NULL);
1148 RegDeleteKeyW(HKEY_CURRENT_USER, wszShellExtKey);
1149 IShellFolder_Release(pDesktopFolder);
1150 ok (SUCCEEDED(hr), "IShellFolder::ParseDisplayName failed! hr = %08x\n", hr);
1151 if (FAILED(hr)) return;
1153 hr = IPersistFolder3_Initialize(pPersistFolder3, pidlWineTestFolder);
1154 ok (SUCCEEDED(hr), "IPersistFolder3::Initialize failed! hr = %08x\n", hr);
1156 IPersistFolder3_Release(pPersistFolder3);
1157 pILFree(pidlWineTestFolder);
1161 hr = IPersistFolder3_GetCurFolder(pPersistFolder3, &pidlCurrentFolder);
1162 ok(SUCCEEDED(hr), "IPersistFolder3_GetCurFolder failed! hr=0x%08x\n", hr);
1163 ok(pILIsEqual(pidlCurrentFolder, pidlWineTestFolder),
1164 "IPersistFolder3_GetCurFolder should return pidlWineTestFolder!\n");
1165 pILFree(pidlCurrentFolder);
1166 pILFree(pidlWineTestFolder);
1168 hr = IPersistFolder3_QueryInterface(pPersistFolder3, &IID_IShellFolder, (LPVOID*)&pShellFolder);
1169 IPersistFolder3_Release(pPersistFolder3);
1170 ok(SUCCEEDED(hr), "IPersistFolder3_QueryInterface(IShellFolder) failed! hr = %08x\n", hr);
1171 if (FAILED(hr)) return;
1173 hr = IShellFolder_GetDisplayNameOf(pShellFolder, NULL, SHGDN_FORPARSING, &strret);
1174 ok(SUCCEEDED(hr), "IShellFolder_GetDisplayNameOf(NULL) failed! hr = %08x\n", hr);
1176 IShellFolder_Release(pShellFolder);
1180 pStrRetToBufW(&strret, NULL, wszBuffer, MAX_PATH);
1181 ok(!lstrcmpiW(wszDesktopPath, wszBuffer), "FolderShortcut returned incorrect folder!\n");
1183 /* Next few lines are meant to show that children of FolderShortcuts are not FolderShortcuts,
1184 * but ShellFSFolders. */
1185 myPathAddBackslashW(wszDesktopPath);
1186 lstrcatW(wszDesktopPath, wszSomeSubFolder);
1187 if (!CreateDirectoryW(wszDesktopPath, NULL)) {
1188 IShellFolder_Release(pShellFolder);
1192 hr = IShellFolder_ParseDisplayName(pShellFolder, NULL, NULL, wszSomeSubFolder, NULL,
1193 &pidlSubFolder, NULL);
1194 RemoveDirectoryW(wszDesktopPath);
1195 ok (SUCCEEDED(hr), "IShellFolder::ParseDisplayName failed! hr = %08x\n", hr);
1197 IShellFolder_Release(pShellFolder);
1201 hr = IShellFolder_BindToObject(pShellFolder, pidlSubFolder, NULL, &IID_IPersistFolder3,
1202 (LPVOID*)&pPersistFolder3);
1203 IShellFolder_Release(pShellFolder);
1204 pILFree(pidlSubFolder);
1205 ok (SUCCEEDED(hr), "IShellFolder::BindToObject failed! hr = %08x\n", hr);
1209 /* On windows, we expect CLSID_ShellFSFolder. On wine we relax this constraint
1210 * a little bit and also allow CLSID_UnixDosFolder. */
1211 hr = IPersistFolder3_GetClassID(pPersistFolder3, &clsid);
1212 ok(SUCCEEDED(hr), "IPersistFolder3_GetClassID failed! hr=0x%08x\n", hr);
1213 ok(IsEqualCLSID(&clsid, &CLSID_ShellFSFolder) || IsEqualCLSID(&clsid, &CLSID_UnixDosFolder),
1214 "IPersistFolder3::GetClassID returned unexpected CLSID!\n");
1216 IPersistFolder3_Release(pPersistFolder3);
1219 #include "pshpack1.h"
1220 struct FileStructA {
1224 WORD uFileDate; /* In our current implementation this is */
1225 WORD uFileTime; /* FileTimeToDosDate(WIN32_FIND_DATA->ftLastWriteTime) */
1230 struct FileStructW {
1231 WORD cbLen; /* Length of this element. */
1232 BYTE abFooBar1[6]; /* Beyond any recognition. */
1233 WORD uDate; /* FileTimeToDosDate(WIN32_FIND_DATA->ftCreationTime)? */
1234 WORD uTime; /* (this is currently speculation) */
1235 WORD uDate2; /* FileTimeToDosDate(WIN32_FIND_DATA->ftLastAccessTime)? */
1236 WORD uTime2; /* (this is currently speculation) */
1237 BYTE abFooBar2[4]; /* Beyond any recognition. */
1238 WCHAR wszName[1]; /* The long filename in unicode. */
1239 /* Just for documentation: Right after the unicode string: */
1240 WORD cbOffset; /* FileStructW's offset from the beginning of the SHITMEID.
1241 * SHITEMID->cb == uOffset + cbLen */
1243 #include "poppack.h"
1245 static void test_ITEMIDLIST_format(void) {
1246 WCHAR wszPersonal[MAX_PATH];
1247 LPSHELLFOLDER psfDesktop, psfPersonal;
1248 LPITEMIDLIST pidlPersonal, pidlFile;
1252 WCHAR wszFile[3][17] = { { 'e','v','e','n','_',0 }, { 'o','d','d','_',0 },
1253 { 'l','o','n','g','e','r','_','t','h','a','n','.','8','_','3',0 } };
1256 if(!pSHGetSpecialFolderPathW) return;
1258 bResult = pSHGetSpecialFolderPathW(NULL, wszPersonal, CSIDL_PERSONAL, FALSE);
1259 ok(bResult, "SHGetSpecialFolderPathW failed! Last error: %u\n", GetLastError());
1260 if (!bResult) return;
1262 bResult = SetCurrentDirectoryW(wszPersonal);
1263 ok(bResult, "SetCurrentDirectory failed! Last error: %u\n", GetLastError());
1264 if (!bResult) return;
1266 hr = SHGetDesktopFolder(&psfDesktop);
1267 ok(SUCCEEDED(hr), "SHGetDesktopFolder failed! hr: %08x\n", hr);
1268 if (FAILED(hr)) return;
1270 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszPersonal, NULL, &pidlPersonal, NULL);
1271 ok(SUCCEEDED(hr), "psfDesktop->ParseDisplayName failed! hr = %08x\n", hr);
1273 IShellFolder_Release(psfDesktop);
1277 hr = IShellFolder_BindToObject(psfDesktop, pidlPersonal, NULL, &IID_IShellFolder,
1278 (LPVOID*)&psfPersonal);
1279 IShellFolder_Release(psfDesktop);
1280 pILFree(pidlPersonal);
1281 ok(SUCCEEDED(hr), "psfDesktop->BindToObject failed! hr = %08x\n", hr);
1282 if (FAILED(hr)) return;
1284 for (i=0; i<3; i++) {
1285 CHAR szFile[MAX_PATH];
1286 struct FileStructA *pFileStructA;
1289 WideCharToMultiByte(CP_ACP, 0, wszFile[i], -1, szFile, MAX_PATH, NULL, NULL);
1291 hFile = CreateFileW(wszFile[i], GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_FLAG_WRITE_THROUGH, NULL);
1292 ok(hFile != INVALID_HANDLE_VALUE, "CreateFile failed! (%u)\n", GetLastError());
1293 if (hFile == INVALID_HANDLE_VALUE) {
1294 IShellFolder_Release(psfPersonal);
1299 hr = IShellFolder_ParseDisplayName(psfPersonal, NULL, NULL, wszFile[i], NULL, &pidlFile, NULL);
1300 DeleteFileW(wszFile[i]);
1301 ok(SUCCEEDED(hr), "psfPersonal->ParseDisplayName failed! hr: %08x\n", hr);
1303 IShellFolder_Release(psfPersonal);
1307 pFileStructA = (struct FileStructA *)pidlFile->mkid.abID;
1308 ok(pFileStructA->type == 0x32, "PIDLTYPE should be 0x32!\n");
1309 ok(pFileStructA->dummy == 0x00, "Dummy Byte should be 0x00!\n");
1310 ok(pFileStructA->dwFileSize == 0, "Filesize should be zero!\n");
1312 if (i < 2) /* First two file names are already in valid 8.3 format */
1313 ok(!strcmp(szFile, (CHAR*)&pidlFile->mkid.abID[12]), "Wrong file name!\n");
1315 /* WinXP stores a derived 8.3 dos name (LONGER~1.8_3) here. We probably
1316 * can't implement this correctly, since unix filesystems don't support
1317 * this nasty short/long filename stuff. So we'll probably stay with our
1318 * current habbit of storing the long filename here, which seems to work
1320 todo_wine { ok(pidlFile->mkid.abID[18] == '~', "Should be derived 8.3 name!\n"); }
1322 if (i == 0) /* First file name has an even number of chars. No need for alignment. */
1323 ok(pidlFile->mkid.abID[12 + strlen(szFile) + 1] != '\0',
1324 "Alignment byte, where there shouldn't be!\n");
1326 if (i == 1) /* Second file name has an uneven number of chars => alignment byte */
1327 ok(pidlFile->mkid.abID[12 + strlen(szFile) + 1] == '\0',
1328 "There should be an alignment byte, but isn't!\n");
1330 /* The offset of the FileStructW member is stored as a WORD at the end of the pidl. */
1331 cbOffset = *(WORD*)(((LPBYTE)pidlFile)+pidlFile->mkid.cb-sizeof(WORD));
1332 ok (cbOffset >= sizeof(struct FileStructA) &&
1333 cbOffset <= pidlFile->mkid.cb - sizeof(struct FileStructW),
1334 "Wrong offset value (%d) stored at the end of the PIDL\n", cbOffset);
1336 if (cbOffset >= sizeof(struct FileStructA) &&
1337 cbOffset <= pidlFile->mkid.cb - sizeof(struct FileStructW))
1339 struct FileStructW *pFileStructW = (struct FileStructW *)(((LPBYTE)pidlFile)+cbOffset);
1341 ok(pidlFile->mkid.cb == cbOffset + pFileStructW->cbLen,
1342 "FileStructW's offset and length should add up to the PIDL's length!\n");
1344 if (pidlFile->mkid.cb == cbOffset + pFileStructW->cbLen) {
1345 /* Since we just created the file, time of creation,
1346 * time of last access and time of last write access just be the same.
1347 * These tests seem to fail sometimes (on WinXP), if the test is run again shortly
1348 * after the first run. I do remember something with NTFS keeping the creation time
1349 * if a file is deleted and then created again within a couple of seconds or so.
1350 * Might be the reason. */
1351 ok (pFileStructA->uFileDate == pFileStructW->uDate &&
1352 pFileStructA->uFileTime == pFileStructW->uTime,
1353 "Last write time should match creation time!\n");
1355 ok (pFileStructA->uFileDate == pFileStructW->uDate2 &&
1356 pFileStructA->uFileTime == pFileStructW->uTime2,
1357 "Last write time should match last access time!\n");
1359 ok (!lstrcmpW(wszFile[i], pFileStructW->wszName),
1360 "The filename should be stored in unicode at this position!\n");
1368 START_TEST(shlfolder)
1370 init_function_pointers();
1371 /* if OleInitialize doesn't get called, ParseDisplayName returns
1372 CO_E_NOTINITIALIZED for malformed directory names on win2k. */
1373 OleInitialize(NULL);
1375 test_ParseDisplayName();
1376 test_BindToObject();
1377 test_EnumObjects_and_CompareIDs();
1378 test_GetDisplayName();
1379 test_GetAttributesOf();
1380 test_SHGetPathFromIDList();
1381 test_CallForAttributes();
1382 test_FolderShortcut();
1383 test_ITEMIDLIST_format();