The program can be implemented to open 3 processes, and monitor the number of processes in real time, until all the processes have exited, and then re-create 3 processes. So repeating
#include "stdafx.h"
#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"),//Create process
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 number of the new process:%d\n", pi.dwprocessid);
printf ("The new process's main thread ID number:%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/*=::P rocess32first (HPROCESSSNAP,&PE32) */;
while (num>=0)
{
num=0;
Hprocesssnap=::createtoolhelp32snapshot (th32cs_snapprocess,0);//repeatedly creating process snapshots
if (Hprocesssnap==invalid_handle_value)
{
printf ("Create process snapshot failed!\n");
return-1;
}
Bmore=::P Rocess32first (HPROCESSSNAP,&PE32);
while (bmore)
{
if (!_tcscmp (exe_name,pe32.szexefile))//constantly query whether the process you created is still running
{
num++;
printf ("Find a processManage.exe process num is%d\n", num);
}
Bmore=::P rocess32next (HPROCESSSNAP,&PE32);
}
if (num>0)
{
Sleep (1000);
}
Else
{
for (int i=0;i<3;i++)
{
OpenProcess ();
Sleep (3000);
}
}
}
return 0;
}
C + + detection process number, open a certain number of processes (iii)