home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 19 / CD_ASCQ_19_010295.iso / win / prg / cenviw / exitwin.cmm < prev    next >
Text File  |  1994-09-03  |  5KB  |  159 lines

  1. // ExitWin.cmm - Exit Windows with options to reboot system, go
  2. // ver.1         to DOS, or run command and return, with options
  3. //               for what to do before exiting.
  4.  
  5.  
  6. Instructions()
  7. {
  8.    puts("\aExitWin - Exit Windows; optionally restart windows or system")
  9.    puts(``)
  10.    puts(`USAGE: CEnvi ExitWin <DOS | REBOOT | RESTART [Command]> [Options]`)
  11.    puts(``)
  12.    puts(`WHERE: DOS - Exit Windows and return to DOS`)
  13.    puts(`       REBOOT - Exit Windows and reboot computer`)
  14.    puts(`       RESTART - Exit Windows, run optional DOS Command, and restart Windows`)
  15.    puts(`       Command - Optional DOS command for RESTART options`)
  16.    puts(`       Options - Any of the following options may be added`)
  17.    puts(`         /FILESAVE - Select the File/Save menu option for Windows`)
  18.    puts(`         /FORCE - Force applications to close; may lose data`)
  19.    puts(``)
  20.    puts(`NOTE: Without the /FORCE and/or /FILESAVE option, some applications may`)
  21.    puts(`      refuse to close and so Windows won't exit. With the /FORCE option`)
  22.    puts(`      applications will be forced to end even if data is not saved.`)
  23.    puts(``)
  24.    puts(`EXAMPLES: CEnvi ExitWin DOS /FILESAVE /FORCE`)
  25.    puts(`          CEnvi ExitWin RESTART copy C:\WIN\WIN.INI C:\WIN\WIN.BAK`)
  26.    puts(`          CEnvi ExitWin REBOOT /FORCE`)
  27.    puts(``)
  28.    printf("Press any key to exit...");
  29.    getch();
  30.    exit(EXIT_FAILURE);
  31. }
  32.  
  33. #include <OptParms.lib>
  34. #include <Message.lib>
  35. #include <WinTools.lib>
  36. #include <MenuCtrl.lib>
  37.  
  38. main(argc,argv)
  39. {
  40.    // parse input to get actions wanted
  41.    FileSave = OptionalParameter(argc,argv,"FILESAVE");
  42.    Force = OptionalParameter(argc,argv,"FORCE");
  43.    if ( 1 < argc ) {
  44.       if ( !stricmp(argv[1],"RESTART") ) {
  45.          if ( 2 == argc )
  46.             ExitFunction = "RestartWindows";
  47.          else {
  48.             ExitFunction = "RunCommandAndRestartWindows";
  49.             ExitCommands = argv + 2;
  50.             ExitCommandCount = argc - 2;
  51.          }
  52.       } else if ( argc == 2 ) {
  53.          if ( !stricmp(argv[1],"DOS") )
  54.             ExitFunction = "ExitToDOS";
  55.          else if ( !stricmp(argv[1],"REBOOT") )
  56.             ExitFunction = "ExitAndRebootComputer";
  57.       }
  58.    }
  59.    if ( !defined(ExitFunction) )
  60.       Instructions();
  61.  
  62.    if ( FileSave ) {
  63.       AutoSelectFileSave();
  64.    }
  65.    if ( Force ) {
  66.       QuitAllNamedWindows();
  67.    }
  68.  
  69.    // call exit function, which should not return
  70.    function(ExitFunction,ExitCommandCount,ExitCommands);
  71.  
  72.    // if exit function returned then there was a problem
  73.    printf("\n\a\n");
  74.    puts(`Exit did not succeed.`)
  75.    printf("Press any key to quit...");
  76.    getch();
  77.    return EXIT_FAILURE;
  78. }
  79.  
  80. RunCommandAndRestartWindows(argc,argv)
  81. {
  82.    // build command parameters
  83.    lExe = defined(COMSPEC) ? COMSPEC : "COMMAND.COM";
  84.    lParms = "/C";
  85.    for ( lArg = 0; lArg < argc; lArg++ ) {
  86.       strcat(lParms," ");
  87.       strcat(lParms,argv[lArg]);
  88.    }
  89.    DynamicLink("USER","EXITWINDOWSEXEC",SWORD16,PASCAL,lExe,lParms);
  90. }
  91.  
  92. RestartWindows()
  93. {
  94.    #define EW_RESTARTWINDOWS  0x42
  95.    DynamicLink("USER","EXITWINDOWS",SWORD16,PASCAL,0,EW_RESTARTWINDOWS,0);
  96. }
  97.  
  98. ExitAndRebootComputer()
  99. {
  100.    #define EW_REBOOTSYSTEM    0x43
  101.    DynamicLink("USER","EXITWINDOWS",SWORD16,PASCAL,0,EW_REBOOTSYSTEM,0);
  102. }
  103.  
  104. ExitToDOS()
  105. {
  106.    DynamicLink("USER","EXITWINDOWS",SWORD16,PASCAL,0,0,0);
  107. }
  108.  
  109. QuitAllNamedWindows()  // Post WM_QUIT to all windows
  110. {
  111.    printf("Send WM_QUIT to all windows...");
  112.    if ( lWinList = WindowList() ) {
  113.       for ( lIdx = 0; lIdx <= GetArraySpan(lWinlist); lIdx++ ) {
  114.          lWin = lWinList[lIdx];
  115.          Title = GetWindowTitle(lWin);
  116.          if ( Title && lWin != ScreenHandle() ) {
  117.             printf("\b\b\b\nQuit \"%s\"...",Title);
  118.             PostMessage(lWin,WM_QUIT,0,0);
  119.          }
  120.       }
  121.    }
  122.    printf("\b\b\b\n");
  123. }
  124.  
  125. AutoSelectFileSave() // select File/Save wherever it is available
  126. {
  127.    printf("Select File/Save where applicable...");
  128.    if ( lWinList = WindowList() ) {
  129.       for ( lIdx = 0; lIdx <= GetArraySpan(lWinlist); lIdx++ ) {
  130.          lWin = lWinList[lIdx];
  131.          Title = GetWindowTitle(lWin);
  132.          if ( Title && lWin != ScreenHandle() )
  133.             SelectSaveFromMenu(lWin,Title);
  134.       }
  135.    }
  136.    printf("\b\b\b\n");
  137. }
  138.  
  139. SelectSaveFromMenu(pHwnd,pTitle)  // if menu has Save, then select it
  140. {
  141.    // get handle for the menu
  142.    if ( MenuHandle = GetMenu(pHwnd) ) {
  143.  
  144.       // Get ID for the "Save" menu item
  145.       if ( SaveID = FindMenuString( MenuHandle, "File##Save", False ) ) {
  146.  
  147.          // Don't select if disabled or grayed
  148.          SaveState = GetMenuState(MenuHandle,SaveID,MF_BYCOMMAND);
  149.          if ( !(SaveState & (MF_DISABLED|MF_GRAYED)) ) {
  150.  
  151.             // Finally! This window has "Save", so select it
  152.             printf("\b\b\b\nFile/Save \"%s\"...",pTitle);
  153.             MenuCommand(pHwnd,SaveID,True);
  154.  
  155.          }
  156.       }
  157.    }
  158. }
  159.