I have application which reads key in registry entry.
Its working good on Multi-Byte Character Set
But On Unicode character set it cannot open key
This is my code:
HKEY hkey = 0;
char buf[255] = {0};
DWORD dwType = 0;
DWORD dwBufSize = sizeof(buf);
const char* subkey_x64 = "SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\UCP";
const char* subkey_x86 = "SOWTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\UCP";
if (RegOpenKey(HKEY_LOCAL_MACHINE, (LPCWSTR)subkey_x64, &hkey) == ERROR_SUCCESS)
{
dwType = REG_SZ;
if (RegQueryValueEx(hkey, (LPCWSTR)"DisplayIcon", 0, &dwType, (BYTE*)buf, &dwBufSize) == ERROR_SUCCESS)
{
cout << "Key Value IS: " << buf << endl;
}
else
{
cout << "Cannot get key value..." << endl << endl;
}
}
else
{
cout << "Cannot open key\n" << endl << endl;
RegCloseKey(HKEY_PERFORMANCE_DATA);
}
Does anybody have an idea why it cannot open key?
(LPCWSTR) is not good approach?
Im using visual studio 2010
const char*is not an Unicode string. Change toconst wchar_t*and you won’t need a cast.With the cast you say the compiler to interpret that address as a pointer to an Unicode string but it’s not.
Moreover when you declare an Unicode string literal you should use
Lprefix:L"DisplayIcon"(again no need for a cast). If your code may run both on Unicode and multi-byte you may use theTEXT()macro to do the trick for you (or its short version_T()):_T("DisplayIcon").