Sunny Milenov
Sunny Milenov

Reputation: 22310

C++ read registry string value in char*

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

Upvotes: 1

Views: 2167

Answers (2)

Alain Rist
Alain Rist

Reputation: 837

You have two solutions here:

  1. Change in your code to wchar_t mydata[2048];. As the underlying code is UNICODE you will get the best performance.
  2. Use RegQueryValueExA() if performance in this area is not your concern.

Upvotes: 2

Ferruccio
Ferruccio

Reputation: 100718

Look at WideCharToMultiByte().

Upvotes: 3

Related Questions