Autor Beitrag
Sascha999999999
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 17



BeitragVerfasst: Di 19.12.06 23:06 
Hallo,

ich habe eine Console für mein Netzwerk geschrieben die CMD befehle hin und her sendet...

die Console ist schon 100% perfekt, stürtzt aber andauert ab (b.z.w. hängt sich auf).

also:

Ich habe diese Funktion (funktioniert):

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:
////////////////////////
function GetConsoleOutput(Command: Stringvar Output: 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;
  Errors: TStringList;
begin
  //Initialisierung ProcessInfo
  Errors:=TStringList.Create;
  FillChar(ProcessInfo, SizeOf(TProcessInformation), 0);
  {
  if(copy(Command, 0, 2)='cd') then begin
  if(copy(copy(Command, 3, length(Command)), 0, 1)=' ') then begin
  ChDir(copy(Command, 4, length(Command)));
  end
  else begin
  ChDir(copy(Command, 3, length(Command)));
  end;
  end;
  }

  if(copy(Command, 03)='cd 'then begin
  ChDir(copy(Command, 4, length(Command)));
  end
  else if(copy(Command, 03)='cd\'then begin
  ChDir(copy(Command, 3, length(Command)));
  end
  else if(copy(Command, 03)='cd.'then begin
  ChDir(copy(Command, 3, length(Command)));
  end
  else if(copy(Command, 05)='BEEP>'then begin
  beep;
  end
  else if(copy(Command, 05)='SHOW>'then begin
  if(copy(Command, 610)='CMD>'then begin
  Command:='cmd /c '+copy(Command, 11, length(Command));
  end;
  winexec(PChar(copy(Command, 6, length(Command))), SW_SHOW);
  end
  else if(copy(Command, 05)='HIDE>'then begin
  if(copy(Command, 59)='CMD>'then begin
  Command:='cmd /c '+copy(Command, 10, length(Command));
  end;
  winexec(PChar(copy(Command, 6, length(Command))), SW_HIDE);
  end
  else if(copy(Command, 04)='RUN>'then begin
  //winexec(PChar(copy(Command, 5, length(Command))), SW_HIDE);
  ShellExecute(FindWindow('explorer'nil), 'open', PChar(copy(Command, 5, pos(' ', Command)-5)), PChar(copy(Command, pos(' ', Command)-4, length(Command))), '', SW_SHOWNORMAL);
  end
  else if(copy(Command, 05)='OPEN>'then begin
  ChDir(copy(Command, 6, length(Command)));
  end
  else if(copy(Command, 09)='EXPLORER>'then begin
  winexec(PChar('explorer "'+copy(Command, 10, length(Command))+'"'), SW_SHOW);
  end;
  if(copy(Command, 04)='CMD>'then begin
  //winexec(PChar(copy(Command, 5, length(Command))), SW_HIDE);
  //ShellExecute(FindWindow('explorer', nil), 'open', PChar(copy(Command, 5, pos(' ', Command)-5)), PChar(copy(Command, pos(' ', Command)-4, length(Command))), '', SW_SHOWNORMAL);
  Command:='cmd /c '+copy(Command, 5, length(Command));
  end;
  //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;
      if(Stream.Size>0then begin
      Errors.LoadFromStream(Stream);
      end;
      //if(Stream.Size>Output.s) then begin
      //Output.LoadFromStream(Stream);
      //end;
    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;
  Errors.Free;
end;
///////////////////


ok, jetzt hängt sich das Programm aber immer auf, wenn ich Grafische Ausgaben oder ich nicht selbst beendene Programme aufrufe und nichtsmehr geht.

Wie erstellt man einen Threard der das ermöglicht?

Idee:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
function incommingcmd(cmd[quote]:String):boolean;
var
  ThreadID: DWORD;
  ThreadHandle: THandle;
begin
  ThreadHandle:=CreateThread(nil0, TFNThreadStartRoutine(@auswerten), nil0, ThreadID);
  if ThreadHandle<>0 then CloseHandle(ThreadHandle);
end;


---> Folge und Probleam

Die Internetkomonenten (Indy) lassen sich nur mit Funktionen die Form1.idTCP aufgerufen werden diese sind aber von Thread scheibar nicht möglich, giebt es da eine Lösung?

Danke! Für jede Hilfe!

Moderiert von user profile iconraziel: Delphi-Tags hinzugefügt
Sascha999999999 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 17



BeitragVerfasst: Di 19.12.06 23:44 
Titel: Problem mit lösung!
Hallo, ich binns nochmal Problem simpel gelöst, nicht "Tform1" sondern nur "form1", jetzt funktioniert das aber das Programm schießt sich nichtmeht:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
function ausführen():boolen;
begin
//Code
Application.terminate;
end;


Schießt sich also nicht.

Moderiert von user profile iconraziel: Delphi-Tags hinzugefügt