- 主题
- 0
- 积分
- 854
- 贝壳
- 854 个
- 性别
- 男
- 来自
- 贵州遵义
- 注册时间
- 2005-3-26
- 最后登录
- 2011-2-26
|
- Option Explicit
- '======================用于查找进程和终止进程的API函数常数定义================ =====
- Private Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
- Private Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
- Private Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) 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 CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
- Const MAX_PATH As Integer = 260
- Private 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 * MAX_PATH
- End Type
- Const TH32CS_SNAPheaplist = &H1
- Const TH32CS_SNAPPROCESS = &H2
- Const TH32CS_SNAPthread = &H4
- Const TH32CS_SNAPmodule = &H8
- Const TH32CS_SNAPall = TH32CS_SNAPPROCESS + TH32CS_SNAPheaplist + TH32CS_SNAPthread + TH32CS_SNAPmodule
- '======================在WIN2000下提升本进程权限的API函数常数定义=========== ==========
- Const STANDARD_RIGHTS_REQUIRED = &HF0000
- Const TOKEN_ASSIGN_PRIMARY = &H1
- Const TOKEN_DUPLICATE = (&H2)
- Const TOKEN_IMPERSONATE = (&H4)
- Const TOKEN_QUERY = (&H8)
- Const TOKEN_QUERY_SOURCE = (&H10)
- Const TOKEN_ADJUST_PRIVILEGES = (&H20)
- Const TOKEN_ADJUST_GROUPS = (&H40)
- Const TOKEN_ADJUST_DEFAULT = (&H80)
- Const TOKEN_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or TOKEN_ASSIGN_PRIMARY Or _
- TOKEN_DUPLICATE Or TOKEN_IMPERSONATE Or TOKEN_QUERY Or TOKEN_QUERY_SOURCE Or _
- TOKEN_ADJUST_PRIVILEGES Or TOKEN_ADJUST_GROUPS Or TOKEN_ADJUST_DEFAULT)
- Const SE_PRIVILEGE_ENABLED = &H2
- Const ANYSIZE_ARRAY = 1
- Private Type LUID
- lowpart As Long
- highpart As Long
- End Type
- Private Type LUID_AND_ATTRIBUTES
- pLuid As LUID
- Attributes As Long
- End Type
- Private Type TOKEN_PRIVILEGES
- PrivilegeCount As Long
- Privileges(ANYSIZE_ARRAY) As LUID_AND_ATTRIBUTES
- End Type
- Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
- Private Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As Long
- Private Declare Function AdjustTokenPrivileges Lib "advapi32.dll" (ByVal TokenHandle As Long, ByVal DisableAllPrivileges As Long, NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, PreviousState As TOKEN_PRIVILEGES, ReturnLength As Long) As Long
- Private Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
- '程序加载
- Private Sub Form_Load()
- AdjustTokenPrivileges2000
- Me.Caption = "WINDOWS 进程管理器"
- Command1.Caption = "刷新"
- Command2.Caption = "终止进程"
- Command3.Caption = "退出"
- ListView1.ColumnHeaders.Clear
- ListView1.ColumnHeaders.Add , "a", "进程ID", 800
- ListView1.ColumnHeaders.Add , "b", "进程名", 4900
- ListView1.View = lvwReport
- Command1_Click '刷新进程列表
- End Sub
- '显示当前系统中全部进程
- Private Sub Command1_Click()
- Dim i As Long, lPid As Long
- Dim Proc As PROCESSENTRY32
- Dim hSnapShot As Long
- ListView1.ListItems.Clear '清空ListView
- hSnapShot = CreateToolhelpSnapshot(TH32CS_SNAPall, 0) '获得进程“快照”的句柄
- Proc.dwSize = Len(Proc)
- lPid = ProcessFirst(hSnapShot, Proc) '获取第一个进程的PROCESSENTRY32结构信息数据
- i = 0
- Do While lPid <> 0 '当返回值非零时继续获取下一个进程
- ListView1.ListItems.Add , "a" & i, Proc.th32ProcessID & "(&H" & Hex(Proc.th32ProcessID) & ")" '将进程ID添加到ListView1第一列
- ListView1.ListItems("a" & i).SubItems(1) = Proc.szExeFile '将进程名添加到ListView1第二列
- i = i + 1
- lPid = ProcessNext(hSnapShot, Proc) '循环获取下一个进程的PROCESSENTRY32结构信息数据
- Loop
- CloseHandle hSnapShot '关闭进程“快照”句柄
- End Sub
- '终止指定进程
- Private Sub Command2_Click()
- Dim lPHand As Long, TMBack As Long
- If ListView1.SelectedItem.Text <> "" Then
- If MsgBox("确实要结束进程[" & ListView1.SelectedItem.SubItems(1) & "]吗?", vbYesNo) = vbYes Then
- lPHand = Val(ListView1.SelectedItem.Text)
- lPHand = OpenProcess(1&, True, lPHand) '获取进程句柄
- TMBack = TerminateProcess(lPHand, 0&) '关闭进程
- If TMBack <> 0 Then
- MsgBox ListView1.SelectedItem.SubItems(1) & "已经被终止!"
- Else
- MsgBox ListView1.SelectedItem.SubItems(1) & "不能被终止!"
- End If
- CloseHandle lPHand
- Command1_Click '刷新进程列表
- End If
- End If
- End Sub
- '退出本程序
- Private Sub Command3_Click()
- Unload Me
- End Sub
- '这个函数用于在WIN2000系统中,本进程提升权限
- Sub AdjustTokenPrivileges2000()
- Dim hdlProcessHandle As Long
- Dim hdlTokenHandle As Long
- Dim tmpLuid As LUID
- Dim tkp As TOKEN_PRIVILEGES
- Dim tkpNewButIgnored As TOKEN_PRIVILEGES
- Dim lBufferNeeded As Long
- Dim lP As Long
- hdlProcessHandle = GetCurrentProcess()
- lP = OpenProcessToken(hdlProcessHandle, TOKEN_ALL_ACCESS, hdlTokenHandle)
- lP = LookupPrivilegeValue("", "SeDebugPrivilege", tmpLuid)
- tkp.PrivilegeCount = 1
- tkp.Privileges(0).pLuid = tmpLuid
- tkp.Privileges(0).Attributes = SE_PRIVILEGE_ENABLED
- lP = AdjustTokenPrivileges(hdlTokenHandle, False, tkp, Len(tkpNewButIgnored), tkpNewButIgnored, lBufferNeeded)
- End Sub
-
-
复制代码 |
|