push 337eb2e2d902d84a5d689451984c5832d7e04fc4
[wine/hacks.git] / dlls / shell32 / tests / shlfolder.c
blob6a06fb7b078d5925b3085a17cd33d1327712cf47
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 *pSHGetSpecialFolderPathW)(HWND, LPWSTR, int, BOOL);
50 static HRESULT (WINAPI *pStrRetToBufW)(STRRET*,LPCITEMIDLIST,LPWSTR,UINT);
51 static LPITEMIDLIST (WINAPI *pILFindLastID)(LPCITEMIDLIST);
52 static void (WINAPI *pILFree)(LPITEMIDLIST);
53 static BOOL (WINAPI *pILIsEqual)(LPCITEMIDLIST, LPCITEMIDLIST);
55 static void init_function_pointers(void)
57 HMODULE hmod;
58 HRESULT hr;
60 hmod = GetModuleHandleA("shell32.dll");
61 pSHBindToParent = (void*)GetProcAddress(hmod, "SHBindToParent");
62 pSHGetFolderPathA = (void*)GetProcAddress(hmod, "SHGetFolderPathA");
63 pSHGetFolderPathAndSubDirA = (void*)GetProcAddress(hmod, "SHGetFolderPathAndSubDirA");
64 pSHGetPathFromIDListW = (void*)GetProcAddress(hmod, "SHGetPathFromIDListW");
65 pSHGetSpecialFolderPathW = (void*)GetProcAddress(hmod, "SHGetSpecialFolderPathW");
66 pILFindLastID = (void *)GetProcAddress(hmod, (LPCSTR)16);
67 pILFree = (void*)GetProcAddress(hmod, (LPSTR)155);
68 pILIsEqual = (void*)GetProcAddress(hmod, (LPSTR)21);
70 hmod = GetModuleHandleA("shlwapi.dll");
71 pStrRetToBufW = (void*)GetProcAddress(hmod, "StrRetToBufW");
73 hr = SHGetMalloc(&ppM);
74 ok(hr == S_OK, "SHGetMalloc failed %08x\n", hr);
77 static void test_ParseDisplayName(void)
79 HRESULT hr;
80 IShellFolder *IDesktopFolder;
81 static const char *cNonExistDir1A = "c:\\nonexist_subdir";
82 static const char *cNonExistDir2A = "c:\\\\nonexist_subdir";
83 DWORD res;
84 WCHAR cTestDirW [MAX_PATH] = {0};
85 ITEMIDLIST *newPIDL;
86 BOOL bRes;
88 hr = SHGetDesktopFolder(&IDesktopFolder);
89 if(hr != S_OK) return;
91 res = GetFileAttributesA(cNonExistDir1A);
92 if(res != INVALID_FILE_ATTRIBUTES) return;
94 MultiByteToWideChar(CP_ACP, 0, cNonExistDir1A, -1, cTestDirW, MAX_PATH);
95 hr = IShellFolder_ParseDisplayName(IDesktopFolder,
96 NULL, NULL, cTestDirW, NULL, &newPIDL, 0);
97 ok((hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) || (hr == E_FAIL),
98 "ParseDisplayName returned %08x, expected 80070002 or E_FAIL\n", hr);
100 res = GetFileAttributesA(cNonExistDir2A);
101 if(res != INVALID_FILE_ATTRIBUTES) return;
103 MultiByteToWideChar(CP_ACP, 0, cNonExistDir2A, -1, cTestDirW, MAX_PATH);
104 hr = IShellFolder_ParseDisplayName(IDesktopFolder,
105 NULL, NULL, cTestDirW, NULL, &newPIDL, 0);
106 ok((hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) || (hr == E_FAIL) || (hr == E_INVALIDARG),
107 "ParseDisplayName returned %08x, expected 80070002, E_FAIL or E_INVALIDARG\n", hr);
109 /* I thought that perhaps the DesktopFolder's ParseDisplayName would recognize the
110 * path corresponding to CSIDL_PERSONAL and return a CLSID_MyDocuments PIDL. Turns
111 * out it doesn't. The magic seems to happen in the file dialogs, then. */
112 if (!pSHGetSpecialFolderPathW || !pILFindLastID) goto finished;
114 bRes = pSHGetSpecialFolderPathW(NULL, cTestDirW, CSIDL_PERSONAL, FALSE);
115 ok(bRes, "SHGetSpecialFolderPath(CSIDL_PERSONAL) failed! %u\n", GetLastError());
116 if (!bRes) goto finished;
118 hr = IShellFolder_ParseDisplayName(IDesktopFolder, NULL, NULL, cTestDirW, NULL, &newPIDL, 0);
119 ok(SUCCEEDED(hr), "DesktopFolder->ParseDisplayName failed. hr = %08x.\n", hr);
120 if (FAILED(hr)) goto finished;
122 ok(pILFindLastID(newPIDL)->mkid.abID[0] == 0x31, "Last pidl should be of type "
123 "PT_FOLDER, but is: %02x\n", pILFindLastID(newPIDL)->mkid.abID[0]);
124 IMalloc_Free(ppM, newPIDL);
126 finished:
127 IShellFolder_Release(IDesktopFolder);
130 /* creates a file with the specified name for tests */
131 static void CreateTestFile(const CHAR *name)
133 HANDLE file;
134 DWORD written;
136 file = CreateFileA(name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
137 if (file != INVALID_HANDLE_VALUE)
139 WriteFile(file, name, strlen(name), &written, NULL);
140 WriteFile(file, "\n", strlen("\n"), &written, NULL);
141 CloseHandle(file);
146 /* initializes the tests */
147 static void CreateFilesFolders(void)
149 CreateDirectoryA(".\\testdir", NULL);
150 CreateDirectoryA(".\\testdir\\test.txt", NULL);
151 CreateTestFile (".\\testdir\\test1.txt ");
152 CreateTestFile (".\\testdir\\test2.txt ");
153 CreateTestFile (".\\testdir\\test3.txt ");
154 CreateDirectoryA(".\\testdir\\testdir2 ", NULL);
155 CreateDirectoryA(".\\testdir\\testdir2\\subdir", NULL);
158 /* cleans after tests */
159 static void Cleanup(void)
161 DeleteFileA(".\\testdir\\test1.txt");
162 DeleteFileA(".\\testdir\\test2.txt");
163 DeleteFileA(".\\testdir\\test3.txt");
164 RemoveDirectoryA(".\\testdir\\test.txt");
165 RemoveDirectoryA(".\\testdir\\testdir2\\subdir");
166 RemoveDirectoryA(".\\testdir\\testdir2");
167 RemoveDirectoryA(".\\testdir");
171 /* perform test */
172 static void test_EnumObjects(IShellFolder *iFolder)
174 IEnumIDList *iEnumList;
175 LPITEMIDLIST newPIDL, idlArr[10];
176 ULONG NumPIDLs;
177 int i=0, j;
178 HRESULT hr;
180 static const WORD iResults [5][5] =
182 { 0,-1,-1,-1,-1},
183 { 1, 0,-1,-1,-1},
184 { 1, 1, 0,-1,-1},
185 { 1, 1, 1, 0,-1},
186 { 1, 1, 1, 1, 0}
189 #define SFGAO_testfor SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR | SFGAO_CAPABILITYMASK
190 /* Don't test for SFGAO_HASSUBFOLDER since we return real state and native cached */
191 static const ULONG attrs[5] =
193 SFGAO_CAPABILITYMASK | SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR,
194 SFGAO_CAPABILITYMASK | SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR,
195 SFGAO_CAPABILITYMASK | SFGAO_FILESYSTEM,
196 SFGAO_CAPABILITYMASK | SFGAO_FILESYSTEM,
197 SFGAO_CAPABILITYMASK | SFGAO_FILESYSTEM,
200 hr = IShellFolder_EnumObjects(iFolder, NULL, SHCONTF_FOLDERS | SHCONTF_NONFOLDERS | SHCONTF_INCLUDEHIDDEN, &iEnumList);
201 ok(hr == S_OK, "EnumObjects failed %08x\n", hr);
203 /* This is to show that, contrary to what is said on MSDN, on IEnumIDList::Next,
204 * the filesystem shellfolders return S_OK even if less than 'celt' items are
205 * returned (in contrast to S_FALSE). We have to do it in a loop since WinXP
206 * only ever returns a single entry per call. */
207 while (IEnumIDList_Next(iEnumList, 10-i, &idlArr[i], &NumPIDLs) == S_OK)
208 i += NumPIDLs;
209 ok (i == 5, "i: %d\n", i);
211 hr = IEnumIDList_Release(iEnumList);
212 ok(hr == S_OK, "IEnumIDList_Release failed %08x\n", hr);
214 /* Sort them first in case of wrong order from system */
215 for (i=0;i<5;i++) for (j=0;j<5;j++)
216 if ((SHORT)IShellFolder_CompareIDs(iFolder, 0, idlArr[i], idlArr[j]) < 0)
218 newPIDL = idlArr[i];
219 idlArr[i] = idlArr[j];
220 idlArr[j] = newPIDL;
223 for (i=0;i<5;i++) for (j=0;j<5;j++)
225 hr = IShellFolder_CompareIDs(iFolder, 0, idlArr[i], idlArr[j]);
226 ok(hr == iResults[i][j], "Got %x expected [%d]-[%d]=%x\n", hr, i, j, iResults[i][j]);
230 for (i = 0; i < 5; i++)
232 SFGAOF flags;
233 /* Native returns all flags no matter what we ask for */
234 flags = SFGAO_CANCOPY;
235 hr = IShellFolder_GetAttributesOf(iFolder, 1, (LPCITEMIDLIST*)(idlArr + i), &flags);
236 flags &= SFGAO_testfor;
237 ok(hr == S_OK, "GetAttributesOf returns %08x\n", hr);
238 ok(flags == (attrs[i]), "GetAttributesOf[%i] got %08x, expected %08x\n", i, flags, attrs[i]);
240 flags = SFGAO_testfor;
241 hr = IShellFolder_GetAttributesOf(iFolder, 1, (LPCITEMIDLIST*)(idlArr + i), &flags);
242 flags &= SFGAO_testfor;
243 ok(hr == S_OK, "GetAttributesOf returns %08x\n", hr);
244 ok(flags == attrs[i], "GetAttributesOf[%i] got %08x, expected %08x\n", i, flags, attrs[i]);
247 for (i=0;i<5;i++)
248 IMalloc_Free(ppM, idlArr[i]);
251 static void test_BindToObject(void)
253 HRESULT hr;
254 UINT cChars;
255 IShellFolder *psfDesktop, *psfChild, *psfMyComputer, *psfSystemDir;
256 SHITEMID emptyitem = { 0, { 0 } };
257 LPITEMIDLIST pidlMyComputer, pidlSystemDir, pidlEmpty = (LPITEMIDLIST)&emptyitem;
258 WCHAR wszSystemDir[MAX_PATH];
259 char szSystemDir[MAX_PATH];
260 WCHAR wszMyComputer[] = {
261 ':',':','{','2','0','D','0','4','F','E','0','-','3','A','E','A','-','1','0','6','9','-',
262 'A','2','D','8','-','0','8','0','0','2','B','3','0','3','0','9','D','}',0 };
264 /* The following tests shows that BindToObject should fail with E_INVALIDARG if called
265 * with an empty pidl. This is tested for Desktop, MyComputer and the FS ShellFolder
267 hr = SHGetDesktopFolder(&psfDesktop);
268 ok (SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08x\n", hr);
269 if (FAILED(hr)) return;
271 hr = IShellFolder_BindToObject(psfDesktop, pidlEmpty, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
272 ok (hr == E_INVALIDARG, "Desktop's BindToObject should fail, when called with empty pidl! hr = %08x\n", hr);
274 hr = IShellFolder_BindToObject(psfDesktop, NULL, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
275 ok (hr == E_INVALIDARG, "Desktop's BindToObject should fail, when called with NULL pidl! hr = %08x\n", hr);
277 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszMyComputer, NULL, &pidlMyComputer, NULL);
278 ok (SUCCEEDED(hr), "Desktop's ParseDisplayName failed to parse MyComputer's CLSID! hr = %08x\n", hr);
279 if (FAILED(hr)) {
280 IShellFolder_Release(psfDesktop);
281 return;
284 hr = IShellFolder_BindToObject(psfDesktop, pidlMyComputer, NULL, &IID_IShellFolder, (LPVOID*)&psfMyComputer);
285 ok (SUCCEEDED(hr), "Desktop failed to bind to MyComputer object! hr = %08x\n", hr);
286 IShellFolder_Release(psfDesktop);
287 IMalloc_Free(ppM, pidlMyComputer);
288 if (FAILED(hr)) return;
290 hr = IShellFolder_BindToObject(psfMyComputer, pidlEmpty, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
291 ok (hr == E_INVALIDARG, "MyComputers's BindToObject should fail, when called with empty pidl! hr = %08x\n", hr);
293 #if 0
294 /* this call segfaults on 98SE */
295 hr = IShellFolder_BindToObject(psfMyComputer, NULL, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
296 ok (hr == E_INVALIDARG, "MyComputers's BindToObject should fail, when called with NULL pidl! hr = %08x\n", hr);
297 #endif
299 cChars = GetSystemDirectoryA(szSystemDir, MAX_PATH);
300 ok (cChars > 0 && cChars < MAX_PATH, "GetSystemDirectoryA failed! LastError: %u\n", GetLastError());
301 if (cChars == 0 || cChars >= MAX_PATH) {
302 IShellFolder_Release(psfMyComputer);
303 return;
305 MultiByteToWideChar(CP_ACP, 0, szSystemDir, -1, wszSystemDir, MAX_PATH);
307 hr = IShellFolder_ParseDisplayName(psfMyComputer, NULL, NULL, wszSystemDir, NULL, &pidlSystemDir, NULL);
308 ok (SUCCEEDED(hr), "MyComputers's ParseDisplayName failed to parse the SystemDirectory! hr = %08x\n", hr);
309 if (FAILED(hr)) {
310 IShellFolder_Release(psfMyComputer);
311 return;
314 hr = IShellFolder_BindToObject(psfMyComputer, pidlSystemDir, NULL, &IID_IShellFolder, (LPVOID*)&psfSystemDir);
315 ok (SUCCEEDED(hr), "MyComputer failed to bind to a FileSystem ShellFolder! hr = %08x\n", hr);
316 IShellFolder_Release(psfMyComputer);
317 IMalloc_Free(ppM, pidlSystemDir);
318 if (FAILED(hr)) return;
320 hr = IShellFolder_BindToObject(psfSystemDir, pidlEmpty, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
321 ok (hr == E_INVALIDARG,
322 "FileSystem ShellFolder's BindToObject should fail, when called with empty pidl! hr = %08x\n", hr);
324 #if 0
325 /* this call segfaults on 98SE */
326 hr = IShellFolder_BindToObject(psfSystemDir, NULL, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
327 ok (hr == E_INVALIDARG,
328 "FileSystem ShellFolder's BindToObject should fail, when called with NULL pidl! hr = %08x\n", hr);
329 #endif
331 IShellFolder_Release(psfSystemDir);
334 /* Based on PathAddBackslashW from dlls/shlwapi/path.c */
335 static LPWSTR myPathAddBackslashW( LPWSTR lpszPath )
337 size_t iLen;
339 if (!lpszPath || (iLen = lstrlenW(lpszPath)) >= MAX_PATH)
340 return NULL;
342 if (iLen)
344 lpszPath += iLen;
345 if (lpszPath[-1] != '\\')
347 *lpszPath++ = '\\';
348 *lpszPath = '\0';
351 return lpszPath;
354 static void test_GetDisplayName(void)
356 BOOL result;
357 HRESULT hr;
358 HANDLE hTestFile;
359 WCHAR wszTestFile[MAX_PATH], wszTestFile2[MAX_PATH], wszTestDir[MAX_PATH];
360 char szTestFile[MAX_PATH], szTestDir[MAX_PATH];
361 DWORD attr;
362 STRRET strret;
363 LPSHELLFOLDER psfDesktop, psfPersonal;
364 IUnknown *psfFile;
365 SHITEMID emptyitem = { 0, { 0 } };
366 LPITEMIDLIST pidlTestFile, pidlEmpty = (LPITEMIDLIST)&emptyitem;
367 LPCITEMIDLIST pidlLast;
368 static const WCHAR wszFileName[] = { 'w','i','n','e','t','e','s','t','.','f','o','o',0 };
369 static const WCHAR wszDirName[] = { 'w','i','n','e','t','e','s','t',0 };
371 /* I'm trying to figure if there is a functional difference between calling
372 * SHGetPathFromIDListW and calling GetDisplayNameOf(SHGDN_FORPARSING) after
373 * binding to the shellfolder. One thing I thought of was that perhaps
374 * SHGetPathFromIDListW would be able to get the path to a file, which does
375 * not exist anymore, while the other method wouldn't. It turns out there's
376 * no functional difference in this respect.
379 if(!pSHGetSpecialFolderPathW) return;
381 /* First creating a directory in MyDocuments and a file in this directory. */
382 result = pSHGetSpecialFolderPathW(NULL, wszTestDir, CSIDL_PERSONAL, FALSE);
383 ok(result, "SHGetSpecialFolderPathW failed! Last error: %u\n", GetLastError());
384 if (!result) return;
386 myPathAddBackslashW(wszTestDir);
387 lstrcatW(wszTestDir, wszDirName);
388 /* Use ANSI file functions so this works on Windows 9x */
389 WideCharToMultiByte(CP_ACP, 0, wszTestDir, -1, szTestDir, MAX_PATH, 0, 0);
390 CreateDirectoryA(szTestDir, NULL);
391 attr=GetFileAttributesA(szTestDir);
392 if (attr == INVALID_FILE_ATTRIBUTES || !(attr & FILE_ATTRIBUTE_DIRECTORY))
394 ok(0, "unable to create the '%s' directory\n", szTestDir);
395 return;
398 lstrcpyW(wszTestFile, wszTestDir);
399 myPathAddBackslashW(wszTestFile);
400 lstrcatW(wszTestFile, wszFileName);
401 WideCharToMultiByte(CP_ACP, 0, wszTestFile, -1, szTestFile, MAX_PATH, 0, 0);
403 hTestFile = CreateFileA(szTestFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
404 ok((hTestFile != INVALID_HANDLE_VALUE), "CreateFileA failed! Last error: %u\n", GetLastError());
405 if (hTestFile == INVALID_HANDLE_VALUE) return;
406 CloseHandle(hTestFile);
408 /* Getting an itemidlist for the file. */
409 hr = SHGetDesktopFolder(&psfDesktop);
410 ok(SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08x\n", hr);
411 if (FAILED(hr)) return;
413 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszTestFile, NULL, &pidlTestFile, NULL);
414 ok(SUCCEEDED(hr), "Desktop->ParseDisplayName failed! hr = %08x\n", hr);
415 if (FAILED(hr)) {
416 IShellFolder_Release(psfDesktop);
417 return;
420 /* WinXP stores the filenames as both ANSI and UNICODE in the pidls */
421 pidlLast = pILFindLastID(pidlTestFile);
422 ok(pidlLast->mkid.cb >=76, "Expected pidl length of at least 76, got %d.\n", pidlLast->mkid.cb);
423 if (pidlLast->mkid.cb >= 76) {
424 ok(!lstrcmpW((WCHAR*)&pidlLast->mkid.abID[46], wszFileName),
425 "WinXP stores the filename as a wchar-string at this position!\n");
428 /* It seems as if we cannot bind to regular files on windows, but only directories.
430 hr = IShellFolder_BindToObject(psfDesktop, pidlTestFile, NULL, &IID_IUnknown, (VOID**)&psfFile);
431 todo_wine { ok (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), "hr = %08x\n", hr); }
432 if (SUCCEEDED(hr)) {
433 IShellFolder_Release(psfFile);
436 if(!pSHBindToParent)
438 skip("SHBindToParent is missing\n");
439 return;
442 /* Some tests for IShellFolder::SetNameOf */
443 hr = pSHBindToParent(pidlTestFile, &IID_IShellFolder, (VOID**)&psfPersonal, &pidlLast);
444 ok(SUCCEEDED(hr), "SHBindToParent failed! hr = %08x\n", hr);
445 if (SUCCEEDED(hr)) {
446 /* It's ok to use this fixed path. Call will fail anyway. */
447 WCHAR wszAbsoluteFilename[] = { 'C',':','\\','w','i','n','e','t','e','s','t', 0 };
448 LPITEMIDLIST pidlNew;
450 /* The pidl returned through the last parameter of SetNameOf is a simple one. */
451 hr = IShellFolder_SetNameOf(psfPersonal, NULL, pidlLast, wszDirName, SHGDN_NORMAL, &pidlNew);
452 ok (SUCCEEDED(hr), "SetNameOf failed! hr = %08x\n", hr);
453 if(hr == S_OK)
455 ok (((LPITEMIDLIST)((LPBYTE)pidlNew+pidlNew->mkid.cb))->mkid.cb == 0,
456 "pidl returned from SetNameOf should be simple!\n");
458 /* Passing an absolute path to SetNameOf fails. The HRESULT code indicates that SetNameOf
459 * is implemented on top of SHFileOperation in WinXP. */
460 hr = IShellFolder_SetNameOf(psfPersonal, NULL, pidlNew, wszAbsoluteFilename,
461 SHGDN_FORPARSING, NULL);
462 ok (hr == HRESULT_FROM_WIN32(ERROR_CANCELLED), "SetNameOf succeeded! hr = %08x\n", hr);
464 /* Rename the file back to its original name. SetNameOf ignores the fact, that the
465 * SHGDN flags specify an absolute path. */
466 hr = IShellFolder_SetNameOf(psfPersonal, NULL, pidlNew, wszFileName, SHGDN_FORPARSING, NULL);
467 ok (SUCCEEDED(hr), "SetNameOf failed! hr = %08x\n", hr);
469 pILFree(pidlNew);
472 IShellFolder_Release(psfPersonal);
475 /* Deleting the file and the directory */
476 DeleteFileA(szTestFile);
477 RemoveDirectoryA(szTestDir);
479 /* SHGetPathFromIDListW still works, although the file is not present anymore. */
480 if (pSHGetPathFromIDListW)
482 result = pSHGetPathFromIDListW(pidlTestFile, wszTestFile2);
483 ok (result, "SHGetPathFromIDListW failed! Last error: %u\n", GetLastError());
484 ok (!lstrcmpiW(wszTestFile, wszTestFile2), "SHGetPathFromIDListW returns incorrect path!\n");
487 /* SHBindToParent fails, if called with a NULL PIDL. */
488 hr = pSHBindToParent(NULL, &IID_IShellFolder, (VOID**)&psfPersonal, &pidlLast);
489 ok (FAILED(hr), "SHBindToParent(NULL) should fail!\n");
491 /* But it succeeds with an empty PIDL. */
492 hr = pSHBindToParent(pidlEmpty, &IID_IShellFolder, (VOID**)&psfPersonal, &pidlLast);
493 ok (SUCCEEDED(hr), "SHBindToParent(empty PIDL) should succeed! hr = %08x\n", hr);
494 ok (pidlLast == pidlEmpty, "The last element of an empty PIDL should be the PIDL itself!\n");
495 if (SUCCEEDED(hr))
496 IShellFolder_Release(psfPersonal);
498 /* Binding to the folder and querying the display name of the file also works. */
499 hr = pSHBindToParent(pidlTestFile, &IID_IShellFolder, (VOID**)&psfPersonal, &pidlLast);
500 ok (SUCCEEDED(hr), "SHBindToParent failed! hr = %08x\n", hr);
501 if (FAILED(hr)) {
502 IShellFolder_Release(psfDesktop);
503 return;
506 /* This test shows that Windows doesn't allocate a new pidlLast, but returns a pointer into
507 * pidlTestFile (In accordance with MSDN). */
508 ok (pILFindLastID(pidlTestFile) == pidlLast,
509 "SHBindToParent doesn't return the last id of the pidl param!\n");
511 hr = IShellFolder_GetDisplayNameOf(psfPersonal, pidlLast, SHGDN_FORPARSING, &strret);
512 ok (SUCCEEDED(hr), "Personal->GetDisplayNameOf failed! hr = %08x\n", hr);
513 if (FAILED(hr)) {
514 IShellFolder_Release(psfDesktop);
515 IShellFolder_Release(psfPersonal);
516 return;
519 if (pStrRetToBufW)
521 hr = pStrRetToBufW(&strret, pidlLast, wszTestFile2, MAX_PATH);
522 ok (SUCCEEDED(hr), "StrRetToBufW failed! hr = %08x\n", hr);
523 ok (!lstrcmpiW(wszTestFile, wszTestFile2), "GetDisplayNameOf returns incorrect path!\n");
526 IShellFolder_Release(psfDesktop);
527 IShellFolder_Release(psfPersonal);
530 static void test_CallForAttributes(void)
532 HKEY hKey;
533 LONG lResult;
534 HRESULT hr;
535 DWORD dwSize;
536 LPSHELLFOLDER psfDesktop;
537 LPITEMIDLIST pidlMyDocuments;
538 DWORD dwAttributes, dwCallForAttributes, dwOrigAttributes, dwOrigCallForAttributes;
539 static const WCHAR wszAttributes[] = { 'A','t','t','r','i','b','u','t','e','s',0 };
540 static const WCHAR wszCallForAttributes[] = {
541 'C','a','l','l','F','o','r','A','t','t','r','i','b','u','t','e','s',0 };
542 static const WCHAR wszMyDocumentsKey[] = {
543 'C','L','S','I','D','\\','{','4','5','0','D','8','F','B','A','-','A','D','2','5','-',
544 '1','1','D','0','-','9','8','A','8','-','0','8','0','0','3','6','1','B','1','1','0','3','}',
545 '\\','S','h','e','l','l','F','o','l','d','e','r',0 };
546 WCHAR wszMyDocuments[] = {
547 ':',':','{','4','5','0','D','8','F','B','A','-','A','D','2','5','-','1','1','D','0','-',
548 '9','8','A','8','-','0','8','0','0','3','6','1','B','1','1','0','3','}',0 };
550 /* For the root of a namespace extension, the attributes are not queried by binding
551 * to the object and calling GetAttributesOf. Instead, the attributes are read from
552 * the registry value HKCR/CLSID/{...}/ShellFolder/Attributes. This is documented on MSDN.
554 * The MyDocuments shellfolder on WinXP has a HKCR/CLSID/{...}/ShellFolder/CallForAttributes
555 * value. It seems that if the folder is queried for one of the flags set in CallForAttributes,
556 * the shell does bind to the folder object and calls GetAttributesOf. This is not documented
557 * on MSDN. This test is meant to document the observed behaviour on WinXP SP2.
559 hr = SHGetDesktopFolder(&psfDesktop);
560 ok (SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08x\n", hr);
561 if (FAILED(hr)) return;
563 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszMyDocuments, NULL,
564 &pidlMyDocuments, NULL);
565 ok (SUCCEEDED(hr),
566 "Desktop's ParseDisplayName failed to parse MyDocuments's CLSID! hr = %08x\n", hr);
567 if (FAILED(hr)) {
568 IShellFolder_Release(psfDesktop);
569 return;
572 dwAttributes = 0xffffffff;
573 hr = IShellFolder_GetAttributesOf(psfDesktop, 1,
574 (LPCITEMIDLIST*)&pidlMyDocuments, &dwAttributes);
575 ok (SUCCEEDED(hr), "Desktop->GetAttributesOf(MyDocuments) failed! hr = %08x\n", hr);
577 /* We need the following setup (as observed on WinXP SP2), for the tests to make sense. */
578 ok (dwAttributes & SFGAO_FILESYSTEM, "SFGAO_FILESYSTEM attribute is not set for MyDocuments!\n");
579 ok (!(dwAttributes & SFGAO_ISSLOW), "SFGAO_ISSLOW attribute is set for MyDocuments!\n");
580 ok (!(dwAttributes & SFGAO_GHOSTED), "SFGAO_GHOSTED attribute is set for MyDocuments!\n");
582 /* We don't have the MyDocuments shellfolder in wine yet, and thus we don't have the registry
583 * key. So the test will return at this point, if run on wine.
585 lResult = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszMyDocumentsKey, 0, KEY_WRITE|KEY_READ, &hKey);
586 ok (lResult == ERROR_SUCCESS, "RegOpenKeyEx failed! result: %08x\n", lResult);
587 if (lResult != ERROR_SUCCESS) {
588 IMalloc_Free(ppM, pidlMyDocuments);
589 IShellFolder_Release(psfDesktop);
590 return;
593 /* Query MyDocuments' Attributes value, to be able to restore it later. */
594 dwSize = sizeof(DWORD);
595 lResult = RegQueryValueExW(hKey, wszAttributes, NULL, NULL, (LPBYTE)&dwOrigAttributes, &dwSize);
596 ok (lResult == ERROR_SUCCESS, "RegQueryValueEx failed! result: %08x\n", lResult);
597 if (lResult != ERROR_SUCCESS) {
598 RegCloseKey(hKey);
599 IMalloc_Free(ppM, pidlMyDocuments);
600 IShellFolder_Release(psfDesktop);
601 return;
604 /* Query MyDocuments' CallForAttributes value, to be able to restore it later. */
605 dwSize = sizeof(DWORD);
606 lResult = RegQueryValueExW(hKey, wszCallForAttributes, NULL, NULL,
607 (LPBYTE)&dwOrigCallForAttributes, &dwSize);
608 ok (lResult == ERROR_SUCCESS, "RegQueryValueEx failed! result: %08x\n", lResult);
609 if (lResult != ERROR_SUCCESS) {
610 RegCloseKey(hKey);
611 IMalloc_Free(ppM, pidlMyDocuments);
612 IShellFolder_Release(psfDesktop);
613 return;
616 /* Define via the Attributes value that MyDocuments attributes are SFGAO_ISSLOW and
617 * SFGAO_GHOSTED and that MyDocuments should be called for the SFGAO_ISSLOW and
618 * SFGAO_FILESYSTEM attributes. */
619 dwAttributes = SFGAO_ISSLOW|SFGAO_GHOSTED;
620 RegSetValueExW(hKey, wszAttributes, 0, REG_DWORD, (LPBYTE)&dwAttributes, sizeof(DWORD));
621 dwCallForAttributes = SFGAO_ISSLOW|SFGAO_FILESYSTEM;
622 RegSetValueExW(hKey, wszCallForAttributes, 0, REG_DWORD,
623 (LPBYTE)&dwCallForAttributes, sizeof(DWORD));
625 /* Although it is not set in CallForAttributes, the SFGAO_GHOSTED flag is reset by
626 * GetAttributesOf. It seems that once there is a single attribute queried, for which
627 * CallForAttributes is set, all flags are taken from the GetAttributesOf call and
628 * the flags in Attributes are ignored.
630 dwAttributes = SFGAO_ISSLOW|SFGAO_GHOSTED|SFGAO_FILESYSTEM;
631 hr = IShellFolder_GetAttributesOf(psfDesktop, 1,
632 (LPCITEMIDLIST*)&pidlMyDocuments, &dwAttributes);
633 ok (SUCCEEDED(hr), "Desktop->GetAttributesOf(MyDocuments) failed! hr = %08x\n", hr);
634 if (SUCCEEDED(hr))
635 ok (dwAttributes == SFGAO_FILESYSTEM,
636 "Desktop->GetAttributes(MyDocuments) returned unexpected attributes: %08x\n",
637 dwAttributes);
639 /* Restore MyDocuments' original Attributes and CallForAttributes registry values */
640 RegSetValueExW(hKey, wszAttributes, 0, REG_DWORD, (LPBYTE)&dwOrigAttributes, sizeof(DWORD));
641 RegSetValueExW(hKey, wszCallForAttributes, 0, REG_DWORD,
642 (LPBYTE)&dwOrigCallForAttributes, sizeof(DWORD));
643 RegCloseKey(hKey);
644 IMalloc_Free(ppM, pidlMyDocuments);
645 IShellFolder_Release(psfDesktop);
648 static void test_GetAttributesOf(void)
650 HRESULT hr;
651 LPSHELLFOLDER psfDesktop, psfMyComputer;
652 SHITEMID emptyitem = { 0, { 0 } };
653 LPCITEMIDLIST pidlEmpty = (LPCITEMIDLIST)&emptyitem;
654 LPITEMIDLIST pidlMyComputer;
655 DWORD dwFlags;
656 static const DWORD dwDesktopFlags = /* As observed on WinXP SP2 */
657 SFGAO_STORAGE | SFGAO_HASPROPSHEET | SFGAO_STORAGEANCESTOR |
658 SFGAO_FILESYSANCESTOR | SFGAO_FOLDER | SFGAO_FILESYSTEM | SFGAO_HASSUBFOLDER;
659 static const DWORD dwMyComputerFlags = /* As observed on WinXP SP2 */
660 SFGAO_CANRENAME | SFGAO_CANDELETE | SFGAO_HASPROPSHEET |
661 SFGAO_DROPTARGET | SFGAO_FILESYSANCESTOR | SFGAO_FOLDER | SFGAO_HASSUBFOLDER;
662 WCHAR wszMyComputer[] = {
663 ':',':','{','2','0','D','0','4','F','E','0','-','3','A','E','A','-','1','0','6','9','-',
664 'A','2','D','8','-','0','8','0','0','2','B','3','0','3','0','9','D','}',0 };
665 char cCurrDirA [MAX_PATH] = {0};
666 WCHAR cCurrDirW [MAX_PATH];
667 static WCHAR cTestDirW[] = {'t','e','s','t','d','i','r',0};
668 static const WCHAR cBackSlash[] = {'\\',0};
669 IShellFolder *IDesktopFolder, *testIShellFolder;
670 ITEMIDLIST *newPIDL;
671 int len;
673 hr = SHGetDesktopFolder(&psfDesktop);
674 ok (SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08x\n", hr);
675 if (FAILED(hr)) return;
677 /* The Desktop attributes can be queried with a single empty itemidlist, .. */
678 dwFlags = 0xffffffff;
679 hr = IShellFolder_GetAttributesOf(psfDesktop, 1, &pidlEmpty, &dwFlags);
680 ok (SUCCEEDED(hr), "Desktop->GetAttributesOf(empty pidl) failed! hr = %08x\n", hr);
681 ok (dwFlags == dwDesktopFlags, "Wrong Desktop attributes: %08x, expected: %08x\n",
682 dwFlags, dwDesktopFlags);
684 /* .. or with no itemidlist at all. */
685 dwFlags = 0xffffffff;
686 hr = IShellFolder_GetAttributesOf(psfDesktop, 0, NULL, &dwFlags);
687 ok (SUCCEEDED(hr), "Desktop->GetAttributesOf(NULL) failed! hr = %08x\n", hr);
688 ok (dwFlags == dwDesktopFlags, "Wrong Desktop attributes: %08x, expected: %08x\n",
689 dwFlags, dwDesktopFlags);
691 /* Testing the attributes of the MyComputer shellfolder */
692 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszMyComputer, NULL, &pidlMyComputer, NULL);
693 ok (SUCCEEDED(hr), "Desktop's ParseDisplayName failed to parse MyComputer's CLSID! hr = %08x\n", hr);
694 if (FAILED(hr)) {
695 IShellFolder_Release(psfDesktop);
696 return;
699 /* WinXP SP2 sets the SFGAO_CANLINK flag, when MyComputer is queried via the Desktop
700 * folder object. It doesn't do this, if MyComputer is queried directly (see below).
701 * SFGAO_CANLINK is the same as DROPEFFECT_LINK, which MSDN says means: "Drag source
702 * should create a link to the original data". You can't create links on MyComputer on
703 * Windows, so this flag shouldn't be set. Seems like a bug in Windows. As long as nobody
704 * depends on this bug, we probably shouldn't imitate it.
706 dwFlags = 0xffffffff;
707 hr = IShellFolder_GetAttributesOf(psfDesktop, 1, (LPCITEMIDLIST*)&pidlMyComputer, &dwFlags);
708 ok (SUCCEEDED(hr), "Desktop->GetAttributesOf(MyComputer) failed! hr = %08x\n", hr);
709 ok ((dwFlags & ~(DWORD)SFGAO_CANLINK) == dwMyComputerFlags,
710 "Wrong MyComputer attributes: %08x, expected: %08x\n", dwFlags, dwMyComputerFlags);
712 hr = IShellFolder_BindToObject(psfDesktop, pidlMyComputer, NULL, &IID_IShellFolder, (LPVOID*)&psfMyComputer);
713 ok (SUCCEEDED(hr), "Desktop failed to bind to MyComputer object! hr = %08x\n", hr);
714 IShellFolder_Release(psfDesktop);
715 IMalloc_Free(ppM, pidlMyComputer);
716 if (FAILED(hr)) return;
718 hr = IShellFolder_GetAttributesOf(psfMyComputer, 1, &pidlEmpty, &dwFlags);
719 todo_wine {ok (hr == E_INVALIDARG, "MyComputer->GetAttributesOf(emtpy pidl) should fail! hr = %08x\n", hr); }
721 dwFlags = 0xffffffff;
722 hr = IShellFolder_GetAttributesOf(psfMyComputer, 0, NULL, &dwFlags);
723 ok (SUCCEEDED(hr), "MyComputer->GetAttributesOf(NULL) failed! hr = %08x\n", hr);
724 todo_wine { ok (dwFlags == dwMyComputerFlags,
725 "Wrong MyComputer attributes: %08x, expected: %08x\n", dwFlags, dwMyComputerFlags); }
727 IShellFolder_Release(psfMyComputer);
729 /* create test directory */
730 CreateFilesFolders();
732 GetCurrentDirectoryA(MAX_PATH, cCurrDirA);
733 len = lstrlenA(cCurrDirA);
735 if (len == 0) {
736 trace("GetCurrentDirectoryA returned empty string. Skipping test_EnumObjects_and_CompareIDs\n");
737 return;
739 if(cCurrDirA[len-1] == '\\')
740 cCurrDirA[len-1] = 0;
742 MultiByteToWideChar(CP_ACP, 0, cCurrDirA, -1, cCurrDirW, MAX_PATH);
744 hr = SHGetDesktopFolder(&IDesktopFolder);
745 ok(hr == S_OK, "SHGetDesktopfolder failed %08x\n", hr);
747 hr = IShellFolder_ParseDisplayName(IDesktopFolder, NULL, NULL, cCurrDirW, NULL, &newPIDL, 0);
748 ok(hr == S_OK, "ParseDisplayName failed %08x\n", hr);
750 hr = IShellFolder_BindToObject(IDesktopFolder, newPIDL, NULL, (REFIID)&IID_IShellFolder, (LPVOID *)&testIShellFolder);
751 ok(hr == S_OK, "BindToObject failed %08x\n", hr);
753 IMalloc_Free(ppM, newPIDL);
755 /* get relative PIDL */
756 hr = IShellFolder_ParseDisplayName(testIShellFolder, NULL, NULL, cTestDirW, NULL, &newPIDL, 0);
757 ok(hr == S_OK, "ParseDisplayName failed %08x\n", hr);
759 /* test the shell attributes of the test directory using the relative PIDL */
760 dwFlags = SFGAO_FOLDER;
761 hr = IShellFolder_GetAttributesOf(testIShellFolder, 1, (LPCITEMIDLIST*)&newPIDL, &dwFlags);
762 ok (SUCCEEDED(hr), "Desktop->GetAttributesOf() failed! hr = %08x\n", hr);
763 ok ((dwFlags&SFGAO_FOLDER), "Wrong directory attribute for relative PIDL: %08x\n", dwFlags);
765 /* free memory */
766 IMalloc_Free(ppM, newPIDL);
768 /* append testdirectory name to path */
769 lstrcatW(cCurrDirW, cBackSlash);
770 lstrcatW(cCurrDirW, cTestDirW);
772 hr = IShellFolder_ParseDisplayName(IDesktopFolder, NULL, NULL, cCurrDirW, NULL, &newPIDL, 0);
773 ok(hr == S_OK, "ParseDisplayName failed %08x\n", hr);
775 /* test the shell attributes of the test directory using the absolute PIDL */
776 dwFlags = SFGAO_FOLDER;
777 hr = IShellFolder_GetAttributesOf(IDesktopFolder, 1, (LPCITEMIDLIST*)&newPIDL, &dwFlags);
778 ok (SUCCEEDED(hr), "Desktop->GetAttributesOf() failed! hr = %08x\n", hr);
779 ok ((dwFlags&SFGAO_FOLDER), "Wrong directory attribute for absolute PIDL: %08x\n", dwFlags);
781 /* free memory */
782 IMalloc_Free(ppM, newPIDL);
784 IShellFolder_Release(testIShellFolder);
786 Cleanup();
788 IShellFolder_Release(IDesktopFolder);
791 static void test_SHGetPathFromIDList(void)
793 SHITEMID emptyitem = { 0, { 0 } };
794 LPCITEMIDLIST pidlEmpty = (LPCITEMIDLIST)&emptyitem;
795 LPITEMIDLIST pidlMyComputer;
796 WCHAR wszPath[MAX_PATH], wszDesktop[MAX_PATH];
797 BOOL result;
798 HRESULT hr;
799 LPSHELLFOLDER psfDesktop;
800 WCHAR wszMyComputer[] = {
801 ':',':','{','2','0','D','0','4','F','E','0','-','3','A','E','A','-','1','0','6','9','-',
802 'A','2','D','8','-','0','8','0','0','2','B','3','0','3','0','9','D','}',0 };
803 WCHAR wszFileName[MAX_PATH];
804 LPITEMIDLIST pidlTestFile;
805 HANDLE hTestFile;
806 STRRET strret;
807 static WCHAR wszTestFile[] = {
808 'w','i','n','e','t','e','s','t','.','f','o','o',0 };
809 HRESULT (WINAPI *pSHGetSpecialFolderLocation)(HWND, int, LPITEMIDLIST *);
810 HMODULE hShell32;
811 LPITEMIDLIST pidlPrograms;
813 if(!pSHGetPathFromIDListW || !pSHGetSpecialFolderPathW)
815 skip("SHGetPathFromIDListW() or SHGetSpecialFolderPathW() is missing\n");
816 return;
819 /* Calling SHGetPathFromIDListW with no pidl should return the empty string */
820 wszPath[0] = 'a';
821 wszPath[1] = '\0';
822 result = pSHGetPathFromIDListW(NULL, wszPath);
823 ok(!result, "Expected failure\n");
824 ok(!wszPath[0], "Expected empty string\n");
826 /* Calling SHGetPathFromIDListW with an empty pidl should return the desktop folder's path. */
827 result = pSHGetSpecialFolderPathW(NULL, wszDesktop, CSIDL_DESKTOP, FALSE);
828 ok(result, "SHGetSpecialFolderPathW(CSIDL_DESKTOP) failed! Last error: %u\n", GetLastError());
829 if (!result) return;
831 result = pSHGetPathFromIDListW(pidlEmpty, wszPath);
832 ok(result, "SHGetPathFromIDListW failed! Last error: %u\n", GetLastError());
833 if (!result) return;
834 ok(!lstrcmpiW(wszDesktop, wszPath), "SHGetPathFromIDListW didn't return desktop path for empty pidl!\n");
836 /* MyComputer does not map to a filesystem path. SHGetPathFromIDListW should fail. */
837 hr = SHGetDesktopFolder(&psfDesktop);
838 ok (SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08x\n", hr);
839 if (FAILED(hr)) return;
841 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszMyComputer, NULL, &pidlMyComputer, NULL);
842 ok (SUCCEEDED(hr), "Desktop's ParseDisplayName failed to parse MyComputer's CLSID! hr = %08x\n", hr);
843 if (FAILED(hr)) {
844 IShellFolder_Release(psfDesktop);
845 return;
848 SetLastError(0xdeadbeef);
849 wszPath[0] = 'a';
850 wszPath[1] = '\0';
851 result = pSHGetPathFromIDListW(pidlMyComputer, wszPath);
852 ok (!result, "SHGetPathFromIDListW succeeded where it shouldn't!\n");
853 ok (GetLastError()==0xdeadbeef, "SHGetPathFromIDListW shouldn't set last error! Last error: %u\n", GetLastError());
854 ok (!wszPath[0], "Expected empty path\n");
855 if (result) {
856 IShellFolder_Release(psfDesktop);
857 return;
860 IMalloc_Free(ppM, pidlMyComputer);
862 result = pSHGetSpecialFolderPathW(NULL, wszFileName, CSIDL_DESKTOPDIRECTORY, FALSE);
863 ok(result, "SHGetSpecialFolderPathW failed! Last error: %u\n", GetLastError());
864 if (!result) {
865 IShellFolder_Release(psfDesktop);
866 return;
868 myPathAddBackslashW(wszFileName);
869 lstrcatW(wszFileName, wszTestFile);
870 hTestFile = CreateFileW(wszFileName, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, NULL);
871 ok(hTestFile != INVALID_HANDLE_VALUE, "CreateFileW failed! Last error: %u\n", GetLastError());
872 if (hTestFile == INVALID_HANDLE_VALUE) {
873 IShellFolder_Release(psfDesktop);
874 return;
876 CloseHandle(hTestFile);
878 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszTestFile, NULL, &pidlTestFile, NULL);
879 ok (SUCCEEDED(hr), "Desktop's ParseDisplayName failed to parse filename hr = %08x\n", hr);
880 if (FAILED(hr)) {
881 IShellFolder_Release(psfDesktop);
882 DeleteFileW(wszFileName);
883 IMalloc_Free(ppM, pidlTestFile);
884 return;
887 /* This test is to show that the Desktop shellfolder prepends the CSIDL_DESKTOPDIRECTORY
888 * path for files placed on the desktop, if called with SHGDN_FORPARSING. */
889 hr = IShellFolder_GetDisplayNameOf(psfDesktop, pidlTestFile, SHGDN_FORPARSING, &strret);
890 ok (SUCCEEDED(hr), "Desktop's GetDisplayNamfOf failed! hr = %08x\n", hr);
891 IShellFolder_Release(psfDesktop);
892 DeleteFileW(wszFileName);
893 if (FAILED(hr)) {
894 IMalloc_Free(ppM, pidlTestFile);
895 return;
897 if (pStrRetToBufW)
899 pStrRetToBufW(&strret, pidlTestFile, wszPath, MAX_PATH);
900 ok(0 == lstrcmpW(wszFileName, wszPath),
901 "Desktop->GetDisplayNameOf(pidlTestFile, SHGDN_FORPARSING) "
902 "returned incorrect path for file placed on desktop\n");
905 result = pSHGetPathFromIDListW(pidlTestFile, wszPath);
906 ok(result, "SHGetPathFromIDListW failed! Last error: %u\n", GetLastError());
907 IMalloc_Free(ppM, pidlTestFile);
908 if (!result) return;
909 ok(0 == lstrcmpW(wszFileName, wszPath), "SHGetPathFromIDListW returned incorrect path for file placed on desktop\n");
912 /* Test if we can get the path from the start menu "program files" PIDL. */
913 hShell32 = GetModuleHandleA("shell32");
914 pSHGetSpecialFolderLocation = (void *)GetProcAddress(hShell32, "SHGetSpecialFolderLocation");
916 hr = pSHGetSpecialFolderLocation(NULL, CSIDL_PROGRAM_FILES, &pidlPrograms);
917 ok(SUCCEEDED(hr), "SHGetFolderLocation failed: 0x%08x\n", hr);
919 SetLastError(0xdeadbeef);
920 result = pSHGetPathFromIDListW(pidlPrograms, wszPath);
921 IMalloc_Free(ppM, pidlPrograms);
922 ok(result, "SHGetPathFromIDListW failed\n");
925 static void test_EnumObjects_and_CompareIDs(void)
927 ITEMIDLIST *newPIDL;
928 IShellFolder *IDesktopFolder, *testIShellFolder;
929 char cCurrDirA [MAX_PATH] = {0};
930 WCHAR cCurrDirW [MAX_PATH];
931 static const WCHAR cTestDirW[] = {'\\','t','e','s','t','d','i','r',0};
932 int len;
933 HRESULT hr;
935 GetCurrentDirectoryA(MAX_PATH, cCurrDirA);
936 len = lstrlenA(cCurrDirA);
938 if(len == 0) {
939 trace("GetCurrentDirectoryA returned empty string. Skipping test_EnumObjects_and_CompareIDs\n");
940 return;
942 if(cCurrDirA[len-1] == '\\')
943 cCurrDirA[len-1] = 0;
945 MultiByteToWideChar(CP_ACP, 0, cCurrDirA, -1, cCurrDirW, MAX_PATH);
946 lstrcatW(cCurrDirW, cTestDirW);
948 hr = SHGetDesktopFolder(&IDesktopFolder);
949 ok(hr == S_OK, "SHGetDesktopfolder failed %08x\n", hr);
951 CreateFilesFolders();
953 hr = IShellFolder_ParseDisplayName(IDesktopFolder, NULL, NULL, cCurrDirW, NULL, &newPIDL, 0);
954 ok(hr == S_OK, "ParseDisplayName failed %08x\n", hr);
956 hr = IShellFolder_BindToObject(IDesktopFolder, newPIDL, NULL, (REFIID)&IID_IShellFolder, (LPVOID *)&testIShellFolder);
957 ok(hr == S_OK, "BindToObject failed %08x\n", hr);
959 test_EnumObjects(testIShellFolder);
961 IShellFolder_Release(testIShellFolder);
963 Cleanup();
965 IMalloc_Free(ppM, newPIDL);
967 IShellFolder_Release(IDesktopFolder);
970 /* A simple implementation of an IPropertyBag, which returns fixed values for
971 * 'Target' and 'Attributes' properties.
973 static HRESULT WINAPI InitPropertyBag_IPropertyBag_QueryInterface(IPropertyBag *iface, REFIID riid,
974 void **ppvObject)
976 if (!ppvObject)
977 return E_INVALIDARG;
979 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IPropertyBag, riid)) {
980 *ppvObject = iface;
981 } else {
982 ok (FALSE, "InitPropertyBag asked for unknown interface!\n");
983 return E_NOINTERFACE;
986 IPropertyBag_AddRef(iface);
987 return S_OK;
990 static ULONG WINAPI InitPropertyBag_IPropertyBag_AddRef(IPropertyBag *iface) {
991 return 2;
994 static ULONG WINAPI InitPropertyBag_IPropertyBag_Release(IPropertyBag *iface) {
995 return 1;
998 static HRESULT WINAPI InitPropertyBag_IPropertyBag_Read(IPropertyBag *iface, LPCOLESTR pszPropName,
999 VARIANT *pVar, IErrorLog *pErrorLog)
1001 static const WCHAR wszTargetSpecialFolder[] = {
1002 'T','a','r','g','e','t','S','p','e','c','i','a','l','F','o','l','d','e','r',0 };
1003 static const WCHAR wszTarget[] = {
1004 'T','a','r','g','e','t',0 };
1005 static const WCHAR wszAttributes[] = {
1006 'A','t','t','r','i','b','u','t','e','s',0 };
1007 static const WCHAR wszResolveLinkFlags[] = {
1008 'R','e','s','o','l','v','e','L','i','n','k','F','l','a','g','s',0 };
1010 if (!lstrcmpW(pszPropName, wszTargetSpecialFolder)) {
1011 ok(V_VT(pVar) == VT_I4, "Wrong variant type for 'TargetSpecialFolder' property!\n");
1012 return E_INVALIDARG;
1015 if (!lstrcmpW(pszPropName, wszResolveLinkFlags))
1017 ok(V_VT(pVar) == VT_UI4, "Wrong variant type for 'ResolveLinkFlags' property!\n");
1018 return E_INVALIDARG;
1021 if (!lstrcmpW(pszPropName, wszTarget)) {
1022 WCHAR wszPath[MAX_PATH];
1023 BOOL result;
1025 ok(V_VT(pVar) == VT_BSTR, "Wrong variant type for 'Target' property!\n");
1026 if (V_VT(pVar) != VT_BSTR) return E_INVALIDARG;
1028 result = pSHGetSpecialFolderPathW(NULL, wszPath, CSIDL_DESKTOPDIRECTORY, FALSE);
1029 ok(result, "SHGetSpecialFolderPathW(DESKTOPDIRECTORY) failed! %u\n", GetLastError());
1030 if (!result) return E_INVALIDARG;
1032 V_BSTR(pVar) = SysAllocString(wszPath);
1033 return S_OK;
1036 if (!lstrcmpW(pszPropName, wszAttributes)) {
1037 ok(V_VT(pVar) == VT_UI4, "Wrong variant type for 'Attributes' property!\n");
1038 if (V_VT(pVar) != VT_UI4) return E_INVALIDARG;
1039 V_UI4(pVar) = SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR|
1040 SFGAO_CANRENAME|SFGAO_FILESYSTEM;
1041 return S_OK;
1044 ok(FALSE, "PropertyBag was asked for unknown property (vt=%d)!\n", V_VT(pVar));
1045 return E_INVALIDARG;
1048 static HRESULT WINAPI InitPropertyBag_IPropertyBag_Write(IPropertyBag *iface, LPCOLESTR pszPropName,
1049 VARIANT *pVar)
1051 ok(FALSE, "Unexpected call to IPropertyBag_Write\n");
1052 return E_NOTIMPL;
1055 static const IPropertyBagVtbl InitPropertyBag_IPropertyBagVtbl = {
1056 InitPropertyBag_IPropertyBag_QueryInterface,
1057 InitPropertyBag_IPropertyBag_AddRef,
1058 InitPropertyBag_IPropertyBag_Release,
1059 InitPropertyBag_IPropertyBag_Read,
1060 InitPropertyBag_IPropertyBag_Write
1063 static struct IPropertyBag InitPropertyBag = {
1064 &InitPropertyBag_IPropertyBagVtbl
1067 static void test_FolderShortcut(void) {
1068 IPersistPropertyBag *pPersistPropertyBag;
1069 IShellFolder *pShellFolder, *pDesktopFolder;
1070 IPersistFolder3 *pPersistFolder3;
1071 HRESULT hr;
1072 STRRET strret;
1073 WCHAR wszDesktopPath[MAX_PATH], wszBuffer[MAX_PATH];
1074 BOOL result;
1075 CLSID clsid;
1076 LPITEMIDLIST pidlCurrentFolder, pidlWineTestFolder, pidlSubFolder;
1077 HKEY hShellExtKey;
1078 WCHAR wszWineTestFolder[] = {
1079 ':',':','{','9','B','3','5','2','E','B','F','-','2','7','6','5','-','4','5','C','1','-',
1080 'B','4','C','6','-','8','5','C','C','7','F','7','A','B','C','6','4','}',0 };
1081 WCHAR wszShellExtKey[] = { 'S','o','f','t','w','a','r','e','\\',
1082 'M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\',
1083 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
1084 'E','x','p','l','o','r','e','r','\\','D','e','s','k','t','o','p','\\',
1085 'N','a','m','e','S','p','a','c','e','\\',
1086 '{','9','b','3','5','2','e','b','f','-','2','7','6','5','-','4','5','c','1','-',
1087 'b','4','c','6','-','8','5','c','c','7','f','7','a','b','c','6','4','}',0 };
1089 WCHAR wszSomeSubFolder[] = { 'S','u','b','F','o','l','d','e','r', 0};
1090 static const GUID CLSID_UnixDosFolder =
1091 {0x9d20aae8, 0x0625, 0x44b0, {0x9c, 0xa7, 0x71, 0x88, 0x9c, 0x22, 0x54, 0xd9}};
1093 if (!pSHGetSpecialFolderPathW || !pStrRetToBufW) return;
1095 /* These tests basically show, that CLSID_FolderShortcuts are initialized
1096 * via their IPersistPropertyBag interface. And that the target folder
1097 * is taken from the IPropertyBag's 'Target' property.
1099 hr = CoCreateInstance(&CLSID_FolderShortcut, NULL, CLSCTX_INPROC_SERVER,
1100 &IID_IPersistPropertyBag, (LPVOID*)&pPersistPropertyBag);
1101 ok (SUCCEEDED(hr), "CoCreateInstance failed! hr = 0x%08x\n", hr);
1102 if (FAILED(hr)) return;
1104 hr = IPersistPropertyBag_Load(pPersistPropertyBag, &InitPropertyBag, NULL);
1105 ok(SUCCEEDED(hr), "IPersistPropertyBag_Load failed! hr = %08x\n", hr);
1106 if (FAILED(hr)) {
1107 IPersistPropertyBag_Release(pPersistPropertyBag);
1108 return;
1111 hr = IPersistPropertyBag_QueryInterface(pPersistPropertyBag, &IID_IShellFolder,
1112 (LPVOID*)&pShellFolder);
1113 IPersistPropertyBag_Release(pPersistPropertyBag);
1114 ok(SUCCEEDED(hr), "IPersistPropertyBag_QueryInterface(IShellFolder) failed! hr = %08x\n", hr);
1115 if (FAILED(hr)) return;
1117 hr = IShellFolder_GetDisplayNameOf(pShellFolder, NULL, SHGDN_FORPARSING, &strret);
1118 ok(SUCCEEDED(hr), "IShellFolder_GetDisplayNameOf(NULL) failed! hr = %08x\n", hr);
1119 if (FAILED(hr)) {
1120 IShellFolder_Release(pShellFolder);
1121 return;
1124 result = pSHGetSpecialFolderPathW(NULL, wszDesktopPath, CSIDL_DESKTOPDIRECTORY, FALSE);
1125 ok(result, "SHGetSpecialFolderPathW(CSIDL_DESKTOPDIRECTORY) failed! %u\n", GetLastError());
1126 if (!result) return;
1128 pStrRetToBufW(&strret, NULL, wszBuffer, MAX_PATH);
1129 ok(!lstrcmpiW(wszDesktopPath, wszBuffer), "FolderShortcut returned incorrect folder!\n");
1131 hr = IShellFolder_QueryInterface(pShellFolder, &IID_IPersistFolder3, (LPVOID*)&pPersistFolder3);
1132 IShellFolder_Release(pShellFolder);
1133 ok(SUCCEEDED(hr), "IShellFolder_QueryInterface(IID_IPersistFolder3 failed! hr = 0x%08x\n", hr);
1134 if (FAILED(hr)) return;
1136 hr = IPersistFolder3_GetClassID(pPersistFolder3, &clsid);
1137 ok(SUCCEEDED(hr), "IPersistFolder3_GetClassID failed! hr=0x%08x\n", hr);
1138 ok(IsEqualCLSID(&clsid, &CLSID_FolderShortcut), "Unexpected CLSID!\n");
1140 hr = IPersistFolder3_GetCurFolder(pPersistFolder3, &pidlCurrentFolder);
1141 ok(SUCCEEDED(hr), "IPersistFolder3_GetCurFolder failed! hr=0x%08x\n", hr);
1142 ok(!pidlCurrentFolder, "IPersistFolder3_GetCurFolder should return a NULL pidl!\n");
1144 /* For FolderShortcut objects, the Initialize method initialized the folder's position in the
1145 * shell namespace. The target folder, read from the property bag above, remains untouched.
1146 * The following tests show this: The itemidlist for some imaginary shellfolder object
1147 * is created and the FolderShortcut is initialized with it. GetCurFolder now returns this
1148 * itemidlist, but GetDisplayNameOf still returns the path from above.
1150 hr = SHGetDesktopFolder(&pDesktopFolder);
1151 ok (SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08x\n", hr);
1152 if (FAILED(hr)) return;
1154 /* Temporarily register WineTestFolder as a shell namespace extension at the Desktop.
1155 * Otherwise ParseDisplayName fails on WinXP with E_INVALIDARG */
1156 RegCreateKeyW(HKEY_CURRENT_USER, wszShellExtKey, &hShellExtKey);
1157 RegCloseKey(hShellExtKey);
1158 hr = IShellFolder_ParseDisplayName(pDesktopFolder, NULL, NULL, wszWineTestFolder, NULL,
1159 &pidlWineTestFolder, NULL);
1160 RegDeleteKeyW(HKEY_CURRENT_USER, wszShellExtKey);
1161 IShellFolder_Release(pDesktopFolder);
1162 ok (SUCCEEDED(hr), "IShellFolder::ParseDisplayName failed! hr = %08x\n", hr);
1163 if (FAILED(hr)) return;
1165 hr = IPersistFolder3_Initialize(pPersistFolder3, pidlWineTestFolder);
1166 ok (SUCCEEDED(hr), "IPersistFolder3::Initialize failed! hr = %08x\n", hr);
1167 if (FAILED(hr)) {
1168 IPersistFolder3_Release(pPersistFolder3);
1169 pILFree(pidlWineTestFolder);
1170 return;
1173 hr = IPersistFolder3_GetCurFolder(pPersistFolder3, &pidlCurrentFolder);
1174 ok(SUCCEEDED(hr), "IPersistFolder3_GetCurFolder failed! hr=0x%08x\n", hr);
1175 ok(pILIsEqual(pidlCurrentFolder, pidlWineTestFolder),
1176 "IPersistFolder3_GetCurFolder should return pidlWineTestFolder!\n");
1177 pILFree(pidlCurrentFolder);
1178 pILFree(pidlWineTestFolder);
1180 hr = IPersistFolder3_QueryInterface(pPersistFolder3, &IID_IShellFolder, (LPVOID*)&pShellFolder);
1181 IPersistFolder3_Release(pPersistFolder3);
1182 ok(SUCCEEDED(hr), "IPersistFolder3_QueryInterface(IShellFolder) failed! hr = %08x\n", hr);
1183 if (FAILED(hr)) return;
1185 hr = IShellFolder_GetDisplayNameOf(pShellFolder, NULL, SHGDN_FORPARSING, &strret);
1186 ok(SUCCEEDED(hr), "IShellFolder_GetDisplayNameOf(NULL) failed! hr = %08x\n", hr);
1187 if (FAILED(hr)) {
1188 IShellFolder_Release(pShellFolder);
1189 return;
1192 pStrRetToBufW(&strret, NULL, wszBuffer, MAX_PATH);
1193 ok(!lstrcmpiW(wszDesktopPath, wszBuffer), "FolderShortcut returned incorrect folder!\n");
1195 /* Next few lines are meant to show that children of FolderShortcuts are not FolderShortcuts,
1196 * but ShellFSFolders. */
1197 myPathAddBackslashW(wszDesktopPath);
1198 lstrcatW(wszDesktopPath, wszSomeSubFolder);
1199 if (!CreateDirectoryW(wszDesktopPath, NULL)) {
1200 IShellFolder_Release(pShellFolder);
1201 return;
1204 hr = IShellFolder_ParseDisplayName(pShellFolder, NULL, NULL, wszSomeSubFolder, NULL,
1205 &pidlSubFolder, NULL);
1206 RemoveDirectoryW(wszDesktopPath);
1207 ok (SUCCEEDED(hr), "IShellFolder::ParseDisplayName failed! hr = %08x\n", hr);
1208 if (FAILED(hr)) {
1209 IShellFolder_Release(pShellFolder);
1210 return;
1213 hr = IShellFolder_BindToObject(pShellFolder, pidlSubFolder, NULL, &IID_IPersistFolder3,
1214 (LPVOID*)&pPersistFolder3);
1215 IShellFolder_Release(pShellFolder);
1216 pILFree(pidlSubFolder);
1217 ok (SUCCEEDED(hr), "IShellFolder::BindToObject failed! hr = %08x\n", hr);
1218 if (FAILED(hr))
1219 return;
1221 /* On windows, we expect CLSID_ShellFSFolder. On wine we relax this constraint
1222 * a little bit and also allow CLSID_UnixDosFolder. */
1223 hr = IPersistFolder3_GetClassID(pPersistFolder3, &clsid);
1224 ok(SUCCEEDED(hr), "IPersistFolder3_GetClassID failed! hr=0x%08x\n", hr);
1225 ok(IsEqualCLSID(&clsid, &CLSID_ShellFSFolder) || IsEqualCLSID(&clsid, &CLSID_UnixDosFolder),
1226 "IPersistFolder3::GetClassID returned unexpected CLSID!\n");
1228 IPersistFolder3_Release(pPersistFolder3);
1231 #include "pshpack1.h"
1232 struct FileStructA {
1233 BYTE type;
1234 BYTE dummy;
1235 DWORD dwFileSize;
1236 WORD uFileDate; /* In our current implementation this is */
1237 WORD uFileTime; /* FileTimeToDosDate(WIN32_FIND_DATA->ftLastWriteTime) */
1238 WORD uFileAttribs;
1239 CHAR szName[1];
1242 struct FileStructW {
1243 WORD cbLen; /* Length of this element. */
1244 BYTE abFooBar1[6]; /* Beyond any recognition. */
1245 WORD uDate; /* FileTimeToDosDate(WIN32_FIND_DATA->ftCreationTime)? */
1246 WORD uTime; /* (this is currently speculation) */
1247 WORD uDate2; /* FileTimeToDosDate(WIN32_FIND_DATA->ftLastAccessTime)? */
1248 WORD uTime2; /* (this is currently speculation) */
1249 BYTE abFooBar2[4]; /* Beyond any recognition. */
1250 WCHAR wszName[1]; /* The long filename in unicode. */
1251 /* Just for documentation: Right after the unicode string: */
1252 WORD cbOffset; /* FileStructW's offset from the beginning of the SHITMEID.
1253 * SHITEMID->cb == uOffset + cbLen */
1255 #include "poppack.h"
1257 static void test_ITEMIDLIST_format(void) {
1258 WCHAR wszPersonal[MAX_PATH];
1259 LPSHELLFOLDER psfDesktop, psfPersonal;
1260 LPITEMIDLIST pidlPersonal, pidlFile;
1261 HANDLE hFile;
1262 HRESULT hr;
1263 BOOL bResult;
1264 WCHAR wszFile[3][17] = { { 'e','v','e','n','_',0 }, { 'o','d','d','_',0 },
1265 { 'l','o','n','g','e','r','_','t','h','a','n','.','8','_','3',0 } };
1266 int i;
1268 if(!pSHGetSpecialFolderPathW) return;
1270 bResult = pSHGetSpecialFolderPathW(NULL, wszPersonal, CSIDL_PERSONAL, FALSE);
1271 ok(bResult, "SHGetSpecialFolderPathW failed! Last error: %u\n", GetLastError());
1272 if (!bResult) return;
1274 SetLastError(0xdeadbeef);
1275 bResult = SetCurrentDirectoryW(wszPersonal);
1276 if (!bResult && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) {
1277 win_skip("Most W-calls are not implemented\n");
1278 return;
1280 ok(bResult, "SetCurrentDirectory failed! Last error: %u\n", GetLastError());
1281 if (!bResult) return;
1283 hr = SHGetDesktopFolder(&psfDesktop);
1284 ok(SUCCEEDED(hr), "SHGetDesktopFolder failed! hr: %08x\n", hr);
1285 if (FAILED(hr)) return;
1287 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszPersonal, NULL, &pidlPersonal, NULL);
1288 ok(SUCCEEDED(hr), "psfDesktop->ParseDisplayName failed! hr = %08x\n", hr);
1289 if (FAILED(hr)) {
1290 IShellFolder_Release(psfDesktop);
1291 return;
1294 hr = IShellFolder_BindToObject(psfDesktop, pidlPersonal, NULL, &IID_IShellFolder,
1295 (LPVOID*)&psfPersonal);
1296 IShellFolder_Release(psfDesktop);
1297 pILFree(pidlPersonal);
1298 ok(SUCCEEDED(hr), "psfDesktop->BindToObject failed! hr = %08x\n", hr);
1299 if (FAILED(hr)) return;
1301 for (i=0; i<3; i++) {
1302 CHAR szFile[MAX_PATH];
1303 struct FileStructA *pFileStructA;
1304 WORD cbOffset;
1306 WideCharToMultiByte(CP_ACP, 0, wszFile[i], -1, szFile, MAX_PATH, NULL, NULL);
1308 hFile = CreateFileW(wszFile[i], GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_FLAG_WRITE_THROUGH, NULL);
1309 ok(hFile != INVALID_HANDLE_VALUE, "CreateFile failed! (%u)\n", GetLastError());
1310 if (hFile == INVALID_HANDLE_VALUE) {
1311 IShellFolder_Release(psfPersonal);
1312 return;
1314 CloseHandle(hFile);
1316 hr = IShellFolder_ParseDisplayName(psfPersonal, NULL, NULL, wszFile[i], NULL, &pidlFile, NULL);
1317 DeleteFileW(wszFile[i]);
1318 ok(SUCCEEDED(hr), "psfPersonal->ParseDisplayName failed! hr: %08x\n", hr);
1319 if (FAILED(hr)) {
1320 IShellFolder_Release(psfPersonal);
1321 return;
1324 pFileStructA = (struct FileStructA *)pidlFile->mkid.abID;
1325 ok(pFileStructA->type == 0x32, "PIDLTYPE should be 0x32!\n");
1326 ok(pFileStructA->dummy == 0x00, "Dummy Byte should be 0x00!\n");
1327 ok(pFileStructA->dwFileSize == 0, "Filesize should be zero!\n");
1329 if (i < 2) /* First two file names are already in valid 8.3 format */
1330 ok(!strcmp(szFile, (CHAR*)&pidlFile->mkid.abID[12]), "Wrong file name!\n");
1331 else
1332 /* WinXP stores a derived 8.3 dos name (LONGER~1.8_3) here. We probably
1333 * can't implement this correctly, since unix filesystems don't support
1334 * this nasty short/long filename stuff. So we'll probably stay with our
1335 * current habbit of storing the long filename here, which seems to work
1336 * just fine. */
1337 todo_wine { ok(pidlFile->mkid.abID[18] == '~', "Should be derived 8.3 name!\n"); }
1339 if (i == 0) /* First file name has an even number of chars. No need for alignment. */
1340 ok(pidlFile->mkid.abID[12 + strlen(szFile) + 1] != '\0',
1341 "Alignment byte, where there shouldn't be!\n");
1343 if (i == 1) /* Second file name has an uneven number of chars => alignment byte */
1344 ok(pidlFile->mkid.abID[12 + strlen(szFile) + 1] == '\0',
1345 "There should be an alignment byte, but isn't!\n");
1347 /* The offset of the FileStructW member is stored as a WORD at the end of the pidl. */
1348 cbOffset = *(WORD*)(((LPBYTE)pidlFile)+pidlFile->mkid.cb-sizeof(WORD));
1349 ok (cbOffset >= sizeof(struct FileStructA) &&
1350 cbOffset <= pidlFile->mkid.cb - sizeof(struct FileStructW),
1351 "Wrong offset value (%d) stored at the end of the PIDL\n", cbOffset);
1353 if (cbOffset >= sizeof(struct FileStructA) &&
1354 cbOffset <= pidlFile->mkid.cb - sizeof(struct FileStructW))
1356 struct FileStructW *pFileStructW = (struct FileStructW *)(((LPBYTE)pidlFile)+cbOffset);
1358 ok(pidlFile->mkid.cb == cbOffset + pFileStructW->cbLen,
1359 "FileStructW's offset and length should add up to the PIDL's length!\n");
1361 if (pidlFile->mkid.cb == cbOffset + pFileStructW->cbLen) {
1362 /* Since we just created the file, time of creation,
1363 * time of last access and time of last write access just be the same.
1364 * These tests seem to fail sometimes (on WinXP), if the test is run again shortly
1365 * after the first run. I do remember something with NTFS keeping the creation time
1366 * if a file is deleted and then created again within a couple of seconds or so.
1367 * Might be the reason. */
1368 ok (pFileStructA->uFileDate == pFileStructW->uDate &&
1369 pFileStructA->uFileTime == pFileStructW->uTime,
1370 "Last write time should match creation time!\n");
1372 ok (pFileStructA->uFileDate == pFileStructW->uDate2 &&
1373 pFileStructA->uFileTime == pFileStructW->uTime2,
1374 "Last write time should match last access time!\n");
1376 ok (!lstrcmpW(wszFile[i], pFileStructW->wszName),
1377 "The filename should be stored in unicode at this position!\n");
1381 pILFree(pidlFile);
1385 static void testSHGetFolderPathAndSubDirA(void)
1387 HRESULT ret;
1388 BOOL delret;
1389 DWORD dwret;
1390 int i;
1391 static char wine[] = "wine";
1392 static char winetemp[] = "wine\\temp";
1393 static char appdata[MAX_PATH];
1394 static char testpath[MAX_PATH];
1395 static char toolongpath[MAX_PATH+1];
1397 if(!pSHGetFolderPathA) {
1398 skip("SHGetFolderPathA not present!\n");
1399 return;
1401 if(FAILED(pSHGetFolderPathA(NULL, CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, appdata)))
1403 skip("SHGetFolderPathA failed for CSIDL_LOCAL_APPDATA!\n");
1404 return;
1407 sprintf(testpath, "%s\\%s", appdata, winetemp);
1408 delret = RemoveDirectoryA(testpath);
1409 if(!delret && (ERROR_PATH_NOT_FOUND != GetLastError()) ) {
1410 skip("RemoveDirectoryA(%s) failed with error %u\n", testpath, GetLastError());
1411 return;
1414 sprintf(testpath, "%s\\%s", appdata, wine);
1415 delret = RemoveDirectoryA(testpath);
1416 if(!delret && (ERROR_PATH_NOT_FOUND != GetLastError()) && (ERROR_FILE_NOT_FOUND != GetLastError())) {
1417 skip("RemoveDirectoryA(%s) failed with error %u\n", testpath, GetLastError());
1418 return;
1420 for(i=0; i< MAX_PATH; i++)
1421 toolongpath[i] = '0' + i % 10;
1422 toolongpath[MAX_PATH] = '\0';
1424 /* test invalid second parameter */
1425 ret = pSHGetFolderPathAndSubDirA(NULL, CSIDL_FLAG_DONT_VERIFY | 0xff, NULL, SHGFP_TYPE_CURRENT, wine, testpath);
1426 ok(E_INVALIDARG == ret, "expected E_INVALIDARG, got %x\n", ret);
1428 /* test invalid forth parameter */
1429 ret = pSHGetFolderPathAndSubDirA(NULL, CSIDL_FLAG_DONT_VERIFY | CSIDL_LOCAL_APPDATA, NULL, 2, wine, testpath);
1430 switch(ret) {
1431 case S_OK: /* winvista */
1432 ok(!strncmp(appdata, testpath, strlen(appdata)),
1433 "expected %s to start with %s\n", testpath, appdata);
1434 ok(!lstrcmpA(&testpath[1 + strlen(appdata)], winetemp),
1435 "expected %s to end with %s\n", testpath, winetemp);
1436 break;
1437 case E_INVALIDARG: /* winxp, win2k3 */
1438 break;
1439 default:
1440 ok(0, "expected S_OK or E_INVALIDARG, got %x\n", ret);
1443 /* test fifth parameter */
1444 testpath[0] = '\0';
1445 ret = pSHGetFolderPathAndSubDirA(NULL, CSIDL_FLAG_DONT_VERIFY | CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, NULL, testpath);
1446 ok(S_OK == ret, "expected S_OK, got %x\n", ret);
1447 ok(!lstrcmpA(appdata, testpath), "expected %s, got %s\n", appdata, testpath);
1449 testpath[0] = '\0';
1450 ret = pSHGetFolderPathAndSubDirA(NULL, CSIDL_FLAG_DONT_VERIFY | CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, "", testpath);
1451 ok(S_OK == ret, "expected S_OK, got %x\n", ret);
1452 ok(!lstrcmpA(appdata, testpath), "expected %s, got %s\n", appdata, testpath);
1454 testpath[0] = '\0';
1455 ret = pSHGetFolderPathAndSubDirA(NULL, CSIDL_FLAG_DONT_VERIFY | CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, "\\", testpath);
1456 ok(S_OK == ret, "expected S_OK, got %x\n", ret);
1457 ok(!lstrcmpA(appdata, testpath), "expected %s, got %s\n", appdata, testpath);
1459 ret = pSHGetFolderPathAndSubDirA(NULL, CSIDL_FLAG_DONT_VERIFY | CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, toolongpath, testpath);
1460 ok(HRESULT_FROM_WIN32(ERROR_FILENAME_EXCED_RANGE) == ret,
1461 "expected %x, got %x\n", HRESULT_FROM_WIN32(ERROR_FILENAME_EXCED_RANGE), ret);
1463 testpath[0] = '\0';
1464 ret = pSHGetFolderPathAndSubDirA(NULL, CSIDL_FLAG_DONT_VERIFY | CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, wine, NULL);
1465 ok((S_OK == ret) || (E_INVALIDARG == ret), "expected S_OK or E_INVALIDARG, got %x\n", ret);
1467 /* test a not existing path */
1468 testpath[0] = '\0';
1469 ret = pSHGetFolderPathAndSubDirA(NULL, CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, winetemp, testpath);
1470 ok(HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND) == ret,
1471 "expected %x, got %x\n", HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND), ret);
1473 /* create a directory inside a not existing directory */
1474 testpath[0] = '\0';
1475 ret = pSHGetFolderPathAndSubDirA(NULL, CSIDL_FLAG_CREATE | CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, winetemp, testpath);
1476 ok(S_OK == ret, "expected S_OK, got %x\n", ret);
1477 ok(!strncmp(appdata, testpath, strlen(appdata)),
1478 "expected %s to start with %s\n", testpath, appdata);
1479 ok(!lstrcmpA(&testpath[1 + strlen(appdata)], winetemp),
1480 "expected %s to end with %s\n", testpath, winetemp);
1481 dwret = GetFileAttributes(testpath);
1482 ok(FILE_ATTRIBUTE_DIRECTORY | dwret, "expected %x to contain FILE_ATTRIBUTE_DIRECTORY\n", dwret);
1484 /* cleanup */
1485 sprintf(testpath, "%s\\%s", appdata, winetemp);
1486 RemoveDirectoryA(testpath);
1487 sprintf(testpath, "%s\\%s", appdata, wine);
1488 RemoveDirectoryA(testpath);
1491 static const char *wine_dbgstr_w(LPCWSTR str)
1493 static char buf[512];
1494 if (!str)
1495 return "(null)";
1496 WideCharToMultiByte(CP_ACP, 0, str, -1, buf, sizeof(buf), NULL, NULL);
1497 return buf;
1500 static void test_LocalizedNames(void)
1502 static char cCurrDirA[MAX_PATH];
1503 WCHAR cCurrDirW[MAX_PATH], tempbufW[25];
1504 IShellFolder *IDesktopFolder, *testIShellFolder;
1505 ITEMIDLIST *newPIDL;
1506 int len;
1507 HRESULT hr;
1508 static char resourcefile[MAX_PATH];
1509 DWORD res;
1510 HANDLE file;
1511 STRRET strret;
1513 static const char desktopini_contents1[] =
1514 "[.ShellClassInfo]\r\n"
1515 "LocalizedResourceName=@";
1516 static const char desktopini_contents2[] =
1517 ",-1\r\n";
1518 static WCHAR foldernameW[] = {'t','e','s','t','f','o','l','d','e','r',0};
1519 static const WCHAR folderdisplayW[] = {'F','o','l','d','e','r',' ','N','a','m','e',' ','R','e','s','o','u','r','c','e',0};
1521 /* create folder with desktop.ini and localized name in GetModuleFileNameA(NULL) */
1522 CreateDirectoryA(".\\testfolder", NULL);
1524 SetFileAttributesA(".\\testfolder", GetFileAttributesA(".\\testfolder")|FILE_ATTRIBUTE_SYSTEM);
1526 GetModuleFileNameA(NULL, resourcefile, MAX_PATH);
1528 file = CreateFileA(".\\testfolder\\desktop.ini", GENERIC_WRITE, 0, NULL,
1529 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1530 ok(file != INVALID_HANDLE_VALUE, "CreateFileA failed %i\n", GetLastError());
1531 ok(WriteFile(file, desktopini_contents1, strlen(desktopini_contents1), &res, NULL) &&
1532 WriteFile(file, resourcefile, strlen(resourcefile), &res, NULL) &&
1533 WriteFile(file, desktopini_contents2, strlen(desktopini_contents2), &res, NULL),
1534 "WriteFile failed %i\n", GetLastError());
1535 CloseHandle(file);
1537 /* get IShellFolder for parent */
1538 GetCurrentDirectoryA(MAX_PATH, cCurrDirA);
1539 len = lstrlenA(cCurrDirA);
1541 if (len == 0) {
1542 trace("GetCurrentDirectoryA returned empty string. Skipping test_LocalizedNames\n");
1543 goto cleanup;
1545 if(cCurrDirA[len-1] == '\\')
1546 cCurrDirA[len-1] = 0;
1548 MultiByteToWideChar(CP_ACP, 0, cCurrDirA, -1, cCurrDirW, MAX_PATH);
1550 hr = SHGetDesktopFolder(&IDesktopFolder);
1551 ok(hr == S_OK, "SHGetDesktopfolder failed %08x\n", hr);
1553 hr = IShellFolder_ParseDisplayName(IDesktopFolder, NULL, NULL, cCurrDirW, NULL, &newPIDL, 0);
1554 ok(hr == S_OK, "ParseDisplayName failed %08x\n", hr);
1556 hr = IShellFolder_BindToObject(IDesktopFolder, newPIDL, NULL, (REFIID)&IID_IShellFolder, (LPVOID *)&testIShellFolder);
1557 ok(hr == S_OK, "BindToObject failed %08x\n", hr);
1559 IMalloc_Free(ppM, newPIDL);
1561 /* windows reads the display name from the resource */
1562 hr = IShellFolder_ParseDisplayName(testIShellFolder, NULL, NULL, foldernameW, NULL, &newPIDL, 0);
1563 ok(hr == S_OK, "ParseDisplayName failed %08x\n", hr);
1565 hr = IShellFolder_GetDisplayNameOf(testIShellFolder, newPIDL, SHGDN_INFOLDER, &strret);
1566 ok(hr == S_OK, "GetDisplayNameOf failed %08x\n", hr);
1568 if (SUCCEEDED(hr) && pStrRetToBufW)
1570 hr = pStrRetToBufW(&strret, newPIDL, tempbufW, sizeof(tempbufW)/sizeof(WCHAR));
1571 ok (SUCCEEDED(hr), "StrRetToBufW failed! hr = %08x\n", hr);
1572 todo_wine
1573 ok (!lstrcmpiW(tempbufW, folderdisplayW) ||
1574 broken(!lstrcmpiW(tempbufW, foldernameW)), /* W2K */
1575 "GetDisplayNameOf returned %s\n", wine_dbgstr_w(tempbufW));
1578 /* editing name is also read from the resource */
1579 hr = IShellFolder_GetDisplayNameOf(testIShellFolder, newPIDL, SHGDN_INFOLDER|SHGDN_FOREDITING, &strret);
1580 ok(hr == S_OK, "GetDisplayNameOf failed %08x\n", hr);
1582 if (SUCCEEDED(hr) && pStrRetToBufW)
1584 hr = pStrRetToBufW(&strret, newPIDL, tempbufW, sizeof(tempbufW)/sizeof(WCHAR));
1585 ok (SUCCEEDED(hr), "StrRetToBufW failed! hr = %08x\n", hr);
1586 todo_wine
1587 ok (!lstrcmpiW(tempbufW, folderdisplayW) ||
1588 broken(!lstrcmpiW(tempbufW, foldernameW)), /* W2K */
1589 "GetDisplayNameOf returned %s\n", wine_dbgstr_w(tempbufW));
1592 /* parsing name is unchanged */
1593 hr = IShellFolder_GetDisplayNameOf(testIShellFolder, newPIDL, SHGDN_INFOLDER|SHGDN_FORPARSING, &strret);
1594 ok(hr == S_OK, "GetDisplayNameOf failed %08x\n", hr);
1596 if (SUCCEEDED(hr) && pStrRetToBufW)
1598 hr = pStrRetToBufW(&strret, newPIDL, tempbufW, sizeof(tempbufW)/sizeof(WCHAR));
1599 ok (SUCCEEDED(hr), "StrRetToBufW failed! hr = %08x\n", hr);
1600 ok (!lstrcmpiW(tempbufW, foldernameW), "GetDisplayNameOf returned %s\n", wine_dbgstr_w(tempbufW));
1603 IShellFolder_Release(IDesktopFolder);
1604 IShellFolder_Release(testIShellFolder);
1606 IMalloc_Free(ppM, newPIDL);
1608 cleanup:
1609 DeleteFileA(".\\testfolder\\desktop.ini");
1610 SetFileAttributesA(".\\testfolder", GetFileAttributesA(".\\testfolder")&~FILE_ATTRIBUTE_SYSTEM);
1611 RemoveDirectoryA(".\\testfolder");
1615 START_TEST(shlfolder)
1617 init_function_pointers();
1618 /* if OleInitialize doesn't get called, ParseDisplayName returns
1619 CO_E_NOTINITIALIZED for malformed directory names on win2k. */
1620 OleInitialize(NULL);
1622 test_ParseDisplayName();
1623 test_BindToObject();
1624 test_EnumObjects_and_CompareIDs();
1625 test_GetDisplayName();
1626 test_GetAttributesOf();
1627 test_SHGetPathFromIDList();
1628 test_CallForAttributes();
1629 test_FolderShortcut();
1630 test_ITEMIDLIST_format();
1631 if(pSHGetFolderPathAndSubDirA)
1632 testSHGetFolderPathAndSubDirA();
1633 else
1634 skip("SHGetFolderPathAndSubDirA not present\n");
1635 test_LocalizedNames();
1637 OleUninitialize();