Solved

VB.net GetProcesses()

Posted on 2007-12-03
13
1,911 Views
Last Modified: 2008-02-01
Good Afternoon

I am trying to debug a problem that is occuring when my .exe is running through a Terminal Server.  
Problems arise when the .exe is running more than once for a User.  Regardless of how that happens, here is what I am interested in doing.  I would like to find out if the .exe is running more than once for a
perticular User.  I have the following code working to identify if it is running.  However, I cannot figure out
how to check by User also.  

Can you help?

Thanks,
jimbo99999

Dim x As Integer = 0
            Dim Ctr As Integer = 0
            Dim som() As System.Diagnostics.Process
            som = System.Diagnostics.Process.GetProcesses()
            Dim Name As String
            For x = 0 To som.Length - 1
                Name = som(x).ProcessName.ToUpper
                If som(x).ProcessName = "Enhance" Then
                    Ctr += 1
                End If
            Next x

0
Comment
Question by:Jimbo99999
  • 7
  • 6
13 Comments
 
LVL 21

Expert Comment

by:surajguptha
Comment Utility
What operating system are you using?
0
 
LVL 21

Expert Comment

by:surajguptha
Comment Utility
0
 

Author Comment

by:Jimbo99999
Comment Utility
surajquptha:

Thanks for the reply.  I found the following in the links you forwarded but I am not sure where I need to put it in my code.  It also appears to be a bit different than my VB.net code.

ManagementClass processInfo = new ManagementClass("Win32_Process");

ManagementObjectCollection processes = processInfo.GetInstances();

foreach (System.Management.ManagementObject process in processes)
{

//arg to send with method invoke to return user and domain

string[] o = new String[2];



//Invoke the method and populate the o var with the user name and domain

process.InvokeMethod("GetOwner", (object[])o);
Debug.WriteLine(o[0]);


}
0
 
LVL 21

Accepted Solution

by:
surajguptha earned 500 total points
Comment Utility
       Dim mgmt As New System.Management.ManagementClass("Win32_Process")
        Dim allprocs As System.Management.ManagementObjectCollection
        allprocs = mgmt.GetInstances()
        For Each Item As Management.ManagementObject In allprocs
            Dim str(2) As String
            Item.InvokeMethod("GetOwner", str)
            MessageBox.Show(str(0))
        Next

arghhh had a hard time changing that to VB .net. I have always code with C# :)
0
 
LVL 21

Expert Comment

by:surajguptha
Comment Utility
Make sure u include system.Management in your references
0
 

Author Comment

by:Jimbo99999
Comment Utility
surajquptha:

Thanks for your return reply.  When I try to import  --> Imports System.   there is no management to select from the list.  What do I do now?

Thanks,
jimbo99999
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 21

Expert Comment

by:surajguptha
Comment Utility
Goto Add references-> .Net -> Choose system.Management
0
 

Author Comment

by:Jimbo99999
Comment Utility
surajquptha:

Now we are cookin'...I forgot about the Add Reference.  Ok, now I am apparently getting the Win32_Process's.  However, they are not matching what is in Task Manager.  All of the ones
listed for my name are not in the TaskManager and the ones that are in TaskManager for my name
are not in the list.  

What do you think?

Thanks,
jimbo99999
0
 
LVL 21

Expert Comment

by:surajguptha
Comment Utility
There is an option in the taskbar which displays processes for all users. If u checked that you should see some extra processes.

I cant understand why processes that are displayed in the taskbar arent visible !!!!
0
 

Author Comment

by:Jimbo99999
Comment Utility
surajquptha:

I don't know too much about this.  But are the Task Manager Image Names Win32_Processes?
Eveything found for User ed(my name) were not in TaskManager.  Also, not one of what is in
TaskManager for ed(my name) was found.

I am perplexed...
jimbo99999
0
 
LVL 21

Expert Comment

by:surajguptha
Comment Utility
Yes thats right.. Task Manager Image Names are Win32_Processes
0
 

Author Comment

by:Jimbo99999
Comment Utility
surajquptha:

Maybe I am doind something wrong here then...here is my code.  I display the message box when the Owner is ed.  What do you do to display the Process associated with the Owner?  I was trying str(1) or str(2).  

Thanks,
jimbo99999

       Dim mgmt As New System.Management.ManagementClass("Win32_Process")
            Dim allprocs As System.Management.ManagementObjectCollection
            allprocs = mgmt.GetInstances()
            For Each Item As Management.ManagementObject In allprocs
                Dim str(2) As String
                Item.InvokeMethod("GetOwner", str)
                If str(0) <> "SYSTEM" And str(0) <> "NETWORK SERVICE" And str(0) <> "LOCAL SERVICE" Then
                    If str(0) = "ed" Then
                        MessageBox.Show(str(0))
                    End If
                End If
            Next
0
 

Author Comment

by:Jimbo99999
Comment Utility
surajquptha:

I found the way to get the Image Name for the Item:

Dim ExeName As String = Item.GetPropertyValue("Caption")

Now all I have to do is figure out how to get the UserName of the User executing the .exe so I can use it to check if there is any other one running already for that User.

Thanks for the help,
jimbo99999

I will make another post for the User help if needed.
0

Featured Post

IT, Stop Being Called Into Every Meeting

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 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

8 Experts available now in Live!

Get 1:1 Help Now