private void Form1_FormClosed(object sender, FormClosedEventArgs e) { if ((int)process_handle != 0) { W_API.CloseHandle(process_handle); } }
/// <summary> /// 读4字节内存 /// </summary> /// <param name="hwnd"></param> /// <param name="hAddr"></param> /// <param name="outData"></param> /// <returns></returns> unsafe public bool Read4Byte(IntPtr hwnd, int hAddr, out Int32 outData) { byte[] out_byte = new byte[4]; IntPtr dataPrt = Marshal.AllocHGlobal(out_byte.Length); int ip = new int(); int o_sta = W_API.ReadProcessMemory(hwnd, hAddr, (byte *)dataPrt, 4, out ip); Marshal.Copy((IntPtr)dataPrt, out_byte, 0, out_byte.Length); Marshal.FreeHGlobal(dataPrt); if (o_sta == 0) { outData = 0; return(false); } UInt32 temp = 0; temp |= (UInt32)out_byte[0]; temp |= ((UInt32)out_byte[1]) << 8; temp |= ((UInt32)out_byte[2]) << 16; temp |= ((UInt32)out_byte[3]) << 24; outData = (Int32)temp; return(true); }
private void button1_Click(object sender, EventArgs e) { run_fast.Checked = false; if ((int)process_handle != 0) { W_API.CloseHandle(process_handle); } //查找窗口 window_hwnd = W_API.FindWindow(null, "PAL4-Application"); if ((int)window_hwnd == 0) { MessageBox.Show("找不到窗口"); return; } //获取PID W_API.GetWindowThreadProcessId(window_hwnd, out process_id); if (process_id == 0) { MessageBox.Show("查找进程ID失败"); return; } //打开进程 process_handle = W_API.OpenProcess(0x1F0FFF, 0, (uint)process_id); if ((int)process_handle == 0) { MessageBox.Show("打开进程失败"); return; } //Get_HP(process_handle); //MessageBox.Show("载入完成"); Text = "仙剑4内存修改器 - PAL4.exe - " + process_id.ToString(); timer1.Enabled = true; button1.Enabled = false; button1.Text = "加载完成"; }
/// <summary> /// 读2字节内存 /// </summary> /// <param name="hwnd"></param> /// <param name="hAddr"></param> /// <param name="outData"></param> /// <returns></returns> unsafe public bool Read2Byte(IntPtr hwnd, int hAddr, out UInt16 outData) { byte[] out_byte = new byte[2]; IntPtr dataPrt = Marshal.AllocHGlobal(out_byte.Length); int ip = new int(); int o_sta = W_API.ReadProcessMemory(hwnd, hAddr, (byte *)dataPrt, 2, out ip); Marshal.Copy((IntPtr)dataPrt, out_byte, 0, out_byte.Length); Marshal.FreeHGlobal(dataPrt); if (o_sta == 0) { outData = 0; return(false); } UInt16 temp = 0; temp |= out_byte[0]; temp |= (UInt16)(out_byte[1] << 8); outData = (UInt16)temp; return(true); }
/// <summary> /// 读多个字节内存 /// </summary> /// <param name="hwnd"></param> /// <param name="hAddr"></param> /// <param name="readLen"></param> /// <param name="outData"></param> /// <returns></returns> unsafe public bool ReadBytes(IntPtr hwnd, int hAddr, uint readLen, byte *outData) { int ip = new int(); int o_sta = W_API.ReadProcessMemory(hwnd, hAddr, outData, readLen, out ip); if (o_sta == 0) { outData = null; return(false); } return(true); }
/// <summary> /// 写2字节内存 /// </summary> /// <param name="hwnd"></param> /// <param name="hAddr"></param> /// <param name="inData"></param> /// <returns></returns> public bool Write2Byte(IntPtr hwnd, int hAddr, UInt16 inData) { byte[] temp_byte = new byte[2]; temp_byte[0] = (byte)inData; temp_byte[1] = (byte)(inData >> 8); int ip = 0; int o_sta = W_API.WriteProcessMemory(hwnd, hAddr, temp_byte, 2, out ip); if (o_sta == 0) { return(false); } return(true); }
/// <summary> /// 写4字节内存 /// </summary> /// <param name="hwnd"></param> /// <param name="hAddr"></param> /// <param name="inData"></param> /// <returns></returns> public bool Write4Byte(IntPtr hwnd, int hAddr, Int32 inData) { byte[] temp_byte = new byte[4]; temp_byte[0] = (byte)inData; temp_byte[1] = (byte)(inData >> 8); temp_byte[2] = (byte)(inData >> 16); temp_byte[3] = (byte)(inData >> 24); int ip = 0; int o_sta = W_API.WriteProcessMemory(hwnd, hAddr, temp_byte, 4, out ip); if (o_sta == 0) { return(false); } return(true); }
//设置金钱 private bool Set_Money(Int32 Money) { Int32 out_data = 0; if (memoryManager.Read4Byte(process_handle, baseAddress.money + OffsetValue, out out_data) == false) { MessageBox.Show("读取失败"); return(false); } if (memoryManager.Write4Byte(process_handle, out_data + 0x134, Money) == false) { MessageBox.Show("写入失败" + W_API.GetLastError().ToString()); return(false); } MessageBox.Show("写入成功"); return(true); }