I am hunting for a means to fix call virtual function dynamicly. What this means is to aid function calls with offsets and dynamic types at runtime.

I am focusing on a wordpress plugin for a game title (Counter-Strike: Source, knowing it). The wordpress plugin interface is not likely which means you cannot extend around many people want. To understand an easy method to speak and manipulate the overall game, I support calling virtual functions. For instance, the CCSPlayer class has individuals techniques:

Offset    Name
201       CCSPlayer::Ignite()
205       CCSPlayer::CommitSuicide(bool Blah)
250       CCSPlayer::SomeFunctionWith2Params(int A1, float A2)

Passing these offsets and also the pointer towards the demonstration of these kinds to my current function (see below), I'm able to refer to this as functions:

CBasePlayer *pPlayer = Players.Find("mike");
bool bResult = Call2<bool, int, float>(210 /* offset */, pPlayer, 20, 50.0f);

I actually do this to call virtual functions I am unable to call with the normal routine since the compiler does not knows the dwelling from the derived CCSPlayer class.

And today I wish to give a scripting language with dynamic virtual function calls. Which means, the scripters can set the quantity of params, as well as what kind they're. Then passing the this-pointer and also the offset towards the function, to finally carry it out. This really is my real questions.

Presently I'm able to only hardcode individuals by utilizing templates and developing a function for each quantity of params. Example:

template<typename T, typename A, typename B> T Call2(int iOffset, void *pThis, A arg1, B arg2)
    void **pVTable = *(void***)pThis;
    void *pPointer = pVTable[_iOffset]; 

        T (CVCallEmpty::*pCall)(A, B);
        void *pAddress;
    } Data;

    Data.pAddress = pPointer;

    return (reinterpret_cast<CVCallEmpty*>(*(void***)&pThis)->*Data.pCall)(arg1, arg2);

Now, can there be any possible ways to support dynamic calls?

This really is so damaged, it's difficult to consider stretching it.

You do not handle virtual inheritance. You do not even handle multiple inheritance.

An online function can't be symbolized like a single "offset" integer. You have to pay a pointer-to-member argument.

As the second poster stated though, there's no native method of carrying out this in C/C++.

HOWEVER, you may be thinking about considering the FFCALL library, or even more particularly avcall. This library enables you to definitely build lists of parameters, after which call functions in it.

There is no built-in method to create the permutations. You could utilize some library, I imagine, however i recommend writing just a little program to create all of the templates for you personally. This provides you full charge of what source code is produced, there is no extraneous junk inside, and it's not hard to single-step.

One problem using the approach presented would be that the parameter types for that pointed-to operate is deduced in the arguments provided the parameter types should certainly be specified individually. Because it stands, you can (say) pass a float right into a function expecting an int, and obtain invalid results (at best). The caller must be very precise using their values! For those who have your source code producing program, you can tweak it slightly to create sending functions, like:

inline void call_vii(int vi,void *obj,int a0,int a1) {

inline float call_viff(int vi,void *obj,int a0,float a1,float a2) {

And so forth, ad infinitum (or otherwise remote).

This is not exactly terribly convenient, obviously. Also it creates lots of functions. But when you are likely to be subjecting this to some scripting language, you will need a function for every mixture of parameter types anyway...