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
23 * Many of these functions are in SHLWAPI.DLL also
30 #include "wine/port.h"
36 #include "wine/debug.h"
47 #include "shell32_main.h"
48 #include "undocshell.h"
50 #include "wine/unicode.h"
54 #include "knownfolders.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(
73 if (SHELL_OsIsUnicode())
74 return PathAppendW(lpszPath1
, lpszPath2
);
75 return PathAppendA(lpszPath1
, lpszPath2
);
78 /*************************************************************************
79 * PathCombine [SHELL32.37]
81 LPVOID WINAPI
PathCombineAW(
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]
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
);
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
);
231 PathRemoveArgsA(lpszPath
);
234 /*************************************************************************
235 * PathRemoveExtension [SHELL32.250]
237 void WINAPI
PathRemoveExtensionAW(LPVOID lpszPath
)
239 if (SHELL_OsIsUnicode())
240 PathRemoveExtensionW(lpszPath
);
242 PathRemoveExtensionA(lpszPath
);
250 /*************************************************************************
251 * PathGetShortPathA [internal]
253 static void PathGetShortPathA(LPSTR pszPath
)
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
);
298 PathRemoveBlanksA(str
);
301 /*************************************************************************
302 * PathQuoteSpaces [SHELL32.55]
304 VOID WINAPI
PathQuoteSpacesAW (LPVOID lpszPath
)
306 if(SHELL_OsIsUnicode())
307 PathQuoteSpacesW(lpszPath
);
309 PathQuoteSpacesA(lpszPath
);
312 /*************************************************************************
313 * PathUnquoteSpaces [SHELL32.56]
315 VOID WINAPI
PathUnquoteSpacesAW(LPVOID str
)
317 if(SHELL_OsIsUnicode())
318 PathUnquoteSpacesW(str
);
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
);
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
;
384 /*************************************************************************
385 * PathIsExeW [internal]
387 static BOOL
PathIsExeW (LPCWSTR lpszPath
)
389 LPCWSTR lpszExtension
= PathGetExtensionW(lpszPath
);
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
;
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
)
461 if (!GetVolumeInformationA(lpszPath
, NULL
, 0, NULL
, &fnlen
, NULL
, NULL
, 0))
466 /*************************************************************************
467 * IsLFNDriveW [SHELL32.42]
469 BOOL WINAPI
IsLFNDriveW(LPCWSTR lpszPath
)
473 if (!GetVolumeInformationW(lpszPath
, NULL
, 0, NULL
, &fnlen
, NULL
, NULL
, 0))
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(
497 LPCSTR lpszShortName
,
501 FIXME("%p %u %s %s %s stub\n",
502 lpszBuffer
, dwBuffSize
, debugstr_a(lpszShortName
),
503 debugstr_a(lpszLongName
), debugstr_a(lpszPathName
));
507 /*************************************************************************
508 * PathMakeUniqueNameW [internal]
510 static BOOL
PathMakeUniqueNameW(
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
));
523 /*************************************************************************
524 * PathMakeUniqueName [SHELL32.47]
526 BOOL WINAPI
PathMakeUniqueNameAW(
529 LPCVOID lpszShortName
,
530 LPCVOID lpszLongName
,
531 LPCVOID lpszPathName
)
533 if (SHELL_OsIsUnicode())
534 return PathMakeUniqueNameW(lpszBuffer
,dwBuffSize
, lpszShortName
,lpszLongName
,lpszPathName
);
535 return PathMakeUniqueNameA(lpszBuffer
,dwBuffSize
, lpszShortName
,lpszLongName
,lpszPathName
);
538 /*************************************************************************
539 * PathYetAnotherMakeUniqueName [SHELL32.75]
541 BOOL WINAPI
PathYetAnotherMakeUniqueName(LPWSTR buffer
, LPCWSTR path
, LPCWSTR shortname
, LPCWSTR longname
)
543 WCHAR pathW
[MAX_PATH
], retW
[MAX_PATH
];
544 const WCHAR
*file
, *ext
;
547 TRACE("(%p, %s, %s, %s)\n", buffer
, debugstr_w(path
), debugstr_w(shortname
), debugstr_w(longname
));
549 file
= longname
? longname
: shortname
;
550 PathCombineW(pathW
, path
, file
);
551 strcpyW(retW
, pathW
);
552 PathRemoveExtensionW(pathW
);
554 ext
= PathFindExtensionW(file
);
556 /* now try to make it unique */
557 while (PathFileExistsW(retW
))
559 static const WCHAR fmtW
[] = {'%','s',' ','(','%','d',')','%','s',0};
561 sprintfW(retW
, fmtW
, pathW
, i
, ext
);
565 strcpyW(buffer
, retW
);
566 TRACE("ret - %s\n", debugstr_w(buffer
));
572 ########## cleaning and resolving paths ##########
575 /*************************************************************************
576 * PathFindOnPath [SHELL32.145]
578 BOOL WINAPI
PathFindOnPathAW(LPVOID sFile
, LPCVOID
*sOtherDirs
)
580 if (SHELL_OsIsUnicode())
581 return PathFindOnPathW(sFile
, (LPCWSTR
*)sOtherDirs
);
582 return PathFindOnPathA(sFile
, (LPCSTR
*)sOtherDirs
);
585 /*************************************************************************
586 * PathCleanupSpec [SHELL32.171]
588 * lpszFile is changed in place.
590 int WINAPI
PathCleanupSpec( LPCWSTR lpszPathW
, LPWSTR lpszFileW
)
596 if (SHELL_OsIsUnicode())
598 LPWSTR p
= lpszFileW
;
600 TRACE("Cleanup %s\n",debugstr_w(lpszFileW
));
603 length
= strlenW(lpszPathW
);
607 int gct
= PathGetCharTypeW(*p
);
608 if (gct
== GCT_INVALID
|| gct
== GCT_WILD
|| gct
== GCT_SEPARATOR
)
611 rc
|= PCS_REPLACEDCHAR
;
617 if (length
+ i
== MAX_PATH
)
619 rc
|= PCS_FATAL
| PCS_PATHTOOLONG
;
627 LPSTR lpszFileA
= (LPSTR
)lpszFileW
;
628 LPCSTR lpszPathA
= (LPCSTR
)lpszPathW
;
631 TRACE("Cleanup %s\n",debugstr_a(lpszFileA
));
634 length
= strlen(lpszPathA
);
638 int gct
= PathGetCharTypeA(*p
);
639 if (gct
== GCT_INVALID
|| gct
== GCT_WILD
|| gct
== GCT_SEPARATOR
)
642 rc
|= PCS_REPLACEDCHAR
;
648 if (length
+ i
== MAX_PATH
)
650 rc
|= PCS_FATAL
| PCS_PATHTOOLONG
;
659 /*************************************************************************
660 * PathQualifyA [SHELL32]
662 static BOOL
PathQualifyA(LPCSTR pszPath
)
664 FIXME("%s\n",pszPath
);
668 /*************************************************************************
669 * PathQualifyW [SHELL32]
671 static BOOL
PathQualifyW(LPCWSTR pszPath
)
673 FIXME("%s\n",debugstr_w(pszPath
));
677 /*************************************************************************
678 * PathQualify [SHELL32.49]
680 BOOL WINAPI
PathQualifyAW(LPCVOID pszPath
)
682 if (SHELL_OsIsUnicode())
683 return PathQualifyW(pszPath
);
684 return PathQualifyA(pszPath
);
687 static BOOL
PathResolveA(LPSTR path
, LPCSTR
*paths
, DWORD flags
)
689 FIXME("(%s,%p,0x%08x),stub!\n", debugstr_a(path
), paths
, flags
);
693 static BOOL
PathResolveW(LPWSTR path
, LPCWSTR
*paths
, DWORD flags
)
695 FIXME("(%s,%p,0x%08x),stub!\n", debugstr_w(path
), paths
, flags
);
699 /*************************************************************************
700 * PathResolve [SHELL32.51]
702 BOOL WINAPI
PathResolveAW(LPVOID path
, LPCVOID
*paths
, DWORD flags
)
704 if (SHELL_OsIsUnicode())
705 return PathResolveW(path
, (LPCWSTR
*)paths
, flags
);
707 return PathResolveA(path
, (LPCSTR
*)paths
, flags
);
710 /*************************************************************************
711 * PathProcessCommandA
713 static LONG
PathProcessCommandA (
719 FIXME("%s %p 0x%04x 0x%04x stub\n",
720 lpszPath
, lpszBuff
, dwBuffSize
, dwFlags
);
721 if(!lpszPath
) return -1;
722 if(lpszBuff
) strcpy(lpszBuff
, lpszPath
);
723 return strlen(lpszPath
);
726 /*************************************************************************
727 * PathProcessCommandW
729 static LONG
PathProcessCommandW (
735 FIXME("(%s, %p, 0x%04x, 0x%04x) stub\n",
736 debugstr_w(lpszPath
), lpszBuff
, dwBuffSize
, dwFlags
);
737 if(!lpszPath
) return -1;
738 if(lpszBuff
) strcpyW(lpszBuff
, lpszPath
);
739 return strlenW(lpszPath
);
742 /*************************************************************************
743 * PathProcessCommand (SHELL32.653)
745 LONG WINAPI
PathProcessCommandAW (
751 if (SHELL_OsIsUnicode())
752 return PathProcessCommandW(lpszPath
, lpszBuff
, dwBuffSize
, dwFlags
);
753 return PathProcessCommandA(lpszPath
, lpszBuff
, dwBuffSize
, dwFlags
);
757 ########## special ##########
760 /*************************************************************************
761 * PathSetDlgItemPath (SHELL32.48)
763 VOID WINAPI
PathSetDlgItemPathAW(HWND hDlg
, int id
, LPCVOID pszPath
)
765 if (SHELL_OsIsUnicode())
766 PathSetDlgItemPathW(hDlg
, id
, pszPath
);
768 PathSetDlgItemPathA(hDlg
, id
, pszPath
);
771 static const WCHAR szCategory
[] = {'C','a','t','e','g','o','r','y',0};
772 static const WCHAR szAttributes
[] = {'A','t','t','r','i','b','u','t','e','s',0};
773 static const WCHAR szName
[] = {'N','a','m','e',0};
774 static const WCHAR szRelativePath
[] = {'R','e','l','a','t','i','v','e','P','a','t','h',0};
775 static const WCHAR szParentFolder
[] = {'P','a','r','e','n','t','F','o','l','d','e','r',0};
777 static const WCHAR szCurrentVersion
[] = {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\0'};
778 static const WCHAR Administrative_ToolsW
[] = {'A','d','m','i','n','i','s','t','r','a','t','i','v','e',' ','T','o','o','l','s','\0'};
779 static const WCHAR AppDataW
[] = {'A','p','p','D','a','t','a','\0'};
780 static const WCHAR AppData_LocalLowW
[] = {'A','p','p','D','a','t','a','\\','L','o','c','a','l','L','o','w','\0'};
781 static const WCHAR Application_DataW
[] = {'A','p','p','l','i','c','a','t','i','o','n',' ','D','a','t','a','\0'};
782 static const WCHAR CacheW
[] = {'C','a','c','h','e','\0'};
783 static const WCHAR CD_BurningW
[] = {'C','D',' ','B','u','r','n','i','n','g','\0'};
784 static const WCHAR Common_Administrative_ToolsW
[] = {'C','o','m','m','o','n',' ','A','d','m','i','n','i','s','t','r','a','t','i','v','e',' ','T','o','o','l','s','\0'};
785 static const WCHAR Common_AppDataW
[] = {'C','o','m','m','o','n',' ','A','p','p','D','a','t','a','\0'};
786 static const WCHAR Common_DesktopW
[] = {'C','o','m','m','o','n',' ','D','e','s','k','t','o','p','\0'};
787 static const WCHAR Common_DocumentsW
[] = {'C','o','m','m','o','n',' ','D','o','c','u','m','e','n','t','s','\0'};
788 static const WCHAR Common_FavoritesW
[] = {'C','o','m','m','o','n',' ','F','a','v','o','r','i','t','e','s','\0'};
789 static const WCHAR CommonFilesDirW
[] = {'C','o','m','m','o','n','F','i','l','e','s','D','i','r','\0'};
790 static const WCHAR CommonFilesDirX86W
[] = {'C','o','m','m','o','n','F','i','l','e','s','D','i','r',' ','(','x','8','6',')','\0'};
791 static const WCHAR CommonMusicW
[] = {'C','o','m','m','o','n','M','u','s','i','c','\0'};
792 static const WCHAR CommonPicturesW
[] = {'C','o','m','m','o','n','P','i','c','t','u','r','e','s','\0'};
793 static const WCHAR Common_ProgramsW
[] = {'C','o','m','m','o','n',' ','P','r','o','g','r','a','m','s','\0'};
794 static const WCHAR Common_StartUpW
[] = {'C','o','m','m','o','n',' ','S','t','a','r','t','U','p','\0'};
795 static const WCHAR Common_Start_MenuW
[] = {'C','o','m','m','o','n',' ','S','t','a','r','t',' ','M','e','n','u','\0'};
796 static const WCHAR Common_TemplatesW
[] = {'C','o','m','m','o','n',' ','T','e','m','p','l','a','t','e','s','\0'};
797 static const WCHAR CommonVideoW
[] = {'C','o','m','m','o','n','V','i','d','e','o','\0'};
798 static const WCHAR ContactsW
[] = {'C','o','n','t','a','c','t','s','\0'};
799 static const WCHAR CookiesW
[] = {'C','o','o','k','i','e','s','\0'};
800 static const WCHAR DesktopW
[] = {'D','e','s','k','t','o','p','\0'};
801 static const WCHAR DocumentsW
[] = {'D','o','c','u','m','e','n','t','s','\0'};
802 static const WCHAR DownloadsW
[] = {'D','o','w','n','l','o','a','d','s','\0'};
803 static const WCHAR FavoritesW
[] = {'F','a','v','o','r','i','t','e','s','\0'};
804 static const WCHAR FontsW
[] = {'F','o','n','t','s','\0'};
805 static const WCHAR HistoryW
[] = {'H','i','s','t','o','r','y','\0'};
806 static const WCHAR LinksW
[] = {'L','i','n','k','s','\0'};
807 static const WCHAR Local_AppDataW
[] = {'L','o','c','a','l',' ','A','p','p','D','a','t','a','\0'};
808 static const WCHAR Local_Settings_Application_DataW
[] = {'L','o','c','a','l',' ','S','e','t','t','i','n','g','s','\\','A','p','p','l','i','c','a','t','i','o','n',' ','D','a','t','a','\0'};
809 static const WCHAR Local_Settings_CD_BurningW
[] = {'L','o','c','a','l',' ','S','e','t','t','i','n','g','s','\\','A','p','p','l','i','c','a','t','i','o','n',' ','D','a','t','a','\\','M','i','c','r','o','s','o','f','t','\\','C','D',' ','B','u','r','n','i','n','g','\0'};
810 static const WCHAR Local_Settings_HistoryW
[] = {'L','o','c','a','l',' ','S','e','t','t','i','n','g','s','\\','H','i','s','t','o','r','y','\0'};
811 static const WCHAR Local_Settings_Temporary_Internet_FilesW
[] = {'L','o','c','a','l',' ','S','e','t','t','i','n','g','s','\\','T','e','m','p','o','r','a','r','y',' ','I','n','t','e','r','n','e','t',' ','F','i','l','e','s','\0'};
812 static const WCHAR Microsoft_Windows_GameExplorerW
[] = {'M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\','G','a','m','e','E','x','p','l','o','r','e','r','\0'};
813 static const WCHAR Microsoft_Windows_LibrariesW
[] = {'M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\','L','i','b','r','a','r','i','e','s','\0'};
814 static const WCHAR Microsoft_Windows_RingtonesW
[] = {'M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\','R','i','n','g','t','o','n','e','s','\0'};
815 static const WCHAR MusicW
[] = {'M','u','s','i','c','\0'};
816 static const WCHAR Music_PlaylistsW
[] = {'M','u','s','i','c','\\','P','l','a','y','l','i','s','t','s','\0'};
817 static const WCHAR Music_Sample_MusicW
[] = {'M','u','s','i','c','\\','S','a','m','p','l','e',' ','M','u','s','i','c','\0'};
818 static const WCHAR Music_Sample_PlaylistsW
[] = {'M','u','s','i','c','\\','S','a','m','p','l','e',' ','P','l','a','y','l','i','s','t','s','\0'};
819 static const WCHAR My_MusicW
[] = {'M','y',' ','M','u','s','i','c','\0'};
820 static const WCHAR My_PicturesW
[] = {'M','y',' ','P','i','c','t','u','r','e','s','\0'};
821 static const WCHAR My_VideosW
[] = {'M','y',' ','V','i','d','e','o','s','\0'};
822 static const WCHAR NetHoodW
[] = {'N','e','t','H','o','o','d','\0'};
823 static const WCHAR OEM_LinksW
[] = {'O','E','M',' ','L','i','n','k','s','\0'};
824 static const WCHAR PersonalW
[] = {'P','e','r','s','o','n','a','l','\0'};
825 static const WCHAR PicturesW
[] = {'P','i','c','t','u','r','e','s','\0'};
826 static const WCHAR Pictures_Sample_PicturesW
[] = {'P','i','c','t','u','r','e','s','\\','S','a','m','p','l','e',' ','P','i','c','t','u','r','e','s','\0'};
827 static const WCHAR Pictures_Slide_ShowsW
[] = {'P','i','c','t','u','r','e','s','\\','S','l','i','d','e',' ','S','h','o','w','s','\0'};
828 static const WCHAR PrintHoodW
[] = {'P','r','i','n','t','H','o','o','d','\0'};
829 static const WCHAR Program_FilesW
[] = {'P','r','o','g','r','a','m',' ','F','i','l','e','s','\0'};
830 static const WCHAR Program_Files_Common_FilesW
[] = {'P','r','o','g','r','a','m',' ','F','i','l','e','s','\\','C','o','m','m','o','n',' ','F','i','l','e','s','\0'};
831 static const WCHAR Program_Files_x86W
[] = {'P','r','o','g','r','a','m',' ','F','i','l','e','s',' ','(','x','8','6',')','\0'};
832 static const WCHAR Program_Files_x86_Common_FilesW
[] = {'P','r','o','g','r','a','m',' ','F','i','l','e','s',' ','(','x','8','6',')','\\','C','o','m','m','o','n',' ','F','i','l','e','s','\0'};
833 static const WCHAR ProgramFilesDirW
[] = {'P','r','o','g','r','a','m','F','i','l','e','s','D','i','r','\0'};
834 static const WCHAR ProgramFilesDirX86W
[] = {'P','r','o','g','r','a','m','F','i','l','e','s','D','i','r',' ','(','x','8','6',')','\0'};
835 static const WCHAR ProgramsW
[] = {'P','r','o','g','r','a','m','s','\0'};
836 static const WCHAR RecentW
[] = {'R','e','c','e','n','t','\0'};
837 static const WCHAR ResourcesW
[] = {'R','e','s','o','u','r','c','e','s','\0'};
838 static const WCHAR Saved_GamesW
[] = {'S','a','v','e','d',' ','G','a','m','e','s','\0'};
839 static const WCHAR SearchesW
[] = {'S','e','a','r','c','h','e','s','\0'};
840 static const WCHAR SendToW
[] = {'S','e','n','d','T','o','\0'};
841 static const WCHAR StartUpW
[] = {'S','t','a','r','t','U','p','\0'};
842 static const WCHAR Start_MenuW
[] = {'S','t','a','r','t',' ','M','e','n','u','\0'};
843 static const WCHAR Start_Menu_ProgramsW
[] = {'S','t','a','r','t',' ','M','e','n','u','\\','P','r','o','g','r','a','m','s','\0'};
844 static const WCHAR Start_Menu_Admin_ToolsW
[] = {'S','t','a','r','t',' ','M','e','n','u','\\','P','r','o','g','r','a','m','s','\\','A','d','m','i','n','i','s','t','r','a','t','i','v','e',' ','T','o','o','l','s','\0'};
845 static const WCHAR Start_Menu_StartupW
[] = {'S','t','a','r','t',' ','M','e','n','u','\\','P','r','o','g','r','a','m','s','\\','S','t','a','r','t','U','p','\0'};
846 static const WCHAR TemplatesW
[] = {'T','e','m','p','l','a','t','e','s','\0'};
847 static const WCHAR UsersW
[] = {'U','s','e','r','s','\0'};
848 static const WCHAR UsersPublicW
[] = {'U','s','e','r','s','\\','P','u','b','l','i','c','\0'};
849 static const WCHAR VideosW
[] = {'V','i','d','e','o','s','\0'};
850 static const WCHAR Videos_Sample_VideosW
[] = {'V','i','d','e','o','s','\\','S','a','m','p','l','e',' ','V','i','d','e','o','s','\0'};
851 static const WCHAR DefaultW
[] = {'.','D','e','f','a','u','l','t','\0'};
852 static const WCHAR AllUsersProfileW
[] = {'%','A','L','L','U','S','E','R','S','P','R','O','F','I','L','E','%','\0'};
853 static const WCHAR UserProfileW
[] = {'%','U','S','E','R','P','R','O','F','I','L','E','%','\0'};
854 static const WCHAR SystemDriveW
[] = {'%','S','y','s','t','e','m','D','r','i','v','e','%','\0'};
855 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};
856 static const WCHAR ProfilesDirectoryW
[] = {'P','r','o','f','i','l','e','s','D','i','r','e','c','t','o','r','y',0};
857 static const WCHAR AllUsersProfileValueW
[] = {'A','l','l','U','s','e','r','s','P','r','o','f','i','l','e','\0'};
858 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'};
859 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'};
860 static const WCHAR szDefaultProfileDirW
[] = {'u','s','e','r','s',0};
861 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'};
862 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};
863 static const WCHAR AllUsersW
[] = {'P','u','b','l','i','c',0};
865 typedef enum _CSIDL_Type
{
869 CSIDL_Type_Disallowed
,
870 CSIDL_Type_NonExistent
,
871 CSIDL_Type_WindowsPath
,
872 CSIDL_Type_SystemPath
,
873 CSIDL_Type_SystemX86Path
,
876 #define CSIDL_CONTACTS 0x0043
877 #define CSIDL_DOWNLOADS 0x0047
878 #define CSIDL_LINKS 0x004d
879 #define CSIDL_APPDATA_LOCALLOW 0x004e
880 #define CSIDL_SAVED_GAMES 0x0062
881 #define CSIDL_SEARCHES 0x0063
885 const KNOWNFOLDERID
*id
;
888 LPCWSTR szDefaultPath
; /* fallback string or resource ID */
891 static const CSIDL_DATA CSIDL_Data
[] =
893 { /* 0x00 - CSIDL_DESKTOP */
897 MAKEINTRESOURCEW(IDS_DESKTOPDIRECTORY
)
899 { /* 0x01 - CSIDL_INTERNET */
900 &FOLDERID_InternetFolder
,
901 CSIDL_Type_Disallowed
,
905 { /* 0x02 - CSIDL_PROGRAMS */
911 { /* 0x03 - CSIDL_CONTROLS (.CPL files) */
912 &FOLDERID_ControlPanelFolder
,
913 CSIDL_Type_SystemPath
,
917 { /* 0x04 - CSIDL_PRINTERS */
918 &FOLDERID_PrintersFolder
,
919 CSIDL_Type_SystemPath
,
923 { /* 0x05 - CSIDL_PERSONAL */
927 MAKEINTRESOURCEW(IDS_PERSONAL
)
929 { /* 0x06 - CSIDL_FAVORITES */
935 { /* 0x07 - CSIDL_STARTUP */
941 { /* 0x08 - CSIDL_RECENT */
947 { /* 0x09 - CSIDL_SENDTO */
953 { /* 0x0a - CSIDL_BITBUCKET - Recycle Bin */
954 &FOLDERID_RecycleBinFolder
,
955 CSIDL_Type_Disallowed
,
959 { /* 0x0b - CSIDL_STARTMENU */
965 { /* 0x0c - CSIDL_MYDOCUMENTS */
967 CSIDL_Type_Disallowed
, /* matches WinXP--can't get its path */
971 { /* 0x0d - CSIDL_MYMUSIC */
975 MAKEINTRESOURCEW(IDS_MYMUSIC
)
977 { /* 0x0e - CSIDL_MYVIDEO */
981 MAKEINTRESOURCEW(IDS_MYVIDEOS
)
983 { /* 0x0f - unassigned */
985 CSIDL_Type_Disallowed
,
989 { /* 0x10 - CSIDL_DESKTOPDIRECTORY */
993 MAKEINTRESOURCEW(IDS_DESKTOPDIRECTORY
)
995 { /* 0x11 - CSIDL_DRIVES */
996 &FOLDERID_ComputerFolder
,
997 CSIDL_Type_Disallowed
,
1001 { /* 0x12 - CSIDL_NETWORK */
1002 &FOLDERID_NetworkFolder
,
1003 CSIDL_Type_Disallowed
,
1007 { /* 0x13 - CSIDL_NETHOOD */
1013 { /* 0x14 - CSIDL_FONTS */
1015 CSIDL_Type_WindowsPath
,
1019 { /* 0x15 - CSIDL_TEMPLATES */
1020 &FOLDERID_Templates
,
1025 { /* 0x16 - CSIDL_COMMON_STARTMENU */
1026 &FOLDERID_CommonStartMenu
,
1027 CSIDL_Type_AllUsers
,
1031 { /* 0x17 - CSIDL_COMMON_PROGRAMS */
1032 &FOLDERID_CommonPrograms
,
1033 CSIDL_Type_AllUsers
,
1035 Start_Menu_ProgramsW
1037 { /* 0x18 - CSIDL_COMMON_STARTUP */
1038 &FOLDERID_CommonStartup
,
1039 CSIDL_Type_AllUsers
,
1043 { /* 0x19 - CSIDL_COMMON_DESKTOPDIRECTORY */
1044 &FOLDERID_PublicDesktop
,
1045 CSIDL_Type_AllUsers
,
1047 MAKEINTRESOURCEW(IDS_DESKTOPDIRECTORY
)
1049 { /* 0x1a - CSIDL_APPDATA */
1050 &FOLDERID_RoamingAppData
,
1055 { /* 0x1b - CSIDL_PRINTHOOD */
1056 &FOLDERID_PrintHood
,
1061 { /* 0x1c - CSIDL_LOCAL_APPDATA */
1062 &FOLDERID_LocalAppData
,
1065 Local_Settings_Application_DataW
1067 { /* 0x1d - CSIDL_ALTSTARTUP */
1069 CSIDL_Type_NonExistent
,
1073 { /* 0x1e - CSIDL_COMMON_ALTSTARTUP */
1075 CSIDL_Type_NonExistent
,
1079 { /* 0x1f - CSIDL_COMMON_FAVORITES */
1080 &FOLDERID_Favorites
,
1081 CSIDL_Type_AllUsers
,
1085 { /* 0x20 - CSIDL_INTERNET_CACHE */
1086 &FOLDERID_InternetCache
,
1089 Local_Settings_Temporary_Internet_FilesW
1091 { /* 0x21 - CSIDL_COOKIES */
1097 { /* 0x22 - CSIDL_HISTORY */
1101 Local_Settings_HistoryW
1103 { /* 0x23 - CSIDL_COMMON_APPDATA */
1104 &FOLDERID_ProgramData
,
1105 CSIDL_Type_AllUsers
,
1109 { /* 0x24 - CSIDL_WINDOWS */
1111 CSIDL_Type_WindowsPath
,
1115 { /* 0x25 - CSIDL_SYSTEM */
1117 CSIDL_Type_SystemPath
,
1121 { /* 0x26 - CSIDL_PROGRAM_FILES */
1122 &FOLDERID_ProgramFiles
,
1127 { /* 0x27 - CSIDL_MYPICTURES */
1131 MAKEINTRESOURCEW(IDS_MYPICTURES
)
1133 { /* 0x28 - CSIDL_PROFILE */
1139 { /* 0x29 - CSIDL_SYSTEMX86 */
1140 &FOLDERID_SystemX86
,
1141 CSIDL_Type_SystemX86Path
,
1145 { /* 0x2a - CSIDL_PROGRAM_FILESX86 */
1146 &FOLDERID_ProgramFilesX86
,
1148 ProgramFilesDirX86W
,
1151 { /* 0x2b - CSIDL_PROGRAM_FILES_COMMON */
1152 &FOLDERID_ProgramFilesCommon
,
1155 Program_Files_Common_FilesW
1157 { /* 0x2c - CSIDL_PROGRAM_FILES_COMMONX86 */
1158 &FOLDERID_ProgramFilesCommonX86
,
1161 Program_Files_x86_Common_FilesW
1163 { /* 0x2d - CSIDL_COMMON_TEMPLATES */
1164 &FOLDERID_CommonTemplates
,
1165 CSIDL_Type_AllUsers
,
1169 { /* 0x2e - CSIDL_COMMON_DOCUMENTS */
1170 &FOLDERID_PublicDocuments
,
1171 CSIDL_Type_AllUsers
,
1175 { /* 0x2f - CSIDL_COMMON_ADMINTOOLS */
1176 &FOLDERID_CommonAdminTools
,
1177 CSIDL_Type_AllUsers
,
1178 Common_Administrative_ToolsW
,
1179 Start_Menu_Admin_ToolsW
1181 { /* 0x30 - CSIDL_ADMINTOOLS */
1182 &FOLDERID_AdminTools
,
1184 Administrative_ToolsW
,
1185 Start_Menu_Admin_ToolsW
1187 { /* 0x31 - CSIDL_CONNECTIONS */
1188 &FOLDERID_ConnectionsFolder
,
1189 CSIDL_Type_Disallowed
,
1193 { /* 0x32 - unassigned */
1195 CSIDL_Type_Disallowed
,
1199 { /* 0x33 - unassigned */
1201 CSIDL_Type_Disallowed
,
1205 { /* 0x34 - unassigned */
1207 CSIDL_Type_Disallowed
,
1211 { /* 0x35 - CSIDL_COMMON_MUSIC */
1212 &FOLDERID_PublicMusic
,
1213 CSIDL_Type_AllUsers
,
1217 { /* 0x36 - CSIDL_COMMON_PICTURES */
1218 &FOLDERID_PublicPictures
,
1219 CSIDL_Type_AllUsers
,
1223 { /* 0x37 - CSIDL_COMMON_VIDEO */
1224 &FOLDERID_PublicVideos
,
1225 CSIDL_Type_AllUsers
,
1229 { /* 0x38 - CSIDL_RESOURCES */
1230 &FOLDERID_ResourceDir
,
1231 CSIDL_Type_WindowsPath
,
1235 { /* 0x39 - CSIDL_RESOURCES_LOCALIZED */
1236 &FOLDERID_LocalizedResourcesDir
,
1237 CSIDL_Type_NonExistent
,
1241 { /* 0x3a - CSIDL_COMMON_OEM_LINKS */
1242 &FOLDERID_CommonOEMLinks
,
1243 CSIDL_Type_AllUsers
,
1247 { /* 0x3b - CSIDL_CDBURN_AREA */
1248 &FOLDERID_CDBurning
,
1251 Local_Settings_CD_BurningW
1253 { /* 0x3c unassigned */
1255 CSIDL_Type_Disallowed
,
1259 { /* 0x3d - CSIDL_COMPUTERSNEARME */
1261 CSIDL_Type_Disallowed
, /* FIXME */
1265 { /* 0x3e - CSIDL_PROFILES */
1267 CSIDL_Type_Disallowed
, /* oddly, this matches WinXP */
1272 &FOLDERID_AddNewPrograms
,
1273 CSIDL_Type_Disallowed
,
1278 &FOLDERID_AppUpdates
,
1279 CSIDL_Type_Disallowed
,
1284 &FOLDERID_ChangeRemovePrograms
,
1285 CSIDL_Type_Disallowed
,
1290 &FOLDERID_ConflictFolder
,
1291 CSIDL_Type_Disallowed
,
1295 { /* 0x43 - CSIDL_CONTACTS */
1302 &FOLDERID_DeviceMetadataStore
,
1303 CSIDL_Type_Disallowed
, /* FIXME */
1314 &FOLDERID_DocumentsLibrary
,
1315 CSIDL_Type_Disallowed
, /* FIXME */
1319 { /* 0x47 - CSIDL_DOWNLOADS */
1320 &FOLDERID_Downloads
,
1327 CSIDL_Type_Disallowed
,
1332 &FOLDERID_GameTasks
,
1333 CSIDL_Type_Disallowed
, /* FIXME */
1338 &FOLDERID_HomeGroup
,
1339 CSIDL_Type_Disallowed
,
1344 &FOLDERID_ImplicitAppShortcuts
,
1345 CSIDL_Type_Disallowed
, /* FIXME */
1350 &FOLDERID_Libraries
,
1351 CSIDL_Type_Disallowed
, /* FIXME */
1355 { /* 0x4d - CSIDL_LINKS */
1361 { /* 0x4e - CSIDL_APPDATA_LOCALLOW */
1362 &FOLDERID_LocalAppDataLow
,
1368 &FOLDERID_MusicLibrary
,
1369 CSIDL_Type_Disallowed
, /* FIXME */
1374 &FOLDERID_OriginalImages
,
1375 CSIDL_Type_Disallowed
, /* FIXME */
1380 &FOLDERID_PhotoAlbums
,
1383 Pictures_Slide_ShowsW
1386 &FOLDERID_PicturesLibrary
,
1387 CSIDL_Type_Disallowed
, /* FIXME */
1392 &FOLDERID_Playlists
,
1398 &FOLDERID_ProgramFilesX64
,
1399 CSIDL_Type_NonExistent
,
1404 &FOLDERID_ProgramFilesCommonX64
,
1405 CSIDL_Type_NonExistent
,
1411 CSIDL_Type_CurrVer
, /* FIXME */
1416 &FOLDERID_PublicDownloads
,
1417 CSIDL_Type_AllUsers
,
1422 &FOLDERID_PublicGameTasks
,
1423 CSIDL_Type_AllUsers
,
1425 Microsoft_Windows_GameExplorerW
1428 &FOLDERID_PublicLibraries
,
1429 CSIDL_Type_AllUsers
,
1431 Microsoft_Windows_LibrariesW
1434 &FOLDERID_PublicRingtones
,
1435 CSIDL_Type_AllUsers
,
1437 Microsoft_Windows_RingtonesW
1440 &FOLDERID_QuickLaunch
,
1441 CSIDL_Type_Disallowed
, /* FIXME */
1446 &FOLDERID_RecordedTVLibrary
,
1447 CSIDL_Type_Disallowed
, /* FIXME */
1452 &FOLDERID_Ringtones
,
1453 CSIDL_Type_Disallowed
, /* FIXME */
1458 &FOLDERID_SampleMusic
,
1459 CSIDL_Type_AllUsers
,
1464 &FOLDERID_SamplePictures
,
1465 CSIDL_Type_AllUsers
,
1467 Pictures_Sample_PicturesW
1470 &FOLDERID_SamplePlaylists
,
1471 CSIDL_Type_AllUsers
,
1473 Music_Sample_PlaylistsW
1476 &FOLDERID_SampleVideos
,
1477 CSIDL_Type_AllUsers
,
1479 Videos_Sample_VideosW
1481 { /* 0x62 - CSIDL_SAVED_GAMES */
1482 &FOLDERID_SavedGames
,
1487 { /* 0x63 - CSIDL_SEARCHES */
1488 &FOLDERID_SavedSearches
,
1494 &FOLDERID_SEARCH_CSC
,
1495 CSIDL_Type_Disallowed
,
1500 &FOLDERID_SEARCH_MAPI
,
1501 CSIDL_Type_Disallowed
,
1506 &FOLDERID_SearchHome
,
1507 CSIDL_Type_Disallowed
,
1512 &FOLDERID_SidebarDefaultParts
,
1513 CSIDL_Type_Disallowed
, /* FIXME */
1518 &FOLDERID_SidebarParts
,
1519 CSIDL_Type_Disallowed
, /* FIXME */
1524 &FOLDERID_SyncManagerFolder
,
1525 CSIDL_Type_Disallowed
,
1530 &FOLDERID_SyncResultsFolder
,
1531 CSIDL_Type_Disallowed
,
1536 &FOLDERID_SyncSetupFolder
,
1537 CSIDL_Type_Disallowed
,
1542 &FOLDERID_UserPinned
,
1543 CSIDL_Type_Disallowed
, /* FIXME */
1548 &FOLDERID_UserProfiles
,
1554 &FOLDERID_UserProgramFiles
,
1555 CSIDL_Type_Disallowed
, /* FIXME */
1560 &FOLDERID_UserProgramFilesCommon
,
1561 CSIDL_Type_Disallowed
, /* FIXME */
1566 &FOLDERID_UsersFiles
,
1567 CSIDL_Type_Disallowed
,
1572 &FOLDERID_UsersLibraries
,
1573 CSIDL_Type_Disallowed
,
1578 &FOLDERID_VideosLibrary
,
1579 CSIDL_Type_Disallowed
, /* FIXME */
1585 static HRESULT
_SHExpandEnvironmentStrings(LPCWSTR szSrc
, LPWSTR szDest
);
1587 /* Gets the value named value from the registry key
1588 * rootKey\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
1589 * (or from rootKey\userPrefix\... if userPrefix is not NULL) into path, which
1590 * is assumed to be MAX_PATH WCHARs in length.
1591 * If it exists, expands the value and writes the expanded value to
1592 * rootKey\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders
1593 * Returns successful error code if the value was retrieved from the registry,
1594 * and a failure otherwise.
1596 static HRESULT
_SHGetUserShellFolderPath(HKEY rootKey
, LPCWSTR userPrefix
,
1597 LPCWSTR value
, LPWSTR path
)
1600 WCHAR shellFolderPath
[MAX_PATH
], userShellFolderPath
[MAX_PATH
];
1601 LPCWSTR pShellFolderPath
, pUserShellFolderPath
;
1602 DWORD dwType
, dwPathLen
= MAX_PATH
;
1603 HKEY userShellFolderKey
, shellFolderKey
;
1605 TRACE("%p,%s,%s,%p\n",rootKey
, debugstr_w(userPrefix
), debugstr_w(value
),
1610 strcpyW(shellFolderPath
, userPrefix
);
1611 PathAddBackslashW(shellFolderPath
);
1612 strcatW(shellFolderPath
, szSHFolders
);
1613 pShellFolderPath
= shellFolderPath
;
1614 strcpyW(userShellFolderPath
, userPrefix
);
1615 PathAddBackslashW(userShellFolderPath
);
1616 strcatW(userShellFolderPath
, szSHUserFolders
);
1617 pUserShellFolderPath
= userShellFolderPath
;
1621 pUserShellFolderPath
= szSHUserFolders
;
1622 pShellFolderPath
= szSHFolders
;
1625 if (RegCreateKeyW(rootKey
, pShellFolderPath
, &shellFolderKey
))
1627 TRACE("Failed to create %s\n", debugstr_w(pShellFolderPath
));
1630 if (RegCreateKeyW(rootKey
, pUserShellFolderPath
, &userShellFolderKey
))
1632 TRACE("Failed to create %s\n",
1633 debugstr_w(pUserShellFolderPath
));
1634 RegCloseKey(shellFolderKey
);
1638 if (!RegQueryValueExW(userShellFolderKey
, value
, NULL
, &dwType
,
1639 (LPBYTE
)path
, &dwPathLen
) && (dwType
== REG_EXPAND_SZ
|| dwType
== REG_SZ
))
1643 path
[dwPathLen
/ sizeof(WCHAR
)] = '\0';
1644 if (dwType
== REG_EXPAND_SZ
&& path
[0] == '%')
1646 WCHAR szTemp
[MAX_PATH
];
1648 _SHExpandEnvironmentStrings(path
, szTemp
);
1649 lstrcpynW(path
, szTemp
, MAX_PATH
);
1651 ret
= RegSetValueExW(shellFolderKey
, value
, 0, REG_SZ
, (LPBYTE
)path
,
1652 (strlenW(path
) + 1) * sizeof(WCHAR
));
1653 if (ret
!= ERROR_SUCCESS
)
1654 hr
= HRESULT_FROM_WIN32(ret
);
1660 RegCloseKey(shellFolderKey
);
1661 RegCloseKey(userShellFolderKey
);
1662 TRACE("returning 0x%08x\n", hr
);
1666 /* Gets a 'semi-expanded' default value of the CSIDL with index folder into
1667 * pszPath, based on the entries in CSIDL_Data. By semi-expanded, I mean:
1668 * - The entry's szDefaultPath may be either a string value or an integer
1669 * resource identifier. In the latter case, the string value of the resource
1671 * - Depending on the entry's type, the path may begin with an (unexpanded)
1672 * environment variable name. The caller is responsible for expanding
1673 * environment strings if so desired.
1674 * The types that are prepended with environment variables are:
1675 * CSIDL_Type_User: %USERPROFILE%
1676 * CSIDL_Type_AllUsers: %ALLUSERSPROFILE%
1677 * CSIDL_Type_CurrVer: %SystemDrive%
1678 * (Others might make sense too, but as yet are unneeded.)
1680 static HRESULT
_SHGetDefaultValue(BYTE folder
, LPWSTR pszPath
)
1683 WCHAR resourcePath
[MAX_PATH
];
1684 LPCWSTR pDefaultPath
= NULL
;
1686 TRACE("0x%02x,%p\n", folder
, pszPath
);
1688 if (folder
>= sizeof(CSIDL_Data
) / sizeof(CSIDL_Data
[0]))
1689 return E_INVALIDARG
;
1691 return E_INVALIDARG
;
1699 case CSIDL_PROGRAM_FILES
:
1700 case CSIDL_PROGRAM_FILESX86
:
1701 IsWow64Process( GetCurrentProcess(), &is_wow64
);
1702 folder
= is_wow64
? CSIDL_PROGRAM_FILESX86
: CSIDL_PROGRAM_FILES
;
1704 case CSIDL_PROGRAM_FILES_COMMON
:
1705 case CSIDL_PROGRAM_FILES_COMMONX86
:
1706 IsWow64Process( GetCurrentProcess(), &is_wow64
);
1707 folder
= is_wow64
? CSIDL_PROGRAM_FILES_COMMONX86
: CSIDL_PROGRAM_FILES_COMMON
;
1712 if (CSIDL_Data
[folder
].szDefaultPath
&&
1713 IS_INTRESOURCE(CSIDL_Data
[folder
].szDefaultPath
))
1715 if (LoadStringW(shell32_hInstance
,
1716 LOWORD(CSIDL_Data
[folder
].szDefaultPath
), resourcePath
, MAX_PATH
))
1719 pDefaultPath
= resourcePath
;
1723 FIXME("(%d,%s), LoadString failed, missing translation?\n", folder
,
1724 debugstr_w(pszPath
));
1731 pDefaultPath
= CSIDL_Data
[folder
].szDefaultPath
;
1735 switch (CSIDL_Data
[folder
].type
)
1737 case CSIDL_Type_User
:
1738 strcpyW(pszPath
, UserProfileW
);
1740 case CSIDL_Type_AllUsers
:
1741 strcpyW(pszPath
, AllUsersProfileW
);
1743 case CSIDL_Type_CurrVer
:
1744 strcpyW(pszPath
, SystemDriveW
);
1747 ; /* no corresponding env. var, do nothing */
1751 PathAddBackslashW(pszPath
);
1752 strcatW(pszPath
, pDefaultPath
);
1755 TRACE("returning 0x%08x\n", hr
);
1759 /* Gets the (unexpanded) value of the folder with index folder into pszPath.
1760 * The folder's type is assumed to be CSIDL_Type_CurrVer. Its default value
1761 * can be overridden in the HKLM\\szCurrentVersion key.
1762 * If dwFlags has SHGFP_TYPE_DEFAULT set or if the value isn't overridden in
1763 * the registry, uses _SHGetDefaultValue to get the value.
1765 static HRESULT
_SHGetCurrentVersionPath(DWORD dwFlags
, BYTE folder
,
1770 TRACE("0x%08x,0x%02x,%p\n", dwFlags
, folder
, pszPath
);
1772 if (folder
>= sizeof(CSIDL_Data
) / sizeof(CSIDL_Data
[0]))
1773 return E_INVALIDARG
;
1774 if (CSIDL_Data
[folder
].type
!= CSIDL_Type_CurrVer
)
1775 return E_INVALIDARG
;
1777 return E_INVALIDARG
;
1779 if (dwFlags
& SHGFP_TYPE_DEFAULT
)
1780 hr
= _SHGetDefaultValue(folder
, pszPath
);
1785 if (RegCreateKeyW(HKEY_LOCAL_MACHINE
, szCurrentVersion
, &hKey
))
1789 DWORD dwType
, dwPathLen
= MAX_PATH
* sizeof(WCHAR
);
1791 if (RegQueryValueExW(hKey
, CSIDL_Data
[folder
].szValueName
, NULL
,
1792 &dwType
, (LPBYTE
)pszPath
, &dwPathLen
) ||
1793 (dwType
!= REG_SZ
&& dwType
!= REG_EXPAND_SZ
))
1795 hr
= _SHGetDefaultValue(folder
, pszPath
);
1796 dwType
= REG_EXPAND_SZ
;
1799 case CSIDL_PROGRAM_FILESX86
:
1800 case CSIDL_PROGRAM_FILES_COMMONX86
:
1801 /* these two should never be set on 32-bit setups */
1805 IsWow64Process( GetCurrentProcess(), &is_wow64
);
1806 if (!is_wow64
) break;
1810 RegSetValueExW(hKey
, CSIDL_Data
[folder
].szValueName
, 0, dwType
,
1811 (LPBYTE
)pszPath
, (strlenW(pszPath
)+1)*sizeof(WCHAR
));
1816 pszPath
[dwPathLen
/ sizeof(WCHAR
)] = '\0';
1822 TRACE("returning 0x%08x (output path is %s)\n", hr
, debugstr_w(pszPath
));
1826 static LPWSTR
_GetUserSidStringFromToken(HANDLE Token
)
1828 char InfoBuffer
[64];
1829 PTOKEN_USER UserInfo
;
1833 UserInfo
= (PTOKEN_USER
) InfoBuffer
;
1834 if (! GetTokenInformation(Token
, TokenUser
, InfoBuffer
, sizeof(InfoBuffer
),
1837 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER
)
1839 UserInfo
= HeapAlloc(GetProcessHeap(), 0, InfoSize
);
1840 if (UserInfo
== NULL
)
1842 if (! GetTokenInformation(Token
, TokenUser
, UserInfo
, InfoSize
,
1845 HeapFree(GetProcessHeap(), 0, UserInfo
);
1850 if (! ConvertSidToStringSidW(UserInfo
->User
.Sid
, &SidStr
))
1853 if (UserInfo
!= (PTOKEN_USER
) InfoBuffer
)
1854 HeapFree(GetProcessHeap(), 0, UserInfo
);
1859 /* Gets the user's path (unexpanded) for the CSIDL with index folder:
1860 * If SHGFP_TYPE_DEFAULT is set, calls _SHGetDefaultValue for it. Otherwise
1861 * calls _SHGetUserShellFolderPath for it. Where it looks depends on hToken:
1862 * - if hToken is -1, looks in HKEY_USERS\.Default
1863 * - otherwise looks first in HKEY_CURRENT_USER, followed by HKEY_LOCAL_MACHINE
1864 * if HKEY_CURRENT_USER doesn't contain any entries. If both fail, finally
1865 * calls _SHGetDefaultValue for it.
1867 static HRESULT
_SHGetUserProfilePath(HANDLE hToken
, DWORD dwFlags
, BYTE folder
,
1870 const WCHAR
*szValueName
;
1874 TRACE("%p,0x%08x,0x%02x,%p\n", hToken
, dwFlags
, folder
, pszPath
);
1876 if (folder
>= sizeof(CSIDL_Data
) / sizeof(CSIDL_Data
[0]))
1877 return E_INVALIDARG
;
1878 if (CSIDL_Data
[folder
].type
!= CSIDL_Type_User
)
1879 return E_INVALIDARG
;
1881 return E_INVALIDARG
;
1883 if (dwFlags
& SHGFP_TYPE_DEFAULT
)
1885 if (hToken
!= NULL
&& hToken
!= (HANDLE
)-1)
1887 FIXME("unsupported for user other than current or default\n");
1890 hr
= _SHGetDefaultValue(folder
, pszPath
);
1894 LPCWSTR userPrefix
= NULL
;
1897 if (hToken
== (HANDLE
)-1)
1899 hRootKey
= HKEY_USERS
;
1900 userPrefix
= DefaultW
;
1902 else if (hToken
== NULL
)
1903 hRootKey
= HKEY_CURRENT_USER
;
1906 hRootKey
= HKEY_USERS
;
1907 userPrefix
= _GetUserSidStringFromToken(hToken
);
1908 if (userPrefix
== NULL
)
1915 /* For CSIDL_Type_User we also use the GUID if no szValueName is provided */
1916 szValueName
= CSIDL_Data
[folder
].szValueName
;
1919 StringFromGUID2( CSIDL_Data
[folder
].id
, buffer
, 39 );
1920 szValueName
= &buffer
[0];
1923 hr
= _SHGetUserShellFolderPath(hRootKey
, userPrefix
, szValueName
, pszPath
);
1924 if (FAILED(hr
) && hRootKey
!= HKEY_LOCAL_MACHINE
)
1925 hr
= _SHGetUserShellFolderPath(HKEY_LOCAL_MACHINE
, NULL
, szValueName
, pszPath
);
1927 hr
= _SHGetDefaultValue(folder
, pszPath
);
1928 if (userPrefix
!= NULL
&& userPrefix
!= DefaultW
)
1929 LocalFree((HLOCAL
) userPrefix
);
1932 TRACE("returning 0x%08x (output path is %s)\n", hr
, debugstr_w(pszPath
));
1936 /* Gets the (unexpanded) path for the CSIDL with index folder. If dwFlags has
1937 * SHGFP_TYPE_DEFAULT set, calls _SHGetDefaultValue. Otherwise calls
1938 * _SHGetUserShellFolderPath for it, looking only in HKEY_LOCAL_MACHINE.
1939 * If this fails, falls back to _SHGetDefaultValue.
1941 static HRESULT
_SHGetAllUsersProfilePath(DWORD dwFlags
, BYTE folder
,
1946 TRACE("0x%08x,0x%02x,%p\n", dwFlags
, folder
, pszPath
);
1948 if (folder
>= sizeof(CSIDL_Data
) / sizeof(CSIDL_Data
[0]))
1949 return E_INVALIDARG
;
1950 if (CSIDL_Data
[folder
].type
!= CSIDL_Type_AllUsers
)
1951 return E_INVALIDARG
;
1953 return E_INVALIDARG
;
1955 if (dwFlags
& SHGFP_TYPE_DEFAULT
)
1956 hr
= _SHGetDefaultValue(folder
, pszPath
);
1959 hr
= _SHGetUserShellFolderPath(HKEY_LOCAL_MACHINE
, NULL
,
1960 CSIDL_Data
[folder
].szValueName
, pszPath
);
1962 hr
= _SHGetDefaultValue(folder
, pszPath
);
1964 TRACE("returning 0x%08x (output path is %s)\n", hr
, debugstr_w(pszPath
));
1968 static HRESULT
_SHOpenProfilesKey(PHKEY pKey
)
1973 lRet
= RegCreateKeyExW(HKEY_LOCAL_MACHINE
, ProfileListW
, 0, NULL
, 0,
1974 KEY_ALL_ACCESS
, NULL
, pKey
, &disp
);
1975 return HRESULT_FROM_WIN32(lRet
);
1978 /* Reads the value named szValueName from the key profilesKey (assumed to be
1979 * opened by _SHOpenProfilesKey) into szValue, which is assumed to be MAX_PATH
1980 * WCHARs in length. If it doesn't exist, returns szDefault (and saves
1981 * szDefault to the registry).
1983 static HRESULT
_SHGetProfilesValue(HKEY profilesKey
, LPCWSTR szValueName
,
1984 LPWSTR szValue
, LPCWSTR szDefault
)
1987 DWORD type
, dwPathLen
= MAX_PATH
* sizeof(WCHAR
);
1990 TRACE("%p,%s,%p,%s\n", profilesKey
, debugstr_w(szValueName
), szValue
,
1991 debugstr_w(szDefault
));
1992 lRet
= RegQueryValueExW(profilesKey
, szValueName
, NULL
, &type
,
1993 (LPBYTE
)szValue
, &dwPathLen
);
1994 if (!lRet
&& (type
== REG_SZ
|| type
== REG_EXPAND_SZ
) && dwPathLen
1997 dwPathLen
/= sizeof(WCHAR
);
1998 szValue
[dwPathLen
] = '\0';
2003 /* Missing or invalid value, set a default */
2004 lstrcpynW(szValue
, szDefault
, MAX_PATH
);
2005 TRACE("Setting missing value %s to %s\n", debugstr_w(szValueName
),
2006 debugstr_w(szValue
));
2007 lRet
= RegSetValueExW(profilesKey
, szValueName
, 0, REG_EXPAND_SZ
,
2009 (strlenW(szValue
) + 1) * sizeof(WCHAR
));
2011 hr
= HRESULT_FROM_WIN32(lRet
);
2015 TRACE("returning 0x%08x (output value is %s)\n", hr
, debugstr_w(szValue
));
2019 /* Attempts to expand environment variables from szSrc into szDest, which is
2020 * assumed to be MAX_PATH characters in length. Before referring to the
2021 * environment, handles a few variables directly, because the environment
2022 * variables may not be set when this is called (as during Wine's installation
2023 * when default values are being written to the registry).
2024 * The directly handled environment variables, and their source, are:
2025 * - ALLUSERSPROFILE, USERPROFILE: reads from the registry
2026 * - SystemDrive: uses GetSystemDirectoryW and uses the drive portion of its
2028 * If one of the directly handled environment variables is expanded, only
2029 * expands a single variable, and only in the beginning of szSrc.
2031 static HRESULT
_SHExpandEnvironmentStrings(LPCWSTR szSrc
, LPWSTR szDest
)
2034 WCHAR szTemp
[MAX_PATH
], szProfilesPrefix
[MAX_PATH
] = { 0 };
2037 TRACE("%s, %p\n", debugstr_w(szSrc
), szDest
);
2039 if (!szSrc
|| !szDest
) return E_INVALIDARG
;
2041 /* short-circuit if there's nothing to expand */
2042 if (szSrc
[0] != '%')
2044 strcpyW(szDest
, szSrc
);
2048 /* Get the profile prefix, we'll probably be needing it */
2049 hr
= _SHOpenProfilesKey(&key
);
2052 WCHAR def_val
[MAX_PATH
];
2054 /* get the system drive */
2055 GetSystemDirectoryW(def_val
, MAX_PATH
);
2056 if (def_val
[1] == ':') strcpyW( def_val
+ 3, szDefaultProfileDirW
);
2057 else FIXME("non-drive system paths unsupported\n");
2059 hr
= _SHGetProfilesValue(key
, ProfilesDirectoryW
, szProfilesPrefix
, def_val
);
2063 strcpyW(szTemp
, szSrc
);
2064 while (SUCCEEDED(hr
) && szTemp
[0] == '%')
2066 if (!strncmpiW(szTemp
, AllUsersProfileW
, strlenW(AllUsersProfileW
)))
2068 WCHAR szAllUsers
[MAX_PATH
];
2070 strcpyW(szDest
, szProfilesPrefix
);
2071 hr
= _SHGetProfilesValue(key
, AllUsersProfileValueW
,
2072 szAllUsers
, AllUsersW
);
2073 PathAppendW(szDest
, szAllUsers
);
2074 PathAppendW(szDest
, szTemp
+ strlenW(AllUsersProfileW
));
2076 else if (!strncmpiW(szTemp
, UserProfileW
, strlenW(UserProfileW
)))
2078 WCHAR userName
[MAX_PATH
];
2079 DWORD userLen
= MAX_PATH
;
2081 strcpyW(szDest
, szProfilesPrefix
);
2082 GetUserNameW(userName
, &userLen
);
2083 PathAppendW(szDest
, userName
);
2084 PathAppendW(szDest
, szTemp
+ strlenW(UserProfileW
));
2086 else if (!strncmpiW(szTemp
, SystemDriveW
, strlenW(SystemDriveW
)))
2088 GetSystemDirectoryW(szDest
, MAX_PATH
);
2089 if (szDest
[1] != ':')
2091 FIXME("non-drive system paths unsupported\n");
2096 strcpyW(szDest
+ 3, szTemp
+ strlenW(SystemDriveW
) + 1);
2102 DWORD ret
= ExpandEnvironmentStringsW(szSrc
, szDest
, MAX_PATH
);
2105 hr
= E_NOT_SUFFICIENT_BUFFER
;
2107 hr
= HRESULT_FROM_WIN32(GetLastError());
2111 if (SUCCEEDED(hr
) && szDest
[0] == '%')
2112 strcpyW(szTemp
, szDest
);
2115 /* terminate loop */
2122 TRACE("returning 0x%08x (input was %s, output is %s)\n", hr
,
2123 debugstr_w(szSrc
), debugstr_w(szDest
));
2127 /*************************************************************************
2128 * SHGetFolderPathW [SHELL32.@]
2130 * Convert nFolder to path.
2134 * Failure: standard HRESULT error codes.
2137 * Most values can be overridden in either
2138 * HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
2139 * or in the same location in HKLM.
2140 * The "Shell Folders" registry key was used in NT4 and earlier systems.
2141 * Beginning with Windows 2000, the "User Shell Folders" key is used, so
2142 * changes made to it are made to the former key too. This synchronization is
2143 * done on-demand: not until someone requests the value of one of these paths
2144 * (by calling one of the SHGet functions) is the value synchronized.
2145 * Furthermore, the HKCU paths take precedence over the HKLM paths.
2147 HRESULT WINAPI
SHGetFolderPathW(
2148 HWND hwndOwner
, /* [I] owner window */
2149 int nFolder
, /* [I] CSIDL identifying the folder */
2150 HANDLE hToken
, /* [I] access token */
2151 DWORD dwFlags
, /* [I] which path to return */
2152 LPWSTR pszPath
) /* [O] converted path */
2154 HRESULT hr
= SHGetFolderPathAndSubDirW(hwndOwner
, nFolder
, hToken
, dwFlags
, NULL
, pszPath
);
2155 if(HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND
) == hr
)
2156 hr
= HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND
);
2160 HRESULT WINAPI
SHGetFolderPathAndSubDirA(
2161 HWND hwndOwner
, /* [I] owner window */
2162 int nFolder
, /* [I] CSIDL identifying the folder */
2163 HANDLE hToken
, /* [I] access token */
2164 DWORD dwFlags
, /* [I] which path to return */
2165 LPCSTR pszSubPath
, /* [I] sub directory of the specified folder */
2166 LPSTR pszPath
) /* [O] converted path */
2170 LPWSTR pszSubPathW
= NULL
;
2171 LPWSTR pszPathW
= NULL
;
2172 TRACE("%08x,%08x,%s\n",nFolder
, dwFlags
, debugstr_w(pszSubPathW
));
2175 pszPathW
= HeapAlloc(GetProcessHeap(), 0, MAX_PATH
* sizeof(WCHAR
));
2177 hr
= HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY
);
2181 TRACE("%08x,%08x,%s\n",nFolder
, dwFlags
, debugstr_w(pszSubPathW
));
2183 /* SHGetFolderPathAndSubDirW does not distinguish if pszSubPath isn't
2184 * set (null), or an empty string.therefore call it without the parameter set
2185 * if pszSubPath is an empty string
2187 if (pszSubPath
&& pszSubPath
[0]) {
2188 length
= MultiByteToWideChar(CP_ACP
, 0, pszSubPath
, -1, NULL
, 0);
2189 pszSubPathW
= HeapAlloc(GetProcessHeap(), 0, length
* sizeof(WCHAR
));
2191 hr
= HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY
);
2194 MultiByteToWideChar(CP_ACP
, 0, pszSubPath
, -1, pszSubPathW
, length
);
2197 hr
= SHGetFolderPathAndSubDirW(hwndOwner
, nFolder
, hToken
, dwFlags
, pszSubPathW
, pszPathW
);
2199 if (SUCCEEDED(hr
) && pszPath
)
2200 WideCharToMultiByte(CP_ACP
, 0, pszPathW
, -1, pszPath
, MAX_PATH
, NULL
, NULL
);
2203 HeapFree(GetProcessHeap(), 0, pszPathW
);
2204 HeapFree(GetProcessHeap(), 0, pszSubPathW
);
2208 /*************************************************************************
2209 * SHGetFolderPathAndSubDirW [SHELL32.@]
2211 HRESULT WINAPI
SHGetFolderPathAndSubDirW(
2212 HWND hwndOwner
, /* [I] owner window */
2213 int nFolder
, /* [I] CSIDL identifying the folder */
2214 HANDLE hToken
, /* [I] access token */
2215 DWORD dwFlags
, /* [I] which path to return */
2216 LPCWSTR pszSubPath
,/* [I] sub directory of the specified folder */
2217 LPWSTR pszPath
) /* [O] converted path */
2220 WCHAR szBuildPath
[MAX_PATH
], szTemp
[MAX_PATH
];
2221 DWORD folder
= nFolder
& CSIDL_FOLDER_MASK
;
2225 TRACE("%p,%p,nFolder=0x%04x,%s\n", hwndOwner
,pszPath
,nFolder
,debugstr_w(pszSubPath
));
2227 /* Windows always NULL-terminates the resulting path regardless of success
2228 * or failure, so do so first
2233 if (folder
>= sizeof(CSIDL_Data
) / sizeof(CSIDL_Data
[0]))
2234 return E_INVALIDARG
;
2235 if ((SHGFP_TYPE_CURRENT
!= dwFlags
) && (SHGFP_TYPE_DEFAULT
!= dwFlags
))
2236 return E_INVALIDARG
;
2238 type
= CSIDL_Data
[folder
].type
;
2241 case CSIDL_Type_Disallowed
:
2244 case CSIDL_Type_NonExistent
:
2247 case CSIDL_Type_WindowsPath
:
2248 GetWindowsDirectoryW(szTemp
, MAX_PATH
);
2249 if (CSIDL_Data
[folder
].szDefaultPath
&&
2250 !IS_INTRESOURCE(CSIDL_Data
[folder
].szDefaultPath
) &&
2251 *CSIDL_Data
[folder
].szDefaultPath
)
2253 PathAddBackslashW(szTemp
);
2254 strcatW(szTemp
, CSIDL_Data
[folder
].szDefaultPath
);
2258 case CSIDL_Type_SystemPath
:
2259 GetSystemDirectoryW(szTemp
, MAX_PATH
);
2260 if (CSIDL_Data
[folder
].szDefaultPath
&&
2261 !IS_INTRESOURCE(CSIDL_Data
[folder
].szDefaultPath
) &&
2262 *CSIDL_Data
[folder
].szDefaultPath
)
2264 PathAddBackslashW(szTemp
);
2265 strcatW(szTemp
, CSIDL_Data
[folder
].szDefaultPath
);
2269 case CSIDL_Type_SystemX86Path
:
2270 if (!GetSystemWow64DirectoryW(szTemp
, MAX_PATH
)) GetSystemDirectoryW(szTemp
, MAX_PATH
);
2271 if (CSIDL_Data
[folder
].szDefaultPath
&&
2272 !IS_INTRESOURCE(CSIDL_Data
[folder
].szDefaultPath
) &&
2273 *CSIDL_Data
[folder
].szDefaultPath
)
2275 PathAddBackslashW(szTemp
);
2276 strcatW(szTemp
, CSIDL_Data
[folder
].szDefaultPath
);
2280 case CSIDL_Type_CurrVer
:
2281 hr
= _SHGetCurrentVersionPath(dwFlags
, folder
, szTemp
);
2283 case CSIDL_Type_User
:
2284 hr
= _SHGetUserProfilePath(hToken
, dwFlags
, folder
, szTemp
);
2286 case CSIDL_Type_AllUsers
:
2287 hr
= _SHGetAllUsersProfilePath(dwFlags
, folder
, szTemp
);
2290 FIXME("bogus type %d, please fix\n", type
);
2295 /* Expand environment strings if necessary */
2297 hr
= _SHExpandEnvironmentStrings(szTemp
, szBuildPath
);
2299 strcpyW(szBuildPath
, szTemp
);
2301 if (FAILED(hr
)) goto end
;
2304 /* make sure the new path does not exceed the buffer length
2305 * and remember to backslash and terminate it */
2306 if(MAX_PATH
< (lstrlenW(szBuildPath
) + lstrlenW(pszSubPath
) + 2)) {
2307 hr
= HRESULT_FROM_WIN32(ERROR_FILENAME_EXCED_RANGE
);
2310 PathAppendW(szBuildPath
, pszSubPath
);
2311 PathRemoveBackslashW(szBuildPath
);
2313 /* Copy the path if it's available before we might return */
2314 if (SUCCEEDED(hr
) && pszPath
)
2315 strcpyW(pszPath
, szBuildPath
);
2317 /* if we don't care about existing directories we are ready */
2318 if(nFolder
& CSIDL_FLAG_DONT_VERIFY
) goto end
;
2320 if (PathFileExistsW(szBuildPath
)) goto end
;
2322 /* not existing but we are not allowed to create it. The return value
2323 * is verified against shell32 version 6.0.
2325 if (!(nFolder
& CSIDL_FLAG_CREATE
))
2327 hr
= HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND
);
2331 /* create directory/directories */
2332 ret
= SHCreateDirectoryExW(hwndOwner
, szBuildPath
, NULL
);
2333 if (ret
&& ret
!= ERROR_ALREADY_EXISTS
)
2335 ERR("Failed to create directory %s.\n", debugstr_w(szBuildPath
));
2340 TRACE("Created missing system directory %s\n", debugstr_w(szBuildPath
));
2342 TRACE("returning 0x%08x (final path is %s)\n", hr
, debugstr_w(szBuildPath
));
2346 /*************************************************************************
2347 * SHGetFolderPathA [SHELL32.@]
2349 * See SHGetFolderPathW.
2351 HRESULT WINAPI
SHGetFolderPathA(
2358 WCHAR szTemp
[MAX_PATH
];
2361 TRACE("%p,%p,nFolder=0x%04x\n",hwndOwner
,pszPath
,nFolder
);
2365 hr
= SHGetFolderPathW(hwndOwner
, nFolder
, hToken
, dwFlags
, szTemp
);
2366 if (SUCCEEDED(hr
) && pszPath
)
2367 WideCharToMultiByte(CP_ACP
, 0, szTemp
, -1, pszPath
, MAX_PATH
, NULL
,
2373 /* For each folder in folders, if its value has not been set in the registry,
2374 * calls _SHGetUserProfilePath or _SHGetAllUsersProfilePath (depending on the
2375 * folder's type) to get the unexpanded value first.
2376 * Writes the unexpanded value to User Shell Folders, and queries it with
2377 * SHGetFolderPathW to force the creation of the directory if it doesn't
2378 * already exist. SHGetFolderPathW also returns the expanded value, which
2379 * this then writes to Shell Folders.
2381 static HRESULT
_SHRegisterFolders(HKEY hRootKey
, HANDLE hToken
,
2382 LPCWSTR szUserShellFolderPath
, LPCWSTR szShellFolderPath
, const UINT folders
[],
2385 const WCHAR
*szValueName
;
2388 WCHAR path
[MAX_PATH
];
2390 HKEY hUserKey
= NULL
, hKey
= NULL
;
2391 DWORD dwType
, dwPathLen
;
2394 TRACE("%p,%p,%s,%p,%u\n", hRootKey
, hToken
,
2395 debugstr_w(szUserShellFolderPath
), folders
, foldersLen
);
2397 ret
= RegCreateKeyW(hRootKey
, szUserShellFolderPath
, &hUserKey
);
2399 hr
= HRESULT_FROM_WIN32(ret
);
2402 ret
= RegCreateKeyW(hRootKey
, szShellFolderPath
, &hKey
);
2404 hr
= HRESULT_FROM_WIN32(ret
);
2406 for (i
= 0; SUCCEEDED(hr
) && i
< foldersLen
; i
++)
2408 dwPathLen
= MAX_PATH
* sizeof(WCHAR
);
2410 /* For CSIDL_Type_User we also use the GUID if no szValueName is provided */
2411 szValueName
= CSIDL_Data
[folders
[i
]].szValueName
;
2412 if (!szValueName
&& CSIDL_Data
[folders
[i
]].type
== CSIDL_Type_User
)
2414 StringFromGUID2( CSIDL_Data
[folders
[i
]].id
, buffer
, 39 );
2415 szValueName
= &buffer
[0];
2418 if (RegQueryValueExW(hUserKey
, szValueName
, NULL
,
2419 &dwType
, (LPBYTE
)path
, &dwPathLen
) || (dwType
!= REG_SZ
&&
2420 dwType
!= REG_EXPAND_SZ
))
2423 if (CSIDL_Data
[folders
[i
]].type
== CSIDL_Type_User
)
2424 _SHGetUserProfilePath(hToken
, SHGFP_TYPE_DEFAULT
, folders
[i
],
2426 else if (CSIDL_Data
[folders
[i
]].type
== CSIDL_Type_AllUsers
)
2427 _SHGetAllUsersProfilePath(SHGFP_TYPE_DEFAULT
, folders
[i
], path
);
2428 else if (CSIDL_Data
[folders
[i
]].type
== CSIDL_Type_WindowsPath
)
2430 GetWindowsDirectoryW(path
, MAX_PATH
);
2431 if (CSIDL_Data
[folders
[i
]].szDefaultPath
&&
2432 !IS_INTRESOURCE(CSIDL_Data
[folders
[i
]].szDefaultPath
))
2434 PathAddBackslashW(path
);
2435 strcatW(path
, CSIDL_Data
[folders
[i
]].szDefaultPath
);
2442 ret
= RegSetValueExW(hUserKey
, szValueName
, 0, REG_EXPAND_SZ
,
2443 (LPBYTE
)path
, (strlenW(path
) + 1) * sizeof(WCHAR
));
2445 hr
= HRESULT_FROM_WIN32(ret
);
2448 hr
= SHGetFolderPathW(NULL
, folders
[i
] | CSIDL_FLAG_CREATE
,
2449 hToken
, SHGFP_TYPE_DEFAULT
, path
);
2450 ret
= RegSetValueExW(hKey
, szValueName
, 0, REG_SZ
,
2451 (LPBYTE
)path
, (strlenW(path
) + 1) * sizeof(WCHAR
));
2453 hr
= HRESULT_FROM_WIN32(ret
);
2459 RegCloseKey(hUserKey
);
2463 TRACE("returning 0x%08x\n", hr
);
2467 static HRESULT
_SHRegisterUserShellFolders(BOOL bDefault
)
2469 static const UINT folders
[] = {
2480 CSIDL_DESKTOPDIRECTORY
,
2484 CSIDL_LOCAL_APPDATA
,
2485 CSIDL_INTERNET_CACHE
,
2494 CSIDL_APPDATA_LOCALLOW
,
2498 WCHAR userShellFolderPath
[MAX_PATH
], shellFolderPath
[MAX_PATH
];
2499 LPCWSTR pUserShellFolderPath
, pShellFolderPath
;
2504 TRACE("%s\n", bDefault
? "TRUE" : "FALSE");
2507 hToken
= (HANDLE
)-1;
2508 hRootKey
= HKEY_USERS
;
2509 strcpyW(userShellFolderPath
, DefaultW
);
2510 PathAddBackslashW(userShellFolderPath
);
2511 strcatW(userShellFolderPath
, szSHUserFolders
);
2512 pUserShellFolderPath
= userShellFolderPath
;
2513 strcpyW(shellFolderPath
, DefaultW
);
2514 PathAddBackslashW(shellFolderPath
);
2515 strcatW(shellFolderPath
, szSHFolders
);
2516 pShellFolderPath
= shellFolderPath
;
2521 hRootKey
= HKEY_CURRENT_USER
;
2522 pUserShellFolderPath
= szSHUserFolders
;
2523 pShellFolderPath
= szSHFolders
;
2526 hr
= _SHRegisterFolders(hRootKey
, hToken
, pUserShellFolderPath
,
2527 pShellFolderPath
, folders
, sizeof(folders
) / sizeof(folders
[0]));
2528 TRACE("returning 0x%08x\n", hr
);
2532 static HRESULT
_SHRegisterCommonShellFolders(void)
2534 static const UINT folders
[] = {
2535 CSIDL_COMMON_STARTMENU
,
2536 CSIDL_COMMON_PROGRAMS
,
2537 CSIDL_COMMON_STARTUP
,
2538 CSIDL_COMMON_DESKTOPDIRECTORY
,
2539 CSIDL_COMMON_FAVORITES
,
2540 CSIDL_COMMON_APPDATA
,
2541 CSIDL_COMMON_TEMPLATES
,
2542 CSIDL_COMMON_DOCUMENTS
,
2543 CSIDL_COMMON_ADMINTOOLS
,
2545 CSIDL_COMMON_PICTURES
,
2551 hr
= _SHRegisterFolders(HKEY_LOCAL_MACHINE
, NULL
, szSHUserFolders
,
2552 szSHFolders
, folders
, sizeof(folders
) / sizeof(folders
[0]));
2553 TRACE("returning 0x%08x\n", hr
);
2557 /******************************************************************************
2558 * _SHAppendToUnixPath [Internal]
2560 * Helper function for _SHCreateSymbolicLinks. Appends pwszSubPath (or the
2561 * corresponding resource, if IS_INTRESOURCE) to the unix base path 'szBasePath'
2562 * and replaces backslashes with slashes.
2565 * szBasePath [IO] The unix base path, which will be appended to (CP_UNXICP).
2566 * pwszSubPath [I] Sub-path or resource id (use MAKEINTRESOURCEW).
2572 static inline BOOL
_SHAppendToUnixPath(char *szBasePath
, LPCWSTR pwszSubPath
) {
2573 WCHAR wszSubPath
[MAX_PATH
];
2574 int cLen
= strlen(szBasePath
);
2577 if (IS_INTRESOURCE(pwszSubPath
)) {
2578 if (!LoadStringW(shell32_hInstance
, LOWORD(pwszSubPath
), wszSubPath
, MAX_PATH
)) {
2579 /* Fall back to hard coded defaults. */
2580 switch (LOWORD(pwszSubPath
)) {
2582 lstrcpyW(wszSubPath
, DocumentsW
);
2585 lstrcpyW(wszSubPath
, My_MusicW
);
2587 case IDS_MYPICTURES
:
2588 lstrcpyW(wszSubPath
, My_PicturesW
);
2591 lstrcpyW(wszSubPath
, My_VideosW
);
2594 ERR("LoadString(%d) failed!\n", LOWORD(pwszSubPath
));
2599 lstrcpyW(wszSubPath
, pwszSubPath
);
2602 if (szBasePath
[cLen
-1] != '/') szBasePath
[cLen
++] = '/';
2604 if (!WideCharToMultiByte(CP_UNIXCP
, 0, wszSubPath
, -1, szBasePath
+ cLen
,
2605 FILENAME_MAX
- cLen
, NULL
, NULL
))
2610 pBackslash
= szBasePath
+ cLen
;
2611 while ((pBackslash
= strchr(pBackslash
, '\\'))) *pBackslash
= '/';
2616 /******************************************************************************
2617 * _SHCreateSymbolicLinks [Internal]
2619 * Sets up symbol links for various shell folders to point into the users home
2620 * directory. We do an educated guess about what the user would probably want:
2621 * - If there is a 'My Documents' directory in $HOME, the user probably wants
2622 * wine's 'My Documents' to point there. Furthermore, we imply that the user
2623 * is a Windows lover and has no problem with wine creating 'My Pictures',
2624 * 'My Music' and 'My Videos' subfolders under '$HOME/My Documents', if those
2625 * do not already exits. We put appropriate symbolic links in place for those,
2627 * - If there is no 'My Documents' directory in $HOME, we let 'My Documents'
2628 * point directly to $HOME. We assume the user to be a unix hacker who does not
2629 * want wine to create anything anywhere besides the .wine directory. So, if
2630 * there already is a 'My Music' directory in $HOME, we symlink the 'My Music'
2631 * shell folder to it. But if not, then we check XDG_MUSIC_DIR - "well known"
2632 * directory, and try to link to that. If that fails, then we symlink to
2633 * $HOME directly. The same holds fo 'My Pictures' and 'My Videos'.
2634 * - The Desktop shell folder is symlinked to XDG_DESKTOP_DIR. If that does not
2635 * exist, then we try '$HOME/Desktop'. If that does not exist, then we leave
2637 * ('My Music',... above in fact means LoadString(IDS_MYMUSIC))
2639 static void _SHCreateSymbolicLinks(void)
2641 UINT aidsMyStuff
[] = { IDS_MYPICTURES
, IDS_MYVIDEOS
, IDS_MYMUSIC
}, i
;
2642 int acsidlMyStuff
[] = { CSIDL_MYPICTURES
, CSIDL_MYVIDEO
, CSIDL_MYMUSIC
};
2643 static const char * const xdg_dirs
[] = { "PICTURES", "VIDEOS", "MUSIC", "DESKTOP" };
2644 static const unsigned int num
= sizeof(xdg_dirs
) / sizeof(xdg_dirs
[0]);
2645 WCHAR wszTempPath
[MAX_PATH
];
2646 char szPersonalTarget
[FILENAME_MAX
], *pszPersonal
;
2647 char szMyStuffTarget
[FILENAME_MAX
], *pszMyStuff
;
2648 char szDesktopTarget
[FILENAME_MAX
], *pszDesktop
;
2649 struct stat statFolder
;
2650 const char *pszHome
;
2652 char ** xdg_results
;
2653 char * xdg_desktop_dir
;
2655 /* Create all necessary profile sub-dirs up to 'My Documents' and get the unix path. */
2656 hr
= SHGetFolderPathW(NULL
, CSIDL_PERSONAL
|CSIDL_FLAG_CREATE
, NULL
,
2657 SHGFP_TYPE_DEFAULT
, wszTempPath
);
2658 if (FAILED(hr
)) return;
2659 pszPersonal
= wine_get_unix_file_name(wszTempPath
);
2660 if (!pszPersonal
) return;
2662 hr
= XDG_UserDirLookup(xdg_dirs
, num
, &xdg_results
);
2663 if (FAILED(hr
)) xdg_results
= NULL
;
2665 pszHome
= getenv("HOME");
2666 if (pszHome
&& !stat(pszHome
, &statFolder
) && S_ISDIR(statFolder
.st_mode
)) {
2667 strcpy(szPersonalTarget
, pszHome
);
2668 if (_SHAppendToUnixPath(szPersonalTarget
, MAKEINTRESOURCEW(IDS_PERSONAL
)) &&
2669 !stat(szPersonalTarget
, &statFolder
) && S_ISDIR(statFolder
.st_mode
))
2671 /* '$HOME/My Documents' exists. Create 'My Pictures', 'My Videos' and
2672 * 'My Music' subfolders or fail silently if they already exist. */
2673 for (i
= 0; i
< sizeof(aidsMyStuff
)/sizeof(aidsMyStuff
[0]); i
++) {
2674 strcpy(szMyStuffTarget
, szPersonalTarget
);
2675 if (_SHAppendToUnixPath(szMyStuffTarget
, MAKEINTRESOURCEW(aidsMyStuff
[i
])))
2676 mkdir(szMyStuffTarget
, 0777);
2681 /* '$HOME/My Documents' doesn't exists, but '$HOME' does. */
2682 strcpy(szPersonalTarget
, pszHome
);
2685 /* Replace 'My Documents' directory with a symlink of fail silently if not empty. */
2687 symlink(szPersonalTarget
, pszPersonal
);
2691 /* '$HOME' doesn't exist. Create 'My Pictures', 'My Videos' and 'My Music' subdirs
2692 * in '%USERPROFILE%\\My Documents' or fail silently if they already exist. */
2694 strcpy(szPersonalTarget
, pszPersonal
);
2695 for (i
= 0; i
< sizeof(aidsMyStuff
)/sizeof(aidsMyStuff
[0]); i
++) {
2696 strcpy(szMyStuffTarget
, szPersonalTarget
);
2697 if (_SHAppendToUnixPath(szMyStuffTarget
, MAKEINTRESOURCEW(aidsMyStuff
[i
])))
2698 mkdir(szMyStuffTarget
, 0777);
2702 /* Create symbolic links for 'My Pictures', 'My Videos' and 'My Music'. */
2703 for (i
=0; i
< sizeof(aidsMyStuff
)/sizeof(aidsMyStuff
[0]); i
++) {
2704 /* Create the current 'My Whatever' folder and get its unix path. */
2705 hr
= SHGetFolderPathW(NULL
, acsidlMyStuff
[i
]|CSIDL_FLAG_CREATE
, NULL
,
2706 SHGFP_TYPE_DEFAULT
, wszTempPath
);
2707 if (FAILED(hr
)) continue;
2708 pszMyStuff
= wine_get_unix_file_name(wszTempPath
);
2709 if (!pszMyStuff
) continue;
2711 strcpy(szMyStuffTarget
, szPersonalTarget
);
2712 if (_SHAppendToUnixPath(szMyStuffTarget
, MAKEINTRESOURCEW(aidsMyStuff
[i
])) &&
2713 !stat(szMyStuffTarget
, &statFolder
) && S_ISDIR(statFolder
.st_mode
))
2715 /* If there's a 'My Whatever' directory where 'My Documents' links to, link to it. */
2717 symlink(szMyStuffTarget
, pszMyStuff
);
2722 if (xdg_results
&& xdg_results
[i
])
2724 /* the folder specified by XDG_XXX_DIR exists, link to it. */
2725 symlink(xdg_results
[i
], pszMyStuff
);
2729 /* Else link to where 'My Documents' itself links to. */
2730 symlink(szPersonalTarget
, pszMyStuff
);
2733 HeapFree(GetProcessHeap(), 0, pszMyStuff
);
2736 /* Last but not least, the Desktop folder */
2738 strcpy(szDesktopTarget
, pszHome
);
2740 strcpy(szDesktopTarget
, pszPersonal
);
2741 HeapFree(GetProcessHeap(), 0, pszPersonal
);
2743 xdg_desktop_dir
= xdg_results
? xdg_results
[num
- 1] : NULL
;
2744 if (xdg_desktop_dir
||
2745 (_SHAppendToUnixPath(szDesktopTarget
, DesktopW
) &&
2746 !stat(szDesktopTarget
, &statFolder
) && S_ISDIR(statFolder
.st_mode
)))
2748 hr
= SHGetFolderPathW(NULL
, CSIDL_DESKTOPDIRECTORY
|CSIDL_FLAG_CREATE
, NULL
,
2749 SHGFP_TYPE_DEFAULT
, wszTempPath
);
2750 if (SUCCEEDED(hr
) && (pszDesktop
= wine_get_unix_file_name(wszTempPath
)))
2753 if (xdg_desktop_dir
)
2754 symlink(xdg_desktop_dir
, pszDesktop
);
2756 symlink(szDesktopTarget
, pszDesktop
);
2757 HeapFree(GetProcessHeap(), 0, pszDesktop
);
2761 /* Free resources allocated by XDG_UserDirLookup() */
2764 for (i
= 0; i
< num
; i
++)
2765 HeapFree(GetProcessHeap(), 0, xdg_results
[i
]);
2766 HeapFree(GetProcessHeap(), 0, xdg_results
);
2770 /******************************************************************************
2771 * create_extra_folders [Internal]
2773 * Create some extra folders that don't have a standard CSIDL definition.
2775 static HRESULT
create_extra_folders(void)
2777 static const WCHAR environW
[] = {'E','n','v','i','r','o','n','m','e','n','t',0};
2778 static const WCHAR microsoftW
[] = {'M','i','c','r','o','s','o','f','t',0};
2779 static const WCHAR TempW
[] = {'T','e','m','p',0};
2780 static const WCHAR TEMPW
[] = {'T','E','M','P',0};
2781 static const WCHAR TMPW
[] = {'T','M','P',0};
2782 WCHAR path
[MAX_PATH
+5];
2785 DWORD type
, size
, ret
;
2787 ret
= RegCreateKeyW( HKEY_CURRENT_USER
, environW
, &hkey
);
2788 if (ret
) return HRESULT_FROM_WIN32( ret
);
2790 /* FIXME: should be under AppData, but we don't want spaces in the temp path */
2791 hr
= SHGetFolderPathAndSubDirW( 0, CSIDL_PROFILE
| CSIDL_FLAG_CREATE
, NULL
,
2792 SHGFP_TYPE_DEFAULT
, TempW
, path
);
2795 size
= sizeof(path
);
2796 if (RegQueryValueExW( hkey
, TEMPW
, NULL
, &type
, (LPBYTE
)path
, &size
))
2797 RegSetValueExW( hkey
, TEMPW
, 0, REG_SZ
, (LPBYTE
)path
, (strlenW(path
) + 1) * sizeof(WCHAR
) );
2798 size
= sizeof(path
);
2799 if (RegQueryValueExW( hkey
, TMPW
, NULL
, &type
, (LPBYTE
)path
, &size
))
2800 RegSetValueExW( hkey
, TMPW
, 0, REG_SZ
, (LPBYTE
)path
, (strlenW(path
) + 1) * sizeof(WCHAR
) );
2802 RegCloseKey( hkey
);
2806 hr
= SHGetFolderPathAndSubDirW( 0, CSIDL_COMMON_APPDATA
| CSIDL_FLAG_CREATE
, NULL
,
2807 SHGFP_TYPE_DEFAULT
, microsoftW
, path
);
2813 /******************************************************************************
2814 * set_folder_attributes
2816 * Set the various folder attributes registry keys.
2818 static HRESULT
set_folder_attributes(void)
2820 static const WCHAR clsidW
[] = {'C','L','S','I','D','\\',0 };
2821 static const WCHAR shellfolderW
[] = {'\\','S','h','e','l','l','F','o','l','d','e','r', 0 };
2822 static const WCHAR wfparsingW
[] = {'W','a','n','t','s','F','O','R','P','A','R','S','I','N','G',0};
2823 static const WCHAR wfdisplayW
[] = {'W','a','n','t','s','F','O','R','D','I','S','P','L','A','Y',0};
2824 static const WCHAR hideasdeleteW
[] = {'H','i','d','e','A','s','D','e','l','e','t','e','P','e','r','U','s','e','r',0};
2825 static const WCHAR cfattributesW
[] = {'C','a','l','l','F','o','r','A','t','t','r','i','b','u','t','e','s',0};
2826 static const WCHAR emptyW
[] = {0};
2835 DWORD call_for_attr
;
2838 { &CLSID_UnixFolder
, TRUE
, FALSE
, FALSE
},
2839 { &CLSID_UnixDosFolder
, TRUE
, FALSE
, FALSE
,
2840 SFGAO_FILESYSANCESTOR
|SFGAO_FOLDER
|SFGAO_HASSUBFOLDER
, SFGAO_FILESYSTEM
},
2841 { &CLSID_FolderShortcut
, FALSE
, FALSE
, FALSE
,
2842 SFGAO_FILESYSTEM
|SFGAO_FOLDER
|SFGAO_LINK
,
2843 SFGAO_HASSUBFOLDER
|SFGAO_FILESYSTEM
|SFGAO_FOLDER
|SFGAO_FILESYSANCESTOR
},
2844 { &CLSID_MyDocuments
, TRUE
, FALSE
, FALSE
,
2845 SFGAO_FILESYSANCESTOR
|SFGAO_FOLDER
|SFGAO_HASSUBFOLDER
, SFGAO_FILESYSTEM
},
2846 { &CLSID_RecycleBin
, FALSE
, FALSE
, FALSE
,
2847 SFGAO_FOLDER
|SFGAO_DROPTARGET
|SFGAO_HASPROPSHEET
},
2848 { &CLSID_ControlPanel
, FALSE
, TRUE
, TRUE
,
2849 SFGAO_FOLDER
|SFGAO_HASSUBFOLDER
}
2853 WCHAR buffer
[39 + (sizeof(clsidW
) + sizeof(shellfolderW
)) / sizeof(WCHAR
)];
2857 for (i
= 0; i
< sizeof(folders
)/sizeof(folders
[0]); i
++)
2859 strcpyW( buffer
, clsidW
);
2860 StringFromGUID2( folders
[i
].clsid
, buffer
+ strlenW(buffer
), 39 );
2861 strcatW( buffer
, shellfolderW
);
2862 res
= RegCreateKeyExW( HKEY_CLASSES_ROOT
, buffer
, 0, NULL
, 0,
2863 KEY_READ
| KEY_WRITE
, NULL
, &hkey
, NULL
);
2864 if (res
) return HRESULT_FROM_WIN32( res
);
2865 if (folders
[i
].wfparsing
)
2866 res
= RegSetValueExW( hkey
, wfparsingW
, 0, REG_SZ
, (const BYTE
*)emptyW
, sizeof(emptyW
) );
2867 if (folders
[i
].wfdisplay
)
2868 res
= RegSetValueExW( hkey
, wfdisplayW
, 0, REG_SZ
, (const BYTE
*)emptyW
, sizeof(emptyW
) );
2869 if (folders
[i
].hideasdel
)
2870 res
= RegSetValueExW( hkey
, hideasdeleteW
, 0, REG_SZ
, (const BYTE
*)emptyW
, sizeof(emptyW
) );
2871 if (folders
[i
].attr
)
2872 res
= RegSetValueExW( hkey
, szAttributes
, 0, REG_DWORD
,
2873 (const BYTE
*)&folders
[i
].attr
, sizeof(DWORD
));
2874 if (folders
[i
].call_for_attr
)
2875 res
= RegSetValueExW( hkey
, cfattributesW
, 0, REG_DWORD
,
2876 (const BYTE
*)&folders
[i
].call_for_attr
, sizeof(DWORD
));
2877 RegCloseKey( hkey
);
2883 /* Register the default values in the registry, as some apps seem to depend
2884 * on their presence. The set registered was taken from Windows XP.
2886 HRESULT
SHELL_RegisterShellFolders(void)
2890 /* Set up '$HOME' targeted symlinks for 'My Documents', 'My Pictures',
2891 * 'My Videos', 'My Music' and 'Desktop' in advance, so that the
2892 * _SHRegister*ShellFolders() functions will find everything nice and clean
2893 * and thus will not attempt to create them in the profile directory. */
2894 _SHCreateSymbolicLinks();
2896 hr
= _SHRegisterUserShellFolders(TRUE
);
2898 hr
= _SHRegisterUserShellFolders(FALSE
);
2900 hr
= _SHRegisterCommonShellFolders();
2902 hr
= create_extra_folders();
2904 hr
= set_folder_attributes();
2908 /*************************************************************************
2909 * SHGetSpecialFolderPathA [SHELL32.@]
2911 BOOL WINAPI
SHGetSpecialFolderPathA (
2917 return SHGetFolderPathA(hwndOwner
, nFolder
+ (bCreate
? CSIDL_FLAG_CREATE
: 0), NULL
, 0,
2921 /*************************************************************************
2922 * SHGetSpecialFolderPathW
2924 BOOL WINAPI
SHGetSpecialFolderPathW (
2930 return SHGetFolderPathW(hwndOwner
, nFolder
+ (bCreate
? CSIDL_FLAG_CREATE
: 0), NULL
, 0,
2934 /*************************************************************************
2935 * SHGetSpecialFolderPath (SHELL32.175)
2937 BOOL WINAPI
SHGetSpecialFolderPathAW (
2944 if (SHELL_OsIsUnicode())
2945 return SHGetSpecialFolderPathW (hwndOwner
, szPath
, nFolder
, bCreate
);
2946 return SHGetSpecialFolderPathA (hwndOwner
, szPath
, nFolder
, bCreate
);
2949 /*************************************************************************
2950 * SHGetFolderLocation [SHELL32.@]
2952 * Gets the folder locations from the registry and creates a pidl.
2956 * nFolder [I] CSIDL_xxxxx
2957 * hToken [I] token representing user, or NULL for current user, or -1 for
2959 * dwReserved [I] must be zero
2960 * ppidl [O] PIDL of a special folder
2964 * Failure: Standard OLE-defined error result, S_FALSE or E_INVALIDARG
2967 * Creates missing reg keys and directories.
2968 * Mostly forwards to SHGetFolderPathW, but a few values of nFolder return
2969 * virtual folders that are handled here.
2971 HRESULT WINAPI
SHGetFolderLocation(
2976 LPITEMIDLIST
*ppidl
)
2978 HRESULT hr
= E_INVALIDARG
;
2980 TRACE("%p 0x%08x %p 0x%08x %p\n",
2981 hwndOwner
, nFolder
, hToken
, dwReserved
, ppidl
);
2984 return E_INVALIDARG
;
2986 return E_INVALIDARG
;
2988 /* The virtual folders' locations are not user-dependent */
2990 switch (nFolder
& CSIDL_FOLDER_MASK
)
2993 *ppidl
= _ILCreateDesktop();
2996 case CSIDL_PERSONAL
:
2997 *ppidl
= _ILCreateMyDocuments();
3000 case CSIDL_INTERNET
:
3001 *ppidl
= _ILCreateIExplore();
3004 case CSIDL_CONTROLS
:
3005 *ppidl
= _ILCreateControlPanel();
3008 case CSIDL_PRINTERS
:
3009 *ppidl
= _ILCreatePrinters();
3012 case CSIDL_BITBUCKET
:
3013 *ppidl
= _ILCreateBitBucket();
3017 *ppidl
= _ILCreateMyComputer();
3021 *ppidl
= _ILCreateNetwork();
3026 WCHAR szPath
[MAX_PATH
];
3028 hr
= SHGetFolderPathW(hwndOwner
, nFolder
, hToken
,
3029 SHGFP_TYPE_CURRENT
, szPath
);
3034 TRACE("Value=%s\n", debugstr_w(szPath
));
3035 hr
= SHILCreateFromPathW(szPath
, ppidl
, &attributes
);
3037 else if (hr
== HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND
))
3039 /* unlike SHGetFolderPath, SHGetFolderLocation in shell32
3040 * version 6.0 returns E_FAIL for nonexistent paths
3049 TRACE("-- (new pidl %p)\n",*ppidl
);
3053 /*************************************************************************
3054 * SHGetSpecialFolderLocation [SHELL32.@]
3057 * In NT5, SHGetSpecialFolderLocation needs the <winntdir>/Recent
3060 HRESULT WINAPI
SHGetSpecialFolderLocation(
3063 LPITEMIDLIST
* ppidl
)
3065 HRESULT hr
= E_INVALIDARG
;
3067 TRACE("(%p,0x%x,%p)\n", hwndOwner
,nFolder
,ppidl
);
3070 return E_INVALIDARG
;
3072 hr
= SHGetFolderLocation(hwndOwner
, nFolder
, NULL
, 0, ppidl
);
3076 static int csidl_from_id( const KNOWNFOLDERID
*id
)
3079 for (i
= 0; i
< sizeof(CSIDL_Data
) / sizeof(CSIDL_Data
[0]); i
++)
3080 if (IsEqualGUID( CSIDL_Data
[i
].id
, id
)) return i
;
3084 /*************************************************************************
3085 * SHGetKnownFolderPath [SHELL32.@]
3087 HRESULT WINAPI
SHGetKnownFolderPath(REFKNOWNFOLDERID rfid
, DWORD flags
, HANDLE token
, PWSTR
*path
)
3090 WCHAR folder
[MAX_PATH
];
3091 int index
= csidl_from_id( rfid
);
3093 TRACE("%s, 0x%08x, %p, %p\n", debugstr_guid(rfid
), flags
, token
, path
);
3098 return HRESULT_FROM_WIN32( ERROR_FILE_NOT_FOUND
);
3100 if (flags
& KF_FLAG_CREATE
)
3101 index
|= CSIDL_FLAG_CREATE
;
3103 if (flags
& KF_FLAG_DONT_VERIFY
)
3104 index
|= CSIDL_FLAG_DONT_VERIFY
;
3106 if (flags
& KF_FLAG_NO_ALIAS
)
3107 index
|= CSIDL_FLAG_NO_ALIAS
;
3109 if (flags
& KF_FLAG_INIT
)
3110 index
|= CSIDL_FLAG_PER_USER_INIT
;
3112 if (flags
& ~(KF_FLAG_CREATE
|KF_FLAG_DONT_VERIFY
|KF_FLAG_NO_ALIAS
|KF_FLAG_INIT
))
3114 FIXME("flags 0x%08x not supported\n", flags
);
3115 return E_INVALIDARG
;
3118 hr
= SHGetFolderPathW( NULL
, index
, token
, 0, folder
);
3121 *path
= CoTaskMemAlloc( (strlenW( folder
) + 1) * sizeof(WCHAR
) );
3123 return E_OUTOFMEMORY
;
3124 strcpyW( *path
, folder
);
3129 /*************************************************************************
3130 * SHGetFolderPathEx [SHELL32.@]
3132 HRESULT WINAPI
SHGetFolderPathEx(REFKNOWNFOLDERID rfid
, DWORD flags
, HANDLE token
, LPWSTR path
, DWORD len
)
3137 TRACE("%s, 0x%08x, %p, %p, %u\n", debugstr_guid(rfid
), flags
, token
, path
, len
);
3139 if (!path
|| !len
) return E_INVALIDARG
;
3141 hr
= SHGetKnownFolderPath( rfid
, flags
, token
, &buffer
);
3142 if (SUCCEEDED( hr
))
3144 if (strlenW( buffer
) + 1 > len
)
3146 CoTaskMemFree( buffer
);
3147 return HRESULT_FROM_WIN32( ERROR_INSUFFICIENT_BUFFER
);
3149 strcpyW( path
, buffer
);
3150 CoTaskMemFree( buffer
);
3156 * Internal function to convert known folder identifier to path of registry key
3157 * associated with known folder.
3160 * rfid [I] pointer to known folder identifier (may be NULL)
3161 * lpStringGuid [I] string with known folder identifier (used when rfid is NULL)
3162 * lpPath [O] place to store string address. String should be
3163 * later freed using HeapFree(GetProcessHeap(),0, ... )
3165 static HRESULT
get_known_folder_registry_path(
3166 REFKNOWNFOLDERID rfid
,
3167 LPWSTR lpStringGuid
,
3170 static const WCHAR sBackslash
[] = {'\\',0};
3175 TRACE("(%s, %s, %p)\n", debugstr_guid(rfid
), debugstr_w(lpStringGuid
), lpPath
);
3178 StringFromGUID2(rfid
, sGuid
, sizeof(sGuid
)/sizeof(sGuid
[0]));
3180 lstrcpyW(sGuid
, lpStringGuid
);
3182 length
= lstrlenW(szKnownFolderDescriptions
)+51;
3183 *lpPath
= HeapAlloc(GetProcessHeap(), 0, length
*sizeof(WCHAR
));
3189 lstrcpyW(*lpPath
, szKnownFolderDescriptions
);
3190 lstrcatW(*lpPath
, sBackslash
);
3191 lstrcatW(*lpPath
, sGuid
);
3197 static HRESULT
get_known_folder_wstr(const WCHAR
*regpath
, const WCHAR
*value
, WCHAR
**out
)
3203 hr
= HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE
, regpath
, value
, RRF_RT_REG_SZ
, NULL
, NULL
, &size
));
3207 *out
= CoTaskMemAlloc(size
);
3209 return E_OUTOFMEMORY
;
3211 hr
= HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE
, regpath
, value
, RRF_RT_REG_SZ
, NULL
, *out
, &size
));
3213 CoTaskMemFree(*out
);
3220 static HRESULT
get_known_folder_dword(const WCHAR
*registryPath
, const WCHAR
*value
, DWORD
*out
)
3222 DWORD dwSize
= sizeof(DWORD
);
3224 return HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE
, registryPath
, value
, RRF_RT_DWORD
, &dwType
, out
, &dwSize
));
3228 * Internal function to get place where folder redirection information are stored.
3231 * rfid [I] pointer to known folder identifier (may be NULL)
3232 * rootKey [O] root key where the redirection information are stored
3233 * It can be HKLM for COMMON folders, and HKCU for PERUSER folders.
3234 * However, besides root key, path is always that same, and is stored
3235 * as "szKnownFolderRedirections" constant
3237 static HRESULT
get_known_folder_redirection_place(
3238 REFKNOWNFOLDERID rfid
,
3242 LPWSTR lpRegistryPath
= NULL
;
3243 KF_CATEGORY category
;
3245 /* first, get known folder's category */
3246 hr
= get_known_folder_registry_path(rfid
, NULL
, &lpRegistryPath
);
3249 hr
= get_known_folder_dword(lpRegistryPath
, szCategory
, &category
);
3253 if(category
== KF_CATEGORY_COMMON
)
3255 *rootKey
= HKEY_LOCAL_MACHINE
;
3258 else if(category
== KF_CATEGORY_PERUSER
)
3260 *rootKey
= HKEY_CURRENT_USER
;
3267 HeapFree(GetProcessHeap(), 0, lpRegistryPath
);
3271 static HRESULT
get_known_folder_path_by_id(REFKNOWNFOLDERID folderId
, LPWSTR lpRegistryPath
, DWORD dwFlags
, LPWSTR
*ppszPath
);
3273 static HRESULT
redirect_known_folder(
3274 REFKNOWNFOLDERID rfid
,
3276 KF_REDIRECT_FLAGS flags
,
3277 LPCWSTR pszTargetPath
,
3279 KNOWNFOLDERID
const *pExclusion
,
3283 HKEY rootKey
= HKEY_LOCAL_MACHINE
, hKey
;
3285 LPWSTR lpRegistryPath
= NULL
, lpSrcPath
= NULL
;
3286 TRACE("(%s, %p, 0x%08x, %s, %d, %p, %p)\n", debugstr_guid(rfid
), hwnd
, flags
, debugstr_w(pszTargetPath
), cFolders
, pExclusion
, ppszError
);
3288 if (ppszError
) *ppszError
= NULL
;
3290 hr
= get_known_folder_registry_path(rfid
, NULL
, &lpRegistryPath
);
3293 hr
= get_known_folder_path_by_id(rfid
, lpRegistryPath
, 0, &lpSrcPath
);
3295 HeapFree(GetProcessHeap(), 0, lpRegistryPath
);
3297 /* get path to redirection storage */
3299 hr
= get_known_folder_redirection_place(rfid
, &rootKey
);
3301 /* write redirection information */
3303 hr
= HRESULT_FROM_WIN32(RegCreateKeyExW(rootKey
, szKnownFolderRedirections
, 0, NULL
, 0, KEY_WRITE
, NULL
, &hKey
, NULL
));
3307 StringFromGUID2(rfid
, sGuid
, sizeof(sGuid
)/sizeof(sGuid
[0]));
3309 hr
= HRESULT_FROM_WIN32(RegSetValueExW(hKey
, sGuid
, 0, REG_SZ
, (LPBYTE
)pszTargetPath
, (lstrlenW(pszTargetPath
)+1)*sizeof(WCHAR
)));
3314 /* make sure destination path exists */
3315 SHCreateDirectory(NULL
, pszTargetPath
);
3317 /* copy content if required */
3318 if(SUCCEEDED(hr
) && (flags
& KF_REDIRECT_COPY_CONTENTS
) )
3320 static const WCHAR sWildcard
[] = {'\\','*',0};
3321 WCHAR srcPath
[MAX_PATH
+1], dstPath
[MAX_PATH
+1];
3322 SHFILEOPSTRUCTW fileOp
;
3324 ZeroMemory(srcPath
, sizeof(srcPath
));
3325 lstrcpyW(srcPath
, lpSrcPath
);
3326 lstrcatW(srcPath
, sWildcard
);
3328 ZeroMemory(dstPath
, sizeof(dstPath
));
3329 lstrcpyW(dstPath
, pszTargetPath
);
3331 ZeroMemory(&fileOp
, sizeof(fileOp
));
3333 if(flags
& KF_REDIRECT_DEL_SOURCE_CONTENTS
)
3334 fileOp
.wFunc
= FO_MOVE
;
3336 fileOp
.wFunc
= FO_COPY
;
3338 fileOp
.pFrom
= srcPath
;
3339 fileOp
.pTo
= dstPath
;
3340 fileOp
.fFlags
= FOF_NO_UI
;
3342 hr
= (SHFileOperationW(&fileOp
)==0 ? S_OK
: E_FAIL
);
3344 if(flags
& KF_REDIRECT_DEL_SOURCE_CONTENTS
)
3346 ZeroMemory(srcPath
, sizeof(srcPath
));
3347 lstrcpyW(srcPath
, lpSrcPath
);
3349 ZeroMemory(&fileOp
, sizeof(fileOp
));
3350 fileOp
.wFunc
= FO_DELETE
;
3351 fileOp
.pFrom
= srcPath
;
3352 fileOp
.fFlags
= FOF_NO_UI
;
3354 hr
= (SHFileOperationW(&fileOp
)==0 ? S_OK
: E_FAIL
);
3358 CoTaskMemFree(lpSrcPath
);
3366 IKnownFolder IKnownFolder_iface
;
3369 LPWSTR registryPath
;
3372 static inline struct knownfolder
*impl_from_IKnownFolder( IKnownFolder
*iface
)
3374 return CONTAINING_RECORD( iface
, struct knownfolder
, IKnownFolder_iface
);
3377 static ULONG WINAPI
knownfolder_AddRef(
3378 IKnownFolder
*iface
)
3380 struct knownfolder
*knownfolder
= impl_from_IKnownFolder( iface
);
3381 return InterlockedIncrement( &knownfolder
->refs
);
3384 static ULONG WINAPI
knownfolder_Release(
3385 IKnownFolder
*iface
)
3387 struct knownfolder
*knownfolder
= impl_from_IKnownFolder( iface
);
3388 LONG refs
= InterlockedDecrement( &knownfolder
->refs
);
3391 TRACE("destroying %p\n", knownfolder
);
3392 HeapFree( GetProcessHeap(), 0, knownfolder
->registryPath
);
3393 HeapFree( GetProcessHeap(), 0, knownfolder
);
3398 static HRESULT WINAPI
knownfolder_QueryInterface(
3399 IKnownFolder
*iface
,
3403 struct knownfolder
*This
= impl_from_IKnownFolder( iface
);
3405 TRACE("%p %s %p\n", This
, debugstr_guid( riid
), ppv
);
3407 if ( IsEqualGUID( riid
, &IID_IKnownFolder
) ||
3408 IsEqualGUID( riid
, &IID_IUnknown
) )
3414 FIXME("interface %s not implemented\n", debugstr_guid(riid
));
3415 return E_NOINTERFACE
;
3417 IKnownFolder_AddRef( iface
);
3421 static HRESULT
knownfolder_set_id(
3422 struct knownfolder
*knownfolder
,
3423 const KNOWNFOLDERID
*kfid
)
3428 TRACE("%s\n", debugstr_guid(kfid
));
3430 knownfolder
->id
= *kfid
;
3432 /* check is it registry-registered folder */
3433 hr
= get_known_folder_registry_path(kfid
, NULL
, &knownfolder
->registryPath
);
3435 hr
= HRESULT_FROM_WIN32(RegOpenKeyExW(HKEY_LOCAL_MACHINE
, knownfolder
->registryPath
, 0, 0, &hKey
));
3444 /* This known folder is not registered. To mark it, we set registryPath to NULL */
3445 HeapFree(GetProcessHeap(), 0, knownfolder
->registryPath
);
3446 knownfolder
->registryPath
= NULL
;
3453 static HRESULT WINAPI
knownfolder_GetId(
3454 IKnownFolder
*iface
,
3455 KNOWNFOLDERID
*pkfid
)
3457 struct knownfolder
*knownfolder
= impl_from_IKnownFolder( iface
);
3459 TRACE("%p\n", pkfid
);
3461 *pkfid
= knownfolder
->id
;
3465 static HRESULT WINAPI
knownfolder_GetCategory(
3466 IKnownFolder
*iface
,
3467 KF_CATEGORY
*pCategory
)
3469 struct knownfolder
*knownfolder
= impl_from_IKnownFolder(iface
);
3472 TRACE("%p, %p\n", knownfolder
, pCategory
);
3474 /* we cannot get a category for a folder which is not registered */
3475 if(!knownfolder
->registryPath
)
3479 hr
= get_known_folder_dword(knownfolder
->registryPath
, szCategory
, pCategory
);
3484 static HRESULT WINAPI
knownfolder_GetShellItem(
3485 IKnownFolder
*iface
,
3490 FIXME("0x%08x, %s, %p\n", dwFlags
, debugstr_guid(riid
), ppv
);
3494 static HRESULT
get_known_folder_path(
3496 LPWSTR registryPath
,
3499 static const WCHAR sBackslash
[] = {'\\',0};
3501 DWORD dwSize
, dwType
;
3502 WCHAR path
[MAX_PATH
] = {0};
3503 WCHAR parentGuid
[39];
3504 KF_CATEGORY category
;
3505 LPWSTR parentRegistryPath
, parentPath
;
3506 HKEY hRedirectionRootKey
= NULL
;
3508 TRACE("(%s, %p)\n", debugstr_w(registryPath
), ppszPath
);
3511 /* check if folder has parent */
3512 dwSize
= sizeof(parentGuid
);
3513 hr
= HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE
, registryPath
, szParentFolder
, RRF_RT_REG_SZ
, &dwType
, parentGuid
, &dwSize
));
3514 if(hr
== HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND
)) hr
= S_FALSE
;
3518 /* get parent's known folder path (recursive) */
3519 hr
= get_known_folder_registry_path(NULL
, parentGuid
, &parentRegistryPath
);
3520 if(FAILED(hr
)) return hr
;
3522 hr
= get_known_folder_path(parentGuid
, parentRegistryPath
, &parentPath
);
3524 HeapFree(GetProcessHeap(), 0, parentRegistryPath
);
3528 lstrcatW(path
, parentPath
);
3529 lstrcatW(path
, sBackslash
);
3531 HeapFree(GetProcessHeap(), 0, parentRegistryPath
);
3532 HeapFree(GetProcessHeap(), 0, parentPath
);
3535 /* check, if folder was redirected */
3537 hr
= get_known_folder_dword(registryPath
, szCategory
, &category
);
3541 if(category
== KF_CATEGORY_COMMON
)
3542 hRedirectionRootKey
= HKEY_LOCAL_MACHINE
;
3543 else if(category
== KF_CATEGORY_PERUSER
)
3544 hRedirectionRootKey
= HKEY_CURRENT_USER
;
3546 if(hRedirectionRootKey
)
3548 hr
= HRESULT_FROM_WIN32(RegGetValueW(hRedirectionRootKey
, szKnownFolderRedirections
, sFolderId
, RRF_RT_REG_SZ
, NULL
, NULL
, &dwSize
));
3552 *ppszPath
= CoTaskMemAlloc(dwSize
+(lstrlenW(path
)+1)*sizeof(WCHAR
));
3553 if(!*ppszPath
) hr
= E_OUTOFMEMORY
;
3558 lstrcpyW(*ppszPath
, path
);
3559 hr
= HRESULT_FROM_WIN32(RegGetValueW(hRedirectionRootKey
, szKnownFolderRedirections
, sFolderId
, RRF_RT_REG_SZ
, NULL
, *ppszPath
+ lstrlenW(path
), &dwSize
));
3565 /* no redirection, use previous way - read the relative path from folder definition */
3566 hr
= HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE
, registryPath
, szRelativePath
, RRF_RT_REG_SZ
, &dwType
, NULL
, &dwSize
));
3570 *ppszPath
= CoTaskMemAlloc(dwSize
+(lstrlenW(path
)+1)*sizeof(WCHAR
));
3571 if(!*ppszPath
) hr
= E_OUTOFMEMORY
;
3576 lstrcpyW(*ppszPath
, path
);
3577 hr
= HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE
, registryPath
, szRelativePath
, RRF_RT_REG_SZ
, &dwType
, *ppszPath
+ lstrlenW(path
), &dwSize
));
3582 TRACE("returning path: %s\n", debugstr_w(*ppszPath
));
3586 static HRESULT
get_known_folder_path_by_id(
3587 REFKNOWNFOLDERID folderId
,
3588 LPWSTR lpRegistryPath
,
3592 HRESULT hr
= E_FAIL
;
3596 TRACE("(%s, %s, 0x%08x, %p)\n", debugstr_guid(folderId
), debugstr_w(lpRegistryPath
), dwFlags
, ppszPath
);
3598 /* if this is registry-registered known folder, get path from registry */
3601 StringFromGUID2(folderId
, sGuid
, sizeof(sGuid
)/sizeof(sGuid
[0]));
3603 hr
= get_known_folder_path(sGuid
, lpRegistryPath
, ppszPath
);
3605 /* in other case, use older way */
3608 hr
= SHGetKnownFolderPath( folderId
, dwFlags
, NULL
, ppszPath
);
3610 if (FAILED(hr
)) return hr
;
3612 /* check if known folder really exists */
3613 dwAttributes
= GetFileAttributesW(*ppszPath
);
3614 if(dwAttributes
== INVALID_FILE_ATTRIBUTES
|| !(dwAttributes
& FILE_ATTRIBUTE_DIRECTORY
) )
3616 TRACE("directory %s not found\n", debugstr_w(*ppszPath
));
3617 CoTaskMemFree(*ppszPath
);
3619 hr
= HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND
);
3625 static HRESULT WINAPI
knownfolder_GetPath(
3626 IKnownFolder
*iface
,
3630 struct knownfolder
*knownfolder
= impl_from_IKnownFolder( iface
);
3631 TRACE("(%p, 0x%08x, %p)\n", knownfolder
, dwFlags
, ppszPath
);
3633 return get_known_folder_path_by_id(&knownfolder
->id
, knownfolder
->registryPath
, dwFlags
, ppszPath
);
3636 static HRESULT WINAPI
knownfolder_SetPath(
3637 IKnownFolder
*iface
,
3641 struct knownfolder
*knownfolder
= impl_from_IKnownFolder( iface
);
3644 TRACE("(%p, 0x%08x, %s)\n", knownfolder
, dwFlags
, debugstr_w(pszPath
));
3646 /* check if the known folder is registered */
3647 if(!knownfolder
->registryPath
)
3651 hr
= redirect_known_folder(&knownfolder
->id
, NULL
, 0, pszPath
, 0, NULL
, NULL
);
3656 static HRESULT WINAPI
knownfolder_GetIDList(
3657 IKnownFolder
*iface
,
3659 PIDLIST_ABSOLUTE
*ppidl
)
3661 FIXME("0x%08x, %p\n", dwFlags
, ppidl
);
3665 static HRESULT WINAPI
knownfolder_GetFolderType(
3666 IKnownFolder
*iface
,
3667 FOLDERTYPEID
*pftid
)
3669 FIXME("%p\n", pftid
);
3673 static HRESULT WINAPI
knownfolder_GetRedirectionCapabilities(
3674 IKnownFolder
*iface
,
3675 KF_REDIRECTION_CAPABILITIES
*pCapabilities
)
3677 FIXME("%p\n", pCapabilities
);
3681 static HRESULT WINAPI
knownfolder_GetFolderDefinition(
3682 IKnownFolder
*iface
,
3683 KNOWNFOLDER_DEFINITION
*pKFD
)
3685 struct knownfolder
*knownfolder
= impl_from_IKnownFolder( iface
);
3688 WCHAR parentGuid
[39];
3689 TRACE("(%p, %p)\n", knownfolder
, pKFD
);
3691 if(!pKFD
) return E_INVALIDARG
;
3693 ZeroMemory(pKFD
, sizeof(*pKFD
));
3695 /* required fields */
3696 hr
= get_known_folder_dword(knownfolder
->registryPath
, szCategory
, &pKFD
->category
);
3700 hr
= get_known_folder_wstr(knownfolder
->registryPath
, szName
, &pKFD
->pszName
);
3704 /* optional fields */
3705 dwSize
= sizeof(parentGuid
);
3706 hr
= HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE
, knownfolder
->registryPath
, szParentFolder
,
3707 RRF_RT_REG_SZ
, NULL
, parentGuid
, &dwSize
));
3709 IIDFromString(parentGuid
, &pKFD
->fidParent
);
3711 get_known_folder_dword(knownfolder
->registryPath
, szAttributes
, &pKFD
->dwAttributes
);
3713 get_known_folder_wstr(knownfolder
->registryPath
, szRelativePath
, &pKFD
->pszRelativePath
);
3718 static const struct IKnownFolderVtbl knownfolder_vtbl
=
3720 knownfolder_QueryInterface
,
3722 knownfolder_Release
,
3724 knownfolder_GetCategory
,
3725 knownfolder_GetShellItem
,
3726 knownfolder_GetPath
,
3727 knownfolder_SetPath
,
3728 knownfolder_GetIDList
,
3729 knownfolder_GetFolderType
,
3730 knownfolder_GetRedirectionCapabilities
,
3731 knownfolder_GetFolderDefinition
3734 static HRESULT
knownfolder_create( struct knownfolder
**knownfolder
)
3736 struct knownfolder
*kf
;
3738 kf
= HeapAlloc( GetProcessHeap(), 0, sizeof(*kf
) );
3739 if (!kf
) return E_OUTOFMEMORY
;
3741 kf
->IKnownFolder_iface
.lpVtbl
= &knownfolder_vtbl
;
3743 memset( &kf
->id
, 0, sizeof(kf
->id
) );
3744 kf
->registryPath
= NULL
;
3748 TRACE("returning iface %p\n", &kf
->IKnownFolder_iface
);
3752 struct foldermanager
3754 IKnownFolderManager IKnownFolderManager_iface
;
3760 static inline struct foldermanager
*impl_from_IKnownFolderManager( IKnownFolderManager
*iface
)
3762 return CONTAINING_RECORD( iface
, struct foldermanager
, IKnownFolderManager_iface
);
3765 static ULONG WINAPI
foldermanager_AddRef(
3766 IKnownFolderManager
*iface
)
3768 struct foldermanager
*foldermanager
= impl_from_IKnownFolderManager( iface
);
3769 return InterlockedIncrement( &foldermanager
->refs
);
3772 static ULONG WINAPI
foldermanager_Release(
3773 IKnownFolderManager
*iface
)
3775 struct foldermanager
*foldermanager
= impl_from_IKnownFolderManager( iface
);
3776 LONG refs
= InterlockedDecrement( &foldermanager
->refs
);
3779 TRACE("destroying %p\n", foldermanager
);
3780 HeapFree( GetProcessHeap(), 0, foldermanager
->ids
);
3781 HeapFree( GetProcessHeap(), 0, foldermanager
);
3786 static HRESULT WINAPI
foldermanager_QueryInterface(
3787 IKnownFolderManager
*iface
,
3791 struct foldermanager
*This
= impl_from_IKnownFolderManager( iface
);
3793 TRACE("%p %s %p\n", This
, debugstr_guid( riid
), ppv
);
3795 if ( IsEqualGUID( riid
, &IID_IKnownFolderManager
) ||
3796 IsEqualGUID( riid
, &IID_IUnknown
) )
3802 FIXME("interface %s not implemented\n", debugstr_guid(riid
));
3803 return E_NOINTERFACE
;
3805 IKnownFolderManager_AddRef( iface
);
3809 static HRESULT WINAPI
foldermanager_FolderIdFromCsidl(
3810 IKnownFolderManager
*iface
,
3812 KNOWNFOLDERID
*pfid
)
3814 TRACE("%d, %p\n", nCsidl
, pfid
);
3816 if (nCsidl
>= sizeof(CSIDL_Data
) / sizeof(CSIDL_Data
[0]))
3817 return E_INVALIDARG
;
3818 *pfid
= *CSIDL_Data
[nCsidl
].id
;
3822 static HRESULT WINAPI
foldermanager_FolderIdToCsidl(
3823 IKnownFolderManager
*iface
,
3824 REFKNOWNFOLDERID rfid
,
3829 TRACE("%s, %p\n", debugstr_guid(rfid
), pnCsidl
);
3831 csidl
= csidl_from_id( rfid
);
3832 if (csidl
== -1) return E_INVALIDARG
;
3837 static HRESULT WINAPI
foldermanager_GetFolderIds(
3838 IKnownFolderManager
*iface
,
3839 KNOWNFOLDERID
**ppKFId
,
3842 struct foldermanager
*fm
= impl_from_IKnownFolderManager( iface
);
3844 TRACE("%p, %p\n", ppKFId
, pCount
);
3847 *pCount
= fm
->num_ids
;
3851 static BOOL
is_knownfolder( struct foldermanager
*fm
, const KNOWNFOLDERID
*id
)
3855 LPWSTR registryPath
= NULL
;
3858 /* TODO: move all entries from "CSIDL_Data" static array to registry known folder descriptions */
3859 for (i
= 0; i
< fm
->num_ids
; i
++)
3860 if (IsEqualGUID( &fm
->ids
[i
], id
)) return TRUE
;
3862 hr
= get_known_folder_registry_path(id
, NULL
, ®istryPath
);
3865 hr
= HRESULT_FROM_WIN32(RegOpenKeyExW(HKEY_LOCAL_MACHINE
, registryPath
, 0, 0, &hKey
));
3866 HeapFree(GetProcessHeap(), 0, registryPath
);
3878 static HRESULT WINAPI
foldermanager_GetFolder(
3879 IKnownFolderManager
*iface
,
3880 REFKNOWNFOLDERID rfid
,
3881 IKnownFolder
**ppkf
)
3883 struct foldermanager
*fm
= impl_from_IKnownFolderManager( iface
);
3884 struct knownfolder
*kf
;
3887 TRACE("%s, %p\n", debugstr_guid(rfid
), ppkf
);
3889 if (!is_knownfolder( fm
, rfid
))
3891 WARN("unknown folder\n");
3892 return E_INVALIDARG
;
3894 hr
= knownfolder_create( &kf
);
3895 if (SUCCEEDED( hr
))
3897 hr
= knownfolder_set_id( kf
, rfid
);
3898 *ppkf
= &kf
->IKnownFolder_iface
;
3906 static HRESULT WINAPI
foldermanager_GetFolderByName(
3907 IKnownFolderManager
*iface
,
3908 LPCWSTR pszCanonicalName
,
3909 IKnownFolder
**ppkf
)
3911 FIXME("%s, %p\n", debugstr_w(pszCanonicalName
), ppkf
);
3915 static HRESULT WINAPI
foldermanager_RegisterFolder(
3916 IKnownFolderManager
*iface
,
3917 REFKNOWNFOLDERID rfid
,
3918 KNOWNFOLDER_DEFINITION
const *pKFD
)
3923 LPWSTR registryPath
= NULL
;
3924 TRACE("(%p, %s, %p)\n", iface
, debugstr_guid(rfid
), pKFD
);
3926 hr
= get_known_folder_registry_path(rfid
, NULL
, ®istryPath
);
3927 TRACE("registry path: %s\n", debugstr_w(registryPath
));
3930 hr
= HRESULT_FROM_WIN32(RegCreateKeyExW(HKEY_LOCAL_MACHINE
, registryPath
, 0, NULL
, 0, KEY_WRITE
, 0, &hKey
, &dwDisp
));
3934 if(dwDisp
== REG_OPENED_EXISTING_KEY
)
3938 hr
= HRESULT_FROM_WIN32(RegSetValueExW(hKey
, szCategory
, 0, REG_DWORD
, (LPBYTE
)&pKFD
->category
, sizeof(pKFD
->category
)));
3941 hr
= HRESULT_FROM_WIN32(RegSetValueExW(hKey
, szName
, 0, REG_SZ
, (LPBYTE
)pKFD
->pszName
, (lstrlenW(pKFD
->pszName
)+1)*sizeof(WCHAR
) ));
3943 if(SUCCEEDED(hr
) && !IsEqualGUID(&pKFD
->fidParent
, &GUID_NULL
))
3945 WCHAR sParentGuid
[39];
3946 StringFromGUID2(&pKFD
->fidParent
, sParentGuid
, sizeof(sParentGuid
)/sizeof(sParentGuid
[0]));
3948 /* this known folder has parent folder */
3949 hr
= HRESULT_FROM_WIN32(RegSetValueExW(hKey
, szParentFolder
, 0, REG_SZ
, (LPBYTE
)sParentGuid
, sizeof(sParentGuid
)));
3952 if(SUCCEEDED(hr
) && pKFD
->category
!= KF_CATEGORY_VIRTUAL
)
3954 if(!pKFD
->pszRelativePath
)
3958 hr
= HRESULT_FROM_WIN32(RegSetValueExW(hKey
, szRelativePath
, 0, REG_SZ
, (LPBYTE
)pKFD
->pszRelativePath
, (lstrlenW(pKFD
->pszRelativePath
)+1)*sizeof(WCHAR
) ));
3964 SHDeleteKeyW(HKEY_LOCAL_MACHINE
, registryPath
);
3967 HeapFree(GetProcessHeap(), 0, registryPath
);
3971 static HRESULT WINAPI
foldermanager_UnregisterFolder(
3972 IKnownFolderManager
*iface
,
3973 REFKNOWNFOLDERID rfid
)
3976 LPWSTR registryPath
= NULL
;
3977 TRACE("(%p, %s)\n", iface
, debugstr_guid(rfid
));
3979 hr
= get_known_folder_registry_path(rfid
, NULL
, ®istryPath
);
3982 hr
= HRESULT_FROM_WIN32(SHDeleteKeyW(HKEY_LOCAL_MACHINE
, registryPath
));
3984 HeapFree(GetProcessHeap(), 0, registryPath
);
3988 static HRESULT WINAPI
foldermanager_FindFolderFromPath(
3989 IKnownFolderManager
*iface
,
3992 IKnownFolder
**ppkf
)
3994 FIXME("%s, 0x%08x, %p\n", debugstr_w(pszPath
), mode
, ppkf
);
3998 static HRESULT WINAPI
foldermanager_FindFolderFromIDList(
3999 IKnownFolderManager
*iface
,
4000 PCIDLIST_ABSOLUTE pidl
,
4001 IKnownFolder
**ppkf
)
4003 FIXME("%p, %p\n", pidl
, ppkf
);
4007 static HRESULT WINAPI
foldermanager_Redirect(
4008 IKnownFolderManager
*iface
,
4009 REFKNOWNFOLDERID rfid
,
4011 KF_REDIRECT_FLAGS flags
,
4012 LPCWSTR pszTargetPath
,
4014 KNOWNFOLDERID
const *pExclusion
,
4017 return redirect_known_folder(rfid
, hwnd
, flags
, pszTargetPath
, cFolders
, pExclusion
, ppszError
);
4020 static const struct IKnownFolderManagerVtbl foldermanager_vtbl
=
4022 foldermanager_QueryInterface
,
4023 foldermanager_AddRef
,
4024 foldermanager_Release
,
4025 foldermanager_FolderIdFromCsidl
,
4026 foldermanager_FolderIdToCsidl
,
4027 foldermanager_GetFolderIds
,
4028 foldermanager_GetFolder
,
4029 foldermanager_GetFolderByName
,
4030 foldermanager_RegisterFolder
,
4031 foldermanager_UnregisterFolder
,
4032 foldermanager_FindFolderFromPath
,
4033 foldermanager_FindFolderFromIDList
,
4034 foldermanager_Redirect
4037 static HRESULT
foldermanager_create( void **ppv
)
4040 struct foldermanager
*fm
;
4042 fm
= HeapAlloc( GetProcessHeap(), 0, sizeof(*fm
) );
4043 if (!fm
) return E_OUTOFMEMORY
;
4045 fm
->IKnownFolderManager_iface
.lpVtbl
= &foldermanager_vtbl
;
4049 for (i
= 0; i
< sizeof(CSIDL_Data
) / sizeof(CSIDL_Data
[0]); i
++)
4051 if (!IsEqualGUID( CSIDL_Data
[i
].id
, &GUID_NULL
)) fm
->num_ids
++;
4053 fm
->ids
= HeapAlloc( GetProcessHeap(), 0, fm
->num_ids
* sizeof(KNOWNFOLDERID
) );
4056 HeapFree( GetProcessHeap(), 0, fm
);
4057 return E_OUTOFMEMORY
;
4059 for (i
= j
= 0; i
< sizeof(CSIDL_Data
) / sizeof(CSIDL_Data
[0]); i
++)
4061 if (!IsEqualGUID( CSIDL_Data
[i
].id
, &GUID_NULL
))
4063 fm
->ids
[j
] = *CSIDL_Data
[i
].id
;
4067 TRACE("found %u known folders\n", fm
->num_ids
);
4068 *ppv
= &fm
->IKnownFolderManager_iface
;
4070 TRACE("returning iface %p\n", *ppv
);
4074 HRESULT WINAPI
KnownFolderManager_Constructor( IUnknown
*punk
, REFIID riid
, void **ppv
)
4076 TRACE("%p, %s, %p\n", punk
, debugstr_guid(riid
), ppv
);
4081 return CLASS_E_NOAGGREGATION
;
4083 return foldermanager_create( ppv
);
4086 HRESULT WINAPI
SHGetKnownFolderIDList(REFKNOWNFOLDERID rfid
, DWORD flags
, HANDLE token
, PIDLIST_ABSOLUTE
*pidl
)
4088 FIXME("%s, 0x%08x, %p, %p\n", debugstr_guid(rfid
), flags
, token
, pidl
);