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