The best way to communicate between .NET and a native C ++ application

As the title suggests - I currently have two apps. One is .NET, winforms and the second is native C ++. Both work together and need to communicate (C ++ to .NET and .NET to C ++), a .NET application is launched from a C ++ application. Currently communication is done by writing data to a file and sending a postmessage to inform the C ++ application that the data has been written. While this works, I don't think this is the best way to do it. Because of this, I would like to ask you what would you recommend as the best way. I am considering writing a CLI wrapper around .NET to bundle it in C ++. Are there any disadvantages of using such a wrapper versus writing data to files?

-------- Update --------

I forgot to add that both applications always run on the same machine. Solution should work on Windows xp, win7, win8

------- Conclusion ------

Thanks a lot for your answers, they are all very helpful. A good overview of the differences between interprocess communication technologies is the website provided in response to Furkan Omay - Externally Supported Interprocess Communication Techniques in Microsoft Windows . However, the issue seems to be related to antivirus programs, such as on this website from McAfee. In some cases, this can be a huge problem. According to C ++ / Cli I like Drax's answer, thanks. I also see no downsides to using the wrapper. Nice tutorial here .

+3


source to share


4 answers


There are several options for this.

  • Client / server connection

    Your application must listen on a TCP (preferred) or UDP port and the other must listen on that.

    Provides the ability to split your application onto another computer / network interface.

    But you leave the port on the machine. You need to make sure it is protected from the internet. Some internal firewalls might even complain about localhost. (But they won't block it.)

  • Named pipes

    You can read and write like a regular file .. NET has built-in support for System.IO.Pipes

    The main difference from the purely file-based approach is that the application can wait to process the data. It's like waiting for water to come out of a pipe.

    You need to handle the directions and safety of the pipes and then dispose of them properly.

    Google Chrome uses this method to communicate between different workflows.

  • Sending Windows Messages

    As i486 mentions, you can use RegisterWindowMessage to register for custom messages and send them using SendMessage or PostMessage. This is a friendlier C ++ approach.

    Poorly written applications can lead to resource depletion , but it won't be easy.

  • mail slots

    Ian Goldby speaks about this. Mailslots provides one-way messaging between applications with very simple Windows API calls . It is best to send many small lines.

  • Message queue

    You can connect your applications to a message broker like RabbitMQ and use a publish-subscribe model to transfer data. This requires a server, which you don't want, but:

    You can use ZeroMQ , which can work without a server. It flares up quickly, can communicate through various technologies. However, more documentation is required to achieve the desired behavior than the previous ones.

  • Standard I / O

    You can redirect your stdout which can be accessed by the child process via stdin.

Personally, I will use named pipes as per your requirements. They remain on the computer and do not require major changes to the code. I used to make a C # app that talked to an OpenCV app written in C ++ and the named pipes worked fine in this case.



You can create two simple classes in both languages ​​to pass data between them.

However, all other solutions will still work. TCP / IP can run on one computer. Messages, Mailslots, Sockets and Pipes are one of the third party interprocess communication methods in Microsoft Windows , and they work even in Windows 2000.

+5


source


I usually log a custom message with the API RegisterWindowMessage

for both apps.



+1


source


The main (only?) Disadvantage of the cli wrapper is that it will take a little more development time because its project is more writing / debugging / maintaining. But this is probably the safest solution if your applications are not small.

Otherwise, you can choose among other communication layers like NamedPipes or Sockets, which is fairly generic but adds an unnecessary protocol layer that doesn't exist if you port your own code to C ++ / Cli. This language was invented for this use case.

+1


source


We have the same problem. From .net to c ++ we call dll, from c ++ to .net we create a local webservice providing Resful APIs for c ++.

0


source







All Articles