shell32/unixfs: Added unicode support in folder and file pidls.
[wine/multimedia.git] / dlls / shell32 / tests / shlfolder.c
blob7abeae1d5a2030d66d88db12a2bac87787cb2088
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 #define SFGAO_testfor SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR | SFGAO_CAPABILITYMASK
167 /* Don't test for SFGAO_HASSUBFOLDER since we return real state and native cached */
168 static const ULONG attrs[5] =
170 SFGAO_CAPABILITYMASK | SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR,
171 SFGAO_CAPABILITYMASK | SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR,
172 SFGAO_CAPABILITYMASK | SFGAO_FILESYSTEM,
173 SFGAO_CAPABILITYMASK | SFGAO_FILESYSTEM,
174 SFGAO_CAPABILITYMASK | SFGAO_FILESYSTEM,
177 hr = IShellFolder_EnumObjects(iFolder, NULL, SHCONTF_FOLDERS | SHCONTF_NONFOLDERS | SHCONTF_INCLUDEHIDDEN, &iEnumList);
178 ok(hr == S_OK, "EnumObjects failed %08lx\n", hr);
180 /* This is to show that, contrary to what is said on MSDN, on IEnumIDList::Next,
181 * the filesystem shellfolders return S_OK even if less than 'celt' items are
182 * returned (in contrast to S_FALSE). We have to do it in a loop since WinXP
183 * only ever returns a single entry per call. */
184 while (IEnumIDList_Next(iEnumList, 10-i, &idlArr[i], &NumPIDLs) == S_OK)
185 i += NumPIDLs;
186 ok (i == 5, "i: %d\n", i);
188 hr = IEnumIDList_Release(iEnumList);
189 ok(hr == S_OK, "IEnumIDList_Release failed %08lx\n", hr);
191 /* Sort them first in case of wrong order from system */
192 for (i=0;i<5;i++) for (j=0;j<5;j++)
193 if ((SHORT)IShellFolder_CompareIDs(iFolder, 0, idlArr[i], idlArr[j]) < 0)
195 newPIDL = idlArr[i];
196 idlArr[i] = idlArr[j];
197 idlArr[j] = newPIDL;
200 for (i=0;i<5;i++) for (j=0;j<5;j++)
202 hr = IShellFolder_CompareIDs(iFolder, 0, idlArr[i], idlArr[j]);
203 ok(hr == iResults[i][j], "Got %lx expected [%d]-[%d]=%x\n", hr, i, j, iResults[i][j]);
207 for (i = 0; i < 5; i++)
209 SFGAOF flags;
210 /* Native returns all flags no matter what we ask for */
211 flags = SFGAO_CANCOPY;
212 hr = IShellFolder_GetAttributesOf(iFolder, 1, (LPCITEMIDLIST*)(idlArr + i), &flags);
213 flags &= SFGAO_testfor;
214 ok(hr == S_OK, "GetAttributesOf returns %08lx\n", hr);
215 ok(flags == (attrs[i]), "GetAttributesOf[%i] got %08lx, expected %08lx\n", i, flags, attrs[i]);
217 flags = SFGAO_testfor;
218 hr = IShellFolder_GetAttributesOf(iFolder, 1, (LPCITEMIDLIST*)(idlArr + i), &flags);
219 flags &= SFGAO_testfor;
220 ok(hr == S_OK, "GetAttributesOf returns %08lx\n", hr);
221 ok(flags == attrs[i], "GetAttributesOf[%i] got %08lx, expected %08lx\n", i, flags, attrs[i]);
224 for (i=0;i<5;i++)
225 IMalloc_Free(ppM, idlArr[i]);
228 static void test_BindToObject(void)
230 HRESULT hr;
231 UINT cChars;
232 IShellFolder *psfDesktop, *psfChild, *psfMyComputer, *psfSystemDir;
233 SHITEMID emptyitem = { 0, { 0 } };
234 LPITEMIDLIST pidlMyComputer, pidlSystemDir, pidlEmpty = (LPITEMIDLIST)&emptyitem;
235 WCHAR wszSystemDir[MAX_PATH];
236 char szSystemDir[MAX_PATH];
237 WCHAR wszMyComputer[] = {
238 ':',':','{','2','0','D','0','4','F','E','0','-','3','A','E','A','-','1','0','6','9','-',
239 'A','2','D','8','-','0','8','0','0','2','B','3','0','3','0','9','D','}',0 };
241 /* The following tests shows that BindToObject should fail with E_INVALIDARG if called
242 * with an empty pidl. This is tested for Desktop, MyComputer and the FS ShellFolder
244 hr = SHGetDesktopFolder(&psfDesktop);
245 ok (SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08lx\n", hr);
246 if (FAILED(hr)) return;
248 hr = IShellFolder_BindToObject(psfDesktop, pidlEmpty, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
249 ok (hr == E_INVALIDARG, "Desktop's BindToObject should fail, when called with empty pidl! hr = %08lx\n", hr);
251 hr = IShellFolder_BindToObject(psfDesktop, NULL, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
252 ok (hr == E_INVALIDARG, "Desktop's BindToObject should fail, when called with NULL pidl! hr = %08lx\n", hr);
254 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszMyComputer, NULL, &pidlMyComputer, NULL);
255 ok (SUCCEEDED(hr), "Desktop's ParseDisplayName failed to parse MyComputer's CLSID! hr = %08lx\n", hr);
256 if (FAILED(hr)) {
257 IShellFolder_Release(psfDesktop);
258 return;
261 hr = IShellFolder_BindToObject(psfDesktop, pidlMyComputer, NULL, &IID_IShellFolder, (LPVOID*)&psfMyComputer);
262 ok (SUCCEEDED(hr), "Desktop failed to bind to MyComputer object! hr = %08lx\n", hr);
263 IShellFolder_Release(psfDesktop);
264 IMalloc_Free(ppM, pidlMyComputer);
265 if (FAILED(hr)) return;
267 hr = IShellFolder_BindToObject(psfMyComputer, pidlEmpty, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
268 ok (hr == E_INVALIDARG, "MyComputers's BindToObject should fail, when called with empty pidl! hr = %08lx\n", hr);
270 #if 0
271 /* this call segfaults on 98SE */
272 hr = IShellFolder_BindToObject(psfMyComputer, NULL, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
273 ok (hr == E_INVALIDARG, "MyComputers's BindToObject should fail, when called with NULL pidl! hr = %08lx\n", hr);
274 #endif
276 cChars = GetSystemDirectoryA(szSystemDir, MAX_PATH);
277 ok (cChars > 0 && cChars < MAX_PATH, "GetSystemDirectoryA failed! LastError: %08lx\n", GetLastError());
278 if (cChars == 0 || cChars >= MAX_PATH) {
279 IShellFolder_Release(psfMyComputer);
280 return;
282 MultiByteToWideChar(CP_ACP, 0, szSystemDir, -1, wszSystemDir, MAX_PATH);
284 hr = IShellFolder_ParseDisplayName(psfMyComputer, NULL, NULL, wszSystemDir, NULL, &pidlSystemDir, NULL);
285 ok (SUCCEEDED(hr), "MyComputers's ParseDisplayName failed to parse the SystemDirectory! hr = %08lx\n", hr);
286 if (FAILED(hr)) {
287 IShellFolder_Release(psfMyComputer);
288 return;
291 hr = IShellFolder_BindToObject(psfMyComputer, pidlSystemDir, NULL, &IID_IShellFolder, (LPVOID*)&psfSystemDir);
292 ok (SUCCEEDED(hr), "MyComputer failed to bind to a FileSystem ShellFolder! hr = %08lx\n", hr);
293 IShellFolder_Release(psfMyComputer);
294 IMalloc_Free(ppM, pidlSystemDir);
295 if (FAILED(hr)) return;
297 hr = IShellFolder_BindToObject(psfSystemDir, pidlEmpty, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
298 ok (hr == E_INVALIDARG,
299 "FileSystem ShellFolder's BindToObject should fail, when called with empty pidl! hr = %08lx\n", hr);
301 #if 0
302 /* this call segfaults on 98SE */
303 hr = IShellFolder_BindToObject(psfSystemDir, NULL, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
304 ok (hr == E_INVALIDARG,
305 "FileSystem ShellFolder's BindToObject should fail, when called with NULL pidl! hr = %08lx\n", hr);
306 #endif
308 IShellFolder_Release(psfSystemDir);
311 static void test_GetDisplayName(void)
313 BOOL result;
314 HRESULT hr;
315 HANDLE hTestFile;
316 WCHAR wszTestFile[MAX_PATH], wszTestFile2[MAX_PATH], wszTestDir[MAX_PATH];
317 char szTestFile[MAX_PATH], szTestDir[MAX_PATH];
318 STRRET strret;
319 LPSHELLFOLDER psfDesktop, psfPersonal;
320 IUnknown *psfFile;
321 SHITEMID emptyitem = { 0, { 0 } };
322 LPITEMIDLIST pidlTestFile, pidlEmpty = (LPITEMIDLIST)&emptyitem;
323 LPCITEMIDLIST pidlLast;
324 static const WCHAR wszFileName[] = { 'w','i','n','e','t','e','s','t','.','f','o','o',0 };
325 static const WCHAR wszDirName[] = { 'w','i','n','e','t','e','s','t',0 };
327 /* I'm trying to figure if there is a functional difference between calling
328 * SHGetPathFromIDList and calling GetDisplayNameOf(SHGDN_FORPARSING) after
329 * binding to the shellfolder. One thing I thought of was that perhaps
330 * SHGetPathFromIDList would be able to get the path to a file, which does
331 * not exist anymore, while the other method would'nt. It turns out there's
332 * no functional difference in this respect.
335 if(!pSHGetSpecialFolderPathW) return;
337 /* First creating a directory in MyDocuments and a file in this directory. */
338 result = pSHGetSpecialFolderPathW(NULL, wszTestDir, CSIDL_PERSONAL, FALSE);
339 ok(result, "SHGetSpecialFolderPathW failed! Last error: %08lx\n", GetLastError());
340 if (!result) return;
342 PathAddBackslashW(wszTestDir);
343 lstrcatW(wszTestDir, wszDirName);
344 WideCharToMultiByte(CP_ACP, 0, wszTestDir, -1, szTestDir, MAX_PATH, 0, 0);
345 result = CreateDirectoryA(szTestDir, NULL);
346 ok(result, "CreateDirectoryA failed! Last error: %08lx\n", GetLastError());
347 if (!result) return;
349 lstrcpyW(wszTestFile, wszTestDir);
350 PathAddBackslashW(wszTestFile);
351 lstrcatW(wszTestFile, wszFileName);
352 WideCharToMultiByte(CP_ACP, 0, wszTestFile, -1, szTestFile, MAX_PATH, 0, 0);
354 hTestFile = CreateFileA(szTestFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
355 ok((hTestFile != INVALID_HANDLE_VALUE), "CreateFileA failed! Last error: %08lx\n", GetLastError());
356 if (hTestFile == INVALID_HANDLE_VALUE) return;
357 CloseHandle(hTestFile);
359 /* Getting an itemidlist for the file. */
360 hr = SHGetDesktopFolder(&psfDesktop);
361 ok(SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08lx\n", hr);
362 if (FAILED(hr)) return;
364 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszTestFile, NULL, &pidlTestFile, NULL);
365 ok(SUCCEEDED(hr), "Desktop->ParseDisplayName failed! hr = %08lx\n", hr);
366 if (FAILED(hr)) {
367 IShellFolder_Release(psfDesktop);
368 return;
371 /* WinXP stores the filenames as both ANSI and UNICODE in the pidls */
372 pidlLast = pILFindLastID(pidlTestFile);
373 ok(pidlLast->mkid.cb >=76, "Expected pidl length of at least 76, got %d.\n", pidlLast->mkid.cb);
374 if (pidlLast->mkid.cb >= 76) {
375 ok(!lstrcmpW((WCHAR*)&pidlLast->mkid.abID[46], wszFileName),
376 "WinXP stores the filename as a wchar-string at this position!\n");
379 /* It seems as if we cannot bind to regular files on windows, but only directories.
381 hr = IShellFolder_BindToObject(psfDesktop, pidlTestFile, NULL, &IID_IUnknown, (VOID**)&psfFile);
382 todo_wine { ok (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), "hr = %08lx\n", hr); }
383 if (SUCCEEDED(hr)) {
384 IShellFolder_Release(psfFile);
387 /* Deleting the file and the directory */
388 DeleteFileA(szTestFile);
389 RemoveDirectoryA(szTestDir);
391 /* SHGetPathFromIDListW still works, although the file is not present anymore. */
392 result = SHGetPathFromIDListW(pidlTestFile, wszTestFile2);
393 ok (result, "SHGetPathFromIDListW failed! Last error: %08lx\n", GetLastError());
394 ok (!lstrcmpiW(wszTestFile, wszTestFile2), "SHGetPathFromIDListW returns incorrect path!\n");
396 if(!pSHBindToParent) return;
398 /* SHBindToParent fails, if called with a NULL PIDL. */
399 hr = pSHBindToParent(NULL, &IID_IShellFolder, (VOID**)&psfPersonal, &pidlLast);
400 ok (FAILED(hr), "SHBindToParent(NULL) should fail!\n");
402 /* But it succeeds with an empty PIDL. */
403 hr = pSHBindToParent(pidlEmpty, &IID_IShellFolder, (VOID**)&psfPersonal, &pidlLast);
404 ok (SUCCEEDED(hr), "SHBindToParent(empty PIDL) should succeed! hr = %08lx\n", hr);
405 ok (pidlLast == pidlEmpty, "The last element of an empty PIDL should be the PIDL itself!\n");
406 if (SUCCEEDED(hr))
407 IShellFolder_Release(psfPersonal);
409 /* Binding to the folder and querying the display name of the file also works. */
410 hr = pSHBindToParent(pidlTestFile, &IID_IShellFolder, (VOID**)&psfPersonal, &pidlLast);
411 ok (SUCCEEDED(hr), "SHBindToParent failed! hr = %08lx\n", hr);
412 if (FAILED(hr)) {
413 IShellFolder_Release(psfDesktop);
414 return;
417 /* This test shows that Windows doesn't allocate a new pidlLast, but returns a pointer into
418 * pidlTestFile (In accordance with MSDN). */
419 ok (pILFindLastID(pidlTestFile) == pidlLast,
420 "SHBindToParent doesn't return the last id of the pidl param!\n");
422 hr = IShellFolder_GetDisplayNameOf(psfPersonal, pidlLast, SHGDN_FORPARSING, &strret);
423 ok (SUCCEEDED(hr), "Personal->GetDisplayNameOf failed! hr = %08lx\n", hr);
424 if (FAILED(hr)) {
425 IShellFolder_Release(psfDesktop);
426 IShellFolder_Release(psfPersonal);
427 return;
430 if (pStrRetToBufW)
432 hr = pStrRetToBufW(&strret, pidlLast, wszTestFile2, MAX_PATH);
433 ok (SUCCEEDED(hr), "StrRetToBufW failed! hr = %08lx\n", hr);
434 ok (!lstrcmpiW(wszTestFile, wszTestFile2), "GetDisplayNameOf returns incorrect path!\n");
437 IShellFolder_Release(psfDesktop);
438 IShellFolder_Release(psfPersonal);
441 static void test_CallForAttributes(void)
443 HKEY hKey;
444 LONG lResult;
445 HRESULT hr;
446 DWORD dwSize;
447 LPSHELLFOLDER psfDesktop;
448 LPITEMIDLIST pidlMyDocuments;
449 DWORD dwAttributes, dwCallForAttributes, dwOrigAttributes, dwOrigCallForAttributes;
450 static const WCHAR wszAttributes[] = { 'A','t','t','r','i','b','u','t','e','s',0 };
451 static const WCHAR wszCallForAttributes[] = {
452 'C','a','l','l','F','o','r','A','t','t','r','i','b','u','t','e','s',0 };
453 static const WCHAR wszMyDocumentsKey[] = {
454 'C','L','S','I','D','\\','{','4','5','0','D','8','F','B','A','-','A','D','2','5','-',
455 '1','1','D','0','-','9','8','A','8','-','0','8','0','0','3','6','1','B','1','1','0','3','}',
456 '\\','S','h','e','l','l','F','o','l','d','e','r',0 };
457 WCHAR wszMyDocuments[] = {
458 ':',':','{','4','5','0','D','8','F','B','A','-','A','D','2','5','-','1','1','D','0','-',
459 '9','8','A','8','-','0','8','0','0','3','6','1','B','1','1','0','3','}',0 };
461 /* For the root of a namespace extension, the attributes are not queried by binding
462 * to the object and calling GetAttributesOf. Instead, the attributes are read from
463 * the registry value HKCR/CLSID/{...}/ShellFolder/Attributes. This is documented on MSDN.
465 * The MyDocuments shellfolder on WinXP has a HKCR/CLSID/{...}/ShellFolder/CallForAttributes
466 * value. It seems that if the folder is queried for one of the flags set in CallForAttributes,
467 * the shell does bind to the folder object and calls GetAttributesOf. This is not documented
468 * on MSDN. This test is meant to document the observed behaviour on WinXP SP2.
470 hr = SHGetDesktopFolder(&psfDesktop);
471 ok (SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08lx\n", hr);
472 if (FAILED(hr)) return;
474 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszMyDocuments, NULL,
475 &pidlMyDocuments, NULL);
476 ok (SUCCEEDED(hr),
477 "Desktop's ParseDisplayName failed to parse MyDocuments's CLSID! hr = %08lx\n", hr);
478 if (FAILED(hr)) {
479 IShellFolder_Release(psfDesktop);
480 return;
483 dwAttributes = 0xffffffff;
484 hr = IShellFolder_GetAttributesOf(psfDesktop, 1,
485 (LPCITEMIDLIST*)&pidlMyDocuments, &dwAttributes);
486 ok (SUCCEEDED(hr), "Desktop->GetAttributesOf(MyDocuments) failed! hr = %08lx\n", hr);
488 /* We need the following setup (as observed on WinXP SP2), for the tests to make sense. */
489 ok (dwAttributes & SFGAO_FILESYSTEM, "SFGAO_FILESYSTEM attribute is not set for MyDocuments!\n");
490 ok (!(dwAttributes & SFGAO_ISSLOW), "SFGAO_ISSLOW attribute is set for MyDocuments!\n");
491 ok (!(dwAttributes & SFGAO_GHOSTED), "SFGAO_GHOSTED attribute is set for MyDocuments!\n");
493 /* We don't have the MyDocuments shellfolder in wine yet, and thus we don't have the registry
494 * key. So the test will return at this point, if run on wine.
496 lResult = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszMyDocumentsKey, 0, KEY_WRITE|KEY_READ, &hKey);
497 ok (lResult == ERROR_SUCCESS, "RegOpenKeyEx failed! result: %08lx\n", lResult);
498 if (lResult != ERROR_SUCCESS) {
499 IMalloc_Free(ppM, pidlMyDocuments);
500 IShellFolder_Release(psfDesktop);
501 return;
504 /* Query MyDocuments' Attributes value, to be able to restore it later. */
505 dwSize = sizeof(DWORD);
506 lResult = RegQueryValueExW(hKey, wszAttributes, NULL, NULL, (LPBYTE)&dwOrigAttributes, &dwSize);
507 ok (lResult == ERROR_SUCCESS, "RegQueryValueEx failed! result: %08lx\n", lResult);
508 if (lResult != ERROR_SUCCESS) {
509 RegCloseKey(hKey);
510 IMalloc_Free(ppM, pidlMyDocuments);
511 IShellFolder_Release(psfDesktop);
512 return;
515 /* Query MyDocuments' CallForAttributes value, to be able to restore it later. */
516 dwSize = sizeof(DWORD);
517 lResult = RegQueryValueExW(hKey, wszCallForAttributes, NULL, NULL,
518 (LPBYTE)&dwOrigCallForAttributes, &dwSize);
519 ok (lResult == ERROR_SUCCESS, "RegQueryValueEx failed! result: %08lx\n", lResult);
520 if (lResult != ERROR_SUCCESS) {
521 RegCloseKey(hKey);
522 IMalloc_Free(ppM, pidlMyDocuments);
523 IShellFolder_Release(psfDesktop);
524 return;
527 /* Define via the Attributes value that MyDocuments attributes are SFGAO_ISSLOW and
528 * SFGAO_GHOSTED and that MyDocuments should be called for the SFGAO_ISSLOW and
529 * SFGAO_FILESYSTEM attributes. */
530 dwAttributes = SFGAO_ISSLOW|SFGAO_GHOSTED;
531 RegSetValueExW(hKey, wszAttributes, 0, REG_DWORD, (LPBYTE)&dwAttributes, sizeof(DWORD));
532 dwCallForAttributes = SFGAO_ISSLOW|SFGAO_FILESYSTEM;
533 RegSetValueExW(hKey, wszCallForAttributes, 0, REG_DWORD,
534 (LPBYTE)&dwCallForAttributes, sizeof(DWORD));
536 /* Although it is not set in CallForAttributes, the SFGAO_GHOSTED flag is reset by
537 * GetAttributesOf. It seems that once there is a single attribute queried, for which
538 * CallForAttributes is set, all flags are taken from the GetAttributesOf call and
539 * the flags in Attributes are ignored.
541 dwAttributes = SFGAO_ISSLOW|SFGAO_GHOSTED|SFGAO_FILESYSTEM;
542 hr = IShellFolder_GetAttributesOf(psfDesktop, 1,
543 (LPCITEMIDLIST*)&pidlMyDocuments, &dwAttributes);
544 ok (SUCCEEDED(hr), "Desktop->GetAttributesOf(MyDocuments) failed! hr = %08lx\n", hr);
545 if (SUCCEEDED(hr))
546 ok (dwAttributes == SFGAO_FILESYSTEM,
547 "Desktop->GetAttributes(MyDocuments) returned unexpected attributes: %08lx\n",
548 dwAttributes);
550 /* Restore MyDocuments' original Attributes and CallForAttributes registry values */
551 RegSetValueExW(hKey, wszAttributes, 0, REG_DWORD, (LPBYTE)&dwOrigAttributes, sizeof(DWORD));
552 RegSetValueExW(hKey, wszCallForAttributes, 0, REG_DWORD,
553 (LPBYTE)&dwOrigCallForAttributes, sizeof(DWORD));
554 RegCloseKey(hKey);
555 IMalloc_Free(ppM, pidlMyDocuments);
556 IShellFolder_Release(psfDesktop);
559 static void test_GetAttributesOf(void)
561 HRESULT hr;
562 LPSHELLFOLDER psfDesktop, psfMyComputer;
563 SHITEMID emptyitem = { 0, { 0 } };
564 LPCITEMIDLIST pidlEmpty = (LPCITEMIDLIST)&emptyitem;
565 LPITEMIDLIST pidlMyComputer;
566 DWORD dwFlags;
567 static const DWORD dwDesktopFlags = /* As observed on WinXP SP2 */
568 SFGAO_STORAGE | SFGAO_HASPROPSHEET | SFGAO_STORAGEANCESTOR |
569 SFGAO_FILESYSANCESTOR | SFGAO_FOLDER | SFGAO_FILESYSTEM | SFGAO_HASSUBFOLDER;
570 static const DWORD dwMyComputerFlags = /* As observed on WinXP SP2 */
571 SFGAO_CANRENAME | SFGAO_CANDELETE | SFGAO_HASPROPSHEET |
572 SFGAO_DROPTARGET | SFGAO_FILESYSANCESTOR | SFGAO_FOLDER | SFGAO_HASSUBFOLDER;
573 WCHAR wszMyComputer[] = {
574 ':',':','{','2','0','D','0','4','F','E','0','-','3','A','E','A','-','1','0','6','9','-',
575 'A','2','D','8','-','0','8','0','0','2','B','3','0','3','0','9','D','}',0 };
577 hr = SHGetDesktopFolder(&psfDesktop);
578 ok (SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08lx\n", hr);
579 if (FAILED(hr)) return;
581 /* The Desktop attributes can be queried with a single empty itemidlist, .. */
582 dwFlags = 0xffffffff;
583 hr = IShellFolder_GetAttributesOf(psfDesktop, 1, &pidlEmpty, &dwFlags);
584 ok (SUCCEEDED(hr), "Desktop->GetAttributesOf(empty pidl) failed! hr = %08lx\n", hr);
585 ok (dwFlags == dwDesktopFlags, "Wrong Desktop attributes: %08lx, expected: %08lx\n",
586 dwFlags, dwDesktopFlags);
588 /* .. or with no itemidlist at all. */
589 dwFlags = 0xffffffff;
590 hr = IShellFolder_GetAttributesOf(psfDesktop, 0, NULL, &dwFlags);
591 ok (SUCCEEDED(hr), "Desktop->GetAttributesOf(NULL) failed! hr = %08lx\n", hr);
592 ok (dwFlags == dwDesktopFlags, "Wrong Desktop attributes: %08lx, expected: %08lx\n",
593 dwFlags, dwDesktopFlags);
595 /* Testing the attributes of the MyComputer shellfolder */
596 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszMyComputer, NULL, &pidlMyComputer, NULL);
597 ok (SUCCEEDED(hr), "Desktop's ParseDisplayName failed to parse MyComputer's CLSID! hr = %08lx\n", hr);
598 if (FAILED(hr)) {
599 IShellFolder_Release(psfDesktop);
600 return;
603 /* WinXP SP2 sets the SFGAO_CANLINK flag, when MyComputer is queried via the Desktop
604 * folder object. It doesn't do this, if MyComputer is queried directly (see below).
605 * SFGAO_CANLINK is the same as DROPEFFECT_LINK, which MSDN says means: "Drag source
606 * should create a link to the original data". You can't create links on MyComputer on
607 * Windows, so this flag shouldn't be set. Seems like a bug in Windows. As long as nobody
608 * depends on this bug, we probably shouldn't imitate it.
610 dwFlags = 0xffffffff;
611 hr = IShellFolder_GetAttributesOf(psfDesktop, 1, (LPCITEMIDLIST*)&pidlMyComputer, &dwFlags);
612 ok (SUCCEEDED(hr), "Desktop->GetAttributesOf(MyComputer) failed! hr = %08lx\n", hr);
613 ok ((dwFlags & ~(DWORD)SFGAO_CANLINK) == dwMyComputerFlags,
614 "Wrong MyComputer attributes: %08lx, expected: %08lx\n", dwFlags, dwMyComputerFlags);
616 hr = IShellFolder_BindToObject(psfDesktop, pidlMyComputer, NULL, &IID_IShellFolder, (LPVOID*)&psfMyComputer);
617 ok (SUCCEEDED(hr), "Desktop failed to bind to MyComputer object! hr = %08lx\n", hr);
618 IShellFolder_Release(psfDesktop);
619 IMalloc_Free(ppM, pidlMyComputer);
620 if (FAILED(hr)) return;
622 hr = IShellFolder_GetAttributesOf(psfMyComputer, 1, &pidlEmpty, &dwFlags);
623 todo_wine {ok (hr == E_INVALIDARG, "MyComputer->GetAttributesOf(emtpy pidl) should fail! hr = %08lx\n", hr); }
625 dwFlags = 0xffffffff;
626 hr = IShellFolder_GetAttributesOf(psfMyComputer, 0, NULL, &dwFlags);
627 ok (SUCCEEDED(hr), "MyComputer->GetAttributesOf(NULL) failed! hr = %08lx\n", hr);
628 todo_wine { ok (dwFlags == dwMyComputerFlags,
629 "Wrong MyComputer attributes: %08lx, expected: %08lx\n", dwFlags, dwMyComputerFlags); }
631 IShellFolder_Release(psfMyComputer);
634 static void test_SHGetPathFromIDList(void)
636 SHITEMID emptyitem = { 0, { 0 } };
637 LPCITEMIDLIST pidlEmpty = (LPCITEMIDLIST)&emptyitem;
638 LPITEMIDLIST pidlMyComputer;
639 WCHAR wszPath[MAX_PATH], wszDesktop[MAX_PATH];
640 BOOL result;
641 HRESULT hr;
642 LPSHELLFOLDER psfDesktop;
643 WCHAR wszMyComputer[] = {
644 ':',':','{','2','0','D','0','4','F','E','0','-','3','A','E','A','-','1','0','6','9','-',
645 'A','2','D','8','-','0','8','0','0','2','B','3','0','3','0','9','D','}',0 };
646 WCHAR wszFileName[MAX_PATH];
647 LPITEMIDLIST pidlTestFile;
648 HANDLE hTestFile;
649 STRRET strret;
650 static WCHAR wszTestFile[] = {
651 'w','i','n','e','t','e','s','t','.','f','o','o',0 };
652 HRESULT (WINAPI *pSHGetSpecialFolderLocation)(HWND, int, LPITEMIDLIST *);
653 HMODULE hShell32;
654 LPITEMIDLIST pidlPrograms;
656 if(!pSHGetSpecialFolderPathW) return;
658 /* Calling SHGetPathFromIDList with an empty pidl should return the desktop folder's path. */
659 result = pSHGetSpecialFolderPathW(NULL, wszDesktop, CSIDL_DESKTOP, FALSE);
660 ok(result, "SHGetSpecialFolderPathW(CSIDL_DESKTOP) failed! Last error: %08lx\n", GetLastError());
661 if (!result) return;
663 result = SHGetPathFromIDListW(pidlEmpty, wszPath);
664 ok(result, "SHGetPathFromIDListW failed! Last error: %08lx\n", GetLastError());
665 if (!result) return;
666 ok(!lstrcmpiW(wszDesktop, wszPath), "SHGetPathFromIDList didn't return desktop path for empty pidl!\n");
668 /* MyComputer does not map to a filesystem path. SHGetPathFromIDList should fail. */
669 hr = SHGetDesktopFolder(&psfDesktop);
670 ok (SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08lx\n", hr);
671 if (FAILED(hr)) return;
673 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszMyComputer, NULL, &pidlMyComputer, NULL);
674 ok (SUCCEEDED(hr), "Desktop's ParseDisplayName failed to parse MyComputer's CLSID! hr = %08lx\n", hr);
675 if (FAILED(hr)) {
676 IShellFolder_Release(psfDesktop);
677 return;
680 SetLastError(0xdeadbeef);
681 result = SHGetPathFromIDListW(pidlMyComputer, wszPath);
682 ok (!result, "SHGetPathFromIDList succeeded where it shouldn't!\n");
683 ok (GetLastError()==0xdeadbeef, "SHGetPathFromIDList shouldn't set last error! Last error: %08lx\n", GetLastError());
684 if (result) {
685 IShellFolder_Release(psfDesktop);
686 return;
689 IMalloc_Free(ppM, pidlMyComputer);
691 result = pSHGetSpecialFolderPathW(NULL, wszFileName, CSIDL_DESKTOPDIRECTORY, FALSE);
692 ok(result, "SHGetSpecialFolderPathW failed! Last error: %08lx\n", GetLastError());
693 if (!result) {
694 IShellFolder_Release(psfDesktop);
695 return;
697 PathAddBackslashW(wszFileName);
698 lstrcatW(wszFileName, wszTestFile);
699 hTestFile = CreateFileW(wszFileName, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, NULL);
700 ok(hTestFile != INVALID_HANDLE_VALUE, "CreateFileW failed! Last error: %08lx\n", GetLastError());
701 if (hTestFile == INVALID_HANDLE_VALUE) {
702 IShellFolder_Release(psfDesktop);
703 return;
705 CloseHandle(hTestFile);
707 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszTestFile, NULL, &pidlTestFile, NULL);
708 ok (SUCCEEDED(hr), "Desktop's ParseDisplayName failed to parse filename hr = %08lx\n", hr);
709 if (FAILED(hr)) {
710 IShellFolder_Release(psfDesktop);
711 DeleteFileW(wszFileName);
712 IMalloc_Free(ppM, pidlTestFile);
713 return;
716 /* This test is to show that the Desktop shellfolder prepends the CSIDL_DESKTOPDIRECTORY
717 * path for files placed on the desktop, if called with SHGDN_FORPARSING. */
718 hr = IShellFolder_GetDisplayNameOf(psfDesktop, pidlTestFile, SHGDN_FORPARSING, &strret);
719 ok (SUCCEEDED(hr), "Desktop's GetDisplayNamfOf failed! hr = %08lx\n", hr);
720 IShellFolder_Release(psfDesktop);
721 DeleteFileW(wszFileName);
722 if (FAILED(hr)) {
723 IMalloc_Free(ppM, pidlTestFile);
724 return;
726 if (pStrRetToBufW)
728 pStrRetToBufW(&strret, pidlTestFile, wszPath, MAX_PATH);
729 ok(0 == lstrcmpW(wszFileName, wszPath),
730 "Desktop->GetDisplayNameOf(pidlTestFile, SHGDN_FORPARSING) "
731 "returned incorrect path for file placed on desktop\n");
734 result = SHGetPathFromIDListW(pidlTestFile, wszPath);
735 ok(result, "SHGetPathFromIDListW failed! Last error: %08lx\n", GetLastError());
736 IMalloc_Free(ppM, pidlTestFile);
737 if (!result) return;
738 ok(0 == lstrcmpW(wszFileName, wszPath), "SHGetPathFromIDListW returned incorrect path for file placed on desktop\n");
741 /* Test if we can get the path from the start menu "program files" PIDL. */
742 hShell32 = GetModuleHandleA("shell32");
743 pSHGetSpecialFolderLocation = (HRESULT(WINAPI*)(HWND,int,LPITEMIDLIST*))GetProcAddress(hShell32, "SHGetSpecialFolderLocation");
745 hr = pSHGetSpecialFolderLocation(NULL, CSIDL_PROGRAM_FILES, &pidlPrograms);
746 ok(SUCCEEDED(hr), "SHGetFolderLocation failed: 0x%08lx\n", hr);
748 SetLastError(0xdeadbeef);
749 result = SHGetPathFromIDListW(pidlPrograms, wszPath);
750 IMalloc_Free(ppM, pidlPrograms);
751 ok(result, "SHGetPathFromIDList failed\n");
754 static void test_EnumObjects_and_CompareIDs(void)
756 ITEMIDLIST *newPIDL;
757 IShellFolder *IDesktopFolder, *testIShellFolder;
758 char cCurrDirA [MAX_PATH] = {0};
759 WCHAR cCurrDirW [MAX_PATH];
760 static const WCHAR cTestDirW[] = {'\\','t','e','s','t','d','i','r',0};
761 int len;
762 HRESULT hr;
764 GetCurrentDirectoryA(MAX_PATH, cCurrDirA);
765 len = lstrlenA(cCurrDirA);
767 if(len == 0) {
768 trace("GetCurrentDirectoryA returned empty string. Skipping test_EnumObjects_and_CompareIDs\n");
769 return;
771 if(cCurrDirA[len-1] == '\\')
772 cCurrDirA[len-1] = 0;
774 MultiByteToWideChar(CP_ACP, 0, cCurrDirA, -1, cCurrDirW, MAX_PATH);
775 strcatW(cCurrDirW, cTestDirW);
777 hr = SHGetDesktopFolder(&IDesktopFolder);
778 ok(hr == S_OK, "SHGetDesktopfolder failed %08lx\n", hr);
780 CreateFilesFolders();
782 hr = IShellFolder_ParseDisplayName(IDesktopFolder, NULL, NULL, cCurrDirW, NULL, &newPIDL, 0);
783 ok(hr == S_OK, "ParseDisplayName failed %08lx\n", hr);
785 hr = IShellFolder_BindToObject(IDesktopFolder, newPIDL, NULL, (REFIID)&IID_IShellFolder, (LPVOID *)&testIShellFolder);
786 ok(hr == S_OK, "BindToObject failed %08lx\n", hr);
788 test_EnumObjects(testIShellFolder);
790 hr = IShellFolder_Release(testIShellFolder);
791 ok(hr == S_OK, "IShellFolder_Release failed %08lx\n", hr);
793 Cleanup();
795 IMalloc_Free(ppM, newPIDL);
798 /* A simple implementation of an IPropertyBag, which returns fixed values for
799 * 'Target' and 'Attributes' properties.
801 static HRESULT WINAPI InitPropertyBag_IPropertyBag_QueryInterface(IPropertyBag *iface, REFIID riid,
802 void **ppvObject)
804 if (!ppvObject)
805 return E_INVALIDARG;
807 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IPropertyBag, riid)) {
808 *ppvObject = iface;
809 } else {
810 ok (FALSE, "InitPropertyBag asked for unknown interface!\n");
811 return E_NOINTERFACE;
814 IPropertyBag_AddRef(iface);
815 return S_OK;
818 static ULONG WINAPI InitPropertyBag_IPropertyBag_AddRef(IPropertyBag *iface) {
819 return 2;
822 static ULONG WINAPI InitPropertyBag_IPropertyBag_Release(IPropertyBag *iface) {
823 return 1;
826 static HRESULT WINAPI InitPropertyBag_IPropertyBag_Read(IPropertyBag *iface, LPCOLESTR pszPropName,
827 VARIANT *pVar, IErrorLog *pErrorLog)
829 static const WCHAR wszTargetSpecialFolder[] = {
830 'T','a','r','g','e','t','S','p','e','c','i','a','l','F','o','l','d','e','r',0 };
831 static const WCHAR wszTarget[] = {
832 'T','a','r','g','e','t',0 };
833 static const WCHAR wszAttributes[] = {
834 'A','t','t','r','i','b','u','t','e','s',0 };
835 static const WCHAR wszResolveLinkFlags[] = {
836 'R','e','s','o','l','v','e','L','i','n','k','F','l','a','g','s',0 };
838 if (!lstrcmpW(pszPropName, wszTargetSpecialFolder)) {
839 ok(V_VT(pVar) == VT_I4, "Wrong variant type for 'TargetSpecialFolder' property!\n");
840 return E_INVALIDARG;
843 if (!lstrcmpW(pszPropName, wszResolveLinkFlags))
845 ok(V_VT(pVar) == VT_UI4, "Wrong variant type for 'ResolveLinkFlags' property!\n");
846 return E_INVALIDARG;
849 if (!lstrcmpW(pszPropName, wszTarget)) {
850 WCHAR wszPath[MAX_PATH];
851 BOOL result;
853 ok(V_VT(pVar) == VT_BSTR, "Wrong variant type for 'Target' property!\n");
854 if (V_VT(pVar) != VT_BSTR) return E_INVALIDARG;
856 result = pSHGetSpecialFolderPathW(NULL, wszPath, CSIDL_DESKTOPDIRECTORY, FALSE);
857 ok(result, "SHGetSpecialFolderPathW(DESKTOPDIRECTORY) failed! x%08lx\n", GetLastError());
858 if (!result) return E_INVALIDARG;
860 V_BSTR(pVar) = SysAllocString(wszPath);
861 return S_OK;
864 if (!lstrcmpW(pszPropName, wszAttributes)) {
865 ok(V_VT(pVar) == VT_UI4, "Wrong variant type for 'Attributes' property!\n");
866 if (V_VT(pVar) != VT_UI4) return E_INVALIDARG;
867 V_UI4(pVar) = SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR|
868 SFGAO_CANRENAME|SFGAO_FILESYSTEM;
869 return S_OK;
872 ok(FALSE, "PropertyBag was asked for unknown property (vt=%d)!\n", V_VT(pVar));
873 return E_INVALIDARG;
876 static HRESULT WINAPI InitPropertyBag_IPropertyBag_Write(IPropertyBag *iface, LPCOLESTR pszPropName,
877 VARIANT *pVar)
879 ok(FALSE, "Unexpected call to IPropertyBag_Write\n");
880 return E_NOTIMPL;
883 static const IPropertyBagVtbl InitPropertyBag_IPropertyBagVtbl = {
884 InitPropertyBag_IPropertyBag_QueryInterface,
885 InitPropertyBag_IPropertyBag_AddRef,
886 InitPropertyBag_IPropertyBag_Release,
887 InitPropertyBag_IPropertyBag_Read,
888 InitPropertyBag_IPropertyBag_Write
891 struct IPropertyBag InitPropertyBag = {
892 &InitPropertyBag_IPropertyBagVtbl
895 void test_FolderShortcut(void) {
896 IPersistPropertyBag *pPersistPropertyBag;
897 IShellFolder *pShellFolder, *pDesktopFolder;
898 IPersistFolder3 *pPersistFolder3;
899 HRESULT hr;
900 STRRET strret;
901 WCHAR wszDesktopPath[MAX_PATH], wszBuffer[MAX_PATH];
902 BOOL result;
903 CLSID clsid;
904 LPITEMIDLIST pidlCurrentFolder, pidlWineTestFolder, pidlSubFolder;
905 HKEY hShellExtKey;
906 WCHAR wszWineTestFolder[] = {
907 ':',':','{','9','B','3','5','2','E','B','F','-','2','7','6','5','-','4','5','C','1','-',
908 'B','4','C','6','-','8','5','C','C','7','F','7','A','B','C','6','4','}',0 };
909 WCHAR wszShellExtKey[] = { 'S','o','f','t','w','a','r','e','\\',
910 'M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\',
911 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
912 'E','x','p','l','o','r','e','r','\\','D','e','s','k','t','o','p','\\',
913 'N','a','m','e','S','p','a','c','e','\\',
914 '{','9','b','3','5','2','e','b','f','-','2','7','6','5','-','4','5','c','1','-',
915 'b','4','c','6','-','8','5','c','c','7','f','7','a','b','c','6','4','}',0 };
917 WCHAR wszSomeSubFolder[] = { 'S','u','b','F','o','l','d','e','r', 0};
918 static const GUID CLSID_UnixDosFolder =
919 {0x9d20aae8, 0x0625, 0x44b0, {0x9c, 0xa7, 0x71, 0x88, 0x9c, 0x22, 0x54, 0xd9}};
921 if (!pSHGetSpecialFolderPathW || !pStrRetToBufW) return;
923 /* These tests basically show, that CLSID_FolderShortcuts are initialized
924 * via their IPersistPropertyBag interface. And that the target folder
925 * is taken from the IPropertyBag's 'Target' property.
927 hr = CoCreateInstance(&CLSID_FolderShortcut, NULL, CLSCTX_INPROC_SERVER,
928 &IID_IPersistPropertyBag, (LPVOID*)&pPersistPropertyBag);
929 ok (SUCCEEDED(hr), "CoCreateInstance failed! hr = 0x%08lx\n", hr);
930 if (FAILED(hr)) return;
932 hr = IPersistPropertyBag_Load(pPersistPropertyBag, &InitPropertyBag, NULL);
933 ok(SUCCEEDED(hr), "IPersistPropertyBag_Load failed! hr = %08lx\n", hr);
934 if (FAILED(hr)) {
935 IPersistPropertyBag_Release(pPersistPropertyBag);
936 return;
939 hr = IPersistPropertyBag_QueryInterface(pPersistPropertyBag, &IID_IShellFolder,
940 (LPVOID*)&pShellFolder);
941 IPersistPropertyBag_Release(pPersistPropertyBag);
942 ok(SUCCEEDED(hr), "IPersistPropertyBag_QueryInterface(IShellFolder) failed! hr = %08lx\n", hr);
943 if (FAILED(hr)) return;
945 hr = IShellFolder_GetDisplayNameOf(pShellFolder, NULL, SHGDN_FORPARSING, &strret);
946 ok(SUCCEEDED(hr), "IShellFolder_GetDisplayNameOf(NULL) failed! hr = %08lx\n", hr);
947 if (FAILED(hr)) {
948 IShellFolder_Release(pShellFolder);
949 return;
952 result = pSHGetSpecialFolderPathW(NULL, wszDesktopPath, CSIDL_DESKTOPDIRECTORY, FALSE);
953 ok(result, "SHGetSpecialFolderPathW(CSIDL_DESKTOPDIRECTORY) failed! 0x%08lx\n", GetLastError());
954 if (!result) return;
956 pStrRetToBufW(&strret, NULL, wszBuffer, MAX_PATH);
957 ok(!lstrcmpiW(wszDesktopPath, wszBuffer), "FolderShortcut returned incorrect folder!\n");
959 hr = IShellFolder_QueryInterface(pShellFolder, &IID_IPersistFolder3, (LPVOID*)&pPersistFolder3);
960 IShellFolder_Release(pShellFolder);
961 ok(SUCCEEDED(hr), "IShellFolder_QueryInterface(IID_IPersistFolder3 failed! hr = 0x%08lx\n", hr);
962 if (FAILED(hr)) return;
964 hr = IPersistFolder3_GetClassID(pPersistFolder3, &clsid);
965 ok(SUCCEEDED(hr), "IPersistFolder3_GetClassID failed! hr=0x%08lx\n", hr);
966 ok(IsEqualCLSID(&clsid, &CLSID_FolderShortcut), "Unexpected CLSID!\n");
968 hr = IPersistFolder3_GetCurFolder(pPersistFolder3, &pidlCurrentFolder);
969 ok(SUCCEEDED(hr), "IPersistFolder3_GetCurFolder failed! hr=0x%08lx\n", hr);
970 ok(!pidlCurrentFolder, "IPersistFolder3_GetCurFolder should return a NULL pidl!\n");
972 /* For FolderShortcut objects, the Initialize method initialized the folder's position in the
973 * shell namespace. The target folder, read from the property bag above, remains untouched.
974 * The following tests show this: The itemidlist for some imaginary shellfolder object
975 * is created and the FolderShortcut is initialized with it. GetCurFolder now returns this
976 * itemidlist, but GetDisplayNameOf still returns the path from above.
978 hr = SHGetDesktopFolder(&pDesktopFolder);
979 ok (SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08lx\n", hr);
980 if (FAILED(hr)) return;
982 /* Temporarily register WineTestFolder as a shell namespace extension at the Desktop.
983 * Otherwise ParseDisplayName fails on WinXP with E_INVALIDARG */
984 RegCreateKeyW(HKEY_CURRENT_USER, wszShellExtKey, &hShellExtKey);
985 RegCloseKey(hShellExtKey);
986 hr = IShellFolder_ParseDisplayName(pDesktopFolder, NULL, NULL, wszWineTestFolder, NULL,
987 &pidlWineTestFolder, NULL);
988 RegDeleteKeyW(HKEY_CURRENT_USER, wszShellExtKey);
989 IShellFolder_Release(pDesktopFolder);
990 ok (SUCCEEDED(hr), "IShellFolder::ParseDisplayName failed! hr = %08lx\n", hr);
991 if (FAILED(hr)) return;
993 hr = IPersistFolder3_Initialize(pPersistFolder3, pidlWineTestFolder);
994 ok (SUCCEEDED(hr), "IPersistFolder3::Initialize failed! hr = %08lx\n", hr);
995 if (FAILED(hr)) {
996 IPersistFolder3_Release(pPersistFolder3);
997 ILFree(pidlWineTestFolder);
998 return;
1001 hr = IPersistFolder3_GetCurFolder(pPersistFolder3, &pidlCurrentFolder);
1002 ok(SUCCEEDED(hr), "IPersistFolder3_GetCurFolder failed! hr=0x%08lx\n", hr);
1003 ok(ILIsEqual(pidlCurrentFolder, pidlWineTestFolder),
1004 "IPersistFolder3_GetCurFolder should return pidlWineTestFolder!\n");
1005 ILFree(pidlCurrentFolder);
1006 ILFree(pidlWineTestFolder);
1008 hr = IPersistFolder3_QueryInterface(pPersistFolder3, &IID_IShellFolder, (LPVOID*)&pShellFolder);
1009 IPersistFolder3_Release(pPersistFolder3);
1010 ok(SUCCEEDED(hr), "IPersistFolder3_QueryInterface(IShellFolder) failed! hr = %08lx\n", hr);
1011 if (FAILED(hr)) return;
1013 hr = IShellFolder_GetDisplayNameOf(pShellFolder, NULL, SHGDN_FORPARSING, &strret);
1014 ok(SUCCEEDED(hr), "IShellFolder_GetDisplayNameOf(NULL) failed! hr = %08lx\n", hr);
1015 if (FAILED(hr)) {
1016 IShellFolder_Release(pShellFolder);
1017 return;
1020 pStrRetToBufW(&strret, NULL, wszBuffer, MAX_PATH);
1021 ok(!lstrcmpiW(wszDesktopPath, wszBuffer), "FolderShortcut returned incorrect folder!\n");
1023 /* Next few lines are meant to show that children of FolderShortcuts are not FolderShortcuts,
1024 * but ShellFSFolders. */
1025 PathAddBackslashW(wszDesktopPath);
1026 lstrcatW(wszDesktopPath, wszSomeSubFolder);
1027 if (!CreateDirectoryW(wszDesktopPath, NULL)) {
1028 IShellFolder_Release(pShellFolder);
1029 return;
1032 hr = IShellFolder_ParseDisplayName(pShellFolder, NULL, NULL, wszSomeSubFolder, NULL,
1033 &pidlSubFolder, NULL);
1034 RemoveDirectoryW(wszDesktopPath);
1035 ok (SUCCEEDED(hr), "IShellFolder::ParseDisplayName failed! hr = %08lx\n", hr);
1036 if (FAILED(hr)) {
1037 IShellFolder_Release(pShellFolder);
1038 return;
1041 hr = IShellFolder_BindToObject(pShellFolder, pidlSubFolder, NULL, &IID_IPersistFolder3,
1042 (LPVOID*)&pPersistFolder3);
1043 IShellFolder_Release(pShellFolder);
1044 ILFree(pidlSubFolder);
1045 ok (SUCCEEDED(hr), "IShellFolder::BindToObject failed! hr = %08lx\n", hr);
1046 if (FAILED(hr))
1047 return;
1049 /* On windows, we expect CLSID_ShellFSFolder. On wine we relax this constraint
1050 * a little bit and also allow CLSID_UnixDosFolder. */
1051 hr = IPersistFolder3_GetClassID(pPersistFolder3, &clsid);
1052 ok(SUCCEEDED(hr), "IPersistFolder3_GetClassID failed! hr=0x%08lx\n", hr);
1053 ok(IsEqualCLSID(&clsid, &CLSID_ShellFSFolder) || IsEqualCLSID(&clsid, &CLSID_UnixDosFolder),
1054 "IPersistFolder3::GetClassID returned unexpected CLSID!\n");
1056 IPersistFolder3_Release(pPersistFolder3);
1059 #include "pshpack1.h"
1060 struct FileStructA {
1061 BYTE type;
1062 BYTE dummy;
1063 DWORD dwFileSize;
1064 WORD uFileDate; /* In our current implementation this is */
1065 WORD uFileTime; /* FileTimeToDosDate(WIN32_FIND_DATA->ftLastWriteTime) */
1066 WORD uFileAttribs;
1067 CHAR szName[1];
1070 struct FileStructW {
1071 WORD cbLen; /* Length of this element. */
1072 BYTE abFooBar1[6]; /* Beyond any recognition. */
1073 WORD uDate; /* FileTimeToDosDate(WIN32_FIND_DATA->ftCreationTime)? */
1074 WORD uTime; /* (this is currently speculation) */
1075 WORD uDate2; /* FileTimeToDosDate(WIN32_FIND_DATA->ftLastAccessTime)? */
1076 WORD uTime2; /* (this is currently speculation) */
1077 BYTE abFooBar2[4]; /* Beyond any recognition. */
1078 WCHAR wszName[1]; /* The long filename in unicode. */
1079 /* Just for documentation: Right after the unicode string: */
1080 WORD cbOffset; /* FileStructW's offset from the beginning of the SHITMEID.
1081 * SHITEMID->cb == uOffset + cbLen */
1083 #include "poppack.h"
1085 void test_ITEMIDLIST_format(void) {
1086 WCHAR wszPersonal[MAX_PATH];
1087 LPSHELLFOLDER psfDesktop, psfPersonal;
1088 LPITEMIDLIST pidlPersonal, pidlFile;
1089 HANDLE hFile;
1090 HRESULT hr;
1091 BOOL bResult;
1092 WCHAR wszFile[3][17] = { { 'e','v','e','n','_',0 }, { 'o','d','d','_',0 },
1093 { 'l','o','n','g','e','r','_','t','h','a','n','.','8','_','3',0 } };
1094 int i;
1096 if(!pSHGetSpecialFolderPathW) return;
1098 bResult = pSHGetSpecialFolderPathW(NULL, wszPersonal, CSIDL_PERSONAL, FALSE);
1099 ok(bResult, "SHGetSpecialFolderPathW failed! Last error: %08lx\n", GetLastError());
1100 if (!bResult) return;
1102 bResult = SetCurrentDirectoryW(wszPersonal);
1103 ok(bResult, "SetCurrentDirectory failed! Last error: %ld\n", GetLastError());
1104 if (!bResult) return;
1106 hr = SHGetDesktopFolder(&psfDesktop);
1107 ok(SUCCEEDED(hr), "SHGetDesktopFolder failed! hr: %08lx\n", hr);
1108 if (FAILED(hr)) return;
1110 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszPersonal, NULL, &pidlPersonal, NULL);
1111 ok(SUCCEEDED(hr), "psfDesktop->ParseDisplayName failed! hr = %08lx\n", hr);
1112 if (FAILED(hr)) {
1113 IShellFolder_Release(psfDesktop);
1114 return;
1117 hr = IShellFolder_BindToObject(psfDesktop, pidlPersonal, NULL, &IID_IShellFolder,
1118 (LPVOID*)&psfPersonal);
1119 IShellFolder_Release(psfDesktop);
1120 ILFree(pidlPersonal);
1121 ok(SUCCEEDED(hr), "psfDesktop->BindToObject failed! hr = %08lx\n", hr);
1122 if (FAILED(hr)) return;
1124 for (i=0; i<3; i++) {
1125 CHAR szFile[MAX_PATH];
1126 struct FileStructA *pFileStructA;
1127 WORD cbOffset;
1129 WideCharToMultiByte(CP_ACP, 0, wszFile[i], -1, szFile, MAX_PATH, NULL, NULL);
1131 hFile = CreateFileW(wszFile[i], GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_FLAG_WRITE_THROUGH, NULL);
1132 ok(hFile != INVALID_HANDLE_VALUE, "CreateFile failed! (%ld)\n", GetLastError());
1133 if (hFile == INVALID_HANDLE_VALUE) {
1134 IShellFolder_Release(psfPersonal);
1135 return;
1137 CloseHandle(hFile);
1139 hr = IShellFolder_ParseDisplayName(psfPersonal, NULL, NULL, wszFile[i], NULL, &pidlFile, NULL);
1140 DeleteFileW(wszFile[i]);
1141 ok(SUCCEEDED(hr), "psfPersonal->ParseDisplayName failed! hr: %08lx\n", hr);
1142 if (FAILED(hr)) {
1143 IShellFolder_Release(psfPersonal);
1144 return;
1147 pFileStructA = (struct FileStructA *)pidlFile->mkid.abID;
1148 ok(pFileStructA->type == 0x32, "PIDLTYPE should be 0x32!\n");
1149 ok(pFileStructA->dummy == 0x00, "Dummy Byte should be 0x00!\n");
1150 ok(pFileStructA->dwFileSize == 0, "Filesize should be zero!\n");
1152 if (i < 2) /* First two file names are already in valid 8.3 format */
1153 ok(!strcmp(szFile, (CHAR*)&pidlFile->mkid.abID[12]), "Wrong file name!\n");
1154 else
1155 /* WinXP stores a derived 8.3 dos name (LONGER~1.8_3) here. We probably
1156 * can't implement this correctly, since unix filesystems don't support
1157 * this nasty short/long filename stuff. So we'll probably stay with our
1158 * current habbit of storing the long filename here, which seems to work
1159 * just fine. */
1160 todo_wine { ok(pidlFile->mkid.abID[18] == '~', "Should be derived 8.3 name!\n"); }
1162 if (i == 0) /* First file name has an even number of chars. No need for alignment. */
1163 ok(pidlFile->mkid.abID[12 + strlen(szFile) + 1] != '\0',
1164 "Alignment byte, where there shouldn't be!\n");
1166 if (i == 1) /* Second file name has an uneven number of chars => alignment byte */
1167 ok(pidlFile->mkid.abID[12 + strlen(szFile) + 1] == '\0',
1168 "There should be an alignment byte, but isn't!\n");
1170 /* The offset of the FileStructW member is stored as a WORD at the end of the pidl. */
1171 cbOffset = *(WORD*)(((LPBYTE)pidlFile)+pidlFile->mkid.cb-sizeof(WORD));
1172 ok (cbOffset >= sizeof(struct FileStructA) &&
1173 cbOffset <= pidlFile->mkid.cb - sizeof(struct FileStructW),
1174 "Wrong offset value (%d) stored at the end of the PIDL\n", cbOffset);
1176 if (cbOffset >= sizeof(struct FileStructA) &&
1177 cbOffset <= pidlFile->mkid.cb - sizeof(struct FileStructW))
1179 struct FileStructW *pFileStructW = (struct FileStructW *)(((LPBYTE)pidlFile)+cbOffset);
1181 ok(pidlFile->mkid.cb == cbOffset + pFileStructW->cbLen,
1182 "FileStructW's offset and length should add up to the PIDL's length!\n");
1184 if (pidlFile->mkid.cb == cbOffset + pFileStructW->cbLen) {
1185 /* Since we just created the file, time of creation,
1186 * time of last access and time of last write access just be the same.
1187 * These tests seem to fail sometimes (on WinXP), if the test is run again shortly
1188 * after the first run. I do remember something with NTFS keeping the creation time
1189 * if a file is deleted and then created again within a couple of seconds or so.
1190 * Might be the reason. */
1191 ok (pFileStructA->uFileDate == pFileStructW->uDate &&
1192 pFileStructA->uFileTime == pFileStructW->uTime,
1193 "Last write time should match creation time!\n");
1195 ok (pFileStructA->uFileDate == pFileStructW->uDate2 &&
1196 pFileStructA->uFileTime == pFileStructW->uTime2,
1197 "Last write time should match last access time!\n");
1199 ok (!lstrcmpW(wszFile[i], pFileStructW->wszName),
1200 "The filename should be stored in unicode at this position!\n");
1204 ILFree(pidlFile);
1208 START_TEST(shlfolder)
1210 init_function_pointers();
1211 /* if OleInitialize doesn't get called, ParseDisplayName returns
1212 CO_E_NOTINITIALIZED for malformed directory names on win2k. */
1213 OleInitialize(NULL);
1215 test_ParseDisplayName();
1216 test_BindToObject();
1217 test_EnumObjects_and_CompareIDs();
1218 test_GetDisplayName();
1219 test_GetAttributesOf();
1220 test_SHGetPathFromIDList();
1221 test_CallForAttributes();
1222 test_FolderShortcut();
1223 test_ITEMIDLIST_format();
1225 OleUninitialize();