如何让Delphi调用外部程序并等待其运行结束

如何让Delphi调用外部程序并等待其运行结束,第1张

调用下面的WinExecAndWait32 函数即可。

function  WinExecAndWait32(FileName:String  Visibility  :  integer):  DWORD  

var  

        zAppName:array[0..512]  of  char

        zCurDir:array[0..255]  of  char

        WorkDir:String

        StartupInfo:TStartupInfo

        ProcessInfo:TProcessInformation

begin

        StrPCopy(zAppName,FileName)

        GetDir(0,WorkDir)

        StrPCopy(zCurDir,WorkDir)

        FillChar(StartupInfo,Sizeof(StartupInfo),#0)

        StartupInfo.cb  :=  Sizeof(StartupInfo)

        StartupInfo.dwFlags  :=  STARTF_USESHOWWINDOW

        StartupInfo.wShowWindow  :=  Visibility

        if  not  CreateProcess(

        nil,

        zAppName,  {  pointer  to  command  line  string  }

        nil,  {  pointer  to  process  security  attributes  }

        nil,  {  pointer  to  thread  security  attributes  }

        false,  {  handle  inheritance  flag  }

        CREATE_NEW_CONSOLE  or  {  creation  flags  }

        NORMAL_PRIORITY_CLASS,

        nil,  {  pointer  to  new  environment  block  }

        nil,  {  pointer  to  current  directory  name  }

        StartupInfo,  {  pointer  to  STARTUPINFO  }

        ProcessInfo  {  pointer  to  PROCESS_INF  }

        )

        then  Result  :=  $FFFFFFFF  else  begin

                WaitforSingleObject(ProcessInfo.hProcess,INFINITE)

                GetExitCodeProcess(ProcessInfo.hProcess,Result)

        end

end

procedure TForm1.N9Click(Sender: TObject)

var i:integer

begin

     WinExecAndWait32('PAS2 >tempc2.txt',0)

     EXECUTEFILE('notepad.EXE','tempc2.txt','',sw_showmaximized)

end

cannot terminate an externally created thread

无法终止外部创建线程

从提示信息推测,建议查看程序在结束之前,是否有线程没有正常关闭。

用下面这个函数可以解决你的问题:

function  WinExecAndWait32(FileName:String  Visibility  :  integer):  DWORD  

var  

        zAppName:array[0..512]  of  char

        zCurDir:array[0..255]  of  char

        WorkDir:String

        StartupInfo:TStartupInfo

        ProcessInfo:TProcessInformation

begin

        StrPCopy(zAppName,FileName)

        GetDir(0,WorkDir)

        StrPCopy(zCurDir,WorkDir)

        FillChar(StartupInfo,Sizeof(StartupInfo),#0)

        StartupInfo.cb  :=  Sizeof(StartupInfo)

        StartupInfo.dwFlags  :=  STARTF_USESHOWWINDOW

        StartupInfo.wShowWindow  :=  Visibility

        if  not  CreateProcess(

        nil,

        zAppName,  {  pointer  to  command  line  string  }

        nil,  {  pointer  to  process  security  attributes  }

        nil,  {  pointer  to  thread  security  attributes  }

        false,  {  handle  inheritance  flag  }

        CREATE_NEW_CONSOLE  or  {  creation  flags  }

        NORMAL_PRIORITY_CLASS,

        nil,  {  pointer  to  new  environment  block  }

        nil,  {  pointer  to  current  directory  name  }

        StartupInfo,  {  pointer  to  STARTUPINFO  }

        ProcessInfo  {  pointer  to  PROCESS_INF  }

        )

        then  Result  :=  $FFFFFFFF  else  begin

                WaitforSingleObject(ProcessInfo.hProcess,INFINITE)

                GetExitCodeProcess(ProcessInfo.hProcess,Result)

        end

end


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/11876298.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存