03 .注册表

//#include "../Dll1/dll.h"
#include <stdio.h>
#include <windows.h>
#include <strsafe.h>
#include <iostream>
using namespace std;

void ShowError(){
    HANDLE hBuff=nullptr;
    DWORD dwErr = GetLastError();
    FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
        nullptr, dwErr, 0, (PTSTR)&hBuff, 0, nullptr);
    MessageBox(nullptr, (LPCWSTR)hBuff, nullptr, 0);
    
}

void EnumRegister(HKEY hKey) {
    TCHAR szSubKey[MAXBYTE] = {};
    DWORD cchSubKey = MAXBYTE;
    DWORD nSubKeyCount = 0;
    DWORD nValueCount = 0;

    //子键和项的个数
    RegQueryInfoKey(hKey, nullptr, nullptr, nullptr,
        &nSubKeyCount, nullptr, nullptr, &nValueCount, nullptr, nullptr, nullptr, nullptr);


    for (DWORD i = 0; i < nSubKeyCount; i++) {

        //遍历子键名
        cchSubKey = MAXBYTE;        
        RegEnumKeyEx(hKey, i, szSubKey, &cchSubKey, nullptr, nullptr, nullptr, nullptr);
        wcout << szSubKey << endl;

        

        /*HKEY hSubKey = nullptr;
        RegOpenKeyEx(hKey, szSubKey, 0, KEY_ALL_ACCESS, &hSubKey);
        RegQueryInfoKey(hSubKey, nullptr, nullptr, nullptr,
            &nSubKeyCount, nullptr, nullptr, &nValueCount, nullptr, nullptr, nullptr, nullptr);*/
    }

    TCHAR szValue[MAXBYTE] = {};
    TCHAR szData[MAXBYTE] = {};        
    DWORD dwType = 0;
    for (DWORD i = 0; i < nValueCount; i++) {
        DWORD cchValue = MAXBYTE;
        DWORD cbData = MAXBYTE * sizeof(TCHAR);

        RegEnumValue(hKey, i, szValue, &cchValue, nullptr, &dwType, (LPBYTE)szData, &cbData);
        switch (dwType) {
        case REG_DWORD: 
            wcout << szValue << " = " << *(DWORD*)szData << endl;
            break;
        
        case REG_QWORD: 
            wcout << szValue << " = " << *(DWORD64 *)szData << endl;
            break;
        
        case REG_SZ:
            wcout << szValue << " = " << szData << endl;
            break;
        
        }
    }
}

int main() { 
    setlocale(LC_ALL, "zh-cn");

    //创建或打开均可
    HKEY hKey = nullptr;
    LSTATUS nResult = RegCreateKeyEx(HKEY_CURRENT_USER, TEXT("SOFTWARE\\2345.com"), 0, nullptr,
        REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, nullptr, &hKey, nullptr);
    if (nResult  != ERROR_SUCCESS) {
        ShowError();
    }

    ////设置数据
    //int nAge = 22;
    //nResult = RegSetValueEx(hKey, TEXT("Age"), 0, REG_DWORD, (BYTE *)&nAge, sizeof(nAge));
    //if (nResult != ERROR_SUCCESS) {
    //    ShowError();
    //}

    //读取数据
    DWORD dwType = 0;
    TCHAR szBuff[MAXBYTE] = {};
    DWORD dwBuffSize = MAXBYTE;

    //读取数据
    //RegGetValue(hKey, nullptr, TEXT("Age"), RRF_RT_ANY, nullptr, szBuff, &dwBuffSize);
   
    //读取数据
    //RegQueryValueEx(hKey, TEXT("Age"), nullptr, nullptr, (LPBYTE)szBuff, &dwBuffSize);

    //删除数据
    //RegDeleteKeyEx(HKEY_CURRENT_USER, TEXT("SOFTWARE\\CR37"), KEY_WOW64_32KEY, 0);

    EnumRegister(hKey);
    


    RegCloseKey(hKey);






    return 0;
}

  

posted @ 2020-07-23 00:31  八转达人  阅读(141)  评论(0编辑  收藏  举报