CreateFileFromDevice() public static method

public static CreateFileFromDevice ( string filename, EFileAccess desiredAccess, EFileShare shareMode ) : IntPtr
filename string
desiredAccess EFileAccess
shareMode EFileShare
return System.IntPtr
Ejemplo n.º 1
0
        protected override bool TryCreateDevice(object key, out HidDevice device, out object completionState)
        {
            string path      = (string)key;
            var    hidDevice = new WinHidDevice(path);
            IntPtr handle    = NativeMethods.CreateFileFromDevice(path, NativeMethods.EFileAccess.None, NativeMethods.EFileShare.All);

            device          = null;
            completionState = null;
            if (handle == (IntPtr)(-1))
            {
                return(false);
            }

            bool ok = false;

            try {
                ok = hidDevice.GetInfo(handle);
            } catch {
            }
            if (!ok)
            {
                NativeMethods.CloseHandle(handle);
                return(false);
            }

            device          = hidDevice;
            completionState = handle;
            return(true);
        }
Ejemplo n.º 2
0
        internal void Init(string devicePath)
        {
            IntPtr handle = NativeMethods.CreateFileFromDevice(devicePath,
                                                               NativeMethods.EFileAccess.Read | NativeMethods.EFileAccess.Write,
                                                               NativeMethods.EFileShare.None);

            if (handle == (IntPtr)(-1))
            {
                int hr = Marshal.GetHRForLastWin32Error();
                throw DeviceException.CreateIOException(Device, "Unable to open serial device (" + devicePath + ").", hr);
            }

            var timeouts = new NativeMethods.COMMTIMEOUTS();

            timeouts.ReadIntervalTimeout        = uint.MaxValue;
            timeouts.ReadTotalTimeoutConstant   = uint.MaxValue - 1; // CP210x fails if this is set to uint.MaxValue.
            timeouts.ReadTotalTimeoutMultiplier = uint.MaxValue;
            if (!NativeMethods.SetCommTimeouts(handle, out timeouts))
            {
                int hr = Marshal.GetHRForLastWin32Error();
                NativeMethods.CloseHandle(handle);
                throw DeviceException.CreateIOException(Device, "Unable to set serial timeouts.", hr);
            }

            _handle = handle;
            HandleInitAndOpen();
        }
Ejemplo n.º 3
0
        internal void Init(string path, WinHidDevice device)
        {
            IntPtr handle = NativeMethods.CreateFileFromDevice(path, NativeMethods.EFileAccess.Read | NativeMethods.EFileAccess.Write, NativeMethods.EFileShare.All);

            if (handle == (IntPtr)(-1))
            {
                throw new IOException("Unable to open HID class device.");
            }

            _device = device;
            _handle = handle;
            HandleInitAndOpen();
        }
Ejemplo n.º 4
0
        internal unsafe void Init(string path)
        {
            IntPtr handle = NativeMethods.CreateFileFromDevice(path, NativeMethods.EFileAccess.Read | NativeMethods.EFileAccess.Write, 0);

            if (handle == (IntPtr)(-1))
            {
                throw DeviceException.CreateIOException(Device, "Unable to open BLE service (" + path + ").");
            }

            _handle = handle;
            HandleInitAndOpen();

            // Let's register to watch all possible characteristics.
            var watchedCharacteristics = new List <WinBleCharacteristic>();

            foreach (var characteristic in Service.GetCharacteristics())
            {
                if (characteristic.IsNotifiable || characteristic.IsIndicatable)
                {
                    watchedCharacteristics.Add((WinBleCharacteristic)characteristic);
                }
            }

            if (watchedCharacteristics.Count > 0)
            {
                var eb = stackalloc byte[checked (NativeMethods.BLUETOOTH_GATT_VALUE_CHANGED_EVENT_REGISTRATION.Size +
                                                  NativeMethods.BTH_LE_GATT_CHARACTERISTIC.Size * watchedCharacteristics.Count)];

                var er = (NativeMethods.BLUETOOTH_GATT_VALUE_CHANGED_EVENT_REGISTRATION *)eb;
                er->NumCharacteristics = (ushort)watchedCharacteristics.Count;
                eb += NativeMethods.BLUETOOTH_GATT_VALUE_CHANGED_EVENT_REGISTRATION.Size;

                for (int i = 0; i < watchedCharacteristics.Count; i++)
                {
                    var wc = watchedCharacteristics[i];
                    Marshal.StructureToPtr(wc.NativeData, (IntPtr)eb, false);
                    eb += NativeMethods.BTH_LE_GATT_CHARACTERISTIC.Size;

                    _watchMap[wc.NativeData.AttributeHandle] = wc;
                }

                _watchCallback = new NativeMethods.BLUETOOTH_GATT_EVENT_CALLBACK(EventCallback);
                int error = NativeMethods.BluetoothGATTRegisterEvent(handle, NativeMethods.BTH_LE_GATT_EVENT_TYPE.CharacteristicValueChangedEvent,
                                                                     er, _watchCallback, IntPtr.Zero, out _watchRegisterEventHandle);
                if (error != 0)
                {
                    Debug.Assert(error == 0); _watchRegisterEventHandle = IntPtr.Zero;
                }
            }
        }
Ejemplo n.º 5
0
        public override string GetDeviceString(int index)
        {
            string        str;
            StringBuilder deviceString = new StringBuilder(256);
            var           handle       = NativeMethods.CreateFileFromDevice(_path, NativeMethods.EFileAccess.None, NativeMethods.EFileShare.Read | NativeMethods.EFileShare.Write);

            if (handle != (IntPtr)(-1) && NativeMethods.HidD_GetIndexedString(handle, (ulong)index, deviceString, 256))
            {
                str = deviceString.ToString();
            }
            else
            {
                str = null;
            }
            NativeMethods.CloseHandle(handle);
            return(str);
        }
Ejemplo n.º 6
0
        internal void Init(string path)
        {
            IntPtr handle = NativeMethods.CreateFileFromDevice(path, NativeMethods.EFileAccess.Read | NativeMethods.EFileAccess.Write, NativeMethods.EFileShare.Read | NativeMethods.EFileShare.Write);

            if (handle == (IntPtr)(-1))
            {
                throw DeviceException.CreateIOException(Device, "Unable to open HID class device (" + path + ").");
            }

            int maxInputBuffers = Environment.OSVersion.Version >= new Version(5, 1) ? 512 : 200; // Windows 2000 supports 200. Windows XP supports 512.

            if (!NativeMethods.HidD_SetNumInputBuffers(handle, maxInputBuffers))
            {
                NativeMethods.CloseHandle(handle);
                throw new IOException("Failed to set input buffers.", new Win32Exception());
            }

            _handle = handle;
            HandleInitAndOpen();
        }
Ejemplo n.º 7
0
        bool TryOpenToGetInfo(Func <IntPtr, bool> action)
        {
            var handle = NativeMethods.CreateFileFromDevice(_path, NativeMethods.EFileAccess.None, NativeMethods.EFileShare.Read | NativeMethods.EFileShare.Write);

            if (handle == (IntPtr)(-1))
            {
                return(false);
            }

            try
            {
                return(action(handle));
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
            }
            finally
            {
                NativeMethods.CloseHandle(handle);
            }

            return(false);
        }