CreateEvent() 개인적인 메소드

private CreateEvent ( SECURITY_ATTRIBUTES &securityAttributes, int bManualReset, int bInitialState, string lpName ) : IntPtr
securityAttributes SECURITY_ATTRIBUTES
bManualReset int
bInitialState int
lpName string
리턴 System.IntPtr
예제 #1
0
        private HidDeviceData ReadData(int timeout)
        {
            var buffer = new byte[Capabilities.InputReportByteLength];
            var status = HidDeviceData.ReadStatus.NoDataRead;

            if (deviceCapabilities.InputReportByteLength > 0)
            {
                var bytesRead = 0;

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

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

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

                    try
                    {
                        NativeMethods.ReadFileOverlapped(ReadHandle, ref buffer[0], buffer.Length, ref bytesRead, ref overlapped);

                        var result = NativeMethods.WaitForSingleObject(overlapped.hEvent, 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; }
                }
                else
                {
                    try
                    {
                        NativeMethods.ReadFile(ReadHandle, ref buffer[0], buffer.Length, ref bytesRead, IntPtr.Zero);
                        if (bytesRead > 0)
                        {
                            status = HidDeviceData.ReadStatus.Success;
                        }
                    }
                    catch { status = HidDeviceData.ReadStatus.ReadError; }
                }
            }
            return(new HidDeviceData(buffer, status));
        }
예제 #2
0
        protected HidDeviceData ReadData(int timeout)
        {
            var buffer = new byte[] { };
            var status = HidDeviceData.ReadStatus.NoDataRead;

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



                if (_deviceReadMode == DeviceMode.Overlapped)
                {
                    if (!reading)
                    {
                        read_buffer = CreateInputBuffer();
                        var security = new NativeMethods.SECURITY_ATTRIBUTES();
                        read_overlapped = new NativeOverlapped();

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

                        read_overlapped.OffsetLow   = 0;
                        read_overlapped.OffsetHigh  = 0;
                        read_overlapped.EventHandle = NativeMethods.CreateEvent(ref security, Convert.ToInt32(false), Convert.ToInt32(true), string.Empty);
                        NativeMethods.ReadFile(Handle, read_buffer, (uint)read_buffer.Length, out bytesRead, ref read_overlapped);
                    }

                    try
                    {
                        var result = NativeMethods.WaitForSingleObject(read_overlapped.EventHandle, timeout);

                        switch (result)
                        {
                        case NativeMethods.WAIT_OBJECT_0: status = HidDeviceData.ReadStatus.Success; reading = false; CloseDeviceIO(read_overlapped.EventHandle); return(new HidDeviceData(read_buffer, status));

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

                        case NativeMethods.WAIT_FAILED:
                            status  = HidDeviceData.ReadStatus.WaitFail;
                            buffer  = new byte[] { };
                            reading = false;
                            CloseDeviceIO(read_overlapped.EventHandle);
                            break;

                        default:
                            status  = HidDeviceData.ReadStatus.NoDataRead;
                            buffer  = new byte[] { };
                            reading = false;
                            CloseDeviceIO(read_overlapped.EventHandle);
                            break;
                        }
                    }
                    catch { status = HidDeviceData.ReadStatus.ReadError; reading = false; CloseDeviceIO(read_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));
        }
예제 #3
0
        protected HidDeviceData ReadData(int timeout)
        {
            var    buffer = new byte[] { };
            var    status = HidDeviceData.ReadStatus.NoDataRead;
            IntPtr nonManagedBuffer;

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

                buffer           = CreateInputBuffer();
                nonManagedBuffer = Marshal.AllocHGlobal(buffer.Length);

                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
                    {
                        var success = NativeMethods.ReadFile(Handle, nonManagedBuffer, (uint)buffer.Length, out bytesRead, ref overlapped);

                        if (success)
                        {
                            status = HidDeviceData.ReadStatus.Success; // No check here to see if bytesRead > 0 . Perhaps not necessary?
                        }
                        else
                        {
                            var result = NativeMethods.WaitForSingleObject(overlapped.EventHandle, overlapTimeout);

                            switch (result)
                            {
                            case NativeMethods.WAIT_OBJECT_0:
                                status = HidDeviceData.ReadStatus.Success;
                                NativeMethods.GetOverlappedResult(Handle, ref overlapped, out bytesRead, false);
                                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;
                            }
                        }
                        Marshal.Copy(nonManagedBuffer, buffer, 0, (int)bytesRead);
                    }
                    catch { status = HidDeviceData.ReadStatus.ReadError; }
                    finally {
                        CloseDeviceIO(overlapped.EventHandle);
                        Marshal.FreeHGlobal(nonManagedBuffer);
                    }
                }
                else
                {
                    try
                    {
                        var overlapped = new NativeOverlapped();

                        NativeMethods.ReadFile(Handle, nonManagedBuffer, (uint)buffer.Length, out bytesRead, ref overlapped);
                        status = HidDeviceData.ReadStatus.Success;
                        Marshal.Copy(nonManagedBuffer, buffer, 0, (int)bytesRead);
                    }
                    catch { status = HidDeviceData.ReadStatus.ReadError; }
                    finally { Marshal.FreeHGlobal(nonManagedBuffer); }
                }
            }
            return(new HidDeviceData(buffer, status));
        }
예제 #4
0
        private bool WriteData(byte[] data, int timeout)
        {
            if (_deviceCapabilities.OutputReportByteLength <= 0)
            {
                return(false);
            }
            uint bytesWritten = 0;

            // Optimization: Don't create a new buffer & copy if given data array is of sufficient size.
            byte[] buffer;
            if (data.Length == _deviceCapabilities.OutputReportByteLength)
            {
                buffer = data;
            }
            else
            {
                buffer = CreateOutputBuffer();
                Array.Copy(data, 0, buffer, 0, Math.Min(data.Length, _deviceCapabilities.OutputReportByteLength));
            }

            // TODO: Do that thing that tells the CLR not to move the 'byte[] data' object around in memory.
            // As there may be a race condition if the location moves during the Write() Win32 call.

            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(Handle, 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:
                    NativeMethods.CloseHandle(overlapped.EventHandle);
                    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(Handle, buffer, (uint)buffer.Length, out bytesWritten, ref overlapped));
                }
                catch { return(false); }
            }
        }
예제 #5
0
        private bool WriteData(byte[] data, int timeout, bool ignoreOutputReportByteLength = false)
        {
            if (!ignoreOutputReportByteLength && _deviceCapabilities.OutputReportByteLength <= 0)
            {
                return(false);
            }

            var buffer = (ignoreOutputReportByteLength)
                                        ? new byte[data.Length] : CreateOutputBuffer();
            uint bytesWritten = 0;
            var  length       = (ignoreOutputReportByteLength)
                                        ? data.Length
                                        : Math.Min(data.Length, _deviceCapabilities.OutputReportByteLength);

            Array.Copy(data, 0, buffer, 0, length);

            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(Handle, 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(Handle, buffer, (uint)buffer.Length, out bytesWritten, ref overlapped));
                }
                catch { return(false); }
            }
        }
예제 #6
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)
                {
                    //IntPtr unManagedBuffer = IntPtr.Zero;
                    //IntPtr unManagedOverlapped = IntPtr.Zero;
                    var unManagedBytesRead = IntPtr.Zero;
                    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(true),
                                                                       Convert.ToInt32(true), string.Empty);

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

                        /*unManagedBuffer = Marshal.AllocHGlobal(buffer.Length);
                         * unManagedOverlapped = Marshal.AllocHGlobal(Marshal.SizeOf(overlapped));
                         * Marshal.StructureToPtr(overlapped, unManagedOverlapped, false);
                         *
                         * //var resultReadFile = NativeMethods.ReadFile(Handle, buffer, (uint) buffer.Length, out bytesRead, ref overlapped);
                         *
                         *
                         * var resultReadFile = NativeMethods.ReadFileUnsafe(Handle, unManagedBuffer,(uint) buffer.Length, unManagedBytesRead,  (NativeOverlapped*)unManagedOverlapped);
                         */
                        if (!resultReadFile && Marshal.GetLastWin32Error() == NativeMethods.ERROR_IO_PENDING)
                        {
                            Console.Out.WriteLine("Marshal.GetLastWin32Error() --------------->  " + Marshal.GetLastWin32Error());
                            var result = NativeMethods.WaitForSingleObject(overlapped.EventHandle, overlapTimeout);
                            //Console.Out.WriteLine("******************BBBBB*********************");
                            //Console.Out.WriteLine("NativeMethods.WaitForSingleObject() --------------->  " + result);
                            switch (result)
                            {
                            case NativeMethods.WAIT_OBJECT_0:
                                status = HidDeviceData.ReadStatus.Success;
                                var stringBuilder = new StringBuilder();

                                //stringBuilder.AppendLine("Length = " + buffer.Length);
                                for (int i = 0; i < buffer.Length; i++)
                                {
                                    stringBuilder.AppendLine("[" + i + "] = " + buffer[i] + " ");
                                }
                                Console.Out.WriteLine("Before GetOverlappedResult bytesRead :  " + bytesRead +
                                                      "\n" + stringBuilder.ToString());

                                //var fresult = NativeMethods.GetOverlappedResultUnsafe(Handle, (NativeOverlapped*)unManagedOverlapped, out bytesRead, true);
                                var fresult = NativeMethods.GetOverlappedResult(Handle, ref overlapped, out bytesRead, true);
                                Console.Out.WriteLine("NativeMethods.GetOverlappedResult result is : " + fresult);
                                NativeMethods.ResetEvent(overlapped.EventHandle);
                                stringBuilder.Clear();
                                //stringBuilder.AppendLine("Length = " + buffer.Length);
                                //buffer = (byte[]) unManagedBuffer
                                for (int i = 0; i < buffer.Length; i++)
                                {
                                    stringBuilder.AppendLine("[" + i + "] = " + buffer[i] + " ");
                                }
                                Console.Out.WriteLine("After GetOverlappedResult bytesRead :  " + bytesRead +
                                                      "\n" + stringBuilder.ToString());
                                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 (Exception ex)
                    {
                        status = HidDeviceData.ReadStatus.ReadError;
                        //Console.Out.WriteLine("catch! --------------->  " + ex.Message + "\n" + ex.StackTrace);
                    }
                    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));
        }