C# library to native C++ application

只愿长相守 提交于 2019-12-31 04:23:25

问题


I know I can use dll import + wrapping (managed) to use C++ libraries in C# applications (question link>>) but what's opposite? How can I use C# library in my C++ application? I'll handle events/operations/results in C# library but my application created with C++. For example here is my C++ application:

//In this example; Manager will be my C# library's instance, Log is a C# library's function that can print something. (That's only an example, not works. For describing something to understand my question)

int main()
{
   if(MySystem.Start() == 0) //If there is no error
   {
      Manager.Log("System started successfully!"); //Post it to C# library!
   }
   return 0;
}

EDIT: Solution should be native C++. I'll not use C++\CLI!

EDIT: Only c++ application will be native. Namely I can create a shared c++ library to handle C# library. C++ App.(Event) >> C++\CLI Lib. >> C# Library, this way is posibble. Is there any example/topic/start point?

>> MY SOLUTION (After answers, should be helpful for others): I'll create a C++\CLI shared library and I'll use it with my native C++ app. This library will handle my C# library (more about in this question>>). If I don't need C#, I'll re-code my C++\CLI library as native library. I think that is pretty useful.


回答1:


You will have to take care to expose your interface from the C# library to a C++-compatible layer.

Out of the top of my head, you could do two things:

  • Create a C API on top of your C# library (best way would be to add a C++/CLI layer to your library).

  • Create a COM interface to your C# library and use the library through COM objects.




回答2:


No CLI?

You can do it using Reverse P/Invoke

See http://tigerang.blogspot.co.il/2008/09/reverse-pinvoke.html.




回答3:


CoreRT allows to compile native libraries for windows, linux and osx x64 platform this avoid to you use an C++ compatible layer. CoreRT uses NetCore, and NetCore doesn't support C++/CLI.

https://dev.to/encrypt0r/writing-native-libraries-in-c-3kl



来源:https://stackoverflow.com/questions/18591662/c-sharp-library-to-native-c-application

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