[转]Delphi调用cmd并取得输出文本
2024-08-21 03:48:36
//转自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;
最新文章
- 从netty-example分析Netty组件续
- Rafy 框架 - 插件级别的扩展点
- 如何解决MSI类型的Sharepoint Server2016 安装即点即用的office 2016 plus问题
- Windows配置端口转发
- Linux系统下UDP发送和接收广播消息小例子
- LVS+PIRANHA测试
- 开源DirectShow分析器和解码器: LAV Filter
- PHP json不转义
- Caffe可视化之VisualDL
- MySQL学习基础知识1
- (转)Debian 安装与卸载包命令
- 【原创】大叔经验分享(35)lzo格式支持
- 11 vs2015 连接oracle 11g 数据库及相关问题
- JDBC(7)—DAO
- 电脑上不安装Oracle时,C# 调用oracle数据库,Oracle客户工具 【转载】
- [转] impress.js学习
- DELPHI之崩溃地址排错代码查看 转
- Linux实现多线程高速下载
- Matlab2013a打开M文件乱码解决
- 北京Uber优步司机奖励政策(4月19日)
热门文章
- 查询及删除重复记录的SQL语句
- bzoj 1230: [Usaco2008 Nov]lites 开关灯【线段树】
- spoj 839 OPTM - Optimal Marks&;&;bzoj 2400【最小割】
- _bzoj1014 [JSOI2008]火星人prefix【Splay】
- [hdu1695] GCD【莫比乌斯反演】
- Styles and Themens(3)android所有主题表
- 【LeetCode】297. Serialize and Deserialize Binary Tree
- [转]Windows Azure安全概述
- Java_JDBC连接数据库
- 2019最新Android面试题