wined3d: Fix a comment.
[wine/hramrach.git] / dlls / shell32 / shellpath.c
blob451d6fff3455bab880544221e833caed6e3be5b6
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 Common_FavoritesW[] = {'C','o','m','m','o','n',' ','F','a','v','o','r','i','t','e','s','\0'};
768 static const WCHAR CommonFilesDirW[] = {'C','o','m','m','o','n','F','i','l','e','s','D','i','r','\0'};
769 static const WCHAR CommonMusicW[] = {'C','o','m','m','o','n','M','u','s','i','c','\0'};
770 static const WCHAR CommonPicturesW[] = {'C','o','m','m','o','n','P','i','c','t','u','r','e','s','\0'};
771 static const WCHAR Common_ProgramsW[] = {'C','o','m','m','o','n',' ','P','r','o','g','r','a','m','s','\0'};
772 static const WCHAR Common_StartUpW[] = {'C','o','m','m','o','n',' ','S','t','a','r','t','U','p','\0'};
773 static const WCHAR Common_Start_MenuW[] = {'C','o','m','m','o','n',' ','S','t','a','r','t',' ','M','e','n','u','\0'};
774 static const WCHAR Common_TemplatesW[] = {'C','o','m','m','o','n',' ','T','e','m','p','l','a','t','e','s','\0'};
775 static const WCHAR CommonVideoW[] = {'C','o','m','m','o','n','V','i','d','e','o','\0'};
776 static const WCHAR CookiesW[] = {'C','o','o','k','i','e','s','\0'};
777 static const WCHAR DesktopW[] = {'D','e','s','k','t','o','p','\0'};
778 static const WCHAR FavoritesW[] = {'F','a','v','o','r','i','t','e','s','\0'};
779 static const WCHAR FontsW[] = {'F','o','n','t','s','\0'};
780 static const WCHAR HistoryW[] = {'H','i','s','t','o','r','y','\0'};
781 static const WCHAR Local_AppDataW[] = {'L','o','c','a','l',' ','A','p','p','D','a','t','a','\0'};
782 static const WCHAR My_MusicW[] = {'M','y',' ','M','u','s','i','c','\0'};
783 static const WCHAR My_PicturesW[] = {'M','y',' ','P','i','c','t','u','r','e','s','\0'};
784 static const WCHAR My_VideoW[] = {'M','y',' ','V','i','d','e','o','s','\0'};
785 static const WCHAR NetHoodW[] = {'N','e','t','H','o','o','d','\0'};
786 static const WCHAR PersonalW[] = {'P','e','r','s','o','n','a','l','\0'};
787 static const WCHAR PrintHoodW[] = {'P','r','i','n','t','H','o','o','d','\0'};
788 static const WCHAR ProgramFilesDirW[] = {'P','r','o','g','r','a','m','F','i','l','e','s','D','i','r','\0'};
789 static const WCHAR ProgramsW[] = {'P','r','o','g','r','a','m','s','\0'};
790 static const WCHAR RecentW[] = {'R','e','c','e','n','t','\0'};
791 static const WCHAR ResourcesW[] = {'R','e','s','o','u','r','c','e','s','\0'};
792 static const WCHAR SendToW[] = {'S','e','n','d','T','o','\0'};
793 static const WCHAR StartUpW[] = {'S','t','a','r','t','U','p','\0'};
794 static const WCHAR Start_MenuW[] = {'S','t','a','r','t',' ','M','e','n','u','\0'};
795 static const WCHAR TemplatesW[] = {'T','e','m','p','l','a','t','e','s','\0'};
796 static const WCHAR DefaultW[] = {'.','D','e','f','a','u','l','t','\0'};
797 static const WCHAR AllUsersProfileW[] = {'%','A','L','L','U','S','E','R','S','P','R','O','F','I','L','E','%','\0'};
798 static const WCHAR UserProfileW[] = {'%','U','S','E','R','P','R','O','F','I','L','E','%','\0'};
799 static const WCHAR SystemDriveW[] = {'%','S','y','s','t','e','m','D','r','i','v','e','%','\0'};
800 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};
801 static const WCHAR ProfilesDirectoryW[] = {'P','r','o','f','i','l','e','s','D','i','r','e','c','t','o','r','y',0};
802 static const WCHAR AllUsersProfileValueW[] = {'A','l','l','U','s','e','r','s','P','r','o','f','i','l','e','\0'};
803 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'};
804 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'};
805 static const WCHAR szDefaultProfileDirW[] = {'u','s','e','r','s',0};
806 static const WCHAR AllUsersW[] = {'P','u','b','l','i','c',0};
808 typedef enum _CSIDL_Type {
809 CSIDL_Type_User,
810 CSIDL_Type_AllUsers,
811 CSIDL_Type_CurrVer,
812 CSIDL_Type_Disallowed,
813 CSIDL_Type_NonExistent,
814 CSIDL_Type_WindowsPath,
815 CSIDL_Type_SystemPath,
816 CSIDL_Type_SystemX86Path,
817 } CSIDL_Type;
819 typedef struct
821 CSIDL_Type type;
822 LPCWSTR szValueName;
823 LPCWSTR szDefaultPath; /* fallback string or resource ID */
824 } CSIDL_DATA;
826 static const CSIDL_DATA CSIDL_Data[] =
828 { /* 0x00 - CSIDL_DESKTOP */
829 CSIDL_Type_User,
830 DesktopW,
831 MAKEINTRESOURCEW(IDS_DESKTOPDIRECTORY)
833 { /* 0x01 - CSIDL_INTERNET */
834 CSIDL_Type_Disallowed,
835 NULL,
836 NULL
838 { /* 0x02 - CSIDL_PROGRAMS */
839 CSIDL_Type_User,
840 ProgramsW,
841 MAKEINTRESOURCEW(IDS_PROGRAMS)
843 { /* 0x03 - CSIDL_CONTROLS (.CPL files) */
844 CSIDL_Type_SystemPath,
845 NULL,
846 NULL
848 { /* 0x04 - CSIDL_PRINTERS */
849 CSIDL_Type_SystemPath,
850 NULL,
851 NULL
853 { /* 0x05 - CSIDL_PERSONAL */
854 CSIDL_Type_User,
855 PersonalW,
856 MAKEINTRESOURCEW(IDS_PERSONAL)
858 { /* 0x06 - CSIDL_FAVORITES */
859 CSIDL_Type_User,
860 FavoritesW,
861 MAKEINTRESOURCEW(IDS_FAVORITES)
863 { /* 0x07 - CSIDL_STARTUP */
864 CSIDL_Type_User,
865 StartUpW,
866 MAKEINTRESOURCEW(IDS_STARTUP)
868 { /* 0x08 - CSIDL_RECENT */
869 CSIDL_Type_User,
870 RecentW,
871 MAKEINTRESOURCEW(IDS_RECENT)
873 { /* 0x09 - CSIDL_SENDTO */
874 CSIDL_Type_User,
875 SendToW,
876 MAKEINTRESOURCEW(IDS_SENDTO)
878 { /* 0x0a - CSIDL_BITBUCKET - Recycle Bin */
879 CSIDL_Type_Disallowed,
880 NULL,
881 NULL,
883 { /* 0x0b - CSIDL_STARTMENU */
884 CSIDL_Type_User,
885 Start_MenuW,
886 MAKEINTRESOURCEW(IDS_STARTMENU)
888 { /* 0x0c - CSIDL_MYDOCUMENTS */
889 CSIDL_Type_Disallowed, /* matches WinXP--can't get its path */
890 NULL,
891 NULL
893 { /* 0x0d - CSIDL_MYMUSIC */
894 CSIDL_Type_User,
895 My_MusicW,
896 MAKEINTRESOURCEW(IDS_MYMUSIC)
898 { /* 0x0e - CSIDL_MYVIDEO */
899 CSIDL_Type_User,
900 My_VideoW,
901 MAKEINTRESOURCEW(IDS_MYVIDEO)
903 { /* 0x0f - unassigned */
904 CSIDL_Type_Disallowed,
905 NULL,
906 NULL,
908 { /* 0x10 - CSIDL_DESKTOPDIRECTORY */
909 CSIDL_Type_User,
910 DesktopW,
911 MAKEINTRESOURCEW(IDS_DESKTOPDIRECTORY)
913 { /* 0x11 - CSIDL_DRIVES */
914 CSIDL_Type_Disallowed,
915 NULL,
916 NULL,
918 { /* 0x12 - CSIDL_NETWORK */
919 CSIDL_Type_Disallowed,
920 NULL,
921 NULL,
923 { /* 0x13 - CSIDL_NETHOOD */
924 CSIDL_Type_User,
925 NetHoodW,
926 MAKEINTRESOURCEW(IDS_NETHOOD)
928 { /* 0x14 - CSIDL_FONTS */
929 CSIDL_Type_WindowsPath,
930 FontsW,
931 FontsW
933 { /* 0x15 - CSIDL_TEMPLATES */
934 CSIDL_Type_User,
935 TemplatesW,
936 MAKEINTRESOURCEW(IDS_TEMPLATES)
938 { /* 0x16 - CSIDL_COMMON_STARTMENU */
939 CSIDL_Type_AllUsers,
940 Common_Start_MenuW,
941 MAKEINTRESOURCEW(IDS_STARTMENU)
943 { /* 0x17 - CSIDL_COMMON_PROGRAMS */
944 CSIDL_Type_AllUsers,
945 Common_ProgramsW,
946 MAKEINTRESOURCEW(IDS_PROGRAMS)
948 { /* 0x18 - CSIDL_COMMON_STARTUP */
949 CSIDL_Type_AllUsers,
950 Common_StartUpW,
951 MAKEINTRESOURCEW(IDS_STARTUP)
953 { /* 0x19 - CSIDL_COMMON_DESKTOPDIRECTORY */
954 CSIDL_Type_AllUsers,
955 Common_DesktopW,
956 MAKEINTRESOURCEW(IDS_DESKTOPDIRECTORY)
958 { /* 0x1a - CSIDL_APPDATA */
959 CSIDL_Type_User,
960 AppDataW,
961 MAKEINTRESOURCEW(IDS_APPDATA)
963 { /* 0x1b - CSIDL_PRINTHOOD */
964 CSIDL_Type_User,
965 PrintHoodW,
966 MAKEINTRESOURCEW(IDS_PRINTHOOD)
968 { /* 0x1c - CSIDL_LOCAL_APPDATA */
969 CSIDL_Type_User,
970 Local_AppDataW,
971 MAKEINTRESOURCEW(IDS_LOCAL_APPDATA)
973 { /* 0x1d - CSIDL_ALTSTARTUP */
974 CSIDL_Type_NonExistent,
975 NULL,
976 NULL
978 { /* 0x1e - CSIDL_COMMON_ALTSTARTUP */
979 CSIDL_Type_NonExistent,
980 NULL,
981 NULL
983 { /* 0x1f - CSIDL_COMMON_FAVORITES */
984 CSIDL_Type_AllUsers,
985 Common_FavoritesW,
986 MAKEINTRESOURCEW(IDS_FAVORITES)
988 { /* 0x20 - CSIDL_INTERNET_CACHE */
989 CSIDL_Type_User,
990 CacheW,
991 MAKEINTRESOURCEW(IDS_INTERNET_CACHE)
993 { /* 0x21 - CSIDL_COOKIES */
994 CSIDL_Type_User,
995 CookiesW,
996 MAKEINTRESOURCEW(IDS_COOKIES)
998 { /* 0x22 - CSIDL_HISTORY */
999 CSIDL_Type_User,
1000 HistoryW,
1001 MAKEINTRESOURCEW(IDS_HISTORY)
1003 { /* 0x23 - CSIDL_COMMON_APPDATA */
1004 CSIDL_Type_AllUsers,
1005 Common_AppDataW,
1006 MAKEINTRESOURCEW(IDS_APPDATA)
1008 { /* 0x24 - CSIDL_WINDOWS */
1009 CSIDL_Type_WindowsPath,
1010 NULL,
1011 NULL
1013 { /* 0x25 - CSIDL_SYSTEM */
1014 CSIDL_Type_SystemPath,
1015 NULL,
1016 NULL
1018 { /* 0x26 - CSIDL_PROGRAM_FILES */
1019 CSIDL_Type_CurrVer,
1020 ProgramFilesDirW,
1021 MAKEINTRESOURCEW(IDS_PROGRAM_FILES)
1023 { /* 0x27 - CSIDL_MYPICTURES */
1024 CSIDL_Type_User,
1025 My_PicturesW,
1026 MAKEINTRESOURCEW(IDS_MYPICTURES)
1028 { /* 0x28 - CSIDL_PROFILE */
1029 CSIDL_Type_User,
1030 NULL,
1031 NULL
1033 { /* 0x29 - CSIDL_SYSTEMX86 */
1034 CSIDL_Type_SystemX86Path,
1035 NULL,
1036 NULL
1038 { /* 0x2a - CSIDL_PROGRAM_FILESX86 */
1039 CSIDL_Type_NonExistent,
1040 NULL,
1041 NULL
1043 { /* 0x2b - CSIDL_PROGRAM_FILES_COMMON */
1044 CSIDL_Type_CurrVer,
1045 CommonFilesDirW,
1046 MAKEINTRESOURCEW(IDS_PROGRAM_FILES_COMMON)
1048 { /* 0x2c - CSIDL_PROGRAM_FILES_COMMONX86 */
1049 CSIDL_Type_NonExistent,
1050 NULL,
1051 NULL
1053 { /* 0x2d - CSIDL_COMMON_TEMPLATES */
1054 CSIDL_Type_AllUsers,
1055 Common_TemplatesW,
1056 MAKEINTRESOURCEW(IDS_TEMPLATES)
1058 { /* 0x2e - CSIDL_COMMON_DOCUMENTS */
1059 CSIDL_Type_AllUsers,
1060 Common_DocumentsW,
1061 MAKEINTRESOURCEW(IDS_COMMON_DOCUMENTS)
1063 { /* 0x2f - CSIDL_COMMON_ADMINTOOLS */
1064 CSIDL_Type_AllUsers,
1065 Common_Administrative_ToolsW,
1066 MAKEINTRESOURCEW(IDS_ADMINTOOLS)
1068 { /* 0x30 - CSIDL_ADMINTOOLS */
1069 CSIDL_Type_User,
1070 Administrative_ToolsW,
1071 MAKEINTRESOURCEW(IDS_ADMINTOOLS)
1073 { /* 0x31 - CSIDL_CONNECTIONS */
1074 CSIDL_Type_Disallowed,
1075 NULL,
1076 NULL
1078 { /* 0x32 - unassigned */
1079 CSIDL_Type_Disallowed,
1080 NULL,
1081 NULL
1083 { /* 0x33 - unassigned */
1084 CSIDL_Type_Disallowed,
1085 NULL,
1086 NULL
1088 { /* 0x34 - unassigned */
1089 CSIDL_Type_Disallowed,
1090 NULL,
1091 NULL
1093 { /* 0x35 - CSIDL_COMMON_MUSIC */
1094 CSIDL_Type_AllUsers,
1095 CommonMusicW,
1096 MAKEINTRESOURCEW(IDS_COMMON_MUSIC)
1098 { /* 0x36 - CSIDL_COMMON_PICTURES */
1099 CSIDL_Type_AllUsers,
1100 CommonPicturesW,
1101 MAKEINTRESOURCEW(IDS_COMMON_PICTURES)
1103 { /* 0x37 - CSIDL_COMMON_VIDEO */
1104 CSIDL_Type_AllUsers,
1105 CommonVideoW,
1106 MAKEINTRESOURCEW(IDS_COMMON_VIDEO)
1108 { /* 0x38 - CSIDL_RESOURCES */
1109 CSIDL_Type_WindowsPath,
1110 NULL,
1111 ResourcesW
1113 { /* 0x39 - CSIDL_RESOURCES_LOCALIZED */
1114 CSIDL_Type_NonExistent,
1115 NULL,
1116 NULL
1118 { /* 0x3a - CSIDL_COMMON_OEM_LINKS */
1119 CSIDL_Type_NonExistent,
1120 NULL,
1121 NULL
1123 { /* 0x3b - CSIDL_CDBURN_AREA */
1124 CSIDL_Type_User,
1125 CD_BurningW,
1126 MAKEINTRESOURCEW(IDS_CDBURN_AREA)
1128 { /* 0x3c unassigned */
1129 CSIDL_Type_Disallowed,
1130 NULL,
1131 NULL
1133 { /* 0x3d - CSIDL_COMPUTERSNEARME */
1134 CSIDL_Type_Disallowed, /* FIXME */
1135 NULL,
1136 NULL
1138 { /* 0x3e - CSIDL_PROFILES */
1139 CSIDL_Type_Disallowed, /* oddly, this matches WinXP */
1140 NULL,
1141 NULL
1145 static HRESULT _SHExpandEnvironmentStrings(LPCWSTR szSrc, LPWSTR szDest);
1147 /* Gets the value named value from the registry key
1148 * rootKey\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
1149 * (or from rootKey\userPrefix\... if userPrefix is not NULL) into path, which
1150 * is assumed to be MAX_PATH WCHARs in length.
1151 * If it exists, expands the value and writes the expanded value to
1152 * rootKey\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders
1153 * Returns successful error code if the value was retrieved from the registry,
1154 * and a failure otherwise.
1156 static HRESULT _SHGetUserShellFolderPath(HKEY rootKey, LPCWSTR userPrefix,
1157 LPCWSTR value, LPWSTR path)
1159 HRESULT hr;
1160 WCHAR shellFolderPath[MAX_PATH], userShellFolderPath[MAX_PATH];
1161 LPCWSTR pShellFolderPath, pUserShellFolderPath;
1162 DWORD dwType, dwPathLen = MAX_PATH;
1163 HKEY userShellFolderKey, shellFolderKey;
1165 TRACE("%p,%s,%s,%p\n",rootKey, debugstr_w(userPrefix), debugstr_w(value),
1166 path);
1168 if (userPrefix)
1170 strcpyW(shellFolderPath, userPrefix);
1171 PathAddBackslashW(shellFolderPath);
1172 strcatW(shellFolderPath, szSHFolders);
1173 pShellFolderPath = shellFolderPath;
1174 strcpyW(userShellFolderPath, userPrefix);
1175 PathAddBackslashW(userShellFolderPath);
1176 strcatW(userShellFolderPath, szSHUserFolders);
1177 pUserShellFolderPath = userShellFolderPath;
1179 else
1181 pUserShellFolderPath = szSHUserFolders;
1182 pShellFolderPath = szSHFolders;
1185 if (RegCreateKeyW(rootKey, pShellFolderPath, &shellFolderKey))
1187 TRACE("Failed to create %s\n", debugstr_w(pShellFolderPath));
1188 return E_FAIL;
1190 if (RegCreateKeyW(rootKey, pUserShellFolderPath, &userShellFolderKey))
1192 TRACE("Failed to create %s\n",
1193 debugstr_w(pUserShellFolderPath));
1194 RegCloseKey(shellFolderKey);
1195 return E_FAIL;
1198 if (!RegQueryValueExW(userShellFolderKey, value, NULL, &dwType,
1199 (LPBYTE)path, &dwPathLen) && (dwType == REG_EXPAND_SZ || dwType == REG_SZ))
1201 LONG ret;
1203 path[dwPathLen / sizeof(WCHAR)] = '\0';
1204 if (dwType == REG_EXPAND_SZ && path[0] == '%')
1206 WCHAR szTemp[MAX_PATH];
1208 _SHExpandEnvironmentStrings(path, szTemp);
1209 lstrcpynW(path, szTemp, MAX_PATH);
1211 ret = RegSetValueExW(shellFolderKey, value, 0, REG_SZ, (LPBYTE)path,
1212 (strlenW(path) + 1) * sizeof(WCHAR));
1213 if (ret != ERROR_SUCCESS)
1214 hr = HRESULT_FROM_WIN32(ret);
1215 else
1216 hr = S_OK;
1218 else
1219 hr = E_FAIL;
1220 RegCloseKey(shellFolderKey);
1221 RegCloseKey(userShellFolderKey);
1222 TRACE("returning 0x%08x\n", hr);
1223 return hr;
1226 /* Gets a 'semi-expanded' default value of the CSIDL with index folder into
1227 * pszPath, based on the entries in CSIDL_Data. By semi-expanded, I mean:
1228 * - The entry's szDefaultPath may be either a string value or an integer
1229 * resource identifier. In the latter case, the string value of the resource
1230 * is written.
1231 * - Depending on the entry's type, the path may begin with an (unexpanded)
1232 * environment variable name. The caller is responsible for expanding
1233 * environment strings if so desired.
1234 * The types that are prepended with environment variables are:
1235 * CSIDL_Type_User: %USERPROFILE%
1236 * CSIDL_Type_AllUsers: %ALLUSERSPROFILE%
1237 * CSIDL_Type_CurrVer: %SystemDrive%
1238 * (Others might make sense too, but as yet are unneeded.)
1240 static HRESULT _SHGetDefaultValue(BYTE folder, LPWSTR pszPath)
1242 HRESULT hr;
1243 WCHAR resourcePath[MAX_PATH];
1244 LPCWSTR pDefaultPath = NULL;
1246 TRACE("0x%02x,%p\n", folder, pszPath);
1248 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1249 return E_INVALIDARG;
1250 if (!pszPath)
1251 return E_INVALIDARG;
1253 if (CSIDL_Data[folder].szDefaultPath &&
1254 IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath))
1256 if (LoadStringW(shell32_hInstance,
1257 LOWORD(CSIDL_Data[folder].szDefaultPath), resourcePath, MAX_PATH))
1259 hr = S_OK;
1260 pDefaultPath = resourcePath;
1262 else
1264 FIXME("(%d,%s), LoadString failed, missing translation?\n", folder,
1265 debugstr_w(pszPath));
1266 hr = E_FAIL;
1269 else
1271 hr = S_OK;
1272 pDefaultPath = CSIDL_Data[folder].szDefaultPath;
1274 if (SUCCEEDED(hr))
1276 switch (CSIDL_Data[folder].type)
1278 case CSIDL_Type_User:
1279 strcpyW(pszPath, UserProfileW);
1280 break;
1281 case CSIDL_Type_AllUsers:
1282 strcpyW(pszPath, AllUsersProfileW);
1283 break;
1284 case CSIDL_Type_CurrVer:
1285 strcpyW(pszPath, SystemDriveW);
1286 break;
1287 default:
1288 ; /* no corresponding env. var, do nothing */
1290 if (pDefaultPath)
1292 PathAddBackslashW(pszPath);
1293 strcatW(pszPath, pDefaultPath);
1296 TRACE("returning 0x%08x\n", hr);
1297 return hr;
1300 /* Gets the (unexpanded) value of the folder with index folder into pszPath.
1301 * The folder's type is assumed to be CSIDL_Type_CurrVer. Its default value
1302 * can be overridden in the HKLM\\szCurrentVersion key.
1303 * If dwFlags has SHGFP_TYPE_DEFAULT set or if the value isn't overridden in
1304 * the registry, uses _SHGetDefaultValue to get the value.
1306 static HRESULT _SHGetCurrentVersionPath(DWORD dwFlags, BYTE folder,
1307 LPWSTR pszPath)
1309 HRESULT hr;
1311 TRACE("0x%08x,0x%02x,%p\n", dwFlags, folder, pszPath);
1313 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1314 return E_INVALIDARG;
1315 if (CSIDL_Data[folder].type != CSIDL_Type_CurrVer)
1316 return E_INVALIDARG;
1317 if (!pszPath)
1318 return E_INVALIDARG;
1320 if (dwFlags & SHGFP_TYPE_DEFAULT)
1321 hr = _SHGetDefaultValue(folder, pszPath);
1322 else
1324 HKEY hKey;
1326 if (RegCreateKeyW(HKEY_LOCAL_MACHINE, szCurrentVersion, &hKey))
1327 hr = E_FAIL;
1328 else
1330 DWORD dwType, dwPathLen = MAX_PATH * sizeof(WCHAR);
1332 if (RegQueryValueExW(hKey, CSIDL_Data[folder].szValueName, NULL,
1333 &dwType, (LPBYTE)pszPath, &dwPathLen) ||
1334 (dwType != REG_SZ && dwType != REG_EXPAND_SZ))
1336 hr = _SHGetDefaultValue(folder, pszPath);
1337 dwType = REG_EXPAND_SZ;
1338 RegSetValueExW(hKey, CSIDL_Data[folder].szValueName, 0, dwType,
1339 (LPBYTE)pszPath, (strlenW(pszPath)+1)*sizeof(WCHAR));
1341 else
1343 pszPath[dwPathLen / sizeof(WCHAR)] = '\0';
1344 hr = S_OK;
1346 RegCloseKey(hKey);
1349 TRACE("returning 0x%08x (output path is %s)\n", hr, debugstr_w(pszPath));
1350 return hr;
1353 static LPWSTR _GetUserSidStringFromToken(HANDLE Token)
1355 char InfoBuffer[64];
1356 PTOKEN_USER UserInfo;
1357 DWORD InfoSize;
1358 LPWSTR SidStr;
1360 UserInfo = (PTOKEN_USER) InfoBuffer;
1361 if (! GetTokenInformation(Token, TokenUser, InfoBuffer, sizeof(InfoBuffer),
1362 &InfoSize))
1364 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
1365 return NULL;
1366 UserInfo = HeapAlloc(GetProcessHeap(), 0, InfoSize);
1367 if (UserInfo == NULL)
1368 return NULL;
1369 if (! GetTokenInformation(Token, TokenUser, UserInfo, InfoSize,
1370 &InfoSize))
1372 HeapFree(GetProcessHeap(), 0, UserInfo);
1373 return NULL;
1377 if (! ConvertSidToStringSidW(UserInfo->User.Sid, &SidStr))
1378 SidStr = NULL;
1380 if (UserInfo != (PTOKEN_USER) InfoBuffer)
1381 HeapFree(GetProcessHeap(), 0, UserInfo);
1383 return SidStr;
1386 /* Gets the user's path (unexpanded) for the CSIDL with index folder:
1387 * If SHGFP_TYPE_DEFAULT is set, calls _SHGetDefaultValue for it. Otherwise
1388 * calls _SHGetUserShellFolderPath for it. Where it looks depends on hToken:
1389 * - if hToken is -1, looks in HKEY_USERS\.Default
1390 * - otherwise looks first in HKEY_CURRENT_USER, followed by HKEY_LOCAL_MACHINE
1391 * if HKEY_CURRENT_USER doesn't contain any entries. If both fail, finally
1392 * calls _SHGetDefaultValue for it.
1394 static HRESULT _SHGetUserProfilePath(HANDLE hToken, DWORD dwFlags, BYTE folder,
1395 LPWSTR pszPath)
1397 HRESULT hr;
1399 TRACE("%p,0x%08x,0x%02x,%p\n", hToken, dwFlags, folder, pszPath);
1401 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1402 return E_INVALIDARG;
1403 if (CSIDL_Data[folder].type != CSIDL_Type_User)
1404 return E_INVALIDARG;
1405 if (!pszPath)
1406 return E_INVALIDARG;
1408 if (dwFlags & SHGFP_TYPE_DEFAULT)
1410 if (hToken != NULL && hToken != (HANDLE)-1)
1412 FIXME("unsupported for user other than current or default\n");
1413 return E_FAIL;
1415 hr = _SHGetDefaultValue(folder, pszPath);
1417 else
1419 LPCWSTR userPrefix = NULL;
1420 HKEY hRootKey;
1422 if (hToken == (HANDLE)-1)
1424 hRootKey = HKEY_USERS;
1425 userPrefix = DefaultW;
1427 else if (hToken == NULL)
1428 hRootKey = HKEY_CURRENT_USER;
1429 else
1431 hRootKey = HKEY_USERS;
1432 userPrefix = _GetUserSidStringFromToken(hToken);
1433 if (userPrefix == NULL)
1435 hr = E_FAIL;
1436 goto error;
1439 hr = _SHGetUserShellFolderPath(hRootKey, userPrefix,
1440 CSIDL_Data[folder].szValueName, pszPath);
1441 if (FAILED(hr) && hRootKey != HKEY_LOCAL_MACHINE)
1442 hr = _SHGetUserShellFolderPath(HKEY_LOCAL_MACHINE, NULL,
1443 CSIDL_Data[folder].szValueName, pszPath);
1444 if (FAILED(hr))
1445 hr = _SHGetDefaultValue(folder, pszPath);
1446 if (userPrefix != NULL && userPrefix != DefaultW)
1447 LocalFree((HLOCAL) userPrefix);
1449 error:
1450 TRACE("returning 0x%08x (output path is %s)\n", hr, debugstr_w(pszPath));
1451 return hr;
1454 /* Gets the (unexpanded) path for the CSIDL with index folder. If dwFlags has
1455 * SHGFP_TYPE_DEFAULT set, calls _SHGetDefaultValue. Otherwise calls
1456 * _SHGetUserShellFolderPath for it, looking only in HKEY_LOCAL_MACHINE.
1457 * If this fails, falls back to _SHGetDefaultValue.
1459 static HRESULT _SHGetAllUsersProfilePath(DWORD dwFlags, BYTE folder,
1460 LPWSTR pszPath)
1462 HRESULT hr;
1464 TRACE("0x%08x,0x%02x,%p\n", dwFlags, folder, pszPath);
1466 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1467 return E_INVALIDARG;
1468 if (CSIDL_Data[folder].type != CSIDL_Type_AllUsers)
1469 return E_INVALIDARG;
1470 if (!pszPath)
1471 return E_INVALIDARG;
1473 if (dwFlags & SHGFP_TYPE_DEFAULT)
1474 hr = _SHGetDefaultValue(folder, pszPath);
1475 else
1477 hr = _SHGetUserShellFolderPath(HKEY_LOCAL_MACHINE, NULL,
1478 CSIDL_Data[folder].szValueName, pszPath);
1479 if (FAILED(hr))
1480 hr = _SHGetDefaultValue(folder, pszPath);
1482 TRACE("returning 0x%08x (output path is %s)\n", hr, debugstr_w(pszPath));
1483 return hr;
1486 static HRESULT _SHOpenProfilesKey(PHKEY pKey)
1488 LONG lRet;
1489 DWORD disp;
1491 lRet = RegCreateKeyExW(HKEY_LOCAL_MACHINE, ProfileListW, 0, NULL, 0,
1492 KEY_ALL_ACCESS, NULL, pKey, &disp);
1493 return HRESULT_FROM_WIN32(lRet);
1496 /* Reads the value named szValueName from the key profilesKey (assumed to be
1497 * opened by _SHOpenProfilesKey) into szValue, which is assumed to be MAX_PATH
1498 * WCHARs in length. If it doesn't exist, returns szDefault (and saves
1499 * szDefault to the registry).
1501 static HRESULT _SHGetProfilesValue(HKEY profilesKey, LPCWSTR szValueName,
1502 LPWSTR szValue, LPCWSTR szDefault)
1504 HRESULT hr;
1505 DWORD type, dwPathLen = MAX_PATH * sizeof(WCHAR);
1506 LONG lRet;
1508 TRACE("%p,%s,%p,%s\n", profilesKey, debugstr_w(szValueName), szValue,
1509 debugstr_w(szDefault));
1510 lRet = RegQueryValueExW(profilesKey, szValueName, NULL, &type,
1511 (LPBYTE)szValue, &dwPathLen);
1512 if (!lRet && (type == REG_SZ || type == REG_EXPAND_SZ) && dwPathLen
1513 && *szValue)
1515 dwPathLen /= sizeof(WCHAR);
1516 szValue[dwPathLen] = '\0';
1517 hr = S_OK;
1519 else
1521 /* Missing or invalid value, set a default */
1522 lstrcpynW(szValue, szDefault, MAX_PATH);
1523 TRACE("Setting missing value %s to %s\n", debugstr_w(szValueName),
1524 debugstr_w(szValue));
1525 lRet = RegSetValueExW(profilesKey, szValueName, 0, REG_EXPAND_SZ,
1526 (LPBYTE)szValue,
1527 (strlenW(szValue) + 1) * sizeof(WCHAR));
1528 if (lRet)
1529 hr = HRESULT_FROM_WIN32(lRet);
1530 else
1531 hr = S_OK;
1533 TRACE("returning 0x%08x (output value is %s)\n", hr, debugstr_w(szValue));
1534 return hr;
1537 /* Attempts to expand environment variables from szSrc into szDest, which is
1538 * assumed to be MAX_PATH characters in length. Before referring to the
1539 * environment, handles a few variables directly, because the environment
1540 * variables may not be set when this is called (as during Wine's installation
1541 * when default values are being written to the registry).
1542 * The directly handled environment variables, and their source, are:
1543 * - ALLUSERSPROFILE, USERPROFILE: reads from the registry
1544 * - SystemDrive: uses GetSystemDirectoryW and uses the drive portion of its
1545 * path
1546 * If one of the directly handled environment variables is expanded, only
1547 * expands a single variable, and only in the beginning of szSrc.
1549 static HRESULT _SHExpandEnvironmentStrings(LPCWSTR szSrc, LPWSTR szDest)
1551 HRESULT hr;
1552 WCHAR szTemp[MAX_PATH], szProfilesPrefix[MAX_PATH] = { 0 };
1553 HKEY key = NULL;
1555 TRACE("%s, %p\n", debugstr_w(szSrc), szDest);
1557 if (!szSrc || !szDest) return E_INVALIDARG;
1559 /* short-circuit if there's nothing to expand */
1560 if (szSrc[0] != '%')
1562 strcpyW(szDest, szSrc);
1563 hr = S_OK;
1564 goto end;
1566 /* Get the profile prefix, we'll probably be needing it */
1567 hr = _SHOpenProfilesKey(&key);
1568 if (SUCCEEDED(hr))
1570 WCHAR def_val[MAX_PATH];
1572 /* get the system drive */
1573 GetSystemDirectoryW(def_val, MAX_PATH);
1574 if (def_val[1] == ':') strcpyW( def_val + 3, szDefaultProfileDirW );
1575 else FIXME("non-drive system paths unsupported\n");
1577 hr = _SHGetProfilesValue(key, ProfilesDirectoryW, szProfilesPrefix, def_val );
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_SystemX86Path:
1788 if (!GetSystemWow64DirectoryW(szTemp, MAX_PATH)) GetSystemDirectoryW(szTemp, MAX_PATH);
1789 if (CSIDL_Data[folder].szDefaultPath &&
1790 !IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath) &&
1791 *CSIDL_Data[folder].szDefaultPath)
1793 PathAddBackslashW(szTemp);
1794 strcatW(szTemp, CSIDL_Data[folder].szDefaultPath);
1796 hr = S_OK;
1797 break;
1798 case CSIDL_Type_CurrVer:
1799 hr = _SHGetCurrentVersionPath(dwFlags, folder, szTemp);
1800 break;
1801 case CSIDL_Type_User:
1802 hr = _SHGetUserProfilePath(hToken, dwFlags, folder, szTemp);
1803 break;
1804 case CSIDL_Type_AllUsers:
1805 hr = _SHGetAllUsersProfilePath(dwFlags, folder, szTemp);
1806 break;
1807 default:
1808 FIXME("bogus type %d, please fix\n", type);
1809 hr = E_INVALIDARG;
1810 break;
1813 /* Expand environment strings if necessary */
1814 if (*szTemp == '%')
1815 hr = _SHExpandEnvironmentStrings(szTemp, szBuildPath);
1816 else
1817 strcpyW(szBuildPath, szTemp);
1819 if (FAILED(hr)) goto end;
1821 if(pszSubPath) {
1822 /* make sure the new path does not exceed th bufferlength
1823 * rememebr to backslash and the termination */
1824 if(MAX_PATH < (lstrlenW(szBuildPath) + lstrlenW(pszSubPath) + 2)) {
1825 hr = HRESULT_FROM_WIN32(ERROR_FILENAME_EXCED_RANGE);
1826 goto end;
1828 PathAppendW(szBuildPath, pszSubPath);
1829 PathRemoveBackslashW(szBuildPath);
1831 /* Copy the path if it's available before we might return */
1832 if (SUCCEEDED(hr) && pszPath)
1833 strcpyW(pszPath, szBuildPath);
1835 /* if we don't care about existing directories we are ready */
1836 if(nFolder & CSIDL_FLAG_DONT_VERIFY) goto end;
1838 if (PathFileExistsW(szBuildPath)) goto end;
1840 /* not existing but we are not allowed to create it. The return value
1841 * is verified against shell32 version 6.0.
1843 if (!(nFolder & CSIDL_FLAG_CREATE))
1845 hr = HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND);
1846 goto end;
1849 /* create directory/directories */
1850 ret = SHCreateDirectoryExW(hwndOwner, szBuildPath, NULL);
1851 if (ret && ret != ERROR_ALREADY_EXISTS)
1853 ERR("Failed to create directory %s.\n", debugstr_w(szBuildPath));
1854 hr = E_FAIL;
1855 goto end;
1858 TRACE("Created missing system directory %s\n", debugstr_w(szBuildPath));
1859 end:
1860 TRACE("returning 0x%08x (final path is %s)\n", hr, debugstr_w(szBuildPath));
1861 return hr;
1864 /*************************************************************************
1865 * SHGetFolderPathA [SHELL32.@]
1867 * See SHGetFolderPathW.
1869 HRESULT WINAPI SHGetFolderPathA(
1870 HWND hwndOwner,
1871 int nFolder,
1872 HANDLE hToken,
1873 DWORD dwFlags,
1874 LPSTR pszPath)
1876 WCHAR szTemp[MAX_PATH];
1877 HRESULT hr;
1879 TRACE("%p,%p,nFolder=0x%04x\n",hwndOwner,pszPath,nFolder);
1881 if (pszPath)
1882 *pszPath = '\0';
1883 hr = SHGetFolderPathW(hwndOwner, nFolder, hToken, dwFlags, szTemp);
1884 if (SUCCEEDED(hr) && pszPath)
1885 WideCharToMultiByte(CP_ACP, 0, szTemp, -1, pszPath, MAX_PATH, NULL,
1886 NULL);
1888 return hr;
1891 /* For each folder in folders, if its value has not been set in the registry,
1892 * calls _SHGetUserProfilePath or _SHGetAllUsersProfilePath (depending on the
1893 * folder's type) to get the unexpanded value first.
1894 * Writes the unexpanded value to User Shell Folders, and queries it with
1895 * SHGetFolderPathW to force the creation of the directory if it doesn't
1896 * already exist. SHGetFolderPathW also returns the expanded value, which
1897 * this then writes to Shell Folders.
1899 static HRESULT _SHRegisterFolders(HKEY hRootKey, HANDLE hToken,
1900 LPCWSTR szUserShellFolderPath, LPCWSTR szShellFolderPath, const UINT folders[],
1901 UINT foldersLen)
1903 UINT i;
1904 WCHAR path[MAX_PATH];
1905 HRESULT hr = S_OK;
1906 HKEY hUserKey = NULL, hKey = NULL;
1907 DWORD dwType, dwPathLen;
1908 LONG ret;
1910 TRACE("%p,%p,%s,%p,%u\n", hRootKey, hToken,
1911 debugstr_w(szUserShellFolderPath), folders, foldersLen);
1913 ret = RegCreateKeyW(hRootKey, szUserShellFolderPath, &hUserKey);
1914 if (ret)
1915 hr = HRESULT_FROM_WIN32(ret);
1916 else
1918 ret = RegCreateKeyW(hRootKey, szShellFolderPath, &hKey);
1919 if (ret)
1920 hr = HRESULT_FROM_WIN32(ret);
1922 for (i = 0; SUCCEEDED(hr) && i < foldersLen; i++)
1924 dwPathLen = MAX_PATH * sizeof(WCHAR);
1925 if (RegQueryValueExW(hUserKey, CSIDL_Data[folders[i]].szValueName, NULL,
1926 &dwType, (LPBYTE)path, &dwPathLen) || (dwType != REG_SZ &&
1927 dwType != REG_EXPAND_SZ))
1929 *path = '\0';
1930 if (CSIDL_Data[folders[i]].type == CSIDL_Type_User)
1931 _SHGetUserProfilePath(hToken, SHGFP_TYPE_DEFAULT, folders[i],
1932 path);
1933 else if (CSIDL_Data[folders[i]].type == CSIDL_Type_AllUsers)
1934 _SHGetAllUsersProfilePath(SHGFP_TYPE_DEFAULT, folders[i], path);
1935 else if (CSIDL_Data[folders[i]].type == CSIDL_Type_WindowsPath)
1937 GetWindowsDirectoryW(path, MAX_PATH);
1938 if (CSIDL_Data[folders[i]].szDefaultPath &&
1939 !IS_INTRESOURCE(CSIDL_Data[folders[i]].szDefaultPath))
1941 PathAddBackslashW(path);
1942 strcatW(path, CSIDL_Data[folders[i]].szDefaultPath);
1945 else
1946 hr = E_FAIL;
1947 if (*path)
1949 ret = RegSetValueExW(hUserKey,
1950 CSIDL_Data[folders[i]].szValueName, 0, REG_EXPAND_SZ,
1951 (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR));
1952 if (ret)
1953 hr = HRESULT_FROM_WIN32(ret);
1954 else
1956 hr = SHGetFolderPathW(NULL, folders[i] | CSIDL_FLAG_CREATE,
1957 hToken, SHGFP_TYPE_DEFAULT, path);
1958 ret = RegSetValueExW(hKey,
1959 CSIDL_Data[folders[i]].szValueName, 0, REG_SZ,
1960 (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR));
1961 if (ret)
1962 hr = HRESULT_FROM_WIN32(ret);
1967 if (hUserKey)
1968 RegCloseKey(hUserKey);
1969 if (hKey)
1970 RegCloseKey(hKey);
1972 TRACE("returning 0x%08x\n", hr);
1973 return hr;
1976 static HRESULT _SHRegisterUserShellFolders(BOOL bDefault)
1978 static const UINT folders[] = {
1979 CSIDL_PROGRAMS,
1980 CSIDL_PERSONAL,
1981 CSIDL_FAVORITES,
1982 CSIDL_APPDATA,
1983 CSIDL_STARTUP,
1984 CSIDL_RECENT,
1985 CSIDL_SENDTO,
1986 CSIDL_STARTMENU,
1987 CSIDL_MYMUSIC,
1988 CSIDL_MYVIDEO,
1989 CSIDL_DESKTOPDIRECTORY,
1990 CSIDL_NETHOOD,
1991 CSIDL_TEMPLATES,
1992 CSIDL_PRINTHOOD,
1993 CSIDL_LOCAL_APPDATA,
1994 CSIDL_INTERNET_CACHE,
1995 CSIDL_COOKIES,
1996 CSIDL_HISTORY,
1997 CSIDL_MYPICTURES,
1998 CSIDL_FONTS
2000 WCHAR userShellFolderPath[MAX_PATH], shellFolderPath[MAX_PATH];
2001 LPCWSTR pUserShellFolderPath, pShellFolderPath;
2002 HRESULT hr = S_OK;
2003 HKEY hRootKey;
2004 HANDLE hToken;
2006 TRACE("%s\n", bDefault ? "TRUE" : "FALSE");
2007 if (bDefault)
2009 hToken = (HANDLE)-1;
2010 hRootKey = HKEY_USERS;
2011 strcpyW(userShellFolderPath, DefaultW);
2012 PathAddBackslashW(userShellFolderPath);
2013 strcatW(userShellFolderPath, szSHUserFolders);
2014 pUserShellFolderPath = userShellFolderPath;
2015 strcpyW(shellFolderPath, DefaultW);
2016 PathAddBackslashW(shellFolderPath);
2017 strcatW(shellFolderPath, szSHFolders);
2018 pShellFolderPath = shellFolderPath;
2020 else
2022 hToken = NULL;
2023 hRootKey = HKEY_CURRENT_USER;
2024 pUserShellFolderPath = szSHUserFolders;
2025 pShellFolderPath = szSHFolders;
2028 hr = _SHRegisterFolders(hRootKey, hToken, pUserShellFolderPath,
2029 pShellFolderPath, folders, sizeof(folders) / sizeof(folders[0]));
2030 TRACE("returning 0x%08x\n", hr);
2031 return hr;
2034 static HRESULT _SHRegisterCommonShellFolders(void)
2036 static const UINT folders[] = {
2037 CSIDL_COMMON_STARTMENU,
2038 CSIDL_COMMON_PROGRAMS,
2039 CSIDL_COMMON_STARTUP,
2040 CSIDL_COMMON_DESKTOPDIRECTORY,
2041 CSIDL_COMMON_FAVORITES,
2042 CSIDL_COMMON_APPDATA,
2043 CSIDL_COMMON_TEMPLATES,
2044 CSIDL_COMMON_DOCUMENTS,
2045 CSIDL_COMMON_ADMINTOOLS,
2046 CSIDL_COMMON_MUSIC,
2047 CSIDL_COMMON_PICTURES,
2048 CSIDL_COMMON_VIDEO,
2050 HRESULT hr;
2052 TRACE("\n");
2053 hr = _SHRegisterFolders(HKEY_LOCAL_MACHINE, NULL, szSHUserFolders,
2054 szSHFolders, folders, sizeof(folders) / sizeof(folders[0]));
2055 TRACE("returning 0x%08x\n", hr);
2056 return hr;
2059 /******************************************************************************
2060 * _SHAppendToUnixPath [Internal]
2062 * Helper function for _SHCreateSymbolicLinks. Appends pwszSubPath (or the
2063 * corresponding resource, if IS_INTRESOURCE) to the unix base path 'szBasePath'
2064 * and replaces backslashes with slashes.
2066 * PARAMS
2067 * szBasePath [IO] The unix base path, which will be appended to (CP_UNXICP).
2068 * pwszSubPath [I] Sub-path or resource id (use MAKEINTRESOURCEW).
2070 * RETURNS
2071 * Success: TRUE,
2072 * Failure: FALSE
2074 static inline BOOL _SHAppendToUnixPath(char *szBasePath, LPCWSTR pwszSubPath) {
2075 WCHAR wszSubPath[MAX_PATH];
2076 int cLen = strlen(szBasePath);
2077 char *pBackslash;
2079 if (IS_INTRESOURCE(pwszSubPath)) {
2080 if (!LoadStringW(shell32_hInstance, LOWORD(pwszSubPath), wszSubPath, MAX_PATH)) {
2081 /* Fall back to hard coded defaults. */
2082 switch (LOWORD(pwszSubPath)) {
2083 case IDS_PERSONAL:
2084 lstrcpyW(wszSubPath, PersonalW);
2085 break;
2086 case IDS_MYMUSIC:
2087 lstrcpyW(wszSubPath, My_MusicW);
2088 break;
2089 case IDS_MYPICTURES:
2090 lstrcpyW(wszSubPath, My_PicturesW);
2091 break;
2092 case IDS_MYVIDEO:
2093 lstrcpyW(wszSubPath, My_VideoW);
2094 break;
2095 default:
2096 ERR("LoadString(%d) failed!\n", LOWORD(pwszSubPath));
2097 return FALSE;
2100 } else {
2101 lstrcpyW(wszSubPath, pwszSubPath);
2104 if (szBasePath[cLen-1] != '/') szBasePath[cLen++] = '/';
2106 if (!WideCharToMultiByte(CP_UNIXCP, 0, wszSubPath, -1, szBasePath + cLen,
2107 FILENAME_MAX - cLen, NULL, NULL))
2109 return FALSE;
2112 pBackslash = szBasePath + cLen;
2113 while ((pBackslash = strchr(pBackslash, '\\'))) *pBackslash = '/';
2115 return TRUE;
2118 /******************************************************************************
2119 * _SHCreateSymbolicLinks [Internal]
2121 * Sets up symbol links for various shell folders to point into the users home
2122 * directory. We do an educated guess about what the user would probably want:
2123 * - If there is a 'My Documents' directory in $HOME, the user probably wants
2124 * wine's 'My Documents' to point there. Furthermore, we imply that the user
2125 * is a Windows lover and has no problem with wine creating 'My Pictures',
2126 * 'My Music' and 'My Video' subfolders under '$HOME/My Documents', if those
2127 * do not already exits. We put appropriate symbolic links in place for those,
2128 * too.
2129 * - If there is no 'My Documents' directory in $HOME, we let 'My Documents'
2130 * point directly to $HOME. We assume the user to be a unix hacker who does not
2131 * want wine to create anything anywhere besides the .wine directory. So, if
2132 * there already is a 'My Music' directory in $HOME, we symlink the 'My Music'
2133 * shell folder to it. But if not, then we check XDG_MUSIC_DIR - "well known"
2134 * directory, and try to link to that. If that fails, then we symlink to
2135 * $HOME directly. The same holds fo 'My Pictures' and 'My Video'.
2136 * - The Desktop shell folder is symlinked to XDG_DESKTOP_DIR. If that does not
2137 * exist, then we try '$HOME/Desktop'. If that does not exist, then we leave
2138 * it alone.
2139 * ('My Music',... above in fact means LoadString(IDS_MYMUSIC))
2141 static void _SHCreateSymbolicLinks(void)
2143 UINT aidsMyStuff[] = { IDS_MYPICTURES, IDS_MYVIDEO, IDS_MYMUSIC }, i;
2144 int acsidlMyStuff[] = { CSIDL_MYPICTURES, CSIDL_MYVIDEO, CSIDL_MYMUSIC };
2145 static const char * const xdg_dirs[] = { "PICTURES", "VIDEOS", "MUSIC", "DESKTOP" };
2146 static const unsigned int num = sizeof(xdg_dirs) / sizeof(xdg_dirs[0]);
2147 WCHAR wszTempPath[MAX_PATH];
2148 char szPersonalTarget[FILENAME_MAX], *pszPersonal;
2149 char szMyStuffTarget[FILENAME_MAX], *pszMyStuff;
2150 char szDesktopTarget[FILENAME_MAX], *pszDesktop;
2151 struct stat statFolder;
2152 const char *pszHome;
2153 HRESULT hr;
2154 char ** xdg_results;
2155 char * xdg_desktop_dir;
2157 /* Create all necessary profile sub-dirs up to 'My Documents' and get the unix path. */
2158 hr = SHGetFolderPathW(NULL, CSIDL_PERSONAL|CSIDL_FLAG_CREATE, NULL,
2159 SHGFP_TYPE_DEFAULT, wszTempPath);
2160 if (FAILED(hr)) return;
2161 pszPersonal = wine_get_unix_file_name(wszTempPath);
2162 if (!pszPersonal) return;
2164 hr = XDG_UserDirLookup(xdg_dirs, num, &xdg_results);
2165 if (FAILED(hr)) xdg_results = NULL;
2167 pszHome = getenv("HOME");
2168 if (pszHome && !stat(pszHome, &statFolder) && S_ISDIR(statFolder.st_mode)) {
2169 strcpy(szPersonalTarget, pszHome);
2170 if (_SHAppendToUnixPath(szPersonalTarget, MAKEINTRESOURCEW(IDS_PERSONAL)) &&
2171 !stat(szPersonalTarget, &statFolder) && S_ISDIR(statFolder.st_mode))
2173 /* '$HOME/My Documents' exists. Create 'My Pictures', 'My Videos' and
2174 * 'My Music' subfolders or fail silently if they already exist. */
2175 for (i = 0; i < sizeof(aidsMyStuff)/sizeof(aidsMyStuff[0]); i++) {
2176 strcpy(szMyStuffTarget, szPersonalTarget);
2177 if (_SHAppendToUnixPath(szMyStuffTarget, MAKEINTRESOURCEW(aidsMyStuff[i])))
2178 mkdir(szMyStuffTarget, 0777);
2181 else
2183 /* '$HOME/My Documents' doesn't exists, but '$HOME' does. */
2184 strcpy(szPersonalTarget, pszHome);
2187 /* Replace 'My Documents' directory with a symlink of fail silently if not empty. */
2188 rmdir(pszPersonal);
2189 symlink(szPersonalTarget, pszPersonal);
2191 else
2193 /* '$HOME' doesn't exist. Create 'My Pictures', 'My Videos' and 'My Music' subdirs
2194 * in '%USERPROFILE%\\My Documents' or fail silently if they already exist. */
2195 strcpy(szPersonalTarget, pszPersonal);
2196 for (i = 0; i < sizeof(aidsMyStuff)/sizeof(aidsMyStuff[0]); i++) {
2197 strcpy(szMyStuffTarget, szPersonalTarget);
2198 if (_SHAppendToUnixPath(szMyStuffTarget, MAKEINTRESOURCEW(aidsMyStuff[i])))
2199 mkdir(szMyStuffTarget, 0777);
2203 /* Create symbolic links for 'My Pictures', 'My Video' and 'My Music'. */
2204 for (i=0; i < sizeof(aidsMyStuff)/sizeof(aidsMyStuff[0]); i++) {
2205 /* Create the current 'My Whatever' folder and get it's unix path. */
2206 hr = SHGetFolderPathW(NULL, acsidlMyStuff[i]|CSIDL_FLAG_CREATE, NULL,
2207 SHGFP_TYPE_DEFAULT, wszTempPath);
2208 if (FAILED(hr)) continue;
2209 pszMyStuff = wine_get_unix_file_name(wszTempPath);
2210 if (!pszMyStuff) continue;
2212 strcpy(szMyStuffTarget, szPersonalTarget);
2213 if (_SHAppendToUnixPath(szMyStuffTarget, MAKEINTRESOURCEW(aidsMyStuff[i])) &&
2214 !stat(szMyStuffTarget, &statFolder) && S_ISDIR(statFolder.st_mode))
2216 /* If there's a 'My Whatever' directory where 'My Documents' links to, link to it. */
2217 rmdir(pszMyStuff);
2218 symlink(szMyStuffTarget, pszMyStuff);
2220 else
2222 rmdir(pszMyStuff);
2223 if (xdg_results && xdg_results[i])
2225 /* the folder specified by XDG_XXX_DIR exists, link to it. */
2226 symlink(xdg_results[i], pszMyStuff);
2228 else
2230 /* Else link to where 'My Documents' itself links to. */
2231 symlink(szPersonalTarget, pszMyStuff);
2234 HeapFree(GetProcessHeap(), 0, pszMyStuff);
2237 /* Last but not least, the Desktop folder */
2238 if (pszHome)
2239 strcpy(szDesktopTarget, pszHome);
2240 else
2241 strcpy(szDesktopTarget, pszPersonal);
2242 HeapFree(GetProcessHeap(), 0, pszPersonal);
2244 xdg_desktop_dir = xdg_results ? xdg_results[num - 1] : NULL;
2245 if (xdg_desktop_dir ||
2246 (_SHAppendToUnixPath(szDesktopTarget, DesktopW) &&
2247 !stat(szDesktopTarget, &statFolder) && S_ISDIR(statFolder.st_mode)))
2249 hr = SHGetFolderPathW(NULL, CSIDL_DESKTOPDIRECTORY|CSIDL_FLAG_CREATE, NULL,
2250 SHGFP_TYPE_DEFAULT, wszTempPath);
2251 if (SUCCEEDED(hr) && (pszDesktop = wine_get_unix_file_name(wszTempPath)))
2253 rmdir(pszDesktop);
2254 if (xdg_desktop_dir)
2255 symlink(xdg_desktop_dir, pszDesktop);
2256 else
2257 symlink(szDesktopTarget, pszDesktop);
2258 HeapFree(GetProcessHeap(), 0, pszDesktop);
2262 /* Free resources allocated by XDG_UserDirLookup() */
2263 if (xdg_results)
2265 for (i = 0; i < num; i++)
2266 HeapFree(GetProcessHeap(), 0, xdg_results[i]);
2267 HeapFree(GetProcessHeap(), 0, xdg_results);
2271 /******************************************************************************
2272 * create_extra_folders [Internal]
2274 * Create some extra folders that don't have a standard CSIDL definition.
2276 static HRESULT create_extra_folders(void)
2278 static const WCHAR environW[] = {'E','n','v','i','r','o','n','m','e','n','t',0};
2279 static const WCHAR TempW[] = {'T','e','m','p',0};
2280 static const WCHAR TEMPW[] = {'T','E','M','P',0};
2281 static const WCHAR TMPW[] = {'T','M','P',0};
2282 WCHAR path[MAX_PATH+5];
2283 HRESULT hr;
2284 HKEY hkey;
2285 DWORD type, size, ret;
2287 ret = RegCreateKeyW( HKEY_CURRENT_USER, environW, &hkey );
2288 if (ret) return HRESULT_FROM_WIN32( ret );
2290 /* FIXME: should be under AppData, but we don't want spaces in the temp path */
2291 hr = SHGetFolderPathAndSubDirW( 0, CSIDL_PROFILE | CSIDL_FLAG_CREATE, NULL,
2292 SHGFP_TYPE_DEFAULT, TempW, path );
2293 if (SUCCEEDED(hr))
2295 size = sizeof(path);
2296 if (RegQueryValueExW( hkey, TEMPW, NULL, &type, (LPBYTE)path, &size ))
2297 RegSetValueExW( hkey, TEMPW, 0, REG_SZ, (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR) );
2298 size = sizeof(path);
2299 if (RegQueryValueExW( hkey, TMPW, NULL, &type, (LPBYTE)path, &size ))
2300 RegSetValueExW( hkey, TMPW, 0, REG_SZ, (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR) );
2302 RegCloseKey( hkey );
2303 return hr;
2307 /* Register the default values in the registry, as some apps seem to depend
2308 * on their presence. The set registered was taken from Windows XP.
2310 HRESULT SHELL_RegisterShellFolders(void)
2312 HRESULT hr;
2314 /* Set up '$HOME' targeted symlinks for 'My Documents', 'My Pictures',
2315 * 'My Video', 'My Music' and 'Desktop' in advance, so that the
2316 * _SHRegister*ShellFolders() functions will find everything nice and clean
2317 * and thus will not attempt to create them in the profile directory. */
2318 _SHCreateSymbolicLinks();
2320 hr = _SHRegisterUserShellFolders(TRUE);
2321 if (SUCCEEDED(hr))
2322 hr = _SHRegisterUserShellFolders(FALSE);
2323 if (SUCCEEDED(hr))
2324 hr = _SHRegisterCommonShellFolders();
2325 if (SUCCEEDED(hr))
2326 hr = create_extra_folders();
2327 return hr;
2330 /*************************************************************************
2331 * SHGetSpecialFolderPathA [SHELL32.@]
2333 BOOL WINAPI SHGetSpecialFolderPathA (
2334 HWND hwndOwner,
2335 LPSTR szPath,
2336 int nFolder,
2337 BOOL bCreate)
2339 return (SHGetFolderPathA(
2340 hwndOwner,
2341 nFolder + (bCreate ? CSIDL_FLAG_CREATE : 0),
2342 NULL,
2344 szPath)) == S_OK ? TRUE : FALSE;
2347 /*************************************************************************
2348 * SHGetSpecialFolderPathW
2350 BOOL WINAPI SHGetSpecialFolderPathW (
2351 HWND hwndOwner,
2352 LPWSTR szPath,
2353 int nFolder,
2354 BOOL bCreate)
2356 return (SHGetFolderPathW(
2357 hwndOwner,
2358 nFolder + (bCreate ? CSIDL_FLAG_CREATE : 0),
2359 NULL,
2361 szPath)) == S_OK ? TRUE : FALSE;
2364 /*************************************************************************
2365 * SHGetSpecialFolderPath (SHELL32.175)
2367 BOOL WINAPI SHGetSpecialFolderPathAW (
2368 HWND hwndOwner,
2369 LPVOID szPath,
2370 int nFolder,
2371 BOOL bCreate)
2374 if (SHELL_OsIsUnicode())
2375 return SHGetSpecialFolderPathW (hwndOwner, szPath, nFolder, bCreate);
2376 return SHGetSpecialFolderPathA (hwndOwner, szPath, nFolder, bCreate);
2379 /*************************************************************************
2380 * SHGetFolderLocation [SHELL32.@]
2382 * Gets the folder locations from the registry and creates a pidl.
2384 * PARAMS
2385 * hwndOwner [I]
2386 * nFolder [I] CSIDL_xxxxx
2387 * hToken [I] token representing user, or NULL for current user, or -1 for
2388 * default user
2389 * dwReserved [I] must be zero
2390 * ppidl [O] PIDL of a special folder
2392 * RETURNS
2393 * Success: S_OK
2394 * Failure: Standard OLE-defined error result, S_FALSE or E_INVALIDARG
2396 * NOTES
2397 * Creates missing reg keys and directories.
2398 * Mostly forwards to SHGetFolderPathW, but a few values of nFolder return
2399 * virtual folders that are handled here.
2401 HRESULT WINAPI SHGetFolderLocation(
2402 HWND hwndOwner,
2403 int nFolder,
2404 HANDLE hToken,
2405 DWORD dwReserved,
2406 LPITEMIDLIST *ppidl)
2408 HRESULT hr = E_INVALIDARG;
2410 TRACE("%p 0x%08x %p 0x%08x %p\n",
2411 hwndOwner, nFolder, hToken, dwReserved, ppidl);
2413 if (!ppidl)
2414 return E_INVALIDARG;
2415 if (dwReserved)
2416 return E_INVALIDARG;
2418 /* The virtual folders' locations are not user-dependent */
2419 *ppidl = NULL;
2420 switch (nFolder & CSIDL_FOLDER_MASK)
2422 case CSIDL_DESKTOP:
2423 *ppidl = _ILCreateDesktop();
2424 break;
2426 case CSIDL_PERSONAL:
2427 *ppidl = _ILCreateMyDocuments();
2428 break;
2430 case CSIDL_INTERNET:
2431 *ppidl = _ILCreateIExplore();
2432 break;
2434 case CSIDL_CONTROLS:
2435 *ppidl = _ILCreateControlPanel();
2436 break;
2438 case CSIDL_PRINTERS:
2439 *ppidl = _ILCreatePrinters();
2440 break;
2442 case CSIDL_BITBUCKET:
2443 *ppidl = _ILCreateBitBucket();
2444 break;
2446 case CSIDL_DRIVES:
2447 *ppidl = _ILCreateMyComputer();
2448 break;
2450 case CSIDL_NETWORK:
2451 *ppidl = _ILCreateNetwork();
2452 break;
2454 default:
2456 WCHAR szPath[MAX_PATH];
2458 hr = SHGetFolderPathW(hwndOwner, nFolder, hToken,
2459 SHGFP_TYPE_CURRENT, szPath);
2460 if (SUCCEEDED(hr))
2462 DWORD attributes=0;
2464 TRACE("Value=%s\n", debugstr_w(szPath));
2465 hr = SHILCreateFromPathW(szPath, ppidl, &attributes);
2467 else if (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
2469 /* unlike SHGetFolderPath, SHGetFolderLocation in shell32
2470 * version 6.0 returns E_FAIL for nonexistent paths
2472 hr = E_FAIL;
2476 if(*ppidl)
2477 hr = NOERROR;
2479 TRACE("-- (new pidl %p)\n",*ppidl);
2480 return hr;
2483 /*************************************************************************
2484 * SHGetSpecialFolderLocation [SHELL32.@]
2486 * NOTES
2487 * In NT5, SHGetSpecialFolderLocation needs the <winntdir>/Recent
2488 * directory.
2490 HRESULT WINAPI SHGetSpecialFolderLocation(
2491 HWND hwndOwner,
2492 INT nFolder,
2493 LPITEMIDLIST * ppidl)
2495 HRESULT hr = E_INVALIDARG;
2497 TRACE("(%p,0x%x,%p)\n", hwndOwner,nFolder,ppidl);
2499 if (!ppidl)
2500 return E_INVALIDARG;
2502 hr = SHGetFolderLocation(hwndOwner, nFolder, NULL, 0, ppidl);
2503 return hr;