I don't even know what I'm doing

This commit is contained in:
Mino 2020-04-01 20:00:15 +09:00
parent 117d1dfbb2
commit 79fb6c5104
3 changed files with 55 additions and 35 deletions

View file

@ -10,9 +10,9 @@ namespace Dalamud.Bootstrap
/// <summary>
/// A class that provides a wrapper over operations on Win32 process.
/// </summary>
internal sealed partial class Process : IDisposable
internal class Process : IDisposable
{
private SafeProcessHandle m_handle;
protected SafeProcessHandle Handle { get; set; }
/// <summary>
/// Creates a process object that can be used to manipulate process's internal state.
@ -20,32 +20,38 @@ namespace Dalamud.Bootstrap
/// <param name="handle">A process handle. Note that this functinon will take the ownership of the handle.</param>
public Process(SafeProcessHandle handle)
{
m_handle = handle;
Handle = handle;
}
public void Dispose()
{
m_handle?.Dispose();
m_handle = null!;
DisposeImpl();
}
public static Process Open(uint pid, PROCESS_ACCESS_RIGHT access)
protected virtual void DisposeImpl()
{
var handle = Win32.OpenProcess((uint) access, false, pid);
Handle?.Dispose();
Handle = null!;
}
protected static SafeProcessHandle OpenHandle(uint pid, PROCESS_ACCESS_RIGHT access)
{
var handle = Win32.OpenProcess((uint)access, false, pid);
if (handle.IsInvalid)
{
ProcessException.ThrowLastOsError(pid);
}
return new Process(handle);
return handle;
}
public uint GetPid() => Win32.GetProcessId(m_handle);
public uint GetPid() => Win32.GetProcessId(Handle);
public void Terminate(uint exitCode = 0)
{
if (!Win32.TerminateProcess(m_handle, exitCode))
if (!Win32.TerminateProcess(Handle, exitCode))
{
ProcessException.ThrowLastOsError(GetPid());
}
@ -65,7 +71,7 @@ namespace Dalamud.Bootstrap
{
IntPtr bytesRead;
if (!Win32.ReadProcessMemory(m_handle, (void*)address, pDest, (IntPtr)destination.Length, &bytesRead))
if (!Win32.ReadProcessMemory(Handle, (void*)address, pDest, (IntPtr)destination.Length, &bytesRead))
{
ProcessException.ThrowLastOsError(GetPid());
}
@ -119,7 +125,7 @@ namespace Dalamud.Bootstrap
unsafe
{
var info = new PROCESS_BASIC_INFORMATION();
var status = Win32.NtQueryInformationProcess(m_handle, PROCESSINFOCLASS.ProcessBasicInformation, &info, sizeof(PROCESS_BASIC_INFORMATION), (IntPtr*)IntPtr.Zero);
var status = Win32.NtQueryInformationProcess(Handle, PROCESSINFOCLASS.ProcessBasicInformation, &info, sizeof(PROCESS_BASIC_INFORMATION), (IntPtr*)IntPtr.Zero);
if (!status.Success)
{
@ -133,7 +139,10 @@ namespace Dalamud.Bootstrap
}
}
public string[] ReadArguments()
/// <summary>
/// Reads command-line arguments from the process.
/// </summary>
public string[] GetArguments()
{
unsafe
{
@ -158,7 +167,7 @@ namespace Dalamud.Bootstrap
{
FileTime creationTime, exitTime, kernelTime, userTime;
if (!Win32.GetProcessTimes(m_handle, &creationTime, &exitTime, &kernelTime, &userTime))
if (!Win32.GetProcessTimes(Handle, &creationTime, &exitTime, &kernelTime, &userTime))
{
ProcessException.ThrowLastOsError(GetPid());
}
@ -213,7 +222,7 @@ namespace Dalamud.Bootstrap
// On success, receives the number of characters written to the buffer, not including the null-terminating character.
var size = buffer.Capacity;
if (!Win32.QueryFullProcessImageNameW(m_handle, 0, buffer, ref size))
if (!Win32.QueryFullProcessImageNameW(Handle, 0, buffer, ref size))
{
ProcessException.ThrowLastOsError(GetPid());
}