Karl S.
Karl S.

Reputation: 355

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

I would like to be able to separately query user and system environment variables.
Like they are shown in the Environment Variables dialog.

By using the GetEnvironmentVariable and *getenv* functions I can only get values obtained after merging the two sets of variables, with the user-defined ones taking precedence.
Just like the set command shows on the command line.

This capability would be useful to avoid silently falling back on a system-defined variable when a user-defined one with the same name doesn't exist.

Even an undocumented API would be fine.

Upvotes: 4

Views: 415

Answers (1)

Anders
Anders

Reputation: 101616

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

Remember that there are two user variable keys in the registry: Environment and Volatile Environment. The volatile key even has sub keys on some versions of Windows.

Upvotes: 3

Related Questions