Пример #1
0
        /// <summary>
        /// Function to enumerate raw input devices.
        /// </summary>
        /// <returns>An array of raw input device structures.</returns>
        public unsafe static RAWINPUTDEVICELIST[] EnumerateInputDevices()
        {
            int deviceCount = 0;
            int structSize  = DirectAccess.SizeOf <RAWINPUTDEVICELIST>();

            if (GetRawInputDeviceList(IntPtr.Zero, ref deviceCount, structSize) < 0)
            {
                throw new Win32Exception();
            }

            if (deviceCount == 0)
            {
                return(new RAWINPUTDEVICELIST[0]);
            }

            RAWINPUTDEVICELIST *deviceListPtr = stackalloc RAWINPUTDEVICELIST[deviceCount];

            if (GetRawInputDeviceList((IntPtr)deviceListPtr, ref deviceCount, structSize) < 0)
            {
                throw new Win32Exception();
            }

            var result = new RAWINPUTDEVICELIST[deviceCount];

            fixed(RAWINPUTDEVICELIST *resultPtr = &result[0])
            {
                DirectAccess.MemoryCopy(resultPtr, deviceListPtr, structSize * deviceCount);
            }

            return(result);
        }
Пример #2
0
 private static extern unsafe int GetRawInputDeviceList(RAWINPUTDEVICELIST *pRawInputDeviceList, ref int puiNumDevices, int cbSize);