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