Marshaling is the process of creating a bridge between managed code and unmanaged code; it is the homer that carries messages from the managed to the unmanaged environment and reverse. It is one of the core services offered by the CLR (Common Language Runtime.)
1 : to place in proper rank or position marshaling the troops. 2 : to bring together and order in an appropriate or effective way marshal arguments marshaled her thoughts before answering the question. 3 : to lead ceremoniously or solicitously : usher marshaling her little group of children down the street.
Marshalling is the process of transforming types when they need to cross between managed and native code. Marshalling is needed because the types in the managed and unmanaged code are different.
Usually the word "marshalling" is used when you're crossing some sort of boundary. Three obvious use cases: Remoting: the RPC data is marshalled to a separate machine (usually) AppDomains: an object crossing an AppDomain boundary needs to be marshalled (or it may be marshalled by reference)
Because different languages and environments have different calling conventions, different layout conventions, different sizes of primitives (cf. char
in C# and char
in C), different object creation/destruction conventions, and different design guidelines. You need a way to get the stuff out of managed land an into somewhere where unmanaged land can see and understand it and vice versa. That's what marshalling is for.
.NET code(C#, VB) is called "managed" because it's "managed" by CLR (Common Language Runtime)
If you write code in C or C++ or assembler it is all called "unmanaged", since no CLR is involved. You are responsible for all memory allocation/de-allocation.
Marshaling is the process between managed code and unmanaged code; It is one of the most important services offered by the CLR.
Marshalling an int
is ideally just what you said: copying the memory from the CLR's managed stack into someplace where the C code can see it. Marshalling strings, objects, arrays, and other types are the difficult things.
But the P/Invoke interop layer takes care of almost all of these things for you.
As Vinko says in the comments, you can pass primitive types without any special marshalling. These are called "blittable" types and include types like byte, short, int, long, etc and their unsigned counterparts.
This page contains the list of blittable and non-blittable types.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With