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
32 SetUserEnvironmentVariable (LPVOID
*Environment
,
37 WCHAR ShortName
[MAX_PATH
];
39 UNICODE_STRING SrcValue
;
40 UNICODE_STRING DstValue
;
47 RtlInitUnicodeString(&SrcValue
,
50 Length
= 2 * MAX_PATH
* sizeof(WCHAR
);
53 DstValue
.MaximumLength
= Length
;
54 DstValue
.Buffer
= Buffer
= LocalAlloc(LPTR
,
57 if (DstValue
.Buffer
== NULL
)
59 DPRINT1("LocalAlloc() failed\n");
63 Status
= RtlExpandEnvironmentStrings_U((PWSTR
)*Environment
,
67 if (!NT_SUCCESS(Status
))
69 DPRINT1("RtlExpandEnvironmentStrings_U() failed (Status %lx)\n", Status
);
70 DPRINT1("Length %lu\n", Length
);
71 if (Buffer
) LocalFree(Buffer
);
77 RtlInitUnicodeString(&DstValue
,
81 if (!_wcsicmp (lpName
, L
"temp") || !_wcsicmp (lpName
, L
"tmp"))
83 if (!GetShortPathNameW(DstValue
.Buffer
, ShortName
, MAX_PATH
))
85 DPRINT1("GetShortPathNameW() failed (Error %lu)\n", GetLastError());
86 if (Buffer
) LocalFree(Buffer
);
90 DPRINT("Buffer: %S\n", ShortName
);
91 RtlInitUnicodeString(&DstValue
,
95 RtlInitUnicodeString(&Name
,
98 DPRINT("Value: %wZ\n", &DstValue
);
100 Status
= RtlSetEnvironmentVariable((PWSTR
*)Environment
,
104 if (Buffer
) LocalFree(Buffer
);
106 if (!NT_SUCCESS(Status
))
108 DPRINT1("RtlSetEnvironmentVariable() failed (Status %lx)\n", Status
);
117 AppendUserEnvironmentVariable (LPVOID
*Environment
,
122 UNICODE_STRING Value
;
125 RtlInitUnicodeString (&Name
,
129 Value
.MaximumLength
= 1024 * sizeof(WCHAR
);
130 Value
.Buffer
= LocalAlloc (LPTR
,
131 1024 * sizeof(WCHAR
));
132 if (Value
.Buffer
== NULL
)
136 Value
.Buffer
[0] = UNICODE_NULL
;
138 Status
= RtlQueryEnvironmentVariable_U ((PWSTR
)*Environment
,
141 if (NT_SUCCESS(Status
))
143 RtlAppendUnicodeToString (&Value
,
147 RtlAppendUnicodeToString (&Value
,
150 Status
= RtlSetEnvironmentVariable ((PWSTR
*)Environment
,
153 LocalFree (Value
.Buffer
);
154 if (!NT_SUCCESS(Status
))
156 DPRINT1 ("RtlSetEnvironmentVariable() failed (Status %lx)\n", Status
);
165 GetCurrentUserKey (HANDLE hToken
)
167 UNICODE_STRING SidString
;
170 if (!GetUserSidFromToken (hToken
,
173 DPRINT1 ("GetUserSidFromToken() failed\n");
177 if (RegOpenKeyExW (HKEY_USERS
,
183 DPRINT1 ("RegOpenKeyExW() failed (Error %ld)\n", GetLastError());
184 RtlFreeUnicodeString (&SidString
);
188 RtlFreeUnicodeString (&SidString
);
195 SetUserEnvironment (LPVOID
*lpEnvironment
,
201 DWORD dwMaxValueNameLength
;
202 DWORD dwMaxValueDataLength
;
203 DWORD dwValueNameLength
;
204 DWORD dwValueDataLength
;
210 if (RegOpenKeyExW (hKey
,
216 DPRINT1 ("RegOpenKeyExW() failed (Error %ld)\n", GetLastError());
220 if (RegQueryInfoKey (hEnvKey
,
228 &dwMaxValueNameLength
,
229 &dwMaxValueDataLength
,
233 DPRINT1 ("RegQueryInforKey() failed (Error %ld)\n", GetLastError());
234 RegCloseKey (hEnvKey
);
240 RegCloseKey (hEnvKey
);
244 /* Allocate buffers */
245 lpValueName
= LocalAlloc (LPTR
,
246 dwMaxValueNameLength
* sizeof(WCHAR
));
247 if (lpValueName
== NULL
)
249 RegCloseKey (hEnvKey
);
253 lpValueData
= LocalAlloc (LPTR
,
254 dwMaxValueDataLength
);
255 if (lpValueData
== NULL
)
257 LocalFree (lpValueName
);
258 RegCloseKey (hEnvKey
);
262 /* Enumerate values */
263 for (i
= 0; i
< dwValues
; i
++)
265 dwValueNameLength
= dwMaxValueNameLength
;
266 dwValueDataLength
= dwMaxValueDataLength
;
267 RegEnumValueW (hEnvKey
,
276 if (!_wcsicmp (lpValueName
, L
"path"))
278 /* Append 'Path' environment variable */
279 AppendUserEnvironmentVariable (lpEnvironment
,
285 /* Set environment variable */
286 SetUserEnvironmentVariable (lpEnvironment
,
289 (dwType
== REG_EXPAND_SZ
));
293 LocalFree (lpValueData
);
294 LocalFree (lpValueName
);
295 RegCloseKey (hEnvKey
);
302 CreateEnvironmentBlock (LPVOID
*lpEnvironment
,
306 WCHAR Buffer
[MAX_PATH
];
311 DPRINT("CreateEnvironmentBlock() called\n");
313 if (lpEnvironment
== NULL
)
316 Status
= RtlCreateEnvironment ((BOOLEAN
)bInherit
,
317 (PWSTR
*)lpEnvironment
);
318 if (!NT_SUCCESS (Status
))
320 DPRINT1 ("RtlCreateEnvironment() failed (Status %lx)\n", Status
);
324 /* Set 'COMPUTERNAME' variable */
326 if (GetComputerNameW (Buffer
,
329 SetUserEnvironmentVariable(lpEnvironment
,
338 hKeyUser
= GetCurrentUserKey (hToken
);
339 if (hKeyUser
== NULL
)
341 DPRINT1 ("GetCurrentUserKey() failed\n");
342 RtlDestroyEnvironment (*lpEnvironment
);
346 /* Set 'ALLUSERSPROFILE' variable */
348 if (GetAllUsersProfileDirectoryW (Buffer
,
351 SetUserEnvironmentVariable(lpEnvironment
,
357 /* Set 'USERPROFILE' variable */
359 if (GetUserProfileDirectoryW (hToken
,
363 SetUserEnvironmentVariable(lpEnvironment
,
369 /* FIXME: Set 'USERDOMAIN' variable */
371 /* FIXME: Set 'USERNAME' variable */
373 /* Set user environment variables */
374 SetUserEnvironment (lpEnvironment
,
378 RegCloseKey (hKeyUser
);
385 DestroyEnvironmentBlock (LPVOID lpEnvironment
)
387 DPRINT ("DestroyEnvironmentBlock() called\n");
389 if (lpEnvironment
== NULL
)
392 RtlDestroyEnvironment (lpEnvironment
);
399 ExpandEnvironmentStringsForUserW(IN HANDLE hToken
,
407 if (lpSrc
== NULL
|| lpDest
== NULL
|| dwSize
== 0)
409 SetLastError(ERROR_INVALID_PARAMETER
);
413 if (CreateEnvironmentBlock(&lpEnvironment
,
417 UNICODE_STRING SrcU
, DestU
;
420 /* initialize the strings */
421 RtlInitUnicodeString(&SrcU
,
424 DestU
.MaximumLength
= dwSize
* sizeof(WCHAR
);
425 DestU
.Buffer
= lpDest
;
427 /* expand the strings */
428 Status
= RtlExpandEnvironmentStrings_U((PWSTR
)lpEnvironment
,
433 DestroyEnvironmentBlock(lpEnvironment
);
435 if (NT_SUCCESS(Status
))
441 SetLastError(RtlNtStatusToDosError(Status
));
450 ExpandEnvironmentStringsForUserA(IN HANDLE hToken
,
456 LPWSTR lpSrcW
= NULL
, lpDestW
= NULL
;
459 if (lpSrc
== NULL
|| lpDest
== NULL
|| dwSize
== 0)
461 SetLastError(ERROR_INVALID_PARAMETER
);
465 dwSrcLen
= strlen(lpSrc
);
466 lpSrcW
= (LPWSTR
)GlobalAlloc(GMEM_FIXED
,
467 (dwSrcLen
+ 1) * sizeof(WCHAR
));
468 if (lpSrcW
== NULL
||
469 MultiByteToWideChar(CP_ACP
,
479 lpDestW
= (LPWSTR
)GlobalAlloc(GMEM_FIXED
,
480 dwSize
* sizeof(WCHAR
));
486 Ret
= ExpandEnvironmentStringsForUserW(hToken
,
492 if (WideCharToMultiByte(CP_ACP
,
508 GlobalFree((HGLOBAL
)lpSrcW
);
513 GlobalFree((HGLOBAL
)lpDestW
);