作者macgyfu (YFU)
看板Visual_Basic
标题[VB6 ] 请教强制取消Shell所执行的程式
时间Wed Jan 17 23:27:15 2007
请教大家一个问题,我之前爬文看了Shell笔记教人家如何使用shell
执行一个外部程式,还有强制关闭,我现在写了一个,但发现并不能
关掉在执行中的程式,
我在C下面产生一个ping.bat然後里面内容为"ping 168.95.1.1 -t"
然後程式码写成
Private Declare Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" _
(ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" _
(ByVal hObject As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" _
(ByVal hProcess As Long, lpExitCode As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" _
(ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function IsWindow Lib "user32" _
(ByVal hwnd As Long) As Long
Const PROCESS_QUERY_INFORMATION = &H400
Const SYNCHRONIZE = &H100000
Const STILL_ALIVE = &H103
Const INFINITE = &HFFFF
Private ExitCode As Long
Private hProcess As Long
Private isDone As Long
Private Sub CommandButton1_Click()
Dim pid As Long
pid = Shell("cmd /c c:\ping.bat ", vbNormalFocus)
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION + SYNCHRONIZE, 0, pid)
ExitEvent = WaitForSingleObject(hProcess, 1500)
x = TerminateProcess(hProcess, 3838)
Call CloseHandle(hProcess)
MsgBox ("Finish")
End Sub
最後执行结束有跳出Finish的讯息,但是DOS视窗并没有关闭,请问程式是哪边有问题呢?
> <
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 61.228.10.225
1F:推 fumizuki:因为执行个体是 cmd.com 不是 ping.bat 01/18 12:42
2F:推 fumizuki:*.bat 不是可执行档 01/18 12:42
3F:推 fumizuki:我好像乱回答了orz 01/18 12:51
5F:→ fumizuki:↑这支范例可以确实把程式关掉 01/18 12:55
6F:→ fumizuki:啊 还是没有结束 cmd.exe 01/18 12:56
7F:→ macgyfu:> < 所以还是没有办法了罗 01/18 20:05