Click here to Skip to main content
15,124,736 members
Articles / Desktop Programming / Win32
Technical Blog
Posted 17 Mar 2010

Tagged as


98 bookmarked

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


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


About the Author

Mohammad Elsheimy
Software Developer (Senior)
Egypt Egypt
Mohammad Elsheimy is a developer, trainer, and technical writer. He is a MCP, MCTS (WinForms), MCPD (Windows Apps), MCSA (SQL Server), MCSE (Data Analytics), and MCT expertized in .NET Framework technologies, data management and analytics. He is also a Project Management Professional (PMP) and a Quranic Readings Institute (Al-Azhar) graduate specialized in Quranic readings, Islamic legislation, and the Arabic language.

Comments and Discussions

QuestionDWORD etc. are not present in managed type, Isn't that misleading Pin
Prashant Choudhary6-Sep-15 1:51
MemberPrashant Choudhary6-Sep-15 1:51 
QuestionHello, pics in your artical need loging to access, could'u plz fix them? Pin
shinkamui23-Jan-14 22:31
Membershinkamui23-Jan-14 22:31 
AnswerRe: Hello, pics in your artical need loging to access, could'u plz fix them? Pin
kladess24-Apr-14 15:35
Memberkladess24-Apr-14 15:35 
GeneralRe: Hello, pics in your artical need loging to access, could'u plz fix them? Pin
shinkamui20-Aug-14 0:52
Membershinkamui20-Aug-14 0:52 
QuestionHelpful Pin
Silvabolt13-Nov-13 6:17
MemberSilvabolt13-Nov-13 6:17 
GeneralMy vote of 5 Pin
prosthor27-May-13 1:44
Memberprosthor27-May-13 1:44 
QuestionHave a look at the definition of marshalling -- Your first line! Pin
Sharjith8-Sep-11 11:48
professionalSharjith8-Sep-11 11:48 
GeneralMy vote of 3 Pin
R. Hoffmann4-Jan-11 9:25
professionalR. Hoffmann4-Jan-11 9:25 
GeneralMy vote of 1 Pin
rm82221-Apr-10 4:08
Memberrm82221-Apr-10 4:08 
GeneralRe: My vote of 1 Pin
Mohammad Elsheimy12-May-10 3:02
MemberMohammad Elsheimy12-May-10 3:02 
GeneralRe: My vote of 1 Pin
mla15426-Mar-13 11:10
Membermla15426-Mar-13 11:10 
GeneralMy vote of 1 Pin
Nathaniel.Bank29-Mar-10 20:17
MemberNathaniel.Bank29-Mar-10 20:17 
GeneralRe: My vote of 1 Pin
acerbr29-Mar-10 22:57
Memberacerbr29-Mar-10 22:57 
GeneralRe: My vote of 1 Pin
Mohammad Elsheimy30-Mar-10 8:15
MemberMohammad Elsheimy30-Mar-10 8: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.