title: Using VirtualQueryEx to enumerate modules at remote process doesn't return all modules
body: <p>I am trying to get a list of DLLs that a given process is using, I am trying to achieve that through VirtualQueryEx. My problem is that it return to me just a partial list of DLLs and not all of them (i can see the list using Process Explorer or using VirtualQuery on the given process).</p>
  
  <p>Here's the code:</p>
  
  <pre><code>    char szBuf[MAX_PATH * 100] = { 0 };
      PBYTE pb = NULL;
      MEMORY_BASIC_INFORMATION mbi;
      HANDLE h_process = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, iPID);
  
  
      while (VirtualQueryEx(h_process, pb, &amp;mbi, sizeof(mbi)) == sizeof(mbi)) {
  
          int nLen;
          char szModName[MAX_PATH];
  
          if (mbi.State == MEM_FREE)
          mbi.AllocationBase = mbi.BaseAddress;
  
          if ((mbi.AllocationBase == hInstDll) ||
              (mbi.AllocationBase != mbi.BaseAddress) ||
              (mbi.AllocationBase == NULL)) {
          // Do not add the module name to the list
          // if any of the following is true:
          // 1. If this region contains this DLL
          // 2. If this block is NOT the beginning of a region
          // 3. If the address is NULL
          nLen = 0;
          } else {
          nLen = GetModuleFileNameA((HINSTANCE) mbi.AllocationBase, 
              szModName, _countof(szModName));
          }
  
          if (nLen &gt; 0) {
          wsprintfA(strchr(szBuf, 0), "\n%p-%s", 
              mbi.AllocationBase, szModName);
          }
  
          pb += mbi.RegionSize;
      }
  </code></pre>
  
  <p>I am getting the result on <code>szBuf</code>.</p>
  
  <p>This function is part of a DLL file so that it is harder for me to debug.</p>
  
  <p>Right now the DLL is compiled as x64 binary and i am using it against x64 processes.</p>
  
  <p>P.S i know about EnumProcessModules and i am not using it with a reason (too long:).</p>