Click here to Skip to main content
Click here to Skip to main content

Marshaling with C# – Chapter 1: Introducing Marshaling

By , 28 Dec 2010
 
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)

About the Author

Mohammad Elsheimy
Technical Writer Just Like a Magic
Egypt Egypt
Member
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:

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionHave a look at the definition of marshalling -- Your first line!memberSharjith8 Sep '11 - 10:48 
http://en.wikipedia.org/wiki/Marshalling_(computer_science)[^]
RegardsSmile | :)
N. Sharjith

GeneralMy vote of 3memberR. Hoffmann4 Jan '11 - 8:25 
This article is a bit short, perhaps it could have been incorporated into chapter 2? The subsequent chapters are really informative though, and more than worth their 5!
GeneralMy vote of 1memberrm82221 Apr '10 - 3:08 
0
GeneralRe: My vote of 1memberMohammad Elsheimy12 May '10 - 2:02 
thx Smile | :)
Regards,
Mohammad Elsheimy
Technology evangelist from Egypt born in 1991
http://JustLikeAMagic.Wordpress.com

GeneralRe: My vote of 1membermla15426 Mar '13 - 10:10 
Your comment is not helpful. Frown | :(
Regards,
Mike

GeneralMy vote of 1memberNathaniel.Bank29 Mar '10 - 19:17 
Difficult to read, very little useful information, no examples. Contains a reference to a Figure 1, yet there is no figure one. What's the point of this article??
GeneralRe: My vote of 1memberacerbr29 Mar '10 - 21:57 
Hi,
I think this is a good starting article for beginners. Short, concise and essential: with a few words it makes the marshaling concept very clear for everyone.
Furthermore it is only a 'Chapter 1': usually, in books, chapter 1 is almost useless, and contains many useless words...
PS: Nathaniel, the figure 1 can be seen in the author's blog (link on top of article)
GeneralRe: My vote of 1memberMohammad Elsheimy30 Mar '10 - 7:15 
I'm sorry, It's my fault. I haven't referenced other chapters.
 
Updates, thanks for your feedbacks.
Regards,
Mohammad Elsheimy
 
---------------------------
Just Like a Magic
http://JustLikeAMagic.Wordpress.com

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

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