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

1.1       root        1: /**************************************************************************\
                      2: *  process.c -- sample program demonstrating the CreateProcess and
                      3: *   TerminateProcess() functions.
                      4: *
                      5: *  written March 92 by Steve Firebaugh
                      6: *
                      7: *  In this sample the main window is a dialog box.  There is no need to
                      8: *   register a new window class or create a new window.  Instead just call
                      9: *   DialogBox() and use the template defined in the .RC file.
                     10: *
                     11: \**************************************************************************/
                     12: 
                     13: #include <windows.h>
                     14: #include <commdlg.h>
                     15: #include <stdio.h>
                     16: #include "process.h"
                     17: 
                     18: /* declare a global instance handle because it is needed for the open
                     19:  *  file common dialog box later on.
                     20:  */
                     21: HANDLE hInst;
                     22: 
                     23: /**************************************************************************\
                     24: *
                     25: *  function:  WinMain()
                     26: *
                     27: *  input parameters:  c.f. generic sample
                     28: *
                     29: \**************************************************************************/
                     30: int APIENTRY WinMain(HANDLE hInstance, HANDLE hPrevInstance,
                     31:                      LPSTR lpCmdLine, int nCmdShow)
                     32: {
                     33: int ret;
                     34: 
                     35:     UNREFERENCED_PARAMETER( hPrevInstance );
                     36:     UNREFERENCED_PARAMETER( lpCmdLine );
                     37:     UNREFERENCED_PARAMETER( nCmdShow);
                     38: 
                     39:     hInst = hInstance;
                     40: 
1.1.1.2 ! root       41:     ret = DialogBox (hInstance, "processDlg", NULL, (DLGPROC)MainDlgProc);
1.1       root       42:     return ret;
                     43: }
                     44: 
                     45: 
                     46: 
                     47: 
                     48: /**************************************************************************\
                     49: *
                     50: *  function:  MainDlgProc()
                     51: *
                     52: *  input parameters:  standard window procedure parameters.
                     53: *
                     54: \**************************************************************************/
                     55: LONG APIENTRY MainDlgProc(HWND hwnd, UINT message, UINT wParam, LONG lParam)
                     56: {
                     57:   UNREFERENCED_PARAMETER(lParam);
                     58: 
                     59: 
                     60:   switch (message) {
                     61: 
                     62:     /********************************************************************\
                     63:     * WM_INITDIALOG
                     64:     *
                     65:     * Establish the correct tab stops for both of the list boxes.
                     66:     *  Fill in the header list box.
                     67:     \********************************************************************/
                     68:     case WM_INITDIALOG: {
                     69:       DWORD  tabs[4];
                     70: 
                     71:       tabs[0] =           (WORD)(sizeof("00000000 ") *4);
                     72:       tabs[1] = tabs[0] + (WORD)(sizeof("00000000 ") *4);
                     73:       tabs[2] = tabs[1] + (WORD)(sizeof("dwProcessID ") *4);
                     74:       tabs[3] = tabs[2] + (WORD)(sizeof("dwThreadID ") *4);
                     75: 
                     76:       SendDlgItemMessage (hwnd, DID_LISTBOX, LB_SETTABSTOPS, 4, (LONG) tabs);
                     77:       SendDlgItemMessage (hwnd, DID_HEADER,  LB_SETTABSTOPS, 4, (LONG) tabs);
                     78: 
                     79:       SendDlgItemMessage (hwnd, DID_HEADER,  LB_ADDSTRING,   0, (LONG)
                     80:             "hProcess \thTread \tdwProcessID \tdwThreadID \tImage file");
                     81: 
                     82:     } return TRUE;
                     83: 
                     84:     /********************************************************************\
                     85:     * WM_SYSCOMMAND
                     86:     *
                     87:     * ignore all syscommand messages, except for SC_CLOSE.
                     88:     *  on this one, call EndDialog().
                     89:     \********************************************************************/
                     90:     case WM_SYSCOMMAND:
                     91:       if (wParam == SC_CLOSE) {
                     92:         EndDialog (hwnd, TRUE);
                     93:         return TRUE;
                     94:       } else
                     95:         return FALSE;
                     96:     break;
                     97: 
                     98: 
                     99:     /********************************************************************\
                    100:     * WM_COMMAND
                    101:     *
                    102:     * When the different buttons are hit, clear the list box, disable
                    103:     *  updating to it, call the function which will fill it, reenable
                    104:     *  updating, and then force a repaint.
                    105:     *
                    106:     \********************************************************************/
                    107:     case WM_COMMAND:
                    108: 
                    109:       /* if the list box sends back messages, return.  */
                    110:       if (LOWORD(wParam)==DID_LISTBOX) return TRUE;
                    111: 
                    112:       /* switch on the control ID of the button that is pressed. */
                    113:       switch (LOWORD(wParam)) {
                    114:         case DID_CREATE :    doCreate (hwnd);   break;
                    115:         case DID_TERMINATE : doTerminate(hwnd); break;
                    116:       } /* end switch (LOWORD()) */
                    117: 
                    118:       return TRUE;
                    119:     break; /* end WM_COMMAND */
                    120: 
                    121: 
                    122:     default: return FALSE;
                    123:   } /* end switch(message) */
                    124: }
                    125: 
                    126: 
                    127: 
                    128: 
                    129: /**************************************************************************\
                    130: *
                    131: *  function:  doCreate()
                    132: *
                    133: *  input parameters: hwnd - window handle for the dialog.
                    134: *  global parameters: hInst (for the OPENFILENAME struct.)
                    135: *
                    136: * Call GetOpenFileName() in order to get the name of some EXE file,
                    137: *  then call CreatProcess() and put the results in the list box.
                    138: *
                    139: \**************************************************************************/
                    140: VOID doCreate (HWND hwnd)
                    141: {
                    142:     OPENFILENAME         of;
                    143:     char buffer [MAXCHARS];
                    144:     char bufferLB [MAXCHARS];
                    145:     STARTUPINFO          sui;
                    146:     PROCESS_INFORMATION  pi;
                    147:     DWORD ret;
                    148: 
                    149:     buffer[0] = 0;
                    150: 
                    151:     /* set up the OPENFILE structure,
                    152:      *  then use the appropriate common dialog
                    153:      */
                    154:     of.lStructSize       = sizeof (OPENFILENAME);
                    155:     of.hwndOwner         = NULL;
                    156:     of.hInstance         = hInst;
                    157:     of.lpstrFilter       = "Executables\000 *.EXE\000\000";
                    158:     of.lpstrCustomFilter = NULL;
                    159:     of.nMaxCustFilter    = NULL;
                    160:     of.nFilterIndex      = NULL;
                    161:     of.lpstrFile         = buffer;
                    162:     of.nMaxFile          = MAXCHARS;
                    163:     of.lpstrFileTitle    = NULL;
                    164:     of.nMaxFileTitle     = NULL;
                    165:     of.lpstrInitialDir   = NULL;
                    166:     of.lpstrTitle        = NULL;
1.1.1.2 ! root      167:     of.Flags             = OFN_HIDEREADONLY;
1.1       root      168:     of.nFileOffset       = NULL;
                    169:     of.nFileExtension    = NULL;
                    170:     of.lpstrDefExt       = NULL;
                    171:     of.lCustData         = NULL;
                    172:     of.lpfnHook          = NULL;
                    173:     of.lpTemplateName    = NULL;
                    174:     if (!GetOpenFileName (&of)) return;
                    175: 
                    176: 
                    177: 
                    178:     /* set up the STARTUPINFO structure,
                    179:      *  then call CreateProcess to try and start the new exe.
                    180:      */
                    181:     sui.cb               = sizeof (STARTUPINFO);
                    182:     sui.lpReserved       = 0;
                    183:     sui.lpDesktop        = NULL;
                    184:     sui.lpTitle          = NULL;
                    185:     sui.dwX              = 0;
                    186:     sui.dwY              = 0;
                    187:     sui.dwXSize          = 0;
                    188:     sui.dwYSize          = 0;
                    189:     sui.dwXCountChars    = 0;
                    190:     sui.dwYCountChars    = 0;
                    191:     sui.dwFillAttribute  = 0;
                    192:     sui.dwFlags          = 0;
                    193:     sui.wShowWindow      = 0;
                    194:     sui.cbReserved2      = 0;
                    195:     sui.lpReserved2      = 0;
                    196: 
                    197:     /**********************************************/
                    198:     /**********************************************/
                    199:     ret = CreateProcess (buffer, NULL, NULL, NULL,
1.1.1.2 ! root      200:                    FALSE, DETACHED_PROCESS,
1.1       root      201:                    NULL, NULL, &sui, &pi );
                    202:     /**********************************************/
                    203:     /**********************************************/
                    204: 
                    205: 
                    206:     if (ret == TRUE){
                    207:       /* Put the data about the new process in the list box. */
                    208:       wsprintf (bufferLB, "%08lx \t%08lx \t%d  \t%d \t%s",
                    209:                   (LONG) pi.hProcess,    (LONG) pi.hThread,
                    210:                   (DWORD)pi.dwProcessId, (DWORD)pi.dwThreadId, buffer);
                    211:       SendDlgItemMessage (hwnd, DID_LISTBOX, LB_ADDSTRING, 0, (LONG) bufferLB);
                    212:     } else {
                    213:       /* report failure to the user. */
                    214:       ret = GetLastError ();
                    215:       sprintf (buffer, "0x%lx", (int)ret);
                    216:       MessageBox (hwnd, buffer, "GetLastError...", MB_ICONSTOP | MB_OK);
                    217:     }
                    218: 
                    219:     return;
                    220: }
                    221: 
                    222: 
                    223: 
                    224: 
                    225: /**************************************************************************\
                    226: *
                    227: *  function:  doTerminate()
                    228: *
                    229: *  input parameters: hwnd - window handle for the dialog.
                    230: *
                    231: * Determine which process is selected in the list box.  Then parse the
                    232: *  process handle out of that string and call TerminateProcess() with it.
                    233: *
                    234: 
                    235: 
                    236: Warning:
                    237: 
                    238: "TerminateProcess is used to cause all of the threads within a process
                    239: to terminate.  While TerminateProcess will cause all threads within a
                    240: process to terminate, and will cause an application to exit, it does
                    241: not notify DLLs that the process is attached to that the process is
                    242: terminating.  TerminateProcess is used to unconditionally cause a
                    243: process to exit. It should only be used in extreme circumstances.
                    244: The state of global data maintained by DLLs may be compromised if
                    245: TerminateProcess is used rather that ExitProcess."
                    246: 
                    247: 
                    248: 
                    249: \**************************************************************************/
                    250: VOID doTerminate(HWND hwnd)
                    251: {
                    252:     char buffer [MAXCHARS];
                    253:     int  sel;
                    254:     DWORD ret;
                    255:     HANDLE hProcess;
                    256: 
                    257:     /* determine which item is selected in the list box, and get the text */
                    258:     sel= SendDlgItemMessage (hwnd, DID_LISTBOX, LB_GETCURSEL, 0, 0);
                    259:     if (sel == LB_ERR) {
                    260:       MessageBox (hwnd, "No listbox item is selected.",
                    261:                   "Incorrect use", MB_ICONSTOP | MB_OK);
                    262:       return;
                    263:     }
                    264:     SendDlgItemMessage (hwnd, DID_LISTBOX, LB_GETTEXT, sel, (LONG)buffer);
                    265: 
                    266:     /* pick the process handle out of the string. */
                    267:     sscanf (buffer, "%lx", &hProcess);
                    268: 
                    269:     /**********************************************/
                    270:     /**********************************************/
                    271:     ret = TerminateProcess (hProcess, 0);
                    272:     /**********************************************/
                    273:     /**********************************************/
                    274: 
                    275:     if (ret == TRUE){
                    276:       SendDlgItemMessage (hwnd, DID_LISTBOX, LB_DELETESTRING, sel, 0);
                    277:     } else {
                    278:       ret = GetLastError ();
                    279:       sprintf (buffer, "0x%lx", (int)ret);
                    280:       MessageBox (hwnd, buffer, "GetLastError...", MB_ICONSTOP | MB_OK);
                    281:     }
                    282: 
                    283:   return ;
                    284: }

unix.superglobalmegacorp.com

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