Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Algorithmen, Datenstrukturen und Klassendesign (https://www.delphipraxis.net/78-algorithmen-datenstrukturen-und-klassendesign/)
-   -   Delphi DosCommand in Konsolenanwendung, identifier not found (https://www.delphipraxis.net/192548-doscommand-konsolenanwendung-identifier-not-found.html)

Monday 29. Apr 2017 10:24

DosCommand in Konsolenanwendung, identifier not found
 
Hallo,

Aufgabenbeschreibung:
eine Konsolenanwendung, soll eine andere Konsolenanwendung auslesen, und den Wert verarbeiten und dann speichern.

Mit einem Grafischen Windowsprogramm ist das kein Problem:
Delphi-Quellcode:
uses ... DosCommand_2009 ...
Delphi-Quellcode:
  TForm1 = class(TForm)
    DosCommand1: TDosCommand;
    ...

var
cmd: TDoscommand;

...

Aber wie geht das gleiche mit einer Konsolenanwendung??
Über Projekteinstellungen/Projektinspektor DosCommand_2009 aufgenommen.

Delphi-Quellcode:

  TMyApplication = class(TCustomApplication)
  protected
    procedure DoRun; override;
  public
    DosCommand1: TDosCommand;
    constructor Create(TheOwner: TComponent); override;
    destructor Destroy; override;
    procedure WriteHelp; virtual;
  end;
Error:...

Delphi-Quellcode:
project1.lpr(20,29) Error: Identifier not found "TDosCommand"
Aber warum?

Nehme DosCommand_2009 aus dem Projektinspektor raus und schreibe es selbst ins Uses, dann meckert er weiter " Error: Identifier not found "graphics"" in DosCommand_2009....

Woran liegt es?

LG
Monday

nahpets 29. Apr 2017 11:39

AW: DosCommand in Konsolenanwendung, identifier not found
 
Ausgehend von Deiner Aufgabenstellung würdest Du eher sowas benötigen:
Delphi-Quellcode:
program Tee;

{$APPTYPE CONSOLE}

var
  line : string;
  sFile : string;
  fFile : TextFile;
begin
  if ParamCount in [1..2] then begin
    sFile := ParamStr(1);
  end else begin
    WriteLn('Syntax: tee Ausgabedatei');
    WriteLn;
    WriteLn('       tee SchreibDieAusgabeHierHin.txt');
    WriteLn;
    WriteLn('       tee SchreibDieAusgabeHierHin.txt append');
    WriteLn('       (Die Ausgabe wird an die bestehende Datei angehangen.)');
    WriteLn;
    halt(255);
  end;
  AssignFile(input, ''); // stdin
  AssignFile(output, ''); // stdout
  AssignFile(fFile, sFile);
  Reset(input);
  Rewrite(output);
  case ParamCount of
    2 : begin
          if ParamStr(2)[1] in ['A','a'] then begin
            {$I-}
            Append(fFile);
            if IOResult <> 0 then Rewrite(fFile);
            {$I+}
          end else begin
            Rewrite(fFile);
          end;
        end;
  else
    Rewrite(fFile);
  end;
  while not Eof do begin
    ReadLn(line);
    WriteLn(line);
    WriteLn(fFile,line);
  end;
  CloseFile(fFile);
  CloseFile(output);
  CloseFile(input);
end.
Das Programm liest die Ausgabe einer Konsolenanwendung und schreibt sie in eine Datei, gibt sie aber auch gleichzeitig auf der Konsole aus.

Der Aufruf erfolgt dann z. B. in dieser Form:
Delphi-Quellcode:
dir /s|tee InDieDateiSollGeschriebenWerden.txt
In der Schleife
Delphi-Quellcode:
while not Eof do begin
könntest Du natürlich dann beliebige Auswertungen, Verarbeitungen ... vornehmen.

Monday 29. Apr 2017 13:53

AW: DosCommand in Konsolenanwendung, identifier not found
 
Ok, Problem gelöst...

Ich musste bei der Konsolenanwendung noch die .pas Dateien über den Projektinspektor einfügen. Das war bei der Windows GUI nicht nötig.
Warum auch immer...

Trotzdem danke für die Mühe.

LG
Monday

himitsu 29. Apr 2017 15:14

AW: DosCommand in Konsolenanwendung, identifier not found
 
Welche Dateien?

Und meine Vermutung geht in Richtung fehlende Default-Namespaces
und alternativ noch zu fehlenden Suchpfaden.

Monday 30. Apr 2017 07:40

AW: DosCommand in Konsolenanwendung, identifier not found
 
Zitat:

Zitat von himitsu (Beitrag 1369438)
Welche Dateien?

Und meine Vermutung geht in Richtung fehlende Default-Namespaces
und alternativ noch zu fehlenden Suchpfaden.

DosCommand_2009.pas + DosCommand als Packages.

Monday 30. Apr 2017 08:07

AW: DosCommand in Konsolenanwendung, identifier not found
 
Irgendwie habe ich mir das einfacher vorgestellt:

Programm ruft Anwendung auf, sendet aber nichts an die Anwendung (??) und geht auch nicht auf DosCommand1NewLine . Warum?

In diesem Beispiel erwarte ich jede Menge "111".


Delphi-Quellcode:
program project1;

{$mode delphi}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes, SysUtils, CustApp, StrUtils, DosCommand_2009,
  interfaces
  { you can add units after this };

type

  { TMyApplication }

  TMyApplication = class(TCustomApplication)
    DosCommand1: TDosCommand;
    procedure DosCommand1NewLine(Sender: TObject; NewLine: string; OutputType: TOutputType);
  protected
    procedure DoRun; override;
  public
    constructor Create(TheOwner: TComponent); override;
    destructor Destroy; override;
    procedure WriteHelp; virtual;
  end;

{ TMyApplication }



procedure TMyApplication.DosCommand1NewLine(Sender: TObject; NewLine: string; OutputType: TOutputType);
begin
  writeln('1');
end;






procedure TMyApplication.DoRun;
var
  ErrorMsg: String;
  s : string;
  datei: string;
begin
  // quick check parameters
  ErrorMsg:=CheckOptions('h','help');
  if ErrorMsg<>'' then begin
    ShowException(Exception.Create(ErrorMsg));
    Terminate;
    Exit;
  end;

  // parse parameters
  if HasOption('h','help') then begin
    WriteHelp;
    Terminate;
    Exit;
  end;

    { add your program here }

    DosCommand1 := TDoscommand.Create(self);
    DosCommand1.OnNewLine := DosCommand1NewLine; //???

    datei := 'cmd';
    DosCommand1.CommandLine := datei;
    DosCommand1.Execute;
    DosCommand1.SendLine('dir',True);

   read(s);

  // stop program loop
  Terminate;
end;

constructor TMyApplication.Create(TheOwner: TComponent);
begin
  inherited Create(TheOwner);
  StopOnException:=True;
end;

destructor TMyApplication.Destroy;
begin
  inherited Destroy;
end;

procedure TMyApplication.WriteHelp;
begin
  { add your help code here }
  writeln('Usage: ',ExeName,' -h');
end;

var
  Application: TMyApplication;

begin
  Application:=TMyApplication.Create(nil);
  Application.Title:='My Application';
  Application.Run;
  Application.Free;
end.

himitsu 30. Apr 2017 10:44

AW: DosCommand in Konsolenanwendung, identifier not found
 
Vermutlich synchronisiert sich DOSCommand mit der VCL und da DU keine Messages verarbeitest, wird das auch nie ausgeführt?


Alle Zeitangaben in WEZ +1. Es ist jetzt 21:51 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz