Win32 Vs .Net  
Author Message
Anu Viswan





PostPosted: Common Language Runtime, Win32 Vs .Net Top

Hi

How does the OS differentiate between win32 and .Net exe what is the difference between the header structure




.NET Development35  
 
 
TaylorMichaelL





PostPosted: Common Language Runtime, Win32 Vs .Net Top

It doesn't. The EXEs are the same. The PE file format consists of a DOS stub program (read by DOS only) which displays the standard "You need Windows" message. Following this is the Win32 portion that allows the loader to run the program. For .NET apps the starting Win32 program is actually a stub (called the runtime invocation stub) that starts up the CLR host and then parses the CLR header and ultimately compiles and runs the main program entry point.

Now to be fair I said there is no difference and there isn't. The structures are the same. However the PE format is extensible such that new sections can be added later on as needed (several have been added over the years). For managed EXEs a new section exists called the CLR header. This header contains all the information needed to get the managed program running including things like the managed entry point, name signatures, manage resource information, version requirements, etc. It is this header that is parsed by the invocation stub to load the program and get it running. However the Win32 loader doesn't have any knowledge of this section nor uses it.

Therefore if you wanted to parse a PE file and determine whether it was managed or not you really only need to look and see if this particular section exists. At the end of the PE header is a fixed array of data dictionary entries. The 15th entry, if set, points to the CLR header. If this entry isn't zero then it is a managed binary. Of course you should probably do more validation if you really want to be sure but the DD entry will give you a good idea.

For a full discussion of the PE and CLR headers I recommend Inside Microsoft .NET IL Assembler by Serge Lidin. One of the first chapters in the book covers the PE file format and how managed binaries look.

Michael Taylor - 10/13/06