C++ 注册表操作

#include<iostream>
#include<windows.h>//所需头文件
//#include"cpptest.h"
using namespace std;

void read_subitem()
{//子健获取
    HKEY cpp_key;
    LPCTSTR cpp_data = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"; //要读取的路径
        cout << "---读取子键---" << endl;
    //尝试打开注册表,‘HKEY_LOCAL_MACHINE’要打开的根表;‘cpp_data’要打开的子表项;‘0’固定值;‘KEY_ALL_ACCESS’申请的权限;‘&cpp_key’返回句柄;
    if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE, cpp_data, 0, KEY_READ, &cpp_key)){
        DWORD dwIndex = 0, NameSize, NameCnt, NameMaxLen, Type;
        DWORD KeySize, KeyCnt, KeyMaxLen, DateSize, MaxDateLen;
        if (ERROR_SUCCESS == RegQueryInfoKey(cpp_key, NULL, NULL, 0, &KeyCnt, &KeyMaxLen, NULL, &NameCnt, &NameMaxLen, &MaxDateLen, NULL, NULL)){
            cout << "共有" << KeyCnt << "个子键" << endl;
            for (DWORD dwIndex = 0; dwIndex < KeyCnt; dwIndex++)//枚举子键
            {
                KeySize = KeyMaxLen + 1;//因为RegQueryInfoKey得到的长度不包括0结束字符,所以应加1
                char* szKeyName = (char*)malloc(KeySize);
                RegEnumKeyEx(cpp_key, dwIndex, szKeyName, &KeySize, NULL, NULL, NULL, NULL);//枚举子键
                cout << szKeyName << endl;
            }
        }
        else{
            cout << "读取子键失败!" << endl;
        }
    }
    else{
        cout << "打开注册表失败!" << endl;
    }
    RegCloseKey(cpp_key);//关闭句柄
    cout << "---读取子键结束---" << endl;
}
void read_value(){//键值获取
    HKEY cpp_key;
    LPCTSTR cpp_data = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion";
    cout << "---读取键值---" << endl;
    //尝试打开注册表,‘HKEY_LOCAL_MACHINE’要打开的根表;‘cpp_data’要打开的子表项;‘0’固定值;‘KEY_ALL_ACCESS’申请的权限;‘&cpp_key’返回句柄;
    if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE, cpp_data, 0, KEY_READ, &cpp_key)){
        DWORD dwIndex = 0, NameSize, NameCnt, NameMaxLen, Type;
        DWORD KeySize, KeyCnt, KeyMaxLen, DateSize, MaxDateLen;
        if (ERROR_SUCCESS == RegQueryInfoKey(cpp_key, NULL, NULL, 0, &KeyCnt, &KeyMaxLen, NULL, &NameCnt, &NameMaxLen, &MaxDateLen, NULL, NULL)){
            cout << "共有" << NameCnt << "个键值" << endl;
            for (DWORD dwIndex = 0; dwIndex < NameCnt; dwIndex++)    //枚举键值
            {
                DateSize = MaxDateLen + 1;
                NameSize = NameMaxLen + 1;
                char * szValueName = (char *)malloc(NameSize);
                LPBYTE szValueDate = (LPBYTE)malloc(DateSize);
                RegEnumValue(cpp_key, dwIndex, szValueName, &NameSize, NULL, &Type, szValueDate, &DateSize);//读取键值
                cout << "类型:" << Type << " 名称:" << szValueName << " 数据:" << szValueDate << endl;
            }
        }
        else{
            cout << "读取子键失败!" << endl;
        }
    }
    else{
        cout << "打开注册表失败!" << endl;
    }
    RegCloseKey(cpp_key);//关闭句柄
    cout << "---读取键值结束---" << endl;
}

int main(void){
    read_subitem();
    read_value();
    system("pause");
    return 0;
}

转载地址:https://blog.csdn.net/u014101410/article/details/77259886?depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3

posted @ 2020-04-15 01:22  神迹丶  阅读(1127)  评论(0编辑  收藏  举报
网站已运行: