3 * Copyright (C) 2004 ReactOS Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 * COPYRIGHT: See COPYING in the top level directory
21 * PROJECT: ReactOS system libraries
22 * FILE: dll/win32/userenv/environment.c
23 * PURPOSE: User environment functions
24 * PROGRAMMER: Eric Kohl
35 SetUserEnvironmentVariable(LPVOID
*Environment
,
40 WCHAR ShortName
[MAX_PATH
];
42 UNICODE_STRING SrcValue
;
43 UNICODE_STRING DstValue
;
50 RtlInitUnicodeString(&SrcValue
,
53 Length
= 2 * MAX_PATH
* sizeof(WCHAR
);
56 DstValue
.MaximumLength
= Length
;
57 DstValue
.Buffer
= Buffer
= LocalAlloc(LPTR
,
59 if (DstValue
.Buffer
== NULL
)
61 DPRINT1("LocalAlloc() failed\n");
65 Status
= RtlExpandEnvironmentStrings_U((PWSTR
)*Environment
,
69 if (!NT_SUCCESS(Status
))
71 DPRINT1("RtlExpandEnvironmentStrings_U() failed (Status %lx)\n", Status
);
72 DPRINT1("Length %lu\n", Length
);
80 RtlInitUnicodeString(&DstValue
,
84 if (!_wcsicmp(lpName
, L
"temp") || !_wcsicmp(lpName
, L
"tmp"))
86 if (!GetShortPathNameW(DstValue
.Buffer
, ShortName
, MAX_PATH
))
88 DPRINT1("GetShortPathNameW() failed for %S (Error %lu)\n", DstValue
.Buffer
, GetLastError());
94 DPRINT("Buffer: %S\n", ShortName
);
95 RtlInitUnicodeString(&DstValue
,
99 RtlInitUnicodeString(&Name
,
102 DPRINT("Value: %wZ\n", &DstValue
);
104 Status
= RtlSetEnvironmentVariable((PWSTR
*)Environment
,
111 if (!NT_SUCCESS(Status
))
113 DPRINT1("RtlSetEnvironmentVariable() failed (Status %lx)\n", Status
);
123 AppendUserEnvironmentVariable(LPVOID
*Environment
,
128 UNICODE_STRING Value
;
131 RtlInitUnicodeString(&Name
,
135 Value
.MaximumLength
= 1024 * sizeof(WCHAR
);
136 Value
.Buffer
= LocalAlloc(LPTR
,
137 1024 * sizeof(WCHAR
));
138 if (Value
.Buffer
== NULL
)
142 Value
.Buffer
[0] = UNICODE_NULL
;
144 Status
= RtlQueryEnvironmentVariable_U((PWSTR
)*Environment
,
147 if (NT_SUCCESS(Status
))
149 RtlAppendUnicodeToString(&Value
,
153 RtlAppendUnicodeToString(&Value
,
156 Status
= RtlSetEnvironmentVariable((PWSTR
*)Environment
,
159 LocalFree(Value
.Buffer
);
160 if (!NT_SUCCESS(Status
))
162 DPRINT1("RtlSetEnvironmentVariable() failed (Status %lx)\n", Status
);
172 GetCurrentUserKey(HANDLE hToken
)
174 UNICODE_STRING SidString
;
178 if (!GetUserSidFromToken(hToken
,
181 DPRINT1("GetUserSidFromToken() failed\n");
185 Error
= RegOpenKeyExW(HKEY_USERS
,
190 if (Error
!= ERROR_SUCCESS
)
192 DPRINT1("RegOpenKeyExW() failed (Error %ld)\n", Error
);
193 RtlFreeUnicodeString(&SidString
);
194 SetLastError((DWORD
)Error
);
198 RtlFreeUnicodeString(&SidString
);
206 SetUserEnvironment(LPVOID
*lpEnvironment
,
212 DWORD dwMaxValueNameLength
;
213 DWORD dwMaxValueDataLength
;
214 DWORD dwValueNameLength
;
215 DWORD dwValueDataLength
;
222 Error
= RegOpenKeyExW(hKey
,
227 if (Error
!= ERROR_SUCCESS
)
229 DPRINT1("RegOpenKeyExW() failed (Error %ld)\n", Error
);
230 SetLastError((DWORD
)Error
);
234 Error
= RegQueryInfoKey(hEnvKey
,
242 &dwMaxValueNameLength
,
243 &dwMaxValueDataLength
,
246 if (Error
!= ERROR_SUCCESS
)
248 DPRINT1("RegQueryInforKey() failed (Error %ld)\n", Error
);
249 RegCloseKey(hEnvKey
);
250 SetLastError((DWORD
)Error
);
256 RegCloseKey(hEnvKey
);
260 /* Allocate buffers */
261 lpValueName
= LocalAlloc(LPTR
,
262 dwMaxValueNameLength
* sizeof(WCHAR
));
263 if (lpValueName
== NULL
)
265 RegCloseKey(hEnvKey
);
269 lpValueData
= LocalAlloc(LPTR
,
270 dwMaxValueDataLength
);
271 if (lpValueData
== NULL
)
273 LocalFree(lpValueName
);
274 RegCloseKey(hEnvKey
);
278 /* Enumerate values */
279 for (i
= 0; i
< dwValues
; i
++)
281 dwValueNameLength
= dwMaxValueNameLength
;
282 dwValueDataLength
= dwMaxValueDataLength
;
283 RegEnumValueW(hEnvKey
,
292 if (!_wcsicmp (lpValueName
, L
"path"))
294 /* Append 'Path' environment variable */
295 AppendUserEnvironmentVariable(lpEnvironment
,
301 /* Set environment variable */
302 SetUserEnvironmentVariable(lpEnvironment
,
305 (dwType
== REG_EXPAND_SZ
));
309 LocalFree(lpValueData
);
310 LocalFree(lpValueName
);
311 RegCloseKey(hEnvKey
);
319 CreateEnvironmentBlock(LPVOID
*lpEnvironment
,
323 WCHAR Buffer
[MAX_PATH
];
332 DPRINT("CreateEnvironmentBlock() called\n");
334 if (lpEnvironment
== NULL
)
336 SetLastError(ERROR_INVALID_PARAMETER
);
340 Status
= RtlCreateEnvironment((BOOLEAN
)bInherit
,
341 (PWSTR
*)lpEnvironment
);
342 if (!NT_SUCCESS (Status
))
344 DPRINT1("RtlCreateEnvironment() failed (Status %lx)\n", Status
);
345 SetLastError(RtlNtStatusToDosError(Status
));
349 /* Set 'COMPUTERNAME' variable */
351 if (GetComputerNameW(Buffer
,
354 SetUserEnvironmentVariable(lpEnvironment
,
360 /* Set 'ALLUSERSPROFILE' variable */
362 if (GetAllUsersProfileDirectoryW(Buffer
,
365 SetUserEnvironmentVariable(lpEnvironment
,
371 lError
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
372 L
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion",
376 if (lError
== ERROR_SUCCESS
)
378 Length
= 1024 * sizeof(WCHAR
);
379 lError
= RegQueryValueExW(hKey
,
385 if (lError
== ERROR_SUCCESS
)
387 SetUserEnvironmentVariable(lpEnvironment
,
393 Length
= 1024 * sizeof(WCHAR
);
394 lError
= RegQueryValueExW(hKey
,
400 if (lError
== ERROR_SUCCESS
)
402 SetUserEnvironmentVariable(lpEnvironment
,
403 L
"CommonProgramFiles",
414 hKeyUser
= GetCurrentUserKey(hToken
);
415 if (hKeyUser
== NULL
)
417 DPRINT1("GetCurrentUserKey() failed\n");
418 RtlDestroyEnvironment(*lpEnvironment
);
422 /* Set 'USERPROFILE' variable */
424 if (GetUserProfileDirectoryW(hToken
,
428 SetUserEnvironmentVariable(lpEnvironment
,
434 /* FIXME: Set 'USERDOMAIN' variable */
437 if (GetUserNameW(Buffer
,
440 SetUserEnvironmentVariable(lpEnvironment
,
446 /* Set user environment variables */
447 SetUserEnvironment(lpEnvironment
,
451 /* Set user volatile environment variables */
452 SetUserEnvironment(lpEnvironment
,
454 L
"Volatile Environment");
456 RegCloseKey(hKeyUser
);
464 DestroyEnvironmentBlock(LPVOID lpEnvironment
)
466 DPRINT("DestroyEnvironmentBlock() called\n");
468 if (lpEnvironment
== NULL
)
470 SetLastError(ERROR_INVALID_PARAMETER
);
474 RtlDestroyEnvironment(lpEnvironment
);
482 ExpandEnvironmentStringsForUserW(IN HANDLE hToken
,
490 if (lpSrc
== NULL
|| lpDest
== NULL
|| dwSize
== 0)
492 SetLastError(ERROR_INVALID_PARAMETER
);
496 if (CreateEnvironmentBlock(&lpEnvironment
,
500 UNICODE_STRING SrcU
, DestU
;
503 /* initialize the strings */
504 RtlInitUnicodeString(&SrcU
,
507 DestU
.MaximumLength
= dwSize
* sizeof(WCHAR
);
508 DestU
.Buffer
= lpDest
;
510 /* expand the strings */
511 Status
= RtlExpandEnvironmentStrings_U((PWSTR
)lpEnvironment
,
516 DestroyEnvironmentBlock(lpEnvironment
);
518 if (NT_SUCCESS(Status
))
524 SetLastError(RtlNtStatusToDosError(Status
));
534 ExpandEnvironmentStringsForUserA(IN HANDLE hToken
,
540 LPWSTR lpSrcW
= NULL
, lpDestW
= NULL
;
543 if (lpSrc
== NULL
|| lpDest
== NULL
|| dwSize
== 0)
545 SetLastError(ERROR_INVALID_PARAMETER
);
549 dwSrcLen
= strlen(lpSrc
);
550 lpSrcW
= (LPWSTR
)GlobalAlloc(GMEM_FIXED
,
551 (dwSrcLen
+ 1) * sizeof(WCHAR
));
552 if (lpSrcW
== NULL
||
553 MultiByteToWideChar(CP_ACP
,
563 lpDestW
= (LPWSTR
)GlobalAlloc(GMEM_FIXED
,
564 dwSize
* sizeof(WCHAR
));
570 Ret
= ExpandEnvironmentStringsForUserW(hToken
,
576 if (WideCharToMultiByte(CP_ACP
,
592 GlobalFree((HGLOBAL
)lpSrcW
);
597 GlobalFree((HGLOBAL
)lpDestW
);