#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <iostream>
#include <cstdio>
#include <string>
#include <tchar.h>

using namespace std;

void setValue(HKEY hRootKey,char *key,char plusPath[],char *key_value,DWORD dwType);
void main()
{
    // Java 环境变量路径
    // char plusPath[] = ";C:\\software\\develop\\eclipse49\\plus\\jdk1.8.0_191\\bin";
    char plusPath[] = ";D:\\AUTO_Uninstaller_x64_8.8.1\\bin\\jre1.8.0_191\\bin";
    // 根路径
    HKEY hRootKey=HKEY_LOCAL_MACHINE;
    // 系统环境变量
    // HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment
    char *key = "System\\CurrentControlSet\\Control\\Session Manager\\Environment";
    // 需要修改的值
    char *key_value="Path";
    // key_value的数据类型
    DWORD valueType = REG_EXPAND_SZ;

    // 调用修改注册表值的函数
    setValue(hRootKey,key,plusPath,key_value,valueType);

    // 测试是否修改成功
    cout << "====================================================================";
    cout << "\n";
    system("java -version");
    system("pause");
}

void setValue(HKEY hRootKey,char *key,char plusPath[],char *key_value,DWORD dwType)
{

    HKEY hKey;
    ] = {};
    DWORD dwSize = sizeof(data);
    DWORD dwDisposition=REG_OPENED_EXISTING_KEY;

    //如果不存在就创建
    LONG resulte=RegCreateKeyExA(hRootKey,key,,NULL,REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL,&hKey,&dwDisposition);
    if (resulte != ERROR_SUCCESS)
    {
        //MessageBox(NULL, _T("打开注册表失败"), _T("提示"), MB_OK);
        cout << "打开注册表失败 [" << key <<"]";
        cout << "\n";
    }
    else
    {
        if (dwDisposition == REG_OPENED_EXISTING_KEY)
        {
            //MessageBox(NULL, _T("打开一个存在的注册表项"), _T("提示"), MB_OK);
            cout << "打开注册表项 [" << key <<"]";
            cout << "\n";
        }
        else if (dwDisposition == REG_CREATED_NEW_KEY)
        {
            //MessageBox(NULL, _T("新建一个注册表项"), _T("提示"), MB_OK);
            cout << "新建注册表项 [" << key <<"]";
            cout << "\n";
        }
    }

    // 打开该key,在程序末尾关闭
    LONG OpenKey_nError = RegOpenKeyExA(HKEY_LOCAL_MACHINE, key, , KEY_READ|KEY_WRITE, &hKey);
    if (OpenKey_nError==ERROR_FILE_NOT_FOUND)
    {
        cout << "Creating registry key: " << key << endl;
        OpenKey_nError = RegCreateKeyExA(hRootKey, key, NULL, NULL, REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL, &hKey, NULL);
    }
    if (OpenKey_nError)
        cout << "Error: " << OpenKey_nError << " Could not find or create " << key << endl;

    // 查询该值
    LONG QueryValue_nError = RegQueryValueExA(hKey, key_value, NULL, &dwType, (LPBYTE)data, &dwSize);
    if (QueryValue_nError)
        cout << "Error: " << QueryValue_nError << " Could not set registry value: " << key_value << endl;

    int length=strlen(plusPath) + strlen(data);
    string data_str=data;
    string plusPath_str=plusPath;
    string plus_str;
    string::size_type idx;
    idx=data_str.find(plusPath_str);

    if(idx == string::npos ){//不存在
        char *environment = new char[length];
        plus_str=data_str+plusPath_str;
        strcpy(environment, plus_str.c_str());//
        cout << environment;
        cout << "\n";
        LONG SetValue_nError = RegSetValueExA(hKey, key_value, NULL, REG_EXPAND_SZ, (const unsigned char *)environment, strlen(environment));
        if (SetValue_nError)
            cout << "Error: " << SetValue_nError << " Could not set registry value: " << key_value << endl;
    }else{
        cout << "已存在 [" << plusPath << "]";
        cout << "\n";
    }

    DWORD dwResult;
    SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, ,LPARAM(_T(, &dwResult);//广播立即执行

    LONG QueryValue2_nError = RegQueryValueExA(hKey, key_value, NULL, &dwType, NULL, &dwSize);

    if (QueryValue2_nError==ERROR_FILE_NOT_FOUND){
        // The value will be created and set to data next time SetVal() is called.
    }else if (QueryValue2_nError)
        cout << "Error: " << QueryValue2_nError << " Could not get registry value : " << key_value << endl;

    RegCloseKey(hKey);
}
05-07 15:12
查看更多