userenv: Add a set_env_var() helper.

Signed-off-by: Zebediah Figura <z.figura12@gmail.com>
Signed-off-by: Alexandre Julliard <julliard@winehq.org>
stable
Zebediah Figura 2019-11-05 21:48:58 -06:00 committed by Alexandre Julliard
parent 99dc0e0eeb
commit b8cf786412
1 changed files with 27 additions and 89 deletions

View File

@ -93,6 +93,15 @@ static BOOL get_reg_value(WCHAR *env, HKEY hkey, const WCHAR *name, WCHAR *val,
return FALSE; return FALSE;
} }
static void set_env_var( WCHAR **env, const WCHAR *name, const WCHAR *val )
{
UNICODE_STRING nameW, valW;
RtlInitUnicodeString( &nameW, name );
RtlInitUnicodeString( &valW, val );
RtlSetEnvironmentVariable( env, &nameW, &valW );
}
static void set_registry_variables(WCHAR **env, HKEY hkey, DWORD type, BOOL set_path) static void set_registry_variables(WCHAR **env, HKEY hkey, DWORD type, BOOL set_path)
{ {
UNICODE_STRING us_name, us_value; UNICODE_STRING us_name, us_value;
@ -124,25 +133,17 @@ static void set_registry_variables(WCHAR **env, HKEY hkey, DWORD type, BOOL set_
continue; continue;
value[size] = ';'; value[size] = ';';
RtlInitUnicodeString(&us_value, value); set_env_var(env, name, value);
RtlSetEnvironmentVariable(env, &us_name, &us_value);
continue; continue;
} }
if (!get_reg_value(*env, hkey, name, value, sizeof(value))) if (get_reg_value(*env, hkey, name, value, sizeof(value)) && value[0])
continue; set_env_var(env, name, value);
if(!value[0])
continue;
RtlInitUnicodeString(&us_value, value);
RtlSetEnvironmentVariable(env, &us_name, &us_value);
} }
} }
static void set_wow64_environment(WCHAR **env) static void set_wow64_environment(WCHAR **env)
{ {
UNICODE_STRING nameW, valueW;
WCHAR buf[64]; WCHAR buf[64];
HKEY hkey; HKEY hkey;
BOOL is_win64 = (sizeof(void *) > sizeof(int)); BOOL is_win64 = (sizeof(void *) > sizeof(int));
@ -158,66 +159,26 @@ static void set_wow64_environment(WCHAR **env)
if (get_reg_value(*env, hkey, L"ProgramFilesDir", buf, sizeof(buf))) if (get_reg_value(*env, hkey, L"ProgramFilesDir", buf, sizeof(buf)))
{ {
if (is_win64 || is_wow64) if (is_win64 || is_wow64) set_env_var(env, L"ProgramW6432", buf);
{ if (is_win64 || !is_wow64) set_env_var(env, L"ProgramFiles", buf);
RtlInitUnicodeString(&nameW, L"ProgramW6432");
RtlInitUnicodeString(&valueW, buf);
RtlSetEnvironmentVariable(env, &nameW, &valueW);
}
if (is_win64 || !is_wow64)
{
RtlInitUnicodeString(&nameW, L"ProgramFiles");
RtlInitUnicodeString(&valueW, buf);
RtlSetEnvironmentVariable(env, &nameW, &valueW);
}
} }
if (get_reg_value(*env, hkey, L"ProgramFilesDir (x86)", buf, sizeof(buf))) if (get_reg_value(*env, hkey, L"ProgramFilesDir (x86)", buf, sizeof(buf)))
{ {
if (is_win64 || is_wow64) if (is_win64 || is_wow64) set_env_var(env, L"ProgramFiles(x86)", buf);
{ if (is_wow64) set_env_var(env, L"ProgramFiles", buf);
RtlInitUnicodeString(&nameW, L"ProgramFiles(x86)");
RtlInitUnicodeString(&valueW, buf);
RtlSetEnvironmentVariable(env, &nameW, &valueW);
}
if (is_wow64)
{
RtlInitUnicodeString(&nameW, L"ProgramFiles");
RtlInitUnicodeString(&valueW, buf);
RtlSetEnvironmentVariable(env, &nameW, &valueW);
}
} }
/* set the CommonProgramFiles variables */ /* set the CommonProgramFiles variables */
if (get_reg_value(*env, hkey, L"CommonFilesDir", buf, sizeof(buf))) if (get_reg_value(*env, hkey, L"CommonFilesDir", buf, sizeof(buf)))
{ {
if (is_win64 || is_wow64) if (is_win64 || is_wow64) set_env_var(env, L"CommonProgramW6432", buf);
{ if (is_win64 || !is_wow64) set_env_var(env, L"CommonProgramFiles", buf);
RtlInitUnicodeString(&nameW, L"CommonProgramW6432");
RtlInitUnicodeString(&valueW, buf);
RtlSetEnvironmentVariable(env, &nameW, &valueW);
}
if (is_win64 || !is_wow64)
{
RtlInitUnicodeString(&nameW, L"CommonProgramFiles");
RtlInitUnicodeString(&valueW, buf);
RtlSetEnvironmentVariable(env, &nameW, &valueW);
}
} }
if (get_reg_value(*env, hkey, L"CommonFilesDir (x86)", buf, sizeof(buf))) if (get_reg_value(*env, hkey, L"CommonFilesDir (x86)", buf, sizeof(buf)))
{ {
if (is_win64 || is_wow64) if (is_win64 || is_wow64) set_env_var(env, L"CommonProgramFiles(x86)", buf);
{ if (is_wow64) set_env_var(env, L"CommonProgramFiles", buf);
RtlInitUnicodeString(&nameW, L"CommonProgramFiles(x86)");
RtlInitUnicodeString(&valueW, buf);
RtlSetEnvironmentVariable(env, &nameW, &valueW);
}
if (is_wow64)
{
RtlInitUnicodeString(&nameW, L"CommonProgramFiles");
RtlInitUnicodeString(&valueW, buf);
RtlSetEnvironmentVariable(env, &nameW, &valueW);
}
} }
RegCloseKey(hkey); RegCloseKey(hkey);
@ -230,7 +191,6 @@ BOOL WINAPI CreateEnvironmentBlock( LPVOID* lpEnvironment,
static const WCHAR profile_keyW[] = L"Software\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList"; static const WCHAR profile_keyW[] = L"Software\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList";
WCHAR *env, buf[UNICODE_STRING_MAX_CHARS], profiles_dir[MAX_PATH]; WCHAR *env, buf[UNICODE_STRING_MAX_CHARS], profiles_dir[MAX_PATH];
UNICODE_STRING us_name, us_val;
DWORD len; DWORD len;
HKEY hkey, hsubkey; HKEY hkey, hsubkey;
@ -256,9 +216,7 @@ BOOL WINAPI CreateEnvironmentBlock( LPVOID* lpEnvironment,
WARN("SystemRoot variable not set\n"); WARN("SystemRoot variable not set\n");
} }
} }
RtlInitUnicodeString(&us_name, L"SystemRoot"); set_env_var(&env, L"SystemRoot", buf);
RtlInitUnicodeString(&us_val, buf);
RtlSetEnvironmentVariable(&env, &us_name, &us_val);
if (!GetEnvironmentVariableW(L"SystemDrive", buf, UNICODE_STRING_MAX_CHARS)) if (!GetEnvironmentVariableW(L"SystemDrive", buf, UNICODE_STRING_MAX_CHARS))
{ {
@ -268,9 +226,7 @@ BOOL WINAPI CreateEnvironmentBlock( LPVOID* lpEnvironment,
WARN("SystemDrive variable not set\n"); WARN("SystemDrive variable not set\n");
} }
} }
RtlInitUnicodeString(&us_name, L"SystemDrive"); set_env_var(&env, L"SystemDrive", buf);
RtlInitUnicodeString(&us_val, buf);
RtlSetEnvironmentVariable(&env, &us_name, &us_val);
set_registry_variables(&env, hkey, REG_SZ, !bInherit); set_registry_variables(&env, hkey, REG_SZ, !bInherit);
set_registry_variables(&env, hkey, REG_EXPAND_SZ, !bInherit); set_registry_variables(&env, hkey, REG_EXPAND_SZ, !bInherit);
@ -302,11 +258,7 @@ BOOL WINAPI CreateEnvironmentBlock( LPVOID* lpEnvironment,
} }
if (get_reg_value(env, hkey, L"Public", buf, UNICODE_STRING_MAX_CHARS)) if (get_reg_value(env, hkey, L"Public", buf, UNICODE_STRING_MAX_CHARS))
{ set_env_var(&env, L"ALLUSERSPROFILE", buf);
RtlInitUnicodeString(&us_name, L"ALLUSERSPROFILE");
RtlInitUnicodeString(&us_val, buf);
RtlSetEnvironmentVariable(&env, &us_name, &us_val);
}
} }
else else
{ {
@ -318,11 +270,7 @@ BOOL WINAPI CreateEnvironmentBlock( LPVOID* lpEnvironment,
len = ARRAY_SIZE(buf); len = ARRAY_SIZE(buf);
if (GetComputerNameW(buf, &len)) if (GetComputerNameW(buf, &len))
{ set_env_var(&env, L"COMPUTERNAME", buf);
RtlInitUnicodeString(&us_name, L"COMPUTERNAME");
RtlInitUnicodeString(&us_val, buf);
RtlSetEnvironmentVariable(&env, &us_name, &us_val);
}
set_wow64_environment(&env); set_wow64_environment(&env);
@ -335,9 +283,7 @@ BOOL WINAPI CreateEnvironmentBlock( LPVOID* lpEnvironment,
{ {
wcscpy(buf, profiles_dir); wcscpy(buf, profiles_dir);
wcscat(buf, L"Default"); wcscat(buf, L"Default");
RtlInitUnicodeString(&us_name, L"USERPROFILE"); set_env_var(&env, L"USERPROFILE", buf);
RtlInitUnicodeString(&us_val, buf);
RtlSetEnvironmentVariable(&env, &us_name, &us_val);
} }
} }
@ -368,16 +314,8 @@ BOOL WINAPI CreateEnvironmentBlock( LPVOID* lpEnvironment,
if (LookupAccountSidW(NULL, token_user->User.Sid, if (LookupAccountSidW(NULL, token_user->User.Sid,
buf+len, &size, NULL, &tmp, &use)) buf+len, &size, NULL, &tmp, &use))
{ {
RtlInitUnicodeString(&us_name, L"USERNAME"); set_env_var(&env, L"USERNAME", buf+len);
RtlInitUnicodeString(&us_val, buf+len); if (len) set_env_var(&env, L"USERPROFILE", buf);
RtlSetEnvironmentVariable(&env, &us_name, &us_val);
if (len)
{
RtlInitUnicodeString(&us_name, L"USERPROFILE");
RtlInitUnicodeString(&us_val, buf);
RtlSetEnvironmentVariable(&env, &us_name, &us_val);
}
} }
HeapFree(GetProcessHeap(), 0, token_user); HeapFree(GetProcessHeap(), 0, token_user);