Display process, shut down process

zhaozj2021-02-16  142

First create a snapshot of a process through the CreateToolhelp32Snapshot function, and then traverse the process by calling Process32First using the handle of the snapshot, the relevant information is stored in an instance of the Processentry32 structure type, and then displayed.

WinXP VB6 SP6

code show as below:

Private Declare Function CreateToolhelp32 Snapshot Lib "Kernel32" (Byval TH32PROCESSID AS Long) As long

Private Declare Function Process32First Lib "Kernel32" (LPPE AS Processentry32) AS Long

Private Declare Function Process32Next Lib "Kernel32" (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 Dwapppprocessid 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 a ListView control, 3 buttons

'Delete the selected process in ListView

Private sub fascist2_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 populate ListView

Private submmand1_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.

My.dwsize = 1060

IF (Process32First (L, MY)) THEN 'traverses the first process

DO

Set mlistItem = listview1.listitems.add (,, my.th32processid)

mlistitem.subitems (1) = my.th32parentProcessID

mlistitem.subitems (2) = my.szexefile

LOOP Until (PROCESS32NEXT (L, MY) <1) 'Traversing All Processes Know the return value false

END IF

L1 = CloseHandle (L)

END IF

End Sub

Private sub flow_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

Does there are specified processes in the cycle detection process, if there is, close, if not, start a program

Private submmand3_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.

My.dwsize = 1060

IF (Process32First (L, MY)) THEN 'traverses the first process

DO

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 the the 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) 'Traversing All Processes Know the return value false

END IF

L1 = CloseHandle (L)

END IF

IF flag = false kil

Msgbox "No Word"

Shell "C: / Program Files / Microsoft Office / Office / Winword.exe", VBNORMALFOCUS

END IF

End Sub

转载请注明原文地址:https://www.9cbs.com/read-8817.html

New Post(0)