Why does the RegQueryValueEx() function return ERROR_FILE_NOT_FOUND while trying to read from a registry key?
You have to escape the slashes, just like you did in the first line...
if( RegOpenKeyEx( HKEY_LOCAL_MACHINE, TEXT("HARDWARE\\DEVICEMAP\\SERIALCOMM"), 0, KEY_QUERY_VALUE, &hKey ) == ERROR_SUCCESS ) { auto ret = RegQueryValueEx( hKey, TEXT("\\Device\\Serial0"), 0, &dwType, (LPBYTE)buf, &dwBufSize ); // ret always == 2 for key with slashes
If you don't, the RegQueryValueEx
function can't find the specified key, and it returns ERROR_FILE_NOT_FOUND
(== 2).
But there's another problem. You should be declaring the buffer array as type wchar_t
(or TCHAR
), rather than char
:
TCHAR buf[255] = {0};
Otherwise, the RegQueryValueEx
function is going to attempt to fill the array with a Unicode string read from the specified registry key, and you're going to get something unreadable.