监视外部程序的运行情况
在VB在利用 Shell 函数可以运行一个外部程序,Shell 方法执行后会继续执行下面的代码,而我们不能确定外部程序什么时候调用结束。我们现在可以通过以下方法来实现对被调用程序的监视。在 Form1 的代码窗口中加入以下代码:
Private Declare Function CloseHandle Lib "kernel32"(ByVal hObject As Long) As Long
Private Declare Function OpenProcess Lib "kernel32"(ByVal dwDesiredAccess As Long,ByVal bInheritHandle As Long,ByVal dwProcessId As Long) As Long
Dim pID As Long
Dim hProgram As Long
Function StillRun(ByVal ProgramID) As Boolean
Dim lHProgram As Long
Dim lReturn As Long
hProgram = 0
hProgram = OpenProcess(0,False,ProgramID)
If Not hProgram = 0 Then
StillRun = True
Else
StillRun = False
End If
CloseHandle hProgram
End Function
Private Sub Form_Load()
If Dir$("c:\windows\cdplayer.exe")<> "" Then
Me.Show
pID = Shell("c:\windows\cdplayer.exe")
While StillRun(pID)
DoEvents
本内容试读结束,登录后可阅读更多
下载后可阅读完整内容,剩余1页未读,立即下载