Marshaling in C#

Why 

.NET code(C#, VB) is called "managed" because it's "managed" by CLR (Common Language Runtime)

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.


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.

How


Marshaling is the process between managed code and unmanaged code; It is one of the most important services offered by the CLR.

Marshalling  int : 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.



Comments

Popular Posts