using System;
using System.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace PlantsVsZombiesTool
{
publicabstractclass Helper
{
[DllImportAttribute("kernel32.dll", EntryPoint ="ReadProcessMemory")]
publicstaticexternbool ReadProcessMemory
(
IntPtr hProcess,
IntPtr lpBaseAddress,
IntPtr lpBuffer,
int nSize,
IntPtr lpNumberOfBytesRead
);
[DllImportAttribute("kernel32.dll", EntryPoint ="OpenProcess")]
publicstaticextern IntPtr OpenProcess
(
int dwDesiredAccess,
bool bInheritHandle,
int dwProcessId
);
[DllImport("kernel32.dll")]
privatestaticexternvoid CloseHandle
(
IntPtr hObject
);
//写内存
[DllImportAttribute("kernel32.dll", EntryPoint ="WriteProcessMemory")]
publicstaticexternbool WriteProcessMemory
(
IntPtr hProcess,
IntPtr lpBaseAddress,
int[] lpBuffer,
int nSize,
IntPtr lpNumberOfBytesWritten
);
//获取窗体的进程标识ID
publicstaticint GetPid(string windowTitle)
{
int rs =0;
Process[] arrayProcess = Process.GetProcesses();
foreach (Process p in arrayProcess)
{
if (p.MainWindowTitle.IndexOf(windowTitle) !=-1)
{
rs = p.Id;
break;
}
}
return rs;
}
//根据进程名获取PID
publicstaticint GetPidByProcessName(string processName)
{
Process[] arrayProcess = Process.GetProcessesByName(processName);
foreach (Process p in arrayProcess)
{
return p.Id;
}
return0;
}
//根据窗体标题查找窗口句柄(支持模糊匹配)
publicstatic IntPtr FindWindow(string title)
{
Process[] ps = Process.GetProcesses();
foreach (Process p in ps)
{
if (p.MainWindowTitle.IndexOf(title) !=-1)
{
return p.MainWindowHandle;
}
}
return IntPtr.Zero;
}
//读取内存中的值
publicstaticint ReadMemoryValue(int baseAddress,string processName)
{
try
{
byte[] buffer =newbyte[4];
IntPtr byteAddress = Marshal.UnsafeAddrOfPinnedArrayElement(buffer, 0); //获取缓冲区地址
IntPtr hProcess = OpenProcess(0x1F0FFF, false, GetPidByProcessName(processName));
ReadProcessMemory(hProcess, (IntPtr)baseAddress, byteAddress, 4, IntPtr.Zero); //将制定内存中的值读入缓冲区
CloseHandle(hProcess);
return Marshal.ReadInt32(byteAddress);
}
catch
{
return0;
}
}
//将值写入指定内存地址中
publicstaticvoid WriteMemoryValue(int baseAddress, string processName, int value)
{
IntPtr hProcess = OpenProcess(0x1F0FFF, false, GetPidByProcessName(processName)); //0x1F0FFF 最高权限
WriteProcessMemory(hProcess, (IntPtr)baseAddress, newint[] { value }, 4, IntPtr.Zero);
CloseHandle(hProcess);
}
}
}
using System.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace PlantsVsZombiesTool
{
publicabstractclass Helper
{
[DllImportAttribute("kernel32.dll", EntryPoint ="ReadProcessMemory")]
publicstaticexternbool ReadProcessMemory
(
IntPtr hProcess,
IntPtr lpBaseAddress,
IntPtr lpBuffer,
int nSize,
IntPtr lpNumberOfBytesRead
);
[DllImportAttribute("kernel32.dll", EntryPoint ="OpenProcess")]
publicstaticextern IntPtr OpenProcess
(
int dwDesiredAccess,
bool bInheritHandle,
int dwProcessId
);
[DllImport("kernel32.dll")]
privatestaticexternvoid CloseHandle
(
IntPtr hObject
);
//写内存
[DllImportAttribute("kernel32.dll", EntryPoint ="WriteProcessMemory")]
publicstaticexternbool WriteProcessMemory
(
IntPtr hProcess,
IntPtr lpBaseAddress,
int[] lpBuffer,
int nSize,
IntPtr lpNumberOfBytesWritten
);
//获取窗体的进程标识ID
publicstaticint GetPid(string windowTitle)
{
int rs =0;
Process[] arrayProcess = Process.GetProcesses();
foreach (Process p in arrayProcess)
{
if (p.MainWindowTitle.IndexOf(windowTitle) !=-1)
{
rs = p.Id;
break;
}
}
return rs;
}
//根据进程名获取PID
publicstaticint GetPidByProcessName(string processName)
{
Process[] arrayProcess = Process.GetProcessesByName(processName);
foreach (Process p in arrayProcess)
{
return p.Id;
}
return0;
}
//根据窗体标题查找窗口句柄(支持模糊匹配)
publicstatic IntPtr FindWindow(string title)
{
Process[] ps = Process.GetProcesses();
foreach (Process p in ps)
{
if (p.MainWindowTitle.IndexOf(title) !=-1)
{
return p.MainWindowHandle;
}
}
return IntPtr.Zero;
}
//读取内存中的值
publicstaticint ReadMemoryValue(int baseAddress,string processName)
{
try
{
byte[] buffer =newbyte[4];
IntPtr byteAddress = Marshal.UnsafeAddrOfPinnedArrayElement(buffer, 0); //获取缓冲区地址
IntPtr hProcess = OpenProcess(0x1F0FFF, false, GetPidByProcessName(processName));
ReadProcessMemory(hProcess, (IntPtr)baseAddress, byteAddress, 4, IntPtr.Zero); //将制定内存中的值读入缓冲区
CloseHandle(hProcess);
return Marshal.ReadInt32(byteAddress);
}
catch
{
return0;
}
}
//将值写入指定内存地址中
publicstaticvoid WriteMemoryValue(int baseAddress, string processName, int value)
{
IntPtr hProcess = OpenProcess(0x1F0FFF, false, GetPidByProcessName(processName)); //0x1F0FFF 最高权限
WriteProcessMemory(hProcess, (IntPtr)baseAddress, newint[] { value }, 4, IntPtr.Zero);
CloseHandle(hProcess);
}
}
}