win10打印所有进程

#include <map>
#include <iostream>
#include <string>

#include <windows.h>
#include <TlHelp32.h> // Never include win32 heads before window.h


bool printAllProcess(std::map<std::wstring, int> &_mapProcess, bool is_print = true)
{
	PROCESSENTRY32 pe32;
	pe32.dwSize = sizeof(pe32);

	HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
	if (hProcessSnap == INVALID_HANDLE_VALUE) {
		std::cout << "CreateToolhelp32Snapshot Error!" << std::endl;;
		return false;
	}

	BOOL bResult = Process32First(hProcessSnap, &pe32);

	int num(0);

	while (bResult)
	{
		std::wstring name = pe32.szExeFile;
		int id = pe32.th32ProcessID;
		if (is_print)
		{
			std::cout << "[" << ++num << "]: " << "--ProcessID:" << id;
			std::wcout << "--Process Name:" << name << std::endl;
		}
		_mapProcess.insert(std::pair<std::wstring, int>(name, id)); 
		bResult = Process32Next(hProcessSnap, &pe32);
	}

	CloseHandle(hProcessSnap);
	return true;
}

  

原文地址:https://www.cnblogs.com/alexYuin/p/9697636.html