使用SetupAPI,我执行函数SetupDiGetClassDevs
并获得一个指针或一个句柄。
然后我开始循环并运行:
Return = SetupDiEnumDeviceInterfaces();
与
SP_DEVICE_INTERFACE_DATA.cbSize = 0
以获得
SP_DEVICE_INTERFACE_DATA
所需的大小。然后,我设置此大小并再次执行:
SP_DEVICE_INTERFACE_DATA.cbSize = return; // (the size)
SetupDiEnumDeviceInterfaces();
从数据结构中我得到:
我相信,来自
DevicePath
的SP_DEVINFO_DATA
和来自注册表的大量信息。我真正想要的是访问HID库并调用
HidD_GetAttributes
从此枚举的设备获取VendorID
,ProductID
和VersionNumber
,因此我可以识别该设备。我希望此特定信息来自USB设备本身。
谁能告诉我该怎么做?
顺便说一句,在我的Windows XP版本中,我使用的是注册表路径,找不到
HKEY_LOCAL_MACHINE\Enum\HID\...\Class
。我什至找不到
HKEY_LOCAL_MACHINE\Enum\
。我认为这是因为我尚未执行功能
SetupDiEnumDeviceInterfaces
。我只能在Lakeview Research中找到完整的数据。但这并不涵盖此主题。为什么垃圾邮件会遍及整个网络?
最佳答案
不知道这是您要找的东西吗?
BOOL CheckIfPresentAndGetUSBDevicePath(void)
{
/*
Before we can "connect" our application to our USB embedded device, we must first find the device.
A USB bus can have many devices simultaneously connected, so somehow we have to find our device only.
This is done with the Vendor ID (VID) and Product ID (PID). Each USB product line should have
a unique combination of VID and PID.
Microsoft has created a number of functions which are useful for finding plug and play devices. Documentation
for each function used can be found in the MSDN library. We will be using the following functions:
SetupDiGetClassDevs() //provided by setupapi.dll, which comes with Windows
SetupDiEnumDeviceInterfaces() //provided by setupapi.dll, which comes with Windows
GetLastError() //provided by kernel32.dll, which comes with Windows
SetupDiDestroyDeviceInfoList() //provided by setupapi.dll, which comes with Windows
SetupDiGetDeviceInterfaceDetail() //provided by setupapi.dll, which comes with Windows
SetupDiGetDeviceRegistryProperty() //provided by setupapi.dll, which comes with Windows
malloc() //part of C runtime library, msvcrt.dll?
CreateFile() //provided by kernel32.dll, which comes with Windows
We will also be using the following unusual data types and structures. Documentation can also be found in
the MSDN library:
PSP_DEVICE_INTERFACE_DATA
PSP_DEVICE_INTERFACE_DETAIL_DATA
SP_DEVINFO_DATA
HDEVINFO
HANDLE
GUID
The ultimate objective of the following code is to get the device path, which will be used elsewhere for getting
read and write handles to the USB device. Once the read/write handles are opened, only then can this
PC application begin reading/writing to the USB device using the WriteFile() and ReadFile() functions.
Getting the device path is a multi-step round about process, which requires calling several of the
SetupDixxx() functions provided by setupapi.dll.
*/
HDEVINFO DeviceInfoTable = INVALID_HANDLE_VALUE;
PSP_DEVICE_INTERFACE_DATA InterfaceDataStructure = new SP_DEVICE_INTERFACE_DATA;
// PSP_DEVICE_INTERFACE_DETAIL_DATA DetailedInterfaceDataStructure = new SP_DEVICE_INTERFACE_DETAIL_DATA; //Globally declared instead
SP_DEVINFO_DATA DevInfoData;
DWORD InterfaceIndex = 0;
DWORD StatusLastError = 0;
DWORD dwRegType;
DWORD dwRegSize;
DWORD StructureSize = 0;
PBYTE PropertyValueBuffer;
bool MatchFound = false;
DWORD ErrorStatus;
BOOL BoolStatus = FALSE;
DWORD LoopCounter = 0;
char * DeviceIDToFind = MY_DEVICE_ID;
// First populate a list of plugged in devices (by specifying "DIGCF_PRESENT"), which are of the specified class GUID.
DeviceInfoTable = SetupDiGetClassDevsUM(&InterfaceClassGuid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
// Now look through the list we just populated. We are trying to see if any of them match our device.
while(true)
{
InterfaceDataStructure->cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
if(SetupDiEnumDeviceInterfacesUM(DeviceInfoTable, NULL, &InterfaceClassGuid, InterfaceIndex, InterfaceDataStructure))
{
ErrorStatus = GetLastError();
if(ErrorStatus == ERROR_NO_MORE_ITEMS) // Did we reach the end of the list of matching devices in the DeviceInfoTable?
{ // Cound not find the device. Must not have been attached.
SetupDiDestroyDeviceInfoListUM(DeviceInfoTable); //Clean up the old structure we no longer need.
return FALSE;
}
}
else // Else some other kind of unknown error ocurred...
{
ErrorStatus = GetLastError();
SetupDiDestroyDeviceInfoListUM(DeviceInfoTable); // Clean up the old structure we no longer need.
return FALSE;
}
// Now retrieve the hardware ID from the registry. The hardware ID contains the VID and PID, which we will then
// check to see if it is the correct device or not.
// Initialize an appropriate SP_DEVINFO_DATA structure. We need this structure for SetupDiGetDeviceRegistryProperty().
DevInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
SetupDiEnumDeviceInfoUM(DeviceInfoTable, InterfaceIndex, &DevInfoData);
// First query for the size of the hardware ID, so we can know how big a buffer to allocate for the data.
SetupDiGetDeviceRegistryPropertyUM(DeviceInfoTable, &DevInfoData, SPDRP_HARDWAREID, &dwRegType, NULL, 0, &dwRegSize);
// Allocate a buffer for the hardware ID.
PropertyValueBuffer = (BYTE *) malloc (dwRegSize);
if(PropertyValueBuffer == NULL) // if null, error, couldn't allocate enough memory
{ // Can't really recover from this situation, just exit instead.
SetupDiDestroyDeviceInfoListUM(DeviceInfoTable); // Clean up the old structure we no longer need.
return FALSE;
}
// Retrieve the hardware IDs for the current device we are looking at. PropertyValueBuffer gets filled with a
// REG_MULTI_SZ (array of null terminated strings). To find a device, we only care about the very first string in the
// buffer, which will be the "device ID". The device ID is a string which contains the VID and PID, in the example
// format "Vid_04d8&Pid_003f".
SetupDiGetDeviceRegistryPropertyUM(DeviceInfoTable, &DevInfoData, SPDRP_HARDWAREID, &dwRegType, PropertyValueBuffer, dwRegSize, NULL);
// Now check if the first string in the hardware ID matches the device ID of my USB device.
#ifdef UNICODE
String^ DeviceIDFromRegistry = gcnew String((wchar_t *)PropertyValueBuffer);
#else
String^ DeviceIDFromRegistry = gcnew String((char *)PropertyValueBuffer);
#endif
free(PropertyValueBuffer); // No longer need the PropertyValueBuffer, free the memory to prevent potential memory leaks
// Convert both strings to lower case. This makes the code more robust/portable accross OS Versions
DeviceIDFromRegistry = DeviceIDFromRegistry->ToLowerInvariant();
DeviceIDToFind = DeviceIDToFind->ToLowerInvariant();
// Now check if the hardware ID we are looking at contains the correct VID/PID
MatchFound = DeviceIDFromRegistry->Contains(DeviceIDToFind);
if(MatchFound == true)
{
// Device must have been found. Open WinUSB interface handle now. In order to do this, we will need the actual device path first.
// We can get the path by calling SetupDiGetDeviceInterfaceDetail(), however, we have to call this function twice: The first
// time to get the size of the required structure/buffer to hold the detailed interface data, then a second time to actually
// get the structure (after we have allocated enough memory for the structure.)
DetailedInterfaceDataStructure->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
// First call populates "StructureSize" with the correct value
SetupDiGetDeviceInterfaceDetailUM(DeviceInfoTable, InterfaceDataStructure, NULL, NULL, &StructureSize, NULL);
DetailedInterfaceDataStructure = (PSP_DEVICE_INTERFACE_DETAIL_DATA)(malloc(StructureSize)); //Allocate enough memory
if(DetailedInterfaceDataStructure == NULL) //if null, error, couldn't allocate enough memory
{ // Can't really recover from this situation, just exit instead.
SetupDiDestroyDeviceInfoListUM(DeviceInfoTable); //Clean up the old structure we no longer need.
return FALSE;
}
DetailedInterfaceDataStructure->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
// Now call SetupDiGetDeviceInterfaceDetail() a second time to receive the goods.
SetupDiGetDeviceInterfaceDetailUM(DeviceInfoTable, InterfaceDataStructure, DetailedInterfaceDataStructure, StructureSize, NULL, NULL);
// We now have the proper device path, and we can finally open a device handle to the device.
// WinUSB requires the device handle to be opened with the FILE_FLAG_OVERLAPPED attribute.
SetupDiDestroyDeviceInfoListUM(DeviceInfoTable); //Clean up the old structure we no longer need.
return TRUE;
}
InterfaceIndex++;
// Keep looping until we either find a device with matching VID and PID, or until we run out of devices to check.
// However, just in case some unexpected error occurs, keep track of the number of loops executed.
// If the number of loops exceeds a very large number, exit anyway, to prevent inadvertent infinite looping.
LoopCounter++;
if(LoopCounter == 10000000) // Surely there aren't more than 10 million devices attached to any forseeable PC...
{
return FALSE;
}
}//end of while(true)
}
关于c++ - 从SetupAPI.DLL到HID.DLL,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2583125/