I’m new to C++ and to WinCe developing.
I want to read a string from the registry and display with the MessageBox(). I have tried the following.
HKEY key;
if (::RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("System\\CurrentControlSet\\GPS Intermediate Driver\\Drivers\\SiRFStar3HW"), 0, KEY_READ, &key) != ERROR_SUCCESS)
{
MessageBox(NULL,L"Can't open the registry!",L"Error",MB_OK);
}
char value[5];
DWORD value_length=5;
DWORD type=REG_SZ;
RegQueryValueEx(key,(LPCTSTR)"Baud", NULL, &type, (LPBYTE)&value, &value_length);
wchar_t buffer[5];
_stprintf(buffer, _T("%i"), value);
::MessageBox(NULL,buffer,L"Value:",MB_OK);
::RegCloseKey(key);
So I know somethings wrong in here, but how can I solve?
Navigating the Win32 API can be a tricky business. The registry APIs are some of the more complicated. Here’s a short program to demonstrate how to read a registry string.
Notes:
std::wstring. This makes string handling a cinch.RegQueryValueExreturnsREG_SZdata that is null-terminated. This code deals with that by truncating beyond the first null character. In case the value returned is not null-terminated, that truncation won’t happen, but the value will still be fine.MessageBox. Like this:MessageBox(0, value.c_str(), L"Caption", MB_OK)