From bfb2a5788178e941fe2d7519f2e733c15cf752a0 Mon Sep 17 00:00:00 2001 From: Kang Yu Date: Wed, 31 Jan 2024 12:57:55 -0800 Subject: [PATCH] capture to db: use powershell command instead of deprecated wmic.exe --- .../Common/PersistentWindowProcessor.cs | 5 ++- .../SystrayShell/Program.cs | 36 ++++++++++++++----- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/Ninjacrab.PersistentWindows.Solution/Common/PersistentWindowProcessor.cs b/Ninjacrab.PersistentWindows.Solution/Common/PersistentWindowProcessor.cs index ced15a4..196f232 100644 --- a/Ninjacrab.PersistentWindows.Solution/Common/PersistentWindowProcessor.cs +++ b/Ninjacrab.PersistentWindows.Solution/Common/PersistentWindowProcessor.cs @@ -1237,7 +1237,8 @@ namespace PersistentWindows.Common try { processName = process.ProcessName; - windowProcessName.Add(hwnd, processName); + if (!windowProcessName.ContainsKey(hwnd)) + windowProcessName.Add(hwnd, processName); } catch(Exception ex) { @@ -2275,6 +2276,8 @@ namespace PersistentWindows.Common Log.Error(ex.ToString()); } } + + processCmd.Clear(); } } else if (!userMovePrev && !immediateCapture && pendingEventCnt > MinWindowOsMoveEvents) diff --git a/Ninjacrab.PersistentWindows.Solution/SystrayShell/Program.cs b/Ninjacrab.PersistentWindows.Solution/SystrayShell/Program.cs index a272851..bf63751 100644 --- a/Ninjacrab.PersistentWindows.Solution/SystrayShell/Program.cs +++ b/Ninjacrab.PersistentWindows.Solution/SystrayShell/Program.cs @@ -30,6 +30,11 @@ namespace PersistentWindows.SystrayShell static bool notification = false; //pop balloon when auto restore static int delay_manual_capture = 5000; //in millisecond + // capture to db + static uint pid = 0; + static string commandline; + static int lineno = 0; + [STAThread] static void Main(string[] args) { @@ -587,9 +592,13 @@ namespace PersistentWindows.SystrayShell static void GetProcessInfo() { Process process = new Process(); + /* process.StartInfo.FileName = "wmic.exe"; //process.StartInfo.Arguments = "process get caption,commandline,processid /format:csv"; process.StartInfo.Arguments = "process get commandline,processid /format:csv"; + */ + process.StartInfo.FileName = "powershell.exe"; + process.StartInfo.Arguments = "get-ciminstance win32_process | select processid,commandline | format-list"; process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = false; @@ -602,25 +611,36 @@ namespace PersistentWindows.SystrayShell process.BeginOutputReadLine(); //process.BeginErrorReadLine(); process.WaitForExit(); + + pid = 0; + lineno = 0; } static void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine) { //* Do your stuff with the output (write to console/log/StringBuilder) string line = outLine.Data; + lineno++; + if (string.IsNullOrEmpty(line)) - return; - string[] fields = line.Split(','); - if (fields.Length < 3) - return; - uint processId; - if (uint.TryParse(fields[2], out processId)) { - if (!string.IsNullOrEmpty(fields[1])) + if (pid != 0) { - pwp.processCmd[processId] = fields[1]; + pwp.processCmd[pid] = commandline; } } + else if (line.StartsWith("processid")) + { + uint.TryParse(line.Split(':')[1], out pid); + } + else if (line.StartsWith("commandline")) + { + commandline = line.Substring(14); + } + else + { + commandline += line.Substring(14); + } } public static void LogError(string format, params object[] args)