简体   繁体   中英

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...], ie 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. Ie 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();
 }
}

查看WideCharToMultiByte ()。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM