gdi32: Make clip_line external.
[wine/multimedia.git] / dlls / shell32 / shellpath.c
blob25ba4be173490ea4850f2a100168d37d8a7487d1
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(
673 LPSTR lpszPath,
674 LPCSTR *alpszPaths,
675 DWORD dwFlags)
677 FIXME("(%s,%p,0x%08x),stub!\n",
678 lpszPath, *alpszPaths, dwFlags);
679 return 0;
682 static BOOL PathResolveW(
683 LPWSTR lpszPath,
684 LPCWSTR *alpszPaths,
685 DWORD dwFlags)
687 FIXME("(%s,%p,0x%08x),stub!\n",
688 debugstr_w(lpszPath), debugstr_w(*alpszPaths), dwFlags);
689 return 0;
692 /*************************************************************************
693 * PathResolve [SHELL32.51]
695 BOOL WINAPI PathResolveAW(
696 LPVOID lpszPath,
697 LPCVOID *alpszPaths,
698 DWORD dwFlags)
700 if (SHELL_OsIsUnicode())
701 return PathResolveW(lpszPath, (LPCWSTR*)alpszPaths, dwFlags);
702 return PathResolveA(lpszPath, (LPCSTR*)alpszPaths, dwFlags);
705 /*************************************************************************
706 * PathProcessCommandA
708 static LONG PathProcessCommandA (
709 LPCSTR lpszPath,
710 LPSTR lpszBuff,
711 DWORD dwBuffSize,
712 DWORD dwFlags)
714 FIXME("%s %p 0x%04x 0x%04x stub\n",
715 lpszPath, lpszBuff, dwBuffSize, dwFlags);
716 if(!lpszPath) return -1;
717 if(lpszBuff) strcpy(lpszBuff, lpszPath);
718 return strlen(lpszPath);
721 /*************************************************************************
722 * PathProcessCommandW
724 static LONG PathProcessCommandW (
725 LPCWSTR lpszPath,
726 LPWSTR lpszBuff,
727 DWORD dwBuffSize,
728 DWORD dwFlags)
730 FIXME("(%s, %p, 0x%04x, 0x%04x) stub\n",
731 debugstr_w(lpszPath), lpszBuff, dwBuffSize, dwFlags);
732 if(!lpszPath) return -1;
733 if(lpszBuff) strcpyW(lpszBuff, lpszPath);
734 return strlenW(lpszPath);
737 /*************************************************************************
738 * PathProcessCommand (SHELL32.653)
740 LONG WINAPI PathProcessCommandAW (
741 LPCVOID lpszPath,
742 LPVOID lpszBuff,
743 DWORD dwBuffSize,
744 DWORD dwFlags)
746 if (SHELL_OsIsUnicode())
747 return PathProcessCommandW(lpszPath, lpszBuff, dwBuffSize, dwFlags);
748 return PathProcessCommandA(lpszPath, lpszBuff, dwBuffSize, dwFlags);
752 ########## special ##########
755 /*************************************************************************
756 * PathSetDlgItemPath (SHELL32.48)
758 VOID WINAPI PathSetDlgItemPathAW(HWND hDlg, int id, LPCVOID pszPath)
760 if (SHELL_OsIsUnicode())
761 PathSetDlgItemPathW(hDlg, id, pszPath);
762 else
763 PathSetDlgItemPathA(hDlg, id, pszPath);
766 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'};
767 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'};
768 static const WCHAR AppDataW[] = {'A','p','p','D','a','t','a','\0'};
769 static const WCHAR CacheW[] = {'C','a','c','h','e','\0'};
770 static const WCHAR CD_BurningW[] = {'C','D',' ','B','u','r','n','i','n','g','\0'};
771 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'};
772 static const WCHAR Common_AppDataW[] = {'C','o','m','m','o','n',' ','A','p','p','D','a','t','a','\0'};
773 static const WCHAR Common_DesktopW[] = {'C','o','m','m','o','n',' ','D','e','s','k','t','o','p','\0'};
774 static const WCHAR Common_DocumentsW[] = {'C','o','m','m','o','n',' ','D','o','c','u','m','e','n','t','s','\0'};
775 static const WCHAR Common_FavoritesW[] = {'C','o','m','m','o','n',' ','F','a','v','o','r','i','t','e','s','\0'};
776 static const WCHAR CommonFilesDirW[] = {'C','o','m','m','o','n','F','i','l','e','s','D','i','r','\0'};
777 static const WCHAR CommonFilesDirX86W[] = {'C','o','m','m','o','n','F','i','l','e','s','D','i','r',' ','(','x','8','6',')','\0'};
778 static const WCHAR CommonMusicW[] = {'C','o','m','m','o','n','M','u','s','i','c','\0'};
779 static const WCHAR CommonPicturesW[] = {'C','o','m','m','o','n','P','i','c','t','u','r','e','s','\0'};
780 static const WCHAR Common_ProgramsW[] = {'C','o','m','m','o','n',' ','P','r','o','g','r','a','m','s','\0'};
781 static const WCHAR Common_StartUpW[] = {'C','o','m','m','o','n',' ','S','t','a','r','t','U','p','\0'};
782 static const WCHAR Common_Start_MenuW[] = {'C','o','m','m','o','n',' ','S','t','a','r','t',' ','M','e','n','u','\0'};
783 static const WCHAR Common_TemplatesW[] = {'C','o','m','m','o','n',' ','T','e','m','p','l','a','t','e','s','\0'};
784 static const WCHAR CommonVideoW[] = {'C','o','m','m','o','n','V','i','d','e','o','\0'};
785 static const WCHAR ContactsW[] = {'C','o','n','t','a','c','t','s','\0'};
786 static const WCHAR CookiesW[] = {'C','o','o','k','i','e','s','\0'};
787 static const WCHAR DesktopW[] = {'D','e','s','k','t','o','p','\0'};
788 static const WCHAR FavoritesW[] = {'F','a','v','o','r','i','t','e','s','\0'};
789 static const WCHAR FontsW[] = {'F','o','n','t','s','\0'};
790 static const WCHAR HistoryW[] = {'H','i','s','t','o','r','y','\0'};
791 static const WCHAR Local_AppDataW[] = {'L','o','c','a','l',' ','A','p','p','D','a','t','a','\0'};
792 static const WCHAR My_MusicW[] = {'M','y',' ','M','u','s','i','c','\0'};
793 static const WCHAR My_PicturesW[] = {'M','y',' ','P','i','c','t','u','r','e','s','\0'};
794 static const WCHAR My_VideoW[] = {'M','y',' ','V','i','d','e','o','s','\0'};
795 static const WCHAR NetHoodW[] = {'N','e','t','H','o','o','d','\0'};
796 static const WCHAR PersonalW[] = {'P','e','r','s','o','n','a','l','\0'};
797 static const WCHAR PrintHoodW[] = {'P','r','i','n','t','H','o','o','d','\0'};
798 static const WCHAR ProgramFilesDirW[] = {'P','r','o','g','r','a','m','F','i','l','e','s','D','i','r','\0'};
799 static const WCHAR ProgramFilesDirX86W[] = {'P','r','o','g','r','a','m','F','i','l','e','s','D','i','r',' ','(','x','8','6',')','\0'};
800 static const WCHAR ProgramsW[] = {'P','r','o','g','r','a','m','s','\0'};
801 static const WCHAR RecentW[] = {'R','e','c','e','n','t','\0'};
802 static const WCHAR ResourcesW[] = {'R','e','s','o','u','r','c','e','s','\0'};
803 static const WCHAR SendToW[] = {'S','e','n','d','T','o','\0'};
804 static const WCHAR StartUpW[] = {'S','t','a','r','t','U','p','\0'};
805 static const WCHAR Start_MenuW[] = {'S','t','a','r','t',' ','M','e','n','u','\0'};
806 static const WCHAR TemplatesW[] = {'T','e','m','p','l','a','t','e','s','\0'};
807 static const WCHAR UsersW[] = {'U','s','e','r','s','\0'};
808 static const WCHAR UsersPublicW[] = {'U','s','e','r','s','\\','P','u','b','l','i','c','\0'};
809 static const WCHAR DefaultW[] = {'.','D','e','f','a','u','l','t','\0'};
810 static const WCHAR AllUsersProfileW[] = {'%','A','L','L','U','S','E','R','S','P','R','O','F','I','L','E','%','\0'};
811 static const WCHAR UserProfileW[] = {'%','U','S','E','R','P','R','O','F','I','L','E','%','\0'};
812 static const WCHAR SystemDriveW[] = {'%','S','y','s','t','e','m','D','r','i','v','e','%','\0'};
813 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};
814 static const WCHAR ProfilesDirectoryW[] = {'P','r','o','f','i','l','e','s','D','i','r','e','c','t','o','r','y',0};
815 static const WCHAR AllUsersProfileValueW[] = {'A','l','l','U','s','e','r','s','P','r','o','f','i','l','e','\0'};
816 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'};
817 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'};
818 static const WCHAR szDefaultProfileDirW[] = {'u','s','e','r','s',0};
819 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'};
820 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};
821 static const WCHAR AllUsersW[] = {'P','u','b','l','i','c',0};
823 typedef enum _CSIDL_Type {
824 CSIDL_Type_User,
825 CSIDL_Type_AllUsers,
826 CSIDL_Type_CurrVer,
827 CSIDL_Type_Disallowed,
828 CSIDL_Type_NonExistent,
829 CSIDL_Type_WindowsPath,
830 CSIDL_Type_SystemPath,
831 CSIDL_Type_SystemX86Path,
832 } CSIDL_Type;
834 typedef struct
836 const KNOWNFOLDERID *id;
837 CSIDL_Type type;
838 LPCWSTR szValueName;
839 LPCWSTR szDefaultPath; /* fallback string or resource ID */
840 } CSIDL_DATA;
842 static const CSIDL_DATA CSIDL_Data[] =
844 { /* 0x00 - CSIDL_DESKTOP */
845 &FOLDERID_Desktop,
846 CSIDL_Type_User,
847 DesktopW,
848 MAKEINTRESOURCEW(IDS_DESKTOPDIRECTORY)
850 { /* 0x01 - CSIDL_INTERNET */
851 &FOLDERID_InternetFolder,
852 CSIDL_Type_Disallowed,
853 NULL,
854 NULL
856 { /* 0x02 - CSIDL_PROGRAMS */
857 &FOLDERID_Programs,
858 CSIDL_Type_User,
859 ProgramsW,
860 MAKEINTRESOURCEW(IDS_PROGRAMS)
862 { /* 0x03 - CSIDL_CONTROLS (.CPL files) */
863 &FOLDERID_ControlPanelFolder,
864 CSIDL_Type_SystemPath,
865 NULL,
866 NULL
868 { /* 0x04 - CSIDL_PRINTERS */
869 &FOLDERID_PrintersFolder,
870 CSIDL_Type_SystemPath,
871 NULL,
872 NULL
874 { /* 0x05 - CSIDL_PERSONAL */
875 &FOLDERID_Documents,
876 CSIDL_Type_User,
877 PersonalW,
878 MAKEINTRESOURCEW(IDS_PERSONAL)
880 { /* 0x06 - CSIDL_FAVORITES */
881 &FOLDERID_Favorites,
882 CSIDL_Type_User,
883 FavoritesW,
884 MAKEINTRESOURCEW(IDS_FAVORITES)
886 { /* 0x07 - CSIDL_STARTUP */
887 &FOLDERID_Startup,
888 CSIDL_Type_User,
889 StartUpW,
890 MAKEINTRESOURCEW(IDS_STARTUP)
892 { /* 0x08 - CSIDL_RECENT */
893 &FOLDERID_Recent,
894 CSIDL_Type_User,
895 RecentW,
896 MAKEINTRESOURCEW(IDS_RECENT)
898 { /* 0x09 - CSIDL_SENDTO */
899 &FOLDERID_SendTo,
900 CSIDL_Type_User,
901 SendToW,
902 MAKEINTRESOURCEW(IDS_SENDTO)
904 { /* 0x0a - CSIDL_BITBUCKET - Recycle Bin */
905 &FOLDERID_RecycleBinFolder,
906 CSIDL_Type_Disallowed,
907 NULL,
908 NULL,
910 { /* 0x0b - CSIDL_STARTMENU */
911 &FOLDERID_StartMenu,
912 CSIDL_Type_User,
913 Start_MenuW,
914 MAKEINTRESOURCEW(IDS_STARTMENU)
916 { /* 0x0c - CSIDL_MYDOCUMENTS */
917 &GUID_NULL,
918 CSIDL_Type_Disallowed, /* matches WinXP--can't get its path */
919 NULL,
920 NULL
922 { /* 0x0d - CSIDL_MYMUSIC */
923 &FOLDERID_Music,
924 CSIDL_Type_User,
925 My_MusicW,
926 MAKEINTRESOURCEW(IDS_MYMUSIC)
928 { /* 0x0e - CSIDL_MYVIDEO */
929 &FOLDERID_Videos,
930 CSIDL_Type_User,
931 My_VideoW,
932 MAKEINTRESOURCEW(IDS_MYVIDEO)
934 { /* 0x0f - unassigned */
935 &GUID_NULL,
936 CSIDL_Type_Disallowed,
937 NULL,
938 NULL,
940 { /* 0x10 - CSIDL_DESKTOPDIRECTORY */
941 &FOLDERID_Desktop,
942 CSIDL_Type_User,
943 DesktopW,
944 MAKEINTRESOURCEW(IDS_DESKTOPDIRECTORY)
946 { /* 0x11 - CSIDL_DRIVES */
947 &FOLDERID_ComputerFolder,
948 CSIDL_Type_Disallowed,
949 NULL,
950 NULL,
952 { /* 0x12 - CSIDL_NETWORK */
953 &FOLDERID_NetworkFolder,
954 CSIDL_Type_Disallowed,
955 NULL,
956 NULL,
958 { /* 0x13 - CSIDL_NETHOOD */
959 &FOLDERID_NetHood,
960 CSIDL_Type_User,
961 NetHoodW,
962 MAKEINTRESOURCEW(IDS_NETHOOD)
964 { /* 0x14 - CSIDL_FONTS */
965 &FOLDERID_Fonts,
966 CSIDL_Type_WindowsPath,
967 FontsW,
968 FontsW
970 { /* 0x15 - CSIDL_TEMPLATES */
971 &FOLDERID_Templates,
972 CSIDL_Type_User,
973 TemplatesW,
974 MAKEINTRESOURCEW(IDS_TEMPLATES)
976 { /* 0x16 - CSIDL_COMMON_STARTMENU */
977 &FOLDERID_CommonStartMenu,
978 CSIDL_Type_AllUsers,
979 Common_Start_MenuW,
980 MAKEINTRESOURCEW(IDS_STARTMENU)
982 { /* 0x17 - CSIDL_COMMON_PROGRAMS */
983 &FOLDERID_CommonPrograms,
984 CSIDL_Type_AllUsers,
985 Common_ProgramsW,
986 MAKEINTRESOURCEW(IDS_PROGRAMS)
988 { /* 0x18 - CSIDL_COMMON_STARTUP */
989 &FOLDERID_CommonStartup,
990 CSIDL_Type_AllUsers,
991 Common_StartUpW,
992 MAKEINTRESOURCEW(IDS_STARTUP)
994 { /* 0x19 - CSIDL_COMMON_DESKTOPDIRECTORY */
995 &FOLDERID_PublicDesktop,
996 CSIDL_Type_AllUsers,
997 Common_DesktopW,
998 MAKEINTRESOURCEW(IDS_DESKTOPDIRECTORY)
1000 { /* 0x1a - CSIDL_APPDATA */
1001 &FOLDERID_RoamingAppData,
1002 CSIDL_Type_User,
1003 AppDataW,
1004 MAKEINTRESOURCEW(IDS_APPDATA)
1006 { /* 0x1b - CSIDL_PRINTHOOD */
1007 &FOLDERID_PrintHood,
1008 CSIDL_Type_User,
1009 PrintHoodW,
1010 MAKEINTRESOURCEW(IDS_PRINTHOOD)
1012 { /* 0x1c - CSIDL_LOCAL_APPDATA */
1013 &FOLDERID_LocalAppData,
1014 CSIDL_Type_User,
1015 Local_AppDataW,
1016 MAKEINTRESOURCEW(IDS_LOCAL_APPDATA)
1018 { /* 0x1d - CSIDL_ALTSTARTUP */
1019 &GUID_NULL,
1020 CSIDL_Type_NonExistent,
1021 NULL,
1022 NULL
1024 { /* 0x1e - CSIDL_COMMON_ALTSTARTUP */
1025 &GUID_NULL,
1026 CSIDL_Type_NonExistent,
1027 NULL,
1028 NULL
1030 { /* 0x1f - CSIDL_COMMON_FAVORITES */
1031 &FOLDERID_Favorites,
1032 CSIDL_Type_AllUsers,
1033 Common_FavoritesW,
1034 MAKEINTRESOURCEW(IDS_FAVORITES)
1036 { /* 0x20 - CSIDL_INTERNET_CACHE */
1037 &FOLDERID_InternetCache,
1038 CSIDL_Type_User,
1039 CacheW,
1040 MAKEINTRESOURCEW(IDS_INTERNET_CACHE)
1042 { /* 0x21 - CSIDL_COOKIES */
1043 &FOLDERID_Cookies,
1044 CSIDL_Type_User,
1045 CookiesW,
1046 MAKEINTRESOURCEW(IDS_COOKIES)
1048 { /* 0x22 - CSIDL_HISTORY */
1049 &FOLDERID_History,
1050 CSIDL_Type_User,
1051 HistoryW,
1052 MAKEINTRESOURCEW(IDS_HISTORY)
1054 { /* 0x23 - CSIDL_COMMON_APPDATA */
1055 &FOLDERID_ProgramData,
1056 CSIDL_Type_AllUsers,
1057 Common_AppDataW,
1058 MAKEINTRESOURCEW(IDS_APPDATA)
1060 { /* 0x24 - CSIDL_WINDOWS */
1061 &FOLDERID_Windows,
1062 CSIDL_Type_WindowsPath,
1063 NULL,
1064 NULL
1066 { /* 0x25 - CSIDL_SYSTEM */
1067 &FOLDERID_System,
1068 CSIDL_Type_SystemPath,
1069 NULL,
1070 NULL
1072 { /* 0x26 - CSIDL_PROGRAM_FILES */
1073 &FOLDERID_ProgramFiles,
1074 CSIDL_Type_CurrVer,
1075 ProgramFilesDirW,
1076 MAKEINTRESOURCEW(IDS_PROGRAM_FILES)
1078 { /* 0x27 - CSIDL_MYPICTURES */
1079 &FOLDERID_Pictures,
1080 CSIDL_Type_User,
1081 My_PicturesW,
1082 MAKEINTRESOURCEW(IDS_MYPICTURES)
1084 { /* 0x28 - CSIDL_PROFILE */
1085 &FOLDERID_Profile,
1086 CSIDL_Type_User,
1087 NULL,
1088 NULL
1090 { /* 0x29 - CSIDL_SYSTEMX86 */
1091 &FOLDERID_SystemX86,
1092 CSIDL_Type_SystemX86Path,
1093 NULL,
1094 NULL
1096 { /* 0x2a - CSIDL_PROGRAM_FILESX86 */
1097 &FOLDERID_ProgramFilesX86,
1098 CSIDL_Type_CurrVer,
1099 ProgramFilesDirX86W,
1100 MAKEINTRESOURCEW(IDS_PROGRAM_FILESX86)
1102 { /* 0x2b - CSIDL_PROGRAM_FILES_COMMON */
1103 &FOLDERID_ProgramFilesCommon,
1104 CSIDL_Type_CurrVer,
1105 CommonFilesDirW,
1106 MAKEINTRESOURCEW(IDS_PROGRAM_FILES_COMMON)
1108 { /* 0x2c - CSIDL_PROGRAM_FILES_COMMONX86 */
1109 &FOLDERID_ProgramFilesCommonX86,
1110 CSIDL_Type_CurrVer,
1111 CommonFilesDirX86W,
1112 MAKEINTRESOURCEW(IDS_PROGRAM_FILES_COMMONX86)
1114 { /* 0x2d - CSIDL_COMMON_TEMPLATES */
1115 &FOLDERID_CommonTemplates,
1116 CSIDL_Type_AllUsers,
1117 Common_TemplatesW,
1118 MAKEINTRESOURCEW(IDS_TEMPLATES)
1120 { /* 0x2e - CSIDL_COMMON_DOCUMENTS */
1121 &FOLDERID_PublicDocuments,
1122 CSIDL_Type_AllUsers,
1123 Common_DocumentsW,
1124 MAKEINTRESOURCEW(IDS_COMMON_DOCUMENTS)
1126 { /* 0x2f - CSIDL_COMMON_ADMINTOOLS */
1127 &FOLDERID_CommonAdminTools,
1128 CSIDL_Type_AllUsers,
1129 Common_Administrative_ToolsW,
1130 MAKEINTRESOURCEW(IDS_ADMINTOOLS)
1132 { /* 0x30 - CSIDL_ADMINTOOLS */
1133 &FOLDERID_AdminTools,
1134 CSIDL_Type_User,
1135 Administrative_ToolsW,
1136 MAKEINTRESOURCEW(IDS_ADMINTOOLS)
1138 { /* 0x31 - CSIDL_CONNECTIONS */
1139 &FOLDERID_ConnectionsFolder,
1140 CSIDL_Type_Disallowed,
1141 NULL,
1142 NULL
1144 { /* 0x32 - unassigned */
1145 &GUID_NULL,
1146 CSIDL_Type_Disallowed,
1147 NULL,
1148 NULL
1150 { /* 0x33 - unassigned */
1151 &GUID_NULL,
1152 CSIDL_Type_Disallowed,
1153 NULL,
1154 NULL
1156 { /* 0x34 - unassigned */
1157 &GUID_NULL,
1158 CSIDL_Type_Disallowed,
1159 NULL,
1160 NULL
1162 { /* 0x35 - CSIDL_COMMON_MUSIC */
1163 &FOLDERID_PublicMusic,
1164 CSIDL_Type_AllUsers,
1165 CommonMusicW,
1166 MAKEINTRESOURCEW(IDS_COMMON_MUSIC)
1168 { /* 0x36 - CSIDL_COMMON_PICTURES */
1169 &FOLDERID_PublicPictures,
1170 CSIDL_Type_AllUsers,
1171 CommonPicturesW,
1172 MAKEINTRESOURCEW(IDS_COMMON_PICTURES)
1174 { /* 0x37 - CSIDL_COMMON_VIDEO */
1175 &FOLDERID_PublicVideos,
1176 CSIDL_Type_AllUsers,
1177 CommonVideoW,
1178 MAKEINTRESOURCEW(IDS_COMMON_VIDEO)
1180 { /* 0x38 - CSIDL_RESOURCES */
1181 &FOLDERID_ResourceDir,
1182 CSIDL_Type_WindowsPath,
1183 NULL,
1184 ResourcesW
1186 { /* 0x39 - CSIDL_RESOURCES_LOCALIZED */
1187 &FOLDERID_LocalizedResourcesDir,
1188 CSIDL_Type_NonExistent,
1189 NULL,
1190 NULL
1192 { /* 0x3a - CSIDL_COMMON_OEM_LINKS */
1193 &FOLDERID_CommonOEMLinks,
1194 CSIDL_Type_AllUsers,
1195 NULL,
1196 MAKEINTRESOURCEW(IDS_COMMON_OEM_LINKS)
1198 { /* 0x3b - CSIDL_CDBURN_AREA */
1199 &FOLDERID_CDBurning,
1200 CSIDL_Type_User,
1201 CD_BurningW,
1202 MAKEINTRESOURCEW(IDS_CDBURN_AREA)
1204 { /* 0x3c unassigned */
1205 &GUID_NULL,
1206 CSIDL_Type_Disallowed,
1207 NULL,
1208 NULL
1210 { /* 0x3d - CSIDL_COMPUTERSNEARME */
1211 &GUID_NULL,
1212 CSIDL_Type_Disallowed, /* FIXME */
1213 NULL,
1214 NULL
1216 { /* 0x3e - CSIDL_PROFILES */
1217 &GUID_NULL,
1218 CSIDL_Type_Disallowed, /* oddly, this matches WinXP */
1219 NULL,
1220 NULL
1222 { /* 0x3f */
1223 &FOLDERID_AddNewPrograms,
1224 CSIDL_Type_Disallowed,
1225 NULL,
1226 NULL
1228 { /* 0x40 */
1229 &FOLDERID_AppUpdates,
1230 CSIDL_Type_Disallowed,
1231 NULL,
1232 NULL
1234 { /* 0x41 */
1235 &FOLDERID_ChangeRemovePrograms,
1236 CSIDL_Type_Disallowed,
1237 NULL,
1238 NULL
1240 { /* 0x42 */
1241 &FOLDERID_ConflictFolder,
1242 CSIDL_Type_Disallowed,
1243 NULL,
1244 NULL
1246 { /* 0x43 */
1247 &FOLDERID_Contacts,
1248 CSIDL_Type_User,
1249 ContactsW,
1250 MAKEINTRESOURCEW(IDS_CONTACTS)
1252 { /* 0x44 */
1253 &FOLDERID_DeviceMetadataStore,
1254 CSIDL_Type_Disallowed, /* FIXME */
1255 NULL,
1256 NULL
1258 { /* 0x45 */
1259 &GUID_NULL,
1260 CSIDL_Type_User,
1261 NULL,
1262 MAKEINTRESOURCEW(IDS_DOCUMENTS)
1264 { /* 0x46 */
1265 &FOLDERID_DocumentsLibrary,
1266 CSIDL_Type_Disallowed, /* FIXME */
1267 NULL,
1268 NULL
1270 { /* 0x47 */
1271 &FOLDERID_Downloads,
1272 CSIDL_Type_User,
1273 NULL,
1274 MAKEINTRESOURCEW(IDS_DOWNLOADS)
1276 { /* 0x48 */
1277 &FOLDERID_Games,
1278 CSIDL_Type_Disallowed,
1279 NULL,
1280 NULL
1282 { /* 0x49 */
1283 &FOLDERID_GameTasks,
1284 CSIDL_Type_Disallowed, /* FIXME */
1285 NULL,
1286 NULL
1288 { /* 0x4a */
1289 &FOLDERID_HomeGroup,
1290 CSIDL_Type_Disallowed,
1291 NULL,
1292 NULL
1294 { /* 0x4b */
1295 &FOLDERID_ImplicitAppShortcuts,
1296 CSIDL_Type_Disallowed, /* FIXME */
1297 NULL,
1298 NULL
1300 { /* 0x4c */
1301 &FOLDERID_Libraries,
1302 CSIDL_Type_Disallowed, /* FIXME */
1303 NULL,
1304 NULL
1306 { /* 0x4d */
1307 &FOLDERID_Links,
1308 CSIDL_Type_User,
1309 NULL,
1310 MAKEINTRESOURCEW(IDS_LINKS)
1312 { /* 0x4e */
1313 &FOLDERID_LocalAppDataLow,
1314 CSIDL_Type_User,
1315 NULL,
1316 MAKEINTRESOURCEW(IDS_LOCAL_APPDATA_LOW)
1318 { /* 0x4f */
1319 &FOLDERID_MusicLibrary,
1320 CSIDL_Type_Disallowed, /* FIXME */
1321 NULL,
1322 NULL
1324 { /* 0x50 */
1325 &FOLDERID_OriginalImages,
1326 CSIDL_Type_Disallowed, /* FIXME */
1327 NULL,
1328 NULL
1330 { /* 0x51 */
1331 &FOLDERID_PhotoAlbums,
1332 CSIDL_Type_User,
1333 NULL,
1334 MAKEINTRESOURCEW(IDS_PHOTO_ALBUMS)
1336 { /* 0x52 */
1337 &FOLDERID_PicturesLibrary,
1338 CSIDL_Type_Disallowed, /* FIXME */
1339 NULL,
1340 NULL
1342 { /* 0x53 */
1343 &FOLDERID_Playlists,
1344 CSIDL_Type_User,
1345 NULL,
1346 MAKEINTRESOURCEW(IDS_PLAYLISTS)
1348 { /* 0x54 */
1349 &FOLDERID_ProgramFilesX64,
1350 CSIDL_Type_NonExistent,
1351 NULL,
1352 NULL
1354 { /* 0x55 */
1355 &FOLDERID_ProgramFilesCommonX64,
1356 CSIDL_Type_NonExistent,
1357 NULL,
1358 NULL
1360 { /* 0x56 */
1361 &FOLDERID_Public,
1362 CSIDL_Type_CurrVer, /* FIXME */
1363 NULL,
1364 UsersPublicW
1366 { /* 0x57 */
1367 &FOLDERID_PublicDownloads,
1368 CSIDL_Type_AllUsers,
1369 NULL,
1370 MAKEINTRESOURCEW(IDS_PUBLIC_DOWNLOADS)
1372 { /* 0x58 */
1373 &FOLDERID_PublicGameTasks,
1374 CSIDL_Type_AllUsers,
1375 NULL,
1376 MAKEINTRESOURCEW(IDS_PUBLIC_GAME_TASKS)
1378 { /* 0x59 */
1379 &FOLDERID_PublicLibraries,
1380 CSIDL_Type_AllUsers,
1381 NULL,
1382 MAKEINTRESOURCEW(IDS_PUBLIC_LIBRARIES)
1384 { /* 0x5a */
1385 &FOLDERID_PublicRingtones,
1386 CSIDL_Type_AllUsers,
1387 NULL,
1388 MAKEINTRESOURCEW(IDS_PUBLIC_RINGTONES)
1390 { /* 0x5b */
1391 &FOLDERID_QuickLaunch,
1392 CSIDL_Type_Disallowed, /* FIXME */
1393 NULL,
1394 NULL
1396 { /* 0x5c */
1397 &FOLDERID_RecordedTVLibrary,
1398 CSIDL_Type_Disallowed, /* FIXME */
1399 NULL,
1400 NULL
1402 { /* 0x5d */
1403 &FOLDERID_Ringtones,
1404 CSIDL_Type_Disallowed, /* FIXME */
1405 NULL,
1406 NULL
1408 { /* 0x5e */
1409 &FOLDERID_SampleMusic,
1410 CSIDL_Type_AllUsers,
1411 NULL,
1412 MAKEINTRESOURCEW(IDS_SAMPLE_MUSIC)
1414 { /* 0x5f */
1415 &FOLDERID_SamplePictures,
1416 CSIDL_Type_AllUsers,
1417 NULL,
1418 MAKEINTRESOURCEW(IDS_SAMPLE_PICTURES)
1420 { /* 0x60 */
1421 &FOLDERID_SamplePlaylists,
1422 CSIDL_Type_AllUsers,
1423 NULL,
1424 MAKEINTRESOURCEW(IDS_SAMPLE_PLAYLISTS)
1426 { /* 0x61 */
1427 &FOLDERID_SampleVideos,
1428 CSIDL_Type_AllUsers,
1429 NULL,
1430 MAKEINTRESOURCEW(IDS_SAMPLE_VIDEOS)
1432 { /* 0x62 */
1433 &FOLDERID_SavedGames,
1434 CSIDL_Type_User,
1435 NULL,
1436 MAKEINTRESOURCEW(IDS_SAVED_GAMES)
1438 { /* 0x63 */
1439 &FOLDERID_SavedSearches,
1440 CSIDL_Type_User,
1441 NULL,
1442 MAKEINTRESOURCEW(IDS_SAVED_SEARCHES)
1444 { /* 0x64 */
1445 &FOLDERID_SEARCH_CSC,
1446 CSIDL_Type_Disallowed,
1447 NULL,
1448 NULL
1450 { /* 0x65 */
1451 &FOLDERID_SEARCH_MAPI,
1452 CSIDL_Type_Disallowed,
1453 NULL,
1454 NULL
1456 { /* 0x66 */
1457 &FOLDERID_SearchHome,
1458 CSIDL_Type_Disallowed,
1459 NULL,
1460 NULL
1462 { /* 0x67 */
1463 &FOLDERID_SidebarDefaultParts,
1464 CSIDL_Type_Disallowed, /* FIXME */
1465 NULL,
1466 NULL
1468 { /* 0x68 */
1469 &FOLDERID_SidebarParts,
1470 CSIDL_Type_Disallowed, /* FIXME */
1471 NULL,
1472 NULL
1474 { /* 0x69 */
1475 &FOLDERID_SyncManagerFolder,
1476 CSIDL_Type_Disallowed,
1477 NULL,
1478 NULL
1480 { /* 0x6a */
1481 &FOLDERID_SyncResultsFolder,
1482 CSIDL_Type_Disallowed,
1483 NULL,
1484 NULL
1486 { /* 0x6b */
1487 &FOLDERID_SyncSetupFolder,
1488 CSIDL_Type_Disallowed,
1489 NULL,
1490 NULL
1492 { /* 0x6c */
1493 &FOLDERID_UserPinned,
1494 CSIDL_Type_Disallowed, /* FIXME */
1495 NULL,
1496 NULL
1498 { /* 0x6d */
1499 &FOLDERID_UserProfiles,
1500 CSIDL_Type_CurrVer,
1501 UsersW,
1502 MAKEINTRESOURCEW(IDS_USER_PROFILES)
1504 { /* 0x6e */
1505 &FOLDERID_UserProgramFiles,
1506 CSIDL_Type_Disallowed, /* FIXME */
1507 NULL,
1508 NULL
1510 { /* 0x6f */
1511 &FOLDERID_UserProgramFilesCommon,
1512 CSIDL_Type_Disallowed, /* FIXME */
1513 NULL,
1514 NULL
1516 { /* 0x70 */
1517 &FOLDERID_UsersFiles,
1518 CSIDL_Type_Disallowed,
1519 NULL,
1520 NULL
1522 { /* 0x71 */
1523 &FOLDERID_UsersLibraries,
1524 CSIDL_Type_Disallowed,
1525 NULL,
1526 NULL
1528 { /* 0x72 */
1529 &FOLDERID_VideosLibrary,
1530 CSIDL_Type_Disallowed, /* FIXME */
1531 NULL,
1532 NULL
1536 static HRESULT _SHExpandEnvironmentStrings(LPCWSTR szSrc, LPWSTR szDest);
1538 /* Gets the value named value from the registry key
1539 * rootKey\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
1540 * (or from rootKey\userPrefix\... if userPrefix is not NULL) into path, which
1541 * is assumed to be MAX_PATH WCHARs in length.
1542 * If it exists, expands the value and writes the expanded value to
1543 * rootKey\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders
1544 * Returns successful error code if the value was retrieved from the registry,
1545 * and a failure otherwise.
1547 static HRESULT _SHGetUserShellFolderPath(HKEY rootKey, LPCWSTR userPrefix,
1548 LPCWSTR value, LPWSTR path)
1550 HRESULT hr;
1551 WCHAR shellFolderPath[MAX_PATH], userShellFolderPath[MAX_PATH];
1552 LPCWSTR pShellFolderPath, pUserShellFolderPath;
1553 DWORD dwType, dwPathLen = MAX_PATH;
1554 HKEY userShellFolderKey, shellFolderKey;
1556 TRACE("%p,%s,%s,%p\n",rootKey, debugstr_w(userPrefix), debugstr_w(value),
1557 path);
1559 if (userPrefix)
1561 strcpyW(shellFolderPath, userPrefix);
1562 PathAddBackslashW(shellFolderPath);
1563 strcatW(shellFolderPath, szSHFolders);
1564 pShellFolderPath = shellFolderPath;
1565 strcpyW(userShellFolderPath, userPrefix);
1566 PathAddBackslashW(userShellFolderPath);
1567 strcatW(userShellFolderPath, szSHUserFolders);
1568 pUserShellFolderPath = userShellFolderPath;
1570 else
1572 pUserShellFolderPath = szSHUserFolders;
1573 pShellFolderPath = szSHFolders;
1576 if (RegCreateKeyW(rootKey, pShellFolderPath, &shellFolderKey))
1578 TRACE("Failed to create %s\n", debugstr_w(pShellFolderPath));
1579 return E_FAIL;
1581 if (RegCreateKeyW(rootKey, pUserShellFolderPath, &userShellFolderKey))
1583 TRACE("Failed to create %s\n",
1584 debugstr_w(pUserShellFolderPath));
1585 RegCloseKey(shellFolderKey);
1586 return E_FAIL;
1589 if (!RegQueryValueExW(userShellFolderKey, value, NULL, &dwType,
1590 (LPBYTE)path, &dwPathLen) && (dwType == REG_EXPAND_SZ || dwType == REG_SZ))
1592 LONG ret;
1594 path[dwPathLen / sizeof(WCHAR)] = '\0';
1595 if (dwType == REG_EXPAND_SZ && path[0] == '%')
1597 WCHAR szTemp[MAX_PATH];
1599 _SHExpandEnvironmentStrings(path, szTemp);
1600 lstrcpynW(path, szTemp, MAX_PATH);
1602 ret = RegSetValueExW(shellFolderKey, value, 0, REG_SZ, (LPBYTE)path,
1603 (strlenW(path) + 1) * sizeof(WCHAR));
1604 if (ret != ERROR_SUCCESS)
1605 hr = HRESULT_FROM_WIN32(ret);
1606 else
1607 hr = S_OK;
1609 else
1610 hr = E_FAIL;
1611 RegCloseKey(shellFolderKey);
1612 RegCloseKey(userShellFolderKey);
1613 TRACE("returning 0x%08x\n", hr);
1614 return hr;
1617 /* Gets a 'semi-expanded' default value of the CSIDL with index folder into
1618 * pszPath, based on the entries in CSIDL_Data. By semi-expanded, I mean:
1619 * - The entry's szDefaultPath may be either a string value or an integer
1620 * resource identifier. In the latter case, the string value of the resource
1621 * is written.
1622 * - Depending on the entry's type, the path may begin with an (unexpanded)
1623 * environment variable name. The caller is responsible for expanding
1624 * environment strings if so desired.
1625 * The types that are prepended with environment variables are:
1626 * CSIDL_Type_User: %USERPROFILE%
1627 * CSIDL_Type_AllUsers: %ALLUSERSPROFILE%
1628 * CSIDL_Type_CurrVer: %SystemDrive%
1629 * (Others might make sense too, but as yet are unneeded.)
1631 static HRESULT _SHGetDefaultValue(BYTE folder, LPWSTR pszPath)
1633 HRESULT hr;
1634 WCHAR resourcePath[MAX_PATH];
1635 LPCWSTR pDefaultPath = NULL;
1637 TRACE("0x%02x,%p\n", folder, pszPath);
1639 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1640 return E_INVALIDARG;
1641 if (!pszPath)
1642 return E_INVALIDARG;
1644 if (!is_win64)
1646 BOOL is_wow64;
1648 switch (folder)
1650 case CSIDL_PROGRAM_FILES:
1651 case CSIDL_PROGRAM_FILESX86:
1652 IsWow64Process( GetCurrentProcess(), &is_wow64 );
1653 folder = is_wow64 ? CSIDL_PROGRAM_FILESX86 : CSIDL_PROGRAM_FILES;
1654 break;
1655 case CSIDL_PROGRAM_FILES_COMMON:
1656 case CSIDL_PROGRAM_FILES_COMMONX86:
1657 IsWow64Process( GetCurrentProcess(), &is_wow64 );
1658 folder = is_wow64 ? CSIDL_PROGRAM_FILES_COMMONX86 : CSIDL_PROGRAM_FILES_COMMON;
1659 break;
1663 if (CSIDL_Data[folder].szDefaultPath &&
1664 IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath))
1666 if (LoadStringW(shell32_hInstance,
1667 LOWORD(CSIDL_Data[folder].szDefaultPath), resourcePath, MAX_PATH))
1669 hr = S_OK;
1670 pDefaultPath = resourcePath;
1672 else
1674 FIXME("(%d,%s), LoadString failed, missing translation?\n", folder,
1675 debugstr_w(pszPath));
1676 hr = E_FAIL;
1679 else
1681 hr = S_OK;
1682 pDefaultPath = CSIDL_Data[folder].szDefaultPath;
1684 if (SUCCEEDED(hr))
1686 switch (CSIDL_Data[folder].type)
1688 case CSIDL_Type_User:
1689 strcpyW(pszPath, UserProfileW);
1690 break;
1691 case CSIDL_Type_AllUsers:
1692 strcpyW(pszPath, AllUsersProfileW);
1693 break;
1694 case CSIDL_Type_CurrVer:
1695 strcpyW(pszPath, SystemDriveW);
1696 break;
1697 default:
1698 ; /* no corresponding env. var, do nothing */
1700 if (pDefaultPath)
1702 PathAddBackslashW(pszPath);
1703 strcatW(pszPath, pDefaultPath);
1706 TRACE("returning 0x%08x\n", hr);
1707 return hr;
1710 /* Gets the (unexpanded) value of the folder with index folder into pszPath.
1711 * The folder's type is assumed to be CSIDL_Type_CurrVer. Its default value
1712 * can be overridden in the HKLM\\szCurrentVersion key.
1713 * If dwFlags has SHGFP_TYPE_DEFAULT set or if the value isn't overridden in
1714 * the registry, uses _SHGetDefaultValue to get the value.
1716 static HRESULT _SHGetCurrentVersionPath(DWORD dwFlags, BYTE folder,
1717 LPWSTR pszPath)
1719 HRESULT hr;
1721 TRACE("0x%08x,0x%02x,%p\n", dwFlags, folder, pszPath);
1723 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1724 return E_INVALIDARG;
1725 if (CSIDL_Data[folder].type != CSIDL_Type_CurrVer)
1726 return E_INVALIDARG;
1727 if (!pszPath)
1728 return E_INVALIDARG;
1730 if (dwFlags & SHGFP_TYPE_DEFAULT)
1731 hr = _SHGetDefaultValue(folder, pszPath);
1732 else
1734 HKEY hKey;
1736 if (RegCreateKeyW(HKEY_LOCAL_MACHINE, szCurrentVersion, &hKey))
1737 hr = E_FAIL;
1738 else
1740 DWORD dwType, dwPathLen = MAX_PATH * sizeof(WCHAR);
1742 if (RegQueryValueExW(hKey, CSIDL_Data[folder].szValueName, NULL,
1743 &dwType, (LPBYTE)pszPath, &dwPathLen) ||
1744 (dwType != REG_SZ && dwType != REG_EXPAND_SZ))
1746 hr = _SHGetDefaultValue(folder, pszPath);
1747 dwType = REG_EXPAND_SZ;
1748 switch (folder)
1750 case CSIDL_PROGRAM_FILESX86:
1751 case CSIDL_PROGRAM_FILES_COMMONX86:
1752 /* these two should never be set on 32-bit setups */
1753 if (!is_win64)
1755 BOOL is_wow64;
1756 IsWow64Process( GetCurrentProcess(), &is_wow64 );
1757 if (!is_wow64) break;
1759 /* fall through */
1760 default:
1761 RegSetValueExW(hKey, CSIDL_Data[folder].szValueName, 0, dwType,
1762 (LPBYTE)pszPath, (strlenW(pszPath)+1)*sizeof(WCHAR));
1765 else
1767 pszPath[dwPathLen / sizeof(WCHAR)] = '\0';
1768 hr = S_OK;
1770 RegCloseKey(hKey);
1773 TRACE("returning 0x%08x (output path is %s)\n", hr, debugstr_w(pszPath));
1774 return hr;
1777 static LPWSTR _GetUserSidStringFromToken(HANDLE Token)
1779 char InfoBuffer[64];
1780 PTOKEN_USER UserInfo;
1781 DWORD InfoSize;
1782 LPWSTR SidStr;
1784 UserInfo = (PTOKEN_USER) InfoBuffer;
1785 if (! GetTokenInformation(Token, TokenUser, InfoBuffer, sizeof(InfoBuffer),
1786 &InfoSize))
1788 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
1789 return NULL;
1790 UserInfo = HeapAlloc(GetProcessHeap(), 0, InfoSize);
1791 if (UserInfo == NULL)
1792 return NULL;
1793 if (! GetTokenInformation(Token, TokenUser, UserInfo, InfoSize,
1794 &InfoSize))
1796 HeapFree(GetProcessHeap(), 0, UserInfo);
1797 return NULL;
1801 if (! ConvertSidToStringSidW(UserInfo->User.Sid, &SidStr))
1802 SidStr = NULL;
1804 if (UserInfo != (PTOKEN_USER) InfoBuffer)
1805 HeapFree(GetProcessHeap(), 0, UserInfo);
1807 return SidStr;
1810 /* Gets the user's path (unexpanded) for the CSIDL with index folder:
1811 * If SHGFP_TYPE_DEFAULT is set, calls _SHGetDefaultValue for it. Otherwise
1812 * calls _SHGetUserShellFolderPath for it. Where it looks depends on hToken:
1813 * - if hToken is -1, looks in HKEY_USERS\.Default
1814 * - otherwise looks first in HKEY_CURRENT_USER, followed by HKEY_LOCAL_MACHINE
1815 * if HKEY_CURRENT_USER doesn't contain any entries. If both fail, finally
1816 * calls _SHGetDefaultValue for it.
1818 static HRESULT _SHGetUserProfilePath(HANDLE hToken, DWORD dwFlags, BYTE folder,
1819 LPWSTR pszPath)
1821 HRESULT hr;
1823 TRACE("%p,0x%08x,0x%02x,%p\n", hToken, dwFlags, folder, pszPath);
1825 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1826 return E_INVALIDARG;
1827 if (CSIDL_Data[folder].type != CSIDL_Type_User)
1828 return E_INVALIDARG;
1829 if (!pszPath)
1830 return E_INVALIDARG;
1832 if (dwFlags & SHGFP_TYPE_DEFAULT)
1834 if (hToken != NULL && hToken != (HANDLE)-1)
1836 FIXME("unsupported for user other than current or default\n");
1837 return E_FAIL;
1839 hr = _SHGetDefaultValue(folder, pszPath);
1841 else
1843 LPCWSTR userPrefix = NULL;
1844 HKEY hRootKey;
1846 if (hToken == (HANDLE)-1)
1848 hRootKey = HKEY_USERS;
1849 userPrefix = DefaultW;
1851 else if (hToken == NULL)
1852 hRootKey = HKEY_CURRENT_USER;
1853 else
1855 hRootKey = HKEY_USERS;
1856 userPrefix = _GetUserSidStringFromToken(hToken);
1857 if (userPrefix == NULL)
1859 hr = E_FAIL;
1860 goto error;
1863 hr = _SHGetUserShellFolderPath(hRootKey, userPrefix,
1864 CSIDL_Data[folder].szValueName, pszPath);
1865 if (FAILED(hr) && hRootKey != HKEY_LOCAL_MACHINE)
1866 hr = _SHGetUserShellFolderPath(HKEY_LOCAL_MACHINE, NULL,
1867 CSIDL_Data[folder].szValueName, pszPath);
1868 if (FAILED(hr))
1869 hr = _SHGetDefaultValue(folder, pszPath);
1870 if (userPrefix != NULL && userPrefix != DefaultW)
1871 LocalFree((HLOCAL) userPrefix);
1873 error:
1874 TRACE("returning 0x%08x (output path is %s)\n", hr, debugstr_w(pszPath));
1875 return hr;
1878 /* Gets the (unexpanded) path for the CSIDL with index folder. If dwFlags has
1879 * SHGFP_TYPE_DEFAULT set, calls _SHGetDefaultValue. Otherwise calls
1880 * _SHGetUserShellFolderPath for it, looking only in HKEY_LOCAL_MACHINE.
1881 * If this fails, falls back to _SHGetDefaultValue.
1883 static HRESULT _SHGetAllUsersProfilePath(DWORD dwFlags, BYTE folder,
1884 LPWSTR pszPath)
1886 HRESULT hr;
1888 TRACE("0x%08x,0x%02x,%p\n", dwFlags, folder, pszPath);
1890 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1891 return E_INVALIDARG;
1892 if (CSIDL_Data[folder].type != CSIDL_Type_AllUsers)
1893 return E_INVALIDARG;
1894 if (!pszPath)
1895 return E_INVALIDARG;
1897 if (dwFlags & SHGFP_TYPE_DEFAULT)
1898 hr = _SHGetDefaultValue(folder, pszPath);
1899 else
1901 hr = _SHGetUserShellFolderPath(HKEY_LOCAL_MACHINE, NULL,
1902 CSIDL_Data[folder].szValueName, pszPath);
1903 if (FAILED(hr))
1904 hr = _SHGetDefaultValue(folder, pszPath);
1906 TRACE("returning 0x%08x (output path is %s)\n", hr, debugstr_w(pszPath));
1907 return hr;
1910 static HRESULT _SHOpenProfilesKey(PHKEY pKey)
1912 LONG lRet;
1913 DWORD disp;
1915 lRet = RegCreateKeyExW(HKEY_LOCAL_MACHINE, ProfileListW, 0, NULL, 0,
1916 KEY_ALL_ACCESS, NULL, pKey, &disp);
1917 return HRESULT_FROM_WIN32(lRet);
1920 /* Reads the value named szValueName from the key profilesKey (assumed to be
1921 * opened by _SHOpenProfilesKey) into szValue, which is assumed to be MAX_PATH
1922 * WCHARs in length. If it doesn't exist, returns szDefault (and saves
1923 * szDefault to the registry).
1925 static HRESULT _SHGetProfilesValue(HKEY profilesKey, LPCWSTR szValueName,
1926 LPWSTR szValue, LPCWSTR szDefault)
1928 HRESULT hr;
1929 DWORD type, dwPathLen = MAX_PATH * sizeof(WCHAR);
1930 LONG lRet;
1932 TRACE("%p,%s,%p,%s\n", profilesKey, debugstr_w(szValueName), szValue,
1933 debugstr_w(szDefault));
1934 lRet = RegQueryValueExW(profilesKey, szValueName, NULL, &type,
1935 (LPBYTE)szValue, &dwPathLen);
1936 if (!lRet && (type == REG_SZ || type == REG_EXPAND_SZ) && dwPathLen
1937 && *szValue)
1939 dwPathLen /= sizeof(WCHAR);
1940 szValue[dwPathLen] = '\0';
1941 hr = S_OK;
1943 else
1945 /* Missing or invalid value, set a default */
1946 lstrcpynW(szValue, szDefault, MAX_PATH);
1947 TRACE("Setting missing value %s to %s\n", debugstr_w(szValueName),
1948 debugstr_w(szValue));
1949 lRet = RegSetValueExW(profilesKey, szValueName, 0, REG_EXPAND_SZ,
1950 (LPBYTE)szValue,
1951 (strlenW(szValue) + 1) * sizeof(WCHAR));
1952 if (lRet)
1953 hr = HRESULT_FROM_WIN32(lRet);
1954 else
1955 hr = S_OK;
1957 TRACE("returning 0x%08x (output value is %s)\n", hr, debugstr_w(szValue));
1958 return hr;
1961 /* Attempts to expand environment variables from szSrc into szDest, which is
1962 * assumed to be MAX_PATH characters in length. Before referring to the
1963 * environment, handles a few variables directly, because the environment
1964 * variables may not be set when this is called (as during Wine's installation
1965 * when default values are being written to the registry).
1966 * The directly handled environment variables, and their source, are:
1967 * - ALLUSERSPROFILE, USERPROFILE: reads from the registry
1968 * - SystemDrive: uses GetSystemDirectoryW and uses the drive portion of its
1969 * path
1970 * If one of the directly handled environment variables is expanded, only
1971 * expands a single variable, and only in the beginning of szSrc.
1973 static HRESULT _SHExpandEnvironmentStrings(LPCWSTR szSrc, LPWSTR szDest)
1975 HRESULT hr;
1976 WCHAR szTemp[MAX_PATH], szProfilesPrefix[MAX_PATH] = { 0 };
1977 HKEY key = NULL;
1979 TRACE("%s, %p\n", debugstr_w(szSrc), szDest);
1981 if (!szSrc || !szDest) return E_INVALIDARG;
1983 /* short-circuit if there's nothing to expand */
1984 if (szSrc[0] != '%')
1986 strcpyW(szDest, szSrc);
1987 hr = S_OK;
1988 goto end;
1990 /* Get the profile prefix, we'll probably be needing it */
1991 hr = _SHOpenProfilesKey(&key);
1992 if (SUCCEEDED(hr))
1994 WCHAR def_val[MAX_PATH];
1996 /* get the system drive */
1997 GetSystemDirectoryW(def_val, MAX_PATH);
1998 if (def_val[1] == ':') strcpyW( def_val + 3, szDefaultProfileDirW );
1999 else FIXME("non-drive system paths unsupported\n");
2001 hr = _SHGetProfilesValue(key, ProfilesDirectoryW, szProfilesPrefix, def_val );
2004 *szDest = 0;
2005 strcpyW(szTemp, szSrc);
2006 while (SUCCEEDED(hr) && szTemp[0] == '%')
2008 if (!strncmpiW(szTemp, AllUsersProfileW, strlenW(AllUsersProfileW)))
2010 WCHAR szAllUsers[MAX_PATH];
2012 strcpyW(szDest, szProfilesPrefix);
2013 hr = _SHGetProfilesValue(key, AllUsersProfileValueW,
2014 szAllUsers, AllUsersW);
2015 PathAppendW(szDest, szAllUsers);
2016 PathAppendW(szDest, szTemp + strlenW(AllUsersProfileW));
2018 else if (!strncmpiW(szTemp, UserProfileW, strlenW(UserProfileW)))
2020 WCHAR userName[MAX_PATH];
2021 DWORD userLen = MAX_PATH;
2023 strcpyW(szDest, szProfilesPrefix);
2024 GetUserNameW(userName, &userLen);
2025 PathAppendW(szDest, userName);
2026 PathAppendW(szDest, szTemp + strlenW(UserProfileW));
2028 else if (!strncmpiW(szTemp, SystemDriveW, strlenW(SystemDriveW)))
2030 GetSystemDirectoryW(szDest, MAX_PATH);
2031 if (szDest[1] != ':')
2033 FIXME("non-drive system paths unsupported\n");
2034 hr = E_FAIL;
2036 else
2038 strcpyW(szDest + 3, szTemp + strlenW(SystemDriveW) + 1);
2039 hr = S_OK;
2042 else
2044 DWORD ret = ExpandEnvironmentStringsW(szSrc, szDest, MAX_PATH);
2046 if (ret > MAX_PATH)
2047 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
2048 else if (ret == 0)
2049 hr = HRESULT_FROM_WIN32(GetLastError());
2050 else
2051 hr = S_OK;
2053 if (SUCCEEDED(hr) && szDest[0] == '%')
2054 strcpyW(szTemp, szDest);
2055 else
2057 /* terminate loop */
2058 szTemp[0] = '\0';
2061 end:
2062 if (key)
2063 RegCloseKey(key);
2064 TRACE("returning 0x%08x (input was %s, output is %s)\n", hr,
2065 debugstr_w(szSrc), debugstr_w(szDest));
2066 return hr;
2069 /*************************************************************************
2070 * SHGetFolderPathW [SHELL32.@]
2072 * Convert nFolder to path.
2074 * RETURNS
2075 * Success: S_OK
2076 * Failure: standard HRESULT error codes.
2078 * NOTES
2079 * Most values can be overridden in either
2080 * HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
2081 * or in the same location in HKLM.
2082 * The "Shell Folders" registry key was used in NT4 and earlier systems.
2083 * Beginning with Windows 2000, the "User Shell Folders" key is used, so
2084 * changes made to it are made to the former key too. This synchronization is
2085 * done on-demand: not until someone requests the value of one of these paths
2086 * (by calling one of the SHGet functions) is the value synchronized.
2087 * Furthermore, the HKCU paths take precedence over the HKLM paths.
2089 HRESULT WINAPI SHGetFolderPathW(
2090 HWND hwndOwner, /* [I] owner window */
2091 int nFolder, /* [I] CSIDL identifying the folder */
2092 HANDLE hToken, /* [I] access token */
2093 DWORD dwFlags, /* [I] which path to return */
2094 LPWSTR pszPath) /* [O] converted path */
2096 HRESULT hr = SHGetFolderPathAndSubDirW(hwndOwner, nFolder, hToken, dwFlags, NULL, pszPath);
2097 if(HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND) == hr)
2098 hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
2099 return hr;
2102 HRESULT WINAPI SHGetFolderPathAndSubDirA(
2103 HWND hwndOwner, /* [I] owner window */
2104 int nFolder, /* [I] CSIDL identifying the folder */
2105 HANDLE hToken, /* [I] access token */
2106 DWORD dwFlags, /* [I] which path to return */
2107 LPCSTR pszSubPath, /* [I] sub directory of the specified folder */
2108 LPSTR pszPath) /* [O] converted path */
2110 int length;
2111 HRESULT hr = S_OK;
2112 LPWSTR pszSubPathW = NULL;
2113 LPWSTR pszPathW = NULL;
2114 TRACE("%08x,%08x,%s\n",nFolder, dwFlags, debugstr_w(pszSubPathW));
2116 if(pszPath) {
2117 pszPathW = HeapAlloc(GetProcessHeap(), 0, MAX_PATH * sizeof(WCHAR));
2118 if(!pszPathW) {
2119 hr = HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
2120 goto cleanup;
2123 TRACE("%08x,%08x,%s\n",nFolder, dwFlags, debugstr_w(pszSubPathW));
2125 /* SHGetFolderPathAndSubDirW does not distinguish if pszSubPath isn't
2126 * set (null), or an empty string.therefore call it without the parameter set
2127 * if pszSubPath is an empty string
2129 if (pszSubPath && pszSubPath[0]) {
2130 length = MultiByteToWideChar(CP_ACP, 0, pszSubPath, -1, NULL, 0);
2131 pszSubPathW = HeapAlloc(GetProcessHeap(), 0, length * sizeof(WCHAR));
2132 if(!pszSubPathW) {
2133 hr = HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
2134 goto cleanup;
2136 MultiByteToWideChar(CP_ACP, 0, pszSubPath, -1, pszSubPathW, length);
2139 hr = SHGetFolderPathAndSubDirW(hwndOwner, nFolder, hToken, dwFlags, pszSubPathW, pszPathW);
2141 if (SUCCEEDED(hr) && pszPath)
2142 WideCharToMultiByte(CP_ACP, 0, pszPathW, -1, pszPath, MAX_PATH, NULL, NULL);
2144 cleanup:
2145 HeapFree(GetProcessHeap(), 0, pszPathW);
2146 HeapFree(GetProcessHeap(), 0, pszSubPathW);
2147 return hr;
2150 /*************************************************************************
2151 * SHGetFolderPathAndSubDirW [SHELL32.@]
2153 HRESULT WINAPI SHGetFolderPathAndSubDirW(
2154 HWND hwndOwner, /* [I] owner window */
2155 int nFolder, /* [I] CSIDL identifying the folder */
2156 HANDLE hToken, /* [I] access token */
2157 DWORD dwFlags, /* [I] which path to return */
2158 LPCWSTR pszSubPath,/* [I] sub directory of the specified folder */
2159 LPWSTR pszPath) /* [O] converted path */
2161 HRESULT hr;
2162 WCHAR szBuildPath[MAX_PATH], szTemp[MAX_PATH];
2163 DWORD folder = nFolder & CSIDL_FOLDER_MASK;
2164 CSIDL_Type type;
2165 int ret;
2167 TRACE("%p,%p,nFolder=0x%04x,%s\n", hwndOwner,pszPath,nFolder,debugstr_w(pszSubPath));
2169 /* Windows always NULL-terminates the resulting path regardless of success
2170 * or failure, so do so first
2172 if (pszPath)
2173 *pszPath = '\0';
2175 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
2176 return E_INVALIDARG;
2177 if ((SHGFP_TYPE_CURRENT != dwFlags) && (SHGFP_TYPE_DEFAULT != dwFlags))
2178 return E_INVALIDARG;
2179 szTemp[0] = 0;
2180 type = CSIDL_Data[folder].type;
2181 switch (type)
2183 case CSIDL_Type_Disallowed:
2184 hr = E_INVALIDARG;
2185 break;
2186 case CSIDL_Type_NonExistent:
2187 hr = S_FALSE;
2188 break;
2189 case CSIDL_Type_WindowsPath:
2190 GetWindowsDirectoryW(szTemp, MAX_PATH);
2191 if (CSIDL_Data[folder].szDefaultPath &&
2192 !IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath) &&
2193 *CSIDL_Data[folder].szDefaultPath)
2195 PathAddBackslashW(szTemp);
2196 strcatW(szTemp, CSIDL_Data[folder].szDefaultPath);
2198 hr = S_OK;
2199 break;
2200 case CSIDL_Type_SystemPath:
2201 GetSystemDirectoryW(szTemp, MAX_PATH);
2202 if (CSIDL_Data[folder].szDefaultPath &&
2203 !IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath) &&
2204 *CSIDL_Data[folder].szDefaultPath)
2206 PathAddBackslashW(szTemp);
2207 strcatW(szTemp, CSIDL_Data[folder].szDefaultPath);
2209 hr = S_OK;
2210 break;
2211 case CSIDL_Type_SystemX86Path:
2212 if (!GetSystemWow64DirectoryW(szTemp, MAX_PATH)) GetSystemDirectoryW(szTemp, MAX_PATH);
2213 if (CSIDL_Data[folder].szDefaultPath &&
2214 !IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath) &&
2215 *CSIDL_Data[folder].szDefaultPath)
2217 PathAddBackslashW(szTemp);
2218 strcatW(szTemp, CSIDL_Data[folder].szDefaultPath);
2220 hr = S_OK;
2221 break;
2222 case CSIDL_Type_CurrVer:
2223 hr = _SHGetCurrentVersionPath(dwFlags, folder, szTemp);
2224 break;
2225 case CSIDL_Type_User:
2226 hr = _SHGetUserProfilePath(hToken, dwFlags, folder, szTemp);
2227 break;
2228 case CSIDL_Type_AllUsers:
2229 hr = _SHGetAllUsersProfilePath(dwFlags, folder, szTemp);
2230 break;
2231 default:
2232 FIXME("bogus type %d, please fix\n", type);
2233 hr = E_INVALIDARG;
2234 break;
2237 /* Expand environment strings if necessary */
2238 if (*szTemp == '%')
2239 hr = _SHExpandEnvironmentStrings(szTemp, szBuildPath);
2240 else
2241 strcpyW(szBuildPath, szTemp);
2243 if (FAILED(hr)) goto end;
2245 if(pszSubPath) {
2246 /* make sure the new path does not exceed th bufferlength
2247 * rememebr to backslash and the termination */
2248 if(MAX_PATH < (lstrlenW(szBuildPath) + lstrlenW(pszSubPath) + 2)) {
2249 hr = HRESULT_FROM_WIN32(ERROR_FILENAME_EXCED_RANGE);
2250 goto end;
2252 PathAppendW(szBuildPath, pszSubPath);
2253 PathRemoveBackslashW(szBuildPath);
2255 /* Copy the path if it's available before we might return */
2256 if (SUCCEEDED(hr) && pszPath)
2257 strcpyW(pszPath, szBuildPath);
2259 /* if we don't care about existing directories we are ready */
2260 if(nFolder & CSIDL_FLAG_DONT_VERIFY) goto end;
2262 if (PathFileExistsW(szBuildPath)) goto end;
2264 /* not existing but we are not allowed to create it. The return value
2265 * is verified against shell32 version 6.0.
2267 if (!(nFolder & CSIDL_FLAG_CREATE))
2269 hr = HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND);
2270 goto end;
2273 /* create directory/directories */
2274 ret = SHCreateDirectoryExW(hwndOwner, szBuildPath, NULL);
2275 if (ret && ret != ERROR_ALREADY_EXISTS)
2277 ERR("Failed to create directory %s.\n", debugstr_w(szBuildPath));
2278 hr = E_FAIL;
2279 goto end;
2282 TRACE("Created missing system directory %s\n", debugstr_w(szBuildPath));
2283 end:
2284 TRACE("returning 0x%08x (final path is %s)\n", hr, debugstr_w(szBuildPath));
2285 return hr;
2288 /*************************************************************************
2289 * SHGetFolderPathA [SHELL32.@]
2291 * See SHGetFolderPathW.
2293 HRESULT WINAPI SHGetFolderPathA(
2294 HWND hwndOwner,
2295 int nFolder,
2296 HANDLE hToken,
2297 DWORD dwFlags,
2298 LPSTR pszPath)
2300 WCHAR szTemp[MAX_PATH];
2301 HRESULT hr;
2303 TRACE("%p,%p,nFolder=0x%04x\n",hwndOwner,pszPath,nFolder);
2305 if (pszPath)
2306 *pszPath = '\0';
2307 hr = SHGetFolderPathW(hwndOwner, nFolder, hToken, dwFlags, szTemp);
2308 if (SUCCEEDED(hr) && pszPath)
2309 WideCharToMultiByte(CP_ACP, 0, szTemp, -1, pszPath, MAX_PATH, NULL,
2310 NULL);
2312 return hr;
2315 /* For each folder in folders, if its value has not been set in the registry,
2316 * calls _SHGetUserProfilePath or _SHGetAllUsersProfilePath (depending on the
2317 * folder's type) to get the unexpanded value first.
2318 * Writes the unexpanded value to User Shell Folders, and queries it with
2319 * SHGetFolderPathW to force the creation of the directory if it doesn't
2320 * already exist. SHGetFolderPathW also returns the expanded value, which
2321 * this then writes to Shell Folders.
2323 static HRESULT _SHRegisterFolders(HKEY hRootKey, HANDLE hToken,
2324 LPCWSTR szUserShellFolderPath, LPCWSTR szShellFolderPath, const UINT folders[],
2325 UINT foldersLen)
2327 UINT i;
2328 WCHAR path[MAX_PATH];
2329 HRESULT hr = S_OK;
2330 HKEY hUserKey = NULL, hKey = NULL;
2331 DWORD dwType, dwPathLen;
2332 LONG ret;
2334 TRACE("%p,%p,%s,%p,%u\n", hRootKey, hToken,
2335 debugstr_w(szUserShellFolderPath), folders, foldersLen);
2337 ret = RegCreateKeyW(hRootKey, szUserShellFolderPath, &hUserKey);
2338 if (ret)
2339 hr = HRESULT_FROM_WIN32(ret);
2340 else
2342 ret = RegCreateKeyW(hRootKey, szShellFolderPath, &hKey);
2343 if (ret)
2344 hr = HRESULT_FROM_WIN32(ret);
2346 for (i = 0; SUCCEEDED(hr) && i < foldersLen; i++)
2348 dwPathLen = MAX_PATH * sizeof(WCHAR);
2349 if (RegQueryValueExW(hUserKey, CSIDL_Data[folders[i]].szValueName, NULL,
2350 &dwType, (LPBYTE)path, &dwPathLen) || (dwType != REG_SZ &&
2351 dwType != REG_EXPAND_SZ))
2353 *path = '\0';
2354 if (CSIDL_Data[folders[i]].type == CSIDL_Type_User)
2355 _SHGetUserProfilePath(hToken, SHGFP_TYPE_DEFAULT, folders[i],
2356 path);
2357 else if (CSIDL_Data[folders[i]].type == CSIDL_Type_AllUsers)
2358 _SHGetAllUsersProfilePath(SHGFP_TYPE_DEFAULT, folders[i], path);
2359 else if (CSIDL_Data[folders[i]].type == CSIDL_Type_WindowsPath)
2361 GetWindowsDirectoryW(path, MAX_PATH);
2362 if (CSIDL_Data[folders[i]].szDefaultPath &&
2363 !IS_INTRESOURCE(CSIDL_Data[folders[i]].szDefaultPath))
2365 PathAddBackslashW(path);
2366 strcatW(path, CSIDL_Data[folders[i]].szDefaultPath);
2369 else
2370 hr = E_FAIL;
2371 if (*path)
2373 ret = RegSetValueExW(hUserKey,
2374 CSIDL_Data[folders[i]].szValueName, 0, REG_EXPAND_SZ,
2375 (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR));
2376 if (ret)
2377 hr = HRESULT_FROM_WIN32(ret);
2378 else
2380 hr = SHGetFolderPathW(NULL, folders[i] | CSIDL_FLAG_CREATE,
2381 hToken, SHGFP_TYPE_DEFAULT, path);
2382 ret = RegSetValueExW(hKey,
2383 CSIDL_Data[folders[i]].szValueName, 0, REG_SZ,
2384 (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR));
2385 if (ret)
2386 hr = HRESULT_FROM_WIN32(ret);
2391 if (hUserKey)
2392 RegCloseKey(hUserKey);
2393 if (hKey)
2394 RegCloseKey(hKey);
2396 TRACE("returning 0x%08x\n", hr);
2397 return hr;
2400 static HRESULT _SHRegisterUserShellFolders(BOOL bDefault)
2402 static const UINT folders[] = {
2403 CSIDL_PROGRAMS,
2404 CSIDL_PERSONAL,
2405 CSIDL_FAVORITES,
2406 CSIDL_APPDATA,
2407 CSIDL_STARTUP,
2408 CSIDL_RECENT,
2409 CSIDL_SENDTO,
2410 CSIDL_STARTMENU,
2411 CSIDL_MYMUSIC,
2412 CSIDL_MYVIDEO,
2413 CSIDL_DESKTOPDIRECTORY,
2414 CSIDL_NETHOOD,
2415 CSIDL_TEMPLATES,
2416 CSIDL_PRINTHOOD,
2417 CSIDL_LOCAL_APPDATA,
2418 CSIDL_INTERNET_CACHE,
2419 CSIDL_COOKIES,
2420 CSIDL_HISTORY,
2421 CSIDL_MYPICTURES,
2422 CSIDL_FONTS
2424 WCHAR userShellFolderPath[MAX_PATH], shellFolderPath[MAX_PATH];
2425 LPCWSTR pUserShellFolderPath, pShellFolderPath;
2426 HRESULT hr = S_OK;
2427 HKEY hRootKey;
2428 HANDLE hToken;
2430 TRACE("%s\n", bDefault ? "TRUE" : "FALSE");
2431 if (bDefault)
2433 hToken = (HANDLE)-1;
2434 hRootKey = HKEY_USERS;
2435 strcpyW(userShellFolderPath, DefaultW);
2436 PathAddBackslashW(userShellFolderPath);
2437 strcatW(userShellFolderPath, szSHUserFolders);
2438 pUserShellFolderPath = userShellFolderPath;
2439 strcpyW(shellFolderPath, DefaultW);
2440 PathAddBackslashW(shellFolderPath);
2441 strcatW(shellFolderPath, szSHFolders);
2442 pShellFolderPath = shellFolderPath;
2444 else
2446 hToken = NULL;
2447 hRootKey = HKEY_CURRENT_USER;
2448 pUserShellFolderPath = szSHUserFolders;
2449 pShellFolderPath = szSHFolders;
2452 hr = _SHRegisterFolders(hRootKey, hToken, pUserShellFolderPath,
2453 pShellFolderPath, folders, sizeof(folders) / sizeof(folders[0]));
2454 TRACE("returning 0x%08x\n", hr);
2455 return hr;
2458 static HRESULT _SHRegisterCommonShellFolders(void)
2460 static const UINT folders[] = {
2461 CSIDL_COMMON_STARTMENU,
2462 CSIDL_COMMON_PROGRAMS,
2463 CSIDL_COMMON_STARTUP,
2464 CSIDL_COMMON_DESKTOPDIRECTORY,
2465 CSIDL_COMMON_FAVORITES,
2466 CSIDL_COMMON_APPDATA,
2467 CSIDL_COMMON_TEMPLATES,
2468 CSIDL_COMMON_DOCUMENTS,
2469 CSIDL_COMMON_ADMINTOOLS,
2470 CSIDL_COMMON_MUSIC,
2471 CSIDL_COMMON_PICTURES,
2472 CSIDL_COMMON_VIDEO,
2474 HRESULT hr;
2476 TRACE("\n");
2477 hr = _SHRegisterFolders(HKEY_LOCAL_MACHINE, NULL, szSHUserFolders,
2478 szSHFolders, folders, sizeof(folders) / sizeof(folders[0]));
2479 TRACE("returning 0x%08x\n", hr);
2480 return hr;
2483 /******************************************************************************
2484 * _SHAppendToUnixPath [Internal]
2486 * Helper function for _SHCreateSymbolicLinks. Appends pwszSubPath (or the
2487 * corresponding resource, if IS_INTRESOURCE) to the unix base path 'szBasePath'
2488 * and replaces backslashes with slashes.
2490 * PARAMS
2491 * szBasePath [IO] The unix base path, which will be appended to (CP_UNXICP).
2492 * pwszSubPath [I] Sub-path or resource id (use MAKEINTRESOURCEW).
2494 * RETURNS
2495 * Success: TRUE,
2496 * Failure: FALSE
2498 static inline BOOL _SHAppendToUnixPath(char *szBasePath, LPCWSTR pwszSubPath) {
2499 WCHAR wszSubPath[MAX_PATH];
2500 int cLen = strlen(szBasePath);
2501 char *pBackslash;
2503 if (IS_INTRESOURCE(pwszSubPath)) {
2504 if (!LoadStringW(shell32_hInstance, LOWORD(pwszSubPath), wszSubPath, MAX_PATH)) {
2505 /* Fall back to hard coded defaults. */
2506 switch (LOWORD(pwszSubPath)) {
2507 case IDS_PERSONAL:
2508 lstrcpyW(wszSubPath, PersonalW);
2509 break;
2510 case IDS_MYMUSIC:
2511 lstrcpyW(wszSubPath, My_MusicW);
2512 break;
2513 case IDS_MYPICTURES:
2514 lstrcpyW(wszSubPath, My_PicturesW);
2515 break;
2516 case IDS_MYVIDEO:
2517 lstrcpyW(wszSubPath, My_VideoW);
2518 break;
2519 default:
2520 ERR("LoadString(%d) failed!\n", LOWORD(pwszSubPath));
2521 return FALSE;
2524 } else {
2525 lstrcpyW(wszSubPath, pwszSubPath);
2528 if (szBasePath[cLen-1] != '/') szBasePath[cLen++] = '/';
2530 if (!WideCharToMultiByte(CP_UNIXCP, 0, wszSubPath, -1, szBasePath + cLen,
2531 FILENAME_MAX - cLen, NULL, NULL))
2533 return FALSE;
2536 pBackslash = szBasePath + cLen;
2537 while ((pBackslash = strchr(pBackslash, '\\'))) *pBackslash = '/';
2539 return TRUE;
2542 /******************************************************************************
2543 * _SHCreateSymbolicLinks [Internal]
2545 * Sets up symbol links for various shell folders to point into the users home
2546 * directory. We do an educated guess about what the user would probably want:
2547 * - If there is a 'My Documents' directory in $HOME, the user probably wants
2548 * wine's 'My Documents' to point there. Furthermore, we imply that the user
2549 * is a Windows lover and has no problem with wine creating 'My Pictures',
2550 * 'My Music' and 'My Video' subfolders under '$HOME/My Documents', if those
2551 * do not already exits. We put appropriate symbolic links in place for those,
2552 * too.
2553 * - If there is no 'My Documents' directory in $HOME, we let 'My Documents'
2554 * point directly to $HOME. We assume the user to be a unix hacker who does not
2555 * want wine to create anything anywhere besides the .wine directory. So, if
2556 * there already is a 'My Music' directory in $HOME, we symlink the 'My Music'
2557 * shell folder to it. But if not, then we check XDG_MUSIC_DIR - "well known"
2558 * directory, and try to link to that. If that fails, then we symlink to
2559 * $HOME directly. The same holds fo 'My Pictures' and 'My Video'.
2560 * - The Desktop shell folder is symlinked to XDG_DESKTOP_DIR. If that does not
2561 * exist, then we try '$HOME/Desktop'. If that does not exist, then we leave
2562 * it alone.
2563 * ('My Music',... above in fact means LoadString(IDS_MYMUSIC))
2565 static void _SHCreateSymbolicLinks(void)
2567 UINT aidsMyStuff[] = { IDS_MYPICTURES, IDS_MYVIDEO, IDS_MYMUSIC }, i;
2568 int acsidlMyStuff[] = { CSIDL_MYPICTURES, CSIDL_MYVIDEO, CSIDL_MYMUSIC };
2569 static const char * const xdg_dirs[] = { "PICTURES", "VIDEOS", "MUSIC", "DESKTOP" };
2570 static const unsigned int num = sizeof(xdg_dirs) / sizeof(xdg_dirs[0]);
2571 WCHAR wszTempPath[MAX_PATH];
2572 char szPersonalTarget[FILENAME_MAX], *pszPersonal;
2573 char szMyStuffTarget[FILENAME_MAX], *pszMyStuff;
2574 char szDesktopTarget[FILENAME_MAX], *pszDesktop;
2575 struct stat statFolder;
2576 const char *pszHome;
2577 HRESULT hr;
2578 char ** xdg_results;
2579 char * xdg_desktop_dir;
2581 /* Create all necessary profile sub-dirs up to 'My Documents' and get the unix path. */
2582 hr = SHGetFolderPathW(NULL, CSIDL_PERSONAL|CSIDL_FLAG_CREATE, NULL,
2583 SHGFP_TYPE_DEFAULT, wszTempPath);
2584 if (FAILED(hr)) return;
2585 pszPersonal = wine_get_unix_file_name(wszTempPath);
2586 if (!pszPersonal) return;
2588 hr = XDG_UserDirLookup(xdg_dirs, num, &xdg_results);
2589 if (FAILED(hr)) xdg_results = NULL;
2591 pszHome = getenv("HOME");
2592 if (pszHome && !stat(pszHome, &statFolder) && S_ISDIR(statFolder.st_mode)) {
2593 strcpy(szPersonalTarget, pszHome);
2594 if (_SHAppendToUnixPath(szPersonalTarget, MAKEINTRESOURCEW(IDS_PERSONAL)) &&
2595 !stat(szPersonalTarget, &statFolder) && S_ISDIR(statFolder.st_mode))
2597 /* '$HOME/My Documents' exists. Create 'My Pictures', 'My Videos' and
2598 * 'My Music' subfolders or fail silently if they already exist. */
2599 for (i = 0; i < sizeof(aidsMyStuff)/sizeof(aidsMyStuff[0]); i++) {
2600 strcpy(szMyStuffTarget, szPersonalTarget);
2601 if (_SHAppendToUnixPath(szMyStuffTarget, MAKEINTRESOURCEW(aidsMyStuff[i])))
2602 mkdir(szMyStuffTarget, 0777);
2605 else
2607 /* '$HOME/My Documents' doesn't exists, but '$HOME' does. */
2608 strcpy(szPersonalTarget, pszHome);
2611 /* Replace 'My Documents' directory with a symlink of fail silently if not empty. */
2612 rmdir(pszPersonal);
2613 symlink(szPersonalTarget, pszPersonal);
2615 else
2617 /* '$HOME' doesn't exist. Create 'My Pictures', 'My Videos' and 'My Music' subdirs
2618 * in '%USERPROFILE%\\My Documents' or fail silently if they already exist. */
2619 strcpy(szPersonalTarget, pszPersonal);
2620 for (i = 0; i < sizeof(aidsMyStuff)/sizeof(aidsMyStuff[0]); i++) {
2621 strcpy(szMyStuffTarget, szPersonalTarget);
2622 if (_SHAppendToUnixPath(szMyStuffTarget, MAKEINTRESOURCEW(aidsMyStuff[i])))
2623 mkdir(szMyStuffTarget, 0777);
2627 /* Create symbolic links for 'My Pictures', 'My Video' and 'My Music'. */
2628 for (i=0; i < sizeof(aidsMyStuff)/sizeof(aidsMyStuff[0]); i++) {
2629 /* Create the current 'My Whatever' folder and get it's unix path. */
2630 hr = SHGetFolderPathW(NULL, acsidlMyStuff[i]|CSIDL_FLAG_CREATE, NULL,
2631 SHGFP_TYPE_DEFAULT, wszTempPath);
2632 if (FAILED(hr)) continue;
2633 pszMyStuff = wine_get_unix_file_name(wszTempPath);
2634 if (!pszMyStuff) continue;
2636 strcpy(szMyStuffTarget, szPersonalTarget);
2637 if (_SHAppendToUnixPath(szMyStuffTarget, MAKEINTRESOURCEW(aidsMyStuff[i])) &&
2638 !stat(szMyStuffTarget, &statFolder) && S_ISDIR(statFolder.st_mode))
2640 /* If there's a 'My Whatever' directory where 'My Documents' links to, link to it. */
2641 rmdir(pszMyStuff);
2642 symlink(szMyStuffTarget, pszMyStuff);
2644 else
2646 rmdir(pszMyStuff);
2647 if (xdg_results && xdg_results[i])
2649 /* the folder specified by XDG_XXX_DIR exists, link to it. */
2650 symlink(xdg_results[i], pszMyStuff);
2652 else
2654 /* Else link to where 'My Documents' itself links to. */
2655 symlink(szPersonalTarget, pszMyStuff);
2658 HeapFree(GetProcessHeap(), 0, pszMyStuff);
2661 /* Last but not least, the Desktop folder */
2662 if (pszHome)
2663 strcpy(szDesktopTarget, pszHome);
2664 else
2665 strcpy(szDesktopTarget, pszPersonal);
2666 HeapFree(GetProcessHeap(), 0, pszPersonal);
2668 xdg_desktop_dir = xdg_results ? xdg_results[num - 1] : NULL;
2669 if (xdg_desktop_dir ||
2670 (_SHAppendToUnixPath(szDesktopTarget, DesktopW) &&
2671 !stat(szDesktopTarget, &statFolder) && S_ISDIR(statFolder.st_mode)))
2673 hr = SHGetFolderPathW(NULL, CSIDL_DESKTOPDIRECTORY|CSIDL_FLAG_CREATE, NULL,
2674 SHGFP_TYPE_DEFAULT, wszTempPath);
2675 if (SUCCEEDED(hr) && (pszDesktop = wine_get_unix_file_name(wszTempPath)))
2677 rmdir(pszDesktop);
2678 if (xdg_desktop_dir)
2679 symlink(xdg_desktop_dir, pszDesktop);
2680 else
2681 symlink(szDesktopTarget, pszDesktop);
2682 HeapFree(GetProcessHeap(), 0, pszDesktop);
2686 /* Free resources allocated by XDG_UserDirLookup() */
2687 if (xdg_results)
2689 for (i = 0; i < num; i++)
2690 HeapFree(GetProcessHeap(), 0, xdg_results[i]);
2691 HeapFree(GetProcessHeap(), 0, xdg_results);
2695 /******************************************************************************
2696 * create_extra_folders [Internal]
2698 * Create some extra folders that don't have a standard CSIDL definition.
2700 static HRESULT create_extra_folders(void)
2702 static const WCHAR environW[] = {'E','n','v','i','r','o','n','m','e','n','t',0};
2703 static const WCHAR TempW[] = {'T','e','m','p',0};
2704 static const WCHAR TEMPW[] = {'T','E','M','P',0};
2705 static const WCHAR TMPW[] = {'T','M','P',0};
2706 WCHAR path[MAX_PATH+5];
2707 HRESULT hr;
2708 HKEY hkey;
2709 DWORD type, size, ret;
2711 ret = RegCreateKeyW( HKEY_CURRENT_USER, environW, &hkey );
2712 if (ret) return HRESULT_FROM_WIN32( ret );
2714 /* FIXME: should be under AppData, but we don't want spaces in the temp path */
2715 hr = SHGetFolderPathAndSubDirW( 0, CSIDL_PROFILE | CSIDL_FLAG_CREATE, NULL,
2716 SHGFP_TYPE_DEFAULT, TempW, path );
2717 if (SUCCEEDED(hr))
2719 size = sizeof(path);
2720 if (RegQueryValueExW( hkey, TEMPW, NULL, &type, (LPBYTE)path, &size ))
2721 RegSetValueExW( hkey, TEMPW, 0, REG_SZ, (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR) );
2722 size = sizeof(path);
2723 if (RegQueryValueExW( hkey, TMPW, NULL, &type, (LPBYTE)path, &size ))
2724 RegSetValueExW( hkey, TMPW, 0, REG_SZ, (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR) );
2726 RegCloseKey( hkey );
2727 return hr;
2731 /******************************************************************************
2732 * set_folder_attributes
2734 * Set the various folder attributes registry keys.
2736 static HRESULT set_folder_attributes(void)
2738 static const WCHAR clsidW[] = {'C','L','S','I','D','\\',0 };
2739 static const WCHAR shellfolderW[] = {'\\','S','h','e','l','l','F','o','l','d','e','r', 0 };
2740 static const WCHAR wfparsingW[] = {'W','a','n','t','s','F','O','R','P','A','R','S','I','N','G',0};
2741 static const WCHAR wfdisplayW[] = {'W','a','n','t','s','F','O','R','D','I','S','P','L','A','Y',0};
2742 static const WCHAR hideasdeleteW[] = {'H','i','d','e','A','s','D','e','l','e','t','e','P','e','r','U','s','e','r',0};
2743 static const WCHAR attributesW[] = {'A','t','t','r','i','b','u','t','e','s',0};
2744 static const WCHAR cfattributesW[] = {'C','a','l','l','F','o','r','A','t','t','r','i','b','u','t','e','s',0};
2745 static const WCHAR emptyW[] = {0};
2747 static const struct
2749 const CLSID *clsid;
2750 BOOL wfparsing : 1;
2751 BOOL wfdisplay : 1;
2752 BOOL hideasdel : 1;
2753 DWORD attr;
2754 DWORD call_for_attr;
2755 } folders[] =
2757 { &CLSID_UnixFolder, TRUE, FALSE, FALSE },
2758 { &CLSID_UnixDosFolder, TRUE, FALSE, FALSE,
2759 SFGAO_FILESYSANCESTOR|SFGAO_FOLDER|SFGAO_HASSUBFOLDER, SFGAO_FILESYSTEM },
2760 { &CLSID_FolderShortcut, FALSE, FALSE, FALSE,
2761 SFGAO_FILESYSTEM|SFGAO_FOLDER|SFGAO_LINK,
2762 SFGAO_HASSUBFOLDER|SFGAO_FILESYSTEM|SFGAO_FOLDER|SFGAO_FILESYSANCESTOR },
2763 { &CLSID_MyDocuments, TRUE, FALSE, FALSE,
2764 SFGAO_FILESYSANCESTOR|SFGAO_FOLDER|SFGAO_HASSUBFOLDER, SFGAO_FILESYSTEM },
2765 { &CLSID_RecycleBin, FALSE, FALSE, FALSE,
2766 SFGAO_FOLDER|SFGAO_DROPTARGET|SFGAO_HASPROPSHEET },
2767 { &CLSID_ControlPanel, FALSE, TRUE, TRUE,
2768 SFGAO_FOLDER|SFGAO_HASSUBFOLDER }
2771 unsigned int i;
2772 WCHAR buffer[39 + (sizeof(clsidW) + sizeof(shellfolderW)) / sizeof(WCHAR)];
2773 LONG res;
2774 HKEY hkey;
2776 for (i = 0; i < sizeof(folders)/sizeof(folders[0]); i++)
2778 strcpyW( buffer, clsidW );
2779 StringFromGUID2( folders[i].clsid, buffer + strlenW(buffer), 39 );
2780 strcatW( buffer, shellfolderW );
2781 res = RegCreateKeyExW( HKEY_CLASSES_ROOT, buffer, 0, NULL, 0,
2782 KEY_READ | KEY_WRITE, NULL, &hkey, NULL);
2783 if (res) return HRESULT_FROM_WIN32( res );
2784 if (folders[i].wfparsing)
2785 res = RegSetValueExW( hkey, wfparsingW, 0, REG_SZ, (const BYTE *)emptyW, sizeof(emptyW) );
2786 if (folders[i].wfdisplay)
2787 res = RegSetValueExW( hkey, wfdisplayW, 0, REG_SZ, (const BYTE *)emptyW, sizeof(emptyW) );
2788 if (folders[i].hideasdel)
2789 res = RegSetValueExW( hkey, hideasdeleteW, 0, REG_SZ, (const BYTE *)emptyW, sizeof(emptyW) );
2790 if (folders[i].attr)
2791 res = RegSetValueExW( hkey, attributesW, 0, REG_DWORD,
2792 (const BYTE *)&folders[i].attr, sizeof(DWORD));
2793 if (folders[i].call_for_attr)
2794 res = RegSetValueExW( hkey, cfattributesW, 0, REG_DWORD,
2795 (const BYTE *)&folders[i].call_for_attr, sizeof(DWORD));
2796 RegCloseKey( hkey );
2798 return S_OK;
2802 /* Register the default values in the registry, as some apps seem to depend
2803 * on their presence. The set registered was taken from Windows XP.
2805 HRESULT SHELL_RegisterShellFolders(void)
2807 HRESULT hr;
2809 /* Set up '$HOME' targeted symlinks for 'My Documents', 'My Pictures',
2810 * 'My Video', 'My Music' and 'Desktop' in advance, so that the
2811 * _SHRegister*ShellFolders() functions will find everything nice and clean
2812 * and thus will not attempt to create them in the profile directory. */
2813 _SHCreateSymbolicLinks();
2815 hr = _SHRegisterUserShellFolders(TRUE);
2816 if (SUCCEEDED(hr))
2817 hr = _SHRegisterUserShellFolders(FALSE);
2818 if (SUCCEEDED(hr))
2819 hr = _SHRegisterCommonShellFolders();
2820 if (SUCCEEDED(hr))
2821 hr = create_extra_folders();
2822 if (SUCCEEDED(hr))
2823 hr = set_folder_attributes();
2824 return hr;
2827 /*************************************************************************
2828 * SHGetSpecialFolderPathA [SHELL32.@]
2830 BOOL WINAPI SHGetSpecialFolderPathA (
2831 HWND hwndOwner,
2832 LPSTR szPath,
2833 int nFolder,
2834 BOOL bCreate)
2836 return (SHGetFolderPathA(
2837 hwndOwner,
2838 nFolder + (bCreate ? CSIDL_FLAG_CREATE : 0),
2839 NULL,
2841 szPath)) == S_OK ? TRUE : FALSE;
2844 /*************************************************************************
2845 * SHGetSpecialFolderPathW
2847 BOOL WINAPI SHGetSpecialFolderPathW (
2848 HWND hwndOwner,
2849 LPWSTR szPath,
2850 int nFolder,
2851 BOOL bCreate)
2853 return (SHGetFolderPathW(
2854 hwndOwner,
2855 nFolder + (bCreate ? CSIDL_FLAG_CREATE : 0),
2856 NULL,
2858 szPath)) == S_OK ? TRUE : FALSE;
2861 /*************************************************************************
2862 * SHGetSpecialFolderPath (SHELL32.175)
2864 BOOL WINAPI SHGetSpecialFolderPathAW (
2865 HWND hwndOwner,
2866 LPVOID szPath,
2867 int nFolder,
2868 BOOL bCreate)
2871 if (SHELL_OsIsUnicode())
2872 return SHGetSpecialFolderPathW (hwndOwner, szPath, nFolder, bCreate);
2873 return SHGetSpecialFolderPathA (hwndOwner, szPath, nFolder, bCreate);
2876 /*************************************************************************
2877 * SHGetFolderLocation [SHELL32.@]
2879 * Gets the folder locations from the registry and creates a pidl.
2881 * PARAMS
2882 * hwndOwner [I]
2883 * nFolder [I] CSIDL_xxxxx
2884 * hToken [I] token representing user, or NULL for current user, or -1 for
2885 * default user
2886 * dwReserved [I] must be zero
2887 * ppidl [O] PIDL of a special folder
2889 * RETURNS
2890 * Success: S_OK
2891 * Failure: Standard OLE-defined error result, S_FALSE or E_INVALIDARG
2893 * NOTES
2894 * Creates missing reg keys and directories.
2895 * Mostly forwards to SHGetFolderPathW, but a few values of nFolder return
2896 * virtual folders that are handled here.
2898 HRESULT WINAPI SHGetFolderLocation(
2899 HWND hwndOwner,
2900 int nFolder,
2901 HANDLE hToken,
2902 DWORD dwReserved,
2903 LPITEMIDLIST *ppidl)
2905 HRESULT hr = E_INVALIDARG;
2907 TRACE("%p 0x%08x %p 0x%08x %p\n",
2908 hwndOwner, nFolder, hToken, dwReserved, ppidl);
2910 if (!ppidl)
2911 return E_INVALIDARG;
2912 if (dwReserved)
2913 return E_INVALIDARG;
2915 /* The virtual folders' locations are not user-dependent */
2916 *ppidl = NULL;
2917 switch (nFolder & CSIDL_FOLDER_MASK)
2919 case CSIDL_DESKTOP:
2920 *ppidl = _ILCreateDesktop();
2921 break;
2923 case CSIDL_PERSONAL:
2924 *ppidl = _ILCreateMyDocuments();
2925 break;
2927 case CSIDL_INTERNET:
2928 *ppidl = _ILCreateIExplore();
2929 break;
2931 case CSIDL_CONTROLS:
2932 *ppidl = _ILCreateControlPanel();
2933 break;
2935 case CSIDL_PRINTERS:
2936 *ppidl = _ILCreatePrinters();
2937 break;
2939 case CSIDL_BITBUCKET:
2940 *ppidl = _ILCreateBitBucket();
2941 break;
2943 case CSIDL_DRIVES:
2944 *ppidl = _ILCreateMyComputer();
2945 break;
2947 case CSIDL_NETWORK:
2948 *ppidl = _ILCreateNetwork();
2949 break;
2951 default:
2953 WCHAR szPath[MAX_PATH];
2955 hr = SHGetFolderPathW(hwndOwner, nFolder, hToken,
2956 SHGFP_TYPE_CURRENT, szPath);
2957 if (SUCCEEDED(hr))
2959 DWORD attributes=0;
2961 TRACE("Value=%s\n", debugstr_w(szPath));
2962 hr = SHILCreateFromPathW(szPath, ppidl, &attributes);
2964 else if (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
2966 /* unlike SHGetFolderPath, SHGetFolderLocation in shell32
2967 * version 6.0 returns E_FAIL for nonexistent paths
2969 hr = E_FAIL;
2973 if(*ppidl)
2974 hr = NOERROR;
2976 TRACE("-- (new pidl %p)\n",*ppidl);
2977 return hr;
2980 /*************************************************************************
2981 * SHGetSpecialFolderLocation [SHELL32.@]
2983 * NOTES
2984 * In NT5, SHGetSpecialFolderLocation needs the <winntdir>/Recent
2985 * directory.
2987 HRESULT WINAPI SHGetSpecialFolderLocation(
2988 HWND hwndOwner,
2989 INT nFolder,
2990 LPITEMIDLIST * ppidl)
2992 HRESULT hr = E_INVALIDARG;
2994 TRACE("(%p,0x%x,%p)\n", hwndOwner,nFolder,ppidl);
2996 if (!ppidl)
2997 return E_INVALIDARG;
2999 hr = SHGetFolderLocation(hwndOwner, nFolder, NULL, 0, ppidl);
3000 return hr;
3003 static int csidl_from_id( const KNOWNFOLDERID *id )
3005 int i;
3006 for (i = 0; i < sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]); i++)
3007 if (IsEqualGUID( CSIDL_Data[i].id, id )) return i;
3008 return -1;
3011 /*************************************************************************
3012 * SHGetKnownFolderPath [SHELL32.@]
3014 HRESULT WINAPI SHGetKnownFolderPath(REFKNOWNFOLDERID rfid, DWORD flags, HANDLE token, PWSTR *path)
3016 HRESULT hr;
3017 WCHAR folder[MAX_PATH];
3018 int index = csidl_from_id( rfid );
3020 TRACE("%s, 0x%08x, %p, %p\n", debugstr_guid(rfid), flags, token, path);
3022 if (index < 0)
3023 return E_INVALIDARG;
3025 if (flags & KF_FLAG_CREATE)
3026 index |= CSIDL_FLAG_CREATE;
3028 if (flags & KF_FLAG_DONT_VERIFY)
3029 index |= CSIDL_FLAG_DONT_VERIFY;
3031 if (flags & KF_FLAG_NO_ALIAS)
3032 index |= CSIDL_FLAG_NO_ALIAS;
3034 if (flags & KF_FLAG_INIT)
3035 index |= CSIDL_FLAG_PER_USER_INIT;
3037 if (flags & ~(KF_FLAG_CREATE|KF_FLAG_DONT_VERIFY|KF_FLAG_NO_ALIAS|KF_FLAG_INIT))
3039 FIXME("flags 0x%08x not supported\n", flags);
3040 return E_INVALIDARG;
3043 hr = SHGetFolderPathW( NULL, index, token, 0, folder );
3044 if (SUCCEEDED(hr))
3046 *path = CoTaskMemAlloc( (strlenW( folder ) + 1) * sizeof(WCHAR) );
3047 if (!*path)
3048 return E_OUTOFMEMORY;
3049 strcpyW( *path, folder );
3051 return hr;
3054 /*************************************************************************
3055 * SHGetFolderPathEx [SHELL32.@]
3057 HRESULT WINAPI SHGetFolderPathEx(REFKNOWNFOLDERID rfid, DWORD flags, HANDLE token, LPWSTR path, DWORD len)
3059 HRESULT hr;
3060 WCHAR *buffer;
3062 TRACE("%s, 0x%08x, %p, %p, %u\n", debugstr_guid(rfid), flags, token, path, len);
3064 if (!path || !len) return E_INVALIDARG;
3066 hr = SHGetKnownFolderPath( rfid, flags, token, &buffer );
3067 if (SUCCEEDED( hr ))
3069 if (strlenW( buffer ) + 1 > len)
3071 CoTaskMemFree( buffer );
3072 return HRESULT_FROM_WIN32( ERROR_INSUFFICIENT_BUFFER );
3074 strcpyW( path, buffer );
3075 CoTaskMemFree( buffer );
3077 return hr;
3080 /* constant values used by known folder functions */
3081 static const WCHAR szCategory[] = {'C','a','t','e','g','o','r','y',0};
3082 static const WCHAR szName[] = {'N','a','m','e',0};
3083 static const WCHAR szRelativePath[] = {'R','e','l','a','t','i','v','e','P','a','t','h',0};
3084 static const WCHAR szParentFolder[] = {'P','a','r','e','n','t','F','o','l','d','e','r',0};
3087 * Internal function to convert known folder identifier to path of registry key
3088 * associated with known folder.
3090 * Parameters:
3091 * rfid [I] pointer to known folder identifier (may be NULL)
3092 * lpStringGuid [I] string with known folder identifier (used when rfid is NULL)
3093 * lpPath [O] place to store string address. String should be
3094 * later freed using HeapFree(GetProcessHeap(),0, ... )
3096 static HRESULT get_known_folder_registry_path(
3097 REFKNOWNFOLDERID rfid,
3098 LPWSTR lpStringGuid,
3099 LPWSTR *lpPath)
3101 static const WCHAR sBackslash[] = {'\\',0};
3102 HRESULT hr = S_OK;
3103 int length;
3104 WCHAR sGuid[50];
3106 TRACE("(%s, %s, %p)\n", debugstr_guid(rfid), debugstr_w(lpStringGuid), lpPath);
3108 if(rfid)
3109 StringFromGUID2(rfid, sGuid, sizeof(sGuid)/sizeof(sGuid[0]));
3110 else
3111 lstrcpyW(sGuid, lpStringGuid);
3113 length = lstrlenW(szKnownFolderDescriptions)+51;
3114 *lpPath = HeapAlloc(GetProcessHeap(), 0, length*sizeof(WCHAR));
3115 if(!(*lpPath))
3116 hr = E_OUTOFMEMORY;
3118 if(SUCCEEDED(hr))
3120 lstrcpyW(*lpPath, szKnownFolderDescriptions);
3121 lstrcatW(*lpPath, sBackslash);
3122 lstrcatW(*lpPath, sGuid);
3125 return hr;
3129 * Internal function to get place where folder redirection information are stored.
3131 * Parameters:
3132 * rfid [I] pointer to known folder identifier (may be NULL)
3133 * rootKey [O] root key where the redirection information are stored
3134 * It can be HKLM for COMMON folders, and HKCU for PERUSER folders.
3135 * However, besides root key, path is always that same, and is stored
3136 * as "szKnownFolderRedirections" constant
3138 static HRESULT get_known_folder_redirection_place(
3139 REFKNOWNFOLDERID rfid,
3140 HKEY *rootKey)
3142 HRESULT hr;
3143 LPWSTR lpRegistryPath = NULL;
3144 KF_CATEGORY category;
3145 DWORD dwSize;
3147 /* first, get known folder's category */
3148 hr = get_known_folder_registry_path(rfid, NULL, &lpRegistryPath);
3150 if(SUCCEEDED(hr))
3152 dwSize = sizeof(category);
3153 hr = HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE, lpRegistryPath, szCategory, RRF_RT_DWORD, NULL, &category, &dwSize));
3156 if(SUCCEEDED(hr))
3158 if(category == KF_CATEGORY_COMMON)
3160 *rootKey = HKEY_LOCAL_MACHINE;
3161 hr = S_OK;
3163 else if(category == KF_CATEGORY_PERUSER)
3165 *rootKey = HKEY_CURRENT_USER;
3166 hr = S_OK;
3168 else
3169 hr = E_FAIL;
3172 HeapFree(GetProcessHeap(), 0, lpRegistryPath);
3173 return hr;
3176 static HRESULT get_known_folder_path_by_id(REFKNOWNFOLDERID folderId, LPWSTR lpRegistryPath, DWORD dwFlags, LPWSTR *ppszPath);
3178 static HRESULT get_known_folder_category(
3179 LPWSTR registryPath,
3180 KF_CATEGORY* pCategory)
3182 DWORD dwSize = sizeof(DWORD);
3183 DWORD dwType;
3184 return HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE, registryPath, szCategory, RRF_RT_DWORD, &dwType, pCategory, &dwSize));
3187 static HRESULT redirect_known_folder(
3188 REFKNOWNFOLDERID rfid,
3189 HWND hwnd,
3190 KF_REDIRECT_FLAGS flags,
3191 LPCWSTR pszTargetPath,
3192 UINT cFolders,
3193 KNOWNFOLDERID const *pExclusion,
3194 LPWSTR *ppszError)
3196 HRESULT hr;
3197 HKEY rootKey = HKEY_LOCAL_MACHINE, hKey;
3198 WCHAR sGuid[39];
3199 LPWSTR lpRegistryPath = NULL, lpSrcPath = NULL;
3200 TRACE("(%s, %p, 0x%08x, %s, %d, %p, %p)\n", debugstr_guid(rfid), hwnd, flags, debugstr_w(pszTargetPath), cFolders, pExclusion, ppszError);
3202 if (ppszError) *ppszError = NULL;
3204 hr = get_known_folder_registry_path(rfid, NULL, &lpRegistryPath);
3206 if(SUCCEEDED(hr))
3207 hr = get_known_folder_path_by_id(rfid, lpRegistryPath, 0, &lpSrcPath);
3209 HeapFree(GetProcessHeap(), 0, lpRegistryPath);
3211 /* get path to redirection storage */
3212 if(SUCCEEDED(hr))
3213 hr = get_known_folder_redirection_place(rfid, &rootKey);
3215 /* write redirection information */
3216 if(SUCCEEDED(hr))
3217 hr = HRESULT_FROM_WIN32(RegCreateKeyExW(rootKey, szKnownFolderRedirections, 0, NULL, 0, KEY_WRITE, NULL, &hKey, NULL));
3219 if(SUCCEEDED(hr))
3221 StringFromGUID2(rfid, sGuid, sizeof(sGuid)/sizeof(sGuid[0]));
3223 hr = HRESULT_FROM_WIN32(RegSetValueExW(hKey, sGuid, 0, REG_SZ, (LPBYTE)pszTargetPath, (lstrlenW(pszTargetPath)+1)*sizeof(WCHAR)));
3225 RegCloseKey(hKey);
3228 /* make sure destination path exists */
3229 SHCreateDirectory(NULL, pszTargetPath);
3231 /* copy content if required */
3232 if(SUCCEEDED(hr) && (flags & KF_REDIRECT_COPY_CONTENTS) )
3234 static const WCHAR sWildcard[] = {'\\','*',0};
3235 WCHAR srcPath[MAX_PATH+1], dstPath[MAX_PATH+1];
3236 SHFILEOPSTRUCTW fileOp;
3238 ZeroMemory(srcPath, sizeof(srcPath));
3239 lstrcpyW(srcPath, lpSrcPath);
3240 lstrcatW(srcPath, sWildcard);
3242 ZeroMemory(dstPath, sizeof(dstPath));
3243 lstrcpyW(dstPath, pszTargetPath);
3245 ZeroMemory(&fileOp, sizeof(fileOp));
3247 if(flags & KF_REDIRECT_DEL_SOURCE_CONTENTS)
3248 fileOp.wFunc = FO_MOVE;
3249 else
3250 fileOp.wFunc = FO_COPY;
3252 fileOp.pFrom = srcPath;
3253 fileOp.pTo = dstPath;
3254 fileOp.fFlags = FOF_NO_UI;
3256 hr = (SHFileOperationW(&fileOp)==0 ? S_OK : E_FAIL);
3258 if(flags & KF_REDIRECT_DEL_SOURCE_CONTENTS)
3260 ZeroMemory(srcPath, sizeof(srcPath));
3261 lstrcpyW(srcPath, lpSrcPath);
3263 ZeroMemory(&fileOp, sizeof(fileOp));
3264 fileOp.wFunc = FO_DELETE;
3265 fileOp.pFrom = srcPath;
3266 fileOp.fFlags = FOF_NO_UI;
3268 hr = (SHFileOperationW(&fileOp)==0 ? S_OK : E_FAIL);
3272 CoTaskMemFree(lpSrcPath);
3274 return hr;
3278 struct knownfolder
3280 IKnownFolder IKnownFolder_iface;
3281 LONG refs;
3282 KNOWNFOLDERID id;
3283 LPWSTR registryPath;
3286 static inline struct knownfolder *impl_from_IKnownFolder( IKnownFolder *iface )
3288 return CONTAINING_RECORD( iface, struct knownfolder, IKnownFolder_iface );
3291 static ULONG WINAPI knownfolder_AddRef(
3292 IKnownFolder *iface )
3294 struct knownfolder *knownfolder = impl_from_IKnownFolder( iface );
3295 return InterlockedIncrement( &knownfolder->refs );
3298 static ULONG WINAPI knownfolder_Release(
3299 IKnownFolder *iface )
3301 struct knownfolder *knownfolder = impl_from_IKnownFolder( iface );
3302 LONG refs = InterlockedDecrement( &knownfolder->refs );
3303 if (!refs)
3305 TRACE("destroying %p\n", knownfolder);
3306 HeapFree( GetProcessHeap(), 0, knownfolder->registryPath);
3307 HeapFree( GetProcessHeap(), 0, knownfolder );
3309 return refs;
3312 static HRESULT WINAPI knownfolder_QueryInterface(
3313 IKnownFolder *iface,
3314 REFIID riid,
3315 void **ppv )
3317 struct knownfolder *This = impl_from_IKnownFolder( iface );
3319 TRACE("%p %s %p\n", This, debugstr_guid( riid ), ppv );
3321 if ( IsEqualGUID( riid, &IID_IKnownFolder ) ||
3322 IsEqualGUID( riid, &IID_IUnknown ) )
3324 *ppv = iface;
3326 else
3328 FIXME("interface %s not implemented\n", debugstr_guid(riid));
3329 return E_NOINTERFACE;
3331 IKnownFolder_AddRef( iface );
3332 return S_OK;
3335 static HRESULT knownfolder_set_id(
3336 struct knownfolder *knownfolder,
3337 const KNOWNFOLDERID *kfid)
3339 HKEY hKey;
3340 HRESULT hr;
3342 TRACE("%s\n", debugstr_guid(kfid));
3344 knownfolder->id = *kfid;
3346 /* check is it registry-registered folder */
3347 hr = get_known_folder_registry_path(kfid, NULL, &knownfolder->registryPath);
3348 if(SUCCEEDED(hr))
3349 hr = HRESULT_FROM_WIN32(RegOpenKeyExW(HKEY_LOCAL_MACHINE, knownfolder->registryPath, 0, 0, &hKey));
3351 if(SUCCEEDED(hr))
3353 hr = S_OK;
3354 RegCloseKey(hKey);
3356 else
3358 /* This known folder is not registered. To mark it, we set registryPath to NULL */
3359 HeapFree(GetProcessHeap(), 0, knownfolder->registryPath);
3360 knownfolder->registryPath = NULL;
3361 hr = S_OK;
3364 return hr;
3367 static HRESULT WINAPI knownfolder_GetId(
3368 IKnownFolder *iface,
3369 KNOWNFOLDERID *pkfid)
3371 struct knownfolder *knownfolder = impl_from_IKnownFolder( iface );
3373 TRACE("%p\n", pkfid);
3375 *pkfid = knownfolder->id;
3376 return S_OK;
3379 static HRESULT WINAPI knownfolder_GetCategory(
3380 IKnownFolder *iface,
3381 KF_CATEGORY *pCategory)
3383 struct knownfolder *knownfolder = impl_from_IKnownFolder(iface);
3384 HRESULT hr = S_OK;
3386 TRACE("%p, %p\n", knownfolder, pCategory);
3388 /* we cannot get a category for a folder which is not registered */
3389 if(!knownfolder->registryPath)
3390 hr = E_FAIL;
3392 if(SUCCEEDED(hr))
3393 hr = get_known_folder_category(knownfolder->registryPath, pCategory);
3395 return hr;
3398 static HRESULT WINAPI knownfolder_GetShellItem(
3399 IKnownFolder *iface,
3400 DWORD dwFlags,
3401 REFIID riid,
3402 void **ppv)
3404 FIXME("0x%08x, %s, %p\n", dwFlags, debugstr_guid(riid), ppv);
3405 return E_NOTIMPL;
3408 static HRESULT get_known_folder_path(
3409 LPWSTR sFolderId,
3410 LPWSTR registryPath,
3411 LPWSTR *ppszPath)
3413 static const WCHAR sBackslash[] = {'\\',0};
3414 HRESULT hr;
3415 DWORD dwSize, dwType;
3416 WCHAR path[MAX_PATH] = {0};
3417 WCHAR parentGuid[39];
3418 KF_CATEGORY category;
3419 LPWSTR parentRegistryPath, parentPath;
3420 HKEY hRedirectionRootKey = NULL;
3422 TRACE("(%s, %p)\n", debugstr_w(registryPath), ppszPath);
3423 *ppszPath = NULL;
3425 /* check if folder has parent */
3426 dwSize = sizeof(parentGuid);
3427 hr = HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE, registryPath, szParentFolder, RRF_RT_REG_SZ, &dwType, parentGuid, &dwSize));
3428 if(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) hr = S_FALSE;
3430 if(hr == S_OK)
3432 /* get parent's known folder path (recursive) */
3433 hr = get_known_folder_registry_path(NULL, parentGuid, &parentRegistryPath);
3434 if(FAILED(hr)) return hr;
3436 hr = get_known_folder_path(parentGuid, parentRegistryPath, &parentPath);
3437 if(FAILED(hr)) {
3438 HeapFree(GetProcessHeap(), 0, parentRegistryPath);
3439 return hr;
3442 lstrcatW(path, parentPath);
3443 lstrcatW(path, sBackslash);
3445 HeapFree(GetProcessHeap(), 0, parentRegistryPath);
3446 HeapFree(GetProcessHeap(), 0, parentPath);
3449 /* check, if folder was redirected */
3450 if(SUCCEEDED(hr))
3451 hr = HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE, registryPath, szCategory, RRF_RT_REG_DWORD, NULL, &category, &dwSize));
3453 if(SUCCEEDED(hr))
3455 if(category == KF_CATEGORY_COMMON)
3456 hRedirectionRootKey = HKEY_LOCAL_MACHINE;
3457 else if(category == KF_CATEGORY_PERUSER)
3458 hRedirectionRootKey = HKEY_CURRENT_USER;
3460 if(hRedirectionRootKey)
3462 hr = HRESULT_FROM_WIN32(RegGetValueW(hRedirectionRootKey, szKnownFolderRedirections, sFolderId, RRF_RT_REG_SZ, NULL, NULL, &dwSize));
3464 if(SUCCEEDED(hr))
3466 *ppszPath = CoTaskMemAlloc(dwSize+(lstrlenW(path)+1)*sizeof(WCHAR));
3467 if(!*ppszPath) hr = E_OUTOFMEMORY;
3470 if(SUCCEEDED(hr))
3472 lstrcpyW(*ppszPath, path);
3473 hr = HRESULT_FROM_WIN32(RegGetValueW(hRedirectionRootKey, szKnownFolderRedirections, sFolderId, RRF_RT_REG_SZ, NULL, *ppszPath + lstrlenW(path), &dwSize));
3477 if(!*ppszPath)
3479 /* no redirection, use previous way - read the relative path from folder definition */
3480 hr = HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE, registryPath, szRelativePath, RRF_RT_REG_SZ, &dwType, NULL, &dwSize));
3482 if(SUCCEEDED(hr))
3484 *ppszPath = CoTaskMemAlloc(dwSize+(lstrlenW(path)+1)*sizeof(WCHAR));
3485 if(!*ppszPath) hr = E_OUTOFMEMORY;
3488 if(SUCCEEDED(hr))
3490 lstrcpyW(*ppszPath, path);
3491 hr = HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE, registryPath, szRelativePath, RRF_RT_REG_SZ, &dwType, *ppszPath + lstrlenW(path), &dwSize));
3496 TRACE("returning path: %s\n", debugstr_w(*ppszPath));
3497 return hr;
3500 static HRESULT get_known_folder_path_by_id(
3501 REFKNOWNFOLDERID folderId,
3502 LPWSTR lpRegistryPath,
3503 DWORD dwFlags,
3504 LPWSTR *ppszPath)
3506 HRESULT hr;
3507 WCHAR sGuid[39];
3508 DWORD dwAttributes;
3510 TRACE("(%s, %s, 0x%08x, %p)\n", debugstr_guid(folderId), debugstr_w(lpRegistryPath), dwFlags, ppszPath);
3512 /* if this is registry-registered known folder, get path from registry */
3513 if(lpRegistryPath)
3515 StringFromGUID2(folderId, sGuid, sizeof(sGuid)/sizeof(sGuid[0]));
3517 hr = get_known_folder_path(sGuid, lpRegistryPath, ppszPath);
3519 /* in other case, use older way */
3520 else
3521 hr = SHGetKnownFolderPath( folderId, dwFlags, NULL, ppszPath );
3523 /* check if known folder really exists */
3524 dwAttributes = GetFileAttributesW(*ppszPath);
3525 if(dwAttributes == INVALID_FILE_ATTRIBUTES || !(dwAttributes & FILE_ATTRIBUTE_DIRECTORY) )
3527 TRACE("directory %s not found\n", debugstr_w(*ppszPath));
3528 CoTaskMemFree(*ppszPath);
3529 *ppszPath = NULL;
3530 hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
3533 return hr;
3536 static HRESULT WINAPI knownfolder_GetPath(
3537 IKnownFolder *iface,
3538 DWORD dwFlags,
3539 LPWSTR *ppszPath)
3541 struct knownfolder *knownfolder = impl_from_IKnownFolder( iface );
3542 TRACE("(%p, 0x%08x, %p)\n", knownfolder, dwFlags, ppszPath);
3544 return get_known_folder_path_by_id(&knownfolder->id, knownfolder->registryPath, dwFlags, ppszPath);
3547 static HRESULT WINAPI knownfolder_SetPath(
3548 IKnownFolder *iface,
3549 DWORD dwFlags,
3550 LPCWSTR pszPath)
3552 struct knownfolder *knownfolder = impl_from_IKnownFolder( iface );
3553 HRESULT hr = S_OK;
3555 TRACE("(%p, 0x%08x, %p)\n", knownfolder, dwFlags, debugstr_w(pszPath));
3557 /* check if the known folder is registered */
3558 if(!knownfolder->registryPath)
3559 hr = E_FAIL;
3561 if(SUCCEEDED(hr))
3562 hr = redirect_known_folder(&knownfolder->id, NULL, 0, pszPath, 0, NULL, NULL);
3564 return hr;
3567 static HRESULT WINAPI knownfolder_GetIDList(
3568 IKnownFolder *iface,
3569 DWORD dwFlags,
3570 PIDLIST_ABSOLUTE *ppidl)
3572 FIXME("0x%08x, %p\n", dwFlags, ppidl);
3573 return E_NOTIMPL;
3576 static HRESULT WINAPI knownfolder_GetFolderType(
3577 IKnownFolder *iface,
3578 FOLDERTYPEID *pftid)
3580 FIXME("%p\n", pftid);
3581 return E_NOTIMPL;
3584 static HRESULT WINAPI knownfolder_GetRedirectionCapabilities(
3585 IKnownFolder *iface,
3586 KF_REDIRECTION_CAPABILITIES *pCapabilities)
3588 FIXME("%p\n", pCapabilities);
3589 return E_NOTIMPL;
3592 static HRESULT WINAPI knownfolder_GetFolderDefinition(
3593 IKnownFolder *iface,
3594 KNOWNFOLDER_DEFINITION *pKFD)
3596 struct knownfolder *knownfolder = impl_from_IKnownFolder( iface );
3597 HRESULT hr;
3598 DWORD dwSize;
3599 TRACE("(%p, %p)\n", knownfolder, pKFD);
3601 if(!pKFD) return E_INVALIDARG;
3603 ZeroMemory(pKFD, sizeof(*pKFD));
3605 hr = get_known_folder_category(knownfolder->registryPath, &pKFD->category);
3607 if(SUCCEEDED(hr))
3608 hr = HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE, knownfolder->registryPath, szName, RRF_RT_REG_SZ, NULL, NULL, &dwSize));
3610 if(SUCCEEDED(hr))
3612 pKFD->pszName = CoTaskMemAlloc(dwSize);
3613 if(!pKFD->pszName) hr = E_OUTOFMEMORY;
3616 if(SUCCEEDED(hr))
3617 hr = HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE, knownfolder->registryPath, szName, RRF_RT_REG_SZ, NULL, pKFD->pszName, &dwSize));
3619 return hr;
3622 static const struct IKnownFolderVtbl knownfolder_vtbl =
3624 knownfolder_QueryInterface,
3625 knownfolder_AddRef,
3626 knownfolder_Release,
3627 knownfolder_GetId,
3628 knownfolder_GetCategory,
3629 knownfolder_GetShellItem,
3630 knownfolder_GetPath,
3631 knownfolder_SetPath,
3632 knownfolder_GetIDList,
3633 knownfolder_GetFolderType,
3634 knownfolder_GetRedirectionCapabilities,
3635 knownfolder_GetFolderDefinition
3638 static HRESULT knownfolder_create( struct knownfolder **knownfolder )
3640 struct knownfolder *kf;
3642 kf = HeapAlloc( GetProcessHeap(), 0, sizeof(*kf) );
3643 if (!kf) return E_OUTOFMEMORY;
3645 kf->IKnownFolder_iface.lpVtbl = &knownfolder_vtbl;
3646 kf->refs = 1;
3647 memset( &kf->id, 0, sizeof(kf->id) );
3648 kf->registryPath = NULL;
3650 *knownfolder = kf;
3652 TRACE("returning iface %p\n", &kf->IKnownFolder_iface);
3653 return S_OK;
3656 struct foldermanager
3658 IKnownFolderManager IKnownFolderManager_iface;
3659 LONG refs;
3660 UINT num_ids;
3661 KNOWNFOLDERID *ids;
3664 static inline struct foldermanager *impl_from_IKnownFolderManager( IKnownFolderManager *iface )
3666 return CONTAINING_RECORD( iface, struct foldermanager, IKnownFolderManager_iface );
3669 static ULONG WINAPI foldermanager_AddRef(
3670 IKnownFolderManager *iface )
3672 struct foldermanager *foldermanager = impl_from_IKnownFolderManager( iface );
3673 return InterlockedIncrement( &foldermanager->refs );
3676 static ULONG WINAPI foldermanager_Release(
3677 IKnownFolderManager *iface )
3679 struct foldermanager *foldermanager = impl_from_IKnownFolderManager( iface );
3680 LONG refs = InterlockedDecrement( &foldermanager->refs );
3681 if (!refs)
3683 TRACE("destroying %p\n", foldermanager);
3684 HeapFree( GetProcessHeap(), 0, foldermanager->ids );
3685 HeapFree( GetProcessHeap(), 0, foldermanager );
3687 return refs;
3690 static HRESULT WINAPI foldermanager_QueryInterface(
3691 IKnownFolderManager *iface,
3692 REFIID riid,
3693 void **ppv )
3695 struct foldermanager *This = impl_from_IKnownFolderManager( iface );
3697 TRACE("%p %s %p\n", This, debugstr_guid( riid ), ppv );
3699 if ( IsEqualGUID( riid, &IID_IKnownFolderManager ) ||
3700 IsEqualGUID( riid, &IID_IUnknown ) )
3702 *ppv = iface;
3704 else
3706 FIXME("interface %s not implemented\n", debugstr_guid(riid));
3707 return E_NOINTERFACE;
3709 IKnownFolderManager_AddRef( iface );
3710 return S_OK;
3713 static HRESULT WINAPI foldermanager_FolderIdFromCsidl(
3714 IKnownFolderManager *iface,
3715 int nCsidl,
3716 KNOWNFOLDERID *pfid)
3718 TRACE("%d, %p\n", nCsidl, pfid);
3720 if (nCsidl >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
3721 return E_INVALIDARG;
3722 *pfid = *CSIDL_Data[nCsidl].id;
3723 return S_OK;
3726 static HRESULT WINAPI foldermanager_FolderIdToCsidl(
3727 IKnownFolderManager *iface,
3728 REFKNOWNFOLDERID rfid,
3729 int *pnCsidl)
3731 int csidl;
3733 TRACE("%s, %p\n", debugstr_guid(rfid), pnCsidl);
3735 csidl = csidl_from_id( rfid );
3736 if (csidl == -1) return E_INVALIDARG;
3737 *pnCsidl = csidl;
3738 return S_OK;
3741 static HRESULT WINAPI foldermanager_GetFolderIds(
3742 IKnownFolderManager *iface,
3743 KNOWNFOLDERID **ppKFId,
3744 UINT *pCount)
3746 struct foldermanager *fm = impl_from_IKnownFolderManager( iface );
3748 TRACE("%p, %p\n", ppKFId, pCount);
3750 *ppKFId = fm->ids;
3751 *pCount = fm->num_ids;
3752 return S_OK;
3755 static BOOL is_knownfolder( struct foldermanager *fm, const KNOWNFOLDERID *id )
3757 UINT i;
3758 HRESULT hr;
3759 LPWSTR registryPath = NULL;
3760 HKEY hKey;
3762 /* TODO: move all entries from "CSIDL_Data" static array to registry known folder descriptions */
3763 for (i = 0; i < fm->num_ids; i++)
3764 if (IsEqualGUID( &fm->ids[i], id )) return TRUE;
3766 hr = get_known_folder_registry_path(id, NULL, &registryPath);
3767 if(SUCCEEDED(hr))
3768 hr = HRESULT_FROM_WIN32(RegOpenKeyExW(HKEY_LOCAL_MACHINE, registryPath, 0, 0, &hKey));
3770 if(SUCCEEDED(hr))
3772 hr = S_OK;
3773 RegCloseKey(hKey);
3776 return hr == S_OK;
3779 static HRESULT WINAPI foldermanager_GetFolder(
3780 IKnownFolderManager *iface,
3781 REFKNOWNFOLDERID rfid,
3782 IKnownFolder **ppkf)
3784 struct foldermanager *fm = impl_from_IKnownFolderManager( iface );
3785 struct knownfolder *kf;
3786 HRESULT hr;
3788 TRACE("%s, %p\n", debugstr_guid(rfid), ppkf);
3790 if (!is_knownfolder( fm, rfid ))
3792 WARN("unknown folder\n");
3793 return E_INVALIDARG;
3795 hr = knownfolder_create( &kf );
3796 if (SUCCEEDED( hr ))
3798 hr = knownfolder_set_id( kf, rfid );
3799 *ppkf = &kf->IKnownFolder_iface;
3801 else
3802 *ppkf = NULL;
3804 return hr;
3807 static HRESULT WINAPI foldermanager_GetFolderByName(
3808 IKnownFolderManager *iface,
3809 LPCWSTR pszCanonicalName,
3810 IKnownFolder **ppkf)
3812 FIXME("%s, %p\n", debugstr_w(pszCanonicalName), ppkf);
3813 return E_NOTIMPL;
3816 static HRESULT WINAPI foldermanager_RegisterFolder(
3817 IKnownFolderManager *iface,
3818 REFKNOWNFOLDERID rfid,
3819 KNOWNFOLDER_DEFINITION const *pKFD)
3821 HRESULT hr;
3822 HKEY hKey = NULL;
3823 DWORD dwDisp;
3824 LPWSTR registryPath = NULL;
3825 TRACE("(%p, %s, %p)\n", iface, debugstr_guid(rfid), pKFD);
3827 hr = get_known_folder_registry_path(rfid, NULL, &registryPath);
3828 TRACE("registry path: %s\n", debugstr_w(registryPath));
3830 if(SUCCEEDED(hr))
3831 hr = HRESULT_FROM_WIN32(RegCreateKeyExW(HKEY_LOCAL_MACHINE, registryPath, 0, NULL, 0, KEY_WRITE, 0, &hKey, &dwDisp));
3833 if(SUCCEEDED(hr))
3835 if(dwDisp == REG_OPENED_EXISTING_KEY)
3836 hr = E_FAIL;
3838 if(SUCCEEDED(hr))
3839 hr = HRESULT_FROM_WIN32(RegSetValueExW(hKey, szCategory, 0, REG_DWORD, (LPBYTE)&pKFD->category, sizeof(pKFD->category)));
3841 if(SUCCEEDED(hr))
3842 hr = HRESULT_FROM_WIN32(RegSetValueExW(hKey, szName, 0, REG_SZ, (LPBYTE)pKFD->pszName, (lstrlenW(pKFD->pszName)+1)*sizeof(WCHAR) ));
3844 if(SUCCEEDED(hr) && !IsEqualGUID(&pKFD->fidParent, &GUID_NULL))
3846 WCHAR sParentGuid[39];
3847 StringFromGUID2(&pKFD->fidParent, sParentGuid, sizeof(sParentGuid)/sizeof(sParentGuid[0]));
3849 /* this known folder has parent folder */
3850 hr = HRESULT_FROM_WIN32(RegSetValueExW(hKey, szParentFolder, 0, REG_SZ, (LPBYTE)sParentGuid, sizeof(sParentGuid)));
3853 if(SUCCEEDED(hr) && pKFD->category != KF_CATEGORY_VIRTUAL)
3855 if(!pKFD->pszRelativePath)
3856 hr = E_INVALIDARG;
3858 if(SUCCEEDED(hr))
3859 hr = HRESULT_FROM_WIN32(RegSetValueExW(hKey, szRelativePath, 0, REG_SZ, (LPBYTE)pKFD->pszRelativePath, (lstrlenW(pKFD->pszRelativePath)+1)*sizeof(WCHAR) ));
3862 RegCloseKey(hKey);
3864 if(FAILED(hr))
3865 SHDeleteKeyW(HKEY_LOCAL_MACHINE, registryPath);
3868 HeapFree(GetProcessHeap(), 0, registryPath);
3869 return hr;
3872 static HRESULT WINAPI foldermanager_UnregisterFolder(
3873 IKnownFolderManager *iface,
3874 REFKNOWNFOLDERID rfid)
3876 HRESULT hr;
3877 LPWSTR registryPath = NULL;
3878 TRACE("(%p, %s)\n", iface, debugstr_guid(rfid));
3880 hr = get_known_folder_registry_path(rfid, NULL, &registryPath);
3882 if(SUCCEEDED(hr))
3883 hr = HRESULT_FROM_WIN32(SHDeleteKeyW(HKEY_LOCAL_MACHINE, registryPath));
3885 HeapFree(GetProcessHeap(), 0, registryPath);
3886 return hr;
3889 static HRESULT WINAPI foldermanager_FindFolderFromPath(
3890 IKnownFolderManager *iface,
3891 LPCWSTR pszPath,
3892 FFFP_MODE mode,
3893 IKnownFolder **ppkf)
3895 FIXME("%s, 0x%08x, %p\n", debugstr_w(pszPath), mode, ppkf);
3896 return E_NOTIMPL;
3899 static HRESULT WINAPI foldermanager_FindFolderFromIDList(
3900 IKnownFolderManager *iface,
3901 PCIDLIST_ABSOLUTE pidl,
3902 IKnownFolder **ppkf)
3904 FIXME("%p, %p\n", pidl, ppkf);
3905 return E_NOTIMPL;
3908 static HRESULT WINAPI foldermanager_Redirect(
3909 IKnownFolderManager *iface,
3910 REFKNOWNFOLDERID rfid,
3911 HWND hwnd,
3912 KF_REDIRECT_FLAGS flags,
3913 LPCWSTR pszTargetPath,
3914 UINT cFolders,
3915 KNOWNFOLDERID const *pExclusion,
3916 LPWSTR *ppszError)
3918 return redirect_known_folder(rfid, hwnd, flags, pszTargetPath, cFolders, pExclusion, ppszError);
3921 static const struct IKnownFolderManagerVtbl foldermanager_vtbl =
3923 foldermanager_QueryInterface,
3924 foldermanager_AddRef,
3925 foldermanager_Release,
3926 foldermanager_FolderIdFromCsidl,
3927 foldermanager_FolderIdToCsidl,
3928 foldermanager_GetFolderIds,
3929 foldermanager_GetFolder,
3930 foldermanager_GetFolderByName,
3931 foldermanager_RegisterFolder,
3932 foldermanager_UnregisterFolder,
3933 foldermanager_FindFolderFromPath,
3934 foldermanager_FindFolderFromIDList,
3935 foldermanager_Redirect
3938 static HRESULT foldermanager_create( void **ppv )
3940 UINT i, j;
3941 struct foldermanager *fm;
3943 fm = HeapAlloc( GetProcessHeap(), 0, sizeof(*fm) );
3944 if (!fm) return E_OUTOFMEMORY;
3946 fm->IKnownFolderManager_iface.lpVtbl = &foldermanager_vtbl;
3947 fm->refs = 1;
3948 fm->num_ids = 0;
3950 for (i = 0; i < sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]); i++)
3952 if (!IsEqualGUID( CSIDL_Data[i].id, &GUID_NULL )) fm->num_ids++;
3954 fm->ids = HeapAlloc( GetProcessHeap(), 0, fm->num_ids * sizeof(KNOWNFOLDERID) );
3955 if (!fm->ids)
3957 HeapFree( GetProcessHeap(), 0, fm );
3958 return E_OUTOFMEMORY;
3960 for (i = j = 0; i < sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]); i++)
3962 if (!IsEqualGUID( CSIDL_Data[i].id, &GUID_NULL ))
3964 fm->ids[j] = *CSIDL_Data[i].id;
3965 j++;
3968 TRACE("found %u known folders\n", fm->num_ids);
3969 *ppv = &fm->IKnownFolderManager_iface;
3971 TRACE("returning iface %p\n", *ppv);
3972 return S_OK;
3975 HRESULT WINAPI KnownFolderManager_Constructor( IUnknown *punk, REFIID riid, void **ppv )
3977 TRACE("%p, %s, %p\n", punk, debugstr_guid(riid), ppv);
3979 if (!ppv)
3980 return E_POINTER;
3981 if (punk)
3982 return CLASS_E_NOAGGREGATION;
3984 return foldermanager_create( ppv );