Registry RegQueryKeyEx

Hey,
I have recenly begun using the Win32 API on XP pro. I need to read the value of a registry key(data value is a file location or string in REG_SZ format)
As i am not too familiar with data types and how to convert them I am stuck at getting this string(Using RegQueryKeyEx) and then converting it from LPBYTE to LPCSTR to display in the MessageBox function. The function definition says something about a buffer, im lost.
Thanks


Answer this question

Registry RegQueryKeyEx

  • Lawrence Parker

    Thanks goes well .
    Is buffer like a character array

    e.g char bob[5] = "hello"

  • Stephan Smetsers

    Thanks for the quick reply. I am still getting the following errors:
    Main.cpp(44) : error C2664: 'MessageBoxA' : cannot convert parameter 2 from 'char' to 'const char *'
    Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast


    error C2440: 'initializing' : cannot convert from 'char *' to 'char'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    (This is for the line TCHAR buffer = new TCHAR[bufSize];)

    error C2541: delete : cannot delete objects that are not pointers

  • ScoobySue

    If you are stuck at the concept of a buffer, I'd suggest you buy or lend a good book on C++ before diving much more. That will surely save you some time later.

    With regard to RegQueryValueEx, what you'd generally want to do is call it once to get the type and size of the data stored at it, and also get a sign if it even exists.

    DWORD bufSize = 0;
    DWORD valueType = 0;
    LONG result;
    
    result = RegQueryValueEx(hkey, _T("valuename"), NULL, &valueType, NULL, &bufSize);
    
    if (result != ERROR_SUCCESS)
    {
     // the value may not exist, or the key is invalid
     return;
    }

    Next, you would like to confirm that the value is actually of a proper type, and that it has a sane size. If everything is ok, RegQueryValueEx will be called a second time to actually get the value. If this call also succeeds, show the result in a message box.

    if ((valueType == REG_SZ || valueType == REG_EXPAND_SZ) && 
      (bufSize > 0))
    {
     TCHAR* buffer = new TCHAR[bufSize / sizeof(TCHAR)];
     
     result = RegQueryValueEx(hkey, _T("valuename"), NULL, NULL, (LPBYTE)buffer, &bufSize);
     
     if (result == ERROR_SUCCESS)
     {
      MessageBox(0, buffer, _T("The value"), MB_SETFOREGROUND);
     }
     
     delete [] buffer;
    }

    Last but not least, free the memory allocated to hold the data.

    One thing I havent touched here, is the fact that strings located in the registry aren't guaranteed to have a terminating null character -- that part is left for you to figure out. An alternative would be to use RegGetValue, which is certain to return null-terminated strings.



  • Matt Stum

    Sorry, a missing * in that would-be pointer declaration. That's what I get from relying on my built-in compiler

    I've updated the previous post now.



  • Registry RegQueryKeyEx