[转]Delphi调用cmd并取得输出文本

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

1 procedure CheckResult(b: Boolean);
 2 begin
 3   if not b then
 4     raise Exception.Create(SysErrorMessage(GetLastError));
 5 end;
 6
 7 function RunDOS(const CommandLine: string): string;
 8 var
 9   HRead, HWrite: THandle;
10   StartInfo: TStartupInfo;
11   ProceInfo: TProcessInformation;
12   b: Boolean;
13   sa: TSecurityAttributes;
14   inS: THandleStream;
15   sRet: TStrings;
16 begin
17   Result := ‘‘;
18   FillChar(sa, sizeof(sa), 0);
19 //设置允许继承,否则在NT和2000下无法取得输出结果
20   sa.nLength := sizeof(sa);
21   sa.bInheritHandle := True;
22   sa.lpSecurityDescriptor := nil;
23   b := CreatePipe(HRead, HWrite, @sa, 0);
24   CheckResult(b);
25
26   FillChar(StartInfo, SizeOf(StartInfo), 0);
27   StartInfo.cb := SizeOf(StartInfo);
28   StartInfo.wShowWindow := SW_HIDE;
29 //使用指定的句柄作为标准输入输出的文件句柄,使用指定的显示方式
30   StartInfo.dwFlags := STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW;
31   StartInfo.hStdError := HWrite;
32   StartInfo.hStdInput := GetStdHandle(STD_INPUT_HANDLE); //HRead;
33   StartInfo.hStdOutput := HWrite;
34
35   b := CreateProcess(nil, //lpApplicationName: PChar
36     PChar(CommandLine), //lpCommandLine: PChar
37     nil, //lpProcessAttributes: PSecurityAttributes
38     nil, //lpThreadAttributes: PSecurityAttributes
39     True, //bInheritHandles: BOOL
40     CREATE_NEW_CONSOLE,
41     nil,
42     nil,
43     StartInfo,
44     ProceInfo);
45
46   CheckResult(b);
47   WaitForSingleObject(ProceInfo.hProcess, INFINITE);
48
49   inS := THandleStream.Create(HRead);
50   if inS.Size > 0 then
51   begin
52     sRet := TStringList.Create;
53     sRet.LoadFromStream(inS);
54     Result := sRet.Text;
55     sRet.Free;
56   end;
57   inS.Free;
58
59   CloseHandle(HRead);
60   CloseHandle(HWrite);
61 end;
时间: 2024-08-24 17:09:36

[转]Delphi调用cmd并取得输出文本的相关文章

[转]Delphi调用cmd的两种方法

delphi调用cmd的两种方法vars:string;begins:='cmd.exe /c '+edit1.Text+' >c:\1.txt';winexec(pchar(s),sw_hide);sleep(2000);memo1.Lines.LoadFromFile('c:\1.txt'); 2shellexecute(handle,nil,'cmd.exe',pchar(form2.edit1.text),nil,sw_hide);WinExec主要运行EXE文件.如:WinExec(’

JAVA中调用CMD命令,并输出执行结果

package com.wzw.util; import java.io.BufferedReader; import java.io.InputStreamReader; public class CmdDemo { public static void main(String[] args) { BufferedReader br = null; try { Process p = Runtime.getRuntime().exec("net user"); br = new Bu

Delphi执行DOS命令显示到文本框

Delphi执行DOS命令显示到文本框(TMemo) 2007-11-24 22:03 procedure CmdExecAndView(FileName: string; memo: TMemo); procedure _AddInfo(mmInfo:TMemo; S: string; var line: string); var i, p: Integer; begin if mmInfo.Lines.Count > 800 then mmInfo.Lines.Clear; //去掉 \r

在IIS中ASP.NET调用cmd程序权限不足

问题 在本地代码中调用cmd 显示的路径为C:\Users\用户名> 而在服务器中调用cmd 显示的路径为C:\Windows\system32\inetsrv > 原因 服务器中调用的不是系统的cmd.exe 而是调用C:\Windows\system32\inetsrv 中的appcmd.exe 该工具来配置和查询 Web 服务器上的对象,并以文本或 XML 格式返回输出. 解决方法 创建一个iis用户(Administrator)注:创建的iis用户必须要有密码,还需要为iis用户添加执

C#程序调用CMD执行命令

在windows环境下,命令行程序为cmd.exe,是一个32位的命令行程序,微软Windows系统基于Windows上的命令解释程序,类似于微软的DOS操作系统.输入一些命令,cmd.exe可以执行,比如输入shutdown -s就会在30秒后关机.总之,它非常有用.打开方法:开始-所有程序-附件 或 开始-寻找-输入:cmd/cmd.exe 回车.它也可以执行BAT文件. 下面介绍使用C#程序调用cmd执行命令: 代码: 1 using System; 2 using System.Coll

客户端调用 CMD 命令并回显结果。

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Windows.Forms; using Syste

Java调用cmd命令 打开一个站点

使用Java程序打开一个站点 近期做了个东西使用SWT技术在一个client程序 须要升级时在提示升级 点击窗口上的一个连接 打开下载网页 花费了我非常长时间 用到了把它记录下来  怕是忘记,须要时能够直接来用到.         try { //不是在Java程序中运行而是在操作系统中运行             Runtime.getRuntime().exec("cmd /c start http://blog.csdn.net/blogluoqi/"); //Runtime.g

C#程序调用cmd执行命令(转)

C#通过程序来调用cmd命令的操作 string str = Console.ReadLine(); System.Diagnostics.Process p = new System.Diagnostics.Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动 p.StartInfo.RedirectStandardInput = true

C#程序调用cmd.exe执行命令

代码部分 using System.Diagnostics; public class CmdHelper { private static string CmdPath = @"C:\Windows\System32\cmd.exe"; /// <summary> /// 执行cmd命令 /// 多命令请使用批处理命令连接符: /// <![CDATA[ /// &:同时执行两个命令 /// |:将上一个命令的输出,作为下一个命令的输入 /// &&