Thursday, February 17, 2011

When a DLL is not found while P/Invoking, how can I get a message about the specific unmanaged DLL that is missing ?

When you link to an unmanaged library (say 'A.dll') which in turn links to another library ('B.dll'), and B.dll is missing, you will get a run-time error message about failing to load 'B.dll'.

But when you P/Invoke into 'A.dll' from managed code, you'll get a general exception of this form:

Unhandled Exception: System.DllNotFoundException: Unable to load DLL 'A.dll': The specified module could not be found.

How can I get an error message that pinpoints the specific unmanaged dll file that failed to load, when p/invoking from managed code ?

From stackoverflow
  • I'm not sure how you can get this in the form of an exception. When I run into this problem I usually try and open the DLL with depends.exe. It's pretty good and reporting missing dependencies that exist if the DLL is simply loaded from it's current location.

  • I don't think there's any specific API you can use to pinpoint why LoadLibrary (the underlying Win32 API) failed for 'A.dll'. I recommend the use of a tool like Dependency Walker to troubleshoot DLL loading errors.

0 comments:

Post a Comment