Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Marshaling with C# – Chapter 1: Introducing Marshaling

, 28 Dec 2010 CPL
Rate this:
Please Sign up or sign in to vote.
Introduction to marshaling.
Download PDF and XPS versions of the book here.
Chapter 1: Introducing Marshaling 
Chapter 2: Marshaling Simple Types 
Chapter 3: Marshaling Compound Types 

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 the 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 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 fall, and how it is required in the communication process between the two environments. 

Download PDF and XPS versions of the book here.
Chapter 1: Introducing Marshaling
Chapter 2: Marshaling Simple Types
Chapter 3: Marshaling Compound Types

License

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

Share

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:
Follow on   Twitter

Comments and Discussions

 
QuestionHello, pics in your artical need loging to access, could'u plz fix them? Pinmembershinkamui23-Jan-14 22:31 
AnswerRe: Hello, pics in your artical need loging to access, could'u plz fix them? Pinmemberkladess24-Apr-14 15:35 
GeneralRe: Hello, pics in your artical need loging to access, could'u plz fix them? Pinmembershinkamui20-Aug-14 0:52 
QuestionHelpful PinmemberSilvabolt13-Nov-13 6:17 
GeneralMy vote of 5 Pinmemberprosthor27-May-13 1:44 
QuestionHave a look at the definition of marshalling -- Your first line! PinmemberSharjith8-Sep-11 11:48 
http://en.wikipedia.org/wiki/Marshalling_(computer_science)[^]
RegardsSmile | :)
N. Sharjith

GeneralMy vote of 3 PinmemberR. Hoffmann4-Jan-11 9:25 
GeneralMy vote of 1 Pinmemberrm82221-Apr-10 4:08 
GeneralRe: My vote of 1 PinmemberMohammad Elsheimy12-May-10 3:02 
GeneralRe: My vote of 1 Pinmembermla15426-Mar-13 11:10 
GeneralMy vote of 1 PinmemberNathaniel.Bank29-Mar-10 20:17 
GeneralRe: My vote of 1 Pinmemberacerbr29-Mar-10 22:57 
GeneralRe: My vote of 1 PinmemberMohammad Elsheimy30-Mar-10 8:15 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 28 Dec 2010
Article Copyright 2010 by Mohammad Elsheimy
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid