[ACCEPTED]-How do you get the UserName of the owner of a process?-process

Accepted answer
Score: 16

Thanks, your answers put me on the proper 1 path. For those who needs a code sample:

public class App
{
    public static void Main(string[] Args)
    {
        Management.ManagementObjectSearcher Processes = new Management.ManagementObjectSearcher("SELECT * FROM Win32_Process");

        foreach (Management.ManagementObject Process in Processes.Get()) {
            if (Process["ExecutablePath"] != null) {
                string ExecutablePath = Process["ExecutablePath"].ToString();

                string[] OwnerInfo = new string[2];
                Process.InvokeMethod("GetOwner", (object[]) OwnerInfo);

                Console.WriteLine(string.Format("{0}: {1}", IO.Path.GetFileName(ExecutablePath), OwnerInfo[0]));
            }
        }

        Console.ReadLine();
    }
}
Score: 12

The CodeProject article How To Get Process Owner ID and Current User SID by Warlib describes how 5 to do this using both WMI and using the 4 Win32 API via PInvoke.

The WMI code is much 3 simpler but is slower to execute. Your question 2 doesn't indicate which would be more appropriate 1 for your scenario.

Score: 3

You will have a hard time getting the username 12 without being an administrator on the computer.

None 11 of the methods with WMI, through the OpenProcess 10 or using the WTSEnumerateProcesses will 9 give you the username unless you are an 8 administrator. Trying to enable SeDebugPrivilege 7 etc does not work either. I have still to 6 see a code that works without being the 5 admin.

If anyone know how to get this WITHOUT 4 being an admin on the machine it is being 3 run, please write how to do it, as I have 2 not found out how to enable that level of 1 access to a service user.

Score: 2

You might look at using System.Management 2 (WMI). With that you can query the Win32_Process 1 tree.

Score: 2

here is the MS link labelled "GetOwner Method 1 of the Win32_Process Class"

Score: 2

Props to Andrew Moore for his answer, I'm 2 merely formatting it because it didn't compile 1 in C# 3.5.

private string GetUserName(string procName)
{
    string query = "SELECT * FROM Win32_Process WHERE Name = \'" + procName + "\'";
    var procs = new System.Management.ManagementObjectSearcher(query);
    foreach (System.Management.ManagementObject p in procs.Get())
    {
        var path = p["ExecutablePath"];
        if (path != null)
        {
            string executablePath = path.ToString();
            string[] ownerInfo = new string[2];
            p.InvokeMethod("GetOwner", (object[])ownerInfo);
            return ownerInfo[0];
        }
    }
    return null;
}
Score: 0

You'll need to add a reference to System.Management.dll for this to work.

Here's what I ended up using. It works 1 in .NET 3.5:

using System.Linq;
using System.Management;

class Program
{
    /// <summary>
    /// Adapted from https://www.codeproject.com/Articles/14828/How-To-Get-Process-Owner-ID-and-Current-User-SID
    /// </summary>
    public static void GetProcessOwnerByProcessId(int processId, out string user, out string domain)
    {
        user = "???";
        domain = "???";

        var sq = new ObjectQuery("Select * from Win32_Process Where ProcessID = '" + processId + "'");
        var searcher = new ManagementObjectSearcher(sq);
        if (searcher.Get().Count != 1)
        {
            return;
        }
        var process = searcher.Get().Cast<ManagementObject>().First();
        var ownerInfo = new string[2];
        process.InvokeMethod("GetOwner", ownerInfo);

        if (user != null)
        {
            user = ownerInfo[0];
        }
        if (domain != null)
        {
            domain = ownerInfo[1];
        }
    }

    public static void Main()
    {
        var processId = System.Diagnostics.Process.GetCurrentProcess().Id;
        string user;
        string domain;
        GetProcessOwnerByProcessId(processId, out user, out domain);
        System.Console.WriteLine(domain + "\\" + user);
    }
}

More Related questions