1 /* $Id: environment.c,v 1.6 2004/08/15 19:02:40 chorns 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
14 SetUserEnvironmentVariable (LPVOID
*Environment
,
19 WCHAR ShortName
[MAX_PATH
];
21 UNICODE_STRING SrcValue
;
22 UNICODE_STRING DstValue
;
28 RtlInitUnicodeString(&SrcValue
,
31 Length
= 2 * MAX_PATH
* sizeof(WCHAR
);
34 DstValue
.MaximumLength
= Length
;
35 DstValue
.Buffer
= LocalAlloc(LPTR
,
37 if (DstValue
.Buffer
== NULL
)
39 DPRINT1("LocalAlloc() failed\n");
43 Status
= RtlExpandEnvironmentStrings_U((PWSTR
)*Environment
,
47 if (!NT_SUCCESS(Status
))
49 DPRINT1("RtlExpandEnvironmentStrings_U() failed (Status %lx)\n", Status
);
50 DPRINT1("Length %lu\n", Length
);
56 RtlInitUnicodeString(&DstValue
,
60 if (!_wcsicmp (lpName
, L
"temp") || !_wcsicmp (lpName
, L
"tmp"))
62 if (!GetShortPathNameW(DstValue
.Buffer
, ShortName
, MAX_PATH
))
64 DPRINT1("GetShortPathNameW() failed (Error %lu)\n", GetLastError());
68 DPRINT("Buffer: %S\n", ShortName
);
69 RtlInitUnicodeString(&DstValue
,
73 RtlInitUnicodeString(&Name
,
76 DPRINT("Value: %wZ\n", &DstValue
);
78 Status
= RtlSetEnvironmentVariable((PWSTR
*)Environment
,
84 LocalFree(DstValue
.Buffer
);
87 if (!NT_SUCCESS(Status
))
89 DPRINT1("RtlSetEnvironmentVariable() failed (Status %lx)\n", Status
);
98 AppendUserEnvironmentVariable (LPVOID
*Environment
,
103 UNICODE_STRING Value
;
106 RtlInitUnicodeString (&Name
,
110 Value
.MaximumLength
= 1024 * sizeof(WCHAR
);
111 Value
.Buffer
= LocalAlloc (LPTR
,
112 1024 * sizeof(WCHAR
));
113 if (Value
.Buffer
== NULL
)
117 Value
.Buffer
[0] = UNICODE_NULL
;
119 Status
= RtlQueryEnvironmentVariable_U ((PWSTR
)*Environment
,
122 if (NT_SUCCESS(Status
))
124 RtlAppendUnicodeToString (&Value
,
128 RtlAppendUnicodeToString (&Value
,
131 Status
= RtlSetEnvironmentVariable ((PWSTR
*)Environment
,
134 LocalFree (Value
.Buffer
);
135 if (!NT_SUCCESS(Status
))
137 DPRINT1 ("RtlSetEnvironmentVariable() failed (Status %lx)\n", Status
);
146 GetCurrentUserKey (HANDLE hToken
)
148 UNICODE_STRING SidString
;
151 if (!GetUserSidFromToken (hToken
,
154 DPRINT1 ("GetUserSidFromToken() failed\n");
158 if (RegOpenKeyExW (HKEY_USERS
,
164 DPRINT1 ("RegOpenKeyExW() failed (Error %ld)\n", GetLastError());
165 RtlFreeUnicodeString (&SidString
);
169 RtlFreeUnicodeString (&SidString
);
176 SetUserEnvironment (LPVOID
*lpEnvironment
,
182 DWORD dwMaxValueNameLength
;
183 DWORD dwMaxValueDataLength
;
184 DWORD dwValueNameLength
;
185 DWORD dwValueDataLength
;
191 if (RegOpenKeyExW (hKey
,
197 DPRINT1 ("RegOpenKeyExW() failed (Error %ld)\n", GetLastError());
201 if (RegQueryInfoKey (hEnvKey
,
209 &dwMaxValueNameLength
,
210 &dwMaxValueDataLength
,
214 DPRINT1 ("RegQueryInforKey() failed (Error %ld)\n", GetLastError());
215 RegCloseKey (hEnvKey
);
221 RegCloseKey (hEnvKey
);
225 /* Allocate buffers */
226 lpValueName
= LocalAlloc (LPTR
,
227 dwMaxValueNameLength
* sizeof(WCHAR
));
228 if (lpValueName
== NULL
)
230 RegCloseKey (hEnvKey
);
234 lpValueData
= LocalAlloc (LPTR
,
235 dwMaxValueDataLength
);
236 if (lpValueData
== NULL
)
238 LocalFree (lpValueName
);
239 RegCloseKey (hEnvKey
);
243 /* Enumerate values */
244 for (i
= 0; i
< dwValues
; i
++)
246 dwValueNameLength
= dwMaxValueNameLength
;
247 dwValueDataLength
= dwMaxValueDataLength
;
248 RegEnumValueW (hEnvKey
,
257 if (!_wcsicmp (lpValueName
, L
"path"))
259 /* Append 'Path' environment variable */
260 AppendUserEnvironmentVariable (lpEnvironment
,
266 /* Set environment variable */
267 SetUserEnvironmentVariable (lpEnvironment
,
270 (dwType
== REG_EXPAND_SZ
));
274 LocalFree (lpValueData
);
275 LocalFree (lpValueName
);
276 RegCloseKey (hEnvKey
);
283 CreateEnvironmentBlock (LPVOID
*lpEnvironment
,
287 WCHAR Buffer
[MAX_PATH
];
292 DPRINT1 ("CreateEnvironmentBlock() called\n");
294 if (lpEnvironment
== NULL
)
297 Status
= RtlCreateEnvironment ((BOOLEAN
)bInherit
,
298 (PWSTR
*)lpEnvironment
);
299 if (!NT_SUCCESS (Status
))
301 DPRINT1 ("RtlCreateEnvironment() failed (Status %lx)\n", Status
);
305 /* Set 'COMPUTERNAME' variable */
307 if (GetComputerNameW (Buffer
,
310 SetUserEnvironmentVariable(lpEnvironment
,
319 hKeyUser
= GetCurrentUserKey (hToken
);
320 if (hKeyUser
== NULL
)
322 DPRINT1 ("GetCurrentUserKey() failed\n");
323 RtlDestroyEnvironment (*lpEnvironment
);
327 /* Set 'ALLUSERSPROFILE' variable */
329 if (GetAllUsersProfileDirectoryW (Buffer
,
332 SetUserEnvironmentVariable(lpEnvironment
,
338 /* Set 'USERPROFILE' variable */
340 if (GetUserProfileDirectoryW (hToken
,
344 SetUserEnvironmentVariable(lpEnvironment
,
350 /* FIXME: Set 'USERDOMAIN' variable */
352 /* FIXME: Set 'USERNAME' variable */
354 /* Set user environment variables */
355 SetUserEnvironment (lpEnvironment
,
359 RegCloseKey (hKeyUser
);
366 DestroyEnvironmentBlock (LPVOID lpEnvironment
)
368 DPRINT ("DestroyEnvironmentBlock() called\n");
370 if (lpEnvironment
== NULL
)
373 RtlDestroyEnvironment (lpEnvironment
);