I just started learning the WinAPI and I stumbled across different variables for functions.
Right now I am struggling with the ReadProcessMemory
.
The code is:
ReadProcessMemory(phandle, (LPVOID)address, &value , sizeof(value), 0);
Now I understand that the first parameter is a handle to the process but I do not understand why the second parameter (the pointer to the base address) must be a void (LPVOID
) of an address.
In MSDN it says: A pointer to the base address in the specified process from which to read. Before any data transfer occurs, the system verifies that all data in the base address and memory of the specified size is accessible for read access, and if it is not accessible the function fails.
So why does it have to be (LPVOID)address and not just address for the second parameter?
Well, lpBaseAddress
is... an address, so it makes sense that its type should be a pointer. What type does it point to? We don't know, and this (the WinApi) is C, so no templates -- hence the void *
. Also, we don't want to modify the memory, so a const
is of good measure.
About your edit (why the cast): it depends on the type of address
. Any non-volatile
object pointer can be implicitly converted to void const *
, so in that case the cast is unnecessary. I suppose, though, that address is a known integer
constant, in which case a reinterpret_cast
is needed to turn it into a pointer. This is done here with the poor style of a C-style cast, but achieves the same.