# HG changeset patch # User sl # Date 1417864419 -3600 # Node ID 974f5fdaebfbb366a7f87b3cf44d09b7fee5e529 # Parent 743cadfacda084410139422839d74bc7a16f21d6 Now getting manufacturer and product name. diff -r 743cadfacda0 -r 974f5fdaebfb RemoteControlDevice.cs --- a/RemoteControlDevice.cs Sat Dec 06 03:26:27 2014 +0100 +++ b/RemoteControlDevice.cs Sat Dec 06 12:13:39 2014 +0100 @@ -3,11 +3,13 @@ using System.Runtime.InteropServices; using System.Diagnostics; using System.Text; +using Microsoft.Win32.SafeHandles; using Hid.UsageTables; using Win32; + namespace Devices.RemoteControl { @@ -395,16 +397,6 @@ return; } - /* - //TODO: This needs create file from the device name/path - //Get device manufacturer - StringBuilder manufacturerString = new StringBuilder(256); - bool returnStatus = Win32.Function.HidD_GetManufacturerString(rawInput.header.hDevice, manufacturerString, manufacturerString.Capacity); - if (returnStatus) - { - Debug.WriteLine("Manufacturer name is {0}", manufacturerString.ToString()); - } - */ //Fetch device info @@ -414,10 +406,48 @@ return; } - // - Debug.WriteLine(RawInput.GetDeviceName(rawInput.header.hDevice)); + //Debug + string deviceName = RawInput.GetDeviceName(rawInput.header.hDevice); + Debug.WriteLine("Device name: " + deviceName); + + //Open our device from the device name/path + SafeFileHandle handle=Win32.Function.CreateFile(deviceName, + Win32.FileAccess.NONE, + Win32.FileShare.FILE_SHARE_READ|Win32.FileShare.FILE_SHARE_WRITE, + IntPtr.Zero, + Win32.CreationDisposition.OPEN_EXISTING, + Win32.FileFlagsAttributes.FILE_FLAG_OVERLAPPED, + IntPtr.Zero + ); + + if (handle.IsInvalid) + { + Debug.WriteLine("Failed to CreateFile from device name " + Marshal.GetLastWin32Error().ToString()); + } + else + { + //Get manufacturer string + StringBuilder manufacturerString = new StringBuilder(256); + bool returnStatus = Win32.Function.HidD_GetManufacturerString(handle, manufacturerString, manufacturerString.Capacity); + if (returnStatus) + { + Debug.WriteLine("Manufacturer: " + manufacturerString.ToString()); + } + + //Get product string + StringBuilder productString = new StringBuilder(256); + returnStatus = Win32.Function.HidD_GetProductString(handle, productString, productString.Capacity); + if (returnStatus) + { + Debug.WriteLine("Product: " + productString.ToString()); + } + + handle.Close(); + + } + if (rawInput.header.dwType == Const.RIM_TYPEHID) //Check that our raw input is HID { Debug.WriteLine("WM_INPUT source device is HID."); diff -r 743cadfacda0 -r 974f5fdaebfb RemoteControlSample.csproj --- a/RemoteControlSample.csproj Sat Dec 06 03:26:27 2014 +0100 +++ b/RemoteControlSample.csproj Sat Dec 06 12:13:39 2014 +0100 @@ -132,6 +132,7 @@ Code + diff -r 743cadfacda0 -r 974f5fdaebfb Win32CreateFile.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Win32CreateFile.cs Sat Dec 06 12:13:39 2014 +0100 @@ -0,0 +1,209 @@ +using System; +using System.Runtime.InteropServices; +using Microsoft.Win32.SafeHandles; + +namespace Win32 +{ + + static partial class Function + { + [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] + public static extern SafeFileHandle CreateFile( + [MarshalAs(UnmanagedType.LPTStr)] string lpFileName, + [MarshalAs(UnmanagedType.U4)] FileAccess dwDesiredAccess, + [MarshalAs(UnmanagedType.U4)] FileShare dwShareMode, + IntPtr lpSecurityAttributes, // optional SECURITY_ATTRIBUTES struct or IntPtr.Zero + [MarshalAs(UnmanagedType.U4)] CreationDisposition dwCreationDisposition, + [MarshalAs(UnmanagedType.U4)] FileFlagsAttributes dwFlagsAndAttributes, + IntPtr hTemplateFile); + + [DllImport("kernel32.dll", CharSet = CharSet.Ansi, SetLastError = true)] + public static extern SafeFileHandle CreateFileA( + [MarshalAs(UnmanagedType.LPStr)] string lpFileName, + [MarshalAs(UnmanagedType.U4)] FileAccess dwDesiredAccess, + [MarshalAs(UnmanagedType.U4)] FileShare dwShareMode, + IntPtr lpSecurityAttributes, + [MarshalAs(UnmanagedType.U4)] CreationDisposition dwCreationDisposition, + [MarshalAs(UnmanagedType.U4)] FileFlagsAttributes dwFlagsAndAttributes, + IntPtr hTemplateFile); + + [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] + public static extern SafeFileHandle CreateFileW( + [MarshalAs(UnmanagedType.LPWStr)] string lpFileName, + [MarshalAs(UnmanagedType.U4)] FileAccess dwDesiredAccess, + [MarshalAs(UnmanagedType.U4)] FileShare dwShareMode, + IntPtr lpSecurityAttributes, + [MarshalAs(UnmanagedType.U4)] CreationDisposition dwCreationDisposition, + [MarshalAs(UnmanagedType.U4)] FileFlagsAttributes dwFlagsAndAttributes, + IntPtr hTemplateFile); + } + + + static partial class Macro + { + + } + + + + static partial class Const + { + + } + + [Flags] + enum FileAccess : uint + { + NONE = 0, + + GENERIC_ALL = 0x10000000, + GENERIC_EXECUTE = 0x20000000, + GENERIC_READ = 0x80000000, + GENERIC_WRITE = 0x40000000, + + FILE_READ_DATA = (0x0001), // file & pipe + FILE_LIST_DIRECTORY = (0x0001), // directory + + FILE_WRITE_DATA = (0x0002), // file & pipe + FILE_ADD_FILE = (0x0002), // directory + + FILE_APPEND_DATA = (0x0004), // file + FILE_ADD_SUBDIRECTORY = (0x0004), // directory + FILE_CREATE_PIPE_INSTANCE = (0x0004), // named pipe + + FILE_READ_EA = (0x0008), // file & directory + + FILE_WRITE_EA = (0x0010), // file & directory + + FILE_EXECUTE = (0x0020), // file + FILE_TRAVERSE = (0x0020), // directory + + FILE_DELETE_CHILD = (0x0040), // directory + + FILE_READ_ATTRIBUTES = (0x0080), // all + + FILE_WRITE_ATTRIBUTES = (0x0100), // all + + FILE_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0x1FF), + + FILE_GENERIC_READ = (STANDARD_RIGHTS_READ | FILE_READ_DATA | FILE_READ_ATTRIBUTES | FILE_READ_EA | SYNCHRONIZE), + FILE_GENERIC_WRITE = (STANDARD_RIGHTS_WRITE | FILE_WRITE_DATA | FILE_WRITE_ATTRIBUTES | FILE_WRITE_EA | FILE_APPEND_DATA | SYNCHRONIZE), + FILE_GENERIC_EXECUTE = (STANDARD_RIGHTS_EXECUTE | FILE_READ_ATTRIBUTES | FILE_EXECUTE | SYNCHRONIZE), + + DELETE = (0x00010000), + READ_CONTROL = (0x00020000), + WRITE_DAC = (0x00040000), + WRITE_OWNER = (0x00080000), + SYNCHRONIZE = (0x00100000), + + STANDARD_RIGHTS_REQUIRED = (0x000F0000), + + STANDARD_RIGHTS_READ = (READ_CONTROL), + STANDARD_RIGHTS_WRITE = (READ_CONTROL), + STANDARD_RIGHTS_EXECUTE = (READ_CONTROL), + + STANDARD_RIGHTS_ALL = (0x001F0000), + + SPECIFIC_RIGHTS_ALL = (0x0000FFFF), + + ACCESS_SYSTEM_SECURITY = (0x01000000), + + MAXIMUM_ALLOWED = (0x02000000) + } + + + + [Flags] + public enum FileShare : uint + { + /// + /// Prevents other processes from opening a file or device if they request delete, read, or write access. + /// + FILE_SHARE_NONE = 0x00000000, + /// + /// Enables subsequent open operations on an object to request read access. + /// Otherwise, other processes cannot open the object if they request read access. + /// If this flag is not specified, but the object has been opened for read access, the function fails. + /// + FILE_SHARE_READ = 0x00000001, + /// + /// Enables subsequent open operations on an object to request write access. + /// Otherwise, other processes cannot open the object if they request write access. + /// If this flag is not specified, but the object has been opened for write access, the function fails. + /// + FILE_SHARE_WRITE = 0x00000002, + /// + /// Enables subsequent open operations on an object to request delete access. + /// Otherwise, other processes cannot open the object if they request delete access. + /// If this flag is not specified, but the object has been opened for delete access, the function fails. + /// + FILE_SHARE_DELETE = 0x00000004 + } + + public enum CreationDisposition : uint + { + /// + /// Creates a new file. The function fails if a specified file exists. + /// + CREATE_NEW = 1, + /// + /// Creates a new file, always. + /// If a file exists, the function overwrites the file, clears the existing attributes, combines the specified file attributes, + /// and flags with FILE_ATTRIBUTE_ARCHIVE, but does not set the security descriptor that the SECURITY_ATTRIBUTES structure specifies. + /// + CREATE_ALWAYS = 2, + /// + /// Opens a file. The function fails if the file does not exist. + /// + OPEN_EXISTING = 3, + /// + /// Opens a file, always. + /// If a file does not exist, the function creates a file as if dwCreationDisposition is CREATE_NEW. + /// + OPEN_ALWAYS = 4, + /// + /// Opens a file and truncates it so that its size is 0 (zero) bytes. The function fails if the file does not exist. + /// The calling process must open the file with the GENERIC_WRITE access right. + /// + TRUNCATE_EXISTING = 5 + } + + [Flags] + public enum FileFlagsAttributes : uint + { + FILE_ATTRIBUTE_READONLY = 0x00000001, + FILE_ATTRIBUTE_HIDDEN = 0x00000002, + FILE_ATTRIBUTE_SYSTEM = 0x00000004, + FILE_ATTRIBUTE_DIRECTORY = 0x00000010, + FILE_ATTRIBUTE_ARCHIVE = 0x00000020, + FILE_ATTRIBUTE_DEVICE = 0x00000040, + FILE_ATTRIBUTE_NORMAL = 0x00000080, + FILE_ATTRIBUTE_TEMPORARY = 0x00000100, + FILE_ATTRIBUTE_SPARSE_FILE = 0x00000200, + FILE_ATTRIBUTE_REPARSE_POINT = 0x00000400, + FILE_ATTRIBUTE_COMPRESSED = 0x00000800, + FILE_ATTRIBUTE_OFFLINE = 0x00001000, + FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 0x00002000, + FILE_ATTRIBUTE_ENCRYPTED = 0x00004000, + FILE_ATTRIBUTE_INTEGRITY_STREAM = 0x00008000, + FILE_ATTRIBUTE_VIRTUAL = 0x00010000, + FILE_ATTRIBUTE_NO_SCRUB_DATA = 0x00020000, + // These are flags supported through CreateFile (W7) and CreateFile2 (W8 and beyond) + FILE_FLAG_WRITE_THROUGH = 0x80000000, + FILE_FLAG_OVERLAPPED = 0x40000000, + FILE_FLAG_NO_BUFFERING = 0x20000000, + FILE_FLAG_RANDOM_ACCESS = 0x10000000, + FILE_FLAG_SEQUENTIAL_SCAN = 0x08000000, + FILE_FLAG_DELETE_ON_CLOSE = 0x04000000, + FILE_FLAG_BACKUP_SEMANTICS = 0x02000000, + FILE_FLAG_POSIX_SEMANTICS = 0x01000000, + FILE_FLAG_SESSION_AWARE = 0x00800000, + FILE_FLAG_OPEN_REPARSE_POINT = 0x00200000, + FILE_FLAG_OPEN_NO_RECALL = 0x00100000, + FILE_FLAG_FIRST_PIPE_INSTANCE = 0x00080000 + } + + + + +} \ No newline at end of file diff -r 743cadfacda0 -r 974f5fdaebfb Win32Hid.cs --- a/Win32Hid.cs Sat Dec 06 03:26:27 2014 +0100 +++ b/Win32Hid.cs Sat Dec 06 12:13:39 2014 +0100 @@ -12,7 +12,10 @@ 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); [DllImport("hid.dll", CharSet = CharSet.Auto, SetLastError = true)] - public static extern Boolean HidD_GetManufacturerString(/*SafeFileHandle*/ IntPtr HidDeviceObject, StringBuilder Buffer, Int32 BufferLength); + public static extern Boolean HidD_GetManufacturerString(SafeFileHandle HidDeviceObject, StringBuilder Buffer, Int32 BufferLength); + + [DllImport("hid.dll", CharSet = CharSet.Auto, SetLastError = true)] + public static extern Boolean HidD_GetProductString(SafeFileHandle HidDeviceObject, StringBuilder Buffer, Int32 BufferLength); }