Pushok

vtable utils

Aug 8th, 2016
836
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <cstdarg>
  3. #include <type_traits>
  4.  
  5. class CLLTools {
  6. public:
  7.     template<class D, class T>
  8.     static D CastRawMethodPtr(T func){
  9.         void* temp = *reinterpret_cast<void**>(&func);
  10.         return *reinterpret_cast<D*>(&temp);
  11.     }
  12.  
  13.     template<class T> static
  14.     typename std::enable_if<std::is_same<T, void>::value, T>::type
  15.     VirtualMethodCall(void* object, int index, int cargs = 0, ...) {
  16.         void** anyArgs = new void*[cargs];
  17.         va_list args;
  18.        
  19.         va_start(args, args);
  20.         for (int i = 0; i < cargs; i++){
  21.             anyArgs[cargs - i - 1] = va_arg(args, void*);
  22.         }
  23.         va_end(args);
  24.        
  25.         for (int i = 0; i < cargs; i++){
  26.             void* t = anyArgs[i];
  27.             __asm push t;
  28.         }
  29.        
  30.         void* method = ((void*)((*(void***)object)[index]));;
  31.         __asm{
  32.             push object;
  33.             pop ecx;
  34.             call method;
  35.         }
  36.     }
  37.    
  38.     //Not finished yet
  39.     template<class T> static
  40.     T VirtualMethodCall(void* object, int index, int cargs = 0, ...) {
  41.         void** anyArgs = new void*[cargs];
  42.         va_list args;
  43.         T result();
  44.        
  45.         va_start(args, args);
  46.         for (int i = 0; i < cargs; i++){
  47.             anyArgs[cargs - i - 1] = va_arg(args, void*);
  48.         }
  49.         va_end(args);
  50.        
  51.         for (int i = 0; i < cargs; i++){
  52.             void* t = anyArgs[i];
  53.             __asm push t;
  54.         }
  55.        
  56.         void* method = ((void*)((*(void***)object)[index]));;
  57.         __asm{
  58.             push object;
  59.             pop ecx;
  60.             call method;
  61.         }
  62.        
  63.         if(std::is_integral<T>::value && sizeof(T) <= 4){
  64.             __asm mov result, eax;
  65.         } else if(std::is_class<T>::value) { //TODO
  66.             return T();
  67.         }
  68.        
  69.         return result;
  70.     }
  71.    
  72.     template<class T, class M> static
  73.     typename std::enable_if<std::is_same<T, void>::value, T>::type
  74.     ClassMethodCall(void* object, M method, int cargs = 0, ... ) {
  75.         void* temp = *reinterpret_cast<void**>(&method);
  76.         void** anyArgs = new void*[cargs];
  77.         va_list args;
  78.         T result();
  79.        
  80.         va_start(args, args);
  81.         for (int i = 0; i < cargs; i++){
  82.             anyArgs[cargs - i - 1] = va_arg(args, void*);
  83.         }
  84.         va_end(args);
  85.        
  86.         for (int i = 0; i < cargs; i++){
  87.             void* t = anyArgs[i];
  88.             __asm push t;
  89.         }
  90.        
  91.         __asm{
  92.             push object;
  93.             pop ecx;
  94.             call dword ptr [temp];
  95.         }
  96.     }
  97. };
Advertisement