0

I have already embedded a .Net runtime(CoreCLR) in my native C++ app. as this tutorial did: https://learn.microsoft.com/en-us/dotnet/core/tutorials/netcore-hosting

Now, I can call C#(managed) from C++(unmanaged). But how to call C++ from C# in this situation. I mean, is there any way to let both sides communicate each other?

------ More Details: -------

I'm developing a cross-platform C++ app (supporting Win/Mac/Linux). .Net Core is a good choice to be the script.

but how to communicate with each other ? (not just single side)

If "C++_app_with_C#_libs" is not suitable, I guess the only way is "C#_app_with_C++_libs"...(is that right?)

As far as I know, the Mono can achieve my target. cause Mono has a special tool:

[DllImport ("__Internal", EntryPoint="EnterPoint")]

which can find symbol in the current executable image. There is no "__Internal" in .Net Core, right?

After all, You know, it's not a good idea to choose Mono in 2019.

hanjie zou
  • 11
  • 2
  • 1
    May this help? https://learn.microsoft.com/en-us/dotnet/standard/native-interop/pinvoke – Renat Jun 18 '19 at 08:09
  • I personally always found it easiest having an intermediate C++/CLI layer... – Aconcagua Jun 18 '19 at 08:10
  • @Renat Thank you! But, is P/Invoke worked in "native C++ app with embedded C# lib"? In my impression,P/Invoke is used for "C# app with C++ libs",is that right? – hanjie zou Jun 18 '19 at 08:35
  • @user10822212, correct. P/Invoke is: c# loads native dll (if it's not loaded yet), then c# calls some function inside native dll. – Renat Jun 18 '19 at 08:39
  • It shouldn't make any difference if the CLR was loaded by OS or by an application (apart from the app possibly not having the same access rights). – Aconcagua Jun 18 '19 at 14:39
  • @Aconcagua Thanks Buddy. I wonder if it is possible to build a native C++ app, with C# component? No matter how the C# component be. either a Independent CLR ? or a host CLR. > As far as I know, the Mono can achieve it. 'cause Mono has a special tool: > [DllImport ("__Internal", EntryPoint="EnterPoint")] > which can find symbol in the current executable image. There is no "__Internal" in .Net Core, right? After all,You know, it's not a good idea to choose Mono in 2019. – hanjie zou Jun 18 '19 at 15:02
  • Biggest problem with mono was that it always was behind in development – though I didn't follow the development any more recently. In a project (that's now quite a while ago), we had generated C++ code and integrated it into a C# application – by building a C++/CLI wrapper around the generated code. That worked pretty nice. Or you get back at @Renat's link... – Aconcagua Jun 18 '19 at 15:20
  • The other way round is a bit trickier, as the unmanaged executable needs to load a CLR, as you found out already. Maybe these two links might help: https://stackoverflow.com/questions/6605391/loading-clr-in-c-start-problem https://stackoverflow.com/questions/6605391/loading-clr-in-c-start-problem However, I didn't dig in too deep (a bit out of .NET development since a while...) – Aconcagua Jun 18 '19 at 15:20
  • @Aconcagua Thank you for your recommendation! I think I will go on researching some articles about this problem. After all, "C#_app_with_C++_libs" is still a reserved choice. – hanjie zou Jun 19 '19 at 08:05

0 Answers0