Good day. I set myself the task of writing a small organizer, which tracks how long certain windows work, and through tracking the running time of the process responsible for the window, made the following function (which is required for EnumWindows ):
BOOL CALLBACK EnumWindowsProc(HWND hWnd, long lParam) { wchar_t buff[255]; if ( IsWindowVisible(hWnd) ) { //PID -> Handle DWORD PID; GetWindowThreadProcessId( hWnd, &PID ); HANDLE pHandle = OpenProcess( PROCESS_QUERY_LIMITED_INFORMATION, FALSE, PID ); //Process Time in strange format FILETIME strangeTime, unuse1, unuse2, unuse3; GetProcessTimes( pHandle, &unuse1, &unuse2, &unuse3, &strangeTime ); //Process time in "normal" format SYSTEMTIME normalTime; FileTimeToSystemTime( &strangeTime, &normalTime ); InternalGetWindowText( hWnd, buff, 254 ); if( ((std::wstring)buff).size()>1 ) { printf("%ls %u:%u:%u\n", buff, normalTime.wHour, normalTime.wMinute, normalTime.wSecond); } } return TRUE; } Actually, EnumWindows is called every 5 seconds, but the time is the same every time.
UPD: There is a similar topic on the English-language stack: https://stackoverflow.com/questions/18107908/getprocesstimes-called-periodically-returns-the-same-results . But there is a slightly different situation (suddenly someone will push)
UPD: the problem is which process counts down, if you derive all the processes and count their time, then everything is fine, the code:
void PrintProcessNameAndID( DWORD processID ) { TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>"); //Just like char, but UNICODE (maybe)Оно // Get a handle to the process. HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, //Параметры доступа TRUE, //Наследование ID от открытого процесса processID ); //Открываем по ID FILETIME strangeTime, unuse1, unuse2, unuse3; GetProcessTimes( hProcess, &unuse1, &unuse2, &unuse3, &strangeTime ); SYSTEMTIME normalTime; FileTimeToSystemTime( &strangeTime, &normalTime ); // Get the process name. if (NULL != hProcess ) { HMODULE hMod; DWORD cbNeeded; if ( EnumProcessModulesEx( hProcess, //"указатель" на процесс &hMod, //Сюда мы кидаем всё, из чего состоит процесс (модули) sizeof(hMod), &cbNeeded, LIST_MODULES_ALL) ) //Общий объём модулей (с учётом которые не поместились) { GetModuleBaseName( hProcess, //"указатель" на процесс hMod, //рассматриваемый модуль szProcessName, //сюда кладём имя sizeof(szProcessName)/sizeof(TCHAR) ); //размер в символах } } // Print the process name and identifier. _tprintf( TEXT("%s (PID: %u) %u:%u:%u\n"), szProcessName, processID, normalTime.wHour, normalTime.wMinute, normalTime.wSecond ); // printf для TCHAR // Release the handle to the process. CloseHandle( hProcess ); } int main( void ) { double time_counter = 0; clock_t this_time = clock(); clock_t last_time = this_time; while(true) { this_time = clock(); time_counter += (double)(this_time - last_time); last_time = this_time; if(time_counter > (double)(NUM_SECONDS * CLOCKS_PER_SEC)) { // Get the list of process identifiers. time_counter -= (double)(NUM_SECONDS * CLOCKS_PER_SEC); DWORD aProcesses[1024], //PID cbNeeded, cProcesses; unsigned int i; if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) ) { return 1; } // Calculate how many process identifiers were returned. cProcesses = cbNeeded / sizeof(DWORD); // Print the name and process identifier for each process. for ( i = 0; i < cProcesses; i++ ) if( aProcesses[i] != 0 ) PrintProcessNameAndID( aProcesses[i] ); printf("\n"); } } return 0; } Now I check the PID and write the final problem.