I currently have a resource light “unmanaged” Windows C++ application using:
-
CreateMutexto ensure there is at most a single instance of the application. -
COPYDATASTRUCTto pass command line parameters over to that single instance, should another instance attempt to start (after sending data, it will then quit).
The use case is a simple one: launch a new application instance to open a file specified on the command line, or otherwise open the file in a previously-launched application instance.
How could I achieve the same behaviour in a Haskell program?
It looks like
CreateMutexisn’t offered in theWin32package that comes with GHC. You’ll need to either bind to it yourself using the FFI or emulate it in a cross-platform way. One trick commonly used on Linux is to choose a particular filename to act as the “mutex”; if it doesn’t exist, you create it and write down how to contact you then delete it when you exit, while if it does, you read the contact information out of it to find out how to connect to the already running process. This approach does have downsides: if either your app or the computer as a whole goes down, you’ll have stale contact information.For IPC, I think there’s a number of cross-platform interfaces. Wikipedia has an excellent list; especially suitable for doing this task in Haskell are sockets, (named) pipes, and dbus. For the latter two, you may need Cygwin, since they ultimately depend on getting the unix package installed.