简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM