procedure CheckResult(b: Boolean);
begin
if not b then
raise Exception.Create(SysErrorMessage(GetLastError));
end; function RunDOS(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), );
//设置允许继承,否则在NT和2000下无法取得输出结果
sa.nLength := sizeof(sa);
sa.bInheritHandle := True;
sa.lpSecurityDescriptor := nil;
b := CreatePipe(HRead, HWrite, @sa, );
CheckResult(b); FillChar(StartInfo, SizeOf(StartInfo), );
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 > then
begin
sRet := TStringList.Create;
sRet.LoadFromStream(inS);
Result := sRet.Text;
sRet.Free;
end;
inS.Free; CloseHandle(HRead);
CloseHandle(HWrite);
end; function GetDosOutput(const CommandLine: string): string;
var
SA: TSecurityAttributes;
SI: TStartupInfo;
PI: TProcessInformation;
StdOutPipeRead, StdOutPipeWrite: THandle;
WasOK: Boolean;
Buffer: array[..] of Char;
BytesRead: Cardinal;
WorkDir, Line: string;
begin
Application.ProcessMessages;
with SA do
begin
nLength := SizeOf(SA);
bInheritHandle := True;
lpSecurityDescriptor := nil;
end;
// create pipe for standard output redirection
CreatePipe(StdOutPipeRead, // read handle
StdOutPipeWrite, // write handle
@SA, // security attributes
// number of bytes reserved for pipe - default
);
try
// Make child process use StdOutPipeWrite as standard out,
// and make sure it does not show on screen.
with SI do
begin
FillChar(SI, SizeOf(SI), );
cb := SizeOf(SI);
dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
wShowWindow := SW_HIDE;
hStdInput := GetStdHandle(STD_INPUT_HANDLE); // don't redirect stdinput
hStdOutput := StdOutPipeWrite;
hStdError := StdOutPipeWrite;
end; // launch the command line compiler
WorkDir := ExtractFilePath(CommandLine);
WasOK := CreateProcess(nil, PChar(CommandLine), nil, nil, True, , nil,
PChar(WorkDir), SI, PI); // Now that the handle has been inherited, close write to be safe.
// We don't want to read or write to it accidentally.
CloseHandle(StdOutPipeWrite);
// if process could be created then handle its output
if not WasOK then
raise Exception.Create('Could not execute command line!')
else
try
// get all output until dos app finishes
Line := '';
repeat
// read block of characters (might contain carriage returns and line feeds)
WasOK := ReadFile(StdOutPipeRead, Buffer, , BytesRead, nil); // has anything been read?
if BytesRead > then
begin
// finish buffer to PChar
Buffer[BytesRead] := #;
// combine the buffer with the rest of the last run
Line := Line + Buffer;
end;
until not WasOK or (BytesRead = );
// wait for console app to finish (should be already at this point)
WaitForSingleObject(PI.hProcess, INFINITE);
finally
// Close all remaining handles
CloseHandle(PI.hThread);
CloseHandle(PI.hProcess);
end;
finally
result := Line;
CloseHandle(StdOutPipeRead);
end;
end; procedure TForm1.btn1Click(Sender: TObject);
var
hReadPipe, hWritePipe: THandle;
si: STARTUPINFO;
lsa: SECURITY_ATTRIBUTES;
pi: PROCESS_INFORMATION;
cchReadBuffer: DWORD;
ph: PChar;
fname: PChar;
begin
fname := allocmem();
ph := AllocMem();
lsa.nLength := sizeof(SECURITY_ATTRIBUTES);
lsa.lpSecurityDescriptor := nil;
lsa.bInheritHandle := True; if CreatePipe(hReadPipe, hWritePipe, @lsa, ) = false then
begin
ShowMessage('Can not create pipe!');
exit;
end;
fillchar(si, sizeof(STARTUPINFO), );
si.cb := sizeof(STARTUPINFO);
si.dwFlags := (STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW);
si.wShowWindow := SW_SHOW;
si.hStdOutput := hWritePipe;
StrPCopy(fname, EdtFilename.text);
if CreateProcess(nil, fname, nil, nil, true, , nil, nil, si, pi) = False then
begin
ShowMessage('can not create process');
FreeMem(ph);
FreeMem(fname);
Exit;
end; while (true) do
begin
if not PeekNamedPipe(hReadPipe, ph, , @cchReadBuffer, nil, nil) then break;
if cchReadBuffer <> then
begin
if ReadFile(hReadPipe, ph^, , cchReadBuffer, nil) = false then break;
ph[cchReadbuffer] := chr();
Mmo1.Lines.Add(ph);
end
else if (WaitForSingleObject(pi.hProcess, ) = WAIT_OBJECT_) then break;
Sleep();
end; ph[cchReadBuffer] := chr();
Mmo1.Lines.Add(ph);
CloseHandle(hReadPipe);
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
CloseHandle(hWritePipe);
FreeMem(ph);
FreeMem(fname);
end;

获取DOS命令的返回值.的更多相关文章

  1. 怎样获取shell函数的返回值及shell命令的返回值?

    1.获取shell函数调用的返回值: #!/bin/sh info() { cat jlb.sh } res=`info` echo "state: "$? echo " ...

  2. 关于ExecuteNonQuery执行存储过程的返回值 、、实例讲解存储过程的返回值与传出参数、、、C#获取存储过程的 Return返回值和Output输出参数值

    关于ExecuteNonQuery执行存储过程的返回值 用到过ExecuteNonQuery()函数的朋友们在开发的时候肯定这么用过. if(cmd.ExecuteNonQuery("xxx ...

  3. [shell]管道连接的命令判断返回值

    场景: 在bash执行管道连接的命令,需要获取到各个命令的返回值用于判断 在脚本中我们可能需要将执行结果打印到屏幕,同时保存在文件中供后面分析用,写出如下的命令 command 2>&1 ...

  4. bash命令行返回值和展开

    bash命令行返回值和展开 标签(空格分隔): bash,命令,状态,展开 1.命令状态结果和执行结果 (1)命令执行的状态返回值,命令执行完成之后,其执行状态结果值保存于bash的特殊状态变量$?中 ...

  5. [转]Linux命令的返回值

    Linux命令的返回值 对于某些监测脚本和探测命令蛮有用的: 在 Linux 下,不管你是启动一个桌面程序也好,还是在控制台下运行命令,所有的程序在结束时,都会返回一个数字值,这个值叫做返回值,或者称 ...

  6. Selenium2学习-036-WebUI自动化实战实例-034-JavaScript 在 Selenium 自动化中的应用实例之六(获取 JS 执行结果返回值)

    Selenium 获取 JavaScript 返回值非常简单,只需要在 js 脚本中将需要返回的数据 return 就可以,然后通过方法返回 js 的执行结果,方法源码如下所示: /** * Get ...

  7. 7 -- Spring的基本用法 -- 10... 获取其他Bean的属性值;获取Field值;获取任意方法的返回值

    7.10 高级依赖关系配置 组件与组件之间的耦合,采用依赖注入管理:但基本类型的成员变量值,应直接在代码中设置. Spring支持将任意方法的返回值.类或对象的Field值.其他Bean的getter ...

  8. 用jquery的ajax方法获取不到return返回值

    如果jquery中,获取不到ajax返回值. 两个错误写法会导致这种情况:1.ajax未用同步 2.在ajax方法中直接return返回值. 下面列举了三种写法,如果想成功获取到返回值,参考第三种写法 ...

  9. 如何通过submit提交form表单获取后台传来的返回值

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/qq_34651764/article/details/76373846 小伙伴是不是遇到过这样的问题 ...

随机推荐

  1. struts2——第一个案例

    步骤如下 编程工具等百度云分享 1.创建一个web项目 2.引入struts2的基本jar包 struts2的基本jar包百度云 链接:https://pan.baidu.com/s/1LBnPJhF ...

  2. 使用VirtualBox安装Android 4.2.2 x86 .

    http://blog.csdn.net/kunoy/article/details/8768205 virtual box 安装 android x86 不显示鼠标, --> 控制 --> ...

  3. Pandas统计函数

    统计方法有助于理解和分析数据的行为.现在我们将学习一些统计函数,可以将这些函数应用到Pandas的对象上. pct_change()函数 系列,DatFrames和Panel都有pct_change( ...

  4. Java多线程 - 线程同步

    多线程操作同一个对象时,容易引发线程安全问题.为了解决线程安全问题,Java多线程引入了同步监视器. 同步代码块 同步代码块语法格式如下: synchronized(obj){ //此处的代码即为同步 ...

  5. Oracle操作ORA-02289: 序列不存在

    解决方案:实现创建序列,创建语句如下所示: create sequence employees_seq minvalue maxvalue start increment cache ; 这时候再执行 ...

  6. linux find命令使用(转)

    常用命令 find  (目录)   [-type d | f]  (文件夹 | 文件)   -name   (名称,可使用正则表达式) find  /root  -name "*core&q ...

  7. Gogs/Gitea 在 docker 中部署

    注:Gitea是Gogs的一个分支版本,由多个维护者开发,支持搜索.lfs等,但是BUG较多,稳定性似乎没有Gogs好. #### 安装 ####// Gogs$ docker pull gogs/g ...

  8. Mac环境下Android Studio配置Git以及最基本使用

    Git是分布式版本管理工具,现在使用十分广泛,相对于SVN,GIT的使用更加方便,在离线环境下,仍然可以进行版本控制工作.且速度十分快.在Windows下,先需要自行安装Git程序,网址git-scm ...

  9. 016——VUE中v-show的使用与v-if的差异对比

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  10. Linux命令四

    作业一: 1) 开启Linux系统前添加一块大小为20G的SCSI硬盘 2) 开启系统,右击桌面,打开终端 安装的是命令行界面 3) 为新加的硬盘分区,一个主分区大小为10G,剩余空间给扩展分区,在扩 ...