SHELL32.ILFindLastID is exported by ordinal.
[wine.git] / dlls / shell32 / tests / shlfolder.c
blob0e7529a09a51abdc9459dd2592ccf19c111720ec
1 /*
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
21 #include <stdarg.h>
22 #include <stdio.h>
24 #define COBJMACROS
26 #include "windef.h"
27 #include "winbase.h"
28 #include "wtypes.h"
29 #include "shellapi.h"
32 #include "shlguid.h"
33 #include "shlobj.h"
34 #include "shobjidl.h"
35 #include "shlwapi.h"
36 #include "ocidl.h"
37 #include "oleauto.h"
40 #include "wine/unicode.h"
41 #include "wine/test.h"
44 static IMalloc *ppM;
46 static HRESULT (WINAPI *pSHBindToParent)(LPCITEMIDLIST, REFIID, LPVOID*, LPCITEMIDLIST*);
47 static BOOL (WINAPI *pSHGetSpecialFolderPathW)(HWND, LPWSTR, int, BOOL);
48 static HRESULT (WINAPI *pStrRetToBufW)(STRRET*,LPCITEMIDLIST,LPWSTR,UINT);
49 static LPITEMIDLIST (WINAPI *pILFindLastID)(LPCITEMIDLIST);
51 static void init_function_pointers(void)
53 HMODULE hmod;
54 HRESULT hr;
56 hmod = GetModuleHandleA("shell32.dll");
57 if(hmod)
59 pSHBindToParent = (void*)GetProcAddress(hmod, "SHBindToParent");
60 pSHGetSpecialFolderPathW = (void*)GetProcAddress(hmod, "SHGetSpecialFolderPathW");
61 pILFindLastID = (void *)GetProcAddress(hmod, (LPCSTR)16);
65 hmod = GetModuleHandleA("shlwapi.dll");
66 if(hmod)
68 pStrRetToBufW = (void*)GetProcAddress(hmod, "StrRetToBufW");
71 hr = SHGetMalloc(&ppM);
72 ok(hr == S_OK, "SHGetMalloc failed %08lx\n", hr);
75 static void test_ParseDisplayName(void)
77 HRESULT hr;
78 IShellFolder *IDesktopFolder;
79 static const char *cNonExistDir1A = "c:\\nonexist_subdir";
80 static const char *cNonExistDir2A = "c:\\\\nonexist_subdir";
81 DWORD res;
82 WCHAR cTestDirW [MAX_PATH] = {0};
83 ITEMIDLIST *newPIDL;
85 hr = SHGetDesktopFolder(&IDesktopFolder);
86 if(hr != S_OK) return;
88 res = GetFileAttributesA(cNonExistDir1A);
89 if(res != INVALID_FILE_ATTRIBUTES) return;
91 MultiByteToWideChar(CP_ACP, 0, cNonExistDir1A, -1, cTestDirW, MAX_PATH);
92 hr = IShellFolder_ParseDisplayName(IDesktopFolder,
93 NULL, NULL, cTestDirW, NULL, &newPIDL, 0);
94 ok((hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) || (hr == E_FAIL),
95 "ParseDisplayName returned %08lx, expected 80070002 or E_FAIL\n", hr);
97 res = GetFileAttributesA(cNonExistDir2A);
98 if(res != INVALID_FILE_ATTRIBUTES) return;
100 MultiByteToWideChar(CP_ACP, 0, cNonExistDir2A, -1, cTestDirW, MAX_PATH);
101 hr = IShellFolder_ParseDisplayName(IDesktopFolder,
102 NULL, NULL, cTestDirW, NULL, &newPIDL, 0);
103 ok((hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) || (hr == E_FAIL) || (hr == E_INVALIDARG),
104 "ParseDisplayName returned %08lx, expected 80070002, E_FAIL or E_INVALIDARG\n", hr);
107 /* creates a file with the specified name for tests */
108 static void CreateTestFile(const CHAR *name)
110 HANDLE file;
111 DWORD written;
113 file = CreateFileA(name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
114 if (file != INVALID_HANDLE_VALUE)
116 WriteFile(file, name, strlen(name), &written, NULL);
117 WriteFile(file, "\n", strlen("\n"), &written, NULL);
118 CloseHandle(file);
123 /* initializes the tests */
124 static void CreateFilesFolders(void)
126 CreateDirectoryA(".\\testdir", NULL);
127 CreateDirectoryA(".\\testdir\\test.txt", NULL);
128 CreateTestFile (".\\testdir\\test1.txt ");
129 CreateTestFile (".\\testdir\\test2.txt ");
130 CreateTestFile (".\\testdir\\test3.txt ");
131 CreateDirectoryA(".\\testdir\\testdir2 ", NULL);
132 CreateDirectoryA(".\\testdir\\testdir2\\subdir", NULL);
135 /* cleans after tests */
136 static void Cleanup(void)
138 DeleteFileA(".\\testdir\\test1.txt");
139 DeleteFileA(".\\testdir\\test2.txt");
140 DeleteFileA(".\\testdir\\test3.txt");
141 RemoveDirectoryA(".\\testdir\\test.txt");
142 RemoveDirectoryA(".\\testdir\\testdir2\\subdir");
143 RemoveDirectoryA(".\\testdir\\testdir2");
144 RemoveDirectoryA(".\\testdir");
148 /* perform test */
149 static void test_EnumObjects(IShellFolder *iFolder)
151 IEnumIDList *iEnumList;
152 LPITEMIDLIST newPIDL, idlArr[10];
153 ULONG NumPIDLs;
154 int i=0, j;
155 HRESULT hr;
157 static const WORD iResults [5][5] =
159 { 0,-1,-1,-1,-1},
160 { 1, 0,-1,-1,-1},
161 { 1, 1, 0,-1,-1},
162 { 1, 1, 1, 0,-1},
163 { 1, 1, 1, 1, 0}
166 /* Just test SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR for now */
167 static const ULONG attrs[5] =
169 SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR,
170 SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR,
171 SFGAO_FILESYSTEM,
172 SFGAO_FILESYSTEM,
173 SFGAO_FILESYSTEM,
176 hr = IShellFolder_EnumObjects(iFolder, NULL, SHCONTF_FOLDERS | SHCONTF_NONFOLDERS | SHCONTF_INCLUDEHIDDEN, &iEnumList);
177 ok(hr == S_OK, "EnumObjects failed %08lx\n", hr);
179 /* This is to show that, contrary to what is said on MSDN, on IEnumIDList::Next,
180 * the filesystem shellfolders return S_OK even if less than 'celt' items are
181 * returned (in contrast to S_FALSE). We have to do it in a loop since WinXP
182 * only ever returns a single entry per call. */
183 while (IEnumIDList_Next(iEnumList, 10-i, &idlArr[i], &NumPIDLs) == S_OK)
184 i += NumPIDLs;
185 ok (i == 5, "i: %d\n", i);
187 hr = IEnumIDList_Release(iEnumList);
188 ok(hr == S_OK, "IEnumIDList_Release failed %08lx\n", hr);
190 /* Sort them first in case of wrong order from system */
191 for (i=0;i<5;i++) for (j=0;j<5;j++)
192 if ((SHORT)IShellFolder_CompareIDs(iFolder, 0, idlArr[i], idlArr[j]) < 0)
194 newPIDL = idlArr[i];
195 idlArr[i] = idlArr[j];
196 idlArr[j] = newPIDL;
199 for (i=0;i<5;i++) for (j=0;j<5;j++)
201 hr = IShellFolder_CompareIDs(iFolder, 0, idlArr[i], idlArr[j]);
202 ok(hr == iResults[i][j], "Got %lx expected [%d]-[%d]=%x\n", hr, i, j, iResults[i][j]);
206 for (i = 0; i < 5; i++)
208 SFGAOF flags;
209 flags = SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR;
210 hr = IShellFolder_GetAttributesOf(iFolder, 1, (LPCITEMIDLIST*)(idlArr + i), &flags);
211 flags &= SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR;
212 ok(hr == S_OK, "GetAttributesOf returns %08lx\n", hr);
213 ok(flags == attrs[i], "GetAttributesOf gets attrs %08lx, expects %08lx\n", flags, attrs[i]);
216 for (i=0;i<5;i++)
217 IMalloc_Free(ppM, idlArr[i]);
220 static void test_BindToObject(void)
222 HRESULT hr;
223 UINT cChars;
224 IShellFolder *psfDesktop, *psfChild, *psfMyComputer, *psfSystemDir;
225 SHITEMID emptyitem = { 0, { 0 } };
226 LPITEMIDLIST pidlMyComputer, pidlSystemDir, pidlEmpty = (LPITEMIDLIST)&emptyitem;
227 WCHAR wszSystemDir[MAX_PATH];
228 char szSystemDir[MAX_PATH];
229 WCHAR wszMyComputer[] = {
230 ':',':','{','2','0','D','0','4','F','E','0','-','3','A','E','A','-','1','0','6','9','-',
231 'A','2','D','8','-','0','8','0','0','2','B','3','0','3','0','9','D','}',0 };
233 /* The following tests shows that BindToObject should fail with E_INVALIDARG if called
234 * with an empty pidl. This is tested for Desktop, MyComputer and the FS ShellFolder
236 hr = SHGetDesktopFolder(&psfDesktop);
237 ok (SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08lx\n", hr);
238 if (FAILED(hr)) return;
240 hr = IShellFolder_BindToObject(psfDesktop, pidlEmpty, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
241 ok (hr == E_INVALIDARG, "Desktop's BindToObject should fail, when called with empty pidl! hr = %08lx\n", hr);
243 hr = IShellFolder_BindToObject(psfDesktop, NULL, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
244 ok (hr == E_INVALIDARG, "Desktop's BindToObject should fail, when called with NULL pidl! hr = %08lx\n", hr);
246 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszMyComputer, NULL, &pidlMyComputer, NULL);
247 ok (SUCCEEDED(hr), "Desktop's ParseDisplayName failed to parse MyComputer's CLSID! hr = %08lx\n", hr);
248 if (FAILED(hr)) {
249 IShellFolder_Release(psfDesktop);
250 return;
253 hr = IShellFolder_BindToObject(psfDesktop, pidlMyComputer, NULL, &IID_IShellFolder, (LPVOID*)&psfMyComputer);
254 ok (SUCCEEDED(hr), "Desktop failed to bind to MyComputer object! hr = %08lx\n", hr);
255 IShellFolder_Release(psfDesktop);
256 IMalloc_Free(ppM, pidlMyComputer);
257 if (FAILED(hr)) return;
259 hr = IShellFolder_BindToObject(psfMyComputer, pidlEmpty, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
260 ok (hr == E_INVALIDARG, "MyComputers's BindToObject should fail, when called with empty pidl! hr = %08lx\n", hr);
262 hr = IShellFolder_BindToObject(psfMyComputer, NULL, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
263 ok (hr == E_INVALIDARG, "MyComputers's BindToObject should fail, when called with NULL pidl! hr = %08lx\n", hr);
265 cChars = GetSystemDirectoryA(szSystemDir, MAX_PATH);
266 ok (cChars > 0 && cChars < MAX_PATH, "GetSystemDirectoryA failed! LastError: %08lx\n", GetLastError());
267 if (cChars == 0 || cChars >= MAX_PATH) {
268 IShellFolder_Release(psfMyComputer);
269 return;
271 MultiByteToWideChar(CP_ACP, 0, szSystemDir, -1, wszSystemDir, MAX_PATH);
273 hr = IShellFolder_ParseDisplayName(psfMyComputer, NULL, NULL, wszSystemDir, NULL, &pidlSystemDir, NULL);
274 ok (SUCCEEDED(hr), "MyComputers's ParseDisplayName failed to parse the SystemDirectory! hr = %08lx\n", hr);
275 if (FAILED(hr)) {
276 IShellFolder_Release(psfMyComputer);
277 return;
280 hr = IShellFolder_BindToObject(psfMyComputer, pidlSystemDir, NULL, &IID_IShellFolder, (LPVOID*)&psfSystemDir);
281 ok (SUCCEEDED(hr), "MyComputer failed to bind to a FileSystem ShellFolder! hr = %08lx\n", hr);
282 IShellFolder_Release(psfMyComputer);
283 IMalloc_Free(ppM, pidlSystemDir);
284 if (FAILED(hr)) return;
286 hr = IShellFolder_BindToObject(psfSystemDir, pidlEmpty, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
287 ok (hr == E_INVALIDARG,
288 "FileSystem ShellFolder's BindToObject should fail, when called with empty pidl! hr = %08lx\n", hr);
290 hr = IShellFolder_BindToObject(psfSystemDir, NULL, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
291 ok (hr == E_INVALIDARG,
292 "FileSystem ShellFolder's BindToObject should fail, when called with NULL pidl! hr = %08lx\n", hr);
294 IShellFolder_Release(psfSystemDir);
297 static void test_GetDisplayName(void)
299 BOOL result;
300 HRESULT hr;
301 HANDLE hTestFile;
302 WCHAR wszTestFile[MAX_PATH], wszTestFile2[MAX_PATH], wszTestDir[MAX_PATH];
303 char szTestFile[MAX_PATH], szTestDir[MAX_PATH];
304 STRRET strret;
305 LPSHELLFOLDER psfDesktop, psfPersonal;
306 IUnknown *psfFile;
307 LPITEMIDLIST pidlTestFile;
308 LPCITEMIDLIST pidlLast;
309 static const WCHAR wszFileName[] = { 'w','i','n','e','t','e','s','t','.','f','o','o',0 };
310 static const WCHAR wszDirName[] = { 'w','i','n','e','t','e','s','t',0 };
312 /* I'm trying to figure if there is a functional difference between calling
313 * SHGetPathFromIDList and calling GetDisplayNameOf(SHGDN_FORPARSING) after
314 * binding to the shellfolder. One thing I thought of was that perhaps
315 * SHGetPathFromIDList would be able to get the path to a file, which does
316 * not exist anymore, while the other method would'nt. It turns out there's
317 * no functional difference in this respect.
320 if(!pSHGetSpecialFolderPathW) return;
322 /* First creating a directory in MyDocuments and a file in this directory. */
323 result = pSHGetSpecialFolderPathW(NULL, wszTestDir, CSIDL_PERSONAL, FALSE);
324 ok(result, "SHGetSpecialFolderPathW failed! Last error: %08lx\n", GetLastError());
325 if (!result) return;
327 PathAddBackslashW(wszTestDir);
328 lstrcatW(wszTestDir, wszDirName);
329 WideCharToMultiByte(CP_ACP, 0, wszTestDir, -1, szTestDir, MAX_PATH, 0, 0);
330 result = CreateDirectoryA(szTestDir, NULL);
331 ok(result, "CreateDirectoryA failed! Last error: %08lx\n", GetLastError());
332 if (!result) return;
334 lstrcpyW(wszTestFile, wszTestDir);
335 PathAddBackslashW(wszTestFile);
336 lstrcatW(wszTestFile, wszFileName);
337 WideCharToMultiByte(CP_ACP, 0, wszTestFile, -1, szTestFile, MAX_PATH, 0, 0);
339 hTestFile = CreateFileA(szTestFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
340 ok((hTestFile != INVALID_HANDLE_VALUE), "CreateFileA failed! Last error: %08lx\n", GetLastError());
341 if (hTestFile == INVALID_HANDLE_VALUE) return;
342 CloseHandle(hTestFile);
344 /* Getting an itemidlist for the file. */
345 hr = SHGetDesktopFolder(&psfDesktop);
346 ok(SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08lx\n", hr);
347 if (FAILED(hr)) return;
349 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszTestFile, NULL, &pidlTestFile, NULL);
350 ok(SUCCEEDED(hr), "Desktop->ParseDisplayName failed! hr = %08lx\n", hr);
351 if (FAILED(hr)) {
352 IShellFolder_Release(psfDesktop);
353 return;
356 /* It seems as if we cannot bind to regular files on windows, but only directories.
358 hr = IShellFolder_BindToObject(psfDesktop, pidlTestFile, NULL, &IID_IUnknown, (VOID**)&psfFile);
359 todo_wine { ok (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), "hr = %08lx\n", hr); }
360 if (SUCCEEDED(hr)) {
361 IShellFolder_Release(psfFile);
364 /* Deleting the file and the directory */
365 DeleteFileA(szTestFile);
366 RemoveDirectoryA(szTestDir);
368 /* SHGetPathFromIDListW still works, although the file is not present anymore. */
369 result = SHGetPathFromIDListW(pidlTestFile, wszTestFile2);
370 ok (result, "SHGetPathFromIDListW failed! Last error: %08lx\n", GetLastError());
371 ok (!lstrcmpiW(wszTestFile, wszTestFile2), "SHGetPathFromIDListW returns incorrect path!\n");
373 if(!pSHBindToParent) return;
375 /* Binding to the folder and querying the display name of the file also works. */
376 hr = pSHBindToParent(pidlTestFile, &IID_IShellFolder, (VOID**)&psfPersonal, &pidlLast);
377 ok (SUCCEEDED(hr), "SHBindToParent failed! hr = %08lx\n", hr);
378 if (FAILED(hr)) {
379 IShellFolder_Release(psfDesktop);
380 return;
383 /* This test shows that Windows doesn't allocate a new pidlLast, but returns a pointer into
384 * pidlTestFile (In accordance with MSDN). */
385 todo_wine{ok (pILFindLastID(pidlTestFile) == pidlLast,
386 "SHBindToParent doesn't return the last id of the pidl param!\n");}
388 hr = IShellFolder_GetDisplayNameOf(psfPersonal, pidlLast, SHGDN_FORPARSING, &strret);
389 ok (SUCCEEDED(hr), "Personal->GetDisplayNameOf failed! hr = %08lx\n", hr);
390 if (FAILED(hr)) {
391 IShellFolder_Release(psfDesktop);
392 IShellFolder_Release(psfPersonal);
393 return;
396 if (pStrRetToBufW)
398 hr = pStrRetToBufW(&strret, pidlLast, wszTestFile2, MAX_PATH);
399 ok (SUCCEEDED(hr), "StrRetToBufW failed! hr = %08lx\n", hr);
400 ok (!lstrcmpiW(wszTestFile, wszTestFile2), "GetDisplayNameOf returns incorrect path!\n");
403 IShellFolder_Release(psfDesktop);
404 IShellFolder_Release(psfPersonal);
407 static void test_CallForAttributes(void)
409 HKEY hKey;
410 LONG lResult;
411 HRESULT hr;
412 DWORD dwSize;
413 LPSHELLFOLDER psfDesktop;
414 LPITEMIDLIST pidlMyDocuments;
415 DWORD dwAttributes, dwCallForAttributes, dwOrigAttributes, dwOrigCallForAttributes;
416 static const WCHAR wszAttributes[] = { 'A','t','t','r','i','b','u','t','e','s',0 };
417 static const WCHAR wszCallForAttributes[] = {
418 'C','a','l','l','F','o','r','A','t','t','r','i','b','u','t','e','s',0 };
419 static const WCHAR wszMyDocumentsKey[] = {
420 'C','L','S','I','D','\\','{','4','5','0','D','8','F','B','A','-','A','D','2','5','-',
421 '1','1','D','0','-','9','8','A','8','-','0','8','0','0','3','6','1','B','1','1','0','3','}',
422 '\\','S','h','e','l','l','F','o','l','d','e','r',0 };
423 WCHAR wszMyDocuments[] = {
424 ':',':','{','4','5','0','D','8','F','B','A','-','A','D','2','5','-','1','1','D','0','-',
425 '9','8','A','8','-','0','8','0','0','3','6','1','B','1','1','0','3','}',0 };
427 /* For the root of a namespace extension, the attributes are not queried by binding
428 * to the object and calling GetAttributesOf. Instead, the attributes are read from
429 * the registry value HKCR/CLSID/{...}/ShellFolder/Attributes. This is documented on MSDN.
431 * The MyDocuments shellfolder on WinXP has a HKCR/CLSID/{...}/ShellFolder/CallForAttributes
432 * value. It seems that if the folder is queried for one of the flags set in CallForAttributes,
433 * the shell does bind to the folder object and calls GetAttributesOf. This is not documented
434 * on MSDN. This test is meant to document the observed behaviour on WinXP SP2.
436 hr = SHGetDesktopFolder(&psfDesktop);
437 ok (SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08lx\n", hr);
438 if (FAILED(hr)) return;
440 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszMyDocuments, NULL,
441 &pidlMyDocuments, NULL);
442 ok (SUCCEEDED(hr),
443 "Desktop's ParseDisplayName failed to parse MyDocuments's CLSID! hr = %08lx\n", hr);
444 if (FAILED(hr)) {
445 IShellFolder_Release(psfDesktop);
446 return;
449 dwAttributes = 0xffffffff;
450 hr = IShellFolder_GetAttributesOf(psfDesktop, 1,
451 (LPCITEMIDLIST*)&pidlMyDocuments, &dwAttributes);
452 ok (SUCCEEDED(hr), "Desktop->GetAttributesOf(MyDocuments) failed! hr = %08lx\n", hr);
454 /* We need the following setup (as observed on WinXP SP2), for the tests to make sense. */
455 todo_wine{ ok (dwAttributes & SFGAO_FILESYSTEM,
456 "SFGAO_FILESYSTEM attribute is not set for MyDocuments!\n"); }
457 ok (!(dwAttributes & SFGAO_ISSLOW), "SFGAO_ISSLOW attribute is set for MyDocuments!\n");
458 ok (!(dwAttributes & SFGAO_GHOSTED), "SFGAO_GHOSTED attribute is set for MyDocuments!\n");
460 /* We don't have the MyDocuments shellfolder in wine yet, and thus we don't have the registry
461 * key. So the test will return at this point, if run on wine.
463 lResult = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszMyDocumentsKey, 0, KEY_WRITE|KEY_READ, &hKey);
464 todo_wine { ok (lResult == ERROR_SUCCESS, "RegOpenKeyEx failed! result: %08lx\n", lResult); }
465 if (lResult != ERROR_SUCCESS) {
466 IMalloc_Free(ppM, pidlMyDocuments);
467 IShellFolder_Release(psfDesktop);
468 return;
471 /* Query MyDocuments' Attributes value, to be able to restore it later. */
472 dwSize = sizeof(DWORD);
473 lResult = RegQueryValueExW(hKey, wszAttributes, NULL, NULL, (LPBYTE)&dwOrigAttributes, &dwSize);
474 ok (lResult == ERROR_SUCCESS, "RegQueryValueEx failed! result: %08lx\n", lResult);
475 if (lResult != ERROR_SUCCESS) {
476 RegCloseKey(hKey);
477 IMalloc_Free(ppM, pidlMyDocuments);
478 IShellFolder_Release(psfDesktop);
479 return;
482 /* Query MyDocuments' CallForAttributes value, to be able to restore it later. */
483 dwSize = sizeof(DWORD);
484 lResult = RegQueryValueExW(hKey, wszCallForAttributes, NULL, NULL,
485 (LPBYTE)&dwOrigCallForAttributes, &dwSize);
486 ok (lResult == ERROR_SUCCESS, "RegQueryValueEx failed! result: %08lx\n", lResult);
487 if (lResult != ERROR_SUCCESS) {
488 RegCloseKey(hKey);
489 IMalloc_Free(ppM, pidlMyDocuments);
490 IShellFolder_Release(psfDesktop);
491 return;
494 /* Define via the Attributes value that MyDocuments attributes are SFGAO_ISSLOW and
495 * SFGAO_GHOSTED and that MyDocuments should be called for the SFGAO_ISSLOW and
496 * SFGAO_FILESYSTEM attributes. */
497 dwAttributes = SFGAO_ISSLOW|SFGAO_GHOSTED;
498 RegSetValueExW(hKey, wszAttributes, 0, REG_DWORD, (LPBYTE)&dwAttributes, sizeof(DWORD));
499 dwCallForAttributes = SFGAO_ISSLOW|SFGAO_FILESYSTEM;
500 RegSetValueExW(hKey, wszCallForAttributes, 0, REG_DWORD,
501 (LPBYTE)&dwCallForAttributes, sizeof(DWORD));
503 /* Although it is not set in CallForAttributes, the SFGAO_GHOSTED flag is reset by
504 * GetAttributesOf. It seems that once there is a single attribute queried, for which
505 * CallForAttributes is set, all flags are taken from the GetAttributesOf call and
506 * the flags in Attributes are ignored.
508 dwAttributes = SFGAO_ISSLOW|SFGAO_GHOSTED|SFGAO_FILESYSTEM;
509 hr = IShellFolder_GetAttributesOf(psfDesktop, 1,
510 (LPCITEMIDLIST*)&pidlMyDocuments, &dwAttributes);
511 ok (SUCCEEDED(hr), "Desktop->GetAttributesOf(MyDocuments) failed! hr = %08lx\n", hr);
512 if (SUCCEEDED(hr))
513 ok (dwAttributes == SFGAO_FILESYSTEM,
514 "Desktop->GetAttributes(MyDocuments) returned unexpected attributes: %08lx\n",
515 dwAttributes);
517 /* Restore MyDocuments' original Attributes and CallForAttributes registry values */
518 RegSetValueExW(hKey, wszAttributes, 0, REG_DWORD, (LPBYTE)&dwOrigAttributes, sizeof(DWORD));
519 RegSetValueExW(hKey, wszCallForAttributes, 0, REG_DWORD,
520 (LPBYTE)&dwOrigCallForAttributes, sizeof(DWORD));
521 RegCloseKey(hKey);
522 IMalloc_Free(ppM, pidlMyDocuments);
523 IShellFolder_Release(psfDesktop);
526 static void test_GetAttributesOf(void)
528 HRESULT hr;
529 LPSHELLFOLDER psfDesktop, psfMyComputer;
530 SHITEMID emptyitem = { 0, { 0 } };
531 LPCITEMIDLIST pidlEmpty = (LPCITEMIDLIST)&emptyitem;
532 LPITEMIDLIST pidlMyComputer;
533 DWORD dwFlags;
534 const static DWORD dwDesktopFlags = /* As observed on WinXP SP2 */
535 SFGAO_STORAGE | SFGAO_HASPROPSHEET | SFGAO_STORAGEANCESTOR |
536 SFGAO_FILESYSANCESTOR | SFGAO_FOLDER | SFGAO_FILESYSTEM | SFGAO_HASSUBFOLDER;
537 const static DWORD dwMyComputerFlags = /* As observed on WinXP SP2 */
538 SFGAO_CANRENAME | SFGAO_CANDELETE | SFGAO_HASPROPSHEET |
539 SFGAO_DROPTARGET | SFGAO_FILESYSANCESTOR | SFGAO_FOLDER | SFGAO_HASSUBFOLDER;
540 WCHAR wszMyComputer[] = {
541 ':',':','{','2','0','D','0','4','F','E','0','-','3','A','E','A','-','1','0','6','9','-',
542 'A','2','D','8','-','0','8','0','0','2','B','3','0','3','0','9','D','}',0 };
544 hr = SHGetDesktopFolder(&psfDesktop);
545 ok (SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08lx\n", hr);
546 if (FAILED(hr)) return;
548 /* The Desktop attributes can be queried with a single empty itemidlist, .. */
549 dwFlags = 0xffffffff;
550 hr = IShellFolder_GetAttributesOf(psfDesktop, 1, &pidlEmpty, &dwFlags);
551 ok (SUCCEEDED(hr), "Desktop->GetAttributesOf(empty pidl) failed! hr = %08lx\n", hr);
552 ok (dwFlags == dwDesktopFlags, "Wrong Desktop attributes: %08lx, expected: %08lx\n",
553 dwFlags, dwDesktopFlags);
555 /* .. or with no itemidlist at all. */
556 dwFlags = 0xffffffff;
557 hr = IShellFolder_GetAttributesOf(psfDesktop, 0, NULL, &dwFlags);
558 ok (SUCCEEDED(hr), "Desktop->GetAttributesOf(NULL) failed! hr = %08lx\n", hr);
559 ok (dwFlags == dwDesktopFlags, "Wrong Desktop attributes: %08lx, expected: %08lx\n",
560 dwFlags, dwDesktopFlags);
562 /* Testing the attributes of the MyComputer shellfolder */
563 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszMyComputer, NULL, &pidlMyComputer, NULL);
564 ok (SUCCEEDED(hr), "Desktop's ParseDisplayName failed to parse MyComputer's CLSID! hr = %08lx\n", hr);
565 if (FAILED(hr)) {
566 IShellFolder_Release(psfDesktop);
567 return;
570 /* WinXP SP2 sets the SFGAO_CANLINK flag, when MyComputer is queried via the Desktop
571 * folder object. It doesn't do this, if MyComputer is queried directly (see below).
572 * SFGAO_CANLINK is the same as DROPEFFECT_LINK, which MSDN says means: "Drag source
573 * should create a link to the original data". You can't create links on MyComputer on
574 * Windows, so this flag shouldn't be set. Seems like a bug in Windows. As long as nobody
575 * depends on this bug, we probably shouldn't imitate it.
577 dwFlags = 0xffffffff;
578 hr = IShellFolder_GetAttributesOf(psfDesktop, 1, (LPCITEMIDLIST*)&pidlMyComputer, &dwFlags);
579 ok (SUCCEEDED(hr), "Desktop->GetAttributesOf(MyComputer) failed! hr = %08lx\n", hr);
580 todo_wine { ok ((dwFlags & ~(DWORD)SFGAO_CANLINK) == dwMyComputerFlags,
581 "Wrong MyComputer attributes: %08lx, expected: %08lx\n", dwFlags, dwMyComputerFlags); }
583 hr = IShellFolder_BindToObject(psfDesktop, pidlMyComputer, NULL, &IID_IShellFolder, (LPVOID*)&psfMyComputer);
584 ok (SUCCEEDED(hr), "Desktop failed to bind to MyComputer object! hr = %08lx\n", hr);
585 IShellFolder_Release(psfDesktop);
586 IMalloc_Free(ppM, pidlMyComputer);
587 if (FAILED(hr)) return;
589 hr = IShellFolder_GetAttributesOf(psfMyComputer, 1, &pidlEmpty, &dwFlags);
590 todo_wine {ok (hr == E_INVALIDARG, "MyComputer->GetAttributesOf(emtpy pidl) should fail! hr = %08lx\n", hr); }
592 dwFlags = 0xffffffff;
593 hr = IShellFolder_GetAttributesOf(psfMyComputer, 0, NULL, &dwFlags);
594 ok (SUCCEEDED(hr), "MyComputer->GetAttributesOf(NULL) failed! hr = %08lx\n", hr);
595 todo_wine { ok (dwFlags == dwMyComputerFlags,
596 "Wrong MyComputer attributes: %08lx, expected: %08lx\n", dwFlags, dwMyComputerFlags); }
598 IShellFolder_Release(psfMyComputer);
601 static void test_SHGetPathFromIDList(void)
603 SHITEMID emptyitem = { 0, { 0 } };
604 LPCITEMIDLIST pidlEmpty = (LPCITEMIDLIST)&emptyitem;
605 LPITEMIDLIST pidlMyComputer;
606 WCHAR wszPath[MAX_PATH], wszDesktop[MAX_PATH];
607 BOOL result;
608 HRESULT hr;
609 LPSHELLFOLDER psfDesktop;
610 WCHAR wszMyComputer[] = {
611 ':',':','{','2','0','D','0','4','F','E','0','-','3','A','E','A','-','1','0','6','9','-',
612 'A','2','D','8','-','0','8','0','0','2','B','3','0','3','0','9','D','}',0 };
613 WCHAR wszFileName[MAX_PATH];
614 LPITEMIDLIST pidlTestFile;
615 HANDLE hTestFile;
616 STRRET strret;
617 static WCHAR wszTestFile[] = {
618 'w','i','n','e','t','e','s','t','.','f','o','o',0 };
620 if(!pSHGetSpecialFolderPathW) return;
622 /* Calling SHGetPathFromIDList with an empty pidl should return the desktop folder's path. */
623 result = pSHGetSpecialFolderPathW(NULL, wszDesktop, CSIDL_DESKTOP, FALSE);
624 ok(result, "SHGetSpecialFolderPathW(CSIDL_DESKTOP) failed! Last error: %08lx\n", GetLastError());
625 if (!result) return;
627 result = SHGetPathFromIDListW(pidlEmpty, wszPath);
628 ok(result, "SHGetPathFromIDListW failed! Last error: %08lx\n", GetLastError());
629 if (!result) return;
630 ok(!lstrcmpiW(wszDesktop, wszPath), "SHGetPathFromIDList didn't return desktop path for empty pidl!\n");
632 /* MyComputer does not map to a filesystem path. SHGetPathFromIDList should fail. */
633 hr = SHGetDesktopFolder(&psfDesktop);
634 ok (SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08lx\n", hr);
635 if (FAILED(hr)) return;
637 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszMyComputer, NULL, &pidlMyComputer, NULL);
638 ok (SUCCEEDED(hr), "Desktop's ParseDisplayName failed to parse MyComputer's CLSID! hr = %08lx\n", hr);
639 if (FAILED(hr)) {
640 IShellFolder_Release(psfDesktop);
641 return;
644 SetLastError(0xdeadbeef);
645 result = SHGetPathFromIDListW(pidlMyComputer, wszPath);
646 ok (!result, "SHGetPathFromIDList succeeded where it shouldn't!\n");
647 ok (GetLastError()==0xdeadbeef, "SHGetPathFromIDList shouldn't set last error! Last error: %08lx\n", GetLastError());
648 if (result) {
649 IShellFolder_Release(psfDesktop);
650 return;
653 IMalloc_Free(ppM, pidlMyComputer);
655 result = pSHGetSpecialFolderPathW(NULL, wszFileName, CSIDL_DESKTOPDIRECTORY, FALSE);
656 ok(result, "SHGetSpecialFolderPathW failed! Last error: %08lx\n", GetLastError());
657 if (!result) {
658 IShellFolder_Release(psfDesktop);
659 return;
661 PathAddBackslashW(wszFileName);
662 lstrcatW(wszFileName, wszTestFile);
663 hTestFile = CreateFileW(wszFileName, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, NULL);
664 ok(hTestFile != INVALID_HANDLE_VALUE, "CreateFileW failed! Last error: %08lx\n", GetLastError());
665 if (hTestFile == INVALID_HANDLE_VALUE) {
666 IShellFolder_Release(psfDesktop);
667 return;
669 CloseHandle(hTestFile);
671 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszTestFile, NULL, &pidlTestFile, NULL);
672 ok (SUCCEEDED(hr), "Desktop's ParseDisplayName failed to parse filename hr = %08lx\n", hr);
673 if (FAILED(hr)) {
674 IShellFolder_Release(psfDesktop);
675 DeleteFileW(wszFileName);
676 IMalloc_Free(ppM, pidlTestFile);
677 return;
680 /* This test is to show that the Desktop shellfolder prepends the CSIDL_DESKTOPDIRECTORY
681 * path for files placed on the desktop, if called with SHGDN_FORPARSING. */
682 hr = IShellFolder_GetDisplayNameOf(psfDesktop, pidlTestFile, SHGDN_FORPARSING, &strret);
683 ok (SUCCEEDED(hr), "Desktop's GetDisplayNamfOf failed! hr = %08lx\n", hr);
684 IShellFolder_Release(psfDesktop);
685 DeleteFileW(wszFileName);
686 if (FAILED(hr)) {
687 IMalloc_Free(ppM, pidlTestFile);
688 return;
690 if (pStrRetToBufW)
692 pStrRetToBufW(&strret, pidlTestFile, wszPath, MAX_PATH);
693 ok(0 == lstrcmpW(wszFileName, wszPath),
694 "Desktop->GetDisplayNameOf(pidlTestFile, SHGDN_FORPARSING) "
695 "returned incorrect path for file placed on desktop\n");
698 result = SHGetPathFromIDListW(pidlTestFile, wszPath);
699 ok(result, "SHGetPathFromIDListW failed! Last error: %08lx\n", GetLastError());
700 IMalloc_Free(ppM, pidlTestFile);
701 if (!result) return;
702 ok(0 == lstrcmpW(wszFileName, wszPath), "SHGetPathFromIDListW returned incorrect path for file placed on desktop\n");
705 static void test_EnumObjects_and_CompareIDs(void)
707 ITEMIDLIST *newPIDL;
708 IShellFolder *IDesktopFolder, *testIShellFolder;
709 char cCurrDirA [MAX_PATH] = {0};
710 WCHAR cCurrDirW [MAX_PATH];
711 static const WCHAR cTestDirW[] = {'\\','t','e','s','t','d','i','r',0};
712 int len;
713 HRESULT hr;
715 GetCurrentDirectoryA(MAX_PATH, cCurrDirA);
716 len = lstrlenA(cCurrDirA);
718 if(len == 0) {
719 trace("GetCurrentDirectoryA returned empty string. Skipping test_EnumObjects_and_CompareIDs\n");
720 return;
722 if(cCurrDirA[len-1] == '\\')
723 cCurrDirA[len-1] = 0;
725 MultiByteToWideChar(CP_ACP, 0, cCurrDirA, -1, cCurrDirW, MAX_PATH);
726 strcatW(cCurrDirW, cTestDirW);
728 hr = SHGetDesktopFolder(&IDesktopFolder);
729 ok(hr == S_OK, "SHGetDesktopfolder failed %08lx\n", hr);
731 CreateFilesFolders();
733 hr = IShellFolder_ParseDisplayName(IDesktopFolder, NULL, NULL, cCurrDirW, NULL, &newPIDL, 0);
734 ok(hr == S_OK, "ParseDisplayName failed %08lx\n", hr);
736 hr = IShellFolder_BindToObject(IDesktopFolder, newPIDL, NULL, (REFIID)&IID_IShellFolder, (LPVOID *)&testIShellFolder);
737 ok(hr == S_OK, "BindToObject failed %08lx\n", hr);
739 test_EnumObjects(testIShellFolder);
741 hr = IShellFolder_Release(testIShellFolder);
742 ok(hr == S_OK, "IShellFolder_Release failed %08lx\n", hr);
744 Cleanup();
746 IMalloc_Free(ppM, newPIDL);
749 /* A simple implementation of an IPropertyBag, which returns fixed values for
750 * 'Target' and 'Attributes' properties.
752 static HRESULT WINAPI InitPropertyBag_IPropertyBag_QueryInterface(IPropertyBag *iface, REFIID riid,
753 void **ppvObject)
755 if (!ppvObject)
756 return E_INVALIDARG;
758 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IPropertyBag, riid)) {
759 *ppvObject = iface;
760 } else {
761 ok (FALSE, "InitPropertyBag asked for unknown interface!\n");
762 return E_NOINTERFACE;
765 IPropertyBag_AddRef(iface);
766 return S_OK;
769 static ULONG WINAPI InitPropertyBag_IPropertyBag_AddRef(IPropertyBag *iface) {
770 return 2;
773 static ULONG WINAPI InitPropertyBag_IPropertyBag_Release(IPropertyBag *iface) {
774 return 1;
777 static HRESULT WINAPI InitPropertyBag_IPropertyBag_Read(IPropertyBag *iface, LPCOLESTR pszPropName,
778 VARIANT *pVar, IErrorLog *pErrorLog)
780 static const WCHAR wszTargetSpecialFolder[] = {
781 'T','a','r','g','e','t','S','p','e','c','i','a','l','F','o','l','d','e','r',0 };
782 static const WCHAR wszTarget[] = {
783 'T','a','r','g','e','t',0 };
784 static const WCHAR wszAttributes[] = {
785 'A','t','t','r','i','b','u','t','e','s',0 };
786 static const WCHAR wszResolveLinkFlags[] = {
787 'R','e','s','o','l','v','e','L','i','n','k','F','l','a','g','s',0 };
789 if (!lstrcmpW(pszPropName, wszTargetSpecialFolder) ||
790 !lstrcmpW(pszPropName, wszResolveLinkFlags))
792 return E_INVALIDARG;
795 if (!lstrcmpW(pszPropName, wszTarget)) {
796 WCHAR wszPath[MAX_PATH];
797 BOOL result;
799 ok(V_VT(pVar) == VT_BSTR, "Wrong variant type for 'Target' property!\n");
800 if (V_VT(pVar) != VT_BSTR) return E_INVALIDARG;
802 result = pSHGetSpecialFolderPathW(NULL, wszPath, CSIDL_DESKTOPDIRECTORY, FALSE);
803 ok(result, "SHGetSpecialFolderPathW(DESKTOPDIRECTORY) failed! x%08lx\n", GetLastError());
804 if (!result) return E_INVALIDARG;
806 V_BSTR(pVar) = SysAllocString(wszPath);
807 return S_OK;
810 if (!lstrcmpW(pszPropName, wszAttributes)) {
811 ok(V_VT(pVar) == VT_UI4, "Wrong variant type for 'Attributes' property!\n");
812 if (V_VT(pVar) != VT_UI4) return E_INVALIDARG;
813 V_UI4(pVar) = SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR|
814 SFGAO_CANRENAME|SFGAO_FILESYSTEM;
815 return S_OK;
818 ok(FALSE, "PropertyBag was asked for unknown property (vt=%d)!\n", V_VT(pVar));
819 return E_INVALIDARG;
822 static HRESULT WINAPI InitPropertyBag_IPropertyBag_Write(IPropertyBag *iface, LPCOLESTR pszPropName,
823 VARIANT *pVar)
825 ok(FALSE, "Unexpected call to IPropertyBag_Write\n");
826 return E_NOTIMPL;
829 static const IPropertyBagVtbl InitPropertyBag_IPropertyBagVtbl = {
830 InitPropertyBag_IPropertyBag_QueryInterface,
831 InitPropertyBag_IPropertyBag_AddRef,
832 InitPropertyBag_IPropertyBag_Release,
833 InitPropertyBag_IPropertyBag_Read,
834 InitPropertyBag_IPropertyBag_Write
837 struct IPropertyBag InitPropertyBag = {
838 &InitPropertyBag_IPropertyBagVtbl
841 void test_FolderShortcut(void) {
842 IPersistPropertyBag *pPersistPropertyBag;
843 IShellFolder *pShellFolder;
844 IPersistFolder3 *pPersistFolder3;
845 HRESULT hr;
846 STRRET strret;
847 WCHAR wszPath[MAX_PATH], wszBuffer[MAX_PATH];
848 BOOL result;
849 CLSID clsid;
850 LPITEMIDLIST pidlCurrentFolder;
852 if (!pSHGetSpecialFolderPathW || !pStrRetToBufW) return;
854 /* These tests basically show, that CLSID_FolderShortcuts are initialized
855 * via their IPersistPropertyBag interface. And that the target folder
856 * is taken from the IPropertyBag's 'Target' property.
858 hr = CoCreateInstance(&CLSID_FolderShortcut, NULL, CLSCTX_INPROC_SERVER,
859 &IID_IPersistPropertyBag, (LPVOID*)&pPersistPropertyBag);
860 ok (SUCCEEDED(hr), "CoCreateInstance failed! hr = 0x%08lx\n", hr);
861 if (FAILED(hr)) return;
863 hr = IPersistPropertyBag_Load(pPersistPropertyBag, &InitPropertyBag, NULL);
864 todo_wine { ok(SUCCEEDED(hr), "IPersistPropertyBag_Load failed! hr = %08lx\n", hr); }
865 if (FAILED(hr)) {
866 IPersistPropertyBag_Release(pPersistPropertyBag);
867 return;
870 hr = IPersistPropertyBag_QueryInterface(pPersistPropertyBag, &IID_IShellFolder,
871 (LPVOID*)&pShellFolder);
872 IPersistPropertyBag_Release(pPersistPropertyBag);
873 ok(SUCCEEDED(hr), "IPersistPropertyBag_QueryInterface(IShellFolder) failed! hr = %08lx\n", hr);
874 if (FAILED(hr)) return;
876 hr = IShellFolder_GetDisplayNameOf(pShellFolder, NULL, SHGDN_FORPARSING, &strret);
877 ok(SUCCEEDED(hr), "IShellFolder_GetDisplayNameOf(NULL) failed! hr = %08lx\n", hr);
878 if (FAILED(hr)) {
879 IShellFolder_Release(pShellFolder);
880 return;
883 result = pSHGetSpecialFolderPathW(NULL, wszPath, CSIDL_DESKTOPDIRECTORY, FALSE);
884 ok(result, "SHGetSpecialFolderPathW(CSIDL_MYDOCUMENTS) failed! 0x%08lx\n", GetLastError());
885 if (!result) return;
887 pStrRetToBufW(&strret, NULL, wszBuffer, MAX_PATH);
888 ok(!lstrcmpiW(wszPath, wszBuffer), "FolderShortcut returned incorrect folder!\n");
890 hr = IShellFolder_QueryInterface(pShellFolder, &IID_IPersistFolder3, (LPVOID*)&pPersistFolder3);
891 IShellFolder_Release(pShellFolder);
892 ok(SUCCEEDED(hr), "IShellFolder_QueryInterface(IID_IPersistFolder3 failed! hr = 0x%08lx\n", hr);
893 if (FAILED(hr)) return;
895 hr = IPersistFolder3_GetClassID(pPersistFolder3, &clsid);
896 ok(SUCCEEDED(hr), "IPersistFolder3_GetClassID failed! hr=0x%08lx\n", hr);
897 ok(IsEqualCLSID(&clsid, &CLSID_FolderShortcut), "Unexpected CLSID!\n");
899 hr = IPersistFolder3_GetCurFolder(pPersistFolder3, &pidlCurrentFolder);
900 ok(SUCCEEDED(hr), "IPersistFolder3_GetCurFolder failed! hr=0x%08lx\n", hr);
901 ok(!pidlCurrentFolder, "IPersistFolder3_GetCurFolder should return a NULL pidl!\n");
903 IPersistFolder3_Release(pPersistFolder3);
906 START_TEST(shlfolder)
908 init_function_pointers();
909 /* if OleInitialize doesn't get called, ParseDisplayName returns
910 CO_E_NOTINITIALIZED for malformed directory names on win2k. */
911 OleInitialize(NULL);
913 test_ParseDisplayName();
914 test_BindToObject();
915 test_EnumObjects_and_CompareIDs();
916 test_GetDisplayName();
917 test_GetAttributesOf();
918 test_SHGetPathFromIDList();
919 test_CallForAttributes();
920 test_FolderShortcut();
922 OleUninitialize();