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. centos 安装 mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz 详细步骤

    1.卸载Linux系统上自带的mysql插件(old版本) 查找mysql相关安装: rpm -qa|grep -i mysql 可能会出现以下的一个或多个,没有更好,说明你的系统很干净,但是以防万一 ...

  2. 对拷 使用scp命令在两台linux上对拷文件或者文件夹

    以前一直是在服务器上tar打包压缩,下载到本地电脑上,再上传到另外一台服务器上,再解压. 其实使用scp命令就可以直接对拷文件或者文件夹了. scp就是secure copy,是用来进行远程文件拷贝的 ...

  3. Web Service简介

    1.1.Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求, ...

  4. 容器编排Kubernetes之kube-dns源码解读

    注:阅读DNS源码前,可以阅读DNS原理入门增加对DNS的认识. 架构图 这是我简单画的架构图,希望能帮助大家理解. 代码结构 k8s.io | dns | cmd // 三大组件的入口 | dnsm ...

  5. 浅谈 session 会话的原理

    先谈 cookie 网络传输基于的Http协议,是无状态的协议,即每次连接断开后再去连接,服务器是无法判断此次连接的客户端是谁. 如果每次数据传输都需要进行连接和断开,那造成的开销是很巨大的. 为了解 ...

  6. python 爬虫003-正则表达式简单介绍

    正则表达式,简单的说就是用一个“字符串”来描述一个特征,然后去验证另外一个“字符串”是否符合这个特征. 正则表达式在线测试工具 http://tool.chinaz.com/regex 实例一,判断字 ...

  7. MySQL忘记密码解决方案

    1.修改本地mysql目录中的my.ini文件 添加skip-grant-tables 2.在win +r 输入cmd,进行mysql的重启启动操作 net stop MySQL 停止服务      ...

  8. 【scala】循环

    1.while循环 Scala的while循环跟其他语言并没有很大差别. var i = 0; while(i<args.length){ println(i); i+=1; } Scala也有 ...

  9. null与""的区别

    两者的区别与 “数字0和没有不是同一种概念”是一个道理.null是空对象,""是空字符串null可以赋值给任何对象,而""就不行了,只能赋值给字符串对象如:St ...

  10. Centos6.8 JDK配置

    记录一下在这个服务器配置的过程 ssh root@IP Password --------------------------------------------------------------- ...