Added OLEIVERB_INPLACEACTIVATE implementation in IOleObject::DoVerb.
[wine.git] / dlls / shell32 / shellpath.c
blobab1d3d7825b9d9e04b496b8a314b8f7316ac83c7
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * NOTES:
23 * Many of these functions are in SHLWAPI.DLL also
27 #include "config.h"
28 #include "wine/port.h"
30 #include <stdarg.h>
31 #include <string.h>
32 #include <ctype.h>
33 #include "wine/debug.h"
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winnls.h"
37 #include "winreg.h"
38 #include "wingdi.h"
39 #include "winuser.h"
41 #include "shlobj.h"
42 #include "shresdef.h"
43 #include "shell32_main.h"
44 #include "undocshell.h"
45 #include "pidl.h"
46 #include "wine/unicode.h"
47 #include "shlwapi.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(shell);
52 ########## Combining and Constructing paths ##########
55 /*************************************************************************
56 * PathAppend [SHELL32.36]
58 BOOL WINAPI PathAppendAW(
59 LPVOID lpszPath1,
60 LPCVOID lpszPath2)
62 if (SHELL_OsIsUnicode())
63 return PathAppendW(lpszPath1, lpszPath2);
64 return PathAppendA(lpszPath1, lpszPath2);
67 /*************************************************************************
68 * PathCombine [SHELL32.37]
70 LPVOID WINAPI PathCombineAW(
71 LPVOID szDest,
72 LPCVOID lpszDir,
73 LPCVOID lpszFile)
75 if (SHELL_OsIsUnicode())
76 return PathCombineW( szDest, lpszDir, lpszFile );
77 return PathCombineA( szDest, lpszDir, lpszFile );
80 /*************************************************************************
81 * PathAddBackslash [SHELL32.32]
83 LPVOID WINAPI PathAddBackslashAW(LPVOID lpszPath)
85 if(SHELL_OsIsUnicode())
86 return PathAddBackslashW(lpszPath);
87 return PathAddBackslashA(lpszPath);
90 /*************************************************************************
91 * PathBuildRoot [SHELL32.30]
93 LPVOID WINAPI PathBuildRootAW(LPVOID lpszPath, int drive)
95 if(SHELL_OsIsUnicode())
96 return PathBuildRootW(lpszPath, drive);
97 return PathBuildRootA(lpszPath, drive);
101 Extracting Component Parts
104 /*************************************************************************
105 * PathFindFileName [SHELL32.34]
107 LPVOID WINAPI PathFindFileNameAW(LPCVOID lpszPath)
109 if(SHELL_OsIsUnicode())
110 return PathFindFileNameW(lpszPath);
111 return PathFindFileNameA(lpszPath);
114 /*************************************************************************
115 * PathFindExtension [SHELL32.31]
117 LPVOID WINAPI PathFindExtensionAW(LPCVOID lpszPath)
119 if (SHELL_OsIsUnicode())
120 return PathFindExtensionW(lpszPath);
121 return PathFindExtensionA(lpszPath);
125 /*************************************************************************
126 * PathGetExtensionA [internal]
128 * NOTES
129 * exported by ordinal
130 * return value points to the first char after the dot
132 static LPSTR PathGetExtensionA(LPCSTR lpszPath)
134 TRACE("(%s)\n",lpszPath);
136 lpszPath = PathFindExtensionA(lpszPath);
137 return (LPSTR)(*lpszPath?(lpszPath+1):lpszPath);
140 /*************************************************************************
141 * PathGetExtensionW [internal]
143 static LPWSTR PathGetExtensionW(LPCWSTR lpszPath)
145 TRACE("(%s)\n",debugstr_w(lpszPath));
147 lpszPath = PathFindExtensionW(lpszPath);
148 return (LPWSTR)(*lpszPath?(lpszPath+1):lpszPath);
151 /*************************************************************************
152 * PathGetExtension [SHELL32.158]
154 LPVOID WINAPI PathGetExtensionAW(LPCVOID lpszPath,DWORD void1, DWORD void2)
156 if (SHELL_OsIsUnicode())
157 return PathGetExtensionW(lpszPath);
158 return PathGetExtensionA(lpszPath);
161 /*************************************************************************
162 * PathGetArgs [SHELL32.52]
164 LPVOID WINAPI PathGetArgsAW(LPVOID lpszPath)
166 if (SHELL_OsIsUnicode())
167 return PathGetArgsW(lpszPath);
168 return PathGetArgsA(lpszPath);
171 /*************************************************************************
172 * PathGetDriveNumber [SHELL32.57]
174 int WINAPI PathGetDriveNumberAW(LPVOID lpszPath)
176 if (SHELL_OsIsUnicode())
177 return PathGetDriveNumberW(lpszPath);
178 return PathGetDriveNumberA(lpszPath);
181 /*************************************************************************
182 * PathRemoveFileSpec [SHELL32.35]
184 BOOL WINAPI PathRemoveFileSpecAW(LPVOID lpszPath)
186 if (SHELL_OsIsUnicode())
187 return PathRemoveFileSpecW(lpszPath);
188 return PathRemoveFileSpecA(lpszPath);
191 /*************************************************************************
192 * PathStripPath [SHELL32.38]
194 void WINAPI PathStripPathAW(LPVOID lpszPath)
196 if (SHELL_OsIsUnicode())
197 PathStripPathW(lpszPath);
198 else
199 PathStripPathA(lpszPath);
202 /*************************************************************************
203 * PathStripToRoot [SHELL32.50]
205 BOOL WINAPI PathStripToRootAW(LPVOID lpszPath)
207 if (SHELL_OsIsUnicode())
208 return PathStripToRootW(lpszPath);
209 return PathStripToRootA(lpszPath);
212 /*************************************************************************
213 * PathRemoveArgs [SHELL32.251]
215 void WINAPI PathRemoveArgsAW(LPVOID lpszPath)
217 if (SHELL_OsIsUnicode())
218 PathRemoveArgsW(lpszPath);
219 else
220 PathRemoveArgsA(lpszPath);
223 /*************************************************************************
224 * PathRemoveExtension [SHELL32.250]
226 void WINAPI PathRemoveExtensionAW(LPVOID lpszPath)
228 if (SHELL_OsIsUnicode())
229 PathRemoveExtensionW(lpszPath);
230 else
231 PathRemoveExtensionA(lpszPath);
236 Path Manipulations
239 /*************************************************************************
240 * PathGetShortPathA [internal]
242 static void PathGetShortPathA(LPSTR pszPath)
244 CHAR path[MAX_PATH];
246 TRACE("%s\n", pszPath);
248 if (GetShortPathNameA(pszPath, path, MAX_PATH))
250 lstrcpyA(pszPath, path);
254 /*************************************************************************
255 * PathGetShortPathW [internal]
257 static void PathGetShortPathW(LPWSTR pszPath)
259 WCHAR path[MAX_PATH];
261 TRACE("%s\n", debugstr_w(pszPath));
263 if (GetShortPathNameW(pszPath, path, MAX_PATH))
265 lstrcpyW(pszPath, path);
269 /*************************************************************************
270 * PathGetShortPath [SHELL32.92]
272 VOID WINAPI PathGetShortPathAW(LPVOID pszPath)
274 if(SHELL_OsIsUnicode())
275 PathGetShortPathW(pszPath);
276 PathGetShortPathA(pszPath);
279 /*************************************************************************
280 * PathRemoveBlanks [SHELL32.33]
282 void WINAPI PathRemoveBlanksAW(LPVOID str)
284 if(SHELL_OsIsUnicode())
285 PathRemoveBlanksW(str);
286 else
287 PathRemoveBlanksA(str);
290 /*************************************************************************
291 * PathQuoteSpaces [SHELL32.55]
293 VOID WINAPI PathQuoteSpacesAW (LPVOID lpszPath)
295 if(SHELL_OsIsUnicode())
296 PathQuoteSpacesW(lpszPath);
297 else
298 PathQuoteSpacesA(lpszPath);
301 /*************************************************************************
302 * PathUnquoteSpaces [SHELL32.56]
304 VOID WINAPI PathUnquoteSpacesAW(LPVOID str)
306 if(SHELL_OsIsUnicode())
307 PathUnquoteSpacesW(str);
308 else
309 PathUnquoteSpacesA(str);
312 /*************************************************************************
313 * PathParseIconLocation [SHELL32.249]
315 int WINAPI PathParseIconLocationAW (LPVOID lpszPath)
317 if(SHELL_OsIsUnicode())
318 return PathParseIconLocationW(lpszPath);
319 return PathParseIconLocationA(lpszPath);
323 ########## Path Testing ##########
325 /*************************************************************************
326 * PathIsUNC [SHELL32.39]
328 BOOL WINAPI PathIsUNCAW (LPCVOID lpszPath)
330 if (SHELL_OsIsUnicode())
331 return PathIsUNCW( lpszPath );
332 return PathIsUNCA( lpszPath );
335 /*************************************************************************
336 * PathIsRelative [SHELL32.40]
338 BOOL WINAPI PathIsRelativeAW (LPCVOID lpszPath)
340 if (SHELL_OsIsUnicode())
341 return PathIsRelativeW( lpszPath );
342 return PathIsRelativeA( lpszPath );
345 /*************************************************************************
346 * PathIsRoot [SHELL32.29]
348 BOOL WINAPI PathIsRootAW(LPCVOID lpszPath)
350 if (SHELL_OsIsUnicode())
351 return PathIsRootW(lpszPath);
352 return PathIsRootA(lpszPath);
355 /*************************************************************************
356 * PathIsExeA [internal]
358 static BOOL PathIsExeA (LPCSTR lpszPath)
360 LPCSTR lpszExtension = PathGetExtensionA(lpszPath);
361 int i;
362 static const char * const lpszExtensions[] =
363 {"exe", "com", "pif", "cmd", "bat", "scf", "scr", NULL };
365 TRACE("path=%s\n",lpszPath);
367 for(i=0; lpszExtensions[i]; i++)
368 if (!lstrcmpiA(lpszExtension,lpszExtensions[i])) return TRUE;
370 return FALSE;
373 /*************************************************************************
374 * PathIsExeW [internal]
376 static BOOL PathIsExeW (LPCWSTR lpszPath)
378 LPCWSTR lpszExtension = PathGetExtensionW(lpszPath);
379 int i;
380 static const WCHAR lpszExtensions[][4] =
381 {{'e','x','e','\0'}, {'c','o','m','\0'}, {'p','i','f','\0'},
382 {'c','m','d','\0'}, {'b','a','t','\0'}, {'s','c','f','\0'},
383 {'s','c','r','\0'}, {'\0'} };
385 TRACE("path=%s\n",debugstr_w(lpszPath));
387 for(i=0; lpszExtensions[i][0]; i++)
388 if (!strcmpiW(lpszExtension,lpszExtensions[i])) return TRUE;
390 return FALSE;
393 /*************************************************************************
394 * PathIsExe [SHELL32.43]
396 BOOL WINAPI PathIsExeAW (LPCVOID path)
398 if (SHELL_OsIsUnicode())
399 return PathIsExeW (path);
400 return PathIsExeA(path);
403 /*************************************************************************
404 * PathIsDirectory [SHELL32.159]
406 BOOL WINAPI PathIsDirectoryAW (LPCVOID lpszPath)
408 if (SHELL_OsIsUnicode())
409 return PathIsDirectoryW (lpszPath);
410 return PathIsDirectoryA (lpszPath);
413 /*************************************************************************
414 * PathFileExists [SHELL32.45]
416 BOOL WINAPI PathFileExistsAW (LPCVOID lpszPath)
418 if (SHELL_OsIsUnicode())
419 return PathFileExistsW (lpszPath);
420 return PathFileExistsA (lpszPath);
423 /*************************************************************************
424 * PathMatchSpec [SHELL32.46]
426 BOOL WINAPI PathMatchSpecAW(LPVOID name, LPVOID mask)
428 if (SHELL_OsIsUnicode())
429 return PathMatchSpecW( name, mask );
430 return PathMatchSpecA( name, mask );
433 /*************************************************************************
434 * PathIsSameRoot [SHELL32.650]
436 BOOL WINAPI PathIsSameRootAW(LPCVOID lpszPath1, LPCVOID lpszPath2)
438 if (SHELL_OsIsUnicode())
439 return PathIsSameRootW(lpszPath1, lpszPath2);
440 return PathIsSameRootA(lpszPath1, lpszPath2);
443 /*************************************************************************
444 * IsLFNDriveA [SHELL32.41]
446 BOOL WINAPI IsLFNDriveA(LPCSTR lpszPath)
448 DWORD fnlen;
450 if (!GetVolumeInformationA(lpszPath, NULL, 0, NULL, &fnlen, NULL, NULL, 0))
451 return FALSE;
452 return fnlen > 12;
455 /*************************************************************************
456 * IsLFNDriveW [SHELL32.42]
458 BOOL WINAPI IsLFNDriveW(LPCWSTR lpszPath)
460 DWORD fnlen;
462 if (!GetVolumeInformationW(lpszPath, NULL, 0, NULL, &fnlen, NULL, NULL, 0))
463 return FALSE;
464 return fnlen > 12;
467 /*************************************************************************
468 * IsLFNDrive [SHELL32.119]
470 BOOL WINAPI IsLFNDriveAW(LPCVOID lpszPath)
472 if (SHELL_OsIsUnicode())
473 return IsLFNDriveW(lpszPath);
474 return IsLFNDriveA(lpszPath);
478 ########## Creating Something Unique ##########
480 /*************************************************************************
481 * PathMakeUniqueNameA [internal]
483 BOOL WINAPI PathMakeUniqueNameA(
484 LPSTR lpszBuffer,
485 DWORD dwBuffSize,
486 LPCSTR lpszShortName,
487 LPCSTR lpszLongName,
488 LPCSTR lpszPathName)
490 FIXME("%p %lu %s %s %s stub\n",
491 lpszBuffer, dwBuffSize, debugstr_a(lpszShortName),
492 debugstr_a(lpszLongName), debugstr_a(lpszPathName));
493 return TRUE;
496 /*************************************************************************
497 * PathMakeUniqueNameW [internal]
499 BOOL WINAPI PathMakeUniqueNameW(
500 LPWSTR lpszBuffer,
501 DWORD dwBuffSize,
502 LPCWSTR lpszShortName,
503 LPCWSTR lpszLongName,
504 LPCWSTR lpszPathName)
506 FIXME("%p %lu %s %s %s stub\n",
507 lpszBuffer, dwBuffSize, debugstr_w(lpszShortName),
508 debugstr_w(lpszLongName), debugstr_w(lpszPathName));
509 return TRUE;
512 /*************************************************************************
513 * PathMakeUniqueName [SHELL32.47]
515 BOOL WINAPI PathMakeUniqueNameAW(
516 LPVOID lpszBuffer,
517 DWORD dwBuffSize,
518 LPCVOID lpszShortName,
519 LPCVOID lpszLongName,
520 LPCVOID lpszPathName)
522 if (SHELL_OsIsUnicode())
523 return PathMakeUniqueNameW(lpszBuffer,dwBuffSize, lpszShortName,lpszLongName,lpszPathName);
524 return PathMakeUniqueNameA(lpszBuffer,dwBuffSize, lpszShortName,lpszLongName,lpszPathName);
527 /*************************************************************************
528 * PathYetAnotherMakeUniqueName [SHELL32.75]
530 * NOTES
531 * exported by ordinal
533 BOOL WINAPI PathYetAnotherMakeUniqueName(
534 LPWSTR lpszBuffer,
535 LPCWSTR lpszPathName,
536 LPCWSTR lpszShortName,
537 LPCWSTR lpszLongName)
539 FIXME("(%p, %s, %s ,%s):stub.\n",
540 lpszBuffer, debugstr_w(lpszPathName), debugstr_w(lpszShortName), debugstr_w(lpszLongName));
541 return TRUE;
546 ########## cleaning and resolving paths ##########
549 /*************************************************************************
550 * PathFindOnPath [SHELL32.145]
552 BOOL WINAPI PathFindOnPathAW(LPVOID sFile, LPCVOID sOtherDirs)
554 if (SHELL_OsIsUnicode())
555 return PathFindOnPathW(sFile, (LPCWSTR *)sOtherDirs);
556 return PathFindOnPathA(sFile, (LPCSTR *)sOtherDirs);
559 /*************************************************************************
560 * PathCleanupSpec [SHELL32.171]
562 * lpszFile is changed in place.
564 int WINAPI PathCleanupSpec( LPCWSTR lpszPathW, LPWSTR lpszFileW )
566 int i = 0;
567 DWORD rc = 0;
568 int length = 0;
570 if (SHELL_OsIsUnicode())
572 LPWSTR p = lpszFileW;
574 TRACE("Cleanup %s\n",debugstr_w(lpszFileW));
576 if (lpszPathW)
577 length = strlenW(lpszPathW);
579 while (*p)
581 int gct = PathGetCharTypeW(*p);
582 if (gct == GCT_INVALID || gct == GCT_WILD || gct == GCT_SEPARATOR)
584 lpszFileW[i]='-';
585 rc |= PCS_REPLACEDCHAR;
587 else
588 lpszFileW[i]=*p;
589 i++;
590 p++;
591 if (length + i == MAX_PATH)
593 rc |= PCS_FATAL | PCS_PATHTOOLONG;
594 break;
597 lpszFileW[i]=0;
599 else
601 LPSTR lpszFileA = (LPSTR)lpszFileW;
602 LPCSTR lpszPathA = (LPSTR)lpszPathW;
603 LPSTR p = lpszFileA;
605 TRACE("Cleanup %s\n",debugstr_a(lpszFileA));
607 if (lpszPathA)
608 length = strlen(lpszPathA);
610 while (*p)
612 int gct = PathGetCharTypeA(*p);
613 if (gct == GCT_INVALID || gct == GCT_WILD || gct == GCT_SEPARATOR)
615 lpszFileA[i]='-';
616 rc |= PCS_REPLACEDCHAR;
618 else
619 lpszFileA[i]=*p;
620 i++;
621 p++;
622 if (length + i == MAX_PATH)
624 rc |= PCS_FATAL | PCS_PATHTOOLONG;
625 break;
628 lpszFileA[i]=0;
630 return rc;
633 /*************************************************************************
634 * PathQualifyA [SHELL32]
636 BOOL WINAPI PathQualifyA(LPCSTR pszPath)
638 FIXME("%s\n",pszPath);
639 return 0;
642 /*************************************************************************
643 * PathQualifyW [SHELL32]
645 BOOL WINAPI PathQualifyW(LPCWSTR pszPath)
647 FIXME("%s\n",debugstr_w(pszPath));
648 return 0;
651 /*************************************************************************
652 * PathQualify [SHELL32.49]
654 BOOL WINAPI PathQualifyAW(LPCVOID pszPath)
656 if (SHELL_OsIsUnicode())
657 return PathQualifyW(pszPath);
658 return PathQualifyA(pszPath);
661 /*************************************************************************
662 * PathResolveA [SHELL32.51]
664 BOOL WINAPI PathResolveA(
665 LPSTR lpszPath,
666 LPCSTR *alpszPaths,
667 DWORD dwFlags)
669 FIXME("(%s,%p,0x%08lx),stub!\n",
670 lpszPath, *alpszPaths, dwFlags);
671 return 0;
674 /*************************************************************************
675 * PathResolveW [SHELL32]
677 BOOL WINAPI PathResolveW(
678 LPWSTR lpszPath,
679 LPCWSTR *alpszPaths,
680 DWORD dwFlags)
682 FIXME("(%s,%p,0x%08lx),stub!\n",
683 debugstr_w(lpszPath), debugstr_w(*alpszPaths), dwFlags);
684 return 0;
687 /*************************************************************************
688 * PathResolve [SHELL32.51]
690 BOOL WINAPI PathResolveAW(
691 LPVOID lpszPath,
692 LPCVOID *alpszPaths,
693 DWORD dwFlags)
695 if (SHELL_OsIsUnicode())
696 return PathResolveW(lpszPath, (LPCWSTR*)alpszPaths, dwFlags);
697 return PathResolveA(lpszPath, (LPCSTR*)alpszPaths, dwFlags);
700 /*************************************************************************
701 * PathProcessCommandA [SHELL32.653]
703 HRESULT WINAPI PathProcessCommandA (
704 LPCSTR lpszPath,
705 LPSTR lpszBuff,
706 DWORD dwBuffSize,
707 DWORD dwFlags)
709 FIXME("%s %p 0x%04lx 0x%04lx stub\n",
710 lpszPath, lpszBuff, dwBuffSize, dwFlags);
711 strcpy(lpszBuff, lpszPath);
712 return 0;
715 /*************************************************************************
716 * PathProcessCommandW
718 HRESULT WINAPI PathProcessCommandW (
719 LPCWSTR lpszPath,
720 LPWSTR lpszBuff,
721 DWORD dwBuffSize,
722 DWORD dwFlags)
724 FIXME("(%s, %p, 0x%04lx, 0x%04lx) stub\n",
725 debugstr_w(lpszPath), lpszBuff, dwBuffSize, dwFlags);
726 strcpyW(lpszBuff, lpszPath);
727 return 0;
730 /*************************************************************************
731 * PathProcessCommand (SHELL32.653)
733 HRESULT WINAPI PathProcessCommandAW (
734 LPCVOID lpszPath,
735 LPVOID lpszBuff,
736 DWORD dwBuffSize,
737 DWORD dwFlags)
739 if (SHELL_OsIsUnicode())
740 return PathProcessCommandW(lpszPath, lpszBuff, dwBuffSize, dwFlags);
741 return PathProcessCommandA(lpszPath, lpszBuff, dwBuffSize, dwFlags);
745 ########## special ##########
748 /*************************************************************************
749 * PathSetDlgItemPath (SHELL32.48)
751 VOID WINAPI PathSetDlgItemPathAW(HWND hDlg, int id, LPCVOID pszPath)
753 if (SHELL_OsIsUnicode())
754 PathSetDlgItemPathW(hDlg, id, pszPath);
755 else
756 PathSetDlgItemPathA(hDlg, id, pszPath);
759 static const WCHAR szCurrentVersion[] = {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\0'};
760 static const WCHAR Administrative_ToolsW[] = {'A','d','m','i','n','i','s','t','r','a','t','i','v','e',' ','T','o','o','l','s','\0'};
761 static const WCHAR AppDataW[] = {'A','p','p','D','a','t','a','\0'};
762 static const WCHAR CacheW[] = {'C','a','c','h','e','\0'};
763 static const WCHAR CD_BurningW[] = {'C','D',' ','B','u','r','n','i','n','g','\0'};
764 static const WCHAR Common_Administrative_ToolsW[] = {'C','o','m','m','o','n',' ','A','d','m','i','n','i','s','t','r','a','t','i','v','e',' ','T','o','o','l','s','\0'};
765 static const WCHAR Common_AppDataW[] = {'C','o','m','m','o','n',' ','A','p','p','D','a','t','a','\0'};
766 static const WCHAR Common_DesktopW[] = {'C','o','m','m','o','n',' ','D','e','s','k','t','o','p','\0'};
767 static const WCHAR Common_DocumentsW[] = {'C','o','m','m','o','n',' ','D','o','c','u','m','e','n','t','s','\0'};
768 static const WCHAR 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','\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 /* This defaults to L"Documents and Settings" on Windows 2000/XP, but we're
806 * acting more Windows 9x-like for now.
808 static const WCHAR szDefaultProfileDirW[] = {'p','r','o','f','i','l','e','s','\0'};
809 static const WCHAR AllUsersW[] = {'A','l','l',' ','U','s','e','r','s','\0'};
811 typedef enum _CSIDL_Type {
812 CSIDL_Type_User,
813 CSIDL_Type_AllUsers,
814 CSIDL_Type_CurrVer,
815 CSIDL_Type_Disallowed,
816 CSIDL_Type_NonExistent,
817 CSIDL_Type_WindowsPath,
818 CSIDL_Type_SystemPath,
819 } CSIDL_Type;
821 typedef struct
823 CSIDL_Type type;
824 LPCWSTR szValueName;
825 LPCWSTR szDefaultPath; /* fallback string or resource ID */
826 } CSIDL_DATA;
828 static const CSIDL_DATA CSIDL_Data[] =
830 { /* 0x00 - CSIDL_DESKTOP */
831 CSIDL_Type_User,
832 DesktopW,
833 MAKEINTRESOURCEW(IDS_DESKTOPDIRECTORY)
835 { /* 0x01 - CSIDL_INTERNET */
836 CSIDL_Type_Disallowed,
837 NULL,
838 NULL
840 { /* 0x02 - CSIDL_PROGRAMS */
841 CSIDL_Type_User,
842 ProgramsW,
843 MAKEINTRESOURCEW(IDS_PROGRAMS)
845 { /* 0x03 - CSIDL_CONTROLS (.CPL files) */
846 CSIDL_Type_SystemPath,
847 NULL,
848 NULL
850 { /* 0x04 - CSIDL_PRINTERS */
851 CSIDL_Type_SystemPath,
852 NULL,
853 NULL
855 { /* 0x05 - CSIDL_PERSONAL */
856 CSIDL_Type_User,
857 PersonalW,
858 MAKEINTRESOURCEW(IDS_PERSONAL)
860 { /* 0x06 - CSIDL_FAVORITES */
861 CSIDL_Type_User,
862 FavoritesW,
863 MAKEINTRESOURCEW(IDS_FAVORITES)
865 { /* 0x07 - CSIDL_STARTUP */
866 CSIDL_Type_User,
867 StartUpW,
868 MAKEINTRESOURCEW(IDS_STARTUP)
870 { /* 0x08 - CSIDL_RECENT */
871 CSIDL_Type_User,
872 RecentW,
873 MAKEINTRESOURCEW(IDS_RECENT)
875 { /* 0x09 - CSIDL_SENDTO */
876 CSIDL_Type_User,
877 SendToW,
878 MAKEINTRESOURCEW(IDS_SENDTO)
880 { /* 0x0a - CSIDL_BITBUCKET - Recycle Bin */
881 CSIDL_Type_Disallowed,
882 NULL,
883 NULL,
885 { /* 0x0b - CSIDL_STARTMENU */
886 CSIDL_Type_User,
887 Start_MenuW,
888 MAKEINTRESOURCEW(IDS_STARTMENU)
890 { /* 0x0c - CSIDL_MYDOCUMENTS */
891 CSIDL_Type_Disallowed, /* matches WinXP--can't get its path */
892 NULL,
893 NULL
895 { /* 0x0d - CSIDL_MYMUSIC */
896 CSIDL_Type_User,
897 My_MusicW,
898 MAKEINTRESOURCEW(IDS_MYMUSIC)
900 { /* 0x0e - CSIDL_MYVIDEO */
901 CSIDL_Type_User,
902 My_VideoW,
903 MAKEINTRESOURCEW(IDS_MYVIDEO)
905 { /* 0x0f - unassigned */
906 CSIDL_Type_Disallowed,
907 NULL,
908 NULL,
910 { /* 0x10 - CSIDL_DESKTOPDIRECTORY */
911 CSIDL_Type_User,
912 DesktopW,
913 MAKEINTRESOURCEW(IDS_DESKTOPDIRECTORY)
915 { /* 0x11 - CSIDL_DRIVES */
916 CSIDL_Type_Disallowed,
917 NULL,
918 NULL,
920 { /* 0x12 - CSIDL_NETWORK */
921 CSIDL_Type_Disallowed,
922 NULL,
923 NULL,
925 { /* 0x13 - CSIDL_NETHOOD */
926 CSIDL_Type_User,
927 NetHoodW,
928 MAKEINTRESOURCEW(IDS_NETHOOD)
930 { /* 0x14 - CSIDL_FONTS */
931 CSIDL_Type_WindowsPath,
932 NULL,
933 FontsW
935 { /* 0x15 - CSIDL_TEMPLATES */
936 CSIDL_Type_User,
937 TemplatesW,
938 MAKEINTRESOURCEW(IDS_TEMPLATES)
940 { /* 0x16 - CSIDL_COMMON_STARTMENU */
941 CSIDL_Type_AllUsers,
942 Common_Start_MenuW,
943 MAKEINTRESOURCEW(IDS_STARTMENU)
945 { /* 0x17 - CSIDL_COMMON_PROGRAMS */
946 CSIDL_Type_AllUsers,
947 Common_ProgramsW,
948 MAKEINTRESOURCEW(IDS_PROGRAMS)
950 { /* 0x18 - CSIDL_COMMON_STARTUP */
951 CSIDL_Type_AllUsers,
952 Common_StartUpW,
953 MAKEINTRESOURCEW(IDS_STARTUP)
955 { /* 0x19 - CSIDL_COMMON_DESKTOPDIRECTORY */
956 CSIDL_Type_AllUsers,
957 Common_DesktopW,
958 MAKEINTRESOURCEW(IDS_DESKTOP)
960 { /* 0x1a - CSIDL_APPDATA */
961 CSIDL_Type_User,
962 AppDataW,
963 MAKEINTRESOURCEW(IDS_APPDATA)
965 { /* 0x1b - CSIDL_PRINTHOOD */
966 CSIDL_Type_User,
967 PrintHoodW,
968 MAKEINTRESOURCEW(IDS_PRINTHOOD)
970 { /* 0x1c - CSIDL_LOCAL_APPDATA */
971 CSIDL_Type_User,
972 Local_AppDataW,
973 MAKEINTRESOURCEW(IDS_LOCAL_APPDATA)
975 { /* 0x1d - CSIDL_ALTSTARTUP */
976 CSIDL_Type_NonExistent,
977 NULL,
978 NULL
980 { /* 0x1e - CSIDL_COMMON_ALTSTARTUP */
981 CSIDL_Type_NonExistent,
982 NULL,
983 NULL
985 { /* 0x1f - CSIDL_COMMON_FAVORITES */
986 CSIDL_Type_AllUsers,
987 FavoritesW,
988 MAKEINTRESOURCEW(IDS_FAVORITES)
990 { /* 0x20 - CSIDL_INTERNET_CACHE */
991 CSIDL_Type_User,
992 CacheW,
993 MAKEINTRESOURCEW(IDS_INTERNET_CACHE)
995 { /* 0x21 - CSIDL_COOKIES */
996 CSIDL_Type_User,
997 CookiesW,
998 MAKEINTRESOURCEW(IDS_COOKIES)
1000 { /* 0x22 - CSIDL_HISTORY */
1001 CSIDL_Type_User,
1002 HistoryW,
1003 MAKEINTRESOURCEW(IDS_HISTORY)
1005 { /* 0x23 - CSIDL_COMMON_APPDATA */
1006 CSIDL_Type_AllUsers,
1007 Common_AppDataW,
1008 MAKEINTRESOURCEW(IDS_APPDATA)
1010 { /* 0x24 - CSIDL_WINDOWS */
1011 CSIDL_Type_WindowsPath,
1012 NULL,
1013 NULL
1015 { /* 0x25 - CSIDL_SYSTEM */
1016 CSIDL_Type_SystemPath,
1017 NULL,
1018 NULL
1020 { /* 0x26 - CSIDL_PROGRAM_FILES */
1021 CSIDL_Type_CurrVer,
1022 ProgramFilesDirW,
1023 MAKEINTRESOURCEW(IDS_PROGRAM_FILES)
1025 { /* 0x27 - CSIDL_MYPICTURES */
1026 CSIDL_Type_User,
1027 My_PicturesW,
1028 MAKEINTRESOURCEW(IDS_MYPICTURES)
1030 { /* 0x28 - CSIDL_PROFILE */
1031 CSIDL_Type_User,
1032 NULL,
1033 NULL
1035 { /* 0x29 - CSIDL_SYSTEMX86 */
1036 CSIDL_Type_NonExistent,
1037 NULL,
1038 NULL
1040 { /* 0x2a - CSIDL_PROGRAM_FILESX86 */
1041 CSIDL_Type_NonExistent,
1042 NULL,
1043 NULL
1045 { /* 0x2b - CSIDL_PROGRAM_FILES_COMMON */
1046 CSIDL_Type_CurrVer,
1047 CommonFilesDirW,
1048 MAKEINTRESOURCEW(IDS_PROGRAM_FILES_COMMON)
1050 { /* 0x2c - CSIDL_PROGRAM_FILES_COMMONX86 */
1051 CSIDL_Type_NonExistent,
1052 NULL,
1053 NULL
1055 { /* 0x2d - CSIDL_COMMON_TEMPLATES */
1056 CSIDL_Type_AllUsers,
1057 Common_TemplatesW,
1058 MAKEINTRESOURCEW(IDS_TEMPLATES)
1060 { /* 0x2e - CSIDL_COMMON_DOCUMENTS */
1061 CSIDL_Type_AllUsers,
1062 Common_DocumentsW,
1063 MAKEINTRESOURCEW(IDS_COMMON_DOCUMENTS)
1065 { /* 0x2f - CSIDL_COMMON_ADMINTOOLS */
1066 CSIDL_Type_AllUsers,
1067 Common_Administrative_ToolsW,
1068 MAKEINTRESOURCEW(IDS_ADMINTOOLS)
1070 { /* 0x30 - CSIDL_ADMINTOOLS */
1071 CSIDL_Type_User,
1072 Administrative_ToolsW,
1073 MAKEINTRESOURCEW(IDS_ADMINTOOLS)
1075 { /* 0x31 - CSIDL_CONNECTIONS */
1076 CSIDL_Type_Disallowed,
1077 NULL,
1078 NULL
1080 { /* 0x32 - unassigned */
1081 CSIDL_Type_Disallowed,
1082 NULL,
1083 NULL
1085 { /* 0x33 - unassigned */
1086 CSIDL_Type_Disallowed,
1087 NULL,
1088 NULL
1090 { /* 0x34 - unassigned */
1091 CSIDL_Type_Disallowed,
1092 NULL,
1093 NULL
1095 { /* 0x35 - CSIDL_COMMON_MUSIC */
1096 CSIDL_Type_AllUsers,
1097 CommonMusicW,
1098 MAKEINTRESOURCEW(IDS_COMMON_MUSIC)
1100 { /* 0x36 - CSIDL_COMMON_PICTURES */
1101 CSIDL_Type_AllUsers,
1102 CommonPicturesW,
1103 MAKEINTRESOURCEW(IDS_COMMON_PICTURES)
1105 { /* 0x37 - CSIDL_COMMON_VIDEO */
1106 CSIDL_Type_AllUsers,
1107 CommonVideoW,
1108 MAKEINTRESOURCEW(IDS_COMMON_VIDEO)
1110 { /* 0x38 - CSIDL_RESOURCES */
1111 CSIDL_Type_WindowsPath,
1112 NULL,
1113 ResourcesW
1115 { /* 0x39 - CSIDL_RESOURCES_LOCALIZED */
1116 CSIDL_Type_NonExistent,
1117 NULL,
1118 NULL
1120 { /* 0x3a - CSIDL_COMMON_OEM_LINKS */
1121 CSIDL_Type_NonExistent,
1122 NULL,
1123 NULL
1125 { /* 0x3b - CSIDL_CDBURN_AREA */
1126 CSIDL_Type_User,
1127 CD_BurningW,
1128 MAKEINTRESOURCEW(IDS_CDBURN_AREA)
1130 { /* 0x3c unassigned */
1131 CSIDL_Type_Disallowed,
1132 NULL,
1133 NULL
1135 { /* 0x3d - CSIDL_COMPUTERSNEARME */
1136 CSIDL_Type_Disallowed, /* FIXME */
1137 NULL,
1138 NULL
1140 { /* 0x3e - CSIDL_PROFILES */
1141 CSIDL_Type_Disallowed, /* oddly, this matches WinXP */
1142 NULL,
1143 NULL
1147 static HRESULT _SHExpandEnvironmentStrings(LPCWSTR szSrc, LPWSTR szDest);
1149 /* Gets the value named value from the registry key
1150 * rootKey\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
1151 * (or from rootKey\userPrefix\... if userPrefix is not NULL) into path, which
1152 * is assumed to be MAX_PATH WCHARs in length.
1153 * If it exists, expands the value and writes the expanded value to
1154 * rootKey\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders
1155 * Returns successful error code if the value was retrieved from the registry,
1156 * and a failure otherwise.
1158 static HRESULT _SHGetUserShellFolderPath(HKEY rootKey, LPCWSTR userPrefix,
1159 LPCWSTR value, LPWSTR path)
1161 HRESULT hr;
1162 WCHAR shellFolderPath[MAX_PATH], userShellFolderPath[MAX_PATH];
1163 LPCWSTR pShellFolderPath, pUserShellFolderPath;
1164 DWORD dwDisp, dwType, dwPathLen = MAX_PATH;
1165 HKEY userShellFolderKey, shellFolderKey;
1167 TRACE("%p,%s,%s,%p\n",rootKey, debugstr_w(userPrefix), debugstr_w(value),
1168 path);
1170 if (userPrefix)
1172 strcpyW(shellFolderPath, userPrefix);
1173 PathAddBackslashW(shellFolderPath);
1174 strcatW(shellFolderPath, szSHFolders);
1175 pShellFolderPath = shellFolderPath;
1176 strcpyW(userShellFolderPath, userPrefix);
1177 PathAddBackslashW(userShellFolderPath);
1178 strcatW(userShellFolderPath, szSHUserFolders);
1179 pUserShellFolderPath = userShellFolderPath;
1181 else
1183 pUserShellFolderPath = szSHUserFolders;
1184 pShellFolderPath = szSHFolders;
1187 if (RegCreateKeyExW(rootKey, pShellFolderPath, 0, NULL, 0, KEY_ALL_ACCESS,
1188 NULL, &shellFolderKey, &dwDisp))
1190 TRACE("Failed to create %s\n", debugstr_w(pShellFolderPath));
1191 return E_FAIL;
1193 if (RegCreateKeyExW(rootKey, pUserShellFolderPath, 0, NULL, 0,
1194 KEY_ALL_ACCESS, NULL, &userShellFolderKey, &dwDisp))
1196 TRACE("Failed to create %s\n",
1197 debugstr_w(pUserShellFolderPath));
1198 RegCloseKey(shellFolderKey);
1199 return E_FAIL;
1202 if (!RegQueryValueExW(userShellFolderKey, value, NULL, &dwType,
1203 (LPBYTE)path, &dwPathLen) && (dwType == REG_EXPAND_SZ || dwType == REG_SZ))
1205 LONG ret;
1207 path[dwPathLen / sizeof(WCHAR)] = '\0';
1208 if (dwType == REG_EXPAND_SZ && path[0] == '%')
1210 WCHAR szTemp[MAX_PATH];
1212 _SHExpandEnvironmentStrings(path, szTemp);
1213 lstrcpynW(path, szTemp, MAX_PATH);
1215 ret = RegSetValueExW(shellFolderKey, value, 0, REG_SZ, (LPBYTE)path,
1216 (strlenW(path) + 1) * sizeof(WCHAR));
1217 if (ret != ERROR_SUCCESS)
1218 hr = HRESULT_FROM_WIN32(ret);
1219 else
1220 hr = S_OK;
1222 else
1223 hr = E_FAIL;
1224 RegCloseKey(shellFolderKey);
1225 RegCloseKey(userShellFolderKey);
1226 TRACE("returning 0x%08lx\n", hr);
1227 return hr;
1230 /* Gets a 'semi-expanded' default value of the CSIDL with index folder into
1231 * pszPath, based on the entries in CSIDL_Data. By semi-expanded, I mean:
1232 * - The entry's szDefaultPath may be either a string value or an integer
1233 * resource identifier. In the latter case, the string value of the resource
1234 * is written.
1235 * - Depending on the entry's type, the path may begin with an (unexpanded)
1236 * environment variable name. The caller is responsible for expanding
1237 * environment strings if so desired.
1238 * The types that are prepended with environment variables are:
1239 * CSIDL_Type_User: %USERPROFILE%
1240 * CSIDL_Type_AllUsers: %ALLUSERSPROFILE%
1241 * CSIDL_Type_CurrVer: %SystemDrive%
1242 * (Others might make sense too, but as yet are unneeded.)
1244 static HRESULT _SHGetDefaultValue(BYTE folder, LPWSTR pszPath)
1246 HRESULT hr;
1247 WCHAR resourcePath[MAX_PATH];
1248 LPCWSTR pDefaultPath = NULL;
1250 TRACE("0x%02x,%p\n", folder, pszPath);
1252 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1253 return E_INVALIDARG;
1254 if (!pszPath)
1255 return E_INVALIDARG;
1257 /* Try special cases first */
1258 hr = E_FAIL;
1259 switch (folder)
1261 case CSIDL_PERSONAL:
1262 case CSIDL_MYMUSIC:
1263 case CSIDL_MYPICTURES:
1264 case CSIDL_MYVIDEO:
1266 const char *home = getenv("HOME");
1268 /* special case for "My Documents", map to $HOME */
1269 if (home)
1271 LPWSTR homeW = wine_get_dos_file_name(home);
1273 if (homeW)
1275 if (PathIsDirectoryW(homeW))
1277 lstrcpynW(pszPath, homeW, MAX_PATH);
1278 hr = S_OK;
1280 HeapFree(GetProcessHeap(), 0, homeW);
1282 else
1283 hr = HRESULT_FROM_WIN32(GetLastError());
1285 break;
1287 case CSIDL_DESKTOP:
1288 case CSIDL_DESKTOPDIRECTORY:
1290 const char *home = getenv("HOME");
1292 /* special case for Desktop, map to $HOME/Desktop if it exists */
1293 if (home)
1295 LPWSTR homeW = wine_get_dos_file_name(home);
1297 if (homeW)
1299 lstrcpynW(pszPath, homeW, MAX_PATH);
1300 if (PathAppendW(pszPath, DesktopW))
1302 if (PathIsDirectoryW(pszPath))
1303 hr = S_OK;
1305 else
1306 hr = HRESULT_FROM_WIN32(GetLastError());
1307 HeapFree(GetProcessHeap(), 0, homeW);
1309 else
1310 hr = HRESULT_FROM_WIN32(GetLastError());
1312 break;
1315 if (SUCCEEDED(hr))
1316 return hr;
1318 /* Either the folder was unhandled, or a suitable default wasn't found,
1319 * so use one of the resource-based defaults
1321 if (CSIDL_Data[folder].szDefaultPath &&
1322 IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath))
1324 if (LoadStringW(shell32_hInstance,
1325 LOWORD(CSIDL_Data[folder].szDefaultPath), resourcePath, MAX_PATH))
1327 hr = S_OK;
1328 pDefaultPath = resourcePath;
1330 else
1332 FIXME("(%d,%s), LoadString failed, missing translation?\n", folder,
1333 debugstr_w(pszPath));
1334 hr = E_FAIL;
1337 else
1339 hr = S_OK;
1340 pDefaultPath = CSIDL_Data[folder].szDefaultPath;
1342 if (SUCCEEDED(hr))
1344 switch (CSIDL_Data[folder].type)
1346 case CSIDL_Type_User:
1347 strcpyW(pszPath, UserProfileW);
1348 break;
1349 case CSIDL_Type_AllUsers:
1350 strcpyW(pszPath, AllUsersProfileW);
1351 break;
1352 case CSIDL_Type_CurrVer:
1353 strcpyW(pszPath, SystemDriveW);
1354 break;
1355 default:
1356 ; /* no corresponding env. var, do nothing */
1358 if (pDefaultPath)
1360 PathAddBackslashW(pszPath);
1361 strcatW(pszPath, pDefaultPath);
1364 TRACE("returning 0x%08lx\n", hr);
1365 return hr;
1368 /* Gets the (unexpanded) value of the folder with index folder into pszPath.
1369 * The folder's type is assumed to be CSIDL_Type_CurrVer. Its default value
1370 * can be overridden in the HKLM\\szCurrentVersion key.
1371 * If dwFlags has SHGFP_TYPE_DEFAULT set or if the value isn't overridden in
1372 * the registry, uses _SHGetDefaultValue to get the value.
1374 static HRESULT _SHGetCurrentVersionPath(DWORD dwFlags, BYTE folder,
1375 LPWSTR pszPath)
1377 HRESULT hr;
1379 TRACE("0x%08lx,0x%02x,%p\n", dwFlags, folder, pszPath);
1381 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1382 return E_INVALIDARG;
1383 if (CSIDL_Data[folder].type != CSIDL_Type_CurrVer)
1384 return E_INVALIDARG;
1385 if (!pszPath)
1386 return E_INVALIDARG;
1388 if (dwFlags & SHGFP_TYPE_DEFAULT)
1389 hr = _SHGetDefaultValue(folder, pszPath);
1390 else
1392 HKEY hKey;
1393 DWORD dwDisp;
1395 if (RegCreateKeyExW(HKEY_LOCAL_MACHINE, szCurrentVersion, 0,
1396 NULL, 0, KEY_ALL_ACCESS, NULL, &hKey, &dwDisp))
1397 hr = E_FAIL;
1398 else
1400 DWORD dwType, dwPathLen = MAX_PATH * sizeof(WCHAR);
1402 if (RegQueryValueExW(hKey, CSIDL_Data[folder].szValueName, NULL,
1403 &dwType, (LPBYTE)pszPath, &dwPathLen) ||
1404 (dwType != REG_SZ && dwType != REG_EXPAND_SZ))
1406 hr = _SHGetDefaultValue(folder, pszPath);
1407 dwType = REG_EXPAND_SZ;
1408 RegSetValueExW(hKey, CSIDL_Data[folder].szValueName, 0, dwType,
1409 (LPBYTE)pszPath, (strlenW(pszPath)+1)*sizeof(WCHAR));
1411 else
1413 pszPath[dwPathLen / sizeof(WCHAR)] = '\0';
1414 hr = S_OK;
1416 RegCloseKey(hKey);
1419 TRACE("returning 0x%08lx (output path is %s)\n", hr, debugstr_w(pszPath));
1420 return hr;
1423 /* Gets the user's path (unexpanded) for the CSIDL with index folder:
1424 * If SHGFP_TYPE_DEFAULT is set, calls _SHGetDefaultValue for it. Otherwise
1425 * calls _SHGetUserShellFolderPath for it. Where it looks depends on hToken:
1426 * - if hToken is -1, looks in HKEY_USERS\.Default
1427 * - otherwise looks first in HKEY_CURRENT_USER, followed by HKEY_LOCAL_MACHINE
1428 * if HKEY_CURRENT_USER doesn't contain any entries. If both fail, finally
1429 * calls _SHGetDefaultValue for it.
1431 static HRESULT _SHGetUserProfilePath(HANDLE hToken, DWORD dwFlags, BYTE folder,
1432 LPWSTR pszPath)
1434 HRESULT hr;
1436 TRACE("%p,0x%08lx,0x%02x,%p\n", hToken, dwFlags, folder, pszPath);
1438 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1439 return E_INVALIDARG;
1440 if (CSIDL_Data[folder].type != CSIDL_Type_User)
1441 return E_INVALIDARG;
1442 if (!pszPath)
1443 return E_INVALIDARG;
1445 /* Only the current user and the default user are supported right now
1446 * I'm afraid.
1447 * FIXME: should be able to call GetTokenInformation on the token,
1448 * then call ConvertSidToStringSidW on it to get the user prefix.
1449 * But Wine's registry doesn't store user info by sid, it stores it
1450 * by user name (and I don't know how to convert from a token to a
1451 * user name).
1453 if (hToken != NULL && hToken != (HANDLE)-1)
1455 FIXME("unsupported for user other than current or default\n");
1456 return E_FAIL;
1459 if (dwFlags & SHGFP_TYPE_DEFAULT)
1460 hr = _SHGetDefaultValue(folder, pszPath);
1461 else
1463 LPCWSTR userPrefix = NULL;
1464 HKEY hRootKey;
1466 if (hToken == (HANDLE)-1)
1468 hRootKey = HKEY_USERS;
1469 userPrefix = DefaultW;
1471 else /* hToken == NULL, other values disallowed above */
1472 hRootKey = HKEY_CURRENT_USER;
1473 hr = _SHGetUserShellFolderPath(hRootKey, userPrefix,
1474 CSIDL_Data[folder].szValueName, pszPath);
1475 if (FAILED(hr) && hRootKey != HKEY_LOCAL_MACHINE)
1476 hr = _SHGetUserShellFolderPath(HKEY_LOCAL_MACHINE, NULL,
1477 CSIDL_Data[folder].szValueName, pszPath);
1478 if (FAILED(hr))
1479 hr = _SHGetDefaultValue(folder, pszPath);
1481 TRACE("returning 0x%08lx (output path is %s)\n", hr, debugstr_w(pszPath));
1482 return hr;
1485 /* Gets the (unexpanded) path for the CSIDL with index folder. If dwFlags has
1486 * SHGFP_TYPE_DEFAULT set, calls _SHGetDefaultValue. Otherwise calls
1487 * _SHGetUserShellFolderPath for it, looking only in HKEY_LOCAL_MACHINE.
1488 * If this fails, falls back to _SHGetDefaultValue.
1490 static HRESULT _SHGetAllUsersProfilePath(DWORD dwFlags, BYTE folder,
1491 LPWSTR pszPath)
1493 HRESULT hr;
1495 TRACE("0x%08lx,0x%02x,%p\n", dwFlags, folder, pszPath);
1497 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1498 return E_INVALIDARG;
1499 if (CSIDL_Data[folder].type != CSIDL_Type_AllUsers)
1500 return E_INVALIDARG;
1501 if (!pszPath)
1502 return E_INVALIDARG;
1504 if (dwFlags & SHGFP_TYPE_DEFAULT)
1505 hr = _SHGetDefaultValue(folder, pszPath);
1506 else
1508 hr = _SHGetUserShellFolderPath(HKEY_LOCAL_MACHINE, NULL,
1509 CSIDL_Data[folder].szValueName, pszPath);
1510 if (FAILED(hr))
1511 hr = _SHGetDefaultValue(folder, pszPath);
1513 TRACE("returning 0x%08lx (output path is %s)\n", hr, debugstr_w(pszPath));
1514 return hr;
1517 static HRESULT _SHOpenProfilesKey(PHKEY pKey)
1519 LONG lRet;
1520 DWORD disp;
1522 lRet = RegCreateKeyExW(HKEY_LOCAL_MACHINE, ProfileListW, 0, NULL, 0,
1523 KEY_ALL_ACCESS, NULL, pKey, &disp);
1524 return HRESULT_FROM_WIN32(lRet);
1527 /* Reads the value named szValueName from the key profilesKey (assumed to be
1528 * opened by _SHOpenProfilesKey) into szValue, which is assumed to be MAX_PATH
1529 * WCHARs in length. If it doesn't exist, returns szDefault (and saves
1530 * szDefault to the registry).
1532 static HRESULT _SHGetProfilesValue(HKEY profilesKey, LPCWSTR szValueName,
1533 LPWSTR szValue, LPCWSTR szDefault)
1535 HRESULT hr;
1536 DWORD type, dwPathLen = MAX_PATH * sizeof(WCHAR);
1537 LONG lRet;
1539 TRACE("%p,%s,%p,%s\n", profilesKey, debugstr_w(szValueName), szValue,
1540 debugstr_w(szDefault));
1541 lRet = RegQueryValueExW(profilesKey, szValueName, NULL, &type,
1542 (LPBYTE)szValue, &dwPathLen);
1543 if (!lRet && (type == REG_SZ || type == REG_EXPAND_SZ) && dwPathLen
1544 && *szValue)
1546 dwPathLen /= sizeof(WCHAR);
1547 szValue[dwPathLen] = '\0';
1548 hr = S_OK;
1550 else
1552 /* Missing or invalid value, set a default */
1553 lstrcpynW(szValue, szDefault, MAX_PATH);
1554 TRACE("Setting missing value %s to %s\n", debugstr_w(szValueName),
1555 debugstr_w(szValue));
1556 lRet = RegSetValueExW(profilesKey, szValueName, 0, REG_EXPAND_SZ,
1557 (LPBYTE)szValue,
1558 (strlenW(szValue) + 1) * sizeof(WCHAR));
1559 if (lRet)
1560 hr = HRESULT_FROM_WIN32(lRet);
1561 else
1562 hr = S_OK;
1564 TRACE("returning 0x%08lx (output value is %s)\n", hr, debugstr_w(szValue));
1565 return hr;
1568 /* Attempts to expand environment variables from szSrc into szDest, which is
1569 * assumed to be MAX_PATH characters in length. Before referring to the
1570 * environment, handles a few variables directly, because the environment
1571 * variables may not be set when this is called (as during Wine's installation
1572 * when default values are being written to the registry).
1573 * The directly handled environment variables, and their source, are:
1574 * - ALLUSERSPROFILE, USERPROFILE: reads from the registry
1575 * - SystemDrive: uses GetSystemDirectoryW and uses the drive portion of its
1576 * path
1577 * If one of the directly handled environment variables is expanded, only
1578 * expands a single variable, and only in the beginning of szSrc.
1580 static HRESULT _SHExpandEnvironmentStrings(LPCWSTR szSrc, LPWSTR szDest)
1582 HRESULT hr;
1583 WCHAR szTemp[MAX_PATH], szProfilesPrefix[MAX_PATH] = { 0 };
1584 HKEY key = NULL;
1586 TRACE("%s, %p\n", debugstr_w(szSrc), szDest);
1588 if (!szSrc || !szDest) return E_INVALIDARG;
1590 /* short-circuit if there's nothing to expand */
1591 if (szSrc[0] != '%')
1593 strcpyW(szDest, szSrc);
1594 hr = S_OK;
1595 goto end;
1597 /* Get the profile prefix, we'll probably be needing it */
1598 hr = _SHOpenProfilesKey(&key);
1599 if (SUCCEEDED(hr))
1601 WCHAR szDefaultProfilesPrefix[MAX_PATH];
1603 GetWindowsDirectoryW(szDefaultProfilesPrefix, MAX_PATH);
1604 PathAddBackslashW(szDefaultProfilesPrefix);
1605 PathAppendW(szDefaultProfilesPrefix, szDefaultProfileDirW);
1606 hr = _SHGetProfilesValue(key, ProfilesDirectoryW, szProfilesPrefix,
1607 szDefaultProfilesPrefix);
1610 *szDest = 0;
1611 strcpyW(szTemp, szSrc);
1612 while (SUCCEEDED(hr) && szTemp[0] == '%')
1614 if (!strncmpiW(szTemp, AllUsersProfileW, strlenW(AllUsersProfileW)))
1616 WCHAR szAllUsers[MAX_PATH];
1618 strcpyW(szDest, szProfilesPrefix);
1619 hr = _SHGetProfilesValue(key, AllUsersProfileValueW,
1620 szAllUsers, AllUsersW);
1621 PathAppendW(szDest, szAllUsers);
1622 PathAppendW(szDest, szTemp + strlenW(AllUsersProfileW));
1624 else if (!strncmpiW(szTemp, UserProfileW, strlenW(UserProfileW)))
1626 WCHAR userName[MAX_PATH];
1627 DWORD userLen = MAX_PATH;
1629 strcpyW(szDest, szProfilesPrefix);
1630 GetUserNameW(userName, &userLen);
1631 PathAppendW(szDest, userName);
1632 PathAppendW(szDest, szTemp + strlenW(UserProfileW));
1634 else if (!strncmpiW(szTemp, SystemDriveW, strlenW(SystemDriveW)))
1636 GetSystemDirectoryW(szDest, MAX_PATH);
1637 if (szDest[1] != ':')
1639 FIXME("non-drive system paths unsupported\n");
1640 hr = E_FAIL;
1642 else
1644 strcpyW(szDest + 3, szTemp + strlenW(SystemDriveW) + 1);
1645 hr = S_OK;
1648 else
1650 DWORD ret = ExpandEnvironmentStringsW(szSrc, szDest, MAX_PATH);
1652 if (ret > MAX_PATH)
1653 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
1654 else if (ret == 0)
1655 hr = HRESULT_FROM_WIN32(GetLastError());
1656 else
1657 hr = S_OK;
1659 if (SUCCEEDED(hr) && szDest[0] == '%')
1660 strcpyW(szTemp, szDest);
1661 else
1663 /* terminate loop */
1664 szTemp[0] = '\0';
1667 end:
1668 if (key)
1669 RegCloseKey(key);
1670 TRACE("returning 0x%08lx (input was %s, output is %s)\n", hr,
1671 debugstr_w(szSrc), debugstr_w(szDest));
1672 return hr;
1675 /*************************************************************************
1676 * SHGetFolderPathW [SHELL32.@]
1678 * Convert nFolder to path.
1680 * RETURNS
1681 * Success: S_OK
1682 * Failure: standard HRESULT error codes.
1684 * NOTES
1685 * Most values can be overridden in either
1686 * HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
1687 * or in the same location in HKLM.
1688 * The registry usage is explained by the following tech note:
1689 * http://www.microsoft.com/windows2000/techinfo/reskit/en-us/default.asp?url=/windows2000/techinfo/reskit/en-us/regentry/36173.asp
1690 * The "Shell Folders" registry key was used in NT4 and earlier systems.
1691 * Beginning with Windows 2000, the "User Shell Folders" key is used, so
1692 * changes made to it are made to the former key too. This synchronization is
1693 * done on-demand: not until someone requests the value of one of these paths
1694 * (by calling one of the SHGet functions) is the value synchronized.
1695 * Furthermore, as explained here:
1696 * http://www.microsoft.com/windows2000/techinfo/reskit/en-us/default.asp?url=/windows2000/techinfo/reskit/en-us/regentry/36276.asp
1697 * the HKCU paths take precedence over the HKLM paths.
1700 HRESULT WINAPI SHGetFolderPathW(
1701 HWND hwndOwner, /* [I] owner window */
1702 int nFolder, /* [I] CSIDL identifing the folder */
1703 HANDLE hToken, /* [I] access token */
1704 DWORD dwFlags, /* [I] which path to return */
1705 LPWSTR pszPath) /* [O] converted path */
1707 HRESULT hr;
1708 WCHAR szBuildPath[MAX_PATH], szTemp[MAX_PATH];
1709 DWORD folder = nFolder & CSIDL_FOLDER_MASK;
1710 CSIDL_Type type;
1711 int ret;
1713 TRACE("%p,%p,nFolder=0x%04x\n", hwndOwner,pszPath,nFolder);
1715 /* Windows always NULL-terminates the resulting path regardless of success
1716 * or failure, so do so first
1718 if (pszPath)
1719 *pszPath = '\0';
1720 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1721 return E_INVALIDARG;
1722 szTemp[0] = 0;
1723 type = CSIDL_Data[folder].type;
1724 switch (type)
1726 case CSIDL_Type_Disallowed:
1727 hr = E_INVALIDARG;
1728 break;
1729 case CSIDL_Type_NonExistent:
1730 hr = S_FALSE;
1731 break;
1732 case CSIDL_Type_WindowsPath:
1733 GetWindowsDirectoryW(szTemp, MAX_PATH);
1734 if (CSIDL_Data[folder].szDefaultPath &&
1735 !IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath) &&
1736 *CSIDL_Data[folder].szDefaultPath)
1738 PathAddBackslashW(szTemp);
1739 strcatW(szTemp, CSIDL_Data[folder].szDefaultPath);
1741 hr = S_OK;
1742 break;
1743 case CSIDL_Type_SystemPath:
1744 GetSystemDirectoryW(szTemp, MAX_PATH);
1745 if (CSIDL_Data[folder].szDefaultPath &&
1746 !IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath) &&
1747 *CSIDL_Data[folder].szDefaultPath)
1749 PathAddBackslashW(szTemp);
1750 strcatW(szTemp, CSIDL_Data[folder].szDefaultPath);
1752 hr = S_OK;
1753 break;
1754 case CSIDL_Type_CurrVer:
1755 hr = _SHGetCurrentVersionPath(dwFlags, folder, szTemp);
1756 break;
1757 case CSIDL_Type_User:
1758 hr = _SHGetUserProfilePath(hToken, dwFlags, folder, szTemp);
1759 break;
1760 case CSIDL_Type_AllUsers:
1761 hr = _SHGetAllUsersProfilePath(dwFlags, folder, szTemp);
1762 break;
1763 default:
1764 FIXME("bogus type %d, please fix\n", type);
1765 hr = E_INVALIDARG;
1766 break;
1769 /* Expand environment strings if necessary */
1770 if (*szTemp == '%')
1771 hr = _SHExpandEnvironmentStrings(szTemp, szBuildPath);
1772 else
1773 strcpyW(szBuildPath, szTemp);
1774 /* Copy the path if it's available before we might return */
1775 if (SUCCEEDED(hr) && pszPath)
1776 strcpyW(pszPath, szBuildPath);
1778 if (FAILED(hr)) goto end;
1780 /* if we don't care about existing directories we are ready */
1781 if(nFolder & CSIDL_FLAG_DONT_VERIFY) goto end;
1783 if (PathFileExistsW(szBuildPath)) goto end;
1785 /* not existing but we are not allowed to create it. The return value
1786 * is verified against shell32 version 6.0.
1788 if (!(nFolder & CSIDL_FLAG_CREATE))
1790 hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
1791 goto end;
1794 /* create directory/directories */
1795 ret = SHCreateDirectoryExW(hwndOwner, szBuildPath, NULL);
1796 if (ret && ret != ERROR_ALREADY_EXISTS)
1798 ERR("Failed to create directory '%s'.\n", debugstr_w(szBuildPath));
1799 hr = E_FAIL;
1800 goto end;
1803 TRACE("Created missing system directory '%s'\n", debugstr_w(szBuildPath));
1804 end:
1805 TRACE("returning 0x%08lx (final path is %s)\n", hr, debugstr_w(szBuildPath));
1806 return hr;
1809 /*************************************************************************
1810 * SHGetFolderPathA [SHELL32.@]
1812 * See SHGetFolderPathW.
1814 HRESULT WINAPI SHGetFolderPathA(
1815 HWND hwndOwner,
1816 int nFolder,
1817 HANDLE hToken,
1818 DWORD dwFlags,
1819 LPSTR pszPath)
1821 WCHAR szTemp[MAX_PATH];
1822 HRESULT hr;
1824 TRACE("%p,%p,nFolder=0x%04x\n",hwndOwner,pszPath,nFolder);
1826 if (pszPath)
1827 *pszPath = '\0';
1828 hr = SHGetFolderPathW(hwndOwner, nFolder, hToken, dwFlags, szTemp);
1829 if (SUCCEEDED(hr) && pszPath)
1830 WideCharToMultiByte(CP_ACP, 0, szTemp, -1, pszPath, MAX_PATH, NULL,
1831 NULL);
1833 return hr;
1836 /* For each folder in folders, if its value has not been set in the registry,
1837 * calls _SHGetUserProfilePath or _SHGetAllUsersProfilePath (depending on the
1838 * folder's type) to get the unexpanded value first.
1839 * Writes the unexpanded value to User Shell Folders, and queries it with
1840 * SHGetFolderPathW to force the creation of the directory if it doesn't
1841 * already exist. SHGetFolderPathW also returns the expanded value, which
1842 * this then writes to Shell Folders.
1844 static HRESULT _SHRegisterFolders(HKEY hRootKey, HANDLE hToken,
1845 LPCWSTR szUserShellFolderPath, LPCWSTR szShellFolderPath, const UINT folders[],
1846 UINT foldersLen)
1848 UINT i;
1849 WCHAR path[MAX_PATH];
1850 HRESULT hr = S_OK;
1851 HKEY hUserKey = NULL, hKey = NULL;
1852 DWORD dwDisp, dwType, dwPathLen;
1853 LONG ret;
1855 TRACE("%p,%p,%s,%p,%u\n", hRootKey, hToken,
1856 debugstr_w(szUserShellFolderPath), folders, foldersLen);
1858 ret = RegCreateKeyExW(hRootKey, szUserShellFolderPath, 0, NULL, 0,
1859 KEY_ALL_ACCESS, NULL, &hUserKey, &dwDisp);
1860 if (ret)
1861 hr = HRESULT_FROM_WIN32(ret);
1862 else
1864 ret = RegCreateKeyExW(hRootKey, szShellFolderPath, 0, NULL, 0,
1865 KEY_ALL_ACCESS, NULL, &hKey, &dwDisp);
1866 if (ret)
1867 hr = HRESULT_FROM_WIN32(ret);
1869 for (i = 0; SUCCEEDED(hr) && i < foldersLen; i++)
1871 dwPathLen = MAX_PATH * sizeof(WCHAR);
1872 if (RegQueryValueExW(hUserKey, CSIDL_Data[folders[i]].szValueName, NULL,
1873 &dwType, (LPBYTE)path, &dwPathLen) || (dwType != REG_SZ &&
1874 dwType != REG_EXPAND_SZ))
1876 *path = '\0';
1877 if (CSIDL_Data[folders[i]].type == CSIDL_Type_User)
1878 _SHGetUserProfilePath(hToken, SHGFP_TYPE_DEFAULT, folders[i],
1879 path);
1880 else if (CSIDL_Data[folders[i]].type == CSIDL_Type_AllUsers)
1881 _SHGetAllUsersProfilePath(SHGFP_TYPE_DEFAULT, folders[i], path);
1882 else
1883 hr = E_FAIL;
1884 if (*path)
1886 ret = RegSetValueExW(hUserKey,
1887 CSIDL_Data[folders[i]].szValueName, 0, REG_EXPAND_SZ,
1888 (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR));
1889 if (ret)
1890 hr = HRESULT_FROM_WIN32(ret);
1891 else
1893 hr = SHGetFolderPathW(NULL, folders[i] | CSIDL_FLAG_CREATE,
1894 hToken, SHGFP_TYPE_DEFAULT, path);
1895 ret = RegSetValueExW(hKey,
1896 CSIDL_Data[folders[i]].szValueName, 0, REG_SZ,
1897 (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR));
1898 if (ret)
1899 hr = HRESULT_FROM_WIN32(ret);
1904 if (hUserKey)
1905 RegCloseKey(hUserKey);
1906 if (hKey)
1907 RegCloseKey(hKey);
1909 TRACE("returning 0x%08lx\n", hr);
1910 return hr;
1913 static HRESULT _SHRegisterUserShellFolders(BOOL bDefault)
1915 static const UINT folders[] = {
1916 CSIDL_PROGRAMS,
1917 CSIDL_PERSONAL,
1918 CSIDL_FAVORITES,
1919 CSIDL_APPDATA,
1920 CSIDL_STARTUP,
1921 CSIDL_RECENT,
1922 CSIDL_SENDTO,
1923 CSIDL_STARTMENU,
1924 CSIDL_DESKTOPDIRECTORY,
1925 CSIDL_NETHOOD,
1926 CSIDL_TEMPLATES,
1927 CSIDL_PRINTHOOD,
1928 CSIDL_COOKIES,
1929 CSIDL_HISTORY,
1931 WCHAR userShellFolderPath[MAX_PATH], shellFolderPath[MAX_PATH];
1932 LPCWSTR pUserShellFolderPath, pShellFolderPath;
1933 HRESULT hr = S_OK;
1934 HKEY hRootKey;
1935 HANDLE hToken;
1937 TRACE("%s\n", bDefault ? "TRUE" : "FALSE");
1938 if (bDefault)
1940 hToken = (HANDLE)-1;
1941 hRootKey = HKEY_USERS;
1942 strcpyW(userShellFolderPath, DefaultW);
1943 PathAddBackslashW(userShellFolderPath);
1944 strcatW(userShellFolderPath, szSHUserFolders);
1945 pUserShellFolderPath = userShellFolderPath;
1946 strcpyW(shellFolderPath, DefaultW);
1947 PathAddBackslashW(shellFolderPath);
1948 strcatW(shellFolderPath, szSHFolders);
1949 pShellFolderPath = shellFolderPath;
1951 else
1953 hToken = NULL;
1954 hRootKey = HKEY_CURRENT_USER;
1955 pUserShellFolderPath = szSHUserFolders;
1956 pShellFolderPath = szSHFolders;
1959 hr = _SHRegisterFolders(hRootKey, hToken, pUserShellFolderPath,
1960 pShellFolderPath, folders, sizeof(folders) / sizeof(folders[0]));
1961 TRACE("returning 0x%08lx\n", hr);
1962 return hr;
1965 static HRESULT _SHRegisterCommonShellFolders(void)
1967 static const UINT folders[] = {
1968 CSIDL_COMMON_STARTMENU,
1969 CSIDL_COMMON_PROGRAMS,
1970 CSIDL_COMMON_STARTUP,
1971 CSIDL_COMMON_DESKTOPDIRECTORY,
1972 CSIDL_COMMON_FAVORITES,
1973 CSIDL_COMMON_APPDATA,
1974 CSIDL_COMMON_TEMPLATES,
1975 CSIDL_COMMON_DOCUMENTS,
1977 HRESULT hr;
1979 TRACE("\n");
1980 hr = _SHRegisterFolders(HKEY_LOCAL_MACHINE, NULL, szSHUserFolders,
1981 szSHFolders, folders, sizeof(folders) / sizeof(folders[0]));
1982 TRACE("returning 0x%08lx\n", hr);
1983 return hr;
1986 /* Register the default values in the registry, as some apps seem to depend
1987 * on their presence. The set registered was taken from Windows XP.
1989 HRESULT SHELL_RegisterShellFolders(void)
1991 HRESULT hr = _SHRegisterUserShellFolders(TRUE);
1993 if (SUCCEEDED(hr))
1994 hr = _SHRegisterUserShellFolders(FALSE);
1995 if (SUCCEEDED(hr))
1996 hr = _SHRegisterCommonShellFolders();
1997 return hr;
2000 /*************************************************************************
2001 * SHGetSpecialFolderPathA [SHELL32.@]
2003 BOOL WINAPI SHGetSpecialFolderPathA (
2004 HWND hwndOwner,
2005 LPSTR szPath,
2006 int nFolder,
2007 BOOL bCreate)
2009 return (SHGetFolderPathA(
2010 hwndOwner,
2011 nFolder + (bCreate ? CSIDL_FLAG_CREATE : 0),
2012 NULL,
2014 szPath)) == S_OK ? TRUE : FALSE;
2017 /*************************************************************************
2018 * SHGetSpecialFolderPathW
2020 BOOL WINAPI SHGetSpecialFolderPathW (
2021 HWND hwndOwner,
2022 LPWSTR szPath,
2023 int nFolder,
2024 BOOL bCreate)
2026 return (SHGetFolderPathW(
2027 hwndOwner,
2028 nFolder + (bCreate ? CSIDL_FLAG_CREATE : 0),
2029 NULL,
2031 szPath)) == S_OK ? TRUE : FALSE;
2034 /*************************************************************************
2035 * SHGetSpecialFolderPath (SHELL32.175)
2037 BOOL WINAPI SHGetSpecialFolderPathAW (
2038 HWND hwndOwner,
2039 LPVOID szPath,
2040 int nFolder,
2041 BOOL bCreate)
2044 if (SHELL_OsIsUnicode())
2045 return SHGetSpecialFolderPathW (hwndOwner, szPath, nFolder, bCreate);
2046 return SHGetSpecialFolderPathA (hwndOwner, szPath, nFolder, bCreate);
2049 /*************************************************************************
2050 * SHGetFolderLocation [SHELL32.@]
2052 * Gets the folder locations from the registry and creates a pidl.
2054 * PARAMS
2055 * hwndOwner [I]
2056 * nFolder [I] CSIDL_xxxxx
2057 * hToken [I] token representing user, or NULL for current user, or -1 for
2058 * default user
2059 * dwReserved [I] must be zero
2060 * ppidl [O] PIDL of a special folder
2062 * RETURNS
2063 * Success: S_OK
2064 * Failure: Standard OLE-defined error result, S_FALSE or E_INVALIDARG
2066 * NOTES
2067 * Creates missing reg keys and directories.
2068 * Mostly forwards to SHGetFolderPathW, but a few values of nFolder return
2069 * virtual folders that are handled here.
2071 HRESULT WINAPI SHGetFolderLocation(
2072 HWND hwndOwner,
2073 int nFolder,
2074 HANDLE hToken,
2075 DWORD dwReserved,
2076 LPITEMIDLIST *ppidl)
2078 HRESULT hr = E_INVALIDARG;
2080 TRACE("%p 0x%08x %p 0x%08lx %p\n",
2081 hwndOwner, nFolder, hToken, dwReserved, ppidl);
2083 if (!ppidl)
2084 return E_INVALIDARG;
2085 if (dwReserved)
2086 return E_INVALIDARG;
2088 /* The virtual folders' locations are not user-dependent */
2089 *ppidl = NULL;
2090 switch (nFolder)
2092 case CSIDL_DESKTOP:
2093 *ppidl = _ILCreateDesktop();
2094 break;
2096 case CSIDL_INTERNET:
2097 *ppidl = _ILCreateIExplore();
2098 break;
2100 case CSIDL_CONTROLS:
2101 *ppidl = _ILCreateControlPanel();
2102 break;
2104 case CSIDL_PRINTERS:
2105 *ppidl = _ILCreatePrinters();
2106 break;
2108 case CSIDL_BITBUCKET:
2109 *ppidl = _ILCreateBitBucket();
2110 break;
2112 case CSIDL_DRIVES:
2113 *ppidl = _ILCreateMyComputer();
2114 break;
2116 case CSIDL_NETWORK:
2117 *ppidl = _ILCreateNetwork();
2118 break;
2120 default:
2122 WCHAR szPath[MAX_PATH];
2124 hr = SHGetFolderPathW(hwndOwner, nFolder, hToken,
2125 SHGFP_TYPE_CURRENT, szPath);
2126 if (SUCCEEDED(hr))
2128 DWORD attributes=0;
2130 TRACE("Value=%s\n", debugstr_w(szPath));
2131 hr = SHILCreateFromPathW(szPath, ppidl, &attributes);
2133 else if (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
2135 /* unlike SHGetFolderPath, SHGetFolderLocation in shell32
2136 * version 6.0 returns E_FAIL for nonexistent paths
2138 hr = E_FAIL;
2142 if(*ppidl)
2143 hr = NOERROR;
2145 TRACE("-- (new pidl %p)\n",*ppidl);
2146 return hr;
2149 /*************************************************************************
2150 * SHGetSpecialFolderLocation [SHELL32.@]
2152 * NOTES
2153 * In NT5, SHGetSpecialFolderLocation needs the <winntdir>/Recent
2154 * directory.
2156 HRESULT WINAPI SHGetSpecialFolderLocation(
2157 HWND hwndOwner,
2158 INT nFolder,
2159 LPITEMIDLIST * ppidl)
2161 HRESULT hr = E_INVALIDARG;
2163 TRACE("(%p,0x%x,%p)\n", hwndOwner,nFolder,ppidl);
2165 if (!ppidl)
2166 return E_INVALIDARG;
2168 hr = SHGetFolderLocation(hwndOwner, nFolder, NULL, 0, ppidl);
2169 return hr;