Khi 1 file phần mềm chạy (do yêu cầu người dùng hay do chương trình khác kích hoạt), nó được nạp vào bộ nhớ và trở thành process. Bạn có thể dùng hàm API của Windows có tên là WTSEnumerateProcesses() để thống kê tất cả các process đang chạy trên máy, mỗi process là của file khả thi nào, từ đó quyết định xử lý chúng theo yêu cầu của mình.
Sau đây chúng tôi xin giới thiệu qui trình điển hình để xây dựng ứng dụng VB 6.0 demo việc thống kê tất cả các process đang chạy và hiển thị thông tin về chúng trong một ListVew để người dùng xem:
1. Chạy VB 6.0, tạo Project ứng dụng dạng 'Standard EXE' đơn giản.
2. Chọn menu Project.Components để hiển thị cửa sổ Components, duyệt tìm vào chọn mục Microsoft Window Common Controls 6.0 rồi Ok để thêm các control trong thư viện này vào ToolBox của Project.
3. Thiết kế Form gồm 1 ListView có tên mặc định là ListView1 như hình 4.
4. Chọn menu View.Code để hiển thị cửa sổ soạn mã nguồn cho form rồi viết code cho nó như sau:
Option Explicit'khai báo hằng và kiểu cần dùngPrivate Const WTS_CURRENT_SERVER_HANDLE = 0&Private Type WTS_PROCESS_INFOSessionID As LongProcessID As LongpProcessName As LongpUserSid As LongEnd Type'khai báo các hàm API cần dùngPrivate Declare Function WTSEnumerateProcesses Lib 'wtsapi32.dll' Alias 'WTSEnumerateProcessesA' (ByVal hServer As Long, ByVal Reserved As Long, ByVal Version As Long, ByRef ppProcessInfo As Long, ByRef pCount As Long) As LongPrivate Declare Sub WTSFreeMemory Lib 'wtsapi32.dll' (ByVal pMemory As Long)Private Declare Sub CopyMemory Lib 'kernel32' Alias 'RtlMoveMemory' (Destination As Any, Source As Any, ByVal Length As Long)'hàm tìm chuỗi từ địa chỉ bộ nhớ xác địnhPrivate Function GetStringFromLP(ByVal StrPtr As Long) As StringDim b As ByteDim tempStr As StringDim bufferStr As StringDim Done As BooleanDone = FalseDo 'lấy từng byte và xử lýCopyMemory b, ByVal StrPtr, 1If b = 0 Then 'kết thúc chuỗiDone = TrueElsetempStr = Chr$(b)bufferStr = bufferStr & tempStrStrPtr = StrPtr + 1 'tăng pointer tới byte kếEnd IfLoop Until DoneGetStringFromLP = bufferStrEnd Function'thủ tục khởi động FormPrivate Sub Form_Load()ListView1.View = lvwReport'tạo header gồm 4 cột thông tin trên ListViewListView1.ColumnHeaders.Add 1, 'SessionID', 'Session ID'ListView1.ColumnHeaders.Add 2, 'ProcessID', 'Process ID'ListView1.ColumnHeaders.Add 3, 'ProcessName', 'Process Name'ListView1.ColumnHeaders.Add 4, 'UserID', 'User ID''thống kê và hiển thị các processGetWTSProcessesEnd Sub'thủ tục thống kê và hiển thị thông tin các processPrivate Sub GetWTSProcesses()'khai báo các biến cần dùngDim RetVal As LongDim Count As LongDim i As IntegerDim lpBuffer As LongDim p As LongDim udtProcInfo As WTS_PROCESS_INFODim itmAdd As ListItem'xóa nội dung cũ của ListViewListView1.ListItems.Clear'thống kê các processRetVal = WTSEnumerateProcesses(WTS_CURRENT_SERVER_HANDLE, 0&, 1, lpBuffer, Count)If RetVal = 0 Then 'nếu thất bạiMsgBox 'Error occurred calling WTSEnumerateProcesses. ' & 'Check the Platform SDK error codes in the MSDN Documentation' & ' for more information.', vbCritical, 'Error ' & Err.LastDllError'dừng thủ tụcExit SubEnd If'nếu thống kê được thì lặp hiển thị từng processp = lpBufferFor i = 1 To Count'copy thông tin về process i vào biến udtProcInfoCopyMemory udtProcInfo, ByVal p, LenB(udtProcInfo)'thêm hàng thông tin về process i vào ListViewSet itmAdd = ListView1.ListItems.Add(i, , CStr(udtProcInfo.SessionID))itmAdd.SubItems(1) = CStr(udtProcInfo.ProcessID)itmAdd.SubItems(2) = GetStringFromLP(udtProcInfo.pProcessName)itmAdd.SubItems(3) = CStr(udtProcInfo.pUserSid)'hiệu chỉnh p về vị trí miêu tả process kế tiếpp = p + LenB(udtProcInfo)Next iSet itmAdd = Nothing'giải phóng bộ nhớWTSFreeMemory lpBufferEnd SubLưu ý tên file khả thi của process được chứa trong trường udtProcInfo.pProcessName.
5. Chọn menu Run.Start để chạy thử Form vừa xây dựng và xem danh sách các process đang chạy trên máy.
Theo pcworld
Nguồn bài viết: thanglong-aptech.com






1 nhận xét:
bichi!
Đăng nhận xét