现在的位置: 首页 > 综合 > 正文

prjProcess – 进程管理 – Written By HackerJLY In Universty – VB6 + API

2013年12月12日 ⁄ 综合 ⁄ 共 11395字 ⁄ 字号 评论关闭

 prjProcess - 进程管理 - Written By HackerJLY In Universty - VB6 + API

modAPI.bas

Option Explicit        '在模块级别中使用,强制显式声明模块中的所有变量。

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''' 常数声明''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Public Const TH32CS_SNAPPROCESS = &H2
Public Const TH32CS_SNAPheaplist = &H1
Public Const TH32CS_SNAPthread = &H4
Public Const TH32CS_SNAPmoudle = &H8
Public Const TH32CS_SNAPall = TH32CS_SNAPPROCESS + _
                              TH32CS_SNAPheaplist + _
                              TH32CS_SNAPthread + _
                              TH32CS_SNAPmoudle
Public Const MAX_PATH As Integer = 260

Public Const PROCESS_ALL_ACCESS = &H1F0FFF
'Public Const PROCESS_CREATE_PROCESS = &H
Public Const PROCESS_CREATE_THREAD = &H2
Public Const PROCESS_DUP_HANDLE = &H40
Public Const PROCESS_QUERY_INFORMATION = &H400
Public Const PROCESS_SET_INFORMATION = &H200
Public Const PROCESS_TERMINATE = &H1
Public Const PROCESS_VM_OPERATION = &H8
Public Const PROCESS_VM_READ = &H10
Public Const PROCESS_VM_WRITE = &H20
Public Const SYNCHRONIZE = &H100000

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''用户自定义类型'''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Public Type PROCESSENTRY32
    dwSize As Long                     ' 指定 “PROCESSENTRY32类型变量 结构”长度。注:在调用PROCESSFIRST函数之前,必须初始化该值
    cntUsage As Long                   '
    th32ProcessID As Long              ' 进程标识符
    th32DefaultHeapID As Long          '
    th32ModuleID As Long               ' 进程的模块标识符
    cntThreads As Long                 ' 被该进程启动的可执行线程的数量
    th32ParentProcessID As Long        ' “创造该进程”的进程的标识符——某个 th32ProcessID
    pcPriClassBase As Long             ' 被进程创建的一些线程的基本优先级
    dwFlags As Long                    ' 保留,不用
    szExeFile As String * MAX_PATH     ' 所获得进程对应的可执行文件的路径和文件名
End Type

' PROCESSENRY32.dwSize = 296 = dwSize As Long                  :   4 字节(字符)   +
'                              cntUsage As Long                :   4 字节(字符)   +
'                              th32ProcessID As Long           :   4 字节(字符)   +
'                              th32DefaultHeapID As Long       :   4 字节(字符)   +
'                              th32ModuleID As Long            :   4 字节(字符)   +
'                              cntThreads As Long              :   4 字节(字符)   +
'                              th32ParentProcessID As Long     :   4 字节(字符)   +
'                              pcPriClassBase As Long          :   4 字节(字符)   +
'                              dwFlags As Long                 :   4 字节(字符)   +
'                              szExeFile As String * MAX_PATH  : 260 字节(字符)
'                            = 296 字节(字符)
' Long 型  : 4 字节

 

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''' API 函数'''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''' 获取进程 API'''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

' 创建进程快照
Public Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" _
      (ByVal lFlags As Long, _
       ByVal lProcessID As Long _
      ) As Long
'       lFlags     : 指定进程快照内包含的内容
'                            Value                 Meaning
'                        (1) TH32CS_INHERIT     :  Indicates that the snapshot handle is to be inheritable.
'                        (2) TH32CS_SNAPALL     :  Equivalent to specifying TH32CS_SNAPHEAPLIST,
'                                                                           TH32CS_SNAPMODULE,
'                                                                           TH32CS_SNAPPROCESS,
'                                                                           TH32CS_SNAPTHREAD.
'                        (3) TH32CS_SNAPHEAPLIST:  Includes the heap list of the specified process in the snapshot.
'                        (4) TH32CS_SNAPMODULE  :  Includes the module list of the specified process in the snapshot.
'                        (5) TH32CS_SNAPPROCESS :  Includes the process list in the snapshot.
'                        (6) TH32CS_SNAPTHREAD  :  Includes the thread list in the snapshot.
          
'       lProcessID : 指定"进程ID"——th32ProcessID
'                    值 : 0 到
'       返回值     : 成功 :返回一个指定的快照的打开的句柄
'                    失败 :-1

' 得到第一个进程API函数
Public Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" _
      (ByVal hSnapShot As Long, _
       uProcess As PROCESSENTRY32 _
      ) As Long
'      hSnapShot  : “CreateToolhelpSnapshot”函数所返回的快照句柄
'      uProcess   :  PROCESSENTRY32类型容器变量
'      返回值     :  有进程 :逻辑真
'                     无进程 :逻辑假

' 得到下一个进程API函数
Public Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" _
      (ByVal hSnapShot As Long, _
       uProcess As PROCESSENTRY32 _
      ) As Long
'      hSnapShot  : “CreateToolhelpSnapshot”函数所返回的快照句柄
'      uProcess   :  PROCESSENTRY32类型容器变量
'      返回值     :  有进程 :逻辑真
'                     无进程 :逻辑假

' 关闭一个“打开的对象句柄”
Public Declare Function CloseHandle Lib "kernel32" _
      (ByVal hObject As Long _
      ) As Long
'      hObject    :  打开的对象的句柄
'      返回值     :  成功 : 非零
'                    失败 : 零
     

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''' 终止进程 API''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

'终止进程
Public Declare Function TerminateProcess Lib "kernel32" _
      (ByVal hProcess As Long, _
       ByVal uExitCode As Long _
      ) As Long
'      hProcess  : 要结束的进程的句柄,由“OpenProcess”函数获得
'      uExitCode : 指定要终止的进程和所有线程的退出代码
'                   用“GetExitCodeProcess”函数获得进程的退出代码
'                   用“GetExitCodeThread” 函数获得线程的退出代码
'                   注:可忽略
'
'
'
'

'此函数返回一个已存在的进程对象的句柄
Public Declare Function OpenProcess Lib "kernel32" _
      (ByVal dwDesiredAccess As Long, _
       ByVal bInheritHandle As Long, _
       ByVal dwProcessId As Long _
      ) As Long
'      dwDesiredAccess  : 指定访问进程的用途
'
'                          Access                      描述
'                          PROCESS_ALL_ACCESS          Specifies all possible access flags for the process object.
'                          PROCESS_CREATE_PROCESS      Used internally.
'                          PROCESS_CREATE_THREAD       Enables using the process handle in the “CreateRemoteThread” function to create a thread in the process.
'                          PROCESS_DUP_HANDLE          Enables using the process handle as either the source or target process in the “DuplicateHandle” function to duplicate a handle.
'                          PROCESS_QUERY_INFORMATION   Enables using the process handle in the “GetExitCodeProcess” and “GetPriorityClass” functions to read information from the process object.
'                          PROCESS_SET_INFORMATION     Enables using the process handle in the “SetPriorityClass” function to set the priority class of the process.
'                          PROCESS_TERMINATE           Enables using the process handle in the “TerminateProcess” function to terminate the process.
'                          PROCESS_VM_OPERATION        Enables using the process handle in the “VirtualProtectEx” and “WriteProcessMemory” functions to modify the virtual memory of the process.
'                          PROCESS_VM_READ             Enables using the process handle in the “ReadProcessMemory” function to read from the virtual memory of the process.
'                          PROCESS_VM_WRITE            Enables using the process handle in the “WriteProcessMemory” function to write to the virtual memory of the process.
'                          SYNCHRONIZE                 Windows NT:Enables using the process handle in any of the wait functions to wait for the process to terminate.

'
'
'
'      bInheritHandle   : 指定“该函数返回的句柄”是否能被“当前进程所创建的进程”所继承
'                          Ture  : 能被继承
'                          False : 不能被继承
'
'
'
'
'      dwProcessId      : 将要打开的进程的标识符
'
'
'
'      返回值           : 成功 : 返回一个指定的进程对象的句柄
'                          失败 : 返回空值(Null),要想得知原因请调用“GetLastError”函数
'

 

'###########################################################################################################
'打开主页

Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
        ByVal hwnd As Long, _
        ByVal lpOperation As String, _
        ByVal lpFile As String, _
        ByVal lpParameters As String, _
        ByVal lpDirectory As String, _
        ByVal nShowCmd As Long) As Long

Public Const SW_SHOWNORMAL = 1

frmMain.frm

Option Explicit

Private Sub cmdAuthorHomepage_Click()
    ShellExecute 0, "open", "http://blog.csdn.net/HackerJLY", vbNullString, vbNullString, SW_SHOWNORMAL
End Sub

' 获得进程

Private Sub CmdGetProcess_Click()
    Dim Hturn As Long, BTurn As Boolean, Proc As PROCESSENTRY32
    Hturn = CreateToolhelpSnapshot(TH32CS_SNAPall, 0)
    Proc.dwSize = Len(Proc)              ' 在调用ProcessFirst函数之前,必须初始化该值
    BTurn = ProcessFirst(Hturn, Proc)
    Dim i As Integer, Item As String, A As String
    i = 1
    ListView1.ListItems.Clear
    While BTurn <> False
          ListView1.ListItems.Add , , Str(i)
          With ListView1.ListItems.Item(i).ListSubItems
               .Add , , Proc.szExeFile
               .Add , , Str(Proc.cntThreads)
               .Add , , Str(Proc.cntUsage)
               .Add , , Str(Proc.dwFlags)
               .Add , , Str(Proc.pcPriClassBase)
               .Add , , Str(Proc.th32DefaultHeapID)
               .Add , , Str(Proc.th32ModuleID)
               .Add , , Str(Proc.th32ParentProcessID)
               .Add , , Str(Proc.th32ProcessID)
               .Add , , Str(Proc.dwSize)
          End With

          BTurn = ProcessNext(Hturn, Proc)
          i = i + 1
    Wend
    CloseHandle Hturn
End Sub

' 终止进程

Private Sub CmdTerminateProcess_Click()
    Dim Hturn As Long
    Hturn = OpenProcess(PROCESS_TERMINATE, True, CLng(ListView1.SelectedItem.ListSubItems(9)))
    ' ListView1.SelectedItem.ListSubItems(9)       从 0 开始,即:“ListView1.SelectedItem.ListSubItems”的第一项的索引为 0
    TerminateProcess Hturn, 0
    TimerTerminateP.Enabled = True
End Sub

 

' 窗体大小改变事件

Private Sub Form_Resize()
    On Error Resume Next
    ListView1.Width = frmMain.ScaleWidth
    ListView1.Height = frmMain.ScaleHeight - 1000
    CmdGetProcess.Top = frmMain.ScaleHeight - 800
    cmdAuthorHomepage.Top = CmdGetProcess.Top
    CmdTerminateProcess.Top = CmdGetProcess.Top
    CmdGetProcess.Left = frmMain.ScaleWidth / 10
    CmdTerminateProcess.Left = 4 * frmMain.ScaleWidth / 10
    cmdAuthorHomepage.Left = 7 * frmMain.ScaleWidth / 10
   
End Sub

' ListView 列首单击事件

'Private Sub ListView1_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)
'    Select Case ColumnHeader.SubItemIndex
'        Case 1               ' 进程名称以字母排序
'             With ListView1
'                 .SortKey = ColumnHeader.SubItemIndex
'                 .SortOrder = Val(Not CBool(Val(ListView1.SortOrder)))
'                 .Sorted = True
'             End With
'        Case 0, 2 To 10      ' 其他以数值大小排序——冒泡排序法
'
'
'    End Select
'End Sub

' 终止进程后刷新进程列表

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''说明'''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

'          Left(string,length)     =   Left$(string,length)   :  截取的是:Length 个“字符数”
'          LeftB( string,length)   =   LeftB$(string,length)  :  截取的是:Length 个“字节数”

'Private Sub TimerGetP_Timer()
'    CmdGetProcess_Click
'End Sub

Private Sub TimerTerminateP_Timer()
    CmdGetProcess_Click
    TimerTerminateP.Enabled = False
End Sub

抱歉!评论已关闭.