kernel32/tests: Add tabular UTF-7 encoding tests.
[wine.git] / dlls / shell32 / shellpath.c
blobe67babeb423769e46c56006ab1a7b34ae9b7ffe6
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_MusicW[] = {'M','y',' ','M','u','s','i','c','\0'};
814 static const WCHAR My_PicturesW[] = {'M','y',' ','P','i','c','t','u','r','e','s','\0'};
815 static const WCHAR My_VideosW[] = {'M','y',' ','V','i','d','e','o','s','\0'};
816 static const WCHAR NetHoodW[] = {'N','e','t','H','o','o','d','\0'};
817 static const WCHAR OEM_LinksW[] = {'O','E','M',' ','L','i','n','k','s','\0'};
818 static const WCHAR PersonalW[] = {'P','e','r','s','o','n','a','l','\0'};
819 static const WCHAR PicturesW[] = {'P','i','c','t','u','r','e','s','\0'};
820 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'};
821 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'};
822 static const WCHAR PrintHoodW[] = {'P','r','i','n','t','H','o','o','d','\0'};
823 static const WCHAR Program_FilesW[] = {'P','r','o','g','r','a','m',' ','F','i','l','e','s','\0'};
824 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'};
825 static const WCHAR Program_Files_x86W[] = {'P','r','o','g','r','a','m',' ','F','i','l','e','s',' ','(','x','8','6',')','\0'};
826 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'};
827 static const WCHAR ProgramFilesDirW[] = {'P','r','o','g','r','a','m','F','i','l','e','s','D','i','r','\0'};
828 static const WCHAR ProgramFilesDirX86W[] = {'P','r','o','g','r','a','m','F','i','l','e','s','D','i','r',' ','(','x','8','6',')','\0'};
829 static const WCHAR ProgramsW[] = {'P','r','o','g','r','a','m','s','\0'};
830 static const WCHAR RecentW[] = {'R','e','c','e','n','t','\0'};
831 static const WCHAR ResourcesW[] = {'R','e','s','o','u','r','c','e','s','\0'};
832 static const WCHAR Saved_GamesW[] = {'S','a','v','e','d',' ','G','a','m','e','s','\0'};
833 static const WCHAR SearchesW[] = {'S','e','a','r','c','h','e','s','\0'};
834 static const WCHAR SendToW[] = {'S','e','n','d','T','o','\0'};
835 static const WCHAR StartUpW[] = {'S','t','a','r','t','U','p','\0'};
836 static const WCHAR Start_MenuW[] = {'S','t','a','r','t',' ','M','e','n','u','\0'};
837 static const WCHAR Start_Menu_ProgramsW[] = {'S','t','a','r','t',' ','M','e','n','u','\\','P','r','o','g','r','a','m','s','\0'};
838 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'};
839 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'};
840 static const WCHAR TemplatesW[] = {'T','e','m','p','l','a','t','e','s','\0'};
841 static const WCHAR UsersW[] = {'U','s','e','r','s','\0'};
842 static const WCHAR UsersPublicW[] = {'U','s','e','r','s','\\','P','u','b','l','i','c','\0'};
843 static const WCHAR VideosW[] = {'V','i','d','e','o','s','\0'};
844 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'};
845 static const WCHAR DefaultW[] = {'.','D','e','f','a','u','l','t','\0'};
846 static const WCHAR AllUsersProfileW[] = {'%','A','L','L','U','S','E','R','S','P','R','O','F','I','L','E','%','\0'};
847 static const WCHAR UserProfileW[] = {'%','U','S','E','R','P','R','O','F','I','L','E','%','\0'};
848 static const WCHAR SystemDriveW[] = {'%','S','y','s','t','e','m','D','r','i','v','e','%','\0'};
849 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};
850 static const WCHAR ProfilesDirectoryW[] = {'P','r','o','f','i','l','e','s','D','i','r','e','c','t','o','r','y',0};
851 static const WCHAR AllUsersProfileValueW[] = {'A','l','l','U','s','e','r','s','P','r','o','f','i','l','e','\0'};
852 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'};
853 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'};
854 static const WCHAR szDefaultProfileDirW[] = {'u','s','e','r','s',0};
855 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'};
856 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};
857 static const WCHAR AllUsersW[] = {'P','u','b','l','i','c',0};
859 typedef enum _CSIDL_Type {
860 CSIDL_Type_User,
861 CSIDL_Type_AllUsers,
862 CSIDL_Type_CurrVer,
863 CSIDL_Type_Disallowed,
864 CSIDL_Type_NonExistent,
865 CSIDL_Type_WindowsPath,
866 CSIDL_Type_SystemPath,
867 CSIDL_Type_SystemX86Path,
868 } CSIDL_Type;
870 #define CSIDL_CONTACTS 0x0043
871 #define CSIDL_DOWNLOADS 0x0047
872 #define CSIDL_LINKS 0x004d
873 #define CSIDL_APPDATA_LOCALLOW 0x004e
874 #define CSIDL_SAVED_GAMES 0x0062
875 #define CSIDL_SEARCHES 0x0063
877 typedef struct
879 const KNOWNFOLDERID *id;
880 CSIDL_Type type;
881 LPCWSTR szValueName;
882 LPCWSTR szDefaultPath; /* fallback string or resource ID */
883 } CSIDL_DATA;
885 static const CSIDL_DATA CSIDL_Data[] =
887 { /* 0x00 - CSIDL_DESKTOP */
888 &FOLDERID_Desktop,
889 CSIDL_Type_User,
890 DesktopW,
891 MAKEINTRESOURCEW(IDS_DESKTOPDIRECTORY)
893 { /* 0x01 - CSIDL_INTERNET */
894 &FOLDERID_InternetFolder,
895 CSIDL_Type_Disallowed,
896 NULL,
897 NULL
899 { /* 0x02 - CSIDL_PROGRAMS */
900 &FOLDERID_Programs,
901 CSIDL_Type_User,
902 ProgramsW,
903 Start_Menu_ProgramsW
905 { /* 0x03 - CSIDL_CONTROLS (.CPL files) */
906 &FOLDERID_ControlPanelFolder,
907 CSIDL_Type_SystemPath,
908 NULL,
909 NULL
911 { /* 0x04 - CSIDL_PRINTERS */
912 &FOLDERID_PrintersFolder,
913 CSIDL_Type_SystemPath,
914 NULL,
915 NULL
917 { /* 0x05 - CSIDL_PERSONAL */
918 &FOLDERID_Documents,
919 CSIDL_Type_User,
920 PersonalW,
921 MAKEINTRESOURCEW(IDS_PERSONAL)
923 { /* 0x06 - CSIDL_FAVORITES */
924 &FOLDERID_Favorites,
925 CSIDL_Type_User,
926 FavoritesW,
927 FavoritesW
929 { /* 0x07 - CSIDL_STARTUP */
930 &FOLDERID_Startup,
931 CSIDL_Type_User,
932 StartUpW,
933 Start_Menu_StartupW
935 { /* 0x08 - CSIDL_RECENT */
936 &FOLDERID_Recent,
937 CSIDL_Type_User,
938 RecentW,
939 RecentW
941 { /* 0x09 - CSIDL_SENDTO */
942 &FOLDERID_SendTo,
943 CSIDL_Type_User,
944 SendToW,
945 SendToW
947 { /* 0x0a - CSIDL_BITBUCKET - Recycle Bin */
948 &FOLDERID_RecycleBinFolder,
949 CSIDL_Type_Disallowed,
950 NULL,
951 NULL,
953 { /* 0x0b - CSIDL_STARTMENU */
954 &FOLDERID_StartMenu,
955 CSIDL_Type_User,
956 Start_MenuW,
957 Start_MenuW
959 { /* 0x0c - CSIDL_MYDOCUMENTS */
960 &GUID_NULL,
961 CSIDL_Type_Disallowed, /* matches WinXP--can't get its path */
962 NULL,
963 NULL
965 { /* 0x0d - CSIDL_MYMUSIC */
966 &FOLDERID_Music,
967 CSIDL_Type_User,
968 My_MusicW,
969 MAKEINTRESOURCEW(IDS_MYMUSIC)
971 { /* 0x0e - CSIDL_MYVIDEO */
972 &FOLDERID_Videos,
973 CSIDL_Type_User,
974 My_VideosW,
975 MAKEINTRESOURCEW(IDS_MYVIDEOS)
977 { /* 0x0f - unassigned */
978 &GUID_NULL,
979 CSIDL_Type_Disallowed,
980 NULL,
981 NULL,
983 { /* 0x10 - CSIDL_DESKTOPDIRECTORY */
984 &FOLDERID_Desktop,
985 CSIDL_Type_User,
986 DesktopW,
987 MAKEINTRESOURCEW(IDS_DESKTOPDIRECTORY)
989 { /* 0x11 - CSIDL_DRIVES */
990 &FOLDERID_ComputerFolder,
991 CSIDL_Type_Disallowed,
992 NULL,
993 NULL,
995 { /* 0x12 - CSIDL_NETWORK */
996 &FOLDERID_NetworkFolder,
997 CSIDL_Type_Disallowed,
998 NULL,
999 NULL,
1001 { /* 0x13 - CSIDL_NETHOOD */
1002 &FOLDERID_NetHood,
1003 CSIDL_Type_User,
1004 NetHoodW,
1005 NetHoodW
1007 { /* 0x14 - CSIDL_FONTS */
1008 &FOLDERID_Fonts,
1009 CSIDL_Type_WindowsPath,
1010 FontsW,
1011 FontsW
1013 { /* 0x15 - CSIDL_TEMPLATES */
1014 &FOLDERID_Templates,
1015 CSIDL_Type_User,
1016 TemplatesW,
1017 TemplatesW
1019 { /* 0x16 - CSIDL_COMMON_STARTMENU */
1020 &FOLDERID_CommonStartMenu,
1021 CSIDL_Type_AllUsers,
1022 Common_Start_MenuW,
1023 Start_MenuW
1025 { /* 0x17 - CSIDL_COMMON_PROGRAMS */
1026 &FOLDERID_CommonPrograms,
1027 CSIDL_Type_AllUsers,
1028 Common_ProgramsW,
1029 Start_Menu_ProgramsW
1031 { /* 0x18 - CSIDL_COMMON_STARTUP */
1032 &FOLDERID_CommonStartup,
1033 CSIDL_Type_AllUsers,
1034 Common_StartUpW,
1035 Start_Menu_StartupW
1037 { /* 0x19 - CSIDL_COMMON_DESKTOPDIRECTORY */
1038 &FOLDERID_PublicDesktop,
1039 CSIDL_Type_AllUsers,
1040 Common_DesktopW,
1041 MAKEINTRESOURCEW(IDS_DESKTOPDIRECTORY)
1043 { /* 0x1a - CSIDL_APPDATA */
1044 &FOLDERID_RoamingAppData,
1045 CSIDL_Type_User,
1046 AppDataW,
1047 Application_DataW
1049 { /* 0x1b - CSIDL_PRINTHOOD */
1050 &FOLDERID_PrintHood,
1051 CSIDL_Type_User,
1052 PrintHoodW,
1053 PrintHoodW
1055 { /* 0x1c - CSIDL_LOCAL_APPDATA */
1056 &FOLDERID_LocalAppData,
1057 CSIDL_Type_User,
1058 Local_AppDataW,
1059 Local_Settings_Application_DataW
1061 { /* 0x1d - CSIDL_ALTSTARTUP */
1062 &GUID_NULL,
1063 CSIDL_Type_NonExistent,
1064 NULL,
1065 NULL
1067 { /* 0x1e - CSIDL_COMMON_ALTSTARTUP */
1068 &GUID_NULL,
1069 CSIDL_Type_NonExistent,
1070 NULL,
1071 NULL
1073 { /* 0x1f - CSIDL_COMMON_FAVORITES */
1074 &FOLDERID_Favorites,
1075 CSIDL_Type_AllUsers,
1076 Common_FavoritesW,
1077 FavoritesW
1079 { /* 0x20 - CSIDL_INTERNET_CACHE */
1080 &FOLDERID_InternetCache,
1081 CSIDL_Type_User,
1082 CacheW,
1083 Local_Settings_Temporary_Internet_FilesW
1085 { /* 0x21 - CSIDL_COOKIES */
1086 &FOLDERID_Cookies,
1087 CSIDL_Type_User,
1088 CookiesW,
1089 CookiesW
1091 { /* 0x22 - CSIDL_HISTORY */
1092 &FOLDERID_History,
1093 CSIDL_Type_User,
1094 HistoryW,
1095 Local_Settings_HistoryW
1097 { /* 0x23 - CSIDL_COMMON_APPDATA */
1098 &FOLDERID_ProgramData,
1099 CSIDL_Type_AllUsers,
1100 Common_AppDataW,
1101 Application_DataW
1103 { /* 0x24 - CSIDL_WINDOWS */
1104 &FOLDERID_Windows,
1105 CSIDL_Type_WindowsPath,
1106 NULL,
1107 NULL
1109 { /* 0x25 - CSIDL_SYSTEM */
1110 &FOLDERID_System,
1111 CSIDL_Type_SystemPath,
1112 NULL,
1113 NULL
1115 { /* 0x26 - CSIDL_PROGRAM_FILES */
1116 &FOLDERID_ProgramFiles,
1117 CSIDL_Type_CurrVer,
1118 ProgramFilesDirW,
1119 Program_FilesW
1121 { /* 0x27 - CSIDL_MYPICTURES */
1122 &FOLDERID_Pictures,
1123 CSIDL_Type_User,
1124 My_PicturesW,
1125 MAKEINTRESOURCEW(IDS_MYPICTURES)
1127 { /* 0x28 - CSIDL_PROFILE */
1128 &FOLDERID_Profile,
1129 CSIDL_Type_User,
1130 NULL,
1131 NULL
1133 { /* 0x29 - CSIDL_SYSTEMX86 */
1134 &FOLDERID_SystemX86,
1135 CSIDL_Type_SystemX86Path,
1136 NULL,
1137 NULL
1139 { /* 0x2a - CSIDL_PROGRAM_FILESX86 */
1140 &FOLDERID_ProgramFilesX86,
1141 CSIDL_Type_CurrVer,
1142 ProgramFilesDirX86W,
1143 Program_Files_x86W
1145 { /* 0x2b - CSIDL_PROGRAM_FILES_COMMON */
1146 &FOLDERID_ProgramFilesCommon,
1147 CSIDL_Type_CurrVer,
1148 CommonFilesDirW,
1149 Program_Files_Common_FilesW
1151 { /* 0x2c - CSIDL_PROGRAM_FILES_COMMONX86 */
1152 &FOLDERID_ProgramFilesCommonX86,
1153 CSIDL_Type_CurrVer,
1154 CommonFilesDirX86W,
1155 Program_Files_x86_Common_FilesW
1157 { /* 0x2d - CSIDL_COMMON_TEMPLATES */
1158 &FOLDERID_CommonTemplates,
1159 CSIDL_Type_AllUsers,
1160 Common_TemplatesW,
1161 TemplatesW
1163 { /* 0x2e - CSIDL_COMMON_DOCUMENTS */
1164 &FOLDERID_PublicDocuments,
1165 CSIDL_Type_AllUsers,
1166 Common_DocumentsW,
1167 DocumentsW
1169 { /* 0x2f - CSIDL_COMMON_ADMINTOOLS */
1170 &FOLDERID_CommonAdminTools,
1171 CSIDL_Type_AllUsers,
1172 Common_Administrative_ToolsW,
1173 Start_Menu_Admin_ToolsW
1175 { /* 0x30 - CSIDL_ADMINTOOLS */
1176 &FOLDERID_AdminTools,
1177 CSIDL_Type_User,
1178 Administrative_ToolsW,
1179 Start_Menu_Admin_ToolsW
1181 { /* 0x31 - CSIDL_CONNECTIONS */
1182 &FOLDERID_ConnectionsFolder,
1183 CSIDL_Type_Disallowed,
1184 NULL,
1185 NULL
1187 { /* 0x32 - unassigned */
1188 &GUID_NULL,
1189 CSIDL_Type_Disallowed,
1190 NULL,
1191 NULL
1193 { /* 0x33 - unassigned */
1194 &GUID_NULL,
1195 CSIDL_Type_Disallowed,
1196 NULL,
1197 NULL
1199 { /* 0x34 - unassigned */
1200 &GUID_NULL,
1201 CSIDL_Type_Disallowed,
1202 NULL,
1203 NULL
1205 { /* 0x35 - CSIDL_COMMON_MUSIC */
1206 &FOLDERID_PublicMusic,
1207 CSIDL_Type_AllUsers,
1208 CommonMusicW,
1209 MusicW
1211 { /* 0x36 - CSIDL_COMMON_PICTURES */
1212 &FOLDERID_PublicPictures,
1213 CSIDL_Type_AllUsers,
1214 CommonPicturesW,
1215 PicturesW
1217 { /* 0x37 - CSIDL_COMMON_VIDEO */
1218 &FOLDERID_PublicVideos,
1219 CSIDL_Type_AllUsers,
1220 CommonVideoW,
1221 VideosW
1223 { /* 0x38 - CSIDL_RESOURCES */
1224 &FOLDERID_ResourceDir,
1225 CSIDL_Type_WindowsPath,
1226 NULL,
1227 ResourcesW
1229 { /* 0x39 - CSIDL_RESOURCES_LOCALIZED */
1230 &FOLDERID_LocalizedResourcesDir,
1231 CSIDL_Type_NonExistent,
1232 NULL,
1233 NULL
1235 { /* 0x3a - CSIDL_COMMON_OEM_LINKS */
1236 &FOLDERID_CommonOEMLinks,
1237 CSIDL_Type_AllUsers,
1238 NULL,
1239 OEM_LinksW
1241 { /* 0x3b - CSIDL_CDBURN_AREA */
1242 &FOLDERID_CDBurning,
1243 CSIDL_Type_User,
1244 CD_BurningW,
1245 Local_Settings_CD_BurningW
1247 { /* 0x3c unassigned */
1248 &GUID_NULL,
1249 CSIDL_Type_Disallowed,
1250 NULL,
1251 NULL
1253 { /* 0x3d - CSIDL_COMPUTERSNEARME */
1254 &GUID_NULL,
1255 CSIDL_Type_Disallowed, /* FIXME */
1256 NULL,
1257 NULL
1259 { /* 0x3e - CSIDL_PROFILES */
1260 &GUID_NULL,
1261 CSIDL_Type_Disallowed, /* oddly, this matches WinXP */
1262 NULL,
1263 NULL
1265 { /* 0x3f */
1266 &FOLDERID_AddNewPrograms,
1267 CSIDL_Type_Disallowed,
1268 NULL,
1269 NULL
1271 { /* 0x40 */
1272 &FOLDERID_AppUpdates,
1273 CSIDL_Type_Disallowed,
1274 NULL,
1275 NULL
1277 { /* 0x41 */
1278 &FOLDERID_ChangeRemovePrograms,
1279 CSIDL_Type_Disallowed,
1280 NULL,
1281 NULL
1283 { /* 0x42 */
1284 &FOLDERID_ConflictFolder,
1285 CSIDL_Type_Disallowed,
1286 NULL,
1287 NULL
1289 { /* 0x43 - CSIDL_CONTACTS */
1290 &FOLDERID_Contacts,
1291 CSIDL_Type_User,
1292 NULL,
1293 ContactsW
1295 { /* 0x44 */
1296 &FOLDERID_DeviceMetadataStore,
1297 CSIDL_Type_Disallowed, /* FIXME */
1298 NULL,
1299 NULL
1301 { /* 0x45 */
1302 &GUID_NULL,
1303 CSIDL_Type_User,
1304 NULL,
1305 DocumentsW
1307 { /* 0x46 */
1308 &FOLDERID_DocumentsLibrary,
1309 CSIDL_Type_Disallowed, /* FIXME */
1310 NULL,
1311 NULL
1313 { /* 0x47 - CSIDL_DOWNLOADS */
1314 &FOLDERID_Downloads,
1315 CSIDL_Type_User,
1316 NULL,
1317 DownloadsW
1319 { /* 0x48 */
1320 &FOLDERID_Games,
1321 CSIDL_Type_Disallowed,
1322 NULL,
1323 NULL
1325 { /* 0x49 */
1326 &FOLDERID_GameTasks,
1327 CSIDL_Type_Disallowed, /* FIXME */
1328 NULL,
1329 NULL
1331 { /* 0x4a */
1332 &FOLDERID_HomeGroup,
1333 CSIDL_Type_Disallowed,
1334 NULL,
1335 NULL
1337 { /* 0x4b */
1338 &FOLDERID_ImplicitAppShortcuts,
1339 CSIDL_Type_Disallowed, /* FIXME */
1340 NULL,
1341 NULL
1343 { /* 0x4c */
1344 &FOLDERID_Libraries,
1345 CSIDL_Type_Disallowed, /* FIXME */
1346 NULL,
1347 NULL
1349 { /* 0x4d - CSIDL_LINKS */
1350 &FOLDERID_Links,
1351 CSIDL_Type_User,
1352 NULL,
1353 LinksW
1355 { /* 0x4e - CSIDL_APPDATA_LOCALLOW */
1356 &FOLDERID_LocalAppDataLow,
1357 CSIDL_Type_User,
1358 NULL,
1359 AppData_LocalLowW
1361 { /* 0x4f */
1362 &FOLDERID_MusicLibrary,
1363 CSIDL_Type_Disallowed, /* FIXME */
1364 NULL,
1365 NULL
1367 { /* 0x50 */
1368 &FOLDERID_OriginalImages,
1369 CSIDL_Type_Disallowed, /* FIXME */
1370 NULL,
1371 NULL
1373 { /* 0x51 */
1374 &FOLDERID_PhotoAlbums,
1375 CSIDL_Type_User,
1376 NULL,
1377 Pictures_Slide_ShowsW
1379 { /* 0x52 */
1380 &FOLDERID_PicturesLibrary,
1381 CSIDL_Type_Disallowed, /* FIXME */
1382 NULL,
1383 NULL
1385 { /* 0x53 */
1386 &FOLDERID_Playlists,
1387 CSIDL_Type_User,
1388 NULL,
1389 Music_PlaylistsW
1391 { /* 0x54 */
1392 &FOLDERID_ProgramFilesX64,
1393 CSIDL_Type_NonExistent,
1394 NULL,
1395 NULL
1397 { /* 0x55 */
1398 &FOLDERID_ProgramFilesCommonX64,
1399 CSIDL_Type_NonExistent,
1400 NULL,
1401 NULL
1403 { /* 0x56 */
1404 &FOLDERID_Public,
1405 CSIDL_Type_CurrVer, /* FIXME */
1406 NULL,
1407 UsersPublicW
1409 { /* 0x57 */
1410 &FOLDERID_PublicDownloads,
1411 CSIDL_Type_AllUsers,
1412 NULL,
1413 DownloadsW
1415 { /* 0x58 */
1416 &FOLDERID_PublicGameTasks,
1417 CSIDL_Type_AllUsers,
1418 NULL,
1419 Microsoft_Windows_GameExplorerW
1421 { /* 0x59 */
1422 &FOLDERID_PublicLibraries,
1423 CSIDL_Type_AllUsers,
1424 NULL,
1425 Microsoft_Windows_LibrariesW
1427 { /* 0x5a */
1428 &FOLDERID_PublicRingtones,
1429 CSIDL_Type_AllUsers,
1430 NULL,
1431 Microsoft_Windows_RingtonesW
1433 { /* 0x5b */
1434 &FOLDERID_QuickLaunch,
1435 CSIDL_Type_Disallowed, /* FIXME */
1436 NULL,
1437 NULL
1439 { /* 0x5c */
1440 &FOLDERID_RecordedTVLibrary,
1441 CSIDL_Type_Disallowed, /* FIXME */
1442 NULL,
1443 NULL
1445 { /* 0x5d */
1446 &FOLDERID_Ringtones,
1447 CSIDL_Type_Disallowed, /* FIXME */
1448 NULL,
1449 NULL
1451 { /* 0x5e */
1452 &FOLDERID_SampleMusic,
1453 CSIDL_Type_AllUsers,
1454 NULL,
1455 Music_Sample_MusicW
1457 { /* 0x5f */
1458 &FOLDERID_SamplePictures,
1459 CSIDL_Type_AllUsers,
1460 NULL,
1461 Pictures_Sample_PicturesW
1463 { /* 0x60 */
1464 &FOLDERID_SamplePlaylists,
1465 CSIDL_Type_AllUsers,
1466 NULL,
1467 Music_Sample_PlaylistsW
1469 { /* 0x61 */
1470 &FOLDERID_SampleVideos,
1471 CSIDL_Type_AllUsers,
1472 NULL,
1473 Videos_Sample_VideosW
1475 { /* 0x62 - CSIDL_SAVED_GAMES */
1476 &FOLDERID_SavedGames,
1477 CSIDL_Type_User,
1478 NULL,
1479 Saved_GamesW
1481 { /* 0x63 - CSIDL_SEARCHES */
1482 &FOLDERID_SavedSearches,
1483 CSIDL_Type_User,
1484 NULL,
1485 SearchesW
1487 { /* 0x64 */
1488 &FOLDERID_SEARCH_CSC,
1489 CSIDL_Type_Disallowed,
1490 NULL,
1491 NULL
1493 { /* 0x65 */
1494 &FOLDERID_SEARCH_MAPI,
1495 CSIDL_Type_Disallowed,
1496 NULL,
1497 NULL
1499 { /* 0x66 */
1500 &FOLDERID_SearchHome,
1501 CSIDL_Type_Disallowed,
1502 NULL,
1503 NULL
1505 { /* 0x67 */
1506 &FOLDERID_SidebarDefaultParts,
1507 CSIDL_Type_Disallowed, /* FIXME */
1508 NULL,
1509 NULL
1511 { /* 0x68 */
1512 &FOLDERID_SidebarParts,
1513 CSIDL_Type_Disallowed, /* FIXME */
1514 NULL,
1515 NULL
1517 { /* 0x69 */
1518 &FOLDERID_SyncManagerFolder,
1519 CSIDL_Type_Disallowed,
1520 NULL,
1521 NULL
1523 { /* 0x6a */
1524 &FOLDERID_SyncResultsFolder,
1525 CSIDL_Type_Disallowed,
1526 NULL,
1527 NULL
1529 { /* 0x6b */
1530 &FOLDERID_SyncSetupFolder,
1531 CSIDL_Type_Disallowed,
1532 NULL,
1533 NULL
1535 { /* 0x6c */
1536 &FOLDERID_UserPinned,
1537 CSIDL_Type_Disallowed, /* FIXME */
1538 NULL,
1539 NULL
1541 { /* 0x6d */
1542 &FOLDERID_UserProfiles,
1543 CSIDL_Type_CurrVer,
1544 UsersW,
1545 UsersW
1547 { /* 0x6e */
1548 &FOLDERID_UserProgramFiles,
1549 CSIDL_Type_Disallowed, /* FIXME */
1550 NULL,
1551 NULL
1553 { /* 0x6f */
1554 &FOLDERID_UserProgramFilesCommon,
1555 CSIDL_Type_Disallowed, /* FIXME */
1556 NULL,
1557 NULL
1559 { /* 0x70 */
1560 &FOLDERID_UsersFiles,
1561 CSIDL_Type_Disallowed,
1562 NULL,
1563 NULL
1565 { /* 0x71 */
1566 &FOLDERID_UsersLibraries,
1567 CSIDL_Type_Disallowed,
1568 NULL,
1569 NULL
1571 { /* 0x72 */
1572 &FOLDERID_VideosLibrary,
1573 CSIDL_Type_Disallowed, /* FIXME */
1574 NULL,
1575 NULL
1579 static HRESULT _SHExpandEnvironmentStrings(LPCWSTR szSrc, LPWSTR szDest);
1581 /* Gets the value named value from the registry key
1582 * rootKey\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
1583 * (or from rootKey\userPrefix\... if userPrefix is not NULL) into path, which
1584 * is assumed to be MAX_PATH WCHARs in length.
1585 * If it exists, expands the value and writes the expanded value to
1586 * rootKey\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders
1587 * Returns successful error code if the value was retrieved from the registry,
1588 * and a failure otherwise.
1590 static HRESULT _SHGetUserShellFolderPath(HKEY rootKey, LPCWSTR userPrefix,
1591 LPCWSTR value, LPWSTR path)
1593 HRESULT hr;
1594 WCHAR shellFolderPath[MAX_PATH], userShellFolderPath[MAX_PATH];
1595 LPCWSTR pShellFolderPath, pUserShellFolderPath;
1596 DWORD dwType, dwPathLen = MAX_PATH;
1597 HKEY userShellFolderKey, shellFolderKey;
1599 TRACE("%p,%s,%s,%p\n",rootKey, debugstr_w(userPrefix), debugstr_w(value),
1600 path);
1602 if (userPrefix)
1604 strcpyW(shellFolderPath, userPrefix);
1605 PathAddBackslashW(shellFolderPath);
1606 strcatW(shellFolderPath, szSHFolders);
1607 pShellFolderPath = shellFolderPath;
1608 strcpyW(userShellFolderPath, userPrefix);
1609 PathAddBackslashW(userShellFolderPath);
1610 strcatW(userShellFolderPath, szSHUserFolders);
1611 pUserShellFolderPath = userShellFolderPath;
1613 else
1615 pUserShellFolderPath = szSHUserFolders;
1616 pShellFolderPath = szSHFolders;
1619 if (RegCreateKeyW(rootKey, pShellFolderPath, &shellFolderKey))
1621 TRACE("Failed to create %s\n", debugstr_w(pShellFolderPath));
1622 return E_FAIL;
1624 if (RegCreateKeyW(rootKey, pUserShellFolderPath, &userShellFolderKey))
1626 TRACE("Failed to create %s\n",
1627 debugstr_w(pUserShellFolderPath));
1628 RegCloseKey(shellFolderKey);
1629 return E_FAIL;
1632 if (!RegQueryValueExW(userShellFolderKey, value, NULL, &dwType,
1633 (LPBYTE)path, &dwPathLen) && (dwType == REG_EXPAND_SZ || dwType == REG_SZ))
1635 LONG ret;
1637 path[dwPathLen / sizeof(WCHAR)] = '\0';
1638 if (dwType == REG_EXPAND_SZ && path[0] == '%')
1640 WCHAR szTemp[MAX_PATH];
1642 _SHExpandEnvironmentStrings(path, szTemp);
1643 lstrcpynW(path, szTemp, MAX_PATH);
1645 ret = RegSetValueExW(shellFolderKey, value, 0, REG_SZ, (LPBYTE)path,
1646 (strlenW(path) + 1) * sizeof(WCHAR));
1647 if (ret != ERROR_SUCCESS)
1648 hr = HRESULT_FROM_WIN32(ret);
1649 else
1650 hr = S_OK;
1652 else
1653 hr = E_FAIL;
1654 RegCloseKey(shellFolderKey);
1655 RegCloseKey(userShellFolderKey);
1656 TRACE("returning 0x%08x\n", hr);
1657 return hr;
1660 /* Gets a 'semi-expanded' default value of the CSIDL with index folder into
1661 * pszPath, based on the entries in CSIDL_Data. By semi-expanded, I mean:
1662 * - The entry's szDefaultPath may be either a string value or an integer
1663 * resource identifier. In the latter case, the string value of the resource
1664 * is written.
1665 * - Depending on the entry's type, the path may begin with an (unexpanded)
1666 * environment variable name. The caller is responsible for expanding
1667 * environment strings if so desired.
1668 * The types that are prepended with environment variables are:
1669 * CSIDL_Type_User: %USERPROFILE%
1670 * CSIDL_Type_AllUsers: %ALLUSERSPROFILE%
1671 * CSIDL_Type_CurrVer: %SystemDrive%
1672 * (Others might make sense too, but as yet are unneeded.)
1674 static HRESULT _SHGetDefaultValue(BYTE folder, LPWSTR pszPath)
1676 HRESULT hr;
1677 WCHAR resourcePath[MAX_PATH];
1678 LPCWSTR pDefaultPath = NULL;
1680 TRACE("0x%02x,%p\n", folder, pszPath);
1682 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1683 return E_INVALIDARG;
1684 if (!pszPath)
1685 return E_INVALIDARG;
1687 if (!is_win64)
1689 BOOL is_wow64;
1691 switch (folder)
1693 case CSIDL_PROGRAM_FILES:
1694 case CSIDL_PROGRAM_FILESX86:
1695 IsWow64Process( GetCurrentProcess(), &is_wow64 );
1696 folder = is_wow64 ? CSIDL_PROGRAM_FILESX86 : CSIDL_PROGRAM_FILES;
1697 break;
1698 case CSIDL_PROGRAM_FILES_COMMON:
1699 case CSIDL_PROGRAM_FILES_COMMONX86:
1700 IsWow64Process( GetCurrentProcess(), &is_wow64 );
1701 folder = is_wow64 ? CSIDL_PROGRAM_FILES_COMMONX86 : CSIDL_PROGRAM_FILES_COMMON;
1702 break;
1706 if (CSIDL_Data[folder].szDefaultPath &&
1707 IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath))
1709 if (LoadStringW(shell32_hInstance,
1710 LOWORD(CSIDL_Data[folder].szDefaultPath), resourcePath, MAX_PATH))
1712 hr = S_OK;
1713 pDefaultPath = resourcePath;
1715 else
1717 FIXME("(%d,%s), LoadString failed, missing translation?\n", folder,
1718 debugstr_w(pszPath));
1719 hr = E_FAIL;
1722 else
1724 hr = S_OK;
1725 pDefaultPath = CSIDL_Data[folder].szDefaultPath;
1727 if (SUCCEEDED(hr))
1729 switch (CSIDL_Data[folder].type)
1731 case CSIDL_Type_User:
1732 strcpyW(pszPath, UserProfileW);
1733 break;
1734 case CSIDL_Type_AllUsers:
1735 strcpyW(pszPath, AllUsersProfileW);
1736 break;
1737 case CSIDL_Type_CurrVer:
1738 strcpyW(pszPath, SystemDriveW);
1739 break;
1740 default:
1741 ; /* no corresponding env. var, do nothing */
1743 if (pDefaultPath)
1745 PathAddBackslashW(pszPath);
1746 strcatW(pszPath, pDefaultPath);
1749 TRACE("returning 0x%08x\n", hr);
1750 return hr;
1753 /* Gets the (unexpanded) value of the folder with index folder into pszPath.
1754 * The folder's type is assumed to be CSIDL_Type_CurrVer. Its default value
1755 * can be overridden in the HKLM\\szCurrentVersion key.
1756 * If dwFlags has SHGFP_TYPE_DEFAULT set or if the value isn't overridden in
1757 * the registry, uses _SHGetDefaultValue to get the value.
1759 static HRESULT _SHGetCurrentVersionPath(DWORD dwFlags, BYTE folder,
1760 LPWSTR pszPath)
1762 HRESULT hr;
1764 TRACE("0x%08x,0x%02x,%p\n", dwFlags, folder, pszPath);
1766 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1767 return E_INVALIDARG;
1768 if (CSIDL_Data[folder].type != CSIDL_Type_CurrVer)
1769 return E_INVALIDARG;
1770 if (!pszPath)
1771 return E_INVALIDARG;
1773 if (dwFlags & SHGFP_TYPE_DEFAULT)
1774 hr = _SHGetDefaultValue(folder, pszPath);
1775 else
1777 HKEY hKey;
1779 if (RegCreateKeyW(HKEY_LOCAL_MACHINE, szCurrentVersion, &hKey))
1780 hr = E_FAIL;
1781 else
1783 DWORD dwType, dwPathLen = MAX_PATH * sizeof(WCHAR);
1785 if (RegQueryValueExW(hKey, CSIDL_Data[folder].szValueName, NULL,
1786 &dwType, (LPBYTE)pszPath, &dwPathLen) ||
1787 (dwType != REG_SZ && dwType != REG_EXPAND_SZ))
1789 hr = _SHGetDefaultValue(folder, pszPath);
1790 dwType = REG_EXPAND_SZ;
1791 switch (folder)
1793 case CSIDL_PROGRAM_FILESX86:
1794 case CSIDL_PROGRAM_FILES_COMMONX86:
1795 /* these two should never be set on 32-bit setups */
1796 if (!is_win64)
1798 BOOL is_wow64;
1799 IsWow64Process( GetCurrentProcess(), &is_wow64 );
1800 if (!is_wow64) break;
1802 /* fall through */
1803 default:
1804 RegSetValueExW(hKey, CSIDL_Data[folder].szValueName, 0, dwType,
1805 (LPBYTE)pszPath, (strlenW(pszPath)+1)*sizeof(WCHAR));
1808 else
1810 pszPath[dwPathLen / sizeof(WCHAR)] = '\0';
1811 hr = S_OK;
1813 RegCloseKey(hKey);
1816 TRACE("returning 0x%08x (output path is %s)\n", hr, debugstr_w(pszPath));
1817 return hr;
1820 static LPWSTR _GetUserSidStringFromToken(HANDLE Token)
1822 char InfoBuffer[64];
1823 PTOKEN_USER UserInfo;
1824 DWORD InfoSize;
1825 LPWSTR SidStr;
1827 UserInfo = (PTOKEN_USER) InfoBuffer;
1828 if (! GetTokenInformation(Token, TokenUser, InfoBuffer, sizeof(InfoBuffer),
1829 &InfoSize))
1831 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
1832 return NULL;
1833 UserInfo = HeapAlloc(GetProcessHeap(), 0, InfoSize);
1834 if (UserInfo == NULL)
1835 return NULL;
1836 if (! GetTokenInformation(Token, TokenUser, UserInfo, InfoSize,
1837 &InfoSize))
1839 HeapFree(GetProcessHeap(), 0, UserInfo);
1840 return NULL;
1844 if (! ConvertSidToStringSidW(UserInfo->User.Sid, &SidStr))
1845 SidStr = NULL;
1847 if (UserInfo != (PTOKEN_USER) InfoBuffer)
1848 HeapFree(GetProcessHeap(), 0, UserInfo);
1850 return SidStr;
1853 /* Gets the user's path (unexpanded) for the CSIDL with index folder:
1854 * If SHGFP_TYPE_DEFAULT is set, calls _SHGetDefaultValue for it. Otherwise
1855 * calls _SHGetUserShellFolderPath for it. Where it looks depends on hToken:
1856 * - if hToken is -1, looks in HKEY_USERS\.Default
1857 * - otherwise looks first in HKEY_CURRENT_USER, followed by HKEY_LOCAL_MACHINE
1858 * if HKEY_CURRENT_USER doesn't contain any entries. If both fail, finally
1859 * calls _SHGetDefaultValue for it.
1861 static HRESULT _SHGetUserProfilePath(HANDLE hToken, DWORD dwFlags, BYTE folder,
1862 LPWSTR pszPath)
1864 const WCHAR *szValueName;
1865 WCHAR buffer[40];
1866 HRESULT hr;
1868 TRACE("%p,0x%08x,0x%02x,%p\n", hToken, dwFlags, folder, pszPath);
1870 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1871 return E_INVALIDARG;
1872 if (CSIDL_Data[folder].type != CSIDL_Type_User)
1873 return E_INVALIDARG;
1874 if (!pszPath)
1875 return E_INVALIDARG;
1877 if (dwFlags & SHGFP_TYPE_DEFAULT)
1879 if (hToken != NULL && hToken != (HANDLE)-1)
1881 FIXME("unsupported for user other than current or default\n");
1882 return E_FAIL;
1884 hr = _SHGetDefaultValue(folder, pszPath);
1886 else
1888 LPCWSTR userPrefix = NULL;
1889 HKEY hRootKey;
1891 if (hToken == (HANDLE)-1)
1893 hRootKey = HKEY_USERS;
1894 userPrefix = DefaultW;
1896 else if (hToken == NULL)
1897 hRootKey = HKEY_CURRENT_USER;
1898 else
1900 hRootKey = HKEY_USERS;
1901 userPrefix = _GetUserSidStringFromToken(hToken);
1902 if (userPrefix == NULL)
1904 hr = E_FAIL;
1905 goto error;
1909 /* For CSIDL_Type_User we also use the GUID if no szValueName is provided */
1910 szValueName = CSIDL_Data[folder].szValueName;
1911 if (!szValueName)
1913 StringFromGUID2( CSIDL_Data[folder].id, buffer, 39 );
1914 szValueName = &buffer[0];
1917 hr = _SHGetUserShellFolderPath(hRootKey, userPrefix, szValueName, pszPath);
1918 if (FAILED(hr) && hRootKey != HKEY_LOCAL_MACHINE)
1919 hr = _SHGetUserShellFolderPath(HKEY_LOCAL_MACHINE, NULL, szValueName, pszPath);
1920 if (FAILED(hr))
1921 hr = _SHGetDefaultValue(folder, pszPath);
1922 if (userPrefix != NULL && userPrefix != DefaultW)
1923 LocalFree((HLOCAL) userPrefix);
1925 error:
1926 TRACE("returning 0x%08x (output path is %s)\n", hr, debugstr_w(pszPath));
1927 return hr;
1930 /* Gets the (unexpanded) path for the CSIDL with index folder. If dwFlags has
1931 * SHGFP_TYPE_DEFAULT set, calls _SHGetDefaultValue. Otherwise calls
1932 * _SHGetUserShellFolderPath for it, looking only in HKEY_LOCAL_MACHINE.
1933 * If this fails, falls back to _SHGetDefaultValue.
1935 static HRESULT _SHGetAllUsersProfilePath(DWORD dwFlags, BYTE folder,
1936 LPWSTR pszPath)
1938 HRESULT hr;
1940 TRACE("0x%08x,0x%02x,%p\n", dwFlags, folder, pszPath);
1942 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1943 return E_INVALIDARG;
1944 if (CSIDL_Data[folder].type != CSIDL_Type_AllUsers)
1945 return E_INVALIDARG;
1946 if (!pszPath)
1947 return E_INVALIDARG;
1949 if (dwFlags & SHGFP_TYPE_DEFAULT)
1950 hr = _SHGetDefaultValue(folder, pszPath);
1951 else
1953 hr = _SHGetUserShellFolderPath(HKEY_LOCAL_MACHINE, NULL,
1954 CSIDL_Data[folder].szValueName, pszPath);
1955 if (FAILED(hr))
1956 hr = _SHGetDefaultValue(folder, pszPath);
1958 TRACE("returning 0x%08x (output path is %s)\n", hr, debugstr_w(pszPath));
1959 return hr;
1962 static HRESULT _SHOpenProfilesKey(PHKEY pKey)
1964 LONG lRet;
1965 DWORD disp;
1967 lRet = RegCreateKeyExW(HKEY_LOCAL_MACHINE, ProfileListW, 0, NULL, 0,
1968 KEY_ALL_ACCESS, NULL, pKey, &disp);
1969 return HRESULT_FROM_WIN32(lRet);
1972 /* Reads the value named szValueName from the key profilesKey (assumed to be
1973 * opened by _SHOpenProfilesKey) into szValue, which is assumed to be MAX_PATH
1974 * WCHARs in length. If it doesn't exist, returns szDefault (and saves
1975 * szDefault to the registry).
1977 static HRESULT _SHGetProfilesValue(HKEY profilesKey, LPCWSTR szValueName,
1978 LPWSTR szValue, LPCWSTR szDefault)
1980 HRESULT hr;
1981 DWORD type, dwPathLen = MAX_PATH * sizeof(WCHAR);
1982 LONG lRet;
1984 TRACE("%p,%s,%p,%s\n", profilesKey, debugstr_w(szValueName), szValue,
1985 debugstr_w(szDefault));
1986 lRet = RegQueryValueExW(profilesKey, szValueName, NULL, &type,
1987 (LPBYTE)szValue, &dwPathLen);
1988 if (!lRet && (type == REG_SZ || type == REG_EXPAND_SZ) && dwPathLen
1989 && *szValue)
1991 dwPathLen /= sizeof(WCHAR);
1992 szValue[dwPathLen] = '\0';
1993 hr = S_OK;
1995 else
1997 /* Missing or invalid value, set a default */
1998 lstrcpynW(szValue, szDefault, MAX_PATH);
1999 TRACE("Setting missing value %s to %s\n", debugstr_w(szValueName),
2000 debugstr_w(szValue));
2001 lRet = RegSetValueExW(profilesKey, szValueName, 0, REG_EXPAND_SZ,
2002 (LPBYTE)szValue,
2003 (strlenW(szValue) + 1) * sizeof(WCHAR));
2004 if (lRet)
2005 hr = HRESULT_FROM_WIN32(lRet);
2006 else
2007 hr = S_OK;
2009 TRACE("returning 0x%08x (output value is %s)\n", hr, debugstr_w(szValue));
2010 return hr;
2013 /* Attempts to expand environment variables from szSrc into szDest, which is
2014 * assumed to be MAX_PATH characters in length. Before referring to the
2015 * environment, handles a few variables directly, because the environment
2016 * variables may not be set when this is called (as during Wine's installation
2017 * when default values are being written to the registry).
2018 * The directly handled environment variables, and their source, are:
2019 * - ALLUSERSPROFILE, USERPROFILE: reads from the registry
2020 * - SystemDrive: uses GetSystemDirectoryW and uses the drive portion of its
2021 * path
2022 * If one of the directly handled environment variables is expanded, only
2023 * expands a single variable, and only in the beginning of szSrc.
2025 static HRESULT _SHExpandEnvironmentStrings(LPCWSTR szSrc, LPWSTR szDest)
2027 HRESULT hr;
2028 WCHAR szTemp[MAX_PATH], szProfilesPrefix[MAX_PATH] = { 0 };
2029 HKEY key = NULL;
2031 TRACE("%s, %p\n", debugstr_w(szSrc), szDest);
2033 if (!szSrc || !szDest) return E_INVALIDARG;
2035 /* short-circuit if there's nothing to expand */
2036 if (szSrc[0] != '%')
2038 strcpyW(szDest, szSrc);
2039 hr = S_OK;
2040 goto end;
2042 /* Get the profile prefix, we'll probably be needing it */
2043 hr = _SHOpenProfilesKey(&key);
2044 if (SUCCEEDED(hr))
2046 WCHAR def_val[MAX_PATH];
2048 /* get the system drive */
2049 GetSystemDirectoryW(def_val, MAX_PATH);
2050 if (def_val[1] == ':') strcpyW( def_val + 3, szDefaultProfileDirW );
2051 else FIXME("non-drive system paths unsupported\n");
2053 hr = _SHGetProfilesValue(key, ProfilesDirectoryW, szProfilesPrefix, def_val );
2056 *szDest = 0;
2057 strcpyW(szTemp, szSrc);
2058 while (SUCCEEDED(hr) && szTemp[0] == '%')
2060 if (!strncmpiW(szTemp, AllUsersProfileW, strlenW(AllUsersProfileW)))
2062 WCHAR szAllUsers[MAX_PATH];
2064 strcpyW(szDest, szProfilesPrefix);
2065 hr = _SHGetProfilesValue(key, AllUsersProfileValueW,
2066 szAllUsers, AllUsersW);
2067 PathAppendW(szDest, szAllUsers);
2068 PathAppendW(szDest, szTemp + strlenW(AllUsersProfileW));
2070 else if (!strncmpiW(szTemp, UserProfileW, strlenW(UserProfileW)))
2072 WCHAR userName[MAX_PATH];
2073 DWORD userLen = MAX_PATH;
2075 strcpyW(szDest, szProfilesPrefix);
2076 GetUserNameW(userName, &userLen);
2077 PathAppendW(szDest, userName);
2078 PathAppendW(szDest, szTemp + strlenW(UserProfileW));
2080 else if (!strncmpiW(szTemp, SystemDriveW, strlenW(SystemDriveW)))
2082 GetSystemDirectoryW(szDest, MAX_PATH);
2083 if (szDest[1] != ':')
2085 FIXME("non-drive system paths unsupported\n");
2086 hr = E_FAIL;
2088 else
2090 strcpyW(szDest + 3, szTemp + strlenW(SystemDriveW) + 1);
2091 hr = S_OK;
2094 else
2096 DWORD ret = ExpandEnvironmentStringsW(szSrc, szDest, MAX_PATH);
2098 if (ret > MAX_PATH)
2099 hr = E_NOT_SUFFICIENT_BUFFER;
2100 else if (ret == 0)
2101 hr = HRESULT_FROM_WIN32(GetLastError());
2102 else
2103 hr = S_OK;
2105 if (SUCCEEDED(hr) && szDest[0] == '%')
2106 strcpyW(szTemp, szDest);
2107 else
2109 /* terminate loop */
2110 szTemp[0] = '\0';
2113 end:
2114 if (key)
2115 RegCloseKey(key);
2116 TRACE("returning 0x%08x (input was %s, output is %s)\n", hr,
2117 debugstr_w(szSrc), debugstr_w(szDest));
2118 return hr;
2121 /*************************************************************************
2122 * SHGetFolderPathW [SHELL32.@]
2124 * Convert nFolder to path.
2126 * RETURNS
2127 * Success: S_OK
2128 * Failure: standard HRESULT error codes.
2130 * NOTES
2131 * Most values can be overridden in either
2132 * HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
2133 * or in the same location in HKLM.
2134 * The "Shell Folders" registry key was used in NT4 and earlier systems.
2135 * Beginning with Windows 2000, the "User Shell Folders" key is used, so
2136 * changes made to it are made to the former key too. This synchronization is
2137 * done on-demand: not until someone requests the value of one of these paths
2138 * (by calling one of the SHGet functions) is the value synchronized.
2139 * Furthermore, the HKCU paths take precedence over the HKLM paths.
2141 HRESULT WINAPI SHGetFolderPathW(
2142 HWND hwndOwner, /* [I] owner window */
2143 int nFolder, /* [I] CSIDL identifying the folder */
2144 HANDLE hToken, /* [I] access token */
2145 DWORD dwFlags, /* [I] which path to return */
2146 LPWSTR pszPath) /* [O] converted path */
2148 HRESULT hr = SHGetFolderPathAndSubDirW(hwndOwner, nFolder, hToken, dwFlags, NULL, pszPath);
2149 if(HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND) == hr)
2150 hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
2151 return hr;
2154 HRESULT WINAPI SHGetFolderPathAndSubDirA(
2155 HWND hwndOwner, /* [I] owner window */
2156 int nFolder, /* [I] CSIDL identifying the folder */
2157 HANDLE hToken, /* [I] access token */
2158 DWORD dwFlags, /* [I] which path to return */
2159 LPCSTR pszSubPath, /* [I] sub directory of the specified folder */
2160 LPSTR pszPath) /* [O] converted path */
2162 int length;
2163 HRESULT hr = S_OK;
2164 LPWSTR pszSubPathW = NULL;
2165 LPWSTR pszPathW = NULL;
2166 TRACE("%08x,%08x,%s\n",nFolder, dwFlags, debugstr_w(pszSubPathW));
2168 if(pszPath) {
2169 pszPathW = HeapAlloc(GetProcessHeap(), 0, MAX_PATH * sizeof(WCHAR));
2170 if(!pszPathW) {
2171 hr = HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
2172 goto cleanup;
2175 TRACE("%08x,%08x,%s\n",nFolder, dwFlags, debugstr_w(pszSubPathW));
2177 /* SHGetFolderPathAndSubDirW does not distinguish if pszSubPath isn't
2178 * set (null), or an empty string.therefore call it without the parameter set
2179 * if pszSubPath is an empty string
2181 if (pszSubPath && pszSubPath[0]) {
2182 length = MultiByteToWideChar(CP_ACP, 0, pszSubPath, -1, NULL, 0);
2183 pszSubPathW = HeapAlloc(GetProcessHeap(), 0, length * sizeof(WCHAR));
2184 if(!pszSubPathW) {
2185 hr = HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
2186 goto cleanup;
2188 MultiByteToWideChar(CP_ACP, 0, pszSubPath, -1, pszSubPathW, length);
2191 hr = SHGetFolderPathAndSubDirW(hwndOwner, nFolder, hToken, dwFlags, pszSubPathW, pszPathW);
2193 if (SUCCEEDED(hr) && pszPath)
2194 WideCharToMultiByte(CP_ACP, 0, pszPathW, -1, pszPath, MAX_PATH, NULL, NULL);
2196 cleanup:
2197 HeapFree(GetProcessHeap(), 0, pszPathW);
2198 HeapFree(GetProcessHeap(), 0, pszSubPathW);
2199 return hr;
2202 /*************************************************************************
2203 * SHGetFolderPathAndSubDirW [SHELL32.@]
2205 HRESULT WINAPI SHGetFolderPathAndSubDirW(
2206 HWND hwndOwner, /* [I] owner window */
2207 int nFolder, /* [I] CSIDL identifying the folder */
2208 HANDLE hToken, /* [I] access token */
2209 DWORD dwFlags, /* [I] which path to return */
2210 LPCWSTR pszSubPath,/* [I] sub directory of the specified folder */
2211 LPWSTR pszPath) /* [O] converted path */
2213 HRESULT hr;
2214 WCHAR szBuildPath[MAX_PATH], szTemp[MAX_PATH];
2215 DWORD folder = nFolder & CSIDL_FOLDER_MASK;
2216 CSIDL_Type type;
2217 int ret;
2219 TRACE("%p,%p,nFolder=0x%04x,%s\n", hwndOwner,pszPath,nFolder,debugstr_w(pszSubPath));
2221 /* Windows always NULL-terminates the resulting path regardless of success
2222 * or failure, so do so first
2224 if (pszPath)
2225 *pszPath = '\0';
2227 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
2228 return E_INVALIDARG;
2229 if ((SHGFP_TYPE_CURRENT != dwFlags) && (SHGFP_TYPE_DEFAULT != dwFlags))
2230 return E_INVALIDARG;
2231 szTemp[0] = 0;
2232 type = CSIDL_Data[folder].type;
2233 switch (type)
2235 case CSIDL_Type_Disallowed:
2236 hr = E_INVALIDARG;
2237 break;
2238 case CSIDL_Type_NonExistent:
2239 hr = S_FALSE;
2240 break;
2241 case CSIDL_Type_WindowsPath:
2242 GetWindowsDirectoryW(szTemp, MAX_PATH);
2243 if (CSIDL_Data[folder].szDefaultPath &&
2244 !IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath) &&
2245 *CSIDL_Data[folder].szDefaultPath)
2247 PathAddBackslashW(szTemp);
2248 strcatW(szTemp, CSIDL_Data[folder].szDefaultPath);
2250 hr = S_OK;
2251 break;
2252 case CSIDL_Type_SystemPath:
2253 GetSystemDirectoryW(szTemp, MAX_PATH);
2254 if (CSIDL_Data[folder].szDefaultPath &&
2255 !IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath) &&
2256 *CSIDL_Data[folder].szDefaultPath)
2258 PathAddBackslashW(szTemp);
2259 strcatW(szTemp, CSIDL_Data[folder].szDefaultPath);
2261 hr = S_OK;
2262 break;
2263 case CSIDL_Type_SystemX86Path:
2264 if (!GetSystemWow64DirectoryW(szTemp, MAX_PATH)) GetSystemDirectoryW(szTemp, MAX_PATH);
2265 if (CSIDL_Data[folder].szDefaultPath &&
2266 !IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath) &&
2267 *CSIDL_Data[folder].szDefaultPath)
2269 PathAddBackslashW(szTemp);
2270 strcatW(szTemp, CSIDL_Data[folder].szDefaultPath);
2272 hr = S_OK;
2273 break;
2274 case CSIDL_Type_CurrVer:
2275 hr = _SHGetCurrentVersionPath(dwFlags, folder, szTemp);
2276 break;
2277 case CSIDL_Type_User:
2278 hr = _SHGetUserProfilePath(hToken, dwFlags, folder, szTemp);
2279 break;
2280 case CSIDL_Type_AllUsers:
2281 hr = _SHGetAllUsersProfilePath(dwFlags, folder, szTemp);
2282 break;
2283 default:
2284 FIXME("bogus type %d, please fix\n", type);
2285 hr = E_INVALIDARG;
2286 break;
2289 /* Expand environment strings if necessary */
2290 if (*szTemp == '%')
2291 hr = _SHExpandEnvironmentStrings(szTemp, szBuildPath);
2292 else
2293 strcpyW(szBuildPath, szTemp);
2295 if (FAILED(hr)) goto end;
2297 if(pszSubPath) {
2298 /* make sure the new path does not exceed the buffer length
2299 * and remember to backslash and terminate it */
2300 if(MAX_PATH < (lstrlenW(szBuildPath) + lstrlenW(pszSubPath) + 2)) {
2301 hr = HRESULT_FROM_WIN32(ERROR_FILENAME_EXCED_RANGE);
2302 goto end;
2304 PathAppendW(szBuildPath, pszSubPath);
2305 PathRemoveBackslashW(szBuildPath);
2307 /* Copy the path if it's available before we might return */
2308 if (SUCCEEDED(hr) && pszPath)
2309 strcpyW(pszPath, szBuildPath);
2311 /* if we don't care about existing directories we are ready */
2312 if(nFolder & CSIDL_FLAG_DONT_VERIFY) goto end;
2314 if (PathFileExistsW(szBuildPath)) goto end;
2316 /* not existing but we are not allowed to create it. The return value
2317 * is verified against shell32 version 6.0.
2319 if (!(nFolder & CSIDL_FLAG_CREATE))
2321 hr = HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND);
2322 goto end;
2325 /* create directory/directories */
2326 ret = SHCreateDirectoryExW(hwndOwner, szBuildPath, NULL);
2327 if (ret && ret != ERROR_ALREADY_EXISTS)
2329 ERR("Failed to create directory %s.\n", debugstr_w(szBuildPath));
2330 hr = E_FAIL;
2331 goto end;
2334 TRACE("Created missing system directory %s\n", debugstr_w(szBuildPath));
2335 end:
2336 TRACE("returning 0x%08x (final path is %s)\n", hr, debugstr_w(szBuildPath));
2337 return hr;
2340 /*************************************************************************
2341 * SHGetFolderPathA [SHELL32.@]
2343 * See SHGetFolderPathW.
2345 HRESULT WINAPI SHGetFolderPathA(
2346 HWND hwndOwner,
2347 int nFolder,
2348 HANDLE hToken,
2349 DWORD dwFlags,
2350 LPSTR pszPath)
2352 WCHAR szTemp[MAX_PATH];
2353 HRESULT hr;
2355 TRACE("%p,%p,nFolder=0x%04x\n",hwndOwner,pszPath,nFolder);
2357 if (pszPath)
2358 *pszPath = '\0';
2359 hr = SHGetFolderPathW(hwndOwner, nFolder, hToken, dwFlags, szTemp);
2360 if (SUCCEEDED(hr) && pszPath)
2361 WideCharToMultiByte(CP_ACP, 0, szTemp, -1, pszPath, MAX_PATH, NULL,
2362 NULL);
2364 return hr;
2367 /* For each folder in folders, if its value has not been set in the registry,
2368 * calls _SHGetUserProfilePath or _SHGetAllUsersProfilePath (depending on the
2369 * folder's type) to get the unexpanded value first.
2370 * Writes the unexpanded value to User Shell Folders, and queries it with
2371 * SHGetFolderPathW to force the creation of the directory if it doesn't
2372 * already exist. SHGetFolderPathW also returns the expanded value, which
2373 * this then writes to Shell Folders.
2375 static HRESULT _SHRegisterFolders(HKEY hRootKey, HANDLE hToken,
2376 LPCWSTR szUserShellFolderPath, LPCWSTR szShellFolderPath, const UINT folders[],
2377 UINT foldersLen)
2379 const WCHAR *szValueName;
2380 WCHAR buffer[40];
2381 UINT i;
2382 WCHAR path[MAX_PATH];
2383 HRESULT hr = S_OK;
2384 HKEY hUserKey = NULL, hKey = NULL;
2385 DWORD dwType, dwPathLen;
2386 LONG ret;
2388 TRACE("%p,%p,%s,%p,%u\n", hRootKey, hToken,
2389 debugstr_w(szUserShellFolderPath), folders, foldersLen);
2391 ret = RegCreateKeyW(hRootKey, szUserShellFolderPath, &hUserKey);
2392 if (ret)
2393 hr = HRESULT_FROM_WIN32(ret);
2394 else
2396 ret = RegCreateKeyW(hRootKey, szShellFolderPath, &hKey);
2397 if (ret)
2398 hr = HRESULT_FROM_WIN32(ret);
2400 for (i = 0; SUCCEEDED(hr) && i < foldersLen; i++)
2402 dwPathLen = MAX_PATH * sizeof(WCHAR);
2404 /* For CSIDL_Type_User we also use the GUID if no szValueName is provided */
2405 szValueName = CSIDL_Data[folders[i]].szValueName;
2406 if (!szValueName && CSIDL_Data[folders[i]].type == CSIDL_Type_User)
2408 StringFromGUID2( CSIDL_Data[folders[i]].id, buffer, 39 );
2409 szValueName = &buffer[0];
2412 if (RegQueryValueExW(hUserKey, szValueName, NULL,
2413 &dwType, (LPBYTE)path, &dwPathLen) || (dwType != REG_SZ &&
2414 dwType != REG_EXPAND_SZ))
2416 *path = '\0';
2417 if (CSIDL_Data[folders[i]].type == CSIDL_Type_User)
2418 _SHGetUserProfilePath(hToken, SHGFP_TYPE_DEFAULT, folders[i],
2419 path);
2420 else if (CSIDL_Data[folders[i]].type == CSIDL_Type_AllUsers)
2421 _SHGetAllUsersProfilePath(SHGFP_TYPE_DEFAULT, folders[i], path);
2422 else if (CSIDL_Data[folders[i]].type == CSIDL_Type_WindowsPath)
2424 GetWindowsDirectoryW(path, MAX_PATH);
2425 if (CSIDL_Data[folders[i]].szDefaultPath &&
2426 !IS_INTRESOURCE(CSIDL_Data[folders[i]].szDefaultPath))
2428 PathAddBackslashW(path);
2429 strcatW(path, CSIDL_Data[folders[i]].szDefaultPath);
2432 else
2433 hr = E_FAIL;
2434 if (*path)
2436 ret = RegSetValueExW(hUserKey, szValueName, 0, REG_EXPAND_SZ,
2437 (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR));
2438 if (ret)
2439 hr = HRESULT_FROM_WIN32(ret);
2440 else
2442 hr = SHGetFolderPathW(NULL, folders[i] | CSIDL_FLAG_CREATE,
2443 hToken, SHGFP_TYPE_DEFAULT, path);
2444 ret = RegSetValueExW(hKey, szValueName, 0, REG_SZ,
2445 (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR));
2446 if (ret)
2447 hr = HRESULT_FROM_WIN32(ret);
2452 if (hUserKey)
2453 RegCloseKey(hUserKey);
2454 if (hKey)
2455 RegCloseKey(hKey);
2457 TRACE("returning 0x%08x\n", hr);
2458 return hr;
2461 static HRESULT _SHRegisterUserShellFolders(BOOL bDefault)
2463 static const UINT folders[] = {
2464 CSIDL_PROGRAMS,
2465 CSIDL_PERSONAL,
2466 CSIDL_FAVORITES,
2467 CSIDL_APPDATA,
2468 CSIDL_STARTUP,
2469 CSIDL_RECENT,
2470 CSIDL_SENDTO,
2471 CSIDL_STARTMENU,
2472 CSIDL_MYMUSIC,
2473 CSIDL_MYVIDEO,
2474 CSIDL_DESKTOPDIRECTORY,
2475 CSIDL_NETHOOD,
2476 CSIDL_TEMPLATES,
2477 CSIDL_PRINTHOOD,
2478 CSIDL_LOCAL_APPDATA,
2479 CSIDL_INTERNET_CACHE,
2480 CSIDL_COOKIES,
2481 CSIDL_HISTORY,
2482 CSIDL_MYPICTURES,
2483 CSIDL_FONTS,
2484 CSIDL_ADMINTOOLS,
2485 CSIDL_CONTACTS,
2486 CSIDL_DOWNLOADS,
2487 CSIDL_LINKS,
2488 CSIDL_APPDATA_LOCALLOW,
2489 CSIDL_SAVED_GAMES,
2490 CSIDL_SEARCHES
2492 WCHAR userShellFolderPath[MAX_PATH], shellFolderPath[MAX_PATH];
2493 LPCWSTR pUserShellFolderPath, pShellFolderPath;
2494 HRESULT hr = S_OK;
2495 HKEY hRootKey;
2496 HANDLE hToken;
2498 TRACE("%s\n", bDefault ? "TRUE" : "FALSE");
2499 if (bDefault)
2501 hToken = (HANDLE)-1;
2502 hRootKey = HKEY_USERS;
2503 strcpyW(userShellFolderPath, DefaultW);
2504 PathAddBackslashW(userShellFolderPath);
2505 strcatW(userShellFolderPath, szSHUserFolders);
2506 pUserShellFolderPath = userShellFolderPath;
2507 strcpyW(shellFolderPath, DefaultW);
2508 PathAddBackslashW(shellFolderPath);
2509 strcatW(shellFolderPath, szSHFolders);
2510 pShellFolderPath = shellFolderPath;
2512 else
2514 hToken = NULL;
2515 hRootKey = HKEY_CURRENT_USER;
2516 pUserShellFolderPath = szSHUserFolders;
2517 pShellFolderPath = szSHFolders;
2520 hr = _SHRegisterFolders(hRootKey, hToken, pUserShellFolderPath,
2521 pShellFolderPath, folders, sizeof(folders) / sizeof(folders[0]));
2522 TRACE("returning 0x%08x\n", hr);
2523 return hr;
2526 static HRESULT _SHRegisterCommonShellFolders(void)
2528 static const UINT folders[] = {
2529 CSIDL_COMMON_STARTMENU,
2530 CSIDL_COMMON_PROGRAMS,
2531 CSIDL_COMMON_STARTUP,
2532 CSIDL_COMMON_DESKTOPDIRECTORY,
2533 CSIDL_COMMON_FAVORITES,
2534 CSIDL_COMMON_APPDATA,
2535 CSIDL_COMMON_TEMPLATES,
2536 CSIDL_COMMON_DOCUMENTS,
2537 CSIDL_COMMON_ADMINTOOLS,
2538 CSIDL_COMMON_MUSIC,
2539 CSIDL_COMMON_PICTURES,
2540 CSIDL_COMMON_VIDEO,
2542 HRESULT hr;
2544 TRACE("\n");
2545 hr = _SHRegisterFolders(HKEY_LOCAL_MACHINE, NULL, szSHUserFolders,
2546 szSHFolders, folders, sizeof(folders) / sizeof(folders[0]));
2547 TRACE("returning 0x%08x\n", hr);
2548 return hr;
2551 /******************************************************************************
2552 * _SHAppendToUnixPath [Internal]
2554 * Helper function for _SHCreateSymbolicLinks. Appends pwszSubPath (or the
2555 * corresponding resource, if IS_INTRESOURCE) to the unix base path 'szBasePath'
2556 * and replaces backslashes with slashes.
2558 * PARAMS
2559 * szBasePath [IO] The unix base path, which will be appended to (CP_UNXICP).
2560 * pwszSubPath [I] Sub-path or resource id (use MAKEINTRESOURCEW).
2562 * RETURNS
2563 * Success: TRUE,
2564 * Failure: FALSE
2566 static inline BOOL _SHAppendToUnixPath(char *szBasePath, LPCWSTR pwszSubPath) {
2567 WCHAR wszSubPath[MAX_PATH];
2568 int cLen = strlen(szBasePath);
2569 char *pBackslash;
2571 if (IS_INTRESOURCE(pwszSubPath)) {
2572 if (!LoadStringW(shell32_hInstance, LOWORD(pwszSubPath), wszSubPath, MAX_PATH)) {
2573 /* Fall back to hard coded defaults. */
2574 switch (LOWORD(pwszSubPath)) {
2575 case IDS_PERSONAL:
2576 lstrcpyW(wszSubPath, PersonalW);
2577 break;
2578 case IDS_MYMUSIC:
2579 lstrcpyW(wszSubPath, My_MusicW);
2580 break;
2581 case IDS_MYPICTURES:
2582 lstrcpyW(wszSubPath, My_PicturesW);
2583 break;
2584 case IDS_MYVIDEOS:
2585 lstrcpyW(wszSubPath, My_VideosW);
2586 break;
2587 default:
2588 ERR("LoadString(%d) failed!\n", LOWORD(pwszSubPath));
2589 return FALSE;
2592 } else {
2593 lstrcpyW(wszSubPath, pwszSubPath);
2596 if (szBasePath[cLen-1] != '/') szBasePath[cLen++] = '/';
2598 if (!WideCharToMultiByte(CP_UNIXCP, 0, wszSubPath, -1, szBasePath + cLen,
2599 FILENAME_MAX - cLen, NULL, NULL))
2601 return FALSE;
2604 pBackslash = szBasePath + cLen;
2605 while ((pBackslash = strchr(pBackslash, '\\'))) *pBackslash = '/';
2607 return TRUE;
2610 /******************************************************************************
2611 * _SHCreateSymbolicLinks [Internal]
2613 * Sets up symbol links for various shell folders to point into the users home
2614 * directory. We do an educated guess about what the user would probably want:
2615 * - If there is a 'My Documents' directory in $HOME, the user probably wants
2616 * wine's 'My Documents' to point there. Furthermore, we imply that the user
2617 * is a Windows lover and has no problem with wine creating 'My Pictures',
2618 * 'My Music' and 'My Videos' subfolders under '$HOME/My Documents', if those
2619 * do not already exits. We put appropriate symbolic links in place for those,
2620 * too.
2621 * - If there is no 'My Documents' directory in $HOME, we let 'My Documents'
2622 * point directly to $HOME. We assume the user to be a unix hacker who does not
2623 * want wine to create anything anywhere besides the .wine directory. So, if
2624 * there already is a 'My Music' directory in $HOME, we symlink the 'My Music'
2625 * shell folder to it. But if not, then we check XDG_MUSIC_DIR - "well known"
2626 * directory, and try to link to that. If that fails, then we symlink to
2627 * $HOME directly. The same holds fo 'My Pictures' and 'My Videos'.
2628 * - The Desktop shell folder is symlinked to XDG_DESKTOP_DIR. If that does not
2629 * exist, then we try '$HOME/Desktop'. If that does not exist, then we leave
2630 * it alone.
2631 * ('My Music',... above in fact means LoadString(IDS_MYMUSIC))
2633 static void _SHCreateSymbolicLinks(void)
2635 UINT aidsMyStuff[] = { IDS_MYPICTURES, IDS_MYVIDEOS, IDS_MYMUSIC }, i;
2636 int acsidlMyStuff[] = { CSIDL_MYPICTURES, CSIDL_MYVIDEO, CSIDL_MYMUSIC };
2637 static const char * const xdg_dirs[] = { "PICTURES", "VIDEOS", "MUSIC", "DESKTOP" };
2638 static const unsigned int num = sizeof(xdg_dirs) / sizeof(xdg_dirs[0]);
2639 WCHAR wszTempPath[MAX_PATH];
2640 char szPersonalTarget[FILENAME_MAX], *pszPersonal;
2641 char szMyStuffTarget[FILENAME_MAX], *pszMyStuff;
2642 char szDesktopTarget[FILENAME_MAX], *pszDesktop;
2643 struct stat statFolder;
2644 const char *pszHome;
2645 HRESULT hr;
2646 char ** xdg_results;
2647 char * xdg_desktop_dir;
2649 /* Create all necessary profile sub-dirs up to 'My Documents' and get the unix path. */
2650 hr = SHGetFolderPathW(NULL, CSIDL_PERSONAL|CSIDL_FLAG_CREATE, NULL,
2651 SHGFP_TYPE_DEFAULT, wszTempPath);
2652 if (FAILED(hr)) return;
2653 pszPersonal = wine_get_unix_file_name(wszTempPath);
2654 if (!pszPersonal) return;
2656 hr = XDG_UserDirLookup(xdg_dirs, num, &xdg_results);
2657 if (FAILED(hr)) xdg_results = NULL;
2659 pszHome = getenv("HOME");
2660 if (pszHome && !stat(pszHome, &statFolder) && S_ISDIR(statFolder.st_mode)) {
2661 strcpy(szPersonalTarget, pszHome);
2662 if (_SHAppendToUnixPath(szPersonalTarget, MAKEINTRESOURCEW(IDS_PERSONAL)) &&
2663 !stat(szPersonalTarget, &statFolder) && S_ISDIR(statFolder.st_mode))
2665 /* '$HOME/My Documents' exists. Create 'My Pictures', 'My Videos' and
2666 * 'My Music' subfolders or fail silently if they already exist. */
2667 for (i = 0; i < sizeof(aidsMyStuff)/sizeof(aidsMyStuff[0]); i++) {
2668 strcpy(szMyStuffTarget, szPersonalTarget);
2669 if (_SHAppendToUnixPath(szMyStuffTarget, MAKEINTRESOURCEW(aidsMyStuff[i])))
2670 mkdir(szMyStuffTarget, 0777);
2673 else
2675 /* '$HOME/My Documents' doesn't exists, but '$HOME' does. */
2676 strcpy(szPersonalTarget, pszHome);
2679 /* Replace 'My Documents' directory with a symlink of fail silently if not empty. */
2680 rmdir(pszPersonal);
2681 symlink(szPersonalTarget, pszPersonal);
2683 else
2685 /* '$HOME' doesn't exist. Create 'My Pictures', 'My Videos' and 'My Music' subdirs
2686 * in '%USERPROFILE%\\My Documents' or fail silently if they already exist. */
2687 strcpy(szPersonalTarget, pszPersonal);
2688 for (i = 0; i < sizeof(aidsMyStuff)/sizeof(aidsMyStuff[0]); i++) {
2689 strcpy(szMyStuffTarget, szPersonalTarget);
2690 if (_SHAppendToUnixPath(szMyStuffTarget, MAKEINTRESOURCEW(aidsMyStuff[i])))
2691 mkdir(szMyStuffTarget, 0777);
2695 /* Create symbolic links for 'My Pictures', 'My Videos' and 'My Music'. */
2696 for (i=0; i < sizeof(aidsMyStuff)/sizeof(aidsMyStuff[0]); i++) {
2697 /* Create the current 'My Whatever' folder and get its unix path. */
2698 hr = SHGetFolderPathW(NULL, acsidlMyStuff[i]|CSIDL_FLAG_CREATE, NULL,
2699 SHGFP_TYPE_DEFAULT, wszTempPath);
2700 if (FAILED(hr)) continue;
2701 pszMyStuff = wine_get_unix_file_name(wszTempPath);
2702 if (!pszMyStuff) continue;
2704 strcpy(szMyStuffTarget, szPersonalTarget);
2705 if (_SHAppendToUnixPath(szMyStuffTarget, MAKEINTRESOURCEW(aidsMyStuff[i])) &&
2706 !stat(szMyStuffTarget, &statFolder) && S_ISDIR(statFolder.st_mode))
2708 /* If there's a 'My Whatever' directory where 'My Documents' links to, link to it. */
2709 rmdir(pszMyStuff);
2710 symlink(szMyStuffTarget, pszMyStuff);
2712 else
2714 rmdir(pszMyStuff);
2715 if (xdg_results && xdg_results[i])
2717 /* the folder specified by XDG_XXX_DIR exists, link to it. */
2718 symlink(xdg_results[i], pszMyStuff);
2720 else
2722 /* Else link to where 'My Documents' itself links to. */
2723 symlink(szPersonalTarget, pszMyStuff);
2726 HeapFree(GetProcessHeap(), 0, pszMyStuff);
2729 /* Last but not least, the Desktop folder */
2730 if (pszHome)
2731 strcpy(szDesktopTarget, pszHome);
2732 else
2733 strcpy(szDesktopTarget, pszPersonal);
2734 HeapFree(GetProcessHeap(), 0, pszPersonal);
2736 xdg_desktop_dir = xdg_results ? xdg_results[num - 1] : NULL;
2737 if (xdg_desktop_dir ||
2738 (_SHAppendToUnixPath(szDesktopTarget, DesktopW) &&
2739 !stat(szDesktopTarget, &statFolder) && S_ISDIR(statFolder.st_mode)))
2741 hr = SHGetFolderPathW(NULL, CSIDL_DESKTOPDIRECTORY|CSIDL_FLAG_CREATE, NULL,
2742 SHGFP_TYPE_DEFAULT, wszTempPath);
2743 if (SUCCEEDED(hr) && (pszDesktop = wine_get_unix_file_name(wszTempPath)))
2745 rmdir(pszDesktop);
2746 if (xdg_desktop_dir)
2747 symlink(xdg_desktop_dir, pszDesktop);
2748 else
2749 symlink(szDesktopTarget, pszDesktop);
2750 HeapFree(GetProcessHeap(), 0, pszDesktop);
2754 /* Free resources allocated by XDG_UserDirLookup() */
2755 if (xdg_results)
2757 for (i = 0; i < num; i++)
2758 HeapFree(GetProcessHeap(), 0, xdg_results[i]);
2759 HeapFree(GetProcessHeap(), 0, xdg_results);
2763 /******************************************************************************
2764 * create_extra_folders [Internal]
2766 * Create some extra folders that don't have a standard CSIDL definition.
2768 static HRESULT create_extra_folders(void)
2770 static const WCHAR environW[] = {'E','n','v','i','r','o','n','m','e','n','t',0};
2771 static const WCHAR microsoftW[] = {'M','i','c','r','o','s','o','f','t',0};
2772 static const WCHAR TempW[] = {'T','e','m','p',0};
2773 static const WCHAR TEMPW[] = {'T','E','M','P',0};
2774 static const WCHAR TMPW[] = {'T','M','P',0};
2775 WCHAR path[MAX_PATH+5];
2776 HRESULT hr;
2777 HKEY hkey;
2778 DWORD type, size, ret;
2780 ret = RegCreateKeyW( HKEY_CURRENT_USER, environW, &hkey );
2781 if (ret) return HRESULT_FROM_WIN32( ret );
2783 /* FIXME: should be under AppData, but we don't want spaces in the temp path */
2784 hr = SHGetFolderPathAndSubDirW( 0, CSIDL_PROFILE | CSIDL_FLAG_CREATE, NULL,
2785 SHGFP_TYPE_DEFAULT, TempW, path );
2786 if (SUCCEEDED(hr))
2788 size = sizeof(path);
2789 if (RegQueryValueExW( hkey, TEMPW, NULL, &type, (LPBYTE)path, &size ))
2790 RegSetValueExW( hkey, TEMPW, 0, REG_SZ, (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR) );
2791 size = sizeof(path);
2792 if (RegQueryValueExW( hkey, TMPW, NULL, &type, (LPBYTE)path, &size ))
2793 RegSetValueExW( hkey, TMPW, 0, REG_SZ, (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR) );
2795 RegCloseKey( hkey );
2797 if (SUCCEEDED(hr))
2799 hr = SHGetFolderPathAndSubDirW( 0, CSIDL_COMMON_APPDATA | CSIDL_FLAG_CREATE, NULL,
2800 SHGFP_TYPE_DEFAULT, microsoftW, path );
2802 return hr;
2806 /******************************************************************************
2807 * set_folder_attributes
2809 * Set the various folder attributes registry keys.
2811 static HRESULT set_folder_attributes(void)
2813 static const WCHAR clsidW[] = {'C','L','S','I','D','\\',0 };
2814 static const WCHAR shellfolderW[] = {'\\','S','h','e','l','l','F','o','l','d','e','r', 0 };
2815 static const WCHAR wfparsingW[] = {'W','a','n','t','s','F','O','R','P','A','R','S','I','N','G',0};
2816 static const WCHAR wfdisplayW[] = {'W','a','n','t','s','F','O','R','D','I','S','P','L','A','Y',0};
2817 static const WCHAR hideasdeleteW[] = {'H','i','d','e','A','s','D','e','l','e','t','e','P','e','r','U','s','e','r',0};
2818 static const WCHAR attributesW[] = {'A','t','t','r','i','b','u','t','e','s',0};
2819 static const WCHAR cfattributesW[] = {'C','a','l','l','F','o','r','A','t','t','r','i','b','u','t','e','s',0};
2820 static const WCHAR emptyW[] = {0};
2822 static const struct
2824 const CLSID *clsid;
2825 BOOL wfparsing : 1;
2826 BOOL wfdisplay : 1;
2827 BOOL hideasdel : 1;
2828 DWORD attr;
2829 DWORD call_for_attr;
2830 } folders[] =
2832 { &CLSID_UnixFolder, TRUE, FALSE, FALSE },
2833 { &CLSID_UnixDosFolder, TRUE, FALSE, FALSE,
2834 SFGAO_FILESYSANCESTOR|SFGAO_FOLDER|SFGAO_HASSUBFOLDER, SFGAO_FILESYSTEM },
2835 { &CLSID_FolderShortcut, FALSE, FALSE, FALSE,
2836 SFGAO_FILESYSTEM|SFGAO_FOLDER|SFGAO_LINK,
2837 SFGAO_HASSUBFOLDER|SFGAO_FILESYSTEM|SFGAO_FOLDER|SFGAO_FILESYSANCESTOR },
2838 { &CLSID_MyDocuments, TRUE, FALSE, FALSE,
2839 SFGAO_FILESYSANCESTOR|SFGAO_FOLDER|SFGAO_HASSUBFOLDER, SFGAO_FILESYSTEM },
2840 { &CLSID_RecycleBin, FALSE, FALSE, FALSE,
2841 SFGAO_FOLDER|SFGAO_DROPTARGET|SFGAO_HASPROPSHEET },
2842 { &CLSID_ControlPanel, FALSE, TRUE, TRUE,
2843 SFGAO_FOLDER|SFGAO_HASSUBFOLDER }
2846 unsigned int i;
2847 WCHAR buffer[39 + (sizeof(clsidW) + sizeof(shellfolderW)) / sizeof(WCHAR)];
2848 LONG res;
2849 HKEY hkey;
2851 for (i = 0; i < sizeof(folders)/sizeof(folders[0]); i++)
2853 strcpyW( buffer, clsidW );
2854 StringFromGUID2( folders[i].clsid, buffer + strlenW(buffer), 39 );
2855 strcatW( buffer, shellfolderW );
2856 res = RegCreateKeyExW( HKEY_CLASSES_ROOT, buffer, 0, NULL, 0,
2857 KEY_READ | KEY_WRITE, NULL, &hkey, NULL);
2858 if (res) return HRESULT_FROM_WIN32( res );
2859 if (folders[i].wfparsing)
2860 res = RegSetValueExW( hkey, wfparsingW, 0, REG_SZ, (const BYTE *)emptyW, sizeof(emptyW) );
2861 if (folders[i].wfdisplay)
2862 res = RegSetValueExW( hkey, wfdisplayW, 0, REG_SZ, (const BYTE *)emptyW, sizeof(emptyW) );
2863 if (folders[i].hideasdel)
2864 res = RegSetValueExW( hkey, hideasdeleteW, 0, REG_SZ, (const BYTE *)emptyW, sizeof(emptyW) );
2865 if (folders[i].attr)
2866 res = RegSetValueExW( hkey, attributesW, 0, REG_DWORD,
2867 (const BYTE *)&folders[i].attr, sizeof(DWORD));
2868 if (folders[i].call_for_attr)
2869 res = RegSetValueExW( hkey, cfattributesW, 0, REG_DWORD,
2870 (const BYTE *)&folders[i].call_for_attr, sizeof(DWORD));
2871 RegCloseKey( hkey );
2873 return S_OK;
2877 /* Register the default values in the registry, as some apps seem to depend
2878 * on their presence. The set registered was taken from Windows XP.
2880 HRESULT SHELL_RegisterShellFolders(void)
2882 HRESULT hr;
2884 /* Set up '$HOME' targeted symlinks for 'My Documents', 'My Pictures',
2885 * 'My Videos', 'My Music' and 'Desktop' in advance, so that the
2886 * _SHRegister*ShellFolders() functions will find everything nice and clean
2887 * and thus will not attempt to create them in the profile directory. */
2888 _SHCreateSymbolicLinks();
2890 hr = _SHRegisterUserShellFolders(TRUE);
2891 if (SUCCEEDED(hr))
2892 hr = _SHRegisterUserShellFolders(FALSE);
2893 if (SUCCEEDED(hr))
2894 hr = _SHRegisterCommonShellFolders();
2895 if (SUCCEEDED(hr))
2896 hr = create_extra_folders();
2897 if (SUCCEEDED(hr))
2898 hr = set_folder_attributes();
2899 return hr;
2902 /*************************************************************************
2903 * SHGetSpecialFolderPathA [SHELL32.@]
2905 BOOL WINAPI SHGetSpecialFolderPathA (
2906 HWND hwndOwner,
2907 LPSTR szPath,
2908 int nFolder,
2909 BOOL bCreate)
2911 return SHGetFolderPathA(hwndOwner, nFolder + (bCreate ? CSIDL_FLAG_CREATE : 0), NULL, 0,
2912 szPath) == S_OK;
2915 /*************************************************************************
2916 * SHGetSpecialFolderPathW
2918 BOOL WINAPI SHGetSpecialFolderPathW (
2919 HWND hwndOwner,
2920 LPWSTR szPath,
2921 int nFolder,
2922 BOOL bCreate)
2924 return SHGetFolderPathW(hwndOwner, nFolder + (bCreate ? CSIDL_FLAG_CREATE : 0), NULL, 0,
2925 szPath) == S_OK;
2928 /*************************************************************************
2929 * SHGetSpecialFolderPath (SHELL32.175)
2931 BOOL WINAPI SHGetSpecialFolderPathAW (
2932 HWND hwndOwner,
2933 LPVOID szPath,
2934 int nFolder,
2935 BOOL bCreate)
2938 if (SHELL_OsIsUnicode())
2939 return SHGetSpecialFolderPathW (hwndOwner, szPath, nFolder, bCreate);
2940 return SHGetSpecialFolderPathA (hwndOwner, szPath, nFolder, bCreate);
2943 /*************************************************************************
2944 * SHGetFolderLocation [SHELL32.@]
2946 * Gets the folder locations from the registry and creates a pidl.
2948 * PARAMS
2949 * hwndOwner [I]
2950 * nFolder [I] CSIDL_xxxxx
2951 * hToken [I] token representing user, or NULL for current user, or -1 for
2952 * default user
2953 * dwReserved [I] must be zero
2954 * ppidl [O] PIDL of a special folder
2956 * RETURNS
2957 * Success: S_OK
2958 * Failure: Standard OLE-defined error result, S_FALSE or E_INVALIDARG
2960 * NOTES
2961 * Creates missing reg keys and directories.
2962 * Mostly forwards to SHGetFolderPathW, but a few values of nFolder return
2963 * virtual folders that are handled here.
2965 HRESULT WINAPI SHGetFolderLocation(
2966 HWND hwndOwner,
2967 int nFolder,
2968 HANDLE hToken,
2969 DWORD dwReserved,
2970 LPITEMIDLIST *ppidl)
2972 HRESULT hr = E_INVALIDARG;
2974 TRACE("%p 0x%08x %p 0x%08x %p\n",
2975 hwndOwner, nFolder, hToken, dwReserved, ppidl);
2977 if (!ppidl)
2978 return E_INVALIDARG;
2979 if (dwReserved)
2980 return E_INVALIDARG;
2982 /* The virtual folders' locations are not user-dependent */
2983 *ppidl = NULL;
2984 switch (nFolder & CSIDL_FOLDER_MASK)
2986 case CSIDL_DESKTOP:
2987 *ppidl = _ILCreateDesktop();
2988 break;
2990 case CSIDL_PERSONAL:
2991 *ppidl = _ILCreateMyDocuments();
2992 break;
2994 case CSIDL_INTERNET:
2995 *ppidl = _ILCreateIExplore();
2996 break;
2998 case CSIDL_CONTROLS:
2999 *ppidl = _ILCreateControlPanel();
3000 break;
3002 case CSIDL_PRINTERS:
3003 *ppidl = _ILCreatePrinters();
3004 break;
3006 case CSIDL_BITBUCKET:
3007 *ppidl = _ILCreateBitBucket();
3008 break;
3010 case CSIDL_DRIVES:
3011 *ppidl = _ILCreateMyComputer();
3012 break;
3014 case CSIDL_NETWORK:
3015 *ppidl = _ILCreateNetwork();
3016 break;
3018 default:
3020 WCHAR szPath[MAX_PATH];
3022 hr = SHGetFolderPathW(hwndOwner, nFolder, hToken,
3023 SHGFP_TYPE_CURRENT, szPath);
3024 if (SUCCEEDED(hr))
3026 DWORD attributes=0;
3028 TRACE("Value=%s\n", debugstr_w(szPath));
3029 hr = SHILCreateFromPathW(szPath, ppidl, &attributes);
3031 else if (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
3033 /* unlike SHGetFolderPath, SHGetFolderLocation in shell32
3034 * version 6.0 returns E_FAIL for nonexistent paths
3036 hr = E_FAIL;
3040 if(*ppidl)
3041 hr = S_OK;
3043 TRACE("-- (new pidl %p)\n",*ppidl);
3044 return hr;
3047 /*************************************************************************
3048 * SHGetSpecialFolderLocation [SHELL32.@]
3050 * NOTES
3051 * In NT5, SHGetSpecialFolderLocation needs the <winntdir>/Recent
3052 * directory.
3054 HRESULT WINAPI SHGetSpecialFolderLocation(
3055 HWND hwndOwner,
3056 INT nFolder,
3057 LPITEMIDLIST * ppidl)
3059 HRESULT hr = E_INVALIDARG;
3061 TRACE("(%p,0x%x,%p)\n", hwndOwner,nFolder,ppidl);
3063 if (!ppidl)
3064 return E_INVALIDARG;
3066 hr = SHGetFolderLocation(hwndOwner, nFolder, NULL, 0, ppidl);
3067 return hr;
3070 static int csidl_from_id( const KNOWNFOLDERID *id )
3072 int i;
3073 for (i = 0; i < sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]); i++)
3074 if (IsEqualGUID( CSIDL_Data[i].id, id )) return i;
3075 return -1;
3078 /*************************************************************************
3079 * SHGetKnownFolderPath [SHELL32.@]
3081 HRESULT WINAPI SHGetKnownFolderPath(REFKNOWNFOLDERID rfid, DWORD flags, HANDLE token, PWSTR *path)
3083 HRESULT hr;
3084 WCHAR folder[MAX_PATH];
3085 int index = csidl_from_id( rfid );
3087 TRACE("%s, 0x%08x, %p, %p\n", debugstr_guid(rfid), flags, token, path);
3089 *path = NULL;
3091 if (index < 0)
3092 return HRESULT_FROM_WIN32( ERROR_FILE_NOT_FOUND );
3094 if (flags & KF_FLAG_CREATE)
3095 index |= CSIDL_FLAG_CREATE;
3097 if (flags & KF_FLAG_DONT_VERIFY)
3098 index |= CSIDL_FLAG_DONT_VERIFY;
3100 if (flags & KF_FLAG_NO_ALIAS)
3101 index |= CSIDL_FLAG_NO_ALIAS;
3103 if (flags & KF_FLAG_INIT)
3104 index |= CSIDL_FLAG_PER_USER_INIT;
3106 if (flags & ~(KF_FLAG_CREATE|KF_FLAG_DONT_VERIFY|KF_FLAG_NO_ALIAS|KF_FLAG_INIT))
3108 FIXME("flags 0x%08x not supported\n", flags);
3109 return E_INVALIDARG;
3112 hr = SHGetFolderPathW( NULL, index, token, 0, folder );
3113 if (SUCCEEDED(hr))
3115 *path = CoTaskMemAlloc( (strlenW( folder ) + 1) * sizeof(WCHAR) );
3116 if (!*path)
3117 return E_OUTOFMEMORY;
3118 strcpyW( *path, folder );
3120 return hr;
3123 /*************************************************************************
3124 * SHGetFolderPathEx [SHELL32.@]
3126 HRESULT WINAPI SHGetFolderPathEx(REFKNOWNFOLDERID rfid, DWORD flags, HANDLE token, LPWSTR path, DWORD len)
3128 HRESULT hr;
3129 WCHAR *buffer;
3131 TRACE("%s, 0x%08x, %p, %p, %u\n", debugstr_guid(rfid), flags, token, path, len);
3133 if (!path || !len) return E_INVALIDARG;
3135 hr = SHGetKnownFolderPath( rfid, flags, token, &buffer );
3136 if (SUCCEEDED( hr ))
3138 if (strlenW( buffer ) + 1 > len)
3140 CoTaskMemFree( buffer );
3141 return HRESULT_FROM_WIN32( ERROR_INSUFFICIENT_BUFFER );
3143 strcpyW( path, buffer );
3144 CoTaskMemFree( buffer );
3146 return hr;
3149 /* constant values used by known folder functions */
3150 static const WCHAR szCategory[] = {'C','a','t','e','g','o','r','y',0};
3151 static const WCHAR szName[] = {'N','a','m','e',0};
3152 static const WCHAR szRelativePath[] = {'R','e','l','a','t','i','v','e','P','a','t','h',0};
3153 static const WCHAR szParentFolder[] = {'P','a','r','e','n','t','F','o','l','d','e','r',0};
3156 * Internal function to convert known folder identifier to path of registry key
3157 * associated with known folder.
3159 * Parameters:
3160 * rfid [I] pointer to known folder identifier (may be NULL)
3161 * lpStringGuid [I] string with known folder identifier (used when rfid is NULL)
3162 * lpPath [O] place to store string address. String should be
3163 * later freed using HeapFree(GetProcessHeap(),0, ... )
3165 static HRESULT get_known_folder_registry_path(
3166 REFKNOWNFOLDERID rfid,
3167 LPWSTR lpStringGuid,
3168 LPWSTR *lpPath)
3170 static const WCHAR sBackslash[] = {'\\',0};
3171 HRESULT hr = S_OK;
3172 int length;
3173 WCHAR sGuid[50];
3175 TRACE("(%s, %s, %p)\n", debugstr_guid(rfid), debugstr_w(lpStringGuid), lpPath);
3177 if(rfid)
3178 StringFromGUID2(rfid, sGuid, sizeof(sGuid)/sizeof(sGuid[0]));
3179 else
3180 lstrcpyW(sGuid, lpStringGuid);
3182 length = lstrlenW(szKnownFolderDescriptions)+51;
3183 *lpPath = HeapAlloc(GetProcessHeap(), 0, length*sizeof(WCHAR));
3184 if(!(*lpPath))
3185 hr = E_OUTOFMEMORY;
3187 if(SUCCEEDED(hr))
3189 lstrcpyW(*lpPath, szKnownFolderDescriptions);
3190 lstrcatW(*lpPath, sBackslash);
3191 lstrcatW(*lpPath, sGuid);
3194 return hr;
3198 * Internal function to get place where folder redirection information are stored.
3200 * Parameters:
3201 * rfid [I] pointer to known folder identifier (may be NULL)
3202 * rootKey [O] root key where the redirection information are stored
3203 * It can be HKLM for COMMON folders, and HKCU for PERUSER folders.
3204 * However, besides root key, path is always that same, and is stored
3205 * as "szKnownFolderRedirections" constant
3207 static HRESULT get_known_folder_redirection_place(
3208 REFKNOWNFOLDERID rfid,
3209 HKEY *rootKey)
3211 HRESULT hr;
3212 LPWSTR lpRegistryPath = NULL;
3213 KF_CATEGORY category;
3214 DWORD dwSize;
3216 /* first, get known folder's category */
3217 hr = get_known_folder_registry_path(rfid, NULL, &lpRegistryPath);
3219 if(SUCCEEDED(hr))
3221 dwSize = sizeof(category);
3222 hr = HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE, lpRegistryPath, szCategory, RRF_RT_DWORD, NULL, &category, &dwSize));
3225 if(SUCCEEDED(hr))
3227 if(category == KF_CATEGORY_COMMON)
3229 *rootKey = HKEY_LOCAL_MACHINE;
3230 hr = S_OK;
3232 else if(category == KF_CATEGORY_PERUSER)
3234 *rootKey = HKEY_CURRENT_USER;
3235 hr = S_OK;
3237 else
3238 hr = E_FAIL;
3241 HeapFree(GetProcessHeap(), 0, lpRegistryPath);
3242 return hr;
3245 static HRESULT get_known_folder_path_by_id(REFKNOWNFOLDERID folderId, LPWSTR lpRegistryPath, DWORD dwFlags, LPWSTR *ppszPath);
3247 static HRESULT get_known_folder_category(
3248 LPWSTR registryPath,
3249 KF_CATEGORY* pCategory)
3251 DWORD dwSize = sizeof(DWORD);
3252 DWORD dwType;
3253 return HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE, registryPath, szCategory, RRF_RT_DWORD, &dwType, pCategory, &dwSize));
3256 static HRESULT redirect_known_folder(
3257 REFKNOWNFOLDERID rfid,
3258 HWND hwnd,
3259 KF_REDIRECT_FLAGS flags,
3260 LPCWSTR pszTargetPath,
3261 UINT cFolders,
3262 KNOWNFOLDERID const *pExclusion,
3263 LPWSTR *ppszError)
3265 HRESULT hr;
3266 HKEY rootKey = HKEY_LOCAL_MACHINE, hKey;
3267 WCHAR sGuid[39];
3268 LPWSTR lpRegistryPath = NULL, lpSrcPath = NULL;
3269 TRACE("(%s, %p, 0x%08x, %s, %d, %p, %p)\n", debugstr_guid(rfid), hwnd, flags, debugstr_w(pszTargetPath), cFolders, pExclusion, ppszError);
3271 if (ppszError) *ppszError = NULL;
3273 hr = get_known_folder_registry_path(rfid, NULL, &lpRegistryPath);
3275 if(SUCCEEDED(hr))
3276 hr = get_known_folder_path_by_id(rfid, lpRegistryPath, 0, &lpSrcPath);
3278 HeapFree(GetProcessHeap(), 0, lpRegistryPath);
3280 /* get path to redirection storage */
3281 if(SUCCEEDED(hr))
3282 hr = get_known_folder_redirection_place(rfid, &rootKey);
3284 /* write redirection information */
3285 if(SUCCEEDED(hr))
3286 hr = HRESULT_FROM_WIN32(RegCreateKeyExW(rootKey, szKnownFolderRedirections, 0, NULL, 0, KEY_WRITE, NULL, &hKey, NULL));
3288 if(SUCCEEDED(hr))
3290 StringFromGUID2(rfid, sGuid, sizeof(sGuid)/sizeof(sGuid[0]));
3292 hr = HRESULT_FROM_WIN32(RegSetValueExW(hKey, sGuid, 0, REG_SZ, (LPBYTE)pszTargetPath, (lstrlenW(pszTargetPath)+1)*sizeof(WCHAR)));
3294 RegCloseKey(hKey);
3297 /* make sure destination path exists */
3298 SHCreateDirectory(NULL, pszTargetPath);
3300 /* copy content if required */
3301 if(SUCCEEDED(hr) && (flags & KF_REDIRECT_COPY_CONTENTS) )
3303 static const WCHAR sWildcard[] = {'\\','*',0};
3304 WCHAR srcPath[MAX_PATH+1], dstPath[MAX_PATH+1];
3305 SHFILEOPSTRUCTW fileOp;
3307 ZeroMemory(srcPath, sizeof(srcPath));
3308 lstrcpyW(srcPath, lpSrcPath);
3309 lstrcatW(srcPath, sWildcard);
3311 ZeroMemory(dstPath, sizeof(dstPath));
3312 lstrcpyW(dstPath, pszTargetPath);
3314 ZeroMemory(&fileOp, sizeof(fileOp));
3316 if(flags & KF_REDIRECT_DEL_SOURCE_CONTENTS)
3317 fileOp.wFunc = FO_MOVE;
3318 else
3319 fileOp.wFunc = FO_COPY;
3321 fileOp.pFrom = srcPath;
3322 fileOp.pTo = dstPath;
3323 fileOp.fFlags = FOF_NO_UI;
3325 hr = (SHFileOperationW(&fileOp)==0 ? S_OK : E_FAIL);
3327 if(flags & KF_REDIRECT_DEL_SOURCE_CONTENTS)
3329 ZeroMemory(srcPath, sizeof(srcPath));
3330 lstrcpyW(srcPath, lpSrcPath);
3332 ZeroMemory(&fileOp, sizeof(fileOp));
3333 fileOp.wFunc = FO_DELETE;
3334 fileOp.pFrom = srcPath;
3335 fileOp.fFlags = FOF_NO_UI;
3337 hr = (SHFileOperationW(&fileOp)==0 ? S_OK : E_FAIL);
3341 CoTaskMemFree(lpSrcPath);
3343 return hr;
3347 struct knownfolder
3349 IKnownFolder IKnownFolder_iface;
3350 LONG refs;
3351 KNOWNFOLDERID id;
3352 LPWSTR registryPath;
3355 static inline struct knownfolder *impl_from_IKnownFolder( IKnownFolder *iface )
3357 return CONTAINING_RECORD( iface, struct knownfolder, IKnownFolder_iface );
3360 static ULONG WINAPI knownfolder_AddRef(
3361 IKnownFolder *iface )
3363 struct knownfolder *knownfolder = impl_from_IKnownFolder( iface );
3364 return InterlockedIncrement( &knownfolder->refs );
3367 static ULONG WINAPI knownfolder_Release(
3368 IKnownFolder *iface )
3370 struct knownfolder *knownfolder = impl_from_IKnownFolder( iface );
3371 LONG refs = InterlockedDecrement( &knownfolder->refs );
3372 if (!refs)
3374 TRACE("destroying %p\n", knownfolder);
3375 HeapFree( GetProcessHeap(), 0, knownfolder->registryPath);
3376 HeapFree( GetProcessHeap(), 0, knownfolder );
3378 return refs;
3381 static HRESULT WINAPI knownfolder_QueryInterface(
3382 IKnownFolder *iface,
3383 REFIID riid,
3384 void **ppv )
3386 struct knownfolder *This = impl_from_IKnownFolder( iface );
3388 TRACE("%p %s %p\n", This, debugstr_guid( riid ), ppv );
3390 if ( IsEqualGUID( riid, &IID_IKnownFolder ) ||
3391 IsEqualGUID( riid, &IID_IUnknown ) )
3393 *ppv = iface;
3395 else
3397 FIXME("interface %s not implemented\n", debugstr_guid(riid));
3398 return E_NOINTERFACE;
3400 IKnownFolder_AddRef( iface );
3401 return S_OK;
3404 static HRESULT knownfolder_set_id(
3405 struct knownfolder *knownfolder,
3406 const KNOWNFOLDERID *kfid)
3408 HKEY hKey;
3409 HRESULT hr;
3411 TRACE("%s\n", debugstr_guid(kfid));
3413 knownfolder->id = *kfid;
3415 /* check is it registry-registered folder */
3416 hr = get_known_folder_registry_path(kfid, NULL, &knownfolder->registryPath);
3417 if(SUCCEEDED(hr))
3418 hr = HRESULT_FROM_WIN32(RegOpenKeyExW(HKEY_LOCAL_MACHINE, knownfolder->registryPath, 0, 0, &hKey));
3420 if(SUCCEEDED(hr))
3422 hr = S_OK;
3423 RegCloseKey(hKey);
3425 else
3427 /* This known folder is not registered. To mark it, we set registryPath to NULL */
3428 HeapFree(GetProcessHeap(), 0, knownfolder->registryPath);
3429 knownfolder->registryPath = NULL;
3430 hr = S_OK;
3433 return hr;
3436 static HRESULT WINAPI knownfolder_GetId(
3437 IKnownFolder *iface,
3438 KNOWNFOLDERID *pkfid)
3440 struct knownfolder *knownfolder = impl_from_IKnownFolder( iface );
3442 TRACE("%p\n", pkfid);
3444 *pkfid = knownfolder->id;
3445 return S_OK;
3448 static HRESULT WINAPI knownfolder_GetCategory(
3449 IKnownFolder *iface,
3450 KF_CATEGORY *pCategory)
3452 struct knownfolder *knownfolder = impl_from_IKnownFolder(iface);
3453 HRESULT hr = S_OK;
3455 TRACE("%p, %p\n", knownfolder, pCategory);
3457 /* we cannot get a category for a folder which is not registered */
3458 if(!knownfolder->registryPath)
3459 hr = E_FAIL;
3461 if(SUCCEEDED(hr))
3462 hr = get_known_folder_category(knownfolder->registryPath, pCategory);
3464 return hr;
3467 static HRESULT WINAPI knownfolder_GetShellItem(
3468 IKnownFolder *iface,
3469 DWORD dwFlags,
3470 REFIID riid,
3471 void **ppv)
3473 FIXME("0x%08x, %s, %p\n", dwFlags, debugstr_guid(riid), ppv);
3474 return E_NOTIMPL;
3477 static HRESULT get_known_folder_path(
3478 LPWSTR sFolderId,
3479 LPWSTR registryPath,
3480 LPWSTR *ppszPath)
3482 static const WCHAR sBackslash[] = {'\\',0};
3483 HRESULT hr;
3484 DWORD dwSize, dwType;
3485 WCHAR path[MAX_PATH] = {0};
3486 WCHAR parentGuid[39];
3487 KF_CATEGORY category;
3488 LPWSTR parentRegistryPath, parentPath;
3489 HKEY hRedirectionRootKey = NULL;
3491 TRACE("(%s, %p)\n", debugstr_w(registryPath), ppszPath);
3492 *ppszPath = NULL;
3494 /* check if folder has parent */
3495 dwSize = sizeof(parentGuid);
3496 hr = HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE, registryPath, szParentFolder, RRF_RT_REG_SZ, &dwType, parentGuid, &dwSize));
3497 if(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) hr = S_FALSE;
3499 if(hr == S_OK)
3501 /* get parent's known folder path (recursive) */
3502 hr = get_known_folder_registry_path(NULL, parentGuid, &parentRegistryPath);
3503 if(FAILED(hr)) return hr;
3505 hr = get_known_folder_path(parentGuid, parentRegistryPath, &parentPath);
3506 if(FAILED(hr)) {
3507 HeapFree(GetProcessHeap(), 0, parentRegistryPath);
3508 return hr;
3511 lstrcatW(path, parentPath);
3512 lstrcatW(path, sBackslash);
3514 HeapFree(GetProcessHeap(), 0, parentRegistryPath);
3515 HeapFree(GetProcessHeap(), 0, parentPath);
3518 /* check, if folder was redirected */
3519 if(SUCCEEDED(hr))
3520 hr = HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE, registryPath, szCategory, RRF_RT_REG_DWORD, NULL, &category, &dwSize));
3522 if(SUCCEEDED(hr))
3524 if(category == KF_CATEGORY_COMMON)
3525 hRedirectionRootKey = HKEY_LOCAL_MACHINE;
3526 else if(category == KF_CATEGORY_PERUSER)
3527 hRedirectionRootKey = HKEY_CURRENT_USER;
3529 if(hRedirectionRootKey)
3531 hr = HRESULT_FROM_WIN32(RegGetValueW(hRedirectionRootKey, szKnownFolderRedirections, sFolderId, RRF_RT_REG_SZ, NULL, NULL, &dwSize));
3533 if(SUCCEEDED(hr))
3535 *ppszPath = CoTaskMemAlloc(dwSize+(lstrlenW(path)+1)*sizeof(WCHAR));
3536 if(!*ppszPath) hr = E_OUTOFMEMORY;
3539 if(SUCCEEDED(hr))
3541 lstrcpyW(*ppszPath, path);
3542 hr = HRESULT_FROM_WIN32(RegGetValueW(hRedirectionRootKey, szKnownFolderRedirections, sFolderId, RRF_RT_REG_SZ, NULL, *ppszPath + lstrlenW(path), &dwSize));
3546 if(!*ppszPath)
3548 /* no redirection, use previous way - read the relative path from folder definition */
3549 hr = HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE, registryPath, szRelativePath, RRF_RT_REG_SZ, &dwType, NULL, &dwSize));
3551 if(SUCCEEDED(hr))
3553 *ppszPath = CoTaskMemAlloc(dwSize+(lstrlenW(path)+1)*sizeof(WCHAR));
3554 if(!*ppszPath) hr = E_OUTOFMEMORY;
3557 if(SUCCEEDED(hr))
3559 lstrcpyW(*ppszPath, path);
3560 hr = HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE, registryPath, szRelativePath, RRF_RT_REG_SZ, &dwType, *ppszPath + lstrlenW(path), &dwSize));
3565 TRACE("returning path: %s\n", debugstr_w(*ppszPath));
3566 return hr;
3569 static HRESULT get_known_folder_path_by_id(
3570 REFKNOWNFOLDERID folderId,
3571 LPWSTR lpRegistryPath,
3572 DWORD dwFlags,
3573 LPWSTR *ppszPath)
3575 HRESULT hr;
3576 WCHAR sGuid[39];
3577 DWORD dwAttributes;
3579 TRACE("(%s, %s, 0x%08x, %p)\n", debugstr_guid(folderId), debugstr_w(lpRegistryPath), dwFlags, ppszPath);
3581 /* if this is registry-registered known folder, get path from registry */
3582 if(lpRegistryPath)
3584 StringFromGUID2(folderId, sGuid, sizeof(sGuid)/sizeof(sGuid[0]));
3586 hr = get_known_folder_path(sGuid, lpRegistryPath, ppszPath);
3588 /* in other case, use older way */
3589 else
3590 hr = SHGetKnownFolderPath( folderId, dwFlags, NULL, ppszPath );
3592 if (FAILED(hr)) return hr;
3594 /* check if known folder really exists */
3595 dwAttributes = GetFileAttributesW(*ppszPath);
3596 if(dwAttributes == INVALID_FILE_ATTRIBUTES || !(dwAttributes & FILE_ATTRIBUTE_DIRECTORY) )
3598 TRACE("directory %s not found\n", debugstr_w(*ppszPath));
3599 CoTaskMemFree(*ppszPath);
3600 *ppszPath = NULL;
3601 hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
3604 return hr;
3607 static HRESULT WINAPI knownfolder_GetPath(
3608 IKnownFolder *iface,
3609 DWORD dwFlags,
3610 LPWSTR *ppszPath)
3612 struct knownfolder *knownfolder = impl_from_IKnownFolder( iface );
3613 TRACE("(%p, 0x%08x, %p)\n", knownfolder, dwFlags, ppszPath);
3615 return get_known_folder_path_by_id(&knownfolder->id, knownfolder->registryPath, dwFlags, ppszPath);
3618 static HRESULT WINAPI knownfolder_SetPath(
3619 IKnownFolder *iface,
3620 DWORD dwFlags,
3621 LPCWSTR pszPath)
3623 struct knownfolder *knownfolder = impl_from_IKnownFolder( iface );
3624 HRESULT hr = S_OK;
3626 TRACE("(%p, 0x%08x, %s)\n", knownfolder, dwFlags, debugstr_w(pszPath));
3628 /* check if the known folder is registered */
3629 if(!knownfolder->registryPath)
3630 hr = E_FAIL;
3632 if(SUCCEEDED(hr))
3633 hr = redirect_known_folder(&knownfolder->id, NULL, 0, pszPath, 0, NULL, NULL);
3635 return hr;
3638 static HRESULT WINAPI knownfolder_GetIDList(
3639 IKnownFolder *iface,
3640 DWORD dwFlags,
3641 PIDLIST_ABSOLUTE *ppidl)
3643 FIXME("0x%08x, %p\n", dwFlags, ppidl);
3644 return E_NOTIMPL;
3647 static HRESULT WINAPI knownfolder_GetFolderType(
3648 IKnownFolder *iface,
3649 FOLDERTYPEID *pftid)
3651 FIXME("%p\n", pftid);
3652 return E_NOTIMPL;
3655 static HRESULT WINAPI knownfolder_GetRedirectionCapabilities(
3656 IKnownFolder *iface,
3657 KF_REDIRECTION_CAPABILITIES *pCapabilities)
3659 FIXME("%p\n", pCapabilities);
3660 return E_NOTIMPL;
3663 static HRESULT WINAPI knownfolder_GetFolderDefinition(
3664 IKnownFolder *iface,
3665 KNOWNFOLDER_DEFINITION *pKFD)
3667 struct knownfolder *knownfolder = impl_from_IKnownFolder( iface );
3668 HRESULT hr;
3669 DWORD dwSize;
3670 TRACE("(%p, %p)\n", knownfolder, pKFD);
3672 if(!pKFD) return E_INVALIDARG;
3674 ZeroMemory(pKFD, sizeof(*pKFD));
3676 hr = get_known_folder_category(knownfolder->registryPath, &pKFD->category);
3678 if(SUCCEEDED(hr))
3679 hr = HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE, knownfolder->registryPath, szName, RRF_RT_REG_SZ, NULL, NULL, &dwSize));
3681 if(SUCCEEDED(hr))
3683 pKFD->pszName = CoTaskMemAlloc(dwSize);
3684 if(!pKFD->pszName) hr = E_OUTOFMEMORY;
3687 if(SUCCEEDED(hr))
3688 hr = HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE, knownfolder->registryPath, szName, RRF_RT_REG_SZ, NULL, pKFD->pszName, &dwSize));
3690 return hr;
3693 static const struct IKnownFolderVtbl knownfolder_vtbl =
3695 knownfolder_QueryInterface,
3696 knownfolder_AddRef,
3697 knownfolder_Release,
3698 knownfolder_GetId,
3699 knownfolder_GetCategory,
3700 knownfolder_GetShellItem,
3701 knownfolder_GetPath,
3702 knownfolder_SetPath,
3703 knownfolder_GetIDList,
3704 knownfolder_GetFolderType,
3705 knownfolder_GetRedirectionCapabilities,
3706 knownfolder_GetFolderDefinition
3709 static HRESULT knownfolder_create( struct knownfolder **knownfolder )
3711 struct knownfolder *kf;
3713 kf = HeapAlloc( GetProcessHeap(), 0, sizeof(*kf) );
3714 if (!kf) return E_OUTOFMEMORY;
3716 kf->IKnownFolder_iface.lpVtbl = &knownfolder_vtbl;
3717 kf->refs = 1;
3718 memset( &kf->id, 0, sizeof(kf->id) );
3719 kf->registryPath = NULL;
3721 *knownfolder = kf;
3723 TRACE("returning iface %p\n", &kf->IKnownFolder_iface);
3724 return S_OK;
3727 struct foldermanager
3729 IKnownFolderManager IKnownFolderManager_iface;
3730 LONG refs;
3731 UINT num_ids;
3732 KNOWNFOLDERID *ids;
3735 static inline struct foldermanager *impl_from_IKnownFolderManager( IKnownFolderManager *iface )
3737 return CONTAINING_RECORD( iface, struct foldermanager, IKnownFolderManager_iface );
3740 static ULONG WINAPI foldermanager_AddRef(
3741 IKnownFolderManager *iface )
3743 struct foldermanager *foldermanager = impl_from_IKnownFolderManager( iface );
3744 return InterlockedIncrement( &foldermanager->refs );
3747 static ULONG WINAPI foldermanager_Release(
3748 IKnownFolderManager *iface )
3750 struct foldermanager *foldermanager = impl_from_IKnownFolderManager( iface );
3751 LONG refs = InterlockedDecrement( &foldermanager->refs );
3752 if (!refs)
3754 TRACE("destroying %p\n", foldermanager);
3755 HeapFree( GetProcessHeap(), 0, foldermanager->ids );
3756 HeapFree( GetProcessHeap(), 0, foldermanager );
3758 return refs;
3761 static HRESULT WINAPI foldermanager_QueryInterface(
3762 IKnownFolderManager *iface,
3763 REFIID riid,
3764 void **ppv )
3766 struct foldermanager *This = impl_from_IKnownFolderManager( iface );
3768 TRACE("%p %s %p\n", This, debugstr_guid( riid ), ppv );
3770 if ( IsEqualGUID( riid, &IID_IKnownFolderManager ) ||
3771 IsEqualGUID( riid, &IID_IUnknown ) )
3773 *ppv = iface;
3775 else
3777 FIXME("interface %s not implemented\n", debugstr_guid(riid));
3778 return E_NOINTERFACE;
3780 IKnownFolderManager_AddRef( iface );
3781 return S_OK;
3784 static HRESULT WINAPI foldermanager_FolderIdFromCsidl(
3785 IKnownFolderManager *iface,
3786 int nCsidl,
3787 KNOWNFOLDERID *pfid)
3789 TRACE("%d, %p\n", nCsidl, pfid);
3791 if (nCsidl >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
3792 return E_INVALIDARG;
3793 *pfid = *CSIDL_Data[nCsidl].id;
3794 return S_OK;
3797 static HRESULT WINAPI foldermanager_FolderIdToCsidl(
3798 IKnownFolderManager *iface,
3799 REFKNOWNFOLDERID rfid,
3800 int *pnCsidl)
3802 int csidl;
3804 TRACE("%s, %p\n", debugstr_guid(rfid), pnCsidl);
3806 csidl = csidl_from_id( rfid );
3807 if (csidl == -1) return E_INVALIDARG;
3808 *pnCsidl = csidl;
3809 return S_OK;
3812 static HRESULT WINAPI foldermanager_GetFolderIds(
3813 IKnownFolderManager *iface,
3814 KNOWNFOLDERID **ppKFId,
3815 UINT *pCount)
3817 struct foldermanager *fm = impl_from_IKnownFolderManager( iface );
3819 TRACE("%p, %p\n", ppKFId, pCount);
3821 *ppKFId = fm->ids;
3822 *pCount = fm->num_ids;
3823 return S_OK;
3826 static BOOL is_knownfolder( struct foldermanager *fm, const KNOWNFOLDERID *id )
3828 UINT i;
3829 HRESULT hr;
3830 LPWSTR registryPath = NULL;
3831 HKEY hKey;
3833 /* TODO: move all entries from "CSIDL_Data" static array to registry known folder descriptions */
3834 for (i = 0; i < fm->num_ids; i++)
3835 if (IsEqualGUID( &fm->ids[i], id )) return TRUE;
3837 hr = get_known_folder_registry_path(id, NULL, &registryPath);
3838 if(SUCCEEDED(hr))
3840 hr = HRESULT_FROM_WIN32(RegOpenKeyExW(HKEY_LOCAL_MACHINE, registryPath, 0, 0, &hKey));
3841 HeapFree(GetProcessHeap(), 0, registryPath);
3844 if(SUCCEEDED(hr))
3846 hr = S_OK;
3847 RegCloseKey(hKey);
3850 return hr == S_OK;
3853 static HRESULT WINAPI foldermanager_GetFolder(
3854 IKnownFolderManager *iface,
3855 REFKNOWNFOLDERID rfid,
3856 IKnownFolder **ppkf)
3858 struct foldermanager *fm = impl_from_IKnownFolderManager( iface );
3859 struct knownfolder *kf;
3860 HRESULT hr;
3862 TRACE("%s, %p\n", debugstr_guid(rfid), ppkf);
3864 if (!is_knownfolder( fm, rfid ))
3866 WARN("unknown folder\n");
3867 return E_INVALIDARG;
3869 hr = knownfolder_create( &kf );
3870 if (SUCCEEDED( hr ))
3872 hr = knownfolder_set_id( kf, rfid );
3873 *ppkf = &kf->IKnownFolder_iface;
3875 else
3876 *ppkf = NULL;
3878 return hr;
3881 static HRESULT WINAPI foldermanager_GetFolderByName(
3882 IKnownFolderManager *iface,
3883 LPCWSTR pszCanonicalName,
3884 IKnownFolder **ppkf)
3886 FIXME("%s, %p\n", debugstr_w(pszCanonicalName), ppkf);
3887 return E_NOTIMPL;
3890 static HRESULT WINAPI foldermanager_RegisterFolder(
3891 IKnownFolderManager *iface,
3892 REFKNOWNFOLDERID rfid,
3893 KNOWNFOLDER_DEFINITION const *pKFD)
3895 HRESULT hr;
3896 HKEY hKey = NULL;
3897 DWORD dwDisp;
3898 LPWSTR registryPath = NULL;
3899 TRACE("(%p, %s, %p)\n", iface, debugstr_guid(rfid), pKFD);
3901 hr = get_known_folder_registry_path(rfid, NULL, &registryPath);
3902 TRACE("registry path: %s\n", debugstr_w(registryPath));
3904 if(SUCCEEDED(hr))
3905 hr = HRESULT_FROM_WIN32(RegCreateKeyExW(HKEY_LOCAL_MACHINE, registryPath, 0, NULL, 0, KEY_WRITE, 0, &hKey, &dwDisp));
3907 if(SUCCEEDED(hr))
3909 if(dwDisp == REG_OPENED_EXISTING_KEY)
3910 hr = E_FAIL;
3912 if(SUCCEEDED(hr))
3913 hr = HRESULT_FROM_WIN32(RegSetValueExW(hKey, szCategory, 0, REG_DWORD, (LPBYTE)&pKFD->category, sizeof(pKFD->category)));
3915 if(SUCCEEDED(hr))
3916 hr = HRESULT_FROM_WIN32(RegSetValueExW(hKey, szName, 0, REG_SZ, (LPBYTE)pKFD->pszName, (lstrlenW(pKFD->pszName)+1)*sizeof(WCHAR) ));
3918 if(SUCCEEDED(hr) && !IsEqualGUID(&pKFD->fidParent, &GUID_NULL))
3920 WCHAR sParentGuid[39];
3921 StringFromGUID2(&pKFD->fidParent, sParentGuid, sizeof(sParentGuid)/sizeof(sParentGuid[0]));
3923 /* this known folder has parent folder */
3924 hr = HRESULT_FROM_WIN32(RegSetValueExW(hKey, szParentFolder, 0, REG_SZ, (LPBYTE)sParentGuid, sizeof(sParentGuid)));
3927 if(SUCCEEDED(hr) && pKFD->category != KF_CATEGORY_VIRTUAL)
3929 if(!pKFD->pszRelativePath)
3930 hr = E_INVALIDARG;
3932 if(SUCCEEDED(hr))
3933 hr = HRESULT_FROM_WIN32(RegSetValueExW(hKey, szRelativePath, 0, REG_SZ, (LPBYTE)pKFD->pszRelativePath, (lstrlenW(pKFD->pszRelativePath)+1)*sizeof(WCHAR) ));
3936 RegCloseKey(hKey);
3938 if(FAILED(hr))
3939 SHDeleteKeyW(HKEY_LOCAL_MACHINE, registryPath);
3942 HeapFree(GetProcessHeap(), 0, registryPath);
3943 return hr;
3946 static HRESULT WINAPI foldermanager_UnregisterFolder(
3947 IKnownFolderManager *iface,
3948 REFKNOWNFOLDERID rfid)
3950 HRESULT hr;
3951 LPWSTR registryPath = NULL;
3952 TRACE("(%p, %s)\n", iface, debugstr_guid(rfid));
3954 hr = get_known_folder_registry_path(rfid, NULL, &registryPath);
3956 if(SUCCEEDED(hr))
3957 hr = HRESULT_FROM_WIN32(SHDeleteKeyW(HKEY_LOCAL_MACHINE, registryPath));
3959 HeapFree(GetProcessHeap(), 0, registryPath);
3960 return hr;
3963 static HRESULT WINAPI foldermanager_FindFolderFromPath(
3964 IKnownFolderManager *iface,
3965 LPCWSTR pszPath,
3966 FFFP_MODE mode,
3967 IKnownFolder **ppkf)
3969 FIXME("%s, 0x%08x, %p\n", debugstr_w(pszPath), mode, ppkf);
3970 return E_NOTIMPL;
3973 static HRESULT WINAPI foldermanager_FindFolderFromIDList(
3974 IKnownFolderManager *iface,
3975 PCIDLIST_ABSOLUTE pidl,
3976 IKnownFolder **ppkf)
3978 FIXME("%p, %p\n", pidl, ppkf);
3979 return E_NOTIMPL;
3982 static HRESULT WINAPI foldermanager_Redirect(
3983 IKnownFolderManager *iface,
3984 REFKNOWNFOLDERID rfid,
3985 HWND hwnd,
3986 KF_REDIRECT_FLAGS flags,
3987 LPCWSTR pszTargetPath,
3988 UINT cFolders,
3989 KNOWNFOLDERID const *pExclusion,
3990 LPWSTR *ppszError)
3992 return redirect_known_folder(rfid, hwnd, flags, pszTargetPath, cFolders, pExclusion, ppszError);
3995 static const struct IKnownFolderManagerVtbl foldermanager_vtbl =
3997 foldermanager_QueryInterface,
3998 foldermanager_AddRef,
3999 foldermanager_Release,
4000 foldermanager_FolderIdFromCsidl,
4001 foldermanager_FolderIdToCsidl,
4002 foldermanager_GetFolderIds,
4003 foldermanager_GetFolder,
4004 foldermanager_GetFolderByName,
4005 foldermanager_RegisterFolder,
4006 foldermanager_UnregisterFolder,
4007 foldermanager_FindFolderFromPath,
4008 foldermanager_FindFolderFromIDList,
4009 foldermanager_Redirect
4012 static HRESULT foldermanager_create( void **ppv )
4014 UINT i, j;
4015 struct foldermanager *fm;
4017 fm = HeapAlloc( GetProcessHeap(), 0, sizeof(*fm) );
4018 if (!fm) return E_OUTOFMEMORY;
4020 fm->IKnownFolderManager_iface.lpVtbl = &foldermanager_vtbl;
4021 fm->refs = 1;
4022 fm->num_ids = 0;
4024 for (i = 0; i < sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]); i++)
4026 if (!IsEqualGUID( CSIDL_Data[i].id, &GUID_NULL )) fm->num_ids++;
4028 fm->ids = HeapAlloc( GetProcessHeap(), 0, fm->num_ids * sizeof(KNOWNFOLDERID) );
4029 if (!fm->ids)
4031 HeapFree( GetProcessHeap(), 0, fm );
4032 return E_OUTOFMEMORY;
4034 for (i = j = 0; i < sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]); i++)
4036 if (!IsEqualGUID( CSIDL_Data[i].id, &GUID_NULL ))
4038 fm->ids[j] = *CSIDL_Data[i].id;
4039 j++;
4042 TRACE("found %u known folders\n", fm->num_ids);
4043 *ppv = &fm->IKnownFolderManager_iface;
4045 TRACE("returning iface %p\n", *ppv);
4046 return S_OK;
4049 HRESULT WINAPI KnownFolderManager_Constructor( IUnknown *punk, REFIID riid, void **ppv )
4051 TRACE("%p, %s, %p\n", punk, debugstr_guid(riid), ppv);
4053 if (!ppv)
4054 return E_POINTER;
4055 if (punk)
4056 return CLASS_E_NOAGGREGATION;
4058 return foldermanager_create( ppv );
4061 HRESULT WINAPI SHGetKnownFolderIDList(REFKNOWNFOLDERID rfid, DWORD flags, HANDLE token, PIDLIST_ABSOLUTE *pidl)
4063 FIXME("%s, 0x%08x, %p, %p\n", debugstr_guid(rfid), flags, token, pidl);
4064 return E_NOTIMPL;