4

What is the difference between typical Windows (WIn32? COM? Not sure what they are called.) DLLs and the DLLs that are compiled using .NET?

I know they are different, but I do not know the inner workings in detail.

I would love to know what the exact difference is.

EDIT: Trying to add more of my doubts here:

Why is it that I cannot add a reference to a Win32 DLL like I would add a reference for a .NET DLL? Also, why do we have to do a PInvoke? How do we have reflection and intellisense for .NET DLLs but not something similar for Win32 dlls?

pnuts
  • 58,317
  • 11
  • 87
  • 139
ashwnacharya
  • 14,601
  • 23
  • 89
  • 112
  • The "starting point" is the [PE header](http://en.wikipedia.org/wiki/Portable_Executable). Recent windows versions have special support for CLR so it knows how to load the IL. Then from there... where would you like to go? :) –  Jul 18 '11 at 04:58
  • Yes please. More of this. Also, why is it that I can add a reference to a .NET DLL but not a WIn32 DLL. Also, why do we have to do a PInvoke, also how we have reflection and intellisense for .NET DLLs but not something similar for Win32 dlls. Thanks for replying. – ashwnacharya Jul 18 '11 at 05:09
  • Reflection for the .NET DLL's would be possible because the .NET runtime can give information on the code (kind of like a virtual machine). – Prime Jul 18 '11 at 05:16
  • Look up the difference between "managed" and "unmanaged" code. This question is hopelessly broad. – Cody Gray - on strike Jul 18 '11 at 06:25

1 Answers1

2

Your question is way too broad to be answered fully, so I'll focus only on your first doubt.

A .NET binary is a "typical" Windows binary.

A .NET PE is a PE just like the others. It has an MZ header, a DOS segment, a PE header, a section table and sections: .text, .reloc, .rsrc. So far, everything is normal. A "typical" Windows binary contains all of these things, only the sections vary , depending on the compiler and language.

The .text table contains an import table that imports one single DLL, mscoree.dll, and one single function, _CorExeMain. It also contains the .NET section: data about how your program works (classes, methods, etc), metadata and IL.

So what happens when you start a .NET binary? Nothing really fancy. It simply works just like a "typical" Windows binary. It calls upon mscoree.dll to create a .NET runtime, that will load the .NET section of your executable. That runtime knows how to execute IL.

user703016
  • 37,307
  • 8
  • 87
  • 112
  • 1
    While your answer is correct, it has nothing to do with the question - since .Net binaries are not compiled the same as .Net DLL's which are not native\backward compatible (like .Net binaries) – sternr Jul 18 '11 at 07:15
  • @user4537: It looks like I misunderstood the question. However .NET DLLs behave like the executables, the difference being that the import is `_CorDllMain` instead of `_CorExeMain`. – user703016 Jul 18 '11 at 07:50
  • 1
    @user4537 .NET DLL's are certainly native AND backwards compatible. – Security Hound Jul 19 '11 at 12:09