Changes in crossover-wine-src-6.1.0 except for configure
[wine/hacks.git] / dlls / shell32 / shellpath.c
blobee1fb584fbab23e10998d574d714c2aca0145ac4
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"
50 WINE_DEFAULT_DEBUG_CHANNEL(shell);
53 ########## Combining and Constructing paths ##########
56 /*************************************************************************
57 * PathAppend [SHELL32.36]
59 BOOL WINAPI PathAppendAW(
60 LPVOID lpszPath1,
61 LPCVOID lpszPath2)
63 if (SHELL_OsIsUnicode())
64 return PathAppendW(lpszPath1, lpszPath2);
65 return PathAppendA(lpszPath1, lpszPath2);
68 /*************************************************************************
69 * PathCombine [SHELL32.37]
71 LPVOID WINAPI PathCombineAW(
72 LPVOID szDest,
73 LPCVOID lpszDir,
74 LPCVOID lpszFile)
76 if (SHELL_OsIsUnicode())
77 return PathCombineW( szDest, lpszDir, lpszFile );
78 return PathCombineA( szDest, lpszDir, lpszFile );
81 /*************************************************************************
82 * PathAddBackslash [SHELL32.32]
84 LPVOID WINAPI PathAddBackslashAW(LPVOID lpszPath)
86 if(SHELL_OsIsUnicode())
87 return PathAddBackslashW(lpszPath);
88 return PathAddBackslashA(lpszPath);
91 /*************************************************************************
92 * PathBuildRoot [SHELL32.30]
94 LPVOID WINAPI PathBuildRootAW(LPVOID lpszPath, int drive)
96 if(SHELL_OsIsUnicode())
97 return PathBuildRootW(lpszPath, drive);
98 return PathBuildRootA(lpszPath, drive);
102 Extracting Component Parts
105 /*************************************************************************
106 * PathFindFileName [SHELL32.34]
108 LPVOID WINAPI PathFindFileNameAW(LPCVOID lpszPath)
110 if(SHELL_OsIsUnicode())
111 return PathFindFileNameW(lpszPath);
112 return PathFindFileNameA(lpszPath);
115 /*************************************************************************
116 * PathFindExtension [SHELL32.31]
118 LPVOID WINAPI PathFindExtensionAW(LPCVOID lpszPath)
120 if (SHELL_OsIsUnicode())
121 return PathFindExtensionW(lpszPath);
122 return PathFindExtensionA(lpszPath);
126 /*************************************************************************
127 * PathGetExtensionA [internal]
129 * NOTES
130 * exported by ordinal
131 * return value points to the first char after the dot
133 static LPSTR PathGetExtensionA(LPCSTR lpszPath)
135 TRACE("(%s)\n",lpszPath);
137 lpszPath = PathFindExtensionA(lpszPath);
138 return (LPSTR)(*lpszPath?(lpszPath+1):lpszPath);
141 /*************************************************************************
142 * PathGetExtensionW [internal]
144 static LPWSTR PathGetExtensionW(LPCWSTR lpszPath)
146 TRACE("(%s)\n",debugstr_w(lpszPath));
148 lpszPath = PathFindExtensionW(lpszPath);
149 return (LPWSTR)(*lpszPath?(lpszPath+1):lpszPath);
152 /*************************************************************************
153 * PathGetExtension [SHELL32.158]
155 LPVOID WINAPI PathGetExtensionAW(LPCVOID lpszPath,DWORD void1, DWORD void2)
157 if (SHELL_OsIsUnicode())
158 return PathGetExtensionW(lpszPath);
159 return PathGetExtensionA(lpszPath);
162 /*************************************************************************
163 * PathGetArgs [SHELL32.52]
165 LPVOID WINAPI PathGetArgsAW(LPVOID lpszPath)
167 if (SHELL_OsIsUnicode())
168 return PathGetArgsW(lpszPath);
169 return PathGetArgsA(lpszPath);
172 /*************************************************************************
173 * PathGetDriveNumber [SHELL32.57]
175 int WINAPI PathGetDriveNumberAW(LPVOID lpszPath)
177 if (SHELL_OsIsUnicode())
178 return PathGetDriveNumberW(lpszPath);
179 return PathGetDriveNumberA(lpszPath);
182 /*************************************************************************
183 * PathRemoveFileSpec [SHELL32.35]
185 BOOL WINAPI PathRemoveFileSpecAW(LPVOID lpszPath)
187 if (SHELL_OsIsUnicode())
188 return PathRemoveFileSpecW(lpszPath);
189 return PathRemoveFileSpecA(lpszPath);
192 /*************************************************************************
193 * PathStripPath [SHELL32.38]
195 void WINAPI PathStripPathAW(LPVOID lpszPath)
197 if (SHELL_OsIsUnicode())
198 PathStripPathW(lpszPath);
199 else
200 PathStripPathA(lpszPath);
203 /*************************************************************************
204 * PathStripToRoot [SHELL32.50]
206 BOOL WINAPI PathStripToRootAW(LPVOID lpszPath)
208 if (SHELL_OsIsUnicode())
209 return PathStripToRootW(lpszPath);
210 return PathStripToRootA(lpszPath);
213 /*************************************************************************
214 * PathRemoveArgs [SHELL32.251]
216 void WINAPI PathRemoveArgsAW(LPVOID lpszPath)
218 if (SHELL_OsIsUnicode())
219 PathRemoveArgsW(lpszPath);
220 else
221 PathRemoveArgsA(lpszPath);
224 /*************************************************************************
225 * PathRemoveExtension [SHELL32.250]
227 void WINAPI PathRemoveExtensionAW(LPVOID lpszPath)
229 if (SHELL_OsIsUnicode())
230 PathRemoveExtensionW(lpszPath);
231 else
232 PathRemoveExtensionA(lpszPath);
237 Path Manipulations
240 /*************************************************************************
241 * PathGetShortPathA [internal]
243 static void PathGetShortPathA(LPSTR pszPath)
245 CHAR path[MAX_PATH];
247 TRACE("%s\n", pszPath);
249 if (GetShortPathNameA(pszPath, path, MAX_PATH))
251 lstrcpyA(pszPath, path);
255 /*************************************************************************
256 * PathGetShortPathW [internal]
258 static void PathGetShortPathW(LPWSTR pszPath)
260 WCHAR path[MAX_PATH];
262 TRACE("%s\n", debugstr_w(pszPath));
264 if (GetShortPathNameW(pszPath, path, MAX_PATH))
266 lstrcpyW(pszPath, path);
270 /*************************************************************************
271 * PathGetShortPath [SHELL32.92]
273 VOID WINAPI PathGetShortPathAW(LPVOID pszPath)
275 if(SHELL_OsIsUnicode())
276 PathGetShortPathW(pszPath);
277 PathGetShortPathA(pszPath);
280 /*************************************************************************
281 * PathRemoveBlanks [SHELL32.33]
283 void WINAPI PathRemoveBlanksAW(LPVOID str)
285 if(SHELL_OsIsUnicode())
286 PathRemoveBlanksW(str);
287 else
288 PathRemoveBlanksA(str);
291 /*************************************************************************
292 * PathQuoteSpaces [SHELL32.55]
294 VOID WINAPI PathQuoteSpacesAW (LPVOID lpszPath)
296 if(SHELL_OsIsUnicode())
297 PathQuoteSpacesW(lpszPath);
298 else
299 PathQuoteSpacesA(lpszPath);
302 /*************************************************************************
303 * PathUnquoteSpaces [SHELL32.56]
305 VOID WINAPI PathUnquoteSpacesAW(LPVOID str)
307 if(SHELL_OsIsUnicode())
308 PathUnquoteSpacesW(str);
309 else
310 PathUnquoteSpacesA(str);
313 /*************************************************************************
314 * PathParseIconLocation [SHELL32.249]
316 int WINAPI PathParseIconLocationAW (LPVOID lpszPath)
318 if(SHELL_OsIsUnicode())
319 return PathParseIconLocationW(lpszPath);
320 return PathParseIconLocationA(lpszPath);
324 ########## Path Testing ##########
326 /*************************************************************************
327 * PathIsUNC [SHELL32.39]
329 BOOL WINAPI PathIsUNCAW (LPCVOID lpszPath)
331 if (SHELL_OsIsUnicode())
332 return PathIsUNCW( lpszPath );
333 return PathIsUNCA( lpszPath );
336 /*************************************************************************
337 * PathIsRelative [SHELL32.40]
339 BOOL WINAPI PathIsRelativeAW (LPCVOID lpszPath)
341 if (SHELL_OsIsUnicode())
342 return PathIsRelativeW( lpszPath );
343 return PathIsRelativeA( lpszPath );
346 /*************************************************************************
347 * PathIsRoot [SHELL32.29]
349 BOOL WINAPI PathIsRootAW(LPCVOID lpszPath)
351 if (SHELL_OsIsUnicode())
352 return PathIsRootW(lpszPath);
353 return PathIsRootA(lpszPath);
356 /*************************************************************************
357 * PathIsExeA [internal]
359 static BOOL PathIsExeA (LPCSTR lpszPath)
361 LPCSTR lpszExtension = PathGetExtensionA(lpszPath);
362 int i;
363 static const char * const lpszExtensions[] =
364 {"exe", "com", "pif", "cmd", "bat", "scf", "scr", NULL };
366 TRACE("path=%s\n",lpszPath);
368 for(i=0; lpszExtensions[i]; i++)
369 if (!lstrcmpiA(lpszExtension,lpszExtensions[i])) return TRUE;
371 return FALSE;
374 /*************************************************************************
375 * PathIsExeW [internal]
377 static BOOL PathIsExeW (LPCWSTR lpszPath)
379 LPCWSTR lpszExtension = PathGetExtensionW(lpszPath);
380 int i;
381 static const WCHAR lpszExtensions[][4] =
382 {{'e','x','e','\0'}, {'c','o','m','\0'}, {'p','i','f','\0'},
383 {'c','m','d','\0'}, {'b','a','t','\0'}, {'s','c','f','\0'},
384 {'s','c','r','\0'}, {'\0'} };
386 TRACE("path=%s\n",debugstr_w(lpszPath));
388 for(i=0; lpszExtensions[i][0]; i++)
389 if (!strcmpiW(lpszExtension,lpszExtensions[i])) return TRUE;
391 return FALSE;
394 /*************************************************************************
395 * PathIsExe [SHELL32.43]
397 BOOL WINAPI PathIsExeAW (LPCVOID path)
399 if (SHELL_OsIsUnicode())
400 return PathIsExeW (path);
401 return PathIsExeA(path);
404 /*************************************************************************
405 * PathIsDirectory [SHELL32.159]
407 BOOL WINAPI PathIsDirectoryAW (LPCVOID lpszPath)
409 if (SHELL_OsIsUnicode())
410 return PathIsDirectoryW (lpszPath);
411 return PathIsDirectoryA (lpszPath);
414 /*************************************************************************
415 * PathFileExists [SHELL32.45]
417 BOOL WINAPI PathFileExistsAW (LPCVOID lpszPath)
419 if (SHELL_OsIsUnicode())
420 return PathFileExistsW (lpszPath);
421 return PathFileExistsA (lpszPath);
424 /*************************************************************************
425 * PathMatchSpec [SHELL32.46]
427 BOOL WINAPI PathMatchSpecAW(LPVOID name, LPVOID mask)
429 if (SHELL_OsIsUnicode())
430 return PathMatchSpecW( name, mask );
431 return PathMatchSpecA( name, mask );
434 /*************************************************************************
435 * PathIsSameRoot [SHELL32.650]
437 BOOL WINAPI PathIsSameRootAW(LPCVOID lpszPath1, LPCVOID lpszPath2)
439 if (SHELL_OsIsUnicode())
440 return PathIsSameRootW(lpszPath1, lpszPath2);
441 return PathIsSameRootA(lpszPath1, lpszPath2);
444 /*************************************************************************
445 * IsLFNDriveA [SHELL32.41]
447 BOOL WINAPI IsLFNDriveA(LPCSTR lpszPath)
449 DWORD fnlen;
451 if (!GetVolumeInformationA(lpszPath, NULL, 0, NULL, &fnlen, NULL, NULL, 0))
452 return FALSE;
453 return fnlen > 12;
456 /*************************************************************************
457 * IsLFNDriveW [SHELL32.42]
459 BOOL WINAPI IsLFNDriveW(LPCWSTR lpszPath)
461 DWORD fnlen;
463 if (!GetVolumeInformationW(lpszPath, NULL, 0, NULL, &fnlen, NULL, NULL, 0))
464 return FALSE;
465 return fnlen > 12;
468 /*************************************************************************
469 * IsLFNDrive [SHELL32.119]
471 BOOL WINAPI IsLFNDriveAW(LPCVOID lpszPath)
473 if (SHELL_OsIsUnicode())
474 return IsLFNDriveW(lpszPath);
475 return IsLFNDriveA(lpszPath);
479 ########## Creating Something Unique ##########
481 /*************************************************************************
482 * PathMakeUniqueNameA [internal]
484 BOOL WINAPI PathMakeUniqueNameA(
485 LPSTR lpszBuffer,
486 DWORD dwBuffSize,
487 LPCSTR lpszShortName,
488 LPCSTR lpszLongName,
489 LPCSTR lpszPathName)
491 FIXME("%p %u %s %s %s stub\n",
492 lpszBuffer, dwBuffSize, debugstr_a(lpszShortName),
493 debugstr_a(lpszLongName), debugstr_a(lpszPathName));
494 return TRUE;
497 /*************************************************************************
498 * PathMakeUniqueNameW [internal]
500 BOOL WINAPI PathMakeUniqueNameW(
501 LPWSTR lpszBuffer,
502 DWORD dwBuffSize,
503 LPCWSTR lpszShortName,
504 LPCWSTR lpszLongName,
505 LPCWSTR lpszPathName)
507 FIXME("%p %u %s %s %s stub\n",
508 lpszBuffer, dwBuffSize, debugstr_w(lpszShortName),
509 debugstr_w(lpszLongName), debugstr_w(lpszPathName));
510 return TRUE;
513 /*************************************************************************
514 * PathMakeUniqueName [SHELL32.47]
516 BOOL WINAPI PathMakeUniqueNameAW(
517 LPVOID lpszBuffer,
518 DWORD dwBuffSize,
519 LPCVOID lpszShortName,
520 LPCVOID lpszLongName,
521 LPCVOID lpszPathName)
523 if (SHELL_OsIsUnicode())
524 return PathMakeUniqueNameW(lpszBuffer,dwBuffSize, lpszShortName,lpszLongName,lpszPathName);
525 return PathMakeUniqueNameA(lpszBuffer,dwBuffSize, lpszShortName,lpszLongName,lpszPathName);
528 /*************************************************************************
529 * PathYetAnotherMakeUniqueName [SHELL32.75]
531 * NOTES
532 * exported by ordinal
534 BOOL WINAPI PathYetAnotherMakeUniqueName(
535 LPWSTR lpszBuffer,
536 LPCWSTR lpszPathName,
537 LPCWSTR lpszShortName,
538 LPCWSTR lpszLongName)
540 FIXME("(%p, %s, %s ,%s):stub.\n",
541 lpszBuffer, debugstr_w(lpszPathName), debugstr_w(lpszShortName), debugstr_w(lpszLongName));
542 return TRUE;
547 ########## cleaning and resolving paths ##########
550 /*************************************************************************
551 * PathFindOnPath [SHELL32.145]
553 BOOL WINAPI PathFindOnPathAW(LPVOID sFile, LPCVOID *sOtherDirs)
555 if (SHELL_OsIsUnicode())
556 return PathFindOnPathW(sFile, (LPCWSTR *)sOtherDirs);
557 return PathFindOnPathA(sFile, (LPCSTR *)sOtherDirs);
560 /*************************************************************************
561 * PathCleanupSpec [SHELL32.171]
563 * lpszFile is changed in place.
565 int WINAPI PathCleanupSpec( LPCWSTR lpszPathW, LPWSTR lpszFileW )
567 int i = 0;
568 DWORD rc = 0;
569 int length = 0;
571 if (SHELL_OsIsUnicode())
573 LPWSTR p = lpszFileW;
575 TRACE("Cleanup %s\n",debugstr_w(lpszFileW));
577 if (lpszPathW)
578 length = strlenW(lpszPathW);
580 while (*p)
582 int gct = PathGetCharTypeW(*p);
583 if (gct == GCT_INVALID || gct == GCT_WILD || gct == GCT_SEPARATOR)
585 lpszFileW[i]='-';
586 rc |= PCS_REPLACEDCHAR;
588 else
589 lpszFileW[i]=*p;
590 i++;
591 p++;
592 if (length + i == MAX_PATH)
594 rc |= PCS_FATAL | PCS_PATHTOOLONG;
595 break;
598 lpszFileW[i]=0;
600 else
602 LPSTR lpszFileA = (LPSTR)lpszFileW;
603 LPCSTR lpszPathA = (LPCSTR)lpszPathW;
604 LPSTR p = lpszFileA;
606 TRACE("Cleanup %s\n",debugstr_a(lpszFileA));
608 if (lpszPathA)
609 length = strlen(lpszPathA);
611 while (*p)
613 int gct = PathGetCharTypeA(*p);
614 if (gct == GCT_INVALID || gct == GCT_WILD || gct == GCT_SEPARATOR)
616 lpszFileA[i]='-';
617 rc |= PCS_REPLACEDCHAR;
619 else
620 lpszFileA[i]=*p;
621 i++;
622 p++;
623 if (length + i == MAX_PATH)
625 rc |= PCS_FATAL | PCS_PATHTOOLONG;
626 break;
629 lpszFileA[i]=0;
631 return rc;
634 /*************************************************************************
635 * PathQualifyA [SHELL32]
637 BOOL WINAPI PathQualifyA(LPCSTR pszPath)
639 FIXME("%s\n",pszPath);
640 return 0;
643 /*************************************************************************
644 * PathQualifyW [SHELL32]
646 BOOL WINAPI PathQualifyW(LPCWSTR pszPath)
648 FIXME("%s\n",debugstr_w(pszPath));
649 return 0;
652 /*************************************************************************
653 * PathQualify [SHELL32.49]
655 BOOL WINAPI PathQualifyAW(LPCVOID pszPath)
657 if (SHELL_OsIsUnicode())
658 return PathQualifyW(pszPath);
659 return PathQualifyA(pszPath);
662 /*************************************************************************
663 * PathResolveA [SHELL32.51]
665 BOOL WINAPI PathResolveA(
666 LPSTR lpszPath,
667 LPCSTR *alpszPaths,
668 DWORD dwFlags)
670 FIXME("(%s,%p,0x%08x),stub!\n",
671 lpszPath, *alpszPaths, dwFlags);
672 return 0;
675 /*************************************************************************
676 * PathResolveW [SHELL32]
678 BOOL WINAPI PathResolveW(
679 LPWSTR lpszPath,
680 LPCWSTR *alpszPaths,
681 DWORD dwFlags)
683 FIXME("(%s,%p,0x%08x),stub!\n",
684 debugstr_w(lpszPath), debugstr_w(*alpszPaths), dwFlags);
685 return 0;
688 /*************************************************************************
689 * PathResolve [SHELL32.51]
691 BOOL WINAPI PathResolveAW(
692 LPVOID lpszPath,
693 LPCVOID *alpszPaths,
694 DWORD dwFlags)
696 if (SHELL_OsIsUnicode())
697 return PathResolveW(lpszPath, (LPCWSTR*)alpszPaths, dwFlags);
698 return PathResolveA(lpszPath, (LPCSTR*)alpszPaths, dwFlags);
701 /*************************************************************************
702 * PathProcessCommandA [SHELL32.653]
704 LONG WINAPI PathProcessCommandA (
705 LPCSTR lpszPath,
706 LPSTR lpszBuff,
707 DWORD dwBuffSize,
708 DWORD dwFlags)
710 FIXME("%s %p 0x%04x 0x%04x stub\n",
711 lpszPath, lpszBuff, dwBuffSize, dwFlags);
712 if(!lpszPath) return -1;
713 if(lpszBuff) strcpy(lpszBuff, lpszPath);
714 return strlen(lpszPath);
717 /*************************************************************************
718 * PathProcessCommandW
720 LONG WINAPI PathProcessCommandW (
721 LPCWSTR lpszPath,
722 LPWSTR lpszBuff,
723 DWORD dwBuffSize,
724 DWORD dwFlags)
726 FIXME("(%s, %p, 0x%04x, 0x%04x) stub\n",
727 debugstr_w(lpszPath), lpszBuff, dwBuffSize, dwFlags);
728 if(!lpszPath) return -1;
729 if(lpszBuff) strcpyW(lpszBuff, lpszPath);
730 return strlenW(lpszPath);
733 /*************************************************************************
734 * PathProcessCommand (SHELL32.653)
736 LONG WINAPI PathProcessCommandAW (
737 LPCVOID lpszPath,
738 LPVOID lpszBuff,
739 DWORD dwBuffSize,
740 DWORD dwFlags)
742 if (SHELL_OsIsUnicode())
743 return PathProcessCommandW(lpszPath, lpszBuff, dwBuffSize, dwFlags);
744 return PathProcessCommandA(lpszPath, lpszBuff, dwBuffSize, dwFlags);
748 ########## special ##########
751 /*************************************************************************
752 * PathSetDlgItemPath (SHELL32.48)
754 VOID WINAPI PathSetDlgItemPathAW(HWND hDlg, int id, LPCVOID pszPath)
756 if (SHELL_OsIsUnicode())
757 PathSetDlgItemPathW(hDlg, id, pszPath);
758 else
759 PathSetDlgItemPathA(hDlg, id, pszPath);
762 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'};
763 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'};
764 static const WCHAR AppDataW[] = {'A','p','p','D','a','t','a','\0'};
765 static const WCHAR CacheW[] = {'C','a','c','h','e','\0'};
766 static const WCHAR CD_BurningW[] = {'C','D',' ','B','u','r','n','i','n','g','\0'};
767 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'};
768 static const WCHAR Common_AppDataW[] = {'C','o','m','m','o','n',' ','A','p','p','D','a','t','a','\0'};
769 static const WCHAR Common_DesktopW[] = {'C','o','m','m','o','n',' ','D','e','s','k','t','o','p','\0'};
770 static const WCHAR Common_DocumentsW[] = {'C','o','m','m','o','n',' ','D','o','c','u','m','e','n','t','s','\0'};
771 static const WCHAR CommonFilesDirW[] = {'C','o','m','m','o','n','F','i','l','e','s','D','i','r','\0'};
772 static const WCHAR CommonMusicW[] = {'C','o','m','m','o','n','M','u','s','i','c','\0'};
773 static const WCHAR CommonPicturesW[] = {'C','o','m','m','o','n','P','i','c','t','u','r','e','s','\0'};
774 static const WCHAR Common_ProgramsW[] = {'C','o','m','m','o','n',' ','P','r','o','g','r','a','m','s','\0'};
775 static const WCHAR Common_StartUpW[] = {'C','o','m','m','o','n',' ','S','t','a','r','t','U','p','\0'};
776 static const WCHAR Common_Start_MenuW[] = {'C','o','m','m','o','n',' ','S','t','a','r','t',' ','M','e','n','u','\0'};
777 static const WCHAR Common_TemplatesW[] = {'C','o','m','m','o','n',' ','T','e','m','p','l','a','t','e','s','\0'};
778 static const WCHAR CommonVideoW[] = {'C','o','m','m','o','n','V','i','d','e','o','\0'};
779 static const WCHAR CookiesW[] = {'C','o','o','k','i','e','s','\0'};
780 static const WCHAR DesktopW[] = {'D','e','s','k','t','o','p','\0'};
781 static const WCHAR FavoritesW[] = {'F','a','v','o','r','i','t','e','s','\0'};
782 static const WCHAR FontsW[] = {'F','o','n','t','s','\0'};
783 static const WCHAR HistoryW[] = {'H','i','s','t','o','r','y','\0'};
784 static const WCHAR Local_AppDataW[] = {'L','o','c','a','l',' ','A','p','p','D','a','t','a','\0'};
785 static const WCHAR My_MusicW[] = {'M','y',' ','M','u','s','i','c','\0'};
786 static const WCHAR My_PicturesW[] = {'M','y',' ','P','i','c','t','u','r','e','s','\0'};
787 static const WCHAR My_VideoW[] = {'M','y',' ','V','i','d','e','o','\0'};
788 static const WCHAR NetHoodW[] = {'N','e','t','H','o','o','d','\0'};
789 static const WCHAR PersonalW[] = {'P','e','r','s','o','n','a','l','\0'};
790 static const WCHAR PrintHoodW[] = {'P','r','i','n','t','H','o','o','d','\0'};
791 static const WCHAR ProgramFilesDirW[] = {'P','r','o','g','r','a','m','F','i','l','e','s','D','i','r','\0'};
792 static const WCHAR ProgramsW[] = {'P','r','o','g','r','a','m','s','\0'};
793 static const WCHAR RecentW[] = {'R','e','c','e','n','t','\0'};
794 static const WCHAR ResourcesW[] = {'R','e','s','o','u','r','c','e','s','\0'};
795 static const WCHAR SendToW[] = {'S','e','n','d','T','o','\0'};
796 static const WCHAR StartUpW[] = {'S','t','a','r','t','U','p','\0'};
797 static const WCHAR Start_MenuW[] = {'S','t','a','r','t',' ','M','e','n','u','\0'};
798 static const WCHAR TemplatesW[] = {'T','e','m','p','l','a','t','e','s','\0'};
799 static const WCHAR DefaultW[] = {'.','D','e','f','a','u','l','t','\0'};
800 static const WCHAR AllUsersProfileW[] = {'%','A','L','L','U','S','E','R','S','P','R','O','F','I','L','E','%','\0'};
801 static const WCHAR UserProfileW[] = {'%','U','S','E','R','P','R','O','F','I','L','E','%','\0'};
802 static const WCHAR SystemDriveW[] = {'%','S','y','s','t','e','m','D','r','i','v','e','%','\0'};
803 static const WCHAR ProfileListW[] = {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','P','r','o','f','i','l','e','L','i','s','t',0};
804 static const WCHAR ProfilesDirectoryW[] = {'P','r','o','f','i','l','e','s','D','i','r','e','c','t','o','r','y',0};
805 static const WCHAR AllUsersProfileValueW[] = {'A','l','l','U','s','e','r','s','P','r','o','f','i','l','e','\0'};
806 static const WCHAR szSHFolders[] = {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','E','x','p','l','o','r','e','r','\\','S','h','e','l','l',' ','F','o','l','d','e','r','s','\0'};
807 static const WCHAR szSHUserFolders[] = {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','E','x','p','l','o','r','e','r','\\','U','s','e','r',' ','S','h','e','l','l',' ','F','o','l','d','e','r','s','\0'};
808 /* This defaults to L"Documents and Settings" on Windows 2000/XP, but we're
809 * acting more Windows 9x-like for now.
811 static const WCHAR szDefaultProfileDirW[] = {'p','r','o','f','i','l','e','s','\0'};
812 static const WCHAR AllUsersW[] = {'A','l','l',' ','U','s','e','r','s','\0'};
814 typedef enum _CSIDL_Type {
815 CSIDL_Type_User,
816 CSIDL_Type_AllUsers,
817 CSIDL_Type_CurrVer,
818 CSIDL_Type_Disallowed,
819 CSIDL_Type_NonExistent,
820 CSIDL_Type_WindowsPath,
821 CSIDL_Type_SystemPath,
822 } CSIDL_Type;
824 typedef struct
826 CSIDL_Type type;
827 LPCWSTR szValueName;
828 LPCWSTR szDefaultPath; /* fallback string or resource ID */
829 } CSIDL_DATA;
831 static const CSIDL_DATA CSIDL_Data[] =
833 { /* 0x00 - CSIDL_DESKTOP */
834 CSIDL_Type_User,
835 DesktopW,
836 MAKEINTRESOURCEW(IDS_DESKTOPDIRECTORY)
838 { /* 0x01 - CSIDL_INTERNET */
839 CSIDL_Type_Disallowed,
840 NULL,
841 NULL
843 { /* 0x02 - CSIDL_PROGRAMS */
844 CSIDL_Type_User,
845 ProgramsW,
846 MAKEINTRESOURCEW(IDS_PROGRAMS)
848 { /* 0x03 - CSIDL_CONTROLS (.CPL files) */
849 CSIDL_Type_SystemPath,
850 NULL,
851 NULL
853 { /* 0x04 - CSIDL_PRINTERS */
854 CSIDL_Type_SystemPath,
855 NULL,
856 NULL
858 { /* 0x05 - CSIDL_PERSONAL */
859 CSIDL_Type_User,
860 PersonalW,
861 MAKEINTRESOURCEW(IDS_PERSONAL)
863 { /* 0x06 - CSIDL_FAVORITES */
864 CSIDL_Type_User,
865 FavoritesW,
866 MAKEINTRESOURCEW(IDS_FAVORITES)
868 { /* 0x07 - CSIDL_STARTUP */
869 CSIDL_Type_User,
870 StartUpW,
871 MAKEINTRESOURCEW(IDS_STARTUP)
873 { /* 0x08 - CSIDL_RECENT */
874 CSIDL_Type_User,
875 RecentW,
876 MAKEINTRESOURCEW(IDS_RECENT)
878 { /* 0x09 - CSIDL_SENDTO */
879 CSIDL_Type_User,
880 SendToW,
881 MAKEINTRESOURCEW(IDS_SENDTO)
883 { /* 0x0a - CSIDL_BITBUCKET - Recycle Bin */
884 CSIDL_Type_Disallowed,
885 NULL,
886 NULL,
888 { /* 0x0b - CSIDL_STARTMENU */
889 CSIDL_Type_User,
890 Start_MenuW,
891 MAKEINTRESOURCEW(IDS_STARTMENU)
893 { /* 0x0c - CSIDL_MYDOCUMENTS */
894 CSIDL_Type_Disallowed, /* matches WinXP--can't get its path */
895 NULL,
896 NULL
898 { /* 0x0d - CSIDL_MYMUSIC */
899 CSIDL_Type_User,
900 My_MusicW,
901 MAKEINTRESOURCEW(IDS_MYMUSIC)
903 { /* 0x0e - CSIDL_MYVIDEO */
904 CSIDL_Type_User,
905 My_VideoW,
906 MAKEINTRESOURCEW(IDS_MYVIDEO)
908 { /* 0x0f - unassigned */
909 CSIDL_Type_Disallowed,
910 NULL,
911 NULL,
913 { /* 0x10 - CSIDL_DESKTOPDIRECTORY */
914 CSIDL_Type_User,
915 DesktopW,
916 MAKEINTRESOURCEW(IDS_DESKTOPDIRECTORY)
918 { /* 0x11 - CSIDL_DRIVES */
919 CSIDL_Type_Disallowed,
920 NULL,
921 NULL,
923 { /* 0x12 - CSIDL_NETWORK */
924 CSIDL_Type_Disallowed,
925 NULL,
926 NULL,
928 { /* 0x13 - CSIDL_NETHOOD */
929 CSIDL_Type_User,
930 NetHoodW,
931 MAKEINTRESOURCEW(IDS_NETHOOD)
933 { /* 0x14 - CSIDL_FONTS */
934 CSIDL_Type_WindowsPath,
935 FontsW,
936 FontsW
938 { /* 0x15 - CSIDL_TEMPLATES */
939 CSIDL_Type_User,
940 TemplatesW,
941 MAKEINTRESOURCEW(IDS_TEMPLATES)
943 { /* 0x16 - CSIDL_COMMON_STARTMENU */
944 CSIDL_Type_AllUsers,
945 Common_Start_MenuW,
946 MAKEINTRESOURCEW(IDS_STARTMENU)
948 { /* 0x17 - CSIDL_COMMON_PROGRAMS */
949 CSIDL_Type_AllUsers,
950 Common_ProgramsW,
951 MAKEINTRESOURCEW(IDS_PROGRAMS)
953 { /* 0x18 - CSIDL_COMMON_STARTUP */
954 CSIDL_Type_AllUsers,
955 Common_StartUpW,
956 MAKEINTRESOURCEW(IDS_STARTUP)
958 { /* 0x19 - CSIDL_COMMON_DESKTOPDIRECTORY */
959 CSIDL_Type_AllUsers,
960 Common_DesktopW,
961 MAKEINTRESOURCEW(IDS_DESKTOP)
963 { /* 0x1a - CSIDL_APPDATA */
964 CSIDL_Type_User,
965 AppDataW,
966 MAKEINTRESOURCEW(IDS_APPDATA)
968 { /* 0x1b - CSIDL_PRINTHOOD */
969 CSIDL_Type_User,
970 PrintHoodW,
971 MAKEINTRESOURCEW(IDS_PRINTHOOD)
973 { /* 0x1c - CSIDL_LOCAL_APPDATA */
974 CSIDL_Type_User,
975 Local_AppDataW,
976 MAKEINTRESOURCEW(IDS_LOCAL_APPDATA)
978 { /* 0x1d - CSIDL_ALTSTARTUP */
979 CSIDL_Type_NonExistent,
980 NULL,
981 NULL
983 { /* 0x1e - CSIDL_COMMON_ALTSTARTUP */
984 CSIDL_Type_NonExistent,
985 NULL,
986 NULL
988 { /* 0x1f - CSIDL_COMMON_FAVORITES */
989 CSIDL_Type_AllUsers,
990 FavoritesW,
991 MAKEINTRESOURCEW(IDS_FAVORITES)
993 { /* 0x20 - CSIDL_INTERNET_CACHE */
994 CSIDL_Type_User,
995 CacheW,
996 MAKEINTRESOURCEW(IDS_INTERNET_CACHE)
998 { /* 0x21 - CSIDL_COOKIES */
999 CSIDL_Type_User,
1000 CookiesW,
1001 MAKEINTRESOURCEW(IDS_COOKIES)
1003 { /* 0x22 - CSIDL_HISTORY */
1004 CSIDL_Type_User,
1005 HistoryW,
1006 MAKEINTRESOURCEW(IDS_HISTORY)
1008 { /* 0x23 - CSIDL_COMMON_APPDATA */
1009 CSIDL_Type_AllUsers,
1010 Common_AppDataW,
1011 MAKEINTRESOURCEW(IDS_APPDATA)
1013 { /* 0x24 - CSIDL_WINDOWS */
1014 CSIDL_Type_WindowsPath,
1015 NULL,
1016 NULL
1018 { /* 0x25 - CSIDL_SYSTEM */
1019 CSIDL_Type_SystemPath,
1020 NULL,
1021 NULL
1023 { /* 0x26 - CSIDL_PROGRAM_FILES */
1024 CSIDL_Type_CurrVer,
1025 ProgramFilesDirW,
1026 MAKEINTRESOURCEW(IDS_PROGRAM_FILES)
1028 { /* 0x27 - CSIDL_MYPICTURES */
1029 CSIDL_Type_User,
1030 My_PicturesW,
1031 MAKEINTRESOURCEW(IDS_MYPICTURES)
1033 { /* 0x28 - CSIDL_PROFILE */
1034 CSIDL_Type_User,
1035 NULL,
1036 NULL
1038 { /* 0x29 - CSIDL_SYSTEMX86 */
1039 CSIDL_Type_NonExistent,
1040 NULL,
1041 NULL
1043 { /* 0x2a - CSIDL_PROGRAM_FILESX86 */
1044 CSIDL_Type_NonExistent,
1045 NULL,
1046 NULL
1048 { /* 0x2b - CSIDL_PROGRAM_FILES_COMMON */
1049 CSIDL_Type_CurrVer,
1050 CommonFilesDirW,
1051 MAKEINTRESOURCEW(IDS_PROGRAM_FILES_COMMON)
1053 { /* 0x2c - CSIDL_PROGRAM_FILES_COMMONX86 */
1054 CSIDL_Type_NonExistent,
1055 NULL,
1056 NULL
1058 { /* 0x2d - CSIDL_COMMON_TEMPLATES */
1059 CSIDL_Type_AllUsers,
1060 Common_TemplatesW,
1061 MAKEINTRESOURCEW(IDS_TEMPLATES)
1063 { /* 0x2e - CSIDL_COMMON_DOCUMENTS */
1064 CSIDL_Type_AllUsers,
1065 Common_DocumentsW,
1066 MAKEINTRESOURCEW(IDS_COMMON_DOCUMENTS)
1068 { /* 0x2f - CSIDL_COMMON_ADMINTOOLS */
1069 CSIDL_Type_AllUsers,
1070 Common_Administrative_ToolsW,
1071 MAKEINTRESOURCEW(IDS_ADMINTOOLS)
1073 { /* 0x30 - CSIDL_ADMINTOOLS */
1074 CSIDL_Type_User,
1075 Administrative_ToolsW,
1076 MAKEINTRESOURCEW(IDS_ADMINTOOLS)
1078 { /* 0x31 - CSIDL_CONNECTIONS */
1079 CSIDL_Type_Disallowed,
1080 NULL,
1081 NULL
1083 { /* 0x32 - unassigned */
1084 CSIDL_Type_Disallowed,
1085 NULL,
1086 NULL
1088 { /* 0x33 - unassigned */
1089 CSIDL_Type_Disallowed,
1090 NULL,
1091 NULL
1093 { /* 0x34 - unassigned */
1094 CSIDL_Type_Disallowed,
1095 NULL,
1096 NULL
1098 { /* 0x35 - CSIDL_COMMON_MUSIC */
1099 CSIDL_Type_AllUsers,
1100 CommonMusicW,
1101 MAKEINTRESOURCEW(IDS_COMMON_MUSIC)
1103 { /* 0x36 - CSIDL_COMMON_PICTURES */
1104 CSIDL_Type_AllUsers,
1105 CommonPicturesW,
1106 MAKEINTRESOURCEW(IDS_COMMON_PICTURES)
1108 { /* 0x37 - CSIDL_COMMON_VIDEO */
1109 CSIDL_Type_AllUsers,
1110 CommonVideoW,
1111 MAKEINTRESOURCEW(IDS_COMMON_VIDEO)
1113 { /* 0x38 - CSIDL_RESOURCES */
1114 CSIDL_Type_WindowsPath,
1115 NULL,
1116 ResourcesW
1118 { /* 0x39 - CSIDL_RESOURCES_LOCALIZED */
1119 CSIDL_Type_NonExistent,
1120 NULL,
1121 NULL
1123 { /* 0x3a - CSIDL_COMMON_OEM_LINKS */
1124 CSIDL_Type_NonExistent,
1125 NULL,
1126 NULL
1128 { /* 0x3b - CSIDL_CDBURN_AREA */
1129 CSIDL_Type_User,
1130 CD_BurningW,
1131 MAKEINTRESOURCEW(IDS_CDBURN_AREA)
1133 { /* 0x3c unassigned */
1134 CSIDL_Type_Disallowed,
1135 NULL,
1136 NULL
1138 { /* 0x3d - CSIDL_COMPUTERSNEARME */
1139 CSIDL_Type_Disallowed, /* FIXME */
1140 NULL,
1141 NULL
1143 { /* 0x3e - CSIDL_PROFILES */
1144 CSIDL_Type_Disallowed, /* oddly, this matches WinXP */
1145 NULL,
1146 NULL
1150 static HRESULT _SHExpandEnvironmentStrings(LPCWSTR szSrc, LPWSTR szDest);
1152 /* Gets the value named value from the registry key
1153 * rootKey\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
1154 * (or from rootKey\userPrefix\... if userPrefix is not NULL) into path, which
1155 * is assumed to be MAX_PATH WCHARs in length.
1156 * If it exists, expands the value and writes the expanded value to
1157 * rootKey\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders
1158 * Returns successful error code if the value was retrieved from the registry,
1159 * and a failure otherwise.
1161 static HRESULT _SHGetUserShellFolderPath(HKEY rootKey, LPCWSTR userPrefix,
1162 LPCWSTR value, LPWSTR path)
1164 HRESULT hr;
1165 WCHAR shellFolderPath[MAX_PATH], userShellFolderPath[MAX_PATH];
1166 LPCWSTR pShellFolderPath, pUserShellFolderPath;
1167 DWORD dwDisp, dwType, dwPathLen = MAX_PATH;
1168 HKEY userShellFolderKey, shellFolderKey;
1170 TRACE("%p,%s,%s,%p\n",rootKey, debugstr_w(userPrefix), debugstr_w(value),
1171 path);
1173 if (userPrefix)
1175 strcpyW(shellFolderPath, userPrefix);
1176 PathAddBackslashW(shellFolderPath);
1177 strcatW(shellFolderPath, szSHFolders);
1178 pShellFolderPath = shellFolderPath;
1179 strcpyW(userShellFolderPath, userPrefix);
1180 PathAddBackslashW(userShellFolderPath);
1181 strcatW(userShellFolderPath, szSHUserFolders);
1182 pUserShellFolderPath = userShellFolderPath;
1184 else
1186 pUserShellFolderPath = szSHUserFolders;
1187 pShellFolderPath = szSHFolders;
1190 if (RegCreateKeyExW(rootKey, pShellFolderPath, 0, NULL, 0, KEY_ALL_ACCESS,
1191 NULL, &shellFolderKey, &dwDisp))
1193 TRACE("Failed to create %s\n", debugstr_w(pShellFolderPath));
1194 return E_FAIL;
1196 if (RegCreateKeyExW(rootKey, pUserShellFolderPath, 0, NULL, 0,
1197 KEY_ALL_ACCESS, NULL, &userShellFolderKey, &dwDisp))
1199 TRACE("Failed to create %s\n",
1200 debugstr_w(pUserShellFolderPath));
1201 RegCloseKey(shellFolderKey);
1202 return E_FAIL;
1205 if (!RegQueryValueExW(userShellFolderKey, value, NULL, &dwType,
1206 (LPBYTE)path, &dwPathLen) && (dwType == REG_EXPAND_SZ || dwType == REG_SZ))
1208 LONG ret;
1210 path[dwPathLen / sizeof(WCHAR)] = '\0';
1211 if (dwType == REG_EXPAND_SZ && path[0] == '%')
1213 WCHAR szTemp[MAX_PATH];
1215 _SHExpandEnvironmentStrings(path, szTemp);
1216 lstrcpynW(path, szTemp, MAX_PATH);
1218 ret = RegSetValueExW(shellFolderKey, value, 0, REG_SZ, (LPBYTE)path,
1219 (strlenW(path) + 1) * sizeof(WCHAR));
1220 if (ret != ERROR_SUCCESS)
1221 hr = HRESULT_FROM_WIN32(ret);
1222 else
1223 hr = S_OK;
1225 else
1226 hr = E_FAIL;
1227 RegCloseKey(shellFolderKey);
1228 RegCloseKey(userShellFolderKey);
1229 TRACE("returning 0x%08x\n", hr);
1230 return hr;
1233 /* CrossOver HACK: Load an English string to work around restoring bottles
1234 * with non-US-ASCII characters, which doesn't work when the locale has
1235 * changed */
1236 static inline INT LoadStringW_English( HINSTANCE instance, UINT resource_id,
1237 LPWSTR buffer, INT buflen )
1239 INT ret;
1240 LCID lcid = GetThreadLocale();
1241 SetThreadLocale(MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_DEFAULT),SORT_DEFAULT));
1242 ret = LoadStringW(instance, resource_id, buffer, buflen);
1243 SetThreadLocale(lcid);
1244 return ret;
1248 /* Gets a 'semi-expanded' default value of the CSIDL with index folder into
1249 * pszPath, based on the entries in CSIDL_Data. By semi-expanded, I mean:
1250 * - The entry's szDefaultPath may be either a string value or an integer
1251 * resource identifier. In the latter case, the string value of the resource
1252 * is written.
1253 * - Depending on the entry's type, the path may begin with an (unexpanded)
1254 * environment variable name. The caller is responsible for expanding
1255 * environment strings if so desired.
1256 * The types that are prepended with environment variables are:
1257 * CSIDL_Type_User: %USERPROFILE%
1258 * CSIDL_Type_AllUsers: %ALLUSERSPROFILE%
1259 * CSIDL_Type_CurrVer: %SystemDrive%
1260 * (Others might make sense too, but as yet are unneeded.)
1262 static HRESULT _SHGetDefaultValue(BYTE folder, LPWSTR pszPath)
1264 HRESULT hr;
1265 WCHAR resourcePath[MAX_PATH];
1266 LPCWSTR pDefaultPath = NULL;
1268 TRACE("0x%02x,%p\n", folder, pszPath);
1270 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1271 return E_INVALIDARG;
1272 if (!pszPath)
1273 return E_INVALIDARG;
1275 if (CSIDL_Data[folder].szDefaultPath &&
1276 IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath))
1278 if (LoadStringW_English(shell32_hInstance,
1279 LOWORD(CSIDL_Data[folder].szDefaultPath), resourcePath, MAX_PATH))
1281 hr = S_OK;
1282 pDefaultPath = resourcePath;
1284 else
1286 FIXME("(%d,%s), LoadString failed, missing translation?\n", folder,
1287 debugstr_w(pszPath));
1288 hr = E_FAIL;
1291 else
1293 hr = S_OK;
1294 pDefaultPath = CSIDL_Data[folder].szDefaultPath;
1296 if (SUCCEEDED(hr))
1298 switch (CSIDL_Data[folder].type)
1300 case CSIDL_Type_User:
1301 strcpyW(pszPath, UserProfileW);
1302 break;
1303 case CSIDL_Type_AllUsers:
1304 strcpyW(pszPath, AllUsersProfileW);
1305 break;
1306 case CSIDL_Type_CurrVer:
1307 strcpyW(pszPath, SystemDriveW);
1308 break;
1309 default:
1310 ; /* no corresponding env. var, do nothing */
1312 if (pDefaultPath)
1314 PathAddBackslashW(pszPath);
1315 strcatW(pszPath, pDefaultPath);
1318 TRACE("returning 0x%08x\n", hr);
1319 return hr;
1322 /* Gets the (unexpanded) value of the folder with index folder into pszPath.
1323 * The folder's type is assumed to be CSIDL_Type_CurrVer. Its default value
1324 * can be overridden in the HKLM\\szCurrentVersion key.
1325 * If dwFlags has SHGFP_TYPE_DEFAULT set or if the value isn't overridden in
1326 * the registry, uses _SHGetDefaultValue to get the value.
1328 static HRESULT _SHGetCurrentVersionPath(DWORD dwFlags, BYTE folder,
1329 LPWSTR pszPath)
1331 HRESULT hr;
1333 TRACE("0x%08x,0x%02x,%p\n", dwFlags, folder, pszPath);
1335 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1336 return E_INVALIDARG;
1337 if (CSIDL_Data[folder].type != CSIDL_Type_CurrVer)
1338 return E_INVALIDARG;
1339 if (!pszPath)
1340 return E_INVALIDARG;
1342 if (dwFlags & SHGFP_TYPE_DEFAULT)
1343 hr = _SHGetDefaultValue(folder, pszPath);
1344 else
1346 HKEY hKey;
1347 DWORD dwDisp;
1349 if (RegCreateKeyExW(HKEY_LOCAL_MACHINE, szCurrentVersion, 0,
1350 NULL, 0, KEY_ALL_ACCESS, NULL, &hKey, &dwDisp))
1351 hr = E_FAIL;
1352 else
1354 DWORD dwType, dwPathLen = MAX_PATH * sizeof(WCHAR);
1356 if (RegQueryValueExW(hKey, CSIDL_Data[folder].szValueName, NULL,
1357 &dwType, (LPBYTE)pszPath, &dwPathLen) ||
1358 (dwType != REG_SZ && dwType != REG_EXPAND_SZ))
1360 hr = _SHGetDefaultValue(folder, pszPath);
1361 dwType = REG_EXPAND_SZ;
1362 RegSetValueExW(hKey, CSIDL_Data[folder].szValueName, 0, dwType,
1363 (LPBYTE)pszPath, (strlenW(pszPath)+1)*sizeof(WCHAR));
1365 else
1367 pszPath[dwPathLen / sizeof(WCHAR)] = '\0';
1368 hr = S_OK;
1370 RegCloseKey(hKey);
1373 TRACE("returning 0x%08x (output path is %s)\n", hr, debugstr_w(pszPath));
1374 return hr;
1377 /* Gets the user's path (unexpanded) for the CSIDL with index folder:
1378 * If SHGFP_TYPE_DEFAULT is set, calls _SHGetDefaultValue for it. Otherwise
1379 * calls _SHGetUserShellFolderPath for it. Where it looks depends on hToken:
1380 * - if hToken is -1, looks in HKEY_USERS\.Default
1381 * - otherwise looks first in HKEY_CURRENT_USER, followed by HKEY_LOCAL_MACHINE
1382 * if HKEY_CURRENT_USER doesn't contain any entries. If both fail, finally
1383 * calls _SHGetDefaultValue for it.
1385 static HRESULT _SHGetUserProfilePath(HANDLE hToken, DWORD dwFlags, BYTE folder,
1386 LPWSTR pszPath)
1388 HRESULT hr;
1390 TRACE("%p,0x%08x,0x%02x,%p\n", hToken, dwFlags, folder, pszPath);
1392 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1393 return E_INVALIDARG;
1394 if (CSIDL_Data[folder].type != CSIDL_Type_User)
1395 return E_INVALIDARG;
1396 if (!pszPath)
1397 return E_INVALIDARG;
1399 /* Only the current user and the default user are supported right now
1400 * I'm afraid.
1401 * FIXME: should be able to call GetTokenInformation on the token,
1402 * then call ConvertSidToStringSidW on it to get the user prefix.
1403 * But Wine's registry doesn't store user info by sid, it stores it
1404 * by user name (and I don't know how to convert from a token to a
1405 * user name).
1407 if (hToken != NULL && hToken != (HANDLE)-1)
1409 FIXME("unsupported for user other than current or default\n");
1410 return E_FAIL;
1413 if (dwFlags & SHGFP_TYPE_DEFAULT)
1414 hr = _SHGetDefaultValue(folder, pszPath);
1415 else
1417 LPCWSTR userPrefix = NULL;
1418 HKEY hRootKey;
1420 if (hToken == (HANDLE)-1)
1422 hRootKey = HKEY_USERS;
1423 userPrefix = DefaultW;
1425 else /* hToken == NULL, other values disallowed above */
1426 hRootKey = HKEY_CURRENT_USER;
1427 hr = _SHGetUserShellFolderPath(hRootKey, userPrefix,
1428 CSIDL_Data[folder].szValueName, pszPath);
1429 if (FAILED(hr) && hRootKey != HKEY_LOCAL_MACHINE)
1430 hr = _SHGetUserShellFolderPath(HKEY_LOCAL_MACHINE, NULL,
1431 CSIDL_Data[folder].szValueName, pszPath);
1432 if (FAILED(hr))
1433 hr = _SHGetDefaultValue(folder, pszPath);
1435 TRACE("returning 0x%08x (output path is %s)\n", hr, debugstr_w(pszPath));
1436 return hr;
1439 /* Gets the (unexpanded) path for the CSIDL with index folder. If dwFlags has
1440 * SHGFP_TYPE_DEFAULT set, calls _SHGetDefaultValue. Otherwise calls
1441 * _SHGetUserShellFolderPath for it, looking only in HKEY_LOCAL_MACHINE.
1442 * If this fails, falls back to _SHGetDefaultValue.
1444 static HRESULT _SHGetAllUsersProfilePath(DWORD dwFlags, BYTE folder,
1445 LPWSTR pszPath)
1447 HRESULT hr;
1449 TRACE("0x%08x,0x%02x,%p\n", dwFlags, folder, pszPath);
1451 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1452 return E_INVALIDARG;
1453 if (CSIDL_Data[folder].type != CSIDL_Type_AllUsers)
1454 return E_INVALIDARG;
1455 if (!pszPath)
1456 return E_INVALIDARG;
1458 if (dwFlags & SHGFP_TYPE_DEFAULT)
1459 hr = _SHGetDefaultValue(folder, pszPath);
1460 else
1462 hr = _SHGetUserShellFolderPath(HKEY_LOCAL_MACHINE, NULL,
1463 CSIDL_Data[folder].szValueName, pszPath);
1464 if (FAILED(hr))
1465 hr = _SHGetDefaultValue(folder, pszPath);
1467 TRACE("returning 0x%08x (output path is %s)\n", hr, debugstr_w(pszPath));
1468 return hr;
1471 static HRESULT _SHOpenProfilesKey(PHKEY pKey)
1473 LONG lRet;
1474 DWORD disp;
1476 lRet = RegCreateKeyExW(HKEY_LOCAL_MACHINE, ProfileListW, 0, NULL, 0,
1477 KEY_ALL_ACCESS, NULL, pKey, &disp);
1478 return HRESULT_FROM_WIN32(lRet);
1481 /* Reads the value named szValueName from the key profilesKey (assumed to be
1482 * opened by _SHOpenProfilesKey) into szValue, which is assumed to be MAX_PATH
1483 * WCHARs in length. If it doesn't exist, returns szDefault (and saves
1484 * szDefault to the registry).
1486 static HRESULT _SHGetProfilesValue(HKEY profilesKey, LPCWSTR szValueName,
1487 LPWSTR szValue, LPCWSTR szDefault)
1489 HRESULT hr;
1490 DWORD type, dwPathLen = MAX_PATH * sizeof(WCHAR);
1491 LONG lRet;
1493 TRACE("%p,%s,%p,%s\n", profilesKey, debugstr_w(szValueName), szValue,
1494 debugstr_w(szDefault));
1495 lRet = RegQueryValueExW(profilesKey, szValueName, NULL, &type,
1496 (LPBYTE)szValue, &dwPathLen);
1497 if (!lRet && (type == REG_SZ || type == REG_EXPAND_SZ) && dwPathLen
1498 && *szValue)
1500 dwPathLen /= sizeof(WCHAR);
1501 szValue[dwPathLen] = '\0';
1502 hr = S_OK;
1504 else
1506 /* Missing or invalid value, set a default */
1507 lstrcpynW(szValue, szDefault, MAX_PATH);
1508 TRACE("Setting missing value %s to %s\n", debugstr_w(szValueName),
1509 debugstr_w(szValue));
1510 lRet = RegSetValueExW(profilesKey, szValueName, 0, REG_EXPAND_SZ,
1511 (LPBYTE)szValue,
1512 (strlenW(szValue) + 1) * sizeof(WCHAR));
1513 if (lRet)
1514 hr = HRESULT_FROM_WIN32(lRet);
1515 else
1516 hr = S_OK;
1518 TRACE("returning 0x%08x (output value is %s)\n", hr, debugstr_w(szValue));
1519 return hr;
1522 /* Attempts to expand environment variables from szSrc into szDest, which is
1523 * assumed to be MAX_PATH characters in length. Before referring to the
1524 * environment, handles a few variables directly, because the environment
1525 * variables may not be set when this is called (as during Wine's installation
1526 * when default values are being written to the registry).
1527 * The directly handled environment variables, and their source, are:
1528 * - ALLUSERSPROFILE, USERPROFILE: reads from the registry
1529 * - SystemDrive: uses GetSystemDirectoryW and uses the drive portion of its
1530 * path
1531 * If one of the directly handled environment variables is expanded, only
1532 * expands a single variable, and only in the beginning of szSrc.
1534 static HRESULT _SHExpandEnvironmentStrings(LPCWSTR szSrc, LPWSTR szDest)
1536 HRESULT hr;
1537 WCHAR szTemp[MAX_PATH], szProfilesPrefix[MAX_PATH] = { 0 };
1538 HKEY key = NULL;
1540 TRACE("%s, %p\n", debugstr_w(szSrc), szDest);
1542 if (!szSrc || !szDest) return E_INVALIDARG;
1544 /* short-circuit if there's nothing to expand */
1545 if (szSrc[0] != '%')
1547 strcpyW(szDest, szSrc);
1548 hr = S_OK;
1549 goto end;
1551 /* Get the profile prefix, we'll probably be needing it */
1552 hr = _SHOpenProfilesKey(&key);
1553 if (SUCCEEDED(hr))
1555 WCHAR szDefaultProfilesPrefix[MAX_PATH];
1557 GetWindowsDirectoryW(szDefaultProfilesPrefix, MAX_PATH);
1558 PathAddBackslashW(szDefaultProfilesPrefix);
1559 PathAppendW(szDefaultProfilesPrefix, szDefaultProfileDirW);
1560 hr = _SHGetProfilesValue(key, ProfilesDirectoryW, szProfilesPrefix,
1561 szDefaultProfilesPrefix);
1564 *szDest = 0;
1565 strcpyW(szTemp, szSrc);
1566 while (SUCCEEDED(hr) && szTemp[0] == '%')
1568 if (!strncmpiW(szTemp, AllUsersProfileW, strlenW(AllUsersProfileW)))
1570 WCHAR szAllUsers[MAX_PATH];
1572 strcpyW(szDest, szProfilesPrefix);
1573 hr = _SHGetProfilesValue(key, AllUsersProfileValueW,
1574 szAllUsers, AllUsersW);
1575 PathAppendW(szDest, szAllUsers);
1576 PathAppendW(szDest, szTemp + strlenW(AllUsersProfileW));
1578 else if (!strncmpiW(szTemp, UserProfileW, strlenW(UserProfileW)))
1580 static const WCHAR userName[] = {'c','r','o','s','s','o','v','e','r',0};
1582 strcpyW(szDest, szProfilesPrefix);
1583 PathAppendW(szDest, userName);
1584 PathAppendW(szDest, szTemp + strlenW(UserProfileW));
1586 else if (!strncmpiW(szTemp, SystemDriveW, strlenW(SystemDriveW)))
1588 GetSystemDirectoryW(szDest, MAX_PATH);
1589 if (szDest[1] != ':')
1591 FIXME("non-drive system paths unsupported\n");
1592 hr = E_FAIL;
1594 else
1596 strcpyW(szDest + 3, szTemp + strlenW(SystemDriveW) + 1);
1597 hr = S_OK;
1600 else
1602 DWORD ret = ExpandEnvironmentStringsW(szSrc, szDest, MAX_PATH);
1604 if (ret > MAX_PATH)
1605 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
1606 else if (ret == 0)
1607 hr = HRESULT_FROM_WIN32(GetLastError());
1608 else
1609 hr = S_OK;
1611 if (SUCCEEDED(hr) && szDest[0] == '%')
1612 strcpyW(szTemp, szDest);
1613 else
1615 /* terminate loop */
1616 szTemp[0] = '\0';
1619 end:
1620 if (key)
1621 RegCloseKey(key);
1622 TRACE("returning 0x%08x (input was %s, output is %s)\n", hr,
1623 debugstr_w(szSrc), debugstr_w(szDest));
1624 return hr;
1627 /*************************************************************************
1628 * SHGetFolderPathW [SHELL32.@]
1630 * Convert nFolder to path.
1632 * RETURNS
1633 * Success: S_OK
1634 * Failure: standard HRESULT error codes.
1636 * NOTES
1637 * Most values can be overridden in either
1638 * HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
1639 * or in the same location in HKLM.
1640 * The registry usage is explained by the following tech note:
1641 * http://www.microsoft.com/windows2000/techinfo/reskit/en-us/default.asp?url=/windows2000/techinfo/reskit/en-us/regentry/36173.asp
1642 * The "Shell Folders" registry key was used in NT4 and earlier systems.
1643 * Beginning with Windows 2000, the "User Shell Folders" key is used, so
1644 * changes made to it are made to the former key too. This synchronization is
1645 * done on-demand: not until someone requests the value of one of these paths
1646 * (by calling one of the SHGet functions) is the value synchronized.
1647 * Furthermore, as explained here:
1648 * http://www.microsoft.com/windows2000/techinfo/reskit/en-us/default.asp?url=/windows2000/techinfo/reskit/en-us/regentry/36276.asp
1649 * the HKCU paths take precedence over the HKLM paths.
1652 HRESULT WINAPI SHGetFolderPathW(
1653 HWND hwndOwner, /* [I] owner window */
1654 int nFolder, /* [I] CSIDL identifying the folder */
1655 HANDLE hToken, /* [I] access token */
1656 DWORD dwFlags, /* [I] which path to return */
1657 LPWSTR pszPath) /* [O] converted path */
1659 HRESULT hr;
1660 WCHAR szBuildPath[MAX_PATH], szTemp[MAX_PATH];
1661 DWORD folder = nFolder & CSIDL_FOLDER_MASK;
1662 CSIDL_Type type;
1663 int ret;
1665 TRACE("%p,%p,nFolder=0x%04x\n", hwndOwner,pszPath,nFolder);
1667 /* Windows always NULL-terminates the resulting path regardless of success
1668 * or failure, so do so first
1670 if (pszPath)
1671 *pszPath = '\0';
1672 if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1673 return E_INVALIDARG;
1674 szTemp[0] = 0;
1675 type = CSIDL_Data[folder].type;
1676 switch (type)
1678 case CSIDL_Type_Disallowed:
1679 hr = E_INVALIDARG;
1680 break;
1681 case CSIDL_Type_NonExistent:
1682 hr = S_FALSE;
1683 break;
1684 case CSIDL_Type_WindowsPath:
1685 GetWindowsDirectoryW(szTemp, MAX_PATH);
1686 if (CSIDL_Data[folder].szDefaultPath &&
1687 !IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath) &&
1688 *CSIDL_Data[folder].szDefaultPath)
1690 PathAddBackslashW(szTemp);
1691 strcatW(szTemp, CSIDL_Data[folder].szDefaultPath);
1693 hr = S_OK;
1694 break;
1695 case CSIDL_Type_SystemPath:
1696 GetSystemDirectoryW(szTemp, MAX_PATH);
1697 if (CSIDL_Data[folder].szDefaultPath &&
1698 !IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath) &&
1699 *CSIDL_Data[folder].szDefaultPath)
1701 PathAddBackslashW(szTemp);
1702 strcatW(szTemp, CSIDL_Data[folder].szDefaultPath);
1704 hr = S_OK;
1705 break;
1706 case CSIDL_Type_CurrVer:
1707 hr = _SHGetCurrentVersionPath(dwFlags, folder, szTemp);
1708 break;
1709 case CSIDL_Type_User:
1710 hr = _SHGetUserProfilePath(hToken, dwFlags, folder, szTemp);
1711 break;
1712 case CSIDL_Type_AllUsers:
1713 hr = _SHGetAllUsersProfilePath(dwFlags, folder, szTemp);
1714 break;
1715 default:
1716 FIXME("bogus type %d, please fix\n", type);
1717 hr = E_INVALIDARG;
1718 break;
1721 /* Expand environment strings if necessary */
1722 if (*szTemp == '%')
1723 hr = _SHExpandEnvironmentStrings(szTemp, szBuildPath);
1724 else
1725 strcpyW(szBuildPath, szTemp);
1726 /* Copy the path if it's available before we might return */
1727 if (SUCCEEDED(hr) && pszPath)
1728 strcpyW(pszPath, szBuildPath);
1730 if (FAILED(hr)) goto end;
1732 /* if we don't care about existing directories we are ready */
1733 if(nFolder & CSIDL_FLAG_DONT_VERIFY) goto end;
1735 if (PathFileExistsW(szBuildPath)) goto end;
1737 /* not existing but we are not allowed to create it. The return value
1738 * is verified against shell32 version 6.0.
1740 if (!(nFolder & CSIDL_FLAG_CREATE))
1742 hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
1743 goto end;
1746 /* create directory/directories */
1747 ret = SHCreateDirectoryExW(hwndOwner, szBuildPath, NULL);
1748 if (ret && ret != ERROR_ALREADY_EXISTS)
1750 ERR("Failed to create directory %s.\n", debugstr_w(szBuildPath));
1751 hr = E_FAIL;
1752 goto end;
1755 TRACE("Created missing system directory %s\n", debugstr_w(szBuildPath));
1756 end:
1757 TRACE("returning 0x%08x (final path is %s)\n", hr, debugstr_w(szBuildPath));
1758 return hr;
1761 /*************************************************************************
1762 * SHGetFolderPathA [SHELL32.@]
1764 * See SHGetFolderPathW.
1766 HRESULT WINAPI SHGetFolderPathA(
1767 HWND hwndOwner,
1768 int nFolder,
1769 HANDLE hToken,
1770 DWORD dwFlags,
1771 LPSTR pszPath)
1773 WCHAR szTemp[MAX_PATH];
1774 HRESULT hr;
1776 TRACE("%p,%p,nFolder=0x%04x\n",hwndOwner,pszPath,nFolder);
1778 if (pszPath)
1779 *pszPath = '\0';
1780 hr = SHGetFolderPathW(hwndOwner, nFolder, hToken, dwFlags, szTemp);
1781 if (SUCCEEDED(hr) && pszPath)
1782 WideCharToMultiByte(CP_ACP, 0, szTemp, -1, pszPath, MAX_PATH, NULL,
1783 NULL);
1785 return hr;
1788 /* For each folder in folders, if its value has not been set in the registry,
1789 * calls _SHGetUserProfilePath or _SHGetAllUsersProfilePath (depending on the
1790 * folder's type) to get the unexpanded value first.
1791 * Writes the unexpanded value to User Shell Folders, and queries it with
1792 * SHGetFolderPathW to force the creation of the directory if it doesn't
1793 * already exist. SHGetFolderPathW also returns the expanded value, which
1794 * this then writes to Shell Folders.
1796 static HRESULT _SHRegisterFolders(HKEY hRootKey, HANDLE hToken,
1797 LPCWSTR szUserShellFolderPath, LPCWSTR szShellFolderPath, const UINT folders[],
1798 UINT foldersLen)
1800 UINT i;
1801 WCHAR path[MAX_PATH];
1802 HRESULT hr = S_OK;
1803 HKEY hUserKey = NULL, hKey = NULL;
1804 DWORD dwDisp, dwType, dwPathLen;
1805 LONG ret;
1807 TRACE("%p,%p,%s,%p,%u\n", hRootKey, hToken,
1808 debugstr_w(szUserShellFolderPath), folders, foldersLen);
1810 ret = RegCreateKeyExW(hRootKey, szUserShellFolderPath, 0, NULL, 0,
1811 KEY_ALL_ACCESS, NULL, &hUserKey, &dwDisp);
1812 if (ret)
1813 hr = HRESULT_FROM_WIN32(ret);
1814 else
1816 ret = RegCreateKeyExW(hRootKey, szShellFolderPath, 0, NULL, 0,
1817 KEY_ALL_ACCESS, NULL, &hKey, &dwDisp);
1818 if (ret)
1819 hr = HRESULT_FROM_WIN32(ret);
1821 for (i = 0; SUCCEEDED(hr) && i < foldersLen; i++)
1823 dwPathLen = MAX_PATH * sizeof(WCHAR);
1824 if (RegQueryValueExW(hUserKey, CSIDL_Data[folders[i]].szValueName, NULL,
1825 &dwType, (LPBYTE)path, &dwPathLen) || (dwType != REG_SZ &&
1826 dwType != REG_EXPAND_SZ))
1828 *path = '\0';
1829 if (CSIDL_Data[folders[i]].type == CSIDL_Type_User)
1830 _SHGetUserProfilePath(hToken, SHGFP_TYPE_DEFAULT, folders[i],
1831 path);
1832 else if (CSIDL_Data[folders[i]].type == CSIDL_Type_AllUsers)
1833 _SHGetAllUsersProfilePath(SHGFP_TYPE_DEFAULT, folders[i], path);
1834 else if (CSIDL_Data[folders[i]].type == CSIDL_Type_WindowsPath)
1835 GetWindowsDirectoryW(path, MAX_PATH);
1836 else
1837 hr = E_FAIL;
1838 if (*path)
1840 ret = RegSetValueExW(hUserKey,
1841 CSIDL_Data[folders[i]].szValueName, 0, REG_EXPAND_SZ,
1842 (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR));
1843 if (ret)
1844 hr = HRESULT_FROM_WIN32(ret);
1845 else
1847 hr = SHGetFolderPathW(NULL, folders[i] | CSIDL_FLAG_CREATE,
1848 hToken, SHGFP_TYPE_DEFAULT, path);
1849 ret = RegSetValueExW(hKey,
1850 CSIDL_Data[folders[i]].szValueName, 0, REG_SZ,
1851 (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR));
1852 if (ret)
1853 hr = HRESULT_FROM_WIN32(ret);
1858 if (hUserKey)
1859 RegCloseKey(hUserKey);
1860 if (hKey)
1861 RegCloseKey(hKey);
1863 TRACE("returning 0x%08x\n", hr);
1864 return hr;
1867 static HRESULT _SHRegisterUserShellFolders(BOOL bDefault)
1869 static const UINT folders[] = {
1870 CSIDL_PROGRAMS,
1871 CSIDL_PERSONAL,
1872 CSIDL_FAVORITES,
1873 CSIDL_APPDATA,
1874 CSIDL_STARTUP,
1875 CSIDL_RECENT,
1876 CSIDL_SENDTO,
1877 CSIDL_STARTMENU,
1878 CSIDL_MYMUSIC,
1879 CSIDL_MYVIDEO,
1880 CSIDL_DESKTOPDIRECTORY,
1881 CSIDL_NETHOOD,
1882 CSIDL_TEMPLATES,
1883 CSIDL_PRINTHOOD,
1884 CSIDL_LOCAL_APPDATA,
1885 CSIDL_INTERNET_CACHE,
1886 CSIDL_COOKIES,
1887 CSIDL_HISTORY,
1888 CSIDL_MYPICTURES,
1889 CSIDL_FONTS
1891 WCHAR userShellFolderPath[MAX_PATH], shellFolderPath[MAX_PATH];
1892 LPCWSTR pUserShellFolderPath, pShellFolderPath;
1893 HRESULT hr = S_OK;
1894 HKEY hRootKey;
1895 HANDLE hToken;
1897 TRACE("%s\n", bDefault ? "TRUE" : "FALSE");
1898 if (bDefault)
1900 hToken = (HANDLE)-1;
1901 hRootKey = HKEY_USERS;
1902 strcpyW(userShellFolderPath, DefaultW);
1903 PathAddBackslashW(userShellFolderPath);
1904 strcatW(userShellFolderPath, szSHUserFolders);
1905 pUserShellFolderPath = userShellFolderPath;
1906 strcpyW(shellFolderPath, DefaultW);
1907 PathAddBackslashW(shellFolderPath);
1908 strcatW(shellFolderPath, szSHFolders);
1909 pShellFolderPath = shellFolderPath;
1911 else
1913 hToken = NULL;
1914 hRootKey = HKEY_CURRENT_USER;
1915 pUserShellFolderPath = szSHUserFolders;
1916 pShellFolderPath = szSHFolders;
1919 hr = _SHRegisterFolders(hRootKey, hToken, pUserShellFolderPath,
1920 pShellFolderPath, folders, sizeof(folders) / sizeof(folders[0]));
1921 TRACE("returning 0x%08x\n", hr);
1922 return hr;
1925 static HRESULT _SHRegisterCommonShellFolders(void)
1927 static const UINT folders[] = {
1928 CSIDL_COMMON_STARTMENU,
1929 CSIDL_COMMON_PROGRAMS,
1930 CSIDL_COMMON_STARTUP,
1931 CSIDL_COMMON_DESKTOPDIRECTORY,
1932 CSIDL_COMMON_FAVORITES,
1933 CSIDL_COMMON_APPDATA,
1934 CSIDL_COMMON_TEMPLATES,
1935 CSIDL_COMMON_DOCUMENTS,
1937 HRESULT hr;
1939 TRACE("\n");
1940 hr = _SHRegisterFolders(HKEY_LOCAL_MACHINE, NULL, szSHUserFolders,
1941 szSHFolders, folders, sizeof(folders) / sizeof(folders[0]));
1942 TRACE("returning 0x%08x\n", hr);
1943 return hr;
1946 /******************************************************************************
1947 * _SHAppendToUnixPath [Internal]
1949 * Helper function for _SHCreateSymbolicLinks. Appends pwszSubPath (or the
1950 * corresponding resource, if IS_INTRESOURCE) to the unix base path 'szBasePath'
1951 * and replaces backslashes with slashes.
1953 * PARAMS
1954 * szBasePath [IO] The unix base path, which will be appended to (CP_UNXICP).
1955 * pwszSubPath [I] Sub-path or resource id (use MAKEINTRESOURCEW).
1957 * RETURNS
1958 * Success: TRUE,
1959 * Failure: FALSE
1961 static inline BOOL _SHAppendToUnixPath(char *szBasePath, LPCWSTR pwszSubPath, int localize) {
1962 WCHAR wszSubPath[MAX_PATH];
1963 int cLen = strlen(szBasePath);
1964 char *pBackslash;
1966 if (IS_INTRESOURCE(pwszSubPath)) {
1967 int rc;
1968 if (localize)
1969 rc=LoadStringW(shell32_hInstance, LOWORD(pwszSubPath), wszSubPath, MAX_PATH);
1970 else
1971 rc=LoadStringW_English(shell32_hInstance, LOWORD(pwszSubPath), wszSubPath, MAX_PATH);
1972 if (!rc) {
1973 /* Fall back to hardcoded defaults. */
1974 WARN("Falling back to hardcoded defaults\n");
1975 switch (LOWORD(pwszSubPath)) {
1976 case IDS_PERSONAL:
1977 lstrcpyW(wszSubPath, PersonalW);
1978 break;
1979 case IDS_MYMUSIC:
1980 lstrcpyW(wszSubPath, My_MusicW);
1981 break;
1982 case IDS_MYPICTURES:
1983 lstrcpyW(wszSubPath, My_PicturesW);
1984 break;
1985 case IDS_MYVIDEO:
1986 lstrcpyW(wszSubPath, My_VideoW);
1987 break;
1988 default:
1989 ERR("LoadString(%d) failed!\n", LOWORD(pwszSubPath));
1990 return FALSE;
1993 } else {
1994 lstrcpyW(wszSubPath, pwszSubPath);
1997 if (szBasePath[cLen-1] != '/') szBasePath[cLen++] = '/';
1999 if (!WideCharToMultiByte(CP_UNIXCP, 0, wszSubPath, -1, szBasePath + cLen,
2000 FILENAME_MAX - cLen, NULL, NULL))
2002 return FALSE;
2005 pBackslash = szBasePath + cLen;
2006 while ((pBackslash = strchr(pBackslash, '\\'))) *pBackslash = '/';
2008 return TRUE;
2011 typedef struct
2013 UINT ids;
2014 int csidl;
2015 UINT unix_location[3];
2016 } folder_mapping_t;
2018 static const folder_mapping_t folder_mappings[]={
2019 { /* Must be the first in the list */
2020 IDS_PERSONAL,
2021 CSIDL_PERSONAL,
2022 { IDS_PERSONAL, /* 'My Documents' */
2023 IDS_COMMON_DOCUMENTS, /* 'Documents' (used on SUSE, Mandriva, ...) */
2027 { IDS_MYPICTURES,
2028 CSIDL_MYPICTURES,
2029 { IDS_MYPICTURES, /* 'My Pictures' */
2030 WINE_IDS_PICTURES, /* 'Pictures' (used on the Mac) */
2034 { IDS_MYVIDEO,
2035 CSIDL_MYVIDEO,
2036 { IDS_MYVIDEO, /* 'My Videos' */
2037 WINE_IDS_MOVIES, /* 'Movies' (used on the Mac) */
2041 { IDS_MYMUSIC,
2042 CSIDL_MYMUSIC,
2043 { IDS_MYMUSIC, /* 'My Music' */
2044 WINE_IDS_MUSIC, /* 'Music' (used on the Mac) */
2050 /******************************************************************************
2051 * _SHCreateSymbolicLinks [Internal]
2053 * Sets up symbol links for various shell folders to point into the users home
2054 * directory. We do an educated guess about what the user would probably want:
2055 * - If there is a 'My Documents' or 'Documents' directory in $HOME, the user
2056 * probably wants Wine's 'My Documents' to point there. Furthermore, we
2057 * assume that the user is a Windows lover and has no problem with Wine
2058 * creating 'My Pictures', 'My Music' and 'My Video' subfolders under
2059 * '$HOME/My Documents', if those do not already exits. We put appropriate
2060 * symbolic links in place for those too.
2061 * - If there is no 'My Documents' directory in $HOME, we let it point
2062 * directly to $HOME. We assume the user to be a Unix hacker who does not
2063 * want Wine to create anything anywhere besides the .wine directory. So, if
2064 * there already is a 'My Music' directory in $HOME, we symlink the
2065 * 'My Music' shell folder to it. But if not, we symlink it to $HOME
2066 * directly. The same holds for 'My Pictures' and 'My Video'.
2067 * - The Desktop shell folder is symlinked to '$HOME/Desktop', if that does
2068 * exists and left alone if not.
2069 * ('My Music', ... above in fact means LoadString(IDS_MYMUSIC))
2071 static void _SHCreateSymbolicLinks(void)
2073 WCHAR wszTempPath[MAX_PATH];
2074 char szPersonalTarget[FILENAME_MAX], *pszPersonal;
2075 char szMyStuffTarget[FILENAME_MAX], *pszMyStuff;
2076 # define szLinuxDesktop "/My Linux Desktop"
2077 # define szMacDesktop "/My Mac Desktop"
2078 # define szNativeDesktop "/My Native Desktop"
2079 static const char* szDesktops[] = {szLinuxDesktop,
2080 szMacDesktop,
2081 szNativeDesktop,
2082 NULL};
2083 const char* pszNativeDesktop;
2084 char *pszDesktopLink;
2085 char szDesktopTarget[FILENAME_MAX], *pszDesktop;
2086 const char *pszHome;
2087 struct stat statHome, statFolder;
2088 static const int MDL_DIRS = 1;
2089 static const int MDL_FLAT_LINKS = 2;
2090 static const int MDL_NESTED_LINKS = 3;
2091 int layout, localize, i, u;
2092 HRESULT hr;
2094 /* Create all necessary profile sub-dirs up to 'My Documents' and get the unix path. */
2095 hr = SHGetFolderPathW(NULL, CSIDL_PERSONAL|CSIDL_FLAG_CREATE, NULL,
2096 SHGFP_TYPE_CURRENT, wszTempPath);
2097 if (FAILED(hr)) return;
2098 pszPersonal = wine_get_unix_file_name(wszTempPath);
2099 if (!pszPersonal) return;
2100 /* SHGetFolderPathW() creates 'My Documents' as a directory which may
2101 * not be what we want. But we need to rmdir() the folder anyway so we
2102 * get another chance to symlink during upgrades in case the folder is
2103 * not used yet.
2105 rmdir(pszPersonal);
2107 pszHome = getenv("HOME");
2108 if (!pszHome || !strcmp(pszHome, "/") || stat(pszHome, &statHome) ||
2109 !S_ISDIR(statHome.st_mode) || statHome.st_uid != geteuid())
2111 /* $HOME does not exist or is otherwise unusable */
2112 pszHome=NULL;
2115 /* Create and / or figure out the 'My Documents' layout */
2116 localize=1;
2117 if (stat(pszPersonal, &statFolder) || !S_ISDIR(statFolder.st_mode) ||
2118 statFolder.st_uid != geteuid())
2120 /* Wine's 'My Documents' does not exist or is unusable.
2121 * Delete it in case it is a dead link, then recreate it.
2123 unlink(pszPersonal);
2125 if (!pszHome)
2127 /* $HOME does not exist so just create 'My Documents' as a
2128 * directory in the c: drive.
2130 layout=MDL_DIRS;
2131 mkdir(pszPersonal, S_IRWXU|S_IRWXG|S_IRWXO);
2133 else
2135 /* Try to find the Unix equivalent to 'My Documents' */
2136 layout=MDL_FLAT_LINKS;
2137 for (localize=1; localize >= 0; localize--)
2139 for (u=0; folder_mappings[0].unix_location[u] != 0; u++)
2141 strcpy(szPersonalTarget, pszHome);
2142 if (_SHAppendToUnixPath(szPersonalTarget, MAKEINTRESOURCEW(folder_mappings[0].unix_location[u]), localize) &&
2143 !stat(szPersonalTarget, &statFolder) &&
2144 S_ISDIR(statFolder.st_mode) &&
2145 statFolder.st_uid == geteuid())
2147 layout=MDL_NESTED_LINKS;
2148 symlink(szPersonalTarget, pszPersonal);
2149 break;
2152 if (layout != MDL_FLAT_LINKS)
2153 break;
2155 if (layout == MDL_FLAT_LINKS)
2157 strcpy(szPersonalTarget, pszHome);
2158 symlink(szPersonalTarget, pszPersonal);
2159 localize=1;
2163 else if (pszHome && statFolder.st_dev == statHome.st_dev &&
2164 statFolder.st_ino == statHome.st_ino)
2166 /* Wine's 'My Documents' is obviously a symbolic link to $HOME */
2167 strcpy(szPersonalTarget, pszHome);
2168 layout=MDL_FLAT_LINKS;
2170 else
2172 lstat(pszPersonal, &statFolder);
2173 if (S_ISLNK(statFolder.st_mode) &&
2174 readlink(pszPersonal, szPersonalTarget, sizeof(szPersonalTarget)) > 0)
2176 /* Wine's 'My Documents' is a symbolic link, presumably to a
2177 * $HOME subdirectory equivalent to 'My Documents'
2179 layout=MDL_NESTED_LINKS;
2181 else
2183 /* Wine's 'My Documents' is a simple directory */
2184 layout=MDL_DIRS;
2187 HeapFree(GetProcessHeap(), 0, pszPersonal);
2189 /* Create the 'My Stuff' folders now */
2190 for (i=1; i<sizeof(folder_mappings)/sizeof(folder_mappings[0]); i++)
2192 /* Create the current 'My Stuff' folder and get its Unix path. */
2193 hr = SHGetFolderPathW(NULL, folder_mappings[i].csidl|CSIDL_FLAG_CREATE, NULL,
2194 SHGFP_TYPE_CURRENT, wszTempPath);
2195 if (FAILED(hr)) continue;
2196 pszMyStuff = wine_get_unix_file_name(wszTempPath);
2197 if (!pszMyStuff) continue;
2198 /* See comment about rmdir(pszPersonal) above */
2199 rmdir(pszMyStuff);
2201 if (!stat(pszMyStuff, &statFolder) && S_ISDIR(statFolder.st_mode) &&
2202 statFolder.st_uid == geteuid())
2204 /* 'My Stuff' already exists, so skip it */
2205 HeapFree(GetProcessHeap(), 0, pszMyStuff);
2206 continue;
2208 /* See comment about unlink(pszPersonal) above */
2209 unlink(pszMyStuff);
2211 if (layout == MDL_DIRS)
2213 mkdir(pszMyStuff, S_IRWXU|S_IRWXG|S_IRWXO);
2215 else
2217 int l, u;
2218 for (l=1; l >= 0; l--)
2220 for (u=0; folder_mappings[i].unix_location[u] != 0; u++)
2222 strcpy(szMyStuffTarget, szPersonalTarget);
2223 if (_SHAppendToUnixPath(szMyStuffTarget, MAKEINTRESOURCEW(folder_mappings[i].unix_location[u]), l) &&
2224 !stat(szMyStuffTarget, &statFolder) &&
2225 S_ISDIR(statFolder.st_mode) &&
2226 statFolder.st_uid == geteuid())
2227 break;
2228 *szMyStuffTarget='\0';
2230 if (*szMyStuffTarget)
2231 break;
2233 if (*szMyStuffTarget)
2235 /* 'My Stuff' already exists on the Unix side, so link to it */
2236 symlink(szMyStuffTarget, pszMyStuff);
2238 else if (layout == MDL_FLAT_LINKS)
2240 /* 'My Stuff' does not exist on the Unix side, so just
2241 * link to $HOME.
2243 symlink(pszHome, pszMyStuff);
2245 else
2247 /* 'My Stuff' does not exist on the Unix side. So create it
2248 * with the 'best' localization as per the 'nested' layout.
2250 strcpy(szMyStuffTarget, szPersonalTarget);
2251 if (!_SHAppendToUnixPath(szMyStuffTarget, MAKEINTRESOURCEW(folder_mappings[i].ids), localize))
2253 /* This should really not happen */
2254 mkdir(pszMyStuff, S_IRWXU|S_IRWXG|S_IRWXO);
2256 else
2258 mkdir(szMyStuffTarget, S_IRWXU|S_IRWXG|S_IRWXO);
2259 symlink(szMyStuffTarget, pszMyStuff);
2263 HeapFree(GetProcessHeap(), 0, pszMyStuff);
2266 /* Last but not least, the Desktop folder */
2267 hr = SHGetFolderPathW(NULL, CSIDL_DESKTOPDIRECTORY|CSIDL_FLAG_CREATE,
2268 NULL, SHGFP_TYPE_CURRENT, wszTempPath);
2269 if (SUCCEEDED(hr) && (pszDesktop = wine_get_unix_file_name(wszTempPath)))
2271 #ifdef linux
2272 pszNativeDesktop=szLinuxDesktop;
2273 #elif defined(__APPLE__)
2274 pszNativeDesktop=szMacDesktop;
2275 #else
2276 pszNativeDesktop=szNativeDesktop;
2277 #endif
2278 for (i=0; szDesktops[i]; i++)
2280 pszDesktopLink = HeapAlloc(GetProcessHeap(), 0, strlen(pszDesktop) + strlen(szDesktops[i])+1);
2281 strcpy(pszDesktopLink, pszDesktop);
2282 strcat(pszDesktopLink, szDesktops[i]);
2283 rmdir(pszDesktopLink);
2284 if (stat(pszDesktopLink, &statFolder) ||
2285 !S_ISDIR(statFolder.st_mode) || statFolder.st_uid != geteuid())
2287 /* Delete the other platforms' links */
2288 unlink(pszDesktopLink);
2290 /* And create one for the current platform */
2291 if (strcmp(szDesktops[i], pszNativeDesktop) == 0 && pszHome)
2293 strcpy(szDesktopTarget, pszHome);
2294 if (_SHAppendToUnixPath(szDesktopTarget, DesktopW, 0) &&
2295 !stat(szDesktopTarget, &statFolder) &&
2296 S_ISDIR(statFolder.st_mode) &&
2297 statFolder.st_uid == geteuid())
2299 symlink(szDesktopTarget, pszDesktopLink);
2303 HeapFree(GetProcessHeap(), 0, pszDesktopLink);
2306 HeapFree(GetProcessHeap(), 0, pszDesktop);
2310 void WINAPI wine_update_symbolic_links(HWND hwnd, HINSTANCE handle, LPCWSTR cmdline, INT show)
2312 TRACE("\n");
2313 _SHCreateSymbolicLinks();
2316 /* Register the default values in the registry, as some apps seem to depend
2317 * on their presence. The set registered was taken from Windows XP.
2319 HRESULT SHELL_RegisterShellFolders(void)
2321 HRESULT hr;
2323 /* Set up '$HOME' targeted symlinks for 'My Documents', 'My Pictures',
2324 * 'My Video', 'My Music' and 'Desktop' in advance, so that the
2325 * _SHRegister*ShellFolders() functions will find everything nice and clean
2326 * and thus will not attempt to create them in the profile directory. */
2327 /* In CrossOver Desktop is not a symlink */
2328 _SHCreateSymbolicLinks();
2330 hr = _SHRegisterUserShellFolders(TRUE);
2331 if (SUCCEEDED(hr))
2332 hr = _SHRegisterUserShellFolders(FALSE);
2333 if (SUCCEEDED(hr))
2334 hr = _SHRegisterCommonShellFolders();
2335 return hr;
2338 /*************************************************************************
2339 * SHGetSpecialFolderPathA [SHELL32.@]
2341 BOOL WINAPI SHGetSpecialFolderPathA (
2342 HWND hwndOwner,
2343 LPSTR szPath,
2344 int nFolder,
2345 BOOL bCreate)
2347 return (SHGetFolderPathA(
2348 hwndOwner,
2349 nFolder + (bCreate ? CSIDL_FLAG_CREATE : 0),
2350 NULL,
2352 szPath)) == S_OK ? TRUE : FALSE;
2355 /*************************************************************************
2356 * SHGetSpecialFolderPathW
2358 BOOL WINAPI SHGetSpecialFolderPathW (
2359 HWND hwndOwner,
2360 LPWSTR szPath,
2361 int nFolder,
2362 BOOL bCreate)
2364 return (SHGetFolderPathW(
2365 hwndOwner,
2366 nFolder + (bCreate ? CSIDL_FLAG_CREATE : 0),
2367 NULL,
2369 szPath)) == S_OK ? TRUE : FALSE;
2372 /*************************************************************************
2373 * SHGetSpecialFolderPath (SHELL32.175)
2375 BOOL WINAPI SHGetSpecialFolderPathAW (
2376 HWND hwndOwner,
2377 LPVOID szPath,
2378 int nFolder,
2379 BOOL bCreate)
2382 if (SHELL_OsIsUnicode())
2383 return SHGetSpecialFolderPathW (hwndOwner, szPath, nFolder, bCreate);
2384 return SHGetSpecialFolderPathA (hwndOwner, szPath, nFolder, bCreate);
2387 /*************************************************************************
2388 * SHGetFolderLocation [SHELL32.@]
2390 * Gets the folder locations from the registry and creates a pidl.
2392 * PARAMS
2393 * hwndOwner [I]
2394 * nFolder [I] CSIDL_xxxxx
2395 * hToken [I] token representing user, or NULL for current user, or -1 for
2396 * default user
2397 * dwReserved [I] must be zero
2398 * ppidl [O] PIDL of a special folder
2400 * RETURNS
2401 * Success: S_OK
2402 * Failure: Standard OLE-defined error result, S_FALSE or E_INVALIDARG
2404 * NOTES
2405 * Creates missing reg keys and directories.
2406 * Mostly forwards to SHGetFolderPathW, but a few values of nFolder return
2407 * virtual folders that are handled here.
2409 HRESULT WINAPI SHGetFolderLocation(
2410 HWND hwndOwner,
2411 int nFolder,
2412 HANDLE hToken,
2413 DWORD dwReserved,
2414 LPITEMIDLIST *ppidl)
2416 HRESULT hr = E_INVALIDARG;
2418 TRACE("%p 0x%08x %p 0x%08x %p\n",
2419 hwndOwner, nFolder, hToken, dwReserved, ppidl);
2421 if (!ppidl)
2422 return E_INVALIDARG;
2423 if (dwReserved)
2424 return E_INVALIDARG;
2426 /* The virtual folders' locations are not user-dependent */
2427 *ppidl = NULL;
2428 switch (nFolder)
2430 case CSIDL_DESKTOP:
2431 *ppidl = _ILCreateDesktop();
2432 break;
2434 case CSIDL_PERSONAL:
2435 *ppidl = _ILCreateMyDocuments();
2436 break;
2438 case CSIDL_INTERNET:
2439 *ppidl = _ILCreateIExplore();
2440 break;
2442 case CSIDL_CONTROLS:
2443 *ppidl = _ILCreateControlPanel();
2444 break;
2446 case CSIDL_PRINTERS:
2447 *ppidl = _ILCreatePrinters();
2448 break;
2450 case CSIDL_BITBUCKET:
2451 *ppidl = _ILCreateBitBucket();
2452 break;
2454 case CSIDL_DRIVES:
2455 *ppidl = _ILCreateMyComputer();
2456 break;
2458 case CSIDL_NETWORK:
2459 *ppidl = _ILCreateNetwork();
2460 break;
2462 default:
2464 WCHAR szPath[MAX_PATH];
2466 hr = SHGetFolderPathW(hwndOwner, nFolder, hToken,
2467 SHGFP_TYPE_CURRENT, szPath);
2468 if (SUCCEEDED(hr))
2470 DWORD attributes=0;
2472 TRACE("Value=%s\n", debugstr_w(szPath));
2473 hr = SHILCreateFromPathW(szPath, ppidl, &attributes);
2475 else if (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
2477 /* unlike SHGetFolderPath, SHGetFolderLocation in shell32
2478 * version 6.0 returns E_FAIL for nonexistent paths
2480 hr = E_FAIL;
2484 if(*ppidl)
2485 hr = NOERROR;
2487 TRACE("-- (new pidl %p)\n",*ppidl);
2488 return hr;
2491 /*************************************************************************
2492 * SHGetSpecialFolderLocation [SHELL32.@]
2494 * NOTES
2495 * In NT5, SHGetSpecialFolderLocation needs the <winntdir>/Recent
2496 * directory.
2498 HRESULT WINAPI SHGetSpecialFolderLocation(
2499 HWND hwndOwner,
2500 INT nFolder,
2501 LPITEMIDLIST * ppidl)
2503 HRESULT hr = E_INVALIDARG;
2505 TRACE("(%p,0x%x,%p)\n", hwndOwner,nFolder,ppidl);
2507 if (!ppidl)
2508 return E_INVALIDARG;
2510 hr = SHGetFolderLocation(hwndOwner, nFolder, NULL, 0, ppidl);
2511 return hr;