home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / startapp.zip / SAPP.C < prev    next >
Text File  |  1998-09-10  |  10KB  |  249 lines

  1. /**************************************************************************
  2.  *  File name  :  sapp.c
  3.  *
  4.  *  Description:
  5.  *
  6.  *  Concepts   :  start applications from PM program
  7.  *
  8.  *  API's      :  WinStartApp
  9.  *
  10.  *  Required
  11.  *    Files    :  OS2.H, MAIN.H, XTRN.H
  12.  *
  13.  *  (c) Copyright IBM Corp. 1998  All rights reserved.
  14.  *
  15.  *  These sample programs are owned by International Business Machines
  16.  *  Corporation or one of its subsidiaries ("IBM") and are copyrighted and
  17.  *  licensed, not sold.
  18.  *
  19.  *  You may copy, modify, and distribute these sample programs in any
  20.  *  form without payment to IBM, for any purpose including developing,
  21.  *  using, marketing or distributing programs that include or are
  22.  *  derivative works of the sample programs.
  23.  *
  24.  *  The sample programs are provided to you on an "AS IS" basis, without
  25.  *  warranty of any kind.  IBM HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES,
  26.  *  EITHER EXPRESS OR IMPLIED, INCLUDING , BUT NOT LIMITED TO, THE IMPLIED
  27.  *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  28.  *  Some jurisdictions do not allow for the exclusion or limitation of
  29.  *  implied warranties, so the above limitations or exclusions may not
  30.  *  apply to you.  IBM shall not be liable for any damages you suffer
  31.  *  as a result of using, modifying or distributing the sample programs
  32.  *  or their derivatives.
  33.  *************************************************************************/
  34. /*
  35.  *  Include files, macros, defined constants, and externs
  36.  */
  37. #define INCL_DOSSESMGR
  38. #define INCL_DOSPROCESS
  39. #define INCL_PM
  40. #define INCL_WIN
  41. #include <os2.h>
  42. #include "main.h"
  43. #include "xtrn.h"
  44. #include <stdio.h>
  45. #include <string.h>
  46.  
  47. /*
  48.  *  Global variables
  49.  */
  50. CHAR         szMsg[100];         /* Message                                  */
  51. PROGDETAILS  pDetails;           /* Program Details Structure                */
  52. HAPP         happ;               /* handle of the app/session started        */
  53.  
  54. /* CAUTION, a happ is returned for the VDM session instantiated. It may not
  55.    be indicative of the application being started.  Thus, when terminating
  56.    via WinTerminate(HAPP), this terminates the session and all applications
  57.    within that session.  It is recommended to always start a PRIVATE VDM
  58.    rather than a common/public VDM
  59.  
  60.    The following variables will be used to set up the environment and identify
  61.    the application, its parameters and other specific settings we may want to
  62.    initialize.
  63.  
  64.    It is important to understand that these settings can only be initialized
  65.    for sessions that are started and not for those already started; for example,
  66.    public sessions. If you are starting an application targeted for a common session,
  67.    these settings will not take affect if a common session is currently running.
  68.  
  69.    The actual settings in the pszEnvironment variable must be separated by a "\0",
  70.    and the last setting in the string must be terminated by "\0\0".
  71.  
  72.    For example, the following assignment to pszEnvironment will force the DOS_FILES
  73.    setting to 40 and to load kernel modules above the 1MB memory address:
  74.  
  75.            pszEnvironment = "DOS_FILES=40\0DOS_HIGH=1\0";
  76.  
  77.    Note that the second "\0" at the end of the string is automatically added by the
  78.    fact that the variable is defined as a string.
  79.  
  80.    Also note that the DOS_HIGH setting is set as "1" rather than "ON". This scheme
  81.    is used for all DOS settings whose values are indicated as "ON/OFF" in the
  82.    DOS and Win-OS/2 settings notebook pages -- ON = 1 and OFF = 0.
  83. */
  84.  
  85. ULONG     User_Progc            = PROG_31_STDSEAMLESSVDM;/* 15                */
  86. ULONG     User_fbVisible        = SHE_VISIBLE;           /* show window       */
  87. PSZ       User_pszTitle         = "MyApp";               /* For task list     */
  88. PSZ       User_pszExecutable    = "NOTEPAD.EXE";         /* executable        */
  89. PSZ       User_pszParameters    = "MyNewFil.txt";        /* command line parm */
  90.          /* applicaton assumes that Warp is installed on the C drive/partition*/
  91. PSZ       User_pszStartupDir    = "C:\\os2\\mdos\\winos2";
  92.          /* do not specify icon for dos/winos2 applications                   */
  93. PSZ       User_pszIcon          = NULL;
  94.          /* we can set dos settings here                                      */
  95. PSZ       User_pszEnvironment   = "DOS_FILES=40\0DOS_HIGH=1\0";
  96. ULONG     User_swpInitial_fl    = SWP_ACTIVATE;
  97. LONG      User_swpInitial_cx    = 0;                       /* width of window */
  98. LONG      User_swpInitial_cy    = 0;                      /* height of window */
  99. LONG      User_swpInitial_y     = 0;                  /* lower edge of window */
  100. LONG      User_swpInitial_x     = 0;                   /* left edge of window */
  101. HWND      User_swpInitial_hwndInsertBehind = HWND_TOP;
  102. ULONG     User_swpInitial_ulReserved1      = 0;
  103. ULONG     User_swpInitial_ulReserved2      = 0;
  104.  
  105. /**************************************************************************
  106.  *
  107.  *  Name       : StrApp(mp2)
  108.  *
  109.  *  Description: Processes selection of the Start App menu item of the
  110.  *               Control pulldown
  111.  *
  112.  *  Concepts:  called whenever Start App from the Control menu is selected
  113.  *
  114.  *  API's      :  WinStartApp
  115.  *
  116.  *  Parameters :  mp2      = second message parameter
  117.  *
  118.  *  Return     :  [none]
  119.  *
  120.  *************************************************************************/
  121. VOID StrApp(MPARAM mp2) {
  122.  
  123. ERRORID          errorCode;    /* holds WinGetLastError value            */
  124.  
  125. /* Initialize the program details structure */
  126.   pDetails.Length          = sizeof(PROGDETAILS);
  127.   pDetails.progt.progc     = User_Progc;
  128.   pDetails.progt.fbVisible = User_fbVisible;
  129.   pDetails.pszTitle        = User_pszTitle;
  130.   pDetails.pszExecutable   = User_pszExecutable;
  131.   pDetails.pszParameters   = User_pszParameters;
  132.   pDetails.pszStartupDir   = User_pszStartupDir;
  133.   pDetails.pszIcon         = User_pszIcon;
  134.   pDetails.pszEnvironment  = User_pszEnvironment;
  135.   pDetails.swpInitial.fl   = User_swpInitial_fl;
  136.   pDetails.swpInitial.cy   = User_swpInitial_cy;
  137.   pDetails.swpInitial.cx   = User_swpInitial_cx;
  138.   pDetails.swpInitial.y    = User_swpInitial_y;
  139.   pDetails.swpInitial.x    = User_swpInitial_x;
  140.   pDetails.swpInitial.hwndInsertBehind = User_swpInitial_hwndInsertBehind;
  141.   pDetails.swpInitial.hwnd             = hwndMain; /* Main window handle external variable */
  142.   pDetails.swpInitial.ulReserved1      = User_swpInitial_ulReserved1;
  143.   pDetails.swpInitial.ulReserved2      = User_swpInitial_ulReserved2;
  144.  
  145. /* Reset application last error code */
  146.   errorCode = WinGetLastError (hwndMain);
  147.  
  148.   sprintf(szMsg,"Starting the Applicaton Via WinStartApp API");
  149.   WinSendMsg(hwndList, LM_INSERTITEM,
  150.              MPFROMSHORT(LIT_END),
  151.              szMsg);
  152.  
  153.   sprintf(szMsg," Category: %u", User_Progc);
  154.   WinSendMsg(hwndList, LM_INSERTITEM,
  155.              MPFROMSHORT(LIT_END),
  156.              szMsg);
  157.  
  158.   sprintf(szMsg," Executable: %s", User_pszExecutable);
  159.   WinSendMsg(hwndList, LM_INSERTITEM,
  160.              MPFROMSHORT(LIT_END),
  161.              szMsg);
  162.  
  163.   sprintf(szMsg," Parameters: %s", User_pszParameters);
  164.   WinSendMsg(hwndList, LM_INSERTITEM,
  165.              MPFROMSHORT(LIT_END),
  166.              szMsg);
  167.  
  168.   sprintf(szMsg," Startup Directory: %s", User_pszStartupDir);
  169.   WinSendMsg(hwndList, LM_INSERTITEM,
  170.              MPFROMSHORT(LIT_END),
  171.              szMsg);
  172.  
  173.   happ = WinStartApp(hwndMain,&pDetails,"",NULL,SAF_INSTALLEDCMDLINE | SAF_STARTCHILDAPP);
  174.   /* A successful return places the application's handle in happ.
  175.      Otherwise happ is equal to zero */
  176.  
  177.   if(happ) {  /* SUCCESSFUL */
  178.     sprintf (szMsg," -> Application handle (HAPP) is %u.", happ);
  179.     WinSendMsg(hwndList, LM_INSERTITEM,
  180.              MPFROMSHORT(LIT_END),
  181.              szMsg);
  182.     fStarted=TRUE;          /* Sets the started flag for menu controls */
  183.     }
  184.    else {     /* FAILURE */
  185.     sprintf (szMsg," -> ERROR Application did not start successfully");
  186.     WinSendMsg(hwndList, LM_INSERTITEM,
  187.              MPFROMSHORT(LIT_END),
  188.              szMsg);
  189.     errorCode = WinGetLastError (hwndMain);
  190.     sprintf (szMsg," - > WinGetLastError was %x.", errorCode);
  191.     WinSendMsg(hwndList, LM_INSERTITEM,
  192.              MPFROMSHORT(LIT_END),
  193.              szMsg);
  194.     }
  195.  
  196. return;
  197.   mp2;   /* This is referenced to avoid unresolved variable warnings */
  198. }   /* End of StrApp   */
  199. /**************************************************************************
  200.  *
  201.  *  Name       : StopApp(mp2)
  202.  *
  203.  *  Description: Processes selection of the Terminate menu item of the
  204.  *               Control pulldown
  205.  *
  206.  *  Concepts:  called whenever Terminate from the Control menu is selected
  207.  *
  208.  *  API's      :  WinTerminateApp
  209.  *
  210.  *  Parameters :  mp2      = second message parameter
  211.  *
  212.  *  Return     :  [none]
  213.  *
  214.  *************************************************************************/
  215. VOID StopApp(MPARAM mp2) {
  216.  
  217. BOOL             Brc = FALSE;
  218. ERRORID          errorCode;
  219.    /* Reset PM last error */
  220.    errorCode = WinGetLastError (hwndMain);
  221. /*
  222.  The application to be terminated must have been started using the
  223.  WinStartApp function with the SAF_STARTCHILDAPP option specified.
  224.  
  225.  The WinTerminateApp function must be called from the same process as the
  226.  WinStartApp function.
  227. */
  228.  
  229.    Brc = WinTerminateApp(happ);  /* now terminate */
  230.  
  231.    if(!Brc) {
  232.     sprintf (szMsg," -> ERROR Application did not terminate");
  233.     WinSendMsg(hwndList, LM_INSERTITEM,
  234.              MPFROMSHORT(LIT_END),
  235.              szMsg);
  236.     errorCode = WinGetLastError (hwndMain);
  237.     sprintf (szMsg," - > WinGetLastError was %x.", errorCode);
  238.     WinSendMsg(hwndList, LM_INSERTITEM,
  239.              MPFROMSHORT(LIT_END),
  240.              szMsg);
  241.     }
  242.    else  {
  243.            fStarted=FALSE;
  244.          }
  245. return;
  246. }
  247. /***************************  End of sapp.c  ****************************/
  248.  
  249.