home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cenvi23.zip / KILL.CMD < prev    next >
OS/2 REXX Batch file  |  1995-03-28  |  4KB  |  95 lines

  1. EXTPROC CEnvi2
  2. /***********************************************************************
  3.  *** Kill.cmd - CEnvi2 program to kill a running process by reference ***
  4.  *** ver.1      to its ID, partial name, or full name, or by full or ***
  5.  ***            partial name of it Window.                           ***
  6.  ***********************************************************************/
  7.  
  8. #include <PMdll.lib>
  9. #include <WinTools.lib>
  10.  
  11. #define  NO_ERROR          0     // return code from most successful DosCalls
  12.  
  13. KillQuietMode = False;
  14.  
  15. DosKillProcess(ActionCode,ProcessID)
  16.    // kill specified ProcessID using action code DKP_PRECESSTREE or DKP_PROCESS
  17. {
  18.    #define DKP_PROCESSTREE    0  // kill process and all descendents, if created by this process
  19.    #define DKP_PROCESS        1  // kill any process even if not created by this process
  20.    #define ORD_DOS32KILLPROCESS  235
  21.    return DynamicLink("doscalls",ORD_DOS32KILLPROCESS,BIT32,CDECL,ActionCode,ProcessID)
  22. }
  23.  
  24. MaybeKill(KillSpec,id,fullname)  // if KillSpec matches any part of ProcSpec, then kill
  25. {                                // the id of ProcSpec; return TRUE if killed (or tried)
  26.    // divide this process id into numbers and parts so we can compare
  27.    // against any one of these types
  28.    root = SplitFileName(fullname).name;
  29.    sprintf(name,"%s%s",root,SplitFileName(fullname).ext);
  30.    if ( (0 != id  &&  id == atoi(KillSpec))
  31.      || 0 == strnicmp(KillSpec,fullname,lSpecLen = strlen(KillSpec))
  32.      || 0 == strnicmp(KillSpec,root,lSpecLen)
  33.      || 0 == strnicmp(KillSpec,name,lSpecLen) ) {
  34.       // this id matched, and so try to kill it
  35.       rc = DosKillProcess(DKP_PROCESS,id);
  36.       if ( !KillQuietMode ) {
  37.          if ( NO_ERROR == rc ) {
  38.             printf("%s has been killed.\n",fullname);
  39.          } else {
  40.             printf("\aError %d in DosKillProcess() from id %d\n",rc,id);
  41.          }
  42.       }
  43.       return(TRUE);
  44.    }
  45.    return(False);
  46. }
  47.  
  48. main(argc,argv)
  49. {
  50.    if ( argc != 2  ||  !strcmp(argv[1],"/?")  ||  !strcmpi(argv[1],"help") ) {
  51.       Instructions();
  52.    } else {
  53.  
  54.       // build list of all running processes
  55.       ProcList = ProcessList();
  56.       assert( NULL != ProcList );
  57.       ProcCount = 1 + GetArraySpan(ProcList);
  58.  
  59.       // check each running process against this ID, whether it be name, or id
  60.       // number, or full or partial name, and try to kill it if it matches
  61.       for ( i = 0; i < ProcCount; i++ )
  62.          MaybeKill(argv[1],ProcList[i].id,ProcList[i].name);
  63.  
  64.       // build list of all window switch titles
  65.       SwitchList = WinQuerySwitchList(Info().hab);
  66.       assert( SwitchList != NULL );
  67.       SwitchCount = 1 + GetArraySpan(SwitchList);
  68.       for ( i = 0; i < SwitchCount; i++ ) {
  69.          if ( 0 == WinQuerySwitchEntry(SwitchList[i],SwEntry) ) {
  70.             if ( !MaybeKill(argv[1],SwEntry.process,SwEntry.title) ) {
  71.                if ( (FullTitle = GetWindowTitle(SwEntry.hwnd)) ) {
  72.                   MaybeKill(argv[1],SwEntry.process,FullTitle);
  73.                }
  74.             }
  75.          }
  76.       }
  77.    }
  78. }
  79.  
  80. Instructions()
  81. {
  82.    printf("\n")
  83.    printf("Kill - Kill a running process by name, Window name, process ID.\n")
  84.    printf("\n")
  85.    printf("SYNTAX: Kill ProcessName | WindowTitle | ProcessID\n")
  86.    printf("\n")
  87.    printf("Where:  ProcessName - Full or partial name of running process\n")
  88.    printf("        WindowTitle - Full Window title\n")
  89.    printf("        ProcessID   - Numeric ID of the process to kill\n")
  90.    printf("\n")
  91.    printf("Example: Kill e.exe\n")
  92.    printf("\n")
  93. }
  94.  
  95.