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

C#.NetUnmanagedMarshallingManaged

C# Problem Overview


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.

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

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

Solution 3 - C#

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.

Solution 4 - C#

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.

Solution 5 - C#

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.

Solution 6 - C#

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiongeorge9170View Question on Stackoverflow
Solution 1 - C#jasonView Answer on Stackoverflow
Solution 2 - C#VojtaView Answer on Stackoverflow
Solution 3 - C#JSBձոգչView Answer on Stackoverflow
Solution 4 - C#JoshView Answer on Stackoverflow
Solution 5 - C#t0mm13bView Answer on Stackoverflow
Solution 6 - C#Maryam SheikhView Answer on Stackoverflow