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
))
246 set_env_var(&env
, L
"ALLUSERSPROFILE", buf
);
256 len
= ARRAY_SIZE(buf
);
257 if (GetComputerNameW(buf
, &len
))
258 set_env_var(&env
, L
"COMPUTERNAME", buf
);
260 set_wow64_environment(&env
);
266 len
= lstrlenW(profiles_dir
);
267 if (len
* sizeof(WCHAR
) + sizeof(L
"Default") < sizeof(buf
))
269 wcscpy(buf
, profiles_dir
);
270 wcscat(buf
, L
"Default");
271 set_env_var(&env
, L
"USERPROFILE", buf
);
275 wcscpy(buf
, L
".Default");
279 TOKEN_USER
*token_user
= NULL
;
284 if (GetTokenInformation(hToken
, TokenUser
, NULL
, 0, &len
) ||
285 GetLastError()!=ERROR_INSUFFICIENT_BUFFER
||
286 !(token_user
= HeapAlloc(GetProcessHeap(), 0, len
)) ||
287 !GetTokenInformation(hToken
, TokenUser
, token_user
, len
, &len
) ||
288 !ConvertSidToStringSidW(token_user
->User
.Sid
, &sidW
))
290 HeapFree(GetProcessHeap(), 0, token_user
);
291 RtlDestroyEnvironment(env
);
295 len
= lstrlenW(profiles_dir
);
296 memcpy(buf
, profiles_dir
, len
*sizeof(WCHAR
));
298 size
= UNICODE_STRING_MAX_CHARS
-len
;
299 if (LookupAccountSidW(NULL
, token_user
->User
.Sid
,
300 buf
+len
, &size
, NULL
, &tmp
, &use
))
302 set_env_var(&env
, L
"USERNAME", buf
+len
);
303 if (len
) set_env_var(&env
, L
"USERPROFILE", buf
);
306 HeapFree(GetProcessHeap(), 0, token_user
);
311 if (RegOpenKeyExW(HKEY_USERS
, buf
, 0, KEY_READ
, &hkey
) == ERROR_SUCCESS
)
313 if (RegOpenKeyExW(hkey
, L
"Environment", 0, KEY_READ
, &hsubkey
) == ERROR_SUCCESS
)
315 set_registry_variables(&env
, hsubkey
, REG_SZ
, !bInherit
);
316 set_registry_variables(&env
, hsubkey
, REG_EXPAND_SZ
, !bInherit
);
317 RegCloseKey(hsubkey
);
320 if (RegOpenKeyExW(hkey
, L
"Volatile Environment", 0, KEY_READ
, &hsubkey
) == ERROR_SUCCESS
)
322 set_registry_variables(&env
, hsubkey
, REG_SZ
, !bInherit
);
323 set_registry_variables(&env
, hsubkey
, REG_EXPAND_SZ
, !bInherit
);
324 RegCloseKey(hsubkey
);
329 *lpEnvironment
= env
;
333 BOOL WINAPI
DestroyEnvironmentBlock(LPVOID lpEnvironment
)
337 TRACE("%p\n", lpEnvironment
);
338 r
= RtlDestroyEnvironment(lpEnvironment
);
339 if (r
== STATUS_SUCCESS
)
344 BOOL WINAPI
ExpandEnvironmentStringsForUserA( HANDLE hToken
, LPCSTR lpSrc
,
345 LPSTR lpDest
, DWORD dwSize
)
349 TRACE("%p %s %p %d\n", hToken
, debugstr_a(lpSrc
), lpDest
, dwSize
);
351 ret
= ExpandEnvironmentStringsA( lpSrc
, lpDest
, dwSize
);
352 TRACE("<- %s\n", debugstr_a(lpDest
));
356 BOOL WINAPI
ExpandEnvironmentStringsForUserW( HANDLE hToken
, LPCWSTR lpSrc
,
357 LPWSTR lpDest
, DWORD dwSize
)
361 TRACE("%p %s %p %d\n", hToken
, debugstr_w(lpSrc
), lpDest
, dwSize
);
363 ret
= ExpandEnvironmentStringsW( lpSrc
, lpDest
, dwSize
);
364 TRACE("<- %s\n", debugstr_w(lpDest
));
368 BOOL WINAPI
GetDefaultUserProfileDirectoryA( LPSTR lpProfileDir
, LPDWORD lpcchSize
)
370 FIXME("%p %p\n", lpProfileDir
, lpcchSize
);
374 BOOL WINAPI
GetDefaultUserProfileDirectoryW( LPWSTR lpProfileDir
, LPDWORD lpcchSize
)
376 FIXME("%p %p\n", lpProfileDir
, lpcchSize
);
380 BOOL WINAPI
GetUserProfileDirectoryA( HANDLE hToken
, LPSTR lpProfileDir
,
386 TRACE( "%p %p %p\n", hToken
, lpProfileDir
, lpcchSize
);
388 if (!lpProfileDir
|| !lpcchSize
)
390 SetLastError( ERROR_INVALID_PARAMETER
);
393 if (!(dirW
= HeapAlloc( GetProcessHeap(), 0, *lpcchSize
* sizeof(WCHAR
) )))
396 if ((ret
= GetUserProfileDirectoryW( hToken
, dirW
, lpcchSize
)))
397 WideCharToMultiByte( CP_ACP
, 0, dirW
, *lpcchSize
, lpProfileDir
, *lpcchSize
, NULL
, NULL
);
399 HeapFree( GetProcessHeap(), 0, dirW
);
403 BOOL WINAPI
GetUserProfileDirectoryW( HANDLE hToken
, LPWSTR lpProfileDir
,
407 WCHAR
*userW
= NULL
, *dirW
= NULL
;
408 DWORD len
, dir_len
, domain_len
;
412 TRACE( "%p %p %p\n", hToken
, lpProfileDir
, lpcchSize
);
416 SetLastError( ERROR_INVALID_PARAMETER
);
421 GetTokenInformation( hToken
, TokenUser
, NULL
, 0, &len
);
422 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER
) return FALSE
;
423 if (!(t
= HeapAlloc( GetProcessHeap(), 0, len
))) return FALSE
;
424 if (!GetTokenInformation( hToken
, TokenUser
, t
, len
, &len
)) goto done
;
426 len
= domain_len
= 0;
427 LookupAccountSidW( NULL
, t
->User
.Sid
, NULL
, &len
, NULL
, &domain_len
, NULL
);
428 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER
) goto done
;
429 if (!(userW
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) ))) goto done
;
430 if (!LookupAccountSidW( NULL
, t
->User
.Sid
, userW
, &len
, NULL
, &domain_len
, &use
)) goto done
;
433 GetProfilesDirectoryW( NULL
, &dir_len
);
434 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER
) goto done
;
435 if (!(dirW
= HeapAlloc( GetProcessHeap(), 0, (dir_len
+ 1) * sizeof(WCHAR
) ))) goto done
;
436 if (!GetProfilesDirectoryW( dirW
, &dir_len
)) goto done
;
439 if (*lpcchSize
< len
)
441 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
445 lstrcpyW( lpProfileDir
, dirW
);
446 lstrcatW( lpProfileDir
, L
"\\" );
447 lstrcatW( lpProfileDir
, userW
);
452 HeapFree( GetProcessHeap(), 0, t
);
453 HeapFree( GetProcessHeap(), 0, userW
);
454 HeapFree( GetProcessHeap(), 0, dirW
);
458 static const char ProfileListA
[] = "Software\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList";
460 BOOL WINAPI
GetProfilesDirectoryA( LPSTR lpProfilesDir
, LPDWORD lpcchSize
)
462 static const char ProfilesDirectory
[] = "ProfilesDirectory";
466 DWORD len
= 0, expanded_len
;
467 LPSTR unexpanded_profiles_dir
= NULL
;
469 TRACE("%p %p\n", lpProfilesDir
, lpcchSize
);
471 if (!lpProfilesDir
|| !lpcchSize
)
473 SetLastError(ERROR_INVALID_PARAMETER
);
477 l
= RegOpenKeyExA(HKEY_LOCAL_MACHINE
, ProfileListA
, 0, KEY_READ
, &key
);
483 l
= RegQueryValueExA(key
, ProfilesDirectory
, NULL
, NULL
, NULL
, &len
);
489 unexpanded_profiles_dir
= HeapAlloc(GetProcessHeap(), 0, len
);
490 if (!unexpanded_profiles_dir
)
492 SetLastError(ERROR_OUTOFMEMORY
);
495 l
= RegQueryValueExA(key
, ProfilesDirectory
, NULL
, NULL
,
496 (BYTE
*)unexpanded_profiles_dir
, &len
);
502 expanded_len
= ExpandEnvironmentStringsA(unexpanded_profiles_dir
, NULL
, 0);
503 /* The returned length doesn't include the NULL terminator. */
504 if (*lpcchSize
< expanded_len
- 1)
506 *lpcchSize
= expanded_len
- 1;
507 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
510 *lpcchSize
= expanded_len
- 1;
511 /* The return value is also the expected length. */
512 ret
= ExpandEnvironmentStringsA(unexpanded_profiles_dir
, lpProfilesDir
,
515 HeapFree(GetProcessHeap(), 0, unexpanded_profiles_dir
);
520 BOOL WINAPI
GetProfilesDirectoryW( LPWSTR lpProfilesDir
, LPDWORD lpcchSize
)
525 DWORD len
= 0, expanded_len
;
526 LPWSTR unexpanded_profiles_dir
= NULL
;
528 TRACE("%p %p\n", lpProfilesDir
, lpcchSize
);
532 SetLastError(ERROR_INVALID_PARAMETER
);
536 l
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
537 L
"Software\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList",
544 l
= RegQueryValueExW(key
, L
"ProfilesDirectory", NULL
, NULL
, NULL
, &len
);
550 unexpanded_profiles_dir
= HeapAlloc(GetProcessHeap(), 0, len
);
551 if (!unexpanded_profiles_dir
)
553 SetLastError(ERROR_OUTOFMEMORY
);
556 l
= RegQueryValueExW(key
, L
"ProfilesDirectory", NULL
, NULL
,
557 (BYTE
*)unexpanded_profiles_dir
, &len
);
563 expanded_len
= ExpandEnvironmentStringsW(unexpanded_profiles_dir
, NULL
, 0);
564 /* The returned length doesn't include the NULL terminator. */
565 if (*lpcchSize
< expanded_len
- 1 || !lpProfilesDir
)
567 *lpcchSize
= expanded_len
- 1;
568 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
571 *lpcchSize
= expanded_len
- 1;
572 /* The return value is also the expected length. */
573 ret
= ExpandEnvironmentStringsW(unexpanded_profiles_dir
, lpProfilesDir
,
576 HeapFree(GetProcessHeap(), 0, unexpanded_profiles_dir
);
581 BOOL WINAPI
GetAllUsersProfileDirectoryA( LPSTR lpProfileDir
, LPDWORD lpcchSize
)
583 FIXME("%p %p\n", lpProfileDir
, lpcchSize
);
587 BOOL WINAPI
GetAllUsersProfileDirectoryW( LPWSTR lpProfileDir
, LPDWORD lpcchSize
)
589 FIXME("%p %p\n", lpProfileDir
, lpcchSize
);
593 BOOL WINAPI
GetProfileType( DWORD
*pdwFlags
)
595 FIXME("%p\n", pdwFlags
);
600 BOOL WINAPI
LoadUserProfileA( HANDLE hToken
, LPPROFILEINFOA lpProfileInfo
)
602 FIXME("%p %p\n", hToken
, lpProfileInfo
);
603 lpProfileInfo
->hProfile
= HKEY_CURRENT_USER
;
607 BOOL WINAPI
LoadUserProfileW( HANDLE hToken
, LPPROFILEINFOW lpProfileInfo
)
609 FIXME("%p %p\n", hToken
, lpProfileInfo
);
610 lpProfileInfo
->hProfile
= HKEY_CURRENT_USER
;
614 BOOL WINAPI
RegisterGPNotification( HANDLE event
, BOOL machine
)
616 FIXME("%p %d\n", event
, machine
);
620 BOOL WINAPI
UnregisterGPNotification( HANDLE event
)
622 FIXME("%p\n", event
);
626 BOOL WINAPI
UnloadUserProfile( HANDLE hToken
, HANDLE hProfile
)
628 FIXME("(%p, %p): stub\n", hToken
, hProfile
);
632 HANDLE WINAPI
EnterCriticalPolicySection(BOOL bMachine
)
634 FIXME("(%x)\n", bMachine
);
635 SetLastError(ERROR_ACCESS_DENIED
);
639 BOOL WINAPI
LeaveCriticalPolicySection(HANDLE hSection
)
641 FIXME("(%p)\n", hSection
);
645 DWORD WINAPI
GetAppliedGPOListW(DWORD dwFlags
, LPCWSTR pMachineName
, PSID pSidUser
, GUID
*pGuidExtension
,
646 PGROUP_POLICY_OBJECTW
*ppGPOList
)
648 FIXME("(%x %s %p %s %p)\n", dwFlags
, debugstr_w(pMachineName
), pSidUser
, debugstr_guid(pGuidExtension
), ppGPOList
);
649 return ERROR_ACCESS_DENIED
;
652 /******************************************************************************
658 * int csidl [in] well-known directory location to create link in
659 * LPCSTR lnk_dir [in] directory (relative to directory specified by csidl) to create link in
660 * LPCSTR lnk_filename [in] filename of the link file without .lnk extension
661 * LPCSTR lnk_target [in] file/directory pointed to by link
662 * LPCSTR lnk_iconfile [in] link icon resource filename
663 * DWORD lnk_iconid [in] link icon resource id in file referred by lnk_iconfile
664 * LPCSTR work_directory [in] link target's work directory
665 * WORD hotkey [in] link hotkey (virtual key id)
666 * DWORD win_state [in] initial window size (SW_SHOWMAXIMIZED to start maximized,
667 * SW_SHOWMINNOACTIVE to start minimized, everything else is default state)
668 * LPCSTR comment [in] comment - link's comment
669 * LPCSTR loc_filename_resfile [in] resource file which holds localized filename for this link file
670 * DWORD loc_filename_resid [in] resource id for this link file's localized filename
673 * TRUE: Link file was successfully created
674 * FALSE: Link file was not created
676 BOOL WINAPI
USERENV_138( int csidl
, LPCSTR lnk_dir
, LPCSTR lnk_filename
,
677 LPCSTR lnk_target
, LPCSTR lnk_iconfile
, DWORD lnk_iconid
,
678 LPCSTR work_directory
, WORD hotkey
, DWORD win_state
, LPCSTR comment
,
679 LPCSTR loc_filename_resfile
, DWORD loc_filename_resid
)
681 FIXME("(%d,%s,%s,%s,%s,%d,%s,0x%x,%d,%s,%s,%d) - stub\n", csidl
, debugstr_a(lnk_dir
),
682 debugstr_a(lnk_filename
), debugstr_a(lnk_target
), debugstr_a(lnk_iconfile
),
683 lnk_iconid
, debugstr_a(work_directory
), hotkey
, win_state
,
684 debugstr_a(comment
), debugstr_a(loc_filename_resfile
), loc_filename_resid
);