Exporting cpp mangled functions from a 3rd party library

断了今生、忘了曾经 提交于 2021-02-20 05:19:26

问题


I have a third party library as source code. It comes with a Visual Studio project that builds a .lib from it.

I want to access the functionality from C# though, so I copied the project and changed it to create a dll.

The DLL didn't contain any exported functions though, so I also created a module definition (.def) file, and added the functions I need in the EXPORTS section.

This works for all the functions that are declared in extern "C" blocks. However, some of the functions that I need are declared outside of those blocks.

If I add those to the EXPORTS section I get the error LNK2001 unresolved external symbol XYZ :(

I don't want to change the sourcecode of the 3rd party library if I can avoid it.

What's the most elegant and hopefully simplest way to access those functions as well?

Edit

One more point for clarification: As far I can tell there is no C++ functionality involved in the interface I want to expose. I honestly don't understand why the 3rd party authors did not just include the few remaining functions into the extern "C" blocks. These functions are at the bottom of the header file, maybe the person that added them just made a mistake and put them outside of the extern "C" blocks scope.


回答1:


For C++ one way (IMHO the most elegant) would be using C++/CLI, which is designed for that. Especially if you have not only functions but also classes.

You create a thin wrapper layer which is fairly simple:

  1. Create a CLI class
  2. put a member instance of the original class
  3. wrap all public methods from the original class

Like This (untested):

C++ nativ:

// original c++ class
class Foo {
public:
    Foo(int v) : m_value(v) {}
    ~Foo() {}

    int  value()      { return m_value; }
    void value(int v) { m_value = v; }

private:
    int m_value;
};

CLI wrapper:

// C++/CLI wrapper
ref class FooWrap
{
public:
    FooWrap(int v) { m_instance = new Foo(v); }
    !FooWrap()     { delete m_instance; }
    ~FooWrap()     { this->!FooWrap(); }

    property int value {
        int  get()      { return m_instance->value(); }
        void set(int v) { m_instance->value(v); }
    }

private:
    Foo *m_instance;
};

Here you can find a short howto, which describes it in more detail.

Edit:

From your comments:

[...] I never worked with C++/CLI though and the language looks a little confusing. Since the project deadline is pretty close I'm not sure if there's enough time to learn it. I'll definitely keep this option in mind though!

If you are not looking for the most elegant way, as in your original question, but for the fastest/shortest way: For C++ one way (IMHO the shortest way) would be using C++/CLI, which is designed for that. Especially if you have not only functions but also classes. You create a thin wrapper layer which is fairly simple... Here you can find a short (in 10 min) howto, which describes it in more detail.



来源:https://stackoverflow.com/questions/56965138/exporting-cpp-mangled-functions-from-a-3rd-party-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!