The Windows Processes and Threads 13

 

 

 

 

 

Environment Variables

 

Every process has an environment block that contains a set of environment variables and their values. There are two types of environment variables: user environment variables (set for each user) and system environment variables (set for everyone). By default, a child process inherits the environment variables of its parent process. Programs started by the command processor inherit the command processor's environment variables. To specify a different environment for a child process, create a new environment block and pass a pointer to it as a parameter to the CreateProcess() function. The command processor provides the set command to display its environment block or to create new environment variables. You can also view or modify the environment variables by selecting System from the Control Panel, selecting Advanced system settings, and clicking Environment Variables.

 

Accessing the Windows Environment variables

 

The Windows System Variables and user's environment variables

 

Each environment block contains the environment variables in the following format:

 

Var1=Value1\0

Var2=Value2\0

Var3=Value3\0

...

VarN=ValueN\0\0

 

The name of an environment variable cannot include an equal sign (=). The GetEnvironmentStrings() function returns a pointer to the environment block of the calling process. This should be treated as a read-only block; do not modify it directly. Instead, use the SetEnvironmentVariable() function to change an environment variable. When you are finished with the environment block obtained from GetEnvironmentStrings(), call the FreeEnvironmentStrings() function to free the block. Calling SetEnvironmentVariable() has no effect on the system environment variables. To programmatically add or modify system environment variables, add them to the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment registry key, then broadcast a WM_SETTINGCHANGE message with lParam set to the string "Environment". This allows applications, such as the shell, to pick up your updates. Note that the values of the environment variables listed in this key are limited to 1024 characters. The GetEnvironmentVariable() function determines whether a specified variable is defined in the environment of the calling process, and, if so, what its value is. To retrieve a copy of the environment block for a given user, use the CreateEnvironmentBlock() function. To expand environment-variable strings, use the ExpandEnvironmentStrings() function. You can use the set command to manipulate the environment variables. The following screenshots show some of the set command and options.

 

The Windows set command

 

The following is the set command without any option(s).

 

set command in action showing the environment variables

 

 

 

< Processes & Threads 12 | Win32 Process & Thread Programming | Win32 Programming | Processes & Threads 14 >