Solved

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

Posted on 2004-08-10
14
567 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
[X]
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
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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

623 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