I want to write an auto for gaming. I use Cheat Engine and found the following Assembly code:
push 014656DC
call 00633940
add esp,04
ret
I inject this code into the app using Cheat Engine and it always worked.
Now, I want to use C++ code so I wrote a DLL:
- header file:
#ifdef DIVLIB_EXPORTS
#define DIVLIB_API __declspec(dllexport)
#else
#define DIVLIB_API __declspec(dllimport)
#endif
extern "C" {
DIVLIB_API void ClickID();
}
- source file:
void ClickID()
{
_asm {
push esi;
push eax;
mov esi, 0x014656DC;
mov eax, 0x00633940;
push esi;
call eax;
add esp, 0x04;
}
}
And I have the Main.exe
to call this DLL
auto h_process = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
char dll[] = "D:\\project\\DivHook\\divhook\\Debug\\divlib.dll";
HMODULE hinstDLL = LoadLibraryA(dll);
LPVOID LoadLibAddress = (LPVOID)GetProcAddress(hinstDLL, "ClickID");
LPVOID MemAlloc = (LPVOID)VirtualAllocEx(h_process, NULL, strlen(dll)+1, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
WriteProcessMemory(h_process, (LPVOID)MemAlloc, dll, strlen(dll) + 1, NULL);
CreateRemoteThread(h_process, NULL, NULL, (LPTHREAD_START_ROUTINE)LoadLibAddress, (LPVOID)MemAlloc, NULL, NULL);
CloseHandle(h_process);
VirtualFreeEx(h_process, (LPVOID)MemAlloc, 0, MEM_RELEASE | MEM_DECOMMIT);
However, my game always crashes when Main.exe
is run.
I have tried the above Assembly over and over with the Cheat Engine and made sure it works perfectly, but C++ code always crashes.