ReactOS  0.4.14-dev-323-g6fe6a88
environment.c File Reference
#include "precomp.h"
#include <debug.h>
Include dependency graph for environment.c:

Go to the source code of this file.

Macros

#define NDEBUG
 

Functions

static BOOL SetUserEnvironmentVariable (PWSTR *Environment, LPWSTR lpName, LPWSTR lpValue, BOOL bExpand)
 
static BOOL AppendUserEnvironmentVariable (PWSTR *Environment, LPWSTR lpName, LPWSTR lpValue)
 
static HKEY GetCurrentUserKey (HANDLE hToken)
 
static BOOL GetUserAndDomainName (IN HANDLE hToken, OUT LPWSTR *UserName, OUT LPWSTR *DomainName)
 
static BOOL SetUserEnvironment (PWSTR *Environment, HKEY hKey, LPWSTR lpSubKeyName)
 
static BOOL SetSystemEnvironment (PWSTR *Environment)
 
BOOL WINAPI CreateEnvironmentBlock (OUT LPVOID *lpEnvironment, IN HANDLE hToken, IN BOOL bInherit)
 
BOOL WINAPI DestroyEnvironmentBlock (IN LPVOID lpEnvironment)
 
BOOL WINAPI ExpandEnvironmentStringsForUserW (IN HANDLE hToken, IN LPCWSTR lpSrc, OUT LPWSTR lpDest, IN DWORD dwSize)
 
BOOL WINAPI ExpandEnvironmentStringsForUserA (IN HANDLE hToken, IN LPCSTR lpSrc, OUT LPSTR lpDest, IN DWORD dwSize)
 

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Definition at line 29 of file environment.c.

Function Documentation

◆ AppendUserEnvironmentVariable()

static BOOL AppendUserEnvironmentVariable ( PWSTR Environment,
LPWSTR  lpName,
LPWSTR  lpValue 
)
static

Definition at line 118 of file environment.c.

121 {
124 
126 
127  Value.Length = 0;
128  Value.MaximumLength = 1024 * sizeof(WCHAR);
129  Value.Buffer = LocalAlloc(LPTR, Value.MaximumLength);
130  if (Value.Buffer == NULL)
131  return FALSE;
132 
133  Value.Buffer[0] = UNICODE_NULL;
134 
136  &Name,
137  &Value);
138  if (NT_SUCCESS(Status))
140 
141  RtlAppendUnicodeToString(&Value, lpValue);
142 
144  &Name,
145  &Value);
146  LocalFree(Value.Buffer);
147  if (!NT_SUCCESS(Status))
148  {
149  DPRINT1("RtlSetEnvironmentVariable() failed (Status %lx)\n", Status);
150  return FALSE;
151  }
152 
153  return TRUE;
154 }
_In_opt_ ULONG _Out_ PULONG Value
Definition: rtlfuncs.h:2343
PVOID PVOID PWCHAR PVOID Environment
Definition: env.c:45
#define TRUE
Definition: types.h:120
LONG NTSTATUS
Definition: precomp.h:26
_In_ LPCSTR lpName
Definition: winbase.h:2736
#define UNICODE_NULL
struct NameRec_ * Name
Definition: cdprocs.h:464
NTSYSAPI NTSTATUS NTAPI RtlQueryEnvironmentVariable_U(_In_opt_ PWSTR Environment, _In_ PCUNICODE_STRING Name, _Out_ PUNICODE_STRING Value)
smooth NULL
Definition: ftsmooth.c:416
NTSYSAPI NTSTATUS NTAPI RtlSetEnvironmentVariable(_In_z_ PWSTR *Environment, _In_ PUNICODE_STRING Name, _In_ PUNICODE_STRING Value)
NTSTATUS RtlAppendUnicodeToString(IN PUNICODE_STRING Str1, IN PWSTR Str2)
Definition: string_lib.cpp:62
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
static const WCHAR L[]
Definition: oid.c:1250
Status
Definition: gdiplustypes.h:24
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1577
#define DPRINT1
Definition: precomp.h:8
#define LPTR
Definition: winbase.h:362
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
HLOCAL NTAPI LocalAlloc(UINT uFlags, SIZE_T dwBytes)
Definition: heapmem.c:1373

Referenced by SetUserEnvironment().

◆ CreateEnvironmentBlock()

BOOL WINAPI CreateEnvironmentBlock ( OUT LPVOID lpEnvironment,
IN HANDLE  hToken,
IN BOOL  bInherit 
)

Definition at line 505 of file environment.c.

508 {
510  LONG lError;
511  PWSTR* Environment = (PWSTR*)lpEnvironment;
512  DWORD Length;
513  DWORD dwType;
514  HKEY hKey;
515  HKEY hKeyUser;
516  LPWSTR lpUserName = NULL;
517  LPWSTR lpDomainName = NULL;
519  WCHAR szValue[1024];
520 
521  DPRINT("CreateEnvironmentBlock() called\n");
522 
523  if (lpEnvironment == NULL)
524  {
526  return FALSE;
527  }
528 
530  if (!NT_SUCCESS(Status))
531  {
532  DPRINT1("RtlCreateEnvironment() failed (Status %lx)\n", Status);
534  return FALSE;
535  }
536 
537  /* Set 'SystemRoot' variable */
539  if (GetEnvironmentVariableW(L"SystemRoot", Buffer, Length))
540  {
542  L"SystemRoot",
543  Buffer,
544  FALSE);
545  }
546 
547  /* Set 'SystemDrive' variable */
548  if (GetEnvironmentVariableW(L"SystemDrive", Buffer, Length))
549  {
551  L"SystemDrive",
552  Buffer,
553  FALSE);
554  }
555 
556  /* Set variables from Session Manager */
558  {
560  return FALSE;
561  }
562 
563  /* Set 'COMPUTERNAME' variable */
566  {
568  L"COMPUTERNAME",
569  Buffer,
570  FALSE);
571  }
572 
573  /* Set 'ALLUSERSPROFILE' variable */
576  {
578  L"ALLUSERSPROFILE",
579  Buffer,
580  FALSE);
581  }
582 
583  /* Set 'USERPROFILE' variable to the default users profile */
586  {
588  L"USERPROFILE",
589  Buffer,
590  TRUE);
591  }
592 
594  L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion",
595  0,
596  KEY_READ,
597  &hKey);
598  if (lError == ERROR_SUCCESS)
599  {
600  Length = sizeof(szValue);
601  lError = RegQueryValueExW(hKey,
602  L"ProgramFilesDir",
603  NULL,
604  &dwType,
605  (LPBYTE)szValue,
606  &Length);
607  if (lError == ERROR_SUCCESS)
608  {
610  L"ProgramFiles",
611  szValue,
612  FALSE);
613  }
614 
615  Length = sizeof(szValue);
616  lError = RegQueryValueExW(hKey,
617  L"CommonFilesDir",
618  NULL,
619  &dwType,
620  (LPBYTE)szValue,
621  &Length);
622  if (lError == ERROR_SUCCESS)
623  {
625  L"CommonProgramFiles",
626  szValue,
627  FALSE);
628  }
629 
630  RegCloseKey(hKey);
631  }
632 
633  /*
634  * If no user token is specified, the system environment variables are set
635  * and we stop here, otherwise continue setting the user-specific variables.
636  */
637  if (hToken == NULL)
638  return TRUE;
639 
640  hKeyUser = GetCurrentUserKey(hToken);
641  if (hKeyUser == NULL)
642  {
643  DPRINT1("GetCurrentUserKey() failed\n");
645  return FALSE;
646  }
647 
648  /* Set 'USERPROFILE' variable */
650  if (GetUserProfileDirectoryW(hToken, Buffer, &Length))
651  {
652  DWORD MinLen = 2;
653 
655  L"USERPROFILE",
656  Buffer,
657  FALSE);
658 
659  // FIXME: Strangely enough the following two environment variables
660  // are not set by userenv.dll in Windows... See r68284 / CORE-9875
661  // FIXME2: This is done by msgina.dll !!
662 
663  /* At least <drive letter>:<path> */
664  if (Length > MinLen)
665  {
666  /* Set 'HOMEDRIVE' variable */
667  StringCchCopyNW(szValue, ARRAYSIZE(Buffer), Buffer, MinLen);
669  L"HOMEDRIVE",
670  szValue,
671  FALSE);
672 
673  /* Set 'HOMEPATH' variable */
674  StringCchCopyNW(szValue, ARRAYSIZE(Buffer), Buffer + MinLen, Length - MinLen);
676  L"HOMEPATH",
677  szValue,
678  FALSE);
679  }
680  }
681  else
682  {
683  DPRINT1("GetUserProfileDirectoryW failed with error %lu\n", GetLastError());
684  }
685 
686  if (GetUserAndDomainName(hToken,
687  &lpUserName,
688  &lpDomainName))
689  {
690  /* Set 'USERNAME' variable */
692  L"USERNAME",
693  lpUserName,
694  FALSE);
695 
696  /* Set 'USERDOMAIN' variable */
698  L"USERDOMAIN",
699  lpDomainName,
700  FALSE);
701 
702  if (lpUserName != NULL)
703  LocalFree(lpUserName);
704 
705  if (lpDomainName != NULL)
706  LocalFree(lpDomainName);
707  }
708 
709  /* Set user environment variables */
711  hKeyUser,
712  L"Environment");
713 
714  /* Set user volatile environment variables */
716  hKeyUser,
717  L"Volatile Environment");
718 
719  RegCloseKey(hKeyUser);
720 
721  return TRUE;
722 }
#define ERROR_INVALID_PARAMETER
Definition: compat.h:91
BOOL WINAPI GetDefaultUserProfileDirectoryW(_Out_opt_ LPWSTR lpProfileDir, _Inout_ LPDWORD lpcchSize)
Definition: profile.c:1360
PVOID PVOID PWCHAR PVOID Environment
Definition: env.c:45
STRSAFEAPI StringCchCopyNW(STRSAFE_LPWSTR pszDest, size_t cchDest, STRSAFE_LPCWSTR pszSrc, size_t cchToCopy)
Definition: strsafe.h:236
#define TRUE
Definition: types.h:120
#define ERROR_SUCCESS
Definition: deptool.c:10
BOOL WINAPI GetUserProfileDirectoryW(_In_ HANDLE hToken, _Out_opt_ LPWSTR lpProfileDir, _Inout_ LPDWORD lpcchSize)
Definition: profile.c:1709
static HKEY GetCurrentUserKey(HANDLE hToken)
Definition: environment.c:159
#define KEY_READ
Definition: nt_native.h:1023
NTSYSAPI VOID NTAPI RtlDestroyEnvironment(_In_ PWSTR Environment)
uint16_t * PWSTR
Definition: typedefs.h:54
LONG NTSTATUS
Definition: precomp.h:26
#define ARRAYSIZE(array)
Definition: filtermapper.c:47
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
static BOOL GetUserAndDomainName(IN HANDLE hToken, OUT LPWSTR *UserName, OUT LPWSTR *DomainName)
Definition: environment.c:192
BOOL WINAPI GetComputerNameW(LPWSTR lpBuffer, LPDWORD lpnSize)
Definition: compname.c:440
LONG WINAPI RegCloseKey(HKEY hKey)
Definition: reg.c:423
unsigned char * LPBYTE
Definition: typedefs.h:52
long LONG
Definition: pedump.c:60
unsigned char BOOLEAN
smooth NULL
Definition: ftsmooth.c:416
void DPRINT(...)
Definition: polytest.cpp:61
Definition: bufpool.h:45
LONG WINAPI RegQueryValueExW(_In_ HKEY hkeyorg, _In_ LPCWSTR name, _In_ LPDWORD reserved, _In_ LPDWORD type, _In_ LPBYTE data, _In_ LPDWORD count)
Definition: reg.c:4116
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define MAX_PATH
Definition: compat.h:26
unsigned long DWORD
Definition: ntddk_ex.h:95
#define SetLastError(x)
Definition: compat.h:409
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
static const WCHAR L[]
Definition: oid.c:1250
Status
Definition: gdiplustypes.h:24
static BOOL SetUserEnvironment(PWSTR *Environment, HKEY hKey, LPWSTR lpSubKeyName)
Definition: environment.c:273
NTSYSAPI NTSTATUS NTAPI RtlCreateEnvironment(_In_ BOOLEAN Inherit, _Out_ PWSTR *Environment)
NTSYSAPI ULONG WINAPI RtlNtStatusToDosError(NTSTATUS)
BOOL WINAPI GetAllUsersProfileDirectoryW(_Out_opt_ LPWSTR lpProfileDir, _Inout_ LPDWORD lpcchSize)
Definition: profile.c:1227
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1577
static BOOL SetUserEnvironmentVariable(PWSTR *Environment, LPWSTR lpName, LPWSTR lpValue, BOOL bExpand)
Definition: environment.c:34
#define DPRINT1
Definition: precomp.h:8
LONG WINAPI RegOpenKeyExW(HKEY hKey, LPCWSTR lpSubKey, DWORD ulOptions, REGSAM samDesired, PHKEY phkResult)
Definition: reg.c:3366
WCHAR * LPWSTR
Definition: xmlstorage.h:184
#define GetEnvironmentVariableW(x, y, z)
Definition: compat.h:412
static BOOL SetSystemEnvironment(PWSTR *Environment)
Definition: environment.c:396
#define HKEY_LOCAL_MACHINE
Definition: winreg.h:12

Referenced by ExpandEnvironmentStringsForUserW(), InstallDevice(), RegenerateUserEnvironment(), ScmStartUserModeService(), StartTaskManager(), StartUserShell(), Test_ApphelpCheckRunApp(), and test_create_env().

◆ DestroyEnvironmentBlock()

BOOL WINAPI DestroyEnvironmentBlock ( IN LPVOID  lpEnvironment)

Definition at line 727 of file environment.c.

728 {
729  DPRINT("DestroyEnvironmentBlock() called\n");
730 
731  if (lpEnvironment == NULL)
732  {
734  return FALSE;
735  }
736 
737  RtlDestroyEnvironment(lpEnvironment);
738 
739  return TRUE;
740 }
#define ERROR_INVALID_PARAMETER
Definition: compat.h:91
#define TRUE
Definition: types.h:120
NTSYSAPI VOID NTAPI RtlDestroyEnvironment(_In_ PWSTR Environment)
smooth NULL
Definition: ftsmooth.c:416
void DPRINT(...)
Definition: polytest.cpp:61
#define SetLastError(x)
Definition: compat.h:409

Referenced by ExpandEnvironmentStringsForUserW(), InstallDevice(), ScmStartUserModeService(), StartTaskManager(), StartUserShell(), Test_ApphelpCheckRunApp(), and test_create_env().

◆ ExpandEnvironmentStringsForUserA()

BOOL WINAPI ExpandEnvironmentStringsForUserA ( IN HANDLE  hToken,
IN LPCSTR  lpSrc,
OUT LPSTR  lpDest,
IN DWORD  dwSize 
)

Definition at line 796 of file environment.c.

800 {
801  BOOL Ret = FALSE;
802  DWORD dwSrcLen;
803  LPWSTR lpSrcW = NULL, lpDestW = NULL;
804 
805  if (lpSrc == NULL || lpDest == NULL || dwSize == 0)
806  {
808  return FALSE;
809  }
810 
811  dwSrcLen = strlen(lpSrc);
812  lpSrcW = (LPWSTR)GlobalAlloc(GMEM_FIXED,
813  (dwSrcLen + 1) * sizeof(WCHAR));
814  if (lpSrcW == NULL ||
816  0,
817  lpSrc,
818  -1,
819  lpSrcW,
820  dwSrcLen + 1) == 0)
821  {
822  goto Cleanup;
823  }
824 
825  lpDestW = (LPWSTR)GlobalAlloc(GMEM_FIXED,
826  dwSize * sizeof(WCHAR));
827  if (lpDestW == NULL)
828  goto Cleanup;
829 
831  lpSrcW,
832  lpDestW,
833  dwSize);
834  if (Ret)
835  {
837  0,
838  lpDestW,
839  -1,
840  lpDest,
841  dwSize,
842  NULL,
843  NULL) == 0)
844  {
845  Ret = FALSE;
846  }
847  }
848 
849 Cleanup:
850  if (lpSrcW != NULL)
851  GlobalFree((HGLOBAL)lpSrcW);
852 
853  if (lpDestW != NULL)
854  GlobalFree((HGLOBAL)lpDestW);
855 
856  return Ret;
857 }
#define ERROR_INVALID_PARAMETER
Definition: compat.h:91
HGLOBAL NTAPI GlobalAlloc(UINT uFlags, SIZE_T dwBytes)
Definition: heapmem.c:368
#define WideCharToMultiByte
Definition: compat.h:101
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
#define CP_ACP
Definition: compat.h:99
unsigned int BOOL
Definition: ntddk_ex.h:94
smooth NULL
Definition: ftsmooth.c:416
__wchar_t WCHAR
Definition: xmlstorage.h:180
unsigned long DWORD
Definition: ntddk_ex.h:95
HGLOBAL NTAPI GlobalFree(HGLOBAL hMem)
Definition: heapmem.c:611
#define SetLastError(x)
Definition: compat.h:409
#define GMEM_FIXED
Definition: winbase.h:290
static const WCHAR Cleanup[]
Definition: register.c:80
#define MultiByteToWideChar
Definition: compat.h:100
BOOL WINAPI ExpandEnvironmentStringsForUserW(IN HANDLE hToken, IN LPCWSTR lpSrc, OUT LPWSTR lpDest, IN DWORD dwSize)
Definition: environment.c:745
WCHAR * LPWSTR
Definition: xmlstorage.h:184
PSDBQUERYRESULT_VISTA PVOID DWORD * dwSize
Definition: env.c:54

◆ ExpandEnvironmentStringsForUserW()

BOOL WINAPI ExpandEnvironmentStringsForUserW ( IN HANDLE  hToken,
IN LPCWSTR  lpSrc,
OUT LPWSTR  lpDest,
IN DWORD  dwSize 
)

Definition at line 745 of file environment.c.

749 {
750  BOOL Ret = FALSE;
751  PVOID lpEnvironment;
752 
753  if (lpSrc == NULL || lpDest == NULL || dwSize == 0)
754  {
756  return FALSE;
757  }
758 
759  if (CreateEnvironmentBlock(&lpEnvironment,
760  hToken,
761  FALSE))
762  {
763  UNICODE_STRING SrcU, DestU;
765 
766  /* Initialize the strings */
767  RtlInitUnicodeString(&SrcU, lpSrc);
768  DestU.Length = 0;
769  DestU.MaximumLength = dwSize * sizeof(WCHAR);
770  DestU.Buffer = lpDest;
771 
772  /* Expand the strings */
773  Status = RtlExpandEnvironmentStrings_U((PWSTR)lpEnvironment,
774  &SrcU,
775  &DestU,
776  NULL);
777 
778  DestroyEnvironmentBlock(lpEnvironment);
779 
780  if (NT_SUCCESS(Status))
781  {
782  Ret = TRUE;
783  }
784  else
785  {
787  }
788  }
789 
790  return Ret;
791 }
#define ERROR_INVALID_PARAMETER
Definition: compat.h:91
#define TRUE
Definition: types.h:120
USHORT MaximumLength
Definition: env_spec_w32.h:370
uint16_t * PWSTR
Definition: typedefs.h:54
LONG NTSTATUS
Definition: precomp.h:26
BOOL WINAPI CreateEnvironmentBlock(OUT LPVOID *lpEnvironment, IN HANDLE hToken, IN BOOL bInherit)
Definition: environment.c:505
unsigned int BOOL
Definition: ntddk_ex.h:94
smooth NULL
Definition: ftsmooth.c:416
NTSYSAPI NTSTATUS WINAPI RtlExpandEnvironmentStrings_U(PCWSTR, const UNICODE_STRING *, UNICODE_STRING *, ULONG *)
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define SetLastError(x)
Definition: compat.h:409
BOOL WINAPI DestroyEnvironmentBlock(IN LPVOID lpEnvironment)
Definition: environment.c:727
Status
Definition: gdiplustypes.h:24
NTSYSAPI ULONG WINAPI RtlNtStatusToDosError(NTSTATUS)
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
PSDBQUERYRESULT_VISTA PVOID DWORD * dwSize
Definition: env.c:54

Referenced by ExpandEnvironmentStringsForUserA().

◆ GetCurrentUserKey()

static HKEY GetCurrentUserKey ( HANDLE  hToken)
static

Definition at line 159 of file environment.c.

160 {
161  LONG Error;
162  UNICODE_STRING SidString;
163  HKEY hKey;
164 
165  if (!GetUserSidStringFromToken(hToken, &SidString))
166  {
167  DPRINT1("GetUserSidFromToken() failed\n");
168  return NULL;
169  }
170 
172  SidString.Buffer,
173  0,
175  &hKey);
176  if (Error != ERROR_SUCCESS)
177  {
178  DPRINT1("RegOpenKeyExW() failed (Error %ld)\n", Error);
179  RtlFreeUnicodeString(&SidString);
181  return NULL;
182  }
183 
184  RtlFreeUnicodeString(&SidString);
185 
186  return hKey;
187 }
#define HKEY_USERS
Definition: winreg.h:13
#define MAXIMUM_ALLOWED
Definition: nt_native.h:83
#define ERROR_SUCCESS
Definition: deptool.c:10
BOOL GetUserSidStringFromToken(HANDLE hToken, PUNICODE_STRING SidString)
Definition: sid.c:102
long LONG
Definition: pedump.c:60
smooth NULL
Definition: ftsmooth.c:416
unsigned long DWORD
Definition: ntddk_ex.h:95
NTSYSAPI VOID NTAPI RtlFreeUnicodeString(PUNICODE_STRING UnicodeString)
BOOL Error
Definition: chkdsk.c:66
#define SetLastError(x)
Definition: compat.h:409
#define DPRINT1
Definition: precomp.h:8
LONG WINAPI RegOpenKeyExW(HKEY hKey, LPCWSTR lpSubKey, DWORD ulOptions, REGSAM samDesired, PHKEY phkResult)
Definition: reg.c:3366

Referenced by CreateEnvironmentBlock().

◆ GetUserAndDomainName()

static BOOL GetUserAndDomainName ( IN HANDLE  hToken,
OUT LPWSTR UserName,
OUT LPWSTR DomainName 
)
static

Definition at line 192 of file environment.c.

195 {
196  BOOL bRet = TRUE;
197  PSID Sid = NULL;
198  LPWSTR lpUserName = NULL;
199  LPWSTR lpDomainName = NULL;
200  DWORD cbUserName = 0;
201  DWORD cbDomainName = 0;
202  SID_NAME_USE SidNameUse;
203 
204  Sid = GetUserSid(hToken);
205  if (Sid == NULL)
206  {
207  DPRINT1("GetUserSid() failed\n");
208  return FALSE;
209  }
210 
211  if (!LookupAccountSidW(NULL,
212  Sid,
213  NULL,
214  &cbUserName,
215  NULL,
216  &cbDomainName,
217  &SidNameUse))
218  {
220  {
221  bRet = FALSE;
222  goto done;
223  }
224  }
225 
226  lpUserName = LocalAlloc(LPTR, cbUserName * sizeof(WCHAR));
227  if (lpUserName == NULL)
228  {
229  bRet = FALSE;
230  goto done;
231  }
232 
233  lpDomainName = LocalAlloc(LPTR, cbDomainName * sizeof(WCHAR));
234  if (lpDomainName == NULL)
235  {
236  bRet = FALSE;
237  goto done;
238  }
239 
240  if (!LookupAccountSidW(NULL,
241  Sid,
242  lpUserName,
243  &cbUserName,
244  lpDomainName,
245  &cbDomainName,
246  &SidNameUse))
247  {
248  bRet = FALSE;
249  goto done;
250  }
251 
252  *UserName = lpUserName;
253  *DomainName = lpDomainName;
254 
255 done:
256  if (bRet == FALSE)
257  {
258  if (lpUserName != NULL)
259  LocalFree(lpUserName);
260 
261  if (lpDomainName != NULL)
262  LocalFree(lpDomainName);
263  }
264 
265  LocalFree(Sid);
266 
267  return bRet;
268 }
#define TRUE
Definition: types.h:120
enum _SID_NAME_USE SID_NAME_USE
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
unsigned int BOOL
Definition: ntddk_ex.h:94
smooth NULL
Definition: ftsmooth.c:416
_In_ ULONG _In_ ACCESS_MASK _In_ PSID Sid
Definition: rtlfuncs.h:1104
BOOL WINAPI LookupAccountSidW(LPCWSTR pSystemName, PSID pSid, LPWSTR pAccountName, LPDWORD pdwAccountName, LPWSTR pDomainName, LPDWORD pdwDomainName, PSID_NAME_USE peUse)
Definition: misc.c:537
__wchar_t WCHAR
Definition: xmlstorage.h:180
unsigned long DWORD
Definition: ntddk_ex.h:95
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1577
#define DPRINT1
Definition: precomp.h:8
PSID WINAPI GetUserSid(HANDLE hToken)
Definition: sid.c:36
#define LPTR
Definition: winbase.h:362
HLOCAL NTAPI LocalAlloc(UINT uFlags, SIZE_T dwBytes)
Definition: heapmem.c:1373
WCHAR * LPWSTR
Definition: xmlstorage.h:184
#define ERROR_INSUFFICIENT_BUFFER
Definition: dderror.h:10

Referenced by CreateEnvironmentBlock().

◆ SetSystemEnvironment()

static BOOL SetSystemEnvironment ( PWSTR Environment)
static

Definition at line 396 of file environment.c.

397 {
398  LONG Error;
399  HKEY hEnvKey;
400  DWORD dwValues;
401  DWORD dwMaxValueNameLength;
402  DWORD dwMaxValueDataLength;
403  DWORD dwValueNameLength;
404  DWORD dwValueDataLength;
405  DWORD dwType;
406  DWORD i;
407  LPWSTR lpValueName;
408  LPWSTR lpValueData;
409 
411  L"SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment",
412  0,
414  &hEnvKey);
415  if (Error != ERROR_SUCCESS)
416  {
417  DPRINT1("RegOpenKeyExW() failed (Error %ld)\n", Error);
418  return FALSE;
419  }
420 
421  Error = RegQueryInfoKey(hEnvKey,
422  NULL,
423  NULL,
424  NULL,
425  NULL,
426  NULL,
427  NULL,
428  &dwValues,
429  &dwMaxValueNameLength,
430  &dwMaxValueDataLength,
431  NULL,
432  NULL);
433  if (Error != ERROR_SUCCESS)
434  {
435  DPRINT1("RegQueryInforKey() failed (Error %ld)\n", Error);
436  RegCloseKey(hEnvKey);
437  return FALSE;
438  }
439 
440  if (dwValues == 0)
441  {
442  RegCloseKey(hEnvKey);
443  return TRUE;
444  }
445 
446  /* Allocate buffers */
447  dwMaxValueNameLength++;
448  lpValueName = LocalAlloc(LPTR, dwMaxValueNameLength * sizeof(WCHAR));
449  if (lpValueName == NULL)
450  {
451  RegCloseKey(hEnvKey);
452  return FALSE;
453  }
454 
455  lpValueData = LocalAlloc(LPTR, dwMaxValueDataLength);
456  if (lpValueData == NULL)
457  {
458  LocalFree(lpValueName);
459  RegCloseKey(hEnvKey);
460  return FALSE;
461  }
462 
463  /* Enumerate values */
464  for (i = 0; i < dwValues; i++)
465  {
466  dwValueNameLength = dwMaxValueNameLength;
467  dwValueDataLength = dwMaxValueDataLength;
468 
469  Error = RegEnumValueW(hEnvKey,
470  i,
471  lpValueName,
472  &dwValueNameLength,
473  NULL,
474  &dwType,
475  (LPBYTE)lpValueData,
476  &dwValueDataLength);
477  if (Error == ERROR_SUCCESS)
478  {
479  /* Set environment variable */
481  lpValueName,
482  lpValueData,
483  (dwType == REG_EXPAND_SZ));
484  }
485  else
486  {
487  LocalFree(lpValueData);
488  LocalFree(lpValueName);
489  RegCloseKey(hEnvKey);
490 
491  return FALSE;
492  }
493  }
494 
495  LocalFree(lpValueData);
496  LocalFree(lpValueName);
497  RegCloseKey(hEnvKey);
498 
499  return TRUE;
500 }
PVOID PVOID PWCHAR PVOID Environment
Definition: env.c:45
#define TRUE
Definition: types.h:120
#define RegQueryInfoKey
Definition: winreg.h:521
#define ERROR_SUCCESS
Definition: deptool.c:10
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
LONG WINAPI RegCloseKey(HKEY hKey)
Definition: reg.c:423
unsigned char * LPBYTE
Definition: typedefs.h:52
long LONG
Definition: pedump.c:60
LONG WINAPI RegEnumValueW(_In_ HKEY hKey, _In_ DWORD index, _Out_ LPWSTR value, _Inout_ PDWORD val_count, _Reserved_ PDWORD reserved, _Out_opt_ PDWORD type, _Out_opt_ LPBYTE data, _Inout_opt_ PDWORD count)
Definition: reg.c:2853
smooth NULL
Definition: ftsmooth.c:416
__wchar_t WCHAR
Definition: xmlstorage.h:180
unsigned long DWORD
Definition: ntddk_ex.h:95
BOOL Error
Definition: chkdsk.c:66
static const WCHAR L[]
Definition: oid.c:1250
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1577
#define KEY_QUERY_VALUE
Definition: nt_native.h:1016
#define REG_EXPAND_SZ
Definition: nt_native.h:1494
static BOOL SetUserEnvironmentVariable(PWSTR *Environment, LPWSTR lpName, LPWSTR lpValue, BOOL bExpand)
Definition: environment.c:34
#define DPRINT1
Definition: precomp.h:8
#define LPTR
Definition: winbase.h:362
LONG WINAPI RegOpenKeyExW(HKEY hKey, LPCWSTR lpSubKey, DWORD ulOptions, REGSAM samDesired, PHKEY phkResult)
Definition: reg.c:3366
HLOCAL NTAPI LocalAlloc(UINT uFlags, SIZE_T dwBytes)
Definition: heapmem.c:1373
WCHAR * LPWSTR
Definition: xmlstorage.h:184
#define HKEY_LOCAL_MACHINE
Definition: winreg.h:12

Referenced by CreateEnvironmentBlock().

◆ SetUserEnvironment()

static BOOL SetUserEnvironment ( PWSTR Environment,
HKEY  hKey,
LPWSTR  lpSubKeyName 
)
static

Definition at line 273 of file environment.c.

276 {
277  LONG Error;
278  HKEY hEnvKey;
279  DWORD dwValues;
280  DWORD dwMaxValueNameLength;
281  DWORD dwMaxValueDataLength;
282  DWORD dwValueNameLength;
283  DWORD dwValueDataLength;
284  DWORD dwType;
285  DWORD i;
286  LPWSTR lpValueName;
287  LPWSTR lpValueData;
288 
289  Error = RegOpenKeyExW(hKey,
290  lpSubKeyName,
291  0,
293  &hEnvKey);
294  if (Error != ERROR_SUCCESS)
295  {
296  DPRINT1("RegOpenKeyExW() failed (Error %ld)\n", Error);
298  return FALSE;
299  }
300 
301  Error = RegQueryInfoKey(hEnvKey,
302  NULL,
303  NULL,
304  NULL,
305  NULL,
306  NULL,
307  NULL,
308  &dwValues,
309  &dwMaxValueNameLength,
310  &dwMaxValueDataLength,
311  NULL,
312  NULL);
313  if (Error != ERROR_SUCCESS)
314  {
315  DPRINT1("RegQueryInforKey() failed (Error %ld)\n", Error);
316  RegCloseKey(hEnvKey);
318  return FALSE;
319  }
320 
321  if (dwValues == 0)
322  {
323  RegCloseKey(hEnvKey);
324  return TRUE;
325  }
326 
327  /* Allocate buffers */
328  dwMaxValueNameLength++;
329  lpValueName = LocalAlloc(LPTR, dwMaxValueNameLength * sizeof(WCHAR));
330  if (lpValueName == NULL)
331  {
332  RegCloseKey(hEnvKey);
333  return FALSE;
334  }
335 
336  lpValueData = LocalAlloc(LPTR, dwMaxValueDataLength);
337  if (lpValueData == NULL)
338  {
339  LocalFree(lpValueName);
340  RegCloseKey(hEnvKey);
341  return FALSE;
342  }
343 
344  /* Enumerate values */
345  for (i = 0; i < dwValues; i++)
346  {
347  dwValueNameLength = dwMaxValueNameLength;
348  dwValueDataLength = dwMaxValueDataLength;
349 
350  Error = RegEnumValueW(hEnvKey,
351  i,
352  lpValueName,
353  &dwValueNameLength,
354  NULL,
355  &dwType,
356  (LPBYTE)lpValueData,
357  &dwValueDataLength);
358  if (Error == ERROR_SUCCESS)
359  {
360  if (!_wcsicmp(lpValueName, L"PATH"))
361  {
362  /* Append 'Path' environment variable */
364  lpValueName,
365  lpValueData);
366  }
367  else
368  {
369  /* Set environment variable */
371  lpValueName,
372  lpValueData,
373  (dwType == REG_EXPAND_SZ));
374  }
375  }
376  else
377  {
378  LocalFree(lpValueData);
379  LocalFree(lpValueName);
380  RegCloseKey(hEnvKey);
381 
382  return FALSE;
383  }
384  }
385 
386  LocalFree(lpValueData);
387  LocalFree(lpValueName);
388  RegCloseKey(hEnvKey);
389 
390  return TRUE;
391 }
PVOID PVOID PWCHAR PVOID Environment
Definition: env.c:45
#define TRUE
Definition: types.h:120
#define RegQueryInfoKey
Definition: winreg.h:521
#define ERROR_SUCCESS
Definition: deptool.c:10
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
LONG WINAPI RegCloseKey(HKEY hKey)
Definition: reg.c:423
unsigned char * LPBYTE
Definition: typedefs.h:52
long LONG
Definition: pedump.c:60
LONG WINAPI RegEnumValueW(_In_ HKEY hKey, _In_ DWORD index, _Out_ LPWSTR value, _Inout_ PDWORD val_count, _Reserved_ PDWORD reserved, _Out_opt_ PDWORD type, _Out_opt_ LPBYTE data, _Inout_opt_ PDWORD count)
Definition: reg.c:2853
smooth NULL
Definition: ftsmooth.c:416
static BOOL AppendUserEnvironmentVariable(PWSTR *Environment, LPWSTR lpName, LPWSTR lpValue)
Definition: environment.c:118
__wchar_t WCHAR
Definition: xmlstorage.h:180
unsigned long DWORD
Definition: ntddk_ex.h:95
BOOL Error
Definition: chkdsk.c:66
#define SetLastError(x)
Definition: compat.h:409
static const WCHAR L[]
Definition: oid.c:1250
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1577
#define KEY_QUERY_VALUE
Definition: nt_native.h:1016
#define REG_EXPAND_SZ
Definition: nt_native.h:1494
static BOOL SetUserEnvironmentVariable(PWSTR *Environment, LPWSTR lpName, LPWSTR lpValue, BOOL bExpand)
Definition: environment.c:34
#define DPRINT1
Definition: precomp.h:8
#define LPTR
Definition: winbase.h:362
LONG WINAPI RegOpenKeyExW(HKEY hKey, LPCWSTR lpSubKey, DWORD ulOptions, REGSAM samDesired, PHKEY phkResult)
Definition: reg.c:3366
HLOCAL NTAPI LocalAlloc(UINT uFlags, SIZE_T dwBytes)
Definition: heapmem.c:1373
WCHAR * LPWSTR
Definition: xmlstorage.h:184
_Check_return_ _CRTIMP int __cdecl _wcsicmp(_In_z_ const wchar_t *_Str1, _In_z_ const wchar_t *_Str2)

Referenced by CreateEnvironmentBlock().

◆ SetUserEnvironmentVariable()

static BOOL SetUserEnvironmentVariable ( PWSTR Environment,
LPWSTR  lpName,
LPWSTR  lpValue,
BOOL  bExpand 
)
static

Definition at line 34 of file environment.c.

38 {
41  UNICODE_STRING SrcValue, DstValue;
42  ULONG Length;
43  PVOID Buffer = NULL;
45 
46  if (bExpand)
47  {
48  RtlInitUnicodeString(&SrcValue, lpValue);
49 
50  Length = 2 * MAX_PATH * sizeof(WCHAR);
51 
52  DstValue.Length = 0;
53  DstValue.MaximumLength = Length;
54  DstValue.Buffer = Buffer = LocalAlloc(LPTR, Length);
55  if (DstValue.Buffer == NULL)
56  {
57  DPRINT1("LocalAlloc() failed\n");
58  return FALSE;
59  }
60 
62  &SrcValue,
63  &DstValue,
64  &Length);
65  if (!NT_SUCCESS(Status))
66  {
67  DPRINT1("RtlExpandEnvironmentStrings_U() failed (Status %lx)\n", Status);
68  DPRINT1("Length %lu\n", Length);
69 
70  if (Buffer)
72 
73  return FALSE;
74  }
75  }
76  else
77  {
78  RtlInitUnicodeString(&DstValue, lpValue);
79  }
80 
81  if (!_wcsicmp(lpName, L"TEMP") || !_wcsicmp(lpName, L"TMP"))
82  {
84  {
85  RtlInitUnicodeString(&DstValue, ShortName);
86  }
87  else
88  {
89  DPRINT("GetShortPathNameW() failed for %S (Error %lu)\n", DstValue.Buffer, GetLastError());
90  }
91 
92  DPRINT("Buffer: %S\n", ShortName);
93  }
94 
96 
97  DPRINT("Value: %wZ\n", &DstValue);
98 
100  &Name,
101  &DstValue);
102 
103  if (Buffer)
104  LocalFree(Buffer);
105 
106  if (!NT_SUCCESS(Status))
107  {
108  DPRINT1("RtlSetEnvironmentVariable() failed (Status %lx)\n", Status);
109  return FALSE;
110  }
111 
112  return TRUE;
113 }
IN PDCB IN POEM_STRING IN PUNICODE_STRING IN OUT POEM_STRING ShortName
Definition: fatprocs.h:1294
PVOID PVOID PWCHAR PVOID Environment
Definition: env.c:45
#define TRUE
Definition: types.h:120
USHORT MaximumLength
Definition: env_spec_w32.h:370
LONG NTSTATUS
Definition: precomp.h:26
_In_ LPCSTR lpName
Definition: winbase.h:2736
#define ARRAYSIZE(array)
Definition: filtermapper.c:47
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
struct NameRec_ * Name
Definition: cdprocs.h:464
smooth NULL
Definition: ftsmooth.c:416
NTSYSAPI NTSTATUS NTAPI RtlSetEnvironmentVariable(_In_z_ PWSTR *Environment, _In_ PUNICODE_STRING Name, _In_ PUNICODE_STRING Value)
void DPRINT(...)
Definition: polytest.cpp:61
Definition: bufpool.h:45
NTSYSAPI NTSTATUS WINAPI RtlExpandEnvironmentStrings_U(PCWSTR, const UNICODE_STRING *, UNICODE_STRING *, ULONG *)
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
DWORD WINAPI GetShortPathNameW(IN LPCWSTR lpszLongPath, OUT LPWSTR lpszShortPath, IN DWORD cchBuffer)
Definition: path.c:1832
#define MAX_PATH
Definition: compat.h:26
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
static const WCHAR L[]
Definition: oid.c:1250
Status
Definition: gdiplustypes.h:24
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1577
#define DPRINT1
Definition: precomp.h:8
unsigned int ULONG
Definition: retypes.h:1
#define LPTR
Definition: winbase.h:362
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
HLOCAL NTAPI LocalAlloc(UINT uFlags, SIZE_T dwBytes)
Definition: heapmem.c:1373
_Check_return_ _CRTIMP int __cdecl _wcsicmp(_In_z_ const wchar_t *_Str1, _In_z_ const wchar_t *_Str2)

Referenced by CreateEnvironmentBlock(), SetSystemEnvironment(), and SetUserEnvironment().