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 );
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.