Autor Beitrag
Thunder00
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 52



BeitragVerfasst: Mo 28.07.03 17:32 
Hallo,

Dazu hab ich das im EDH2000 gefunden:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
function ExeFileIsRunning(ExeFile: string): boolean;
var
  H:word;
begin
  H := CreateFile(PChar(ExeFile),
                  GENERIC_READ,
                  0,
                  nil,
                  OPEN_EXISTING,
                  0,
                  0);
  Result := (H >= 65535);
  CloseHandle(H);
end;

Und dazu diese Schleife geschrieben:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
procedure TForm1.Button2Click(Sender: TObject);
begin
  Repeat
    If ExeFileIsRunning(Progdatei) = False then
    begin
      ShellExecute(0,
                 Nil,
                 PChar(Progdatei),
                 '',
                 '',
                 SW_NORMAL);
      Wait(Sek + 1000);
    end;
  Until
    Stop = True;
end;

Die Prozedur "wait" sieht so aus:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
procedure Wait(time: word);
var Start: Integer;
begin
  Start:=GetTickCount;
  while GetTickCount-Start <= time do
    application.ProcessMessages;
end;

Ebenfalls ausm EDH2000

so. wenn ich das alles mit Notepad teste, startet er Notepad immer und immer wieder und irgendwann is Notepad 200mal geöffnet. Er soll Notepad aber nur starten, wenn es noch nicht gestartet ist, bzw. wenn es beendet wurde oder abgestürtzt ist.

Wie?? Wo is der Fehler?? Danke im VOraus

Moderiert von user profile iconTino: Code- durch Delphi-Tags ersetzt.

_________________
If you can't make it good, at least make it looks good. (Bill Gates)
MathiasSimmack
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Mo 28.07.03 17:39 
Du findest den Fehler nicht?
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
If ExeFileIsRunning(Progdatei) = False then
begin
  ShellExecute(0,
    Nil,
    PChar(Progdatei),
    '',
    '',
    SW_NORMAL);
  Wait(Sek + 1000);
end;

Mal in verständliches Deutsch und Pseudo-Code übersetzen, was du da programmiert hast:
ausblenden Quelltext
1:
2:
Wenn ExeFileLäuft('Notepad.exe') dann
  NotePad_mit_ShellExecute_starten

?
Terra23
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 872

Win 8
Delphi 7
BeitragVerfasst: Mo 28.07.03 17:56 
@Mathias: Ich will mich ja nicht einmischen geschweige denn behaupten, daß ich mich damit auskenne, aber heißt

ausblenden Delphi-Quelltext
1:
If ExeFileIsRunning(Progdatei) = False then..					


nicht übersetzt: Wenn die Exe-Datei nicht läuft???

_________________
Hasta La Victoria Siempre
MathiasSimmack
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Mo 28.07.03 18:06 
Doch, misch dich ruhig ein! Ich habe ja nie behauptet, dass ich fehlerlos bin. Ich gestehe: ich habe das FALSE ignoriert. Das liegt aber daran, dass ich so nicht programmiere. Ich schreibe:
ausblenden Delphi-Quelltext
1:
if (dies_und_das)					

bzw.
ausblenden Delphi-Quelltext
1:
if (not dies_und_das)					

:!:

Auf der anderen Seite wollte ich nicht glauben, dass im EDH Quark steht. Und siehe da: bei mir geht´s. Notepad startet nur, wenn es nicht läuft bzw. beendet wurde.
Motzi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2931

XP Prof, Vista Business
D6, D2k5-D2k7 je Prof
BeitragVerfasst: Mo 28.07.03 18:37 
Der Fehler liegt IMHO in der Funktion ExeFileIsRunning... und zwar in der Zeile:
ausblenden Delphi-Quelltext
1:
H := CreateFile(PChar(ExeFile), GENERIC_READ, 0nil, OPEN_EXISTING, 00);					

Denn Lese-Zugriff bekommt man auf eine exe (fast) immer (auch wenn sie ausgeführt wird)!

_________________
gringo pussy cats - eef i see you i will pull your tail out by eets roots!
MathiasSimmack
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Mo 28.07.03 19:34 
Das erklärt aber nicht, warum es bei mir klappt. Und ich habe bloß den Code von oben kopiert.
Thunder00 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 52



BeitragVerfasst: Mo 28.07.03 21:34 
ja hmm...woran kann das liegen.

bitte helft mir!

_________________
If you can't make it good, at least make it looks good. (Bill Gates)
Luckie
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Di 29.07.03 02:55 
Mach besser einen SnapShot und kuck ob die Exe dort auftaucht.
Thunder00 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 52



BeitragVerfasst: Mi 30.07.03 17:29 
wie n Snapshot???

_________________
If you can't make it good, at least make it looks good. (Bill Gates)
Luckie
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Mi 30.07.03 21:57 
Cyrus
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 56



BeitragVerfasst: Do 31.07.03 09:27 
Ich hab da auch noch was gefunden:

Ein Programm starten und Warten bis es Beendet ist:
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:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
// With CreateProcess: 
//***************************************************** 

{1} 

function WinExecAndWait32(FileName: string; Visibility: Integer): Longword; 
var { by Pat Ritchey } 
  zAppName: array[0..512of Char; 
  zCurDir: array[0..255of Char; 
  WorkDir: string
  StartupInfo: TStartupInfo; 
  ProcessInfo: TProcessInformation; 
begin 
  StrPCopy(zAppName, FileName); 
  GetDir(0, WorkDir); 
  StrPCopy(zCurDir, WorkDir); 
  FillChar(StartupInfo, SizeOf(StartupInfo), #0); 
  StartupInfo.cb          := SizeOf(StartupInfo); 
  StartupInfo.dwFlags     := STARTF_USESHOWWINDOW; 
  StartupInfo.wShowWindow := Visibility; 
  if not CreateProcess(nil
    zAppName, // pointer to command line string 
    nil// pointer to process security attributes 
    nil// pointer to thread security attributes 
    False, // handle inheritance flag 
    CREATE_NEW_CONSOLE or // creation flags 
    NORMAL_PRIORITY_CLASS, 
    nil//pointer to new environment block 
    nil// pointer to current directory name 
    StartupInfo, // pointer to STARTUPINFO 
    ProcessInfo) // pointer to PROCESS_INF 
    then Result := WAIT_FAILED 
  else 
  begin 
    WaitForSingleObject(ProcessInfo.hProcess, INFINITE); 
    GetExitCodeProcess(ProcessInfo.hProcess, Result); 
    CloseHandle(ProcessInfo.hProcess); 
    CloseHandle(ProcessInfo.hThread); 
  end
end{ WinExecAndWait32 } 


procedure TForm1.Button1Click(Sender: TObject); 
begin 
  WinExecAndWait32('notepad.exe', False, True); 
end

{*******************************} 

{2} "Anti-Freezing": 

function ExecAndWait(const FileName: stringconst CmdShow: Integer): Longword; 
var { by Pat Ritchey } 
  zAppName: array[0..512of Char; 
  zCurDir: array[0..255of Char; 
  WorkDir: string
  StartupInfo: TStartupInfo; 
  ProcessInfo: TProcessInformation; 
  AppIsRunning: DWORD; 
begin 
  StrPCopy(zAppName, FileName); 
  GetDir(0, WorkDir); 
  StrPCopy(zCurDir, WorkDir); 
  FillChar(StartupInfo, SizeOf(StartupInfo), #0); 
  StartupInfo.cb          := SizeOf(StartupInfo); 
  StartupInfo.dwFlags     := STARTF_USESHOWWINDOW; 
  StartupInfo.wShowWindow := CmdShow; 
  if not CreateProcess(nil
    zAppName, // pointer to command line string 
    nil// pointer to process security attributes 
    nil// pointer to thread security attributes 
    False, // handle inheritance flag 
    CREATE_NEW_CONSOLE or // creation flags 
    NORMAL_PRIORITY_CLASS, 
    nil//pointer to new environment block 
    nil// pointer to current directory name 
    StartupInfo, // pointer to STARTUPINFO 
    ProcessInfo) // pointer to PROCESS_INF 
    then Result := WAIT_FAILED 
  else 
  begin 
    while WaitForSingleObject(ProcessInfo.hProcess, 0) = WAIT_TIMEOUT do 
    begin 
      Application.ProcessMessages; 
      Sleep(50); 
    end
    
    // or: 
    repeat 
      AppIsRunning := WaitForSingleObject(ProcessInfo.hProcess, 100); 
      Application.ProcessMessages; 
      Sleep(50); 
    until (AppIsRunning <> WAIT_TIMEOUT); 
    }
 

    WaitForSingleObject(ProcessInfo.hProcess, INFINITE); 
    GetExitCodeProcess(ProcessInfo.hProcess, Result); 
    CloseHandle(ProcessInfo.hProcess); 
    CloseHandle(ProcessInfo.hThread); 
  end
end{ WinExecAndWait32 } 

procedure TForm1.Button1Click(Sender: TObject); 
begin 
  ExecAndWait('C:\Programme\WinZip\WINZIP32.EXE', SW_SHOW); 
end


{3} 

{--WinExecAndWait32V2 ------------------------------------------------} 
{: Executes a program and waits for it to terminate 
@Param FileName contains executable + any parameters 
@Param Visibility is one of the ShowWindow options, e.g. SW_SHOWNORMAL 
@Returns -1 in case of error, otherwise the programs exit code 
@Desc In case of error SysErrorMessage( GetlastError ) will return an 
  error message. The routine will process paint messages and messages 
  send from other threads while it waits. 
}
{ Created 27.10.2000 by P. Below 
-----------------------------------------------------------------------}
 

function WinExecAndWait32V2(FileName: string; Visibility: Integer): DWORD; 
  procedure WaitFor(processHandle: THandle); 
  var 
    Msg: TMsg; 
    ret: DWORD; 
  begin 
    repeat 
      ret := MsgWaitForMultipleObjects(1{ 1 handle to wait on } 
        processHandle, { the handle } 
        False, { wake on any event } 
        INFINITE, { wait without timeout } 
        QS_PAINT or { wake on paint messages } 
        QS_SENDMESSAGE { or messages from other threads } 
        ); 
      if ret = WAIT_FAILED then Exit; { can do little here } 
      if ret = (WAIT_OBJECT_0 + 1then 
      begin 
          { Woke on a message, process paint messages only. Calling 
            PeekMessage gets messages send from other threads processed. }
 
        while PeekMessage(Msg, 0, WM_PAINT, WM_PAINT, PM_REMOVE) do 
          DispatchMessage(Msg); 
      end
    until ret = WAIT_OBJECT_0; 
  end{ Waitfor } 
var { V1 by Pat Ritchey, V2 by P.Below } 
  zAppName: array[0..512of char; 
  StartupInfo: TStartupInfo; 
  ProcessInfo: TProcessInformation; 
begin { WinExecAndWait32V2 } 
  StrPCopy(zAppName, FileName); 
  FillChar(StartupInfo, SizeOf(StartupInfo), #0); 
  StartupInfo.cb          := SizeOf(StartupInfo); 
  StartupInfo.dwFlags     := STARTF_USESHOWWINDOW; 
  StartupInfo.wShowWindow := Visibility; 
  if not CreateProcess(nil
    zAppName, { pointer to command line string } 
    nil{ pointer to process security attributes } 
    nil{ pointer to thread security attributes } 
    False, { handle inheritance flag } 
    CREATE_NEW_CONSOLE or { creation flags } 
    NORMAL_PRIORITY_CLASS, 
    nil{ pointer to new environment block } 
    nil{ pointer to current directory name } 
    StartupInfo, { pointer to STARTUPINFO } 
    ProcessInfo) { pointer to PROCESS_INF } then 
    Result := DWORD(-1{ failed, GetLastError has error code } 
  else 
  begin 
    Waitfor(ProcessInfo.hProcess); 
    GetExitCodeProcess(ProcessInfo.hProcess, Result); 
    CloseHandle(ProcessInfo.hProcess); 
    CloseHandle(ProcessInfo.hThread); 
  end{ Else } 
end{ WinExecAndWait32V2 } 


procedure TForm1.Button1Click(Sender: TObject); 
begin 
  WinExecAndWait32V2('notepad.exe', SW_SHOWNORMAL); 
end


// With ShellExecuteEx: 
//***************************************************** 

{1} 

uses 
  ShellApi; 

procedure ShellExecute_AndWait(FileName: string; Params: string); 
var 
  exInfo: TShellExecuteInfo; 
  Ph: DWORD; 
begin 
  FillChar(exInfo, SizeOf(exInfo), 0); 
  with exInfo do 
  begin 
    cbSize := SizeOf(exInfo); 
    fMask := SEE_MASK_NOCLOSEPROCESS or SEE_MASK_FLAG_DDEWAIT; 
    Wnd := GetActiveWindow(); 
    ExInfo.lpVerb := 'open'
    ExInfo.lpParameters := PChar(Params); 
    lpFile := PChar(FileName); 
    nShow := SW_SHOWNORMAL; 
  end
  if ShellExecuteEx(@exInfo) then 
    Ph := exInfo.HProcess 
  else 
  begin 
    ShowMessage(SysErrorMessage(GetLastError)); 
    Exit; 
  end
  while WaitForSingleObject(ExInfo.hProcess, 50) <> WAIT_OBJECT_0 do 
    Application.ProcessMessages; 
  CloseHandle(Ph); 
end

procedure TForm1.Button1Click(Sender: TObject); 
begin 
  ShellExecute_AndWait('FileName''Parameter'); 
end



{*******************************} 

{2} 

function ShellExecute_AndWait(Operation, FileName, Parameter, Directory: string
  Show: Word; bWait: Boolean): Longint; 
var 
  bOK: Boolean; 
  Info: TShellExecuteInfo; 

  ****** Parameters ****** 
  Operation: 

  edit  Launches an editor and opens the document for editing. 
  explore Explores the folder specified by lpFile. 
  find Initiates a search starting from the specified directory. 
  open Opens the file, folder specified by the lpFile parameter. 
  print Prints the document file specified by lpFile. 
  properties Displays the file or folder's properties. 

  FileName: 

  Specifies the name of the file or object on which 
  ShellExecuteEx will perform the action specified by the lpVerb parameter. 

  Parameter: 

  String that contains the application parameters. 
  The parameters must be separated by spaces. 

  Directory: 

  specifies the name of the working directory. 
  If this member is not specified, the current directory is used as the working directory. 

  Show: 

  Flags that specify how an application is to be shown when it is opened. 
  It can be one of the SW_ values 

  bWait: 

  If true, the function waits for the process to terminate 
}
 
begin 
  FillChar(Info, SizeOf(Info), Chr(0)); 
  Info.cbSize := SizeOf(Info); 
  Info.fMask := SEE_MASK_NOCLOSEPROCESS; 
  Info.lpVerb := PChar(Operation); 
  Info.lpFile := PChar(FileName); 
  Info.lpParameters := PChar(Parameter); 
  Info.lpDirectory := PChar(Directory); 
  Info.nShow := Show; 
  bOK := Boolean(ShellExecuteEx(@Info)); 
  if bOK then 
  begin 
    if bWait then 
    begin 
      while 
        WaitForSingleObject(Info.hProcess, 100) = WAIT_TIMEOUT 
        do Application.ProcessMessages; 
      bOK := GetExitCodeProcess(Info.hProcess, DWORD(Result)); 
    end 
    else 
      Result := 0
  end
  if not bOK then Result := -1
end;


vielleicht hilfts dir weiter

Greetz Cyrus

_________________
Wer glaub er ist, hört auf zu werden!
Delphi Rulez!!!