user1784636
user1784636

Reputation: 23

Registry Handling in Cpp

I am working on the code in wherin i have to get the data under the particular regitry path..For this i m using the fnctions to open the key and subkey as RegOpenKeyEx (HKEY_LOCAL_MACHINE, sk, NULL, KEY_READ, &hKey); which on debugging giving me unused value handle to the path i need to access. What is going wrong here ? can anybody tell me ?

void GetAlgorithmList()
{
    HKEY hKey=0;

    LPCTSTR sk = TEXT("SOFTWARE\\ALGORITHM");


    LONG openRes=RegOpenKeyEx (HKEY_LOCAL_MACHINE,sk,NULL,KEY_READ   ,&hKey);

    long lret;
    PVALENT val_list=0;

    unsigned long totalsize = 1000;
    lret = 0;

    LPWSTR szValueBuf=NULL;

    lret = RegQueryMultipleValues(hKey,val_list,totalsize,szValueBuf,&totalsize);
    if (lret == ERROR_SUCCESS)
    {
        printf("Success 1");
    }
     FILE* pFile = fopen("D:\\HinalH\\logFile.txt", "a+");
    fopen("D:\\logFile.txt", "a+");
    fprintf(pFile, "%d\n",szValueBuf);
    fclose(pFile);

    RegCloseKey(hKey);

}

Thanks in advance

Upvotes: 2

Views: 2100

Answers (1)

David J
David J

Reputation: 1564

First: You should check the result of RegOpenKeyEx. Please read the documentation more carefully. Its really necessary to handle errors if they occur at runtime.

Second: Please take a look to the documentation for function RegQueryMultipleValues. Just take your attention to the val_list parameter and to the lpValueBuf parameter both are out parameter. I cannot see that you handle them in the right way in your code.

Third: I cannot find any sample using RegQueryMultipleValues in the web. I played a little arround and created a working example.

Here is the sample I did.

#include <Windows.h>
#include <string>
#include <stdlib.h>

#define MY_KEY TEXT("SYSTEM\\CurrentControlSet\\services\\BITS")

int _tmain(int argc, _TCHAR* argv[])
{
    HKEY hKey;
    LONG lResult;

    lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, MY_KEY, 0, KEY_READ, &hKey);

    if(lResult == ERROR_SUCCESS)
    {
        VALENT val_list[4];
        memset(val_list, 0, sizeof(val_list));

        val_list[0].ve_valuename = TEXT("ImagePath");
        val_list[1].ve_valuename = TEXT("Start");
        val_list[2].ve_valuename = TEXT("DisplayName");
        val_list[3].ve_valuename = TEXT("FailureActions");

        DWORD totalsize = 0;

        RegQueryMultipleValues(hKey, val_list, sizeof(val_list)/sizeof(VALENT), NULL, &totalsize);

        LPWSTR lpBuffer = (LPWSTR)malloc(totalsize);
        if (lpBuffer == NULL)
        {
            // TODO: Error handling
        }

        lResult = RegQueryMultipleValues(hKey, val_list, sizeof(val_list)/sizeof(VALENT), lpBuffer, &totalsize);
        if (lResult == ERROR_SUCCESS)
        {
            for (int i = 0; i < sizeof(val_list)/sizeof(VALENT); i++)
            {
                DWORD len = val_list[i].ve_valuelen;
                DWORD *ptr = (DWORD *)val_list[i].ve_valueptr;

                if (val_list[i].ve_type == REG_SZ || val_list[i].ve_type == REG_EXPAND_SZ)
                {
                    printf("len:%d content:\"%S\"\n", len, ptr);
                }
                else if (val_list[i].ve_type == REG_DWORD)
                {
                    printf("len:%d content:\"%08x\"\n", len, *ptr);
                }
                else if (val_list[i].ve_type == REG_BINARY)
                {
                    printf("len:%d\n", len);
                    for (unsigned k = 0; k < len; k++) printf("%02x ", ((BYTE *)ptr)[k]);
                }
                else
                {
                    // TODO: implement more
                }
            }
        }

        free(lpBuffer);
        RegCloseKey(hKey);
    }
    else
    {
        // TODO: Error handling
    }

    return 0;
}

To be honest for me it seems this function is not the best option to access information in the registry. Better you should use functions like RegQueryValueEx.

Upvotes: 2

Related Questions