CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2007
    Location
    France
    Posts
    329

    Can read REG_SZ but not REG_DWORD value

    Hi,

    I can read REG_SZ values but not REG_DWORD values from the registry.

    Do I need another type than char* as 5th parameter of the RegQueryValueEx() function if I want to read REG_DWORD value
    or something else is the problem?

    Code:
    #include <windows.h>
    #include <winreg.h>
    #include <iostream>
    using namespace std;
    
    int main()
    {
        HKEY hKey;
        char Data[255];
        DWORD Datasize = sizeof(Data);
        DWORD Type;
    
        if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
                        "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion",
                        0,
                        KEY_QUERY_VALUE|KEY_WOW64_32KEY,
                        &hKey)== ERROR_SUCCESS)
    
        {
            DWORD error = RegQueryValueEx(hKey,
                            "InstallDate",
                            NULL,
                            &Type,
                            (LPBYTE)&Data,
                            &Datasize);
    
            if(error == ERROR_SUCCESS)
            {
                cout << " Data = " << Data <<
                     "\n Size = " << Datasize <<
                     "\n Type = " << Type <<endl;
            
            }
            else
            {
               cout << "RegQueryValueEx error " << error << endl;
            }
    
        }
        else cout << "RegopenkeyEx error " << error << endl;
    
        RegCloseKey(hKey);
    
        return 0;
    }

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Can read REG_SZ but not REG_DWORD value

    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Dec 2007
    Location
    France
    Posts
    329

    Re: Can read REG_SZ but not REG_DWORD value

    Thank you Kaud!

    I managed to read other REG_DWORD values by modifying the code with the help of your link but this specific key returns me 0.

    Edit: but on another machine its working so it must be something else than the code.
    Last edited by MasterDucky; September 25th, 2015 at 06:34 AM.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Can read REG_SZ but not REG_DWORD value

    Check out the CRegKey class in msdn - all that registry stuff is done for you.

  5. #5
    Join Date
    Dec 2007
    Location
    France
    Posts
    329

    Re: Can read REG_SZ but not REG_DWORD value

    Great, thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured