1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
#include <cstring>
struct FUNCTION{
void* func;
char* arg;
int arg_sz;
void set(void* f, int arg_sz, ...);
void execute();
};
void addd(double a, double b, double& c){
c = a + b;
}
void addi(int a, int b, int& c){
c = a + b;
}
void FUNCTION::set(void* f, int in_arg_sz, ...){
func = f;
char* ptr = (char*)(&in_arg_sz+1);
arg_sz = in_arg_sz;
arg = new char[arg_sz];
memcpy(arg, ptr, arg_sz);
}
void FUNCTION::execute(){
void* cptr;
__asm{
sub esp, dword ptr[arg_sz]
mov dword ptr[cptr], esp
}
memcpy(cptr, arg, arg_sz);
void* F = func;
_asm{
call dword ptr[F]
add esp, dword ptr[arg_sz]
}
}
int main(){
double cd;
int ci;
FUNCTION fd, fi;
fd.set(addd, 8+8+4, 5.0, 3.0, &cd);
fi.set(addi, 4+4+4, 5, 3, &ci);
fd.execute();
fi.execute();
return 0;
}
|