刷新环境变量的值

发布于 2024-12-11 03:43:05 字数 356 浏览 2 评论 0原文

在我的 Installscript 项目中,我需要重新启动才能注册环境变量的值。但我希望该应用程序无需重新启动即可安装。那么他们有什么方法可以刷新环境变量的值,以便我的应用程序注册并且不需要重新启动? 我已经在使用以下代码行:

define WM_WININICHANGE 0x001A'
define HWND_BROADCAST 0xffff'
szEnv = "Environment";
pEnv = &szEnv;
SendMessage( HWND_BROADCAST, WM_WININICHANGE, 0, pEnv );`

是否有其他方法来刷新环境变量值? 我在 Windows xp 上运行这个程序。

In my Installscript project I need a reboot to register the values of Environment variables. But I want that application should setup without a reboot. So is their any way to refresh the values of Environment variable so that my application gets registered and no reboot is required?
I am already using following code line :

define WM_WININICHANGE 0x001A'
define HWND_BROADCAST 0xffff'
szEnv = "Environment";
pEnv = &szEnv;
SendMessage( HWND_BROADCAST, WM_WININICHANGE, 0, pEnv );`

Is their is any other way to refresh the Environment variable values?
I am running this on Windows xp.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

思慕 2024-12-18 03:43:05

设置环境变量的 Windows 进程无法访问该变量进行读取。这是 Windows 中的限制。这个想法是,如果您的进程设置了一个变量,它就已经知道该变量值。

因此,如果您的安装程序正在设置环境变量,则您的应用程序必须在单独且不相关的进程中运行才能读取该变量。这就是为什么安装完成后启动应用程序不起作用的原因。

解决方案是在安装期间启动应用程序时通过应用程序命令行传递变量值。任何未来的启动仍将直接访问该变量。

A Windows process which sets an environment variable cannot access that variable for reading. This is a limitation in Windows. The idea is that if your process sets a variable, it already knows the variable value.

So if your installer is setting an environment variable, your application must run in a separate and unrelated process to read that variable. This is why launching the application when the installation finishes doesn't work.

A solution is to pass the variable value through your application command line when launching it during install. Any future launches will still access the variable directly.

好久不见√ 2024-12-18 03:43:05

将 WININICHANGE 消息发送到广播地址是正确的做法。但是,不要求所有正在运行的进程正确地子类化该消息并更新该进程的环境变量。他们应该这样做,但并不总是发生。最臭名昭著的例子是服务控制管理器。您必须重新启动 SCM 才能看到新的变量/值。

现在,如果您问“如何让当前正在运行的进程看到这个值?” ( Cosmin 似乎认为这就是你所问的,但我不确定你是否是)那么答案在于理解环境空间有四个集合:

用户
机器
过程
易失

http://msdn.microsoft.com/en -us/library/6s7w15a0(v=vs.85).aspx

您的代码所做的是设置 SYSTEM 的环境变量。这就像过去您在 autoexec.bat ( SET FOO=BAR ) 中添加一行并重新启动一样。但是您也可以从 Windows 创建一个新的 dos 提示符并执行 SET SOMETHING=ELSE ,它只会在该进程和子进程的生命周期中可见,而不会在其他进程中可见。这是“进程”空间与“系统”空间。另外,如果您使用新变量更新了 AUTEXEC.BAT 并创建了一个新进程而不重新启动,它将看不到新变量,但您始终可以自己设置并查看它(尽管在技术上不是相同的)。

我知道,使用 SendMessage 您不需要重新启动,但无论如何并非所有进程都会收到消息。

因此,如果您需要当前的 InstallScript 进程也具有这个新变量,您需要调用 Kernel32 的 SetEnvironmentVariable 函数,根据MSDN“设置指定环境变量的内容当前的进程。”

有趣的是,InstallScript 有一个 GetEnvVar 函数,但没有 SetEnvVar 函数,因此您必须将其原型化为外部函数,然后调用它。

可以在此处找到有关示例的讨论。

Sending the WININICHANGE message to the broadcast address is the correct thing to do. However there is no requirement that all running processes correctly subclass that message and update their environment variables for that process. They are supposed to, but it doesn't always happen. The most notorious example of this is the Service Control Manager. You have to reboot for the SCM to see the new variable/value.

Now if you are asking "how do I get my current running process to see this value?" ( Cosmin seems to think that's what you are asking but I'm not sure if you are or not ) then the answer lies in understanding that the environment space has four collections:

User
Machine
Process
Volatile

http://msdn.microsoft.com/en-us/library/6s7w15a0(v=vs.85).aspx

What your code does is set the environment var for SYSTEM. This is like the old days when you would put a line in your autoexec.bat ( SET FOO=BAR ) and reboot. But you could also create a new dos prompt from windows and do SET SOMETHING=ELSE and it would only be seen for the life of that process and child processes but no other processes. This is the "Process" space vs the "System" space. Also if you updated AUTEXEC.BAT with a new variable and created a new process without rebooting it wouldn't see the new variable but you could always set it yourself and see it ( although not technically the same one ).

I know, with SendMessage you shouldn't need the reboot but regardless not all processes will get the message.

So, if you need the current InstallScript process to also have this new variable, you'll want to call Kernel32's SetEnvironmentVariable function which according to MSDN "Sets the contents of the specified environment variable for the current process."

Interesting InstallScript has a GetEnvVar function but not a SetEnvVar function so you'll have to prototype this as an external function and then call it.

A discussion with samples can be found here.

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