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 is as follows: Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long Private Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long Private Declare Function Process32Next Lib "Kernel32" (Byval HSNAPSHTRY32) AS Long Private Declare Function CloseHandle LIB "Kernel32" (Byval Hobject As Long) AS Long
Private Declare Function OpenProcess LIB "Kernel 32" (Byval Blnherithazi As Long, Byval Dwapppprocessid As long) As long
Private Declare Function TerminateProcess LIB "Kernel32" (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, three buttons' Delete listview selected process Private Sub Command2_click () if msgbox ("You want to delete" & listview1.selecteditem.subitems (2) & "process?", Vbyesno vbquesNo <> Vbyes kil mpue dim mprocess (1 &, -1) , ListView1.SelectedItem) TerminateProcess mProcID, 0 & DoEvents ListView1.ListItems.Remove (ListView1.SelectedItem.Index) ListView1.Refresh End Sub 'obtained filling process 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, MY)) Then' Traversing the first process Do SET MLISTI tem = ListView1.ListItems.Add (,, my.th32ProcessID) mlistitem.SubItems (1) = my.th32ParentProcessID mlistitem.SubItems (2) = my.szExeFile Loop Until (Process32Next (l, my) <1) 'through all processes Know the return value for 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 subduate whether there is a specified process in the loop 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 Then my.dwsize = 1060 if (Process32First (L, MY)) Then 'traversing the first process Do I = INSTR (1, my.szeefile, chr (0)) MNAME = LCASE (Left (my.szexefile, i - 1))) if mname = "winword.exe" the 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) 'Traverse all processes know the return value is false end if l1 = closeHandle (L) end if if flag = false the msgbox "No Word" shell "C: / Program Files / Microsoft Office / Office / WinWord. EXE ", VBNORMALFOCUS END IF E ND SUB