有没有办法在Windows上以C ++编程方式设置环境路径?

时间:2021-04-13 23:32:20

Is there a way to set the global windows path environment variable programmatically (C++)?

As far as I can see, putenv sets it only for the current application.
Changing directly in the registry (HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment) is also an option though I would prefer API methods if there are?

有没有办法以编程方式设置全局Windows路径环境变量(C ++)?据我所见,putenv仅为当前应用程序设置它。直接在注册表中更改(HKLM \ SYSTEM \ CurrentControlSet \ Control \ Session Manager \ Environment)也是一个选项虽然我更喜欢API方法,如果有的话?

5 个解决方案

#1


24  

MSDN Says:

Calling SetEnvironmentVariable has no effect on the system environment variables. To programmatically add or modify system environment variables, add them to the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment registry key, then broadcast a WM_SETTINGCHANGE message with lParam set to the string "Environment". This allows applications, such as the shell, to pick up your updates. Note that the values of the environment variables listed in this key are limited to 1024 characters.

调用SetEnvironmentVariable对系统环境变量没有影响。若要以编程方式添加或修改系统环境变量,将它们添加到HKEY_LOCAL_MACHINE \ System \ CurrentControlSet \ Control \ Session Manager \ Environment注册表项,然后广播WM_SETTINGCHANGE消息,并将lParam设置为字符串“Environment”。这允许应用程序(如shell)获取更新。请注意,此键中列出的环境变量的值限制为1024个字符。

#2


9  

As was pointed out earlier, to change the PATH at the machine level just change this registry entry:

如前所述,要更改机器级别的PATH,只需更改此注册表项:

HLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

But you can also set the PATH at the user level by changing this registry entry:

但您也可以通过更改此注册表项来在用户级别设置PATH:

HKEY_CURRENT_USER\Environment\Path

And you can also set the PATH at the application level by adding the application\Path details to this registry entry:

您还可以通过将application \ Path详细信息添加到此注册表项来在应用程序级别设置PATH:

HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\

#3


3  

There is no API - changing the registry is the way to do it. The changed value will be used for processes starting after the change was made.

没有API - 改变注册表就是这样做的方法。更改后的值将用于更改后开始的进程。

Also: Notice that running applications must actively process the settings changed message and many (most?) do not do so.

另外:请注意,正在运行的应用程序必须主动处理设置已更改的消息,并且许多(大多数?)不这样做。

#4


2  

If you want to do it through the registry, you might want to look at the source code of this program.

如果您想通过注册表执行此操作,您可能需要查看此程序的源代码。

Microsoft also provides a small command line utility called setx with its resource toolkits, which will let you do this. By the way, the regular set command just lets you define local environment variables I think.

Microsoft还提供了一个名为setx的小型命令行实用程序及其资源工具包,可以让您执行此操作。顺便说一下,常规set命令只允许你定义我认为的局部环境变量。

#5


2  

Yes You are correct. You also need to effect these settings without logging off

是的,你是对的。您还需要在不注销的情况下实现这些设置

Send Message of borad casting to all windows SETTINGCHANGE for the parameter (LPARAM) "Environment" with SMTO_ABORTIFHUNG for some milliseconds(5000) using SendMessageTimeout API.

使用SendMessageTimeout API将borad强制转换消息发送到所有窗口SETTINGCHANGE,参数(LPARAM)“Environment”与SMTO_ABORTIFHUNG一起使用毫秒(5000)。

This is what setX.exe provided with resource Kit does.

这就是setX.exe随资源Kit提供的功能。

#1


24  

MSDN Says:

Calling SetEnvironmentVariable has no effect on the system environment variables. To programmatically add or modify system environment variables, add them to the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment registry key, then broadcast a WM_SETTINGCHANGE message with lParam set to the string "Environment". This allows applications, such as the shell, to pick up your updates. Note that the values of the environment variables listed in this key are limited to 1024 characters.

调用SetEnvironmentVariable对系统环境变量没有影响。若要以编程方式添加或修改系统环境变量,将它们添加到HKEY_LOCAL_MACHINE \ System \ CurrentControlSet \ Control \ Session Manager \ Environment注册表项,然后广播WM_SETTINGCHANGE消息,并将lParam设置为字符串“Environment”。这允许应用程序(如shell)获取更新。请注意,此键中列出的环境变量的值限制为1024个字符。

#2


9  

As was pointed out earlier, to change the PATH at the machine level just change this registry entry:

如前所述,要更改机器级别的PATH,只需更改此注册表项:

HLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

But you can also set the PATH at the user level by changing this registry entry:

但您也可以通过更改此注册表项来在用户级别设置PATH:

HKEY_CURRENT_USER\Environment\Path

And you can also set the PATH at the application level by adding the application\Path details to this registry entry:

您还可以通过将application \ Path详细信息添加到此注册表项来在应用程序级别设置PATH:

HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\

#3


3  

There is no API - changing the registry is the way to do it. The changed value will be used for processes starting after the change was made.

没有API - 改变注册表就是这样做的方法。更改后的值将用于更改后开始的进程。

Also: Notice that running applications must actively process the settings changed message and many (most?) do not do so.

另外:请注意,正在运行的应用程序必须主动处理设置已更改的消息,并且许多(大多数?)不这样做。

#4


2  

If you want to do it through the registry, you might want to look at the source code of this program.

如果您想通过注册表执行此操作,您可能需要查看此程序的源代码。

Microsoft also provides a small command line utility called setx with its resource toolkits, which will let you do this. By the way, the regular set command just lets you define local environment variables I think.

Microsoft还提供了一个名为setx的小型命令行实用程序及其资源工具包,可以让您执行此操作。顺便说一下,常规set命令只允许你定义我认为的局部环境变量。

#5


2  

Yes You are correct. You also need to effect these settings without logging off

是的,你是对的。您还需要在不注销的情况下实现这些设置

Send Message of borad casting to all windows SETTINGCHANGE for the parameter (LPARAM) "Environment" with SMTO_ABORTIFHUNG for some milliseconds(5000) using SendMessageTimeout API.

使用SendMessageTimeout API将borad强制转换消息发送到所有窗口SETTINGCHANGE,参数(LPARAM)“Environment”与SMTO_ABORTIFHUNG一起使用毫秒(5000)。

This is what setX.exe provided with resource Kit does.

这就是setX.exe随资源Kit提供的功能。