How to detect a managed dll?

  • Thread starter Thread starter Bob Altman
  • Start date Start date
B

Bob Altman

Hi all,

I need to be able to tell if a DLL is native or managed. The approach most
commonly suggested seems to be to simply try to load the DLL as an assembly
and to catch the appropriate exception if it's not a valid assembly. The
problem with this approach (in my application) is that it winds up
write-locking the DLL. True, I could load the DLL into a different
AppDomain than my main application and then nuke that AppDomain after I've
done my "is it managed" detection, but that's a pain to implement.

Is there some data in the file header that I could look at to determine if
the DLL contains managed code? I have existing code that calls the Windows
MapAndLoad function and digs through the image header for other information
(specifically, the image subsystem to see if it's a GUI application), so if
there is something in the image header that is unique to managed DLLs then I
could easily enough dig that out as well.

TIA - Bob
 
Hi Bob ,

The most reliable and easy way to detect this is parsing the PE header. The
CLI specification requires that RVA 15 from the PE header points to the CLI
header. See the links below for more details:
"How to Determine if a file is a .Net assembly (in Delphi and C#)"
http://www.anastasiosyal.com/archive/2007/04/17/3.aspx
"How to determine whether a file is a .NET Assembly or not?"
http://geekswithblogs.net/rupreet/archive/2005/11/02/58873.aspx

Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=========================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top