|
| 1 | +using System.ComponentModel; |
| 2 | +using System.Runtime.InteropServices; |
| 3 | +using Microsoft.Win32.SafeHandles; |
| 4 | + |
| 5 | +namespace NosCore.PacketLogger.Remote; |
| 6 | + |
| 7 | +internal static class NativeMethods |
| 8 | +{ |
| 9 | + [Flags] |
| 10 | + public enum ProcessAccess : uint |
| 11 | + { |
| 12 | + QueryLimitedInformation = 0x1000, |
| 13 | + VirtualMemoryRead = 0x0010, |
| 14 | + VirtualMemoryWrite = 0x0020, |
| 15 | + VirtualMemoryOperation = 0x0008, |
| 16 | + CreateThread = 0x0002, |
| 17 | + |
| 18 | + ReadOnly = QueryLimitedInformation | VirtualMemoryRead, |
| 19 | + ReadWrite = ReadOnly | VirtualMemoryWrite | VirtualMemoryOperation, |
| 20 | + } |
| 21 | + |
| 22 | + [Flags] |
| 23 | + public enum SnapshotFlags : uint |
| 24 | + { |
| 25 | + Module = 0x00000008, |
| 26 | + Module32 = 0x00000010, |
| 27 | + } |
| 28 | + |
| 29 | + public const int MaxPath = 260; |
| 30 | + public const int MaxModuleName32 = 255; |
| 31 | + |
| 32 | + public static readonly IntPtr InvalidHandleValue = new(-1); |
| 33 | + |
| 34 | + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] |
| 35 | + public struct ModuleEntry32 |
| 36 | + { |
| 37 | + public uint dwSize; |
| 38 | + public uint th32ModuleID; |
| 39 | + public uint th32ProcessID; |
| 40 | + public uint GlblcntUsage; |
| 41 | + public uint ProccntUsage; |
| 42 | + public IntPtr modBaseAddr; |
| 43 | + public uint modBaseSize; |
| 44 | + public IntPtr hModule; |
| 45 | + |
| 46 | + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = MaxModuleName32 + 1)] |
| 47 | + public string szModule; |
| 48 | + |
| 49 | + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = MaxPath)] |
| 50 | + public string szExePath; |
| 51 | + } |
| 52 | + |
| 53 | + [DllImport("kernel32.dll", SetLastError = true)] |
| 54 | + public static extern SafeProcessHandle OpenProcess(ProcessAccess desiredAccess, [MarshalAs(UnmanagedType.Bool)] bool inheritHandle, int processId); |
| 55 | + |
| 56 | + [DllImport("kernel32.dll", SetLastError = true)] |
| 57 | + [return: MarshalAs(UnmanagedType.Bool)] |
| 58 | + public static extern bool ReadProcessMemory(SafeProcessHandle process, IntPtr baseAddress, byte[] buffer, UIntPtr size, out UIntPtr bytesRead); |
| 59 | + |
| 60 | + [DllImport("kernel32.dll", SetLastError = true)] |
| 61 | + [return: MarshalAs(UnmanagedType.Bool)] |
| 62 | + public static extern bool WriteProcessMemory(SafeProcessHandle process, IntPtr baseAddress, byte[] buffer, UIntPtr size, out UIntPtr bytesWritten); |
| 63 | + |
| 64 | + [DllImport("kernel32.dll", SetLastError = true)] |
| 65 | + public static extern IntPtr CreateToolhelp32Snapshot(SnapshotFlags flags, uint processId); |
| 66 | + |
| 67 | + [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)] |
| 68 | + [return: MarshalAs(UnmanagedType.Bool)] |
| 69 | + public static extern bool Module32FirstW(IntPtr snapshot, ref ModuleEntry32 entry); |
| 70 | + |
| 71 | + [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)] |
| 72 | + [return: MarshalAs(UnmanagedType.Bool)] |
| 73 | + public static extern bool Module32NextW(IntPtr snapshot, ref ModuleEntry32 entry); |
| 74 | + |
| 75 | + [DllImport("kernel32.dll", SetLastError = true)] |
| 76 | + [return: MarshalAs(UnmanagedType.Bool)] |
| 77 | + public static extern bool CloseHandle(IntPtr handle); |
| 78 | + |
| 79 | + [DllImport("kernel32.dll", SetLastError = true)] |
| 80 | + [return: MarshalAs(UnmanagedType.Bool)] |
| 81 | + public static extern bool IsWow64Process(SafeProcessHandle process, [MarshalAs(UnmanagedType.Bool)] out bool isWow64); |
| 82 | + |
| 83 | + public static Win32Exception LastWin32(string label) => |
| 84 | + new(Marshal.GetLastWin32Error(), label); |
| 85 | +} |
0 commit comments