1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/Win32/Win32Hid.cs Sun Mar 15 20:25:58 2015 +0100
1.3 @@ -0,0 +1,515 @@
1.4 +//
1.5 +// Copyright (C) 2014-2015 Stéphane Lenclud.
1.6 +//
1.7 +// This file is part of SharpLibHid.
1.8 +//
1.9 +// SharpDisplayManager is free software: you can redistribute it and/or modify
1.10 +// it under the terms of the GNU General Public License as published by
1.11 +// the Free Software Foundation, either version 3 of the License, or
1.12 +// (at your option) any later version.
1.13 +//
1.14 +// SharpDisplayManager is distributed in the hope that it will be useful,
1.15 +// but WITHOUT ANY WARRANTY; without even the implied warranty of
1.16 +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.17 +// GNU General Public License for more details.
1.18 +//
1.19 +// You should have received a copy of the GNU General Public License
1.20 +// along with SharpDisplayManager. If not, see <http://www.gnu.org/licenses/>.
1.21 +//
1.22 +
1.23 +using System;
1.24 +using System.Runtime.InteropServices;
1.25 +using Microsoft.Win32.SafeHandles;
1.26 +using System.Text;
1.27 +
1.28 +namespace SharpLib.Win32
1.29 +{
1.30 +
1.31 + static partial class Function
1.32 + {
1.33 + [DllImport("hid.dll", CharSet = CharSet.Unicode)]
1.34 + 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);
1.35 +
1.36 + [DllImport("hid.dll", CharSet = CharSet.Auto, SetLastError = true)]
1.37 + public static extern Boolean HidD_GetManufacturerString(SafeFileHandle HidDeviceObject, StringBuilder Buffer, Int32 BufferLength);
1.38 +
1.39 + [DllImport("hid.dll", CharSet = CharSet.Auto, SetLastError = true)]
1.40 + public static extern Boolean HidD_GetProductString(SafeFileHandle HidDeviceObject, StringBuilder Buffer, Int32 BufferLength);
1.41 +
1.42 + [DllImport("hid.dll", CharSet = CharSet.Auto, SetLastError = true)]
1.43 + public static extern Boolean HidD_GetAttributes(SafeFileHandle HidDeviceObject, ref HIDD_ATTRIBUTES Attributes);
1.44 +
1.45 + /// Return Type: NTSTATUS->LONG->int
1.46 + ///PreparsedData: PHIDP_PREPARSED_DATA->_HIDP_PREPARSED_DATA*
1.47 + ///Capabilities: PHIDP_CAPS->_HIDP_CAPS*
1.48 + [DllImport("hid.dll", EntryPoint = "HidP_GetCaps", CallingConvention = CallingConvention.StdCall)]
1.49 + public static extern HidStatus HidP_GetCaps(System.IntPtr PreparsedData, ref HIDP_CAPS Capabilities);
1.50 +
1.51 + /// Return Type: NTSTATUS->LONG->int
1.52 + ///ReportType: HIDP_REPORT_TYPE->_HIDP_REPORT_TYPE
1.53 + ///ButtonCaps: PHIDP_BUTTON_CAPS->_HIDP_BUTTON_CAPS*
1.54 + ///ButtonCapsLength: PUSHORT->USHORT*
1.55 + ///PreparsedData: PHIDP_PREPARSED_DATA->_HIDP_PREPARSED_DATA*
1.56 + [DllImport("hid.dll", EntryPoint = "HidP_GetButtonCaps", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
1.57 + 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);
1.58 +
1.59 + /// Return Type: NTSTATUS->LONG->int
1.60 + ///ReportType: HIDP_REPORT_TYPE->_HIDP_REPORT_TYPE
1.61 + ///ValueCaps: PHIDP_VALUE_CAPS->_HIDP_VALUE_CAPS*
1.62 + ///ValueCapsLength: PUSHORT->USHORT*
1.63 + ///PreparsedData: PHIDP_PREPARSED_DATA->_HIDP_PREPARSED_DATA*
1.64 + [DllImport("hid.dll", EntryPoint = "HidP_GetValueCaps", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
1.65 + 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);
1.66 +
1.67 + /// Return Type: NTSTATUS->LONG->int
1.68 + ///ReportType: HIDP_REPORT_TYPE->_HIDP_REPORT_TYPE
1.69 + ///UsagePage: USAGE->USHORT->unsigned short
1.70 + ///LinkCollection: USHORT->unsigned short
1.71 + ///Usage: USAGE->USHORT->unsigned short
1.72 + ///UsageValue: PULONG->ULONG*
1.73 + ///PreparsedData: PHIDP_PREPARSED_DATA->_HIDP_PREPARSED_DATA*
1.74 + ///Report: PCHAR->CHAR*
1.75 + ///ReportLength: ULONG->unsigned int
1.76 + [DllImport("hid.dll", EntryPoint = "HidP_GetUsageValue", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
1.77 + 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);
1.78 +
1.79 +
1.80 +
1.81 + }
1.82 +
1.83 +
1.84 + static partial class Macro
1.85 + {
1.86 +
1.87 +
1.88 + }
1.89 +
1.90 +
1.91 + static partial class Const
1.92 + {
1.93 +
1.94 +
1.95 + }
1.96 +
1.97 +
1.98 + public enum HIDP_REPORT_TYPE : ushort
1.99 + {
1.100 + HidP_Input = 0,
1.101 + HidP_Output,
1.102 + HidP_Feature
1.103 + }
1.104 +
1.105 +
1.106 + public enum HidStatus : uint
1.107 + {
1.108 + HIDP_STATUS_SUCCESS = 0x110000,
1.109 + HIDP_STATUS_NULL = 0x80110001,
1.110 + HIDP_STATUS_INVALID_PREPARSED_DATA = 0xc0110001,
1.111 + HIDP_STATUS_INVALID_REPORT_TYPE = 0xc0110002,
1.112 + HIDP_STATUS_INVALID_REPORT_LENGTH = 0xc0110003,
1.113 + HIDP_STATUS_USAGE_NOT_FOUND = 0xc0110004,
1.114 + HIDP_STATUS_VALUE_OUT_OF_RANGE = 0xc0110005,
1.115 + HIDP_STATUS_BAD_LOG_PHY_VALUES = 0xc0110006,
1.116 + HIDP_STATUS_BUFFER_TOO_SMALL = 0xc0110007,
1.117 + HIDP_STATUS_INTERNAL_ERROR = 0xc0110008,
1.118 + HIDP_STATUS_I8042_TRANS_UNKNOWN = 0xc0110009,
1.119 + HIDP_STATUS_INCOMPATIBLE_REPORT_ID = 0xc011000a,
1.120 + HIDP_STATUS_NOT_VALUE_ARRAY = 0xc011000b,
1.121 + HIDP_STATUS_IS_VALUE_ARRAY = 0xc011000c,
1.122 + HIDP_STATUS_DATA_INDEX_NOT_FOUND = 0xc011000d,
1.123 + HIDP_STATUS_DATA_INDEX_OUT_OF_RANGE = 0xc011000e,
1.124 + HIDP_STATUS_BUTTON_NOT_PRESSED = 0xc011000f,
1.125 + HIDP_STATUS_REPORT_DOES_NOT_EXIST = 0xc0110010,
1.126 + HIDP_STATUS_NOT_IMPLEMENTED = 0xc0110020,
1.127 + HIDP_STATUS_I8242_TRANS_UNKNOWN = 0xc0110009
1.128 + }
1.129 +
1.130 +
1.131 + [StructLayout(LayoutKind.Sequential)]
1.132 + public struct USAGE_AND_PAGE
1.133 + {
1.134 + public ushort Usage;
1.135 + public ushort UsagePage;
1.136 + };
1.137 +
1.138 + [StructLayout(LayoutKind.Sequential)]
1.139 + public struct HIDD_ATTRIBUTES
1.140 + {
1.141 + public uint Size;
1.142 + public ushort VendorID;
1.143 + public ushort ProductID;
1.144 + public ushort VersionNumber;
1.145 + }
1.146 +
1.147 +
1.148 + [StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
1.149 + public struct HIDP_CAPS
1.150 + {
1.151 + /// USAGE->USHORT->unsigned short
1.152 + public ushort Usage;
1.153 +
1.154 + /// USAGE->USHORT->unsigned short
1.155 + public ushort UsagePage;
1.156 +
1.157 + /// USHORT->unsigned short
1.158 + public ushort InputReportByteLength;
1.159 +
1.160 + /// USHORT->unsigned short
1.161 + public ushort OutputReportByteLength;
1.162 +
1.163 + /// USHORT->unsigned short
1.164 + public ushort FeatureReportByteLength;
1.165 +
1.166 + /// USHORT[17]
1.167 + [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst = 17, ArraySubType = System.Runtime.InteropServices.UnmanagedType.U2)]
1.168 + public ushort[] Reserved;
1.169 +
1.170 + /// USHORT->unsigned short
1.171 + public ushort NumberLinkCollectionNodes;
1.172 +
1.173 + /// USHORT->unsigned short
1.174 + public ushort NumberInputButtonCaps;
1.175 +
1.176 + /// USHORT->unsigned short
1.177 + public ushort NumberInputValueCaps;
1.178 +
1.179 + /// USHORT->unsigned short
1.180 + public ushort NumberInputDataIndices;
1.181 +
1.182 + /// USHORT->unsigned short
1.183 + public ushort NumberOutputButtonCaps;
1.184 +
1.185 + /// USHORT->unsigned short
1.186 + public ushort NumberOutputValueCaps;
1.187 +
1.188 + /// USHORT->unsigned short
1.189 + public ushort NumberOutputDataIndices;
1.190 +
1.191 + /// USHORT->unsigned short
1.192 + public ushort NumberFeatureButtonCaps;
1.193 +
1.194 + /// USHORT->unsigned short
1.195 + public ushort NumberFeatureValueCaps;
1.196 +
1.197 + /// USHORT->unsigned short
1.198 + public ushort NumberFeatureDataIndices;
1.199 + }
1.200 +
1.201 + /// <summary>
1.202 + /// Type created in place of an anonymous struct
1.203 + /// </summary>
1.204 + [StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
1.205 + public struct HIDP_BUTTON_CAPS_RANGE
1.206 + {
1.207 +
1.208 + /// USAGE->USHORT->unsigned short
1.209 + public ushort UsageMin;
1.210 +
1.211 + /// USAGE->USHORT->unsigned short
1.212 + public ushort UsageMax;
1.213 +
1.214 + /// USHORT->unsigned short
1.215 + public ushort StringMin;
1.216 +
1.217 + /// USHORT->unsigned short
1.218 + public ushort StringMax;
1.219 +
1.220 + /// USHORT->unsigned short
1.221 + public ushort DesignatorMin;
1.222 +
1.223 + /// USHORT->unsigned short
1.224 + public ushort DesignatorMax;
1.225 +
1.226 + /// USHORT->unsigned short
1.227 + public ushort DataIndexMin;
1.228 +
1.229 + /// USHORT->unsigned short
1.230 + public ushort DataIndexMax;
1.231 + }
1.232 +
1.233 + /// <summary>
1.234 + /// Type created in place of an anonymous struct
1.235 + /// </summary>
1.236 + [StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
1.237 + public struct HIDP_BUTTON_CAPS_NOT_RANGE
1.238 + {
1.239 +
1.240 + /// USAGE->USHORT->unsigned short
1.241 + public ushort Usage;
1.242 +
1.243 + /// USAGE->USHORT->unsigned short
1.244 + public ushort Reserved1;
1.245 +
1.246 + /// USHORT->unsigned short
1.247 + public ushort StringIndex;
1.248 +
1.249 + /// USHORT->unsigned short
1.250 + public ushort Reserved2;
1.251 +
1.252 + /// USHORT->unsigned short
1.253 + public ushort DesignatorIndex;
1.254 +
1.255 + /// USHORT->unsigned short
1.256 + public ushort Reserved3;
1.257 +
1.258 + /// USHORT->unsigned short
1.259 + public ushort DataIndex;
1.260 +
1.261 + /// USHORT->unsigned short
1.262 + public ushort Reserved4;
1.263 + }
1.264 +
1.265 + /// <summary>
1.266 + ///
1.267 + /// </summary>
1.268 + [StructLayout(LayoutKind.Explicit, Pack = 1)]
1.269 + public struct HIDP_BUTTON_CAPS
1.270 + {
1.271 + /// USAGE->USHORT->unsigned short
1.272 + [FieldOffset(0)]
1.273 + public ushort UsagePage;
1.274 +
1.275 + /// UCHAR->unsigned char
1.276 + [FieldOffset(2)]
1.277 + public byte ReportID;
1.278 +
1.279 + /// BOOLEAN->BYTE->unsigned char
1.280 + [FieldOffset(3)]
1.281 + [MarshalAs(UnmanagedType.U1)]
1.282 + public bool IsAlias;
1.283 +
1.284 + /// USHORT->unsigned short
1.285 + [FieldOffset(4)]
1.286 + public ushort BitField;
1.287 +
1.288 + /// USHORT->unsigned short
1.289 + [FieldOffset(6)]
1.290 + public ushort LinkCollection;
1.291 +
1.292 + /// USAGE->USHORT->unsigned short
1.293 + [FieldOffset(8)]
1.294 + public ushort LinkUsage;
1.295 +
1.296 + /// USAGE->USHORT->unsigned short
1.297 + [FieldOffset(10)]
1.298 + public ushort LinkUsagePage;
1.299 +
1.300 + /// BOOLEAN->BYTE->unsigned char
1.301 + [FieldOffset(12)]
1.302 + [MarshalAs(UnmanagedType.U1)]
1.303 + public bool IsRange;
1.304 +
1.305 + /// BOOLEAN->BYTE->unsigned char
1.306 + [FieldOffset(13)]
1.307 + [MarshalAs(UnmanagedType.U1)]
1.308 + public bool IsStringRange;
1.309 +
1.310 + /// BOOLEAN->BYTE->unsigned char
1.311 + [FieldOffset(14)]
1.312 + [MarshalAs(UnmanagedType.U1)]
1.313 + public bool IsDesignatorRange;
1.314 +
1.315 + /// BOOLEAN->BYTE->unsigned char
1.316 + [FieldOffset(15)]
1.317 + [MarshalAs(UnmanagedType.U1)]
1.318 + public bool IsAbsolute;
1.319 +
1.320 + /// ULONG[10]
1.321 + [FieldOffset(16)]
1.322 + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10, ArraySubType = UnmanagedType.U4)]
1.323 + public uint[] Reserved;
1.324 +
1.325 + /// Union Range/NotRange
1.326 + [FieldOffset(56)]
1.327 + public HIDP_BUTTON_CAPS_RANGE Range;
1.328 +
1.329 + [FieldOffset(56)]
1.330 + public HIDP_BUTTON_CAPS_NOT_RANGE NotRange;
1.331 + }
1.332 +
1.333 + /// <summary>
1.334 + /// Type created in place of an anonymous struct
1.335 + /// </summary>
1.336 + [StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
1.337 + public struct HIDP_VALUE_CAPS_RANGE
1.338 + {
1.339 +
1.340 + /// USAGE->USHORT->unsigned short
1.341 + public ushort UsageMin;
1.342 +
1.343 + /// USAGE->USHORT->unsigned short
1.344 + public ushort UsageMax;
1.345 +
1.346 + /// USHORT->unsigned short
1.347 + public ushort StringMin;
1.348 +
1.349 + /// USHORT->unsigned short
1.350 + public ushort StringMax;
1.351 +
1.352 + /// USHORT->unsigned short
1.353 + public ushort DesignatorMin;
1.354 +
1.355 + /// USHORT->unsigned short
1.356 + public ushort DesignatorMax;
1.357 +
1.358 + /// USHORT->unsigned short
1.359 + public ushort DataIndexMin;
1.360 +
1.361 + /// USHORT->unsigned short
1.362 + public ushort DataIndexMax;
1.363 + }
1.364 +
1.365 + /// <summary>
1.366 + /// Type created in place of an anonymous struct
1.367 + /// </summary>
1.368 + [StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
1.369 + public struct HIDP_VALUE_CAPS_NOT_RANGE
1.370 + {
1.371 +
1.372 + /// USAGE->USHORT->unsigned short
1.373 + public ushort Usage;
1.374 +
1.375 + /// USAGE->USHORT->unsigned short
1.376 + public ushort Reserved1;
1.377 +
1.378 + /// USHORT->unsigned short
1.379 + public ushort StringIndex;
1.380 +
1.381 + /// USHORT->unsigned short
1.382 + public ushort Reserved2;
1.383 +
1.384 + /// USHORT->unsigned short
1.385 + public ushort DesignatorIndex;
1.386 +
1.387 + /// USHORT->unsigned short
1.388 + public ushort Reserved3;
1.389 +
1.390 + /// USHORT->unsigned short
1.391 + public ushort DataIndex;
1.392 +
1.393 + /// USHORT->unsigned short
1.394 + public ushort Reserved4;
1.395 + }
1.396 +
1.397 +
1.398 + /// <summary>
1.399 + ///
1.400 + /// </summary>
1.401 + [StructLayout(LayoutKind.Explicit, Pack = 1)]
1.402 + public struct HIDP_VALUE_CAPS
1.403 + {
1.404 +
1.405 + /// USAGE->USHORT->unsigned short
1.406 + [FieldOffset(0)]
1.407 + public ushort UsagePage;
1.408 +
1.409 + /// UCHAR->unsigned char
1.410 + [FieldOffset(2)]
1.411 + public byte ReportID;
1.412 +
1.413 + /// BOOLEAN->BYTE->unsigned char
1.414 + [FieldOffset(3)]
1.415 + [MarshalAs(UnmanagedType.U1)]
1.416 + public bool IsAlias;
1.417 +
1.418 + /// USHORT->unsigned short
1.419 + [FieldOffset(4)]
1.420 + public ushort BitField;
1.421 +
1.422 + /// USHORT->unsigned short
1.423 + [FieldOffset(6)]
1.424 + public ushort LinkCollection;
1.425 +
1.426 + /// USAGE->USHORT->unsigned short
1.427 + [FieldOffset(8)]
1.428 + public ushort LinkUsage;
1.429 +
1.430 + /// USAGE->USHORT->unsigned short
1.431 + [FieldOffset(10)]
1.432 + public ushort LinkUsagePage;
1.433 +
1.434 + /// BOOLEAN->BYTE->unsigned char
1.435 + [FieldOffset(12)]
1.436 + [MarshalAs(UnmanagedType.U1)]
1.437 + public bool IsRange;
1.438 +
1.439 + /// BOOLEAN->BYTE->unsigned char
1.440 + [FieldOffset(13)]
1.441 + [MarshalAs(UnmanagedType.U1)]
1.442 + public bool IsStringRange;
1.443 +
1.444 + /// BOOLEAN->BYTE->unsigned char
1.445 + [FieldOffset(14)]
1.446 + [MarshalAs(UnmanagedType.U1)]
1.447 + public bool IsDesignatorRange;
1.448 +
1.449 + /// BOOLEAN->BYTE->unsigned char
1.450 + [FieldOffset(15)]
1.451 + [MarshalAs(UnmanagedType.U1)]
1.452 + public bool IsAbsolute;
1.453 +
1.454 + /// BOOLEAN->BYTE->unsigned char
1.455 + [FieldOffset(16)]
1.456 + [MarshalAs(UnmanagedType.U1)]
1.457 + public bool HasNull;
1.458 +
1.459 + /// UCHAR->unsigned char
1.460 + [FieldOffset(17)]
1.461 + public byte Reserved;
1.462 +
1.463 + /// USHORT->unsigned short
1.464 + [FieldOffset(18)]
1.465 + public ushort BitSize;
1.466 +
1.467 + /// USHORT->unsigned short
1.468 + [FieldOffset(20)]
1.469 + public ushort ReportCount;
1.470 +
1.471 + /// USHORT[5]
1.472 + /// We had to use 5 ushorts instead of an array to avoid alignment exception issues.
1.473 + [FieldOffset(22)]
1.474 + public ushort Reserved21;
1.475 + [FieldOffset(24)]
1.476 + public ushort Reserved22;
1.477 + [FieldOffset(26)]
1.478 + public ushort Reserved23;
1.479 + [FieldOffset(28)]
1.480 + public ushort Reserved24;
1.481 + [FieldOffset(30)]
1.482 + public ushort Reserved25;
1.483 +
1.484 + /// ULONG->unsigned int
1.485 + [FieldOffset(32)]
1.486 + public uint UnitsExp;
1.487 +
1.488 + /// ULONG->unsigned int
1.489 + [FieldOffset(36)]
1.490 + public uint Units;
1.491 +
1.492 + /// LONG->int
1.493 + [FieldOffset(40)]
1.494 + public int LogicalMin;
1.495 +
1.496 + /// LONG->int
1.497 + [FieldOffset(44)]
1.498 + public int LogicalMax;
1.499 +
1.500 + /// LONG->int
1.501 + [FieldOffset(48)]
1.502 + public int PhysicalMin;
1.503 +
1.504 + /// LONG->int
1.505 + [FieldOffset(52)]
1.506 + public int PhysicalMax;
1.507 +
1.508 + /// Union Range/NotRange
1.509 + [FieldOffset(56)]
1.510 + public HIDP_VALUE_CAPS_RANGE Range;
1.511 +
1.512 + [FieldOffset(56)]
1.513 + public HIDP_VALUE_CAPS_NOT_RANGE NotRange;
1.514 + }
1.515 +
1.516 +}
1.517 +
1.518 +