一下程序可以实现打开3个进程,并且实时的监控进程数量,等到所有的进程都退出后,再重新创建3个进程。如此循环往复
#include <iostream>
#include <windows.h>
#include <stdlib.h>
#include <tlhelp32.h>
#include "Psapi.h"
#pragma comment(lib,"Kernel32.lib")
using namespace std;
void openProcess()
{
STARTUPINFO si;
memset(&si,0,sizeof(STARTUPINFO));
si.cb=sizeof(STARTUPINFO);
si.dwFlags=STARTF_USESHOWWINDOW;
si.wShowWindow=SW_SHOW;
PROCESS_INFORMATION pi;
if(!CreateProcess(TEXT("D:\\abc\\abc.exe"),//创建进程
NULL,NULL,NULL,FALSE,CREATE_NEW_CONSOLE,NULL,NULL,&si,&pi))
{cout<<"create failed"<<endl;
}
else
cout<<"create a process successfully!"<<endl;
Sleep(300);
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
printf("新进程的ID号:%d\n",pi.dwProcessId);
printf("新进程的主线程ID号:%d\n",pi.dwThreadId);
}
int _tmain(int argc, TCHAR* argv[])
{
for(int i=0;i<3;i++)
{
openProcess();
Sleep(3000);
}
int num=0;
TCHAR exe_name[256]=_T("abc.exe");
PROCESSENTRY32 pe32;
HANDLE HprocessSnap/*=::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0)*/;
pe32.dwSize=sizeof(pe32);
BOOL bMore/*=::Process32First(HprocessSnap,&pe32)*/;
while(num>=0)
{
num=0;
HprocessSnap=::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);//不断重复创建进程快照
if(HprocessSnap==INVALID_HANDLE_VALUE)
{
printf("create 进程快照失败!\n");
return -1;
}
bMore=::Process32First(HprocessSnap,&pe32);
while(bMore)
{
if(!_tcscmp(exe_name,pe32.szExeFile))//不停的查询自己创建的进程是否还在运行
{
num++;
printf("find a processManage.exe process num is %d\n",num);
}
bMore=::Process32Next(HprocessSnap,&pe32);
}
if(num>0)
{
Sleep(1000);
}
else
{
for(int i=0;i<3;i++)
{
openProcess();
Sleep(3000);
}
}
}
return 0;
}