Autor Beitrag
volvox
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 112

WIN XP
Delphi 7 Personal
BeitragVerfasst: Mi 18.01.06 21:00 
Hi. Ich bin neu im Forum und programmiere erst seit Herbst mit Delphi. Kann mir jemand erklären, wie ich über Delphi die Eingabeaufforderung (cmd) öffnen und Befehle ausführen kann?. Man soll z.B. Anwendungen wie explorer.exe öffnen, oder andere PCs pingen können. Gehr dies bei Delphi 7 Personal über ganz normale ANwendungen, oder brauche ich diese batch-Dateien? (Hab noch nie damit gearbeitet!) Wäre gut, wenn mir das jemand recht genau erklären kann, da ich ncoh nicht so viel Erfahrung mit Delphi habe. PHILIPP
Marco D.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2750

Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
BeitragVerfasst: Mi 18.01.06 21:36 
:welcome: :wink2: :welcome:

Also du kannst eine Stringlist benutzen.


ausblenden Delphi-Quelltext
1:
2:
3:
4:
var
list:TStringlist;

list:=TStringlist.create


Eine Stringlist ist wie eine Listbox, eine Liste von Strings, wobei jeder einzelne String über einen Index angesprochen werden kann.
Nun fügst der Stringlist deinen cmd-Befehl hinzu:

ausblenden Delphi-Quelltext
1:
list.add('ping 192.168.0.1');					


DIe Stringlist wird nun als bat-Datei gespeichert.

ausblenden Delphi-Quelltext
1:
list.savetofile(extractfilepath(paramstr(0))+'temp.bat');					


Nun muss diese Datei ausgeführt werden, das machst du mit dieser anweisung:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
ShellExecute(Application.Handle,
                 'open',
                 PChar(extractfilepath(paramstr(0))+'temp.bat'),
                 NilNil, SW_NORMAL)


Nun muss noch die Stringlist ausm Speicher gefegt und die bat-Datei gelöscht werden

ausblenden Delphi-Quelltext
1:
2:
list.free;
deletefile(extractfilepath(paramstr(0))+'temp.bat');


Wenn du was nicht verstanden hast, frag ruhig nach :wink:

Grüße von Koller

_________________
Pascal keeps your hand tied. C gives you enough rope to hang yourself. C++ gives you enough rope to shoot yourself in the foot


Zuletzt bearbeitet von Marco D. am Mi 18.01.06 21:38, insgesamt 1-mal bearbeitet
MrFox
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 208

WIN 2000 Professional, Ubuntu 5.10
D3 Prof, D7 Pers, D2005 Pers, Java (Eclipse)
BeitragVerfasst: Mi 18.01.06 21:36 
Wenn du lediglich einen Befehl ausführen willst, würde sich ShellExecute mit entsprechender Parameterübergabe eignen.

//EDIT: War wohl zu spät dran

_________________
Das Leben auf der Erde mag zwar teuer sein, aber eine jährliche Rundreise um die Sonne ist gratis mit dabei.
Marco D.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2750

Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
BeitragVerfasst: Mi 18.01.06 21:39 
Hatte noch vergessen, dass du oben die Unit 'shellapi' einbinden musst. Sorry

_________________
Pascal keeps your hand tied. C gives you enough rope to hang yourself. C++ gives you enough rope to shoot yourself in the foot
UGrohne
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Veteran
Beiträge: 5502
Erhaltene Danke: 220

Windows 8 , Server 2012
D7 Pro, VS.NET 2012 (C#)
BeitragVerfasst: Mi 18.01.06 21:43 
Die Antworten von ping willst Du doch bestimmt wiede rzurück haben? Ich hab den folgenden Code mal gefunden gehabt, der funktioniert bei mir einwandfrei:
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:
function GetConsoleOutput(const Command: Stringvar Output, Errors: TStringList): Boolean;
var
  StartupInfo: TStartupInfo;
  ProcessInfo: TProcessInformation;
  SecurityAttr: TSecurityAttributes;
  PipeOutputRead: THandle;
  PipeOutputWrite: THandle;
  PipeErrorsRead: THandle;
  PipeErrorsWrite: THandle;
  Succeed: Boolean;
  Buffer: array [0..255of Char;
  NumberOfBytesRead: DWORD;
  Stream: TMemoryStream;
begin
  //Initialisierung ProcessInfo
  FillChar(ProcessInfo, SizeOf(TProcessInformation), 0);

  //Initialisierung SecurityAttr
  FillChar(SecurityAttr, SizeOf(TSecurityAttributes), 0);
  SecurityAttr.nLength := SizeOf(SecurityAttr);
  SecurityAttr.bInheritHandle := true;
  SecurityAttr.lpSecurityDescriptor := nil;

  //Pipes erzeugen
  CreatePipe(PipeOutputRead, PipeOutputWrite, @SecurityAttr, 0);
  CreatePipe(PipeErrorsRead, PipeErrorsWrite, @SecurityAttr, 0);

  //Initialisierung StartupInfo
  FillChar(StartupInfo, SizeOf(TStartupInfo), 0);
  StartupInfo.cb:=SizeOf(StartupInfo);
  StartupInfo.hStdInput := 0;
  StartupInfo.hStdOutput := PipeOutputWrite;
  StartupInfo.hStdError := PipeErrorsWrite;
  StartupInfo.wShowWindow := sw_Hide;
  StartupInfo.dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;

  if  CreateProcess(nil, PChar(command), nilnil, true,
  CREATE_DEFAULT_ERROR_MODE or CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS, nilnil,
  StartupInfo, ProcessInfo) then begin
    result:=true;
    //Write-Pipes schließen
    CloseHandle(PipeOutputWrite);
    CloseHandle(PipeErrorsWrite);

    //Ausgabe Read-Pipe auslesen
    Stream := TMemoryStream.Create;
    try
      while true do begin
        succeed := ReadFile(PipeOutputRead, Buffer, 255, NumberOfBytesRead, nil);
        if not succeed then break;
        Stream.Write(Buffer, NumberOfBytesRead);
      end;
      Stream.Position := 0;
      Output.LoadFromStream(Stream);
    finally
      Stream.Free;
    end;
    CloseHandle(PipeOutputRead);

    //Fehler Read-Pipe auslesen
    Stream := TMemoryStream.Create;
    try
      while true do begin
        succeed := ReadFile(PipeErrorsRead, Buffer, 255, NumberOfBytesRead, nil);
        if not succeed then break;
        Stream.Write(Buffer, NumberOfBytesRead);
      end;
      Stream.Position := 0;
      Errors.LoadFromStream(Stream);
    finally
      Stream.Free;
    end;
    CloseHandle(PipeErrorsRead);

    WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
    CloseHandle(ProcessInfo.hProcess);
  end
  else begin
    result:=false;
    CloseHandle(PipeOutputRead);
    CloseHandle(PipeOutputWrite);
    CloseHandle(PipeErrorsRead);
    CloseHandle(PipeErrorsWrite);
  end;
end;

in Command gibst Du das DOS-Kommando ein, Output enthält die Ausgabe, die normalerweise auf der Konsole erfolgen würde und Errors die Fehler ;-)