Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdarg>
- #include <type_traits>
- class CLLTools {
- public:
- template<class D, class T>
- static D CastRawMethodPtr(T func){
- void* temp = *reinterpret_cast<void**>(&func);
- return *reinterpret_cast<D*>(&temp);
- }
- template<class T> static
- typename std::enable_if<std::is_same<T, void>::value, T>::type
- VirtualMethodCall(void* object, int index, int cargs = 0, ...) {
- void** anyArgs = new void*[cargs];
- va_list args;
- va_start(args, args);
- for (int i = 0; i < cargs; i++){
- anyArgs[cargs - i - 1] = va_arg(args, void*);
- }
- va_end(args);
- for (int i = 0; i < cargs; i++){
- void* t = anyArgs[i];
- __asm push t;
- }
- void* method = ((void*)((*(void***)object)[index]));;
- __asm{
- push object;
- pop ecx;
- call method;
- }
- }
- //Not finished yet
- template<class T> static
- T VirtualMethodCall(void* object, int index, int cargs = 0, ...) {
- void** anyArgs = new void*[cargs];
- va_list args;
- T result();
- va_start(args, args);
- for (int i = 0; i < cargs; i++){
- anyArgs[cargs - i - 1] = va_arg(args, void*);
- }
- va_end(args);
- for (int i = 0; i < cargs; i++){
- void* t = anyArgs[i];
- __asm push t;
- }
- void* method = ((void*)((*(void***)object)[index]));;
- __asm{
- push object;
- pop ecx;
- call method;
- }
- if(std::is_integral<T>::value && sizeof(T) <= 4){
- __asm mov result, eax;
- } else if(std::is_class<T>::value) { //TODO
- return T();
- }
- return result;
- }
- template<class T, class M> static
- typename std::enable_if<std::is_same<T, void>::value, T>::type
- ClassMethodCall(void* object, M method, int cargs = 0, ... ) {
- void* temp = *reinterpret_cast<void**>(&method);
- void** anyArgs = new void*[cargs];
- va_list args;
- T result();
- va_start(args, args);
- for (int i = 0; i < cargs; i++){
- anyArgs[cargs - i - 1] = va_arg(args, void*);
- }
- va_end(args);
- for (int i = 0; i < cargs; i++){
- void* t = anyArgs[i];
- __asm push t;
- }
- __asm{
- push object;
- pop ecx;
- call dword ptr [temp];
- }
- }
- };
Advertisement