列举进程和模块
今天下午写的,关于列举出系统内所有运行的进程信息和模块信息,还不完全,有待加工
1BOOL GetProcessModule(DWORD dwPID,DWORD dwModuleID,LPMODULEENTRY32 lpMe32,DWORD cbMe32)
2{
3 BOOL bRet = FALSE;
4 BOOL bFound = FALSE;
5 HANDLE hModuleSnap = NULL;
6 MODULEENTRY32 me32 ={0};
7
8 hModuleSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPMODULE,dwPID);
9 if(hModuleSnap == INVALID_HANDLE_VALUE)
10 return FALSE;
11
12 me32.dwSize = sizeof(MODULEENTRY32);
13 if(::Module32First(hModuleSnap,&me32))
14 {
15 do
16 {
17 //cout<<me32.szExePath<<"\t\t\t"<<me32.th32ProcessID<<endl;
18 if(me32.th32ProcessID==dwPID)
19 {
20 CopyMemory(lpMe32,&me32,cbMe32);
21 bFound = TRUE;
22 }
23 if(bFound==TRUE)
24 break;
25 }
26 while(::Module32Next(hModuleSnap,&me32));
27 }
28
29 CloseHandle(hModuleSnap);
30 return bFound;
31}
32
33
34BOOL GetProcessList()
35{
36 HANDLE m_hProcessSnap = NULL;
37 BOOL bRet = FALSE;
38 PROCESSENTRY32 pe32 = {0};
39
40 m_hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
41
42 if(m_hProcessSnap==INVALID_HANDLE_VALUE)
43 return FALSE;
44
45 //Fill in the size of the structure
46 pe32.dwSize = sizeof(PROCESSENTRY32);
47
48 if(::Process32First(m_hProcessSnap,&pe32))
49 {
50 DWORD dwPriorityClass;
51 BOOL bGotModules = FALSE;
52 MODULEENTRY32 me32 = {0};
53 do
54 {
55 //cout<<pe32.th32ProcessID<<"\t"<<endl;
56 bGotModules = GetProcessModule(pe32.th32ProcessID,pe32.th32ModuleID,&me32,sizeof(MODULEENTRY32));
57 if(bGotModules)
58 {
59 HANDLE hProcess;
60 hProcess = OpenProcess(PROCESS_ALL_ACCESS,FALSE,pe32.th32ProcessID);
61 dwPriorityClass = ::GetPriorityClass(hProcess); //Get the priority of the process
62 CloseHandle(hProcess);
63
64 printf("\nPriority Class:\t%d",pe32.pcPriClassBase);
65 printf("\nThreadCount:\t%d",pe32.cntThreads);
66 printf("\nProcessId:\t%d",pe32.th32ProcessID);
67 printf("\nModuleId:\t%d",me32.th32ModuleID);
68 printf("\nExePath:\t%s",me32.szExePath);
69 printf("\nModuleName:\t%s",me32.szModule);
70 printf("\n---------------------------------------------------");
71 bRet = TRUE;
72 }
73 }while(::Process32Next(m_hProcessSnap,&pe32));
74 //bRet = TRUE;
75 }
76
77 CloseHandle(m_hProcessSnap);
78 return bRet;
79}
2{
3 BOOL bRet = FALSE;
4 BOOL bFound = FALSE;
5 HANDLE hModuleSnap = NULL;
6 MODULEENTRY32 me32 ={0};
7
8 hModuleSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPMODULE,dwPID);
9 if(hModuleSnap == INVALID_HANDLE_VALUE)
10 return FALSE;
11
12 me32.dwSize = sizeof(MODULEENTRY32);
13 if(::Module32First(hModuleSnap,&me32))
14 {
15 do
16 {
17 //cout<<me32.szExePath<<"\t\t\t"<<me32.th32ProcessID<<endl;
18 if(me32.th32ProcessID==dwPID)
19 {
20 CopyMemory(lpMe32,&me32,cbMe32);
21 bFound = TRUE;
22 }
23 if(bFound==TRUE)
24 break;
25 }
26 while(::Module32Next(hModuleSnap,&me32));
27 }
28
29 CloseHandle(hModuleSnap);
30 return bFound;
31}
32
33
34BOOL GetProcessList()
35{
36 HANDLE m_hProcessSnap = NULL;
37 BOOL bRet = FALSE;
38 PROCESSENTRY32 pe32 = {0};
39
40 m_hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
41
42 if(m_hProcessSnap==INVALID_HANDLE_VALUE)
43 return FALSE;
44
45 //Fill in the size of the structure
46 pe32.dwSize = sizeof(PROCESSENTRY32);
47
48 if(::Process32First(m_hProcessSnap,&pe32))
49 {
50 DWORD dwPriorityClass;
51 BOOL bGotModules = FALSE;
52 MODULEENTRY32 me32 = {0};
53 do
54 {
55 //cout<<pe32.th32ProcessID<<"\t"<<endl;
56 bGotModules = GetProcessModule(pe32.th32ProcessID,pe32.th32ModuleID,&me32,sizeof(MODULEENTRY32));
57 if(bGotModules)
58 {
59 HANDLE hProcess;
60 hProcess = OpenProcess(PROCESS_ALL_ACCESS,FALSE,pe32.th32ProcessID);
61 dwPriorityClass = ::GetPriorityClass(hProcess); //Get the priority of the process
62 CloseHandle(hProcess);
63
64 printf("\nPriority Class:\t%d",pe32.pcPriClassBase);
65 printf("\nThreadCount:\t%d",pe32.cntThreads);
66 printf("\nProcessId:\t%d",pe32.th32ProcessID);
67 printf("\nModuleId:\t%d",me32.th32ModuleID);
68 printf("\nExePath:\t%s",me32.szExePath);
69 printf("\nModuleName:\t%s",me32.szModule);
70 printf("\n---------------------------------------------------");
71 bRet = TRUE;
72 }
73 }while(::Process32Next(m_hProcessSnap,&pe32));
74 //bRet = TRUE;
75 }
76
77 CloseHandle(m_hProcessSnap);
78 return bRet;
79}