我知道如何在windows中使用设置api,但是,我似乎不知道如何获得我需要的一切。我想获得并知道如何为设备友好的名称,制造商和描述。但是,我似乎不知道如何获取设备路径,以便调用create file。有谁能给我指个方向吗?
发布于 2018-06-26 03:33:49
我明白,只链接到非现场的答案是不好的形式,但这个话题相当深刻。碰巧的是,微软也有一篇关于被问到的问题的文章:https://docs.microsoft.com/en-us/windows-hardware/drivers/install/using-setupapi-to-access-device-properties--windows-vista-and-later-。
下面的代码片段将返回GUID指定的所有设备的设备路径(至少包括initguid.h
,然后包括特定于设备的报头,例如usbiodef.h
)。这应该会让您对SetupAPI的工作原理有一个大致的了解。设备的路径是一个字符串,可以将其传递给CreateFile
以创建适合DeviceIoControl
使用的句柄。
vector<wstring> EnumDevices(
_In_ const GUID Guid
)
{
vector<wstring> r;
int index = 0;
HDEVINFO hDevInfo = SetupDiGetClassDevs(&Guid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
SP_DEVINFO_DATA DevInfoData;
memset(&DevInfoData, 0, sizeof(SP_DEVINFO_DATA));
DevInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
while (SetupDiEnumDeviceInfo(hDevInfo, index, &DevInfoData)) {
index++;
int jndex = 0;
SP_DEVICE_INTERFACE_DATA DevIntData;
memset(&DevIntData, 0, sizeof(SP_DEVICE_INTERFACE_DATA));
DevIntData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
while (SetupDiEnumDeviceInterfaces(
hDevInfo,
&DevInfoData, &Guid, jndex, &DevIntData
)) {
jndex++;
// Get the size required for the structure.
DWORD RequiredSize;
SetupDiGetDeviceInterfaceDetail(
hDevInfo, &DevIntData, NULL, NULL, &RequiredSize, NULL
);
PSP_DEVICE_INTERFACE_DETAIL_DATA pDevIntDetData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(
sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA) + RequiredSize
);
memset(pDevIntDetData, 0, sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA) + RequiredSize);
pDevIntDetData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
SetupDiGetDeviceInterfaceDetail(
hDevInfo,
&DevIntData,
pDevIntDetData, RequiredSize,
NULL,
&DevInfoData
);
r.push_back(wstring(pDevIntDetData->DevicePath));
free(pDevIntDetData);
}
}
return r;
}
https://stackoverflow.com/questions/31624228
复制相似问题