Solved

Want to create small app to identify owner of running processes.

Posted on 2004-08-10
14
546 Views
Last Modified: 2008-02-01
I'm a die-hard Windows XP user and at my new job they use 2000 pro.  With XP I could shut down a lot of the running processes because it would say either "myUserName" or "System" etc.  Is there a way to create an app where I could identify the owner of these processes?
0
Comment
Question by:UberDeveloper
14 Comments
 
LVL 3

Expert Comment

by:Scoobyed
Comment Utility
0
 
LVL 2

Author Comment

by:UberDeveloper
Comment Utility
I'm not that advanced yet I only have 9120 expert points :D

how do I implement?

If I want to have just one file in my project I need to:

A)  Place the class after the "Form" class?  Or is it a C# class and thus I would need to put it in its own file?

B) Do I just call it like so:  CIM_Process.GetOwner   ?

or I mean GetOwner = CIM_Process.GetOwner

Normally I would take the time to work my way through the MSDN but my new work computer has 128MB RAM so the DevEnv runs kinda slow.  So I really need to shut down unnecessary processes.  BY the way.  I was told that ANY processes that a certain user owns can be shut down.  Are there any exceptions to this rule?  (If there are can you or anyone tell me as I would like to "CATCH" these exceptions.) ;)  get it???   LOL.   Thanks in advance.
0
 
LVL 4

Expert Comment

by:srcalc
Comment Utility
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 300 total points
Comment Utility
(1) Add reference to System.Management.dll

(2) Add this code to a form or class module.

 Public Function EnumerateProcesses(ByVal processName As String) As String()

    ' Form a WMI object query to get all the processes running on the local machine.
    Dim queryObjects As New ObjectQuery("SELECT * FROM Win32_Process")

    ' Search for all Win32_Process objects.
    Dim searcherObjects As New ManagementObjectSearcher(queryObjects)

    ' Keep the strings in the format:  
    '
    '   Process name: iexplore.exe, user: {domain}\{user}
    Dim objectList() As String
    Dim objectCount As Integer

    For Each objectFound As ManagementObject In searcherObjects.Get()

      ' Call the GetOwner method from the Win32_Process object to get Domain and User properties.
      Dim outParams As ManagementBaseObject = objectFound.InvokeMethod("GetOwner", Nothing, Nothing)

      ' Make enough room for the process reference.
      ReDim Preserve objectList(objectCount)

      ' Format the string to return to the caller.
      Dim formatOutput As String = String.Format("Process name: {0}, user: {1}\{2}", _
        objectFound("Name"), outParams("Domain"), outParams("User"))

      objectCount += 1

    Next

    Return objectList

  End Function 'EnumerateProcesses'

(3)  Add Imports System.Management at the top of the module.


Bob
0
 
LVL 2

Author Comment

by:UberDeveloper
Comment Utility
Thanks bob, I got it to work, sort of.  If I step through the code I see the values for the processes.  I'm having trouble displaying the results though.  

I'm trying to display the results in a listbox so I have:

        Dim i As Integer
        For i = 0 To objectCount
            ListBox1.Items.Add(objectList.GetValue(objectCount))
        Next

But I get an Array Index out of bounds error.  Any Ideas?
0
 
LVL 4

Assisted Solution

by:srcalc
srcalc earned 200 total points
Comment Utility
       Dim i As Integer
        For i = 0 To objectCount - 1
            ListBox1.Items.Add(objectList.GetValue(objectCount))
        Next
0
 
LVL 2

Author Comment

by:UberDeveloper
Comment Utility
Now I"m getting this error:

Additional information: Index was outside the bounds of the array.

On this line:

ListBox1.Items.Add(objectList.GetValue(objectCount))
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 4

Expert Comment

by:srcalc
Comment Utility
      Dim i As Integer
        For i = 0 To objectCount - 1
            ListBox1.Items.Add(objectList.GetValue(i))
        Next
0
 
LVL 2

Author Comment

by:UberDeveloper
Comment Utility
Now Value Cannot Be Null.   :(
0
 
LVL 4

Expert Comment

by:srcalc
Comment Utility
paste in all your code and i can see if i can get it to work.
0
 
LVL 2

Author Comment

by:UberDeveloper
Comment Utility
Imports System.Management
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.ListBox1 = New System.Windows.Forms.ListBox
        Me.SuspendLayout()
        '
        'ListBox1
        '
        Me.ListBox1.Location = New System.Drawing.Point(16, 8)
        Me.ListBox1.Name = "ListBox1"
        Me.ListBox1.Size = New System.Drawing.Size(264, 251)
        Me.ListBox1.TabIndex = 0
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Controls.Add(Me.ListBox1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region
    Public objectList() As String
    Public objectCount As Integer

    Public Function EnumerateProcesses(ByVal processName As String) As String()

        ' Form a WMI object query to get all the processes running on the local machine.
        Dim queryObjects As New ObjectQuery("SELECT * FROM Win32_Process")

        ' Search for all Win32_Process objects.
        Dim searcherObjects As New ManagementObjectSearcher(queryObjects)

        ' Keep the strings in the format:  
        '
        '   Process name: iexplore.exe, user: {domain}\{user}


        For Each objectFound As ManagementObject In searcherObjects.Get()

            ' Call the GetOwner method from the Win32_Process object to get Domain and User properties.
            Dim outParams As ManagementBaseObject = objectFound.InvokeMethod("GetOwner", Nothing, Nothing)

            ' Make enough room for the process reference.
            ReDim Preserve objectList(objectCount)

            ' Format the string to return to the caller.
            Dim formatOutput As String = String.Format("Process name: {0}, user: {1}\{2}", _
              objectFound("Name"), outParams("Domain"), outParams("User"))

            objectCount += 1

        Next

        Return objectList

    End Function 'EnumerateProcesses'


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim Process As String
        EnumerateProcesses(Process)
        Dim i As Integer
        For i = 0 To objectCount
            ListBox1.Items.Add(objectList.GetValue(i))
        Next
    End Sub
End Class
0
 
LVL 4

Expert Comment

by:srcalc
Comment Utility
Imports System.Management
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.ListBox1 = New System.Windows.Forms.ListBox
        Me.SuspendLayout()
        '
        'ListBox1
        '
        Me.ListBox1.Location = New System.Drawing.Point(16, 8)
        Me.ListBox1.Name = "ListBox1"
        Me.ListBox1.Size = New System.Drawing.Size(264, 251)
        Me.ListBox1.TabIndex = 0
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Controls.Add(Me.ListBox1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region
    Public objectList() As String
    Public objectCount As Integer

    Public Function EnumerateProcesses(ByVal processName As String) As String()

        ' Form a WMI object query to get all the processes running on the local machine.
        Dim queryObjects As New ObjectQuery("SELECT * FROM Win32_Process")

        ' Search for all Win32_Process objects.
        Dim searcherObjects As New ManagementObjectSearcher(queryObjects)

        ' Keep the strings in the format:  
        '
        '   Process name: iexplore.exe, user: {domain}\{user}


        For Each objectFound As ManagementObject In searcherObjects.Get()

            ' Call the GetOwner method from the Win32_Process object to get Domain and User properties.
            Dim outParams As ManagementBaseObject = objectFound.InvokeMethod("GetOwner", Nothing, Nothing)

            ' Make enough room for the process reference.
            ReDim Preserve objectList(objectCount)

            ' Format the string to return to the caller.
            Dim formatOutput As String = String.Format("Process name: {0}, user: {1}\{2}", _
              objectFound("Name"), outParams("Domain"), outParams("User"))
            objectList(objectCount) = formatOutput
            objectCount += 1

        Next

        Return objectList

    End Function 'EnumerateProcesses'


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim Process As String
        EnumerateProcesses(Process)
        Dim i As Integer
        For i = 0 To objectCount - 1
            ListBox1.Items.Add(objectList.GetValue(i))
        Next
    End Sub
End Class
0
 
LVL 4

Expert Comment

by:srcalc
Comment Utility
That code worked for me, all i needed to do was add this line:
objectList(objectCount) = formatOutput

And change this one:
For i = 0 To objectCount - 1


If it doesnt work I'll be back in a few hours to check.
0
 
LVL 2

Author Comment

by:UberDeveloper
Comment Utility
Sweet It worked.

Thanks a lot.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

763 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

6 Experts available now in Live!

Get 1:1 Help Now