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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.