static void Main(string[] args) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); //build "Device Map" from registry try { ComPortControlTable.InitTable(); } catch (Exception err) { yMessageBox.ShowError(null, err.Message); return; } //load config file try { OwnerTranslate.LoadFromFile(); AppConfig.LoadGlobalConfig(); ThemeManager.Init(); } catch //if config file has error, just ignore it, we will use the default setting { } Application.Run(new FormMain()); }
private void ctxMenuPortsTable_Opening(object sender, CancelEventArgs e) { //Don't show the "KILL" menu if the serial port is not opened. if (listPortTable.SelectedIndices.Count < 0) { menuKill.Visible = false; return; } string strPort = listPortTable.SelectedItems[0].SubItems[0].Text; ComPortItem item = ComPortControlTable.GetItemByPortName(strPort); if (item == null || item.OwnProcessId <= 0) { menuKill.Visible = false; return; } menuKill.Visible = true; menuEnableAutoRefresh.Checked = AppConfig.EnableAutoRefresh; menuOwnerFormatDomainUser.Checked = (AppConfig.OwnerFormat == OwnerShowFormat.DomainUser); menuOwnerFormatFullName.Checked = (AppConfig.OwnerFormat == OwnerShowFormat.FullName); menuOwnerFormatShortName.Checked = (AppConfig.OwnerFormat == OwnerShowFormat.ShortName || AppConfig.OwnerFormat == OwnerShowFormat.Default); menuOwnerFormatPhone.Checked = (AppConfig.OwnerFormat == OwnerShowFormat.Phone); }
/// <summary> /// Refresh all serial port information and update it to GUI. /// </summary> private void RefreshAllComPorts() { DateTime timeBegin = DateTime.Now; ComPortControlTable.RefreshAll(); DateTime timeEnd = DateTime.Now; TimeSpan timeConsume = DateTime.Now - timeBegin; statusLabelRefreshTime.Text = string.Format("Last Refresh @{0:D2}:{1:D2}:{2:D2},", timeEnd.Hour, timeEnd.Minute, timeEnd.Second); statusLabelRefreshConsumeTime.Text = string.Format(" Consumes {0:F1}", timeConsume.TotalSeconds); }
public static string GetPortOwner(string targetPort, ref string appName, ref Process ownProcess) { List <Process> listProcs = new List <Process>(); Process[] allSecureCrtProcess = Process.GetProcessesByName("SecureCRT"); Process[] allTeraTermProcess = Process.GetProcessesByName("ttermpro"); Process[] allMultyTermProcess = Process.GetProcessesByName("Multy-Term"); listProcs.AddRange(allSecureCrtProcess); listProcs.AddRange(allTeraTermProcess); listProcs.AddRange(allMultyTermProcess); foreach (Process proc in listProcs) { ArrayList handles = new HandleWrapper(proc.Id).GetComFileHandle(); ArrayList ports = ComPortControlTable.GetPorts(handles); foreach (string port in ports) { if (port == targetPort) { try { string owner = Utility.GetProcessOwner(proc.Id); //string sid = null; //string owner = ProcessOwnerFinderWmi.GetProcessOwnerByPID(proc.Id, out sid); if (allSecureCrtProcess.Contains(proc)) { appName = "SecureCRT"; } else if (allTeraTermProcess.Contains(proc)) { appName = "TeraTerm"; } else if (allMultyTermProcess.Contains(proc)) { appName = "Multy-Term"; } else { appName = "???"; } ownProcess = proc; return(owner); } catch { continue; } } } } return(null); }
private void listPortTable_DoubleClick(object sender, EventArgs e) { if (listPortTable.SelectedIndices.Count <= 0) { return; } string portName = listPortTable.SelectedItems[0].Text; ComPortItem item = ComPortControlTable.GetItemByPortName(portName); if (item == null || item.OwnProcessId < 0 || item.OwnUser == null) { return; } new ComPortShowDialog(item).ShowDialog(); }
/// <summary> /// Create row for each COM port and initialize it to empty except port name. /// </summary> private void CreateRows() { listPortTable.BeginUpdate(); listPortTable.Items.Clear(); string[] ports = ComPortControlTable.AllPortNames; comboBoxGotoPort.Items.Clear(); comboBoxGotoPort.Items.AddRange(ports); foreach (string port in ports) { ListViewItem lvi = new ListViewItem(new string[] { port, string.Empty, string.Empty, string.Empty }); listPortTable.Items.Add(lvi); ComPortItem item = ComPortControlTable.GetItemByPortName(port); if (item != null) { item.GuiItem = lvi; } } listPortTable.EndUpdate(); }
static public void RefreshAll() { ResetDetectedFlag(); //Get all processes that are opening serial port by utility "handle" List <ProcessOwnInfo> listOwnProcesses = HandleWrapper.GetAllOwnProcesses(ComPortControlTable.DeviceNamePatterns); if (listOwnProcesses == null) { return; } //Usually, one process often opens more than one serial port, so here creats the dictionary //to store the process that have known its owner, so as not to search process owner twice. Dictionary <int, string> procOwnerTable = new Dictionary <int, string>(); //key: process ID; value: owner foreach (ProcessOwnInfo pinfo in listOwnProcesses) { string owner = null; string procName = null; Application.DoEvents(); //here map the device name to serial port name ComPortItem citem = ComPortControlTable.GetItemByDevieName(pinfo.DeviceName); if (citem == null) { continue; } //We have searched the process owner before, we just copy its result, //so as not to search twice. if (procOwnerTable.ContainsKey(pinfo.ProcessId)) { owner = procOwnerTable[pinfo.ProcessId]; } else { //We havn't searched the process owner before string sid = null; owner = Utility.GetProcessOwner(pinfo.ProcessId, true); //owner = ProcessOwnerFinderWmi.GetProcessOwnerByPID(pinfo.ProcessId, out sid); if (owner == null) //search owner failed { continue; } procOwnerTable.Add(pinfo.ProcessId, owner); } procName = pinfo.ProcessName; //if the process name have suffix ".exe", we just remove it to have nice format data show to user. int exeindex = pinfo.ProcessName.LastIndexOf(".exe"); if (exeindex > 0) { procName = pinfo.ProcessName.Substring(0, exeindex); //remove the suffix ".exe" } //update the serial port info as we get all result. if (citem.Update(owner, procName, pinfo.ProcessName + " (" + pinfo.ProcessId + ")", pinfo.ProcessId)) { citem.GuiItem.SubItems[1].Text = citem.FormatedOwner; citem.GuiItem.SubItems[2].Text = citem.OwnAppName; citem.GuiItem.SubItems[3].Text = citem.OwnDetailProcessInfo; } citem.DetectedFlag = true; //mark the serial port is detected be opened. } //handle other serial ports that is not opening, so reset the info to empty. ComPortItem[] allItems = ComPortControlTable.AllItems; foreach (ComPortItem item in allItems) { if (item.GuiItem != null && !item.DetectedFlag) { item.ClearContent(); } } }