Hello.
Any way of preventing multiple instances of executables?

I'm writing a program that uses a file, it would be quite messy if someone would start another instance of same program.

If it depends on OS, I would like to see solutions for windows and unix-type (linux) systems.

Thanx

If You have No restrictions on editing your file, i guess you can write something like

a single letter in the starting of the document, something like
a single digit,
{0,1}

Then in your program

int main()
{
//Code to open file.....
//Then check 

if (filein[0]='0')//Here "filein" is the file-stream with which you have opened
{
cout<<"Another run of this  Program Already Using File , Please Switch it off and try again";
cin.get();
}
else if (infile[0]=='1')
{
//Write "1"in place of "0" 
//Then 
//Normal running of the program
}
}

I might not be that clear in my explaination but i hope it would be helpful .

Eh... that's dangerous enough. What happens if the program crashes for some reason? I think the solution I posted from msdn is better in regard to that, though not portable. Make a header file, make an instance of the object, and test the return value of the "IsAnotherInstanceRunning" method.

Eh... that's dangerous enough. What happens if the program crashes for some reason? I think the solution I posted from msdn is better in regard to that, though not portable. Make a header file, make an instance of the object, and test the return value of the "IsAnotherInstanceRunning" method.

I think. nothing happens if the second instance can't open opened (by the 1st instance) flag file and die.

I've tried solution with IsAnotherInstanceRunning(), and it works great.

But since the header file includes windows.h, it can't run on unix-types... I'd appreciate some help on that filed

I don't run linux so I don't know if this'll work, but here's a solution I've found that'll work on linux: http://www.linuxquestions.org/questions/programming-9/restricting-multiple-instance-of-a-program-242069/? It uses a lock file but the lock is killed when the program ends, even if killed or crashed etc. What you do is have an #ifdef switch. Check for a windows maching, and if it's true use the code I first linked to above. Then #elif for a linux machine and paste the code from this link... i.e.

#ifdev WIN32
// windows code

#elif <whatever else>
// linux code

#endif

...which is the correct solution for Linux.

On Windows, register a new window message (specific to your application) and broadcast it when your program is considering starting up.

Any existing instance should respond that it already exists. The new instance can then terminate. If there isn't any response, then there is no other instance.

There are various ways to do this involving mutexes and the like... but just sticking to a window message won't lock.

It also gives the new instance a way of communicating extra information (like files to open, or "focus yourself", etc) to the existing instance.


There isn't any good way to do this "cross-platform". Some people will suggest playing with map files, but they aren't infallable and doing it is platform-specific anyway... so you should heed twomers advice. Be sure to check through the Pre-defined C/C++ Compiler Macros pages to make sure your code will compile with different compilers and OSes.

Good luck!

[edit] All the above is according to memory --I could be wrong on some small account. There are some very good pages on the net about bullet-proof application mutexing, but I don't remember where they are ... alas.

This article has been dead for over six months. Start a new discussion instead.