Trouble with RegQueryValueEx()
-
I am trying to extract a register value. This is my code:
@
int QEnumerator::getDeviceInfo(HDEVINFO hDevInfo,
QSerialPortInfo *portInfo,
PSP_DEVINFO_DATA portInfoData)
{
QString friendName;
QString phyName;
QString enumName;
QString hardwareID;
QString vid;
QString pid;
QString portName;
QString msg;// Get device information. friendName = getDeviceProperty(hDevInfo, portInfoData, SPDRP_FRIENDLYNAME); portInfo->SetFriendName(friendName); phyName = getDeviceProperty(hDevInfo, portInfoData, SPDRP_PHYSICAL_DEVICE_OBJECT_NAME); portInfo->SetPhyName(phyName); enumName = getDeviceProperty(hDevInfo, portInfoData, SPDRP_ENUMERATOR_NAME); portInfo->SetEnumName(enumName); hardwareID = getDeviceProperty(hDevInfo, portInfoData, SPDRP_HARDWAREID);
-
// This does not appear to work.
// Get the port name from the registry.
HKEY devKey = SetupDiOpenDevRegKey(hDevInfo,
portInfoData,
DICS_FLAG_GLOBAL,
0,
DIREG_DEV,
KEY_READ);
if(NULL != devKey)
{
portName = getRegKeyValue(devKey, (LPCTSTR)("Port Name"));
portInfo->SetPortName(portName);
}
else
QMessageBox::information(NULL, "Reg Key", "No key handle"); -
// Get the PID and VID from the hardware ID.
QRegExp regExpHWID("VID_(\w+)&PID_(\w+)");
if(hardwareID.toUpper().contains(regExpHWID))
{
vid = regExpHWID.cap(1);
portInfo->SetVendorID(vid);
pid = regExpHWID.cap(2);
portInfo->SetProductID(pid);
}return 0;
}
QString QEnumerator::getRegKeyValue(HKEY devKey, LPCTSTR property)
{
// Get the key value of the property.
QString result;
DWORD size = -1;
DWORD type;
DWORD i;
DWORD ret;
QString msg;// Get the size of the returned buffer. RegQueryValueEx(devKey, property, NULL, NULL, NULL, &size); BYTE *buff = new BYTE[size]; msg.sprintf("size = %ld", size); QMessageBox::information(NULL, "Key Value", msg); ret = RegQueryValueEx(devKey, property, NULL, &type, buff, &size); if(ret == ERROR_SUCCESS) { result.clear(); for(i = 0; i < size; i++) { if('\0' == (char)buff[i]) continue; else result.append((char)(buff[i])); } result.append('\0'); } else if(ERROR_MORE_DATA == ret) QMessageBox::information(NULL, "Reg Query", "More data."); else if(ERROR_FILE_NOT_FOUND == ret) QMessageBox::information(NULL, "Reg Query", "File not found"); else if(ERROR_NOACCESS == ret) { QMessageBox::information(NULL, "Reg Query", "No access."); } RegCloseKey(devKey); delete [] buff; return result;
}
@What I get back from the RegQueryValueEx function is ERROR_NOACCESS.
Opening the key with KEY_READ combines KEY_QUERY_VALUE, STANDARD_RIGHTS_READ,
KEY_ENUMERATE_SUB_KEYS and KEY_NOTIFY. I did try using just KEY_QUERY_VALUE with
the same results.I can go into the registry, manually, and find the value I am looking for and it is there.
I'm using Qt4.8.1 in Netbeans 7.2.1.
I'm running Windows Vista Home Premium.Thanks,
DCM -