C++ retrieves a system process instance based on CreateToolhelp32Snapshot

  • 2020-04-02 02:48:30
  • OfStack

This article illustrates the C++ based on CreateToolhelp32Snapshot to obtain the system process. Share with you for your reference. Specific methods are as follows:


//Getwinprocess.cpp: defines the entry point for the console application.
//

#include "stdafx.h"
#include <Windows.h>
#include <TlHelp32.h>

int _tmain(int argc, _TCHAR* argv[])
{
 HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
 if (hProcessSnap == FALSE )
 {
 printf("CreateToolhelp32Snapshot error");
 return -1;
 }
 PROCESSENTRY32 pe32;
 pe32.dwSize = sizeof(PROCESSENTRY32);
 
 BOOL bRet = Process32First(hProcessSnap, &pe32);
 while (bRet)
 {
 printf("[process name]:%wsn", pe32.szExeFile);
 printf("[PID]:%dnn",pe32.th32ProcessID);
 bRet = Process32Next(hProcessSnap, &pe32);
 }
 ::CloseHandle(hProcessSnap); //Often forget this sentence
 return 0;
}

Hope that the article described in the C++ programming to help you.


Related articles: