views:

130

answers:

4

Hi,

I have a solution with 2 projects:

  1. a c++ main project
  2. a c# project (display simulator)

Today these 2 apps share data using a loopback TCP client/server connection, but that's not very optimal (timing issues..).

I was wondering if there was a way to access the c# data from the c++ project directly and vice versa? (It seems to be possible with 2 c# projects..) If it's not possible, what's the best way to implement this with shared memory?

thanks! Michael

EDIT: thanks for the answers. The 2 projects used to be independant solutions and are both executables - I'm actually trying to merge the 2 into 1 solution / executable. For info: The c++ app is a PC version of an embedded app - the c# app is a lcd/HMI simulator.

A: 

You can use COM Interop or Platform Invoke to access native code in C#.

If that's not what you're asking for, please elaborate.

SLaks
A: 

Named Pipes?

For interprocess communication via named pipes you can check out the .NET 3.5 feature http://msdn.microsoft.com/en-us/library/system.io.pipes.aspx for the C# side. From the C++ side, I assume you know the equivalent :).

Aggelos Mpimpoudis
I suspect that he wants in-process communication; it sounds like the C++ project is a library consumed by the C# UI.
SLaks
A: 

There are two ways I know of to get direct access to memory between c++ and c# without the overhead of marshaling/demarshaling and com. If it is required to keep the c++ part of your code native then the only way I know to achieve this is to host the clr from your c++ application. A relatively complicated undertaking. Google "hosting the common language runtime". Then you can load your c# code as an assembly, call into the c# code and provide a common shared memory style interface. Although you will have to write all of the shared memory support yourself as I have found no direct support for shared memory in c#/.net.

The other way is to compile your c++ code with common language runtime support. This is by far easier and will allow you all the power and glory of c++ while allowing access to clr based data types, and the ability to call back and forth between your c++ and c# code. See "Language Features for Targeting the CLR" in your VS2008 documentation. pin_ptr will become your close friend and ally in this process.

Slaftos
+1  A: 

Converting the C++ project to a C++/CLI project might be the easiest way to go. Note however that some code doesn't play well with C++/CLI (we've had problems using libraries that use boost::thread in a managed executable).

Brian Stewart