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