Assorted spelling fixes.
[wine.git] / dlls / shell32 / shellpath.c
blobe184434bfb77d30d657ed158e089e7f6c48fe057
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 Microsoft_Windows_Start_MenuW[] = {'M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\','S','t','a','r','t',' ','M','e','n','u',0};
816 static const WCHAR MusicW[] = {'M','u','s','i','c','\0'};
817 static const WCHAR Music_PlaylistsW[] = {'M','u','s','i','c','\\','P','l','a','y','l','i','s','t','s','\0'};
818 static const WCHAR Music_Sample_MusicW[] = {'M','u','s','i','c','\\','S','a','m','p','l','e',' ','M','u','s','i','c','\0'};
819 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'};
820 static const WCHAR My_MusicW[] = {'M','y',' ','M','u','s','i','c','\0'};
821 static const WCHAR My_PicturesW[] = {'M','y',' ','P','i','c','t','u','r','e','s','\0'};
822 static const WCHAR My_VideosW[] = {'M','y',' ','V','i','d','e','o','s','\0'};
823 static const WCHAR NetHoodW[] = {'N','e','t','H','o','o','d','\0'};
824 static const WCHAR OEM_LinksW[] = {'O','E','M',' ','L','i','n','k','s','\0'};
825 static const WCHAR PersonalW[] = {'P','e','r','s','o','n','a','l','\0'};
826 static const WCHAR PicturesW[] = {'P','i','c','t','u','r','e','s','\0'};
827 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'};
828 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'};
829 static const WCHAR PrintHoodW[] = {'P','r','i','n','t','H','o','o','d','\0'};
830 static const WCHAR Program_FilesW[] = {'P','r','o','g','r','a','m',' ','F','i','l','e','s','\0'};
831 static const WCHAR ProgramFilesW[] = {'P','r','o','g','r','a','m','F','i','l','e','s','\0'};
832 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'};
833 static const WCHAR Program_Files_x86W[] = {'P','r','o','g','r','a','m',' ','F','i','l','e','s',' ','(','x','8','6',')','\0'};
834 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'};
835 static const WCHAR ProgramFilesCommonW[] = {'P','r','o','g','r','a','m','F','i','l','e','s','C','o','m','m','o','n',0};
836 static const WCHAR ProgramFilesDirW[] = {'P','r','o','g','r','a','m','F','i','l','e','s','D','i','r','\0'};
837 static const WCHAR ProgramFilesDirX86W[] = {'P','r','o','g','r','a','m','F','i','l','e','s','D','i','r',' ','(','x','8','6',')','\0'};
838 static const WCHAR ProgramsW[] = {'P','r','o','g','r','a','m','s','\0'};
839 static const WCHAR RecentW[] = {'R','e','c','e','n','t','\0'};
840 static const WCHAR ResourcesW[] = {'R','e','s','o','u','r','c','e','s','\0'};
841 static const WCHAR Saved_GamesW[] = {'S','a','v','e','d',' ','G','a','m','e','s','\0'};
842 static const WCHAR SearchesW[] = {'S','e','a','r','c','h','e','s','\0'};
843 static const WCHAR SendToW[] = {'S','e','n','d','T','o','\0'};
844 static const WCHAR StartUpW[] = {'S','t','a','r','t','U','p','\0'};
845 static const WCHAR Start_MenuW[] = {'S','t','a','r','t',' ','M','e','n','u','\0'};
846 static const WCHAR Start_Menu_ProgramsW[] = {'S','t','a','r','t',' ','M','e','n','u','\\','P','r','o','g','r','a','m','s','\0'};
847 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'};
848 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'};
849 static const WCHAR SystemW[] = {'S','y','s','t','e','m',0};
850 static const WCHAR TemplatesW[] = {'T','e','m','p','l','a','t','e','s','\0'};
851 static const WCHAR UsersW[] = {'U','s','e','r','s','\0'};
852 static const WCHAR UsersPublicW[] = {'U','s','e','r','s','\\','P','u','b','l','i','c','\0'};
853 static const WCHAR VideosW[] = {'V','i','d','e','o','s','\0'};
854 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'};
855 static const WCHAR WindowsW[] = {'W','i','n','d','o','w','s',0};
856 static const WCHAR DefaultW[] = {'.','D','e','f','a','u','l','t','\0'};
857 static const WCHAR AllUsersProfileW[] = {'%','A','L','L','U','S','E','R','S','P','R','O','F','I','L','E','%','\0'};
858 static const WCHAR UserProfileW[] = {'%','U','S','E','R','P','R','O','F','I','L','E','%','\0'};
859 static const WCHAR SystemDriveW[] = {'%','S','y','s','t','e','m','D','r','i','v','e','%','\0'};
860 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};
861 static const WCHAR ProfilesDirectoryW[] = {'P','r','o','f','i','l','e','s','D','i','r','e','c','t','o','r','y',0};
862 static const WCHAR AllUsersProfileValueW[] = {'A','l','l','U','s','e','r','s','P','r','o','f','i','l','e','\0'};
863 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'};
864 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'};
865 static const WCHAR szDefaultProfileDirW[] = {'u','s','e','r','s',0};
866 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'};
867 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};
868 static const WCHAR AllUsersW[] = {'P','u','b','l','i','c',0};
870 typedef enum _CSIDL_Type {
871 CSIDL_Type_User,
872 CSIDL_Type_AllUsers,
873 CSIDL_Type_CurrVer,
874 CSIDL_Type_Disallowed,
875 CSIDL_Type_NonExistent,
876 CSIDL_Type_WindowsPath,
877 CSIDL_Type_SystemPath,
878 CSIDL_Type_SystemX86Path,
879 } CSIDL_Type;
881 #define CSIDL_CONTACTS 0x0043
882 #define CSIDL_DOWNLOADS 0x0047
883 #define CSIDL_LINKS 0x004d
884 #define CSIDL_APPDATA_LOCALLOW 0x004e
885 #define CSIDL_SAVED_GAMES 0x0062
886 #define CSIDL_SEARCHES 0x0063
888 typedef struct
890 const KNOWNFOLDERID *id;
891 CSIDL_Type type;
892 LPCWSTR szValueName;
893 LPCWSTR szDefaultPath; /* fallback string or resource ID */
895 /* KNOWNFOLDER_DEFINITION fields */
896 KF_CATEGORY category;
897 const WCHAR *pszName;
898 const WCHAR *pszDescription;
899 const KNOWNFOLDERID *fidParent;
900 const WCHAR *pszRelativePath;
901 const WCHAR *pszParsingName;
902 const WCHAR *pszTooltip;
903 const WCHAR *pszLocalizedName;
904 const WCHAR *pszIcon;
905 const WCHAR *pszSecurity;
906 DWORD dwAttributes;
907 KF_DEFINITION_FLAGS kfdFlags;
908 const FOLDERTYPEID *ftidType;
909 } CSIDL_DATA;
911 static const CSIDL_DATA CSIDL_Data[] =
913 { /* 0x00 - CSIDL_DESKTOP */
914 &FOLDERID_Desktop,
915 CSIDL_Type_User,
916 DesktopW,
917 MAKEINTRESOURCEW(IDS_DESKTOPDIRECTORY),
919 KF_CATEGORY_PERUSER, /* category */
920 DesktopW, /* name */
921 NULL, /* description */
922 &GUID_NULL, /* parent */
923 DesktopW, /* relative path */
924 NULL, /* parsing */
925 NULL, /* tooltip */
926 NULL, /* localized */
927 NULL, /* icon */
928 NULL, /* security */
929 FILE_ATTRIBUTE_READONLY, /* attributes */
930 0, /* flags */
931 &GUID_NULL /* typeid */
933 { /* 0x01 - CSIDL_INTERNET */
934 &FOLDERID_InternetFolder,
935 CSIDL_Type_Disallowed,
936 NULL,
937 NULL
939 { /* 0x02 - CSIDL_PROGRAMS */
940 &FOLDERID_Programs,
941 CSIDL_Type_User,
942 ProgramsW,
943 Start_Menu_ProgramsW
945 { /* 0x03 - CSIDL_CONTROLS (.CPL files) */
946 &FOLDERID_ControlPanelFolder,
947 CSIDL_Type_SystemPath,
948 NULL,
949 NULL
951 { /* 0x04 - CSIDL_PRINTERS */
952 &FOLDERID_PrintersFolder,
953 CSIDL_Type_SystemPath,
954 NULL,
955 NULL
957 { /* 0x05 - CSIDL_PERSONAL */
958 &FOLDERID_Documents,
959 CSIDL_Type_User,
960 PersonalW,
961 MAKEINTRESOURCEW(IDS_PERSONAL)
963 { /* 0x06 - CSIDL_FAVORITES */
964 &FOLDERID_Favorites,
965 CSIDL_Type_User,
966 FavoritesW,
967 FavoritesW
969 { /* 0x07 - CSIDL_STARTUP */
970 &FOLDERID_Startup,
971 CSIDL_Type_User,
972 StartUpW,
973 Start_Menu_StartupW
975 { /* 0x08 - CSIDL_RECENT */
976 &FOLDERID_Recent,
977 CSIDL_Type_User,
978 RecentW,
979 RecentW
981 { /* 0x09 - CSIDL_SENDTO */
982 &FOLDERID_SendTo,
983 CSIDL_Type_User,
984 SendToW,
985 SendToW
987 { /* 0x0a - CSIDL_BITBUCKET - Recycle Bin */
988 &FOLDERID_RecycleBinFolder,
989 CSIDL_Type_Disallowed,
990 NULL,
991 NULL,
993 { /* 0x0b - CSIDL_STARTMENU */
994 &FOLDERID_StartMenu,
995 CSIDL_Type_User,
996 Start_MenuW,
997 Start_MenuW
999 { /* 0x0c - CSIDL_MYDOCUMENTS */
1000 &GUID_NULL,
1001 CSIDL_Type_Disallowed, /* matches WinXP--can't get its path */
1002 NULL,
1003 NULL
1005 { /* 0x0d - CSIDL_MYMUSIC */
1006 &FOLDERID_Music,
1007 CSIDL_Type_User,
1008 My_MusicW,
1009 MAKEINTRESOURCEW(IDS_MYMUSIC)
1011 { /* 0x0e - CSIDL_MYVIDEO */
1012 &FOLDERID_Videos,
1013 CSIDL_Type_User,
1014 My_VideosW,
1015 MAKEINTRESOURCEW(IDS_MYVIDEOS)
1017 { /* 0x0f - unassigned */
1018 &GUID_NULL,
1019 CSIDL_Type_Disallowed,
1020 NULL,
1021 NULL,
1023 { /* 0x10 - CSIDL_DESKTOPDIRECTORY */
1024 &FOLDERID_Desktop,
1025 CSIDL_Type_User,
1026 DesktopW,
1027 MAKEINTRESOURCEW(IDS_DESKTOPDIRECTORY)
1029 { /* 0x11 - CSIDL_DRIVES */
1030 &FOLDERID_ComputerFolder,
1031 CSIDL_Type_Disallowed,
1032 NULL,
1033 NULL,
1035 { /* 0x12 - CSIDL_NETWORK */
1036 &FOLDERID_NetworkFolder,
1037 CSIDL_Type_Disallowed,
1038 NULL,
1039 NULL,
1041 { /* 0x13 - CSIDL_NETHOOD */
1042 &FOLDERID_NetHood,
1043 CSIDL_Type_User,
1044 NetHoodW,
1045 NetHoodW
1047 { /* 0x14 - CSIDL_FONTS */
1048 &FOLDERID_Fonts,
1049 CSIDL_Type_WindowsPath,
1050 FontsW,
1051 FontsW,
1053 KF_CATEGORY_FIXED, /* category */
1054 FontsW, /* name */
1055 NULL, /* description */
1056 &GUID_NULL, /* parent */
1057 NULL, /* relative path */
1058 NULL, /* parsing */
1059 NULL, /* tooltip */
1060 NULL, /* localized */
1061 NULL, /* icon */
1062 NULL, /* security */
1063 0, /* attributes */
1064 0, /* flags */
1065 &FOLDERID_Windows/* typeid */
1067 { /* 0x15 - CSIDL_TEMPLATES */
1068 &FOLDERID_Templates,
1069 CSIDL_Type_User,
1070 TemplatesW,
1071 TemplatesW
1073 { /* 0x16 - CSIDL_COMMON_STARTMENU */
1074 &FOLDERID_CommonStartMenu,
1075 CSIDL_Type_AllUsers,
1076 Common_Start_MenuW,
1077 Start_MenuW,
1079 KF_CATEGORY_COMMON, /* category */
1080 Common_Start_MenuW, /* name */
1081 NULL, /* description */
1082 &FOLDERID_ProgramData, /* parent */
1083 Microsoft_Windows_Start_MenuW, /* relative path */
1084 NULL, /* parsing */
1085 NULL, /* tooltip */
1086 NULL, /* localized */
1087 NULL, /* icon */
1088 NULL, /* security */
1089 FILE_ATTRIBUTE_READONLY, /* attributes */
1090 0, /* flags */
1091 &GUID_NULL /* typeid */
1093 { /* 0x17 - CSIDL_COMMON_PROGRAMS */
1094 &FOLDERID_CommonPrograms,
1095 CSIDL_Type_AllUsers,
1096 Common_ProgramsW,
1097 Start_Menu_ProgramsW,
1099 KF_CATEGORY_COMMON, /* category */
1100 Common_ProgramsW, /* name */
1101 NULL, /* description */
1102 &FOLDERID_CommonStartMenu, /* parent */
1103 ProgramsW, /* relative path */
1104 NULL, /* parsing */
1105 NULL, /* tooltip */
1106 NULL, /* localized */
1107 NULL, /* icon */
1108 NULL, /* security */
1109 FILE_ATTRIBUTE_READONLY, /* attributes */
1110 0, /* flags */
1111 &GUID_NULL /* typeid */
1113 { /* 0x18 - CSIDL_COMMON_STARTUP */
1114 &FOLDERID_CommonStartup,
1115 CSIDL_Type_AllUsers,
1116 Common_StartUpW,
1117 Start_Menu_StartupW
1119 { /* 0x19 - CSIDL_COMMON_DESKTOPDIRECTORY */
1120 &FOLDERID_PublicDesktop,
1121 CSIDL_Type_AllUsers,
1122 Common_DesktopW,
1123 MAKEINTRESOURCEW(IDS_DESKTOPDIRECTORY)
1125 { /* 0x1a - CSIDL_APPDATA */
1126 &FOLDERID_RoamingAppData,
1127 CSIDL_Type_User,
1128 AppDataW,
1129 Application_DataW
1131 { /* 0x1b - CSIDL_PRINTHOOD */
1132 &FOLDERID_PrintHood,
1133 CSIDL_Type_User,
1134 PrintHoodW,
1135 PrintHoodW
1137 { /* 0x1c - CSIDL_LOCAL_APPDATA */
1138 &FOLDERID_LocalAppData,
1139 CSIDL_Type_User,
1140 Local_AppDataW,
1141 Local_Settings_Application_DataW
1143 { /* 0x1d - CSIDL_ALTSTARTUP */
1144 &GUID_NULL,
1145 CSIDL_Type_NonExistent,
1146 NULL,
1147 NULL
1149 { /* 0x1e - CSIDL_COMMON_ALTSTARTUP */
1150 &GUID_NULL,
1151 CSIDL_Type_NonExistent,
1152 NULL,
1153 NULL
1155 { /* 0x1f - CSIDL_COMMON_FAVORITES */
1156 &FOLDERID_Favorites,
1157 CSIDL_Type_AllUsers,
1158 Common_FavoritesW,
1159 FavoritesW
1161 { /* 0x20 - CSIDL_INTERNET_CACHE */
1162 &FOLDERID_InternetCache,
1163 CSIDL_Type_User,
1164 CacheW,
1165 Local_Settings_Temporary_Internet_FilesW
1167 { /* 0x21 - CSIDL_COOKIES */
1168 &FOLDERID_Cookies,
1169 CSIDL_Type_User,
1170 CookiesW,
1171 CookiesW
1173 { /* 0x22 - CSIDL_HISTORY */
1174 &FOLDERID_History,
1175 CSIDL_Type_User,
1176 HistoryW,
1177 Local_Settings_HistoryW
1179 { /* 0x23 - CSIDL_COMMON_APPDATA */
1180 &FOLDERID_ProgramData,
1181 CSIDL_Type_AllUsers,
1182 Common_AppDataW,
1183 Application_DataW,
1185 KF_CATEGORY_FIXED, /* category */
1186 Common_AppDataW, /* name */
1187 NULL, /* description */
1188 &GUID_NULL, /* parent */
1189 NULL, /* relative path */
1190 NULL, /* parsing */
1191 NULL, /* tooltip */
1192 NULL, /* localized */
1193 NULL, /* icon */
1194 NULL, /* security */
1195 0, /* attributes */
1196 0, /* flags */
1197 &GUID_NULL /* typeid */
1199 { /* 0x24 - CSIDL_WINDOWS */
1200 &FOLDERID_Windows,
1201 CSIDL_Type_WindowsPath,
1202 NULL,
1203 NULL,
1205 KF_CATEGORY_FIXED, /* category */
1206 WindowsW, /* name */
1207 NULL, /* description */
1208 &GUID_NULL, /* parent */
1209 NULL, /* relative path */
1210 NULL, /* parsing */
1211 NULL, /* tooltip */
1212 NULL, /* localized */
1213 NULL, /* icon */
1214 NULL, /* security */
1215 0, /* attributes */
1216 0, /* flags */
1217 &GUID_NULL /* typeid */
1219 { /* 0x25 - CSIDL_SYSTEM */
1220 &FOLDERID_System,
1221 CSIDL_Type_SystemPath,
1222 NULL,
1223 NULL,
1225 KF_CATEGORY_FIXED, /* category */
1226 SystemW, /* name */
1227 NULL, /* description */
1228 &GUID_NULL, /* parent */
1229 NULL, /* relative path */
1230 NULL, /* parsing */
1231 NULL, /* tooltip */
1232 NULL, /* localized */
1233 NULL, /* icon */
1234 NULL, /* security */
1235 0, /* attributes */
1236 0, /* flags */
1237 &GUID_NULL /* typeid */
1239 { /* 0x26 - CSIDL_PROGRAM_FILES */
1240 &FOLDERID_ProgramFiles,
1241 CSIDL_Type_CurrVer,
1242 ProgramFilesDirW,
1243 Program_FilesW,
1245 KF_CATEGORY_FIXED, /* category */
1246 ProgramFilesW, /* name */
1247 NULL, /* description */
1248 &GUID_NULL, /* parent */
1249 NULL, /* relative path */
1250 NULL, /* parsing */
1251 NULL, /* tooltip */
1252 NULL, /* localized */
1253 NULL, /* icon */
1254 NULL, /* security */
1255 FILE_ATTRIBUTE_READONLY, /* attributes */
1256 0, /* flags */
1257 &GUID_NULL /* typeid */
1259 { /* 0x27 - CSIDL_MYPICTURES */
1260 &FOLDERID_Pictures,
1261 CSIDL_Type_User,
1262 My_PicturesW,
1263 MAKEINTRESOURCEW(IDS_MYPICTURES)
1265 { /* 0x28 - CSIDL_PROFILE */
1266 &FOLDERID_Profile,
1267 CSIDL_Type_User,
1268 NULL,
1269 NULL
1271 { /* 0x29 - CSIDL_SYSTEMX86 */
1272 &FOLDERID_SystemX86,
1273 CSIDL_Type_SystemX86Path,
1274 NULL,
1275 NULL
1277 { /* 0x2a - CSIDL_PROGRAM_FILESX86 */
1278 &FOLDERID_ProgramFilesX86,
1279 CSIDL_Type_CurrVer,
1280 ProgramFilesDirX86W,
1281 Program_Files_x86W
1283 { /* 0x2b - CSIDL_PROGRAM_FILES_COMMON */
1284 &FOLDERID_ProgramFilesCommon,
1285 CSIDL_Type_CurrVer,
1286 CommonFilesDirW,
1287 Program_Files_Common_FilesW,
1289 KF_CATEGORY_FIXED, /* category */
1290 ProgramFilesCommonW, /* name */
1291 NULL, /* description */
1292 &GUID_NULL, /* parent */
1293 NULL, /* relative path */
1294 NULL, /* parsing */
1295 NULL, /* tooltip */
1296 NULL, /* localized */
1297 NULL, /* icon */
1298 NULL, /* security */
1299 0, /* attributes */
1300 0, /* flags */
1301 &GUID_NULL /* typeid */
1303 { /* 0x2c - CSIDL_PROGRAM_FILES_COMMONX86 */
1304 &FOLDERID_ProgramFilesCommonX86,
1305 CSIDL_Type_CurrVer,
1306 CommonFilesDirX86W,
1307 Program_Files_x86_Common_FilesW
1309 { /* 0x2d - CSIDL_COMMON_TEMPLATES */
1310 &FOLDERID_CommonTemplates,
1311 CSIDL_Type_AllUsers,
1312 Common_TemplatesW,
1313 TemplatesW
1315 { /* 0x2e - CSIDL_COMMON_DOCUMENTS */
1316 &FOLDERID_PublicDocuments,
1317 CSIDL_Type_AllUsers,
1318 Common_DocumentsW,
1319 DocumentsW
1321 { /* 0x2f - CSIDL_COMMON_ADMINTOOLS */
1322 &FOLDERID_CommonAdminTools,
1323 CSIDL_Type_AllUsers,
1324 Common_Administrative_ToolsW,
1325 Start_Menu_Admin_ToolsW
1327 { /* 0x30 - CSIDL_ADMINTOOLS */
1328 &FOLDERID_AdminTools,
1329 CSIDL_Type_User,
1330 Administrative_ToolsW,
1331 Start_Menu_Admin_ToolsW
1333 { /* 0x31 - CSIDL_CONNECTIONS */
1334 &FOLDERID_ConnectionsFolder,
1335 CSIDL_Type_Disallowed,
1336 NULL,
1337 NULL
1339 { /* 0x32 - unassigned */
1340 &GUID_NULL,
1341 CSIDL_Type_Disallowed,
1342 NULL,
1343 NULL
1345 { /* 0x33 - unassigned */
1346 &GUID_NULL,
1347 CSIDL_Type_Disallowed,
1348 NULL,
1349 NULL
1351 { /* 0x34 - unassigned */
1352 &GUID_NULL,
1353 CSIDL_Type_Disallowed,
1354 NULL,
1355 NULL
1357 { /* 0x35 - CSIDL_COMMON_MUSIC */
1358 &FOLDERID_PublicMusic,
1359 CSIDL_Type_AllUsers,
1360 CommonMusicW,
1361 MusicW
1363 { /* 0x36 - CSIDL_COMMON_PICTURES */
1364 &FOLDERID_PublicPictures,
1365 CSIDL_Type_AllUsers,
1366 CommonPicturesW,
1367 PicturesW
1369 { /* 0x37 - CSIDL_COMMON_VIDEO */
1370 &FOLDERID_PublicVideos,
1371 CSIDL_Type_AllUsers,
1372 CommonVideoW,
1373 VideosW
1375 { /* 0x38 - CSIDL_RESOURCES */
1376 &FOLDERID_ResourceDir,
1377 CSIDL_Type_WindowsPath,
1378 NULL,
1379 ResourcesW
1381 { /* 0x39 - CSIDL_RESOURCES_LOCALIZED */
1382 &FOLDERID_LocalizedResourcesDir,
1383 CSIDL_Type_NonExistent,
1384 NULL,
1385 NULL
1387 { /* 0x3a - CSIDL_COMMON_OEM_LINKS */
1388 &FOLDERID_CommonOEMLinks,
1389 CSIDL_Type_AllUsers,
1390 NULL,
1391 OEM_LinksW
1393 { /* 0x3b - CSIDL_CDBURN_AREA */
1394 &FOLDERID_CDBurning,
1395 CSIDL_Type_User,
1396 CD_BurningW,
1397 Local_Settings_CD_BurningW
1399 { /* 0x3c unassigned */
1400 &GUID_NULL,
1401 CSIDL_Type_Disallowed,
1402 NULL,
1403 NULL
1405 { /* 0x3d - CSIDL_COMPUTERSNEARME */
1406 &GUID_NULL,
1407 CSIDL_Type_Disallowed, /* FIXME */
1408 NULL,
1409 NULL
1411 { /* 0x3e - CSIDL_PROFILES */
1412 &GUID_NULL,
1413 CSIDL_Type_Disallowed, /* oddly, this matches WinXP */
1414 NULL,
1415 NULL
1417 { /* 0x3f */
1418 &FOLDERID_AddNewPrograms,
1419 CSIDL_Type_Disallowed,
1420 NULL,
1421 NULL
1423 { /* 0x40 */
1424 &FOLDERID_AppUpdates,
1425 CSIDL_Type_Disallowed,
1426 NULL,
1427 NULL
1429 { /* 0x41 */
1430 &FOLDERID_ChangeRemovePrograms,
1431 CSIDL_Type_Disallowed,
1432 NULL,
1433 NULL
1435 { /* 0x42 */
1436 &FOLDERID_ConflictFolder,
1437 CSIDL_Type_Disallowed,
1438 NULL,
1439 NULL
1441 { /* 0x43 - CSIDL_CONTACTS */
1442 &FOLDERID_Contacts,
1443 CSIDL_Type_User,
1444 NULL,
1445 ContactsW
1447 { /* 0x44 */
1448 &FOLDERID_DeviceMetadataStore,
1449 CSIDL_Type_Disallowed, /* FIXME */
1450 NULL,
1451 NULL
1453 { /* 0x45 */
1454 &GUID_NULL,
1455 CSIDL_Type_User,
1456 NULL,
1457 DocumentsW
1459 { /* 0x46 */
1460 &FOLDERID_DocumentsLibrary,
1461 CSIDL_Type_Disallowed, /* FIXME */
1462 NULL,
1463 NULL
1465 { /* 0x47 - CSIDL_DOWNLOADS */
1466 &FOLDERID_Downloads,
1467 CSIDL_Type_User,
1468 NULL,
1469 DownloadsW
1471 { /* 0x48 */
1472 &FOLDERID_Games,
1473 CSIDL_Type_Disallowed,
1474 NULL,
1475 NULL
1477 { /* 0x49 */
1478 &FOLDERID_GameTasks,
1479 CSIDL_Type_Disallowed, /* FIXME */
1480 NULL,
1481 NULL
1483 { /* 0x4a */
1484 &FOLDERID_HomeGroup,
1485 CSIDL_Type_Disallowed,
1486 NULL,
1487 NULL
1489 { /* 0x4b */
1490 &FOLDERID_ImplicitAppShortcuts,
1491 CSIDL_Type_Disallowed, /* FIXME */
1492 NULL,
1493 NULL
1495 { /* 0x4c */
1496 &FOLDERID_Libraries,
1497 CSIDL_Type_Disallowed, /* FIXME */
1498 NULL,
1499 NULL
1501 { /* 0x4d - CSIDL_LINKS */
1502 &FOLDERID_Links,
1503 CSIDL_Type_User,
1504 NULL,
1505 LinksW
1507 { /* 0x4e - CSIDL_APPDATA_LOCALLOW */
1508 &FOLDERID_LocalAppDataLow,
1509 CSIDL_Type_User,
1510 NULL,
1511 AppData_LocalLowW
1513 { /* 0x4f */
1514 &FOLDERID_MusicLibrary,
1515 CSIDL_Type_Disallowed, /* FIXME */
1516 NULL,
1517 NULL
1519 { /* 0x50 */
1520 &FOLDERID_OriginalImages,
1521 CSIDL_Type_Disallowed, /* FIXME */
1522 NULL,
1523 NULL
1525 { /* 0x51 */
1526 &FOLDERID_PhotoAlbums,
1527 CSIDL_Type_User,
1528 NULL,
1529 Pictures_Slide_ShowsW
1531 { /* 0x52 */
1532 &FOLDERID_PicturesLibrary,
1533 CSIDL_Type_Disallowed, /* FIXME */
1534 NULL,
1535 NULL
1537 { /* 0x53 */
1538 &FOLDERID_Playlists,
1539 CSIDL_Type_User,
1540 NULL,
1541 Music_PlaylistsW
1543 { /* 0x54 */
1544 &FOLDERID_ProgramFilesX64,
1545 CSIDL_Type_NonExistent,
1546 NULL,
1547 NULL
1549 { /* 0x55 */
1550 &FOLDERID_ProgramFilesCommonX64,
1551 CSIDL_Type_NonExistent,
1552 NULL,
1553 NULL
1555 { /* 0x56 */
1556 &FOLDERID_Public,
1557 CSIDL_Type_CurrVer, /* FIXME */
1558 NULL,
1559 UsersPublicW
1561 { /* 0x57 */
1562 &FOLDERID_PublicDownloads,
1563 CSIDL_Type_AllUsers,
1564 NULL,
1565 DownloadsW
1567 { /* 0x58 */
1568 &FOLDERID_PublicGameTasks,
1569 CSIDL_Type_AllUsers,
1570 NULL,
1571 Microsoft_Windows_GameExplorerW
1573 { /* 0x59 */
1574 &FOLDERID_PublicLibraries,
1575 CSIDL_Type_AllUsers,
1576 NULL,
1577 Microsoft_Windows_LibrariesW
1579 { /* 0x5a */
1580 &FOLDERID_PublicRingtones,
1581 CSIDL_Type_AllUsers,
1582 NULL,
1583 Microsoft_Windows_RingtonesW
1585 { /* 0x5b */
1586 &FOLDERID_QuickLaunch,
1587 CSIDL_Type_Disallowed, /* FIXME */
1588 NULL,
1589 NULL
1591 { /* 0x5c */
1592 &FOLDERID_RecordedTVLibrary,
1593 CSIDL_Type_Disallowed, /* FIXME */
1594 NULL,
1595 NULL
1597 { /* 0x5d */
1598 &FOLDERID_Ringtones,
1599 CSIDL_Type_Disallowed, /* FIXME */
1600 NULL,
1601 NULL
1603 { /* 0x5e */
1604 &FOLDERID_SampleMusic,
1605 CSIDL_Type_AllUsers,
1606 NULL,
1607 Music_Sample_MusicW
1609 { /* 0x5f */
1610 &FOLDERID_SamplePictures,
1611 CSIDL_Type_AllUsers,
1612 NULL,
1613 Pictures_Sample_PicturesW
1615 { /* 0x60 */
1616 &FOLDERID_SamplePlaylists,
1617 CSIDL_Type_AllUsers,
1618 NULL,
1619 Music_Sample_PlaylistsW
1621 { /* 0x61 */
1622 &FOLDERID_SampleVideos,
1623 CSIDL_Type_AllUsers,
1624 NULL,
1625 Videos_Sample_VideosW
1627 { /* 0x62 - CSIDL_SAVED_GAMES */
1628 &FOLDERID_SavedGames,
1629 CSIDL_Type_User,
1630 NULL,
1631 Saved_GamesW
1633 { /* 0x63 - CSIDL_SEARCHES */
1634 &FOLDERID_SavedSearches,
1635 CSIDL_Type_User,
1636 NULL,
1637 SearchesW
1639 { /* 0x64 */
1640 &FOLDERID_SEARCH_CSC,
1641 CSIDL_Type_Disallowed,
1642 NULL,
1643 NULL
1645 { /* 0x65 */
1646 &FOLDERID_SEARCH_MAPI,
1647 CSIDL_Type_Disallowed,
1648 NULL,
1649 NULL
1651 { /* 0x66 */
1652 &FOLDERID_SearchHome,
1653 CSIDL_Type_Disallowed,
1654 NULL,
1655 NULL
1657 { /* 0x67 */
1658 &FOLDERID_SidebarDefaultParts,
1659 CSIDL_Type_Disallowed, /* FIXME */
1660 NULL,
1661 NULL
1663 { /* 0x68 */
1664 &FOLDERID_SidebarParts,
1665 CSIDL_Type_Disallowed, /* FIXME */
1666 NULL,
1667 NULL
1669 { /* 0x69 */
1670 &FOLDERID_SyncManagerFolder,
1671 CSIDL_Type_Disallowed,
1672 NULL,
1673 NULL
1675 { /* 0x6a */
1676 &FOLDERID_SyncResultsFolder,
1677 CSIDL_Type_Disallowed,
1678 NULL,
1679 NULL
1681 { /* 0x6b */
1682 &FOLDERID_SyncSetupFolder,
1683 CSIDL_Type_Disallowed,
1684 NULL,
1685 NULL
1687 { /* 0x6c */
1688 &FOLDERID_UserPinned,
1689 CSIDL_Type_Disallowed, /* FIXME */
1690 NULL,
1691 NULL
1693 { /* 0x6d */
1694 &FOLDERID_UserProfiles,
1695 CSIDL_Type_CurrVer,
1696 UsersW,
1697 UsersW
1699 { /* 0x6e */
1700 &FOLDERID_UserProgramFiles,
1701 CSIDL_Type_Disallowed, /* FIXME */
1702 NULL,
1703 NULL
1705 { /* 0x6f */
1706 &FOLDERID_UserProgramFilesCommon,
1707 CSIDL_Type_Disallowed, /* FIXME */
1708 NULL,
1709 NULL
1711 { /* 0x70 */
1712 &FOLDERID_UsersFiles,
1713 CSIDL_Type_Disallowed,
1714 NULL,
1715 NULL
1717 { /* 0x71 */
1718 &FOLDERID_UsersLibraries,
1719 CSIDL_Type_Disallowed,
1720 NULL,
1721 NULL
1723 { /* 0x72 */
1724 &FOLDERID_VideosLibrary,
1725 CSIDL_Type_Disallowed, /* FIXME */
1726 NULL,
1727 NULL
1731 static HRESULT _SHExpandEnvironmentStrings(LPCWSTR szSrc, LPWSTR szDest);
1733 /* Gets the value named value from the registry key
1734 * rootKey\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
1735 * (or from rootKey\userPrefix\... if userPrefix is not NULL) into path, which
1736 * is assumed to be MAX_PATH WCHARs in length.
1737 * If it exists, expands the value and writes the expanded value to
1738 * rootKey\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders
1739 * Returns successful error code if the value was retrieved from the registry,
1740 * and a failure otherwise.
1742 static HRESULT _SHGetUserShellFolderPath(HKEY rootKey, LPCWSTR userPrefix,
1743 LPCWSTR value, LPWSTR path)
1745 HRESULT hr;
1746 WCHAR shellFolderPath[MAX_PATH], userShellFolderPath[MAX_PATH];
1747 LPCWSTR pShellFolderPath, pUserShellFolderPath;
1748 DWORD dwType, dwPathLen = MAX_PATH;
1749 HKEY userShellFolderKey, shellFolderKey;
1751 TRACE("%p,%s,%s,%p\n",rootKey, debugstr_w(userPrefix), debugstr_w(value),
1752 path);
1754 if (userPrefix)
1756 strcpyW(shellFolderPath, userPrefix);
1757 PathAddBackslashW(shellFolderPath);
1758 strcatW(shellFolderPath, szSHFolders);
1759 pShellFolderPath = shellFolderPath;
1760 strcpyW(userShellFolderPath, userPrefix);
1761 PathAddBackslashW(userShellFolderPath);
1762 strcatW(userShellFolderPath, szSHUserFolders);
1763 pUserShellFolderPath = userShellFolderPath;
1765 else
1767 pUserShellFolderPath = szSHUserFolders;
1768 pShellFolderPath = szSHFolders;
1771 if (RegCreateKeyW(rootKey, pShellFolderPath, &shellFolderKey))
1773 TRACE("Failed to create %s\n", debugstr_w(pShellFolderPath));
1774 return E_FAIL;
1776 if (RegCreateKeyW(rootKey, pUserShellFolderPath, &userShellFolderKey))
1778 TRACE("Failed to create %s\n",
1779 debugstr_w(pUserShellFolderPath));
1780 RegCloseKey(shellFolderKey);
1781 return E_FAIL;
1784 if (!RegQueryValueExW(userShellFolderKey, value, NULL, &dwType,
1785 (LPBYTE)path, &dwPathLen) && (dwType == REG_EXPAND_SZ || dwType == REG_SZ))
1787 LONG ret;
1789 path[dwPathLen / sizeof(WCHAR)] = '\0';
1790 if (dwType == REG_EXPAND_SZ && path[0] == '%')
1792 WCHAR szTemp[MAX_PATH];
1794 _SHExpandEnvironmentStrings(path, szTemp);
1795 lstrcpynW(path, szTemp, MAX_PATH);
1797 ret = RegSetValueExW(shellFolderKey, value, 0, REG_SZ, (LPBYTE)path,
1798 (strlenW(path) + 1) * sizeof(WCHAR));
1799 if (ret != ERROR_SUCCESS)
1800 hr = HRESULT_FROM_WIN32(ret);
1801 else
1802 hr = S_OK;
1804 else
1805 hr = E_FAIL;
1806 RegCloseKey(shellFolderKey);
1807 RegCloseKey(userShellFolderKey);
1808 TRACE("returning 0x%08x\n", hr);
1809 return hr;
1812 /* Gets a 'semi-expanded' default value of the CSIDL with index folder into
1813 * pszPath, based on the entries in CSIDL_Data. By semi-expanded, I mean:
1814 * - The entry's szDefaultPath may be either a string value or an integer
1815 * resource identifier. In the latter case, the string value of the resource
1816 * is written.
1817 * - Depending on the entry's type, the path may begin with an (unexpanded)
1818 * environment variable name. The caller is responsible for expanding
1819 * environment strings if so desired.
1820 * The types that are prepended with environment variables are:
1821 * CSIDL_Type_User: %USERPROFILE%
1822 * CSIDL_Type_AllUsers: %ALLUSERSPROFILE%
1823 * CSIDL_Type_CurrVer: %SystemDrive%
1824 * (Others might make sense too, but as yet are unneeded.)
1826 static HRESULT _SHGetDefaultValue(BYTE folder, LPWSTR pszPath)
1828 HRESULT hr;
1829 WCHAR resourcePath[MAX_PATH];
1830 LPCWSTR pDefaultPath = NULL;
1832 TRACE("0x%02x,%p\n", folder, pszPath);
1834 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1835 return E_INVALIDARG;
1836 if (!pszPath)
1837 return E_INVALIDARG;
1839 if (!is_win64)
1841 BOOL is_wow64;
1843 switch (folder)
1845 case CSIDL_PROGRAM_FILES:
1846 case CSIDL_PROGRAM_FILESX86:
1847 IsWow64Process( GetCurrentProcess(), &is_wow64 );
1848 folder = is_wow64 ? CSIDL_PROGRAM_FILESX86 : CSIDL_PROGRAM_FILES;
1849 break;
1850 case CSIDL_PROGRAM_FILES_COMMON:
1851 case CSIDL_PROGRAM_FILES_COMMONX86:
1852 IsWow64Process( GetCurrentProcess(), &is_wow64 );
1853 folder = is_wow64 ? CSIDL_PROGRAM_FILES_COMMONX86 : CSIDL_PROGRAM_FILES_COMMON;
1854 break;
1858 if (CSIDL_Data[folder].szDefaultPath &&
1859 IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath))
1861 if (LoadStringW(shell32_hInstance,
1862 LOWORD(CSIDL_Data[folder].szDefaultPath), resourcePath, MAX_PATH))
1864 hr = S_OK;
1865 pDefaultPath = resourcePath;
1867 else
1869 FIXME("(%d,%s), LoadString failed, missing translation?\n", folder,
1870 debugstr_w(pszPath));
1871 hr = E_FAIL;
1874 else
1876 hr = S_OK;
1877 pDefaultPath = CSIDL_Data[folder].szDefaultPath;
1879 if (SUCCEEDED(hr))
1881 switch (CSIDL_Data[folder].type)
1883 case CSIDL_Type_User:
1884 strcpyW(pszPath, UserProfileW);
1885 break;
1886 case CSIDL_Type_AllUsers:
1887 strcpyW(pszPath, AllUsersProfileW);
1888 break;
1889 case CSIDL_Type_CurrVer:
1890 strcpyW(pszPath, SystemDriveW);
1891 break;
1892 default:
1893 ; /* no corresponding env. var, do nothing */
1895 if (pDefaultPath)
1897 PathAddBackslashW(pszPath);
1898 strcatW(pszPath, pDefaultPath);
1901 TRACE("returning 0x%08x\n", hr);
1902 return hr;
1905 /* Gets the (unexpanded) value of the folder with index folder into pszPath.
1906 * The folder's type is assumed to be CSIDL_Type_CurrVer. Its default value
1907 * can be overridden in the HKLM\\szCurrentVersion key.
1908 * If dwFlags has SHGFP_TYPE_DEFAULT set or if the value isn't overridden in
1909 * the registry, uses _SHGetDefaultValue to get the value.
1911 static HRESULT _SHGetCurrentVersionPath(DWORD dwFlags, BYTE folder,
1912 LPWSTR pszPath)
1914 HRESULT hr;
1916 TRACE("0x%08x,0x%02x,%p\n", dwFlags, folder, pszPath);
1918 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1919 return E_INVALIDARG;
1920 if (CSIDL_Data[folder].type != CSIDL_Type_CurrVer)
1921 return E_INVALIDARG;
1922 if (!pszPath)
1923 return E_INVALIDARG;
1925 if (dwFlags & SHGFP_TYPE_DEFAULT)
1926 hr = _SHGetDefaultValue(folder, pszPath);
1927 else
1929 HKEY hKey;
1931 if (RegCreateKeyW(HKEY_LOCAL_MACHINE, szCurrentVersion, &hKey))
1932 hr = E_FAIL;
1933 else
1935 DWORD dwType, dwPathLen = MAX_PATH * sizeof(WCHAR);
1937 if (RegQueryValueExW(hKey, CSIDL_Data[folder].szValueName, NULL,
1938 &dwType, (LPBYTE)pszPath, &dwPathLen) ||
1939 (dwType != REG_SZ && dwType != REG_EXPAND_SZ))
1941 hr = _SHGetDefaultValue(folder, pszPath);
1942 dwType = REG_EXPAND_SZ;
1943 switch (folder)
1945 case CSIDL_PROGRAM_FILESX86:
1946 case CSIDL_PROGRAM_FILES_COMMONX86:
1947 /* these two should never be set on 32-bit setups */
1948 if (!is_win64)
1950 BOOL is_wow64;
1951 IsWow64Process( GetCurrentProcess(), &is_wow64 );
1952 if (!is_wow64) break;
1954 /* fall through */
1955 default:
1956 RegSetValueExW(hKey, CSIDL_Data[folder].szValueName, 0, dwType,
1957 (LPBYTE)pszPath, (strlenW(pszPath)+1)*sizeof(WCHAR));
1960 else
1962 pszPath[dwPathLen / sizeof(WCHAR)] = '\0';
1963 hr = S_OK;
1965 RegCloseKey(hKey);
1968 TRACE("returning 0x%08x (output path is %s)\n", hr, debugstr_w(pszPath));
1969 return hr;
1972 static LPWSTR _GetUserSidStringFromToken(HANDLE Token)
1974 char InfoBuffer[64];
1975 PTOKEN_USER UserInfo;
1976 DWORD InfoSize;
1977 LPWSTR SidStr;
1979 UserInfo = (PTOKEN_USER) InfoBuffer;
1980 if (! GetTokenInformation(Token, TokenUser, InfoBuffer, sizeof(InfoBuffer),
1981 &InfoSize))
1983 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
1984 return NULL;
1985 UserInfo = HeapAlloc(GetProcessHeap(), 0, InfoSize);
1986 if (UserInfo == NULL)
1987 return NULL;
1988 if (! GetTokenInformation(Token, TokenUser, UserInfo, InfoSize,
1989 &InfoSize))
1991 HeapFree(GetProcessHeap(), 0, UserInfo);
1992 return NULL;
1996 if (! ConvertSidToStringSidW(UserInfo->User.Sid, &SidStr))
1997 SidStr = NULL;
1999 if (UserInfo != (PTOKEN_USER) InfoBuffer)
2000 HeapFree(GetProcessHeap(), 0, UserInfo);
2002 return SidStr;
2005 /* Gets the user's path (unexpanded) for the CSIDL with index folder:
2006 * If SHGFP_TYPE_DEFAULT is set, calls _SHGetDefaultValue for it. Otherwise
2007 * calls _SHGetUserShellFolderPath for it. Where it looks depends on hToken:
2008 * - if hToken is -1, looks in HKEY_USERS\.Default
2009 * - otherwise looks first in HKEY_CURRENT_USER, followed by HKEY_LOCAL_MACHINE
2010 * if HKEY_CURRENT_USER doesn't contain any entries. If both fail, finally
2011 * calls _SHGetDefaultValue for it.
2013 static HRESULT _SHGetUserProfilePath(HANDLE hToken, DWORD dwFlags, BYTE folder,
2014 LPWSTR pszPath)
2016 const WCHAR *szValueName;
2017 WCHAR buffer[40];
2018 HRESULT hr;
2020 TRACE("%p,0x%08x,0x%02x,%p\n", hToken, dwFlags, folder, pszPath);
2022 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
2023 return E_INVALIDARG;
2024 if (CSIDL_Data[folder].type != CSIDL_Type_User)
2025 return E_INVALIDARG;
2026 if (!pszPath)
2027 return E_INVALIDARG;
2029 if (dwFlags & SHGFP_TYPE_DEFAULT)
2031 if (hToken != NULL && hToken != (HANDLE)-1)
2033 FIXME("unsupported for user other than current or default\n");
2034 return E_FAIL;
2036 hr = _SHGetDefaultValue(folder, pszPath);
2038 else
2040 LPCWSTR userPrefix = NULL;
2041 HKEY hRootKey;
2043 if (hToken == (HANDLE)-1)
2045 hRootKey = HKEY_USERS;
2046 userPrefix = DefaultW;
2048 else if (hToken == NULL)
2049 hRootKey = HKEY_CURRENT_USER;
2050 else
2052 hRootKey = HKEY_USERS;
2053 userPrefix = _GetUserSidStringFromToken(hToken);
2054 if (userPrefix == NULL)
2056 hr = E_FAIL;
2057 goto error;
2061 /* For CSIDL_Type_User we also use the GUID if no szValueName is provided */
2062 szValueName = CSIDL_Data[folder].szValueName;
2063 if (!szValueName)
2065 StringFromGUID2( CSIDL_Data[folder].id, buffer, 39 );
2066 szValueName = &buffer[0];
2069 hr = _SHGetUserShellFolderPath(hRootKey, userPrefix, szValueName, pszPath);
2070 if (FAILED(hr) && hRootKey != HKEY_LOCAL_MACHINE)
2071 hr = _SHGetUserShellFolderPath(HKEY_LOCAL_MACHINE, NULL, szValueName, pszPath);
2072 if (FAILED(hr))
2073 hr = _SHGetDefaultValue(folder, pszPath);
2074 if (userPrefix != NULL && userPrefix != DefaultW)
2075 LocalFree((HLOCAL) userPrefix);
2077 error:
2078 TRACE("returning 0x%08x (output path is %s)\n", hr, debugstr_w(pszPath));
2079 return hr;
2082 /* Gets the (unexpanded) path for the CSIDL with index folder. If dwFlags has
2083 * SHGFP_TYPE_DEFAULT set, calls _SHGetDefaultValue. Otherwise calls
2084 * _SHGetUserShellFolderPath for it, looking only in HKEY_LOCAL_MACHINE.
2085 * If this fails, falls back to _SHGetDefaultValue.
2087 static HRESULT _SHGetAllUsersProfilePath(DWORD dwFlags, BYTE folder,
2088 LPWSTR pszPath)
2090 HRESULT hr;
2092 TRACE("0x%08x,0x%02x,%p\n", dwFlags, folder, pszPath);
2094 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
2095 return E_INVALIDARG;
2096 if (CSIDL_Data[folder].type != CSIDL_Type_AllUsers)
2097 return E_INVALIDARG;
2098 if (!pszPath)
2099 return E_INVALIDARG;
2101 if (dwFlags & SHGFP_TYPE_DEFAULT)
2102 hr = _SHGetDefaultValue(folder, pszPath);
2103 else
2105 hr = _SHGetUserShellFolderPath(HKEY_LOCAL_MACHINE, NULL,
2106 CSIDL_Data[folder].szValueName, pszPath);
2107 if (FAILED(hr))
2108 hr = _SHGetDefaultValue(folder, pszPath);
2110 TRACE("returning 0x%08x (output path is %s)\n", hr, debugstr_w(pszPath));
2111 return hr;
2114 static HRESULT _SHOpenProfilesKey(PHKEY pKey)
2116 LONG lRet;
2117 DWORD disp;
2119 lRet = RegCreateKeyExW(HKEY_LOCAL_MACHINE, ProfileListW, 0, NULL, 0,
2120 KEY_ALL_ACCESS, NULL, pKey, &disp);
2121 return HRESULT_FROM_WIN32(lRet);
2124 /* Reads the value named szValueName from the key profilesKey (assumed to be
2125 * opened by _SHOpenProfilesKey) into szValue, which is assumed to be MAX_PATH
2126 * WCHARs in length. If it doesn't exist, returns szDefault (and saves
2127 * szDefault to the registry).
2129 static HRESULT _SHGetProfilesValue(HKEY profilesKey, LPCWSTR szValueName,
2130 LPWSTR szValue, LPCWSTR szDefault)
2132 HRESULT hr;
2133 DWORD type, dwPathLen = MAX_PATH * sizeof(WCHAR);
2134 LONG lRet;
2136 TRACE("%p,%s,%p,%s\n", profilesKey, debugstr_w(szValueName), szValue,
2137 debugstr_w(szDefault));
2138 lRet = RegQueryValueExW(profilesKey, szValueName, NULL, &type,
2139 (LPBYTE)szValue, &dwPathLen);
2140 if (!lRet && (type == REG_SZ || type == REG_EXPAND_SZ) && dwPathLen
2141 && *szValue)
2143 dwPathLen /= sizeof(WCHAR);
2144 szValue[dwPathLen] = '\0';
2145 hr = S_OK;
2147 else
2149 /* Missing or invalid value, set a default */
2150 lstrcpynW(szValue, szDefault, MAX_PATH);
2151 TRACE("Setting missing value %s to %s\n", debugstr_w(szValueName),
2152 debugstr_w(szValue));
2153 lRet = RegSetValueExW(profilesKey, szValueName, 0, REG_EXPAND_SZ,
2154 (LPBYTE)szValue,
2155 (strlenW(szValue) + 1) * sizeof(WCHAR));
2156 if (lRet)
2157 hr = HRESULT_FROM_WIN32(lRet);
2158 else
2159 hr = S_OK;
2161 TRACE("returning 0x%08x (output value is %s)\n", hr, debugstr_w(szValue));
2162 return hr;
2165 /* Attempts to expand environment variables from szSrc into szDest, which is
2166 * assumed to be MAX_PATH characters in length. Before referring to the
2167 * environment, handles a few variables directly, because the environment
2168 * variables may not be set when this is called (as during Wine's installation
2169 * when default values are being written to the registry).
2170 * The directly handled environment variables, and their source, are:
2171 * - ALLUSERSPROFILE, USERPROFILE: reads from the registry
2172 * - SystemDrive: uses GetSystemDirectoryW and uses the drive portion of its
2173 * path
2174 * If one of the directly handled environment variables is expanded, only
2175 * expands a single variable, and only in the beginning of szSrc.
2177 static HRESULT _SHExpandEnvironmentStrings(LPCWSTR szSrc, LPWSTR szDest)
2179 HRESULT hr;
2180 WCHAR szTemp[MAX_PATH], szProfilesPrefix[MAX_PATH] = { 0 };
2181 HKEY key = NULL;
2183 TRACE("%s, %p\n", debugstr_w(szSrc), szDest);
2185 if (!szSrc || !szDest) return E_INVALIDARG;
2187 /* short-circuit if there's nothing to expand */
2188 if (szSrc[0] != '%')
2190 strcpyW(szDest, szSrc);
2191 hr = S_OK;
2192 goto end;
2194 /* Get the profile prefix, we'll probably be needing it */
2195 hr = _SHOpenProfilesKey(&key);
2196 if (SUCCEEDED(hr))
2198 WCHAR def_val[MAX_PATH];
2200 /* get the system drive */
2201 GetSystemDirectoryW(def_val, MAX_PATH);
2202 if (def_val[1] == ':') strcpyW( def_val + 3, szDefaultProfileDirW );
2203 else FIXME("non-drive system paths unsupported\n");
2205 hr = _SHGetProfilesValue(key, ProfilesDirectoryW, szProfilesPrefix, def_val );
2208 *szDest = 0;
2209 strcpyW(szTemp, szSrc);
2210 while (SUCCEEDED(hr) && szTemp[0] == '%')
2212 if (!strncmpiW(szTemp, AllUsersProfileW, strlenW(AllUsersProfileW)))
2214 WCHAR szAllUsers[MAX_PATH];
2216 strcpyW(szDest, szProfilesPrefix);
2217 hr = _SHGetProfilesValue(key, AllUsersProfileValueW,
2218 szAllUsers, AllUsersW);
2219 PathAppendW(szDest, szAllUsers);
2220 PathAppendW(szDest, szTemp + strlenW(AllUsersProfileW));
2222 else if (!strncmpiW(szTemp, UserProfileW, strlenW(UserProfileW)))
2224 WCHAR userName[MAX_PATH];
2225 DWORD userLen = MAX_PATH;
2227 strcpyW(szDest, szProfilesPrefix);
2228 GetUserNameW(userName, &userLen);
2229 PathAppendW(szDest, userName);
2230 PathAppendW(szDest, szTemp + strlenW(UserProfileW));
2232 else if (!strncmpiW(szTemp, SystemDriveW, strlenW(SystemDriveW)))
2234 GetSystemDirectoryW(szDest, MAX_PATH);
2235 if (szDest[1] != ':')
2237 FIXME("non-drive system paths unsupported\n");
2238 hr = E_FAIL;
2240 else
2242 strcpyW(szDest + 3, szTemp + strlenW(SystemDriveW) + 1);
2243 hr = S_OK;
2246 else
2248 DWORD ret = ExpandEnvironmentStringsW(szSrc, szDest, MAX_PATH);
2250 if (ret > MAX_PATH)
2251 hr = E_NOT_SUFFICIENT_BUFFER;
2252 else if (ret == 0)
2253 hr = HRESULT_FROM_WIN32(GetLastError());
2254 else
2255 hr = S_OK;
2257 if (SUCCEEDED(hr) && szDest[0] == '%')
2258 strcpyW(szTemp, szDest);
2259 else
2261 /* terminate loop */
2262 szTemp[0] = '\0';
2265 end:
2266 if (key)
2267 RegCloseKey(key);
2268 TRACE("returning 0x%08x (input was %s, output is %s)\n", hr,
2269 debugstr_w(szSrc), debugstr_w(szDest));
2270 return hr;
2273 /*************************************************************************
2274 * SHGetFolderPathW [SHELL32.@]
2276 * Convert nFolder to path.
2278 * RETURNS
2279 * Success: S_OK
2280 * Failure: standard HRESULT error codes.
2282 * NOTES
2283 * Most values can be overridden in either
2284 * HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
2285 * or in the same location in HKLM.
2286 * The "Shell Folders" registry key was used in NT4 and earlier systems.
2287 * Beginning with Windows 2000, the "User Shell Folders" key is used, so
2288 * changes made to it are made to the former key too. This synchronization is
2289 * done on-demand: not until someone requests the value of one of these paths
2290 * (by calling one of the SHGet functions) is the value synchronized.
2291 * Furthermore, the HKCU paths take precedence over the HKLM paths.
2293 HRESULT WINAPI SHGetFolderPathW(
2294 HWND hwndOwner, /* [I] owner window */
2295 int nFolder, /* [I] CSIDL identifying the folder */
2296 HANDLE hToken, /* [I] access token */
2297 DWORD dwFlags, /* [I] which path to return */
2298 LPWSTR pszPath) /* [O] converted path */
2300 HRESULT hr = SHGetFolderPathAndSubDirW(hwndOwner, nFolder, hToken, dwFlags, NULL, pszPath);
2301 if(HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND) == hr)
2302 hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
2303 return hr;
2306 HRESULT WINAPI SHGetFolderPathAndSubDirA(
2307 HWND hwndOwner, /* [I] owner window */
2308 int nFolder, /* [I] CSIDL identifying the folder */
2309 HANDLE hToken, /* [I] access token */
2310 DWORD dwFlags, /* [I] which path to return */
2311 LPCSTR pszSubPath, /* [I] sub directory of the specified folder */
2312 LPSTR pszPath) /* [O] converted path */
2314 int length;
2315 HRESULT hr = S_OK;
2316 LPWSTR pszSubPathW = NULL;
2317 LPWSTR pszPathW = NULL;
2318 TRACE("%08x,%08x,%s\n",nFolder, dwFlags, debugstr_w(pszSubPathW));
2320 if(pszPath) {
2321 pszPathW = HeapAlloc(GetProcessHeap(), 0, MAX_PATH * sizeof(WCHAR));
2322 if(!pszPathW) {
2323 hr = HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
2324 goto cleanup;
2327 TRACE("%08x,%08x,%s\n",nFolder, dwFlags, debugstr_w(pszSubPathW));
2329 /* SHGetFolderPathAndSubDirW does not distinguish if pszSubPath isn't
2330 * set (null), or an empty string.therefore call it without the parameter set
2331 * if pszSubPath is an empty string
2333 if (pszSubPath && pszSubPath[0]) {
2334 length = MultiByteToWideChar(CP_ACP, 0, pszSubPath, -1, NULL, 0);
2335 pszSubPathW = HeapAlloc(GetProcessHeap(), 0, length * sizeof(WCHAR));
2336 if(!pszSubPathW) {
2337 hr = HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
2338 goto cleanup;
2340 MultiByteToWideChar(CP_ACP, 0, pszSubPath, -1, pszSubPathW, length);
2343 hr = SHGetFolderPathAndSubDirW(hwndOwner, nFolder, hToken, dwFlags, pszSubPathW, pszPathW);
2345 if (SUCCEEDED(hr) && pszPath)
2346 WideCharToMultiByte(CP_ACP, 0, pszPathW, -1, pszPath, MAX_PATH, NULL, NULL);
2348 cleanup:
2349 HeapFree(GetProcessHeap(), 0, pszPathW);
2350 HeapFree(GetProcessHeap(), 0, pszSubPathW);
2351 return hr;
2354 /*************************************************************************
2355 * SHGetFolderPathAndSubDirW [SHELL32.@]
2357 HRESULT WINAPI SHGetFolderPathAndSubDirW(
2358 HWND hwndOwner, /* [I] owner window */
2359 int nFolder, /* [I] CSIDL identifying the folder */
2360 HANDLE hToken, /* [I] access token */
2361 DWORD dwFlags, /* [I] which path to return */
2362 LPCWSTR pszSubPath,/* [I] sub directory of the specified folder */
2363 LPWSTR pszPath) /* [O] converted path */
2365 HRESULT hr;
2366 WCHAR szBuildPath[MAX_PATH], szTemp[MAX_PATH];
2367 DWORD folder = nFolder & CSIDL_FOLDER_MASK;
2368 CSIDL_Type type;
2369 int ret;
2371 TRACE("%p,%p,nFolder=0x%04x,%s\n", hwndOwner,pszPath,nFolder,debugstr_w(pszSubPath));
2373 /* Windows always NULL-terminates the resulting path regardless of success
2374 * or failure, so do so first
2376 if (pszPath)
2377 *pszPath = '\0';
2379 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
2380 return E_INVALIDARG;
2381 if ((SHGFP_TYPE_CURRENT != dwFlags) && (SHGFP_TYPE_DEFAULT != dwFlags))
2382 return E_INVALIDARG;
2383 szTemp[0] = 0;
2384 type = CSIDL_Data[folder].type;
2385 switch (type)
2387 case CSIDL_Type_Disallowed:
2388 hr = E_INVALIDARG;
2389 break;
2390 case CSIDL_Type_NonExistent:
2391 hr = S_FALSE;
2392 break;
2393 case CSIDL_Type_WindowsPath:
2394 GetWindowsDirectoryW(szTemp, MAX_PATH);
2395 if (CSIDL_Data[folder].szDefaultPath &&
2396 !IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath) &&
2397 *CSIDL_Data[folder].szDefaultPath)
2399 PathAddBackslashW(szTemp);
2400 strcatW(szTemp, CSIDL_Data[folder].szDefaultPath);
2402 hr = S_OK;
2403 break;
2404 case CSIDL_Type_SystemPath:
2405 GetSystemDirectoryW(szTemp, MAX_PATH);
2406 if (CSIDL_Data[folder].szDefaultPath &&
2407 !IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath) &&
2408 *CSIDL_Data[folder].szDefaultPath)
2410 PathAddBackslashW(szTemp);
2411 strcatW(szTemp, CSIDL_Data[folder].szDefaultPath);
2413 hr = S_OK;
2414 break;
2415 case CSIDL_Type_SystemX86Path:
2416 if (!GetSystemWow64DirectoryW(szTemp, MAX_PATH)) GetSystemDirectoryW(szTemp, MAX_PATH);
2417 if (CSIDL_Data[folder].szDefaultPath &&
2418 !IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath) &&
2419 *CSIDL_Data[folder].szDefaultPath)
2421 PathAddBackslashW(szTemp);
2422 strcatW(szTemp, CSIDL_Data[folder].szDefaultPath);
2424 hr = S_OK;
2425 break;
2426 case CSIDL_Type_CurrVer:
2427 hr = _SHGetCurrentVersionPath(dwFlags, folder, szTemp);
2428 break;
2429 case CSIDL_Type_User:
2430 hr = _SHGetUserProfilePath(hToken, dwFlags, folder, szTemp);
2431 break;
2432 case CSIDL_Type_AllUsers:
2433 hr = _SHGetAllUsersProfilePath(dwFlags, folder, szTemp);
2434 break;
2435 default:
2436 FIXME("bogus type %d, please fix\n", type);
2437 hr = E_INVALIDARG;
2438 break;
2441 /* Expand environment strings if necessary */
2442 if (*szTemp == '%')
2443 hr = _SHExpandEnvironmentStrings(szTemp, szBuildPath);
2444 else
2445 strcpyW(szBuildPath, szTemp);
2447 if (FAILED(hr)) goto end;
2449 if(pszSubPath) {
2450 /* make sure the new path does not exceed the buffer length
2451 * and remember to backslash and terminate it */
2452 if(MAX_PATH < (lstrlenW(szBuildPath) + lstrlenW(pszSubPath) + 2)) {
2453 hr = HRESULT_FROM_WIN32(ERROR_FILENAME_EXCED_RANGE);
2454 goto end;
2456 PathAppendW(szBuildPath, pszSubPath);
2457 PathRemoveBackslashW(szBuildPath);
2459 /* Copy the path if it's available before we might return */
2460 if (SUCCEEDED(hr) && pszPath)
2461 strcpyW(pszPath, szBuildPath);
2463 /* if we don't care about existing directories we are ready */
2464 if(nFolder & CSIDL_FLAG_DONT_VERIFY) goto end;
2466 if (PathFileExistsW(szBuildPath)) goto end;
2468 /* not existing but we are not allowed to create it. The return value
2469 * is verified against shell32 version 6.0.
2471 if (!(nFolder & CSIDL_FLAG_CREATE))
2473 hr = HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND);
2474 goto end;
2477 /* create directory/directories */
2478 ret = SHCreateDirectoryExW(hwndOwner, szBuildPath, NULL);
2479 if (ret && ret != ERROR_ALREADY_EXISTS)
2481 ERR("Failed to create directory %s.\n", debugstr_w(szBuildPath));
2482 hr = E_FAIL;
2483 goto end;
2486 TRACE("Created missing system directory %s\n", debugstr_w(szBuildPath));
2487 end:
2488 TRACE("returning 0x%08x (final path is %s)\n", hr, debugstr_w(szBuildPath));
2489 return hr;
2492 /*************************************************************************
2493 * SHGetFolderPathA [SHELL32.@]
2495 * See SHGetFolderPathW.
2497 HRESULT WINAPI SHGetFolderPathA(
2498 HWND hwndOwner,
2499 int nFolder,
2500 HANDLE hToken,
2501 DWORD dwFlags,
2502 LPSTR pszPath)
2504 WCHAR szTemp[MAX_PATH];
2505 HRESULT hr;
2507 TRACE("%p,%p,nFolder=0x%04x\n",hwndOwner,pszPath,nFolder);
2509 if (pszPath)
2510 *pszPath = '\0';
2511 hr = SHGetFolderPathW(hwndOwner, nFolder, hToken, dwFlags, szTemp);
2512 if (SUCCEEDED(hr) && pszPath)
2513 WideCharToMultiByte(CP_ACP, 0, szTemp, -1, pszPath, MAX_PATH, NULL,
2514 NULL);
2516 return hr;
2519 /* For each folder in folders, if its value has not been set in the registry,
2520 * calls _SHGetUserProfilePath or _SHGetAllUsersProfilePath (depending on the
2521 * folder's type) to get the unexpanded value first.
2522 * Writes the unexpanded value to User Shell Folders, and queries it with
2523 * SHGetFolderPathW to force the creation of the directory if it doesn't
2524 * already exist. SHGetFolderPathW also returns the expanded value, which
2525 * this then writes to Shell Folders.
2527 static HRESULT _SHRegisterFolders(HKEY hRootKey, HANDLE hToken,
2528 LPCWSTR szUserShellFolderPath, LPCWSTR szShellFolderPath, const UINT folders[],
2529 UINT foldersLen)
2531 const WCHAR *szValueName;
2532 WCHAR buffer[40];
2533 UINT i;
2534 WCHAR path[MAX_PATH];
2535 HRESULT hr = S_OK;
2536 HKEY hUserKey = NULL, hKey = NULL;
2537 DWORD dwType, dwPathLen;
2538 LONG ret;
2540 TRACE("%p,%p,%s,%p,%u\n", hRootKey, hToken,
2541 debugstr_w(szUserShellFolderPath), folders, foldersLen);
2543 ret = RegCreateKeyW(hRootKey, szUserShellFolderPath, &hUserKey);
2544 if (ret)
2545 hr = HRESULT_FROM_WIN32(ret);
2546 else
2548 ret = RegCreateKeyW(hRootKey, szShellFolderPath, &hKey);
2549 if (ret)
2550 hr = HRESULT_FROM_WIN32(ret);
2552 for (i = 0; SUCCEEDED(hr) && i < foldersLen; i++)
2554 dwPathLen = MAX_PATH * sizeof(WCHAR);
2556 /* For CSIDL_Type_User we also use the GUID if no szValueName is provided */
2557 szValueName = CSIDL_Data[folders[i]].szValueName;
2558 if (!szValueName && CSIDL_Data[folders[i]].type == CSIDL_Type_User)
2560 StringFromGUID2( CSIDL_Data[folders[i]].id, buffer, 39 );
2561 szValueName = &buffer[0];
2564 if (RegQueryValueExW(hUserKey, szValueName, NULL,
2565 &dwType, (LPBYTE)path, &dwPathLen) || (dwType != REG_SZ &&
2566 dwType != REG_EXPAND_SZ))
2568 *path = '\0';
2569 if (CSIDL_Data[folders[i]].type == CSIDL_Type_User)
2570 _SHGetUserProfilePath(hToken, SHGFP_TYPE_DEFAULT, folders[i],
2571 path);
2572 else if (CSIDL_Data[folders[i]].type == CSIDL_Type_AllUsers)
2573 _SHGetAllUsersProfilePath(SHGFP_TYPE_DEFAULT, folders[i], path);
2574 else if (CSIDL_Data[folders[i]].type == CSIDL_Type_WindowsPath)
2576 GetWindowsDirectoryW(path, MAX_PATH);
2577 if (CSIDL_Data[folders[i]].szDefaultPath &&
2578 !IS_INTRESOURCE(CSIDL_Data[folders[i]].szDefaultPath))
2580 PathAddBackslashW(path);
2581 strcatW(path, CSIDL_Data[folders[i]].szDefaultPath);
2584 else
2585 hr = E_FAIL;
2586 if (*path)
2588 ret = RegSetValueExW(hUserKey, szValueName, 0, REG_EXPAND_SZ,
2589 (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR));
2590 if (ret)
2591 hr = HRESULT_FROM_WIN32(ret);
2592 else
2594 hr = SHGetFolderPathW(NULL, folders[i] | CSIDL_FLAG_CREATE,
2595 hToken, SHGFP_TYPE_DEFAULT, path);
2596 ret = RegSetValueExW(hKey, szValueName, 0, REG_SZ,
2597 (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR));
2598 if (ret)
2599 hr = HRESULT_FROM_WIN32(ret);
2604 if (hUserKey)
2605 RegCloseKey(hUserKey);
2606 if (hKey)
2607 RegCloseKey(hKey);
2609 TRACE("returning 0x%08x\n", hr);
2610 return hr;
2613 static HRESULT _SHRegisterUserShellFolders(BOOL bDefault)
2615 static const UINT folders[] = {
2616 CSIDL_PROGRAMS,
2617 CSIDL_PERSONAL,
2618 CSIDL_FAVORITES,
2619 CSIDL_APPDATA,
2620 CSIDL_STARTUP,
2621 CSIDL_RECENT,
2622 CSIDL_SENDTO,
2623 CSIDL_STARTMENU,
2624 CSIDL_MYMUSIC,
2625 CSIDL_MYVIDEO,
2626 CSIDL_DESKTOPDIRECTORY,
2627 CSIDL_NETHOOD,
2628 CSIDL_TEMPLATES,
2629 CSIDL_PRINTHOOD,
2630 CSIDL_LOCAL_APPDATA,
2631 CSIDL_INTERNET_CACHE,
2632 CSIDL_COOKIES,
2633 CSIDL_HISTORY,
2634 CSIDL_MYPICTURES,
2635 CSIDL_FONTS,
2636 CSIDL_ADMINTOOLS,
2637 CSIDL_CONTACTS,
2638 CSIDL_DOWNLOADS,
2639 CSIDL_LINKS,
2640 CSIDL_APPDATA_LOCALLOW,
2641 CSIDL_SAVED_GAMES,
2642 CSIDL_SEARCHES
2644 WCHAR userShellFolderPath[MAX_PATH], shellFolderPath[MAX_PATH];
2645 LPCWSTR pUserShellFolderPath, pShellFolderPath;
2646 HRESULT hr = S_OK;
2647 HKEY hRootKey;
2648 HANDLE hToken;
2650 TRACE("%s\n", bDefault ? "TRUE" : "FALSE");
2651 if (bDefault)
2653 hToken = (HANDLE)-1;
2654 hRootKey = HKEY_USERS;
2655 strcpyW(userShellFolderPath, DefaultW);
2656 PathAddBackslashW(userShellFolderPath);
2657 strcatW(userShellFolderPath, szSHUserFolders);
2658 pUserShellFolderPath = userShellFolderPath;
2659 strcpyW(shellFolderPath, DefaultW);
2660 PathAddBackslashW(shellFolderPath);
2661 strcatW(shellFolderPath, szSHFolders);
2662 pShellFolderPath = shellFolderPath;
2664 else
2666 hToken = NULL;
2667 hRootKey = HKEY_CURRENT_USER;
2668 pUserShellFolderPath = szSHUserFolders;
2669 pShellFolderPath = szSHFolders;
2672 hr = _SHRegisterFolders(hRootKey, hToken, pUserShellFolderPath,
2673 pShellFolderPath, folders, sizeof(folders) / sizeof(folders[0]));
2674 TRACE("returning 0x%08x\n", hr);
2675 return hr;
2678 static HRESULT _SHRegisterCommonShellFolders(void)
2680 static const UINT folders[] = {
2681 CSIDL_COMMON_STARTMENU,
2682 CSIDL_COMMON_PROGRAMS,
2683 CSIDL_COMMON_STARTUP,
2684 CSIDL_COMMON_DESKTOPDIRECTORY,
2685 CSIDL_COMMON_FAVORITES,
2686 CSIDL_COMMON_APPDATA,
2687 CSIDL_COMMON_TEMPLATES,
2688 CSIDL_COMMON_DOCUMENTS,
2689 CSIDL_COMMON_ADMINTOOLS,
2690 CSIDL_COMMON_MUSIC,
2691 CSIDL_COMMON_PICTURES,
2692 CSIDL_COMMON_VIDEO,
2694 HRESULT hr;
2696 TRACE("\n");
2697 hr = _SHRegisterFolders(HKEY_LOCAL_MACHINE, NULL, szSHUserFolders,
2698 szSHFolders, folders, sizeof(folders) / sizeof(folders[0]));
2699 TRACE("returning 0x%08x\n", hr);
2700 return hr;
2703 /******************************************************************************
2704 * _SHAppendToUnixPath [Internal]
2706 * Helper function for _SHCreateSymbolicLinks. Appends pwszSubPath (or the
2707 * corresponding resource, if IS_INTRESOURCE) to the unix base path 'szBasePath'
2708 * and replaces backslashes with slashes.
2710 * PARAMS
2711 * szBasePath [IO] The unix base path, which will be appended to (CP_UNXICP).
2712 * pwszSubPath [I] Sub-path or resource id (use MAKEINTRESOURCEW).
2714 * RETURNS
2715 * Success: TRUE,
2716 * Failure: FALSE
2718 static inline BOOL _SHAppendToUnixPath(char *szBasePath, LPCWSTR pwszSubPath) {
2719 WCHAR wszSubPath[MAX_PATH];
2720 int cLen = strlen(szBasePath);
2721 char *pBackslash;
2723 if (IS_INTRESOURCE(pwszSubPath)) {
2724 if (!LoadStringW(shell32_hInstance, LOWORD(pwszSubPath), wszSubPath, MAX_PATH)) {
2725 /* Fall back to hard coded defaults. */
2726 switch (LOWORD(pwszSubPath)) {
2727 case IDS_PERSONAL:
2728 lstrcpyW(wszSubPath, DocumentsW);
2729 break;
2730 case IDS_MYMUSIC:
2731 lstrcpyW(wszSubPath, My_MusicW);
2732 break;
2733 case IDS_MYPICTURES:
2734 lstrcpyW(wszSubPath, My_PicturesW);
2735 break;
2736 case IDS_MYVIDEOS:
2737 lstrcpyW(wszSubPath, My_VideosW);
2738 break;
2739 default:
2740 ERR("LoadString(%d) failed!\n", LOWORD(pwszSubPath));
2741 return FALSE;
2744 } else {
2745 lstrcpyW(wszSubPath, pwszSubPath);
2748 if (szBasePath[cLen-1] != '/') szBasePath[cLen++] = '/';
2750 if (!WideCharToMultiByte(CP_UNIXCP, 0, wszSubPath, -1, szBasePath + cLen,
2751 FILENAME_MAX - cLen, NULL, NULL))
2753 return FALSE;
2756 pBackslash = szBasePath + cLen;
2757 while ((pBackslash = strchr(pBackslash, '\\'))) *pBackslash = '/';
2759 return TRUE;
2762 /******************************************************************************
2763 * _SHCreateSymbolicLinks [Internal]
2765 * Sets up symbol links for various shell folders to point into the users home
2766 * directory. We do an educated guess about what the user would probably want:
2767 * - If there is a 'My Documents' directory in $HOME, the user probably wants
2768 * wine's 'My Documents' to point there. Furthermore, we imply that the user
2769 * is a Windows lover and has no problem with wine creating 'My Pictures',
2770 * 'My Music' and 'My Videos' subfolders under '$HOME/My Documents', if those
2771 * do not already exits. We put appropriate symbolic links in place for those,
2772 * too.
2773 * - If there is no 'My Documents' directory in $HOME, we let 'My Documents'
2774 * point directly to $HOME. We assume the user to be a unix hacker who does not
2775 * want wine to create anything anywhere besides the .wine directory. So, if
2776 * there already is a 'My Music' directory in $HOME, we symlink the 'My Music'
2777 * shell folder to it. But if not, then we check XDG_MUSIC_DIR - "well known"
2778 * directory, and try to link to that. If that fails, then we symlink to
2779 * $HOME directly. The same holds fo 'My Pictures' and 'My Videos'.
2780 * - The Desktop shell folder is symlinked to XDG_DESKTOP_DIR. If that does not
2781 * exist, then we try '$HOME/Desktop'. If that does not exist, then we leave
2782 * it alone.
2783 * ('My Music',... above in fact means LoadString(IDS_MYMUSIC))
2785 static void _SHCreateSymbolicLinks(void)
2787 UINT aidsMyStuff[] = { IDS_MYPICTURES, IDS_MYVIDEOS, IDS_MYMUSIC }, i;
2788 int acsidlMyStuff[] = { CSIDL_MYPICTURES, CSIDL_MYVIDEO, CSIDL_MYMUSIC };
2789 static const char * const xdg_dirs[] = { "PICTURES", "VIDEOS", "MUSIC", "DESKTOP" };
2790 static const unsigned int num = sizeof(xdg_dirs) / sizeof(xdg_dirs[0]);
2791 WCHAR wszTempPath[MAX_PATH];
2792 char szPersonalTarget[FILENAME_MAX], *pszPersonal;
2793 char szMyStuffTarget[FILENAME_MAX], *pszMyStuff;
2794 char szDesktopTarget[FILENAME_MAX], *pszDesktop;
2795 struct stat statFolder;
2796 const char *pszHome;
2797 HRESULT hr;
2798 char ** xdg_results;
2799 char * xdg_desktop_dir;
2801 /* Create all necessary profile sub-dirs up to 'My Documents' and get the unix path. */
2802 hr = SHGetFolderPathW(NULL, CSIDL_PERSONAL|CSIDL_FLAG_CREATE, NULL,
2803 SHGFP_TYPE_DEFAULT, wszTempPath);
2804 if (FAILED(hr)) return;
2805 pszPersonal = wine_get_unix_file_name(wszTempPath);
2806 if (!pszPersonal) return;
2808 hr = XDG_UserDirLookup(xdg_dirs, num, &xdg_results);
2809 if (FAILED(hr)) xdg_results = NULL;
2811 pszHome = getenv("HOME");
2812 if (pszHome && !stat(pszHome, &statFolder) && S_ISDIR(statFolder.st_mode)) {
2813 strcpy(szPersonalTarget, pszHome);
2814 if (_SHAppendToUnixPath(szPersonalTarget, MAKEINTRESOURCEW(IDS_PERSONAL)) &&
2815 !stat(szPersonalTarget, &statFolder) && S_ISDIR(statFolder.st_mode))
2817 /* '$HOME/My Documents' exists. Create 'My Pictures', 'My Videos' and
2818 * 'My Music' subfolders or fail silently if they already exist. */
2819 for (i = 0; i < sizeof(aidsMyStuff)/sizeof(aidsMyStuff[0]); i++) {
2820 strcpy(szMyStuffTarget, szPersonalTarget);
2821 if (_SHAppendToUnixPath(szMyStuffTarget, MAKEINTRESOURCEW(aidsMyStuff[i])))
2822 mkdir(szMyStuffTarget, 0777);
2825 else
2827 /* '$HOME/My Documents' doesn't exists, but '$HOME' does. */
2828 strcpy(szPersonalTarget, pszHome);
2831 /* Replace 'My Documents' directory with a symlink or fail silently if not empty. */
2832 rmdir(pszPersonal);
2833 symlink(szPersonalTarget, pszPersonal);
2835 else
2837 /* '$HOME' doesn't exist. Create 'My Pictures', 'My Videos' and 'My Music' subdirs
2838 * in '%USERPROFILE%\\My Documents' or fail silently if they already exist. */
2839 pszHome = NULL;
2840 strcpy(szPersonalTarget, pszPersonal);
2841 for (i = 0; i < sizeof(aidsMyStuff)/sizeof(aidsMyStuff[0]); i++) {
2842 strcpy(szMyStuffTarget, szPersonalTarget);
2843 if (_SHAppendToUnixPath(szMyStuffTarget, MAKEINTRESOURCEW(aidsMyStuff[i])))
2844 mkdir(szMyStuffTarget, 0777);
2848 /* Create symbolic links for 'My Pictures', 'My Videos' and 'My Music'. */
2849 for (i=0; i < sizeof(aidsMyStuff)/sizeof(aidsMyStuff[0]); i++) {
2850 /* Create the current 'My Whatever' folder and get its unix path. */
2851 hr = SHGetFolderPathW(NULL, acsidlMyStuff[i]|CSIDL_FLAG_CREATE, NULL,
2852 SHGFP_TYPE_DEFAULT, wszTempPath);
2853 if (FAILED(hr)) continue;
2854 pszMyStuff = wine_get_unix_file_name(wszTempPath);
2855 if (!pszMyStuff) continue;
2857 strcpy(szMyStuffTarget, szPersonalTarget);
2858 if (_SHAppendToUnixPath(szMyStuffTarget, MAKEINTRESOURCEW(aidsMyStuff[i])) &&
2859 !stat(szMyStuffTarget, &statFolder) && S_ISDIR(statFolder.st_mode))
2861 /* If there's a 'My Whatever' directory where 'My Documents' links to, link to it. */
2862 rmdir(pszMyStuff);
2863 symlink(szMyStuffTarget, pszMyStuff);
2865 else
2867 rmdir(pszMyStuff);
2868 if (xdg_results && xdg_results[i])
2870 /* the folder specified by XDG_XXX_DIR exists, link to it. */
2871 symlink(xdg_results[i], pszMyStuff);
2873 else
2875 /* Else link to where 'My Documents' itself links to. */
2876 symlink(szPersonalTarget, pszMyStuff);
2879 HeapFree(GetProcessHeap(), 0, pszMyStuff);
2882 /* Last but not least, the Desktop folder */
2883 if (pszHome)
2884 strcpy(szDesktopTarget, pszHome);
2885 else
2886 strcpy(szDesktopTarget, pszPersonal);
2887 HeapFree(GetProcessHeap(), 0, pszPersonal);
2889 xdg_desktop_dir = xdg_results ? xdg_results[num - 1] : NULL;
2890 if (xdg_desktop_dir ||
2891 (_SHAppendToUnixPath(szDesktopTarget, DesktopW) &&
2892 !stat(szDesktopTarget, &statFolder) && S_ISDIR(statFolder.st_mode)))
2894 hr = SHGetFolderPathW(NULL, CSIDL_DESKTOPDIRECTORY|CSIDL_FLAG_CREATE, NULL,
2895 SHGFP_TYPE_DEFAULT, wszTempPath);
2896 if (SUCCEEDED(hr) && (pszDesktop = wine_get_unix_file_name(wszTempPath)))
2898 rmdir(pszDesktop);
2899 if (xdg_desktop_dir)
2900 symlink(xdg_desktop_dir, pszDesktop);
2901 else
2902 symlink(szDesktopTarget, pszDesktop);
2903 HeapFree(GetProcessHeap(), 0, pszDesktop);
2907 /* Free resources allocated by XDG_UserDirLookup() */
2908 if (xdg_results)
2910 for (i = 0; i < num; i++)
2911 HeapFree(GetProcessHeap(), 0, xdg_results[i]);
2912 HeapFree(GetProcessHeap(), 0, xdg_results);
2916 /******************************************************************************
2917 * create_extra_folders [Internal]
2919 * Create some extra folders that don't have a standard CSIDL definition.
2921 static HRESULT create_extra_folders(void)
2923 static const WCHAR environW[] = {'E','n','v','i','r','o','n','m','e','n','t',0};
2924 static const WCHAR microsoftW[] = {'M','i','c','r','o','s','o','f','t',0};
2925 static const WCHAR TempW[] = {'T','e','m','p',0};
2926 static const WCHAR TEMPW[] = {'T','E','M','P',0};
2927 static const WCHAR TMPW[] = {'T','M','P',0};
2928 WCHAR path[MAX_PATH+5];
2929 HRESULT hr;
2930 HKEY hkey;
2931 DWORD type, size, ret;
2933 ret = RegCreateKeyW( HKEY_CURRENT_USER, environW, &hkey );
2934 if (ret) return HRESULT_FROM_WIN32( ret );
2936 /* FIXME: should be under AppData, but we don't want spaces in the temp path */
2937 hr = SHGetFolderPathAndSubDirW( 0, CSIDL_PROFILE | CSIDL_FLAG_CREATE, NULL,
2938 SHGFP_TYPE_DEFAULT, TempW, path );
2939 if (SUCCEEDED(hr))
2941 size = sizeof(path);
2942 if (RegQueryValueExW( hkey, TEMPW, NULL, &type, (LPBYTE)path, &size ))
2943 RegSetValueExW( hkey, TEMPW, 0, REG_SZ, (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR) );
2944 size = sizeof(path);
2945 if (RegQueryValueExW( hkey, TMPW, NULL, &type, (LPBYTE)path, &size ))
2946 RegSetValueExW( hkey, TMPW, 0, REG_SZ, (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR) );
2948 RegCloseKey( hkey );
2950 if (SUCCEEDED(hr))
2952 hr = SHGetFolderPathAndSubDirW( 0, CSIDL_COMMON_APPDATA | CSIDL_FLAG_CREATE, NULL,
2953 SHGFP_TYPE_DEFAULT, microsoftW, path );
2955 return hr;
2959 /******************************************************************************
2960 * set_folder_attributes
2962 * Set the various folder attributes registry keys.
2964 static HRESULT set_folder_attributes(void)
2966 static const WCHAR clsidW[] = {'C','L','S','I','D','\\',0 };
2967 static const WCHAR shellfolderW[] = {'\\','S','h','e','l','l','F','o','l','d','e','r', 0 };
2968 static const WCHAR wfparsingW[] = {'W','a','n','t','s','F','O','R','P','A','R','S','I','N','G',0};
2969 static const WCHAR wfdisplayW[] = {'W','a','n','t','s','F','O','R','D','I','S','P','L','A','Y',0};
2970 static const WCHAR hideasdeleteW[] = {'H','i','d','e','A','s','D','e','l','e','t','e','P','e','r','U','s','e','r',0};
2971 static const WCHAR cfattributesW[] = {'C','a','l','l','F','o','r','A','t','t','r','i','b','u','t','e','s',0};
2972 static const WCHAR emptyW[] = {0};
2974 static const struct
2976 const CLSID *clsid;
2977 BOOL wfparsing : 1;
2978 BOOL wfdisplay : 1;
2979 BOOL hideasdel : 1;
2980 DWORD attr;
2981 DWORD call_for_attr;
2982 } folders[] =
2984 { &CLSID_UnixFolder, TRUE, FALSE, FALSE },
2985 { &CLSID_UnixDosFolder, TRUE, FALSE, FALSE,
2986 SFGAO_FILESYSANCESTOR|SFGAO_FOLDER|SFGAO_HASSUBFOLDER, SFGAO_FILESYSTEM },
2987 { &CLSID_FolderShortcut, FALSE, FALSE, FALSE,
2988 SFGAO_FILESYSTEM|SFGAO_FOLDER|SFGAO_LINK,
2989 SFGAO_HASSUBFOLDER|SFGAO_FILESYSTEM|SFGAO_FOLDER|SFGAO_FILESYSANCESTOR },
2990 { &CLSID_MyDocuments, TRUE, FALSE, FALSE,
2991 SFGAO_FILESYSANCESTOR|SFGAO_FOLDER|SFGAO_HASSUBFOLDER, SFGAO_FILESYSTEM },
2992 { &CLSID_RecycleBin, FALSE, FALSE, FALSE,
2993 SFGAO_FOLDER|SFGAO_DROPTARGET|SFGAO_HASPROPSHEET },
2994 { &CLSID_ControlPanel, FALSE, TRUE, TRUE,
2995 SFGAO_FOLDER|SFGAO_HASSUBFOLDER }
2998 unsigned int i;
2999 WCHAR buffer[39 + (sizeof(clsidW) + sizeof(shellfolderW)) / sizeof(WCHAR)];
3000 LONG res;
3001 HKEY hkey;
3003 for (i = 0; i < sizeof(folders)/sizeof(folders[0]); i++)
3005 strcpyW( buffer, clsidW );
3006 StringFromGUID2( folders[i].clsid, buffer + strlenW(buffer), 39 );
3007 strcatW( buffer, shellfolderW );
3008 res = RegCreateKeyExW( HKEY_CLASSES_ROOT, buffer, 0, NULL, 0,
3009 KEY_READ | KEY_WRITE, NULL, &hkey, NULL);
3010 if (res) return HRESULT_FROM_WIN32( res );
3011 if (folders[i].wfparsing)
3012 res = RegSetValueExW( hkey, wfparsingW, 0, REG_SZ, (const BYTE *)emptyW, sizeof(emptyW) );
3013 if (folders[i].wfdisplay)
3014 res = RegSetValueExW( hkey, wfdisplayW, 0, REG_SZ, (const BYTE *)emptyW, sizeof(emptyW) );
3015 if (folders[i].hideasdel)
3016 res = RegSetValueExW( hkey, hideasdeleteW, 0, REG_SZ, (const BYTE *)emptyW, sizeof(emptyW) );
3017 if (folders[i].attr)
3018 res = RegSetValueExW( hkey, szAttributes, 0, REG_DWORD,
3019 (const BYTE *)&folders[i].attr, sizeof(DWORD));
3020 if (folders[i].call_for_attr)
3021 res = RegSetValueExW( hkey, cfattributesW, 0, REG_DWORD,
3022 (const BYTE *)&folders[i].call_for_attr, sizeof(DWORD));
3023 RegCloseKey( hkey );
3025 return S_OK;
3028 /*************************************************************************
3029 * SHGetSpecialFolderPathA [SHELL32.@]
3031 BOOL WINAPI SHGetSpecialFolderPathA (
3032 HWND hwndOwner,
3033 LPSTR szPath,
3034 int nFolder,
3035 BOOL bCreate)
3037 return SHGetFolderPathA(hwndOwner, nFolder + (bCreate ? CSIDL_FLAG_CREATE : 0), NULL, 0,
3038 szPath) == S_OK;
3041 /*************************************************************************
3042 * SHGetSpecialFolderPathW
3044 BOOL WINAPI SHGetSpecialFolderPathW (
3045 HWND hwndOwner,
3046 LPWSTR szPath,
3047 int nFolder,
3048 BOOL bCreate)
3050 return SHGetFolderPathW(hwndOwner, nFolder + (bCreate ? CSIDL_FLAG_CREATE : 0), NULL, 0,
3051 szPath) == S_OK;
3054 /*************************************************************************
3055 * SHGetSpecialFolderPath (SHELL32.175)
3057 BOOL WINAPI SHGetSpecialFolderPathAW (
3058 HWND hwndOwner,
3059 LPVOID szPath,
3060 int nFolder,
3061 BOOL bCreate)
3064 if (SHELL_OsIsUnicode())
3065 return SHGetSpecialFolderPathW (hwndOwner, szPath, nFolder, bCreate);
3066 return SHGetSpecialFolderPathA (hwndOwner, szPath, nFolder, bCreate);
3069 /*************************************************************************
3070 * SHGetFolderLocation [SHELL32.@]
3072 * Gets the folder locations from the registry and creates a pidl.
3074 * PARAMS
3075 * hwndOwner [I]
3076 * nFolder [I] CSIDL_xxxxx
3077 * hToken [I] token representing user, or NULL for current user, or -1 for
3078 * default user
3079 * dwReserved [I] must be zero
3080 * ppidl [O] PIDL of a special folder
3082 * RETURNS
3083 * Success: S_OK
3084 * Failure: Standard OLE-defined error result, S_FALSE or E_INVALIDARG
3086 * NOTES
3087 * Creates missing reg keys and directories.
3088 * Mostly forwards to SHGetFolderPathW, but a few values of nFolder return
3089 * virtual folders that are handled here.
3091 HRESULT WINAPI SHGetFolderLocation(
3092 HWND hwndOwner,
3093 int nFolder,
3094 HANDLE hToken,
3095 DWORD dwReserved,
3096 LPITEMIDLIST *ppidl)
3098 HRESULT hr = E_INVALIDARG;
3100 TRACE("%p 0x%08x %p 0x%08x %p\n",
3101 hwndOwner, nFolder, hToken, dwReserved, ppidl);
3103 if (!ppidl)
3104 return E_INVALIDARG;
3105 if (dwReserved)
3106 return E_INVALIDARG;
3108 /* The virtual folders' locations are not user-dependent */
3109 *ppidl = NULL;
3110 switch (nFolder & CSIDL_FOLDER_MASK)
3112 case CSIDL_DESKTOP:
3113 *ppidl = _ILCreateDesktop();
3114 break;
3116 case CSIDL_PERSONAL:
3117 *ppidl = _ILCreateMyDocuments();
3118 break;
3120 case CSIDL_INTERNET:
3121 *ppidl = _ILCreateIExplore();
3122 break;
3124 case CSIDL_CONTROLS:
3125 *ppidl = _ILCreateControlPanel();
3126 break;
3128 case CSIDL_PRINTERS:
3129 *ppidl = _ILCreatePrinters();
3130 break;
3132 case CSIDL_BITBUCKET:
3133 *ppidl = _ILCreateBitBucket();
3134 break;
3136 case CSIDL_DRIVES:
3137 *ppidl = _ILCreateMyComputer();
3138 break;
3140 case CSIDL_NETWORK:
3141 *ppidl = _ILCreateNetwork();
3142 break;
3144 default:
3146 WCHAR szPath[MAX_PATH];
3148 hr = SHGetFolderPathW(hwndOwner, nFolder, hToken,
3149 SHGFP_TYPE_CURRENT, szPath);
3150 if (SUCCEEDED(hr))
3152 DWORD attributes=0;
3154 TRACE("Value=%s\n", debugstr_w(szPath));
3155 hr = SHILCreateFromPathW(szPath, ppidl, &attributes);
3157 else if (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
3159 /* unlike SHGetFolderPath, SHGetFolderLocation in shell32
3160 * version 6.0 returns E_FAIL for nonexistent paths
3162 hr = E_FAIL;
3166 if(*ppidl)
3167 hr = S_OK;
3169 TRACE("-- (new pidl %p)\n",*ppidl);
3170 return hr;
3173 /*************************************************************************
3174 * SHGetSpecialFolderLocation [SHELL32.@]
3176 * NOTES
3177 * In NT5, SHGetSpecialFolderLocation needs the <winntdir>/Recent
3178 * directory.
3180 HRESULT WINAPI SHGetSpecialFolderLocation(
3181 HWND hwndOwner,
3182 INT nFolder,
3183 LPITEMIDLIST * ppidl)
3185 HRESULT hr = E_INVALIDARG;
3187 TRACE("(%p,0x%x,%p)\n", hwndOwner,nFolder,ppidl);
3189 if (!ppidl)
3190 return E_INVALIDARG;
3192 hr = SHGetFolderLocation(hwndOwner, nFolder, NULL, 0, ppidl);
3193 return hr;
3196 static int csidl_from_id( const KNOWNFOLDERID *id )
3198 int i;
3199 for (i = 0; i < sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]); i++)
3200 if (IsEqualGUID( CSIDL_Data[i].id, id )) return i;
3201 return -1;
3204 /*************************************************************************
3205 * SHGetKnownFolderPath [SHELL32.@]
3207 HRESULT WINAPI SHGetKnownFolderPath(REFKNOWNFOLDERID rfid, DWORD flags, HANDLE token, PWSTR *path)
3209 HRESULT hr;
3210 WCHAR folder[MAX_PATH];
3211 int index = csidl_from_id( rfid );
3213 TRACE("%s, 0x%08x, %p, %p\n", debugstr_guid(rfid), flags, token, path);
3215 *path = NULL;
3217 if (index < 0)
3218 return HRESULT_FROM_WIN32( ERROR_FILE_NOT_FOUND );
3220 if (flags & KF_FLAG_CREATE)
3221 index |= CSIDL_FLAG_CREATE;
3223 if (flags & KF_FLAG_DONT_VERIFY)
3224 index |= CSIDL_FLAG_DONT_VERIFY;
3226 if (flags & KF_FLAG_NO_ALIAS)
3227 index |= CSIDL_FLAG_NO_ALIAS;
3229 if (flags & KF_FLAG_INIT)
3230 index |= CSIDL_FLAG_PER_USER_INIT;
3232 if (flags & ~(KF_FLAG_CREATE|KF_FLAG_DONT_VERIFY|KF_FLAG_NO_ALIAS|KF_FLAG_INIT))
3234 FIXME("flags 0x%08x not supported\n", flags);
3235 return E_INVALIDARG;
3238 hr = SHGetFolderPathW( NULL, index, token, 0, folder );
3239 if (SUCCEEDED(hr))
3241 *path = CoTaskMemAlloc( (strlenW( folder ) + 1) * sizeof(WCHAR) );
3242 if (!*path)
3243 return E_OUTOFMEMORY;
3244 strcpyW( *path, folder );
3246 return hr;
3249 /*************************************************************************
3250 * SHGetFolderPathEx [SHELL32.@]
3252 HRESULT WINAPI SHGetFolderPathEx(REFKNOWNFOLDERID rfid, DWORD flags, HANDLE token, LPWSTR path, DWORD len)
3254 HRESULT hr;
3255 WCHAR *buffer;
3257 TRACE("%s, 0x%08x, %p, %p, %u\n", debugstr_guid(rfid), flags, token, path, len);
3259 if (!path || !len) return E_INVALIDARG;
3261 hr = SHGetKnownFolderPath( rfid, flags, token, &buffer );
3262 if (SUCCEEDED( hr ))
3264 if (strlenW( buffer ) + 1 > len)
3266 CoTaskMemFree( buffer );
3267 return HRESULT_FROM_WIN32( ERROR_INSUFFICIENT_BUFFER );
3269 strcpyW( path, buffer );
3270 CoTaskMemFree( buffer );
3272 return hr;
3276 * Internal function to convert known folder identifier to path of registry key
3277 * associated with known folder.
3279 * Parameters:
3280 * rfid [I] pointer to known folder identifier (may be NULL)
3281 * lpStringGuid [I] string with known folder identifier (used when rfid is NULL)
3282 * lpPath [O] place to store string address. String should be
3283 * later freed using HeapFree(GetProcessHeap(),0, ... )
3285 static HRESULT get_known_folder_registry_path(
3286 REFKNOWNFOLDERID rfid,
3287 LPWSTR lpStringGuid,
3288 LPWSTR *lpPath)
3290 static const WCHAR sBackslash[] = {'\\',0};
3291 HRESULT hr = S_OK;
3292 int length;
3293 WCHAR sGuid[50];
3295 TRACE("(%s, %s, %p)\n", debugstr_guid(rfid), debugstr_w(lpStringGuid), lpPath);
3297 if(rfid)
3298 StringFromGUID2(rfid, sGuid, sizeof(sGuid)/sizeof(sGuid[0]));
3299 else
3300 lstrcpyW(sGuid, lpStringGuid);
3302 length = lstrlenW(szKnownFolderDescriptions)+51;
3303 *lpPath = HeapAlloc(GetProcessHeap(), 0, length*sizeof(WCHAR));
3304 if(!(*lpPath))
3305 hr = E_OUTOFMEMORY;
3307 if(SUCCEEDED(hr))
3309 lstrcpyW(*lpPath, szKnownFolderDescriptions);
3310 lstrcatW(*lpPath, sBackslash);
3311 lstrcatW(*lpPath, sGuid);
3314 return hr;
3317 static HRESULT get_known_folder_wstr(const WCHAR *regpath, const WCHAR *value, WCHAR **out)
3319 DWORD size = 0;
3320 HRESULT hr;
3322 size = 0;
3323 hr = HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE, regpath, value, RRF_RT_REG_SZ, NULL, NULL, &size));
3324 if(FAILED(hr))
3325 return hr;
3327 *out = CoTaskMemAlloc(size);
3328 if(!*out)
3329 return E_OUTOFMEMORY;
3331 hr = HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE, regpath, value, RRF_RT_REG_SZ, NULL, *out, &size));
3332 if(FAILED(hr)){
3333 CoTaskMemFree(*out);
3334 *out = NULL;
3337 return hr;
3340 static HRESULT get_known_folder_dword(const WCHAR *registryPath, const WCHAR *value, DWORD *out)
3342 DWORD dwSize = sizeof(DWORD);
3343 DWORD dwType;
3344 return HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE, registryPath, value, RRF_RT_DWORD, &dwType, out, &dwSize));
3348 * Internal function to get place where folder redirection information are stored.
3350 * Parameters:
3351 * rfid [I] pointer to known folder identifier (may be NULL)
3352 * rootKey [O] root key where the redirection information are stored
3353 * It can be HKLM for COMMON folders, and HKCU for PERUSER folders.
3354 * However, besides root key, path is always that same, and is stored
3355 * as "szKnownFolderRedirections" constant
3357 static HRESULT get_known_folder_redirection_place(
3358 REFKNOWNFOLDERID rfid,
3359 HKEY *rootKey)
3361 HRESULT hr;
3362 LPWSTR lpRegistryPath = NULL;
3363 KF_CATEGORY category;
3365 /* first, get known folder's category */
3366 hr = get_known_folder_registry_path(rfid, NULL, &lpRegistryPath);
3368 if(SUCCEEDED(hr))
3369 hr = get_known_folder_dword(lpRegistryPath, szCategory, &category);
3371 if(SUCCEEDED(hr))
3373 if(category == KF_CATEGORY_COMMON)
3375 *rootKey = HKEY_LOCAL_MACHINE;
3376 hr = S_OK;
3378 else if(category == KF_CATEGORY_PERUSER)
3380 *rootKey = HKEY_CURRENT_USER;
3381 hr = S_OK;
3383 else
3384 hr = E_FAIL;
3387 HeapFree(GetProcessHeap(), 0, lpRegistryPath);
3388 return hr;
3391 static HRESULT get_known_folder_path_by_id(REFKNOWNFOLDERID folderId, LPWSTR lpRegistryPath, DWORD dwFlags, LPWSTR *ppszPath);
3393 static HRESULT redirect_known_folder(
3394 REFKNOWNFOLDERID rfid,
3395 HWND hwnd,
3396 KF_REDIRECT_FLAGS flags,
3397 LPCWSTR pszTargetPath,
3398 UINT cFolders,
3399 KNOWNFOLDERID const *pExclusion,
3400 LPWSTR *ppszError)
3402 HRESULT hr;
3403 HKEY rootKey = HKEY_LOCAL_MACHINE, hKey;
3404 WCHAR sGuid[39];
3405 LPWSTR lpRegistryPath = NULL, lpSrcPath = NULL;
3406 TRACE("(%s, %p, 0x%08x, %s, %d, %p, %p)\n", debugstr_guid(rfid), hwnd, flags, debugstr_w(pszTargetPath), cFolders, pExclusion, ppszError);
3408 if (ppszError) *ppszError = NULL;
3410 hr = get_known_folder_registry_path(rfid, NULL, &lpRegistryPath);
3412 if(SUCCEEDED(hr))
3413 hr = get_known_folder_path_by_id(rfid, lpRegistryPath, 0, &lpSrcPath);
3415 HeapFree(GetProcessHeap(), 0, lpRegistryPath);
3417 /* get path to redirection storage */
3418 if(SUCCEEDED(hr))
3419 hr = get_known_folder_redirection_place(rfid, &rootKey);
3421 /* write redirection information */
3422 if(SUCCEEDED(hr))
3423 hr = HRESULT_FROM_WIN32(RegCreateKeyExW(rootKey, szKnownFolderRedirections, 0, NULL, 0, KEY_WRITE, NULL, &hKey, NULL));
3425 if(SUCCEEDED(hr))
3427 StringFromGUID2(rfid, sGuid, sizeof(sGuid)/sizeof(sGuid[0]));
3429 hr = HRESULT_FROM_WIN32(RegSetValueExW(hKey, sGuid, 0, REG_SZ, (LPBYTE)pszTargetPath, (lstrlenW(pszTargetPath)+1)*sizeof(WCHAR)));
3431 RegCloseKey(hKey);
3434 /* make sure destination path exists */
3435 SHCreateDirectory(NULL, pszTargetPath);
3437 /* copy content if required */
3438 if(SUCCEEDED(hr) && (flags & KF_REDIRECT_COPY_CONTENTS) )
3440 static const WCHAR sWildcard[] = {'\\','*',0};
3441 WCHAR srcPath[MAX_PATH+1], dstPath[MAX_PATH+1];
3442 SHFILEOPSTRUCTW fileOp;
3444 ZeroMemory(srcPath, sizeof(srcPath));
3445 lstrcpyW(srcPath, lpSrcPath);
3446 lstrcatW(srcPath, sWildcard);
3448 ZeroMemory(dstPath, sizeof(dstPath));
3449 lstrcpyW(dstPath, pszTargetPath);
3451 ZeroMemory(&fileOp, sizeof(fileOp));
3453 if(flags & KF_REDIRECT_DEL_SOURCE_CONTENTS)
3454 fileOp.wFunc = FO_MOVE;
3455 else
3456 fileOp.wFunc = FO_COPY;
3458 fileOp.pFrom = srcPath;
3459 fileOp.pTo = dstPath;
3460 fileOp.fFlags = FOF_NO_UI;
3462 hr = (SHFileOperationW(&fileOp)==0 ? S_OK : E_FAIL);
3464 if(flags & KF_REDIRECT_DEL_SOURCE_CONTENTS)
3466 ZeroMemory(srcPath, sizeof(srcPath));
3467 lstrcpyW(srcPath, lpSrcPath);
3469 ZeroMemory(&fileOp, sizeof(fileOp));
3470 fileOp.wFunc = FO_DELETE;
3471 fileOp.pFrom = srcPath;
3472 fileOp.fFlags = FOF_NO_UI;
3474 hr = (SHFileOperationW(&fileOp)==0 ? S_OK : E_FAIL);
3478 CoTaskMemFree(lpSrcPath);
3480 return hr;
3484 struct knownfolder
3486 IKnownFolder IKnownFolder_iface;
3487 LONG refs;
3488 KNOWNFOLDERID id;
3489 LPWSTR registryPath;
3492 static inline struct knownfolder *impl_from_IKnownFolder( IKnownFolder *iface )
3494 return CONTAINING_RECORD( iface, struct knownfolder, IKnownFolder_iface );
3497 static ULONG WINAPI knownfolder_AddRef(
3498 IKnownFolder *iface )
3500 struct knownfolder *knownfolder = impl_from_IKnownFolder( iface );
3501 return InterlockedIncrement( &knownfolder->refs );
3504 static ULONG WINAPI knownfolder_Release(
3505 IKnownFolder *iface )
3507 struct knownfolder *knownfolder = impl_from_IKnownFolder( iface );
3508 LONG refs = InterlockedDecrement( &knownfolder->refs );
3509 if (!refs)
3511 TRACE("destroying %p\n", knownfolder);
3512 HeapFree( GetProcessHeap(), 0, knownfolder->registryPath);
3513 HeapFree( GetProcessHeap(), 0, knownfolder );
3515 return refs;
3518 static HRESULT WINAPI knownfolder_QueryInterface(
3519 IKnownFolder *iface,
3520 REFIID riid,
3521 void **ppv )
3523 struct knownfolder *This = impl_from_IKnownFolder( iface );
3525 TRACE("%p %s %p\n", This, debugstr_guid( riid ), ppv );
3527 if ( IsEqualGUID( riid, &IID_IKnownFolder ) ||
3528 IsEqualGUID( riid, &IID_IUnknown ) )
3530 *ppv = iface;
3532 else
3534 FIXME("interface %s not implemented\n", debugstr_guid(riid));
3535 return E_NOINTERFACE;
3537 IKnownFolder_AddRef( iface );
3538 return S_OK;
3541 static HRESULT knownfolder_set_id(
3542 struct knownfolder *knownfolder,
3543 const KNOWNFOLDERID *kfid)
3545 HKEY hKey;
3546 HRESULT hr;
3548 TRACE("%s\n", debugstr_guid(kfid));
3550 knownfolder->id = *kfid;
3552 /* check is it registry-registered folder */
3553 hr = get_known_folder_registry_path(kfid, NULL, &knownfolder->registryPath);
3554 if(SUCCEEDED(hr))
3555 hr = HRESULT_FROM_WIN32(RegOpenKeyExW(HKEY_LOCAL_MACHINE, knownfolder->registryPath, 0, 0, &hKey));
3557 if(SUCCEEDED(hr))
3559 hr = S_OK;
3560 RegCloseKey(hKey);
3562 else
3564 /* This known folder is not registered. To mark it, we set registryPath to NULL */
3565 HeapFree(GetProcessHeap(), 0, knownfolder->registryPath);
3566 knownfolder->registryPath = NULL;
3567 hr = S_OK;
3570 return hr;
3573 static HRESULT WINAPI knownfolder_GetId(
3574 IKnownFolder *iface,
3575 KNOWNFOLDERID *pkfid)
3577 struct knownfolder *knownfolder = impl_from_IKnownFolder( iface );
3579 TRACE("%p\n", pkfid);
3581 *pkfid = knownfolder->id;
3582 return S_OK;
3585 static HRESULT WINAPI knownfolder_GetCategory(
3586 IKnownFolder *iface,
3587 KF_CATEGORY *pCategory)
3589 struct knownfolder *knownfolder = impl_from_IKnownFolder(iface);
3590 HRESULT hr = S_OK;
3592 TRACE("%p, %p\n", knownfolder, pCategory);
3594 /* we cannot get a category for a folder which is not registered */
3595 if(!knownfolder->registryPath)
3596 hr = E_FAIL;
3598 if(SUCCEEDED(hr))
3599 hr = get_known_folder_dword(knownfolder->registryPath, szCategory, pCategory);
3601 return hr;
3604 static HRESULT WINAPI knownfolder_GetShellItem(
3605 IKnownFolder *iface,
3606 DWORD dwFlags,
3607 REFIID riid,
3608 void **ppv)
3610 FIXME("0x%08x, %s, %p\n", dwFlags, debugstr_guid(riid), ppv);
3611 return E_NOTIMPL;
3614 static HRESULT get_known_folder_path(
3615 LPWSTR sFolderId,
3616 LPWSTR registryPath,
3617 LPWSTR *ppszPath)
3619 static const WCHAR sBackslash[] = {'\\',0};
3620 HRESULT hr;
3621 DWORD dwSize, dwType;
3622 WCHAR path[MAX_PATH] = {0};
3623 WCHAR parentGuid[39];
3624 KF_CATEGORY category;
3625 LPWSTR parentRegistryPath, parentPath;
3626 HKEY hRedirectionRootKey = NULL;
3628 TRACE("(%s, %p)\n", debugstr_w(registryPath), ppszPath);
3629 *ppszPath = NULL;
3631 /* check if folder has parent */
3632 dwSize = sizeof(parentGuid);
3633 hr = HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE, registryPath, szParentFolder, RRF_RT_REG_SZ, &dwType, parentGuid, &dwSize));
3634 if(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) hr = S_FALSE;
3636 if(hr == S_OK)
3638 /* get parent's known folder path (recursive) */
3639 hr = get_known_folder_registry_path(NULL, parentGuid, &parentRegistryPath);
3640 if(FAILED(hr)) return hr;
3642 hr = get_known_folder_path(parentGuid, parentRegistryPath, &parentPath);
3643 if(FAILED(hr)) {
3644 HeapFree(GetProcessHeap(), 0, parentRegistryPath);
3645 return hr;
3648 lstrcatW(path, parentPath);
3649 lstrcatW(path, sBackslash);
3651 HeapFree(GetProcessHeap(), 0, parentRegistryPath);
3652 HeapFree(GetProcessHeap(), 0, parentPath);
3655 /* check, if folder was redirected */
3656 if(SUCCEEDED(hr))
3657 hr = get_known_folder_dword(registryPath, szCategory, &category);
3659 if(SUCCEEDED(hr))
3661 if(category == KF_CATEGORY_COMMON)
3662 hRedirectionRootKey = HKEY_LOCAL_MACHINE;
3663 else if(category == KF_CATEGORY_PERUSER)
3664 hRedirectionRootKey = HKEY_CURRENT_USER;
3666 if(hRedirectionRootKey)
3668 hr = HRESULT_FROM_WIN32(RegGetValueW(hRedirectionRootKey, szKnownFolderRedirections, sFolderId, RRF_RT_REG_SZ, NULL, NULL, &dwSize));
3670 if(SUCCEEDED(hr))
3672 *ppszPath = CoTaskMemAlloc(dwSize+(lstrlenW(path)+1)*sizeof(WCHAR));
3673 if(!*ppszPath) hr = E_OUTOFMEMORY;
3676 if(SUCCEEDED(hr))
3678 lstrcpyW(*ppszPath, path);
3679 hr = HRESULT_FROM_WIN32(RegGetValueW(hRedirectionRootKey, szKnownFolderRedirections, sFolderId, RRF_RT_REG_SZ, NULL, *ppszPath + lstrlenW(path), &dwSize));
3683 if(!*ppszPath)
3685 /* no redirection, use previous way - read the relative path from folder definition */
3686 hr = HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE, registryPath, szRelativePath, RRF_RT_REG_SZ, &dwType, NULL, &dwSize));
3688 if(SUCCEEDED(hr))
3690 *ppszPath = CoTaskMemAlloc(dwSize+(lstrlenW(path)+1)*sizeof(WCHAR));
3691 if(!*ppszPath) hr = E_OUTOFMEMORY;
3694 if(SUCCEEDED(hr))
3696 lstrcpyW(*ppszPath, path);
3697 hr = HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE, registryPath, szRelativePath, RRF_RT_REG_SZ, &dwType, *ppszPath + lstrlenW(path), &dwSize));
3702 TRACE("returning path: %s\n", debugstr_w(*ppszPath));
3703 return hr;
3706 static HRESULT get_known_folder_path_by_id(
3707 REFKNOWNFOLDERID folderId,
3708 LPWSTR lpRegistryPath,
3709 DWORD dwFlags,
3710 LPWSTR *ppszPath)
3712 HRESULT hr = E_FAIL;
3713 WCHAR sGuid[39];
3714 DWORD dwAttributes;
3716 TRACE("(%s, %s, 0x%08x, %p)\n", debugstr_guid(folderId), debugstr_w(lpRegistryPath), dwFlags, ppszPath);
3718 /* if this is registry-registered known folder, get path from registry */
3719 if(lpRegistryPath)
3721 StringFromGUID2(folderId, sGuid, sizeof(sGuid)/sizeof(sGuid[0]));
3723 hr = get_known_folder_path(sGuid, lpRegistryPath, ppszPath);
3725 /* in other case, use older way */
3727 if(FAILED(hr))
3728 hr = SHGetKnownFolderPath( folderId, dwFlags, NULL, ppszPath );
3730 if (FAILED(hr)) return hr;
3732 /* check if known folder really exists */
3733 dwAttributes = GetFileAttributesW(*ppszPath);
3734 if(dwAttributes == INVALID_FILE_ATTRIBUTES || !(dwAttributes & FILE_ATTRIBUTE_DIRECTORY) )
3736 TRACE("directory %s not found\n", debugstr_w(*ppszPath));
3737 CoTaskMemFree(*ppszPath);
3738 *ppszPath = NULL;
3739 hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
3742 return hr;
3745 static HRESULT WINAPI knownfolder_GetPath(
3746 IKnownFolder *iface,
3747 DWORD dwFlags,
3748 LPWSTR *ppszPath)
3750 struct knownfolder *knownfolder = impl_from_IKnownFolder( iface );
3751 TRACE("(%p, 0x%08x, %p)\n", knownfolder, dwFlags, ppszPath);
3753 return get_known_folder_path_by_id(&knownfolder->id, knownfolder->registryPath, dwFlags, ppszPath);
3756 static HRESULT WINAPI knownfolder_SetPath(
3757 IKnownFolder *iface,
3758 DWORD dwFlags,
3759 LPCWSTR pszPath)
3761 struct knownfolder *knownfolder = impl_from_IKnownFolder( iface );
3762 HRESULT hr = S_OK;
3764 TRACE("(%p, 0x%08x, %s)\n", knownfolder, dwFlags, debugstr_w(pszPath));
3766 /* check if the known folder is registered */
3767 if(!knownfolder->registryPath)
3768 hr = E_FAIL;
3770 if(SUCCEEDED(hr))
3771 hr = redirect_known_folder(&knownfolder->id, NULL, 0, pszPath, 0, NULL, NULL);
3773 return hr;
3776 static HRESULT WINAPI knownfolder_GetIDList(
3777 IKnownFolder *iface,
3778 DWORD dwFlags,
3779 PIDLIST_ABSOLUTE *ppidl)
3781 FIXME("0x%08x, %p\n", dwFlags, ppidl);
3782 return E_NOTIMPL;
3785 static HRESULT WINAPI knownfolder_GetFolderType(
3786 IKnownFolder *iface,
3787 FOLDERTYPEID *pftid)
3789 FIXME("%p\n", pftid);
3790 return E_NOTIMPL;
3793 static HRESULT WINAPI knownfolder_GetRedirectionCapabilities(
3794 IKnownFolder *iface,
3795 KF_REDIRECTION_CAPABILITIES *pCapabilities)
3797 FIXME("%p\n", pCapabilities);
3798 return E_NOTIMPL;
3801 static HRESULT WINAPI knownfolder_GetFolderDefinition(
3802 IKnownFolder *iface,
3803 KNOWNFOLDER_DEFINITION *pKFD)
3805 struct knownfolder *knownfolder = impl_from_IKnownFolder( iface );
3806 HRESULT hr;
3807 DWORD dwSize;
3808 WCHAR parentGuid[39];
3809 TRACE("(%p, %p)\n", knownfolder, pKFD);
3811 if(!pKFD) return E_INVALIDARG;
3813 ZeroMemory(pKFD, sizeof(*pKFD));
3815 /* required fields */
3816 hr = get_known_folder_dword(knownfolder->registryPath, szCategory, &pKFD->category);
3817 if(FAILED(hr))
3818 return hr;
3820 hr = get_known_folder_wstr(knownfolder->registryPath, szName, &pKFD->pszName);
3821 if(FAILED(hr))
3822 return hr;
3824 /* optional fields */
3825 dwSize = sizeof(parentGuid);
3826 hr = HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE, knownfolder->registryPath, szParentFolder,
3827 RRF_RT_REG_SZ, NULL, parentGuid, &dwSize));
3828 if(SUCCEEDED(hr))
3829 IIDFromString(parentGuid, &pKFD->fidParent);
3831 get_known_folder_dword(knownfolder->registryPath, szAttributes, &pKFD->dwAttributes);
3833 get_known_folder_wstr(knownfolder->registryPath, szRelativePath, &pKFD->pszRelativePath);
3835 return S_OK;
3838 static const struct IKnownFolderVtbl knownfolder_vtbl =
3840 knownfolder_QueryInterface,
3841 knownfolder_AddRef,
3842 knownfolder_Release,
3843 knownfolder_GetId,
3844 knownfolder_GetCategory,
3845 knownfolder_GetShellItem,
3846 knownfolder_GetPath,
3847 knownfolder_SetPath,
3848 knownfolder_GetIDList,
3849 knownfolder_GetFolderType,
3850 knownfolder_GetRedirectionCapabilities,
3851 knownfolder_GetFolderDefinition
3854 static HRESULT knownfolder_create( struct knownfolder **knownfolder )
3856 struct knownfolder *kf;
3858 kf = HeapAlloc( GetProcessHeap(), 0, sizeof(*kf) );
3859 if (!kf) return E_OUTOFMEMORY;
3861 kf->IKnownFolder_iface.lpVtbl = &knownfolder_vtbl;
3862 kf->refs = 1;
3863 memset( &kf->id, 0, sizeof(kf->id) );
3864 kf->registryPath = NULL;
3866 *knownfolder = kf;
3868 TRACE("returning iface %p\n", &kf->IKnownFolder_iface);
3869 return S_OK;
3872 struct foldermanager
3874 IKnownFolderManager IKnownFolderManager_iface;
3875 LONG refs;
3876 UINT num_ids;
3877 KNOWNFOLDERID *ids;
3880 static inline struct foldermanager *impl_from_IKnownFolderManager( IKnownFolderManager *iface )
3882 return CONTAINING_RECORD( iface, struct foldermanager, IKnownFolderManager_iface );
3885 static ULONG WINAPI foldermanager_AddRef(
3886 IKnownFolderManager *iface )
3888 struct foldermanager *foldermanager = impl_from_IKnownFolderManager( iface );
3889 return InterlockedIncrement( &foldermanager->refs );
3892 static ULONG WINAPI foldermanager_Release(
3893 IKnownFolderManager *iface )
3895 struct foldermanager *foldermanager = impl_from_IKnownFolderManager( iface );
3896 LONG refs = InterlockedDecrement( &foldermanager->refs );
3897 if (!refs)
3899 TRACE("destroying %p\n", foldermanager);
3900 HeapFree( GetProcessHeap(), 0, foldermanager->ids );
3901 HeapFree( GetProcessHeap(), 0, foldermanager );
3903 return refs;
3906 static HRESULT WINAPI foldermanager_QueryInterface(
3907 IKnownFolderManager *iface,
3908 REFIID riid,
3909 void **ppv )
3911 struct foldermanager *This = impl_from_IKnownFolderManager( iface );
3913 TRACE("%p %s %p\n", This, debugstr_guid( riid ), ppv );
3915 if ( IsEqualGUID( riid, &IID_IKnownFolderManager ) ||
3916 IsEqualGUID( riid, &IID_IUnknown ) )
3918 *ppv = iface;
3920 else
3922 FIXME("interface %s not implemented\n", debugstr_guid(riid));
3923 return E_NOINTERFACE;
3925 IKnownFolderManager_AddRef( iface );
3926 return S_OK;
3929 static HRESULT WINAPI foldermanager_FolderIdFromCsidl(
3930 IKnownFolderManager *iface,
3931 int nCsidl,
3932 KNOWNFOLDERID *pfid)
3934 TRACE("%d, %p\n", nCsidl, pfid);
3936 if (nCsidl >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
3937 return E_INVALIDARG;
3938 *pfid = *CSIDL_Data[nCsidl].id;
3939 return S_OK;
3942 static HRESULT WINAPI foldermanager_FolderIdToCsidl(
3943 IKnownFolderManager *iface,
3944 REFKNOWNFOLDERID rfid,
3945 int *pnCsidl)
3947 int csidl;
3949 TRACE("%s, %p\n", debugstr_guid(rfid), pnCsidl);
3951 csidl = csidl_from_id( rfid );
3952 if (csidl == -1) return E_INVALIDARG;
3953 *pnCsidl = csidl;
3954 return S_OK;
3957 static HRESULT WINAPI foldermanager_GetFolderIds(
3958 IKnownFolderManager *iface,
3959 KNOWNFOLDERID **ppKFId,
3960 UINT *pCount)
3962 struct foldermanager *fm = impl_from_IKnownFolderManager( iface );
3964 TRACE("%p, %p\n", ppKFId, pCount);
3966 *ppKFId = fm->ids;
3967 *pCount = fm->num_ids;
3968 return S_OK;
3971 static BOOL is_knownfolder( struct foldermanager *fm, const KNOWNFOLDERID *id )
3973 UINT i;
3974 HRESULT hr;
3975 LPWSTR registryPath = NULL;
3976 HKEY hKey;
3978 /* TODO: move all entries from "CSIDL_Data" static array to registry known folder descriptions */
3979 for (i = 0; i < fm->num_ids; i++)
3980 if (IsEqualGUID( &fm->ids[i], id )) return TRUE;
3982 hr = get_known_folder_registry_path(id, NULL, &registryPath);
3983 if(SUCCEEDED(hr))
3985 hr = HRESULT_FROM_WIN32(RegOpenKeyExW(HKEY_LOCAL_MACHINE, registryPath, 0, 0, &hKey));
3986 HeapFree(GetProcessHeap(), 0, registryPath);
3989 if(SUCCEEDED(hr))
3991 hr = S_OK;
3992 RegCloseKey(hKey);
3995 return hr == S_OK;
3998 static HRESULT WINAPI foldermanager_GetFolder(
3999 IKnownFolderManager *iface,
4000 REFKNOWNFOLDERID rfid,
4001 IKnownFolder **ppkf)
4003 struct foldermanager *fm = impl_from_IKnownFolderManager( iface );
4004 struct knownfolder *kf;
4005 HRESULT hr;
4007 TRACE("%s, %p\n", debugstr_guid(rfid), ppkf);
4009 if (!is_knownfolder( fm, rfid ))
4011 WARN("unknown folder\n");
4012 return E_INVALIDARG;
4014 hr = knownfolder_create( &kf );
4015 if (SUCCEEDED( hr ))
4017 hr = knownfolder_set_id( kf, rfid );
4018 *ppkf = &kf->IKnownFolder_iface;
4020 else
4021 *ppkf = NULL;
4023 return hr;
4026 static HRESULT WINAPI foldermanager_GetFolderByName(
4027 IKnownFolderManager *iface,
4028 LPCWSTR pszCanonicalName,
4029 IKnownFolder **ppkf)
4031 FIXME("%s, %p\n", debugstr_w(pszCanonicalName), ppkf);
4032 return E_NOTIMPL;
4035 static HRESULT register_folder(const KNOWNFOLDERID *rfid, const KNOWNFOLDER_DEFINITION *pKFD)
4037 HRESULT hr;
4038 HKEY hKey = NULL;
4039 DWORD dwDisp;
4040 LPWSTR registryPath = NULL;
4042 hr = get_known_folder_registry_path(rfid, NULL, &registryPath);
4043 TRACE("registry path: %s\n", debugstr_w(registryPath));
4045 if(SUCCEEDED(hr))
4046 hr = HRESULT_FROM_WIN32(RegCreateKeyExW(HKEY_LOCAL_MACHINE, registryPath, 0, NULL, 0, KEY_WRITE, 0, &hKey, &dwDisp));
4048 if(SUCCEEDED(hr))
4050 if(SUCCEEDED(hr))
4051 hr = HRESULT_FROM_WIN32(RegSetValueExW(hKey, szCategory, 0, REG_DWORD, (LPBYTE)&pKFD->category, sizeof(pKFD->category)));
4053 if(SUCCEEDED(hr) && pKFD->dwAttributes != 0)
4054 hr = HRESULT_FROM_WIN32(RegSetValueExW(hKey, szAttributes, 0, REG_DWORD, (LPBYTE)&pKFD->dwAttributes, sizeof(pKFD->dwAttributes)));
4056 if(SUCCEEDED(hr))
4057 hr = HRESULT_FROM_WIN32(RegSetValueExW(hKey, szName, 0, REG_SZ, (LPBYTE)pKFD->pszName, (lstrlenW(pKFD->pszName)+1)*sizeof(WCHAR) ));
4059 if(SUCCEEDED(hr) && !IsEqualGUID(&pKFD->fidParent, &GUID_NULL))
4061 WCHAR sParentGuid[39];
4062 StringFromGUID2(&pKFD->fidParent, sParentGuid, sizeof(sParentGuid)/sizeof(sParentGuid[0]));
4064 /* this known folder has parent folder */
4065 hr = HRESULT_FROM_WIN32(RegSetValueExW(hKey, szParentFolder, 0, REG_SZ, (LPBYTE)sParentGuid, sizeof(sParentGuid)));
4068 if(SUCCEEDED(hr) && pKFD->category != KF_CATEGORY_VIRTUAL && pKFD->pszRelativePath)
4070 if(SUCCEEDED(hr))
4071 hr = HRESULT_FROM_WIN32(RegSetValueExW(hKey, szRelativePath, 0, REG_SZ, (LPBYTE)pKFD->pszRelativePath, (lstrlenW(pKFD->pszRelativePath)+1)*sizeof(WCHAR) ));
4074 RegCloseKey(hKey);
4076 if(FAILED(hr))
4077 SHDeleteKeyW(HKEY_LOCAL_MACHINE, registryPath);
4080 HeapFree(GetProcessHeap(), 0, registryPath);
4081 return hr;
4084 static HRESULT WINAPI foldermanager_RegisterFolder(
4085 IKnownFolderManager *iface,
4086 REFKNOWNFOLDERID rfid,
4087 KNOWNFOLDER_DEFINITION const *pKFD)
4089 TRACE("(%p, %s, %p)\n", iface, debugstr_guid(rfid), pKFD);
4090 return register_folder(rfid, pKFD);
4093 static HRESULT WINAPI foldermanager_UnregisterFolder(
4094 IKnownFolderManager *iface,
4095 REFKNOWNFOLDERID rfid)
4097 HRESULT hr;
4098 LPWSTR registryPath = NULL;
4099 TRACE("(%p, %s)\n", iface, debugstr_guid(rfid));
4101 hr = get_known_folder_registry_path(rfid, NULL, &registryPath);
4103 if(SUCCEEDED(hr))
4104 hr = HRESULT_FROM_WIN32(SHDeleteKeyW(HKEY_LOCAL_MACHINE, registryPath));
4106 HeapFree(GetProcessHeap(), 0, registryPath);
4107 return hr;
4110 static HRESULT WINAPI foldermanager_FindFolderFromPath(
4111 IKnownFolderManager *iface,
4112 LPCWSTR pszPath,
4113 FFFP_MODE mode,
4114 IKnownFolder **ppkf)
4116 FIXME("%s, 0x%08x, %p\n", debugstr_w(pszPath), mode, ppkf);
4117 return E_NOTIMPL;
4120 static HRESULT WINAPI foldermanager_FindFolderFromIDList(
4121 IKnownFolderManager *iface,
4122 PCIDLIST_ABSOLUTE pidl,
4123 IKnownFolder **ppkf)
4125 FIXME("%p, %p\n", pidl, ppkf);
4126 return E_NOTIMPL;
4129 static HRESULT WINAPI foldermanager_Redirect(
4130 IKnownFolderManager *iface,
4131 REFKNOWNFOLDERID rfid,
4132 HWND hwnd,
4133 KF_REDIRECT_FLAGS flags,
4134 LPCWSTR pszTargetPath,
4135 UINT cFolders,
4136 KNOWNFOLDERID const *pExclusion,
4137 LPWSTR *ppszError)
4139 return redirect_known_folder(rfid, hwnd, flags, pszTargetPath, cFolders, pExclusion, ppszError);
4142 static const struct IKnownFolderManagerVtbl foldermanager_vtbl =
4144 foldermanager_QueryInterface,
4145 foldermanager_AddRef,
4146 foldermanager_Release,
4147 foldermanager_FolderIdFromCsidl,
4148 foldermanager_FolderIdToCsidl,
4149 foldermanager_GetFolderIds,
4150 foldermanager_GetFolder,
4151 foldermanager_GetFolderByName,
4152 foldermanager_RegisterFolder,
4153 foldermanager_UnregisterFolder,
4154 foldermanager_FindFolderFromPath,
4155 foldermanager_FindFolderFromIDList,
4156 foldermanager_Redirect
4159 static HRESULT foldermanager_create( void **ppv )
4161 UINT i, j;
4162 struct foldermanager *fm;
4164 fm = HeapAlloc( GetProcessHeap(), 0, sizeof(*fm) );
4165 if (!fm) return E_OUTOFMEMORY;
4167 fm->IKnownFolderManager_iface.lpVtbl = &foldermanager_vtbl;
4168 fm->refs = 1;
4169 fm->num_ids = 0;
4171 for (i = 0; i < sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]); i++)
4173 if (!IsEqualGUID( CSIDL_Data[i].id, &GUID_NULL )) fm->num_ids++;
4175 fm->ids = HeapAlloc( GetProcessHeap(), 0, fm->num_ids * sizeof(KNOWNFOLDERID) );
4176 if (!fm->ids)
4178 HeapFree( GetProcessHeap(), 0, fm );
4179 return E_OUTOFMEMORY;
4181 for (i = j = 0; i < sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]); i++)
4183 if (!IsEqualGUID( CSIDL_Data[i].id, &GUID_NULL ))
4185 fm->ids[j] = *CSIDL_Data[i].id;
4186 j++;
4189 TRACE("found %u known folders\n", fm->num_ids);
4190 *ppv = &fm->IKnownFolderManager_iface;
4192 TRACE("returning iface %p\n", *ppv);
4193 return S_OK;
4196 HRESULT WINAPI KnownFolderManager_Constructor( IUnknown *punk, REFIID riid, void **ppv )
4198 TRACE("%p, %s, %p\n", punk, debugstr_guid(riid), ppv);
4200 if (!ppv)
4201 return E_POINTER;
4202 if (punk)
4203 return CLASS_E_NOAGGREGATION;
4205 return foldermanager_create( ppv );
4208 HRESULT WINAPI SHGetKnownFolderIDList(REFKNOWNFOLDERID rfid, DWORD flags, HANDLE token, PIDLIST_ABSOLUTE *pidl)
4210 FIXME("%s, 0x%08x, %p, %p\n", debugstr_guid(rfid), flags, token, pidl);
4211 return E_NOTIMPL;
4214 static void register_system_knownfolders(void)
4216 int i;
4217 for(i = 0; i < sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]); ++i){
4218 const CSIDL_DATA *folder = &CSIDL_Data[i];
4219 if(folder->pszName){
4220 KNOWNFOLDER_DEFINITION kfd;
4222 /* register_folder won't modify kfd, so cast away const instead of
4223 * reallocating */
4224 kfd.category = folder->category;
4225 kfd.pszName = (WCHAR*)folder->pszName;
4226 kfd.pszDescription = (WCHAR*)folder->pszDescription;
4227 memcpy(&kfd.fidParent, folder->fidParent, sizeof(KNOWNFOLDERID));
4228 kfd.pszRelativePath = (WCHAR*)folder->pszRelativePath;
4229 kfd.pszParsingName = (WCHAR*)folder->pszParsingName;
4230 kfd.pszTooltip = (WCHAR*)folder->pszTooltip;
4231 kfd.pszLocalizedName = (WCHAR*)folder->pszLocalizedName;
4232 kfd.pszIcon = (WCHAR*)folder->pszIcon;
4233 kfd.pszSecurity = (WCHAR*)folder->pszSecurity;
4234 kfd.dwAttributes = folder->dwAttributes;
4235 kfd.kfdFlags = folder->kfdFlags;
4236 memcpy(&kfd.ftidType, folder->ftidType, sizeof(FOLDERTYPEID));
4238 register_folder(folder->id, &kfd);
4243 HRESULT SHELL_RegisterShellFolders(void)
4245 HRESULT hr;
4247 /* Set up '$HOME' targeted symlinks for 'My Documents', 'My Pictures',
4248 * 'My Videos', 'My Music' and 'Desktop' in advance, so that the
4249 * _SHRegister*ShellFolders() functions will find everything nice and clean
4250 * and thus will not attempt to create them in the profile directory. */
4251 _SHCreateSymbolicLinks();
4253 hr = _SHRegisterUserShellFolders(TRUE);
4254 if (SUCCEEDED(hr))
4255 hr = _SHRegisterUserShellFolders(FALSE);
4256 if (SUCCEEDED(hr))
4257 hr = _SHRegisterCommonShellFolders();
4258 if (SUCCEEDED(hr))
4259 hr = create_extra_folders();
4260 if (SUCCEEDED(hr))
4261 hr = set_folder_attributes();
4262 if (SUCCEEDED(hr))
4263 register_system_knownfolders();
4264 return hr;