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 CommonFilesDirX86W[] = {'C','o','m','m','o','n','F','i','l','e','s','D','i','r',' ','(','x','8','6',')','\0'};
771 static const WCHAR CommonMusicW[] = {'C','o','m','m','o','n','M','u','s','i','c','\0'};
772 static const WCHAR CommonPicturesW[] = {'C','o','m','m','o','n','P','i','c','t','u','r','e','s','\0'};
773 static const WCHAR Common_ProgramsW[] = {'C','o','m','m','o','n',' ','P','r','o','g','r','a','m','s','\0'};
774 static const WCHAR Common_StartUpW[] = {'C','o','m','m','o','n',' ','S','t','a','r','t','U','p','\0'};
775 static const WCHAR Common_Start_MenuW[] = {'C','o','m','m','o','n',' ','S','t','a','r','t',' ','M','e','n','u','\0'};
776 static const WCHAR Common_TemplatesW[] = {'C','o','m','m','o','n',' ','T','e','m','p','l','a','t','e','s','\0'};
777 static const WCHAR CommonVideoW[] = {'C','o','m','m','o','n','V','i','d','e','o','\0'};
778 static const WCHAR CookiesW[] = {'C','o','o','k','i','e','s','\0'};
779 static const WCHAR DesktopW[] = {'D','e','s','k','t','o','p','\0'};
780 static const WCHAR FavoritesW[] = {'F','a','v','o','r','i','t','e','s','\0'};
781 static const WCHAR FontsW[] = {'F','o','n','t','s','\0'};
782 static const WCHAR HistoryW[] = {'H','i','s','t','o','r','y','\0'};
783 static const WCHAR Local_AppDataW[] = {'L','o','c','a','l',' ','A','p','p','D','a','t','a','\0'};
784 static const WCHAR My_MusicW[] = {'M','y',' ','M','u','s','i','c','\0'};
785 static const WCHAR My_PicturesW[] = {'M','y',' ','P','i','c','t','u','r','e','s','\0'};
786 static const WCHAR My_VideoW[] = {'M','y',' ','V','i','d','e','o','s','\0'};
787 static const WCHAR NetHoodW[] = {'N','e','t','H','o','o','d','\0'};
788 static const WCHAR PersonalW[] = {'P','e','r','s','o','n','a','l','\0'};
789 static const WCHAR PrintHoodW[] = {'P','r','i','n','t','H','o','o','d','\0'};
790 static const WCHAR ProgramFilesDirW[] = {'P','r','o','g','r','a','m','F','i','l','e','s','D','i','r','\0'};
791 static const WCHAR ProgramFilesDirX86W[] = {'P','r','o','g','r','a','m','F','i','l','e','s','D','i','r',' ','(','x','8','6',')','\0'};
792 static const WCHAR ProgramsW[] = {'P','r','o','g','r','a','m','s','\0'};
793 static const WCHAR RecentW[] = {'R','e','c','e','n','t','\0'};
794 static const WCHAR ResourcesW[] = {'R','e','s','o','u','r','c','e','s','\0'};
795 static const WCHAR SendToW[] = {'S','e','n','d','T','o','\0'};
796 static const WCHAR StartUpW[] = {'S','t','a','r','t','U','p','\0'};
797 static const WCHAR Start_MenuW[] = {'S','t','a','r','t',' ','M','e','n','u','\0'};
798 static const WCHAR TemplatesW[] = {'T','e','m','p','l','a','t','e','s','\0'};
799 static const WCHAR DefaultW[] = {'.','D','e','f','a','u','l','t','\0'};
800 static const WCHAR AllUsersProfileW[] = {'%','A','L','L','U','S','E','R','S','P','R','O','F','I','L','E','%','\0'};
801 static const WCHAR UserProfileW[] = {'%','U','S','E','R','P','R','O','F','I','L','E','%','\0'};
802 static const WCHAR SystemDriveW[] = {'%','S','y','s','t','e','m','D','r','i','v','e','%','\0'};
803 static const WCHAR ProfileListW[] = {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','P','r','o','f','i','l','e','L','i','s','t',0};
804 static const WCHAR ProfilesDirectoryW[] = {'P','r','o','f','i','l','e','s','D','i','r','e','c','t','o','r','y',0};
805 static const WCHAR AllUsersProfileValueW[] = {'A','l','l','U','s','e','r','s','P','r','o','f','i','l','e','\0'};
806 static const WCHAR szSHFolders[] = {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','E','x','p','l','o','r','e','r','\\','S','h','e','l','l',' ','F','o','l','d','e','r','s','\0'};
807 static const WCHAR szSHUserFolders[] = {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','E','x','p','l','o','r','e','r','\\','U','s','e','r',' ','S','h','e','l','l',' ','F','o','l','d','e','r','s','\0'};
808 static const WCHAR szDefaultProfileDirW[] = {'u','s','e','r','s',0};
809 static const WCHAR AllUsersW[] = {'P','u','b','l','i','c',0};
811 typedef enum _CSIDL_Type {
815 CSIDL_Type_Disallowed,
816 CSIDL_Type_NonExistent,
817 CSIDL_Type_WindowsPath,
818 CSIDL_Type_SystemPath,
819 CSIDL_Type_SystemX86Path,
826 LPCWSTR szDefaultPath; /* fallback string or resource ID */
829 static const CSIDL_DATA CSIDL_Data[] =
831 { /* 0x00 - CSIDL_DESKTOP */
834 MAKEINTRESOURCEW(IDS_DESKTOPDIRECTORY)
836 { /* 0x01 - CSIDL_INTERNET */
837 CSIDL_Type_Disallowed,
841 { /* 0x02 - CSIDL_PROGRAMS */
844 MAKEINTRESOURCEW(IDS_PROGRAMS)
846 { /* 0x03 - CSIDL_CONTROLS (.CPL files) */
847 CSIDL_Type_SystemPath,
851 { /* 0x04 - CSIDL_PRINTERS */
852 CSIDL_Type_SystemPath,
856 { /* 0x05 - CSIDL_PERSONAL */
859 MAKEINTRESOURCEW(IDS_PERSONAL)
861 { /* 0x06 - CSIDL_FAVORITES */
864 MAKEINTRESOURCEW(IDS_FAVORITES)
866 { /* 0x07 - CSIDL_STARTUP */
869 MAKEINTRESOURCEW(IDS_STARTUP)
871 { /* 0x08 - CSIDL_RECENT */
874 MAKEINTRESOURCEW(IDS_RECENT)
876 { /* 0x09 - CSIDL_SENDTO */
879 MAKEINTRESOURCEW(IDS_SENDTO)
881 { /* 0x0a - CSIDL_BITBUCKET - Recycle Bin */
882 CSIDL_Type_Disallowed,
886 { /* 0x0b - CSIDL_STARTMENU */
889 MAKEINTRESOURCEW(IDS_STARTMENU)
891 { /* 0x0c - CSIDL_MYDOCUMENTS */
892 CSIDL_Type_Disallowed, /* matches WinXP--can't get its path */
896 { /* 0x0d - CSIDL_MYMUSIC */
899 MAKEINTRESOURCEW(IDS_MYMUSIC)
901 { /* 0x0e - CSIDL_MYVIDEO */
904 MAKEINTRESOURCEW(IDS_MYVIDEO)
906 { /* 0x0f - unassigned */
907 CSIDL_Type_Disallowed,
911 { /* 0x10 - CSIDL_DESKTOPDIRECTORY */
914 MAKEINTRESOURCEW(IDS_DESKTOPDIRECTORY)
916 { /* 0x11 - CSIDL_DRIVES */
917 CSIDL_Type_Disallowed,
921 { /* 0x12 - CSIDL_NETWORK */
922 CSIDL_Type_Disallowed,
926 { /* 0x13 - CSIDL_NETHOOD */
929 MAKEINTRESOURCEW(IDS_NETHOOD)
931 { /* 0x14 - CSIDL_FONTS */
932 CSIDL_Type_WindowsPath,
936 { /* 0x15 - CSIDL_TEMPLATES */
939 MAKEINTRESOURCEW(IDS_TEMPLATES)
941 { /* 0x16 - CSIDL_COMMON_STARTMENU */
944 MAKEINTRESOURCEW(IDS_STARTMENU)
946 { /* 0x17 - CSIDL_COMMON_PROGRAMS */
949 MAKEINTRESOURCEW(IDS_PROGRAMS)
951 { /* 0x18 - CSIDL_COMMON_STARTUP */
954 MAKEINTRESOURCEW(IDS_STARTUP)
956 { /* 0x19 - CSIDL_COMMON_DESKTOPDIRECTORY */
959 MAKEINTRESOURCEW(IDS_DESKTOPDIRECTORY)
961 { /* 0x1a - CSIDL_APPDATA */
964 MAKEINTRESOURCEW(IDS_APPDATA)
966 { /* 0x1b - CSIDL_PRINTHOOD */
969 MAKEINTRESOURCEW(IDS_PRINTHOOD)
971 { /* 0x1c - CSIDL_LOCAL_APPDATA */
974 MAKEINTRESOURCEW(IDS_LOCAL_APPDATA)
976 { /* 0x1d - CSIDL_ALTSTARTUP */
977 CSIDL_Type_NonExistent,
981 { /* 0x1e - CSIDL_COMMON_ALTSTARTUP */
982 CSIDL_Type_NonExistent,
986 { /* 0x1f - CSIDL_COMMON_FAVORITES */
989 MAKEINTRESOURCEW(IDS_FAVORITES)
991 { /* 0x20 - CSIDL_INTERNET_CACHE */
994 MAKEINTRESOURCEW(IDS_INTERNET_CACHE)
996 { /* 0x21 - CSIDL_COOKIES */
999 MAKEINTRESOURCEW(IDS_COOKIES)
1001 { /* 0x22 - CSIDL_HISTORY */
1004 MAKEINTRESOURCEW(IDS_HISTORY)
1006 { /* 0x23 - CSIDL_COMMON_APPDATA */
1007 CSIDL_Type_AllUsers,
1009 MAKEINTRESOURCEW(IDS_APPDATA)
1011 { /* 0x24 - CSIDL_WINDOWS */
1012 CSIDL_Type_WindowsPath,
1016 { /* 0x25 - CSIDL_SYSTEM */
1017 CSIDL_Type_SystemPath,
1021 { /* 0x26 - CSIDL_PROGRAM_FILES */
1024 MAKEINTRESOURCEW(IDS_PROGRAM_FILES)
1026 { /* 0x27 - CSIDL_MYPICTURES */
1029 MAKEINTRESOURCEW(IDS_MYPICTURES)
1031 { /* 0x28 - CSIDL_PROFILE */
1036 { /* 0x29 - CSIDL_SYSTEMX86 */
1037 CSIDL_Type_SystemX86Path,
1041 { /* 0x2a - CSIDL_PROGRAM_FILESX86 */
1043 ProgramFilesDirX86W,
1044 MAKEINTRESOURCEW(IDS_PROGRAM_FILESX86)
1046 { /* 0x2b - CSIDL_PROGRAM_FILES_COMMON */
1049 MAKEINTRESOURCEW(IDS_PROGRAM_FILES_COMMON)
1051 { /* 0x2c - CSIDL_PROGRAM_FILES_COMMONX86 */
1054 MAKEINTRESOURCEW(IDS_PROGRAM_FILES_COMMONX86)
1056 { /* 0x2d - CSIDL_COMMON_TEMPLATES */
1057 CSIDL_Type_AllUsers,
1059 MAKEINTRESOURCEW(IDS_TEMPLATES)
1061 { /* 0x2e - CSIDL_COMMON_DOCUMENTS */
1062 CSIDL_Type_AllUsers,
1064 MAKEINTRESOURCEW(IDS_COMMON_DOCUMENTS)
1066 { /* 0x2f - CSIDL_COMMON_ADMINTOOLS */
1067 CSIDL_Type_AllUsers,
1068 Common_Administrative_ToolsW,
1069 MAKEINTRESOURCEW(IDS_ADMINTOOLS)
1071 { /* 0x30 - CSIDL_ADMINTOOLS */
1073 Administrative_ToolsW,
1074 MAKEINTRESOURCEW(IDS_ADMINTOOLS)
1076 { /* 0x31 - CSIDL_CONNECTIONS */
1077 CSIDL_Type_Disallowed,
1081 { /* 0x32 - unassigned */
1082 CSIDL_Type_Disallowed,
1086 { /* 0x33 - unassigned */
1087 CSIDL_Type_Disallowed,
1091 { /* 0x34 - unassigned */
1092 CSIDL_Type_Disallowed,
1096 { /* 0x35 - CSIDL_COMMON_MUSIC */
1097 CSIDL_Type_AllUsers,
1099 MAKEINTRESOURCEW(IDS_COMMON_MUSIC)
1101 { /* 0x36 - CSIDL_COMMON_PICTURES */
1102 CSIDL_Type_AllUsers,
1104 MAKEINTRESOURCEW(IDS_COMMON_PICTURES)
1106 { /* 0x37 - CSIDL_COMMON_VIDEO */
1107 CSIDL_Type_AllUsers,
1109 MAKEINTRESOURCEW(IDS_COMMON_VIDEO)
1111 { /* 0x38 - CSIDL_RESOURCES */
1112 CSIDL_Type_WindowsPath,
1116 { /* 0x39 - CSIDL_RESOURCES_LOCALIZED */
1117 CSIDL_Type_NonExistent,
1121 { /* 0x3a - CSIDL_COMMON_OEM_LINKS */
1122 CSIDL_Type_NonExistent,
1126 { /* 0x3b - CSIDL_CDBURN_AREA */
1129 MAKEINTRESOURCEW(IDS_CDBURN_AREA)
1131 { /* 0x3c unassigned */
1132 CSIDL_Type_Disallowed,
1136 { /* 0x3d - CSIDL_COMPUTERSNEARME */
1137 CSIDL_Type_Disallowed, /* FIXME */
1141 { /* 0x3e - CSIDL_PROFILES */
1142 CSIDL_Type_Disallowed, /* oddly, this matches WinXP */
1148 static HRESULT _SHExpandEnvironmentStrings(LPCWSTR szSrc, LPWSTR szDest);
1150 /* Gets the value named value from the registry key
1151 * rootKey\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
1152 * (or from rootKey\userPrefix\... if userPrefix is not NULL) into path, which
1153 * is assumed to be MAX_PATH WCHARs in length.
1154 * If it exists, expands the value and writes the expanded value to
1155 * rootKey\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders
1156 * Returns successful error code if the value was retrieved from the registry,
1157 * and a failure otherwise.
1159 static HRESULT _SHGetUserShellFolderPath(HKEY rootKey, LPCWSTR userPrefix,
1160 LPCWSTR value, LPWSTR path)
1163 WCHAR shellFolderPath[MAX_PATH], userShellFolderPath[MAX_PATH];
1164 LPCWSTR pShellFolderPath, pUserShellFolderPath;
1165 DWORD dwType, dwPathLen = MAX_PATH;
1166 HKEY userShellFolderKey, shellFolderKey;
1168 TRACE("%p,%s,%s,%p\n",rootKey, debugstr_w(userPrefix), debugstr_w(value),
1173 strcpyW(shellFolderPath, userPrefix);
1174 PathAddBackslashW(shellFolderPath);
1175 strcatW(shellFolderPath, szSHFolders);
1176 pShellFolderPath = shellFolderPath;
1177 strcpyW(userShellFolderPath, userPrefix);
1178 PathAddBackslashW(userShellFolderPath);
1179 strcatW(userShellFolderPath, szSHUserFolders);
1180 pUserShellFolderPath = userShellFolderPath;
1184 pUserShellFolderPath = szSHUserFolders;
1185 pShellFolderPath = szSHFolders;
1188 if (RegCreateKeyW(rootKey, pShellFolderPath, &shellFolderKey))
1190 TRACE("Failed to create %s\n", debugstr_w(pShellFolderPath));
1193 if (RegCreateKeyW(rootKey, pUserShellFolderPath, &userShellFolderKey))
1195 TRACE("Failed to create %s\n",
1196 debugstr_w(pUserShellFolderPath));
1197 RegCloseKey(shellFolderKey);
1201 if (!RegQueryValueExW(userShellFolderKey, value, NULL, &dwType,
1202 (LPBYTE)path, &dwPathLen) && (dwType == REG_EXPAND_SZ || dwType == REG_SZ))
1206 path[dwPathLen / sizeof(WCHAR)] = '\0';
1207 if (dwType == REG_EXPAND_SZ && path[0] == '%')
1209 WCHAR szTemp[MAX_PATH];
1211 _SHExpandEnvironmentStrings(path, szTemp);
1212 lstrcpynW(path, szTemp, MAX_PATH);
1214 ret = RegSetValueExW(shellFolderKey, value, 0, REG_SZ, (LPBYTE)path,
1215 (strlenW(path) + 1) * sizeof(WCHAR));
1216 if (ret != ERROR_SUCCESS)
1217 hr = HRESULT_FROM_WIN32(ret);
1223 RegCloseKey(shellFolderKey);
1224 RegCloseKey(userShellFolderKey);
1225 TRACE("returning 0x%08x\n", hr);
1229 /* Gets a 'semi-expanded' default value of the CSIDL with index folder into
1230 * pszPath, based on the entries in CSIDL_Data. By semi-expanded, I mean:
1231 * - The entry's szDefaultPath may be either a string value or an integer
1232 * resource identifier. In the latter case, the string value of the resource
1234 * - Depending on the entry's type, the path may begin with an (unexpanded)
1235 * environment variable name. The caller is responsible for expanding
1236 * environment strings if so desired.
1237 * The types that are prepended with environment variables are:
1238 * CSIDL_Type_User: %USERPROFILE%
1239 * CSIDL_Type_AllUsers: %ALLUSERSPROFILE%
1240 * CSIDL_Type_CurrVer: %SystemDrive%
1241 * (Others might make sense too, but as yet are unneeded.)
1243 static HRESULT _SHGetDefaultValue(BYTE folder, LPWSTR pszPath)
1246 WCHAR resourcePath[MAX_PATH];
1247 LPCWSTR pDefaultPath = NULL;
1249 TRACE("0x%02x,%p\n", folder, pszPath);
1251 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1252 return E_INVALIDARG;
1254 return E_INVALIDARG;
1256 if (CSIDL_Data[folder].szDefaultPath &&
1257 IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath))
1259 if (LoadStringW(shell32_hInstance,
1260 LOWORD(CSIDL_Data[folder].szDefaultPath), resourcePath, MAX_PATH))
1263 pDefaultPath = resourcePath;
1267 FIXME("(%d,%s), LoadString failed, missing translation?\n", folder,
1268 debugstr_w(pszPath));
1275 pDefaultPath = CSIDL_Data[folder].szDefaultPath;
1279 switch (CSIDL_Data[folder].type)
1281 case CSIDL_Type_User:
1282 strcpyW(pszPath, UserProfileW);
1284 case CSIDL_Type_AllUsers:
1285 strcpyW(pszPath, AllUsersProfileW);
1287 case CSIDL_Type_CurrVer:
1288 strcpyW(pszPath, SystemDriveW);
1291 ; /* no corresponding env. var, do nothing */
1295 PathAddBackslashW(pszPath);
1296 strcatW(pszPath, pDefaultPath);
1299 TRACE("returning 0x%08x\n", hr);
1303 /* Gets the (unexpanded) value of the folder with index folder into pszPath.
1304 * The folder's type is assumed to be CSIDL_Type_CurrVer. Its default value
1305 * can be overridden in the HKLM\\szCurrentVersion key.
1306 * If dwFlags has SHGFP_TYPE_DEFAULT set or if the value isn't overridden in
1307 * the registry, uses _SHGetDefaultValue to get the value.
1309 static HRESULT _SHGetCurrentVersionPath(DWORD dwFlags, BYTE folder,
1314 TRACE("0x%08x,0x%02x,%p\n", dwFlags, folder, pszPath);
1316 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1317 return E_INVALIDARG;
1318 if (CSIDL_Data[folder].type != CSIDL_Type_CurrVer)
1319 return E_INVALIDARG;
1321 return E_INVALIDARG;
1323 if (dwFlags & SHGFP_TYPE_DEFAULT)
1324 hr = _SHGetDefaultValue(folder, pszPath);
1329 if (RegCreateKeyW(HKEY_LOCAL_MACHINE, szCurrentVersion, &hKey))
1333 DWORD dwType, dwPathLen = MAX_PATH * sizeof(WCHAR);
1335 if (RegQueryValueExW(hKey, CSIDL_Data[folder].szValueName, NULL,
1336 &dwType, (LPBYTE)pszPath, &dwPathLen) ||
1337 (dwType != REG_SZ && dwType != REG_EXPAND_SZ))
1339 hr = _SHGetDefaultValue(folder, pszPath);
1340 dwType = REG_EXPAND_SZ;
1341 RegSetValueExW(hKey, CSIDL_Data[folder].szValueName, 0, dwType,
1342 (LPBYTE)pszPath, (strlenW(pszPath)+1)*sizeof(WCHAR));
1346 pszPath[dwPathLen / sizeof(WCHAR)] = '\0';
1352 TRACE("returning 0x%08x (output path is %s)\n", hr, debugstr_w(pszPath));
1356 static LPWSTR _GetUserSidStringFromToken(HANDLE Token)
1358 char InfoBuffer[64];
1359 PTOKEN_USER UserInfo;
1363 UserInfo = (PTOKEN_USER) InfoBuffer;
1364 if (! GetTokenInformation(Token, TokenUser, InfoBuffer, sizeof(InfoBuffer),
1367 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
1369 UserInfo = HeapAlloc(GetProcessHeap(), 0, InfoSize);
1370 if (UserInfo == NULL)
1372 if (! GetTokenInformation(Token, TokenUser, UserInfo, InfoSize,
1375 HeapFree(GetProcessHeap(), 0, UserInfo);
1380 if (! ConvertSidToStringSidW(UserInfo->User.Sid, &SidStr))
1383 if (UserInfo != (PTOKEN_USER) InfoBuffer)
1384 HeapFree(GetProcessHeap(), 0, UserInfo);
1389 /* Gets the user's path (unexpanded) for the CSIDL with index folder:
1390 * If SHGFP_TYPE_DEFAULT is set, calls _SHGetDefaultValue for it. Otherwise
1391 * calls _SHGetUserShellFolderPath for it. Where it looks depends on hToken:
1392 * - if hToken is -1, looks in HKEY_USERS\.Default
1393 * - otherwise looks first in HKEY_CURRENT_USER, followed by HKEY_LOCAL_MACHINE
1394 * if HKEY_CURRENT_USER doesn't contain any entries. If both fail, finally
1395 * calls _SHGetDefaultValue for it.
1397 static HRESULT _SHGetUserProfilePath(HANDLE hToken, DWORD dwFlags, BYTE folder,
1402 TRACE("%p,0x%08x,0x%02x,%p\n", hToken, dwFlags, folder, pszPath);
1404 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1405 return E_INVALIDARG;
1406 if (CSIDL_Data[folder].type != CSIDL_Type_User)
1407 return E_INVALIDARG;
1409 return E_INVALIDARG;
1411 if (dwFlags & SHGFP_TYPE_DEFAULT)
1413 if (hToken != NULL && hToken != (HANDLE)-1)
1415 FIXME("unsupported for user other than current or default\n");
1418 hr = _SHGetDefaultValue(folder, pszPath);
1422 LPCWSTR userPrefix = NULL;
1425 if (hToken == (HANDLE)-1)
1427 hRootKey = HKEY_USERS;
1428 userPrefix = DefaultW;
1430 else if (hToken == NULL)
1431 hRootKey = HKEY_CURRENT_USER;
1434 hRootKey = HKEY_USERS;
1435 userPrefix = _GetUserSidStringFromToken(hToken);
1436 if (userPrefix == NULL)
1442 hr = _SHGetUserShellFolderPath(hRootKey, userPrefix,
1443 CSIDL_Data[folder].szValueName, pszPath);
1444 if (FAILED(hr) && hRootKey != HKEY_LOCAL_MACHINE)
1445 hr = _SHGetUserShellFolderPath(HKEY_LOCAL_MACHINE, NULL,
1446 CSIDL_Data[folder].szValueName, pszPath);
1448 hr = _SHGetDefaultValue(folder, pszPath);
1449 if (userPrefix != NULL && userPrefix != DefaultW)
1450 LocalFree((HLOCAL) userPrefix);
1453 TRACE("returning 0x%08x (output path is %s)\n", hr, debugstr_w(pszPath));
1457 /* Gets the (unexpanded) path for the CSIDL with index folder. If dwFlags has
1458 * SHGFP_TYPE_DEFAULT set, calls _SHGetDefaultValue. Otherwise calls
1459 * _SHGetUserShellFolderPath for it, looking only in HKEY_LOCAL_MACHINE.
1460 * If this fails, falls back to _SHGetDefaultValue.
1462 static HRESULT _SHGetAllUsersProfilePath(DWORD dwFlags, BYTE folder,
1467 TRACE("0x%08x,0x%02x,%p\n", dwFlags, folder, pszPath);
1469 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1470 return E_INVALIDARG;
1471 if (CSIDL_Data[folder].type != CSIDL_Type_AllUsers)
1472 return E_INVALIDARG;
1474 return E_INVALIDARG;
1476 if (dwFlags & SHGFP_TYPE_DEFAULT)
1477 hr = _SHGetDefaultValue(folder, pszPath);
1480 hr = _SHGetUserShellFolderPath(HKEY_LOCAL_MACHINE, NULL,
1481 CSIDL_Data[folder].szValueName, pszPath);
1483 hr = _SHGetDefaultValue(folder, pszPath);
1485 TRACE("returning 0x%08x (output path is %s)\n", hr, debugstr_w(pszPath));
1489 static HRESULT _SHOpenProfilesKey(PHKEY pKey)
1494 lRet = RegCreateKeyExW(HKEY_LOCAL_MACHINE, ProfileListW, 0, NULL, 0,
1495 KEY_ALL_ACCESS, NULL, pKey, &disp);
1496 return HRESULT_FROM_WIN32(lRet);
1499 /* Reads the value named szValueName from the key profilesKey (assumed to be
1500 * opened by _SHOpenProfilesKey) into szValue, which is assumed to be MAX_PATH
1501 * WCHARs in length. If it doesn't exist, returns szDefault (and saves
1502 * szDefault to the registry).
1504 static HRESULT _SHGetProfilesValue(HKEY profilesKey, LPCWSTR szValueName,
1505 LPWSTR szValue, LPCWSTR szDefault)
1508 DWORD type, dwPathLen = MAX_PATH * sizeof(WCHAR);
1511 TRACE("%p,%s,%p,%s\n", profilesKey, debugstr_w(szValueName), szValue,
1512 debugstr_w(szDefault));
1513 lRet = RegQueryValueExW(profilesKey, szValueName, NULL, &type,
1514 (LPBYTE)szValue, &dwPathLen);
1515 if (!lRet && (type == REG_SZ || type == REG_EXPAND_SZ) && dwPathLen
1518 dwPathLen /= sizeof(WCHAR);
1519 szValue[dwPathLen] = '\0';
1524 /* Missing or invalid value, set a default */
1525 lstrcpynW(szValue, szDefault, MAX_PATH);
1526 TRACE("Setting missing value %s to %s\n", debugstr_w(szValueName),
1527 debugstr_w(szValue));
1528 lRet = RegSetValueExW(profilesKey, szValueName, 0, REG_EXPAND_SZ,
1530 (strlenW(szValue) + 1) * sizeof(WCHAR));
1532 hr = HRESULT_FROM_WIN32(lRet);
1536 TRACE("returning 0x%08x (output value is %s)\n", hr, debugstr_w(szValue));
1540 /* Attempts to expand environment variables from szSrc into szDest, which is
1541 * assumed to be MAX_PATH characters in length. Before referring to the
1542 * environment, handles a few variables directly, because the environment
1543 * variables may not be set when this is called (as during Wine's installation
1544 * when default values are being written to the registry).
1545 * The directly handled environment variables, and their source, are:
1546 * - ALLUSERSPROFILE, USERPROFILE: reads from the registry
1547 * - SystemDrive: uses GetSystemDirectoryW and uses the drive portion of its
1549 * If one of the directly handled environment variables is expanded, only
1550 * expands a single variable, and only in the beginning of szSrc.
1552 static HRESULT _SHExpandEnvironmentStrings(LPCWSTR szSrc, LPWSTR szDest)
1555 WCHAR szTemp[MAX_PATH], szProfilesPrefix[MAX_PATH] = { 0 };
1558 TRACE("%s, %p\n", debugstr_w(szSrc), szDest);
1560 if (!szSrc || !szDest) return E_INVALIDARG;
1562 /* short-circuit if there's nothing to expand */
1563 if (szSrc[0] != '%')
1565 strcpyW(szDest, szSrc);
1569 /* Get the profile prefix, we'll probably be needing it */
1570 hr = _SHOpenProfilesKey(&key);
1573 WCHAR def_val[MAX_PATH];
1575 /* get the system drive */
1576 GetSystemDirectoryW(def_val, MAX_PATH);
1577 if (def_val[1] == ':') strcpyW( def_val + 3, szDefaultProfileDirW );
1578 else FIXME("non-drive system paths unsupported\n");
1580 hr = _SHGetProfilesValue(key, ProfilesDirectoryW, szProfilesPrefix, def_val );
1584 strcpyW(szTemp, szSrc);
1585 while (SUCCEEDED(hr) && szTemp[0] == '%')
1587 if (!strncmpiW(szTemp, AllUsersProfileW, strlenW(AllUsersProfileW)))
1589 WCHAR szAllUsers[MAX_PATH];
1591 strcpyW(szDest, szProfilesPrefix);
1592 hr = _SHGetProfilesValue(key, AllUsersProfileValueW,
1593 szAllUsers, AllUsersW);
1594 PathAppendW(szDest, szAllUsers);
1595 PathAppendW(szDest, szTemp + strlenW(AllUsersProfileW));
1597 else if (!strncmpiW(szTemp, UserProfileW, strlenW(UserProfileW)))
1599 WCHAR userName[MAX_PATH];
1600 DWORD userLen = MAX_PATH;
1602 strcpyW(szDest, szProfilesPrefix);
1603 GetUserNameW(userName, &userLen);
1604 PathAppendW(szDest, userName);
1605 PathAppendW(szDest, szTemp + strlenW(UserProfileW));
1607 else if (!strncmpiW(szTemp, SystemDriveW, strlenW(SystemDriveW)))
1609 GetSystemDirectoryW(szDest, MAX_PATH);
1610 if (szDest[1] != ':')
1612 FIXME("non-drive system paths unsupported\n");
1617 strcpyW(szDest + 3, szTemp + strlenW(SystemDriveW) + 1);
1623 DWORD ret = ExpandEnvironmentStringsW(szSrc, szDest, MAX_PATH);
1626 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
1628 hr = HRESULT_FROM_WIN32(GetLastError());
1632 if (SUCCEEDED(hr) && szDest[0] == '%')
1633 strcpyW(szTemp, szDest);
1636 /* terminate loop */
1643 TRACE("returning 0x%08x (input was %s, output is %s)\n", hr,
1644 debugstr_w(szSrc), debugstr_w(szDest));
1648 /*************************************************************************
1649 * SHGetFolderPathW [SHELL32.@]
1651 * Convert nFolder to path.
1655 * Failure: standard HRESULT error codes.
1658 * Most values can be overridden in either
1659 * HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
1660 * or in the same location in HKLM.
1661 * The "Shell Folders" registry key was used in NT4 and earlier systems.
1662 * Beginning with Windows 2000, the "User Shell Folders" key is used, so
1663 * changes made to it are made to the former key too. This synchronization is
1664 * done on-demand: not until someone requests the value of one of these paths
1665 * (by calling one of the SHGet functions) is the value synchronized.
1666 * Furthermore, the HKCU paths take precedence over the HKLM paths.
1668 HRESULT WINAPI SHGetFolderPathW(
1669 HWND hwndOwner, /* [I] owner window */
1670 int nFolder, /* [I] CSIDL identifying the folder */
1671 HANDLE hToken, /* [I] access token */
1672 DWORD dwFlags, /* [I] which path to return */
1673 LPWSTR pszPath) /* [O] converted path */
1675 HRESULT hr = SHGetFolderPathAndSubDirW(hwndOwner, nFolder, hToken, dwFlags, NULL, pszPath);
1676 if(HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND) == hr)
1677 hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
1681 HRESULT WINAPI SHGetFolderPathAndSubDirA(
1682 HWND hwndOwner, /* [I] owner window */
1683 int nFolder, /* [I] CSIDL identifying the folder */
1684 HANDLE hToken, /* [I] access token */
1685 DWORD dwFlags, /* [I] which path to return */
1686 LPCSTR pszSubPath, /* [I] sub directory of the specified folder */
1687 LPSTR pszPath) /* [O] converted path */
1691 LPWSTR pszSubPathW = NULL;
1692 LPWSTR pszPathW = NULL;
1693 TRACE("%08x,%08x,%s\n",nFolder, dwFlags, debugstr_w(pszSubPathW));
1696 pszPathW = HeapAlloc(GetProcessHeap(), 0, MAX_PATH * sizeof(WCHAR));
1698 hr = HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
1702 TRACE("%08x,%08x,%s\n",nFolder, dwFlags, debugstr_w(pszSubPathW));
1704 /* SHGetFolderPathAndSubDirW does not distinguish if pszSubPath isn't
1705 * set (null), or an empty string.therefore call it without the parameter set
1706 * if pszSubPath is an empty string
1708 if (pszSubPath && pszSubPath[0]) {
1709 length = MultiByteToWideChar(CP_ACP, 0, pszSubPath, -1, NULL, 0);
1710 pszSubPathW = HeapAlloc(GetProcessHeap(), 0, length * sizeof(WCHAR));
1712 hr = HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
1715 MultiByteToWideChar(CP_ACP, 0, pszSubPath, -1, pszSubPathW, length);
1718 hr = SHGetFolderPathAndSubDirW(hwndOwner, nFolder, hToken, dwFlags, pszSubPathW, pszPathW);
1720 if (SUCCEEDED(hr) && pszPath)
1721 WideCharToMultiByte(CP_ACP, 0, pszPathW, -1, pszPath, MAX_PATH, NULL, NULL);
1724 HeapFree(GetProcessHeap(), 0, pszPathW);
1725 HeapFree(GetProcessHeap(), 0, pszSubPathW);
1729 /*************************************************************************
1730 * SHGetFolderPathAndSubDirW [SHELL32.@]
1732 HRESULT WINAPI SHGetFolderPathAndSubDirW(
1733 HWND hwndOwner, /* [I] owner window */
1734 int nFolder, /* [I] CSIDL identifying the folder */
1735 HANDLE hToken, /* [I] access token */
1736 DWORD dwFlags, /* [I] which path to return */
1737 LPCWSTR pszSubPath,/* [I] sub directory of the specified folder */
1738 LPWSTR pszPath) /* [O] converted path */
1741 WCHAR szBuildPath[MAX_PATH], szTemp[MAX_PATH];
1742 DWORD folder = nFolder & CSIDL_FOLDER_MASK;
1746 TRACE("%p,%p,nFolder=0x%04x,%s\n", hwndOwner,pszPath,nFolder,debugstr_w(pszSubPath));
1748 /* Windows always NULL-terminates the resulting path regardless of success
1749 * or failure, so do so first
1754 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1755 return E_INVALIDARG;
1756 if ((SHGFP_TYPE_CURRENT != dwFlags) && (SHGFP_TYPE_DEFAULT != dwFlags))
1757 return E_INVALIDARG;
1759 type = CSIDL_Data[folder].type;
1762 case CSIDL_Type_Disallowed:
1765 case CSIDL_Type_NonExistent:
1768 case CSIDL_Type_WindowsPath:
1769 GetWindowsDirectoryW(szTemp, MAX_PATH);
1770 if (CSIDL_Data[folder].szDefaultPath &&
1771 !IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath) &&
1772 *CSIDL_Data[folder].szDefaultPath)
1774 PathAddBackslashW(szTemp);
1775 strcatW(szTemp, CSIDL_Data[folder].szDefaultPath);
1779 case CSIDL_Type_SystemPath:
1780 GetSystemDirectoryW(szTemp, MAX_PATH);
1781 if (CSIDL_Data[folder].szDefaultPath &&
1782 !IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath) &&
1783 *CSIDL_Data[folder].szDefaultPath)
1785 PathAddBackslashW(szTemp);
1786 strcatW(szTemp, CSIDL_Data[folder].szDefaultPath);
1790 case CSIDL_Type_SystemX86Path:
1791 if (!GetSystemWow64DirectoryW(szTemp, MAX_PATH)) GetSystemDirectoryW(szTemp, MAX_PATH);
1792 if (CSIDL_Data[folder].szDefaultPath &&
1793 !IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath) &&
1794 *CSIDL_Data[folder].szDefaultPath)
1796 PathAddBackslashW(szTemp);
1797 strcatW(szTemp, CSIDL_Data[folder].szDefaultPath);
1801 case CSIDL_Type_CurrVer:
1802 hr = _SHGetCurrentVersionPath(dwFlags, folder, szTemp);
1804 case CSIDL_Type_User:
1805 hr = _SHGetUserProfilePath(hToken, dwFlags, folder, szTemp);
1807 case CSIDL_Type_AllUsers:
1808 hr = _SHGetAllUsersProfilePath(dwFlags, folder, szTemp);
1811 FIXME("bogus type %d, please fix\n", type);
1816 /* Expand environment strings if necessary */
1818 hr = _SHExpandEnvironmentStrings(szTemp, szBuildPath);
1820 strcpyW(szBuildPath, szTemp);
1822 if (FAILED(hr)) goto end;
1825 /* make sure the new path does not exceed th bufferlength
1826 * rememebr to backslash and the termination */
1827 if(MAX_PATH < (lstrlenW(szBuildPath) + lstrlenW(pszSubPath) + 2)) {
1828 hr = HRESULT_FROM_WIN32(ERROR_FILENAME_EXCED_RANGE);
1831 PathAppendW(szBuildPath, pszSubPath);
1832 PathRemoveBackslashW(szBuildPath);
1834 /* Copy the path if it's available before we might return */
1835 if (SUCCEEDED(hr) && pszPath)
1836 strcpyW(pszPath, szBuildPath);
1838 /* if we don't care about existing directories we are ready */
1839 if(nFolder & CSIDL_FLAG_DONT_VERIFY) goto end;
1841 if (PathFileExistsW(szBuildPath)) goto end;
1843 /* not existing but we are not allowed to create it. The return value
1844 * is verified against shell32 version 6.0.
1846 if (!(nFolder & CSIDL_FLAG_CREATE))
1848 hr = HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND);
1852 /* create directory/directories */
1853 ret = SHCreateDirectoryExW(hwndOwner, szBuildPath, NULL);
1854 if (ret && ret != ERROR_ALREADY_EXISTS)
1856 ERR("Failed to create directory %s.\n", debugstr_w(szBuildPath));
1861 TRACE("Created missing system directory %s\n", debugstr_w(szBuildPath));
1863 TRACE("returning 0x%08x (final path is %s)\n", hr, debugstr_w(szBuildPath));
1867 /*************************************************************************
1868 * SHGetFolderPathA [SHELL32.@]
1870 * See SHGetFolderPathW.
1872 HRESULT WINAPI SHGetFolderPathA(
1879 WCHAR szTemp[MAX_PATH];
1882 TRACE("%p,%p,nFolder=0x%04x\n",hwndOwner,pszPath,nFolder);
1886 hr = SHGetFolderPathW(hwndOwner, nFolder, hToken, dwFlags, szTemp);
1887 if (SUCCEEDED(hr) && pszPath)
1888 WideCharToMultiByte(CP_ACP, 0, szTemp, -1, pszPath, MAX_PATH, NULL,
1894 /* For each folder in folders, if its value has not been set in the registry,
1895 * calls _SHGetUserProfilePath or _SHGetAllUsersProfilePath (depending on the
1896 * folder's type) to get the unexpanded value first.
1897 * Writes the unexpanded value to User Shell Folders, and queries it with
1898 * SHGetFolderPathW to force the creation of the directory if it doesn't
1899 * already exist. SHGetFolderPathW also returns the expanded value, which
1900 * this then writes to Shell Folders.
1902 static HRESULT _SHRegisterFolders(HKEY hRootKey, HANDLE hToken,
1903 LPCWSTR szUserShellFolderPath, LPCWSTR szShellFolderPath, const UINT folders[],
1907 WCHAR path[MAX_PATH];
1909 HKEY hUserKey = NULL, hKey = NULL;
1910 DWORD dwType, dwPathLen;
1913 TRACE("%p,%p,%s,%p,%u\n", hRootKey, hToken,
1914 debugstr_w(szUserShellFolderPath), folders, foldersLen);
1916 ret = RegCreateKeyW(hRootKey, szUserShellFolderPath, &hUserKey);
1918 hr = HRESULT_FROM_WIN32(ret);
1921 ret = RegCreateKeyW(hRootKey, szShellFolderPath, &hKey);
1923 hr = HRESULT_FROM_WIN32(ret);
1925 for (i = 0; SUCCEEDED(hr) && i < foldersLen; i++)
1927 dwPathLen = MAX_PATH * sizeof(WCHAR);
1928 if (RegQueryValueExW(hUserKey, CSIDL_Data[folders[i]].szValueName, NULL,
1929 &dwType, (LPBYTE)path, &dwPathLen) || (dwType != REG_SZ &&
1930 dwType != REG_EXPAND_SZ))
1933 if (CSIDL_Data[folders[i]].type == CSIDL_Type_User)
1934 _SHGetUserProfilePath(hToken, SHGFP_TYPE_DEFAULT, folders[i],
1936 else if (CSIDL_Data[folders[i]].type == CSIDL_Type_AllUsers)
1937 _SHGetAllUsersProfilePath(SHGFP_TYPE_DEFAULT, folders[i], path);
1938 else if (CSIDL_Data[folders[i]].type == CSIDL_Type_WindowsPath)
1940 GetWindowsDirectoryW(path, MAX_PATH);
1941 if (CSIDL_Data[folders[i]].szDefaultPath &&
1942 !IS_INTRESOURCE(CSIDL_Data[folders[i]].szDefaultPath))
1944 PathAddBackslashW(path);
1945 strcatW(path, CSIDL_Data[folders[i]].szDefaultPath);
1952 ret = RegSetValueExW(hUserKey,
1953 CSIDL_Data[folders[i]].szValueName, 0, REG_EXPAND_SZ,
1954 (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR));
1956 hr = HRESULT_FROM_WIN32(ret);
1959 hr = SHGetFolderPathW(NULL, folders[i] | CSIDL_FLAG_CREATE,
1960 hToken, SHGFP_TYPE_DEFAULT, path);
1961 ret = RegSetValueExW(hKey,
1962 CSIDL_Data[folders[i]].szValueName, 0, REG_SZ,
1963 (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR));
1965 hr = HRESULT_FROM_WIN32(ret);
1971 RegCloseKey(hUserKey);
1975 TRACE("returning 0x%08x\n", hr);
1979 static HRESULT _SHRegisterUserShellFolders(BOOL bDefault)
1981 static const UINT folders[] = {
1992 CSIDL_DESKTOPDIRECTORY,
1996 CSIDL_LOCAL_APPDATA,
1997 CSIDL_INTERNET_CACHE,
2003 WCHAR userShellFolderPath[MAX_PATH], shellFolderPath[MAX_PATH];
2004 LPCWSTR pUserShellFolderPath, pShellFolderPath;
2009 TRACE("%s\n", bDefault ? "TRUE" : "FALSE");
2012 hToken = (HANDLE)-1;
2013 hRootKey = HKEY_USERS;
2014 strcpyW(userShellFolderPath, DefaultW);
2015 PathAddBackslashW(userShellFolderPath);
2016 strcatW(userShellFolderPath, szSHUserFolders);
2017 pUserShellFolderPath = userShellFolderPath;
2018 strcpyW(shellFolderPath, DefaultW);
2019 PathAddBackslashW(shellFolderPath);
2020 strcatW(shellFolderPath, szSHFolders);
2021 pShellFolderPath = shellFolderPath;
2026 hRootKey = HKEY_CURRENT_USER;
2027 pUserShellFolderPath = szSHUserFolders;
2028 pShellFolderPath = szSHFolders;
2031 hr = _SHRegisterFolders(hRootKey, hToken, pUserShellFolderPath,
2032 pShellFolderPath, folders, sizeof(folders) / sizeof(folders[0]));
2033 TRACE("returning 0x%08x\n", hr);
2037 static HRESULT _SHRegisterCommonShellFolders(void)
2039 static const UINT folders[] = {
2040 CSIDL_COMMON_STARTMENU,
2041 CSIDL_COMMON_PROGRAMS,
2042 CSIDL_COMMON_STARTUP,
2043 CSIDL_COMMON_DESKTOPDIRECTORY,
2044 CSIDL_COMMON_FAVORITES,
2045 CSIDL_COMMON_APPDATA,
2046 CSIDL_COMMON_TEMPLATES,
2047 CSIDL_COMMON_DOCUMENTS,
2048 CSIDL_COMMON_ADMINTOOLS,
2050 CSIDL_COMMON_PICTURES,
2056 hr = _SHRegisterFolders(HKEY_LOCAL_MACHINE, NULL, szSHUserFolders,
2057 szSHFolders, folders, sizeof(folders) / sizeof(folders[0]));
2058 TRACE("returning 0x%08x\n", hr);
2062 /******************************************************************************
2063 * _SHAppendToUnixPath [Internal]
2065 * Helper function for _SHCreateSymbolicLinks. Appends pwszSubPath (or the
2066 * corresponding resource, if IS_INTRESOURCE) to the unix base path 'szBasePath'
2067 * and replaces backslashes with slashes.
2070 * szBasePath [IO] The unix base path, which will be appended to (CP_UNXICP).
2071 * pwszSubPath [I] Sub-path or resource id (use MAKEINTRESOURCEW).
2077 static inline BOOL _SHAppendToUnixPath(char *szBasePath, LPCWSTR pwszSubPath) {
2078 WCHAR wszSubPath[MAX_PATH];
2079 int cLen = strlen(szBasePath);
2082 if (IS_INTRESOURCE(pwszSubPath)) {
2083 if (!LoadStringW(shell32_hInstance, LOWORD(pwszSubPath), wszSubPath, MAX_PATH)) {
2084 /* Fall back to hard coded defaults. */
2085 switch (LOWORD(pwszSubPath)) {
2087 lstrcpyW(wszSubPath, PersonalW);
2090 lstrcpyW(wszSubPath, My_MusicW);
2092 case IDS_MYPICTURES:
2093 lstrcpyW(wszSubPath, My_PicturesW);
2096 lstrcpyW(wszSubPath, My_VideoW);
2099 ERR("LoadString(%d) failed!\n", LOWORD(pwszSubPath));
2104 lstrcpyW(wszSubPath, pwszSubPath);
2107 if (szBasePath[cLen-1] != '/') szBasePath[cLen++] = '/';
2109 if (!WideCharToMultiByte(CP_UNIXCP, 0, wszSubPath, -1, szBasePath + cLen,
2110 FILENAME_MAX - cLen, NULL, NULL))
2115 pBackslash = szBasePath + cLen;
2116 while ((pBackslash = strchr(pBackslash, '\\'))) *pBackslash = '/';
2121 /******************************************************************************
2122 * _SHCreateSymbolicLinks [Internal]
2124 * Sets up symbol links for various shell folders to point into the users home
2125 * directory. We do an educated guess about what the user would probably want:
2126 * - If there is a 'My Documents' directory in $HOME, the user probably wants
2127 * wine's 'My Documents' to point there. Furthermore, we imply that the user
2128 * is a Windows lover and has no problem with wine creating 'My Pictures',
2129 * 'My Music' and 'My Video' subfolders under '$HOME/My Documents', if those
2130 * do not already exits. We put appropriate symbolic links in place for those,
2132 * - If there is no 'My Documents' directory in $HOME, we let 'My Documents'
2133 * point directly to $HOME. We assume the user to be a unix hacker who does not
2134 * want wine to create anything anywhere besides the .wine directory. So, if
2135 * there already is a 'My Music' directory in $HOME, we symlink the 'My Music'
2136 * shell folder to it. But if not, then we check XDG_MUSIC_DIR - "well known"
2137 * directory, and try to link to that. If that fails, then we symlink to
2138 * $HOME directly. The same holds fo 'My Pictures' and 'My Video'.
2139 * - The Desktop shell folder is symlinked to XDG_DESKTOP_DIR. If that does not
2140 * exist, then we try '$HOME/Desktop'. If that does not exist, then we leave
2142 * ('My Music',... above in fact means LoadString(IDS_MYMUSIC))
2144 static void _SHCreateSymbolicLinks(void)
2146 UINT aidsMyStuff[] = { IDS_MYPICTURES, IDS_MYVIDEO, IDS_MYMUSIC }, i;
2147 int acsidlMyStuff[] = { CSIDL_MYPICTURES, CSIDL_MYVIDEO, CSIDL_MYMUSIC };
2148 static const char * const xdg_dirs[] = { "PICTURES", "VIDEOS", "MUSIC", "DESKTOP" };
2149 static const unsigned int num = sizeof(xdg_dirs) / sizeof(xdg_dirs[0]);
2150 WCHAR wszTempPath[MAX_PATH];
2151 char szPersonalTarget[FILENAME_MAX], *pszPersonal;
2152 char szMyStuffTarget[FILENAME_MAX], *pszMyStuff;
2153 char szDesktopTarget[FILENAME_MAX], *pszDesktop;
2154 struct stat statFolder;
2155 const char *pszHome;
2157 char ** xdg_results;
2158 char * xdg_desktop_dir;
2160 /* Create all necessary profile sub-dirs up to 'My Documents' and get the unix path. */
2161 hr = SHGetFolderPathW(NULL, CSIDL_PERSONAL|CSIDL_FLAG_CREATE, NULL,
2162 SHGFP_TYPE_DEFAULT, wszTempPath);
2163 if (FAILED(hr)) return;
2164 pszPersonal = wine_get_unix_file_name(wszTempPath);
2165 if (!pszPersonal) return;
2167 hr = XDG_UserDirLookup(xdg_dirs, num, &xdg_results);
2168 if (FAILED(hr)) xdg_results = NULL;
2170 pszHome = getenv("HOME");
2171 if (pszHome && !stat(pszHome, &statFolder) && S_ISDIR(statFolder.st_mode)) {
2172 strcpy(szPersonalTarget, pszHome);
2173 if (_SHAppendToUnixPath(szPersonalTarget, MAKEINTRESOURCEW(IDS_PERSONAL)) &&
2174 !stat(szPersonalTarget, &statFolder) && S_ISDIR(statFolder.st_mode))
2176 /* '$HOME/My Documents' exists. Create 'My Pictures', 'My Videos' and
2177 * 'My Music' subfolders or fail silently if they already exist. */
2178 for (i = 0; i < sizeof(aidsMyStuff)/sizeof(aidsMyStuff[0]); i++) {
2179 strcpy(szMyStuffTarget, szPersonalTarget);
2180 if (_SHAppendToUnixPath(szMyStuffTarget, MAKEINTRESOURCEW(aidsMyStuff[i])))
2181 mkdir(szMyStuffTarget, 0777);
2186 /* '$HOME/My Documents' doesn't exists, but '$HOME' does. */
2187 strcpy(szPersonalTarget, pszHome);
2190 /* Replace 'My Documents' directory with a symlink of fail silently if not empty. */
2192 symlink(szPersonalTarget, pszPersonal);
2196 /* '$HOME' doesn't exist. Create 'My Pictures', 'My Videos' and 'My Music' subdirs
2197 * in '%USERPROFILE%\\My Documents' or fail silently if they already exist. */
2198 strcpy(szPersonalTarget, pszPersonal);
2199 for (i = 0; i < sizeof(aidsMyStuff)/sizeof(aidsMyStuff[0]); i++) {
2200 strcpy(szMyStuffTarget, szPersonalTarget);
2201 if (_SHAppendToUnixPath(szMyStuffTarget, MAKEINTRESOURCEW(aidsMyStuff[i])))
2202 mkdir(szMyStuffTarget, 0777);
2206 /* Create symbolic links for 'My Pictures', 'My Video' and 'My Music'. */
2207 for (i=0; i < sizeof(aidsMyStuff)/sizeof(aidsMyStuff[0]); i++) {
2208 /* Create the current 'My Whatever' folder and get it's unix path. */
2209 hr = SHGetFolderPathW(NULL, acsidlMyStuff[i]|CSIDL_FLAG_CREATE, NULL,
2210 SHGFP_TYPE_DEFAULT, wszTempPath);
2211 if (FAILED(hr)) continue;
2212 pszMyStuff = wine_get_unix_file_name(wszTempPath);
2213 if (!pszMyStuff) continue;
2215 strcpy(szMyStuffTarget, szPersonalTarget);
2216 if (_SHAppendToUnixPath(szMyStuffTarget, MAKEINTRESOURCEW(aidsMyStuff[i])) &&
2217 !stat(szMyStuffTarget, &statFolder) && S_ISDIR(statFolder.st_mode))
2219 /* If there's a 'My Whatever' directory where 'My Documents' links to, link to it. */
2221 symlink(szMyStuffTarget, pszMyStuff);
2226 if (xdg_results && xdg_results[i])
2228 /* the folder specified by XDG_XXX_DIR exists, link to it. */
2229 symlink(xdg_results[i], pszMyStuff);
2233 /* Else link to where 'My Documents' itself links to. */
2234 symlink(szPersonalTarget, pszMyStuff);
2237 HeapFree(GetProcessHeap(), 0, pszMyStuff);
2240 /* Last but not least, the Desktop folder */
2242 strcpy(szDesktopTarget, pszHome);
2244 strcpy(szDesktopTarget, pszPersonal);
2245 HeapFree(GetProcessHeap(), 0, pszPersonal);
2247 xdg_desktop_dir = xdg_results ? xdg_results[num - 1] : NULL;
2248 if (xdg_desktop_dir ||
2249 (_SHAppendToUnixPath(szDesktopTarget, DesktopW) &&
2250 !stat(szDesktopTarget, &statFolder) && S_ISDIR(statFolder.st_mode)))
2252 hr = SHGetFolderPathW(NULL, CSIDL_DESKTOPDIRECTORY|CSIDL_FLAG_CREATE, NULL,
2253 SHGFP_TYPE_DEFAULT, wszTempPath);
2254 if (SUCCEEDED(hr) && (pszDesktop = wine_get_unix_file_name(wszTempPath)))
2257 if (xdg_desktop_dir)
2258 symlink(xdg_desktop_dir, pszDesktop);
2260 symlink(szDesktopTarget, pszDesktop);
2261 HeapFree(GetProcessHeap(), 0, pszDesktop);
2265 /* Free resources allocated by XDG_UserDirLookup() */
2268 for (i = 0; i < num; i++)
2269 HeapFree(GetProcessHeap(), 0, xdg_results[i]);
2270 HeapFree(GetProcessHeap(), 0, xdg_results);
2274 /******************************************************************************
2275 * create_extra_folders [Internal]
2277 * Create some extra folders that don't have a standard CSIDL definition.
2279 static HRESULT create_extra_folders(void)
2281 static const WCHAR environW[] = {'E','n','v','i','r','o','n','m','e','n','t',0};
2282 static const WCHAR TempW[] = {'T','e','m','p',0};
2283 static const WCHAR TEMPW[] = {'T','E','M','P',0};
2284 static const WCHAR TMPW[] = {'T','M','P',0};
2285 WCHAR path[MAX_PATH+5];
2288 DWORD type, size, ret;
2290 ret = RegCreateKeyW( HKEY_CURRENT_USER, environW, &hkey );
2291 if (ret) return HRESULT_FROM_WIN32( ret );
2293 /* FIXME: should be under AppData, but we don't want spaces in the temp path */
2294 hr = SHGetFolderPathAndSubDirW( 0, CSIDL_PROFILE | CSIDL_FLAG_CREATE, NULL,
2295 SHGFP_TYPE_DEFAULT, TempW, path );
2298 size = sizeof(path);
2299 if (RegQueryValueExW( hkey, TEMPW, NULL, &type, (LPBYTE)path, &size ))
2300 RegSetValueExW( hkey, TEMPW, 0, REG_SZ, (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR) );
2301 size = sizeof(path);
2302 if (RegQueryValueExW( hkey, TMPW, NULL, &type, (LPBYTE)path, &size ))
2303 RegSetValueExW( hkey, TMPW, 0, REG_SZ, (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR) );
2305 RegCloseKey( hkey );
2310 /* Register the default values in the registry, as some apps seem to depend
2311 * on their presence. The set registered was taken from Windows XP.
2313 HRESULT SHELL_RegisterShellFolders(void)
2317 /* Set up '$HOME' targeted symlinks for 'My Documents', 'My Pictures',
2318 * 'My Video', 'My Music' and 'Desktop' in advance, so that the
2319 * _SHRegister*ShellFolders() functions will find everything nice and clean
2320 * and thus will not attempt to create them in the profile directory. */
2321 _SHCreateSymbolicLinks();
2323 hr = _SHRegisterUserShellFolders(TRUE);
2325 hr = _SHRegisterUserShellFolders(FALSE);
2327 hr = _SHRegisterCommonShellFolders();
2329 hr = create_extra_folders();
2333 /*************************************************************************
2334 * SHGetSpecialFolderPathA [SHELL32.@]
2336 BOOL WINAPI SHGetSpecialFolderPathA (
2342 return (SHGetFolderPathA(
2344 nFolder + (bCreate ? CSIDL_FLAG_CREATE : 0),
2347 szPath)) == S_OK ? TRUE : FALSE;
2350 /*************************************************************************
2351 * SHGetSpecialFolderPathW
2353 BOOL WINAPI SHGetSpecialFolderPathW (
2359 return (SHGetFolderPathW(
2361 nFolder + (bCreate ? CSIDL_FLAG_CREATE : 0),
2364 szPath)) == S_OK ? TRUE : FALSE;
2367 /*************************************************************************
2368 * SHGetSpecialFolderPath (SHELL32.175)
2370 BOOL WINAPI SHGetSpecialFolderPathAW (
2377 if (SHELL_OsIsUnicode())
2378 return SHGetSpecialFolderPathW (hwndOwner, szPath, nFolder, bCreate);
2379 return SHGetSpecialFolderPathA (hwndOwner, szPath, nFolder, bCreate);
2382 /*************************************************************************
2383 * SHGetFolderLocation [SHELL32.@]
2385 * Gets the folder locations from the registry and creates a pidl.
2389 * nFolder [I] CSIDL_xxxxx
2390 * hToken [I] token representing user, or NULL for current user, or -1 for
2392 * dwReserved [I] must be zero
2393 * ppidl [O] PIDL of a special folder
2397 * Failure: Standard OLE-defined error result, S_FALSE or E_INVALIDARG
2400 * Creates missing reg keys and directories.
2401 * Mostly forwards to SHGetFolderPathW, but a few values of nFolder return
2402 * virtual folders that are handled here.
2404 HRESULT WINAPI SHGetFolderLocation(
2409 LPITEMIDLIST *ppidl)
2411 HRESULT hr = E_INVALIDARG;
2413 TRACE("%p 0x%08x %p 0x%08x %p\n",
2414 hwndOwner, nFolder, hToken, dwReserved, ppidl);
2417 return E_INVALIDARG;
2419 return E_INVALIDARG;
2421 /* The virtual folders' locations are not user-dependent */
2423 switch (nFolder & CSIDL_FOLDER_MASK)
2426 *ppidl = _ILCreateDesktop();
2429 case CSIDL_PERSONAL:
2430 *ppidl = _ILCreateMyDocuments();
2433 case CSIDL_INTERNET:
2434 *ppidl = _ILCreateIExplore();
2437 case CSIDL_CONTROLS:
2438 *ppidl = _ILCreateControlPanel();
2441 case CSIDL_PRINTERS:
2442 *ppidl = _ILCreatePrinters();
2445 case CSIDL_BITBUCKET:
2446 *ppidl = _ILCreateBitBucket();
2450 *ppidl = _ILCreateMyComputer();
2454 *ppidl = _ILCreateNetwork();
2459 WCHAR szPath[MAX_PATH];
2461 hr = SHGetFolderPathW(hwndOwner, nFolder, hToken,
2462 SHGFP_TYPE_CURRENT, szPath);
2467 TRACE("Value=%s\n", debugstr_w(szPath));
2468 hr = SHILCreateFromPathW(szPath, ppidl, &attributes);
2470 else if (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
2472 /* unlike SHGetFolderPath, SHGetFolderLocation in shell32
2473 * version 6.0 returns E_FAIL for nonexistent paths
2482 TRACE("-- (new pidl %p)\n",*ppidl);
2486 /*************************************************************************
2487 * SHGetSpecialFolderLocation [SHELL32.@]
2490 * In NT5, SHGetSpecialFolderLocation needs the <winntdir>/Recent
2493 HRESULT WINAPI SHGetSpecialFolderLocation(
2496 LPITEMIDLIST * ppidl)
2498 HRESULT hr = E_INVALIDARG;
2500 TRACE("(%p,0x%x,%p)\n", hwndOwner,nFolder,ppidl);
2503 return E_INVALIDARG;
2505 hr = SHGetFolderLocation(hwndOwner, nFolder, NULL, 0, ppidl);
2509 /*************************************************************************
2510 * SHGetKnownFolderPath [SHELL32.@]
2512 HRESULT WINAPI SHGetKnownFolderPath(REFKNOWNFOLDERID rfid, DWORD flags, HANDLE token, PWSTR *path)
2514 FIXME("(%s, %d, %p, %p) stub!\n", debugstr_guid(rfid), flags, token, path);