views:

73

answers:

3

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?

+3  A: 

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

CesarGon
Good idea. How would I go about checking that?
Tom Wright
DependencyWalker is a nifty tool that will tell you what other modules (DLLs, drivers, etc.) you need in order to load a given DLL. Get it here: http://www.dependencywalker.com/
CesarGon
Thanks, that did the trick.
Tom Wright
Glad it worked.
CesarGon
+3  A: 

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.

jsmith
Unfortunately this didn't help. Additionally, the code supplied by the vendor works without the extension.
Tom Wright
@Tom if the code from the vendor works, why would you be asking about it on stack overflow? The tutorial must not be exactly accurate, possibly an issue with the dll itself?
jsmith
It's not a tutorial, more of a test app to check the integrity I guess. I'm asking the question because my verbatim copying didn't work. Thanks for the link though.
Tom Wright
A: 

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)

Grzenio