示例#1
0
        public void Open()
        {
            try
            {
                _deviceHandle = NativeInterface.CreateFile_SafeFileHandle(_devicePath,
                                                                          (NativeInterface.GENERIC_WRITE | NativeInterface.GENERIC_READ),
                                                                          NativeInterface.FILE_SHARE_READ | NativeInterface.FILE_SHARE_WRITE,
                                                                          IntPtr.Zero,
                                                                          NativeInterface.OPEN_EXISTING,
                                                                          NativeInterface.FILE_ATTRIBUTE_NORMAL | NativeInterface.FILE_FLAG_OVERLAPPED,
                                                                          0);
                if (_deviceHandle.IsInvalid)
                {
                    throw new Win32Exception("Failed to open WinUSB device handle.");
                }

                _InitializeDevice();
            }
            catch (Exception)
            {
                if (_deviceHandle != null)
                {
                    _deviceHandle.Dispose();
                    _deviceHandle = null;
                }

                _FreeWinUSB();
                throw;
            }
        }