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
28 #include "wine/port.h"
34 #include "wine/debug.h"
45 #include "shell32_main.h"
46 #include "undocshell.h"
48 #include "wine/unicode.h"
53 WINE_DEFAULT_DEBUG_CHANNEL(shell);
56 ########## Combining and Constructing paths ##########
59 /*************************************************************************
60 * PathAppend [SHELL32.36]
62 BOOL WINAPI PathAppendAW(
66 if (SHELL_OsIsUnicode())
67 return PathAppendW(lpszPath1, lpszPath2);
68 return PathAppendA(lpszPath1, lpszPath2);
71 /*************************************************************************
72 * PathCombine [SHELL32.37]
74 LPVOID WINAPI PathCombineAW(
79 if (SHELL_OsIsUnicode())
80 return PathCombineW( szDest, lpszDir, lpszFile );
81 return PathCombineA( szDest, lpszDir, lpszFile );
84 /*************************************************************************
85 * PathAddBackslash [SHELL32.32]
87 LPVOID WINAPI PathAddBackslashAW(LPVOID lpszPath)
89 if(SHELL_OsIsUnicode())
90 return PathAddBackslashW(lpszPath);
91 return PathAddBackslashA(lpszPath);
94 /*************************************************************************
95 * PathBuildRoot [SHELL32.30]
97 LPVOID WINAPI PathBuildRootAW(LPVOID lpszPath, int drive)
99 if(SHELL_OsIsUnicode())
100 return PathBuildRootW(lpszPath, drive);
101 return PathBuildRootA(lpszPath, drive);
105 Extracting Component Parts
108 /*************************************************************************
109 * PathFindFileName [SHELL32.34]
111 LPVOID WINAPI PathFindFileNameAW(LPCVOID lpszPath)
113 if(SHELL_OsIsUnicode())
114 return PathFindFileNameW(lpszPath);
115 return PathFindFileNameA(lpszPath);
118 /*************************************************************************
119 * PathFindExtension [SHELL32.31]
121 LPVOID WINAPI PathFindExtensionAW(LPCVOID lpszPath)
123 if (SHELL_OsIsUnicode())
124 return PathFindExtensionW(lpszPath);
125 return PathFindExtensionA(lpszPath);
129 /*************************************************************************
130 * PathGetExtensionA [internal]
133 * exported by ordinal
134 * return value points to the first char after the dot
136 static LPSTR PathGetExtensionA(LPCSTR lpszPath)
138 TRACE("(%s)\n",lpszPath);
140 lpszPath = PathFindExtensionA(lpszPath);
141 return (LPSTR)(*lpszPath?(lpszPath+1):lpszPath);
144 /*************************************************************************
145 * PathGetExtensionW [internal]
147 static LPWSTR PathGetExtensionW(LPCWSTR lpszPath)
149 TRACE("(%s)\n",debugstr_w(lpszPath));
151 lpszPath = PathFindExtensionW(lpszPath);
152 return (LPWSTR)(*lpszPath?(lpszPath+1):lpszPath);
155 /*************************************************************************
156 * PathGetExtension [SHELL32.158]
158 LPVOID WINAPI PathGetExtensionAW(LPCVOID lpszPath,DWORD void1, DWORD void2)
160 if (SHELL_OsIsUnicode())
161 return PathGetExtensionW(lpszPath);
162 return PathGetExtensionA(lpszPath);
165 /*************************************************************************
166 * PathGetArgs [SHELL32.52]
168 LPVOID WINAPI PathGetArgsAW(LPVOID lpszPath)
170 if (SHELL_OsIsUnicode())
171 return PathGetArgsW(lpszPath);
172 return PathGetArgsA(lpszPath);
175 /*************************************************************************
176 * PathGetDriveNumber [SHELL32.57]
178 int WINAPI PathGetDriveNumberAW(LPVOID lpszPath)
180 if (SHELL_OsIsUnicode())
181 return PathGetDriveNumberW(lpszPath);
182 return PathGetDriveNumberA(lpszPath);
185 /*************************************************************************
186 * PathRemoveFileSpec [SHELL32.35]
188 BOOL WINAPI PathRemoveFileSpecAW(LPVOID lpszPath)
190 if (SHELL_OsIsUnicode())
191 return PathRemoveFileSpecW(lpszPath);
192 return PathRemoveFileSpecA(lpszPath);
195 /*************************************************************************
196 * PathStripPath [SHELL32.38]
198 void WINAPI PathStripPathAW(LPVOID lpszPath)
200 if (SHELL_OsIsUnicode())
201 PathStripPathW(lpszPath);
203 PathStripPathA(lpszPath);
206 /*************************************************************************
207 * PathStripToRoot [SHELL32.50]
209 BOOL WINAPI PathStripToRootAW(LPVOID lpszPath)
211 if (SHELL_OsIsUnicode())
212 return PathStripToRootW(lpszPath);
213 return PathStripToRootA(lpszPath);
216 /*************************************************************************
217 * PathRemoveArgs [SHELL32.251]
219 void WINAPI PathRemoveArgsAW(LPVOID lpszPath)
221 if (SHELL_OsIsUnicode())
222 PathRemoveArgsW(lpszPath);
224 PathRemoveArgsA(lpszPath);
227 /*************************************************************************
228 * PathRemoveExtension [SHELL32.250]
230 void WINAPI PathRemoveExtensionAW(LPVOID lpszPath)
232 if (SHELL_OsIsUnicode())
233 PathRemoveExtensionW(lpszPath);
235 PathRemoveExtensionA(lpszPath);
243 /*************************************************************************
244 * PathGetShortPathA [internal]
246 static void PathGetShortPathA(LPSTR pszPath)
250 TRACE("%s\n", pszPath);
252 if (GetShortPathNameA(pszPath, path, MAX_PATH))
254 lstrcpyA(pszPath, path);
258 /*************************************************************************
259 * PathGetShortPathW [internal]
261 static void PathGetShortPathW(LPWSTR pszPath)
263 WCHAR path[MAX_PATH];
265 TRACE("%s\n", debugstr_w(pszPath));
267 if (GetShortPathNameW(pszPath, path, MAX_PATH))
269 lstrcpyW(pszPath, path);
273 /*************************************************************************
274 * PathGetShortPath [SHELL32.92]
276 VOID WINAPI PathGetShortPathAW(LPVOID pszPath)
278 if(SHELL_OsIsUnicode())
279 PathGetShortPathW(pszPath);
280 PathGetShortPathA(pszPath);
283 /*************************************************************************
284 * PathRemoveBlanks [SHELL32.33]
286 void WINAPI PathRemoveBlanksAW(LPVOID str)
288 if(SHELL_OsIsUnicode())
289 PathRemoveBlanksW(str);
291 PathRemoveBlanksA(str);
294 /*************************************************************************
295 * PathQuoteSpaces [SHELL32.55]
297 VOID WINAPI PathQuoteSpacesAW (LPVOID lpszPath)
299 if(SHELL_OsIsUnicode())
300 PathQuoteSpacesW(lpszPath);
302 PathQuoteSpacesA(lpszPath);
305 /*************************************************************************
306 * PathUnquoteSpaces [SHELL32.56]
308 VOID WINAPI PathUnquoteSpacesAW(LPVOID str)
310 if(SHELL_OsIsUnicode())
311 PathUnquoteSpacesW(str);
313 PathUnquoteSpacesA(str);
316 /*************************************************************************
317 * PathParseIconLocation [SHELL32.249]
319 int WINAPI PathParseIconLocationAW (LPVOID lpszPath)
321 if(SHELL_OsIsUnicode())
322 return PathParseIconLocationW(lpszPath);
323 return PathParseIconLocationA(lpszPath);
327 ########## Path Testing ##########
329 /*************************************************************************
330 * PathIsUNC [SHELL32.39]
332 BOOL WINAPI PathIsUNCAW (LPCVOID lpszPath)
334 if (SHELL_OsIsUnicode())
335 return PathIsUNCW( lpszPath );
336 return PathIsUNCA( lpszPath );
339 /*************************************************************************
340 * PathIsRelative [SHELL32.40]
342 BOOL WINAPI PathIsRelativeAW (LPCVOID lpszPath)
344 if (SHELL_OsIsUnicode())
345 return PathIsRelativeW( lpszPath );
346 return PathIsRelativeA( lpszPath );
349 /*************************************************************************
350 * PathIsRoot [SHELL32.29]
352 BOOL WINAPI PathIsRootAW(LPCVOID lpszPath)
354 if (SHELL_OsIsUnicode())
355 return PathIsRootW(lpszPath);
356 return PathIsRootA(lpszPath);
359 /*************************************************************************
360 * PathIsExeA [internal]
362 static BOOL PathIsExeA (LPCSTR lpszPath)
364 LPCSTR lpszExtension = PathGetExtensionA(lpszPath);
366 static const char * const lpszExtensions[] =
367 {"exe", "com", "pif", "cmd", "bat", "scf", "scr", NULL };
369 TRACE("path=%s\n",lpszPath);
371 for(i=0; lpszExtensions[i]; i++)
372 if (!lstrcmpiA(lpszExtension,lpszExtensions[i])) return TRUE;
377 /*************************************************************************
378 * PathIsExeW [internal]
380 static BOOL PathIsExeW (LPCWSTR lpszPath)
382 LPCWSTR lpszExtension = PathGetExtensionW(lpszPath);
384 static const WCHAR lpszExtensions[][4] =
385 {{'e','x','e','\0'}, {'c','o','m','\0'}, {'p','i','f','\0'},
386 {'c','m','d','\0'}, {'b','a','t','\0'}, {'s','c','f','\0'},
387 {'s','c','r','\0'}, {'\0'} };
389 TRACE("path=%s\n",debugstr_w(lpszPath));
391 for(i=0; lpszExtensions[i][0]; i++)
392 if (!strcmpiW(lpszExtension,lpszExtensions[i])) return TRUE;
397 /*************************************************************************
398 * PathIsExe [SHELL32.43]
400 BOOL WINAPI PathIsExeAW (LPCVOID path)
402 if (SHELL_OsIsUnicode())
403 return PathIsExeW (path);
404 return PathIsExeA(path);
407 /*************************************************************************
408 * PathIsDirectory [SHELL32.159]
410 BOOL WINAPI PathIsDirectoryAW (LPCVOID lpszPath)
412 if (SHELL_OsIsUnicode())
413 return PathIsDirectoryW (lpszPath);
414 return PathIsDirectoryA (lpszPath);
417 /*************************************************************************
418 * PathFileExists [SHELL32.45]
420 BOOL WINAPI PathFileExistsAW (LPCVOID lpszPath)
422 if (SHELL_OsIsUnicode())
423 return PathFileExistsW (lpszPath);
424 return PathFileExistsA (lpszPath);
427 /*************************************************************************
428 * PathMatchSpec [SHELL32.46]
430 BOOL WINAPI PathMatchSpecAW(LPVOID name, LPVOID mask)
432 if (SHELL_OsIsUnicode())
433 return PathMatchSpecW( name, mask );
434 return PathMatchSpecA( name, mask );
437 /*************************************************************************
438 * PathIsSameRoot [SHELL32.650]
440 BOOL WINAPI PathIsSameRootAW(LPCVOID lpszPath1, LPCVOID lpszPath2)
442 if (SHELL_OsIsUnicode())
443 return PathIsSameRootW(lpszPath1, lpszPath2);
444 return PathIsSameRootA(lpszPath1, lpszPath2);
447 /*************************************************************************
448 * IsLFNDriveA [SHELL32.41]
450 BOOL WINAPI IsLFNDriveA(LPCSTR lpszPath)
454 if (!GetVolumeInformationA(lpszPath, NULL, 0, NULL, &fnlen, NULL, NULL, 0))
459 /*************************************************************************
460 * IsLFNDriveW [SHELL32.42]
462 BOOL WINAPI IsLFNDriveW(LPCWSTR lpszPath)
466 if (!GetVolumeInformationW(lpszPath, NULL, 0, NULL, &fnlen, NULL, NULL, 0))
471 /*************************************************************************
472 * IsLFNDrive [SHELL32.119]
474 BOOL WINAPI IsLFNDriveAW(LPCVOID lpszPath)
476 if (SHELL_OsIsUnicode())
477 return IsLFNDriveW(lpszPath);
478 return IsLFNDriveA(lpszPath);
482 ########## Creating Something Unique ##########
484 /*************************************************************************
485 * PathMakeUniqueNameA [internal]
487 static BOOL PathMakeUniqueNameA(
490 LPCSTR lpszShortName,
494 FIXME("%p %u %s %s %s stub\n",
495 lpszBuffer, dwBuffSize, debugstr_a(lpszShortName),
496 debugstr_a(lpszLongName), debugstr_a(lpszPathName));
500 /*************************************************************************
501 * PathMakeUniqueNameW [internal]
503 static BOOL PathMakeUniqueNameW(
506 LPCWSTR lpszShortName,
507 LPCWSTR lpszLongName,
508 LPCWSTR lpszPathName)
510 FIXME("%p %u %s %s %s stub\n",
511 lpszBuffer, dwBuffSize, debugstr_w(lpszShortName),
512 debugstr_w(lpszLongName), debugstr_w(lpszPathName));
516 /*************************************************************************
517 * PathMakeUniqueName [SHELL32.47]
519 BOOL WINAPI PathMakeUniqueNameAW(
522 LPCVOID lpszShortName,
523 LPCVOID lpszLongName,
524 LPCVOID lpszPathName)
526 if (SHELL_OsIsUnicode())
527 return PathMakeUniqueNameW(lpszBuffer,dwBuffSize, lpszShortName,lpszLongName,lpszPathName);
528 return PathMakeUniqueNameA(lpszBuffer,dwBuffSize, lpszShortName,lpszLongName,lpszPathName);
531 /*************************************************************************
532 * PathYetAnotherMakeUniqueName [SHELL32.75]
535 * exported by ordinal
537 BOOL WINAPI PathYetAnotherMakeUniqueName(
539 LPCWSTR lpszPathName,
540 LPCWSTR lpszShortName,
541 LPCWSTR lpszLongName)
543 FIXME("(%p, %s, %s ,%s):stub.\n",
544 lpszBuffer, debugstr_w(lpszPathName), debugstr_w(lpszShortName), debugstr_w(lpszLongName));
550 ########## cleaning and resolving paths ##########
553 /*************************************************************************
554 * PathFindOnPath [SHELL32.145]
556 BOOL WINAPI PathFindOnPathAW(LPVOID sFile, LPCVOID *sOtherDirs)
558 if (SHELL_OsIsUnicode())
559 return PathFindOnPathW(sFile, (LPCWSTR *)sOtherDirs);
560 return PathFindOnPathA(sFile, (LPCSTR *)sOtherDirs);
563 /*************************************************************************
564 * PathCleanupSpec [SHELL32.171]
566 * lpszFile is changed in place.
568 int WINAPI PathCleanupSpec( LPCWSTR lpszPathW, LPWSTR lpszFileW )
574 if (SHELL_OsIsUnicode())
576 LPWSTR p = lpszFileW;
578 TRACE("Cleanup %s\n",debugstr_w(lpszFileW));
581 length = strlenW(lpszPathW);
585 int gct = PathGetCharTypeW(*p);
586 if (gct == GCT_INVALID || gct == GCT_WILD || gct == GCT_SEPARATOR)
589 rc |= PCS_REPLACEDCHAR;
595 if (length + i == MAX_PATH)
597 rc |= PCS_FATAL | PCS_PATHTOOLONG;
605 LPSTR lpszFileA = (LPSTR)lpszFileW;
606 LPCSTR lpszPathA = (LPCSTR)lpszPathW;
609 TRACE("Cleanup %s\n",debugstr_a(lpszFileA));
612 length = strlen(lpszPathA);
616 int gct = PathGetCharTypeA(*p);
617 if (gct == GCT_INVALID || gct == GCT_WILD || gct == GCT_SEPARATOR)
620 rc |= PCS_REPLACEDCHAR;
626 if (length + i == MAX_PATH)
628 rc |= PCS_FATAL | PCS_PATHTOOLONG;
637 /*************************************************************************
638 * PathQualifyA [SHELL32]
640 static BOOL PathQualifyA(LPCSTR pszPath)
642 FIXME("%s\n",pszPath);
646 /*************************************************************************
647 * PathQualifyW [SHELL32]
649 static BOOL PathQualifyW(LPCWSTR pszPath)
651 FIXME("%s\n",debugstr_w(pszPath));
655 /*************************************************************************
656 * PathQualify [SHELL32.49]
658 BOOL WINAPI PathQualifyAW(LPCVOID pszPath)
660 if (SHELL_OsIsUnicode())
661 return PathQualifyW(pszPath);
662 return PathQualifyA(pszPath);
665 static BOOL PathResolveA(
670 FIXME("(%s,%p,0x%08x),stub!\n",
671 lpszPath, *alpszPaths, dwFlags);
675 static BOOL PathResolveW(
680 FIXME("(%s,%p,0x%08x),stub!\n",
681 debugstr_w(lpszPath), debugstr_w(*alpszPaths), dwFlags);
685 /*************************************************************************
686 * PathResolve [SHELL32.51]
688 BOOL WINAPI PathResolveAW(
693 if (SHELL_OsIsUnicode())
694 return PathResolveW(lpszPath, (LPCWSTR*)alpszPaths, dwFlags);
695 return PathResolveA(lpszPath, (LPCSTR*)alpszPaths, dwFlags);
698 /*************************************************************************
699 * PathProcessCommandA
701 static LONG PathProcessCommandA (
707 FIXME("%s %p 0x%04x 0x%04x stub\n",
708 lpszPath, lpszBuff, dwBuffSize, dwFlags);
709 if(!lpszPath) return -1;
710 if(lpszBuff) strcpy(lpszBuff, lpszPath);
711 return strlen(lpszPath);
714 /*************************************************************************
715 * PathProcessCommandW
717 static LONG PathProcessCommandW (
723 FIXME("(%s, %p, 0x%04x, 0x%04x) stub\n",
724 debugstr_w(lpszPath), lpszBuff, dwBuffSize, dwFlags);
725 if(!lpszPath) return -1;
726 if(lpszBuff) strcpyW(lpszBuff, lpszPath);
727 return strlenW(lpszPath);
730 /*************************************************************************
731 * PathProcessCommand (SHELL32.653)
733 LONG WINAPI PathProcessCommandAW (
739 if (SHELL_OsIsUnicode())
740 return PathProcessCommandW(lpszPath, lpszBuff, dwBuffSize, dwFlags);
741 return PathProcessCommandA(lpszPath, lpszBuff, dwBuffSize, dwFlags);
745 ########## special ##########
748 /*************************************************************************
749 * PathSetDlgItemPath (SHELL32.48)
751 VOID WINAPI PathSetDlgItemPathAW(HWND hDlg, int id, LPCVOID pszPath)
753 if (SHELL_OsIsUnicode())
754 PathSetDlgItemPathW(hDlg, id, pszPath);
756 PathSetDlgItemPathA(hDlg, id, pszPath);
759 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'};
760 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'};
761 static const WCHAR AppDataW[] = {'A','p','p','D','a','t','a','\0'};
762 static const WCHAR CacheW[] = {'C','a','c','h','e','\0'};
763 static const WCHAR CD_BurningW[] = {'C','D',' ','B','u','r','n','i','n','g','\0'};
764 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'};
765 static const WCHAR Common_AppDataW[] = {'C','o','m','m','o','n',' ','A','p','p','D','a','t','a','\0'};
766 static const WCHAR Common_DesktopW[] = {'C','o','m','m','o','n',' ','D','e','s','k','t','o','p','\0'};
767 static const WCHAR Common_DocumentsW[] = {'C','o','m','m','o','n',' ','D','o','c','u','m','e','n','t','s','\0'};
768 static const WCHAR Common_FavoritesW[] = {'C','o','m','m','o','n',' ','F','a','v','o','r','i','t','e','s','\0'};
769 static const WCHAR CommonFilesDirW[] = {'C','o','m','m','o','n','F','i','l','e','s','D','i','r','\0'};
770 static const WCHAR CommonMusicW[] = {'C','o','m','m','o','n','M','u','s','i','c','\0'};
771 static const WCHAR CommonPicturesW[] = {'C','o','m','m','o','n','P','i','c','t','u','r','e','s','\0'};
772 static const WCHAR Common_ProgramsW[] = {'C','o','m','m','o','n',' ','P','r','o','g','r','a','m','s','\0'};
773 static const WCHAR Common_StartUpW[] = {'C','o','m','m','o','n',' ','S','t','a','r','t','U','p','\0'};
774 static const WCHAR Common_Start_MenuW[] = {'C','o','m','m','o','n',' ','S','t','a','r','t',' ','M','e','n','u','\0'};
775 static const WCHAR Common_TemplatesW[] = {'C','o','m','m','o','n',' ','T','e','m','p','l','a','t','e','s','\0'};
776 static const WCHAR CommonVideoW[] = {'C','o','m','m','o','n','V','i','d','e','o','\0'};
777 static const WCHAR CookiesW[] = {'C','o','o','k','i','e','s','\0'};
778 static const WCHAR DesktopW[] = {'D','e','s','k','t','o','p','\0'};
779 static const WCHAR FavoritesW[] = {'F','a','v','o','r','i','t','e','s','\0'};
780 static const WCHAR FontsW[] = {'F','o','n','t','s','\0'};
781 static const WCHAR HistoryW[] = {'H','i','s','t','o','r','y','\0'};
782 static const WCHAR Local_AppDataW[] = {'L','o','c','a','l',' ','A','p','p','D','a','t','a','\0'};
783 static const WCHAR My_MusicW[] = {'M','y',' ','M','u','s','i','c','\0'};
784 static const WCHAR My_PicturesW[] = {'M','y',' ','P','i','c','t','u','r','e','s','\0'};
785 static const WCHAR My_VideoW[] = {'M','y',' ','V','i','d','e','o','s','\0'};
786 static const WCHAR NetHoodW[] = {'N','e','t','H','o','o','d','\0'};
787 static const WCHAR PersonalW[] = {'P','e','r','s','o','n','a','l','\0'};
788 static const WCHAR PrintHoodW[] = {'P','r','i','n','t','H','o','o','d','\0'};
789 static const WCHAR ProgramFilesDirW[] = {'P','r','o','g','r','a','m','F','i','l','e','s','D','i','r','\0'};
790 static const WCHAR ProgramsW[] = {'P','r','o','g','r','a','m','s','\0'};
791 static const WCHAR RecentW[] = {'R','e','c','e','n','t','\0'};
792 static const WCHAR ResourcesW[] = {'R','e','s','o','u','r','c','e','s','\0'};
793 static const WCHAR SendToW[] = {'S','e','n','d','T','o','\0'};
794 static const WCHAR StartUpW[] = {'S','t','a','r','t','U','p','\0'};
795 static const WCHAR Start_MenuW[] = {'S','t','a','r','t',' ','M','e','n','u','\0'};
796 static const WCHAR TemplatesW[] = {'T','e','m','p','l','a','t','e','s','\0'};
797 static const WCHAR DefaultW[] = {'.','D','e','f','a','u','l','t','\0'};
798 static const WCHAR AllUsersProfileW[] = {'%','A','L','L','U','S','E','R','S','P','R','O','F','I','L','E','%','\0'};
799 static const WCHAR UserProfileW[] = {'%','U','S','E','R','P','R','O','F','I','L','E','%','\0'};
800 static const WCHAR SystemDriveW[] = {'%','S','y','s','t','e','m','D','r','i','v','e','%','\0'};
801 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};
802 static const WCHAR ProfilesDirectoryW[] = {'P','r','o','f','i','l','e','s','D','i','r','e','c','t','o','r','y',0};
803 static const WCHAR AllUsersProfileValueW[] = {'A','l','l','U','s','e','r','s','P','r','o','f','i','l','e','\0'};
804 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'};
805 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'};
806 static const WCHAR szDefaultProfileDirW[] = {'u','s','e','r','s',0};
807 static const WCHAR AllUsersW[] = {'P','u','b','l','i','c',0};
809 typedef enum _CSIDL_Type {
813 CSIDL_Type_Disallowed,
814 CSIDL_Type_NonExistent,
815 CSIDL_Type_WindowsPath,
816 CSIDL_Type_SystemPath,
817 CSIDL_Type_SystemX86Path,
824 LPCWSTR szDefaultPath; /* fallback string or resource ID */
827 static const CSIDL_DATA CSIDL_Data[] =
829 { /* 0x00 - CSIDL_DESKTOP */
832 MAKEINTRESOURCEW(IDS_DESKTOPDIRECTORY)
834 { /* 0x01 - CSIDL_INTERNET */
835 CSIDL_Type_Disallowed,
839 { /* 0x02 - CSIDL_PROGRAMS */
842 MAKEINTRESOURCEW(IDS_PROGRAMS)
844 { /* 0x03 - CSIDL_CONTROLS (.CPL files) */
845 CSIDL_Type_SystemPath,
849 { /* 0x04 - CSIDL_PRINTERS */
850 CSIDL_Type_SystemPath,
854 { /* 0x05 - CSIDL_PERSONAL */
857 MAKEINTRESOURCEW(IDS_PERSONAL)
859 { /* 0x06 - CSIDL_FAVORITES */
862 MAKEINTRESOURCEW(IDS_FAVORITES)
864 { /* 0x07 - CSIDL_STARTUP */
867 MAKEINTRESOURCEW(IDS_STARTUP)
869 { /* 0x08 - CSIDL_RECENT */
872 MAKEINTRESOURCEW(IDS_RECENT)
874 { /* 0x09 - CSIDL_SENDTO */
877 MAKEINTRESOURCEW(IDS_SENDTO)
879 { /* 0x0a - CSIDL_BITBUCKET - Recycle Bin */
880 CSIDL_Type_Disallowed,
884 { /* 0x0b - CSIDL_STARTMENU */
887 MAKEINTRESOURCEW(IDS_STARTMENU)
889 { /* 0x0c - CSIDL_MYDOCUMENTS */
890 CSIDL_Type_Disallowed, /* matches WinXP--can't get its path */
894 { /* 0x0d - CSIDL_MYMUSIC */
897 MAKEINTRESOURCEW(IDS_MYMUSIC)
899 { /* 0x0e - CSIDL_MYVIDEO */
902 MAKEINTRESOURCEW(IDS_MYVIDEO)
904 { /* 0x0f - unassigned */
905 CSIDL_Type_Disallowed,
909 { /* 0x10 - CSIDL_DESKTOPDIRECTORY */
912 MAKEINTRESOURCEW(IDS_DESKTOPDIRECTORY)
914 { /* 0x11 - CSIDL_DRIVES */
915 CSIDL_Type_Disallowed,
919 { /* 0x12 - CSIDL_NETWORK */
920 CSIDL_Type_Disallowed,
924 { /* 0x13 - CSIDL_NETHOOD */
927 MAKEINTRESOURCEW(IDS_NETHOOD)
929 { /* 0x14 - CSIDL_FONTS */
930 CSIDL_Type_WindowsPath,
934 { /* 0x15 - CSIDL_TEMPLATES */
937 MAKEINTRESOURCEW(IDS_TEMPLATES)
939 { /* 0x16 - CSIDL_COMMON_STARTMENU */
942 MAKEINTRESOURCEW(IDS_STARTMENU)
944 { /* 0x17 - CSIDL_COMMON_PROGRAMS */
947 MAKEINTRESOURCEW(IDS_PROGRAMS)
949 { /* 0x18 - CSIDL_COMMON_STARTUP */
952 MAKEINTRESOURCEW(IDS_STARTUP)
954 { /* 0x19 - CSIDL_COMMON_DESKTOPDIRECTORY */
957 MAKEINTRESOURCEW(IDS_DESKTOPDIRECTORY)
959 { /* 0x1a - CSIDL_APPDATA */
962 MAKEINTRESOURCEW(IDS_APPDATA)
964 { /* 0x1b - CSIDL_PRINTHOOD */
967 MAKEINTRESOURCEW(IDS_PRINTHOOD)
969 { /* 0x1c - CSIDL_LOCAL_APPDATA */
972 MAKEINTRESOURCEW(IDS_LOCAL_APPDATA)
974 { /* 0x1d - CSIDL_ALTSTARTUP */
975 CSIDL_Type_NonExistent,
979 { /* 0x1e - CSIDL_COMMON_ALTSTARTUP */
980 CSIDL_Type_NonExistent,
984 { /* 0x1f - CSIDL_COMMON_FAVORITES */
987 MAKEINTRESOURCEW(IDS_FAVORITES)
989 { /* 0x20 - CSIDL_INTERNET_CACHE */
992 MAKEINTRESOURCEW(IDS_INTERNET_CACHE)
994 { /* 0x21 - CSIDL_COOKIES */
997 MAKEINTRESOURCEW(IDS_COOKIES)
999 { /* 0x22 - CSIDL_HISTORY */
1002 MAKEINTRESOURCEW(IDS_HISTORY)
1004 { /* 0x23 - CSIDL_COMMON_APPDATA */
1005 CSIDL_Type_AllUsers,
1007 MAKEINTRESOURCEW(IDS_APPDATA)
1009 { /* 0x24 - CSIDL_WINDOWS */
1010 CSIDL_Type_WindowsPath,
1014 { /* 0x25 - CSIDL_SYSTEM */
1015 CSIDL_Type_SystemPath,
1019 { /* 0x26 - CSIDL_PROGRAM_FILES */
1022 MAKEINTRESOURCEW(IDS_PROGRAM_FILES)
1024 { /* 0x27 - CSIDL_MYPICTURES */
1027 MAKEINTRESOURCEW(IDS_MYPICTURES)
1029 { /* 0x28 - CSIDL_PROFILE */
1034 { /* 0x29 - CSIDL_SYSTEMX86 */
1035 CSIDL_Type_SystemX86Path,
1039 { /* 0x2a - CSIDL_PROGRAM_FILESX86 */
1040 CSIDL_Type_NonExistent,
1044 { /* 0x2b - CSIDL_PROGRAM_FILES_COMMON */
1047 MAKEINTRESOURCEW(IDS_PROGRAM_FILES_COMMON)
1049 { /* 0x2c - CSIDL_PROGRAM_FILES_COMMONX86 */
1050 CSIDL_Type_NonExistent,
1054 { /* 0x2d - CSIDL_COMMON_TEMPLATES */
1055 CSIDL_Type_AllUsers,
1057 MAKEINTRESOURCEW(IDS_TEMPLATES)
1059 { /* 0x2e - CSIDL_COMMON_DOCUMENTS */
1060 CSIDL_Type_AllUsers,
1062 MAKEINTRESOURCEW(IDS_COMMON_DOCUMENTS)
1064 { /* 0x2f - CSIDL_COMMON_ADMINTOOLS */
1065 CSIDL_Type_AllUsers,
1066 Common_Administrative_ToolsW,
1067 MAKEINTRESOURCEW(IDS_ADMINTOOLS)
1069 { /* 0x30 - CSIDL_ADMINTOOLS */
1071 Administrative_ToolsW,
1072 MAKEINTRESOURCEW(IDS_ADMINTOOLS)
1074 { /* 0x31 - CSIDL_CONNECTIONS */
1075 CSIDL_Type_Disallowed,
1079 { /* 0x32 - unassigned */
1080 CSIDL_Type_Disallowed,
1084 { /* 0x33 - unassigned */
1085 CSIDL_Type_Disallowed,
1089 { /* 0x34 - unassigned */
1090 CSIDL_Type_Disallowed,
1094 { /* 0x35 - CSIDL_COMMON_MUSIC */
1095 CSIDL_Type_AllUsers,
1097 MAKEINTRESOURCEW(IDS_COMMON_MUSIC)
1099 { /* 0x36 - CSIDL_COMMON_PICTURES */
1100 CSIDL_Type_AllUsers,
1102 MAKEINTRESOURCEW(IDS_COMMON_PICTURES)
1104 { /* 0x37 - CSIDL_COMMON_VIDEO */
1105 CSIDL_Type_AllUsers,
1107 MAKEINTRESOURCEW(IDS_COMMON_VIDEO)
1109 { /* 0x38 - CSIDL_RESOURCES */
1110 CSIDL_Type_WindowsPath,
1114 { /* 0x39 - CSIDL_RESOURCES_LOCALIZED */
1115 CSIDL_Type_NonExistent,
1119 { /* 0x3a - CSIDL_COMMON_OEM_LINKS */
1120 CSIDL_Type_NonExistent,
1124 { /* 0x3b - CSIDL_CDBURN_AREA */
1127 MAKEINTRESOURCEW(IDS_CDBURN_AREA)
1129 { /* 0x3c unassigned */
1130 CSIDL_Type_Disallowed,
1134 { /* 0x3d - CSIDL_COMPUTERSNEARME */
1135 CSIDL_Type_Disallowed, /* FIXME */
1139 { /* 0x3e - CSIDL_PROFILES */
1140 CSIDL_Type_Disallowed, /* oddly, this matches WinXP */
1146 static HRESULT _SHExpandEnvironmentStrings(LPCWSTR szSrc, LPWSTR szDest);
1148 /* Gets the value named value from the registry key
1149 * rootKey\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
1150 * (or from rootKey\userPrefix\... if userPrefix is not NULL) into path, which
1151 * is assumed to be MAX_PATH WCHARs in length.
1152 * If it exists, expands the value and writes the expanded value to
1153 * rootKey\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders
1154 * Returns successful error code if the value was retrieved from the registry,
1155 * and a failure otherwise.
1157 static HRESULT _SHGetUserShellFolderPath(HKEY rootKey, LPCWSTR userPrefix,
1158 LPCWSTR value, LPWSTR path)
1161 WCHAR shellFolderPath[MAX_PATH], userShellFolderPath[MAX_PATH];
1162 LPCWSTR pShellFolderPath, pUserShellFolderPath;
1163 DWORD dwType, dwPathLen = MAX_PATH;
1164 HKEY userShellFolderKey, shellFolderKey;
1166 TRACE("%p,%s,%s,%p\n",rootKey, debugstr_w(userPrefix), debugstr_w(value),
1171 strcpyW(shellFolderPath, userPrefix);
1172 PathAddBackslashW(shellFolderPath);
1173 strcatW(shellFolderPath, szSHFolders);
1174 pShellFolderPath = shellFolderPath;
1175 strcpyW(userShellFolderPath, userPrefix);
1176 PathAddBackslashW(userShellFolderPath);
1177 strcatW(userShellFolderPath, szSHUserFolders);
1178 pUserShellFolderPath = userShellFolderPath;
1182 pUserShellFolderPath = szSHUserFolders;
1183 pShellFolderPath = szSHFolders;
1186 if (RegCreateKeyW(rootKey, pShellFolderPath, &shellFolderKey))
1188 TRACE("Failed to create %s\n", debugstr_w(pShellFolderPath));
1191 if (RegCreateKeyW(rootKey, pUserShellFolderPath, &userShellFolderKey))
1193 TRACE("Failed to create %s\n",
1194 debugstr_w(pUserShellFolderPath));
1195 RegCloseKey(shellFolderKey);
1199 if (!RegQueryValueExW(userShellFolderKey, value, NULL, &dwType,
1200 (LPBYTE)path, &dwPathLen) && (dwType == REG_EXPAND_SZ || dwType == REG_SZ))
1204 path[dwPathLen / sizeof(WCHAR)] = '\0';
1205 if (dwType == REG_EXPAND_SZ && path[0] == '%')
1207 WCHAR szTemp[MAX_PATH];
1209 _SHExpandEnvironmentStrings(path, szTemp);
1210 lstrcpynW(path, szTemp, MAX_PATH);
1212 ret = RegSetValueExW(shellFolderKey, value, 0, REG_SZ, (LPBYTE)path,
1213 (strlenW(path) + 1) * sizeof(WCHAR));
1214 if (ret != ERROR_SUCCESS)
1215 hr = HRESULT_FROM_WIN32(ret);
1221 RegCloseKey(shellFolderKey);
1222 RegCloseKey(userShellFolderKey);
1223 TRACE("returning 0x%08x\n", hr);
1227 /* Gets a 'semi-expanded' default value of the CSIDL with index folder into
1228 * pszPath, based on the entries in CSIDL_Data. By semi-expanded, I mean:
1229 * - The entry's szDefaultPath may be either a string value or an integer
1230 * resource identifier. In the latter case, the string value of the resource
1232 * - Depending on the entry's type, the path may begin with an (unexpanded)
1233 * environment variable name. The caller is responsible for expanding
1234 * environment strings if so desired.
1235 * The types that are prepended with environment variables are:
1236 * CSIDL_Type_User: %USERPROFILE%
1237 * CSIDL_Type_AllUsers: %ALLUSERSPROFILE%
1238 * CSIDL_Type_CurrVer: %SystemDrive%
1239 * (Others might make sense too, but as yet are unneeded.)
1241 static HRESULT _SHGetDefaultValue(BYTE folder, LPWSTR pszPath)
1244 WCHAR resourcePath[MAX_PATH];
1245 LPCWSTR pDefaultPath = NULL;
1247 TRACE("0x%02x,%p\n", folder, pszPath);
1249 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1250 return E_INVALIDARG;
1252 return E_INVALIDARG;
1254 if (CSIDL_Data[folder].szDefaultPath &&
1255 IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath))
1257 if (LoadStringW(shell32_hInstance,
1258 LOWORD(CSIDL_Data[folder].szDefaultPath), resourcePath, MAX_PATH))
1261 pDefaultPath = resourcePath;
1265 FIXME("(%d,%s), LoadString failed, missing translation?\n", folder,
1266 debugstr_w(pszPath));
1273 pDefaultPath = CSIDL_Data[folder].szDefaultPath;
1277 switch (CSIDL_Data[folder].type)
1279 case CSIDL_Type_User:
1280 strcpyW(pszPath, UserProfileW);
1282 case CSIDL_Type_AllUsers:
1283 strcpyW(pszPath, AllUsersProfileW);
1285 case CSIDL_Type_CurrVer:
1286 strcpyW(pszPath, SystemDriveW);
1289 ; /* no corresponding env. var, do nothing */
1293 PathAddBackslashW(pszPath);
1294 strcatW(pszPath, pDefaultPath);
1297 TRACE("returning 0x%08x\n", hr);
1301 /* Gets the (unexpanded) value of the folder with index folder into pszPath.
1302 * The folder's type is assumed to be CSIDL_Type_CurrVer. Its default value
1303 * can be overridden in the HKLM\\szCurrentVersion key.
1304 * If dwFlags has SHGFP_TYPE_DEFAULT set or if the value isn't overridden in
1305 * the registry, uses _SHGetDefaultValue to get the value.
1307 static HRESULT _SHGetCurrentVersionPath(DWORD dwFlags, BYTE folder,
1312 TRACE("0x%08x,0x%02x,%p\n", dwFlags, folder, pszPath);
1314 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1315 return E_INVALIDARG;
1316 if (CSIDL_Data[folder].type != CSIDL_Type_CurrVer)
1317 return E_INVALIDARG;
1319 return E_INVALIDARG;
1321 if (dwFlags & SHGFP_TYPE_DEFAULT)
1322 hr = _SHGetDefaultValue(folder, pszPath);
1327 if (RegCreateKeyW(HKEY_LOCAL_MACHINE, szCurrentVersion, &hKey))
1331 DWORD dwType, dwPathLen = MAX_PATH * sizeof(WCHAR);
1333 if (RegQueryValueExW(hKey, CSIDL_Data[folder].szValueName, NULL,
1334 &dwType, (LPBYTE)pszPath, &dwPathLen) ||
1335 (dwType != REG_SZ && dwType != REG_EXPAND_SZ))
1337 hr = _SHGetDefaultValue(folder, pszPath);
1338 dwType = REG_EXPAND_SZ;
1339 RegSetValueExW(hKey, CSIDL_Data[folder].szValueName, 0, dwType,
1340 (LPBYTE)pszPath, (strlenW(pszPath)+1)*sizeof(WCHAR));
1344 pszPath[dwPathLen / sizeof(WCHAR)] = '\0';
1350 TRACE("returning 0x%08x (output path is %s)\n", hr, debugstr_w(pszPath));
1354 static LPWSTR _GetUserSidStringFromToken(HANDLE Token)
1356 char InfoBuffer[64];
1357 PTOKEN_USER UserInfo;
1361 UserInfo = (PTOKEN_USER) InfoBuffer;
1362 if (! GetTokenInformation(Token, TokenUser, InfoBuffer, sizeof(InfoBuffer),
1365 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
1367 UserInfo = HeapAlloc(GetProcessHeap(), 0, InfoSize);
1368 if (UserInfo == NULL)
1370 if (! GetTokenInformation(Token, TokenUser, UserInfo, InfoSize,
1373 HeapFree(GetProcessHeap(), 0, UserInfo);
1378 if (! ConvertSidToStringSidW(UserInfo->User.Sid, &SidStr))
1381 if (UserInfo != (PTOKEN_USER) InfoBuffer)
1382 HeapFree(GetProcessHeap(), 0, UserInfo);
1387 /* Gets the user's path (unexpanded) for the CSIDL with index folder:
1388 * If SHGFP_TYPE_DEFAULT is set, calls _SHGetDefaultValue for it. Otherwise
1389 * calls _SHGetUserShellFolderPath for it. Where it looks depends on hToken:
1390 * - if hToken is -1, looks in HKEY_USERS\.Default
1391 * - otherwise looks first in HKEY_CURRENT_USER, followed by HKEY_LOCAL_MACHINE
1392 * if HKEY_CURRENT_USER doesn't contain any entries. If both fail, finally
1393 * calls _SHGetDefaultValue for it.
1395 static HRESULT _SHGetUserProfilePath(HANDLE hToken, DWORD dwFlags, BYTE folder,
1400 TRACE("%p,0x%08x,0x%02x,%p\n", hToken, dwFlags, folder, pszPath);
1402 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1403 return E_INVALIDARG;
1404 if (CSIDL_Data[folder].type != CSIDL_Type_User)
1405 return E_INVALIDARG;
1407 return E_INVALIDARG;
1409 if (dwFlags & SHGFP_TYPE_DEFAULT)
1411 if (hToken != NULL && hToken != (HANDLE)-1)
1413 FIXME("unsupported for user other than current or default\n");
1416 hr = _SHGetDefaultValue(folder, pszPath);
1420 LPCWSTR userPrefix = NULL;
1423 if (hToken == (HANDLE)-1)
1425 hRootKey = HKEY_USERS;
1426 userPrefix = DefaultW;
1428 else if (hToken == NULL)
1429 hRootKey = HKEY_CURRENT_USER;
1432 hRootKey = HKEY_USERS;
1433 userPrefix = _GetUserSidStringFromToken(hToken);
1434 if (userPrefix == NULL)
1440 hr = _SHGetUserShellFolderPath(hRootKey, userPrefix,
1441 CSIDL_Data[folder].szValueName, pszPath);
1442 if (FAILED(hr) && hRootKey != HKEY_LOCAL_MACHINE)
1443 hr = _SHGetUserShellFolderPath(HKEY_LOCAL_MACHINE, NULL,
1444 CSIDL_Data[folder].szValueName, pszPath);
1446 hr = _SHGetDefaultValue(folder, pszPath);
1447 if (userPrefix != NULL && userPrefix != DefaultW)
1448 LocalFree((HLOCAL) userPrefix);
1451 TRACE("returning 0x%08x (output path is %s)\n", hr, debugstr_w(pszPath));
1455 /* Gets the (unexpanded) path for the CSIDL with index folder. If dwFlags has
1456 * SHGFP_TYPE_DEFAULT set, calls _SHGetDefaultValue. Otherwise calls
1457 * _SHGetUserShellFolderPath for it, looking only in HKEY_LOCAL_MACHINE.
1458 * If this fails, falls back to _SHGetDefaultValue.
1460 static HRESULT _SHGetAllUsersProfilePath(DWORD dwFlags, BYTE folder,
1465 TRACE("0x%08x,0x%02x,%p\n", dwFlags, folder, pszPath);
1467 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1468 return E_INVALIDARG;
1469 if (CSIDL_Data[folder].type != CSIDL_Type_AllUsers)
1470 return E_INVALIDARG;
1472 return E_INVALIDARG;
1474 if (dwFlags & SHGFP_TYPE_DEFAULT)
1475 hr = _SHGetDefaultValue(folder, pszPath);
1478 hr = _SHGetUserShellFolderPath(HKEY_LOCAL_MACHINE, NULL,
1479 CSIDL_Data[folder].szValueName, pszPath);
1481 hr = _SHGetDefaultValue(folder, pszPath);
1483 TRACE("returning 0x%08x (output path is %s)\n", hr, debugstr_w(pszPath));
1487 static HRESULT _SHOpenProfilesKey(PHKEY pKey)
1492 lRet = RegCreateKeyExW(HKEY_LOCAL_MACHINE, ProfileListW, 0, NULL, 0,
1493 KEY_ALL_ACCESS, NULL, pKey, &disp);
1494 return HRESULT_FROM_WIN32(lRet);
1497 /* Reads the value named szValueName from the key profilesKey (assumed to be
1498 * opened by _SHOpenProfilesKey) into szValue, which is assumed to be MAX_PATH
1499 * WCHARs in length. If it doesn't exist, returns szDefault (and saves
1500 * szDefault to the registry).
1502 static HRESULT _SHGetProfilesValue(HKEY profilesKey, LPCWSTR szValueName,
1503 LPWSTR szValue, LPCWSTR szDefault)
1506 DWORD type, dwPathLen = MAX_PATH * sizeof(WCHAR);
1509 TRACE("%p,%s,%p,%s\n", profilesKey, debugstr_w(szValueName), szValue,
1510 debugstr_w(szDefault));
1511 lRet = RegQueryValueExW(profilesKey, szValueName, NULL, &type,
1512 (LPBYTE)szValue, &dwPathLen);
1513 if (!lRet && (type == REG_SZ || type == REG_EXPAND_SZ) && dwPathLen
1516 dwPathLen /= sizeof(WCHAR);
1517 szValue[dwPathLen] = '\0';
1522 /* Missing or invalid value, set a default */
1523 lstrcpynW(szValue, szDefault, MAX_PATH);
1524 TRACE("Setting missing value %s to %s\n", debugstr_w(szValueName),
1525 debugstr_w(szValue));
1526 lRet = RegSetValueExW(profilesKey, szValueName, 0, REG_EXPAND_SZ,
1528 (strlenW(szValue) + 1) * sizeof(WCHAR));
1530 hr = HRESULT_FROM_WIN32(lRet);
1534 TRACE("returning 0x%08x (output value is %s)\n", hr, debugstr_w(szValue));
1538 /* Attempts to expand environment variables from szSrc into szDest, which is
1539 * assumed to be MAX_PATH characters in length. Before referring to the
1540 * environment, handles a few variables directly, because the environment
1541 * variables may not be set when this is called (as during Wine's installation
1542 * when default values are being written to the registry).
1543 * The directly handled environment variables, and their source, are:
1544 * - ALLUSERSPROFILE, USERPROFILE: reads from the registry
1545 * - SystemDrive: uses GetSystemDirectoryW and uses the drive portion of its
1547 * If one of the directly handled environment variables is expanded, only
1548 * expands a single variable, and only in the beginning of szSrc.
1550 static HRESULT _SHExpandEnvironmentStrings(LPCWSTR szSrc, LPWSTR szDest)
1553 WCHAR szTemp[MAX_PATH], szProfilesPrefix[MAX_PATH] = { 0 };
1556 TRACE("%s, %p\n", debugstr_w(szSrc), szDest);
1558 if (!szSrc || !szDest) return E_INVALIDARG;
1560 /* short-circuit if there's nothing to expand */
1561 if (szSrc[0] != '%')
1563 strcpyW(szDest, szSrc);
1567 /* Get the profile prefix, we'll probably be needing it */
1568 hr = _SHOpenProfilesKey(&key);
1571 WCHAR def_val[MAX_PATH];
1573 /* get the system drive */
1574 GetSystemDirectoryW(def_val, MAX_PATH);
1575 if (def_val[1] == ':') strcpyW( def_val + 3, szDefaultProfileDirW );
1576 else FIXME("non-drive system paths unsupported\n");
1578 hr = _SHGetProfilesValue(key, ProfilesDirectoryW, szProfilesPrefix, def_val );
1582 strcpyW(szTemp, szSrc);
1583 while (SUCCEEDED(hr) && szTemp[0] == '%')
1585 if (!strncmpiW(szTemp, AllUsersProfileW, strlenW(AllUsersProfileW)))
1587 WCHAR szAllUsers[MAX_PATH];
1589 strcpyW(szDest, szProfilesPrefix);
1590 hr = _SHGetProfilesValue(key, AllUsersProfileValueW,
1591 szAllUsers, AllUsersW);
1592 PathAppendW(szDest, szAllUsers);
1593 PathAppendW(szDest, szTemp + strlenW(AllUsersProfileW));
1595 else if (!strncmpiW(szTemp, UserProfileW, strlenW(UserProfileW)))
1597 WCHAR userName[MAX_PATH];
1598 DWORD userLen = MAX_PATH;
1600 strcpyW(szDest, szProfilesPrefix);
1601 GetUserNameW(userName, &userLen);
1602 PathAppendW(szDest, userName);
1603 PathAppendW(szDest, szTemp + strlenW(UserProfileW));
1605 else if (!strncmpiW(szTemp, SystemDriveW, strlenW(SystemDriveW)))
1607 GetSystemDirectoryW(szDest, MAX_PATH);
1608 if (szDest[1] != ':')
1610 FIXME("non-drive system paths unsupported\n");
1615 strcpyW(szDest + 3, szTemp + strlenW(SystemDriveW) + 1);
1621 DWORD ret = ExpandEnvironmentStringsW(szSrc, szDest, MAX_PATH);
1624 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
1626 hr = HRESULT_FROM_WIN32(GetLastError());
1630 if (SUCCEEDED(hr) && szDest[0] == '%')
1631 strcpyW(szTemp, szDest);
1634 /* terminate loop */
1641 TRACE("returning 0x%08x (input was %s, output is %s)\n", hr,
1642 debugstr_w(szSrc), debugstr_w(szDest));
1646 /*************************************************************************
1647 * SHGetFolderPathW [SHELL32.@]
1649 * Convert nFolder to path.
1653 * Failure: standard HRESULT error codes.
1656 * Most values can be overridden in either
1657 * HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
1658 * or in the same location in HKLM.
1659 * The "Shell Folders" registry key was used in NT4 and earlier systems.
1660 * Beginning with Windows 2000, the "User Shell Folders" key is used, so
1661 * changes made to it are made to the former key too. This synchronization is
1662 * done on-demand: not until someone requests the value of one of these paths
1663 * (by calling one of the SHGet functions) is the value synchronized.
1664 * Furthermore, the HKCU paths take precedence over the HKLM paths.
1666 HRESULT WINAPI SHGetFolderPathW(
1667 HWND hwndOwner, /* [I] owner window */
1668 int nFolder, /* [I] CSIDL identifying the folder */
1669 HANDLE hToken, /* [I] access token */
1670 DWORD dwFlags, /* [I] which path to return */
1671 LPWSTR pszPath) /* [O] converted path */
1673 HRESULT hr = SHGetFolderPathAndSubDirW(hwndOwner, nFolder, hToken, dwFlags, NULL, pszPath);
1674 if(HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND) == hr)
1675 hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
1679 HRESULT WINAPI SHGetFolderPathAndSubDirA(
1680 HWND hwndOwner, /* [I] owner window */
1681 int nFolder, /* [I] CSIDL identifying the folder */
1682 HANDLE hToken, /* [I] access token */
1683 DWORD dwFlags, /* [I] which path to return */
1684 LPCSTR pszSubPath, /* [I] sub directory of the specified folder */
1685 LPSTR pszPath) /* [O] converted path */
1689 LPWSTR pszSubPathW = NULL;
1690 LPWSTR pszPathW = NULL;
1691 TRACE("%08x,%08x,%s\n",nFolder, dwFlags, debugstr_w(pszSubPathW));
1694 pszPathW = HeapAlloc(GetProcessHeap(), 0, MAX_PATH * sizeof(WCHAR));
1696 hr = HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
1700 TRACE("%08x,%08x,%s\n",nFolder, dwFlags, debugstr_w(pszSubPathW));
1702 /* SHGetFolderPathAndSubDirW does not distinguish if pszSubPath isn't
1703 * set (null), or an empty string.therefore call it without the parameter set
1704 * if pszSubPath is an empty string
1706 if (pszSubPath && pszSubPath[0]) {
1707 length = MultiByteToWideChar(CP_ACP, 0, pszSubPath, -1, NULL, 0);
1708 pszSubPathW = HeapAlloc(GetProcessHeap(), 0, length * sizeof(WCHAR));
1710 hr = HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
1713 MultiByteToWideChar(CP_ACP, 0, pszSubPath, -1, pszSubPathW, length);
1716 hr = SHGetFolderPathAndSubDirW(hwndOwner, nFolder, hToken, dwFlags, pszSubPathW, pszPathW);
1718 if (SUCCEEDED(hr) && pszPath)
1719 WideCharToMultiByte(CP_ACP, 0, pszPathW, -1, pszPath, MAX_PATH, NULL, NULL);
1722 HeapFree(GetProcessHeap(), 0, pszPathW);
1723 HeapFree(GetProcessHeap(), 0, pszSubPathW);
1727 /*************************************************************************
1728 * SHGetFolderPathAndSubDirW [SHELL32.@]
1730 HRESULT WINAPI SHGetFolderPathAndSubDirW(
1731 HWND hwndOwner, /* [I] owner window */
1732 int nFolder, /* [I] CSIDL identifying the folder */
1733 HANDLE hToken, /* [I] access token */
1734 DWORD dwFlags, /* [I] which path to return */
1735 LPCWSTR pszSubPath,/* [I] sub directory of the specified folder */
1736 LPWSTR pszPath) /* [O] converted path */
1739 WCHAR szBuildPath[MAX_PATH], szTemp[MAX_PATH];
1740 DWORD folder = nFolder & CSIDL_FOLDER_MASK;
1744 TRACE("%p,%p,nFolder=0x%04x,%s\n", hwndOwner,pszPath,nFolder,debugstr_w(pszSubPath));
1746 /* Windows always NULL-terminates the resulting path regardless of success
1747 * or failure, so do so first
1752 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1753 return E_INVALIDARG;
1754 if ((SHGFP_TYPE_CURRENT != dwFlags) && (SHGFP_TYPE_DEFAULT != dwFlags))
1755 return E_INVALIDARG;
1757 type = CSIDL_Data[folder].type;
1760 case CSIDL_Type_Disallowed:
1763 case CSIDL_Type_NonExistent:
1766 case CSIDL_Type_WindowsPath:
1767 GetWindowsDirectoryW(szTemp, MAX_PATH);
1768 if (CSIDL_Data[folder].szDefaultPath &&
1769 !IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath) &&
1770 *CSIDL_Data[folder].szDefaultPath)
1772 PathAddBackslashW(szTemp);
1773 strcatW(szTemp, CSIDL_Data[folder].szDefaultPath);
1777 case CSIDL_Type_SystemPath:
1778 GetSystemDirectoryW(szTemp, MAX_PATH);
1779 if (CSIDL_Data[folder].szDefaultPath &&
1780 !IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath) &&
1781 *CSIDL_Data[folder].szDefaultPath)
1783 PathAddBackslashW(szTemp);
1784 strcatW(szTemp, CSIDL_Data[folder].szDefaultPath);
1788 case CSIDL_Type_SystemX86Path:
1789 if (!GetSystemWow64DirectoryW(szTemp, MAX_PATH)) GetSystemDirectoryW(szTemp, MAX_PATH);
1790 if (CSIDL_Data[folder].szDefaultPath &&
1791 !IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath) &&
1792 *CSIDL_Data[folder].szDefaultPath)
1794 PathAddBackslashW(szTemp);
1795 strcatW(szTemp, CSIDL_Data[folder].szDefaultPath);
1799 case CSIDL_Type_CurrVer:
1800 hr = _SHGetCurrentVersionPath(dwFlags, folder, szTemp);
1802 case CSIDL_Type_User:
1803 hr = _SHGetUserProfilePath(hToken, dwFlags, folder, szTemp);
1805 case CSIDL_Type_AllUsers:
1806 hr = _SHGetAllUsersProfilePath(dwFlags, folder, szTemp);
1809 FIXME("bogus type %d, please fix\n", type);
1814 /* Expand environment strings if necessary */
1816 hr = _SHExpandEnvironmentStrings(szTemp, szBuildPath);
1818 strcpyW(szBuildPath, szTemp);
1820 if (FAILED(hr)) goto end;
1823 /* make sure the new path does not exceed th bufferlength
1824 * rememebr to backslash and the termination */
1825 if(MAX_PATH < (lstrlenW(szBuildPath) + lstrlenW(pszSubPath) + 2)) {
1826 hr = HRESULT_FROM_WIN32(ERROR_FILENAME_EXCED_RANGE);
1829 PathAppendW(szBuildPath, pszSubPath);
1830 PathRemoveBackslashW(szBuildPath);
1832 /* Copy the path if it's available before we might return */
1833 if (SUCCEEDED(hr) && pszPath)
1834 strcpyW(pszPath, szBuildPath);
1836 /* if we don't care about existing directories we are ready */
1837 if(nFolder & CSIDL_FLAG_DONT_VERIFY) goto end;
1839 if (PathFileExistsW(szBuildPath)) goto end;
1841 /* not existing but we are not allowed to create it. The return value
1842 * is verified against shell32 version 6.0.
1844 if (!(nFolder & CSIDL_FLAG_CREATE))
1846 hr = HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND);
1850 /* create directory/directories */
1851 ret = SHCreateDirectoryExW(hwndOwner, szBuildPath, NULL);
1852 if (ret && ret != ERROR_ALREADY_EXISTS)
1854 ERR("Failed to create directory %s.\n", debugstr_w(szBuildPath));
1859 TRACE("Created missing system directory %s\n", debugstr_w(szBuildPath));
1861 TRACE("returning 0x%08x (final path is %s)\n", hr, debugstr_w(szBuildPath));
1865 /*************************************************************************
1866 * SHGetFolderPathA [SHELL32.@]
1868 * See SHGetFolderPathW.
1870 HRESULT WINAPI SHGetFolderPathA(
1877 WCHAR szTemp[MAX_PATH];
1880 TRACE("%p,%p,nFolder=0x%04x\n",hwndOwner,pszPath,nFolder);
1884 hr = SHGetFolderPathW(hwndOwner, nFolder, hToken, dwFlags, szTemp);
1885 if (SUCCEEDED(hr) && pszPath)
1886 WideCharToMultiByte(CP_ACP, 0, szTemp, -1, pszPath, MAX_PATH, NULL,
1892 /* For each folder in folders, if its value has not been set in the registry,
1893 * calls _SHGetUserProfilePath or _SHGetAllUsersProfilePath (depending on the
1894 * folder's type) to get the unexpanded value first.
1895 * Writes the unexpanded value to User Shell Folders, and queries it with
1896 * SHGetFolderPathW to force the creation of the directory if it doesn't
1897 * already exist. SHGetFolderPathW also returns the expanded value, which
1898 * this then writes to Shell Folders.
1900 static HRESULT _SHRegisterFolders(HKEY hRootKey, HANDLE hToken,
1901 LPCWSTR szUserShellFolderPath, LPCWSTR szShellFolderPath, const UINT folders[],
1905 WCHAR path[MAX_PATH];
1907 HKEY hUserKey = NULL, hKey = NULL;
1908 DWORD dwType, dwPathLen;
1911 TRACE("%p,%p,%s,%p,%u\n", hRootKey, hToken,
1912 debugstr_w(szUserShellFolderPath), folders, foldersLen);
1914 ret = RegCreateKeyW(hRootKey, szUserShellFolderPath, &hUserKey);
1916 hr = HRESULT_FROM_WIN32(ret);
1919 ret = RegCreateKeyW(hRootKey, szShellFolderPath, &hKey);
1921 hr = HRESULT_FROM_WIN32(ret);
1923 for (i = 0; SUCCEEDED(hr) && i < foldersLen; i++)
1925 dwPathLen = MAX_PATH * sizeof(WCHAR);
1926 if (RegQueryValueExW(hUserKey, CSIDL_Data[folders[i]].szValueName, NULL,
1927 &dwType, (LPBYTE)path, &dwPathLen) || (dwType != REG_SZ &&
1928 dwType != REG_EXPAND_SZ))
1931 if (CSIDL_Data[folders[i]].type == CSIDL_Type_User)
1932 _SHGetUserProfilePath(hToken, SHGFP_TYPE_DEFAULT, folders[i],
1934 else if (CSIDL_Data[folders[i]].type == CSIDL_Type_AllUsers)
1935 _SHGetAllUsersProfilePath(SHGFP_TYPE_DEFAULT, folders[i], path);
1936 else if (CSIDL_Data[folders[i]].type == CSIDL_Type_WindowsPath)
1938 GetWindowsDirectoryW(path, MAX_PATH);
1939 if (CSIDL_Data[folders[i]].szDefaultPath &&
1940 !IS_INTRESOURCE(CSIDL_Data[folders[i]].szDefaultPath))
1942 PathAddBackslashW(path);
1943 strcatW(path, CSIDL_Data[folders[i]].szDefaultPath);
1950 ret = RegSetValueExW(hUserKey,
1951 CSIDL_Data[folders[i]].szValueName, 0, REG_EXPAND_SZ,
1952 (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR));
1954 hr = HRESULT_FROM_WIN32(ret);
1957 hr = SHGetFolderPathW(NULL, folders[i] | CSIDL_FLAG_CREATE,
1958 hToken, SHGFP_TYPE_DEFAULT, path);
1959 ret = RegSetValueExW(hKey,
1960 CSIDL_Data[folders[i]].szValueName, 0, REG_SZ,
1961 (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR));
1963 hr = HRESULT_FROM_WIN32(ret);
1969 RegCloseKey(hUserKey);
1973 TRACE("returning 0x%08x\n", hr);
1977 static HRESULT _SHRegisterUserShellFolders(BOOL bDefault)
1979 static const UINT folders[] = {
1990 CSIDL_DESKTOPDIRECTORY,
1994 CSIDL_LOCAL_APPDATA,
1995 CSIDL_INTERNET_CACHE,
2001 WCHAR userShellFolderPath[MAX_PATH], shellFolderPath[MAX_PATH];
2002 LPCWSTR pUserShellFolderPath, pShellFolderPath;
2007 TRACE("%s\n", bDefault ? "TRUE" : "FALSE");
2010 hToken = (HANDLE)-1;
2011 hRootKey = HKEY_USERS;
2012 strcpyW(userShellFolderPath, DefaultW);
2013 PathAddBackslashW(userShellFolderPath);
2014 strcatW(userShellFolderPath, szSHUserFolders);
2015 pUserShellFolderPath = userShellFolderPath;
2016 strcpyW(shellFolderPath, DefaultW);
2017 PathAddBackslashW(shellFolderPath);
2018 strcatW(shellFolderPath, szSHFolders);
2019 pShellFolderPath = shellFolderPath;
2024 hRootKey = HKEY_CURRENT_USER;
2025 pUserShellFolderPath = szSHUserFolders;
2026 pShellFolderPath = szSHFolders;
2029 hr = _SHRegisterFolders(hRootKey, hToken, pUserShellFolderPath,
2030 pShellFolderPath, folders, sizeof(folders) / sizeof(folders[0]));
2031 TRACE("returning 0x%08x\n", hr);
2035 static HRESULT _SHRegisterCommonShellFolders(void)
2037 static const UINT folders[] = {
2038 CSIDL_COMMON_STARTMENU,
2039 CSIDL_COMMON_PROGRAMS,
2040 CSIDL_COMMON_STARTUP,
2041 CSIDL_COMMON_DESKTOPDIRECTORY,
2042 CSIDL_COMMON_FAVORITES,
2043 CSIDL_COMMON_APPDATA,
2044 CSIDL_COMMON_TEMPLATES,
2045 CSIDL_COMMON_DOCUMENTS,
2046 CSIDL_COMMON_ADMINTOOLS,
2048 CSIDL_COMMON_PICTURES,
2054 hr = _SHRegisterFolders(HKEY_LOCAL_MACHINE, NULL, szSHUserFolders,
2055 szSHFolders, folders, sizeof(folders) / sizeof(folders[0]));
2056 TRACE("returning 0x%08x\n", hr);
2060 /******************************************************************************
2061 * _SHAppendToUnixPath [Internal]
2063 * Helper function for _SHCreateSymbolicLinks. Appends pwszSubPath (or the
2064 * corresponding resource, if IS_INTRESOURCE) to the unix base path 'szBasePath'
2065 * and replaces backslashes with slashes.
2068 * szBasePath [IO] The unix base path, which will be appended to (CP_UNXICP).
2069 * pwszSubPath [I] Sub-path or resource id (use MAKEINTRESOURCEW).
2075 static inline BOOL _SHAppendToUnixPath(char *szBasePath, LPCWSTR pwszSubPath) {
2076 WCHAR wszSubPath[MAX_PATH];
2077 int cLen = strlen(szBasePath);
2080 if (IS_INTRESOURCE(pwszSubPath)) {
2081 if (!LoadStringW(shell32_hInstance, LOWORD(pwszSubPath), wszSubPath, MAX_PATH)) {
2082 /* Fall back to hard coded defaults. */
2083 switch (LOWORD(pwszSubPath)) {
2085 lstrcpyW(wszSubPath, PersonalW);
2088 lstrcpyW(wszSubPath, My_MusicW);
2090 case IDS_MYPICTURES:
2091 lstrcpyW(wszSubPath, My_PicturesW);
2094 lstrcpyW(wszSubPath, My_VideoW);
2097 ERR("LoadString(%d) failed!\n", LOWORD(pwszSubPath));
2102 lstrcpyW(wszSubPath, pwszSubPath);
2105 if (szBasePath[cLen-1] != '/') szBasePath[cLen++] = '/';
2107 if (!WideCharToMultiByte(CP_UNIXCP, 0, wszSubPath, -1, szBasePath + cLen,
2108 FILENAME_MAX - cLen, NULL, NULL))
2113 pBackslash = szBasePath + cLen;
2114 while ((pBackslash = strchr(pBackslash, '\\'))) *pBackslash = '/';
2119 /******************************************************************************
2120 * _SHCreateSymbolicLinks [Internal]
2122 * Sets up symbol links for various shell folders to point into the users home
2123 * directory. We do an educated guess about what the user would probably want:
2124 * - If there is a 'My Documents' directory in $HOME, the user probably wants
2125 * wine's 'My Documents' to point there. Furthermore, we imply that the user
2126 * is a Windows lover and has no problem with wine creating 'My Pictures',
2127 * 'My Music' and 'My Video' subfolders under '$HOME/My Documents', if those
2128 * do not already exits. We put appropriate symbolic links in place for those,
2130 * - If there is no 'My Documents' directory in $HOME, we let 'My Documents'
2131 * point directly to $HOME. We assume the user to be a unix hacker who does not
2132 * want wine to create anything anywhere besides the .wine directory. So, if
2133 * there already is a 'My Music' directory in $HOME, we symlink the 'My Music'
2134 * shell folder to it. But if not, then we check XDG_MUSIC_DIR - "well known"
2135 * directory, and try to link to that. If that fails, then we symlink to
2136 * $HOME directly. The same holds fo 'My Pictures' and 'My Video'.
2137 * - The Desktop shell folder is symlinked to XDG_DESKTOP_DIR. If that does not
2138 * exist, then we try '$HOME/Desktop'. If that does not exist, then we leave
2140 * ('My Music',... above in fact means LoadString(IDS_MYMUSIC))
2142 static void _SHCreateSymbolicLinks(void)
2144 UINT aidsMyStuff[] = { IDS_MYPICTURES, IDS_MYVIDEO, IDS_MYMUSIC }, i;
2145 int acsidlMyStuff[] = { CSIDL_MYPICTURES, CSIDL_MYVIDEO, CSIDL_MYMUSIC };
2146 static const char * const xdg_dirs[] = { "PICTURES", "VIDEOS", "MUSIC", "DESKTOP" };
2147 static const unsigned int num = sizeof(xdg_dirs) / sizeof(xdg_dirs[0]);
2148 WCHAR wszTempPath[MAX_PATH];
2149 char szPersonalTarget[FILENAME_MAX], *pszPersonal;
2150 char szMyStuffTarget[FILENAME_MAX], *pszMyStuff;
2151 char szDesktopTarget[FILENAME_MAX], *pszDesktop;
2152 struct stat statFolder;
2153 const char *pszHome;
2155 char ** xdg_results;
2156 char * xdg_desktop_dir;
2158 /* Create all necessary profile sub-dirs up to 'My Documents' and get the unix path. */
2159 hr = SHGetFolderPathW(NULL, CSIDL_PERSONAL|CSIDL_FLAG_CREATE, NULL,
2160 SHGFP_TYPE_DEFAULT, wszTempPath);
2161 if (FAILED(hr)) return;
2162 pszPersonal = wine_get_unix_file_name(wszTempPath);
2163 if (!pszPersonal) return;
2165 hr = XDG_UserDirLookup(xdg_dirs, num, &xdg_results);
2166 if (FAILED(hr)) xdg_results = NULL;
2168 pszHome = getenv("HOME");
2169 if (pszHome && !stat(pszHome, &statFolder) && S_ISDIR(statFolder.st_mode)) {
2170 strcpy(szPersonalTarget, pszHome);
2171 if (_SHAppendToUnixPath(szPersonalTarget, MAKEINTRESOURCEW(IDS_PERSONAL)) &&
2172 !stat(szPersonalTarget, &statFolder) && S_ISDIR(statFolder.st_mode))
2174 /* '$HOME/My Documents' exists. Create 'My Pictures', 'My Videos' and
2175 * 'My Music' subfolders or fail silently if they already exist. */
2176 for (i = 0; i < sizeof(aidsMyStuff)/sizeof(aidsMyStuff[0]); i++) {
2177 strcpy(szMyStuffTarget, szPersonalTarget);
2178 if (_SHAppendToUnixPath(szMyStuffTarget, MAKEINTRESOURCEW(aidsMyStuff[i])))
2179 mkdir(szMyStuffTarget, 0777);
2184 /* '$HOME/My Documents' doesn't exists, but '$HOME' does. */
2185 strcpy(szPersonalTarget, pszHome);
2188 /* Replace 'My Documents' directory with a symlink of fail silently if not empty. */
2190 symlink(szPersonalTarget, pszPersonal);
2194 /* '$HOME' doesn't exist. Create 'My Pictures', 'My Videos' and 'My Music' subdirs
2195 * in '%USERPROFILE%\\My Documents' or fail silently if they already exist. */
2196 strcpy(szPersonalTarget, pszPersonal);
2197 for (i = 0; i < sizeof(aidsMyStuff)/sizeof(aidsMyStuff[0]); i++) {
2198 strcpy(szMyStuffTarget, szPersonalTarget);
2199 if (_SHAppendToUnixPath(szMyStuffTarget, MAKEINTRESOURCEW(aidsMyStuff[i])))
2200 mkdir(szMyStuffTarget, 0777);
2204 /* Create symbolic links for 'My Pictures', 'My Video' and 'My Music'. */
2205 for (i=0; i < sizeof(aidsMyStuff)/sizeof(aidsMyStuff[0]); i++) {
2206 /* Create the current 'My Whatever' folder and get it's unix path. */
2207 hr = SHGetFolderPathW(NULL, acsidlMyStuff[i]|CSIDL_FLAG_CREATE, NULL,
2208 SHGFP_TYPE_DEFAULT, wszTempPath);
2209 if (FAILED(hr)) continue;
2210 pszMyStuff = wine_get_unix_file_name(wszTempPath);
2211 if (!pszMyStuff) continue;
2213 strcpy(szMyStuffTarget, szPersonalTarget);
2214 if (_SHAppendToUnixPath(szMyStuffTarget, MAKEINTRESOURCEW(aidsMyStuff[i])) &&
2215 !stat(szMyStuffTarget, &statFolder) && S_ISDIR(statFolder.st_mode))
2217 /* If there's a 'My Whatever' directory where 'My Documents' links to, link to it. */
2219 symlink(szMyStuffTarget, pszMyStuff);
2224 if (xdg_results && xdg_results[i])
2226 /* the folder specified by XDG_XXX_DIR exists, link to it. */
2227 symlink(xdg_results[i], pszMyStuff);
2231 /* Else link to where 'My Documents' itself links to. */
2232 symlink(szPersonalTarget, pszMyStuff);
2235 HeapFree(GetProcessHeap(), 0, pszMyStuff);
2238 /* Last but not least, the Desktop folder */
2240 strcpy(szDesktopTarget, pszHome);
2242 strcpy(szDesktopTarget, pszPersonal);
2243 HeapFree(GetProcessHeap(), 0, pszPersonal);
2245 xdg_desktop_dir = xdg_results ? xdg_results[num - 1] : NULL;
2246 if (xdg_desktop_dir ||
2247 (_SHAppendToUnixPath(szDesktopTarget, DesktopW) &&
2248 !stat(szDesktopTarget, &statFolder) && S_ISDIR(statFolder.st_mode)))
2250 hr = SHGetFolderPathW(NULL, CSIDL_DESKTOPDIRECTORY|CSIDL_FLAG_CREATE, NULL,
2251 SHGFP_TYPE_DEFAULT, wszTempPath);
2252 if (SUCCEEDED(hr) && (pszDesktop = wine_get_unix_file_name(wszTempPath)))
2255 if (xdg_desktop_dir)
2256 symlink(xdg_desktop_dir, pszDesktop);
2258 symlink(szDesktopTarget, pszDesktop);
2259 HeapFree(GetProcessHeap(), 0, pszDesktop);
2263 /* Free resources allocated by XDG_UserDirLookup() */
2266 for (i = 0; i < num; i++)
2267 HeapFree(GetProcessHeap(), 0, xdg_results[i]);
2268 HeapFree(GetProcessHeap(), 0, xdg_results);
2272 /******************************************************************************
2273 * create_extra_folders [Internal]
2275 * Create some extra folders that don't have a standard CSIDL definition.
2277 static HRESULT create_extra_folders(void)
2279 static const WCHAR environW[] = {'E','n','v','i','r','o','n','m','e','n','t',0};
2280 static const WCHAR TempW[] = {'T','e','m','p',0};
2281 static const WCHAR TEMPW[] = {'T','E','M','P',0};
2282 static const WCHAR TMPW[] = {'T','M','P',0};
2283 WCHAR path[MAX_PATH+5];
2286 DWORD type, size, ret;
2288 ret = RegCreateKeyW( HKEY_CURRENT_USER, environW, &hkey );
2289 if (ret) return HRESULT_FROM_WIN32( ret );
2291 /* FIXME: should be under AppData, but we don't want spaces in the temp path */
2292 hr = SHGetFolderPathAndSubDirW( 0, CSIDL_PROFILE | CSIDL_FLAG_CREATE, NULL,
2293 SHGFP_TYPE_DEFAULT, TempW, path );
2296 size = sizeof(path);
2297 if (RegQueryValueExW( hkey, TEMPW, NULL, &type, (LPBYTE)path, &size ))
2298 RegSetValueExW( hkey, TEMPW, 0, REG_SZ, (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR) );
2299 size = sizeof(path);
2300 if (RegQueryValueExW( hkey, TMPW, NULL, &type, (LPBYTE)path, &size ))
2301 RegSetValueExW( hkey, TMPW, 0, REG_SZ, (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR) );
2303 RegCloseKey( hkey );
2308 /* Register the default values in the registry, as some apps seem to depend
2309 * on their presence. The set registered was taken from Windows XP.
2311 HRESULT SHELL_RegisterShellFolders(void)
2315 /* Set up '$HOME' targeted symlinks for 'My Documents', 'My Pictures',
2316 * 'My Video', 'My Music' and 'Desktop' in advance, so that the
2317 * _SHRegister*ShellFolders() functions will find everything nice and clean
2318 * and thus will not attempt to create them in the profile directory. */
2319 _SHCreateSymbolicLinks();
2321 hr = _SHRegisterUserShellFolders(TRUE);
2323 hr = _SHRegisterUserShellFolders(FALSE);
2325 hr = _SHRegisterCommonShellFolders();
2327 hr = create_extra_folders();
2331 /*************************************************************************
2332 * SHGetSpecialFolderPathA [SHELL32.@]
2334 BOOL WINAPI SHGetSpecialFolderPathA (
2340 return (SHGetFolderPathA(
2342 nFolder + (bCreate ? CSIDL_FLAG_CREATE : 0),
2345 szPath)) == S_OK ? TRUE : FALSE;
2348 /*************************************************************************
2349 * SHGetSpecialFolderPathW
2351 BOOL WINAPI SHGetSpecialFolderPathW (
2357 return (SHGetFolderPathW(
2359 nFolder + (bCreate ? CSIDL_FLAG_CREATE : 0),
2362 szPath)) == S_OK ? TRUE : FALSE;
2365 /*************************************************************************
2366 * SHGetSpecialFolderPath (SHELL32.175)
2368 BOOL WINAPI SHGetSpecialFolderPathAW (
2375 if (SHELL_OsIsUnicode())
2376 return SHGetSpecialFolderPathW (hwndOwner, szPath, nFolder, bCreate);
2377 return SHGetSpecialFolderPathA (hwndOwner, szPath, nFolder, bCreate);
2380 /*************************************************************************
2381 * SHGetFolderLocation [SHELL32.@]
2383 * Gets the folder locations from the registry and creates a pidl.
2387 * nFolder [I] CSIDL_xxxxx
2388 * hToken [I] token representing user, or NULL for current user, or -1 for
2390 * dwReserved [I] must be zero
2391 * ppidl [O] PIDL of a special folder
2395 * Failure: Standard OLE-defined error result, S_FALSE or E_INVALIDARG
2398 * Creates missing reg keys and directories.
2399 * Mostly forwards to SHGetFolderPathW, but a few values of nFolder return
2400 * virtual folders that are handled here.
2402 HRESULT WINAPI SHGetFolderLocation(
2407 LPITEMIDLIST *ppidl)
2409 HRESULT hr = E_INVALIDARG;
2411 TRACE("%p 0x%08x %p 0x%08x %p\n",
2412 hwndOwner, nFolder, hToken, dwReserved, ppidl);
2415 return E_INVALIDARG;
2417 return E_INVALIDARG;
2419 /* The virtual folders' locations are not user-dependent */
2421 switch (nFolder & CSIDL_FOLDER_MASK)
2424 *ppidl = _ILCreateDesktop();
2427 case CSIDL_PERSONAL:
2428 *ppidl = _ILCreateMyDocuments();
2431 case CSIDL_INTERNET:
2432 *ppidl = _ILCreateIExplore();
2435 case CSIDL_CONTROLS:
2436 *ppidl = _ILCreateControlPanel();
2439 case CSIDL_PRINTERS:
2440 *ppidl = _ILCreatePrinters();
2443 case CSIDL_BITBUCKET:
2444 *ppidl = _ILCreateBitBucket();
2448 *ppidl = _ILCreateMyComputer();
2452 *ppidl = _ILCreateNetwork();
2457 WCHAR szPath[MAX_PATH];
2459 hr = SHGetFolderPathW(hwndOwner, nFolder, hToken,
2460 SHGFP_TYPE_CURRENT, szPath);
2465 TRACE("Value=%s\n", debugstr_w(szPath));
2466 hr = SHILCreateFromPathW(szPath, ppidl, &attributes);
2468 else if (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
2470 /* unlike SHGetFolderPath, SHGetFolderLocation in shell32
2471 * version 6.0 returns E_FAIL for nonexistent paths
2480 TRACE("-- (new pidl %p)\n",*ppidl);
2484 /*************************************************************************
2485 * SHGetSpecialFolderLocation [SHELL32.@]
2488 * In NT5, SHGetSpecialFolderLocation needs the <winntdir>/Recent
2491 HRESULT WINAPI SHGetSpecialFolderLocation(
2494 LPITEMIDLIST * ppidl)
2496 HRESULT hr = E_INVALIDARG;
2498 TRACE("(%p,0x%x,%p)\n", hwndOwner,nFolder,ppidl);
2501 return E_INVALIDARG;
2503 hr = SHGetFolderLocation(hwndOwner, nFolder, NULL, 0, ppidl);
2507 /*************************************************************************
2508 * SHGetKnownFolderPath [SHELL32.@]
2510 HRESULT WINAPI SHGetKnownFolderPath(REFKNOWNFOLDERID rfid, DWORD flags, HANDLE token, PWSTR *path)
2512 FIXME("(%s, %d, %p, %p) stub!\n", debugstr_guid(rfid), flags, token, path);