MMC Snap-in and .NET Framework 4.0

MMC based snap-in application targeting .NET Framework 4.0 always makes developer life difficult. It always throw System.BadImageFormat Exception with message as “assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded” while adding registered snapin dll to MMC console.

Cause:

MMC is a native windows component which hosts the CLR through the CLR native hosting APIs. The MMC for the OS shipped before .NET 4.0 targets CLR 2.0 by default and doesn't have new APIs to target the latest CLR. Hence .NET 4.0 Frameworks doesn't support MMC snap-ins on old OS.

The OS released after the release of .NET 4.0 Framework has updated MMC which can call new APIs to target the latest CLR and hence MMC Snap-ins works with .NET Framework 4.0 without any issue.

Solution:

To make MMC snap-in work with .NET Framework 4.0 on the OS shipped before .NET 4.0 Framework (till windows server 2008 R2 and windows 7), supportedRuntime version option needs to be used to direct MMC to use CLR 4. The steps to do this are:

1.       Create an mmc.exe.activation_config file anywhere in the system and specify supportedRuntime version as v4.0.

2.       Create a new system environment variable COMPLUS_ApplicationMigrationRuntimeActivationConfigPath and sets its value with the path of mmc.exe.activation_config file

Above steps actually forces to load CLR 4.0 and hence run the application without any problem. As .NET 4.0 Framework allow hosting of multiple CLR in a process which can run side by side, other snap-in applications or any .NET applications remains unaffected.