Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass std::string value to RegSetValueEx() in C++

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()?

like image 838
Raul Lam Avatar asked Jan 03 '17 08:01

Raul Lam


People also ask

How to pass a string to a function in C?

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.

How many examples of regsetvalueex are there in the real world?

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.

How to read a string in C++?

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; }

What is string in C with example?

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.


1 Answers

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.

like image 130
Cody Gray Avatar answered Oct 01 '22 07:10

Cody Gray