home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 8 / CDASC08.ISO / VRAC / RSTART14.ZIP / RESTART.C < prev    next >
C/C++ Source or Header  |  1993-09-07  |  11KB  |  272 lines

  1. /////////////////////////////////////////////////////////////////
  2. // RESTART.C - 1993, Jeffrey M. Perkel
  3. // Restart is a utility that allows the user to quit or restart
  4. // Windows.
  5. //
  6. // Usage:  RESTART [/R | /E]
  7. //               "R" -- Restart
  8. //               "E" -- Exit
  9. /////////////////////////////////////////////////////////////////
  10.  
  11. #include <windows.h>
  12. #include <string.h>
  13. #include <ctype.h>      // For definition of TOLOWER (INT C)
  14. #include "restart.h"
  15.                     
  16. // Global Variables
  17.  
  18. static HINSTANCE ghInstance;
  19. static char szAppName[] = "Restart";
  20. static int Action, Confirm;
  21. static BOOL bSuccess, bAnswer;
  22. static LPSTR lpCommandLine;
  23.  
  24. // Exported Functions
  25.  
  26. BOOL FAR PASCAL _export MainDlgProc (HWND hDlg, unsigned message, WORD
  27.         wParam, LONG lParam);
  28. BOOL FAR PASCAL _export AboutDlgProc (HWND hAboutDlg, UINT message, UINT
  29.         wParam, LONG lParam);
  30.  
  31. // Internal Functions
  32.  
  33. BOOL ParseCmdLine (LPSTR lpCommandLine);
  34. BOOL MakeItSo (int Action);
  35. BOOL AreYouSure (HWND hDlg, int Action);
  36. int ErrorHandler (HWND hwnd, int iError);
  37.  
  38. // Program Entry Point
  39.  
  40. int PASCAL WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  41.         LPSTR lpCmdLine, int nCmdShow)
  42.         {
  43.         BOOL bCmdLineParam = FALSE;
  44.         
  45.         ghInstance = hInstance;
  46.         lpCommandLine = lpCmdLine;
  47.         
  48.         if (!hPrevInstance)
  49.                 {
  50.                 WNDCLASS wndclass;
  51.                 
  52.                 wndclass.style = CS_HREDRAW | CS_VREDRAW;
  53.                 wndclass.lpfnWndProc = DefDlgProc;
  54.                 wndclass.cbClsExtra = 0;
  55.                 wndclass.cbWndExtra = DLGWINDOWEXTRA;
  56.                 wndclass.hInstance = ghInstance;
  57.                 wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  58.                 wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
  59.                 wndclass.hbrBackground = GetStockObject (WHITE_BRUSH);
  60.                 wndclass.lpszMenuName = NULL;
  61.                 wndclass.lpszClassName = szAppName;                
  62.         
  63.                 if (!RegisterClass (&wndclass)) {
  64.                         ErrorHandler (NULL, ERR_WNDREGISTRATIONFAIL);
  65.                         return FALSE;
  66.                         }
  67.                 }
  68.         
  69. // Determine if RESTART was invoked using command line parameters
  70.  
  71.         bCmdLineParam = (ParseCmdLine (lpCommandLine));
  72.         if (bCmdLineParam == TRUE) {
  73.                 bAnswer = AreYouSure ((HWND) NULL, Action);
  74.                 if (bAnswer == TRUE) MakeItSo (Action);
  75.                 else return 1;
  76.                 }
  77.                 
  78.         return DialogBoxParam (ghInstance, szAppName, NULL, (DLGPROC)
  79.                 MakeProcInstance ((FARPROC) MainDlgProc, ghInstance),
  80.                 (LONG) nCmdShow);
  81. }
  82.  
  83. ///////////////////////////////////////////////////////////////
  84. // MainDlgProc - Dialog Procedure for the Main Program Window
  85. ///////////////////////////////////////////////////////////////
  86.  
  87. BOOL FAR PASCAL _export MainDlgProc (HWND hDlg, unsigned message, WORD
  88.         wParam, LONG lParam)
  89. {
  90.         switch (message)
  91.                 {
  92.                 case WM_INITDIALOG:
  93.                         ShowWindow (hDlg, LOWORD(lParam));
  94.                         SendDlgItemMessage (hDlg, IDD_CONFIRM_ON,
  95.                                 BM_SETCHECK, TRUE, 0L);
  96.                         break;
  97.                 
  98.                 case WM_CLOSE:
  99.                         EndDialog (hDlg, FALSE);
  100.                         break;
  101.                 
  102.                 case WM_COMMAND:
  103.                         switch (wParam)
  104.                                 {
  105.                               case ID_OK:
  106.                                 {
  107.                                 Confirm = (IsDlgButtonChecked (hDlg,
  108.                                     IDD_CONFIRM_ON)) ? 1 : 0;
  109.                                 Action = (IsDlgButtonChecked (hDlg,
  110.                                     IDD_RESTART)) ? EW_RESTARTWINDOWS :
  111.                                     (IsDlgButtonChecked (hDlg, IDD_EXIT)) ?
  112.                                     NULL : NOCHECK;
  113.                                 if (Action == NOCHECK) 
  114.                                         {
  115.                                         MessageBeep(0);
  116.                                         break;
  117.                                         }
  118.                                 if (Confirm)
  119.                                         {
  120.                                         bAnswer = AreYouSure (hDlg, Action);
  121.                                         if (!bAnswer) break;
  122.                                         }
  123.                                 bSuccess = MakeItSo (Action);
  124.                                 if (!bSuccess) {
  125.                                         ErrorHandler (hDlg,
  126.                                                 ERR_APPREFUSETERMINATE);
  127.                                         }
  128.                                
  129.                                 break;
  130.                                }
  131.                               case ID_CANCEL:
  132.                                         EndDialog (hDlg, FALSE);
  133.                                         break;
  134.                                         
  135.                               case ID_ABOUT:
  136.                                         {
  137.                                         FARPROC lpfnAboutDlgProc;
  138.                                         
  139.                                         lpfnAboutDlgProc = 
  140.                                                 MakeProcInstance((FARPROC)
  141.                                                 AboutDlgProc, ghInstance); 
  142.                                         DialogBox(ghInstance, 
  143.                                                 MAKEINTRESOURCE (AboutBox),
  144.                                                 hDlg, (DLGPROC) 
  145.                                                 lpfnAboutDlgProc);
  146.                                         FreeProcInstance (
  147.                                                 lpfnAboutDlgProc);
  148.                                         break;         
  149.                                         }
  150.                                 break;
  151.                                 } 
  152.                                 break;       
  153.                               default:
  154.                                         return FALSE;
  155.                                 }
  156.                                 
  157.                         return TRUE;
  158.                         }
  159.                         
  160.                         
  161. ///////////////////////////////////////////
  162. // AboutDlgProc - About... box proceedure
  163. ///////////////////////////////////////////
  164.  
  165. BOOL FAR PASCAL _export AboutDlgProc (HWND hAboutDlg, UINT message,
  166.         UINT wParam, LONG lParam)
  167.         
  168. {
  169.         switch (message)
  170.                 {
  171.                 case WM_INITDIALOG:
  172.                         return TRUE;
  173.                         
  174.                 case WM_COMMAND:
  175.                         switch (wParam)
  176.                                 {
  177.                                 case ID_OK:
  178.                                         EndDialog (hAboutDlg, 0);
  179.                                         return TRUE;
  180.                                 }
  181.                         break;
  182.                 }
  183.         return FALSE;
  184. }
  185. /////////////////////////////////////////////////////////////
  186. // MakeItSo - This function is the "meat" of the program.
  187. /////////////////////////////////////////////////////////////                                        
  188.  
  189. BOOL MakeItSo (int Action)
  190. {
  191.         BOOL bResult;
  192.             
  193.         bResult = ExitWindows (Action, 0);
  194.         return bResult;
  195. }                                                  
  196.  
  197. /////////////////////////////////////////////////////////////
  198. // AreYouSure - Confirm the user's response.
  199. /////////////////////////////////////////////////////////////
  200.  
  201. BOOL AreYouSure (HWND hDlg, int Action)
  202. {
  203.         char szBuffer[30];
  204.         short nAnswer;
  205.         HWND hwnd;
  206.         
  207.         wsprintf (szBuffer, "%s the Windows System?", (LPSTR) (Action == 
  208.             EW_RESTARTWINDOWS ? "Restart" : "Exit"));
  209.         nAnswer = MessageBox (hDlg, szBuffer,
  210.             "Confirmation", MB_YESNOCANCEL | MB_ICONQUESTION);
  211.         
  212.         if ((nAnswer == IDNO) || (nAnswer == IDCANCEL)) return FALSE;
  213.         else return TRUE;
  214. }
  215.  
  216. /////////////////////////////////////////////////////////////
  217. // ParseCmdLine - Parses the command line
  218. /////////////////////////////////////////////////////////////
  219.  
  220. BOOL ParseCmdLine (LPSTR lpCommandLine) {
  221.         
  222.         while ((*lpCommandLine == '/') || (*lpCommandLine == '-')) {
  223.            lpCommandLine++;
  224.                 switch (tolower (*lpCommandLine)) {
  225.                    case 'r':
  226.                         Action = EW_RESTARTWINDOWS;
  227.                         return TRUE;
  228.                         break;
  229.                    case 'e':
  230.                         Action = NULL;
  231.                         return TRUE;
  232.                         break;         
  233.                    default:
  234.                         ErrorHandler (NULL, ERR_BADCMDLINESWITCH);
  235.                         return FALSE;
  236.                 }      
  237.         }
  238.         if (*lpCommandLine == 0) return FALSE;
  239. }
  240.  
  241. /////////////////////////////////////////////////////////////
  242. // Error Handler Routine
  243. // Parameters:  hwnd - handle to the parent window
  244. //              iError - error code
  245. /////////////////////////////////////////////////////////////
  246.  
  247. int ErrorHandler (HWND hwnd, int iError)
  248. {
  249.         char szBuffer[50];
  250.         
  251.         switch (iError) {
  252.                 case ERR_BADCMDLINESWITCH:
  253.                         strcpy (szBuffer, "Invalid Command Line Switch!");
  254.                         break;
  255.                 
  256.                 case ERR_WNDREGISTRATIONFAIL:
  257.                         strcpy (szBuffer, "RegisterClass() failed!");
  258.                         break;
  259.                 
  260.                 case ERR_APPREFUSETERMINATE:
  261.                         strcpy (szBuffer, 
  262.                         "One or more applications refused to terminate!");
  263.                         break;        
  264.                 default:
  265.                         strcpy (szBuffer, "Error!");
  266.                         
  267.                 }
  268.         
  269.         return MessageBox (hwnd, szBuffer, "Error!", MB_ICONEXCLAMATION |
  270.                 MB_OK); 
  271. }                
  272.