Autor Beitrag
accessViolation
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 68

Win
Delphi 5 Enterpr.
BeitragVerfasst: Sa 09.12.06 17:18 
Hallo zusammen!

Ich programmiere gerade ein System, das aus mehreren Teilanwendungen (einzelne Exe-Dateien) besteht.

Jede der Teilanwendungen darf auf einem Rechner aber nur einmal laufen. D.h. wenn eine Teilanwendung läuft, muss ein erneutes Starten dieser Anwendung eine Meldung erzeugen.

Hat jemand eine Idee, wie man sowas macht?

Viele Grüße

Dietmar
Marco D.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2750

Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
BeitragVerfasst: Sa 09.12.06 18:00 
Aus dem Easy Delphi-Helper:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
uses TlHelp32;

function IsExeRunning(const AExeName: string): boolean;
var
  h: THandle;
  p: TProcessEntry32;
begin
  Result := False;

  p.dwSize := SizeOf(p);
  h := CreateToolHelp32Snapshot(TH32CS_SnapProcess, 0);
  try
    Process32First(h, p);
    repeat
      Result := AnsiUpperCase(AExeName) = AnsiUpperCase(p.szExeFile);
    until Result or (not Process32Next(h, p));
  finally
    CloseHandle(h);
  end;
end;

_________________
Pascal keeps your hand tied. C gives you enough rope to hang yourself. C++ gives you enough rope to shoot yourself in the foot
Chryzler
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1097
Erhaltene Danke: 2



BeitragVerfasst: Sa 09.12.06 18:52 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
function CheckInstance(ID: PChar): Boolean;
begin
  CreateMutex(nil, True, ID);
  result := GetLastError <> ERROR_ALREADY_EXISTS;
end;

Beim Start von deinem Programm rufst du die Funktion auf. Wenn sie true zurückgibt, ist nur eine Instanz von deinem Programm vorhanden, wenn sie false zurückgibt, läuft dein Programm bereits. Als ID übergibst du einen String, z.B. Application.ExeName, du kannst aber auch nur 'blub' nehmen.
accessViolation Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 68

Win
Delphi 5 Enterpr.
BeitragVerfasst: Sa 09.12.06 20:20 
Hallo und vielen Dank Euch beiden.

Habe beide Methoden ausprobiert und beide funktionieren prinzipiell. Nur Marcos Methode kann ich nicht verwenden, denn sie eignet sich naturgemäß nicht zur Überprüfung, ob die Exe des "eigenen" Programms läuft, denn die läuft immer...

Dann würde mein Aufruf also so aussehen:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
procedure TForm1.FormCreate(Sender: TObject);
begin
   if not CheckInstance('blub') then begin
      showmessage('Dieses Programm läuft bereits.');
      Application.Terminate;
   end;
end;


Ist Application.Terminate an dieser Stelle ok, oder ist das eher die Holzhammer-Methode?

Viele Grüße
Dietmar
Chryzler
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1097
Erhaltene Danke: 2



BeitragVerfasst: Sa 09.12.06 20:36 
Ist ok, denke ich. Close kannst du warscheinlich nicht aufrufen, da du im OnCreate-Ereignis bist, und die Form noch nicht ganz "erstellt" wurde.


Zuletzt bearbeitet von Chryzler am Mi 02.01.08 19:02, insgesamt 1-mal bearbeitet
Sinspin
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1340
Erhaltene Danke: 120

Win 10
RIO, CE, Lazarus
BeitragVerfasst: Sa 09.12.06 21:30 
Ich würde diese Prüfung nicht an dieser Stelle, sondern im Hauptprogramm vor Application.Initialize; vornehmen. Dann hast du erst garnicht mit Application zu tun. So ist das Programm wieder beendet bevor überhaut erst ein Fester erstellt wird.

_________________
Wir zerstören die Natur und Wälder der Erde. Wir töten wilde Tiere für Trophäen. Wir produzieren Lebewesen als Massenware um sie nach wenigen Monaten zu töten. Warum sollte unser aller Mutter, die Natur, nicht die gleichen Rechte haben?
Reinhard Kern
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 591
Erhaltene Danke: 14



BeitragVerfasst: So 10.12.06 13:48 
user profile iconSinspin hat folgendes geschrieben:
Ich würde diese Prüfung nicht an dieser Stelle, sondern im Hauptprogramm vor Application.Initialize; vornehmen. Dann hast du erst garnicht mit Application zu tun. So ist das Programm wieder beendet bevor überhaut erst ein Fester erstellt wird.


Hallo,

ja, genau, das sieht dann bei mir so aus:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
begin
  if IsFirstInstance ('BPMLog4App') then
    begin
    Application.Initialize;
      PrepareAppRun;
    Application.CreateForm(TBLMainForm, BLMainForm);
    Application.Run;
      DoneAppRun;
    end;
end.


PrepareAppRun und DoneAppRun erledigem hauptsächlich den Umgang mit Setup-Daten. IsFirstInstance erledigt alles, was gebraucht wird, wenn das Programm NICHT neu starten soll, d.h. die bereits laufende Version wird sichtbar gemacht und in den Vordergrund gebracht - das ist das, was der Benutzer in dem Fall intuitiv erwarten würde.

Braucht man das nicht, kann man die 2 Hälfte weglassen.

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
Function IsFirstInstance (AName : PChar) : bool;
var MName : array [0..80] of char;
    FIWindow,OWindow,PopupWindow : THandle;
    FGThread,OtherThread : THandle;
begin
StrCopy (RKLAppName,AName);
FirstInstance := true;
StrCopy (MName,RKLAppName);
StrCat (MName,'_Mutex');
RKMutexHandle := CreateMutex (nil,false,MName);
if RKMutexHandle > 0 then
  WaitForSingleObject(RKMutexHandle,5000);
   { wait until other process has created his window }
StrCopy (MName,RKLAppName);
StrCat (MName,'_Class');
FIWindow := FindWindow (MName,nil);
  { window of first instance }
if FIWindow > 0 then
  begin
  FirstInstance := false;
    { there is a owner window of class "TApplication" }
  OWindow := GetWindow (FIWindow,GW_OWNER);
  if OWindow > 0 then
    if IsIconic (OWindow) then
      ShowWindow (OWindow,sw_Restore);
  PopupWindow := GetLastActivePopup (FIWindow);
  if PopupWindow > 0 then FIWindow := PopupWindow;
    { SetForeground.. flashes only, if not called from
      the app in the foreground (XP) }

  FGThread := GetWindowThreadProcessID (GetForegroundWindow,nil);
  OtherThread := GetWindowThreadProcessID (FIWindow,nil);
  AttachThreadInput (FGThread,OtherThread,true);
  SetForegroundWindow (FIWindow);
  AttachThreadInput (FGThread,OtherThread,false);
  end;
IsFirstInstance := FirstInstance;
end;


Gruss Reinhard