简体   繁体   English

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

[英]Is there a way to set the environment path programmatically in C++ on Windows?

Is there a way to set the global windows path environment variable programmatically (C++)? 有没有办法以编程方式设置全局Windows路径环境变量(C ++)?

As far as I can see, putenv sets it only for the current application. 据我所知,putenv仅为当前应用程序设置它。
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? 直接在注册表中更改(HKLM\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment)也是一个选项虽然我更喜欢API方法,如果有的话?

MSDN Says : MSDN

Calling SetEnvironmentVariable has no effect on the system environment variables. 调用SetEnvironmentVariable对系统环境变量没有影响。 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". 若要以编程方式添加或修改系统环境变量,将它们添加到HKEY_LOCAL_MACHINE \\ System \\ CurrentControlSet \\ Control \\ Session Manager \\ Environment注册表项,然后广播WM_SETTINGCHANGE消息,并将lParam设置为字符串“Environment”。 This allows applications, such as the shell, to pick up your updates. 这允许应用程序(如shell)获取更新。 Note that the values of the environment variables listed in this key are limited to 1024 characters. 请注意,此键中列出的环境变量的值限制为1024个字符。

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\

There is no API - changing the registry is the way to do it. 没有API - 改变注册表就是这样做的。 The changed value will be used for processes starting after the change was made. 更改后的值将用于更改后开始的进程。

Also: Notice that running applications must actively process the settings changed message and many (most?) do not do so. 另外:请注意,正在运行的应用程序必须主动处理设置已更改的消息,并且许多(大多数?)不这样做。

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. Microsoft还提供了一个名为setx的小命令行实用程序及其资源工具包 ,可以让您执行此操作。 By the way, the regular set command just lets you define local environment variables I think. 顺便说一下,常规set命令只允许你定义我认为的局部环境变量。

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)“环境”使用SMTO_ABORTIFHUNG一段时间(5000)。

This is what setX.exe provided with resource Kit does. 这是setX.exe随资源Kit提供的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM