RawInput.cs
changeset 60 687cace560d2
parent 47 bd068db7779a
child 61 60bfe5083721
     1.1 --- a/RawInput.cs	Sun Feb 15 13:30:54 2015 +0100
     1.2 +++ b/RawInput.cs	Sun Feb 15 18:15:41 2015 +0100
     1.3 @@ -1,9 +1,10 @@
     1.4  using System;
     1.5  using System.Runtime.InteropServices;
     1.6  using System.Diagnostics;
     1.7 +using System.Windows.Forms;
     1.8  
     1.9  
    1.10 -namespace Win32.Utils
    1.11 +namespace Win32
    1.12  {
    1.13      /// <summary>
    1.14      /// Provide some utility functions for raw input handling.
    1.15 @@ -67,7 +68,7 @@
    1.16                  uint deviceInfoSize = (uint)Marshal.SizeOf(typeof(RID_DEVICE_INFO));
    1.17                  deviceInfoBuffer = Marshal.AllocHGlobal((int)deviceInfoSize);
    1.18  
    1.19 -                int res = Win32.Function.GetRawInputDeviceInfo(hDevice, Const.RIDI_DEVICEINFO, deviceInfoBuffer, ref deviceInfoSize);
    1.20 +                int res = Win32.Function.GetRawInputDeviceInfo(hDevice, Win32.RawInputDeviceInfoType.RIDI_DEVICEINFO, deviceInfoBuffer, ref deviceInfoSize);
    1.21                  if (res <= 0)
    1.22                  {
    1.23                      Debug.WriteLine("WM_INPUT could not read device info: " + Marshal.GetLastWin32Error().ToString());
    1.24 @@ -99,8 +100,8 @@
    1.25          /// <returns></returns>
    1.26          public static IntPtr GetPreParsedData(IntPtr hDevice)
    1.27          {
    1.28 -            uint ppDataSize = 256;
    1.29 -            int result = Win32.Function.GetRawInputDeviceInfo(hDevice, Win32.Const.RIDI_PREPARSEDDATA, IntPtr.Zero, ref ppDataSize);
    1.30 +            uint ppDataSize = 0;
    1.31 +            int result = Win32.Function.GetRawInputDeviceInfo(hDevice, RawInputDeviceInfoType.RIDI_PREPARSEDDATA, IntPtr.Zero, ref ppDataSize);
    1.32              if (result != 0)
    1.33              {
    1.34                  Debug.WriteLine("Failed to get raw input pre-parsed data size" + result + Marshal.GetLastWin32Error());
    1.35 @@ -108,7 +109,7 @@
    1.36              }
    1.37  
    1.38              IntPtr ppData = Marshal.AllocHGlobal((int)ppDataSize);
    1.39 -            result = Win32.Function.GetRawInputDeviceInfo(hDevice, Win32.Const.RIDI_PREPARSEDDATA, ppData, ref ppDataSize);
    1.40 +            result = Win32.Function.GetRawInputDeviceInfo(hDevice, RawInputDeviceInfoType.RIDI_PREPARSEDDATA, ppData, ref ppDataSize);
    1.41              if (result <= 0)
    1.42              {
    1.43                  Debug.WriteLine("Failed to get raw input pre-parsed data" + result + Marshal.GetLastWin32Error());
    1.44 @@ -125,7 +126,7 @@
    1.45          public static string GetDeviceName(IntPtr device)
    1.46          {
    1.47              uint deviceNameSize = 256;
    1.48 -            int result = Win32.Function.GetRawInputDeviceInfo(device, Win32.Const.RIDI_DEVICENAME, IntPtr.Zero, ref deviceNameSize);
    1.49 +            int result = Win32.Function.GetRawInputDeviceInfo(device, RawInputDeviceInfoType.RIDI_DEVICENAME, IntPtr.Zero, ref deviceNameSize);
    1.50              if (result != 0)
    1.51              {
    1.52                  return string.Empty;
    1.53 @@ -134,7 +135,7 @@
    1.54              IntPtr deviceName = Marshal.AllocHGlobal((int)deviceNameSize * 2);  // size is the character count not byte count
    1.55              try
    1.56              {
    1.57 -                result = Win32.Function.GetRawInputDeviceInfo(device, Win32.Const.RIDI_DEVICENAME, deviceName, ref deviceNameSize);
    1.58 +                result = Win32.Function.GetRawInputDeviceInfo(device, RawInputDeviceInfoType.RIDI_DEVICENAME, deviceName, ref deviceNameSize);
    1.59                  if (result > 0)
    1.60                  {
    1.61                      return Marshal.PtrToStringAnsi(deviceName, result - 1); // -1 for NULL termination
    1.62 @@ -149,5 +150,41 @@
    1.63          }
    1.64  
    1.65  
    1.66 +        /// <summary>
    1.67 +        /// 
    1.68 +        /// </summary>
    1.69 +        /// <param name="aTreeView"></param>
    1.70 +        public static void PopulateDeviceList(TreeView aTreeView)
    1.71 +        {
    1.72 +
    1.73 +            //Get our list of devices
    1.74 +            RAWINPUTDEVICELIST[] ridList = null;
    1.75 +            uint deviceCount = 0;
    1.76 +            int res = Win32.Function.GetRawInputDeviceList(ridList, ref deviceCount,(uint)Marshal.SizeOf(typeof(RAWINPUTDEVICELIST)));
    1.77 +            if (res == -1)
    1.78 +            {
    1.79 +                //Just give up then
    1.80 +                return;
    1.81 +            }
    1.82 +
    1.83 +            ridList = new RAWINPUTDEVICELIST[deviceCount];
    1.84 +            res = Win32.Function.GetRawInputDeviceList(ridList, ref deviceCount, (uint)Marshal.SizeOf(typeof(RAWINPUTDEVICELIST)));
    1.85 +            if (res != deviceCount)
    1.86 +            {
    1.87 +                //Just give up then
    1.88 +                return;
    1.89 +            }
    1.90 +
    1.91 +            //For each our device add an node to our treeview
    1.92 +            foreach (RAWINPUTDEVICELIST device in ridList)
    1.93 +            {
    1.94 +                string name=GetDeviceName(device.hDevice);
    1.95 +                aTreeView.Nodes.Add(name, name);
    1.96 +            }
    1.97 +
    1.98 +            //TreeNode node = null;
    1.99 +
   1.100 +        }
   1.101 +
   1.102      }
   1.103  }
   1.104 \ No newline at end of file