private int GetProcessID(string title)
{
Process[] arrayProcess = Process.GetProcesses(); foreach (Process p in arrayProcess)
{
if (p.MainWindowTitle.IndexOf(title) != -1)
{
return p.Id;
}
} return 0;
} private int GetRead(string title, IntPtr baseAddress)
{
byte[] buffer = new byte[4];
IntPtr byteAddress = Marshal.UnsafeAddrOfPinnedArrayElement(buffer, 0);
IntPtr hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, GetProcessID(title));
ReadProcessMemory(hProcess, baseAddress, byteAddress, buffer.Length, hProcess);
CloseHandle(hProcess);
return Marshal.ReadInt32(byteAddress);
} [DllImportAttribute("kernel32.dll", EntryPoint = "ReadProcessMemory")]
public static extern bool
ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, IntPtr lpBuffer, int nSize, IntPtr lpNumberOfBytesRead); [DllImportAttribute("kernel32.dll", EntryPoint = "OpenProcess")]
public static extern IntPtr
OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId); [DllImport("kernel32.dll")]
private static extern void CloseHandle(IntPtr hObject); public const int PROCESS_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED | (SYNCHRONIZE | 4095)); public const int STANDARD_RIGHTS_REQUIRED = 983040; public const int SYNCHRONIZE = 1048576;
读取地址的时候报:尝试读取或写入受保护的内存。这通常指示其他内存已损坏
public static extern bool ReadProcessMemory(ntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, UInt32 nSize, ref UInt32 lpNumberOfBytesRead);