I have an assignment for university which requires me to access the vtable of a class. I need to write a function (here called pb) which takes a pointer to an object as an argument as well as an integer, and then just prints the output of the methods of the class. I have managed to access the first function, but I don't know how to access the second function. Here's the code I have so far:
typedef int(*firstFun)();
typedef int(*secondFun)(int);
class B {
public:
virtual int __cdecl first() = 0;
virtual int __cdecl second(int) = 0;
};
class D : public B {
public:
virtual int __cdecl first() { return 42; }
virtual int __cdecl second(int x) {
return first() + x; }
};
void pb(B* object, int x) {
unsigned int adressVTable = *(unsigned int*)object;
unsigned int adressVTable2; //yet unknown
firstFun bFirst = (firstFun)(*(unsigned int*)(adressVTable));
secondFun bSecond = (secondFun)(*(unsigned int*)(int)(adressVTable2));
int f=bFirst();
int s=bSecond(x);
printf("First: %d, second: %d", f, s);
}
In conclusion, how to get bSecond to work for second(int) like bFirst works for first()?