WINAPI VirtualQueryEx - 无效句柄

时间:2015-12-20 01:01:17

标签: c++ winapi visual-studio-2012

我正在尝试使用Visual Studio 2012使用VirtualQueryEx读取32位进程的一些内存页。

然而,当我运行程序时,我得到VirtualQueryEx错误6:无效句柄。但是句柄本身[hProcess]没有错误,我传入适当的参数。可能是什么?

#include <windows.h>
#include <tlhelp32.h>
#include <tchar.h>
#include <stdio.h>

//  Forward declarations:
BOOL GetProcessList( );
BOOL ListProcessModules( DWORD dwPID );
BOOL ListProcessThreads( DWORD dwOwnerPID );
void printError( TCHAR* msg );

int main( void )
{
  GetProcessList( );
   system("pause");

  return 0;
}

BOOL GetProcessList( )
{
  HANDLE hProcessSnap;
  HANDLE hProcess;
  PROCESSENTRY32 pe32;
  DWORD dwPriorityClass;

  // Take a snapshot of all processes in the system.
  hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
  if( hProcessSnap == INVALID_HANDLE_VALUE )
  {
    printError( TEXT("CreateToolhelp32Snapshot (of processes)") );
    return( FALSE );
  }

  // Set the size of the structure before using it.
  pe32.dwSize = sizeof( PROCESSENTRY32 );

  // Retrieve information about the first process,
  // and exit if unsuccessful
  if( !Process32First( hProcessSnap, &pe32 ) )
  {
    printError( TEXT("Process32First") ); // show cause of failure
    CloseHandle( hProcessSnap );          // clean the snapshot object
    return( FALSE );
  }

  // Now walk the snapshot of processes, and
  // display information about each process in turn
  do
  {
    //If the process name equals foo_process.exe
if (!_tcscmp(pe32.szExeFile, _T("foo_process.exe"))) 
{

    hProcess = OpenProcess(PROCESS_VM_READ | PROCESS_QUERY_INFORMATION, false, pe32.th32ProcessID );
    if( hProcess == NULL )
      printError( TEXT("OpenProcess") );

    unsigned char *addr = NULL;
    MEMORY_BASIC_INFORMATION meminfo;

    if (VirtualQueryEx(hProcess, addr, &meminfo, sizeof(meminfo)) == 0){
         printError( TEXT("VirtualQueryEx") );
         //return FALSE;
    }

}
  } while( Process32Next( hProcessSnap, &pe32 ) );

  CloseHandle( hProcessSnap );

  return( TRUE );
}

void printError( TCHAR* msg )
{
 ...
}

编辑:句柄有价值: enter image description here

编辑2:更多信息:

  • Windows 7 64位平台。

  • 运行Visual Studio 2012(32位调试器) 作为管理员

  • 处理是* 32(32位)

1 个答案:

答案 0 :(得分:-1)

无符号字符* addr = NULL;

您要VirtualQueryEx查询无效的地址0,从而导致失败。