seyed_farid
دوشنبه 19 آذر 1403, 15:34 عصر
با سلام
برای دریافت CPU Usage برنامه ها در ویندوز 11 64 بیت دستور چیه؟
من با دستورات زیر در ویندوز 10 جواب گرفتم ولی الان برنامه رو رو ویندوز 11 اجرا که میکنم، مقدار درست را نمیدهد و با تسک منیجر ویندوز خیلی فرق میکنه.
function FindProcessByModuleNameNT(Name: string): THandle;
var
PIDList: array[0..1023] of THandle;
i: integer;
ListCount, size: cardinal;
hProcess, hModule: THandle;
ModuleName: array[0..MAX_PATH] of char;
begin
result:= 0;
EnumProcesses(@PIDList, sizeof(PIDList), ListCount);
ListCount:= ListCount div sizeof(THandle);
for i:= 0 to ListCount-1 do
begin
// get process handle for each process id we got
hProcess:= OpenProcess(PROCESS_QUERY_INFORMATION +
PROCESS_VM_READ, false, PIDList[i]);
try
if hProcess <> 0 then
begin
// get handle to first module, which is the exe
if EnumProcessModules(hProcess, @hModule,
sizeof(hModule), size) then
begin
// extract the filename
GetModuleFileNameEx(hProcess, hModule,
ModuleName, sizeof(ModuleName));
if AnsiCompareText(ExtractFileName(StrPas(@ModuleName )),Name) = 0 then
begin
result:= PIDList[i];
exit;
end;
end;
end;
finally
CloseHandle(hProcess);
end;
end;
end;
//
A function that returns CPU usage (in percent) for a given process id
function GetCpuUsage(PID:cardinal):single;
const
cWaitTime = 1000;// 750;
var
h: Cardinal;
mCreationTime,mExitTime,mKernelTime, mUserTime: _FILETIME;
TotalTime1,TotalTime2: Int64;
SysInfo : _SYSTEM_INFO;
CpuCount: Word;
begin
//We need to know the number of CPUs and divide the result by that, otherwise we will get wrong percentage on multi-core systems
GetSystemInfo(SysInfo);
CpuCount := SysInfo.dwNumberOfProcessors;
//f add............................................... .......
form1.Label12.Caption := 'CPU Count: '+ IntToStr(CpuCount);
//f add............................................... .......
//We need to get a handle of the process with PROCESS_QUERY_INFORMATION privileges.
h := OpenProcess(PROCESS_QUERY_INFORMATION,false,PID);
//We can use the GetProcessTimes() function to get the amount of time the process has spent in kernel mode and user mode.
GetProcessTimes(h,mCreationTime,mExitTime,mKernelT ime,mUserTime);
TotalTime1:=int64(mKernelTime.dwLowDateTime or (mKernelTime.dwHighDateTime shr 32))
+ int64(mUserTime.dwLowDateTime or (mUserTime.dwHighDateTime shr 32));
//Wait a little
Sleep(cWaitTime);
GetProcessTimes(h,mCreationTime,mExitTime,mKernelT ime,mUserTime);
TotalTime2 := int64(mKernelTime.dwLowDateTime or (mKernelTime.dwHighDateTime shr 32))+
int64(mUserTime.dwLowDateTime or (mUserTime.dwHighDateTime shr 32));
//This should work out nicely, as there were approx. 250 ms between the calls
//and the result will be a percentage between 0 and 100
Result:=((TotalTime2-TotalTime1)/cWaitTime)/100 /CpuCount;
Result := Round(10* Result) / 10;
// result := StrToFloat(FormatFloat('0.0' , Result));
CloseHandle(h);
end;
...................... دستور استفاده .................................................
CPU := GetCpuUsage(FindProcessByModuleNameNT(ss));
.................................................. .................................................. .................
دوستان میدونید مشکل چی هست ؟
ممنون
برای دریافت CPU Usage برنامه ها در ویندوز 11 64 بیت دستور چیه؟
من با دستورات زیر در ویندوز 10 جواب گرفتم ولی الان برنامه رو رو ویندوز 11 اجرا که میکنم، مقدار درست را نمیدهد و با تسک منیجر ویندوز خیلی فرق میکنه.
function FindProcessByModuleNameNT(Name: string): THandle;
var
PIDList: array[0..1023] of THandle;
i: integer;
ListCount, size: cardinal;
hProcess, hModule: THandle;
ModuleName: array[0..MAX_PATH] of char;
begin
result:= 0;
EnumProcesses(@PIDList, sizeof(PIDList), ListCount);
ListCount:= ListCount div sizeof(THandle);
for i:= 0 to ListCount-1 do
begin
// get process handle for each process id we got
hProcess:= OpenProcess(PROCESS_QUERY_INFORMATION +
PROCESS_VM_READ, false, PIDList[i]);
try
if hProcess <> 0 then
begin
// get handle to first module, which is the exe
if EnumProcessModules(hProcess, @hModule,
sizeof(hModule), size) then
begin
// extract the filename
GetModuleFileNameEx(hProcess, hModule,
ModuleName, sizeof(ModuleName));
if AnsiCompareText(ExtractFileName(StrPas(@ModuleName )),Name) = 0 then
begin
result:= PIDList[i];
exit;
end;
end;
end;
finally
CloseHandle(hProcess);
end;
end;
end;
//
A function that returns CPU usage (in percent) for a given process id
function GetCpuUsage(PID:cardinal):single;
const
cWaitTime = 1000;// 750;
var
h: Cardinal;
mCreationTime,mExitTime,mKernelTime, mUserTime: _FILETIME;
TotalTime1,TotalTime2: Int64;
SysInfo : _SYSTEM_INFO;
CpuCount: Word;
begin
//We need to know the number of CPUs and divide the result by that, otherwise we will get wrong percentage on multi-core systems
GetSystemInfo(SysInfo);
CpuCount := SysInfo.dwNumberOfProcessors;
//f add............................................... .......
form1.Label12.Caption := 'CPU Count: '+ IntToStr(CpuCount);
//f add............................................... .......
//We need to get a handle of the process with PROCESS_QUERY_INFORMATION privileges.
h := OpenProcess(PROCESS_QUERY_INFORMATION,false,PID);
//We can use the GetProcessTimes() function to get the amount of time the process has spent in kernel mode and user mode.
GetProcessTimes(h,mCreationTime,mExitTime,mKernelT ime,mUserTime);
TotalTime1:=int64(mKernelTime.dwLowDateTime or (mKernelTime.dwHighDateTime shr 32))
+ int64(mUserTime.dwLowDateTime or (mUserTime.dwHighDateTime shr 32));
//Wait a little
Sleep(cWaitTime);
GetProcessTimes(h,mCreationTime,mExitTime,mKernelT ime,mUserTime);
TotalTime2 := int64(mKernelTime.dwLowDateTime or (mKernelTime.dwHighDateTime shr 32))+
int64(mUserTime.dwLowDateTime or (mUserTime.dwHighDateTime shr 32));
//This should work out nicely, as there were approx. 250 ms between the calls
//and the result will be a percentage between 0 and 100
Result:=((TotalTime2-TotalTime1)/cWaitTime)/100 /CpuCount;
Result := Round(10* Result) / 10;
// result := StrToFloat(FormatFloat('0.0' , Result));
CloseHandle(h);
end;
...................... دستور استفاده .................................................
CPU := GetCpuUsage(FindProcessByModuleNameNT(ss));
.................................................. .................................................. .................
دوستان میدونید مشکل چی هست ؟
ممنون