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!

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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#

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.