简体   繁体   English

C#库到本机C ++应用程序

[英]C# library to native C++ application

I know I can use dll import + wrapping (managed) to use C++ libraries in C# applications ( question link>> ) but what's opposite? 我知道我可以使用dll导入+包装(托管)在C#应用程序中使用C ++库( 问题链接>> ),但是相反吗? How can I use C# library in my C++ application? 如何在我的C ++应用程序中使用C#库? I'll handle events/operations/results in C# library but my application created with C++. 我将在C#库中处理事件/操作/结果,但我的应用程序是使用C ++创建的。 For example here is my C++ application: 例如,这是我的C ++应用程序:

//In this example; //在这个例子中; Manager will be my C# library's instance, Log is a C# library's function that can print something. Manager将是我的C#库的实例,Log是可以打印某些内容的C#库的函数。 (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++. 编辑:解决方案应该是本机C ++。 I'll not use C++\\CLI! 我不会使用C ++ \\ CLI!

EDIT: Only c++ application will be native. 编辑:只有C ++应用程序是本机的。 Namely I can create a shared c++ library to handle C# library. 即我可以创建一个共享的C ++库来处理C#库。 C++ App.(Event) >> C++\\CLI Lib. C ++应用程序(事件)>> C ++ \\ CLI库 >> C# Library , this way is posibble. >> C#库 ,这种方式很有用。 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. >>我的解决方案( 回答后,应该​​会对其他人有所帮助 ):我将创建一个C ++ \\ CLI共享库,并将其与本机C ++应用程序一起使用。 This library will handle my C# library ( more about in this question>> ). 该库将处理我的C#库( 有关此问题的更多信息>> )。 If I don't need C#, I'll re-code my C++\\CLI library as native library. 如果不需要C#,则将C ++ \\ CLI库重新编码为本机库。 I think that is pretty useful. 我认为这很有用。

CoreRT allows to compile native libraries for windows, linux and osx x64 platform this avoid to you use an C++ compatible layer. CoreRT允许编译Windows,Linux和osx x64平台的本机库,从而避免使用C ++兼容层。 CoreRT uses NetCore, and NetCore doesn't support C++/CLI. CoreRT使用NetCore,而NetCore不支持C ++ / CLI。

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

You will have to take care to expose your interface from the C# library to a C++-compatible layer. 您必须注意将您的接口从C#库公开到C ++兼容层。

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). 在C#库的顶部创建一个C API(最好的方法是在您的库中添加一个C ++ / CLI层)。

  • Create a COM interface to your C# library and use the library through COM objects. 为您的C#库创建一个COM接口,并通过COM对象使用该库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM