P.S.
Пример я проверял на правах Администратора.
- Код: Выделить всё
'В модуль
Option Explicit
Public Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
Public Const MAX_SIZE As Long = 255
Public Function GetSystemDir() As String
Dim strBuf As String
strBuf = Space$(MAX_SIZE)
If GetSystemDirectory(strBuf, MAX_SIZE) > 0 Then
strBuf = StripTerminator(strBuf)
GetSystemDir = AddDirSep(strBuf)
Else
GetSystemDir = AddDirSep(App.Path)
End If
End Function
Public Function AddDirSep(ByVal strPathName As String) As String
If Asc(Right$(strPathName, 1)) <> 92 Then strPathName = strPathName & Chr$(92)
AddDirSep = strPathName
End Function
Public Function StripTerminator(ByVal strString As String) As String
Dim intZeroPos As Integer
intZeroPos = InStr(strString, vbNullChar)
If intZeroPos > 0 Then
StripTerminator = Left$(strString, intZeroPos - 1)
Else
StripTerminator = strString
End If
End Function
Public Function CreateObjectName(ByVal strFullString As String) As String
If strFullString = vbNullString Then Exit Function
Dim i As Long
Dim strTmp As String
CreateObjectName = strFullString
For i = 1 To Len(CreateObjectName)
strTmp = Right$(CreateObjectName, i)
If Asc(Left$(strTmp, 1)) = 92 Then
CreateObjectName = Right$(strTmp, Len(strTmp) - 1)
Exit For
End If
Next i
End Function
'В форму
Option Explicit
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_SNAPHEAPLIST As Long = &H1
Private Const TH32CS_SNAPPROCESS As Long = &H2
Private Const TH32CS_SNAPTHREAD As Long = &H4
Private Const TH32CS_SNAPMODULE As Long = &H8
Private Const TH32CS_SNAPALL As Long = (TH32CS_SNAPHEAPLIST Or TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD Or TH32CS_SNAPMODULE)
Private Const PROCESS_TERMINATE As Long = (&H1)
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 CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Private Declare Function Process32First Lib "kernel32" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Sub CloseHandle Lib "kernel32" (ByVal hPass As Long)
Private Function GetProcessPID(ByVal strFileName As String) As Long
Dim hSnapShot As Long, uProcess As PROCESSENTRY32
Dim lRet As Long
hSnapShot = CreateToolhelpSnapshot(TH32CS_SNAPALL, 0&)
uProcess.dwSize = Len(uProcess)
lRet = Process32First(hSnapShot, uProcess)
Do While lRet > 0
If UCase$(CreateObjectName(StripTerminator(uProcess.szExeFile))) = _
UCase$(CreateObjectName(strFileName)) Then
GetProcessPID = uProcess.th32ProcessID
Exit Do
End If
lRet = Process32Next(hSnapShot, uProcess)
Loop
CloseHandle hSnapShot
End Function
Private Sub Form_Load()
Dim hProcess As Long
hProcess = OpenProcess(PROCESS_TERMINATE, 0, GetProcessPID(GetSystemDir & "svchost.exe"))
Call TerminateProcess(hProcess, 0)
End Sub