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
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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
,
60 if (DstValue
.Buffer
== NULL
)
62 DPRINT1("LocalAlloc() failed\n");
66 Status
= RtlExpandEnvironmentStrings_U((PWSTR
)*Environment
,
70 if (!NT_SUCCESS(Status
))
72 DPRINT1("RtlExpandEnvironmentStrings_U() failed (Status %lx)\n", Status
);
73 DPRINT1("Length %lu\n", Length
);
74 if (Buffer
) LocalFree(Buffer
);
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 (Error %lu)\n", GetLastError());
89 if (Buffer
) LocalFree(Buffer
);
93 DPRINT("Buffer: %S\n", ShortName
);
94 RtlInitUnicodeString(&DstValue
,
98 RtlInitUnicodeString(&Name
,
101 DPRINT("Value: %wZ\n", &DstValue
);
103 Status
= RtlSetEnvironmentVariable((PWSTR
*)Environment
,
107 if (Buffer
) LocalFree(Buffer
);
109 if (!NT_SUCCESS(Status
))
111 DPRINT1("RtlSetEnvironmentVariable() failed (Status %lx)\n", Status
);
120 AppendUserEnvironmentVariable (LPVOID
*Environment
,
125 UNICODE_STRING Value
;
128 RtlInitUnicodeString (&Name
,
132 Value
.MaximumLength
= 1024 * sizeof(WCHAR
);
133 Value
.Buffer
= LocalAlloc (LPTR
,
134 1024 * sizeof(WCHAR
));
135 if (Value
.Buffer
== NULL
)
139 Value
.Buffer
[0] = UNICODE_NULL
;
141 Status
= RtlQueryEnvironmentVariable_U ((PWSTR
)*Environment
,
144 if (NT_SUCCESS(Status
))
146 RtlAppendUnicodeToString (&Value
,
150 RtlAppendUnicodeToString (&Value
,
153 Status
= RtlSetEnvironmentVariable ((PWSTR
*)Environment
,
156 LocalFree (Value
.Buffer
);
157 if (!NT_SUCCESS(Status
))
159 DPRINT1 ("RtlSetEnvironmentVariable() failed (Status %lx)\n", Status
);
168 GetCurrentUserKey (HANDLE hToken
)
170 UNICODE_STRING SidString
;
173 if (!GetUserSidFromToken (hToken
,
176 DPRINT1 ("GetUserSidFromToken() failed\n");
180 if (RegOpenKeyExW (HKEY_USERS
,
186 DPRINT1 ("RegOpenKeyExW() failed (Error %ld)\n", GetLastError());
187 RtlFreeUnicodeString (&SidString
);
191 RtlFreeUnicodeString (&SidString
);
198 SetUserEnvironment (LPVOID
*lpEnvironment
,
204 DWORD dwMaxValueNameLength
;
205 DWORD dwMaxValueDataLength
;
206 DWORD dwValueNameLength
;
207 DWORD dwValueDataLength
;
213 if (RegOpenKeyExW (hKey
,
219 DPRINT1 ("RegOpenKeyExW() failed (Error %ld)\n", GetLastError());
223 if (RegQueryInfoKey (hEnvKey
,
231 &dwMaxValueNameLength
,
232 &dwMaxValueDataLength
,
236 DPRINT1 ("RegQueryInforKey() failed (Error %ld)\n", GetLastError());
237 RegCloseKey (hEnvKey
);
243 RegCloseKey (hEnvKey
);
247 /* Allocate buffers */
248 lpValueName
= LocalAlloc (LPTR
,
249 dwMaxValueNameLength
* sizeof(WCHAR
));
250 if (lpValueName
== NULL
)
252 RegCloseKey (hEnvKey
);
256 lpValueData
= LocalAlloc (LPTR
,
257 dwMaxValueDataLength
);
258 if (lpValueData
== NULL
)
260 LocalFree (lpValueName
);
261 RegCloseKey (hEnvKey
);
265 /* Enumerate values */
266 for (i
= 0; i
< dwValues
; i
++)
268 dwValueNameLength
= dwMaxValueNameLength
;
269 dwValueDataLength
= dwMaxValueDataLength
;
270 RegEnumValueW (hEnvKey
,
279 if (!_wcsicmp (lpValueName
, L
"path"))
281 /* Append 'Path' environment variable */
282 AppendUserEnvironmentVariable (lpEnvironment
,
288 /* Set environment variable */
289 SetUserEnvironmentVariable (lpEnvironment
,
292 (dwType
== REG_EXPAND_SZ
));
296 LocalFree (lpValueData
);
297 LocalFree (lpValueName
);
298 RegCloseKey (hEnvKey
);
305 CreateEnvironmentBlock (LPVOID
*lpEnvironment
,
309 WCHAR Buffer
[MAX_PATH
];
314 DPRINT("CreateEnvironmentBlock() called\n");
316 if (lpEnvironment
== NULL
)
319 Status
= RtlCreateEnvironment ((BOOLEAN
)bInherit
,
320 (PWSTR
*)lpEnvironment
);
321 if (!NT_SUCCESS (Status
))
323 DPRINT1 ("RtlCreateEnvironment() failed (Status %lx)\n", Status
);
327 /* Set 'COMPUTERNAME' variable */
329 if (GetComputerNameW (Buffer
,
332 SetUserEnvironmentVariable(lpEnvironment
,
341 hKeyUser
= GetCurrentUserKey (hToken
);
342 if (hKeyUser
== NULL
)
344 DPRINT1 ("GetCurrentUserKey() failed\n");
345 RtlDestroyEnvironment (*lpEnvironment
);
349 /* Set 'ALLUSERSPROFILE' variable */
351 if (GetAllUsersProfileDirectoryW (Buffer
,
354 SetUserEnvironmentVariable(lpEnvironment
,
360 /* Set 'USERPROFILE' variable */
362 if (GetUserProfileDirectoryW (hToken
,
366 SetUserEnvironmentVariable(lpEnvironment
,
372 /* FIXME: Set 'USERDOMAIN' variable */
374 /* FIXME: Set 'USERNAME' variable */
376 /* Set user environment variables */
377 SetUserEnvironment (lpEnvironment
,
381 RegCloseKey (hKeyUser
);
388 DestroyEnvironmentBlock (LPVOID lpEnvironment
)
390 DPRINT ("DestroyEnvironmentBlock() called\n");
392 if (lpEnvironment
== NULL
)
395 RtlDestroyEnvironment (lpEnvironment
);
402 ExpandEnvironmentStringsForUserW(IN HANDLE hToken
,
410 if (lpSrc
== NULL
|| lpDest
== NULL
|| dwSize
== 0)
412 SetLastError(ERROR_INVALID_PARAMETER
);
416 if (CreateEnvironmentBlock(&lpEnvironment
,
420 UNICODE_STRING SrcU
, DestU
;
423 /* initialize the strings */
424 RtlInitUnicodeString(&SrcU
,
427 DestU
.MaximumLength
= dwSize
* sizeof(WCHAR
);
428 DestU
.Buffer
= lpDest
;
430 /* expand the strings */
431 Status
= RtlExpandEnvironmentStrings_U((PWSTR
)lpEnvironment
,
436 DestroyEnvironmentBlock(lpEnvironment
);
438 if (NT_SUCCESS(Status
))
444 SetLastError(RtlNtStatusToDosError(Status
));
453 ExpandEnvironmentStringsForUserA(IN HANDLE hToken
,
459 LPWSTR lpSrcW
= NULL
, lpDestW
= NULL
;
462 if (lpSrc
== NULL
|| lpDest
== NULL
|| dwSize
== 0)
464 SetLastError(ERROR_INVALID_PARAMETER
);
468 dwSrcLen
= strlen(lpSrc
);
469 lpSrcW
= (LPWSTR
)GlobalAlloc(GMEM_FIXED
,
470 (dwSrcLen
+ 1) * sizeof(WCHAR
));
471 if (lpSrcW
== NULL
||
472 MultiByteToWideChar(CP_ACP
,
482 lpDestW
= (LPWSTR
)GlobalAlloc(GMEM_FIXED
,
483 dwSize
* sizeof(WCHAR
));
489 Ret
= ExpandEnvironmentStringsForUserW(hToken
,
495 if (WideCharToMultiByte(CP_ACP
,
511 GlobalFree((HGLOBAL
)lpSrcW
);
516 GlobalFree((HGLOBAL
)lpDestW
);