Solved

VB.net GetProcesses()

Posted on 2007-12-03
13
1,913 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
ID: 20396045
What operating system are you using?
0
 
LVL 21

Expert Comment

by:surajguptha
ID: 20396071
0
 

Author Comment

by:Jimbo99999
ID: 20396169
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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 21

Accepted Solution

by:
surajguptha earned 500 total points
ID: 20396378
       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
ID: 20396384
Make sure u include system.Management in your references
0
 

Author Comment

by:Jimbo99999
ID: 20396574
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
 
LVL 21

Expert Comment

by:surajguptha
ID: 20396751
Goto Add references-> .Net -> Choose system.Management
0
 

Author Comment

by:Jimbo99999
ID: 20396924
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
ID: 20397025
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
ID: 20397079
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
ID: 20397526
Yes thats right.. Task Manager Image Names are Win32_Processes
0
 

Author Comment

by:Jimbo99999
ID: 20397622
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
ID: 20397823
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

776 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