简体   繁体   中英

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 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?

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.

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.

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 technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM