Friday, March 19, 2010

C++ read registry string value in char*

Programmer Question

I'm reading a registry value like this:



char mydata[2048];
DWORD dataLength = sizeof(mydata);
DWORD dwType = REG_SZ;

..... open key, etc
ReqQueryValueEx(hKey, keyName, 0, &dwType, (BYTE*)mydata, &dataLength);


My problem is, that after this, mydata content looks like: [63, 00, 3A, 00, 5C, 00...], i.e. this looks like a unicode?!?!.



I need to convert this somehow to be a normal char array, without these [00], as they fail a simple logging function I have. I.e. if I call like this: WriteMessage(mydata), it outputs only "c", which is the first char in the registry. I have calls to this logging function all over the place, so I'd better of not modify it, but somehow "fix" the registry value. Here is the log function:



void Logger::WriteMessage(const char *msg)
{
time_t now = time(0);
struct tm* tm = localtime(&now);
std::ofstream logFile;

logFile.open(filename, std::ios::out | std::ios::app);

if ( logFile.is_open() )
{
logFile << tm->tm_mon << '/' << tm->tm_mday << '/' << tm->tm_year << ' ';
logFile << tm->tm_hour << ':' << tm->tm_min << ':' << tm->tm_sec << "> ";
logFile << msg << "\n";
logFile.close();
}
}


Find the answer here

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails