Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1000425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:30:38+00:00 2026-05-16T07:30:38+00:00

I tried to read a REG_BINARY value of the Windows registry, but I don’t

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T07:30:39+00:00Added an answer on May 16, 2026 at 7:30 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't have much of a socket programming experience but I tried read a
I already read/tried these posts but that didn't help: Display multiple lines within a
I tried to read the User Names which are in the Active Directory, but
Possible Duplicate: read/write to Windows Registry using Java I'm trying to run this cmd
I tried to read the related questions and didn't find any new tool. Here
I am new to WPF, even though I have tried to read as much
Hello I am trying to read and write a value to the registry using
I tried to read signature row of atmega32 with boot_signature_byte_get(0); but i get this
I've tried to read up on a few articles about this, but some of
I've tried to read http://www.w3.org/TR/xml-infoset/ and the wikipedia entry . But frankly I'm still

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.