What is the difference between User Variables and System Variables in Windows?
For years, I never understood the difference between two boxes on Environment Variables window. I simply chose one and added my git or python executable to the PATH variable in either, and somehow, it always worked, regardless what box I used. For this reason, I never took much thought to the difference between user and system variables.
Why two different boxes?
Environment variables play a critical role in system processes and are a fundamental aspect of operating systems. This article serves to provide an instructive, practical, and direct exploration of these elements and how they interact within the system's processes.
Understanding Environment Variables
At its core, every system process comprises an environment block, which is home to a collection of environment variables alongside their respective values.
These variables can be divided into two main categories:
User Environment Variables are unique to each user.
System Environment Variables apply to all users within the system as a whole.
The rest of this article will cover environment variables for programmers, explaining how they work internally, with relationship to processes and child processes.
Inheritance of Environment Variables and Process Creation
Typically, a newly created child process inherits the parent process's environment variables by default. In a similar vein, programs that are initiated by the command processor will also take on the command processor's environment variables.
However, for a distinctive environment for a child process, you'll need to generate a fresh environment block and pass a pointer to it via the CreateProcess function as a parameter.
Interacting with Environment Variables
The command processor offers the set command, which is used for two primary functions: displaying its environment block or for the creation of new environment variables.
For viewing or modifying environment variables, the pathway is straightforward.
Simply select System from the 'Control Panel', click on Advanced system settings, and then navigate to Environment Variables.
Comprehending the Environment Block Structure
An environment block features the environment variables formatted as follows:
Var1=Value1\0
Var2=Value2\0
Var3=Value3\0
...
VarN=ValueN\0\0
Note that an environment variable's name must not include an equal sign =.
Manipulating Environment Variables
The GetEnvironmentStrings function can be utilized to return a pointer to the environment block of the process that called it. Bear in mind that this should be considered a read-only block, meaning you should refrain from modifying it directly.
If you need to alter an environment variable, make use of the SetEnvironmentVariable function. Once you've finished with the environment block acquired from GetEnvironmentStrings, use the FreeEnvironmentStrings function to free the block.
Modifying System Environment Variables
It's worth noting that employing the SetEnvironmentVariable function does not impact the system environment variables. If you need to modify or add system environment variables programmatically, you can do this by adding them to the following registry key:
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment
Subsequently, broadcast a WM_SETTINGCHANGE message with lParam set to the string "Environment". This process enables applications, such as the shell, to detect and incorporate your updates.
Understanding Variable and Block Size Limitations
A user-defined environment variable can hold a maximum of 32,767 characters. While there's no formal restriction on the environment block's size, practical limits exist based on the method used to access the block. For instance, a batch file won't be able to set a variable that exceeds the maximum command line length.
Historically, Windows Server 2003 and Windows XP placed a restriction of 32,767 characters on the environment block for the process. However, beginning with Windows Vista and Windows Server 2008, this technical limitation on the environment block size was removed.
Inspecting and Modifying Variables
Use the GetEnvironmentVariable function if you wish to determine whether a specified variable is defined within the environment of the process that called it and, if it exists, its value.
Retrieving and Expanding Environment Blocks
If you need to fetch a copy of the environment block for a specific user, you can make use of the CreateEnvironmentBlock function. Moreover, to expand environment-variable strings, you should employ the ExpandEnvironmentStrings function.