一次获取USB设备描述信息的尝试(USB device descriptor)

一开始用下面的方法尝试获取:

#include 
#include 
#include 
#include 
#include 
#pragma warning(disable:4996)

void getUsbDeviceDescriptor(HANDLE);

int main()
{
	HANDLE hDevice = CreateFile("\\\\.\\G:",
		GENERIC_READ,
		FILE_SHARE_READ,
		NULL,
		OPEN_EXISTING,
		0,
		NULL);
	printf("%d", hDevice);

	getUsbDeviceDescriptor(hDevice);

	CloseHandle(hDevice);
}

void getUsbDeviceDescriptor(HANDLE hDevice)
{
	DWORD readed;
	USBSCAN_GET_DESCRIPTOR inputBuf = { 0 };
	inputBuf.DescriptorType = USB_DEVICE_DESCRIPTOR_TYPE;

	USB_DEVICE_DESCRIPTOR outputBuf = { 0 };

	bool result = DeviceIoControl(hDevice,
		IOCTL_GET_USB_DESCRIPTOR,
		&inputBuf, sizeof(USBSCAN_GET_DESCRIPTOR),
		&outputBuf, sizeof(USB_DEVICE_DESCRIPTOR),
		NULL,
		NULL);

}

但是DeviceIoControl一直返回0,百思不得其解。上网搜了一下,其他人也遇到这个问题:
https://bbs.csdn.net/topics/100043390
https://bbs.pediy.com/thread-79084.htm

 

后面改用驱动器号获取handle,改进后的代码如下:

DWORD GetPhysicalDriveFromPartitionLetter(CHAR letter)
{
    HANDLE hDevice;               // handle to the drive to be examined
    BOOL result;                  // results flag
    DWORD readed;                   // discard results
    STORAGE_DEVICE_NUMBER number;   //use this to get disk numbers
 
    CHAR path[20];
    sprintf(path, "\\\\.\\%c:", letter);
    hDevice = CreateFile(path, // drive to open
                         GENERIC_READ | GENERIC_WRITE,    // access to the drive
                         FILE_SHARE_READ | FILE_SHARE_WRITE,    //share mode
                         NULL,             // default security attributes
                         OPEN_EXISTING,    // disposition
                         0,                // file attributes
                         NULL);            // do not copy file attribute
    if (hDevice == INVALID_HANDLE_VALUE) // cannot open the drive
    {
        fprintf(stderr, "CreateFile() Error: %ld\n", GetLastError());
        return DWORD(-1);
    }
 
    result = DeviceIoControl(
                hDevice,                // handle to device
                IOCTL_STORAGE_GET_DEVICE_NUMBER, // dwIoControlCode
                NULL,                            // lpInBuffer
                0,                               // nInBufferSize
                &number,           // output buffer
                sizeof(number),         // size of output buffer
                &readed,       // number of bytes returned
                NULL      // OVERLAPPED structure
            );
    if (!result) // fail
    {
      fprintf(stderr, "IOCTL_STORAGE_GET_DEVICE_NUMBER Error: %ld\n", GetLastError());
        (void)CloseHandle(hDevice);
        return (DWORD)-1;
    }
	printf("%d %d %d\n\n", number.DeviceType, number.DeviceNumber, number.PartitionNumber);
 
    (void)CloseHandle(hDevice);
    return number.DeviceNumber;
}

国内关于这方面的资料还是太少,上bing搜索,找到了下面这个帖子 
https://www.techtalkz.com/threads/usb-device-descriptor.266373/ 原来IOCTL_GET_USB_DESCRIPTOR只支持图像设备(扫描仪之类的?)...
一次失败的经历。。。

转载于:https://www.cnblogs.com/sherlock-merlin/p/11076941.html

你可能感兴趣的:(一次获取USB设备描述信息的尝试(USB device descriptor))