views:

34

answers:

2

I am Creating the Registry Key using following code:

   LPCTSTR  lpNDS= TEXT("SOFTWARE\\myKEY");
    if(OK==ERROR_SUCCESS)
    {
            MessageBox ( NULL, "Success", 
                             _T("SimpleShlExt"),
                            MB_ICONINFORMATION );
    }
    else
    {
        MessageBox ( NULL, "Failed" ,
                             _T("SimpleShlExt"),
                            MB_ICONINFORMATION );
    }

        LONG openRes = RegCreateKeyEx(
                    HKEY_LOCAL_MACHINE,
                    lpNDS,
                    0,
                    NULL,
                    REG_OPTION_NON_VOLATILE,
                    KEY_ALL_ACCESS,
                    NULL,
                    &hRegKey,
                    NULL);

         if (openRes!=ERROR_SUCCESS) 
                MessageBox ( NULL, "Registry Createion Failed", 
                             _T("SimpleShlExt"),
                            MB_ICONINFORMATION );

Now I am writing up to the Key using:

CSimpleShlExt::WriteToRegistry(LPSTR lpRegKeyVal)
{
        LONG setRes = RegSetValueEx (hRegKey, "NDS", 0, REG_SZ, (LPBYTE)lpRegKeyVal, strlen(lpRegKeyVal)+1);
        CloseRegistryKey();

}

Now I am trying read the registry key value that I have created using WriteToRegistry function. I have tried with

RegOpenKeyEx(hRegKey,lpNDS,0,KEY_QUERY_VALUE,&hRegKey);

But this is failing.

Which function shall I use to read the value contained inside the key?

A: 

Which function shall I use to read the value contained inside the key?

Try RegQueryValue and/or RegEnumValue. Read the documentation to see which one suits your needs the best.

dirkgently
+1  A: 

You can try something like :

TCHAR szValue[TEMP_STR_SIZE] = {0};
DWORD dwStorageSize = TEMP_STR_SIZE;
LPBYTE lpStorage = reinterpret_cast<LPBYTE>(szValue);

// In case of registry value retrieval failure
if (ERROR_SUCCESS != RegQueryValueEx(hRegKey, _T("NDS"), 0, 0, lpStorage, &dwStorageSize))
{
    // Prompt error
}

Edit:
Just noticed there are no subkey, changed my sample code

YeenFei
@YeenFei, Now How will I get the Value of the RegistryKey, In My Case , I am writing some value to NDS. *lpStorage is an Integer. Sorry for being so simple but I am just new to this
Subhen
Small edit which worked for me and may be upadted in answer:
Subhen
if (ERROR_SUCCESS == RegQueryValueEx(hRegKey, _T("NDS"), 0, 0, (BYTE*)szValue, }
Subhen