2 using Microsoft.Extensions.Logging;
9 using System.Threading.Tasks;
19 readonly ILogger<WindowsProcessFeatures>
logger;
27 this.logger = logger ??
throw new ArgumentNullException(nameof(logger));
34 throw new ArgumentNullException(nameof(process));
38 foreach (ProcessThread thread
in process.Threads)
41 if (pOpenThread == IntPtr.Zero)
45 throw new Win32Exception();
50 logger.LogTrace(
"Resumed PID {0}", process.Id);
54 logger.LogError(e,
"Failed to resume PID {0}!", process.Id);
63 throw new ArgumentNullException(nameof(process));
67 foreach (ProcessThread thread
in process.Threads)
70 if (pOpenThread == IntPtr.Zero)
74 throw new Win32Exception();
79 logger.LogTrace(
"Suspended PID {0}", process.Id);
83 logger.LogError(e,
"Failed to suspend PID {0}!", process.Id);
89 public Task<string>
GetExecutingUsername(global::System.Diagnostics.Process process, CancellationToken cancellationToken)
91 string query = $
"SELECT * FROM Win32_Process WHERE ProcessId = {process?.Id ?? throw new ArgumentNullException(nameof(process))}";
92 using var searcher =
new ManagementObjectSearcher(query);
93 foreach (ManagementObject obj
in searcher.Get())
95 var argList =
new string[] { String.Empty, String.Empty };
96 var returnString = obj.InvokeMethod(
101 if (!Int32.TryParse(returnString, out var returnVal))
102 return Task.FromResult($
"BAD RETURN PARSE: {returnString}");
107 string owner = argList.Last() +
"\\" + argList.First();
108 return Task.FromResult(owner);
112 return Task.FromResult(
"NO OWNER");
116 public Task CreateDump(global::System.Diagnostics.Process process,
string outputFile, CancellationToken cancellationToken)
117 => Task.Factory.StartNew(
120 using var fileStream =
new FileStream(outputFile, FileMode.CreateNew);
124 fileStream.SafeFileHandle,
133 throw new Win32Exception();
136 TaskCreationOptions.LongRunning,
137 TaskScheduler.Current);
static bool MiniDumpWriteDump(IntPtr hProcess, uint processId, SafeHandle hFile, MiniDumpType dumpType, IntPtr expParam, IntPtr userStreamParam, IntPtr callbackParam)
See https://docs.microsoft.com/en-us/windows/win32/api/minidumpapiset/nf-minidumpapiset-minidumpwrite...
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
MiniDumpType
See https://docs.microsoft.com/en-us/windows/win32/api/minidumpapiset/ne-minidumpapiset-minidump_type...
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 ...