RegAsm.exe “failed to load” “because it is not a valid .net assembly”

Problem

Ever get this error when trying to register a .Net DLL as an ActiveX component using RegAsm.exe?

“failed to load MyAssembly.dll because it is not a valid .net assembly”

I’ll bet you’re using a version of .Net higher than 2.0, aren’t you?  Turns out, each version of .Net you have installed has it’s own, version specific version of RegAsm.exe and when you run the RegAsm.exe command for your .Net 4.0 or so DLL, it’s running the .Net 2.0 version of RegAsm.exe.

Windows Registry Forensics
Windows Registry Forensics

Why make an ActiveX control if you’re using .Net?  Sometimes you just have to for many reasons which are outside the scope of this article.

Solution

[GARD]

There are a couple of ways to resolve this.

Specify Specific Version

If you want to specifically state which version to run, make a batch file for each version and name the batch file appropriately, and have that batch file have the hard path to the right RegAsm.exe.  For example, for the .Net 4.0 version of RegAsm, I created a batch file called RegAsm_4_0.bat and put it in the same folder with the RegAsm.exe file for .Net 4.0 here:

C:\Windows\Microsoft.NET\Framework\v4.0.30319

The contents of my RegAsm_4_0.bat file looks like this:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\regasm.exe %1 %2

Now, from any Visual Studio command prompt (regardless of what folder I’m in), I type RegAsm_4_0 (plus my parameters) and it’ll run it.

Override default version

If, on the other hand, you want to continue using the command RegAsm, you can create a batch file named RegAsm.bat and put it in each of the framework folders.  All copies of those batch files should run the same, explicit version of RegAsm.exe you want.

Unfortunately, you can’t simply rename the RegAsm.exe files without messing around with security settings.  But that’s an option too which I’m not getting into here.

See these images?

image

You’ll find an actual working versions of them at the top and bottom of this article. Please click the appropriate buttons in it to let your friends know about this article.

Check back later for updates too!