Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-08-10
14
Medium Priority
?
569 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 96

Accepted Solution

by:
Bob Learned earned 1200 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 800 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

688 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