中国程序员联盟 正在重新改版中ing 不便之处还请见谅 改版后将内容涉及java delphi .net php
 
  首页 | 数据库开发 | 网络通讯 | 多线程 | 多媒体开发 | 图像处理 | 程序人生 | 系统函数 | 控件开发 | Web服务
 
  当前位置:笨鱼delphi技术网>系统函数>文章内容

delphi 如何将一控制台输出到应用程序

来源:国外 关于:Jonas Bilinkevicius 发布时间:2007-07-03   [收藏] [推荐]
Problem/Question/Abstract:
Does anyone have a working example of a GUI application that redirects the screen output from a console application? I've tried it using the CreateProcess API function, however it only works for me when I launch it through the D5 UI, but not when I double click the compiled executable.
Answer:
unit consoleoutput;
interface
uses
  Controls, Windows, SysUtils, Forms;
function GetDosOutput(const CommandLine: string): string;
implementation
function GetDosOutput(const CommandLine: string): string;
var
  SA: TSecurityAttributes;
  SI: TStartupInfo;
  PI: TProcessInformation;
  StdOutPipeRead, StdOutPipeWrite: THandle;
  WasOK: Boolean;
  Buffer: array[0..255] 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}
    0 {number of bytes reserved for pipe - 0 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), 0);
      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 := 'C:\';}
    WorkDir := '';
    WasOK := CreateProcess(nil, PChar(CommandLine), nil, nil, True, 0, nil, nil, 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, 255, BytesRead, nil);
        {has anything been read?}
        if BytesRead > 0 then
        begin
          {finish buffer to PChar}
          Buffer[BytesRead] := #0;
          {combine the buffer with the rest of the last run}
          Line := Line + Buffer;
        end;
      until
        not WasOK or (BytesRead = 0);
      {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;
end.

[浏览: 次]   
上一篇:delphi 如何重画Caption Bar   下一篇:delphi 不经确认关闭应用程序
[收藏] [推荐] [返回顶部] [打印本页] [关闭窗口]  
    评论加载中…
google adsense热点文章
·delphi Delphi中ShellExecute的妙用
·delphi 如何快速读取文本文件
·delphi 如何判断输入值是否中文
·delphi 在应用层截获键盘消息
·delphi delphi实现服务开启与关闭
·delphi 实时记录事件日志
·delphi 使MEMO自动滚动
·delphi 如何区分键盘两个Enter键
·delphi 切换界面的方法
·delphi 汉字输入法的编程及使用
·delphi Delphi程序输入法自动切换最简
·delphi 消息是由谁来发出又由谁来完成
     delphi技术网 | firefox 下载 | Avant Browser下载 | dedecms 技术网 | drupal 爱好者 | php 技术网
  Copyright@www.delphichm.com,2006-2009.All Rights Reserved.
 
程序员联盟 | delphi Java .net|QQ:707102932