C++VirtualQueryEx无限循环

C++ VirtualQueryEx infinite loop

本文关键字:无限循环 C++VirtualQueryEx      更新时间:2023-10-16

我目前正在使用C++重新创建一个内存修改器应用程序,最初是在C#中。

所有的功劳都归功于"gimmeamilk",他是我在YouTube上关注的教程(第1段,共8段)。我强烈建议任何试图创建类似应用程序的人使用这些教程。

我遇到的问题是,我的VirtualQueryEx似乎永远都在运行。我扫描的过程是"notepad.exe",我通过命令行参数传递给应用程序。

std::cout<<"Create scan startedn";
#define WRITABLE (PAGE_READWRITE | PAGE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY) //These are all the flags that will be used to determin if a memory block is writable.
MEMBLOCK * mb_list = NULL;          //pointer to the head of the link list to be returned
MEMORY_BASIC_INFORMATION meminfo;   //holder for the VirtualQueryEx return struct
unsigned char *addr = 0;            //holds the value to pass to VirtualQueryEx
HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS,false, pid);
if(hProc) 
{
    while(1)
    {
        if(VirtualQueryEx(hProc,addr, &meminfo, sizeof(meminfo)) == 0) 
        {
            break;
        }

        if((meminfo.State & MEM_COMMIT) && (meminfo.Protect & WRITABLE)) //((binary comparison of meminfos state and MEM_COMMIT, this is basically filtering out memory that the process has reserved but not used)())
        {
            MEMBLOCK * mb = create_memblock(hProc, &meminfo);
            if(mb)
            {
                mb->next = mb_list;
                mb_list = mb;
            }
        }
        addr = (unsigned char *)meminfo.BaseAddress + meminfo.RegionSize;//move the adress along by adding on the length of the current block
    }
}
else
{
    std::cout<<"Failed to open processn";
}
std::cout<<"Create scan finishedn";
return mb_list;

此代码的输出将导致

Create scan started on process:7228

然后它不会向控制台返回任何其他内容。不幸的是,通过Youtube视频链接到的示例源代码已不可用。(7228将根据notepad.exe的当前pid进行更改)

编辑问题回复@Hans Passant我还是不明白,我想我做的是

Starting a infinite loop
{
   Testing using vqx if the address is valid and populating my MEM_BASIC_etc..
   {
        (has the process commited to using that addr of memory)(is the memory writeable)
        {
            create memblock etc
        } 
   }
   move the address along by the size of the current block
}

我的程序是x32,记事本也是(据我所知)。

我的问题是因为我使用的是x64位操作系统,所以我实际上检查了一半的块(这里的块是指操作系统在内存中分配的单元),并导致它循环吗?

非常感谢你的帮助!我想了解我的问题并解决它。

您的问题是编译一个32位程序,并使用它来解析64位程序的内存。您将"addr"定义为无符号字符指针,在本例中为32位大小。它不能包含64位地址,这是您出现问题的原因。

如果您的目标进程是64位的,那么也可以将程序编译为64位。对于32位目标进程,编译32位。这通常是处理外部进程内存的最佳技术,也是最快的解决方案。

根据您所做的操作,您还可以使用#ifdef和其他条件语句来使用64位变量,具体取决于目标,但原始解决方案通常更容易。