tags:

views:

114

answers:

2

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;
}
A: 

When you query a value, you can't tell Windows that the value's type is binary. You query the value, Windows will tell you what the type is (and what the value is), and you have to provide some pointers to tell Windows where to put the information that it will tell you.

Windows programmer
+1  A: 

There are two problems with the code you posted - the buffer for the binary value is too small and KEY_ALL_ACCESS could be too much to ask for, KEY_QUERY_VALUE is enough.

Here is the code that has this mistakes corrected. You can change dwReturn[1000] to dwReturn[1] and see that RegQueryValueEx returns an error and the required buffer size.

int main()
{
    HKEY hKey;
    DWORD dwReturn[1000];  //dwReturn[1]
    DWORD dwBufSize = sizeof(dwReturn);

    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)
        {
            cout << "dwReturn[0]=" << dwReturn[0] << ", dwBufSize=" << dwBufSize <<endl;
        }
        else
        {
           cout << "Cannot query for key value; Error is : " << error << ", dwBufSize="<<dwBufSize<<endl;
        }
    }

    RegCloseKey(hKey);

    return 0;
}

Note that in the case of "DigitalProductId" the first DWORD contains the length so dwReturn[0] and dwBufSize are the same.

Tanks so much man! That's what I needed. I added my complete functionally code in my first post, if you want to see.
geek1983