本文實例講述了C++基于CreateToolhelp32Snapshot獲取系統進程的實現方法。分享給大家供大家參考。具體方法如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
// GetWinProcess.cpp : 定義控制臺應用程序的入口點。 // #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]:%ws\n" , pe32.szExeFile); printf ( "[PID]:%d\n\n" ,pe32.th32ProcessID); bRet = Process32Next(hProcessSnap, &pe32); } ::CloseHandle(hProcessSnap); // 經常忘記這句 return 0; } |
希望本文所述對大家的C++程序設計有所幫助。