3

Is there any simple way to get a few data variables from a C++ program into C#? Both programs would run on the same computer.

I need this to communicate the results of a C++ coded program (position: x,y (integer) and orientation (double)) to another device using C# coded sdk...

I am thinking along the lines of allocating a memory region then declaring it in a file, reading the file in C# to get the pointers and then working from there using a semaphore to control access.

Would this work? Any references on how to write something like that in both C# and C++?

All help is much appreciated!

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Laov
  • 65
  • 7

3 Answers3

5

Sure! The one example is shared memory: Sharing variables between C# and C++ (that is what you mean with allocate memory and so on)

The second one is the "named pipes" method: http://www.codeproject.com/Tips/420582/Inter-Process-Communication-between-Csharp-and-Cpl

Community
  • 1
  • 1
Leo Chapiro
  • 13,678
  • 8
  • 61
  • 92
1

You can communicate between two separate processes in a lot of ways. I am pointing out a few of them -

  • Inter Process Communication The first one I like is inter process communication, shortly known as IPC. There are a lot of articles on it. So I am not providing a code sample, you can get one from code project here -

Inter-Process Communication between C# and C++ using named pipes

  • Shared Memory You can also use shared memory, I personally find it a little harmful, because it violates a rule to read and write in another programs memory. Here is an example -

Sharing variables between C# and C++

  • Database Its only possible if both of your programs use same database. Very easy, you already know it. But only works in limited cases.

  • Your own Custom Implementation Example would be writing in a file in a location and then making that file accessible to both the programs.

Pick your choice !!!... Personally I would go for IPC for your case.

Community
  • 1
  • 1
brainless coder
  • 6,310
  • 1
  • 20
  • 36
0

Since you are saying that both programs are running on the same system, a simple solution would be to write the output of your C++ program to a file and use this file as input for a C# program. Maybe you can use a FileSystemWatcher on the C# side to monitor that file for changes.

Some more links:

How to write to a file in C++

How to read a file in C#