Hướng dẫn viết chương trình hiển thị tất cả tiến trình đang chạy
Một số chương trình của Windows hay trojan chạy ẩn mà chúng ta không hay biết, TaskManager chỉ hiển thị một phần nào đó. Chương trình sau sẽ hiện tất cả các tiến trình đang chạy, và có thể tắt tiến trình đó nếu như ta không muốn chạy (Ví dụ Trojan chẳng hạn).
Thành phần của chương trình
- Hàm API:
Private Declare Function GetWindow Lib "user32" _
(ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetParent Lib "user32" _
(ByVal hwnd As Long) As Long
Private Declare Function GetWindowTextLength Lib _
"user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Private Declare Function GetWindowText Lib "user32" _
Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal _
lpString As String, ByVal cch As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As
String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long,
ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Const GW_HWNDFIRST = 0
Const GW_HWNDNEXT = 2
- Đối tượng
3 Command Button có thuộc tính (Name) lần lượt là cmdKillProcess, cmdRefresh, cmdQuit
1 ListBox có thuộc tính (Name) là lstTientrinh
Mã nguồn
Private Declare Function GetWindow Lib "user32" _
(ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetParent Lib "user32" _
(ByVal hwnd As Long) As Long
Private Declare Function GetWindowTextLength Lib _
"user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Private Declare Function GetWindowText Lib "user32" _
Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal _
lpString As String, ByVal cch As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As
String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long,
ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Const GW_HWNDFIRST = 0
Const GW_HWNDNEXT = 2
Dim TenTienTrinh As String
Private Sub cmdKillProcess_Click()
Dim Ret As Long
Dim hwnd As Long
On Error GoTo Refresh
hwnd = FindWindow(vbNullString, TenTienTrinh)
If hwnd <> 0 Then
Ret = PostMessage(hwnd, &H10, 0, 0)
If Ret = 0 Then
MsgBox "Khong the tat chuong trinh " & TenTienTrinh, vbExclamation, "Warning"
Else
MsgBox "Da tat chuong trinh " & TenTienTrinh, vbInformation, "Warning"
End If
End If
Refresh:
HienThiTienTrinh
End Sub
Private Sub cmdQuit_Click()
Unload Me
End Sub
Private Sub Form_Load()
HienThiTienTrinh
End Sub
Sub HienThiTienTrinh()
Dim HwndHienTai As Long
Dim Length As Long, i As Long
Dim Parent As Long
Dim TienTrinh As String
i = 0
lstTientrinh.ListItems.Clear
lstTientrinh.ColumnHeaders.Add , , "STT"
lstTientrinh.ColumnHeaders.Add , , "Processes"
lstTientrinh.ColumnHeaders.Item(1).Width = 500
lstTientrinh.ColumnHeaders.Item(2).Width = lstTientrinh.Width - 400
HwndHienTai = GetWindow(frmShowProcess.hwnd, GW_HWNDFIRST)
While HwndHienTai <> 0
Length = GetWindowTextLength(HwndHienTai)
TienTrinh = Space(Length + 1)
Length = GetWindowText(HwndHienTai, TienTrinh, Length + 1)
TienTrinh = Left(TienTrinh, Len(TienTrinh) - 1)
If Length <> 0 Then
If Me.Caption <> TienTrinh Then
If TienTrinh <> "taskmon" Then
i = i + 1
lstTientrinh.ListItems.Add(, , i).ListSubItems.Add , , TienTrinh
End If
End If
End If
HwndHienTai = GetWindow(HwndHienTai, GW_HWNDNEXT)
DoEvents
Wend
End Sub
Private Sub lstTientrinh_ItemClick(ByVal Item As MSComctlLib.ListItem)
TenTienTrinh = Item.ListSubItems(1).Text
End Sub