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
34 SetUserEnvironmentVariable(LPVOID
*Environment
,
39 WCHAR ShortName
[MAX_PATH
];
41 UNICODE_STRING SrcValue
;
42 UNICODE_STRING DstValue
;
49 RtlInitUnicodeString(&SrcValue
,
52 Length
= 2 * MAX_PATH
* sizeof(WCHAR
);
55 DstValue
.MaximumLength
= Length
;
56 DstValue
.Buffer
= Buffer
= LocalAlloc(LPTR
,
58 if (DstValue
.Buffer
== NULL
)
60 DPRINT1("LocalAlloc() failed\n");
64 Status
= RtlExpandEnvironmentStrings_U((PWSTR
)*Environment
,
68 if (!NT_SUCCESS(Status
))
70 DPRINT1("RtlExpandEnvironmentStrings_U() failed (Status %lx)\n", Status
);
71 DPRINT1("Length %lu\n", Length
);
79 RtlInitUnicodeString(&DstValue
,
83 if (!_wcsicmp(lpName
, L
"temp") || !_wcsicmp(lpName
, L
"tmp"))
85 if (!GetShortPathNameW(DstValue
.Buffer
, ShortName
, MAX_PATH
))
87 DPRINT1("GetShortPathNameW() failed for %S (Error %lu)\n", DstValue
.Buffer
, GetLastError());
93 DPRINT("Buffer: %S\n", ShortName
);
94 RtlInitUnicodeString(&DstValue
,
98 RtlInitUnicodeString(&Name
,
101 DPRINT("Value: %wZ\n", &DstValue
);
103 Status
= RtlSetEnvironmentVariable((PWSTR
*)Environment
,
110 if (!NT_SUCCESS(Status
))
112 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
);
171 GetCurrentUserKey(HANDLE hToken
)
173 UNICODE_STRING SidString
;
177 if (!GetUserSidStringFromToken(hToken
,
180 DPRINT1("GetUserSidFromToken() failed\n");
184 Error
= RegOpenKeyExW(HKEY_USERS
,
189 if (Error
!= ERROR_SUCCESS
)
191 DPRINT1("RegOpenKeyExW() failed (Error %ld)\n", Error
);
192 RtlFreeUnicodeString(&SidString
);
193 SetLastError((DWORD
)Error
);
197 RtlFreeUnicodeString(&SidString
);
205 GetUserAndDomainName(IN HANDLE hToken
,
206 OUT LPWSTR
*UserName
,
207 OUT LPWSTR
*DomainName
)
210 LPWSTR lpUserName
= NULL
;
211 LPWSTR lpDomainName
= NULL
;
212 DWORD cbUserName
= 0;
213 DWORD cbDomainName
= 0;
214 SID_NAME_USE SidNameUse
;
217 if (!GetUserSidFromToken(hToken
,
220 DPRINT1("GetUserSidFromToken() failed\n");
224 if (!LookupAccountSidW(NULL
,
232 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER
)
239 lpUserName
= LocalAlloc(LPTR
,
240 cbUserName
* sizeof(WCHAR
));
241 if (lpUserName
== NULL
)
247 lpDomainName
= LocalAlloc(LPTR
,
248 cbDomainName
* sizeof(WCHAR
));
249 if (lpDomainName
== NULL
)
255 if (!LookupAccountSidW(NULL
,
267 *UserName
= lpUserName
;
268 *DomainName
= lpDomainName
;
273 if (lpUserName
!= NULL
)
274 LocalFree(lpUserName
);
276 if (lpDomainName
!= NULL
)
277 LocalFree(lpDomainName
);
288 SetUserEnvironment(LPVOID
*lpEnvironment
,
294 DWORD dwMaxValueNameLength
;
295 DWORD dwMaxValueDataLength
;
296 DWORD dwValueNameLength
;
297 DWORD dwValueDataLength
;
304 Error
= RegOpenKeyExW(hKey
,
309 if (Error
!= ERROR_SUCCESS
)
311 DPRINT1("RegOpenKeyExW() failed (Error %ld)\n", Error
);
312 SetLastError((DWORD
)Error
);
316 Error
= RegQueryInfoKey(hEnvKey
,
324 &dwMaxValueNameLength
,
325 &dwMaxValueDataLength
,
328 if (Error
!= ERROR_SUCCESS
)
330 DPRINT1("RegQueryInforKey() failed (Error %ld)\n", Error
);
331 RegCloseKey(hEnvKey
);
332 SetLastError((DWORD
)Error
);
338 RegCloseKey(hEnvKey
);
342 /* Allocate buffers */
343 lpValueName
= LocalAlloc(LPTR
,
344 dwMaxValueNameLength
* sizeof(WCHAR
));
345 if (lpValueName
== NULL
)
347 RegCloseKey(hEnvKey
);
351 lpValueData
= LocalAlloc(LPTR
,
352 dwMaxValueDataLength
);
353 if (lpValueData
== NULL
)
355 LocalFree(lpValueName
);
356 RegCloseKey(hEnvKey
);
360 /* Enumerate values */
361 for (i
= 0; i
< dwValues
; i
++)
363 dwValueNameLength
= dwMaxValueNameLength
;
364 dwValueDataLength
= dwMaxValueDataLength
;
365 RegEnumValueW(hEnvKey
,
374 if (!_wcsicmp (lpValueName
, L
"path"))
376 /* Append 'Path' environment variable */
377 AppendUserEnvironmentVariable(lpEnvironment
,
383 /* Set environment variable */
384 SetUserEnvironmentVariable(lpEnvironment
,
387 (dwType
== REG_EXPAND_SZ
));
391 LocalFree(lpValueData
);
392 LocalFree(lpValueName
);
393 RegCloseKey(hEnvKey
);
401 CreateEnvironmentBlock(LPVOID
*lpEnvironment
,
405 WCHAR Buffer
[MAX_PATH
];
411 LPWSTR lpUserName
= NULL
;
412 LPWSTR lpDomainName
= NULL
;
416 DPRINT("CreateEnvironmentBlock() called\n");
418 if (lpEnvironment
== NULL
)
420 SetLastError(ERROR_INVALID_PARAMETER
);
424 Status
= RtlCreateEnvironment((BOOLEAN
)bInherit
,
425 (PWSTR
*)lpEnvironment
);
426 if (!NT_SUCCESS (Status
))
428 DPRINT1("RtlCreateEnvironment() failed (Status %lx)\n", Status
);
429 SetLastError(RtlNtStatusToDosError(Status
));
433 /* Set 'COMPUTERNAME' variable */
435 if (GetComputerNameW(Buffer
,
438 SetUserEnvironmentVariable(lpEnvironment
,
444 /* Set 'ALLUSERSPROFILE' variable */
446 if (GetAllUsersProfileDirectoryW(Buffer
,
449 SetUserEnvironmentVariable(lpEnvironment
,
455 lError
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
456 L
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion",
460 if (lError
== ERROR_SUCCESS
)
462 Length
= 1024 * sizeof(WCHAR
);
463 lError
= RegQueryValueExW(hKey
,
469 if (lError
== ERROR_SUCCESS
)
471 SetUserEnvironmentVariable(lpEnvironment
,
477 Length
= 1024 * sizeof(WCHAR
);
478 lError
= RegQueryValueExW(hKey
,
484 if (lError
== ERROR_SUCCESS
)
486 SetUserEnvironmentVariable(lpEnvironment
,
487 L
"CommonProgramFiles",
498 hKeyUser
= GetCurrentUserKey(hToken
);
499 if (hKeyUser
== NULL
)
501 DPRINT1("GetCurrentUserKey() failed\n");
502 RtlDestroyEnvironment(*lpEnvironment
);
506 /* Set 'USERPROFILE' variable */
508 if (GetUserProfileDirectoryW(hToken
,
512 SetUserEnvironmentVariable(lpEnvironment
,
518 if (GetUserAndDomainName(hToken
,
522 /* Set 'USERDOMAIN' variable */
523 SetUserEnvironmentVariable(lpEnvironment
,
528 /* Set 'USERNAME' variable */
529 SetUserEnvironmentVariable(lpEnvironment
,
535 /* Set user environment variables */
536 SetUserEnvironment(lpEnvironment
,
540 /* Set user volatile environment variables */
541 SetUserEnvironment(lpEnvironment
,
543 L
"Volatile Environment");
545 RegCloseKey(hKeyUser
);
547 if (lpUserName
!= NULL
)
548 LocalFree(lpUserName
);
550 if (lpDomainName
!= NULL
)
551 LocalFree(lpDomainName
);
559 DestroyEnvironmentBlock(LPVOID lpEnvironment
)
561 DPRINT("DestroyEnvironmentBlock() called\n");
563 if (lpEnvironment
== NULL
)
565 SetLastError(ERROR_INVALID_PARAMETER
);
569 RtlDestroyEnvironment(lpEnvironment
);
577 ExpandEnvironmentStringsForUserW(IN HANDLE hToken
,
585 if (lpSrc
== NULL
|| lpDest
== NULL
|| dwSize
== 0)
587 SetLastError(ERROR_INVALID_PARAMETER
);
591 if (CreateEnvironmentBlock(&lpEnvironment
,
595 UNICODE_STRING SrcU
, DestU
;
598 /* initialize the strings */
599 RtlInitUnicodeString(&SrcU
,
602 DestU
.MaximumLength
= dwSize
* sizeof(WCHAR
);
603 DestU
.Buffer
= lpDest
;
605 /* expand the strings */
606 Status
= RtlExpandEnvironmentStrings_U((PWSTR
)lpEnvironment
,
611 DestroyEnvironmentBlock(lpEnvironment
);
613 if (NT_SUCCESS(Status
))
619 SetLastError(RtlNtStatusToDosError(Status
));
629 ExpandEnvironmentStringsForUserA(IN HANDLE hToken
,
635 LPWSTR lpSrcW
= NULL
, lpDestW
= NULL
;
638 if (lpSrc
== NULL
|| lpDest
== NULL
|| dwSize
== 0)
640 SetLastError(ERROR_INVALID_PARAMETER
);
644 dwSrcLen
= strlen(lpSrc
);
645 lpSrcW
= (LPWSTR
)GlobalAlloc(GMEM_FIXED
,
646 (dwSrcLen
+ 1) * sizeof(WCHAR
));
647 if (lpSrcW
== NULL
||
648 MultiByteToWideChar(CP_ACP
,
658 lpDestW
= (LPWSTR
)GlobalAlloc(GMEM_FIXED
,
659 dwSize
* sizeof(WCHAR
));
665 Ret
= ExpandEnvironmentStringsForUserW(hToken
,
671 if (WideCharToMultiByte(CP_ACP
,
687 GlobalFree((HGLOBAL
)lpSrcW
);
692 GlobalFree((HGLOBAL
)lpDestW
);