/// <summary> /// 将值写入指定内存地址中 /// </summary> /// <param name="baseAddress">内存地址</param> /// <param name="PId">进程Id</param> /// <param name="value">要写入的值</param> public static void WriteMemoryValue(int baseAddress, int PId, int value) { IntPtr hProcess = Kernel32Api.OpenProcess(0x1F0FFF, false, PId); //0x1F0FFF 最高权限 Kernel32Api.WriteProcessMemory(hProcess, (IntPtr)baseAddress, new int[] { value }, 4, IntPtr.Zero); Kernel32Api.CloseHandle(hProcess); }
void CloseDevice(IntPtr handle) { if (Environment.OSVersion.Version.Major > 5) { Kernel32Api.CancelIoEx(handle, IntPtr.Zero); } Kernel32Api.CloseHandle(handle); }
//读取内存中的值 /// <summary> /// 读取内存中的值 /// </summary> /// <param name="baseAddress">内存地址</param> /// <param name="processName">进程名</param> /// <returns>值</returns> public static int ReadMemoryValue(int baseAddress, string processName) { try { byte[] buffer = new byte[4]; IntPtr byteAddress = Marshal.UnsafeAddrOfPinnedArrayElement(buffer, 0); //获取缓冲区地址 IntPtr hProcess = Kernel32Api.OpenProcess(0x1F0FFF, false, GetPidByProcessName(processName)); Kernel32Api.ReadProcessMemory(hProcess, (IntPtr)baseAddress, byteAddress, 4, IntPtr.Zero); //将制定内存中的值读入缓冲区 Kernel32Api.CloseHandle(hProcess); return(Marshal.ReadInt32(byteAddress)); } catch { return(0); } }