Using this code snippet:
TCHAR buf[255] = {0};
DWORD dwBufSize = sizeof(buf);
HKEY hKey = 0;
DWORD dwType = REG_DWORD;
if ( RegOpenKeyEx( HKEY_CURRENT_USER, TEXT("Software\\Developer\\KGReader"), 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS)
{
if( RegQueryValueEx( HKEY_CURRENT_USER, TEXT("Software\\Developer\\KGReader\\ComPort"), 0, &dwType, (LPBYTE)buf, &dwBufSize ) == ERROR_SUCCESS )
{
nrPort = _wtoi(buf);
}
if( RegQueryValueEx( HKEY_CURRENT_USER, TEXT("Software\\Developer\\KGReader\\KGID"), 0, &dwType, (LPBYTE)buf, &dwBufSize ) == ERROR_SUCCESS )
{
nrCentrala = _wtoi(buf);
}
}
- No value is passed to
hKeyusingRegOpenKeyExmethod. - Although
RegOpenKeyExreturns an ERROR_SUCCES,RegQueryValueExreturns 2 (ERROR_FILE_NOT_FOUND).KGIDandComPortdo exist,i added them myself, i double checked the paths.
Pass the
hKeyobtained from theRegOpenKeyEx()call to theRegQueryValueEx()functions instead ofHKEY_CURRENT_USER. The second argument toRegQueryValueEx()is the name of the value, not a path.For example:
Remember to reset
bufanddwBufSizebefore the second call toRegQueryValueEx:For error reporting purposes you should save the return value of the
RegOpenKeyEx()andRegQueryValueEx()functions so the failure reason can be made known.The posted code may be incomplete but ensure
RegCloseKey(hKey)is called if the call toRegOpenKeyEx()was successful.