记录一次读取程序基址加偏移地址的变量

事情是这样的:使用C#写一个程序,需要检测一个智能笔的是否插入,电量 多少。

 第一想法:SDK呀,不好意思没有条件。

那么如何把这变量数值传到我的程序中呢, 想了许多办法和查看这程序(C++写的)。

最终决定还是使用CE查查询内存吧(毕竟这不是游戏 故意防此hook以及内存故意混淆的可能性极低)

使用CE简单过虑  查到为基址 + 0x0B4060

书写简单代码如下

        [DllImport("kernel32.dll")]
        public static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);

        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, int dwSize, out int lpNumberOfBytesRead);

[DllImport("kernel32.dll")]
public static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, uint nSize, out int lpNumberOfBytesWritten);
[DllImport(
"kernel32.dll", SetLastError = true)] public static extern bool CloseHandle(IntPtr hObject); public static void HookCheck() { Task.Run(() => { while (true) { try { var preocess = Process.GetProcessesByName("PresenterControl"); IntPtr processHandle = OpenProcess(0x0010, false, preocess.First().Id); // 0x0010 是 PROCESS_VM_READ 权限 Process targetProcess = Process.GetProcessById(preocess.First().Id); IntPtr baseAddress = targetProcess.MainModule.BaseAddress; IntPtr address = IntPtr.Add(baseAddress, 0x0B4060);// 替换为要读取的地址 byte[] buffer = new byte[4]; // 假设要读取4个字节的值 int bytesRead; bool success = ReadProcessMemory(processHandle, address, buffer, buffer.Length, out bytesRead); if (success) { int value = BitConverter.ToInt32(buffer, 0); EventAggregatorManager.Instance.GetEvent<SmartPenPowerEvent>().Publish(value); } else { } } catch (Exception) { } Thread.Sleep(1000); } }); }

感觉非常有代表情,故记录。

posted @ 2023-07-18 09:17  stweily  阅读(137)  评论(0编辑  收藏  举报