VB 判定程序是否已经结束

VB 判定程序是否已经结束,第1张

概述Option Explicit Dim DemoFile$ Private Const NORMAL_PRIORITY_CLASS = &H20    '如果进程位于前台,则基本值是9;如果在后台,则优先值为7 Private Const INFINITE = &HFFFFFFFF Private Const WAIT_TIMEOUT = &H102& '对象保持未发出信号的状态,但等待超时时间已

Option Explicit


Dim Demofile$
Private Const norMAL_PRIORITY_CLASS = &H20 '如果进程位于前台,则基本值是9;如果在后台,则优先值为7
Private Const INFINITE = &HFFFFFFFF
Private Const WAIT_TIMEOUT = &H102& '对象保持未发出信号的状态,但等待超时时间已经超过
'说明∶PROCESS_informatION结构由CreateProcess函数将关于新建立的进程和'主要线索的信息写入其中成员变量

Private Type PROCESS_informatION
hProcess As Long
hThread As Long
DWProcessID As Long
DWThreadID As Long
End Type


'说明∶STARTUPINFO结构用在CreateProcess函数中指定为新进程建立的新窗口的主要属性。这一信息影响由Createwindows函数建立的第一个窗口

Private Type STARTUPINFO
cb As Long
lpReserved As String
lpDesktop As String
lpTitle As String
DWX As Long
DWY As Long
DWXSize As Long
DWYSize As Long
DWXCountChars As Long
DWYCountChars As Long
DWFillAttribute As Long
DWFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdinput As Long
hStdOutput As Long
hStdError As Long
End Type


Private Declare Function CloseHandle lib "kernel32" (ByVal hObject As Long) As Long

Private Declare Function WaitForSingleObject lib "kernel32" (ByVal hHandle As Long,ByVal DWMilliseconds As Long) As Long

Private Declare Function CreateProcess lib "kernel32" Alias " CreateProcessA" (ByVal lpApplicationname As String,ByVal lpCommandline As String,_

ByVal lpProcessAttributes As Long,ByVal lpThreadAttributes As Long,ByVal binheritHandles As Long,ByVal DWCreationFlags As Long,_

lpEnvironment As Any,ByVal lpCurrentDirectory As String,lpStartupInfo As STARTUPINFO,lpProcessinformation As PROCESS_informatION) As Long

Private Declare Function WaitForinputIDle lib "user32" (ByVal hProcess As Long,ByVal DWMilliseconds As Long) As Long

Private Sub command1_Click()
Dim res&
Dim sinfo As STARTUPINFO
Dim pinfo As PROCESS_informatION
sinfo.cb = Len(sinfo)
sinfo.lpReserved = vbNullString
sinfo.lpDesktop = vbNullString
sinfo.lpTitle = vbNullString
sinfo.DWFlags = 0
Label1.Caption = "正在启动程序"
Label1.Refresh
' CreateProcess函数,用于创建一个新的进程
res = CreateProcess(Demofile,vbNullString,True,norMAL_PRIORITY_CLASS,ByVal 0&,sinfo,pinfo)
If res Then
Label1.Caption = "程序正在运行"
WaitForTerm pinfo
End If
Label1.Caption = "程序已经结束"
End Sub

Private Sub WaitForTerm(pinfo As PROCESS_informatION)
Dim res&
' 等待指定的进程进入空闲状态,,空闲(IDle)指的是进程准备处理
'一条消息、但目前暂时没有消息需要处理的一种状态
Call WaitForinputIDle(pinfo.hProcess,INFINITE)
' 关闭指定进程
Call CloseHandle(pinfo.hThread)
Command1.Enabled = False
Label1.Refresh
Do
'等待发出信号
res = WaitForSingleObject(pinfo.hProcess,0)
If res <> WAIT_TIMEOUT Then '如果对象发出了信号
Exit Do
End If
DoEvents
Loop While True
Command1.Enabled = True
Call CloseHandle(pinfo.hProcess)
End Sub

Private Sub Form_Load()
Demofile = inputBox$("请输入需要运行的程序名","C:\windows\notepad.exe")
End Sub

例程如下: Option Explicit Private Declare Function OpenProcess lib "kernel32" (ByVal DWDesiredAccess As Long,ByVal binheritHandle As Long,ByVal DWProcessID As Long) As Long Private Declare Function GetExitCodeProcess lib "kernel32" (ByVal hProcess As Long,lpExitCode As Long) As Long Private Declare Function CloseHandle lib "kernel32" (ByVal hObject As Long) As Long Private Const PROCESS_query_informatION = &H400 Private Const STATUS_PENDING = &H103 Private Sub RunShell(cmdline As String) Dim hProcess As Long Dim ProcessID As Long Dim ExitCode As Long ProcessID = Shell(cmdline,1) hProcess = OpenProcess(PROCESS_query_informatION,False,ProcessID) Do Call GetExitCodeProcess(hProcess,ExitCode) DoEvents Loop While ExitCode = STATUS_PENDING Call CloseHandle(hProcess) MsgBox cmdline & "已经关闭" End Sub Private Sub Command1_Click() RunShell "c:\windows\notepad.exe" End Sub

总结

以上是内存溢出为你收集整理的VB 判定程序是否已经结束全部内容,希望文章能够帮你解决VB 判定程序是否已经结束所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

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

原文地址: http://outofmemory.cn/langs/1284334.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-09
下一篇 2022-06-09

发表评论

登录后才能评论

评论列表(0条)

保存