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