سلام
آموزش كار با Process در VB توسط BlackCode
ابتدا بايد يك اشاره گر از تمام پروسس هاي در حالاجرا بگيريم
کد:
Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
حال با توابع زير اولين و به ترتيب پروسس هاي بعدي رو دريافت مي كنيم
کد:
Private Declare Function Process32First Lib "kernel32.dll" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "kernel32.dll" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
نوع PROCESSENTRY32 :
کد:
'MAXPATH=255
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 * MAX_PATH
End Type
توضيح انواع(مهمترين ها):
DWSIZE: كل طول تابع كه بايد ارسال شود.
TH32PROCESSID: آيدي پروسس جاري
SZEXEFILE: نام پروسس
سپس با اين توابع كه در زير نوشته شده مسير پروسس (به جز پروسس هاي سيستمي) رو دريافت مي كنيم:
کد:
Private Declare Function Module32First Lib "kernel32" (ByVal hSnapshot As Long, uProcess As MODULEENTRY32) As Long
Private Declare Function Module32Next Lib "kernel32" (ByVal hSnapshot As Long, uProcess As MODULEENTRY32) As Long
براي درك بيشتر سورس زير را با دقت بخوانيد(بايد متغيير هاي r از نوع long و pr از نوع MODULENTERY32 را تعريف كنيد) :
کد:
Dim a As String
Dim hm As MODULEENTRY32
Dim hwnd As Long
Dim p As String
Dim pt As String
List1.Clear
hsnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
pr.dwSize = Len(pr)
r = Process32First(hsnap, pr)
r = Process32Next(hsnap, pr)
Do While r <> 0
hwnd = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, pr.th32ProcessID)
hm.dwSize = Len(hm)
Module32First hwnd, hm
p = Left(hm.szExePath, InStr(1, hm.szExePath, Chr(0)) - 1)
pt = Left(pr.szExeFile, InStr(pr.szExeFile, Chr(0)) - 1) & " : " & "ID: " & pr.th32ProcessID _
& IIf(Right(p, 1) = "e" Or Right(p, 1) = "E", " --> Path: " & p, " <Unknown>")
CloseHandle hwnd
List1.AddItem pt
r = Process32Next(hsnap, pr)
Loop
CloseHandle hsnap
'By BlackCode
'Good Luck