🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Call virtual C++ methods by their index

Started by
2 comments, last by WitchLord 9 years, 6 months ago

Hi.

Is possible register object method by specify not pointer to method, but their index in vtable?

For example, I dynamically bind to OLE (COM) objects, and not have nor function address, nor index in vtable at compile time, and have it only at runtime.

I found in sources constant "ICC_VIRTUAL_THISCALL" and implementing of call by index in vtable in "CallSystemFunctionNative", but in all sources I was found only "case ICC_VIRTUAL_THISCALL:" and "if (something == ICC_VIRTUAL_THISCALL)", but none of "something=ICC_VIRTUAL_THISCALL".

And if it is possible, can I have "ICC_VIRTUAL_STDCALL" ?

In OLE objects, regardless of compiler, calling convention is STDCALL, obect pointer send as first arg, and first pointer in object is pointer to vtable.

Advertisement

When calling virtual methods the actual function address is looked up in the virtual function table. You register the virtual methods in exactly the same way as normal non-virtual class methods using the asCALL_THISCALL calling convention. AngelScript automatically detects if the method is virtual or non-virtual by inspecting the method pointer received in the RegisterObjectMethod call.

Even when dynamically loading COM objects you have the interface definition for the objects at compile time, so you should register the methods from that interface.

Or are you somehow dynamically determining the C++ interface too at runtime? If so, I imagine you could define a dummy interface with several virtual methods, one for each function table index. And then register the method pointers from that dummy interface instead, thus having a valid virtual method pointer.

Alternatively you could do something more hardcoded and construct the virtual method pointer manually. But then you'll have to make sure you the deal with the platform differences, as each compiler/platform can use different structures to represent virtual method pointers.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Thanks for reply.

I was found, automatic detect of pointer on virtual method worked in gcc, by checking one bit in pointer.

In MSVC, when passing pointer to virtual method, compiler create "thunk" for it and pointer seems like pointer to nonvirt method.

At compile time I do not know yet number of virt method, it known at runtime only.

Well, I will write dummy interface for it, thanks for the tip!

Let me know how it goes. And if you can share some code on how you dynamically determine the function and index it would be great. Perhaps I can add an article in the manual for how to register COM objects with the engine if you show me how it can be done.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

This topic is closed to new replies.

Advertisement