国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++內存查找實例

2020-01-26 15:15:18
字體:
來源:轉載
供稿:網友

本文實例講述了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 <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++程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 西林县| 纳雍县| 西城区| 郑州市| 峨边| 长岭县| 黔东| 义乌市| 鄂托克旗| 象山县| 朝阳县| 花垣县| 荥经县| 武宣县| 儋州市| 慈溪市| 灵台县| 宣威市| 上蔡县| 印江| 罗田县| 大埔区| 会昌县| 白朗县| 苗栗市| 遵化市| 双鸭山市| 申扎县| 苗栗县| 墨竹工卡县| 汉沽区| 多伦县| 榆林市| 高青县| 苏尼特右旗| 白山市| 万盛区| 彰武县| 鸡西市| 寿阳县| 乌鲁木齐县|