我用VS2005做的一个改键程序之前没有读取内存使用一切正常后来调用kernel32.dll里的API读取下魔兽争霸的内存在32位环境下运行正常在64位环境下运行程序后毫...
我用VS2005做的一个改键程序之前没有读取内存 使用一切正常 后来调用 kernel32.dll 里的API 读取下魔兽争霸的内存 在32位环境下 运行正常 在64位环境下运行程序后毫无反应(不显示界面也没有抛出异常) 怀疑是程序加载时没有正确调用 kernel32.dll 的缘故 不太清楚X86和X64调用API的区别 求大神指点~ 部分代码如下:
[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
);
64位系统IntPtr的Size是8字节,而32位的是4字节
可能因为这个问题出现错误
需要更详细的情况,比如调用的返回值
搜索P.Invoke能找到很多平台调用(DLL)的资料
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool ReadProcessMemory(
IntPtr hProcess,
IntPtr lpBaseAddress,
[Out] byte[] lpBuffer,
int dwSize,
out int lpNumberOfBytesRead
);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool ReadProcessMemory(
IntPtr hProcess,
IntPtr lpBaseAddress,
[Out, MarshalAs(UnmanagedType.AsAny)] object lpBuffer,
int dwSize,
out int lpNumberOfBytesRead
);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool ReadProcessMemory(
IntPtr hProcess,
IntPtr lpBaseAddress,
IntPtr lpBuffer,
int dwSize,
out int lpNumberOfBytesRead
);
//示例
public int ReadInt32(IntPtr hProcess, uint dwAddress)
{
byte[] buffer = new byte[4];
int bytesread;
Win32Api.ReadProcessMemory(hProcess, dwAddress, buffer, 4, out bytesread);
return BitConverter.ToInt32(buffer, 0);
}
参考资料:
http://www.pinvoke.net/default.aspx/kernel32.readprocessmemory