【问题标题】:How to add environment variable in C++?如何在 C++ 中添加环境变量?
【发布时间】:2011-07-11 21:20:42
【问题描述】:

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

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

谢谢

【问题讨论】:

标签: c++ windows environment-variables


【解决方案1】:

来自MSDN

以编程方式添加或修改 系统环境变量,添加它们 到 HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment 注册表项,然后 广播WM_SETTINGCHANGE 消息 将lParam 设置为字符串 “环境”。这允许 应用程序,例如外壳,以 拿起你的更新...

【讨论】:

  • 不使用注册表还有其他方法吗?
  • @code9215:不,目前还没有,但如果你找到了,你应该写一个答案。
  • 系统是全局设置。添加用户设置怎么样?
【解决方案2】:

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

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

祝你好运。

【讨论】:

  • 经过我自己的独立研究后,我发现这是最简单的机制。请记住,在您更新路径环境变量之前存在的每个进程可能会或可能不会响应您应该在更新后发送的消息。这意味着,更改可能仅适用于新流程。
【解决方案3】:

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

【讨论】:

    【解决方案4】:

    【讨论】:

      【解决方案5】:
      #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;
      }
      

      【讨论】:

      • 欢迎来到 Stack Overflow。如果您能解释为什么您的答案比已经提供的更好,那就太好了。
      【解决方案6】:

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

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

      【讨论】:

      • 他想全局设置变量;该函数只适用于当前进程。
      • 我想你还没有阅读函数的文档。该函数可以为“Process”或“User”或“Machine”设置变量。您可以通过传递 targer = EnvironmentVariableTarget.Machine 在机器级别执行此操作
      • 这是一个C#函数;他在问 C++。
      • 好吧,如果你不想同意,我能说什么。我不会再发表任何评论了。最后一件事供您参考。此方法可以在 C++、C#、VB 和 F# 中调用(请参阅所有语言的示例代码链接)。我同意该功能是框架的一部分,而不是 Win32 的一部分,但所提出的问题不需要。问题只是说“Windows 上的 C++”。我想你没有仔细阅读这个问题。总之玩的开心!! :)
      • “Windows 上的 C++”无法调用 .Net 方法。 (很少有人使用 C++/CLI)
      猜你喜欢
      • 2022-07-11
      • 2018-06-16
      • 2013-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-30
      • 2015-07-10
      • 1970-01-01
      相关资源
      最近更新 更多