WIP
DirectOutput framework for virtual pinball cabinets WIP
Go to:
Overview 
HIDImports.cs
Go to the documentation of this file.
1 // HIDImports.cs
3 // For more information: http://wiimotelib.codeplex.com/
5 using System;
6 using System.IO;
7 using System.Runtime.InteropServices;
8 using Microsoft.Win32.SafeHandles;
9 
10 namespace DirectOutput.Cab.Out.PS
11 {
12  public class HIDImports
13  {
14  // Flags controlling what is included in the device information set built by SetupDiGetClassDevs
15  internal const int DIGCF_DEFAULT = 0x00000001; // only valid with DIGCF_DEVICEINTERFACE
16  internal const int DIGCF_PRESENT = 0x00000002;
17  internal const int DIGCF_ALLCLASSES = 0x00000004;
18  internal const int DIGCF_PROFILE = 0x00000008;
19  internal const int DIGCF_DEVICEINTERFACE = 0x00000010;
20 
21  [Flags]
22  internal enum EFileAttributes : uint
23  {
24  Readonly = 0x00000001,
25  Hidden = 0x00000002,
26  System = 0x00000004,
27  Directory = 0x00000010,
28  Archive = 0x00000020,
29  Device = 0x00000040,
30  Normal = 0x00000080,
31  Temporary = 0x00000100,
32  SparseFile = 0x00000200,
33  ReparsePoint = 0x00000400,
34  Compressed = 0x00000800,
35  Offline = 0x00001000,
36  NotContentIndexed = 0x00002000,
37  Encrypted = 0x00004000,
38  Write_Through = 0x80000000,
39  Overlapped = 0x40000000,
40  NoBuffering = 0x20000000,
41  RandomAccess = 0x10000000,
42  SequentialScan = 0x08000000,
43  DeleteOnClose = 0x04000000,
44  BackupSemantics = 0x02000000,
45  PosixSemantics = 0x01000000,
46  OpenReparsePoint = 0x00200000,
47  OpenNoRecall = 0x00100000,
48  FirstPipeInstance = 0x00080000
49  }
50 
51  [StructLayout(LayoutKind.Sequential)]
52  internal struct SP_DEVINFO_DATA
53  {
54  public uint cbSize;
55  public Guid ClassGuid;
56  public uint DevInst;
57  public IntPtr Reserved;
58  }
59 
60  [StructLayout(LayoutKind.Sequential)]
61  internal struct SP_DEVICE_INTERFACE_DATA
62  {
63  public int cbSize;
64  public Guid InterfaceClassGuid;
65  public int Flags;
66  public IntPtr RESERVED;
67  }
68 
69  [StructLayout(LayoutKind.Sequential, Pack = 1)]
70  internal struct SP_DEVICE_INTERFACE_DETAIL_DATA
71  {
72  public UInt32 cbSize;
73  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
74  public string DevicePath;
75  }
76 
77  [StructLayout(LayoutKind.Sequential)]
78  public struct HIDD_ATTRIBUTES
79  {
80  public int Size;
81  public short VendorID;
82  public short ProductID;
83  public short VersionNumber;
84  }
85 
86  [DllImport(@"hid.dll", CharSet = CharSet.Auto, SetLastError = true)]
87  internal static extern void HidD_GetHidGuid(out Guid gHid);
88 
89  [DllImport("hid.dll")]
90  internal static extern Boolean HidD_GetAttributes(IntPtr HidDeviceObject, ref HIDD_ATTRIBUTES Attributes);
91 
92  [DllImport("hid.dll")]
93  internal extern static bool HidD_SetOutputReport(
94  IntPtr HidDeviceObject,
95  byte[] lpReportBuffer,
96  uint ReportBufferLength);
97 
98  [DllImport("hid.dll")]
99  internal extern static bool HidD_GetInputReport(
100  IntPtr HidDeviceObject,
101  byte[] lpREportBuffer,
102  uint ReportBufferLength);
103 
104  [DllImport("hid.dll")]
105  internal extern static bool HidD_GetProductString(
106  IntPtr HidDeviceObject,
107  byte[] Buffer,
108  uint BufferLength);
109 
110  [DllImport(@"setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
111  internal static extern IntPtr SetupDiGetClassDevs(
112  ref Guid ClassGuid,
113  [MarshalAs(UnmanagedType.LPTStr)] string Enumerator,
114  IntPtr hwndParent,
115  UInt32 Flags
116  );
117 
118  [DllImport(@"setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
119  internal static extern Boolean SetupDiEnumDeviceInterfaces(
120  IntPtr hDevInfo,
121  IntPtr devInvo,
122  ref Guid interfaceClassGuid,
123  UInt32 memberIndex,
124  ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData
125  );
126 
127  [DllImport(@"setupapi.dll", SetLastError = true)]
128  internal static extern Boolean SetupDiGetDeviceInterfaceDetail(
129  IntPtr hDevInfo,
130  ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData,
131  IntPtr deviceInterfaceDetailData,
132  UInt32 deviceInterfaceDetailDataSize,
133  out UInt32 requiredSize,
134  IntPtr deviceInfoData
135  );
136 
137  [DllImport(@"setupapi.dll", SetLastError = true)]
138  internal static extern Boolean SetupDiGetDeviceInterfaceDetail(
139  IntPtr hDevInfo,
140  ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData,
141  ref SP_DEVICE_INTERFACE_DETAIL_DATA deviceInterfaceDetailData,
142  UInt32 deviceInterfaceDetailDataSize,
143  out UInt32 requiredSize,
144  IntPtr deviceInfoData
145  );
146 
147  [DllImport(@"setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
148  internal static extern UInt16 SetupDiDestroyDeviceInfoList(IntPtr hDevInfo);
149 
150  [DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
151  internal static extern IntPtr CreateFile(
152  string fileName,
153  [MarshalAs(UnmanagedType.U4)] UInt32 fileAccess,
154  [MarshalAs(UnmanagedType.U4)] UInt32 fileShare,
155  IntPtr securityAttributes,
156  [MarshalAs(UnmanagedType.U4)] FileMode creationDisposition,
157  [MarshalAs(UnmanagedType.U4)] EFileAttributes flags,
158  IntPtr template);
159 
160  public const uint GENERIC_READ = 0x80000000;
161  public const uint GENERIC_WRITE = 0x40000000;
162  public const uint GENERIC_READ_WRITE = GENERIC_READ | GENERIC_WRITE;
163 
164  public const uint SHARE_NONE = 0;
165  public const uint SHARE_READ = 1;
166  public const uint SHARE_WRITE = 2;
167  public const uint SHARE_READ_WRITE = SHARE_READ | SHARE_WRITE;
168  public const uint SHARE_DELETE = 4;
169 
170  [DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
171  internal static extern int WriteFile(
172  IntPtr hFile,
173  byte[] lpBuffer,
174  UInt32 bytesToWrite,
175  out UInt32 bytesWritten,
176  ref System.Threading.NativeOverlapped lpOverlapped);
177 
178  [DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
179  internal static extern int ReadFile(
180  IntPtr hFile,
181  byte[] lpBuffer,
182  UInt32 bytesToRead,
183  out UInt32 bytesRead,
184  ref System.Threading.NativeOverlapped lpOverlapped);
185 
186  [DllImport("kernel32.dll", SetLastError = true)]
187  [return: MarshalAs(UnmanagedType.Bool)]
188  public static extern bool CloseHandle(IntPtr hObject);
189 
190  [StructLayout(LayoutKind.Sequential)]
191  internal struct HIDP_PREPARSED_DATA
192  {
193  public int cbSize;
194  public Guid InterfaceClassGuid;
195  public int Flags;
196  public IntPtr RESERVED;
197  }
198 
199  [StructLayout(LayoutKind.Sequential)]
200  internal struct HIDP_CAPS
201  {
202  public ushort Usage;
203  public ushort UsagePage;
204  public ushort InputReportByteLength;
205  public ushort OutputReportByteLength;
206  public ushort FeatureReportByteLength;
207  public ushort Reserved0;
208  public ushort Reserved1;
209  public ushort Reserved2;
210  public ushort Reserved3;
211  public ushort Reserved4;
212  public ushort Reserved5;
213  public ushort Reserved6;
214  public ushort Reserved7;
215  public ushort Reserved8;
216  public ushort Reserved9;
217  public ushort Reserved10;
218  public ushort Reserved11;
219  public ushort Reserved12;
220  public ushort Reserved13;
221  public ushort Reserved14;
222  public ushort Reserved15;
223  public ushort Reserved16;
224  public ushort NumberLinkCollectionNodes;
225  public ushort NumberInputButtonCaps;
226  public ushort NumberInputValueCaps;
227  public ushort NumberInputDataIndices;
228  public ushort NumberOutputButtonCaps;
229  public ushort NumberOutputValueCaps;
230  public ushort NumberOutputDataIndices;
231  public ushort NumberFeatureButtonCaps;
232  public ushort NumberFeatureValueCaps;
233  public ushort NumberFeatureDataIndices;
234  }
235 
236  [DllImport("Hid.dll", SetLastError = true, CharSet = CharSet.Auto)]
237  internal static extern bool HidD_GetPreparsedData(
238  IntPtr hFile,
239  out IntPtr pp);
240 
241  [DllImport("Hid.dll", SetLastError = true, CharSet = CharSet.Auto)]
242  internal static extern bool HidD_FreePreparsedData(
243  IntPtr pp);
244 
245  [DllImport("Hid.dll", SetLastError = true, CharSet = CharSet.Auto)]
246  internal static extern bool HidP_GetCaps(
247  IntPtr pp,
248  ref HIDP_CAPS caps);
249 
250 
251  }
252 }
253