Thread: Read Registry value

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    29

    Read Registry value

    I got a problem with reading a value from the registry.Ive check MSDN what function to use, and i tried RegGetValue, RegQueryalueEx, but RegGetValue is declared, and RegQueryValue returns something what is not right.What function do i need to use?

    This is the piece of code

    Code:
    HKEY key;
        DWORD a;
        HKEY buffer;
        DWORD iNumGet=512;
        char bufGet[512];
    
        int OpenKey = MessageBox(
                        NULL,
                        "Do you want to check if the key's are there?",
                        "Open/Write Key",
                        MB_YESNO
                      );
        if (OpenKey == IDYES) {
            RegOpenKeyEx(
                key,
                "Software\\Nephiroth",
                0,
                NULL,
                &buffer
            );
            
    
            RegQueryValueEx(
                key, 
                "Test", 
                0,
                NULL,
                (BYTE *)bufGet,
                &iNumGet
            );
    
            RegCloseKey(key);
            if (bufGet == "texst") {
                MessageBox(
                    NULL,
                    "works",
                    "test",
                    MB_OK
                );
            } else {
                MessageBox(
                    NULL,
                    "Nope",
                    "test",
                    MB_OK
                );
            }
    } else if (OpenKey == IDNO) {
    // not needed stuff here
    }
    The registry looks like this schematic

    HKEY_LOCAL_MACHINE
    ->Software
    ->Nephiroth
    -"Test" with the value "texst"

    Please help me on this one

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Maybe it has something to do with key being uninitialised.
    Y'know, these functions return fairly detailed error information for those bothered enough to look at it.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    29
    I dont really need the error information, i need the function to use to read a value.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > I got a problem with reading a value from the registry
    > I dont really need the error information,
    When you've resolved your logical inconsistencies - come back with some proper code.

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    29
    The problem is that i dont know the function to use

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    The problem is, you don't initialise variables
    > HKEY key;
    Which key were you planning to read with this statement?

    Perhaps if you'd bothered to look at the error messages, you might have seen RegOpenKeyEx return (I guess) something like "unknown key".

    Here, start reading
    http://msdn.microsoft.com/library/de...gopenkeyex.asp

    > if (bufGet == "texst")
    Also, to compare strings, you need to use strcmp()

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    29
    Okey, ive done a couple of things...
    Fixed the HKEY key to HKEY key = HKEY_LOCAL_MACHINE
    Added return check
    and used strcmp() with the string comparison, thanks
    i got this now

    Code:
    if (OpenKey == IDYES) {
            if (RegOpenKeyEx(
                key,
                "Software\\Nephiroth",
                0,
                NULL,
                &buffer
            ) == ERROR_ACCESS_DENIED) {
                MessageBox(
                    NULL,
                    "There was an error, opening the registry key",
                    "Error",
                    MB_ICONERROR|MB_OK
                );
            }
            
            RegQueryValueEx(key, "Test", 0, NULL, (BYTE *)bufGet, &iNumGet);
    
            RegCloseKey(key);
            if (strcmp (bufGet,"texst") == 0) {
                MessageBox(
                    NULL,
                    "works",
                    "test",
                    MB_OK
                );
            } else {
                MessageBox(
                    NULL,
                    "Nope",
                    "test",
                    MB_OK
                );
            }
           
    
        } else if (OpenKey == IDNO) {
         //stuff here
    }
    I did some testing with the return values, and there comes out ACCES DENIED.Kinda weird actually, as i have made a key before.

    EDIT:
    Added KEY_READ and KEY_QUERY_VALUE as parameters..no errors now.But isnt giving right answer
    Last edited by Nephiroth; 01-28-2006 at 03:44 PM.

  8. #8
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Code:
     HKEY hKey;
        char szTest[512] = {0};
        DWORD datasize;
        if(RegOpenKeyEx (HKEY_LOCAL_MACHINE, "Software\\Nephiroth", 0, KEY_QUERY_VALUE,
            &hKey) == ERROR_SUCCESS)
        {
            datasize = sizeof (szTest);
            if (RegQueryValueEx(hKey, "Test",NULL, NULL, (unsigned char *)&szTest,
                (unsigned long *)&datasize) == ERROR_SUCCESS)
                printf("RegQueryValueEx returned %s\n", szTest);
            else
                printf("RegQueryValueEx failed\n");
            RegCloseKey(hKey); 
        }
        else printf("RegOpenKeyEx failed\n");

  9. #9
    Registered User
    Join Date
    Jan 2006
    Posts
    29
    great i used parts of your method to figure it all out.Got it working now, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. I am lost on how to read from file and output to file?
    By vicvic2477 in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2005, 11:52 AM
  4. Read Array pro!!Plz help!!
    By Supra in forum C Programming
    Replies: 2
    Last Post: 03-04-2002, 03:49 PM
  5. Help! Can't read decimal number
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 09-07-2001, 02:09 AM