0

I've seen a few answers for almost the same question, but there wasn't C# code to help me understand.

I have a C++ .DLL file injected into memory. Although, this DLL simply fetches information from the application and also gets information from the C# .exe.

The C# exe information will change every second, while the C++ information will change only once or twice.

How can I create a shared variable between the two running applications, and how can I read/write to it?

Thank you!

4
  • Perhaps redis.io? Commented Jul 26, 2013 at 2:12
  • Umm, not quite... It's not explained that well... I am more searching for something I can put in memory, like a struct or something Commented Jul 26, 2013 at 2:28
  • That's actually what redis is. It's an in-memory shared struct server. It's designed to be lightweight system for stuffing values that can be accessed by multiple processes. The API is simple and there are clients in just about every imaginable language. You can spin up a redis instance and have multiple programs read and writing, or you can run redis on a remote server that processes across a network can access. There is no administration or user access lists, it's just a fast way to store and retrieve simple or complex values Commented Jul 26, 2013 at 3:54
  • There is also ZeroMQ (zeromq.org) which has even less infrastructure (there is no server the library is the server) and it lets you wire up processes in multiple languages and optionally across a network (ZeroMQ doesn't only use TCP/IP it has inter-process sharing as well). The difference is there is no persistence in ZeroMQ it's more event driven data passing so each environment would be responsible to remembering the last value a common variable had. Commented Jul 26, 2013 at 3:58

2 Answers 2

1

If you want to build a reliable solution, you should use the following:

  1. Calls from C# to C++ functions. Keep types of parameters as simple as possible. Any C# thread can make these calls.
  2. Start your own C++ threads that read/modify only the C++ data and call only the C++ methods and functions.
  3. Use Windows kernel objects for synchronization and other similar stuffs. It is fine when one side signals the event and the other side waits for it.

Other types of interaction are possible but I would discourage from using them. These additional types are not simple and not that clearly defined, contain caveats, etc. The 3 methods above allow building complex applications.

To call a C++ finction, add to your assembly:

[DllImport("User32.dll")]
static extern Boolean MessageBeep(UInt32 beepType);

This tells that User32 has and entry point MessageBeep that your want to call. After that you can use it as any other function:

MessageBeep(0);

In a similar way you can call GetProcAddress or any other entry point in your own DLL.

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

6 Comments

How can I do the solution 1?
What is unclear in particular? Try reading this: msdn.microsoft.com/en-us/magazine/cc164193.aspx
The thing is that I think we use DllImport to call a function on a Dll on disk. The thing now is that I need to share a struct or a variable between two running processes.
To share data between processes you need to use shared memory regions. This is completely different subject.
I though about named pipes?
|
0

I will be using a named pipe so the two processes can communicate together easily! (C# server and C++ Client)

Thanks for the help everyone!

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.