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

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

unix.superglobalmegacorp.com

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