Hi all, I tried to read a REG_BINARY value of the Windows registry, but I don't know how... I'm really new to the c++ world and I hope that you'll be cool and help me with that problem.
I found that code on this website, I know this is not doing the job, but just for let you know what I'm trying to do.
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
HKEY hKey;
DWORD dwDisp = REG_BINARY;
DWORD dwSize = sizeof(dwDisp);
DWORD dwValue = 0;
DWORD dwReturn;
DWORD dwBufSize = sizeof(dwDisp);
if( RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion",0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS)
{
DWORD error = RegQueryValueEx(hKey,"DigitalProductId",0,0, (LPBYTE)&dwReturn, &dwBufSize);
if(error == ERROR_SUCCESS)
{
cout << "Key value is :" << dwReturn << endl;
}
else
{
cout << "Cannot query for key value; Error is : " << error << endl;
}
}
RegCloseKey(hKey);
return 0;
}
P.S. I'm using gcc without .net.
Thanks for your help it will be very appreciated.
Thanks all! There is the fully functional code:
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
HKEY hKey;
DWORD dwBufSize = 200;
UCHAR dwReturn[200];
UCHAR digits[] = {'B','C','D','F','G','H','J','K','M','P','Q','R','T','V','W','X','Y','2','3','4','6','7','8','9'};
UCHAR strresult[26];
if( RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion",0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS)
{
DWORD error = RegQueryValueEx(hKey,"DigitalProductId",0,0, (LPBYTE)dwReturn, &dwBufSize);
if(error == ERROR_SUCCESS)
{
for (int i=24;i>=0;i--) {
int x=0;
for (int j=14;j>=0;j--) {
x = (x<<8) + (dwReturn+0x34)[j];
(dwReturn+0x34)[j] = x / 24;
x = x % 24;
}
strresult[i]=digits[x];
}
string part1, part2, part3, part4, part5;
string str;
string strRetVal;
for(int i = 0; strresult[i] != 0; i++)
{
str += strresult[i];
}
part1 = str.substr(0,5) + "-";
part2 = str.substr(5,5) + "-";
part3 = str.substr(10,5) + "-";
part4 = str.substr(15,5) + "-";
part5 = str.substr(20,5);
strRetVal = part1 + part2 + part3 + part4 + part5;
cout << "Windows Key : " << strRetVal << endl;
}
else
{
cout << "Cannot query for key value; Error is : " << error << ", dwBufSize="<<dwBufSize<<endl;
}
}
RegCloseKey(hKey);
return 0;
}