Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Process.StartInfo.Username is empty

I start a Process by

Process app = new Process();
app.StartInfo.UseShellExecute = false;
app.StartInfo.FileName = path;
app.StartInfo.Domain = "Domain";
app.StartInfo.UserName = "userName";

string password = "Password";
System.Security.SecureString ssPwd = new System.Security.SecureString();

for (int x = 0; x < password.Length; x++)
{
    ssPwd.AppendChar(password[x]);
}

password = "";

app.StartInfo.Password = ssPwd;
app.Start();

Then I confirm it is running by:

private bool IsRunning(string name)
{
    Process[] processlist = Process.GetProcesses();

    if (Process.GetProcessesByName(name).Length > 0)
    {
        string user = Process.GetProcessesByName(name)[0].StartInfo.UserName; 
        log.Debug("Process " + name + " is running by : " + user);

        return true;
    }
    else
    {
        return false;
    }
}

I get back true and find the process but the UserName is empty. Why is that?

I also found some code to get the Owner of the Process but when I use this the Owner is also empty.

public string GetProcessOwner(int processId)
{
    string query = "SELECT * FROM Win32_Process WHERE ProcessID = " + processId;
    ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
    ManagementObjectCollection processList = searcher.Get();

    foreach (ManagementObject obj in processList)
    {
        string[] argList = new string[] { string.Empty, string.Empty };
        int returnVal = Convert.ToInt32(obj.InvokeMethod("GetOwner", argList));

        if (returnVal == 0)
        {
            // return DOMAIN\user
            return argList[1] + "\\" + argList[0];
        }
    }

    return "NO OWNER";
}

Please can you explain me why this is so?

like image 656
Ephedra Avatar asked Dec 05 '18 09:12

Ephedra


2 Answers

This is by design; when requesting information about a Process via eg. GetProcessesByName the UserName doesn't get retrieved/resolved.

GetProcessByName internally retrieves its info via the code below, constructing Process instances from the obtained ProcesInfo data.

  public static Process[] GetProcesses(string machineName) 
  {
        bool isRemoteMachine = ProcessManager.IsRemoteMachine(machineName);
        ProcessInfo[] processInfos = ProcessManager.GetProcessInfos(machineName);
        Process[] processes = new Process[processInfos.Length];
        for (int i = 0; i < processInfos.Length; i++) {
            ProcessInfo processInfo = processInfos[i];

            processes[i] = new Process(machineName, isRemoteMachine, processInfo.processId, processInfo);
        }

        return processes;
}


A ProcessInfo instance doesn't contain any user/owner information.

internal class ProcessInfo 
{
    public ArrayList threadInfoList = new ArrayList();
    public int basePriority;
    public string processName;
    public int processId;
    public int handleCount;
    public long poolPagedBytes;
    public long poolNonpagedBytes;
    public long virtualBytes;
    public long virtualBytesPeak;
    public long workingSetPeak;
    public long workingSet;
    public long pageFileBytesPeak;
    public long pageFileBytes;
    public long privateBytes;
    public int mainModuleId;
    public int sessionId;
}


The private constructor of the Process class accepts this ProcessInfo.

Because no ProcessStartInfo has been set, it instantiates one on retrieval, having an empty UserName.

public string UserName {
    get { 
        if( userName == null) {
            return string.Empty;
        }
        else {        
            return userName;                     
        }
    } 
    set { userName = value; }
}

About the GetProcessOwner code; this results from the answer to the How do I determine the owner of a process in C#? question.

This is rather an other WMI related topic; not receiving any owner info might have to do with permissions as suggested in the comments.
For me, "it works on my machine" ...
Better start a separate question for this one.

like image 114
pfx Avatar answered Oct 22 '22 10:10

pfx


I tried with solution using WinAPIs

You would get "NO OWNER" when the application you are trying to get the owner, is not run as admin Please check by running your application as Administrator

like image 43
Umesh CHILAKA Avatar answered Oct 22 '22 08:10

Umesh CHILAKA