How to write/read a string in windows registry in C++ ?
I am able to write/read DWORD (number) in windows registry using the following code. But, unable to write/read a string value, as it gets stored as chinese like characters in the registry.
void SetVal(HKEY hKey, LPCTSTR lpValue, DWORD data)
{
LONG nError = RegSetValueEx(hKey, lpValue, NULL, REG_DWORD, (LPBYTE)&data, sizeof(DWORD));
if (nError)
cout << "Error: " << nError << " Could not set registry value: " << (char*)lpValue << endl;
}
DWORD GetVal(HKEY hKey, LPCTSTR lpValue)
{
DWORD data; DWORD size = sizeof(data); DWORD type = REG_DWORD;
LONG nError = RegQueryValueEx(hKey, lpValue, NULL, &type, (LPBYTE)&data, &size);
if (nError==ERROR_FILE_NOT_FOUND)
data = 0; // The value will be created and set to data next time SetVal() is called.
else if (nError)
cout << "Error: " << nError << " Could not get registry value " << (char*)lpValue << endl;
return data;
}
Code used to write/read string value (Stored as chinese like characters in the registry):
void SetVal(HKEY hKey, LPCTSTR lpValue, string data)
{
LONG nError = RegSetValueEx(hKey, lpValue, NULL, REG_SZ, (LPBYTE)&data, sizeof(data));
if (nError)
cout << "Error: " << nError << " Could not set registry value: " << (char*)lpValue << endl;
}
string GetVal(HKEY hKey, LPCTSTR lpValue)
{
string data; DWORD size = sizeof(data); DWORD type = REG_SZ;
LONG nError = RegQueryValueEx(hKey, lpValue, NULL, &type, (LPBYTE)&data, &size);
if (nError==ERROR_FILE_NOT_FOUND)
data = "0"; // The value will be created and set to data next time SetVal() is called.
else if (nError)
cout << "Error: " << nError << " Could not get registry value " << (char*)lpValue << endl;
return data;
}
The problem with your code is that you are naively casting string
type variables to LPBYTE
.
This is your code with corrections:
void SetVal(HKEY hKey, LPCTSTR lpValue, string data)
{
const char *x = data.c_str();
LONG nError = RegSetValueEx(hKey, lpValue, NULL, REG_SZ, (LPBYTE)data.c_str(), data.size());
if (nError)
cout << "Error: " << nError << " Could not set registry value: " << (char*)lpValue << endl;
}
string GetVal(HKEY hKey, LPCTSTR lpValue)
{
string data;
#define MAXLENGTH 100
char buffer[100];
DWORD size = sizeof(buffer);
DWORD type = REG_SZ;
LONG nError = RegQueryValueEx(hKey, lpValue, NULL, &type, (LPBYTE)buffer, &size);
if (nError == ERROR_FILE_NOT_FOUND)
{
data = "0"; // The value will be created and set to data next time SetVal() is called.
return data;
}
else if (nError)
cout << "Error: " << nError << " Could not get registry value " << (char*)lpValue << endl;
data = buffer;
return data;
}
There is still room for improvement, for example the maximum string length is limited to 100 and the error handling should be improved.
Explanations:
This is your code in SetVal
RegSetValueEx(hKey, lpValue, NULL, REG_DWORD, (LPBYTE)&data, sizeof(DWORD));
data
to LPBYTE
. sizeof(DWORD)
is wrong, because you need to provide the length of the string This is your code in GetVal
:
string data;
DWORD size = sizeof(data);
DWORD type = REG_SZ;
LONG nError = RegQueryValueEx(hKey, lpValue, NULL, &type, (LPBYTE)&data, &size);
sizeof(data)
when data
is a string
doesn't make any sense. It is not the length of the string, which at that moment is 0 anyway. string
to LPBYTE
when calling RegQueryValueEx
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.