第一:为什么要用label?用Memo不是更好?
第二:对于内部命令要写成cmd /c dir *.*的格式,Win98是command /c dir *.*,因为内部命令没有对应的可执行文件。
核心代码如下:
procedure TForm1.Button1Click(Sender: TObject);
var
SecurityAttributes : TSecurityAttributes;
hProcess
: TProcessInformation;
StartupInfo
: TStartupInfo;
hReadPipe,hWritePipe : THandle;
Buff : array [0..256] of char;
Num : DWORD;
dwExit:DWORD;
SendName : String;
s : String;
begin
SecurityAttributes.nLength:=Sizeof(SecurityAttributes);
SecurityAttributes.lpSecurityDescriptor:=nil;
SecurityAttributes.bInheritHandle:=true;
CreatePipe(hReadPipe,hWritePipe,@SecurityAttributes,0);
FillChar(StartupInfo,Sizeof(StartupInfo),#0);
StartupInfo.cb:=Sizeof(StartupInfo);
StartupInfo.dwFlags:=STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
StartupInfo.wShowWindow:=SW_HIDE;
StartupInfo.hStdOutput:=hWritePipe;
StartupInfo.hStdError:=hWritePipe;
if CreateProcess(nil,PChar(self.Edit1.Text),
@SecurityAttributes,
@SecurityAttributes,
true,0,nil,nil,
StartupInfo,
hProcess) then
begin
CloseHandle(hProcess.hThread);
WaitForSingleObject(hProcess.hProcess,INFINITE);
GetExitCodeProcess(hProcess.hProcess,dwExit);
CloseHandle(hProcess.hProcess);
CloseHandle(hWritePipe);
Num:=0; s:='';
while ReadFile(hReadPipe,Buff,256,Num,nil) do
begin
Buff[Num]:=#0;
s:=s+Buff;
end;
CloseHandle(hReadPipe);
//
self.Label1.Caption:=s;
end
else
MessageBox(self.Handle,PChar('调用命令 '+self.Edit1.Text+' 失败!'),'调用失败',MB_OK or MB_ICONWARNING);
end;