shell32/tests: Make tests run again on win95 and NT.
[wine] / dlls / shell32 / shellpath.c
1 /*
2  * Path Functions
3  *
4  * Copyright 1998, 1999, 2000 Juergen Schmied
5  * Copyright 2004 Juan Lang
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  *
21  * NOTES:
22  *
23  * Many of these functions are in SHLWAPI.DLL also
24  *
25  */
26
27 #include "config.h"
28 #include "wine/port.h"
29
30 #include <stdio.h>
31 #include <stdarg.h>
32 #include <string.h>
33 #include <ctype.h>
34 #include "wine/debug.h"
35 #include "windef.h"
36 #include "winbase.h"
37 #include "winnls.h"
38 #include "winreg.h"
39 #include "wingdi.h"
40 #include "winuser.h"
41
42 #include "shlobj.h"
43 #include "shresdef.h"
44 #include "shell32_main.h"
45 #include "undocshell.h"
46 #include "pidl.h"
47 #include "wine/unicode.h"
48 #include "shlwapi.h"
49
50 WINE_DEFAULT_DEBUG_CHANNEL(shell);
51
52 /*
53         ########## Combining and Constructing paths ##########
54 */
55
56 /*************************************************************************
57  * PathAppend           [SHELL32.36]
58  */
59 BOOL WINAPI PathAppendAW(
60         LPVOID lpszPath1,
61         LPCVOID lpszPath2)
62 {
63         if (SHELL_OsIsUnicode())
64           return PathAppendW(lpszPath1, lpszPath2);
65         return PathAppendA(lpszPath1, lpszPath2);
66 }
67
68 /*************************************************************************
69  * PathCombine   [SHELL32.37]
70  */
71 LPVOID WINAPI PathCombineAW(
72         LPVOID szDest,
73         LPCVOID lpszDir,
74         LPCVOID lpszFile)
75 {
76         if (SHELL_OsIsUnicode())
77           return PathCombineW( szDest, lpszDir, lpszFile );
78         return PathCombineA( szDest, lpszDir, lpszFile );
79 }
80
81 /*************************************************************************
82  * PathAddBackslash             [SHELL32.32]
83  */
84 LPVOID WINAPI PathAddBackslashAW(LPVOID lpszPath)
85 {
86         if(SHELL_OsIsUnicode())
87           return PathAddBackslashW(lpszPath);
88         return PathAddBackslashA(lpszPath);
89 }
90
91 /*************************************************************************
92  * PathBuildRoot                [SHELL32.30]
93  */
94 LPVOID WINAPI PathBuildRootAW(LPVOID lpszPath, int drive)
95 {
96         if(SHELL_OsIsUnicode())
97           return PathBuildRootW(lpszPath, drive);
98         return PathBuildRootA(lpszPath, drive);
99 }
100
101 /*
102         Extracting Component Parts
103 */
104
105 /*************************************************************************
106  * PathFindFileName     [SHELL32.34]
107  */
108 LPVOID WINAPI PathFindFileNameAW(LPCVOID lpszPath)
109 {
110         if(SHELL_OsIsUnicode())
111           return PathFindFileNameW(lpszPath);
112         return PathFindFileNameA(lpszPath);
113 }
114
115 /*************************************************************************
116  * PathFindExtension            [SHELL32.31]
117  */
118 LPVOID WINAPI PathFindExtensionAW(LPCVOID lpszPath)
119 {
120         if (SHELL_OsIsUnicode())
121           return PathFindExtensionW(lpszPath);
122         return PathFindExtensionA(lpszPath);
123
124 }
125
126 /*************************************************************************
127  * PathGetExtensionA            [internal]
128  *
129  * NOTES
130  *  exported by ordinal
131  *  return value points to the first char after the dot
132  */
133 static LPSTR PathGetExtensionA(LPCSTR lpszPath)
134 {
135         TRACE("(%s)\n",lpszPath);
136
137         lpszPath = PathFindExtensionA(lpszPath);
138         return (LPSTR)(*lpszPath?(lpszPath+1):lpszPath);
139 }
140
141 /*************************************************************************
142  * PathGetExtensionW            [internal]
143  */
144 static LPWSTR PathGetExtensionW(LPCWSTR lpszPath)
145 {
146         TRACE("(%s)\n",debugstr_w(lpszPath));
147
148         lpszPath = PathFindExtensionW(lpszPath);
149         return (LPWSTR)(*lpszPath?(lpszPath+1):lpszPath);
150 }
151
152 /*************************************************************************
153  * PathGetExtension             [SHELL32.158]
154  */
155 LPVOID WINAPI PathGetExtensionAW(LPCVOID lpszPath,DWORD void1, DWORD void2)
156 {
157         if (SHELL_OsIsUnicode())
158           return PathGetExtensionW(lpszPath);
159         return PathGetExtensionA(lpszPath);
160 }
161
162 /*************************************************************************
163  * PathGetArgs  [SHELL32.52]
164  */
165 LPVOID WINAPI PathGetArgsAW(LPVOID lpszPath)
166 {
167         if (SHELL_OsIsUnicode())
168           return PathGetArgsW(lpszPath);
169         return PathGetArgsA(lpszPath);
170 }
171
172 /*************************************************************************
173  * PathGetDriveNumber   [SHELL32.57]
174  */
175 int WINAPI PathGetDriveNumberAW(LPVOID lpszPath)
176 {
177         if (SHELL_OsIsUnicode())
178           return PathGetDriveNumberW(lpszPath);
179         return PathGetDriveNumberA(lpszPath);
180 }
181
182 /*************************************************************************
183  * PathRemoveFileSpec [SHELL32.35]
184  */
185 BOOL WINAPI PathRemoveFileSpecAW(LPVOID lpszPath)
186 {
187         if (SHELL_OsIsUnicode())
188           return PathRemoveFileSpecW(lpszPath);
189         return PathRemoveFileSpecA(lpszPath);
190 }
191
192 /*************************************************************************
193  * PathStripPath        [SHELL32.38]
194  */
195 void WINAPI PathStripPathAW(LPVOID lpszPath)
196 {
197         if (SHELL_OsIsUnicode())
198             PathStripPathW(lpszPath);
199         else
200             PathStripPathA(lpszPath);
201 }
202
203 /*************************************************************************
204  * PathStripToRoot      [SHELL32.50]
205  */
206 BOOL WINAPI PathStripToRootAW(LPVOID lpszPath)
207 {
208         if (SHELL_OsIsUnicode())
209           return PathStripToRootW(lpszPath);
210         return PathStripToRootA(lpszPath);
211 }
212
213 /*************************************************************************
214  * PathRemoveArgs       [SHELL32.251]
215  */
216 void WINAPI PathRemoveArgsAW(LPVOID lpszPath)
217 {
218         if (SHELL_OsIsUnicode())
219             PathRemoveArgsW(lpszPath);
220         else
221             PathRemoveArgsA(lpszPath);
222 }
223
224 /*************************************************************************
225  * PathRemoveExtension  [SHELL32.250]
226  */
227 void WINAPI PathRemoveExtensionAW(LPVOID lpszPath)
228 {
229         if (SHELL_OsIsUnicode())
230             PathRemoveExtensionW(lpszPath);
231         else
232             PathRemoveExtensionA(lpszPath);
233 }
234
235
236 /*
237         Path Manipulations
238 */
239
240 /*************************************************************************
241  * PathGetShortPathA [internal]
242  */
243 static void PathGetShortPathA(LPSTR pszPath)
244 {
245         CHAR path[MAX_PATH];
246
247         TRACE("%s\n", pszPath);
248
249         if (GetShortPathNameA(pszPath, path, MAX_PATH))
250         {
251           lstrcpyA(pszPath, path);
252         }
253 }
254
255 /*************************************************************************
256  * PathGetShortPathW [internal]
257  */
258 static void PathGetShortPathW(LPWSTR pszPath)
259 {
260         WCHAR path[MAX_PATH];
261
262         TRACE("%s\n", debugstr_w(pszPath));
263
264         if (GetShortPathNameW(pszPath, path, MAX_PATH))
265         {
266           lstrcpyW(pszPath, path);
267         }
268 }
269
270 /*************************************************************************
271  * PathGetShortPath [SHELL32.92]
272  */
273 VOID WINAPI PathGetShortPathAW(LPVOID pszPath)
274 {
275         if(SHELL_OsIsUnicode())
276           PathGetShortPathW(pszPath);
277         PathGetShortPathA(pszPath);
278 }
279
280 /*************************************************************************
281  * PathRemoveBlanks [SHELL32.33]
282  */
283 void WINAPI PathRemoveBlanksAW(LPVOID str)
284 {
285         if(SHELL_OsIsUnicode())
286             PathRemoveBlanksW(str);
287         else
288             PathRemoveBlanksA(str);
289 }
290
291 /*************************************************************************
292  * PathQuoteSpaces [SHELL32.55]
293  */
294 VOID WINAPI PathQuoteSpacesAW (LPVOID lpszPath)
295 {
296         if(SHELL_OsIsUnicode())
297             PathQuoteSpacesW(lpszPath);
298         else
299             PathQuoteSpacesA(lpszPath);
300 }
301
302 /*************************************************************************
303  * PathUnquoteSpaces [SHELL32.56]
304  */
305 VOID WINAPI PathUnquoteSpacesAW(LPVOID str)
306 {
307         if(SHELL_OsIsUnicode())
308           PathUnquoteSpacesW(str);
309         else
310           PathUnquoteSpacesA(str);
311 }
312
313 /*************************************************************************
314  * PathParseIconLocation        [SHELL32.249]
315  */
316 int WINAPI PathParseIconLocationAW (LPVOID lpszPath)
317 {
318         if(SHELL_OsIsUnicode())
319           return PathParseIconLocationW(lpszPath);
320         return PathParseIconLocationA(lpszPath);
321 }
322
323 /*
324         ########## Path Testing ##########
325 */
326 /*************************************************************************
327  * PathIsUNC            [SHELL32.39]
328  */
329 BOOL WINAPI PathIsUNCAW (LPCVOID lpszPath)
330 {
331         if (SHELL_OsIsUnicode())
332           return PathIsUNCW( lpszPath );
333         return PathIsUNCA( lpszPath );
334 }
335
336 /*************************************************************************
337  *  PathIsRelative      [SHELL32.40]
338  */
339 BOOL WINAPI PathIsRelativeAW (LPCVOID lpszPath)
340 {
341         if (SHELL_OsIsUnicode())
342           return PathIsRelativeW( lpszPath );
343         return PathIsRelativeA( lpszPath );
344 }
345
346 /*************************************************************************
347  * PathIsRoot           [SHELL32.29]
348  */
349 BOOL WINAPI PathIsRootAW(LPCVOID lpszPath)
350 {
351         if (SHELL_OsIsUnicode())
352           return PathIsRootW(lpszPath);
353         return PathIsRootA(lpszPath);
354 }
355
356 /*************************************************************************
357  *  PathIsExeA          [internal]
358  */
359 static BOOL PathIsExeA (LPCSTR lpszPath)
360 {
361         LPCSTR lpszExtension = PathGetExtensionA(lpszPath);
362         int i;
363         static const char * const lpszExtensions[] =
364             {"exe", "com", "pif", "cmd", "bat", "scf", "scr", NULL };
365
366         TRACE("path=%s\n",lpszPath);
367
368         for(i=0; lpszExtensions[i]; i++)
369           if (!lstrcmpiA(lpszExtension,lpszExtensions[i])) return TRUE;
370
371         return FALSE;
372 }
373
374 /*************************************************************************
375  *  PathIsExeW          [internal]
376  */
377 static BOOL PathIsExeW (LPCWSTR lpszPath)
378 {
379         LPCWSTR lpszExtension = PathGetExtensionW(lpszPath);
380         int i;
381         static const WCHAR lpszExtensions[][4] =
382             {{'e','x','e','\0'}, {'c','o','m','\0'}, {'p','i','f','\0'},
383              {'c','m','d','\0'}, {'b','a','t','\0'}, {'s','c','f','\0'},
384              {'s','c','r','\0'}, {'\0'} };
385
386         TRACE("path=%s\n",debugstr_w(lpszPath));
387
388         for(i=0; lpszExtensions[i][0]; i++)
389           if (!strcmpiW(lpszExtension,lpszExtensions[i])) return TRUE;
390
391         return FALSE;
392 }
393
394 /*************************************************************************
395  *  PathIsExe           [SHELL32.43]
396  */
397 BOOL WINAPI PathIsExeAW (LPCVOID path)
398 {
399         if (SHELL_OsIsUnicode())
400           return PathIsExeW (path);
401         return PathIsExeA(path);
402 }
403
404 /*************************************************************************
405  * PathIsDirectory      [SHELL32.159]
406  */
407 BOOL WINAPI PathIsDirectoryAW (LPCVOID lpszPath)
408 {
409         if (SHELL_OsIsUnicode())
410           return PathIsDirectoryW (lpszPath);
411         return PathIsDirectoryA (lpszPath);
412 }
413
414 /*************************************************************************
415  * PathFileExists       [SHELL32.45]
416  */
417 BOOL WINAPI PathFileExistsAW (LPCVOID lpszPath)
418 {
419         if (SHELL_OsIsUnicode())
420           return PathFileExistsW (lpszPath);
421         return PathFileExistsA (lpszPath);
422 }
423
424 /*************************************************************************
425  * PathMatchSpec        [SHELL32.46]
426  */
427 BOOL WINAPI PathMatchSpecAW(LPVOID name, LPVOID mask)
428 {
429         if (SHELL_OsIsUnicode())
430           return PathMatchSpecW( name, mask );
431         return PathMatchSpecA( name, mask );
432 }
433
434 /*************************************************************************
435  * PathIsSameRoot       [SHELL32.650]
436  */
437 BOOL WINAPI PathIsSameRootAW(LPCVOID lpszPath1, LPCVOID lpszPath2)
438 {
439         if (SHELL_OsIsUnicode())
440           return PathIsSameRootW(lpszPath1, lpszPath2);
441         return PathIsSameRootA(lpszPath1, lpszPath2);
442 }
443
444 /*************************************************************************
445  * IsLFNDriveA          [SHELL32.41]
446  */
447 BOOL WINAPI IsLFNDriveA(LPCSTR lpszPath)
448 {
449     DWORD       fnlen;
450
451     if (!GetVolumeInformationA(lpszPath, NULL, 0, NULL, &fnlen, NULL, NULL, 0))
452         return FALSE;
453     return fnlen > 12;
454 }
455
456 /*************************************************************************
457  * IsLFNDriveW          [SHELL32.42]
458  */
459 BOOL WINAPI IsLFNDriveW(LPCWSTR lpszPath)
460 {
461     DWORD       fnlen;
462
463     if (!GetVolumeInformationW(lpszPath, NULL, 0, NULL, &fnlen, NULL, NULL, 0))
464         return FALSE;
465     return fnlen > 12;
466 }
467
468 /*************************************************************************
469  * IsLFNDrive           [SHELL32.119]
470  */
471 BOOL WINAPI IsLFNDriveAW(LPCVOID lpszPath)
472 {
473         if (SHELL_OsIsUnicode())
474           return IsLFNDriveW(lpszPath);
475         return IsLFNDriveA(lpszPath);
476 }
477
478 /*
479         ########## Creating Something Unique ##########
480 */
481 /*************************************************************************
482  * PathMakeUniqueNameA  [internal]
483  */
484 BOOL WINAPI PathMakeUniqueNameA(
485         LPSTR lpszBuffer,
486         DWORD dwBuffSize,
487         LPCSTR lpszShortName,
488         LPCSTR lpszLongName,
489         LPCSTR lpszPathName)
490 {
491         FIXME("%p %u %s %s %s stub\n",
492          lpszBuffer, dwBuffSize, debugstr_a(lpszShortName),
493          debugstr_a(lpszLongName), debugstr_a(lpszPathName));
494         return TRUE;
495 }
496
497 /*************************************************************************
498  * PathMakeUniqueNameW  [internal]
499  */
500 BOOL WINAPI PathMakeUniqueNameW(
501         LPWSTR lpszBuffer,
502         DWORD dwBuffSize,
503         LPCWSTR lpszShortName,
504         LPCWSTR lpszLongName,
505         LPCWSTR lpszPathName)
506 {
507         FIXME("%p %u %s %s %s stub\n",
508          lpszBuffer, dwBuffSize, debugstr_w(lpszShortName),
509          debugstr_w(lpszLongName), debugstr_w(lpszPathName));
510         return TRUE;
511 }
512
513 /*************************************************************************
514  * PathMakeUniqueName   [SHELL32.47]
515  */
516 BOOL WINAPI PathMakeUniqueNameAW(
517         LPVOID lpszBuffer,
518         DWORD dwBuffSize,
519         LPCVOID lpszShortName,
520         LPCVOID lpszLongName,
521         LPCVOID lpszPathName)
522 {
523         if (SHELL_OsIsUnicode())
524           return PathMakeUniqueNameW(lpszBuffer,dwBuffSize, lpszShortName,lpszLongName,lpszPathName);
525         return PathMakeUniqueNameA(lpszBuffer,dwBuffSize, lpszShortName,lpszLongName,lpszPathName);
526 }
527
528 /*************************************************************************
529  * PathYetAnotherMakeUniqueName [SHELL32.75]
530  *
531  * NOTES
532  *     exported by ordinal
533  */
534 BOOL WINAPI PathYetAnotherMakeUniqueName(
535         LPWSTR lpszBuffer,
536         LPCWSTR lpszPathName,
537         LPCWSTR lpszShortName,
538         LPCWSTR lpszLongName)
539 {
540     FIXME("(%p, %s, %s ,%s):stub.\n",
541           lpszBuffer, debugstr_w(lpszPathName), debugstr_w(lpszShortName), debugstr_w(lpszLongName));
542     return TRUE;
543 }
544
545
546 /*
547         ########## cleaning and resolving paths ##########
548  */
549
550 /*************************************************************************
551  * PathFindOnPath       [SHELL32.145]
552  */
553 BOOL WINAPI PathFindOnPathAW(LPVOID sFile, LPCVOID *sOtherDirs)
554 {
555         if (SHELL_OsIsUnicode())
556           return PathFindOnPathW(sFile, (LPCWSTR *)sOtherDirs);
557         return PathFindOnPathA(sFile, (LPCSTR *)sOtherDirs);
558 }
559
560 /*************************************************************************
561  * PathCleanupSpec      [SHELL32.171]
562  *
563  * lpszFile is changed in place.
564  */
565 int WINAPI PathCleanupSpec( LPCWSTR lpszPathW, LPWSTR lpszFileW )
566 {
567     int i = 0;
568     DWORD rc = 0;
569     int length = 0;
570
571     if (SHELL_OsIsUnicode())
572     {
573         LPWSTR p = lpszFileW;
574
575         TRACE("Cleanup %s\n",debugstr_w(lpszFileW));
576
577         if (lpszPathW)
578             length = strlenW(lpszPathW);
579
580         while (*p)
581         {
582             int gct = PathGetCharTypeW(*p);
583             if (gct == GCT_INVALID || gct == GCT_WILD || gct == GCT_SEPARATOR)
584             {
585                 lpszFileW[i]='-';
586                 rc |= PCS_REPLACEDCHAR;
587             }
588             else
589                 lpszFileW[i]=*p;
590             i++;
591             p++;
592             if (length + i == MAX_PATH)
593             {
594                 rc |= PCS_FATAL | PCS_PATHTOOLONG;
595                 break;
596             }
597         }
598         lpszFileW[i]=0;
599     }
600     else
601     {
602         LPSTR lpszFileA = (LPSTR)lpszFileW;
603         LPCSTR lpszPathA = (LPCSTR)lpszPathW;
604         LPSTR p = lpszFileA;
605
606         TRACE("Cleanup %s\n",debugstr_a(lpszFileA));
607
608         if (lpszPathA)
609             length = strlen(lpszPathA);
610
611         while (*p)
612         {
613             int gct = PathGetCharTypeA(*p);
614             if (gct == GCT_INVALID || gct == GCT_WILD || gct == GCT_SEPARATOR)
615             {
616                 lpszFileA[i]='-';
617                 rc |= PCS_REPLACEDCHAR;
618             }
619             else
620                 lpszFileA[i]=*p;
621             i++;
622             p++;
623             if (length + i == MAX_PATH)
624             {
625                 rc |= PCS_FATAL | PCS_PATHTOOLONG;
626                 break;
627             }
628         }
629         lpszFileA[i]=0;
630     }
631     return rc;
632 }
633
634 /*************************************************************************
635  * PathQualifyA         [SHELL32]
636  */
637 BOOL WINAPI PathQualifyA(LPCSTR pszPath)
638 {
639         FIXME("%s\n",pszPath);
640         return 0;
641 }
642
643 /*************************************************************************
644  * PathQualifyW         [SHELL32]
645  */
646 BOOL WINAPI PathQualifyW(LPCWSTR pszPath)
647 {
648         FIXME("%s\n",debugstr_w(pszPath));
649         return 0;
650 }
651
652 /*************************************************************************
653  * PathQualify  [SHELL32.49]
654  */
655 BOOL WINAPI PathQualifyAW(LPCVOID pszPath)
656 {
657         if (SHELL_OsIsUnicode())
658           return PathQualifyW(pszPath);
659         return PathQualifyA(pszPath);
660 }
661
662 /*************************************************************************
663  * PathResolveA [SHELL32.51]
664  */
665 BOOL WINAPI PathResolveA(
666         LPSTR lpszPath,
667         LPCSTR *alpszPaths,
668         DWORD dwFlags)
669 {
670         FIXME("(%s,%p,0x%08x),stub!\n",
671           lpszPath, *alpszPaths, dwFlags);
672         return 0;
673 }
674
675 /*************************************************************************
676  * PathResolveW [SHELL32]
677  */
678 BOOL WINAPI PathResolveW(
679         LPWSTR lpszPath,
680         LPCWSTR *alpszPaths,
681         DWORD dwFlags)
682 {
683         FIXME("(%s,%p,0x%08x),stub!\n",
684           debugstr_w(lpszPath), debugstr_w(*alpszPaths), dwFlags);
685         return 0;
686 }
687
688 /*************************************************************************
689  * PathResolve [SHELL32.51]
690  */
691 BOOL WINAPI PathResolveAW(
692         LPVOID lpszPath,
693         LPCVOID *alpszPaths,
694         DWORD dwFlags)
695 {
696         if (SHELL_OsIsUnicode())
697           return PathResolveW(lpszPath, (LPCWSTR*)alpszPaths, dwFlags);
698         return PathResolveA(lpszPath, (LPCSTR*)alpszPaths, dwFlags);
699 }
700
701 /*************************************************************************
702 *       PathProcessCommandA     [SHELL32.653]
703 */
704 LONG WINAPI PathProcessCommandA (
705         LPCSTR lpszPath,
706         LPSTR lpszBuff,
707         DWORD dwBuffSize,
708         DWORD dwFlags)
709 {
710         FIXME("%s %p 0x%04x 0x%04x stub\n",
711         lpszPath, lpszBuff, dwBuffSize, dwFlags);
712         if(!lpszPath) return -1;
713         if(lpszBuff) strcpy(lpszBuff, lpszPath);
714         return strlen(lpszPath);
715 }
716
717 /*************************************************************************
718 *       PathProcessCommandW
719 */
720 LONG WINAPI PathProcessCommandW (
721         LPCWSTR lpszPath,
722         LPWSTR lpszBuff,
723         DWORD dwBuffSize,
724         DWORD dwFlags)
725 {
726         FIXME("(%s, %p, 0x%04x, 0x%04x) stub\n",
727         debugstr_w(lpszPath), lpszBuff, dwBuffSize, dwFlags);
728         if(!lpszPath) return -1;
729         if(lpszBuff) strcpyW(lpszBuff, lpszPath);
730         return strlenW(lpszPath);
731 }
732
733 /*************************************************************************
734 *       PathProcessCommand (SHELL32.653)
735 */
736 LONG WINAPI PathProcessCommandAW (
737         LPCVOID lpszPath,
738         LPVOID lpszBuff,
739         DWORD dwBuffSize,
740         DWORD dwFlags)
741 {
742         if (SHELL_OsIsUnicode())
743           return PathProcessCommandW(lpszPath, lpszBuff, dwBuffSize, dwFlags);
744         return PathProcessCommandA(lpszPath, lpszBuff, dwBuffSize, dwFlags);
745 }
746
747 /*
748         ########## special ##########
749 */
750
751 /*************************************************************************
752  * PathSetDlgItemPath (SHELL32.48)
753  */
754 VOID WINAPI PathSetDlgItemPathAW(HWND hDlg, int id, LPCVOID pszPath)
755 {
756         if (SHELL_OsIsUnicode())
757             PathSetDlgItemPathW(hDlg, id, pszPath);
758         else
759             PathSetDlgItemPathA(hDlg, id, pszPath);
760 }
761
762 static const WCHAR szCurrentVersion[] = {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\0'};
763 static const WCHAR Administrative_ToolsW[] = {'A','d','m','i','n','i','s','t','r','a','t','i','v','e',' ','T','o','o','l','s','\0'};
764 static const WCHAR AppDataW[] = {'A','p','p','D','a','t','a','\0'};
765 static const WCHAR CacheW[] = {'C','a','c','h','e','\0'};
766 static const WCHAR CD_BurningW[] = {'C','D',' ','B','u','r','n','i','n','g','\0'};
767 static const WCHAR Common_Administrative_ToolsW[] = {'C','o','m','m','o','n',' ','A','d','m','i','n','i','s','t','r','a','t','i','v','e',' ','T','o','o','l','s','\0'};
768 static const WCHAR Common_AppDataW[] = {'C','o','m','m','o','n',' ','A','p','p','D','a','t','a','\0'};
769 static const WCHAR Common_DesktopW[] = {'C','o','m','m','o','n',' ','D','e','s','k','t','o','p','\0'};
770 static const WCHAR Common_DocumentsW[] = {'C','o','m','m','o','n',' ','D','o','c','u','m','e','n','t','s','\0'};
771 static const WCHAR CommonFilesDirW[] = {'C','o','m','m','o','n','F','i','l','e','s','D','i','r','\0'};
772 static const WCHAR CommonMusicW[] = {'C','o','m','m','o','n','M','u','s','i','c','\0'};
773 static const WCHAR CommonPicturesW[] = {'C','o','m','m','o','n','P','i','c','t','u','r','e','s','\0'};
774 static const WCHAR Common_ProgramsW[] = {'C','o','m','m','o','n',' ','P','r','o','g','r','a','m','s','\0'};
775 static const WCHAR Common_StartUpW[] = {'C','o','m','m','o','n',' ','S','t','a','r','t','U','p','\0'};
776 static const WCHAR Common_Start_MenuW[] = {'C','o','m','m','o','n',' ','S','t','a','r','t',' ','M','e','n','u','\0'};
777 static const WCHAR Common_TemplatesW[] = {'C','o','m','m','o','n',' ','T','e','m','p','l','a','t','e','s','\0'};
778 static const WCHAR CommonVideoW[] = {'C','o','m','m','o','n','V','i','d','e','o','\0'};
779 static const WCHAR CookiesW[] = {'C','o','o','k','i','e','s','\0'};
780 static const WCHAR DesktopW[] = {'D','e','s','k','t','o','p','\0'};
781 static const WCHAR FavoritesW[] = {'F','a','v','o','r','i','t','e','s','\0'};
782 static const WCHAR FontsW[] = {'F','o','n','t','s','\0'};
783 static const WCHAR HistoryW[] = {'H','i','s','t','o','r','y','\0'};
784 static const WCHAR Local_AppDataW[] = {'L','o','c','a','l',' ','A','p','p','D','a','t','a','\0'};
785 static const WCHAR My_MusicW[] = {'M','y',' ','M','u','s','i','c','\0'};
786 static const WCHAR My_PicturesW[] = {'M','y',' ','P','i','c','t','u','r','e','s','\0'};
787 static const WCHAR My_VideoW[] = {'M','y',' ','V','i','d','e','o','s','\0'};
788 static const WCHAR NetHoodW[] = {'N','e','t','H','o','o','d','\0'};
789 static const WCHAR PersonalW[] = {'P','e','r','s','o','n','a','l','\0'};
790 static const WCHAR PrintHoodW[] = {'P','r','i','n','t','H','o','o','d','\0'};
791 static const WCHAR ProgramFilesDirW[] = {'P','r','o','g','r','a','m','F','i','l','e','s','D','i','r','\0'};
792 static const WCHAR ProgramsW[] = {'P','r','o','g','r','a','m','s','\0'};
793 static const WCHAR RecentW[] = {'R','e','c','e','n','t','\0'};
794 static const WCHAR ResourcesW[] = {'R','e','s','o','u','r','c','e','s','\0'};
795 static const WCHAR SendToW[] = {'S','e','n','d','T','o','\0'};
796 static const WCHAR StartUpW[] = {'S','t','a','r','t','U','p','\0'};
797 static const WCHAR Start_MenuW[] = {'S','t','a','r','t',' ','M','e','n','u','\0'};
798 static const WCHAR TemplatesW[] = {'T','e','m','p','l','a','t','e','s','\0'};
799 static const WCHAR DefaultW[] = {'.','D','e','f','a','u','l','t','\0'};
800 static const WCHAR AllUsersProfileW[] = {'%','A','L','L','U','S','E','R','S','P','R','O','F','I','L','E','%','\0'};
801 static const WCHAR UserProfileW[] = {'%','U','S','E','R','P','R','O','F','I','L','E','%','\0'};
802 static const WCHAR SystemDriveW[] = {'%','S','y','s','t','e','m','D','r','i','v','e','%','\0'};
803 static const WCHAR ProfileListW[] = {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','P','r','o','f','i','l','e','L','i','s','t',0};
804 static const WCHAR ProfilesDirectoryW[] = {'P','r','o','f','i','l','e','s','D','i','r','e','c','t','o','r','y',0};
805 static const WCHAR AllUsersProfileValueW[] = {'A','l','l','U','s','e','r','s','P','r','o','f','i','l','e','\0'};
806 static const WCHAR szSHFolders[] = {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','E','x','p','l','o','r','e','r','\\','S','h','e','l','l',' ','F','o','l','d','e','r','s','\0'};
807 static const WCHAR szSHUserFolders[] = {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','E','x','p','l','o','r','e','r','\\','U','s','e','r',' ','S','h','e','l','l',' ','F','o','l','d','e','r','s','\0'};
808 /* This defaults to L"Documents and Settings" on Windows 2000/XP, but we're
809  * acting more Windows 9x-like for now.
810  */
811 static const WCHAR szDefaultProfileDirW[] = {'p','r','o','f','i','l','e','s','\0'};
812 static const WCHAR AllUsersW[] = {'A','l','l',' ','U','s','e','r','s','\0'};
813
814 typedef enum _CSIDL_Type {
815     CSIDL_Type_User,
816     CSIDL_Type_AllUsers,
817     CSIDL_Type_CurrVer,
818     CSIDL_Type_Disallowed,
819     CSIDL_Type_NonExistent,
820     CSIDL_Type_WindowsPath,
821     CSIDL_Type_SystemPath,
822 } CSIDL_Type;
823
824 typedef struct
825 {
826     CSIDL_Type type;
827     LPCWSTR    szValueName;
828     LPCWSTR    szDefaultPath; /* fallback string or resource ID */
829 } CSIDL_DATA;
830
831 static const CSIDL_DATA CSIDL_Data[] =
832 {
833     { /* 0x00 - CSIDL_DESKTOP */
834         CSIDL_Type_User,
835         DesktopW,
836         MAKEINTRESOURCEW(IDS_DESKTOPDIRECTORY)
837     },
838     { /* 0x01 - CSIDL_INTERNET */
839         CSIDL_Type_Disallowed,
840         NULL,
841         NULL
842     },
843     { /* 0x02 - CSIDL_PROGRAMS */
844         CSIDL_Type_User,
845         ProgramsW,
846         MAKEINTRESOURCEW(IDS_PROGRAMS)
847     },
848     { /* 0x03 - CSIDL_CONTROLS (.CPL files) */
849         CSIDL_Type_SystemPath,
850         NULL,
851         NULL
852     },
853     { /* 0x04 - CSIDL_PRINTERS */
854         CSIDL_Type_SystemPath,
855         NULL,
856         NULL
857     },
858     { /* 0x05 - CSIDL_PERSONAL */
859         CSIDL_Type_User,
860         PersonalW,
861         MAKEINTRESOURCEW(IDS_PERSONAL)
862     },
863     { /* 0x06 - CSIDL_FAVORITES */
864         CSIDL_Type_User,
865         FavoritesW,
866         MAKEINTRESOURCEW(IDS_FAVORITES)
867     },
868     { /* 0x07 - CSIDL_STARTUP */
869         CSIDL_Type_User,
870         StartUpW,
871         MAKEINTRESOURCEW(IDS_STARTUP)
872     },
873     { /* 0x08 - CSIDL_RECENT */
874         CSIDL_Type_User,
875         RecentW,
876         MAKEINTRESOURCEW(IDS_RECENT)
877     },
878     { /* 0x09 - CSIDL_SENDTO */
879         CSIDL_Type_User,
880         SendToW,
881         MAKEINTRESOURCEW(IDS_SENDTO)
882     },
883     { /* 0x0a - CSIDL_BITBUCKET - Recycle Bin */
884         CSIDL_Type_Disallowed,
885         NULL,
886         NULL,
887     },
888     { /* 0x0b - CSIDL_STARTMENU */
889         CSIDL_Type_User,
890         Start_MenuW,
891         MAKEINTRESOURCEW(IDS_STARTMENU)
892     },
893     { /* 0x0c - CSIDL_MYDOCUMENTS */
894         CSIDL_Type_Disallowed, /* matches WinXP--can't get its path */
895         NULL,
896         NULL
897     },
898     { /* 0x0d - CSIDL_MYMUSIC */
899         CSIDL_Type_User,
900         My_MusicW,
901         MAKEINTRESOURCEW(IDS_MYMUSIC)
902     },
903     { /* 0x0e - CSIDL_MYVIDEO */
904         CSIDL_Type_User,
905         My_VideoW,
906         MAKEINTRESOURCEW(IDS_MYVIDEO)
907     },
908     { /* 0x0f - unassigned */
909         CSIDL_Type_Disallowed,
910         NULL,
911         NULL,
912     },
913     { /* 0x10 - CSIDL_DESKTOPDIRECTORY */
914         CSIDL_Type_User,
915         DesktopW,
916         MAKEINTRESOURCEW(IDS_DESKTOPDIRECTORY)
917     },
918     { /* 0x11 - CSIDL_DRIVES */
919         CSIDL_Type_Disallowed,
920         NULL,
921         NULL,
922     },
923     { /* 0x12 - CSIDL_NETWORK */
924         CSIDL_Type_Disallowed,
925         NULL,
926         NULL,
927     },
928     { /* 0x13 - CSIDL_NETHOOD */
929         CSIDL_Type_User,
930         NetHoodW,
931         MAKEINTRESOURCEW(IDS_NETHOOD)
932     },
933     { /* 0x14 - CSIDL_FONTS */
934         CSIDL_Type_WindowsPath,
935         FontsW,
936         FontsW
937     },
938     { /* 0x15 - CSIDL_TEMPLATES */
939         CSIDL_Type_User,
940         TemplatesW,
941         MAKEINTRESOURCEW(IDS_TEMPLATES)
942     },
943     { /* 0x16 - CSIDL_COMMON_STARTMENU */
944         CSIDL_Type_AllUsers,
945         Common_Start_MenuW,
946         MAKEINTRESOURCEW(IDS_STARTMENU)
947     },
948     { /* 0x17 - CSIDL_COMMON_PROGRAMS */
949         CSIDL_Type_AllUsers,
950         Common_ProgramsW,
951         MAKEINTRESOURCEW(IDS_PROGRAMS)
952     },
953     { /* 0x18 - CSIDL_COMMON_STARTUP */
954         CSIDL_Type_AllUsers,
955         Common_StartUpW,
956         MAKEINTRESOURCEW(IDS_STARTUP)
957     },
958     { /* 0x19 - CSIDL_COMMON_DESKTOPDIRECTORY */
959         CSIDL_Type_AllUsers,
960         Common_DesktopW,
961         MAKEINTRESOURCEW(IDS_DESKTOP)
962     },
963     { /* 0x1a - CSIDL_APPDATA */
964         CSIDL_Type_User,
965         AppDataW,
966         MAKEINTRESOURCEW(IDS_APPDATA)
967     },
968     { /* 0x1b - CSIDL_PRINTHOOD */
969         CSIDL_Type_User,
970         PrintHoodW,
971         MAKEINTRESOURCEW(IDS_PRINTHOOD)
972     },
973     { /* 0x1c - CSIDL_LOCAL_APPDATA */
974         CSIDL_Type_User,
975         Local_AppDataW,
976         MAKEINTRESOURCEW(IDS_LOCAL_APPDATA)
977     },
978     { /* 0x1d - CSIDL_ALTSTARTUP */
979         CSIDL_Type_NonExistent,
980         NULL,
981         NULL
982     },
983     { /* 0x1e - CSIDL_COMMON_ALTSTARTUP */
984         CSIDL_Type_NonExistent,
985         NULL,
986         NULL
987     },
988     { /* 0x1f - CSIDL_COMMON_FAVORITES */
989         CSIDL_Type_AllUsers,
990         FavoritesW,
991         MAKEINTRESOURCEW(IDS_FAVORITES)
992     },
993     { /* 0x20 - CSIDL_INTERNET_CACHE */
994         CSIDL_Type_User,
995         CacheW,
996         MAKEINTRESOURCEW(IDS_INTERNET_CACHE)
997     },
998     { /* 0x21 - CSIDL_COOKIES */
999         CSIDL_Type_User,
1000         CookiesW,
1001         MAKEINTRESOURCEW(IDS_COOKIES)
1002     },
1003     { /* 0x22 - CSIDL_HISTORY */
1004         CSIDL_Type_User,
1005         HistoryW,
1006         MAKEINTRESOURCEW(IDS_HISTORY)
1007     },
1008     { /* 0x23 - CSIDL_COMMON_APPDATA */
1009         CSIDL_Type_AllUsers,
1010         Common_AppDataW,
1011         MAKEINTRESOURCEW(IDS_APPDATA)
1012     },
1013     { /* 0x24 - CSIDL_WINDOWS */
1014         CSIDL_Type_WindowsPath,
1015         NULL,
1016         NULL
1017     },
1018     { /* 0x25 - CSIDL_SYSTEM */
1019         CSIDL_Type_SystemPath,
1020         NULL,
1021         NULL
1022     },
1023     { /* 0x26 - CSIDL_PROGRAM_FILES */
1024         CSIDL_Type_CurrVer,
1025         ProgramFilesDirW,
1026         MAKEINTRESOURCEW(IDS_PROGRAM_FILES)
1027     },
1028     { /* 0x27 - CSIDL_MYPICTURES */
1029         CSIDL_Type_User,
1030         My_PicturesW,
1031         MAKEINTRESOURCEW(IDS_MYPICTURES)
1032     },
1033     { /* 0x28 - CSIDL_PROFILE */
1034         CSIDL_Type_User,
1035         NULL,
1036         NULL
1037     },
1038     { /* 0x29 - CSIDL_SYSTEMX86 */
1039         CSIDL_Type_NonExistent,
1040         NULL,
1041         NULL
1042     },
1043     { /* 0x2a - CSIDL_PROGRAM_FILESX86 */
1044         CSIDL_Type_NonExistent,
1045         NULL,
1046         NULL
1047     },
1048     { /* 0x2b - CSIDL_PROGRAM_FILES_COMMON */
1049         CSIDL_Type_CurrVer,
1050         CommonFilesDirW,
1051         MAKEINTRESOURCEW(IDS_PROGRAM_FILES_COMMON)
1052     },
1053     { /* 0x2c - CSIDL_PROGRAM_FILES_COMMONX86 */
1054         CSIDL_Type_NonExistent,
1055         NULL,
1056         NULL
1057     },
1058     { /* 0x2d - CSIDL_COMMON_TEMPLATES */
1059         CSIDL_Type_AllUsers,
1060         Common_TemplatesW,
1061         MAKEINTRESOURCEW(IDS_TEMPLATES)
1062     },
1063     { /* 0x2e - CSIDL_COMMON_DOCUMENTS */
1064         CSIDL_Type_AllUsers,
1065         Common_DocumentsW,
1066         MAKEINTRESOURCEW(IDS_COMMON_DOCUMENTS)
1067     },
1068     { /* 0x2f - CSIDL_COMMON_ADMINTOOLS */
1069         CSIDL_Type_AllUsers,
1070         Common_Administrative_ToolsW,
1071         MAKEINTRESOURCEW(IDS_ADMINTOOLS)
1072     },
1073     { /* 0x30 - CSIDL_ADMINTOOLS */
1074         CSIDL_Type_User,
1075         Administrative_ToolsW,
1076         MAKEINTRESOURCEW(IDS_ADMINTOOLS)
1077     },
1078     { /* 0x31 - CSIDL_CONNECTIONS */
1079         CSIDL_Type_Disallowed,
1080         NULL,
1081         NULL
1082     },
1083     { /* 0x32 - unassigned */
1084         CSIDL_Type_Disallowed,
1085         NULL,
1086         NULL
1087     },
1088     { /* 0x33 - unassigned */
1089         CSIDL_Type_Disallowed,
1090         NULL,
1091         NULL
1092     },
1093     { /* 0x34 - unassigned */
1094         CSIDL_Type_Disallowed,
1095         NULL,
1096         NULL
1097     },
1098     { /* 0x35 - CSIDL_COMMON_MUSIC */
1099         CSIDL_Type_AllUsers,
1100         CommonMusicW,
1101         MAKEINTRESOURCEW(IDS_COMMON_MUSIC)
1102     },
1103     { /* 0x36 - CSIDL_COMMON_PICTURES */
1104         CSIDL_Type_AllUsers,
1105         CommonPicturesW,
1106         MAKEINTRESOURCEW(IDS_COMMON_PICTURES)
1107     },
1108     { /* 0x37 - CSIDL_COMMON_VIDEO */
1109         CSIDL_Type_AllUsers,
1110         CommonVideoW,
1111         MAKEINTRESOURCEW(IDS_COMMON_VIDEO)
1112     },
1113     { /* 0x38 - CSIDL_RESOURCES */
1114         CSIDL_Type_WindowsPath,
1115         NULL,
1116         ResourcesW
1117     },
1118     { /* 0x39 - CSIDL_RESOURCES_LOCALIZED */
1119         CSIDL_Type_NonExistent,
1120         NULL,
1121         NULL
1122     },
1123     { /* 0x3a - CSIDL_COMMON_OEM_LINKS */
1124         CSIDL_Type_NonExistent,
1125         NULL,
1126         NULL
1127     },
1128     { /* 0x3b - CSIDL_CDBURN_AREA */
1129         CSIDL_Type_User,
1130         CD_BurningW,
1131         MAKEINTRESOURCEW(IDS_CDBURN_AREA)
1132     },
1133     { /* 0x3c unassigned */
1134         CSIDL_Type_Disallowed,
1135         NULL,
1136         NULL
1137     },
1138     { /* 0x3d - CSIDL_COMPUTERSNEARME */
1139         CSIDL_Type_Disallowed, /* FIXME */
1140         NULL,
1141         NULL
1142     },
1143     { /* 0x3e - CSIDL_PROFILES */
1144         CSIDL_Type_Disallowed, /* oddly, this matches WinXP */
1145         NULL,
1146         NULL
1147     }
1148 };
1149
1150 static HRESULT _SHExpandEnvironmentStrings(LPCWSTR szSrc, LPWSTR szDest);
1151
1152 /* Gets the value named value from the registry key
1153  * rootKey\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
1154  * (or from rootKey\userPrefix\... if userPrefix is not NULL) into path, which
1155  * is assumed to be MAX_PATH WCHARs in length.
1156  * If it exists, expands the value and writes the expanded value to
1157  * rootKey\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders
1158  * Returns successful error code if the value was retrieved from the registry,
1159  * and a failure otherwise.
1160  */
1161 static HRESULT _SHGetUserShellFolderPath(HKEY rootKey, LPCWSTR userPrefix,
1162  LPCWSTR value, LPWSTR path)
1163 {
1164     HRESULT hr;
1165     WCHAR shellFolderPath[MAX_PATH], userShellFolderPath[MAX_PATH];
1166     LPCWSTR pShellFolderPath, pUserShellFolderPath;
1167     DWORD dwType, dwPathLen = MAX_PATH;
1168     HKEY userShellFolderKey, shellFolderKey;
1169
1170     TRACE("%p,%s,%s,%p\n",rootKey, debugstr_w(userPrefix), debugstr_w(value),
1171      path);
1172
1173     if (userPrefix)
1174     {
1175         strcpyW(shellFolderPath, userPrefix);
1176         PathAddBackslashW(shellFolderPath);
1177         strcatW(shellFolderPath, szSHFolders);
1178         pShellFolderPath = shellFolderPath;
1179         strcpyW(userShellFolderPath, userPrefix);
1180         PathAddBackslashW(userShellFolderPath);
1181         strcatW(userShellFolderPath, szSHUserFolders);
1182         pUserShellFolderPath = userShellFolderPath;
1183     }
1184     else
1185     {
1186         pUserShellFolderPath = szSHUserFolders;
1187         pShellFolderPath = szSHFolders;
1188     }
1189
1190     if (RegCreateKeyW(rootKey, pShellFolderPath, &shellFolderKey))
1191     {
1192         TRACE("Failed to create %s\n", debugstr_w(pShellFolderPath));
1193         return E_FAIL;
1194     }
1195     if (RegCreateKeyW(rootKey, pUserShellFolderPath, &userShellFolderKey))
1196     {
1197         TRACE("Failed to create %s\n",
1198          debugstr_w(pUserShellFolderPath));
1199         RegCloseKey(shellFolderKey);
1200         return E_FAIL;
1201     }
1202
1203     if (!RegQueryValueExW(userShellFolderKey, value, NULL, &dwType,
1204      (LPBYTE)path, &dwPathLen) && (dwType == REG_EXPAND_SZ || dwType == REG_SZ))
1205     {
1206         LONG ret;
1207
1208         path[dwPathLen / sizeof(WCHAR)] = '\0';
1209         if (dwType == REG_EXPAND_SZ && path[0] == '%')
1210         {
1211             WCHAR szTemp[MAX_PATH];
1212
1213             _SHExpandEnvironmentStrings(path, szTemp);
1214             lstrcpynW(path, szTemp, MAX_PATH);
1215         }
1216         ret = RegSetValueExW(shellFolderKey, value, 0, REG_SZ, (LPBYTE)path,
1217          (strlenW(path) + 1) * sizeof(WCHAR));
1218         if (ret != ERROR_SUCCESS)
1219             hr = HRESULT_FROM_WIN32(ret);
1220         else
1221             hr = S_OK;
1222     }
1223     else
1224         hr = E_FAIL;
1225     RegCloseKey(shellFolderKey);
1226     RegCloseKey(userShellFolderKey);
1227     TRACE("returning 0x%08x\n", hr);
1228     return hr;
1229 }
1230
1231 /* Gets a 'semi-expanded' default value of the CSIDL with index folder into
1232  * pszPath, based on the entries in CSIDL_Data.  By semi-expanded, I mean:
1233  * - The entry's szDefaultPath may be either a string value or an integer
1234  *   resource identifier.  In the latter case, the string value of the resource
1235  *   is written.
1236  * - Depending on the entry's type, the path may begin with an (unexpanded)
1237  *   environment variable name.  The caller is responsible for expanding
1238  *   environment strings if so desired.
1239  *   The types that are prepended with environment variables are:
1240  *   CSIDL_Type_User:     %USERPROFILE%
1241  *   CSIDL_Type_AllUsers: %ALLUSERSPROFILE%
1242  *   CSIDL_Type_CurrVer:  %SystemDrive%
1243  *   (Others might make sense too, but as yet are unneeded.)
1244  */
1245 static HRESULT _SHGetDefaultValue(BYTE folder, LPWSTR pszPath)
1246 {
1247     HRESULT hr;
1248     WCHAR resourcePath[MAX_PATH];
1249     LPCWSTR pDefaultPath = NULL;
1250
1251     TRACE("0x%02x,%p\n", folder, pszPath);
1252
1253     if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1254         return E_INVALIDARG;
1255     if (!pszPath)
1256         return E_INVALIDARG;
1257
1258     if (CSIDL_Data[folder].szDefaultPath &&
1259      IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath))
1260     {
1261         if (LoadStringW(shell32_hInstance,
1262          LOWORD(CSIDL_Data[folder].szDefaultPath), resourcePath, MAX_PATH))
1263         {
1264             hr = S_OK;
1265             pDefaultPath = resourcePath;
1266         }
1267         else
1268         {
1269             FIXME("(%d,%s), LoadString failed, missing translation?\n", folder,
1270              debugstr_w(pszPath));
1271             hr = E_FAIL;
1272         }
1273     }
1274     else
1275     {
1276         hr = S_OK;
1277         pDefaultPath = CSIDL_Data[folder].szDefaultPath;
1278     }
1279     if (SUCCEEDED(hr))
1280     {
1281         switch (CSIDL_Data[folder].type)
1282         {
1283             case CSIDL_Type_User:
1284                 strcpyW(pszPath, UserProfileW);
1285                 break;
1286             case CSIDL_Type_AllUsers:
1287                 strcpyW(pszPath, AllUsersProfileW);
1288                 break;
1289             case CSIDL_Type_CurrVer:
1290                 strcpyW(pszPath, SystemDriveW);
1291                 break;
1292             default:
1293                 ; /* no corresponding env. var, do nothing */
1294         }
1295         if (pDefaultPath)
1296         {
1297             PathAddBackslashW(pszPath);
1298             strcatW(pszPath, pDefaultPath);
1299         }
1300     }
1301     TRACE("returning 0x%08x\n", hr);
1302     return hr;
1303 }
1304
1305 /* Gets the (unexpanded) value of the folder with index folder into pszPath.
1306  * The folder's type is assumed to be CSIDL_Type_CurrVer.  Its default value
1307  * can be overridden in the HKLM\\szCurrentVersion key.
1308  * If dwFlags has SHGFP_TYPE_DEFAULT set or if the value isn't overridden in
1309  * the registry, uses _SHGetDefaultValue to get the value.
1310  */
1311 static HRESULT _SHGetCurrentVersionPath(DWORD dwFlags, BYTE folder,
1312  LPWSTR pszPath)
1313 {
1314     HRESULT hr;
1315
1316     TRACE("0x%08x,0x%02x,%p\n", dwFlags, folder, pszPath);
1317
1318     if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1319         return E_INVALIDARG;
1320     if (CSIDL_Data[folder].type != CSIDL_Type_CurrVer)
1321         return E_INVALIDARG;
1322     if (!pszPath)
1323         return E_INVALIDARG;
1324
1325     if (dwFlags & SHGFP_TYPE_DEFAULT)
1326         hr = _SHGetDefaultValue(folder, pszPath);
1327     else
1328     {
1329         HKEY hKey;
1330
1331         if (RegCreateKeyW(HKEY_LOCAL_MACHINE, szCurrentVersion, &hKey))
1332             hr = E_FAIL;
1333         else
1334         {
1335             DWORD dwType, dwPathLen = MAX_PATH * sizeof(WCHAR);
1336
1337             if (RegQueryValueExW(hKey, CSIDL_Data[folder].szValueName, NULL,
1338              &dwType, (LPBYTE)pszPath, &dwPathLen) ||
1339              (dwType != REG_SZ && dwType != REG_EXPAND_SZ))
1340             {
1341                 hr = _SHGetDefaultValue(folder, pszPath);
1342                 dwType = REG_EXPAND_SZ;
1343                 RegSetValueExW(hKey, CSIDL_Data[folder].szValueName, 0, dwType,
1344                  (LPBYTE)pszPath, (strlenW(pszPath)+1)*sizeof(WCHAR));
1345             }
1346             else
1347             {
1348                 pszPath[dwPathLen / sizeof(WCHAR)] = '\0';
1349                 hr = S_OK;
1350             }
1351             RegCloseKey(hKey);
1352         }
1353     }
1354     TRACE("returning 0x%08x (output path is %s)\n", hr, debugstr_w(pszPath));
1355     return hr;
1356 }
1357
1358 /* Gets the user's path (unexpanded) for the CSIDL with index folder:
1359  * If SHGFP_TYPE_DEFAULT is set, calls _SHGetDefaultValue for it.  Otherwise
1360  * calls _SHGetUserShellFolderPath for it.  Where it looks depends on hToken:
1361  * - if hToken is -1, looks in HKEY_USERS\.Default
1362  * - otherwise looks first in HKEY_CURRENT_USER, followed by HKEY_LOCAL_MACHINE
1363  *   if HKEY_CURRENT_USER doesn't contain any entries.  If both fail, finally
1364  *   calls _SHGetDefaultValue for it.
1365  */
1366 static HRESULT _SHGetUserProfilePath(HANDLE hToken, DWORD dwFlags, BYTE folder,
1367  LPWSTR pszPath)
1368 {
1369     HRESULT hr;
1370
1371     TRACE("%p,0x%08x,0x%02x,%p\n", hToken, dwFlags, folder, pszPath);
1372
1373     if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1374         return E_INVALIDARG;
1375     if (CSIDL_Data[folder].type != CSIDL_Type_User)
1376         return E_INVALIDARG;
1377     if (!pszPath)
1378         return E_INVALIDARG;
1379
1380     /* Only the current user and the default user are supported right now
1381      * I'm afraid.
1382      * FIXME: should be able to call GetTokenInformation on the token,
1383      * then call ConvertSidToStringSidW on it to get the user prefix.
1384      * But Wine's registry doesn't store user info by sid, it stores it
1385      * by user name (and I don't know how to convert from a token to a
1386      * user name).
1387      */
1388     if (hToken != NULL && hToken != (HANDLE)-1)
1389     {
1390         FIXME("unsupported for user other than current or default\n");
1391         return E_FAIL;
1392     }
1393
1394     if (dwFlags & SHGFP_TYPE_DEFAULT)
1395         hr = _SHGetDefaultValue(folder, pszPath);
1396     else
1397     {
1398         LPCWSTR userPrefix = NULL;
1399         HKEY hRootKey;
1400
1401         if (hToken == (HANDLE)-1)
1402         {
1403             hRootKey = HKEY_USERS;
1404             userPrefix = DefaultW;
1405         }
1406         else /* hToken == NULL, other values disallowed above */
1407             hRootKey = HKEY_CURRENT_USER;
1408         hr = _SHGetUserShellFolderPath(hRootKey, userPrefix,
1409          CSIDL_Data[folder].szValueName, pszPath);
1410         if (FAILED(hr) && hRootKey != HKEY_LOCAL_MACHINE)
1411             hr = _SHGetUserShellFolderPath(HKEY_LOCAL_MACHINE, NULL,
1412              CSIDL_Data[folder].szValueName, pszPath);
1413         if (FAILED(hr))
1414             hr = _SHGetDefaultValue(folder, pszPath);
1415     }
1416     TRACE("returning 0x%08x (output path is %s)\n", hr, debugstr_w(pszPath));
1417     return hr;
1418 }
1419
1420 /* Gets the (unexpanded) path for the CSIDL with index folder.  If dwFlags has
1421  * SHGFP_TYPE_DEFAULT set, calls _SHGetDefaultValue.  Otherwise calls
1422  * _SHGetUserShellFolderPath for it, looking only in HKEY_LOCAL_MACHINE.
1423  * If this fails, falls back to _SHGetDefaultValue.
1424  */
1425 static HRESULT _SHGetAllUsersProfilePath(DWORD dwFlags, BYTE folder,
1426  LPWSTR pszPath)
1427 {
1428     HRESULT hr;
1429
1430     TRACE("0x%08x,0x%02x,%p\n", dwFlags, folder, pszPath);
1431
1432     if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1433         return E_INVALIDARG;
1434     if (CSIDL_Data[folder].type != CSIDL_Type_AllUsers)
1435         return E_INVALIDARG;
1436     if (!pszPath)
1437         return E_INVALIDARG;
1438
1439     if (dwFlags & SHGFP_TYPE_DEFAULT)
1440         hr = _SHGetDefaultValue(folder, pszPath);
1441     else
1442     {
1443         hr = _SHGetUserShellFolderPath(HKEY_LOCAL_MACHINE, NULL,
1444          CSIDL_Data[folder].szValueName, pszPath);
1445         if (FAILED(hr))
1446             hr = _SHGetDefaultValue(folder, pszPath);
1447     }
1448     TRACE("returning 0x%08x (output path is %s)\n", hr, debugstr_w(pszPath));
1449     return hr;
1450 }
1451
1452 static HRESULT _SHOpenProfilesKey(PHKEY pKey)
1453 {
1454     LONG lRet;
1455     DWORD disp;
1456
1457     lRet = RegCreateKeyExW(HKEY_LOCAL_MACHINE, ProfileListW, 0, NULL, 0,
1458      KEY_ALL_ACCESS, NULL, pKey, &disp);
1459     return HRESULT_FROM_WIN32(lRet);
1460 }
1461
1462 /* Reads the value named szValueName from the key profilesKey (assumed to be
1463  * opened by _SHOpenProfilesKey) into szValue, which is assumed to be MAX_PATH
1464  * WCHARs in length.  If it doesn't exist, returns szDefault (and saves
1465  * szDefault to the registry).
1466  */
1467 static HRESULT _SHGetProfilesValue(HKEY profilesKey, LPCWSTR szValueName,
1468  LPWSTR szValue, LPCWSTR szDefault)
1469 {
1470     HRESULT hr;
1471     DWORD type, dwPathLen = MAX_PATH * sizeof(WCHAR);
1472     LONG lRet;
1473
1474     TRACE("%p,%s,%p,%s\n", profilesKey, debugstr_w(szValueName), szValue,
1475      debugstr_w(szDefault));
1476     lRet = RegQueryValueExW(profilesKey, szValueName, NULL, &type,
1477      (LPBYTE)szValue, &dwPathLen);
1478     if (!lRet && (type == REG_SZ || type == REG_EXPAND_SZ) && dwPathLen
1479      && *szValue)
1480     {
1481         dwPathLen /= sizeof(WCHAR);
1482         szValue[dwPathLen] = '\0';
1483         hr = S_OK;
1484     }
1485     else
1486     {
1487         /* Missing or invalid value, set a default */
1488         lstrcpynW(szValue, szDefault, MAX_PATH);
1489         TRACE("Setting missing value %s to %s\n", debugstr_w(szValueName),
1490                                                   debugstr_w(szValue));
1491         lRet = RegSetValueExW(profilesKey, szValueName, 0, REG_EXPAND_SZ,
1492                               (LPBYTE)szValue,
1493                               (strlenW(szValue) + 1) * sizeof(WCHAR));
1494         if (lRet)
1495             hr = HRESULT_FROM_WIN32(lRet);
1496         else
1497             hr = S_OK;
1498     }
1499     TRACE("returning 0x%08x (output value is %s)\n", hr, debugstr_w(szValue));
1500     return hr;
1501 }
1502
1503 /* Attempts to expand environment variables from szSrc into szDest, which is
1504  * assumed to be MAX_PATH characters in length.  Before referring to the
1505  * environment, handles a few variables directly, because the environment
1506  * variables may not be set when this is called (as during Wine's installation
1507  * when default values are being written to the registry).
1508  * The directly handled environment variables, and their source, are:
1509  * - ALLUSERSPROFILE, USERPROFILE: reads from the registry
1510  * - SystemDrive: uses GetSystemDirectoryW and uses the drive portion of its
1511  *   path
1512  * If one of the directly handled environment variables is expanded, only
1513  * expands a single variable, and only in the beginning of szSrc.
1514  */
1515 static HRESULT _SHExpandEnvironmentStrings(LPCWSTR szSrc, LPWSTR szDest)
1516 {
1517     HRESULT hr;
1518     WCHAR szTemp[MAX_PATH], szProfilesPrefix[MAX_PATH] = { 0 };
1519     HKEY key = NULL;
1520
1521     TRACE("%s, %p\n", debugstr_w(szSrc), szDest);
1522
1523     if (!szSrc || !szDest) return E_INVALIDARG;
1524
1525     /* short-circuit if there's nothing to expand */
1526     if (szSrc[0] != '%')
1527     {
1528         strcpyW(szDest, szSrc);
1529         hr = S_OK;
1530         goto end;
1531     }
1532     /* Get the profile prefix, we'll probably be needing it */
1533     hr = _SHOpenProfilesKey(&key);
1534     if (SUCCEEDED(hr))
1535     {
1536         WCHAR szDefaultProfilesPrefix[MAX_PATH];
1537
1538         GetWindowsDirectoryW(szDefaultProfilesPrefix, MAX_PATH);
1539         PathAddBackslashW(szDefaultProfilesPrefix);
1540         PathAppendW(szDefaultProfilesPrefix, szDefaultProfileDirW);
1541         hr = _SHGetProfilesValue(key, ProfilesDirectoryW, szProfilesPrefix,
1542          szDefaultProfilesPrefix);
1543     }
1544
1545     *szDest = 0;
1546     strcpyW(szTemp, szSrc);
1547     while (SUCCEEDED(hr) && szTemp[0] == '%')
1548     {
1549         if (!strncmpiW(szTemp, AllUsersProfileW, strlenW(AllUsersProfileW)))
1550         {
1551             WCHAR szAllUsers[MAX_PATH];
1552
1553             strcpyW(szDest, szProfilesPrefix);
1554             hr = _SHGetProfilesValue(key, AllUsersProfileValueW,
1555              szAllUsers, AllUsersW);
1556             PathAppendW(szDest, szAllUsers);
1557             PathAppendW(szDest, szTemp + strlenW(AllUsersProfileW));
1558         }
1559         else if (!strncmpiW(szTemp, UserProfileW, strlenW(UserProfileW)))
1560         {
1561             WCHAR userName[MAX_PATH];
1562             DWORD userLen = MAX_PATH;
1563
1564             strcpyW(szDest, szProfilesPrefix);
1565             GetUserNameW(userName, &userLen);
1566             PathAppendW(szDest, userName);
1567             PathAppendW(szDest, szTemp + strlenW(UserProfileW));
1568         }
1569         else if (!strncmpiW(szTemp, SystemDriveW, strlenW(SystemDriveW)))
1570         {
1571             GetSystemDirectoryW(szDest, MAX_PATH);
1572             if (szDest[1] != ':')
1573             {
1574                 FIXME("non-drive system paths unsupported\n");
1575                 hr = E_FAIL;
1576             }
1577             else
1578             {
1579                 strcpyW(szDest + 3, szTemp + strlenW(SystemDriveW) + 1);
1580                 hr = S_OK;
1581             }
1582         }
1583         else
1584         {
1585             DWORD ret = ExpandEnvironmentStringsW(szSrc, szDest, MAX_PATH);
1586
1587             if (ret > MAX_PATH)
1588                 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
1589             else if (ret == 0)
1590                 hr = HRESULT_FROM_WIN32(GetLastError());
1591             else
1592                 hr = S_OK;
1593         }
1594         if (SUCCEEDED(hr) && szDest[0] == '%')
1595             strcpyW(szTemp, szDest);
1596         else
1597         {
1598             /* terminate loop */
1599             szTemp[0] = '\0';
1600         }
1601     }
1602 end:
1603     if (key)
1604         RegCloseKey(key);
1605     TRACE("returning 0x%08x (input was %s, output is %s)\n", hr,
1606      debugstr_w(szSrc), debugstr_w(szDest));
1607     return hr;
1608 }
1609
1610 /*************************************************************************
1611  * SHGetFolderPathW                     [SHELL32.@]
1612  *
1613  * Convert nFolder to path.  
1614  *
1615  * RETURNS
1616  *  Success: S_OK
1617  *  Failure: standard HRESULT error codes.
1618  *
1619  * NOTES
1620  * Most values can be overridden in either
1621  * HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
1622  * or in the same location in HKLM.
1623  * The "Shell Folders" registry key was used in NT4 and earlier systems.
1624  * Beginning with Windows 2000, the "User Shell Folders" key is used, so
1625  * changes made to it are made to the former key too.  This synchronization is
1626  * done on-demand: not until someone requests the value of one of these paths
1627  * (by calling one of the SHGet functions) is the value synchronized.
1628  * Furthermore, the HKCU paths take precedence over the HKLM paths.
1629  */
1630 HRESULT WINAPI SHGetFolderPathW(
1631         HWND hwndOwner,    /* [I] owner window */
1632         int nFolder,       /* [I] CSIDL identifying the folder */
1633         HANDLE hToken,     /* [I] access token */
1634         DWORD dwFlags,     /* [I] which path to return */
1635         LPWSTR pszPath)    /* [O] converted path */
1636 {
1637     HRESULT    hr;
1638     WCHAR      szBuildPath[MAX_PATH], szTemp[MAX_PATH];
1639     DWORD      folder = nFolder & CSIDL_FOLDER_MASK;
1640     CSIDL_Type type;
1641     int        ret;
1642     
1643     TRACE("%p,%p,nFolder=0x%04x\n", hwndOwner,pszPath,nFolder);
1644
1645     /* Windows always NULL-terminates the resulting path regardless of success
1646      * or failure, so do so first
1647      */
1648     if (pszPath)
1649         *pszPath = '\0';
1650     if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1651         return E_INVALIDARG;
1652     szTemp[0] = 0;
1653     type = CSIDL_Data[folder].type;
1654     switch (type)
1655     {
1656         case CSIDL_Type_Disallowed:
1657             hr = E_INVALIDARG;
1658             break;
1659         case CSIDL_Type_NonExistent:
1660             hr = S_FALSE;
1661             break;
1662         case CSIDL_Type_WindowsPath:
1663             GetWindowsDirectoryW(szTemp, MAX_PATH);
1664             if (CSIDL_Data[folder].szDefaultPath &&
1665              !IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath) &&
1666              *CSIDL_Data[folder].szDefaultPath)
1667             {
1668                 PathAddBackslashW(szTemp);
1669                 strcatW(szTemp, CSIDL_Data[folder].szDefaultPath);
1670             }
1671             hr = S_OK;
1672             break;
1673         case CSIDL_Type_SystemPath:
1674             GetSystemDirectoryW(szTemp, MAX_PATH);
1675             if (CSIDL_Data[folder].szDefaultPath &&
1676              !IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath) &&
1677              *CSIDL_Data[folder].szDefaultPath)
1678             {
1679                 PathAddBackslashW(szTemp);
1680                 strcatW(szTemp, CSIDL_Data[folder].szDefaultPath);
1681             }
1682             hr = S_OK;
1683             break;
1684         case CSIDL_Type_CurrVer:
1685             hr = _SHGetCurrentVersionPath(dwFlags, folder, szTemp);
1686             break;
1687         case CSIDL_Type_User:
1688             hr = _SHGetUserProfilePath(hToken, dwFlags, folder, szTemp);
1689             break;
1690         case CSIDL_Type_AllUsers:
1691             hr = _SHGetAllUsersProfilePath(dwFlags, folder, szTemp);
1692             break;
1693         default:
1694             FIXME("bogus type %d, please fix\n", type);
1695             hr = E_INVALIDARG;
1696             break;
1697     }
1698
1699     /* Expand environment strings if necessary */
1700     if (*szTemp == '%')
1701         hr = _SHExpandEnvironmentStrings(szTemp, szBuildPath);
1702     else
1703         strcpyW(szBuildPath, szTemp);
1704     /* Copy the path if it's available before we might return */
1705     if (SUCCEEDED(hr) && pszPath)
1706         strcpyW(pszPath, szBuildPath);
1707
1708     if (FAILED(hr)) goto end;
1709
1710     /* if we don't care about existing directories we are ready */
1711     if(nFolder & CSIDL_FLAG_DONT_VERIFY) goto end;
1712
1713     if (PathFileExistsW(szBuildPath)) goto end;
1714
1715     /* not existing but we are not allowed to create it.  The return value
1716      * is verified against shell32 version 6.0.
1717      */
1718     if (!(nFolder & CSIDL_FLAG_CREATE))
1719     {
1720         hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
1721         goto end;
1722     }
1723
1724     /* create directory/directories */
1725     ret = SHCreateDirectoryExW(hwndOwner, szBuildPath, NULL);
1726     if (ret && ret != ERROR_ALREADY_EXISTS)
1727     {
1728         ERR("Failed to create directory %s.\n", debugstr_w(szBuildPath));
1729         hr = E_FAIL;
1730         goto end;
1731     }
1732
1733     TRACE("Created missing system directory %s\n", debugstr_w(szBuildPath));
1734 end:
1735     TRACE("returning 0x%08x (final path is %s)\n", hr, debugstr_w(szBuildPath));
1736     return hr;
1737 }
1738
1739 /*************************************************************************
1740  * SHGetFolderPathA                     [SHELL32.@]
1741  *
1742  * See SHGetFolderPathW.
1743  */
1744 HRESULT WINAPI SHGetFolderPathA(
1745         HWND hwndOwner,
1746         int nFolder,
1747         HANDLE hToken,
1748         DWORD dwFlags,
1749         LPSTR pszPath)
1750 {
1751     WCHAR szTemp[MAX_PATH];
1752     HRESULT hr;
1753
1754     TRACE("%p,%p,nFolder=0x%04x\n",hwndOwner,pszPath,nFolder);
1755
1756     if (pszPath)
1757         *pszPath = '\0';
1758     hr = SHGetFolderPathW(hwndOwner, nFolder, hToken, dwFlags, szTemp);
1759     if (SUCCEEDED(hr) && pszPath)
1760         WideCharToMultiByte(CP_ACP, 0, szTemp, -1, pszPath, MAX_PATH, NULL,
1761          NULL);
1762
1763     return hr;
1764 }
1765
1766 /* For each folder in folders, if its value has not been set in the registry,
1767  * calls _SHGetUserProfilePath or _SHGetAllUsersProfilePath (depending on the
1768  * folder's type) to get the unexpanded value first.
1769  * Writes the unexpanded value to User Shell Folders, and queries it with
1770  * SHGetFolderPathW to force the creation of the directory if it doesn't
1771  * already exist.  SHGetFolderPathW also returns the expanded value, which
1772  * this then writes to Shell Folders.
1773  */
1774 static HRESULT _SHRegisterFolders(HKEY hRootKey, HANDLE hToken,
1775  LPCWSTR szUserShellFolderPath, LPCWSTR szShellFolderPath, const UINT folders[],
1776  UINT foldersLen)
1777 {
1778     UINT i;
1779     WCHAR path[MAX_PATH];
1780     HRESULT hr = S_OK;
1781     HKEY hUserKey = NULL, hKey = NULL;
1782     DWORD dwType, dwPathLen;
1783     LONG ret;
1784
1785     TRACE("%p,%p,%s,%p,%u\n", hRootKey, hToken,
1786      debugstr_w(szUserShellFolderPath), folders, foldersLen);
1787
1788     ret = RegCreateKeyW(hRootKey, szUserShellFolderPath, &hUserKey);
1789     if (ret)
1790         hr = HRESULT_FROM_WIN32(ret);
1791     else
1792     {
1793         ret = RegCreateKeyW(hRootKey, szShellFolderPath, &hKey);
1794         if (ret)
1795             hr = HRESULT_FROM_WIN32(ret);
1796     }
1797     for (i = 0; SUCCEEDED(hr) && i < foldersLen; i++)
1798     {
1799         dwPathLen = MAX_PATH * sizeof(WCHAR);
1800         if (RegQueryValueExW(hUserKey, CSIDL_Data[folders[i]].szValueName, NULL,
1801          &dwType, (LPBYTE)path, &dwPathLen) || (dwType != REG_SZ &&
1802          dwType != REG_EXPAND_SZ))
1803         {
1804             *path = '\0';
1805             if (CSIDL_Data[folders[i]].type == CSIDL_Type_User)
1806                 _SHGetUserProfilePath(hToken, SHGFP_TYPE_DEFAULT, folders[i],
1807                  path);
1808             else if (CSIDL_Data[folders[i]].type == CSIDL_Type_AllUsers)
1809                 _SHGetAllUsersProfilePath(SHGFP_TYPE_DEFAULT, folders[i], path);
1810             else if (CSIDL_Data[folders[i]].type == CSIDL_Type_WindowsPath)
1811                 GetWindowsDirectoryW(path, MAX_PATH);
1812             else
1813                 hr = E_FAIL;
1814             if (*path)
1815             {
1816                 ret = RegSetValueExW(hUserKey,
1817                  CSIDL_Data[folders[i]].szValueName, 0, REG_EXPAND_SZ,
1818                  (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR));
1819                 if (ret)
1820                     hr = HRESULT_FROM_WIN32(ret);
1821                 else
1822                 {
1823                     hr = SHGetFolderPathW(NULL, folders[i] | CSIDL_FLAG_CREATE,
1824                      hToken, SHGFP_TYPE_DEFAULT, path);
1825                     ret = RegSetValueExW(hKey,
1826                      CSIDL_Data[folders[i]].szValueName, 0, REG_SZ,
1827                      (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR));
1828                     if (ret)
1829                         hr = HRESULT_FROM_WIN32(ret);
1830                 }
1831             }
1832         }
1833     }
1834     if (hUserKey)
1835         RegCloseKey(hUserKey);
1836     if (hKey)
1837         RegCloseKey(hKey);
1838
1839     TRACE("returning 0x%08x\n", hr);
1840     return hr;
1841 }
1842
1843 static HRESULT _SHRegisterUserShellFolders(BOOL bDefault)
1844 {
1845     static const UINT folders[] = {
1846      CSIDL_PROGRAMS,
1847      CSIDL_PERSONAL,
1848      CSIDL_FAVORITES,
1849      CSIDL_APPDATA,
1850      CSIDL_STARTUP,
1851      CSIDL_RECENT,
1852      CSIDL_SENDTO,
1853      CSIDL_STARTMENU,
1854      CSIDL_MYMUSIC,
1855      CSIDL_MYVIDEO,
1856      CSIDL_DESKTOPDIRECTORY,
1857      CSIDL_NETHOOD,
1858      CSIDL_TEMPLATES,
1859      CSIDL_PRINTHOOD,
1860      CSIDL_LOCAL_APPDATA,
1861      CSIDL_INTERNET_CACHE,
1862      CSIDL_COOKIES,
1863      CSIDL_HISTORY,
1864      CSIDL_MYPICTURES,
1865      CSIDL_FONTS
1866     };
1867     WCHAR userShellFolderPath[MAX_PATH], shellFolderPath[MAX_PATH];
1868     LPCWSTR pUserShellFolderPath, pShellFolderPath;
1869     HRESULT hr = S_OK;
1870     HKEY hRootKey;
1871     HANDLE hToken;
1872
1873     TRACE("%s\n", bDefault ? "TRUE" : "FALSE");
1874     if (bDefault)
1875     {
1876         hToken = (HANDLE)-1;
1877         hRootKey = HKEY_USERS;
1878         strcpyW(userShellFolderPath, DefaultW);
1879         PathAddBackslashW(userShellFolderPath);
1880         strcatW(userShellFolderPath, szSHUserFolders);
1881         pUserShellFolderPath = userShellFolderPath;
1882         strcpyW(shellFolderPath, DefaultW);
1883         PathAddBackslashW(shellFolderPath);
1884         strcatW(shellFolderPath, szSHFolders);
1885         pShellFolderPath = shellFolderPath;
1886     }
1887     else
1888     {
1889         hToken = NULL;
1890         hRootKey = HKEY_CURRENT_USER;
1891         pUserShellFolderPath = szSHUserFolders;
1892         pShellFolderPath = szSHFolders;
1893     }
1894
1895     hr = _SHRegisterFolders(hRootKey, hToken, pUserShellFolderPath,
1896      pShellFolderPath, folders, sizeof(folders) / sizeof(folders[0]));
1897     TRACE("returning 0x%08x\n", hr);
1898     return hr;
1899 }
1900
1901 static HRESULT _SHRegisterCommonShellFolders(void)
1902 {
1903     static const UINT folders[] = {
1904      CSIDL_COMMON_STARTMENU,
1905      CSIDL_COMMON_PROGRAMS,
1906      CSIDL_COMMON_STARTUP,
1907      CSIDL_COMMON_DESKTOPDIRECTORY,
1908      CSIDL_COMMON_FAVORITES,
1909      CSIDL_COMMON_APPDATA,
1910      CSIDL_COMMON_TEMPLATES,
1911      CSIDL_COMMON_DOCUMENTS,
1912     };
1913     HRESULT hr;
1914
1915     TRACE("\n");
1916     hr = _SHRegisterFolders(HKEY_LOCAL_MACHINE, NULL, szSHUserFolders,
1917      szSHFolders, folders, sizeof(folders) / sizeof(folders[0]));
1918     TRACE("returning 0x%08x\n", hr);
1919     return hr;
1920 }
1921
1922 /******************************************************************************
1923  * _SHAppendToUnixPath  [Internal]
1924  *
1925  * Helper function for _SHCreateSymbolicLinks. Appends pwszSubPath (or the 
1926  * corresponding resource, if IS_INTRESOURCE) to the unix base path 'szBasePath' 
1927  * and replaces backslashes with slashes.
1928  *
1929  * PARAMS
1930  *  szBasePath  [IO] The unix base path, which will be appended to (CP_UNXICP).
1931  *  pwszSubPath [I]  Sub-path or resource id (use MAKEINTRESOURCEW).
1932  *
1933  * RETURNS
1934  *  Success: TRUE,
1935  *  Failure: FALSE
1936  */
1937 static inline BOOL _SHAppendToUnixPath(char *szBasePath, LPCWSTR pwszSubPath) {
1938     WCHAR wszSubPath[MAX_PATH];
1939     int cLen = strlen(szBasePath);
1940     char *pBackslash;
1941
1942     if (IS_INTRESOURCE(pwszSubPath)) {
1943         if (!LoadStringW(shell32_hInstance, LOWORD(pwszSubPath), wszSubPath, MAX_PATH)) {
1944             /* Fall back to hard coded defaults. */
1945             switch (LOWORD(pwszSubPath)) {
1946                 case IDS_PERSONAL:
1947                     lstrcpyW(wszSubPath, PersonalW);
1948                     break;
1949                 case IDS_MYMUSIC:
1950                     lstrcpyW(wszSubPath, My_MusicW);
1951                     break;
1952                 case IDS_MYPICTURES:
1953                     lstrcpyW(wszSubPath, My_PicturesW);
1954                     break;
1955                 case IDS_MYVIDEO:
1956                     lstrcpyW(wszSubPath, My_VideoW);
1957                     break;
1958                 default:
1959                     ERR("LoadString(%d) failed!\n", LOWORD(pwszSubPath));
1960                     return FALSE;
1961             }
1962         }
1963     } else {
1964         lstrcpyW(wszSubPath, pwszSubPath);
1965     }
1966  
1967     if (szBasePath[cLen-1] != '/') szBasePath[cLen++] = '/';
1968  
1969     if (!WideCharToMultiByte(CP_UNIXCP, 0, wszSubPath, -1, szBasePath + cLen,
1970                              FILENAME_MAX - cLen, NULL, NULL))
1971     {
1972         return FALSE;
1973     }
1974  
1975     pBackslash = szBasePath + cLen;
1976     while ((pBackslash = strchr(pBackslash, '\\'))) *pBackslash = '/';
1977  
1978     return TRUE;
1979 }
1980
1981 /******************************************************************************
1982  * _SHCreateSymbolicLinks  [Internal]
1983  * 
1984  * Sets up symbol links for various shell folders to point into the users home
1985  * directory. We do an educated guess about what the user would probably want:
1986  * - If there is a 'My Documents' directory in $HOME, the user probably wants
1987  *   wine's 'My Documents' to point there. Furthermore, we imply that the user
1988  *   is a Windows lover and has no problem with wine creating 'My Pictures',
1989  *   'My Music' and 'My Video' subfolders under '$HOME/My Documents', if those
1990  *   do not already exits. We put appropriate symbolic links in place for those,
1991  *   too.
1992  * - If there is no 'My Documents' directory in $HOME, we let 'My Documents'
1993  *   point directly to $HOME. We assume the user to be a unix hacker who does not
1994  *   want wine to create anything anywhere besides the .wine directory. So, if
1995  *   there already is a 'My Music' directory in $HOME, we symlink the 'My Music'
1996  *   shell folder to it. But if not, we symlink it to $HOME directly. The same
1997  *   holds fo 'My Pictures' and 'My Video'.
1998  * - The Desktop shell folder is symlinked to '$HOME/Desktop', if that does
1999  *   exists and left alone if not.
2000  * ('My Music',... above in fact means LoadString(IDS_MYMUSIC))
2001  */
2002 static void _SHCreateSymbolicLinks(void)
2003 {
2004     UINT aidsMyStuff[] = { IDS_MYPICTURES, IDS_MYVIDEO, IDS_MYMUSIC }, i;
2005     int acsidlMyStuff[] = { CSIDL_MYPICTURES, CSIDL_MYVIDEO, CSIDL_MYMUSIC };
2006     WCHAR wszTempPath[MAX_PATH];
2007     char szPersonalTarget[FILENAME_MAX], *pszPersonal;
2008     char szMyStuffTarget[FILENAME_MAX], *pszMyStuff;
2009     char szDesktopTarget[FILENAME_MAX], *pszDesktop;
2010     struct stat statFolder;
2011     const char *pszHome;
2012     HRESULT hr;
2013
2014     /* Create all necessary profile sub-dirs up to 'My Documents' and get the unix path. */
2015     hr = SHGetFolderPathW(NULL, CSIDL_PERSONAL|CSIDL_FLAG_CREATE, NULL,
2016                           SHGFP_TYPE_DEFAULT, wszTempPath);
2017     if (FAILED(hr)) return;
2018     pszPersonal = wine_get_unix_file_name(wszTempPath);
2019     if (!pszPersonal) return;
2020
2021     pszHome = getenv("HOME");
2022     if (pszHome && !stat(pszHome, &statFolder) && S_ISDIR(statFolder.st_mode)) {
2023         strcpy(szPersonalTarget, pszHome);
2024         if (_SHAppendToUnixPath(szPersonalTarget, MAKEINTRESOURCEW(IDS_PERSONAL)) &&
2025             !stat(szPersonalTarget, &statFolder) && S_ISDIR(statFolder.st_mode))
2026         {
2027             /* '$HOME/My Documents' exists. Create 'My Pictures', 'My Videos' and 
2028              * 'My Music' subfolders or fail silently if they already exist. */
2029             for (i = 0; i < sizeof(aidsMyStuff)/sizeof(aidsMyStuff[0]); i++) {
2030                 strcpy(szMyStuffTarget, szPersonalTarget);
2031                 if (_SHAppendToUnixPath(szMyStuffTarget, MAKEINTRESOURCEW(aidsMyStuff[i])))
2032                     mkdir(szMyStuffTarget, 0777);
2033             }
2034         } 
2035         else
2036         {
2037             /* '$HOME/My Documents' doesn't exists, but '$HOME' does. */ 
2038             strcpy(szPersonalTarget, pszHome);
2039         }
2040
2041         /* Replace 'My Documents' directory with a symlink of fail silently if not empty. */
2042         rmdir(pszPersonal);
2043         symlink(szPersonalTarget, pszPersonal);
2044     }
2045     else
2046     {
2047         /* '$HOME' doesn't exist. Create 'My Pictures', 'My Videos' and 'My Music' subdirs
2048          * in '%USERPROFILE%\\My Documents' or fail silently if they already exist. */
2049         strcpy(szPersonalTarget, pszPersonal);
2050         for (i = 0; i < sizeof(aidsMyStuff)/sizeof(aidsMyStuff[0]); i++) {
2051             strcpy(szMyStuffTarget, szPersonalTarget);
2052             if (_SHAppendToUnixPath(szMyStuffTarget, MAKEINTRESOURCEW(aidsMyStuff[i])))
2053                 mkdir(szMyStuffTarget, 0777);
2054         }
2055     }
2056
2057     /* Create symbolic links for 'My Pictures', 'My Video' and 'My Music'. */
2058     for (i=0; i < sizeof(aidsMyStuff)/sizeof(aidsMyStuff[0]); i++) {
2059         /* Create the current 'My Whatever' folder and get it's unix path. */
2060         hr = SHGetFolderPathW(NULL, acsidlMyStuff[i]|CSIDL_FLAG_CREATE, NULL,
2061                               SHGFP_TYPE_DEFAULT, wszTempPath);
2062         if (FAILED(hr)) continue;
2063         pszMyStuff = wine_get_unix_file_name(wszTempPath);
2064         if (!pszMyStuff) continue;
2065         
2066         strcpy(szMyStuffTarget, szPersonalTarget);
2067         if (_SHAppendToUnixPath(szMyStuffTarget, MAKEINTRESOURCEW(aidsMyStuff[i])) &&
2068             !stat(szMyStuffTarget, &statFolder) && S_ISDIR(statFolder.st_mode))
2069         {
2070             /* If there's a 'My Whatever' directory where 'My Documents' links to, link to it. */
2071             rmdir(pszMyStuff);
2072             symlink(szMyStuffTarget, pszMyStuff);
2073         } 
2074         else
2075         {
2076             /* Else link to where 'My Documents' itself links to. */
2077             rmdir(pszMyStuff);
2078             symlink(szPersonalTarget, pszMyStuff);
2079         }
2080         HeapFree(GetProcessHeap(), 0, pszMyStuff);
2081     }
2082
2083     /* Last but not least, the Desktop folder */
2084     if (pszHome)
2085         strcpy(szDesktopTarget, pszHome);
2086     else
2087         strcpy(szDesktopTarget, pszPersonal);
2088     HeapFree(GetProcessHeap(), 0, pszPersonal);
2089
2090     if (_SHAppendToUnixPath(szDesktopTarget, DesktopW) &&
2091         !stat(szDesktopTarget, &statFolder) && S_ISDIR(statFolder.st_mode))
2092     {
2093         hr = SHGetFolderPathW(NULL, CSIDL_DESKTOPDIRECTORY|CSIDL_FLAG_CREATE, NULL,
2094                               SHGFP_TYPE_DEFAULT, wszTempPath);
2095         if (SUCCEEDED(hr) && (pszDesktop = wine_get_unix_file_name(wszTempPath))) 
2096         {
2097             rmdir(pszDesktop);
2098             symlink(szDesktopTarget, pszDesktop);
2099             HeapFree(GetProcessHeap(), 0, pszDesktop);
2100         }
2101     }
2102 }
2103
2104 /* Register the default values in the registry, as some apps seem to depend
2105  * on their presence.  The set registered was taken from Windows XP.
2106  */
2107 HRESULT SHELL_RegisterShellFolders(void)
2108 {
2109     HRESULT hr;
2110
2111     /* Set up '$HOME' targeted symlinks for 'My Documents', 'My Pictures',
2112      * 'My Video', 'My Music' and 'Desktop' in advance, so that the
2113      * _SHRegister*ShellFolders() functions will find everything nice and clean
2114      * and thus will not attempt to create them in the profile directory. */
2115     _SHCreateSymbolicLinks();
2116     
2117     hr = _SHRegisterUserShellFolders(TRUE);
2118     if (SUCCEEDED(hr))
2119         hr = _SHRegisterUserShellFolders(FALSE);
2120     if (SUCCEEDED(hr))
2121         hr = _SHRegisterCommonShellFolders();
2122     return hr;
2123 }
2124
2125 /*************************************************************************
2126  * SHGetSpecialFolderPathA [SHELL32.@]
2127  */
2128 BOOL WINAPI SHGetSpecialFolderPathA (
2129         HWND hwndOwner,
2130         LPSTR szPath,
2131         int nFolder,
2132         BOOL bCreate)
2133 {
2134         return (SHGetFolderPathA(
2135                 hwndOwner,
2136                 nFolder + (bCreate ? CSIDL_FLAG_CREATE : 0),
2137                 NULL,
2138                 0,
2139                 szPath)) == S_OK ? TRUE : FALSE;
2140 }
2141
2142 /*************************************************************************
2143  * SHGetSpecialFolderPathW
2144  */
2145 BOOL WINAPI SHGetSpecialFolderPathW (
2146         HWND hwndOwner,
2147         LPWSTR szPath,
2148         int nFolder,
2149         BOOL bCreate)
2150 {
2151         return (SHGetFolderPathW(
2152                 hwndOwner,
2153                 nFolder + (bCreate ? CSIDL_FLAG_CREATE : 0),
2154                 NULL,
2155                 0,
2156                 szPath)) == S_OK ? TRUE : FALSE;
2157 }
2158
2159 /*************************************************************************
2160  * SHGetSpecialFolderPath (SHELL32.175)
2161  */
2162 BOOL WINAPI SHGetSpecialFolderPathAW (
2163         HWND hwndOwner,
2164         LPVOID szPath,
2165         int nFolder,
2166         BOOL bCreate)
2167
2168 {
2169         if (SHELL_OsIsUnicode())
2170           return SHGetSpecialFolderPathW (hwndOwner, szPath, nFolder, bCreate);
2171         return SHGetSpecialFolderPathA (hwndOwner, szPath, nFolder, bCreate);
2172 }
2173
2174 /*************************************************************************
2175  * SHGetFolderLocation [SHELL32.@]
2176  *
2177  * Gets the folder locations from the registry and creates a pidl.
2178  *
2179  * PARAMS
2180  *   hwndOwner  [I]
2181  *   nFolder    [I] CSIDL_xxxxx
2182  *   hToken     [I] token representing user, or NULL for current user, or -1 for
2183  *                  default user
2184  *   dwReserved [I] must be zero
2185  *   ppidl      [O] PIDL of a special folder
2186  *
2187  * RETURNS
2188  *  Success: S_OK
2189  *  Failure: Standard OLE-defined error result, S_FALSE or E_INVALIDARG
2190  *
2191  * NOTES
2192  *  Creates missing reg keys and directories.
2193  *  Mostly forwards to SHGetFolderPathW, but a few values of nFolder return
2194  *  virtual folders that are handled here.
2195  */
2196 HRESULT WINAPI SHGetFolderLocation(
2197         HWND hwndOwner,
2198         int nFolder,
2199         HANDLE hToken,
2200         DWORD dwReserved,
2201         LPITEMIDLIST *ppidl)
2202 {
2203     HRESULT hr = E_INVALIDARG;
2204
2205     TRACE("%p 0x%08x %p 0x%08x %p\n",
2206      hwndOwner, nFolder, hToken, dwReserved, ppidl);
2207     
2208     if (!ppidl)
2209         return E_INVALIDARG;
2210     if (dwReserved)
2211         return E_INVALIDARG;
2212
2213     /* The virtual folders' locations are not user-dependent */
2214     *ppidl = NULL;
2215     switch (nFolder)
2216     {
2217         case CSIDL_DESKTOP:
2218             *ppidl = _ILCreateDesktop();
2219             break;
2220
2221         case CSIDL_PERSONAL:
2222             *ppidl = _ILCreateMyDocuments();
2223             break;
2224
2225         case CSIDL_INTERNET:
2226             *ppidl = _ILCreateIExplore();
2227             break;
2228
2229         case CSIDL_CONTROLS:
2230             *ppidl = _ILCreateControlPanel();
2231             break;
2232
2233         case CSIDL_PRINTERS:
2234             *ppidl = _ILCreatePrinters();
2235             break;
2236
2237         case CSIDL_BITBUCKET:
2238             *ppidl = _ILCreateBitBucket();
2239             break;
2240
2241         case CSIDL_DRIVES:
2242             *ppidl = _ILCreateMyComputer();
2243             break;
2244
2245         case CSIDL_NETWORK:
2246             *ppidl = _ILCreateNetwork();
2247             break;
2248
2249         default:
2250         {
2251             WCHAR szPath[MAX_PATH];
2252
2253             hr = SHGetFolderPathW(hwndOwner, nFolder, hToken,
2254              SHGFP_TYPE_CURRENT, szPath);
2255             if (SUCCEEDED(hr))
2256             {
2257                 DWORD attributes=0;
2258
2259                 TRACE("Value=%s\n", debugstr_w(szPath));
2260                 hr = SHILCreateFromPathW(szPath, ppidl, &attributes);
2261             }
2262             else if (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
2263             {
2264                 /* unlike SHGetFolderPath, SHGetFolderLocation in shell32
2265                  * version 6.0 returns E_FAIL for nonexistent paths
2266                  */
2267                 hr = E_FAIL;
2268             }
2269         }
2270     }
2271     if(*ppidl)
2272         hr = NOERROR;
2273
2274     TRACE("-- (new pidl %p)\n",*ppidl);
2275     return hr;
2276 }
2277
2278 /*************************************************************************
2279  * SHGetSpecialFolderLocation           [SHELL32.@]
2280  *
2281  * NOTES
2282  *   In NT5, SHGetSpecialFolderLocation needs the <winntdir>/Recent
2283  *   directory.
2284  */
2285 HRESULT WINAPI SHGetSpecialFolderLocation(
2286         HWND hwndOwner,
2287         INT nFolder,
2288         LPITEMIDLIST * ppidl)
2289 {
2290     HRESULT hr = E_INVALIDARG;
2291
2292     TRACE("(%p,0x%x,%p)\n", hwndOwner,nFolder,ppidl);
2293
2294     if (!ppidl)
2295         return E_INVALIDARG;
2296
2297     hr = SHGetFolderLocation(hwndOwner, nFolder, NULL, 0, ppidl);
2298     return hr;
2299 }