shell32: Fall back on CSIDL path for knownfolders without a path in the registry.
[wine.git] / dlls / shell32 / shellpath.c
blob84e663b4e62b090c58751b5b4ce0415448d48f23
1 /*
2 * Path Functions
4 * Copyright 1998, 1999, 2000 Juergen Schmied
5 * Copyright 2004 Juan Lang
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.
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.
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
21 * NOTES:
23 * Many of these functions are in SHLWAPI.DLL also
27 #define COBJMACROS
29 #include "config.h"
30 #include "wine/port.h"
32 #include <stdio.h>
33 #include <stdarg.h>
34 #include <string.h>
35 #include <ctype.h>
36 #include "wine/debug.h"
37 #include "windef.h"
38 #include "winbase.h"
39 #include "winnls.h"
40 #include "winreg.h"
41 #include "wingdi.h"
42 #include "winuser.h"
44 #include "shlobj.h"
45 #include "shtypes.h"
46 #include "shresdef.h"
47 #include "shell32_main.h"
48 #include "undocshell.h"
49 #include "pidl.h"
50 #include "wine/unicode.h"
51 #include "shlwapi.h"
52 #include "xdg.h"
53 #include "sddl.h"
54 #include "knownfolders.h"
55 #include "initguid.h"
56 #include "shobjidl.h"
58 WINE_DEFAULT_DEBUG_CHANNEL(shell);
60 static const BOOL is_win64 = sizeof(void *) > sizeof(int);
63 ########## Combining and Constructing paths ##########
66 /*************************************************************************
67 * PathAppend [SHELL32.36]
69 BOOL WINAPI PathAppendAW(
70 LPVOID lpszPath1,
71 LPCVOID lpszPath2)
73 if (SHELL_OsIsUnicode())
74 return PathAppendW(lpszPath1, lpszPath2);
75 return PathAppendA(lpszPath1, lpszPath2);
78 /*************************************************************************
79 * PathCombine [SHELL32.37]
81 LPVOID WINAPI PathCombineAW(
82 LPVOID szDest,
83 LPCVOID lpszDir,
84 LPCVOID lpszFile)
86 if (SHELL_OsIsUnicode())
87 return PathCombineW( szDest, lpszDir, lpszFile );
88 return PathCombineA( szDest, lpszDir, lpszFile );
91 /*************************************************************************
92 * PathAddBackslash [SHELL32.32]
94 LPVOID WINAPI PathAddBackslashAW(LPVOID lpszPath)
96 if(SHELL_OsIsUnicode())
97 return PathAddBackslashW(lpszPath);
98 return PathAddBackslashA(lpszPath);
101 /*************************************************************************
102 * PathBuildRoot [SHELL32.30]
104 LPVOID WINAPI PathBuildRootAW(LPVOID lpszPath, int drive)
106 if(SHELL_OsIsUnicode())
107 return PathBuildRootW(lpszPath, drive);
108 return PathBuildRootA(lpszPath, drive);
112 Extracting Component Parts
115 /*************************************************************************
116 * PathFindFileName [SHELL32.34]
118 LPVOID WINAPI PathFindFileNameAW(LPCVOID lpszPath)
120 if(SHELL_OsIsUnicode())
121 return PathFindFileNameW(lpszPath);
122 return PathFindFileNameA(lpszPath);
125 /*************************************************************************
126 * PathFindExtension [SHELL32.31]
128 LPVOID WINAPI PathFindExtensionAW(LPCVOID lpszPath)
130 if (SHELL_OsIsUnicode())
131 return PathFindExtensionW(lpszPath);
132 return PathFindExtensionA(lpszPath);
136 /*************************************************************************
137 * PathGetExtensionA [internal]
139 * NOTES
140 * exported by ordinal
141 * return value points to the first char after the dot
143 static LPSTR PathGetExtensionA(LPCSTR lpszPath)
145 TRACE("(%s)\n",lpszPath);
147 lpszPath = PathFindExtensionA(lpszPath);
148 return (LPSTR)(*lpszPath?(lpszPath+1):lpszPath);
151 /*************************************************************************
152 * PathGetExtensionW [internal]
154 static LPWSTR PathGetExtensionW(LPCWSTR lpszPath)
156 TRACE("(%s)\n",debugstr_w(lpszPath));
158 lpszPath = PathFindExtensionW(lpszPath);
159 return (LPWSTR)(*lpszPath?(lpszPath+1):lpszPath);
162 /*************************************************************************
163 * PathGetExtension [SHELL32.158]
165 LPVOID WINAPI PathGetExtensionAW(LPCVOID lpszPath,DWORD void1, DWORD void2)
167 if (SHELL_OsIsUnicode())
168 return PathGetExtensionW(lpszPath);
169 return PathGetExtensionA(lpszPath);
172 /*************************************************************************
173 * PathGetArgs [SHELL32.52]
175 LPVOID WINAPI PathGetArgsAW(LPVOID lpszPath)
177 if (SHELL_OsIsUnicode())
178 return PathGetArgsW(lpszPath);
179 return PathGetArgsA(lpszPath);
182 /*************************************************************************
183 * PathGetDriveNumber [SHELL32.57]
185 int WINAPI PathGetDriveNumberAW(LPVOID lpszPath)
187 if (SHELL_OsIsUnicode())
188 return PathGetDriveNumberW(lpszPath);
189 return PathGetDriveNumberA(lpszPath);
192 /*************************************************************************
193 * PathRemoveFileSpec [SHELL32.35]
195 BOOL WINAPI PathRemoveFileSpecAW(LPVOID lpszPath)
197 if (SHELL_OsIsUnicode())
198 return PathRemoveFileSpecW(lpszPath);
199 return PathRemoveFileSpecA(lpszPath);
202 /*************************************************************************
203 * PathStripPath [SHELL32.38]
205 void WINAPI PathStripPathAW(LPVOID lpszPath)
207 if (SHELL_OsIsUnicode())
208 PathStripPathW(lpszPath);
209 else
210 PathStripPathA(lpszPath);
213 /*************************************************************************
214 * PathStripToRoot [SHELL32.50]
216 BOOL WINAPI PathStripToRootAW(LPVOID lpszPath)
218 if (SHELL_OsIsUnicode())
219 return PathStripToRootW(lpszPath);
220 return PathStripToRootA(lpszPath);
223 /*************************************************************************
224 * PathRemoveArgs [SHELL32.251]
226 void WINAPI PathRemoveArgsAW(LPVOID lpszPath)
228 if (SHELL_OsIsUnicode())
229 PathRemoveArgsW(lpszPath);
230 else
231 PathRemoveArgsA(lpszPath);
234 /*************************************************************************
235 * PathRemoveExtension [SHELL32.250]
237 void WINAPI PathRemoveExtensionAW(LPVOID lpszPath)
239 if (SHELL_OsIsUnicode())
240 PathRemoveExtensionW(lpszPath);
241 else
242 PathRemoveExtensionA(lpszPath);
247 Path Manipulations
250 /*************************************************************************
251 * PathGetShortPathA [internal]
253 static void PathGetShortPathA(LPSTR pszPath)
255 CHAR path[MAX_PATH];
257 TRACE("%s\n", pszPath);
259 if (GetShortPathNameA(pszPath, path, MAX_PATH))
261 lstrcpyA(pszPath, path);
265 /*************************************************************************
266 * PathGetShortPathW [internal]
268 static void PathGetShortPathW(LPWSTR pszPath)
270 WCHAR path[MAX_PATH];
272 TRACE("%s\n", debugstr_w(pszPath));
274 if (GetShortPathNameW(pszPath, path, MAX_PATH))
276 lstrcpyW(pszPath, path);
280 /*************************************************************************
281 * PathGetShortPath [SHELL32.92]
283 VOID WINAPI PathGetShortPathAW(LPVOID pszPath)
285 if(SHELL_OsIsUnicode())
286 PathGetShortPathW(pszPath);
287 PathGetShortPathA(pszPath);
290 /*************************************************************************
291 * PathRemoveBlanks [SHELL32.33]
293 void WINAPI PathRemoveBlanksAW(LPVOID str)
295 if(SHELL_OsIsUnicode())
296 PathRemoveBlanksW(str);
297 else
298 PathRemoveBlanksA(str);
301 /*************************************************************************
302 * PathQuoteSpaces [SHELL32.55]
304 VOID WINAPI PathQuoteSpacesAW (LPVOID lpszPath)
306 if(SHELL_OsIsUnicode())
307 PathQuoteSpacesW(lpszPath);
308 else
309 PathQuoteSpacesA(lpszPath);
312 /*************************************************************************
313 * PathUnquoteSpaces [SHELL32.56]
315 VOID WINAPI PathUnquoteSpacesAW(LPVOID str)
317 if(SHELL_OsIsUnicode())
318 PathUnquoteSpacesW(str);
319 else
320 PathUnquoteSpacesA(str);
323 /*************************************************************************
324 * PathParseIconLocation [SHELL32.249]
326 int WINAPI PathParseIconLocationAW (LPVOID lpszPath)
328 if(SHELL_OsIsUnicode())
329 return PathParseIconLocationW(lpszPath);
330 return PathParseIconLocationA(lpszPath);
334 ########## Path Testing ##########
336 /*************************************************************************
337 * PathIsUNC [SHELL32.39]
339 BOOL WINAPI PathIsUNCAW (LPCVOID lpszPath)
341 if (SHELL_OsIsUnicode())
342 return PathIsUNCW( lpszPath );
343 return PathIsUNCA( lpszPath );
346 /*************************************************************************
347 * PathIsRelative [SHELL32.40]
349 BOOL WINAPI PathIsRelativeAW (LPCVOID lpszPath)
351 if (SHELL_OsIsUnicode())
352 return PathIsRelativeW( lpszPath );
353 return PathIsRelativeA( lpszPath );
356 /*************************************************************************
357 * PathIsRoot [SHELL32.29]
359 BOOL WINAPI PathIsRootAW(LPCVOID lpszPath)
361 if (SHELL_OsIsUnicode())
362 return PathIsRootW(lpszPath);
363 return PathIsRootA(lpszPath);
366 /*************************************************************************
367 * PathIsExeA [internal]
369 static BOOL PathIsExeA (LPCSTR lpszPath)
371 LPCSTR lpszExtension = PathGetExtensionA(lpszPath);
372 int i;
373 static const char * const lpszExtensions[] =
374 {"exe", "com", "pif", "cmd", "bat", "scf", "scr", NULL };
376 TRACE("path=%s\n",lpszPath);
378 for(i=0; lpszExtensions[i]; i++)
379 if (!lstrcmpiA(lpszExtension,lpszExtensions[i])) return TRUE;
381 return FALSE;
384 /*************************************************************************
385 * PathIsExeW [internal]
387 static BOOL PathIsExeW (LPCWSTR lpszPath)
389 LPCWSTR lpszExtension = PathGetExtensionW(lpszPath);
390 int i;
391 static const WCHAR lpszExtensions[][4] =
392 {{'e','x','e','\0'}, {'c','o','m','\0'}, {'p','i','f','\0'},
393 {'c','m','d','\0'}, {'b','a','t','\0'}, {'s','c','f','\0'},
394 {'s','c','r','\0'}, {'\0'} };
396 TRACE("path=%s\n",debugstr_w(lpszPath));
398 for(i=0; lpszExtensions[i][0]; i++)
399 if (!strcmpiW(lpszExtension,lpszExtensions[i])) return TRUE;
401 return FALSE;
404 /*************************************************************************
405 * PathIsExe [SHELL32.43]
407 BOOL WINAPI PathIsExeAW (LPCVOID path)
409 if (SHELL_OsIsUnicode())
410 return PathIsExeW (path);
411 return PathIsExeA(path);
414 /*************************************************************************
415 * PathIsDirectory [SHELL32.159]
417 BOOL WINAPI PathIsDirectoryAW (LPCVOID lpszPath)
419 if (SHELL_OsIsUnicode())
420 return PathIsDirectoryW (lpszPath);
421 return PathIsDirectoryA (lpszPath);
424 /*************************************************************************
425 * PathFileExists [SHELL32.45]
427 BOOL WINAPI PathFileExistsAW (LPCVOID lpszPath)
429 if (SHELL_OsIsUnicode())
430 return PathFileExistsW (lpszPath);
431 return PathFileExistsA (lpszPath);
434 /*************************************************************************
435 * PathMatchSpec [SHELL32.46]
437 BOOL WINAPI PathMatchSpecAW(LPVOID name, LPVOID mask)
439 if (SHELL_OsIsUnicode())
440 return PathMatchSpecW( name, mask );
441 return PathMatchSpecA( name, mask );
444 /*************************************************************************
445 * PathIsSameRoot [SHELL32.650]
447 BOOL WINAPI PathIsSameRootAW(LPCVOID lpszPath1, LPCVOID lpszPath2)
449 if (SHELL_OsIsUnicode())
450 return PathIsSameRootW(lpszPath1, lpszPath2);
451 return PathIsSameRootA(lpszPath1, lpszPath2);
454 /*************************************************************************
455 * IsLFNDriveA [SHELL32.41]
457 BOOL WINAPI IsLFNDriveA(LPCSTR lpszPath)
459 DWORD fnlen;
461 if (!GetVolumeInformationA(lpszPath, NULL, 0, NULL, &fnlen, NULL, NULL, 0))
462 return FALSE;
463 return fnlen > 12;
466 /*************************************************************************
467 * IsLFNDriveW [SHELL32.42]
469 BOOL WINAPI IsLFNDriveW(LPCWSTR lpszPath)
471 DWORD fnlen;
473 if (!GetVolumeInformationW(lpszPath, NULL, 0, NULL, &fnlen, NULL, NULL, 0))
474 return FALSE;
475 return fnlen > 12;
478 /*************************************************************************
479 * IsLFNDrive [SHELL32.119]
481 BOOL WINAPI IsLFNDriveAW(LPCVOID lpszPath)
483 if (SHELL_OsIsUnicode())
484 return IsLFNDriveW(lpszPath);
485 return IsLFNDriveA(lpszPath);
489 ########## Creating Something Unique ##########
491 /*************************************************************************
492 * PathMakeUniqueNameA [internal]
494 static BOOL PathMakeUniqueNameA(
495 LPSTR lpszBuffer,
496 DWORD dwBuffSize,
497 LPCSTR lpszShortName,
498 LPCSTR lpszLongName,
499 LPCSTR lpszPathName)
501 FIXME("%p %u %s %s %s stub\n",
502 lpszBuffer, dwBuffSize, debugstr_a(lpszShortName),
503 debugstr_a(lpszLongName), debugstr_a(lpszPathName));
504 return TRUE;
507 /*************************************************************************
508 * PathMakeUniqueNameW [internal]
510 static BOOL PathMakeUniqueNameW(
511 LPWSTR lpszBuffer,
512 DWORD dwBuffSize,
513 LPCWSTR lpszShortName,
514 LPCWSTR lpszLongName,
515 LPCWSTR lpszPathName)
517 FIXME("%p %u %s %s %s stub\n",
518 lpszBuffer, dwBuffSize, debugstr_w(lpszShortName),
519 debugstr_w(lpszLongName), debugstr_w(lpszPathName));
520 return TRUE;
523 /*************************************************************************
524 * PathMakeUniqueName [SHELL32.47]
526 BOOL WINAPI PathMakeUniqueNameAW(
527 LPVOID lpszBuffer,
528 DWORD dwBuffSize,
529 LPCVOID lpszShortName,
530 LPCVOID lpszLongName,
531 LPCVOID lpszPathName)
533 if (SHELL_OsIsUnicode())
534 return PathMakeUniqueNameW(lpszBuffer,dwBuffSize, lpszShortName,lpszLongName,lpszPathName);
535 return PathMakeUniqueNameA(lpszBuffer,dwBuffSize, lpszShortName,lpszLongName,lpszPathName);
538 /*************************************************************************
539 * PathYetAnotherMakeUniqueName [SHELL32.75]
541 BOOL WINAPI PathYetAnotherMakeUniqueName(LPWSTR buffer, LPCWSTR path, LPCWSTR shortname, LPCWSTR longname)
543 WCHAR pathW[MAX_PATH], retW[MAX_PATH];
544 const WCHAR *file, *ext;
545 int i = 2;
547 TRACE("(%p, %s, %s, %s)\n", buffer, debugstr_w(path), debugstr_w(shortname), debugstr_w(longname));
549 file = longname ? longname : shortname;
550 PathCombineW(pathW, path, file);
551 strcpyW(retW, pathW);
552 PathRemoveExtensionW(pathW);
554 ext = PathFindExtensionW(file);
556 /* now try to make it unique */
557 while (PathFileExistsW(retW))
559 static const WCHAR fmtW[] = {'%','s',' ','(','%','d',')','%','s',0};
561 sprintfW(retW, fmtW, pathW, i, ext);
562 i++;
565 strcpyW(buffer, retW);
566 TRACE("ret - %s\n", debugstr_w(buffer));
568 return TRUE;
572 ########## cleaning and resolving paths ##########
575 /*************************************************************************
576 * PathFindOnPath [SHELL32.145]
578 BOOL WINAPI PathFindOnPathAW(LPVOID sFile, LPCVOID *sOtherDirs)
580 if (SHELL_OsIsUnicode())
581 return PathFindOnPathW(sFile, (LPCWSTR *)sOtherDirs);
582 return PathFindOnPathA(sFile, (LPCSTR *)sOtherDirs);
585 /*************************************************************************
586 * PathCleanupSpec [SHELL32.171]
588 * lpszFile is changed in place.
590 int WINAPI PathCleanupSpec( LPCWSTR lpszPathW, LPWSTR lpszFileW )
592 int i = 0;
593 DWORD rc = 0;
594 int length = 0;
596 if (SHELL_OsIsUnicode())
598 LPWSTR p = lpszFileW;
600 TRACE("Cleanup %s\n",debugstr_w(lpszFileW));
602 if (lpszPathW)
603 length = strlenW(lpszPathW);
605 while (*p)
607 int gct = PathGetCharTypeW(*p);
608 if (gct == GCT_INVALID || gct == GCT_WILD || gct == GCT_SEPARATOR)
610 lpszFileW[i]='-';
611 rc |= PCS_REPLACEDCHAR;
613 else
614 lpszFileW[i]=*p;
615 i++;
616 p++;
617 if (length + i == MAX_PATH)
619 rc |= PCS_FATAL | PCS_PATHTOOLONG;
620 break;
623 lpszFileW[i]=0;
625 else
627 LPSTR lpszFileA = (LPSTR)lpszFileW;
628 LPCSTR lpszPathA = (LPCSTR)lpszPathW;
629 LPSTR p = lpszFileA;
631 TRACE("Cleanup %s\n",debugstr_a(lpszFileA));
633 if (lpszPathA)
634 length = strlen(lpszPathA);
636 while (*p)
638 int gct = PathGetCharTypeA(*p);
639 if (gct == GCT_INVALID || gct == GCT_WILD || gct == GCT_SEPARATOR)
641 lpszFileA[i]='-';
642 rc |= PCS_REPLACEDCHAR;
644 else
645 lpszFileA[i]=*p;
646 i++;
647 p++;
648 if (length + i == MAX_PATH)
650 rc |= PCS_FATAL | PCS_PATHTOOLONG;
651 break;
654 lpszFileA[i]=0;
656 return rc;
659 /*************************************************************************
660 * PathQualifyA [SHELL32]
662 static BOOL PathQualifyA(LPCSTR pszPath)
664 FIXME("%s\n",pszPath);
665 return FALSE;
668 /*************************************************************************
669 * PathQualifyW [SHELL32]
671 static BOOL PathQualifyW(LPCWSTR pszPath)
673 FIXME("%s\n",debugstr_w(pszPath));
674 return FALSE;
677 /*************************************************************************
678 * PathQualify [SHELL32.49]
680 BOOL WINAPI PathQualifyAW(LPCVOID pszPath)
682 if (SHELL_OsIsUnicode())
683 return PathQualifyW(pszPath);
684 return PathQualifyA(pszPath);
687 static BOOL PathResolveA(LPSTR path, LPCSTR *paths, DWORD flags)
689 FIXME("(%s,%p,0x%08x),stub!\n", debugstr_a(path), paths, flags);
690 return FALSE;
693 static BOOL PathResolveW(LPWSTR path, LPCWSTR *paths, DWORD flags)
695 FIXME("(%s,%p,0x%08x),stub!\n", debugstr_w(path), paths, flags);
696 return FALSE;
699 /*************************************************************************
700 * PathResolve [SHELL32.51]
702 BOOL WINAPI PathResolveAW(LPVOID path, LPCVOID *paths, DWORD flags)
704 if (SHELL_OsIsUnicode())
705 return PathResolveW(path, (LPCWSTR*)paths, flags);
706 else
707 return PathResolveA(path, (LPCSTR*)paths, flags);
710 /*************************************************************************
711 * PathProcessCommandA
713 static LONG PathProcessCommandA (
714 LPCSTR lpszPath,
715 LPSTR lpszBuff,
716 DWORD dwBuffSize,
717 DWORD dwFlags)
719 FIXME("%s %p 0x%04x 0x%04x stub\n",
720 lpszPath, lpszBuff, dwBuffSize, dwFlags);
721 if(!lpszPath) return -1;
722 if(lpszBuff) strcpy(lpszBuff, lpszPath);
723 return strlen(lpszPath);
726 /*************************************************************************
727 * PathProcessCommandW
729 static LONG PathProcessCommandW (
730 LPCWSTR lpszPath,
731 LPWSTR lpszBuff,
732 DWORD dwBuffSize,
733 DWORD dwFlags)
735 FIXME("(%s, %p, 0x%04x, 0x%04x) stub\n",
736 debugstr_w(lpszPath), lpszBuff, dwBuffSize, dwFlags);
737 if(!lpszPath) return -1;
738 if(lpszBuff) strcpyW(lpszBuff, lpszPath);
739 return strlenW(lpszPath);
742 /*************************************************************************
743 * PathProcessCommand (SHELL32.653)
745 LONG WINAPI PathProcessCommandAW (
746 LPCVOID lpszPath,
747 LPVOID lpszBuff,
748 DWORD dwBuffSize,
749 DWORD dwFlags)
751 if (SHELL_OsIsUnicode())
752 return PathProcessCommandW(lpszPath, lpszBuff, dwBuffSize, dwFlags);
753 return PathProcessCommandA(lpszPath, lpszBuff, dwBuffSize, dwFlags);
757 ########## special ##########
760 /*************************************************************************
761 * PathSetDlgItemPath (SHELL32.48)
763 VOID WINAPI PathSetDlgItemPathAW(HWND hDlg, int id, LPCVOID pszPath)
765 if (SHELL_OsIsUnicode())
766 PathSetDlgItemPathW(hDlg, id, pszPath);
767 else
768 PathSetDlgItemPathA(hDlg, id, pszPath);
771 static const WCHAR szCategory[] = {'C','a','t','e','g','o','r','y',0};
772 static const WCHAR szAttributes[] = {'A','t','t','r','i','b','u','t','e','s',0};
773 static const WCHAR szName[] = {'N','a','m','e',0};
774 static const WCHAR szRelativePath[] = {'R','e','l','a','t','i','v','e','P','a','t','h',0};
775 static const WCHAR szParentFolder[] = {'P','a','r','e','n','t','F','o','l','d','e','r',0};
777 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'};
778 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'};
779 static const WCHAR AppDataW[] = {'A','p','p','D','a','t','a','\0'};
780 static const WCHAR AppData_LocalLowW[] = {'A','p','p','D','a','t','a','\\','L','o','c','a','l','L','o','w','\0'};
781 static const WCHAR Application_DataW[] = {'A','p','p','l','i','c','a','t','i','o','n',' ','D','a','t','a','\0'};
782 static const WCHAR CacheW[] = {'C','a','c','h','e','\0'};
783 static const WCHAR CD_BurningW[] = {'C','D',' ','B','u','r','n','i','n','g','\0'};
784 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'};
785 static const WCHAR Common_AppDataW[] = {'C','o','m','m','o','n',' ','A','p','p','D','a','t','a','\0'};
786 static const WCHAR Common_DesktopW[] = {'C','o','m','m','o','n',' ','D','e','s','k','t','o','p','\0'};
787 static const WCHAR Common_DocumentsW[] = {'C','o','m','m','o','n',' ','D','o','c','u','m','e','n','t','s','\0'};
788 static const WCHAR Common_FavoritesW[] = {'C','o','m','m','o','n',' ','F','a','v','o','r','i','t','e','s','\0'};
789 static const WCHAR CommonFilesDirW[] = {'C','o','m','m','o','n','F','i','l','e','s','D','i','r','\0'};
790 static const WCHAR CommonFilesDirX86W[] = {'C','o','m','m','o','n','F','i','l','e','s','D','i','r',' ','(','x','8','6',')','\0'};
791 static const WCHAR CommonMusicW[] = {'C','o','m','m','o','n','M','u','s','i','c','\0'};
792 static const WCHAR CommonPicturesW[] = {'C','o','m','m','o','n','P','i','c','t','u','r','e','s','\0'};
793 static const WCHAR Common_ProgramsW[] = {'C','o','m','m','o','n',' ','P','r','o','g','r','a','m','s','\0'};
794 static const WCHAR Common_StartUpW[] = {'C','o','m','m','o','n',' ','S','t','a','r','t','U','p','\0'};
795 static const WCHAR Common_Start_MenuW[] = {'C','o','m','m','o','n',' ','S','t','a','r','t',' ','M','e','n','u','\0'};
796 static const WCHAR Common_TemplatesW[] = {'C','o','m','m','o','n',' ','T','e','m','p','l','a','t','e','s','\0'};
797 static const WCHAR CommonVideoW[] = {'C','o','m','m','o','n','V','i','d','e','o','\0'};
798 static const WCHAR ContactsW[] = {'C','o','n','t','a','c','t','s','\0'};
799 static const WCHAR CookiesW[] = {'C','o','o','k','i','e','s','\0'};
800 static const WCHAR DesktopW[] = {'D','e','s','k','t','o','p','\0'};
801 static const WCHAR DocumentsW[] = {'D','o','c','u','m','e','n','t','s','\0'};
802 static const WCHAR DownloadsW[] = {'D','o','w','n','l','o','a','d','s','\0'};
803 static const WCHAR FavoritesW[] = {'F','a','v','o','r','i','t','e','s','\0'};
804 static const WCHAR FontsW[] = {'F','o','n','t','s','\0'};
805 static const WCHAR HistoryW[] = {'H','i','s','t','o','r','y','\0'};
806 static const WCHAR LinksW[] = {'L','i','n','k','s','\0'};
807 static const WCHAR Local_AppDataW[] = {'L','o','c','a','l',' ','A','p','p','D','a','t','a','\0'};
808 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'};
809 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'};
810 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'};
811 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'};
812 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'};
813 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'};
814 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'};
815 static const WCHAR MusicW[] = {'M','u','s','i','c','\0'};
816 static const WCHAR Music_PlaylistsW[] = {'M','u','s','i','c','\\','P','l','a','y','l','i','s','t','s','\0'};
817 static const WCHAR Music_Sample_MusicW[] = {'M','u','s','i','c','\\','S','a','m','p','l','e',' ','M','u','s','i','c','\0'};
818 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'};
819 static const WCHAR My_MusicW[] = {'M','y',' ','M','u','s','i','c','\0'};
820 static const WCHAR My_PicturesW[] = {'M','y',' ','P','i','c','t','u','r','e','s','\0'};
821 static const WCHAR My_VideosW[] = {'M','y',' ','V','i','d','e','o','s','\0'};
822 static const WCHAR NetHoodW[] = {'N','e','t','H','o','o','d','\0'};
823 static const WCHAR OEM_LinksW[] = {'O','E','M',' ','L','i','n','k','s','\0'};
824 static const WCHAR PersonalW[] = {'P','e','r','s','o','n','a','l','\0'};
825 static const WCHAR PicturesW[] = {'P','i','c','t','u','r','e','s','\0'};
826 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'};
827 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'};
828 static const WCHAR PrintHoodW[] = {'P','r','i','n','t','H','o','o','d','\0'};
829 static const WCHAR Program_FilesW[] = {'P','r','o','g','r','a','m',' ','F','i','l','e','s','\0'};
830 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'};
831 static const WCHAR Program_Files_x86W[] = {'P','r','o','g','r','a','m',' ','F','i','l','e','s',' ','(','x','8','6',')','\0'};
832 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'};
833 static const WCHAR ProgramFilesDirW[] = {'P','r','o','g','r','a','m','F','i','l','e','s','D','i','r','\0'};
834 static const WCHAR ProgramFilesDirX86W[] = {'P','r','o','g','r','a','m','F','i','l','e','s','D','i','r',' ','(','x','8','6',')','\0'};
835 static const WCHAR ProgramsW[] = {'P','r','o','g','r','a','m','s','\0'};
836 static const WCHAR RecentW[] = {'R','e','c','e','n','t','\0'};
837 static const WCHAR ResourcesW[] = {'R','e','s','o','u','r','c','e','s','\0'};
838 static const WCHAR Saved_GamesW[] = {'S','a','v','e','d',' ','G','a','m','e','s','\0'};
839 static const WCHAR SearchesW[] = {'S','e','a','r','c','h','e','s','\0'};
840 static const WCHAR SendToW[] = {'S','e','n','d','T','o','\0'};
841 static const WCHAR StartUpW[] = {'S','t','a','r','t','U','p','\0'};
842 static const WCHAR Start_MenuW[] = {'S','t','a','r','t',' ','M','e','n','u','\0'};
843 static const WCHAR Start_Menu_ProgramsW[] = {'S','t','a','r','t',' ','M','e','n','u','\\','P','r','o','g','r','a','m','s','\0'};
844 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'};
845 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'};
846 static const WCHAR TemplatesW[] = {'T','e','m','p','l','a','t','e','s','\0'};
847 static const WCHAR UsersW[] = {'U','s','e','r','s','\0'};
848 static const WCHAR UsersPublicW[] = {'U','s','e','r','s','\\','P','u','b','l','i','c','\0'};
849 static const WCHAR VideosW[] = {'V','i','d','e','o','s','\0'};
850 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'};
851 static const WCHAR DefaultW[] = {'.','D','e','f','a','u','l','t','\0'};
852 static const WCHAR AllUsersProfileW[] = {'%','A','L','L','U','S','E','R','S','P','R','O','F','I','L','E','%','\0'};
853 static const WCHAR UserProfileW[] = {'%','U','S','E','R','P','R','O','F','I','L','E','%','\0'};
854 static const WCHAR SystemDriveW[] = {'%','S','y','s','t','e','m','D','r','i','v','e','%','\0'};
855 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};
856 static const WCHAR ProfilesDirectoryW[] = {'P','r','o','f','i','l','e','s','D','i','r','e','c','t','o','r','y',0};
857 static const WCHAR AllUsersProfileValueW[] = {'A','l','l','U','s','e','r','s','P','r','o','f','i','l','e','\0'};
858 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'};
859 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'};
860 static const WCHAR szDefaultProfileDirW[] = {'u','s','e','r','s',0};
861 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'};
862 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};
863 static const WCHAR AllUsersW[] = {'P','u','b','l','i','c',0};
865 typedef enum _CSIDL_Type {
866 CSIDL_Type_User,
867 CSIDL_Type_AllUsers,
868 CSIDL_Type_CurrVer,
869 CSIDL_Type_Disallowed,
870 CSIDL_Type_NonExistent,
871 CSIDL_Type_WindowsPath,
872 CSIDL_Type_SystemPath,
873 CSIDL_Type_SystemX86Path,
874 } CSIDL_Type;
876 #define CSIDL_CONTACTS 0x0043
877 #define CSIDL_DOWNLOADS 0x0047
878 #define CSIDL_LINKS 0x004d
879 #define CSIDL_APPDATA_LOCALLOW 0x004e
880 #define CSIDL_SAVED_GAMES 0x0062
881 #define CSIDL_SEARCHES 0x0063
883 typedef struct
885 const KNOWNFOLDERID *id;
886 CSIDL_Type type;
887 LPCWSTR szValueName;
888 LPCWSTR szDefaultPath; /* fallback string or resource ID */
889 } CSIDL_DATA;
891 static const CSIDL_DATA CSIDL_Data[] =
893 { /* 0x00 - CSIDL_DESKTOP */
894 &FOLDERID_Desktop,
895 CSIDL_Type_User,
896 DesktopW,
897 MAKEINTRESOURCEW(IDS_DESKTOPDIRECTORY)
899 { /* 0x01 - CSIDL_INTERNET */
900 &FOLDERID_InternetFolder,
901 CSIDL_Type_Disallowed,
902 NULL,
903 NULL
905 { /* 0x02 - CSIDL_PROGRAMS */
906 &FOLDERID_Programs,
907 CSIDL_Type_User,
908 ProgramsW,
909 Start_Menu_ProgramsW
911 { /* 0x03 - CSIDL_CONTROLS (.CPL files) */
912 &FOLDERID_ControlPanelFolder,
913 CSIDL_Type_SystemPath,
914 NULL,
915 NULL
917 { /* 0x04 - CSIDL_PRINTERS */
918 &FOLDERID_PrintersFolder,
919 CSIDL_Type_SystemPath,
920 NULL,
921 NULL
923 { /* 0x05 - CSIDL_PERSONAL */
924 &FOLDERID_Documents,
925 CSIDL_Type_User,
926 PersonalW,
927 MAKEINTRESOURCEW(IDS_PERSONAL)
929 { /* 0x06 - CSIDL_FAVORITES */
930 &FOLDERID_Favorites,
931 CSIDL_Type_User,
932 FavoritesW,
933 FavoritesW
935 { /* 0x07 - CSIDL_STARTUP */
936 &FOLDERID_Startup,
937 CSIDL_Type_User,
938 StartUpW,
939 Start_Menu_StartupW
941 { /* 0x08 - CSIDL_RECENT */
942 &FOLDERID_Recent,
943 CSIDL_Type_User,
944 RecentW,
945 RecentW
947 { /* 0x09 - CSIDL_SENDTO */
948 &FOLDERID_SendTo,
949 CSIDL_Type_User,
950 SendToW,
951 SendToW
953 { /* 0x0a - CSIDL_BITBUCKET - Recycle Bin */
954 &FOLDERID_RecycleBinFolder,
955 CSIDL_Type_Disallowed,
956 NULL,
957 NULL,
959 { /* 0x0b - CSIDL_STARTMENU */
960 &FOLDERID_StartMenu,
961 CSIDL_Type_User,
962 Start_MenuW,
963 Start_MenuW
965 { /* 0x0c - CSIDL_MYDOCUMENTS */
966 &GUID_NULL,
967 CSIDL_Type_Disallowed, /* matches WinXP--can't get its path */
968 NULL,
969 NULL
971 { /* 0x0d - CSIDL_MYMUSIC */
972 &FOLDERID_Music,
973 CSIDL_Type_User,
974 My_MusicW,
975 MAKEINTRESOURCEW(IDS_MYMUSIC)
977 { /* 0x0e - CSIDL_MYVIDEO */
978 &FOLDERID_Videos,
979 CSIDL_Type_User,
980 My_VideosW,
981 MAKEINTRESOURCEW(IDS_MYVIDEOS)
983 { /* 0x0f - unassigned */
984 &GUID_NULL,
985 CSIDL_Type_Disallowed,
986 NULL,
987 NULL,
989 { /* 0x10 - CSIDL_DESKTOPDIRECTORY */
990 &FOLDERID_Desktop,
991 CSIDL_Type_User,
992 DesktopW,
993 MAKEINTRESOURCEW(IDS_DESKTOPDIRECTORY)
995 { /* 0x11 - CSIDL_DRIVES */
996 &FOLDERID_ComputerFolder,
997 CSIDL_Type_Disallowed,
998 NULL,
999 NULL,
1001 { /* 0x12 - CSIDL_NETWORK */
1002 &FOLDERID_NetworkFolder,
1003 CSIDL_Type_Disallowed,
1004 NULL,
1005 NULL,
1007 { /* 0x13 - CSIDL_NETHOOD */
1008 &FOLDERID_NetHood,
1009 CSIDL_Type_User,
1010 NetHoodW,
1011 NetHoodW
1013 { /* 0x14 - CSIDL_FONTS */
1014 &FOLDERID_Fonts,
1015 CSIDL_Type_WindowsPath,
1016 FontsW,
1017 FontsW
1019 { /* 0x15 - CSIDL_TEMPLATES */
1020 &FOLDERID_Templates,
1021 CSIDL_Type_User,
1022 TemplatesW,
1023 TemplatesW
1025 { /* 0x16 - CSIDL_COMMON_STARTMENU */
1026 &FOLDERID_CommonStartMenu,
1027 CSIDL_Type_AllUsers,
1028 Common_Start_MenuW,
1029 Start_MenuW
1031 { /* 0x17 - CSIDL_COMMON_PROGRAMS */
1032 &FOLDERID_CommonPrograms,
1033 CSIDL_Type_AllUsers,
1034 Common_ProgramsW,
1035 Start_Menu_ProgramsW
1037 { /* 0x18 - CSIDL_COMMON_STARTUP */
1038 &FOLDERID_CommonStartup,
1039 CSIDL_Type_AllUsers,
1040 Common_StartUpW,
1041 Start_Menu_StartupW
1043 { /* 0x19 - CSIDL_COMMON_DESKTOPDIRECTORY */
1044 &FOLDERID_PublicDesktop,
1045 CSIDL_Type_AllUsers,
1046 Common_DesktopW,
1047 MAKEINTRESOURCEW(IDS_DESKTOPDIRECTORY)
1049 { /* 0x1a - CSIDL_APPDATA */
1050 &FOLDERID_RoamingAppData,
1051 CSIDL_Type_User,
1052 AppDataW,
1053 Application_DataW
1055 { /* 0x1b - CSIDL_PRINTHOOD */
1056 &FOLDERID_PrintHood,
1057 CSIDL_Type_User,
1058 PrintHoodW,
1059 PrintHoodW
1061 { /* 0x1c - CSIDL_LOCAL_APPDATA */
1062 &FOLDERID_LocalAppData,
1063 CSIDL_Type_User,
1064 Local_AppDataW,
1065 Local_Settings_Application_DataW
1067 { /* 0x1d - CSIDL_ALTSTARTUP */
1068 &GUID_NULL,
1069 CSIDL_Type_NonExistent,
1070 NULL,
1071 NULL
1073 { /* 0x1e - CSIDL_COMMON_ALTSTARTUP */
1074 &GUID_NULL,
1075 CSIDL_Type_NonExistent,
1076 NULL,
1077 NULL
1079 { /* 0x1f - CSIDL_COMMON_FAVORITES */
1080 &FOLDERID_Favorites,
1081 CSIDL_Type_AllUsers,
1082 Common_FavoritesW,
1083 FavoritesW
1085 { /* 0x20 - CSIDL_INTERNET_CACHE */
1086 &FOLDERID_InternetCache,
1087 CSIDL_Type_User,
1088 CacheW,
1089 Local_Settings_Temporary_Internet_FilesW
1091 { /* 0x21 - CSIDL_COOKIES */
1092 &FOLDERID_Cookies,
1093 CSIDL_Type_User,
1094 CookiesW,
1095 CookiesW
1097 { /* 0x22 - CSIDL_HISTORY */
1098 &FOLDERID_History,
1099 CSIDL_Type_User,
1100 HistoryW,
1101 Local_Settings_HistoryW
1103 { /* 0x23 - CSIDL_COMMON_APPDATA */
1104 &FOLDERID_ProgramData,
1105 CSIDL_Type_AllUsers,
1106 Common_AppDataW,
1107 Application_DataW
1109 { /* 0x24 - CSIDL_WINDOWS */
1110 &FOLDERID_Windows,
1111 CSIDL_Type_WindowsPath,
1112 NULL,
1113 NULL
1115 { /* 0x25 - CSIDL_SYSTEM */
1116 &FOLDERID_System,
1117 CSIDL_Type_SystemPath,
1118 NULL,
1119 NULL
1121 { /* 0x26 - CSIDL_PROGRAM_FILES */
1122 &FOLDERID_ProgramFiles,
1123 CSIDL_Type_CurrVer,
1124 ProgramFilesDirW,
1125 Program_FilesW
1127 { /* 0x27 - CSIDL_MYPICTURES */
1128 &FOLDERID_Pictures,
1129 CSIDL_Type_User,
1130 My_PicturesW,
1131 MAKEINTRESOURCEW(IDS_MYPICTURES)
1133 { /* 0x28 - CSIDL_PROFILE */
1134 &FOLDERID_Profile,
1135 CSIDL_Type_User,
1136 NULL,
1137 NULL
1139 { /* 0x29 - CSIDL_SYSTEMX86 */
1140 &FOLDERID_SystemX86,
1141 CSIDL_Type_SystemX86Path,
1142 NULL,
1143 NULL
1145 { /* 0x2a - CSIDL_PROGRAM_FILESX86 */
1146 &FOLDERID_ProgramFilesX86,
1147 CSIDL_Type_CurrVer,
1148 ProgramFilesDirX86W,
1149 Program_Files_x86W
1151 { /* 0x2b - CSIDL_PROGRAM_FILES_COMMON */
1152 &FOLDERID_ProgramFilesCommon,
1153 CSIDL_Type_CurrVer,
1154 CommonFilesDirW,
1155 Program_Files_Common_FilesW
1157 { /* 0x2c - CSIDL_PROGRAM_FILES_COMMONX86 */
1158 &FOLDERID_ProgramFilesCommonX86,
1159 CSIDL_Type_CurrVer,
1160 CommonFilesDirX86W,
1161 Program_Files_x86_Common_FilesW
1163 { /* 0x2d - CSIDL_COMMON_TEMPLATES */
1164 &FOLDERID_CommonTemplates,
1165 CSIDL_Type_AllUsers,
1166 Common_TemplatesW,
1167 TemplatesW
1169 { /* 0x2e - CSIDL_COMMON_DOCUMENTS */
1170 &FOLDERID_PublicDocuments,
1171 CSIDL_Type_AllUsers,
1172 Common_DocumentsW,
1173 DocumentsW
1175 { /* 0x2f - CSIDL_COMMON_ADMINTOOLS */
1176 &FOLDERID_CommonAdminTools,
1177 CSIDL_Type_AllUsers,
1178 Common_Administrative_ToolsW,
1179 Start_Menu_Admin_ToolsW
1181 { /* 0x30 - CSIDL_ADMINTOOLS */
1182 &FOLDERID_AdminTools,
1183 CSIDL_Type_User,
1184 Administrative_ToolsW,
1185 Start_Menu_Admin_ToolsW
1187 { /* 0x31 - CSIDL_CONNECTIONS */
1188 &FOLDERID_ConnectionsFolder,
1189 CSIDL_Type_Disallowed,
1190 NULL,
1191 NULL
1193 { /* 0x32 - unassigned */
1194 &GUID_NULL,
1195 CSIDL_Type_Disallowed,
1196 NULL,
1197 NULL
1199 { /* 0x33 - unassigned */
1200 &GUID_NULL,
1201 CSIDL_Type_Disallowed,
1202 NULL,
1203 NULL
1205 { /* 0x34 - unassigned */
1206 &GUID_NULL,
1207 CSIDL_Type_Disallowed,
1208 NULL,
1209 NULL
1211 { /* 0x35 - CSIDL_COMMON_MUSIC */
1212 &FOLDERID_PublicMusic,
1213 CSIDL_Type_AllUsers,
1214 CommonMusicW,
1215 MusicW
1217 { /* 0x36 - CSIDL_COMMON_PICTURES */
1218 &FOLDERID_PublicPictures,
1219 CSIDL_Type_AllUsers,
1220 CommonPicturesW,
1221 PicturesW
1223 { /* 0x37 - CSIDL_COMMON_VIDEO */
1224 &FOLDERID_PublicVideos,
1225 CSIDL_Type_AllUsers,
1226 CommonVideoW,
1227 VideosW
1229 { /* 0x38 - CSIDL_RESOURCES */
1230 &FOLDERID_ResourceDir,
1231 CSIDL_Type_WindowsPath,
1232 NULL,
1233 ResourcesW
1235 { /* 0x39 - CSIDL_RESOURCES_LOCALIZED */
1236 &FOLDERID_LocalizedResourcesDir,
1237 CSIDL_Type_NonExistent,
1238 NULL,
1239 NULL
1241 { /* 0x3a - CSIDL_COMMON_OEM_LINKS */
1242 &FOLDERID_CommonOEMLinks,
1243 CSIDL_Type_AllUsers,
1244 NULL,
1245 OEM_LinksW
1247 { /* 0x3b - CSIDL_CDBURN_AREA */
1248 &FOLDERID_CDBurning,
1249 CSIDL_Type_User,
1250 CD_BurningW,
1251 Local_Settings_CD_BurningW
1253 { /* 0x3c unassigned */
1254 &GUID_NULL,
1255 CSIDL_Type_Disallowed,
1256 NULL,
1257 NULL
1259 { /* 0x3d - CSIDL_COMPUTERSNEARME */
1260 &GUID_NULL,
1261 CSIDL_Type_Disallowed, /* FIXME */
1262 NULL,
1263 NULL
1265 { /* 0x3e - CSIDL_PROFILES */
1266 &GUID_NULL,
1267 CSIDL_Type_Disallowed, /* oddly, this matches WinXP */
1268 NULL,
1269 NULL
1271 { /* 0x3f */
1272 &FOLDERID_AddNewPrograms,
1273 CSIDL_Type_Disallowed,
1274 NULL,
1275 NULL
1277 { /* 0x40 */
1278 &FOLDERID_AppUpdates,
1279 CSIDL_Type_Disallowed,
1280 NULL,
1281 NULL
1283 { /* 0x41 */
1284 &FOLDERID_ChangeRemovePrograms,
1285 CSIDL_Type_Disallowed,
1286 NULL,
1287 NULL
1289 { /* 0x42 */
1290 &FOLDERID_ConflictFolder,
1291 CSIDL_Type_Disallowed,
1292 NULL,
1293 NULL
1295 { /* 0x43 - CSIDL_CONTACTS */
1296 &FOLDERID_Contacts,
1297 CSIDL_Type_User,
1298 NULL,
1299 ContactsW
1301 { /* 0x44 */
1302 &FOLDERID_DeviceMetadataStore,
1303 CSIDL_Type_Disallowed, /* FIXME */
1304 NULL,
1305 NULL
1307 { /* 0x45 */
1308 &GUID_NULL,
1309 CSIDL_Type_User,
1310 NULL,
1311 DocumentsW
1313 { /* 0x46 */
1314 &FOLDERID_DocumentsLibrary,
1315 CSIDL_Type_Disallowed, /* FIXME */
1316 NULL,
1317 NULL
1319 { /* 0x47 - CSIDL_DOWNLOADS */
1320 &FOLDERID_Downloads,
1321 CSIDL_Type_User,
1322 NULL,
1323 DownloadsW
1325 { /* 0x48 */
1326 &FOLDERID_Games,
1327 CSIDL_Type_Disallowed,
1328 NULL,
1329 NULL
1331 { /* 0x49 */
1332 &FOLDERID_GameTasks,
1333 CSIDL_Type_Disallowed, /* FIXME */
1334 NULL,
1335 NULL
1337 { /* 0x4a */
1338 &FOLDERID_HomeGroup,
1339 CSIDL_Type_Disallowed,
1340 NULL,
1341 NULL
1343 { /* 0x4b */
1344 &FOLDERID_ImplicitAppShortcuts,
1345 CSIDL_Type_Disallowed, /* FIXME */
1346 NULL,
1347 NULL
1349 { /* 0x4c */
1350 &FOLDERID_Libraries,
1351 CSIDL_Type_Disallowed, /* FIXME */
1352 NULL,
1353 NULL
1355 { /* 0x4d - CSIDL_LINKS */
1356 &FOLDERID_Links,
1357 CSIDL_Type_User,
1358 NULL,
1359 LinksW
1361 { /* 0x4e - CSIDL_APPDATA_LOCALLOW */
1362 &FOLDERID_LocalAppDataLow,
1363 CSIDL_Type_User,
1364 NULL,
1365 AppData_LocalLowW
1367 { /* 0x4f */
1368 &FOLDERID_MusicLibrary,
1369 CSIDL_Type_Disallowed, /* FIXME */
1370 NULL,
1371 NULL
1373 { /* 0x50 */
1374 &FOLDERID_OriginalImages,
1375 CSIDL_Type_Disallowed, /* FIXME */
1376 NULL,
1377 NULL
1379 { /* 0x51 */
1380 &FOLDERID_PhotoAlbums,
1381 CSIDL_Type_User,
1382 NULL,
1383 Pictures_Slide_ShowsW
1385 { /* 0x52 */
1386 &FOLDERID_PicturesLibrary,
1387 CSIDL_Type_Disallowed, /* FIXME */
1388 NULL,
1389 NULL
1391 { /* 0x53 */
1392 &FOLDERID_Playlists,
1393 CSIDL_Type_User,
1394 NULL,
1395 Music_PlaylistsW
1397 { /* 0x54 */
1398 &FOLDERID_ProgramFilesX64,
1399 CSIDL_Type_NonExistent,
1400 NULL,
1401 NULL
1403 { /* 0x55 */
1404 &FOLDERID_ProgramFilesCommonX64,
1405 CSIDL_Type_NonExistent,
1406 NULL,
1407 NULL
1409 { /* 0x56 */
1410 &FOLDERID_Public,
1411 CSIDL_Type_CurrVer, /* FIXME */
1412 NULL,
1413 UsersPublicW
1415 { /* 0x57 */
1416 &FOLDERID_PublicDownloads,
1417 CSIDL_Type_AllUsers,
1418 NULL,
1419 DownloadsW
1421 { /* 0x58 */
1422 &FOLDERID_PublicGameTasks,
1423 CSIDL_Type_AllUsers,
1424 NULL,
1425 Microsoft_Windows_GameExplorerW
1427 { /* 0x59 */
1428 &FOLDERID_PublicLibraries,
1429 CSIDL_Type_AllUsers,
1430 NULL,
1431 Microsoft_Windows_LibrariesW
1433 { /* 0x5a */
1434 &FOLDERID_PublicRingtones,
1435 CSIDL_Type_AllUsers,
1436 NULL,
1437 Microsoft_Windows_RingtonesW
1439 { /* 0x5b */
1440 &FOLDERID_QuickLaunch,
1441 CSIDL_Type_Disallowed, /* FIXME */
1442 NULL,
1443 NULL
1445 { /* 0x5c */
1446 &FOLDERID_RecordedTVLibrary,
1447 CSIDL_Type_Disallowed, /* FIXME */
1448 NULL,
1449 NULL
1451 { /* 0x5d */
1452 &FOLDERID_Ringtones,
1453 CSIDL_Type_Disallowed, /* FIXME */
1454 NULL,
1455 NULL
1457 { /* 0x5e */
1458 &FOLDERID_SampleMusic,
1459 CSIDL_Type_AllUsers,
1460 NULL,
1461 Music_Sample_MusicW
1463 { /* 0x5f */
1464 &FOLDERID_SamplePictures,
1465 CSIDL_Type_AllUsers,
1466 NULL,
1467 Pictures_Sample_PicturesW
1469 { /* 0x60 */
1470 &FOLDERID_SamplePlaylists,
1471 CSIDL_Type_AllUsers,
1472 NULL,
1473 Music_Sample_PlaylistsW
1475 { /* 0x61 */
1476 &FOLDERID_SampleVideos,
1477 CSIDL_Type_AllUsers,
1478 NULL,
1479 Videos_Sample_VideosW
1481 { /* 0x62 - CSIDL_SAVED_GAMES */
1482 &FOLDERID_SavedGames,
1483 CSIDL_Type_User,
1484 NULL,
1485 Saved_GamesW
1487 { /* 0x63 - CSIDL_SEARCHES */
1488 &FOLDERID_SavedSearches,
1489 CSIDL_Type_User,
1490 NULL,
1491 SearchesW
1493 { /* 0x64 */
1494 &FOLDERID_SEARCH_CSC,
1495 CSIDL_Type_Disallowed,
1496 NULL,
1497 NULL
1499 { /* 0x65 */
1500 &FOLDERID_SEARCH_MAPI,
1501 CSIDL_Type_Disallowed,
1502 NULL,
1503 NULL
1505 { /* 0x66 */
1506 &FOLDERID_SearchHome,
1507 CSIDL_Type_Disallowed,
1508 NULL,
1509 NULL
1511 { /* 0x67 */
1512 &FOLDERID_SidebarDefaultParts,
1513 CSIDL_Type_Disallowed, /* FIXME */
1514 NULL,
1515 NULL
1517 { /* 0x68 */
1518 &FOLDERID_SidebarParts,
1519 CSIDL_Type_Disallowed, /* FIXME */
1520 NULL,
1521 NULL
1523 { /* 0x69 */
1524 &FOLDERID_SyncManagerFolder,
1525 CSIDL_Type_Disallowed,
1526 NULL,
1527 NULL
1529 { /* 0x6a */
1530 &FOLDERID_SyncResultsFolder,
1531 CSIDL_Type_Disallowed,
1532 NULL,
1533 NULL
1535 { /* 0x6b */
1536 &FOLDERID_SyncSetupFolder,
1537 CSIDL_Type_Disallowed,
1538 NULL,
1539 NULL
1541 { /* 0x6c */
1542 &FOLDERID_UserPinned,
1543 CSIDL_Type_Disallowed, /* FIXME */
1544 NULL,
1545 NULL
1547 { /* 0x6d */
1548 &FOLDERID_UserProfiles,
1549 CSIDL_Type_CurrVer,
1550 UsersW,
1551 UsersW
1553 { /* 0x6e */
1554 &FOLDERID_UserProgramFiles,
1555 CSIDL_Type_Disallowed, /* FIXME */
1556 NULL,
1557 NULL
1559 { /* 0x6f */
1560 &FOLDERID_UserProgramFilesCommon,
1561 CSIDL_Type_Disallowed, /* FIXME */
1562 NULL,
1563 NULL
1565 { /* 0x70 */
1566 &FOLDERID_UsersFiles,
1567 CSIDL_Type_Disallowed,
1568 NULL,
1569 NULL
1571 { /* 0x71 */
1572 &FOLDERID_UsersLibraries,
1573 CSIDL_Type_Disallowed,
1574 NULL,
1575 NULL
1577 { /* 0x72 */
1578 &FOLDERID_VideosLibrary,
1579 CSIDL_Type_Disallowed, /* FIXME */
1580 NULL,
1581 NULL
1585 static HRESULT _SHExpandEnvironmentStrings(LPCWSTR szSrc, LPWSTR szDest);
1587 /* Gets the value named value from the registry key
1588 * rootKey\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
1589 * (or from rootKey\userPrefix\... if userPrefix is not NULL) into path, which
1590 * is assumed to be MAX_PATH WCHARs in length.
1591 * If it exists, expands the value and writes the expanded value to
1592 * rootKey\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders
1593 * Returns successful error code if the value was retrieved from the registry,
1594 * and a failure otherwise.
1596 static HRESULT _SHGetUserShellFolderPath(HKEY rootKey, LPCWSTR userPrefix,
1597 LPCWSTR value, LPWSTR path)
1599 HRESULT hr;
1600 WCHAR shellFolderPath[MAX_PATH], userShellFolderPath[MAX_PATH];
1601 LPCWSTR pShellFolderPath, pUserShellFolderPath;
1602 DWORD dwType, dwPathLen = MAX_PATH;
1603 HKEY userShellFolderKey, shellFolderKey;
1605 TRACE("%p,%s,%s,%p\n",rootKey, debugstr_w(userPrefix), debugstr_w(value),
1606 path);
1608 if (userPrefix)
1610 strcpyW(shellFolderPath, userPrefix);
1611 PathAddBackslashW(shellFolderPath);
1612 strcatW(shellFolderPath, szSHFolders);
1613 pShellFolderPath = shellFolderPath;
1614 strcpyW(userShellFolderPath, userPrefix);
1615 PathAddBackslashW(userShellFolderPath);
1616 strcatW(userShellFolderPath, szSHUserFolders);
1617 pUserShellFolderPath = userShellFolderPath;
1619 else
1621 pUserShellFolderPath = szSHUserFolders;
1622 pShellFolderPath = szSHFolders;
1625 if (RegCreateKeyW(rootKey, pShellFolderPath, &shellFolderKey))
1627 TRACE("Failed to create %s\n", debugstr_w(pShellFolderPath));
1628 return E_FAIL;
1630 if (RegCreateKeyW(rootKey, pUserShellFolderPath, &userShellFolderKey))
1632 TRACE("Failed to create %s\n",
1633 debugstr_w(pUserShellFolderPath));
1634 RegCloseKey(shellFolderKey);
1635 return E_FAIL;
1638 if (!RegQueryValueExW(userShellFolderKey, value, NULL, &dwType,
1639 (LPBYTE)path, &dwPathLen) && (dwType == REG_EXPAND_SZ || dwType == REG_SZ))
1641 LONG ret;
1643 path[dwPathLen / sizeof(WCHAR)] = '\0';
1644 if (dwType == REG_EXPAND_SZ && path[0] == '%')
1646 WCHAR szTemp[MAX_PATH];
1648 _SHExpandEnvironmentStrings(path, szTemp);
1649 lstrcpynW(path, szTemp, MAX_PATH);
1651 ret = RegSetValueExW(shellFolderKey, value, 0, REG_SZ, (LPBYTE)path,
1652 (strlenW(path) + 1) * sizeof(WCHAR));
1653 if (ret != ERROR_SUCCESS)
1654 hr = HRESULT_FROM_WIN32(ret);
1655 else
1656 hr = S_OK;
1658 else
1659 hr = E_FAIL;
1660 RegCloseKey(shellFolderKey);
1661 RegCloseKey(userShellFolderKey);
1662 TRACE("returning 0x%08x\n", hr);
1663 return hr;
1666 /* Gets a 'semi-expanded' default value of the CSIDL with index folder into
1667 * pszPath, based on the entries in CSIDL_Data. By semi-expanded, I mean:
1668 * - The entry's szDefaultPath may be either a string value or an integer
1669 * resource identifier. In the latter case, the string value of the resource
1670 * is written.
1671 * - Depending on the entry's type, the path may begin with an (unexpanded)
1672 * environment variable name. The caller is responsible for expanding
1673 * environment strings if so desired.
1674 * The types that are prepended with environment variables are:
1675 * CSIDL_Type_User: %USERPROFILE%
1676 * CSIDL_Type_AllUsers: %ALLUSERSPROFILE%
1677 * CSIDL_Type_CurrVer: %SystemDrive%
1678 * (Others might make sense too, but as yet are unneeded.)
1680 static HRESULT _SHGetDefaultValue(BYTE folder, LPWSTR pszPath)
1682 HRESULT hr;
1683 WCHAR resourcePath[MAX_PATH];
1684 LPCWSTR pDefaultPath = NULL;
1686 TRACE("0x%02x,%p\n", folder, pszPath);
1688 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1689 return E_INVALIDARG;
1690 if (!pszPath)
1691 return E_INVALIDARG;
1693 if (!is_win64)
1695 BOOL is_wow64;
1697 switch (folder)
1699 case CSIDL_PROGRAM_FILES:
1700 case CSIDL_PROGRAM_FILESX86:
1701 IsWow64Process( GetCurrentProcess(), &is_wow64 );
1702 folder = is_wow64 ? CSIDL_PROGRAM_FILESX86 : CSIDL_PROGRAM_FILES;
1703 break;
1704 case CSIDL_PROGRAM_FILES_COMMON:
1705 case CSIDL_PROGRAM_FILES_COMMONX86:
1706 IsWow64Process( GetCurrentProcess(), &is_wow64 );
1707 folder = is_wow64 ? CSIDL_PROGRAM_FILES_COMMONX86 : CSIDL_PROGRAM_FILES_COMMON;
1708 break;
1712 if (CSIDL_Data[folder].szDefaultPath &&
1713 IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath))
1715 if (LoadStringW(shell32_hInstance,
1716 LOWORD(CSIDL_Data[folder].szDefaultPath), resourcePath, MAX_PATH))
1718 hr = S_OK;
1719 pDefaultPath = resourcePath;
1721 else
1723 FIXME("(%d,%s), LoadString failed, missing translation?\n", folder,
1724 debugstr_w(pszPath));
1725 hr = E_FAIL;
1728 else
1730 hr = S_OK;
1731 pDefaultPath = CSIDL_Data[folder].szDefaultPath;
1733 if (SUCCEEDED(hr))
1735 switch (CSIDL_Data[folder].type)
1737 case CSIDL_Type_User:
1738 strcpyW(pszPath, UserProfileW);
1739 break;
1740 case CSIDL_Type_AllUsers:
1741 strcpyW(pszPath, AllUsersProfileW);
1742 break;
1743 case CSIDL_Type_CurrVer:
1744 strcpyW(pszPath, SystemDriveW);
1745 break;
1746 default:
1747 ; /* no corresponding env. var, do nothing */
1749 if (pDefaultPath)
1751 PathAddBackslashW(pszPath);
1752 strcatW(pszPath, pDefaultPath);
1755 TRACE("returning 0x%08x\n", hr);
1756 return hr;
1759 /* Gets the (unexpanded) value of the folder with index folder into pszPath.
1760 * The folder's type is assumed to be CSIDL_Type_CurrVer. Its default value
1761 * can be overridden in the HKLM\\szCurrentVersion key.
1762 * If dwFlags has SHGFP_TYPE_DEFAULT set or if the value isn't overridden in
1763 * the registry, uses _SHGetDefaultValue to get the value.
1765 static HRESULT _SHGetCurrentVersionPath(DWORD dwFlags, BYTE folder,
1766 LPWSTR pszPath)
1768 HRESULT hr;
1770 TRACE("0x%08x,0x%02x,%p\n", dwFlags, folder, pszPath);
1772 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1773 return E_INVALIDARG;
1774 if (CSIDL_Data[folder].type != CSIDL_Type_CurrVer)
1775 return E_INVALIDARG;
1776 if (!pszPath)
1777 return E_INVALIDARG;
1779 if (dwFlags & SHGFP_TYPE_DEFAULT)
1780 hr = _SHGetDefaultValue(folder, pszPath);
1781 else
1783 HKEY hKey;
1785 if (RegCreateKeyW(HKEY_LOCAL_MACHINE, szCurrentVersion, &hKey))
1786 hr = E_FAIL;
1787 else
1789 DWORD dwType, dwPathLen = MAX_PATH * sizeof(WCHAR);
1791 if (RegQueryValueExW(hKey, CSIDL_Data[folder].szValueName, NULL,
1792 &dwType, (LPBYTE)pszPath, &dwPathLen) ||
1793 (dwType != REG_SZ && dwType != REG_EXPAND_SZ))
1795 hr = _SHGetDefaultValue(folder, pszPath);
1796 dwType = REG_EXPAND_SZ;
1797 switch (folder)
1799 case CSIDL_PROGRAM_FILESX86:
1800 case CSIDL_PROGRAM_FILES_COMMONX86:
1801 /* these two should never be set on 32-bit setups */
1802 if (!is_win64)
1804 BOOL is_wow64;
1805 IsWow64Process( GetCurrentProcess(), &is_wow64 );
1806 if (!is_wow64) break;
1808 /* fall through */
1809 default:
1810 RegSetValueExW(hKey, CSIDL_Data[folder].szValueName, 0, dwType,
1811 (LPBYTE)pszPath, (strlenW(pszPath)+1)*sizeof(WCHAR));
1814 else
1816 pszPath[dwPathLen / sizeof(WCHAR)] = '\0';
1817 hr = S_OK;
1819 RegCloseKey(hKey);
1822 TRACE("returning 0x%08x (output path is %s)\n", hr, debugstr_w(pszPath));
1823 return hr;
1826 static LPWSTR _GetUserSidStringFromToken(HANDLE Token)
1828 char InfoBuffer[64];
1829 PTOKEN_USER UserInfo;
1830 DWORD InfoSize;
1831 LPWSTR SidStr;
1833 UserInfo = (PTOKEN_USER) InfoBuffer;
1834 if (! GetTokenInformation(Token, TokenUser, InfoBuffer, sizeof(InfoBuffer),
1835 &InfoSize))
1837 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
1838 return NULL;
1839 UserInfo = HeapAlloc(GetProcessHeap(), 0, InfoSize);
1840 if (UserInfo == NULL)
1841 return NULL;
1842 if (! GetTokenInformation(Token, TokenUser, UserInfo, InfoSize,
1843 &InfoSize))
1845 HeapFree(GetProcessHeap(), 0, UserInfo);
1846 return NULL;
1850 if (! ConvertSidToStringSidW(UserInfo->User.Sid, &SidStr))
1851 SidStr = NULL;
1853 if (UserInfo != (PTOKEN_USER) InfoBuffer)
1854 HeapFree(GetProcessHeap(), 0, UserInfo);
1856 return SidStr;
1859 /* Gets the user's path (unexpanded) for the CSIDL with index folder:
1860 * If SHGFP_TYPE_DEFAULT is set, calls _SHGetDefaultValue for it. Otherwise
1861 * calls _SHGetUserShellFolderPath for it. Where it looks depends on hToken:
1862 * - if hToken is -1, looks in HKEY_USERS\.Default
1863 * - otherwise looks first in HKEY_CURRENT_USER, followed by HKEY_LOCAL_MACHINE
1864 * if HKEY_CURRENT_USER doesn't contain any entries. If both fail, finally
1865 * calls _SHGetDefaultValue for it.
1867 static HRESULT _SHGetUserProfilePath(HANDLE hToken, DWORD dwFlags, BYTE folder,
1868 LPWSTR pszPath)
1870 const WCHAR *szValueName;
1871 WCHAR buffer[40];
1872 HRESULT hr;
1874 TRACE("%p,0x%08x,0x%02x,%p\n", hToken, dwFlags, folder, pszPath);
1876 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1877 return E_INVALIDARG;
1878 if (CSIDL_Data[folder].type != CSIDL_Type_User)
1879 return E_INVALIDARG;
1880 if (!pszPath)
1881 return E_INVALIDARG;
1883 if (dwFlags & SHGFP_TYPE_DEFAULT)
1885 if (hToken != NULL && hToken != (HANDLE)-1)
1887 FIXME("unsupported for user other than current or default\n");
1888 return E_FAIL;
1890 hr = _SHGetDefaultValue(folder, pszPath);
1892 else
1894 LPCWSTR userPrefix = NULL;
1895 HKEY hRootKey;
1897 if (hToken == (HANDLE)-1)
1899 hRootKey = HKEY_USERS;
1900 userPrefix = DefaultW;
1902 else if (hToken == NULL)
1903 hRootKey = HKEY_CURRENT_USER;
1904 else
1906 hRootKey = HKEY_USERS;
1907 userPrefix = _GetUserSidStringFromToken(hToken);
1908 if (userPrefix == NULL)
1910 hr = E_FAIL;
1911 goto error;
1915 /* For CSIDL_Type_User we also use the GUID if no szValueName is provided */
1916 szValueName = CSIDL_Data[folder].szValueName;
1917 if (!szValueName)
1919 StringFromGUID2( CSIDL_Data[folder].id, buffer, 39 );
1920 szValueName = &buffer[0];
1923 hr = _SHGetUserShellFolderPath(hRootKey, userPrefix, szValueName, pszPath);
1924 if (FAILED(hr) && hRootKey != HKEY_LOCAL_MACHINE)
1925 hr = _SHGetUserShellFolderPath(HKEY_LOCAL_MACHINE, NULL, szValueName, pszPath);
1926 if (FAILED(hr))
1927 hr = _SHGetDefaultValue(folder, pszPath);
1928 if (userPrefix != NULL && userPrefix != DefaultW)
1929 LocalFree((HLOCAL) userPrefix);
1931 error:
1932 TRACE("returning 0x%08x (output path is %s)\n", hr, debugstr_w(pszPath));
1933 return hr;
1936 /* Gets the (unexpanded) path for the CSIDL with index folder. If dwFlags has
1937 * SHGFP_TYPE_DEFAULT set, calls _SHGetDefaultValue. Otherwise calls
1938 * _SHGetUserShellFolderPath for it, looking only in HKEY_LOCAL_MACHINE.
1939 * If this fails, falls back to _SHGetDefaultValue.
1941 static HRESULT _SHGetAllUsersProfilePath(DWORD dwFlags, BYTE folder,
1942 LPWSTR pszPath)
1944 HRESULT hr;
1946 TRACE("0x%08x,0x%02x,%p\n", dwFlags, folder, pszPath);
1948 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1949 return E_INVALIDARG;
1950 if (CSIDL_Data[folder].type != CSIDL_Type_AllUsers)
1951 return E_INVALIDARG;
1952 if (!pszPath)
1953 return E_INVALIDARG;
1955 if (dwFlags & SHGFP_TYPE_DEFAULT)
1956 hr = _SHGetDefaultValue(folder, pszPath);
1957 else
1959 hr = _SHGetUserShellFolderPath(HKEY_LOCAL_MACHINE, NULL,
1960 CSIDL_Data[folder].szValueName, pszPath);
1961 if (FAILED(hr))
1962 hr = _SHGetDefaultValue(folder, pszPath);
1964 TRACE("returning 0x%08x (output path is %s)\n", hr, debugstr_w(pszPath));
1965 return hr;
1968 static HRESULT _SHOpenProfilesKey(PHKEY pKey)
1970 LONG lRet;
1971 DWORD disp;
1973 lRet = RegCreateKeyExW(HKEY_LOCAL_MACHINE, ProfileListW, 0, NULL, 0,
1974 KEY_ALL_ACCESS, NULL, pKey, &disp);
1975 return HRESULT_FROM_WIN32(lRet);
1978 /* Reads the value named szValueName from the key profilesKey (assumed to be
1979 * opened by _SHOpenProfilesKey) into szValue, which is assumed to be MAX_PATH
1980 * WCHARs in length. If it doesn't exist, returns szDefault (and saves
1981 * szDefault to the registry).
1983 static HRESULT _SHGetProfilesValue(HKEY profilesKey, LPCWSTR szValueName,
1984 LPWSTR szValue, LPCWSTR szDefault)
1986 HRESULT hr;
1987 DWORD type, dwPathLen = MAX_PATH * sizeof(WCHAR);
1988 LONG lRet;
1990 TRACE("%p,%s,%p,%s\n", profilesKey, debugstr_w(szValueName), szValue,
1991 debugstr_w(szDefault));
1992 lRet = RegQueryValueExW(profilesKey, szValueName, NULL, &type,
1993 (LPBYTE)szValue, &dwPathLen);
1994 if (!lRet && (type == REG_SZ || type == REG_EXPAND_SZ) && dwPathLen
1995 && *szValue)
1997 dwPathLen /= sizeof(WCHAR);
1998 szValue[dwPathLen] = '\0';
1999 hr = S_OK;
2001 else
2003 /* Missing or invalid value, set a default */
2004 lstrcpynW(szValue, szDefault, MAX_PATH);
2005 TRACE("Setting missing value %s to %s\n", debugstr_w(szValueName),
2006 debugstr_w(szValue));
2007 lRet = RegSetValueExW(profilesKey, szValueName, 0, REG_EXPAND_SZ,
2008 (LPBYTE)szValue,
2009 (strlenW(szValue) + 1) * sizeof(WCHAR));
2010 if (lRet)
2011 hr = HRESULT_FROM_WIN32(lRet);
2012 else
2013 hr = S_OK;
2015 TRACE("returning 0x%08x (output value is %s)\n", hr, debugstr_w(szValue));
2016 return hr;
2019 /* Attempts to expand environment variables from szSrc into szDest, which is
2020 * assumed to be MAX_PATH characters in length. Before referring to the
2021 * environment, handles a few variables directly, because the environment
2022 * variables may not be set when this is called (as during Wine's installation
2023 * when default values are being written to the registry).
2024 * The directly handled environment variables, and their source, are:
2025 * - ALLUSERSPROFILE, USERPROFILE: reads from the registry
2026 * - SystemDrive: uses GetSystemDirectoryW and uses the drive portion of its
2027 * path
2028 * If one of the directly handled environment variables is expanded, only
2029 * expands a single variable, and only in the beginning of szSrc.
2031 static HRESULT _SHExpandEnvironmentStrings(LPCWSTR szSrc, LPWSTR szDest)
2033 HRESULT hr;
2034 WCHAR szTemp[MAX_PATH], szProfilesPrefix[MAX_PATH] = { 0 };
2035 HKEY key = NULL;
2037 TRACE("%s, %p\n", debugstr_w(szSrc), szDest);
2039 if (!szSrc || !szDest) return E_INVALIDARG;
2041 /* short-circuit if there's nothing to expand */
2042 if (szSrc[0] != '%')
2044 strcpyW(szDest, szSrc);
2045 hr = S_OK;
2046 goto end;
2048 /* Get the profile prefix, we'll probably be needing it */
2049 hr = _SHOpenProfilesKey(&key);
2050 if (SUCCEEDED(hr))
2052 WCHAR def_val[MAX_PATH];
2054 /* get the system drive */
2055 GetSystemDirectoryW(def_val, MAX_PATH);
2056 if (def_val[1] == ':') strcpyW( def_val + 3, szDefaultProfileDirW );
2057 else FIXME("non-drive system paths unsupported\n");
2059 hr = _SHGetProfilesValue(key, ProfilesDirectoryW, szProfilesPrefix, def_val );
2062 *szDest = 0;
2063 strcpyW(szTemp, szSrc);
2064 while (SUCCEEDED(hr) && szTemp[0] == '%')
2066 if (!strncmpiW(szTemp, AllUsersProfileW, strlenW(AllUsersProfileW)))
2068 WCHAR szAllUsers[MAX_PATH];
2070 strcpyW(szDest, szProfilesPrefix);
2071 hr = _SHGetProfilesValue(key, AllUsersProfileValueW,
2072 szAllUsers, AllUsersW);
2073 PathAppendW(szDest, szAllUsers);
2074 PathAppendW(szDest, szTemp + strlenW(AllUsersProfileW));
2076 else if (!strncmpiW(szTemp, UserProfileW, strlenW(UserProfileW)))
2078 WCHAR userName[MAX_PATH];
2079 DWORD userLen = MAX_PATH;
2081 strcpyW(szDest, szProfilesPrefix);
2082 GetUserNameW(userName, &userLen);
2083 PathAppendW(szDest, userName);
2084 PathAppendW(szDest, szTemp + strlenW(UserProfileW));
2086 else if (!strncmpiW(szTemp, SystemDriveW, strlenW(SystemDriveW)))
2088 GetSystemDirectoryW(szDest, MAX_PATH);
2089 if (szDest[1] != ':')
2091 FIXME("non-drive system paths unsupported\n");
2092 hr = E_FAIL;
2094 else
2096 strcpyW(szDest + 3, szTemp + strlenW(SystemDriveW) + 1);
2097 hr = S_OK;
2100 else
2102 DWORD ret = ExpandEnvironmentStringsW(szSrc, szDest, MAX_PATH);
2104 if (ret > MAX_PATH)
2105 hr = E_NOT_SUFFICIENT_BUFFER;
2106 else if (ret == 0)
2107 hr = HRESULT_FROM_WIN32(GetLastError());
2108 else
2109 hr = S_OK;
2111 if (SUCCEEDED(hr) && szDest[0] == '%')
2112 strcpyW(szTemp, szDest);
2113 else
2115 /* terminate loop */
2116 szTemp[0] = '\0';
2119 end:
2120 if (key)
2121 RegCloseKey(key);
2122 TRACE("returning 0x%08x (input was %s, output is %s)\n", hr,
2123 debugstr_w(szSrc), debugstr_w(szDest));
2124 return hr;
2127 /*************************************************************************
2128 * SHGetFolderPathW [SHELL32.@]
2130 * Convert nFolder to path.
2132 * RETURNS
2133 * Success: S_OK
2134 * Failure: standard HRESULT error codes.
2136 * NOTES
2137 * Most values can be overridden in either
2138 * HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
2139 * or in the same location in HKLM.
2140 * The "Shell Folders" registry key was used in NT4 and earlier systems.
2141 * Beginning with Windows 2000, the "User Shell Folders" key is used, so
2142 * changes made to it are made to the former key too. This synchronization is
2143 * done on-demand: not until someone requests the value of one of these paths
2144 * (by calling one of the SHGet functions) is the value synchronized.
2145 * Furthermore, the HKCU paths take precedence over the HKLM paths.
2147 HRESULT WINAPI SHGetFolderPathW(
2148 HWND hwndOwner, /* [I] owner window */
2149 int nFolder, /* [I] CSIDL identifying the folder */
2150 HANDLE hToken, /* [I] access token */
2151 DWORD dwFlags, /* [I] which path to return */
2152 LPWSTR pszPath) /* [O] converted path */
2154 HRESULT hr = SHGetFolderPathAndSubDirW(hwndOwner, nFolder, hToken, dwFlags, NULL, pszPath);
2155 if(HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND) == hr)
2156 hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
2157 return hr;
2160 HRESULT WINAPI SHGetFolderPathAndSubDirA(
2161 HWND hwndOwner, /* [I] owner window */
2162 int nFolder, /* [I] CSIDL identifying the folder */
2163 HANDLE hToken, /* [I] access token */
2164 DWORD dwFlags, /* [I] which path to return */
2165 LPCSTR pszSubPath, /* [I] sub directory of the specified folder */
2166 LPSTR pszPath) /* [O] converted path */
2168 int length;
2169 HRESULT hr = S_OK;
2170 LPWSTR pszSubPathW = NULL;
2171 LPWSTR pszPathW = NULL;
2172 TRACE("%08x,%08x,%s\n",nFolder, dwFlags, debugstr_w(pszSubPathW));
2174 if(pszPath) {
2175 pszPathW = HeapAlloc(GetProcessHeap(), 0, MAX_PATH * sizeof(WCHAR));
2176 if(!pszPathW) {
2177 hr = HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
2178 goto cleanup;
2181 TRACE("%08x,%08x,%s\n",nFolder, dwFlags, debugstr_w(pszSubPathW));
2183 /* SHGetFolderPathAndSubDirW does not distinguish if pszSubPath isn't
2184 * set (null), or an empty string.therefore call it without the parameter set
2185 * if pszSubPath is an empty string
2187 if (pszSubPath && pszSubPath[0]) {
2188 length = MultiByteToWideChar(CP_ACP, 0, pszSubPath, -1, NULL, 0);
2189 pszSubPathW = HeapAlloc(GetProcessHeap(), 0, length * sizeof(WCHAR));
2190 if(!pszSubPathW) {
2191 hr = HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
2192 goto cleanup;
2194 MultiByteToWideChar(CP_ACP, 0, pszSubPath, -1, pszSubPathW, length);
2197 hr = SHGetFolderPathAndSubDirW(hwndOwner, nFolder, hToken, dwFlags, pszSubPathW, pszPathW);
2199 if (SUCCEEDED(hr) && pszPath)
2200 WideCharToMultiByte(CP_ACP, 0, pszPathW, -1, pszPath, MAX_PATH, NULL, NULL);
2202 cleanup:
2203 HeapFree(GetProcessHeap(), 0, pszPathW);
2204 HeapFree(GetProcessHeap(), 0, pszSubPathW);
2205 return hr;
2208 /*************************************************************************
2209 * SHGetFolderPathAndSubDirW [SHELL32.@]
2211 HRESULT WINAPI SHGetFolderPathAndSubDirW(
2212 HWND hwndOwner, /* [I] owner window */
2213 int nFolder, /* [I] CSIDL identifying the folder */
2214 HANDLE hToken, /* [I] access token */
2215 DWORD dwFlags, /* [I] which path to return */
2216 LPCWSTR pszSubPath,/* [I] sub directory of the specified folder */
2217 LPWSTR pszPath) /* [O] converted path */
2219 HRESULT hr;
2220 WCHAR szBuildPath[MAX_PATH], szTemp[MAX_PATH];
2221 DWORD folder = nFolder & CSIDL_FOLDER_MASK;
2222 CSIDL_Type type;
2223 int ret;
2225 TRACE("%p,%p,nFolder=0x%04x,%s\n", hwndOwner,pszPath,nFolder,debugstr_w(pszSubPath));
2227 /* Windows always NULL-terminates the resulting path regardless of success
2228 * or failure, so do so first
2230 if (pszPath)
2231 *pszPath = '\0';
2233 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
2234 return E_INVALIDARG;
2235 if ((SHGFP_TYPE_CURRENT != dwFlags) && (SHGFP_TYPE_DEFAULT != dwFlags))
2236 return E_INVALIDARG;
2237 szTemp[0] = 0;
2238 type = CSIDL_Data[folder].type;
2239 switch (type)
2241 case CSIDL_Type_Disallowed:
2242 hr = E_INVALIDARG;
2243 break;
2244 case CSIDL_Type_NonExistent:
2245 hr = S_FALSE;
2246 break;
2247 case CSIDL_Type_WindowsPath:
2248 GetWindowsDirectoryW(szTemp, MAX_PATH);
2249 if (CSIDL_Data[folder].szDefaultPath &&
2250 !IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath) &&
2251 *CSIDL_Data[folder].szDefaultPath)
2253 PathAddBackslashW(szTemp);
2254 strcatW(szTemp, CSIDL_Data[folder].szDefaultPath);
2256 hr = S_OK;
2257 break;
2258 case CSIDL_Type_SystemPath:
2259 GetSystemDirectoryW(szTemp, MAX_PATH);
2260 if (CSIDL_Data[folder].szDefaultPath &&
2261 !IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath) &&
2262 *CSIDL_Data[folder].szDefaultPath)
2264 PathAddBackslashW(szTemp);
2265 strcatW(szTemp, CSIDL_Data[folder].szDefaultPath);
2267 hr = S_OK;
2268 break;
2269 case CSIDL_Type_SystemX86Path:
2270 if (!GetSystemWow64DirectoryW(szTemp, MAX_PATH)) GetSystemDirectoryW(szTemp, MAX_PATH);
2271 if (CSIDL_Data[folder].szDefaultPath &&
2272 !IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath) &&
2273 *CSIDL_Data[folder].szDefaultPath)
2275 PathAddBackslashW(szTemp);
2276 strcatW(szTemp, CSIDL_Data[folder].szDefaultPath);
2278 hr = S_OK;
2279 break;
2280 case CSIDL_Type_CurrVer:
2281 hr = _SHGetCurrentVersionPath(dwFlags, folder, szTemp);
2282 break;
2283 case CSIDL_Type_User:
2284 hr = _SHGetUserProfilePath(hToken, dwFlags, folder, szTemp);
2285 break;
2286 case CSIDL_Type_AllUsers:
2287 hr = _SHGetAllUsersProfilePath(dwFlags, folder, szTemp);
2288 break;
2289 default:
2290 FIXME("bogus type %d, please fix\n", type);
2291 hr = E_INVALIDARG;
2292 break;
2295 /* Expand environment strings if necessary */
2296 if (*szTemp == '%')
2297 hr = _SHExpandEnvironmentStrings(szTemp, szBuildPath);
2298 else
2299 strcpyW(szBuildPath, szTemp);
2301 if (FAILED(hr)) goto end;
2303 if(pszSubPath) {
2304 /* make sure the new path does not exceed the buffer length
2305 * and remember to backslash and terminate it */
2306 if(MAX_PATH < (lstrlenW(szBuildPath) + lstrlenW(pszSubPath) + 2)) {
2307 hr = HRESULT_FROM_WIN32(ERROR_FILENAME_EXCED_RANGE);
2308 goto end;
2310 PathAppendW(szBuildPath, pszSubPath);
2311 PathRemoveBackslashW(szBuildPath);
2313 /* Copy the path if it's available before we might return */
2314 if (SUCCEEDED(hr) && pszPath)
2315 strcpyW(pszPath, szBuildPath);
2317 /* if we don't care about existing directories we are ready */
2318 if(nFolder & CSIDL_FLAG_DONT_VERIFY) goto end;
2320 if (PathFileExistsW(szBuildPath)) goto end;
2322 /* not existing but we are not allowed to create it. The return value
2323 * is verified against shell32 version 6.0.
2325 if (!(nFolder & CSIDL_FLAG_CREATE))
2327 hr = HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND);
2328 goto end;
2331 /* create directory/directories */
2332 ret = SHCreateDirectoryExW(hwndOwner, szBuildPath, NULL);
2333 if (ret && ret != ERROR_ALREADY_EXISTS)
2335 ERR("Failed to create directory %s.\n", debugstr_w(szBuildPath));
2336 hr = E_FAIL;
2337 goto end;
2340 TRACE("Created missing system directory %s\n", debugstr_w(szBuildPath));
2341 end:
2342 TRACE("returning 0x%08x (final path is %s)\n", hr, debugstr_w(szBuildPath));
2343 return hr;
2346 /*************************************************************************
2347 * SHGetFolderPathA [SHELL32.@]
2349 * See SHGetFolderPathW.
2351 HRESULT WINAPI SHGetFolderPathA(
2352 HWND hwndOwner,
2353 int nFolder,
2354 HANDLE hToken,
2355 DWORD dwFlags,
2356 LPSTR pszPath)
2358 WCHAR szTemp[MAX_PATH];
2359 HRESULT hr;
2361 TRACE("%p,%p,nFolder=0x%04x\n",hwndOwner,pszPath,nFolder);
2363 if (pszPath)
2364 *pszPath = '\0';
2365 hr = SHGetFolderPathW(hwndOwner, nFolder, hToken, dwFlags, szTemp);
2366 if (SUCCEEDED(hr) && pszPath)
2367 WideCharToMultiByte(CP_ACP, 0, szTemp, -1, pszPath, MAX_PATH, NULL,
2368 NULL);
2370 return hr;
2373 /* For each folder in folders, if its value has not been set in the registry,
2374 * calls _SHGetUserProfilePath or _SHGetAllUsersProfilePath (depending on the
2375 * folder's type) to get the unexpanded value first.
2376 * Writes the unexpanded value to User Shell Folders, and queries it with
2377 * SHGetFolderPathW to force the creation of the directory if it doesn't
2378 * already exist. SHGetFolderPathW also returns the expanded value, which
2379 * this then writes to Shell Folders.
2381 static HRESULT _SHRegisterFolders(HKEY hRootKey, HANDLE hToken,
2382 LPCWSTR szUserShellFolderPath, LPCWSTR szShellFolderPath, const UINT folders[],
2383 UINT foldersLen)
2385 const WCHAR *szValueName;
2386 WCHAR buffer[40];
2387 UINT i;
2388 WCHAR path[MAX_PATH];
2389 HRESULT hr = S_OK;
2390 HKEY hUserKey = NULL, hKey = NULL;
2391 DWORD dwType, dwPathLen;
2392 LONG ret;
2394 TRACE("%p,%p,%s,%p,%u\n", hRootKey, hToken,
2395 debugstr_w(szUserShellFolderPath), folders, foldersLen);
2397 ret = RegCreateKeyW(hRootKey, szUserShellFolderPath, &hUserKey);
2398 if (ret)
2399 hr = HRESULT_FROM_WIN32(ret);
2400 else
2402 ret = RegCreateKeyW(hRootKey, szShellFolderPath, &hKey);
2403 if (ret)
2404 hr = HRESULT_FROM_WIN32(ret);
2406 for (i = 0; SUCCEEDED(hr) && i < foldersLen; i++)
2408 dwPathLen = MAX_PATH * sizeof(WCHAR);
2410 /* For CSIDL_Type_User we also use the GUID if no szValueName is provided */
2411 szValueName = CSIDL_Data[folders[i]].szValueName;
2412 if (!szValueName && CSIDL_Data[folders[i]].type == CSIDL_Type_User)
2414 StringFromGUID2( CSIDL_Data[folders[i]].id, buffer, 39 );
2415 szValueName = &buffer[0];
2418 if (RegQueryValueExW(hUserKey, szValueName, NULL,
2419 &dwType, (LPBYTE)path, &dwPathLen) || (dwType != REG_SZ &&
2420 dwType != REG_EXPAND_SZ))
2422 *path = '\0';
2423 if (CSIDL_Data[folders[i]].type == CSIDL_Type_User)
2424 _SHGetUserProfilePath(hToken, SHGFP_TYPE_DEFAULT, folders[i],
2425 path);
2426 else if (CSIDL_Data[folders[i]].type == CSIDL_Type_AllUsers)
2427 _SHGetAllUsersProfilePath(SHGFP_TYPE_DEFAULT, folders[i], path);
2428 else if (CSIDL_Data[folders[i]].type == CSIDL_Type_WindowsPath)
2430 GetWindowsDirectoryW(path, MAX_PATH);
2431 if (CSIDL_Data[folders[i]].szDefaultPath &&
2432 !IS_INTRESOURCE(CSIDL_Data[folders[i]].szDefaultPath))
2434 PathAddBackslashW(path);
2435 strcatW(path, CSIDL_Data[folders[i]].szDefaultPath);
2438 else
2439 hr = E_FAIL;
2440 if (*path)
2442 ret = RegSetValueExW(hUserKey, szValueName, 0, REG_EXPAND_SZ,
2443 (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR));
2444 if (ret)
2445 hr = HRESULT_FROM_WIN32(ret);
2446 else
2448 hr = SHGetFolderPathW(NULL, folders[i] | CSIDL_FLAG_CREATE,
2449 hToken, SHGFP_TYPE_DEFAULT, path);
2450 ret = RegSetValueExW(hKey, szValueName, 0, REG_SZ,
2451 (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR));
2452 if (ret)
2453 hr = HRESULT_FROM_WIN32(ret);
2458 if (hUserKey)
2459 RegCloseKey(hUserKey);
2460 if (hKey)
2461 RegCloseKey(hKey);
2463 TRACE("returning 0x%08x\n", hr);
2464 return hr;
2467 static HRESULT _SHRegisterUserShellFolders(BOOL bDefault)
2469 static const UINT folders[] = {
2470 CSIDL_PROGRAMS,
2471 CSIDL_PERSONAL,
2472 CSIDL_FAVORITES,
2473 CSIDL_APPDATA,
2474 CSIDL_STARTUP,
2475 CSIDL_RECENT,
2476 CSIDL_SENDTO,
2477 CSIDL_STARTMENU,
2478 CSIDL_MYMUSIC,
2479 CSIDL_MYVIDEO,
2480 CSIDL_DESKTOPDIRECTORY,
2481 CSIDL_NETHOOD,
2482 CSIDL_TEMPLATES,
2483 CSIDL_PRINTHOOD,
2484 CSIDL_LOCAL_APPDATA,
2485 CSIDL_INTERNET_CACHE,
2486 CSIDL_COOKIES,
2487 CSIDL_HISTORY,
2488 CSIDL_MYPICTURES,
2489 CSIDL_FONTS,
2490 CSIDL_ADMINTOOLS,
2491 CSIDL_CONTACTS,
2492 CSIDL_DOWNLOADS,
2493 CSIDL_LINKS,
2494 CSIDL_APPDATA_LOCALLOW,
2495 CSIDL_SAVED_GAMES,
2496 CSIDL_SEARCHES
2498 WCHAR userShellFolderPath[MAX_PATH], shellFolderPath[MAX_PATH];
2499 LPCWSTR pUserShellFolderPath, pShellFolderPath;
2500 HRESULT hr = S_OK;
2501 HKEY hRootKey;
2502 HANDLE hToken;
2504 TRACE("%s\n", bDefault ? "TRUE" : "FALSE");
2505 if (bDefault)
2507 hToken = (HANDLE)-1;
2508 hRootKey = HKEY_USERS;
2509 strcpyW(userShellFolderPath, DefaultW);
2510 PathAddBackslashW(userShellFolderPath);
2511 strcatW(userShellFolderPath, szSHUserFolders);
2512 pUserShellFolderPath = userShellFolderPath;
2513 strcpyW(shellFolderPath, DefaultW);
2514 PathAddBackslashW(shellFolderPath);
2515 strcatW(shellFolderPath, szSHFolders);
2516 pShellFolderPath = shellFolderPath;
2518 else
2520 hToken = NULL;
2521 hRootKey = HKEY_CURRENT_USER;
2522 pUserShellFolderPath = szSHUserFolders;
2523 pShellFolderPath = szSHFolders;
2526 hr = _SHRegisterFolders(hRootKey, hToken, pUserShellFolderPath,
2527 pShellFolderPath, folders, sizeof(folders) / sizeof(folders[0]));
2528 TRACE("returning 0x%08x\n", hr);
2529 return hr;
2532 static HRESULT _SHRegisterCommonShellFolders(void)
2534 static const UINT folders[] = {
2535 CSIDL_COMMON_STARTMENU,
2536 CSIDL_COMMON_PROGRAMS,
2537 CSIDL_COMMON_STARTUP,
2538 CSIDL_COMMON_DESKTOPDIRECTORY,
2539 CSIDL_COMMON_FAVORITES,
2540 CSIDL_COMMON_APPDATA,
2541 CSIDL_COMMON_TEMPLATES,
2542 CSIDL_COMMON_DOCUMENTS,
2543 CSIDL_COMMON_ADMINTOOLS,
2544 CSIDL_COMMON_MUSIC,
2545 CSIDL_COMMON_PICTURES,
2546 CSIDL_COMMON_VIDEO,
2548 HRESULT hr;
2550 TRACE("\n");
2551 hr = _SHRegisterFolders(HKEY_LOCAL_MACHINE, NULL, szSHUserFolders,
2552 szSHFolders, folders, sizeof(folders) / sizeof(folders[0]));
2553 TRACE("returning 0x%08x\n", hr);
2554 return hr;
2557 /******************************************************************************
2558 * _SHAppendToUnixPath [Internal]
2560 * Helper function for _SHCreateSymbolicLinks. Appends pwszSubPath (or the
2561 * corresponding resource, if IS_INTRESOURCE) to the unix base path 'szBasePath'
2562 * and replaces backslashes with slashes.
2564 * PARAMS
2565 * szBasePath [IO] The unix base path, which will be appended to (CP_UNXICP).
2566 * pwszSubPath [I] Sub-path or resource id (use MAKEINTRESOURCEW).
2568 * RETURNS
2569 * Success: TRUE,
2570 * Failure: FALSE
2572 static inline BOOL _SHAppendToUnixPath(char *szBasePath, LPCWSTR pwszSubPath) {
2573 WCHAR wszSubPath[MAX_PATH];
2574 int cLen = strlen(szBasePath);
2575 char *pBackslash;
2577 if (IS_INTRESOURCE(pwszSubPath)) {
2578 if (!LoadStringW(shell32_hInstance, LOWORD(pwszSubPath), wszSubPath, MAX_PATH)) {
2579 /* Fall back to hard coded defaults. */
2580 switch (LOWORD(pwszSubPath)) {
2581 case IDS_PERSONAL:
2582 lstrcpyW(wszSubPath, DocumentsW);
2583 break;
2584 case IDS_MYMUSIC:
2585 lstrcpyW(wszSubPath, My_MusicW);
2586 break;
2587 case IDS_MYPICTURES:
2588 lstrcpyW(wszSubPath, My_PicturesW);
2589 break;
2590 case IDS_MYVIDEOS:
2591 lstrcpyW(wszSubPath, My_VideosW);
2592 break;
2593 default:
2594 ERR("LoadString(%d) failed!\n", LOWORD(pwszSubPath));
2595 return FALSE;
2598 } else {
2599 lstrcpyW(wszSubPath, pwszSubPath);
2602 if (szBasePath[cLen-1] != '/') szBasePath[cLen++] = '/';
2604 if (!WideCharToMultiByte(CP_UNIXCP, 0, wszSubPath, -1, szBasePath + cLen,
2605 FILENAME_MAX - cLen, NULL, NULL))
2607 return FALSE;
2610 pBackslash = szBasePath + cLen;
2611 while ((pBackslash = strchr(pBackslash, '\\'))) *pBackslash = '/';
2613 return TRUE;
2616 /******************************************************************************
2617 * _SHCreateSymbolicLinks [Internal]
2619 * Sets up symbol links for various shell folders to point into the users home
2620 * directory. We do an educated guess about what the user would probably want:
2621 * - If there is a 'My Documents' directory in $HOME, the user probably wants
2622 * wine's 'My Documents' to point there. Furthermore, we imply that the user
2623 * is a Windows lover and has no problem with wine creating 'My Pictures',
2624 * 'My Music' and 'My Videos' subfolders under '$HOME/My Documents', if those
2625 * do not already exits. We put appropriate symbolic links in place for those,
2626 * too.
2627 * - If there is no 'My Documents' directory in $HOME, we let 'My Documents'
2628 * point directly to $HOME. We assume the user to be a unix hacker who does not
2629 * want wine to create anything anywhere besides the .wine directory. So, if
2630 * there already is a 'My Music' directory in $HOME, we symlink the 'My Music'
2631 * shell folder to it. But if not, then we check XDG_MUSIC_DIR - "well known"
2632 * directory, and try to link to that. If that fails, then we symlink to
2633 * $HOME directly. The same holds fo 'My Pictures' and 'My Videos'.
2634 * - The Desktop shell folder is symlinked to XDG_DESKTOP_DIR. If that does not
2635 * exist, then we try '$HOME/Desktop'. If that does not exist, then we leave
2636 * it alone.
2637 * ('My Music',... above in fact means LoadString(IDS_MYMUSIC))
2639 static void _SHCreateSymbolicLinks(void)
2641 UINT aidsMyStuff[] = { IDS_MYPICTURES, IDS_MYVIDEOS, IDS_MYMUSIC }, i;
2642 int acsidlMyStuff[] = { CSIDL_MYPICTURES, CSIDL_MYVIDEO, CSIDL_MYMUSIC };
2643 static const char * const xdg_dirs[] = { "PICTURES", "VIDEOS", "MUSIC", "DESKTOP" };
2644 static const unsigned int num = sizeof(xdg_dirs) / sizeof(xdg_dirs[0]);
2645 WCHAR wszTempPath[MAX_PATH];
2646 char szPersonalTarget[FILENAME_MAX], *pszPersonal;
2647 char szMyStuffTarget[FILENAME_MAX], *pszMyStuff;
2648 char szDesktopTarget[FILENAME_MAX], *pszDesktop;
2649 struct stat statFolder;
2650 const char *pszHome;
2651 HRESULT hr;
2652 char ** xdg_results;
2653 char * xdg_desktop_dir;
2655 /* Create all necessary profile sub-dirs up to 'My Documents' and get the unix path. */
2656 hr = SHGetFolderPathW(NULL, CSIDL_PERSONAL|CSIDL_FLAG_CREATE, NULL,
2657 SHGFP_TYPE_DEFAULT, wszTempPath);
2658 if (FAILED(hr)) return;
2659 pszPersonal = wine_get_unix_file_name(wszTempPath);
2660 if (!pszPersonal) return;
2662 hr = XDG_UserDirLookup(xdg_dirs, num, &xdg_results);
2663 if (FAILED(hr)) xdg_results = NULL;
2665 pszHome = getenv("HOME");
2666 if (pszHome && !stat(pszHome, &statFolder) && S_ISDIR(statFolder.st_mode)) {
2667 strcpy(szPersonalTarget, pszHome);
2668 if (_SHAppendToUnixPath(szPersonalTarget, MAKEINTRESOURCEW(IDS_PERSONAL)) &&
2669 !stat(szPersonalTarget, &statFolder) && S_ISDIR(statFolder.st_mode))
2671 /* '$HOME/My Documents' exists. Create 'My Pictures', 'My Videos' and
2672 * 'My Music' subfolders or fail silently if they already exist. */
2673 for (i = 0; i < sizeof(aidsMyStuff)/sizeof(aidsMyStuff[0]); i++) {
2674 strcpy(szMyStuffTarget, szPersonalTarget);
2675 if (_SHAppendToUnixPath(szMyStuffTarget, MAKEINTRESOURCEW(aidsMyStuff[i])))
2676 mkdir(szMyStuffTarget, 0777);
2679 else
2681 /* '$HOME/My Documents' doesn't exists, but '$HOME' does. */
2682 strcpy(szPersonalTarget, pszHome);
2685 /* Replace 'My Documents' directory with a symlink of fail silently if not empty. */
2686 rmdir(pszPersonal);
2687 symlink(szPersonalTarget, pszPersonal);
2689 else
2691 /* '$HOME' doesn't exist. Create 'My Pictures', 'My Videos' and 'My Music' subdirs
2692 * in '%USERPROFILE%\\My Documents' or fail silently if they already exist. */
2693 pszHome = NULL;
2694 strcpy(szPersonalTarget, pszPersonal);
2695 for (i = 0; i < sizeof(aidsMyStuff)/sizeof(aidsMyStuff[0]); i++) {
2696 strcpy(szMyStuffTarget, szPersonalTarget);
2697 if (_SHAppendToUnixPath(szMyStuffTarget, MAKEINTRESOURCEW(aidsMyStuff[i])))
2698 mkdir(szMyStuffTarget, 0777);
2702 /* Create symbolic links for 'My Pictures', 'My Videos' and 'My Music'. */
2703 for (i=0; i < sizeof(aidsMyStuff)/sizeof(aidsMyStuff[0]); i++) {
2704 /* Create the current 'My Whatever' folder and get its unix path. */
2705 hr = SHGetFolderPathW(NULL, acsidlMyStuff[i]|CSIDL_FLAG_CREATE, NULL,
2706 SHGFP_TYPE_DEFAULT, wszTempPath);
2707 if (FAILED(hr)) continue;
2708 pszMyStuff = wine_get_unix_file_name(wszTempPath);
2709 if (!pszMyStuff) continue;
2711 strcpy(szMyStuffTarget, szPersonalTarget);
2712 if (_SHAppendToUnixPath(szMyStuffTarget, MAKEINTRESOURCEW(aidsMyStuff[i])) &&
2713 !stat(szMyStuffTarget, &statFolder) && S_ISDIR(statFolder.st_mode))
2715 /* If there's a 'My Whatever' directory where 'My Documents' links to, link to it. */
2716 rmdir(pszMyStuff);
2717 symlink(szMyStuffTarget, pszMyStuff);
2719 else
2721 rmdir(pszMyStuff);
2722 if (xdg_results && xdg_results[i])
2724 /* the folder specified by XDG_XXX_DIR exists, link to it. */
2725 symlink(xdg_results[i], pszMyStuff);
2727 else
2729 /* Else link to where 'My Documents' itself links to. */
2730 symlink(szPersonalTarget, pszMyStuff);
2733 HeapFree(GetProcessHeap(), 0, pszMyStuff);
2736 /* Last but not least, the Desktop folder */
2737 if (pszHome)
2738 strcpy(szDesktopTarget, pszHome);
2739 else
2740 strcpy(szDesktopTarget, pszPersonal);
2741 HeapFree(GetProcessHeap(), 0, pszPersonal);
2743 xdg_desktop_dir = xdg_results ? xdg_results[num - 1] : NULL;
2744 if (xdg_desktop_dir ||
2745 (_SHAppendToUnixPath(szDesktopTarget, DesktopW) &&
2746 !stat(szDesktopTarget, &statFolder) && S_ISDIR(statFolder.st_mode)))
2748 hr = SHGetFolderPathW(NULL, CSIDL_DESKTOPDIRECTORY|CSIDL_FLAG_CREATE, NULL,
2749 SHGFP_TYPE_DEFAULT, wszTempPath);
2750 if (SUCCEEDED(hr) && (pszDesktop = wine_get_unix_file_name(wszTempPath)))
2752 rmdir(pszDesktop);
2753 if (xdg_desktop_dir)
2754 symlink(xdg_desktop_dir, pszDesktop);
2755 else
2756 symlink(szDesktopTarget, pszDesktop);
2757 HeapFree(GetProcessHeap(), 0, pszDesktop);
2761 /* Free resources allocated by XDG_UserDirLookup() */
2762 if (xdg_results)
2764 for (i = 0; i < num; i++)
2765 HeapFree(GetProcessHeap(), 0, xdg_results[i]);
2766 HeapFree(GetProcessHeap(), 0, xdg_results);
2770 /******************************************************************************
2771 * create_extra_folders [Internal]
2773 * Create some extra folders that don't have a standard CSIDL definition.
2775 static HRESULT create_extra_folders(void)
2777 static const WCHAR environW[] = {'E','n','v','i','r','o','n','m','e','n','t',0};
2778 static const WCHAR microsoftW[] = {'M','i','c','r','o','s','o','f','t',0};
2779 static const WCHAR TempW[] = {'T','e','m','p',0};
2780 static const WCHAR TEMPW[] = {'T','E','M','P',0};
2781 static const WCHAR TMPW[] = {'T','M','P',0};
2782 WCHAR path[MAX_PATH+5];
2783 HRESULT hr;
2784 HKEY hkey;
2785 DWORD type, size, ret;
2787 ret = RegCreateKeyW( HKEY_CURRENT_USER, environW, &hkey );
2788 if (ret) return HRESULT_FROM_WIN32( ret );
2790 /* FIXME: should be under AppData, but we don't want spaces in the temp path */
2791 hr = SHGetFolderPathAndSubDirW( 0, CSIDL_PROFILE | CSIDL_FLAG_CREATE, NULL,
2792 SHGFP_TYPE_DEFAULT, TempW, path );
2793 if (SUCCEEDED(hr))
2795 size = sizeof(path);
2796 if (RegQueryValueExW( hkey, TEMPW, NULL, &type, (LPBYTE)path, &size ))
2797 RegSetValueExW( hkey, TEMPW, 0, REG_SZ, (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR) );
2798 size = sizeof(path);
2799 if (RegQueryValueExW( hkey, TMPW, NULL, &type, (LPBYTE)path, &size ))
2800 RegSetValueExW( hkey, TMPW, 0, REG_SZ, (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR) );
2802 RegCloseKey( hkey );
2804 if (SUCCEEDED(hr))
2806 hr = SHGetFolderPathAndSubDirW( 0, CSIDL_COMMON_APPDATA | CSIDL_FLAG_CREATE, NULL,
2807 SHGFP_TYPE_DEFAULT, microsoftW, path );
2809 return hr;
2813 /******************************************************************************
2814 * set_folder_attributes
2816 * Set the various folder attributes registry keys.
2818 static HRESULT set_folder_attributes(void)
2820 static const WCHAR clsidW[] = {'C','L','S','I','D','\\',0 };
2821 static const WCHAR shellfolderW[] = {'\\','S','h','e','l','l','F','o','l','d','e','r', 0 };
2822 static const WCHAR wfparsingW[] = {'W','a','n','t','s','F','O','R','P','A','R','S','I','N','G',0};
2823 static const WCHAR wfdisplayW[] = {'W','a','n','t','s','F','O','R','D','I','S','P','L','A','Y',0};
2824 static const WCHAR hideasdeleteW[] = {'H','i','d','e','A','s','D','e','l','e','t','e','P','e','r','U','s','e','r',0};
2825 static const WCHAR cfattributesW[] = {'C','a','l','l','F','o','r','A','t','t','r','i','b','u','t','e','s',0};
2826 static const WCHAR emptyW[] = {0};
2828 static const struct
2830 const CLSID *clsid;
2831 BOOL wfparsing : 1;
2832 BOOL wfdisplay : 1;
2833 BOOL hideasdel : 1;
2834 DWORD attr;
2835 DWORD call_for_attr;
2836 } folders[] =
2838 { &CLSID_UnixFolder, TRUE, FALSE, FALSE },
2839 { &CLSID_UnixDosFolder, TRUE, FALSE, FALSE,
2840 SFGAO_FILESYSANCESTOR|SFGAO_FOLDER|SFGAO_HASSUBFOLDER, SFGAO_FILESYSTEM },
2841 { &CLSID_FolderShortcut, FALSE, FALSE, FALSE,
2842 SFGAO_FILESYSTEM|SFGAO_FOLDER|SFGAO_LINK,
2843 SFGAO_HASSUBFOLDER|SFGAO_FILESYSTEM|SFGAO_FOLDER|SFGAO_FILESYSANCESTOR },
2844 { &CLSID_MyDocuments, TRUE, FALSE, FALSE,
2845 SFGAO_FILESYSANCESTOR|SFGAO_FOLDER|SFGAO_HASSUBFOLDER, SFGAO_FILESYSTEM },
2846 { &CLSID_RecycleBin, FALSE, FALSE, FALSE,
2847 SFGAO_FOLDER|SFGAO_DROPTARGET|SFGAO_HASPROPSHEET },
2848 { &CLSID_ControlPanel, FALSE, TRUE, TRUE,
2849 SFGAO_FOLDER|SFGAO_HASSUBFOLDER }
2852 unsigned int i;
2853 WCHAR buffer[39 + (sizeof(clsidW) + sizeof(shellfolderW)) / sizeof(WCHAR)];
2854 LONG res;
2855 HKEY hkey;
2857 for (i = 0; i < sizeof(folders)/sizeof(folders[0]); i++)
2859 strcpyW( buffer, clsidW );
2860 StringFromGUID2( folders[i].clsid, buffer + strlenW(buffer), 39 );
2861 strcatW( buffer, shellfolderW );
2862 res = RegCreateKeyExW( HKEY_CLASSES_ROOT, buffer, 0, NULL, 0,
2863 KEY_READ | KEY_WRITE, NULL, &hkey, NULL);
2864 if (res) return HRESULT_FROM_WIN32( res );
2865 if (folders[i].wfparsing)
2866 res = RegSetValueExW( hkey, wfparsingW, 0, REG_SZ, (const BYTE *)emptyW, sizeof(emptyW) );
2867 if (folders[i].wfdisplay)
2868 res = RegSetValueExW( hkey, wfdisplayW, 0, REG_SZ, (const BYTE *)emptyW, sizeof(emptyW) );
2869 if (folders[i].hideasdel)
2870 res = RegSetValueExW( hkey, hideasdeleteW, 0, REG_SZ, (const BYTE *)emptyW, sizeof(emptyW) );
2871 if (folders[i].attr)
2872 res = RegSetValueExW( hkey, szAttributes, 0, REG_DWORD,
2873 (const BYTE *)&folders[i].attr, sizeof(DWORD));
2874 if (folders[i].call_for_attr)
2875 res = RegSetValueExW( hkey, cfattributesW, 0, REG_DWORD,
2876 (const BYTE *)&folders[i].call_for_attr, sizeof(DWORD));
2877 RegCloseKey( hkey );
2879 return S_OK;
2883 /* Register the default values in the registry, as some apps seem to depend
2884 * on their presence. The set registered was taken from Windows XP.
2886 HRESULT SHELL_RegisterShellFolders(void)
2888 HRESULT hr;
2890 /* Set up '$HOME' targeted symlinks for 'My Documents', 'My Pictures',
2891 * 'My Videos', 'My Music' and 'Desktop' in advance, so that the
2892 * _SHRegister*ShellFolders() functions will find everything nice and clean
2893 * and thus will not attempt to create them in the profile directory. */
2894 _SHCreateSymbolicLinks();
2896 hr = _SHRegisterUserShellFolders(TRUE);
2897 if (SUCCEEDED(hr))
2898 hr = _SHRegisterUserShellFolders(FALSE);
2899 if (SUCCEEDED(hr))
2900 hr = _SHRegisterCommonShellFolders();
2901 if (SUCCEEDED(hr))
2902 hr = create_extra_folders();
2903 if (SUCCEEDED(hr))
2904 hr = set_folder_attributes();
2905 return hr;
2908 /*************************************************************************
2909 * SHGetSpecialFolderPathA [SHELL32.@]
2911 BOOL WINAPI SHGetSpecialFolderPathA (
2912 HWND hwndOwner,
2913 LPSTR szPath,
2914 int nFolder,
2915 BOOL bCreate)
2917 return SHGetFolderPathA(hwndOwner, nFolder + (bCreate ? CSIDL_FLAG_CREATE : 0), NULL, 0,
2918 szPath) == S_OK;
2921 /*************************************************************************
2922 * SHGetSpecialFolderPathW
2924 BOOL WINAPI SHGetSpecialFolderPathW (
2925 HWND hwndOwner,
2926 LPWSTR szPath,
2927 int nFolder,
2928 BOOL bCreate)
2930 return SHGetFolderPathW(hwndOwner, nFolder + (bCreate ? CSIDL_FLAG_CREATE : 0), NULL, 0,
2931 szPath) == S_OK;
2934 /*************************************************************************
2935 * SHGetSpecialFolderPath (SHELL32.175)
2937 BOOL WINAPI SHGetSpecialFolderPathAW (
2938 HWND hwndOwner,
2939 LPVOID szPath,
2940 int nFolder,
2941 BOOL bCreate)
2944 if (SHELL_OsIsUnicode())
2945 return SHGetSpecialFolderPathW (hwndOwner, szPath, nFolder, bCreate);
2946 return SHGetSpecialFolderPathA (hwndOwner, szPath, nFolder, bCreate);
2949 /*************************************************************************
2950 * SHGetFolderLocation [SHELL32.@]
2952 * Gets the folder locations from the registry and creates a pidl.
2954 * PARAMS
2955 * hwndOwner [I]
2956 * nFolder [I] CSIDL_xxxxx
2957 * hToken [I] token representing user, or NULL for current user, or -1 for
2958 * default user
2959 * dwReserved [I] must be zero
2960 * ppidl [O] PIDL of a special folder
2962 * RETURNS
2963 * Success: S_OK
2964 * Failure: Standard OLE-defined error result, S_FALSE or E_INVALIDARG
2966 * NOTES
2967 * Creates missing reg keys and directories.
2968 * Mostly forwards to SHGetFolderPathW, but a few values of nFolder return
2969 * virtual folders that are handled here.
2971 HRESULT WINAPI SHGetFolderLocation(
2972 HWND hwndOwner,
2973 int nFolder,
2974 HANDLE hToken,
2975 DWORD dwReserved,
2976 LPITEMIDLIST *ppidl)
2978 HRESULT hr = E_INVALIDARG;
2980 TRACE("%p 0x%08x %p 0x%08x %p\n",
2981 hwndOwner, nFolder, hToken, dwReserved, ppidl);
2983 if (!ppidl)
2984 return E_INVALIDARG;
2985 if (dwReserved)
2986 return E_INVALIDARG;
2988 /* The virtual folders' locations are not user-dependent */
2989 *ppidl = NULL;
2990 switch (nFolder & CSIDL_FOLDER_MASK)
2992 case CSIDL_DESKTOP:
2993 *ppidl = _ILCreateDesktop();
2994 break;
2996 case CSIDL_PERSONAL:
2997 *ppidl = _ILCreateMyDocuments();
2998 break;
3000 case CSIDL_INTERNET:
3001 *ppidl = _ILCreateIExplore();
3002 break;
3004 case CSIDL_CONTROLS:
3005 *ppidl = _ILCreateControlPanel();
3006 break;
3008 case CSIDL_PRINTERS:
3009 *ppidl = _ILCreatePrinters();
3010 break;
3012 case CSIDL_BITBUCKET:
3013 *ppidl = _ILCreateBitBucket();
3014 break;
3016 case CSIDL_DRIVES:
3017 *ppidl = _ILCreateMyComputer();
3018 break;
3020 case CSIDL_NETWORK:
3021 *ppidl = _ILCreateNetwork();
3022 break;
3024 default:
3026 WCHAR szPath[MAX_PATH];
3028 hr = SHGetFolderPathW(hwndOwner, nFolder, hToken,
3029 SHGFP_TYPE_CURRENT, szPath);
3030 if (SUCCEEDED(hr))
3032 DWORD attributes=0;
3034 TRACE("Value=%s\n", debugstr_w(szPath));
3035 hr = SHILCreateFromPathW(szPath, ppidl, &attributes);
3037 else if (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
3039 /* unlike SHGetFolderPath, SHGetFolderLocation in shell32
3040 * version 6.0 returns E_FAIL for nonexistent paths
3042 hr = E_FAIL;
3046 if(*ppidl)
3047 hr = S_OK;
3049 TRACE("-- (new pidl %p)\n",*ppidl);
3050 return hr;
3053 /*************************************************************************
3054 * SHGetSpecialFolderLocation [SHELL32.@]
3056 * NOTES
3057 * In NT5, SHGetSpecialFolderLocation needs the <winntdir>/Recent
3058 * directory.
3060 HRESULT WINAPI SHGetSpecialFolderLocation(
3061 HWND hwndOwner,
3062 INT nFolder,
3063 LPITEMIDLIST * ppidl)
3065 HRESULT hr = E_INVALIDARG;
3067 TRACE("(%p,0x%x,%p)\n", hwndOwner,nFolder,ppidl);
3069 if (!ppidl)
3070 return E_INVALIDARG;
3072 hr = SHGetFolderLocation(hwndOwner, nFolder, NULL, 0, ppidl);
3073 return hr;
3076 static int csidl_from_id( const KNOWNFOLDERID *id )
3078 int i;
3079 for (i = 0; i < sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]); i++)
3080 if (IsEqualGUID( CSIDL_Data[i].id, id )) return i;
3081 return -1;
3084 /*************************************************************************
3085 * SHGetKnownFolderPath [SHELL32.@]
3087 HRESULT WINAPI SHGetKnownFolderPath(REFKNOWNFOLDERID rfid, DWORD flags, HANDLE token, PWSTR *path)
3089 HRESULT hr;
3090 WCHAR folder[MAX_PATH];
3091 int index = csidl_from_id( rfid );
3093 TRACE("%s, 0x%08x, %p, %p\n", debugstr_guid(rfid), flags, token, path);
3095 *path = NULL;
3097 if (index < 0)
3098 return HRESULT_FROM_WIN32( ERROR_FILE_NOT_FOUND );
3100 if (flags & KF_FLAG_CREATE)
3101 index |= CSIDL_FLAG_CREATE;
3103 if (flags & KF_FLAG_DONT_VERIFY)
3104 index |= CSIDL_FLAG_DONT_VERIFY;
3106 if (flags & KF_FLAG_NO_ALIAS)
3107 index |= CSIDL_FLAG_NO_ALIAS;
3109 if (flags & KF_FLAG_INIT)
3110 index |= CSIDL_FLAG_PER_USER_INIT;
3112 if (flags & ~(KF_FLAG_CREATE|KF_FLAG_DONT_VERIFY|KF_FLAG_NO_ALIAS|KF_FLAG_INIT))
3114 FIXME("flags 0x%08x not supported\n", flags);
3115 return E_INVALIDARG;
3118 hr = SHGetFolderPathW( NULL, index, token, 0, folder );
3119 if (SUCCEEDED(hr))
3121 *path = CoTaskMemAlloc( (strlenW( folder ) + 1) * sizeof(WCHAR) );
3122 if (!*path)
3123 return E_OUTOFMEMORY;
3124 strcpyW( *path, folder );
3126 return hr;
3129 /*************************************************************************
3130 * SHGetFolderPathEx [SHELL32.@]
3132 HRESULT WINAPI SHGetFolderPathEx(REFKNOWNFOLDERID rfid, DWORD flags, HANDLE token, LPWSTR path, DWORD len)
3134 HRESULT hr;
3135 WCHAR *buffer;
3137 TRACE("%s, 0x%08x, %p, %p, %u\n", debugstr_guid(rfid), flags, token, path, len);
3139 if (!path || !len) return E_INVALIDARG;
3141 hr = SHGetKnownFolderPath( rfid, flags, token, &buffer );
3142 if (SUCCEEDED( hr ))
3144 if (strlenW( buffer ) + 1 > len)
3146 CoTaskMemFree( buffer );
3147 return HRESULT_FROM_WIN32( ERROR_INSUFFICIENT_BUFFER );
3149 strcpyW( path, buffer );
3150 CoTaskMemFree( buffer );
3152 return hr;
3156 * Internal function to convert known folder identifier to path of registry key
3157 * associated with known folder.
3159 * Parameters:
3160 * rfid [I] pointer to known folder identifier (may be NULL)
3161 * lpStringGuid [I] string with known folder identifier (used when rfid is NULL)
3162 * lpPath [O] place to store string address. String should be
3163 * later freed using HeapFree(GetProcessHeap(),0, ... )
3165 static HRESULT get_known_folder_registry_path(
3166 REFKNOWNFOLDERID rfid,
3167 LPWSTR lpStringGuid,
3168 LPWSTR *lpPath)
3170 static const WCHAR sBackslash[] = {'\\',0};
3171 HRESULT hr = S_OK;
3172 int length;
3173 WCHAR sGuid[50];
3175 TRACE("(%s, %s, %p)\n", debugstr_guid(rfid), debugstr_w(lpStringGuid), lpPath);
3177 if(rfid)
3178 StringFromGUID2(rfid, sGuid, sizeof(sGuid)/sizeof(sGuid[0]));
3179 else
3180 lstrcpyW(sGuid, lpStringGuid);
3182 length = lstrlenW(szKnownFolderDescriptions)+51;
3183 *lpPath = HeapAlloc(GetProcessHeap(), 0, length*sizeof(WCHAR));
3184 if(!(*lpPath))
3185 hr = E_OUTOFMEMORY;
3187 if(SUCCEEDED(hr))
3189 lstrcpyW(*lpPath, szKnownFolderDescriptions);
3190 lstrcatW(*lpPath, sBackslash);
3191 lstrcatW(*lpPath, sGuid);
3194 return hr;
3197 static HRESULT get_known_folder_wstr(const WCHAR *regpath, const WCHAR *value, WCHAR **out)
3199 DWORD size = 0;
3200 HRESULT hr;
3202 size = 0;
3203 hr = HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE, regpath, value, RRF_RT_REG_SZ, NULL, NULL, &size));
3204 if(FAILED(hr))
3205 return hr;
3207 *out = CoTaskMemAlloc(size);
3208 if(!*out)
3209 return E_OUTOFMEMORY;
3211 hr = HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE, regpath, value, RRF_RT_REG_SZ, NULL, *out, &size));
3212 if(FAILED(hr)){
3213 CoTaskMemFree(*out);
3214 *out = NULL;
3217 return hr;
3220 static HRESULT get_known_folder_dword(const WCHAR *registryPath, const WCHAR *value, DWORD *out)
3222 DWORD dwSize = sizeof(DWORD);
3223 DWORD dwType;
3224 return HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE, registryPath, value, RRF_RT_DWORD, &dwType, out, &dwSize));
3228 * Internal function to get place where folder redirection information are stored.
3230 * Parameters:
3231 * rfid [I] pointer to known folder identifier (may be NULL)
3232 * rootKey [O] root key where the redirection information are stored
3233 * It can be HKLM for COMMON folders, and HKCU for PERUSER folders.
3234 * However, besides root key, path is always that same, and is stored
3235 * as "szKnownFolderRedirections" constant
3237 static HRESULT get_known_folder_redirection_place(
3238 REFKNOWNFOLDERID rfid,
3239 HKEY *rootKey)
3241 HRESULT hr;
3242 LPWSTR lpRegistryPath = NULL;
3243 KF_CATEGORY category;
3245 /* first, get known folder's category */
3246 hr = get_known_folder_registry_path(rfid, NULL, &lpRegistryPath);
3248 if(SUCCEEDED(hr))
3249 hr = get_known_folder_dword(lpRegistryPath, szCategory, &category);
3251 if(SUCCEEDED(hr))
3253 if(category == KF_CATEGORY_COMMON)
3255 *rootKey = HKEY_LOCAL_MACHINE;
3256 hr = S_OK;
3258 else if(category == KF_CATEGORY_PERUSER)
3260 *rootKey = HKEY_CURRENT_USER;
3261 hr = S_OK;
3263 else
3264 hr = E_FAIL;
3267 HeapFree(GetProcessHeap(), 0, lpRegistryPath);
3268 return hr;
3271 static HRESULT get_known_folder_path_by_id(REFKNOWNFOLDERID folderId, LPWSTR lpRegistryPath, DWORD dwFlags, LPWSTR *ppszPath);
3273 static HRESULT redirect_known_folder(
3274 REFKNOWNFOLDERID rfid,
3275 HWND hwnd,
3276 KF_REDIRECT_FLAGS flags,
3277 LPCWSTR pszTargetPath,
3278 UINT cFolders,
3279 KNOWNFOLDERID const *pExclusion,
3280 LPWSTR *ppszError)
3282 HRESULT hr;
3283 HKEY rootKey = HKEY_LOCAL_MACHINE, hKey;
3284 WCHAR sGuid[39];
3285 LPWSTR lpRegistryPath = NULL, lpSrcPath = NULL;
3286 TRACE("(%s, %p, 0x%08x, %s, %d, %p, %p)\n", debugstr_guid(rfid), hwnd, flags, debugstr_w(pszTargetPath), cFolders, pExclusion, ppszError);
3288 if (ppszError) *ppszError = NULL;
3290 hr = get_known_folder_registry_path(rfid, NULL, &lpRegistryPath);
3292 if(SUCCEEDED(hr))
3293 hr = get_known_folder_path_by_id(rfid, lpRegistryPath, 0, &lpSrcPath);
3295 HeapFree(GetProcessHeap(), 0, lpRegistryPath);
3297 /* get path to redirection storage */
3298 if(SUCCEEDED(hr))
3299 hr = get_known_folder_redirection_place(rfid, &rootKey);
3301 /* write redirection information */
3302 if(SUCCEEDED(hr))
3303 hr = HRESULT_FROM_WIN32(RegCreateKeyExW(rootKey, szKnownFolderRedirections, 0, NULL, 0, KEY_WRITE, NULL, &hKey, NULL));
3305 if(SUCCEEDED(hr))
3307 StringFromGUID2(rfid, sGuid, sizeof(sGuid)/sizeof(sGuid[0]));
3309 hr = HRESULT_FROM_WIN32(RegSetValueExW(hKey, sGuid, 0, REG_SZ, (LPBYTE)pszTargetPath, (lstrlenW(pszTargetPath)+1)*sizeof(WCHAR)));
3311 RegCloseKey(hKey);
3314 /* make sure destination path exists */
3315 SHCreateDirectory(NULL, pszTargetPath);
3317 /* copy content if required */
3318 if(SUCCEEDED(hr) && (flags & KF_REDIRECT_COPY_CONTENTS) )
3320 static const WCHAR sWildcard[] = {'\\','*',0};
3321 WCHAR srcPath[MAX_PATH+1], dstPath[MAX_PATH+1];
3322 SHFILEOPSTRUCTW fileOp;
3324 ZeroMemory(srcPath, sizeof(srcPath));
3325 lstrcpyW(srcPath, lpSrcPath);
3326 lstrcatW(srcPath, sWildcard);
3328 ZeroMemory(dstPath, sizeof(dstPath));
3329 lstrcpyW(dstPath, pszTargetPath);
3331 ZeroMemory(&fileOp, sizeof(fileOp));
3333 if(flags & KF_REDIRECT_DEL_SOURCE_CONTENTS)
3334 fileOp.wFunc = FO_MOVE;
3335 else
3336 fileOp.wFunc = FO_COPY;
3338 fileOp.pFrom = srcPath;
3339 fileOp.pTo = dstPath;
3340 fileOp.fFlags = FOF_NO_UI;
3342 hr = (SHFileOperationW(&fileOp)==0 ? S_OK : E_FAIL);
3344 if(flags & KF_REDIRECT_DEL_SOURCE_CONTENTS)
3346 ZeroMemory(srcPath, sizeof(srcPath));
3347 lstrcpyW(srcPath, lpSrcPath);
3349 ZeroMemory(&fileOp, sizeof(fileOp));
3350 fileOp.wFunc = FO_DELETE;
3351 fileOp.pFrom = srcPath;
3352 fileOp.fFlags = FOF_NO_UI;
3354 hr = (SHFileOperationW(&fileOp)==0 ? S_OK : E_FAIL);
3358 CoTaskMemFree(lpSrcPath);
3360 return hr;
3364 struct knownfolder
3366 IKnownFolder IKnownFolder_iface;
3367 LONG refs;
3368 KNOWNFOLDERID id;
3369 LPWSTR registryPath;
3372 static inline struct knownfolder *impl_from_IKnownFolder( IKnownFolder *iface )
3374 return CONTAINING_RECORD( iface, struct knownfolder, IKnownFolder_iface );
3377 static ULONG WINAPI knownfolder_AddRef(
3378 IKnownFolder *iface )
3380 struct knownfolder *knownfolder = impl_from_IKnownFolder( iface );
3381 return InterlockedIncrement( &knownfolder->refs );
3384 static ULONG WINAPI knownfolder_Release(
3385 IKnownFolder *iface )
3387 struct knownfolder *knownfolder = impl_from_IKnownFolder( iface );
3388 LONG refs = InterlockedDecrement( &knownfolder->refs );
3389 if (!refs)
3391 TRACE("destroying %p\n", knownfolder);
3392 HeapFree( GetProcessHeap(), 0, knownfolder->registryPath);
3393 HeapFree( GetProcessHeap(), 0, knownfolder );
3395 return refs;
3398 static HRESULT WINAPI knownfolder_QueryInterface(
3399 IKnownFolder *iface,
3400 REFIID riid,
3401 void **ppv )
3403 struct knownfolder *This = impl_from_IKnownFolder( iface );
3405 TRACE("%p %s %p\n", This, debugstr_guid( riid ), ppv );
3407 if ( IsEqualGUID( riid, &IID_IKnownFolder ) ||
3408 IsEqualGUID( riid, &IID_IUnknown ) )
3410 *ppv = iface;
3412 else
3414 FIXME("interface %s not implemented\n", debugstr_guid(riid));
3415 return E_NOINTERFACE;
3417 IKnownFolder_AddRef( iface );
3418 return S_OK;
3421 static HRESULT knownfolder_set_id(
3422 struct knownfolder *knownfolder,
3423 const KNOWNFOLDERID *kfid)
3425 HKEY hKey;
3426 HRESULT hr;
3428 TRACE("%s\n", debugstr_guid(kfid));
3430 knownfolder->id = *kfid;
3432 /* check is it registry-registered folder */
3433 hr = get_known_folder_registry_path(kfid, NULL, &knownfolder->registryPath);
3434 if(SUCCEEDED(hr))
3435 hr = HRESULT_FROM_WIN32(RegOpenKeyExW(HKEY_LOCAL_MACHINE, knownfolder->registryPath, 0, 0, &hKey));
3437 if(SUCCEEDED(hr))
3439 hr = S_OK;
3440 RegCloseKey(hKey);
3442 else
3444 /* This known folder is not registered. To mark it, we set registryPath to NULL */
3445 HeapFree(GetProcessHeap(), 0, knownfolder->registryPath);
3446 knownfolder->registryPath = NULL;
3447 hr = S_OK;
3450 return hr;
3453 static HRESULT WINAPI knownfolder_GetId(
3454 IKnownFolder *iface,
3455 KNOWNFOLDERID *pkfid)
3457 struct knownfolder *knownfolder = impl_from_IKnownFolder( iface );
3459 TRACE("%p\n", pkfid);
3461 *pkfid = knownfolder->id;
3462 return S_OK;
3465 static HRESULT WINAPI knownfolder_GetCategory(
3466 IKnownFolder *iface,
3467 KF_CATEGORY *pCategory)
3469 struct knownfolder *knownfolder = impl_from_IKnownFolder(iface);
3470 HRESULT hr = S_OK;
3472 TRACE("%p, %p\n", knownfolder, pCategory);
3474 /* we cannot get a category for a folder which is not registered */
3475 if(!knownfolder->registryPath)
3476 hr = E_FAIL;
3478 if(SUCCEEDED(hr))
3479 hr = get_known_folder_dword(knownfolder->registryPath, szCategory, pCategory);
3481 return hr;
3484 static HRESULT WINAPI knownfolder_GetShellItem(
3485 IKnownFolder *iface,
3486 DWORD dwFlags,
3487 REFIID riid,
3488 void **ppv)
3490 FIXME("0x%08x, %s, %p\n", dwFlags, debugstr_guid(riid), ppv);
3491 return E_NOTIMPL;
3494 static HRESULT get_known_folder_path(
3495 LPWSTR sFolderId,
3496 LPWSTR registryPath,
3497 LPWSTR *ppszPath)
3499 static const WCHAR sBackslash[] = {'\\',0};
3500 HRESULT hr;
3501 DWORD dwSize, dwType;
3502 WCHAR path[MAX_PATH] = {0};
3503 WCHAR parentGuid[39];
3504 KF_CATEGORY category;
3505 LPWSTR parentRegistryPath, parentPath;
3506 HKEY hRedirectionRootKey = NULL;
3508 TRACE("(%s, %p)\n", debugstr_w(registryPath), ppszPath);
3509 *ppszPath = NULL;
3511 /* check if folder has parent */
3512 dwSize = sizeof(parentGuid);
3513 hr = HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE, registryPath, szParentFolder, RRF_RT_REG_SZ, &dwType, parentGuid, &dwSize));
3514 if(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) hr = S_FALSE;
3516 if(hr == S_OK)
3518 /* get parent's known folder path (recursive) */
3519 hr = get_known_folder_registry_path(NULL, parentGuid, &parentRegistryPath);
3520 if(FAILED(hr)) return hr;
3522 hr = get_known_folder_path(parentGuid, parentRegistryPath, &parentPath);
3523 if(FAILED(hr)) {
3524 HeapFree(GetProcessHeap(), 0, parentRegistryPath);
3525 return hr;
3528 lstrcatW(path, parentPath);
3529 lstrcatW(path, sBackslash);
3531 HeapFree(GetProcessHeap(), 0, parentRegistryPath);
3532 HeapFree(GetProcessHeap(), 0, parentPath);
3535 /* check, if folder was redirected */
3536 if(SUCCEEDED(hr))
3537 hr = get_known_folder_dword(registryPath, szCategory, &category);
3539 if(SUCCEEDED(hr))
3541 if(category == KF_CATEGORY_COMMON)
3542 hRedirectionRootKey = HKEY_LOCAL_MACHINE;
3543 else if(category == KF_CATEGORY_PERUSER)
3544 hRedirectionRootKey = HKEY_CURRENT_USER;
3546 if(hRedirectionRootKey)
3548 hr = HRESULT_FROM_WIN32(RegGetValueW(hRedirectionRootKey, szKnownFolderRedirections, sFolderId, RRF_RT_REG_SZ, NULL, NULL, &dwSize));
3550 if(SUCCEEDED(hr))
3552 *ppszPath = CoTaskMemAlloc(dwSize+(lstrlenW(path)+1)*sizeof(WCHAR));
3553 if(!*ppszPath) hr = E_OUTOFMEMORY;
3556 if(SUCCEEDED(hr))
3558 lstrcpyW(*ppszPath, path);
3559 hr = HRESULT_FROM_WIN32(RegGetValueW(hRedirectionRootKey, szKnownFolderRedirections, sFolderId, RRF_RT_REG_SZ, NULL, *ppszPath + lstrlenW(path), &dwSize));
3563 if(!*ppszPath)
3565 /* no redirection, use previous way - read the relative path from folder definition */
3566 hr = HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE, registryPath, szRelativePath, RRF_RT_REG_SZ, &dwType, NULL, &dwSize));
3568 if(SUCCEEDED(hr))
3570 *ppszPath = CoTaskMemAlloc(dwSize+(lstrlenW(path)+1)*sizeof(WCHAR));
3571 if(!*ppszPath) hr = E_OUTOFMEMORY;
3574 if(SUCCEEDED(hr))
3576 lstrcpyW(*ppszPath, path);
3577 hr = HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE, registryPath, szRelativePath, RRF_RT_REG_SZ, &dwType, *ppszPath + lstrlenW(path), &dwSize));
3582 TRACE("returning path: %s\n", debugstr_w(*ppszPath));
3583 return hr;
3586 static HRESULT get_known_folder_path_by_id(
3587 REFKNOWNFOLDERID folderId,
3588 LPWSTR lpRegistryPath,
3589 DWORD dwFlags,
3590 LPWSTR *ppszPath)
3592 HRESULT hr = E_FAIL;
3593 WCHAR sGuid[39];
3594 DWORD dwAttributes;
3596 TRACE("(%s, %s, 0x%08x, %p)\n", debugstr_guid(folderId), debugstr_w(lpRegistryPath), dwFlags, ppszPath);
3598 /* if this is registry-registered known folder, get path from registry */
3599 if(lpRegistryPath)
3601 StringFromGUID2(folderId, sGuid, sizeof(sGuid)/sizeof(sGuid[0]));
3603 hr = get_known_folder_path(sGuid, lpRegistryPath, ppszPath);
3605 /* in other case, use older way */
3607 if(FAILED(hr))
3608 hr = SHGetKnownFolderPath( folderId, dwFlags, NULL, ppszPath );
3610 if (FAILED(hr)) return hr;
3612 /* check if known folder really exists */
3613 dwAttributes = GetFileAttributesW(*ppszPath);
3614 if(dwAttributes == INVALID_FILE_ATTRIBUTES || !(dwAttributes & FILE_ATTRIBUTE_DIRECTORY) )
3616 TRACE("directory %s not found\n", debugstr_w(*ppszPath));
3617 CoTaskMemFree(*ppszPath);
3618 *ppszPath = NULL;
3619 hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
3622 return hr;
3625 static HRESULT WINAPI knownfolder_GetPath(
3626 IKnownFolder *iface,
3627 DWORD dwFlags,
3628 LPWSTR *ppszPath)
3630 struct knownfolder *knownfolder = impl_from_IKnownFolder( iface );
3631 TRACE("(%p, 0x%08x, %p)\n", knownfolder, dwFlags, ppszPath);
3633 return get_known_folder_path_by_id(&knownfolder->id, knownfolder->registryPath, dwFlags, ppszPath);
3636 static HRESULT WINAPI knownfolder_SetPath(
3637 IKnownFolder *iface,
3638 DWORD dwFlags,
3639 LPCWSTR pszPath)
3641 struct knownfolder *knownfolder = impl_from_IKnownFolder( iface );
3642 HRESULT hr = S_OK;
3644 TRACE("(%p, 0x%08x, %s)\n", knownfolder, dwFlags, debugstr_w(pszPath));
3646 /* check if the known folder is registered */
3647 if(!knownfolder->registryPath)
3648 hr = E_FAIL;
3650 if(SUCCEEDED(hr))
3651 hr = redirect_known_folder(&knownfolder->id, NULL, 0, pszPath, 0, NULL, NULL);
3653 return hr;
3656 static HRESULT WINAPI knownfolder_GetIDList(
3657 IKnownFolder *iface,
3658 DWORD dwFlags,
3659 PIDLIST_ABSOLUTE *ppidl)
3661 FIXME("0x%08x, %p\n", dwFlags, ppidl);
3662 return E_NOTIMPL;
3665 static HRESULT WINAPI knownfolder_GetFolderType(
3666 IKnownFolder *iface,
3667 FOLDERTYPEID *pftid)
3669 FIXME("%p\n", pftid);
3670 return E_NOTIMPL;
3673 static HRESULT WINAPI knownfolder_GetRedirectionCapabilities(
3674 IKnownFolder *iface,
3675 KF_REDIRECTION_CAPABILITIES *pCapabilities)
3677 FIXME("%p\n", pCapabilities);
3678 return E_NOTIMPL;
3681 static HRESULT WINAPI knownfolder_GetFolderDefinition(
3682 IKnownFolder *iface,
3683 KNOWNFOLDER_DEFINITION *pKFD)
3685 struct knownfolder *knownfolder = impl_from_IKnownFolder( iface );
3686 HRESULT hr;
3687 DWORD dwSize;
3688 WCHAR parentGuid[39];
3689 TRACE("(%p, %p)\n", knownfolder, pKFD);
3691 if(!pKFD) return E_INVALIDARG;
3693 ZeroMemory(pKFD, sizeof(*pKFD));
3695 /* required fields */
3696 hr = get_known_folder_dword(knownfolder->registryPath, szCategory, &pKFD->category);
3697 if(FAILED(hr))
3698 return hr;
3700 hr = get_known_folder_wstr(knownfolder->registryPath, szName, &pKFD->pszName);
3701 if(FAILED(hr))
3702 return hr;
3704 /* optional fields */
3705 dwSize = sizeof(parentGuid);
3706 hr = HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE, knownfolder->registryPath, szParentFolder,
3707 RRF_RT_REG_SZ, NULL, parentGuid, &dwSize));
3708 if(SUCCEEDED(hr))
3709 IIDFromString(parentGuid, &pKFD->fidParent);
3711 get_known_folder_dword(knownfolder->registryPath, szAttributes, &pKFD->dwAttributes);
3713 get_known_folder_wstr(knownfolder->registryPath, szRelativePath, &pKFD->pszRelativePath);
3715 return S_OK;
3718 static const struct IKnownFolderVtbl knownfolder_vtbl =
3720 knownfolder_QueryInterface,
3721 knownfolder_AddRef,
3722 knownfolder_Release,
3723 knownfolder_GetId,
3724 knownfolder_GetCategory,
3725 knownfolder_GetShellItem,
3726 knownfolder_GetPath,
3727 knownfolder_SetPath,
3728 knownfolder_GetIDList,
3729 knownfolder_GetFolderType,
3730 knownfolder_GetRedirectionCapabilities,
3731 knownfolder_GetFolderDefinition
3734 static HRESULT knownfolder_create( struct knownfolder **knownfolder )
3736 struct knownfolder *kf;
3738 kf = HeapAlloc( GetProcessHeap(), 0, sizeof(*kf) );
3739 if (!kf) return E_OUTOFMEMORY;
3741 kf->IKnownFolder_iface.lpVtbl = &knownfolder_vtbl;
3742 kf->refs = 1;
3743 memset( &kf->id, 0, sizeof(kf->id) );
3744 kf->registryPath = NULL;
3746 *knownfolder = kf;
3748 TRACE("returning iface %p\n", &kf->IKnownFolder_iface);
3749 return S_OK;
3752 struct foldermanager
3754 IKnownFolderManager IKnownFolderManager_iface;
3755 LONG refs;
3756 UINT num_ids;
3757 KNOWNFOLDERID *ids;
3760 static inline struct foldermanager *impl_from_IKnownFolderManager( IKnownFolderManager *iface )
3762 return CONTAINING_RECORD( iface, struct foldermanager, IKnownFolderManager_iface );
3765 static ULONG WINAPI foldermanager_AddRef(
3766 IKnownFolderManager *iface )
3768 struct foldermanager *foldermanager = impl_from_IKnownFolderManager( iface );
3769 return InterlockedIncrement( &foldermanager->refs );
3772 static ULONG WINAPI foldermanager_Release(
3773 IKnownFolderManager *iface )
3775 struct foldermanager *foldermanager = impl_from_IKnownFolderManager( iface );
3776 LONG refs = InterlockedDecrement( &foldermanager->refs );
3777 if (!refs)
3779 TRACE("destroying %p\n", foldermanager);
3780 HeapFree( GetProcessHeap(), 0, foldermanager->ids );
3781 HeapFree( GetProcessHeap(), 0, foldermanager );
3783 return refs;
3786 static HRESULT WINAPI foldermanager_QueryInterface(
3787 IKnownFolderManager *iface,
3788 REFIID riid,
3789 void **ppv )
3791 struct foldermanager *This = impl_from_IKnownFolderManager( iface );
3793 TRACE("%p %s %p\n", This, debugstr_guid( riid ), ppv );
3795 if ( IsEqualGUID( riid, &IID_IKnownFolderManager ) ||
3796 IsEqualGUID( riid, &IID_IUnknown ) )
3798 *ppv = iface;
3800 else
3802 FIXME("interface %s not implemented\n", debugstr_guid(riid));
3803 return E_NOINTERFACE;
3805 IKnownFolderManager_AddRef( iface );
3806 return S_OK;
3809 static HRESULT WINAPI foldermanager_FolderIdFromCsidl(
3810 IKnownFolderManager *iface,
3811 int nCsidl,
3812 KNOWNFOLDERID *pfid)
3814 TRACE("%d, %p\n", nCsidl, pfid);
3816 if (nCsidl >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
3817 return E_INVALIDARG;
3818 *pfid = *CSIDL_Data[nCsidl].id;
3819 return S_OK;
3822 static HRESULT WINAPI foldermanager_FolderIdToCsidl(
3823 IKnownFolderManager *iface,
3824 REFKNOWNFOLDERID rfid,
3825 int *pnCsidl)
3827 int csidl;
3829 TRACE("%s, %p\n", debugstr_guid(rfid), pnCsidl);
3831 csidl = csidl_from_id( rfid );
3832 if (csidl == -1) return E_INVALIDARG;
3833 *pnCsidl = csidl;
3834 return S_OK;
3837 static HRESULT WINAPI foldermanager_GetFolderIds(
3838 IKnownFolderManager *iface,
3839 KNOWNFOLDERID **ppKFId,
3840 UINT *pCount)
3842 struct foldermanager *fm = impl_from_IKnownFolderManager( iface );
3844 TRACE("%p, %p\n", ppKFId, pCount);
3846 *ppKFId = fm->ids;
3847 *pCount = fm->num_ids;
3848 return S_OK;
3851 static BOOL is_knownfolder( struct foldermanager *fm, const KNOWNFOLDERID *id )
3853 UINT i;
3854 HRESULT hr;
3855 LPWSTR registryPath = NULL;
3856 HKEY hKey;
3858 /* TODO: move all entries from "CSIDL_Data" static array to registry known folder descriptions */
3859 for (i = 0; i < fm->num_ids; i++)
3860 if (IsEqualGUID( &fm->ids[i], id )) return TRUE;
3862 hr = get_known_folder_registry_path(id, NULL, &registryPath);
3863 if(SUCCEEDED(hr))
3865 hr = HRESULT_FROM_WIN32(RegOpenKeyExW(HKEY_LOCAL_MACHINE, registryPath, 0, 0, &hKey));
3866 HeapFree(GetProcessHeap(), 0, registryPath);
3869 if(SUCCEEDED(hr))
3871 hr = S_OK;
3872 RegCloseKey(hKey);
3875 return hr == S_OK;
3878 static HRESULT WINAPI foldermanager_GetFolder(
3879 IKnownFolderManager *iface,
3880 REFKNOWNFOLDERID rfid,
3881 IKnownFolder **ppkf)
3883 struct foldermanager *fm = impl_from_IKnownFolderManager( iface );
3884 struct knownfolder *kf;
3885 HRESULT hr;
3887 TRACE("%s, %p\n", debugstr_guid(rfid), ppkf);
3889 if (!is_knownfolder( fm, rfid ))
3891 WARN("unknown folder\n");
3892 return E_INVALIDARG;
3894 hr = knownfolder_create( &kf );
3895 if (SUCCEEDED( hr ))
3897 hr = knownfolder_set_id( kf, rfid );
3898 *ppkf = &kf->IKnownFolder_iface;
3900 else
3901 *ppkf = NULL;
3903 return hr;
3906 static HRESULT WINAPI foldermanager_GetFolderByName(
3907 IKnownFolderManager *iface,
3908 LPCWSTR pszCanonicalName,
3909 IKnownFolder **ppkf)
3911 FIXME("%s, %p\n", debugstr_w(pszCanonicalName), ppkf);
3912 return E_NOTIMPL;
3915 static HRESULT WINAPI foldermanager_RegisterFolder(
3916 IKnownFolderManager *iface,
3917 REFKNOWNFOLDERID rfid,
3918 KNOWNFOLDER_DEFINITION const *pKFD)
3920 HRESULT hr;
3921 HKEY hKey = NULL;
3922 DWORD dwDisp;
3923 LPWSTR registryPath = NULL;
3924 TRACE("(%p, %s, %p)\n", iface, debugstr_guid(rfid), pKFD);
3926 hr = get_known_folder_registry_path(rfid, NULL, &registryPath);
3927 TRACE("registry path: %s\n", debugstr_w(registryPath));
3929 if(SUCCEEDED(hr))
3930 hr = HRESULT_FROM_WIN32(RegCreateKeyExW(HKEY_LOCAL_MACHINE, registryPath, 0, NULL, 0, KEY_WRITE, 0, &hKey, &dwDisp));
3932 if(SUCCEEDED(hr))
3934 if(dwDisp == REG_OPENED_EXISTING_KEY)
3935 hr = E_FAIL;
3937 if(SUCCEEDED(hr))
3938 hr = HRESULT_FROM_WIN32(RegSetValueExW(hKey, szCategory, 0, REG_DWORD, (LPBYTE)&pKFD->category, sizeof(pKFD->category)));
3940 if(SUCCEEDED(hr))
3941 hr = HRESULT_FROM_WIN32(RegSetValueExW(hKey, szName, 0, REG_SZ, (LPBYTE)pKFD->pszName, (lstrlenW(pKFD->pszName)+1)*sizeof(WCHAR) ));
3943 if(SUCCEEDED(hr) && !IsEqualGUID(&pKFD->fidParent, &GUID_NULL))
3945 WCHAR sParentGuid[39];
3946 StringFromGUID2(&pKFD->fidParent, sParentGuid, sizeof(sParentGuid)/sizeof(sParentGuid[0]));
3948 /* this known folder has parent folder */
3949 hr = HRESULT_FROM_WIN32(RegSetValueExW(hKey, szParentFolder, 0, REG_SZ, (LPBYTE)sParentGuid, sizeof(sParentGuid)));
3952 if(SUCCEEDED(hr) && pKFD->category != KF_CATEGORY_VIRTUAL)
3954 if(!pKFD->pszRelativePath)
3955 hr = E_INVALIDARG;
3957 if(SUCCEEDED(hr))
3958 hr = HRESULT_FROM_WIN32(RegSetValueExW(hKey, szRelativePath, 0, REG_SZ, (LPBYTE)pKFD->pszRelativePath, (lstrlenW(pKFD->pszRelativePath)+1)*sizeof(WCHAR) ));
3961 RegCloseKey(hKey);
3963 if(FAILED(hr))
3964 SHDeleteKeyW(HKEY_LOCAL_MACHINE, registryPath);
3967 HeapFree(GetProcessHeap(), 0, registryPath);
3968 return hr;
3971 static HRESULT WINAPI foldermanager_UnregisterFolder(
3972 IKnownFolderManager *iface,
3973 REFKNOWNFOLDERID rfid)
3975 HRESULT hr;
3976 LPWSTR registryPath = NULL;
3977 TRACE("(%p, %s)\n", iface, debugstr_guid(rfid));
3979 hr = get_known_folder_registry_path(rfid, NULL, &registryPath);
3981 if(SUCCEEDED(hr))
3982 hr = HRESULT_FROM_WIN32(SHDeleteKeyW(HKEY_LOCAL_MACHINE, registryPath));
3984 HeapFree(GetProcessHeap(), 0, registryPath);
3985 return hr;
3988 static HRESULT WINAPI foldermanager_FindFolderFromPath(
3989 IKnownFolderManager *iface,
3990 LPCWSTR pszPath,
3991 FFFP_MODE mode,
3992 IKnownFolder **ppkf)
3994 FIXME("%s, 0x%08x, %p\n", debugstr_w(pszPath), mode, ppkf);
3995 return E_NOTIMPL;
3998 static HRESULT WINAPI foldermanager_FindFolderFromIDList(
3999 IKnownFolderManager *iface,
4000 PCIDLIST_ABSOLUTE pidl,
4001 IKnownFolder **ppkf)
4003 FIXME("%p, %p\n", pidl, ppkf);
4004 return E_NOTIMPL;
4007 static HRESULT WINAPI foldermanager_Redirect(
4008 IKnownFolderManager *iface,
4009 REFKNOWNFOLDERID rfid,
4010 HWND hwnd,
4011 KF_REDIRECT_FLAGS flags,
4012 LPCWSTR pszTargetPath,
4013 UINT cFolders,
4014 KNOWNFOLDERID const *pExclusion,
4015 LPWSTR *ppszError)
4017 return redirect_known_folder(rfid, hwnd, flags, pszTargetPath, cFolders, pExclusion, ppszError);
4020 static const struct IKnownFolderManagerVtbl foldermanager_vtbl =
4022 foldermanager_QueryInterface,
4023 foldermanager_AddRef,
4024 foldermanager_Release,
4025 foldermanager_FolderIdFromCsidl,
4026 foldermanager_FolderIdToCsidl,
4027 foldermanager_GetFolderIds,
4028 foldermanager_GetFolder,
4029 foldermanager_GetFolderByName,
4030 foldermanager_RegisterFolder,
4031 foldermanager_UnregisterFolder,
4032 foldermanager_FindFolderFromPath,
4033 foldermanager_FindFolderFromIDList,
4034 foldermanager_Redirect
4037 static HRESULT foldermanager_create( void **ppv )
4039 UINT i, j;
4040 struct foldermanager *fm;
4042 fm = HeapAlloc( GetProcessHeap(), 0, sizeof(*fm) );
4043 if (!fm) return E_OUTOFMEMORY;
4045 fm->IKnownFolderManager_iface.lpVtbl = &foldermanager_vtbl;
4046 fm->refs = 1;
4047 fm->num_ids = 0;
4049 for (i = 0; i < sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]); i++)
4051 if (!IsEqualGUID( CSIDL_Data[i].id, &GUID_NULL )) fm->num_ids++;
4053 fm->ids = HeapAlloc( GetProcessHeap(), 0, fm->num_ids * sizeof(KNOWNFOLDERID) );
4054 if (!fm->ids)
4056 HeapFree( GetProcessHeap(), 0, fm );
4057 return E_OUTOFMEMORY;
4059 for (i = j = 0; i < sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]); i++)
4061 if (!IsEqualGUID( CSIDL_Data[i].id, &GUID_NULL ))
4063 fm->ids[j] = *CSIDL_Data[i].id;
4064 j++;
4067 TRACE("found %u known folders\n", fm->num_ids);
4068 *ppv = &fm->IKnownFolderManager_iface;
4070 TRACE("returning iface %p\n", *ppv);
4071 return S_OK;
4074 HRESULT WINAPI KnownFolderManager_Constructor( IUnknown *punk, REFIID riid, void **ppv )
4076 TRACE("%p, %s, %p\n", punk, debugstr_guid(riid), ppv);
4078 if (!ppv)
4079 return E_POINTER;
4080 if (punk)
4081 return CLASS_E_NOAGGREGATION;
4083 return foldermanager_create( ppv );
4086 HRESULT WINAPI SHGetKnownFolderIDList(REFKNOWNFOLDERID rfid, DWORD flags, HANDLE token, PIDLIST_ABSOLUTE *pidl)
4088 FIXME("%s, 0x%08x, %p, %p\n", debugstr_guid(rfid), flags, token, pidl);
4089 return E_NOTIMPL;