msvcrt/tests: Remove a space before a '\n'.
[wine/gsoc-2012-control.git] / dlls / shell32 / shellpath.c
blobd9fd6dddaaf628ec65e6f507c9e24e92d99ec6e3
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"
50 #include "sddl.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(shell);
55 ########## Combining and Constructing paths ##########
58 /*************************************************************************
59 * PathAppend [SHELL32.36]
61 BOOL WINAPI PathAppendAW(
62 LPVOID lpszPath1,
63 LPCVOID lpszPath2)
65 if (SHELL_OsIsUnicode())
66 return PathAppendW(lpszPath1, lpszPath2);
67 return PathAppendA(lpszPath1, lpszPath2);
70 /*************************************************************************
71 * PathCombine [SHELL32.37]
73 LPVOID WINAPI PathCombineAW(
74 LPVOID szDest,
75 LPCVOID lpszDir,
76 LPCVOID lpszFile)
78 if (SHELL_OsIsUnicode())
79 return PathCombineW( szDest, lpszDir, lpszFile );
80 return PathCombineA( szDest, lpszDir, lpszFile );
83 /*************************************************************************
84 * PathAddBackslash [SHELL32.32]
86 LPVOID WINAPI PathAddBackslashAW(LPVOID lpszPath)
88 if(SHELL_OsIsUnicode())
89 return PathAddBackslashW(lpszPath);
90 return PathAddBackslashA(lpszPath);
93 /*************************************************************************
94 * PathBuildRoot [SHELL32.30]
96 LPVOID WINAPI PathBuildRootAW(LPVOID lpszPath, int drive)
98 if(SHELL_OsIsUnicode())
99 return PathBuildRootW(lpszPath, drive);
100 return PathBuildRootA(lpszPath, drive);
104 Extracting Component Parts
107 /*************************************************************************
108 * PathFindFileName [SHELL32.34]
110 LPVOID WINAPI PathFindFileNameAW(LPCVOID lpszPath)
112 if(SHELL_OsIsUnicode())
113 return PathFindFileNameW(lpszPath);
114 return PathFindFileNameA(lpszPath);
117 /*************************************************************************
118 * PathFindExtension [SHELL32.31]
120 LPVOID WINAPI PathFindExtensionAW(LPCVOID lpszPath)
122 if (SHELL_OsIsUnicode())
123 return PathFindExtensionW(lpszPath);
124 return PathFindExtensionA(lpszPath);
128 /*************************************************************************
129 * PathGetExtensionA [internal]
131 * NOTES
132 * exported by ordinal
133 * return value points to the first char after the dot
135 static LPSTR PathGetExtensionA(LPCSTR lpszPath)
137 TRACE("(%s)\n",lpszPath);
139 lpszPath = PathFindExtensionA(lpszPath);
140 return (LPSTR)(*lpszPath?(lpszPath+1):lpszPath);
143 /*************************************************************************
144 * PathGetExtensionW [internal]
146 static LPWSTR PathGetExtensionW(LPCWSTR lpszPath)
148 TRACE("(%s)\n",debugstr_w(lpszPath));
150 lpszPath = PathFindExtensionW(lpszPath);
151 return (LPWSTR)(*lpszPath?(lpszPath+1):lpszPath);
154 /*************************************************************************
155 * PathGetExtension [SHELL32.158]
157 LPVOID WINAPI PathGetExtensionAW(LPCVOID lpszPath,DWORD void1, DWORD void2)
159 if (SHELL_OsIsUnicode())
160 return PathGetExtensionW(lpszPath);
161 return PathGetExtensionA(lpszPath);
164 /*************************************************************************
165 * PathGetArgs [SHELL32.52]
167 LPVOID WINAPI PathGetArgsAW(LPVOID lpszPath)
169 if (SHELL_OsIsUnicode())
170 return PathGetArgsW(lpszPath);
171 return PathGetArgsA(lpszPath);
174 /*************************************************************************
175 * PathGetDriveNumber [SHELL32.57]
177 int WINAPI PathGetDriveNumberAW(LPVOID lpszPath)
179 if (SHELL_OsIsUnicode())
180 return PathGetDriveNumberW(lpszPath);
181 return PathGetDriveNumberA(lpszPath);
184 /*************************************************************************
185 * PathRemoveFileSpec [SHELL32.35]
187 BOOL WINAPI PathRemoveFileSpecAW(LPVOID lpszPath)
189 if (SHELL_OsIsUnicode())
190 return PathRemoveFileSpecW(lpszPath);
191 return PathRemoveFileSpecA(lpszPath);
194 /*************************************************************************
195 * PathStripPath [SHELL32.38]
197 void WINAPI PathStripPathAW(LPVOID lpszPath)
199 if (SHELL_OsIsUnicode())
200 PathStripPathW(lpszPath);
201 else
202 PathStripPathA(lpszPath);
205 /*************************************************************************
206 * PathStripToRoot [SHELL32.50]
208 BOOL WINAPI PathStripToRootAW(LPVOID lpszPath)
210 if (SHELL_OsIsUnicode())
211 return PathStripToRootW(lpszPath);
212 return PathStripToRootA(lpszPath);
215 /*************************************************************************
216 * PathRemoveArgs [SHELL32.251]
218 void WINAPI PathRemoveArgsAW(LPVOID lpszPath)
220 if (SHELL_OsIsUnicode())
221 PathRemoveArgsW(lpszPath);
222 else
223 PathRemoveArgsA(lpszPath);
226 /*************************************************************************
227 * PathRemoveExtension [SHELL32.250]
229 void WINAPI PathRemoveExtensionAW(LPVOID lpszPath)
231 if (SHELL_OsIsUnicode())
232 PathRemoveExtensionW(lpszPath);
233 else
234 PathRemoveExtensionA(lpszPath);
239 Path Manipulations
242 /*************************************************************************
243 * PathGetShortPathA [internal]
245 static void PathGetShortPathA(LPSTR pszPath)
247 CHAR path[MAX_PATH];
249 TRACE("%s\n", pszPath);
251 if (GetShortPathNameA(pszPath, path, MAX_PATH))
253 lstrcpyA(pszPath, path);
257 /*************************************************************************
258 * PathGetShortPathW [internal]
260 static void PathGetShortPathW(LPWSTR pszPath)
262 WCHAR path[MAX_PATH];
264 TRACE("%s\n", debugstr_w(pszPath));
266 if (GetShortPathNameW(pszPath, path, MAX_PATH))
268 lstrcpyW(pszPath, path);
272 /*************************************************************************
273 * PathGetShortPath [SHELL32.92]
275 VOID WINAPI PathGetShortPathAW(LPVOID pszPath)
277 if(SHELL_OsIsUnicode())
278 PathGetShortPathW(pszPath);
279 PathGetShortPathA(pszPath);
282 /*************************************************************************
283 * PathRemoveBlanks [SHELL32.33]
285 void WINAPI PathRemoveBlanksAW(LPVOID str)
287 if(SHELL_OsIsUnicode())
288 PathRemoveBlanksW(str);
289 else
290 PathRemoveBlanksA(str);
293 /*************************************************************************
294 * PathQuoteSpaces [SHELL32.55]
296 VOID WINAPI PathQuoteSpacesAW (LPVOID lpszPath)
298 if(SHELL_OsIsUnicode())
299 PathQuoteSpacesW(lpszPath);
300 else
301 PathQuoteSpacesA(lpszPath);
304 /*************************************************************************
305 * PathUnquoteSpaces [SHELL32.56]
307 VOID WINAPI PathUnquoteSpacesAW(LPVOID str)
309 if(SHELL_OsIsUnicode())
310 PathUnquoteSpacesW(str);
311 else
312 PathUnquoteSpacesA(str);
315 /*************************************************************************
316 * PathParseIconLocation [SHELL32.249]
318 int WINAPI PathParseIconLocationAW (LPVOID lpszPath)
320 if(SHELL_OsIsUnicode())
321 return PathParseIconLocationW(lpszPath);
322 return PathParseIconLocationA(lpszPath);
326 ########## Path Testing ##########
328 /*************************************************************************
329 * PathIsUNC [SHELL32.39]
331 BOOL WINAPI PathIsUNCAW (LPCVOID lpszPath)
333 if (SHELL_OsIsUnicode())
334 return PathIsUNCW( lpszPath );
335 return PathIsUNCA( lpszPath );
338 /*************************************************************************
339 * PathIsRelative [SHELL32.40]
341 BOOL WINAPI PathIsRelativeAW (LPCVOID lpszPath)
343 if (SHELL_OsIsUnicode())
344 return PathIsRelativeW( lpszPath );
345 return PathIsRelativeA( lpszPath );
348 /*************************************************************************
349 * PathIsRoot [SHELL32.29]
351 BOOL WINAPI PathIsRootAW(LPCVOID lpszPath)
353 if (SHELL_OsIsUnicode())
354 return PathIsRootW(lpszPath);
355 return PathIsRootA(lpszPath);
358 /*************************************************************************
359 * PathIsExeA [internal]
361 static BOOL PathIsExeA (LPCSTR lpszPath)
363 LPCSTR lpszExtension = PathGetExtensionA(lpszPath);
364 int i;
365 static const char * const lpszExtensions[] =
366 {"exe", "com", "pif", "cmd", "bat", "scf", "scr", NULL };
368 TRACE("path=%s\n",lpszPath);
370 for(i=0; lpszExtensions[i]; i++)
371 if (!lstrcmpiA(lpszExtension,lpszExtensions[i])) return TRUE;
373 return FALSE;
376 /*************************************************************************
377 * PathIsExeW [internal]
379 static BOOL PathIsExeW (LPCWSTR lpszPath)
381 LPCWSTR lpszExtension = PathGetExtensionW(lpszPath);
382 int i;
383 static const WCHAR lpszExtensions[][4] =
384 {{'e','x','e','\0'}, {'c','o','m','\0'}, {'p','i','f','\0'},
385 {'c','m','d','\0'}, {'b','a','t','\0'}, {'s','c','f','\0'},
386 {'s','c','r','\0'}, {'\0'} };
388 TRACE("path=%s\n",debugstr_w(lpszPath));
390 for(i=0; lpszExtensions[i][0]; i++)
391 if (!strcmpiW(lpszExtension,lpszExtensions[i])) return TRUE;
393 return FALSE;
396 /*************************************************************************
397 * PathIsExe [SHELL32.43]
399 BOOL WINAPI PathIsExeAW (LPCVOID path)
401 if (SHELL_OsIsUnicode())
402 return PathIsExeW (path);
403 return PathIsExeA(path);
406 /*************************************************************************
407 * PathIsDirectory [SHELL32.159]
409 BOOL WINAPI PathIsDirectoryAW (LPCVOID lpszPath)
411 if (SHELL_OsIsUnicode())
412 return PathIsDirectoryW (lpszPath);
413 return PathIsDirectoryA (lpszPath);
416 /*************************************************************************
417 * PathFileExists [SHELL32.45]
419 BOOL WINAPI PathFileExistsAW (LPCVOID lpszPath)
421 if (SHELL_OsIsUnicode())
422 return PathFileExistsW (lpszPath);
423 return PathFileExistsA (lpszPath);
426 /*************************************************************************
427 * PathMatchSpec [SHELL32.46]
429 BOOL WINAPI PathMatchSpecAW(LPVOID name, LPVOID mask)
431 if (SHELL_OsIsUnicode())
432 return PathMatchSpecW( name, mask );
433 return PathMatchSpecA( name, mask );
436 /*************************************************************************
437 * PathIsSameRoot [SHELL32.650]
439 BOOL WINAPI PathIsSameRootAW(LPCVOID lpszPath1, LPCVOID lpszPath2)
441 if (SHELL_OsIsUnicode())
442 return PathIsSameRootW(lpszPath1, lpszPath2);
443 return PathIsSameRootA(lpszPath1, lpszPath2);
446 /*************************************************************************
447 * IsLFNDriveA [SHELL32.41]
449 BOOL WINAPI IsLFNDriveA(LPCSTR lpszPath)
451 DWORD fnlen;
453 if (!GetVolumeInformationA(lpszPath, NULL, 0, NULL, &fnlen, NULL, NULL, 0))
454 return FALSE;
455 return fnlen > 12;
458 /*************************************************************************
459 * IsLFNDriveW [SHELL32.42]
461 BOOL WINAPI IsLFNDriveW(LPCWSTR lpszPath)
463 DWORD fnlen;
465 if (!GetVolumeInformationW(lpszPath, NULL, 0, NULL, &fnlen, NULL, NULL, 0))
466 return FALSE;
467 return fnlen > 12;
470 /*************************************************************************
471 * IsLFNDrive [SHELL32.119]
473 BOOL WINAPI IsLFNDriveAW(LPCVOID lpszPath)
475 if (SHELL_OsIsUnicode())
476 return IsLFNDriveW(lpszPath);
477 return IsLFNDriveA(lpszPath);
481 ########## Creating Something Unique ##########
483 /*************************************************************************
484 * PathMakeUniqueNameA [internal]
486 static BOOL PathMakeUniqueNameA(
487 LPSTR lpszBuffer,
488 DWORD dwBuffSize,
489 LPCSTR lpszShortName,
490 LPCSTR lpszLongName,
491 LPCSTR lpszPathName)
493 FIXME("%p %u %s %s %s stub\n",
494 lpszBuffer, dwBuffSize, debugstr_a(lpszShortName),
495 debugstr_a(lpszLongName), debugstr_a(lpszPathName));
496 return TRUE;
499 /*************************************************************************
500 * PathMakeUniqueNameW [internal]
502 static BOOL PathMakeUniqueNameW(
503 LPWSTR lpszBuffer,
504 DWORD dwBuffSize,
505 LPCWSTR lpszShortName,
506 LPCWSTR lpszLongName,
507 LPCWSTR lpszPathName)
509 FIXME("%p %u %s %s %s stub\n",
510 lpszBuffer, dwBuffSize, debugstr_w(lpszShortName),
511 debugstr_w(lpszLongName), debugstr_w(lpszPathName));
512 return TRUE;
515 /*************************************************************************
516 * PathMakeUniqueName [SHELL32.47]
518 BOOL WINAPI PathMakeUniqueNameAW(
519 LPVOID lpszBuffer,
520 DWORD dwBuffSize,
521 LPCVOID lpszShortName,
522 LPCVOID lpszLongName,
523 LPCVOID lpszPathName)
525 if (SHELL_OsIsUnicode())
526 return PathMakeUniqueNameW(lpszBuffer,dwBuffSize, lpszShortName,lpszLongName,lpszPathName);
527 return PathMakeUniqueNameA(lpszBuffer,dwBuffSize, lpszShortName,lpszLongName,lpszPathName);
530 /*************************************************************************
531 * PathYetAnotherMakeUniqueName [SHELL32.75]
533 * NOTES
534 * exported by ordinal
536 BOOL WINAPI PathYetAnotherMakeUniqueName(
537 LPWSTR lpszBuffer,
538 LPCWSTR lpszPathName,
539 LPCWSTR lpszShortName,
540 LPCWSTR lpszLongName)
542 FIXME("(%p, %s, %s ,%s):stub.\n",
543 lpszBuffer, debugstr_w(lpszPathName), debugstr_w(lpszShortName), debugstr_w(lpszLongName));
544 return TRUE;
549 ########## cleaning and resolving paths ##########
552 /*************************************************************************
553 * PathFindOnPath [SHELL32.145]
555 BOOL WINAPI PathFindOnPathAW(LPVOID sFile, LPCVOID *sOtherDirs)
557 if (SHELL_OsIsUnicode())
558 return PathFindOnPathW(sFile, (LPCWSTR *)sOtherDirs);
559 return PathFindOnPathA(sFile, (LPCSTR *)sOtherDirs);
562 /*************************************************************************
563 * PathCleanupSpec [SHELL32.171]
565 * lpszFile is changed in place.
567 int WINAPI PathCleanupSpec( LPCWSTR lpszPathW, LPWSTR lpszFileW )
569 int i = 0;
570 DWORD rc = 0;
571 int length = 0;
573 if (SHELL_OsIsUnicode())
575 LPWSTR p = lpszFileW;
577 TRACE("Cleanup %s\n",debugstr_w(lpszFileW));
579 if (lpszPathW)
580 length = strlenW(lpszPathW);
582 while (*p)
584 int gct = PathGetCharTypeW(*p);
585 if (gct == GCT_INVALID || gct == GCT_WILD || gct == GCT_SEPARATOR)
587 lpszFileW[i]='-';
588 rc |= PCS_REPLACEDCHAR;
590 else
591 lpszFileW[i]=*p;
592 i++;
593 p++;
594 if (length + i == MAX_PATH)
596 rc |= PCS_FATAL | PCS_PATHTOOLONG;
597 break;
600 lpszFileW[i]=0;
602 else
604 LPSTR lpszFileA = (LPSTR)lpszFileW;
605 LPCSTR lpszPathA = (LPCSTR)lpszPathW;
606 LPSTR p = lpszFileA;
608 TRACE("Cleanup %s\n",debugstr_a(lpszFileA));
610 if (lpszPathA)
611 length = strlen(lpszPathA);
613 while (*p)
615 int gct = PathGetCharTypeA(*p);
616 if (gct == GCT_INVALID || gct == GCT_WILD || gct == GCT_SEPARATOR)
618 lpszFileA[i]='-';
619 rc |= PCS_REPLACEDCHAR;
621 else
622 lpszFileA[i]=*p;
623 i++;
624 p++;
625 if (length + i == MAX_PATH)
627 rc |= PCS_FATAL | PCS_PATHTOOLONG;
628 break;
631 lpszFileA[i]=0;
633 return rc;
636 /*************************************************************************
637 * PathQualifyA [SHELL32]
639 static BOOL PathQualifyA(LPCSTR pszPath)
641 FIXME("%s\n",pszPath);
642 return 0;
645 /*************************************************************************
646 * PathQualifyW [SHELL32]
648 static BOOL PathQualifyW(LPCWSTR pszPath)
650 FIXME("%s\n",debugstr_w(pszPath));
651 return 0;
654 /*************************************************************************
655 * PathQualify [SHELL32.49]
657 BOOL WINAPI PathQualifyAW(LPCVOID pszPath)
659 if (SHELL_OsIsUnicode())
660 return PathQualifyW(pszPath);
661 return PathQualifyA(pszPath);
664 static BOOL PathResolveA(
665 LPSTR lpszPath,
666 LPCSTR *alpszPaths,
667 DWORD dwFlags)
669 FIXME("(%s,%p,0x%08x),stub!\n",
670 lpszPath, *alpszPaths, dwFlags);
671 return 0;
674 static BOOL PathResolveW(
675 LPWSTR lpszPath,
676 LPCWSTR *alpszPaths,
677 DWORD dwFlags)
679 FIXME("(%s,%p,0x%08x),stub!\n",
680 debugstr_w(lpszPath), debugstr_w(*alpszPaths), dwFlags);
681 return 0;
684 /*************************************************************************
685 * PathResolve [SHELL32.51]
687 BOOL WINAPI PathResolveAW(
688 LPVOID lpszPath,
689 LPCVOID *alpszPaths,
690 DWORD dwFlags)
692 if (SHELL_OsIsUnicode())
693 return PathResolveW(lpszPath, (LPCWSTR*)alpszPaths, dwFlags);
694 return PathResolveA(lpszPath, (LPCSTR*)alpszPaths, dwFlags);
697 /*************************************************************************
698 * PathProcessCommandA
700 static LONG PathProcessCommandA (
701 LPCSTR lpszPath,
702 LPSTR lpszBuff,
703 DWORD dwBuffSize,
704 DWORD dwFlags)
706 FIXME("%s %p 0x%04x 0x%04x stub\n",
707 lpszPath, lpszBuff, dwBuffSize, dwFlags);
708 if(!lpszPath) return -1;
709 if(lpszBuff) strcpy(lpszBuff, lpszPath);
710 return strlen(lpszPath);
713 /*************************************************************************
714 * PathProcessCommandW
716 static LONG PathProcessCommandW (
717 LPCWSTR lpszPath,
718 LPWSTR lpszBuff,
719 DWORD dwBuffSize,
720 DWORD dwFlags)
722 FIXME("(%s, %p, 0x%04x, 0x%04x) stub\n",
723 debugstr_w(lpszPath), lpszBuff, dwBuffSize, dwFlags);
724 if(!lpszPath) return -1;
725 if(lpszBuff) strcpyW(lpszBuff, lpszPath);
726 return strlenW(lpszPath);
729 /*************************************************************************
730 * PathProcessCommand (SHELL32.653)
732 LONG WINAPI PathProcessCommandAW (
733 LPCVOID lpszPath,
734 LPVOID lpszBuff,
735 DWORD dwBuffSize,
736 DWORD dwFlags)
738 if (SHELL_OsIsUnicode())
739 return PathProcessCommandW(lpszPath, lpszBuff, dwBuffSize, dwFlags);
740 return PathProcessCommandA(lpszPath, lpszBuff, dwBuffSize, dwFlags);
744 ########## special ##########
747 /*************************************************************************
748 * PathSetDlgItemPath (SHELL32.48)
750 VOID WINAPI PathSetDlgItemPathAW(HWND hDlg, int id, LPCVOID pszPath)
752 if (SHELL_OsIsUnicode())
753 PathSetDlgItemPathW(hDlg, id, pszPath);
754 else
755 PathSetDlgItemPathA(hDlg, id, pszPath);
758 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'};
759 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'};
760 static const WCHAR AppDataW[] = {'A','p','p','D','a','t','a','\0'};
761 static const WCHAR CacheW[] = {'C','a','c','h','e','\0'};
762 static const WCHAR CD_BurningW[] = {'C','D',' ','B','u','r','n','i','n','g','\0'};
763 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'};
764 static const WCHAR Common_AppDataW[] = {'C','o','m','m','o','n',' ','A','p','p','D','a','t','a','\0'};
765 static const WCHAR Common_DesktopW[] = {'C','o','m','m','o','n',' ','D','e','s','k','t','o','p','\0'};
766 static const WCHAR Common_DocumentsW[] = {'C','o','m','m','o','n',' ','D','o','c','u','m','e','n','t','s','\0'};
767 static const WCHAR CommonFilesDirW[] = {'C','o','m','m','o','n','F','i','l','e','s','D','i','r','\0'};
768 static const WCHAR CommonMusicW[] = {'C','o','m','m','o','n','M','u','s','i','c','\0'};
769 static const WCHAR CommonPicturesW[] = {'C','o','m','m','o','n','P','i','c','t','u','r','e','s','\0'};
770 static const WCHAR Common_ProgramsW[] = {'C','o','m','m','o','n',' ','P','r','o','g','r','a','m','s','\0'};
771 static const WCHAR Common_StartUpW[] = {'C','o','m','m','o','n',' ','S','t','a','r','t','U','p','\0'};
772 static const WCHAR Common_Start_MenuW[] = {'C','o','m','m','o','n',' ','S','t','a','r','t',' ','M','e','n','u','\0'};
773 static const WCHAR Common_TemplatesW[] = {'C','o','m','m','o','n',' ','T','e','m','p','l','a','t','e','s','\0'};
774 static const WCHAR CommonVideoW[] = {'C','o','m','m','o','n','V','i','d','e','o','\0'};
775 static const WCHAR CookiesW[] = {'C','o','o','k','i','e','s','\0'};
776 static const WCHAR DesktopW[] = {'D','e','s','k','t','o','p','\0'};
777 static const WCHAR FavoritesW[] = {'F','a','v','o','r','i','t','e','s','\0'};
778 static const WCHAR FontsW[] = {'F','o','n','t','s','\0'};
779 static const WCHAR HistoryW[] = {'H','i','s','t','o','r','y','\0'};
780 static const WCHAR Local_AppDataW[] = {'L','o','c','a','l',' ','A','p','p','D','a','t','a','\0'};
781 static const WCHAR My_MusicW[] = {'M','y',' ','M','u','s','i','c','\0'};
782 static const WCHAR My_PicturesW[] = {'M','y',' ','P','i','c','t','u','r','e','s','\0'};
783 static const WCHAR My_VideoW[] = {'M','y',' ','V','i','d','e','o','s','\0'};
784 static const WCHAR NetHoodW[] = {'N','e','t','H','o','o','d','\0'};
785 static const WCHAR PersonalW[] = {'P','e','r','s','o','n','a','l','\0'};
786 static const WCHAR PrintHoodW[] = {'P','r','i','n','t','H','o','o','d','\0'};
787 static const WCHAR ProgramFilesDirW[] = {'P','r','o','g','r','a','m','F','i','l','e','s','D','i','r','\0'};
788 static const WCHAR ProgramsW[] = {'P','r','o','g','r','a','m','s','\0'};
789 static const WCHAR RecentW[] = {'R','e','c','e','n','t','\0'};
790 static const WCHAR ResourcesW[] = {'R','e','s','o','u','r','c','e','s','\0'};
791 static const WCHAR SendToW[] = {'S','e','n','d','T','o','\0'};
792 static const WCHAR StartUpW[] = {'S','t','a','r','t','U','p','\0'};
793 static const WCHAR Start_MenuW[] = {'S','t','a','r','t',' ','M','e','n','u','\0'};
794 static const WCHAR TemplatesW[] = {'T','e','m','p','l','a','t','e','s','\0'};
795 static const WCHAR DefaultW[] = {'.','D','e','f','a','u','l','t','\0'};
796 static const WCHAR AllUsersProfileW[] = {'%','A','L','L','U','S','E','R','S','P','R','O','F','I','L','E','%','\0'};
797 static const WCHAR UserProfileW[] = {'%','U','S','E','R','P','R','O','F','I','L','E','%','\0'};
798 static const WCHAR SystemDriveW[] = {'%','S','y','s','t','e','m','D','r','i','v','e','%','\0'};
799 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};
800 static const WCHAR ProfilesDirectoryW[] = {'P','r','o','f','i','l','e','s','D','i','r','e','c','t','o','r','y',0};
801 static const WCHAR AllUsersProfileValueW[] = {'A','l','l','U','s','e','r','s','P','r','o','f','i','l','e','\0'};
802 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'};
803 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'};
804 /* This defaults to L"Documents and Settings" on Windows 2000/XP, but we're
805 * acting more Windows 9x-like for now.
807 static const WCHAR szDefaultProfileDirW[] = {'p','r','o','f','i','l','e','s','\0'};
808 static const WCHAR AllUsersW[] = {'A','l','l',' ','U','s','e','r','s','\0'};
810 typedef enum _CSIDL_Type {
811 CSIDL_Type_User,
812 CSIDL_Type_AllUsers,
813 CSIDL_Type_CurrVer,
814 CSIDL_Type_Disallowed,
815 CSIDL_Type_NonExistent,
816 CSIDL_Type_WindowsPath,
817 CSIDL_Type_SystemPath,
818 } CSIDL_Type;
820 typedef struct
822 CSIDL_Type type;
823 LPCWSTR szValueName;
824 LPCWSTR szDefaultPath; /* fallback string or resource ID */
825 } CSIDL_DATA;
827 static const CSIDL_DATA CSIDL_Data[] =
829 { /* 0x00 - CSIDL_DESKTOP */
830 CSIDL_Type_User,
831 DesktopW,
832 MAKEINTRESOURCEW(IDS_DESKTOPDIRECTORY)
834 { /* 0x01 - CSIDL_INTERNET */
835 CSIDL_Type_Disallowed,
836 NULL,
837 NULL
839 { /* 0x02 - CSIDL_PROGRAMS */
840 CSIDL_Type_User,
841 ProgramsW,
842 MAKEINTRESOURCEW(IDS_PROGRAMS)
844 { /* 0x03 - CSIDL_CONTROLS (.CPL files) */
845 CSIDL_Type_SystemPath,
846 NULL,
847 NULL
849 { /* 0x04 - CSIDL_PRINTERS */
850 CSIDL_Type_SystemPath,
851 NULL,
852 NULL
854 { /* 0x05 - CSIDL_PERSONAL */
855 CSIDL_Type_User,
856 PersonalW,
857 MAKEINTRESOURCEW(IDS_PERSONAL)
859 { /* 0x06 - CSIDL_FAVORITES */
860 CSIDL_Type_User,
861 FavoritesW,
862 MAKEINTRESOURCEW(IDS_FAVORITES)
864 { /* 0x07 - CSIDL_STARTUP */
865 CSIDL_Type_User,
866 StartUpW,
867 MAKEINTRESOURCEW(IDS_STARTUP)
869 { /* 0x08 - CSIDL_RECENT */
870 CSIDL_Type_User,
871 RecentW,
872 MAKEINTRESOURCEW(IDS_RECENT)
874 { /* 0x09 - CSIDL_SENDTO */
875 CSIDL_Type_User,
876 SendToW,
877 MAKEINTRESOURCEW(IDS_SENDTO)
879 { /* 0x0a - CSIDL_BITBUCKET - Recycle Bin */
880 CSIDL_Type_Disallowed,
881 NULL,
882 NULL,
884 { /* 0x0b - CSIDL_STARTMENU */
885 CSIDL_Type_User,
886 Start_MenuW,
887 MAKEINTRESOURCEW(IDS_STARTMENU)
889 { /* 0x0c - CSIDL_MYDOCUMENTS */
890 CSIDL_Type_Disallowed, /* matches WinXP--can't get its path */
891 NULL,
892 NULL
894 { /* 0x0d - CSIDL_MYMUSIC */
895 CSIDL_Type_User,
896 My_MusicW,
897 MAKEINTRESOURCEW(IDS_MYMUSIC)
899 { /* 0x0e - CSIDL_MYVIDEO */
900 CSIDL_Type_User,
901 My_VideoW,
902 MAKEINTRESOURCEW(IDS_MYVIDEO)
904 { /* 0x0f - unassigned */
905 CSIDL_Type_Disallowed,
906 NULL,
907 NULL,
909 { /* 0x10 - CSIDL_DESKTOPDIRECTORY */
910 CSIDL_Type_User,
911 DesktopW,
912 MAKEINTRESOURCEW(IDS_DESKTOPDIRECTORY)
914 { /* 0x11 - CSIDL_DRIVES */
915 CSIDL_Type_Disallowed,
916 NULL,
917 NULL,
919 { /* 0x12 - CSIDL_NETWORK */
920 CSIDL_Type_Disallowed,
921 NULL,
922 NULL,
924 { /* 0x13 - CSIDL_NETHOOD */
925 CSIDL_Type_User,
926 NetHoodW,
927 MAKEINTRESOURCEW(IDS_NETHOOD)
929 { /* 0x14 - CSIDL_FONTS */
930 CSIDL_Type_WindowsPath,
931 FontsW,
932 FontsW
934 { /* 0x15 - CSIDL_TEMPLATES */
935 CSIDL_Type_User,
936 TemplatesW,
937 MAKEINTRESOURCEW(IDS_TEMPLATES)
939 { /* 0x16 - CSIDL_COMMON_STARTMENU */
940 CSIDL_Type_AllUsers,
941 Common_Start_MenuW,
942 MAKEINTRESOURCEW(IDS_STARTMENU)
944 { /* 0x17 - CSIDL_COMMON_PROGRAMS */
945 CSIDL_Type_AllUsers,
946 Common_ProgramsW,
947 MAKEINTRESOURCEW(IDS_PROGRAMS)
949 { /* 0x18 - CSIDL_COMMON_STARTUP */
950 CSIDL_Type_AllUsers,
951 Common_StartUpW,
952 MAKEINTRESOURCEW(IDS_STARTUP)
954 { /* 0x19 - CSIDL_COMMON_DESKTOPDIRECTORY */
955 CSIDL_Type_AllUsers,
956 Common_DesktopW,
957 MAKEINTRESOURCEW(IDS_DESKTOP)
959 { /* 0x1a - CSIDL_APPDATA */
960 CSIDL_Type_User,
961 AppDataW,
962 MAKEINTRESOURCEW(IDS_APPDATA)
964 { /* 0x1b - CSIDL_PRINTHOOD */
965 CSIDL_Type_User,
966 PrintHoodW,
967 MAKEINTRESOURCEW(IDS_PRINTHOOD)
969 { /* 0x1c - CSIDL_LOCAL_APPDATA */
970 CSIDL_Type_User,
971 Local_AppDataW,
972 MAKEINTRESOURCEW(IDS_LOCAL_APPDATA)
974 { /* 0x1d - CSIDL_ALTSTARTUP */
975 CSIDL_Type_NonExistent,
976 NULL,
977 NULL
979 { /* 0x1e - CSIDL_COMMON_ALTSTARTUP */
980 CSIDL_Type_NonExistent,
981 NULL,
982 NULL
984 { /* 0x1f - CSIDL_COMMON_FAVORITES */
985 CSIDL_Type_AllUsers,
986 FavoritesW,
987 MAKEINTRESOURCEW(IDS_FAVORITES)
989 { /* 0x20 - CSIDL_INTERNET_CACHE */
990 CSIDL_Type_User,
991 CacheW,
992 MAKEINTRESOURCEW(IDS_INTERNET_CACHE)
994 { /* 0x21 - CSIDL_COOKIES */
995 CSIDL_Type_User,
996 CookiesW,
997 MAKEINTRESOURCEW(IDS_COOKIES)
999 { /* 0x22 - CSIDL_HISTORY */
1000 CSIDL_Type_User,
1001 HistoryW,
1002 MAKEINTRESOURCEW(IDS_HISTORY)
1004 { /* 0x23 - CSIDL_COMMON_APPDATA */
1005 CSIDL_Type_AllUsers,
1006 Common_AppDataW,
1007 MAKEINTRESOURCEW(IDS_APPDATA)
1009 { /* 0x24 - CSIDL_WINDOWS */
1010 CSIDL_Type_WindowsPath,
1011 NULL,
1012 NULL
1014 { /* 0x25 - CSIDL_SYSTEM */
1015 CSIDL_Type_SystemPath,
1016 NULL,
1017 NULL
1019 { /* 0x26 - CSIDL_PROGRAM_FILES */
1020 CSIDL_Type_CurrVer,
1021 ProgramFilesDirW,
1022 MAKEINTRESOURCEW(IDS_PROGRAM_FILES)
1024 { /* 0x27 - CSIDL_MYPICTURES */
1025 CSIDL_Type_User,
1026 My_PicturesW,
1027 MAKEINTRESOURCEW(IDS_MYPICTURES)
1029 { /* 0x28 - CSIDL_PROFILE */
1030 CSIDL_Type_User,
1031 NULL,
1032 NULL
1034 { /* 0x29 - CSIDL_SYSTEMX86 */
1035 CSIDL_Type_NonExistent,
1036 NULL,
1037 NULL
1039 { /* 0x2a - CSIDL_PROGRAM_FILESX86 */
1040 CSIDL_Type_NonExistent,
1041 NULL,
1042 NULL
1044 { /* 0x2b - CSIDL_PROGRAM_FILES_COMMON */
1045 CSIDL_Type_CurrVer,
1046 CommonFilesDirW,
1047 MAKEINTRESOURCEW(IDS_PROGRAM_FILES_COMMON)
1049 { /* 0x2c - CSIDL_PROGRAM_FILES_COMMONX86 */
1050 CSIDL_Type_NonExistent,
1051 NULL,
1052 NULL
1054 { /* 0x2d - CSIDL_COMMON_TEMPLATES */
1055 CSIDL_Type_AllUsers,
1056 Common_TemplatesW,
1057 MAKEINTRESOURCEW(IDS_TEMPLATES)
1059 { /* 0x2e - CSIDL_COMMON_DOCUMENTS */
1060 CSIDL_Type_AllUsers,
1061 Common_DocumentsW,
1062 MAKEINTRESOURCEW(IDS_COMMON_DOCUMENTS)
1064 { /* 0x2f - CSIDL_COMMON_ADMINTOOLS */
1065 CSIDL_Type_AllUsers,
1066 Common_Administrative_ToolsW,
1067 MAKEINTRESOURCEW(IDS_ADMINTOOLS)
1069 { /* 0x30 - CSIDL_ADMINTOOLS */
1070 CSIDL_Type_User,
1071 Administrative_ToolsW,
1072 MAKEINTRESOURCEW(IDS_ADMINTOOLS)
1074 { /* 0x31 - CSIDL_CONNECTIONS */
1075 CSIDL_Type_Disallowed,
1076 NULL,
1077 NULL
1079 { /* 0x32 - unassigned */
1080 CSIDL_Type_Disallowed,
1081 NULL,
1082 NULL
1084 { /* 0x33 - unassigned */
1085 CSIDL_Type_Disallowed,
1086 NULL,
1087 NULL
1089 { /* 0x34 - unassigned */
1090 CSIDL_Type_Disallowed,
1091 NULL,
1092 NULL
1094 { /* 0x35 - CSIDL_COMMON_MUSIC */
1095 CSIDL_Type_AllUsers,
1096 CommonMusicW,
1097 MAKEINTRESOURCEW(IDS_COMMON_MUSIC)
1099 { /* 0x36 - CSIDL_COMMON_PICTURES */
1100 CSIDL_Type_AllUsers,
1101 CommonPicturesW,
1102 MAKEINTRESOURCEW(IDS_COMMON_PICTURES)
1104 { /* 0x37 - CSIDL_COMMON_VIDEO */
1105 CSIDL_Type_AllUsers,
1106 CommonVideoW,
1107 MAKEINTRESOURCEW(IDS_COMMON_VIDEO)
1109 { /* 0x38 - CSIDL_RESOURCES */
1110 CSIDL_Type_WindowsPath,
1111 NULL,
1112 ResourcesW
1114 { /* 0x39 - CSIDL_RESOURCES_LOCALIZED */
1115 CSIDL_Type_NonExistent,
1116 NULL,
1117 NULL
1119 { /* 0x3a - CSIDL_COMMON_OEM_LINKS */
1120 CSIDL_Type_NonExistent,
1121 NULL,
1122 NULL
1124 { /* 0x3b - CSIDL_CDBURN_AREA */
1125 CSIDL_Type_User,
1126 CD_BurningW,
1127 MAKEINTRESOURCEW(IDS_CDBURN_AREA)
1129 { /* 0x3c unassigned */
1130 CSIDL_Type_Disallowed,
1131 NULL,
1132 NULL
1134 { /* 0x3d - CSIDL_COMPUTERSNEARME */
1135 CSIDL_Type_Disallowed, /* FIXME */
1136 NULL,
1137 NULL
1139 { /* 0x3e - CSIDL_PROFILES */
1140 CSIDL_Type_Disallowed, /* oddly, this matches WinXP */
1141 NULL,
1142 NULL
1146 static HRESULT _SHExpandEnvironmentStrings(LPCWSTR szSrc, LPWSTR szDest);
1148 /* Gets the value named value from the registry key
1149 * rootKey\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
1150 * (or from rootKey\userPrefix\... if userPrefix is not NULL) into path, which
1151 * is assumed to be MAX_PATH WCHARs in length.
1152 * If it exists, expands the value and writes the expanded value to
1153 * rootKey\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders
1154 * Returns successful error code if the value was retrieved from the registry,
1155 * and a failure otherwise.
1157 static HRESULT _SHGetUserShellFolderPath(HKEY rootKey, LPCWSTR userPrefix,
1158 LPCWSTR value, LPWSTR path)
1160 HRESULT hr;
1161 WCHAR shellFolderPath[MAX_PATH], userShellFolderPath[MAX_PATH];
1162 LPCWSTR pShellFolderPath, pUserShellFolderPath;
1163 DWORD dwType, dwPathLen = MAX_PATH;
1164 HKEY userShellFolderKey, shellFolderKey;
1166 TRACE("%p,%s,%s,%p\n",rootKey, debugstr_w(userPrefix), debugstr_w(value),
1167 path);
1169 if (userPrefix)
1171 strcpyW(shellFolderPath, userPrefix);
1172 PathAddBackslashW(shellFolderPath);
1173 strcatW(shellFolderPath, szSHFolders);
1174 pShellFolderPath = shellFolderPath;
1175 strcpyW(userShellFolderPath, userPrefix);
1176 PathAddBackslashW(userShellFolderPath);
1177 strcatW(userShellFolderPath, szSHUserFolders);
1178 pUserShellFolderPath = userShellFolderPath;
1180 else
1182 pUserShellFolderPath = szSHUserFolders;
1183 pShellFolderPath = szSHFolders;
1186 if (RegCreateKeyW(rootKey, pShellFolderPath, &shellFolderKey))
1188 TRACE("Failed to create %s\n", debugstr_w(pShellFolderPath));
1189 return E_FAIL;
1191 if (RegCreateKeyW(rootKey, pUserShellFolderPath, &userShellFolderKey))
1193 TRACE("Failed to create %s\n",
1194 debugstr_w(pUserShellFolderPath));
1195 RegCloseKey(shellFolderKey);
1196 return E_FAIL;
1199 if (!RegQueryValueExW(userShellFolderKey, value, NULL, &dwType,
1200 (LPBYTE)path, &dwPathLen) && (dwType == REG_EXPAND_SZ || dwType == REG_SZ))
1202 LONG ret;
1204 path[dwPathLen / sizeof(WCHAR)] = '\0';
1205 if (dwType == REG_EXPAND_SZ && path[0] == '%')
1207 WCHAR szTemp[MAX_PATH];
1209 _SHExpandEnvironmentStrings(path, szTemp);
1210 lstrcpynW(path, szTemp, MAX_PATH);
1212 ret = RegSetValueExW(shellFolderKey, value, 0, REG_SZ, (LPBYTE)path,
1213 (strlenW(path) + 1) * sizeof(WCHAR));
1214 if (ret != ERROR_SUCCESS)
1215 hr = HRESULT_FROM_WIN32(ret);
1216 else
1217 hr = S_OK;
1219 else
1220 hr = E_FAIL;
1221 RegCloseKey(shellFolderKey);
1222 RegCloseKey(userShellFolderKey);
1223 TRACE("returning 0x%08x\n", hr);
1224 return hr;
1227 /* Gets a 'semi-expanded' default value of the CSIDL with index folder into
1228 * pszPath, based on the entries in CSIDL_Data. By semi-expanded, I mean:
1229 * - The entry's szDefaultPath may be either a string value or an integer
1230 * resource identifier. In the latter case, the string value of the resource
1231 * is written.
1232 * - Depending on the entry's type, the path may begin with an (unexpanded)
1233 * environment variable name. The caller is responsible for expanding
1234 * environment strings if so desired.
1235 * The types that are prepended with environment variables are:
1236 * CSIDL_Type_User: %USERPROFILE%
1237 * CSIDL_Type_AllUsers: %ALLUSERSPROFILE%
1238 * CSIDL_Type_CurrVer: %SystemDrive%
1239 * (Others might make sense too, but as yet are unneeded.)
1241 static HRESULT _SHGetDefaultValue(BYTE folder, LPWSTR pszPath)
1243 HRESULT hr;
1244 WCHAR resourcePath[MAX_PATH];
1245 LPCWSTR pDefaultPath = NULL;
1247 TRACE("0x%02x,%p\n", folder, pszPath);
1249 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1250 return E_INVALIDARG;
1251 if (!pszPath)
1252 return E_INVALIDARG;
1254 if (CSIDL_Data[folder].szDefaultPath &&
1255 IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath))
1257 if (LoadStringW(shell32_hInstance,
1258 LOWORD(CSIDL_Data[folder].szDefaultPath), resourcePath, MAX_PATH))
1260 hr = S_OK;
1261 pDefaultPath = resourcePath;
1263 else
1265 FIXME("(%d,%s), LoadString failed, missing translation?\n", folder,
1266 debugstr_w(pszPath));
1267 hr = E_FAIL;
1270 else
1272 hr = S_OK;
1273 pDefaultPath = CSIDL_Data[folder].szDefaultPath;
1275 if (SUCCEEDED(hr))
1277 switch (CSIDL_Data[folder].type)
1279 case CSIDL_Type_User:
1280 strcpyW(pszPath, UserProfileW);
1281 break;
1282 case CSIDL_Type_AllUsers:
1283 strcpyW(pszPath, AllUsersProfileW);
1284 break;
1285 case CSIDL_Type_CurrVer:
1286 strcpyW(pszPath, SystemDriveW);
1287 break;
1288 default:
1289 ; /* no corresponding env. var, do nothing */
1291 if (pDefaultPath)
1293 PathAddBackslashW(pszPath);
1294 strcatW(pszPath, pDefaultPath);
1297 TRACE("returning 0x%08x\n", hr);
1298 return hr;
1301 /* Gets the (unexpanded) value of the folder with index folder into pszPath.
1302 * The folder's type is assumed to be CSIDL_Type_CurrVer. Its default value
1303 * can be overridden in the HKLM\\szCurrentVersion key.
1304 * If dwFlags has SHGFP_TYPE_DEFAULT set or if the value isn't overridden in
1305 * the registry, uses _SHGetDefaultValue to get the value.
1307 static HRESULT _SHGetCurrentVersionPath(DWORD dwFlags, BYTE folder,
1308 LPWSTR pszPath)
1310 HRESULT hr;
1312 TRACE("0x%08x,0x%02x,%p\n", dwFlags, folder, pszPath);
1314 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1315 return E_INVALIDARG;
1316 if (CSIDL_Data[folder].type != CSIDL_Type_CurrVer)
1317 return E_INVALIDARG;
1318 if (!pszPath)
1319 return E_INVALIDARG;
1321 if (dwFlags & SHGFP_TYPE_DEFAULT)
1322 hr = _SHGetDefaultValue(folder, pszPath);
1323 else
1325 HKEY hKey;
1327 if (RegCreateKeyW(HKEY_LOCAL_MACHINE, szCurrentVersion, &hKey))
1328 hr = E_FAIL;
1329 else
1331 DWORD dwType, dwPathLen = MAX_PATH * sizeof(WCHAR);
1333 if (RegQueryValueExW(hKey, CSIDL_Data[folder].szValueName, NULL,
1334 &dwType, (LPBYTE)pszPath, &dwPathLen) ||
1335 (dwType != REG_SZ && dwType != REG_EXPAND_SZ))
1337 hr = _SHGetDefaultValue(folder, pszPath);
1338 dwType = REG_EXPAND_SZ;
1339 RegSetValueExW(hKey, CSIDL_Data[folder].szValueName, 0, dwType,
1340 (LPBYTE)pszPath, (strlenW(pszPath)+1)*sizeof(WCHAR));
1342 else
1344 pszPath[dwPathLen / sizeof(WCHAR)] = '\0';
1345 hr = S_OK;
1347 RegCloseKey(hKey);
1350 TRACE("returning 0x%08x (output path is %s)\n", hr, debugstr_w(pszPath));
1351 return hr;
1354 static LPWSTR _GetUserSidStringFromToken(HANDLE Token)
1356 char InfoBuffer[64];
1357 PTOKEN_USER UserInfo;
1358 DWORD InfoSize;
1359 LPWSTR SidStr;
1361 UserInfo = (PTOKEN_USER) InfoBuffer;
1362 if (! GetTokenInformation(Token, TokenUser, InfoBuffer, sizeof(InfoBuffer),
1363 &InfoSize))
1365 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
1366 return NULL;
1367 UserInfo = HeapAlloc(GetProcessHeap(), 0, InfoSize);
1368 if (UserInfo == NULL)
1369 return NULL;
1370 if (! GetTokenInformation(Token, TokenUser, UserInfo, InfoSize,
1371 &InfoSize))
1373 HeapFree(GetProcessHeap(), 0, UserInfo);
1374 return NULL;
1378 if (! ConvertSidToStringSidW(UserInfo->User.Sid, &SidStr))
1379 SidStr = NULL;
1381 if (UserInfo != (PTOKEN_USER) InfoBuffer)
1382 HeapFree(GetProcessHeap(), 0, UserInfo);
1384 return SidStr;
1387 /* Gets the user's path (unexpanded) for the CSIDL with index folder:
1388 * If SHGFP_TYPE_DEFAULT is set, calls _SHGetDefaultValue for it. Otherwise
1389 * calls _SHGetUserShellFolderPath for it. Where it looks depends on hToken:
1390 * - if hToken is -1, looks in HKEY_USERS\.Default
1391 * - otherwise looks first in HKEY_CURRENT_USER, followed by HKEY_LOCAL_MACHINE
1392 * if HKEY_CURRENT_USER doesn't contain any entries. If both fail, finally
1393 * calls _SHGetDefaultValue for it.
1395 static HRESULT _SHGetUserProfilePath(HANDLE hToken, DWORD dwFlags, BYTE folder,
1396 LPWSTR pszPath)
1398 HRESULT hr;
1400 TRACE("%p,0x%08x,0x%02x,%p\n", hToken, dwFlags, folder, pszPath);
1402 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1403 return E_INVALIDARG;
1404 if (CSIDL_Data[folder].type != CSIDL_Type_User)
1405 return E_INVALIDARG;
1406 if (!pszPath)
1407 return E_INVALIDARG;
1409 if (dwFlags & SHGFP_TYPE_DEFAULT)
1411 if (hToken != NULL && hToken != (HANDLE)-1)
1413 FIXME("unsupported for user other than current or default\n");
1414 return E_FAIL;
1416 hr = _SHGetDefaultValue(folder, pszPath);
1418 else
1420 LPCWSTR userPrefix = NULL;
1421 HKEY hRootKey;
1423 if (hToken == (HANDLE)-1)
1425 hRootKey = HKEY_USERS;
1426 userPrefix = DefaultW;
1428 else if (hToken == NULL)
1429 hRootKey = HKEY_CURRENT_USER;
1430 else
1432 hRootKey = HKEY_USERS;
1433 userPrefix = _GetUserSidStringFromToken(hToken);
1434 if (userPrefix == NULL)
1436 hr = E_FAIL;
1437 goto error;
1440 hr = _SHGetUserShellFolderPath(hRootKey, userPrefix,
1441 CSIDL_Data[folder].szValueName, pszPath);
1442 if (FAILED(hr) && hRootKey != HKEY_LOCAL_MACHINE)
1443 hr = _SHGetUserShellFolderPath(HKEY_LOCAL_MACHINE, NULL,
1444 CSIDL_Data[folder].szValueName, pszPath);
1445 if (FAILED(hr))
1446 hr = _SHGetDefaultValue(folder, pszPath);
1447 if (userPrefix != NULL && userPrefix != DefaultW)
1448 LocalFree((HLOCAL) userPrefix);
1450 error:
1451 TRACE("returning 0x%08x (output path is %s)\n", hr, debugstr_w(pszPath));
1452 return hr;
1455 /* Gets the (unexpanded) path for the CSIDL with index folder. If dwFlags has
1456 * SHGFP_TYPE_DEFAULT set, calls _SHGetDefaultValue. Otherwise calls
1457 * _SHGetUserShellFolderPath for it, looking only in HKEY_LOCAL_MACHINE.
1458 * If this fails, falls back to _SHGetDefaultValue.
1460 static HRESULT _SHGetAllUsersProfilePath(DWORD dwFlags, BYTE folder,
1461 LPWSTR pszPath)
1463 HRESULT hr;
1465 TRACE("0x%08x,0x%02x,%p\n", dwFlags, folder, pszPath);
1467 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1468 return E_INVALIDARG;
1469 if (CSIDL_Data[folder].type != CSIDL_Type_AllUsers)
1470 return E_INVALIDARG;
1471 if (!pszPath)
1472 return E_INVALIDARG;
1474 if (dwFlags & SHGFP_TYPE_DEFAULT)
1475 hr = _SHGetDefaultValue(folder, pszPath);
1476 else
1478 hr = _SHGetUserShellFolderPath(HKEY_LOCAL_MACHINE, NULL,
1479 CSIDL_Data[folder].szValueName, pszPath);
1480 if (FAILED(hr))
1481 hr = _SHGetDefaultValue(folder, pszPath);
1483 TRACE("returning 0x%08x (output path is %s)\n", hr, debugstr_w(pszPath));
1484 return hr;
1487 static HRESULT _SHOpenProfilesKey(PHKEY pKey)
1489 LONG lRet;
1490 DWORD disp;
1492 lRet = RegCreateKeyExW(HKEY_LOCAL_MACHINE, ProfileListW, 0, NULL, 0,
1493 KEY_ALL_ACCESS, NULL, pKey, &disp);
1494 return HRESULT_FROM_WIN32(lRet);
1497 /* Reads the value named szValueName from the key profilesKey (assumed to be
1498 * opened by _SHOpenProfilesKey) into szValue, which is assumed to be MAX_PATH
1499 * WCHARs in length. If it doesn't exist, returns szDefault (and saves
1500 * szDefault to the registry).
1502 static HRESULT _SHGetProfilesValue(HKEY profilesKey, LPCWSTR szValueName,
1503 LPWSTR szValue, LPCWSTR szDefault)
1505 HRESULT hr;
1506 DWORD type, dwPathLen = MAX_PATH * sizeof(WCHAR);
1507 LONG lRet;
1509 TRACE("%p,%s,%p,%s\n", profilesKey, debugstr_w(szValueName), szValue,
1510 debugstr_w(szDefault));
1511 lRet = RegQueryValueExW(profilesKey, szValueName, NULL, &type,
1512 (LPBYTE)szValue, &dwPathLen);
1513 if (!lRet && (type == REG_SZ || type == REG_EXPAND_SZ) && dwPathLen
1514 && *szValue)
1516 dwPathLen /= sizeof(WCHAR);
1517 szValue[dwPathLen] = '\0';
1518 hr = S_OK;
1520 else
1522 /* Missing or invalid value, set a default */
1523 lstrcpynW(szValue, szDefault, MAX_PATH);
1524 TRACE("Setting missing value %s to %s\n", debugstr_w(szValueName),
1525 debugstr_w(szValue));
1526 lRet = RegSetValueExW(profilesKey, szValueName, 0, REG_EXPAND_SZ,
1527 (LPBYTE)szValue,
1528 (strlenW(szValue) + 1) * sizeof(WCHAR));
1529 if (lRet)
1530 hr = HRESULT_FROM_WIN32(lRet);
1531 else
1532 hr = S_OK;
1534 TRACE("returning 0x%08x (output value is %s)\n", hr, debugstr_w(szValue));
1535 return hr;
1538 /* Attempts to expand environment variables from szSrc into szDest, which is
1539 * assumed to be MAX_PATH characters in length. Before referring to the
1540 * environment, handles a few variables directly, because the environment
1541 * variables may not be set when this is called (as during Wine's installation
1542 * when default values are being written to the registry).
1543 * The directly handled environment variables, and their source, are:
1544 * - ALLUSERSPROFILE, USERPROFILE: reads from the registry
1545 * - SystemDrive: uses GetSystemDirectoryW and uses the drive portion of its
1546 * path
1547 * If one of the directly handled environment variables is expanded, only
1548 * expands a single variable, and only in the beginning of szSrc.
1550 static HRESULT _SHExpandEnvironmentStrings(LPCWSTR szSrc, LPWSTR szDest)
1552 HRESULT hr;
1553 WCHAR szTemp[MAX_PATH], szProfilesPrefix[MAX_PATH] = { 0 };
1554 HKEY key = NULL;
1556 TRACE("%s, %p\n", debugstr_w(szSrc), szDest);
1558 if (!szSrc || !szDest) return E_INVALIDARG;
1560 /* short-circuit if there's nothing to expand */
1561 if (szSrc[0] != '%')
1563 strcpyW(szDest, szSrc);
1564 hr = S_OK;
1565 goto end;
1567 /* Get the profile prefix, we'll probably be needing it */
1568 hr = _SHOpenProfilesKey(&key);
1569 if (SUCCEEDED(hr))
1571 WCHAR szDefaultProfilesPrefix[MAX_PATH];
1573 GetWindowsDirectoryW(szDefaultProfilesPrefix, MAX_PATH);
1574 PathAddBackslashW(szDefaultProfilesPrefix);
1575 PathAppendW(szDefaultProfilesPrefix, szDefaultProfileDirW);
1576 hr = _SHGetProfilesValue(key, ProfilesDirectoryW, szProfilesPrefix,
1577 szDefaultProfilesPrefix);
1580 *szDest = 0;
1581 strcpyW(szTemp, szSrc);
1582 while (SUCCEEDED(hr) && szTemp[0] == '%')
1584 if (!strncmpiW(szTemp, AllUsersProfileW, strlenW(AllUsersProfileW)))
1586 WCHAR szAllUsers[MAX_PATH];
1588 strcpyW(szDest, szProfilesPrefix);
1589 hr = _SHGetProfilesValue(key, AllUsersProfileValueW,
1590 szAllUsers, AllUsersW);
1591 PathAppendW(szDest, szAllUsers);
1592 PathAppendW(szDest, szTemp + strlenW(AllUsersProfileW));
1594 else if (!strncmpiW(szTemp, UserProfileW, strlenW(UserProfileW)))
1596 WCHAR userName[MAX_PATH];
1597 DWORD userLen = MAX_PATH;
1599 strcpyW(szDest, szProfilesPrefix);
1600 GetUserNameW(userName, &userLen);
1601 PathAppendW(szDest, userName);
1602 PathAppendW(szDest, szTemp + strlenW(UserProfileW));
1604 else if (!strncmpiW(szTemp, SystemDriveW, strlenW(SystemDriveW)))
1606 GetSystemDirectoryW(szDest, MAX_PATH);
1607 if (szDest[1] != ':')
1609 FIXME("non-drive system paths unsupported\n");
1610 hr = E_FAIL;
1612 else
1614 strcpyW(szDest + 3, szTemp + strlenW(SystemDriveW) + 1);
1615 hr = S_OK;
1618 else
1620 DWORD ret = ExpandEnvironmentStringsW(szSrc, szDest, MAX_PATH);
1622 if (ret > MAX_PATH)
1623 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
1624 else if (ret == 0)
1625 hr = HRESULT_FROM_WIN32(GetLastError());
1626 else
1627 hr = S_OK;
1629 if (SUCCEEDED(hr) && szDest[0] == '%')
1630 strcpyW(szTemp, szDest);
1631 else
1633 /* terminate loop */
1634 szTemp[0] = '\0';
1637 end:
1638 if (key)
1639 RegCloseKey(key);
1640 TRACE("returning 0x%08x (input was %s, output is %s)\n", hr,
1641 debugstr_w(szSrc), debugstr_w(szDest));
1642 return hr;
1645 /*************************************************************************
1646 * SHGetFolderPathW [SHELL32.@]
1648 * Convert nFolder to path.
1650 * RETURNS
1651 * Success: S_OK
1652 * Failure: standard HRESULT error codes.
1654 * NOTES
1655 * Most values can be overridden in either
1656 * HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
1657 * or in the same location in HKLM.
1658 * The "Shell Folders" registry key was used in NT4 and earlier systems.
1659 * Beginning with Windows 2000, the "User Shell Folders" key is used, so
1660 * changes made to it are made to the former key too. This synchronization is
1661 * done on-demand: not until someone requests the value of one of these paths
1662 * (by calling one of the SHGet functions) is the value synchronized.
1663 * Furthermore, the HKCU paths take precedence over the HKLM paths.
1665 HRESULT WINAPI SHGetFolderPathW(
1666 HWND hwndOwner, /* [I] owner window */
1667 int nFolder, /* [I] CSIDL identifying the folder */
1668 HANDLE hToken, /* [I] access token */
1669 DWORD dwFlags, /* [I] which path to return */
1670 LPWSTR pszPath) /* [O] converted path */
1672 HRESULT hr = SHGetFolderPathAndSubDirW(hwndOwner, nFolder, hToken, dwFlags, NULL, pszPath);
1673 if(HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND) == hr)
1674 hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
1675 return hr;
1678 HRESULT WINAPI SHGetFolderPathAndSubDirA(
1679 HWND hwndOwner, /* [I] owner window */
1680 int nFolder, /* [I] CSIDL identifying the folder */
1681 HANDLE hToken, /* [I] access token */
1682 DWORD dwFlags, /* [I] which path to return */
1683 LPCSTR pszSubPath, /* [I] sub directory of the specified folder */
1684 LPSTR pszPath) /* [O] converted path */
1686 int length;
1687 HRESULT hr = S_OK;
1688 LPWSTR pszSubPathW = NULL;
1689 LPWSTR pszPathW = NULL;
1690 TRACE("%08x,%08x,%s\n",nFolder, dwFlags, debugstr_w(pszSubPathW));
1692 if(pszPath) {
1693 pszPathW = HeapAlloc(GetProcessHeap(), 0, MAX_PATH * sizeof(WCHAR));
1694 if(!pszPathW) {
1695 hr = HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
1696 goto cleanup;
1699 TRACE("%08x,%08x,%s\n",nFolder, dwFlags, debugstr_w(pszSubPathW));
1701 /* SHGetFolderPathAndSubDirW does not distinguish if pszSubPath isn't
1702 * set (null), or an empty string.therefore call it without the parameter set
1703 * if pszSubPath is an empty string
1705 if (pszSubPath && pszSubPath[0]) {
1706 length = MultiByteToWideChar(CP_ACP, 0, pszSubPath, -1, NULL, 0);
1707 pszSubPathW = HeapAlloc(GetProcessHeap(), 0, length * sizeof(WCHAR));
1708 if(!pszSubPathW) {
1709 hr = HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
1710 goto cleanup;
1712 MultiByteToWideChar(CP_ACP, 0, pszSubPath, -1, pszSubPathW, length);
1715 hr = SHGetFolderPathAndSubDirW(hwndOwner, nFolder, hToken, dwFlags, pszSubPathW, pszPathW);
1717 if (SUCCEEDED(hr) && pszPath)
1718 WideCharToMultiByte(CP_ACP, 0, pszPathW, -1, pszPath, MAX_PATH, NULL, NULL);
1720 cleanup:
1721 HeapFree(GetProcessHeap(), 0, pszPathW);
1722 HeapFree(GetProcessHeap(), 0, pszSubPathW);
1723 return hr;
1726 /*************************************************************************
1727 * SHGetFolderPathAndSubDirW [SHELL32.@]
1729 HRESULT WINAPI SHGetFolderPathAndSubDirW(
1730 HWND hwndOwner, /* [I] owner window */
1731 int nFolder, /* [I] CSIDL identifying the folder */
1732 HANDLE hToken, /* [I] access token */
1733 DWORD dwFlags, /* [I] which path to return */
1734 LPCWSTR pszSubPath,/* [I] sub directory of the specified folder */
1735 LPWSTR pszPath) /* [O] converted path */
1737 HRESULT hr;
1738 WCHAR szBuildPath[MAX_PATH], szTemp[MAX_PATH];
1739 DWORD folder = nFolder & CSIDL_FOLDER_MASK;
1740 CSIDL_Type type;
1741 int ret;
1743 TRACE("%p,%p,nFolder=0x%04x,%s\n", hwndOwner,pszPath,nFolder,debugstr_w(pszSubPath));
1745 /* Windows always NULL-terminates the resulting path regardless of success
1746 * or failure, so do so first
1748 if (pszPath)
1749 *pszPath = '\0';
1751 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1752 return E_INVALIDARG;
1753 if ((SHGFP_TYPE_CURRENT != dwFlags) && (SHGFP_TYPE_DEFAULT != dwFlags))
1754 return E_INVALIDARG;
1755 szTemp[0] = 0;
1756 type = CSIDL_Data[folder].type;
1757 switch (type)
1759 case CSIDL_Type_Disallowed:
1760 hr = E_INVALIDARG;
1761 break;
1762 case CSIDL_Type_NonExistent:
1763 hr = S_FALSE;
1764 break;
1765 case CSIDL_Type_WindowsPath:
1766 GetWindowsDirectoryW(szTemp, MAX_PATH);
1767 if (CSIDL_Data[folder].szDefaultPath &&
1768 !IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath) &&
1769 *CSIDL_Data[folder].szDefaultPath)
1771 PathAddBackslashW(szTemp);
1772 strcatW(szTemp, CSIDL_Data[folder].szDefaultPath);
1774 hr = S_OK;
1775 break;
1776 case CSIDL_Type_SystemPath:
1777 GetSystemDirectoryW(szTemp, MAX_PATH);
1778 if (CSIDL_Data[folder].szDefaultPath &&
1779 !IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath) &&
1780 *CSIDL_Data[folder].szDefaultPath)
1782 PathAddBackslashW(szTemp);
1783 strcatW(szTemp, CSIDL_Data[folder].szDefaultPath);
1785 hr = S_OK;
1786 break;
1787 case CSIDL_Type_CurrVer:
1788 hr = _SHGetCurrentVersionPath(dwFlags, folder, szTemp);
1789 break;
1790 case CSIDL_Type_User:
1791 hr = _SHGetUserProfilePath(hToken, dwFlags, folder, szTemp);
1792 break;
1793 case CSIDL_Type_AllUsers:
1794 hr = _SHGetAllUsersProfilePath(dwFlags, folder, szTemp);
1795 break;
1796 default:
1797 FIXME("bogus type %d, please fix\n", type);
1798 hr = E_INVALIDARG;
1799 break;
1802 /* Expand environment strings if necessary */
1803 if (*szTemp == '%')
1804 hr = _SHExpandEnvironmentStrings(szTemp, szBuildPath);
1805 else
1806 strcpyW(szBuildPath, szTemp);
1808 if (FAILED(hr)) goto end;
1810 if(pszSubPath) {
1811 /* make sure the new path does not exceed th bufferlength
1812 * rememebr to backslash and the termination */
1813 if(MAX_PATH < (lstrlenW(szBuildPath) + lstrlenW(pszSubPath) + 2)) {
1814 hr = HRESULT_FROM_WIN32(ERROR_FILENAME_EXCED_RANGE);
1815 goto end;
1817 PathAppendW(szBuildPath, pszSubPath);
1818 PathRemoveBackslashW(szBuildPath);
1820 /* Copy the path if it's available before we might return */
1821 if (SUCCEEDED(hr) && pszPath)
1822 strcpyW(pszPath, szBuildPath);
1824 /* if we don't care about existing directories we are ready */
1825 if(nFolder & CSIDL_FLAG_DONT_VERIFY) goto end;
1827 if (PathFileExistsW(szBuildPath)) goto end;
1829 /* not existing but we are not allowed to create it. The return value
1830 * is verified against shell32 version 6.0.
1832 if (!(nFolder & CSIDL_FLAG_CREATE))
1834 hr = HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND);
1835 goto end;
1838 /* create directory/directories */
1839 ret = SHCreateDirectoryExW(hwndOwner, szBuildPath, NULL);
1840 if (ret && ret != ERROR_ALREADY_EXISTS)
1842 ERR("Failed to create directory %s.\n", debugstr_w(szBuildPath));
1843 hr = E_FAIL;
1844 goto end;
1847 TRACE("Created missing system directory %s\n", debugstr_w(szBuildPath));
1848 end:
1849 TRACE("returning 0x%08x (final path is %s)\n", hr, debugstr_w(szBuildPath));
1850 return hr;
1853 /*************************************************************************
1854 * SHGetFolderPathA [SHELL32.@]
1856 * See SHGetFolderPathW.
1858 HRESULT WINAPI SHGetFolderPathA(
1859 HWND hwndOwner,
1860 int nFolder,
1861 HANDLE hToken,
1862 DWORD dwFlags,
1863 LPSTR pszPath)
1865 WCHAR szTemp[MAX_PATH];
1866 HRESULT hr;
1868 TRACE("%p,%p,nFolder=0x%04x\n",hwndOwner,pszPath,nFolder);
1870 if (pszPath)
1871 *pszPath = '\0';
1872 hr = SHGetFolderPathW(hwndOwner, nFolder, hToken, dwFlags, szTemp);
1873 if (SUCCEEDED(hr) && pszPath)
1874 WideCharToMultiByte(CP_ACP, 0, szTemp, -1, pszPath, MAX_PATH, NULL,
1875 NULL);
1877 return hr;
1880 /* For each folder in folders, if its value has not been set in the registry,
1881 * calls _SHGetUserProfilePath or _SHGetAllUsersProfilePath (depending on the
1882 * folder's type) to get the unexpanded value first.
1883 * Writes the unexpanded value to User Shell Folders, and queries it with
1884 * SHGetFolderPathW to force the creation of the directory if it doesn't
1885 * already exist. SHGetFolderPathW also returns the expanded value, which
1886 * this then writes to Shell Folders.
1888 static HRESULT _SHRegisterFolders(HKEY hRootKey, HANDLE hToken,
1889 LPCWSTR szUserShellFolderPath, LPCWSTR szShellFolderPath, const UINT folders[],
1890 UINT foldersLen)
1892 UINT i;
1893 WCHAR path[MAX_PATH];
1894 HRESULT hr = S_OK;
1895 HKEY hUserKey = NULL, hKey = NULL;
1896 DWORD dwType, dwPathLen;
1897 LONG ret;
1899 TRACE("%p,%p,%s,%p,%u\n", hRootKey, hToken,
1900 debugstr_w(szUserShellFolderPath), folders, foldersLen);
1902 ret = RegCreateKeyW(hRootKey, szUserShellFolderPath, &hUserKey);
1903 if (ret)
1904 hr = HRESULT_FROM_WIN32(ret);
1905 else
1907 ret = RegCreateKeyW(hRootKey, szShellFolderPath, &hKey);
1908 if (ret)
1909 hr = HRESULT_FROM_WIN32(ret);
1911 for (i = 0; SUCCEEDED(hr) && i < foldersLen; i++)
1913 dwPathLen = MAX_PATH * sizeof(WCHAR);
1914 if (RegQueryValueExW(hUserKey, CSIDL_Data[folders[i]].szValueName, NULL,
1915 &dwType, (LPBYTE)path, &dwPathLen) || (dwType != REG_SZ &&
1916 dwType != REG_EXPAND_SZ))
1918 *path = '\0';
1919 if (CSIDL_Data[folders[i]].type == CSIDL_Type_User)
1920 _SHGetUserProfilePath(hToken, SHGFP_TYPE_DEFAULT, folders[i],
1921 path);
1922 else if (CSIDL_Data[folders[i]].type == CSIDL_Type_AllUsers)
1923 _SHGetAllUsersProfilePath(SHGFP_TYPE_DEFAULT, folders[i], path);
1924 else if (CSIDL_Data[folders[i]].type == CSIDL_Type_WindowsPath)
1926 GetWindowsDirectoryW(path, MAX_PATH);
1927 if (CSIDL_Data[folders[i]].szDefaultPath &&
1928 !IS_INTRESOURCE(CSIDL_Data[folders[i]].szDefaultPath))
1930 PathAddBackslashW(path);
1931 strcatW(path, CSIDL_Data[folders[i]].szDefaultPath);
1934 else
1935 hr = E_FAIL;
1936 if (*path)
1938 ret = RegSetValueExW(hUserKey,
1939 CSIDL_Data[folders[i]].szValueName, 0, REG_EXPAND_SZ,
1940 (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR));
1941 if (ret)
1942 hr = HRESULT_FROM_WIN32(ret);
1943 else
1945 hr = SHGetFolderPathW(NULL, folders[i] | CSIDL_FLAG_CREATE,
1946 hToken, SHGFP_TYPE_DEFAULT, path);
1947 ret = RegSetValueExW(hKey,
1948 CSIDL_Data[folders[i]].szValueName, 0, REG_SZ,
1949 (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR));
1950 if (ret)
1951 hr = HRESULT_FROM_WIN32(ret);
1956 if (hUserKey)
1957 RegCloseKey(hUserKey);
1958 if (hKey)
1959 RegCloseKey(hKey);
1961 TRACE("returning 0x%08x\n", hr);
1962 return hr;
1965 static HRESULT _SHRegisterUserShellFolders(BOOL bDefault)
1967 static const UINT folders[] = {
1968 CSIDL_PROGRAMS,
1969 CSIDL_PERSONAL,
1970 CSIDL_FAVORITES,
1971 CSIDL_APPDATA,
1972 CSIDL_STARTUP,
1973 CSIDL_RECENT,
1974 CSIDL_SENDTO,
1975 CSIDL_STARTMENU,
1976 CSIDL_MYMUSIC,
1977 CSIDL_MYVIDEO,
1978 CSIDL_DESKTOPDIRECTORY,
1979 CSIDL_NETHOOD,
1980 CSIDL_TEMPLATES,
1981 CSIDL_PRINTHOOD,
1982 CSIDL_LOCAL_APPDATA,
1983 CSIDL_INTERNET_CACHE,
1984 CSIDL_COOKIES,
1985 CSIDL_HISTORY,
1986 CSIDL_MYPICTURES,
1987 CSIDL_FONTS
1989 WCHAR userShellFolderPath[MAX_PATH], shellFolderPath[MAX_PATH];
1990 LPCWSTR pUserShellFolderPath, pShellFolderPath;
1991 HRESULT hr = S_OK;
1992 HKEY hRootKey;
1993 HANDLE hToken;
1995 TRACE("%s\n", bDefault ? "TRUE" : "FALSE");
1996 if (bDefault)
1998 hToken = (HANDLE)-1;
1999 hRootKey = HKEY_USERS;
2000 strcpyW(userShellFolderPath, DefaultW);
2001 PathAddBackslashW(userShellFolderPath);
2002 strcatW(userShellFolderPath, szSHUserFolders);
2003 pUserShellFolderPath = userShellFolderPath;
2004 strcpyW(shellFolderPath, DefaultW);
2005 PathAddBackslashW(shellFolderPath);
2006 strcatW(shellFolderPath, szSHFolders);
2007 pShellFolderPath = shellFolderPath;
2009 else
2011 hToken = NULL;
2012 hRootKey = HKEY_CURRENT_USER;
2013 pUserShellFolderPath = szSHUserFolders;
2014 pShellFolderPath = szSHFolders;
2017 hr = _SHRegisterFolders(hRootKey, hToken, pUserShellFolderPath,
2018 pShellFolderPath, folders, sizeof(folders) / sizeof(folders[0]));
2019 TRACE("returning 0x%08x\n", hr);
2020 return hr;
2023 static HRESULT _SHRegisterCommonShellFolders(void)
2025 static const UINT folders[] = {
2026 CSIDL_COMMON_STARTMENU,
2027 CSIDL_COMMON_PROGRAMS,
2028 CSIDL_COMMON_STARTUP,
2029 CSIDL_COMMON_DESKTOPDIRECTORY,
2030 CSIDL_COMMON_FAVORITES,
2031 CSIDL_COMMON_APPDATA,
2032 CSIDL_COMMON_TEMPLATES,
2033 CSIDL_COMMON_DOCUMENTS,
2035 HRESULT hr;
2037 TRACE("\n");
2038 hr = _SHRegisterFolders(HKEY_LOCAL_MACHINE, NULL, szSHUserFolders,
2039 szSHFolders, folders, sizeof(folders) / sizeof(folders[0]));
2040 TRACE("returning 0x%08x\n", hr);
2041 return hr;
2044 /******************************************************************************
2045 * _SHAppendToUnixPath [Internal]
2047 * Helper function for _SHCreateSymbolicLinks. Appends pwszSubPath (or the
2048 * corresponding resource, if IS_INTRESOURCE) to the unix base path 'szBasePath'
2049 * and replaces backslashes with slashes.
2051 * PARAMS
2052 * szBasePath [IO] The unix base path, which will be appended to (CP_UNXICP).
2053 * pwszSubPath [I] Sub-path or resource id (use MAKEINTRESOURCEW).
2055 * RETURNS
2056 * Success: TRUE,
2057 * Failure: FALSE
2059 static inline BOOL _SHAppendToUnixPath(char *szBasePath, LPCWSTR pwszSubPath) {
2060 WCHAR wszSubPath[MAX_PATH];
2061 int cLen = strlen(szBasePath);
2062 char *pBackslash;
2064 if (IS_INTRESOURCE(pwszSubPath)) {
2065 if (!LoadStringW(shell32_hInstance, LOWORD(pwszSubPath), wszSubPath, MAX_PATH)) {
2066 /* Fall back to hard coded defaults. */
2067 switch (LOWORD(pwszSubPath)) {
2068 case IDS_PERSONAL:
2069 lstrcpyW(wszSubPath, PersonalW);
2070 break;
2071 case IDS_MYMUSIC:
2072 lstrcpyW(wszSubPath, My_MusicW);
2073 break;
2074 case IDS_MYPICTURES:
2075 lstrcpyW(wszSubPath, My_PicturesW);
2076 break;
2077 case IDS_MYVIDEO:
2078 lstrcpyW(wszSubPath, My_VideoW);
2079 break;
2080 default:
2081 ERR("LoadString(%d) failed!\n", LOWORD(pwszSubPath));
2082 return FALSE;
2085 } else {
2086 lstrcpyW(wszSubPath, pwszSubPath);
2089 if (szBasePath[cLen-1] != '/') szBasePath[cLen++] = '/';
2091 if (!WideCharToMultiByte(CP_UNIXCP, 0, wszSubPath, -1, szBasePath + cLen,
2092 FILENAME_MAX - cLen, NULL, NULL))
2094 return FALSE;
2097 pBackslash = szBasePath + cLen;
2098 while ((pBackslash = strchr(pBackslash, '\\'))) *pBackslash = '/';
2100 return TRUE;
2103 /******************************************************************************
2104 * _SHCreateSymbolicLinks [Internal]
2106 * Sets up symbol links for various shell folders to point into the users home
2107 * directory. We do an educated guess about what the user would probably want:
2108 * - If there is a 'My Documents' directory in $HOME, the user probably wants
2109 * wine's 'My Documents' to point there. Furthermore, we imply that the user
2110 * is a Windows lover and has no problem with wine creating 'My Pictures',
2111 * 'My Music' and 'My Video' subfolders under '$HOME/My Documents', if those
2112 * do not already exits. We put appropriate symbolic links in place for those,
2113 * too.
2114 * - If there is no 'My Documents' directory in $HOME, we let 'My Documents'
2115 * point directly to $HOME. We assume the user to be a unix hacker who does not
2116 * want wine to create anything anywhere besides the .wine directory. So, if
2117 * there already is a 'My Music' directory in $HOME, we symlink the 'My Music'
2118 * shell folder to it. But if not, then we check XDG_MUSIC_DIR - "well known"
2119 * directory, and try to link to that. If that fails, then we symlink to
2120 * $HOME directly. The same holds fo 'My Pictures' and 'My Video'.
2121 * - The Desktop shell folder is symlinked to XDG_DESKTOP_DIR. If that does not
2122 * exist, then we try '$HOME/Desktop'. If that does not exist, then we leave
2123 * it alone.
2124 * ('My Music',... above in fact means LoadString(IDS_MYMUSIC))
2126 static void _SHCreateSymbolicLinks(void)
2128 UINT aidsMyStuff[] = { IDS_MYPICTURES, IDS_MYVIDEO, IDS_MYMUSIC }, i;
2129 int acsidlMyStuff[] = { CSIDL_MYPICTURES, CSIDL_MYVIDEO, CSIDL_MYMUSIC };
2130 static const char * const xdg_dirs[] = { "PICTURES", "VIDEOS", "MUSIC", "DESKTOP" };
2131 static const unsigned int num = sizeof(xdg_dirs) / sizeof(xdg_dirs[0]);
2132 WCHAR wszTempPath[MAX_PATH];
2133 char szPersonalTarget[FILENAME_MAX], *pszPersonal;
2134 char szMyStuffTarget[FILENAME_MAX], *pszMyStuff;
2135 char szDesktopTarget[FILENAME_MAX], *pszDesktop;
2136 struct stat statFolder;
2137 const char *pszHome;
2138 HRESULT hr;
2139 char ** xdg_results;
2140 char * xdg_desktop_dir;
2142 /* Create all necessary profile sub-dirs up to 'My Documents' and get the unix path. */
2143 hr = SHGetFolderPathW(NULL, CSIDL_PERSONAL|CSIDL_FLAG_CREATE, NULL,
2144 SHGFP_TYPE_DEFAULT, wszTempPath);
2145 if (FAILED(hr)) return;
2146 pszPersonal = wine_get_unix_file_name(wszTempPath);
2147 if (!pszPersonal) return;
2149 hr = XDG_UserDirLookup(xdg_dirs, num, &xdg_results);
2150 if (FAILED(hr)) xdg_results = NULL;
2152 pszHome = getenv("HOME");
2153 if (pszHome && !stat(pszHome, &statFolder) && S_ISDIR(statFolder.st_mode)) {
2154 strcpy(szPersonalTarget, pszHome);
2155 if (_SHAppendToUnixPath(szPersonalTarget, MAKEINTRESOURCEW(IDS_PERSONAL)) &&
2156 !stat(szPersonalTarget, &statFolder) && S_ISDIR(statFolder.st_mode))
2158 /* '$HOME/My Documents' exists. Create 'My Pictures', 'My Videos' and
2159 * 'My Music' subfolders or fail silently if they already exist. */
2160 for (i = 0; i < sizeof(aidsMyStuff)/sizeof(aidsMyStuff[0]); i++) {
2161 strcpy(szMyStuffTarget, szPersonalTarget);
2162 if (_SHAppendToUnixPath(szMyStuffTarget, MAKEINTRESOURCEW(aidsMyStuff[i])))
2163 mkdir(szMyStuffTarget, 0777);
2166 else
2168 /* '$HOME/My Documents' doesn't exists, but '$HOME' does. */
2169 strcpy(szPersonalTarget, pszHome);
2172 /* Replace 'My Documents' directory with a symlink of fail silently if not empty. */
2173 rmdir(pszPersonal);
2174 symlink(szPersonalTarget, pszPersonal);
2176 else
2178 /* '$HOME' doesn't exist. Create 'My Pictures', 'My Videos' and 'My Music' subdirs
2179 * in '%USERPROFILE%\\My Documents' or fail silently if they already exist. */
2180 strcpy(szPersonalTarget, pszPersonal);
2181 for (i = 0; i < sizeof(aidsMyStuff)/sizeof(aidsMyStuff[0]); i++) {
2182 strcpy(szMyStuffTarget, szPersonalTarget);
2183 if (_SHAppendToUnixPath(szMyStuffTarget, MAKEINTRESOURCEW(aidsMyStuff[i])))
2184 mkdir(szMyStuffTarget, 0777);
2188 /* Create symbolic links for 'My Pictures', 'My Video' and 'My Music'. */
2189 for (i=0; i < sizeof(aidsMyStuff)/sizeof(aidsMyStuff[0]); i++) {
2190 /* Create the current 'My Whatever' folder and get it's unix path. */
2191 hr = SHGetFolderPathW(NULL, acsidlMyStuff[i]|CSIDL_FLAG_CREATE, NULL,
2192 SHGFP_TYPE_DEFAULT, wszTempPath);
2193 if (FAILED(hr)) continue;
2194 pszMyStuff = wine_get_unix_file_name(wszTempPath);
2195 if (!pszMyStuff) continue;
2197 strcpy(szMyStuffTarget, szPersonalTarget);
2198 if (_SHAppendToUnixPath(szMyStuffTarget, MAKEINTRESOURCEW(aidsMyStuff[i])) &&
2199 !stat(szMyStuffTarget, &statFolder) && S_ISDIR(statFolder.st_mode))
2201 /* If there's a 'My Whatever' directory where 'My Documents' links to, link to it. */
2202 rmdir(pszMyStuff);
2203 symlink(szMyStuffTarget, pszMyStuff);
2205 else
2207 rmdir(pszMyStuff);
2208 if (xdg_results && xdg_results[i])
2210 /* the folder specified by XDG_XXX_DIR exists, link to it. */
2211 symlink(xdg_results[i], pszMyStuff);
2213 else
2215 /* Else link to where 'My Documents' itself links to. */
2216 symlink(szPersonalTarget, pszMyStuff);
2219 HeapFree(GetProcessHeap(), 0, pszMyStuff);
2222 /* Last but not least, the Desktop folder */
2223 if (pszHome)
2224 strcpy(szDesktopTarget, pszHome);
2225 else
2226 strcpy(szDesktopTarget, pszPersonal);
2227 HeapFree(GetProcessHeap(), 0, pszPersonal);
2229 xdg_desktop_dir = xdg_results ? xdg_results[num - 1] : NULL;
2230 if (xdg_desktop_dir ||
2231 (_SHAppendToUnixPath(szDesktopTarget, DesktopW) &&
2232 !stat(szDesktopTarget, &statFolder) && S_ISDIR(statFolder.st_mode)))
2234 hr = SHGetFolderPathW(NULL, CSIDL_DESKTOPDIRECTORY|CSIDL_FLAG_CREATE, NULL,
2235 SHGFP_TYPE_DEFAULT, wszTempPath);
2236 if (SUCCEEDED(hr) && (pszDesktop = wine_get_unix_file_name(wszTempPath)))
2238 rmdir(pszDesktop);
2239 if (xdg_desktop_dir)
2240 symlink(xdg_desktop_dir, pszDesktop);
2241 else
2242 symlink(szDesktopTarget, pszDesktop);
2243 HeapFree(GetProcessHeap(), 0, pszDesktop);
2247 /* Free resources allocated by XDG_UserDirLookup() */
2248 if (xdg_results)
2250 for (i = 0; i < num; i++)
2251 HeapFree(GetProcessHeap(), 0, xdg_results[i]);
2252 HeapFree(GetProcessHeap(), 0, xdg_results);
2256 /* Register the default values in the registry, as some apps seem to depend
2257 * on their presence. The set registered was taken from Windows XP.
2259 HRESULT SHELL_RegisterShellFolders(void)
2261 HRESULT hr;
2263 /* Set up '$HOME' targeted symlinks for 'My Documents', 'My Pictures',
2264 * 'My Video', 'My Music' and 'Desktop' in advance, so that the
2265 * _SHRegister*ShellFolders() functions will find everything nice and clean
2266 * and thus will not attempt to create them in the profile directory. */
2267 _SHCreateSymbolicLinks();
2269 hr = _SHRegisterUserShellFolders(TRUE);
2270 if (SUCCEEDED(hr))
2271 hr = _SHRegisterUserShellFolders(FALSE);
2272 if (SUCCEEDED(hr))
2273 hr = _SHRegisterCommonShellFolders();
2274 return hr;
2277 /*************************************************************************
2278 * SHGetSpecialFolderPathA [SHELL32.@]
2280 BOOL WINAPI SHGetSpecialFolderPathA (
2281 HWND hwndOwner,
2282 LPSTR szPath,
2283 int nFolder,
2284 BOOL bCreate)
2286 return (SHGetFolderPathA(
2287 hwndOwner,
2288 nFolder + (bCreate ? CSIDL_FLAG_CREATE : 0),
2289 NULL,
2291 szPath)) == S_OK ? TRUE : FALSE;
2294 /*************************************************************************
2295 * SHGetSpecialFolderPathW
2297 BOOL WINAPI SHGetSpecialFolderPathW (
2298 HWND hwndOwner,
2299 LPWSTR szPath,
2300 int nFolder,
2301 BOOL bCreate)
2303 return (SHGetFolderPathW(
2304 hwndOwner,
2305 nFolder + (bCreate ? CSIDL_FLAG_CREATE : 0),
2306 NULL,
2308 szPath)) == S_OK ? TRUE : FALSE;
2311 /*************************************************************************
2312 * SHGetSpecialFolderPath (SHELL32.175)
2314 BOOL WINAPI SHGetSpecialFolderPathAW (
2315 HWND hwndOwner,
2316 LPVOID szPath,
2317 int nFolder,
2318 BOOL bCreate)
2321 if (SHELL_OsIsUnicode())
2322 return SHGetSpecialFolderPathW (hwndOwner, szPath, nFolder, bCreate);
2323 return SHGetSpecialFolderPathA (hwndOwner, szPath, nFolder, bCreate);
2326 /*************************************************************************
2327 * SHGetFolderLocation [SHELL32.@]
2329 * Gets the folder locations from the registry and creates a pidl.
2331 * PARAMS
2332 * hwndOwner [I]
2333 * nFolder [I] CSIDL_xxxxx
2334 * hToken [I] token representing user, or NULL for current user, or -1 for
2335 * default user
2336 * dwReserved [I] must be zero
2337 * ppidl [O] PIDL of a special folder
2339 * RETURNS
2340 * Success: S_OK
2341 * Failure: Standard OLE-defined error result, S_FALSE or E_INVALIDARG
2343 * NOTES
2344 * Creates missing reg keys and directories.
2345 * Mostly forwards to SHGetFolderPathW, but a few values of nFolder return
2346 * virtual folders that are handled here.
2348 HRESULT WINAPI SHGetFolderLocation(
2349 HWND hwndOwner,
2350 int nFolder,
2351 HANDLE hToken,
2352 DWORD dwReserved,
2353 LPITEMIDLIST *ppidl)
2355 HRESULT hr = E_INVALIDARG;
2357 TRACE("%p 0x%08x %p 0x%08x %p\n",
2358 hwndOwner, nFolder, hToken, dwReserved, ppidl);
2360 if (!ppidl)
2361 return E_INVALIDARG;
2362 if (dwReserved)
2363 return E_INVALIDARG;
2365 /* The virtual folders' locations are not user-dependent */
2366 *ppidl = NULL;
2367 switch (nFolder)
2369 case CSIDL_DESKTOP:
2370 *ppidl = _ILCreateDesktop();
2371 break;
2373 case CSIDL_PERSONAL:
2374 *ppidl = _ILCreateMyDocuments();
2375 break;
2377 case CSIDL_INTERNET:
2378 *ppidl = _ILCreateIExplore();
2379 break;
2381 case CSIDL_CONTROLS:
2382 *ppidl = _ILCreateControlPanel();
2383 break;
2385 case CSIDL_PRINTERS:
2386 *ppidl = _ILCreatePrinters();
2387 break;
2389 case CSIDL_BITBUCKET:
2390 *ppidl = _ILCreateBitBucket();
2391 break;
2393 case CSIDL_DRIVES:
2394 *ppidl = _ILCreateMyComputer();
2395 break;
2397 case CSIDL_NETWORK:
2398 *ppidl = _ILCreateNetwork();
2399 break;
2401 default:
2403 WCHAR szPath[MAX_PATH];
2405 hr = SHGetFolderPathW(hwndOwner, nFolder, hToken,
2406 SHGFP_TYPE_CURRENT, szPath);
2407 if (SUCCEEDED(hr))
2409 DWORD attributes=0;
2411 TRACE("Value=%s\n", debugstr_w(szPath));
2412 hr = SHILCreateFromPathW(szPath, ppidl, &attributes);
2414 else if (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
2416 /* unlike SHGetFolderPath, SHGetFolderLocation in shell32
2417 * version 6.0 returns E_FAIL for nonexistent paths
2419 hr = E_FAIL;
2423 if(*ppidl)
2424 hr = NOERROR;
2426 TRACE("-- (new pidl %p)\n",*ppidl);
2427 return hr;
2430 /*************************************************************************
2431 * SHGetSpecialFolderLocation [SHELL32.@]
2433 * NOTES
2434 * In NT5, SHGetSpecialFolderLocation needs the <winntdir>/Recent
2435 * directory.
2437 HRESULT WINAPI SHGetSpecialFolderLocation(
2438 HWND hwndOwner,
2439 INT nFolder,
2440 LPITEMIDLIST * ppidl)
2442 HRESULT hr = E_INVALIDARG;
2444 TRACE("(%p,0x%x,%p)\n", hwndOwner,nFolder,ppidl);
2446 if (!ppidl)
2447 return E_INVALIDARG;
2449 hr = SHGetFolderLocation(hwndOwner, nFolder, NULL, 0, ppidl);
2450 return hr;