Win32 API编程:显示系统进程列表

#include <windows.h>
#include <tlhelp32.h> // 声明快照函数的头文件
#include "tchar.h"
#include "stdio.h"
#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
	PROCESSENTRY32 pe32;
	// 在使用这个结构之前,先设置它的大小
	pe32.dwSize = sizeof(pe32); 
	
	// 给系统内的所有进程拍一个快照
	HANDLE hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
	if(hProcessSnap == INVALID_HANDLE_VALUE)
	{
		printf(" CreateToolhelp32Snapshot调用失败! 
");
		return -1;
	}
	
	// 遍历进程快照,轮流显示每个进程的信息
	BOOL bMore = ::Process32First(hProcessSnap, &pe32);
	while(bMore)
	{
		//printf(" 进程名称:%s 
", pe32.szExeFile);
		//CHAR tt[] = TEXT("haha");
		//printf(" 进程名称:%s 
", tt);
		//cout << pe32.szExeFile << endl; 
		//CHAR cFileName[9]= "ATUO.BAT";
		//printf("File name is %s
",cFileName);
		//wchar_t buf[100] = L"123";
		//wprintf(L"%ls
", buf);
		wprintf(L"Process Name is : %ls
", pe32.szExeFile);
		printf(" Process ID is:%u 

", pe32.th32ProcessID);

		bMore = ::Process32Next(hProcessSnap, &pe32);
	}

	// 不要忘记清除掉snapshot对象
	::CloseHandle(hProcessSnap);
	getchar();
	return 0;
}

  

原文地址:https://www.cnblogs.com/fancing/p/6488672.html