tags:

views:

701

answers:

7

Hello there,

I have a C++ application with data that needs to be shared with a C# application. I'm currently transferring the data via files, but with speed and quantity of the data becoming an issue I would like to find a way to share the data through memory.

I'm a beginner to intermediate programmer at best, and so far I have heard of two methods that may be able to help me do this: Socket Programming & Memory Mapped Files

So my questions is, which is the best way to do this? (yes, speed is a factor)

and any info or links to info that could help me in my research and comprehension of the method you suggest would be very much appreciated.

Thank you,

+6  A: 

You could use named pipes for interprocess communication. I haven't used it from c++ land yet though..

driAn
Asking about C#, I'm guessing that you're probably developing on windows. If so, then to get started in both C++ and C#/pinvoke, see http://msdn.microsoft.com/en-us/library/aa365590.aspx
Aaron
+2  A: 

.NET - COM interoperability

John MacIntyre
+4  A: 

Sockets, IMO. It is standard, fast (even more so if you are running in the same machine) and very flexible. Memory mapped files I'm not sure if it is supported by C# but I could be wrong.

Otávio Décio
+3  A: 

This is a fairly difficult question, mostly because there is no right answer. You definitely could use sockets or memory mapped files to communicate between two processes. Other alternatives are COM or simply posting a windows message from one process to another.

consultutah
A: 

I'd probably suggest googling interprocess communication to do this. Each OS has a different method, but you basically need to signal one application from the other (with the signal encoding the data and datatypes).

Using sockets is a good method, but has the problem that if the loopback address of the machine is kaput (which can easily be done through mis-configuration while playing around with ip stuff) then your app won't be able to communicate. On a more serious note, if the loopback address is compromised and made to point at an attackers machine then your application is suddenly sending data to a malicious source, although this may or may not be a problem for you.

workmad3
A: 

Memory mapped files aren't natively available in C#, but you could probably do something using P/Invoke.

Jon Tackabury
+1  A: 

Erm. I would suggest a different approach. I believe you would find it much easier to make an object (or objects) in managed C++. You can keep everything in your project native C++, except this file/class, which you would compile with /clr.

The /clr class will be able to interop trivially with .net, since it is .net. Then you can add regular C++ methods to the object to get data in and out as you see fit. Typically I would suggest copying data across the boundary so you don't need to screw around with pinning or anything.

It takes a little getting used to, but this approach is very flexible.

Here's a little something that talks about it better than I can: http://blogs.microsoft.co.il/blogs/sasha/archive/2008/02/16/net-to-c-bridge.aspx

Joe