代码如下:为什么不能做到强行终止程序啊!??Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Dim hwd As Long
Dim ph As LongPrivate Sub Command1_Click()
Dim a_text As String
a_text = Text1.Text
hwd = FindWindow(vbNullString, a_text) If hwd <> 0 Then
MsgBox "存在此窗体,检测句柄成功"
Else
MsgBox "不存在此窗体,错误"
End IfDebug.Print hwd
End SubPrivate Sub Command2_Click()
Dim a_text As String
a_text = Text1.Text
hwd = FindWindow(vbNullString, a_text)
ph = OpenProcess(1, 0, hwd)
Debug.Print hello
TerminateProcess ph, 0
Debug.Print "ph="; ph
Debug.Print "hwd="; hwd
MsgBox "程序成功终止"
End Sub
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Dim hwd As Long
Dim ph As LongPrivate Sub Command1_Click()
Dim a_text As String
a_text = Text1.Text
hwd = FindWindow(vbNullString, a_text) If hwd <> 0 Then
MsgBox "存在此窗体,检测句柄成功"
Else
MsgBox "不存在此窗体,错误"
End IfDebug.Print hwd
End SubPrivate Sub Command2_Click()
Dim a_text As String
a_text = Text1.Text
hwd = FindWindow(vbNullString, a_text)
ph = OpenProcess(1, 0, hwd)
Debug.Print hello
TerminateProcess ph, 0
Debug.Print "ph="; ph
Debug.Print "hwd="; hwd
MsgBox "程序成功终止"
End Sub
ph = OpenProcess(1, 0, hwd)这里有问题,OpenProcess的第3个参数需要传入的是PID(一般用shell获得),而不是窗口句柄,如:
Option Explicit
Const PROCESS_QUERY_INFORMATION = &H400
Const PROCESS_TERMINATE = &H1
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As LongSub main()
Dim ProcessId As Long
Dim hProcess As Long
ProcessId = Shell("notepad.exe", 1) '此处利用了 Shell当函数使用时返回的任务标识
hProcess = OpenProcess(PROCESS_TERMINATE Or PROCESS_QUERY_INFORMATION, False, ProcessId)
Call TerminateProcess(hProcess, 3838)
End Sub
GetWindowThreadProcessId【操作系统】
Win9X:Yes
WinNT:Yes【声明】
GetWindowThreadProcessId Lib "user32" Alias "GetWindowThreadProcessId" (ByVal hwnd As Long, lpdwProcessId As Long) As Long【说明】 获取与指定窗口关联在一起的一个进程和线程标识符 【返回值】 Long,拥有窗口的线程的标识符 【其它】
【参数表】
hwnd ----------- Long,指定窗口句柄 lpdwProcessId -- Long,指定一个变量,用于装载拥有那个窗口的一个进程的标识符
使用GetWindowThreadProcessId根据窗口句柄得到进程ID,然后再使用rainstormmaster(暴风雨 v2.0) 的方法。
Private Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal blnheritHandle As Long, ByVal dwAppProcessId As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal ApphProcess As Long, ByVal uExitCode As Long) As LongPrivate 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 * 1024
End Type
Const TH32CS_SNAPHEAPLIST = &H1
Const TH32CS_SNAPPROCESS = &H2
Const TH32CS_SNAPTHREAD = &H4
Const TH32CS_SNAPMODULE = &H8
Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST Or TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD Or TH32CS_SNAPMODULE)
Const TH32CS_INHERIT = &H80000000
Dim pid As Long
Dim pname As StringPrivate Sub Command1_Click()
Dim my As PROCESSENTRY32
Dim l As Long
Dim l1 As Long
Dim flag As Boolean
Dim mName As String
Dim i As Integer
l = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
If l Then
my.dwSize = 1060
If (Process32First(l, my)) Then
Do
i = InStr(1, my.szExeFile, Chr(0))
mName = LCase(Left(my.szExeFile, i - 1))
If mName = "notepad.exe" Then
pid = my.th32ProcessID
pname = mName
If MsgBox("Kill " & mName & " Down?", vbYesNo + vbQuestion) <> vbYes Then
Exit Sub
End If
Dim mProcID As Long
mProcID = OpenProcess(1&, -1&, pid)
TerminateProcess mProcID, 0&
flag = True
Exit Sub
Else
flag = False
End If
Loop Until (Process32Next(l, my) < 1)
End If
l1 = CloseHandle(l)
End If
If flag = False Then
MsgBox "No found!"
End IfEnd Sub
Const PROCESS_QUERY_INFORMATION = &H400
Const PROCESS_TERMINATE = &H1
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Dim hwd As Long
Dim ph As LongPrivate Sub Command2_Click()
Dim a_text As String
a_text = Text1.Text
Dim pid As Long, tid As Long
hwd = FindWindow(vbNullString, a_text)
If hwd <> 0 Then
tid = GetWindowThreadProcessId(hwd, pid)
ph = OpenProcess(PROCESS_TERMINATE Or PROCESS_QUERY_INFORMATION, 0, pid)
Debug.Print "hello"
TerminateProcess ph, 0
Debug.Print "ph="; ph
Debug.Print "hwd="; hwd
MsgBox "程序成功终止"
Else
MsgBox "没有找到指定窗口"
End If
End Sub