home *** CD-ROM | disk | FTP | other *** search
/ Mega Top 1 / os2_top1.zip / os2_top1 / APPS / PROG / C_PLUS / CNVLIB2 / KILLPROC.CMD < prev    next >
Encoding:
Text File  |  1993-06-14  |  2.7 KB  |  72 lines

  1. EXTPROC CEnvi
  2. /***************************************************************************
  3.  *** KillProc.cmd - CEnvi program to kill a running process by reference ***
  4.  ***                to its ID, partial name, or full name.               ***
  5.  ***************************************************************************/
  6.  
  7. #define  NO_ERROR          0     // return code from most successful DosCalls
  8.  
  9. DosKillProcess(ActionCode,ProcessID)
  10.    // kill specified ProcessID using action code DKP_PRECESSTREE or DKP_PROCESS
  11. {
  12.    #define DKP_PROCESSTREE    0  // kill process and all descendents, if created by this process
  13.    #define DKP_PROCESS        1  // kill any process even if not created by this process
  14.    #define ORD_DOS32KILLPROCESS  235
  15.    return DynamicLink("doscalls",ORD_DOS32KILLPROCESS,BIT32,CDECL,ActionCode,ProcessID)
  16. }
  17.  
  18. MaybeKill(KillSpec,ProcSpec)  // if KillSpec matches any part of ProcSpec, then kill
  19. {                             // the id of ProcSpec
  20.    // divide this process id into numbers and parts so we can compare
  21.    // against any one of these types
  22.    id = ProcSpec.id;
  23.    fullname = ProcSpec.name;
  24.    root = SplitFileName(fullname).name;
  25.    sprintf(name,"%s%s",root,SplitFileName(fullname).ext);
  26.    if ( (0 != id  &&  id == atoi(KillSpec))
  27.      || 0 == stricmp(KillSpec,fullname)
  28.      || 0 == stricmp(KillSpec,root)
  29.      || 0 == stricmp(KillSpec,name) ) {
  30.       // this id matched, and so try to kill it
  31.       rc = DosKillProcess(DKP_PROCESS,id);
  32.       if ( NO_ERROR == rc ) {
  33.          printf("%s has been killed.\n",fullname);
  34.       } else {
  35.          printf("\aError %d in DosKillProcess() from id %d\n",rc,id);
  36.       }
  37.    }
  38. }
  39.  
  40. main(argc,argv)
  41. {
  42.    if ( argc != 2  ||  !strcmp(argv[1],"/?")  ||  !strcmpi(argv[1],"help") ) {
  43.       Instructions();
  44.    } else {
  45.       Proc = argv[1];
  46.       // build list of all running processes
  47.       ProcList = ProcessList();
  48.       assert( NULL != ProcList );
  49.       ProcCount = 1 + GetArraySpan(ProcList);
  50.       // check each running process against this ID, whether it be name, or id
  51.       // number, or full or partial name, and try to kill it if it matches
  52.       for ( i = 0; i < ProcCount; i++ ) {
  53.          MaybeKill(argv[1],ProcList[i]);
  54.       }
  55.    }
  56. }
  57.  
  58. Instructions()
  59. {
  60.    printf("\n")
  61.    printf("KillProc - Kill a running process by name or by process ID.\n")
  62.    printf("\n")
  63.    printf("SYNTAX: KillProc ProcessName | ProcessID\n")
  64.    printf("\n")
  65.    printf("Where:  ProcessName - Full or partial name of running process\n")
  66.    printf("        FileSpec - FileSpec defines set of drive:filename files to list\n")
  67.    printf("\n")
  68.    printf("Example: KillProc e.exe\n")
  69.    printf("\n")
  70. }
  71.  
  72.