Beispiel #1
0
        private bool OpenFileHandle(Win32FileAccess desiredAccess)
        {
            handle = Kernel32Methods.CreateFile(
                devicePath,
                desiredAccess,
                FileShare.ReadWrite, IntPtr.Zero, FileMode.Open, (uint)Win32FileAttributes.Overlapped, IntPtr.Zero);

            return(handle.IsInvalid == false);
        }
Beispiel #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="HidStream"/> class.
        /// </summary>
        /// <param name="devicePath">The path of the associated device.</param>
        public HidStream(string devicePath)
        {
            this.devicePath = devicePath;

            // Create the file handler from the device path
            // Win10 requires shared access
            this.handle = Kernel32Methods.CreateFile(
                devicePath,
                Win32Api.Win32FileAccess.GenericRead | Win32FileAccess.GenericWrite,
                FileShare.ReadWrite, IntPtr.Zero, FileMode.Open, Win32FileAttributes.Overlapped, IntPtr.Zero);

            //this.handle = Kernel32Methods.CreateFile(
            //    devicePath,
            //    Win32Api.Win32FileAccess.GenericRead | Win32FileAccess.GenericWrite,
            //    FileShare.ReadWrite, IntPtr.Zero, FileMode.Open, Win32FileAttributes.Overlapped, IntPtr.Zero);

            if (this.handle.IsInvalid)
            {
                // TODO: raise a better exception
                throw new Exception("Failed to create device file", new Win32Exception(Marshal.GetLastWin32Error()));
            }
        }