无需重新启动即可访问自定义环境变量(使用 C++)

发布于 2024-12-26 05:29:21 字数 336 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

坏尐絯 2025-01-02 05:29:21

如果您想在不重新启动系统的情况下执行此操作,则需要广播它。 资源管理器的某些功能

 SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0,
    (LPARAM) "Environment", SMTO_ABORTIFHUNG,
    5000, &dwReturnValue);

可以正确处理此消息,因此在此广播之后启动的程序将看到更改。

  1. 此外,从技术上讲,您不需要重新启动,只需注销即可
    登录就足够了
  2. 另一种选择是简单地重新启动资源管理器

If you want to do this without rebooting the system you need to broadcast it. Something along the lines of

 SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0,
    (LPARAM) "Environment", SMTO_ABORTIFHUNG,
    5000, &dwReturnValue);

Explorer handles this message correctly so programs started after this broadcast will see the changes.

  1. Also technically you don't need to reboot , a simple logoff and
    login will suffice
  2. Another option is to simply restart explorer
暮年慕年 2025-01-02 05:29:21

我最近遇到了类似的情况,广播消息是正确的方式,如本知识库(和parapura)中所述:

http://support.microsoft.com/kb/104011

但是,我建议将 _T() 放在“环境”(或者可能是“L”)周围,以确保您传递的是正确的字符串(ANSI 或宽)。像这样:

    SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0,
        (LPARAM) _T("Environment"), SMTO_ABORTIFHUNG,
        5000, &dwReturnValue);

我在命令行应用程序中使用了上面的内容。如果没有 _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:

    SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0,
        (LPARAM) _T("Environment"), SMTO_ABORTIFHUNG,
        5000, &dwReturnValue);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文