Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 865
rayanisran Avatar asked Aug 29 '11 15:08

rayanisran


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is\\ C?

\c means you are escaping the "c" character, allowing you to print c \c means you are escaping the "\" character, which allows you to print \c. Escaping a character means that you are making the compiler ignore the character, if it is used for any functions or is reserved within a string.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

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).

like image 131
Ed S. Avatar answered Sep 18 '22 16:09

Ed S.


There isn't enough information here to help, as you're not showing the API (in native code) you're trying to import, etc.

That being said, I'd strongly recommend reading the Platform Invoke Tutorial as well as A Closer Look at Platform Invoke on MSDN. They walk through the main issues, as well as showing many examples of how to import and use functions from a C++ DLL.

like image 26
Reed Copsey Avatar answered Sep 18 '22 16:09

Reed Copsey