Now getting manufacturer and product name.
authorsl
Sat, 06 Dec 2014 12:13:39 +0100
changeset 24974f5fdaebfb
parent 23 743cadfacda0
child 25 5f4e0fbb3ea1
Now getting manufacturer and product name.
RemoteControlDevice.cs
RemoteControlSample.csproj
Win32CreateFile.cs
Win32Hid.cs
     1.1 --- a/RemoteControlDevice.cs	Sat Dec 06 03:26:27 2014 +0100
     1.2 +++ b/RemoteControlDevice.cs	Sat Dec 06 12:13:39 2014 +0100
     1.3 @@ -3,11 +3,13 @@
     1.4  using System.Runtime.InteropServices;
     1.5  using System.Diagnostics;
     1.6  using System.Text;
     1.7 +using Microsoft.Win32.SafeHandles;
     1.8  
     1.9  using Hid.UsageTables;
    1.10  using Win32;
    1.11  
    1.12  
    1.13 +
    1.14  namespace Devices.RemoteControl
    1.15  {
    1.16  
    1.17 @@ -395,16 +397,6 @@
    1.18                      return;
    1.19                  }
    1.20  
    1.21 -                /*
    1.22 -                //TODO: This needs create file from the device name/path
    1.23 -                //Get device manufacturer
    1.24 -                StringBuilder manufacturerString = new StringBuilder(256);
    1.25 -                bool returnStatus = Win32.Function.HidD_GetManufacturerString(rawInput.header.hDevice, manufacturerString, manufacturerString.Capacity);
    1.26 -                if (returnStatus)
    1.27 -                {
    1.28 -                    Debug.WriteLine("Manufacturer name is {0}", manufacturerString.ToString());
    1.29 -                }
    1.30 -                */
    1.31  
    1.32  
    1.33                  //Fetch device info
    1.34 @@ -414,10 +406,48 @@
    1.35                      return;
    1.36                  }
    1.37  
    1.38 -                //
    1.39 -                Debug.WriteLine(RawInput.GetDeviceName(rawInput.header.hDevice));
    1.40 +                //Debug
    1.41 +                string deviceName = RawInput.GetDeviceName(rawInput.header.hDevice);
    1.42 +                Debug.WriteLine("Device name: " + deviceName);
    1.43 +                
    1.44 +                //Open our device from the device name/path
    1.45 +                SafeFileHandle handle=Win32.Function.CreateFile(deviceName,
    1.46 +                    Win32.FileAccess.NONE,
    1.47 +                    Win32.FileShare.FILE_SHARE_READ|Win32.FileShare.FILE_SHARE_WRITE,
    1.48 +                    IntPtr.Zero,
    1.49 +                    Win32.CreationDisposition.OPEN_EXISTING,
    1.50 +                    Win32.FileFlagsAttributes.FILE_FLAG_OVERLAPPED,
    1.51 +                    IntPtr.Zero
    1.52 +                    );
    1.53 +
    1.54 +                if (handle.IsInvalid)
    1.55 +                {
    1.56 +                    Debug.WriteLine("Failed to CreateFile from device name " + Marshal.GetLastWin32Error().ToString());
    1.57 +                }
    1.58 +                else
    1.59 +                {
    1.60 +                    //Get manufacturer string
    1.61 +                    StringBuilder manufacturerString = new StringBuilder(256);
    1.62 +                    bool returnStatus = Win32.Function.HidD_GetManufacturerString(handle, manufacturerString, manufacturerString.Capacity);
    1.63 +                    if (returnStatus)
    1.64 +                    {
    1.65 +                        Debug.WriteLine("Manufacturer: " + manufacturerString.ToString());
    1.66 +                    }
    1.67 +
    1.68 +                    //Get product string
    1.69 +                    StringBuilder productString = new StringBuilder(256);
    1.70 +                    returnStatus = Win32.Function.HidD_GetProductString(handle, productString, productString.Capacity);
    1.71 +                    if (returnStatus)
    1.72 +                    {
    1.73 +                        Debug.WriteLine("Product: " + productString.ToString());
    1.74 +                    }
    1.75 +
    1.76 +                    handle.Close();
    1.77 +
    1.78 +                }
    1.79                 
    1.80  
    1.81 +
    1.82                  if (rawInput.header.dwType == Const.RIM_TYPEHID)  //Check that our raw input is HID                        
    1.83                  {
    1.84                      Debug.WriteLine("WM_INPUT source device is HID.");
     2.1 --- a/RemoteControlSample.csproj	Sat Dec 06 03:26:27 2014 +0100
     2.2 +++ b/RemoteControlSample.csproj	Sat Dec 06 12:13:39 2014 +0100
     2.3 @@ -132,6 +132,7 @@
     2.4      <Compile Include="RemoteControlDevice.cs">
     2.5        <SubType>Code</SubType>
     2.6      </Compile>
     2.7 +    <Compile Include="Win32CreateFile.cs" />
     2.8      <Compile Include="Win32Hid.cs" />
     2.9      <Compile Include="Win32RawInput.cs" />
    2.10      <Content Include="App.ico" />
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/Win32CreateFile.cs	Sat Dec 06 12:13:39 2014 +0100
     3.3 @@ -0,0 +1,209 @@
     3.4 +using System;
     3.5 +using System.Runtime.InteropServices;
     3.6 +using Microsoft.Win32.SafeHandles;
     3.7 +
     3.8 +namespace Win32
     3.9 +{
    3.10 +
    3.11 +    static partial class Function
    3.12 +    {
    3.13 +        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    3.14 +        public static extern SafeFileHandle CreateFile(
    3.15 +             [MarshalAs(UnmanagedType.LPTStr)] string lpFileName,
    3.16 +             [MarshalAs(UnmanagedType.U4)] FileAccess dwDesiredAccess,
    3.17 +             [MarshalAs(UnmanagedType.U4)] FileShare dwShareMode,
    3.18 +             IntPtr lpSecurityAttributes, // optional SECURITY_ATTRIBUTES struct or IntPtr.Zero
    3.19 +             [MarshalAs(UnmanagedType.U4)] CreationDisposition dwCreationDisposition,
    3.20 +             [MarshalAs(UnmanagedType.U4)] FileFlagsAttributes dwFlagsAndAttributes,
    3.21 +             IntPtr hTemplateFile);
    3.22 +
    3.23 +        [DllImport("kernel32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
    3.24 +        public static extern SafeFileHandle CreateFileA(
    3.25 +             [MarshalAs(UnmanagedType.LPStr)] string lpFileName,
    3.26 +             [MarshalAs(UnmanagedType.U4)] FileAccess dwDesiredAccess,
    3.27 +             [MarshalAs(UnmanagedType.U4)] FileShare dwShareMode,
    3.28 +             IntPtr lpSecurityAttributes,
    3.29 +             [MarshalAs(UnmanagedType.U4)] CreationDisposition dwCreationDisposition,
    3.30 +             [MarshalAs(UnmanagedType.U4)] FileFlagsAttributes dwFlagsAndAttributes,
    3.31 +             IntPtr hTemplateFile);
    3.32 +
    3.33 +        [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    3.34 +        public static extern SafeFileHandle CreateFileW(
    3.35 +             [MarshalAs(UnmanagedType.LPWStr)] string lpFileName,
    3.36 +             [MarshalAs(UnmanagedType.U4)] FileAccess dwDesiredAccess,
    3.37 +             [MarshalAs(UnmanagedType.U4)] FileShare dwShareMode,
    3.38 +             IntPtr lpSecurityAttributes,
    3.39 +             [MarshalAs(UnmanagedType.U4)] CreationDisposition dwCreationDisposition,
    3.40 +             [MarshalAs(UnmanagedType.U4)] FileFlagsAttributes dwFlagsAndAttributes,
    3.41 +             IntPtr hTemplateFile);
    3.42 +    }
    3.43 +
    3.44 +
    3.45 +    static partial class Macro
    3.46 +    {
    3.47 +
    3.48 +    }
    3.49 +
    3.50 +
    3.51 +
    3.52 +    static partial class Const
    3.53 +    {
    3.54 +
    3.55 +    }
    3.56 +
    3.57 +    [Flags]
    3.58 +    enum FileAccess : uint
    3.59 +    {
    3.60 +        NONE = 0,
    3.61 +
    3.62 +        GENERIC_ALL = 0x10000000,
    3.63 +        GENERIC_EXECUTE = 0x20000000,
    3.64 +        GENERIC_READ = 0x80000000,
    3.65 +        GENERIC_WRITE = 0x40000000,
    3.66 +
    3.67 +        FILE_READ_DATA = (0x0001),  // file & pipe
    3.68 +        FILE_LIST_DIRECTORY = (0x0001),  // directory
    3.69 +
    3.70 +        FILE_WRITE_DATA = (0x0002),   // file & pipe
    3.71 +        FILE_ADD_FILE = (0x0002),  // directory
    3.72 +
    3.73 +        FILE_APPEND_DATA = (0x0004), // file
    3.74 +        FILE_ADD_SUBDIRECTORY = (0x0004),  // directory
    3.75 +        FILE_CREATE_PIPE_INSTANCE = (0x0004),   // named pipe
    3.76 +
    3.77 +        FILE_READ_EA = (0x0008),  // file & directory
    3.78 +
    3.79 +        FILE_WRITE_EA = (0x0010),    // file & directory
    3.80 +
    3.81 +        FILE_EXECUTE = (0x0020),    // file
    3.82 +        FILE_TRAVERSE = (0x0020),    // directory
    3.83 +
    3.84 +        FILE_DELETE_CHILD = (0x0040),    // directory
    3.85 +
    3.86 +        FILE_READ_ATTRIBUTES = (0x0080),    // all
    3.87 +
    3.88 +        FILE_WRITE_ATTRIBUTES = (0x0100),    // all
    3.89 +
    3.90 +        FILE_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0x1FF),
    3.91 +
    3.92 +        FILE_GENERIC_READ = (STANDARD_RIGHTS_READ | FILE_READ_DATA | FILE_READ_ATTRIBUTES | FILE_READ_EA | SYNCHRONIZE),
    3.93 +        FILE_GENERIC_WRITE = (STANDARD_RIGHTS_WRITE | FILE_WRITE_DATA | FILE_WRITE_ATTRIBUTES | FILE_WRITE_EA | FILE_APPEND_DATA | SYNCHRONIZE),
    3.94 +        FILE_GENERIC_EXECUTE = (STANDARD_RIGHTS_EXECUTE | FILE_READ_ATTRIBUTES | FILE_EXECUTE | SYNCHRONIZE),
    3.95 +
    3.96 +        DELETE = (0x00010000),
    3.97 +        READ_CONTROL = (0x00020000),
    3.98 +        WRITE_DAC = (0x00040000),
    3.99 +        WRITE_OWNER = (0x00080000),
   3.100 +        SYNCHRONIZE = (0x00100000),
   3.101 +
   3.102 +        STANDARD_RIGHTS_REQUIRED = (0x000F0000),
   3.103 +
   3.104 +        STANDARD_RIGHTS_READ = (READ_CONTROL),
   3.105 +        STANDARD_RIGHTS_WRITE = (READ_CONTROL),
   3.106 +        STANDARD_RIGHTS_EXECUTE = (READ_CONTROL),
   3.107 +
   3.108 +        STANDARD_RIGHTS_ALL = (0x001F0000),
   3.109 +
   3.110 +        SPECIFIC_RIGHTS_ALL = (0x0000FFFF),
   3.111 +
   3.112 +        ACCESS_SYSTEM_SECURITY = (0x01000000),
   3.113 +
   3.114 +        MAXIMUM_ALLOWED = (0x02000000)
   3.115 +    }
   3.116 +    
   3.117 +
   3.118 +
   3.119 +    [Flags]
   3.120 +    public enum FileShare : uint
   3.121 +    {
   3.122 +        /// <summary>
   3.123 +        /// Prevents other processes from opening a file or device if they request delete, read, or write access.
   3.124 +        /// </summary>
   3.125 +        FILE_SHARE_NONE = 0x00000000,
   3.126 +        /// <summary>
   3.127 +        /// Enables subsequent open operations on an object to request read access.
   3.128 +        /// Otherwise, other processes cannot open the object if they request read access.
   3.129 +        /// If this flag is not specified, but the object has been opened for read access, the function fails.
   3.130 +        /// </summary>
   3.131 +        FILE_SHARE_READ = 0x00000001,
   3.132 +        /// <summary>
   3.133 +        /// Enables subsequent open operations on an object to request write access.
   3.134 +        /// Otherwise, other processes cannot open the object if they request write access.
   3.135 +        /// If this flag is not specified, but the object has been opened for write access, the function fails.
   3.136 +        /// </summary>
   3.137 +        FILE_SHARE_WRITE = 0x00000002,
   3.138 +        /// <summary>
   3.139 +        /// Enables subsequent open operations on an object to request delete access.
   3.140 +        /// Otherwise, other processes cannot open the object if they request delete access.
   3.141 +        /// If this flag is not specified, but the object has been opened for delete access, the function fails.
   3.142 +        /// </summary>
   3.143 +        FILE_SHARE_DELETE = 0x00000004
   3.144 +    }
   3.145 +
   3.146 +    public enum CreationDisposition : uint
   3.147 +    {
   3.148 +        /// <summary>
   3.149 +        /// Creates a new file. The function fails if a specified file exists.
   3.150 +        /// </summary>
   3.151 +        CREATE_NEW = 1,
   3.152 +        /// <summary>
   3.153 +        /// Creates a new file, always.
   3.154 +        /// If a file exists, the function overwrites the file, clears the existing attributes, combines the specified file attributes,
   3.155 +        /// and flags with FILE_ATTRIBUTE_ARCHIVE, but does not set the security descriptor that the SECURITY_ATTRIBUTES structure specifies.
   3.156 +        /// </summary>
   3.157 +        CREATE_ALWAYS = 2,
   3.158 +        /// <summary>
   3.159 +        /// Opens a file. The function fails if the file does not exist.
   3.160 +        /// </summary>
   3.161 +        OPEN_EXISTING = 3,
   3.162 +        /// <summary>
   3.163 +        /// Opens a file, always.
   3.164 +        /// If a file does not exist, the function creates a file as if dwCreationDisposition is CREATE_NEW.
   3.165 +        /// </summary>
   3.166 +        OPEN_ALWAYS = 4,
   3.167 +        /// <summary>
   3.168 +        /// Opens a file and truncates it so that its size is 0 (zero) bytes. The function fails if the file does not exist.
   3.169 +        /// The calling process must open the file with the GENERIC_WRITE access right.
   3.170 +        /// </summary>
   3.171 +        TRUNCATE_EXISTING = 5
   3.172 +    }
   3.173 +
   3.174 +    [Flags]
   3.175 +    public enum FileFlagsAttributes : uint
   3.176 +    {
   3.177 +        FILE_ATTRIBUTE_READONLY = 0x00000001,
   3.178 +        FILE_ATTRIBUTE_HIDDEN = 0x00000002,
   3.179 +        FILE_ATTRIBUTE_SYSTEM = 0x00000004,
   3.180 +        FILE_ATTRIBUTE_DIRECTORY = 0x00000010,
   3.181 +        FILE_ATTRIBUTE_ARCHIVE = 0x00000020,
   3.182 +        FILE_ATTRIBUTE_DEVICE = 0x00000040,
   3.183 +        FILE_ATTRIBUTE_NORMAL = 0x00000080,
   3.184 +        FILE_ATTRIBUTE_TEMPORARY = 0x00000100,
   3.185 +        FILE_ATTRIBUTE_SPARSE_FILE = 0x00000200,
   3.186 +        FILE_ATTRIBUTE_REPARSE_POINT = 0x00000400,
   3.187 +        FILE_ATTRIBUTE_COMPRESSED = 0x00000800,
   3.188 +        FILE_ATTRIBUTE_OFFLINE = 0x00001000,
   3.189 +        FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 0x00002000,
   3.190 +        FILE_ATTRIBUTE_ENCRYPTED = 0x00004000,
   3.191 +        FILE_ATTRIBUTE_INTEGRITY_STREAM = 0x00008000,
   3.192 +        FILE_ATTRIBUTE_VIRTUAL = 0x00010000,
   3.193 +        FILE_ATTRIBUTE_NO_SCRUB_DATA = 0x00020000,
   3.194 +        //  These are flags supported through CreateFile (W7) and CreateFile2 (W8 and beyond)
   3.195 +        FILE_FLAG_WRITE_THROUGH = 0x80000000,
   3.196 +        FILE_FLAG_OVERLAPPED = 0x40000000,
   3.197 +        FILE_FLAG_NO_BUFFERING = 0x20000000,
   3.198 +        FILE_FLAG_RANDOM_ACCESS = 0x10000000,
   3.199 +        FILE_FLAG_SEQUENTIAL_SCAN = 0x08000000,
   3.200 +        FILE_FLAG_DELETE_ON_CLOSE = 0x04000000,
   3.201 +        FILE_FLAG_BACKUP_SEMANTICS = 0x02000000,
   3.202 +        FILE_FLAG_POSIX_SEMANTICS = 0x01000000,
   3.203 +        FILE_FLAG_SESSION_AWARE = 0x00800000,
   3.204 +        FILE_FLAG_OPEN_REPARSE_POINT = 0x00200000,
   3.205 +        FILE_FLAG_OPEN_NO_RECALL = 0x00100000,
   3.206 +        FILE_FLAG_FIRST_PIPE_INSTANCE = 0x00080000
   3.207 +    }
   3.208 +
   3.209 +
   3.210 +
   3.211 +
   3.212 +}
   3.213 \ No newline at end of file
     4.1 --- a/Win32Hid.cs	Sat Dec 06 03:26:27 2014 +0100
     4.2 +++ b/Win32Hid.cs	Sat Dec 06 12:13:39 2014 +0100
     4.3 @@ -12,7 +12,10 @@
     4.4          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);
     4.5  
     4.6          [DllImport("hid.dll", CharSet = CharSet.Auto, SetLastError = true)]
     4.7 -        public static extern Boolean HidD_GetManufacturerString(/*SafeFileHandle*/ IntPtr HidDeviceObject, StringBuilder Buffer, Int32 BufferLength);
     4.8 +        public static extern Boolean HidD_GetManufacturerString(SafeFileHandle HidDeviceObject, StringBuilder Buffer, Int32 BufferLength);
     4.9 +
    4.10 +        [DllImport("hid.dll", CharSet = CharSet.Auto, SetLastError = true)]
    4.11 +        public static extern Boolean HidD_GetProductString(SafeFileHandle HidDeviceObject, StringBuilder Buffer, Int32 BufferLength);
    4.12  
    4.13      }
    4.14