RawInput.cs
changeset 22 8b2a3da54187
parent 19 d066e3999973
child 23 743cadfacda0
     1.1 --- a/RawInput.cs	Sat Dec 06 01:53:55 2014 +0100
     1.2 +++ b/RawInput.cs	Sat Dec 06 02:46:46 2014 +0100
     1.3 @@ -68,7 +68,7 @@
     1.4                  uint deviceInfoSize = (uint)Marshal.SizeOf(typeof(RID_DEVICE_INFO));
     1.5                  deviceInfoBuffer = Marshal.AllocHGlobal((int)deviceInfoSize);
     1.6  
     1.7 -                int res = Win32.Function.GetRawInputDeviceInfo(hDevice, Const.RIDI_DEVICEINFO, deviceInfoBuffer, ref deviceInfoSize);
     1.8 +                int res = Win32.Function.GetRawInputDeviceInfoW(hDevice, Const.RIDI_DEVICEINFO, deviceInfoBuffer, ref deviceInfoSize);
     1.9                  if (res <= 0)
    1.10                  {
    1.11                      Debug.WriteLine("WM_INPUT could not read device info: " + Marshal.GetLastWin32Error().ToString());
    1.12 @@ -101,7 +101,7 @@
    1.13          public static IntPtr GetPreParsedData(IntPtr hDevice)
    1.14          {
    1.15              uint ppDataSize = 256;
    1.16 -            int result = Win32.Function.GetRawInputDeviceInfo(hDevice, Win32.Const.RIDI_PREPARSEDDATA, IntPtr.Zero, ref ppDataSize);
    1.17 +            int result = Win32.Function.GetRawInputDeviceInfoW(hDevice, Win32.Const.RIDI_PREPARSEDDATA, IntPtr.Zero, ref ppDataSize);
    1.18              if (result != 0)
    1.19              {
    1.20                  Debug.WriteLine("Failed to get raw input pre-parsed data size" + result + Marshal.GetLastWin32Error());
    1.21 @@ -109,7 +109,7 @@
    1.22              }
    1.23  
    1.24              IntPtr ppData = Marshal.AllocHGlobal((int)ppDataSize);
    1.25 -            result = Win32.Function.GetRawInputDeviceInfo(hDevice, Win32.Const.RIDI_PREPARSEDDATA, ppData, ref ppDataSize);
    1.26 +            result = Win32.Function.GetRawInputDeviceInfoW(hDevice, Win32.Const.RIDI_PREPARSEDDATA, ppData, ref ppDataSize);
    1.27              if (result <= 0)
    1.28              {
    1.29                  Debug.WriteLine("Failed to get raw input pre-parsed data" + result + Marshal.GetLastWin32Error());
    1.30 @@ -118,6 +118,37 @@
    1.31              return ppData;
    1.32          }
    1.33  
    1.34 +        /// <summary>
    1.35 +        /// 
    1.36 +        /// </summary>
    1.37 +        /// <param name="device"></param>
    1.38 +        /// <returns></returns>
    1.39 +        public static string GetDeviceName(IntPtr device)
    1.40 +        {
    1.41 +            uint deviceNameSize = 256;
    1.42 +            int result = Win32.Function.GetRawInputDeviceInfoW(device, Win32.Const.RIDI_DEVICENAME, IntPtr.Zero, ref deviceNameSize);
    1.43 +            if (result != 0)
    1.44 +            {
    1.45 +                return string.Empty;
    1.46 +            }
    1.47 +
    1.48 +            IntPtr deviceName = Marshal.AllocHGlobal((int)deviceNameSize * 2);  // size is the character count not byte count
    1.49 +            try
    1.50 +            {
    1.51 +                result = Win32.Function.GetRawInputDeviceInfoW(device, Win32.Const.RIDI_DEVICENAME, deviceName, ref deviceNameSize);
    1.52 +                if (result > 0)
    1.53 +                {
    1.54 +                    return Marshal.PtrToStringUni(deviceName, result - 1); // -1 for NULL termination
    1.55 +                }
    1.56 +
    1.57 +                return string.Empty;
    1.58 +            }
    1.59 +            finally
    1.60 +            {
    1.61 +                Marshal.FreeHGlobal(deviceName);
    1.62 +            }
    1.63 +        }
    1.64 +
    1.65  
    1.66      }
    1.67  }
    1.68 \ No newline at end of file