C # Access USB (HID) devices

Source: Internet
Author: User
Tags reserved

Apart, directly to the code, if you really want to do this thing, or a little research, no ready-made good class use, you need to understand its principles
Here is the function of the API that invokes Windows
Get GUID
[DllImport ("Hid.dll")]
public static extern void Hidd_gethidguid (ref Guid hidguid);
Guid Guidhid = Guid.Empty;
filtration equipment, obtaining the required equipment
[DllImport ("setupapi.dll", SetLastError = True)]
public static extern IntPtr Setupdigetclassdevs (ref Guid Classguid, uint Enumerator, IntPtr hwndparent, DIGCF Fla GS);
IntPtr Hdevinfo;
Gets the device, true gets the
[DllImport ("setupapi.dll", CharSet = CharSet.Auto, SetLastError = True)]
public static extern Boolean setupdienumdeviceinterfaces (IntPtr hdevinfo, IntPtr devInfo, ref Guid INTERFACECLASSGU ID, UInt32 memberindex, ref sp_device_interface_data deviceinterfacedata);
public struct Sp_device_interface_data
{
public int cbsize;
Public Guid Interfaceclassguid;
public int flags;
public int reserved;
}

Get the details of the interface must call two times 1th time return length 2nd time Fetch data
[DllImport ("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern bool Setupdigetdeviceinterfacedetail (IntPtr deviceinfoset, ref Sp_device_interface_data Dev Iceinterfacedata, IntPtr Deviceinterfacedetaildata,
int deviceinterfacedetaildatasize, ref int requiredsize, sp_devinfo_data deviceinfodata);
[StructLayout (LayoutKind.Sequential)]
public class Sp_devinfo_data
{
public int cbsize = marshal.sizeof (typeof (Sp_devinfo_data));
Public Guid classguid = Guid.Empty; Temp
public int devinst = 0; Dumy
public int reserved = 0;
}

[StructLayout (layoutkind.sequential, Pack = 2)]
internal struct Sp_device_interface_detail_data
{
internal int cbsize;
Internal short DevicePath;
}

public enum DIGCF
{
Digcf_default = 0x1,
Digcf_present = 0x2,
Digcf_allclasses = 0x4,
Digcf_profile = 0x8,
Digcf_deviceinterface = 0x10
}

Get device files
[DllImport ("kernel32.dll", SetLastError = True)]
private static extern int CreateFile (
String lpfilename,//file name
UINT dwDesiredAccess,//access mode
UINT dwShareMode,//share mode
UINT lpSecurityAttributes,//SD
UINT dwCreationDisposition,//How to create
UINT dwFlagsAndAttributes,//File attributes
UINT hTemplateFile//handle to template file
);
Reading device files
[DllImport ("Kernel32.dll", SetLastError = True)]
private static extern bool ReadFile
(
INTPTR hfile,
byte [] lpbuffer,
UINT nNumberOfBytesToRead,
Ref UINT Lpnumberofbytesread,
IntPtr lpoverlapped
);

Release device
[DllImport ("Hid.dll")]
static public extern bool Hidd_freepreparseddata (ref INTPTR preparseddata);
Turn off access to device handles, and when you end the process, add this to the insurance.
[DllImport ("kernel32.dll")]
static public extern int CloseHandle (int hobject);

Next is the code to access the device
The code is temporarily not sorted, the incoming parameter is the device serial number,
Some USB devices actually have a lot of HID devices, that is, there are several devices on an interface, this time need
Loop with index++ until the device returns false, jump out, and get the device
The path is recorded all right, I know the specific equipment number, so there is no cycle, wasting my time

Fixed in the handle sequence number and some parameters, you can go to the Internet to find the parameters of these APIs, after the text I see whether the information can also be written up
int hidhandle =-1;
Public const UINT GENERIC_READ = 0x80000000;
Public const UINT GENERIC_WRITE = 0x40000000;
Public const UINT FILE_SHARE_READ = 0x00000001;
Public const UINT FILE_SHARE_WRITE = 0x00000002;
public const int open_existing = 3;

private void Usbmethod (int index)
{
Hidd_gethidguid (ref Guidhid);
Hdevinfo = Setupdigetclassdevs (ref guidhid, 0, IntPtr.Zero, DIGCF.  digcf_present | DIGCF. Digcf_deviceinterface);
int buffersize = 0;
ArrayList hidusbaddress = new ArrayList ();

while (true)
// {
Gets the device, true gets the
Sp_device_interface_data deviceinterfacedata = new Sp_device_interface_data ();
Deviceinterfacedata.cbsize = marshal.sizeof (Deviceinterfacedata);
for (int i = 0; i < 3; i++)
// {
BOOL result = Setupdienumdeviceinterfaces (Hdevinfo, IntPtr.Zero, ref guidhid, (UInt32) index, ref deviceinterfacedat a);
// }
The first call was an error, but the correct size could be returned
Sp_devinfo_data strtinterfacedata = new Sp_devinfo_data ();
result = Setupdigetdeviceinterfacedetail (Hdevinfo, ref deviceinterfacedata, IntPtr.Zero, 0, ref buffersize, StrtIn Terfacedata);
&nbs

Related Article

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.