سلام.
از این کد استفاده کنید. نسبت به حروف کوچک و بزرگ هم حساس نیست. اسم پروسه رو تو تکس باکس بده و هر چند تا پروسه به اون اسم پیدا کنه میبنده. یه شرطم گذاشتم که اگه پروسه مورد نظر با برنامه خودت هم اسم بود برنامه خودت بسته نشه. البته سرویس ها رو نمیبنده ها اونا روش بستنشون فرق داره باید استاپ شن.
کد:
Option Explicit
'EndProcess
Private Declare Function GetCurrentProcessId Lib "kernel32" () As Long
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 hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject 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 * 260
End Type
Private Const TH32CS_SNAPALL = &HF
Private Const PROCESS_ALL_ACCESS = &H1F0FFF
'EndProcess
Private Sub EndProcess(strProcessName As String)
Dim strEXEName As String
Dim hSnapshot As Long, lngNextProcess As Long, lngProcessHandle As Long
Dim ProcessInfo As PROCESSENTRY32
hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0)
ProcessInfo.dwSize = Len(ProcessInfo)
lngNextProcess = Process32First(hSnapshot, ProcessInfo)
While lngNextProcess <> 0
strEXEName = Left(ProcessInfo.szExeFile, InStr(ProcessInfo.szExeFile, vbNullChar) - 1)
If UCase(strEXEName) = UCase(strProcessName) Then
If ProcessInfo.th32ProcessID = GetCurrentProcessId Then ProcessInfo.th32ProcessID = 0
lngProcessHandle = OpenProcess(PROCESS_ALL_ACCESS, 0&, ProcessInfo.th32ProcessID)
TerminateProcess lngProcessHandle, 0&
DoEvents
End If
lngNextProcess = Process32Next(hSnapshot, ProcessInfo)
Wend
CloseHandle (hSnapshot)
End Sub
Private Sub Command1_Click()
EndProcess Text1.Text
End Sub