4 * Copyright 1998, 1999, 2000 Juergen Schmied
5 * Copyright 2004 Juan Lang
6 * Copyright 2018-2019 Katayama Hirofumi MZ
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 * Many of these functions are in SHLWAPI.DLL also
28 #define WIN32_NO_STATUS
32 #include <wine/config.h>
37 #include <undocshell.h>
41 #include <wine/debug.h>
42 #include <wine/unicode.h>
44 #include <shlwapi_undoc.h>
45 #include <shellutils.h>
50 #include "shell32_main.h"
53 WINE_DEFAULT_DEBUG_CHANNEL(shell
);
55 static const BOOL is_win64
= sizeof(void *) > sizeof(int);
58 ########## Combining and Constructing paths ##########
61 /*************************************************************************
62 * PathAppend [SHELL32.36]
64 BOOL WINAPI
PathAppendAW(
68 if (SHELL_OsIsUnicode())
69 return PathAppendW(lpszPath1
, lpszPath2
);
70 return PathAppendA(lpszPath1
, lpszPath2
);
73 /*************************************************************************
74 * PathGetExtensionA [internal]
78 * return value points to the first char after the dot
80 static LPSTR
PathGetExtensionA(LPCSTR lpszPath
)
82 TRACE("(%s)\n",lpszPath
);
84 lpszPath
= PathFindExtensionA(lpszPath
);
85 return (LPSTR
)(*lpszPath
?(lpszPath
+1):lpszPath
);
88 /*************************************************************************
89 * PathGetExtensionW [internal]
91 static LPWSTR
PathGetExtensionW(LPCWSTR lpszPath
)
93 TRACE("(%s)\n",debugstr_w(lpszPath
));
95 lpszPath
= PathFindExtensionW(lpszPath
);
96 return (LPWSTR
)(*lpszPath
?(lpszPath
+1):lpszPath
);
99 /*************************************************************************
100 * SHPathGetExtension [SHELL32.158]
102 LPVOID WINAPI
SHPathGetExtensionW(LPCWSTR lpszPath
, DWORD void1
, DWORD void2
)
104 return PathGetExtensionW(lpszPath
);
107 /*************************************************************************
108 * PathRemoveFileSpec [SHELL32.35]
110 BOOL WINAPI
PathRemoveFileSpecAW(LPVOID lpszPath
)
112 if (SHELL_OsIsUnicode())
113 return PathRemoveFileSpecW(lpszPath
);
114 return PathRemoveFileSpecA(lpszPath
);
121 /*************************************************************************
122 * PathGetShortPathA [internal]
124 static void PathGetShortPathA(LPSTR pszPath
)
128 TRACE("%s\n", pszPath
);
130 if (GetShortPathNameA(pszPath
, path
, MAX_PATH
))
132 lstrcpyA(pszPath
, path
);
136 /*************************************************************************
137 * PathGetShortPathW [internal]
139 static void PathGetShortPathW(LPWSTR pszPath
)
141 WCHAR path
[MAX_PATH
];
143 TRACE("%s\n", debugstr_w(pszPath
));
145 if (GetShortPathNameW(pszPath
, path
, MAX_PATH
))
147 lstrcpyW(pszPath
, path
);
151 /*************************************************************************
152 * PathGetShortPath [SHELL32.92]
154 VOID WINAPI
PathGetShortPathAW(LPVOID pszPath
)
156 if(SHELL_OsIsUnicode())
157 PathGetShortPathW(pszPath
);
158 PathGetShortPathA(pszPath
);
162 ########## Path Testing ##########
165 /*************************************************************************
166 * PathIsRoot [SHELL32.29]
168 BOOL WINAPI
PathIsRootAW(LPCVOID lpszPath
)
170 if (SHELL_OsIsUnicode())
171 return PathIsRootW(lpszPath
);
172 return PathIsRootA(lpszPath
);
175 /*************************************************************************
176 * PathIsExeA [internal]
178 static BOOL
PathIsExeA (LPCSTR lpszPath
)
180 LPCSTR lpszExtension
= PathGetExtensionA(lpszPath
);
182 static const char * const lpszExtensions
[] =
183 {"exe", "com", "pif", "cmd", "bat", "scf", "scr", NULL
};
185 TRACE("path=%s\n",lpszPath
);
187 for(i
=0; lpszExtensions
[i
]; i
++)
188 if (!lstrcmpiA(lpszExtension
,lpszExtensions
[i
])) return TRUE
;
193 /*************************************************************************
194 * PathIsExeW [internal]
196 BOOL
PathIsExeW (LPCWSTR lpszPath
)
198 LPCWSTR lpszExtension
= PathGetExtensionW(lpszPath
);
200 static const WCHAR lpszExtensions
[][4] =
201 {{'e','x','e','\0'}, {'c','o','m','\0'}, {'p','i','f','\0'},
202 {'c','m','d','\0'}, {'b','a','t','\0'}, {'s','c','f','\0'},
203 {'s','c','r','\0'}, {'\0'} };
205 TRACE("path=%s\n",debugstr_w(lpszPath
));
207 for(i
=0; lpszExtensions
[i
][0]; i
++)
208 if (!strcmpiW(lpszExtension
,lpszExtensions
[i
])) return TRUE
;
213 /*************************************************************************
214 * PathIsExe [SHELL32.43]
216 BOOL WINAPI
PathIsExeAW (LPCVOID path
)
218 if (SHELL_OsIsUnicode())
219 return PathIsExeW (path
);
220 return PathIsExeA(path
);
223 /*************************************************************************
224 * PathFileExists [SHELL32.45]
226 BOOL WINAPI
PathFileExistsAW (LPCVOID lpszPath
)
228 if (SHELL_OsIsUnicode())
229 return PathFileExistsW (lpszPath
);
230 return PathFileExistsA (lpszPath
);
233 /*************************************************************************
234 * IsLFNDriveA [SHELL32.41]
236 BOOL WINAPI
IsLFNDriveA(LPCSTR lpszPath
)
240 if (!GetVolumeInformationA(lpszPath
, NULL
, 0, NULL
, &fnlen
, NULL
, NULL
, 0))
245 /*************************************************************************
246 * IsLFNDriveW [SHELL32.42]
248 BOOL WINAPI
IsLFNDriveW(LPCWSTR lpszPath
)
252 if (!GetVolumeInformationW(lpszPath
, NULL
, 0, NULL
, &fnlen
, NULL
, NULL
, 0))
257 /*************************************************************************
258 * IsLFNDrive [SHELL32.119]
260 BOOL WINAPI
IsLFNDriveAW(LPCVOID lpszPath
)
262 if (SHELL_OsIsUnicode())
263 return IsLFNDriveW(lpszPath
);
264 return IsLFNDriveA(lpszPath
);
268 ########## Creating Something Unique ##########
270 /*************************************************************************
271 * PathMakeUniqueNameA [internal]
273 static BOOL
PathMakeUniqueNameA(
276 LPCSTR lpszShortName
,
280 FIXME("%p %u %s %s %s stub\n",
281 lpszBuffer
, dwBuffSize
, debugstr_a(lpszShortName
),
282 debugstr_a(lpszLongName
), debugstr_a(lpszPathName
));
286 /*************************************************************************
287 * PathMakeUniqueNameW [internal]
289 static BOOL
PathMakeUniqueNameW(
292 LPCWSTR lpszShortName
,
293 LPCWSTR lpszLongName
,
294 LPCWSTR lpszPathName
)
296 FIXME("%p %u %s %s %s stub\n",
297 lpszBuffer
, dwBuffSize
, debugstr_w(lpszShortName
),
298 debugstr_w(lpszLongName
), debugstr_w(lpszPathName
));
302 /*************************************************************************
303 * PathMakeUniqueName [SHELL32.47]
305 BOOL WINAPI
PathMakeUniqueNameAW(
308 LPCVOID lpszShortName
,
309 LPCVOID lpszLongName
,
310 LPCVOID lpszPathName
)
312 if (SHELL_OsIsUnicode())
313 return PathMakeUniqueNameW(lpszBuffer
,dwBuffSize
, lpszShortName
,lpszLongName
,lpszPathName
);
314 return PathMakeUniqueNameA(lpszBuffer
,dwBuffSize
, lpszShortName
,lpszLongName
,lpszPathName
);
317 /*************************************************************************
318 * PathYetAnotherMakeUniqueName [SHELL32.75]
320 BOOL WINAPI
PathYetAnotherMakeUniqueName(LPWSTR buffer
, LPCWSTR path
, LPCWSTR shortname
, LPCWSTR longname
)
322 WCHAR pathW
[MAX_PATH
], retW
[MAX_PATH
];
323 const WCHAR
*file
, *ext
;
326 TRACE("(%p, %s, %s, %s)\n", buffer
, debugstr_w(path
), debugstr_w(shortname
), debugstr_w(longname
));
328 file
= longname
? longname
: shortname
;
329 PathCombineW(pathW
, path
, file
);
330 strcpyW(retW
, pathW
);
331 PathRemoveExtensionW(pathW
);
333 ext
= PathFindExtensionW(file
);
335 /* now try to make it unique */
336 while (PathFileExistsW(retW
))
338 static const WCHAR fmtW
[] = {'%','s',' ','(','%','d',')','%','s',0};
340 sprintfW(retW
, fmtW
, pathW
, i
, ext
);
344 strcpyW(buffer
, retW
);
345 TRACE("ret - %s\n", debugstr_w(buffer
));
351 ########## cleaning and resolving paths ##########
354 /*************************************************************************
355 * PathCleanupSpec [SHELL32.171]
357 * lpszFile is changed in place.
359 int WINAPI
PathCleanupSpec( LPCWSTR lpszPathW
, LPWSTR lpszFileW
)
365 if (SHELL_OsIsUnicode())
367 LPWSTR p
= lpszFileW
;
369 TRACE("Cleanup %s\n",debugstr_w(lpszFileW
));
372 length
= strlenW(lpszPathW
);
376 int gct
= PathGetCharTypeW(*p
);
377 if (gct
== GCT_INVALID
|| gct
== GCT_WILD
|| gct
== GCT_SEPARATOR
)
380 rc
|= PCS_REPLACEDCHAR
;
386 if (length
+ i
== MAX_PATH
)
388 rc
|= PCS_FATAL
| PCS_PATHTOOLONG
;
396 LPSTR lpszFileA
= (LPSTR
)lpszFileW
;
397 LPCSTR lpszPathA
= (LPCSTR
)lpszPathW
;
400 TRACE("Cleanup %s\n",debugstr_a(lpszFileA
));
403 length
= strlen(lpszPathA
);
407 int gct
= PathGetCharTypeA(*p
);
408 if (gct
== GCT_INVALID
|| gct
== GCT_WILD
|| gct
== GCT_SEPARATOR
)
411 rc
|= PCS_REPLACEDCHAR
;
417 if (length
+ i
== MAX_PATH
)
419 rc
|= PCS_FATAL
| PCS_PATHTOOLONG
;
428 /*************************************************************************
429 * PathQualifyA [SHELL32]
431 static BOOL
PathQualifyA(LPCSTR pszPath
)
433 FIXME("%s\n",pszPath
);
437 /*************************************************************************
438 * PathQualifyW [SHELL32]
440 static BOOL
PathQualifyW(LPCWSTR pszPath
)
442 FIXME("%s\n",debugstr_w(pszPath
));
446 /*************************************************************************
447 * PathQualify [SHELL32.49]
449 BOOL WINAPI
PathQualifyAW(LPCVOID pszPath
)
451 if (SHELL_OsIsUnicode())
452 return PathQualifyW(pszPath
);
453 return PathQualifyA(pszPath
);
456 static BOOL
PathResolveA(LPSTR path
, LPCSTR
*paths
, DWORD flags
)
458 FIXME("(%s,%p,0x%08x),stub!\n", debugstr_a(path
), paths
, flags
);
462 static BOOL
PathResolveW(LPWSTR path
, LPCWSTR
*paths
, DWORD flags
)
464 FIXME("(%s,%p,0x%08x),stub!\n", debugstr_w(path
), paths
, flags
);
468 /*************************************************************************
469 * PathResolve [SHELL32.51]
471 BOOL WINAPI
PathResolveAW(LPVOID path
, LPCVOID
*paths
, DWORD flags
)
473 if (SHELL_OsIsUnicode())
474 return PathResolveW(path
, (LPCWSTR
*)paths
, flags
);
476 return PathResolveA(path
, (LPCSTR
*)paths
, flags
);
479 /*************************************************************************
480 * PathProcessCommandA
482 static LONG
PathProcessCommandA (
488 FIXME("%s %p 0x%04x 0x%04x stub\n",
489 lpszPath
, lpszBuff
, dwBuffSize
, dwFlags
);
490 if(!lpszPath
) return -1;
491 if(lpszBuff
) strcpy(lpszBuff
, lpszPath
);
492 return strlen(lpszPath
);
495 /*************************************************************************
496 * PathProcessCommandW
498 static LONG
PathProcessCommandW (
504 FIXME("(%s, %p, 0x%04x, 0x%04x) stub\n",
505 debugstr_w(lpszPath
), lpszBuff
, dwBuffSize
, dwFlags
);
506 if(!lpszPath
) return -1;
507 if(lpszBuff
) strcpyW(lpszBuff
, lpszPath
);
508 return strlenW(lpszPath
);
511 /*************************************************************************
512 * PathProcessCommand (SHELL32.653)
514 LONG WINAPI
PathProcessCommandAW (
520 if (SHELL_OsIsUnicode())
521 return PathProcessCommandW(lpszPath
, lpszBuff
, dwBuffSize
, dwFlags
);
522 return PathProcessCommandA(lpszPath
, lpszBuff
, dwBuffSize
, dwFlags
);
526 ########## special ##########
529 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'};
530 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'};
531 static const WCHAR AppDataW
[] = {'A','p','p','D','a','t','a','\0'};
533 static const WCHAR AppData_LocalLowW
[] = {'A','p','p','D','a','t','a','\\','L','o','c','a','l','L','o','w','\0'};
534 static const WCHAR Application_DataW
[] = {'A','p','p','l','i','c','a','t','i','o','n',' ','D','a','t','a','\0'};
536 static const WCHAR CacheW
[] = {'C','a','c','h','e','\0'};
537 static const WCHAR CD_BurningW
[] = {'C','D',' ','B','u','r','n','i','n','g','\0'};
538 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'};
539 static const WCHAR Common_AppDataW
[] = {'C','o','m','m','o','n',' ','A','p','p','D','a','t','a','\0'};
540 static const WCHAR Common_DesktopW
[] = {'C','o','m','m','o','n',' ','D','e','s','k','t','o','p','\0'};
541 static const WCHAR Common_DocumentsW
[] = {'C','o','m','m','o','n',' ','D','o','c','u','m','e','n','t','s','\0'};
542 static const WCHAR Common_FavoritesW
[] = {'C','o','m','m','o','n',' ','F','a','v','o','r','i','t','e','s','\0'};
543 static const WCHAR CommonFilesDirW
[] = {'C','o','m','m','o','n','F','i','l','e','s','D','i','r','\0'};
544 static const WCHAR CommonFilesDirX86W
[] = {'C','o','m','m','o','n','F','i','l','e','s','D','i','r',' ','(','x','8','6',')','\0'};
545 static const WCHAR CommonMusicW
[] = {'C','o','m','m','o','n','M','u','s','i','c','\0'};
546 static const WCHAR CommonPicturesW
[] = {'C','o','m','m','o','n','P','i','c','t','u','r','e','s','\0'};
547 static const WCHAR Common_ProgramsW
[] = {'C','o','m','m','o','n',' ','P','r','o','g','r','a','m','s','\0'};
548 static const WCHAR Common_StartUpW
[] = {'C','o','m','m','o','n',' ','S','t','a','r','t','U','p','\0'};
549 static const WCHAR Common_Start_MenuW
[] = {'C','o','m','m','o','n',' ','S','t','a','r','t',' ','M','e','n','u','\0'};
550 static const WCHAR Common_TemplatesW
[] = {'C','o','m','m','o','n',' ','T','e','m','p','l','a','t','e','s','\0'};
551 static const WCHAR CommonVideoW
[] = {'C','o','m','m','o','n','V','i','d','e','o','\0'};
553 static const WCHAR ContactsW
[] = {'C','o','n','t','a','c','t','s','\0'};
555 static const WCHAR CookiesW
[] = {'C','o','o','k','i','e','s','\0'};
556 static const WCHAR DesktopW
[] = {'D','e','s','k','t','o','p','\0'};
558 static const WCHAR DocumentsW
[] = {'D','o','c','u','m','e','n','t','s','\0'};
559 static const WCHAR DownloadsW
[] = {'D','o','w','n','l','o','a','d','s','\0'};
561 static const WCHAR FavoritesW
[] = {'F','a','v','o','r','i','t','e','s','\0'};
562 static const WCHAR FontsW
[] = {'F','o','n','t','s','\0'};
563 static const WCHAR HistoryW
[] = {'H','i','s','t','o','r','y','\0'};
565 static const WCHAR LinksW
[] = {'L','i','n','k','s','\0'};
567 static const WCHAR Local_AppDataW
[] = {'L','o','c','a','l',' ','A','p','p','D','a','t','a','\0'};
569 static const WCHAR Local_Settings_Application_DataW
[] = {'L','o','c','a','l',' ','S','e','t','t','i','n','g','s','\\','A','p','p','l','i','c','a','t','i','o','n',' ','D','a','t','a','\0'};
571 static const WCHAR Local_Settings_CD_BurningW
[] = {'L','o','c','a','l',' ','S','e','t','t','i','n','g','s','\\','A','p','p','l','i','c','a','t','i','o','n',' ','D','a','t','a','\\','M','i','c','r','o','s','o','f','t','\\','C','D',' ','B','u','r','n','i','n','g','\0'};
573 static const WCHAR Local_Settings_HistoryW
[] = {'L','o','c','a','l',' ','S','e','t','t','i','n','g','s','\\','H','i','s','t','o','r','y','\0'};
574 static const WCHAR Local_Settings_Temporary_Internet_FilesW
[] = {'L','o','c','a','l',' ','S','e','t','t','i','n','g','s','\\','T','e','m','p','o','r','a','r','y',' ','I','n','t','e','r','n','e','t',' ','F','i','l','e','s','\0'};
575 static const WCHAR Microsoft_Windows_GameExplorerW
[] = {'M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\','G','a','m','e','E','x','p','l','o','r','e','r','\0'};
576 static const WCHAR Microsoft_Windows_LibrariesW
[] = {'M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\','L','i','b','r','a','r','i','e','s','\0'};
577 static const WCHAR Microsoft_Windows_RingtonesW
[] = {'M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\','R','i','n','g','t','o','n','e','s','\0'};
578 static const WCHAR MusicW
[] = {'M','u','s','i','c','\0'};
579 static const WCHAR Music_PlaylistsW
[] = {'M','u','s','i','c','\\','P','l','a','y','l','i','s','t','s','\0'};
580 static const WCHAR Music_Sample_MusicW
[] = {'M','u','s','i','c','\\','S','a','m','p','l','e',' ','M','u','s','i','c','\0'};
581 static const WCHAR Music_Sample_PlaylistsW
[] = {'M','u','s','i','c','\\','S','a','m','p','l','e',' ','P','l','a','y','l','i','s','t','s','\0'};
583 static const WCHAR My_MusicW
[] = {'M','y',' ','M','u','s','i','c','\0'};
584 static const WCHAR My_PicturesW
[] = {'M','y',' ','P','i','c','t','u','r','e','s','\0'};
585 static const WCHAR My_VideoW
[] = {'M','y',' ','V','i','d','e','o','\0'};
586 static const WCHAR NetHoodW
[] = {'N','e','t','H','o','o','d','\0'};
587 static const WCHAR OEM_LinksW
[] = {'O','E','M',' ','L','i','n','k','s','\0'};
588 static const WCHAR PersonalW
[] = {'P','e','r','s','o','n','a','l','\0'};
590 static const WCHAR PicturesW
[] = {'P','i','c','t','u','r','e','s','\0'};
591 static const WCHAR Pictures_Sample_PicturesW
[] = {'P','i','c','t','u','r','e','s','\\','S','a','m','p','l','e',' ','P','i','c','t','u','r','e','s','\0'};
592 static const WCHAR Pictures_Slide_ShowsW
[] = {'P','i','c','t','u','r','e','s','\\','S','l','i','d','e',' ','S','h','o','w','s','\0'};
594 static const WCHAR PrintHoodW
[] = {'P','r','i','n','t','H','o','o','d','\0'};
596 static const WCHAR Program_FilesW
[] = {'P','r','o','g','r','a','m',' ','F','i','l','e','s','\0'};
597 static const WCHAR Program_Files_Common_FilesW
[] = {'P','r','o','g','r','a','m',' ','F','i','l','e','s','\\','C','o','m','m','o','n',' ','F','i','l','e','s','\0'};
599 static const WCHAR Program_Files_x86W
[] = {'P','r','o','g','r','a','m',' ','F','i','l','e','s',' ','(','x','8','6',')','\0'};
600 static const WCHAR Program_Files_x86_Common_FilesW
[] = {'P','r','o','g','r','a','m',' ','F','i','l','e','s',' ','(','x','8','6',')','\\','C','o','m','m','o','n',' ','F','i','l','e','s','\0'};
601 static const WCHAR ProgramFilesDirW
[] = {'P','r','o','g','r','a','m','F','i','l','e','s','D','i','r','\0'};
602 static const WCHAR ProgramFilesDirX86W
[] = {'P','r','o','g','r','a','m','F','i','l','e','s','D','i','r',' ','(','x','8','6',')','\0'};
603 static const WCHAR ProgramsW
[] = {'P','r','o','g','r','a','m','s','\0'};
605 static const WCHAR PublicW
[] = {'P','u','b','l','i','c',0};
607 static const WCHAR RecentW
[] = {'R','e','c','e','n','t','\0'};
608 static const WCHAR ResourcesW
[] = {'R','e','s','o','u','r','c','e','s','\0'};
610 static const WCHAR Saved_GamesW
[] = {'S','a','v','e','d',' ','G','a','m','e','s','\0'};
611 static const WCHAR SearchesW
[] = {'S','e','a','r','c','h','e','s','\0'};
613 static const WCHAR SendToW
[] = {'S','e','n','d','T','o','\0'};
614 static const WCHAR StartUpW
[] = {'S','t','a','r','t','U','p','\0'};
615 static const WCHAR Start_MenuW
[] = {'S','t','a','r','t',' ','M','e','n','u','\0'};
617 static const WCHAR Start_Menu_ProgramsW
[] = {'S','t','a','r','t',' ','M','e','n','u','\\','P','r','o','g','r','a','m','s','\0'};
618 static const WCHAR Start_Menu_Admin_ToolsW
[] = {'S','t','a','r','t',' ','M','e','n','u','\\','P','r','o','g','r','a','m','s','\\','A','d','m','i','n','i','s','t','r','a','t','i','v','e',' ','T','o','o','l','s','\0'};
619 static const WCHAR Start_Menu_StartupW
[] = {'S','t','a','r','t',' ','M','e','n','u','\\','P','r','o','g','r','a','m','s','\\','S','t','a','r','t','U','p','\0'};
621 static const WCHAR TemplatesW
[] = {'T','e','m','p','l','a','t','e','s','\0'};
623 static const WCHAR UsersW
[] = {'U','s','e','r','s','\0'};
624 static const WCHAR UsersPublicW
[] = {'U','s','e','r','s','\\','P','u','b','l','i','c','\0'};
625 static const WCHAR VideosW
[] = {'V','i','d','e','o','s','\0'};
626 static const WCHAR Videos_Sample_VideosW
[] = {'V','i','d','e','o','s','\\','S','a','m','p','l','e',' ','V','i','d','e','o','s','\0'};
628 static const WCHAR DefaultW
[] = {'.','D','e','f','a','u','l','t','\0'};
629 static const WCHAR AllUsersProfileW
[] = {'%','A','L','L','U','S','E','R','S','P','R','O','F','I','L','E','%','\0'};
631 static const WCHAR PublicProfileW
[] = {'%','P','U','B','L','I','C','%',0};
633 static const WCHAR UserProfileW
[] = {'%','U','S','E','R','P','R','O','F','I','L','E','%','\0'};
634 static const WCHAR SystemDriveW
[] = {'%','S','y','s','t','e','m','D','r','i','v','e','%','\0'};
636 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};
637 static const WCHAR ProfilesDirectoryW
[] = {'P','r','o','f','i','l','e','s','D','i','r','e','c','t','o','r','y',0};
638 static const WCHAR AllUsersProfileValueW
[] = {'A','l','l','U','s','e','r','s','P','r','o','f','i','l','e','\0'};
640 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'};
641 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'};
642 static const WCHAR szDefaultProfileDirW
[] = {'u','s','e','r','s',0};
644 static const WCHAR szKnownFolderDescriptions
[] = {'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','\\','F','o','l','d','e','r','D','e','s','c','r','i','p','t','i','o','n','s','\0'};
645 static const WCHAR szKnownFolderRedirections
[] = {'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};
647 static const WCHAR AllUsersW
[] = {'P','u','b','l','i','c',0};
649 typedef enum _CSIDL_Type
{
653 CSIDL_Type_Disallowed
,
654 CSIDL_Type_NonExistent
,
655 CSIDL_Type_WindowsPath
,
656 CSIDL_Type_SystemPath
,
657 CSIDL_Type_SystemX86Path
,
660 /* Cannot use #if _WIN32_WINNT >= 0x0600 because _WIN32_WINNT == 0x0600 here. */
662 #define CSIDL_CONTACTS 0x0043
663 #define CSIDL_DOWNLOADS 0x0047
664 #define CSIDL_LINKS 0x004d
665 #define CSIDL_APPDATA_LOCALLOW 0x004e
666 #define CSIDL_SAVED_GAMES 0x0062
667 #define CSIDL_SEARCHES 0x0063
672 const KNOWNFOLDERID
*id
;
675 LPCWSTR szDefaultPath
; /* fallback string or resource ID */
676 INT nShell32IconIndex
;
679 static const CSIDL_DATA CSIDL_Data
[] =
681 { /* 0x00 - CSIDL_DESKTOP */
685 MAKEINTRESOURCEW(IDS_DESKTOPDIRECTORY
),
688 { /* 0x01 - CSIDL_INTERNET */
689 &FOLDERID_InternetFolder
,
690 CSIDL_Type_Disallowed
,
694 { /* 0x02 - CSIDL_PROGRAMS */
698 MAKEINTRESOURCEW(IDS_PROGRAMS
),
699 -IDI_SHELL_PROGRAMS_FOLDER
701 { /* 0x03 - CSIDL_CONTROLS (.CPL files) */
702 &FOLDERID_ControlPanelFolder
,
703 CSIDL_Type_SystemPath
,
706 -IDI_SHELL_CONTROL_PANEL
708 { /* 0x04 - CSIDL_PRINTERS */
709 &FOLDERID_PrintersFolder
,
710 CSIDL_Type_SystemPath
,
713 -IDI_SHELL_PRINTERS_FOLDER
715 { /* 0x05 - CSIDL_PERSONAL */
719 MAKEINTRESOURCEW(IDS_PERSONAL
),
720 -IDI_SHELL_MY_DOCUMENTS
722 { /* 0x06 - CSIDL_FAVORITES */
726 MAKEINTRESOURCEW(IDS_FAVORITES
),
729 { /* 0x07 - CSIDL_STARTUP */
733 MAKEINTRESOURCEW(IDS_STARTUP
)
735 { /* 0x08 - CSIDL_RECENT */
739 MAKEINTRESOURCEW(IDS_RECENT
),
740 -IDI_SHELL_RECENT_DOCUMENTS
742 { /* 0x09 - CSIDL_SENDTO */
746 MAKEINTRESOURCEW(IDS_SENDTO
)
748 { /* 0x0a - CSIDL_BITBUCKET - Recycle Bin */
749 &FOLDERID_RecycleBinFolder
,
750 CSIDL_Type_Disallowed
,
754 { /* 0x0b - CSIDL_STARTMENU */
758 MAKEINTRESOURCEW(IDS_STARTMENU
),
759 -IDI_SHELL_TSKBAR_STARTMENU
761 { /* 0x0c - CSIDL_MYDOCUMENTS */
763 CSIDL_Type_Disallowed
, /* matches WinXP--can't get its path */
766 -IDI_SHELL_MY_DOCUMENTS
768 { /* 0x0d - CSIDL_MYMUSIC */
772 MAKEINTRESOURCEW(IDS_MYMUSIC
),
775 { /* 0x0e - CSIDL_MYVIDEO */
779 MAKEINTRESOURCEW(IDS_MYVIDEO
),
782 { /* 0x0f - unassigned */
784 CSIDL_Type_Disallowed
,
788 { /* 0x10 - CSIDL_DESKTOPDIRECTORY */
792 MAKEINTRESOURCEW(IDS_DESKTOPDIRECTORY
),
795 { /* 0x11 - CSIDL_DRIVES */
796 &FOLDERID_ComputerFolder
,
797 CSIDL_Type_Disallowed
,
800 -IDI_SHELL_COMPUTER_FOLDER
802 { /* 0x12 - CSIDL_NETWORK */
803 &FOLDERID_NetworkFolder
,
804 CSIDL_Type_Disallowed
,
807 -IDI_SHELL_NETWORK_FOLDER
809 { /* 0x13 - CSIDL_NETHOOD */
813 MAKEINTRESOURCEW(IDS_NETHOOD
),
816 { /* 0x14 - CSIDL_FONTS */
818 CSIDL_Type_WindowsPath
,
821 -IDI_SHELL_FONTS_FOLDER
823 { /* 0x15 - CSIDL_TEMPLATES */
827 MAKEINTRESOURCEW(IDS_TEMPLATES
)
829 { /* 0x16 - CSIDL_COMMON_STARTMENU */
830 &FOLDERID_CommonStartMenu
,
833 MAKEINTRESOURCEW(IDS_STARTMENU
),
834 -IDI_SHELL_TSKBAR_STARTMENU
836 { /* 0x17 - CSIDL_COMMON_PROGRAMS */
837 &FOLDERID_CommonPrograms
,
840 MAKEINTRESOURCEW(IDS_PROGRAMS
),
841 -IDI_SHELL_PROGRAMS_FOLDER
843 { /* 0x18 - CSIDL_COMMON_STARTUP */
844 &FOLDERID_CommonStartup
,
847 MAKEINTRESOURCEW(IDS_STARTUP
)
849 { /* 0x19 - CSIDL_COMMON_DESKTOPDIRECTORY */
850 &FOLDERID_PublicDesktop
,
853 MAKEINTRESOURCEW(IDS_DESKTOPDIRECTORY
),
856 { /* 0x1a - CSIDL_APPDATA */
857 &FOLDERID_RoamingAppData
,
860 MAKEINTRESOURCEW(IDS_APPDATA
)
862 { /* 0x1b - CSIDL_PRINTHOOD */
866 MAKEINTRESOURCEW(IDS_PRINTHOOD
),
867 -IDI_SHELL_PRINTERS_FOLDER
869 { /* 0x1c - CSIDL_LOCAL_APPDATA */
870 &FOLDERID_LocalAppData
,
873 MAKEINTRESOURCEW(IDS_LOCAL_APPDATA
)
875 { /* 0x1d - CSIDL_ALTSTARTUP */
877 CSIDL_Type_NonExistent
,
881 { /* 0x1e - CSIDL_COMMON_ALTSTARTUP */
883 CSIDL_Type_NonExistent
,
887 { /* 0x1f - CSIDL_COMMON_FAVORITES */
891 MAKEINTRESOURCEW(IDS_FAVORITES
),
894 { /* 0x20 - CSIDL_INTERNET_CACHE */
895 &FOLDERID_InternetCache
,
898 MAKEINTRESOURCEW(IDS_INTERNET_CACHE
)
900 { /* 0x21 - CSIDL_COOKIES */
904 MAKEINTRESOURCEW(IDS_COOKIES
)
906 { /* 0x22 - CSIDL_HISTORY */
910 MAKEINTRESOURCEW(IDS_HISTORY
)
912 { /* 0x23 - CSIDL_COMMON_APPDATA */
913 &FOLDERID_ProgramData
,
916 MAKEINTRESOURCEW(IDS_APPDATA
)
918 { /* 0x24 - CSIDL_WINDOWS */
920 CSIDL_Type_WindowsPath
,
923 -IDI_SHELL_SYSTEM_GEAR
925 { /* 0x25 - CSIDL_SYSTEM */
927 CSIDL_Type_SystemPath
,
930 -IDI_SHELL_SYSTEM_GEAR
932 { /* 0x26 - CSIDL_PROGRAM_FILES */
933 &FOLDERID_ProgramFiles
,
936 MAKEINTRESOURCEW(IDS_PROGRAM_FILES
),
937 -IDI_SHELL_PROGRAMS_FOLDER
939 { /* 0x27 - CSIDL_MYPICTURES */
943 MAKEINTRESOURCEW(IDS_MYPICTURES
),
944 -IDI_SHELL_MY_PICTURES
946 { /* 0x28 - CSIDL_PROFILE */
952 { /* 0x29 - CSIDL_SYSTEMX86 */
954 CSIDL_Type_SystemX86Path
,
957 -IDI_SHELL_SYSTEM_GEAR
959 { /* 0x2a - CSIDL_PROGRAM_FILESX86 */
960 &FOLDERID_ProgramFilesX86
,
964 -IDI_SHELL_PROGRAMS_FOLDER
966 { /* 0x2b - CSIDL_PROGRAM_FILES_COMMON */
967 &FOLDERID_ProgramFilesCommon
,
970 MAKEINTRESOURCEW(IDS_PROGRAM_FILES_COMMON
),
971 -IDI_SHELL_PROGRAMS_FOLDER
973 { /* 0x2c - CSIDL_PROGRAM_FILES_COMMONX86 */
974 &FOLDERID_ProgramFilesCommonX86
,
977 Program_Files_x86_Common_FilesW
979 { /* 0x2d - CSIDL_COMMON_TEMPLATES */
980 &FOLDERID_CommonTemplates
,
983 MAKEINTRESOURCEW(IDS_TEMPLATES
)
985 { /* 0x2e - CSIDL_COMMON_DOCUMENTS */
986 &FOLDERID_PublicDocuments
,
989 MAKEINTRESOURCEW(IDS_PERSONAL
),
990 -IDI_SHELL_MY_DOCUMENTS
992 { /* 0x2f - CSIDL_COMMON_ADMINTOOLS */
993 &FOLDERID_CommonAdminTools
,
995 Common_Administrative_ToolsW
,
996 MAKEINTRESOURCEW(IDS_ADMINTOOLS
)
998 { /* 0x30 - CSIDL_ADMINTOOLS */
999 &FOLDERID_AdminTools
,
1001 Administrative_ToolsW
,
1002 MAKEINTRESOURCEW(IDS_ADMINTOOLS
)
1004 { /* 0x31 - CSIDL_CONNECTIONS */
1005 &FOLDERID_ConnectionsFolder
,
1006 CSIDL_Type_Disallowed
,
1009 -IDI_SHELL_NETWORK_CONNECTIONS
1011 { /* 0x32 - unassigned */
1013 CSIDL_Type_Disallowed
,
1017 { /* 0x33 - unassigned */
1019 CSIDL_Type_Disallowed
,
1023 { /* 0x34 - unassigned */
1025 CSIDL_Type_Disallowed
,
1029 { /* 0x35 - CSIDL_COMMON_MUSIC */
1030 &FOLDERID_PublicMusic
,
1031 CSIDL_Type_AllUsers
,
1033 MAKEINTRESOURCEW(IDS_COMMON_MUSIC
),
1036 { /* 0x36 - CSIDL_COMMON_PICTURES */
1037 &FOLDERID_PublicPictures
,
1038 CSIDL_Type_AllUsers
,
1040 MAKEINTRESOURCEW(IDS_COMMON_PICTURES
),
1041 -IDI_SHELL_MY_PICTURES
1043 { /* 0x37 - CSIDL_COMMON_VIDEO */
1044 &FOLDERID_PublicVideos
,
1045 CSIDL_Type_AllUsers
,
1047 MAKEINTRESOURCEW(IDS_COMMON_VIDEO
),
1048 -IDI_SHELL_MY_MOVIES
1050 { /* 0x38 - CSIDL_RESOURCES */
1051 &FOLDERID_ResourceDir
,
1052 CSIDL_Type_WindowsPath
,
1056 { /* 0x39 - CSIDL_RESOURCES_LOCALIZED */
1057 &FOLDERID_LocalizedResourcesDir
,
1058 CSIDL_Type_NonExistent
,
1062 { /* 0x3a - CSIDL_COMMON_OEM_LINKS */
1063 &FOLDERID_CommonOEMLinks
,
1064 CSIDL_Type_AllUsers
,
1068 { /* 0x3b - CSIDL_CDBURN_AREA */
1069 &FOLDERID_CDBurning
,
1072 Local_Settings_CD_BurningW
1074 { /* 0x3c unassigned */
1076 CSIDL_Type_Disallowed
,
1080 { /* 0x3d - CSIDL_COMPUTERSNEARME */
1082 CSIDL_Type_Disallowed
, /* FIXME */
1086 { /* 0x3e - CSIDL_PROFILES */
1088 CSIDL_Type_Disallowed
, /* oddly, this matches WinXP */
1092 /* Cannot use #if _WIN32_WINNT >= 0x0600 because _WIN32_WINNT == 0x0600 here. */
1095 &FOLDERID_AddNewPrograms
,
1096 CSIDL_Type_Disallowed
,
1101 &FOLDERID_AppUpdates
,
1102 CSIDL_Type_Disallowed
,
1107 &FOLDERID_ChangeRemovePrograms
,
1108 CSIDL_Type_Disallowed
,
1113 &FOLDERID_ConflictFolder
,
1114 CSIDL_Type_Disallowed
,
1118 { /* 0x43 - CSIDL_CONTACTS */
1125 &FOLDERID_DeviceMetadataStore
,
1126 CSIDL_Type_Disallowed
, /* FIXME */
1137 &FOLDERID_DocumentsLibrary
,
1138 CSIDL_Type_Disallowed
, /* FIXME */
1142 { /* 0x47 - CSIDL_DOWNLOADS */
1143 &FOLDERID_Downloads
,
1150 CSIDL_Type_Disallowed
,
1155 &FOLDERID_GameTasks
,
1156 CSIDL_Type_Disallowed
, /* FIXME */
1161 &FOLDERID_HomeGroup
,
1162 CSIDL_Type_Disallowed
,
1167 &FOLDERID_ImplicitAppShortcuts
,
1168 CSIDL_Type_Disallowed
, /* FIXME */
1173 &FOLDERID_Libraries
,
1174 CSIDL_Type_Disallowed
, /* FIXME */
1178 { /* 0x4d - CSIDL_LINKS */
1184 { /* 0x4e - CSIDL_APPDATA_LOCALLOW */
1185 &FOLDERID_LocalAppDataLow
,
1191 &FOLDERID_MusicLibrary
,
1192 CSIDL_Type_Disallowed
, /* FIXME */
1197 &FOLDERID_OriginalImages
,
1198 CSIDL_Type_Disallowed
, /* FIXME */
1203 &FOLDERID_PhotoAlbums
,
1206 Pictures_Slide_ShowsW
1209 &FOLDERID_PicturesLibrary
,
1210 CSIDL_Type_Disallowed
, /* FIXME */
1215 &FOLDERID_Playlists
,
1221 &FOLDERID_ProgramFilesX64
,
1222 CSIDL_Type_NonExistent
,
1227 &FOLDERID_ProgramFilesCommonX64
,
1228 CSIDL_Type_NonExistent
,
1234 CSIDL_Type_CurrVer
, /* FIXME */
1239 &FOLDERID_PublicDownloads
,
1240 CSIDL_Type_AllUsers
,
1245 &FOLDERID_PublicGameTasks
,
1246 CSIDL_Type_AllUsers
,
1248 Microsoft_Windows_GameExplorerW
1251 &FOLDERID_PublicLibraries
,
1252 CSIDL_Type_AllUsers
,
1254 Microsoft_Windows_LibrariesW
1257 &FOLDERID_PublicRingtones
,
1258 CSIDL_Type_AllUsers
,
1260 Microsoft_Windows_RingtonesW
1263 &FOLDERID_QuickLaunch
,
1264 CSIDL_Type_Disallowed
, /* FIXME */
1269 &FOLDERID_RecordedTVLibrary
,
1270 CSIDL_Type_Disallowed
, /* FIXME */
1275 &FOLDERID_Ringtones
,
1276 CSIDL_Type_Disallowed
, /* FIXME */
1281 &FOLDERID_SampleMusic
,
1282 CSIDL_Type_AllUsers
,
1287 &FOLDERID_SamplePictures
,
1288 CSIDL_Type_AllUsers
,
1290 Pictures_Sample_PicturesW
1293 &FOLDERID_SamplePlaylists
,
1294 CSIDL_Type_AllUsers
,
1296 Music_Sample_PlaylistsW
1299 &FOLDERID_SampleVideos
,
1300 CSIDL_Type_AllUsers
,
1302 Videos_Sample_VideosW
1304 { /* 0x62 - CSIDL_SAVED_GAMES */
1305 &FOLDERID_SavedGames
,
1310 { /* 0x63 - CSIDL_SEARCHES */
1311 &FOLDERID_SavedSearches
,
1317 &FOLDERID_SEARCH_CSC
,
1318 CSIDL_Type_Disallowed
,
1323 &FOLDERID_SEARCH_MAPI
,
1324 CSIDL_Type_Disallowed
,
1329 &FOLDERID_SearchHome
,
1330 CSIDL_Type_Disallowed
,
1335 &FOLDERID_SidebarDefaultParts
,
1336 CSIDL_Type_Disallowed
, /* FIXME */
1341 &FOLDERID_SidebarParts
,
1342 CSIDL_Type_Disallowed
, /* FIXME */
1347 &FOLDERID_SyncManagerFolder
,
1348 CSIDL_Type_Disallowed
,
1353 &FOLDERID_SyncResultsFolder
,
1354 CSIDL_Type_Disallowed
,
1359 &FOLDERID_SyncSetupFolder
,
1360 CSIDL_Type_Disallowed
,
1365 &FOLDERID_UserPinned
,
1366 CSIDL_Type_Disallowed
, /* FIXME */
1371 &FOLDERID_UserProfiles
,
1377 &FOLDERID_UserProgramFiles
,
1378 CSIDL_Type_Disallowed
, /* FIXME */
1383 &FOLDERID_UserProgramFilesCommon
,
1384 CSIDL_Type_Disallowed
, /* FIXME */
1389 &FOLDERID_UsersFiles
,
1390 CSIDL_Type_Disallowed
,
1395 &FOLDERID_UsersLibraries
,
1396 CSIDL_Type_Disallowed
,
1401 &FOLDERID_VideosLibrary
,
1402 CSIDL_Type_Disallowed
, /* FIXME */
1410 static HRESULT
_SHExpandEnvironmentStrings(LPCWSTR szSrc
, LPWSTR szDest
);
1412 static HRESULT
_SHExpandEnvironmentStrings(HANDLE hToken
, LPCWSTR szSrc
, LPWSTR szDest
, DWORD cchDest
);
1415 /* Gets the value named value from the registry key
1416 * rootKey\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
1417 * (or from rootKey\userPrefix\... if userPrefix is not NULL) into path, which
1418 * is assumed to be MAX_PATH WCHARs in length.
1419 * If it exists, expands the value and writes the expanded value to
1420 * rootKey\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders
1421 * Returns successful error code if the value was retrieved from the registry,
1422 * and a failure otherwise.
1425 static HRESULT
_SHGetUserShellFolderPath(HKEY rootKey
, LPCWSTR userPrefix
,
1427 static HRESULT
_SHGetUserShellFolderPath(HKEY rootKey
, HANDLE hToken
, LPCWSTR userPrefix
,
1429 LPCWSTR value
, LPWSTR path
)
1432 WCHAR shellFolderPath
[MAX_PATH
], userShellFolderPath
[MAX_PATH
];
1433 LPCWSTR pShellFolderPath
, pUserShellFolderPath
;
1434 HKEY userShellFolderKey
, shellFolderKey
;
1435 DWORD dwType
, dwPathLen
;
1437 TRACE("%p,%s,%s,%p\n",rootKey
, debugstr_w(userPrefix
), debugstr_w(value
),
1442 strcpyW(shellFolderPath
, userPrefix
);
1443 PathAddBackslashW(shellFolderPath
);
1444 strcatW(shellFolderPath
, szSHFolders
);
1445 pShellFolderPath
= shellFolderPath
;
1446 strcpyW(userShellFolderPath
, userPrefix
);
1447 PathAddBackslashW(userShellFolderPath
);
1448 strcatW(userShellFolderPath
, szSHUserFolders
);
1449 pUserShellFolderPath
= userShellFolderPath
;
1453 pUserShellFolderPath
= szSHUserFolders
;
1454 pShellFolderPath
= szSHFolders
;
1457 if (RegCreateKeyW(rootKey
, pShellFolderPath
, &shellFolderKey
))
1459 TRACE("Failed to create %s\n", debugstr_w(pShellFolderPath
));
1462 if (RegCreateKeyW(rootKey
, pUserShellFolderPath
, &userShellFolderKey
))
1464 TRACE("Failed to create %s\n",
1465 debugstr_w(pUserShellFolderPath
));
1466 RegCloseKey(shellFolderKey
);
1470 dwPathLen
= MAX_PATH
* sizeof(WCHAR
);
1471 if (!RegQueryValueExW(userShellFolderKey
, value
, NULL
, &dwType
,
1472 (LPBYTE
)path
, &dwPathLen
) && (dwType
== REG_EXPAND_SZ
|| dwType
== REG_SZ
))
1476 path
[dwPathLen
/ sizeof(WCHAR
)] = '\0';
1477 if (dwType
== REG_EXPAND_SZ
&& path
[0] == '%')
1479 WCHAR szTemp
[MAX_PATH
];
1482 _SHExpandEnvironmentStrings(path
, szTemp
);
1484 hr
= _SHExpandEnvironmentStrings(hToken
, path
, szTemp
, _countof(szTemp
));
1488 lstrcpynW(path
, szTemp
, MAX_PATH
);
1490 ret
= RegSetValueExW(shellFolderKey
, value
, 0, REG_SZ
, (LPBYTE
)path
,
1491 (strlenW(path
) + 1) * sizeof(WCHAR
));
1492 if (ret
!= ERROR_SUCCESS
)
1493 hr
= HRESULT_FROM_WIN32(ret
);
1502 RegCloseKey(shellFolderKey
);
1503 RegCloseKey(userShellFolderKey
);
1504 TRACE("returning 0x%08x\n", hr
);
1508 BOOL
_SHGetUserProfileDirectoryW(HANDLE hToken
, LPWSTR szPath
, LPDWORD lpcchPath
)
1513 OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY
, &hToken
);
1514 result
= GetUserProfileDirectoryW(hToken
, szPath
, lpcchPath
);
1515 CloseHandle(hToken
);
1517 else if ((INT
) hToken
== -1)
1519 result
= GetDefaultUserProfileDirectoryW(szPath
, lpcchPath
);
1523 result
= GetUserProfileDirectoryW(hToken
, szPath
, lpcchPath
);
1525 TRACE("_SHGetUserProfileDirectoryW returning %S\n", szPath
);
1529 /* Gets a 'semi-expanded' default value of the CSIDL with index folder into
1530 * pszPath, based on the entries in CSIDL_Data. By semi-expanded, I mean:
1531 * - The entry's szDefaultPath may be either a string value or an integer
1532 * resource identifier. In the latter case, the string value of the resource
1534 * - Depending on the entry's type, the path may begin with an (unexpanded)
1535 * environment variable name. The caller is responsible for expanding
1536 * environment strings if so desired.
1537 * The types that are prepended with environment variables are:
1538 * CSIDL_Type_User: %USERPROFILE%
1539 * CSIDL_Type_AllUsers: %ALLUSERSPROFILE%
1540 * CSIDL_Type_CurrVer: %SystemDrive%
1541 * (Others might make sense too, but as yet are unneeded.)
1544 static HRESULT
_SHGetDefaultValue(BYTE folder
, LPWSTR pszPath
)
1546 static HRESULT
_SHGetDefaultValue(HANDLE hToken
, BYTE folder
, LPWSTR pszPath
)
1550 WCHAR resourcePath
[MAX_PATH
];
1552 TRACE("0x%02x,%p\n", folder
, pszPath
);
1554 if (folder
>= ARRAY_SIZE(CSIDL_Data
))
1555 return E_INVALIDARG
;
1558 return E_INVALIDARG
;
1561 if (hToken
!= NULL
&& hToken
!= (HANDLE
)-1)
1563 FIXME("unsupported for user other than current or default\n");
1573 case CSIDL_PROGRAM_FILES
:
1574 case CSIDL_PROGRAM_FILESX86
:
1575 IsWow64Process( GetCurrentProcess(), &is_wow64
);
1576 folder
= is_wow64
? CSIDL_PROGRAM_FILESX86
: CSIDL_PROGRAM_FILES
;
1578 case CSIDL_PROGRAM_FILES_COMMON
:
1579 case CSIDL_PROGRAM_FILES_COMMONX86
:
1580 IsWow64Process( GetCurrentProcess(), &is_wow64
);
1581 folder
= is_wow64
? CSIDL_PROGRAM_FILES_COMMONX86
: CSIDL_PROGRAM_FILES_COMMON
;
1586 switch (CSIDL_Data
[folder
].type
)
1588 case CSIDL_Type_User
:
1589 strcpyW(pszPath
, UserProfileW
);
1591 case CSIDL_Type_AllUsers
:
1593 strcpyW(pszPath
, PublicProfileW
);
1595 strcpyW(pszPath
, AllUsersProfileW
);
1598 case CSIDL_Type_CurrVer
:
1599 strcpyW(pszPath
, SystemDriveW
);
1602 ; /* no corresponding env. var, do nothing */
1606 if (CSIDL_Data
[folder
].szDefaultPath
)
1608 if (IS_INTRESOURCE(CSIDL_Data
[folder
].szDefaultPath
))
1610 if (LoadStringW(shell32_hInstance
,
1611 LOWORD(CSIDL_Data
[folder
].szDefaultPath
), resourcePath
, MAX_PATH
))
1613 PathAppendW(pszPath
, resourcePath
);
1617 ERR("(%d,%s), LoadString failed, missing translation?\n", folder
,
1618 debugstr_w(pszPath
));
1624 PathAppendW(pszPath
, CSIDL_Data
[folder
].szDefaultPath
);
1627 TRACE("returning 0x%08x\n", hr
);
1631 /* Gets the (unexpanded) value of the folder with index folder into pszPath.
1632 * The folder's type is assumed to be CSIDL_Type_CurrVer. Its default value
1633 * can be overridden in the HKLM\\szCurrentVersion key.
1634 * If dwFlags has SHGFP_TYPE_DEFAULT set or if the value isn't overridden in
1635 * the registry, uses _SHGetDefaultValue to get the value.
1637 static HRESULT
_SHGetCurrentVersionPath(DWORD dwFlags
, BYTE folder
,
1642 TRACE("0x%08x,0x%02x,%p\n", dwFlags
, folder
, pszPath
);
1644 if (folder
>= ARRAY_SIZE(CSIDL_Data
))
1645 return E_INVALIDARG
;
1646 if (CSIDL_Data
[folder
].type
!= CSIDL_Type_CurrVer
)
1647 return E_INVALIDARG
;
1649 return E_INVALIDARG
;
1651 if (dwFlags
& SHGFP_TYPE_DEFAULT
)
1653 hr
= _SHGetDefaultValue(folder
, pszPath
);
1655 hr
= _SHGetDefaultValue(NULL
, folder
, pszPath
);
1661 if (RegCreateKeyW(HKEY_LOCAL_MACHINE
, szCurrentVersion
, &hKey
))
1665 DWORD dwType
, dwPathLen
= MAX_PATH
* sizeof(WCHAR
);
1667 if (RegQueryValueExW(hKey
, CSIDL_Data
[folder
].szValueName
, NULL
,
1668 &dwType
, (LPBYTE
)pszPath
, &dwPathLen
) ||
1669 (dwType
!= REG_SZ
&& dwType
!= REG_EXPAND_SZ
))
1672 hr
= _SHGetDefaultValue(folder
, pszPath
);
1674 hr
= _SHGetDefaultValue(NULL
, folder
, pszPath
);
1676 dwType
= REG_EXPAND_SZ
;
1679 case CSIDL_PROGRAM_FILESX86
:
1680 case CSIDL_PROGRAM_FILES_COMMONX86
:
1681 /* these two should never be set on 32-bit setups */
1685 IsWow64Process( GetCurrentProcess(), &is_wow64
);
1686 if (!is_wow64
) break;
1690 RegSetValueExW(hKey
, CSIDL_Data
[folder
].szValueName
, 0, dwType
,
1691 (LPBYTE
)pszPath
, (strlenW(pszPath
)+1)*sizeof(WCHAR
));
1696 pszPath
[dwPathLen
/ sizeof(WCHAR
)] = '\0';
1702 TRACE("returning 0x%08x (output path is %s)\n", hr
, debugstr_w(pszPath
));
1706 static LPWSTR
_GetUserSidStringFromToken(HANDLE Token
)
1708 char InfoBuffer
[64];
1709 PTOKEN_USER UserInfo
;
1713 UserInfo
= (PTOKEN_USER
) InfoBuffer
;
1714 if (! GetTokenInformation(Token
, TokenUser
, InfoBuffer
, sizeof(InfoBuffer
),
1717 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER
)
1719 UserInfo
= HeapAlloc(GetProcessHeap(), 0, InfoSize
);
1720 if (UserInfo
== NULL
)
1722 if (! GetTokenInformation(Token
, TokenUser
, UserInfo
, InfoSize
,
1725 HeapFree(GetProcessHeap(), 0, UserInfo
);
1730 if (! ConvertSidToStringSidW(UserInfo
->User
.Sid
, &SidStr
))
1733 if (UserInfo
!= (PTOKEN_USER
) InfoBuffer
)
1734 HeapFree(GetProcessHeap(), 0, UserInfo
);
1739 /* Gets the user's path (unexpanded) for the CSIDL with index folder:
1740 * If SHGFP_TYPE_DEFAULT is set, calls _SHGetDefaultValue for it. Otherwise
1741 * calls _SHGetUserShellFolderPath for it. Where it looks depends on hToken:
1742 * - if hToken is -1, looks in HKEY_USERS\.Default
1743 * - otherwise looks first in HKEY_CURRENT_USER, followed by HKEY_LOCAL_MACHINE
1744 * if HKEY_CURRENT_USER doesn't contain any entries. If both fail, finally
1745 * calls _SHGetDefaultValue for it.
1747 static HRESULT
_SHGetUserProfilePath(HANDLE hToken
, DWORD dwFlags
, BYTE folder
,
1750 const WCHAR
*szValueName
;
1754 TRACE("%p,0x%08x,0x%02x,%p\n", hToken
, dwFlags
, folder
, pszPath
);
1756 if (folder
>= ARRAY_SIZE(CSIDL_Data
))
1757 return E_INVALIDARG
;
1758 if (CSIDL_Data
[folder
].type
!= CSIDL_Type_User
)
1759 return E_INVALIDARG
;
1761 return E_INVALIDARG
;
1763 if (dwFlags
& SHGFP_TYPE_DEFAULT
)
1766 hr
= _SHGetDefaultValue(folder
, pszPath
);
1768 hr
= _SHGetDefaultValue(hToken
, folder
, pszPath
);
1773 LPCWSTR userPrefix
= NULL
;
1776 if (hToken
== (HANDLE
)-1)
1778 hRootKey
= HKEY_USERS
;
1779 userPrefix
= DefaultW
;
1781 else if (hToken
== NULL
)
1782 hRootKey
= HKEY_CURRENT_USER
;
1785 hRootKey
= HKEY_USERS
;
1786 userPrefix
= _GetUserSidStringFromToken(hToken
);
1787 if (userPrefix
== NULL
)
1794 /* For CSIDL_Type_User we also use the GUID if no szValueName is provided */
1795 szValueName
= CSIDL_Data
[folder
].szValueName
;
1798 StringFromGUID2( CSIDL_Data
[folder
].id
, buffer
, 39 );
1799 szValueName
= &buffer
[0];
1803 hr
= _SHGetUserShellFolderPath(hRootKey
, userPrefix
, szValueName
, pszPath
);
1804 if (FAILED(hr
) && hRootKey
!= HKEY_LOCAL_MACHINE
)
1805 hr
= _SHGetUserShellFolderPath(HKEY_LOCAL_MACHINE
, NULL
, szValueName
, pszPath
);
1807 hr
= _SHGetDefaultValue(folder
, pszPath
);
1809 hr
= _SHGetUserShellFolderPath(hRootKey
, hToken
, userPrefix
, szValueName
, pszPath
);
1810 if (FAILED(hr
) && hRootKey
!= HKEY_LOCAL_MACHINE
)
1811 hr
= _SHGetUserShellFolderPath(HKEY_LOCAL_MACHINE
, hToken
, NULL
, szValueName
, pszPath
);
1813 hr
= _SHGetDefaultValue(hToken
, folder
, pszPath
);
1815 if (userPrefix
!= NULL
&& userPrefix
!= DefaultW
)
1816 LocalFree((HLOCAL
) userPrefix
);
1819 TRACE("returning 0x%08x (output path is %s)\n", hr
, debugstr_w(pszPath
));
1823 /* Gets the (unexpanded) path for the CSIDL with index folder. If dwFlags has
1824 * SHGFP_TYPE_DEFAULT set, calls _SHGetDefaultValue. Otherwise calls
1825 * _SHGetUserShellFolderPath for it, looking only in HKEY_LOCAL_MACHINE.
1826 * If this fails, falls back to _SHGetDefaultValue.
1828 static HRESULT
_SHGetAllUsersProfilePath(DWORD dwFlags
, BYTE folder
,
1833 TRACE("0x%08x,0x%02x,%p\n", dwFlags
, folder
, pszPath
);
1835 if (folder
>= ARRAY_SIZE(CSIDL_Data
))
1836 return E_INVALIDARG
;
1837 if (CSIDL_Data
[folder
].type
!= CSIDL_Type_AllUsers
)
1838 return E_INVALIDARG
;
1840 return E_INVALIDARG
;
1842 if (dwFlags
& SHGFP_TYPE_DEFAULT
)
1844 hr
= _SHGetDefaultValue(folder
, pszPath
);
1846 hr
= _SHGetDefaultValue(NULL
, folder
, pszPath
);
1851 hr
= _SHGetUserShellFolderPath(HKEY_LOCAL_MACHINE
, NULL
,
1853 hr
= _SHGetUserShellFolderPath(HKEY_LOCAL_MACHINE
, NULL
, NULL
,
1855 CSIDL_Data
[folder
].szValueName
, pszPath
);
1858 hr
= _SHGetDefaultValue(folder
, pszPath
);
1860 hr
= _SHGetDefaultValue(NULL
, folder
, pszPath
);
1863 TRACE("returning 0x%08x (output path is %s)\n", hr
, debugstr_w(pszPath
));
1868 static HRESULT
_SHOpenProfilesKey(PHKEY pKey
)
1873 lRet
= RegCreateKeyExW(HKEY_LOCAL_MACHINE
, ProfileListW
, 0, NULL
, 0,
1874 KEY_ALL_ACCESS
, NULL
, pKey
, &disp
);
1875 return HRESULT_FROM_WIN32(lRet
);
1878 /* Reads the value named szValueName from the key profilesKey (assumed to be
1879 * opened by _SHOpenProfilesKey) into szValue, which is assumed to be MAX_PATH
1880 * WCHARs in length. If it doesn't exist, returns szDefault (and saves
1881 * szDefault to the registry).
1883 static HRESULT
_SHGetProfilesValue(HKEY profilesKey
, LPCWSTR szValueName
,
1884 LPWSTR szValue
, LPCWSTR szDefault
)
1887 DWORD type
, dwPathLen
= MAX_PATH
* sizeof(WCHAR
);
1890 TRACE("%p,%s,%p,%s\n", profilesKey
, debugstr_w(szValueName
), szValue
,
1891 debugstr_w(szDefault
));
1892 lRet
= RegQueryValueExW(profilesKey
, szValueName
, NULL
, &type
,
1893 (LPBYTE
)szValue
, &dwPathLen
);
1894 if (!lRet
&& (type
== REG_SZ
|| type
== REG_EXPAND_SZ
) && dwPathLen
1897 dwPathLen
/= sizeof(WCHAR
);
1898 szValue
[dwPathLen
] = '\0';
1903 /* Missing or invalid value, set a default */
1904 lstrcpynW(szValue
, szDefault
, MAX_PATH
);
1905 TRACE("Setting missing value %s to %s\n", debugstr_w(szValueName
),
1906 debugstr_w(szValue
));
1907 lRet
= RegSetValueExW(profilesKey
, szValueName
, 0, REG_EXPAND_SZ
,
1909 (strlenW(szValue
) + 1) * sizeof(WCHAR
));
1911 hr
= HRESULT_FROM_WIN32(lRet
);
1915 TRACE("returning 0x%08x (output value is %s)\n", hr
, debugstr_w(szValue
));
1920 /* Attempts to expand environment variables from szSrc into szDest, which is
1921 * assumed to be MAX_PATH characters in length. Before referring to the
1922 * environment, handles a few variables directly, because the environment
1923 * variables may not be set when this is called (as during Wine's installation
1924 * when default values are being written to the registry).
1925 * The directly handled environment variables, and their source, are:
1926 * - ALLUSERSPROFILE, USERPROFILE: reads from the registry
1927 * - SystemDrive: uses GetSystemDirectoryW and uses the drive portion of its
1929 * If one of the directly handled environment variables is expanded, only
1930 * expands a single variable, and only in the beginning of szSrc.
1933 static HRESULT
_SHExpandEnvironmentStrings(LPCWSTR szSrc
, LPWSTR szDest
)
1935 static HRESULT
_SHExpandEnvironmentStrings(HANDLE hToken
, LPCWSTR szSrc
, LPWSTR szDest
, DWORD cchDest
)
1940 WCHAR szTemp
[MAX_PATH
], szProfilesPrefix
[MAX_PATH
] = { 0 };
1943 WCHAR szTemp
[MAX_PATH
];
1946 TRACE("%s, %p\n", debugstr_w(szSrc
), szDest
);
1948 if (!szSrc
|| !szDest
) return E_INVALIDARG
;
1950 /* short-circuit if there's nothing to expand */
1951 if (szSrc
[0] != '%')
1953 strcpyW(szDest
, szSrc
);
1958 /* Get the profile prefix, we'll probably be needing it */
1959 hr
= _SHOpenProfilesKey(&key
);
1962 WCHAR def_val
[MAX_PATH
];
1964 /* get the system drive */
1965 GetSystemDirectoryW(def_val
, MAX_PATH
);
1966 strcpyW( def_val
+ 3, szDefaultProfileDirW
);
1968 hr
= _SHGetProfilesValue(key
, ProfilesDirectoryW
, szProfilesPrefix
, def_val
);
1975 strcpyW(szTemp
, szSrc
);
1976 while (SUCCEEDED(hr
) && szTemp
[0] == '%')
1978 if (!strncmpiW(szTemp
, AllUsersProfileW
, strlenW(AllUsersProfileW
)))
1981 WCHAR szAllUsers
[MAX_PATH
];
1983 strcpyW(szDest
, szProfilesPrefix
);
1984 hr
= _SHGetProfilesValue(key
, AllUsersProfileValueW
,
1985 szAllUsers
, AllUsersW
);
1986 PathAppendW(szDest
, szAllUsers
);
1988 DWORD cchSize
= cchDest
;
1989 if (!GetAllUsersProfileDirectoryW(szDest
, &cchSize
))
1990 goto fallback_expand
;
1992 PathAppendW(szDest
, szTemp
+ strlenW(AllUsersProfileW
));
1995 else if (!strncmpiW(szTemp
, PublicProfileW
, strlenW(PublicProfileW
)))
1997 WCHAR szAllUsers
[MAX_PATH
], def_val
[MAX_PATH
];
1999 GetSystemDirectoryW(def_val
, MAX_PATH
);
2000 strcpyW( def_val
+ 3, UsersPublicW
);
2002 hr
= _SHGetProfilesValue(key
, PublicW
, szAllUsers
, def_val
);
2003 PathAppendW(szDest
, szAllUsers
);
2004 PathAppendW(szDest
, szTemp
+ strlenW(PublicProfileW
));
2007 else if (!strncmpiW(szTemp
, UserProfileW
, strlenW(UserProfileW
)))
2010 WCHAR userName
[MAX_PATH
];
2011 DWORD userLen
= MAX_PATH
;
2013 strcpyW(szDest
, szProfilesPrefix
);
2014 GetUserNameW(userName
, &userLen
);
2015 PathAppendW(szDest
, userName
);
2017 DWORD cchSize
= cchDest
;
2018 if (!_SHGetUserProfileDirectoryW(hToken
, szDest
, &cchSize
))
2019 goto fallback_expand
;
2021 PathAppendW(szDest
, szTemp
+ strlenW(UserProfileW
));
2023 else if (!strncmpiW(szTemp
, SystemDriveW
, strlenW(SystemDriveW
)))
2026 GetSystemDirectoryW(szDest
, MAX_PATH
);
2028 if (!GetSystemDirectoryW(szDest
, cchDest
))
2029 goto fallback_expand
;
2031 strcpyW(szDest
+ 3, szTemp
+ strlenW(SystemDriveW
) + 1);
2039 DWORD ret
= ExpandEnvironmentStringsW(szTemp
, szDest
, MAX_PATH
);
2041 DWORD ret
= SHExpandEnvironmentStringsForUserW(hToken
, szTemp
, szDest
, cchDest
);
2049 hr
= E_NOT_SUFFICIENT_BUFFER
;
2051 hr
= HRESULT_FROM_WIN32(GetLastError());
2052 else if (!strcmpW( szTemp
, szDest
)) break; /* nothing expanded */
2054 if (SUCCEEDED(hr
)) strcpyW(szTemp
, szDest
);
2061 TRACE("returning 0x%08x (input was %s, output is %s)\n", hr
,
2062 debugstr_w(szSrc
), debugstr_w(szDest
));
2066 /*************************************************************************
2067 * SHGetFolderPathW [SHELL32.@]
2069 * Convert nFolder to path.
2073 * Failure: standard HRESULT error codes.
2076 * Most values can be overridden in either
2077 * HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
2078 * or in the same location in HKLM.
2079 * The "Shell Folders" registry key was used in NT4 and earlier systems.
2080 * Beginning with Windows 2000, the "User Shell Folders" key is used, so
2081 * changes made to it are made to the former key too. This synchronization is
2082 * done on-demand: not until someone requests the value of one of these paths
2083 * (by calling one of the SHGet functions) is the value synchronized.
2084 * Furthermore, the HKCU paths take precedence over the HKLM paths.
2086 HRESULT WINAPI
SHGetFolderPathW(
2087 HWND hwndOwner
, /* [I] owner window */
2088 int nFolder
, /* [I] CSIDL identifying the folder */
2089 HANDLE hToken
, /* [I] access token */
2090 DWORD dwFlags
, /* [I] which path to return */
2091 LPWSTR pszPath
) /* [O] converted path */
2093 HRESULT hr
= SHGetFolderPathAndSubDirW(hwndOwner
, nFolder
, hToken
, dwFlags
, NULL
, pszPath
);
2094 if(HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND
) == hr
)
2095 hr
= HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND
);
2099 HRESULT WINAPI
SHGetFolderPathAndSubDirA(
2100 HWND hwndOwner
, /* [I] owner window */
2101 int nFolder
, /* [I] CSIDL identifying the folder */
2102 HANDLE hToken
, /* [I] access token */
2103 DWORD dwFlags
, /* [I] which path to return */
2104 LPCSTR pszSubPath
, /* [I] sub directory of the specified folder */
2105 LPSTR pszPath
) /* [O] converted path */
2109 LPWSTR pszSubPathW
= NULL
;
2110 LPWSTR pszPathW
= NULL
;
2112 TRACE("%p,%#x,%p,%#x,%s,%p\n", hwndOwner
, nFolder
, hToken
, dwFlags
, debugstr_a(pszSubPath
), pszPath
);
2115 pszPathW
= HeapAlloc(GetProcessHeap(), 0, MAX_PATH
* sizeof(WCHAR
));
2117 hr
= HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY
);
2121 TRACE("%08x,%08x,%s\n",nFolder
, dwFlags
, debugstr_w(pszSubPathW
));
2123 /* SHGetFolderPathAndSubDirW does not distinguish if pszSubPath isn't
2124 * set (null), or an empty string.therefore call it without the parameter set
2125 * if pszSubPath is an empty string
2127 if (pszSubPath
&& pszSubPath
[0]) {
2128 length
= MultiByteToWideChar(CP_ACP
, 0, pszSubPath
, -1, NULL
, 0);
2129 pszSubPathW
= HeapAlloc(GetProcessHeap(), 0, length
* sizeof(WCHAR
));
2131 hr
= HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY
);
2134 MultiByteToWideChar(CP_ACP
, 0, pszSubPath
, -1, pszSubPathW
, length
);
2137 hr
= SHGetFolderPathAndSubDirW(hwndOwner
, nFolder
, hToken
, dwFlags
, pszSubPathW
, pszPathW
);
2139 if (SUCCEEDED(hr
) && pszPath
)
2140 WideCharToMultiByte(CP_ACP
, 0, pszPathW
, -1, pszPath
, MAX_PATH
, NULL
, NULL
);
2143 HeapFree(GetProcessHeap(), 0, pszPathW
);
2144 HeapFree(GetProcessHeap(), 0, pszSubPathW
);
2150 LPCWSTR pszLinkPath
,
2152 LPCWSTR pszArg OPTIONAL
,
2153 LPCWSTR pszDir OPTIONAL
,
2154 LPCWSTR pszIconPath OPTIONAL
,
2155 INT iIconNr OPTIONAL
,
2156 LPCWSTR pszComment OPTIONAL
)
2161 HRESULT hr
= CoCreateInstance(&CLSID_ShellLink
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IShellLink
, (LPVOID
*)&psl
);
2162 if (FAILED_UNEXPECTEDLY(hr
))
2165 hr
= IShellLinkW_SetPath(psl
, pszCmd
);
2166 if (FAILED_UNEXPECTEDLY(hr
))
2168 IShellLinkW_Release(psl
);
2173 hr
= IShellLinkW_SetArguments(psl
, pszArg
);
2176 hr
= IShellLinkW_SetWorkingDirectory(psl
, pszDir
);
2179 hr
= IShellLinkW_SetIconLocation(psl
, pszIconPath
, iIconNr
);
2182 hr
= IShellLinkW_SetDescription(psl
, pszComment
);
2184 hr
= IShellLinkW_QueryInterface(psl
, &IID_IPersistFile
, (LPVOID
*)&ppf
);
2188 hr
= IPersistFile_Save(ppf
, pszLinkPath
, TRUE
);
2189 IPersistFile_Release(ppf
);
2192 IShellLinkW_Release(psl
);
2197 HRESULT
DoCreateSendToFiles(LPCWSTR pszSendTo
)
2199 WCHAR szTarget
[MAX_PATH
];
2200 WCHAR szSendToFile
[MAX_PATH
];
2203 SHGetSpecialFolderPathW(NULL
, szTarget
, CSIDL_MYDOCUMENTS
, TRUE
);
2205 StringCbCopyW(szSendToFile
, sizeof(szSendToFile
), pszSendTo
);
2206 PathAppendW(szSendToFile
, PathFindFileNameW(szTarget
));
2207 StringCbCatW(szSendToFile
, sizeof(szSendToFile
), L
".lnk");
2209 hr
= CreateShellLink(szSendToFile
, szTarget
, NULL
, NULL
, NULL
, -1, NULL
);
2210 if (FAILED_UNEXPECTEDLY(hr
))
2216 /*************************************************************************
2217 * SHGetFolderPathAndSubDirW [SHELL32.@]
2219 HRESULT WINAPI
SHGetFolderPathAndSubDirW(
2220 HWND hwndOwner
, /* [I] owner window */
2221 int nFolder
, /* [I] CSIDL identifying the folder */
2222 HANDLE hToken
, /* [I] access token */
2223 DWORD dwFlags
, /* [I] which path to return */
2224 LPCWSTR pszSubPath
,/* [I] sub directory of the specified folder */
2225 LPWSTR pszPath
) /* [O] converted path */
2228 WCHAR szBuildPath
[MAX_PATH
], szTemp
[MAX_PATH
];
2229 DWORD folder
= nFolder
& CSIDL_FOLDER_MASK
;
2233 TRACE("%p,%#x,%p,%#x,%s,%p\n", hwndOwner
, nFolder
, hToken
, dwFlags
, debugstr_w(pszSubPath
), pszPath
);
2235 /* Windows always NULL-terminates the resulting path regardless of success
2236 * or failure, so do so first
2241 if (folder
>= ARRAY_SIZE(CSIDL_Data
))
2242 return E_INVALIDARG
;
2243 if ((SHGFP_TYPE_CURRENT
!= dwFlags
) && (SHGFP_TYPE_DEFAULT
!= dwFlags
))
2244 return E_INVALIDARG
;
2246 type
= CSIDL_Data
[folder
].type
;
2249 case CSIDL_Type_Disallowed
:
2252 case CSIDL_Type_NonExistent
:
2255 case CSIDL_Type_WindowsPath
:
2256 GetWindowsDirectoryW(szTemp
, MAX_PATH
);
2257 if (CSIDL_Data
[folder
].szDefaultPath
&&
2258 !IS_INTRESOURCE(CSIDL_Data
[folder
].szDefaultPath
) &&
2259 *CSIDL_Data
[folder
].szDefaultPath
)
2261 PathAddBackslashW(szTemp
);
2262 strcatW(szTemp
, CSIDL_Data
[folder
].szDefaultPath
);
2266 case CSIDL_Type_SystemPath
:
2267 GetSystemDirectoryW(szTemp
, MAX_PATH
);
2268 if (CSIDL_Data
[folder
].szDefaultPath
&&
2269 !IS_INTRESOURCE(CSIDL_Data
[folder
].szDefaultPath
) &&
2270 *CSIDL_Data
[folder
].szDefaultPath
)
2272 PathAddBackslashW(szTemp
);
2273 strcatW(szTemp
, CSIDL_Data
[folder
].szDefaultPath
);
2277 case CSIDL_Type_SystemX86Path
:
2278 if (!GetSystemWow64DirectoryW(szTemp
, MAX_PATH
)) GetSystemDirectoryW(szTemp
, MAX_PATH
);
2279 if (CSIDL_Data
[folder
].szDefaultPath
&&
2280 !IS_INTRESOURCE(CSIDL_Data
[folder
].szDefaultPath
) &&
2281 *CSIDL_Data
[folder
].szDefaultPath
)
2283 PathAddBackslashW(szTemp
);
2284 strcatW(szTemp
, CSIDL_Data
[folder
].szDefaultPath
);
2288 case CSIDL_Type_CurrVer
:
2289 hr
= _SHGetCurrentVersionPath(dwFlags
, folder
, szTemp
);
2291 case CSIDL_Type_User
:
2292 hr
= _SHGetUserProfilePath(hToken
, dwFlags
, folder
, szTemp
);
2294 case CSIDL_Type_AllUsers
:
2295 hr
= _SHGetAllUsersProfilePath(dwFlags
, folder
, szTemp
);
2298 FIXME("bogus type %d, please fix\n", type
);
2303 /* Expand environment strings if necessary */
2306 hr
= _SHExpandEnvironmentStrings(szTemp
, szBuildPath
);
2308 hr
= _SHExpandEnvironmentStrings(hToken
, szTemp
, szBuildPath
, _countof(szBuildPath
));
2311 strcpyW(szBuildPath
, szTemp
);
2313 if (FAILED(hr
)) goto end
;
2316 /* make sure the new path does not exceed the buffer length
2317 * and remember to backslash and terminate it */
2318 if(MAX_PATH
< (lstrlenW(szBuildPath
) + lstrlenW(pszSubPath
) + 2)) {
2319 hr
= HRESULT_FROM_WIN32(ERROR_FILENAME_EXCED_RANGE
);
2322 PathAppendW(szBuildPath
, pszSubPath
);
2323 PathRemoveBackslashW(szBuildPath
);
2325 /* Copy the path if it's available before we might return */
2326 if (SUCCEEDED(hr
) && pszPath
)
2327 strcpyW(pszPath
, szBuildPath
);
2329 /* if we don't care about existing directories we are ready */
2330 if(nFolder
& CSIDL_FLAG_DONT_VERIFY
) goto end
;
2332 if (PathFileExistsW(szBuildPath
)) goto end
;
2334 /* not existing but we are not allowed to create it. The return value
2335 * is verified against shell32 version 6.0.
2337 if (!(nFolder
& CSIDL_FLAG_CREATE
))
2339 hr
= HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND
);
2343 /* create directory/directories */
2344 ret
= SHCreateDirectoryExW(hwndOwner
, szBuildPath
, NULL
);
2345 if (ret
&& ret
!= ERROR_ALREADY_EXISTS
)
2347 ERR("Failed to create directory %s.\n", debugstr_w(szBuildPath
));
2352 TRACE("Created missing system directory %s\n", debugstr_w(szBuildPath
));
2354 /* create desktop.ini for custom icon */
2355 if (CSIDL_Data
[folder
].nShell32IconIndex
)
2357 static const WCHAR s_szFormat
[] = L
"%%SystemRoot%%\\system32\\shell32.dll,%d";
2358 WCHAR szIconLocation
[MAX_PATH
];
2361 /* make the directory a system folder */
2362 dwAttributes
= GetFileAttributesW(szBuildPath
);
2363 dwAttributes
|= FILE_ATTRIBUTE_SYSTEM
;
2364 SetFileAttributesW(szBuildPath
, dwAttributes
);
2366 /* build the desktop.ini file path */
2367 PathAppendW(szBuildPath
, L
"desktop.ini");
2369 /* build the icon location */
2370 StringCchPrintfW(szIconLocation
, _countof(szIconLocation
), s_szFormat
,
2371 CSIDL_Data
[folder
].nShell32IconIndex
);
2373 /* write desktop.ini */
2374 WritePrivateProfileStringW(L
".ShellClassInfo", L
"IconResource", szIconLocation
, szBuildPath
);
2377 WritePrivateProfileStringW(NULL
, NULL
, NULL
, szBuildPath
);
2379 /* make the desktop.ini a system and hidden file */
2380 dwAttributes
= GetFileAttributesW(szBuildPath
);
2381 dwAttributes
|= FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
;
2382 SetFileAttributesW(szBuildPath
, dwAttributes
);
2386 if (folder
== CSIDL_SENDTO
)
2388 if (PathIsDirectoryEmptyW(szBuildPath
))
2389 DoCreateSendToFiles(szBuildPath
);
2391 TRACE("returning 0x%08x (final path is %s)\n", hr
, debugstr_w(szBuildPath
));
2395 /*************************************************************************
2396 * SHGetFolderPathA [SHELL32.@]
2398 * See SHGetFolderPathW.
2400 HRESULT WINAPI
SHGetFolderPathA(
2407 WCHAR szTemp
[MAX_PATH
];
2410 TRACE("%p,%d,%p,%#x,%p\n", hwndOwner
, nFolder
, hToken
, dwFlags
, pszPath
);
2414 hr
= SHGetFolderPathW(hwndOwner
, nFolder
, hToken
, dwFlags
, szTemp
);
2415 if (SUCCEEDED(hr
) && pszPath
)
2416 WideCharToMultiByte(CP_ACP
, 0, szTemp
, -1, pszPath
, MAX_PATH
, NULL
,
2422 /* For each folder in folders, if its value has not been set in the registry,
2423 * calls _SHGetUserProfilePath or _SHGetAllUsersProfilePath (depending on the
2424 * folder's type) to get the unexpanded value first.
2425 * Writes the unexpanded value to User Shell Folders, and queries it with
2426 * SHGetFolderPathW to force the creation of the directory if it doesn't
2427 * already exist. SHGetFolderPathW also returns the expanded value, which
2428 * this then writes to Shell Folders.
2430 static HRESULT
_SHRegisterFolders(HKEY hRootKey
, HANDLE hToken
,
2431 LPCWSTR szUserShellFolderPath
, LPCWSTR szShellFolderPath
, const UINT folders
[],
2434 const WCHAR
*szValueName
;
2437 WCHAR path
[MAX_PATH
];
2439 HKEY hUserKey
= NULL
, hKey
= NULL
;
2440 DWORD dwType
, dwPathLen
;
2443 TRACE("%p,%p,%s,%p,%u\n", hRootKey
, hToken
,
2444 debugstr_w(szUserShellFolderPath
), folders
, foldersLen
);
2446 ret
= RegCreateKeyW(hRootKey
, szUserShellFolderPath
, &hUserKey
);
2448 hr
= HRESULT_FROM_WIN32(ret
);
2451 ret
= RegCreateKeyW(hRootKey
, szShellFolderPath
, &hKey
);
2453 hr
= HRESULT_FROM_WIN32(ret
);
2455 for (i
= 0; SUCCEEDED(hr
) && i
< foldersLen
; i
++)
2457 dwPathLen
= MAX_PATH
* sizeof(WCHAR
);
2459 /* For CSIDL_Type_User we also use the GUID if no szValueName is provided */
2460 szValueName
= CSIDL_Data
[folders
[i
]].szValueName
;
2461 if (!szValueName
&& CSIDL_Data
[folders
[i
]].type
== CSIDL_Type_User
)
2463 StringFromGUID2( CSIDL_Data
[folders
[i
]].id
, buffer
, 39 );
2464 szValueName
= &buffer
[0];
2467 if (RegQueryValueExW(hUserKey
, szValueName
, NULL
,
2468 &dwType
, (LPBYTE
)path
, &dwPathLen
) || (dwType
!= REG_SZ
&&
2469 dwType
!= REG_EXPAND_SZ
))
2472 if (CSIDL_Data
[folders
[i
]].type
== CSIDL_Type_User
)
2473 _SHGetUserProfilePath(hToken
, SHGFP_TYPE_DEFAULT
, folders
[i
],
2475 else if (CSIDL_Data
[folders
[i
]].type
== CSIDL_Type_AllUsers
)
2476 _SHGetAllUsersProfilePath(SHGFP_TYPE_DEFAULT
, folders
[i
], path
);
2477 else if (CSIDL_Data
[folders
[i
]].type
== CSIDL_Type_WindowsPath
)
2479 GetWindowsDirectoryW(path
, MAX_PATH
);
2480 if (CSIDL_Data
[folders
[i
]].szDefaultPath
&&
2481 !IS_INTRESOURCE(CSIDL_Data
[folders
[i
]].szDefaultPath
))
2483 PathAddBackslashW(path
);
2484 strcatW(path
, CSIDL_Data
[folders
[i
]].szDefaultPath
);
2491 ret
= RegSetValueExW(hUserKey
, szValueName
, 0, REG_EXPAND_SZ
,
2492 (LPBYTE
)path
, (strlenW(path
) + 1) * sizeof(WCHAR
));
2494 hr
= HRESULT_FROM_WIN32(ret
);
2497 hr
= SHGetFolderPathW(NULL
, folders
[i
] | CSIDL_FLAG_CREATE
,
2498 hToken
, SHGFP_TYPE_DEFAULT
, path
);
2499 ret
= RegSetValueExW(hKey
, szValueName
, 0, REG_SZ
,
2500 (LPBYTE
)path
, (strlenW(path
) + 1) * sizeof(WCHAR
));
2502 hr
= HRESULT_FROM_WIN32(ret
);
2508 RegCloseKey(hUserKey
);
2512 TRACE("returning 0x%08x\n", hr
);
2516 static HRESULT
_SHRegisterUserShellFolders(BOOL bDefault
)
2518 static const UINT folders
[] = {
2529 CSIDL_DESKTOPDIRECTORY
,
2533 CSIDL_LOCAL_APPDATA
,
2534 CSIDL_INTERNET_CACHE
,
2540 /* Cannot use #if _WIN32_WINNT >= 0x0600 because _WIN32_WINNT == 0x0600 here. */
2545 CSIDL_APPDATA_LOCALLOW
,
2550 WCHAR userShellFolderPath
[MAX_PATH
], shellFolderPath
[MAX_PATH
];
2551 LPCWSTR pUserShellFolderPath
, pShellFolderPath
;
2556 TRACE("%s\n", bDefault
? "TRUE" : "FALSE");
2559 hToken
= (HANDLE
)-1;
2560 hRootKey
= HKEY_USERS
;
2561 strcpyW(userShellFolderPath
, DefaultW
);
2562 PathAddBackslashW(userShellFolderPath
);
2563 strcatW(userShellFolderPath
, szSHUserFolders
);
2564 pUserShellFolderPath
= userShellFolderPath
;
2565 strcpyW(shellFolderPath
, DefaultW
);
2566 PathAddBackslashW(shellFolderPath
);
2567 strcatW(shellFolderPath
, szSHFolders
);
2568 pShellFolderPath
= shellFolderPath
;
2573 hRootKey
= HKEY_CURRENT_USER
;
2574 pUserShellFolderPath
= szSHUserFolders
;
2575 pShellFolderPath
= szSHFolders
;
2578 hr
= _SHRegisterFolders(hRootKey
, hToken
, pUserShellFolderPath
,
2579 pShellFolderPath
, folders
, ARRAY_SIZE(folders
));
2580 TRACE("returning 0x%08x\n", hr
);
2584 static HRESULT
_SHRegisterCommonShellFolders(void)
2586 static const UINT folders
[] = {
2587 CSIDL_COMMON_STARTMENU
,
2588 CSIDL_COMMON_PROGRAMS
,
2589 CSIDL_COMMON_STARTUP
,
2590 CSIDL_COMMON_DESKTOPDIRECTORY
,
2591 CSIDL_COMMON_FAVORITES
,
2592 CSIDL_COMMON_APPDATA
,
2593 CSIDL_COMMON_TEMPLATES
,
2594 CSIDL_COMMON_DOCUMENTS
,
2595 CSIDL_COMMON_ADMINTOOLS
,
2597 CSIDL_COMMON_PICTURES
,
2603 hr
= _SHRegisterFolders(HKEY_LOCAL_MACHINE
, NULL
, szSHUserFolders
,
2604 szSHFolders
, folders
, ARRAY_SIZE(folders
));
2605 TRACE("returning 0x%08x\n", hr
);
2609 /* Register the default values in the registry, as some apps seem to depend
2610 * on their presence. The set registered was taken from Windows XP.
2612 HRESULT
SHELL_RegisterShellFolders(void)
2616 hr
= _SHRegisterUserShellFolders(TRUE
);
2618 hr
= _SHRegisterUserShellFolders(FALSE
);
2620 hr
= _SHRegisterCommonShellFolders();
2624 /*************************************************************************
2625 * SHGetSpecialFolderPathA [SHELL32.@]
2627 BOOL WINAPI
SHGetSpecialFolderPathA (
2633 return SHGetFolderPathA(hwndOwner
, nFolder
+ (bCreate
? CSIDL_FLAG_CREATE
: 0), NULL
, 0,
2637 /*************************************************************************
2638 * SHGetSpecialFolderPathW
2640 BOOL WINAPI
SHGetSpecialFolderPathW (
2646 return SHGetFolderPathW(hwndOwner
, nFolder
+ (bCreate
? CSIDL_FLAG_CREATE
: 0), NULL
, 0,
2650 /*************************************************************************
2651 * SHGetFolderLocation [SHELL32.@]
2653 * Gets the folder locations from the registry and creates a pidl.
2657 * nFolder [I] CSIDL_xxxxx
2658 * hToken [I] token representing user, or NULL for current user, or -1 for
2660 * dwReserved [I] must be zero
2661 * ppidl [O] PIDL of a special folder
2665 * Failure: Standard OLE-defined error result, S_FALSE or E_INVALIDARG
2668 * Creates missing reg keys and directories.
2669 * Mostly forwards to SHGetFolderPathW, but a few values of nFolder return
2670 * virtual folders that are handled here.
2672 HRESULT WINAPI
SHGetFolderLocation(
2677 LPITEMIDLIST
*ppidl
)
2679 HRESULT hr
= E_INVALIDARG
;
2681 TRACE("%p 0x%08x %p 0x%08x %p\n",
2682 hwndOwner
, nFolder
, hToken
, dwReserved
, ppidl
);
2685 return E_INVALIDARG
;
2687 return E_INVALIDARG
;
2689 /* The virtual folders' locations are not user-dependent */
2691 switch (nFolder
& CSIDL_FOLDER_MASK
)
2694 *ppidl
= _ILCreateDesktop();
2697 case CSIDL_PERSONAL
:
2698 *ppidl
= _ILCreateMyDocuments();
2701 case CSIDL_INTERNET
:
2702 *ppidl
= _ILCreateIExplore();
2705 case CSIDL_CONTROLS
:
2706 *ppidl
= _ILCreateControlPanel();
2709 case CSIDL_PRINTERS
:
2710 *ppidl
= _ILCreatePrinters();
2713 case CSIDL_BITBUCKET
:
2714 *ppidl
= _ILCreateBitBucket();
2718 *ppidl
= _ILCreateMyComputer();
2722 *ppidl
= _ILCreateNetwork();
2727 WCHAR szPath
[MAX_PATH
];
2729 hr
= SHGetFolderPathW(hwndOwner
, nFolder
, hToken
,
2730 SHGFP_TYPE_CURRENT
, szPath
);
2735 TRACE("Value=%s\n", debugstr_w(szPath
));
2736 hr
= SHILCreateFromPathW(szPath
, ppidl
, &attributes
);
2738 else if (hr
== HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND
))
2740 /* unlike SHGetFolderPath, SHGetFolderLocation in shell32
2741 * version 6.0 returns E_FAIL for nonexistent paths
2750 TRACE("-- (new pidl %p)\n",*ppidl
);
2754 /*************************************************************************
2755 * SHGetSpecialFolderLocation [SHELL32.@]
2758 * In NT5, SHGetSpecialFolderLocation needs the <winntdir>/Recent
2761 HRESULT WINAPI
SHGetSpecialFolderLocation(
2764 LPITEMIDLIST
* ppidl
)
2766 HRESULT hr
= E_INVALIDARG
;
2768 TRACE("(%p,0x%x,%p)\n", hwndOwner
,nFolder
,ppidl
);
2771 return E_INVALIDARG
;
2773 hr
= SHGetFolderLocation(hwndOwner
, nFolder
, NULL
, 0, ppidl
);