Click here to Skip to main content
15,888,733 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I am using .net 2010
Now I am trying to get the user if from a process id. The code is:

C#
using System;
using System.Net;
using System.Collections;
//using System.Net.NetworkInformation;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Management;
using System.Security.Permissions;
using System.Security.Principal;
using System.ComponentModel.Composition;
using System.ComponentModel;
using System.Diagnostics;


namespace getUser
{
    public static class Utils
    {
        public const int NO_ERROR = 0;
        public const int MIB_TCP_STATE_CLOSED = 1;
        public const int MIB_TCP_STATE_LISTEN = 2;
        public const int MIB_TCP_STATE_SYN_SENT = 3;
        public const int MIB_TCP_STATE_SYN_RCVD = 4;
        public const int MIB_TCP_STATE_ESTAB = 5;
        public const int MIB_TCP_STATE_FIN_WAIT1 = 6;
        public const int MIB_TCP_STATE_FIN_WAIT2 = 7;
        public const int MIB_TCP_STATE_CLOSE_WAIT = 8;
        public const int MIB_TCP_STATE_CLOSING = 9;
        public const int MIB_TCP_STATE_LAST_ACK = 10;
        public const int MIB_TCP_STATE_TIME_WAIT = 11;
        public const int MIB_TCP_STATE_DELETE_TCB = 12;
        //#region helper function
        const int MAXSIZE = 16384; // size _does_ matter
        public static string GetProcessInfoByPID(int PID, out string User, out string Domain)//, out string OwnerSID)
        {
            //DataTable dt = new DataTable();
            //dt.Columns.Add("ProcessID");
            //dt.Columns.Add("Name");
            //dt.Columns.Add("Description");
            //dt.Columns.Add("User");
            //dt.Columns.Add("Domain");
            //dt.Columns.Add("OwnerSID");
            User = String.Empty;
            Domain = String.Empty;
            String OwnerSID = String.Empty;
            string processname = String.Empty;
            try
            {
                ObjectQuery sq = new ObjectQuery("Select * from Win32_Process Where ProcessID = '" + PID + "'");
                //ObjectQuery sq = new ObjectQuery(Query);
                ManagementObjectSearcher searcher = new ManagementObjectSearcher(sq);
                if (searcher.Get().Count == 0)
                    return "Unknown";
                foreach (ManagementObject oReturn in searcher.Get())
                {
                    //Name of process
                    //arg to send with method invoke to return user and domain - below is link to SDK doc on it
                    string[] o = new String[2];
                    //Invoke the method and populate the o var with the user name and domain
                    oReturn.InvokeMethod("GetOwner", (object[])o);
                    //int pid = (int)oReturn["ProcessID"];
                    processname = (string)oReturn["Name"];
                    //dr[2] = oReturn["Description"];
                    User = o[0];
                    if (User == null)
                        User = String.Empty;
                    Domain = o[1];
                    if (Domain == null)
                        Domain = String.Empty;
                    string[] sid = new String[1];
                    oReturn.InvokeMethod("GetOwnerSid", (object[])sid);
                    OwnerSID = sid[0];
                    return OwnerSID;
                }
            }
            catch
            {
                return OwnerSID;
            }
            return OwnerSID;
        }
    }
}



Now I get this error:

Error	3	The type 'System.ComponentModel.Component' is defined in an assembly that is not referenced. You must add a reference to assembly 'System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.	C:\Documents and Settings\Administrator\My Documents\Visual Studio 2010\Projects\getUser\getprocessownersid.cs	54	17	getUser




Can anyone tell me what to do?
Posted
Updated 1-Aug-11 16:34pm
v2

1 solution

Hi,

See this sample. I use to pass Apps Name rather than ProcessID for query.

C#
   using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Management;
using System.Text;
public partial class ProcessId : System.Web.UI.Page
{
       
    protected void Button1_Click(object sender, EventArgs e)
    {
        string PID = "Outlook.exe";
        string User = string.Empty;
        string Domain = string.Empty;
        var processId = GetProcessInfoByPID(PID, User, Domain);
    }
    private object GetProcessInfoByPID(string PID, string User, string Domain) //, out string OwnerSID)
    {
        string  OwnerSID = string.Empty;
        ConnectionOptions connection = new ConnectionOptions();
        ConnectionOptions options = new ConnectionOptions();
        string targetIpAddress = "210.0.0.000";   // Pass a valid IP address (sample only)
        connection.Authentication = System.Management.AuthenticationLevel.Packet;
        ManagementScope scope = new ManagementScope(("\\\\" + targetIpAddress), options);
        scope.Connect();
        ManagementPath p = new ManagementPath("Win32_Product");
        ManagementClass classInstance = new ManagementClass(scope, p, null);
        ObjectQuery query = new ObjectQuery("Select * from Win32_Process Where Name = '" + PID + "'");
        ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
        foreach (ManagementObject oReturn in searcher.Get())
        {
            //Name of process
            //arg to send with method invoke to return user and domain - below is link to SDK doc on it
            string[] o = new String[2];
            //Invoke the method and populate the o var with the user name and domain
            oReturn.InvokeMethod("GetOwner", (object[])o);
            var pid = oReturn["ProcessID"];
            var processname = (string)oReturn["Name"];
            //dr[2] = oReturn["Description"];
            User = o[0];
            if (User == null)
                User = String.Empty;
            Domain = o[1];
            if (Domain == null)
                Domain = String.Empty;
            string[] sid = new String[1];
            oReturn.InvokeMethod("GetOwnerSid", (object[])sid);
            OwnerSID = sid[0];
            return OwnerSID;
        }
        return OwnerSID;
    }
}


Please remember to mark the replies as answers if they help and unmark them if they provide no help.

Regards,

Algem
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900