The best way I know of is:
Attempt to Create a named mutex. If it succeeds, there is no existing app.
If it fails, then there is an app.
At that point, many people recommend looking for the app by process name and
then killing the process but there's a problem with that. You can never
really be sure that no other app on the machine is using the same name even
though it's a different application.
You do want to use EnumWindows from the Win32API to get applications that
match your app name but then the safest way to know you're killing the
correct application is to use the SendMessage API function to send a message
to the existing app.
If the existing app is your app, and you program it to do so, it will
recognize the message you send and shut down. If it is a similarly named
app, but not your app, then it will not recognize the message and will
discard it.
Hope that helps.
Dale
Susan said:
I am running an application in the background. I need to be able to see
when another instance of that app is opened, and close the first instance of
the app. Anyone know how to do that? I'd appreciate any information.