Monday, October 23, 2006

DLL not found exception

I've been working on a C++ project recently that involved creating a DLL.  Although I had unit tests to pummel the objects that were used inside the DLL I wanted an easy way to test the DLL itself through its interface.  I chose to create a C# Winforms app that used the DLL using interop.

Really easy to do:

public static extern int MyFoo(string bar);

There's heaps of documentation out there for this kind of stuff.  Suffice to say that the .NET framework goes to great lengths to make this happen as easily as possible.

One gotcha that I ran into though was that initially the JIT compiler threw an exception - a DllNotFoundException.  I checked to ensure that the DLL and EXE were in the same directory, I used dumpbin to ensure that the DLL was correctly exporting the expected symbols etc...couldn't figure out what was wrong. 

Turns out that my DLL had a dependency on another DLL that wasn't in the path.  Unfortunately the exception doesn't give you any further useful information so watch out for this problem.


George S. said...

Thank you for this post. It really solved our problem. We had been spending lots of time by figuring out what was wrong. Our C# code would not call external DLL until we figured out that the external DLL was dependent on another DLL which was not present. Thanks again!

George S.

Matt said...

Hearing that it helped made my day. Thanks for letting me know George!

All the best,

donb2000 said...

Matt, this one saved me a lot of time and trouble as well. Nothing like finding a post that gets straight to the point of solving a problem! Thanks!

Nikhil said...

Thanks so much!! I was going crazy trying to figure out why Visual Studio couldn't find the dll even if I copied it into the project folder.

Chris said...

Bravo...saved my bacon after a long day or interop hell!