Annotation of q_a/samples/termproc/termproc.c, revision 1.1

1.1     ! root        1: #include <stdio.h>
        !             2: #include <stdlib.h>
        !             3: #include <stdarg.h>
        !             4: #include <windows.h>
        !             5: #include <string.h>
        !             6: #include <math.h>
        !             7: #include <winbase.h>
        !             8: 
        !             9: /*-----------------------------------------------------------------*/
        !            10: 
        !            11: void ErrorOut(char errstring[30])
        !            12: /*
        !            13: Purpose: Print out an meainful error code by means of
        !            14:          GetLastError and printf
        !            15: 
        !            16: Inputs:  errstring - the action that failed, passed by the
        !            17:                      calling proc.
        !            18: 
        !            19: Returns: none
        !            20: 
        !            21: Calls:   GetLastError
        !            22: */
        !            23: 
        !            24: {
        !            25: DWORD Error;
        !            26: 
        !            27: Error= GetLastError();
        !            28: printf("Error on %s = %d\n", errstring, Error);
        !            29: }
        !            30: 
        !            31: /*-----------------------------------------------------------------*/
        !            32: 
        !            33: 
        !            34: void main(int argc, char *argv[])
        !            35: /*
        !            36: Purpose: terminate a process based on the PID passed in
        !            37: 
        !            38: Inputs:  argc - number of arguments passed in
        !            39:          argv - arguments, including PID of process to terminate
        !            40: 
        !            41: Returns: none
        !            42: 
        !            43: Calls:   GetLastError
        !            44:          OpenProcess - to get handle to process
        !            45:          TerminateProcess - to terminate process
        !            46: */
        !            47: 
        !            48: {
        !            49: HANDLE hProcess;
        !            50: double ProcId;
        !            51: BOOL TermSucc;
        !            52: int chrintval, chrpos, downcount;
        !            53: int count=0;
        !            54: 
        !            55: 
        !            56: if (argc == 2)  /* if a PID is passed in */
        !            57:   {
        !            58:     chrpos= strcspn( argv[1], "\0");
        !            59:     count=0;
        !            60:     ProcId=0;
        !            61: 
        !            62:          /* convert decimal value to hex */
        !            63:     for (downcount = chrpos -1 ; downcount >= 0; downcount--)
        !            64:       {
        !            65:       if (argv[1][downcount] >= '0' && argv[1][downcount] <= '9')
        !            66:         chrintval= argv[1][downcount]-48;
        !            67:       else
        !            68:         if (argv[1][downcount] >= 'a' && argv[1][downcount] <= 'f')
        !            69:           chrintval= argv[1][downcount]-87;
        !            70:         else
        !            71:           chrintval= argv[1][downcount]-55;
        !            72:       ProcId+= chrintval * pow( (double)16, (double)count );
        !            73:       count++;
        !            74:       }
        !            75:        /* open the process to terminate */
        !            76:   hProcess= OpenProcess(PROCESS_ALL_ACCESS, TRUE, (DWORD)ProcId);
        !            77:   if (hProcess == NULL)
        !            78:     ErrorOut("OpenProcess");
        !            79:   TermSucc= TerminateProcess(hProcess, 0); /* terminate it */
        !            80:   if (TermSucc == FALSE)
        !            81:     ErrorOut("TerminateProcess");
        !            82:   else
        !            83:     printf("Process# %s terminated successfully!\n", argv[1]);
        !            84:   }
        !            85: else  /* no PID, inform user of operation */
        !            86:   {
        !            87:   printf("\nTerminates an active Process\n");
        !            88:   printf("Usage: termproc ProcessID\n");
        !            89:   }
        !            90: 
        !            91: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.