[VB6 ] 請教強制取消Shell所執行的程式
請教大家一個問題,我之前爬文看了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
推
01/18 12:42, , 1F
01/18 12:42, 1F
推
01/18 12:42, , 2F
01/18 12:42, 2F
推
01/18 12:51, , 3F
01/18 12:51, 3F
推
01/18 12:54, , 4F
01/18 12:54, 4F
→
01/18 12:55, , 5F
01/18 12:55, 5F
→
01/18 12:56, , 6F
01/18 12:56, 6F
→
01/18 20:05, , 7F
01/18 20:05, 7F
討論串 (同標題文章)
完整討論串 (本文為第 1 之 3 篇):
Visual_Basic 近期熱門文章
PTT數位生活區 即時熱門文章