敬业的IT人 >> 编程开发 >> PowerBuilder >> PB调用外部程序及判断其完成的方法

PB调用外部程序及判断其完成的方法

敬业的IT人 互联网 佚名 2008-1-4 11:34:48

  在PB中常常需要运行一些外部的程序或命令,并等待其执行完成后,才接下来运行剩余的代码。我们可以有两种方法:

  先定义全局外部函数:

Function long ShellExecuteA (long hwnd, string lpOperation ,String lpFile, String lpParameters, String lpDirectory, Long nShowCmd) Library "shell32.dll"

Function long FindWindowA (String lpClassName , String lpWindowName ) Library "user32.dll"

Function boolean IsWindow (Long hwnd ) Library "user32.dll"

  第一种方式用Run() 函数,可在窗口上建立按扭,clicked事件中包含如下Script:

ulong ll_handle

int li_loop

SetPointer(HourGlass!)

//最小化执行xxx.bat

run("xxx.bat", Minimized!)

//循环到窗口打开,根据程序执行打开所需的时间设定li_loop的循环次数,可预留长一些。

for li_loop= 1 to 10000

ll_handle = FindWindowA("tty","xxx")

yield()

if ll_handle $#@60;$#@62; 0 then

exit

end if

next

//一直循环到窗口关闭

Do While isWindow(ll_handle)

Yield()

Loop

//应用执行完成

messagebox(‘ok’, ‘执行完成!’)

  这种方法的缺点是不能隐藏外部应用程序窗口,只能最小化。

  第二种方式用API函数,可以隐藏应用程序的窗口,但是调用bat批处理命令时需要先建立一个PIF文件指定执行完成后关闭窗口,否则窗口不会自行关闭。可在窗口上建立按扭,clicked事件中包含如下Script:

uint lu_return

ulong ll_handle

int li_loop

string ls_Path

SetPointer(HourGlass!)

lu_return = ShellExecutea(handle(parent), "open", "xxx.pif", "", ls_path, 0)

//最后一个参数改为 4,可以显示执行情况

if lu_return $#@62; 32 then

for li_loop= 1 to 10000

ll_handle = FindWindowA("tty","xxx")

yield()

if ll_handle $#@60;$#@62; 0 then

exit

end if

next

//一直循环到窗口关闭

Do While isWindow(lu_handle)

Yield()

Loop

//应用执行完成

Messag x("ok", "执行完成!")

Else

//error

messagebox("错误", "调用外部应用程序不成功,请检查应用程序路径!")

end if

进入讨论组讨论。
粤ICP备06119539号
Copyright CiscoSky.Org,Some Rights Reserved.
Email:me1228#tom.com