作者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
推 fumizuki:因為執行個體是 cmd.com 不是 ping.bat 01/18 12:42
推 fumizuki:*.bat 不是可執行檔 01/18 12:42
推 fumizuki:我好像亂回答了orz 01/18 12:51
→ fumizuki:↑這支範例可以確實把程式關掉 01/18 12:55
→ fumizuki:啊 還是沒有結束 cmd.exe 01/18 12:56
→ macgyfu:> < 所以還是沒有辦法了囉 01/18 20:05