VirtualQuery gives illegal result. This is mistake?

My code:

MEMORY_BASIC_INFORMATION meminf;
::VirtualQuery(box.pBits, &meminf, sizeof(meminf));

      

Results:

   meminf:
    BaseAddress 0x40001000 void *
    AllocationBase 0x00000000 void *
    AllocationProtect 0x00000000 unsigned long
    RegionSize 0x0de0f000 unsigned long
    State 0x00010000 unsigned long
    Protect 0x00000001 unsigned long
    Type 0x00000000 unsigned long

Notes:

(1) AllocationBase is NULL while BaseAddress is not NULL
(2) AllocationProtect is 0 (not a protection value)

Is this a VirtualQuery bug?

+2


source to share


2 answers


Passing a kernel-mode pointer to this function may not return information. Check the return value.



+1


source


It's not a mistake. The documentation VirtualQuery()

says:

The return value is the actual number of bytes returned in the information buffer.

If the function fails, the return value is zero. To get extended error information, call GetLastError. Possible error values: ERROR_INVALID_PARAMETER.



Before using data in a structure, check the function result to be equal, sizeof(meminf)

or initialize the structure with values ​​that will lead to the following rule. If the function returned 0, no data was copied into the structure, so it will still contain any data previously. Without initialization, these will be random bytes on the stack.

+1


source







All Articles