A
A
Arthur2020-12-29 18:27:56
C++ / C#
Arthur, 2020-12-29 18:27:56

How to write a process to freeze and unfreeze?

I wrote this code:

#include <Windows.h>
#include <TlHelp32.h>
#include <iostream>

using namespace std;

bool KillProcByPid(DWORD pid) {  // Метод убийства процесса
  DWORD ExitCode;
  HANDLE hp;
  bool ret = true;

  if (pid)
  {
    hp = OpenProcess(PROCESS_ALL_ACCESS, true, pid);
    if (hp)
    {
      GetExitCodeProcess(hp, &ExitCode);
      ret = TerminateProcess(hp, ExitCode);
    }
    else
    {
      return false;
    }
  }
  else
  {
    return false;
  }

  CloseHandle(hp);
  return ret;
}

int GetProcesByName(wstring name) { // Метод получения PID

  HANDLE snapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  PROCESSENTRY32 pInfo = { 0 }; pInfo.dwSize = sizeof(PROCESSENTRY32);

  while (Process32Next(snapShot, &pInfo))
  {
    if (pInfo.szExeFile == name)
    {
      CloseHandle(snapShot);
      return pInfo.th32ProcessID;
    }
  }
  CloseHandle(snapShot);
  return 0;
}


int main() {

  int pID = GetProcesByName(L"Name.exe"); //Название вашего процесса, по которому ищется PID
  cout << pID << "\n";  // Вписываем в консоль его PID
  KillProcByPid(pID);   // Убиваем процесс по PID
  return 0;
}

This opening program just kills the process, but I want to freeze it and unfreeze it when needed.
I need to understand how the SuspendThread / ResumeThread methods work with an example.
And in general, is it possible to freeze the main thread knowing the PID? If you need to receive a handle, please tell me how it's done.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Ananiev, 2020-12-29
@69Artist

https://stackoverflow.com/questions/11010165/how-t...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question