displaying processes, shutting down processes

Source: Internet
Author: User
Tags exit
You first create a snapshot of a process through the CreateToolhelp32Snapshot function, and then iterate through the process by calling Process32First using the handle returned by the snapshot, where the relevant information is stored in an instance of the PROCESSENTRY32 struct type. And then show it.



Winxp+vb6+sp6

The code is as follows:

Private Declare Function createtoolhelp32snapshot Lib "kernel32" (ByVal dwflags as Long, ByVal Th32processid as Long) as L Ong

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 long



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 * 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

' Add ListView control, 3 buttons

' Delete the selected process in ListView

Private Sub Command2_Click ()

If MsgBox ("Do you want to delete" & ListView1.SelectedItem.SubItems (2) & "process?", vbYesNo + vbquestion) <> vbyes Then

Exit Sub

End If



Dim Mprocid as Long

Mprocid = OpenProcess (1&, -1&, Listview1.selecteditem)

TerminateProcess Mprocid, 0&

DoEvents

ListView1.ListItems.Remove (ListView1.SelectedItem.Index)

Listview1.refresh

End Sub

' Get the process filled ListView

Private Sub Command1_Click ()

Dim my as PROCESSENTRY32

Dim L as Long

Dim L1 as Long

Dim Mlistitem as ListItem



' List1.clear

L = createtoolhelp32snapshot (th32cs_snapprocess, 0)

If L Then

My.dwsize = 1060

If (Process32First (L)) Then ' traverse the first process

Todo

Set Mlistitem = ListView1.ListItems.Add (,, My.th32processid)

Mlistitem. SubItems (1) = My.th32parentprocessid

Mlistitem. SubItems (2) = My.szexefile

Loop Until (Process32Next (L, my) < 1) ' traverses all processes knowing the return value is False

End If

L1 = CloseHandle (l)

End If

End Sub



Private Sub Form_Load ()

ListView1.ListItems.Clear

ListView1.ColumnHeaders.Clear

Listview1.fullrowselect = True

ListView1.ColumnHeaders.Add,, "Process ID", 1500

ListView1.ColumnHeaders.Add,, "Process Parent ID", (1500)

ListView1.ColumnHeaders.Add,, "Name", (me.width-3200)

Listview1.view = Lvwreport

End Sub



' Loop detect if there is a specified process in the process, if there is, close, if not, start a program

Private Sub Command3_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)) Then ' traverse the first process

Todo

i = InStr (1, My.szexefile, CHR (0))

Mname = LCase (left (My.szexefile, i-1))

If mname = "winword.exe" Then

PID = My.th32processid

PName = Mname

MsgBox "Find word"

If MsgBox ("Do you want to delete" & Mname & "process?", 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) ' traverses all processes knowing the return value is False

End If

L1 = CloseHandle (l)

End If



If flag = False Then

MsgBox "Word not Found"

Shell "C:\Program Files\Microsoft Office\office\winword. EXE ", Vbnormalfocus

End If

End Sub






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.