Std :: string in LPBYTE and RegEnumValueA
How can I use std :: string for LPBYTE to make this code work?
string Name;
string Value;
RegEnumValueA(hKey, 0, const_cast<char*>(Name.c_str()), &dwSize, NULL, NULL, (LPBYTE)const_cast<char*>(Value.c_str()), &dwSize2);
When I try to use this code, each one is okey with a string name, but there is an error with an invalid pointer in the string value
source to share
The correct way to get std::string
with the data you want is
//alloc buffers on stack
char buff1[1024];
char buff2[1024];
//prepare size variables
DWORD size1=sizeof(buff1);
DWORD size2=sizeof(buff2);
//call
RegEnumValueA(hKey, 0, buff1, &size1, NULL, NULL, (LPBYTE)buff2, &size2);
//"cast" to std::string
std::string Name(buff1);
std::string Value(buff2);
source to share
Pointer parameters must point to valid buffers to be modified by the function. c_str()
the unified string does not indicate anything valid.
Use char buffers instead of string
s. This is a very good case where const_cast <> is completely unclaimed.
char Name[200], Value[200]; //Sizes are arbitrary
DWORD dwSize = sizeof(Name), dwSize2 = sizeof(Value);
RegEnumValueA(hKey, 0, Name, &dwSize, NULL, NULL, (LPBYTE)Value, &dwSize2);
source to share
If you really want to "distinguish" std::string
as you said, you can consider the following piece of code. It should work on whatever implementation std::string
I know of, but it's still a bit "hacky" and I wouldn't recommend it.
string Name(1024, ' ');
string Value(1024, ' ');
DWORD dwSize=Name.size();
DWORD dwSize2=Value.size();
RegEnumValueA(hKey, 0, const_cast<char*>(&Name[0]), &dwSize, NULL, NULL, (LPBYTE)const_cast<char*>(&Value[0]), &dwSize2);
source to share