我
我爱吃草莓
Unregistered / Unconfirmed
GUEST, unregistred user!
一个朋友给了下面的代码,目的是在EDIT1里面输入DOS命令,然后在MEMO1里面回显,可是执行的时候总是出错,哪位富翁能帮着看一下是什么原因导致的。[]
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
Edit1: TEdit;
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure ShellThread;
end;
var
Form1: TForm1;
Terminated: boolean;
Cmdbuffer:string;
implementation
{$R *.dfm}
procedure TForm1.ShellThread;
const
MAX_CHUNK: dword = 8191;
var
Buffer: array [0..8191] of byte;
SecurityAttributes: SECURITY_ATTRIBUTES;
hiRead, hoRead, hiWrite, hoWrite: THandle;
StartupInfo: TSTARTUPINFO;
ProcessInfo: TProcessInformation;
BytesRead, BytesWritten, ExitCode, PipeMode: dword;
Temp:string;
begin
SecurityAttributes.nLength := SizeOf(SECURITY_ATTRIBUTES);
SecurityAttributes.lpSecurityDescriptor := nil;
SecurityAttributes.bInheritHandle := True;
CreatePipe(hiRead, hiWrite, @SecurityAttributes, 0);
CreatePipe(hoRead, hoWrite, @SecurityAttributes, 0);
GetStartupInfo(StartupInfo);
StartupInfo.hStdOutput := hoWrite;
StartupInfo.hStdError := hoWrite;
StartupInfo.hStdInput := hiRead;
StartupInfo.dwFlags := STARTF_USESHOWWINDOW + STARTF_USESTDHANDLES;
StartupInfo.wShowWindow := SW_HIDE;
CreateProcess(nil,'cmd.exe', nil, nil, True, CREATE_NEW_CONSOLE, nil, nil, StartupInfo, ProcessInfo);
CloseHandle(hoWrite);
CloseHandle(hiRead);
PipeMode := PIPE_NOWAIT;
SetNamedPipeHandleState(hoRead, PipeMode , nil, nil);
while (not Terminated) do
begin
if Terminated then Break;
Sleep(100);
GetExitCodeProcess(ProcessInfo.hProcess, ExitCode);
if ExitCode <> STILL_ACTIVE then Break;
ReadFile(hoRead, Buffer, MAX_CHUNK, BytesRead, nil);
if BytesRead > 0 then
begin
SetLength(Temp,BytesRead);
Copymemory(@Temp[1],@Buffer[0],BytesRead);
Memo1.Text:=Temp;
end;
Sleep(100);
if Length(Cmdbuffer) > 0 then
begin
WriteFile(hiWrite, CmdBuffer[1], Length(CmdBuffer), BytesWritten, nil);
CmdBuffer:='';
end;
end;
GetExitCodeProcess(ProcessInfo.hProcess, ExitCode);
if ExitCode = STILL_ACTIVE then TerminateProcess(ProcessInfo.hProcess, 0);
CloseHandle(hoRead);
CloseHandle(hiWrite);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Cmdbuffer:=Edit1.Text ; //执行的命令
end;
procedure TForm1.Button2Click(Sender: TObject); //开始按钮
var ThreadIdword;
begin
Terminated:=False;
CreateThread(nil,0,@TForm1.ShellThread,nil,0,ThreadId);
end;
procedure TForm1.Button3Click(Sender: TObject); //停止按钮
begin
Terminated:=True;
end;
end.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
Edit1: TEdit;
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure ShellThread;
end;
var
Form1: TForm1;
Terminated: boolean;
Cmdbuffer:string;
implementation
{$R *.dfm}
procedure TForm1.ShellThread;
const
MAX_CHUNK: dword = 8191;
var
Buffer: array [0..8191] of byte;
SecurityAttributes: SECURITY_ATTRIBUTES;
hiRead, hoRead, hiWrite, hoWrite: THandle;
StartupInfo: TSTARTUPINFO;
ProcessInfo: TProcessInformation;
BytesRead, BytesWritten, ExitCode, PipeMode: dword;
Temp:string;
begin
SecurityAttributes.nLength := SizeOf(SECURITY_ATTRIBUTES);
SecurityAttributes.lpSecurityDescriptor := nil;
SecurityAttributes.bInheritHandle := True;
CreatePipe(hiRead, hiWrite, @SecurityAttributes, 0);
CreatePipe(hoRead, hoWrite, @SecurityAttributes, 0);
GetStartupInfo(StartupInfo);
StartupInfo.hStdOutput := hoWrite;
StartupInfo.hStdError := hoWrite;
StartupInfo.hStdInput := hiRead;
StartupInfo.dwFlags := STARTF_USESHOWWINDOW + STARTF_USESTDHANDLES;
StartupInfo.wShowWindow := SW_HIDE;
CreateProcess(nil,'cmd.exe', nil, nil, True, CREATE_NEW_CONSOLE, nil, nil, StartupInfo, ProcessInfo);
CloseHandle(hoWrite);
CloseHandle(hiRead);
PipeMode := PIPE_NOWAIT;
SetNamedPipeHandleState(hoRead, PipeMode , nil, nil);
while (not Terminated) do
begin
if Terminated then Break;
Sleep(100);
GetExitCodeProcess(ProcessInfo.hProcess, ExitCode);
if ExitCode <> STILL_ACTIVE then Break;
ReadFile(hoRead, Buffer, MAX_CHUNK, BytesRead, nil);
if BytesRead > 0 then
begin
SetLength(Temp,BytesRead);
Copymemory(@Temp[1],@Buffer[0],BytesRead);
Memo1.Text:=Temp;
end;
Sleep(100);
if Length(Cmdbuffer) > 0 then
begin
WriteFile(hiWrite, CmdBuffer[1], Length(CmdBuffer), BytesWritten, nil);
CmdBuffer:='';
end;
end;
GetExitCodeProcess(ProcessInfo.hProcess, ExitCode);
if ExitCode = STILL_ACTIVE then TerminateProcess(ProcessInfo.hProcess, 0);
CloseHandle(hoRead);
CloseHandle(hiWrite);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Cmdbuffer:=Edit1.Text ; //执行的命令
end;
procedure TForm1.Button2Click(Sender: TObject); //开始按钮
var ThreadIdword;
begin
Terminated:=False;
CreateThread(nil,0,@TForm1.ShellThread,nil,0,ThreadId);
end;
procedure TForm1.Button3Click(Sender: TObject); //停止按钮
begin
Terminated:=True;
end;
end.