怎样将计算机连接到usb设备,怎样获得已联接到计算机的USB设备的列表?

// DeviceManager.cpp : 定义控制台应用程序的入口点。

#include stdio.h

#include stdlib.h

#include windows.h

#include setupapi.h

#define DIRECTINPUT_VERSION 0x0800

#include Dinput.h

extern C

{

#include hidsdi.h

}

#include tchar.h

#pragma comment(lib, Dinput8.lib)

#pragma comment(lib, Dinput.lib)

#pragma comment(lib, Setupapi.lib)

#pragma comment(lib, hid.lib)

GUID myGuid;

LPDIRECTINPUT8 m_lpDI; // DI8接口指针

LPDIENUMDEVICESCALLBACK DIEnumDevicesCallback(const DIDEVICEINSTANCE* lpddi, VOID* pvRef)

{

*(GUID*) pvRef = lpddi-guidInstance;

return DIENUM_STOP;

}

int main(void)

{

GUID HidGuid;

// 查找本系统中HID类的GUID标识

HidD_GetHidGuid(&HidGuid);

printf(系统中HID类的GUID标识为:%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x\n,

HidGuid.Data1,HidGuid.Data2 ,HidGuid.Data3 ,

HidGuid.Data4[0],HidGuid.Data4[1],HidGuid.Data4[2],

HidGuid.Data4[3],HidGuid.Data4[4],HidGuid.Data4[5],

HidGuid.Data4[6],HidGuid.Data4[7]);

// 准备查找符合HID规范的USB设备

HDEVINFO hDevInfo = SetupDiGetClassDevs(

&HidGuid,

NULL,

NULL,

DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);

if (hDevInfo == INVALID_HANDLE_VALUE)

{

printf(符合HID规范的USB设备发生错误 \n);

return 0;

}

printf(正在查找可用的USB设备。。。。 \n);

DWORD MemberIndex = 0;

SP_DEVICE_INTERFACE_DATA DeviceInterfaceData;

BOOL bSuccess = FALSE;

DeviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);

do

{

bSuccess = SetupDiEnumDeviceInterfaces(

hDevInfo,

NULL,

&HidGuid,

MemberIndex,

&DeviceInterfaceData);

if ((!bSuccess) && (GetLastError() == ERROR_NO_MORE_ITEMS))

{

if(MemberIndex == 0)

{

printf(抱歉,未找到可用的USB设备! \n);

}

else

{

printf(没有更多的可用的USB设备! \n);

}

SetupDiDestroyDeviceInfoList(hDevInfo);

return 0;

}

printf(找到了一个USB设备: \n);

// 若找到了一个USB设备,则获取该设备的细节信息

PSP_DEVICE_INTERFACE_DETAIL_DATA pDeviceInterfaceDetailData;

DWORD Length = 0;

SetupDiGetDeviceInterfaceDetail(

hDevInfo,

&DeviceInterfaceData,

NULL,

0,

&Length,

NULL);

pDeviceInterfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(Length);

pDeviceInterfaceDetailData-cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); //MUST BE!!!

if (!SetupDiGetDeviceInterfaceDetail(

hDevInfo,

&DeviceInterfaceData,

pDeviceInterfaceDetailData,

Length,

NULL,

NULL))

{

printf(查找路径设备时出错! \n);

}

else

{

printf(设备路径:%s , pDeviceInterfaceDetailData-DevicePath);

}

//打开设备句柄

HANDLE hDeviceHandle = CreateFile(pDeviceInterfaceDetailData-DevicePath,

GENERIC_READ | GENERIC_WRITE,

FILE_SHARE_READ | FILE_SHARE_WRITE,

NULL,

OPEN_EXISTING,

0,

NULL);

if (hDeviceHandle == INVALID_HANDLE_VALUE)

{

printf(打开设备路径出错! \n);

// printf(%d, GetLastError());

}

else

{

HIDD_ATTRIBUTES Attributes;

HidD_GetAttributes(hDeviceHandle,&Attributes);

/**************************************************/

/*

long result;

int ReadBuffer[10];

int flag;

do

{

ReadBuffer[9] = 0;

ReadBuffer[8] = 0;

ReadBuffer[7] = 0;

ReadBuffer[6] = 0;

ReadBuffer[5] = 0;

ReadBuffer[4] = 0;

ReadBuffer[3] = 0;

ReadBuffer[2] = 0;

ReadBuffer[1] = 0;

ReadBuffer[0] = 0;

result = HidD_GetFeature(

hDeviceHandle,

(PVOID)ReadBuffer,

0x09);

// for (int i = 0; i ReadBuffer[1]; i++)

{

//printf(%d, ReadBuffer[i]);

}

}

//scanf(%d, &flag);

while(1);

*/

HRESULT hr;

HINSTANCE hInstance = GetModuleHandle(NULL) ;//获取实例句柄;

//建立DirectInput接口

if(NULL == m_lpDI)

{

hr = DirectInput8Create(hInstance,

DIRECTINPUT_VERSION,

IID_IDirectInput8,

(void**)&m_lpDI, //接口取值

NULL);

hr = m_lpDI-EnumDevices(DI8DEVCLASS_GAMECTRL,

(LPDIENUMDEVICESCALLBACK)DIEnumDevicesCallback, //回调函数

&myGuid, //赋值GUID

DIEDFL_ATTACHEDONLY); //扫描安装好的和连接好的设备

}

/**************************************************/

//将有关该设备的标识显示出来

printf(供应商ID :0X%04X ,Attributes.VendorID);

printf(产品ID :0X%04X ,Attributes.ProductID);

printf(产品版本号:0X%04X ,Attributes.VersionNumber);

WCHAR mString[256];

TCHAR Buffer[256];

HidD_GetManufacturerString(hDeviceHandle,mString,sizeof(mString));

if (wcstombs((char *)Buffer,mString,256) == -1) // fail

{

Buffer[0] = NULL;

}

printf(生产商: %s ,Buffer);

HidD_GetProductString(hDeviceHandle,mString,sizeof(mString));

if (wcstombs((char *)Buffer,mString,256) == -1)

{

Buffer[0] = NULL;

}

printf(产品名称: %s ,Buffer);

// 通信:

PHIDP_PREPARSED_DATA pHidpPreparsedData;

HIDP_CAPS hidPCaps;

if (!HidD_GetPreparsedData(hDeviceHandle, &pHidpPreparsedData))

{

printf(获取 HID PREPARED DATA 失败! \n);

return 0;

}

NTSTATUS status = HidP_GetCaps(pHidpPreparsedData,&hidPCaps);

if (status == HIDP_STATUS_SUCCESS)

{

printf(CAP信息如下: \n);

printf( InputReportByteLength %d , hidPCaps.InputReportByteLength);

printf( OutputReportByteLength %d , hidPCaps.OutputReportByteLength);

}

DWORD nReadBytes = 0;

BYTE *pInputReport = new BYTE[hidPCaps.InputReportByteLength];

memset(pInputReport,0,hidPCaps.InputReportByteLength);

do

{

ReadFile(hDeviceHandle,

pInputReport,

hidPCaps.InputReportByteLength,

&nReadBytes,

NULL);

if (hidPCaps.InputReportByteLength == nReadBytes)

{

for (unsigned int i = 0; i nReadBytes - 1;i++)

{

printf(%02x-,pInputReport[i]);

}

printf(%02x ,pInputReport[nReadBytes-1]);

}

// if (pInputReport[nReadBytes-2] == 0x20) //break the loop when pressing a specific key

{

// printf( \n);

break;

}

Sleep(10);

}while (hidPCaps.InputReportByteLength == nReadBytes);

// 释放句柄资源

CloseHandle(hDeviceHandle);

}

MemberIndex++;

}while(bSuccess);

SetupDiDestroyDeviceInfoList(hDevInfo);

return 0;

}

取消

评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值