I am writing a program to edit the windows registry key by C++, but when I try to pass a string value to library function RegSetValueEx(), there is a file start with TEXT() which could only be hardcode value in it.
Parts of my code:
string region;
string excelserver_type;
string keyname = region + excelserver_type;
if (RegSetValueEx(key64, TEXT("XXXXXXXXX"), 0, REG_SZ, (LPBYTE)TEXT("XXXXXXXXXX"), 100) != ERROR_SUCCESS)
{
RegCloseKey(key);
cout << "Unable to set registry value in HKEY_LOCAL_MACHINE\\Software" << endl;
}
When I try to replace "XXXXXXXX" by keyname, it gives me an error. How do I pass value of keyname in RegSetValueEx()?
Strings can be passed to a function in a similar way as arrays. Learn more about passing arrays to a function. Similar like arrays, string names are "decayed" to pointers. Hence, you can use pointers to manipulate elements of the string. We recommended you to check C Arrays and Pointers before you check this example.
C++ (Cpp) RegSetValueEx - 30 examples found. These are the top rated real world C++ (Cpp) examples of RegSetValueEx extracted from open source projects. You can rate examples to help us improve the quality of examples.
You can use the scanf () function to read a string. The scanf () function reads the sequence of characters until it encounters whitespace (space, newline, tab, etc.). Example 1: scanf () to read a string #include <stdio.h> int main() { char name ; printf("Enter name: "); scanf("%s", name); printf("Your name is %s.", name); return 0; }
In C, array of characters is called a string. A string is terminated with a null character \0. For example: "c string tutorial". Here, c string tutorial is a string. When compiler encounter strings, it appends a null character \0 at the end of the string.
You need to use the std::wstring
type instead. This will give you a wide (Unicode) string, based on the wchar_t
type, which is how Windows stores strings internally.
Then, you can simply use the c_str()
member function to retrieve a pointer to a C-style string, and pass this directly to the RegSetValueEx
function. The size()
member function gives you the length of the string, which you can pass as the cbData
parameter, except for two caveats:
cbData
expects the length of the string to include the terminating NUL character, so you will need to add 1 to the length returned by size()
.cbData
expects the size of the string in bytes, not the number of characters, so for a wide string, you will need to multiply the value returned by size()
by the length of a wchar_t
.bool SetStringValue(HKEY hRegistryKey,
const std::wstring& valueName,
const std::wstring& data)
{
assert(hRegistryKey != nullptr);
return (RegSetValueExW(hRegistryKey,
valueName.c_str(),
0,
REG_SZ,
(LPBYTE)(data.c_str()),
(data.size() + 1) * sizeof(wchar_t)) == ERROR_SUCCESS);
}
If you absolutely have to use narrow (ANSI) strings (and you shouldn't, because you're interfacing directly with the operating system here, not working with user data), you can do the same thing but explicitly call the ANSI version of RegSetValueEx
, which has an A
suffix. Here, you still need to add 1 to the length, but the size in bytes is equivalent to the number of characters, so no scaling is necessary.
bool SetStringValue_ANSI(HKEY hRegistryKey,
const std::string& valueName,
const std::string& data)
{
assert(hRegistryKey != nullptr);
return (RegSetValueExA(hRegistryKey,
valueName.c_str(),
0,
REG_SZ,
(LPBYTE)(data.c_str()),
data.size() + 1) == ERROR_SUCCESS);
}
Internally, RegSetValueExA
will convert the string to Unicode and then perform the same task as RegSetValueExW
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With