📄 新建 文本文档.txt
字号:
GUID hidGuid;
HidD_GetHidGuid (&hidGuid);
HDEVINFO deviceInfo = SetupDiGetClassDevs ( &hidGuid, 0, 0, DIGCF_PRESENT |
DIGCF_INTERFACEDEVICE));
if ( !deviceInfo ) return m_deviceCount;
// Setup data for HID devices enumeration:
BOOL isSuccess = FALSE; // Note: You must use BOOL...NOT...***bool***with WIN32 API calls;
unsigned long bytes = 0;
SP_INTERFACE_DEVICE_DATA deviceData;
memset( &deviceData, 0, sizeof(SP_INTERFACE_DEVICE_DATA) );
deviceData.cbSize = sizeof (SP_INTERFACE_DEVICE_DATA);
PSP_INTERFACE_DEVICE_DETAIL_DATA deviceInterfaceData = 0;
size_t size = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
for ( int i = 0; i < PT_MAX_USB_DEVICES; i++ )
{
// Get each HID device attached...we will ignore those
isSuccess = SetupDiEnumInterfaceDevice ( deviceInfo, 0, &hidGuid,
i,&deviceData);
if ( !isSuccess )
{
if ( ERROR_NO_MORE_ITEMS == GetLastError() )
break;
else
continue;
}
// Get specified device interface information.
// Note: We call this 2x. The first time is to acquire the amount
of memory we will need to store the device info in.
SetupDiGetInterfaceDeviceDetail( deviceInfo, &deviceData, 0, 0,
&bytes, 0);
deviceInterfaceData = (PSP_INTERFACE_DEVICE_DETAIL_DATA) new BYTE[bytes]; // Create the device interface data structure...
if ( !deviceInterfaceData )
{
outOfMemory();
SetupDiDestroyDeviceInfoList( deviceInfo );
return m_deviceCount;
}
memset( deviceInterfaceData, 0, bytes );
deviceInterfaceData->cbSize = size; // Retrieve the device
interface information:
isSuccess = SetupDiGetInterfaceDeviceDetail ( deviceInfo,
&deviceData, deviceInterfaceData, bytes, &bytes, 0);
if ( !isSuccess )
{
reportLastError( __FILE__, __LINE__ );
delete [] (BYTE*)deviceInterfaceData;
SetupDiDestroyDeviceInfoList( deviceInfo );
return m_deviceCount;
}
// Open device for both read and write...
HANDLE hidDevice = CreateFile( deviceInterfaceData->DevicePath,
//communication port name
GENERIC_READ | GENERIC_WRITE,
//read/write types
FILE_SHARE_READ | FILE_SHARE_WRITE,
//comm devices must be opened with exclusive access
0,
//no security attributes
OPEN_EXISTING,
//communication devices must use OPEN_EXISTING
0,
//Sync I/O
0);
if ( INVALID_HANDLE_VALUE == hidDevice)
{
delete [] (BYTE*)deviceInterfaceData;
continue;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -