views:

275

answers:

2

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();
 }
}
+3  A: 

Look at WideCharToMultiByte().

Ferruccio
The code I used: WideCharToMultiByte(CP_NONE, 0, (LPCWSTR)keyval, -1, ascii, asciil, NULL, NULL);
Sunny
Unless you really need to build your project with the Character Set property set to Unicode (the default under VS2008), Maurizio's solution is probably better.
Ferruccio
+2  A: 

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. cheers, AR
Alain Rist
Doh! headslap for not thinking of #2, so obvious...
Ferruccio