Windows API 获取MachineGUID

MachineGUID在注册表HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography\中。
使用WindowsAPI获取,需要用到RegOpenKeyA()RegQueryValueExA(),和RegCloseKey()这三个函数

代码如下:

#include 
#include 
#include 
int main()
{
    std::string sub_key = "SOFTWARE\\Microsoft\\Cryptography";
    std::string name = "MachineGuid";
    HKEY hKey;
    DWORD dwType = REG_SZ;
    DWORD dwLen = MAX_PATH;
    if (RegOpenKeyA(HKEY_LOCAL_MACHINE, sub_key.c_str(), &hKey) == ERROR_SUCCESS){
        std::cout<<"ok\n";
    }
    unsigned char buf[100];
    PLONG data = 0;
    if(RegQueryValueExA(hKey, name.c_str() ,0 ,&dwType, (LPBYTE)buf,  &dwLen) == ERROR_SUCCESS){
        std::cout<<"ok\n";
    }
    else std::cout<<"GetLastError() = "<<GetLastError()<<std::endl;
    std::cout<<buf<<std::endl;
    return 0;
}

你可能感兴趣的:(笔记,windows,c++)