1

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.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
PilawyerDev
  • 271
  • 2
  • 16
  • See http://stackoverflow.com/questions/1827102/managed-c-to-form-a-bridge-between-c-sharp-and-c as a reference for using a managed C++ wrapper as an intermediary between unmanaged C++ and C#. – whalebiologist Sep 03 '13 at 12:09

3 Answers3

1

No CLI?

You can do it using Reverse P/Invoke

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

sara
  • 3,824
  • 9
  • 43
  • 71
  • I've updated my question. Maybe I can create another shared library which is C++\CLI. Example/topic/start point? – PilawyerDev Sep 03 '13 at 12:09
  • 1
    @PilawyerDev see http://stackoverflow.com/questions/13293888/how-to-call-a-c-sharp-library-from-c-using-c-cli-and-ijw and http://stackoverflow.com/questions/4818136/call-c-sharp-dll-function-from-visual-c – sara Sep 03 '13 at 12:23
0

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

Joseph Moreno
  • 61
  • 1
  • 5
  • @Jean-François Fabre This isn't spam. I think the flagger is inappropriately flagging these. – George Stocker May 29 '19 at 18:39
  • 2
    OP is posting the same link more than once. I've cleared the first spam flag, but not the other ones. 4 posts promote the same tool – Jean-François Fabre May 29 '19 at 18:40
  • @Jean-FrançoisFabre There is no prohibition against the same link being in more than one answer; One of the considerations is are the questions duplicates of each other? If they are then the answer on the duplicate could be deleted, but not on the source. If the exact same content is on multiple answers; we try to eliminate the issue by closing the duplicates and deleting the duplicate answer on the closed question. – George Stocker May 30 '19 at 12:00
-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.

utnapistim
  • 26,809
  • 3
  • 46
  • 82