Thunk (compatibility mapping)

As a thunk is called in the jargon of software development calling code belongs to another platform or another framework. When converting from 16 to 32 bits, for example, the operating systems (OS / 2, Windows NT, etc.) were able to achieve 16-bit code by reacting the corresponding call parameters and addresses, so that 16 bit programs could still be used. In the modern software development is a thunk eg the call to native code from managed code out and vice versa (see Java Native Access or. NET P / Invoke). It is a platform transition ( transition), in which the calling conventions and / or transfer parameters must be implemented accordingly ( marshalling). . C / CLI programming language from the NET Framework from Microsoft has been specifically designed to enable such thunks in both directions:

Managed - Unmanaged call

Given a native C class example in a C project or as part of a C / CLI project, which is subsequently used by managed code:

Public class CNativeClass {    private:      int m_i;    public:      void SetValue ( int i )      {        m_i = i;      } }; Managed C / CLI class (which can be directly instantiated in the form of for example C #) which uses the native class shown earlier:

Public ref class CManagedClass {    public:      CManagedClass ()      {        System :: Int32 i = 42;        CNativeClass * pNativeClass = new CNativeClass ();        pNativeClass -> SetValue ( i ) ;/ / implementation of the data type        delete pNativeClass;      } }; Unmanaged Managed call

Managed C / CLI class:

Public ref class CManagedClass {    private:      System :: Int32 m_i;    public:      void SetValue ( int i )      {        m_i = i ;/ / implementation of the data type      } }; Native C class in a C / CLI project. Here it is seen that the reverse is also possible, namely the instantiation of managed code within an unmanaged class. The condition, however, that it is a C / CLI project, so that the compiler understands the appropriate syntax. The thunk already occurs in the gcnew statement because here the constructor of the managed class is called:

Public class CNativeClass {    public:      void Foo ()      {        int i = 42;        CManagedClass ^ pManagedClass = gcnew CManagedClass ();        pManagedClass -> SetValue ( i);      } }; literature

  • Marcus Heege: Expert C / CLI. Apress Publishing, Berkeley, 2007, ISBN 978-1-59059-756-9, Chapter 9, starting on page 203
  • Programming
774362
de