Marshaling – what is it and why do we need it?

george9170 picture george9170 · Feb 10, 2010 · Viewed 54.3k times · Source

What is marshalling and why do we need it?

I find it hard to believe that I cannot send an int over the wire from C# to C and have to marshall it. Why can't C# just send the 32 bits over with a starting and terminating signal, telling C code that it has received an int?

If there are any good tutorials or sites about why we need marshalling and how to use it, that would be great.

Answer

jason picture jason · Feb 10, 2010

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.