zoukankan      html  css  js  c++  java
  • 调用外部程序并等待程序运行结束《Lcemeaning》

    unit Unit1;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, Vcl.ExtCtrls,Clipbrd;
    
    type
      TForm1 = class(TForm)
        Button1: TButton;
        img1: TImage;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    function WinExecAndWait32(FileName: string; Visibility: Boolean): integer;
    var
      zAppName: array[0..512] of char; //存放应用程序名
      StartupInfo: TStartupInfo;
      ProcessInfo: TProcessInformation;
      exitCode: Dword;
      aVisibility: integer;
    begin
      StrPCopy(zAppName, FileName);
      FillChar(StartupInfo, Sizeof(StartupInfo), #0);
      //给StartupInfo结构体赋值
      StartupInfo.cb := Sizeof(StartupInfo);
      StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
      if Visibility then
        aVisibility := 1
      else
        aVisibility := 0;
    
      StartupInfo.wShowWindow := aVisibility;
      //调用CreateProcess 创建进程,执行指定的可执行文件
      if not CreateProcess(nil, zAppName, nil, nil, false
        , Create_NEW_CONSOLE or NORMAL_PRIORITY_CLASS
        , nil, nil, StartupInfo, ProcessInfo) then
          Result := -1
      else
        begin
          //等待可执行文件退出
          WaitforSingleObject(ProcessInfo.hProcess, INFINITE);
          //得到进程终止状态码
          GetExitCodeProcess(ProcessInfo.hProcess, exitCode);
          result := Exitcode;
        end;
    end;
    
    procedure TForm1.Button1Click(Sender: TObject);
    var
     i : Integer;
    begin
      Hide;
      Sleep(1000);
      i := WinExecAndWait32('D:PrintScreen.exe',True);
      if i=0 then
        img1.Picture.LoadFromClipboardFormat(CF_BITMAP,ClipBoard.GetAsHandle(CF_BITMAP),0);
      Show;
    end;
    
    end.
  • 相关阅读:
    重建索引报错-python数据分析
    机器学习实战1-2.1 KNN改进约会网站的配对效果 datingTestSet2.txt 下载方法
    机器学习实战1-1 KNN电影分类遇到的问题
    权限掩码umask详解
    spark配置(7)--on yarn配置
    spark配置(6)-独立应用程序
    spark配置(5)-独立应用程序
    spark配置(4)-----Spark Streaming
    spark配置(3)
    spark配置(2)
  • 原文地址:https://www.cnblogs.com/LceMeaning/p/3365766.html
Copyright © 2011-2022 走看看