capture to db: use powershell command instead of deprecated wmic.exe

This commit is contained in:
Kang Yu 2024-01-31 12:57:55 -08:00
parent 472b12ba3a
commit bfb2a57881
2 changed files with 32 additions and 9 deletions

View file

@ -1237,7 +1237,8 @@ namespace PersistentWindows.Common
try try
{ {
processName = process.ProcessName; processName = process.ProcessName;
windowProcessName.Add(hwnd, processName); if (!windowProcessName.ContainsKey(hwnd))
windowProcessName.Add(hwnd, processName);
} }
catch(Exception ex) catch(Exception ex)
{ {
@ -2275,6 +2276,8 @@ namespace PersistentWindows.Common
Log.Error(ex.ToString()); Log.Error(ex.ToString());
} }
} }
processCmd.Clear();
} }
} }
else if (!userMovePrev && !immediateCapture && pendingEventCnt > MinWindowOsMoveEvents) else if (!userMovePrev && !immediateCapture && pendingEventCnt > MinWindowOsMoveEvents)

View file

@ -30,6 +30,11 @@ namespace PersistentWindows.SystrayShell
static bool notification = false; //pop balloon when auto restore static bool notification = false; //pop balloon when auto restore
static int delay_manual_capture = 5000; //in millisecond static int delay_manual_capture = 5000; //in millisecond
// capture to db
static uint pid = 0;
static string commandline;
static int lineno = 0;
[STAThread] [STAThread]
static void Main(string[] args) static void Main(string[] args)
{ {
@ -587,9 +592,13 @@ namespace PersistentWindows.SystrayShell
static void GetProcessInfo() static void GetProcessInfo()
{ {
Process process = new Process(); Process process = new Process();
/*
process.StartInfo.FileName = "wmic.exe"; process.StartInfo.FileName = "wmic.exe";
//process.StartInfo.Arguments = "process get caption,commandline,processid /format:csv"; //process.StartInfo.Arguments = "process get caption,commandline,processid /format:csv";
process.StartInfo.Arguments = "process get 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.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = false; process.StartInfo.RedirectStandardError = false;
@ -602,25 +611,36 @@ namespace PersistentWindows.SystrayShell
process.BeginOutputReadLine(); process.BeginOutputReadLine();
//process.BeginErrorReadLine(); //process.BeginErrorReadLine();
process.WaitForExit(); process.WaitForExit();
pid = 0;
lineno = 0;
} }
static void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine) static void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
{ {
//* Do your stuff with the output (write to console/log/StringBuilder) //* Do your stuff with the output (write to console/log/StringBuilder)
string line = outLine.Data; string line = outLine.Data;
lineno++;
if (string.IsNullOrEmpty(line)) 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) public static void LogError(string format, params object[] args)