c++winapivirtualquery

Win API VirtualQueryEx Function,ERROR_BAD_LENGTH


Hi I try to call the VirtualQueryEx function to get some Information about Memory Protection, however my code gives me error 0x18 (ERROR_BAD_LENGTH) and i dont know whats wrong with my code;

code snippet:

PMEMORY_BASIC_INFORMATION alte;

VirtualQueryEx(processhandle,(LPVOID) (address),alte,sizeof(PMEMORY_BASIC_INFORMATION));

thanks for your help


Solution

  • alte needes to by declared as MEMORY_BASIC_INFORMATION not a pointer to one.

    MEMORY_BASIC_INFORMATION alte;
    
    VirtualQueryEx(processhandle,(LPVOID) (address),&alte,sizeof(MEMORY_BASIC_INFORMATION));
    

    edit: Note its sizeof(MEMORY_BASIC_INFORMATION) not sizeof(PMEMORY_BASIC_INFORMATION).

    Actually, it's better to write this anyway

    VirtualQueryEx(processhandle,(LPVOID) (address),&alte,sizeof(alte));