Why does the RegQueryValueEx () function return ERROR_FILE_NOT_FOUND when trying to read from a registry key?

System: Windows 7 32bit
Language: C ++

I tried to access the register HKEY_LOCAL_MACHINE\\HARDWARE\\DEVICEMAP\\Scsi\\Scsi Port 0 , key Driver (type REG_SZ ) - not a problem.

The same is for reading from HKEY_LOCAL_MACHINE\\HARDWARE\\DEVICEMAP\\SERIALCOMM , all keys ( REG_SZ types) have slashes, for example \Device\Serial0 .

When reading such keys, it always returns 2 (there is no such file) with the following code example:

 HKEY hKey = 0; DWORD dwType = REG_SZ; char buf[255] = {0}; DWORD dwBufSize = sizeof(buf); 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 --- CUT --- 

What is the correct way to read key values ​​using slashes in a name?


The above was correct answered by Cody Gray.
Below is another problem.

I get the same problem when I use a variable instead of a text string.
Iv considered both single and double slash approaches:

 HKEY hKey = 0; DWORD keyType = REG_SZ; TCHAR buf[255] = {0}; DWORD bufSize = sizeof(buf); QSettings winReg("HKEY_LOCAL_MACHINE\\HARDWARE\\DEVICEMAP\\SERIALCOMM", QSettings::NativeFormat); auto comsKey = winReg.allKeys(); FOREACH( auto com, comsKey ) { // FOREACH - boost macro // comsKey = QList<QString> (list of key names) [from Qt framework] // com = QString (single key name) [from Qt framework] if( RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("HARDWARE\\DEVICEMAP\\SERIALCOMM"), 0, KEY_QUERY_VALUE, &hKey ) == ERROR_SUCCESS ) { wchar_t* keyw = new wchar_t(); //com.replace("/", "\\\\"); <- checked both variants commented and not commented; com == /Device/Serial0 so im converting to \\Device\\Serial0 int size = com.size(); mbstowcs( keyw, com.toStdString().data(), size ); //auto ret = RegQueryValueEx( hKey, TEXT("\\Device\\Serial0"), 0, &keyType, (LPBYTE)buf, &bufSize ); // <- this works! auto ret = RegQueryValueExW( hKey, (LPCWSTR)&keyw, 0, &keyType, (LPBYTE)buf, &bufSize ); // <- this one not works! 

I tried all the options with "\ Device ..", "/ Device", "\ Device", etc.

+6
source share
2 answers

You need to avoid slashes, as 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 do not, the RegQueryValueEx function RegQueryValueEx not find the specified key and returns ERROR_FILE_NOT_FOUND (== 2).


But there is another problem. You should declare the buffer array as type wchar_t (or TCHAR ), not char :

 TCHAR buf[255] = {0}; 

Otherwise, the RegQueryValueEx function will try to fill the array with a Unicode string read from the specified registry key, and you will get something unreadable.

+7
source
 using System; using System.IO.Ports; namespace SerialPortExample { class SerialPortExample { public static void Main() { // Get a list of serial port names. string[] ports = SerialPort.GetPortNames(); Console.WriteLine("The following serial ports were found:"); // Display each port name to the console. foreach(string port in ports) { Console.WriteLine(port); } Console.ReadLine(); } } } 

See http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.getportnames(v = vs .110) .aspx

0
source

Source: https://habr.com/ru/post/904234/


All Articles