23

I hold before you a DLL. Using only the Win32 SDK, can you tell me if this DLL is a .NET assembly?

Why? Our application loads plugins in the form of DLLs. We are trying to extend the definition of these plugins to allow for .NET assemblies but the interface will be different and thus the loader will need to know if the DLL is managed or unmanaged before loading it.

starblue
  • 55,348
  • 14
  • 97
  • 151
Charles
  • 2,642
  • 3
  • 33
  • 53

3 Answers3

31

To determine whether a DLL (or EXE) is managed or unmanaged, use dumpbin.exe with the /dependents switch. If you see mscoree.dll in the output, then the assembly is a managed assembly.

For example, for a managed DLL that I created in Visual Studio 2010, I get the following output:

Dump of file <MANAGED_DLL>.dll

File Type: DLL

  Image has the following dependencies:

    mscoree.dll

  Summary

        2000 .reloc
        2000 .rsrc
        2000 .sdata
       12000 .text

dumpbin.exe is delivered as part of the Visual Studio Tools. To run it, a convenient way to do so is via the Visual Studio Command Prompt. For example, from my Windows 7 machine running Visual Studio 2010, I find the Visual Studio Command Prompt in the Windows Start Menu at:

Microsoft Visual Studio 2010 => Visual Studio Tools => Visual Studio Command Prompt (2010)

Then, within the Visual Studio Command Prompt just enter:

dumpbin /dependents DLL_OF_INTEREST.DLL

or

dumpbin /dependents EXE_OF_INTEREST.EXE

As an alternative, you can use the corflags.exe utility that is also included with Visual Studio Tools. Running it from the Visual Studio Command Prompt on an unmanaged assembly:

corflags UNMANAGED.DLL

..you'll get:

corflags : error CF008 : The specified file does not have a valid managed header

...whereas on a managed assembly, you'll get something like:

Version   : v2.0.50727
CLR Header: 2.5
PE        : PE32
CorFlags  : 1
ILONLY    : 1
32BIT     : 0
Signed    : 0

Related:

Community
  • 1
  • 1
DavidRR
  • 18,291
  • 25
  • 109
  • 191
  • For anyone that can't find `dumpbin.exe`, it's included as part of MSVC C++ build tools (e.g. `MSVC v142 - VS 2019 C++ x64/x86 build tools`). – encryptedcurse Nov 10 '21 at 03:51
6

I would simply try to load it as a .NET assembly, and if it fails, fall back to "unmanaged" interface.

Fyodor Soikin
  • 78,590
  • 9
  • 125
  • 172
  • Wouldn't load errors like missing dependencies when loading managed assemblies cause the plugin loader to fall back to unmanaged mode unnecessarily then? – Harindaka Oct 21 '11 at 03:05
  • @Harindaka: I guess it would, so what? Unmanaged loader will also fail, and that will be the end of story. What problems do you see with this? – Fyodor Soikin Oct 24 '11 at 04:09
4

You can check the PE header information for the information about what type of information is contained in the DLL. This article describes how to accomplish this in detail.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • 9
    All the useful information should be included in this post, not just linked, the link is dead now ;) – DrCopyPaste Feb 11 '19 at 09:42
  • @DrCopyPaste https://web.archive.org/web/20160202125049/http://blogs.msdn.com/b/kstanton/archive/2004/03/31/105060.aspx – jaybro Jul 22 '20 at 14:46