| 1 | ////////////////////////////////////////////////////////////////////////////////// |
|---|
| 2 | // HIDImports.cs |
|---|
| 3 | // Managed Wiimote Library |
|---|
| 4 | // Written by Brian Peek (http://www.brianpeek.com/) |
|---|
| 5 | // for MSDN's Coding4Fun (http://msdn.microsoft.com/coding4fun/) |
|---|
| 6 | // Visit http://msdn.microsoft.com/coding4fun/hardware/article.aspx?articleid=1879033 |
|---|
| 7 | // for more information |
|---|
| 8 | ////////////////////////////////////////////////////////////////////////////////// |
|---|
| 9 | |
|---|
| 10 | using System; |
|---|
| 11 | using System.Runtime.InteropServices; |
|---|
| 12 | using System.IO; |
|---|
| 13 | using Microsoft.Win32.SafeHandles; |
|---|
| 14 | |
|---|
| 15 | namespace WiimoteLib |
|---|
| 16 | { |
|---|
| 17 | /// <summary> |
|---|
| 18 | /// Win32 import information for use with the Wiimote library |
|---|
| 19 | /// </summary> |
|---|
| 20 | class HIDImports |
|---|
| 21 | { |
|---|
| 22 | // |
|---|
| 23 | // Flags controlling what is included in the device information set built |
|---|
| 24 | // by SetupDiGetClassDevs |
|---|
| 25 | // |
|---|
| 26 | public const int DIGCF_DEFAULT = 0x00000001; // only valid with DIGCF_DEVICEINTERFACE |
|---|
| 27 | public const int DIGCF_PRESENT = 0x00000002; |
|---|
| 28 | public const int DIGCF_ALLCLASSES = 0x00000004; |
|---|
| 29 | public const int DIGCF_PROFILE = 0x00000008; |
|---|
| 30 | public const int DIGCF_DEVICEINTERFACE = 0x00000010; |
|---|
| 31 | |
|---|
| 32 | [Flags] |
|---|
| 33 | public enum EFileAttributes : uint |
|---|
| 34 | { |
|---|
| 35 | Readonly = 0x00000001, |
|---|
| 36 | Hidden = 0x00000002, |
|---|
| 37 | System = 0x00000004, |
|---|
| 38 | Directory = 0x00000010, |
|---|
| 39 | Archive = 0x00000020, |
|---|
| 40 | Device = 0x00000040, |
|---|
| 41 | Normal = 0x00000080, |
|---|
| 42 | Temporary = 0x00000100, |
|---|
| 43 | SparseFile = 0x00000200, |
|---|
| 44 | ReparsePoint = 0x00000400, |
|---|
| 45 | Compressed = 0x00000800, |
|---|
| 46 | Offline = 0x00001000, |
|---|
| 47 | NotContentIndexed= 0x00002000, |
|---|
| 48 | Encrypted = 0x00004000, |
|---|
| 49 | Write_Through = 0x80000000, |
|---|
| 50 | Overlapped = 0x40000000, |
|---|
| 51 | NoBuffering = 0x20000000, |
|---|
| 52 | RandomAccess = 0x10000000, |
|---|
| 53 | SequentialScan = 0x08000000, |
|---|
| 54 | DeleteOnClose = 0x04000000, |
|---|
| 55 | BackupSemantics = 0x02000000, |
|---|
| 56 | PosixSemantics = 0x01000000, |
|---|
| 57 | OpenReparsePoint = 0x00200000, |
|---|
| 58 | OpenNoRecall = 0x00100000, |
|---|
| 59 | FirstPipeInstance= 0x00080000 |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | [StructLayout(LayoutKind.Sequential)] |
|---|
| 63 | public struct SP_DEVINFO_DATA |
|---|
| 64 | { |
|---|
| 65 | public uint cbSize; |
|---|
| 66 | public Guid ClassGuid; |
|---|
| 67 | public uint DevInst; |
|---|
| 68 | public IntPtr Reserved; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | [StructLayout(LayoutKind.Sequential)] |
|---|
| 72 | public struct SP_DEVICE_INTERFACE_DATA |
|---|
| 73 | { |
|---|
| 74 | public int cbSize; |
|---|
| 75 | public Guid InterfaceClassGuid; |
|---|
| 76 | public int Flags; |
|---|
| 77 | public IntPtr RESERVED; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | [StructLayout(LayoutKind.Sequential, Pack = 1)] |
|---|
| 81 | public struct SP_DEVICE_INTERFACE_DETAIL_DATA |
|---|
| 82 | { |
|---|
| 83 | public UInt32 cbSize; |
|---|
| 84 | [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)] |
|---|
| 85 | public string DevicePath; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | [StructLayout(LayoutKind.Sequential)] |
|---|
| 89 | public struct HIDD_ATTRIBUTES |
|---|
| 90 | { |
|---|
| 91 | public int Size; |
|---|
| 92 | public short VendorID; |
|---|
| 93 | public short ProductID; |
|---|
| 94 | public short VersionNumber; |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | [DllImport(@"hid.dll", CharSet=CharSet.Auto, SetLastError = true)] |
|---|
| 98 | public static extern void HidD_GetHidGuid(out Guid gHid); |
|---|
| 99 | |
|---|
| 100 | [DllImport("hid.dll")] |
|---|
| 101 | public static extern Boolean HidD_GetAttributes(IntPtr HidDeviceObject, ref HIDD_ATTRIBUTES Attributes); |
|---|
| 102 | |
|---|
| 103 | [DllImport("hid.dll")] |
|---|
| 104 | internal extern static bool HidD_SetOutputReport( |
|---|
| 105 | IntPtr HidDeviceObject, |
|---|
| 106 | byte[] lpReportBuffer, |
|---|
| 107 | uint ReportBufferLength); |
|---|
| 108 | |
|---|
| 109 | [DllImport(@"setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)] |
|---|
| 110 | public static extern IntPtr SetupDiGetClassDevs( |
|---|
| 111 | ref Guid ClassGuid, |
|---|
| 112 | [MarshalAs(UnmanagedType.LPTStr)] string Enumerator, |
|---|
| 113 | IntPtr hwndParent, |
|---|
| 114 | UInt32 Flags |
|---|
| 115 | ); |
|---|
| 116 | |
|---|
| 117 | [DllImport(@"setupapi.dll", CharSet=CharSet.Auto, SetLastError = true)] |
|---|
| 118 | public static extern Boolean SetupDiEnumDeviceInterfaces( |
|---|
| 119 | IntPtr hDevInfo, |
|---|
| 120 | //ref SP_DEVINFO_DATA devInfo, |
|---|
| 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 | public 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 | public 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 | public static extern UInt16 SetupDiDestroyDeviceInfoList( IntPtr hDevInfo ); |
|---|
| 149 | |
|---|
| 150 | [DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)] |
|---|
| 151 | public static extern SafeFileHandle CreateFile( |
|---|
| 152 | string fileName, |
|---|
| 153 | [MarshalAs(UnmanagedType.U4)] FileAccess fileAccess, |
|---|
| 154 | [MarshalAs(UnmanagedType.U4)] FileShare fileShare, |
|---|
| 155 | IntPtr securityAttributes, |
|---|
| 156 | [MarshalAs(UnmanagedType.U4)] FileMode creationDisposition, |
|---|
| 157 | [MarshalAs(UnmanagedType.U4)] EFileAttributes flags, |
|---|
| 158 | IntPtr template); |
|---|
| 159 | |
|---|
| 160 | [DllImport("kernel32.dll", SetLastError=true)] |
|---|
| 161 | [return: MarshalAs(UnmanagedType.Bool)] |
|---|
| 162 | public static extern bool CloseHandle(IntPtr hObject); |
|---|
| 163 | } |
|---|
| 164 | } |
|---|