From d663252ed10455465ed6aea6ddf9cb5665730fcf Mon Sep 17 00:00:00 2001 From: Mino <1381835+Minoost@users.noreply.github.com> Date: Sat, 29 Feb 2020 18:40:53 +0900 Subject: [PATCH] ntdll --- Dalamud.Injector/FFI/Win32.cs | 54 ++++++++++++++++++++++++++++++++++ Dalamud.Injector/OS/Process.cs | 27 +++++++++++++++-- 2 files changed, 79 insertions(+), 2 deletions(-) diff --git a/Dalamud.Injector/FFI/Win32.cs b/Dalamud.Injector/FFI/Win32.cs index 64f2f6658..394065fbc 100644 --- a/Dalamud.Injector/FFI/Win32.cs +++ b/Dalamud.Injector/FFI/Win32.cs @@ -1,11 +1,65 @@ +using Microsoft.Win32.SafeHandles; using System; using System.Collections.Generic; +using System.Runtime.InteropServices; using System.Text; namespace Dalamud.Injector.FFI { internal static class Win32 { + [DllImport("kernel32", CallingConvention = CallingConvention.Winapi, SetLastError = true)] + public static extern SafeProcessHandle OpenProcess(uint dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, uint dwProcessId); + + [DllImport("kernel32", CallingConvention = CallingConvention.Winapi, SetLastError = true)] + public static extern NtStatus NtQueryInformationProcess(/* TODO */); + } + + [StructLayout(LayoutKind.Sequential)] + internal partial struct NtStatus + { + public uint Value { get; } + } + + internal partial struct NtStatus + { + public NtStatus(uint value) + { + Value = value; + } + + /// + /// Equivalent to NT_SUCCESS + /// + public bool Success => Value <= 0x7FFFFFFF; + + /// + /// Equivalent to NT_INFORMATION + /// + public bool Information => 0x40000000 <= Value && Value <= 0x7FFFFFFF; + + /// + /// Equivalent to NT_WARNING + /// + public bool Warning => 0x80000000 <= Value && Value <= 0xBFFFFFFF; + + /// + /// Equivalent to NT_ERROR + /// + public bool Error => 0xC0000000 <= Value; + } + + [StructLayout(LayoutKind.Sequential)] + internal struct PROCESS_BASIC_INFORMATION + { + // https://github.com/processhacker/processhacker/blob/e43d7c0513ec5368c3309a58c9f2c2a3ca5de367/phnt/include/ntpsapi.h#L272 + public NtStatus ExitStatus; + public IntPtr PebBaseAddress; + public IntPtr AffinityMask; + public IntPtr BasePriority; + public IntPtr UniqueProcessId; + public IntPtr InheritedFromUniqueProcessId; + } } diff --git a/Dalamud.Injector/OS/Process.cs b/Dalamud.Injector/OS/Process.cs index ded8833a7..fd316bf91 100644 --- a/Dalamud.Injector/OS/Process.cs +++ b/Dalamud.Injector/OS/Process.cs @@ -1,10 +1,33 @@ -using System; +using Microsoft.Win32.SafeHandles; +using System; using System.Collections.Generic; using System.Text; namespace Dalamud.Injector.OS { - internal sealed class Process + internal sealed partial class Process : IDisposable { + private SafeProcessHandle m_handle; + } + + internal sealed partial class Process { + internal Process(SafeProcessHandle handle) + { + m_handle = handle; + } + + public static Process Open(uint pid/* and perms? maybe? */) + { + // + throw new NotImplementedException("TODO"); + } + + + + public void Dispose() + { + m_handle?.Dispose(); + m_handle = null!; + } } }