1 /* $Id: environment.c,v 1.5 2004/06/29 12:06:01 ekohl Exp $
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS system libraries
5 * FILE: lib/userenv/environment.c
6 * PURPOSE: User environment functions
7 * PROGRAMMER: Eric Kohl
10 #define NTOS_MODE_USER
19 SetUserEnvironmentVariable (LPVOID
*Environment
,
24 WCHAR ShortName
[MAX_PATH
];
26 UNICODE_STRING SrcValue
;
27 UNICODE_STRING DstValue
;
33 RtlInitUnicodeString(&SrcValue
,
36 Length
= 2 * MAX_PATH
* sizeof(WCHAR
);
39 DstValue
.MaximumLength
= Length
;
40 DstValue
.Buffer
= LocalAlloc(LPTR
,
42 if (DstValue
.Buffer
== NULL
)
44 DPRINT1("LocalAlloc() failed\n");
48 Status
= RtlExpandEnvironmentStrings_U((PWSTR
)*Environment
,
52 if (!NT_SUCCESS(Status
))
54 DPRINT1("RtlExpandEnvironmentStrings_U() failed (Status %lx)\n", Status
);
55 DPRINT1("Length %lu\n", Length
);
61 RtlInitUnicodeString(&DstValue
,
65 if (!_wcsicmp (lpName
, L
"temp") || !_wcsicmp (lpName
, L
"tmp"))
67 if (!GetShortPathNameW(DstValue
.Buffer
, ShortName
, MAX_PATH
))
69 DPRINT1("GetShortPathNameW() failed (Error %lu)\n", GetLastError());
73 DPRINT("Buffer: %S\n", ShortName
);
74 RtlInitUnicodeString(&DstValue
,
78 RtlInitUnicodeString(&Name
,
81 DPRINT("Value: %wZ\n", &DstValue
);
83 Status
= RtlSetEnvironmentVariable((PWSTR
*)Environment
,
89 LocalFree(DstValue
.Buffer
);
92 if (!NT_SUCCESS(Status
))
94 DPRINT1("RtlSetEnvironmentVariable() failed (Status %lx)\n", Status
);
103 AppendUserEnvironmentVariable (LPVOID
*Environment
,
108 UNICODE_STRING Value
;
111 RtlInitUnicodeString (&Name
,
115 Value
.MaximumLength
= 1024 * sizeof(WCHAR
);
116 Value
.Buffer
= LocalAlloc (LPTR
,
117 1024 * sizeof(WCHAR
));
118 if (Value
.Buffer
== NULL
)
122 Value
.Buffer
[0] = UNICODE_NULL
;
124 Status
= RtlQueryEnvironmentVariable_U ((PWSTR
)*Environment
,
127 if (NT_SUCCESS(Status
))
129 RtlAppendUnicodeToString (&Value
,
133 RtlAppendUnicodeToString (&Value
,
136 Status
= RtlSetEnvironmentVariable ((PWSTR
*)Environment
,
139 LocalFree (Value
.Buffer
);
140 if (!NT_SUCCESS(Status
))
142 DPRINT1 ("RtlSetEnvironmentVariable() failed (Status %lx)\n", Status
);
151 GetCurrentUserKey (HANDLE hToken
)
153 UNICODE_STRING SidString
;
156 if (!GetUserSidFromToken (hToken
,
159 DPRINT1 ("GetUserSidFromToken() failed\n");
163 if (RegOpenKeyExW (HKEY_USERS
,
169 DPRINT1 ("RegOpenKeyExW() failed (Error %ld)\n", GetLastError());
170 RtlFreeUnicodeString (&SidString
);
174 RtlFreeUnicodeString (&SidString
);
181 SetUserEnvironment (LPVOID
*lpEnvironment
,
187 DWORD dwMaxValueNameLength
;
188 DWORD dwMaxValueDataLength
;
189 DWORD dwValueNameLength
;
190 DWORD dwValueDataLength
;
196 if (RegOpenKeyExW (hKey
,
202 DPRINT1 ("RegOpenKeyExW() failed (Error %ld)\n", GetLastError());
206 if (RegQueryInfoKey (hEnvKey
,
214 &dwMaxValueNameLength
,
215 &dwMaxValueDataLength
,
219 DPRINT1 ("RegQueryInforKey() failed (Error %ld)\n", GetLastError());
220 RegCloseKey (hEnvKey
);
226 RegCloseKey (hEnvKey
);
230 /* Allocate buffers */
231 lpValueName
= LocalAlloc (LPTR
,
232 dwMaxValueNameLength
* sizeof(WCHAR
));
233 if (lpValueName
== NULL
)
235 RegCloseKey (hEnvKey
);
239 lpValueData
= LocalAlloc (LPTR
,
240 dwMaxValueDataLength
);
241 if (lpValueData
== NULL
)
243 LocalFree (lpValueName
);
244 RegCloseKey (hEnvKey
);
248 /* Enumerate values */
249 for (i
= 0; i
< dwValues
; i
++)
251 dwValueNameLength
= dwMaxValueNameLength
;
252 dwValueDataLength
= dwMaxValueDataLength
;
253 RegEnumValueW (hEnvKey
,
262 if (!_wcsicmp (lpValueName
, L
"path"))
264 /* Append 'Path' environment variable */
265 AppendUserEnvironmentVariable (lpEnvironment
,
271 /* Set environment variable */
272 SetUserEnvironmentVariable (lpEnvironment
,
275 (dwType
== REG_EXPAND_SZ
));
279 LocalFree (lpValueData
);
280 LocalFree (lpValueName
);
281 RegCloseKey (hEnvKey
);
288 CreateEnvironmentBlock (LPVOID
*lpEnvironment
,
292 WCHAR Buffer
[MAX_PATH
];
297 DPRINT1 ("CreateEnvironmentBlock() called\n");
299 if (lpEnvironment
== NULL
)
302 Status
= RtlCreateEnvironment ((BOOLEAN
)bInherit
,
303 (PWSTR
*)lpEnvironment
);
304 if (!NT_SUCCESS (Status
))
306 DPRINT1 ("RtlCreateEnvironment() failed (Status %lx)\n", Status
);
310 /* Set 'COMPUTERNAME' variable */
312 if (GetComputerNameW (Buffer
,
315 SetUserEnvironmentVariable(lpEnvironment
,
324 hKeyUser
= GetCurrentUserKey (hToken
);
325 if (hKeyUser
== NULL
)
327 DPRINT1 ("GetCurrentUserKey() failed\n");
328 RtlDestroyEnvironment (*lpEnvironment
);
332 /* Set 'ALLUSERSPROFILE' variable */
334 if (GetAllUsersProfileDirectoryW (Buffer
,
337 SetUserEnvironmentVariable(lpEnvironment
,
343 /* Set 'USERPROFILE' variable */
345 if (GetUserProfileDirectoryW (hToken
,
349 SetUserEnvironmentVariable(lpEnvironment
,
355 /* FIXME: Set 'USERDOMAIN' variable */
357 /* FIXME: Set 'USERNAME' variable */
359 /* Set user environment variables */
360 SetUserEnvironment (lpEnvironment
,
364 RegCloseKey (hKeyUser
);
371 DestroyEnvironmentBlock (LPVOID lpEnvironment
)
373 DPRINT ("DestroyEnvironmentBlock() called\n");
375 if (lpEnvironment
== NULL
)
378 RtlDestroyEnvironment (lpEnvironment
);