public static CreateFileFromDevice ( string filename, EFileAccess desiredAccess, EFileShare shareMode ) : |
||
filename | string | |
desiredAccess | EFileAccess | |
shareMode | EFileShare | |
return |
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); }
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(); }
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(); }
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; } } }
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); }
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(); }
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); }