Help with RegQueryValueEx

This is a discussion on Help with RegQueryValueEx within the C Programming forums, part of the General Programming Boards category; I'll try to read com port value from the register with following code. I don't get a value , could ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    3

    Help with RegQueryValueEx

    I'll try to read com port value from the register with following code.
    I don't get a value , could somebody help?

    --------------------------
    Code:
        HKEY    myKey;
        BYTE*   valData;
        DWORD   valDataSize;
         
    	if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, "HARDWARE\\DEVICEMAP\\SERIALCOMM\0", 0, KEY_QUERY_VALUE, &myKey)
            != ERROR_SUCCESS)
        {
            printf ("\nCould not open key.");
           
        }   
        valData = (BYTE*)malloc (4);
        valDataSize = 1024;
        if (RegQueryValueEx (myKey, "Device\\Serial0\0", NULL, NULL, valData, &valDataSize)
            != ERROR_SUCCESS)
        {
            printf ("\nCould not query integer value.");
            
        }  
        printf("\nHistoryListLength: %d", *valData);    
            
    	
        if (RegQueryValueEx (myKey, "Device\\Serial0\0", NULL, NULL, NULL, &valDataSize) 
    	!= ERROR_SUCCESS)
        {
            printf ("\nCould not query string size.");
           
        } 
        
        
        valData = (BYTE*)realloc (valData, valDataSize);
        if (RegQueryValueEx (myKey, "Device\\Serial0\0", NULL, NULL, valData, &valDataSize) 
    	!= ERROR_SUCCESS) 
        {
            printf ("\nCould not query string value.");
            
        }
        printf ("\nUserName: %s", valData);
        
        
    
        free (valData);
        RegCloseKey (myKey);

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And can you describe how it goes wrong, whether you get an error message or just "bad data"?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > valData = (BYTE*)malloc (4);
    > valDataSize = 1024;
    How is allocating 4 bytes, then saying you've got 1024 bytes going to help?
    That's 1020 bytes trashed over someone else's memory.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    3
    Quote Originally Posted by Salem View Post
    > valData = (BYTE*)malloc (4);
    > valDataSize = 1024;
    How is allocating 4 bytes, then saying you've got 1024 bytes going to help?
    That's 1020 bytes trashed over someone else's memory.
    Sorry, right value is 4, that 1024 is just typo.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    How can it be a typo, unless that isn't your real code.

    If it isn't your real code, then you're wasting everyone's time.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    3
    Typo,blackout, whatever when I wrote it. I'm terrible sorry.

    Like John Lennon sang:

    I didn't mean to hurt you
    I'm sorry that I made you cry...

  7. #7
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Looks like your subkey might be wrong:

    This

    Code:
    if (RegQueryValueEx (myKey, "Device\\Serial0\0", NULL, NULL, NULL, &valDataSize) 
    	!= ERROR_SUCCESS)
        {
            printf ("\nCould not query string size.");
           
        }
    should be this
    Code:
    if (RegQueryValueEx (myKey, "\\Device\\Serial0\0", NULL, NULL, NULL, &valDataSize) 
    	!= ERROR_SUCCESS)
        {
            printf ("\nCould not query string size.");
           
        }
    EDIT You're also calling RegQueryValueEx one too many times. It should only be called twice. The first time to get actual size of the character array needed. And the second time to get the actual data.
    Last edited by BobS0327; 01-08-2008 at 07:29 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. RegQueryValueEx ERROR
    By koyboy in forum Windows Programming
    Replies: 11
    Last Post: 05-12-2008, 01:14 PM
  2. RegQueryValueEx Doesn't return data
    By willc0de4food in forum Windows Programming
    Replies: 8
    Last Post: 10-22-2007, 09:41 AM
  3. RegQueryValueEx problem
    By BobS0327 in forum Windows Programming
    Replies: 4
    Last Post: 10-08-2006, 08:11 PM
  4. RegQueryValueEx Giving error 234
    By Narcose in forum C Programming
    Replies: 4
    Last Post: 07-14-2006, 10:16 AM
  5. RegQueryValueEx
    By ipe in forum Windows Programming
    Replies: 2
    Last Post: 09-06-2003, 05:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21