How to get the CPU Usage of other applications? Thanks

L

linuxfedora

Language: C++, tool: Borland C++ Builder 4.0

I have tried to search in the Internet, and found that i can use WMI,
here is an example from Internet:
private void timer1_Tick(object sender, System.EventArgs e)
{
ManagementObjectSearcher search = new
ManagementObjectSearcher("Select * from
Win32_PerfRawData_PerfOS_Processor where Name='0' ");
foreach(ManagementObject info in search.Get())
{
decimal PercentProcessorTime=0;
ulong u_newCPU = (ulong)info["PercentProcessorTime"];
ulong u_newNano = (ulong)info["TimeStamp_Sys100NS"];
decimal d_newCPU = Convert.ToDecimal(u_newCPU);
decimal d_newNano = Convert.ToDecimal(u_newNano);
decimal d_oldCPU = Convert.ToDecimal(u_oldCPU);
decimal d_oldNano = Convert.ToDecimal(u_oldNano);

PercentProcessorTime =
(1 - ((d_newCPU-d_oldCPU)/(d_newNano - d_oldNano)))*100m;

u_oldCPU = u_newCPU;
u_oldNano = u_newNano;

this.Text = PercentProcessorTime.ToString("N"); //ÏÔʾµ½´°Ìå±êÌâÀ¸
}
}

but i am writing Borland C++ Builder program, so i wrote:

GetWmiInfo(Memo1->Lines, "select * from
win32_PerfFormattedData_PerfProc_Process where name = 'explorer");

void GetWmiInfo(TStrings *lpList, WideString wsClass)
{

IWbemLocator *pWbemLocator = NULL;
if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL,
CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown,
(void**)&pWbemLocator) == S_OK)
{
IWbemServices *pWbemServices = NULL;
WideString wsNamespace = (L"root\\cimv2");
if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0,
NULL, NULL, &pWbemServices) == S_OK)
{
IEnumWbemClassObject *pEnumClassObject = NULL;
WideString wsWQL=L"WQL", wsQuery= wsClass;//
WideString(L"Select * from ")+wsClass;
if(pWbemServices->ExecQuery(wsWQL, wsQuery,
WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)
{
IWbemClassObject *pClassObject = NULL;
ULONG uCount = 1, uReturned;
if(pEnumClassObject->Reset() == S_OK)
{
int iEnumIdx = 0;
while(pEnumClassObject->Next(WBEM_INFINITE, uCount,
&pClassObject, &uReturned) == S_OK)
{
lpList->Add("---------------- ["+IntToStr(iEnumIdx)
+"] -----------------");

SAFEARRAY *pvNames = NULL;
if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS |
WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)
{
long vbl, vbu;
SafeArrayGetLBound(pvNames, 1, &vbl);
SafeArrayGetUBound(pvNames, 1, &vbu);
for(long idx=vbl; idx<=vbu; idx++)
{
long aidx = idx;
wchar_t *wsName = 0;
VARIANT vValue;
VariantInit(&vValue);
SafeArrayGetElement(pvNames, &aidx, &wsName);

BSTR bs = SysAllocString(wsName);
HRESULT hRes = pClassObject->Get(bs, 0,
&vValue, NULL, 0);
SysFreeString(bs);

if(hRes == S_OK)
{
AnsiString s;
Variant v = *(Variant*)&vValue;
if(v.IsArray())
{
for(int i=v.ArrayLowBound();
i<=v.ArrayHighBound(); i++)
{
Variant a = v.GetElement(i);
if(!s.IsEmpty())
s+=", ";
s+=VarToStr(a);
}
}
else
{
s = VarToStr(v);
}
lpList->Add(AnsiString(wsName)+"="+s);
}

VariantClear(&vValue);
SysFreeString(wsName);
}
}
if(pvNames)SafeArrayDestroy(pvNames);
iEnumIdx++;
}
}
if(pClassObject)pClassObject->Release();
}
if(pEnumClassObject)pEnumClassObject->Release();
}
if(pWbemServices)pWbemServices->Release();
}
if(pWbemLocator)pWbemLocator->Release();
}


But i found that the value TimeStamp_Sys100NS i get is NULL, so i
cannot make a calculation like the example one, can anyone tell me why?
Thanks!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top