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

前端 未结 3 2216
北海茫月
北海茫月 2020-12-10 10:28

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

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-10 11:02

    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.

提交回复
热议问题