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