makefiles: List the static libraries as normal imports instead of extra libs.
[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     /* Some tests for IShellFolder::SetNameOf */
437     hr = pSHBindToParent(pidlTestFile, &IID_IShellFolder, (VOID**)&psfPersonal, &pidlLast);
438     ok(SUCCEEDED(hr), "SHBindToParent failed! hr = %08x\n", hr);
439     if (SUCCEEDED(hr)) {
440         /* It's ok to use this fixed path. Call will fail anyway. */
441         WCHAR wszAbsoluteFilename[] = { 'C',':','\\','w','i','n','e','t','e','s','t', 0 };
442         LPITEMIDLIST pidlNew;
443
444         /* The pidl returned through the last parameter of SetNameOf is a simple one. */
445         hr = IShellFolder_SetNameOf(psfPersonal, NULL, pidlLast, wszDirName, SHGDN_NORMAL, &pidlNew);
446         ok (SUCCEEDED(hr), "SetNameOf failed! hr = %08x\n", hr);
447         ok (((LPITEMIDLIST)((LPBYTE)pidlNew+pidlNew->mkid.cb))->mkid.cb == 0, 
448             "pidl returned from SetNameOf should be simple!\n");
449
450         /* Passing an absolute path to SetNameOf fails. The HRESULT code indicates that SetNameOf
451          * is implemented on top of SHFileOperation in WinXP. */
452         hr = IShellFolder_SetNameOf(psfPersonal, NULL, pidlNew, wszAbsoluteFilename, 
453                 SHGDN_FORPARSING, NULL);
454         ok (hr == HRESULT_FROM_WIN32(ERROR_CANCELLED), "SetNameOf succeeded! hr = %08x\n", hr);
455
456         /* Rename the file back to its original name. SetNameOf ignores the fact, that the
457          * SHGDN flags specify an absolute path. */
458         hr = IShellFolder_SetNameOf(psfPersonal, NULL, pidlNew, wszFileName, SHGDN_FORPARSING, NULL);
459         ok (SUCCEEDED(hr), "SetNameOf failed! hr = %08x\n", hr);
460
461         pILFree(pidlNew);
462         IShellFolder_Release(psfPersonal);
463     }
464
465     /* Deleting the file and the directory */
466     DeleteFileA(szTestFile);
467     RemoveDirectoryA(szTestDir);
468
469     /* SHGetPathFromIDListW still works, although the file is not present anymore. */
470     if (pSHGetPathFromIDListW)
471     {
472         result = pSHGetPathFromIDListW(pidlTestFile, wszTestFile2);
473         ok (result, "SHGetPathFromIDListW failed! Last error: %u\n", GetLastError());
474         ok (!lstrcmpiW(wszTestFile, wszTestFile2), "SHGetPathFromIDListW returns incorrect path!\n");
475     }
476
477     if(!pSHBindToParent) return;
478
479     /* SHBindToParent fails, if called with a NULL PIDL. */
480     hr = pSHBindToParent(NULL, &IID_IShellFolder, (VOID**)&psfPersonal, &pidlLast);
481     ok (FAILED(hr), "SHBindToParent(NULL) should fail!\n");
482
483     /* But it succeeds with an empty PIDL. */
484     hr = pSHBindToParent(pidlEmpty, &IID_IShellFolder, (VOID**)&psfPersonal, &pidlLast);
485     ok (SUCCEEDED(hr), "SHBindToParent(empty PIDL) should succeed! hr = %08x\n", hr);
486     ok (pidlLast == pidlEmpty, "The last element of an empty PIDL should be the PIDL itself!\n");
487     if (SUCCEEDED(hr)) 
488         IShellFolder_Release(psfPersonal);
489     
490     /* Binding to the folder and querying the display name of the file also works. */
491     hr = pSHBindToParent(pidlTestFile, &IID_IShellFolder, (VOID**)&psfPersonal, &pidlLast); 
492     ok (SUCCEEDED(hr), "SHBindToParent failed! hr = %08x\n", hr);
493     if (FAILED(hr)) {
494         IShellFolder_Release(psfDesktop);
495         return;
496     }
497
498     /* This test shows that Windows doesn't allocate a new pidlLast, but returns a pointer into 
499      * pidlTestFile (In accordance with MSDN). */
500     ok (pILFindLastID(pidlTestFile) == pidlLast, 
501                                 "SHBindToParent doesn't return the last id of the pidl param!\n");
502     
503     hr = IShellFolder_GetDisplayNameOf(psfPersonal, pidlLast, SHGDN_FORPARSING, &strret);
504     ok (SUCCEEDED(hr), "Personal->GetDisplayNameOf failed! hr = %08x\n", hr);
505     if (FAILED(hr)) {
506         IShellFolder_Release(psfDesktop);
507         IShellFolder_Release(psfPersonal);
508         return;
509     }
510
511     if (pStrRetToBufW)
512     {
513         hr = pStrRetToBufW(&strret, pidlLast, wszTestFile2, MAX_PATH);
514         ok (SUCCEEDED(hr), "StrRetToBufW failed! hr = %08x\n", hr);
515         ok (!lstrcmpiW(wszTestFile, wszTestFile2), "GetDisplayNameOf returns incorrect path!\n");
516     }
517     
518     IShellFolder_Release(psfDesktop);
519     IShellFolder_Release(psfPersonal);
520 }
521
522 static void test_CallForAttributes(void)
523 {
524     HKEY hKey;
525     LONG lResult;
526     HRESULT hr;
527     DWORD dwSize;
528     LPSHELLFOLDER psfDesktop;
529     LPITEMIDLIST pidlMyDocuments;
530     DWORD dwAttributes, dwCallForAttributes, dwOrigAttributes, dwOrigCallForAttributes;
531     static const WCHAR wszAttributes[] = { 'A','t','t','r','i','b','u','t','e','s',0 };
532     static const WCHAR wszCallForAttributes[] = { 
533         'C','a','l','l','F','o','r','A','t','t','r','i','b','u','t','e','s',0 };
534     static const WCHAR wszMyDocumentsKey[] = {
535         'C','L','S','I','D','\\','{','4','5','0','D','8','F','B','A','-','A','D','2','5','-',
536         '1','1','D','0','-','9','8','A','8','-','0','8','0','0','3','6','1','B','1','1','0','3','}',
537         '\\','S','h','e','l','l','F','o','l','d','e','r',0 };
538     WCHAR wszMyDocuments[] = {
539         ':',':','{','4','5','0','D','8','F','B','A','-','A','D','2','5','-','1','1','D','0','-',
540         '9','8','A','8','-','0','8','0','0','3','6','1','B','1','1','0','3','}',0 };
541     
542     /* For the root of a namespace extension, the attributes are not queried by binding
543      * to the object and calling GetAttributesOf. Instead, the attributes are read from 
544      * the registry value HKCR/CLSID/{...}/ShellFolder/Attributes. This is documented on MSDN.
545      *
546      * The MyDocuments shellfolder on WinXP has a HKCR/CLSID/{...}/ShellFolder/CallForAttributes
547      * value. It seems that if the folder is queried for one of the flags set in CallForAttributes,
548      * the shell does bind to the folder object and calls GetAttributesOf. This is not documented
549      * on MSDN. This test is meant to document the observed behaviour on WinXP SP2.
550      */
551     hr = SHGetDesktopFolder(&psfDesktop);
552     ok (SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08x\n", hr);
553     if (FAILED(hr)) return;
554     
555     hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszMyDocuments, NULL, 
556                                        &pidlMyDocuments, NULL);
557     ok (SUCCEEDED(hr), 
558         "Desktop's ParseDisplayName failed to parse MyDocuments's CLSID! hr = %08x\n", hr);
559     if (FAILED(hr)) {
560         IShellFolder_Release(psfDesktop);
561         return;
562     }
563
564     dwAttributes = 0xffffffff;
565     hr = IShellFolder_GetAttributesOf(psfDesktop, 1, 
566                                       (LPCITEMIDLIST*)&pidlMyDocuments, &dwAttributes);
567     ok (SUCCEEDED(hr), "Desktop->GetAttributesOf(MyDocuments) failed! hr = %08x\n", hr);
568
569     /* We need the following setup (as observed on WinXP SP2), for the tests to make sense. */
570     ok (dwAttributes & SFGAO_FILESYSTEM, "SFGAO_FILESYSTEM attribute is not set for MyDocuments!\n");
571     ok (!(dwAttributes & SFGAO_ISSLOW), "SFGAO_ISSLOW attribute is set for MyDocuments!\n");
572     ok (!(dwAttributes & SFGAO_GHOSTED), "SFGAO_GHOSTED attribute is set for MyDocuments!\n");
573
574     /* We don't have the MyDocuments shellfolder in wine yet, and thus we don't have the registry
575      * key. So the test will return at this point, if run on wine. 
576      */
577     lResult = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszMyDocumentsKey, 0, KEY_WRITE|KEY_READ, &hKey);
578     ok (lResult == ERROR_SUCCESS, "RegOpenKeyEx failed! result: %08x\n", lResult);
579     if (lResult != ERROR_SUCCESS) {
580         IMalloc_Free(ppM, pidlMyDocuments);
581         IShellFolder_Release(psfDesktop);
582         return;
583     }
584     
585     /* Query MyDocuments' Attributes value, to be able to restore it later. */
586     dwSize = sizeof(DWORD);
587     lResult = RegQueryValueExW(hKey, wszAttributes, NULL, NULL, (LPBYTE)&dwOrigAttributes, &dwSize);
588     ok (lResult == ERROR_SUCCESS, "RegQueryValueEx failed! result: %08x\n", lResult);
589     if (lResult != ERROR_SUCCESS) {
590         RegCloseKey(hKey);
591         IMalloc_Free(ppM, pidlMyDocuments);
592         IShellFolder_Release(psfDesktop);
593         return;
594     }
595
596     /* Query MyDocuments' CallForAttributes value, to be able to restore it later. */
597     dwSize = sizeof(DWORD);
598     lResult = RegQueryValueExW(hKey, wszCallForAttributes, NULL, NULL, 
599                               (LPBYTE)&dwOrigCallForAttributes, &dwSize);
600     ok (lResult == ERROR_SUCCESS, "RegQueryValueEx failed! result: %08x\n", lResult);
601     if (lResult != ERROR_SUCCESS) {
602         RegCloseKey(hKey);
603         IMalloc_Free(ppM, pidlMyDocuments);
604         IShellFolder_Release(psfDesktop);
605         return;
606     }
607     
608     /* Define via the Attributes value that MyDocuments attributes are SFGAO_ISSLOW and 
609      * SFGAO_GHOSTED and that MyDocuments should be called for the SFGAO_ISSLOW and
610      * SFGAO_FILESYSTEM attributes. */
611     dwAttributes = SFGAO_ISSLOW|SFGAO_GHOSTED;
612     RegSetValueExW(hKey, wszAttributes, 0, REG_DWORD, (LPBYTE)&dwAttributes, sizeof(DWORD));
613     dwCallForAttributes = SFGAO_ISSLOW|SFGAO_FILESYSTEM;
614     RegSetValueExW(hKey, wszCallForAttributes, 0, REG_DWORD, 
615                    (LPBYTE)&dwCallForAttributes, sizeof(DWORD));
616
617     /* Although it is not set in CallForAttributes, the SFGAO_GHOSTED flag is reset by 
618      * GetAttributesOf. It seems that once there is a single attribute queried, for which
619      * CallForAttributes is set, all flags are taken from the GetAttributesOf call and
620      * the flags in Attributes are ignored. 
621      */
622     dwAttributes = SFGAO_ISSLOW|SFGAO_GHOSTED|SFGAO_FILESYSTEM;
623     hr = IShellFolder_GetAttributesOf(psfDesktop, 1, 
624                                       (LPCITEMIDLIST*)&pidlMyDocuments, &dwAttributes);
625     ok (SUCCEEDED(hr), "Desktop->GetAttributesOf(MyDocuments) failed! hr = %08x\n", hr);
626     if (SUCCEEDED(hr)) 
627         ok (dwAttributes == SFGAO_FILESYSTEM, 
628             "Desktop->GetAttributes(MyDocuments) returned unexpected attributes: %08x\n", 
629             dwAttributes);
630
631     /* Restore MyDocuments' original Attributes and CallForAttributes registry values */
632     RegSetValueExW(hKey, wszAttributes, 0, REG_DWORD, (LPBYTE)&dwOrigAttributes, sizeof(DWORD));
633     RegSetValueExW(hKey, wszCallForAttributes, 0, REG_DWORD, 
634                    (LPBYTE)&dwOrigCallForAttributes, sizeof(DWORD));
635     RegCloseKey(hKey);
636     IMalloc_Free(ppM, pidlMyDocuments);
637     IShellFolder_Release(psfDesktop);
638 }
639
640 static void test_GetAttributesOf(void) 
641 {
642     HRESULT hr;
643     LPSHELLFOLDER psfDesktop, psfMyComputer;
644     SHITEMID emptyitem = { 0, { 0 } };
645     LPCITEMIDLIST pidlEmpty = (LPCITEMIDLIST)&emptyitem;
646     LPITEMIDLIST pidlMyComputer;
647     DWORD dwFlags;
648     static const DWORD dwDesktopFlags = /* As observed on WinXP SP2 */
649         SFGAO_STORAGE | SFGAO_HASPROPSHEET | SFGAO_STORAGEANCESTOR |
650         SFGAO_FILESYSANCESTOR | SFGAO_FOLDER | SFGAO_FILESYSTEM | SFGAO_HASSUBFOLDER;
651     static const DWORD dwMyComputerFlags = /* As observed on WinXP SP2 */
652         SFGAO_CANRENAME | SFGAO_CANDELETE | SFGAO_HASPROPSHEET |
653         SFGAO_DROPTARGET | SFGAO_FILESYSANCESTOR | SFGAO_FOLDER | SFGAO_HASSUBFOLDER;
654     WCHAR wszMyComputer[] = { 
655         ':',':','{','2','0','D','0','4','F','E','0','-','3','A','E','A','-','1','0','6','9','-',
656         'A','2','D','8','-','0','8','0','0','2','B','3','0','3','0','9','D','}',0 };
657     char  cCurrDirA [MAX_PATH] = {0};
658     WCHAR cCurrDirW [MAX_PATH];
659     static WCHAR cTestDirW[] = {'t','e','s','t','d','i','r',0};
660     static const WCHAR cBackSlash[] = {'\\',0};
661     IShellFolder *IDesktopFolder, *testIShellFolder;
662     ITEMIDLIST *newPIDL;
663     int len;
664
665     hr = SHGetDesktopFolder(&psfDesktop);
666     ok (SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08x\n", hr);
667     if (FAILED(hr)) return;
668
669     /* The Desktop attributes can be queried with a single empty itemidlist, .. */
670     dwFlags = 0xffffffff;
671     hr = IShellFolder_GetAttributesOf(psfDesktop, 1, &pidlEmpty, &dwFlags);
672     ok (SUCCEEDED(hr), "Desktop->GetAttributesOf(empty pidl) failed! hr = %08x\n", hr);
673     ok (dwFlags == dwDesktopFlags, "Wrong Desktop attributes: %08x, expected: %08x\n", 
674         dwFlags, dwDesktopFlags);
675
676     /* .. or with no itemidlist at all. */
677     dwFlags = 0xffffffff;
678     hr = IShellFolder_GetAttributesOf(psfDesktop, 0, NULL, &dwFlags);
679     ok (SUCCEEDED(hr), "Desktop->GetAttributesOf(NULL) failed! hr = %08x\n", hr);
680     ok (dwFlags == dwDesktopFlags, "Wrong Desktop attributes: %08x, expected: %08x\n", 
681         dwFlags, dwDesktopFlags);
682    
683     /* Testing the attributes of the MyComputer shellfolder */
684     hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszMyComputer, NULL, &pidlMyComputer, NULL);
685     ok (SUCCEEDED(hr), "Desktop's ParseDisplayName failed to parse MyComputer's CLSID! hr = %08x\n", hr);
686     if (FAILED(hr)) {
687         IShellFolder_Release(psfDesktop);
688         return;
689     }
690
691     /* WinXP SP2 sets the SFGAO_CANLINK flag, when MyComputer is queried via the Desktop 
692      * folder object. It doesn't do this, if MyComputer is queried directly (see below).
693      * SFGAO_CANLINK is the same as DROPEFFECT_LINK, which MSDN says means: "Drag source
694      * should create a link to the original data". You can't create links on MyComputer on
695      * Windows, so this flag shouldn't be set. Seems like a bug in Windows. As long as nobody
696      * depends on this bug, we probably shouldn't imitate it.
697      */
698     dwFlags = 0xffffffff;
699     hr = IShellFolder_GetAttributesOf(psfDesktop, 1, (LPCITEMIDLIST*)&pidlMyComputer, &dwFlags);
700     ok (SUCCEEDED(hr), "Desktop->GetAttributesOf(MyComputer) failed! hr = %08x\n", hr);
701     ok ((dwFlags & ~(DWORD)SFGAO_CANLINK) == dwMyComputerFlags, 
702                     "Wrong MyComputer attributes: %08x, expected: %08x\n", dwFlags, dwMyComputerFlags);
703
704     hr = IShellFolder_BindToObject(psfDesktop, pidlMyComputer, NULL, &IID_IShellFolder, (LPVOID*)&psfMyComputer);
705     ok (SUCCEEDED(hr), "Desktop failed to bind to MyComputer object! hr = %08x\n", hr);
706     IShellFolder_Release(psfDesktop);
707     IMalloc_Free(ppM, pidlMyComputer);
708     if (FAILED(hr)) return;
709
710     hr = IShellFolder_GetAttributesOf(psfMyComputer, 1, &pidlEmpty, &dwFlags);
711     todo_wine {ok (hr == E_INVALIDARG, "MyComputer->GetAttributesOf(emtpy pidl) should fail! hr = %08x\n", hr); }
712
713     dwFlags = 0xffffffff;
714     hr = IShellFolder_GetAttributesOf(psfMyComputer, 0, NULL, &dwFlags);
715     ok (SUCCEEDED(hr), "MyComputer->GetAttributesOf(NULL) failed! hr = %08x\n", hr); 
716     todo_wine { ok (dwFlags == dwMyComputerFlags, 
717                     "Wrong MyComputer attributes: %08x, expected: %08x\n", dwFlags, dwMyComputerFlags); }
718
719     IShellFolder_Release(psfMyComputer);
720
721     /* create test directory */
722     CreateFilesFolders();
723
724     GetCurrentDirectoryA(MAX_PATH, cCurrDirA);
725     len = lstrlenA(cCurrDirA);
726
727     if (len == 0) {
728         trace("GetCurrentDirectoryA returned empty string. Skipping test_EnumObjects_and_CompareIDs\n");
729         return;
730     }
731     if(cCurrDirA[len-1] == '\\')
732         cCurrDirA[len-1] = 0;
733
734     MultiByteToWideChar(CP_ACP, 0, cCurrDirA, -1, cCurrDirW, MAX_PATH);
735  
736     hr = SHGetDesktopFolder(&IDesktopFolder);
737     ok(hr == S_OK, "SHGetDesktopfolder failed %08x\n", hr);
738
739     hr = IShellFolder_ParseDisplayName(IDesktopFolder, NULL, NULL, cCurrDirW, NULL, &newPIDL, 0);
740     ok(hr == S_OK, "ParseDisplayName failed %08x\n", hr);
741
742     hr = IShellFolder_BindToObject(IDesktopFolder, newPIDL, NULL, (REFIID)&IID_IShellFolder, (LPVOID *)&testIShellFolder);
743     ok(hr == S_OK, "BindToObject failed %08x\n", hr);
744
745     IMalloc_Free(ppM, newPIDL);
746
747     /* get relative PIDL */
748     hr = IShellFolder_ParseDisplayName(testIShellFolder, NULL, NULL, cTestDirW, NULL, &newPIDL, 0);
749     ok(hr == S_OK, "ParseDisplayName failed %08x\n", hr);
750
751     /* test the shell attributes of the test directory using the relative PIDL */
752     dwFlags = SFGAO_FOLDER;
753     hr = IShellFolder_GetAttributesOf(testIShellFolder, 1, (LPCITEMIDLIST*)&newPIDL, &dwFlags);
754     ok (SUCCEEDED(hr), "Desktop->GetAttributesOf() failed! hr = %08x\n", hr);
755     ok ((dwFlags&SFGAO_FOLDER), "Wrong directory attribute for relative PIDL: %08x\n", dwFlags);
756
757     /* free memory */
758     IMalloc_Free(ppM, newPIDL);
759
760     /* append testdirectory name to path */
761     lstrcatW(cCurrDirW, cBackSlash);
762     lstrcatW(cCurrDirW, cTestDirW);
763
764     hr = IShellFolder_ParseDisplayName(IDesktopFolder, NULL, NULL, cCurrDirW, NULL, &newPIDL, 0);
765     ok(hr == S_OK, "ParseDisplayName failed %08x\n", hr);
766
767     /* test the shell attributes of the test directory using the absolute PIDL */
768     dwFlags = SFGAO_FOLDER;
769     hr = IShellFolder_GetAttributesOf(IDesktopFolder, 1, (LPCITEMIDLIST*)&newPIDL, &dwFlags);
770     ok (SUCCEEDED(hr), "Desktop->GetAttributesOf() failed! hr = %08x\n", hr);
771     ok ((dwFlags&SFGAO_FOLDER), "Wrong directory attribute for absolute PIDL: %08x\n", dwFlags);
772         
773     /* free memory */
774     IMalloc_Free(ppM, newPIDL);
775
776     IShellFolder_Release(testIShellFolder);
777
778     Cleanup();
779
780     IShellFolder_Release(IDesktopFolder);
781 }    
782
783 static void test_SHGetPathFromIDList(void)
784 {
785     SHITEMID emptyitem = { 0, { 0 } };
786     LPCITEMIDLIST pidlEmpty = (LPCITEMIDLIST)&emptyitem;
787     LPITEMIDLIST pidlMyComputer;
788     WCHAR wszPath[MAX_PATH], wszDesktop[MAX_PATH];
789     BOOL result;
790     HRESULT hr;
791     LPSHELLFOLDER psfDesktop;
792     WCHAR wszMyComputer[] = { 
793         ':',':','{','2','0','D','0','4','F','E','0','-','3','A','E','A','-','1','0','6','9','-',
794         'A','2','D','8','-','0','8','0','0','2','B','3','0','3','0','9','D','}',0 };
795     WCHAR wszFileName[MAX_PATH];
796     LPITEMIDLIST pidlTestFile;
797     HANDLE hTestFile;
798     STRRET strret;
799     static WCHAR wszTestFile[] = {
800         'w','i','n','e','t','e','s','t','.','f','o','o',0 };
801         HRESULT (WINAPI *pSHGetSpecialFolderLocation)(HWND, int, LPITEMIDLIST *);
802         HMODULE hShell32;
803         LPITEMIDLIST pidlPrograms;
804
805     if(!pSHGetPathFromIDListW || !pSHGetSpecialFolderPathW)
806     {
807         skip("SHGetPathFromIDListW() or SHGetSpecialFolderPathW() is missing\n");
808         return;
809     }
810
811     /* Calling SHGetPathFromIDListW with no pidl should return the empty string */
812     wszPath[0] = 'a';
813     wszPath[1] = '\0';
814     result = pSHGetPathFromIDListW(NULL, wszPath);
815     ok(!result, "Expected failure\n");
816     ok(!wszPath[0], "Expected empty string\n");
817
818     /* Calling SHGetPathFromIDListW with an empty pidl should return the desktop folder's path. */
819     result = pSHGetSpecialFolderPathW(NULL, wszDesktop, CSIDL_DESKTOP, FALSE);
820     ok(result, "SHGetSpecialFolderPathW(CSIDL_DESKTOP) failed! Last error: %u\n", GetLastError());
821     if (!result) return;
822     
823     result = pSHGetPathFromIDListW(pidlEmpty, wszPath);
824     ok(result, "SHGetPathFromIDListW failed! Last error: %u\n", GetLastError());
825     if (!result) return;
826     ok(!lstrcmpiW(wszDesktop, wszPath), "SHGetPathFromIDListW didn't return desktop path for empty pidl!\n");
827
828     /* MyComputer does not map to a filesystem path. SHGetPathFromIDListW should fail. */
829     hr = SHGetDesktopFolder(&psfDesktop);
830     ok (SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08x\n", hr);
831     if (FAILED(hr)) return;
832
833     hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszMyComputer, NULL, &pidlMyComputer, NULL);
834     ok (SUCCEEDED(hr), "Desktop's ParseDisplayName failed to parse MyComputer's CLSID! hr = %08x\n", hr);
835     if (FAILED(hr)) {
836         IShellFolder_Release(psfDesktop);
837         return;
838     }
839
840     SetLastError(0xdeadbeef);
841     wszPath[0] = 'a';
842     wszPath[1] = '\0';
843     result = pSHGetPathFromIDListW(pidlMyComputer, wszPath);
844     ok (!result, "SHGetPathFromIDListW succeeded where it shouldn't!\n");
845     ok (GetLastError()==0xdeadbeef, "SHGetPathFromIDListW shouldn't set last error! Last error: %u\n", GetLastError());
846     ok (!wszPath[0], "Expected empty path\n");
847     if (result) {
848         IShellFolder_Release(psfDesktop);
849         return;
850     }
851
852     IMalloc_Free(ppM, pidlMyComputer);
853
854     result = pSHGetSpecialFolderPathW(NULL, wszFileName, CSIDL_DESKTOPDIRECTORY, FALSE);
855     ok(result, "SHGetSpecialFolderPathW failed! Last error: %u\n", GetLastError());
856     if (!result) {
857         IShellFolder_Release(psfDesktop);
858         return;
859     }
860     myPathAddBackslashW(wszFileName);
861     lstrcatW(wszFileName, wszTestFile);
862     hTestFile = CreateFileW(wszFileName, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, NULL);
863     ok(hTestFile != INVALID_HANDLE_VALUE, "CreateFileW failed! Last error: %u\n", GetLastError());
864     if (hTestFile == INVALID_HANDLE_VALUE) {
865         IShellFolder_Release(psfDesktop);
866         return;
867     }
868     CloseHandle(hTestFile);
869
870     hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszTestFile, NULL, &pidlTestFile, NULL);
871     ok (SUCCEEDED(hr), "Desktop's ParseDisplayName failed to parse filename hr = %08x\n", hr);
872     if (FAILED(hr)) {
873         IShellFolder_Release(psfDesktop);
874         DeleteFileW(wszFileName);
875         IMalloc_Free(ppM, pidlTestFile);
876         return;
877     }
878
879     /* This test is to show that the Desktop shellfolder prepends the CSIDL_DESKTOPDIRECTORY
880      * path for files placed on the desktop, if called with SHGDN_FORPARSING. */
881     hr = IShellFolder_GetDisplayNameOf(psfDesktop, pidlTestFile, SHGDN_FORPARSING, &strret);
882     ok (SUCCEEDED(hr), "Desktop's GetDisplayNamfOf failed! hr = %08x\n", hr);
883     IShellFolder_Release(psfDesktop);
884     DeleteFileW(wszFileName);
885     if (FAILED(hr)) {
886         IMalloc_Free(ppM, pidlTestFile);
887         return;
888     }
889     if (pStrRetToBufW)
890     {
891         pStrRetToBufW(&strret, pidlTestFile, wszPath, MAX_PATH);
892         ok(0 == lstrcmpW(wszFileName, wszPath), 
893            "Desktop->GetDisplayNameOf(pidlTestFile, SHGDN_FORPARSING) "
894            "returned incorrect path for file placed on desktop\n");
895     }
896
897     result = pSHGetPathFromIDListW(pidlTestFile, wszPath);
898     ok(result, "SHGetPathFromIDListW failed! Last error: %u\n", GetLastError());
899     IMalloc_Free(ppM, pidlTestFile);
900     if (!result) return;
901     ok(0 == lstrcmpW(wszFileName, wszPath), "SHGetPathFromIDListW returned incorrect path for file placed on desktop\n");
902
903
904         /* Test if we can get the path from the start menu "program files" PIDL. */
905     hShell32 = GetModuleHandleA("shell32");
906     pSHGetSpecialFolderLocation = (void *)GetProcAddress(hShell32, "SHGetSpecialFolderLocation");
907
908     hr = pSHGetSpecialFolderLocation(NULL, CSIDL_PROGRAM_FILES, &pidlPrograms);
909     ok(SUCCEEDED(hr), "SHGetFolderLocation failed: 0x%08x\n", hr);
910
911     SetLastError(0xdeadbeef);
912     result = pSHGetPathFromIDListW(pidlPrograms, wszPath);
913         IMalloc_Free(ppM, pidlPrograms);
914     ok(result, "SHGetPathFromIDListW failed\n");
915 }
916
917 static void test_EnumObjects_and_CompareIDs(void)
918 {
919     ITEMIDLIST *newPIDL;
920     IShellFolder *IDesktopFolder, *testIShellFolder;
921     char  cCurrDirA [MAX_PATH] = {0};
922     WCHAR cCurrDirW [MAX_PATH];
923     static const WCHAR cTestDirW[] = {'\\','t','e','s','t','d','i','r',0};
924     int len;
925     HRESULT hr;
926
927     GetCurrentDirectoryA(MAX_PATH, cCurrDirA);
928     len = lstrlenA(cCurrDirA);
929
930     if(len == 0) {
931         trace("GetCurrentDirectoryA returned empty string. Skipping test_EnumObjects_and_CompareIDs\n");
932         return;
933     }
934     if(cCurrDirA[len-1] == '\\')
935         cCurrDirA[len-1] = 0;
936
937     MultiByteToWideChar(CP_ACP, 0, cCurrDirA, -1, cCurrDirW, MAX_PATH);
938     lstrcatW(cCurrDirW, cTestDirW);
939
940     hr = SHGetDesktopFolder(&IDesktopFolder);
941     ok(hr == S_OK, "SHGetDesktopfolder failed %08x\n", hr);
942
943     CreateFilesFolders();
944
945     hr = IShellFolder_ParseDisplayName(IDesktopFolder, NULL, NULL, cCurrDirW, NULL, &newPIDL, 0);
946     ok(hr == S_OK, "ParseDisplayName failed %08x\n", hr);
947
948     hr = IShellFolder_BindToObject(IDesktopFolder, newPIDL, NULL, (REFIID)&IID_IShellFolder, (LPVOID *)&testIShellFolder);
949     ok(hr == S_OK, "BindToObject failed %08x\n", hr);
950
951     test_EnumObjects(testIShellFolder);
952
953     IShellFolder_Release(testIShellFolder);
954
955     Cleanup();
956
957     IMalloc_Free(ppM, newPIDL);
958
959     IShellFolder_Release(IDesktopFolder);
960 }
961
962 /* A simple implementation of an IPropertyBag, which returns fixed values for
963  * 'Target' and 'Attributes' properties.
964  */
965 static HRESULT WINAPI InitPropertyBag_IPropertyBag_QueryInterface(IPropertyBag *iface, REFIID riid,
966     void **ppvObject) 
967 {
968     if (!ppvObject)
969         return E_INVALIDARG;
970
971     if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IPropertyBag, riid)) {
972         *ppvObject = iface;
973     } else {
974         ok (FALSE, "InitPropertyBag asked for unknown interface!\n");
975         return E_NOINTERFACE;
976     }
977
978     IPropertyBag_AddRef(iface);
979     return S_OK;
980 }
981
982 static ULONG WINAPI InitPropertyBag_IPropertyBag_AddRef(IPropertyBag *iface) {
983     return 2;
984 }
985
986 static ULONG WINAPI InitPropertyBag_IPropertyBag_Release(IPropertyBag *iface) {
987     return 1;
988 }
989
990 static HRESULT WINAPI InitPropertyBag_IPropertyBag_Read(IPropertyBag *iface, LPCOLESTR pszPropName,
991     VARIANT *pVar, IErrorLog *pErrorLog)
992 {
993     static const WCHAR wszTargetSpecialFolder[] = {
994         'T','a','r','g','e','t','S','p','e','c','i','a','l','F','o','l','d','e','r',0 };
995     static const WCHAR wszTarget[] = {
996         'T','a','r','g','e','t',0 };
997     static const WCHAR wszAttributes[] = {
998         'A','t','t','r','i','b','u','t','e','s',0 };
999     static const WCHAR wszResolveLinkFlags[] = {
1000         'R','e','s','o','l','v','e','L','i','n','k','F','l','a','g','s',0 };
1001        
1002     if (!lstrcmpW(pszPropName, wszTargetSpecialFolder)) {
1003         ok(V_VT(pVar) == VT_I4, "Wrong variant type for 'TargetSpecialFolder' property!\n");
1004         return E_INVALIDARG;
1005     }
1006     
1007     if (!lstrcmpW(pszPropName, wszResolveLinkFlags)) 
1008     {
1009         ok(V_VT(pVar) == VT_UI4, "Wrong variant type for 'ResolveLinkFlags' property!\n");
1010         return E_INVALIDARG;
1011     }
1012
1013     if (!lstrcmpW(pszPropName, wszTarget)) {
1014         WCHAR wszPath[MAX_PATH];
1015         BOOL result;
1016         
1017         ok(V_VT(pVar) == VT_BSTR, "Wrong variant type for 'Target' property!\n");
1018         if (V_VT(pVar) != VT_BSTR) return E_INVALIDARG;
1019
1020         result = pSHGetSpecialFolderPathW(NULL, wszPath, CSIDL_DESKTOPDIRECTORY, FALSE);
1021         ok(result, "SHGetSpecialFolderPathW(DESKTOPDIRECTORY) failed! %u\n", GetLastError());
1022         if (!result) return E_INVALIDARG;
1023
1024         V_BSTR(pVar) = SysAllocString(wszPath);
1025         return S_OK;
1026     }
1027
1028     if (!lstrcmpW(pszPropName, wszAttributes)) {
1029         ok(V_VT(pVar) == VT_UI4, "Wrong variant type for 'Attributes' property!\n");
1030         if (V_VT(pVar) != VT_UI4) return E_INVALIDARG;
1031         V_UI4(pVar) = SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR|
1032                       SFGAO_CANRENAME|SFGAO_FILESYSTEM;
1033         return S_OK;
1034     }
1035
1036     ok(FALSE, "PropertyBag was asked for unknown property (vt=%d)!\n", V_VT(pVar));
1037     return E_INVALIDARG;
1038 }
1039
1040 static HRESULT WINAPI InitPropertyBag_IPropertyBag_Write(IPropertyBag *iface, LPCOLESTR pszPropName,
1041     VARIANT *pVar)
1042 {
1043     ok(FALSE, "Unexpected call to IPropertyBag_Write\n");
1044     return E_NOTIMPL;
1045 }
1046     
1047 static const IPropertyBagVtbl InitPropertyBag_IPropertyBagVtbl = {
1048     InitPropertyBag_IPropertyBag_QueryInterface,
1049     InitPropertyBag_IPropertyBag_AddRef,
1050     InitPropertyBag_IPropertyBag_Release,
1051     InitPropertyBag_IPropertyBag_Read,
1052     InitPropertyBag_IPropertyBag_Write
1053 };
1054
1055 static struct IPropertyBag InitPropertyBag = {
1056     &InitPropertyBag_IPropertyBagVtbl
1057 };
1058
1059 static void test_FolderShortcut(void) {
1060     IPersistPropertyBag *pPersistPropertyBag;
1061     IShellFolder *pShellFolder, *pDesktopFolder;
1062     IPersistFolder3 *pPersistFolder3;
1063     HRESULT hr;
1064     STRRET strret;
1065     WCHAR wszDesktopPath[MAX_PATH], wszBuffer[MAX_PATH];
1066     BOOL result;
1067     CLSID clsid;
1068     LPITEMIDLIST pidlCurrentFolder, pidlWineTestFolder, pidlSubFolder;
1069     HKEY hShellExtKey;
1070     WCHAR wszWineTestFolder[] = {
1071         ':',':','{','9','B','3','5','2','E','B','F','-','2','7','6','5','-','4','5','C','1','-',
1072         'B','4','C','6','-','8','5','C','C','7','F','7','A','B','C','6','4','}',0 };
1073     WCHAR wszShellExtKey[] = { 'S','o','f','t','w','a','r','e','\\',
1074         'M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\',
1075         'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
1076         'E','x','p','l','o','r','e','r','\\','D','e','s','k','t','o','p','\\',
1077         'N','a','m','e','S','p','a','c','e','\\',
1078         '{','9','b','3','5','2','e','b','f','-','2','7','6','5','-','4','5','c','1','-',
1079         'b','4','c','6','-','8','5','c','c','7','f','7','a','b','c','6','4','}',0 };
1080     
1081     WCHAR wszSomeSubFolder[] = { 'S','u','b','F','o','l','d','e','r', 0};
1082     static const GUID CLSID_UnixDosFolder = 
1083         {0x9d20aae8, 0x0625, 0x44b0, {0x9c, 0xa7, 0x71, 0x88, 0x9c, 0x22, 0x54, 0xd9}};
1084
1085     if (!pSHGetSpecialFolderPathW || !pStrRetToBufW) return;
1086    
1087     /* These tests basically show, that CLSID_FolderShortcuts are initialized
1088      * via their IPersistPropertyBag interface. And that the target folder
1089      * is taken from the IPropertyBag's 'Target' property.
1090      */
1091     hr = CoCreateInstance(&CLSID_FolderShortcut, NULL, CLSCTX_INPROC_SERVER, 
1092                           &IID_IPersistPropertyBag, (LPVOID*)&pPersistPropertyBag);
1093     ok (SUCCEEDED(hr), "CoCreateInstance failed! hr = 0x%08x\n", hr);
1094     if (FAILED(hr)) return;
1095
1096     hr = IPersistPropertyBag_Load(pPersistPropertyBag, &InitPropertyBag, NULL);
1097     ok(SUCCEEDED(hr), "IPersistPropertyBag_Load failed! hr = %08x\n", hr);
1098     if (FAILED(hr)) {
1099         IPersistPropertyBag_Release(pPersistPropertyBag);
1100         return;
1101     }
1102     
1103     hr = IPersistPropertyBag_QueryInterface(pPersistPropertyBag, &IID_IShellFolder, 
1104                                             (LPVOID*)&pShellFolder);
1105     IPersistPropertyBag_Release(pPersistPropertyBag);
1106     ok(SUCCEEDED(hr), "IPersistPropertyBag_QueryInterface(IShellFolder) failed! hr = %08x\n", hr);
1107     if (FAILED(hr)) return;
1108
1109     hr = IShellFolder_GetDisplayNameOf(pShellFolder, NULL, SHGDN_FORPARSING, &strret);
1110     ok(SUCCEEDED(hr), "IShellFolder_GetDisplayNameOf(NULL) failed! hr = %08x\n", hr);
1111     if (FAILED(hr)) {
1112         IShellFolder_Release(pShellFolder);
1113         return;
1114     }
1115
1116     result = pSHGetSpecialFolderPathW(NULL, wszDesktopPath, CSIDL_DESKTOPDIRECTORY, FALSE);
1117     ok(result, "SHGetSpecialFolderPathW(CSIDL_DESKTOPDIRECTORY) failed! %u\n", GetLastError());
1118     if (!result) return;
1119
1120     pStrRetToBufW(&strret, NULL, wszBuffer, MAX_PATH);
1121     ok(!lstrcmpiW(wszDesktopPath, wszBuffer), "FolderShortcut returned incorrect folder!\n");
1122
1123     hr = IShellFolder_QueryInterface(pShellFolder, &IID_IPersistFolder3, (LPVOID*)&pPersistFolder3);
1124     IShellFolder_Release(pShellFolder);
1125     ok(SUCCEEDED(hr), "IShellFolder_QueryInterface(IID_IPersistFolder3 failed! hr = 0x%08x\n", hr);
1126     if (FAILED(hr)) return;
1127
1128     hr = IPersistFolder3_GetClassID(pPersistFolder3, &clsid);
1129     ok(SUCCEEDED(hr), "IPersistFolder3_GetClassID failed! hr=0x%08x\n", hr);
1130     ok(IsEqualCLSID(&clsid, &CLSID_FolderShortcut), "Unexpected CLSID!\n");
1131
1132     hr = IPersistFolder3_GetCurFolder(pPersistFolder3, &pidlCurrentFolder);
1133     ok(SUCCEEDED(hr), "IPersistFolder3_GetCurFolder failed! hr=0x%08x\n", hr);
1134     ok(!pidlCurrentFolder, "IPersistFolder3_GetCurFolder should return a NULL pidl!\n");
1135                     
1136     /* For FolderShortcut objects, the Initialize method initialized the folder's position in the
1137      * shell namespace. The target folder, read from the property bag above, remains untouched. 
1138      * The following tests show this: The itemidlist for some imaginary shellfolder object
1139      * is created and the FolderShortcut is initialized with it. GetCurFolder now returns this
1140      * itemidlist, but GetDisplayNameOf still returns the path from above.
1141      */
1142     hr = SHGetDesktopFolder(&pDesktopFolder);
1143     ok (SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08x\n", hr);
1144     if (FAILED(hr)) return;
1145
1146     /* Temporarily register WineTestFolder as a shell namespace extension at the Desktop. 
1147      * Otherwise ParseDisplayName fails on WinXP with E_INVALIDARG */
1148     RegCreateKeyW(HKEY_CURRENT_USER, wszShellExtKey, &hShellExtKey);
1149     RegCloseKey(hShellExtKey);
1150     hr = IShellFolder_ParseDisplayName(pDesktopFolder, NULL, NULL, wszWineTestFolder, NULL,
1151                                        &pidlWineTestFolder, NULL);
1152     RegDeleteKeyW(HKEY_CURRENT_USER, wszShellExtKey);
1153     IShellFolder_Release(pDesktopFolder);
1154     ok (SUCCEEDED(hr), "IShellFolder::ParseDisplayName failed! hr = %08x\n", hr);
1155     if (FAILED(hr)) return;
1156
1157     hr = IPersistFolder3_Initialize(pPersistFolder3, pidlWineTestFolder);
1158     ok (SUCCEEDED(hr), "IPersistFolder3::Initialize failed! hr = %08x\n", hr);
1159     if (FAILED(hr)) {
1160         IPersistFolder3_Release(pPersistFolder3);
1161         pILFree(pidlWineTestFolder);
1162         return;
1163     }
1164
1165     hr = IPersistFolder3_GetCurFolder(pPersistFolder3, &pidlCurrentFolder);
1166     ok(SUCCEEDED(hr), "IPersistFolder3_GetCurFolder failed! hr=0x%08x\n", hr);
1167     ok(pILIsEqual(pidlCurrentFolder, pidlWineTestFolder),
1168         "IPersistFolder3_GetCurFolder should return pidlWineTestFolder!\n");
1169     pILFree(pidlCurrentFolder);
1170     pILFree(pidlWineTestFolder);
1171
1172     hr = IPersistFolder3_QueryInterface(pPersistFolder3, &IID_IShellFolder, (LPVOID*)&pShellFolder);
1173     IPersistFolder3_Release(pPersistFolder3);
1174     ok(SUCCEEDED(hr), "IPersistFolder3_QueryInterface(IShellFolder) failed! hr = %08x\n", hr);
1175     if (FAILED(hr)) return;
1176
1177     hr = IShellFolder_GetDisplayNameOf(pShellFolder, NULL, SHGDN_FORPARSING, &strret);
1178     ok(SUCCEEDED(hr), "IShellFolder_GetDisplayNameOf(NULL) failed! hr = %08x\n", hr);
1179     if (FAILED(hr)) {
1180         IShellFolder_Release(pShellFolder);
1181         return;
1182     }
1183
1184     pStrRetToBufW(&strret, NULL, wszBuffer, MAX_PATH);
1185     ok(!lstrcmpiW(wszDesktopPath, wszBuffer), "FolderShortcut returned incorrect folder!\n");
1186
1187     /* Next few lines are meant to show that children of FolderShortcuts are not FolderShortcuts,
1188      * but ShellFSFolders. */
1189     myPathAddBackslashW(wszDesktopPath);
1190     lstrcatW(wszDesktopPath, wszSomeSubFolder);
1191     if (!CreateDirectoryW(wszDesktopPath, NULL)) {
1192         IShellFolder_Release(pShellFolder);
1193         return;
1194     }
1195     
1196     hr = IShellFolder_ParseDisplayName(pShellFolder, NULL, NULL, wszSomeSubFolder, NULL, 
1197                                        &pidlSubFolder, NULL);
1198     RemoveDirectoryW(wszDesktopPath);
1199     ok (SUCCEEDED(hr), "IShellFolder::ParseDisplayName failed! hr = %08x\n", hr);
1200     if (FAILED(hr)) {
1201         IShellFolder_Release(pShellFolder);
1202         return;
1203     }
1204
1205     hr = IShellFolder_BindToObject(pShellFolder, pidlSubFolder, NULL, &IID_IPersistFolder3,
1206                                    (LPVOID*)&pPersistFolder3);
1207     IShellFolder_Release(pShellFolder);
1208     pILFree(pidlSubFolder);
1209     ok (SUCCEEDED(hr), "IShellFolder::BindToObject failed! hr = %08x\n", hr);
1210     if (FAILED(hr))
1211         return;
1212
1213     /* On windows, we expect CLSID_ShellFSFolder. On wine we relax this constraint
1214      * a little bit and also allow CLSID_UnixDosFolder. */
1215     hr = IPersistFolder3_GetClassID(pPersistFolder3, &clsid);
1216     ok(SUCCEEDED(hr), "IPersistFolder3_GetClassID failed! hr=0x%08x\n", hr);
1217     ok(IsEqualCLSID(&clsid, &CLSID_ShellFSFolder) || IsEqualCLSID(&clsid, &CLSID_UnixDosFolder),
1218         "IPersistFolder3::GetClassID returned unexpected CLSID!\n");
1219
1220     IPersistFolder3_Release(pPersistFolder3);
1221 }
1222
1223 #include "pshpack1.h"
1224 struct FileStructA {
1225     BYTE  type;
1226     BYTE  dummy;
1227     DWORD dwFileSize;
1228     WORD  uFileDate;    /* In our current implementation this is */
1229     WORD  uFileTime;    /* FileTimeToDosDate(WIN32_FIND_DATA->ftLastWriteTime) */
1230     WORD  uFileAttribs;
1231     CHAR  szName[1];
1232 };
1233
1234 struct FileStructW {
1235     WORD  cbLen;        /* Length of this element. */
1236     BYTE  abFooBar1[6]; /* Beyond any recognition. */
1237     WORD  uDate;        /* FileTimeToDosDate(WIN32_FIND_DATA->ftCreationTime)? */
1238     WORD  uTime;        /* (this is currently speculation) */
1239     WORD  uDate2;       /* FileTimeToDosDate(WIN32_FIND_DATA->ftLastAccessTime)? */
1240     WORD  uTime2;       /* (this is currently speculation) */
1241     BYTE  abFooBar2[4]; /* Beyond any recognition. */
1242     WCHAR wszName[1];   /* The long filename in unicode. */
1243     /* Just for documentation: Right after the unicode string: */
1244     WORD  cbOffset;     /* FileStructW's offset from the beginning of the SHITMEID. 
1245                          * SHITEMID->cb == uOffset + cbLen */
1246 };
1247 #include "poppack.h"
1248
1249 static void test_ITEMIDLIST_format(void) {
1250     WCHAR wszPersonal[MAX_PATH];
1251     LPSHELLFOLDER psfDesktop, psfPersonal;
1252     LPITEMIDLIST pidlPersonal, pidlFile;
1253     HANDLE hFile;
1254     HRESULT hr;
1255     BOOL bResult;
1256     WCHAR wszFile[3][17] = { { 'e','v','e','n','_',0 }, { 'o','d','d','_',0 }, 
1257         { 'l','o','n','g','e','r','_','t','h','a','n','.','8','_','3',0 } };
1258     int i;
1259     
1260     if(!pSHGetSpecialFolderPathW) return;
1261
1262     bResult = pSHGetSpecialFolderPathW(NULL, wszPersonal, CSIDL_PERSONAL, FALSE);
1263     ok(bResult, "SHGetSpecialFolderPathW failed! Last error: %u\n", GetLastError());
1264     if (!bResult) return;
1265
1266     bResult = SetCurrentDirectoryW(wszPersonal);
1267     ok(bResult, "SetCurrentDirectory failed! Last error: %u\n", GetLastError());
1268     if (!bResult) return;
1269
1270     hr = SHGetDesktopFolder(&psfDesktop);
1271     ok(SUCCEEDED(hr), "SHGetDesktopFolder failed! hr: %08x\n", hr);
1272     if (FAILED(hr)) return;
1273
1274     hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszPersonal, NULL, &pidlPersonal, NULL);
1275     ok(SUCCEEDED(hr), "psfDesktop->ParseDisplayName failed! hr = %08x\n", hr);
1276     if (FAILED(hr)) {
1277         IShellFolder_Release(psfDesktop);
1278         return;
1279     }
1280
1281     hr = IShellFolder_BindToObject(psfDesktop, pidlPersonal, NULL, &IID_IShellFolder,
1282         (LPVOID*)&psfPersonal);
1283     IShellFolder_Release(psfDesktop);
1284     pILFree(pidlPersonal);
1285     ok(SUCCEEDED(hr), "psfDesktop->BindToObject failed! hr = %08x\n", hr);
1286     if (FAILED(hr)) return;
1287
1288     for (i=0; i<3; i++) {
1289         CHAR szFile[MAX_PATH];
1290         struct FileStructA *pFileStructA;
1291         WORD cbOffset;
1292         
1293         WideCharToMultiByte(CP_ACP, 0, wszFile[i], -1, szFile, MAX_PATH, NULL, NULL);
1294         
1295         hFile = CreateFileW(wszFile[i], GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_FLAG_WRITE_THROUGH, NULL);
1296         ok(hFile != INVALID_HANDLE_VALUE, "CreateFile failed! (%u)\n", GetLastError());
1297         if (hFile == INVALID_HANDLE_VALUE) {
1298             IShellFolder_Release(psfPersonal);
1299             return;
1300         }
1301         CloseHandle(hFile);
1302
1303         hr = IShellFolder_ParseDisplayName(psfPersonal, NULL, NULL, wszFile[i], NULL, &pidlFile, NULL);
1304         DeleteFileW(wszFile[i]);
1305         ok(SUCCEEDED(hr), "psfPersonal->ParseDisplayName failed! hr: %08x\n", hr);
1306         if (FAILED(hr)) {
1307             IShellFolder_Release(psfPersonal);
1308             return;
1309         }
1310
1311         pFileStructA = (struct FileStructA *)pidlFile->mkid.abID;
1312         ok(pFileStructA->type == 0x32, "PIDLTYPE should be 0x32!\n");
1313         ok(pFileStructA->dummy == 0x00, "Dummy Byte should be 0x00!\n");
1314         ok(pFileStructA->dwFileSize == 0, "Filesize should be zero!\n"); 
1315
1316         if (i < 2) /* First two file names are already in valid 8.3 format */ 
1317             ok(!strcmp(szFile, (CHAR*)&pidlFile->mkid.abID[12]), "Wrong file name!\n");
1318         else 
1319             /* WinXP stores a derived 8.3 dos name (LONGER~1.8_3) here. We probably
1320              * can't implement this correctly, since unix filesystems don't support
1321              * this nasty short/long filename stuff. So we'll probably stay with our
1322              * current habbit of storing the long filename here, which seems to work
1323              * just fine. */
1324             todo_wine { ok(pidlFile->mkid.abID[18] == '~', "Should be derived 8.3 name!\n"); }
1325
1326         if (i == 0) /* First file name has an even number of chars. No need for alignment. */
1327             ok(pidlFile->mkid.abID[12 + strlen(szFile) + 1] != '\0', 
1328                 "Alignment byte, where there shouldn't be!\n"); 
1329         
1330         if (i == 1) /* Second file name has an uneven number of chars => alignment byte */
1331             ok(pidlFile->mkid.abID[12 + strlen(szFile) + 1] == '\0', 
1332                 "There should be an alignment byte, but isn't!\n");
1333
1334         /* The offset of the FileStructW member is stored as a WORD at the end of the pidl. */
1335         cbOffset = *(WORD*)(((LPBYTE)pidlFile)+pidlFile->mkid.cb-sizeof(WORD));
1336         ok (cbOffset >= sizeof(struct FileStructA) &&
1337             cbOffset <= pidlFile->mkid.cb - sizeof(struct FileStructW),
1338             "Wrong offset value (%d) stored at the end of the PIDL\n", cbOffset);
1339
1340         if (cbOffset >= sizeof(struct FileStructA) &&
1341             cbOffset <= pidlFile->mkid.cb - sizeof(struct FileStructW)) 
1342         {
1343             struct FileStructW *pFileStructW = (struct FileStructW *)(((LPBYTE)pidlFile)+cbOffset);
1344
1345             ok(pidlFile->mkid.cb == cbOffset + pFileStructW->cbLen, 
1346                 "FileStructW's offset and length should add up to the PIDL's length!\n");
1347
1348             if (pidlFile->mkid.cb == cbOffset + pFileStructW->cbLen) {
1349                 /* Since we just created the file, time of creation,
1350                  * time of last access and time of last write access just be the same. 
1351                  * These tests seem to fail sometimes (on WinXP), if the test is run again shortly 
1352                  * after the first run. I do remember something with NTFS keeping the creation time
1353                  * if a file is deleted and then created again within a couple of seconds or so.
1354                  * Might be the reason. */
1355                 ok (pFileStructA->uFileDate == pFileStructW->uDate &&
1356                     pFileStructA->uFileTime == pFileStructW->uTime,
1357                     "Last write time should match creation time!\n");
1358     
1359                 ok (pFileStructA->uFileDate == pFileStructW->uDate2 &&
1360                     pFileStructA->uFileTime == pFileStructW->uTime2,
1361                     "Last write time should match last access time!\n");
1362
1363                 ok (!lstrcmpW(wszFile[i], pFileStructW->wszName),
1364                     "The filename should be stored in unicode at this position!\n");
1365             }
1366         }
1367
1368         pILFree(pidlFile);
1369     }
1370 }
1371
1372 static void testSHGetFolderPathAndSubDirA(void)
1373 {
1374     HRESULT ret;
1375     BOOL delret;
1376     DWORD dwret;
1377     int i;
1378     static char wine[] = "wine";
1379     static char winetemp[] = "wine\\temp";
1380     static char appdata[MAX_PATH];
1381     static char testpath[MAX_PATH];
1382     static char toolongpath[MAX_PATH+1];
1383
1384     if(!pSHGetFolderPathA) {
1385         skip("SHGetFolderPathA not present!\n");
1386         return;
1387     }
1388     if(!SUCCEEDED(pSHGetFolderPathA(NULL, CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, appdata)))
1389     {
1390         skip("SHGetFolderPathA failed for CSIDL_LOCAL_APPDATA!\n");
1391         return;
1392     }
1393
1394     sprintf(testpath, "%s\\%s", appdata, winetemp);
1395     delret = RemoveDirectoryA(testpath);
1396     if(!delret && (ERROR_PATH_NOT_FOUND != GetLastError()) ) {
1397         skip("RemoveDirectoryA(%s) failed with error %u\n", testpath, GetLastError());
1398         return;
1399     }
1400
1401     sprintf(testpath, "%s\\%s", appdata, wine);
1402     delret = RemoveDirectoryA(testpath);
1403     if(!delret && (ERROR_PATH_NOT_FOUND != GetLastError()) && (ERROR_FILE_NOT_FOUND != GetLastError())) {
1404         skip("RemoveDirectoryA(%s) failed with error %u\n", testpath, GetLastError());
1405         return;
1406     }
1407     for(i=0; i< MAX_PATH; i++)
1408         toolongpath[i] = '0' + i % 10;
1409     toolongpath[MAX_PATH] = '\0';
1410
1411     /* test invalid second parameter */
1412     ret = pSHGetFolderPathAndSubDirA(NULL, CSIDL_FLAG_DONT_VERIFY | 0xff, NULL, SHGFP_TYPE_CURRENT, wine, testpath);
1413     ok(E_INVALIDARG == ret, "expected E_INVALIDARG, got  %x\n", ret);
1414
1415     /* test invalid forth parameter */
1416     ret = pSHGetFolderPathAndSubDirA(NULL, CSIDL_FLAG_DONT_VERIFY | CSIDL_LOCAL_APPDATA, NULL, 2, wine, testpath);
1417     switch(ret) {
1418         case S_OK: /* winvista */
1419             ok(!strncmp(appdata, testpath, strlen(appdata)),
1420                 "expected %s to start with %s\n", testpath, appdata);
1421             ok(!lstrcmpA(&testpath[1 + strlen(appdata)], winetemp),
1422                 "expected %s to end with %s\n", testpath, winetemp);
1423             break;
1424         case E_INVALIDARG: /* winxp, win2k3 */
1425             break;
1426         default:
1427             ok(0, "expected S_OK or E_INVALIDARG, got  %x\n", ret);
1428     }
1429
1430     /* test fifth parameter */
1431     testpath[0] = '\0';
1432     ret = pSHGetFolderPathAndSubDirA(NULL, CSIDL_FLAG_DONT_VERIFY | CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, NULL, testpath);
1433     ok(S_OK == ret, "expected S_OK, got %x\n", ret);
1434     ok(!lstrcmpA(appdata, testpath), "expected %s, got %s\n", appdata, testpath);
1435
1436     testpath[0] = '\0';
1437     ret = pSHGetFolderPathAndSubDirA(NULL, CSIDL_FLAG_DONT_VERIFY | CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, "", testpath);
1438     ok(S_OK == ret, "expected S_OK, got %x\n", ret);
1439     ok(!lstrcmpA(appdata, testpath), "expected %s, got %s\n", appdata, testpath);
1440
1441     testpath[0] = '\0';
1442     ret = pSHGetFolderPathAndSubDirA(NULL, CSIDL_FLAG_DONT_VERIFY | CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, "\\", testpath);
1443     ok(S_OK == ret, "expected S_OK, got %x\n", ret);
1444     ok(!lstrcmpA(appdata, testpath), "expected %s, got %s\n", appdata, testpath);
1445
1446     ret = pSHGetFolderPathAndSubDirA(NULL, CSIDL_FLAG_DONT_VERIFY | CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, toolongpath, testpath);
1447     ok(HRESULT_FROM_WIN32(ERROR_FILENAME_EXCED_RANGE) == ret,
1448         "expected %x, got %x\n", HRESULT_FROM_WIN32(ERROR_FILENAME_EXCED_RANGE), ret);
1449
1450     testpath[0] = '\0';
1451     ret = pSHGetFolderPathAndSubDirA(NULL, CSIDL_FLAG_DONT_VERIFY | CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, wine, NULL);
1452     ok((S_OK == ret) || (E_INVALIDARG == ret), "expected S_OK or E_INVALIDARG, got %x\n", ret);
1453
1454     /* test a not existing path */
1455     testpath[0] = '\0';
1456     ret = pSHGetFolderPathAndSubDirA(NULL, CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, winetemp, testpath);
1457     ok(HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND) == ret,
1458         "expected %x, got %x\n", HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND), ret);
1459
1460     /* create a directory inside a not existing directory */
1461     testpath[0] = '\0';
1462     ret = pSHGetFolderPathAndSubDirA(NULL, CSIDL_FLAG_CREATE | CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, winetemp, testpath);
1463     ok(S_OK == ret, "expected S_OK, got %x\n", ret);
1464     ok(!strncmp(appdata, testpath, strlen(appdata)),
1465         "expected %s to start with %s\n", testpath, appdata);
1466     ok(!lstrcmpA(&testpath[1 + strlen(appdata)], winetemp),
1467         "expected %s to end with %s\n", testpath, winetemp);
1468     dwret = GetFileAttributes(testpath);
1469     ok(FILE_ATTRIBUTE_DIRECTORY | dwret, "expected %x to contain FILE_ATTRIBUTE_DIRECTORY\n", dwret);
1470
1471     /* cleanup */
1472     sprintf(testpath, "%s\\%s", appdata, winetemp);
1473     RemoveDirectoryA(testpath);
1474     sprintf(testpath, "%s\\%s", appdata, wine);
1475     RemoveDirectoryA(testpath);
1476 }
1477
1478
1479 START_TEST(shlfolder)
1480 {
1481     init_function_pointers();
1482     /* if OleInitialize doesn't get called, ParseDisplayName returns
1483        CO_E_NOTINITIALIZED for malformed directory names on win2k. */
1484     OleInitialize(NULL);
1485
1486     test_ParseDisplayName();
1487     test_BindToObject();
1488     test_EnumObjects_and_CompareIDs();
1489     test_GetDisplayName();
1490     test_GetAttributesOf();
1491     test_SHGetPathFromIDList();
1492     test_CallForAttributes();
1493     test_FolderShortcut();
1494     test_ITEMIDLIST_format();
1495     if(pSHGetFolderPathAndSubDirA)
1496         testSHGetFolderPathAndSubDirA();
1497     else
1498         skip("SHGetFolderPathAndSubDirA not present\n");
1499
1500     OleUninitialize();
1501 }