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