WinExecAndWait32调用外部程序,等待外部程序运行完成

Posted on 2008-10-17 23:29  白兴科技  阅读(1367)  评论(0编辑  收藏  举报

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;  

 

Copyright © 2024 白兴科技
Powered by .NET 9.0 on Kubernetes