View Single Post
  #1  
Old 10-21-2021, 16:34
Succubus
 
Posts: n/a
Lightbulb [C++] Calling any function dynamically without typedef

CODE

PHP Code:
template<typename DATA_TYPE, typename T, typename... PARAMS>
DATA_TYPE dynamic_call(T address, PARAMS... args)
{
    return ((
DATA_TYPE(__fastcall *)(...))reinterpret_cast<void*>(address))(args...);
} 
USAGE

PHP Code:
auto sum = dynamic_call<int>(0xdeadbeef, 1, 2);
printf("\nsum: %d\n", sum); 
MORE INFO

PHP Code:
// <int> = return type 
// 1st param = target address
// 2nd param = arguments
// return type can be anything like <const char*> <char*> etc 
OTHER

This is kinda useful if you don't like to write typedef at all or if you have ida pro and u want to copy the source code of the whole function without typing the typedef of each call inside that function.

Sometimes the function source code in IDA PRO got weird calls like the first param is a 'this' and it does this + any dword. and it seems like ida treat it as a function like

v31 = *(__int64* (this + 0x31))(v1);

something or similar like that.. you can now just do that also

PHP Code:
// xD well you cant call it this because thats reserve
// I asume you have like void* _this as your first param for example                    
auto set_hp = dynamic_call<__int64*>(_this + 0x31, 100);
printf("\ntest: %d\n", set_hp); 
This help also to make your source cleaner, less typedef for those functions you really don't care about and you just wanna try it or experiment with it.

OTHER USAGE

I also uses this on hook

PHP Code:
DWORD64 osample;

zend_op_array* __fastcall sample(__int64 a1, DWORD *a2, int *a3) {

    
auto ok = dynamic_call<zend_op_array*>(osample, a1, a2, a3);

    return 
ok;
}

....
MH_CreateHook(reinterpret_cast<void*>(0xdeadbeef), &sample, reinterpret_cast<void**>(&osample));
MH_EnableHook(reinterpret_cast<void*>(0xdeadbeef)); 
no need typedef

Last edited by Succubus; 10-21-2021 at 17:07. Reason: added more example
Reply With Quote
The Following 5 Users Say Thank You to For This Useful Post:
b30wulf (10-22-2021), Fyyre (10-27-2021), niculaita (10-22-2021), tonyweb (09-08-2022)