Friday, February 17, 2012

System.DllNotFoundException: Unable to load DLL, figured it out.

In my previous post, I was trying to make a DLL that would load from C#, and in one case, had the exception:

System.BadImageFormatException: An attempt was made to load a program with an in
correct format. (Exception from HRESULT: 0x8007000B)

Interestingly, I found that I could also generate this one by solving the problem above:

System.DllNotFoundException: Unable to load DLL 'simpleDLL.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)

If one has .NET 2.0, the C# binds as 32-bit, so an explicit 64-bit DLL might try to call a 32-bit DLL. I used Microsoft's sysinterals to understand what was happening and Dependency Walker to check my DLLs.

In closing, if you get a System.DllNotFoundException even when you DLL is in the same directory as your executable, it is a binding issue and change from .NET 2.0 to something higher (I used 3.5). If you get a BadImageFormatException, it is due to the binary format, so be sure that the DLL matches your machine bit width.

No comments:

Post a Comment