var OutStr:String; StartInfo:TStartupInfo; ProceInfo:TProcessInformation; //run codec, there two methods: FillChar(StartInfo,SizeOf(StartInfo),0); StartInfo.cb:=SizeOf(StartInfo); StartInfo.wShowWindow:=SW_HIDE; StartInfo.dwFlags:=STARTF_USESTDHANDLES+STARTF_USESHOWWINDOW; StartInfo.hStdError:=0; StartInfo.hStdInput:=GetStdHandle(STD_INPUT_HANDLE);//HRead; StartInfo.hStdOutput:=0; if CreateProcess(pchar(ACodecExeName),pchar(EncodeExeFile+' '+EncodePassword),nil,nil,true,CREATE_NEW_CONSOLE+HIGH_PRIORITY_CLASS,nil,pChar(ExtractFilePath(ACodecExeName)),StartInfo,ProceInfo)=false then begin MessageBox(Handle,pchar(TransStr('ErrRunCodec','Error when run Codec!!')),pchar(Application.Title),MB_ICONERROR); exit; end; if (WaitForSingleObject(ProceInfo.hProcess,5000)=WAIT_TIMEOUT) then begin //wait for 5 seconds MessageBox(Handle,pchar(TransStr('ErrRunTimeout','Time out when run Codec!!')),pchar(Application.Title),MB_ICONERROR); CloseHandle(ProceInfo.hProcess); CloseHandle(ProceInfo.hThread); exit; end; CloseHandle(ProceInfo.hProcess); CloseHandle(ProceInfo.hThread);
错了,应该是: function ThreadComp.RunTerminal(const Prog, CommandLine,Dir: String;var ExitCode:DWORD): 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(PChar(Prog),//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); 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; AResultText:=EmptyStr; AResultText:=RunTerminal( ExtractFilePath(Application.ExeName)+'UPX.exe', ExtractFilePath(Application.ExeName)+'UPX.exe '+ACompOption+' '+AFileName, ExtractFilePath(AFileName), xCode);
比较复杂,我做过一个程序,运行CMD.exe,从Edit中接受命令,输出到Memo,不知道对16位程序是不是也适用(我想可以,不过没试过),要的话我可以发给你。
命令行后写上>a.txt然后从文本文件中读取例如
masm>a.txt
程序中
memo1.lines.loadfromfile('a.txt');
或者再进行拼凑不知能否满足楼主要求
OutStr:String;
StartInfo:TStartupInfo;
ProceInfo:TProcessInformation; //run codec, there two methods:
FillChar(StartInfo,SizeOf(StartInfo),0);
StartInfo.cb:=SizeOf(StartInfo);
StartInfo.wShowWindow:=SW_HIDE;
StartInfo.dwFlags:=STARTF_USESTDHANDLES+STARTF_USESHOWWINDOW;
StartInfo.hStdError:=0;
StartInfo.hStdInput:=GetStdHandle(STD_INPUT_HANDLE);//HRead;
StartInfo.hStdOutput:=0;
if CreateProcess(pchar(ACodecExeName),pchar(EncodeExeFile+' '+EncodePassword),nil,nil,true,CREATE_NEW_CONSOLE+HIGH_PRIORITY_CLASS,nil,pChar(ExtractFilePath(ACodecExeName)),StartInfo,ProceInfo)=false then begin
MessageBox(Handle,pchar(TransStr('ErrRunCodec','Error when run Codec!!')),pchar(Application.Title),MB_ICONERROR);
exit;
end;
if (WaitForSingleObject(ProceInfo.hProcess,5000)=WAIT_TIMEOUT) then begin //wait for 5 seconds
MessageBox(Handle,pchar(TransStr('ErrRunTimeout','Time out when run Codec!!')),pchar(Application.Title),MB_ICONERROR);
CloseHandle(ProceInfo.hProcess);
CloseHandle(ProceInfo.hThread);
exit;
end;
CloseHandle(ProceInfo.hProcess);
CloseHandle(ProceInfo.hThread);
function ThreadComp.RunTerminal(const Prog, CommandLine,Dir: String;var ExitCode:DWORD): 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(PChar(Prog),//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); 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; AResultText:=EmptyStr;
AResultText:=RunTerminal(
ExtractFilePath(Application.ExeName)+'UPX.exe',
ExtractFilePath(Application.ExeName)+'UPX.exe '+ACompOption+' '+AFileName,
ExtractFilePath(AFileName),
xCode);