2 using Microsoft.Extensions.Logging;
8 using System.Threading.Tasks;
18 readonly ILogger<WindowsProcessFeatures>
logger;
26 this.logger = logger ??
throw new ArgumentNullException(nameof(logger));
33 throw new ArgumentNullException(nameof(process));
37 foreach (ProcessThread thread
in process.Threads)
40 if (pOpenThread == IntPtr.Zero)
44 throw new Win32Exception();
49 logger.LogTrace(
"Resumed PID {0}", process.Id);
53 logger.LogError(e,
"Failed to resume PID {0}!", process.Id);
62 throw new ArgumentNullException(nameof(process));
66 foreach (ProcessThread thread
in process.Threads)
69 if (pOpenThread == IntPtr.Zero)
73 throw new Win32Exception();
78 logger.LogTrace(
"Suspended PID {0}", process.Id);
82 logger.LogError(e,
"Failed to suspend PID {0}!", process.Id);
88 public Task<string>
GetExecutingUsername(global::System.Diagnostics.Process process, CancellationToken cancellationToken)
90 string query = $
"SELECT * FROM Win32_Process WHERE ProcessId = {process?.Id ?? throw new ArgumentNullException(nameof(process))}";
91 using var searcher =
new ManagementObjectSearcher(query);
92 foreach (ManagementObject obj
in searcher.Get())
94 var argList =
new string[] { String.Empty, String.Empty };
95 var returnString = obj.InvokeMethod(
100 if (!Int32.TryParse(returnString, out var returnVal))
101 return Task.FromResult($
"BAD RETURN PARSE: {returnString}");
106 string owner = argList.Last() +
"\\" + argList.First();
107 return Task.FromResult(owner);
111 return Task.FromResult(
"NO OWNER");
static uint ResumeThread(IntPtr hThread)
See https://msdn.microsoft.com/en-us/library/windows/desktop/ms685086(v=vs.85).aspx ...
readonly ILogger< WindowsProcessFeatures > logger
The ILogger<TCategoryName> for the WindowsProcessFeatures.
Use server authentication
void SuspendProcess(global::System.Diagnostics.Process process)
Suspend a given Process.
WindowsProcessFeatures(ILogger< WindowsProcessFeatures > logger)
Initializes a new instance of the WindowsProcessFeatures .
void ResumeProcess(global::System.Diagnostics.Process process)
Resume a given suspended Process.
Task< string > GetExecutingUsername(global::System.Diagnostics.Process process, CancellationToken cancellationToken)
Get the name of the user executing a given process .
static bool CloseHandle(IntPtr hObject)
See https://msdn.microsoft.com/en-us/library/windows/desktop/ms724211(v=vs.85).aspx ...
Native Windows methods used by the code
ThreadAccess
See https://msdn.microsoft.com/en-us/library/windows/desktop/ms686769(v=vs.85).aspx ...
static uint SuspendThread(IntPtr hThread)
See https://msdn.microsoft.com/en-us/library/windows/desktop/ms686345(v=vs.85).aspx ...
Abstraction for suspending and resuming processes.
static IntPtr OpenThread(ThreadAccess dwDesiredAccess, bool bInheritHandle, uint dwThreadId)
See https://msdn.microsoft.com/en-us/library/windows/desktop/ms684335(v=vs.85).aspx ...