2 * Implementation of userenv.dll
4 * Copyright 2006 Mike McCormack for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #define WIN32_NO_STATUS
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL( userenv
);
38 static BOOL
get_reg_value(WCHAR
*env
, HKEY hkey
, const WCHAR
*name
, WCHAR
*val
, DWORD size
)
40 DWORD type
, res_size
=0;
42 if (RegQueryValueExW(hkey
, name
, 0, &type
, NULL
, &res_size
) != ERROR_SUCCESS
)
50 return RegQueryValueExW(hkey
, name
, 0, NULL
, (BYTE
*)val
, &size
) == ERROR_SUCCESS
;
52 else if (type
== REG_EXPAND_SZ
)
54 UNICODE_STRING us_buf
, us_expanded
;
55 WCHAR
*buf
= HeapAlloc(GetProcessHeap(), 0, res_size
);
59 if (RegQueryValueExW(hkey
, name
, 0, NULL
, (BYTE
*)buf
, &res_size
) != ERROR_SUCCESS
)
61 HeapFree(GetProcessHeap(), 0, buf
);
65 RtlInitUnicodeString(&us_buf
, buf
);
66 us_expanded
.Buffer
= val
;
67 us_expanded
.MaximumLength
= size
;
68 if (RtlExpandEnvironmentStrings_U(env
, &us_buf
, &us_expanded
, &size
) != STATUS_SUCCESS
)
70 HeapFree(GetProcessHeap(), 0, buf
);
74 HeapFree(GetProcessHeap(), 0, buf
);
81 static void set_env_var( WCHAR
**env
, const WCHAR
*name
, const WCHAR
*val
)
83 UNICODE_STRING nameW
, valW
;
85 RtlInitUnicodeString( &nameW
, name
);
86 RtlInitUnicodeString( &valW
, val
);
87 RtlSetEnvironmentVariable( env
, &nameW
, &valW
);
90 static void set_registry_variables(WCHAR
**env
, HKEY hkey
, DWORD type
, BOOL set_path
)
92 UNICODE_STRING us_name
, us_value
;
93 WCHAR name
[1024], value
[1024];
94 DWORD ret
, index
, size
;
96 for (index
= 0; ; index
++)
98 size
= ARRAY_SIZE(name
);
99 ret
= RegEnumValueW(hkey
, index
, name
, &size
, NULL
, NULL
, NULL
, NULL
);
100 if (ret
!= ERROR_SUCCESS
)
103 if (!wcsicmp(name
, L
"SystemRoot")) continue;
104 if (!wcsicmp(name
, L
"SystemDrive")) continue;
106 RtlInitUnicodeString(&us_name
, name
);
107 us_value
.Buffer
= value
;
108 us_value
.MaximumLength
= sizeof(value
);
109 if (!wcsicmp(name
, L
"PATH") &&
110 !RtlQueryEnvironmentVariable_U(*env
, &us_name
, &us_value
))
115 size
= lstrlenW(value
)+1;
116 if (!get_reg_value(*env
, hkey
, name
, value
+size
,
117 sizeof(value
)-size
*sizeof(WCHAR
)))
121 set_env_var(env
, name
, value
);
125 if (get_reg_value(*env
, hkey
, name
, value
, sizeof(value
)) && value
[0])
126 set_env_var(env
, name
, value
);
130 static void set_wow64_environment(WCHAR
**env
)
134 BOOL is_win64
= (sizeof(void *) > sizeof(int));
137 IsWow64Process( GetCurrentProcess(), &is_wow64
);
139 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE
, L
"Software\\Microsoft\\Windows\\CurrentVersion",
140 0, KEY_READ
|KEY_WOW64_64KEY
, &hkey
))
143 /* set the ProgramFiles variables */
145 if (get_reg_value(*env
, hkey
, L
"ProgramFilesDir", buf
, sizeof(buf
)))
147 if (is_win64
|| is_wow64
) set_env_var(env
, L
"ProgramW6432", buf
);
148 if (is_win64
|| !is_wow64
) set_env_var(env
, L
"ProgramFiles", buf
);
150 if (get_reg_value(*env
, hkey
, L
"ProgramFilesDir (x86)", buf
, sizeof(buf
)))
152 if (is_win64
|| is_wow64
) set_env_var(env
, L
"ProgramFiles(x86)", buf
);
153 if (is_wow64
) set_env_var(env
, L
"ProgramFiles", buf
);
156 /* set the CommonProgramFiles variables */
158 if (get_reg_value(*env
, hkey
, L
"CommonFilesDir", buf
, sizeof(buf
)))
160 if (is_win64
|| is_wow64
) set_env_var(env
, L
"CommonProgramW6432", buf
);
161 if (is_win64
|| !is_wow64
) set_env_var(env
, L
"CommonProgramFiles", buf
);
163 if (get_reg_value(*env
, hkey
, L
"CommonFilesDir (x86)", buf
, sizeof(buf
)))
165 if (is_win64
|| is_wow64
) set_env_var(env
, L
"CommonProgramFiles(x86)", buf
);
166 if (is_wow64
) set_env_var(env
, L
"CommonProgramFiles", buf
);
172 BOOL WINAPI
CreateEnvironmentBlock( LPVOID
* lpEnvironment
,
173 HANDLE hToken
, BOOL bInherit
)
175 static const WCHAR env_keyW
[] = L
"System\\CurrentControlSet\\Control\\Session Manager\\Environment";
176 static const WCHAR profile_keyW
[] = L
"Software\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList";
178 WCHAR
*env
, buf
[UNICODE_STRING_MAX_CHARS
], profiles_dir
[MAX_PATH
];
182 TRACE("%p %p %d\n", lpEnvironment
, hToken
, bInherit
);
187 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE
, env_keyW
, 0, KEY_READ
, &hkey
) != ERROR_SUCCESS
)
190 if (RtlCreateEnvironment(bInherit
, &env
) != STATUS_SUCCESS
)
196 if (!GetEnvironmentVariableW(L
"SystemRoot", buf
, UNICODE_STRING_MAX_CHARS
))
198 if (!get_reg_value(env
, hkey
, L
"SystemRoot", buf
, UNICODE_STRING_MAX_CHARS
))
201 WARN("SystemRoot variable not set\n");
204 set_env_var(&env
, L
"SystemRoot", buf
);
206 if (!GetEnvironmentVariableW(L
"SystemDrive", buf
, UNICODE_STRING_MAX_CHARS
))
208 if (!get_reg_value(env
, hkey
, L
"SystemDrive", buf
, UNICODE_STRING_MAX_CHARS
))
211 WARN("SystemDrive variable not set\n");
214 set_env_var(&env
, L
"SystemDrive", buf
);
216 set_registry_variables(&env
, hkey
, REG_SZ
, !bInherit
);
217 set_registry_variables(&env
, hkey
, REG_EXPAND_SZ
, !bInherit
);
219 if (RegOpenKeyExW(hkey
, L
"Environment", 0, KEY_READ
, &hsubkey
) == ERROR_SUCCESS
)
221 set_registry_variables(&env
, hsubkey
, REG_SZ
, !bInherit
);
222 set_registry_variables(&env
, hsubkey
, REG_EXPAND_SZ
, !bInherit
);
223 RegCloseKey(hsubkey
);
226 if (RegOpenKeyExW(hkey
, L
"Volatile Environment", 0, KEY_READ
, &hsubkey
) == ERROR_SUCCESS
)
228 set_registry_variables(&env
, hsubkey
, REG_SZ
, !bInherit
);
229 set_registry_variables(&env
, hsubkey
, REG_EXPAND_SZ
, !bInherit
);
230 RegCloseKey(hsubkey
);
234 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE
, profile_keyW
, 0, KEY_READ
, &hkey
) == ERROR_SUCCESS
)
236 if (get_reg_value(env
, hkey
, L
"ProfilesDirectory", profiles_dir
, MAX_PATH
- sizeof(WCHAR
)))
238 len
= lstrlenW(profiles_dir
);
239 if (profiles_dir
[len
-1] != '\\')
241 profiles_dir
[len
++] = '\\';
242 profiles_dir
[len
] = '\0';
245 if (get_reg_value(env
, hkey
, L
"Public", buf
, UNICODE_STRING_MAX_CHARS
))
247 set_env_var(&env
, L
"ALLUSERSPROFILE", buf
);
248 set_env_var(&env
, L
"ProgramData", buf
);
259 len
= ARRAY_SIZE(buf
);
260 if (GetComputerNameW(buf
, &len
))
261 set_env_var(&env
, L
"COMPUTERNAME", buf
);
263 set_wow64_environment(&env
);
269 len
= lstrlenW(profiles_dir
);
270 if (len
* sizeof(WCHAR
) + sizeof(L
"Default") < sizeof(buf
))
272 wcscpy(buf
, profiles_dir
);
273 wcscat(buf
, L
"Default");
274 set_env_var(&env
, L
"USERPROFILE", buf
);
278 wcscpy(buf
, L
".Default");
282 TOKEN_USER
*token_user
= NULL
;
287 if (GetTokenInformation(hToken
, TokenUser
, NULL
, 0, &len
) ||
288 GetLastError()!=ERROR_INSUFFICIENT_BUFFER
||
289 !(token_user
= HeapAlloc(GetProcessHeap(), 0, len
)) ||
290 !GetTokenInformation(hToken
, TokenUser
, token_user
, len
, &len
) ||
291 !ConvertSidToStringSidW(token_user
->User
.Sid
, &sidW
))
293 HeapFree(GetProcessHeap(), 0, token_user
);
294 RtlDestroyEnvironment(env
);
298 len
= lstrlenW(profiles_dir
);
299 memcpy(buf
, profiles_dir
, len
*sizeof(WCHAR
));
301 size
= UNICODE_STRING_MAX_CHARS
-len
;
302 if (LookupAccountSidW(NULL
, token_user
->User
.Sid
,
303 buf
+len
, &size
, NULL
, &tmp
, &use
))
305 set_env_var(&env
, L
"USERNAME", buf
+len
);
306 if (len
) set_env_var(&env
, L
"USERPROFILE", buf
);
309 HeapFree(GetProcessHeap(), 0, token_user
);
314 if (RegOpenKeyExW(HKEY_USERS
, buf
, 0, KEY_READ
, &hkey
) == ERROR_SUCCESS
)
316 if (RegOpenKeyExW(hkey
, L
"Environment", 0, KEY_READ
, &hsubkey
) == ERROR_SUCCESS
)
318 set_registry_variables(&env
, hsubkey
, REG_SZ
, !bInherit
);
319 set_registry_variables(&env
, hsubkey
, REG_EXPAND_SZ
, !bInherit
);
320 RegCloseKey(hsubkey
);
323 if (RegOpenKeyExW(hkey
, L
"Volatile Environment", 0, KEY_READ
, &hsubkey
) == ERROR_SUCCESS
)
325 set_registry_variables(&env
, hsubkey
, REG_SZ
, !bInherit
);
326 set_registry_variables(&env
, hsubkey
, REG_EXPAND_SZ
, !bInherit
);
327 RegCloseKey(hsubkey
);
332 *lpEnvironment
= env
;
336 BOOL WINAPI
DestroyEnvironmentBlock(LPVOID lpEnvironment
)
340 TRACE("%p\n", lpEnvironment
);
341 r
= RtlDestroyEnvironment(lpEnvironment
);
342 if (r
== STATUS_SUCCESS
)
347 BOOL WINAPI
ExpandEnvironmentStringsForUserA( HANDLE hToken
, LPCSTR lpSrc
,
348 LPSTR lpDest
, DWORD dwSize
)
352 TRACE("%p %s %p %ld\n", hToken
, debugstr_a(lpSrc
), lpDest
, dwSize
);
354 ret
= ExpandEnvironmentStringsA( lpSrc
, lpDest
, dwSize
);
355 TRACE("<- %s\n", debugstr_a(lpDest
));
359 BOOL WINAPI
ExpandEnvironmentStringsForUserW( HANDLE hToken
, LPCWSTR lpSrc
,
360 LPWSTR lpDest
, DWORD dwSize
)
364 TRACE("%p %s %p %ld\n", hToken
, debugstr_w(lpSrc
), lpDest
, dwSize
);
366 ret
= ExpandEnvironmentStringsW( lpSrc
, lpDest
, dwSize
);
367 TRACE("<- %s\n", debugstr_w(lpDest
));
371 BOOL WINAPI
GetDefaultUserProfileDirectoryA( LPSTR lpProfileDir
, LPDWORD lpcchSize
)
373 FIXME("%p %p\n", lpProfileDir
, lpcchSize
);
377 BOOL WINAPI
GetDefaultUserProfileDirectoryW( LPWSTR lpProfileDir
, LPDWORD lpcchSize
)
379 FIXME("%p %p\n", lpProfileDir
, lpcchSize
);
383 BOOL WINAPI
GetUserProfileDirectoryA( HANDLE hToken
, LPSTR lpProfileDir
,
389 TRACE( "%p %p %p\n", hToken
, lpProfileDir
, lpcchSize
);
391 if (!lpProfileDir
|| !lpcchSize
)
393 SetLastError( ERROR_INVALID_PARAMETER
);
396 if (!(dirW
= HeapAlloc( GetProcessHeap(), 0, *lpcchSize
* sizeof(WCHAR
) )))
399 if ((ret
= GetUserProfileDirectoryW( hToken
, dirW
, lpcchSize
)))
400 WideCharToMultiByte( CP_ACP
, 0, dirW
, *lpcchSize
, lpProfileDir
, *lpcchSize
, NULL
, NULL
);
402 HeapFree( GetProcessHeap(), 0, dirW
);
406 BOOL WINAPI
GetUserProfileDirectoryW( HANDLE hToken
, LPWSTR lpProfileDir
,
410 WCHAR
*userW
= NULL
, *dirW
= NULL
;
411 DWORD len
, dir_len
, domain_len
;
415 TRACE( "%p %p %p\n", hToken
, lpProfileDir
, lpcchSize
);
419 SetLastError( ERROR_INVALID_PARAMETER
);
424 GetTokenInformation( hToken
, TokenUser
, NULL
, 0, &len
);
425 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER
) return FALSE
;
426 if (!(t
= HeapAlloc( GetProcessHeap(), 0, len
))) return FALSE
;
427 if (!GetTokenInformation( hToken
, TokenUser
, t
, len
, &len
)) goto done
;
429 len
= domain_len
= 0;
430 LookupAccountSidW( NULL
, t
->User
.Sid
, NULL
, &len
, NULL
, &domain_len
, NULL
);
431 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER
) goto done
;
432 if (!(userW
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) ))) goto done
;
433 if (!LookupAccountSidW( NULL
, t
->User
.Sid
, userW
, &len
, NULL
, &domain_len
, &use
)) goto done
;
436 GetProfilesDirectoryW( NULL
, &dir_len
);
437 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER
) goto done
;
438 if (!(dirW
= HeapAlloc( GetProcessHeap(), 0, (dir_len
+ 1) * sizeof(WCHAR
) ))) goto done
;
439 if (!GetProfilesDirectoryW( dirW
, &dir_len
)) goto done
;
442 if (*lpcchSize
< len
)
444 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
448 lstrcpyW( lpProfileDir
, dirW
);
449 lstrcatW( lpProfileDir
, L
"\\" );
450 lstrcatW( lpProfileDir
, userW
);
455 HeapFree( GetProcessHeap(), 0, t
);
456 HeapFree( GetProcessHeap(), 0, userW
);
457 HeapFree( GetProcessHeap(), 0, dirW
);
461 static const char ProfileListA
[] = "Software\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList";
463 BOOL WINAPI
GetProfilesDirectoryA( LPSTR lpProfilesDir
, LPDWORD lpcchSize
)
465 static const char ProfilesDirectory
[] = "ProfilesDirectory";
469 DWORD len
= 0, expanded_len
;
470 LPSTR unexpanded_profiles_dir
= NULL
;
472 TRACE("%p %p\n", lpProfilesDir
, lpcchSize
);
474 if (!lpProfilesDir
|| !lpcchSize
)
476 SetLastError(ERROR_INVALID_PARAMETER
);
480 l
= RegOpenKeyExA(HKEY_LOCAL_MACHINE
, ProfileListA
, 0, KEY_READ
, &key
);
486 l
= RegQueryValueExA(key
, ProfilesDirectory
, NULL
, NULL
, NULL
, &len
);
492 unexpanded_profiles_dir
= HeapAlloc(GetProcessHeap(), 0, len
);
493 if (!unexpanded_profiles_dir
)
495 SetLastError(ERROR_OUTOFMEMORY
);
498 l
= RegQueryValueExA(key
, ProfilesDirectory
, NULL
, NULL
,
499 (BYTE
*)unexpanded_profiles_dir
, &len
);
505 expanded_len
= ExpandEnvironmentStringsA(unexpanded_profiles_dir
, NULL
, 0);
506 /* The returned length doesn't include the NULL terminator. */
507 if (*lpcchSize
< expanded_len
- 1)
509 *lpcchSize
= expanded_len
- 1;
510 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
513 *lpcchSize
= expanded_len
- 1;
514 /* The return value is also the expected length. */
515 ret
= ExpandEnvironmentStringsA(unexpanded_profiles_dir
, lpProfilesDir
,
518 HeapFree(GetProcessHeap(), 0, unexpanded_profiles_dir
);
523 BOOL WINAPI
GetProfilesDirectoryW( LPWSTR lpProfilesDir
, LPDWORD lpcchSize
)
528 DWORD len
= 0, expanded_len
;
529 LPWSTR unexpanded_profiles_dir
= NULL
;
531 TRACE("%p %p\n", lpProfilesDir
, lpcchSize
);
535 SetLastError(ERROR_INVALID_PARAMETER
);
539 l
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
540 L
"Software\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList",
547 l
= RegQueryValueExW(key
, L
"ProfilesDirectory", NULL
, NULL
, NULL
, &len
);
553 unexpanded_profiles_dir
= HeapAlloc(GetProcessHeap(), 0, len
);
554 if (!unexpanded_profiles_dir
)
556 SetLastError(ERROR_OUTOFMEMORY
);
559 l
= RegQueryValueExW(key
, L
"ProfilesDirectory", NULL
, NULL
,
560 (BYTE
*)unexpanded_profiles_dir
, &len
);
566 expanded_len
= ExpandEnvironmentStringsW(unexpanded_profiles_dir
, NULL
, 0);
567 /* The returned length doesn't include the NULL terminator. */
568 if (*lpcchSize
< expanded_len
- 1 || !lpProfilesDir
)
570 *lpcchSize
= expanded_len
- 1;
571 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
574 *lpcchSize
= expanded_len
- 1;
575 /* The return value is also the expected length. */
576 ret
= ExpandEnvironmentStringsW(unexpanded_profiles_dir
, lpProfilesDir
,
579 HeapFree(GetProcessHeap(), 0, unexpanded_profiles_dir
);
584 BOOL WINAPI
GetAllUsersProfileDirectoryA( LPSTR lpProfileDir
, LPDWORD lpcchSize
)
586 FIXME("%p %p\n", lpProfileDir
, lpcchSize
);
590 BOOL WINAPI
GetAllUsersProfileDirectoryW( LPWSTR lpProfileDir
, LPDWORD lpcchSize
)
592 FIXME("%p %p\n", lpProfileDir
, lpcchSize
);
596 BOOL WINAPI
GetProfileType( DWORD
*pdwFlags
)
598 FIXME("%p\n", pdwFlags
);
603 BOOL WINAPI
LoadUserProfileA( HANDLE hToken
, LPPROFILEINFOA lpProfileInfo
)
605 FIXME("%p %p\n", hToken
, lpProfileInfo
);
606 lpProfileInfo
->hProfile
= HKEY_CURRENT_USER
;
610 BOOL WINAPI
LoadUserProfileW( HANDLE hToken
, LPPROFILEINFOW lpProfileInfo
)
612 FIXME("%p %p\n", hToken
, lpProfileInfo
);
613 lpProfileInfo
->hProfile
= HKEY_CURRENT_USER
;
617 BOOL WINAPI
RegisterGPNotification( HANDLE event
, BOOL machine
)
619 FIXME("%p %d\n", event
, machine
);
623 BOOL WINAPI
UnregisterGPNotification( HANDLE event
)
625 FIXME("%p\n", event
);
629 BOOL WINAPI
UnloadUserProfile( HANDLE hToken
, HANDLE hProfile
)
631 FIXME("(%p, %p): stub\n", hToken
, hProfile
);
635 HANDLE WINAPI
EnterCriticalPolicySection(BOOL bMachine
)
637 FIXME("(%x)\n", bMachine
);
638 SetLastError(ERROR_ACCESS_DENIED
);
642 BOOL WINAPI
LeaveCriticalPolicySection(HANDLE hSection
)
644 FIXME("(%p)\n", hSection
);
648 DWORD WINAPI
GetAppliedGPOListW(DWORD dwFlags
, LPCWSTR pMachineName
, PSID pSidUser
, GUID
*pGuidExtension
,
649 PGROUP_POLICY_OBJECTW
*ppGPOList
)
651 FIXME("(%lx %s %p %s %p)\n", dwFlags
, debugstr_w(pMachineName
), pSidUser
, debugstr_guid(pGuidExtension
), ppGPOList
);
652 return ERROR_ACCESS_DENIED
;
655 /******************************************************************************
661 * int csidl [in] well-known directory location to create link in
662 * LPCSTR lnk_dir [in] directory (relative to directory specified by csidl) to create link in
663 * LPCSTR lnk_filename [in] filename of the link file without .lnk extension
664 * LPCSTR lnk_target [in] file/directory pointed to by link
665 * LPCSTR lnk_iconfile [in] link icon resource filename
666 * DWORD lnk_iconid [in] link icon resource id in file referred by lnk_iconfile
667 * LPCSTR work_directory [in] link target's work directory
668 * WORD hotkey [in] link hotkey (virtual key id)
669 * DWORD win_state [in] initial window size (SW_SHOWMAXIMIZED to start maximized,
670 * SW_SHOWMINNOACTIVE to start minimized, everything else is default state)
671 * LPCSTR comment [in] comment - link's comment
672 * LPCSTR loc_filename_resfile [in] resource file which holds localized filename for this link file
673 * DWORD loc_filename_resid [in] resource id for this link file's localized filename
676 * TRUE: Link file was successfully created
677 * FALSE: Link file was not created
679 BOOL WINAPI
USERENV_138( int csidl
, LPCSTR lnk_dir
, LPCSTR lnk_filename
,
680 LPCSTR lnk_target
, LPCSTR lnk_iconfile
, DWORD lnk_iconid
,
681 LPCSTR work_directory
, WORD hotkey
, DWORD win_state
, LPCSTR comment
,
682 LPCSTR loc_filename_resfile
, DWORD loc_filename_resid
)
684 FIXME("(%d,%s,%s,%s,%s,%ld,%s,0x%x,%ld,%s,%s,%ld) - stub\n", csidl
, debugstr_a(lnk_dir
),
685 debugstr_a(lnk_filename
), debugstr_a(lnk_target
), debugstr_a(lnk_iconfile
),
686 lnk_iconid
, debugstr_a(work_directory
), hotkey
, win_state
,
687 debugstr_a(comment
), debugstr_a(loc_filename_resfile
), loc_filename_resid
);