Search code examples
.netmanaged

How can I programmatically determine if a DLL is managed or unmanaged?


How can I, in a program, determine if a DLL is managed or unmanaged code? We're using this code to make sure all of our managed DLLs are MSIL. But it throws an exception on unmanaged DLLs so I need a way to first check if a DLL is managed.


Solution

  • The simplest option would most likely be to just try to open the file as you are, and catching the exception. Any unmanaged assembly will throw an exception on Assembly.LoadFrom.

    However, if you want to determine this more rigorously, you'd need to examine the PE header for the appropriate information yourself. This article describes the process in detail, but it requires checking the IMAGE_OPTIONAL_HEADER structure of the PE header of the DLL.