Home
English
Informationen
Downloads
Buchtipps
Autor
Links

Mit der API - Funktion : FindWindow kontrollieren,
ob die Anwendung schon läuft .

Manchmal darf ein Proramm nur einmal gestartet werden. z.B. Eine serielle
Schnittstelle kann nur einmal geöffnet werden. Mit der API - Funktion
FindWindow() kann man ganz leicht feststellen, ob die Anwendung schon läuft.
Man muss nicht immer mit Mutex benutzen.

Beschreibung :
Nach dem Programmstart sucht die Funktion FindWindow, das Programm mit dem
Windowtitel : RunApp.  Wenn das Programm nicht gefunden wird, gibt die Fkt.
eine NULL zurück. Das Programm läuft noch nicht. Der Titel des Programmes
muss ein anderer sein.Danach muss der Windowtitel, der Anwendung noch geändert
werden, sonst findet es sich selbst.

Funktion :

HWND FindWindow ( LPCTSTR lpClassName, LPCTSTR lpWindowName );

Quellcode :

//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
//---------------------------------------------------------------------------
USEFORM("RunApp.cpp", Form1);
//---------------------------------------------------------------------------
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
       HWND nErr = 0;
       try
       {
               nErr = FindWindow( NULL , "RunApp" );

               if( nErr != NULL )
               {
                       ShowMessage( "Programm läuft schon !" );
                       return 1;
               }
                 Application->Initialize();
                Application->Title = "RunApp";
                 Application->CreateForm(__classid(TForm1), &Form1);
                 Application->Run();
       }
       catch (Exception &exception)
       {
                 Application->ShowException(&exception);
       }
       catch (...)
       {
                 try
                 {
                         throw Exception("");
                 }
                 catch (Exception &exception)
                 {
                         Application->ShowException(&exception);
                 }
       }
       return 0;
}
//---------------------------------------------------------------------------
 

[Home] [English] [Informationen] [Downloads] [Buchtipps] [Autor] [Links]