Here is a simple example of C++ solution.
Warning: the code was not tested, I might explode right into your face. It searches only for a single value, to adjust it to search for a sequence is left as an exercise.
Code: Select all
template <class T>
bool MyReadProcessMemory(HANDLE hProcess, LPCVOID baseAddress, size_t bytesToRead, std::vector<T>& data)
{
try {
wxCHECK(bytesToRead >= sizeof(T) && bytesToRead % sizeof(T) == 0, false);
data.resize(bytesToRead / sizeof(T));
SIZE_T bytesRead;
if (!ReadProcessMemory(hProcess, baseAddress,
reinterpret_cast<LPVOID>(&data.front()),
bytesToRead, &bytesRead)
|| bytesToRead != bytesRead) {
data.clear();
return false;
}
return true;
}
catch (std::bad_alloc&) {
// handle out of memory error here
}
return false;
}
template <class T>
int FindValue(HANDLE hProcess, LPCVOID baseAddress, size_t bytesToRead, T value)
{
std::vector<T> data;
if (MyReadProcessMemory(hProcess, baseAddress, bytesToRead, data)) {
std::vector<T>::iterator it;
it = std::find(data.begin(), data.end(), value);
if (it != data.end()) {
return it - data.begin();
}
}
return -1;
}
Now you can do
Code: Select all
wxUint8 u8value = 8;
int result = FindValue(hProcess, baseAddress, bytesToRead, u8value);
just like like you can do
Code: Select all
wxUint64 u64value = 64;
int result = FindValue(hProcess, baseAddress, bytesToRead, u64value);
But if you need to search the same block of memory several times using different T type, you have to chose another approach. I don't know what you will use the code for. Also notice the value returned from FindValue is not an address in bytes, but index of an element, changing this would be very simple though.
In the end this code might be useless to you as it is, but it may serve as an example of C++ solution.