I have a hex literal to use as lpBaseAddress for the function below:
1 2 3 4 5 6
|
bool WINAPI WriteProcessMemory(
HANDLE hProcess,
LPVOID lpBaseAddress,
LPCVOID lpBuffer,
SIZE_T nSize,
SIZE_T *lpNumberOfBytesWritten)
|
I plan to do the following, then pass p1 as lpBaseAddress to WriteProcessMemory:
1 2 3
|
#include <cstdint>
uintptr_t v1{ 0x12345678 };
LPVOID p1{ reinterpret_cast<LPVOID>(V1) };
|
My questions are:
(I'm sorry for the open ended questions below; if someone would like to answer, do feel free to address just one topic and forget the rest)
1) Does my code above confirm to ("modern/C++11/14") best practices?
(I think I've seen examples where v1 is stored as DWORD, then passed straight into WriteProcessMemory(); at other times I've seen (LPVOID) v1 used prior to calling the function:
http://www.cplusplus.com/forum/beginner/9504/ )
2) Are such conversions above safe/predictable/have well defined behavior)?
I would be converting an integer to a pointer, does this fall under the "reverse round-trip" category below? (I don't think it does; at the same time, is there a reason and an example for "
the same pointer may have multiple integer representations"?)
Below is the excerpt from:
http://en.cppreference.com/w/cpp/language/reinterpret_cast :
A pointer converted to an integer of sufficient size and back to the same pointer type is guaranteed to have its original value...(the round-trip conversion in the opposite direction is not guaranteed; the same pointer may have multiple integer representations) |
3) If an address has a 1 in its most significant bit (e.g., 0xFFFFFFFF), would the signed type INTPTR_T be inappropriate for storing the hex literal as address?
(my tentative answer is no, b/c it seems that both uINTPTR_T and INTPTR_T should work, since I could skip them both and instead do
LPVOID p1{ reinterpret_cast<LPVOID>(0x12345678)}; |
i.e., regardless of uINTPTR_T or INTPTR_T, they're the same bits underneath)
4) What exactly are the differences, in terms of internal implementation, between a pointer and an int initialized to 0x12345678?
(From
http://www.cplusplus.com/doc/oldtutorial/variables/ , my tentative answer is that there's no difference in terms of internal implementation; the type information is only there to help the compiler interpret the bytes in memory)
Thank you very much