gameProcess stuff

This commit is contained in:
Mino 2020-04-03 10:46:04 +09:00
parent 79fb6c5104
commit ac80895f43
2 changed files with 33 additions and 22 deletions

View file

@ -5,9 +5,19 @@ using Microsoft.Win32.SafeHandles;
namespace Dalamud.Bootstrap namespace Dalamud.Bootstrap
{ {
internal sealed class GameProcess : Process internal sealed class GameProcess : IDisposable
{ {
public GameProcess(SafeProcessHandle handle) : base(handle) { } private Process m_process;
public GameProcess(Process process)
{
m_process = process;
}
public void Dispose()
{
m_process?.Dispose();
m_process = null!;
}
public static GameProcess Open(uint pid) public static GameProcess Open(uint pid)
{ {
@ -21,9 +31,9 @@ namespace Dalamud.Bootstrap
// TODO: unfuck VM_WRITE // TODO: unfuck VM_WRITE
var handle = Process.OpenHandle(pid, access); var process = Process.Open(pid, access);
return new GameProcess(handle); return new GameProcess(process);
} }
/// <summary> /// <summary>

View file

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