Author: RedProgramer
The following is the code for obtaining the process of information, there is a selection window to choose the information you want to display. H file typef struct _tag_pdhcounterstruct {hcounter hcounter; // handle to the counter - Given to use by PDH Library Int NNEXTINDEX; // Element to get the next raw value int nOldestIndex; // element containing the oldes raw value int nRawCount; // number of elements containing raw values PDH_RAW_COUNTER a_RawValue [1024]; // Ring buffer to contain raw values} PDHCOUNTERSTRUCT, * PPDHCOUNTERSTRUCT;
PDH_STATUS __stdcall PDH_BrowseCallback (DWORD dwParam); BOOL PDH_AddCounter (LPTSTR szCounterName, int nItemIndex); void InitQuery (); extern PDH_BROWSE_DLG_CONFIG gpdhBrowseDlgConfig; extern PPDHCOUNTERSTRUCT MyCounter; extern HQUERY hQuery; extern LPTSTR gszReturnPath; extern DWORD gdwReturnPathSize; extern PACKAGE TForm1 * Form1; extern NcounterIndex;
.CPP file void initQuery () {PDH_STATUS PDHSTATUS = PDHOPENQUERY (NULL, 0, & HQuery); if (iSerrorSeverity (PDHSTATUS)) {Return;}
MyCounter = (PPDHCOUNTERSTRUCT) GlobalAlloc (GPTR, sizeof (PDHCOUNTERSTRUCT) * 1024); gdwReturnPathSize = 1024; gszReturnPath = (LPTSTR) LocalAlloc (LPTR, gdwReturnPathSize); nCounterIndex = 0;} int TForm1 :: GetProcessCount () {PDH_STATUS pdhStatus; PDH_FMT_COUNTERVALUE FMTVALUE; DWORD CTRTYPE; INT NRETCODE = 0;
PDHSTATUS = PDHCOLLECTQUERYDATA (HQuery);
// Get the current value of this counter for (int i = 0; i
}
int TForm1 :: GetTotalCommittedMemory (int ProcessID) {HANDLE hProcess; Pointer pAddr = 0; int dwTotalCommit = 0; int ret; MEMORY_BASIC_INFORMATION mi; hProcess = OpenProcess (PROCESS_QUERY_INFORMATION, 0, ProcessID); ret = VirtualQueryEx ((char *) hProcess, (char *) PADDR, & MI, SIZEOF (MI));
Do {paddr = (char.baseaddress) mi.regionSize; if (int) paddr> 0x7fffffff) Break; if (mi.state == MEM_COMMIT) {dwtotalcommit = mi.regionSize;} Ret = VirtualQueryex ((char *) HProcess, (char *) PADDR, & MI, SIZEOF (MI));} while (RET == SizeOf (MI)); CloseHandle (HProcess); Return DWTOTALCOMMIT / 1024;
Void __fastcall tform1 :: Timer1Timer (TOBJECT * Sender) {getProcessCount ();} // ------------------------------- -------------------------------------------- # include
PDH_STATUS __STDCALL PDH_BROWSECALLBACK (DWORD DWPARAM) {PDH_STATUS RETURNVALUE; LPTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTS
if (gpdhBrowseDlgConfig.CallBackStatus == PDH_MORE_DATA) {// Buffer too small for the counters selected // Free original buffer if (NULL = LocalFree (gszReturnPath)!) {return PDH_MEMORY_ALLOCATION_FAILURE;}
// Alloc new larger buffer gdwReturnPathSize = 1024; gszReturnPath = (LPTSTR) LocalAlloc (LPTR, gdwReturnPathSize); if (gszReturnPath == NULL) {return PDH_MEMORY_ALLOCATION_FAILURE;}
// set the config structure members for the new buffer gpdhbrowsedlgconfig.szreturnPathbuffer = gszreturnPath; gpdhbrowsedlgconfig.cchreturnpathLecth = GDWRETURNPATHSIZE
// retry the counter browse selection return pdh_retry;}
returnValue = ERROR_SUCCESS; szCurrentPath = gszReturnPath; // This string manipulation code is in a try / except block // to gracefully handle any problems // with pointer values, null termination, boundary conditions, etc. __try {while (TRUE) {nLen = lstrlen (szcurrentpath);
IF (nlen == 0) Break;
PDH_ADDCOUNTER (SZCurrentPath, ncounterIndex); NCOUNTERINDEX ; szcurrentpath = (Nlen 1);}} __except (Exception_execute_handler) {ReturnValue = PDH_INVALID_BUFFER;}
Return ReturnValue;
Void __fastcall tform1 :: button9click (TOBJECT * Sender) {
ZeromeMory (& gpdhbrowsedlgconfig, sizeof (gpdhbrowsedlgconfig);
GPDHBROWSEDLGCONFIG.HWNDOWNER = this-> Handle;
gpdhBrowseDlgConfig.bIncludeInstanceIndex = false; gpdhBrowseDlgConfig.bSingleCounterPerAdd = false; gpdhBrowseDlgConfig.bSingleCounterPerDialog = false; gpdhBrowseDlgConfig.bLocalCountersOnly = true; gpdhBrowseDlgConfig.bWildCardInstances = true; gpdhBrowseDlgConfig.bHideDetailBox = true; gpdhBrowseDlgConfig.bInitializePath = true; gpdhBrowseDlgConfig.szReturnPathBuffer = gszReturnPath; gpdhBrowseDlgConfig. CchreturnpathLength = gdwreturnpathsize; gpdhbrowsedlgconfig.pcallback = PDH_BROWSECALLBACK;
__try {PdhBrowseCounters (& gpdhBrowseDlgConfig);} __except (EXCEPTION_EXECUTE_HANDLER) {// UpdateStatus (TEXT ( "Exception in PdhBrowseCounters (Handled).")); // MessageBeep (0);}
} // ----------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------- Bool PDH_ADDCOUNTER (LPTSTSTSTSTSTSTSTSTERNAME, INT NITEMINDEX) {BOOL FRES = TRUE;
// allocate a pdhcounterstruct for this New Counter
// Add the counter to the list view control
__try {// Add the counter to the current query if (ERROR_SUCCESS == PdhAddCounter (hQuery, szCounterName, (DWORD) (& MyCounter [nItemIndex]), & (MyCounter [nItemIndex] .hCounter))) {TListItem * Item = Form1- > Listview2-> items-> add (); item-> caption = szcounteame; item-> subs-> add ("); fres = false;}} __except (eXception_execute_handler) {Fres = false;}
// if the add failed, Then Clean Up The List view and the counter structure
Return Fres;