Win32Hid.cs
author StephaneLenclud
Sun, 15 Feb 2015 21:48:07 +0100
changeset 63 4b8b058de215
parent 58 7ef0f9dc229c
child 64 8c2380995bb7
permissions -rw-r--r--
HidDevice: Adding friendly name property.
     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 
   116         /// USAGE->USHORT->unsigned short
   117         public ushort Usage;
   118 
   119         /// USAGE->USHORT->unsigned short
   120         public ushort UsagePage;
   121 
   122         /// USHORT->unsigned short
   123         public ushort InputReportByteLength;
   124 
   125         /// USHORT->unsigned short
   126         public ushort OutputReportByteLength;
   127 
   128         /// USHORT->unsigned short
   129         public ushort FeatureReportByteLength;
   130 
   131         /// USHORT[17]
   132         [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst = 17, ArraySubType = System.Runtime.InteropServices.UnmanagedType.U2)]
   133         public ushort[] Reserved;
   134 
   135         /// USHORT->unsigned short
   136         public ushort NumberLinkCollectionNodes;
   137 
   138         /// USHORT->unsigned short
   139         public ushort NumberInputButtonCaps;
   140 
   141         /// USHORT->unsigned short
   142         public ushort NumberInputValueCaps;
   143 
   144         /// USHORT->unsigned short
   145         public ushort NumberInputDataIndices;
   146 
   147         /// USHORT->unsigned short
   148         public ushort NumberOutputButtonCaps;
   149 
   150         /// USHORT->unsigned short
   151         public ushort NumberOutputValueCaps;
   152 
   153         /// USHORT->unsigned short
   154         public ushort NumberOutputDataIndices;
   155 
   156         /// USHORT->unsigned short
   157         public ushort NumberFeatureButtonCaps;
   158 
   159         /// USHORT->unsigned short
   160         public ushort NumberFeatureValueCaps;
   161 
   162         /// USHORT->unsigned short
   163         public ushort NumberFeatureDataIndices;
   164     }
   165 
   166     /// <summary>
   167     /// Type created in place of an anonymous struct
   168     /// </summary>
   169     [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
   170     public struct HIDP_BUTTON_CAPS_RANGE
   171     {
   172 
   173         /// USAGE->USHORT->unsigned short
   174         public ushort UsageMin;
   175 
   176         /// USAGE->USHORT->unsigned short
   177         public ushort UsageMax;
   178 
   179         /// USHORT->unsigned short
   180         public ushort StringMin;
   181 
   182         /// USHORT->unsigned short
   183         public ushort StringMax;
   184 
   185         /// USHORT->unsigned short
   186         public ushort DesignatorMin;
   187 
   188         /// USHORT->unsigned short
   189         public ushort DesignatorMax;
   190 
   191         /// USHORT->unsigned short
   192         public ushort DataIndexMin;
   193 
   194         /// USHORT->unsigned short
   195         public ushort DataIndexMax;
   196     }
   197 
   198     /// <summary>
   199     /// Type created in place of an anonymous struct
   200     /// </summary>
   201     [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
   202     public struct HIDP_BUTTON_CAPS_NOT_RANGE
   203     {
   204 
   205         /// USAGE->USHORT->unsigned short
   206         public ushort Usage;
   207 
   208         /// USAGE->USHORT->unsigned short
   209         public ushort Reserved1;
   210 
   211         /// USHORT->unsigned short
   212         public ushort StringIndex;
   213 
   214         /// USHORT->unsigned short
   215         public ushort Reserved2;
   216 
   217         /// USHORT->unsigned short
   218         public ushort DesignatorIndex;
   219 
   220         /// USHORT->unsigned short
   221         public ushort Reserved3;
   222 
   223         /// USHORT->unsigned short
   224         public ushort DataIndex;
   225 
   226         /// USHORT->unsigned short
   227         public ushort Reserved4;
   228     }
   229 
   230     /// <summary>
   231     /// 
   232     /// </summary>
   233     [StructLayoutAttribute(LayoutKind.Explicit, Pack = 1)]
   234     public struct HIDP_BUTTON_CAPS
   235     {
   236         /// USAGE->USHORT->unsigned short
   237         [FieldOffset(0)]
   238         public ushort UsagePage;
   239 
   240         /// UCHAR->unsigned char
   241         [FieldOffset(2)]
   242         public byte ReportID;
   243 
   244         /// BOOLEAN->BYTE->unsigned char
   245         [FieldOffset(3)]
   246         [MarshalAsAttribute(UnmanagedType.U1)]
   247         public bool IsAlias;
   248 
   249         /// USHORT->unsigned short
   250         [FieldOffset(4)]
   251         public ushort BitField;
   252 
   253         /// USHORT->unsigned short
   254         [FieldOffset(6)]
   255         public ushort LinkCollection;
   256 
   257         /// USAGE->USHORT->unsigned short
   258         [FieldOffset(8)]
   259         public ushort LinkUsage;
   260 
   261         /// USAGE->USHORT->unsigned short
   262         [FieldOffset(10)]
   263         public ushort LinkUsagePage;
   264 
   265         /// BOOLEAN->BYTE->unsigned char
   266         [FieldOffset(12)]
   267         [MarshalAsAttribute(UnmanagedType.U1)]
   268         public bool IsRange;
   269 
   270         /// BOOLEAN->BYTE->unsigned char
   271         [FieldOffset(13)]
   272         [MarshalAsAttribute(UnmanagedType.U1)]
   273         public bool IsStringRange;
   274 
   275         /// BOOLEAN->BYTE->unsigned char
   276         [FieldOffset(14)]
   277         [MarshalAsAttribute(UnmanagedType.U1)]
   278         public bool IsDesignatorRange;
   279 
   280         /// BOOLEAN->BYTE->unsigned char
   281         [FieldOffset(15)]
   282         [MarshalAsAttribute(UnmanagedType.U1)]
   283         public bool IsAbsolute;
   284 
   285         /// ULONG[10]
   286         [FieldOffset(16)]
   287         [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 10, ArraySubType = UnmanagedType.U4)]
   288         public uint[] Reserved;
   289 
   290         /// Union Range/NotRange
   291         [FieldOffset(56)]
   292         HIDP_BUTTON_CAPS_RANGE Range;
   293 
   294         [FieldOffset(56)]
   295         HIDP_BUTTON_CAPS_NOT_RANGE NotRange;       
   296     }
   297 
   298     /// <summary>
   299     /// Type created in place of an anonymous struct
   300     /// </summary>
   301     [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
   302     public struct HIDP_VALUE_CAPS_RANGE
   303     {
   304 
   305         /// USAGE->USHORT->unsigned short
   306         public ushort UsageMin;
   307 
   308         /// USAGE->USHORT->unsigned short
   309         public ushort UsageMax;
   310 
   311         /// USHORT->unsigned short
   312         public ushort StringMin;
   313 
   314         /// USHORT->unsigned short
   315         public ushort StringMax;
   316 
   317         /// USHORT->unsigned short
   318         public ushort DesignatorMin;
   319 
   320         /// USHORT->unsigned short
   321         public ushort DesignatorMax;
   322 
   323         /// USHORT->unsigned short
   324         public ushort DataIndexMin;
   325 
   326         /// USHORT->unsigned short
   327         public ushort DataIndexMax;
   328     }
   329 
   330     /// <summary>
   331     /// Type created in place of an anonymous struct
   332     /// </summary>
   333     [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
   334     public struct HIDP_VALUE_CAPS_NOT_RANGE
   335     {
   336 
   337         /// USAGE->USHORT->unsigned short
   338         public ushort Usage;
   339 
   340         /// USAGE->USHORT->unsigned short
   341         public ushort Reserved1;
   342 
   343         /// USHORT->unsigned short
   344         public ushort StringIndex;
   345 
   346         /// USHORT->unsigned short
   347         public ushort Reserved2;
   348 
   349         /// USHORT->unsigned short
   350         public ushort DesignatorIndex;
   351 
   352         /// USHORT->unsigned short
   353         public ushort Reserved3;
   354 
   355         /// USHORT->unsigned short
   356         public ushort DataIndex;
   357 
   358         /// USHORT->unsigned short
   359         public ushort Reserved4;
   360     }
   361 
   362 
   363     /// <summary>
   364     /// 
   365     /// </summary>
   366     [StructLayoutAttribute(LayoutKind.Explicit, Pack = 1)]
   367     public struct HIDP_VALUE_CAPS
   368     {
   369 
   370         /// USAGE->USHORT->unsigned short
   371         [FieldOffset(0)]
   372         public ushort UsagePage;
   373 
   374         /// UCHAR->unsigned char
   375         [FieldOffset(2)]
   376         public byte ReportID;
   377 
   378         /// BOOLEAN->BYTE->unsigned char
   379         [FieldOffset(3)]
   380         [MarshalAsAttribute(UnmanagedType.U1)]
   381         public bool IsAlias;
   382 
   383         /// USHORT->unsigned short
   384         [FieldOffset(4)]
   385         public ushort BitField;
   386 
   387         /// USHORT->unsigned short
   388         [FieldOffset(6)]
   389         public ushort LinkCollection;
   390 
   391         /// USAGE->USHORT->unsigned short
   392         [FieldOffset(8)]
   393         public ushort LinkUsage;
   394 
   395         /// USAGE->USHORT->unsigned short
   396         [FieldOffset(10)]
   397         public ushort LinkUsagePage;
   398 
   399         /// BOOLEAN->BYTE->unsigned char
   400         [FieldOffset(12)]
   401         [MarshalAsAttribute(UnmanagedType.U1)]
   402         public bool IsRange;
   403 
   404         /// BOOLEAN->BYTE->unsigned char
   405         [FieldOffset(13)]
   406         [MarshalAsAttribute(UnmanagedType.U1)]
   407         public bool IsStringRange;
   408 
   409         /// BOOLEAN->BYTE->unsigned char
   410         [FieldOffset(14)]
   411         [MarshalAsAttribute(UnmanagedType.U1)]
   412         public bool IsDesignatorRange;
   413 
   414         /// BOOLEAN->BYTE->unsigned char
   415         [FieldOffset(15)]
   416         [MarshalAsAttribute(UnmanagedType.U1)]
   417         public bool IsAbsolute;
   418 
   419         /// BOOLEAN->BYTE->unsigned char
   420         [FieldOffset(16)]
   421         [MarshalAsAttribute(UnmanagedType.U1)]
   422         public bool HasNull;
   423 
   424         /// UCHAR->unsigned char
   425         [FieldOffset(17)]
   426         public byte Reserved;
   427 
   428         /// USHORT->unsigned short
   429         [FieldOffset(18)]
   430         public ushort BitSize;
   431 
   432         /// USHORT->unsigned short
   433         [FieldOffset(20)]
   434         public ushort ReportCount;
   435 
   436         /// USHORT[5]
   437         /// We had to use 5 ushorts instead of an array to avoid alignment exception issues.
   438         [FieldOffset(22)]
   439         public ushort Reserved21;
   440         [FieldOffset(24)]
   441         public ushort Reserved22;
   442         [FieldOffset(26)]
   443         public ushort Reserved23;
   444         [FieldOffset(28)]
   445         public ushort Reserved24;
   446         [FieldOffset(30)]
   447         public ushort Reserved25;
   448 
   449         /// ULONG->unsigned int
   450         [FieldOffset(32)]
   451         public uint UnitsExp;
   452 
   453         /// ULONG->unsigned int
   454         [FieldOffset(36)]
   455         public uint Units;
   456 
   457         /// LONG->int
   458         [FieldOffset(40)]
   459         public int LogicalMin;
   460 
   461         /// LONG->int
   462         [FieldOffset(44)]
   463         public int LogicalMax;
   464 
   465         /// LONG->int
   466         [FieldOffset(48)]
   467         public int PhysicalMin;
   468 
   469         /// LONG->int
   470         [FieldOffset(52)]
   471         public int PhysicalMax;
   472 
   473         /// Union Range/NotRange
   474         [FieldOffset(56)]
   475         public HIDP_VALUE_CAPS_RANGE Range;
   476 
   477         [FieldOffset(56)]
   478         public HIDP_VALUE_CAPS_NOT_RANGE NotRange;
   479     }
   480     
   481 }
   482 
   483