I am creating two independent C++ .dlls that both talk to the same piece of hardware. If both are connected at the same time, their commands will be mixed up and the hardware will behave incorrectly. So I need to ensure that only one is connected to the hardware at once (even if both DLLs are loaded and running).
Here is my idea so far:
- before connecting, the DLL creates a file in a known location
- when the DLL connects, it looks for the other DLL’s file, and refuses to connect if it is present.
- after disconnecting, the DLL deletes its file.
This seems simple enough, but I’m wondering if there’s an better way? Some kind of system-wide variable the DLLs can define? Simplicity is key here – the fewer lines of code, the better.
Use the CreateMutex function