home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / onshtdwn.zip / source.zip / onshutdown.c < prev    next >
Text File  |  1994-05-21  |  4KB  |  138 lines

  1. /****************************************************************************
  2.  
  3.     OnShutdown.c
  4.  
  5.     (C) Copyright Claudio Fahey, 1994
  6.  
  7.     e-mail: claudio@uclink.berkeley.edu
  8.  
  9.     Permission granted to use, distribute, and modify, provided that this
  10.     notice remains intact.  If you distribute a modified version, you must
  11.     identify your modifications as such.
  12.  
  13. ****************************************************************************/
  14.  
  15. #define INCL_WIN
  16. #define INCL_DOS
  17. #include <os2.h>
  18. #include <string.h>
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21.  
  22. static HAB hab;
  23. static HMQ hmq;
  24. static char szCmdParams[1024];
  25. static char *szCmd;
  26. static char szUsage[] =
  27.     "Usage:\n"
  28.     "  OnShutdown <program> [arguments]\n\n"
  29.     "Example:\n"
  30.     "  OnShutdown c:\\shutdown.cmd\n\n"
  31.     "Purpose:\n"
  32.     "  OnShutdown will start the specified\n"
  33.     "  program when the system shuts\n"
  34.     "  down.";
  35.  
  36. MRESULT EXPENTRY ShutdownObject( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 );
  37.  
  38. int main( int argc, char *argv[] )
  39.     {
  40.     QMSG qmsg;
  41.     HWND hwnd;
  42.     int i;
  43.  
  44.     hab = WinInitialize( 0L );
  45.     hmq = WinCreateMsgQueue( hab, 0 );
  46.  
  47.     WinRegisterClass( hab, "Shutdown Object", ShutdownObject, 0L, 0 );
  48.  
  49.     // Setup application parameters
  50.     if ( argc >= 2 )
  51.         szCmd = argv[1];
  52.     else
  53.         {
  54.         WinMessageBox( HWND_DESKTOP, HWND_DESKTOP, szUsage, "On Shutdown", 0L,
  55.             MB_OK | MB_ERROR | MB_MOVEABLE );
  56.         return 1;
  57.         }
  58.  
  59.     szCmdParams[0] = '\0';
  60.     for ( i = 2 ; i < argc - 1; i++ )
  61.         {
  62.         strcat( szCmdParams, argv[i] );
  63.         strcat( szCmdParams, " " );
  64.         }
  65.     if ( argc >= 3 )
  66.         strcat( szCmdParams, argv[argc-1] );
  67.  
  68.     hwnd = WinCreateWindow( HWND_OBJECT, "Shutdown Object", 0L, 0L, 0, 0, 0, 0, 0L,
  69.         HWND_TOP, 0, 0L, 0L );
  70.  
  71.     while ( WinGetMsg( hab, &qmsg, 0L, 0, 0 ) )
  72.         WinDispatchMsg( hab, &qmsg );
  73.  
  74.     WinDestroyWindow( hwnd );
  75.     WinDestroyMsgQueue( hmq );
  76.     WinTerminate( hab );
  77.  
  78.     return 0;
  79.     }
  80.  
  81. MRESULT EXPENTRY ShutdownObject( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 )
  82.     {
  83.     QMSG qmsg;
  84.     PROGDETAILS Details;
  85.     HAPP happ;
  86.     char szMessage[1024];
  87.  
  88.     switch( msg )
  89.         {
  90.         case WM_DESTROY:
  91.             // Start application
  92.             Details.Length          = sizeof(PROGDETAILS);
  93.             Details.progt.progc     = PROG_DEFAULT;
  94.             Details.progt.fbVisible = SHE_VISIBLE;
  95.             Details.pszTitle        = "Shutdown Process";
  96.             Details.pszExecutable   = szCmd;
  97.             Details.pszParameters   = NULL;
  98.             Details.pszStartupDir   = "";
  99.             Details.pszIcon         = "";
  100.             Details.pszEnvironment  = "";
  101.             Details.swpInitial.fl   = SWP_ACTIVATE;
  102.             Details.swpInitial.cy   = 0;
  103.             Details.swpInitial.cx   = 0;
  104.             Details.swpInitial.y    = 0;
  105.             Details.swpInitial.x    = 0;
  106.             Details.swpInitial.hwndInsertBehind = HWND_TOP;
  107.             Details.swpInitial.hwnd             = 0L;
  108.             Details.swpInitial.ulReserved1      = 0;
  109.             Details.swpInitial.ulReserved2      = 0;
  110.             happ = WinStartApp( hwnd, &Details, szCmdParams, NULL, SAF_STARTCHILDAPP );
  111.  
  112.             if ( happ )
  113.                 {
  114.                 // Process message queue because we need to wait for WM_APPTERMINATENOTIFY
  115.                 while ( WinGetMsg( hab, &qmsg, 0L, 0, 0 ) )
  116.                     WinDispatchMsg( hab, &qmsg );
  117.                 }
  118.             else
  119.                 {
  120.                 sprintf( szMessage, "Error starting application \"%s\".\n\n"
  121.                     "Do you want to cancel shutdown?", szCmd );
  122.                 if ( WinMessageBox( HWND_DESKTOP, HWND_DESKTOP, szMessage, "On Shutdown Error", 0L,
  123.                         MB_YESNO | MB_ERROR | MB_MOVEABLE ) == MBID_YES )
  124.                     WinCancelShutdown( hmq, FALSE );
  125.                 }
  126.             break;
  127.  
  128.         case WM_APPTERMINATENOTIFY:
  129.             WinPostMsg( hwnd, WM_QUIT, 0L, 0L );
  130.             break;
  131.  
  132.         default:
  133.             return WinDefWindowProc( hwnd, msg, mp1, mp2 );
  134.         }
  135.     return 0L;
  136.     }
  137.  
  138.