[FORMATTING]

No code changes.

svn path=/trunk/; revision=47107
This commit is contained in:
Eric Kohl 2010-05-05 22:53:01 +00:00
parent 51e6829b67
commit ac293dd5d6

View file

@ -32,394 +32,396 @@
static BOOL static BOOL
SetUserEnvironmentVariable (LPVOID *Environment, SetUserEnvironmentVariable(LPVOID *Environment,
LPWSTR lpName, LPWSTR lpName,
LPWSTR lpValue, LPWSTR lpValue,
BOOL bExpand) BOOL bExpand)
{ {
WCHAR ShortName[MAX_PATH]; WCHAR ShortName[MAX_PATH];
UNICODE_STRING Name; UNICODE_STRING Name;
UNICODE_STRING SrcValue; UNICODE_STRING SrcValue;
UNICODE_STRING DstValue; UNICODE_STRING DstValue;
ULONG Length; ULONG Length;
NTSTATUS Status; NTSTATUS Status;
PVOID Buffer=NULL; PVOID Buffer = NULL;
if (bExpand) if (bExpand)
{
RtlInitUnicodeString(&SrcValue,
lpValue);
Length = 2 * MAX_PATH * sizeof(WCHAR);
DstValue.Length = 0;
DstValue.MaximumLength = Length;
DstValue.Buffer = Buffer = LocalAlloc(LPTR,
Length);
if (DstValue.Buffer == NULL)
{
DPRINT1("LocalAlloc() failed\n");
return FALSE;
}
Status = RtlExpandEnvironmentStrings_U((PWSTR)*Environment,
&SrcValue,
&DstValue,
&Length);
if (!NT_SUCCESS(Status))
{
DPRINT1("RtlExpandEnvironmentStrings_U() failed (Status %lx)\n", Status);
DPRINT1("Length %lu\n", Length);
if (Buffer) LocalFree(Buffer);
return FALSE;
}
}
else
{
RtlInitUnicodeString(&DstValue,
lpValue);
}
if (!_wcsicmp (lpName, L"temp") || !_wcsicmp (lpName, L"tmp"))
{
if (!GetShortPathNameW(DstValue.Buffer, ShortName, MAX_PATH))
{
DPRINT1("GetShortPathNameW() failed for %S (Error %lu)\n", DstValue.Buffer, GetLastError());
if (Buffer) LocalFree(Buffer);
return FALSE;
}
DPRINT("Buffer: %S\n", ShortName);
RtlInitUnicodeString(&DstValue,
ShortName);
}
RtlInitUnicodeString(&Name,
lpName);
DPRINT("Value: %wZ\n", &DstValue);
Status = RtlSetEnvironmentVariable((PWSTR*)Environment,
&Name,
&DstValue);
if (Buffer) LocalFree(Buffer);
if (!NT_SUCCESS(Status))
{ {
DPRINT1("RtlSetEnvironmentVariable() failed (Status %lx)\n", Status); RtlInitUnicodeString(&SrcValue,
return FALSE; lpValue);
Length = 2 * MAX_PATH * sizeof(WCHAR);
DstValue.Length = 0;
DstValue.MaximumLength = Length;
DstValue.Buffer = Buffer = LocalAlloc(LPTR,
Length);
if (DstValue.Buffer == NULL)
{
DPRINT1("LocalAlloc() failed\n");
return FALSE;
}
Status = RtlExpandEnvironmentStrings_U((PWSTR)*Environment,
&SrcValue,
&DstValue,
&Length);
if (!NT_SUCCESS(Status))
{
DPRINT1("RtlExpandEnvironmentStrings_U() failed (Status %lx)\n", Status);
DPRINT1("Length %lu\n", Length);
if (Buffer)
LocalFree(Buffer);
return FALSE;
}
}
else
{
RtlInitUnicodeString(&DstValue,
lpValue);
} }
return TRUE; if (!_wcsicmp(lpName, L"temp") || !_wcsicmp(lpName, L"tmp"))
{
if (!GetShortPathNameW(DstValue.Buffer, ShortName, MAX_PATH))
{
DPRINT1("GetShortPathNameW() failed for %S (Error %lu)\n", DstValue.Buffer, GetLastError());
if (Buffer)
LocalFree(Buffer);
return FALSE;
}
DPRINT("Buffer: %S\n", ShortName);
RtlInitUnicodeString(&DstValue,
ShortName);
}
RtlInitUnicodeString(&Name,
lpName);
DPRINT("Value: %wZ\n", &DstValue);
Status = RtlSetEnvironmentVariable((PWSTR*)Environment,
&Name,
&DstValue);
if (Buffer)
LocalFree(Buffer);
if (!NT_SUCCESS(Status))
{
DPRINT1("RtlSetEnvironmentVariable() failed (Status %lx)\n", Status);
return FALSE;
}
return TRUE;
} }
static BOOL static BOOL
AppendUserEnvironmentVariable (LPVOID *Environment, AppendUserEnvironmentVariable(LPVOID *Environment,
LPWSTR lpName, LPWSTR lpName,
LPWSTR lpValue) LPWSTR lpValue)
{ {
UNICODE_STRING Name; UNICODE_STRING Name;
UNICODE_STRING Value; UNICODE_STRING Value;
NTSTATUS Status; NTSTATUS Status;
RtlInitUnicodeString (&Name, RtlInitUnicodeString(&Name,
lpName); lpName);
Value.Length = 0; Value.Length = 0;
Value.MaximumLength = 1024 * sizeof(WCHAR); Value.MaximumLength = 1024 * sizeof(WCHAR);
Value.Buffer = LocalAlloc (LPTR, Value.Buffer = LocalAlloc(LPTR,
1024 * sizeof(WCHAR)); 1024 * sizeof(WCHAR));
if (Value.Buffer == NULL) if (Value.Buffer == NULL)
{ {
return FALSE; return FALSE;
} }
Value.Buffer[0] = UNICODE_NULL; Value.Buffer[0] = UNICODE_NULL;
Status = RtlQueryEnvironmentVariable_U ((PWSTR)*Environment, Status = RtlQueryEnvironmentVariable_U((PWSTR)*Environment,
&Name, &Name,
&Value); &Value);
if (NT_SUCCESS(Status)) if (NT_SUCCESS(Status))
{ {
RtlAppendUnicodeToString (&Value, RtlAppendUnicodeToString(&Value,
L";"); L";");
} }
RtlAppendUnicodeToString (&Value, RtlAppendUnicodeToString(&Value,
lpValue); lpValue);
Status = RtlSetEnvironmentVariable ((PWSTR*)Environment, Status = RtlSetEnvironmentVariable((PWSTR*)Environment,
&Name, &Name,
&Value); &Value);
LocalFree (Value.Buffer); LocalFree(Value.Buffer);
if (!NT_SUCCESS(Status)) if (!NT_SUCCESS(Status))
{ {
DPRINT1 ("RtlSetEnvironmentVariable() failed (Status %lx)\n", Status); DPRINT1("RtlSetEnvironmentVariable() failed (Status %lx)\n", Status);
return FALSE; return FALSE;
} }
return TRUE; return TRUE;
} }
static HKEY static HKEY
GetCurrentUserKey (HANDLE hToken) GetCurrentUserKey(HANDLE hToken)
{ {
UNICODE_STRING SidString; UNICODE_STRING SidString;
HKEY hKey; HKEY hKey;
LONG Error; LONG Error;
if (!GetUserSidFromToken (hToken, if (!GetUserSidFromToken(hToken,
&SidString)) &SidString))
{ {
DPRINT1 ("GetUserSidFromToken() failed\n"); DPRINT1("GetUserSidFromToken() failed\n");
return NULL; return NULL;
} }
Error = RegOpenKeyExW (HKEY_USERS, Error = RegOpenKeyExW(HKEY_USERS,
SidString.Buffer, SidString.Buffer,
0, 0,
MAXIMUM_ALLOWED, MAXIMUM_ALLOWED,
&hKey); &hKey);
if (Error != ERROR_SUCCESS) if (Error != ERROR_SUCCESS)
{ {
DPRINT1 ("RegOpenKeyExW() failed (Error %ld)\n", Error); DPRINT1("RegOpenKeyExW() failed (Error %ld)\n", Error);
RtlFreeUnicodeString (&SidString); RtlFreeUnicodeString(&SidString);
SetLastError((DWORD)Error); SetLastError((DWORD)Error);
return NULL; return NULL;
} }
RtlFreeUnicodeString (&SidString); RtlFreeUnicodeString(&SidString);
return hKey; return hKey;
} }
static BOOL static BOOL
SetUserEnvironment (LPVOID *lpEnvironment, SetUserEnvironment(LPVOID *lpEnvironment,
HKEY hKey, HKEY hKey,
LPWSTR lpSubKeyName) LPWSTR lpSubKeyName)
{ {
HKEY hEnvKey; HKEY hEnvKey;
DWORD dwValues; DWORD dwValues;
DWORD dwMaxValueNameLength; DWORD dwMaxValueNameLength;
DWORD dwMaxValueDataLength; DWORD dwMaxValueDataLength;
DWORD dwValueNameLength; DWORD dwValueNameLength;
DWORD dwValueDataLength; DWORD dwValueDataLength;
DWORD dwType; DWORD dwType;
DWORD i; DWORD i;
LPWSTR lpValueName; LPWSTR lpValueName;
LPWSTR lpValueData; LPWSTR lpValueData;
LONG Error; LONG Error;
Error = RegOpenKeyExW (hKey, Error = RegOpenKeyExW(hKey,
lpSubKeyName, lpSubKeyName,
0, 0,
KEY_QUERY_VALUE, KEY_QUERY_VALUE,
&hEnvKey); &hEnvKey);
if (Error != ERROR_SUCCESS) if (Error != ERROR_SUCCESS)
{ {
DPRINT1 ("RegOpenKeyExW() failed (Error %ld)\n", Error); DPRINT1("RegOpenKeyExW() failed (Error %ld)\n", Error);
SetLastError((DWORD)Error); SetLastError((DWORD)Error);
return FALSE; return FALSE;
} }
Error = RegQueryInfoKey (hEnvKey, Error = RegQueryInfoKey(hEnvKey,
NULL, NULL,
NULL, NULL,
NULL, NULL,
NULL, NULL,
NULL, NULL,
NULL, NULL,
&dwValues, &dwValues,
&dwMaxValueNameLength, &dwMaxValueNameLength,
&dwMaxValueDataLength, &dwMaxValueDataLength,
NULL, NULL,
NULL); NULL);
if (Error != ERROR_SUCCESS) if (Error != ERROR_SUCCESS)
{ {
DPRINT1 ("RegQueryInforKey() failed (Error %ld)\n", Error); DPRINT1("RegQueryInforKey() failed (Error %ld)\n", Error);
RegCloseKey (hEnvKey); RegCloseKey(hEnvKey);
SetLastError((DWORD)Error); SetLastError((DWORD)Error);
return FALSE; return FALSE;
} }
if (dwValues == 0) if (dwValues == 0)
{ {
RegCloseKey (hEnvKey); RegCloseKey(hEnvKey);
return TRUE; return TRUE;
} }
/* Allocate buffers */ /* Allocate buffers */
lpValueName = LocalAlloc (LPTR, lpValueName = LocalAlloc(LPTR,
dwMaxValueNameLength * sizeof(WCHAR)); dwMaxValueNameLength * sizeof(WCHAR));
if (lpValueName == NULL) if (lpValueName == NULL)
{ {
RegCloseKey (hEnvKey); RegCloseKey(hEnvKey);
return FALSE; return FALSE;
} }
lpValueData = LocalAlloc (LPTR, lpValueData = LocalAlloc(LPTR,
dwMaxValueDataLength); dwMaxValueDataLength);
if (lpValueData == NULL) if (lpValueData == NULL)
{ {
LocalFree (lpValueName); LocalFree(lpValueName);
RegCloseKey (hEnvKey); RegCloseKey(hEnvKey);
return FALSE; return FALSE;
} }
/* Enumerate values */ /* Enumerate values */
for (i = 0; i < dwValues; i++) for (i = 0; i < dwValues; i++)
{ {
dwValueNameLength = dwMaxValueNameLength; dwValueNameLength = dwMaxValueNameLength;
dwValueDataLength = dwMaxValueDataLength; dwValueDataLength = dwMaxValueDataLength;
RegEnumValueW (hEnvKey, RegEnumValueW(hEnvKey,
i, i,
lpValueName, lpValueName,
&dwValueNameLength, &dwValueNameLength,
NULL, NULL,
&dwType, &dwType,
(LPBYTE)lpValueData, (LPBYTE)lpValueData,
&dwValueDataLength); &dwValueDataLength);
if (!_wcsicmp (lpValueName, L"path")) if (!_wcsicmp (lpValueName, L"path"))
{ {
/* Append 'Path' environment variable */ /* Append 'Path' environment variable */
AppendUserEnvironmentVariable (lpEnvironment, AppendUserEnvironmentVariable(lpEnvironment,
lpValueName, lpValueName,
lpValueData); lpValueData);
} }
else else
{ {
/* Set environment variable */ /* Set environment variable */
SetUserEnvironmentVariable (lpEnvironment, SetUserEnvironmentVariable(lpEnvironment,
lpValueName, lpValueName,
lpValueData, lpValueData,
(dwType == REG_EXPAND_SZ)); (dwType == REG_EXPAND_SZ));
} }
} }
LocalFree (lpValueData); LocalFree(lpValueData);
LocalFree (lpValueName); LocalFree(lpValueName);
RegCloseKey (hEnvKey); RegCloseKey(hEnvKey);
return TRUE;
}
BOOL WINAPI
CreateEnvironmentBlock (LPVOID *lpEnvironment,
HANDLE hToken,
BOOL bInherit)
{
WCHAR Buffer[MAX_PATH];
DWORD Length;
HKEY hKeyUser;
NTSTATUS Status;
DPRINT("CreateEnvironmentBlock() called\n");
if (lpEnvironment == NULL)
{
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
Status = RtlCreateEnvironment ((BOOLEAN)bInherit,
(PWSTR*)lpEnvironment);
if (!NT_SUCCESS (Status))
{
DPRINT1 ("RtlCreateEnvironment() failed (Status %lx)\n", Status);
SetLastError (RtlNtStatusToDosError (Status));
return FALSE;
}
/* Set 'COMPUTERNAME' variable */
Length = MAX_PATH;
if (GetComputerNameW (Buffer,
&Length))
{
SetUserEnvironmentVariable(lpEnvironment,
L"COMPUTERNAME",
Buffer,
FALSE);
}
if (hToken == NULL)
return TRUE; return TRUE;
hKeyUser = GetCurrentUserKey (hToken);
if (hKeyUser == NULL)
{
DPRINT1 ("GetCurrentUserKey() failed\n");
RtlDestroyEnvironment (*lpEnvironment);
return FALSE;
}
/* Set 'ALLUSERSPROFILE' variable */
Length = MAX_PATH;
if (GetAllUsersProfileDirectoryW (Buffer,
&Length))
{
SetUserEnvironmentVariable(lpEnvironment,
L"ALLUSERSPROFILE",
Buffer,
FALSE);
}
/* Set 'USERPROFILE' variable */
Length = MAX_PATH;
if (GetUserProfileDirectoryW (hToken,
Buffer,
&Length))
{
SetUserEnvironmentVariable(lpEnvironment,
L"USERPROFILE",
Buffer,
FALSE);
}
/* FIXME: Set 'USERDOMAIN' variable */
Length = MAX_PATH;
if (GetUserNameW(Buffer,
&Length))
{
SetUserEnvironmentVariable(lpEnvironment,
L"USERNAME",
Buffer,
FALSE);
}
/* Set user environment variables */
SetUserEnvironment (lpEnvironment,
hKeyUser,
L"Environment");
RegCloseKey (hKeyUser);
return TRUE;
} }
BOOL WINAPI BOOL WINAPI
DestroyEnvironmentBlock (LPVOID lpEnvironment) CreateEnvironmentBlock(LPVOID *lpEnvironment,
HANDLE hToken,
BOOL bInherit)
{ {
DPRINT ("DestroyEnvironmentBlock() called\n"); WCHAR Buffer[MAX_PATH];
DWORD Length;
HKEY hKeyUser;
NTSTATUS Status;
if (lpEnvironment == NULL) DPRINT("CreateEnvironmentBlock() called\n");
if (lpEnvironment == NULL)
{ {
SetLastError(ERROR_INVALID_PARAMETER); SetLastError(ERROR_INVALID_PARAMETER);
return FALSE; return FALSE;
} }
RtlDestroyEnvironment (lpEnvironment); Status = RtlCreateEnvironment((BOOLEAN)bInherit,
(PWSTR*)lpEnvironment);
if (!NT_SUCCESS (Status))
{
DPRINT1("RtlCreateEnvironment() failed (Status %lx)\n", Status);
SetLastError(RtlNtStatusToDosError(Status));
return FALSE;
}
return TRUE; /* Set 'COMPUTERNAME' variable */
Length = MAX_PATH;
if (GetComputerNameW(Buffer,
&Length))
{
SetUserEnvironmentVariable(lpEnvironment,
L"COMPUTERNAME",
Buffer,
FALSE);
}
if (hToken == NULL)
return TRUE;
hKeyUser = GetCurrentUserKey(hToken);
if (hKeyUser == NULL)
{
DPRINT1("GetCurrentUserKey() failed\n");
RtlDestroyEnvironment(*lpEnvironment);
return FALSE;
}
/* Set 'ALLUSERSPROFILE' variable */
Length = MAX_PATH;
if (GetAllUsersProfileDirectoryW(Buffer,
&Length))
{
SetUserEnvironmentVariable(lpEnvironment,
L"ALLUSERSPROFILE",
Buffer,
FALSE);
}
/* Set 'USERPROFILE' variable */
Length = MAX_PATH;
if (GetUserProfileDirectoryW(hToken,
Buffer,
&Length))
{
SetUserEnvironmentVariable(lpEnvironment,
L"USERPROFILE",
Buffer,
FALSE);
}
/* FIXME: Set 'USERDOMAIN' variable */
Length = MAX_PATH;
if (GetUserNameW(Buffer,
&Length))
{
SetUserEnvironmentVariable(lpEnvironment,
L"USERNAME",
Buffer,
FALSE);
}
/* Set user environment variables */
SetUserEnvironment(lpEnvironment,
hKeyUser,
L"Environment");
RegCloseKey(hKeyUser);
return TRUE;
}
BOOL WINAPI
DestroyEnvironmentBlock(LPVOID lpEnvironment)
{
DPRINT("DestroyEnvironmentBlock() called\n");
if (lpEnvironment == NULL)
{
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
RtlDestroyEnvironment(lpEnvironment);
return TRUE;
} }