Autor Beitrag
Mr.D3ath
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 24



BeitragVerfasst: Mo 19.03.07 18:49 
Ah hi eine Frage wie kann ich von Delphi 7 Windows befehle nehmen lassen z.B. Shutdown oder Net Send so den Kram halt ?!
Muss doch irgendwie möglich sowas zu machen oder?!


Zuletzt bearbeitet von Mr.D3ath am Mi 21.03.07 20:13, insgesamt 1-mal bearbeitet
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mo 19.03.07 19:01 
Moin und :welcome: im Forum!

Ich nehme mal an, du möchtest Konsolen-Befehle in einem Delphi-Programm ausführen. ;)

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Mr.D3ath Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 24



BeitragVerfasst: Mo 19.03.07 19:08 
user profile iconNarses hat folgendes geschrieben:
Moin und :welcome: im Forum!

Ich nehme mal an, du möchtest Konsolen-Befehle in einem Delphi-Programm ausführen. ;)

cu
Narses


Thx Thx
Mr.D3ath Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 24



BeitragVerfasst: Mo 19.03.07 19:12 
find ich gut passt aber nicht so wie ich es mir vorstelle :)
Ich möchte Befehle ausführen bei deiner test version ist es ja ein cmd,
ich möchte zum bsp ein Befehl per button click ausführen so meine ich das !
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mo 19.03.07 19:26 
Moin!

user profile iconMr.D3ath hat folgendes geschrieben:
Ich möchte Befehle ausführen bei deiner test version ist es ja ein cmd,
ich möchte zum bsp ein Befehl per button click ausführen so meine ich das !

Was hindert dich denn? :gruebel:

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Mr.D3ath Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 24



BeitragVerfasst: Mo 19.03.07 19:58 
Ich bekomm das einfach nicht so hin wie ich will !

Bsp.: Ein teil woran ich gerade versuche zu arbeiten ^^

"shutdown -r -t" soll fix vorgegeben sein danach hat man ein Edit fehlt dort gibt man eine Zahl ein die dann angfügt wird und mit den Button Submit soll dann alles in allen ausgeführt werden !

Bsp:

User gibt ins Edit feld 60 ein
Shutdown -r -t 60 und mit den Button Submit wird der Befehl gestartet !
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mo 19.03.07 20:01 
Moin!

Zeig doch mal deinen Code, dann sehen wir, woran es hängt. ;)

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mo 19.03.07 20:22 
Moin!

a) Bitte achte etwas auf deine Umgangsformen, das Forum ist keine in deinen Computer eingebaute, interaktive Hilfe-Funktion oder ein Code-Generator! :? Konkret schadet es also nicht, wenn du mal z.B. "Hallo" oder sowas sagst... :roll:

b) Es gibt hier Delphi-Tags, damit der Code durch Syntax-Highlighting besser lesbar wird. Einfach den Quelltext markieren und auf das "+" neben dem "Delphi" in der Combobox des Editors klicken.

c) Den Code aus der Lib kenne ich schon - hab ihn selbst geschrieben... 8) Was davon ist jetzt von dir und wo genau ist dein Problem? :nixweiss:

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Mr.D3ath Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 24



BeitragVerfasst: Mo 19.03.07 20:29 
Also Hallo sry aba bin schon ziemlich verzweifelt !
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:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    Edit2: TEdit;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function ExecConsole(const Command: String; var Output, Errors: String): Boolean;  
  var
    StartupInfo: TStartupInfo;  
    ProcessInfo: TProcessInformation;  
    SecurityAttr: TSecurityAttributes;  
    PipeOutputRead, PipeOutputWrite,  
    PipeErrorsRead, PipeErrorsWrite: THandle;

 
  // Pipe in einen String auslesen (speicherschonend)  
  function ReadPipeToString(const hPipe: THandle): String;
    const  
      MEM_CHUNK_SIZE = 8192; // Blockgröße, mit der Speicher angefordert wird  
    var  
      NumberOfBytesRead,
      NumberOfBytesTotal: Cardinal;  
  begin  
    Result := ''; // Standard-Ergebnis  
    NumberOfBytesTotal := 0; // noch nichts gelesen
    repeat  
      SetLength(Result,Length(Result) +MEM_CHUNK_SIZE); // mehr Platz machen  
      // versuchen, aus der Pipe zu lesen  
      if ReadFile(hPipe,(@Result[1+NumberOfBytesTotal])^,MEM_CHUNK_SIZE,
                  NumberOfBytesRead,NIL) then // hat geklappt  
        Inc(NumberOfBytesTotal,NumberOfBytesRead); // Gesamtanzahl aktualisieren  
      SetLength(Result,NumberOfBytesTotal); // überzählige Bytes abschneiden  
    until (NumberOfBytesRead = 0); // bis die Pipe leer ist
  end;  

 
begin
  // Win-API-Strukturen initialisieren/füllen  
  FillChar(ProcessInfo,SizeOf(TProcessInformation),0);  
  FillChar(SecurityAttr,SizeOf(TSecurityAttributes),0);
  SecurityAttr.nLength := SizeOf(SecurityAttr);  
  SecurityAttr.bInheritHandle := TRUE;  
  SecurityAttr.lpSecurityDescriptor := NIL;
  CreatePipe(PipeOutputRead,PipeOutputWrite,@SecurityAttr,0);  
  CreatePipe(PipeErrorsRead,PipeErrorsWrite,@SecurityAttr,0);  
  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;
  // Befehl ausführen (Prozess starten)  
  Result := CreateProcess(NIL,PChar(Command),NIL,NIL,TRUE,  
                          CREATE_DEFAULT_ERROR_MODE
                          or CREATE_NEW_CONSOLE  
                          or NORMAL_PRIORITY_CLASS,  
                          NIL,NIL,StartupInfo,ProcessInfo);
  // Write-Pipes schließen  
  CloseHandle(PipeOutputWrite);
  CloseHandle(PipeErrorsWrite);  
  if (Result) then begin // konnte der Befehl ausgeführt werden?
    Output := ReadPipeToString(PipeOutputRead); // Ausgabe-Read-Pipe auslesen  
    Errors := ReadPipeToString(PipeErrorsRead); // Fehler-Read-Pipe auslesen
    WaitForSingleObject(ProcessInfo.hProcess,INFINITE); // auf Prozessende warten  
    CloseHandle(ProcessInfo.hProcess); // und Handle freigeben
  end;  
  // Read-Pipes schließen
  CloseHandle(PipeOutputRead);  
  CloseHandle(PipeErrorsRead);
end;


procedure TForm1.Button1Click(Sender: TObject);
var
  fix, user, all: integer;
  Output, Errors: String;
  tmp: TStringList;
begin
    fix:=StrToInt(Edit2.Text);
    user:=StrToInt(Edit1.Text);
    all:=fix+user
    if ExecConsole('cmd /c all',Output,Errors) then begin
      if (Output <> '') then begin
        Log.Lines.Add('---Output---');
        tmp.Text :=  Output; // Ausgabe in Zeilen zerlegen
        Log.Lines.AddStrings(tmp); // und ins Log einfügen

end;

end.


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:
// Consolenkommando ausführen und Ergebnis/Fehler zurückliefern  
function ExecConsole(const Command: String; var Output, Errors: String): Boolean;  
  var  
    StartupInfo: TStartupInfo;  
    ProcessInfo: TProcessInformation;  
    SecurityAttr: TSecurityAttributes;  
    PipeOutputRead, PipeOutputWrite,  
    PipeErrorsRead, PipeErrorsWrite: THandle;  

 
  // Pipe in einen String auslesen (speicherschonend)  
  function ReadPipeToString(const hPipe: THandle): String;  
    const  
      MEM_CHUNK_SIZE = 8192; // Blockgröße, mit der Speicher angefordert wird  
    var  
      NumberOfBytesRead,  
      NumberOfBytesTotal: Cardinal;  
  begin  
    Result := ''; // Standard-Ergebnis  
    NumberOfBytesTotal := 0; // noch nichts gelesen  
    repeat  
      SetLength(Result,Length(Result) +MEM_CHUNK_SIZE); // mehr Platz machen  
      // versuchen, aus der Pipe zu lesen  
      if ReadFile(hPipe,(@Result[1+NumberOfBytesTotal])^,MEM_CHUNK_SIZE,  
                  NumberOfBytesRead,NIL) then // hat geklappt  
        Inc(NumberOfBytesTotal,NumberOfBytesRead); // Gesamtanzahl aktualisieren  
      SetLength(Result,NumberOfBytesTotal); // überzählige Bytes abschneiden  
    until (NumberOfBytesRead = 0); // bis die Pipe leer ist  
  end;  

 
begin  
  // Win-API-Strukturen initialisieren/füllen  
  FillChar(ProcessInfo,SizeOf(TProcessInformation),0);  
  FillChar(SecurityAttr,SizeOf(TSecurityAttributes),0);  
  SecurityAttr.nLength := SizeOf(SecurityAttr);  
  SecurityAttr.bInheritHandle := TRUE;  
  SecurityAttr.lpSecurityDescriptor := NIL;  
  CreatePipe(PipeOutputRead,PipeOutputWrite,@SecurityAttr,0);  
  CreatePipe(PipeErrorsRead,PipeErrorsWrite,@SecurityAttr,0);  
  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;  
  // Befehl ausführen (Prozess starten)  
  Result := CreateProcess(NIL,PChar(Command),NIL,NIL,TRUE,  
                          CREATE_DEFAULT_ERROR_MODE  
                          or CREATE_NEW_CONSOLE  
                          or NORMAL_PRIORITY_CLASS,  
                          NIL,NIL,StartupInfo,ProcessInfo);  
  // Write-Pipes schließen  
  CloseHandle(PipeOutputWrite);  
  CloseHandle(PipeErrorsWrite);  
  if (Result) then begin // konnte der Befehl ausgeführt werden?  
    Output := ReadPipeToString(PipeOutputRead); // Ausgabe-Read-Pipe auslesen  
    Errors := ReadPipeToString(PipeErrorsRead); // Fehler-Read-Pipe auslesen  
    WaitForSingleObject(ProcessInfo.hProcess,INFINITE); // auf Prozessende warten  
    CloseHandle(ProcessInfo.hProcess); // und Handle freigeben  
  end;  
  // Read-Pipes schließen  
  CloseHandle(PipeOutputRead);  
  CloseHandle(PipeErrorsRead);  
end;


Das muss ich Sicher einfügen so wie ich das verstehe um die COmmandos überhaupt auszuführen zu können oder?
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mo 19.03.07 20:38 
Moin!

Du hast jetzt zum 3. Mal den Code aus der Lib gepostet... :shock: meinst du nicht, es reicht, wenn du einfach nur deine Button-Klick-Prozedur einfügst? :roll: ;) Also konkret das hier:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
procedure TForm1.Button1Click(Sender: TObject);  
var  
  fix, user, all: integer;  
  Output, Errors: String;  
  tmp: TStringList;  
begin  
    fix:=Edit2.Text; // StrToInt nicht notwendig
    user:=Edit1.Text;  
    all:=fix+user;
    if ExecConsole('cmd /c all',Output,Errors) then begin // du wolltest doch shutdown ausführen?!
      if (Output <> '') then begin  
        Log.Lines.Add('---Output---');  
        tmp.Text :=  Output; // Ausgabe in Zeilen zerlegen  
        Log.Lines.AddStrings(tmp); // und ins Log einfügen  
end;

Kannst du mal beschreiben, wozu du Edit1 und Edit2 benutzt?

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Mr.D3ath Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 24



BeitragVerfasst: Mo 19.03.07 20:42 
Also Hi sry wegen den Code geschreibe wollte nur sicher gehen das es stimmt? oder das es der eine
Code fix verwendet werden muss um den Befehl auszuführen?
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
procedure TForm1.Button1Click(Sender: TObject);    
var    
  fix, user, all: integer;    
  Output, Errors: String;    
  tmp: TStringList;    
begin    
    fix:=Edit2.Text; // StrToInt nicht notwendig  // Von mir vorgegeben (Shutdown -r -t )
    user:=Edit1.Text;   // Legt der User fest also mit ner Zahl ! 
    all:=fix+user;  // Legt das ganze zusammen?!
    if ExecConsole('cmd /c all',Output,Errors) then begin // Führt doch das ganze aus?!  
      if (Output <> '') then begin    
        Log.Lines.Add('---Output---');    
        tmp.Text :=  Output; // Ausgabe in Zeilen zerlegen    
        Log.Lines.AddStrings(tmp); // und ins Log einfügen    
end;
Calculon
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 676

Win XP Professional
Delphi 7 PE, Delphi 3 PRO
BeitragVerfasst: Mo 19.03.07 20:47 
Mr.D3ath hat folgendes geschrieben:
Bsp:

User gibt ins Edit feld 60 ein
Shutdown -r -t 60 und mit den Button Submit wird der Befehl gestartet !

Also ich verstehe dein Problem ehrlich gesagt auch nicht. Mit Narses' Code kannst du das doch super umsetzen. Einen String um Edit.Text erweitern und schon kannst du das oben genannte Beispiel (einfach function ExecConsole aufrufen) umsetzen.

Gruß

Calculon
--
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mo 19.03.07 20:49 
Moin!

Du hast in alldoch schon das Kommando, also einfach ausführen: ;)
ausblenden Delphi-Quelltext
1:
if ExecConsole(all,Output,Errors) then begin					

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Mr.D3ath Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 24



BeitragVerfasst: Mo 19.03.07 20:54 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
procedure TForm1.Button1Click(Sender: TObject);
var
  fix, user, all: integer;
begin
    fix:=Edit2.Text;
    user:=Edit1.Text;
    all:=fix+user;
    if ExecConsole(all,Output,Errors) then begin
end;


Also dann müsste doch das eigentlich reichen wenn ich das jetzt richtig verstehe oder Narses?
Troz allem bekomm ich Fehler beim compilieren : Inkompatible Typen: Integer und TCaption,Inkompatible Typen: Integer und String

Edit: K hab den Fehler muss natürlich statt integer string machen aber trozdem hab ich noch einen fehler beim Compilieren:

ausblenden Delphi-Quelltext
1:
2:
[Fehler] Unit1.pas(105): Die Typen der tatsächlichen und formalen Var-Parameter müssen übereinstimmen
[Fataler Fehler] Project1.dpr(5): Verwendete Unit 'Unit1.pas' kann nicht compiliert werden
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mo 19.03.07 20:59 
Moin!

Musst natürlich auch wieder Outputund Errorsals Stringdeklarieren. ;)

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Mr.D3ath Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 24



BeitragVerfasst: Mo 19.03.07 21:04 
Also ich muss euch mal soooooo danken ihr wart mir echt sooooo ne große hilfe es klappt ja wirklich 99% aber problem ist
das der befehl so gewirkt wir shutdown -r -tXX gehört aber so shutdown -r -t XX ahhh könnt ihr mir das noch helfen ?! Narses tut mir leid das ich dir so viel Zeit geraubt habe thx thx thx :)

Edit: Hab es gelöst thx thx thx für deine mühe Narses ;)
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Di 20.03.07 01:10 
Hallo,

bitte ändere den Titel des Topics, da er wenig über das eigentlich Thema verrät. Hier der entsprechende Absatz aus den Richtlinien:

1.2 Beiträge:
Bitte formuliere den Betreff Deiner Beiträge so, dass andere Mitglieder anhand dieser bereits das eigentliche Thema festmachen können. Beiträge wie etwa "Eine Anfängerfrage" oder "Weiß jemand, wie das geht?" lassen den Leser im Unklaren darüber, was das Thema der Diskussion ist.[...]


Einfach oben bei Deinem ersten Beitrag auf user defined image oder user defined image klicken und den Titel ändern. Danke Dir!

Viele Grüße,
Christian S.

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".