I made a test program and made a function that just prints some text, and I am trying to call it from a DLL by reversing it using IDA/Ghidra and getting a pointer to it.
I thought IDA was giving the wrong address so I checked with Ghidra and I got the same address...
Here is my code to call the function from my DLL
#include <iostream>
void coolRoutine() {
printf("starting...\n");
void(*ofunct)() = (void(__cdecl *)())(0x00401000);
printf("got funct!\n");
ofunct();
printf("done!\n");
}
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
coolRoutine();
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
The program prints "starting..." and "got funct!" but does not call the original function.
I have tried looking at several posts and I can't figure out what I am doing wrong as someone else did something similar and it worked for them.
Update: as someone suggested, I have tried adding the base address to the function pointer however I have ended up with the same result.
Here is what I tried:
void coolRoutine() {
printf("starting...\n");
uintptr_t baseAddress = (uintptr_t)GetModuleHandle(0);
std::cout << "Base Address: " << baseAddress << std::endl;
void(*ofunct)() = (void(__cdecl *)())(baseAddress + 0x00401000);
printf("got funct!\n");
ofunct();
printf("done!\n");
}
It is getting the base address correctly (or atleast I think it is, since it isn't null), but it isn't executing ofunct and print "done!".