Vtentry 1 1

// IL code elided for clarity here

Figure 9-8 shows the opposite direction of managed-unmanaged transitions and virtual function calls. Here, a managed caller (main) invokes a native virtual function (F2).


// compiled w/o /clr #include "SampleClass.h"

// SampleClass.h

class SampleClass



virtual void

thiscall Fl();

virtual void


// compiled without /clr #include "SampleClass.h"

rvoid _thiscall SampleClass::Fl()

â–ºvoid _stdcall SampleClass::F2()

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

To call the native virtual function F2, the managed caller uses the IL instructions for virtual memory access discussed in Chapter 8 to determine the address to the virtual function pointer from the vtable. Once the virtual function pointer is obtained, the function can be called via the CALLI instruction. For the call p->F2(), the compiler emits the following IL code:

ldloc p // Push SampleClass pointer on the stack

(later used as this argument for call)

ldloc p // Push SampleClass pointer on the stack to get vtable pointer ldind.i4 // Retrieve vtable pointer ldc.i4 4 // F2 is second virtual function => offset in vtable is 4

add // add this offset to vtable pointer to determine address

// at which the virtual function pointer can be found ldind.i4 // load virtual function pointer from address

// invoke virtual function via its pointer calli unmanaged stdcall void modopt(System.Runtime.CompilerServices.CallConvStdcall)(native int)

Was this article helpful?

0 0

Post a comment