예제 #1
0
        private bool WriteData(byte[] data, int timeout)
        {
            if (_deviceCapabilities.OutputReportByteLength <= 0) return false;

            var buffer = CreateOutputBuffer();
            uint bytesWritten = 0;

            Array.Copy(data, 0, buffer, 0, Math.Min(data.Length, _deviceCapabilities.OutputReportByteLength));

            if (_deviceWriteMode == DeviceMode.Overlapped)
            {
                var security = new NativeMethods.SECURITY_ATTRIBUTES();
                var overlapped = new NativeOverlapped();

                var overlapTimeout = timeout <= 0 ? NativeMethods.WAIT_INFINITE : timeout;

                security.lpSecurityDescriptor = IntPtr.Zero;
                security.bInheritHandle = true;
                security.nLength = Marshal.SizeOf(security);

                overlapped.OffsetLow = 0;
                overlapped.OffsetHigh = 0;
                overlapped.EventHandle = NativeMethods.CreateEvent(ref security, Convert.ToInt32(false), Convert.ToInt32(true), "");

                try
                {
                    NativeMethods.WriteFile(WriteHandle, buffer, (uint)buffer.Length, out bytesWritten, ref overlapped);
                }
                catch { return false; }

                var result = NativeMethods.WaitForSingleObject(overlapped.EventHandle, overlapTimeout);

                switch (result)
                {
                    case NativeMethods.WAIT_OBJECT_0:
                        return true;
                    case NativeMethods.WAIT_TIMEOUT:
                        return false;
                    case NativeMethods.WAIT_FAILED:
                        return false;
                    default:
                        return false;
                }
            }
            else
            {
                try
                {
                    var overlapped = new NativeOverlapped();
                    return NativeMethods.WriteFile(WriteHandle, buffer, (uint)buffer.Length, out bytesWritten, ref overlapped);
                }
                catch { return false; }
            }
        }
예제 #2
0
        private static IntPtr OpenDeviceIO(string devicePath, DeviceMode deviceMode, uint deviceAccess)
        {
            var security = new NativeMethods.SECURITY_ATTRIBUTES();
            var flags = 0;

            if (deviceMode == DeviceMode.Overlapped) flags = NativeMethods.FILE_FLAG_OVERLAPPED;

            security.lpSecurityDescriptor = IntPtr.Zero;
            security.bInheritHandle = true;
            security.nLength = Marshal.SizeOf(security);

            return NativeMethods.CreateFile(devicePath, deviceAccess, NativeMethods.FILE_SHARE_READ | NativeMethods.FILE_SHARE_WRITE, ref security, NativeMethods.OPEN_EXISTING, flags, 0);
        }
예제 #3
0
        private HidDeviceData ReadData(int timeout)
        {
            var buffer = new byte[] { };
            var status = HidDeviceData.ReadStatus.NoDataRead;

            if (_deviceCapabilities.InputReportByteLength > 0)
            {
                uint bytesRead = 0;

                buffer = CreateInputBuffer();

                if (_deviceReadMode == DeviceMode.Overlapped)
                {
                    var security = new NativeMethods.SECURITY_ATTRIBUTES();
                    var overlapped = new NativeOverlapped();
                    var overlapTimeout = timeout <= 0 ? NativeMethods.WAIT_INFINITE : timeout;

                    security.lpSecurityDescriptor = IntPtr.Zero;
                    security.bInheritHandle = true;
                    security.nLength = Marshal.SizeOf(security);

                    overlapped.OffsetLow = 0;
                    overlapped.OffsetHigh = 0;
                    overlapped.EventHandle = NativeMethods.CreateEvent(ref security, Convert.ToInt32(false), Convert.ToInt32(true), string.Empty);

                    try
                    {
                        NativeMethods.ReadFile(ReadHandle, buffer, (uint)buffer.Length, out bytesRead, ref overlapped);

                        var result = NativeMethods.WaitForSingleObject(overlapped.EventHandle, overlapTimeout);

                        switch (result)
                        {
                            case NativeMethods.WAIT_OBJECT_0: status = HidDeviceData.ReadStatus.Success; break;
                            case NativeMethods.WAIT_TIMEOUT:
                                status = HidDeviceData.ReadStatus.WaitTimedOut;
                                buffer = new byte[] {};
                                break;
                            case NativeMethods.WAIT_FAILED:
                                status = HidDeviceData.ReadStatus.WaitFail;
                                buffer = new byte[] { };
                                break;
                            default:
                                status = HidDeviceData.ReadStatus.NoDataRead;
                                buffer = new byte[] { };
                                break;
                        }
                    }
                    catch { status = HidDeviceData.ReadStatus.ReadError; }
                    finally { CloseDeviceIO(overlapped.EventHandle); }
                }
                else
                {
                    try
                    {
                        var overlapped = new NativeOverlapped();

                        NativeMethods.ReadFile(ReadHandle, buffer, (uint)buffer.Length, out bytesRead, ref overlapped);
                        status = HidDeviceData.ReadStatus.Success;
                    }
                    catch { status = HidDeviceData.ReadStatus.ReadError; }
                }
            }
            return new HidDeviceData(buffer, status);
        }
예제 #4
0
        protected HidDeviceData ReadData(int timeout)
        {
            var buffer = new byte[] { };
            var status = HidDeviceData.ReadStatus.NoDataRead;

            if (_deviceCapabilities.InputReportByteLength > 0)
            {
                uint bytesRead = 0;

                buffer = CreateInputBuffer();

                if (_deviceReadMode == DeviceMode.Overlapped)
                {
                    var security       = new NativeMethods.SECURITY_ATTRIBUTES();
                    var overlapped     = new NativeOverlapped();
                    var overlapTimeout = timeout <= 0 ? NativeMethods.WAIT_INFINITE : timeout;

                    security.lpSecurityDescriptor = IntPtr.Zero;
                    security.bInheritHandle       = true;
                    security.nLength = Marshal.SizeOf(security);

                    overlapped.OffsetLow   = 0;
                    overlapped.OffsetHigh  = 0;
                    overlapped.EventHandle = NativeMethods.CreateEvent(ref security, Convert.ToInt32(false), Convert.ToInt32(true), string.Empty);

                    try
                    {
                        NativeMethods.ReadFile(Handle, buffer, (uint)buffer.Length, out bytesRead, ref overlapped);

                        var result = NativeMethods.WaitForSingleObject(overlapped.EventHandle, overlapTimeout);

                        switch (result)
                        {
                        case NativeMethods.WAIT_OBJECT_0: status = HidDeviceData.ReadStatus.Success; break;

                        case NativeMethods.WAIT_TIMEOUT:
                            status = HidDeviceData.ReadStatus.WaitTimedOut;
                            buffer = new byte[] { };
                            break;

                        case NativeMethods.WAIT_FAILED:
                            status = HidDeviceData.ReadStatus.WaitFail;
                            buffer = new byte[] { };
                            break;

                        default:
                            status = HidDeviceData.ReadStatus.NoDataRead;
                            buffer = new byte[] { };
                            break;
                        }
                    }
                    catch { status = HidDeviceData.ReadStatus.ReadError; }
                    finally { CloseDeviceIO(overlapped.EventHandle); }
                }
                else
                {
                    try
                    {
                        var overlapped = new NativeOverlapped();

                        NativeMethods.ReadFile(Handle, buffer, (uint)buffer.Length, out bytesRead, ref overlapped);
                        status = HidDeviceData.ReadStatus.Success;
                    }
                    catch { status = HidDeviceData.ReadStatus.ReadError; }
                }
            }
            return(new HidDeviceData(buffer, status));
        }