Click here to Skip to main content
13,864,351 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


94 bookmarked
Posted 17 Mar 2010
Licenced CPL

Marshaling with C# – Chapter 1: Introducing Marshaling

, 28 Dec 2010
Rate this:
Please Sign up or sign in to vote.
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
Technical Writer Just Like a Magic
Egypt Egypt
Independent technical writer from Egypt born in 1991.

I am a student of The Holy Quranic Sciences Institute. There, we study the Holy Quranic sciences and Islamic legislation.

I currently run two sites:

You may also be interested in...

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05 | 2.8.190214.1 | Last Updated 28 Dec 2010
Article Copyright 2010 by Mohammad Elsheimy
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid