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