Getting WMI properties from Win32_PerfFormattedData_PerfProc_Process class when searching by IDProce

  • Thread starter Thread starter Christopher Attard
  • Start date Start date
C

Christopher Attard

Hi,
I'm trying to run this function which attempts to get 2 WMI properties from the specific WMI class for a process with a specified IDProcess. It's giving an "Invalid Object Path" exception. The function works when I search by process name (in this case WMI path should be: "Win32_PerfFormattedData_PerfProc_Process.Name='" +sProcessName +"'" ) but I need to search by IDProcess

public static void GetCpuUsage(string sIDProcess)

try

{

double d1,d2,n1,n2;

ManagementObject counter = new ManagementObject("Win32_PerfFormattedData_PerfProc_Process.IDProcess=" +sIDProcess);

n1 = Convert.ToDouble(counter["PercentProcessorTime"]);

d1 = Convert.ToDouble(counter["TimeStamp_Sys100NS"]);


}

catch(Exception ex)

{

Console.WriteLine(ex.Message);

}

Console.ReadLine();

}


Any suggestions about what might be wrong in this code? Thanks in advance.

Chris
 
Hi,

public static void GetCpuUsage(string sIDProcess)

{

try

{

double d1,d2,n1,n2;

ManagementObjectSearcher searcher =

new ManagementObjectSearcher("select * from Win32_PerfFormattedData_PerfProc_Process");


foreach(ManagementObject buf in searcher.Get())

if(buf["IDProcess"].ToString() == sIDProcess)

{

n1 = Convert.ToDouble(buf["PercentProcessorTime"]);

d1 = Convert.ToDouble(buf["TimeStamp_Sys100NS"]);

Console.WriteLine("IDProcess: " + buf["IDProcess"] +

" ProcTime: " + n1 + " TimeStamp: " + d1);

}



}

catch(Exception ex)

{

Console.WriteLine(ex.Message);

}

Console.ReadLine();

}

I don't say if this is the best method but I do so :)

bye :)



Hi,
I'm trying to run this function which attempts to get 2 WMI properties from the specific WMI class for a process with a specified IDProcess. It's giving an "Invalid Object Path" exception. The function works when I search by process name (in this case WMI path should be: "Win32_PerfFormattedData_PerfProc_Process.Name='" +sProcessName +"'" ) but I need to search by IDProcess

public static void GetCpuUsage(string sIDProcess)

try

{

double d1,d2,n1,n2;

ManagementObject counter = new ManagementObject("Win32_PerfFormattedData_PerfProc_Process.IDProcess=" +sIDProcess);

n1 = Convert.ToDouble(counter["PercentProcessorTime"]);

d1 = Convert.ToDouble(counter["TimeStamp_Sys100NS"]);


}

catch(Exception ex)

{

Console.WriteLine(ex.Message);

}

Console.ReadLine();

}


Any suggestions about what might be wrong in this code? Thanks in advance.

Chris
 
IDProcess is not a key attribute so you can't use it to create an instance of the class.
You should issue a select query using the IDProcess in the where clause like this:

select PercentProcessorTime, TimeStamp_Sys100NS from Win32_PerfFormattedData_PerfProc_Process where IDProcess = nnn

where nnn is the pid.

Willy.

Hi,
I'm trying to run this function which attempts to get 2 WMI properties from the specific WMI class for a process with a specified IDProcess. It's giving an "Invalid Object Path" exception. The function works when I search by process name (in this case WMI path should be: "Win32_PerfFormattedData_PerfProc_Process.Name='" +sProcessName +"'" ) but I need to search by IDProcess

public static void GetCpuUsage(string sIDProcess)

try

{

double d1,d2,n1,n2;

ManagementObject counter = new ManagementObject("Win32_PerfFormattedData_PerfProc_Process.IDProcess=" +sIDProcess);

n1 = Convert.ToDouble(counter["PercentProcessorTime"]);

d1 = Convert.ToDouble(counter["TimeStamp_Sys100NS"]);


}

catch(Exception ex)

{

Console.WriteLine(ex.Message);

}

Console.ReadLine();

}


Any suggestions about what might be wrong in this code? Thanks in advance.

Chris
 
Ok, problem solved. 10x to both of you.

Chris
Hi,
I'm trying to run this function which attempts to get 2 WMI properties from the specific WMI class for a process with a specified IDProcess. It's giving an "Invalid Object Path" exception. The function works when I search by process name (in this case WMI path should be: "Win32_PerfFormattedData_PerfProc_Process.Name='" +sProcessName +"'" ) but I need to search by IDProcess

public static void GetCpuUsage(string sIDProcess)

try

{

double d1,d2,n1,n2;

ManagementObject counter = new ManagementObject("Win32_PerfFormattedData_PerfProc_Process.IDProcess=" +sIDProcess);

n1 = Convert.ToDouble(counter["PercentProcessorTime"]);

d1 = Convert.ToDouble(counter["TimeStamp_Sys100NS"]);


}

catch(Exception ex)

{

Console.WriteLine(ex.Message);

}

Console.ReadLine();

}


Any suggestions about what might be wrong in this code? Thanks in advance.

Chris
 
Back
Top