/// <summary> /// Get the process. /// </summary> /// <param name="host">The host name to get system information for</param> /// <param name="username">The username for the host</param> /// <param name="password">The password for the host</param> /// <param name="processID">The id of the process to get.</param> /// <param name="process">The process.</param> /// <returns>The status the the connection.</returns> public Nequeo.Management.Sys.Status GetProcess(String host, String username, String password, int processID, out ProcessDetails process) { ProcessDetails[] processes = null; process = processes[0]; return(GetProcessesEx(host, username, password, out processes, processID)); }
/// <summary> /// Get the processes. /// </summary> /// <param name="host">The host name to get system information for</param> /// <param name="username">The username for the host</param> /// <param name="password">The password for the host</param> /// <param name="processes">The collection of processes.</param> /// <param name="processID">The id of the process to get.</param> /// <returns>The status the the connection.</returns> private Nequeo.Management.Sys.Status GetProcessesEx(String host, String username, String password, out ProcessDetails[] processes, int processID = -1) { processes = null; // No blank username's allowed. if (username == string.Empty) { username = null; password = null; } // Configure the connection settings. ConnectionOptions options = new ConnectionOptions(); options.Username = username; options.Password = password; // Set the scope for the connection. ManagementPath path = new ManagementPath(String.Format("\\\\{0}\\root\\cimv2", host)); ManagementScope scope = new ManagementScope(path, options); // Try and connect to the remote (or local) machine. try { // Connect to the machine scope.Connect(); } catch (ManagementException) { return(Nequeo.Management.Sys.Status.AuthenticateFailure); } catch (System.Runtime.InteropServices.COMException) { return(Nequeo.Management.Sys.Status.RPCServicesUnavailable); } catch (System.UnauthorizedAccessException) { return(Nequeo.Management.Sys.Status.UnauthorizedAccess); } catch (System.Exception) { return(Nequeo.Management.Sys.Status.Failed); } // Create a management searcher for all processes ManagementObjectSearcher moSearch = new ManagementObjectSearcher(scope, new ObjectQuery("SELECT " + "Handle, ProcessId, Name, Caption, Description, " + "ExecutablePath, HandleCount, Status, ThreadCount, " + "VirtualSize, ReadTransferCount, WriteTransferCount " + "FROM Win32_Process" + (processID > -1 ? " WHERE ProcessId = " + processID.ToString() : ""))); ManagementObjectCollection moReturn = moSearch.Get(); // Create the process collection. processes = new ProcessDetails[moReturn.Count]; int i = 0; // For each process found load into process structure. foreach (ManagementObject mo in moReturn) { processes[i].Handle = mo["Handle"].ToString(); processes[i].ProcessId = uint.Parse(mo["ProcessId"].ToString()); processes[i].Name = mo["Name"].ToString(); processes[i].Caption = mo["Caption"].ToString(); processes[i].Description = mo["Description"].ToString(); processes[i].ExecutablePath = mo["ExecutablePath"].ToString(); processes[i].HandleCount = uint.Parse(mo["HandleCount"].ToString()); processes[i].Status = mo["Status"].ToString(); processes[i].ThreadCount = uint.Parse(mo["ThreadCount"].ToString()); processes[i].VirtualSize = ulong.Parse(mo["VirtualSize"].ToString()); processes[i].ReadTransferCount = ulong.Parse(mo["ReadTransferCount"].ToString()); processes[i].WriteTransferCount = ulong.Parse(mo["WriteTransferCount"].ToString()); i++; } // Successful. return(Nequeo.Management.Sys.Status.Success); }
/// <summary> /// Get the process. /// </summary> /// <param name="processID">The id of the process to get.</param> /// <param name="process">The process.</param> /// <param name="host">The host name to get system information for (Default = localhost)</param> /// <returns>The status the the connection.</returns> public Nequeo.Management.Sys.Status GetProcess(int processID, out ProcessDetails process, String host = "localhost") { ProcessDetails[] processes = null; process = processes[0]; return(GetProcessesEx(host, null, null, out processes, processID)); }