This topic has not yet been rated - Rate this topic

Marshaling Blittable Types

Visual Studio .NET 2003

Blittable types are data types whose in-memory representation is identical in managed and unmanaged code. When used between managed and unmanaged code, they require very little overhead, providing a performance benefit, and do not require marshaling, providing ease of interoperability. All other types require marshaling when working between managed and unmanaged code.

When managed code calls into unmanaged functions, a thunk is required to perform the transition. For clients that do not use Managed Extensions, you must provide wrapper classes or PInvoke declarations to insert the appropriate thunks or wrapper classes. For clients that use Managed Extensions, you include the desired header file and link to the appropriate library. The compiler then provides the appropriate thunks as necessary.

The following code example exposes the PickANumber function with PInvoke and then uses the function.

#using <mscorlib.dll>
using namespace System;
using namespace System::Runtime::InteropServices;
public __value struct TraditionalDLL
{
   [DllImport("TraditionalDLL.dll")]
   static public int PickANumber();
};
int main()
{
   int number;
   number = TraditionalDLL::PickANumber();
   Console::WriteLine("You picked {0}",__box(number));
   return 0;
}

Go to the next step | Go back to the last step

See Also

Managed Extensions for C++ and Data Marshaling Tutorial

Did you find this helpful?
(1500 characters remaining)
© 2013 Microsoft. All rights reserved.