Devgrapher
Devgrapher

Reputation: 87

What is the best way to communicate with DLLs each other

My program has several modules which is dll. And one of dlls should notify to the others with some data. What would you do?

Use shared memory map and event? or give them a callback function and relaying of program(exe)?

Please let me know the general way.

Upvotes: 1

Views: 640

Answers (2)

tp1
tp1

Reputation: 288

Here's a nice trick for communication between modules:

struct A { int a; int b; };
void dll1(A &a);
void dll2(A &a);
int main() {
  A a;
  while(1) {
     dll1(a);
     dll2(a);
  }
}

This will put the whole dll1 to a single function. It can communicate with the other dll by modifying the struct A. Now this will have a problem that main() needs to be modified when you add new dlls to the system. Also struct A will need to be modified when the communication interface between the dlls will change. Removing data from struct A might become impossible, but adding new data will still work fine.

Upvotes: 0

Matt Kline
Matt Kline

Reputation: 10477

If all of the DLLs are loaded into the same thread, you should be able to just call the DLL's exported functions as you would call any other ones. If each module is running in a different thread, you can use standard lock-based techniques with semaphores, mutexes, and critical sections, or atomic operations with memory barriers. If each module is running in it's own process, look up inter-process communication techniques such as named pipes or connecting via the network stack through localhost.

The fact that you load functions from a DLL doesn't really imply anything special. The only complication arises if you're going to try to catch exceptions through the DLL barrier, which can get complicated.

Upvotes: 1

Related Questions