Click here to Skip to main content
15,888,590 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
C++
_WIN32_WINNT 0x0500 // define needed to use GetConsoleWindow() function
#include <windows.h>
#include <tlhelp32.h>
#include <string.h>
 
int main()
{    
    BOOL is_running = 0;
    DWORD pid = 0;
    PROCESSENTRY32 pe32;
    pe32.dwSize = sizeof(PROCESSENTRY32);
 
    //start of the checking loop, check happens once in every 10 seconds
    while(1){ 
        //take a snapshot of windows running processes
        HANDLE hProcessSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
        Process32First(hProcessSnapshot, &pe32);
 
        //compare the name of each process in the process list with "sleep.exe"
        while (Process32Next(hProcessSnapshot, &pe32)) {
            if (!strcmp("1.exe", pe32.szExeFile)) {
                is_running = 1; //change is_running status if name matches
                break;
            }
        }
 
        if(!is_running) system("start 1.exe"); //start the process if it's not running
        is_running = 0; //restore is_running status
        Sleep(10000); //sleep for 10 seconds
       
        while (Process32Next(hProcessSnapshot, &pe32)) {
            if (!strcmp("2.exe", pe32.szExeFile)) {
                is_running = 1; //change is_running status if name matches
                break;
            }
        }
 
        if(!is_running) system("start 2.exe"); //start the process if it's not running
        is_running = 0; //restore is_running status
        Sleep(10000); //sleep for 10 seconds        
    }
 
    return 0;
}


What I have tried:

want to simplify my question
Posted
Updated 16-Sep-18 6:48am
v3
Comments
Richard Deeming 18-Sep-18 15:58pm    
Sounds like the sort of thing a virus would do.

1 solution

C++
while(1){
        HANDLE hProcessSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
// ...
        HANDLE hProcessSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);

You are trying to redefine the same variable. Remove the type (HANDLE) from the second occurrence. Or if you want two such handles then rename one of them.
 
Share this answer
 
Comments
rohit7373 16-Sep-18 11:06am    
Tried To modify or correct it but still not working as I want. I need SLEEP.exe,notpad.exe to be checked every 10 seconds whether it is running or not and if not running then run it.
[no name] 16-Sep-18 11:27am    
a.) Typo notpad vs. notepad
if(!is_running) system("start notpad.exe");
b.) Caps vs. Non Caps
strcmp("SLEEP.exe", pe32.szExeFile....
I suggest to make the comparison _not_ case sensitive.
Richard MacCutchan 16-Sep-18 13:02pm    
And what happens when you try? We cannot guess what is not working.
KarstenK 16-Sep-18 12:49pm    
you better use a full path in the start command.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900