Get the code of the process of the process.

xiaoxiao2021-03-05  28

Author: BCB_FANS

A few days ago, four famous names, four squares, visit all walks of heroes, fortunately, a master pointing, finally got it.

============================================================================================================================================================================================================= ==================

One sentence: Calculate the total time of a particular process and the total time of all processes in a specific period of time, their ratio is the specific process of CPU, where I use 1 second.

============================================================================================================================================================================================================= ==================

typedef struct _THREAD_INFO {LARGE_INTEGER CreateTime; DWORD dwUnknown1; DWORD dwStartAddress; DWORD StartEIP; DWORD dwOwnerPID; DWORD dwThreadId; DWORD dwCurrentPriority; DWORD dwBasePriority; DWORD dwContextSwitches; DWORD Unknown; DWORD WaitReason;

ThreadInfo, * pthreadinfo;

Typedef struct _unicode_string {ushort length; ushort maxlength; pwstr buffer;

Unicode_string;

typedef struct _PROCESS_INFO {DWORD dwOffset; DWORD dwThreadsCount; DWORD dwUnused1 [6]; LARGE_INTEGER CreateTime; LARGE_INTEGER UserTime; LARGE_INTEGER KernelTime; UNICODE_STRING ProcessName;

DWORD DWPROCESSID; DWORD DWPARETPROCESSID; DWORD DWHANDECOUNT; DWORD DWUNUSED3 [2];

DWORD dwVirtualBytesPeak; DWORD dwVirtualBytes; ULONG dwPageFaults; DWORD dwWorkingSetPeak; DWORD dwWorkingSet; DWORD dwQuotaPeakPagedPoolUsage; DWORD dwQuotaPagedPoolUsage; DWORD dwQuotaPeakNonPagedPoolUsage; DWORD dwQuotaNonPagedPoolUsage; DWORD dwPageFileUsage; DWORD dwPageFileUsagePeak; DWORD dCommitCharge; THREADINFO ThreadSysInfo [1];

PROCESSINFO, * PPROCESSINFO;

// Query a void __fastcall tform1 :: timer1timal (null) {Button2Click (null) {Button2Click (null)} // --------------------------------------------------------- -------------------------------------------------- -

void __fastcall TForm1 :: Button2Click (TObject * Sender) {PVOID pProcInfo = NULL; DWORD dwInfoSize = 0x20000; PPROCESSINFO pProcessInfo; DWORD dwWorkingSet; long (__stdcall * NtQuerySystemInformation) (DWORD, PVOID, DWORD, DWORD);

Static __int64 lasttotalprocesscpuusage = 0; static __int64 lastcurrentprocesscpuusage = 0;

Int CurrentDelta; Int TotalDelta;

__INT64 TOTALPROCPUUSAGE = 0; __INT64 CURRENTPROCPUUSAGE = 0;

/

PPROCICINFO = (pvoid) (New Byte [dwinfosize]);

NTQuerySystemInformation = (long (__ stdcall *) (DWORD, PVOID, DWORD, DWORD) GetProcadDress ("NTDLL.DLL"), "NTQuerySystemInformation");

NTQuerySystemInformation (5, PProcinfo, Dwinfosize, 0);

PPROCESSINFO = (pprocessinfo) PPROCICINFO;

Do {TotalProcessCPUUSAGE = (__INT64) PPROCESSINFO-> kernelTime.quadpart (__int64) PProcessInfo-> useertime.quadpart;

if (pProcessInfo-> dwProcessID == GetCurrentProcessId ()) {dwWorkingSet = pProcessInfo-> dwWorkingSet; CurrentProcessCPUUsage = (__int64) pProcessInfo-> KernelTime.QuadPart (__int64) pProcessInfo-> UserTime.QuadPart;}

/ If (pprocessinfo-> dwoffset == 0) {Break;} PPROCESSINFO = (PPROCESSINFO) (Byte *) PPRocessInfo PPRocessInfo-> dwoffset;} while (true);

TotalDelta = TotalProcesscpuusage - LasttotalProcessCPUusage; CurrentDelta = CURRENTPROCPUUSAGE - LastCurrentProcessCPUusage;

IF (TotalDelta! = 0) this-> caption = "CPU =" INTOSTR (100 * CurrentDelta / TotalDelta) "Memory =" INTOSTR (DWworkingSet / 1024) "K";

LasttotalProcessCPUusage; LastCurrentProcessCPUusage = CURRENTPROCPUUSAGE

DELETE [] PPROCICINFO;

转载请注明原文地址:https://www.9cbs.com/read-34288.html

New Post(0)