RawInput.cs
author sl
Fri, 07 Nov 2014 22:12:22 +0100
changeset 10 17f8421146ba
child 17 8f7e35c3bfd1
permissions -rw-r--r--
More cleanup and re-org.
sl@10
     1
using System;
sl@10
     2
using System.Runtime.InteropServices;
sl@10
     3
using System.Diagnostics;
sl@10
     4
sl@10
     5
sl@10
     6
namespace Win32
sl@10
     7
{
sl@10
     8
    static class RawInput
sl@10
     9
    {
sl@10
    10
        /// <summary>
sl@10
    11
        /// 
sl@10
    12
        /// </summary>
sl@10
    13
        /// <param name="aRawInputHandle"></param>
sl@10
    14
        /// <param name="aRawInput"></param>
sl@10
    15
        /// <param name="rawInputBuffer">Caller must free up memory on the pointer using Marshal.FreeHGlobal</param>
sl@10
    16
        /// <returns></returns>
sl@10
    17
        public static bool GetRawInputData(IntPtr aRawInputHandle, ref RAWINPUT aRawInput, ref IntPtr rawInputBuffer)
sl@10
    18
        {
sl@10
    19
            bool success = true;
sl@10
    20
            rawInputBuffer = IntPtr.Zero;
sl@10
    21
sl@10
    22
            try
sl@10
    23
            {
sl@10
    24
                uint dwSize = 0;
sl@10
    25
                uint sizeOfHeader = (uint)Marshal.SizeOf(typeof(RAWINPUTHEADER));
sl@10
    26
sl@10
    27
                //Get the size of our raw input data.
sl@10
    28
                Win32.Function.GetRawInputData(aRawInputHandle, Const.RID_INPUT, IntPtr.Zero, ref dwSize, sizeOfHeader);
sl@10
    29
sl@10
    30
                //Allocate a large enough buffer
sl@10
    31
                 rawInputBuffer = Marshal.AllocHGlobal((int)dwSize);
sl@10
    32
sl@10
    33
                //Now read our RAWINPUT data
sl@10
    34
                if (Win32.Function.GetRawInputData(aRawInputHandle, Const.RID_INPUT, rawInputBuffer, ref dwSize, (uint)Marshal.SizeOf(typeof(RAWINPUTHEADER))) != dwSize)
sl@10
    35
                {
sl@10
    36
                    return false;
sl@10
    37
                }
sl@10
    38
sl@10
    39
                //Cast our buffer
sl@10
    40
                aRawInput = (RAWINPUT)Marshal.PtrToStructure(rawInputBuffer, typeof(RAWINPUT));
sl@10
    41
            }
sl@10
    42
            catch
sl@10
    43
            {
sl@10
    44
                Debug.WriteLine("GetRawInputData failed!");
sl@10
    45
                success = false;
sl@10
    46
            }
sl@10
    47
sl@10
    48
            return success;
sl@10
    49
        }
sl@10
    50
sl@10
    51
        /// <summary>
sl@10
    52
        /// 
sl@10
    53
        /// </summary>
sl@10
    54
        /// <param name="aRawInputHandle"></param>
sl@10
    55
        /// <param name="aUsagePage"></param>
sl@10
    56
        /// <param name="aUsage"></param>
sl@10
    57
        /// <returns></returns>
sl@10
    58
        public static bool GetDeviceInfo(IntPtr hDevice, ref RID_DEVICE_INFO deviceInfo)
sl@10
    59
        {
sl@10
    60
            bool success = true;
sl@10
    61
            IntPtr deviceInfoBuffer = IntPtr.Zero;
sl@10
    62
            try
sl@10
    63
            {
sl@10
    64
                //Get Device Info
sl@10
    65
                uint deviceInfoSize = (uint)Marshal.SizeOf(typeof(RID_DEVICE_INFO));
sl@10
    66
                deviceInfoBuffer = Marshal.AllocHGlobal((int)deviceInfoSize);
sl@10
    67
sl@10
    68
                int res = Win32.Function.GetRawInputDeviceInfo(hDevice, Const.RIDI_DEVICEINFO, deviceInfoBuffer, ref deviceInfoSize);
sl@10
    69
                if (res <= 0)
sl@10
    70
                {
sl@10
    71
                    Debug.WriteLine("WM_INPUT could not read device info: " + Marshal.GetLastWin32Error().ToString());
sl@10
    72
                    return false;
sl@10
    73
                }
sl@10
    74
sl@10
    75
                //Cast our buffer
sl@10
    76
                deviceInfo = (RID_DEVICE_INFO)Marshal.PtrToStructure(deviceInfoBuffer, typeof(RID_DEVICE_INFO));
sl@10
    77
            }
sl@10
    78
            catch
sl@10
    79
            {
sl@10
    80
                Debug.WriteLine("GetRawInputData failed!");
sl@10
    81
                success = false;
sl@10
    82
            }
sl@10
    83
            finally
sl@10
    84
            {
sl@10
    85
                //Always executes, prevents memory leak
sl@10
    86
                Marshal.FreeHGlobal(deviceInfoBuffer);
sl@10
    87
            }
sl@10
    88
sl@10
    89
            
sl@10
    90
            return success;
sl@10
    91
        }
sl@10
    92
sl@10
    93
    }
sl@10
    94
}