Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of dwLength parameter of VirtualQueryEx?

Tags:

c

windows

winapi

What is the purpose of dwLength parameter of VirtualQueryEx? Here it's described like this:

lpBuffer [out] A pointer to a MEMORY_BASIC_INFORMATION structure in which information about the specified page range is returned.

dwLength [in] The size of the buffer pointed to by the lpBuffer parameter, in bytes.

Are there any reasons to use anything but sizeof(MEMORY_BASIC_INFORMATION) for that?

like image 717
aemxdp Avatar asked Jan 18 '26 22:01

aemxdp


1 Answers

  1. As often happens with Windows APIs, the size field is used for structure versioning. A future version of Windows can provide additional information in a bigger structure for applications that are aware of the new functionality without breaking the binary compatibility with older applications.

  2. There's also a more interesting use case: current Windows versions allow both 32 and 64 bit processes to co-exist on the same machine. Each process gets APIs with the correct data type sizes & co., but what happens if a 64 bit process (say, a debugger) wants to mess with the memory pages of a 32 bit process or viceversa? In this case, it cannot use the "regular" MEMORY_BASIC_INFORMATION structure - which is an alias for a structure appropriate for its "bitness", but must use the version appropriate for the target process (see here, at the bottom of the page). In this case, VirtualQueryEx can use the size info to check if the caller has provided the correct struct, and provide an error instead of writing in wrong memory.

like image 166
Matteo Italia Avatar answered Jan 20 '26 12:01

Matteo Italia