我正在更新我的问题,以更好地反映我实际追求的目标。为了快速说明我最初的困惑,说“设备接口类 GUID ”和设备实例 ID之间存在一对一的关系是不正确的。一个设备可以有很多设备接口。正如Ben Voigt在评论中指出的那样,有关更多信息,请参阅此内容。
CM_Get_Child (...)
调用函数后如何打开子设备的句柄?
以下面的代码片段为例:
#pragma comment (lib, "Setupapi.lib")
#pragma comment (lib, "Cfgmgr32.lib")
#include <iostream>
#include <Windows.h>
#include <Setupapi.h>
#include <Cfgmgr32.h>
#define GUID_STRING_SIZE 40
int main ()
{
CONFIGRET CMResult = CR_SUCCESS;
WCHAR DeviceInstanceID[] = L"USB\\VID_2109&PID_0813\\8&216C1825&0&4\0"; // Parent Device Instance ID.
DEVNODE ParentDeviceNode = (DWORD) 0; // A device instance handle. This handle is bounded to the local machine.
CMResult = CM_Locate_DevNode ((PDEVINST) &ParentDeviceNode, DeviceInstanceID, CM_LOCATE_DEVNODE_NORMAL);
if (CMResult != CR_SUCCESS)
{
std::cout << "No parent device node found." << std::endl;
return -1;
}
else
{
DEVINST NextChildDeviceNode = (DWORD) 0;
CMResult = CM_Get_Child ((PDEVINST) &NextChildDeviceNode, ParentDeviceNode, 0x0); // Gets the first child of the parent node. If this returns "CR_NO_SUCH_DEVNODE," then there is no child attached.
if (CMResult != CR_SUCCESS)
{
std::cout << "No child device node found." << std::endl;
return -2;
}
else
{
ULONG ChildInstanceIDBuffLength = 0;
CMResult = CM_Get_Device_ID_Size (&ChildInstanceIDBuffLength, NextChildDeviceNode, 0x0);
if (CMResult != CR_SUCCESS)
{
std::cout << "Could not get the size of the device instance ID of child device." << std::endl;
return -3;
}
else
{
WCHAR * ChildInstanceIDBuff = (WCHAR *) malloc (ChildInstanceIDBuffLength);
CMResult = CM_Get_Device_IDW (NextChildDeviceNode, ChildInstanceIDBuff, ChildInstanceIDBuffLength, 0x0);
if (CMResult != CR_SUCCESS)
{
std::cout << "Could not actual device instance ID string of child device" << std::endl;
return -4;
}
else
{
std::cout << "Found child device instance ID: ";
std::wcout << ChildInstanceIDBuff << std::endl;
/*
* Open handle to the child device node now!
*/
}
free (ChildInstanceIDBuff);
}
}
}
return 0;
}
如何使用新获得的子设备实例 ID来打开设备的句柄? CreateFile (...)
需要完整的设备路径,其中包括缺少的“设备接口类 GUID”。
更具体地说,设备路径具有以下格式:
\\?\usb#vid_2109&pid_0813#7&3981C8D6&0&2#{[DEVICE_INTERFACE_GUID]}
,其中:
- [DEVICE_INTERFACE_GUID] - 这是“设备接口类 GUID ”。这与“设备设置类 GUID ”不同。
如果没有某种程度的蛮力(例如使用 CM_ENUMERATE_CLASSES_INTERFACE 标志),似乎没有一种简单的方法来获得这个“设备接口类 GUID ”。是否可以调用一个函数来仅使用设备的“设备实例 ID ”来获取设备的句柄,以便我可以调用和查询有关设备的信息?CM_Enumerate_Classes (...)
DeviceIoControl (...)