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