po: Update Catalan translation.
[wine.git] / dlls / shell32 / shellpath.c
blob9612687c856de7acf14ab2aee180fb318d945e26
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 #define INITGUID
55 #include "knownfolders.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 * NOTES
542 * exported by ordinal
544 BOOL WINAPI PathYetAnotherMakeUniqueName(
545 LPWSTR lpszBuffer,
546 LPCWSTR lpszPathName,
547 LPCWSTR lpszShortName,
548 LPCWSTR lpszLongName)
550 FIXME("(%p, %s, %s ,%s):stub.\n",
551 lpszBuffer, debugstr_w(lpszPathName), debugstr_w(lpszShortName), debugstr_w(lpszLongName));
552 return TRUE;
557 ########## cleaning and resolving paths ##########
560 /*************************************************************************
561 * PathFindOnPath [SHELL32.145]
563 BOOL WINAPI PathFindOnPathAW(LPVOID sFile, LPCVOID *sOtherDirs)
565 if (SHELL_OsIsUnicode())
566 return PathFindOnPathW(sFile, (LPCWSTR *)sOtherDirs);
567 return PathFindOnPathA(sFile, (LPCSTR *)sOtherDirs);
570 /*************************************************************************
571 * PathCleanupSpec [SHELL32.171]
573 * lpszFile is changed in place.
575 int WINAPI PathCleanupSpec( LPCWSTR lpszPathW, LPWSTR lpszFileW )
577 int i = 0;
578 DWORD rc = 0;
579 int length = 0;
581 if (SHELL_OsIsUnicode())
583 LPWSTR p = lpszFileW;
585 TRACE("Cleanup %s\n",debugstr_w(lpszFileW));
587 if (lpszPathW)
588 length = strlenW(lpszPathW);
590 while (*p)
592 int gct = PathGetCharTypeW(*p);
593 if (gct == GCT_INVALID || gct == GCT_WILD || gct == GCT_SEPARATOR)
595 lpszFileW[i]='-';
596 rc |= PCS_REPLACEDCHAR;
598 else
599 lpszFileW[i]=*p;
600 i++;
601 p++;
602 if (length + i == MAX_PATH)
604 rc |= PCS_FATAL | PCS_PATHTOOLONG;
605 break;
608 lpszFileW[i]=0;
610 else
612 LPSTR lpszFileA = (LPSTR)lpszFileW;
613 LPCSTR lpszPathA = (LPCSTR)lpszPathW;
614 LPSTR p = lpszFileA;
616 TRACE("Cleanup %s\n",debugstr_a(lpszFileA));
618 if (lpszPathA)
619 length = strlen(lpszPathA);
621 while (*p)
623 int gct = PathGetCharTypeA(*p);
624 if (gct == GCT_INVALID || gct == GCT_WILD || gct == GCT_SEPARATOR)
626 lpszFileA[i]='-';
627 rc |= PCS_REPLACEDCHAR;
629 else
630 lpszFileA[i]=*p;
631 i++;
632 p++;
633 if (length + i == MAX_PATH)
635 rc |= PCS_FATAL | PCS_PATHTOOLONG;
636 break;
639 lpszFileA[i]=0;
641 return rc;
644 /*************************************************************************
645 * PathQualifyA [SHELL32]
647 static BOOL PathQualifyA(LPCSTR pszPath)
649 FIXME("%s\n",pszPath);
650 return 0;
653 /*************************************************************************
654 * PathQualifyW [SHELL32]
656 static BOOL PathQualifyW(LPCWSTR pszPath)
658 FIXME("%s\n",debugstr_w(pszPath));
659 return 0;
662 /*************************************************************************
663 * PathQualify [SHELL32.49]
665 BOOL WINAPI PathQualifyAW(LPCVOID pszPath)
667 if (SHELL_OsIsUnicode())
668 return PathQualifyW(pszPath);
669 return PathQualifyA(pszPath);
672 static BOOL PathResolveA(LPSTR path, LPCSTR *paths, DWORD flags)
674 FIXME("(%s,%p,0x%08x),stub!\n", debugstr_a(path), paths, flags);
675 return FALSE;
678 static BOOL PathResolveW(LPWSTR path, LPCWSTR *paths, DWORD flags)
680 FIXME("(%s,%p,0x%08x),stub!\n", debugstr_w(path), paths, flags);
681 return FALSE;
684 /*************************************************************************
685 * PathResolve [SHELL32.51]
687 BOOL WINAPI PathResolveAW(LPVOID path, LPCVOID *paths, DWORD flags)
689 if (SHELL_OsIsUnicode())
690 return PathResolveW(path, (LPCWSTR*)paths, flags);
691 else
692 return PathResolveA(path, (LPCSTR*)paths, flags);
695 /*************************************************************************
696 * PathProcessCommandA
698 static LONG PathProcessCommandA (
699 LPCSTR lpszPath,
700 LPSTR lpszBuff,
701 DWORD dwBuffSize,
702 DWORD dwFlags)
704 FIXME("%s %p 0x%04x 0x%04x stub\n",
705 lpszPath, lpszBuff, dwBuffSize, dwFlags);
706 if(!lpszPath) return -1;
707 if(lpszBuff) strcpy(lpszBuff, lpszPath);
708 return strlen(lpszPath);
711 /*************************************************************************
712 * PathProcessCommandW
714 static LONG PathProcessCommandW (
715 LPCWSTR lpszPath,
716 LPWSTR lpszBuff,
717 DWORD dwBuffSize,
718 DWORD dwFlags)
720 FIXME("(%s, %p, 0x%04x, 0x%04x) stub\n",
721 debugstr_w(lpszPath), lpszBuff, dwBuffSize, dwFlags);
722 if(!lpszPath) return -1;
723 if(lpszBuff) strcpyW(lpszBuff, lpszPath);
724 return strlenW(lpszPath);
727 /*************************************************************************
728 * PathProcessCommand (SHELL32.653)
730 LONG WINAPI PathProcessCommandAW (
731 LPCVOID lpszPath,
732 LPVOID lpszBuff,
733 DWORD dwBuffSize,
734 DWORD dwFlags)
736 if (SHELL_OsIsUnicode())
737 return PathProcessCommandW(lpszPath, lpszBuff, dwBuffSize, dwFlags);
738 return PathProcessCommandA(lpszPath, lpszBuff, dwBuffSize, dwFlags);
742 ########## special ##########
745 /*************************************************************************
746 * PathSetDlgItemPath (SHELL32.48)
748 VOID WINAPI PathSetDlgItemPathAW(HWND hDlg, int id, LPCVOID pszPath)
750 if (SHELL_OsIsUnicode())
751 PathSetDlgItemPathW(hDlg, id, pszPath);
752 else
753 PathSetDlgItemPathA(hDlg, id, pszPath);
756 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'};
757 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'};
758 static const WCHAR AppDataW[] = {'A','p','p','D','a','t','a','\0'};
759 static const WCHAR CacheW[] = {'C','a','c','h','e','\0'};
760 static const WCHAR CD_BurningW[] = {'C','D',' ','B','u','r','n','i','n','g','\0'};
761 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'};
762 static const WCHAR Common_AppDataW[] = {'C','o','m','m','o','n',' ','A','p','p','D','a','t','a','\0'};
763 static const WCHAR Common_DesktopW[] = {'C','o','m','m','o','n',' ','D','e','s','k','t','o','p','\0'};
764 static const WCHAR Common_DocumentsW[] = {'C','o','m','m','o','n',' ','D','o','c','u','m','e','n','t','s','\0'};
765 static const WCHAR Common_FavoritesW[] = {'C','o','m','m','o','n',' ','F','a','v','o','r','i','t','e','s','\0'};
766 static const WCHAR CommonFilesDirW[] = {'C','o','m','m','o','n','F','i','l','e','s','D','i','r','\0'};
767 static const WCHAR CommonFilesDirX86W[] = {'C','o','m','m','o','n','F','i','l','e','s','D','i','r',' ','(','x','8','6',')','\0'};
768 static const WCHAR CommonMusicW[] = {'C','o','m','m','o','n','M','u','s','i','c','\0'};
769 static const WCHAR CommonPicturesW[] = {'C','o','m','m','o','n','P','i','c','t','u','r','e','s','\0'};
770 static const WCHAR Common_ProgramsW[] = {'C','o','m','m','o','n',' ','P','r','o','g','r','a','m','s','\0'};
771 static const WCHAR Common_StartUpW[] = {'C','o','m','m','o','n',' ','S','t','a','r','t','U','p','\0'};
772 static const WCHAR Common_Start_MenuW[] = {'C','o','m','m','o','n',' ','S','t','a','r','t',' ','M','e','n','u','\0'};
773 static const WCHAR Common_TemplatesW[] = {'C','o','m','m','o','n',' ','T','e','m','p','l','a','t','e','s','\0'};
774 static const WCHAR CommonVideoW[] = {'C','o','m','m','o','n','V','i','d','e','o','\0'};
775 static const WCHAR ContactsW[] = {'C','o','n','t','a','c','t','s','\0'};
776 static const WCHAR CookiesW[] = {'C','o','o','k','i','e','s','\0'};
777 static const WCHAR DesktopW[] = {'D','e','s','k','t','o','p','\0'};
778 static const WCHAR FavoritesW[] = {'F','a','v','o','r','i','t','e','s','\0'};
779 static const WCHAR FontsW[] = {'F','o','n','t','s','\0'};
780 static const WCHAR HistoryW[] = {'H','i','s','t','o','r','y','\0'};
781 static const WCHAR Local_AppDataW[] = {'L','o','c','a','l',' ','A','p','p','D','a','t','a','\0'};
782 static const WCHAR My_MusicW[] = {'M','y',' ','M','u','s','i','c','\0'};
783 static const WCHAR My_PicturesW[] = {'M','y',' ','P','i','c','t','u','r','e','s','\0'};
784 static const WCHAR My_VideoW[] = {'M','y',' ','V','i','d','e','o','s','\0'};
785 static const WCHAR NetHoodW[] = {'N','e','t','H','o','o','d','\0'};
786 static const WCHAR PersonalW[] = {'P','e','r','s','o','n','a','l','\0'};
787 static const WCHAR PrintHoodW[] = {'P','r','i','n','t','H','o','o','d','\0'};
788 static const WCHAR ProgramFilesDirW[] = {'P','r','o','g','r','a','m','F','i','l','e','s','D','i','r','\0'};
789 static const WCHAR ProgramFilesDirX86W[] = {'P','r','o','g','r','a','m','F','i','l','e','s','D','i','r',' ','(','x','8','6',')','\0'};
790 static const WCHAR ProgramsW[] = {'P','r','o','g','r','a','m','s','\0'};
791 static const WCHAR RecentW[] = {'R','e','c','e','n','t','\0'};
792 static const WCHAR ResourcesW[] = {'R','e','s','o','u','r','c','e','s','\0'};
793 static const WCHAR SendToW[] = {'S','e','n','d','T','o','\0'};
794 static const WCHAR StartUpW[] = {'S','t','a','r','t','U','p','\0'};
795 static const WCHAR Start_MenuW[] = {'S','t','a','r','t',' ','M','e','n','u','\0'};
796 static const WCHAR TemplatesW[] = {'T','e','m','p','l','a','t','e','s','\0'};
797 static const WCHAR UsersW[] = {'U','s','e','r','s','\0'};
798 static const WCHAR UsersPublicW[] = {'U','s','e','r','s','\\','P','u','b','l','i','c','\0'};
799 static const WCHAR DefaultW[] = {'.','D','e','f','a','u','l','t','\0'};
800 static const WCHAR AllUsersProfileW[] = {'%','A','L','L','U','S','E','R','S','P','R','O','F','I','L','E','%','\0'};
801 static const WCHAR UserProfileW[] = {'%','U','S','E','R','P','R','O','F','I','L','E','%','\0'};
802 static const WCHAR SystemDriveW[] = {'%','S','y','s','t','e','m','D','r','i','v','e','%','\0'};
803 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};
804 static const WCHAR ProfilesDirectoryW[] = {'P','r','o','f','i','l','e','s','D','i','r','e','c','t','o','r','y',0};
805 static const WCHAR AllUsersProfileValueW[] = {'A','l','l','U','s','e','r','s','P','r','o','f','i','l','e','\0'};
806 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'};
807 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'};
808 static const WCHAR szDefaultProfileDirW[] = {'u','s','e','r','s',0};
809 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'};
810 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};
811 static const WCHAR AllUsersW[] = {'P','u','b','l','i','c',0};
813 typedef enum _CSIDL_Type {
814 CSIDL_Type_User,
815 CSIDL_Type_AllUsers,
816 CSIDL_Type_CurrVer,
817 CSIDL_Type_Disallowed,
818 CSIDL_Type_NonExistent,
819 CSIDL_Type_WindowsPath,
820 CSIDL_Type_SystemPath,
821 CSIDL_Type_SystemX86Path,
822 } CSIDL_Type;
824 typedef struct
826 const KNOWNFOLDERID *id;
827 CSIDL_Type type;
828 LPCWSTR szValueName;
829 LPCWSTR szDefaultPath; /* fallback string or resource ID */
830 } CSIDL_DATA;
832 static const CSIDL_DATA CSIDL_Data[] =
834 { /* 0x00 - CSIDL_DESKTOP */
835 &FOLDERID_Desktop,
836 CSIDL_Type_User,
837 DesktopW,
838 MAKEINTRESOURCEW(IDS_DESKTOPDIRECTORY)
840 { /* 0x01 - CSIDL_INTERNET */
841 &FOLDERID_InternetFolder,
842 CSIDL_Type_Disallowed,
843 NULL,
844 NULL
846 { /* 0x02 - CSIDL_PROGRAMS */
847 &FOLDERID_Programs,
848 CSIDL_Type_User,
849 ProgramsW,
850 MAKEINTRESOURCEW(IDS_PROGRAMS)
852 { /* 0x03 - CSIDL_CONTROLS (.CPL files) */
853 &FOLDERID_ControlPanelFolder,
854 CSIDL_Type_SystemPath,
855 NULL,
856 NULL
858 { /* 0x04 - CSIDL_PRINTERS */
859 &FOLDERID_PrintersFolder,
860 CSIDL_Type_SystemPath,
861 NULL,
862 NULL
864 { /* 0x05 - CSIDL_PERSONAL */
865 &FOLDERID_Documents,
866 CSIDL_Type_User,
867 PersonalW,
868 MAKEINTRESOURCEW(IDS_PERSONAL)
870 { /* 0x06 - CSIDL_FAVORITES */
871 &FOLDERID_Favorites,
872 CSIDL_Type_User,
873 FavoritesW,
874 MAKEINTRESOURCEW(IDS_FAVORITES)
876 { /* 0x07 - CSIDL_STARTUP */
877 &FOLDERID_Startup,
878 CSIDL_Type_User,
879 StartUpW,
880 MAKEINTRESOURCEW(IDS_STARTUP)
882 { /* 0x08 - CSIDL_RECENT */
883 &FOLDERID_Recent,
884 CSIDL_Type_User,
885 RecentW,
886 MAKEINTRESOURCEW(IDS_RECENT)
888 { /* 0x09 - CSIDL_SENDTO */
889 &FOLDERID_SendTo,
890 CSIDL_Type_User,
891 SendToW,
892 MAKEINTRESOURCEW(IDS_SENDTO)
894 { /* 0x0a - CSIDL_BITBUCKET - Recycle Bin */
895 &FOLDERID_RecycleBinFolder,
896 CSIDL_Type_Disallowed,
897 NULL,
898 NULL,
900 { /* 0x0b - CSIDL_STARTMENU */
901 &FOLDERID_StartMenu,
902 CSIDL_Type_User,
903 Start_MenuW,
904 MAKEINTRESOURCEW(IDS_STARTMENU)
906 { /* 0x0c - CSIDL_MYDOCUMENTS */
907 &GUID_NULL,
908 CSIDL_Type_Disallowed, /* matches WinXP--can't get its path */
909 NULL,
910 NULL
912 { /* 0x0d - CSIDL_MYMUSIC */
913 &FOLDERID_Music,
914 CSIDL_Type_User,
915 My_MusicW,
916 MAKEINTRESOURCEW(IDS_MYMUSIC)
918 { /* 0x0e - CSIDL_MYVIDEO */
919 &FOLDERID_Videos,
920 CSIDL_Type_User,
921 My_VideoW,
922 MAKEINTRESOURCEW(IDS_MYVIDEO)
924 { /* 0x0f - unassigned */
925 &GUID_NULL,
926 CSIDL_Type_Disallowed,
927 NULL,
928 NULL,
930 { /* 0x10 - CSIDL_DESKTOPDIRECTORY */
931 &FOLDERID_Desktop,
932 CSIDL_Type_User,
933 DesktopW,
934 MAKEINTRESOURCEW(IDS_DESKTOPDIRECTORY)
936 { /* 0x11 - CSIDL_DRIVES */
937 &FOLDERID_ComputerFolder,
938 CSIDL_Type_Disallowed,
939 NULL,
940 NULL,
942 { /* 0x12 - CSIDL_NETWORK */
943 &FOLDERID_NetworkFolder,
944 CSIDL_Type_Disallowed,
945 NULL,
946 NULL,
948 { /* 0x13 - CSIDL_NETHOOD */
949 &FOLDERID_NetHood,
950 CSIDL_Type_User,
951 NetHoodW,
952 MAKEINTRESOURCEW(IDS_NETHOOD)
954 { /* 0x14 - CSIDL_FONTS */
955 &FOLDERID_Fonts,
956 CSIDL_Type_WindowsPath,
957 FontsW,
958 FontsW
960 { /* 0x15 - CSIDL_TEMPLATES */
961 &FOLDERID_Templates,
962 CSIDL_Type_User,
963 TemplatesW,
964 MAKEINTRESOURCEW(IDS_TEMPLATES)
966 { /* 0x16 - CSIDL_COMMON_STARTMENU */
967 &FOLDERID_CommonStartMenu,
968 CSIDL_Type_AllUsers,
969 Common_Start_MenuW,
970 MAKEINTRESOURCEW(IDS_STARTMENU)
972 { /* 0x17 - CSIDL_COMMON_PROGRAMS */
973 &FOLDERID_CommonPrograms,
974 CSIDL_Type_AllUsers,
975 Common_ProgramsW,
976 MAKEINTRESOURCEW(IDS_PROGRAMS)
978 { /* 0x18 - CSIDL_COMMON_STARTUP */
979 &FOLDERID_CommonStartup,
980 CSIDL_Type_AllUsers,
981 Common_StartUpW,
982 MAKEINTRESOURCEW(IDS_STARTUP)
984 { /* 0x19 - CSIDL_COMMON_DESKTOPDIRECTORY */
985 &FOLDERID_PublicDesktop,
986 CSIDL_Type_AllUsers,
987 Common_DesktopW,
988 MAKEINTRESOURCEW(IDS_DESKTOPDIRECTORY)
990 { /* 0x1a - CSIDL_APPDATA */
991 &FOLDERID_RoamingAppData,
992 CSIDL_Type_User,
993 AppDataW,
994 MAKEINTRESOURCEW(IDS_APPDATA)
996 { /* 0x1b - CSIDL_PRINTHOOD */
997 &FOLDERID_PrintHood,
998 CSIDL_Type_User,
999 PrintHoodW,
1000 MAKEINTRESOURCEW(IDS_PRINTHOOD)
1002 { /* 0x1c - CSIDL_LOCAL_APPDATA */
1003 &FOLDERID_LocalAppData,
1004 CSIDL_Type_User,
1005 Local_AppDataW,
1006 MAKEINTRESOURCEW(IDS_LOCAL_APPDATA)
1008 { /* 0x1d - CSIDL_ALTSTARTUP */
1009 &GUID_NULL,
1010 CSIDL_Type_NonExistent,
1011 NULL,
1012 NULL
1014 { /* 0x1e - CSIDL_COMMON_ALTSTARTUP */
1015 &GUID_NULL,
1016 CSIDL_Type_NonExistent,
1017 NULL,
1018 NULL
1020 { /* 0x1f - CSIDL_COMMON_FAVORITES */
1021 &FOLDERID_Favorites,
1022 CSIDL_Type_AllUsers,
1023 Common_FavoritesW,
1024 MAKEINTRESOURCEW(IDS_FAVORITES)
1026 { /* 0x20 - CSIDL_INTERNET_CACHE */
1027 &FOLDERID_InternetCache,
1028 CSIDL_Type_User,
1029 CacheW,
1030 MAKEINTRESOURCEW(IDS_INTERNET_CACHE)
1032 { /* 0x21 - CSIDL_COOKIES */
1033 &FOLDERID_Cookies,
1034 CSIDL_Type_User,
1035 CookiesW,
1036 MAKEINTRESOURCEW(IDS_COOKIES)
1038 { /* 0x22 - CSIDL_HISTORY */
1039 &FOLDERID_History,
1040 CSIDL_Type_User,
1041 HistoryW,
1042 MAKEINTRESOURCEW(IDS_HISTORY)
1044 { /* 0x23 - CSIDL_COMMON_APPDATA */
1045 &FOLDERID_ProgramData,
1046 CSIDL_Type_AllUsers,
1047 Common_AppDataW,
1048 MAKEINTRESOURCEW(IDS_APPDATA)
1050 { /* 0x24 - CSIDL_WINDOWS */
1051 &FOLDERID_Windows,
1052 CSIDL_Type_WindowsPath,
1053 NULL,
1054 NULL
1056 { /* 0x25 - CSIDL_SYSTEM */
1057 &FOLDERID_System,
1058 CSIDL_Type_SystemPath,
1059 NULL,
1060 NULL
1062 { /* 0x26 - CSIDL_PROGRAM_FILES */
1063 &FOLDERID_ProgramFiles,
1064 CSIDL_Type_CurrVer,
1065 ProgramFilesDirW,
1066 MAKEINTRESOURCEW(IDS_PROGRAM_FILES)
1068 { /* 0x27 - CSIDL_MYPICTURES */
1069 &FOLDERID_Pictures,
1070 CSIDL_Type_User,
1071 My_PicturesW,
1072 MAKEINTRESOURCEW(IDS_MYPICTURES)
1074 { /* 0x28 - CSIDL_PROFILE */
1075 &FOLDERID_Profile,
1076 CSIDL_Type_User,
1077 NULL,
1078 NULL
1080 { /* 0x29 - CSIDL_SYSTEMX86 */
1081 &FOLDERID_SystemX86,
1082 CSIDL_Type_SystemX86Path,
1083 NULL,
1084 NULL
1086 { /* 0x2a - CSIDL_PROGRAM_FILESX86 */
1087 &FOLDERID_ProgramFilesX86,
1088 CSIDL_Type_CurrVer,
1089 ProgramFilesDirX86W,
1090 MAKEINTRESOURCEW(IDS_PROGRAM_FILESX86)
1092 { /* 0x2b - CSIDL_PROGRAM_FILES_COMMON */
1093 &FOLDERID_ProgramFilesCommon,
1094 CSIDL_Type_CurrVer,
1095 CommonFilesDirW,
1096 MAKEINTRESOURCEW(IDS_PROGRAM_FILES_COMMON)
1098 { /* 0x2c - CSIDL_PROGRAM_FILES_COMMONX86 */
1099 &FOLDERID_ProgramFilesCommonX86,
1100 CSIDL_Type_CurrVer,
1101 CommonFilesDirX86W,
1102 MAKEINTRESOURCEW(IDS_PROGRAM_FILES_COMMONX86)
1104 { /* 0x2d - CSIDL_COMMON_TEMPLATES */
1105 &FOLDERID_CommonTemplates,
1106 CSIDL_Type_AllUsers,
1107 Common_TemplatesW,
1108 MAKEINTRESOURCEW(IDS_TEMPLATES)
1110 { /* 0x2e - CSIDL_COMMON_DOCUMENTS */
1111 &FOLDERID_PublicDocuments,
1112 CSIDL_Type_AllUsers,
1113 Common_DocumentsW,
1114 MAKEINTRESOURCEW(IDS_COMMON_DOCUMENTS)
1116 { /* 0x2f - CSIDL_COMMON_ADMINTOOLS */
1117 &FOLDERID_CommonAdminTools,
1118 CSIDL_Type_AllUsers,
1119 Common_Administrative_ToolsW,
1120 MAKEINTRESOURCEW(IDS_ADMINTOOLS)
1122 { /* 0x30 - CSIDL_ADMINTOOLS */
1123 &FOLDERID_AdminTools,
1124 CSIDL_Type_User,
1125 Administrative_ToolsW,
1126 MAKEINTRESOURCEW(IDS_ADMINTOOLS)
1128 { /* 0x31 - CSIDL_CONNECTIONS */
1129 &FOLDERID_ConnectionsFolder,
1130 CSIDL_Type_Disallowed,
1131 NULL,
1132 NULL
1134 { /* 0x32 - unassigned */
1135 &GUID_NULL,
1136 CSIDL_Type_Disallowed,
1137 NULL,
1138 NULL
1140 { /* 0x33 - unassigned */
1141 &GUID_NULL,
1142 CSIDL_Type_Disallowed,
1143 NULL,
1144 NULL
1146 { /* 0x34 - unassigned */
1147 &GUID_NULL,
1148 CSIDL_Type_Disallowed,
1149 NULL,
1150 NULL
1152 { /* 0x35 - CSIDL_COMMON_MUSIC */
1153 &FOLDERID_PublicMusic,
1154 CSIDL_Type_AllUsers,
1155 CommonMusicW,
1156 MAKEINTRESOURCEW(IDS_COMMON_MUSIC)
1158 { /* 0x36 - CSIDL_COMMON_PICTURES */
1159 &FOLDERID_PublicPictures,
1160 CSIDL_Type_AllUsers,
1161 CommonPicturesW,
1162 MAKEINTRESOURCEW(IDS_COMMON_PICTURES)
1164 { /* 0x37 - CSIDL_COMMON_VIDEO */
1165 &FOLDERID_PublicVideos,
1166 CSIDL_Type_AllUsers,
1167 CommonVideoW,
1168 MAKEINTRESOURCEW(IDS_COMMON_VIDEO)
1170 { /* 0x38 - CSIDL_RESOURCES */
1171 &FOLDERID_ResourceDir,
1172 CSIDL_Type_WindowsPath,
1173 NULL,
1174 ResourcesW
1176 { /* 0x39 - CSIDL_RESOURCES_LOCALIZED */
1177 &FOLDERID_LocalizedResourcesDir,
1178 CSIDL_Type_NonExistent,
1179 NULL,
1180 NULL
1182 { /* 0x3a - CSIDL_COMMON_OEM_LINKS */
1183 &FOLDERID_CommonOEMLinks,
1184 CSIDL_Type_AllUsers,
1185 NULL,
1186 MAKEINTRESOURCEW(IDS_COMMON_OEM_LINKS)
1188 { /* 0x3b - CSIDL_CDBURN_AREA */
1189 &FOLDERID_CDBurning,
1190 CSIDL_Type_User,
1191 CD_BurningW,
1192 MAKEINTRESOURCEW(IDS_CDBURN_AREA)
1194 { /* 0x3c unassigned */
1195 &GUID_NULL,
1196 CSIDL_Type_Disallowed,
1197 NULL,
1198 NULL
1200 { /* 0x3d - CSIDL_COMPUTERSNEARME */
1201 &GUID_NULL,
1202 CSIDL_Type_Disallowed, /* FIXME */
1203 NULL,
1204 NULL
1206 { /* 0x3e - CSIDL_PROFILES */
1207 &GUID_NULL,
1208 CSIDL_Type_Disallowed, /* oddly, this matches WinXP */
1209 NULL,
1210 NULL
1212 { /* 0x3f */
1213 &FOLDERID_AddNewPrograms,
1214 CSIDL_Type_Disallowed,
1215 NULL,
1216 NULL
1218 { /* 0x40 */
1219 &FOLDERID_AppUpdates,
1220 CSIDL_Type_Disallowed,
1221 NULL,
1222 NULL
1224 { /* 0x41 */
1225 &FOLDERID_ChangeRemovePrograms,
1226 CSIDL_Type_Disallowed,
1227 NULL,
1228 NULL
1230 { /* 0x42 */
1231 &FOLDERID_ConflictFolder,
1232 CSIDL_Type_Disallowed,
1233 NULL,
1234 NULL
1236 { /* 0x43 */
1237 &FOLDERID_Contacts,
1238 CSIDL_Type_User,
1239 ContactsW,
1240 MAKEINTRESOURCEW(IDS_CONTACTS)
1242 { /* 0x44 */
1243 &FOLDERID_DeviceMetadataStore,
1244 CSIDL_Type_Disallowed, /* FIXME */
1245 NULL,
1246 NULL
1248 { /* 0x45 */
1249 &GUID_NULL,
1250 CSIDL_Type_User,
1251 NULL,
1252 MAKEINTRESOURCEW(IDS_DOCUMENTS)
1254 { /* 0x46 */
1255 &FOLDERID_DocumentsLibrary,
1256 CSIDL_Type_Disallowed, /* FIXME */
1257 NULL,
1258 NULL
1260 { /* 0x47 */
1261 &FOLDERID_Downloads,
1262 CSIDL_Type_User,
1263 NULL,
1264 MAKEINTRESOURCEW(IDS_DOWNLOADS)
1266 { /* 0x48 */
1267 &FOLDERID_Games,
1268 CSIDL_Type_Disallowed,
1269 NULL,
1270 NULL
1272 { /* 0x49 */
1273 &FOLDERID_GameTasks,
1274 CSIDL_Type_Disallowed, /* FIXME */
1275 NULL,
1276 NULL
1278 { /* 0x4a */
1279 &FOLDERID_HomeGroup,
1280 CSIDL_Type_Disallowed,
1281 NULL,
1282 NULL
1284 { /* 0x4b */
1285 &FOLDERID_ImplicitAppShortcuts,
1286 CSIDL_Type_Disallowed, /* FIXME */
1287 NULL,
1288 NULL
1290 { /* 0x4c */
1291 &FOLDERID_Libraries,
1292 CSIDL_Type_Disallowed, /* FIXME */
1293 NULL,
1294 NULL
1296 { /* 0x4d */
1297 &FOLDERID_Links,
1298 CSIDL_Type_User,
1299 NULL,
1300 MAKEINTRESOURCEW(IDS_LINKS)
1302 { /* 0x4e */
1303 &FOLDERID_LocalAppDataLow,
1304 CSIDL_Type_User,
1305 NULL,
1306 MAKEINTRESOURCEW(IDS_LOCAL_APPDATA_LOW)
1308 { /* 0x4f */
1309 &FOLDERID_MusicLibrary,
1310 CSIDL_Type_Disallowed, /* FIXME */
1311 NULL,
1312 NULL
1314 { /* 0x50 */
1315 &FOLDERID_OriginalImages,
1316 CSIDL_Type_Disallowed, /* FIXME */
1317 NULL,
1318 NULL
1320 { /* 0x51 */
1321 &FOLDERID_PhotoAlbums,
1322 CSIDL_Type_User,
1323 NULL,
1324 MAKEINTRESOURCEW(IDS_PHOTO_ALBUMS)
1326 { /* 0x52 */
1327 &FOLDERID_PicturesLibrary,
1328 CSIDL_Type_Disallowed, /* FIXME */
1329 NULL,
1330 NULL
1332 { /* 0x53 */
1333 &FOLDERID_Playlists,
1334 CSIDL_Type_User,
1335 NULL,
1336 MAKEINTRESOURCEW(IDS_PLAYLISTS)
1338 { /* 0x54 */
1339 &FOLDERID_ProgramFilesX64,
1340 CSIDL_Type_NonExistent,
1341 NULL,
1342 NULL
1344 { /* 0x55 */
1345 &FOLDERID_ProgramFilesCommonX64,
1346 CSIDL_Type_NonExistent,
1347 NULL,
1348 NULL
1350 { /* 0x56 */
1351 &FOLDERID_Public,
1352 CSIDL_Type_CurrVer, /* FIXME */
1353 NULL,
1354 UsersPublicW
1356 { /* 0x57 */
1357 &FOLDERID_PublicDownloads,
1358 CSIDL_Type_AllUsers,
1359 NULL,
1360 MAKEINTRESOURCEW(IDS_PUBLIC_DOWNLOADS)
1362 { /* 0x58 */
1363 &FOLDERID_PublicGameTasks,
1364 CSIDL_Type_AllUsers,
1365 NULL,
1366 MAKEINTRESOURCEW(IDS_PUBLIC_GAME_TASKS)
1368 { /* 0x59 */
1369 &FOLDERID_PublicLibraries,
1370 CSIDL_Type_AllUsers,
1371 NULL,
1372 MAKEINTRESOURCEW(IDS_PUBLIC_LIBRARIES)
1374 { /* 0x5a */
1375 &FOLDERID_PublicRingtones,
1376 CSIDL_Type_AllUsers,
1377 NULL,
1378 MAKEINTRESOURCEW(IDS_PUBLIC_RINGTONES)
1380 { /* 0x5b */
1381 &FOLDERID_QuickLaunch,
1382 CSIDL_Type_Disallowed, /* FIXME */
1383 NULL,
1384 NULL
1386 { /* 0x5c */
1387 &FOLDERID_RecordedTVLibrary,
1388 CSIDL_Type_Disallowed, /* FIXME */
1389 NULL,
1390 NULL
1392 { /* 0x5d */
1393 &FOLDERID_Ringtones,
1394 CSIDL_Type_Disallowed, /* FIXME */
1395 NULL,
1396 NULL
1398 { /* 0x5e */
1399 &FOLDERID_SampleMusic,
1400 CSIDL_Type_AllUsers,
1401 NULL,
1402 MAKEINTRESOURCEW(IDS_SAMPLE_MUSIC)
1404 { /* 0x5f */
1405 &FOLDERID_SamplePictures,
1406 CSIDL_Type_AllUsers,
1407 NULL,
1408 MAKEINTRESOURCEW(IDS_SAMPLE_PICTURES)
1410 { /* 0x60 */
1411 &FOLDERID_SamplePlaylists,
1412 CSIDL_Type_AllUsers,
1413 NULL,
1414 MAKEINTRESOURCEW(IDS_SAMPLE_PLAYLISTS)
1416 { /* 0x61 */
1417 &FOLDERID_SampleVideos,
1418 CSIDL_Type_AllUsers,
1419 NULL,
1420 MAKEINTRESOURCEW(IDS_SAMPLE_VIDEOS)
1422 { /* 0x62 */
1423 &FOLDERID_SavedGames,
1424 CSIDL_Type_User,
1425 NULL,
1426 MAKEINTRESOURCEW(IDS_SAVED_GAMES)
1428 { /* 0x63 */
1429 &FOLDERID_SavedSearches,
1430 CSIDL_Type_User,
1431 NULL,
1432 MAKEINTRESOURCEW(IDS_SAVED_SEARCHES)
1434 { /* 0x64 */
1435 &FOLDERID_SEARCH_CSC,
1436 CSIDL_Type_Disallowed,
1437 NULL,
1438 NULL
1440 { /* 0x65 */
1441 &FOLDERID_SEARCH_MAPI,
1442 CSIDL_Type_Disallowed,
1443 NULL,
1444 NULL
1446 { /* 0x66 */
1447 &FOLDERID_SearchHome,
1448 CSIDL_Type_Disallowed,
1449 NULL,
1450 NULL
1452 { /* 0x67 */
1453 &FOLDERID_SidebarDefaultParts,
1454 CSIDL_Type_Disallowed, /* FIXME */
1455 NULL,
1456 NULL
1458 { /* 0x68 */
1459 &FOLDERID_SidebarParts,
1460 CSIDL_Type_Disallowed, /* FIXME */
1461 NULL,
1462 NULL
1464 { /* 0x69 */
1465 &FOLDERID_SyncManagerFolder,
1466 CSIDL_Type_Disallowed,
1467 NULL,
1468 NULL
1470 { /* 0x6a */
1471 &FOLDERID_SyncResultsFolder,
1472 CSIDL_Type_Disallowed,
1473 NULL,
1474 NULL
1476 { /* 0x6b */
1477 &FOLDERID_SyncSetupFolder,
1478 CSIDL_Type_Disallowed,
1479 NULL,
1480 NULL
1482 { /* 0x6c */
1483 &FOLDERID_UserPinned,
1484 CSIDL_Type_Disallowed, /* FIXME */
1485 NULL,
1486 NULL
1488 { /* 0x6d */
1489 &FOLDERID_UserProfiles,
1490 CSIDL_Type_CurrVer,
1491 UsersW,
1492 MAKEINTRESOURCEW(IDS_USER_PROFILES)
1494 { /* 0x6e */
1495 &FOLDERID_UserProgramFiles,
1496 CSIDL_Type_Disallowed, /* FIXME */
1497 NULL,
1498 NULL
1500 { /* 0x6f */
1501 &FOLDERID_UserProgramFilesCommon,
1502 CSIDL_Type_Disallowed, /* FIXME */
1503 NULL,
1504 NULL
1506 { /* 0x70 */
1507 &FOLDERID_UsersFiles,
1508 CSIDL_Type_Disallowed,
1509 NULL,
1510 NULL
1512 { /* 0x71 */
1513 &FOLDERID_UsersLibraries,
1514 CSIDL_Type_Disallowed,
1515 NULL,
1516 NULL
1518 { /* 0x72 */
1519 &FOLDERID_VideosLibrary,
1520 CSIDL_Type_Disallowed, /* FIXME */
1521 NULL,
1522 NULL
1526 static HRESULT _SHExpandEnvironmentStrings(LPCWSTR szSrc, LPWSTR szDest);
1528 /* Gets the value named value from the registry key
1529 * rootKey\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
1530 * (or from rootKey\userPrefix\... if userPrefix is not NULL) into path, which
1531 * is assumed to be MAX_PATH WCHARs in length.
1532 * If it exists, expands the value and writes the expanded value to
1533 * rootKey\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders
1534 * Returns successful error code if the value was retrieved from the registry,
1535 * and a failure otherwise.
1537 static HRESULT _SHGetUserShellFolderPath(HKEY rootKey, LPCWSTR userPrefix,
1538 LPCWSTR value, LPWSTR path)
1540 HRESULT hr;
1541 WCHAR shellFolderPath[MAX_PATH], userShellFolderPath[MAX_PATH];
1542 LPCWSTR pShellFolderPath, pUserShellFolderPath;
1543 DWORD dwType, dwPathLen = MAX_PATH;
1544 HKEY userShellFolderKey, shellFolderKey;
1546 TRACE("%p,%s,%s,%p\n",rootKey, debugstr_w(userPrefix), debugstr_w(value),
1547 path);
1549 if (userPrefix)
1551 strcpyW(shellFolderPath, userPrefix);
1552 PathAddBackslashW(shellFolderPath);
1553 strcatW(shellFolderPath, szSHFolders);
1554 pShellFolderPath = shellFolderPath;
1555 strcpyW(userShellFolderPath, userPrefix);
1556 PathAddBackslashW(userShellFolderPath);
1557 strcatW(userShellFolderPath, szSHUserFolders);
1558 pUserShellFolderPath = userShellFolderPath;
1560 else
1562 pUserShellFolderPath = szSHUserFolders;
1563 pShellFolderPath = szSHFolders;
1566 if (RegCreateKeyW(rootKey, pShellFolderPath, &shellFolderKey))
1568 TRACE("Failed to create %s\n", debugstr_w(pShellFolderPath));
1569 return E_FAIL;
1571 if (RegCreateKeyW(rootKey, pUserShellFolderPath, &userShellFolderKey))
1573 TRACE("Failed to create %s\n",
1574 debugstr_w(pUserShellFolderPath));
1575 RegCloseKey(shellFolderKey);
1576 return E_FAIL;
1579 if (!RegQueryValueExW(userShellFolderKey, value, NULL, &dwType,
1580 (LPBYTE)path, &dwPathLen) && (dwType == REG_EXPAND_SZ || dwType == REG_SZ))
1582 LONG ret;
1584 path[dwPathLen / sizeof(WCHAR)] = '\0';
1585 if (dwType == REG_EXPAND_SZ && path[0] == '%')
1587 WCHAR szTemp[MAX_PATH];
1589 _SHExpandEnvironmentStrings(path, szTemp);
1590 lstrcpynW(path, szTemp, MAX_PATH);
1592 ret = RegSetValueExW(shellFolderKey, value, 0, REG_SZ, (LPBYTE)path,
1593 (strlenW(path) + 1) * sizeof(WCHAR));
1594 if (ret != ERROR_SUCCESS)
1595 hr = HRESULT_FROM_WIN32(ret);
1596 else
1597 hr = S_OK;
1599 else
1600 hr = E_FAIL;
1601 RegCloseKey(shellFolderKey);
1602 RegCloseKey(userShellFolderKey);
1603 TRACE("returning 0x%08x\n", hr);
1604 return hr;
1607 /* Gets a 'semi-expanded' default value of the CSIDL with index folder into
1608 * pszPath, based on the entries in CSIDL_Data. By semi-expanded, I mean:
1609 * - The entry's szDefaultPath may be either a string value or an integer
1610 * resource identifier. In the latter case, the string value of the resource
1611 * is written.
1612 * - Depending on the entry's type, the path may begin with an (unexpanded)
1613 * environment variable name. The caller is responsible for expanding
1614 * environment strings if so desired.
1615 * The types that are prepended with environment variables are:
1616 * CSIDL_Type_User: %USERPROFILE%
1617 * CSIDL_Type_AllUsers: %ALLUSERSPROFILE%
1618 * CSIDL_Type_CurrVer: %SystemDrive%
1619 * (Others might make sense too, but as yet are unneeded.)
1621 static HRESULT _SHGetDefaultValue(BYTE folder, LPWSTR pszPath)
1623 HRESULT hr;
1624 WCHAR resourcePath[MAX_PATH];
1625 LPCWSTR pDefaultPath = NULL;
1627 TRACE("0x%02x,%p\n", folder, pszPath);
1629 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1630 return E_INVALIDARG;
1631 if (!pszPath)
1632 return E_INVALIDARG;
1634 if (!is_win64)
1636 BOOL is_wow64;
1638 switch (folder)
1640 case CSIDL_PROGRAM_FILES:
1641 case CSIDL_PROGRAM_FILESX86:
1642 IsWow64Process( GetCurrentProcess(), &is_wow64 );
1643 folder = is_wow64 ? CSIDL_PROGRAM_FILESX86 : CSIDL_PROGRAM_FILES;
1644 break;
1645 case CSIDL_PROGRAM_FILES_COMMON:
1646 case CSIDL_PROGRAM_FILES_COMMONX86:
1647 IsWow64Process( GetCurrentProcess(), &is_wow64 );
1648 folder = is_wow64 ? CSIDL_PROGRAM_FILES_COMMONX86 : CSIDL_PROGRAM_FILES_COMMON;
1649 break;
1653 if (CSIDL_Data[folder].szDefaultPath &&
1654 IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath))
1656 if (LoadStringW(shell32_hInstance,
1657 LOWORD(CSIDL_Data[folder].szDefaultPath), resourcePath, MAX_PATH))
1659 hr = S_OK;
1660 pDefaultPath = resourcePath;
1662 else
1664 FIXME("(%d,%s), LoadString failed, missing translation?\n", folder,
1665 debugstr_w(pszPath));
1666 hr = E_FAIL;
1669 else
1671 hr = S_OK;
1672 pDefaultPath = CSIDL_Data[folder].szDefaultPath;
1674 if (SUCCEEDED(hr))
1676 switch (CSIDL_Data[folder].type)
1678 case CSIDL_Type_User:
1679 strcpyW(pszPath, UserProfileW);
1680 break;
1681 case CSIDL_Type_AllUsers:
1682 strcpyW(pszPath, AllUsersProfileW);
1683 break;
1684 case CSIDL_Type_CurrVer:
1685 strcpyW(pszPath, SystemDriveW);
1686 break;
1687 default:
1688 ; /* no corresponding env. var, do nothing */
1690 if (pDefaultPath)
1692 PathAddBackslashW(pszPath);
1693 strcatW(pszPath, pDefaultPath);
1696 TRACE("returning 0x%08x\n", hr);
1697 return hr;
1700 /* Gets the (unexpanded) value of the folder with index folder into pszPath.
1701 * The folder's type is assumed to be CSIDL_Type_CurrVer. Its default value
1702 * can be overridden in the HKLM\\szCurrentVersion key.
1703 * If dwFlags has SHGFP_TYPE_DEFAULT set or if the value isn't overridden in
1704 * the registry, uses _SHGetDefaultValue to get the value.
1706 static HRESULT _SHGetCurrentVersionPath(DWORD dwFlags, BYTE folder,
1707 LPWSTR pszPath)
1709 HRESULT hr;
1711 TRACE("0x%08x,0x%02x,%p\n", dwFlags, folder, pszPath);
1713 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1714 return E_INVALIDARG;
1715 if (CSIDL_Data[folder].type != CSIDL_Type_CurrVer)
1716 return E_INVALIDARG;
1717 if (!pszPath)
1718 return E_INVALIDARG;
1720 if (dwFlags & SHGFP_TYPE_DEFAULT)
1721 hr = _SHGetDefaultValue(folder, pszPath);
1722 else
1724 HKEY hKey;
1726 if (RegCreateKeyW(HKEY_LOCAL_MACHINE, szCurrentVersion, &hKey))
1727 hr = E_FAIL;
1728 else
1730 DWORD dwType, dwPathLen = MAX_PATH * sizeof(WCHAR);
1732 if (RegQueryValueExW(hKey, CSIDL_Data[folder].szValueName, NULL,
1733 &dwType, (LPBYTE)pszPath, &dwPathLen) ||
1734 (dwType != REG_SZ && dwType != REG_EXPAND_SZ))
1736 hr = _SHGetDefaultValue(folder, pszPath);
1737 dwType = REG_EXPAND_SZ;
1738 switch (folder)
1740 case CSIDL_PROGRAM_FILESX86:
1741 case CSIDL_PROGRAM_FILES_COMMONX86:
1742 /* these two should never be set on 32-bit setups */
1743 if (!is_win64)
1745 BOOL is_wow64;
1746 IsWow64Process( GetCurrentProcess(), &is_wow64 );
1747 if (!is_wow64) break;
1749 /* fall through */
1750 default:
1751 RegSetValueExW(hKey, CSIDL_Data[folder].szValueName, 0, dwType,
1752 (LPBYTE)pszPath, (strlenW(pszPath)+1)*sizeof(WCHAR));
1755 else
1757 pszPath[dwPathLen / sizeof(WCHAR)] = '\0';
1758 hr = S_OK;
1760 RegCloseKey(hKey);
1763 TRACE("returning 0x%08x (output path is %s)\n", hr, debugstr_w(pszPath));
1764 return hr;
1767 static LPWSTR _GetUserSidStringFromToken(HANDLE Token)
1769 char InfoBuffer[64];
1770 PTOKEN_USER UserInfo;
1771 DWORD InfoSize;
1772 LPWSTR SidStr;
1774 UserInfo = (PTOKEN_USER) InfoBuffer;
1775 if (! GetTokenInformation(Token, TokenUser, InfoBuffer, sizeof(InfoBuffer),
1776 &InfoSize))
1778 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
1779 return NULL;
1780 UserInfo = HeapAlloc(GetProcessHeap(), 0, InfoSize);
1781 if (UserInfo == NULL)
1782 return NULL;
1783 if (! GetTokenInformation(Token, TokenUser, UserInfo, InfoSize,
1784 &InfoSize))
1786 HeapFree(GetProcessHeap(), 0, UserInfo);
1787 return NULL;
1791 if (! ConvertSidToStringSidW(UserInfo->User.Sid, &SidStr))
1792 SidStr = NULL;
1794 if (UserInfo != (PTOKEN_USER) InfoBuffer)
1795 HeapFree(GetProcessHeap(), 0, UserInfo);
1797 return SidStr;
1800 /* Gets the user's path (unexpanded) for the CSIDL with index folder:
1801 * If SHGFP_TYPE_DEFAULT is set, calls _SHGetDefaultValue for it. Otherwise
1802 * calls _SHGetUserShellFolderPath for it. Where it looks depends on hToken:
1803 * - if hToken is -1, looks in HKEY_USERS\.Default
1804 * - otherwise looks first in HKEY_CURRENT_USER, followed by HKEY_LOCAL_MACHINE
1805 * if HKEY_CURRENT_USER doesn't contain any entries. If both fail, finally
1806 * calls _SHGetDefaultValue for it.
1808 static HRESULT _SHGetUserProfilePath(HANDLE hToken, DWORD dwFlags, BYTE folder,
1809 LPWSTR pszPath)
1811 HRESULT hr;
1813 TRACE("%p,0x%08x,0x%02x,%p\n", hToken, dwFlags, folder, pszPath);
1815 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1816 return E_INVALIDARG;
1817 if (CSIDL_Data[folder].type != CSIDL_Type_User)
1818 return E_INVALIDARG;
1819 if (!pszPath)
1820 return E_INVALIDARG;
1822 if (dwFlags & SHGFP_TYPE_DEFAULT)
1824 if (hToken != NULL && hToken != (HANDLE)-1)
1826 FIXME("unsupported for user other than current or default\n");
1827 return E_FAIL;
1829 hr = _SHGetDefaultValue(folder, pszPath);
1831 else
1833 LPCWSTR userPrefix = NULL;
1834 HKEY hRootKey;
1836 if (hToken == (HANDLE)-1)
1838 hRootKey = HKEY_USERS;
1839 userPrefix = DefaultW;
1841 else if (hToken == NULL)
1842 hRootKey = HKEY_CURRENT_USER;
1843 else
1845 hRootKey = HKEY_USERS;
1846 userPrefix = _GetUserSidStringFromToken(hToken);
1847 if (userPrefix == NULL)
1849 hr = E_FAIL;
1850 goto error;
1853 hr = _SHGetUserShellFolderPath(hRootKey, userPrefix,
1854 CSIDL_Data[folder].szValueName, pszPath);
1855 if (FAILED(hr) && hRootKey != HKEY_LOCAL_MACHINE)
1856 hr = _SHGetUserShellFolderPath(HKEY_LOCAL_MACHINE, NULL,
1857 CSIDL_Data[folder].szValueName, pszPath);
1858 if (FAILED(hr))
1859 hr = _SHGetDefaultValue(folder, pszPath);
1860 if (userPrefix != NULL && userPrefix != DefaultW)
1861 LocalFree((HLOCAL) userPrefix);
1863 error:
1864 TRACE("returning 0x%08x (output path is %s)\n", hr, debugstr_w(pszPath));
1865 return hr;
1868 /* Gets the (unexpanded) path for the CSIDL with index folder. If dwFlags has
1869 * SHGFP_TYPE_DEFAULT set, calls _SHGetDefaultValue. Otherwise calls
1870 * _SHGetUserShellFolderPath for it, looking only in HKEY_LOCAL_MACHINE.
1871 * If this fails, falls back to _SHGetDefaultValue.
1873 static HRESULT _SHGetAllUsersProfilePath(DWORD dwFlags, BYTE folder,
1874 LPWSTR pszPath)
1876 HRESULT hr;
1878 TRACE("0x%08x,0x%02x,%p\n", dwFlags, folder, pszPath);
1880 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1881 return E_INVALIDARG;
1882 if (CSIDL_Data[folder].type != CSIDL_Type_AllUsers)
1883 return E_INVALIDARG;
1884 if (!pszPath)
1885 return E_INVALIDARG;
1887 if (dwFlags & SHGFP_TYPE_DEFAULT)
1888 hr = _SHGetDefaultValue(folder, pszPath);
1889 else
1891 hr = _SHGetUserShellFolderPath(HKEY_LOCAL_MACHINE, NULL,
1892 CSIDL_Data[folder].szValueName, pszPath);
1893 if (FAILED(hr))
1894 hr = _SHGetDefaultValue(folder, pszPath);
1896 TRACE("returning 0x%08x (output path is %s)\n", hr, debugstr_w(pszPath));
1897 return hr;
1900 static HRESULT _SHOpenProfilesKey(PHKEY pKey)
1902 LONG lRet;
1903 DWORD disp;
1905 lRet = RegCreateKeyExW(HKEY_LOCAL_MACHINE, ProfileListW, 0, NULL, 0,
1906 KEY_ALL_ACCESS, NULL, pKey, &disp);
1907 return HRESULT_FROM_WIN32(lRet);
1910 /* Reads the value named szValueName from the key profilesKey (assumed to be
1911 * opened by _SHOpenProfilesKey) into szValue, which is assumed to be MAX_PATH
1912 * WCHARs in length. If it doesn't exist, returns szDefault (and saves
1913 * szDefault to the registry).
1915 static HRESULT _SHGetProfilesValue(HKEY profilesKey, LPCWSTR szValueName,
1916 LPWSTR szValue, LPCWSTR szDefault)
1918 HRESULT hr;
1919 DWORD type, dwPathLen = MAX_PATH * sizeof(WCHAR);
1920 LONG lRet;
1922 TRACE("%p,%s,%p,%s\n", profilesKey, debugstr_w(szValueName), szValue,
1923 debugstr_w(szDefault));
1924 lRet = RegQueryValueExW(profilesKey, szValueName, NULL, &type,
1925 (LPBYTE)szValue, &dwPathLen);
1926 if (!lRet && (type == REG_SZ || type == REG_EXPAND_SZ) && dwPathLen
1927 && *szValue)
1929 dwPathLen /= sizeof(WCHAR);
1930 szValue[dwPathLen] = '\0';
1931 hr = S_OK;
1933 else
1935 /* Missing or invalid value, set a default */
1936 lstrcpynW(szValue, szDefault, MAX_PATH);
1937 TRACE("Setting missing value %s to %s\n", debugstr_w(szValueName),
1938 debugstr_w(szValue));
1939 lRet = RegSetValueExW(profilesKey, szValueName, 0, REG_EXPAND_SZ,
1940 (LPBYTE)szValue,
1941 (strlenW(szValue) + 1) * sizeof(WCHAR));
1942 if (lRet)
1943 hr = HRESULT_FROM_WIN32(lRet);
1944 else
1945 hr = S_OK;
1947 TRACE("returning 0x%08x (output value is %s)\n", hr, debugstr_w(szValue));
1948 return hr;
1951 /* Attempts to expand environment variables from szSrc into szDest, which is
1952 * assumed to be MAX_PATH characters in length. Before referring to the
1953 * environment, handles a few variables directly, because the environment
1954 * variables may not be set when this is called (as during Wine's installation
1955 * when default values are being written to the registry).
1956 * The directly handled environment variables, and their source, are:
1957 * - ALLUSERSPROFILE, USERPROFILE: reads from the registry
1958 * - SystemDrive: uses GetSystemDirectoryW and uses the drive portion of its
1959 * path
1960 * If one of the directly handled environment variables is expanded, only
1961 * expands a single variable, and only in the beginning of szSrc.
1963 static HRESULT _SHExpandEnvironmentStrings(LPCWSTR szSrc, LPWSTR szDest)
1965 HRESULT hr;
1966 WCHAR szTemp[MAX_PATH], szProfilesPrefix[MAX_PATH] = { 0 };
1967 HKEY key = NULL;
1969 TRACE("%s, %p\n", debugstr_w(szSrc), szDest);
1971 if (!szSrc || !szDest) return E_INVALIDARG;
1973 /* short-circuit if there's nothing to expand */
1974 if (szSrc[0] != '%')
1976 strcpyW(szDest, szSrc);
1977 hr = S_OK;
1978 goto end;
1980 /* Get the profile prefix, we'll probably be needing it */
1981 hr = _SHOpenProfilesKey(&key);
1982 if (SUCCEEDED(hr))
1984 WCHAR def_val[MAX_PATH];
1986 /* get the system drive */
1987 GetSystemDirectoryW(def_val, MAX_PATH);
1988 if (def_val[1] == ':') strcpyW( def_val + 3, szDefaultProfileDirW );
1989 else FIXME("non-drive system paths unsupported\n");
1991 hr = _SHGetProfilesValue(key, ProfilesDirectoryW, szProfilesPrefix, def_val );
1994 *szDest = 0;
1995 strcpyW(szTemp, szSrc);
1996 while (SUCCEEDED(hr) && szTemp[0] == '%')
1998 if (!strncmpiW(szTemp, AllUsersProfileW, strlenW(AllUsersProfileW)))
2000 WCHAR szAllUsers[MAX_PATH];
2002 strcpyW(szDest, szProfilesPrefix);
2003 hr = _SHGetProfilesValue(key, AllUsersProfileValueW,
2004 szAllUsers, AllUsersW);
2005 PathAppendW(szDest, szAllUsers);
2006 PathAppendW(szDest, szTemp + strlenW(AllUsersProfileW));
2008 else if (!strncmpiW(szTemp, UserProfileW, strlenW(UserProfileW)))
2010 WCHAR userName[MAX_PATH];
2011 DWORD userLen = MAX_PATH;
2013 strcpyW(szDest, szProfilesPrefix);
2014 GetUserNameW(userName, &userLen);
2015 PathAppendW(szDest, userName);
2016 PathAppendW(szDest, szTemp + strlenW(UserProfileW));
2018 else if (!strncmpiW(szTemp, SystemDriveW, strlenW(SystemDriveW)))
2020 GetSystemDirectoryW(szDest, MAX_PATH);
2021 if (szDest[1] != ':')
2023 FIXME("non-drive system paths unsupported\n");
2024 hr = E_FAIL;
2026 else
2028 strcpyW(szDest + 3, szTemp + strlenW(SystemDriveW) + 1);
2029 hr = S_OK;
2032 else
2034 DWORD ret = ExpandEnvironmentStringsW(szSrc, szDest, MAX_PATH);
2036 if (ret > MAX_PATH)
2037 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
2038 else if (ret == 0)
2039 hr = HRESULT_FROM_WIN32(GetLastError());
2040 else
2041 hr = S_OK;
2043 if (SUCCEEDED(hr) && szDest[0] == '%')
2044 strcpyW(szTemp, szDest);
2045 else
2047 /* terminate loop */
2048 szTemp[0] = '\0';
2051 end:
2052 if (key)
2053 RegCloseKey(key);
2054 TRACE("returning 0x%08x (input was %s, output is %s)\n", hr,
2055 debugstr_w(szSrc), debugstr_w(szDest));
2056 return hr;
2059 /*************************************************************************
2060 * SHGetFolderPathW [SHELL32.@]
2062 * Convert nFolder to path.
2064 * RETURNS
2065 * Success: S_OK
2066 * Failure: standard HRESULT error codes.
2068 * NOTES
2069 * Most values can be overridden in either
2070 * HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
2071 * or in the same location in HKLM.
2072 * The "Shell Folders" registry key was used in NT4 and earlier systems.
2073 * Beginning with Windows 2000, the "User Shell Folders" key is used, so
2074 * changes made to it are made to the former key too. This synchronization is
2075 * done on-demand: not until someone requests the value of one of these paths
2076 * (by calling one of the SHGet functions) is the value synchronized.
2077 * Furthermore, the HKCU paths take precedence over the HKLM paths.
2079 HRESULT WINAPI SHGetFolderPathW(
2080 HWND hwndOwner, /* [I] owner window */
2081 int nFolder, /* [I] CSIDL identifying the folder */
2082 HANDLE hToken, /* [I] access token */
2083 DWORD dwFlags, /* [I] which path to return */
2084 LPWSTR pszPath) /* [O] converted path */
2086 HRESULT hr = SHGetFolderPathAndSubDirW(hwndOwner, nFolder, hToken, dwFlags, NULL, pszPath);
2087 if(HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND) == hr)
2088 hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
2089 return hr;
2092 HRESULT WINAPI SHGetFolderPathAndSubDirA(
2093 HWND hwndOwner, /* [I] owner window */
2094 int nFolder, /* [I] CSIDL identifying the folder */
2095 HANDLE hToken, /* [I] access token */
2096 DWORD dwFlags, /* [I] which path to return */
2097 LPCSTR pszSubPath, /* [I] sub directory of the specified folder */
2098 LPSTR pszPath) /* [O] converted path */
2100 int length;
2101 HRESULT hr = S_OK;
2102 LPWSTR pszSubPathW = NULL;
2103 LPWSTR pszPathW = NULL;
2104 TRACE("%08x,%08x,%s\n",nFolder, dwFlags, debugstr_w(pszSubPathW));
2106 if(pszPath) {
2107 pszPathW = HeapAlloc(GetProcessHeap(), 0, MAX_PATH * sizeof(WCHAR));
2108 if(!pszPathW) {
2109 hr = HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
2110 goto cleanup;
2113 TRACE("%08x,%08x,%s\n",nFolder, dwFlags, debugstr_w(pszSubPathW));
2115 /* SHGetFolderPathAndSubDirW does not distinguish if pszSubPath isn't
2116 * set (null), or an empty string.therefore call it without the parameter set
2117 * if pszSubPath is an empty string
2119 if (pszSubPath && pszSubPath[0]) {
2120 length = MultiByteToWideChar(CP_ACP, 0, pszSubPath, -1, NULL, 0);
2121 pszSubPathW = HeapAlloc(GetProcessHeap(), 0, length * sizeof(WCHAR));
2122 if(!pszSubPathW) {
2123 hr = HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
2124 goto cleanup;
2126 MultiByteToWideChar(CP_ACP, 0, pszSubPath, -1, pszSubPathW, length);
2129 hr = SHGetFolderPathAndSubDirW(hwndOwner, nFolder, hToken, dwFlags, pszSubPathW, pszPathW);
2131 if (SUCCEEDED(hr) && pszPath)
2132 WideCharToMultiByte(CP_ACP, 0, pszPathW, -1, pszPath, MAX_PATH, NULL, NULL);
2134 cleanup:
2135 HeapFree(GetProcessHeap(), 0, pszPathW);
2136 HeapFree(GetProcessHeap(), 0, pszSubPathW);
2137 return hr;
2140 /*************************************************************************
2141 * SHGetFolderPathAndSubDirW [SHELL32.@]
2143 HRESULT WINAPI SHGetFolderPathAndSubDirW(
2144 HWND hwndOwner, /* [I] owner window */
2145 int nFolder, /* [I] CSIDL identifying the folder */
2146 HANDLE hToken, /* [I] access token */
2147 DWORD dwFlags, /* [I] which path to return */
2148 LPCWSTR pszSubPath,/* [I] sub directory of the specified folder */
2149 LPWSTR pszPath) /* [O] converted path */
2151 HRESULT hr;
2152 WCHAR szBuildPath[MAX_PATH], szTemp[MAX_PATH];
2153 DWORD folder = nFolder & CSIDL_FOLDER_MASK;
2154 CSIDL_Type type;
2155 int ret;
2157 TRACE("%p,%p,nFolder=0x%04x,%s\n", hwndOwner,pszPath,nFolder,debugstr_w(pszSubPath));
2159 /* Windows always NULL-terminates the resulting path regardless of success
2160 * or failure, so do so first
2162 if (pszPath)
2163 *pszPath = '\0';
2165 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
2166 return E_INVALIDARG;
2167 if ((SHGFP_TYPE_CURRENT != dwFlags) && (SHGFP_TYPE_DEFAULT != dwFlags))
2168 return E_INVALIDARG;
2169 szTemp[0] = 0;
2170 type = CSIDL_Data[folder].type;
2171 switch (type)
2173 case CSIDL_Type_Disallowed:
2174 hr = E_INVALIDARG;
2175 break;
2176 case CSIDL_Type_NonExistent:
2177 hr = S_FALSE;
2178 break;
2179 case CSIDL_Type_WindowsPath:
2180 GetWindowsDirectoryW(szTemp, MAX_PATH);
2181 if (CSIDL_Data[folder].szDefaultPath &&
2182 !IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath) &&
2183 *CSIDL_Data[folder].szDefaultPath)
2185 PathAddBackslashW(szTemp);
2186 strcatW(szTemp, CSIDL_Data[folder].szDefaultPath);
2188 hr = S_OK;
2189 break;
2190 case CSIDL_Type_SystemPath:
2191 GetSystemDirectoryW(szTemp, MAX_PATH);
2192 if (CSIDL_Data[folder].szDefaultPath &&
2193 !IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath) &&
2194 *CSIDL_Data[folder].szDefaultPath)
2196 PathAddBackslashW(szTemp);
2197 strcatW(szTemp, CSIDL_Data[folder].szDefaultPath);
2199 hr = S_OK;
2200 break;
2201 case CSIDL_Type_SystemX86Path:
2202 if (!GetSystemWow64DirectoryW(szTemp, MAX_PATH)) GetSystemDirectoryW(szTemp, MAX_PATH);
2203 if (CSIDL_Data[folder].szDefaultPath &&
2204 !IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath) &&
2205 *CSIDL_Data[folder].szDefaultPath)
2207 PathAddBackslashW(szTemp);
2208 strcatW(szTemp, CSIDL_Data[folder].szDefaultPath);
2210 hr = S_OK;
2211 break;
2212 case CSIDL_Type_CurrVer:
2213 hr = _SHGetCurrentVersionPath(dwFlags, folder, szTemp);
2214 break;
2215 case CSIDL_Type_User:
2216 hr = _SHGetUserProfilePath(hToken, dwFlags, folder, szTemp);
2217 break;
2218 case CSIDL_Type_AllUsers:
2219 hr = _SHGetAllUsersProfilePath(dwFlags, folder, szTemp);
2220 break;
2221 default:
2222 FIXME("bogus type %d, please fix\n", type);
2223 hr = E_INVALIDARG;
2224 break;
2227 /* Expand environment strings if necessary */
2228 if (*szTemp == '%')
2229 hr = _SHExpandEnvironmentStrings(szTemp, szBuildPath);
2230 else
2231 strcpyW(szBuildPath, szTemp);
2233 if (FAILED(hr)) goto end;
2235 if(pszSubPath) {
2236 /* make sure the new path does not exceed th bufferlength
2237 * rememebr to backslash and the termination */
2238 if(MAX_PATH < (lstrlenW(szBuildPath) + lstrlenW(pszSubPath) + 2)) {
2239 hr = HRESULT_FROM_WIN32(ERROR_FILENAME_EXCED_RANGE);
2240 goto end;
2242 PathAppendW(szBuildPath, pszSubPath);
2243 PathRemoveBackslashW(szBuildPath);
2245 /* Copy the path if it's available before we might return */
2246 if (SUCCEEDED(hr) && pszPath)
2247 strcpyW(pszPath, szBuildPath);
2249 /* if we don't care about existing directories we are ready */
2250 if(nFolder & CSIDL_FLAG_DONT_VERIFY) goto end;
2252 if (PathFileExistsW(szBuildPath)) goto end;
2254 /* not existing but we are not allowed to create it. The return value
2255 * is verified against shell32 version 6.0.
2257 if (!(nFolder & CSIDL_FLAG_CREATE))
2259 hr = HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND);
2260 goto end;
2263 /* create directory/directories */
2264 ret = SHCreateDirectoryExW(hwndOwner, szBuildPath, NULL);
2265 if (ret && ret != ERROR_ALREADY_EXISTS)
2267 ERR("Failed to create directory %s.\n", debugstr_w(szBuildPath));
2268 hr = E_FAIL;
2269 goto end;
2272 TRACE("Created missing system directory %s\n", debugstr_w(szBuildPath));
2273 end:
2274 TRACE("returning 0x%08x (final path is %s)\n", hr, debugstr_w(szBuildPath));
2275 return hr;
2278 /*************************************************************************
2279 * SHGetFolderPathA [SHELL32.@]
2281 * See SHGetFolderPathW.
2283 HRESULT WINAPI SHGetFolderPathA(
2284 HWND hwndOwner,
2285 int nFolder,
2286 HANDLE hToken,
2287 DWORD dwFlags,
2288 LPSTR pszPath)
2290 WCHAR szTemp[MAX_PATH];
2291 HRESULT hr;
2293 TRACE("%p,%p,nFolder=0x%04x\n",hwndOwner,pszPath,nFolder);
2295 if (pszPath)
2296 *pszPath = '\0';
2297 hr = SHGetFolderPathW(hwndOwner, nFolder, hToken, dwFlags, szTemp);
2298 if (SUCCEEDED(hr) && pszPath)
2299 WideCharToMultiByte(CP_ACP, 0, szTemp, -1, pszPath, MAX_PATH, NULL,
2300 NULL);
2302 return hr;
2305 /* For each folder in folders, if its value has not been set in the registry,
2306 * calls _SHGetUserProfilePath or _SHGetAllUsersProfilePath (depending on the
2307 * folder's type) to get the unexpanded value first.
2308 * Writes the unexpanded value to User Shell Folders, and queries it with
2309 * SHGetFolderPathW to force the creation of the directory if it doesn't
2310 * already exist. SHGetFolderPathW also returns the expanded value, which
2311 * this then writes to Shell Folders.
2313 static HRESULT _SHRegisterFolders(HKEY hRootKey, HANDLE hToken,
2314 LPCWSTR szUserShellFolderPath, LPCWSTR szShellFolderPath, const UINT folders[],
2315 UINT foldersLen)
2317 UINT i;
2318 WCHAR path[MAX_PATH];
2319 HRESULT hr = S_OK;
2320 HKEY hUserKey = NULL, hKey = NULL;
2321 DWORD dwType, dwPathLen;
2322 LONG ret;
2324 TRACE("%p,%p,%s,%p,%u\n", hRootKey, hToken,
2325 debugstr_w(szUserShellFolderPath), folders, foldersLen);
2327 ret = RegCreateKeyW(hRootKey, szUserShellFolderPath, &hUserKey);
2328 if (ret)
2329 hr = HRESULT_FROM_WIN32(ret);
2330 else
2332 ret = RegCreateKeyW(hRootKey, szShellFolderPath, &hKey);
2333 if (ret)
2334 hr = HRESULT_FROM_WIN32(ret);
2336 for (i = 0; SUCCEEDED(hr) && i < foldersLen; i++)
2338 dwPathLen = MAX_PATH * sizeof(WCHAR);
2339 if (RegQueryValueExW(hUserKey, CSIDL_Data[folders[i]].szValueName, NULL,
2340 &dwType, (LPBYTE)path, &dwPathLen) || (dwType != REG_SZ &&
2341 dwType != REG_EXPAND_SZ))
2343 *path = '\0';
2344 if (CSIDL_Data[folders[i]].type == CSIDL_Type_User)
2345 _SHGetUserProfilePath(hToken, SHGFP_TYPE_DEFAULT, folders[i],
2346 path);
2347 else if (CSIDL_Data[folders[i]].type == CSIDL_Type_AllUsers)
2348 _SHGetAllUsersProfilePath(SHGFP_TYPE_DEFAULT, folders[i], path);
2349 else if (CSIDL_Data[folders[i]].type == CSIDL_Type_WindowsPath)
2351 GetWindowsDirectoryW(path, MAX_PATH);
2352 if (CSIDL_Data[folders[i]].szDefaultPath &&
2353 !IS_INTRESOURCE(CSIDL_Data[folders[i]].szDefaultPath))
2355 PathAddBackslashW(path);
2356 strcatW(path, CSIDL_Data[folders[i]].szDefaultPath);
2359 else
2360 hr = E_FAIL;
2361 if (*path)
2363 ret = RegSetValueExW(hUserKey,
2364 CSIDL_Data[folders[i]].szValueName, 0, REG_EXPAND_SZ,
2365 (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR));
2366 if (ret)
2367 hr = HRESULT_FROM_WIN32(ret);
2368 else
2370 hr = SHGetFolderPathW(NULL, folders[i] | CSIDL_FLAG_CREATE,
2371 hToken, SHGFP_TYPE_DEFAULT, path);
2372 ret = RegSetValueExW(hKey,
2373 CSIDL_Data[folders[i]].szValueName, 0, REG_SZ,
2374 (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR));
2375 if (ret)
2376 hr = HRESULT_FROM_WIN32(ret);
2381 if (hUserKey)
2382 RegCloseKey(hUserKey);
2383 if (hKey)
2384 RegCloseKey(hKey);
2386 TRACE("returning 0x%08x\n", hr);
2387 return hr;
2390 static HRESULT _SHRegisterUserShellFolders(BOOL bDefault)
2392 static const UINT folders[] = {
2393 CSIDL_PROGRAMS,
2394 CSIDL_PERSONAL,
2395 CSIDL_FAVORITES,
2396 CSIDL_APPDATA,
2397 CSIDL_STARTUP,
2398 CSIDL_RECENT,
2399 CSIDL_SENDTO,
2400 CSIDL_STARTMENU,
2401 CSIDL_MYMUSIC,
2402 CSIDL_MYVIDEO,
2403 CSIDL_DESKTOPDIRECTORY,
2404 CSIDL_NETHOOD,
2405 CSIDL_TEMPLATES,
2406 CSIDL_PRINTHOOD,
2407 CSIDL_LOCAL_APPDATA,
2408 CSIDL_INTERNET_CACHE,
2409 CSIDL_COOKIES,
2410 CSIDL_HISTORY,
2411 CSIDL_MYPICTURES,
2412 CSIDL_FONTS
2414 WCHAR userShellFolderPath[MAX_PATH], shellFolderPath[MAX_PATH];
2415 LPCWSTR pUserShellFolderPath, pShellFolderPath;
2416 HRESULT hr = S_OK;
2417 HKEY hRootKey;
2418 HANDLE hToken;
2420 TRACE("%s\n", bDefault ? "TRUE" : "FALSE");
2421 if (bDefault)
2423 hToken = (HANDLE)-1;
2424 hRootKey = HKEY_USERS;
2425 strcpyW(userShellFolderPath, DefaultW);
2426 PathAddBackslashW(userShellFolderPath);
2427 strcatW(userShellFolderPath, szSHUserFolders);
2428 pUserShellFolderPath = userShellFolderPath;
2429 strcpyW(shellFolderPath, DefaultW);
2430 PathAddBackslashW(shellFolderPath);
2431 strcatW(shellFolderPath, szSHFolders);
2432 pShellFolderPath = shellFolderPath;
2434 else
2436 hToken = NULL;
2437 hRootKey = HKEY_CURRENT_USER;
2438 pUserShellFolderPath = szSHUserFolders;
2439 pShellFolderPath = szSHFolders;
2442 hr = _SHRegisterFolders(hRootKey, hToken, pUserShellFolderPath,
2443 pShellFolderPath, folders, sizeof(folders) / sizeof(folders[0]));
2444 TRACE("returning 0x%08x\n", hr);
2445 return hr;
2448 static HRESULT _SHRegisterCommonShellFolders(void)
2450 static const UINT folders[] = {
2451 CSIDL_COMMON_STARTMENU,
2452 CSIDL_COMMON_PROGRAMS,
2453 CSIDL_COMMON_STARTUP,
2454 CSIDL_COMMON_DESKTOPDIRECTORY,
2455 CSIDL_COMMON_FAVORITES,
2456 CSIDL_COMMON_APPDATA,
2457 CSIDL_COMMON_TEMPLATES,
2458 CSIDL_COMMON_DOCUMENTS,
2459 CSIDL_COMMON_ADMINTOOLS,
2460 CSIDL_COMMON_MUSIC,
2461 CSIDL_COMMON_PICTURES,
2462 CSIDL_COMMON_VIDEO,
2464 HRESULT hr;
2466 TRACE("\n");
2467 hr = _SHRegisterFolders(HKEY_LOCAL_MACHINE, NULL, szSHUserFolders,
2468 szSHFolders, folders, sizeof(folders) / sizeof(folders[0]));
2469 TRACE("returning 0x%08x\n", hr);
2470 return hr;
2473 /******************************************************************************
2474 * _SHAppendToUnixPath [Internal]
2476 * Helper function for _SHCreateSymbolicLinks. Appends pwszSubPath (or the
2477 * corresponding resource, if IS_INTRESOURCE) to the unix base path 'szBasePath'
2478 * and replaces backslashes with slashes.
2480 * PARAMS
2481 * szBasePath [IO] The unix base path, which will be appended to (CP_UNXICP).
2482 * pwszSubPath [I] Sub-path or resource id (use MAKEINTRESOURCEW).
2484 * RETURNS
2485 * Success: TRUE,
2486 * Failure: FALSE
2488 static inline BOOL _SHAppendToUnixPath(char *szBasePath, LPCWSTR pwszSubPath) {
2489 WCHAR wszSubPath[MAX_PATH];
2490 int cLen = strlen(szBasePath);
2491 char *pBackslash;
2493 if (IS_INTRESOURCE(pwszSubPath)) {
2494 if (!LoadStringW(shell32_hInstance, LOWORD(pwszSubPath), wszSubPath, MAX_PATH)) {
2495 /* Fall back to hard coded defaults. */
2496 switch (LOWORD(pwszSubPath)) {
2497 case IDS_PERSONAL:
2498 lstrcpyW(wszSubPath, PersonalW);
2499 break;
2500 case IDS_MYMUSIC:
2501 lstrcpyW(wszSubPath, My_MusicW);
2502 break;
2503 case IDS_MYPICTURES:
2504 lstrcpyW(wszSubPath, My_PicturesW);
2505 break;
2506 case IDS_MYVIDEO:
2507 lstrcpyW(wszSubPath, My_VideoW);
2508 break;
2509 default:
2510 ERR("LoadString(%d) failed!\n", LOWORD(pwszSubPath));
2511 return FALSE;
2514 } else {
2515 lstrcpyW(wszSubPath, pwszSubPath);
2518 if (szBasePath[cLen-1] != '/') szBasePath[cLen++] = '/';
2520 if (!WideCharToMultiByte(CP_UNIXCP, 0, wszSubPath, -1, szBasePath + cLen,
2521 FILENAME_MAX - cLen, NULL, NULL))
2523 return FALSE;
2526 pBackslash = szBasePath + cLen;
2527 while ((pBackslash = strchr(pBackslash, '\\'))) *pBackslash = '/';
2529 return TRUE;
2532 /******************************************************************************
2533 * _SHCreateSymbolicLinks [Internal]
2535 * Sets up symbol links for various shell folders to point into the users home
2536 * directory. We do an educated guess about what the user would probably want:
2537 * - If there is a 'My Documents' directory in $HOME, the user probably wants
2538 * wine's 'My Documents' to point there. Furthermore, we imply that the user
2539 * is a Windows lover and has no problem with wine creating 'My Pictures',
2540 * 'My Music' and 'My Video' subfolders under '$HOME/My Documents', if those
2541 * do not already exits. We put appropriate symbolic links in place for those,
2542 * too.
2543 * - If there is no 'My Documents' directory in $HOME, we let 'My Documents'
2544 * point directly to $HOME. We assume the user to be a unix hacker who does not
2545 * want wine to create anything anywhere besides the .wine directory. So, if
2546 * there already is a 'My Music' directory in $HOME, we symlink the 'My Music'
2547 * shell folder to it. But if not, then we check XDG_MUSIC_DIR - "well known"
2548 * directory, and try to link to that. If that fails, then we symlink to
2549 * $HOME directly. The same holds fo 'My Pictures' and 'My Video'.
2550 * - The Desktop shell folder is symlinked to XDG_DESKTOP_DIR. If that does not
2551 * exist, then we try '$HOME/Desktop'. If that does not exist, then we leave
2552 * it alone.
2553 * ('My Music',... above in fact means LoadString(IDS_MYMUSIC))
2555 static void _SHCreateSymbolicLinks(void)
2557 UINT aidsMyStuff[] = { IDS_MYPICTURES, IDS_MYVIDEO, IDS_MYMUSIC }, i;
2558 int acsidlMyStuff[] = { CSIDL_MYPICTURES, CSIDL_MYVIDEO, CSIDL_MYMUSIC };
2559 static const char * const xdg_dirs[] = { "PICTURES", "VIDEOS", "MUSIC", "DESKTOP" };
2560 static const unsigned int num = sizeof(xdg_dirs) / sizeof(xdg_dirs[0]);
2561 WCHAR wszTempPath[MAX_PATH];
2562 char szPersonalTarget[FILENAME_MAX], *pszPersonal;
2563 char szMyStuffTarget[FILENAME_MAX], *pszMyStuff;
2564 char szDesktopTarget[FILENAME_MAX], *pszDesktop;
2565 struct stat statFolder;
2566 const char *pszHome;
2567 HRESULT hr;
2568 char ** xdg_results;
2569 char * xdg_desktop_dir;
2571 /* Create all necessary profile sub-dirs up to 'My Documents' and get the unix path. */
2572 hr = SHGetFolderPathW(NULL, CSIDL_PERSONAL|CSIDL_FLAG_CREATE, NULL,
2573 SHGFP_TYPE_DEFAULT, wszTempPath);
2574 if (FAILED(hr)) return;
2575 pszPersonal = wine_get_unix_file_name(wszTempPath);
2576 if (!pszPersonal) return;
2578 hr = XDG_UserDirLookup(xdg_dirs, num, &xdg_results);
2579 if (FAILED(hr)) xdg_results = NULL;
2581 pszHome = getenv("HOME");
2582 if (pszHome && !stat(pszHome, &statFolder) && S_ISDIR(statFolder.st_mode)) {
2583 strcpy(szPersonalTarget, pszHome);
2584 if (_SHAppendToUnixPath(szPersonalTarget, MAKEINTRESOURCEW(IDS_PERSONAL)) &&
2585 !stat(szPersonalTarget, &statFolder) && S_ISDIR(statFolder.st_mode))
2587 /* '$HOME/My Documents' exists. Create 'My Pictures', 'My Videos' and
2588 * 'My Music' subfolders or fail silently if they already exist. */
2589 for (i = 0; i < sizeof(aidsMyStuff)/sizeof(aidsMyStuff[0]); i++) {
2590 strcpy(szMyStuffTarget, szPersonalTarget);
2591 if (_SHAppendToUnixPath(szMyStuffTarget, MAKEINTRESOURCEW(aidsMyStuff[i])))
2592 mkdir(szMyStuffTarget, 0777);
2595 else
2597 /* '$HOME/My Documents' doesn't exists, but '$HOME' does. */
2598 strcpy(szPersonalTarget, pszHome);
2601 /* Replace 'My Documents' directory with a symlink of fail silently if not empty. */
2602 rmdir(pszPersonal);
2603 symlink(szPersonalTarget, pszPersonal);
2605 else
2607 /* '$HOME' doesn't exist. Create 'My Pictures', 'My Videos' and 'My Music' subdirs
2608 * in '%USERPROFILE%\\My Documents' or fail silently if they already exist. */
2609 strcpy(szPersonalTarget, pszPersonal);
2610 for (i = 0; i < sizeof(aidsMyStuff)/sizeof(aidsMyStuff[0]); i++) {
2611 strcpy(szMyStuffTarget, szPersonalTarget);
2612 if (_SHAppendToUnixPath(szMyStuffTarget, MAKEINTRESOURCEW(aidsMyStuff[i])))
2613 mkdir(szMyStuffTarget, 0777);
2617 /* Create symbolic links for 'My Pictures', 'My Video' and 'My Music'. */
2618 for (i=0; i < sizeof(aidsMyStuff)/sizeof(aidsMyStuff[0]); i++) {
2619 /* Create the current 'My Whatever' folder and get it's unix path. */
2620 hr = SHGetFolderPathW(NULL, acsidlMyStuff[i]|CSIDL_FLAG_CREATE, NULL,
2621 SHGFP_TYPE_DEFAULT, wszTempPath);
2622 if (FAILED(hr)) continue;
2623 pszMyStuff = wine_get_unix_file_name(wszTempPath);
2624 if (!pszMyStuff) continue;
2626 strcpy(szMyStuffTarget, szPersonalTarget);
2627 if (_SHAppendToUnixPath(szMyStuffTarget, MAKEINTRESOURCEW(aidsMyStuff[i])) &&
2628 !stat(szMyStuffTarget, &statFolder) && S_ISDIR(statFolder.st_mode))
2630 /* If there's a 'My Whatever' directory where 'My Documents' links to, link to it. */
2631 rmdir(pszMyStuff);
2632 symlink(szMyStuffTarget, pszMyStuff);
2634 else
2636 rmdir(pszMyStuff);
2637 if (xdg_results && xdg_results[i])
2639 /* the folder specified by XDG_XXX_DIR exists, link to it. */
2640 symlink(xdg_results[i], pszMyStuff);
2642 else
2644 /* Else link to where 'My Documents' itself links to. */
2645 symlink(szPersonalTarget, pszMyStuff);
2648 HeapFree(GetProcessHeap(), 0, pszMyStuff);
2651 /* Last but not least, the Desktop folder */
2652 if (pszHome)
2653 strcpy(szDesktopTarget, pszHome);
2654 else
2655 strcpy(szDesktopTarget, pszPersonal);
2656 HeapFree(GetProcessHeap(), 0, pszPersonal);
2658 xdg_desktop_dir = xdg_results ? xdg_results[num - 1] : NULL;
2659 if (xdg_desktop_dir ||
2660 (_SHAppendToUnixPath(szDesktopTarget, DesktopW) &&
2661 !stat(szDesktopTarget, &statFolder) && S_ISDIR(statFolder.st_mode)))
2663 hr = SHGetFolderPathW(NULL, CSIDL_DESKTOPDIRECTORY|CSIDL_FLAG_CREATE, NULL,
2664 SHGFP_TYPE_DEFAULT, wszTempPath);
2665 if (SUCCEEDED(hr) && (pszDesktop = wine_get_unix_file_name(wszTempPath)))
2667 rmdir(pszDesktop);
2668 if (xdg_desktop_dir)
2669 symlink(xdg_desktop_dir, pszDesktop);
2670 else
2671 symlink(szDesktopTarget, pszDesktop);
2672 HeapFree(GetProcessHeap(), 0, pszDesktop);
2676 /* Free resources allocated by XDG_UserDirLookup() */
2677 if (xdg_results)
2679 for (i = 0; i < num; i++)
2680 HeapFree(GetProcessHeap(), 0, xdg_results[i]);
2681 HeapFree(GetProcessHeap(), 0, xdg_results);
2685 /******************************************************************************
2686 * create_extra_folders [Internal]
2688 * Create some extra folders that don't have a standard CSIDL definition.
2690 static HRESULT create_extra_folders(void)
2692 static const WCHAR environW[] = {'E','n','v','i','r','o','n','m','e','n','t',0};
2693 static const WCHAR TempW[] = {'T','e','m','p',0};
2694 static const WCHAR TEMPW[] = {'T','E','M','P',0};
2695 static const WCHAR TMPW[] = {'T','M','P',0};
2696 WCHAR path[MAX_PATH+5];
2697 HRESULT hr;
2698 HKEY hkey;
2699 DWORD type, size, ret;
2701 ret = RegCreateKeyW( HKEY_CURRENT_USER, environW, &hkey );
2702 if (ret) return HRESULT_FROM_WIN32( ret );
2704 /* FIXME: should be under AppData, but we don't want spaces in the temp path */
2705 hr = SHGetFolderPathAndSubDirW( 0, CSIDL_PROFILE | CSIDL_FLAG_CREATE, NULL,
2706 SHGFP_TYPE_DEFAULT, TempW, path );
2707 if (SUCCEEDED(hr))
2709 size = sizeof(path);
2710 if (RegQueryValueExW( hkey, TEMPW, NULL, &type, (LPBYTE)path, &size ))
2711 RegSetValueExW( hkey, TEMPW, 0, REG_SZ, (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR) );
2712 size = sizeof(path);
2713 if (RegQueryValueExW( hkey, TMPW, NULL, &type, (LPBYTE)path, &size ))
2714 RegSetValueExW( hkey, TMPW, 0, REG_SZ, (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR) );
2716 RegCloseKey( hkey );
2717 return hr;
2721 /******************************************************************************
2722 * set_folder_attributes
2724 * Set the various folder attributes registry keys.
2726 static HRESULT set_folder_attributes(void)
2728 static const WCHAR clsidW[] = {'C','L','S','I','D','\\',0 };
2729 static const WCHAR shellfolderW[] = {'\\','S','h','e','l','l','F','o','l','d','e','r', 0 };
2730 static const WCHAR wfparsingW[] = {'W','a','n','t','s','F','O','R','P','A','R','S','I','N','G',0};
2731 static const WCHAR wfdisplayW[] = {'W','a','n','t','s','F','O','R','D','I','S','P','L','A','Y',0};
2732 static const WCHAR hideasdeleteW[] = {'H','i','d','e','A','s','D','e','l','e','t','e','P','e','r','U','s','e','r',0};
2733 static const WCHAR attributesW[] = {'A','t','t','r','i','b','u','t','e','s',0};
2734 static const WCHAR cfattributesW[] = {'C','a','l','l','F','o','r','A','t','t','r','i','b','u','t','e','s',0};
2735 static const WCHAR emptyW[] = {0};
2737 static const struct
2739 const CLSID *clsid;
2740 BOOL wfparsing : 1;
2741 BOOL wfdisplay : 1;
2742 BOOL hideasdel : 1;
2743 DWORD attr;
2744 DWORD call_for_attr;
2745 } folders[] =
2747 { &CLSID_UnixFolder, TRUE, FALSE, FALSE },
2748 { &CLSID_UnixDosFolder, TRUE, FALSE, FALSE,
2749 SFGAO_FILESYSANCESTOR|SFGAO_FOLDER|SFGAO_HASSUBFOLDER, SFGAO_FILESYSTEM },
2750 { &CLSID_FolderShortcut, FALSE, FALSE, FALSE,
2751 SFGAO_FILESYSTEM|SFGAO_FOLDER|SFGAO_LINK,
2752 SFGAO_HASSUBFOLDER|SFGAO_FILESYSTEM|SFGAO_FOLDER|SFGAO_FILESYSANCESTOR },
2753 { &CLSID_MyDocuments, TRUE, FALSE, FALSE,
2754 SFGAO_FILESYSANCESTOR|SFGAO_FOLDER|SFGAO_HASSUBFOLDER, SFGAO_FILESYSTEM },
2755 { &CLSID_RecycleBin, FALSE, FALSE, FALSE,
2756 SFGAO_FOLDER|SFGAO_DROPTARGET|SFGAO_HASPROPSHEET },
2757 { &CLSID_ControlPanel, FALSE, TRUE, TRUE,
2758 SFGAO_FOLDER|SFGAO_HASSUBFOLDER }
2761 unsigned int i;
2762 WCHAR buffer[39 + (sizeof(clsidW) + sizeof(shellfolderW)) / sizeof(WCHAR)];
2763 LONG res;
2764 HKEY hkey;
2766 for (i = 0; i < sizeof(folders)/sizeof(folders[0]); i++)
2768 strcpyW( buffer, clsidW );
2769 StringFromGUID2( folders[i].clsid, buffer + strlenW(buffer), 39 );
2770 strcatW( buffer, shellfolderW );
2771 res = RegCreateKeyExW( HKEY_CLASSES_ROOT, buffer, 0, NULL, 0,
2772 KEY_READ | KEY_WRITE, NULL, &hkey, NULL);
2773 if (res) return HRESULT_FROM_WIN32( res );
2774 if (folders[i].wfparsing)
2775 res = RegSetValueExW( hkey, wfparsingW, 0, REG_SZ, (const BYTE *)emptyW, sizeof(emptyW) );
2776 if (folders[i].wfdisplay)
2777 res = RegSetValueExW( hkey, wfdisplayW, 0, REG_SZ, (const BYTE *)emptyW, sizeof(emptyW) );
2778 if (folders[i].hideasdel)
2779 res = RegSetValueExW( hkey, hideasdeleteW, 0, REG_SZ, (const BYTE *)emptyW, sizeof(emptyW) );
2780 if (folders[i].attr)
2781 res = RegSetValueExW( hkey, attributesW, 0, REG_DWORD,
2782 (const BYTE *)&folders[i].attr, sizeof(DWORD));
2783 if (folders[i].call_for_attr)
2784 res = RegSetValueExW( hkey, cfattributesW, 0, REG_DWORD,
2785 (const BYTE *)&folders[i].call_for_attr, sizeof(DWORD));
2786 RegCloseKey( hkey );
2788 return S_OK;
2792 /* Register the default values in the registry, as some apps seem to depend
2793 * on their presence. The set registered was taken from Windows XP.
2795 HRESULT SHELL_RegisterShellFolders(void)
2797 HRESULT hr;
2799 /* Set up '$HOME' targeted symlinks for 'My Documents', 'My Pictures',
2800 * 'My Video', 'My Music' and 'Desktop' in advance, so that the
2801 * _SHRegister*ShellFolders() functions will find everything nice and clean
2802 * and thus will not attempt to create them in the profile directory. */
2803 _SHCreateSymbolicLinks();
2805 hr = _SHRegisterUserShellFolders(TRUE);
2806 if (SUCCEEDED(hr))
2807 hr = _SHRegisterUserShellFolders(FALSE);
2808 if (SUCCEEDED(hr))
2809 hr = _SHRegisterCommonShellFolders();
2810 if (SUCCEEDED(hr))
2811 hr = create_extra_folders();
2812 if (SUCCEEDED(hr))
2813 hr = set_folder_attributes();
2814 return hr;
2817 /*************************************************************************
2818 * SHGetSpecialFolderPathA [SHELL32.@]
2820 BOOL WINAPI SHGetSpecialFolderPathA (
2821 HWND hwndOwner,
2822 LPSTR szPath,
2823 int nFolder,
2824 BOOL bCreate)
2826 return (SHGetFolderPathA(
2827 hwndOwner,
2828 nFolder + (bCreate ? CSIDL_FLAG_CREATE : 0),
2829 NULL,
2831 szPath)) == S_OK ? TRUE : FALSE;
2834 /*************************************************************************
2835 * SHGetSpecialFolderPathW
2837 BOOL WINAPI SHGetSpecialFolderPathW (
2838 HWND hwndOwner,
2839 LPWSTR szPath,
2840 int nFolder,
2841 BOOL bCreate)
2843 return (SHGetFolderPathW(
2844 hwndOwner,
2845 nFolder + (bCreate ? CSIDL_FLAG_CREATE : 0),
2846 NULL,
2848 szPath)) == S_OK ? TRUE : FALSE;
2851 /*************************************************************************
2852 * SHGetSpecialFolderPath (SHELL32.175)
2854 BOOL WINAPI SHGetSpecialFolderPathAW (
2855 HWND hwndOwner,
2856 LPVOID szPath,
2857 int nFolder,
2858 BOOL bCreate)
2861 if (SHELL_OsIsUnicode())
2862 return SHGetSpecialFolderPathW (hwndOwner, szPath, nFolder, bCreate);
2863 return SHGetSpecialFolderPathA (hwndOwner, szPath, nFolder, bCreate);
2866 /*************************************************************************
2867 * SHGetFolderLocation [SHELL32.@]
2869 * Gets the folder locations from the registry and creates a pidl.
2871 * PARAMS
2872 * hwndOwner [I]
2873 * nFolder [I] CSIDL_xxxxx
2874 * hToken [I] token representing user, or NULL for current user, or -1 for
2875 * default user
2876 * dwReserved [I] must be zero
2877 * ppidl [O] PIDL of a special folder
2879 * RETURNS
2880 * Success: S_OK
2881 * Failure: Standard OLE-defined error result, S_FALSE or E_INVALIDARG
2883 * NOTES
2884 * Creates missing reg keys and directories.
2885 * Mostly forwards to SHGetFolderPathW, but a few values of nFolder return
2886 * virtual folders that are handled here.
2888 HRESULT WINAPI SHGetFolderLocation(
2889 HWND hwndOwner,
2890 int nFolder,
2891 HANDLE hToken,
2892 DWORD dwReserved,
2893 LPITEMIDLIST *ppidl)
2895 HRESULT hr = E_INVALIDARG;
2897 TRACE("%p 0x%08x %p 0x%08x %p\n",
2898 hwndOwner, nFolder, hToken, dwReserved, ppidl);
2900 if (!ppidl)
2901 return E_INVALIDARG;
2902 if (dwReserved)
2903 return E_INVALIDARG;
2905 /* The virtual folders' locations are not user-dependent */
2906 *ppidl = NULL;
2907 switch (nFolder & CSIDL_FOLDER_MASK)
2909 case CSIDL_DESKTOP:
2910 *ppidl = _ILCreateDesktop();
2911 break;
2913 case CSIDL_PERSONAL:
2914 *ppidl = _ILCreateMyDocuments();
2915 break;
2917 case CSIDL_INTERNET:
2918 *ppidl = _ILCreateIExplore();
2919 break;
2921 case CSIDL_CONTROLS:
2922 *ppidl = _ILCreateControlPanel();
2923 break;
2925 case CSIDL_PRINTERS:
2926 *ppidl = _ILCreatePrinters();
2927 break;
2929 case CSIDL_BITBUCKET:
2930 *ppidl = _ILCreateBitBucket();
2931 break;
2933 case CSIDL_DRIVES:
2934 *ppidl = _ILCreateMyComputer();
2935 break;
2937 case CSIDL_NETWORK:
2938 *ppidl = _ILCreateNetwork();
2939 break;
2941 default:
2943 WCHAR szPath[MAX_PATH];
2945 hr = SHGetFolderPathW(hwndOwner, nFolder, hToken,
2946 SHGFP_TYPE_CURRENT, szPath);
2947 if (SUCCEEDED(hr))
2949 DWORD attributes=0;
2951 TRACE("Value=%s\n", debugstr_w(szPath));
2952 hr = SHILCreateFromPathW(szPath, ppidl, &attributes);
2954 else if (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
2956 /* unlike SHGetFolderPath, SHGetFolderLocation in shell32
2957 * version 6.0 returns E_FAIL for nonexistent paths
2959 hr = E_FAIL;
2963 if(*ppidl)
2964 hr = NOERROR;
2966 TRACE("-- (new pidl %p)\n",*ppidl);
2967 return hr;
2970 /*************************************************************************
2971 * SHGetSpecialFolderLocation [SHELL32.@]
2973 * NOTES
2974 * In NT5, SHGetSpecialFolderLocation needs the <winntdir>/Recent
2975 * directory.
2977 HRESULT WINAPI SHGetSpecialFolderLocation(
2978 HWND hwndOwner,
2979 INT nFolder,
2980 LPITEMIDLIST * ppidl)
2982 HRESULT hr = E_INVALIDARG;
2984 TRACE("(%p,0x%x,%p)\n", hwndOwner,nFolder,ppidl);
2986 if (!ppidl)
2987 return E_INVALIDARG;
2989 hr = SHGetFolderLocation(hwndOwner, nFolder, NULL, 0, ppidl);
2990 return hr;
2993 static int csidl_from_id( const KNOWNFOLDERID *id )
2995 int i;
2996 for (i = 0; i < sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]); i++)
2997 if (IsEqualGUID( CSIDL_Data[i].id, id )) return i;
2998 return -1;
3001 /*************************************************************************
3002 * SHGetKnownFolderPath [SHELL32.@]
3004 HRESULT WINAPI SHGetKnownFolderPath(REFKNOWNFOLDERID rfid, DWORD flags, HANDLE token, PWSTR *path)
3006 HRESULT hr;
3007 WCHAR folder[MAX_PATH];
3008 int index = csidl_from_id( rfid );
3010 TRACE("%s, 0x%08x, %p, %p\n", debugstr_guid(rfid), flags, token, path);
3012 if (index < 0)
3013 return E_INVALIDARG;
3015 if (flags & KF_FLAG_CREATE)
3016 index |= CSIDL_FLAG_CREATE;
3018 if (flags & KF_FLAG_DONT_VERIFY)
3019 index |= CSIDL_FLAG_DONT_VERIFY;
3021 if (flags & KF_FLAG_NO_ALIAS)
3022 index |= CSIDL_FLAG_NO_ALIAS;
3024 if (flags & KF_FLAG_INIT)
3025 index |= CSIDL_FLAG_PER_USER_INIT;
3027 if (flags & ~(KF_FLAG_CREATE|KF_FLAG_DONT_VERIFY|KF_FLAG_NO_ALIAS|KF_FLAG_INIT))
3029 FIXME("flags 0x%08x not supported\n", flags);
3030 return E_INVALIDARG;
3033 hr = SHGetFolderPathW( NULL, index, token, 0, folder );
3034 if (SUCCEEDED(hr))
3036 *path = CoTaskMemAlloc( (strlenW( folder ) + 1) * sizeof(WCHAR) );
3037 if (!*path)
3038 return E_OUTOFMEMORY;
3039 strcpyW( *path, folder );
3041 return hr;
3044 /*************************************************************************
3045 * SHGetFolderPathEx [SHELL32.@]
3047 HRESULT WINAPI SHGetFolderPathEx(REFKNOWNFOLDERID rfid, DWORD flags, HANDLE token, LPWSTR path, DWORD len)
3049 HRESULT hr;
3050 WCHAR *buffer;
3052 TRACE("%s, 0x%08x, %p, %p, %u\n", debugstr_guid(rfid), flags, token, path, len);
3054 if (!path || !len) return E_INVALIDARG;
3056 hr = SHGetKnownFolderPath( rfid, flags, token, &buffer );
3057 if (SUCCEEDED( hr ))
3059 if (strlenW( buffer ) + 1 > len)
3061 CoTaskMemFree( buffer );
3062 return HRESULT_FROM_WIN32( ERROR_INSUFFICIENT_BUFFER );
3064 strcpyW( path, buffer );
3065 CoTaskMemFree( buffer );
3067 return hr;
3070 /* constant values used by known folder functions */
3071 static const WCHAR szCategory[] = {'C','a','t','e','g','o','r','y',0};
3072 static const WCHAR szName[] = {'N','a','m','e',0};
3073 static const WCHAR szRelativePath[] = {'R','e','l','a','t','i','v','e','P','a','t','h',0};
3074 static const WCHAR szParentFolder[] = {'P','a','r','e','n','t','F','o','l','d','e','r',0};
3077 * Internal function to convert known folder identifier to path of registry key
3078 * associated with known folder.
3080 * Parameters:
3081 * rfid [I] pointer to known folder identifier (may be NULL)
3082 * lpStringGuid [I] string with known folder identifier (used when rfid is NULL)
3083 * lpPath [O] place to store string address. String should be
3084 * later freed using HeapFree(GetProcessHeap(),0, ... )
3086 static HRESULT get_known_folder_registry_path(
3087 REFKNOWNFOLDERID rfid,
3088 LPWSTR lpStringGuid,
3089 LPWSTR *lpPath)
3091 static const WCHAR sBackslash[] = {'\\',0};
3092 HRESULT hr = S_OK;
3093 int length;
3094 WCHAR sGuid[50];
3096 TRACE("(%s, %s, %p)\n", debugstr_guid(rfid), debugstr_w(lpStringGuid), lpPath);
3098 if(rfid)
3099 StringFromGUID2(rfid, sGuid, sizeof(sGuid)/sizeof(sGuid[0]));
3100 else
3101 lstrcpyW(sGuid, lpStringGuid);
3103 length = lstrlenW(szKnownFolderDescriptions)+51;
3104 *lpPath = HeapAlloc(GetProcessHeap(), 0, length*sizeof(WCHAR));
3105 if(!(*lpPath))
3106 hr = E_OUTOFMEMORY;
3108 if(SUCCEEDED(hr))
3110 lstrcpyW(*lpPath, szKnownFolderDescriptions);
3111 lstrcatW(*lpPath, sBackslash);
3112 lstrcatW(*lpPath, sGuid);
3115 return hr;
3119 * Internal function to get place where folder redirection information are stored.
3121 * Parameters:
3122 * rfid [I] pointer to known folder identifier (may be NULL)
3123 * rootKey [O] root key where the redirection information are stored
3124 * It can be HKLM for COMMON folders, and HKCU for PERUSER folders.
3125 * However, besides root key, path is always that same, and is stored
3126 * as "szKnownFolderRedirections" constant
3128 static HRESULT get_known_folder_redirection_place(
3129 REFKNOWNFOLDERID rfid,
3130 HKEY *rootKey)
3132 HRESULT hr;
3133 LPWSTR lpRegistryPath = NULL;
3134 KF_CATEGORY category;
3135 DWORD dwSize;
3137 /* first, get known folder's category */
3138 hr = get_known_folder_registry_path(rfid, NULL, &lpRegistryPath);
3140 if(SUCCEEDED(hr))
3142 dwSize = sizeof(category);
3143 hr = HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE, lpRegistryPath, szCategory, RRF_RT_DWORD, NULL, &category, &dwSize));
3146 if(SUCCEEDED(hr))
3148 if(category == KF_CATEGORY_COMMON)
3150 *rootKey = HKEY_LOCAL_MACHINE;
3151 hr = S_OK;
3153 else if(category == KF_CATEGORY_PERUSER)
3155 *rootKey = HKEY_CURRENT_USER;
3156 hr = S_OK;
3158 else
3159 hr = E_FAIL;
3162 HeapFree(GetProcessHeap(), 0, lpRegistryPath);
3163 return hr;
3166 static HRESULT get_known_folder_path_by_id(REFKNOWNFOLDERID folderId, LPWSTR lpRegistryPath, DWORD dwFlags, LPWSTR *ppszPath);
3168 static HRESULT get_known_folder_category(
3169 LPWSTR registryPath,
3170 KF_CATEGORY* pCategory)
3172 DWORD dwSize = sizeof(DWORD);
3173 DWORD dwType;
3174 return HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE, registryPath, szCategory, RRF_RT_DWORD, &dwType, pCategory, &dwSize));
3177 static HRESULT redirect_known_folder(
3178 REFKNOWNFOLDERID rfid,
3179 HWND hwnd,
3180 KF_REDIRECT_FLAGS flags,
3181 LPCWSTR pszTargetPath,
3182 UINT cFolders,
3183 KNOWNFOLDERID const *pExclusion,
3184 LPWSTR *ppszError)
3186 HRESULT hr;
3187 HKEY rootKey = HKEY_LOCAL_MACHINE, hKey;
3188 WCHAR sGuid[39];
3189 LPWSTR lpRegistryPath = NULL, lpSrcPath = NULL;
3190 TRACE("(%s, %p, 0x%08x, %s, %d, %p, %p)\n", debugstr_guid(rfid), hwnd, flags, debugstr_w(pszTargetPath), cFolders, pExclusion, ppszError);
3192 if (ppszError) *ppszError = NULL;
3194 hr = get_known_folder_registry_path(rfid, NULL, &lpRegistryPath);
3196 if(SUCCEEDED(hr))
3197 hr = get_known_folder_path_by_id(rfid, lpRegistryPath, 0, &lpSrcPath);
3199 HeapFree(GetProcessHeap(), 0, lpRegistryPath);
3201 /* get path to redirection storage */
3202 if(SUCCEEDED(hr))
3203 hr = get_known_folder_redirection_place(rfid, &rootKey);
3205 /* write redirection information */
3206 if(SUCCEEDED(hr))
3207 hr = HRESULT_FROM_WIN32(RegCreateKeyExW(rootKey, szKnownFolderRedirections, 0, NULL, 0, KEY_WRITE, NULL, &hKey, NULL));
3209 if(SUCCEEDED(hr))
3211 StringFromGUID2(rfid, sGuid, sizeof(sGuid)/sizeof(sGuid[0]));
3213 hr = HRESULT_FROM_WIN32(RegSetValueExW(hKey, sGuid, 0, REG_SZ, (LPBYTE)pszTargetPath, (lstrlenW(pszTargetPath)+1)*sizeof(WCHAR)));
3215 RegCloseKey(hKey);
3218 /* make sure destination path exists */
3219 SHCreateDirectory(NULL, pszTargetPath);
3221 /* copy content if required */
3222 if(SUCCEEDED(hr) && (flags & KF_REDIRECT_COPY_CONTENTS) )
3224 static const WCHAR sWildcard[] = {'\\','*',0};
3225 WCHAR srcPath[MAX_PATH+1], dstPath[MAX_PATH+1];
3226 SHFILEOPSTRUCTW fileOp;
3228 ZeroMemory(srcPath, sizeof(srcPath));
3229 lstrcpyW(srcPath, lpSrcPath);
3230 lstrcatW(srcPath, sWildcard);
3232 ZeroMemory(dstPath, sizeof(dstPath));
3233 lstrcpyW(dstPath, pszTargetPath);
3235 ZeroMemory(&fileOp, sizeof(fileOp));
3237 if(flags & KF_REDIRECT_DEL_SOURCE_CONTENTS)
3238 fileOp.wFunc = FO_MOVE;
3239 else
3240 fileOp.wFunc = FO_COPY;
3242 fileOp.pFrom = srcPath;
3243 fileOp.pTo = dstPath;
3244 fileOp.fFlags = FOF_NO_UI;
3246 hr = (SHFileOperationW(&fileOp)==0 ? S_OK : E_FAIL);
3248 if(flags & KF_REDIRECT_DEL_SOURCE_CONTENTS)
3250 ZeroMemory(srcPath, sizeof(srcPath));
3251 lstrcpyW(srcPath, lpSrcPath);
3253 ZeroMemory(&fileOp, sizeof(fileOp));
3254 fileOp.wFunc = FO_DELETE;
3255 fileOp.pFrom = srcPath;
3256 fileOp.fFlags = FOF_NO_UI;
3258 hr = (SHFileOperationW(&fileOp)==0 ? S_OK : E_FAIL);
3262 CoTaskMemFree(lpSrcPath);
3264 return hr;
3268 struct knownfolder
3270 IKnownFolder IKnownFolder_iface;
3271 LONG refs;
3272 KNOWNFOLDERID id;
3273 LPWSTR registryPath;
3276 static inline struct knownfolder *impl_from_IKnownFolder( IKnownFolder *iface )
3278 return CONTAINING_RECORD( iface, struct knownfolder, IKnownFolder_iface );
3281 static ULONG WINAPI knownfolder_AddRef(
3282 IKnownFolder *iface )
3284 struct knownfolder *knownfolder = impl_from_IKnownFolder( iface );
3285 return InterlockedIncrement( &knownfolder->refs );
3288 static ULONG WINAPI knownfolder_Release(
3289 IKnownFolder *iface )
3291 struct knownfolder *knownfolder = impl_from_IKnownFolder( iface );
3292 LONG refs = InterlockedDecrement( &knownfolder->refs );
3293 if (!refs)
3295 TRACE("destroying %p\n", knownfolder);
3296 HeapFree( GetProcessHeap(), 0, knownfolder->registryPath);
3297 HeapFree( GetProcessHeap(), 0, knownfolder );
3299 return refs;
3302 static HRESULT WINAPI knownfolder_QueryInterface(
3303 IKnownFolder *iface,
3304 REFIID riid,
3305 void **ppv )
3307 struct knownfolder *This = impl_from_IKnownFolder( iface );
3309 TRACE("%p %s %p\n", This, debugstr_guid( riid ), ppv );
3311 if ( IsEqualGUID( riid, &IID_IKnownFolder ) ||
3312 IsEqualGUID( riid, &IID_IUnknown ) )
3314 *ppv = iface;
3316 else
3318 FIXME("interface %s not implemented\n", debugstr_guid(riid));
3319 return E_NOINTERFACE;
3321 IKnownFolder_AddRef( iface );
3322 return S_OK;
3325 static HRESULT knownfolder_set_id(
3326 struct knownfolder *knownfolder,
3327 const KNOWNFOLDERID *kfid)
3329 HKEY hKey;
3330 HRESULT hr;
3332 TRACE("%s\n", debugstr_guid(kfid));
3334 knownfolder->id = *kfid;
3336 /* check is it registry-registered folder */
3337 hr = get_known_folder_registry_path(kfid, NULL, &knownfolder->registryPath);
3338 if(SUCCEEDED(hr))
3339 hr = HRESULT_FROM_WIN32(RegOpenKeyExW(HKEY_LOCAL_MACHINE, knownfolder->registryPath, 0, 0, &hKey));
3341 if(SUCCEEDED(hr))
3343 hr = S_OK;
3344 RegCloseKey(hKey);
3346 else
3348 /* This known folder is not registered. To mark it, we set registryPath to NULL */
3349 HeapFree(GetProcessHeap(), 0, knownfolder->registryPath);
3350 knownfolder->registryPath = NULL;
3351 hr = S_OK;
3354 return hr;
3357 static HRESULT WINAPI knownfolder_GetId(
3358 IKnownFolder *iface,
3359 KNOWNFOLDERID *pkfid)
3361 struct knownfolder *knownfolder = impl_from_IKnownFolder( iface );
3363 TRACE("%p\n", pkfid);
3365 *pkfid = knownfolder->id;
3366 return S_OK;
3369 static HRESULT WINAPI knownfolder_GetCategory(
3370 IKnownFolder *iface,
3371 KF_CATEGORY *pCategory)
3373 struct knownfolder *knownfolder = impl_from_IKnownFolder(iface);
3374 HRESULT hr = S_OK;
3376 TRACE("%p, %p\n", knownfolder, pCategory);
3378 /* we cannot get a category for a folder which is not registered */
3379 if(!knownfolder->registryPath)
3380 hr = E_FAIL;
3382 if(SUCCEEDED(hr))
3383 hr = get_known_folder_category(knownfolder->registryPath, pCategory);
3385 return hr;
3388 static HRESULT WINAPI knownfolder_GetShellItem(
3389 IKnownFolder *iface,
3390 DWORD dwFlags,
3391 REFIID riid,
3392 void **ppv)
3394 FIXME("0x%08x, %s, %p\n", dwFlags, debugstr_guid(riid), ppv);
3395 return E_NOTIMPL;
3398 static HRESULT get_known_folder_path(
3399 LPWSTR sFolderId,
3400 LPWSTR registryPath,
3401 LPWSTR *ppszPath)
3403 static const WCHAR sBackslash[] = {'\\',0};
3404 HRESULT hr;
3405 DWORD dwSize, dwType;
3406 WCHAR path[MAX_PATH] = {0};
3407 WCHAR parentGuid[39];
3408 KF_CATEGORY category;
3409 LPWSTR parentRegistryPath, parentPath;
3410 HKEY hRedirectionRootKey = NULL;
3412 TRACE("(%s, %p)\n", debugstr_w(registryPath), ppszPath);
3413 *ppszPath = NULL;
3415 /* check if folder has parent */
3416 dwSize = sizeof(parentGuid);
3417 hr = HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE, registryPath, szParentFolder, RRF_RT_REG_SZ, &dwType, parentGuid, &dwSize));
3418 if(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) hr = S_FALSE;
3420 if(hr == S_OK)
3422 /* get parent's known folder path (recursive) */
3423 hr = get_known_folder_registry_path(NULL, parentGuid, &parentRegistryPath);
3424 if(FAILED(hr)) return hr;
3426 hr = get_known_folder_path(parentGuid, parentRegistryPath, &parentPath);
3427 if(FAILED(hr)) {
3428 HeapFree(GetProcessHeap(), 0, parentRegistryPath);
3429 return hr;
3432 lstrcatW(path, parentPath);
3433 lstrcatW(path, sBackslash);
3435 HeapFree(GetProcessHeap(), 0, parentRegistryPath);
3436 HeapFree(GetProcessHeap(), 0, parentPath);
3439 /* check, if folder was redirected */
3440 if(SUCCEEDED(hr))
3441 hr = HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE, registryPath, szCategory, RRF_RT_REG_DWORD, NULL, &category, &dwSize));
3443 if(SUCCEEDED(hr))
3445 if(category == KF_CATEGORY_COMMON)
3446 hRedirectionRootKey = HKEY_LOCAL_MACHINE;
3447 else if(category == KF_CATEGORY_PERUSER)
3448 hRedirectionRootKey = HKEY_CURRENT_USER;
3450 if(hRedirectionRootKey)
3452 hr = HRESULT_FROM_WIN32(RegGetValueW(hRedirectionRootKey, szKnownFolderRedirections, sFolderId, RRF_RT_REG_SZ, NULL, NULL, &dwSize));
3454 if(SUCCEEDED(hr))
3456 *ppszPath = CoTaskMemAlloc(dwSize+(lstrlenW(path)+1)*sizeof(WCHAR));
3457 if(!*ppszPath) hr = E_OUTOFMEMORY;
3460 if(SUCCEEDED(hr))
3462 lstrcpyW(*ppszPath, path);
3463 hr = HRESULT_FROM_WIN32(RegGetValueW(hRedirectionRootKey, szKnownFolderRedirections, sFolderId, RRF_RT_REG_SZ, NULL, *ppszPath + lstrlenW(path), &dwSize));
3467 if(!*ppszPath)
3469 /* no redirection, use previous way - read the relative path from folder definition */
3470 hr = HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE, registryPath, szRelativePath, RRF_RT_REG_SZ, &dwType, NULL, &dwSize));
3472 if(SUCCEEDED(hr))
3474 *ppszPath = CoTaskMemAlloc(dwSize+(lstrlenW(path)+1)*sizeof(WCHAR));
3475 if(!*ppszPath) hr = E_OUTOFMEMORY;
3478 if(SUCCEEDED(hr))
3480 lstrcpyW(*ppszPath, path);
3481 hr = HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE, registryPath, szRelativePath, RRF_RT_REG_SZ, &dwType, *ppszPath + lstrlenW(path), &dwSize));
3486 TRACE("returning path: %s\n", debugstr_w(*ppszPath));
3487 return hr;
3490 static HRESULT get_known_folder_path_by_id(
3491 REFKNOWNFOLDERID folderId,
3492 LPWSTR lpRegistryPath,
3493 DWORD dwFlags,
3494 LPWSTR *ppszPath)
3496 HRESULT hr;
3497 WCHAR sGuid[39];
3498 DWORD dwAttributes;
3500 TRACE("(%s, %s, 0x%08x, %p)\n", debugstr_guid(folderId), debugstr_w(lpRegistryPath), dwFlags, ppszPath);
3502 /* if this is registry-registered known folder, get path from registry */
3503 if(lpRegistryPath)
3505 StringFromGUID2(folderId, sGuid, sizeof(sGuid)/sizeof(sGuid[0]));
3507 hr = get_known_folder_path(sGuid, lpRegistryPath, ppszPath);
3509 /* in other case, use older way */
3510 else
3511 hr = SHGetKnownFolderPath( folderId, dwFlags, NULL, ppszPath );
3513 /* check if known folder really exists */
3514 dwAttributes = GetFileAttributesW(*ppszPath);
3515 if(dwAttributes == INVALID_FILE_ATTRIBUTES || !(dwAttributes & FILE_ATTRIBUTE_DIRECTORY) )
3517 TRACE("directory %s not found\n", debugstr_w(*ppszPath));
3518 CoTaskMemFree(*ppszPath);
3519 *ppszPath = NULL;
3520 hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
3523 return hr;
3526 static HRESULT WINAPI knownfolder_GetPath(
3527 IKnownFolder *iface,
3528 DWORD dwFlags,
3529 LPWSTR *ppszPath)
3531 struct knownfolder *knownfolder = impl_from_IKnownFolder( iface );
3532 TRACE("(%p, 0x%08x, %p)\n", knownfolder, dwFlags, ppszPath);
3534 return get_known_folder_path_by_id(&knownfolder->id, knownfolder->registryPath, dwFlags, ppszPath);
3537 static HRESULT WINAPI knownfolder_SetPath(
3538 IKnownFolder *iface,
3539 DWORD dwFlags,
3540 LPCWSTR pszPath)
3542 struct knownfolder *knownfolder = impl_from_IKnownFolder( iface );
3543 HRESULT hr = S_OK;
3545 TRACE("(%p, 0x%08x, %p)\n", knownfolder, dwFlags, debugstr_w(pszPath));
3547 /* check if the known folder is registered */
3548 if(!knownfolder->registryPath)
3549 hr = E_FAIL;
3551 if(SUCCEEDED(hr))
3552 hr = redirect_known_folder(&knownfolder->id, NULL, 0, pszPath, 0, NULL, NULL);
3554 return hr;
3557 static HRESULT WINAPI knownfolder_GetIDList(
3558 IKnownFolder *iface,
3559 DWORD dwFlags,
3560 PIDLIST_ABSOLUTE *ppidl)
3562 FIXME("0x%08x, %p\n", dwFlags, ppidl);
3563 return E_NOTIMPL;
3566 static HRESULT WINAPI knownfolder_GetFolderType(
3567 IKnownFolder *iface,
3568 FOLDERTYPEID *pftid)
3570 FIXME("%p\n", pftid);
3571 return E_NOTIMPL;
3574 static HRESULT WINAPI knownfolder_GetRedirectionCapabilities(
3575 IKnownFolder *iface,
3576 KF_REDIRECTION_CAPABILITIES *pCapabilities)
3578 FIXME("%p\n", pCapabilities);
3579 return E_NOTIMPL;
3582 static HRESULT WINAPI knownfolder_GetFolderDefinition(
3583 IKnownFolder *iface,
3584 KNOWNFOLDER_DEFINITION *pKFD)
3586 struct knownfolder *knownfolder = impl_from_IKnownFolder( iface );
3587 HRESULT hr;
3588 DWORD dwSize;
3589 TRACE("(%p, %p)\n", knownfolder, pKFD);
3591 if(!pKFD) return E_INVALIDARG;
3593 ZeroMemory(pKFD, sizeof(*pKFD));
3595 hr = get_known_folder_category(knownfolder->registryPath, &pKFD->category);
3597 if(SUCCEEDED(hr))
3598 hr = HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE, knownfolder->registryPath, szName, RRF_RT_REG_SZ, NULL, NULL, &dwSize));
3600 if(SUCCEEDED(hr))
3602 pKFD->pszName = CoTaskMemAlloc(dwSize);
3603 if(!pKFD->pszName) hr = E_OUTOFMEMORY;
3606 if(SUCCEEDED(hr))
3607 hr = HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE, knownfolder->registryPath, szName, RRF_RT_REG_SZ, NULL, pKFD->pszName, &dwSize));
3609 return hr;
3612 static const struct IKnownFolderVtbl knownfolder_vtbl =
3614 knownfolder_QueryInterface,
3615 knownfolder_AddRef,
3616 knownfolder_Release,
3617 knownfolder_GetId,
3618 knownfolder_GetCategory,
3619 knownfolder_GetShellItem,
3620 knownfolder_GetPath,
3621 knownfolder_SetPath,
3622 knownfolder_GetIDList,
3623 knownfolder_GetFolderType,
3624 knownfolder_GetRedirectionCapabilities,
3625 knownfolder_GetFolderDefinition
3628 static HRESULT knownfolder_create( struct knownfolder **knownfolder )
3630 struct knownfolder *kf;
3632 kf = HeapAlloc( GetProcessHeap(), 0, sizeof(*kf) );
3633 if (!kf) return E_OUTOFMEMORY;
3635 kf->IKnownFolder_iface.lpVtbl = &knownfolder_vtbl;
3636 kf->refs = 1;
3637 memset( &kf->id, 0, sizeof(kf->id) );
3638 kf->registryPath = NULL;
3640 *knownfolder = kf;
3642 TRACE("returning iface %p\n", &kf->IKnownFolder_iface);
3643 return S_OK;
3646 struct foldermanager
3648 IKnownFolderManager IKnownFolderManager_iface;
3649 LONG refs;
3650 UINT num_ids;
3651 KNOWNFOLDERID *ids;
3654 static inline struct foldermanager *impl_from_IKnownFolderManager( IKnownFolderManager *iface )
3656 return CONTAINING_RECORD( iface, struct foldermanager, IKnownFolderManager_iface );
3659 static ULONG WINAPI foldermanager_AddRef(
3660 IKnownFolderManager *iface )
3662 struct foldermanager *foldermanager = impl_from_IKnownFolderManager( iface );
3663 return InterlockedIncrement( &foldermanager->refs );
3666 static ULONG WINAPI foldermanager_Release(
3667 IKnownFolderManager *iface )
3669 struct foldermanager *foldermanager = impl_from_IKnownFolderManager( iface );
3670 LONG refs = InterlockedDecrement( &foldermanager->refs );
3671 if (!refs)
3673 TRACE("destroying %p\n", foldermanager);
3674 HeapFree( GetProcessHeap(), 0, foldermanager->ids );
3675 HeapFree( GetProcessHeap(), 0, foldermanager );
3677 return refs;
3680 static HRESULT WINAPI foldermanager_QueryInterface(
3681 IKnownFolderManager *iface,
3682 REFIID riid,
3683 void **ppv )
3685 struct foldermanager *This = impl_from_IKnownFolderManager( iface );
3687 TRACE("%p %s %p\n", This, debugstr_guid( riid ), ppv );
3689 if ( IsEqualGUID( riid, &IID_IKnownFolderManager ) ||
3690 IsEqualGUID( riid, &IID_IUnknown ) )
3692 *ppv = iface;
3694 else
3696 FIXME("interface %s not implemented\n", debugstr_guid(riid));
3697 return E_NOINTERFACE;
3699 IKnownFolderManager_AddRef( iface );
3700 return S_OK;
3703 static HRESULT WINAPI foldermanager_FolderIdFromCsidl(
3704 IKnownFolderManager *iface,
3705 int nCsidl,
3706 KNOWNFOLDERID *pfid)
3708 TRACE("%d, %p\n", nCsidl, pfid);
3710 if (nCsidl >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
3711 return E_INVALIDARG;
3712 *pfid = *CSIDL_Data[nCsidl].id;
3713 return S_OK;
3716 static HRESULT WINAPI foldermanager_FolderIdToCsidl(
3717 IKnownFolderManager *iface,
3718 REFKNOWNFOLDERID rfid,
3719 int *pnCsidl)
3721 int csidl;
3723 TRACE("%s, %p\n", debugstr_guid(rfid), pnCsidl);
3725 csidl = csidl_from_id( rfid );
3726 if (csidl == -1) return E_INVALIDARG;
3727 *pnCsidl = csidl;
3728 return S_OK;
3731 static HRESULT WINAPI foldermanager_GetFolderIds(
3732 IKnownFolderManager *iface,
3733 KNOWNFOLDERID **ppKFId,
3734 UINT *pCount)
3736 struct foldermanager *fm = impl_from_IKnownFolderManager( iface );
3738 TRACE("%p, %p\n", ppKFId, pCount);
3740 *ppKFId = fm->ids;
3741 *pCount = fm->num_ids;
3742 return S_OK;
3745 static BOOL is_knownfolder( struct foldermanager *fm, const KNOWNFOLDERID *id )
3747 UINT i;
3748 HRESULT hr;
3749 LPWSTR registryPath = NULL;
3750 HKEY hKey;
3752 /* TODO: move all entries from "CSIDL_Data" static array to registry known folder descriptions */
3753 for (i = 0; i < fm->num_ids; i++)
3754 if (IsEqualGUID( &fm->ids[i], id )) return TRUE;
3756 hr = get_known_folder_registry_path(id, NULL, &registryPath);
3757 if(SUCCEEDED(hr))
3758 hr = HRESULT_FROM_WIN32(RegOpenKeyExW(HKEY_LOCAL_MACHINE, registryPath, 0, 0, &hKey));
3760 if(SUCCEEDED(hr))
3762 hr = S_OK;
3763 RegCloseKey(hKey);
3766 return hr == S_OK;
3769 static HRESULT WINAPI foldermanager_GetFolder(
3770 IKnownFolderManager *iface,
3771 REFKNOWNFOLDERID rfid,
3772 IKnownFolder **ppkf)
3774 struct foldermanager *fm = impl_from_IKnownFolderManager( iface );
3775 struct knownfolder *kf;
3776 HRESULT hr;
3778 TRACE("%s, %p\n", debugstr_guid(rfid), ppkf);
3780 if (!is_knownfolder( fm, rfid ))
3782 WARN("unknown folder\n");
3783 return E_INVALIDARG;
3785 hr = knownfolder_create( &kf );
3786 if (SUCCEEDED( hr ))
3788 hr = knownfolder_set_id( kf, rfid );
3789 *ppkf = &kf->IKnownFolder_iface;
3791 else
3792 *ppkf = NULL;
3794 return hr;
3797 static HRESULT WINAPI foldermanager_GetFolderByName(
3798 IKnownFolderManager *iface,
3799 LPCWSTR pszCanonicalName,
3800 IKnownFolder **ppkf)
3802 FIXME("%s, %p\n", debugstr_w(pszCanonicalName), ppkf);
3803 return E_NOTIMPL;
3806 static HRESULT WINAPI foldermanager_RegisterFolder(
3807 IKnownFolderManager *iface,
3808 REFKNOWNFOLDERID rfid,
3809 KNOWNFOLDER_DEFINITION const *pKFD)
3811 HRESULT hr;
3812 HKEY hKey = NULL;
3813 DWORD dwDisp;
3814 LPWSTR registryPath = NULL;
3815 TRACE("(%p, %s, %p)\n", iface, debugstr_guid(rfid), pKFD);
3817 hr = get_known_folder_registry_path(rfid, NULL, &registryPath);
3818 TRACE("registry path: %s\n", debugstr_w(registryPath));
3820 if(SUCCEEDED(hr))
3821 hr = HRESULT_FROM_WIN32(RegCreateKeyExW(HKEY_LOCAL_MACHINE, registryPath, 0, NULL, 0, KEY_WRITE, 0, &hKey, &dwDisp));
3823 if(SUCCEEDED(hr))
3825 if(dwDisp == REG_OPENED_EXISTING_KEY)
3826 hr = E_FAIL;
3828 if(SUCCEEDED(hr))
3829 hr = HRESULT_FROM_WIN32(RegSetValueExW(hKey, szCategory, 0, REG_DWORD, (LPBYTE)&pKFD->category, sizeof(pKFD->category)));
3831 if(SUCCEEDED(hr))
3832 hr = HRESULT_FROM_WIN32(RegSetValueExW(hKey, szName, 0, REG_SZ, (LPBYTE)pKFD->pszName, (lstrlenW(pKFD->pszName)+1)*sizeof(WCHAR) ));
3834 if(SUCCEEDED(hr) && !IsEqualGUID(&pKFD->fidParent, &GUID_NULL))
3836 WCHAR sParentGuid[39];
3837 StringFromGUID2(&pKFD->fidParent, sParentGuid, sizeof(sParentGuid)/sizeof(sParentGuid[0]));
3839 /* this known folder has parent folder */
3840 hr = HRESULT_FROM_WIN32(RegSetValueExW(hKey, szParentFolder, 0, REG_SZ, (LPBYTE)sParentGuid, sizeof(sParentGuid)));
3843 if(SUCCEEDED(hr) && pKFD->category != KF_CATEGORY_VIRTUAL)
3845 if(!pKFD->pszRelativePath)
3846 hr = E_INVALIDARG;
3848 if(SUCCEEDED(hr))
3849 hr = HRESULT_FROM_WIN32(RegSetValueExW(hKey, szRelativePath, 0, REG_SZ, (LPBYTE)pKFD->pszRelativePath, (lstrlenW(pKFD->pszRelativePath)+1)*sizeof(WCHAR) ));
3852 RegCloseKey(hKey);
3854 if(FAILED(hr))
3855 SHDeleteKeyW(HKEY_LOCAL_MACHINE, registryPath);
3858 HeapFree(GetProcessHeap(), 0, registryPath);
3859 return hr;
3862 static HRESULT WINAPI foldermanager_UnregisterFolder(
3863 IKnownFolderManager *iface,
3864 REFKNOWNFOLDERID rfid)
3866 HRESULT hr;
3867 LPWSTR registryPath = NULL;
3868 TRACE("(%p, %s)\n", iface, debugstr_guid(rfid));
3870 hr = get_known_folder_registry_path(rfid, NULL, &registryPath);
3872 if(SUCCEEDED(hr))
3873 hr = HRESULT_FROM_WIN32(SHDeleteKeyW(HKEY_LOCAL_MACHINE, registryPath));
3875 HeapFree(GetProcessHeap(), 0, registryPath);
3876 return hr;
3879 static HRESULT WINAPI foldermanager_FindFolderFromPath(
3880 IKnownFolderManager *iface,
3881 LPCWSTR pszPath,
3882 FFFP_MODE mode,
3883 IKnownFolder **ppkf)
3885 FIXME("%s, 0x%08x, %p\n", debugstr_w(pszPath), mode, ppkf);
3886 return E_NOTIMPL;
3889 static HRESULT WINAPI foldermanager_FindFolderFromIDList(
3890 IKnownFolderManager *iface,
3891 PCIDLIST_ABSOLUTE pidl,
3892 IKnownFolder **ppkf)
3894 FIXME("%p, %p\n", pidl, ppkf);
3895 return E_NOTIMPL;
3898 static HRESULT WINAPI foldermanager_Redirect(
3899 IKnownFolderManager *iface,
3900 REFKNOWNFOLDERID rfid,
3901 HWND hwnd,
3902 KF_REDIRECT_FLAGS flags,
3903 LPCWSTR pszTargetPath,
3904 UINT cFolders,
3905 KNOWNFOLDERID const *pExclusion,
3906 LPWSTR *ppszError)
3908 return redirect_known_folder(rfid, hwnd, flags, pszTargetPath, cFolders, pExclusion, ppszError);
3911 static const struct IKnownFolderManagerVtbl foldermanager_vtbl =
3913 foldermanager_QueryInterface,
3914 foldermanager_AddRef,
3915 foldermanager_Release,
3916 foldermanager_FolderIdFromCsidl,
3917 foldermanager_FolderIdToCsidl,
3918 foldermanager_GetFolderIds,
3919 foldermanager_GetFolder,
3920 foldermanager_GetFolderByName,
3921 foldermanager_RegisterFolder,
3922 foldermanager_UnregisterFolder,
3923 foldermanager_FindFolderFromPath,
3924 foldermanager_FindFolderFromIDList,
3925 foldermanager_Redirect
3928 static HRESULT foldermanager_create( void **ppv )
3930 UINT i, j;
3931 struct foldermanager *fm;
3933 fm = HeapAlloc( GetProcessHeap(), 0, sizeof(*fm) );
3934 if (!fm) return E_OUTOFMEMORY;
3936 fm->IKnownFolderManager_iface.lpVtbl = &foldermanager_vtbl;
3937 fm->refs = 1;
3938 fm->num_ids = 0;
3940 for (i = 0; i < sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]); i++)
3942 if (!IsEqualGUID( CSIDL_Data[i].id, &GUID_NULL )) fm->num_ids++;
3944 fm->ids = HeapAlloc( GetProcessHeap(), 0, fm->num_ids * sizeof(KNOWNFOLDERID) );
3945 if (!fm->ids)
3947 HeapFree( GetProcessHeap(), 0, fm );
3948 return E_OUTOFMEMORY;
3950 for (i = j = 0; i < sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]); i++)
3952 if (!IsEqualGUID( CSIDL_Data[i].id, &GUID_NULL ))
3954 fm->ids[j] = *CSIDL_Data[i].id;
3955 j++;
3958 TRACE("found %u known folders\n", fm->num_ids);
3959 *ppv = &fm->IKnownFolderManager_iface;
3961 TRACE("returning iface %p\n", *ppv);
3962 return S_OK;
3965 HRESULT WINAPI KnownFolderManager_Constructor( IUnknown *punk, REFIID riid, void **ppv )
3967 TRACE("%p, %s, %p\n", punk, debugstr_guid(riid), ppv);
3969 if (!ppv)
3970 return E_POINTER;
3971 if (punk)
3972 return CLASS_E_NOAGGREGATION;
3974 return foldermanager_create( ppv );