Esempio n. 1
0
        public static RAWINPUTDEVICELIST[] GetInputDeviceList()
        {
            uint deviceCount = 0;
            int  size        = Marshal.SizeOf(typeof(RAWINPUTDEVICELIST));

            if (GetRawInputDeviceList(IntPtr.Zero, ref deviceCount, size) != 0)
            {
                throw new Win32Exception("Failed to get raw input devices");
            }

            RAWINPUTDEVICELIST[] rawInputDevices = new RAWINPUTDEVICELIST[deviceCount + 1]; // TODO: This is right? deviceCount + 1?
            if (deviceCount > 0)
            {
                IntPtr data = Marshal.AllocHGlobal((int)(size * deviceCount));
                try
                {
                    GetRawInputDeviceList(data, ref deviceCount, size);
                    for (int i = 0; i < deviceCount; i++)
                    {
                        RAWINPUTDEVICELIST rid = (RAWINPUTDEVICELIST)Marshal.PtrToStructure(
                            new IntPtr((data.ToInt32() + (size * i))), typeof(RAWINPUTDEVICELIST));
                        rawInputDevices[i] = rid;
                    }
                }
                finally
                {
                    Marshal.FreeHGlobal(data);
                }
            }
            return(rawInputDevices);
        }
        public static RAWINPUTDEVICELIST[] GetInputDeviceList()
        {
            uint deviceCount = 0;
            int size = Marshal.SizeOf(typeof(RAWINPUTDEVICELIST));

            if (GetRawInputDeviceList(IntPtr.Zero, ref deviceCount, size) != 0)
                throw new Win32Exception("Failed to get raw input devices");

            RAWINPUTDEVICELIST[] rawInputDevices = new RAWINPUTDEVICELIST[deviceCount + 1]; // TODO: This is right? deviceCount + 1?
            if (deviceCount > 0)
            {
                IntPtr data = Marshal.AllocHGlobal((int)(size * deviceCount));
                try
                {
                    GetRawInputDeviceList(data, ref deviceCount, size);
                    for (int i = 0; i < deviceCount; i++)
                    {
                        RAWINPUTDEVICELIST rid = (RAWINPUTDEVICELIST)Marshal.PtrToStructure(
                            new IntPtr((data.ToInt32() + (size * i))), typeof(RAWINPUTDEVICELIST));
                        rawInputDevices[i] = rid;
                    }
                }
                finally
                {
                    Marshal.FreeHGlobal(data);
                }
            }
            return rawInputDevices;
        }