Call a C# method/function from a C++ DLL (which is loaded from C# with “Dllimport”)

前端 未结 4 789
逝去的感伤
逝去的感伤 2021-01-22 00:21

It\'s a little hard to resume it in a single title, so here my situation.

I\'m building a C# application that loads a C++ library.
I call functions from that C++ DLL

4条回答
  •  一个人的身影
    2021-01-22 00:50

    The Unmanaged Exports project might help you with this:
    https://sites.google.com/site/robertgiesecke/Home/uploads/unmanagedexports

    Basically it allows you to export Functions from your assembly using the DllExport Attribute. Those functions can then be used like normal native Dll exports. It's something like the missing counterpart of the DllImport attribute.

    You would then declare your method like this:

    [DllExport("myCSharpFunction")]
    static public void myCSharpFunction()
    {
        MessageBox.Show("Called from C++ code !!");
    }
    

    But also such a two-way dependency would usually look suspicious to me. Maybe it is also possible in your case to use callbacks, like ken suggested.

提交回复
热议问题