Solved

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

Posted on 2004-08-10
14
559 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
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

777 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