从C ++更新系统环境变量 [英] Update system environment variable from c++

查看:74
本文介绍了从C ++更新系统环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在编写一个与系统环境变量一起使用的非托管C ++程序.我正在通过GetEnvironmentVariable(...)获得价值.现在我有一个C#程序,可以随时更改此变量,例如像这样:

I am currently writing an unmanaged C++ program which works with a system environment variable. I am getting the value with GetEnvironmentVariable(...). Now I have an C# program which may change this variable at any time, e.g. like this:

Environment.SetEnvironmentVariable("CalledPath", System.Windows.Forms.Application.ExecutablePath, EnvironmentVariableTarget.Machine);

问题在于C ++程序不会自动更新此变量(或一般来说是其环境块),因此,除非重新启动程序,否则我仍然会使用旧值,除非这样做确实不好.有没有一种方法可以更新环境块,或者最好是另一种方法来读取系统环境变量?

The problem is that the C++ program does not update this variable (or its environment block in general) automatically so that I am still working with the old value unless I restart the program which is not really good. Is there a way to update the environment block or preferably another way to read system environment variables?

预先感谢,俄罗斯

推荐答案

谢谢大家,但我终于想通了.由于我通过GetEnvironmentVariable接收的值不是当前值,因此我直接从注册表中读取值.机器环境变量存储在

Thank you guys but I finally figured it out myself. Since the values I receive with GetEnvironmentVariable are not the current ones I read the values directly from the registry. The machine environment variables are stored in

HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Control \ Session Manager \ Environment

我通过RegOpenKeyEx(...)和RegQueryValueEx(...)函数阅读它们,效果很好.

I read them via the RegOpenKeyEx(...) and RegQueryValueEx(...) functions which works perfectly well.

这篇关于从C ++更新系统环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆