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