private void MaintainPort() { WSDLogger.WriterDebugger("Start MaintainPort"); List <DeviceEntity> list = hc.GetHiddenDevice(); if (list != null && list.Count > 0) { IEnumerable <string> Ematchs = list.Select(p => p.DeviceID); if (Ematchs != null && Ematchs.Count() > 0) { try { hc.SetState(Ematchs.ToArray(), false); } catch (Exception es) { WSDLogger.WriterDebugger("MaintainPortCOM端口出错." + es.ToString()); } } } else { WSDLogger.WriterDebugger("没有发现隐藏COM端口."); } WSDLogger.WriterDebugger("End MaintainPort"); }
protected override void OnStart(string[] args) { try { WSDLogger.WriterDebugger("OnStart"); hc = new HardwareClass(); hc.AllowNotifications(this.ServiceHandle, true); MaintainPort(); } catch (Exception e) { WSDLogger.WriterDebugger("OnStart 发生错误" + e.ToString()); } }
public void Start() { try { WSDLogger.WriterDebugger("OnStart"); hc = new HardwareClass(); //hc.AllowNotifications(this.ServiceHandle, true); MaintainPort(); } catch (Exception e) { WSDLogger.WriterDebugger("OnStart 发生错误" + e.ToString()); } }
public List <DeviceEntity> GetHiddenDevice() { WSDLogger.WriterDebugger("Start GetHiddenDevice"); SP_DEVINFO_DATA DeviceInfoData = new SP_DEVINFO_DATA(); List <DeviceEntity> list = new List <DeviceEntity>(); List <DeviceEntity> listCurrent = new List <DeviceEntity>(); IntPtr hDevInfo = SetupDiGetClassDevs(GUID_DEVCLASS_PORTS, 0, IntPtr.Zero, DIGCF_NOSET); if (hDevInfo.ToInt64() == -1) { WSDLogger.WriterDebugger("SetupDiGetClassDevs: 获取设备错误。"); } else { DeviceInfoData.cbSize = Marshal.SizeOf(DeviceInfoData); DeviceInfoData.devInst = 0; DeviceInfoData.classGuid = System.Guid.Empty; DeviceInfoData.reserved = 0; var devs = SetupDiGetClassDevsExW(GUID_DEVCLASS_PORTS, null, IntPtr.Zero, DIGCF_NOSET, null, null, 0); var devsNow = SetupDiGetClassDevsExW(GUID_DEVCLASS_PORTS, null, IntPtr.Zero, DIGCF_PRESENT, null, null, 0); if (devsNow.ToInt64() != -1) { for (uint i = 0; SetupDiEnumDeviceInfo(devsNow, i, DeviceInfoData); i++) { DeviceEntity entity = new DeviceEntity(); StringBuilder DeviceName = new StringBuilder("", 254); bool resName = SetupDiGetDeviceRegistryPropertyA(devs, DeviceInfoData, SPDRP_FRIENDLYNAME, 0, DeviceName, MAX_DEV_LEN, IntPtr.Zero); if (!resName) { } else { entity.DeviceName = DeviceName.ToString(); } StringBuilder DeviceID = new StringBuilder("", 254); if (CM_Get_Device_IDA(DeviceInfoData.devInst, DeviceID, 254, 0) == CR_SUCCESS) { entity.DeviceID = DeviceID.ToString(); } listCurrent.Add(entity); } } for (uint i = 0; SetupDiEnumDeviceInfo(devs, i, DeviceInfoData); i++) { DeviceEntity entity = new DeviceEntity(); StringBuilder DeviceID = new StringBuilder("", 254); if (CM_Get_Device_IDA(DeviceInfoData.devInst, DeviceID, 254, 0) == CR_SUCCESS) { entity.DeviceID = DeviceID.ToString(); } if (listCurrent.Exists(p => p.DeviceID == DeviceID.ToString())) { continue; } else { StringBuilder DeviceName = new StringBuilder("", 254); bool resName = SetupDiGetDeviceRegistryPropertyA(devs, DeviceInfoData, SPDRP_FRIENDLYNAME, 0, DeviceName, MAX_DEV_LEN, IntPtr.Zero); if (!resName) { } else { entity.DeviceName = DeviceName.ToString(); } StringBuilder Mfg = new StringBuilder("", 254); bool resHardwareMfg = SetupDiGetDeviceRegistryPropertyA(devs, DeviceInfoData, SPDRP_MFG, 0, Mfg, MAX_DEV_LEN, IntPtr.Zero); if (!resHardwareMfg) { } else { entity.DevicePIDVID = Mfg.ToString(); } StringBuilder realPath = new StringBuilder("", 2047); bool resHardwareRealPath = SetupDiGetDeviceRegistryPropertyA(devs, DeviceInfoData, SPDRP_LOCATION_PATHS, 0, realPath, MAX_DEV_LEN, IntPtr.Zero); if (!resHardwareRealPath) { } else { entity.RealTimePath = realPath.ToString(); } StringBuilder installState = new StringBuilder("", 64); bool resHardwareInstallState = SetupDiGetDeviceRegistryPropertyA(devs, DeviceInfoData, SPDRP_INSTALL_STATE, 0, installState, MAX_DEV_LEN, IntPtr.Zero); if (!resHardwareInstallState) { } else { entity.InstallState = installState.ToString(); } //bool isHidde = !SetupDiSelectDevice(devsNow, DeviceInfoData); entity.IsHiddenDevice = true; list.Add(entity); } } SetupDiDestroyDeviceInfoList(devs); SetupDiDestroyDeviceInfoList(devsNow); ScanForHardwareChange(); } WSDLogger.WriterDebugger("End GetHiddenDevice"); return(list); }