473,405 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

How to get the CPU Usage of other applications? Thanks

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_WbemAdministrativeLocato r, 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!!
Jul 15 '08 #1
1 2940
li*********@yahoo.com.hk wrote:
>
[entire post redacted]

CPU Utilization is not defined by ISO/IEC 14882:2003. Neither is
"other applications".

Your question is highly platform specific. Please ask in a newsgroup
dedicated to your platform (try something with "ms" or "windows" in its
name).

For some suggested newsgroups, please see FAQ 5.9 --

http://www.parashift.com/c++-faq-lit...t.html#faq-5.9

Thank you
Jul 15 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: rbt | last post by:
Would a Python process consume more memory on a PC with lots of memory? For example, say I have the same Python script running on two WinXP computers that both have Python 2.4.0. One computer has...
7
by: Falnes | last post by:
We are developing applications in VB.Net which might be very CPU intensive. Our problem is that other applications running on the same machine is barely getting any processor resources at all. This...
12
by: Fabian Knopf | last post by:
Hi @ll, my IBM DB2 server runs with 60% Cpu Usage but no task has to be done. Why this ??? Cu, Fabian
20
by: Philip Carnstam | last post by:
How come .Net applications use so much memory? Every application I compile uses at least 10 MB of memory, even the ones consisting of only a form and nothing else. If I minimize them though the...
11
by: Paulo Eduardo | last post by:
Hi, All! We are developing one app for windows 95/98/Me/NT4.0/2000/XP/2003 using Visual C++ 6.0. We need to set the % of CPU Usage to app process. Is there an API to set % of CPU Usage? Can...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.