USB-related programming

Source: Internet
Author: User

Now there are many USB devices, so the USB device to find and read and write is essential. But there is little information about USB reading and writing. Here we use VC + + to demonstrate some ways to get USB information.

First, enumerate USB devices

Enumerate the USB hub-> on this controller by enumerating the-> of the USB controller to enumerate each port-> the HUB to obtain device information.

Enumerate controllers:

wsprintf(HCName, "\\\\.\\HCD%d", HCNum);
hHCDev = CreateFile(HCName,GENERIC_WRITE,FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,NULL);

The USB host controller on the computer is named HCD1,HCD2. With the controller name, use Ccreatefile to open it. Use DeviceIoControl to get its driver name and the name of the hub with which it is attached. Use Ccreatefile to open the hub to obtain connection information. The various ports of the hub are then enumerated to obtain the attached device information.

Ii. Enumerating HID Devices

HID device is the standard human-machine interface specification defined by Microsoft. such as USB mouse, USB game handles and so on. The GUID can be obtained using API Hidd_gethidguid (&guidhid) without looking for the GUID of the specific device. A GUID is available via API Setupdienumdeviceinterfaces to obtain a device connection. If this type of device connection obtains its device path information through Setupdigetdeviceinterfacedetail. Use Ccreatefile to open it and obtain its basic property information through Hidd_getattributes. You can use DeviceIoControl to get more detailed properties. In this code, if you have a USB game handle plugged into your computer, you can get information about it. But do not know why XP can not get the information of the USB mouse.

Three to enumerate U disk

First use the GetDriveType API to obtain the type of equipment, if the type is removable (of course, some of the large capacity U disk may be reported as fixed, that will require other methods to determine), that is, may be a U disk. After the Ccreatefile is opened, the properties are obtained by using Ioctl_storage_query_property as the DeviceIoControl of the parameter.

Iv. concluding remarks

The example project was compiled and passed under WINXP+XPDDK+VC6. USB devices are more types, but also more special, different vendors of different hardware, control software is not the same (I think the main ictl code is different, also not easy to find). The device that accesses the USB port is not as convenient as the serial port. This routine simply shows the basic approach to access. Some of these problems have not been resolved, sent out to hope that you can notify me or published after the resolution.

Refer to the Usbport,usbview and other code, together thanks.

This article supporting source code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.