Under Windows 3.1 it was very easy to create a program that wouldn't allow a user to run it more than once. You simply examined the hPrevInstance parameter in WinMain and if it was not NULL, you were already running.
However 32-bit apps can't do this because hPrevInstance is always NULL for these programs. So what's the solution? One possible answer is to use a MUTEX (or mutual exclusion) object. These objects are really for synchronizing multiple theads, but there is no reason you can't use them to signify that a program is running.
For an MFC program, you simply place a member variable (say mutext of type HANDLE. In the constructor, set this variable to NULL. Then add the following code to the beginning of your application object's InitInstance method:
// Only allow one instance
mutex=::CreateMutex(NULL,FALSE,"onetimeapp_mutex");
if (mutex) // if it failed, what to do?
{
if (::WaitForSingleObject(mutex,0)==WAIT_TIMEOUT)
{
AfxMessageBox("Please only run one copy of onetime");
return FALSE;
}
}
.
.
.
You could put the same kind of code in WinMain if you aren't using MFC.
You should also release the mutex when you are done. For MFC, try this code in your CWinApp::ExitInstance:
if (mutex)
{
::ReleaseMutex(mutex);
::CloseHandle(mutex);
}
return CWinApp::ExitInstance();