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.
19 /* $Id: environment.c,v 1.7 2004/09/30 20:23:00 ekohl Exp $
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
;
46 RtlInitUnicodeString(&SrcValue
,
49 Length
= 2 * MAX_PATH
* sizeof(WCHAR
);
52 DstValue
.MaximumLength
= Length
;
53 DstValue
.Buffer
= LocalAlloc(LPTR
,
55 if (DstValue
.Buffer
== NULL
)
57 DPRINT1("LocalAlloc() failed\n");
61 Status
= RtlExpandEnvironmentStrings_U((PWSTR
)*Environment
,
65 if (!NT_SUCCESS(Status
))
67 DPRINT1("RtlExpandEnvironmentStrings_U() failed (Status %lx)\n", Status
);
68 DPRINT1("Length %lu\n", Length
);
74 RtlInitUnicodeString(&DstValue
,
78 if (!_wcsicmp (lpName
, L
"temp") || !_wcsicmp (lpName
, L
"tmp"))
80 if (!GetShortPathNameW(DstValue
.Buffer
, ShortName
, MAX_PATH
))
82 DPRINT1("GetShortPathNameW() failed (Error %lu)\n", GetLastError());
86 DPRINT("Buffer: %S\n", ShortName
);
87 RtlInitUnicodeString(&DstValue
,
91 RtlInitUnicodeString(&Name
,
94 DPRINT("Value: %wZ\n", &DstValue
);
96 Status
= RtlSetEnvironmentVariable((PWSTR
*)Environment
,
102 LocalFree(DstValue
.Buffer
);
105 if (!NT_SUCCESS(Status
))
107 DPRINT1("RtlSetEnvironmentVariable() failed (Status %lx)\n", Status
);
116 AppendUserEnvironmentVariable (LPVOID
*Environment
,
121 UNICODE_STRING Value
;
124 RtlInitUnicodeString (&Name
,
128 Value
.MaximumLength
= 1024 * sizeof(WCHAR
);
129 Value
.Buffer
= LocalAlloc (LPTR
,
130 1024 * sizeof(WCHAR
));
131 if (Value
.Buffer
== NULL
)
135 Value
.Buffer
[0] = UNICODE_NULL
;
137 Status
= RtlQueryEnvironmentVariable_U ((PWSTR
)*Environment
,
140 if (NT_SUCCESS(Status
))
142 RtlAppendUnicodeToString (&Value
,
146 RtlAppendUnicodeToString (&Value
,
149 Status
= RtlSetEnvironmentVariable ((PWSTR
*)Environment
,
152 LocalFree (Value
.Buffer
);
153 if (!NT_SUCCESS(Status
))
155 DPRINT1 ("RtlSetEnvironmentVariable() failed (Status %lx)\n", Status
);
164 GetCurrentUserKey (HANDLE hToken
)
166 UNICODE_STRING SidString
;
169 if (!GetUserSidFromToken (hToken
,
172 DPRINT1 ("GetUserSidFromToken() failed\n");
176 if (RegOpenKeyExW (HKEY_USERS
,
182 DPRINT1 ("RegOpenKeyExW() failed (Error %ld)\n", GetLastError());
183 RtlFreeUnicodeString (&SidString
);
187 RtlFreeUnicodeString (&SidString
);
194 SetUserEnvironment (LPVOID
*lpEnvironment
,
200 DWORD dwMaxValueNameLength
;
201 DWORD dwMaxValueDataLength
;
202 DWORD dwValueNameLength
;
203 DWORD dwValueDataLength
;
209 if (RegOpenKeyExW (hKey
,
215 DPRINT1 ("RegOpenKeyExW() failed (Error %ld)\n", GetLastError());
219 if (RegQueryInfoKey (hEnvKey
,
227 &dwMaxValueNameLength
,
228 &dwMaxValueDataLength
,
232 DPRINT1 ("RegQueryInforKey() failed (Error %ld)\n", GetLastError());
233 RegCloseKey (hEnvKey
);
239 RegCloseKey (hEnvKey
);
243 /* Allocate buffers */
244 lpValueName
= LocalAlloc (LPTR
,
245 dwMaxValueNameLength
* sizeof(WCHAR
));
246 if (lpValueName
== NULL
)
248 RegCloseKey (hEnvKey
);
252 lpValueData
= LocalAlloc (LPTR
,
253 dwMaxValueDataLength
);
254 if (lpValueData
== NULL
)
256 LocalFree (lpValueName
);
257 RegCloseKey (hEnvKey
);
261 /* Enumerate values */
262 for (i
= 0; i
< dwValues
; i
++)
264 dwValueNameLength
= dwMaxValueNameLength
;
265 dwValueDataLength
= dwMaxValueDataLength
;
266 RegEnumValueW (hEnvKey
,
275 if (!_wcsicmp (lpValueName
, L
"path"))
277 /* Append 'Path' environment variable */
278 AppendUserEnvironmentVariable (lpEnvironment
,
284 /* Set environment variable */
285 SetUserEnvironmentVariable (lpEnvironment
,
288 (dwType
== REG_EXPAND_SZ
));
292 LocalFree (lpValueData
);
293 LocalFree (lpValueName
);
294 RegCloseKey (hEnvKey
);
301 CreateEnvironmentBlock (LPVOID
*lpEnvironment
,
305 WCHAR Buffer
[MAX_PATH
];
310 DPRINT1 ("CreateEnvironmentBlock() called\n");
312 if (lpEnvironment
== NULL
)
315 Status
= RtlCreateEnvironment ((BOOLEAN
)bInherit
,
316 (PWSTR
*)lpEnvironment
);
317 if (!NT_SUCCESS (Status
))
319 DPRINT1 ("RtlCreateEnvironment() failed (Status %lx)\n", Status
);
323 /* Set 'COMPUTERNAME' variable */
325 if (GetComputerNameW (Buffer
,
328 SetUserEnvironmentVariable(lpEnvironment
,
337 hKeyUser
= GetCurrentUserKey (hToken
);
338 if (hKeyUser
== NULL
)
340 DPRINT1 ("GetCurrentUserKey() failed\n");
341 RtlDestroyEnvironment (*lpEnvironment
);
345 /* Set 'ALLUSERSPROFILE' variable */
347 if (GetAllUsersProfileDirectoryW (Buffer
,
350 SetUserEnvironmentVariable(lpEnvironment
,
356 /* Set 'USERPROFILE' variable */
358 if (GetUserProfileDirectoryW (hToken
,
362 SetUserEnvironmentVariable(lpEnvironment
,
368 /* FIXME: Set 'USERDOMAIN' variable */
370 /* FIXME: Set 'USERNAME' variable */
372 /* Set user environment variables */
373 SetUserEnvironment (lpEnvironment
,
377 RegCloseKey (hKeyUser
);
384 DestroyEnvironmentBlock (LPVOID lpEnvironment
)
386 DPRINT ("DestroyEnvironmentBlock() called\n");
388 if (lpEnvironment
== NULL
)
391 RtlDestroyEnvironment (lpEnvironment
);