0

I've got a PInvoke wrapper set up for a certain native DLL, but any time I try to invoke anything from it, it crashes, saying System.DllNotFoundException: The specified procedure could not be found.

Things I've already checked:

  • The DLL is in the same folder as the EXE.
  • Dependency Walker shows that the DLL only has one non-system DLL dependency, and it's already loaded (successfully) into the process by this point.
  • It gives the same error no matter which function I try to call into, including a dummy function with no body. (So points 1 and 3 from the answer here do not apply.)
  • I've used Dependency Walker to ensure that the functions are being exported, with the same names, including case sensitivity.

By this point, I'm just about at my wits' end. How can I debug this and figure out what's actually going wrong? The error message I'm getting is a spectacular pile of fail when it comes to usefulness; it doesn't even tell me the name of the procedure it can't find.

2 Answers 2

2

I'd debug this first in a native application. For reasons I will explain later.

Put a simple console app in the same directory as your C# executable. Have that console app call LoadLibrary and GetProcAddress on the DLL you are p/invoking. Does the same error occur?

If the error does occur you can now debug with Dependency Walker in profile mode. Load the console app into Dependency Walker. Then use the actions on the Profile menu to instrument the application executing. You should see the error reported there and you'll know exactly which function could not be found.

Dependency Walker, to the best of my knowledge, does not play well with .net, hence the native test host.

If the error does not occur, then something deeper is amiss

1
  • Testing with a native host, I found that my copy of that one dependency was out of date, and my DLL needed a function that wasn't in the old version. After updating it, everything seems to be working now. Aug 20, 2015 at 14:53
1

Use gflags.exe to turn on Loader snaps for your application.

This should give heaps of information related to dll loading / resolving on the debug output of the process, which you can trace to see where it fails.

More info: Debugging LoadLibrary Failures

Please note: depending on your visual studio settings, your code will run either in ProjectName.vshost.exe or ProjectName.exe

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.