Click here to Skip to main content
15,867,308 members
Articles / Desktop Programming / Win32

Marshaling with C# – Chapter 1: Introducing Marshaling

Rate me:
Please Sign up or sign in to vote.
4.73/5 (35 votes)
28 Dec 2010CPL2 min read 217K   98   14
Introduction to marshaling

What is Marshaling?

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

Because much of the types in unmanaged environment do not have counterparts in managed environment, you need to create conversion routines that convert the managed types into unmanaged and vice versa; and that is the marshaling process.

As a refresher, we call .NET code “managed” because it is controlled (managed) by the CLR. Other code that is not controlled by the CLR is called unmanaged.

Why Marshaling?

You already know that there is no such compatibility between managed and unmanaged environments. In other words, .NET does not contain such types HRESULT, DWORD, and HANDLE that exist in the realm of unmanaged code. Therefore, you need to find a .NET substitute or create your own if needed. That is what called marshaling.

An example is the unmanaged DWORD; it is an unsigned 32-bit integer, so we can marshal it in .NET as System.UInt32. Therefore, System.UInt32 is a substitute for the unmanaged DWORD. On the other hand, unmanaged compound types (structures, unions, etc.) do not have counterparts or substitutes in the managed environment. Thus, you’ll need to create your own managed types (structures/classes) that will serve as the substitutes for the unmanaged types you use.

When I Need to Marshal?

Marshaling comes in handy when you are working with unmanaged code, whether you are working with Windows API or COM components. It helps you interoperating (i.e. working) correctly with these environments by providing a way to share data between the two environments. Figure 1 shows the marshaling process, where it falls, and how it is required in the communication process between the two environments.

License

This article, along with any associated source code and files, is licensed under The Common Public License Version 1.0 (CPL)


Written By
Technical Lead
Egypt Egypt
Mohammad Elsheimy is a developer, trainer, and technical writer currently hired by one of the leading fintech companies in Middle East, as a technical lead.

Mohammad is a MCP, MCTS, MCPD, MCSA, MCSE, and MCT expertized in Microsoft technologies, data management, analytics, Azure and DevOps solutions. He is also a Project Management Professional (PMP) and a Quranic Readings college (Al-Azhar) graduate specialized in Quranic readings, Islamic legislation, and the Arabic language.

Mohammad was born in Egypt. He loves his machine and his code more than anything else!

Currently, Mohammad runs two blogs: "Just Like [a] Magic" (http://JustLikeAMagic.com) and "مع الدوت نت" (http://WithdDotNet.net), both dedicated for programming and Microsoft technologies.

You can reach Mohammad at elsheimy[at]live[dot]com

Comments and Discussions

 
QuestionDWORD etc. are not present in managed type, Isn't that misleading Pin
Prashant Choudhary6-Sep-15 0:51
Prashant Choudhary6-Sep-15 0:51 
QuestionHello, pics in your artical need loging to access, could'u plz fix them? Pin
shinkamui23-Jan-14 21:31
shinkamui23-Jan-14 21:31 
AnswerRe: Hello, pics in your artical need loging to access, could'u plz fix them? Pin
kladess24-Apr-14 14:35
kladess24-Apr-14 14:35 
GeneralRe: Hello, pics in your artical need loging to access, could'u plz fix them? Pin
shinkamui19-Aug-14 23:52
shinkamui19-Aug-14 23:52 
QuestionHelpful Pin
Silvabolt13-Nov-13 5:17
Silvabolt13-Nov-13 5:17 
GeneralMy vote of 5 Pin
prosthor27-May-13 0:44
prosthor27-May-13 0:44 
QuestionHave a look at the definition of marshalling -- Your first line! Pin
Sharjith8-Sep-11 10:48
professionalSharjith8-Sep-11 10:48 
GeneralMy vote of 3 Pin
R. Hoffmann4-Jan-11 8:25
professionalR. Hoffmann4-Jan-11 8:25 
GeneralMy vote of 1 Pin
rm82221-Apr-10 3:08
rm82221-Apr-10 3:08 
GeneralRe: My vote of 1 Pin
Mohammad Elsheimy12-May-10 2:02
Mohammad Elsheimy12-May-10 2:02 
GeneralRe: My vote of 1 Pin
mla15426-Mar-13 10:10
mla15426-Mar-13 10:10 
GeneralMy vote of 1 Pin
Nathaniel.Bank29-Mar-10 19:17
Nathaniel.Bank29-Mar-10 19:17 
GeneralRe: My vote of 1 Pin
acerbr29-Mar-10 21:57
acerbr29-Mar-10 21:57 
GeneralRe: My vote of 1 Pin
Mohammad Elsheimy30-Mar-10 7:15
Mohammad Elsheimy30-Mar-10 7:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.