2using System.Diagnostics;
5using System.Management;
6using System.Runtime.Versioning;
8using System.Threading.Tasks;
10using BetterWin32Errors;
11using Microsoft.Extensions.Logging;
20 [SupportedOSPlatform(
"windows")]
26 readonly ILogger<WindowsProcessFeatures>
logger;
34 this.logger =
logger ??
throw new ArgumentNullException(nameof(
logger));
40 ArgumentNullException.ThrowIfNull(process);
43 foreach (ProcessThread thread
in process.Threads)
45 var threadId = (uint)thread.Id;
46 logger.LogTrace(
"Resuming thread {threadId}...", threadId);
48 if (pOpenThread == IntPtr.Zero)
50 logger.LogDebug(
new Win32Exception(),
"Failed to open thread {threadId}!", threadId);
57 throw new Win32Exception();
69 ArgumentNullException.ThrowIfNull(process);
72 foreach (ProcessThread thread
in process.Threads)
74 var threadId = (uint)thread.Id;
75 logger.LogTrace(
"Suspending thread {threadId}...", threadId);
77 if (pOpenThread == IntPtr.Zero)
79 logger.LogDebug(
new Win32Exception(),
"Failed to open thread {threadId}!", threadId);
86 throw new Win32Exception();
98 string query = $
"SELECT * FROM Win32_Process WHERE ProcessId = {process?.Id ?? throw new ArgumentNullException(nameof(process))}";
99 using var searcher =
new ManagementObjectSearcher(query);
100 foreach (var obj
in searcher.Get().Cast<ManagementObject>())
102 var argList =
new string[] { String.Empty, String.Empty };
103 var returnString = obj.InvokeMethod(
108 if (!Int32.TryParse(returnString, out var returnVal))
109 return $
"BAD RETURN PARSE: {returnString}";
114 string owner = argList.Last() +
"\\" + argList.First();
123 public async ValueTask
CreateDump(global::System.Diagnostics.Process process,
string outputFile, CancellationToken cancellationToken)
127 if (process.HasExited)
130 catch (InvalidOperationException ex)
135 await
using var fileStream =
new FileStream(outputFile, FileMode.CreateNew);
137 await Task.Factory.StartNew(
143 fileStream.SafeFileHandle,
152 throw new Win32Exception();
156 TaskScheduler.Current);
IIOManager that resolves paths to Environment.CurrentDirectory.
const TaskCreationOptions BlockingTaskCreationOptions
The TaskCreationOptions used to spawn Tasks for potentially long running, blocking operations.
Operation exceptions thrown from the context of a Models.Job.
Native methods used by the code.
MiniDumpType
See https://docs.microsoft.com/en-us/windows/win32/api/minidumpapiset/ne-minidumpapiset-minidump_type...
static IntPtr OpenThread(ThreadAccess dwDesiredAccess, bool bInheritHandle, uint dwThreadId)
See https://msdn.microsoft.com/en-us/library/windows/desktop/ms684335(v=vs.85).aspx.
static uint ResumeThread(IntPtr hThread)
See https://msdn.microsoft.com/en-us/library/windows/desktop/ms685086(v=vs.85).aspx.
static uint SuspendThread(IntPtr hThread)
See https://msdn.microsoft.com/en-us/library/windows/desktop/ms686345(v=vs.85).aspx.
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 bool CloseHandle(IntPtr hObject)
See https://msdn.microsoft.com/en-us/library/windows/desktop/ms724211(v=vs.85).aspx.
ThreadAccess
See https://msdn.microsoft.com/en-us/library/windows/desktop/ms686769(v=vs.85).aspx.
void SuspendProcess(global::System.Diagnostics.Process process)
Suspend a given process .
void ResumeProcess(global::System.Diagnostics.Process process)
Resume a given suspended Process.
WindowsProcessFeatures(ILogger< WindowsProcessFeatures > logger)
Initializes a new instance of the WindowsProcessFeatures class.
async ValueTask CreateDump(global::System.Diagnostics.Process process, string outputFile, CancellationToken cancellationToken)
Create a dump file for a given process . A ValueTask representing the running operation.
readonly ILogger< WindowsProcessFeatures > logger
The ILogger for the WindowsProcessFeatures.
string GetExecutingUsername(global::System.Diagnostics.Process process)
Get the name of the user executing a given process . The name of the user executing process .
Abstraction for suspending and resuming processes.
ErrorCode
Types of Response.ErrorMessageResponses that the API may return.