Unit1
procedure TForm1.IntegratClick(Sender: TObject);
Const
Prog = 'a.exe';
ProgEnd = 'The termination of Preint.';
Var
ExitCode : Dword;
FileName,Result: String;
Restr : TStrings;
begin
ExitCode := 0;
FileName := XmPath + '/' + XmName;
CommandLine := Prog + ' ' + '"' + FileName + '"' ;
Dir := XmPath;
If (Dir = '') then
begin
save1Click(Sender);
Dir :=XmPath;
end;
MemoDisplay.Clear ;
Mediavar.Clear;
MediaVar.Strings[0] := CommandLine;
MediaVar.Strings[1] := Dir;
RunDosThread.Resume ;
MediaVar.Free ;
Result := 'Blank testing';
Restr := TStringList.Create ;
Restr.Add(Result);
MemoDisplay.Lines := Restr;
MemoDisPlay.Lines.Append(ProgEnd);
MemoDisPlay.Lines.Append('');
Restr.Free ;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
MemoDisplay.Clear;
MemoDisplay.ReadOnly := True;
RunDosThread:=TRunDosThread.Create(True);
end;
==========================
Unit2
procedure TRunDosThread.Execute;
var
Cmdl,dr:string;
ExitCd
word;
begin
cmdl := Form1.MediaVar[0];
dr := Form1.MediaVar[1];
ExitCd :=0;
{ Place thread code here }
If(Terminated)then
exit;
RunDOS(Cmdl, Dr, ExitCd);
end;
procedure TRunDosThread.CheckResult (b: Boolean);
begin
if not b then
Raise Exception.Create(SysErrorMessage(GetLastError));
end;
function TRunDosThread.RunDOS (const CommandLine,Dir: String;
ExitCode
word): String;
var
HRead,HWrite:THandle;
StartInfo:TStartupInfo;
ProceInfo:TProcessInformation;
b:Boolean;
sa:TSecurityAttributes;
inS:THandleStream;
sRet:TStrings;
begin
Result := '';
FillChar(sa,sizeof(sa),0);
//设置允许继承,否则在NT和2000下无法取得输出结果
sa.nLength := sizeof(sa);
sa.bInheritHandle := True;
sa.lpSecurityDescriptor := nil;
b := CreatePipe(HRead,HWrite,@sa,0);
CheckResult(b);
FillChar(StartInfo,SizeOf(StartInfo),0);
StartInfo.cb := SizeOf(StartInfo);
StartInfo.wShowWindow := SW_HIDE;
//使用指定的句柄作为标准输入输出的文件句柄,使用指定的显示方式
StartInfo.dwFlags := STARTF_USESTDHANDLES+STARTF_USESHOWWINDOW;
StartInfo.hStdError := HWrite;
StartInfo.hStdInput := GetStdHandle(STD_INPUT_HANDLE);//HRead;
StartInfo.hStdOutput := HWrite;
b := CreateProcess(Nil,//lpApplicationName: PChar
PChar(CommandLine), //lpCommandLine: PChar
nil, //lpProcessAttributes: PSecurityAttributes
nil, //lpThreadAttributes: PSecurityAttributes
True, //bInheritHandles: BOOL
CREATE_NEW_CONSOLE,
nil,
PChar(Dir),
StartInfo,
ProceInfo );
CheckResult(b);
WaitForSingleObject(ProceInfo.hProcess,INFINITE);
GetExitCodeProcess(ProceInfo.hProcess,ExitCode);
// GetExitCodeProcess(ProceInfo.hProcess,ExitCode);
inS := THandleStream.Create(HRead);
if inS.Size>0 then
begin
sRet := TStringList.Create;
sRet.LoadFromStream(inS);
Result := sRet.Text;
sRet.Free;
end;
inS.Free;
CloseHandle(HRead);
CloseHandle(HWrite);
end;