PDA

View Full Version : How to detect a running application



yogesh
20th February 2009, 07:35
Hi,
I have a requirement where I need to start a separate background application (non gui)from one of my demo application. But it can so happen that the background application is already running.
Is there any API or class thorugh which I can determine whether the background application is already running and need not start it again. I am trying to use QProcess to start the background application from my demo application.

Thanks in advance.
Yogesh

wysota
20th February 2009, 08:00
Is it a 3rd party application (I mean the background app) or can you modify it? if so, you can use QSharedMemory for example.

talk2amulya
20th February 2009, 08:30
for windows specific check, here is how u do it:


bool URCLASS::alreadyExists(wchar_t * appFileName)
{

/// Create mutex named for the current exe and check for its existence
bool bResult = false;

// Don't separate these next two statements; GetLastError()
// must be the first call after the CreateMutex() call
hMutex = ::CreateMutex(NULL, TRUE, appFileName);
DWORD dwLastError = ::GetLastError();

// If the mutex already exists, return true to indicate that this exe
// is already running.
if (dwLastError == ERROR_ALREADY_EXISTS)
{
bResult = true;
}

return bResult;
}

is that what u need?

yogesh
20th February 2009, 08:50
Hi,
The background application is a server (that is again my own application )which can be accessed by any number of client applications running on the system. I want the mechanism to be generic so that I can use it across platform. can you please specify how to use QSharedMemory to get the desired result ?

Thanks in advance.
Regards,
Yogesh

wysota
20th February 2009, 10:22
In general in a fashion similar to this: SingleApplication.

The minimal possible implementation would be to create a shared memory block in the server and try to attach to it from the client. If the block already exists it means the server is running.

Another approach (without shared memory) is to open a local socket from within the server and connect to it from the client. If the connection is successful, it means the server is running.