david david - 1 year ago 457
C# Question

C# importing C++ dll

I have a managed dll file which imports functions from a C++ dll to the managed environment. I'm using some of its functions in my program but the problem is, I get this error when I use it:

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

I placed the .dll file in the program's directory and in the system32 folder. However, it still doesn't work. I think I have to use DLLImport but I have no idea how to use it.. even after looking at some examples I am still confused. Can someone help me here?

Answer Source

You say:

I placed the .dll file in the program's directory...

But:

Unable to load DLL 'Libraries\lib.dll'

We need to see your DLLImport attribute creation, i.e., the C# signature of the native method. It looks to me like you probably specify the path, i.e.,

[DLLImport( "Libraries\lib.dll" )];
static extern void MyNativeMethod();

Try using this instead:

[DLLImport( "lib.dll" )];
static extern void MyNativeMethod();

That will search the running directory as well as through your PATH environment variable. If you specify a file path as you do I honestly don't know if it will search through PATH if the file is not found (I couldn't find mention of it in the docs).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download