2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Diagnostics;
7using System.Management;
8using System.Runtime.Versioning;
10using System.Threading.Tasks;
12using Microsoft.Extensions.Logging;
21 [SupportedOSPlatform(
"windows")]
27 readonly ILogger<WindowsProcessFeatures>
logger;
35 this.logger =
logger ??
throw new ArgumentNullException(nameof(
logger));
41 ArgumentNullException.ThrowIfNull(process);
44 foreach (ProcessThread thread
in process.Threads)
46 var threadId = (uint)thread.Id;
47 logger.LogTrace(
"Resuming thread {threadId}...", threadId);
49 if (pOpenThread == IntPtr.Zero)
51 logger.LogDebug(
new Win32Exception(),
"Failed to open thread {threadId}!", threadId);
58 throw new Win32Exception();
70 ArgumentNullException.ThrowIfNull(process);
72 var suspendedThreadIds =
new HashSet<uint>();
73 bool suspendedNewThreads;
76 suspendedNewThreads =
false;
78 foreach (ProcessThread thread
in process.Threads)
80 var threadId = (uint)thread.Id;
82 if (!suspendedThreadIds.Add(threadId))
85 suspendedNewThreads =
true;
86 logger.LogTrace(
"Suspending thread {threadId}...", threadId);
88 if (pOpenThread == IntPtr.Zero)
90 logger.LogDebug(
new Win32Exception(),
"Failed to open thread {threadId}!", threadId);
97 throw new Win32Exception();
105 while (suspendedNewThreads);
111 string query = $
"SELECT * FROM Win32_Process WHERE ProcessId = {process?.Id ?? throw new ArgumentNullException(nameof(process))}";
112 using var searcher =
new ManagementObjectSearcher(query);
113 foreach (var obj
in searcher.Get().Cast<ManagementObject>())
115 var argList =
new string[] { String.Empty, String.Empty };
116 var returnString = obj.InvokeMethod(
121 if (!Int32.TryParse(returnString, out var returnVal))
122 return $
"BAD RETURN PARSE: {returnString}";
127 string owner = argList.Last() +
"\\" + argList.First();
136 public async ValueTask
CreateDump(global::System.Diagnostics.Process process,
string outputFile,
bool minidump, CancellationToken cancellationToken)
140 if (process.HasExited)
143 catch (InvalidOperationException ex)
148 await
using var fileStream =
new FileStream(outputFile, FileMode.CreateNew);
150 await Task.Factory.StartNew(
153 var flags = NativeMethods.MiniDumpType.WithHandleData
154 | NativeMethods.MiniDumpType.WithThreadInfo
164 fileStream.SafeFileHandle,
169 throw new Win32Exception();
173 TaskScheduler.Current);
177 public ValueTask<int>
HandleProcessStart(global::System.Diagnostics.Process process, CancellationToken cancellationToken)
178 => ValueTask.FromResult((process ??
throw new ArgumentNullException(nameof(process))).Id);
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 global::System.Diagnostics.Process.
WindowsProcessFeatures(ILogger< WindowsProcessFeatures > logger)
Initializes a new instance of the WindowsProcessFeatures class.
async ValueTask CreateDump(global::System.Diagnostics.Process process, string outputFile, bool minidump, CancellationToken cancellationToken)
Create a dump file for a given process .A ValueTask representing the running operation.
ValueTask< int > HandleProcessStart(global::System.Diagnostics.Process process, CancellationToken cancellationToken)
Run events on starting a process.A ValueTask<TResult> resulting in the process ID.
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.