您的位置:首页 > 其它

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: