Reputation: 571
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);
}
}
hKey
using RegOpenKeyEx
method.RegOpenKeyEx
returns an ERROR_SUCCES, RegQueryValueEx
returns 2 (ERROR_FILE_NOT_FOUND). KGID
and ComPort
do exist,i added them myself, i double checked the paths.Upvotes: 0
Views: 1592
Reputation: 122001
Pass the hKey
obtained from the RegOpenKeyEx()
call to the RegQueryValueEx()
functions instead of HKEY_CURRENT_USER
. The second argument to RegQueryValueEx()
is the name of the value, not a path.
For example:
if( RegQueryValueEx(hKey,
TEXT("ComPort"),
0,
&dwType,
(LPBYTE)buf,
&dwBufSize ) == ERROR_SUCCESS )
Remember to reset buf
and dwBufSize
before the second call to RegQueryValueEx
:
memset(buf, 0, sizeof(buf));
dwBufSize = sizeof(buf);
For error reporting purposes you should save the return value of the RegOpenKeyEx()
and RegQueryValueEx()
functions so the failure reason can be made known.
The posted code may be incomplete but ensure RegCloseKey(hKey)
is called if the call to RegOpenKeyEx()
was successful.
Upvotes: 4
Reputation: 30590
The first argument to RegQueryValueEx
should be the HKEY
opened by RegOpenKeyEx
. In addition, the second argument should just be a value name, not the full path:
if( RegQueryValueEx( hKey, TEXT("ComPort"), 0, &dwType,
(LPBYTE)buf, &dwBufSize ) == ERROR_SUCCESS )
Upvotes: 1