gpt4 book ai didi

c++ - SetupDiGetDeviceInterfaceDetail 只为所有 USB HID 对象的路径返回 "\"

转载 作者:可可西里 更新时间:2023-11-01 14:47:17 25 4
gpt4 key购买 nike

我可以知道我有多少个 USB HID 设备 (7),但每次我尝试获取任何设备的详细信息时,为它返回的路径总是“\”,这样我就无法访问设备。我使用的代码在过程中与此代码非常相似:

HANDLE connectDeviceNumber(DWORD deviceIndex)
{
GUID hidGUID;
HDEVINFO hardwareDeviceInfoSet;
SP_DEVICE_INTERFACE_DATA deviceInterfaceData;
PSP_INTERFACE_DEVICE_DETAIL_DATA deviceDetail;
ULONG requiredSize;
HANDLE deviceHandle = INVALID_HANDLE_VALUE;
DWORD result;

//Get the HID GUID value - used as mask to get list of devices
HidD_GetHidGuid (&hidGUID);

//Get a list of devices matching the criteria (hid interface, present)
hardwareDeviceInfoSet = SetupDiGetClassDevs (&hidGUID,
NULL, // Define no enumerator (global)
NULL, // Define no
(DIGCF_PRESENT | // Only Devices present
DIGCF_DEVICEINTERFACE)); // Function class devices.

deviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);

//Go through the list and get the interface data
result = SetupDiEnumDeviceInterfaces (hardwareDeviceInfoSet,
NULL, //infoData,
&hidGUID, //interfaceClassGuid,
deviceIndex,
&deviceInterfaceData);

/* Failed to get a device - possibly the index is larger than the number of devices */
if (result == FALSE)
{
SetupDiDestroyDeviceInfoList (hardwareDeviceInfoSet);
Log("hidin: -- failed to get specified device number");
return INVALID_HANDLE_VALUE;
}

//Get the details with null values to get the required size of the buffer
SetupDiGetDeviceInterfaceDetail (hardwareDeviceInfoSet,
&deviceInterfaceData,
NULL, //interfaceDetail,
0, //interfaceDetailSize,
&requiredSize,
0); //infoData))

//Allocate the buffer
deviceDetail = (PSP_INTERFACE_DEVICE_DETAIL_DATA)malloc(requiredSize);
deviceDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);

//Fill the buffer with the device details
if (!SetupDiGetDeviceInterfaceDetail (hardwareDeviceInfoSet,
&deviceInterfaceData,
deviceDetail,
requiredSize,
&requiredSize,
NULL))
{
SetupDiDestroyDeviceInfoList (hardwareDeviceInfoSet);
free (deviceDetail);
Log("hidin: -- failed to get device info");
return INVALID_HANDLE_VALUE;
}

Log("Opening device with path: %s", deviceDetail->DevicePath);

最佳答案

确定您正在使用定义的 UNICODE 进行编译吗?那么你的 Log() 格式化字符串是错误的。修复:

   Log("Opening device with path: %ls", deviceDetail->DevicePath);

关于c++ - SetupDiGetDeviceInterfaceDetail 只为所有 USB HID 对象的路径返回 "\",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1744513/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com