title: What is in a DLL and how does it work?
body: <p>I'm always referencing DLLs in my C# code, but they have remained somewhat of a mystery which I would like to clarify. This is a sort of brain dump of questions regarding DLLs.</p>
  
  <p>I understand a DLL is a dynamically linked library which means that another program can access this library at run time to get "functionality". However, consider the following ASP.NET project with <code>Web.dll</code> and <code>Business.dll</code> (<code>Web.dll</code> is the front end functionality and it references <code>Business.dll</code> for types and methods).</p>
  
  <ol>
  <li><p>At what point does <code>Web.dll</code> dynamically link to <code>Business.dll</code>? You notice a lot in Windows HDD thrashing for seemingly small tasks when using Word (etc.) and I reckon that Word is going off and dynamically linking in functionality from other DLLs?</p>
  
  <p>1a. Additionally, what loads and links the DLL - the OS or some run time framework such as the .NET framework?</p>
  
  <p>1b. What is the process of "linking"?  Are compatibility checks made? Loading into the same memory?  What does linking actually mean?</p></li>
  <li><p>What actually executes the code in the DLL? Does it get executed by the processor or is there another stage of translation or compilation before the processor will understand the code inside the DLL?</p>
  
  <p>2a. In the case of a DLL built in C# .NET, what is running this: the .NET framework or the operating system directly?</p></li>
  <li><p>Does a DLL from Linux work on a Windows system (if such a thing exists), or are they operating system specific?</p></li>
  <li><p>Are DLLs specific to a particular framework? Can a DLL built using C# .NET be used by a DLL built with, for example, Borland C++? </p>
  
  <p>4a. If the answer to 4 is "no" then what is the point of a DLL? Why dont the various frameworks use their own formats for linked files? For example: an .exe built in .NET knows that a file type of .abc is something that it can link into its code.</p></li>
  <li><p>Going back to the <code>Web.dll</code> / <code>Business.dll</code> example - to get a class type of customer I need to reference <code>Business.dll</code> from <code>Web.dll</code>. This must mean that <code>Business.dll</code> contains some sort of a specification as to what a customer class actually is. If I had compiled my <code>Business.dll</code> file in, say, Delphi: would C# understand it and be able to create a customer class, or is there some sort of header info or something that says "hey sorry you can only use me from another Delphi DLL"?</p>
  
  <p>5a. Same applies for methods; can I write a <code>CreateInvoice()</code> method in a DLL, compile it in C++, and then access and run it from C#? What stops or allows me from doing this?</p></li>
  <li><p>On the subject of DLL hijacking, surely the replacement (bad) DLL must contain the exact method signatures and types as the one that is being hijacked. I suppose this wouldn't be hard to do if you could find out what methods were available in the original DLL.</p>
  
  <p>6a. What in my C# program is deciding if I can access another DLL? If my hijacked DLL contained exactly the same methods and types as the original but it was compiled in another language, would it work?</p></li>
  </ol>
  
  <p>What is DLL importing and DLL registration?</p>