shell32/tests: Use the correct directory on Win95.
[wine/multimedia.git] / dlls / shell32 / tests / shlfolder.c
blob7a29626949f9fcc2f75ac2fb86bfde281de76a74
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
22 #include <stdio.h>
24 #define COBJMACROS
25 #define CONST_VTABLE
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wtypes.h"
30 #include "shellapi.h"
33 #include "shlguid.h"
34 #include "shlobj.h"
35 #include "shobjidl.h"
36 #include "shlwapi.h"
37 #include "ocidl.h"
38 #include "oleauto.h"
40 #include "wine/test.h"
43 static IMalloc *ppM;
45 static HRESULT (WINAPI *pSHBindToParent)(LPCITEMIDLIST, REFIID, LPVOID*, LPCITEMIDLIST*);
46 static HRESULT (WINAPI *pSHGetFolderPathA)(HWND, int, HANDLE, DWORD, LPSTR);
47 static HRESULT (WINAPI *pSHGetFolderPathAndSubDirA)(HWND, int, HANDLE, DWORD, LPCSTR, LPSTR);
48 static BOOL (WINAPI *pSHGetPathFromIDListW)(LPCITEMIDLIST,LPWSTR);
49 static BOOL (WINAPI *pSHGetSpecialFolderPathA)(HWND, LPSTR, int, BOOL);
50 static BOOL (WINAPI *pSHGetSpecialFolderPathW)(HWND, LPWSTR, int, BOOL);
51 static HRESULT (WINAPI *pStrRetToBufW)(STRRET*,LPCITEMIDLIST,LPWSTR,UINT);
52 static LPITEMIDLIST (WINAPI *pILFindLastID)(LPCITEMIDLIST);
53 static void (WINAPI *pILFree)(LPITEMIDLIST);
54 static BOOL (WINAPI *pILIsEqual)(LPCITEMIDLIST, LPCITEMIDLIST);
56 static void init_function_pointers(void)
58 HMODULE hmod;
59 HRESULT hr;
61 hmod = GetModuleHandleA("shell32.dll");
62 pSHBindToParent = (void*)GetProcAddress(hmod, "SHBindToParent");
63 pSHGetFolderPathA = (void*)GetProcAddress(hmod, "SHGetFolderPathA");
64 pSHGetFolderPathAndSubDirA = (void*)GetProcAddress(hmod, "SHGetFolderPathAndSubDirA");
65 pSHGetPathFromIDListW = (void*)GetProcAddress(hmod, "SHGetPathFromIDListW");
66 pSHGetSpecialFolderPathA = (void*)GetProcAddress(hmod, "SHGetSpecialFolderPathA");
67 pSHGetSpecialFolderPathW = (void*)GetProcAddress(hmod, "SHGetSpecialFolderPathW");
68 pILFindLastID = (void *)GetProcAddress(hmod, (LPCSTR)16);
69 pILFree = (void*)GetProcAddress(hmod, (LPSTR)155);
70 pILIsEqual = (void*)GetProcAddress(hmod, (LPSTR)21);
72 hmod = GetModuleHandleA("shlwapi.dll");
73 pStrRetToBufW = (void*)GetProcAddress(hmod, "StrRetToBufW");
75 hr = SHGetMalloc(&ppM);
76 ok(hr == S_OK, "SHGetMalloc failed %08x\n", hr);
79 static void test_ParseDisplayName(void)
81 HRESULT hr;
82 IShellFolder *IDesktopFolder;
83 static const char *cNonExistDir1A = "c:\\nonexist_subdir";
84 static const char *cNonExistDir2A = "c:\\\\nonexist_subdir";
85 static const char *cInetTestA = "http:\\yyy";
86 static const char *cInetTest2A = "xx:yyy";
87 DWORD res;
88 WCHAR cTestDirW [MAX_PATH] = {0};
89 ITEMIDLIST *newPIDL;
90 BOOL bRes;
92 hr = SHGetDesktopFolder(&IDesktopFolder);
93 if(hr != S_OK) return;
95 MultiByteToWideChar(CP_ACP, 0, cInetTestA, -1, cTestDirW, MAX_PATH);
96 hr = IShellFolder_ParseDisplayName(IDesktopFolder,
97 NULL, NULL, cTestDirW, NULL, &newPIDL, 0);
98 todo_wine ok((SUCCEEDED(hr) || broken(hr == E_FAIL) /* NT4 */),
99 "ParseDisplayName returned %08x, expected SUCCESS or E_FAIL\n", hr);
100 if (SUCCEEDED(hr))
102 ok(pILFindLastID(newPIDL)->mkid.abID[0] == 0x61, "Last pidl should be of type "
103 "PT_IESPECIAL1, but is: %02x\n", pILFindLastID(newPIDL)->mkid.abID[0]);
104 IMalloc_Free(ppM, newPIDL);
107 MultiByteToWideChar(CP_ACP, 0, cInetTest2A, -1, cTestDirW, MAX_PATH);
108 hr = IShellFolder_ParseDisplayName(IDesktopFolder,
109 NULL, NULL, cTestDirW, NULL, &newPIDL, 0);
110 todo_wine ok((SUCCEEDED(hr) || broken(hr == E_FAIL) /* NT4 */),
111 "ParseDisplayName returned %08x, expected SUCCESS or E_FAIL\n", hr);
112 if (SUCCEEDED(hr))
114 ok(pILFindLastID(newPIDL)->mkid.abID[0] == 0x61, "Last pidl should be of type "
115 "PT_IESPECIAL1, but is: %02x\n", pILFindLastID(newPIDL)->mkid.abID[0]);
116 IMalloc_Free(ppM, newPIDL);
119 res = GetFileAttributesA(cNonExistDir1A);
120 if(res != INVALID_FILE_ATTRIBUTES) return;
122 MultiByteToWideChar(CP_ACP, 0, cNonExistDir1A, -1, cTestDirW, MAX_PATH);
123 hr = IShellFolder_ParseDisplayName(IDesktopFolder,
124 NULL, NULL, cTestDirW, NULL, &newPIDL, 0);
125 ok((hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) || (hr == E_FAIL),
126 "ParseDisplayName returned %08x, expected 80070002 or E_FAIL\n", hr);
128 res = GetFileAttributesA(cNonExistDir2A);
129 if(res != INVALID_FILE_ATTRIBUTES) return;
131 MultiByteToWideChar(CP_ACP, 0, cNonExistDir2A, -1, cTestDirW, MAX_PATH);
132 hr = IShellFolder_ParseDisplayName(IDesktopFolder,
133 NULL, NULL, cTestDirW, NULL, &newPIDL, 0);
134 ok((hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) || (hr == E_FAIL) || (hr == E_INVALIDARG),
135 "ParseDisplayName returned %08x, expected 80070002, E_FAIL or E_INVALIDARG\n", hr);
137 /* I thought that perhaps the DesktopFolder's ParseDisplayName would recognize the
138 * path corresponding to CSIDL_PERSONAL and return a CLSID_MyDocuments PIDL. Turns
139 * out it doesn't. The magic seems to happen in the file dialogs, then. */
140 if (!pSHGetSpecialFolderPathW || !pILFindLastID) goto finished;
142 bRes = pSHGetSpecialFolderPathW(NULL, cTestDirW, CSIDL_PERSONAL, FALSE);
143 ok(bRes, "SHGetSpecialFolderPath(CSIDL_PERSONAL) failed! %u\n", GetLastError());
144 if (!bRes) goto finished;
146 hr = IShellFolder_ParseDisplayName(IDesktopFolder, NULL, NULL, cTestDirW, NULL, &newPIDL, 0);
147 ok(SUCCEEDED(hr), "DesktopFolder->ParseDisplayName failed. hr = %08x.\n", hr);
148 if (FAILED(hr)) goto finished;
150 ok(pILFindLastID(newPIDL)->mkid.abID[0] == 0x31, "Last pidl should be of type "
151 "PT_FOLDER, but is: %02x\n", pILFindLastID(newPIDL)->mkid.abID[0]);
152 IMalloc_Free(ppM, newPIDL);
154 finished:
155 IShellFolder_Release(IDesktopFolder);
158 /* creates a file with the specified name for tests */
159 static void CreateTestFile(const CHAR *name)
161 HANDLE file;
162 DWORD written;
164 file = CreateFileA(name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
165 if (file != INVALID_HANDLE_VALUE)
167 WriteFile(file, name, strlen(name), &written, NULL);
168 WriteFile(file, "\n", strlen("\n"), &written, NULL);
169 CloseHandle(file);
174 /* initializes the tests */
175 static void CreateFilesFolders(void)
177 CreateDirectoryA(".\\testdir", NULL);
178 CreateDirectoryA(".\\testdir\\test.txt", NULL);
179 CreateTestFile (".\\testdir\\test1.txt ");
180 CreateTestFile (".\\testdir\\test2.txt ");
181 CreateTestFile (".\\testdir\\test3.txt ");
182 CreateDirectoryA(".\\testdir\\testdir2 ", NULL);
183 CreateDirectoryA(".\\testdir\\testdir2\\subdir", NULL);
186 /* cleans after tests */
187 static void Cleanup(void)
189 DeleteFileA(".\\testdir\\test1.txt");
190 DeleteFileA(".\\testdir\\test2.txt");
191 DeleteFileA(".\\testdir\\test3.txt");
192 RemoveDirectoryA(".\\testdir\\test.txt");
193 RemoveDirectoryA(".\\testdir\\testdir2\\subdir");
194 RemoveDirectoryA(".\\testdir\\testdir2");
195 RemoveDirectoryA(".\\testdir");
199 /* perform test */
200 static void test_EnumObjects(IShellFolder *iFolder)
202 IEnumIDList *iEnumList;
203 LPITEMIDLIST newPIDL, idlArr[10];
204 ULONG NumPIDLs;
205 int i=0, j;
206 HRESULT hr;
208 static const WORD iResults [5][5] =
210 { 0,-1,-1,-1,-1},
211 { 1, 0,-1,-1,-1},
212 { 1, 1, 0,-1,-1},
213 { 1, 1, 1, 0,-1},
214 { 1, 1, 1, 1, 0}
217 #define SFGAO_testfor SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR | SFGAO_CAPABILITYMASK
218 /* Don't test for SFGAO_HASSUBFOLDER since we return real state and native cached */
219 static const ULONG attrs[5] =
221 SFGAO_CAPABILITYMASK | SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR,
222 SFGAO_CAPABILITYMASK | SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR,
223 SFGAO_CAPABILITYMASK | SFGAO_FILESYSTEM,
224 SFGAO_CAPABILITYMASK | SFGAO_FILESYSTEM,
225 SFGAO_CAPABILITYMASK | SFGAO_FILESYSTEM,
228 hr = IShellFolder_EnumObjects(iFolder, NULL, SHCONTF_FOLDERS | SHCONTF_NONFOLDERS | SHCONTF_INCLUDEHIDDEN, &iEnumList);
229 ok(hr == S_OK, "EnumObjects failed %08x\n", hr);
231 /* This is to show that, contrary to what is said on MSDN, on IEnumIDList::Next,
232 * the filesystem shellfolders return S_OK even if less than 'celt' items are
233 * returned (in contrast to S_FALSE). We have to do it in a loop since WinXP
234 * only ever returns a single entry per call. */
235 while (IEnumIDList_Next(iEnumList, 10-i, &idlArr[i], &NumPIDLs) == S_OK)
236 i += NumPIDLs;
237 ok (i == 5, "i: %d\n", i);
239 hr = IEnumIDList_Release(iEnumList);
240 ok(hr == S_OK, "IEnumIDList_Release failed %08x\n", hr);
242 /* Sort them first in case of wrong order from system */
243 for (i=0;i<5;i++) for (j=0;j<5;j++)
244 if ((SHORT)IShellFolder_CompareIDs(iFolder, 0, idlArr[i], idlArr[j]) < 0)
246 newPIDL = idlArr[i];
247 idlArr[i] = idlArr[j];
248 idlArr[j] = newPIDL;
251 for (i=0;i<5;i++) for (j=0;j<5;j++)
253 hr = IShellFolder_CompareIDs(iFolder, 0, idlArr[i], idlArr[j]);
254 ok(hr == iResults[i][j], "Got %x expected [%d]-[%d]=%x\n", hr, i, j, iResults[i][j]);
258 for (i = 0; i < 5; i++)
260 SFGAOF flags;
261 /* Native returns all flags no matter what we ask for */
262 flags = SFGAO_CANCOPY;
263 hr = IShellFolder_GetAttributesOf(iFolder, 1, (LPCITEMIDLIST*)(idlArr + i), &flags);
264 flags &= SFGAO_testfor;
265 ok(hr == S_OK, "GetAttributesOf returns %08x\n", hr);
266 ok(flags == (attrs[i]), "GetAttributesOf[%i] got %08x, expected %08x\n", i, flags, attrs[i]);
268 flags = SFGAO_testfor;
269 hr = IShellFolder_GetAttributesOf(iFolder, 1, (LPCITEMIDLIST*)(idlArr + i), &flags);
270 flags &= SFGAO_testfor;
271 ok(hr == S_OK, "GetAttributesOf returns %08x\n", hr);
272 ok(flags == attrs[i], "GetAttributesOf[%i] got %08x, expected %08x\n", i, flags, attrs[i]);
275 for (i=0;i<5;i++)
276 IMalloc_Free(ppM, idlArr[i]);
279 static void test_BindToObject(void)
281 HRESULT hr;
282 UINT cChars;
283 IShellFolder *psfDesktop, *psfChild, *psfMyComputer, *psfSystemDir;
284 SHITEMID emptyitem = { 0, { 0 } };
285 LPITEMIDLIST pidlMyComputer, pidlSystemDir, pidlEmpty = (LPITEMIDLIST)&emptyitem;
286 WCHAR wszSystemDir[MAX_PATH];
287 char szSystemDir[MAX_PATH];
288 WCHAR wszMyComputer[] = {
289 ':',':','{','2','0','D','0','4','F','E','0','-','3','A','E','A','-','1','0','6','9','-',
290 'A','2','D','8','-','0','8','0','0','2','B','3','0','3','0','9','D','}',0 };
292 /* The following tests shows that BindToObject should fail with E_INVALIDARG if called
293 * with an empty pidl. This is tested for Desktop, MyComputer and the FS ShellFolder
295 hr = SHGetDesktopFolder(&psfDesktop);
296 ok (SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08x\n", hr);
297 if (FAILED(hr)) return;
299 hr = IShellFolder_BindToObject(psfDesktop, pidlEmpty, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
300 ok (hr == E_INVALIDARG, "Desktop's BindToObject should fail, when called with empty pidl! hr = %08x\n", hr);
302 hr = IShellFolder_BindToObject(psfDesktop, NULL, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
303 ok (hr == E_INVALIDARG, "Desktop's BindToObject should fail, when called with NULL pidl! hr = %08x\n", hr);
305 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszMyComputer, NULL, &pidlMyComputer, NULL);
306 ok (SUCCEEDED(hr), "Desktop's ParseDisplayName failed to parse MyComputer's CLSID! hr = %08x\n", hr);
307 if (FAILED(hr)) {
308 IShellFolder_Release(psfDesktop);
309 return;
312 hr = IShellFolder_BindToObject(psfDesktop, pidlMyComputer, NULL, &IID_IShellFolder, (LPVOID*)&psfMyComputer);
313 ok (SUCCEEDED(hr), "Desktop failed to bind to MyComputer object! hr = %08x\n", hr);
314 IShellFolder_Release(psfDesktop);
315 IMalloc_Free(ppM, pidlMyComputer);
316 if (FAILED(hr)) return;
318 hr = IShellFolder_BindToObject(psfMyComputer, pidlEmpty, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
319 ok (hr == E_INVALIDARG, "MyComputers's BindToObject should fail, when called with empty pidl! hr = %08x\n", hr);
321 #if 0
322 /* this call segfaults on 98SE */
323 hr = IShellFolder_BindToObject(psfMyComputer, NULL, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
324 ok (hr == E_INVALIDARG, "MyComputers's BindToObject should fail, when called with NULL pidl! hr = %08x\n", hr);
325 #endif
327 cChars = GetSystemDirectoryA(szSystemDir, MAX_PATH);
328 ok (cChars > 0 && cChars < MAX_PATH, "GetSystemDirectoryA failed! LastError: %u\n", GetLastError());
329 if (cChars == 0 || cChars >= MAX_PATH) {
330 IShellFolder_Release(psfMyComputer);
331 return;
333 MultiByteToWideChar(CP_ACP, 0, szSystemDir, -1, wszSystemDir, MAX_PATH);
335 hr = IShellFolder_ParseDisplayName(psfMyComputer, NULL, NULL, wszSystemDir, NULL, &pidlSystemDir, NULL);
336 ok (SUCCEEDED(hr), "MyComputers's ParseDisplayName failed to parse the SystemDirectory! hr = %08x\n", hr);
337 if (FAILED(hr)) {
338 IShellFolder_Release(psfMyComputer);
339 return;
342 hr = IShellFolder_BindToObject(psfMyComputer, pidlSystemDir, NULL, &IID_IShellFolder, (LPVOID*)&psfSystemDir);
343 ok (SUCCEEDED(hr), "MyComputer failed to bind to a FileSystem ShellFolder! hr = %08x\n", hr);
344 IShellFolder_Release(psfMyComputer);
345 IMalloc_Free(ppM, pidlSystemDir);
346 if (FAILED(hr)) return;
348 hr = IShellFolder_BindToObject(psfSystemDir, pidlEmpty, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
349 ok (hr == E_INVALIDARG,
350 "FileSystem ShellFolder's BindToObject should fail, when called with empty pidl! hr = %08x\n", hr);
352 #if 0
353 /* this call segfaults on 98SE */
354 hr = IShellFolder_BindToObject(psfSystemDir, NULL, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
355 ok (hr == E_INVALIDARG,
356 "FileSystem ShellFolder's BindToObject should fail, when called with NULL pidl! hr = %08x\n", hr);
357 #endif
359 IShellFolder_Release(psfSystemDir);
362 /* Based on PathAddBackslashW from dlls/shlwapi/path.c */
363 static LPWSTR myPathAddBackslashW( LPWSTR lpszPath )
365 size_t iLen;
367 if (!lpszPath || (iLen = lstrlenW(lpszPath)) >= MAX_PATH)
368 return NULL;
370 if (iLen)
372 lpszPath += iLen;
373 if (lpszPath[-1] != '\\')
375 *lpszPath++ = '\\';
376 *lpszPath = '\0';
379 return lpszPath;
382 static void test_GetDisplayName(void)
384 BOOL result;
385 HRESULT hr;
386 HANDLE hTestFile;
387 WCHAR wszTestFile[MAX_PATH], wszTestFile2[MAX_PATH];
388 char szTestFile[MAX_PATH], szTestDir[MAX_PATH];
389 DWORD attr;
390 STRRET strret;
391 LPSHELLFOLDER psfDesktop, psfPersonal;
392 IUnknown *psfFile;
393 SHITEMID emptyitem = { 0, { 0 } };
394 LPITEMIDLIST pidlTestFile, pidlEmpty = (LPITEMIDLIST)&emptyitem;
395 LPCITEMIDLIST pidlLast;
396 static const CHAR szFileName[] = "winetest.foo";
397 static const WCHAR wszFileName[] = { 'w','i','n','e','t','e','s','t','.','f','o','o',0 };
398 static const WCHAR wszDirName[] = { 'w','i','n','e','t','e','s','t',0 };
400 /* I'm trying to figure if there is a functional difference between calling
401 * SHGetPathFromIDListW and calling GetDisplayNameOf(SHGDN_FORPARSING) after
402 * binding to the shellfolder. One thing I thought of was that perhaps
403 * SHGetPathFromIDListW would be able to get the path to a file, which does
404 * not exist anymore, while the other method wouldn't. It turns out there's
405 * no functional difference in this respect.
408 if(!pSHGetSpecialFolderPathA) {
409 win_skip("SHGetSpecialFolderPathA is not available\n");
410 return;
413 /* First creating a directory in MyDocuments and a file in this directory. */
414 result = pSHGetSpecialFolderPathA(NULL, szTestDir, CSIDL_PERSONAL, FALSE);
415 ok(result, "SHGetSpecialFolderPathA failed! Last error: %u\n", GetLastError());
416 if (!result) return;
418 /* Use ANSI file functions so this works on Windows 9x */
419 lstrcatA(szTestDir, "\\winetest");
420 CreateDirectoryA(szTestDir, NULL);
421 attr=GetFileAttributesA(szTestDir);
422 if (attr == INVALID_FILE_ATTRIBUTES || !(attr & FILE_ATTRIBUTE_DIRECTORY))
424 ok(0, "unable to create the '%s' directory\n", szTestDir);
425 return;
428 lstrcpyA(szTestFile, szTestDir);
429 lstrcatA(szTestFile, "\\");
430 lstrcatA(szTestFile, szFileName);
431 hTestFile = CreateFileA(szTestFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
432 ok((hTestFile != INVALID_HANDLE_VALUE), "CreateFileA failed! Last error: %u\n", GetLastError());
433 if (hTestFile == INVALID_HANDLE_VALUE) return;
434 CloseHandle(hTestFile);
436 /* Getting an itemidlist for the file. */
437 hr = SHGetDesktopFolder(&psfDesktop);
438 ok(SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08x\n", hr);
439 if (FAILED(hr)) return;
441 MultiByteToWideChar(CP_ACP, 0, szTestFile, -1, wszTestFile, MAX_PATH);
443 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszTestFile, NULL, &pidlTestFile, NULL);
444 ok(SUCCEEDED(hr), "Desktop->ParseDisplayName failed! hr = %08x\n", hr);
445 if (FAILED(hr)) {
446 IShellFolder_Release(psfDesktop);
447 return;
450 pidlLast = pILFindLastID(pidlTestFile);
451 ok(pidlLast->mkid.cb >=76 ||
452 broken(pidlLast->mkid.cb == 28) || /* W2K */
453 broken(pidlLast->mkid.cb == 40), /* Win9x, WinME */
454 "Expected pidl length of at least 76, got %d.\n", pidlLast->mkid.cb);
455 if (pidlLast->mkid.cb >= 28) {
456 ok(!lstrcmpA((CHAR*)&pidlLast->mkid.abID[12], szFileName),
457 "Filename should be stored as ansi-string at this position!\n");
459 /* WinXP and up store the filenames as both ANSI and UNICODE in the pidls */
460 if (pidlLast->mkid.cb >= 76) {
461 ok(!lstrcmpW((WCHAR*)&pidlLast->mkid.abID[46], wszFileName),
462 "Filename should be stored as wchar-string at this position!\n");
465 /* It seems as if we cannot bind to regular files on windows, but only directories.
467 hr = IShellFolder_BindToObject(psfDesktop, pidlTestFile, NULL, &IID_IUnknown, (VOID**)&psfFile);
468 todo_wine
469 ok (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) ||
470 broken(SUCCEEDED(hr)), /* Win9x, W2K */
471 "hr = %08x\n", hr);
472 if (SUCCEEDED(hr)) {
473 IShellFolder_Release(psfFile);
476 if(!pSHBindToParent)
478 win_skip("SHBindToParent is missing\n");
479 DeleteFileA(szTestFile);
480 RemoveDirectoryA(szTestDir);
481 return;
484 /* Some tests for IShellFolder::SetNameOf */
485 hr = pSHBindToParent(pidlTestFile, &IID_IShellFolder, (VOID**)&psfPersonal, &pidlLast);
486 ok(SUCCEEDED(hr), "SHBindToParent failed! hr = %08x\n", hr);
487 if (SUCCEEDED(hr)) {
488 /* It's ok to use this fixed path. Call will fail anyway. */
489 WCHAR wszAbsoluteFilename[] = { 'C',':','\\','w','i','n','e','t','e','s','t', 0 };
490 LPITEMIDLIST pidlNew;
492 /* The pidl returned through the last parameter of SetNameOf is a simple one. */
493 hr = IShellFolder_SetNameOf(psfPersonal, NULL, pidlLast, wszDirName, SHGDN_NORMAL, &pidlNew);
494 ok (SUCCEEDED(hr), "SetNameOf failed! hr = %08x\n", hr);
495 if(hr == S_OK)
497 ok (((LPITEMIDLIST)((LPBYTE)pidlNew+pidlNew->mkid.cb))->mkid.cb == 0,
498 "pidl returned from SetNameOf should be simple!\n");
500 /* Passing an absolute path to SetNameOf fails. The HRESULT code indicates that SetNameOf
501 * is implemented on top of SHFileOperation in WinXP. */
502 hr = IShellFolder_SetNameOf(psfPersonal, NULL, pidlNew, wszAbsoluteFilename,
503 SHGDN_FORPARSING, NULL);
504 ok (hr == HRESULT_FROM_WIN32(ERROR_CANCELLED), "SetNameOf succeeded! hr = %08x\n", hr);
506 /* Rename the file back to its original name. SetNameOf ignores the fact, that the
507 * SHGDN flags specify an absolute path. */
508 hr = IShellFolder_SetNameOf(psfPersonal, NULL, pidlNew, wszFileName, SHGDN_FORPARSING, NULL);
509 ok (SUCCEEDED(hr), "SetNameOf failed! hr = %08x\n", hr);
511 pILFree(pidlNew);
514 IShellFolder_Release(psfPersonal);
517 /* Deleting the file and the directory */
518 DeleteFileA(szTestFile);
519 RemoveDirectoryA(szTestDir);
521 /* SHGetPathFromIDListW still works, although the file is not present anymore. */
522 if (pSHGetPathFromIDListW)
524 result = pSHGetPathFromIDListW(pidlTestFile, wszTestFile2);
525 ok (result, "SHGetPathFromIDListW failed! Last error: %u\n", GetLastError());
526 ok (!lstrcmpiW(wszTestFile, wszTestFile2), "SHGetPathFromIDListW returns incorrect path!\n");
529 /* SHBindToParent fails, if called with a NULL PIDL. */
530 hr = pSHBindToParent(NULL, &IID_IShellFolder, (VOID**)&psfPersonal, &pidlLast);
531 ok (FAILED(hr), "SHBindToParent(NULL) should fail!\n");
533 /* But it succeeds with an empty PIDL. */
534 hr = pSHBindToParent(pidlEmpty, &IID_IShellFolder, (VOID**)&psfPersonal, &pidlLast);
535 ok (SUCCEEDED(hr), "SHBindToParent(empty PIDL) should succeed! hr = %08x\n", hr);
536 ok (pidlLast == pidlEmpty, "The last element of an empty PIDL should be the PIDL itself!\n");
537 if (SUCCEEDED(hr))
538 IShellFolder_Release(psfPersonal);
540 /* Binding to the folder and querying the display name of the file also works. */
541 hr = pSHBindToParent(pidlTestFile, &IID_IShellFolder, (VOID**)&psfPersonal, &pidlLast);
542 ok (SUCCEEDED(hr), "SHBindToParent failed! hr = %08x\n", hr);
543 if (FAILED(hr)) {
544 IShellFolder_Release(psfDesktop);
545 return;
548 /* This test shows that Windows doesn't allocate a new pidlLast, but returns a pointer into
549 * pidlTestFile (In accordance with MSDN). */
550 ok (pILFindLastID(pidlTestFile) == pidlLast,
551 "SHBindToParent doesn't return the last id of the pidl param!\n");
553 hr = IShellFolder_GetDisplayNameOf(psfPersonal, pidlLast, SHGDN_FORPARSING, &strret);
554 ok (SUCCEEDED(hr), "Personal->GetDisplayNameOf failed! hr = %08x\n", hr);
555 if (FAILED(hr)) {
556 IShellFolder_Release(psfDesktop);
557 IShellFolder_Release(psfPersonal);
558 return;
561 if (pStrRetToBufW)
563 hr = pStrRetToBufW(&strret, pidlLast, wszTestFile2, MAX_PATH);
564 ok (SUCCEEDED(hr), "StrRetToBufW failed! hr = %08x\n", hr);
565 ok (!lstrcmpiW(wszTestFile, wszTestFile2), "GetDisplayNameOf returns incorrect path!\n");
568 IShellFolder_Release(psfDesktop);
569 IShellFolder_Release(psfPersonal);
572 static void test_CallForAttributes(void)
574 HKEY hKey;
575 LONG lResult;
576 HRESULT hr;
577 DWORD dwSize;
578 LPSHELLFOLDER psfDesktop;
579 LPITEMIDLIST pidlMyDocuments;
580 DWORD dwAttributes, dwCallForAttributes, dwOrigAttributes, dwOrigCallForAttributes;
581 static const WCHAR wszAttributes[] = { 'A','t','t','r','i','b','u','t','e','s',0 };
582 static const WCHAR wszCallForAttributes[] = {
583 'C','a','l','l','F','o','r','A','t','t','r','i','b','u','t','e','s',0 };
584 static const WCHAR wszMyDocumentsKey[] = {
585 'C','L','S','I','D','\\','{','4','5','0','D','8','F','B','A','-','A','D','2','5','-',
586 '1','1','D','0','-','9','8','A','8','-','0','8','0','0','3','6','1','B','1','1','0','3','}',
587 '\\','S','h','e','l','l','F','o','l','d','e','r',0 };
588 WCHAR wszMyDocuments[] = {
589 ':',':','{','4','5','0','D','8','F','B','A','-','A','D','2','5','-','1','1','D','0','-',
590 '9','8','A','8','-','0','8','0','0','3','6','1','B','1','1','0','3','}',0 };
592 /* For the root of a namespace extension, the attributes are not queried by binding
593 * to the object and calling GetAttributesOf. Instead, the attributes are read from
594 * the registry value HKCR/CLSID/{...}/ShellFolder/Attributes. This is documented on MSDN.
596 * The MyDocuments shellfolder on WinXP has a HKCR/CLSID/{...}/ShellFolder/CallForAttributes
597 * value. It seems that if the folder is queried for one of the flags set in CallForAttributes,
598 * the shell does bind to the folder object and calls GetAttributesOf. This is not documented
599 * on MSDN. This test is meant to document the observed behaviour on WinXP SP2.
601 hr = SHGetDesktopFolder(&psfDesktop);
602 ok (SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08x\n", hr);
603 if (FAILED(hr)) return;
605 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszMyDocuments, NULL,
606 &pidlMyDocuments, NULL);
607 ok (SUCCEEDED(hr),
608 "Desktop's ParseDisplayName failed to parse MyDocuments's CLSID! hr = %08x\n", hr);
609 if (FAILED(hr)) {
610 IShellFolder_Release(psfDesktop);
611 return;
614 dwAttributes = 0xffffffff;
615 hr = IShellFolder_GetAttributesOf(psfDesktop, 1,
616 (LPCITEMIDLIST*)&pidlMyDocuments, &dwAttributes);
617 ok (SUCCEEDED(hr), "Desktop->GetAttributesOf(MyDocuments) failed! hr = %08x\n", hr);
619 /* We need the following setup (as observed on WinXP SP2), for the tests to make sense. */
620 ok (dwAttributes & SFGAO_FILESYSTEM, "SFGAO_FILESYSTEM attribute is not set for MyDocuments!\n");
621 ok (!(dwAttributes & SFGAO_ISSLOW), "SFGAO_ISSLOW attribute is set for MyDocuments!\n");
622 ok (!(dwAttributes & SFGAO_GHOSTED), "SFGAO_GHOSTED attribute is set for MyDocuments!\n");
624 /* We don't have the MyDocuments shellfolder in wine yet, and thus we don't have the registry
625 * key. So the test will return at this point, if run on wine.
627 lResult = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszMyDocumentsKey, 0, KEY_WRITE|KEY_READ, &hKey);
628 ok (lResult == ERROR_SUCCESS, "RegOpenKeyEx failed! result: %08x\n", lResult);
629 if (lResult != ERROR_SUCCESS) {
630 IMalloc_Free(ppM, pidlMyDocuments);
631 IShellFolder_Release(psfDesktop);
632 return;
635 /* Query MyDocuments' Attributes value, to be able to restore it later. */
636 dwSize = sizeof(DWORD);
637 lResult = RegQueryValueExW(hKey, wszAttributes, NULL, NULL, (LPBYTE)&dwOrigAttributes, &dwSize);
638 ok (lResult == ERROR_SUCCESS, "RegQueryValueEx failed! result: %08x\n", lResult);
639 if (lResult != ERROR_SUCCESS) {
640 RegCloseKey(hKey);
641 IMalloc_Free(ppM, pidlMyDocuments);
642 IShellFolder_Release(psfDesktop);
643 return;
646 /* Query MyDocuments' CallForAttributes value, to be able to restore it later. */
647 dwSize = sizeof(DWORD);
648 lResult = RegQueryValueExW(hKey, wszCallForAttributes, NULL, NULL,
649 (LPBYTE)&dwOrigCallForAttributes, &dwSize);
650 ok (lResult == ERROR_SUCCESS, "RegQueryValueEx failed! result: %08x\n", lResult);
651 if (lResult != ERROR_SUCCESS) {
652 RegCloseKey(hKey);
653 IMalloc_Free(ppM, pidlMyDocuments);
654 IShellFolder_Release(psfDesktop);
655 return;
658 /* Define via the Attributes value that MyDocuments attributes are SFGAO_ISSLOW and
659 * SFGAO_GHOSTED and that MyDocuments should be called for the SFGAO_ISSLOW and
660 * SFGAO_FILESYSTEM attributes. */
661 dwAttributes = SFGAO_ISSLOW|SFGAO_GHOSTED;
662 RegSetValueExW(hKey, wszAttributes, 0, REG_DWORD, (LPBYTE)&dwAttributes, sizeof(DWORD));
663 dwCallForAttributes = SFGAO_ISSLOW|SFGAO_FILESYSTEM;
664 RegSetValueExW(hKey, wszCallForAttributes, 0, REG_DWORD,
665 (LPBYTE)&dwCallForAttributes, sizeof(DWORD));
667 /* Although it is not set in CallForAttributes, the SFGAO_GHOSTED flag is reset by
668 * GetAttributesOf. It seems that once there is a single attribute queried, for which
669 * CallForAttributes is set, all flags are taken from the GetAttributesOf call and
670 * the flags in Attributes are ignored.
672 dwAttributes = SFGAO_ISSLOW|SFGAO_GHOSTED|SFGAO_FILESYSTEM;
673 hr = IShellFolder_GetAttributesOf(psfDesktop, 1,
674 (LPCITEMIDLIST*)&pidlMyDocuments, &dwAttributes);
675 ok (SUCCEEDED(hr), "Desktop->GetAttributesOf(MyDocuments) failed! hr = %08x\n", hr);
676 if (SUCCEEDED(hr))
677 ok (dwAttributes == SFGAO_FILESYSTEM,
678 "Desktop->GetAttributes(MyDocuments) returned unexpected attributes: %08x\n",
679 dwAttributes);
681 /* Restore MyDocuments' original Attributes and CallForAttributes registry values */
682 RegSetValueExW(hKey, wszAttributes, 0, REG_DWORD, (LPBYTE)&dwOrigAttributes, sizeof(DWORD));
683 RegSetValueExW(hKey, wszCallForAttributes, 0, REG_DWORD,
684 (LPBYTE)&dwOrigCallForAttributes, sizeof(DWORD));
685 RegCloseKey(hKey);
686 IMalloc_Free(ppM, pidlMyDocuments);
687 IShellFolder_Release(psfDesktop);
690 static void test_GetAttributesOf(void)
692 HRESULT hr;
693 LPSHELLFOLDER psfDesktop, psfMyComputer;
694 SHITEMID emptyitem = { 0, { 0 } };
695 LPCITEMIDLIST pidlEmpty = (LPCITEMIDLIST)&emptyitem;
696 LPITEMIDLIST pidlMyComputer;
697 DWORD dwFlags;
698 static const DWORD dwDesktopFlags = /* As observed on WinXP SP2 */
699 SFGAO_STORAGE | SFGAO_HASPROPSHEET | SFGAO_STORAGEANCESTOR |
700 SFGAO_FILESYSANCESTOR | SFGAO_FOLDER | SFGAO_FILESYSTEM | SFGAO_HASSUBFOLDER;
701 static const DWORD dwMyComputerFlags = /* As observed on WinXP SP2 */
702 SFGAO_CANRENAME | SFGAO_CANDELETE | SFGAO_HASPROPSHEET |
703 SFGAO_DROPTARGET | SFGAO_FILESYSANCESTOR | SFGAO_FOLDER | SFGAO_HASSUBFOLDER;
704 WCHAR wszMyComputer[] = {
705 ':',':','{','2','0','D','0','4','F','E','0','-','3','A','E','A','-','1','0','6','9','-',
706 'A','2','D','8','-','0','8','0','0','2','B','3','0','3','0','9','D','}',0 };
707 char cCurrDirA [MAX_PATH] = {0};
708 WCHAR cCurrDirW [MAX_PATH];
709 static WCHAR cTestDirW[] = {'t','e','s','t','d','i','r',0};
710 static const WCHAR cBackSlash[] = {'\\',0};
711 IShellFolder *IDesktopFolder, *testIShellFolder;
712 ITEMIDLIST *newPIDL;
713 int len;
715 hr = SHGetDesktopFolder(&psfDesktop);
716 ok (SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08x\n", hr);
717 if (FAILED(hr)) return;
719 /* The Desktop attributes can be queried with a single empty itemidlist, .. */
720 dwFlags = 0xffffffff;
721 hr = IShellFolder_GetAttributesOf(psfDesktop, 1, &pidlEmpty, &dwFlags);
722 ok (SUCCEEDED(hr), "Desktop->GetAttributesOf(empty pidl) failed! hr = %08x\n", hr);
723 ok (dwFlags == dwDesktopFlags, "Wrong Desktop attributes: %08x, expected: %08x\n",
724 dwFlags, dwDesktopFlags);
726 /* .. or with no itemidlist at all. */
727 dwFlags = 0xffffffff;
728 hr = IShellFolder_GetAttributesOf(psfDesktop, 0, NULL, &dwFlags);
729 ok (SUCCEEDED(hr), "Desktop->GetAttributesOf(NULL) failed! hr = %08x\n", hr);
730 ok (dwFlags == dwDesktopFlags, "Wrong Desktop attributes: %08x, expected: %08x\n",
731 dwFlags, dwDesktopFlags);
733 /* Testing the attributes of the MyComputer shellfolder */
734 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszMyComputer, NULL, &pidlMyComputer, NULL);
735 ok (SUCCEEDED(hr), "Desktop's ParseDisplayName failed to parse MyComputer's CLSID! hr = %08x\n", hr);
736 if (FAILED(hr)) {
737 IShellFolder_Release(psfDesktop);
738 return;
741 /* WinXP SP2 sets the SFGAO_CANLINK flag, when MyComputer is queried via the Desktop
742 * folder object. It doesn't do this, if MyComputer is queried directly (see below).
743 * SFGAO_CANLINK is the same as DROPEFFECT_LINK, which MSDN says means: "Drag source
744 * should create a link to the original data". You can't create links on MyComputer on
745 * Windows, so this flag shouldn't be set. Seems like a bug in Windows. As long as nobody
746 * depends on this bug, we probably shouldn't imitate it.
748 dwFlags = 0xffffffff;
749 hr = IShellFolder_GetAttributesOf(psfDesktop, 1, (LPCITEMIDLIST*)&pidlMyComputer, &dwFlags);
750 ok (SUCCEEDED(hr), "Desktop->GetAttributesOf(MyComputer) failed! hr = %08x\n", hr);
751 ok ((dwFlags & ~(DWORD)SFGAO_CANLINK) == dwMyComputerFlags,
752 "Wrong MyComputer attributes: %08x, expected: %08x\n", dwFlags, dwMyComputerFlags);
754 hr = IShellFolder_BindToObject(psfDesktop, pidlMyComputer, NULL, &IID_IShellFolder, (LPVOID*)&psfMyComputer);
755 ok (SUCCEEDED(hr), "Desktop failed to bind to MyComputer object! hr = %08x\n", hr);
756 IShellFolder_Release(psfDesktop);
757 IMalloc_Free(ppM, pidlMyComputer);
758 if (FAILED(hr)) return;
760 hr = IShellFolder_GetAttributesOf(psfMyComputer, 1, &pidlEmpty, &dwFlags);
761 todo_wine {ok (hr == E_INVALIDARG, "MyComputer->GetAttributesOf(emtpy pidl) should fail! hr = %08x\n", hr); }
763 dwFlags = 0xffffffff;
764 hr = IShellFolder_GetAttributesOf(psfMyComputer, 0, NULL, &dwFlags);
765 ok (SUCCEEDED(hr), "MyComputer->GetAttributesOf(NULL) failed! hr = %08x\n", hr);
766 todo_wine { ok (dwFlags == dwMyComputerFlags,
767 "Wrong MyComputer attributes: %08x, expected: %08x\n", dwFlags, dwMyComputerFlags); }
769 IShellFolder_Release(psfMyComputer);
771 /* create test directory */
772 CreateFilesFolders();
774 GetCurrentDirectoryA(MAX_PATH, cCurrDirA);
775 len = lstrlenA(cCurrDirA);
777 if (len == 0) {
778 trace("GetCurrentDirectoryA returned empty string. Skipping test_EnumObjects_and_CompareIDs\n");
779 return;
781 if(cCurrDirA[len-1] == '\\')
782 cCurrDirA[len-1] = 0;
784 MultiByteToWideChar(CP_ACP, 0, cCurrDirA, -1, cCurrDirW, MAX_PATH);
786 hr = SHGetDesktopFolder(&IDesktopFolder);
787 ok(hr == S_OK, "SHGetDesktopfolder failed %08x\n", hr);
789 hr = IShellFolder_ParseDisplayName(IDesktopFolder, NULL, NULL, cCurrDirW, NULL, &newPIDL, 0);
790 ok(hr == S_OK, "ParseDisplayName failed %08x\n", hr);
792 hr = IShellFolder_BindToObject(IDesktopFolder, newPIDL, NULL, (REFIID)&IID_IShellFolder, (LPVOID *)&testIShellFolder);
793 ok(hr == S_OK, "BindToObject failed %08x\n", hr);
795 IMalloc_Free(ppM, newPIDL);
797 /* get relative PIDL */
798 hr = IShellFolder_ParseDisplayName(testIShellFolder, NULL, NULL, cTestDirW, NULL, &newPIDL, 0);
799 ok(hr == S_OK, "ParseDisplayName failed %08x\n", hr);
801 /* test the shell attributes of the test directory using the relative PIDL */
802 dwFlags = SFGAO_FOLDER;
803 hr = IShellFolder_GetAttributesOf(testIShellFolder, 1, (LPCITEMIDLIST*)&newPIDL, &dwFlags);
804 ok (SUCCEEDED(hr), "Desktop->GetAttributesOf() failed! hr = %08x\n", hr);
805 ok ((dwFlags&SFGAO_FOLDER), "Wrong directory attribute for relative PIDL: %08x\n", dwFlags);
807 /* free memory */
808 IMalloc_Free(ppM, newPIDL);
810 /* append testdirectory name to path */
811 lstrcatW(cCurrDirW, cBackSlash);
812 lstrcatW(cCurrDirW, cTestDirW);
814 hr = IShellFolder_ParseDisplayName(IDesktopFolder, NULL, NULL, cCurrDirW, NULL, &newPIDL, 0);
815 ok(hr == S_OK, "ParseDisplayName failed %08x\n", hr);
817 /* test the shell attributes of the test directory using the absolute PIDL */
818 dwFlags = SFGAO_FOLDER;
819 hr = IShellFolder_GetAttributesOf(IDesktopFolder, 1, (LPCITEMIDLIST*)&newPIDL, &dwFlags);
820 ok (SUCCEEDED(hr), "Desktop->GetAttributesOf() failed! hr = %08x\n", hr);
821 ok ((dwFlags&SFGAO_FOLDER), "Wrong directory attribute for absolute PIDL: %08x\n", dwFlags);
823 /* free memory */
824 IMalloc_Free(ppM, newPIDL);
826 IShellFolder_Release(testIShellFolder);
828 Cleanup();
830 IShellFolder_Release(IDesktopFolder);
833 static void test_SHGetPathFromIDList(void)
835 SHITEMID emptyitem = { 0, { 0 } };
836 LPCITEMIDLIST pidlEmpty = (LPCITEMIDLIST)&emptyitem;
837 LPITEMIDLIST pidlMyComputer;
838 WCHAR wszPath[MAX_PATH], wszDesktop[MAX_PATH];
839 BOOL result;
840 HRESULT hr;
841 LPSHELLFOLDER psfDesktop;
842 WCHAR wszMyComputer[] = {
843 ':',':','{','2','0','D','0','4','F','E','0','-','3','A','E','A','-','1','0','6','9','-',
844 'A','2','D','8','-','0','8','0','0','2','B','3','0','3','0','9','D','}',0 };
845 WCHAR wszFileName[MAX_PATH];
846 LPITEMIDLIST pidlTestFile;
847 HANDLE hTestFile;
848 STRRET strret;
849 static WCHAR wszTestFile[] = {
850 'w','i','n','e','t','e','s','t','.','f','o','o',0 };
851 HRESULT (WINAPI *pSHGetSpecialFolderLocation)(HWND, int, LPITEMIDLIST *);
852 HMODULE hShell32;
853 LPITEMIDLIST pidlPrograms;
855 if(!pSHGetPathFromIDListW || !pSHGetSpecialFolderPathW)
857 skip("SHGetPathFromIDListW() or SHGetSpecialFolderPathW() is missing\n");
858 return;
861 /* Calling SHGetPathFromIDListW with no pidl should return the empty string */
862 wszPath[0] = 'a';
863 wszPath[1] = '\0';
864 result = pSHGetPathFromIDListW(NULL, wszPath);
865 ok(!result, "Expected failure\n");
866 ok(!wszPath[0], "Expected empty string\n");
868 /* Calling SHGetPathFromIDListW with an empty pidl should return the desktop folder's path. */
869 result = pSHGetSpecialFolderPathW(NULL, wszDesktop, CSIDL_DESKTOP, FALSE);
870 ok(result, "SHGetSpecialFolderPathW(CSIDL_DESKTOP) failed! Last error: %u\n", GetLastError());
871 if (!result) return;
873 result = pSHGetPathFromIDListW(pidlEmpty, wszPath);
874 ok(result, "SHGetPathFromIDListW failed! Last error: %u\n", GetLastError());
875 if (!result) return;
876 ok(!lstrcmpiW(wszDesktop, wszPath), "SHGetPathFromIDListW didn't return desktop path for empty pidl!\n");
878 /* MyComputer does not map to a filesystem path. SHGetPathFromIDListW should fail. */
879 hr = SHGetDesktopFolder(&psfDesktop);
880 ok (SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08x\n", hr);
881 if (FAILED(hr)) return;
883 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszMyComputer, NULL, &pidlMyComputer, NULL);
884 ok (SUCCEEDED(hr), "Desktop's ParseDisplayName failed to parse MyComputer's CLSID! hr = %08x\n", hr);
885 if (FAILED(hr)) {
886 IShellFolder_Release(psfDesktop);
887 return;
890 SetLastError(0xdeadbeef);
891 wszPath[0] = 'a';
892 wszPath[1] = '\0';
893 result = pSHGetPathFromIDListW(pidlMyComputer, wszPath);
894 ok (!result, "SHGetPathFromIDListW succeeded where it shouldn't!\n");
895 ok (GetLastError()==0xdeadbeef, "SHGetPathFromIDListW shouldn't set last error! Last error: %u\n", GetLastError());
896 ok (!wszPath[0], "Expected empty path\n");
897 if (result) {
898 IShellFolder_Release(psfDesktop);
899 return;
902 IMalloc_Free(ppM, pidlMyComputer);
904 result = pSHGetSpecialFolderPathW(NULL, wszFileName, CSIDL_DESKTOPDIRECTORY, FALSE);
905 ok(result, "SHGetSpecialFolderPathW failed! Last error: %u\n", GetLastError());
906 if (!result) {
907 IShellFolder_Release(psfDesktop);
908 return;
910 myPathAddBackslashW(wszFileName);
911 lstrcatW(wszFileName, wszTestFile);
912 hTestFile = CreateFileW(wszFileName, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, NULL);
913 ok(hTestFile != INVALID_HANDLE_VALUE, "CreateFileW failed! Last error: %u\n", GetLastError());
914 if (hTestFile == INVALID_HANDLE_VALUE) {
915 IShellFolder_Release(psfDesktop);
916 return;
918 CloseHandle(hTestFile);
920 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszTestFile, NULL, &pidlTestFile, NULL);
921 ok (SUCCEEDED(hr), "Desktop's ParseDisplayName failed to parse filename hr = %08x\n", hr);
922 if (FAILED(hr)) {
923 IShellFolder_Release(psfDesktop);
924 DeleteFileW(wszFileName);
925 IMalloc_Free(ppM, pidlTestFile);
926 return;
929 /* This test is to show that the Desktop shellfolder prepends the CSIDL_DESKTOPDIRECTORY
930 * path for files placed on the desktop, if called with SHGDN_FORPARSING. */
931 hr = IShellFolder_GetDisplayNameOf(psfDesktop, pidlTestFile, SHGDN_FORPARSING, &strret);
932 ok (SUCCEEDED(hr), "Desktop's GetDisplayNamfOf failed! hr = %08x\n", hr);
933 IShellFolder_Release(psfDesktop);
934 DeleteFileW(wszFileName);
935 if (FAILED(hr)) {
936 IMalloc_Free(ppM, pidlTestFile);
937 return;
939 if (pStrRetToBufW)
941 pStrRetToBufW(&strret, pidlTestFile, wszPath, MAX_PATH);
942 ok(0 == lstrcmpW(wszFileName, wszPath),
943 "Desktop->GetDisplayNameOf(pidlTestFile, SHGDN_FORPARSING) "
944 "returned incorrect path for file placed on desktop\n");
947 result = pSHGetPathFromIDListW(pidlTestFile, wszPath);
948 ok(result, "SHGetPathFromIDListW failed! Last error: %u\n", GetLastError());
949 IMalloc_Free(ppM, pidlTestFile);
950 if (!result) return;
951 ok(0 == lstrcmpW(wszFileName, wszPath), "SHGetPathFromIDListW returned incorrect path for file placed on desktop\n");
954 /* Test if we can get the path from the start menu "program files" PIDL. */
955 hShell32 = GetModuleHandleA("shell32");
956 pSHGetSpecialFolderLocation = (void *)GetProcAddress(hShell32, "SHGetSpecialFolderLocation");
958 hr = pSHGetSpecialFolderLocation(NULL, CSIDL_PROGRAM_FILES, &pidlPrograms);
959 ok(SUCCEEDED(hr), "SHGetFolderLocation failed: 0x%08x\n", hr);
961 SetLastError(0xdeadbeef);
962 result = pSHGetPathFromIDListW(pidlPrograms, wszPath);
963 IMalloc_Free(ppM, pidlPrograms);
964 ok(result, "SHGetPathFromIDListW failed\n");
967 static void test_EnumObjects_and_CompareIDs(void)
969 ITEMIDLIST *newPIDL;
970 IShellFolder *IDesktopFolder, *testIShellFolder;
971 char cCurrDirA [MAX_PATH] = {0};
972 static const CHAR cTestDirA[] = "\\testdir";
973 WCHAR cTestDirW[MAX_PATH];
974 int len;
975 HRESULT hr;
977 GetCurrentDirectoryA(MAX_PATH, cCurrDirA);
978 len = lstrlenA(cCurrDirA);
980 if(len == 0) {
981 win_skip("GetCurrentDirectoryA returned empty string. Skipping test_EnumObjects_and_CompareIDs\n");
982 return;
984 if(cCurrDirA[len-1] == '\\')
985 cCurrDirA[len-1] = 0;
987 lstrcatA(cCurrDirA, cTestDirA);
988 MultiByteToWideChar(CP_ACP, 0, cCurrDirA, -1, cTestDirW, MAX_PATH);
990 hr = SHGetDesktopFolder(&IDesktopFolder);
991 ok(hr == S_OK, "SHGetDesktopfolder failed %08x\n", hr);
993 CreateFilesFolders();
995 hr = IShellFolder_ParseDisplayName(IDesktopFolder, NULL, NULL, cTestDirW, NULL, &newPIDL, 0);
996 ok(hr == S_OK, "ParseDisplayName failed %08x\n", hr);
998 hr = IShellFolder_BindToObject(IDesktopFolder, newPIDL, NULL, (REFIID)&IID_IShellFolder, (LPVOID *)&testIShellFolder);
999 ok(hr == S_OK, "BindToObject failed %08x\n", hr);
1001 test_EnumObjects(testIShellFolder);
1003 IShellFolder_Release(testIShellFolder);
1005 Cleanup();
1007 IMalloc_Free(ppM, newPIDL);
1009 IShellFolder_Release(IDesktopFolder);
1012 /* A simple implementation of an IPropertyBag, which returns fixed values for
1013 * 'Target' and 'Attributes' properties.
1015 static HRESULT WINAPI InitPropertyBag_IPropertyBag_QueryInterface(IPropertyBag *iface, REFIID riid,
1016 void **ppvObject)
1018 if (!ppvObject)
1019 return E_INVALIDARG;
1021 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IPropertyBag, riid)) {
1022 *ppvObject = iface;
1023 } else {
1024 ok (FALSE, "InitPropertyBag asked for unknown interface!\n");
1025 return E_NOINTERFACE;
1028 IPropertyBag_AddRef(iface);
1029 return S_OK;
1032 static ULONG WINAPI InitPropertyBag_IPropertyBag_AddRef(IPropertyBag *iface) {
1033 return 2;
1036 static ULONG WINAPI InitPropertyBag_IPropertyBag_Release(IPropertyBag *iface) {
1037 return 1;
1040 static HRESULT WINAPI InitPropertyBag_IPropertyBag_Read(IPropertyBag *iface, LPCOLESTR pszPropName,
1041 VARIANT *pVar, IErrorLog *pErrorLog)
1043 static const WCHAR wszTargetSpecialFolder[] = {
1044 'T','a','r','g','e','t','S','p','e','c','i','a','l','F','o','l','d','e','r',0 };
1045 static const WCHAR wszTarget[] = {
1046 'T','a','r','g','e','t',0 };
1047 static const WCHAR wszAttributes[] = {
1048 'A','t','t','r','i','b','u','t','e','s',0 };
1049 static const WCHAR wszResolveLinkFlags[] = {
1050 'R','e','s','o','l','v','e','L','i','n','k','F','l','a','g','s',0 };
1052 if (!lstrcmpW(pszPropName, wszTargetSpecialFolder)) {
1053 ok(V_VT(pVar) == VT_I4, "Wrong variant type for 'TargetSpecialFolder' property!\n");
1054 return E_INVALIDARG;
1057 if (!lstrcmpW(pszPropName, wszResolveLinkFlags))
1059 ok(V_VT(pVar) == VT_UI4, "Wrong variant type for 'ResolveLinkFlags' property!\n");
1060 return E_INVALIDARG;
1063 if (!lstrcmpW(pszPropName, wszTarget)) {
1064 WCHAR wszPath[MAX_PATH];
1065 BOOL result;
1067 ok(V_VT(pVar) == VT_BSTR, "Wrong variant type for 'Target' property!\n");
1068 if (V_VT(pVar) != VT_BSTR) return E_INVALIDARG;
1070 result = pSHGetSpecialFolderPathW(NULL, wszPath, CSIDL_DESKTOPDIRECTORY, FALSE);
1071 ok(result, "SHGetSpecialFolderPathW(DESKTOPDIRECTORY) failed! %u\n", GetLastError());
1072 if (!result) return E_INVALIDARG;
1074 V_BSTR(pVar) = SysAllocString(wszPath);
1075 return S_OK;
1078 if (!lstrcmpW(pszPropName, wszAttributes)) {
1079 ok(V_VT(pVar) == VT_UI4, "Wrong variant type for 'Attributes' property!\n");
1080 if (V_VT(pVar) != VT_UI4) return E_INVALIDARG;
1081 V_UI4(pVar) = SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR|
1082 SFGAO_CANRENAME|SFGAO_FILESYSTEM;
1083 return S_OK;
1086 ok(FALSE, "PropertyBag was asked for unknown property (vt=%d)!\n", V_VT(pVar));
1087 return E_INVALIDARG;
1090 static HRESULT WINAPI InitPropertyBag_IPropertyBag_Write(IPropertyBag *iface, LPCOLESTR pszPropName,
1091 VARIANT *pVar)
1093 ok(FALSE, "Unexpected call to IPropertyBag_Write\n");
1094 return E_NOTIMPL;
1097 static const IPropertyBagVtbl InitPropertyBag_IPropertyBagVtbl = {
1098 InitPropertyBag_IPropertyBag_QueryInterface,
1099 InitPropertyBag_IPropertyBag_AddRef,
1100 InitPropertyBag_IPropertyBag_Release,
1101 InitPropertyBag_IPropertyBag_Read,
1102 InitPropertyBag_IPropertyBag_Write
1105 static struct IPropertyBag InitPropertyBag = {
1106 &InitPropertyBag_IPropertyBagVtbl
1109 static void test_FolderShortcut(void) {
1110 IPersistPropertyBag *pPersistPropertyBag;
1111 IShellFolder *pShellFolder, *pDesktopFolder;
1112 IPersistFolder3 *pPersistFolder3;
1113 HRESULT hr;
1114 STRRET strret;
1115 WCHAR wszDesktopPath[MAX_PATH], wszBuffer[MAX_PATH];
1116 BOOL result;
1117 CLSID clsid;
1118 LPITEMIDLIST pidlCurrentFolder, pidlWineTestFolder, pidlSubFolder;
1119 HKEY hShellExtKey;
1120 WCHAR wszWineTestFolder[] = {
1121 ':',':','{','9','B','3','5','2','E','B','F','-','2','7','6','5','-','4','5','C','1','-',
1122 'B','4','C','6','-','8','5','C','C','7','F','7','A','B','C','6','4','}',0 };
1123 WCHAR wszShellExtKey[] = { 'S','o','f','t','w','a','r','e','\\',
1124 'M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\',
1125 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
1126 'E','x','p','l','o','r','e','r','\\','D','e','s','k','t','o','p','\\',
1127 'N','a','m','e','S','p','a','c','e','\\',
1128 '{','9','b','3','5','2','e','b','f','-','2','7','6','5','-','4','5','c','1','-',
1129 'b','4','c','6','-','8','5','c','c','7','f','7','a','b','c','6','4','}',0 };
1131 WCHAR wszSomeSubFolder[] = { 'S','u','b','F','o','l','d','e','r', 0};
1132 static const GUID CLSID_UnixDosFolder =
1133 {0x9d20aae8, 0x0625, 0x44b0, {0x9c, 0xa7, 0x71, 0x88, 0x9c, 0x22, 0x54, 0xd9}};
1135 if (!pSHGetSpecialFolderPathW || !pStrRetToBufW) {
1136 win_skip("SHGetSpecialFolderPathW and/or StrRetToBufW are not available\n");
1137 return;
1140 /* These tests basically show, that CLSID_FolderShortcuts are initialized
1141 * via their IPersistPropertyBag interface. And that the target folder
1142 * is taken from the IPropertyBag's 'Target' property.
1144 hr = CoCreateInstance(&CLSID_FolderShortcut, NULL, CLSCTX_INPROC_SERVER,
1145 &IID_IPersistPropertyBag, (LPVOID*)&pPersistPropertyBag);
1146 if (hr == REGDB_E_CLASSNOTREG) {
1147 win_skip("CLSID_FolderShortcut is not implemented\n");
1148 return;
1150 ok (SUCCEEDED(hr), "CoCreateInstance failed! hr = 0x%08x\n", hr);
1151 if (FAILED(hr)) return;
1153 hr = IPersistPropertyBag_Load(pPersistPropertyBag, &InitPropertyBag, NULL);
1154 ok(SUCCEEDED(hr), "IPersistPropertyBag_Load failed! hr = %08x\n", hr);
1155 if (FAILED(hr)) {
1156 IPersistPropertyBag_Release(pPersistPropertyBag);
1157 return;
1160 hr = IPersistPropertyBag_QueryInterface(pPersistPropertyBag, &IID_IShellFolder,
1161 (LPVOID*)&pShellFolder);
1162 IPersistPropertyBag_Release(pPersistPropertyBag);
1163 ok(SUCCEEDED(hr), "IPersistPropertyBag_QueryInterface(IShellFolder) failed! hr = %08x\n", hr);
1164 if (FAILED(hr)) return;
1166 hr = IShellFolder_GetDisplayNameOf(pShellFolder, NULL, SHGDN_FORPARSING, &strret);
1167 ok(SUCCEEDED(hr), "IShellFolder_GetDisplayNameOf(NULL) failed! hr = %08x\n", hr);
1168 if (FAILED(hr)) {
1169 IShellFolder_Release(pShellFolder);
1170 return;
1173 result = pSHGetSpecialFolderPathW(NULL, wszDesktopPath, CSIDL_DESKTOPDIRECTORY, FALSE);
1174 ok(result, "SHGetSpecialFolderPathW(CSIDL_DESKTOPDIRECTORY) failed! %u\n", GetLastError());
1175 if (!result) return;
1177 pStrRetToBufW(&strret, NULL, wszBuffer, MAX_PATH);
1178 ok(!lstrcmpiW(wszDesktopPath, wszBuffer), "FolderShortcut returned incorrect folder!\n");
1180 hr = IShellFolder_QueryInterface(pShellFolder, &IID_IPersistFolder3, (LPVOID*)&pPersistFolder3);
1181 IShellFolder_Release(pShellFolder);
1182 ok(SUCCEEDED(hr), "IShellFolder_QueryInterface(IID_IPersistFolder3 failed! hr = 0x%08x\n", hr);
1183 if (FAILED(hr)) return;
1185 hr = IPersistFolder3_GetClassID(pPersistFolder3, &clsid);
1186 ok(SUCCEEDED(hr), "IPersistFolder3_GetClassID failed! hr=0x%08x\n", hr);
1187 ok(IsEqualCLSID(&clsid, &CLSID_FolderShortcut), "Unexpected CLSID!\n");
1189 hr = IPersistFolder3_GetCurFolder(pPersistFolder3, &pidlCurrentFolder);
1190 ok(SUCCEEDED(hr), "IPersistFolder3_GetCurFolder failed! hr=0x%08x\n", hr);
1191 ok(!pidlCurrentFolder, "IPersistFolder3_GetCurFolder should return a NULL pidl!\n");
1193 /* For FolderShortcut objects, the Initialize method initialized the folder's position in the
1194 * shell namespace. The target folder, read from the property bag above, remains untouched.
1195 * The following tests show this: The itemidlist for some imaginary shellfolder object
1196 * is created and the FolderShortcut is initialized with it. GetCurFolder now returns this
1197 * itemidlist, but GetDisplayNameOf still returns the path from above.
1199 hr = SHGetDesktopFolder(&pDesktopFolder);
1200 ok (SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08x\n", hr);
1201 if (FAILED(hr)) return;
1203 /* Temporarily register WineTestFolder as a shell namespace extension at the Desktop.
1204 * Otherwise ParseDisplayName fails on WinXP with E_INVALIDARG */
1205 RegCreateKeyW(HKEY_CURRENT_USER, wszShellExtKey, &hShellExtKey);
1206 RegCloseKey(hShellExtKey);
1207 hr = IShellFolder_ParseDisplayName(pDesktopFolder, NULL, NULL, wszWineTestFolder, NULL,
1208 &pidlWineTestFolder, NULL);
1209 RegDeleteKeyW(HKEY_CURRENT_USER, wszShellExtKey);
1210 IShellFolder_Release(pDesktopFolder);
1211 ok (SUCCEEDED(hr), "IShellFolder::ParseDisplayName failed! hr = %08x\n", hr);
1212 if (FAILED(hr)) return;
1214 hr = IPersistFolder3_Initialize(pPersistFolder3, pidlWineTestFolder);
1215 ok (SUCCEEDED(hr), "IPersistFolder3::Initialize failed! hr = %08x\n", hr);
1216 if (FAILED(hr)) {
1217 IPersistFolder3_Release(pPersistFolder3);
1218 pILFree(pidlWineTestFolder);
1219 return;
1222 hr = IPersistFolder3_GetCurFolder(pPersistFolder3, &pidlCurrentFolder);
1223 ok(SUCCEEDED(hr), "IPersistFolder3_GetCurFolder failed! hr=0x%08x\n", hr);
1224 ok(pILIsEqual(pidlCurrentFolder, pidlWineTestFolder),
1225 "IPersistFolder3_GetCurFolder should return pidlWineTestFolder!\n");
1226 pILFree(pidlCurrentFolder);
1227 pILFree(pidlWineTestFolder);
1229 hr = IPersistFolder3_QueryInterface(pPersistFolder3, &IID_IShellFolder, (LPVOID*)&pShellFolder);
1230 IPersistFolder3_Release(pPersistFolder3);
1231 ok(SUCCEEDED(hr), "IPersistFolder3_QueryInterface(IShellFolder) failed! hr = %08x\n", hr);
1232 if (FAILED(hr)) return;
1234 hr = IShellFolder_GetDisplayNameOf(pShellFolder, NULL, SHGDN_FORPARSING, &strret);
1235 ok(SUCCEEDED(hr), "IShellFolder_GetDisplayNameOf(NULL) failed! hr = %08x\n", hr);
1236 if (FAILED(hr)) {
1237 IShellFolder_Release(pShellFolder);
1238 return;
1241 pStrRetToBufW(&strret, NULL, wszBuffer, MAX_PATH);
1242 ok(!lstrcmpiW(wszDesktopPath, wszBuffer), "FolderShortcut returned incorrect folder!\n");
1244 /* Next few lines are meant to show that children of FolderShortcuts are not FolderShortcuts,
1245 * but ShellFSFolders. */
1246 myPathAddBackslashW(wszDesktopPath);
1247 lstrcatW(wszDesktopPath, wszSomeSubFolder);
1248 if (!CreateDirectoryW(wszDesktopPath, NULL)) {
1249 IShellFolder_Release(pShellFolder);
1250 return;
1253 hr = IShellFolder_ParseDisplayName(pShellFolder, NULL, NULL, wszSomeSubFolder, NULL,
1254 &pidlSubFolder, NULL);
1255 RemoveDirectoryW(wszDesktopPath);
1256 ok (SUCCEEDED(hr), "IShellFolder::ParseDisplayName failed! hr = %08x\n", hr);
1257 if (FAILED(hr)) {
1258 IShellFolder_Release(pShellFolder);
1259 return;
1262 hr = IShellFolder_BindToObject(pShellFolder, pidlSubFolder, NULL, &IID_IPersistFolder3,
1263 (LPVOID*)&pPersistFolder3);
1264 IShellFolder_Release(pShellFolder);
1265 pILFree(pidlSubFolder);
1266 ok (SUCCEEDED(hr), "IShellFolder::BindToObject failed! hr = %08x\n", hr);
1267 if (FAILED(hr))
1268 return;
1270 /* On windows, we expect CLSID_ShellFSFolder. On wine we relax this constraint
1271 * a little bit and also allow CLSID_UnixDosFolder. */
1272 hr = IPersistFolder3_GetClassID(pPersistFolder3, &clsid);
1273 ok(SUCCEEDED(hr), "IPersistFolder3_GetClassID failed! hr=0x%08x\n", hr);
1274 ok(IsEqualCLSID(&clsid, &CLSID_ShellFSFolder) || IsEqualCLSID(&clsid, &CLSID_UnixDosFolder),
1275 "IPersistFolder3::GetClassID returned unexpected CLSID!\n");
1277 IPersistFolder3_Release(pPersistFolder3);
1280 #include "pshpack1.h"
1281 struct FileStructA {
1282 BYTE type;
1283 BYTE dummy;
1284 DWORD dwFileSize;
1285 WORD uFileDate; /* In our current implementation this is */
1286 WORD uFileTime; /* FileTimeToDosDate(WIN32_FIND_DATA->ftLastWriteTime) */
1287 WORD uFileAttribs;
1288 CHAR szName[1];
1291 struct FileStructW {
1292 WORD cbLen; /* Length of this element. */
1293 BYTE abFooBar1[6]; /* Beyond any recognition. */
1294 WORD uDate; /* FileTimeToDosDate(WIN32_FIND_DATA->ftCreationTime)? */
1295 WORD uTime; /* (this is currently speculation) */
1296 WORD uDate2; /* FileTimeToDosDate(WIN32_FIND_DATA->ftLastAccessTime)? */
1297 WORD uTime2; /* (this is currently speculation) */
1298 BYTE abFooBar2[4]; /* Beyond any recognition. */
1299 WCHAR wszName[1]; /* The long filename in unicode. */
1300 /* Just for documentation: Right after the unicode string: */
1301 WORD cbOffset; /* FileStructW's offset from the beginning of the SHITMEID.
1302 * SHITEMID->cb == uOffset + cbLen */
1304 #include "poppack.h"
1306 static void test_ITEMIDLIST_format(void) {
1307 WCHAR wszPersonal[MAX_PATH];
1308 LPSHELLFOLDER psfDesktop, psfPersonal;
1309 LPITEMIDLIST pidlPersonal, pidlFile;
1310 HANDLE hFile;
1311 HRESULT hr;
1312 BOOL bResult;
1313 WCHAR wszFile[3][17] = { { 'e','v','e','n','_',0 }, { 'o','d','d','_',0 },
1314 { 'l','o','n','g','e','r','_','t','h','a','n','.','8','_','3',0 } };
1315 int i;
1317 if(!pSHGetSpecialFolderPathW) return;
1319 bResult = pSHGetSpecialFolderPathW(NULL, wszPersonal, CSIDL_PERSONAL, FALSE);
1320 ok(bResult, "SHGetSpecialFolderPathW failed! Last error: %u\n", GetLastError());
1321 if (!bResult) return;
1323 SetLastError(0xdeadbeef);
1324 bResult = SetCurrentDirectoryW(wszPersonal);
1325 if (!bResult && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) {
1326 win_skip("Most W-calls are not implemented\n");
1327 return;
1329 ok(bResult, "SetCurrentDirectory failed! Last error: %u\n", GetLastError());
1330 if (!bResult) return;
1332 hr = SHGetDesktopFolder(&psfDesktop);
1333 ok(SUCCEEDED(hr), "SHGetDesktopFolder failed! hr: %08x\n", hr);
1334 if (FAILED(hr)) return;
1336 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszPersonal, NULL, &pidlPersonal, NULL);
1337 ok(SUCCEEDED(hr), "psfDesktop->ParseDisplayName failed! hr = %08x\n", hr);
1338 if (FAILED(hr)) {
1339 IShellFolder_Release(psfDesktop);
1340 return;
1343 hr = IShellFolder_BindToObject(psfDesktop, pidlPersonal, NULL, &IID_IShellFolder,
1344 (LPVOID*)&psfPersonal);
1345 IShellFolder_Release(psfDesktop);
1346 pILFree(pidlPersonal);
1347 ok(SUCCEEDED(hr), "psfDesktop->BindToObject failed! hr = %08x\n", hr);
1348 if (FAILED(hr)) return;
1350 for (i=0; i<3; i++) {
1351 CHAR szFile[MAX_PATH];
1352 struct FileStructA *pFileStructA;
1353 WORD cbOffset;
1355 WideCharToMultiByte(CP_ACP, 0, wszFile[i], -1, szFile, MAX_PATH, NULL, NULL);
1357 hFile = CreateFileW(wszFile[i], GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_FLAG_WRITE_THROUGH, NULL);
1358 ok(hFile != INVALID_HANDLE_VALUE, "CreateFile failed! (%u)\n", GetLastError());
1359 if (hFile == INVALID_HANDLE_VALUE) {
1360 IShellFolder_Release(psfPersonal);
1361 return;
1363 CloseHandle(hFile);
1365 hr = IShellFolder_ParseDisplayName(psfPersonal, NULL, NULL, wszFile[i], NULL, &pidlFile, NULL);
1366 DeleteFileW(wszFile[i]);
1367 ok(SUCCEEDED(hr), "psfPersonal->ParseDisplayName failed! hr: %08x\n", hr);
1368 if (FAILED(hr)) {
1369 IShellFolder_Release(psfPersonal);
1370 return;
1373 pFileStructA = (struct FileStructA *)pidlFile->mkid.abID;
1374 ok(pFileStructA->type == 0x32, "PIDLTYPE should be 0x32!\n");
1375 ok(pFileStructA->dummy == 0x00, "Dummy Byte should be 0x00!\n");
1376 ok(pFileStructA->dwFileSize == 0, "Filesize should be zero!\n");
1378 if (i < 2) /* First two file names are already in valid 8.3 format */
1379 ok(!strcmp(szFile, (CHAR*)&pidlFile->mkid.abID[12]), "Wrong file name!\n");
1380 else
1381 /* WinXP stores a derived 8.3 dos name (LONGER~1.8_3) here. We probably
1382 * can't implement this correctly, since unix filesystems don't support
1383 * this nasty short/long filename stuff. So we'll probably stay with our
1384 * current habbit of storing the long filename here, which seems to work
1385 * just fine. */
1386 todo_wine { ok(pidlFile->mkid.abID[18] == '~', "Should be derived 8.3 name!\n"); }
1388 if (i == 0) /* First file name has an even number of chars. No need for alignment. */
1389 ok(pidlFile->mkid.abID[12 + strlen(szFile) + 1] != '\0',
1390 "Alignment byte, where there shouldn't be!\n");
1392 if (i == 1) /* Second file name has an uneven number of chars => alignment byte */
1393 ok(pidlFile->mkid.abID[12 + strlen(szFile) + 1] == '\0',
1394 "There should be an alignment byte, but isn't!\n");
1396 /* The offset of the FileStructW member is stored as a WORD at the end of the pidl. */
1397 cbOffset = *(WORD*)(((LPBYTE)pidlFile)+pidlFile->mkid.cb-sizeof(WORD));
1398 ok (cbOffset >= sizeof(struct FileStructA) &&
1399 cbOffset <= pidlFile->mkid.cb - sizeof(struct FileStructW),
1400 "Wrong offset value (%d) stored at the end of the PIDL\n", cbOffset);
1402 if (cbOffset >= sizeof(struct FileStructA) &&
1403 cbOffset <= pidlFile->mkid.cb - sizeof(struct FileStructW))
1405 struct FileStructW *pFileStructW = (struct FileStructW *)(((LPBYTE)pidlFile)+cbOffset);
1407 ok(pidlFile->mkid.cb == cbOffset + pFileStructW->cbLen,
1408 "FileStructW's offset and length should add up to the PIDL's length!\n");
1410 if (pidlFile->mkid.cb == cbOffset + pFileStructW->cbLen) {
1411 /* Since we just created the file, time of creation,
1412 * time of last access and time of last write access just be the same.
1413 * These tests seem to fail sometimes (on WinXP), if the test is run again shortly
1414 * after the first run. I do remember something with NTFS keeping the creation time
1415 * if a file is deleted and then created again within a couple of seconds or so.
1416 * Might be the reason. */
1417 ok (pFileStructA->uFileDate == pFileStructW->uDate &&
1418 pFileStructA->uFileTime == pFileStructW->uTime,
1419 "Last write time should match creation time!\n");
1421 ok (pFileStructA->uFileDate == pFileStructW->uDate2 &&
1422 pFileStructA->uFileTime == pFileStructW->uTime2,
1423 "Last write time should match last access time!\n");
1425 ok (!lstrcmpW(wszFile[i], pFileStructW->wszName),
1426 "The filename should be stored in unicode at this position!\n");
1430 pILFree(pidlFile);
1434 static void testSHGetFolderPathAndSubDirA(void)
1436 HRESULT ret;
1437 BOOL delret;
1438 DWORD dwret;
1439 int i;
1440 static char wine[] = "wine";
1441 static char winetemp[] = "wine\\temp";
1442 static char appdata[MAX_PATH];
1443 static char testpath[MAX_PATH];
1444 static char toolongpath[MAX_PATH+1];
1446 if(!pSHGetFolderPathA) {
1447 skip("SHGetFolderPathA not present!\n");
1448 return;
1450 if(FAILED(pSHGetFolderPathA(NULL, CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, appdata)))
1452 skip("SHGetFolderPathA failed for CSIDL_LOCAL_APPDATA!\n");
1453 return;
1456 sprintf(testpath, "%s\\%s", appdata, winetemp);
1457 delret = RemoveDirectoryA(testpath);
1458 if(!delret && (ERROR_PATH_NOT_FOUND != GetLastError()) ) {
1459 skip("RemoveDirectoryA(%s) failed with error %u\n", testpath, GetLastError());
1460 return;
1463 sprintf(testpath, "%s\\%s", appdata, wine);
1464 delret = RemoveDirectoryA(testpath);
1465 if(!delret && (ERROR_PATH_NOT_FOUND != GetLastError()) && (ERROR_FILE_NOT_FOUND != GetLastError())) {
1466 skip("RemoveDirectoryA(%s) failed with error %u\n", testpath, GetLastError());
1467 return;
1469 for(i=0; i< MAX_PATH; i++)
1470 toolongpath[i] = '0' + i % 10;
1471 toolongpath[MAX_PATH] = '\0';
1473 /* test invalid second parameter */
1474 ret = pSHGetFolderPathAndSubDirA(NULL, CSIDL_FLAG_DONT_VERIFY | 0xff, NULL, SHGFP_TYPE_CURRENT, wine, testpath);
1475 ok(E_INVALIDARG == ret, "expected E_INVALIDARG, got %x\n", ret);
1477 /* test invalid forth parameter */
1478 ret = pSHGetFolderPathAndSubDirA(NULL, CSIDL_FLAG_DONT_VERIFY | CSIDL_LOCAL_APPDATA, NULL, 2, wine, testpath);
1479 switch(ret) {
1480 case S_OK: /* winvista */
1481 ok(!strncmp(appdata, testpath, strlen(appdata)),
1482 "expected %s to start with %s\n", testpath, appdata);
1483 ok(!lstrcmpA(&testpath[1 + strlen(appdata)], winetemp),
1484 "expected %s to end with %s\n", testpath, winetemp);
1485 break;
1486 case E_INVALIDARG: /* winxp, win2k3 */
1487 break;
1488 default:
1489 ok(0, "expected S_OK or E_INVALIDARG, got %x\n", ret);
1492 /* test fifth parameter */
1493 testpath[0] = '\0';
1494 ret = pSHGetFolderPathAndSubDirA(NULL, CSIDL_FLAG_DONT_VERIFY | CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, NULL, testpath);
1495 ok(S_OK == ret, "expected S_OK, got %x\n", ret);
1496 ok(!lstrcmpA(appdata, testpath), "expected %s, got %s\n", appdata, testpath);
1498 testpath[0] = '\0';
1499 ret = pSHGetFolderPathAndSubDirA(NULL, CSIDL_FLAG_DONT_VERIFY | CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, "", testpath);
1500 ok(S_OK == ret, "expected S_OK, got %x\n", ret);
1501 ok(!lstrcmpA(appdata, testpath), "expected %s, got %s\n", appdata, testpath);
1503 testpath[0] = '\0';
1504 ret = pSHGetFolderPathAndSubDirA(NULL, CSIDL_FLAG_DONT_VERIFY | CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, "\\", testpath);
1505 ok(S_OK == ret, "expected S_OK, got %x\n", ret);
1506 ok(!lstrcmpA(appdata, testpath), "expected %s, got %s\n", appdata, testpath);
1508 ret = pSHGetFolderPathAndSubDirA(NULL, CSIDL_FLAG_DONT_VERIFY | CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, toolongpath, testpath);
1509 ok(HRESULT_FROM_WIN32(ERROR_FILENAME_EXCED_RANGE) == ret,
1510 "expected %x, got %x\n", HRESULT_FROM_WIN32(ERROR_FILENAME_EXCED_RANGE), ret);
1512 testpath[0] = '\0';
1513 ret = pSHGetFolderPathAndSubDirA(NULL, CSIDL_FLAG_DONT_VERIFY | CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, wine, NULL);
1514 ok((S_OK == ret) || (E_INVALIDARG == ret), "expected S_OK or E_INVALIDARG, got %x\n", ret);
1516 /* test a not existing path */
1517 testpath[0] = '\0';
1518 ret = pSHGetFolderPathAndSubDirA(NULL, CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, winetemp, testpath);
1519 ok(HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND) == ret,
1520 "expected %x, got %x\n", HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND), ret);
1522 /* create a directory inside a not existing directory */
1523 testpath[0] = '\0';
1524 ret = pSHGetFolderPathAndSubDirA(NULL, CSIDL_FLAG_CREATE | CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, winetemp, testpath);
1525 ok(S_OK == ret, "expected S_OK, got %x\n", ret);
1526 ok(!strncmp(appdata, testpath, strlen(appdata)),
1527 "expected %s to start with %s\n", testpath, appdata);
1528 ok(!lstrcmpA(&testpath[1 + strlen(appdata)], winetemp),
1529 "expected %s to end with %s\n", testpath, winetemp);
1530 dwret = GetFileAttributes(testpath);
1531 ok(FILE_ATTRIBUTE_DIRECTORY | dwret, "expected %x to contain FILE_ATTRIBUTE_DIRECTORY\n", dwret);
1533 /* cleanup */
1534 sprintf(testpath, "%s\\%s", appdata, winetemp);
1535 RemoveDirectoryA(testpath);
1536 sprintf(testpath, "%s\\%s", appdata, wine);
1537 RemoveDirectoryA(testpath);
1540 static const char *wine_dbgstr_w(LPCWSTR str)
1542 static char buf[512];
1543 if (!str)
1544 return "(null)";
1545 WideCharToMultiByte(CP_ACP, 0, str, -1, buf, sizeof(buf), NULL, NULL);
1546 return buf;
1549 static void test_LocalizedNames(void)
1551 static char cCurrDirA[MAX_PATH];
1552 WCHAR cCurrDirW[MAX_PATH], tempbufW[25];
1553 IShellFolder *IDesktopFolder, *testIShellFolder;
1554 ITEMIDLIST *newPIDL;
1555 int len;
1556 HRESULT hr;
1557 static char resourcefile[MAX_PATH];
1558 DWORD res;
1559 HANDLE file;
1560 STRRET strret;
1562 static const char desktopini_contents1[] =
1563 "[.ShellClassInfo]\r\n"
1564 "LocalizedResourceName=@";
1565 static const char desktopini_contents2[] =
1566 ",-1\r\n";
1567 static WCHAR foldernameW[] = {'t','e','s','t','f','o','l','d','e','r',0};
1568 static const WCHAR folderdisplayW[] = {'F','o','l','d','e','r',' ','N','a','m','e',' ','R','e','s','o','u','r','c','e',0};
1570 /* create folder with desktop.ini and localized name in GetModuleFileNameA(NULL) */
1571 CreateDirectoryA(".\\testfolder", NULL);
1573 SetFileAttributesA(".\\testfolder", GetFileAttributesA(".\\testfolder")|FILE_ATTRIBUTE_SYSTEM);
1575 GetModuleFileNameA(NULL, resourcefile, MAX_PATH);
1577 file = CreateFileA(".\\testfolder\\desktop.ini", GENERIC_WRITE, 0, NULL,
1578 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1579 ok(file != INVALID_HANDLE_VALUE, "CreateFileA failed %i\n", GetLastError());
1580 ok(WriteFile(file, desktopini_contents1, strlen(desktopini_contents1), &res, NULL) &&
1581 WriteFile(file, resourcefile, strlen(resourcefile), &res, NULL) &&
1582 WriteFile(file, desktopini_contents2, strlen(desktopini_contents2), &res, NULL),
1583 "WriteFile failed %i\n", GetLastError());
1584 CloseHandle(file);
1586 /* get IShellFolder for parent */
1587 GetCurrentDirectoryA(MAX_PATH, cCurrDirA);
1588 len = lstrlenA(cCurrDirA);
1590 if (len == 0) {
1591 trace("GetCurrentDirectoryA returned empty string. Skipping test_LocalizedNames\n");
1592 goto cleanup;
1594 if(cCurrDirA[len-1] == '\\')
1595 cCurrDirA[len-1] = 0;
1597 MultiByteToWideChar(CP_ACP, 0, cCurrDirA, -1, cCurrDirW, MAX_PATH);
1599 hr = SHGetDesktopFolder(&IDesktopFolder);
1600 ok(hr == S_OK, "SHGetDesktopfolder failed %08x\n", hr);
1602 hr = IShellFolder_ParseDisplayName(IDesktopFolder, NULL, NULL, cCurrDirW, NULL, &newPIDL, 0);
1603 ok(hr == S_OK, "ParseDisplayName failed %08x\n", hr);
1605 hr = IShellFolder_BindToObject(IDesktopFolder, newPIDL, NULL, (REFIID)&IID_IShellFolder, (LPVOID *)&testIShellFolder);
1606 ok(hr == S_OK, "BindToObject failed %08x\n", hr);
1608 IMalloc_Free(ppM, newPIDL);
1610 /* windows reads the display name from the resource */
1611 hr = IShellFolder_ParseDisplayName(testIShellFolder, NULL, NULL, foldernameW, NULL, &newPIDL, 0);
1612 ok(hr == S_OK, "ParseDisplayName failed %08x\n", hr);
1614 hr = IShellFolder_GetDisplayNameOf(testIShellFolder, newPIDL, SHGDN_INFOLDER, &strret);
1615 ok(hr == S_OK, "GetDisplayNameOf failed %08x\n", hr);
1617 if (SUCCEEDED(hr) && pStrRetToBufW)
1619 hr = pStrRetToBufW(&strret, newPIDL, tempbufW, sizeof(tempbufW)/sizeof(WCHAR));
1620 ok (SUCCEEDED(hr), "StrRetToBufW failed! hr = %08x\n", hr);
1621 todo_wine
1622 ok (!lstrcmpiW(tempbufW, folderdisplayW) ||
1623 broken(!lstrcmpiW(tempbufW, foldernameW)), /* W2K */
1624 "GetDisplayNameOf returned %s\n", wine_dbgstr_w(tempbufW));
1627 /* editing name is also read from the resource */
1628 hr = IShellFolder_GetDisplayNameOf(testIShellFolder, newPIDL, SHGDN_INFOLDER|SHGDN_FOREDITING, &strret);
1629 ok(hr == S_OK, "GetDisplayNameOf failed %08x\n", hr);
1631 if (SUCCEEDED(hr) && pStrRetToBufW)
1633 hr = pStrRetToBufW(&strret, newPIDL, tempbufW, sizeof(tempbufW)/sizeof(WCHAR));
1634 ok (SUCCEEDED(hr), "StrRetToBufW failed! hr = %08x\n", hr);
1635 todo_wine
1636 ok (!lstrcmpiW(tempbufW, folderdisplayW) ||
1637 broken(!lstrcmpiW(tempbufW, foldernameW)), /* W2K */
1638 "GetDisplayNameOf returned %s\n", wine_dbgstr_w(tempbufW));
1641 /* parsing name is unchanged */
1642 hr = IShellFolder_GetDisplayNameOf(testIShellFolder, newPIDL, SHGDN_INFOLDER|SHGDN_FORPARSING, &strret);
1643 ok(hr == S_OK, "GetDisplayNameOf failed %08x\n", hr);
1645 if (SUCCEEDED(hr) && pStrRetToBufW)
1647 hr = pStrRetToBufW(&strret, newPIDL, tempbufW, sizeof(tempbufW)/sizeof(WCHAR));
1648 ok (SUCCEEDED(hr), "StrRetToBufW failed! hr = %08x\n", hr);
1649 ok (!lstrcmpiW(tempbufW, foldernameW), "GetDisplayNameOf returned %s\n", wine_dbgstr_w(tempbufW));
1652 IShellFolder_Release(IDesktopFolder);
1653 IShellFolder_Release(testIShellFolder);
1655 IMalloc_Free(ppM, newPIDL);
1657 cleanup:
1658 DeleteFileA(".\\testfolder\\desktop.ini");
1659 SetFileAttributesA(".\\testfolder", GetFileAttributesA(".\\testfolder")&~FILE_ATTRIBUTE_SYSTEM);
1660 RemoveDirectoryA(".\\testfolder");
1664 START_TEST(shlfolder)
1666 init_function_pointers();
1667 /* if OleInitialize doesn't get called, ParseDisplayName returns
1668 CO_E_NOTINITIALIZED for malformed directory names on win2k. */
1669 OleInitialize(NULL);
1671 test_ParseDisplayName();
1672 test_BindToObject();
1673 test_EnumObjects_and_CompareIDs();
1674 test_GetDisplayName();
1675 test_GetAttributesOf();
1676 test_SHGetPathFromIDList();
1677 test_CallForAttributes();
1678 test_FolderShortcut();
1679 test_ITEMIDLIST_format();
1680 if(pSHGetFolderPathAndSubDirA)
1681 testSHGetFolderPathAndSubDirA();
1682 else
1683 skip("SHGetFolderPathAndSubDirA not present\n");
1684 test_LocalizedNames();
1686 OleUninitialize();