[C] Keep a process alive

This is a discussion on [C] Keep a process alive within the Windows Programming forums, part of the Platform Specific Boards category; Hi, I've done a little program that looks if another process is executed.. if it is not executed, it executes ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    161

    [C] Keep a process alive

    Hi, I've done a little program that looks if another process is executed.. if it is not executed, it executes the process. And it starts again ... looping

    it enumerates all running processes, then it checks if one of those is Limiter.exe, if so, it keeps looping, else it executes Limiter.exe and start again looping.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>
    #include "psapi.h"
    #include <string.h>
    
    int ProcessFinder (void);
    
    int main(void)
    {
    
      while( 1 ){  
        sleep(200);
        ProcessFinder();
      }// end while
      
      return 0;
    }
    
    
    /* --------- PROCESS FINDER --------- */
    int ProcessFinder (void){
      
      char szProcessName[MAX_PATH] = "BianConiglioRulez";
      DWORD aProcesses[1024], cbNeeded, cProcesses;
      unsigned int i;
      double tmp;
      char tempid[MAX_PATH];
      char pid[MAX_PATH];
      int  PidNumber = 0;
      
      if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
            return -1;
    
        cProcesses = cbNeeded / sizeof(DWORD);
        
        for ( i = 0; i < cProcesses; i++ ) // cerca tutti i processi
        {
          HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
                                         PROCESS_VM_READ,
                                         FALSE,
                                         aProcesses[i] );
          if (NULL != hProcess )
           {
              HMODULE hMod;
              DWORD cbNeeded;
    
              if ( EnumProcessModules( hProcess,
                                       &hMod,
                                       sizeof(hMod),
                                       &cbNeeded) )
              {
                  GetModuleBaseName( hProcess,
                                     hMod,
                                     szProcessName,
                                     sizeof(szProcessName));
              }
           }
        
           if (!strcmp(szProcessName, "Limiter.exe"))
            {
               //printf("found\n");
               return 0;
            }
          CloseHandle( hProcess );
        }// end FOR
        
        //printf("Not found.. executing\n");
        
        ShellExecute(NULL,
                       "open",
                       "C:\\Limiter\\Limiter.exe",
                       NULL,
                       NULL,
                       SW_SHOWNORMAL);
        
        return 0;
    }
    It seems to work fine but.. sometimes...it does not... if I kill Limiter.exe several times, it can happen that, it is not executed anymore after I killed it.. and my program (the keeper alive) keeps looping without execute Limiter.exe anymore..

    Can you find some "logic" bug in the program? I cant
    This forum is the best one I've ever seen. Great ppl, great coders

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    Maybe it's bacuse I just call CloseHandle( hProcess ); only if I find the process and I don't call it if it is not found?

    Yesterday in my tests everything was okey, I really don't know how to create the particular situation that crushes this program (really not a crush, it just loops without doing anything)..

    This forum is the best one I've ever seen. Great ppl, great coders

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Code:
    if (!strcmp(szProcessName, "Limiter.exe"))
    You still do this check even when you have a NULL handle, I think this is the source of your bug.
    Last edited by Quantum1024; 03-10-2005 at 04:36 AM.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    thanx for your help
    I will try to correct it, anyway, how can it be possible to have a NULL handle? I mean, when can it happen?

    And if I have a NULL handle, it will be different from "Limiter.exe" then it will execute it, it will not keep looping without executing anything, right?
    Last edited by BianConiglio; 03-10-2005 at 04:47 AM.
    This forum is the best one I've ever seen. Great ppl, great coders

  5. #5
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    how can it be possible to have a NULL handle
    Because that is windows method of denoting an error has occured, and you should do something about it. For example, if you get an error, call GetLastError() to get extended information of the error and report this to the user.

    when can it happen?
    When the OpenProcess call fails.
    Be a leader and not a follower.

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    thanx for your help

    I will correct my code and see if it will work
    This forum is the best one I've ever seen. Great ppl, great coders

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Quote Originally Posted by BianConiglio
    thanx for your help
    And if I have a NULL handle, it will be different from "Limiter.exe" then it will execute it, it will not keep looping without executing anything, right?
    szProcessName may or may not have been set to "limiter.exe" by a previous run thru the loop. Weather limiter.exe is executed when you have a NULL handle is indeterminate because it depends on what happened during the previous run(s) thru the loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. init adopts zombie process?
    By password636 in forum Linux Programming
    Replies: 4
    Last Post: 07-01-2009, 11:05 AM
  2. create a child process that creates a child process
    By cus in forum Linux Programming
    Replies: 9
    Last Post: 01-13-2009, 02:14 PM
  3. Replies: 3
    Last Post: 10-15-2008, 10:24 AM
  4. Problem with forking a process
    By Unitedroad in forum C Programming
    Replies: 10
    Last Post: 10-04-2007, 02:43 AM
  5. process programming
    By St0rM-MaN in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 08:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21