WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
// neuen Mutex creieren
HANDLE hInstanceMutex = ::CreateMutex(NULL,TRUE, "MUTEXNAME");
// wenn Mutex "MUTEXNAME" schon existiert, d.h. die Anwendung schon
// einmal geöffnet war, wird der Fehler ERROR_ALREADY_EXISTS erzeugt
if (GetLastError() == ERROR_ALREADY_EXISTS)
{
// Handle auf den Mutex zerstören
if (hInstanceMutex) CloseHandle(hInstanceMutex);
// Anwendung beenden
return 0;
}
// normale Abarbeitung der Anwendung
try
{
Application->Initialize();
Application->CreateForm(__classid(TForm1), &Form1);
Application->Run();
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
// nach normalem Beenden der Anwendung den Mutex wieder freigeben
ReleaseMutex(hInstanceMutex);
CloseHandle(hInstanceMutex);
return 0;
}