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