I wrote the following function:
template <typename T> void SetPointer(DWORD64 base, vector<DWORD>Offsets, T value){
base = *reinterpret_cast<DWORD64*>(base);
for (int i = 0; i < Offsets.size() - 1; i++){
base = *reinterpret_cast<DWORD64*>(base + Offsets[i]);
}
*reinterpret_cast<T*>(base + Offsets[Offsets.size() - 1]) = value;
}
It works perfectly, except that I'd like to have checks to make sure the address is valid and that it won't cause a crash, but I haven't found any way that works well to do that. What would be the best way to acheive this?
Edit: This did the trick for what I need:
template <typename T> void SetPointer(DWORD64 base, vector<DWORD>Offsets, T value){
if (base == 0) return;
base = *reinterpret_cast<DWORD64*>(base);
for (int i = 0; i < Offsets.size() - 1; i++){
base = *reinterpret_cast<DWORD64*>(base + Offsets[i]);
if (base == Offsets[i] || base == 0) return;
}
*reinterpret_cast<T*>(base + Offsets[Offsets.size() - 1]) = value;
}