USB device information

Source: Internet
Author: User
Compiling environment: Borland C ++ Builder 6.0, Windows Server 2003 DDK
Running Environment: Win98/2000/XP/2003

The following functions are required to access the USB port:

# Include <VCL. h>
# Include <dir. h>
# Include <setupapi. h>
# Include "C:/winddk/3790/INC/DDK/w2k/usbdi. H"
# Include "C:/winddk/3790/INC/DDK/w2k/devictl. H"
# Include <initguid. h>
//---------------------------------------------------------------------------
// The following must be the guid value of the driver. Here I am writing a number in disorder.
Define_guid (usb_driver_guid, 0x12345678, 0 xabcd, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0x00 );
//---------------------------------------------------------------------------
Handle openonedevice (hdevinfo hdvcinfo, psp_interface_device_data dvcinfodata, char * sdevnamebuf)
{
Handle Hout = invalid_handle_value;

Ulong ireqlen = 0;
Setupdigetinterfacedevicedetail (hdvcinfo, dvcinfodata, null, 0, & ireqlen, null );

Ulong idevdatalen = ireqlen; // sizeof (sp_fnclass_device_data) + 512;
Psp_interface_device_detail_data pdevdata = (psp_interface_device_detail_data) malloc (idevdatalen );

Pdevdata-> cbsize = sizeof (sp_interface_device_detail_data );
If (setupdigetinterfacedevicedetail (hdvcinfo, dvcinfodata, pdevdata, idevdatalen, & ireqlen, null ))
{
Strcpy (sdevnamebuf, pdevdata-> devicepath );
Hout = createfile (pdevdata-> devicepath, generic_read | generic_write, file_pai_read | file_pai_write, null, open_existing, 0, null );
}

Free (pdevdata );
Return Hout;
}
//---------------------------------------------------------------------------
Handle openusbdevice (const guid * pguid, char * sdevnamebuf)
{
Handle Hout = invalid_handle_value;

Hdevinfo = setupdigetclassdevs (pguid, null, null, digcf_present | digcf_interfacedevice );

Sp_interface_device_data deviceinfodata;
Deviceinfodata. cbsize = sizeof (sp_interface_device_data );

Ulong nguesscount = maxlong;
For (ulong idevindex = 0; idevindex <nguesscount; idevindex ++)
{
If (setupdienumdeviceinterfaces (hdevinfo, 0, pguid, idevindex, & deviceinfodata ))
{
If (Hout = openonedevice (hdevinfo, & deviceinfodata, sdevnamebuf ))! = Invalid_handle_value)
Break;
}
Else if (getlasterror () = error_no_more_items) // no more items
{
Break;
}
}
Setupdidestroydeviceinfolist (hdevinfo );
Return Hout;
}
//---------------------------------------------------------------------------
Bool getusbdevicefilename (const guid * pguid, char * sdevnamebuf)
{
Handle hdev = openusbdevice (pguid, sdevnamebuf );
If (hdev! = Invalid_handle_value)
{
Closehandle (hdev );
Return true;
}
Return false;
}
//---------------------------------------------------------------------------
Handle openmydevice ()
{
Char devicename [maxpath] = "";
Return openusbdevice (& usb_driver_guid, devicename );
}
//---------------------------------------------------------------------------
Handle openmydevpipe (const char * pipename)
{
Char devicename [maxpath] = "";
If (getusbdevicefilename (& usb_driver_guid, devicename ))
{
Strcat (devicename ,"//");
Strcat (devicename, pipename );
Return createfile (devicename, generic_write | generic_read, file_pai_write | file_pai_read, null, open_existing, 0, null );
}
Return invalid_handle_value;
}
//---------------------------------------------------------------------------

With the above function, you can access the USB port:
// Enable USB port read/write, determined by the driver pipe name

Handle hpipe = openmydevpipe ("mypipe1"); // The Pipe name in the driver, corresponding to the I/O that accesses an endpoint. Here I am writing it in disorder and it must be consistent with the driver
If (hpipe! = Invalid_handle_value) // The pipe is successfully enabled.
{
Readfile (hpipe, buffer, bufsize, & nbytesread, null); // read data from hpipe to buffer
// Writefile (hpipe, buffer, bytestowrite, & nbyteswritten, null); // write bytestowrite bytes in the buffer to hpipe
Closehandle (hpipe );
}

// Use deviceiocontrol to access a USB device

Handle hdevice = openmydevice ();
If (hdevice! = Invalid_handle_value) // The device is successfully enabled.
{
// These deviceiocontrol functions are defined by the device. For more information, see the device and driver information.
If (deviceiocontrol (hdevice, ioctl_read_xxxx, & ioblock, sizeof (ioblock), & C, 1, & nbytes, null ))
{
// Succeeded
}
Closehandle (hdevice );
}

 
USB device, USB driver, USB Application

1. USB device hardware
A. The hardware identifier is the vender ID and product ID, that is, the "manufacturer ID" and "Product ID"
B. This hardware specifies the nature of each end point, read/write, and type (control/interrupt/bulk/isochronous)
C. The hardware firmware contains the implementation part of deviceiocontrol, which specifies the specific parameters and actions of the function.

2. USB device driver
① Hardware interface
A. You need to identify the vender ID and product ID
B. assign a pipe to each I/O of each endpoint and name it as the software interface.
C. interfaces for deviceiocontrol
② Software Interface
A. guid: The identifier of the driver. Each driver uses a different guid. guid identifies the driver and has nothing to do with the hardware. (The GUID of the Driver Upgrade cannot be modified)
B. In the hardware interface, B: pipe is called a software interface. The Pipe name is defined by the driver and has nothing to do with the hardware. The upgrade driver cannot change the pipe name.
C. the parameters of C in the hardware interface are also software interfaces. These parameters are provided by the hardware, not the driver specification. Of course, they can also be escaped in the driver to hide the real situation of the device.
③ This driver is compiled using winddk. You can compile the program code using a text editor or an editor of other development tools, and then call winddk to compile the program.

3. Read and Write the USB port Program
① Interface with the driver
A. Use the guid in the driver to find the device file name and use the createfile function to open the device. The openusbdevice in the previous program serves this purpose.
B. Open pipe through the device file name obtained by A. and the pipe name in the driver to access the corresponding USB endpoint of pipe (read and write data)
C. Use the handle obtained from createfile of A. And use deviceiocontrol to implement the action specified by the device.
② Required information
A. Vender ID, product ID, and guid can be seen in the. inf file of the driver. If they cannot be found, contact the manufacturer.
B. The Pipe name is specified by the driver. You must have the driver information to know.
C. The deviceiocontrol parameters must have driver information or hardware information to be known.
③ This program is generally written in C/C ++ directly. If other languages (such as VB and Pb) need to call the C/C ++ compiled DLL

Other related content:

The USB driver can be found in the registry:
"HKEY_LOCAL_MACHINE // system // controlset001 // Enum // USB // VID _ manufacturer ID & pid _ product ID // driver"

The classguid is the guid of the driver, for example, {36fc9e60-c465-11cf-8056-444553540000}
Equivalent to the program: define_guid (usb_driver_guid, 0x36fc9e60, 0xc465, 0x11cf, 0x80, 0x56, 0x44,0x45, 0x53,0x54,0x00,0x00 );
You can also find other device descriptions in the registry key, such as devicedesc = "USB Mass Storage Device ".

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.