Get USB bus reported device description in qt
-
how can we get USB bus reported device description in Qt?
I searched in google and saw some functions like SetupDiGetDeviceProperty in windows.h or maybe setupapi.h. but i don't know how can use it in Qt.@vakh
As you say, this doubtless is not available from Qt libraries and you need to call some Windows SDK function.Qt/C++ programs can call Windows SDK functions like any other function which happens to be written in C. Just make sure:
-
You
#include
the necessary Windows.h
file, e.g. maybe#include <windows.h>
. Qt may already be including this, I don't know. -
You link with the necessary Windows
.lib
files. Again I suspect Qt may already be doing this.
However I see SetupDiGetDevicePropertyW function (setupapi.h) is in
setupapi.h
and that is inside Device and Driver Installation Reference. I do not know whether that is available in a normal, non-driver-installation program. -
-
thanks a lot. I finally found how to use this library. this is my code:
#include <string> #include <windows.h> #include <setupapi.h> #include <initguid.h> #include <devguid.h> #include <hidsdi.h> uint8_t valid_ports_counter =0; // Check Device Bus Reported Device Description HDEVINFO hDevInfo; SP_DEVINFO_DATA DeviceInfoData = { sizeof(DeviceInfoData) }; // Get device class information handle hDevInfo = SetupDiGetClassDevs(&GUID_DEVCLASS_PORTS, 0, 0, DIGCF_PRESENT); if (hDevInfo != INVALID_HANDLE_VALUE) { const int BUFFER_SIZE = 1024; BYTE ptrBuf[BUFFER_SIZE]; static DEVPROPKEY *DEVPKEY_Device_BusReportedDeviceDesc; DEVPKEY_Device_BusReportedDeviceDesc = new DEVPROPKEY(); DEVPKEY_Device_BusReportedDeviceDesc->fmtid.Data1 = 0x540b947e; DEVPKEY_Device_BusReportedDeviceDesc->fmtid.Data2 = 0x8b40; DEVPKEY_Device_BusReportedDeviceDesc->fmtid.Data3 = 0x45bc; DEVPKEY_Device_BusReportedDeviceDesc->fmtid.Data4[0] = 0xa8; DEVPKEY_Device_BusReportedDeviceDesc->fmtid.Data4[1] = 0xa2; DEVPKEY_Device_BusReportedDeviceDesc->fmtid.Data4[2] = 0x6a; DEVPKEY_Device_BusReportedDeviceDesc->fmtid.Data4[3] = 0x0b; DEVPKEY_Device_BusReportedDeviceDesc->fmtid.Data4[4] = 0x89; DEVPKEY_Device_BusReportedDeviceDesc->fmtid.Data4[5] = 0x4c; DEVPKEY_Device_BusReportedDeviceDesc->fmtid.Data4[6] = 0xbd; DEVPKEY_Device_BusReportedDeviceDesc->fmtid.Data4[7] = 0xa2; DEVPKEY_Device_BusReportedDeviceDesc->pid = 4; DEVPROPTYPE propRegDataType; // enumerute device information for (int i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++) { DWORD req_bufsize = 0; if (SetupDiGetDevicePropertyW(hDevInfo, &DeviceInfoData, DEVPKEY_Device_BusReportedDeviceDesc, &propRegDataType, ptrBuf, BUFFER_SIZE, &req_bufsize, 0)) { // BYTE to char QString port_name; for(uint16_t i = 0; i < (req_bufsize-2); i += 2) port_name.push_back(static_cast<QChar>(ptrBuf[i])); if(port_name == "###") { DWORD DataT; char friendly_name[2046] = { 0 }; DWORD buffersize = 2046; // get device description information if (SetupDiGetDeviceRegistryPropertyA(hDevInfo, &DeviceInfoData, SPDRP_FRIENDLYNAME, &DataT, (PBYTE)friendly_name, buffersize, &req_bufsize)) { QString com_port = friendly_name; com_port = com_port.sliced(com_port.indexOf('(') + 1, com_port.indexOf(')') - com_port.indexOf('(') - 1); valid_ports_counter++; COMInfo comi; comi.type = COM_MCB; comi.name = com_port; ports_type.push_back(comi); ui->comboBox_ports->addItem(com_port); QPixmap pusb(":/files/USB.png"); QIcon iusb(pusb); ui->comboBox_ports->setItemIcon(ui->comboBox_ports->count() - 1, iusb); } } } } }
-