windows 如何在C++中添加环境变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5246046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add environment variable in C++?
提问by VextoR
Is there any way I can add environment variable in Windows via C++?
有什么办法可以通过 C++ 在 Windows 中添加环境变量?
They have to be added in "My computer->properties->advanced->environment variables"
它们必须添加到“我的电脑->属性->高级->环境变量”中
Thank you
谢谢
回答by SteelBytes
from MSDN:
来自MSDN:
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 aWM_SETTINGCHANGE
message withlParam
set to the string "Environment". This allows applications, such as the shell, to pick up your updates ...
以编程方式添加或修改系统环境变量,把它们添加到
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment
注册表项,然后广播WM_SETTINGCHANGE
与信息lParam
设置为字符串“环境”。这允许应用程序(例如外壳程序)获取您的更新......
回答by 0xC0000022L
The only way I know is via the registry.
我知道的唯一方法是通过注册表。
Hint, the global variables are in HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
and those for each user in HKEY_USERS\*\Environment
, where *
denotes the SID of the user.
提示,全局变量在 中HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
,每个用户的变量在中HKEY_USERS\*\Environment
,其中*
表示用户的 SID。
Good luck.
祝你好运。
回答by Nati
Here's a simple implementation (Based on the MSDN instruction posted by SteelBytes):
这是一个简单的实现(基于 SteelBytes 发布的 MSDN 指令):
bool SetPermanentEnvironmentVariable(LPCTSTR value, LPCTSTR data)
{
HKEY hKey;
LPCTSTR keyPath = TEXT("System\CurrentControlSet\Control\Session Manager\Environment");
LSTATUS lOpenStatus = RegOpenKeyEx(HKEY_LOCAL_MACHINE, keyPath, 0, KEY_ALL_ACCESS, &hKey);
if (lOpenStatus == ERROR_SUCCESS)
{
LSTATUS lSetStatus = RegSetValueEx(hKey, value, 0, REG_SZ,(LPBYTE)data, strlen(data) + 1);
RegCloseKey(hKey);
if (lSetStatus == ERROR_SUCCESS)
{
SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM)"Environment", SMTO_BLOCK, 100, NULL);
return true;
}
}
return false;
}
回答by Kewalkumar Shah
#include <iostream>
#include <windows.h>
#include <cstring>
#include "tchar.h"
void SetUserVariablePath(){
HKEY hkey;
long regOpenResult;
const char key_name[] = "Environment";
const char path[]="D:/custom_command"; //new_value path need to update
regOpenResult = RegOpenKeyEx(HKEY_CURRENT_USER,key_name, 0, KEY_ALL_ACCESS, &hkey);
LPCSTR stuff = "VVS_LOGGING_PATH"; //Variable Name
RegSetValueEx(hkey,stuff,0,REG_SZ,(BYTE*) path, strlen(path));
RegCloseKey(hkey);
}
void GetUserVariablePath(){
static const char path[] = "VVS_LOGGING_PATH" ; //Variable Name
static BYTE buffer1[1000000] ;
DWORD buffsz1 = sizeof(buffer1) ;
{
//HKEY_CURRENT_USER\Environment
const char key_name[] = "Environment";
HKEY key ;
if( RegOpenKeyExA( HKEY_CURRENT_USER, key_name, 0, KEY_QUERY_VALUE, std::addressof(key) ) == 0 &&
RegQueryValueExA( key, path, nullptr, nullptr, buffer1, std::addressof(buffsz1) ) == 0 )
{
std::cout << "The updated value of the user variable is : " << reinterpret_cast<const char*>(buffer1) << '\n' ;
}
}
}
int main()
{
SetUserVariablePath();
GetUserVariablePath();
return 0;
}
回答by ADringer
Have you tried Set local environment variables in C++?
您是否尝试过在 C++ 中设置本地环境变量?
回答by Tayyab
The environment variables in Windows are stored in Windows Registry. You can use "SetEnvironmentVariable" function for the purpose, please see the documentation of the function at link below.
Windows 中的环境变量存储在 Windows 注册表中。您可以为此使用“SetEnvironmentVariable”函数,请参阅下面链接中的函数文档。