#include <windows.h>
#include <tlhelp32.h>
#include <iostream.h>
#include <tlhelp32.h>
#include <iostream.h>
void main()
{
HANDLE hsnap=::CreateToolhelp32Snapshot
(TH32CS_SNAPPROCESS,0);
if(INVALID_HANDLE_VALUE==hsnap)
{
cout<<"sorry create snap of the processes failed!"<<endl;
return;
}
else
{
PROCESSENTRY32 pe;
pe.dwSize=sizeof(PROCESSENTRY32);
int b=::Process32First(hsnap,&pe);
while(b)
{
cout<<"进程名:"<<pe.szExeFile<<"
进程ID:"
<<pe.th32ProcessID<<endl;
b=::Process32Next(hsnap,&pe);
}
{
}
或者以下写法:
int main()
{
DWORD process_ids[1024 * 2];
HANDLE process[1024 * 2];
DWORD num_process;
TCHAR fileName[1024];
EnumProcesses(process_ids, sizeof(process_ids), &num_process);
for (int i = 0; i < num_process; i++)
{
process[i] = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, 0, process_ids[i]);
if (GetModuleFileNameExA(process[i], NULL, fileName, sizeof(fileName)))
cout << fileName << endl;
}
system("pause");
return 0;
}
{
DWORD process_ids[1024 * 2];
HANDLE process[1024 * 2];
DWORD num_process;
TCHAR fileName[1024];
EnumProcesses(process_ids, sizeof(process_ids), &num_process);
for (int i = 0; i < num_process; i++)
{
process[i] = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, 0, process_ids[i]);
if (GetModuleFileNameExA(process[i], NULL, fileName, sizeof(fileName)))
cout << fileName << endl;
}
system("pause");
return 0;
}