tags:

views:

57

answers:

2

My code:

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

The 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 it a bug of VirtualQuery?

+1  A: 

Passing a kernel-mode pointer to this function can result in no information being returned. Check the return value.

neuralbrain
+1  A: 

This is not a bug. The documentation of VirtualQuery() states:

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 include ERROR_INVALID_PARAMETER.

Check the function result to be equal to sizeof(meminf) before using the data in the structure, or initialize the structure with values that will make the code that follows do the right thing. If the function returned 0 no data was copied to the structure, so it will still contain whatever data was previously in it. Without initialization this will be random bytes on the stack.

mghie