MFC(C++) CPU, memory check question.

Asked 2 years ago, Updated 2 years ago, 153 views

Leave program number 1 (ex: Test program) running (just for CPU state, memory state change)

Run program number 2 (ex: check program)

Select the program you want to monitor as program number 1 and

The current time, CPU state, and memory state of program number 1 every 1 second

Display the value in the second program list control (View: Report)

You want to create a program that leaves a log with a *.txt file.

I can't feel it because I'm trying to implement it unemployed

Is this possible?

Please advise me how to code.

// ----------------------------------------------------------------------------------------------------------------------------------------------//

I have a few additional questions.

I've implemented almost everything, but there's a blockage in the CPU status check part, so I'm posting it.

double CCheckUtilDlg::getCPU()
{
    FILETIME ftime, fsys, fuser;
    ULARGE_INTEGER now, sys, user;
    double percent;

    GetSystemTimeAsFileTime(&ftime);
    memcpy(&now, &ftime, sizeof(FILETIME));

    GetProcessTimes(self, &ftime, &ftime, &fsys, &fuser);
    memcpy(&sys, &fsys, sizeof(FILETIME));
    memcpy(&user, &fuser, sizeof(FILETIME));

    **percent = (sys.QuadPart - lastSysCPU.QuadPart) + (user.QuadPart - lastUserCPU.QuadPart);**
    **percent /= (now.QuadPart - lastCPU.QuadPart);**
    **percent /= numProcessors;**

    lastCPU = now;
    lastUserCPU = user;
    lastSysCPU = sys;

    return percent * 100;
}

In the percent calculation part,

"warning C4244: '=': Data may be lost while converting from 'ULONGLONG' to 'double'. "

Whether the warning solution and percent calculation formula are correct

(The calculated value of the first line is almost 0, and the percent value of the zero is divided by

-1.#IND0000000000000000 This value is displayed.)

Finally,

I wonder if the calculated value in the formula is the average CPU value in red squares.

If not, I wonder how to read the average CPU value.

c c++ mfc cpu memory

2022-09-22 19:52

1 Answers

First of all, we need a way to get the CPU and memory status of a specific process.

Let's solve them one by one.

First, you can obtain CPU and memory states by using WinAPI and PDH. This part comes out right away if you search a little bit, so I won't write it down separately.

It would be helpful if you refer to the link below. Describes how to obtain the CPU/Memory state of the current process.

https://stackoverflow.com/questions/63166/how-to-determine-cpu-and-memory-consumption-from-inside-a-process

What we want is the status information of a specific process, so we need to know the process handle of program 1 instead of the current process handle obtained by GetCurrentProcess() to get the status information with the API in the link above.

If you search for this part, there are many ways to get the process handle you want by referring to the link below.

http://ddiggam.tistory.com/59

Based on the above, you can now perform a loop cycle to obtain CPU/Memory status and record it in listview and file.


2022-09-22 19:52

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.