[VB6 ] 請教強制取消Shell所執行的程式

看板Visual_Basic作者 (YFU)時間18年前 (2007/01/17 23:27), 編輯推噓4(403)
留言7則, 2人參與, 最新討論串1/3 (看更多)
請教大家一個問題,我之前爬文看了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
因為執行個體是 cmd.com 不是 ping.bat
01/18 12:42, 1F

01/18 12:42, , 2F
*.bat 不是可執行檔
01/18 12:42, 2F

01/18 12:51, , 3F
我好像亂回答了orz
01/18 12:51, 3F

01/18 12:55, , 5F
↑這支範例可以確實把程式關掉
01/18 12:55, 5F

01/18 12:56, , 6F
啊 還是沒有結束 cmd.exe
01/18 12:56, 6F

01/18 20:05, , 7F
> < 所以還是沒有辦法了囉
01/18 20:05, 7F
文章代碼(AID): #15ha1KRg (Visual_Basic)
文章代碼(AID): #15ha1KRg (Visual_Basic)