Avatar billede yates Nybegynder
16. januar 2006 - 08:04 Der er 2 kommentarer

Hvornår er konsol-app'en færdig?

Hej,

Jeg skriver på en app som aktiverer en konsol-app X via shellexecute. X laver et output som skal anvendes umiddelbart derefter, men jeg er nødt til først at aktivere dette EFTER at X
har aflsuttet sig selv.
Så derfor: Hvordan tjekker jeg hvornår X som er kaldt via shellexecute er helt færdig?

Tak for svaret.
Y.
Avatar billede pidgeot Nybegynder
16. januar 2006 - 12:58 #1
Du bruger ShellExecuteEx:

// Execute the Windows Calculator and pop up
// a message when the Calc is terminated.
uses ShellApi;
...
var
  SEInfo: TShellExecuteInfo;
  ExitCode: DWORD;
  ExecuteFile, ParamString, StartInString: string;
begin
  ExecuteFile:='c:\Windows\Calc.exe';
 
  FillChar(SEInfo, SizeOf(SEInfo), 0);
  SEInfo.cbSize := SizeOf(TShellExecuteInfo);
  with SEInfo do begin
    fMask := SEE_MASK_NOCLOSEPROCESS;
    Wnd := Application.Handle;
    lpFile := PChar(ExecuteFile);
{
ParamString can contain the
application parameters.
}
// lpParameters := PChar(ParamString);
{
StartInString specifies the
name of the working directory.
If ommited, the current directory is used.
}
//  lpDirectory := PChar(StartInString);
    nShow := SW_SHOWNORMAL;
  end;
  if ShellExecuteEx(@SEInfo) then begin
    repeat
      Application.ProcessMessages;
      GetExitCodeProcess(SEInfo.hProcess, ExitCode);
    until (ExitCode <> STILL_ACTIVE) or
      Application.Terminated;
    ShowMessage('Calculator terminated');
  end
  else ShowMessage('Error starting Calc!');
end;

Eksempel fra http://delphi.about.com/od/windowsshellapi/l/aa082499.htm
Avatar billede psycosoft-funware Nybegynder
16. januar 2006 - 17:08 #2
function GetDosOutput(Const CommandLine: String): String;
var
  SA: TSecurityAttributes;
  SI: TStartupInfo;
  PI: TProcessInformation;
  StdOutPipeRead, StdOutPipeWrite: THandle;
  WasOK: Boolean;
  Buffer: Array[0..255] of Char;
  BytesRead: Cardinal;
  WorkDir, Line: String;
begin
Application.ProcessMessages;
with SA do
begin
nLength := SizeOf(SA);
bInheritHandle := True;
lpSecurityDescriptor := Nil;
end;
CreatePipe(StdOutPipeRead, StdOutPipeWrite, @SA, 0);
try
with SI do
begin
FillChar(SI, SizeOf(SI), 0);
cb := SizeOf(SI);
dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
wShowWindow := SW_HIDE;
hStdInput := GetStdHandle(STD_INPUT_HANDLE);
hStdOutput := StdOutPipeWrite;
hStdError := StdOutPipeWrite;
end;
WorkDir := 'C:\';
WorkDir := '';
WasOK := CreateProcess(nil, PChar(CommandLine), nil, nil, True, 0, nil, nil,SI, PI);
CloseHandle(StdOutPipeWrite);
if not WASOK then
raise Exception.Create('Could Not Execute Command Line!')
else
try
Line := '';
repeat
WasOK := ReadFile(StdOutPipeRead, Buffer, 255, BytesRead, nil);
if BytesRead > 0 then
begin
Buffer[BytesRead] := #0;
Line := Line + Buffer;
end;
until not WasOK or (BytesRead = 0);
WaitForSingleObject(PI.hProcess, INFINITE);
finally
CloseHandle(PI.hThread);
CloseHandle(PI.hProcess);
end;
finally
Result := Line;
CloseHandle(StdOutPipeRead);
end;
end;

//denne kode afvikler, afslutter og modtager konsol outputtet...

procedure TForm1.Button1.Colick(Sender: TObject);
begin
Memo1.Text := GetDosOutput('dinkonsolapp.exe');
end;
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester