shell32: Create profile directories available in Vista and above.
[wine.git] / dlls / shell32 / shellpath.c
blobe36148350886d1207391afcb0195b6ba4fb96745
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 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'};
772 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'};
773 static const WCHAR AppDataW[] = {'A','p','p','D','a','t','a','\0'};
774 static const WCHAR AppData_LocalLowW[] = {'A','p','p','D','a','t','a','\\','L','o','c','a','l','L','o','w','\0'};
775 static const WCHAR Application_DataW[] = {'A','p','p','l','i','c','a','t','i','o','n',' ','D','a','t','a','\0'};
776 static const WCHAR CacheW[] = {'C','a','c','h','e','\0'};
777 static const WCHAR CD_BurningW[] = {'C','D',' ','B','u','r','n','i','n','g','\0'};
778 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'};
779 static const WCHAR Common_AppDataW[] = {'C','o','m','m','o','n',' ','A','p','p','D','a','t','a','\0'};
780 static const WCHAR Common_DesktopW[] = {'C','o','m','m','o','n',' ','D','e','s','k','t','o','p','\0'};
781 static const WCHAR Common_DocumentsW[] = {'C','o','m','m','o','n',' ','D','o','c','u','m','e','n','t','s','\0'};
782 static const WCHAR Common_FavoritesW[] = {'C','o','m','m','o','n',' ','F','a','v','o','r','i','t','e','s','\0'};
783 static const WCHAR CommonFilesDirW[] = {'C','o','m','m','o','n','F','i','l','e','s','D','i','r','\0'};
784 static const WCHAR CommonFilesDirX86W[] = {'C','o','m','m','o','n','F','i','l','e','s','D','i','r',' ','(','x','8','6',')','\0'};
785 static const WCHAR CommonMusicW[] = {'C','o','m','m','o','n','M','u','s','i','c','\0'};
786 static const WCHAR CommonPicturesW[] = {'C','o','m','m','o','n','P','i','c','t','u','r','e','s','\0'};
787 static const WCHAR Common_ProgramsW[] = {'C','o','m','m','o','n',' ','P','r','o','g','r','a','m','s','\0'};
788 static const WCHAR Common_StartUpW[] = {'C','o','m','m','o','n',' ','S','t','a','r','t','U','p','\0'};
789 static const WCHAR Common_Start_MenuW[] = {'C','o','m','m','o','n',' ','S','t','a','r','t',' ','M','e','n','u','\0'};
790 static const WCHAR Common_TemplatesW[] = {'C','o','m','m','o','n',' ','T','e','m','p','l','a','t','e','s','\0'};
791 static const WCHAR CommonVideoW[] = {'C','o','m','m','o','n','V','i','d','e','o','\0'};
792 static const WCHAR ContactsW[] = {'C','o','n','t','a','c','t','s','\0'};
793 static const WCHAR CookiesW[] = {'C','o','o','k','i','e','s','\0'};
794 static const WCHAR DesktopW[] = {'D','e','s','k','t','o','p','\0'};
795 static const WCHAR DocumentsW[] = {'D','o','c','u','m','e','n','t','s','\0'};
796 static const WCHAR DownloadsW[] = {'D','o','w','n','l','o','a','d','s','\0'};
797 static const WCHAR FavoritesW[] = {'F','a','v','o','r','i','t','e','s','\0'};
798 static const WCHAR FontsW[] = {'F','o','n','t','s','\0'};
799 static const WCHAR HistoryW[] = {'H','i','s','t','o','r','y','\0'};
800 static const WCHAR LinksW[] = {'L','i','n','k','s','\0'};
801 static const WCHAR Local_AppDataW[] = {'L','o','c','a','l',' ','A','p','p','D','a','t','a','\0'};
802 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'};
803 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'};
804 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'};
805 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'};
806 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'};
807 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'};
808 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'};
809 static const WCHAR MusicW[] = {'M','u','s','i','c','\0'};
810 static const WCHAR Music_PlaylistsW[] = {'M','u','s','i','c','\\','P','l','a','y','l','i','s','t','s','\0'};
811 static const WCHAR Music_Sample_MusicW[] = {'M','u','s','i','c','\\','S','a','m','p','l','e',' ','M','u','s','i','c','\0'};
812 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'};
813 static const WCHAR My_DocumentsW[] = {'M','y',' ','D','o','c','u','m','e','n','t','s','\0'};
814 static const WCHAR My_MusicW[] = {'M','y',' ','M','u','s','i','c','\0'};
815 static const WCHAR My_PicturesW[] = {'M','y',' ','P','i','c','t','u','r','e','s','\0'};
816 static const WCHAR My_VideosW[] = {'M','y',' ','V','i','d','e','o','s','\0'};
817 static const WCHAR NetHoodW[] = {'N','e','t','H','o','o','d','\0'};
818 static const WCHAR OEM_LinksW[] = {'O','E','M',' ','L','i','n','k','s','\0'};
819 static const WCHAR PersonalW[] = {'P','e','r','s','o','n','a','l','\0'};
820 static const WCHAR PicturesW[] = {'P','i','c','t','u','r','e','s','\0'};
821 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'};
822 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'};
823 static const WCHAR PrintHoodW[] = {'P','r','i','n','t','H','o','o','d','\0'};
824 static const WCHAR Program_FilesW[] = {'P','r','o','g','r','a','m',' ','F','i','l','e','s','\0'};
825 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'};
826 static const WCHAR Program_Files_x86W[] = {'P','r','o','g','r','a','m',' ','F','i','l','e','s',' ','(','x','8','6',')','\0'};
827 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'};
828 static const WCHAR ProgramFilesDirW[] = {'P','r','o','g','r','a','m','F','i','l','e','s','D','i','r','\0'};
829 static const WCHAR ProgramFilesDirX86W[] = {'P','r','o','g','r','a','m','F','i','l','e','s','D','i','r',' ','(','x','8','6',')','\0'};
830 static const WCHAR ProgramsW[] = {'P','r','o','g','r','a','m','s','\0'};
831 static const WCHAR RecentW[] = {'R','e','c','e','n','t','\0'};
832 static const WCHAR ResourcesW[] = {'R','e','s','o','u','r','c','e','s','\0'};
833 static const WCHAR Saved_GamesW[] = {'S','a','v','e','d',' ','G','a','m','e','s','\0'};
834 static const WCHAR SearchesW[] = {'S','e','a','r','c','h','e','s','\0'};
835 static const WCHAR SendToW[] = {'S','e','n','d','T','o','\0'};
836 static const WCHAR StartUpW[] = {'S','t','a','r','t','U','p','\0'};
837 static const WCHAR Start_MenuW[] = {'S','t','a','r','t',' ','M','e','n','u','\0'};
838 static const WCHAR Start_Menu_ProgramsW[] = {'S','t','a','r','t',' ','M','e','n','u','\\','P','r','o','g','r','a','m','s','\0'};
839 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'};
840 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'};
841 static const WCHAR TemplatesW[] = {'T','e','m','p','l','a','t','e','s','\0'};
842 static const WCHAR UsersW[] = {'U','s','e','r','s','\0'};
843 static const WCHAR UsersPublicW[] = {'U','s','e','r','s','\\','P','u','b','l','i','c','\0'};
844 static const WCHAR VideosW[] = {'V','i','d','e','o','s','\0'};
845 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'};
846 static const WCHAR DefaultW[] = {'.','D','e','f','a','u','l','t','\0'};
847 static const WCHAR AllUsersProfileW[] = {'%','A','L','L','U','S','E','R','S','P','R','O','F','I','L','E','%','\0'};
848 static const WCHAR UserProfileW[] = {'%','U','S','E','R','P','R','O','F','I','L','E','%','\0'};
849 static const WCHAR SystemDriveW[] = {'%','S','y','s','t','e','m','D','r','i','v','e','%','\0'};
850 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};
851 static const WCHAR ProfilesDirectoryW[] = {'P','r','o','f','i','l','e','s','D','i','r','e','c','t','o','r','y',0};
852 static const WCHAR AllUsersProfileValueW[] = {'A','l','l','U','s','e','r','s','P','r','o','f','i','l','e','\0'};
853 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'};
854 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'};
855 static const WCHAR szDefaultProfileDirW[] = {'u','s','e','r','s',0};
856 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'};
857 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};
858 static const WCHAR AllUsersW[] = {'P','u','b','l','i','c',0};
860 typedef enum _CSIDL_Type {
861 CSIDL_Type_User,
862 CSIDL_Type_AllUsers,
863 CSIDL_Type_CurrVer,
864 CSIDL_Type_Disallowed,
865 CSIDL_Type_NonExistent,
866 CSIDL_Type_WindowsPath,
867 CSIDL_Type_SystemPath,
868 CSIDL_Type_SystemX86Path,
869 } CSIDL_Type;
871 #define CSIDL_CONTACTS 0x0043
872 #define CSIDL_DOWNLOADS 0x0047
873 #define CSIDL_LINKS 0x004d
874 #define CSIDL_APPDATA_LOCALLOW 0x004e
875 #define CSIDL_SAVED_GAMES 0x0062
876 #define CSIDL_SEARCHES 0x0063
878 typedef struct
880 const KNOWNFOLDERID *id;
881 CSIDL_Type type;
882 LPCWSTR szValueName;
883 LPCWSTR szDefaultPath; /* fallback string or resource ID */
884 } CSIDL_DATA;
886 static const CSIDL_DATA CSIDL_Data[] =
888 { /* 0x00 - CSIDL_DESKTOP */
889 &FOLDERID_Desktop,
890 CSIDL_Type_User,
891 DesktopW,
892 MAKEINTRESOURCEW(IDS_DESKTOPDIRECTORY)
894 { /* 0x01 - CSIDL_INTERNET */
895 &FOLDERID_InternetFolder,
896 CSIDL_Type_Disallowed,
897 NULL,
898 NULL
900 { /* 0x02 - CSIDL_PROGRAMS */
901 &FOLDERID_Programs,
902 CSIDL_Type_User,
903 ProgramsW,
904 Start_Menu_ProgramsW
906 { /* 0x03 - CSIDL_CONTROLS (.CPL files) */
907 &FOLDERID_ControlPanelFolder,
908 CSIDL_Type_SystemPath,
909 NULL,
910 NULL
912 { /* 0x04 - CSIDL_PRINTERS */
913 &FOLDERID_PrintersFolder,
914 CSIDL_Type_SystemPath,
915 NULL,
916 NULL
918 { /* 0x05 - CSIDL_PERSONAL */
919 &FOLDERID_Documents,
920 CSIDL_Type_User,
921 PersonalW,
922 MAKEINTRESOURCEW(IDS_PERSONAL)
924 { /* 0x06 - CSIDL_FAVORITES */
925 &FOLDERID_Favorites,
926 CSIDL_Type_User,
927 FavoritesW,
928 FavoritesW
930 { /* 0x07 - CSIDL_STARTUP */
931 &FOLDERID_Startup,
932 CSIDL_Type_User,
933 StartUpW,
934 Start_Menu_StartupW
936 { /* 0x08 - CSIDL_RECENT */
937 &FOLDERID_Recent,
938 CSIDL_Type_User,
939 RecentW,
940 RecentW
942 { /* 0x09 - CSIDL_SENDTO */
943 &FOLDERID_SendTo,
944 CSIDL_Type_User,
945 SendToW,
946 SendToW
948 { /* 0x0a - CSIDL_BITBUCKET - Recycle Bin */
949 &FOLDERID_RecycleBinFolder,
950 CSIDL_Type_Disallowed,
951 NULL,
952 NULL,
954 { /* 0x0b - CSIDL_STARTMENU */
955 &FOLDERID_StartMenu,
956 CSIDL_Type_User,
957 Start_MenuW,
958 Start_MenuW
960 { /* 0x0c - CSIDL_MYDOCUMENTS */
961 &GUID_NULL,
962 CSIDL_Type_Disallowed, /* matches WinXP--can't get its path */
963 NULL,
964 NULL
966 { /* 0x0d - CSIDL_MYMUSIC */
967 &FOLDERID_Music,
968 CSIDL_Type_User,
969 My_MusicW,
970 MAKEINTRESOURCEW(IDS_MYMUSIC)
972 { /* 0x0e - CSIDL_MYVIDEO */
973 &FOLDERID_Videos,
974 CSIDL_Type_User,
975 My_VideosW,
976 MAKEINTRESOURCEW(IDS_MYVIDEOS)
978 { /* 0x0f - unassigned */
979 &GUID_NULL,
980 CSIDL_Type_Disallowed,
981 NULL,
982 NULL,
984 { /* 0x10 - CSIDL_DESKTOPDIRECTORY */
985 &FOLDERID_Desktop,
986 CSIDL_Type_User,
987 DesktopW,
988 MAKEINTRESOURCEW(IDS_DESKTOPDIRECTORY)
990 { /* 0x11 - CSIDL_DRIVES */
991 &FOLDERID_ComputerFolder,
992 CSIDL_Type_Disallowed,
993 NULL,
994 NULL,
996 { /* 0x12 - CSIDL_NETWORK */
997 &FOLDERID_NetworkFolder,
998 CSIDL_Type_Disallowed,
999 NULL,
1000 NULL,
1002 { /* 0x13 - CSIDL_NETHOOD */
1003 &FOLDERID_NetHood,
1004 CSIDL_Type_User,
1005 NetHoodW,
1006 NetHoodW
1008 { /* 0x14 - CSIDL_FONTS */
1009 &FOLDERID_Fonts,
1010 CSIDL_Type_WindowsPath,
1011 FontsW,
1012 FontsW
1014 { /* 0x15 - CSIDL_TEMPLATES */
1015 &FOLDERID_Templates,
1016 CSIDL_Type_User,
1017 TemplatesW,
1018 TemplatesW
1020 { /* 0x16 - CSIDL_COMMON_STARTMENU */
1021 &FOLDERID_CommonStartMenu,
1022 CSIDL_Type_AllUsers,
1023 Common_Start_MenuW,
1024 Start_MenuW
1026 { /* 0x17 - CSIDL_COMMON_PROGRAMS */
1027 &FOLDERID_CommonPrograms,
1028 CSIDL_Type_AllUsers,
1029 Common_ProgramsW,
1030 Start_Menu_ProgramsW
1032 { /* 0x18 - CSIDL_COMMON_STARTUP */
1033 &FOLDERID_CommonStartup,
1034 CSIDL_Type_AllUsers,
1035 Common_StartUpW,
1036 Start_Menu_StartupW
1038 { /* 0x19 - CSIDL_COMMON_DESKTOPDIRECTORY */
1039 &FOLDERID_PublicDesktop,
1040 CSIDL_Type_AllUsers,
1041 Common_DesktopW,
1042 MAKEINTRESOURCEW(IDS_DESKTOPDIRECTORY)
1044 { /* 0x1a - CSIDL_APPDATA */
1045 &FOLDERID_RoamingAppData,
1046 CSIDL_Type_User,
1047 AppDataW,
1048 Application_DataW
1050 { /* 0x1b - CSIDL_PRINTHOOD */
1051 &FOLDERID_PrintHood,
1052 CSIDL_Type_User,
1053 PrintHoodW,
1054 PrintHoodW
1056 { /* 0x1c - CSIDL_LOCAL_APPDATA */
1057 &FOLDERID_LocalAppData,
1058 CSIDL_Type_User,
1059 Local_AppDataW,
1060 Local_Settings_Application_DataW
1062 { /* 0x1d - CSIDL_ALTSTARTUP */
1063 &GUID_NULL,
1064 CSIDL_Type_NonExistent,
1065 NULL,
1066 NULL
1068 { /* 0x1e - CSIDL_COMMON_ALTSTARTUP */
1069 &GUID_NULL,
1070 CSIDL_Type_NonExistent,
1071 NULL,
1072 NULL
1074 { /* 0x1f - CSIDL_COMMON_FAVORITES */
1075 &FOLDERID_Favorites,
1076 CSIDL_Type_AllUsers,
1077 Common_FavoritesW,
1078 FavoritesW
1080 { /* 0x20 - CSIDL_INTERNET_CACHE */
1081 &FOLDERID_InternetCache,
1082 CSIDL_Type_User,
1083 CacheW,
1084 Local_Settings_Temporary_Internet_FilesW
1086 { /* 0x21 - CSIDL_COOKIES */
1087 &FOLDERID_Cookies,
1088 CSIDL_Type_User,
1089 CookiesW,
1090 CookiesW
1092 { /* 0x22 - CSIDL_HISTORY */
1093 &FOLDERID_History,
1094 CSIDL_Type_User,
1095 HistoryW,
1096 Local_Settings_HistoryW
1098 { /* 0x23 - CSIDL_COMMON_APPDATA */
1099 &FOLDERID_ProgramData,
1100 CSIDL_Type_AllUsers,
1101 Common_AppDataW,
1102 Application_DataW
1104 { /* 0x24 - CSIDL_WINDOWS */
1105 &FOLDERID_Windows,
1106 CSIDL_Type_WindowsPath,
1107 NULL,
1108 NULL
1110 { /* 0x25 - CSIDL_SYSTEM */
1111 &FOLDERID_System,
1112 CSIDL_Type_SystemPath,
1113 NULL,
1114 NULL
1116 { /* 0x26 - CSIDL_PROGRAM_FILES */
1117 &FOLDERID_ProgramFiles,
1118 CSIDL_Type_CurrVer,
1119 ProgramFilesDirW,
1120 Program_FilesW
1122 { /* 0x27 - CSIDL_MYPICTURES */
1123 &FOLDERID_Pictures,
1124 CSIDL_Type_User,
1125 My_PicturesW,
1126 MAKEINTRESOURCEW(IDS_MYPICTURES)
1128 { /* 0x28 - CSIDL_PROFILE */
1129 &FOLDERID_Profile,
1130 CSIDL_Type_User,
1131 NULL,
1132 NULL
1134 { /* 0x29 - CSIDL_SYSTEMX86 */
1135 &FOLDERID_SystemX86,
1136 CSIDL_Type_SystemX86Path,
1137 NULL,
1138 NULL
1140 { /* 0x2a - CSIDL_PROGRAM_FILESX86 */
1141 &FOLDERID_ProgramFilesX86,
1142 CSIDL_Type_CurrVer,
1143 ProgramFilesDirX86W,
1144 Program_Files_x86W
1146 { /* 0x2b - CSIDL_PROGRAM_FILES_COMMON */
1147 &FOLDERID_ProgramFilesCommon,
1148 CSIDL_Type_CurrVer,
1149 CommonFilesDirW,
1150 Program_Files_Common_FilesW
1152 { /* 0x2c - CSIDL_PROGRAM_FILES_COMMONX86 */
1153 &FOLDERID_ProgramFilesCommonX86,
1154 CSIDL_Type_CurrVer,
1155 CommonFilesDirX86W,
1156 Program_Files_x86_Common_FilesW
1158 { /* 0x2d - CSIDL_COMMON_TEMPLATES */
1159 &FOLDERID_CommonTemplates,
1160 CSIDL_Type_AllUsers,
1161 Common_TemplatesW,
1162 TemplatesW
1164 { /* 0x2e - CSIDL_COMMON_DOCUMENTS */
1165 &FOLDERID_PublicDocuments,
1166 CSIDL_Type_AllUsers,
1167 Common_DocumentsW,
1168 DocumentsW
1170 { /* 0x2f - CSIDL_COMMON_ADMINTOOLS */
1171 &FOLDERID_CommonAdminTools,
1172 CSIDL_Type_AllUsers,
1173 Common_Administrative_ToolsW,
1174 Start_Menu_Admin_ToolsW
1176 { /* 0x30 - CSIDL_ADMINTOOLS */
1177 &FOLDERID_AdminTools,
1178 CSIDL_Type_User,
1179 Administrative_ToolsW,
1180 Start_Menu_Admin_ToolsW
1182 { /* 0x31 - CSIDL_CONNECTIONS */
1183 &FOLDERID_ConnectionsFolder,
1184 CSIDL_Type_Disallowed,
1185 NULL,
1186 NULL
1188 { /* 0x32 - unassigned */
1189 &GUID_NULL,
1190 CSIDL_Type_Disallowed,
1191 NULL,
1192 NULL
1194 { /* 0x33 - unassigned */
1195 &GUID_NULL,
1196 CSIDL_Type_Disallowed,
1197 NULL,
1198 NULL
1200 { /* 0x34 - unassigned */
1201 &GUID_NULL,
1202 CSIDL_Type_Disallowed,
1203 NULL,
1204 NULL
1206 { /* 0x35 - CSIDL_COMMON_MUSIC */
1207 &FOLDERID_PublicMusic,
1208 CSIDL_Type_AllUsers,
1209 CommonMusicW,
1210 MusicW
1212 { /* 0x36 - CSIDL_COMMON_PICTURES */
1213 &FOLDERID_PublicPictures,
1214 CSIDL_Type_AllUsers,
1215 CommonPicturesW,
1216 PicturesW
1218 { /* 0x37 - CSIDL_COMMON_VIDEO */
1219 &FOLDERID_PublicVideos,
1220 CSIDL_Type_AllUsers,
1221 CommonVideoW,
1222 VideosW
1224 { /* 0x38 - CSIDL_RESOURCES */
1225 &FOLDERID_ResourceDir,
1226 CSIDL_Type_WindowsPath,
1227 NULL,
1228 ResourcesW
1230 { /* 0x39 - CSIDL_RESOURCES_LOCALIZED */
1231 &FOLDERID_LocalizedResourcesDir,
1232 CSIDL_Type_NonExistent,
1233 NULL,
1234 NULL
1236 { /* 0x3a - CSIDL_COMMON_OEM_LINKS */
1237 &FOLDERID_CommonOEMLinks,
1238 CSIDL_Type_AllUsers,
1239 NULL,
1240 OEM_LinksW
1242 { /* 0x3b - CSIDL_CDBURN_AREA */
1243 &FOLDERID_CDBurning,
1244 CSIDL_Type_User,
1245 CD_BurningW,
1246 Local_Settings_CD_BurningW
1248 { /* 0x3c unassigned */
1249 &GUID_NULL,
1250 CSIDL_Type_Disallowed,
1251 NULL,
1252 NULL
1254 { /* 0x3d - CSIDL_COMPUTERSNEARME */
1255 &GUID_NULL,
1256 CSIDL_Type_Disallowed, /* FIXME */
1257 NULL,
1258 NULL
1260 { /* 0x3e - CSIDL_PROFILES */
1261 &GUID_NULL,
1262 CSIDL_Type_Disallowed, /* oddly, this matches WinXP */
1263 NULL,
1264 NULL
1266 { /* 0x3f */
1267 &FOLDERID_AddNewPrograms,
1268 CSIDL_Type_Disallowed,
1269 NULL,
1270 NULL
1272 { /* 0x40 */
1273 &FOLDERID_AppUpdates,
1274 CSIDL_Type_Disallowed,
1275 NULL,
1276 NULL
1278 { /* 0x41 */
1279 &FOLDERID_ChangeRemovePrograms,
1280 CSIDL_Type_Disallowed,
1281 NULL,
1282 NULL
1284 { /* 0x42 */
1285 &FOLDERID_ConflictFolder,
1286 CSIDL_Type_Disallowed,
1287 NULL,
1288 NULL
1290 { /* 0x43 - CSIDL_CONTACTS */
1291 &FOLDERID_Contacts,
1292 CSIDL_Type_User,
1293 NULL,
1294 ContactsW
1296 { /* 0x44 */
1297 &FOLDERID_DeviceMetadataStore,
1298 CSIDL_Type_Disallowed, /* FIXME */
1299 NULL,
1300 NULL
1302 { /* 0x45 */
1303 &GUID_NULL,
1304 CSIDL_Type_User,
1305 NULL,
1306 DocumentsW
1308 { /* 0x46 */
1309 &FOLDERID_DocumentsLibrary,
1310 CSIDL_Type_Disallowed, /* FIXME */
1311 NULL,
1312 NULL
1314 { /* 0x47 - CSIDL_DOWNLOADS */
1315 &FOLDERID_Downloads,
1316 CSIDL_Type_User,
1317 NULL,
1318 DownloadsW
1320 { /* 0x48 */
1321 &FOLDERID_Games,
1322 CSIDL_Type_Disallowed,
1323 NULL,
1324 NULL
1326 { /* 0x49 */
1327 &FOLDERID_GameTasks,
1328 CSIDL_Type_Disallowed, /* FIXME */
1329 NULL,
1330 NULL
1332 { /* 0x4a */
1333 &FOLDERID_HomeGroup,
1334 CSIDL_Type_Disallowed,
1335 NULL,
1336 NULL
1338 { /* 0x4b */
1339 &FOLDERID_ImplicitAppShortcuts,
1340 CSIDL_Type_Disallowed, /* FIXME */
1341 NULL,
1342 NULL
1344 { /* 0x4c */
1345 &FOLDERID_Libraries,
1346 CSIDL_Type_Disallowed, /* FIXME */
1347 NULL,
1348 NULL
1350 { /* 0x4d - CSIDL_LINKS */
1351 &FOLDERID_Links,
1352 CSIDL_Type_User,
1353 NULL,
1354 LinksW
1356 { /* 0x4e - CSIDL_APPDATA_LOCALLOW */
1357 &FOLDERID_LocalAppDataLow,
1358 CSIDL_Type_User,
1359 NULL,
1360 AppData_LocalLowW
1362 { /* 0x4f */
1363 &FOLDERID_MusicLibrary,
1364 CSIDL_Type_Disallowed, /* FIXME */
1365 NULL,
1366 NULL
1368 { /* 0x50 */
1369 &FOLDERID_OriginalImages,
1370 CSIDL_Type_Disallowed, /* FIXME */
1371 NULL,
1372 NULL
1374 { /* 0x51 */
1375 &FOLDERID_PhotoAlbums,
1376 CSIDL_Type_User,
1377 NULL,
1378 Pictures_Slide_ShowsW
1380 { /* 0x52 */
1381 &FOLDERID_PicturesLibrary,
1382 CSIDL_Type_Disallowed, /* FIXME */
1383 NULL,
1384 NULL
1386 { /* 0x53 */
1387 &FOLDERID_Playlists,
1388 CSIDL_Type_User,
1389 NULL,
1390 Music_PlaylistsW
1392 { /* 0x54 */
1393 &FOLDERID_ProgramFilesX64,
1394 CSIDL_Type_NonExistent,
1395 NULL,
1396 NULL
1398 { /* 0x55 */
1399 &FOLDERID_ProgramFilesCommonX64,
1400 CSIDL_Type_NonExistent,
1401 NULL,
1402 NULL
1404 { /* 0x56 */
1405 &FOLDERID_Public,
1406 CSIDL_Type_CurrVer, /* FIXME */
1407 NULL,
1408 UsersPublicW
1410 { /* 0x57 */
1411 &FOLDERID_PublicDownloads,
1412 CSIDL_Type_AllUsers,
1413 NULL,
1414 DownloadsW
1416 { /* 0x58 */
1417 &FOLDERID_PublicGameTasks,
1418 CSIDL_Type_AllUsers,
1419 NULL,
1420 Microsoft_Windows_GameExplorerW
1422 { /* 0x59 */
1423 &FOLDERID_PublicLibraries,
1424 CSIDL_Type_AllUsers,
1425 NULL,
1426 Microsoft_Windows_LibrariesW
1428 { /* 0x5a */
1429 &FOLDERID_PublicRingtones,
1430 CSIDL_Type_AllUsers,
1431 NULL,
1432 Microsoft_Windows_RingtonesW
1434 { /* 0x5b */
1435 &FOLDERID_QuickLaunch,
1436 CSIDL_Type_Disallowed, /* FIXME */
1437 NULL,
1438 NULL
1440 { /* 0x5c */
1441 &FOLDERID_RecordedTVLibrary,
1442 CSIDL_Type_Disallowed, /* FIXME */
1443 NULL,
1444 NULL
1446 { /* 0x5d */
1447 &FOLDERID_Ringtones,
1448 CSIDL_Type_Disallowed, /* FIXME */
1449 NULL,
1450 NULL
1452 { /* 0x5e */
1453 &FOLDERID_SampleMusic,
1454 CSIDL_Type_AllUsers,
1455 NULL,
1456 Music_Sample_MusicW
1458 { /* 0x5f */
1459 &FOLDERID_SamplePictures,
1460 CSIDL_Type_AllUsers,
1461 NULL,
1462 Pictures_Sample_PicturesW
1464 { /* 0x60 */
1465 &FOLDERID_SamplePlaylists,
1466 CSIDL_Type_AllUsers,
1467 NULL,
1468 Music_Sample_PlaylistsW
1470 { /* 0x61 */
1471 &FOLDERID_SampleVideos,
1472 CSIDL_Type_AllUsers,
1473 NULL,
1474 Videos_Sample_VideosW
1476 { /* 0x62 - CSIDL_SAVED_GAMES */
1477 &FOLDERID_SavedGames,
1478 CSIDL_Type_User,
1479 NULL,
1480 Saved_GamesW
1482 { /* 0x63 - CSIDL_SEARCHES */
1483 &FOLDERID_SavedSearches,
1484 CSIDL_Type_User,
1485 NULL,
1486 SearchesW
1488 { /* 0x64 */
1489 &FOLDERID_SEARCH_CSC,
1490 CSIDL_Type_Disallowed,
1491 NULL,
1492 NULL
1494 { /* 0x65 */
1495 &FOLDERID_SEARCH_MAPI,
1496 CSIDL_Type_Disallowed,
1497 NULL,
1498 NULL
1500 { /* 0x66 */
1501 &FOLDERID_SearchHome,
1502 CSIDL_Type_Disallowed,
1503 NULL,
1504 NULL
1506 { /* 0x67 */
1507 &FOLDERID_SidebarDefaultParts,
1508 CSIDL_Type_Disallowed, /* FIXME */
1509 NULL,
1510 NULL
1512 { /* 0x68 */
1513 &FOLDERID_SidebarParts,
1514 CSIDL_Type_Disallowed, /* FIXME */
1515 NULL,
1516 NULL
1518 { /* 0x69 */
1519 &FOLDERID_SyncManagerFolder,
1520 CSIDL_Type_Disallowed,
1521 NULL,
1522 NULL
1524 { /* 0x6a */
1525 &FOLDERID_SyncResultsFolder,
1526 CSIDL_Type_Disallowed,
1527 NULL,
1528 NULL
1530 { /* 0x6b */
1531 &FOLDERID_SyncSetupFolder,
1532 CSIDL_Type_Disallowed,
1533 NULL,
1534 NULL
1536 { /* 0x6c */
1537 &FOLDERID_UserPinned,
1538 CSIDL_Type_Disallowed, /* FIXME */
1539 NULL,
1540 NULL
1542 { /* 0x6d */
1543 &FOLDERID_UserProfiles,
1544 CSIDL_Type_CurrVer,
1545 UsersW,
1546 UsersW
1548 { /* 0x6e */
1549 &FOLDERID_UserProgramFiles,
1550 CSIDL_Type_Disallowed, /* FIXME */
1551 NULL,
1552 NULL
1554 { /* 0x6f */
1555 &FOLDERID_UserProgramFilesCommon,
1556 CSIDL_Type_Disallowed, /* FIXME */
1557 NULL,
1558 NULL
1560 { /* 0x70 */
1561 &FOLDERID_UsersFiles,
1562 CSIDL_Type_Disallowed,
1563 NULL,
1564 NULL
1566 { /* 0x71 */
1567 &FOLDERID_UsersLibraries,
1568 CSIDL_Type_Disallowed,
1569 NULL,
1570 NULL
1572 { /* 0x72 */
1573 &FOLDERID_VideosLibrary,
1574 CSIDL_Type_Disallowed, /* FIXME */
1575 NULL,
1576 NULL
1580 static HRESULT _SHExpandEnvironmentStrings(LPCWSTR szSrc, LPWSTR szDest);
1582 /* Gets the value named value from the registry key
1583 * rootKey\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
1584 * (or from rootKey\userPrefix\... if userPrefix is not NULL) into path, which
1585 * is assumed to be MAX_PATH WCHARs in length.
1586 * If it exists, expands the value and writes the expanded value to
1587 * rootKey\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders
1588 * Returns successful error code if the value was retrieved from the registry,
1589 * and a failure otherwise.
1591 static HRESULT _SHGetUserShellFolderPath(HKEY rootKey, LPCWSTR userPrefix,
1592 LPCWSTR value, LPWSTR path)
1594 HRESULT hr;
1595 WCHAR shellFolderPath[MAX_PATH], userShellFolderPath[MAX_PATH];
1596 LPCWSTR pShellFolderPath, pUserShellFolderPath;
1597 DWORD dwType, dwPathLen = MAX_PATH;
1598 HKEY userShellFolderKey, shellFolderKey;
1600 TRACE("%p,%s,%s,%p\n",rootKey, debugstr_w(userPrefix), debugstr_w(value),
1601 path);
1603 if (userPrefix)
1605 strcpyW(shellFolderPath, userPrefix);
1606 PathAddBackslashW(shellFolderPath);
1607 strcatW(shellFolderPath, szSHFolders);
1608 pShellFolderPath = shellFolderPath;
1609 strcpyW(userShellFolderPath, userPrefix);
1610 PathAddBackslashW(userShellFolderPath);
1611 strcatW(userShellFolderPath, szSHUserFolders);
1612 pUserShellFolderPath = userShellFolderPath;
1614 else
1616 pUserShellFolderPath = szSHUserFolders;
1617 pShellFolderPath = szSHFolders;
1620 if (RegCreateKeyW(rootKey, pShellFolderPath, &shellFolderKey))
1622 TRACE("Failed to create %s\n", debugstr_w(pShellFolderPath));
1623 return E_FAIL;
1625 if (RegCreateKeyW(rootKey, pUserShellFolderPath, &userShellFolderKey))
1627 TRACE("Failed to create %s\n",
1628 debugstr_w(pUserShellFolderPath));
1629 RegCloseKey(shellFolderKey);
1630 return E_FAIL;
1633 if (!RegQueryValueExW(userShellFolderKey, value, NULL, &dwType,
1634 (LPBYTE)path, &dwPathLen) && (dwType == REG_EXPAND_SZ || dwType == REG_SZ))
1636 LONG ret;
1638 path[dwPathLen / sizeof(WCHAR)] = '\0';
1639 if (dwType == REG_EXPAND_SZ && path[0] == '%')
1641 WCHAR szTemp[MAX_PATH];
1643 _SHExpandEnvironmentStrings(path, szTemp);
1644 lstrcpynW(path, szTemp, MAX_PATH);
1646 ret = RegSetValueExW(shellFolderKey, value, 0, REG_SZ, (LPBYTE)path,
1647 (strlenW(path) + 1) * sizeof(WCHAR));
1648 if (ret != ERROR_SUCCESS)
1649 hr = HRESULT_FROM_WIN32(ret);
1650 else
1651 hr = S_OK;
1653 else
1654 hr = E_FAIL;
1655 RegCloseKey(shellFolderKey);
1656 RegCloseKey(userShellFolderKey);
1657 TRACE("returning 0x%08x\n", hr);
1658 return hr;
1661 /* Gets a 'semi-expanded' default value of the CSIDL with index folder into
1662 * pszPath, based on the entries in CSIDL_Data. By semi-expanded, I mean:
1663 * - The entry's szDefaultPath may be either a string value or an integer
1664 * resource identifier. In the latter case, the string value of the resource
1665 * is written.
1666 * - Depending on the entry's type, the path may begin with an (unexpanded)
1667 * environment variable name. The caller is responsible for expanding
1668 * environment strings if so desired.
1669 * The types that are prepended with environment variables are:
1670 * CSIDL_Type_User: %USERPROFILE%
1671 * CSIDL_Type_AllUsers: %ALLUSERSPROFILE%
1672 * CSIDL_Type_CurrVer: %SystemDrive%
1673 * (Others might make sense too, but as yet are unneeded.)
1675 static HRESULT _SHGetDefaultValue(BYTE folder, LPWSTR pszPath)
1677 HRESULT hr;
1678 WCHAR resourcePath[MAX_PATH];
1679 LPCWSTR pDefaultPath = NULL;
1681 TRACE("0x%02x,%p\n", folder, pszPath);
1683 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1684 return E_INVALIDARG;
1685 if (!pszPath)
1686 return E_INVALIDARG;
1688 if (!is_win64)
1690 BOOL is_wow64;
1692 switch (folder)
1694 case CSIDL_PROGRAM_FILES:
1695 case CSIDL_PROGRAM_FILESX86:
1696 IsWow64Process( GetCurrentProcess(), &is_wow64 );
1697 folder = is_wow64 ? CSIDL_PROGRAM_FILESX86 : CSIDL_PROGRAM_FILES;
1698 break;
1699 case CSIDL_PROGRAM_FILES_COMMON:
1700 case CSIDL_PROGRAM_FILES_COMMONX86:
1701 IsWow64Process( GetCurrentProcess(), &is_wow64 );
1702 folder = is_wow64 ? CSIDL_PROGRAM_FILES_COMMONX86 : CSIDL_PROGRAM_FILES_COMMON;
1703 break;
1707 if (CSIDL_Data[folder].szDefaultPath &&
1708 IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath))
1710 if (LoadStringW(shell32_hInstance,
1711 LOWORD(CSIDL_Data[folder].szDefaultPath), resourcePath, MAX_PATH))
1713 hr = S_OK;
1714 pDefaultPath = resourcePath;
1716 else
1718 FIXME("(%d,%s), LoadString failed, missing translation?\n", folder,
1719 debugstr_w(pszPath));
1720 hr = E_FAIL;
1723 else
1725 hr = S_OK;
1726 pDefaultPath = CSIDL_Data[folder].szDefaultPath;
1728 if (SUCCEEDED(hr))
1730 switch (CSIDL_Data[folder].type)
1732 case CSIDL_Type_User:
1733 strcpyW(pszPath, UserProfileW);
1734 break;
1735 case CSIDL_Type_AllUsers:
1736 strcpyW(pszPath, AllUsersProfileW);
1737 break;
1738 case CSIDL_Type_CurrVer:
1739 strcpyW(pszPath, SystemDriveW);
1740 break;
1741 default:
1742 ; /* no corresponding env. var, do nothing */
1744 if (pDefaultPath)
1746 PathAddBackslashW(pszPath);
1747 strcatW(pszPath, pDefaultPath);
1750 TRACE("returning 0x%08x\n", hr);
1751 return hr;
1754 /* Gets the (unexpanded) value of the folder with index folder into pszPath.
1755 * The folder's type is assumed to be CSIDL_Type_CurrVer. Its default value
1756 * can be overridden in the HKLM\\szCurrentVersion key.
1757 * If dwFlags has SHGFP_TYPE_DEFAULT set or if the value isn't overridden in
1758 * the registry, uses _SHGetDefaultValue to get the value.
1760 static HRESULT _SHGetCurrentVersionPath(DWORD dwFlags, BYTE folder,
1761 LPWSTR pszPath)
1763 HRESULT hr;
1765 TRACE("0x%08x,0x%02x,%p\n", dwFlags, folder, pszPath);
1767 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1768 return E_INVALIDARG;
1769 if (CSIDL_Data[folder].type != CSIDL_Type_CurrVer)
1770 return E_INVALIDARG;
1771 if (!pszPath)
1772 return E_INVALIDARG;
1774 if (dwFlags & SHGFP_TYPE_DEFAULT)
1775 hr = _SHGetDefaultValue(folder, pszPath);
1776 else
1778 HKEY hKey;
1780 if (RegCreateKeyW(HKEY_LOCAL_MACHINE, szCurrentVersion, &hKey))
1781 hr = E_FAIL;
1782 else
1784 DWORD dwType, dwPathLen = MAX_PATH * sizeof(WCHAR);
1786 if (RegQueryValueExW(hKey, CSIDL_Data[folder].szValueName, NULL,
1787 &dwType, (LPBYTE)pszPath, &dwPathLen) ||
1788 (dwType != REG_SZ && dwType != REG_EXPAND_SZ))
1790 hr = _SHGetDefaultValue(folder, pszPath);
1791 dwType = REG_EXPAND_SZ;
1792 switch (folder)
1794 case CSIDL_PROGRAM_FILESX86:
1795 case CSIDL_PROGRAM_FILES_COMMONX86:
1796 /* these two should never be set on 32-bit setups */
1797 if (!is_win64)
1799 BOOL is_wow64;
1800 IsWow64Process( GetCurrentProcess(), &is_wow64 );
1801 if (!is_wow64) break;
1803 /* fall through */
1804 default:
1805 RegSetValueExW(hKey, CSIDL_Data[folder].szValueName, 0, dwType,
1806 (LPBYTE)pszPath, (strlenW(pszPath)+1)*sizeof(WCHAR));
1809 else
1811 pszPath[dwPathLen / sizeof(WCHAR)] = '\0';
1812 hr = S_OK;
1814 RegCloseKey(hKey);
1817 TRACE("returning 0x%08x (output path is %s)\n", hr, debugstr_w(pszPath));
1818 return hr;
1821 static LPWSTR _GetUserSidStringFromToken(HANDLE Token)
1823 char InfoBuffer[64];
1824 PTOKEN_USER UserInfo;
1825 DWORD InfoSize;
1826 LPWSTR SidStr;
1828 UserInfo = (PTOKEN_USER) InfoBuffer;
1829 if (! GetTokenInformation(Token, TokenUser, InfoBuffer, sizeof(InfoBuffer),
1830 &InfoSize))
1832 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
1833 return NULL;
1834 UserInfo = HeapAlloc(GetProcessHeap(), 0, InfoSize);
1835 if (UserInfo == NULL)
1836 return NULL;
1837 if (! GetTokenInformation(Token, TokenUser, UserInfo, InfoSize,
1838 &InfoSize))
1840 HeapFree(GetProcessHeap(), 0, UserInfo);
1841 return NULL;
1845 if (! ConvertSidToStringSidW(UserInfo->User.Sid, &SidStr))
1846 SidStr = NULL;
1848 if (UserInfo != (PTOKEN_USER) InfoBuffer)
1849 HeapFree(GetProcessHeap(), 0, UserInfo);
1851 return SidStr;
1854 /* Gets the user's path (unexpanded) for the CSIDL with index folder:
1855 * If SHGFP_TYPE_DEFAULT is set, calls _SHGetDefaultValue for it. Otherwise
1856 * calls _SHGetUserShellFolderPath for it. Where it looks depends on hToken:
1857 * - if hToken is -1, looks in HKEY_USERS\.Default
1858 * - otherwise looks first in HKEY_CURRENT_USER, followed by HKEY_LOCAL_MACHINE
1859 * if HKEY_CURRENT_USER doesn't contain any entries. If both fail, finally
1860 * calls _SHGetDefaultValue for it.
1862 static HRESULT _SHGetUserProfilePath(HANDLE hToken, DWORD dwFlags, BYTE folder,
1863 LPWSTR pszPath)
1865 const WCHAR *szValueName;
1866 WCHAR buffer[40];
1867 HRESULT hr;
1869 TRACE("%p,0x%08x,0x%02x,%p\n", hToken, dwFlags, folder, pszPath);
1871 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1872 return E_INVALIDARG;
1873 if (CSIDL_Data[folder].type != CSIDL_Type_User)
1874 return E_INVALIDARG;
1875 if (!pszPath)
1876 return E_INVALIDARG;
1878 if (dwFlags & SHGFP_TYPE_DEFAULT)
1880 if (hToken != NULL && hToken != (HANDLE)-1)
1882 FIXME("unsupported for user other than current or default\n");
1883 return E_FAIL;
1885 hr = _SHGetDefaultValue(folder, pszPath);
1887 else
1889 LPCWSTR userPrefix = NULL;
1890 HKEY hRootKey;
1892 if (hToken == (HANDLE)-1)
1894 hRootKey = HKEY_USERS;
1895 userPrefix = DefaultW;
1897 else if (hToken == NULL)
1898 hRootKey = HKEY_CURRENT_USER;
1899 else
1901 hRootKey = HKEY_USERS;
1902 userPrefix = _GetUserSidStringFromToken(hToken);
1903 if (userPrefix == NULL)
1905 hr = E_FAIL;
1906 goto error;
1910 /* For CSIDL_Type_User we also use the GUID if no szValueName is provided */
1911 szValueName = CSIDL_Data[folder].szValueName;
1912 if (!szValueName)
1914 StringFromGUID2( CSIDL_Data[folder].id, buffer, 39 );
1915 szValueName = &buffer[0];
1918 hr = _SHGetUserShellFolderPath(hRootKey, userPrefix, szValueName, pszPath);
1919 if (FAILED(hr) && hRootKey != HKEY_LOCAL_MACHINE)
1920 hr = _SHGetUserShellFolderPath(HKEY_LOCAL_MACHINE, NULL, szValueName, pszPath);
1921 if (FAILED(hr))
1922 hr = _SHGetDefaultValue(folder, pszPath);
1923 if (userPrefix != NULL && userPrefix != DefaultW)
1924 LocalFree((HLOCAL) userPrefix);
1926 error:
1927 TRACE("returning 0x%08x (output path is %s)\n", hr, debugstr_w(pszPath));
1928 return hr;
1931 /* Gets the (unexpanded) path for the CSIDL with index folder. If dwFlags has
1932 * SHGFP_TYPE_DEFAULT set, calls _SHGetDefaultValue. Otherwise calls
1933 * _SHGetUserShellFolderPath for it, looking only in HKEY_LOCAL_MACHINE.
1934 * If this fails, falls back to _SHGetDefaultValue.
1936 static HRESULT _SHGetAllUsersProfilePath(DWORD dwFlags, BYTE folder,
1937 LPWSTR pszPath)
1939 HRESULT hr;
1941 TRACE("0x%08x,0x%02x,%p\n", dwFlags, folder, pszPath);
1943 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1944 return E_INVALIDARG;
1945 if (CSIDL_Data[folder].type != CSIDL_Type_AllUsers)
1946 return E_INVALIDARG;
1947 if (!pszPath)
1948 return E_INVALIDARG;
1950 if (dwFlags & SHGFP_TYPE_DEFAULT)
1951 hr = _SHGetDefaultValue(folder, pszPath);
1952 else
1954 hr = _SHGetUserShellFolderPath(HKEY_LOCAL_MACHINE, NULL,
1955 CSIDL_Data[folder].szValueName, pszPath);
1956 if (FAILED(hr))
1957 hr = _SHGetDefaultValue(folder, pszPath);
1959 TRACE("returning 0x%08x (output path is %s)\n", hr, debugstr_w(pszPath));
1960 return hr;
1963 static HRESULT _SHOpenProfilesKey(PHKEY pKey)
1965 LONG lRet;
1966 DWORD disp;
1968 lRet = RegCreateKeyExW(HKEY_LOCAL_MACHINE, ProfileListW, 0, NULL, 0,
1969 KEY_ALL_ACCESS, NULL, pKey, &disp);
1970 return HRESULT_FROM_WIN32(lRet);
1973 /* Reads the value named szValueName from the key profilesKey (assumed to be
1974 * opened by _SHOpenProfilesKey) into szValue, which is assumed to be MAX_PATH
1975 * WCHARs in length. If it doesn't exist, returns szDefault (and saves
1976 * szDefault to the registry).
1978 static HRESULT _SHGetProfilesValue(HKEY profilesKey, LPCWSTR szValueName,
1979 LPWSTR szValue, LPCWSTR szDefault)
1981 HRESULT hr;
1982 DWORD type, dwPathLen = MAX_PATH * sizeof(WCHAR);
1983 LONG lRet;
1985 TRACE("%p,%s,%p,%s\n", profilesKey, debugstr_w(szValueName), szValue,
1986 debugstr_w(szDefault));
1987 lRet = RegQueryValueExW(profilesKey, szValueName, NULL, &type,
1988 (LPBYTE)szValue, &dwPathLen);
1989 if (!lRet && (type == REG_SZ || type == REG_EXPAND_SZ) && dwPathLen
1990 && *szValue)
1992 dwPathLen /= sizeof(WCHAR);
1993 szValue[dwPathLen] = '\0';
1994 hr = S_OK;
1996 else
1998 /* Missing or invalid value, set a default */
1999 lstrcpynW(szValue, szDefault, MAX_PATH);
2000 TRACE("Setting missing value %s to %s\n", debugstr_w(szValueName),
2001 debugstr_w(szValue));
2002 lRet = RegSetValueExW(profilesKey, szValueName, 0, REG_EXPAND_SZ,
2003 (LPBYTE)szValue,
2004 (strlenW(szValue) + 1) * sizeof(WCHAR));
2005 if (lRet)
2006 hr = HRESULT_FROM_WIN32(lRet);
2007 else
2008 hr = S_OK;
2010 TRACE("returning 0x%08x (output value is %s)\n", hr, debugstr_w(szValue));
2011 return hr;
2014 /* Attempts to expand environment variables from szSrc into szDest, which is
2015 * assumed to be MAX_PATH characters in length. Before referring to the
2016 * environment, handles a few variables directly, because the environment
2017 * variables may not be set when this is called (as during Wine's installation
2018 * when default values are being written to the registry).
2019 * The directly handled environment variables, and their source, are:
2020 * - ALLUSERSPROFILE, USERPROFILE: reads from the registry
2021 * - SystemDrive: uses GetSystemDirectoryW and uses the drive portion of its
2022 * path
2023 * If one of the directly handled environment variables is expanded, only
2024 * expands a single variable, and only in the beginning of szSrc.
2026 static HRESULT _SHExpandEnvironmentStrings(LPCWSTR szSrc, LPWSTR szDest)
2028 HRESULT hr;
2029 WCHAR szTemp[MAX_PATH], szProfilesPrefix[MAX_PATH] = { 0 };
2030 HKEY key = NULL;
2032 TRACE("%s, %p\n", debugstr_w(szSrc), szDest);
2034 if (!szSrc || !szDest) return E_INVALIDARG;
2036 /* short-circuit if there's nothing to expand */
2037 if (szSrc[0] != '%')
2039 strcpyW(szDest, szSrc);
2040 hr = S_OK;
2041 goto end;
2043 /* Get the profile prefix, we'll probably be needing it */
2044 hr = _SHOpenProfilesKey(&key);
2045 if (SUCCEEDED(hr))
2047 WCHAR def_val[MAX_PATH];
2049 /* get the system drive */
2050 GetSystemDirectoryW(def_val, MAX_PATH);
2051 if (def_val[1] == ':') strcpyW( def_val + 3, szDefaultProfileDirW );
2052 else FIXME("non-drive system paths unsupported\n");
2054 hr = _SHGetProfilesValue(key, ProfilesDirectoryW, szProfilesPrefix, def_val );
2057 *szDest = 0;
2058 strcpyW(szTemp, szSrc);
2059 while (SUCCEEDED(hr) && szTemp[0] == '%')
2061 if (!strncmpiW(szTemp, AllUsersProfileW, strlenW(AllUsersProfileW)))
2063 WCHAR szAllUsers[MAX_PATH];
2065 strcpyW(szDest, szProfilesPrefix);
2066 hr = _SHGetProfilesValue(key, AllUsersProfileValueW,
2067 szAllUsers, AllUsersW);
2068 PathAppendW(szDest, szAllUsers);
2069 PathAppendW(szDest, szTemp + strlenW(AllUsersProfileW));
2071 else if (!strncmpiW(szTemp, UserProfileW, strlenW(UserProfileW)))
2073 WCHAR userName[MAX_PATH];
2074 DWORD userLen = MAX_PATH;
2076 strcpyW(szDest, szProfilesPrefix);
2077 GetUserNameW(userName, &userLen);
2078 PathAppendW(szDest, userName);
2079 PathAppendW(szDest, szTemp + strlenW(UserProfileW));
2081 else if (!strncmpiW(szTemp, SystemDriveW, strlenW(SystemDriveW)))
2083 GetSystemDirectoryW(szDest, MAX_PATH);
2084 if (szDest[1] != ':')
2086 FIXME("non-drive system paths unsupported\n");
2087 hr = E_FAIL;
2089 else
2091 strcpyW(szDest + 3, szTemp + strlenW(SystemDriveW) + 1);
2092 hr = S_OK;
2095 else
2097 DWORD ret = ExpandEnvironmentStringsW(szSrc, szDest, MAX_PATH);
2099 if (ret > MAX_PATH)
2100 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
2101 else if (ret == 0)
2102 hr = HRESULT_FROM_WIN32(GetLastError());
2103 else
2104 hr = S_OK;
2106 if (SUCCEEDED(hr) && szDest[0] == '%')
2107 strcpyW(szTemp, szDest);
2108 else
2110 /* terminate loop */
2111 szTemp[0] = '\0';
2114 end:
2115 if (key)
2116 RegCloseKey(key);
2117 TRACE("returning 0x%08x (input was %s, output is %s)\n", hr,
2118 debugstr_w(szSrc), debugstr_w(szDest));
2119 return hr;
2122 /*************************************************************************
2123 * SHGetFolderPathW [SHELL32.@]
2125 * Convert nFolder to path.
2127 * RETURNS
2128 * Success: S_OK
2129 * Failure: standard HRESULT error codes.
2131 * NOTES
2132 * Most values can be overridden in either
2133 * HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
2134 * or in the same location in HKLM.
2135 * The "Shell Folders" registry key was used in NT4 and earlier systems.
2136 * Beginning with Windows 2000, the "User Shell Folders" key is used, so
2137 * changes made to it are made to the former key too. This synchronization is
2138 * done on-demand: not until someone requests the value of one of these paths
2139 * (by calling one of the SHGet functions) is the value synchronized.
2140 * Furthermore, the HKCU paths take precedence over the HKLM paths.
2142 HRESULT WINAPI SHGetFolderPathW(
2143 HWND hwndOwner, /* [I] owner window */
2144 int nFolder, /* [I] CSIDL identifying the folder */
2145 HANDLE hToken, /* [I] access token */
2146 DWORD dwFlags, /* [I] which path to return */
2147 LPWSTR pszPath) /* [O] converted path */
2149 HRESULT hr = SHGetFolderPathAndSubDirW(hwndOwner, nFolder, hToken, dwFlags, NULL, pszPath);
2150 if(HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND) == hr)
2151 hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
2152 return hr;
2155 HRESULT WINAPI SHGetFolderPathAndSubDirA(
2156 HWND hwndOwner, /* [I] owner window */
2157 int nFolder, /* [I] CSIDL identifying the folder */
2158 HANDLE hToken, /* [I] access token */
2159 DWORD dwFlags, /* [I] which path to return */
2160 LPCSTR pszSubPath, /* [I] sub directory of the specified folder */
2161 LPSTR pszPath) /* [O] converted path */
2163 int length;
2164 HRESULT hr = S_OK;
2165 LPWSTR pszSubPathW = NULL;
2166 LPWSTR pszPathW = NULL;
2167 TRACE("%08x,%08x,%s\n",nFolder, dwFlags, debugstr_w(pszSubPathW));
2169 if(pszPath) {
2170 pszPathW = HeapAlloc(GetProcessHeap(), 0, MAX_PATH * sizeof(WCHAR));
2171 if(!pszPathW) {
2172 hr = HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
2173 goto cleanup;
2176 TRACE("%08x,%08x,%s\n",nFolder, dwFlags, debugstr_w(pszSubPathW));
2178 /* SHGetFolderPathAndSubDirW does not distinguish if pszSubPath isn't
2179 * set (null), or an empty string.therefore call it without the parameter set
2180 * if pszSubPath is an empty string
2182 if (pszSubPath && pszSubPath[0]) {
2183 length = MultiByteToWideChar(CP_ACP, 0, pszSubPath, -1, NULL, 0);
2184 pszSubPathW = HeapAlloc(GetProcessHeap(), 0, length * sizeof(WCHAR));
2185 if(!pszSubPathW) {
2186 hr = HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
2187 goto cleanup;
2189 MultiByteToWideChar(CP_ACP, 0, pszSubPath, -1, pszSubPathW, length);
2192 hr = SHGetFolderPathAndSubDirW(hwndOwner, nFolder, hToken, dwFlags, pszSubPathW, pszPathW);
2194 if (SUCCEEDED(hr) && pszPath)
2195 WideCharToMultiByte(CP_ACP, 0, pszPathW, -1, pszPath, MAX_PATH, NULL, NULL);
2197 cleanup:
2198 HeapFree(GetProcessHeap(), 0, pszPathW);
2199 HeapFree(GetProcessHeap(), 0, pszSubPathW);
2200 return hr;
2203 /*************************************************************************
2204 * SHGetFolderPathAndSubDirW [SHELL32.@]
2206 HRESULT WINAPI SHGetFolderPathAndSubDirW(
2207 HWND hwndOwner, /* [I] owner window */
2208 int nFolder, /* [I] CSIDL identifying the folder */
2209 HANDLE hToken, /* [I] access token */
2210 DWORD dwFlags, /* [I] which path to return */
2211 LPCWSTR pszSubPath,/* [I] sub directory of the specified folder */
2212 LPWSTR pszPath) /* [O] converted path */
2214 HRESULT hr;
2215 WCHAR szBuildPath[MAX_PATH], szTemp[MAX_PATH];
2216 DWORD folder = nFolder & CSIDL_FOLDER_MASK;
2217 CSIDL_Type type;
2218 int ret;
2220 TRACE("%p,%p,nFolder=0x%04x,%s\n", hwndOwner,pszPath,nFolder,debugstr_w(pszSubPath));
2222 /* Windows always NULL-terminates the resulting path regardless of success
2223 * or failure, so do so first
2225 if (pszPath)
2226 *pszPath = '\0';
2228 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
2229 return E_INVALIDARG;
2230 if ((SHGFP_TYPE_CURRENT != dwFlags) && (SHGFP_TYPE_DEFAULT != dwFlags))
2231 return E_INVALIDARG;
2232 szTemp[0] = 0;
2233 type = CSIDL_Data[folder].type;
2234 switch (type)
2236 case CSIDL_Type_Disallowed:
2237 hr = E_INVALIDARG;
2238 break;
2239 case CSIDL_Type_NonExistent:
2240 hr = S_FALSE;
2241 break;
2242 case CSIDL_Type_WindowsPath:
2243 GetWindowsDirectoryW(szTemp, MAX_PATH);
2244 if (CSIDL_Data[folder].szDefaultPath &&
2245 !IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath) &&
2246 *CSIDL_Data[folder].szDefaultPath)
2248 PathAddBackslashW(szTemp);
2249 strcatW(szTemp, CSIDL_Data[folder].szDefaultPath);
2251 hr = S_OK;
2252 break;
2253 case CSIDL_Type_SystemPath:
2254 GetSystemDirectoryW(szTemp, MAX_PATH);
2255 if (CSIDL_Data[folder].szDefaultPath &&
2256 !IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath) &&
2257 *CSIDL_Data[folder].szDefaultPath)
2259 PathAddBackslashW(szTemp);
2260 strcatW(szTemp, CSIDL_Data[folder].szDefaultPath);
2262 hr = S_OK;
2263 break;
2264 case CSIDL_Type_SystemX86Path:
2265 if (!GetSystemWow64DirectoryW(szTemp, MAX_PATH)) GetSystemDirectoryW(szTemp, MAX_PATH);
2266 if (CSIDL_Data[folder].szDefaultPath &&
2267 !IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath) &&
2268 *CSIDL_Data[folder].szDefaultPath)
2270 PathAddBackslashW(szTemp);
2271 strcatW(szTemp, CSIDL_Data[folder].szDefaultPath);
2273 hr = S_OK;
2274 break;
2275 case CSIDL_Type_CurrVer:
2276 hr = _SHGetCurrentVersionPath(dwFlags, folder, szTemp);
2277 break;
2278 case CSIDL_Type_User:
2279 hr = _SHGetUserProfilePath(hToken, dwFlags, folder, szTemp);
2280 break;
2281 case CSIDL_Type_AllUsers:
2282 hr = _SHGetAllUsersProfilePath(dwFlags, folder, szTemp);
2283 break;
2284 default:
2285 FIXME("bogus type %d, please fix\n", type);
2286 hr = E_INVALIDARG;
2287 break;
2290 /* Expand environment strings if necessary */
2291 if (*szTemp == '%')
2292 hr = _SHExpandEnvironmentStrings(szTemp, szBuildPath);
2293 else
2294 strcpyW(szBuildPath, szTemp);
2296 if (FAILED(hr)) goto end;
2298 if(pszSubPath) {
2299 /* make sure the new path does not exceed the buffer length
2300 * and remember to backslash and terminate it */
2301 if(MAX_PATH < (lstrlenW(szBuildPath) + lstrlenW(pszSubPath) + 2)) {
2302 hr = HRESULT_FROM_WIN32(ERROR_FILENAME_EXCED_RANGE);
2303 goto end;
2305 PathAppendW(szBuildPath, pszSubPath);
2306 PathRemoveBackslashW(szBuildPath);
2308 /* Copy the path if it's available before we might return */
2309 if (SUCCEEDED(hr) && pszPath)
2310 strcpyW(pszPath, szBuildPath);
2312 /* if we don't care about existing directories we are ready */
2313 if(nFolder & CSIDL_FLAG_DONT_VERIFY) goto end;
2315 if (PathFileExistsW(szBuildPath)) goto end;
2317 /* not existing but we are not allowed to create it. The return value
2318 * is verified against shell32 version 6.0.
2320 if (!(nFolder & CSIDL_FLAG_CREATE))
2322 hr = HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND);
2323 goto end;
2326 /* create directory/directories */
2327 ret = SHCreateDirectoryExW(hwndOwner, szBuildPath, NULL);
2328 if (ret && ret != ERROR_ALREADY_EXISTS)
2330 ERR("Failed to create directory %s.\n", debugstr_w(szBuildPath));
2331 hr = E_FAIL;
2332 goto end;
2335 TRACE("Created missing system directory %s\n", debugstr_w(szBuildPath));
2336 end:
2337 TRACE("returning 0x%08x (final path is %s)\n", hr, debugstr_w(szBuildPath));
2338 return hr;
2341 /*************************************************************************
2342 * SHGetFolderPathA [SHELL32.@]
2344 * See SHGetFolderPathW.
2346 HRESULT WINAPI SHGetFolderPathA(
2347 HWND hwndOwner,
2348 int nFolder,
2349 HANDLE hToken,
2350 DWORD dwFlags,
2351 LPSTR pszPath)
2353 WCHAR szTemp[MAX_PATH];
2354 HRESULT hr;
2356 TRACE("%p,%p,nFolder=0x%04x\n",hwndOwner,pszPath,nFolder);
2358 if (pszPath)
2359 *pszPath = '\0';
2360 hr = SHGetFolderPathW(hwndOwner, nFolder, hToken, dwFlags, szTemp);
2361 if (SUCCEEDED(hr) && pszPath)
2362 WideCharToMultiByte(CP_ACP, 0, szTemp, -1, pszPath, MAX_PATH, NULL,
2363 NULL);
2365 return hr;
2368 /* For each folder in folders, if its value has not been set in the registry,
2369 * calls _SHGetUserProfilePath or _SHGetAllUsersProfilePath (depending on the
2370 * folder's type) to get the unexpanded value first.
2371 * Writes the unexpanded value to User Shell Folders, and queries it with
2372 * SHGetFolderPathW to force the creation of the directory if it doesn't
2373 * already exist. SHGetFolderPathW also returns the expanded value, which
2374 * this then writes to Shell Folders.
2376 static HRESULT _SHRegisterFolders(HKEY hRootKey, HANDLE hToken,
2377 LPCWSTR szUserShellFolderPath, LPCWSTR szShellFolderPath, const UINT folders[],
2378 UINT foldersLen)
2380 const WCHAR *szValueName;
2381 WCHAR buffer[40];
2382 UINT i;
2383 WCHAR path[MAX_PATH];
2384 HRESULT hr = S_OK;
2385 HKEY hUserKey = NULL, hKey = NULL;
2386 DWORD dwType, dwPathLen;
2387 LONG ret;
2389 TRACE("%p,%p,%s,%p,%u\n", hRootKey, hToken,
2390 debugstr_w(szUserShellFolderPath), folders, foldersLen);
2392 ret = RegCreateKeyW(hRootKey, szUserShellFolderPath, &hUserKey);
2393 if (ret)
2394 hr = HRESULT_FROM_WIN32(ret);
2395 else
2397 ret = RegCreateKeyW(hRootKey, szShellFolderPath, &hKey);
2398 if (ret)
2399 hr = HRESULT_FROM_WIN32(ret);
2401 for (i = 0; SUCCEEDED(hr) && i < foldersLen; i++)
2403 dwPathLen = MAX_PATH * sizeof(WCHAR);
2405 /* For CSIDL_Type_User we also use the GUID if no szValueName is provided */
2406 szValueName = CSIDL_Data[folders[i]].szValueName;
2407 if (!szValueName && CSIDL_Data[folders[i]].type == CSIDL_Type_User)
2409 StringFromGUID2( CSIDL_Data[folders[i]].id, buffer, 39 );
2410 szValueName = &buffer[0];
2413 if (RegQueryValueExW(hUserKey, szValueName, NULL,
2414 &dwType, (LPBYTE)path, &dwPathLen) || (dwType != REG_SZ &&
2415 dwType != REG_EXPAND_SZ))
2417 *path = '\0';
2418 if (CSIDL_Data[folders[i]].type == CSIDL_Type_User)
2419 _SHGetUserProfilePath(hToken, SHGFP_TYPE_DEFAULT, folders[i],
2420 path);
2421 else if (CSIDL_Data[folders[i]].type == CSIDL_Type_AllUsers)
2422 _SHGetAllUsersProfilePath(SHGFP_TYPE_DEFAULT, folders[i], path);
2423 else if (CSIDL_Data[folders[i]].type == CSIDL_Type_WindowsPath)
2425 GetWindowsDirectoryW(path, MAX_PATH);
2426 if (CSIDL_Data[folders[i]].szDefaultPath &&
2427 !IS_INTRESOURCE(CSIDL_Data[folders[i]].szDefaultPath))
2429 PathAddBackslashW(path);
2430 strcatW(path, CSIDL_Data[folders[i]].szDefaultPath);
2433 else
2434 hr = E_FAIL;
2435 if (*path)
2437 ret = RegSetValueExW(hUserKey, szValueName, 0, REG_EXPAND_SZ,
2438 (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR));
2439 if (ret)
2440 hr = HRESULT_FROM_WIN32(ret);
2441 else
2443 hr = SHGetFolderPathW(NULL, folders[i] | CSIDL_FLAG_CREATE,
2444 hToken, SHGFP_TYPE_DEFAULT, path);
2445 ret = RegSetValueExW(hKey, szValueName, 0, REG_SZ,
2446 (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR));
2447 if (ret)
2448 hr = HRESULT_FROM_WIN32(ret);
2453 if (hUserKey)
2454 RegCloseKey(hUserKey);
2455 if (hKey)
2456 RegCloseKey(hKey);
2458 TRACE("returning 0x%08x\n", hr);
2459 return hr;
2462 static HRESULT _SHRegisterUserShellFolders(BOOL bDefault)
2464 static const UINT folders[] = {
2465 CSIDL_PROGRAMS,
2466 CSIDL_PERSONAL,
2467 CSIDL_FAVORITES,
2468 CSIDL_APPDATA,
2469 CSIDL_STARTUP,
2470 CSIDL_RECENT,
2471 CSIDL_SENDTO,
2472 CSIDL_STARTMENU,
2473 CSIDL_MYMUSIC,
2474 CSIDL_MYVIDEO,
2475 CSIDL_DESKTOPDIRECTORY,
2476 CSIDL_NETHOOD,
2477 CSIDL_TEMPLATES,
2478 CSIDL_PRINTHOOD,
2479 CSIDL_LOCAL_APPDATA,
2480 CSIDL_INTERNET_CACHE,
2481 CSIDL_COOKIES,
2482 CSIDL_HISTORY,
2483 CSIDL_MYPICTURES,
2484 CSIDL_FONTS,
2485 CSIDL_ADMINTOOLS,
2486 CSIDL_CONTACTS,
2487 CSIDL_DOWNLOADS,
2488 CSIDL_LINKS,
2489 CSIDL_APPDATA_LOCALLOW,
2490 CSIDL_SAVED_GAMES,
2491 CSIDL_SEARCHES
2493 WCHAR userShellFolderPath[MAX_PATH], shellFolderPath[MAX_PATH];
2494 LPCWSTR pUserShellFolderPath, pShellFolderPath;
2495 HRESULT hr = S_OK;
2496 HKEY hRootKey;
2497 HANDLE hToken;
2499 TRACE("%s\n", bDefault ? "TRUE" : "FALSE");
2500 if (bDefault)
2502 hToken = (HANDLE)-1;
2503 hRootKey = HKEY_USERS;
2504 strcpyW(userShellFolderPath, DefaultW);
2505 PathAddBackslashW(userShellFolderPath);
2506 strcatW(userShellFolderPath, szSHUserFolders);
2507 pUserShellFolderPath = userShellFolderPath;
2508 strcpyW(shellFolderPath, DefaultW);
2509 PathAddBackslashW(shellFolderPath);
2510 strcatW(shellFolderPath, szSHFolders);
2511 pShellFolderPath = shellFolderPath;
2513 else
2515 hToken = NULL;
2516 hRootKey = HKEY_CURRENT_USER;
2517 pUserShellFolderPath = szSHUserFolders;
2518 pShellFolderPath = szSHFolders;
2521 hr = _SHRegisterFolders(hRootKey, hToken, pUserShellFolderPath,
2522 pShellFolderPath, folders, sizeof(folders) / sizeof(folders[0]));
2523 TRACE("returning 0x%08x\n", hr);
2524 return hr;
2527 static HRESULT _SHRegisterCommonShellFolders(void)
2529 static const UINT folders[] = {
2530 CSIDL_COMMON_STARTMENU,
2531 CSIDL_COMMON_PROGRAMS,
2532 CSIDL_COMMON_STARTUP,
2533 CSIDL_COMMON_DESKTOPDIRECTORY,
2534 CSIDL_COMMON_FAVORITES,
2535 CSIDL_COMMON_APPDATA,
2536 CSIDL_COMMON_TEMPLATES,
2537 CSIDL_COMMON_DOCUMENTS,
2538 CSIDL_COMMON_ADMINTOOLS,
2539 CSIDL_COMMON_MUSIC,
2540 CSIDL_COMMON_PICTURES,
2541 CSIDL_COMMON_VIDEO,
2543 HRESULT hr;
2545 TRACE("\n");
2546 hr = _SHRegisterFolders(HKEY_LOCAL_MACHINE, NULL, szSHUserFolders,
2547 szSHFolders, folders, sizeof(folders) / sizeof(folders[0]));
2548 TRACE("returning 0x%08x\n", hr);
2549 return hr;
2552 /******************************************************************************
2553 * _SHAppendToUnixPath [Internal]
2555 * Helper function for _SHCreateSymbolicLinks. Appends pwszSubPath (or the
2556 * corresponding resource, if IS_INTRESOURCE) to the unix base path 'szBasePath'
2557 * and replaces backslashes with slashes.
2559 * PARAMS
2560 * szBasePath [IO] The unix base path, which will be appended to (CP_UNXICP).
2561 * pwszSubPath [I] Sub-path or resource id (use MAKEINTRESOURCEW).
2563 * RETURNS
2564 * Success: TRUE,
2565 * Failure: FALSE
2567 static inline BOOL _SHAppendToUnixPath(char *szBasePath, LPCWSTR pwszSubPath) {
2568 WCHAR wszSubPath[MAX_PATH];
2569 int cLen = strlen(szBasePath);
2570 char *pBackslash;
2572 if (IS_INTRESOURCE(pwszSubPath)) {
2573 if (!LoadStringW(shell32_hInstance, LOWORD(pwszSubPath), wszSubPath, MAX_PATH)) {
2574 /* Fall back to hard coded defaults. */
2575 switch (LOWORD(pwszSubPath)) {
2576 case IDS_PERSONAL:
2577 lstrcpyW(wszSubPath, PersonalW);
2578 break;
2579 case IDS_MYMUSIC:
2580 lstrcpyW(wszSubPath, My_MusicW);
2581 break;
2582 case IDS_MYPICTURES:
2583 lstrcpyW(wszSubPath, My_PicturesW);
2584 break;
2585 case IDS_MYVIDEOS:
2586 lstrcpyW(wszSubPath, My_VideosW);
2587 break;
2588 default:
2589 ERR("LoadString(%d) failed!\n", LOWORD(pwszSubPath));
2590 return FALSE;
2593 } else {
2594 lstrcpyW(wszSubPath, pwszSubPath);
2597 if (szBasePath[cLen-1] != '/') szBasePath[cLen++] = '/';
2599 if (!WideCharToMultiByte(CP_UNIXCP, 0, wszSubPath, -1, szBasePath + cLen,
2600 FILENAME_MAX - cLen, NULL, NULL))
2602 return FALSE;
2605 pBackslash = szBasePath + cLen;
2606 while ((pBackslash = strchr(pBackslash, '\\'))) *pBackslash = '/';
2608 return TRUE;
2611 /******************************************************************************
2612 * _SHCreateSymbolicLinks [Internal]
2614 * Sets up symbol links for various shell folders to point into the users home
2615 * directory. We do an educated guess about what the user would probably want:
2616 * - If there is a 'My Documents' directory in $HOME, the user probably wants
2617 * wine's 'My Documents' to point there. Furthermore, we imply that the user
2618 * is a Windows lover and has no problem with wine creating 'My Pictures',
2619 * 'My Music' and 'My Videos' subfolders under '$HOME/My Documents', if those
2620 * do not already exits. We put appropriate symbolic links in place for those,
2621 * too.
2622 * - If there is no 'My Documents' directory in $HOME, we let 'My Documents'
2623 * point directly to $HOME. We assume the user to be a unix hacker who does not
2624 * want wine to create anything anywhere besides the .wine directory. So, if
2625 * there already is a 'My Music' directory in $HOME, we symlink the 'My Music'
2626 * shell folder to it. But if not, then we check XDG_MUSIC_DIR - "well known"
2627 * directory, and try to link to that. If that fails, then we symlink to
2628 * $HOME directly. The same holds fo 'My Pictures' and 'My Videos'.
2629 * - The Desktop shell folder is symlinked to XDG_DESKTOP_DIR. If that does not
2630 * exist, then we try '$HOME/Desktop'. If that does not exist, then we leave
2631 * it alone.
2632 * ('My Music',... above in fact means LoadString(IDS_MYMUSIC))
2634 static void _SHCreateSymbolicLinks(void)
2636 UINT aidsMyStuff[] = { IDS_MYPICTURES, IDS_MYVIDEOS, IDS_MYMUSIC }, i;
2637 int acsidlMyStuff[] = { CSIDL_MYPICTURES, CSIDL_MYVIDEO, CSIDL_MYMUSIC };
2638 static const char * const xdg_dirs[] = { "PICTURES", "VIDEOS", "MUSIC", "DESKTOP" };
2639 static const unsigned int num = sizeof(xdg_dirs) / sizeof(xdg_dirs[0]);
2640 WCHAR wszTempPath[MAX_PATH];
2641 char szPersonalTarget[FILENAME_MAX], *pszPersonal;
2642 char szMyStuffTarget[FILENAME_MAX], *pszMyStuff;
2643 char szDesktopTarget[FILENAME_MAX], *pszDesktop;
2644 struct stat statFolder;
2645 const char *pszHome;
2646 HRESULT hr;
2647 char ** xdg_results;
2648 char * xdg_desktop_dir;
2650 /* Create all necessary profile sub-dirs up to 'My Documents' and get the unix path. */
2651 hr = SHGetFolderPathW(NULL, CSIDL_PERSONAL|CSIDL_FLAG_CREATE, NULL,
2652 SHGFP_TYPE_DEFAULT, wszTempPath);
2653 if (FAILED(hr)) return;
2654 pszPersonal = wine_get_unix_file_name(wszTempPath);
2655 if (!pszPersonal) return;
2657 hr = XDG_UserDirLookup(xdg_dirs, num, &xdg_results);
2658 if (FAILED(hr)) xdg_results = NULL;
2660 pszHome = getenv("HOME");
2661 if (pszHome && !stat(pszHome, &statFolder) && S_ISDIR(statFolder.st_mode)) {
2662 strcpy(szPersonalTarget, pszHome);
2663 if (_SHAppendToUnixPath(szPersonalTarget, MAKEINTRESOURCEW(IDS_PERSONAL)) &&
2664 !stat(szPersonalTarget, &statFolder) && S_ISDIR(statFolder.st_mode))
2666 /* '$HOME/My Documents' exists. Create 'My Pictures', 'My Videos' and
2667 * 'My Music' subfolders or fail silently if they already exist. */
2668 for (i = 0; i < sizeof(aidsMyStuff)/sizeof(aidsMyStuff[0]); i++) {
2669 strcpy(szMyStuffTarget, szPersonalTarget);
2670 if (_SHAppendToUnixPath(szMyStuffTarget, MAKEINTRESOURCEW(aidsMyStuff[i])))
2671 mkdir(szMyStuffTarget, 0777);
2674 else
2676 /* '$HOME/My Documents' doesn't exists, but '$HOME' does. */
2677 strcpy(szPersonalTarget, pszHome);
2680 /* Replace 'My Documents' directory with a symlink of fail silently if not empty. */
2681 rmdir(pszPersonal);
2682 symlink(szPersonalTarget, pszPersonal);
2684 else
2686 /* '$HOME' doesn't exist. Create 'My Pictures', 'My Videos' and 'My Music' subdirs
2687 * in '%USERPROFILE%\\My Documents' or fail silently if they already exist. */
2688 strcpy(szPersonalTarget, pszPersonal);
2689 for (i = 0; i < sizeof(aidsMyStuff)/sizeof(aidsMyStuff[0]); i++) {
2690 strcpy(szMyStuffTarget, szPersonalTarget);
2691 if (_SHAppendToUnixPath(szMyStuffTarget, MAKEINTRESOURCEW(aidsMyStuff[i])))
2692 mkdir(szMyStuffTarget, 0777);
2696 /* Create symbolic links for 'My Pictures', 'My Videos' and 'My Music'. */
2697 for (i=0; i < sizeof(aidsMyStuff)/sizeof(aidsMyStuff[0]); i++) {
2698 /* Create the current 'My Whatever' folder and get its unix path. */
2699 hr = SHGetFolderPathW(NULL, acsidlMyStuff[i]|CSIDL_FLAG_CREATE, NULL,
2700 SHGFP_TYPE_DEFAULT, wszTempPath);
2701 if (FAILED(hr)) continue;
2702 pszMyStuff = wine_get_unix_file_name(wszTempPath);
2703 if (!pszMyStuff) continue;
2705 strcpy(szMyStuffTarget, szPersonalTarget);
2706 if (_SHAppendToUnixPath(szMyStuffTarget, MAKEINTRESOURCEW(aidsMyStuff[i])) &&
2707 !stat(szMyStuffTarget, &statFolder) && S_ISDIR(statFolder.st_mode))
2709 /* If there's a 'My Whatever' directory where 'My Documents' links to, link to it. */
2710 rmdir(pszMyStuff);
2711 symlink(szMyStuffTarget, pszMyStuff);
2713 else
2715 rmdir(pszMyStuff);
2716 if (xdg_results && xdg_results[i])
2718 /* the folder specified by XDG_XXX_DIR exists, link to it. */
2719 symlink(xdg_results[i], pszMyStuff);
2721 else
2723 /* Else link to where 'My Documents' itself links to. */
2724 symlink(szPersonalTarget, pszMyStuff);
2727 HeapFree(GetProcessHeap(), 0, pszMyStuff);
2730 /* Last but not least, the Desktop folder */
2731 if (pszHome)
2732 strcpy(szDesktopTarget, pszHome);
2733 else
2734 strcpy(szDesktopTarget, pszPersonal);
2735 HeapFree(GetProcessHeap(), 0, pszPersonal);
2737 xdg_desktop_dir = xdg_results ? xdg_results[num - 1] : NULL;
2738 if (xdg_desktop_dir ||
2739 (_SHAppendToUnixPath(szDesktopTarget, DesktopW) &&
2740 !stat(szDesktopTarget, &statFolder) && S_ISDIR(statFolder.st_mode)))
2742 hr = SHGetFolderPathW(NULL, CSIDL_DESKTOPDIRECTORY|CSIDL_FLAG_CREATE, NULL,
2743 SHGFP_TYPE_DEFAULT, wszTempPath);
2744 if (SUCCEEDED(hr) && (pszDesktop = wine_get_unix_file_name(wszTempPath)))
2746 rmdir(pszDesktop);
2747 if (xdg_desktop_dir)
2748 symlink(xdg_desktop_dir, pszDesktop);
2749 else
2750 symlink(szDesktopTarget, pszDesktop);
2751 HeapFree(GetProcessHeap(), 0, pszDesktop);
2755 /* Free resources allocated by XDG_UserDirLookup() */
2756 if (xdg_results)
2758 for (i = 0; i < num; i++)
2759 HeapFree(GetProcessHeap(), 0, xdg_results[i]);
2760 HeapFree(GetProcessHeap(), 0, xdg_results);
2764 /******************************************************************************
2765 * create_extra_folders [Internal]
2767 * Create some extra folders that don't have a standard CSIDL definition.
2769 static HRESULT create_extra_folders(void)
2771 static const WCHAR environW[] = {'E','n','v','i','r','o','n','m','e','n','t',0};
2772 static const WCHAR microsoftW[] = {'M','i','c','r','o','s','o','f','t',0};
2773 static const WCHAR TempW[] = {'T','e','m','p',0};
2774 static const WCHAR TEMPW[] = {'T','E','M','P',0};
2775 static const WCHAR TMPW[] = {'T','M','P',0};
2776 WCHAR path[MAX_PATH+5];
2777 HRESULT hr;
2778 HKEY hkey;
2779 DWORD type, size, ret;
2781 ret = RegCreateKeyW( HKEY_CURRENT_USER, environW, &hkey );
2782 if (ret) return HRESULT_FROM_WIN32( ret );
2784 /* FIXME: should be under AppData, but we don't want spaces in the temp path */
2785 hr = SHGetFolderPathAndSubDirW( 0, CSIDL_PROFILE | CSIDL_FLAG_CREATE, NULL,
2786 SHGFP_TYPE_DEFAULT, TempW, path );
2787 if (SUCCEEDED(hr))
2789 size = sizeof(path);
2790 if (RegQueryValueExW( hkey, TEMPW, NULL, &type, (LPBYTE)path, &size ))
2791 RegSetValueExW( hkey, TEMPW, 0, REG_SZ, (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR) );
2792 size = sizeof(path);
2793 if (RegQueryValueExW( hkey, TMPW, NULL, &type, (LPBYTE)path, &size ))
2794 RegSetValueExW( hkey, TMPW, 0, REG_SZ, (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR) );
2796 RegCloseKey( hkey );
2798 if (SUCCEEDED(hr))
2800 hr = SHGetFolderPathAndSubDirW( 0, CSIDL_COMMON_APPDATA | CSIDL_FLAG_CREATE, NULL,
2801 SHGFP_TYPE_DEFAULT, microsoftW, path );
2803 return hr;
2807 /******************************************************************************
2808 * set_folder_attributes
2810 * Set the various folder attributes registry keys.
2812 static HRESULT set_folder_attributes(void)
2814 static const WCHAR clsidW[] = {'C','L','S','I','D','\\',0 };
2815 static const WCHAR shellfolderW[] = {'\\','S','h','e','l','l','F','o','l','d','e','r', 0 };
2816 static const WCHAR wfparsingW[] = {'W','a','n','t','s','F','O','R','P','A','R','S','I','N','G',0};
2817 static const WCHAR wfdisplayW[] = {'W','a','n','t','s','F','O','R','D','I','S','P','L','A','Y',0};
2818 static const WCHAR hideasdeleteW[] = {'H','i','d','e','A','s','D','e','l','e','t','e','P','e','r','U','s','e','r',0};
2819 static const WCHAR attributesW[] = {'A','t','t','r','i','b','u','t','e','s',0};
2820 static const WCHAR cfattributesW[] = {'C','a','l','l','F','o','r','A','t','t','r','i','b','u','t','e','s',0};
2821 static const WCHAR emptyW[] = {0};
2823 static const struct
2825 const CLSID *clsid;
2826 BOOL wfparsing : 1;
2827 BOOL wfdisplay : 1;
2828 BOOL hideasdel : 1;
2829 DWORD attr;
2830 DWORD call_for_attr;
2831 } folders[] =
2833 { &CLSID_UnixFolder, TRUE, FALSE, FALSE },
2834 { &CLSID_UnixDosFolder, TRUE, FALSE, FALSE,
2835 SFGAO_FILESYSANCESTOR|SFGAO_FOLDER|SFGAO_HASSUBFOLDER, SFGAO_FILESYSTEM },
2836 { &CLSID_FolderShortcut, FALSE, FALSE, FALSE,
2837 SFGAO_FILESYSTEM|SFGAO_FOLDER|SFGAO_LINK,
2838 SFGAO_HASSUBFOLDER|SFGAO_FILESYSTEM|SFGAO_FOLDER|SFGAO_FILESYSANCESTOR },
2839 { &CLSID_MyDocuments, TRUE, FALSE, FALSE,
2840 SFGAO_FILESYSANCESTOR|SFGAO_FOLDER|SFGAO_HASSUBFOLDER, SFGAO_FILESYSTEM },
2841 { &CLSID_RecycleBin, FALSE, FALSE, FALSE,
2842 SFGAO_FOLDER|SFGAO_DROPTARGET|SFGAO_HASPROPSHEET },
2843 { &CLSID_ControlPanel, FALSE, TRUE, TRUE,
2844 SFGAO_FOLDER|SFGAO_HASSUBFOLDER }
2847 unsigned int i;
2848 WCHAR buffer[39 + (sizeof(clsidW) + sizeof(shellfolderW)) / sizeof(WCHAR)];
2849 LONG res;
2850 HKEY hkey;
2852 for (i = 0; i < sizeof(folders)/sizeof(folders[0]); i++)
2854 strcpyW( buffer, clsidW );
2855 StringFromGUID2( folders[i].clsid, buffer + strlenW(buffer), 39 );
2856 strcatW( buffer, shellfolderW );
2857 res = RegCreateKeyExW( HKEY_CLASSES_ROOT, buffer, 0, NULL, 0,
2858 KEY_READ | KEY_WRITE, NULL, &hkey, NULL);
2859 if (res) return HRESULT_FROM_WIN32( res );
2860 if (folders[i].wfparsing)
2861 res = RegSetValueExW( hkey, wfparsingW, 0, REG_SZ, (const BYTE *)emptyW, sizeof(emptyW) );
2862 if (folders[i].wfdisplay)
2863 res = RegSetValueExW( hkey, wfdisplayW, 0, REG_SZ, (const BYTE *)emptyW, sizeof(emptyW) );
2864 if (folders[i].hideasdel)
2865 res = RegSetValueExW( hkey, hideasdeleteW, 0, REG_SZ, (const BYTE *)emptyW, sizeof(emptyW) );
2866 if (folders[i].attr)
2867 res = RegSetValueExW( hkey, attributesW, 0, REG_DWORD,
2868 (const BYTE *)&folders[i].attr, sizeof(DWORD));
2869 if (folders[i].call_for_attr)
2870 res = RegSetValueExW( hkey, cfattributesW, 0, REG_DWORD,
2871 (const BYTE *)&folders[i].call_for_attr, sizeof(DWORD));
2872 RegCloseKey( hkey );
2874 return S_OK;
2878 /* Register the default values in the registry, as some apps seem to depend
2879 * on their presence. The set registered was taken from Windows XP.
2881 HRESULT SHELL_RegisterShellFolders(void)
2883 HRESULT hr;
2885 /* Set up '$HOME' targeted symlinks for 'My Documents', 'My Pictures',
2886 * 'My Videos', 'My Music' and 'Desktop' in advance, so that the
2887 * _SHRegister*ShellFolders() functions will find everything nice and clean
2888 * and thus will not attempt to create them in the profile directory. */
2889 _SHCreateSymbolicLinks();
2891 hr = _SHRegisterUserShellFolders(TRUE);
2892 if (SUCCEEDED(hr))
2893 hr = _SHRegisterUserShellFolders(FALSE);
2894 if (SUCCEEDED(hr))
2895 hr = _SHRegisterCommonShellFolders();
2896 if (SUCCEEDED(hr))
2897 hr = create_extra_folders();
2898 if (SUCCEEDED(hr))
2899 hr = set_folder_attributes();
2900 return hr;
2903 /*************************************************************************
2904 * SHGetSpecialFolderPathA [SHELL32.@]
2906 BOOL WINAPI SHGetSpecialFolderPathA (
2907 HWND hwndOwner,
2908 LPSTR szPath,
2909 int nFolder,
2910 BOOL bCreate)
2912 return SHGetFolderPathA(hwndOwner, nFolder + (bCreate ? CSIDL_FLAG_CREATE : 0), NULL, 0,
2913 szPath) == S_OK;
2916 /*************************************************************************
2917 * SHGetSpecialFolderPathW
2919 BOOL WINAPI SHGetSpecialFolderPathW (
2920 HWND hwndOwner,
2921 LPWSTR szPath,
2922 int nFolder,
2923 BOOL bCreate)
2925 return SHGetFolderPathW(hwndOwner, nFolder + (bCreate ? CSIDL_FLAG_CREATE : 0), NULL, 0,
2926 szPath) == S_OK;
2929 /*************************************************************************
2930 * SHGetSpecialFolderPath (SHELL32.175)
2932 BOOL WINAPI SHGetSpecialFolderPathAW (
2933 HWND hwndOwner,
2934 LPVOID szPath,
2935 int nFolder,
2936 BOOL bCreate)
2939 if (SHELL_OsIsUnicode())
2940 return SHGetSpecialFolderPathW (hwndOwner, szPath, nFolder, bCreate);
2941 return SHGetSpecialFolderPathA (hwndOwner, szPath, nFolder, bCreate);
2944 /*************************************************************************
2945 * SHGetFolderLocation [SHELL32.@]
2947 * Gets the folder locations from the registry and creates a pidl.
2949 * PARAMS
2950 * hwndOwner [I]
2951 * nFolder [I] CSIDL_xxxxx
2952 * hToken [I] token representing user, or NULL for current user, or -1 for
2953 * default user
2954 * dwReserved [I] must be zero
2955 * ppidl [O] PIDL of a special folder
2957 * RETURNS
2958 * Success: S_OK
2959 * Failure: Standard OLE-defined error result, S_FALSE or E_INVALIDARG
2961 * NOTES
2962 * Creates missing reg keys and directories.
2963 * Mostly forwards to SHGetFolderPathW, but a few values of nFolder return
2964 * virtual folders that are handled here.
2966 HRESULT WINAPI SHGetFolderLocation(
2967 HWND hwndOwner,
2968 int nFolder,
2969 HANDLE hToken,
2970 DWORD dwReserved,
2971 LPITEMIDLIST *ppidl)
2973 HRESULT hr = E_INVALIDARG;
2975 TRACE("%p 0x%08x %p 0x%08x %p\n",
2976 hwndOwner, nFolder, hToken, dwReserved, ppidl);
2978 if (!ppidl)
2979 return E_INVALIDARG;
2980 if (dwReserved)
2981 return E_INVALIDARG;
2983 /* The virtual folders' locations are not user-dependent */
2984 *ppidl = NULL;
2985 switch (nFolder & CSIDL_FOLDER_MASK)
2987 case CSIDL_DESKTOP:
2988 *ppidl = _ILCreateDesktop();
2989 break;
2991 case CSIDL_PERSONAL:
2992 *ppidl = _ILCreateMyDocuments();
2993 break;
2995 case CSIDL_INTERNET:
2996 *ppidl = _ILCreateIExplore();
2997 break;
2999 case CSIDL_CONTROLS:
3000 *ppidl = _ILCreateControlPanel();
3001 break;
3003 case CSIDL_PRINTERS:
3004 *ppidl = _ILCreatePrinters();
3005 break;
3007 case CSIDL_BITBUCKET:
3008 *ppidl = _ILCreateBitBucket();
3009 break;
3011 case CSIDL_DRIVES:
3012 *ppidl = _ILCreateMyComputer();
3013 break;
3015 case CSIDL_NETWORK:
3016 *ppidl = _ILCreateNetwork();
3017 break;
3019 default:
3021 WCHAR szPath[MAX_PATH];
3023 hr = SHGetFolderPathW(hwndOwner, nFolder, hToken,
3024 SHGFP_TYPE_CURRENT, szPath);
3025 if (SUCCEEDED(hr))
3027 DWORD attributes=0;
3029 TRACE("Value=%s\n", debugstr_w(szPath));
3030 hr = SHILCreateFromPathW(szPath, ppidl, &attributes);
3032 else if (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
3034 /* unlike SHGetFolderPath, SHGetFolderLocation in shell32
3035 * version 6.0 returns E_FAIL for nonexistent paths
3037 hr = E_FAIL;
3041 if(*ppidl)
3042 hr = S_OK;
3044 TRACE("-- (new pidl %p)\n",*ppidl);
3045 return hr;
3048 /*************************************************************************
3049 * SHGetSpecialFolderLocation [SHELL32.@]
3051 * NOTES
3052 * In NT5, SHGetSpecialFolderLocation needs the <winntdir>/Recent
3053 * directory.
3055 HRESULT WINAPI SHGetSpecialFolderLocation(
3056 HWND hwndOwner,
3057 INT nFolder,
3058 LPITEMIDLIST * ppidl)
3060 HRESULT hr = E_INVALIDARG;
3062 TRACE("(%p,0x%x,%p)\n", hwndOwner,nFolder,ppidl);
3064 if (!ppidl)
3065 return E_INVALIDARG;
3067 hr = SHGetFolderLocation(hwndOwner, nFolder, NULL, 0, ppidl);
3068 return hr;
3071 static int csidl_from_id( const KNOWNFOLDERID *id )
3073 int i;
3074 for (i = 0; i < sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]); i++)
3075 if (IsEqualGUID( CSIDL_Data[i].id, id )) return i;
3076 return -1;
3079 /*************************************************************************
3080 * SHGetKnownFolderPath [SHELL32.@]
3082 HRESULT WINAPI SHGetKnownFolderPath(REFKNOWNFOLDERID rfid, DWORD flags, HANDLE token, PWSTR *path)
3084 HRESULT hr;
3085 WCHAR folder[MAX_PATH];
3086 int index = csidl_from_id( rfid );
3088 TRACE("%s, 0x%08x, %p, %p\n", debugstr_guid(rfid), flags, token, path);
3090 *path = NULL;
3092 if (index < 0)
3093 return HRESULT_FROM_WIN32( ERROR_FILE_NOT_FOUND );
3095 if (flags & KF_FLAG_CREATE)
3096 index |= CSIDL_FLAG_CREATE;
3098 if (flags & KF_FLAG_DONT_VERIFY)
3099 index |= CSIDL_FLAG_DONT_VERIFY;
3101 if (flags & KF_FLAG_NO_ALIAS)
3102 index |= CSIDL_FLAG_NO_ALIAS;
3104 if (flags & KF_FLAG_INIT)
3105 index |= CSIDL_FLAG_PER_USER_INIT;
3107 if (flags & ~(KF_FLAG_CREATE|KF_FLAG_DONT_VERIFY|KF_FLAG_NO_ALIAS|KF_FLAG_INIT))
3109 FIXME("flags 0x%08x not supported\n", flags);
3110 return E_INVALIDARG;
3113 hr = SHGetFolderPathW( NULL, index, token, 0, folder );
3114 if (SUCCEEDED(hr))
3116 *path = CoTaskMemAlloc( (strlenW( folder ) + 1) * sizeof(WCHAR) );
3117 if (!*path)
3118 return E_OUTOFMEMORY;
3119 strcpyW( *path, folder );
3121 return hr;
3124 /*************************************************************************
3125 * SHGetFolderPathEx [SHELL32.@]
3127 HRESULT WINAPI SHGetFolderPathEx(REFKNOWNFOLDERID rfid, DWORD flags, HANDLE token, LPWSTR path, DWORD len)
3129 HRESULT hr;
3130 WCHAR *buffer;
3132 TRACE("%s, 0x%08x, %p, %p, %u\n", debugstr_guid(rfid), flags, token, path, len);
3134 if (!path || !len) return E_INVALIDARG;
3136 hr = SHGetKnownFolderPath( rfid, flags, token, &buffer );
3137 if (SUCCEEDED( hr ))
3139 if (strlenW( buffer ) + 1 > len)
3141 CoTaskMemFree( buffer );
3142 return HRESULT_FROM_WIN32( ERROR_INSUFFICIENT_BUFFER );
3144 strcpyW( path, buffer );
3145 CoTaskMemFree( buffer );
3147 return hr;
3150 /* constant values used by known folder functions */
3151 static const WCHAR szCategory[] = {'C','a','t','e','g','o','r','y',0};
3152 static const WCHAR szName[] = {'N','a','m','e',0};
3153 static const WCHAR szRelativePath[] = {'R','e','l','a','t','i','v','e','P','a','t','h',0};
3154 static const WCHAR szParentFolder[] = {'P','a','r','e','n','t','F','o','l','d','e','r',0};
3157 * Internal function to convert known folder identifier to path of registry key
3158 * associated with known folder.
3160 * Parameters:
3161 * rfid [I] pointer to known folder identifier (may be NULL)
3162 * lpStringGuid [I] string with known folder identifier (used when rfid is NULL)
3163 * lpPath [O] place to store string address. String should be
3164 * later freed using HeapFree(GetProcessHeap(),0, ... )
3166 static HRESULT get_known_folder_registry_path(
3167 REFKNOWNFOLDERID rfid,
3168 LPWSTR lpStringGuid,
3169 LPWSTR *lpPath)
3171 static const WCHAR sBackslash[] = {'\\',0};
3172 HRESULT hr = S_OK;
3173 int length;
3174 WCHAR sGuid[50];
3176 TRACE("(%s, %s, %p)\n", debugstr_guid(rfid), debugstr_w(lpStringGuid), lpPath);
3178 if(rfid)
3179 StringFromGUID2(rfid, sGuid, sizeof(sGuid)/sizeof(sGuid[0]));
3180 else
3181 lstrcpyW(sGuid, lpStringGuid);
3183 length = lstrlenW(szKnownFolderDescriptions)+51;
3184 *lpPath = HeapAlloc(GetProcessHeap(), 0, length*sizeof(WCHAR));
3185 if(!(*lpPath))
3186 hr = E_OUTOFMEMORY;
3188 if(SUCCEEDED(hr))
3190 lstrcpyW(*lpPath, szKnownFolderDescriptions);
3191 lstrcatW(*lpPath, sBackslash);
3192 lstrcatW(*lpPath, sGuid);
3195 return hr;
3199 * Internal function to get place where folder redirection information are stored.
3201 * Parameters:
3202 * rfid [I] pointer to known folder identifier (may be NULL)
3203 * rootKey [O] root key where the redirection information are stored
3204 * It can be HKLM for COMMON folders, and HKCU for PERUSER folders.
3205 * However, besides root key, path is always that same, and is stored
3206 * as "szKnownFolderRedirections" constant
3208 static HRESULT get_known_folder_redirection_place(
3209 REFKNOWNFOLDERID rfid,
3210 HKEY *rootKey)
3212 HRESULT hr;
3213 LPWSTR lpRegistryPath = NULL;
3214 KF_CATEGORY category;
3215 DWORD dwSize;
3217 /* first, get known folder's category */
3218 hr = get_known_folder_registry_path(rfid, NULL, &lpRegistryPath);
3220 if(SUCCEEDED(hr))
3222 dwSize = sizeof(category);
3223 hr = HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE, lpRegistryPath, szCategory, RRF_RT_DWORD, NULL, &category, &dwSize));
3226 if(SUCCEEDED(hr))
3228 if(category == KF_CATEGORY_COMMON)
3230 *rootKey = HKEY_LOCAL_MACHINE;
3231 hr = S_OK;
3233 else if(category == KF_CATEGORY_PERUSER)
3235 *rootKey = HKEY_CURRENT_USER;
3236 hr = S_OK;
3238 else
3239 hr = E_FAIL;
3242 HeapFree(GetProcessHeap(), 0, lpRegistryPath);
3243 return hr;
3246 static HRESULT get_known_folder_path_by_id(REFKNOWNFOLDERID folderId, LPWSTR lpRegistryPath, DWORD dwFlags, LPWSTR *ppszPath);
3248 static HRESULT get_known_folder_category(
3249 LPWSTR registryPath,
3250 KF_CATEGORY* pCategory)
3252 DWORD dwSize = sizeof(DWORD);
3253 DWORD dwType;
3254 return HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE, registryPath, szCategory, RRF_RT_DWORD, &dwType, pCategory, &dwSize));
3257 static HRESULT redirect_known_folder(
3258 REFKNOWNFOLDERID rfid,
3259 HWND hwnd,
3260 KF_REDIRECT_FLAGS flags,
3261 LPCWSTR pszTargetPath,
3262 UINT cFolders,
3263 KNOWNFOLDERID const *pExclusion,
3264 LPWSTR *ppszError)
3266 HRESULT hr;
3267 HKEY rootKey = HKEY_LOCAL_MACHINE, hKey;
3268 WCHAR sGuid[39];
3269 LPWSTR lpRegistryPath = NULL, lpSrcPath = NULL;
3270 TRACE("(%s, %p, 0x%08x, %s, %d, %p, %p)\n", debugstr_guid(rfid), hwnd, flags, debugstr_w(pszTargetPath), cFolders, pExclusion, ppszError);
3272 if (ppszError) *ppszError = NULL;
3274 hr = get_known_folder_registry_path(rfid, NULL, &lpRegistryPath);
3276 if(SUCCEEDED(hr))
3277 hr = get_known_folder_path_by_id(rfid, lpRegistryPath, 0, &lpSrcPath);
3279 HeapFree(GetProcessHeap(), 0, lpRegistryPath);
3281 /* get path to redirection storage */
3282 if(SUCCEEDED(hr))
3283 hr = get_known_folder_redirection_place(rfid, &rootKey);
3285 /* write redirection information */
3286 if(SUCCEEDED(hr))
3287 hr = HRESULT_FROM_WIN32(RegCreateKeyExW(rootKey, szKnownFolderRedirections, 0, NULL, 0, KEY_WRITE, NULL, &hKey, NULL));
3289 if(SUCCEEDED(hr))
3291 StringFromGUID2(rfid, sGuid, sizeof(sGuid)/sizeof(sGuid[0]));
3293 hr = HRESULT_FROM_WIN32(RegSetValueExW(hKey, sGuid, 0, REG_SZ, (LPBYTE)pszTargetPath, (lstrlenW(pszTargetPath)+1)*sizeof(WCHAR)));
3295 RegCloseKey(hKey);
3298 /* make sure destination path exists */
3299 SHCreateDirectory(NULL, pszTargetPath);
3301 /* copy content if required */
3302 if(SUCCEEDED(hr) && (flags & KF_REDIRECT_COPY_CONTENTS) )
3304 static const WCHAR sWildcard[] = {'\\','*',0};
3305 WCHAR srcPath[MAX_PATH+1], dstPath[MAX_PATH+1];
3306 SHFILEOPSTRUCTW fileOp;
3308 ZeroMemory(srcPath, sizeof(srcPath));
3309 lstrcpyW(srcPath, lpSrcPath);
3310 lstrcatW(srcPath, sWildcard);
3312 ZeroMemory(dstPath, sizeof(dstPath));
3313 lstrcpyW(dstPath, pszTargetPath);
3315 ZeroMemory(&fileOp, sizeof(fileOp));
3317 if(flags & KF_REDIRECT_DEL_SOURCE_CONTENTS)
3318 fileOp.wFunc = FO_MOVE;
3319 else
3320 fileOp.wFunc = FO_COPY;
3322 fileOp.pFrom = srcPath;
3323 fileOp.pTo = dstPath;
3324 fileOp.fFlags = FOF_NO_UI;
3326 hr = (SHFileOperationW(&fileOp)==0 ? S_OK : E_FAIL);
3328 if(flags & KF_REDIRECT_DEL_SOURCE_CONTENTS)
3330 ZeroMemory(srcPath, sizeof(srcPath));
3331 lstrcpyW(srcPath, lpSrcPath);
3333 ZeroMemory(&fileOp, sizeof(fileOp));
3334 fileOp.wFunc = FO_DELETE;
3335 fileOp.pFrom = srcPath;
3336 fileOp.fFlags = FOF_NO_UI;
3338 hr = (SHFileOperationW(&fileOp)==0 ? S_OK : E_FAIL);
3342 CoTaskMemFree(lpSrcPath);
3344 return hr;
3348 struct knownfolder
3350 IKnownFolder IKnownFolder_iface;
3351 LONG refs;
3352 KNOWNFOLDERID id;
3353 LPWSTR registryPath;
3356 static inline struct knownfolder *impl_from_IKnownFolder( IKnownFolder *iface )
3358 return CONTAINING_RECORD( iface, struct knownfolder, IKnownFolder_iface );
3361 static ULONG WINAPI knownfolder_AddRef(
3362 IKnownFolder *iface )
3364 struct knownfolder *knownfolder = impl_from_IKnownFolder( iface );
3365 return InterlockedIncrement( &knownfolder->refs );
3368 static ULONG WINAPI knownfolder_Release(
3369 IKnownFolder *iface )
3371 struct knownfolder *knownfolder = impl_from_IKnownFolder( iface );
3372 LONG refs = InterlockedDecrement( &knownfolder->refs );
3373 if (!refs)
3375 TRACE("destroying %p\n", knownfolder);
3376 HeapFree( GetProcessHeap(), 0, knownfolder->registryPath);
3377 HeapFree( GetProcessHeap(), 0, knownfolder );
3379 return refs;
3382 static HRESULT WINAPI knownfolder_QueryInterface(
3383 IKnownFolder *iface,
3384 REFIID riid,
3385 void **ppv )
3387 struct knownfolder *This = impl_from_IKnownFolder( iface );
3389 TRACE("%p %s %p\n", This, debugstr_guid( riid ), ppv );
3391 if ( IsEqualGUID( riid, &IID_IKnownFolder ) ||
3392 IsEqualGUID( riid, &IID_IUnknown ) )
3394 *ppv = iface;
3396 else
3398 FIXME("interface %s not implemented\n", debugstr_guid(riid));
3399 return E_NOINTERFACE;
3401 IKnownFolder_AddRef( iface );
3402 return S_OK;
3405 static HRESULT knownfolder_set_id(
3406 struct knownfolder *knownfolder,
3407 const KNOWNFOLDERID *kfid)
3409 HKEY hKey;
3410 HRESULT hr;
3412 TRACE("%s\n", debugstr_guid(kfid));
3414 knownfolder->id = *kfid;
3416 /* check is it registry-registered folder */
3417 hr = get_known_folder_registry_path(kfid, NULL, &knownfolder->registryPath);
3418 if(SUCCEEDED(hr))
3419 hr = HRESULT_FROM_WIN32(RegOpenKeyExW(HKEY_LOCAL_MACHINE, knownfolder->registryPath, 0, 0, &hKey));
3421 if(SUCCEEDED(hr))
3423 hr = S_OK;
3424 RegCloseKey(hKey);
3426 else
3428 /* This known folder is not registered. To mark it, we set registryPath to NULL */
3429 HeapFree(GetProcessHeap(), 0, knownfolder->registryPath);
3430 knownfolder->registryPath = NULL;
3431 hr = S_OK;
3434 return hr;
3437 static HRESULT WINAPI knownfolder_GetId(
3438 IKnownFolder *iface,
3439 KNOWNFOLDERID *pkfid)
3441 struct knownfolder *knownfolder = impl_from_IKnownFolder( iface );
3443 TRACE("%p\n", pkfid);
3445 *pkfid = knownfolder->id;
3446 return S_OK;
3449 static HRESULT WINAPI knownfolder_GetCategory(
3450 IKnownFolder *iface,
3451 KF_CATEGORY *pCategory)
3453 struct knownfolder *knownfolder = impl_from_IKnownFolder(iface);
3454 HRESULT hr = S_OK;
3456 TRACE("%p, %p\n", knownfolder, pCategory);
3458 /* we cannot get a category for a folder which is not registered */
3459 if(!knownfolder->registryPath)
3460 hr = E_FAIL;
3462 if(SUCCEEDED(hr))
3463 hr = get_known_folder_category(knownfolder->registryPath, pCategory);
3465 return hr;
3468 static HRESULT WINAPI knownfolder_GetShellItem(
3469 IKnownFolder *iface,
3470 DWORD dwFlags,
3471 REFIID riid,
3472 void **ppv)
3474 FIXME("0x%08x, %s, %p\n", dwFlags, debugstr_guid(riid), ppv);
3475 return E_NOTIMPL;
3478 static HRESULT get_known_folder_path(
3479 LPWSTR sFolderId,
3480 LPWSTR registryPath,
3481 LPWSTR *ppszPath)
3483 static const WCHAR sBackslash[] = {'\\',0};
3484 HRESULT hr;
3485 DWORD dwSize, dwType;
3486 WCHAR path[MAX_PATH] = {0};
3487 WCHAR parentGuid[39];
3488 KF_CATEGORY category;
3489 LPWSTR parentRegistryPath, parentPath;
3490 HKEY hRedirectionRootKey = NULL;
3492 TRACE("(%s, %p)\n", debugstr_w(registryPath), ppszPath);
3493 *ppszPath = NULL;
3495 /* check if folder has parent */
3496 dwSize = sizeof(parentGuid);
3497 hr = HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE, registryPath, szParentFolder, RRF_RT_REG_SZ, &dwType, parentGuid, &dwSize));
3498 if(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) hr = S_FALSE;
3500 if(hr == S_OK)
3502 /* get parent's known folder path (recursive) */
3503 hr = get_known_folder_registry_path(NULL, parentGuid, &parentRegistryPath);
3504 if(FAILED(hr)) return hr;
3506 hr = get_known_folder_path(parentGuid, parentRegistryPath, &parentPath);
3507 if(FAILED(hr)) {
3508 HeapFree(GetProcessHeap(), 0, parentRegistryPath);
3509 return hr;
3512 lstrcatW(path, parentPath);
3513 lstrcatW(path, sBackslash);
3515 HeapFree(GetProcessHeap(), 0, parentRegistryPath);
3516 HeapFree(GetProcessHeap(), 0, parentPath);
3519 /* check, if folder was redirected */
3520 if(SUCCEEDED(hr))
3521 hr = HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE, registryPath, szCategory, RRF_RT_REG_DWORD, NULL, &category, &dwSize));
3523 if(SUCCEEDED(hr))
3525 if(category == KF_CATEGORY_COMMON)
3526 hRedirectionRootKey = HKEY_LOCAL_MACHINE;
3527 else if(category == KF_CATEGORY_PERUSER)
3528 hRedirectionRootKey = HKEY_CURRENT_USER;
3530 if(hRedirectionRootKey)
3532 hr = HRESULT_FROM_WIN32(RegGetValueW(hRedirectionRootKey, szKnownFolderRedirections, sFolderId, RRF_RT_REG_SZ, NULL, NULL, &dwSize));
3534 if(SUCCEEDED(hr))
3536 *ppszPath = CoTaskMemAlloc(dwSize+(lstrlenW(path)+1)*sizeof(WCHAR));
3537 if(!*ppszPath) hr = E_OUTOFMEMORY;
3540 if(SUCCEEDED(hr))
3542 lstrcpyW(*ppszPath, path);
3543 hr = HRESULT_FROM_WIN32(RegGetValueW(hRedirectionRootKey, szKnownFolderRedirections, sFolderId, RRF_RT_REG_SZ, NULL, *ppszPath + lstrlenW(path), &dwSize));
3547 if(!*ppszPath)
3549 /* no redirection, use previous way - read the relative path from folder definition */
3550 hr = HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE, registryPath, szRelativePath, RRF_RT_REG_SZ, &dwType, NULL, &dwSize));
3552 if(SUCCEEDED(hr))
3554 *ppszPath = CoTaskMemAlloc(dwSize+(lstrlenW(path)+1)*sizeof(WCHAR));
3555 if(!*ppszPath) hr = E_OUTOFMEMORY;
3558 if(SUCCEEDED(hr))
3560 lstrcpyW(*ppszPath, path);
3561 hr = HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE, registryPath, szRelativePath, RRF_RT_REG_SZ, &dwType, *ppszPath + lstrlenW(path), &dwSize));
3566 TRACE("returning path: %s\n", debugstr_w(*ppszPath));
3567 return hr;
3570 static HRESULT get_known_folder_path_by_id(
3571 REFKNOWNFOLDERID folderId,
3572 LPWSTR lpRegistryPath,
3573 DWORD dwFlags,
3574 LPWSTR *ppszPath)
3576 HRESULT hr;
3577 WCHAR sGuid[39];
3578 DWORD dwAttributes;
3580 TRACE("(%s, %s, 0x%08x, %p)\n", debugstr_guid(folderId), debugstr_w(lpRegistryPath), dwFlags, ppszPath);
3582 /* if this is registry-registered known folder, get path from registry */
3583 if(lpRegistryPath)
3585 StringFromGUID2(folderId, sGuid, sizeof(sGuid)/sizeof(sGuid[0]));
3587 hr = get_known_folder_path(sGuid, lpRegistryPath, ppszPath);
3589 /* in other case, use older way */
3590 else
3591 hr = SHGetKnownFolderPath( folderId, dwFlags, NULL, ppszPath );
3593 if (FAILED(hr)) return hr;
3595 /* check if known folder really exists */
3596 dwAttributes = GetFileAttributesW(*ppszPath);
3597 if(dwAttributes == INVALID_FILE_ATTRIBUTES || !(dwAttributes & FILE_ATTRIBUTE_DIRECTORY) )
3599 TRACE("directory %s not found\n", debugstr_w(*ppszPath));
3600 CoTaskMemFree(*ppszPath);
3601 *ppszPath = NULL;
3602 hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
3605 return hr;
3608 static HRESULT WINAPI knownfolder_GetPath(
3609 IKnownFolder *iface,
3610 DWORD dwFlags,
3611 LPWSTR *ppszPath)
3613 struct knownfolder *knownfolder = impl_from_IKnownFolder( iface );
3614 TRACE("(%p, 0x%08x, %p)\n", knownfolder, dwFlags, ppszPath);
3616 return get_known_folder_path_by_id(&knownfolder->id, knownfolder->registryPath, dwFlags, ppszPath);
3619 static HRESULT WINAPI knownfolder_SetPath(
3620 IKnownFolder *iface,
3621 DWORD dwFlags,
3622 LPCWSTR pszPath)
3624 struct knownfolder *knownfolder = impl_from_IKnownFolder( iface );
3625 HRESULT hr = S_OK;
3627 TRACE("(%p, 0x%08x, %s)\n", knownfolder, dwFlags, debugstr_w(pszPath));
3629 /* check if the known folder is registered */
3630 if(!knownfolder->registryPath)
3631 hr = E_FAIL;
3633 if(SUCCEEDED(hr))
3634 hr = redirect_known_folder(&knownfolder->id, NULL, 0, pszPath, 0, NULL, NULL);
3636 return hr;
3639 static HRESULT WINAPI knownfolder_GetIDList(
3640 IKnownFolder *iface,
3641 DWORD dwFlags,
3642 PIDLIST_ABSOLUTE *ppidl)
3644 FIXME("0x%08x, %p\n", dwFlags, ppidl);
3645 return E_NOTIMPL;
3648 static HRESULT WINAPI knownfolder_GetFolderType(
3649 IKnownFolder *iface,
3650 FOLDERTYPEID *pftid)
3652 FIXME("%p\n", pftid);
3653 return E_NOTIMPL;
3656 static HRESULT WINAPI knownfolder_GetRedirectionCapabilities(
3657 IKnownFolder *iface,
3658 KF_REDIRECTION_CAPABILITIES *pCapabilities)
3660 FIXME("%p\n", pCapabilities);
3661 return E_NOTIMPL;
3664 static HRESULT WINAPI knownfolder_GetFolderDefinition(
3665 IKnownFolder *iface,
3666 KNOWNFOLDER_DEFINITION *pKFD)
3668 struct knownfolder *knownfolder = impl_from_IKnownFolder( iface );
3669 HRESULT hr;
3670 DWORD dwSize;
3671 TRACE("(%p, %p)\n", knownfolder, pKFD);
3673 if(!pKFD) return E_INVALIDARG;
3675 ZeroMemory(pKFD, sizeof(*pKFD));
3677 hr = get_known_folder_category(knownfolder->registryPath, &pKFD->category);
3679 if(SUCCEEDED(hr))
3680 hr = HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE, knownfolder->registryPath, szName, RRF_RT_REG_SZ, NULL, NULL, &dwSize));
3682 if(SUCCEEDED(hr))
3684 pKFD->pszName = CoTaskMemAlloc(dwSize);
3685 if(!pKFD->pszName) hr = E_OUTOFMEMORY;
3688 if(SUCCEEDED(hr))
3689 hr = HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE, knownfolder->registryPath, szName, RRF_RT_REG_SZ, NULL, pKFD->pszName, &dwSize));
3691 return hr;
3694 static const struct IKnownFolderVtbl knownfolder_vtbl =
3696 knownfolder_QueryInterface,
3697 knownfolder_AddRef,
3698 knownfolder_Release,
3699 knownfolder_GetId,
3700 knownfolder_GetCategory,
3701 knownfolder_GetShellItem,
3702 knownfolder_GetPath,
3703 knownfolder_SetPath,
3704 knownfolder_GetIDList,
3705 knownfolder_GetFolderType,
3706 knownfolder_GetRedirectionCapabilities,
3707 knownfolder_GetFolderDefinition
3710 static HRESULT knownfolder_create( struct knownfolder **knownfolder )
3712 struct knownfolder *kf;
3714 kf = HeapAlloc( GetProcessHeap(), 0, sizeof(*kf) );
3715 if (!kf) return E_OUTOFMEMORY;
3717 kf->IKnownFolder_iface.lpVtbl = &knownfolder_vtbl;
3718 kf->refs = 1;
3719 memset( &kf->id, 0, sizeof(kf->id) );
3720 kf->registryPath = NULL;
3722 *knownfolder = kf;
3724 TRACE("returning iface %p\n", &kf->IKnownFolder_iface);
3725 return S_OK;
3728 struct foldermanager
3730 IKnownFolderManager IKnownFolderManager_iface;
3731 LONG refs;
3732 UINT num_ids;
3733 KNOWNFOLDERID *ids;
3736 static inline struct foldermanager *impl_from_IKnownFolderManager( IKnownFolderManager *iface )
3738 return CONTAINING_RECORD( iface, struct foldermanager, IKnownFolderManager_iface );
3741 static ULONG WINAPI foldermanager_AddRef(
3742 IKnownFolderManager *iface )
3744 struct foldermanager *foldermanager = impl_from_IKnownFolderManager( iface );
3745 return InterlockedIncrement( &foldermanager->refs );
3748 static ULONG WINAPI foldermanager_Release(
3749 IKnownFolderManager *iface )
3751 struct foldermanager *foldermanager = impl_from_IKnownFolderManager( iface );
3752 LONG refs = InterlockedDecrement( &foldermanager->refs );
3753 if (!refs)
3755 TRACE("destroying %p\n", foldermanager);
3756 HeapFree( GetProcessHeap(), 0, foldermanager->ids );
3757 HeapFree( GetProcessHeap(), 0, foldermanager );
3759 return refs;
3762 static HRESULT WINAPI foldermanager_QueryInterface(
3763 IKnownFolderManager *iface,
3764 REFIID riid,
3765 void **ppv )
3767 struct foldermanager *This = impl_from_IKnownFolderManager( iface );
3769 TRACE("%p %s %p\n", This, debugstr_guid( riid ), ppv );
3771 if ( IsEqualGUID( riid, &IID_IKnownFolderManager ) ||
3772 IsEqualGUID( riid, &IID_IUnknown ) )
3774 *ppv = iface;
3776 else
3778 FIXME("interface %s not implemented\n", debugstr_guid(riid));
3779 return E_NOINTERFACE;
3781 IKnownFolderManager_AddRef( iface );
3782 return S_OK;
3785 static HRESULT WINAPI foldermanager_FolderIdFromCsidl(
3786 IKnownFolderManager *iface,
3787 int nCsidl,
3788 KNOWNFOLDERID *pfid)
3790 TRACE("%d, %p\n", nCsidl, pfid);
3792 if (nCsidl >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
3793 return E_INVALIDARG;
3794 *pfid = *CSIDL_Data[nCsidl].id;
3795 return S_OK;
3798 static HRESULT WINAPI foldermanager_FolderIdToCsidl(
3799 IKnownFolderManager *iface,
3800 REFKNOWNFOLDERID rfid,
3801 int *pnCsidl)
3803 int csidl;
3805 TRACE("%s, %p\n", debugstr_guid(rfid), pnCsidl);
3807 csidl = csidl_from_id( rfid );
3808 if (csidl == -1) return E_INVALIDARG;
3809 *pnCsidl = csidl;
3810 return S_OK;
3813 static HRESULT WINAPI foldermanager_GetFolderIds(
3814 IKnownFolderManager *iface,
3815 KNOWNFOLDERID **ppKFId,
3816 UINT *pCount)
3818 struct foldermanager *fm = impl_from_IKnownFolderManager( iface );
3820 TRACE("%p, %p\n", ppKFId, pCount);
3822 *ppKFId = fm->ids;
3823 *pCount = fm->num_ids;
3824 return S_OK;
3827 static BOOL is_knownfolder( struct foldermanager *fm, const KNOWNFOLDERID *id )
3829 UINT i;
3830 HRESULT hr;
3831 LPWSTR registryPath = NULL;
3832 HKEY hKey;
3834 /* TODO: move all entries from "CSIDL_Data" static array to registry known folder descriptions */
3835 for (i = 0; i < fm->num_ids; i++)
3836 if (IsEqualGUID( &fm->ids[i], id )) return TRUE;
3838 hr = get_known_folder_registry_path(id, NULL, &registryPath);
3839 if(SUCCEEDED(hr))
3841 hr = HRESULT_FROM_WIN32(RegOpenKeyExW(HKEY_LOCAL_MACHINE, registryPath, 0, 0, &hKey));
3842 HeapFree(GetProcessHeap(), 0, registryPath);
3845 if(SUCCEEDED(hr))
3847 hr = S_OK;
3848 RegCloseKey(hKey);
3851 return hr == S_OK;
3854 static HRESULT WINAPI foldermanager_GetFolder(
3855 IKnownFolderManager *iface,
3856 REFKNOWNFOLDERID rfid,
3857 IKnownFolder **ppkf)
3859 struct foldermanager *fm = impl_from_IKnownFolderManager( iface );
3860 struct knownfolder *kf;
3861 HRESULT hr;
3863 TRACE("%s, %p\n", debugstr_guid(rfid), ppkf);
3865 if (!is_knownfolder( fm, rfid ))
3867 WARN("unknown folder\n");
3868 return E_INVALIDARG;
3870 hr = knownfolder_create( &kf );
3871 if (SUCCEEDED( hr ))
3873 hr = knownfolder_set_id( kf, rfid );
3874 *ppkf = &kf->IKnownFolder_iface;
3876 else
3877 *ppkf = NULL;
3879 return hr;
3882 static HRESULT WINAPI foldermanager_GetFolderByName(
3883 IKnownFolderManager *iface,
3884 LPCWSTR pszCanonicalName,
3885 IKnownFolder **ppkf)
3887 FIXME("%s, %p\n", debugstr_w(pszCanonicalName), ppkf);
3888 return E_NOTIMPL;
3891 static HRESULT WINAPI foldermanager_RegisterFolder(
3892 IKnownFolderManager *iface,
3893 REFKNOWNFOLDERID rfid,
3894 KNOWNFOLDER_DEFINITION const *pKFD)
3896 HRESULT hr;
3897 HKEY hKey = NULL;
3898 DWORD dwDisp;
3899 LPWSTR registryPath = NULL;
3900 TRACE("(%p, %s, %p)\n", iface, debugstr_guid(rfid), pKFD);
3902 hr = get_known_folder_registry_path(rfid, NULL, &registryPath);
3903 TRACE("registry path: %s\n", debugstr_w(registryPath));
3905 if(SUCCEEDED(hr))
3906 hr = HRESULT_FROM_WIN32(RegCreateKeyExW(HKEY_LOCAL_MACHINE, registryPath, 0, NULL, 0, KEY_WRITE, 0, &hKey, &dwDisp));
3908 if(SUCCEEDED(hr))
3910 if(dwDisp == REG_OPENED_EXISTING_KEY)
3911 hr = E_FAIL;
3913 if(SUCCEEDED(hr))
3914 hr = HRESULT_FROM_WIN32(RegSetValueExW(hKey, szCategory, 0, REG_DWORD, (LPBYTE)&pKFD->category, sizeof(pKFD->category)));
3916 if(SUCCEEDED(hr))
3917 hr = HRESULT_FROM_WIN32(RegSetValueExW(hKey, szName, 0, REG_SZ, (LPBYTE)pKFD->pszName, (lstrlenW(pKFD->pszName)+1)*sizeof(WCHAR) ));
3919 if(SUCCEEDED(hr) && !IsEqualGUID(&pKFD->fidParent, &GUID_NULL))
3921 WCHAR sParentGuid[39];
3922 StringFromGUID2(&pKFD->fidParent, sParentGuid, sizeof(sParentGuid)/sizeof(sParentGuid[0]));
3924 /* this known folder has parent folder */
3925 hr = HRESULT_FROM_WIN32(RegSetValueExW(hKey, szParentFolder, 0, REG_SZ, (LPBYTE)sParentGuid, sizeof(sParentGuid)));
3928 if(SUCCEEDED(hr) && pKFD->category != KF_CATEGORY_VIRTUAL)
3930 if(!pKFD->pszRelativePath)
3931 hr = E_INVALIDARG;
3933 if(SUCCEEDED(hr))
3934 hr = HRESULT_FROM_WIN32(RegSetValueExW(hKey, szRelativePath, 0, REG_SZ, (LPBYTE)pKFD->pszRelativePath, (lstrlenW(pKFD->pszRelativePath)+1)*sizeof(WCHAR) ));
3937 RegCloseKey(hKey);
3939 if(FAILED(hr))
3940 SHDeleteKeyW(HKEY_LOCAL_MACHINE, registryPath);
3943 HeapFree(GetProcessHeap(), 0, registryPath);
3944 return hr;
3947 static HRESULT WINAPI foldermanager_UnregisterFolder(
3948 IKnownFolderManager *iface,
3949 REFKNOWNFOLDERID rfid)
3951 HRESULT hr;
3952 LPWSTR registryPath = NULL;
3953 TRACE("(%p, %s)\n", iface, debugstr_guid(rfid));
3955 hr = get_known_folder_registry_path(rfid, NULL, &registryPath);
3957 if(SUCCEEDED(hr))
3958 hr = HRESULT_FROM_WIN32(SHDeleteKeyW(HKEY_LOCAL_MACHINE, registryPath));
3960 HeapFree(GetProcessHeap(), 0, registryPath);
3961 return hr;
3964 static HRESULT WINAPI foldermanager_FindFolderFromPath(
3965 IKnownFolderManager *iface,
3966 LPCWSTR pszPath,
3967 FFFP_MODE mode,
3968 IKnownFolder **ppkf)
3970 FIXME("%s, 0x%08x, %p\n", debugstr_w(pszPath), mode, ppkf);
3971 return E_NOTIMPL;
3974 static HRESULT WINAPI foldermanager_FindFolderFromIDList(
3975 IKnownFolderManager *iface,
3976 PCIDLIST_ABSOLUTE pidl,
3977 IKnownFolder **ppkf)
3979 FIXME("%p, %p\n", pidl, ppkf);
3980 return E_NOTIMPL;
3983 static HRESULT WINAPI foldermanager_Redirect(
3984 IKnownFolderManager *iface,
3985 REFKNOWNFOLDERID rfid,
3986 HWND hwnd,
3987 KF_REDIRECT_FLAGS flags,
3988 LPCWSTR pszTargetPath,
3989 UINT cFolders,
3990 KNOWNFOLDERID const *pExclusion,
3991 LPWSTR *ppszError)
3993 return redirect_known_folder(rfid, hwnd, flags, pszTargetPath, cFolders, pExclusion, ppszError);
3996 static const struct IKnownFolderManagerVtbl foldermanager_vtbl =
3998 foldermanager_QueryInterface,
3999 foldermanager_AddRef,
4000 foldermanager_Release,
4001 foldermanager_FolderIdFromCsidl,
4002 foldermanager_FolderIdToCsidl,
4003 foldermanager_GetFolderIds,
4004 foldermanager_GetFolder,
4005 foldermanager_GetFolderByName,
4006 foldermanager_RegisterFolder,
4007 foldermanager_UnregisterFolder,
4008 foldermanager_FindFolderFromPath,
4009 foldermanager_FindFolderFromIDList,
4010 foldermanager_Redirect
4013 static HRESULT foldermanager_create( void **ppv )
4015 UINT i, j;
4016 struct foldermanager *fm;
4018 fm = HeapAlloc( GetProcessHeap(), 0, sizeof(*fm) );
4019 if (!fm) return E_OUTOFMEMORY;
4021 fm->IKnownFolderManager_iface.lpVtbl = &foldermanager_vtbl;
4022 fm->refs = 1;
4023 fm->num_ids = 0;
4025 for (i = 0; i < sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]); i++)
4027 if (!IsEqualGUID( CSIDL_Data[i].id, &GUID_NULL )) fm->num_ids++;
4029 fm->ids = HeapAlloc( GetProcessHeap(), 0, fm->num_ids * sizeof(KNOWNFOLDERID) );
4030 if (!fm->ids)
4032 HeapFree( GetProcessHeap(), 0, fm );
4033 return E_OUTOFMEMORY;
4035 for (i = j = 0; i < sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]); i++)
4037 if (!IsEqualGUID( CSIDL_Data[i].id, &GUID_NULL ))
4039 fm->ids[j] = *CSIDL_Data[i].id;
4040 j++;
4043 TRACE("found %u known folders\n", fm->num_ids);
4044 *ppv = &fm->IKnownFolderManager_iface;
4046 TRACE("returning iface %p\n", *ppv);
4047 return S_OK;
4050 HRESULT WINAPI KnownFolderManager_Constructor( IUnknown *punk, REFIID riid, void **ppv )
4052 TRACE("%p, %s, %p\n", punk, debugstr_guid(riid), ppv);
4054 if (!ppv)
4055 return E_POINTER;
4056 if (punk)
4057 return CLASS_E_NOAGGREGATION;
4059 return foldermanager_create( ppv );
4062 HRESULT WINAPI SHGetKnownFolderIDList(REFKNOWNFOLDERID rfid, DWORD flags, HANDLE token, PIDLIST_ABSOLUTE *pidl)
4064 FIXME("%s, 0x%08x, %p, %p\n", debugstr_guid(rfid), flags, token, pidl);
4065 return E_NOTIMPL;