C++内存查找实例
来源:本站原创|时间:2020-01-10|栏目:C语言|点击: 次
本文实例讲述了C++内存查找的方法,分享给大家供大家参考。具体如下:
windows程序设计中的内存查找功能,主程序代码如下:
复制代码 代码如下:
// MemRepair.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <Windows.h>
BOOL FindFirst(DWORD dwValue);
BOOL FindNext(DWORD dwValue);
HANDLE g_hProcess;
DWORD g_arList[1024];
DWORD g_nListCnt;
BOOL CompareAPage(DWORD dwBaseAddr, DWORD dwValue)
{
//读取一页内存
BYTE arBytes[4096];
BOOL bRead = ::ReadProcessMemory(g_hProcess, (LPVOID)dwBaseAddr, arBytes, 4096,NULL);
if (bRead == FALSE)
{
return FALSE;
}
DWORD *pdw;
for (int i=0;i<4096-4;i++)
{
pdw = (DWORD*)&arBytes[i];
if (pdw[0] == dwValue)
{
g_arList[g_nListCnt++] = dwBaseAddr+i;
}
/*出错,应该将地址先转换成DWORD*,即指向DWORD的地址,然后再取[0]
if ((DWORD)&arBytes[i] == dwValue)
{
g_arList[g_nListCnt++] = dwBaseAddr+i;
}
*/
}
if (g_nListCnt > 1024)
{
printf("the position is large than 1024..");
return FALSE;
}
return TRUE;
}
BOOL FindFirst(DWORD dwValue)
{
const DWORD dwOneGB = 1 * 1024 *1024 *1024; // 1GB
const DWORD dwOnePage = 4* 1024; // 4K
DWORD dwBase;
OSVERSIONINFO versionInfo={0};
versionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
::GetVersionEx(&versionInfo);
if (versionInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS ) //win98
{
dwBase = 4 * 1024 *1024; // 4MB
}
else
{
dwBase = 64 * 1024; // 64KB
}
//从开始地址到2GB的空间查找
for (;dwBase<2*dwOneGB;dwBase+=dwOnePage)
{
CompareAPage(dwBase,dwValue);
}
return TRUE;
}
BOOL FindNext(DWORD dwValue)
{
DWORD dwOriCnt = g_nListCnt;
DWORD dwReadValue;
BOOL bRet = FALSE;
g_nListCnt = 0;
for (int i=0;i<dwOriCnt;i++)
{
if (::ReadProcessMemory(g_hProcess,(LPVOID)g_arList[i],&dwReadValue,sizeof(DWORD),0))
{
if (dwReadValue == dwValue)
{
g_arList[g_nListCnt++] = g_arList[i];
bRet = TRUE;
}
}
}
return bRet;
}
void ShowList()
{
for (int i=0;i<g_nListCnt;i++)
{
printf("%08lX\n", g_arList[i]);
}
}
BOOL WriteMemory(DWORD dwAddr, DWORD dwValue)
{
//出错的情况:写入的是&dwValue,而不是(LPVOID)dwValue
return WriteProcessMemory(g_hProcess,(LPVOID)dwAddr,&dwValue,sizeof(DWORD),NULL);
}
int _tmain(int argc, _TCHAR* argv[])
{
g_nListCnt = 0;
memset(g_arList,0,sizeof(g_arList));
char szCommandLine[]="c:\\testor.exe";
STARTUPINFO si={sizeof(STARTUPINFO)};
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = TRUE;
PROCESS_INFORMATION pi;
BOOL bRet = CreateProcess(NULL, szCommandLine,NULL,NULL,FALSE,CREATE_NEW_CONSOLE,NULL,NULL,&si,&pi);
if (bRet == FALSE)
{
printf("createProcess failed...");
return -1;
}
::CloseHandle(pi.hThread);
g_hProcess = pi.hProcess;
//输入修改值
int iVal;
printf("Input iVal=");
scanf("%d", &iVal);
//进行第一次查找
FindFirst(iVal);
//打印结果
ShowList();
//再次查找
while (g_nListCnt > 1)
{
printf("input iVal:\n");
scanf("%d",&iVal);
FindNext(iVal);
ShowList();
}
//修改值
printf("input new value:\n");
scanf("%d",&iVal);
if (WriteMemory(g_arList[0],iVal))
{
printf("write suc...");
}
::CloseHandle(g_hProcess);
return 0;
}
//
#include "stdafx.h"
#include <Windows.h>
BOOL FindFirst(DWORD dwValue);
BOOL FindNext(DWORD dwValue);
HANDLE g_hProcess;
DWORD g_arList[1024];
DWORD g_nListCnt;
BOOL CompareAPage(DWORD dwBaseAddr, DWORD dwValue)
{
//读取一页内存
BYTE arBytes[4096];
BOOL bRead = ::ReadProcessMemory(g_hProcess, (LPVOID)dwBaseAddr, arBytes, 4096,NULL);
if (bRead == FALSE)
{
return FALSE;
}
DWORD *pdw;
for (int i=0;i<4096-4;i++)
{
pdw = (DWORD*)&arBytes[i];
if (pdw[0] == dwValue)
{
g_arList[g_nListCnt++] = dwBaseAddr+i;
}
/*出错,应该将地址先转换成DWORD*,即指向DWORD的地址,然后再取[0]
if ((DWORD)&arBytes[i] == dwValue)
{
g_arList[g_nListCnt++] = dwBaseAddr+i;
}
*/
}
if (g_nListCnt > 1024)
{
printf("the position is large than 1024..");
return FALSE;
}
return TRUE;
}
BOOL FindFirst(DWORD dwValue)
{
const DWORD dwOneGB = 1 * 1024 *1024 *1024; // 1GB
const DWORD dwOnePage = 4* 1024; // 4K
DWORD dwBase;
OSVERSIONINFO versionInfo={0};
versionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
::GetVersionEx(&versionInfo);
if (versionInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS ) //win98
{
dwBase = 4 * 1024 *1024; // 4MB
}
else
{
dwBase = 64 * 1024; // 64KB
}
//从开始地址到2GB的空间查找
for (;dwBase<2*dwOneGB;dwBase+=dwOnePage)
{
CompareAPage(dwBase,dwValue);
}
return TRUE;
}
BOOL FindNext(DWORD dwValue)
{
DWORD dwOriCnt = g_nListCnt;
DWORD dwReadValue;
BOOL bRet = FALSE;
g_nListCnt = 0;
for (int i=0;i<dwOriCnt;i++)
{
if (::ReadProcessMemory(g_hProcess,(LPVOID)g_arList[i],&dwReadValue,sizeof(DWORD),0))
{
if (dwReadValue == dwValue)
{
g_arList[g_nListCnt++] = g_arList[i];
bRet = TRUE;
}
}
}
return bRet;
}
void ShowList()
{
for (int i=0;i<g_nListCnt;i++)
{
printf("%08lX\n", g_arList[i]);
}
}
BOOL WriteMemory(DWORD dwAddr, DWORD dwValue)
{
//出错的情况:写入的是&dwValue,而不是(LPVOID)dwValue
return WriteProcessMemory(g_hProcess,(LPVOID)dwAddr,&dwValue,sizeof(DWORD),NULL);
}
int _tmain(int argc, _TCHAR* argv[])
{
g_nListCnt = 0;
memset(g_arList,0,sizeof(g_arList));
char szCommandLine[]="c:\\testor.exe";
STARTUPINFO si={sizeof(STARTUPINFO)};
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = TRUE;
PROCESS_INFORMATION pi;
BOOL bRet = CreateProcess(NULL, szCommandLine,NULL,NULL,FALSE,CREATE_NEW_CONSOLE,NULL,NULL,&si,&pi);
if (bRet == FALSE)
{
printf("createProcess failed...");
return -1;
}
::CloseHandle(pi.hThread);
g_hProcess = pi.hProcess;
//输入修改值
int iVal;
printf("Input iVal=");
scanf("%d", &iVal);
//进行第一次查找
FindFirst(iVal);
//打印结果
ShowList();
//再次查找
while (g_nListCnt > 1)
{
printf("input iVal:\n");
scanf("%d",&iVal);
FindNext(iVal);
ShowList();
}
//修改值
printf("input new value:\n");
scanf("%d",&iVal);
if (WriteMemory(g_arList[0],iVal))
{
printf("write suc...");
}
::CloseHandle(g_hProcess);
return 0;
}
测试用的程序代码如下:
复制代码 代码如下:
#include "stdafx.h"
#include <stdio.h>
int g_nNum = 1003;
int _tmain(int argc, _TCHAR* argv[])
{
int i = 200;
while(1)
{
printf("i=%d,&i=%08lX...g_nNum=%d,&g_nNum=%08lX\n\n",i--,&i,--g_nNum,&g_nNum);
getchar();
}
return 0;
}
#include <stdio.h>
int g_nNum = 1003;
int _tmain(int argc, _TCHAR* argv[])
{
int i = 200;
while(1)
{
printf("i=%d,&i=%08lX...g_nNum=%d,&g_nNum=%08lX\n\n",i--,&i,--g_nNum,&g_nNum);
getchar();
}
return 0;
}
希望本文所述对大家的C++程序设计有所帮助。
您可能感兴趣的文章
- 04-02c语言函数调用后清空内存 c语言调用函数删除字符
- 04-02c语言没有round函数 round c语言
- 01-10深入理解C++中常见的关键字含义
- 01-10使用C++实现全排列算法的方法详解
- 01-10如何查看进程实际的内存占用情况详解
- 01-10c++中inline的用法分析
- 01-10用C++实现DBSCAN聚类算法
- 01-10全排列算法的非递归实现与递归实现的方法(C++)
- 01-10C++大数模板(推荐)
- 01-10浅谈C/C++中的static与extern关键字的使用详解
阅读排行
本栏相关
- 04-02c语言函数调用后清空内存 c语言调用
- 04-02func函数+在C语言 func函数在c语言中
- 04-02c语言的正则匹配函数 c语言正则表达
- 04-02c语言用函数写分段 用c语言表示分段
- 04-02c语言中对数函数的表达式 c语言中对
- 04-02c语言编写函数冒泡排序 c语言冒泡排
- 04-02c语言没有round函数 round c语言
- 04-02c语言分段函数怎么求 用c语言求分段
- 04-02C语言中怎么打出三角函数 c语言中怎
- 04-02c语言调用函数求fibo C语言调用函数求
随机阅读
- 01-10C#中split用法实例总结
- 01-10SublimeText编译C开发环境设置
- 01-10delphi制作wav文件的方法
- 01-11ajax实现页面的局部加载
- 01-10使用C语言求解扑克牌的顺子及n个骰子
- 04-02jquery与jsp,用jquery
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文
- 08-05DEDE织梦data目录下的sessions文件夹有什
- 08-05织梦dedecms什么时候用栏目交叉功能?
- 08-05dedecms(织梦)副栏目数量限制代码修改