读其内存值的问题?返回值与实际值不同
static class Program{
private int baseAddress = 0x006A9EC0; //游戏内存基址
private string processName = "PlantsVsZombies"; //游戏进程名字
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
int address = ReadMemoryValue(baseAddress);//本来这里实际返加的不是0,但次次运行都返回为0,不知道哪里错了,请大家多看看
Console.WriteLine(address.ToString());
Console.ReadLine();
}
//根据进程名获取PID
public static int GetPidByProcessName(string processName)
{
Process[] arrayProcess = Process.GetProcessesByName(processName);
foreach (Process p in arrayProcess)
{
return p.Id;
}
return 0;
}
//读内存值
public static int ReadMemoryValue(int baseAddress,string processName)
{
try
{
byte[] buffer = new byte[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
{
return 0;
}
}
}