| 476 | |
| 477 | template <class F> |
| 478 | void forEachRunningProcess(F&& f) |
| 479 | { |
| 480 | HandlePtr snapshot(CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)); |
| 481 | |
| 482 | if (snapshot.get() == INVALID_HANDLE_VALUE) { |
| 483 | const auto e = GetLastError(); |
| 484 | log::error("CreateToolhelp32Snapshot() failed, {}", formatSystemMessage(e)); |
| 485 | return; |
| 486 | } |
| 487 | |
| 488 | PROCESSENTRY32 entry = {}; |
| 489 | entry.dwSize = sizeof(entry); |
| 490 | |
| 491 | // first process, this shouldn't fail because there's at least one process |
| 492 | // running |
| 493 | if (!Process32First(snapshot.get(), &entry)) { |
| 494 | const auto e = GetLastError(); |
| 495 | log::error("Process32First() failed, {}", formatSystemMessage(e)); |
| 496 | return; |
| 497 | } |
| 498 | |
| 499 | for (;;) { |
| 500 | if (!f(entry)) { |
| 501 | break; |
| 502 | } |
| 503 | |
| 504 | // next process |
| 505 | if (!Process32Next(snapshot.get(), &entry)) { |
| 506 | const auto e = GetLastError(); |
| 507 | |
| 508 | // no more processes is not an error |
| 509 | if (e != ERROR_NO_MORE_FILES) |
| 510 | log::error("Process32Next() failed, {}", formatSystemMessage(e)); |
| 511 | |
| 512 | break; |
| 513 | } |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | std::vector<Process> getRunningProcesses() |
| 518 | { |
no test coverage detected