gpt4 book ai didi

c++ - WINAPI VirtualQueryEx - 无效句柄

转载 作者:行者123 更新时间:2023-12-01 14:30:38 25 4
gpt4 key购买 nike

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

但是,当我运行该程序时,出现 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 )
{
...
}

编辑:句柄具有值(value): enter image description here

编辑 2:更多信息:

  • Windows 7 64 位平台。

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

  • 进程为 *32(32 位)

最佳答案

unsigned char *addr = NULL;

你要求 VirtualQueryEx 查询无效的地址 0,导致失败。

关于c++ - WINAPI VirtualQueryEx - 无效句柄,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34377013/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com