Internet Sellout

Demand Unearned Rewards

Kill Process By User SID VB.NET

I see a few C# Versions of this on the web. Here is a VB version.

Private Shared Function ProcessKillByOwnerSID(sProcessName As String, sSID As String) As Integer
        Dim oProcesses() As Process = System.Diagnostics.Process.GetProcessesByName(sProcessName)
        Dim i As Integer = 0
        For Each oProcess As Process In oProcesses
            If GetProcessInfoByPID(oProcess.Id, Nothing, Nothing) = sSID Then
                oProcess.Kill()
                i = i + 1
            End If
        Next
        Return i
End Function
Private Shared Function GetProcessInfoByPID(PID As Integer, sOutUser As String, sOutDomain As String) As String
        Dim sOwnerSID As String = String.Empty
        Try
            Dim sQuery As System.Management.ObjectQuery = New System.Management.ObjectQuery("Select * from Win32_Process Where ProcessID = '" & PID.ToString() & "'")
            Dim oSearcher As System.Management.ManagementObjectSearcher = New System.Management.ManagementObjectSearcher(sQuery)
            If oSearcher.Get().Count = 0 Then Return sOwnerSID
            For Each oReturn As System.Management.ManagementObject In oSearcher.Get()
                Dim sOwner(2) As String
                oReturn.InvokeMethod("GetOwner", CType(sOwner, Object()))
                sOutUser = sOwner(0)
                If sOutUser Is Nothing Then sOutUser = String.Empty
                sOutDomain = sOwner(1)
                If sOutDomain Is Nothing Then sOutDomain = String.Empty
                Dim sSID(1) As String
                oReturn.InvokeMethod("GetOwnerSid", CType(sSID, Object()))
                sOwnerSID = sSID(0)
                Return sOwnerSID
            Next
        Catch
            Return sOwnerSID
        End Try
        Return sOwnerSID
End Function
Comments are closed