如何在C++中添加环境变量?

发布于 2024-10-21 01:29:29 字数 96 浏览 3 评论 0原文

有什么方法可以通过 C++ 在 Windows 中添加环境变量吗?

必须在“我的电脑->属性->高级->环境变量”中添加,

谢谢

Is there any way I can add environment variable in Windows via C++?

They have to be added in "My computer->properties->advanced->environment variables"

Thank you

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

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

发布评论

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

评论(6

作妖 2024-10-28 01:29:29

来自 MSDN

以编程方式添加或修改
系统环境变量,添加

HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment 注册表项,然后
广播 WM_SETTINGCHANGE 消息
lParam 设置为字符串
“环境”。这允许
应用程序,例如 shell,
获取您的更新...

from 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 a WM_SETTINGCHANGE message
with lParam set to the string
"Environment". This allows
applications, such as the shell, to
pick up your updates ...

浮光之海 2024-10-28 01:29:29

我知道的唯一方法是通过注册表。

提示,全局变量位于 HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment 中,每个用户的变量位于 HKEY_USERS\*\Environment 中,其中 * 表示用户的SID。

祝你好运。

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.

Good luck.

心欲静而疯不止 2024-10-28 01:29:29

这是一个简单的实现(基于 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;
}

Here's a simple implementation (Based on the MSDN instruction posted by SteelBytes):

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;
}
冷情妓 2024-10-28 01:29:29
#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;
}
#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;
}
抚你发端 2024-10-28 01:29:29

Windows中的环境变量存储在Windows注册表中。您可以使用“System.Environment.SetEnvironmentVariable”.NET 函数来实现此目的,请参阅下面链接中的函数文档。

http://msdn.microsoft.com/en-us/library/96xafkes。 ASPX

The environment variables in Windows are stored in Windows Registry. You can use "System.Environment.SetEnvironmentVariable" .NET function for the purpose, please see the documentation of the function at link below.

http://msdn.microsoft.com/en-us/library/96xafkes.aspx

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