Einzelnen Beitrag anzeigen

Klaus01

Registriert seit: 30. Nov 2005
Ort: München
5.767 Beiträge
 
Delphi 10.4 Sydney
 
#8

Re: Bei Programmaufruf eigene Anwendung nicht beenden?

  Alt 23. Aug 2006, 11:55
Hallo Heike,

ich benutze meistens diesen Code:

Delphi-Quellcode:
{ WindowState is one of the SW_xxx constants.
  Look up ShowWindow in the API help for a list.}

function ExecAndWait(const Filename, Params: string;wait:Boolean;WindowState: word): boolean;
{$IFDEF WIN32}
var
  SUInfo: TStartupInfo;
  ProcInfo: TProcessInformation;
  CmdLine: string;
begin
  { Enclose filename in quotes to take care of
    long filenames with spaces. }

  CmdLine := '"' + Filename + '" ' + Params;
  FillChar(SUInfo, SizeOf(SUInfo), #0);
  with SUInfo do begin
    cb := SizeOf(SUInfo);
    dwFlags := STARTF_USESHOWWINDOW;
    wShowWindow := WindowState;
  end;
  try
    Result := CreateProcess(NIL, PChar(CmdLine), NIL, NIL, FALSE,
                            CREATE_NEW_CONSOLE or
                            NORMAL_PRIORITY_CLASS, NIL,
                            PChar(ExtractFilePath(Filename)),
                            SUInfo, ProcInfo);
  except
    on E: Exception do
      begin
        Result:=false;
      end;
  end;

  { Wait for it to finish. }
  if Result and wait then
    WaitForSingleObject(ProcInfo.hProcess, INFINITE);

{$ELSE}
var
  InstanceID : THandle;
  Buff: array[0..255] of char;
begin
  StrPCopy(Buff, Filename + ' ' + Params);
  InstanceID := WinExec(Buff, WindowState);
  if InstanceID < 32 then
  { a value less than 32 indicates an Exec error }
    Result := FALSE
  else begin
    Result := TRUE;
    if wait then
      repeat
        Application.ProcessMessages;
      until Application.Terminated or (GetModuleUsage(InstanceID) = 0);
  end;
{$ENDIF}
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ExecandWait('C:\WinNT\System32\sndrec32.exe','',false,sw_normal);
end;
Habe das mal mit dem Aufruf von SondRec ausprobiert, aber das Programm stürzt nicht ab.

Kannst Du Deine Anwendung eventuell mal auf einem andern Rechner ausprobieren?
Vielleicht ist etwas mit dem Speicher?

Grüße
Klaus
Klaus
  Mit Zitat antworten Zitat