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