The Windows Registry 12

 

 

 

 

 

Querying the Registry Value Program Example

 

An application typically calls RegEnumValue() to determine the value names and then RegQueryValueEx() to retrieve the data for the names. If the data has the REG_SZ, REG_MULTI_SZ or REG_EXPAND_SZ type, the string may not have been stored with the proper terminating null characters. Therefore, even if the function returns ERROR_SUCCESS, the application should ensure that the string is properly terminated before using it; otherwise, it may overwrite a buffer. (Note that REG_MULTI_SZ strings should have two terminating null characters.) One way an application can ensure that the string is properly terminated is to use RegGetValue(), which adds terminating null characters if needed. If the data has the REG_SZ, REG_MULTI_SZ or REG_EXPAND_SZ type, and the ANSI version of this function is used (either by explicitly calling RegQueryValueExA() or by not defining UNICODE before including the Windows.h file), this function converts the stored Unicode string to an ANSI string before copying it to the buffer pointed to by lpData. When calling the RegQueryValueEx() function with hKey set to the HKEY_PERFORMANCE_DATA handle and a value string of a specified object, the returned data structure sometimes has unrequested objects. Do not be surprised; this is normal behavior. When calling the RegQueryValueEx() function, you should always expect to walk the returned data structure to look for the requested object. Note that operations that access certain registry keys are redirected. Ensure that you reinitialize the value pointed to by the lpcbData parameter each time you call this function. This is very important when you call this function in a loop, as in the following code example.

Create a new empty Win32 console application project. Give a suitable project name and change the project location if needed.

Then, add the source file and give it a suitable name.

Next, add the following source code.

 

#include <windows.h>

#include <malloc.h>

#include <stdio.h>

 

#define TOTALBYTES    8192

#define BYTEINCREMENT 4096

 

int wmain(int argc, WCHAR *argv[])

{

    DWORD BufferSize = TOTALBYTES;

    DWORD cbData;

    DWORD dwRet;

    PPERF_DATA_BLOCK PerfData = (PPERF_DATA_BLOCK) malloc(BufferSize);

    cbData = BufferSize;

 

    wprintf(L"\nRetrieving the data.  ");

 

      // Using predefined registry key that always open

      // The name of the registry value is "mike spoon"

      //  If the function succeeds, the return value is ERROR_SUCCESS

      //  If the function fails, the return value is a system error code

      //  If the lpData buffer is too small to receive the data, the function returns ERROR_MORE_DATA

      //  If the lpValueName registry value does not exist, the function returns ERROR_FILE_NOT_FOUND.

    dwRet = RegQueryValueEx(HKEY_PERFORMANCE_DATA,

                             L"Global",

                             NULL,

                             NULL,

                             (LPBYTE)PerfData,

                             &cbData);

 

    while(dwRet == ERROR_MORE_DATA)

    {

            wprintf(L"\nRegQueryValueEx() returns %u, need more buffer!", dwRet);

        // Get a buffer that is big enough

        BufferSize += BYTEINCREMENT;

        PerfData = (PPERF_DATA_BLOCK)realloc(PerfData, BufferSize);

        cbData = BufferSize;

 

        dwRet = RegQueryValueEx(HKEY_PERFORMANCE_DATA,

                         L"Global",

                         NULL,

                         NULL,

                         (LPBYTE) PerfData,

                         &cbData);

      }

 

      wprintf(L"\nProvided buffer is enough...\n");

     

      if(dwRet == ERROR_FILE_NOT_FOUND)

      {

            // This part failed although providing the non-exist

            // registry value name. It goes to the next 'if'

            wprintf(L"\nRegQueryValueEx() returns %u", dwRet);

            wprintf(L"\nRegistry value does not exist!\n");

      }

    else if(dwRet == ERROR_SUCCESS)

      {

        wprintf(L"\nRegQueryValueEx() returns %u", dwRet);         

        wprintf(L"\nFinal buffer size is %d\n", BufferSize);

            // wprintf(L"\nRegistry value found!");

            // wprintf(L"\nPerfData->Version: %u",PerfData->Version);

            // wprintf(L"\nPerfData->TotalByteLength: %u",PerfData->TotalByteLength);

            // wprintf(L"\nPerfData->SystemNameLength: %u",PerfData->SystemNameLength);

            // wprintf(L"\nPerfData->Revision: %u\n",PerfData->Revision);

      }

    else

            wprintf(L"\nRegQueryValueEx() failed, error code %u\n", dwRet);

 

      return 0;

}

 

Build and run the project. The following screenshots are sample outputs.

 

Querying the Registry Value Program Example: A sample console program output in action 1

 

Querying the Registry Value Program Example: A sample console program output in action 2

 

The following is another loose and simple code example.

Create a new empty Win32 console application project. Give a suitable project name and change the project location if needed.

Then, add the source file and give it a suitable name.

Next, add the following source code.

 

#include <windows.h>

#include <stdio.h>

 

int wmain(int argc, WCHAR **argv)

{

      DWORD dwData;

      HKEY hKey;

      LONG returnStatus;

      DWORD dwType = REG_DWORD;

      DWORD dwSize = sizeof(DWORD)*2;

 

    returnStatus = RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\ASP.NET", 0L, KEY_ALL_ACCESS, &hKey);

 

    if (returnStatus  == ERROR_SUCCESS)

    {

        wprintf(L"RegOpenKeyEx() is OK!\n");

        returnStatus = RegQueryValueEx(hKey, L"DefaultDocInstalled", NULL, &dwType, (LPBYTE)&dwData, &dwSize);

 

        if (returnStatus == ERROR_SUCCESS)

        {

                  wprintf(L"RegQueryValueEx() is OK!\n");

                  wprintf(L"REG_DWORD value is 0X%.8X\n", dwData);

        }

            else

                  wprintf(L"RegQueryValueEx() failed, error %u\n", GetLastError());

     }

      else

            wprintf(L"RegOpenKeyEx() failed, error %u\n", GetLastError());

 

    if(RegCloseKey(hKey) == ERROR_SUCCESS)

            wprintf(L"Closing the hKey handle...\n");

 

      return 0;

}

 

Build and run the project. The following screenshot is a sample output.

 

Querying the Registry Value Program Example: A sample console program output in action 3

 

 

 

 

< Windows Registry 11 | Windows Registry Index | Win32 Programming | Windows Registry 13 >