advapi32/tests: Add check to see if LookupAccountNameA is implemented.
[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','\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 dwDisp, 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 (RegCreateKeyExW(rootKey, pShellFolderPath, 0, NULL, 0, KEY_ALL_ACCESS,
1191      NULL, &shellFolderKey, &dwDisp))
1192     {
1193         TRACE("Failed to create %s\n", debugstr_w(pShellFolderPath));
1194         return E_FAIL;
1195     }
1196     if (RegCreateKeyExW(rootKey, pUserShellFolderPath, 0, NULL, 0,
1197      KEY_ALL_ACCESS, NULL, &userShellFolderKey, &dwDisp))
1198     {
1199         TRACE("Failed to create %s\n",
1200          debugstr_w(pUserShellFolderPath));
1201         RegCloseKey(shellFolderKey);
1202         return E_FAIL;
1203     }
1204
1205     if (!RegQueryValueExW(userShellFolderKey, value, NULL, &dwType,
1206      (LPBYTE)path, &dwPathLen) && (dwType == REG_EXPAND_SZ || dwType == REG_SZ))
1207     {
1208         LONG ret;
1209
1210         path[dwPathLen / sizeof(WCHAR)] = '\0';
1211         if (dwType == REG_EXPAND_SZ && path[0] == '%')
1212         {
1213             WCHAR szTemp[MAX_PATH];
1214
1215             _SHExpandEnvironmentStrings(path, szTemp);
1216             lstrcpynW(path, szTemp, MAX_PATH);
1217         }
1218         ret = RegSetValueExW(shellFolderKey, value, 0, REG_SZ, (LPBYTE)path,
1219          (strlenW(path) + 1) * sizeof(WCHAR));
1220         if (ret != ERROR_SUCCESS)
1221             hr = HRESULT_FROM_WIN32(ret);
1222         else
1223             hr = S_OK;
1224     }
1225     else
1226         hr = E_FAIL;
1227     RegCloseKey(shellFolderKey);
1228     RegCloseKey(userShellFolderKey);
1229     TRACE("returning 0x%08x\n", hr);
1230     return hr;
1231 }
1232
1233 /* Gets a 'semi-expanded' default value of the CSIDL with index folder into
1234  * pszPath, based on the entries in CSIDL_Data.  By semi-expanded, I mean:
1235  * - The entry's szDefaultPath may be either a string value or an integer
1236  *   resource identifier.  In the latter case, the string value of the resource
1237  *   is written.
1238  * - Depending on the entry's type, the path may begin with an (unexpanded)
1239  *   environment variable name.  The caller is responsible for expanding
1240  *   environment strings if so desired.
1241  *   The types that are prepended with environment variables are:
1242  *   CSIDL_Type_User:     %USERPROFILE%
1243  *   CSIDL_Type_AllUsers: %ALLUSERSPROFILE%
1244  *   CSIDL_Type_CurrVer:  %SystemDrive%
1245  *   (Others might make sense too, but as yet are unneeded.)
1246  */
1247 static HRESULT _SHGetDefaultValue(BYTE folder, LPWSTR pszPath)
1248 {
1249     HRESULT hr;
1250     WCHAR resourcePath[MAX_PATH];
1251     LPCWSTR pDefaultPath = NULL;
1252
1253     TRACE("0x%02x,%p\n", folder, pszPath);
1254
1255     if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1256         return E_INVALIDARG;
1257     if (!pszPath)
1258         return E_INVALIDARG;
1259
1260     if (CSIDL_Data[folder].szDefaultPath &&
1261      IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath))
1262     {
1263         if (LoadStringW(shell32_hInstance,
1264          LOWORD(CSIDL_Data[folder].szDefaultPath), resourcePath, MAX_PATH))
1265         {
1266             hr = S_OK;
1267             pDefaultPath = resourcePath;
1268         }
1269         else
1270         {
1271             FIXME("(%d,%s), LoadString failed, missing translation?\n", folder,
1272              debugstr_w(pszPath));
1273             hr = E_FAIL;
1274         }
1275     }
1276     else
1277     {
1278         hr = S_OK;
1279         pDefaultPath = CSIDL_Data[folder].szDefaultPath;
1280     }
1281     if (SUCCEEDED(hr))
1282     {
1283         switch (CSIDL_Data[folder].type)
1284         {
1285             case CSIDL_Type_User:
1286                 strcpyW(pszPath, UserProfileW);
1287                 break;
1288             case CSIDL_Type_AllUsers:
1289                 strcpyW(pszPath, AllUsersProfileW);
1290                 break;
1291             case CSIDL_Type_CurrVer:
1292                 strcpyW(pszPath, SystemDriveW);
1293                 break;
1294             default:
1295                 ; /* no corresponding env. var, do nothing */
1296         }
1297         if (pDefaultPath)
1298         {
1299             PathAddBackslashW(pszPath);
1300             strcatW(pszPath, pDefaultPath);
1301         }
1302     }
1303     TRACE("returning 0x%08x\n", hr);
1304     return hr;
1305 }
1306
1307 /* Gets the (unexpanded) value of the folder with index folder into pszPath.
1308  * The folder's type is assumed to be CSIDL_Type_CurrVer.  Its default value
1309  * can be overridden in the HKLM\\szCurrentVersion key.
1310  * If dwFlags has SHGFP_TYPE_DEFAULT set or if the value isn't overridden in
1311  * the registry, uses _SHGetDefaultValue to get the value.
1312  */
1313 static HRESULT _SHGetCurrentVersionPath(DWORD dwFlags, BYTE folder,
1314  LPWSTR pszPath)
1315 {
1316     HRESULT hr;
1317
1318     TRACE("0x%08x,0x%02x,%p\n", dwFlags, folder, pszPath);
1319
1320     if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1321         return E_INVALIDARG;
1322     if (CSIDL_Data[folder].type != CSIDL_Type_CurrVer)
1323         return E_INVALIDARG;
1324     if (!pszPath)
1325         return E_INVALIDARG;
1326
1327     if (dwFlags & SHGFP_TYPE_DEFAULT)
1328         hr = _SHGetDefaultValue(folder, pszPath);
1329     else
1330     {
1331         HKEY hKey;
1332         DWORD dwDisp;
1333
1334         if (RegCreateKeyExW(HKEY_LOCAL_MACHINE, szCurrentVersion, 0,
1335          NULL, 0, KEY_ALL_ACCESS, NULL, &hKey, &dwDisp))
1336             hr = E_FAIL;
1337         else
1338         {
1339             DWORD dwType, dwPathLen = MAX_PATH * sizeof(WCHAR);
1340
1341             if (RegQueryValueExW(hKey, CSIDL_Data[folder].szValueName, NULL,
1342              &dwType, (LPBYTE)pszPath, &dwPathLen) ||
1343              (dwType != REG_SZ && dwType != REG_EXPAND_SZ))
1344             {
1345                 hr = _SHGetDefaultValue(folder, pszPath);
1346                 dwType = REG_EXPAND_SZ;
1347                 RegSetValueExW(hKey, CSIDL_Data[folder].szValueName, 0, dwType,
1348                  (LPBYTE)pszPath, (strlenW(pszPath)+1)*sizeof(WCHAR));
1349             }
1350             else
1351             {
1352                 pszPath[dwPathLen / sizeof(WCHAR)] = '\0';
1353                 hr = S_OK;
1354             }
1355             RegCloseKey(hKey);
1356         }
1357     }
1358     TRACE("returning 0x%08x (output path is %s)\n", hr, debugstr_w(pszPath));
1359     return hr;
1360 }
1361
1362 /* Gets the user's path (unexpanded) for the CSIDL with index folder:
1363  * If SHGFP_TYPE_DEFAULT is set, calls _SHGetDefaultValue for it.  Otherwise
1364  * calls _SHGetUserShellFolderPath for it.  Where it looks depends on hToken:
1365  * - if hToken is -1, looks in HKEY_USERS\.Default
1366  * - otherwise looks first in HKEY_CURRENT_USER, followed by HKEY_LOCAL_MACHINE
1367  *   if HKEY_CURRENT_USER doesn't contain any entries.  If both fail, finally
1368  *   calls _SHGetDefaultValue for it.
1369  */
1370 static HRESULT _SHGetUserProfilePath(HANDLE hToken, DWORD dwFlags, BYTE folder,
1371  LPWSTR pszPath)
1372 {
1373     HRESULT hr;
1374
1375     TRACE("%p,0x%08x,0x%02x,%p\n", hToken, dwFlags, folder, pszPath);
1376
1377     if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1378         return E_INVALIDARG;
1379     if (CSIDL_Data[folder].type != CSIDL_Type_User)
1380         return E_INVALIDARG;
1381     if (!pszPath)
1382         return E_INVALIDARG;
1383
1384     /* Only the current user and the default user are supported right now
1385      * I'm afraid.
1386      * FIXME: should be able to call GetTokenInformation on the token,
1387      * then call ConvertSidToStringSidW on it to get the user prefix.
1388      * But Wine's registry doesn't store user info by sid, it stores it
1389      * by user name (and I don't know how to convert from a token to a
1390      * user name).
1391      */
1392     if (hToken != NULL && hToken != (HANDLE)-1)
1393     {
1394         FIXME("unsupported for user other than current or default\n");
1395         return E_FAIL;
1396     }
1397
1398     if (dwFlags & SHGFP_TYPE_DEFAULT)
1399         hr = _SHGetDefaultValue(folder, pszPath);
1400     else
1401     {
1402         LPCWSTR userPrefix = NULL;
1403         HKEY hRootKey;
1404
1405         if (hToken == (HANDLE)-1)
1406         {
1407             hRootKey = HKEY_USERS;
1408             userPrefix = DefaultW;
1409         }
1410         else /* hToken == NULL, other values disallowed above */
1411             hRootKey = HKEY_CURRENT_USER;
1412         hr = _SHGetUserShellFolderPath(hRootKey, userPrefix,
1413          CSIDL_Data[folder].szValueName, pszPath);
1414         if (FAILED(hr) && hRootKey != HKEY_LOCAL_MACHINE)
1415             hr = _SHGetUserShellFolderPath(HKEY_LOCAL_MACHINE, NULL,
1416              CSIDL_Data[folder].szValueName, pszPath);
1417         if (FAILED(hr))
1418             hr = _SHGetDefaultValue(folder, pszPath);
1419     }
1420     TRACE("returning 0x%08x (output path is %s)\n", hr, debugstr_w(pszPath));
1421     return hr;
1422 }
1423
1424 /* Gets the (unexpanded) path for the CSIDL with index folder.  If dwFlags has
1425  * SHGFP_TYPE_DEFAULT set, calls _SHGetDefaultValue.  Otherwise calls
1426  * _SHGetUserShellFolderPath for it, looking only in HKEY_LOCAL_MACHINE.
1427  * If this fails, falls back to _SHGetDefaultValue.
1428  */
1429 static HRESULT _SHGetAllUsersProfilePath(DWORD dwFlags, BYTE folder,
1430  LPWSTR pszPath)
1431 {
1432     HRESULT hr;
1433
1434     TRACE("0x%08x,0x%02x,%p\n", dwFlags, folder, pszPath);
1435
1436     if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1437         return E_INVALIDARG;
1438     if (CSIDL_Data[folder].type != CSIDL_Type_AllUsers)
1439         return E_INVALIDARG;
1440     if (!pszPath)
1441         return E_INVALIDARG;
1442
1443     if (dwFlags & SHGFP_TYPE_DEFAULT)
1444         hr = _SHGetDefaultValue(folder, pszPath);
1445     else
1446     {
1447         hr = _SHGetUserShellFolderPath(HKEY_LOCAL_MACHINE, NULL,
1448          CSIDL_Data[folder].szValueName, pszPath);
1449         if (FAILED(hr))
1450             hr = _SHGetDefaultValue(folder, pszPath);
1451     }
1452     TRACE("returning 0x%08x (output path is %s)\n", hr, debugstr_w(pszPath));
1453     return hr;
1454 }
1455
1456 static HRESULT _SHOpenProfilesKey(PHKEY pKey)
1457 {
1458     LONG lRet;
1459     DWORD disp;
1460
1461     lRet = RegCreateKeyExW(HKEY_LOCAL_MACHINE, ProfileListW, 0, NULL, 0,
1462      KEY_ALL_ACCESS, NULL, pKey, &disp);
1463     return HRESULT_FROM_WIN32(lRet);
1464 }
1465
1466 /* Reads the value named szValueName from the key profilesKey (assumed to be
1467  * opened by _SHOpenProfilesKey) into szValue, which is assumed to be MAX_PATH
1468  * WCHARs in length.  If it doesn't exist, returns szDefault (and saves
1469  * szDefault to the registry).
1470  */
1471 static HRESULT _SHGetProfilesValue(HKEY profilesKey, LPCWSTR szValueName,
1472  LPWSTR szValue, LPCWSTR szDefault)
1473 {
1474     HRESULT hr;
1475     DWORD type, dwPathLen = MAX_PATH * sizeof(WCHAR);
1476     LONG lRet;
1477
1478     TRACE("%p,%s,%p,%s\n", profilesKey, debugstr_w(szValueName), szValue,
1479      debugstr_w(szDefault));
1480     lRet = RegQueryValueExW(profilesKey, szValueName, NULL, &type,
1481      (LPBYTE)szValue, &dwPathLen);
1482     if (!lRet && (type == REG_SZ || type == REG_EXPAND_SZ) && dwPathLen
1483      && *szValue)
1484     {
1485         dwPathLen /= sizeof(WCHAR);
1486         szValue[dwPathLen] = '\0';
1487         hr = S_OK;
1488     }
1489     else
1490     {
1491         /* Missing or invalid value, set a default */
1492         lstrcpynW(szValue, szDefault, MAX_PATH);
1493         TRACE("Setting missing value %s to %s\n", debugstr_w(szValueName),
1494                                                   debugstr_w(szValue));
1495         lRet = RegSetValueExW(profilesKey, szValueName, 0, REG_EXPAND_SZ,
1496                               (LPBYTE)szValue,
1497                               (strlenW(szValue) + 1) * sizeof(WCHAR));
1498         if (lRet)
1499             hr = HRESULT_FROM_WIN32(lRet);
1500         else
1501             hr = S_OK;
1502     }
1503     TRACE("returning 0x%08x (output value is %s)\n", hr, debugstr_w(szValue));
1504     return hr;
1505 }
1506
1507 /* Attempts to expand environment variables from szSrc into szDest, which is
1508  * assumed to be MAX_PATH characters in length.  Before referring to the
1509  * environment, handles a few variables directly, because the environment
1510  * variables may not be set when this is called (as during Wine's installation
1511  * when default values are being written to the registry).
1512  * The directly handled environment variables, and their source, are:
1513  * - ALLUSERSPROFILE, USERPROFILE: reads from the registry
1514  * - SystemDrive: uses GetSystemDirectoryW and uses the drive portion of its
1515  *   path
1516  * If one of the directly handled environment variables is expanded, only
1517  * expands a single variable, and only in the beginning of szSrc.
1518  */
1519 static HRESULT _SHExpandEnvironmentStrings(LPCWSTR szSrc, LPWSTR szDest)
1520 {
1521     HRESULT hr;
1522     WCHAR szTemp[MAX_PATH], szProfilesPrefix[MAX_PATH] = { 0 };
1523     HKEY key = NULL;
1524
1525     TRACE("%s, %p\n", debugstr_w(szSrc), szDest);
1526
1527     if (!szSrc || !szDest) return E_INVALIDARG;
1528
1529     /* short-circuit if there's nothing to expand */
1530     if (szSrc[0] != '%')
1531     {
1532         strcpyW(szDest, szSrc);
1533         hr = S_OK;
1534         goto end;
1535     }
1536     /* Get the profile prefix, we'll probably be needing it */
1537     hr = _SHOpenProfilesKey(&key);
1538     if (SUCCEEDED(hr))
1539     {
1540         WCHAR szDefaultProfilesPrefix[MAX_PATH];
1541
1542         GetWindowsDirectoryW(szDefaultProfilesPrefix, MAX_PATH);
1543         PathAddBackslashW(szDefaultProfilesPrefix);
1544         PathAppendW(szDefaultProfilesPrefix, szDefaultProfileDirW);
1545         hr = _SHGetProfilesValue(key, ProfilesDirectoryW, szProfilesPrefix,
1546          szDefaultProfilesPrefix);
1547     }
1548
1549     *szDest = 0;
1550     strcpyW(szTemp, szSrc);
1551     while (SUCCEEDED(hr) && szTemp[0] == '%')
1552     {
1553         if (!strncmpiW(szTemp, AllUsersProfileW, strlenW(AllUsersProfileW)))
1554         {
1555             WCHAR szAllUsers[MAX_PATH];
1556
1557             strcpyW(szDest, szProfilesPrefix);
1558             hr = _SHGetProfilesValue(key, AllUsersProfileValueW,
1559              szAllUsers, AllUsersW);
1560             PathAppendW(szDest, szAllUsers);
1561             PathAppendW(szDest, szTemp + strlenW(AllUsersProfileW));
1562         }
1563         else if (!strncmpiW(szTemp, UserProfileW, strlenW(UserProfileW)))
1564         {
1565             WCHAR userName[MAX_PATH];
1566             DWORD userLen = MAX_PATH;
1567
1568             strcpyW(szDest, szProfilesPrefix);
1569             GetUserNameW(userName, &userLen);
1570             PathAppendW(szDest, userName);
1571             PathAppendW(szDest, szTemp + strlenW(UserProfileW));
1572         }
1573         else if (!strncmpiW(szTemp, SystemDriveW, strlenW(SystemDriveW)))
1574         {
1575             GetSystemDirectoryW(szDest, MAX_PATH);
1576             if (szDest[1] != ':')
1577             {
1578                 FIXME("non-drive system paths unsupported\n");
1579                 hr = E_FAIL;
1580             }
1581             else
1582             {
1583                 strcpyW(szDest + 3, szTemp + strlenW(SystemDriveW) + 1);
1584                 hr = S_OK;
1585             }
1586         }
1587         else
1588         {
1589             DWORD ret = ExpandEnvironmentStringsW(szSrc, szDest, MAX_PATH);
1590
1591             if (ret > MAX_PATH)
1592                 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
1593             else if (ret == 0)
1594                 hr = HRESULT_FROM_WIN32(GetLastError());
1595             else
1596                 hr = S_OK;
1597         }
1598         if (SUCCEEDED(hr) && szDest[0] == '%')
1599             strcpyW(szTemp, szDest);
1600         else
1601         {
1602             /* terminate loop */
1603             szTemp[0] = '\0';
1604         }
1605     }
1606 end:
1607     if (key)
1608         RegCloseKey(key);
1609     TRACE("returning 0x%08x (input was %s, output is %s)\n", hr,
1610      debugstr_w(szSrc), debugstr_w(szDest));
1611     return hr;
1612 }
1613
1614 /*************************************************************************
1615  * SHGetFolderPathW                     [SHELL32.@]
1616  *
1617  * Convert nFolder to path.  
1618  *
1619  * RETURNS
1620  *  Success: S_OK
1621  *  Failure: standard HRESULT error codes.
1622  *
1623  * NOTES
1624  * Most values can be overridden in either
1625  * HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
1626  * or in the same location in HKLM.
1627  * The registry usage is explained by the following tech note:
1628  * http://www.microsoft.com/windows2000/techinfo/reskit/en-us/default.asp?url=/windows2000/techinfo/reskit/en-us/regentry/36173.asp
1629  * The "Shell Folders" registry key was used in NT4 and earlier systems.
1630  * Beginning with Windows 2000, the "User Shell Folders" key is used, so
1631  * changes made to it are made to the former key too.  This synchronization is
1632  * done on-demand: not until someone requests the value of one of these paths
1633  * (by calling one of the SHGet functions) is the value synchronized.
1634  * Furthermore, as explained here:
1635  * http://www.microsoft.com/windows2000/techinfo/reskit/en-us/default.asp?url=/windows2000/techinfo/reskit/en-us/regentry/36276.asp
1636  * the HKCU paths take precedence over the HKLM paths.
1637  *
1638  */
1639 HRESULT WINAPI SHGetFolderPathW(
1640         HWND hwndOwner,    /* [I] owner window */
1641         int nFolder,       /* [I] CSIDL identifying the folder */
1642         HANDLE hToken,     /* [I] access token */
1643         DWORD dwFlags,     /* [I] which path to return */
1644         LPWSTR pszPath)    /* [O] converted path */
1645 {
1646     HRESULT    hr;
1647     WCHAR      szBuildPath[MAX_PATH], szTemp[MAX_PATH];
1648     DWORD      folder = nFolder & CSIDL_FOLDER_MASK;
1649     CSIDL_Type type;
1650     int        ret;
1651     
1652     TRACE("%p,%p,nFolder=0x%04x\n", hwndOwner,pszPath,nFolder);
1653
1654     /* Windows always NULL-terminates the resulting path regardless of success
1655      * or failure, so do so first
1656      */
1657     if (pszPath)
1658         *pszPath = '\0';
1659     if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1660         return E_INVALIDARG;
1661     szTemp[0] = 0;
1662     type = CSIDL_Data[folder].type;
1663     switch (type)
1664     {
1665         case CSIDL_Type_Disallowed:
1666             hr = E_INVALIDARG;
1667             break;
1668         case CSIDL_Type_NonExistent:
1669             hr = S_FALSE;
1670             break;
1671         case CSIDL_Type_WindowsPath:
1672             GetWindowsDirectoryW(szTemp, MAX_PATH);
1673             if (CSIDL_Data[folder].szDefaultPath &&
1674              !IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath) &&
1675              *CSIDL_Data[folder].szDefaultPath)
1676             {
1677                 PathAddBackslashW(szTemp);
1678                 strcatW(szTemp, CSIDL_Data[folder].szDefaultPath);
1679             }
1680             hr = S_OK;
1681             break;
1682         case CSIDL_Type_SystemPath:
1683             GetSystemDirectoryW(szTemp, MAX_PATH);
1684             if (CSIDL_Data[folder].szDefaultPath &&
1685              !IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath) &&
1686              *CSIDL_Data[folder].szDefaultPath)
1687             {
1688                 PathAddBackslashW(szTemp);
1689                 strcatW(szTemp, CSIDL_Data[folder].szDefaultPath);
1690             }
1691             hr = S_OK;
1692             break;
1693         case CSIDL_Type_CurrVer:
1694             hr = _SHGetCurrentVersionPath(dwFlags, folder, szTemp);
1695             break;
1696         case CSIDL_Type_User:
1697             hr = _SHGetUserProfilePath(hToken, dwFlags, folder, szTemp);
1698             break;
1699         case CSIDL_Type_AllUsers:
1700             hr = _SHGetAllUsersProfilePath(dwFlags, folder, szTemp);
1701             break;
1702         default:
1703             FIXME("bogus type %d, please fix\n", type);
1704             hr = E_INVALIDARG;
1705             break;
1706     }
1707
1708     /* Expand environment strings if necessary */
1709     if (*szTemp == '%')
1710         hr = _SHExpandEnvironmentStrings(szTemp, szBuildPath);
1711     else
1712         strcpyW(szBuildPath, szTemp);
1713     /* Copy the path if it's available before we might return */
1714     if (SUCCEEDED(hr) && pszPath)
1715         strcpyW(pszPath, szBuildPath);
1716
1717     if (FAILED(hr)) goto end;
1718
1719     /* if we don't care about existing directories we are ready */
1720     if(nFolder & CSIDL_FLAG_DONT_VERIFY) goto end;
1721
1722     if (PathFileExistsW(szBuildPath)) goto end;
1723
1724     /* not existing but we are not allowed to create it.  The return value
1725      * is verified against shell32 version 6.0.
1726      */
1727     if (!(nFolder & CSIDL_FLAG_CREATE))
1728     {
1729         hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
1730         goto end;
1731     }
1732
1733     /* create directory/directories */
1734     ret = SHCreateDirectoryExW(hwndOwner, szBuildPath, NULL);
1735     if (ret && ret != ERROR_ALREADY_EXISTS)
1736     {
1737         ERR("Failed to create directory %s.\n", debugstr_w(szBuildPath));
1738         hr = E_FAIL;
1739         goto end;
1740     }
1741
1742     TRACE("Created missing system directory %s\n", debugstr_w(szBuildPath));
1743 end:
1744     TRACE("returning 0x%08x (final path is %s)\n", hr, debugstr_w(szBuildPath));
1745     return hr;
1746 }
1747
1748 /*************************************************************************
1749  * SHGetFolderPathA                     [SHELL32.@]
1750  *
1751  * See SHGetFolderPathW.
1752  */
1753 HRESULT WINAPI SHGetFolderPathA(
1754         HWND hwndOwner,
1755         int nFolder,
1756         HANDLE hToken,
1757         DWORD dwFlags,
1758         LPSTR pszPath)
1759 {
1760     WCHAR szTemp[MAX_PATH];
1761     HRESULT hr;
1762
1763     TRACE("%p,%p,nFolder=0x%04x\n",hwndOwner,pszPath,nFolder);
1764
1765     if (pszPath)
1766         *pszPath = '\0';
1767     hr = SHGetFolderPathW(hwndOwner, nFolder, hToken, dwFlags, szTemp);
1768     if (SUCCEEDED(hr) && pszPath)
1769         WideCharToMultiByte(CP_ACP, 0, szTemp, -1, pszPath, MAX_PATH, NULL,
1770          NULL);
1771
1772     return hr;
1773 }
1774
1775 /* For each folder in folders, if its value has not been set in the registry,
1776  * calls _SHGetUserProfilePath or _SHGetAllUsersProfilePath (depending on the
1777  * folder's type) to get the unexpanded value first.
1778  * Writes the unexpanded value to User Shell Folders, and queries it with
1779  * SHGetFolderPathW to force the creation of the directory if it doesn't
1780  * already exist.  SHGetFolderPathW also returns the expanded value, which
1781  * this then writes to Shell Folders.
1782  */
1783 static HRESULT _SHRegisterFolders(HKEY hRootKey, HANDLE hToken,
1784  LPCWSTR szUserShellFolderPath, LPCWSTR szShellFolderPath, const UINT folders[],
1785  UINT foldersLen)
1786 {
1787     UINT i;
1788     WCHAR path[MAX_PATH];
1789     HRESULT hr = S_OK;
1790     HKEY hUserKey = NULL, hKey = NULL;
1791     DWORD dwDisp, dwType, dwPathLen;
1792     LONG ret;
1793
1794     TRACE("%p,%p,%s,%p,%u\n", hRootKey, hToken,
1795      debugstr_w(szUserShellFolderPath), folders, foldersLen);
1796
1797     ret = RegCreateKeyExW(hRootKey, szUserShellFolderPath, 0, NULL, 0,
1798      KEY_ALL_ACCESS, NULL, &hUserKey, &dwDisp);
1799     if (ret)
1800         hr = HRESULT_FROM_WIN32(ret);
1801     else
1802     {
1803         ret = RegCreateKeyExW(hRootKey, szShellFolderPath, 0, NULL, 0,
1804          KEY_ALL_ACCESS, NULL, &hKey, &dwDisp);
1805         if (ret)
1806             hr = HRESULT_FROM_WIN32(ret);
1807     }
1808     for (i = 0; SUCCEEDED(hr) && i < foldersLen; i++)
1809     {
1810         dwPathLen = MAX_PATH * sizeof(WCHAR);
1811         if (RegQueryValueExW(hUserKey, CSIDL_Data[folders[i]].szValueName, NULL,
1812          &dwType, (LPBYTE)path, &dwPathLen) || (dwType != REG_SZ &&
1813          dwType != REG_EXPAND_SZ))
1814         {
1815             *path = '\0';
1816             if (CSIDL_Data[folders[i]].type == CSIDL_Type_User)
1817                 _SHGetUserProfilePath(hToken, SHGFP_TYPE_DEFAULT, folders[i],
1818                  path);
1819             else if (CSIDL_Data[folders[i]].type == CSIDL_Type_AllUsers)
1820                 _SHGetAllUsersProfilePath(SHGFP_TYPE_DEFAULT, folders[i], path);
1821             else if (CSIDL_Data[folders[i]].type == CSIDL_Type_WindowsPath)
1822                 GetWindowsDirectoryW(path, MAX_PATH);
1823             else
1824                 hr = E_FAIL;
1825             if (*path)
1826             {
1827                 ret = RegSetValueExW(hUserKey,
1828                  CSIDL_Data[folders[i]].szValueName, 0, REG_EXPAND_SZ,
1829                  (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR));
1830                 if (ret)
1831                     hr = HRESULT_FROM_WIN32(ret);
1832                 else
1833                 {
1834                     hr = SHGetFolderPathW(NULL, folders[i] | CSIDL_FLAG_CREATE,
1835                      hToken, SHGFP_TYPE_DEFAULT, path);
1836                     ret = RegSetValueExW(hKey,
1837                      CSIDL_Data[folders[i]].szValueName, 0, REG_SZ,
1838                      (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR));
1839                     if (ret)
1840                         hr = HRESULT_FROM_WIN32(ret);
1841                 }
1842             }
1843         }
1844     }
1845     if (hUserKey)
1846         RegCloseKey(hUserKey);
1847     if (hKey)
1848         RegCloseKey(hKey);
1849
1850     TRACE("returning 0x%08x\n", hr);
1851     return hr;
1852 }
1853
1854 static HRESULT _SHRegisterUserShellFolders(BOOL bDefault)
1855 {
1856     static const UINT folders[] = {
1857      CSIDL_PROGRAMS,
1858      CSIDL_PERSONAL,
1859      CSIDL_FAVORITES,
1860      CSIDL_APPDATA,
1861      CSIDL_STARTUP,
1862      CSIDL_RECENT,
1863      CSIDL_SENDTO,
1864      CSIDL_STARTMENU,
1865      CSIDL_MYMUSIC,
1866      CSIDL_MYVIDEO,
1867      CSIDL_DESKTOPDIRECTORY,
1868      CSIDL_NETHOOD,
1869      CSIDL_TEMPLATES,
1870      CSIDL_PRINTHOOD,
1871      CSIDL_LOCAL_APPDATA,
1872      CSIDL_INTERNET_CACHE,
1873      CSIDL_COOKIES,
1874      CSIDL_HISTORY,
1875      CSIDL_MYPICTURES,
1876      CSIDL_FONTS
1877     };
1878     WCHAR userShellFolderPath[MAX_PATH], shellFolderPath[MAX_PATH];
1879     LPCWSTR pUserShellFolderPath, pShellFolderPath;
1880     HRESULT hr = S_OK;
1881     HKEY hRootKey;
1882     HANDLE hToken;
1883
1884     TRACE("%s\n", bDefault ? "TRUE" : "FALSE");
1885     if (bDefault)
1886     {
1887         hToken = (HANDLE)-1;
1888         hRootKey = HKEY_USERS;
1889         strcpyW(userShellFolderPath, DefaultW);
1890         PathAddBackslashW(userShellFolderPath);
1891         strcatW(userShellFolderPath, szSHUserFolders);
1892         pUserShellFolderPath = userShellFolderPath;
1893         strcpyW(shellFolderPath, DefaultW);
1894         PathAddBackslashW(shellFolderPath);
1895         strcatW(shellFolderPath, szSHFolders);
1896         pShellFolderPath = shellFolderPath;
1897     }
1898     else
1899     {
1900         hToken = NULL;
1901         hRootKey = HKEY_CURRENT_USER;
1902         pUserShellFolderPath = szSHUserFolders;
1903         pShellFolderPath = szSHFolders;
1904     }
1905
1906     hr = _SHRegisterFolders(hRootKey, hToken, pUserShellFolderPath,
1907      pShellFolderPath, folders, sizeof(folders) / sizeof(folders[0]));
1908     TRACE("returning 0x%08x\n", hr);
1909     return hr;
1910 }
1911
1912 static HRESULT _SHRegisterCommonShellFolders(void)
1913 {
1914     static const UINT folders[] = {
1915      CSIDL_COMMON_STARTMENU,
1916      CSIDL_COMMON_PROGRAMS,
1917      CSIDL_COMMON_STARTUP,
1918      CSIDL_COMMON_DESKTOPDIRECTORY,
1919      CSIDL_COMMON_FAVORITES,
1920      CSIDL_COMMON_APPDATA,
1921      CSIDL_COMMON_TEMPLATES,
1922      CSIDL_COMMON_DOCUMENTS,
1923     };
1924     HRESULT hr;
1925
1926     TRACE("\n");
1927     hr = _SHRegisterFolders(HKEY_LOCAL_MACHINE, NULL, szSHUserFolders,
1928      szSHFolders, folders, sizeof(folders) / sizeof(folders[0]));
1929     TRACE("returning 0x%08x\n", hr);
1930     return hr;
1931 }
1932
1933 /******************************************************************************
1934  * _SHAppendToUnixPath  [Internal]
1935  *
1936  * Helper function for _SHCreateSymbolicLinks. Appends pwszSubPath (or the 
1937  * corresponding resource, if IS_INTRESOURCE) to the unix base path 'szBasePath' 
1938  * and replaces backslashes with slashes.
1939  *
1940  * PARAMS
1941  *  szBasePath  [IO] The unix base path, which will be appended to (CP_UNXICP).
1942  *  pwszSubPath [I]  Sub-path or resource id (use MAKEINTRESOURCEW).
1943  *
1944  * RETURNS
1945  *  Success: TRUE,
1946  *  Failure: FALSE
1947  */
1948 static inline BOOL _SHAppendToUnixPath(char *szBasePath, LPCWSTR pwszSubPath) {
1949     WCHAR wszSubPath[MAX_PATH];
1950     int cLen = strlen(szBasePath);
1951     char *pBackslash;
1952
1953     if (IS_INTRESOURCE(pwszSubPath)) {
1954         if (!LoadStringW(shell32_hInstance, LOWORD(pwszSubPath), wszSubPath, MAX_PATH)) {
1955             /* Fall back to hard coded defaults. */
1956             switch (LOWORD(pwszSubPath)) {
1957                 case IDS_PERSONAL:
1958                     lstrcpyW(wszSubPath, PersonalW);
1959                     break;
1960                 case IDS_MYMUSIC:
1961                     lstrcpyW(wszSubPath, My_MusicW);
1962                     break;
1963                 case IDS_MYPICTURES:
1964                     lstrcpyW(wszSubPath, My_PicturesW);
1965                     break;
1966                 case IDS_MYVIDEO:
1967                     lstrcpyW(wszSubPath, My_VideoW);
1968                     break;
1969                 default:
1970                     ERR("LoadString(%d) failed!\n", LOWORD(pwszSubPath));
1971                     return FALSE;
1972             }
1973         }
1974     } else {
1975         lstrcpyW(wszSubPath, pwszSubPath);
1976     }
1977  
1978     if (szBasePath[cLen-1] != '/') szBasePath[cLen++] = '/';
1979  
1980     if (!WideCharToMultiByte(CP_UNIXCP, 0, wszSubPath, -1, szBasePath + cLen,
1981                              FILENAME_MAX - cLen, NULL, NULL))
1982     {
1983         return FALSE;
1984     }
1985  
1986     pBackslash = szBasePath + cLen;
1987     while ((pBackslash = strchr(pBackslash, '\\'))) *pBackslash = '/';
1988  
1989     return TRUE;
1990 }
1991
1992 /******************************************************************************
1993  * _SHCreateSymbolicLinks  [Internal]
1994  * 
1995  * Sets up symbol links for various shell folders to point into the users home
1996  * directory. We do an educated guess about what the user would probably want:
1997  * - If there is a 'My Documents' directory in $HOME, the user probably wants
1998  *   wine's 'My Documents' to point there. Furthermore, we imply that the user
1999  *   is a Windows lover and has no problem with wine creating 'My Pictures',
2000  *   'My Music' and 'My Video' subfolders under '$HOME/My Documents', if those
2001  *   do not already exits. We put appropriate symbolic links in place for those,
2002  *   too.
2003  * - If there is no 'My Documents' directory in $HOME, we let 'My Documents'
2004  *   point directly to $HOME. We assume the user to be a unix hacker who does not
2005  *   want wine to create anything anywhere besides the .wine directory. So, if
2006  *   there already is a 'My Music' directory in $HOME, we symlink the 'My Music'
2007  *   shell folder to it. But if not, we symlink it to $HOME directly. The same
2008  *   holds fo 'My Pictures' and 'My Video'.
2009  * - The Desktop shell folder is symlinked to '$HOME/Desktop', if that does
2010  *   exists and left alone if not.
2011  * ('My Music',... above in fact means LoadString(IDS_MYMUSIC))
2012  */
2013 static void _SHCreateSymbolicLinks(void)
2014 {
2015     UINT aidsMyStuff[] = { IDS_MYPICTURES, IDS_MYVIDEO, IDS_MYMUSIC }, i;
2016     int acsidlMyStuff[] = { CSIDL_MYPICTURES, CSIDL_MYVIDEO, CSIDL_MYMUSIC };
2017     WCHAR wszTempPath[MAX_PATH];
2018     char szPersonalTarget[FILENAME_MAX], *pszPersonal;
2019     char szMyStuffTarget[FILENAME_MAX], *pszMyStuff;
2020     char szDesktopTarget[FILENAME_MAX], *pszDesktop;
2021     struct stat statFolder;
2022     const char *pszHome;
2023     HRESULT hr;
2024
2025     /* Create all necessary profile sub-dirs up to 'My Documents' and get the unix path. */
2026     hr = SHGetFolderPathW(NULL, CSIDL_PERSONAL|CSIDL_FLAG_CREATE, NULL,
2027                           SHGFP_TYPE_DEFAULT, wszTempPath);
2028     if (FAILED(hr)) return;
2029     pszPersonal = wine_get_unix_file_name(wszTempPath);
2030     if (!pszPersonal) return;
2031
2032     pszHome = getenv("HOME");
2033     if (pszHome && !stat(pszHome, &statFolder) && S_ISDIR(statFolder.st_mode)) {
2034         strcpy(szPersonalTarget, pszHome);
2035         if (_SHAppendToUnixPath(szPersonalTarget, MAKEINTRESOURCEW(IDS_PERSONAL)) &&
2036             !stat(szPersonalTarget, &statFolder) && S_ISDIR(statFolder.st_mode))
2037         {
2038             /* '$HOME/My Documents' exists. Create 'My Pictures', 'My Videos' and 
2039              * 'My Music' subfolders or fail silently if they already exist. */
2040             for (i = 0; i < sizeof(aidsMyStuff)/sizeof(aidsMyStuff[0]); i++) {
2041                 strcpy(szMyStuffTarget, szPersonalTarget);
2042                 if (_SHAppendToUnixPath(szMyStuffTarget, MAKEINTRESOURCEW(aidsMyStuff[i])))
2043                     mkdir(szMyStuffTarget, S_IRWXU|S_IRWXG|S_IRWXO);
2044             }
2045         } 
2046         else
2047         {
2048             /* '$HOME/My Documents' doesn't exists, but '$HOME' does. */ 
2049             strcpy(szPersonalTarget, pszHome);
2050         }
2051
2052         /* Replace 'My Documents' directory with a symlink of fail silently if not empty. */
2053         rmdir(pszPersonal);
2054         symlink(szPersonalTarget, pszPersonal);
2055     }
2056     else
2057     {
2058         /* '$HOME' doesn't exist. Create 'My Pictures', 'My Videos' and 'My Music' subdirs
2059          * in '%USERPROFILE%\\My Documents' or fail silently if they already exist. */
2060         strcpy(szPersonalTarget, pszPersonal);
2061         for (i = 0; i < sizeof(aidsMyStuff)/sizeof(aidsMyStuff[0]); i++) {
2062             strcpy(szMyStuffTarget, szPersonalTarget);
2063             if (_SHAppendToUnixPath(szMyStuffTarget, MAKEINTRESOURCEW(aidsMyStuff[i])))
2064                 mkdir(szMyStuffTarget, S_IRWXU|S_IRWXG|S_IRWXO);
2065         }
2066     }
2067
2068     HeapFree(GetProcessHeap(), 0, pszPersonal);
2069
2070     /* Create symbolic links for 'My Pictures', 'My Video' and 'My Music'. */
2071     for (i=0; i < sizeof(aidsMyStuff)/sizeof(aidsMyStuff[0]); i++) {
2072         /* Create the current 'My Whatever' folder and get it's unix path. */
2073         hr = SHGetFolderPathW(NULL, acsidlMyStuff[i]|CSIDL_FLAG_CREATE, NULL,
2074                               SHGFP_TYPE_DEFAULT, wszTempPath);
2075         if (FAILED(hr)) continue;
2076         pszMyStuff = wine_get_unix_file_name(wszTempPath);
2077         if (!pszMyStuff) continue;
2078         
2079         strcpy(szMyStuffTarget, szPersonalTarget);
2080         if (_SHAppendToUnixPath(szMyStuffTarget, MAKEINTRESOURCEW(aidsMyStuff[i])) &&
2081             !stat(szMyStuffTarget, &statFolder) && S_ISDIR(statFolder.st_mode))
2082         {
2083             /* If there's a 'My Whatever' directory where 'My Documents' links to, link to it. */
2084             rmdir(pszMyStuff);
2085             symlink(szMyStuffTarget, pszMyStuff);
2086         } 
2087         else
2088         {
2089             /* Else link to where 'My Documents' itself links to. */
2090             rmdir(pszMyStuff);
2091             symlink(szPersonalTarget, pszMyStuff);
2092         }
2093         HeapFree(GetProcessHeap(), 0, pszMyStuff);
2094     }
2095
2096     /* Last but not least, the Desktop folder */
2097     strcpy(szDesktopTarget, pszHome);
2098     if (_SHAppendToUnixPath(szDesktopTarget, DesktopW) &&
2099         !stat(szDesktopTarget, &statFolder) && S_ISDIR(statFolder.st_mode))
2100     {
2101         hr = SHGetFolderPathW(NULL, CSIDL_DESKTOPDIRECTORY|CSIDL_FLAG_CREATE, NULL,
2102                               SHGFP_TYPE_DEFAULT, wszTempPath);
2103         if (SUCCEEDED(hr) && (pszDesktop = wine_get_unix_file_name(wszTempPath))) 
2104         {
2105             rmdir(pszDesktop);
2106             symlink(szDesktopTarget, pszDesktop);
2107             HeapFree(GetProcessHeap(), 0, pszDesktop);
2108         }
2109     }
2110 }
2111
2112 /* Register the default values in the registry, as some apps seem to depend
2113  * on their presence.  The set registered was taken from Windows XP.
2114  */
2115 HRESULT SHELL_RegisterShellFolders(void)
2116 {
2117     HRESULT hr;
2118
2119     /* Set up '$HOME' targeted symlinks for 'My Documents', 'My Pictures',
2120      * 'My Video', 'My Music' and 'Desktop' in advance, so that the
2121      * _SHRegister*ShellFolders() functions will find everything nice and clean
2122      * and thus will not attempt to create them in the profile directory. */
2123     _SHCreateSymbolicLinks();
2124     
2125     hr = _SHRegisterUserShellFolders(TRUE);
2126     if (SUCCEEDED(hr))
2127         hr = _SHRegisterUserShellFolders(FALSE);
2128     if (SUCCEEDED(hr))
2129         hr = _SHRegisterCommonShellFolders();
2130     return hr;
2131 }
2132
2133 /*************************************************************************
2134  * SHGetSpecialFolderPathA [SHELL32.@]
2135  */
2136 BOOL WINAPI SHGetSpecialFolderPathA (
2137         HWND hwndOwner,
2138         LPSTR szPath,
2139         int nFolder,
2140         BOOL bCreate)
2141 {
2142         return (SHGetFolderPathA(
2143                 hwndOwner,
2144                 nFolder + (bCreate ? CSIDL_FLAG_CREATE : 0),
2145                 NULL,
2146                 0,
2147                 szPath)) == S_OK ? TRUE : FALSE;
2148 }
2149
2150 /*************************************************************************
2151  * SHGetSpecialFolderPathW
2152  */
2153 BOOL WINAPI SHGetSpecialFolderPathW (
2154         HWND hwndOwner,
2155         LPWSTR szPath,
2156         int nFolder,
2157         BOOL bCreate)
2158 {
2159         return (SHGetFolderPathW(
2160                 hwndOwner,
2161                 nFolder + (bCreate ? CSIDL_FLAG_CREATE : 0),
2162                 NULL,
2163                 0,
2164                 szPath)) == S_OK ? TRUE : FALSE;
2165 }
2166
2167 /*************************************************************************
2168  * SHGetSpecialFolderPath (SHELL32.175)
2169  */
2170 BOOL WINAPI SHGetSpecialFolderPathAW (
2171         HWND hwndOwner,
2172         LPVOID szPath,
2173         int nFolder,
2174         BOOL bCreate)
2175
2176 {
2177         if (SHELL_OsIsUnicode())
2178           return SHGetSpecialFolderPathW (hwndOwner, szPath, nFolder, bCreate);
2179         return SHGetSpecialFolderPathA (hwndOwner, szPath, nFolder, bCreate);
2180 }
2181
2182 /*************************************************************************
2183  * SHGetFolderLocation [SHELL32.@]
2184  *
2185  * Gets the folder locations from the registry and creates a pidl.
2186  *
2187  * PARAMS
2188  *   hwndOwner  [I]
2189  *   nFolder    [I] CSIDL_xxxxx
2190  *   hToken     [I] token representing user, or NULL for current user, or -1 for
2191  *                  default user
2192  *   dwReserved [I] must be zero
2193  *   ppidl      [O] PIDL of a special folder
2194  *
2195  * RETURNS
2196  *  Success: S_OK
2197  *  Failure: Standard OLE-defined error result, S_FALSE or E_INVALIDARG
2198  *
2199  * NOTES
2200  *  Creates missing reg keys and directories.
2201  *  Mostly forwards to SHGetFolderPathW, but a few values of nFolder return
2202  *  virtual folders that are handled here.
2203  */
2204 HRESULT WINAPI SHGetFolderLocation(
2205         HWND hwndOwner,
2206         int nFolder,
2207         HANDLE hToken,
2208         DWORD dwReserved,
2209         LPITEMIDLIST *ppidl)
2210 {
2211     HRESULT hr = E_INVALIDARG;
2212
2213     TRACE("%p 0x%08x %p 0x%08x %p\n",
2214      hwndOwner, nFolder, hToken, dwReserved, ppidl);
2215     
2216     if (!ppidl)
2217         return E_INVALIDARG;
2218     if (dwReserved)
2219         return E_INVALIDARG;
2220
2221     /* The virtual folders' locations are not user-dependent */
2222     *ppidl = NULL;
2223     switch (nFolder)
2224     {
2225         case CSIDL_DESKTOP:
2226             *ppidl = _ILCreateDesktop();
2227             break;
2228
2229         case CSIDL_PERSONAL:
2230             *ppidl = _ILCreateMyDocuments();
2231             break;
2232
2233         case CSIDL_INTERNET:
2234             *ppidl = _ILCreateIExplore();
2235             break;
2236
2237         case CSIDL_CONTROLS:
2238             *ppidl = _ILCreateControlPanel();
2239             break;
2240
2241         case CSIDL_PRINTERS:
2242             *ppidl = _ILCreatePrinters();
2243             break;
2244
2245         case CSIDL_BITBUCKET:
2246             *ppidl = _ILCreateBitBucket();
2247             break;
2248
2249         case CSIDL_DRIVES:
2250             *ppidl = _ILCreateMyComputer();
2251             break;
2252
2253         case CSIDL_NETWORK:
2254             *ppidl = _ILCreateNetwork();
2255             break;
2256
2257         default:
2258         {
2259             WCHAR szPath[MAX_PATH];
2260
2261             hr = SHGetFolderPathW(hwndOwner, nFolder, hToken,
2262              SHGFP_TYPE_CURRENT, szPath);
2263             if (SUCCEEDED(hr))
2264             {
2265                 DWORD attributes=0;
2266
2267                 TRACE("Value=%s\n", debugstr_w(szPath));
2268                 hr = SHILCreateFromPathW(szPath, ppidl, &attributes);
2269             }
2270             else if (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
2271             {
2272                 /* unlike SHGetFolderPath, SHGetFolderLocation in shell32
2273                  * version 6.0 returns E_FAIL for nonexistent paths
2274                  */
2275                 hr = E_FAIL;
2276             }
2277         }
2278     }
2279     if(*ppidl)
2280         hr = NOERROR;
2281
2282     TRACE("-- (new pidl %p)\n",*ppidl);
2283     return hr;
2284 }
2285
2286 /*************************************************************************
2287  * SHGetSpecialFolderLocation           [SHELL32.@]
2288  *
2289  * NOTES
2290  *   In NT5, SHGetSpecialFolderLocation needs the <winntdir>/Recent
2291  *   directory.
2292  */
2293 HRESULT WINAPI SHGetSpecialFolderLocation(
2294         HWND hwndOwner,
2295         INT nFolder,
2296         LPITEMIDLIST * ppidl)
2297 {
2298     HRESULT hr = E_INVALIDARG;
2299
2300     TRACE("(%p,0x%x,%p)\n", hwndOwner,nFolder,ppidl);
2301
2302     if (!ppidl)
2303         return E_INVALIDARG;
2304
2305     hr = SHGetFolderLocation(hwndOwner, nFolder, NULL, 0, ppidl);
2306     return hr;
2307 }