AHOI智能评测系统开发(五)
AHOI智能评测系统开发(五) 目前为止,测试系统de测试模块基本搞定。
为了系统的健壮性,采取超时中止选手程序进程的方案。
(前几次测试的时候,这是影响效率的主要问题,一遇到选手程序s掉,评测系统也挂掉。。
)超时靠timer来实现,同步执行之前,设
timer.enabled=true
timer.interval=1000 '间隔时间设为为一秒
timer触发代码为:
Private Sub Timer1_Timer()
Dim pro As Long
Static n As Integer
On Error Resume Next
pro = GetJingCheng('calc.exe')
If pro <> 0 Then
n = n + 1
If n = 10 Then
pro = GetJingCheng('calc.exe')
If pro = 0 Then
n = 0
Timer1.Enabled = False
Else
EndJingCheng (pro)
n = 0
End If
End If
End If
End Sub
接着就是模块中的API引用定义啦:
Public Declare Function CreateToolhelpSnapshot Lib 'kernel32' Alias 'CreateToolhelp32Snapshot' (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Public Declare Function ProcessFirst Lib 'kernel32' Alias 'Process32First' (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Public Declare Function ProcessNext Lib 'kernel32' Alias 'Process32Next' (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Public Const MAX_PATH As Integer = 260
Public Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szExeFile As String * MAX_PATH
End Type
Public Id As Long
Public Declare Function TerminateProcess Lib 'kernel32' (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Public Declare Function OpenProcess Lib 'kernel32' (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Public Declare Function CloseHandle Lib 'kernel32' (ByVal hObject As Long) As Long
Public Const TH32CS_SNAPPROCESS = &H2
Public Const TH32CS_SNAPheaplist = &H1
Public Const TH32CS_SNAPthread = &H4
Public Const TH32CS_SNAPmodule = &H8
Public Const TH32CS_SNAPall = TH32CS_SNAPPROCESS + TH32CS_SNAPheaplist + TH32CS_SNAPthread + TH32CS_SNAPmodule
最后是GetJingCheng和EndJingCheng函数:(hpygzhx520提供)
Public Function GetJingCheng(Exename As String) As String ' 取得进程
GetJingCheng = ''
Dim i As Long
Dim theloop As Long
Dim proc As PROCESSENTRY32
Dim snap As Long
Dim Lent As Integer
Lent = Len(Exename)
GetJingCheng = '' '清空所有内容
snap = CreateToolhelpSnapshot(TH32CS_SNAPall, 0) '获得进程“快照”的句柄
proc.dwSize = Len(proc)
theloop = ProcessFirst(snap, proc) '获取第一个进程,并得到其返回值
While theloop <> 0 '当返回值非零时继续获取下一个进程
If Left(proc.szExeFile, Lent) = Exename Then '这个条件是我添加的,为什么这个条件始终不满足?
GetJingCheng = proc.th32ProcessID '而进程列表中有explorer.exe,请问为什么?
End If
theloop = ProcessNext(snap, proc)
Wend
CloseHandle snap '关闭进程“快照”句柄
End Function
Public Function EndJingCheng(MyId As Long) As Long ' 结束进程
Dim i As Long
Dim Mystr As String
Dim hand As Long
hand = OpenProcess(1, True, MyId) '获取进程句柄
EndJingCheng = TerminateProcess(hand, 1) '关闭进程
End Function
'***********************************************************************************************
可能这样感觉代码太长,也可以这样实现:( MagicianLiu提供)
Public Sub subKillProcess(ByVal strProcess As String)
Dim strComputer As String
Dim objWMIService As Object
Dim colProcessList
Dim objProcess As Object
On Error Resume Next
strComputer = '.'
Set objWMIService = GetObject('winmgmts:' _
& '{impersonationLevel=impersonate}!\\' & strComputer & '\root\cimv2')
' strProcess = 'Excel.exe'
Set colProcessList = objWMIService.ExecQuery _
('Select * from Win32_Process Where Name = '' & strProcess & ''')
For Each objProcess In colProcessList
objProcess.Terminate
Next
End Sub
'********************************************************************************************
未解决的问题:由于绕不开同步执行函数中的DoEvens,timer总是不会溢出
积极思考中。。用脑用脑,发功。。。~~~~~
附同步执行程序:
Sub ShellWait(sCommandLine As String) '等到外部程序执行完成
Dim hShell As Long, hProc As Long, lExit As Long
hShell = Shell(sCommandLine, vbHide)
hProc = OpenProcess(&H400, False, hShell)
Do
GetExitCodeProcess hProc, lExit
DoEvents
Loop While lExit = &H103
End Sub
继续发功。。。。。。。。。。。
卡卡