Autor Beitrag
Andreas L.
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 1703
Erhaltene Danke: 25

Windows Vista / Windows 10
Delphi 2009 Pro (JVCL, DragDrop, rmKlever, ICS, EmbeddedWB, DEC, Indy)
BeitragVerfasst: So 14.09.03 18:48 
hi,
wenn ich in der cmd ipconfig eingebe gibt das ipconfig-Proggi ja die IP-Adresse aus. Diese Steht dann ja in der cmd. Meine Frage ist, wie kann ich die Ausgabe von ipconfig bzw. von anderen MS-DOS-Tools in meinen Proggi in einen TLabel anzeigen?

Greetz
Wolff68
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 302
Erhaltene Danke: 1

WinXP home
D6 Prof
BeitragVerfasst: So 14.09.03 19:33 
Da könnte Dir die Komponente TDosCommand weiterhelfen.
Bei dieser Kompo hast Du die DOS-Ausgabe in einem Memo und kannst sie somit auswerten.

Was auch geht (Und völlig ohne extra Komponente)
ipconfig > C:\Temp\IPErgebnis.txt ausführen.
mit dem > weist Du DOS an die Ausgabe an die angegebene Datei zu senden. (Die Datei wird dabei erzeugt)
Somit hast Du das Ergebnis in der Datei und kannst diese dann auswerten.

Edit: Der obige Link funktioniert anscheinend nicht mehr.
Die Kompo kann man aber auch bei Torry herunterladen (In der Liste nach DosCommand suchen)

_________________
"Der Mensch ist nicht was er sich vorstellt oder wünscht zu sein, sondern das was andere in ihm sehen."


Zuletzt bearbeitet von Wolff68 am Di 16.09.03 17:24, insgesamt 1-mal bearbeitet
Ex0rzist
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 550

Win XP Prof.
Mandrake 10.0

D6
BeitragVerfasst: So 14.09.03 19:38 
Wolff68 hat folgendes geschrieben:
ipconfig > C:\Temp\IPErgebnis.txt


Das funktioniert aber nur innerhalb der Eingabeaufforderung.
Andreas L. Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 1703
Erhaltene Danke: 25

Windows Vista / Windows 10
Delphi 2009 Pro (JVCL, DragDrop, rmKlever, ICS, EmbeddedWB, DEC, Indy)
BeitragVerfasst: So 14.09.03 19:44 
Die DOSCommand habe ich schon, nur wieß ich nicht wie man diese verwendet.
Wolff68
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 302
Erhaltene Danke: 1

WinXP home
D6 Prof
BeitragVerfasst: So 14.09.03 20:34 
Ex0rzist hat folgendes geschrieben:
Das funktioniert aber nur innerhalb der Eingabeaufforderung.

Auch nicht, wenn man mit ShellExecute die CMD aufruft, und die Zeile als Parameter übergibt !? Hmmm. Dachte das es funktionieren sollte. Lass' mich aber gerne eines besseren belehren.

@Onlinehome:
Was brauchst denn genau? Nur die IP-Adresse?
Vielleicht kann ich Dir ein paar Zeilen dazu proggen.
(Vor Di-Abend aber leider nicht)

PS. Das Ergebnis von IpConfig ist mehrzeilig. Willst Du das echt in ein Label packen?

_________________
"Der Mensch ist nicht was er sich vorstellt oder wünscht zu sein, sondern das was andere in ihm sehen."
Wolff68
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 302
Erhaltene Danke: 1

WinXP home
D6 Prof
BeitragVerfasst: So 14.09.03 20:46 
OK. Kurzes, einfachstes DosCommand-Prog:

1 Edit
1 Button
1 Memo
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
uses ... DosCommand ...

procedure TForm1.FormCreate(Sender: TObject);
begin
  DosBox := TDosCommand.Create(self);
  DosBox.OutputLines := Memo1.Lines;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  DosBox.CommandLine := Edit1.Text;
  DosBox.Execute;
end;


(Hab auch noch nicht alles ausgetestet, aber damit sollte Dir schon klar werden, wie Du es verwenden kannst)

_________________
"Der Mensch ist nicht was er sich vorstellt oder wünscht zu sein, sondern das was andere in ihm sehen."
Andreas L. Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 1703
Erhaltene Danke: 25

Windows Vista / Windows 10
Delphi 2009 Pro (JVCL, DragDrop, rmKlever, ICS, EmbeddedWB, DEC, Indy)
BeitragVerfasst: Mo 15.09.03 16:59 
Wolff68 hat folgendes geschrieben:
OK. Kurzes, einfachstes DosCommand-Prog:

1 Edit
1 Button
1 Memo
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
uses ... DosCommand ...

procedure TForm1.FormCreate(Sender: TObject);
begin
  DosBox := TDosCommand.Create(self);
  DosBox.OutputLines := Memo1.Lines;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  DosBox.CommandLine := Edit1.Text;
  DosBox.Execute;
end;


(Hab auch noch nicht alles ausgetestet, aber damit sollte Dir schon klar werden, wie Du es verwenden kannst)


Ach so geht das.....


Jetzt ist die Farge beantwortet, ich mache einfach alles mit TDOSCommand :-)
maxk
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1696
Erhaltene Danke: 1

Win XP, Debian Lenny
Delphi 6 Personal
BeitragVerfasst: Mo 15.09.03 18:46 
Diese Komponente habe ich zu diesem Zweck mal geschrieben:
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:
{
TConsoleSession by maxk

If think, all properties are known. So I needn't present it here.
PLEASE NOTE: TConsoleSession isn't able to run 16-Bit applications
under systems higher than Windows NT.
If you have any questions and/or ideas please mail me: delphi_max@lycos.de
}


unit ConsoleSession;

interface

uses
  Windows, Messages, SysUtils, Classes, ExtCtrls;

type
  TGotBytesEvent=procedure(Sender:TObject;Buffer:string) of object;
  TConsoleSession = class(TComponent)
   constructor Create(AOwner:TComponent); override;
   destructor Destroy; override;
   procedure Write(Buffer:string);
   procedure WriteLn(Buffer:string);
   function Running:boolean;
   procedure ReadFromPipe;
   procedure Execute;
   procedure Terminate(ExitCode:Cardinal=0);
   procedure WaitFor(TimeOut:Cardinal=0);
   function GetProcess:Cardinal;
  private
    FCommandLine:string;
    FReadBufferSize:Cardinal;
    FWriteBufferSize:Cardinal;
    FReadInterval:integer;
    FOnGotBytes:TGotBytesEvent;
    Timer:TTimer;
    hIReadPipe,hIWritePipe,hOReadPipe,hOWritePipe:THandle;
    ProcessInfo:TProcessInformation;
    procedure FReadFromPipe(Sender:TObject);
    procedure SetReadInterval(NewInterval:integer);
    procedure CleanUp;
  protected
    { Protected-Deklarationen }
  public
    { Public-Deklarationen }
  published
    property CommandLine:string read FCommandLine write FCommandLine;
    property ReadBufferSize:Cardinal read FReadBufferSize write FReadBufferSize;
    property WriteBufferSize:Cardinal read FWriteBufferSize
     write FWriteBufferSize;
    property ReadInterval:integer read FReadInterval write SetReadInterval;
    property OnGotBytes:TGotBytesEvent read FOnGotBytes write FOnGotBytes;
  end;

procedure Register;

implementation

constructor TConsoleSession.Create(AOwner:TComponent);
begin
 inherited Create(AOwner);

 if csDesigning  in ComponentState then begin
  MessageBox(0,'Please note that TConsoleSession isn''t able to run 16-Bit '+
   'applications under systems higher than Windows NT.'+#13+'When you have any'+
   ' ideas so mail me: delphi_max@lycos.de - Thanks'+#13+'This message only '+
   'appears in design time.','Warning',MB_ICONEXCLAMATION);
 end;

 FCommandLine:='cmd.exe';
 FReadBufferSize:=256;
 FWriteBufferSize:=65535;
 Timer:=TTimer.Create(nil);
 Timer.OnTimer:=FReadFromPipe;
 SetReadInterval(100);
end;

destructor TConsoleSession.Destroy;
begin
 Timer.Free;
 if Running then TerminateProcess(ProcessInfo.hProcess,0);
 CleanUp;
 inherited Destroy;
end;

procedure TConsoleSession.Write(Buffer:string);
var BytesToWrite,BytesWritten:Cardinal;
begin
 if not Running then
  raise Exception.Create(Name+': Command not running');
 while length(Buffer)>0 do begin
  BytesToWrite:=length(Buffer);
  if BytesToWrite>FWriteBufferSize then BytesToWrite:=FWriteBufferSize;
  WriteFile(hIWritePipe,Buffer[1],BytesToWrite,BytesWritten,nil);
  Delete(Buffer,1,BytesWritten);
 end;
end;

procedure TConsoleSession.WriteLn(Buffer:string);
begin
 Write(Buffer+#13#10);
end;

function TConsoleSession.Running:boolean;
begin
 Result:=WaitForSingleObject(ProcessInfo.hProcess,0)=WAIT_TIMEOUT;
end;

procedure TConsoleSession.ReadFromPipe;
begin
 if not Running then
  raise Exception.Create(Name+': Command not running');
 FReadFromPipe(nil);
end;

procedure TConsoleSession.Execute;
var SecAttr:TSecurityAttributes;
    StartupInfo:TStartUpInfo;
begin
 if not Running then CleanUp else
  raise Exception.Create(Name+': Already running');

 with SecAttr do begin
  nLength:=SizeOf(TSecurityAttributes);
  bInheritHandle:=true;
  lpSecurityDescriptor:=nil;
 end;

 if not CreatePipe(hIReadPipe,hIWritePipe,@SecAttr,0) then
  raise Exception.Create(Name+': Unable to create write pipe');
 if not Createpipe(hOReadPipe,hOWritePipe,@SecAttr,0) then
  raise Exception.Create(Name+': Unable to create read pipe');

 FillChar(StartupInfo,Sizeof(StartupInfo),#0);
 with StartupInfo do begin
  cb:=SizeOf(StartupInfo);
  hStdOutput:=hOWritePipe;
  hStdInput:=hIReadPipe;
  dwFlags:=STARTF_USESTDHANDLES+STARTF_USESHOWWINDOW;
  wShowWindow:=SW_HIDE;
 end;

 if not CreateProcess(nil,PChar(FCommandLine),@SecAttr,@SecAttr,True,
  NORMAL_PRIORITY_CLASS,nil,nil,StartupInfo,ProcessInfo) then
   raise Exception.Create(Name+': Unable to start command.'+#13+'Error: '+
   IntToStr(GetLastError));
end;

procedure TConsoleSession.Terminate(ExitCode:Cardinal=0);
begin
 if not Running then
  raise Exception.Create(Name+': Command not running');
 TerminateProcess(ProcessInfo.hProcess,ExitCode);
 CleanUp;
end;

procedure TConsoleSession.WaitFor(TimeOut:Cardinal=0);
var Tick:Cardinal;
begin
 Tick:=GetTickCount;
 while (Tick+TimeOut>GetTickCount) and (Running) and (TimeOut=0) do begin
  Sleep(1);
 end;
end;

function TConsoleSession.GetProcess:Cardinal;
begin
 if not Running then
  raise Exception.Create(Name+': Command not running');
 Result:=ProcessInfo.hProcess;
end;

procedure TConsoleSession.FReadFromPipe(Sender:TObject);

 function BytesInPipe:Cardinal;
 begin
  if not PeekNamedPipe(hOReadPipe,nil,0,nil,@Result,nil) then Result:=0;
 end;

var BytesRead:DWord;
    Buffer:string;
begin
 if (not Running) or (BytesInPipe=0) then exit;
 try
  Timer.Enabled:=False;
  repeat
   BytesRead:=0;
   SetLength(Buffer,FReadBufferSize);
   ReadFile(hOReadPipe,Buffer[1],length(Buffer),BytesRead,nil);
   SetLength(Buffer,BytesRead); // Remove trash from other reads
   OemToAnsi(@Buffer[1],@Buffer[1]);
   if Assigned(FOnGotBytes) then FOnGotBytes(Self,Buffer);
  until BytesInPipe=0;
 finally
  Timer.Enabled:=True;
 end;
end;

procedure TConsoleSession.SetReadInterval(NewInterval:integer);
begin
 Timer.Interval:=NewInterval;
 FReadInterval:=Timer.Interval;
end;

procedure TConsoleSession.CleanUp;
begin
 if ProcessInfo.hProcess<>0 then CloseHandle(ProcessInfo.hProcess);
 if ProcessInfo.hThread<>0 then CloseHandle(ProcessInfo.hThread);
 if hOReadPipe<>0 then CloseHandle(hOReadPipe);
 if hOWritePipe<>0 then CloseHandle(hOWritePipe);
 if hIReadPipe<>0 then CloseHandle(hIReadPipe);
 if hIWritePipe<>0 then CloseHandle(hIWritePipe);

 ProcessInfo.hProcess:=0;
 ProcessInfo.hThread:=0;
 hOReadPipe:=0;
 hOWritePipe:=0;
 hIWritePipe:=0;
 hOWritePipe:=0;
end;

procedure Register;
begin
  RegisterComponents('maxk', [TConsoleSession]);
end;

end.

_________________
Ein Computer wird das tun, was Du programmierst - nicht das, was Du willst.