几天没写东西了。这几天主要在看多线程,感觉有些复杂。
今天写个关于内存搜索的文章。在游戏中经常需要这样的操作,当然可以用工具实现,但是自己写下代码有助于理解工具的内部实现,也便于自己修改某些功能。
代码只是描述思路和基本操作。(在我机器上运行结果不对,不知道为什么)
#include
<
stdio.h
>
#include < windows.h >
#include < winbase.h >
void main()
... {
const DWORD dwOneGB = 1024*1024*1024;
const DWORD dwOnePage = 4*1024;
DWORD dwValue;
DWORD g_arList[1024];
DWORD g_nListCnt=0;
DWORD id;
DWORD dwBase;
BYTE arBytes[4*1024]=...{0};
HWND hWindow = ::FindWindow(NULL,"扫雷");
if(hWindow == NULL)
...{
::MessageBox(NULL,"窗口不存在","错误",NULL);
return;
}
GetWindowThreadProcessId(hWindow,&id);
HANDLE hProcess1=OpenProcess(PROCESS_ALL_ACCESS,FALSE,id);
printf("要查找的值: ");
scanf("%ld",&dwValue);
//下面判断OS版本。因为不同的系统起始地址不同
OSVERSIONINFO vi;
::GetVersionEx(&vi);
if(vi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
dwBase = 4*1024*1024;
else
dwBase = 64*1024;
for( ;dwBase<2*dwOneGB;dwBase+=dwOnePage)
...{
if(!::ReadProcessMemory(hProcess1,(LPCVOID)dwBase,(LPVOID)arBytes,4*1024,NULL))
//读取系统当前页(4K)的数据
...{
long err = ::GetLastError(); //得到内存读取的错误信息
::MessageBox(NULL,"读内存错误","错误",NULL);
printf("%d",err);
return ;
}
for(int k=0;k<100;k++)
printf("%0x ",arBytes[k]); getchar();
for(int i=0;i<(int)4*1024-3;i++) //与我要找的值做比较
...{
DWORD *pdw;
pdw = (DWORD *)&arBytes[i];
if(pdw[0] == dwValue)
...{
if(g_nListCnt>=1024)
return ;
g_arList[g_nListCnt++] = dwBase+i; //如果找到就保存地址
}
}
}
for(int j=0;j<1024;j++)
printf(" %08x ",g_arList[j]);
}
#include < windows.h >
#include < winbase.h >
void main()
... {
const DWORD dwOneGB = 1024*1024*1024;
const DWORD dwOnePage = 4*1024;
DWORD dwValue;
DWORD g_arList[1024];
DWORD g_nListCnt=0;
DWORD id;
DWORD dwBase;
BYTE arBytes[4*1024]=...{0};
HWND hWindow = ::FindWindow(NULL,"扫雷");
if(hWindow == NULL)
...{
::MessageBox(NULL,"窗口不存在","错误",NULL);
return;
}
GetWindowThreadProcessId(hWindow,&id);
HANDLE hProcess1=OpenProcess(PROCESS_ALL_ACCESS,FALSE,id);
printf("要查找的值: ");
scanf("%ld",&dwValue);
//下面判断OS版本。因为不同的系统起始地址不同
OSVERSIONINFO vi;
::GetVersionEx(&vi);
if(vi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
dwBase = 4*1024*1024;
else
dwBase = 64*1024;
for( ;dwBase<2*dwOneGB;dwBase+=dwOnePage)
...{
if(!::ReadProcessMemory(hProcess1,(LPCVOID)dwBase,(LPVOID)arBytes,4*1024,NULL))
//读取系统当前页(4K)的数据
...{
long err = ::GetLastError(); //得到内存读取的错误信息
::MessageBox(NULL,"读内存错误","错误",NULL);
printf("%d",err);
return ;
}
for(int k=0;k<100;k++)
printf("%0x ",arBytes[k]); getchar();
for(int i=0;i<(int)4*1024-3;i++) //与我要找的值做比较
...{
DWORD *pdw;
pdw = (DWORD *)&arBytes[i];
if(pdw[0] == dwValue)
...{
if(g_nListCnt>=1024)
return ;
g_arList[g_nListCnt++] = dwBase+i; //如果找到就保存地址
}
}
}
for(int j=0;j<1024;j++)
printf(" %08x ",g_arList[j]);
}