Where to place an unmanaged DLL for use importing

2019-07-30 16:51发布

问题:

This must be a really stupid question, but I'm still very green when it comes to C#.

Anyway, I've got a DLL and I'm importing it with a line like this:

[DllImport(@"MyCoolDll")]

I've lifted this straight from the demo app provided by the vendor, but it keep complaining that it can't find the DLL. The actual error (from Visual Studio 2010) is like this:

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

I've tried placing the compiled DLL in the bin/debug and bin/release folders. I've even tried copying it to system32, but nothing seems to work.

Any ideas?

回答1:

Your DLL may have dependencies that also need to be loaded. Did you check for that?



回答2:

I know you have to give the full file name. So

[DllImport(@"MyCoolDll.dll")]

It should work from the bin\debug or bin\release folders.

Update

This is where I learned how to import unmanaged dlls. If it was a test app that is working correctly, check it's bin\debug folder to see what is different from yours. Possibly an extra dll being referenced? Also check all the references within the sample app to make sure you are not missing any.



回答3:

As far as I know you have to provide extension:

[DllImport(@"MyCoolDll.dll")]

I usually keep these dlls locally with the program binaries (so in bin\Debug for development)



回答4:

The key for me was to look at the paths listed in the Visual Studio Build console output, to see where the binaries were being placed. Once I found that, I knew where to copy the unmanaged dll and it fixed the "Unable to load DLL" error.