diff --git a/reactos/dll/win32/userenv/environment.c b/reactos/dll/win32/userenv/environment.c index 9b434d92889..f62e8e3f0e3 100644 --- a/reactos/dll/win32/userenv/environment.c +++ b/reactos/dll/win32/userenv/environment.c @@ -372,7 +372,7 @@ SetUserEnvironment(LPVOID *lpEnvironment, &dwValueDataLength); if (Error == ERROR_SUCCESS) { - if (!_wcsicmp (lpValueName, L"path")) + if (!_wcsicmp(lpValueName, L"path")) { /* Append 'Path' environment variable */ AppendUserEnvironmentVariable(lpEnvironment, @@ -406,6 +406,117 @@ SetUserEnvironment(LPVOID *lpEnvironment, } +static +BOOL +SetSystemEnvironment(LPVOID *lpEnvironment) +{ + HKEY hEnvKey; + DWORD dwValues; + DWORD dwMaxValueNameLength; + DWORD dwMaxValueDataLength; + DWORD dwValueNameLength; + DWORD dwValueDataLength; + DWORD dwType; + DWORD i; + LPWSTR lpValueName; + LPWSTR lpValueData; + LONG Error; + + Error = RegOpenKeyExW(HKEY_LOCAL_MACHINE, + L"SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment", + 0, + KEY_QUERY_VALUE, + &hEnvKey); + if (Error != ERROR_SUCCESS) + { + DPRINT1("RegOpenKeyExW() failed (Error %ld)\n", Error); + return FALSE; + } + + Error = RegQueryInfoKey(hEnvKey, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + &dwValues, + &dwMaxValueNameLength, + &dwMaxValueDataLength, + NULL, + NULL); + if (Error != ERROR_SUCCESS) + { + DPRINT1("RegQueryInforKey() failed (Error %ld)\n", Error); + RegCloseKey(hEnvKey); + return FALSE; + } + + if (dwValues == 0) + { + RegCloseKey(hEnvKey); + return TRUE; + } + + /* Allocate buffers */ + dwMaxValueNameLength++; + lpValueName = LocalAlloc(LPTR, + dwMaxValueNameLength * sizeof(WCHAR)); + if (lpValueName == NULL) + { + RegCloseKey(hEnvKey); + return FALSE; + } + + lpValueData = LocalAlloc(LPTR, + dwMaxValueDataLength); + if (lpValueData == NULL) + { + LocalFree(lpValueName); + RegCloseKey(hEnvKey); + return FALSE; + } + + /* Enumerate values */ + for (i = 0; i < dwValues; i++) + { + dwValueNameLength = dwMaxValueNameLength; + dwValueDataLength = dwMaxValueDataLength; + + Error = RegEnumValueW(hEnvKey, + i, + lpValueName, + &dwValueNameLength, + NULL, + &dwType, + (LPBYTE)lpValueData, + &dwValueDataLength); + if (Error == ERROR_SUCCESS) + { + /* Set environment variable */ + SetUserEnvironmentVariable(lpEnvironment, + lpValueName, + lpValueData, + (dwType == REG_EXPAND_SZ)); + } + else + { + LocalFree(lpValueData); + LocalFree(lpValueName); + RegCloseKey(hEnvKey); + + return FALSE; + } + } + + LocalFree(lpValueData); + LocalFree(lpValueName); + RegCloseKey(hEnvKey); + + return TRUE; +} + + BOOL WINAPI CreateEnvironmentBlock(LPVOID *lpEnvironment, @@ -433,7 +544,7 @@ CreateEnvironmentBlock(LPVOID *lpEnvironment, Status = RtlCreateEnvironment((BOOLEAN)bInherit, (PWSTR*)lpEnvironment); - if (!NT_SUCCESS (Status)) + if (!NT_SUCCESS(Status)) { DPRINT1("RtlCreateEnvironment() failed (Status %lx)\n", Status); SetLastError(RtlNtStatusToDosError(Status)); @@ -464,9 +575,11 @@ CreateEnvironmentBlock(LPVOID *lpEnvironment, } /* Set variables from Session Manager */ - SetUserEnvironment(lpEnvironment, - HKEY_LOCAL_MACHINE, - L"System\\CurrentControlSet\\Control\\Session Manager\\Environment"); + if (!SetSystemEnvironment(lpEnvironment)) + { + RtlDestroyEnvironment(*lpEnvironment); + return FALSE; + } /* Set 'COMPUTERNAME' variable */ Length = MAX_PATH;