Allowing only a single instance of a program

BlitzMax Forums/BlitzMax Programming/Allowing only a single instance of a program

kfprimm(Posted 2006) [#1]
Does anyone have a tweak to do this? I know how to do this but I don't know which module to look in to make the changes. I also need a way to pass the command line parameters of the second instance to the first instance.


Robert Cummings(Posted 2006) [#2]
No need to tweak. There is mutex code in code archives which merely gives your program a way to identify if it is already running.

Search for mutex etc, this has been covered a few times.


kfprimm(Posted 2006) [#3]
Sweet...now how would I get the parameters from the second instance?


kenshin(Posted 2006) [#4]
Have the second instance write the parameters to a temporary file before quitting. Then have the first instance read the parameters from the temporary file and delete it. If that's too messy, then you could also do the same type of thing using an environment variable or the registry. Just have the second instance dump it's parameters somewhere and then have the first instance read them.


FlameDuck(Posted 2006) [#5]
now how would I get the parameters from the second instance?
IPC - Inter-Process Communication. Basicly, you send a message from the second instance to the first. I'm sure some Win32 API shark is going to come around soon enough with an implementation for Windows. Or you could look at MSDN if you're feeling particularly adventureous.


kfprimm(Posted 2006) [#6]
cool, I'll have a look around.

kenshin,
I thought of using that method but I think its too messy.


Picklesworth(Posted 2006) [#7]
I can give you some C++ example code from my web plugin system which does inter-process communication using two DLLs.

Since it's kind of ugly and unfinished code, the best solution, of course, is to search for WM_COPYDATA on Google.
It is a window message which takes a pointer to a variable. It makes this variable accessible to the target window's process and it sends the message to that process.



There may be a bit of complexity using Mutexes for this job, though, as the whole inter-process communication thing requires that you have a window handle for the process. (I'm not sure about windowless processes here... maybe someone else knows that).

Can we get an actual process handle from a mutex, or is it just one single string that tells us some program, somewhere, is running and wrote the same mutex as the one we're looking for?

Nonetheless, in order to send messages between your program instances, you would need some way for one of the instances of your program to blindly seek out the other one's window handle using FindWindow, some kind of FindProcess thing (does one exist?), or mysterious mutex functionality.


I, for one, have always despised the concept of searching for window titles to find another application. There may be a better way.


Robert Cummings(Posted 2006) [#8]
Dudes, its in code archives already. No need for C++ or MSDN.

I'd link you but I haven't got time to look right now. The parameter I suspect you mention is the hwnd, which is easy to find in blitzmax... good luck