Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Get Process User name????

Posted on 2006-01-05
14
Medium Priority
?
5,649 Views
Last Modified: 2012-06-27
Is there any way to get the User Name that shows up in the task manager for a specific process? I know how to access the process collection and get pretty much every other piece of info from each one, but for the life of me, I can't figure out how to get the user name. I'm trying to automate Excel and have to manually kill each proces and I want to be able to pinpoint the exact process I'm trying to kill vs kill the first one in the list. Is there any other way I can pinpoint a particular process?
0
Comment
Question by:Turgeson
  • 5
  • 5
  • 2
  • +2
14 Comments
 

Author Comment

by:Turgeson
ID: 15621076
Just a follow up: I would prefer a .NET framework solution if possible.
0
 
LVL 9

Expert Comment

by:Headspace
ID: 15621293
...i think you need to use the wts api for that.  here's a sample that i located on the net (read the last comment):
 
http://www.dotnet247.com/247reference/msgs/20/100126.aspx

if that doesn't work, post back and i'll see if i can figure it out for you.

wil
0
 
LVL 22

Expert Comment

by:cookre
ID: 15621710
WindowsIdentity has to be called with an account token.  

To get the token, open the process with OpenProcess(),  then get the token with OpenProcessToken().

That said, I've never used WindowsIdentity, instead using GetTokenInformation() then LookupAccountSid().
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

Expert Comment

by:Bob Learned
ID: 15623015
VB.NET or C#?

Bob
0
 

Author Comment

by:Turgeson
ID: 15624189
Doesn't matter, but C# if possible.
0
 

Expert Comment

by:gr_kline
ID: 15628822
Use WMI to get the info.  You can use WMI query syntax if you want a specific process.

                        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 96

Expert Comment

by:Bob Learned
ID: 15629305
Well, there you go, some C# code to get your job done.

Too slow *GRIN*

Bob
0
 

Author Comment

by:Turgeson
ID: 15630960
This works, however I don't see how it's associated with a specific process as shown in my task manager. Here's how I was hoping I could handle it, but obviously with no UserName property or something equivalent, it doesn't work.

Process[] aProcs = Process.GetProcessesByName("EXCEL");
foreach(Process pr in aProcs)
{
      if(pr.UserName == _ThisUser)
      {
         pr.Kill();
         break;
      }
}

 I need someway to determine what particular Excel process was created by a specific user.

0
 

Expert Comment

by:gr_kline
ID: 15632393
There's probably a simplier was with query syntax, but this works.

                        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];
                            string caption;
                            string user = "Administrator";

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

                            caption = (string)process.GetPropertyValue("Caption");
                            Debug.WriteLine(String.Format("{0} - {1}", caption, o[0]));

                           

                            if (caption == "EXCEL.EXE" && o[0] == user)
                            {

                                process.InvokeMethod("Terminate", null);

                            }
                           


                        }
0
 

Author Comment

by:Turgeson
ID: 15632811
Ok this works, but now I've discovered getting the Procesname and Username may not be enough once many users start hitting my server. There's a possibility of the ASPNET user being listed twice. Using the Excel interop, is there a way to associate the handle of my Excel application object with the corresponding process created in the task manager.

Excel.Application oXL;

oXL = new Excel.Application()

A new process is created in the task manager, but neither its handle or processID relates to any of my oXL object's properties. There has to be some common property somewhere or else Windows wouldn't be able to list the process.
0
 

Accepted Solution

by:
gr_kline earned 825 total points
ID: 15645816
You shouldn't be using Excel on the server, it's not thread safe.  Maybe you should be looking at OWC.

Regardless, I answered your original question.
0
 

Expert Comment

by:gr_kline
ID: 15645883
How is your ASP.NET security and authorization configured?  Are you using Windows security and are you setting your ASP.NET application to impersonate the user?  If so, then the Excel.exe process may be lauched with the user's credentials.  Based on your description, it sounds as if that is not the case.

You can get the process id from the Excel.Application.Hwnd.  However, multilple ASP.NET threads (multiple users) will be using the same instance of Excel.exe.  Thusly killing an Excel.exe process will effect mulitple ASP.NET users.  Can't be a good thing.

I would strongly suggest that you not try to automate Excel on the server.  Office 12 will provide this ability, but not until late '06.  There are a few commerical products that I've used to translate an excel workbook to .NET code.  Look into those as well.
0
 

Author Comment

by:Turgeson
ID: 15651555
Do have a link to a product that can do that?
0
 

Expert Comment

by:gr_kline
ID: 15653837
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Starting up a Project

963 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