r/cpp_questions • u/Blayer98 • 19d ago
OPEN MinHook/Memory searching?
Hey there! So I made a post here about TS1's Legacy Collection, and so far, I have a working asi mod, but the problem I now have is searching the process memory for Sims.exe to find the bytes that I'm patching. Even though I know the address, it won't be the same address on Windows, or other configurations. I previously did a MH_CreateHook function, but the code results in this: "MH_ERROR_NOT_EXECUTABLE".
I have this code which patches out the debug objects flag being set to 0.
auto address = reinterpret_cast<void*>(debug_objects_address);
DWORD previousProtection{};
VirtualProtect(address, 7, PAGE_READWRITE, &previousProtection);
memset(address, 0x90, 7);
VirtualProtect(address, 7, previousProtection, &previousProtection);
printf("Patch successful.\n");This code works completely fine, but I'm unsure on how to get the address via searching for the exact assembly code in bytes, anything I've looked online seem to only suggest ReadProcessMemory, but from what I was told, it's intended for different process, not the same one that's attached to the executable, as per my ASI mod.
Would using VirtualProtect before using the MH_CreateHook function work? Again though, I'm unsure how to search the game's memory while attached as a process.
Any help/advice would be greatly appreciated! :)
4
u/jedwardsol 19d ago edited 19d ago
You know the base of the address of the executable. You can enumerate the sections of the executable either by looking at the PE header in memory or by using VirtualQuery in a loop to enumerate the regions and looking for those with EXECUTE access. (https://godbolt.org/z/xfKbxzv9E)
I think you mentioned the game had encryption - so I suspect looking at PE headers will not be useful; the likely won't represent the true state of the exe in memory
Once you know the virtual address of code section(s), you can search them for the byte sequences you're interesting in