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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
38 #include "wine/unicode.h"
39 #include "wine/test.h"
44 static HRESULT (WINAPI *pSHBindToParent)(LPCITEMIDLIST, REFIID, LPVOID*, LPCITEMIDLIST*);
45 static BOOL (WINAPI *pSHGetSpecialFolderPathW)(HWND, LPWSTR, int, BOOL);
46 static HRESULT (WINAPI *pStrRetToBufW)(STRRET*,LPCITEMIDLIST,LPWSTR,UINT);
48 static void init_function_pointers(void)
53 hmod = GetModuleHandleA("shell32.dll");
56 pSHBindToParent = (void*)GetProcAddress(hmod, "SHBindToParent");
57 pSHGetSpecialFolderPathW = (void*)GetProcAddress(hmod, "SHGetSpecialFolderPathW");
60 hmod = GetModuleHandleA("shlwapi.dll");
63 pStrRetToBufW = (void*)GetProcAddress(hmod, "StrRetToBufW");
66 hr = SHGetMalloc(&ppM);
67 ok(hr == S_OK, "SHGetMalloc failed %08lx\n", hr);
70 static void test_ParseDisplayName(void)
73 IShellFolder *IDesktopFolder;
74 static const char *cNonExistDir1A = "c:\\nonexist_subdir";
75 static const char *cNonExistDir2A = "c:\\\\nonexist_subdir";
77 WCHAR cTestDirW [MAX_PATH] = {0};
80 hr = SHGetDesktopFolder(&IDesktopFolder);
81 if(hr != S_OK) return;
83 res = GetFileAttributesA(cNonExistDir1A);
84 if(res != INVALID_FILE_ATTRIBUTES) return;
86 MultiByteToWideChar(CP_ACP, 0, cNonExistDir1A, -1, cTestDirW, MAX_PATH);
87 hr = IShellFolder_ParseDisplayName(IDesktopFolder,
88 NULL, NULL, cTestDirW, NULL, &newPIDL, 0);
89 ok((hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) || (hr == E_FAIL),
90 "ParseDisplayName returned %08lx, expected 80070002 or E_FAIL\n", hr);
92 res = GetFileAttributesA(cNonExistDir2A);
93 if(res != INVALID_FILE_ATTRIBUTES) return;
95 MultiByteToWideChar(CP_ACP, 0, cNonExistDir2A, -1, cTestDirW, MAX_PATH);
96 hr = IShellFolder_ParseDisplayName(IDesktopFolder,
97 NULL, NULL, cTestDirW, NULL, &newPIDL, 0);
98 ok((hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) || (hr == E_FAIL) || (hr == E_INVALIDARG),
99 "ParseDisplayName returned %08lx, expected 80070002, E_FAIL or E_INVALIDARG\n", hr);
102 /* creates a file with the specified name for tests */
103 static void CreateTestFile(const CHAR *name)
108 file = CreateFileA(name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
109 if (file != INVALID_HANDLE_VALUE)
111 WriteFile(file, name, strlen(name), &written, NULL);
112 WriteFile(file, "\n", strlen("\n"), &written, NULL);
118 /* initializes the tests */
119 static void CreateFilesFolders(void)
121 CreateDirectoryA(".\\testdir", NULL);
122 CreateDirectoryA(".\\testdir\\test.txt", NULL);
123 CreateTestFile (".\\testdir\\test1.txt ");
124 CreateTestFile (".\\testdir\\test2.txt ");
125 CreateTestFile (".\\testdir\\test3.txt ");
126 CreateDirectoryA(".\\testdir\\testdir2 ", NULL);
127 CreateDirectoryA(".\\testdir\\testdir2\\subdir", NULL);
130 /* cleans after tests */
131 static void Cleanup(void)
133 DeleteFileA(".\\testdir\\test1.txt");
134 DeleteFileA(".\\testdir\\test2.txt");
135 DeleteFileA(".\\testdir\\test3.txt");
136 RemoveDirectoryA(".\\testdir\\test.txt");
137 RemoveDirectoryA(".\\testdir\\testdir2\\subdir");
138 RemoveDirectoryA(".\\testdir\\testdir2");
139 RemoveDirectoryA(".\\testdir");
144 static void test_EnumObjects(IShellFolder *iFolder)
146 IEnumIDList *iEnumList;
147 LPITEMIDLIST newPIDL, idlArr[10];
152 static const WORD iResults [5][5] =
161 /* Just test SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR for now */
162 static const ULONG attrs[5] =
164 SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR,
165 SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR,
171 hr = IShellFolder_EnumObjects(iFolder, NULL, SHCONTF_FOLDERS | SHCONTF_NONFOLDERS | SHCONTF_INCLUDEHIDDEN, &iEnumList);
172 ok(hr == S_OK, "EnumObjects failed %08lx\n", hr);
174 /* This is to show that, contrary to what is said on MSDN, on IEnumIDList::Next,
175 * the filesystem shellfolders return S_OK even if less than 'celt' items are
176 * returned (in contrast to S_FALSE). We have to do it in a loop since WinXP
177 * only ever returns a single entry per call. */
178 while (IEnumIDList_Next(iEnumList, 10-i, &idlArr[i], &NumPIDLs) == S_OK)
180 ok (i == 5, "i: %d\n", i);
182 hr = IEnumIDList_Release(iEnumList);
183 ok(hr == S_OK, "IEnumIDList_Release failed %08lx\n", hr);
185 /* Sort them first in case of wrong order from system */
186 for (i=0;i<5;i++) for (j=0;j<5;j++)
187 if ((SHORT)IShellFolder_CompareIDs(iFolder, 0, idlArr[i], idlArr[j]) < 0)
190 idlArr[i] = idlArr[j];
194 for (i=0;i<5;i++) for (j=0;j<5;j++)
196 hr = IShellFolder_CompareIDs(iFolder, 0, idlArr[i], idlArr[j]);
197 ok(hr == iResults[i][j], "Got %lx expected [%d]-[%d]=%x\n", hr, i, j, iResults[i][j]);
201 for (i = 0; i < 5; i++)
204 flags = SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR;
205 hr = IShellFolder_GetAttributesOf(iFolder, 1, (LPCITEMIDLIST*)(idlArr + i), &flags);
206 flags &= SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR;
207 ok(hr == S_OK, "GetAttributesOf returns %08lx\n", hr);
208 ok(flags == attrs[i], "GetAttributesOf gets attrs %08lx, expects %08lx\n", flags, attrs[i]);
212 IMalloc_Free(ppM, idlArr[i]);
215 static void test_BindToObject(void)
219 IShellFolder *psfDesktop, *psfChild, *psfMyComputer, *psfSystemDir;
220 SHITEMID emptyitem = { 0, { 0 } };
221 LPITEMIDLIST pidlMyComputer, pidlSystemDir, pidlEmpty = (LPITEMIDLIST)&emptyitem;
222 WCHAR wszSystemDir[MAX_PATH];
223 char szSystemDir[MAX_PATH];
224 WCHAR wszMyComputer[] = {
225 ':',':','{','2','0','D','0','4','F','E','0','-','3','A','E','A','-','1','0','6','9','-',
226 'A','2','D','8','-','0','8','0','0','2','B','3','0','3','0','9','D','}',0 };
228 /* The following tests shows that BindToObject should fail with E_INVALIDARG if called
229 * with an empty pidl. This is tested for Desktop, MyComputer and the FS ShellFolder
231 hr = SHGetDesktopFolder(&psfDesktop);
232 ok (SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08lx\n", hr);
233 if (FAILED(hr)) return;
235 hr = IShellFolder_BindToObject(psfDesktop, pidlEmpty, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
236 ok (hr == E_INVALIDARG, "Desktop's BindToObject should fail, when called with empty pidl! hr = %08lx\n", hr);
238 hr = IShellFolder_BindToObject(psfDesktop, NULL, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
239 ok (hr == E_INVALIDARG, "Desktop's BindToObject should fail, when called with NULL pidl! hr = %08lx\n", hr);
241 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszMyComputer, NULL, &pidlMyComputer, NULL);
242 ok (SUCCEEDED(hr), "Desktop's ParseDisplayName failed to parse MyComputer's CLSID! hr = %08lx\n", hr);
244 IShellFolder_Release(psfDesktop);
248 hr = IShellFolder_BindToObject(psfDesktop, pidlMyComputer, NULL, &IID_IShellFolder, (LPVOID*)&psfMyComputer);
249 ok (SUCCEEDED(hr), "Desktop failed to bind to MyComputer object! hr = %08lx\n", hr);
250 IShellFolder_Release(psfDesktop);
251 IMalloc_Free(ppM, pidlMyComputer);
252 if (FAILED(hr)) return;
254 hr = IShellFolder_BindToObject(psfMyComputer, pidlEmpty, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
255 ok (hr == E_INVALIDARG, "MyComputers's BindToObject should fail, when called with empty pidl! hr = %08lx\n", hr);
257 hr = IShellFolder_BindToObject(psfMyComputer, NULL, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
258 ok (hr == E_INVALIDARG, "MyComputers's BindToObject should fail, when called with NULL pidl! hr = %08lx\n", hr);
260 cChars = GetSystemDirectoryA(szSystemDir, MAX_PATH);
261 ok (cChars > 0 && cChars < MAX_PATH, "GetSystemDirectoryA failed! LastError: %08lx\n", GetLastError());
262 if (cChars == 0 || cChars >= MAX_PATH) {
263 IShellFolder_Release(psfMyComputer);
266 MultiByteToWideChar(CP_ACP, 0, szSystemDir, -1, wszSystemDir, MAX_PATH);
268 hr = IShellFolder_ParseDisplayName(psfMyComputer, NULL, NULL, wszSystemDir, NULL, &pidlSystemDir, NULL);
269 ok (SUCCEEDED(hr), "MyComputers's ParseDisplayName failed to parse the SystemDirectory! hr = %08lx\n", hr);
271 IShellFolder_Release(psfMyComputer);
275 hr = IShellFolder_BindToObject(psfMyComputer, pidlSystemDir, NULL, &IID_IShellFolder, (LPVOID*)&psfSystemDir);
276 ok (SUCCEEDED(hr), "MyComputer failed to bind to a FileSystem ShellFolder! hr = %08lx\n", hr);
277 IShellFolder_Release(psfMyComputer);
278 IMalloc_Free(ppM, pidlSystemDir);
279 if (FAILED(hr)) return;
281 hr = IShellFolder_BindToObject(psfSystemDir, pidlEmpty, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
282 ok (hr == E_INVALIDARG,
283 "FileSystem ShellFolder's BindToObject should fail, when called with empty pidl! hr = %08lx\n", hr);
285 hr = IShellFolder_BindToObject(psfSystemDir, NULL, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
286 ok (hr == E_INVALIDARG,
287 "FileSystem ShellFolder's BindToObject should fail, when called with NULL pidl! hr = %08lx\n", hr);
289 IShellFolder_Release(psfSystemDir);
292 static void test_GetDisplayName(void)
297 WCHAR wszTestFile[MAX_PATH], wszTestFile2[MAX_PATH], wszTestDir[MAX_PATH];
298 char szTestFile[MAX_PATH], szTestDir[MAX_PATH];
300 LPSHELLFOLDER psfDesktop, psfPersonal;
302 LPITEMIDLIST pidlTestFile;
303 LPCITEMIDLIST pidlLast;
304 static const WCHAR wszFileName[] = { 'w','i','n','e','t','e','s','t','.','f','o','o',0 };
305 static const WCHAR wszDirName[] = { 'w','i','n','e','t','e','s','t',0 };
307 /* I'm trying to figure if there is a functional difference between calling
308 * SHGetPathFromIDList and calling GetDisplayNameOf(SHGDN_FORPARSING) after
309 * binding to the shellfolder. One thing I thought of was that perhaps
310 * SHGetPathFromIDList would be able to get the path to a file, which does
311 * not exist anymore, while the other method would'nt. It turns out there's
312 * no functional difference in this respect.
315 if(!pSHGetSpecialFolderPathW) return;
317 /* First creating a directory in MyDocuments and a file in this directory. */
318 result = pSHGetSpecialFolderPathW(NULL, wszTestDir, CSIDL_PERSONAL, FALSE);
319 ok(result, "SHGetSpecialFolderPathW failed! Last error: %08lx\n", GetLastError());
322 PathAddBackslashW(wszTestDir);
323 lstrcatW(wszTestDir, wszDirName);
324 WideCharToMultiByte(CP_ACP, 0, wszTestDir, -1, szTestDir, MAX_PATH, 0, 0);
325 result = CreateDirectoryA(szTestDir, NULL);
326 ok(result, "CreateDirectoryA failed! Last error: %08lx\n", GetLastError());
329 lstrcpyW(wszTestFile, wszTestDir);
330 PathAddBackslashW(wszTestFile);
331 lstrcatW(wszTestFile, wszFileName);
332 WideCharToMultiByte(CP_ACP, 0, wszTestFile, -1, szTestFile, MAX_PATH, 0, 0);
334 hTestFile = CreateFileA(szTestFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
335 ok((hTestFile != INVALID_HANDLE_VALUE), "CreateFileA failed! Last error: %08lx\n", GetLastError());
336 if (hTestFile == INVALID_HANDLE_VALUE) return;
337 CloseHandle(hTestFile);
339 /* Getting an itemidlist for the file. */
340 hr = SHGetDesktopFolder(&psfDesktop);
341 ok(SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08lx\n", hr);
342 if (FAILED(hr)) return;
344 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszTestFile, NULL, &pidlTestFile, NULL);
345 ok(SUCCEEDED(hr), "Desktop->ParseDisplayName failed! hr = %08lx\n", hr);
347 IShellFolder_Release(psfDesktop);
351 /* It seems as if we cannot bind to regular files on windows, but only directories.
353 hr = IShellFolder_BindToObject(psfDesktop, pidlTestFile, NULL, &IID_IUnknown, (VOID**)&psfFile);
354 todo_wine { ok (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), "hr = %08lx\n", hr); }
356 IShellFolder_Release(psfFile);
359 /* Deleting the file and the directory */
360 DeleteFileA(szTestFile);
361 RemoveDirectoryA(szTestDir);
363 /* SHGetPathFromIDListW still works, although the file is not present anymore. */
364 result = SHGetPathFromIDListW(pidlTestFile, wszTestFile2);
365 ok (result, "SHGetPathFromIDListW failed! Last error: %08lx\n", GetLastError());
366 ok (!lstrcmpiW(wszTestFile, wszTestFile2), "SHGetPathFromIDListW returns incorrect path!\n");
368 if(!pSHBindToParent) return;
370 /* Binding to the folder and querying the display name of the file also works. */
371 hr = pSHBindToParent(pidlTestFile, &IID_IShellFolder, (VOID**)&psfPersonal, &pidlLast);
372 ok (SUCCEEDED(hr), "SHBindToParent failed! hr = %08lx\n", hr);
374 IShellFolder_Release(psfDesktop);
378 hr = IShellFolder_GetDisplayNameOf(psfPersonal, pidlLast, SHGDN_FORPARSING, &strret);
379 ok (SUCCEEDED(hr), "Personal->GetDisplayNameOf failed! hr = %08lx\n", hr);
381 IShellFolder_Release(psfDesktop);
382 IShellFolder_Release(psfPersonal);
388 hr = pStrRetToBufW(&strret, pidlLast, wszTestFile2, MAX_PATH);
389 ok (SUCCEEDED(hr), "StrRetToBufW failed! hr = %08lx\n", hr);
390 ok (!lstrcmpiW(wszTestFile, wszTestFile2), "GetDisplayNameOf returns incorrect path!\n");
393 IShellFolder_Release(psfDesktop);
394 IShellFolder_Release(psfPersonal);
397 static void test_CallForAttributes(void)
403 LPSHELLFOLDER psfDesktop;
404 LPITEMIDLIST pidlMyDocuments;
405 DWORD dwAttributes, dwCallForAttributes, dwOrigAttributes, dwOrigCallForAttributes;
406 static const WCHAR wszAttributes[] = { 'A','t','t','r','i','b','u','t','e','s',0 };
407 static const WCHAR wszCallForAttributes[] = {
408 'C','a','l','l','F','o','r','A','t','t','r','i','b','u','t','e','s',0 };
409 static const WCHAR wszMyDocumentsKey[] = {
410 'C','L','S','I','D','\\','{','4','5','0','D','8','F','B','A','-','A','D','2','5','-',
411 '1','1','D','0','-','9','8','A','8','-','0','8','0','0','3','6','1','B','1','1','0','3','}',
412 '\\','S','h','e','l','l','F','o','l','d','e','r',0 };
413 WCHAR wszMyDocuments[] = {
414 ':',':','{','4','5','0','D','8','F','B','A','-','A','D','2','5','-','1','1','D','0','-',
415 '9','8','A','8','-','0','8','0','0','3','6','1','B','1','1','0','3','}',0 };
417 /* For the root of a namespace extension, the attributes are not queried by binding
418 * to the object and calling GetAttributesOf. Instead, the attributes are read from
419 * the registry value HKCR/CLSID/{...}/ShellFolder/Attributes. This is documented on MSDN.
421 * The MyDocuments shellfolder on WinXP has a HKCR/CLSID/{...}/ShellFolder/CallForAttributes
422 * value. It seems that if the folder is queried for one of the flags set in CallForAttributes,
423 * the shell does bind to the folder object and calls GetAttributesOf. This is not documented
424 * on MSDN. This test is meant to document the observed behaviour on WinXP SP2.
426 hr = SHGetDesktopFolder(&psfDesktop);
427 ok (SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08lx\n", hr);
428 if (FAILED(hr)) return;
430 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszMyDocuments, NULL,
431 &pidlMyDocuments, NULL);
433 "Desktop's ParseDisplayName failed to parse MyDocuments's CLSID! hr = %08lx\n", hr);
435 IShellFolder_Release(psfDesktop);
439 dwAttributes = 0xffffffff;
440 hr = IShellFolder_GetAttributesOf(psfDesktop, 1,
441 (LPCITEMIDLIST*)&pidlMyDocuments, &dwAttributes);
442 ok (SUCCEEDED(hr), "Desktop->GetAttributesOf(MyDocuments) failed! hr = %08lx\n", hr);
444 /* We need the following setup (as observed on WinXP SP2), for the tests to make sense. */
445 todo_wine{ ok (dwAttributes & SFGAO_FILESYSTEM,
446 "SFGAO_FILESYSTEM attribute is not set for MyDocuments!\n"); }
447 ok (!(dwAttributes & SFGAO_ISSLOW), "SFGAO_ISSLOW attribute is set for MyDocuments!\n");
448 ok (!(dwAttributes & SFGAO_GHOSTED), "SFGAO_GHOSTED attribute is set for MyDocuments!\n");
450 /* We don't have the MyDocuments shellfolder in wine yet, and thus we don't have the registry
451 * key. So the test will return at this point, if run on wine.
453 lResult = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszMyDocumentsKey, 0, KEY_WRITE|KEY_READ, &hKey);
454 todo_wine { ok (lResult == ERROR_SUCCESS, "RegOpenKeyEx failed! result: %08lx\n", lResult); }
455 if (lResult != ERROR_SUCCESS) {
456 IMalloc_Free(ppM, pidlMyDocuments);
457 IShellFolder_Release(psfDesktop);
461 /* Query MyDocuments' Attributes value, to be able to restore it later. */
462 dwSize = sizeof(DWORD);
463 lResult = RegQueryValueExW(hKey, wszAttributes, NULL, NULL, (LPBYTE)&dwOrigAttributes, &dwSize);
464 ok (lResult == ERROR_SUCCESS, "RegQueryValueEx failed! result: %08lx\n", lResult);
465 if (lResult != ERROR_SUCCESS) {
467 IMalloc_Free(ppM, pidlMyDocuments);
468 IShellFolder_Release(psfDesktop);
472 /* Query MyDocuments' CallForAttributes value, to be able to restore it later. */
473 dwSize = sizeof(DWORD);
474 lResult = RegQueryValueExW(hKey, wszCallForAttributes, NULL, NULL,
475 (LPBYTE)&dwOrigCallForAttributes, &dwSize);
476 ok (lResult == ERROR_SUCCESS, "RegQueryValueEx failed! result: %08lx\n", lResult);
477 if (lResult != ERROR_SUCCESS) {
479 IMalloc_Free(ppM, pidlMyDocuments);
480 IShellFolder_Release(psfDesktop);
484 /* Define via the Attributes value that MyDocuments attributes are SFGAO_ISSLOW and
485 * SFGAO_GHOSTED and that MyDocuments should be called for the SFGAO_ISSLOW and
486 * SFGAO_FILESYSTEM attributes. */
487 dwAttributes = SFGAO_ISSLOW|SFGAO_GHOSTED;
488 RegSetValueExW(hKey, wszAttributes, 0, REG_DWORD, (LPBYTE)&dwAttributes, sizeof(DWORD));
489 dwCallForAttributes = SFGAO_ISSLOW|SFGAO_FILESYSTEM;
490 RegSetValueExW(hKey, wszCallForAttributes, 0, REG_DWORD,
491 (LPBYTE)&dwCallForAttributes, sizeof(DWORD));
493 /* Although it is not set in CallForAttributes, the SFGAO_GHOSTED flag is reset by
494 * GetAttributesOf. It seems that once there is a single attribute queried, for which
495 * CallForAttributes is set, all flags are taken from the GetAttributesOf call and
496 * the flags in Attributes are ignored.
498 dwAttributes = SFGAO_ISSLOW|SFGAO_GHOSTED|SFGAO_FILESYSTEM;
499 hr = IShellFolder_GetAttributesOf(psfDesktop, 1,
500 (LPCITEMIDLIST*)&pidlMyDocuments, &dwAttributes);
501 ok (SUCCEEDED(hr), "Desktop->GetAttributesOf(MyDocuments) failed! hr = %08lx\n", hr);
503 ok (dwAttributes == SFGAO_FILESYSTEM,
504 "Desktop->GetAttributes(MyDocuments) returned unexpected attributes: %08lx\n",
507 /* Restore MyDocuments' original Attributes and CallForAttributes registry values */
508 RegSetValueExW(hKey, wszAttributes, 0, REG_DWORD, (LPBYTE)&dwOrigAttributes, sizeof(DWORD));
509 RegSetValueExW(hKey, wszCallForAttributes, 0, REG_DWORD,
510 (LPBYTE)&dwOrigCallForAttributes, sizeof(DWORD));
512 IMalloc_Free(ppM, pidlMyDocuments);
513 IShellFolder_Release(psfDesktop);
516 static void test_GetAttributesOf(void)
519 LPSHELLFOLDER psfDesktop, psfMyComputer;
520 SHITEMID emptyitem = { 0, { 0 } };
521 LPCITEMIDLIST pidlEmpty = (LPCITEMIDLIST)&emptyitem;
522 LPITEMIDLIST pidlMyComputer;
524 const static DWORD dwDesktopFlags = /* As observed on WinXP SP2 */
525 SFGAO_STORAGE | SFGAO_HASPROPSHEET | SFGAO_STORAGEANCESTOR |
526 SFGAO_FILESYSANCESTOR | SFGAO_FOLDER | SFGAO_FILESYSTEM | SFGAO_HASSUBFOLDER;
527 const static DWORD dwMyComputerFlags = /* As observed on WinXP SP2 */
528 SFGAO_CANRENAME | SFGAO_CANDELETE | SFGAO_HASPROPSHEET |
529 SFGAO_DROPTARGET | SFGAO_FILESYSANCESTOR | SFGAO_FOLDER | SFGAO_HASSUBFOLDER;
530 WCHAR wszMyComputer[] = {
531 ':',':','{','2','0','D','0','4','F','E','0','-','3','A','E','A','-','1','0','6','9','-',
532 'A','2','D','8','-','0','8','0','0','2','B','3','0','3','0','9','D','}',0 };
534 hr = SHGetDesktopFolder(&psfDesktop);
535 ok (SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08lx\n", hr);
536 if (FAILED(hr)) return;
538 /* The Desktop attributes can be queried with a single empty itemidlist, .. */
539 dwFlags = 0xffffffff;
540 hr = IShellFolder_GetAttributesOf(psfDesktop, 1, &pidlEmpty, &dwFlags);
541 ok (SUCCEEDED(hr), "Desktop->GetAttributesOf(empty pidl) failed! hr = %08lx\n", hr);
542 ok (dwFlags == dwDesktopFlags, "Wrong Desktop attributes: %08lx, expected: %08lx\n",
543 dwFlags, dwDesktopFlags);
545 /* .. or with no itemidlist at all. */
546 dwFlags = 0xffffffff;
547 hr = IShellFolder_GetAttributesOf(psfDesktop, 0, NULL, &dwFlags);
548 ok (SUCCEEDED(hr), "Desktop->GetAttributesOf(NULL) failed! hr = %08lx\n", hr);
549 ok (dwFlags == dwDesktopFlags, "Wrong Desktop attributes: %08lx, expected: %08lx\n",
550 dwFlags, dwDesktopFlags);
552 /* Testing the attributes of the MyComputer shellfolder */
553 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszMyComputer, NULL, &pidlMyComputer, NULL);
554 ok (SUCCEEDED(hr), "Desktop's ParseDisplayName failed to parse MyComputer's CLSID! hr = %08lx\n", hr);
556 IShellFolder_Release(psfDesktop);
560 /* WinXP SP2 sets the SFGAO_CANLINK flag, when MyComputer is queried via the Desktop
561 * folder object. It doesn't do this, if MyComputer is queried directly (see below).
562 * SFGAO_CANLINK is the same as DROPEFFECT_LINK, which MSDN says means: "Drag source
563 * should create a link to the original data". You can't create links on MyComputer on
564 * Windows, so this flag shouldn't be set. Seems like a bug in Windows. As long as nobody
565 * depends on this bug, we probably shouldn't imitate it.
567 dwFlags = 0xffffffff;
568 hr = IShellFolder_GetAttributesOf(psfDesktop, 1, (LPCITEMIDLIST*)&pidlMyComputer, &dwFlags);
569 ok (SUCCEEDED(hr), "Desktop->GetAttributesOf(MyComputer) failed! hr = %08lx\n", hr);
570 todo_wine { ok ((dwFlags & ~(DWORD)SFGAO_CANLINK) == dwMyComputerFlags,
571 "Wrong MyComputer attributes: %08lx, expected: %08lx\n", dwFlags, dwMyComputerFlags); }
573 hr = IShellFolder_BindToObject(psfDesktop, pidlMyComputer, NULL, &IID_IShellFolder, (LPVOID*)&psfMyComputer);
574 ok (SUCCEEDED(hr), "Desktop failed to bind to MyComputer object! hr = %08lx\n", hr);
575 IShellFolder_Release(psfDesktop);
576 IMalloc_Free(ppM, pidlMyComputer);
577 if (FAILED(hr)) return;
579 hr = IShellFolder_GetAttributesOf(psfMyComputer, 1, &pidlEmpty, &dwFlags);
580 todo_wine {ok (hr == E_INVALIDARG, "MyComputer->GetAttributesOf(emtpy pidl) should fail! hr = %08lx\n", hr); }
582 dwFlags = 0xffffffff;
583 hr = IShellFolder_GetAttributesOf(psfMyComputer, 0, NULL, &dwFlags);
584 ok (SUCCEEDED(hr), "MyComputer->GetAttributesOf(NULL) failed! hr = %08lx\n", hr);
585 todo_wine { ok (dwFlags == dwMyComputerFlags,
586 "Wrong MyComputer attributes: %08lx, expected: %08lx\n", dwFlags, dwMyComputerFlags); }
588 IShellFolder_Release(psfMyComputer);
591 static void test_SHGetPathFromIDList(void)
593 SHITEMID emptyitem = { 0, { 0 } };
594 LPCITEMIDLIST pidlEmpty = (LPCITEMIDLIST)&emptyitem;
595 LPITEMIDLIST pidlMyComputer;
596 WCHAR wszPath[MAX_PATH], wszDesktop[MAX_PATH];
599 LPSHELLFOLDER psfDesktop;
600 WCHAR wszMyComputer[] = {
601 ':',':','{','2','0','D','0','4','F','E','0','-','3','A','E','A','-','1','0','6','9','-',
602 'A','2','D','8','-','0','8','0','0','2','B','3','0','3','0','9','D','}',0 };
603 WCHAR wszFileName[MAX_PATH];
604 LPITEMIDLIST pidlTestFile;
607 static WCHAR wszTestFile[] = {
608 'w','i','n','e','t','e','s','t','.','f','o','o',0 };
610 if(!pSHGetSpecialFolderPathW) return;
612 /* Calling SHGetPathFromIDList with an empty pidl should return the desktop folder's path. */
613 result = pSHGetSpecialFolderPathW(NULL, wszDesktop, CSIDL_DESKTOP, FALSE);
614 ok(result, "SHGetSpecialFolderPathW(CSIDL_DESKTOP) failed! Last error: %08lx\n", GetLastError());
617 result = SHGetPathFromIDListW(pidlEmpty, wszPath);
618 ok(result, "SHGetPathFromIDListW failed! Last error: %08lx\n", GetLastError());
620 ok(!lstrcmpiW(wszDesktop, wszPath), "SHGetPathFromIDList didn't return desktop path for empty pidl!\n");
622 /* MyComputer does not map to a filesystem path. SHGetPathFromIDList should fail. */
623 hr = SHGetDesktopFolder(&psfDesktop);
624 ok (SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08lx\n", hr);
625 if (FAILED(hr)) return;
627 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszMyComputer, NULL, &pidlMyComputer, NULL);
628 ok (SUCCEEDED(hr), "Desktop's ParseDisplayName failed to parse MyComputer's CLSID! hr = %08lx\n", hr);
630 IShellFolder_Release(psfDesktop);
634 SetLastError(0xdeadbeef);
635 result = SHGetPathFromIDListW(pidlMyComputer, wszPath);
636 ok (!result, "SHGetPathFromIDList succeeded where it shouldn't!\n");
637 ok (GetLastError()==0xdeadbeef, "SHGetPathFromIDList shouldn't set last error! Last error: %08lx\n", GetLastError());
639 IShellFolder_Release(psfDesktop);
643 IMalloc_Free(ppM, pidlMyComputer);
645 result = pSHGetSpecialFolderPathW(NULL, wszFileName, CSIDL_DESKTOPDIRECTORY, FALSE);
646 ok(result, "SHGetSpecialFolderPathW failed! Last error: %08lx\n", GetLastError());
648 IShellFolder_Release(psfDesktop);
651 PathAddBackslashW(wszFileName);
652 lstrcatW(wszFileName, wszTestFile);
653 hTestFile = CreateFileW(wszFileName, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, NULL);
654 ok(hTestFile != INVALID_HANDLE_VALUE, "CreateFileW failed! Last error: %08lx\n", GetLastError());
655 if (hTestFile == INVALID_HANDLE_VALUE) {
656 IShellFolder_Release(psfDesktop);
659 CloseHandle(hTestFile);
661 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszTestFile, NULL, &pidlTestFile, NULL);
662 ok (SUCCEEDED(hr), "Desktop's ParseDisplayName failed to parse filename hr = %08lx\n", hr);
664 IShellFolder_Release(psfDesktop);
665 DeleteFileW(wszFileName);
666 IMalloc_Free(ppM, pidlTestFile);
670 /* This test is to show that the Desktop shellfolder prepends the CSIDL_DESKTOPDIRECTORY
671 * path for files placed on the desktop, if called with SHGDN_FORPARSING. */
672 hr = IShellFolder_GetDisplayNameOf(psfDesktop, pidlTestFile, SHGDN_FORPARSING, &strret);
673 ok (SUCCEEDED(hr), "Desktop's GetDisplayNamfOf failed! hr = %08lx\n", hr);
674 IShellFolder_Release(psfDesktop);
675 DeleteFileW(wszFileName);
677 IMalloc_Free(ppM, pidlTestFile);
682 pStrRetToBufW(&strret, pidlTestFile, wszPath, MAX_PATH);
683 ok(0 == lstrcmpW(wszFileName, wszPath),
684 "Desktop->GetDisplayNameOf(pidlTestFile, SHGDN_FORPARSING) "
685 "returned incorrect path for file placed on desktop\n");
688 result = SHGetPathFromIDListW(pidlTestFile, wszPath);
689 ok(result, "SHGetPathFromIDListW failed! Last error: %08lx\n", GetLastError());
690 IMalloc_Free(ppM, pidlTestFile);
692 ok(0 == lstrcmpW(wszFileName, wszPath), "SHGetPathFromIDListW returned incorrect path for file placed on desktop\n");
695 static void test_EnumObjects_and_CompareIDs(void)
698 IShellFolder *IDesktopFolder, *testIShellFolder;
699 char cCurrDirA [MAX_PATH] = {0};
700 WCHAR cCurrDirW [MAX_PATH];
701 static const WCHAR cTestDirW[] = {'\\','t','e','s','t','d','i','r',0};
705 GetCurrentDirectoryA(MAX_PATH, cCurrDirA);
706 len = lstrlenA(cCurrDirA);
709 trace("GetCurrentDirectoryA returned empty string. Skipping test_EnumObjects_and_CompareIDs\n");
712 if(cCurrDirA[len-1] == '\\')
713 cCurrDirA[len-1] = 0;
715 MultiByteToWideChar(CP_ACP, 0, cCurrDirA, -1, cCurrDirW, MAX_PATH);
716 strcatW(cCurrDirW, cTestDirW);
718 hr = SHGetDesktopFolder(&IDesktopFolder);
719 ok(hr == S_OK, "SHGetDesktopfolder failed %08lx\n", hr);
721 CreateFilesFolders();
723 hr = IShellFolder_ParseDisplayName(IDesktopFolder, NULL, NULL, cCurrDirW, NULL, &newPIDL, 0);
724 ok(hr == S_OK, "ParseDisplayName failed %08lx\n", hr);
726 hr = IShellFolder_BindToObject(IDesktopFolder, newPIDL, NULL, (REFIID)&IID_IShellFolder, (LPVOID *)&testIShellFolder);
727 ok(hr == S_OK, "BindToObject failed %08lx\n", hr);
729 test_EnumObjects(testIShellFolder);
731 hr = IShellFolder_Release(testIShellFolder);
732 ok(hr == S_OK, "IShellFolder_Release failed %08lx\n", hr);
736 IMalloc_Free(ppM, newPIDL);
739 START_TEST(shlfolder)
741 init_function_pointers();
742 /* if OleInitialize doesn't get called, ParseDisplayName returns
743 CO_E_NOTINITIALIZED for malformed directory names on win2k. */
746 test_ParseDisplayName();
748 test_EnumObjects_and_CompareIDs();
749 test_GetDisplayName();
750 test_GetAttributesOf();
751 test_SHGetPathFromIDList();
752 test_CallForAttributes();