Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2004-08-10
14
560 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
ID: 11760787
0
 
LVL 2

Author Comment

by:UberDeveloper
ID: 11761021
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
ID: 11763249
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 96

Accepted Solution

by:
Bob Learned earned 300 total points
ID: 11766674
(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
ID: 11767800
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
ID: 11767996
       Dim i As Integer
        For i = 0 To objectCount - 1
            ListBox1.Items.Add(objectList.GetValue(objectCount))
        Next
0
 
LVL 2

Author Comment

by:UberDeveloper
ID: 11768117
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
 
LVL 4

Expert Comment

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

Author Comment

by:UberDeveloper
ID: 11768221
Now Value Cannot Be Null.   :(
0
 
LVL 4

Expert Comment

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

Author Comment

by:UberDeveloper
ID: 11768309
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
ID: 11768374
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
ID: 11768382
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
ID: 11768422
Sweet It worked.

Thanks a lot.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

789 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