Einzelnen Beitrag anzeigen

Benutzerbild von KodeZwerg
KodeZwerg

Registriert seit: 1. Feb 2018
3.685 Beiträge
 
Delphi 11 Alexandria
 
#1

CommandLineInterpreter aufrufen und Daten übermitteln

  Alt 9. Aug 2022, 08:55
Hallo liebe Delphi Gemeinde,

ich bin etwas am verzweifeln und bin mir sicher das der Fehler vor dem Rechner sitzt

Ich hatte vor eine Art Batch ersatz für Pascal zu entwerfen wo der User eine Liste an Befehlen einer Methode übergibt die eine CLI startet und die Befehle nach und nach abarbeitet.
Soweit so gut, bis hier hin bin ich gekommen aber es funktioniert einfach nicht, ich finde leider nicht den Punkt an dem ich etwas falsch mache und erhoffe mir Hilfe.
Delphi-Quellcode:
function ExecuteBatch(const ABatch: TStringList): Boolean;
  procedure LWritePipeOut(OutputPipe: THandle; InString: PChar);
  var
    byteswritten: DWORD;
    AnsiBuf: AnsiString;
  begin
    AnsiBuf := String(InString) + #13#10;
    WriteFile(OutputPipe, AnsiBuf, Length(AnsiBuf), byteswritten, nil);
  end;
var
  i: Integer;
  LCLI: AnsiString;
  LSecurityAttribute : TSecurityAttributes;
  LProcessInformation: TProcessInformation;
  LStartUpInfo: TStartUpInfo;
  NewStdIn, WriteStdIn: THandle;
begin
  LCLI := GetEnvironmentVariable('COMSPEC');
  UniqueString(LCLI);

  LSecurityAttribute.nlength := SizeOf(TSecurityAttributes) ;
  LSecurityAttribute.binherithandle := True;
  LSecurityAttribute.lpsecuritydescriptor := nil;
  CreatePipe(NewStdIn, WriteStdIn, @LSecurityAttribute, 0);

  FillChar(LStartUpInfo, Sizeof(LStartUpInfo), #0) ;
  LStartUpInfo.hStdInput := NewStdIn;
  LStartUpInfo.dwFlags := STARTF_USESTDHANDLES + STARTF_USESHOWWINDOW;
  LStartUpInfo.wShowWindow := SW_Show;
  LStartUpInfo.cb := SizeOf(LStartUpInfo);
  LStartUpInfo.lpTitle := PChar('Batch Console');

  if CreateProcess(nil, PChar(LCLI), @LSecurityAttribute, @LSecurityAttribute, True,
                   CREATE_NEW_CONSOLE or SYNCHRONIZE,
                   nil, nil, LStartUpInfo, LProcessInformation) then
    begin
      for i := 0 to Pred(ABatch.Count) do
        begin
          LWritePipeOut(WriteStdIn, PChar(ABatch.Strings[i]));
        end;
      CloseHandle(LProcessInformation.hProcess);
      CloseHandle(LProcessInformation.hThread);
      Result := True
    end
    else
      Result := False;
  CloseHandle(NewStdIn);
  CloseHandle(WriteStdIn);
end;
Momentan wird eine CLI gestartet aber keiner der Befehle erscheint in der CLI.
Gruß vom KodeZwerg
  Mit Zitat antworten Zitat