L
L
Lnkova2020-12-08 14:09:59
C++ / C#
Lnkova, 2020-12-08 14:09:59

Why is task manager not scanning?

I am writing a task manager using winapi. Its task is to show running processes and their number, stop and resume the process and change the priority. But it doesn't do 1 task. What to do, tell me?

#include "pch.h"

#include <windows.h>

#include <tlhelp32.h>

#include <iostream>

#include <Psapi.h>

#include <string>

using namespace std;

DWORD Priority(int x) {

  switch (x) {

  case 0:

    return IDLE_PRIORITY_CLASS;

    break;

  case 1:

    return BELOW_NORMAL_PRIORITY_CLASS;

    break;

  case 2:

    return NORMAL_PRIORITY_CLASS;

    break;

  case 3:

    return ABOVE_NORMAL_PRIORITY_CLASS;

    break;

  case 4:

    return HIGH_PRIORITY_CLASS;

    break;

  case 5:

    return REALTIME_PRIORITY_CLASS;

    break;

  }

}

int main()

{

  string decision;

  PROCESS_MEMORY_COUNTERS memCounter;

  HANDLE hSnap, process[200];

  int size, prior_dec, proc_num;

  int i = 0;

  hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

  PROCESSENTRY32 proc;

  while (true) {

    cout << "What to do? (scan/change/stop)" << endl;

    cin >> decision;

    if (decision == "scan") {

      if (Process32First(hSnap, &proc)) {

        do {

          process[i] = OpenProcess(PROCESS_ALL_ACCESS, false, proc.th32ProcessID);

          GetProcessMemoryInfo(process[i], &memCounter, sizeof(memCounter));

          cout << "#" << i << " " << proc.szExeFile << " " << memCounter.WorkingSetSize / 8 << " KB" << endl;

          i++;

        } while (Process32Next(hSnap, &proc));

      }

    }

    if (decision == "change") {

      cout << "Process number?" << endl;

      cin >> proc_num;
      cout << "Priority?(0-5)" << endl;

      cin >> prior_dec;

      SetPriorityClass(process[proc_num], Priority(prior_dec));

    }

  }

  system("pause");
  return 0;

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2020-12-08
@jcmvbkbc

process[i] = OpenProcess(PROCESS_ALL_ACCESS, false, proc.th32ProcessID);

A normal user will only be able to open their own processes with PROCESS_ALL_ACCESS access. For all other processes, this call will fail.
To call GetProcessMemoryInfo PROCESS_ALL_ACCESS is not needed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question