zoukankan      html  css  js  c++  java
  • VC 设置环境变量

       //unicode编码下

           HKEY hregkey;
       DWORD dwReturnValue = 0;
       CString strEnvironmentPath(_T("")); //环境变量值

       TCHAR chsys[MAX_PATH] = {0};
       CString sSysCh;
       GetSystemDirectory( chsys , sizeof(chsys) ); //获取系统盘盘符
       sSysCh.Format( _T("%s") , chsys );
       CString charSysCh = _T("");
       charSysCh.Format(_T("%c"),sSysCh.GetAt(0));
       sSysCh = charSysCh + _T(":\\windows\\system32;") ;//设置system32环境变量参数
       long res = RegOpenKeyEx(HKEY_LOCAL_MACHINE, 
          _T("System\\CurrentControlSet\\Control\\Session Manager\\Environment"), 
          0, KEY_ALL_ACCESS, &hregkey); //打开注册表环境变量
       if(res == 0)
       {
         LPBYTE lpa = new BYTE[500];
          DWORD dwAidSize=500;
          DWORD type=REG_SZ;
          if(ERROR_SUCCESS == ::RegQueryValueEx(hregkey,_T("Path"),
             NULL,&type,lpa,&dwAidSize))
          {
             strEnvironmentPath = (LPTSTR)lpa;

           if (strEnvironmentPath.Find(sSysCh) !=  -1)
                return TRUE;
             strEnvironmentPath.Insert(0,sSysCh);   
             if (ERROR_SUCCESS ==::RegSetValueEx(hregkey,_T("Path"),
                0,REG_SZ,(LPBYTE)(LPCTSTR)strEnvironmentPath,
                 strEnvironmentPath.GetLength()*2)
              )
           {
              SendMessageTimeout(HWND_BROADCAST, 
               WM_SETTINGCHANGE, 
               0, (LPARAM) _T("Environment"), 
               SMTO_ABORTIFHUNG, 
               5000,
               &dwReturnValue); //使修改的环境变量立即生效
              ::RegCloseKey(hregkey);
              return TRUE;
           }
           AfxMessageBox(strEnvironmentPath);
          }
          ::RegCloseKey(hregkey); 
       }

  • 相关阅读:
    在Visual Studio中使用正则表达式匹配换行和批量替换
    Microsoft Visual Studio Ultimate 2013 Update 2 RC 英文版--离线完整安装ISO+简体中文语言包
    TFS 2012 在IE11和Chrome (Windows 8.1) 显示英文的解决方案
    Windows 8 应用商店无法连接到网络的终极完美解决方案
    Visual Studio 2012 Update 4 RC 启动调试失败解决方案
    2013最新版Subversion 1.7.10 for Windows x86 + Apache 2.4.4 x64 安装配置教程+错误解决方案
    PKCS#1
    密钥协商机制
    base64的编码
    认证过程
  • 原文地址:https://www.cnblogs.com/pbreak/p/1745635.html
Copyright © 2011-2022 走看看