Home > database >  64 bit equivalent of "GetModuleHandleA" need
64 bit equivalent of "GetModuleHandleA" need

Time:06-24

I need to extent the compatibility of my application to 64 bit exe.

__int64 GetGameFunctionAddress(std::string GameFileExe, std::string Address)
  {
    // Get integer value address of the original function hook

    #if defined(_WIN64)

        /// code to emulate GetModuleHandleA on 64 executible

    #else

        return (__int64)GetModuleHandleA(GameFileExe.c_str())   std::strtoul(Address.c_str(), NULL, 16);
    
    #end if
    
}

this function get Address to pass to IDA for hook a function of a game.

I call this function with this line:

Output.MainFunctHookAddressInt = GetGameFunctionAddress(Output.ExeFile, GameInfo.MainFunctHookAddressInt);

AddressOfHookSoundFunction = Output.MainFunctHookAddressInt;

and in a second time I pass it to detours:

DetourAttach(&(LPVOID&)AddressOfHookSoundFunction, HookMainFunction);

Unfortunately "GetModuleHandleA" work only on 32 bit games, but I need to extend the compatibility to 64 bit games too.

So I need to fix my 'GetGameFunctionAddress' function to add 64 bit compatibility.

Can you help me please ?

Update:

One user tell me:

According to this GetModuleHandleA does not work to get the base address if the process is 64bit. Why does getting the base address using GetModuleHandle work?

CodePudding user response:

GetModuleHandleW returns a value of type HMODULE (which is the same as HINSTANCE, aka HANDLE, aka PVOID, aka void*). In other words: It returns a pointer sized value.

Pointer sized values are 32 bits wide in 32-bit processes, and 64 bits wide in 64-bit processes. Either way you get the address of the module base address, irrespective of the bitness of the process.

Now obviously, since you are interacting with filesystem objects that aren't under your control, you do not want to call the ANSI version of the API (GetModuleHandleA) but the Unicode version: GetModuleHandleW. And while you're doing pretty low-level stuff here, you probably don't want to use types from the C Standard Library either (if you insist, use std::wstring/std::wstring_view).

  • Related