VC获取usb设备信息
2014-12-31 16:25
393 查看
#include <windows.h> #include <stdio.h> #include <Shlwapi.h> #include <conio.h> extern "C" { // Declare the C libraries used #include <setupapi.h> // Must link in setupapi.lib #include <hidsdi.h> // Must link in hid.lib } static /*const*/ GUID GUID_DEVINTERFACE_USB_DEVICE = { 0xA5DCBF10L, 0x6530, 0x11D2, { 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED } }; TCHAR * GetErrString(TCHAR *str, DWORD errcode) { LPVOID lpbuf; if(FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errcode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpbuf, 0, NULL )) { lstrcpy(str,(LPCWSTR)lpbuf); LocalFree(lpbuf); } return str; } int main(int argc, char* argv[]) { char szTraceBuf[256]; // Get device interface info set handle for all devices attached to system HDEVINFO hDevInfo = SetupDiGetClassDevs( &GUID_DEVINTERFACE_USB_DEVICE, /* CONST GUID * ClassGuid - USB class GUID */ NULL, /* PCTSTR Enumerator */ NULL, /* HWND hwndParent */ DIGCF_PRESENT | DIGCF_DEVICEINTERFACE /* DWORD Flags */ ); if (hDevInfo == INVALID_HANDLE_VALUE) { sprintf(szTraceBuf, "SetupDiClassDevs() failed. GetLastError() " \ "returns: 0x%x\n", GetLastError()); OutputDebugStringA(szTraceBuf); return 1; } sprintf(szTraceBuf, "Device info set handle for all devices attached to " \ "system: 0x%x\n", hDevInfo); OutputDebugStringA(szTraceBuf); // Retrieve a context structure for a device interface of a device // information set. DWORD dwIndex = 0; SP_DEVICE_INTERFACE_DATA devInterfaceData; ZeroMemory(&devInterfaceData, sizeof(SP_DEVICE_INTERFACE_DATA)); devInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA); BOOL bRet = FALSE; ULONG neededLength,requiredLength; PSP_DEVICE_INTERFACE_DETAIL_DATA ClassDeviceData; HIDD_ATTRIBUTES attributes; while(TRUE) { bRet = SetupDiEnumDeviceInterfaces( hDevInfo, /* HDEVINFO DeviceInfoSet */ NULL, /* PSP_DEVINFO_DATA DeviceInfoData */ &GUID_DEVINTERFACE_USB_DEVICE, /* CONST GUID * InterfaceClassGuid */ dwIndex, &devInterfaceData /* PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData */ ); if (!bRet) { TCHAR buffer[1024]; TCHAR szTraceBuf[1024]; GetErrString(buffer,GetLastError()); wsprintf(szTraceBuf, L"SetupDiEnumDeviceInterfaces failed msg:%s",buffer); OutputDebugStringW(szTraceBuf); if (GetLastError() == ERROR_NO_MORE_ITEMS) { break; } }else{ // 发现一个HID设备,获取设备的详细信息 // 第一次调用SetupDiGetDeviceInterfaceDetail得到ClassDeviceData的大小,但返回错误 SetupDiGetDeviceInterfaceDetail(hDevInfo,&devInterfaceData, NULL,0,&requiredLength,NULL); neededLength =requiredLength; ClassDeviceData =(PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(neededLength); ClassDeviceData-> cbSize =sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); //第二次调用SetupDiGetDeviceInterfaceDetail // 使用 合适的neededLength. if (!SetupDiGetDeviceInterfaceDetail(hDevInfo,&devInterfaceData, ClassDeviceData,neededLength,&requiredLength,NULL)) { free(ClassDeviceData); SetupDiDestroyDeviceInfoList(hDevInfo); return -1; } // 建立HID设备的句柄 HANDLE handle=CreateFile(ClassDeviceData-> DevicePath, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,OPEN_EXISTING,0,NULL); // 获取 attributes 以便得到Vendor ID 和 Product ID HidD_GetAttributes(handle,&attributes); TCHAR buffer[1024]; wsprintf(buffer,L"name:%s pid=%d vid=%d version=%d \n",ClassDeviceData-> DevicePath,attributes.ProductID,attributes.VendorID,attributes.VersionNumber); OutputDebugStringW(buffer); CloseHandle(handle); free(ClassDeviceData); } dwIndex++; } sprintf(szTraceBuf, "Number of device interface sets representing all " \ "devices attached to system: %d\n", dwIndex); OutputDebugStringA(szTraceBuf); SetupDiDestroyDeviceInfoList(hDevInfo); getch(); return 0; }
相关文章推荐
- 【VC】 获取电脑USB设备VID和PID等信息。
- VC 获取USB设备的ID号
- Windows 下获取USB设备信息的通用浏览器插件实现
- vc通过usb获取手机设备vidpid
- Windows下USB磁盘开发系列三:枚举系统中U盘、并获取其设备信息
- 请问如何获取车载USB设备信息(名称等)
- linux c 下获取USB设备的信息并禁止使用USB设备的实现
- VC中实现用WMI获取设备信息
- 获取电脑USB设备VID和PID等信息。
- 在VC中如何获得USB设备的Product ID,Vendor ID等信息
- Android USB API获取设备信息失败问题
- vc获取windows设备枚举设备信息
- Qt 获取usb设备信息 hacking
- VC获取设备信息
- Windows下USB磁盘开发系列三:枚举系统中U盘、并获取其设备信息
- 获取USB设备的信息
- 在VC中如何获得USB设备的Product ID,Vendor ID等信息
- VC++获取串口设备信息,CPU ID,硬盘ID,网卡MAC等机器硬件信息,用于加密
- 获取 usb 信息 尝试
- vc++实现控制USB设备启用与否