Where to place an unmanaged DLL for use importing into a C# program?
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 fr开发者_Python百科om 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?
Your DLL may have dependencies that also need to be loaded. Did you check for that?
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.
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)
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.
精彩评论