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.
21 * COPYRIGHT: See COPYING in the top level directory
22 * PROJECT: ReactOS system libraries
23 * FILE: lib/userenv/environment.c
24 * PURPOSE: User environment functions
25 * 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
);
122 AppendUserEnvironmentVariable(LPVOID
*Environment
,
127 UNICODE_STRING Value
;
130 RtlInitUnicodeString(&Name
,
134 Value
.MaximumLength
= 1024 * sizeof(WCHAR
);
135 Value
.Buffer
= LocalAlloc(LPTR
,
136 1024 * sizeof(WCHAR
));
137 if (Value
.Buffer
== NULL
)
141 Value
.Buffer
[0] = UNICODE_NULL
;
143 Status
= RtlQueryEnvironmentVariable_U((PWSTR
)*Environment
,
146 if (NT_SUCCESS(Status
))
148 RtlAppendUnicodeToString(&Value
,
152 RtlAppendUnicodeToString(&Value
,
155 Status
= RtlSetEnvironmentVariable((PWSTR
*)Environment
,
158 LocalFree(Value
.Buffer
);
159 if (!NT_SUCCESS(Status
))
161 DPRINT1("RtlSetEnvironmentVariable() failed (Status %lx)\n", Status
);
170 GetCurrentUserKey(HANDLE hToken
)
172 UNICODE_STRING SidString
;
176 if (!GetUserSidFromToken(hToken
,
179 DPRINT1("GetUserSidFromToken() failed\n");
183 Error
= RegOpenKeyExW(HKEY_USERS
,
188 if (Error
!= ERROR_SUCCESS
)
190 DPRINT1("RegOpenKeyExW() failed (Error %ld)\n", Error
);
191 RtlFreeUnicodeString(&SidString
);
192 SetLastError((DWORD
)Error
);
196 RtlFreeUnicodeString(&SidString
);
203 SetUserEnvironment(LPVOID
*lpEnvironment
,
209 DWORD dwMaxValueNameLength
;
210 DWORD dwMaxValueDataLength
;
211 DWORD dwValueNameLength
;
212 DWORD dwValueDataLength
;
219 Error
= RegOpenKeyExW(hKey
,
224 if (Error
!= ERROR_SUCCESS
)
226 DPRINT1("RegOpenKeyExW() failed (Error %ld)\n", Error
);
227 SetLastError((DWORD
)Error
);
231 Error
= RegQueryInfoKey(hEnvKey
,
239 &dwMaxValueNameLength
,
240 &dwMaxValueDataLength
,
243 if (Error
!= ERROR_SUCCESS
)
245 DPRINT1("RegQueryInforKey() failed (Error %ld)\n", Error
);
246 RegCloseKey(hEnvKey
);
247 SetLastError((DWORD
)Error
);
253 RegCloseKey(hEnvKey
);
257 /* Allocate buffers */
258 lpValueName
= LocalAlloc(LPTR
,
259 dwMaxValueNameLength
* sizeof(WCHAR
));
260 if (lpValueName
== NULL
)
262 RegCloseKey(hEnvKey
);
266 lpValueData
= LocalAlloc(LPTR
,
267 dwMaxValueDataLength
);
268 if (lpValueData
== NULL
)
270 LocalFree(lpValueName
);
271 RegCloseKey(hEnvKey
);
275 /* Enumerate values */
276 for (i
= 0; i
< dwValues
; i
++)
278 dwValueNameLength
= dwMaxValueNameLength
;
279 dwValueDataLength
= dwMaxValueDataLength
;
280 RegEnumValueW(hEnvKey
,
289 if (!_wcsicmp (lpValueName
, L
"path"))
291 /* Append 'Path' environment variable */
292 AppendUserEnvironmentVariable(lpEnvironment
,
298 /* Set environment variable */
299 SetUserEnvironmentVariable(lpEnvironment
,
302 (dwType
== REG_EXPAND_SZ
));
306 LocalFree(lpValueData
);
307 LocalFree(lpValueName
);
308 RegCloseKey(hEnvKey
);
315 CreateEnvironmentBlock(LPVOID
*lpEnvironment
,
319 WCHAR Buffer
[MAX_PATH
];
328 DPRINT("CreateEnvironmentBlock() called\n");
330 if (lpEnvironment
== NULL
)
332 SetLastError(ERROR_INVALID_PARAMETER
);
336 Status
= RtlCreateEnvironment((BOOLEAN
)bInherit
,
337 (PWSTR
*)lpEnvironment
);
338 if (!NT_SUCCESS (Status
))
340 DPRINT1("RtlCreateEnvironment() failed (Status %lx)\n", Status
);
341 SetLastError(RtlNtStatusToDosError(Status
));
345 /* Set 'COMPUTERNAME' variable */
347 if (GetComputerNameW(Buffer
,
350 SetUserEnvironmentVariable(lpEnvironment
,
356 /* Set 'ALLUSERSPROFILE' variable */
358 if (GetAllUsersProfileDirectoryW(Buffer
,
361 SetUserEnvironmentVariable(lpEnvironment
,
367 lError
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
368 L
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion",
372 if (lError
== ERROR_SUCCESS
)
374 Length
= 1024 * sizeof(WCHAR
);
375 lError
= RegQueryValueExW(hKey
,
381 if (lError
== ERROR_SUCCESS
)
383 SetUserEnvironmentVariable(lpEnvironment
,
389 Length
= 1024 * sizeof(WCHAR
);
390 lError
= RegQueryValueExW(hKey
,
396 if (lError
== ERROR_SUCCESS
)
398 SetUserEnvironmentVariable(lpEnvironment
,
399 L
"CommonProgramFiles",
410 hKeyUser
= GetCurrentUserKey(hToken
);
411 if (hKeyUser
== NULL
)
413 DPRINT1("GetCurrentUserKey() failed\n");
414 RtlDestroyEnvironment(*lpEnvironment
);
418 /* Set 'USERPROFILE' variable */
420 if (GetUserProfileDirectoryW(hToken
,
424 SetUserEnvironmentVariable(lpEnvironment
,
430 /* FIXME: Set 'USERDOMAIN' variable */
433 if (GetUserNameW(Buffer
,
436 SetUserEnvironmentVariable(lpEnvironment
,
442 /* Set user environment variables */
443 SetUserEnvironment(lpEnvironment
,
447 /* Set user volatile environment variables */
448 SetUserEnvironment(lpEnvironment
,
450 L
"Volatile Environment");
452 RegCloseKey(hKeyUser
);
459 DestroyEnvironmentBlock(LPVOID lpEnvironment
)
461 DPRINT("DestroyEnvironmentBlock() called\n");
463 if (lpEnvironment
== NULL
)
465 SetLastError(ERROR_INVALID_PARAMETER
);
469 RtlDestroyEnvironment(lpEnvironment
);
476 ExpandEnvironmentStringsForUserW(IN HANDLE hToken
,
484 if (lpSrc
== NULL
|| lpDest
== NULL
|| dwSize
== 0)
486 SetLastError(ERROR_INVALID_PARAMETER
);
490 if (CreateEnvironmentBlock(&lpEnvironment
,
494 UNICODE_STRING SrcU
, DestU
;
497 /* initialize the strings */
498 RtlInitUnicodeString(&SrcU
,
501 DestU
.MaximumLength
= dwSize
* sizeof(WCHAR
);
502 DestU
.Buffer
= lpDest
;
504 /* expand the strings */
505 Status
= RtlExpandEnvironmentStrings_U((PWSTR
)lpEnvironment
,
510 DestroyEnvironmentBlock(lpEnvironment
);
512 if (NT_SUCCESS(Status
))
518 SetLastError(RtlNtStatusToDosError(Status
));
527 ExpandEnvironmentStringsForUserA(IN HANDLE hToken
,
533 LPWSTR lpSrcW
= NULL
, lpDestW
= NULL
;
536 if (lpSrc
== NULL
|| lpDest
== NULL
|| dwSize
== 0)
538 SetLastError(ERROR_INVALID_PARAMETER
);
542 dwSrcLen
= strlen(lpSrc
);
543 lpSrcW
= (LPWSTR
)GlobalAlloc(GMEM_FIXED
,
544 (dwSrcLen
+ 1) * sizeof(WCHAR
));
545 if (lpSrcW
== NULL
||
546 MultiByteToWideChar(CP_ACP
,
556 lpDestW
= (LPWSTR
)GlobalAlloc(GMEM_FIXED
,
557 dwSize
* sizeof(WCHAR
));
563 Ret
= ExpandEnvironmentStringsForUserW(hToken
,
569 if (WideCharToMultiByte(CP_ACP
,
585 GlobalFree((HGLOBAL
)lpSrcW
);
590 GlobalFree((HGLOBAL
)lpDestW
);