Solved

How do I kill a process by user on a Terminal Server

Posted on 2007-04-04
6
318 Views
Last Modified: 2013-11-07
I have a program that needs to kill any previous instances of itself whenever it is opened.
I'm using the following code...

    Dim processes As Process
    Dim temp As Byte, x As Byte
    Dim tempProcess(5) As Process : Dim TempProcessTime As DateTime
    For Each processes In Process.GetProcesses
      If processes.ToString = "System.Diagnostics.Process (SipcaEMR)" Then temp = temp + 1 : tempProcess(temp) = processes : If DateDiff(DateInterval.Second, processes.StartTime, TempProcessTime) < 0 Then TempProcessTime = processes.StartTime
    Next
    If temp > 1 Then
      For x = 1 To temp
        If Not tempProcess(x).StartTime = TempProcessTime Then tempProcess(x).Kill()
      Next
    End If
This works fine on my Fat Clients.
The problem is that this will not work on a terminal server because it will kill of the processes of all the users using this application. I want it to only kill off and previously opened SipcaEMR that this user is running. Leaving the rest alone
0
Comment
Question by:jamescorwin
  • 3
  • 3
6 Comments
 
LVL 10

Expert Comment

by:tpwells
ID: 18881959
Add System.Managment as a Reference to your project and then try this function:

    Private Sub KillProcess(ByVal ProcessName As String, ByVal UserName As String)
        Dim iProc As New Management.ManagementClass("Win32_Process")
        Dim procs As Management.ManagementObjectCollection = iProc.GetInstances()
        For Each oProc As Management.ManagementObject In procs
            Dim user(1) As String
            oProc.InvokeMethod("GetOwner", CType(user, Object()))
            If Not user(1) Is Nothing Then
                If (oProc.GetPropertyValue("Caption").ToString() = ProcessName _
                And UserName = user(0).ToString()) Then
                    oProc.InvokeMethod("Terminate", Nothing)
                End If
            End If
        Next
    End Sub

Created From Code on this Post:
http://www.experts-exchange.com/Programming/Misc/Q_21685488.html
0
 

Author Comment

by:jamescorwin
ID: 18882462
When I try to add import System.Management to my list of imports it does not know Management.
I can't seem to find the dll reference for system.management in case I need to browse for that in my references.
I'm am using Visual Studio.Net 2003. I'm in the VB.Net part of it.
How do I add the reference for System.Managment
0
 
LVL 10

Expert Comment

by:tpwells
ID: 18882502
In the Solutions Explorer right-click on The References folder and select "Add Reference". In the dialogue that comes up scroll down the list on the .NET tab until you find the System.Management Component then double-click it and then Click "OK"
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:jamescorwin
ID: 18882552
Nevermind, I found it in Project, Add Reference
0
 

Author Comment

by:jamescorwin
ID: 18883181
Is there a way I can know which instance is the last opened as I do not want to kill that one.
Such as can I use as command like oProc.InvokeMethod("GetOwner", CType(user, Object()))
to get the time it was opened or the amount of time it has been running.
0
 
LVL 10

Accepted Solution

by:
tpwells earned 500 total points
ID: 18883594
You can use the CreationDate Property of the oProc above. Here is some modified code of what I posted above to kill all but the newest process.

    Private Sub KillProcess(ByVal ProcessName As String, ByVal UserName As String)
        Dim Processes As New ArrayList 'list of the processes with this name and user
        Dim iProc As New Management.ManagementClass("Win32_Process")
        Dim procs As Management.ManagementObjectCollection = iProc.GetInstances()
        For Each oProc As Management.ManagementObject In procs
            Dim user(1) As String
            oProc.InvokeMethod("GetOwner", CType(user, Object()))
            If Not user(1) Is Nothing Then
                If (oProc.GetPropertyValue("Caption").ToString() = ProcessName _
                And UserName = user(0).ToString()) Then
                    Dim strCreateTime As Int64 = Convert.ToInt64(oProc.GetPropertyValue("CreationDate").ToString.Split(".".ToCharArray())(0))
                    Dim intHandle As Integer = Convert.ToInt32(oProc.GetPropertyValue("Handle"))
                    Processes.Add(New clsProcess(intHandle, strCreateTime))
                End If
            End If
        Next
        If Processes.Count > 1 Then
            'kill the older processes
            Dim CurrentWinner As clsProcess = CType(Processes(0), clsProcess)
            For i As Integer = 1 To Processes.Count - 1
                CurrentWinner = CheckProcesses(CurrentWinner, CType(Processes(i), clsProcess))
            Next
        End If
    End Sub

    'returns the process that is newer and kills the older one
    Private Function CheckProcesses(ByVal Proc1 As clsProcess, ByVal Proc2 As clsProcess) As clsProcess
        If (Proc1.CreateTime > Proc2.CreateTime) Then
            Proc2.Kill()
            Return Proc1
        Else
            If Not (Proc1 Is Proc2) Then
                Proc1.Kill()
                Return Proc2
            End If
        End If
    End Function

    Private Class clsProcess
        Public Sub New(ByVal ProcHandle As Integer, ByVal ProcCreateTime As Int64)
            _Handle = ProcHandle
            _CreateTime = ProcCreateTime
        End Sub
        Private _Handle As Integer
        Private _CreateTime As Int64
        Private _Killed As Boolean
        Public ReadOnly Property Handle() As Integer
            Get
                Return _Handle
            End Get
        End Property

        Public ReadOnly Property CreateTime() As Int64
            Get
                Return _CreateTime
            End Get
        End Property

        Public ReadOnly Property Killed() As Boolean
            Get
                Return _Killed
            End Get
        End Property

        Public Sub Kill()
            Dim proc As Process = Process.GetProcessById(Handle)
            proc.Kill()
            _Killed = True
        End Sub
    End Class
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now