title: C++ VirtualQueryEx infinite loop
body: <p>I'm currently re-creating a memory modifier application using C++, the original was in C#.</p>
  
  <p>All credit goes to "gimmeamilk" who's tutorials Ive been following on <a href="http://www.youtube.com/watch?v=YRPMdb1YMS8&amp;feature=share&amp;list=UUnxW29RC80oLvwTMGNI0dAg" rel="nofollow">YouTube(video 1 of 8)</a>. I would highly recommend these tutorials for anyone attempting to create a similar application.</p>
  
  <p>The problem I have is that my VirtualQueryEx seems to run forever. The process I'm scanning is "notepad.exe" and I am passing to the application via command line parameter.</p>
  
  <pre><code>std::cout&lt;&lt;"Create scan started\n";
  #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, &amp;meminfo, sizeof(meminfo)) == 0) 
          {
              break;
          }
  
  
          if((meminfo.State &amp; MEM_COMMIT) &amp;&amp; (meminfo.Protect &amp; 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, &amp;meminfo);
              if(mb)
              {
                  mb-&gt;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&lt;&lt;"Failed to open process\n";
  }
  std::cout&lt;&lt;"Create scan finished\n";
  
  return mb_list;
  </code></pre>
  
  <p>The output from this code results in</p>
  
  <pre><code>Create scan started on process:7228
  </code></pre>
  
  <p>Then it does not return anything else to the console. Unfortunately the example source code linked to via the Youtube video is no longer available.
  (7228 will change based on the current pid of notepad.exe)</p>
  
  <p><strong>edit-reply to question @Hans Passant</strong>
  I still don't understand, what I think Im doing is</p>
  
  <pre><code>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
  }
  </code></pre>
  
  <p>My program is x32 and so is notepad (as far as I'm aware).</p>
  
  <p>Is my problem that because I'm using a x64 bit OS that I'm actually inspecting half of a block (a block here meaning the unit assigned by the OS in memory) and its causing it to loop?</p>
  
  <p>Big thanks for your help! I want to understand my problem as well as fix it.</p>