Passing Native Managed Boundaries with Virtual Function Calls

C++ programmers usually prefer to call virtual functions instead of using native function pointers. Virtual functions can be compiled to native code or managed code. The caller of a virtual function can either be a native caller or a managed caller. This leaves room for further interoperability scenarios: a native caller can call a managed virtual function and a managed caller can call a native virtual function. Figure 9-7 shows a native caller invoking a managed virtual function.


// SampleClass.h class SampleClass {


virtual void _thiscall Fl();

virtual void _stdcall F2();

#include "SampleClass.h

#include "SampleClass.h

SampleClass* p: p = new SampleClass;

#include "SampleClass.h

Figure 9-7. Calling managed virtual functions from native code

Since the caller of the virtual functions is a native caller, it simply picks a function pointer from a vtable to invoke the virtual function. When a virtual function is compiled to managed code, a pointer to the thunk must end up in the vtable. To create this thunk, the compiler produces .vtentry and .vtfixup metadata for the methods F1 and F2 of SampleClass:

.method assembly static void modopt([mscorlib]System.Runtime.CompilerServices.CallConvThiscall) SampleClass.F1(

valuetype SampleClass*

modopt([mscorlib]System.Runtime.CompilerServices.IsConst) modopt([mscorlib]System.Runtime.CompilerServices.IsConst) this ) cil managed

Was this article helpful?

0 0

Post a comment