无需重新启动即可访问自定义环境变量(使用 C++)
我正在使用 C++ 编写一个程序,该程序利用自定义的系统范围环境变量。该变量由 msi 安装程序设置。后来我的程序使用 GetEnvironmentVariable() API 读取它。
问题是,似乎需要重新启动系统才能使我的自定义环境变量在我的程序中可见,而我不想为此重新启动系统。
似乎奇怪的是,如果(无需重新启动)我右键单击“我的电脑”,然后进入“属性”->“高级并单击“环境变量”,我的自定义环境变量位于该列表中,但由于某种原因 GetEnvironmentVariable() 仍然看不到它。
那么有没有其他 API 可以让我在不重新启动系统的情况下使用呢? (因为系统属性可以清楚地看到它。)
I'm writing a program using C++ that takes advantage of a custom system-wide environment variable. That variable is set by an msi installer. Later my program reads it using GetEnvironmentVariable() API.
The problem is that it seems like the system needs to be rebooted for my custom environment variable to be visible in my program and I'd hate to reboot the system just for that.
What seems to be odd is that if (without rebooting) I right-click on My Computer and then go into Properties -> Advanced and click on "Environment variables" my custom environment variable is in that list but for some reason GetEnvironmentVariable() still doesn't see it.
So is there any other API that I can use that will work without rebooting the system? (As system properties can clearly see it then.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想在不重新启动系统的情况下执行此操作,则需要广播它。 资源管理器的某些功能
可以正确处理此消息,因此在此广播之后启动的程序将看到更改。
登录就足够了
If you want to do this without rebooting the system you need to broadcast it. Something along the lines of
Explorer handles this message correctly so programs started after this broadcast will see the changes.
login will suffice
我最近遇到了类似的情况,广播消息是正确的方式,如本知识库(和parapura)中所述:
http://support.microsoft.com/kb/104011
但是,我建议将 _T() 放在“环境”(或者可能是“L”)周围,以确保您传递的是正确的字符串(ANSI 或宽)。像这样:
我在命令行应用程序中使用了上面的内容。如果没有 _T() 消息发送会成功,但我的系统似乎从未收到环境变量的更新。
顺便说一句,“setx”命令行可能使用相同的机制来更新环境变量。
另外,我在 atl dll 中使用它。
i recently encountered something like this and broadcasting the message is the correct way as explained in this kb (and by parapura):
http://support.microsoft.com/kb/104011
however, i would suggest to put _T() around the "Environment" (or maybe an 'L') to make sure you are passing in the correct string (ansi or wide). like this:
i used the above in a commandline app. without the _T() the message sending succeeds but my system never seem to receive update of the environment variable.
btw, the 'setx' command line probably uses the same mechanism to update the environment variables.
also, i'm using this in an atl dll.