写了个程序解决曾有人问及PB打开其它应用程序(包括批处理)后,如何判断是否运行结束

incats 2003-10-22 10:50:14
$PBExportHeader$nvo_filesrv.sru
$PBExportComments$文件服务
forward
global type nvo_filesrv from nonvisualobject
end type
type process_information from structure within nvo_filesrv
end type
type startupinfo from structure within nvo_filesrv
end type
type security_attributes from structure within nvo_filesrv
end type
end forward

type PROCESS_INFORMATION from structure
long hProcess
long hThread
long dwProcessID
long dwThreadID
end type

type startupinfo from structure
long cb
long lpReserved
long lpDesktop
long lpTitle
long dwX
long dwY
long dwXSize
long dwYSize
long dwXCountChars
long dwYCountChars
long dwFillAttribute
long dwFlags
integer wShowWindow
integer cbReserved2
long lpReserved2
long hStdInput
long hStdOutput
long hStdError
end type

type SECURITY_ATTRIBUTES from structure
long nLength
long lpSecurityDescriptor
long bInheritHandle
end type

global type nvo_filesrv from nonvisualobject autoinstantiate
end type

type prototypes
function ulong CreateProcessA(string lpApplicationName, string lpCommandLine, SECURITY_ATTRIBUTES lpProcessAttributes, SECURITY_ATTRIBUTES lpThreadAttributes,long bInheritHandles, long dwCreationFlags, long lpEnvironment, string lpCurrentDirectory, STARTUPINFO lpStartupInfo, ref PROCESS_INFORMATION lpProcessInformation) library "kernel32"
function long WaitForSingleObject(long hHandle,long dwMilliseconds) library "kernel32.dll"
subroutine Sleep(ulong dwMilliseconds) library "kernel32"

end prototypes

type variables


end variables
forward prototypes
public subroutine aa_readme ()
public function long of_runfile (string as_filename)
public function long of_runfile (string as_filename, long al_waittime)
protected function long runfile (string as_filename, string as_param)
public function long of_runfile (string as_filename, string as_param, long al_waittime)
end prototypes

public subroutine aa_readme ();/* 调用示例:

1. 运行程序, 并立刻返回:
nvo_filesrv n_file
n_file.of_RunFile("c:\aa.bat")

2. 运行程序, 延时5秒钟后返回:
nvo_filesrv n_file
n_file.of_RunFile("c:\aa.bat", 5)

3. 运行程序, 直到程序结束后返回:
nvo_filesrv n_file
n_file.of_RunFile("c:\aa.bat", -1)


Beijing tank
2003.10
*/
end subroutine

public function long of_runfile (string as_filename);long ll_hProcess

ll_hProcess = runfile(as_filename, "")

return ll_hProcess
end function

public function long of_runfile (string as_filename, long al_waittime);return of_runfile(as_filename, "", al_waittime)
end function

protected function long runfile (string as_filename, string as_param);PROCESS_INFORMATION lpProcessInformation
SECURITY_ATTRIBUTES lpProcessAttributes
STARTUPINFO lpStartupInfo

long NULL_LONG
string NULL_STRING
SetNull(NULL_LONG)
SetNull(NULL_STRING)

string ls_CommandLine, ls_param
if isnull(as_filename) or len(as_filename) <= 0 then return -1
if isnull(as_param) then
ls_param = ""
else
ls_param = as_param
end if

ls_CommandLine = "~"" + as_filename + "~" " + ls_param

string ls_path
SetNull(ls_path)

CreateProcessA(NULL_STRING, ls_CommandLine, lpProcessAttributes, lpProcessAttributes, NULL_LONG, &
NULL_LONG, NULL_LONG, ls_path, lpStartupInfo, lpProcessInformation)

return lpProcessInformation.hProcess
end function

public function long of_runfile (string as_filename, string as_param, long al_waittime);//////////////////////////////////////////////////////////////////////
//
// Function: of_runfile
//
// Purpose: 运行指定程序
//
// Scope: public
//
// Arguments: as_filename 指定程序文件
// as_param 指定程序的运行参数
// al_waittime 延迟时间(单位:秒)
// 指定此时间内调用程序处于等待状态
// 为负数时, 表示调用程序需一直等到新进程不存在后,再继续运行.
//
// Returns: long
// 延迟时间消耗完后, 如新进程依然存在, 就返回该进程的句柄.
// 如新进程不存在了, 就返回0.
// 如新进程死锁或没产生,就返回-1.
//
//////////////////////////////////////////////////////////////////////

long ll_hProcess
long ll_waittime
long ll_return, ret

long INFINITE = -1
long WAIT_TIMEOUT = 258
long WAIT_OBJECT_0 = 0
long WAIT_ABANDONED = 128

if al_waittime < 0 then
//为负数, 进入无限等待状态
ll_waittime = INFINITE
else
ll_waittime = al_waittime * 1000
end if

ll_hProcess = runfile(as_filename, as_param)
if ll_hProcess <= 0 then return -1

ll_return = WaitForSingleObject(ll_hProcess, ll_waittime)

choose case ll_return
case WAIT_TIMEOUT
ret = ll_hProcess
case WAIT_OBJECT_0
ret = 0
case else
ret = -1
end choose

return ret
end function
on nvo_filesrv.create
call super::create
TriggerEvent( this, "constructor" )
end on

on nvo_filesrv.destroy
TriggerEvent( this, "destructor" )
call super::destroy
end on

...全文
43 点赞 收藏 1
写回复
1 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
yjhyn 2003-10-22
学习中……
回复
相关推荐
发帖
API 调用
创建于2007-09-28

667

社区成员

PowerBuilder API 调用
申请成为版主
帖子事件
创建了帖子
2003-10-22 10:50
社区公告
暂无公告