Link to home
Start Free TrialLog in
Avatar of Turgeson
Turgeson

asked on

Get Process User name????

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?
Avatar of Turgeson
Turgeson

ASKER

Just a follow up: I would prefer a .NET framework solution if possible.
...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
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().
Avatar of Bob Learned
VB.NET or C#?

Bob
Doesn't matter, but C# if possible.
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]);


                        }
Well, there you go, some C# code to get your job done.

Too slow *GRIN*

Bob
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.

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);

                            }
                           


                        }
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.
ASKER CERTIFIED SOLUTION
Avatar of gr_kline
gr_kline

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Do have a link to a product that can do that?