#ifndef USBInterfH#define USBInterfH//---------------------------------------------------------------------------#include <setupapi.h>#include <QString>class TUSBio{public:QString *DevicePathList;HANDLE NewHandle;private: HDEVINFO DeviceList; DWORD NextDeviceID; int __fastcall GetDevicePath(const GUID &DeviceGuid, int DeviceIndex, QString &Path); GUID DeviceGuid;public: __fastcall TUSBio(); virtual __fastcall ~TUSBio();//===================== Поиск устройств =====================================// bool __fastcall Scan(const GUID &ADeviceGuid);//===========================================================================////============= Открытие выбранного устройства из списка ====================// bool _fastcall Open(int index);//===========================================================================////======================== Закрытие устройства ==============================// void _fastcall Close();//===========================================================================///*****************************************************************************//*****************************************************************************//*****************************************************************************///===========================================================================//};//---------------------------------------------------------------------------#endif
//---------------------------------------------------------------------------#include "USBInterf.h"#include <setupapi.h>#include <qt_windows.h>#include <windows.h>#include "ECE02061_XPioctl.h"#include <string>//---------------------------------------------------------------------------//---------------------------------------------------------------------------//---------------------------------------------------------------------------TUSBio::TUSBio(){ DevicePathList = new QString(); NewHandle = INVALID_HANDLE_VALUE;}//---------------------------------------------------------------------------TUSBio::~TUSBio(){ delete (DevicePathList);}//---------------------------------------------------------------------------int TUSBio::GetDevicePath(const GUID &DeviceGuid, int DeviceIndex, QString &Path){ bool BOOLRes; DWORD DWORDRes; SP_DEVICE_INTERFACE_DATA DeviceData; DeviceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA); BOOLRes = SetupDiEnumDeviceInterfaces(DeviceList, NULL, &DeviceGuid, DeviceIndex, &DeviceData); if (!BOOLRes) { return GetLastError(); } DWORD RequiredSize = 0; SetupDiGetDeviceInterfaceDetail(DeviceList, &DeviceData, NULL, 0, &RequiredSize, NULL); if ( RequiredSize==0) return GetLastError(); SP_DEVICE_INTERFACE_DETAIL_DATA *DeviceInterfaceDetail; DeviceInterfaceDetail = (SP_DEVICE_INTERFACE_DETAIL_DATA*) new char[RequiredSize]; DeviceInterfaceDetail->cbSize = 5; BOOLRes = SetupDiGetDeviceInterfaceDetail(DeviceList, &DeviceData, DeviceInterfaceDetail, RequiredSize, &RequiredSize, NULL); if (BOOLRes) Path = DeviceInterfaceDetail->DevicePath[0]; delete ((char*)DeviceInterfaceDetail); if (!BOOLRes) GetLastError(); return 0;}//---------------------------------------------------------------------------//////поиск устройствbool TUSBio::Scan(const GUID &ADeviceGuid){ DevicePathList->clear(); DeviceGuid = ADeviceGuid; bool BOOLRes = false; DWORD DWORDRes; SetupDiDestroyDeviceInfoList(DeviceList); DeviceList = NULL; DeviceList = SetupDiGetClassDevs(&DeviceGuid, NULL, NULL, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT); if (DeviceList!=INVALID_HANDLE_VALUE && DeviceList!=NULL) { QString DevicePath; int i=0; for (int i=0;; i++) { DWORDRes = GetDevicePath(DeviceGuid, i, DevicePath); if (DWORDRes==ERROR_NO_MORE_ITEMS) { BOOLRes = true; break; } if (DWORDRes!=0) { SetLastError(DWORDRes); break; } DevicePathList->append(GetDevicePath(DeviceGuid, i, DevicePath)); } } return BOOLRes;}
for (int i=0;i < 32;i++)
C++ (Qt)int TUSBio::GetDevicePath(const GUID &DeviceGuid, int DeviceIndex, QString &Path){ ... SP_DEVICE_INTERFACE_DETAIL_DATA *DeviceInterfaceDetail; DeviceInterfaceDetail = (SP_DEVICE_INTERFACE_DETAIL_DATA*) new char[RequiredSize]; DeviceInterfaceDetail->cbSize = 5; BOOLRes = SetupDiGetDeviceInterfaceDetail(DeviceList, &DeviceData, DeviceInterfaceDetail, RequiredSize, &RequiredSize, NULL); if (BOOLRes) Path = DeviceInterfaceDetail->DevicePath[0]; delete ((char*)DeviceInterfaceDetail); if (!BOOLRes) GetLastError(); return 0;}
C++ (Qt)int TUSBio::GetDevicePath(const GUID &DeviceGuid, int DeviceIndex, QString &Path){ ... SP_DEVICE_INTERFACE_DETAIL_DATA DeviceInterfaceDetail; DeviceInterfaceDetail.cbSize = 5; BOOLRes = SetupDiGetDeviceInterfaceDetail(DeviceList, &DeviceData, &DeviceInterfaceDetail, RequiredSize, &RequiredSize, NULL); if (!BOOLRes) return GetLastError(); Path = DeviceInterfaceDetail->DevicePath[0]; return 0;}
C++ (Qt)for( int i = 0; i < 32; i++ ){ i = 0;}
C++ (Qt)while( true )
C++ (Qt)for( int i = 0; ; i++ )