[ad_1]
I can successfully kill processes using API calls.
I am now trying to terminate a SYSTEM process, but my attempts to kill it fail. OpenProcess returns Access Denied.
I can kill this system process by using the TaskManager and choosing “End Process”.
What am I missing to make OpenProcess work?
Thank you.
Here is my entire code:
Public Sub KillProcess(ByVal uProcID As Long)
pGiveThisAppKillPrivilege
Dim lHandle&
lHandle = OpenProcess(SYNCHRONIZE Or PROCESS_TERMINATE, ByVal 0&, uProcID)
If lHandle = 0 Then
Debug.Assert False
MsgBox "Dll Error " & Err.LastDllError & " occured while trying to open process."
Exit Sub
End If
Debug.Print Err.LastDllError
If lHandle = 0 Then
Debug.Assert False
MsgBox "Error finding target process handle"
Exit Sub
End If
' Terminate the process.
If TerminateProcess(lHandle, 0&) = 0 Then
Debug.Assert False
MsgBox "Error terminating process"
Else
MsgBox "Process terminated"
End If
CloseHandle lHandle
End Sub
Private Sub pGiveThisAppKillPrivilege()
Dim hdlTokenHandle As Long
Dim tmpLuid As LUID
Dim tkp As TOKEN_PRIVILEGES
Dim tkpNewButIgnored As TOKEN_PRIVILEGES
Dim lBufferNeeded As Long
Dim lHandleOfThisProcess As Long
lHandleOfThisProcess = GetCurrentProcess()
Dim lRet&
lRet = OpenProcessToken(lHandleOfThisProcess, (TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY), hdlTokenHandle)
If lRet = 0 Then
Debug.Assert False
MsgBox "Could not OpenProcessToken! Error: " & Err.LastDllError & ", lret: " & lRet
Debug.Assert False
Exit Sub
End If
LookupPrivilegeValue "", "SeDebugPrivilege", tmpLuid
tkp.PrivilegeCount = 1 ' One privilege to set
tkp.TheLuid = tmpLuid
tkp.Attributes = SE_PRIVILEGE_ENABLED
' Enable the kill privilege in the access token of this
' process.
lRet = AdjustTokenPrivileges(hdlTokenHandle, False, tkp, Len(tkpNewButIgnored), tkpNewButIgnored, lBufferNeeded)
If lRet = 0 Then
Debug.Assert False
MsgBox "Could not AdjustTokenPrivileges! Error: " & Err.LastDllError & ", lret: " & lRet
Debug.Assert False
Exit Sub
End If
End Sub
[ad_2]