• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 168
  • Last Modified:

Communication between C++ / C# applications

I am launching an applicaton written in C# from within a C++ application and need to pass data to the C# application when it is launched and pass data back to the c++ application when the c# application is closed.  How can I accomplish this? I tried comman line arguments for the C++ to C# side and WM_COPYDATA for c# to c++ to no avail.  
3 Solutions

Named pipe

Miguel OzSoftware EngineerCommented:
What kind of applications are they? Console, Winforms, server/client.
can you modify them (C# app) to a server app?
harry12Author Commented:
They are basic UI apps where the user enters in data and the data entered is eventually saved to a file. The C++ app should send data from a file to the c# app where the data will get updated.  The c# app needs to send the updated data back to c++ app where it will be saved.
Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

Why don't you just use the file as the neutral means of communication.  Since your already writing to a file, close it from c++.  Then open it in c#, work with it, close it.  And then the c++ app can take back over.
So besides "NamedPipes" mentioned before, one good possibility is to use sockets
For C# you can use System.Net namespace and for C/C++ you can youe WSA (windows.h/winsock2.h) to establish a communication. This method is sure to work, but you will have to establish kind of a protocol. For example:

1. launch C++
2. launch C#
3. put C# in "listening" mode
4. trying to connect to C# socket
5. after connect send data to C# and put C++ in listening mode
6. process data within C# and connect to C++
7.receive data back in C++
8. close C# program
9. process returned data in C++
10. close C++ program

This is the basic algorithm :)
Miguel OzSoftware EngineerCommented:
If the files do not have a lot of read/writes uses it and it is not slow, then you can use the file as a means of communication. Just be aware that your programs must :
- Lock the file for multiple read and exclusive write. e.g.
  lock alternatives  
- File permissions
- File contents encoding (ASCII, UTF-8 or Unicode)
Jaime OlivaresCommented:
If your C# application is a console application, I would suggest to output your result of your C# app to the console and trap the output from your C++ application.
There are lots of examples of that in internet, for example:

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Tackle projects and never again get stuck behind a technical roadblock.
Join Now