How do I read Registry Key Value - c++

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?

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

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.

Related

Adding to the registry in C++ win32

I have a batch file that has this line..
REG ADD "HKCU\Software\Google\Chrome\NativeMessagingHosts\SomeName" /ve /t REG_SZ /d "/path/to/SomeName.json" /f
I want to do the same from a c++ code. I found the function RegCreateKeyExA and I am using it like this
HKEY hKey;
LPCSTR sk = "Software\Google\Chrome\NativeMessagingHosts\SomeName";
LONG openRes = RegCreateKeyExA(
HKEY_CURRENT_USER,
sk,
0,
NULL,
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL,
&hKey,
NULL);
but how to add the data part? /ve /t REG_SZ /d "/path/to/SomeName.json" /f
UPDATE:
based on #Alan Britles comment I did the following:
HKEY hKey;
LONG openRes = RegCreateKeyExA(
HKEY_CURRENT_USER,
"HKCU\\Software\\Google\\Chrome\\NativeMessagingHosts\\SomeName",
0,
NULL,
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL,
&hKey,
NULL);
if (openRes == ERROR_SUCCESS) {
log+="Success creating key.";
}
else {
log += "Error creating key.";
}
LPCSTR value = NULL;
std::string temp = QString(data_path + "\\SomeName.json").toStdString();
LPCSTR data = temp.c_str()+'\0';
LONG setRes = RegSetValueExA(hKey, value, 0, REG_SZ, (LPBYTE)data, strlen(data) + 1);
if (setRes == ERROR_SUCCESS) {
log+="Success writing to Registry.";
}
else {
log += "Error writing to Registry.";
}
I get success Writing but I don't see it added in the registry editor... What am I doing wrong?
Update #2:
I got rid of HKCU in the sub key..
LONG openRes = RegCreateKeyExA(
HKEY_CURRENT_USER,
"Software\\Google\\Chrome\\NativeMessagingHosts\\SomeName",
0,
NULL,
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL,
&hKey,
NULL);
But I still get success returns and nothing added to the registry.. Is it a problem related to permissions? Or am I calling the wrong functions?

RegSetValueEx() not changing the key value

In my application, when I first set the key-value using RegSetValueEx() it works, but when I try to change the value using the same function it doesn't work, the value remains same. What am I doing wrong ?
Here is the code :
SetSZValue( "MyAppData", "versionInfo", "1.0.0" );
HKEY CreateKey( string regkey )
{
HKEY hKey ;
DWORD disValue ;
char msg[512] ;
string _key = "HKEY_LOCAL_MACHINE\\" ;
_key += regkey ;
LONG retValue = RegCreateKeyEx( HKEY_LOCAL_MACHINE, regkey.c_str(), 0, 0, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 0, &hKey, &disValue ) ;
if (retValue != ERROR_SUCCESS)
{
FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, 0, GetLastError(), 0, &msg[0], 512, 0 ) ;
MessageBox( 0, &msg[0], "CreateKey", MB_OK | MB_ICONEXCLAMATION );
}
return hKey ;
}
void SetSZValue( string regkey, string keyName, string keyValue )
{
HKEY hKey;
DWORD disValue;
char msg[512];
hKey = CreateKey(regkey);
if (hKey)
{
LONG retValue = RegSetValueEx( hKey, keyName.c_str(), 0, REG_SZ, ( const BYTE* )( keyValue.c_str() ), keyValue.size()+1 );
if ( retValue != ERROR_SUCCESS )
{
FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, 0, GetLastError(), 0, &msg[0], 512, 0 );
MessageBox( 0, &msg[0], "SetSZValue", MB_OK | MB_ICONEXCLAMATION );
}
RegCloseKey ( hKey );
}
}
RegSetValueEx accepts the name of the value inside the key to change; not the name of the key. Supply the value name instead; the key name comes from the HKEY itself.
Is your app a 32-bit process running on a 64-bit Windows version? If so, does your app have a UAC manifest with a "requestedExecutionLevel" value in it? If not, your key may be getting virtualized to another section of the Registry and you are simply not looking in the right place. Registry Virtualization is a feature of WOW64 so legacy 32-bit and 64-bit processes do not step over each other in the Registry. You should install SysInternals Process Monitor, it will show you which keys and values your app is actually accessing.

Create new data in registry key?

I just create new registry key called MyTestApp, and want to add new data in it, but don't know how. So I have following code.
bool CreateSectionDataRegistry(CString sectionData, CString sectionValue)
{
HKEY hKey; //registry key handle
LONG lResult; //result of registry operations
DWORD dwType, dwSize=0;
//try to open the key that we are currently pointing at with rootPath
lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, rootPath + "\\" + "MyTestApp", NULL, KEY_ALL_ACCESS, &hKey);
if (lResult == ERROR_SUCCESS || lResult == ERROR_MORE_DATA)
{
//we have successfully opened the registry key. Now try to access the data
lResult = RegQueryValueEx(HKEY_LOCAL_MACHINE, sectionData, 0, &dwType, NULL, &dwSize);
if(lResult == ERROR_SUCCESS || lResult == ERROR_MORE_DATA)
{
//data already exists, so just return
RegCloseKey(hKey);
return true;
}
else//section data does not exist, so create new data
{
DWORD dwDisposition;
lResult = RegCreateKeyEx(HKEY_LOCAL_MACHINE, sectionData, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, &dwDisposition);
RegCloseKey(hKey);
return true;
}
}
return false;
}
But when I trace down to the RegCreateKeyEx() call, it returns success but data is not created under the MyTestApp registry key or anywhere in the registry. Am I using the correct function to create new data under MyTestApp. Also from the argument above, I have CString sectionValue, so how do I check if I need to create data for REG_SZ or REG_DWORD?
Thanks!
It seems I just need to call RegSetValueEx() to create registry value under MyTestApp. Probably just me but when I look at the RegSetValueEx() definition in Microsoft website, it says set the value, but it does not say if value does not exist, it will create the value.
LPTSTR lpszData = new TCHAR[sectionValue.GetLength() + 1];
_tcscpy(lpszData, sectionValue);
lResult = RegSetValueEx(hKey, sectionData, 0, REG_SZ, (LPBYTE)lpszData, sectionValue.GetLength());
So now it creates the registry value, however I cannot still tell if sectionValue is REG_SZ or REG_DWORD.

How to set a value in windows registry? (C++)

I want to edit key "HKEY_LOCAL_MACHINE\Software\company name\game name\settings\value" to "1" (DWORD)
This is my code:
HKEY hkey;
DWORD dwDisposition;
if(RegCreateKeyEx(HKEY_LOCAL_MACHINE, TEXT("Software\\company name\\game name\\settings"), 0, NULL, 0, 0, NULL, &hkey, &dwDisposition) == ERROR_SUCCESS){
DWORD dwType, dwSize;
dwType = REG_DWORD;
dwSize = sizeof(DWORD);
DWORD rofl = 1;
RegSetValueEx(hkey, TEXT("value"), 0, dwType, (PBYTE)&rofl, dwSize); // does not create anything
RegCloseKey(hkey);
}
But it doesnt do anything. RegCreateKeyEx() is the only function that actually does something: creates the "folders" in the registry only. So once again how im failing? How i can create "files" in the registry?
Always check the return value of API functions. You'll see that RegSetValueEx() returns 5, access denied. You didn't ask for write permission. Fix:
if(RegCreateKeyEx(HKEY_LOCAL_MACHINE,
TEXT("Software\\company name\\game name\\settings"),
0, NULL, 0,
KEY_WRITE, NULL,
&hkey, &dwDisposition) == ERROR_SUCCESS) {
// etc..
}
You probably need to pass in KEY_WRITE as the value of the samDesired arugment to RegCreateKeyEx() function (sixth argument).
This is a copy and edit from an actual code, may contain errors.
LONG
SetRegValue
(
const wchar_t* path
,const wchar_t *name
,const wchar_t *value
)
{
LONG status;
HKEY hKey;
status = RegOpenKeyEx(HKEY_CURRENT_USER, path, 0, KEY_ALL_ACCESS, &hKey);
if ( (status == ERROR_SUCCESS) && (hKey != NULL))
{
status = RegSetValueEx( hKey, name, 0, REG_SZ, (BYTE*)value, ((DWORD)wcslen(value) + 1)*sizeof(wchar_t));
RegCloseKey(hKey);
}
return status;
}

Access is Denied for registry

I am playing with the registry programmatically for the first time, and it's not working that well (but at least I haven't destroyed my computer). Specifically, I keep getting back Error 5 (Access is Denied) from RegCreateKeyEx and RegSetValueEx. The thing that is strangest to me is that when HKEY_CURRENT_USER\Software\dir1\Sub Directory already exists, RegCreateKeyEx fails with Error 5, but when it doesn't already exist, it creates it successfully; and then fails on the RegSetValueEx.
Am I doing anything wrong in this code?
BOOL MyDialog::SaveLocationsToRegistry()
{
HKEY hkey;
DWORD dwDisposition;
DWORD dwType, dwSize;
LONG result = RegCreateKeyEx(HKEY_CURRENT_USER, TEXT("Software\\dir1\\Sub Directory"),
0, NULL, 0, 0, NULL, &hkey, &dwDisposition);
if(result == ERROR_SUCCESS)
{
LPCTSTR szLastFolder = "C:\\Documents and Settings\\user\\My Documents\\Copy of item\0";
dwType = REG_SZ;
dwSize = strlen(szLastFolder)+1;
LONG setResult = RegSetValueEx(hkey, TEXT("LastFolder"), 0, dwType,
(PBYTE)&szLastFolder, dwSize);
RegCloseKey(hkey);
return setResult == ERROR_SUCCESS;
}
else
{
return false;
}
}
Note: The absolute path is only there temporarily. Baby steps ;-)
You're not asking for any access rights. You probably want to specify KEY_WRITE (or something) for the 6th parameter (samDesired).
LONG result = RegCreateKeyEx(HKEY_CURRENT_USER, TEXT("Software\\dir1\\Sub Directory"),
0, NULL, 0, KEY_WRITE, NULL, &hkey, &dwDisposition);