push 73336d9f381967eae40f391d78198b916ed9848d
[wine/hacks.git] / dlls / shell32 / shellpath.c
blob738e9e39875f8f0a64dabbbde82393dee04637e2
1 /*
2 * Path Functions
4 * Copyright 1998, 1999, 2000 Juergen Schmied
5 * Copyright 2004 Juan Lang
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 * NOTES:
23 * Many of these functions are in SHLWAPI.DLL also
27 #include "config.h"
28 #include "wine/port.h"
30 #include <stdio.h>
31 #include <stdarg.h>
32 #include <string.h>
33 #include <ctype.h>
34 #include "wine/debug.h"
35 #include "windef.h"
36 #include "winbase.h"
37 #include "winnls.h"
38 #include "winreg.h"
39 #include "wingdi.h"
40 #include "winuser.h"
42 #include "shlobj.h"
43 #include "shresdef.h"
44 #include "shell32_main.h"
45 #include "undocshell.h"
46 #include "pidl.h"
47 #include "wine/unicode.h"
48 #include "shlwapi.h"
49 #include "xdg.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(shell);
54 ########## Combining and Constructing paths ##########
57 /*************************************************************************
58 * PathAppend [SHELL32.36]
60 BOOL WINAPI PathAppendAW(
61 LPVOID lpszPath1,
62 LPCVOID lpszPath2)
64 if (SHELL_OsIsUnicode())
65 return PathAppendW(lpszPath1, lpszPath2);
66 return PathAppendA(lpszPath1, lpszPath2);
69 /*************************************************************************
70 * PathCombine [SHELL32.37]
72 LPVOID WINAPI PathCombineAW(
73 LPVOID szDest,
74 LPCVOID lpszDir,
75 LPCVOID lpszFile)
77 if (SHELL_OsIsUnicode())
78 return PathCombineW( szDest, lpszDir, lpszFile );
79 return PathCombineA( szDest, lpszDir, lpszFile );
82 /*************************************************************************
83 * PathAddBackslash [SHELL32.32]
85 LPVOID WINAPI PathAddBackslashAW(LPVOID lpszPath)
87 if(SHELL_OsIsUnicode())
88 return PathAddBackslashW(lpszPath);
89 return PathAddBackslashA(lpszPath);
92 /*************************************************************************
93 * PathBuildRoot [SHELL32.30]
95 LPVOID WINAPI PathBuildRootAW(LPVOID lpszPath, int drive)
97 if(SHELL_OsIsUnicode())
98 return PathBuildRootW(lpszPath, drive);
99 return PathBuildRootA(lpszPath, drive);
103 Extracting Component Parts
106 /*************************************************************************
107 * PathFindFileName [SHELL32.34]
109 LPVOID WINAPI PathFindFileNameAW(LPCVOID lpszPath)
111 if(SHELL_OsIsUnicode())
112 return PathFindFileNameW(lpszPath);
113 return PathFindFileNameA(lpszPath);
116 /*************************************************************************
117 * PathFindExtension [SHELL32.31]
119 LPVOID WINAPI PathFindExtensionAW(LPCVOID lpszPath)
121 if (SHELL_OsIsUnicode())
122 return PathFindExtensionW(lpszPath);
123 return PathFindExtensionA(lpszPath);
127 /*************************************************************************
128 * PathGetExtensionA [internal]
130 * NOTES
131 * exported by ordinal
132 * return value points to the first char after the dot
134 static LPSTR PathGetExtensionA(LPCSTR lpszPath)
136 TRACE("(%s)\n",lpszPath);
138 lpszPath = PathFindExtensionA(lpszPath);
139 return (LPSTR)(*lpszPath?(lpszPath+1):lpszPath);
142 /*************************************************************************
143 * PathGetExtensionW [internal]
145 static LPWSTR PathGetExtensionW(LPCWSTR lpszPath)
147 TRACE("(%s)\n",debugstr_w(lpszPath));
149 lpszPath = PathFindExtensionW(lpszPath);
150 return (LPWSTR)(*lpszPath?(lpszPath+1):lpszPath);
153 /*************************************************************************
154 * PathGetExtension [SHELL32.158]
156 LPVOID WINAPI PathGetExtensionAW(LPCVOID lpszPath,DWORD void1, DWORD void2)
158 if (SHELL_OsIsUnicode())
159 return PathGetExtensionW(lpszPath);
160 return PathGetExtensionA(lpszPath);
163 /*************************************************************************
164 * PathGetArgs [SHELL32.52]
166 LPVOID WINAPI PathGetArgsAW(LPVOID lpszPath)
168 if (SHELL_OsIsUnicode())
169 return PathGetArgsW(lpszPath);
170 return PathGetArgsA(lpszPath);
173 /*************************************************************************
174 * PathGetDriveNumber [SHELL32.57]
176 int WINAPI PathGetDriveNumberAW(LPVOID lpszPath)
178 if (SHELL_OsIsUnicode())
179 return PathGetDriveNumberW(lpszPath);
180 return PathGetDriveNumberA(lpszPath);
183 /*************************************************************************
184 * PathRemoveFileSpec [SHELL32.35]
186 BOOL WINAPI PathRemoveFileSpecAW(LPVOID lpszPath)
188 if (SHELL_OsIsUnicode())
189 return PathRemoveFileSpecW(lpszPath);
190 return PathRemoveFileSpecA(lpszPath);
193 /*************************************************************************
194 * PathStripPath [SHELL32.38]
196 void WINAPI PathStripPathAW(LPVOID lpszPath)
198 if (SHELL_OsIsUnicode())
199 PathStripPathW(lpszPath);
200 else
201 PathStripPathA(lpszPath);
204 /*************************************************************************
205 * PathStripToRoot [SHELL32.50]
207 BOOL WINAPI PathStripToRootAW(LPVOID lpszPath)
209 if (SHELL_OsIsUnicode())
210 return PathStripToRootW(lpszPath);
211 return PathStripToRootA(lpszPath);
214 /*************************************************************************
215 * PathRemoveArgs [SHELL32.251]
217 void WINAPI PathRemoveArgsAW(LPVOID lpszPath)
219 if (SHELL_OsIsUnicode())
220 PathRemoveArgsW(lpszPath);
221 else
222 PathRemoveArgsA(lpszPath);
225 /*************************************************************************
226 * PathRemoveExtension [SHELL32.250]
228 void WINAPI PathRemoveExtensionAW(LPVOID lpszPath)
230 if (SHELL_OsIsUnicode())
231 PathRemoveExtensionW(lpszPath);
232 else
233 PathRemoveExtensionA(lpszPath);
238 Path Manipulations
241 /*************************************************************************
242 * PathGetShortPathA [internal]
244 static void PathGetShortPathA(LPSTR pszPath)
246 CHAR path[MAX_PATH];
248 TRACE("%s\n", pszPath);
250 if (GetShortPathNameA(pszPath, path, MAX_PATH))
252 lstrcpyA(pszPath, path);
256 /*************************************************************************
257 * PathGetShortPathW [internal]
259 static void PathGetShortPathW(LPWSTR pszPath)
261 WCHAR path[MAX_PATH];
263 TRACE("%s\n", debugstr_w(pszPath));
265 if (GetShortPathNameW(pszPath, path, MAX_PATH))
267 lstrcpyW(pszPath, path);
271 /*************************************************************************
272 * PathGetShortPath [SHELL32.92]
274 VOID WINAPI PathGetShortPathAW(LPVOID pszPath)
276 if(SHELL_OsIsUnicode())
277 PathGetShortPathW(pszPath);
278 PathGetShortPathA(pszPath);
281 /*************************************************************************
282 * PathRemoveBlanks [SHELL32.33]
284 void WINAPI PathRemoveBlanksAW(LPVOID str)
286 if(SHELL_OsIsUnicode())
287 PathRemoveBlanksW(str);
288 else
289 PathRemoveBlanksA(str);
292 /*************************************************************************
293 * PathQuoteSpaces [SHELL32.55]
295 VOID WINAPI PathQuoteSpacesAW (LPVOID lpszPath)
297 if(SHELL_OsIsUnicode())
298 PathQuoteSpacesW(lpszPath);
299 else
300 PathQuoteSpacesA(lpszPath);
303 /*************************************************************************
304 * PathUnquoteSpaces [SHELL32.56]
306 VOID WINAPI PathUnquoteSpacesAW(LPVOID str)
308 if(SHELL_OsIsUnicode())
309 PathUnquoteSpacesW(str);
310 else
311 PathUnquoteSpacesA(str);
314 /*************************************************************************
315 * PathParseIconLocation [SHELL32.249]
317 int WINAPI PathParseIconLocationAW (LPVOID lpszPath)
319 if(SHELL_OsIsUnicode())
320 return PathParseIconLocationW(lpszPath);
321 return PathParseIconLocationA(lpszPath);
325 ########## Path Testing ##########
327 /*************************************************************************
328 * PathIsUNC [SHELL32.39]
330 BOOL WINAPI PathIsUNCAW (LPCVOID lpszPath)
332 if (SHELL_OsIsUnicode())
333 return PathIsUNCW( lpszPath );
334 return PathIsUNCA( lpszPath );
337 /*************************************************************************
338 * PathIsRelative [SHELL32.40]
340 BOOL WINAPI PathIsRelativeAW (LPCVOID lpszPath)
342 if (SHELL_OsIsUnicode())
343 return PathIsRelativeW( lpszPath );
344 return PathIsRelativeA( lpszPath );
347 /*************************************************************************
348 * PathIsRoot [SHELL32.29]
350 BOOL WINAPI PathIsRootAW(LPCVOID lpszPath)
352 if (SHELL_OsIsUnicode())
353 return PathIsRootW(lpszPath);
354 return PathIsRootA(lpszPath);
357 /*************************************************************************
358 * PathIsExeA [internal]
360 static BOOL PathIsExeA (LPCSTR lpszPath)
362 LPCSTR lpszExtension = PathGetExtensionA(lpszPath);
363 int i;
364 static const char * const lpszExtensions[] =
365 {"exe", "com", "pif", "cmd", "bat", "scf", "scr", NULL };
367 TRACE("path=%s\n",lpszPath);
369 for(i=0; lpszExtensions[i]; i++)
370 if (!lstrcmpiA(lpszExtension,lpszExtensions[i])) return TRUE;
372 return FALSE;
375 /*************************************************************************
376 * PathIsExeW [internal]
378 static BOOL PathIsExeW (LPCWSTR lpszPath)
380 LPCWSTR lpszExtension = PathGetExtensionW(lpszPath);
381 int i;
382 static const WCHAR lpszExtensions[][4] =
383 {{'e','x','e','\0'}, {'c','o','m','\0'}, {'p','i','f','\0'},
384 {'c','m','d','\0'}, {'b','a','t','\0'}, {'s','c','f','\0'},
385 {'s','c','r','\0'}, {'\0'} };
387 TRACE("path=%s\n",debugstr_w(lpszPath));
389 for(i=0; lpszExtensions[i][0]; i++)
390 if (!strcmpiW(lpszExtension,lpszExtensions[i])) return TRUE;
392 return FALSE;
395 /*************************************************************************
396 * PathIsExe [SHELL32.43]
398 BOOL WINAPI PathIsExeAW (LPCVOID path)
400 if (SHELL_OsIsUnicode())
401 return PathIsExeW (path);
402 return PathIsExeA(path);
405 /*************************************************************************
406 * PathIsDirectory [SHELL32.159]
408 BOOL WINAPI PathIsDirectoryAW (LPCVOID lpszPath)
410 if (SHELL_OsIsUnicode())
411 return PathIsDirectoryW (lpszPath);
412 return PathIsDirectoryA (lpszPath);
415 /*************************************************************************
416 * PathFileExists [SHELL32.45]
418 BOOL WINAPI PathFileExistsAW (LPCVOID lpszPath)
420 if (SHELL_OsIsUnicode())
421 return PathFileExistsW (lpszPath);
422 return PathFileExistsA (lpszPath);
425 /*************************************************************************
426 * PathMatchSpec [SHELL32.46]
428 BOOL WINAPI PathMatchSpecAW(LPVOID name, LPVOID mask)
430 if (SHELL_OsIsUnicode())
431 return PathMatchSpecW( name, mask );
432 return PathMatchSpecA( name, mask );
435 /*************************************************************************
436 * PathIsSameRoot [SHELL32.650]
438 BOOL WINAPI PathIsSameRootAW(LPCVOID lpszPath1, LPCVOID lpszPath2)
440 if (SHELL_OsIsUnicode())
441 return PathIsSameRootW(lpszPath1, lpszPath2);
442 return PathIsSameRootA(lpszPath1, lpszPath2);
445 /*************************************************************************
446 * IsLFNDriveA [SHELL32.41]
448 BOOL WINAPI IsLFNDriveA(LPCSTR lpszPath)
450 DWORD fnlen;
452 if (!GetVolumeInformationA(lpszPath, NULL, 0, NULL, &fnlen, NULL, NULL, 0))
453 return FALSE;
454 return fnlen > 12;
457 /*************************************************************************
458 * IsLFNDriveW [SHELL32.42]
460 BOOL WINAPI IsLFNDriveW(LPCWSTR lpszPath)
462 DWORD fnlen;
464 if (!GetVolumeInformationW(lpszPath, NULL, 0, NULL, &fnlen, NULL, NULL, 0))
465 return FALSE;
466 return fnlen > 12;
469 /*************************************************************************
470 * IsLFNDrive [SHELL32.119]
472 BOOL WINAPI IsLFNDriveAW(LPCVOID lpszPath)
474 if (SHELL_OsIsUnicode())
475 return IsLFNDriveW(lpszPath);
476 return IsLFNDriveA(lpszPath);
480 ########## Creating Something Unique ##########
482 /*************************************************************************
483 * PathMakeUniqueNameA [internal]
485 BOOL WINAPI PathMakeUniqueNameA(
486 LPSTR lpszBuffer,
487 DWORD dwBuffSize,
488 LPCSTR lpszShortName,
489 LPCSTR lpszLongName,
490 LPCSTR lpszPathName)
492 FIXME("%p %u %s %s %s stub\n",
493 lpszBuffer, dwBuffSize, debugstr_a(lpszShortName),
494 debugstr_a(lpszLongName), debugstr_a(lpszPathName));
495 return TRUE;
498 /*************************************************************************
499 * PathMakeUniqueNameW [internal]
501 BOOL WINAPI PathMakeUniqueNameW(
502 LPWSTR lpszBuffer,
503 DWORD dwBuffSize,
504 LPCWSTR lpszShortName,
505 LPCWSTR lpszLongName,
506 LPCWSTR lpszPathName)
508 FIXME("%p %u %s %s %s stub\n",
509 lpszBuffer, dwBuffSize, debugstr_w(lpszShortName),
510 debugstr_w(lpszLongName), debugstr_w(lpszPathName));
511 return TRUE;
514 /*************************************************************************
515 * PathMakeUniqueName [SHELL32.47]
517 BOOL WINAPI PathMakeUniqueNameAW(
518 LPVOID lpszBuffer,
519 DWORD dwBuffSize,
520 LPCVOID lpszShortName,
521 LPCVOID lpszLongName,
522 LPCVOID lpszPathName)
524 if (SHELL_OsIsUnicode())
525 return PathMakeUniqueNameW(lpszBuffer,dwBuffSize, lpszShortName,lpszLongName,lpszPathName);
526 return PathMakeUniqueNameA(lpszBuffer,dwBuffSize, lpszShortName,lpszLongName,lpszPathName);
529 /*************************************************************************
530 * PathYetAnotherMakeUniqueName [SHELL32.75]
532 * NOTES
533 * exported by ordinal
535 BOOL WINAPI PathYetAnotherMakeUniqueName(
536 LPWSTR lpszBuffer,
537 LPCWSTR lpszPathName,
538 LPCWSTR lpszShortName,
539 LPCWSTR lpszLongName)
541 FIXME("(%p, %s, %s ,%s):stub.\n",
542 lpszBuffer, debugstr_w(lpszPathName), debugstr_w(lpszShortName), debugstr_w(lpszLongName));
543 return TRUE;
548 ########## cleaning and resolving paths ##########
551 /*************************************************************************
552 * PathFindOnPath [SHELL32.145]
554 BOOL WINAPI PathFindOnPathAW(LPVOID sFile, LPCVOID *sOtherDirs)
556 if (SHELL_OsIsUnicode())
557 return PathFindOnPathW(sFile, (LPCWSTR *)sOtherDirs);
558 return PathFindOnPathA(sFile, (LPCSTR *)sOtherDirs);
561 /*************************************************************************
562 * PathCleanupSpec [SHELL32.171]
564 * lpszFile is changed in place.
566 int WINAPI PathCleanupSpec( LPCWSTR lpszPathW, LPWSTR lpszFileW )
568 int i = 0;
569 DWORD rc = 0;
570 int length = 0;
572 if (SHELL_OsIsUnicode())
574 LPWSTR p = lpszFileW;
576 TRACE("Cleanup %s\n",debugstr_w(lpszFileW));
578 if (lpszPathW)
579 length = strlenW(lpszPathW);
581 while (*p)
583 int gct = PathGetCharTypeW(*p);
584 if (gct == GCT_INVALID || gct == GCT_WILD || gct == GCT_SEPARATOR)
586 lpszFileW[i]='-';
587 rc |= PCS_REPLACEDCHAR;
589 else
590 lpszFileW[i]=*p;
591 i++;
592 p++;
593 if (length + i == MAX_PATH)
595 rc |= PCS_FATAL | PCS_PATHTOOLONG;
596 break;
599 lpszFileW[i]=0;
601 else
603 LPSTR lpszFileA = (LPSTR)lpszFileW;
604 LPCSTR lpszPathA = (LPCSTR)lpszPathW;
605 LPSTR p = lpszFileA;
607 TRACE("Cleanup %s\n",debugstr_a(lpszFileA));
609 if (lpszPathA)
610 length = strlen(lpszPathA);
612 while (*p)
614 int gct = PathGetCharTypeA(*p);
615 if (gct == GCT_INVALID || gct == GCT_WILD || gct == GCT_SEPARATOR)
617 lpszFileA[i]='-';
618 rc |= PCS_REPLACEDCHAR;
620 else
621 lpszFileA[i]=*p;
622 i++;
623 p++;
624 if (length + i == MAX_PATH)
626 rc |= PCS_FATAL | PCS_PATHTOOLONG;
627 break;
630 lpszFileA[i]=0;
632 return rc;
635 /*************************************************************************
636 * PathQualifyA [SHELL32]
638 BOOL WINAPI PathQualifyA(LPCSTR pszPath)
640 FIXME("%s\n",pszPath);
641 return 0;
644 /*************************************************************************
645 * PathQualifyW [SHELL32]
647 BOOL WINAPI PathQualifyW(LPCWSTR pszPath)
649 FIXME("%s\n",debugstr_w(pszPath));
650 return 0;
653 /*************************************************************************
654 * PathQualify [SHELL32.49]
656 BOOL WINAPI PathQualifyAW(LPCVOID pszPath)
658 if (SHELL_OsIsUnicode())
659 return PathQualifyW(pszPath);
660 return PathQualifyA(pszPath);
663 /*************************************************************************
664 * PathResolveA [SHELL32.51]
666 BOOL WINAPI PathResolveA(
667 LPSTR lpszPath,
668 LPCSTR *alpszPaths,
669 DWORD dwFlags)
671 FIXME("(%s,%p,0x%08x),stub!\n",
672 lpszPath, *alpszPaths, dwFlags);
673 return 0;
676 /*************************************************************************
677 * PathResolveW [SHELL32]
679 BOOL WINAPI PathResolveW(
680 LPWSTR lpszPath,
681 LPCWSTR *alpszPaths,
682 DWORD dwFlags)
684 FIXME("(%s,%p,0x%08x),stub!\n",
685 debugstr_w(lpszPath), debugstr_w(*alpszPaths), dwFlags);
686 return 0;
689 /*************************************************************************
690 * PathResolve [SHELL32.51]
692 BOOL WINAPI PathResolveAW(
693 LPVOID lpszPath,
694 LPCVOID *alpszPaths,
695 DWORD dwFlags)
697 if (SHELL_OsIsUnicode())
698 return PathResolveW(lpszPath, (LPCWSTR*)alpszPaths, dwFlags);
699 return PathResolveA(lpszPath, (LPCSTR*)alpszPaths, dwFlags);
702 /*************************************************************************
703 * PathProcessCommandA [SHELL32.653]
705 LONG WINAPI PathProcessCommandA (
706 LPCSTR lpszPath,
707 LPSTR lpszBuff,
708 DWORD dwBuffSize,
709 DWORD dwFlags)
711 FIXME("%s %p 0x%04x 0x%04x stub\n",
712 lpszPath, lpszBuff, dwBuffSize, dwFlags);
713 if(!lpszPath) return -1;
714 if(lpszBuff) strcpy(lpszBuff, lpszPath);
715 return strlen(lpszPath);
718 /*************************************************************************
719 * PathProcessCommandW
721 LONG WINAPI PathProcessCommandW (
722 LPCWSTR lpszPath,
723 LPWSTR lpszBuff,
724 DWORD dwBuffSize,
725 DWORD dwFlags)
727 FIXME("(%s, %p, 0x%04x, 0x%04x) stub\n",
728 debugstr_w(lpszPath), lpszBuff, dwBuffSize, dwFlags);
729 if(!lpszPath) return -1;
730 if(lpszBuff) strcpyW(lpszBuff, lpszPath);
731 return strlenW(lpszPath);
734 /*************************************************************************
735 * PathProcessCommand (SHELL32.653)
737 LONG WINAPI PathProcessCommandAW (
738 LPCVOID lpszPath,
739 LPVOID lpszBuff,
740 DWORD dwBuffSize,
741 DWORD dwFlags)
743 if (SHELL_OsIsUnicode())
744 return PathProcessCommandW(lpszPath, lpszBuff, dwBuffSize, dwFlags);
745 return PathProcessCommandA(lpszPath, lpszBuff, dwBuffSize, dwFlags);
749 ########## special ##########
752 /*************************************************************************
753 * PathSetDlgItemPath (SHELL32.48)
755 VOID WINAPI PathSetDlgItemPathAW(HWND hDlg, int id, LPCVOID pszPath)
757 if (SHELL_OsIsUnicode())
758 PathSetDlgItemPathW(hDlg, id, pszPath);
759 else
760 PathSetDlgItemPathA(hDlg, id, pszPath);
763 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'};
764 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'};
765 static const WCHAR AppDataW[] = {'A','p','p','D','a','t','a','\0'};
766 static const WCHAR CacheW[] = {'C','a','c','h','e','\0'};
767 static const WCHAR CD_BurningW[] = {'C','D',' ','B','u','r','n','i','n','g','\0'};
768 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'};
769 static const WCHAR Common_AppDataW[] = {'C','o','m','m','o','n',' ','A','p','p','D','a','t','a','\0'};
770 static const WCHAR Common_DesktopW[] = {'C','o','m','m','o','n',' ','D','e','s','k','t','o','p','\0'};
771 static const WCHAR Common_DocumentsW[] = {'C','o','m','m','o','n',' ','D','o','c','u','m','e','n','t','s','\0'};
772 static const WCHAR CommonFilesDirW[] = {'C','o','m','m','o','n','F','i','l','e','s','D','i','r','\0'};
773 static const WCHAR CommonMusicW[] = {'C','o','m','m','o','n','M','u','s','i','c','\0'};
774 static const WCHAR CommonPicturesW[] = {'C','o','m','m','o','n','P','i','c','t','u','r','e','s','\0'};
775 static const WCHAR Common_ProgramsW[] = {'C','o','m','m','o','n',' ','P','r','o','g','r','a','m','s','\0'};
776 static const WCHAR Common_StartUpW[] = {'C','o','m','m','o','n',' ','S','t','a','r','t','U','p','\0'};
777 static const WCHAR Common_Start_MenuW[] = {'C','o','m','m','o','n',' ','S','t','a','r','t',' ','M','e','n','u','\0'};
778 static const WCHAR Common_TemplatesW[] = {'C','o','m','m','o','n',' ','T','e','m','p','l','a','t','e','s','\0'};
779 static const WCHAR CommonVideoW[] = {'C','o','m','m','o','n','V','i','d','e','o','\0'};
780 static const WCHAR CookiesW[] = {'C','o','o','k','i','e','s','\0'};
781 static const WCHAR DesktopW[] = {'D','e','s','k','t','o','p','\0'};
782 static const WCHAR FavoritesW[] = {'F','a','v','o','r','i','t','e','s','\0'};
783 static const WCHAR FontsW[] = {'F','o','n','t','s','\0'};
784 static const WCHAR HistoryW[] = {'H','i','s','t','o','r','y','\0'};
785 static const WCHAR Local_AppDataW[] = {'L','o','c','a','l',' ','A','p','p','D','a','t','a','\0'};
786 static const WCHAR My_MusicW[] = {'M','y',' ','M','u','s','i','c','\0'};
787 static const WCHAR My_PicturesW[] = {'M','y',' ','P','i','c','t','u','r','e','s','\0'};
788 static const WCHAR My_VideoW[] = {'M','y',' ','V','i','d','e','o','s','\0'};
789 static const WCHAR NetHoodW[] = {'N','e','t','H','o','o','d','\0'};
790 static const WCHAR PersonalW[] = {'P','e','r','s','o','n','a','l','\0'};
791 static const WCHAR PrintHoodW[] = {'P','r','i','n','t','H','o','o','d','\0'};
792 static const WCHAR ProgramFilesDirW[] = {'P','r','o','g','r','a','m','F','i','l','e','s','D','i','r','\0'};
793 static const WCHAR ProgramsW[] = {'P','r','o','g','r','a','m','s','\0'};
794 static const WCHAR RecentW[] = {'R','e','c','e','n','t','\0'};
795 static const WCHAR ResourcesW[] = {'R','e','s','o','u','r','c','e','s','\0'};
796 static const WCHAR SendToW[] = {'S','e','n','d','T','o','\0'};
797 static const WCHAR StartUpW[] = {'S','t','a','r','t','U','p','\0'};
798 static const WCHAR Start_MenuW[] = {'S','t','a','r','t',' ','M','e','n','u','\0'};
799 static const WCHAR TemplatesW[] = {'T','e','m','p','l','a','t','e','s','\0'};
800 static const WCHAR DefaultW[] = {'.','D','e','f','a','u','l','t','\0'};
801 static const WCHAR AllUsersProfileW[] = {'%','A','L','L','U','S','E','R','S','P','R','O','F','I','L','E','%','\0'};
802 static const WCHAR UserProfileW[] = {'%','U','S','E','R','P','R','O','F','I','L','E','%','\0'};
803 static const WCHAR SystemDriveW[] = {'%','S','y','s','t','e','m','D','r','i','v','e','%','\0'};
804 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};
805 static const WCHAR ProfilesDirectoryW[] = {'P','r','o','f','i','l','e','s','D','i','r','e','c','t','o','r','y',0};
806 static const WCHAR AllUsersProfileValueW[] = {'A','l','l','U','s','e','r','s','P','r','o','f','i','l','e','\0'};
807 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'};
808 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'};
809 /* This defaults to L"Documents and Settings" on Windows 2000/XP, but we're
810 * acting more Windows 9x-like for now.
812 static const WCHAR szDefaultProfileDirW[] = {'p','r','o','f','i','l','e','s','\0'};
813 static const WCHAR AllUsersW[] = {'A','l','l',' ','U','s','e','r','s','\0'};
815 typedef enum _CSIDL_Type {
816 CSIDL_Type_User,
817 CSIDL_Type_AllUsers,
818 CSIDL_Type_CurrVer,
819 CSIDL_Type_Disallowed,
820 CSIDL_Type_NonExistent,
821 CSIDL_Type_WindowsPath,
822 CSIDL_Type_SystemPath,
823 } CSIDL_Type;
825 typedef struct
827 CSIDL_Type type;
828 LPCWSTR szValueName;
829 LPCWSTR szDefaultPath; /* fallback string or resource ID */
830 } CSIDL_DATA;
832 static const CSIDL_DATA CSIDL_Data[] =
834 { /* 0x00 - CSIDL_DESKTOP */
835 CSIDL_Type_User,
836 DesktopW,
837 MAKEINTRESOURCEW(IDS_DESKTOPDIRECTORY)
839 { /* 0x01 - CSIDL_INTERNET */
840 CSIDL_Type_Disallowed,
841 NULL,
842 NULL
844 { /* 0x02 - CSIDL_PROGRAMS */
845 CSIDL_Type_User,
846 ProgramsW,
847 MAKEINTRESOURCEW(IDS_PROGRAMS)
849 { /* 0x03 - CSIDL_CONTROLS (.CPL files) */
850 CSIDL_Type_SystemPath,
851 NULL,
852 NULL
854 { /* 0x04 - CSIDL_PRINTERS */
855 CSIDL_Type_SystemPath,
856 NULL,
857 NULL
859 { /* 0x05 - CSIDL_PERSONAL */
860 CSIDL_Type_User,
861 PersonalW,
862 MAKEINTRESOURCEW(IDS_PERSONAL)
864 { /* 0x06 - CSIDL_FAVORITES */
865 CSIDL_Type_User,
866 FavoritesW,
867 MAKEINTRESOURCEW(IDS_FAVORITES)
869 { /* 0x07 - CSIDL_STARTUP */
870 CSIDL_Type_User,
871 StartUpW,
872 MAKEINTRESOURCEW(IDS_STARTUP)
874 { /* 0x08 - CSIDL_RECENT */
875 CSIDL_Type_User,
876 RecentW,
877 MAKEINTRESOURCEW(IDS_RECENT)
879 { /* 0x09 - CSIDL_SENDTO */
880 CSIDL_Type_User,
881 SendToW,
882 MAKEINTRESOURCEW(IDS_SENDTO)
884 { /* 0x0a - CSIDL_BITBUCKET - Recycle Bin */
885 CSIDL_Type_Disallowed,
886 NULL,
887 NULL,
889 { /* 0x0b - CSIDL_STARTMENU */
890 CSIDL_Type_User,
891 Start_MenuW,
892 MAKEINTRESOURCEW(IDS_STARTMENU)
894 { /* 0x0c - CSIDL_MYDOCUMENTS */
895 CSIDL_Type_Disallowed, /* matches WinXP--can't get its path */
896 NULL,
897 NULL
899 { /* 0x0d - CSIDL_MYMUSIC */
900 CSIDL_Type_User,
901 My_MusicW,
902 MAKEINTRESOURCEW(IDS_MYMUSIC)
904 { /* 0x0e - CSIDL_MYVIDEO */
905 CSIDL_Type_User,
906 My_VideoW,
907 MAKEINTRESOURCEW(IDS_MYVIDEO)
909 { /* 0x0f - unassigned */
910 CSIDL_Type_Disallowed,
911 NULL,
912 NULL,
914 { /* 0x10 - CSIDL_DESKTOPDIRECTORY */
915 CSIDL_Type_User,
916 DesktopW,
917 MAKEINTRESOURCEW(IDS_DESKTOPDIRECTORY)
919 { /* 0x11 - CSIDL_DRIVES */
920 CSIDL_Type_Disallowed,
921 NULL,
922 NULL,
924 { /* 0x12 - CSIDL_NETWORK */
925 CSIDL_Type_Disallowed,
926 NULL,
927 NULL,
929 { /* 0x13 - CSIDL_NETHOOD */
930 CSIDL_Type_User,
931 NetHoodW,
932 MAKEINTRESOURCEW(IDS_NETHOOD)
934 { /* 0x14 - CSIDL_FONTS */
935 CSIDL_Type_WindowsPath,
936 FontsW,
937 FontsW
939 { /* 0x15 - CSIDL_TEMPLATES */
940 CSIDL_Type_User,
941 TemplatesW,
942 MAKEINTRESOURCEW(IDS_TEMPLATES)
944 { /* 0x16 - CSIDL_COMMON_STARTMENU */
945 CSIDL_Type_AllUsers,
946 Common_Start_MenuW,
947 MAKEINTRESOURCEW(IDS_STARTMENU)
949 { /* 0x17 - CSIDL_COMMON_PROGRAMS */
950 CSIDL_Type_AllUsers,
951 Common_ProgramsW,
952 MAKEINTRESOURCEW(IDS_PROGRAMS)
954 { /* 0x18 - CSIDL_COMMON_STARTUP */
955 CSIDL_Type_AllUsers,
956 Common_StartUpW,
957 MAKEINTRESOURCEW(IDS_STARTUP)
959 { /* 0x19 - CSIDL_COMMON_DESKTOPDIRECTORY */
960 CSIDL_Type_AllUsers,
961 Common_DesktopW,
962 MAKEINTRESOURCEW(IDS_DESKTOP)
964 { /* 0x1a - CSIDL_APPDATA */
965 CSIDL_Type_User,
966 AppDataW,
967 MAKEINTRESOURCEW(IDS_APPDATA)
969 { /* 0x1b - CSIDL_PRINTHOOD */
970 CSIDL_Type_User,
971 PrintHoodW,
972 MAKEINTRESOURCEW(IDS_PRINTHOOD)
974 { /* 0x1c - CSIDL_LOCAL_APPDATA */
975 CSIDL_Type_User,
976 Local_AppDataW,
977 MAKEINTRESOURCEW(IDS_LOCAL_APPDATA)
979 { /* 0x1d - CSIDL_ALTSTARTUP */
980 CSIDL_Type_NonExistent,
981 NULL,
982 NULL
984 { /* 0x1e - CSIDL_COMMON_ALTSTARTUP */
985 CSIDL_Type_NonExistent,
986 NULL,
987 NULL
989 { /* 0x1f - CSIDL_COMMON_FAVORITES */
990 CSIDL_Type_AllUsers,
991 FavoritesW,
992 MAKEINTRESOURCEW(IDS_FAVORITES)
994 { /* 0x20 - CSIDL_INTERNET_CACHE */
995 CSIDL_Type_User,
996 CacheW,
997 MAKEINTRESOURCEW(IDS_INTERNET_CACHE)
999 { /* 0x21 - CSIDL_COOKIES */
1000 CSIDL_Type_User,
1001 CookiesW,
1002 MAKEINTRESOURCEW(IDS_COOKIES)
1004 { /* 0x22 - CSIDL_HISTORY */
1005 CSIDL_Type_User,
1006 HistoryW,
1007 MAKEINTRESOURCEW(IDS_HISTORY)
1009 { /* 0x23 - CSIDL_COMMON_APPDATA */
1010 CSIDL_Type_AllUsers,
1011 Common_AppDataW,
1012 MAKEINTRESOURCEW(IDS_APPDATA)
1014 { /* 0x24 - CSIDL_WINDOWS */
1015 CSIDL_Type_WindowsPath,
1016 NULL,
1017 NULL
1019 { /* 0x25 - CSIDL_SYSTEM */
1020 CSIDL_Type_SystemPath,
1021 NULL,
1022 NULL
1024 { /* 0x26 - CSIDL_PROGRAM_FILES */
1025 CSIDL_Type_CurrVer,
1026 ProgramFilesDirW,
1027 MAKEINTRESOURCEW(IDS_PROGRAM_FILES)
1029 { /* 0x27 - CSIDL_MYPICTURES */
1030 CSIDL_Type_User,
1031 My_PicturesW,
1032 MAKEINTRESOURCEW(IDS_MYPICTURES)
1034 { /* 0x28 - CSIDL_PROFILE */
1035 CSIDL_Type_User,
1036 NULL,
1037 NULL
1039 { /* 0x29 - CSIDL_SYSTEMX86 */
1040 CSIDL_Type_NonExistent,
1041 NULL,
1042 NULL
1044 { /* 0x2a - CSIDL_PROGRAM_FILESX86 */
1045 CSIDL_Type_NonExistent,
1046 NULL,
1047 NULL
1049 { /* 0x2b - CSIDL_PROGRAM_FILES_COMMON */
1050 CSIDL_Type_CurrVer,
1051 CommonFilesDirW,
1052 MAKEINTRESOURCEW(IDS_PROGRAM_FILES_COMMON)
1054 { /* 0x2c - CSIDL_PROGRAM_FILES_COMMONX86 */
1055 CSIDL_Type_NonExistent,
1056 NULL,
1057 NULL
1059 { /* 0x2d - CSIDL_COMMON_TEMPLATES */
1060 CSIDL_Type_AllUsers,
1061 Common_TemplatesW,
1062 MAKEINTRESOURCEW(IDS_TEMPLATES)
1064 { /* 0x2e - CSIDL_COMMON_DOCUMENTS */
1065 CSIDL_Type_AllUsers,
1066 Common_DocumentsW,
1067 MAKEINTRESOURCEW(IDS_COMMON_DOCUMENTS)
1069 { /* 0x2f - CSIDL_COMMON_ADMINTOOLS */
1070 CSIDL_Type_AllUsers,
1071 Common_Administrative_ToolsW,
1072 MAKEINTRESOURCEW(IDS_ADMINTOOLS)
1074 { /* 0x30 - CSIDL_ADMINTOOLS */
1075 CSIDL_Type_User,
1076 Administrative_ToolsW,
1077 MAKEINTRESOURCEW(IDS_ADMINTOOLS)
1079 { /* 0x31 - CSIDL_CONNECTIONS */
1080 CSIDL_Type_Disallowed,
1081 NULL,
1082 NULL
1084 { /* 0x32 - unassigned */
1085 CSIDL_Type_Disallowed,
1086 NULL,
1087 NULL
1089 { /* 0x33 - unassigned */
1090 CSIDL_Type_Disallowed,
1091 NULL,
1092 NULL
1094 { /* 0x34 - unassigned */
1095 CSIDL_Type_Disallowed,
1096 NULL,
1097 NULL
1099 { /* 0x35 - CSIDL_COMMON_MUSIC */
1100 CSIDL_Type_AllUsers,
1101 CommonMusicW,
1102 MAKEINTRESOURCEW(IDS_COMMON_MUSIC)
1104 { /* 0x36 - CSIDL_COMMON_PICTURES */
1105 CSIDL_Type_AllUsers,
1106 CommonPicturesW,
1107 MAKEINTRESOURCEW(IDS_COMMON_PICTURES)
1109 { /* 0x37 - CSIDL_COMMON_VIDEO */
1110 CSIDL_Type_AllUsers,
1111 CommonVideoW,
1112 MAKEINTRESOURCEW(IDS_COMMON_VIDEO)
1114 { /* 0x38 - CSIDL_RESOURCES */
1115 CSIDL_Type_WindowsPath,
1116 NULL,
1117 ResourcesW
1119 { /* 0x39 - CSIDL_RESOURCES_LOCALIZED */
1120 CSIDL_Type_NonExistent,
1121 NULL,
1122 NULL
1124 { /* 0x3a - CSIDL_COMMON_OEM_LINKS */
1125 CSIDL_Type_NonExistent,
1126 NULL,
1127 NULL
1129 { /* 0x3b - CSIDL_CDBURN_AREA */
1130 CSIDL_Type_User,
1131 CD_BurningW,
1132 MAKEINTRESOURCEW(IDS_CDBURN_AREA)
1134 { /* 0x3c unassigned */
1135 CSIDL_Type_Disallowed,
1136 NULL,
1137 NULL
1139 { /* 0x3d - CSIDL_COMPUTERSNEARME */
1140 CSIDL_Type_Disallowed, /* FIXME */
1141 NULL,
1142 NULL
1144 { /* 0x3e - CSIDL_PROFILES */
1145 CSIDL_Type_Disallowed, /* oddly, this matches WinXP */
1146 NULL,
1147 NULL
1151 static HRESULT _SHExpandEnvironmentStrings(LPCWSTR szSrc, LPWSTR szDest);
1153 /* Gets the value named value from the registry key
1154 * rootKey\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
1155 * (or from rootKey\userPrefix\... if userPrefix is not NULL) into path, which
1156 * is assumed to be MAX_PATH WCHARs in length.
1157 * If it exists, expands the value and writes the expanded value to
1158 * rootKey\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders
1159 * Returns successful error code if the value was retrieved from the registry,
1160 * and a failure otherwise.
1162 static HRESULT _SHGetUserShellFolderPath(HKEY rootKey, LPCWSTR userPrefix,
1163 LPCWSTR value, LPWSTR path)
1165 HRESULT hr;
1166 WCHAR shellFolderPath[MAX_PATH], userShellFolderPath[MAX_PATH];
1167 LPCWSTR pShellFolderPath, pUserShellFolderPath;
1168 DWORD dwType, dwPathLen = MAX_PATH;
1169 HKEY userShellFolderKey, shellFolderKey;
1171 TRACE("%p,%s,%s,%p\n",rootKey, debugstr_w(userPrefix), debugstr_w(value),
1172 path);
1174 if (userPrefix)
1176 strcpyW(shellFolderPath, userPrefix);
1177 PathAddBackslashW(shellFolderPath);
1178 strcatW(shellFolderPath, szSHFolders);
1179 pShellFolderPath = shellFolderPath;
1180 strcpyW(userShellFolderPath, userPrefix);
1181 PathAddBackslashW(userShellFolderPath);
1182 strcatW(userShellFolderPath, szSHUserFolders);
1183 pUserShellFolderPath = userShellFolderPath;
1185 else
1187 pUserShellFolderPath = szSHUserFolders;
1188 pShellFolderPath = szSHFolders;
1191 if (RegCreateKeyW(rootKey, pShellFolderPath, &shellFolderKey))
1193 TRACE("Failed to create %s\n", debugstr_w(pShellFolderPath));
1194 return E_FAIL;
1196 if (RegCreateKeyW(rootKey, pUserShellFolderPath, &userShellFolderKey))
1198 TRACE("Failed to create %s\n",
1199 debugstr_w(pUserShellFolderPath));
1200 RegCloseKey(shellFolderKey);
1201 return E_FAIL;
1204 if (!RegQueryValueExW(userShellFolderKey, value, NULL, &dwType,
1205 (LPBYTE)path, &dwPathLen) && (dwType == REG_EXPAND_SZ || dwType == REG_SZ))
1207 LONG ret;
1209 path[dwPathLen / sizeof(WCHAR)] = '\0';
1210 if (dwType == REG_EXPAND_SZ && path[0] == '%')
1212 WCHAR szTemp[MAX_PATH];
1214 _SHExpandEnvironmentStrings(path, szTemp);
1215 lstrcpynW(path, szTemp, MAX_PATH);
1217 ret = RegSetValueExW(shellFolderKey, value, 0, REG_SZ, (LPBYTE)path,
1218 (strlenW(path) + 1) * sizeof(WCHAR));
1219 if (ret != ERROR_SUCCESS)
1220 hr = HRESULT_FROM_WIN32(ret);
1221 else
1222 hr = S_OK;
1224 else
1225 hr = E_FAIL;
1226 RegCloseKey(shellFolderKey);
1227 RegCloseKey(userShellFolderKey);
1228 TRACE("returning 0x%08x\n", hr);
1229 return hr;
1232 /* Gets a 'semi-expanded' default value of the CSIDL with index folder into
1233 * pszPath, based on the entries in CSIDL_Data. By semi-expanded, I mean:
1234 * - The entry's szDefaultPath may be either a string value or an integer
1235 * resource identifier. In the latter case, the string value of the resource
1236 * is written.
1237 * - Depending on the entry's type, the path may begin with an (unexpanded)
1238 * environment variable name. The caller is responsible for expanding
1239 * environment strings if so desired.
1240 * The types that are prepended with environment variables are:
1241 * CSIDL_Type_User: %USERPROFILE%
1242 * CSIDL_Type_AllUsers: %ALLUSERSPROFILE%
1243 * CSIDL_Type_CurrVer: %SystemDrive%
1244 * (Others might make sense too, but as yet are unneeded.)
1246 static HRESULT _SHGetDefaultValue(BYTE folder, LPWSTR pszPath)
1248 HRESULT hr;
1249 WCHAR resourcePath[MAX_PATH];
1250 LPCWSTR pDefaultPath = NULL;
1252 TRACE("0x%02x,%p\n", folder, pszPath);
1254 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1255 return E_INVALIDARG;
1256 if (!pszPath)
1257 return E_INVALIDARG;
1259 if (CSIDL_Data[folder].szDefaultPath &&
1260 IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath))
1262 if (LoadStringW(shell32_hInstance,
1263 LOWORD(CSIDL_Data[folder].szDefaultPath), resourcePath, MAX_PATH))
1265 hr = S_OK;
1266 pDefaultPath = resourcePath;
1268 else
1270 FIXME("(%d,%s), LoadString failed, missing translation?\n", folder,
1271 debugstr_w(pszPath));
1272 hr = E_FAIL;
1275 else
1277 hr = S_OK;
1278 pDefaultPath = CSIDL_Data[folder].szDefaultPath;
1280 if (SUCCEEDED(hr))
1282 switch (CSIDL_Data[folder].type)
1284 case CSIDL_Type_User:
1285 strcpyW(pszPath, UserProfileW);
1286 break;
1287 case CSIDL_Type_AllUsers:
1288 strcpyW(pszPath, AllUsersProfileW);
1289 break;
1290 case CSIDL_Type_CurrVer:
1291 strcpyW(pszPath, SystemDriveW);
1292 break;
1293 default:
1294 ; /* no corresponding env. var, do nothing */
1296 if (pDefaultPath)
1298 PathAddBackslashW(pszPath);
1299 strcatW(pszPath, pDefaultPath);
1302 TRACE("returning 0x%08x\n", hr);
1303 return hr;
1306 /* Gets the (unexpanded) value of the folder with index folder into pszPath.
1307 * The folder's type is assumed to be CSIDL_Type_CurrVer. Its default value
1308 * can be overridden in the HKLM\\szCurrentVersion key.
1309 * If dwFlags has SHGFP_TYPE_DEFAULT set or if the value isn't overridden in
1310 * the registry, uses _SHGetDefaultValue to get the value.
1312 static HRESULT _SHGetCurrentVersionPath(DWORD dwFlags, BYTE folder,
1313 LPWSTR pszPath)
1315 HRESULT hr;
1317 TRACE("0x%08x,0x%02x,%p\n", dwFlags, folder, pszPath);
1319 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1320 return E_INVALIDARG;
1321 if (CSIDL_Data[folder].type != CSIDL_Type_CurrVer)
1322 return E_INVALIDARG;
1323 if (!pszPath)
1324 return E_INVALIDARG;
1326 if (dwFlags & SHGFP_TYPE_DEFAULT)
1327 hr = _SHGetDefaultValue(folder, pszPath);
1328 else
1330 HKEY hKey;
1332 if (RegCreateKeyW(HKEY_LOCAL_MACHINE, szCurrentVersion, &hKey))
1333 hr = E_FAIL;
1334 else
1336 DWORD dwType, dwPathLen = MAX_PATH * sizeof(WCHAR);
1338 if (RegQueryValueExW(hKey, CSIDL_Data[folder].szValueName, NULL,
1339 &dwType, (LPBYTE)pszPath, &dwPathLen) ||
1340 (dwType != REG_SZ && dwType != REG_EXPAND_SZ))
1342 hr = _SHGetDefaultValue(folder, pszPath);
1343 dwType = REG_EXPAND_SZ;
1344 RegSetValueExW(hKey, CSIDL_Data[folder].szValueName, 0, dwType,
1345 (LPBYTE)pszPath, (strlenW(pszPath)+1)*sizeof(WCHAR));
1347 else
1349 pszPath[dwPathLen / sizeof(WCHAR)] = '\0';
1350 hr = S_OK;
1352 RegCloseKey(hKey);
1355 TRACE("returning 0x%08x (output path is %s)\n", hr, debugstr_w(pszPath));
1356 return hr;
1359 /* Gets the user's path (unexpanded) for the CSIDL with index folder:
1360 * If SHGFP_TYPE_DEFAULT is set, calls _SHGetDefaultValue for it. Otherwise
1361 * calls _SHGetUserShellFolderPath for it. Where it looks depends on hToken:
1362 * - if hToken is -1, looks in HKEY_USERS\.Default
1363 * - otherwise looks first in HKEY_CURRENT_USER, followed by HKEY_LOCAL_MACHINE
1364 * if HKEY_CURRENT_USER doesn't contain any entries. If both fail, finally
1365 * calls _SHGetDefaultValue for it.
1367 static HRESULT _SHGetUserProfilePath(HANDLE hToken, DWORD dwFlags, BYTE folder,
1368 LPWSTR pszPath)
1370 HRESULT hr;
1372 TRACE("%p,0x%08x,0x%02x,%p\n", hToken, dwFlags, folder, pszPath);
1374 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1375 return E_INVALIDARG;
1376 if (CSIDL_Data[folder].type != CSIDL_Type_User)
1377 return E_INVALIDARG;
1378 if (!pszPath)
1379 return E_INVALIDARG;
1381 /* Only the current user and the default user are supported right now
1382 * I'm afraid.
1383 * FIXME: should be able to call GetTokenInformation on the token,
1384 * then call ConvertSidToStringSidW on it to get the user prefix.
1385 * But Wine's registry doesn't store user info by sid, it stores it
1386 * by user name (and I don't know how to convert from a token to a
1387 * user name).
1389 if (hToken != NULL && hToken != (HANDLE)-1)
1391 FIXME("unsupported for user other than current or default\n");
1392 return E_FAIL;
1395 if (dwFlags & SHGFP_TYPE_DEFAULT)
1396 hr = _SHGetDefaultValue(folder, pszPath);
1397 else
1399 LPCWSTR userPrefix = NULL;
1400 HKEY hRootKey;
1402 if (hToken == (HANDLE)-1)
1404 hRootKey = HKEY_USERS;
1405 userPrefix = DefaultW;
1407 else /* hToken == NULL, other values disallowed above */
1408 hRootKey = HKEY_CURRENT_USER;
1409 hr = _SHGetUserShellFolderPath(hRootKey, userPrefix,
1410 CSIDL_Data[folder].szValueName, pszPath);
1411 if (FAILED(hr) && hRootKey != HKEY_LOCAL_MACHINE)
1412 hr = _SHGetUserShellFolderPath(HKEY_LOCAL_MACHINE, NULL,
1413 CSIDL_Data[folder].szValueName, pszPath);
1414 if (FAILED(hr))
1415 hr = _SHGetDefaultValue(folder, pszPath);
1417 TRACE("returning 0x%08x (output path is %s)\n", hr, debugstr_w(pszPath));
1418 return hr;
1421 /* Gets the (unexpanded) path for the CSIDL with index folder. If dwFlags has
1422 * SHGFP_TYPE_DEFAULT set, calls _SHGetDefaultValue. Otherwise calls
1423 * _SHGetUserShellFolderPath for it, looking only in HKEY_LOCAL_MACHINE.
1424 * If this fails, falls back to _SHGetDefaultValue.
1426 static HRESULT _SHGetAllUsersProfilePath(DWORD dwFlags, BYTE folder,
1427 LPWSTR pszPath)
1429 HRESULT hr;
1431 TRACE("0x%08x,0x%02x,%p\n", dwFlags, folder, pszPath);
1433 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1434 return E_INVALIDARG;
1435 if (CSIDL_Data[folder].type != CSIDL_Type_AllUsers)
1436 return E_INVALIDARG;
1437 if (!pszPath)
1438 return E_INVALIDARG;
1440 if (dwFlags & SHGFP_TYPE_DEFAULT)
1441 hr = _SHGetDefaultValue(folder, pszPath);
1442 else
1444 hr = _SHGetUserShellFolderPath(HKEY_LOCAL_MACHINE, NULL,
1445 CSIDL_Data[folder].szValueName, pszPath);
1446 if (FAILED(hr))
1447 hr = _SHGetDefaultValue(folder, pszPath);
1449 TRACE("returning 0x%08x (output path is %s)\n", hr, debugstr_w(pszPath));
1450 return hr;
1453 static HRESULT _SHOpenProfilesKey(PHKEY pKey)
1455 LONG lRet;
1456 DWORD disp;
1458 lRet = RegCreateKeyExW(HKEY_LOCAL_MACHINE, ProfileListW, 0, NULL, 0,
1459 KEY_ALL_ACCESS, NULL, pKey, &disp);
1460 return HRESULT_FROM_WIN32(lRet);
1463 /* Reads the value named szValueName from the key profilesKey (assumed to be
1464 * opened by _SHOpenProfilesKey) into szValue, which is assumed to be MAX_PATH
1465 * WCHARs in length. If it doesn't exist, returns szDefault (and saves
1466 * szDefault to the registry).
1468 static HRESULT _SHGetProfilesValue(HKEY profilesKey, LPCWSTR szValueName,
1469 LPWSTR szValue, LPCWSTR szDefault)
1471 HRESULT hr;
1472 DWORD type, dwPathLen = MAX_PATH * sizeof(WCHAR);
1473 LONG lRet;
1475 TRACE("%p,%s,%p,%s\n", profilesKey, debugstr_w(szValueName), szValue,
1476 debugstr_w(szDefault));
1477 lRet = RegQueryValueExW(profilesKey, szValueName, NULL, &type,
1478 (LPBYTE)szValue, &dwPathLen);
1479 if (!lRet && (type == REG_SZ || type == REG_EXPAND_SZ) && dwPathLen
1480 && *szValue)
1482 dwPathLen /= sizeof(WCHAR);
1483 szValue[dwPathLen] = '\0';
1484 hr = S_OK;
1486 else
1488 /* Missing or invalid value, set a default */
1489 lstrcpynW(szValue, szDefault, MAX_PATH);
1490 TRACE("Setting missing value %s to %s\n", debugstr_w(szValueName),
1491 debugstr_w(szValue));
1492 lRet = RegSetValueExW(profilesKey, szValueName, 0, REG_EXPAND_SZ,
1493 (LPBYTE)szValue,
1494 (strlenW(szValue) + 1) * sizeof(WCHAR));
1495 if (lRet)
1496 hr = HRESULT_FROM_WIN32(lRet);
1497 else
1498 hr = S_OK;
1500 TRACE("returning 0x%08x (output value is %s)\n", hr, debugstr_w(szValue));
1501 return hr;
1504 /* Attempts to expand environment variables from szSrc into szDest, which is
1505 * assumed to be MAX_PATH characters in length. Before referring to the
1506 * environment, handles a few variables directly, because the environment
1507 * variables may not be set when this is called (as during Wine's installation
1508 * when default values are being written to the registry).
1509 * The directly handled environment variables, and their source, are:
1510 * - ALLUSERSPROFILE, USERPROFILE: reads from the registry
1511 * - SystemDrive: uses GetSystemDirectoryW and uses the drive portion of its
1512 * path
1513 * If one of the directly handled environment variables is expanded, only
1514 * expands a single variable, and only in the beginning of szSrc.
1516 static HRESULT _SHExpandEnvironmentStrings(LPCWSTR szSrc, LPWSTR szDest)
1518 HRESULT hr;
1519 WCHAR szTemp[MAX_PATH], szProfilesPrefix[MAX_PATH] = { 0 };
1520 HKEY key = NULL;
1522 TRACE("%s, %p\n", debugstr_w(szSrc), szDest);
1524 if (!szSrc || !szDest) return E_INVALIDARG;
1526 /* short-circuit if there's nothing to expand */
1527 if (szSrc[0] != '%')
1529 strcpyW(szDest, szSrc);
1530 hr = S_OK;
1531 goto end;
1533 /* Get the profile prefix, we'll probably be needing it */
1534 hr = _SHOpenProfilesKey(&key);
1535 if (SUCCEEDED(hr))
1537 WCHAR szDefaultProfilesPrefix[MAX_PATH];
1539 GetWindowsDirectoryW(szDefaultProfilesPrefix, MAX_PATH);
1540 PathAddBackslashW(szDefaultProfilesPrefix);
1541 PathAppendW(szDefaultProfilesPrefix, szDefaultProfileDirW);
1542 hr = _SHGetProfilesValue(key, ProfilesDirectoryW, szProfilesPrefix,
1543 szDefaultProfilesPrefix);
1546 *szDest = 0;
1547 strcpyW(szTemp, szSrc);
1548 while (SUCCEEDED(hr) && szTemp[0] == '%')
1550 if (!strncmpiW(szTemp, AllUsersProfileW, strlenW(AllUsersProfileW)))
1552 WCHAR szAllUsers[MAX_PATH];
1554 strcpyW(szDest, szProfilesPrefix);
1555 hr = _SHGetProfilesValue(key, AllUsersProfileValueW,
1556 szAllUsers, AllUsersW);
1557 PathAppendW(szDest, szAllUsers);
1558 PathAppendW(szDest, szTemp + strlenW(AllUsersProfileW));
1560 else if (!strncmpiW(szTemp, UserProfileW, strlenW(UserProfileW)))
1562 WCHAR userName[MAX_PATH];
1563 DWORD userLen = MAX_PATH;
1565 strcpyW(szDest, szProfilesPrefix);
1566 GetUserNameW(userName, &userLen);
1567 PathAppendW(szDest, userName);
1568 PathAppendW(szDest, szTemp + strlenW(UserProfileW));
1570 else if (!strncmpiW(szTemp, SystemDriveW, strlenW(SystemDriveW)))
1572 GetSystemDirectoryW(szDest, MAX_PATH);
1573 if (szDest[1] != ':')
1575 FIXME("non-drive system paths unsupported\n");
1576 hr = E_FAIL;
1578 else
1580 strcpyW(szDest + 3, szTemp + strlenW(SystemDriveW) + 1);
1581 hr = S_OK;
1584 else
1586 DWORD ret = ExpandEnvironmentStringsW(szSrc, szDest, MAX_PATH);
1588 if (ret > MAX_PATH)
1589 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
1590 else if (ret == 0)
1591 hr = HRESULT_FROM_WIN32(GetLastError());
1592 else
1593 hr = S_OK;
1595 if (SUCCEEDED(hr) && szDest[0] == '%')
1596 strcpyW(szTemp, szDest);
1597 else
1599 /* terminate loop */
1600 szTemp[0] = '\0';
1603 end:
1604 if (key)
1605 RegCloseKey(key);
1606 TRACE("returning 0x%08x (input was %s, output is %s)\n", hr,
1607 debugstr_w(szSrc), debugstr_w(szDest));
1608 return hr;
1611 /*************************************************************************
1612 * SHGetFolderPathW [SHELL32.@]
1614 * Convert nFolder to path.
1616 * RETURNS
1617 * Success: S_OK
1618 * Failure: standard HRESULT error codes.
1620 * NOTES
1621 * Most values can be overridden in either
1622 * HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
1623 * or in the same location in HKLM.
1624 * The "Shell Folders" registry key was used in NT4 and earlier systems.
1625 * Beginning with Windows 2000, the "User Shell Folders" key is used, so
1626 * changes made to it are made to the former key too. This synchronization is
1627 * done on-demand: not until someone requests the value of one of these paths
1628 * (by calling one of the SHGet functions) is the value synchronized.
1629 * Furthermore, the HKCU paths take precedence over the HKLM paths.
1631 HRESULT WINAPI SHGetFolderPathW(
1632 HWND hwndOwner, /* [I] owner window */
1633 int nFolder, /* [I] CSIDL identifying the folder */
1634 HANDLE hToken, /* [I] access token */
1635 DWORD dwFlags, /* [I] which path to return */
1636 LPWSTR pszPath) /* [O] converted path */
1638 HRESULT hr;
1639 WCHAR szBuildPath[MAX_PATH], szTemp[MAX_PATH];
1640 DWORD folder = nFolder & CSIDL_FOLDER_MASK;
1641 CSIDL_Type type;
1642 int ret;
1644 TRACE("%p,%p,nFolder=0x%04x\n", hwndOwner,pszPath,nFolder);
1646 /* Windows always NULL-terminates the resulting path regardless of success
1647 * or failure, so do so first
1649 if (pszPath)
1650 *pszPath = '\0';
1651 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1652 return E_INVALIDARG;
1653 szTemp[0] = 0;
1654 type = CSIDL_Data[folder].type;
1655 switch (type)
1657 case CSIDL_Type_Disallowed:
1658 hr = E_INVALIDARG;
1659 break;
1660 case CSIDL_Type_NonExistent:
1661 hr = S_FALSE;
1662 break;
1663 case CSIDL_Type_WindowsPath:
1664 GetWindowsDirectoryW(szTemp, MAX_PATH);
1665 if (CSIDL_Data[folder].szDefaultPath &&
1666 !IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath) &&
1667 *CSIDL_Data[folder].szDefaultPath)
1669 PathAddBackslashW(szTemp);
1670 strcatW(szTemp, CSIDL_Data[folder].szDefaultPath);
1672 hr = S_OK;
1673 break;
1674 case CSIDL_Type_SystemPath:
1675 GetSystemDirectoryW(szTemp, MAX_PATH);
1676 if (CSIDL_Data[folder].szDefaultPath &&
1677 !IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath) &&
1678 *CSIDL_Data[folder].szDefaultPath)
1680 PathAddBackslashW(szTemp);
1681 strcatW(szTemp, CSIDL_Data[folder].szDefaultPath);
1683 hr = S_OK;
1684 break;
1685 case CSIDL_Type_CurrVer:
1686 hr = _SHGetCurrentVersionPath(dwFlags, folder, szTemp);
1687 break;
1688 case CSIDL_Type_User:
1689 hr = _SHGetUserProfilePath(hToken, dwFlags, folder, szTemp);
1690 break;
1691 case CSIDL_Type_AllUsers:
1692 hr = _SHGetAllUsersProfilePath(dwFlags, folder, szTemp);
1693 break;
1694 default:
1695 FIXME("bogus type %d, please fix\n", type);
1696 hr = E_INVALIDARG;
1697 break;
1700 /* Expand environment strings if necessary */
1701 if (*szTemp == '%')
1702 hr = _SHExpandEnvironmentStrings(szTemp, szBuildPath);
1703 else
1704 strcpyW(szBuildPath, szTemp);
1705 /* Copy the path if it's available before we might return */
1706 if (SUCCEEDED(hr) && pszPath)
1707 strcpyW(pszPath, szBuildPath);
1709 if (FAILED(hr)) goto end;
1711 /* if we don't care about existing directories we are ready */
1712 if(nFolder & CSIDL_FLAG_DONT_VERIFY) goto end;
1714 if (PathFileExistsW(szBuildPath)) goto end;
1716 /* not existing but we are not allowed to create it. The return value
1717 * is verified against shell32 version 6.0.
1719 if (!(nFolder & CSIDL_FLAG_CREATE))
1721 hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
1722 goto end;
1725 /* create directory/directories */
1726 ret = SHCreateDirectoryExW(hwndOwner, szBuildPath, NULL);
1727 if (ret && ret != ERROR_ALREADY_EXISTS)
1729 ERR("Failed to create directory %s.\n", debugstr_w(szBuildPath));
1730 hr = E_FAIL;
1731 goto end;
1734 TRACE("Created missing system directory %s\n", debugstr_w(szBuildPath));
1735 end:
1736 TRACE("returning 0x%08x (final path is %s)\n", hr, debugstr_w(szBuildPath));
1737 return hr;
1740 /*************************************************************************
1741 * SHGetFolderPathA [SHELL32.@]
1743 * See SHGetFolderPathW.
1745 HRESULT WINAPI SHGetFolderPathA(
1746 HWND hwndOwner,
1747 int nFolder,
1748 HANDLE hToken,
1749 DWORD dwFlags,
1750 LPSTR pszPath)
1752 WCHAR szTemp[MAX_PATH];
1753 HRESULT hr;
1755 TRACE("%p,%p,nFolder=0x%04x\n",hwndOwner,pszPath,nFolder);
1757 if (pszPath)
1758 *pszPath = '\0';
1759 hr = SHGetFolderPathW(hwndOwner, nFolder, hToken, dwFlags, szTemp);
1760 if (SUCCEEDED(hr) && pszPath)
1761 WideCharToMultiByte(CP_ACP, 0, szTemp, -1, pszPath, MAX_PATH, NULL,
1762 NULL);
1764 return hr;
1767 /* For each folder in folders, if its value has not been set in the registry,
1768 * calls _SHGetUserProfilePath or _SHGetAllUsersProfilePath (depending on the
1769 * folder's type) to get the unexpanded value first.
1770 * Writes the unexpanded value to User Shell Folders, and queries it with
1771 * SHGetFolderPathW to force the creation of the directory if it doesn't
1772 * already exist. SHGetFolderPathW also returns the expanded value, which
1773 * this then writes to Shell Folders.
1775 static HRESULT _SHRegisterFolders(HKEY hRootKey, HANDLE hToken,
1776 LPCWSTR szUserShellFolderPath, LPCWSTR szShellFolderPath, const UINT folders[],
1777 UINT foldersLen)
1779 UINT i;
1780 WCHAR path[MAX_PATH];
1781 HRESULT hr = S_OK;
1782 HKEY hUserKey = NULL, hKey = NULL;
1783 DWORD dwType, dwPathLen;
1784 LONG ret;
1786 TRACE("%p,%p,%s,%p,%u\n", hRootKey, hToken,
1787 debugstr_w(szUserShellFolderPath), folders, foldersLen);
1789 ret = RegCreateKeyW(hRootKey, szUserShellFolderPath, &hUserKey);
1790 if (ret)
1791 hr = HRESULT_FROM_WIN32(ret);
1792 else
1794 ret = RegCreateKeyW(hRootKey, szShellFolderPath, &hKey);
1795 if (ret)
1796 hr = HRESULT_FROM_WIN32(ret);
1798 for (i = 0; SUCCEEDED(hr) && i < foldersLen; i++)
1800 dwPathLen = MAX_PATH * sizeof(WCHAR);
1801 if (RegQueryValueExW(hUserKey, CSIDL_Data[folders[i]].szValueName, NULL,
1802 &dwType, (LPBYTE)path, &dwPathLen) || (dwType != REG_SZ &&
1803 dwType != REG_EXPAND_SZ))
1805 *path = '\0';
1806 if (CSIDL_Data[folders[i]].type == CSIDL_Type_User)
1807 _SHGetUserProfilePath(hToken, SHGFP_TYPE_DEFAULT, folders[i],
1808 path);
1809 else if (CSIDL_Data[folders[i]].type == CSIDL_Type_AllUsers)
1810 _SHGetAllUsersProfilePath(SHGFP_TYPE_DEFAULT, folders[i], path);
1811 else if (CSIDL_Data[folders[i]].type == CSIDL_Type_WindowsPath)
1812 GetWindowsDirectoryW(path, MAX_PATH);
1813 else
1814 hr = E_FAIL;
1815 if (*path)
1817 ret = RegSetValueExW(hUserKey,
1818 CSIDL_Data[folders[i]].szValueName, 0, REG_EXPAND_SZ,
1819 (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR));
1820 if (ret)
1821 hr = HRESULT_FROM_WIN32(ret);
1822 else
1824 hr = SHGetFolderPathW(NULL, folders[i] | CSIDL_FLAG_CREATE,
1825 hToken, SHGFP_TYPE_DEFAULT, path);
1826 ret = RegSetValueExW(hKey,
1827 CSIDL_Data[folders[i]].szValueName, 0, REG_SZ,
1828 (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR));
1829 if (ret)
1830 hr = HRESULT_FROM_WIN32(ret);
1835 if (hUserKey)
1836 RegCloseKey(hUserKey);
1837 if (hKey)
1838 RegCloseKey(hKey);
1840 TRACE("returning 0x%08x\n", hr);
1841 return hr;
1844 static HRESULT _SHRegisterUserShellFolders(BOOL bDefault)
1846 static const UINT folders[] = {
1847 CSIDL_PROGRAMS,
1848 CSIDL_PERSONAL,
1849 CSIDL_FAVORITES,
1850 CSIDL_APPDATA,
1851 CSIDL_STARTUP,
1852 CSIDL_RECENT,
1853 CSIDL_SENDTO,
1854 CSIDL_STARTMENU,
1855 CSIDL_MYMUSIC,
1856 CSIDL_MYVIDEO,
1857 CSIDL_DESKTOPDIRECTORY,
1858 CSIDL_NETHOOD,
1859 CSIDL_TEMPLATES,
1860 CSIDL_PRINTHOOD,
1861 CSIDL_LOCAL_APPDATA,
1862 CSIDL_INTERNET_CACHE,
1863 CSIDL_COOKIES,
1864 CSIDL_HISTORY,
1865 CSIDL_MYPICTURES,
1866 CSIDL_FONTS
1868 WCHAR userShellFolderPath[MAX_PATH], shellFolderPath[MAX_PATH];
1869 LPCWSTR pUserShellFolderPath, pShellFolderPath;
1870 HRESULT hr = S_OK;
1871 HKEY hRootKey;
1872 HANDLE hToken;
1874 TRACE("%s\n", bDefault ? "TRUE" : "FALSE");
1875 if (bDefault)
1877 hToken = (HANDLE)-1;
1878 hRootKey = HKEY_USERS;
1879 strcpyW(userShellFolderPath, DefaultW);
1880 PathAddBackslashW(userShellFolderPath);
1881 strcatW(userShellFolderPath, szSHUserFolders);
1882 pUserShellFolderPath = userShellFolderPath;
1883 strcpyW(shellFolderPath, DefaultW);
1884 PathAddBackslashW(shellFolderPath);
1885 strcatW(shellFolderPath, szSHFolders);
1886 pShellFolderPath = shellFolderPath;
1888 else
1890 hToken = NULL;
1891 hRootKey = HKEY_CURRENT_USER;
1892 pUserShellFolderPath = szSHUserFolders;
1893 pShellFolderPath = szSHFolders;
1896 hr = _SHRegisterFolders(hRootKey, hToken, pUserShellFolderPath,
1897 pShellFolderPath, folders, sizeof(folders) / sizeof(folders[0]));
1898 TRACE("returning 0x%08x\n", hr);
1899 return hr;
1902 static HRESULT _SHRegisterCommonShellFolders(void)
1904 static const UINT folders[] = {
1905 CSIDL_COMMON_STARTMENU,
1906 CSIDL_COMMON_PROGRAMS,
1907 CSIDL_COMMON_STARTUP,
1908 CSIDL_COMMON_DESKTOPDIRECTORY,
1909 CSIDL_COMMON_FAVORITES,
1910 CSIDL_COMMON_APPDATA,
1911 CSIDL_COMMON_TEMPLATES,
1912 CSIDL_COMMON_DOCUMENTS,
1914 HRESULT hr;
1916 TRACE("\n");
1917 hr = _SHRegisterFolders(HKEY_LOCAL_MACHINE, NULL, szSHUserFolders,
1918 szSHFolders, folders, sizeof(folders) / sizeof(folders[0]));
1919 TRACE("returning 0x%08x\n", hr);
1920 return hr;
1923 /******************************************************************************
1924 * _SHAppendToUnixPath [Internal]
1926 * Helper function for _SHCreateSymbolicLinks. Appends pwszSubPath (or the
1927 * corresponding resource, if IS_INTRESOURCE) to the unix base path 'szBasePath'
1928 * and replaces backslashes with slashes.
1930 * PARAMS
1931 * szBasePath [IO] The unix base path, which will be appended to (CP_UNXICP).
1932 * pwszSubPath [I] Sub-path or resource id (use MAKEINTRESOURCEW).
1934 * RETURNS
1935 * Success: TRUE,
1936 * Failure: FALSE
1938 static inline BOOL _SHAppendToUnixPath(char *szBasePath, LPCWSTR pwszSubPath) {
1939 WCHAR wszSubPath[MAX_PATH];
1940 int cLen = strlen(szBasePath);
1941 char *pBackslash;
1943 if (IS_INTRESOURCE(pwszSubPath)) {
1944 if (!LoadStringW(shell32_hInstance, LOWORD(pwszSubPath), wszSubPath, MAX_PATH)) {
1945 /* Fall back to hard coded defaults. */
1946 switch (LOWORD(pwszSubPath)) {
1947 case IDS_PERSONAL:
1948 lstrcpyW(wszSubPath, PersonalW);
1949 break;
1950 case IDS_MYMUSIC:
1951 lstrcpyW(wszSubPath, My_MusicW);
1952 break;
1953 case IDS_MYPICTURES:
1954 lstrcpyW(wszSubPath, My_PicturesW);
1955 break;
1956 case IDS_MYVIDEO:
1957 lstrcpyW(wszSubPath, My_VideoW);
1958 break;
1959 default:
1960 ERR("LoadString(%d) failed!\n", LOWORD(pwszSubPath));
1961 return FALSE;
1964 } else {
1965 lstrcpyW(wszSubPath, pwszSubPath);
1968 if (szBasePath[cLen-1] != '/') szBasePath[cLen++] = '/';
1970 if (!WideCharToMultiByte(CP_UNIXCP, 0, wszSubPath, -1, szBasePath + cLen,
1971 FILENAME_MAX - cLen, NULL, NULL))
1973 return FALSE;
1976 pBackslash = szBasePath + cLen;
1977 while ((pBackslash = strchr(pBackslash, '\\'))) *pBackslash = '/';
1979 return TRUE;
1982 /******************************************************************************
1983 * _SHCreateSymbolicLinks [Internal]
1985 * Sets up symbol links for various shell folders to point into the users home
1986 * directory. We do an educated guess about what the user would probably want:
1987 * - If there is a 'My Documents' directory in $HOME, the user probably wants
1988 * wine's 'My Documents' to point there. Furthermore, we imply that the user
1989 * is a Windows lover and has no problem with wine creating 'My Pictures',
1990 * 'My Music' and 'My Video' subfolders under '$HOME/My Documents', if those
1991 * do not already exits. We put appropriate symbolic links in place for those,
1992 * too.
1993 * - If there is no 'My Documents' directory in $HOME, we let 'My Documents'
1994 * point directly to $HOME. We assume the user to be a unix hacker who does not
1995 * want wine to create anything anywhere besides the .wine directory. So, if
1996 * there already is a 'My Music' directory in $HOME, we symlink the 'My Music'
1997 * shell folder to it. But if not, then we check XDG_MUSIC_DIR - "well known"
1998 * directory, and try to link to that. If that fails, then we symlink to
1999 * $HOME directly. The same holds fo 'My Pictures' and 'My Video'.
2000 * - The Desktop shell folder is symlinked to XDG_DESKTOP_DIR. If that does not
2001 * exist, then we try '$HOME/Desktop'. If that does not exist, then we leave
2002 * it alone.
2003 * ('My Music',... above in fact means LoadString(IDS_MYMUSIC))
2005 static void _SHCreateSymbolicLinks(void)
2007 UINT aidsMyStuff[] = { IDS_MYPICTURES, IDS_MYVIDEO, IDS_MYMUSIC }, i;
2008 int acsidlMyStuff[] = { CSIDL_MYPICTURES, CSIDL_MYVIDEO, CSIDL_MYMUSIC };
2009 static const char * xdg_dirs[] = { "PICTURES", "VIDEOS", "MUSIC", "DESKTOP" };
2010 WCHAR wszTempPath[MAX_PATH];
2011 char szPersonalTarget[FILENAME_MAX], *pszPersonal;
2012 char szMyStuffTarget[FILENAME_MAX], *pszMyStuff;
2013 char szDesktopTarget[FILENAME_MAX], *pszDesktop;
2014 struct stat statFolder;
2015 const char *pszHome;
2016 HRESULT hr;
2017 const unsigned int num = sizeof(xdg_dirs) / sizeof(xdg_dirs[0]);
2018 char ** xdg_results = NULL;
2019 char * xdg_desktop_dir;
2021 /* Create all necessary profile sub-dirs up to 'My Documents' and get the unix path. */
2022 hr = SHGetFolderPathW(NULL, CSIDL_PERSONAL|CSIDL_FLAG_CREATE, NULL,
2023 SHGFP_TYPE_DEFAULT, wszTempPath);
2024 if (FAILED(hr)) return;
2025 pszPersonal = wine_get_unix_file_name(wszTempPath);
2026 if (!pszPersonal) return;
2028 XDG_UserDirLookup(xdg_dirs, num, &xdg_results);
2030 pszHome = getenv("HOME");
2031 if (pszHome && !stat(pszHome, &statFolder) && S_ISDIR(statFolder.st_mode)) {
2032 strcpy(szPersonalTarget, pszHome);
2033 if (_SHAppendToUnixPath(szPersonalTarget, MAKEINTRESOURCEW(IDS_PERSONAL)) &&
2034 !stat(szPersonalTarget, &statFolder) && S_ISDIR(statFolder.st_mode))
2036 /* '$HOME/My Documents' exists. Create 'My Pictures', 'My Videos' and
2037 * 'My Music' subfolders or fail silently if they already exist. */
2038 for (i = 0; i < sizeof(aidsMyStuff)/sizeof(aidsMyStuff[0]); i++) {
2039 strcpy(szMyStuffTarget, szPersonalTarget);
2040 if (_SHAppendToUnixPath(szMyStuffTarget, MAKEINTRESOURCEW(aidsMyStuff[i])))
2041 mkdir(szMyStuffTarget, 0777);
2044 else
2046 /* '$HOME/My Documents' doesn't exists, but '$HOME' does. */
2047 strcpy(szPersonalTarget, pszHome);
2050 /* Replace 'My Documents' directory with a symlink of fail silently if not empty. */
2051 rmdir(pszPersonal);
2052 symlink(szPersonalTarget, pszPersonal);
2054 else
2056 /* '$HOME' doesn't exist. Create 'My Pictures', 'My Videos' and 'My Music' subdirs
2057 * in '%USERPROFILE%\\My Documents' or fail silently if they already exist. */
2058 strcpy(szPersonalTarget, pszPersonal);
2059 for (i = 0; i < sizeof(aidsMyStuff)/sizeof(aidsMyStuff[0]); i++) {
2060 strcpy(szMyStuffTarget, szPersonalTarget);
2061 if (_SHAppendToUnixPath(szMyStuffTarget, MAKEINTRESOURCEW(aidsMyStuff[i])))
2062 mkdir(szMyStuffTarget, 0777);
2066 /* Create symbolic links for 'My Pictures', 'My Video' and 'My Music'. */
2067 for (i=0; i < sizeof(aidsMyStuff)/sizeof(aidsMyStuff[0]); i++) {
2068 /* Create the current 'My Whatever' folder and get it's unix path. */
2069 hr = SHGetFolderPathW(NULL, acsidlMyStuff[i]|CSIDL_FLAG_CREATE, NULL,
2070 SHGFP_TYPE_DEFAULT, wszTempPath);
2071 if (FAILED(hr)) continue;
2072 pszMyStuff = wine_get_unix_file_name(wszTempPath);
2073 if (!pszMyStuff) continue;
2075 strcpy(szMyStuffTarget, szPersonalTarget);
2076 if (_SHAppendToUnixPath(szMyStuffTarget, MAKEINTRESOURCEW(aidsMyStuff[i])) &&
2077 !stat(szMyStuffTarget, &statFolder) && S_ISDIR(statFolder.st_mode))
2079 /* If there's a 'My Whatever' directory where 'My Documents' links to, link to it. */
2080 rmdir(pszMyStuff);
2081 symlink(szMyStuffTarget, pszMyStuff);
2083 else
2085 rmdir(pszMyStuff);
2086 if (xdg_results && xdg_results[i])
2088 /* the folder specified by XDG_XXX_DIR exists, link to it. */
2089 symlink(xdg_results[i], pszMyStuff);
2091 else
2093 /* Else link to where 'My Documents' itself links to. */
2094 symlink(szPersonalTarget, pszMyStuff);
2097 HeapFree(GetProcessHeap(), 0, pszMyStuff);
2100 /* Last but not least, the Desktop folder */
2101 if (pszHome)
2102 strcpy(szDesktopTarget, pszHome);
2103 else
2104 strcpy(szDesktopTarget, pszPersonal);
2105 HeapFree(GetProcessHeap(), 0, pszPersonal);
2107 xdg_desktop_dir = xdg_results ? xdg_results[num - 1] : NULL;
2108 if (xdg_desktop_dir ||
2109 (_SHAppendToUnixPath(szDesktopTarget, DesktopW) &&
2110 !stat(szDesktopTarget, &statFolder) && S_ISDIR(statFolder.st_mode)))
2112 hr = SHGetFolderPathW(NULL, CSIDL_DESKTOPDIRECTORY|CSIDL_FLAG_CREATE, NULL,
2113 SHGFP_TYPE_DEFAULT, wszTempPath);
2114 if (SUCCEEDED(hr) && (pszDesktop = wine_get_unix_file_name(wszTempPath)))
2116 rmdir(pszDesktop);
2117 if (xdg_desktop_dir)
2118 symlink(xdg_desktop_dir, pszDesktop);
2119 else
2120 symlink(szDesktopTarget, pszDesktop);
2121 HeapFree(GetProcessHeap(), 0, pszDesktop);
2125 /* Free resources allocated by XDG_UserDirLookup() */
2126 if (xdg_results)
2128 for (i = 0; i < num; i++)
2129 HeapFree(GetProcessHeap(), 0, xdg_results[i]);
2130 HeapFree(GetProcessHeap(), 0, xdg_results);
2134 /* Register the default values in the registry, as some apps seem to depend
2135 * on their presence. The set registered was taken from Windows XP.
2137 HRESULT SHELL_RegisterShellFolders(void)
2139 HRESULT hr;
2141 /* Set up '$HOME' targeted symlinks for 'My Documents', 'My Pictures',
2142 * 'My Video', 'My Music' and 'Desktop' in advance, so that the
2143 * _SHRegister*ShellFolders() functions will find everything nice and clean
2144 * and thus will not attempt to create them in the profile directory. */
2145 _SHCreateSymbolicLinks();
2147 hr = _SHRegisterUserShellFolders(TRUE);
2148 if (SUCCEEDED(hr))
2149 hr = _SHRegisterUserShellFolders(FALSE);
2150 if (SUCCEEDED(hr))
2151 hr = _SHRegisterCommonShellFolders();
2152 return hr;
2155 /*************************************************************************
2156 * SHGetSpecialFolderPathA [SHELL32.@]
2158 BOOL WINAPI SHGetSpecialFolderPathA (
2159 HWND hwndOwner,
2160 LPSTR szPath,
2161 int nFolder,
2162 BOOL bCreate)
2164 return (SHGetFolderPathA(
2165 hwndOwner,
2166 nFolder + (bCreate ? CSIDL_FLAG_CREATE : 0),
2167 NULL,
2169 szPath)) == S_OK ? TRUE : FALSE;
2172 /*************************************************************************
2173 * SHGetSpecialFolderPathW
2175 BOOL WINAPI SHGetSpecialFolderPathW (
2176 HWND hwndOwner,
2177 LPWSTR szPath,
2178 int nFolder,
2179 BOOL bCreate)
2181 return (SHGetFolderPathW(
2182 hwndOwner,
2183 nFolder + (bCreate ? CSIDL_FLAG_CREATE : 0),
2184 NULL,
2186 szPath)) == S_OK ? TRUE : FALSE;
2189 /*************************************************************************
2190 * SHGetSpecialFolderPath (SHELL32.175)
2192 BOOL WINAPI SHGetSpecialFolderPathAW (
2193 HWND hwndOwner,
2194 LPVOID szPath,
2195 int nFolder,
2196 BOOL bCreate)
2199 if (SHELL_OsIsUnicode())
2200 return SHGetSpecialFolderPathW (hwndOwner, szPath, nFolder, bCreate);
2201 return SHGetSpecialFolderPathA (hwndOwner, szPath, nFolder, bCreate);
2204 /*************************************************************************
2205 * SHGetFolderLocation [SHELL32.@]
2207 * Gets the folder locations from the registry and creates a pidl.
2209 * PARAMS
2210 * hwndOwner [I]
2211 * nFolder [I] CSIDL_xxxxx
2212 * hToken [I] token representing user, or NULL for current user, or -1 for
2213 * default user
2214 * dwReserved [I] must be zero
2215 * ppidl [O] PIDL of a special folder
2217 * RETURNS
2218 * Success: S_OK
2219 * Failure: Standard OLE-defined error result, S_FALSE or E_INVALIDARG
2221 * NOTES
2222 * Creates missing reg keys and directories.
2223 * Mostly forwards to SHGetFolderPathW, but a few values of nFolder return
2224 * virtual folders that are handled here.
2226 HRESULT WINAPI SHGetFolderLocation(
2227 HWND hwndOwner,
2228 int nFolder,
2229 HANDLE hToken,
2230 DWORD dwReserved,
2231 LPITEMIDLIST *ppidl)
2233 HRESULT hr = E_INVALIDARG;
2235 TRACE("%p 0x%08x %p 0x%08x %p\n",
2236 hwndOwner, nFolder, hToken, dwReserved, ppidl);
2238 if (!ppidl)
2239 return E_INVALIDARG;
2240 if (dwReserved)
2241 return E_INVALIDARG;
2243 /* The virtual folders' locations are not user-dependent */
2244 *ppidl = NULL;
2245 switch (nFolder)
2247 case CSIDL_DESKTOP:
2248 *ppidl = _ILCreateDesktop();
2249 break;
2251 case CSIDL_PERSONAL:
2252 *ppidl = _ILCreateMyDocuments();
2253 break;
2255 case CSIDL_INTERNET:
2256 *ppidl = _ILCreateIExplore();
2257 break;
2259 case CSIDL_CONTROLS:
2260 *ppidl = _ILCreateControlPanel();
2261 break;
2263 case CSIDL_PRINTERS:
2264 *ppidl = _ILCreatePrinters();
2265 break;
2267 case CSIDL_BITBUCKET:
2268 *ppidl = _ILCreateBitBucket();
2269 break;
2271 case CSIDL_DRIVES:
2272 *ppidl = _ILCreateMyComputer();
2273 break;
2275 case CSIDL_NETWORK:
2276 *ppidl = _ILCreateNetwork();
2277 break;
2279 default:
2281 WCHAR szPath[MAX_PATH];
2283 hr = SHGetFolderPathW(hwndOwner, nFolder, hToken,
2284 SHGFP_TYPE_CURRENT, szPath);
2285 if (SUCCEEDED(hr))
2287 DWORD attributes=0;
2289 TRACE("Value=%s\n", debugstr_w(szPath));
2290 hr = SHILCreateFromPathW(szPath, ppidl, &attributes);
2292 else if (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
2294 /* unlike SHGetFolderPath, SHGetFolderLocation in shell32
2295 * version 6.0 returns E_FAIL for nonexistent paths
2297 hr = E_FAIL;
2301 if(*ppidl)
2302 hr = NOERROR;
2304 TRACE("-- (new pidl %p)\n",*ppidl);
2305 return hr;
2308 /*************************************************************************
2309 * SHGetSpecialFolderLocation [SHELL32.@]
2311 * NOTES
2312 * In NT5, SHGetSpecialFolderLocation needs the <winntdir>/Recent
2313 * directory.
2315 HRESULT WINAPI SHGetSpecialFolderLocation(
2316 HWND hwndOwner,
2317 INT nFolder,
2318 LPITEMIDLIST * ppidl)
2320 HRESULT hr = E_INVALIDARG;
2322 TRACE("(%p,0x%x,%p)\n", hwndOwner,nFolder,ppidl);
2324 if (!ppidl)
2325 return E_INVALIDARG;
2327 hr = SHGetFolderLocation(hwndOwner, nFolder, NULL, 0, ppidl);
2328 return hr;