shell32/tests: Fix a test failure on Win98.
[wine/hacks.git] / dlls / shell32 / tests / shlfolder.c
blob0536d2e28639cb3764a22d0b77832d1894c0c215
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 ||
151 pILFindLastID(newPIDL)->mkid.abID[0] == 0xb1, /* Win98 */
152 "Last pidl should be of type PT_FOLDER or PT_IESPECIAL2, but is: %02x\n",
153 pILFindLastID(newPIDL)->mkid.abID[0]);
154 IMalloc_Free(ppM, newPIDL);
156 finished:
157 IShellFolder_Release(IDesktopFolder);
160 /* creates a file with the specified name for tests */
161 static void CreateTestFile(const CHAR *name)
163 HANDLE file;
164 DWORD written;
166 file = CreateFileA(name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
167 if (file != INVALID_HANDLE_VALUE)
169 WriteFile(file, name, strlen(name), &written, NULL);
170 WriteFile(file, "\n", strlen("\n"), &written, NULL);
171 CloseHandle(file);
176 /* initializes the tests */
177 static void CreateFilesFolders(void)
179 CreateDirectoryA(".\\testdir", NULL);
180 CreateDirectoryA(".\\testdir\\test.txt", NULL);
181 CreateTestFile (".\\testdir\\test1.txt ");
182 CreateTestFile (".\\testdir\\test2.txt ");
183 CreateTestFile (".\\testdir\\test3.txt ");
184 CreateDirectoryA(".\\testdir\\testdir2 ", NULL);
185 CreateDirectoryA(".\\testdir\\testdir2\\subdir", NULL);
188 /* cleans after tests */
189 static void Cleanup(void)
191 DeleteFileA(".\\testdir\\test1.txt");
192 DeleteFileA(".\\testdir\\test2.txt");
193 DeleteFileA(".\\testdir\\test3.txt");
194 RemoveDirectoryA(".\\testdir\\test.txt");
195 RemoveDirectoryA(".\\testdir\\testdir2\\subdir");
196 RemoveDirectoryA(".\\testdir\\testdir2");
197 RemoveDirectoryA(".\\testdir");
201 /* perform test */
202 static void test_EnumObjects(IShellFolder *iFolder)
204 IEnumIDList *iEnumList;
205 LPITEMIDLIST newPIDL, idlArr[10];
206 ULONG NumPIDLs;
207 int i=0, j;
208 HRESULT hr;
210 static const WORD iResults [5][5] =
212 { 0,-1,-1,-1,-1},
213 { 1, 0,-1,-1,-1},
214 { 1, 1, 0,-1,-1},
215 { 1, 1, 1, 0,-1},
216 { 1, 1, 1, 1, 0}
219 #define SFGAO_testfor SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR | SFGAO_CAPABILITYMASK
220 /* Don't test for SFGAO_HASSUBFOLDER since we return real state and native cached */
221 static const ULONG attrs[5] =
223 SFGAO_CAPABILITYMASK | SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR,
224 SFGAO_CAPABILITYMASK | SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR,
225 SFGAO_CAPABILITYMASK | SFGAO_FILESYSTEM,
226 SFGAO_CAPABILITYMASK | SFGAO_FILESYSTEM,
227 SFGAO_CAPABILITYMASK | SFGAO_FILESYSTEM,
230 hr = IShellFolder_EnumObjects(iFolder, NULL, SHCONTF_FOLDERS | SHCONTF_NONFOLDERS | SHCONTF_INCLUDEHIDDEN, &iEnumList);
231 ok(hr == S_OK, "EnumObjects failed %08x\n", hr);
233 /* This is to show that, contrary to what is said on MSDN, on IEnumIDList::Next,
234 * the filesystem shellfolders return S_OK even if less than 'celt' items are
235 * returned (in contrast to S_FALSE). We have to do it in a loop since WinXP
236 * only ever returns a single entry per call. */
237 while (IEnumIDList_Next(iEnumList, 10-i, &idlArr[i], &NumPIDLs) == S_OK)
238 i += NumPIDLs;
239 ok (i == 5, "i: %d\n", i);
241 hr = IEnumIDList_Release(iEnumList);
242 ok(hr == S_OK, "IEnumIDList_Release failed %08x\n", hr);
244 /* Sort them first in case of wrong order from system */
245 for (i=0;i<5;i++) for (j=0;j<5;j++)
246 if ((SHORT)IShellFolder_CompareIDs(iFolder, 0, idlArr[i], idlArr[j]) < 0)
248 newPIDL = idlArr[i];
249 idlArr[i] = idlArr[j];
250 idlArr[j] = newPIDL;
253 for (i=0;i<5;i++) for (j=0;j<5;j++)
255 hr = IShellFolder_CompareIDs(iFolder, 0, idlArr[i], idlArr[j]);
256 ok(hr == iResults[i][j], "Got %x expected [%d]-[%d]=%x\n", hr, i, j, iResults[i][j]);
260 for (i = 0; i < 5; i++)
262 SFGAOF flags;
263 /* Native returns all flags no matter what we ask for */
264 flags = SFGAO_CANCOPY;
265 hr = IShellFolder_GetAttributesOf(iFolder, 1, (LPCITEMIDLIST*)(idlArr + i), &flags);
266 flags &= SFGAO_testfor;
267 ok(hr == S_OK, "GetAttributesOf returns %08x\n", hr);
268 ok(flags == (attrs[i]) ||
269 flags == (attrs[i] & ~SFGAO_FILESYSANCESTOR), /* Win9x, NT4 */
270 "GetAttributesOf[%i] got %08x, expected %08x\n", i, flags, attrs[i]);
272 flags = SFGAO_testfor;
273 hr = IShellFolder_GetAttributesOf(iFolder, 1, (LPCITEMIDLIST*)(idlArr + i), &flags);
274 flags &= SFGAO_testfor;
275 ok(hr == S_OK, "GetAttributesOf returns %08x\n", hr);
276 ok(flags == attrs[i] ||
277 flags == (attrs[i] & ~SFGAO_FILESYSANCESTOR), /* Win9x, NT4 */
278 "GetAttributesOf[%i] got %08x, expected %08x\n", i, flags, attrs[i]);
281 for (i=0;i<5;i++)
282 IMalloc_Free(ppM, idlArr[i]);
285 static void test_BindToObject(void)
287 HRESULT hr;
288 UINT cChars;
289 IShellFolder *psfDesktop, *psfChild, *psfMyComputer, *psfSystemDir;
290 SHITEMID emptyitem = { 0, { 0 } };
291 LPITEMIDLIST pidlMyComputer, pidlSystemDir, pidlEmpty = (LPITEMIDLIST)&emptyitem;
292 WCHAR wszSystemDir[MAX_PATH];
293 char szSystemDir[MAX_PATH];
294 WCHAR wszMyComputer[] = {
295 ':',':','{','2','0','D','0','4','F','E','0','-','3','A','E','A','-','1','0','6','9','-',
296 'A','2','D','8','-','0','8','0','0','2','B','3','0','3','0','9','D','}',0 };
298 /* The following tests shows that BindToObject should fail with E_INVALIDARG if called
299 * with an empty pidl. This is tested for Desktop, MyComputer and the FS ShellFolder
301 hr = SHGetDesktopFolder(&psfDesktop);
302 ok (SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08x\n", hr);
303 if (FAILED(hr)) return;
305 hr = IShellFolder_BindToObject(psfDesktop, pidlEmpty, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
306 ok (hr == E_INVALIDARG, "Desktop's BindToObject should fail, when called with empty pidl! hr = %08x\n", hr);
308 hr = IShellFolder_BindToObject(psfDesktop, NULL, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
309 ok (hr == E_INVALIDARG, "Desktop's BindToObject should fail, when called with NULL pidl! hr = %08x\n", hr);
311 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszMyComputer, NULL, &pidlMyComputer, NULL);
312 ok (SUCCEEDED(hr), "Desktop's ParseDisplayName failed to parse MyComputer's CLSID! hr = %08x\n", hr);
313 if (FAILED(hr)) {
314 IShellFolder_Release(psfDesktop);
315 return;
318 hr = IShellFolder_BindToObject(psfDesktop, pidlMyComputer, NULL, &IID_IShellFolder, (LPVOID*)&psfMyComputer);
319 ok (SUCCEEDED(hr), "Desktop failed to bind to MyComputer object! hr = %08x\n", hr);
320 IShellFolder_Release(psfDesktop);
321 IMalloc_Free(ppM, pidlMyComputer);
322 if (FAILED(hr)) return;
324 hr = IShellFolder_BindToObject(psfMyComputer, pidlEmpty, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
325 ok (hr == E_INVALIDARG, "MyComputers's BindToObject should fail, when called with empty pidl! hr = %08x\n", hr);
327 #if 0
328 /* this call segfaults on 98SE */
329 hr = IShellFolder_BindToObject(psfMyComputer, NULL, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
330 ok (hr == E_INVALIDARG, "MyComputers's BindToObject should fail, when called with NULL pidl! hr = %08x\n", hr);
331 #endif
333 cChars = GetSystemDirectoryA(szSystemDir, MAX_PATH);
334 ok (cChars > 0 && cChars < MAX_PATH, "GetSystemDirectoryA failed! LastError: %u\n", GetLastError());
335 if (cChars == 0 || cChars >= MAX_PATH) {
336 IShellFolder_Release(psfMyComputer);
337 return;
339 MultiByteToWideChar(CP_ACP, 0, szSystemDir, -1, wszSystemDir, MAX_PATH);
341 hr = IShellFolder_ParseDisplayName(psfMyComputer, NULL, NULL, wszSystemDir, NULL, &pidlSystemDir, NULL);
342 ok (SUCCEEDED(hr), "MyComputers's ParseDisplayName failed to parse the SystemDirectory! hr = %08x\n", hr);
343 if (FAILED(hr)) {
344 IShellFolder_Release(psfMyComputer);
345 return;
348 hr = IShellFolder_BindToObject(psfMyComputer, pidlSystemDir, NULL, &IID_IShellFolder, (LPVOID*)&psfSystemDir);
349 ok (SUCCEEDED(hr), "MyComputer failed to bind to a FileSystem ShellFolder! hr = %08x\n", hr);
350 IShellFolder_Release(psfMyComputer);
351 IMalloc_Free(ppM, pidlSystemDir);
352 if (FAILED(hr)) return;
354 hr = IShellFolder_BindToObject(psfSystemDir, pidlEmpty, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
355 ok (hr == E_INVALIDARG,
356 "FileSystem ShellFolder's BindToObject should fail, when called with empty pidl! hr = %08x\n", hr);
358 #if 0
359 /* this call segfaults on 98SE */
360 hr = IShellFolder_BindToObject(psfSystemDir, NULL, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
361 ok (hr == E_INVALIDARG,
362 "FileSystem ShellFolder's BindToObject should fail, when called with NULL pidl! hr = %08x\n", hr);
363 #endif
365 IShellFolder_Release(psfSystemDir);
368 /* Based on PathAddBackslashW from dlls/shlwapi/path.c */
369 static LPWSTR myPathAddBackslashW( LPWSTR lpszPath )
371 size_t iLen;
373 if (!lpszPath || (iLen = lstrlenW(lpszPath)) >= MAX_PATH)
374 return NULL;
376 if (iLen)
378 lpszPath += iLen;
379 if (lpszPath[-1] != '\\')
381 *lpszPath++ = '\\';
382 *lpszPath = '\0';
385 return lpszPath;
388 static void test_GetDisplayName(void)
390 BOOL result;
391 HRESULT hr;
392 HANDLE hTestFile;
393 WCHAR wszTestFile[MAX_PATH], wszTestFile2[MAX_PATH];
394 char szTestFile[MAX_PATH], szTestDir[MAX_PATH];
395 DWORD attr;
396 STRRET strret;
397 LPSHELLFOLDER psfDesktop, psfPersonal;
398 IUnknown *psfFile;
399 SHITEMID emptyitem = { 0, { 0 } };
400 LPITEMIDLIST pidlTestFile, pidlEmpty = (LPITEMIDLIST)&emptyitem;
401 LPCITEMIDLIST pidlLast;
402 static const CHAR szFileName[] = "winetest.foo";
403 static const WCHAR wszFileName[] = { 'w','i','n','e','t','e','s','t','.','f','o','o',0 };
404 static const WCHAR wszDirName[] = { 'w','i','n','e','t','e','s','t',0 };
406 /* I'm trying to figure if there is a functional difference between calling
407 * SHGetPathFromIDListW and calling GetDisplayNameOf(SHGDN_FORPARSING) after
408 * binding to the shellfolder. One thing I thought of was that perhaps
409 * SHGetPathFromIDListW would be able to get the path to a file, which does
410 * not exist anymore, while the other method wouldn't. It turns out there's
411 * no functional difference in this respect.
414 if(!pSHGetSpecialFolderPathA) {
415 win_skip("SHGetSpecialFolderPathA is not available\n");
416 return;
419 /* First creating a directory in MyDocuments and a file in this directory. */
420 result = pSHGetSpecialFolderPathA(NULL, szTestDir, CSIDL_PERSONAL, FALSE);
421 ok(result, "SHGetSpecialFolderPathA failed! Last error: %u\n", GetLastError());
422 if (!result) return;
424 /* Use ANSI file functions so this works on Windows 9x */
425 lstrcatA(szTestDir, "\\winetest");
426 CreateDirectoryA(szTestDir, NULL);
427 attr=GetFileAttributesA(szTestDir);
428 if (attr == INVALID_FILE_ATTRIBUTES || !(attr & FILE_ATTRIBUTE_DIRECTORY))
430 ok(0, "unable to create the '%s' directory\n", szTestDir);
431 return;
434 lstrcpyA(szTestFile, szTestDir);
435 lstrcatA(szTestFile, "\\");
436 lstrcatA(szTestFile, szFileName);
437 hTestFile = CreateFileA(szTestFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
438 ok((hTestFile != INVALID_HANDLE_VALUE), "CreateFileA failed! Last error: %u\n", GetLastError());
439 if (hTestFile == INVALID_HANDLE_VALUE) return;
440 CloseHandle(hTestFile);
442 /* Getting an itemidlist for the file. */
443 hr = SHGetDesktopFolder(&psfDesktop);
444 ok(SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08x\n", hr);
445 if (FAILED(hr)) return;
447 MultiByteToWideChar(CP_ACP, 0, szTestFile, -1, wszTestFile, MAX_PATH);
449 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszTestFile, NULL, &pidlTestFile, NULL);
450 ok(SUCCEEDED(hr), "Desktop->ParseDisplayName failed! hr = %08x\n", hr);
451 if (FAILED(hr)) {
452 IShellFolder_Release(psfDesktop);
453 return;
456 pidlLast = pILFindLastID(pidlTestFile);
457 ok(pidlLast->mkid.cb >=76 ||
458 broken(pidlLast->mkid.cb == 28) || /* W2K */
459 broken(pidlLast->mkid.cb == 40), /* Win9x, WinME */
460 "Expected pidl length of at least 76, got %d.\n", pidlLast->mkid.cb);
461 if (pidlLast->mkid.cb >= 28) {
462 ok(!lstrcmpA((CHAR*)&pidlLast->mkid.abID[12], szFileName),
463 "Filename should be stored as ansi-string at this position!\n");
465 /* WinXP and up store the filenames as both ANSI and UNICODE in the pidls */
466 if (pidlLast->mkid.cb >= 76) {
467 ok(!lstrcmpW((WCHAR*)&pidlLast->mkid.abID[46], wszFileName),
468 "Filename should be stored as wchar-string at this position!\n");
471 /* It seems as if we cannot bind to regular files on windows, but only directories.
473 hr = IShellFolder_BindToObject(psfDesktop, pidlTestFile, NULL, &IID_IUnknown, (VOID**)&psfFile);
474 todo_wine
475 ok (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) ||
476 broken(SUCCEEDED(hr)), /* Win9x, W2K */
477 "hr = %08x\n", hr);
478 if (SUCCEEDED(hr)) {
479 IShellFolder_Release(psfFile);
482 if (!pSHBindToParent)
484 win_skip("SHBindToParent is missing\n");
485 DeleteFileA(szTestFile);
486 RemoveDirectoryA(szTestDir);
487 return;
490 /* Some tests for IShellFolder::SetNameOf */
491 if (pSHGetFolderPathAndSubDirA)
493 hr = pSHBindToParent(pidlTestFile, &IID_IShellFolder, (VOID**)&psfPersonal, &pidlLast);
494 ok(SUCCEEDED(hr), "SHBindToParent failed! hr = %08x\n", hr);
495 if (SUCCEEDED(hr)) {
496 /* It's ok to use this fixed path. Call will fail anyway. */
497 WCHAR wszAbsoluteFilename[] = { 'C',':','\\','w','i','n','e','t','e','s','t', 0 };
498 LPITEMIDLIST pidlNew;
500 /* The pidl returned through the last parameter of SetNameOf is a simple one. */
501 hr = IShellFolder_SetNameOf(psfPersonal, NULL, pidlLast, wszDirName, SHGDN_NORMAL, &pidlNew);
502 ok (SUCCEEDED(hr), "SetNameOf failed! hr = %08x\n", hr);
503 if (hr == S_OK)
505 ok (((LPITEMIDLIST)((LPBYTE)pidlNew+pidlNew->mkid.cb))->mkid.cb == 0,
506 "pidl returned from SetNameOf should be simple!\n");
508 /* Passing an absolute path to SetNameOf fails. The HRESULT code indicates that SetNameOf
509 * is implemented on top of SHFileOperation in WinXP. */
510 hr = IShellFolder_SetNameOf(psfPersonal, NULL, pidlNew, wszAbsoluteFilename,
511 SHGDN_FORPARSING, NULL);
512 ok (hr == HRESULT_FROM_WIN32(ERROR_CANCELLED), "SetNameOf succeeded! hr = %08x\n", hr);
514 /* Rename the file back to its original name. SetNameOf ignores the fact, that the
515 * SHGDN flags specify an absolute path. */
516 hr = IShellFolder_SetNameOf(psfPersonal, NULL, pidlNew, wszFileName, SHGDN_FORPARSING, NULL);
517 ok (SUCCEEDED(hr), "SetNameOf failed! hr = %08x\n", hr);
519 pILFree(pidlNew);
522 IShellFolder_Release(psfPersonal);
525 else
526 win_skip("Avoid needs of interaction on Win2k\n");
528 /* Deleting the file and the directory */
529 DeleteFileA(szTestFile);
530 RemoveDirectoryA(szTestDir);
532 /* SHGetPathFromIDListW still works, although the file is not present anymore. */
533 if (pSHGetPathFromIDListW)
535 result = pSHGetPathFromIDListW(pidlTestFile, wszTestFile2);
536 ok (result, "SHGetPathFromIDListW failed! Last error: %u\n", GetLastError());
537 ok (!lstrcmpiW(wszTestFile, wszTestFile2), "SHGetPathFromIDListW returns incorrect path!\n");
540 /* SHBindToParent fails, if called with a NULL PIDL. */
541 hr = pSHBindToParent(NULL, &IID_IShellFolder, (VOID**)&psfPersonal, &pidlLast);
542 ok (FAILED(hr), "SHBindToParent(NULL) should fail!\n");
544 /* But it succeeds with an empty PIDL. */
545 hr = pSHBindToParent(pidlEmpty, &IID_IShellFolder, (VOID**)&psfPersonal, &pidlLast);
546 ok (SUCCEEDED(hr), "SHBindToParent(empty PIDL) should succeed! hr = %08x\n", hr);
547 ok (pidlLast == pidlEmpty, "The last element of an empty PIDL should be the PIDL itself!\n");
548 if (SUCCEEDED(hr))
549 IShellFolder_Release(psfPersonal);
551 /* Binding to the folder and querying the display name of the file also works. */
552 hr = pSHBindToParent(pidlTestFile, &IID_IShellFolder, (VOID**)&psfPersonal, &pidlLast);
553 ok (SUCCEEDED(hr), "SHBindToParent failed! hr = %08x\n", hr);
554 if (FAILED(hr)) {
555 IShellFolder_Release(psfDesktop);
556 return;
559 /* This test shows that Windows doesn't allocate a new pidlLast, but returns a pointer into
560 * pidlTestFile (In accordance with MSDN). */
561 ok (pILFindLastID(pidlTestFile) == pidlLast,
562 "SHBindToParent doesn't return the last id of the pidl param!\n");
564 hr = IShellFolder_GetDisplayNameOf(psfPersonal, pidlLast, SHGDN_FORPARSING, &strret);
565 ok (SUCCEEDED(hr), "Personal->GetDisplayNameOf failed! hr = %08x\n", hr);
566 if (FAILED(hr)) {
567 IShellFolder_Release(psfDesktop);
568 IShellFolder_Release(psfPersonal);
569 return;
572 if (pStrRetToBufW)
574 hr = pStrRetToBufW(&strret, pidlLast, wszTestFile2, MAX_PATH);
575 ok (SUCCEEDED(hr), "StrRetToBufW failed! hr = %08x\n", hr);
576 ok (!lstrcmpiW(wszTestFile, wszTestFile2), "GetDisplayNameOf returns incorrect path!\n");
579 IShellFolder_Release(psfDesktop);
580 IShellFolder_Release(psfPersonal);
583 static void test_CallForAttributes(void)
585 HKEY hKey;
586 LONG lResult;
587 HRESULT hr;
588 DWORD dwSize;
589 LPSHELLFOLDER psfDesktop;
590 LPITEMIDLIST pidlMyDocuments;
591 DWORD dwAttributes, dwCallForAttributes, dwOrigAttributes, dwOrigCallForAttributes;
592 static const WCHAR wszAttributes[] = { 'A','t','t','r','i','b','u','t','e','s',0 };
593 static const WCHAR wszCallForAttributes[] = {
594 'C','a','l','l','F','o','r','A','t','t','r','i','b','u','t','e','s',0 };
595 static const WCHAR wszMyDocumentsKey[] = {
596 'C','L','S','I','D','\\','{','4','5','0','D','8','F','B','A','-','A','D','2','5','-',
597 '1','1','D','0','-','9','8','A','8','-','0','8','0','0','3','6','1','B','1','1','0','3','}',
598 '\\','S','h','e','l','l','F','o','l','d','e','r',0 };
599 WCHAR wszMyDocuments[] = {
600 ':',':','{','4','5','0','D','8','F','B','A','-','A','D','2','5','-','1','1','D','0','-',
601 '9','8','A','8','-','0','8','0','0','3','6','1','B','1','1','0','3','}',0 };
603 /* For the root of a namespace extension, the attributes are not queried by binding
604 * to the object and calling GetAttributesOf. Instead, the attributes are read from
605 * the registry value HKCR/CLSID/{...}/ShellFolder/Attributes. This is documented on MSDN.
607 * The MyDocuments shellfolder on WinXP has a HKCR/CLSID/{...}/ShellFolder/CallForAttributes
608 * value. It seems that if the folder is queried for one of the flags set in CallForAttributes,
609 * the shell does bind to the folder object and calls GetAttributesOf. This is not documented
610 * on MSDN. This test is meant to document the observed behaviour on WinXP SP2.
612 hr = SHGetDesktopFolder(&psfDesktop);
613 ok (SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08x\n", hr);
614 if (FAILED(hr)) return;
616 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszMyDocuments, NULL,
617 &pidlMyDocuments, NULL);
618 ok (SUCCEEDED(hr) ||
619 broken(hr == E_INVALIDARG), /* Win95, NT4 */
620 "Desktop's ParseDisplayName failed to parse MyDocuments's CLSID! hr = %08x\n", hr);
621 if (FAILED(hr)) {
622 IShellFolder_Release(psfDesktop);
623 return;
626 dwAttributes = 0xffffffff;
627 hr = IShellFolder_GetAttributesOf(psfDesktop, 1,
628 (LPCITEMIDLIST*)&pidlMyDocuments, &dwAttributes);
629 ok (SUCCEEDED(hr), "Desktop->GetAttributesOf(MyDocuments) failed! hr = %08x\n", hr);
631 /* We need the following setup (as observed on WinXP SP2), for the tests to make sense. */
632 ok (dwAttributes & SFGAO_FILESYSTEM, "SFGAO_FILESYSTEM attribute is not set for MyDocuments!\n");
633 ok (!(dwAttributes & SFGAO_ISSLOW), "SFGAO_ISSLOW attribute is set for MyDocuments!\n");
634 ok (!(dwAttributes & SFGAO_GHOSTED), "SFGAO_GHOSTED attribute is set for MyDocuments!\n");
636 /* We don't have the MyDocuments shellfolder in wine yet, and thus we don't have the registry
637 * key. So the test will return at this point, if run on wine.
639 lResult = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszMyDocumentsKey, 0, KEY_WRITE|KEY_READ, &hKey);
640 ok (lResult == ERROR_SUCCESS, "RegOpenKeyEx failed! result: %08x\n", lResult);
641 if (lResult != ERROR_SUCCESS) {
642 IMalloc_Free(ppM, pidlMyDocuments);
643 IShellFolder_Release(psfDesktop);
644 return;
647 /* Query MyDocuments' Attributes value, to be able to restore it later. */
648 dwSize = sizeof(DWORD);
649 lResult = RegQueryValueExW(hKey, wszAttributes, NULL, NULL, (LPBYTE)&dwOrigAttributes, &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 /* Query MyDocuments' CallForAttributes value, to be able to restore it later. */
659 dwSize = sizeof(DWORD);
660 lResult = RegQueryValueExW(hKey, wszCallForAttributes, NULL, NULL,
661 (LPBYTE)&dwOrigCallForAttributes, &dwSize);
662 ok (lResult == ERROR_SUCCESS, "RegQueryValueEx failed! result: %08x\n", lResult);
663 if (lResult != ERROR_SUCCESS) {
664 RegCloseKey(hKey);
665 IMalloc_Free(ppM, pidlMyDocuments);
666 IShellFolder_Release(psfDesktop);
667 return;
670 /* Define via the Attributes value that MyDocuments attributes are SFGAO_ISSLOW and
671 * SFGAO_GHOSTED and that MyDocuments should be called for the SFGAO_ISSLOW and
672 * SFGAO_FILESYSTEM attributes. */
673 dwAttributes = SFGAO_ISSLOW|SFGAO_GHOSTED;
674 RegSetValueExW(hKey, wszAttributes, 0, REG_DWORD, (LPBYTE)&dwAttributes, sizeof(DWORD));
675 dwCallForAttributes = SFGAO_ISSLOW|SFGAO_FILESYSTEM;
676 RegSetValueExW(hKey, wszCallForAttributes, 0, REG_DWORD,
677 (LPBYTE)&dwCallForAttributes, sizeof(DWORD));
679 /* Although it is not set in CallForAttributes, the SFGAO_GHOSTED flag is reset by
680 * GetAttributesOf. It seems that once there is a single attribute queried, for which
681 * CallForAttributes is set, all flags are taken from the GetAttributesOf call and
682 * the flags in Attributes are ignored.
684 dwAttributes = SFGAO_ISSLOW|SFGAO_GHOSTED|SFGAO_FILESYSTEM;
685 hr = IShellFolder_GetAttributesOf(psfDesktop, 1,
686 (LPCITEMIDLIST*)&pidlMyDocuments, &dwAttributes);
687 ok (SUCCEEDED(hr), "Desktop->GetAttributesOf(MyDocuments) failed! hr = %08x\n", hr);
688 if (SUCCEEDED(hr))
689 ok (dwAttributes == SFGAO_FILESYSTEM,
690 "Desktop->GetAttributes(MyDocuments) returned unexpected attributes: %08x\n",
691 dwAttributes);
693 /* Restore MyDocuments' original Attributes and CallForAttributes registry values */
694 RegSetValueExW(hKey, wszAttributes, 0, REG_DWORD, (LPBYTE)&dwOrigAttributes, sizeof(DWORD));
695 RegSetValueExW(hKey, wszCallForAttributes, 0, REG_DWORD,
696 (LPBYTE)&dwOrigCallForAttributes, sizeof(DWORD));
697 RegCloseKey(hKey);
698 IMalloc_Free(ppM, pidlMyDocuments);
699 IShellFolder_Release(psfDesktop);
702 static void test_GetAttributesOf(void)
704 HRESULT hr;
705 LPSHELLFOLDER psfDesktop, psfMyComputer;
706 SHITEMID emptyitem = { 0, { 0 } };
707 LPCITEMIDLIST pidlEmpty = (LPCITEMIDLIST)&emptyitem;
708 LPITEMIDLIST pidlMyComputer;
709 DWORD dwFlags;
710 static const DWORD desktopFlags[] = {
711 /* WinXP */
712 SFGAO_STORAGE | SFGAO_HASPROPSHEET | SFGAO_STORAGEANCESTOR | SFGAO_FILESYSANCESTOR |
713 SFGAO_FOLDER | SFGAO_FILESYSTEM | SFGAO_HASSUBFOLDER,
714 /* Win2k */
715 SFGAO_CANRENAME | SFGAO_HASPROPSHEET | SFGAO_STREAM | SFGAO_FILESYSANCESTOR |
716 SFGAO_FOLDER | SFGAO_FILESYSTEM | SFGAO_HASSUBFOLDER,
717 /* WinMe, Win9x, WinNT*/
718 SFGAO_CANRENAME | SFGAO_HASPROPSHEET | SFGAO_FILESYSANCESTOR |
719 SFGAO_FOLDER | SFGAO_FILESYSTEM | SFGAO_HASSUBFOLDER
721 static const DWORD myComputerFlags[] = {
722 /* WinXP */
723 SFGAO_CANRENAME | SFGAO_CANDELETE | SFGAO_HASPROPSHEET | SFGAO_DROPTARGET |
724 SFGAO_FILESYSANCESTOR | SFGAO_FOLDER | SFGAO_HASSUBFOLDER,
725 /* Win2k */
726 SFGAO_CANRENAME | SFGAO_HASPROPSHEET | SFGAO_DROPTARGET | SFGAO_STREAM |
727 SFGAO_FILESYSANCESTOR | SFGAO_FOLDER | SFGAO_HASSUBFOLDER,
728 /* WinMe, Win9x, WinNT */
729 SFGAO_CANRENAME | SFGAO_HASPROPSHEET | SFGAO_DROPTARGET | SFGAO_FILESYSANCESTOR |
730 SFGAO_FOLDER | SFGAO_HASSUBFOLDER,
731 /* Win95, WinNT when queried directly */
732 SFGAO_CANLINK | SFGAO_HASPROPSHEET | SFGAO_DROPTARGET | SFGAO_FILESYSANCESTOR |
733 SFGAO_FOLDER | SFGAO_FILESYSTEM | SFGAO_HASSUBFOLDER
735 WCHAR wszMyComputer[] = {
736 ':',':','{','2','0','D','0','4','F','E','0','-','3','A','E','A','-','1','0','6','9','-',
737 'A','2','D','8','-','0','8','0','0','2','B','3','0','3','0','9','D','}',0 };
738 char cCurrDirA [MAX_PATH] = {0};
739 WCHAR cCurrDirW [MAX_PATH];
740 static WCHAR cTestDirW[] = {'t','e','s','t','d','i','r',0};
741 IShellFolder *IDesktopFolder, *testIShellFolder;
742 ITEMIDLIST *newPIDL;
743 int len, i;
744 BOOL foundFlagsMatch;
746 hr = SHGetDesktopFolder(&psfDesktop);
747 ok (SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08x\n", hr);
748 if (FAILED(hr)) return;
750 /* The Desktop attributes can be queried with a single empty itemidlist, .. */
751 dwFlags = 0xffffffff;
752 hr = IShellFolder_GetAttributesOf(psfDesktop, 1, &pidlEmpty, &dwFlags);
753 ok (SUCCEEDED(hr), "Desktop->GetAttributesOf(empty pidl) failed! hr = %08x\n", hr);
754 for (i = 0, foundFlagsMatch = FALSE; !foundFlagsMatch &&
755 i < sizeof(desktopFlags) / sizeof(desktopFlags[0]); i++)
757 if (desktopFlags[i] == dwFlags)
758 foundFlagsMatch = TRUE;
760 ok (foundFlagsMatch, "Wrong Desktop attributes: %08x\n", dwFlags);
762 /* .. or with no itemidlist at all. */
763 dwFlags = 0xffffffff;
764 hr = IShellFolder_GetAttributesOf(psfDesktop, 0, NULL, &dwFlags);
765 ok (SUCCEEDED(hr), "Desktop->GetAttributesOf(NULL) failed! hr = %08x\n", hr);
766 for (i = 0, foundFlagsMatch = FALSE; !foundFlagsMatch &&
767 i < sizeof(desktopFlags) / sizeof(desktopFlags[0]); i++)
769 if (desktopFlags[i] == dwFlags)
770 foundFlagsMatch = TRUE;
772 ok (foundFlagsMatch, "Wrong Desktop attributes: %08x\n", dwFlags);
774 /* Testing the attributes of the MyComputer shellfolder */
775 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszMyComputer, NULL, &pidlMyComputer, NULL);
776 ok (SUCCEEDED(hr), "Desktop's ParseDisplayName failed to parse MyComputer's CLSID! hr = %08x\n", hr);
777 if (FAILED(hr)) {
778 IShellFolder_Release(psfDesktop);
779 return;
782 /* Windows sets the SFGAO_CANLINK flag, when MyComputer is queried via the Desktop
783 * folder object. It doesn't do this, if MyComputer is queried directly (see below).
785 dwFlags = 0xffffffff;
786 hr = IShellFolder_GetAttributesOf(psfDesktop, 1, (LPCITEMIDLIST*)&pidlMyComputer, &dwFlags);
787 ok (SUCCEEDED(hr), "Desktop->GetAttributesOf(MyComputer) failed! hr = %08x\n", hr);
788 for (i = 0, foundFlagsMatch = FALSE; !foundFlagsMatch &&
789 i < sizeof(myComputerFlags) / sizeof(myComputerFlags[0]); i++)
791 if ((myComputerFlags[i] | SFGAO_CANLINK) == dwFlags)
792 foundFlagsMatch = TRUE;
794 todo_wine
795 ok (foundFlagsMatch, "Wrong MyComputer attributes: %08x\n", dwFlags);
797 hr = IShellFolder_BindToObject(psfDesktop, pidlMyComputer, NULL, &IID_IShellFolder, (LPVOID*)&psfMyComputer);
798 ok (SUCCEEDED(hr), "Desktop failed to bind to MyComputer object! hr = %08x\n", hr);
799 IShellFolder_Release(psfDesktop);
800 IMalloc_Free(ppM, pidlMyComputer);
801 if (FAILED(hr)) return;
803 hr = IShellFolder_GetAttributesOf(psfMyComputer, 1, &pidlEmpty, &dwFlags);
804 todo_wine
805 ok (hr == E_INVALIDARG ||
806 broken(SUCCEEDED(hr)), /* W2K and earlier */
807 "MyComputer->GetAttributesOf(emtpy pidl) should fail! hr = %08x\n", hr);
809 dwFlags = 0xffffffff;
810 hr = IShellFolder_GetAttributesOf(psfMyComputer, 0, NULL, &dwFlags);
811 ok (SUCCEEDED(hr), "MyComputer->GetAttributesOf(NULL) failed! hr = %08x\n", hr);
812 for (i = 0, foundFlagsMatch = FALSE; !foundFlagsMatch &&
813 i < sizeof(myComputerFlags) / sizeof(myComputerFlags[0]); i++)
815 if (myComputerFlags[i] == dwFlags)
816 foundFlagsMatch = TRUE;
818 todo_wine
819 ok (foundFlagsMatch, "Wrong MyComputer attributes: %08x\n", dwFlags);
821 IShellFolder_Release(psfMyComputer);
823 GetCurrentDirectoryA(MAX_PATH, cCurrDirA);
824 len = lstrlenA(cCurrDirA);
826 if (len == 0) {
827 win_skip("GetCurrentDirectoryA returned empty string. Skipping test_GetAttributesOf\n");
828 return;
830 if (len > 3 && cCurrDirA[len-1] == '\\')
831 cCurrDirA[len-1] = 0;
833 /* create test directory */
834 CreateFilesFolders();
836 MultiByteToWideChar(CP_ACP, 0, cCurrDirA, -1, cCurrDirW, MAX_PATH);
838 hr = SHGetDesktopFolder(&IDesktopFolder);
839 ok(hr == S_OK, "SHGetDesktopfolder failed %08x\n", hr);
841 hr = IShellFolder_ParseDisplayName(IDesktopFolder, NULL, NULL, cCurrDirW, NULL, &newPIDL, 0);
842 ok(hr == S_OK, "ParseDisplayName failed %08x\n", hr);
844 hr = IShellFolder_BindToObject(IDesktopFolder, newPIDL, NULL, (REFIID)&IID_IShellFolder, (LPVOID *)&testIShellFolder);
845 ok(hr == S_OK, "BindToObject failed %08x\n", hr);
847 IMalloc_Free(ppM, newPIDL);
849 /* get relative PIDL */
850 hr = IShellFolder_ParseDisplayName(testIShellFolder, NULL, NULL, cTestDirW, NULL, &newPIDL, 0);
851 ok(hr == S_OK, "ParseDisplayName failed %08x\n", hr);
853 /* test the shell attributes of the test directory using the relative PIDL */
854 dwFlags = SFGAO_FOLDER;
855 hr = IShellFolder_GetAttributesOf(testIShellFolder, 1, (LPCITEMIDLIST*)&newPIDL, &dwFlags);
856 ok (SUCCEEDED(hr), "Desktop->GetAttributesOf() failed! hr = %08x\n", hr);
857 ok ((dwFlags&SFGAO_FOLDER), "Wrong directory attribute for relative PIDL: %08x\n", dwFlags);
859 /* free memory */
860 IMalloc_Free(ppM, newPIDL);
862 /* append testdirectory name to path */
863 if (cCurrDirA[len-1] == '\\')
864 cCurrDirA[len-1] = 0;
865 lstrcatA(cCurrDirA, "\\testdir");
866 MultiByteToWideChar(CP_ACP, 0, cCurrDirA, -1, cCurrDirW, MAX_PATH);
868 hr = IShellFolder_ParseDisplayName(IDesktopFolder, NULL, NULL, cCurrDirW, NULL, &newPIDL, 0);
869 ok(hr == S_OK, "ParseDisplayName failed %08x\n", hr);
871 /* test the shell attributes of the test directory using the absolute PIDL */
872 dwFlags = SFGAO_FOLDER;
873 hr = IShellFolder_GetAttributesOf(IDesktopFolder, 1, (LPCITEMIDLIST*)&newPIDL, &dwFlags);
874 ok (SUCCEEDED(hr), "Desktop->GetAttributesOf() failed! hr = %08x\n", hr);
875 ok ((dwFlags&SFGAO_FOLDER), "Wrong directory attribute for absolute PIDL: %08x\n", dwFlags);
877 /* free memory */
878 IMalloc_Free(ppM, newPIDL);
880 IShellFolder_Release(testIShellFolder);
882 Cleanup();
884 IShellFolder_Release(IDesktopFolder);
887 static void test_SHGetPathFromIDList(void)
889 SHITEMID emptyitem = { 0, { 0 } };
890 LPCITEMIDLIST pidlEmpty = (LPCITEMIDLIST)&emptyitem;
891 LPITEMIDLIST pidlMyComputer;
892 WCHAR wszPath[MAX_PATH], wszDesktop[MAX_PATH];
893 BOOL result;
894 HRESULT hr;
895 LPSHELLFOLDER psfDesktop;
896 WCHAR wszMyComputer[] = {
897 ':',':','{','2','0','D','0','4','F','E','0','-','3','A','E','A','-','1','0','6','9','-',
898 'A','2','D','8','-','0','8','0','0','2','B','3','0','3','0','9','D','}',0 };
899 WCHAR wszFileName[MAX_PATH];
900 LPITEMIDLIST pidlTestFile;
901 HANDLE hTestFile;
902 STRRET strret;
903 static WCHAR wszTestFile[] = {
904 'w','i','n','e','t','e','s','t','.','f','o','o',0 };
905 HRESULT (WINAPI *pSHGetSpecialFolderLocation)(HWND, int, LPITEMIDLIST *);
906 HMODULE hShell32;
907 LPITEMIDLIST pidlPrograms;
909 if(!pSHGetPathFromIDListW || !pSHGetSpecialFolderPathW)
911 win_skip("SHGetPathFromIDListW() or SHGetSpecialFolderPathW() is missing\n");
912 return;
915 /* Calling SHGetPathFromIDListW with no pidl should return the empty string */
916 wszPath[0] = 'a';
917 wszPath[1] = '\0';
918 result = pSHGetPathFromIDListW(NULL, wszPath);
919 ok(!result, "Expected failure\n");
920 ok(!wszPath[0], "Expected empty string\n");
922 /* Calling SHGetPathFromIDListW with an empty pidl should return the desktop folder's path. */
923 result = pSHGetSpecialFolderPathW(NULL, wszDesktop, CSIDL_DESKTOP, FALSE);
924 ok(result, "SHGetSpecialFolderPathW(CSIDL_DESKTOP) failed! Last error: %u\n", GetLastError());
925 if (!result) return;
927 /* Check if we are on Win9x */
928 SetLastError(0xdeadbeef);
929 lstrcmpiW(wszDesktop, wszDesktop);
930 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
932 win_skip("Most W-calls are not implemented\n");
933 return;
936 result = pSHGetPathFromIDListW(pidlEmpty, wszPath);
937 ok(result, "SHGetPathFromIDListW failed! Last error: %u\n", GetLastError());
938 if (!result) return;
939 ok(!lstrcmpiW(wszDesktop, wszPath), "SHGetPathFromIDListW didn't return desktop path for empty pidl!\n");
941 /* MyComputer does not map to a filesystem path. SHGetPathFromIDListW should fail. */
942 hr = SHGetDesktopFolder(&psfDesktop);
943 ok (SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08x\n", hr);
944 if (FAILED(hr)) return;
946 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszMyComputer, NULL, &pidlMyComputer, NULL);
947 ok (SUCCEEDED(hr), "Desktop's ParseDisplayName failed to parse MyComputer's CLSID! hr = %08x\n", hr);
948 if (FAILED(hr)) {
949 IShellFolder_Release(psfDesktop);
950 return;
953 SetLastError(0xdeadbeef);
954 wszPath[0] = 'a';
955 wszPath[1] = '\0';
956 result = pSHGetPathFromIDListW(pidlMyComputer, wszPath);
957 ok (!result, "SHGetPathFromIDListW succeeded where it shouldn't!\n");
958 ok (GetLastError()==0xdeadbeef, "SHGetPathFromIDListW shouldn't set last error! Last error: %u\n", GetLastError());
959 ok (!wszPath[0], "Expected empty path\n");
960 if (result) {
961 IShellFolder_Release(psfDesktop);
962 return;
965 IMalloc_Free(ppM, pidlMyComputer);
967 result = pSHGetSpecialFolderPathW(NULL, wszFileName, CSIDL_DESKTOPDIRECTORY, FALSE);
968 ok(result, "SHGetSpecialFolderPathW failed! Last error: %u\n", GetLastError());
969 if (!result) {
970 IShellFolder_Release(psfDesktop);
971 return;
973 myPathAddBackslashW(wszFileName);
974 lstrcatW(wszFileName, wszTestFile);
975 hTestFile = CreateFileW(wszFileName, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, NULL);
976 ok(hTestFile != INVALID_HANDLE_VALUE, "CreateFileW failed! Last error: %u\n", GetLastError());
977 if (hTestFile == INVALID_HANDLE_VALUE) {
978 IShellFolder_Release(psfDesktop);
979 return;
981 CloseHandle(hTestFile);
983 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszTestFile, NULL, &pidlTestFile, NULL);
984 ok (SUCCEEDED(hr), "Desktop's ParseDisplayName failed to parse filename hr = %08x\n", hr);
985 if (FAILED(hr)) {
986 IShellFolder_Release(psfDesktop);
987 DeleteFileW(wszFileName);
988 IMalloc_Free(ppM, pidlTestFile);
989 return;
992 /* This test is to show that the Desktop shellfolder prepends the CSIDL_DESKTOPDIRECTORY
993 * path for files placed on the desktop, if called with SHGDN_FORPARSING. */
994 hr = IShellFolder_GetDisplayNameOf(psfDesktop, pidlTestFile, SHGDN_FORPARSING, &strret);
995 ok (SUCCEEDED(hr), "Desktop's GetDisplayNamfOf failed! hr = %08x\n", hr);
996 IShellFolder_Release(psfDesktop);
997 DeleteFileW(wszFileName);
998 if (FAILED(hr)) {
999 IMalloc_Free(ppM, pidlTestFile);
1000 return;
1002 if (pStrRetToBufW)
1004 pStrRetToBufW(&strret, pidlTestFile, wszPath, MAX_PATH);
1005 ok(0 == lstrcmpW(wszFileName, wszPath),
1006 "Desktop->GetDisplayNameOf(pidlTestFile, SHGDN_FORPARSING) "
1007 "returned incorrect path for file placed on desktop\n");
1010 result = pSHGetPathFromIDListW(pidlTestFile, wszPath);
1011 ok(result, "SHGetPathFromIDListW failed! Last error: %u\n", GetLastError());
1012 IMalloc_Free(ppM, pidlTestFile);
1013 if (!result) return;
1014 ok(0 == lstrcmpW(wszFileName, wszPath), "SHGetPathFromIDListW returned incorrect path for file placed on desktop\n");
1017 /* Test if we can get the path from the start menu "program files" PIDL. */
1018 hShell32 = GetModuleHandleA("shell32");
1019 pSHGetSpecialFolderLocation = (void *)GetProcAddress(hShell32, "SHGetSpecialFolderLocation");
1021 hr = pSHGetSpecialFolderLocation(NULL, CSIDL_PROGRAM_FILES, &pidlPrograms);
1022 ok(SUCCEEDED(hr), "SHGetFolderLocation failed: 0x%08x\n", hr);
1024 SetLastError(0xdeadbeef);
1025 result = pSHGetPathFromIDListW(pidlPrograms, wszPath);
1026 IMalloc_Free(ppM, pidlPrograms);
1027 ok(result, "SHGetPathFromIDListW failed\n");
1030 static void test_EnumObjects_and_CompareIDs(void)
1032 ITEMIDLIST *newPIDL;
1033 IShellFolder *IDesktopFolder, *testIShellFolder;
1034 char cCurrDirA [MAX_PATH] = {0};
1035 static const CHAR cTestDirA[] = "\\testdir";
1036 WCHAR cTestDirW[MAX_PATH];
1037 int len;
1038 HRESULT hr;
1040 GetCurrentDirectoryA(MAX_PATH, cCurrDirA);
1041 len = lstrlenA(cCurrDirA);
1043 if(len == 0) {
1044 win_skip("GetCurrentDirectoryA returned empty string. Skipping test_EnumObjects_and_CompareIDs\n");
1045 return;
1047 if(cCurrDirA[len-1] == '\\')
1048 cCurrDirA[len-1] = 0;
1050 lstrcatA(cCurrDirA, cTestDirA);
1051 MultiByteToWideChar(CP_ACP, 0, cCurrDirA, -1, cTestDirW, MAX_PATH);
1053 hr = SHGetDesktopFolder(&IDesktopFolder);
1054 ok(hr == S_OK, "SHGetDesktopfolder failed %08x\n", hr);
1056 CreateFilesFolders();
1058 hr = IShellFolder_ParseDisplayName(IDesktopFolder, NULL, NULL, cTestDirW, NULL, &newPIDL, 0);
1059 ok(hr == S_OK, "ParseDisplayName failed %08x\n", hr);
1061 hr = IShellFolder_BindToObject(IDesktopFolder, newPIDL, NULL, (REFIID)&IID_IShellFolder, (LPVOID *)&testIShellFolder);
1062 ok(hr == S_OK, "BindToObject failed %08x\n", hr);
1064 test_EnumObjects(testIShellFolder);
1066 IShellFolder_Release(testIShellFolder);
1068 Cleanup();
1070 IMalloc_Free(ppM, newPIDL);
1072 IShellFolder_Release(IDesktopFolder);
1075 /* A simple implementation of an IPropertyBag, which returns fixed values for
1076 * 'Target' and 'Attributes' properties.
1078 static HRESULT WINAPI InitPropertyBag_IPropertyBag_QueryInterface(IPropertyBag *iface, REFIID riid,
1079 void **ppvObject)
1081 if (!ppvObject)
1082 return E_INVALIDARG;
1084 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IPropertyBag, riid)) {
1085 *ppvObject = iface;
1086 } else {
1087 ok (FALSE, "InitPropertyBag asked for unknown interface!\n");
1088 return E_NOINTERFACE;
1091 IPropertyBag_AddRef(iface);
1092 return S_OK;
1095 static ULONG WINAPI InitPropertyBag_IPropertyBag_AddRef(IPropertyBag *iface) {
1096 return 2;
1099 static ULONG WINAPI InitPropertyBag_IPropertyBag_Release(IPropertyBag *iface) {
1100 return 1;
1103 static HRESULT WINAPI InitPropertyBag_IPropertyBag_Read(IPropertyBag *iface, LPCOLESTR pszPropName,
1104 VARIANT *pVar, IErrorLog *pErrorLog)
1106 static const WCHAR wszTargetSpecialFolder[] = {
1107 'T','a','r','g','e','t','S','p','e','c','i','a','l','F','o','l','d','e','r',0 };
1108 static const WCHAR wszTarget[] = {
1109 'T','a','r','g','e','t',0 };
1110 static const WCHAR wszAttributes[] = {
1111 'A','t','t','r','i','b','u','t','e','s',0 };
1112 static const WCHAR wszResolveLinkFlags[] = {
1113 'R','e','s','o','l','v','e','L','i','n','k','F','l','a','g','s',0 };
1115 if (!lstrcmpW(pszPropName, wszTargetSpecialFolder)) {
1116 ok(V_VT(pVar) == VT_I4 ||
1117 broken(V_VT(pVar) == VT_BSTR), /* Win2k */
1118 "Wrong variant type for 'TargetSpecialFolder' property!\n");
1119 return E_INVALIDARG;
1122 if (!lstrcmpW(pszPropName, wszResolveLinkFlags))
1124 ok(V_VT(pVar) == VT_UI4, "Wrong variant type for 'ResolveLinkFlags' property!\n");
1125 return E_INVALIDARG;
1128 if (!lstrcmpW(pszPropName, wszTarget)) {
1129 WCHAR wszPath[MAX_PATH];
1130 BOOL result;
1132 ok(V_VT(pVar) == VT_BSTR ||
1133 broken(V_VT(pVar) == VT_EMPTY), /* Win2k */
1134 "Wrong variant type for 'Target' property!\n");
1135 if (V_VT(pVar) != VT_BSTR) return E_INVALIDARG;
1137 result = pSHGetSpecialFolderPathW(NULL, wszPath, CSIDL_DESKTOPDIRECTORY, FALSE);
1138 ok(result, "SHGetSpecialFolderPathW(DESKTOPDIRECTORY) failed! %u\n", GetLastError());
1139 if (!result) return E_INVALIDARG;
1141 V_BSTR(pVar) = SysAllocString(wszPath);
1142 return S_OK;
1145 if (!lstrcmpW(pszPropName, wszAttributes)) {
1146 ok(V_VT(pVar) == VT_UI4, "Wrong variant type for 'Attributes' property!\n");
1147 if (V_VT(pVar) != VT_UI4) return E_INVALIDARG;
1148 V_UI4(pVar) = SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR|
1149 SFGAO_CANRENAME|SFGAO_FILESYSTEM;
1150 return S_OK;
1153 ok(FALSE, "PropertyBag was asked for unknown property (vt=%d)!\n", V_VT(pVar));
1154 return E_INVALIDARG;
1157 static HRESULT WINAPI InitPropertyBag_IPropertyBag_Write(IPropertyBag *iface, LPCOLESTR pszPropName,
1158 VARIANT *pVar)
1160 ok(FALSE, "Unexpected call to IPropertyBag_Write\n");
1161 return E_NOTIMPL;
1164 static const IPropertyBagVtbl InitPropertyBag_IPropertyBagVtbl = {
1165 InitPropertyBag_IPropertyBag_QueryInterface,
1166 InitPropertyBag_IPropertyBag_AddRef,
1167 InitPropertyBag_IPropertyBag_Release,
1168 InitPropertyBag_IPropertyBag_Read,
1169 InitPropertyBag_IPropertyBag_Write
1172 static struct IPropertyBag InitPropertyBag = {
1173 &InitPropertyBag_IPropertyBagVtbl
1176 static void test_FolderShortcut(void) {
1177 IPersistPropertyBag *pPersistPropertyBag;
1178 IShellFolder *pShellFolder, *pDesktopFolder;
1179 IPersistFolder3 *pPersistFolder3;
1180 HRESULT hr;
1181 STRRET strret;
1182 WCHAR wszDesktopPath[MAX_PATH], wszBuffer[MAX_PATH];
1183 BOOL result;
1184 CLSID clsid;
1185 LPITEMIDLIST pidlCurrentFolder, pidlWineTestFolder, pidlSubFolder;
1186 HKEY hShellExtKey;
1187 WCHAR wszWineTestFolder[] = {
1188 ':',':','{','9','B','3','5','2','E','B','F','-','2','7','6','5','-','4','5','C','1','-',
1189 'B','4','C','6','-','8','5','C','C','7','F','7','A','B','C','6','4','}',0 };
1190 WCHAR wszShellExtKey[] = { 'S','o','f','t','w','a','r','e','\\',
1191 'M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\',
1192 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
1193 'E','x','p','l','o','r','e','r','\\','D','e','s','k','t','o','p','\\',
1194 'N','a','m','e','S','p','a','c','e','\\',
1195 '{','9','b','3','5','2','e','b','f','-','2','7','6','5','-','4','5','c','1','-',
1196 'b','4','c','6','-','8','5','c','c','7','f','7','a','b','c','6','4','}',0 };
1198 WCHAR wszSomeSubFolder[] = { 'S','u','b','F','o','l','d','e','r', 0};
1199 static const GUID CLSID_UnixDosFolder =
1200 {0x9d20aae8, 0x0625, 0x44b0, {0x9c, 0xa7, 0x71, 0x88, 0x9c, 0x22, 0x54, 0xd9}};
1202 if (!pSHGetSpecialFolderPathW || !pStrRetToBufW) {
1203 win_skip("SHGetSpecialFolderPathW and/or StrRetToBufW are not available\n");
1204 return;
1207 if (!pSHGetFolderPathAndSubDirA)
1209 win_skip("FolderShortcut test doesn't work on Win2k\n");
1210 return;
1213 /* These tests basically show, that CLSID_FolderShortcuts are initialized
1214 * via their IPersistPropertyBag interface. And that the target folder
1215 * is taken from the IPropertyBag's 'Target' property.
1217 hr = CoCreateInstance(&CLSID_FolderShortcut, NULL, CLSCTX_INPROC_SERVER,
1218 &IID_IPersistPropertyBag, (LPVOID*)&pPersistPropertyBag);
1219 if (hr == REGDB_E_CLASSNOTREG) {
1220 win_skip("CLSID_FolderShortcut is not implemented\n");
1221 return;
1223 ok (SUCCEEDED(hr), "CoCreateInstance failed! hr = 0x%08x\n", hr);
1224 if (FAILED(hr)) return;
1226 hr = IPersistPropertyBag_Load(pPersistPropertyBag, &InitPropertyBag, NULL);
1227 ok(SUCCEEDED(hr), "IPersistPropertyBag_Load failed! hr = %08x\n", hr);
1228 if (FAILED(hr)) {
1229 IPersistPropertyBag_Release(pPersistPropertyBag);
1230 return;
1233 hr = IPersistPropertyBag_QueryInterface(pPersistPropertyBag, &IID_IShellFolder,
1234 (LPVOID*)&pShellFolder);
1235 IPersistPropertyBag_Release(pPersistPropertyBag);
1236 ok(SUCCEEDED(hr), "IPersistPropertyBag_QueryInterface(IShellFolder) failed! hr = %08x\n", hr);
1237 if (FAILED(hr)) return;
1239 hr = IShellFolder_GetDisplayNameOf(pShellFolder, NULL, SHGDN_FORPARSING, &strret);
1240 ok(SUCCEEDED(hr), "IShellFolder_GetDisplayNameOf(NULL) failed! hr = %08x\n", hr);
1241 if (FAILED(hr)) {
1242 IShellFolder_Release(pShellFolder);
1243 return;
1246 result = pSHGetSpecialFolderPathW(NULL, wszDesktopPath, CSIDL_DESKTOPDIRECTORY, FALSE);
1247 ok(result, "SHGetSpecialFolderPathW(CSIDL_DESKTOPDIRECTORY) failed! %u\n", GetLastError());
1248 if (!result) return;
1250 pStrRetToBufW(&strret, NULL, wszBuffer, MAX_PATH);
1251 ok(!lstrcmpiW(wszDesktopPath, wszBuffer), "FolderShortcut returned incorrect folder!\n");
1253 hr = IShellFolder_QueryInterface(pShellFolder, &IID_IPersistFolder3, (LPVOID*)&pPersistFolder3);
1254 IShellFolder_Release(pShellFolder);
1255 ok(SUCCEEDED(hr), "IShellFolder_QueryInterface(IID_IPersistFolder3 failed! hr = 0x%08x\n", hr);
1256 if (FAILED(hr)) return;
1258 hr = IPersistFolder3_GetClassID(pPersistFolder3, &clsid);
1259 ok(SUCCEEDED(hr), "IPersistFolder3_GetClassID failed! hr=0x%08x\n", hr);
1260 ok(IsEqualCLSID(&clsid, &CLSID_FolderShortcut), "Unexpected CLSID!\n");
1262 hr = IPersistFolder3_GetCurFolder(pPersistFolder3, &pidlCurrentFolder);
1263 ok(SUCCEEDED(hr), "IPersistFolder3_GetCurFolder failed! hr=0x%08x\n", hr);
1264 ok(!pidlCurrentFolder, "IPersistFolder3_GetCurFolder should return a NULL pidl!\n");
1266 /* For FolderShortcut objects, the Initialize method initialized the folder's position in the
1267 * shell namespace. The target folder, read from the property bag above, remains untouched.
1268 * The following tests show this: The itemidlist for some imaginary shellfolder object
1269 * is created and the FolderShortcut is initialized with it. GetCurFolder now returns this
1270 * itemidlist, but GetDisplayNameOf still returns the path from above.
1272 hr = SHGetDesktopFolder(&pDesktopFolder);
1273 ok (SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08x\n", hr);
1274 if (FAILED(hr)) return;
1276 /* Temporarily register WineTestFolder as a shell namespace extension at the Desktop.
1277 * Otherwise ParseDisplayName fails on WinXP with E_INVALIDARG */
1278 RegCreateKeyW(HKEY_CURRENT_USER, wszShellExtKey, &hShellExtKey);
1279 RegCloseKey(hShellExtKey);
1280 hr = IShellFolder_ParseDisplayName(pDesktopFolder, NULL, NULL, wszWineTestFolder, NULL,
1281 &pidlWineTestFolder, NULL);
1282 RegDeleteKeyW(HKEY_CURRENT_USER, wszShellExtKey);
1283 IShellFolder_Release(pDesktopFolder);
1284 ok (SUCCEEDED(hr), "IShellFolder::ParseDisplayName failed! hr = %08x\n", hr);
1285 if (FAILED(hr)) return;
1287 hr = IPersistFolder3_Initialize(pPersistFolder3, pidlWineTestFolder);
1288 ok (SUCCEEDED(hr), "IPersistFolder3::Initialize failed! hr = %08x\n", hr);
1289 if (FAILED(hr)) {
1290 IPersistFolder3_Release(pPersistFolder3);
1291 pILFree(pidlWineTestFolder);
1292 return;
1295 hr = IPersistFolder3_GetCurFolder(pPersistFolder3, &pidlCurrentFolder);
1296 ok(SUCCEEDED(hr), "IPersistFolder3_GetCurFolder failed! hr=0x%08x\n", hr);
1297 ok(pILIsEqual(pidlCurrentFolder, pidlWineTestFolder),
1298 "IPersistFolder3_GetCurFolder should return pidlWineTestFolder!\n");
1299 pILFree(pidlCurrentFolder);
1300 pILFree(pidlWineTestFolder);
1302 hr = IPersistFolder3_QueryInterface(pPersistFolder3, &IID_IShellFolder, (LPVOID*)&pShellFolder);
1303 IPersistFolder3_Release(pPersistFolder3);
1304 ok(SUCCEEDED(hr), "IPersistFolder3_QueryInterface(IShellFolder) failed! hr = %08x\n", hr);
1305 if (FAILED(hr)) return;
1307 hr = IShellFolder_GetDisplayNameOf(pShellFolder, NULL, SHGDN_FORPARSING, &strret);
1308 ok(SUCCEEDED(hr), "IShellFolder_GetDisplayNameOf(NULL) failed! hr = %08x\n", hr);
1309 if (FAILED(hr)) {
1310 IShellFolder_Release(pShellFolder);
1311 return;
1314 pStrRetToBufW(&strret, NULL, wszBuffer, MAX_PATH);
1315 ok(!lstrcmpiW(wszDesktopPath, wszBuffer), "FolderShortcut returned incorrect folder!\n");
1317 /* Next few lines are meant to show that children of FolderShortcuts are not FolderShortcuts,
1318 * but ShellFSFolders. */
1319 myPathAddBackslashW(wszDesktopPath);
1320 lstrcatW(wszDesktopPath, wszSomeSubFolder);
1321 if (!CreateDirectoryW(wszDesktopPath, NULL)) {
1322 IShellFolder_Release(pShellFolder);
1323 return;
1326 hr = IShellFolder_ParseDisplayName(pShellFolder, NULL, NULL, wszSomeSubFolder, NULL,
1327 &pidlSubFolder, NULL);
1328 RemoveDirectoryW(wszDesktopPath);
1329 ok (SUCCEEDED(hr), "IShellFolder::ParseDisplayName failed! hr = %08x\n", hr);
1330 if (FAILED(hr)) {
1331 IShellFolder_Release(pShellFolder);
1332 return;
1335 hr = IShellFolder_BindToObject(pShellFolder, pidlSubFolder, NULL, &IID_IPersistFolder3,
1336 (LPVOID*)&pPersistFolder3);
1337 IShellFolder_Release(pShellFolder);
1338 pILFree(pidlSubFolder);
1339 ok (SUCCEEDED(hr), "IShellFolder::BindToObject failed! hr = %08x\n", hr);
1340 if (FAILED(hr))
1341 return;
1343 /* On windows, we expect CLSID_ShellFSFolder. On wine we relax this constraint
1344 * a little bit and also allow CLSID_UnixDosFolder. */
1345 hr = IPersistFolder3_GetClassID(pPersistFolder3, &clsid);
1346 ok(SUCCEEDED(hr), "IPersistFolder3_GetClassID failed! hr=0x%08x\n", hr);
1347 ok(IsEqualCLSID(&clsid, &CLSID_ShellFSFolder) || IsEqualCLSID(&clsid, &CLSID_UnixDosFolder),
1348 "IPersistFolder3::GetClassID returned unexpected CLSID!\n");
1350 IPersistFolder3_Release(pPersistFolder3);
1353 #include "pshpack1.h"
1354 struct FileStructA {
1355 BYTE type;
1356 BYTE dummy;
1357 DWORD dwFileSize;
1358 WORD uFileDate; /* In our current implementation this is */
1359 WORD uFileTime; /* FileTimeToDosDate(WIN32_FIND_DATA->ftLastWriteTime) */
1360 WORD uFileAttribs;
1361 CHAR szName[1];
1364 struct FileStructW {
1365 WORD cbLen; /* Length of this element. */
1366 BYTE abFooBar1[6]; /* Beyond any recognition. */
1367 WORD uDate; /* FileTimeToDosDate(WIN32_FIND_DATA->ftCreationTime)? */
1368 WORD uTime; /* (this is currently speculation) */
1369 WORD uDate2; /* FileTimeToDosDate(WIN32_FIND_DATA->ftLastAccessTime)? */
1370 WORD uTime2; /* (this is currently speculation) */
1371 BYTE abFooBar2[4]; /* Beyond any recognition. */
1372 WCHAR wszName[1]; /* The long filename in unicode. */
1373 /* Just for documentation: Right after the unicode string: */
1374 WORD cbOffset; /* FileStructW's offset from the beginning of the SHITMEID.
1375 * SHITEMID->cb == uOffset + cbLen */
1377 #include "poppack.h"
1379 static void test_ITEMIDLIST_format(void) {
1380 WCHAR wszPersonal[MAX_PATH];
1381 LPSHELLFOLDER psfDesktop, psfPersonal;
1382 LPITEMIDLIST pidlPersonal, pidlFile;
1383 HANDLE hFile;
1384 HRESULT hr;
1385 BOOL bResult;
1386 WCHAR wszFile[3][17] = { { 'e','v','e','n','_',0 }, { 'o','d','d','_',0 },
1387 { 'l','o','n','g','e','r','_','t','h','a','n','.','8','_','3',0 } };
1388 int i;
1390 if (!pSHGetSpecialFolderPathW) return;
1392 bResult = pSHGetSpecialFolderPathW(NULL, wszPersonal, CSIDL_PERSONAL, FALSE);
1393 ok(bResult, "SHGetSpecialFolderPathW failed! Last error: %u\n", GetLastError());
1394 if (!bResult) return;
1396 SetLastError(0xdeadbeef);
1397 bResult = SetCurrentDirectoryW(wszPersonal);
1398 if (!bResult && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) {
1399 win_skip("Most W-calls are not implemented\n");
1400 return;
1402 ok(bResult, "SetCurrentDirectory failed! Last error: %u\n", GetLastError());
1403 if (!bResult) return;
1405 hr = SHGetDesktopFolder(&psfDesktop);
1406 ok(SUCCEEDED(hr), "SHGetDesktopFolder failed! hr: %08x\n", hr);
1407 if (FAILED(hr)) return;
1409 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszPersonal, NULL, &pidlPersonal, NULL);
1410 ok(SUCCEEDED(hr), "psfDesktop->ParseDisplayName failed! hr = %08x\n", hr);
1411 if (FAILED(hr)) {
1412 IShellFolder_Release(psfDesktop);
1413 return;
1416 hr = IShellFolder_BindToObject(psfDesktop, pidlPersonal, NULL, &IID_IShellFolder,
1417 (LPVOID*)&psfPersonal);
1418 IShellFolder_Release(psfDesktop);
1419 pILFree(pidlPersonal);
1420 ok(SUCCEEDED(hr), "psfDesktop->BindToObject failed! hr = %08x\n", hr);
1421 if (FAILED(hr)) return;
1423 for (i=0; i<3; i++) {
1424 CHAR szFile[MAX_PATH];
1425 struct FileStructA *pFileStructA;
1426 WORD cbOffset;
1428 WideCharToMultiByte(CP_ACP, 0, wszFile[i], -1, szFile, MAX_PATH, NULL, NULL);
1430 hFile = CreateFileW(wszFile[i], GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_FLAG_WRITE_THROUGH, NULL);
1431 ok(hFile != INVALID_HANDLE_VALUE, "CreateFile failed! (%u)\n", GetLastError());
1432 if (hFile == INVALID_HANDLE_VALUE) {
1433 IShellFolder_Release(psfPersonal);
1434 return;
1436 CloseHandle(hFile);
1438 hr = IShellFolder_ParseDisplayName(psfPersonal, NULL, NULL, wszFile[i], NULL, &pidlFile, NULL);
1439 DeleteFileW(wszFile[i]);
1440 ok(SUCCEEDED(hr), "psfPersonal->ParseDisplayName failed! hr: %08x\n", hr);
1441 if (FAILED(hr)) {
1442 IShellFolder_Release(psfPersonal);
1443 return;
1446 pFileStructA = (struct FileStructA *)pidlFile->mkid.abID;
1447 ok(pFileStructA->type == 0x32, "PIDLTYPE should be 0x32!\n");
1448 ok(pFileStructA->dummy == 0x00, "Dummy Byte should be 0x00!\n");
1449 ok(pFileStructA->dwFileSize == 0, "Filesize should be zero!\n");
1451 if (i < 2) /* First two file names are already in valid 8.3 format */
1452 ok(!strcmp(szFile, (CHAR*)&pidlFile->mkid.abID[12]), "Wrong file name!\n");
1453 else
1454 /* WinXP stores a derived 8.3 dos name (LONGER~1.8_3) here. We probably
1455 * can't implement this correctly, since unix filesystems don't support
1456 * this nasty short/long filename stuff. So we'll probably stay with our
1457 * current habbit of storing the long filename here, which seems to work
1458 * just fine. */
1459 todo_wine
1460 ok(pidlFile->mkid.abID[18] == '~' ||
1461 broken(pidlFile->mkid.abID[34] == '~'), /* Win2k */
1462 "Should be derived 8.3 name!\n");
1464 if (i == 0) /* First file name has an even number of chars. No need for alignment. */
1465 ok(pidlFile->mkid.abID[12 + strlen(szFile) + 1] != '\0' ||
1466 broken(pidlFile->mkid.cb == 2 + 12 + strlen(szFile) + 1 + 1), /* Win2k */
1467 "Alignment byte, where there shouldn't be!\n");
1469 if (i == 1) /* Second file name has an uneven number of chars => alignment byte */
1470 ok(pidlFile->mkid.abID[12 + strlen(szFile) + 1] == '\0',
1471 "There should be an alignment byte, but isn't!\n");
1473 /* The offset of the FileStructW member is stored as a WORD at the end of the pidl. */
1474 cbOffset = *(WORD*)(((LPBYTE)pidlFile)+pidlFile->mkid.cb-sizeof(WORD));
1475 ok ((cbOffset >= sizeof(struct FileStructA) &&
1476 cbOffset <= pidlFile->mkid.cb - sizeof(struct FileStructW)) ||
1477 broken(pidlFile->mkid.cb == 2 + 12 + strlen(szFile) + 1 + 1) || /* Win2k on short names */
1478 broken(pidlFile->mkid.cb == 2 + 12 + strlen(szFile) + 1 + 12 + 1), /* Win2k on long names */
1479 "Wrong offset value (%d) stored at the end of the PIDL\n", cbOffset);
1481 if (cbOffset >= sizeof(struct FileStructA) &&
1482 cbOffset <= pidlFile->mkid.cb - sizeof(struct FileStructW))
1484 struct FileStructW *pFileStructW = (struct FileStructW *)(((LPBYTE)pidlFile)+cbOffset);
1486 ok(pidlFile->mkid.cb == cbOffset + pFileStructW->cbLen,
1487 "FileStructW's offset and length should add up to the PIDL's length!\n");
1489 if (pidlFile->mkid.cb == cbOffset + pFileStructW->cbLen) {
1490 /* Since we just created the file, time of creation,
1491 * time of last access and time of last write access just be the same.
1492 * These tests seem to fail sometimes (on WinXP), if the test is run again shortly
1493 * after the first run. I do remember something with NTFS keeping the creation time
1494 * if a file is deleted and then created again within a couple of seconds or so.
1495 * Might be the reason. */
1496 ok (pFileStructA->uFileDate == pFileStructW->uDate &&
1497 pFileStructA->uFileTime == pFileStructW->uTime,
1498 "Last write time should match creation time!\n");
1500 ok (pFileStructA->uFileDate == pFileStructW->uDate2 &&
1501 pFileStructA->uFileTime == pFileStructW->uTime2,
1502 "Last write time should match last access time!\n");
1504 ok (!lstrcmpW(wszFile[i], pFileStructW->wszName) ||
1505 !lstrcmpW(wszFile[i], (WCHAR *)(pFileStructW->abFooBar2 + 22)), /* Vista */
1506 "The filename should be stored in unicode at this position!\n");
1510 pILFree(pidlFile);
1514 static void testSHGetFolderPathAndSubDirA(void)
1516 HRESULT ret;
1517 BOOL delret;
1518 DWORD dwret;
1519 int i;
1520 static char wine[] = "wine";
1521 static char winetemp[] = "wine\\temp";
1522 static char appdata[MAX_PATH];
1523 static char testpath[MAX_PATH];
1524 static char toolongpath[MAX_PATH+1];
1526 if(!pSHGetFolderPathA) {
1527 skip("SHGetFolderPathA not present!\n");
1528 return;
1530 if(FAILED(pSHGetFolderPathA(NULL, CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, appdata)))
1532 skip("SHGetFolderPathA failed for CSIDL_LOCAL_APPDATA!\n");
1533 return;
1536 sprintf(testpath, "%s\\%s", appdata, winetemp);
1537 delret = RemoveDirectoryA(testpath);
1538 if(!delret && (ERROR_PATH_NOT_FOUND != GetLastError()) ) {
1539 skip("RemoveDirectoryA(%s) failed with error %u\n", testpath, GetLastError());
1540 return;
1543 sprintf(testpath, "%s\\%s", appdata, wine);
1544 delret = RemoveDirectoryA(testpath);
1545 if(!delret && (ERROR_PATH_NOT_FOUND != GetLastError()) && (ERROR_FILE_NOT_FOUND != GetLastError())) {
1546 skip("RemoveDirectoryA(%s) failed with error %u\n", testpath, GetLastError());
1547 return;
1549 for(i=0; i< MAX_PATH; i++)
1550 toolongpath[i] = '0' + i % 10;
1551 toolongpath[MAX_PATH] = '\0';
1553 /* test invalid second parameter */
1554 ret = pSHGetFolderPathAndSubDirA(NULL, CSIDL_FLAG_DONT_VERIFY | 0xff, NULL, SHGFP_TYPE_CURRENT, wine, testpath);
1555 ok(E_INVALIDARG == ret, "expected E_INVALIDARG, got %x\n", ret);
1557 /* test invalid forth parameter */
1558 ret = pSHGetFolderPathAndSubDirA(NULL, CSIDL_FLAG_DONT_VERIFY | CSIDL_LOCAL_APPDATA, NULL, 2, wine, testpath);
1559 switch(ret) {
1560 case S_OK: /* winvista */
1561 ok(!strncmp(appdata, testpath, strlen(appdata)),
1562 "expected %s to start with %s\n", testpath, appdata);
1563 ok(!lstrcmpA(&testpath[1 + strlen(appdata)], winetemp),
1564 "expected %s to end with %s\n", testpath, winetemp);
1565 break;
1566 case E_INVALIDARG: /* winxp, win2k3 */
1567 break;
1568 default:
1569 ok(0, "expected S_OK or E_INVALIDARG, got %x\n", ret);
1572 /* test fifth parameter */
1573 testpath[0] = '\0';
1574 ret = pSHGetFolderPathAndSubDirA(NULL, CSIDL_FLAG_DONT_VERIFY | CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, NULL, testpath);
1575 ok(S_OK == ret, "expected S_OK, got %x\n", ret);
1576 ok(!lstrcmpA(appdata, testpath), "expected %s, got %s\n", appdata, testpath);
1578 testpath[0] = '\0';
1579 ret = pSHGetFolderPathAndSubDirA(NULL, CSIDL_FLAG_DONT_VERIFY | CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, "", testpath);
1580 ok(S_OK == ret, "expected S_OK, got %x\n", ret);
1581 ok(!lstrcmpA(appdata, testpath), "expected %s, got %s\n", appdata, testpath);
1583 testpath[0] = '\0';
1584 ret = pSHGetFolderPathAndSubDirA(NULL, CSIDL_FLAG_DONT_VERIFY | CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, "\\", testpath);
1585 ok(S_OK == ret, "expected S_OK, got %x\n", ret);
1586 ok(!lstrcmpA(appdata, testpath), "expected %s, got %s\n", appdata, testpath);
1588 ret = pSHGetFolderPathAndSubDirA(NULL, CSIDL_FLAG_DONT_VERIFY | CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, toolongpath, testpath);
1589 ok(HRESULT_FROM_WIN32(ERROR_FILENAME_EXCED_RANGE) == ret,
1590 "expected %x, got %x\n", HRESULT_FROM_WIN32(ERROR_FILENAME_EXCED_RANGE), ret);
1592 testpath[0] = '\0';
1593 ret = pSHGetFolderPathAndSubDirA(NULL, CSIDL_FLAG_DONT_VERIFY | CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, wine, NULL);
1594 ok((S_OK == ret) || (E_INVALIDARG == ret), "expected S_OK or E_INVALIDARG, got %x\n", ret);
1596 /* test a not existing path */
1597 testpath[0] = '\0';
1598 ret = pSHGetFolderPathAndSubDirA(NULL, CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, winetemp, testpath);
1599 ok(HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND) == ret,
1600 "expected %x, got %x\n", HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND), ret);
1602 /* create a directory inside a not existing directory */
1603 testpath[0] = '\0';
1604 ret = pSHGetFolderPathAndSubDirA(NULL, CSIDL_FLAG_CREATE | CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, winetemp, testpath);
1605 ok(S_OK == ret, "expected S_OK, got %x\n", ret);
1606 ok(!strncmp(appdata, testpath, strlen(appdata)),
1607 "expected %s to start with %s\n", testpath, appdata);
1608 ok(!lstrcmpA(&testpath[1 + strlen(appdata)], winetemp),
1609 "expected %s to end with %s\n", testpath, winetemp);
1610 dwret = GetFileAttributes(testpath);
1611 ok(FILE_ATTRIBUTE_DIRECTORY | dwret, "expected %x to contain FILE_ATTRIBUTE_DIRECTORY\n", dwret);
1613 /* cleanup */
1614 sprintf(testpath, "%s\\%s", appdata, winetemp);
1615 RemoveDirectoryA(testpath);
1616 sprintf(testpath, "%s\\%s", appdata, wine);
1617 RemoveDirectoryA(testpath);
1620 static const char *wine_dbgstr_w(LPCWSTR str)
1622 static char buf[512];
1623 if (!str)
1624 return "(null)";
1625 WideCharToMultiByte(CP_ACP, 0, str, -1, buf, sizeof(buf), NULL, NULL);
1626 return buf;
1629 static void test_LocalizedNames(void)
1631 static char cCurrDirA[MAX_PATH];
1632 WCHAR cCurrDirW[MAX_PATH], tempbufW[25];
1633 IShellFolder *IDesktopFolder, *testIShellFolder;
1634 ITEMIDLIST *newPIDL;
1635 int len;
1636 HRESULT hr;
1637 static char resourcefile[MAX_PATH];
1638 DWORD res;
1639 HANDLE file;
1640 STRRET strret;
1642 static const char desktopini_contents1[] =
1643 "[.ShellClassInfo]\r\n"
1644 "LocalizedResourceName=@";
1645 static const char desktopini_contents2[] =
1646 ",-1\r\n";
1647 static WCHAR foldernameW[] = {'t','e','s','t','f','o','l','d','e','r',0};
1648 static const WCHAR folderdisplayW[] = {'F','o','l','d','e','r',' ','N','a','m','e',' ','R','e','s','o','u','r','c','e',0};
1650 /* create folder with desktop.ini and localized name in GetModuleFileNameA(NULL) */
1651 CreateDirectoryA(".\\testfolder", NULL);
1653 SetFileAttributesA(".\\testfolder", GetFileAttributesA(".\\testfolder")|FILE_ATTRIBUTE_SYSTEM);
1655 GetModuleFileNameA(NULL, resourcefile, MAX_PATH);
1657 file = CreateFileA(".\\testfolder\\desktop.ini", GENERIC_WRITE, 0, NULL,
1658 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1659 ok(file != INVALID_HANDLE_VALUE, "CreateFileA failed %i\n", GetLastError());
1660 ok(WriteFile(file, desktopini_contents1, strlen(desktopini_contents1), &res, NULL) &&
1661 WriteFile(file, resourcefile, strlen(resourcefile), &res, NULL) &&
1662 WriteFile(file, desktopini_contents2, strlen(desktopini_contents2), &res, NULL),
1663 "WriteFile failed %i\n", GetLastError());
1664 CloseHandle(file);
1666 /* get IShellFolder for parent */
1667 GetCurrentDirectoryA(MAX_PATH, cCurrDirA);
1668 len = lstrlenA(cCurrDirA);
1670 if (len == 0) {
1671 trace("GetCurrentDirectoryA returned empty string. Skipping test_LocalizedNames\n");
1672 goto cleanup;
1674 if(cCurrDirA[len-1] == '\\')
1675 cCurrDirA[len-1] = 0;
1677 MultiByteToWideChar(CP_ACP, 0, cCurrDirA, -1, cCurrDirW, MAX_PATH);
1679 hr = SHGetDesktopFolder(&IDesktopFolder);
1680 ok(hr == S_OK, "SHGetDesktopfolder failed %08x\n", hr);
1682 hr = IShellFolder_ParseDisplayName(IDesktopFolder, NULL, NULL, cCurrDirW, NULL, &newPIDL, 0);
1683 ok(hr == S_OK, "ParseDisplayName failed %08x\n", hr);
1685 hr = IShellFolder_BindToObject(IDesktopFolder, newPIDL, NULL, (REFIID)&IID_IShellFolder, (LPVOID *)&testIShellFolder);
1686 ok(hr == S_OK, "BindToObject failed %08x\n", hr);
1688 IMalloc_Free(ppM, newPIDL);
1690 /* windows reads the display name from the resource */
1691 hr = IShellFolder_ParseDisplayName(testIShellFolder, NULL, NULL, foldernameW, NULL, &newPIDL, 0);
1692 ok(hr == S_OK, "ParseDisplayName failed %08x\n", hr);
1694 hr = IShellFolder_GetDisplayNameOf(testIShellFolder, newPIDL, SHGDN_INFOLDER, &strret);
1695 ok(hr == S_OK, "GetDisplayNameOf failed %08x\n", hr);
1697 if (SUCCEEDED(hr) && pStrRetToBufW)
1699 hr = pStrRetToBufW(&strret, newPIDL, tempbufW, sizeof(tempbufW)/sizeof(WCHAR));
1700 ok (SUCCEEDED(hr), "StrRetToBufW failed! hr = %08x\n", hr);
1701 todo_wine
1702 ok (!lstrcmpiW(tempbufW, folderdisplayW) ||
1703 broken(!lstrcmpiW(tempbufW, foldernameW)), /* W2K */
1704 "GetDisplayNameOf returned %s\n", wine_dbgstr_w(tempbufW));
1707 /* editing name is also read from the resource */
1708 hr = IShellFolder_GetDisplayNameOf(testIShellFolder, newPIDL, SHGDN_INFOLDER|SHGDN_FOREDITING, &strret);
1709 ok(hr == S_OK, "GetDisplayNameOf failed %08x\n", hr);
1711 if (SUCCEEDED(hr) && pStrRetToBufW)
1713 hr = pStrRetToBufW(&strret, newPIDL, tempbufW, sizeof(tempbufW)/sizeof(WCHAR));
1714 ok (SUCCEEDED(hr), "StrRetToBufW failed! hr = %08x\n", hr);
1715 todo_wine
1716 ok (!lstrcmpiW(tempbufW, folderdisplayW) ||
1717 broken(!lstrcmpiW(tempbufW, foldernameW)), /* W2K */
1718 "GetDisplayNameOf returned %s\n", wine_dbgstr_w(tempbufW));
1721 /* parsing name is unchanged */
1722 hr = IShellFolder_GetDisplayNameOf(testIShellFolder, newPIDL, SHGDN_INFOLDER|SHGDN_FORPARSING, &strret);
1723 ok(hr == S_OK, "GetDisplayNameOf failed %08x\n", hr);
1725 if (SUCCEEDED(hr) && pStrRetToBufW)
1727 hr = pStrRetToBufW(&strret, newPIDL, tempbufW, sizeof(tempbufW)/sizeof(WCHAR));
1728 ok (SUCCEEDED(hr), "StrRetToBufW failed! hr = %08x\n", hr);
1729 ok (!lstrcmpiW(tempbufW, foldernameW), "GetDisplayNameOf returned %s\n", wine_dbgstr_w(tempbufW));
1732 IShellFolder_Release(IDesktopFolder);
1733 IShellFolder_Release(testIShellFolder);
1735 IMalloc_Free(ppM, newPIDL);
1737 cleanup:
1738 DeleteFileA(".\\testfolder\\desktop.ini");
1739 SetFileAttributesA(".\\testfolder", GetFileAttributesA(".\\testfolder")&~FILE_ATTRIBUTE_SYSTEM);
1740 RemoveDirectoryA(".\\testfolder");
1744 START_TEST(shlfolder)
1746 init_function_pointers();
1747 /* if OleInitialize doesn't get called, ParseDisplayName returns
1748 CO_E_NOTINITIALIZED for malformed directory names on win2k. */
1749 OleInitialize(NULL);
1751 test_ParseDisplayName();
1752 test_BindToObject();
1753 test_EnumObjects_and_CompareIDs();
1754 test_GetDisplayName();
1755 test_GetAttributesOf();
1756 test_SHGetPathFromIDList();
1757 test_CallForAttributes();
1758 test_FolderShortcut();
1759 test_ITEMIDLIST_format();
1760 if(pSHGetFolderPathAndSubDirA)
1761 testSHGetFolderPathAndSubDirA();
1762 else
1763 skip("SHGetFolderPathAndSubDirA not present\n");
1764 test_LocalizedNames();
1766 OleUninitialize();