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

Accessing c++ variables from c#?

Dear all,
I have a c++ application and another c# application, 2 processes.
I want to access the data from the c++ application from c#.
Any idea on how to do that?
  • 2
1 Solution
It is not clear what you want to achieve. Can you please clarify. Do you need a some sort of inter-process communication between these two programs? Is it some sort of client-server environment? Do you need to access common data? Do you need to read the information from the program GUI? Do you have access to both program code? Are these program on the same machine?
you have some choices:
using shared memory via memory mapped files
using globally allocated memory and pass the handles by (Windows) messages.

write a file by one process, read it by the second.
using windows sockets to create a client-server connection between the processes and send/receive messages by tcp/ip or udp.
more ...

any of the communications has advantages and disadvantages.

hassanayoub85Author Commented:
Yes, both exes are on same machine. have source code for them.
both are on c#.
I want to have in the result 2 processes running, 1 is doing calculations and return values to the other process, and the other process give orders to process 1.
So Only variable reading from the 2 processes.

I need your cooperation and help with the most easy way plz
Note: I already tried write to file and read from it, but it is so slow, I am working on 40millisecond timer
the easiest way is to write to file and read from it. but as you have seen it is the slowest way.

the next easier methods are either memory mapped files via System.IO.MemoryMappedFiles or you WINAPI to allocate global memory, store the data there, and then pass the handle to the other process by windows message. the other process then could access the memory using the handle.

IntPtr hglobal = Marshal.AllocHGlobal(1000);
IntPtr ptr         = GlobalLock(hglobal);
// now use Marshal copy functions to copy the data to global memory
// send the handle to other process. 
// you would need its window handle what could be retrieved by FindWindow
IntPtr uid = RegisterWindowMessage("MyWindowID");
SendMessage(hwndProc, uid, hglobal, 0);
// when SendMessage returned the other process has fetched the data


Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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