Win32Hid.cs
author StephaneLenclud
Sun, 15 Feb 2015 23:16:57 +0100
changeset 67 bda062e75e47
parent 59 89dfad9633b2
child 68 bbe61b1021bb
permissions -rw-r--r--
Adding status bar to show the device that just sent an event.
     1 using System;
     2 using System.Runtime.InteropServices;
     3 using Microsoft.Win32.SafeHandles;
     4 using System.Text;
     5 
     6 namespace Win32
     7 {
     8 
     9     static partial class Function
    10     {
    11         [DllImport("hid.dll", CharSet = CharSet.Unicode)]
    12         public static extern HidStatus HidP_GetUsagesEx(HIDP_REPORT_TYPE ReportType, ushort LinkCollection, [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 3)] USAGE_AND_PAGE[] ButtonList, ref uint UsageLength, IntPtr PreparsedData, [MarshalAs(UnmanagedType.LPArray)] byte[] Report, uint ReportLength);
    13 
    14         [DllImport("hid.dll", CharSet = CharSet.Auto, SetLastError = true)]
    15         public static extern Boolean HidD_GetManufacturerString(SafeFileHandle HidDeviceObject, StringBuilder Buffer, Int32 BufferLength);
    16 
    17         [DllImport("hid.dll", CharSet = CharSet.Auto, SetLastError = true)]
    18         public static extern Boolean HidD_GetProductString(SafeFileHandle HidDeviceObject, StringBuilder Buffer, Int32 BufferLength);
    19 
    20         [DllImport("hid.dll", CharSet = CharSet.Auto, SetLastError = true)]
    21         public static extern Boolean HidD_GetAttributes(SafeFileHandle HidDeviceObject, ref HIDD_ATTRIBUTES Attributes);
    22 
    23         /// Return Type: NTSTATUS->LONG->int
    24         ///PreparsedData: PHIDP_PREPARSED_DATA->_HIDP_PREPARSED_DATA*
    25         ///Capabilities: PHIDP_CAPS->_HIDP_CAPS*
    26         [DllImportAttribute("hid.dll", EntryPoint = "HidP_GetCaps", CallingConvention = CallingConvention.StdCall)]
    27         public static extern HidStatus HidP_GetCaps(System.IntPtr PreparsedData, ref HIDP_CAPS Capabilities);
    28 
    29         /// Return Type: NTSTATUS->LONG->int
    30         ///ReportType: HIDP_REPORT_TYPE->_HIDP_REPORT_TYPE
    31         ///ButtonCaps: PHIDP_BUTTON_CAPS->_HIDP_BUTTON_CAPS*
    32         ///ButtonCapsLength: PUSHORT->USHORT*
    33         ///PreparsedData: PHIDP_PREPARSED_DATA->_HIDP_PREPARSED_DATA*
    34         [System.Runtime.InteropServices.DllImportAttribute("hid.dll", EntryPoint = "HidP_GetButtonCaps", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
    35         public static extern HidStatus HidP_GetButtonCaps(HIDP_REPORT_TYPE ReportType, [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)] HIDP_BUTTON_CAPS[] ButtonCaps, ref ushort ButtonCapsLength, System.IntPtr PreparsedData);
    36 
    37         /// Return Type: NTSTATUS->LONG->int
    38         ///ReportType: HIDP_REPORT_TYPE->_HIDP_REPORT_TYPE
    39         ///ValueCaps: PHIDP_VALUE_CAPS->_HIDP_VALUE_CAPS*
    40         ///ValueCapsLength: PUSHORT->USHORT*
    41         ///PreparsedData: PHIDP_PREPARSED_DATA->_HIDP_PREPARSED_DATA*
    42         [System.Runtime.InteropServices.DllImportAttribute("hid.dll", EntryPoint = "HidP_GetValueCaps", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
    43         public static extern HidStatus HidP_GetValueCaps(HIDP_REPORT_TYPE ReportType, [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)] HIDP_VALUE_CAPS[] ValueCaps, ref ushort ValueCapsLength, System.IntPtr PreparsedData);
    44 
    45     }
    46 
    47 
    48     static partial class Macro
    49     {
    50 
    51 
    52     }
    53 
    54 
    55     static partial class Const
    56     {
    57 
    58 
    59     }
    60 
    61 
    62     public enum HIDP_REPORT_TYPE : ushort
    63     {
    64         HidP_Input = 0,
    65         HidP_Output,
    66         HidP_Feature
    67     }
    68 
    69 
    70     public enum HidStatus : uint
    71     {
    72         HIDP_STATUS_SUCCESS = 0x110000,
    73         HIDP_STATUS_NULL = 0x80110001,
    74         HIDP_STATUS_INVALID_PREPARSED_DATA = 0xc0110001,
    75         HIDP_STATUS_INVALID_REPORT_TYPE = 0xc0110002,
    76         HIDP_STATUS_INVALID_REPORT_LENGTH = 0xc0110003,
    77         HIDP_STATUS_USAGE_NOT_FOUND = 0xc0110004,
    78         HIDP_STATUS_VALUE_OUT_OF_RANGE = 0xc0110005,
    79         HIDP_STATUS_BAD_LOG_PHY_VALUES = 0xc0110006,
    80         HIDP_STATUS_BUFFER_TOO_SMALL = 0xc0110007,
    81         HIDP_STATUS_INTERNAL_ERROR = 0xc0110008,
    82         HIDP_STATUS_I8042_TRANS_UNKNOWN = 0xc0110009,
    83         HIDP_STATUS_INCOMPATIBLE_REPORT_ID = 0xc011000a,
    84         HIDP_STATUS_NOT_VALUE_ARRAY = 0xc011000b,
    85         HIDP_STATUS_IS_VALUE_ARRAY = 0xc011000c,
    86         HIDP_STATUS_DATA_INDEX_NOT_FOUND = 0xc011000d,
    87         HIDP_STATUS_DATA_INDEX_OUT_OF_RANGE = 0xc011000e,
    88         HIDP_STATUS_BUTTON_NOT_PRESSED = 0xc011000f,
    89         HIDP_STATUS_REPORT_DOES_NOT_EXIST = 0xc0110010,
    90         HIDP_STATUS_NOT_IMPLEMENTED = 0xc0110020,
    91         HIDP_STATUS_I8242_TRANS_UNKNOWN = 0xc0110009
    92     }
    93 
    94 
    95     [StructLayout(LayoutKind.Sequential)]
    96     public struct USAGE_AND_PAGE
    97     {
    98         public ushort Usage;
    99         public ushort UsagePage;
   100     };
   101 
   102     [StructLayout(LayoutKind.Sequential)]
   103     public struct HIDD_ATTRIBUTES
   104     {
   105         public uint Size;
   106         public ushort VendorID;
   107         public ushort ProductID;
   108         public ushort VersionNumber;
   109     }
   110 
   111 
   112     [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
   113     public struct HIDP_CAPS
   114     {
   115         /// USAGE->USHORT->unsigned short
   116         public ushort Usage;
   117 
   118         /// USAGE->USHORT->unsigned short
   119         public ushort UsagePage;
   120 
   121         /// USHORT->unsigned short
   122         public ushort InputReportByteLength;
   123 
   124         /// USHORT->unsigned short
   125         public ushort OutputReportByteLength;
   126 
   127         /// USHORT->unsigned short
   128         public ushort FeatureReportByteLength;
   129 
   130         /// USHORT[17]
   131         [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst = 17, ArraySubType = System.Runtime.InteropServices.UnmanagedType.U2)]
   132         public ushort[] Reserved;
   133 
   134         /// USHORT->unsigned short
   135         public ushort NumberLinkCollectionNodes;
   136 
   137         /// USHORT->unsigned short
   138         public ushort NumberInputButtonCaps;
   139 
   140         /// USHORT->unsigned short
   141         public ushort NumberInputValueCaps;
   142 
   143         /// USHORT->unsigned short
   144         public ushort NumberInputDataIndices;
   145 
   146         /// USHORT->unsigned short
   147         public ushort NumberOutputButtonCaps;
   148 
   149         /// USHORT->unsigned short
   150         public ushort NumberOutputValueCaps;
   151 
   152         /// USHORT->unsigned short
   153         public ushort NumberOutputDataIndices;
   154 
   155         /// USHORT->unsigned short
   156         public ushort NumberFeatureButtonCaps;
   157 
   158         /// USHORT->unsigned short
   159         public ushort NumberFeatureValueCaps;
   160 
   161         /// USHORT->unsigned short
   162         public ushort NumberFeatureDataIndices;
   163     }
   164 
   165     /// <summary>
   166     /// Type created in place of an anonymous struct
   167     /// </summary>
   168     [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
   169     public struct HIDP_BUTTON_CAPS_RANGE
   170     {
   171 
   172         /// USAGE->USHORT->unsigned short
   173         public ushort UsageMin;
   174 
   175         /// USAGE->USHORT->unsigned short
   176         public ushort UsageMax;
   177 
   178         /// USHORT->unsigned short
   179         public ushort StringMin;
   180 
   181         /// USHORT->unsigned short
   182         public ushort StringMax;
   183 
   184         /// USHORT->unsigned short
   185         public ushort DesignatorMin;
   186 
   187         /// USHORT->unsigned short
   188         public ushort DesignatorMax;
   189 
   190         /// USHORT->unsigned short
   191         public ushort DataIndexMin;
   192 
   193         /// USHORT->unsigned short
   194         public ushort DataIndexMax;
   195     }
   196 
   197     /// <summary>
   198     /// Type created in place of an anonymous struct
   199     /// </summary>
   200     [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
   201     public struct HIDP_BUTTON_CAPS_NOT_RANGE
   202     {
   203 
   204         /// USAGE->USHORT->unsigned short
   205         public ushort Usage;
   206 
   207         /// USAGE->USHORT->unsigned short
   208         public ushort Reserved1;
   209 
   210         /// USHORT->unsigned short
   211         public ushort StringIndex;
   212 
   213         /// USHORT->unsigned short
   214         public ushort Reserved2;
   215 
   216         /// USHORT->unsigned short
   217         public ushort DesignatorIndex;
   218 
   219         /// USHORT->unsigned short
   220         public ushort Reserved3;
   221 
   222         /// USHORT->unsigned short
   223         public ushort DataIndex;
   224 
   225         /// USHORT->unsigned short
   226         public ushort Reserved4;
   227     }
   228 
   229     /// <summary>
   230     /// 
   231     /// </summary>
   232     [StructLayoutAttribute(LayoutKind.Explicit, Pack = 1)]
   233     public struct HIDP_BUTTON_CAPS
   234     {
   235         /// USAGE->USHORT->unsigned short
   236         [FieldOffset(0)]
   237         public ushort UsagePage;
   238 
   239         /// UCHAR->unsigned char
   240         [FieldOffset(2)]
   241         public byte ReportID;
   242 
   243         /// BOOLEAN->BYTE->unsigned char
   244         [FieldOffset(3)]
   245         [MarshalAsAttribute(UnmanagedType.U1)]
   246         public bool IsAlias;
   247 
   248         /// USHORT->unsigned short
   249         [FieldOffset(4)]
   250         public ushort BitField;
   251 
   252         /// USHORT->unsigned short
   253         [FieldOffset(6)]
   254         public ushort LinkCollection;
   255 
   256         /// USAGE->USHORT->unsigned short
   257         [FieldOffset(8)]
   258         public ushort LinkUsage;
   259 
   260         /// USAGE->USHORT->unsigned short
   261         [FieldOffset(10)]
   262         public ushort LinkUsagePage;
   263 
   264         /// BOOLEAN->BYTE->unsigned char
   265         [FieldOffset(12)]
   266         [MarshalAsAttribute(UnmanagedType.U1)]
   267         public bool IsRange;
   268 
   269         /// BOOLEAN->BYTE->unsigned char
   270         [FieldOffset(13)]
   271         [MarshalAsAttribute(UnmanagedType.U1)]
   272         public bool IsStringRange;
   273 
   274         /// BOOLEAN->BYTE->unsigned char
   275         [FieldOffset(14)]
   276         [MarshalAsAttribute(UnmanagedType.U1)]
   277         public bool IsDesignatorRange;
   278 
   279         /// BOOLEAN->BYTE->unsigned char
   280         [FieldOffset(15)]
   281         [MarshalAsAttribute(UnmanagedType.U1)]
   282         public bool IsAbsolute;
   283 
   284         /// ULONG[10]
   285         [FieldOffset(16)]
   286         [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 10, ArraySubType = UnmanagedType.U4)]
   287         public uint[] Reserved;
   288 
   289         /// Union Range/NotRange
   290         [FieldOffset(56)]
   291         HIDP_BUTTON_CAPS_RANGE Range;
   292 
   293         [FieldOffset(56)]
   294         HIDP_BUTTON_CAPS_NOT_RANGE NotRange;       
   295     }
   296 
   297     /// <summary>
   298     /// Type created in place of an anonymous struct
   299     /// </summary>
   300     [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
   301     public struct HIDP_VALUE_CAPS_RANGE
   302     {
   303 
   304         /// USAGE->USHORT->unsigned short
   305         public ushort UsageMin;
   306 
   307         /// USAGE->USHORT->unsigned short
   308         public ushort UsageMax;
   309 
   310         /// USHORT->unsigned short
   311         public ushort StringMin;
   312 
   313         /// USHORT->unsigned short
   314         public ushort StringMax;
   315 
   316         /// USHORT->unsigned short
   317         public ushort DesignatorMin;
   318 
   319         /// USHORT->unsigned short
   320         public ushort DesignatorMax;
   321 
   322         /// USHORT->unsigned short
   323         public ushort DataIndexMin;
   324 
   325         /// USHORT->unsigned short
   326         public ushort DataIndexMax;
   327     }
   328 
   329     /// <summary>
   330     /// Type created in place of an anonymous struct
   331     /// </summary>
   332     [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
   333     public struct HIDP_VALUE_CAPS_NOT_RANGE
   334     {
   335 
   336         /// USAGE->USHORT->unsigned short
   337         public ushort Usage;
   338 
   339         /// USAGE->USHORT->unsigned short
   340         public ushort Reserved1;
   341 
   342         /// USHORT->unsigned short
   343         public ushort StringIndex;
   344 
   345         /// USHORT->unsigned short
   346         public ushort Reserved2;
   347 
   348         /// USHORT->unsigned short
   349         public ushort DesignatorIndex;
   350 
   351         /// USHORT->unsigned short
   352         public ushort Reserved3;
   353 
   354         /// USHORT->unsigned short
   355         public ushort DataIndex;
   356 
   357         /// USHORT->unsigned short
   358         public ushort Reserved4;
   359     }
   360 
   361 
   362     /// <summary>
   363     /// 
   364     /// </summary>
   365     [StructLayoutAttribute(LayoutKind.Explicit, Pack = 1)]
   366     public struct HIDP_VALUE_CAPS
   367     {
   368 
   369         /// USAGE->USHORT->unsigned short
   370         [FieldOffset(0)]
   371         public ushort UsagePage;
   372 
   373         /// UCHAR->unsigned char
   374         [FieldOffset(2)]
   375         public byte ReportID;
   376 
   377         /// BOOLEAN->BYTE->unsigned char
   378         [FieldOffset(3)]
   379         [MarshalAsAttribute(UnmanagedType.U1)]
   380         public bool IsAlias;
   381 
   382         /// USHORT->unsigned short
   383         [FieldOffset(4)]
   384         public ushort BitField;
   385 
   386         /// USHORT->unsigned short
   387         [FieldOffset(6)]
   388         public ushort LinkCollection;
   389 
   390         /// USAGE->USHORT->unsigned short
   391         [FieldOffset(8)]
   392         public ushort LinkUsage;
   393 
   394         /// USAGE->USHORT->unsigned short
   395         [FieldOffset(10)]
   396         public ushort LinkUsagePage;
   397 
   398         /// BOOLEAN->BYTE->unsigned char
   399         [FieldOffset(12)]
   400         [MarshalAsAttribute(UnmanagedType.U1)]
   401         public bool IsRange;
   402 
   403         /// BOOLEAN->BYTE->unsigned char
   404         [FieldOffset(13)]
   405         [MarshalAsAttribute(UnmanagedType.U1)]
   406         public bool IsStringRange;
   407 
   408         /// BOOLEAN->BYTE->unsigned char
   409         [FieldOffset(14)]
   410         [MarshalAsAttribute(UnmanagedType.U1)]
   411         public bool IsDesignatorRange;
   412 
   413         /// BOOLEAN->BYTE->unsigned char
   414         [FieldOffset(15)]
   415         [MarshalAsAttribute(UnmanagedType.U1)]
   416         public bool IsAbsolute;
   417 
   418         /// BOOLEAN->BYTE->unsigned char
   419         [FieldOffset(16)]
   420         [MarshalAsAttribute(UnmanagedType.U1)]
   421         public bool HasNull;
   422 
   423         /// UCHAR->unsigned char
   424         [FieldOffset(17)]
   425         public byte Reserved;
   426 
   427         /// USHORT->unsigned short
   428         [FieldOffset(18)]
   429         public ushort BitSize;
   430 
   431         /// USHORT->unsigned short
   432         [FieldOffset(20)]
   433         public ushort ReportCount;
   434 
   435         /// USHORT[5]
   436         /// We had to use 5 ushorts instead of an array to avoid alignment exception issues.
   437         [FieldOffset(22)]
   438         public ushort Reserved21;
   439         [FieldOffset(24)]
   440         public ushort Reserved22;
   441         [FieldOffset(26)]
   442         public ushort Reserved23;
   443         [FieldOffset(28)]
   444         public ushort Reserved24;
   445         [FieldOffset(30)]
   446         public ushort Reserved25;
   447 
   448         /// ULONG->unsigned int
   449         [FieldOffset(32)]
   450         public uint UnitsExp;
   451 
   452         /// ULONG->unsigned int
   453         [FieldOffset(36)]
   454         public uint Units;
   455 
   456         /// LONG->int
   457         [FieldOffset(40)]
   458         public int LogicalMin;
   459 
   460         /// LONG->int
   461         [FieldOffset(44)]
   462         public int LogicalMax;
   463 
   464         /// LONG->int
   465         [FieldOffset(48)]
   466         public int PhysicalMin;
   467 
   468         /// LONG->int
   469         [FieldOffset(52)]
   470         public int PhysicalMax;
   471 
   472         /// Union Range/NotRange
   473         [FieldOffset(56)]
   474         public HIDP_VALUE_CAPS_RANGE Range;
   475 
   476         [FieldOffset(56)]
   477         public HIDP_VALUE_CAPS_NOT_RANGE NotRange;
   478     }
   479     
   480 }
   481 
   482