C++內(nèi)存查找實(shí)例
本文實(shí)例講述了C++內(nèi)存查找的方法,分享給大家供大家參考。具體如下:
windows程序設(shè)計(jì)中的內(nèi)存查找功能,主程序代碼如下:
//
#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)
{
//讀取一頁內(nèi)存
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;
}
/*出錯(cuò),應(yīng)該將地址先轉(zhuǎn)換成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)
{
//出錯(cuò)的情況:寫入的是&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);
//進(jìn)行第一次查找
FindFirst(iVal);
//打印結(jié)果
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;
}
測(cè)試用的程序代碼如下:
#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;
}
希望本文所述對(duì)大家的C++程序設(shè)計(jì)有所幫助。
上一篇:C++實(shí)現(xiàn)raw_input的方法
欄 目:C語言
下一篇:C++實(shí)現(xiàn)查殼程序代碼實(shí)例
本文標(biāo)題:C++內(nèi)存查找實(shí)例
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/3301.html
您可能感興趣的文章
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用函數(shù)刪除字符
- 04-02c語言沒有round函數(shù) round c語言
- 01-10深入理解C++中常見的關(guān)鍵字含義
- 01-10使用C++實(shí)現(xiàn)全排列算法的方法詳解
- 01-10如何查看進(jìn)程實(shí)際的內(nèi)存占用情況詳解
- 01-10c++中inline的用法分析
- 01-10用C++實(shí)現(xiàn)DBSCAN聚類算法
- 01-10全排列算法的非遞歸實(shí)現(xiàn)與遞歸實(shí)現(xiàn)的方法(C++)
- 01-10C++大數(shù)模板(推薦)
- 01-10淺談C/C++中的static與extern關(guān)鍵字的使用詳解


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對(duì)數(shù)函數(shù)的表達(dá)式 c語言中對(duì)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10delphi制作wav文件的方法
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 04-02jquery與jsp,用jquery
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改