Warm tip: This article is reproduced from serverfault.com, please click

Import a C++ .lib and .h file into a C# project?

发布于 2013-08-09 15:32:19

I have just started a C# project and want to import a C++ .lib and it's corresponding header (.h) file.

I've read various posts that all mention .dll, rather than .lib, which is confusing me.

The image below shows the .lib and .h file I'm referring to, all I've done is drag them into the project.

enter image description here

Can anyone point me to a clearer explanation of how to go about doing this? I'm sure it can't be as hard as it seems.

Questioner
Dan James Palmer
Viewed
0
2,843 2015-12-01 23:22:11

What you could do, is creating a C++/CLI wrapper and expose the functionality of the lib you want to use via your wrapper. The created wrapper dll you can easily reference in your C# project. This of course takes a little bit of work to create the managed/unmanaged wrapper, but will pay off in the long run.

To create a managed C++ project select under the C++ project templates CLR and Class Library. Here you can link to your lib, use the header file the way you are used to.

Next create a new class (ref class) and wrap your library in it. An example might look something like this:

LibHeader.h

int foo(...);

You write a wrapper class like this: Header:

Wrapper.h

public ref class MyWrapper
{
    public:
        int fooWrapped();
};

Your Implementation:

Wrapper.cpp

#include Libheader.h

int MyWrapper::fooWrapped()
{
     return foo();
}

Namespaces and all the good stuff omitted for simplicity. Now you can use MyWrapper in your C# code just as easy as any other managed class. Of course when the interface of the lib gets more complicated you have to think about it a bit more, but it might help to separate the lib-code from your application. Hope to have shed some light on it.