Mason Wheeler
Mason Wheeler

Reputation: 84540

How do I debug "System.DllNotFoundException: The specified procedure could not be found"?

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:

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.

Upvotes: 0

Views: 1922

Answers (2)

David Heffernan
David Heffernan

Reputation: 612794

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

Upvotes: 2

Mark Jansen
Mark Jansen

Reputation: 1509

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

Upvotes: 1

Related Questions