//Name: SetDeviceState //Inputs: string[],bool //Outputs: bool //Errors: This method may throw the following exceptions. // Failed to enumerate device tree! //Remarks: This is nearly identical to the method above except it // tries to match the hardware description against the criteria // passed in. If a match is found, that device will the be // enabled or disabled based on bEnable. public bool SetDeviceState(string match, bool bEnable) { bool success = false; try { Guid myGUID = System.Guid.Empty; IntPtr hDevInfo = Native.SetupDiGetClassDevs(ref myGUID, 0, IntPtr.Zero, Native.DIGCF_ALLCLASSES | Native.DIGCF_PRESENT); if (hDevInfo.ToInt64() == Native.INVALID_HANDLE_VALUE)//changed from Int32 { return(false); } Native.SP_DEVINFO_DATA DeviceInfoData; DeviceInfoData = new Native.SP_DEVINFO_DATA(); DeviceInfoData.cbSize = Marshal.SizeOf(typeof(Native.SP_DEVINFO_DATA)); //is devices exist for class DeviceInfoData.devInst = 0; DeviceInfoData.classGuid = System.Guid.Empty; DeviceInfoData.reserved = 0; UInt32 i; StringBuilder DeviceName = new StringBuilder(""); DeviceName.Capacity = Native.MAX_DEV_LEN; for (i = 0; Native.SetupDiEnumDeviceInfo(hDevInfo, i, DeviceInfoData); i++) { if (!Native.SetupDiGetDeviceRegistryProperty(hDevInfo, DeviceInfoData, Native.SPDRP_DEVICEDESC, 0, DeviceName, Native.MAX_DEV_LEN, IntPtr.Zero)) { continue; } if (DeviceName.ToString().ToLower().Contains(match)) { if (this.ResetIt(hDevInfo, DeviceInfoData)) { success = true; } } } Native.SetupDiDestroyDeviceInfoList(hDevInfo); } catch (Exception ex) { Exceptions.Write(new Exception("Failed to enumerate device tree!", ex)); return(false); } return(success); }
private bool Send() { byte[] data; if (SLClient == null || DBSend == null || !SLClient.Connected || DBSend.IsAllRead || !DBSend.Read(out data)) { return(false); } byte[] result_data = TcpAsyncCommon.CreatePacket(data, PacketMode);// result.ToArray(); if (result_data.IsNullOrEmpty()) { return(false); } int sent = 0; int size = result_data.Length; SendTimeout.Reset(); do { int packet = size - sent; if (packet > TcpAsyncServer.PacketSizeTCP) { packet = TcpAsyncServer.PacketSizeTCP; } try { sent += SLClient.Send(result_data, sent, packet, SocketFlags.None); //Thread.Sleep(1); } catch (Exception ex) { Exceptions.Write(ex); this.StopClient(); return(false); } if (SendTimeout.IsTriggered) { return(false); } }while (sent < size); return(true); }
public static void SetDevicesEnabled(string classGuid, string instance_match, bool enable) { if (classGuid.IsNullOrWhiteSpace()) { return; } Guid guid = new Guid(classGuid); SafeDeviceInfoSetHandle diSetHandle = NativeMethods.SetupDiGetClassDevs(ref guid, null, IntPtr.Zero, SetupDiGetClassDevsFlags.Present); DeviceInfoData[] diData = GetDeviceInfoData(diSetHandle); if (diData.IsNullOrEmpty()) { return; } int[] indexes = GetIndexesOfInstance(diSetHandle, diData, instance_match); if (indexes.IsNullOrEmpty()) { return; } ExceptionBuffer Exceptions = new ExceptionBuffer(); foreach (int idx in indexes) { try { EnableDevice(diSetHandle, diData[idx], enable); } catch (Exception ex) { Exceptions.Write(ex); continue; } } if (diSetHandle != null) { if (diSetHandle.IsClosed == false) { diSetHandle.Close(); } diSetHandle.Dispose(); } }