113

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.

Jämes
  • 6,945
  • 4
  • 40
  • 56
  • 3
    In fact, you **can** just send the 32 bits over with a starting and terminating signal. That would be writing your own marshalling for an int. But how would you pass a Dictionary> to C code? – Vinko Vrsalovic Feb 10 '10 at 22:29
  • 4
    Endianness comes to mind when you say "over the wire". – user7116 Feb 10 '10 at 22:46
  • true lets disregard big/little Endian or any other variation. –  Feb 10 '10 at 23:34
  • 62
    This is a bit of an odd question. It's rather like asking "why do we need a postal system, when we could simply have a system where letter carriers pick up correspondance, take it to central locations for sorting, and then deliver it to the addressees?" But... that's a postal system. You ask why we need a marshalling system when instead we could have... a marshalling system. I think I'm missing the point of your question. Can you clarify? – Eric Lippert Feb 11 '10 at 00:41
  • 1
    I think the best way to understand this is to understand how "methods" work in assembly - how the last instruction address is saved on the stack, parameters are passed through the stack, the stack pointer register is modified by the method, the instruction pointer register is used, and, in particular, how there could be slight variations in the different techniques for implementing methods. In effect, understanding "the wire" should shed some light on your question. – ylax Apr 19 '18 at 20:17

6 Answers6

88

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.

jason
  • 236,483
  • 35
  • 423
  • 525
35

.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.

Vojta
  • 1,060
  • 13
  • 11
13

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.

JSBձոգչ
  • 40,684
  • 18
  • 101
  • 169
  • 3
    Is Marshalling actually doing copy operations? I'm looking at real time image processing operations, and would prefer to not have to make copies of everything in memory. – ias Apr 15 '19 at 16:52
10

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.

Josh
  • 68,005
  • 14
  • 144
  • 156
8

Marshalling is a "medium" for want of a better word or a gateway, to communicate with the unmanaged world's data types and vice versa, by using the pinvoke, and ensures the data is returned back in a safe manner.

John Smith
  • 7,243
  • 6
  • 49
  • 61
t0mm13b
  • 34,087
  • 8
  • 78
  • 110
4

Marshalling is passing signature of a function to a different process which is on a different machine, and it is usually implemented by conversion of structured data to a dedicated format, which can be transferred to other processor systems (serialization / deserialization).

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480