Click here to Skip to main content
13,766,082 members
Rate this:
 
Please Sign up or sign in to vote.
_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 16-Sep-18 1:02am
Updated 16-Sep-18 7:48am
v3
Comments
Richard Deeming 18-Sep-18 15:58pm
   
Sounds like the sort of thing a virus would do.

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
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.
0x01AA 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web04-2016 | 2.8.181114.1 | Last Updated 16 Sep 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100