//转自http://www.oschina.net/code/snippet_136241_3980 

1 procedure CheckResult(b: Boolean);
begin
if not b then
raise Exception.Create(SysErrorMessage(GetLastError));
end; function RunDOS(const CommandLine: string): 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 or 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,
nil,
StartInfo,
ProceInfo); CheckResult(b);
WaitForSingleObject(ProceInfo.hProcess, INFINITE); 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;

最新文章

  1. 从netty-example分析Netty组件续
  2. Rafy 框架 - 插件级别的扩展点
  3. 如何解决MSI类型的Sharepoint Server2016 安装即点即用的office 2016 plus问题
  4. Windows配置端口转发
  5. Linux系统下UDP发送和接收广播消息小例子
  6. LVS+PIRANHA测试
  7. 开源DirectShow分析器和解码器: LAV Filter
  8. PHP json不转义
  9. Caffe可视化之VisualDL
  10. MySQL学习基础知识1
  11. (转)Debian 安装与卸载包命令
  12. 【原创】大叔经验分享(35)lzo格式支持
  13. 11 vs2015 连接oracle 11g 数据库及相关问题
  14. JDBC(7)—DAO
  15. 电脑上不安装Oracle时,C# 调用oracle数据库,Oracle客户工具 【转载】
  16. [转] impress.js学习
  17. DELPHI之崩溃地址排错代码查看 转
  18. Linux实现多线程高速下载
  19. Matlab2013a打开M文件乱码解决
  20. 北京Uber优步司机奖励政策(4月19日)

热门文章

  1. 查询及删除重复记录的SQL语句
  2. bzoj 1230: [Usaco2008 Nov]lites 开关灯【线段树】
  3. spoj 839 OPTM - Optimal Marks&&bzoj 2400【最小割】
  4. _bzoj1014 [JSOI2008]火星人prefix【Splay】
  5. [hdu1695] GCD【莫比乌斯反演】
  6. Styles and Themens(3)android所有主题表
  7. 【LeetCode】297. Serialize and Deserialize Binary Tree
  8. [转]Windows Azure安全概述
  9. Java_JDBC连接数据库
  10. 2019最新Android面试题