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?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.