Check - is app running

How to safely check if my program is running and
how to prevent the user from replacing my app with other one? (maybe simply add restriction to program files/myapp folder?)

And how to do this for every logged in user?
lavitzAsked:
Who is Participating?
 
pgnatyukCommented:
If it's your application, you can create a named object (mutex, for example) and check if it exists or not. So you will know that your application is running or not.
hMutex = ::CreateMutex(NULL, TRUE, "MyAppIsRunning");
DWORD nErr= ::GetLastError();
if (nErr == ERROR_ALREADY_EXISTS)
//It's running
Any time you can enumerate processes and find your process:
http://msdn.microsoft.com/en-us/library/ms682629(VS.85).aspx
Here is an example:
http://msdn.microsoft.com/en-us/library/ms682623(v=VS.85).aspx
>>how to prevent the user from replacing my app with other one?
I didn't understand what you mean. This is your app. Do you mean that a user may have an app with the same name? Set read-only attribute on the executable file.
0
 
lavitzAuthor Commented:
Ok, thanks. Mutex is very helpfull. But what if someone create mutex with name of my mutex? I was thinking about create random names. I mean, my service run my client app and then watch if it stay alive, so I can somehow inject random name of mutex. Or maybe there is simple solution for that?
0
 
lavitzAuthor Commented:
And ofcourse, application that i want watch is my app
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
belamkarvCommented:
you should use a global mutex;
precede the name of the mutex with "Global"

"Global\\MyAppIsRunning" in the above example.
This should help you to check if your application is being run by any user on the current system.

note that "Global" is case sensitive
0
 
pgnatyukCommented:
>>But what if someone create mutex with name of my mutex?
I do not think that this is possible.
You can use something like "My Application named GREAT is running. This is my named object."
If you want, so take a look:
http://msdn.microsoft.com/en-us/library/34wc6k1f(VS.80).aspx
char _tmpFileName[] = "MYAPPXXXXXX";
mkstemp(_tmpFileName);
 
 
0
 
MvanderKooijCommented:
or use a GUID as mutex name. I personally use this also for my installer, (Inno Setup) which checks if a certain mutex is already created. The it knows that the application is already running and wait's until it is closed.
0
 
lavitzAuthor Commented:
I mean, what if someone starts fake program that make mutex with same name as my. How to avoid that?
0
 
MvanderKooijCommented:
You can't, but why would someone bother?
0
 
pgnatyukCommented:
Really. Another application creates the same mutex. What for?
Actually it is a known pattern that is used when you need to have only one instance of your application. On start you create a named mutex. If the mutex is created successfully, no errors from GetLastError, your application begins. If you see ERROR_ALREADY_EXISTS you need to look for the main window of your application and bring it to front (SetForegroundWindow) and exit from the second instance.
It is required by Microsoft for any Windows Mobile application for example. On Mac OS X you don't need to write such things - it is by default, it is the standard behavior.
0
 
lavitzAuthor Commented:
Its rather need for some kind of watchdog that keep an eye on my other app. So, iam afraid that someone could start mutex with same name, and my watchdog will think that my app is running.
0
 
pgnatyukCommented:
I do not think that this mutex will be enough for the watchdog. If the mutex exists, it's better to check other things too. For example, send a message to your app (maybe simply WM_COPY) and the application should answer. This is the simplest case, but you will know if your application is still responding.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.