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

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

unix.superglobalmegacorp.com

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