紧急求救,各位兄弟姐妹帮忙!!!!

wsmfighter2002 2004-03-23 01:00:57
我怎么样获取一个进程,并且把他关闭??
...全文
31 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
sakurako 2004-03-24
  • 打赏
  • 举报
回复
模块中的代码:

Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Public Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Public Const WM_CLOSE = &H10
Public Const GW_HWNDNEXT = 2
Public Const GW_HWNDFIRST = 0
Public WinText As String * 256
Public Handler(200) As Long

Public Sub findrunningprogram()
Dim RenHWND As Long
Dim Count As Integer
Dim RetValue As Long
Dim WinTextLength As Long
Count = 0
Form1.List1.Clear
RenHWND = GetWindow(Form1.hwnd, GW_HWNDFIRST)
Do
RetValue = GetWindowText(RenHWND, WinText, 256)
If RetValue <> 0 Then
WinTextLength = GetWindowTextLength(RenHWND)
If Left$(WinText, WinTextLength) <> Form1.Caption And Left$(WinText, WinTextLength) <> App.Title Then
Form1.List1.AddItem "[" & RenHWND & "] " & WinText
Handler(Count) = RenHWND
Count = Count + 1
End If
End If
RenHWND = GetWindow(RenHWND, GW_HWNDNEXT)
Loop Until RenHWND = 0
Form1.List1.ListIndex = 0
Form1.Label2.Caption = "共(" & Count & "项)"
End Sub

Form1中的代码:

Private Sub Command1_Click()
Dim RenValue As Long
RetValue = SendMessage(Handler(Form1.List1.ListIndex), WM_CLOSE, 0, 0)
If RetValue <> 0 Then
MsgBox "清除不成功!"
Call findrunningprogram
Else
MsgBox "清除成功!"
End If
End Sub

Private Sub Command2_Click()
Call findrunningprogram
End Sub

Private Sub Command3_Click()
Unload Form1
End
End Sub

Private Sub Form_Load()
Call findrunningprogram
End Sub


在Form1上放3个按钮,1个label2,1个list1
运行,ok
wsmfighter2002 2004-03-23
  • 打赏
  • 举报
回复
楼上的大哥,可以把函数声明和PROCESSENTRY32 贴出来吗?小弟比较菜!!
hcj2002 2004-03-23
  • 打赏
  • 举报
回复
Private Const TH32CS_SNAPHEAPLIST = &H1
Private Const TH32CS_SNAPPROCESS = &H2
Private Const TH32CS_SNAPTHREAD = &H4
Private Const TH32CS_SNAPMODULE = &H8
Private Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST Or TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD Or TH32CS_SNAPMODULE)
Private Const TH32CS_INHERIT = &H80000000
Private Const PROCESS_TERMINATE = &H1&

Private Sub Command1_Click()
Dim Process As PROCESSENTRY32
Dim ProcSnap As Long
Dim cntProcess As Long
cntProcess = 0
List1.Clear
ProcSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
If ProcSnap Then
Process.dwsize = 1060 ' 通常用法
Process32First ProcSnap, Process
Do Until Process32Next(ProcSnap, Process) < 1 ' 遍历所有进程直到返回值为False
List1.AddItem Trim(Process.szExeFile)
cntProcess = cntProcess + 1
Loop
End If
ReDim ProcessID(cntProcess) As Long
Dim i As Long
i = 0
Process32First ProcSnap, Process
Do Until Process32Next(ProcSnap, Process) < 1 ' 遍历所有进程直到返回值为False
ProcessID(i) = Process.th32ProcessID
i = i + 1
Loop
CloseHandle (ProcSnap)
End Sub

Private Sub Command2_Click()
Dim c As Integer
If List1.ListIndex < 0 Then
MsgBox "请选择进程!", vbOKOnly + vbInformation, "提示"
Else
Dim hProcess As Long
hProcess = OpenProcess(PROCESS_TERMINATE, False, ProcessID(List1.ListIndex))
If hProcess Then TerminateProcess hProcess, 0
c = List1.ListCount
While List1.ListCount = c
Command1_Click
Wend
End If
End Sub

Private Sub Command3_Click()
Unload Me
End Sub
hisofty 2004-03-23
  • 打赏
  • 举报
回复
Private Declare Function CreateToolhelp32Snapshot Lib "KERNEL32.DLL" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
Private Declare Function EnumProcesses Lib "psapi.dll" (ByRef lpidProcess As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long

也可以用这两个函数枚举进程,再关闭
zhangyuqin 2004-03-23
  • 打赏
  • 举报
回复
使用Windows API FindWindow PostMessage

1,486

社区成员

发帖
与我相关
我的任务
社区描述
VB API
社区管理员
  • API
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧