How to separately get user and system environment variables in Windows using C

I would like to be able to separately request user and system environment variables.
As shown in the dialog box Environment Variables.

Using the functions GetEnvironmentVariableand *getenv*, I can only get the values ​​obtained after merging two sets of variables, with user-defined priorities.
Just as the command setshows on the command line.

This feature would be useful to avoid silently rejecting a system variable if a user with the same name does not exist.

Even the uncertified API will be fine.

+4
source share
1 answer

ExpandEnvironmentStringsForUser( CreateEnvironmentBlock) will only use system variables if you pass a handle to a NULL token, but I don’t think there is a function that gives you only user variables, so you have to manually read them from the registry.

Remember that there are two user variable keys in the registry: Environmentand Volatile Environment. The non-volatile key even has auxiliary keys in some versions of Windows.

+3
source

Source: https://habr.com/ru/post/1681175/


All Articles