home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR13 / TI_BC1.ZIP / TI1560.ZIP / TI1560.ASC
Text File  |  1993-10-05  |  10KB  |  463 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.   PRODUCT  :  Borland C++                           NUMBER  :  1560
  9.   VERSION  :  3.x
  10.        OS  :  Win
  11.      DATE  :  October 5, 1993                          PAGE  :  1/7
  12.  
  13.     TITLE  :  Spawning programs from within windows
  14.  
  15.  
  16.  
  17.  
  18.  
  19.      Included are three files wspawn.c, wspawn.h and test.c.
  20.      wspawn.c is an example of emulating the DOS spawn() function
  21.      in Windows. test.c excercises this function. wspawn.h
  22.      provides the prototypes and external definitions needed for
  23.      wspawn().
  24.  
  25.  
  26.   // ========= //
  27.   // WSPAWN.C  //
  28.   // ========= //
  29.  
  30.   // =============================================================
  31.   //
  32.   //  wspawn - creates and runs a child process in Windows
  33.   //
  34.   //  Syntax:
  35.   //     wspawn ( HINSTANCE hInst, int Mode, LPCSTR lpszCommand )
  36.   //
  37.   //  Remarks:
  38.   //
  39.   //     hInst - the hInstance of the parent task. If NULL,
  40.   //             wspawn() determines the instance of the current
  41.   //             task.
  42.   //
  43.   //     Mode - either WP_WAIT or WP_NOWAIT
  44.   //
  45.   //         WP_WAIT   - Puts parent process "on hold" until child
  46.   //                     process completes execution
  47.   //         WP_NOWAIT - Continues to run parent process while
  48.   //                     child process runs.
  49.   //
  50.   //     lpszCommand - long pointer to a zero terminated string
  51.   //                   that specifies the command _AND_ its
  52.   //                   arguments.
  53.   //
  54.   //
  55.   //     ** CAUTION **
  56.   //         You may want to modify the "while ( IsWaiting )" loop
  57.   //     in the wspawn() function.  This loop disables the parent
  58.   //     process until the child terminates.  However, Windows
  59.   //     will still allow mouse clicks on the parent possibly
  60.   //     filling up the message queue.  To circumvent this,
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.   PRODUCT  :  Borland C++                           NUMBER  :  1560
  75.   VERSION  :  3.x
  76.        OS  :  Win
  77.      DATE  :  October 5, 1993                          PAGE  :  2/7
  78.  
  79.     TITLE  :  Spawning programs from within windows
  80.  
  81.  
  82.  
  83.  
  84.   //     you may want to disable all the windows associated with
  85.   //     the parent task using EnumTaskWindows() and
  86.   //     EnableWindow(hwnd,FALSE) in wspawn() and then re-enable
  87.   //     the windows in WspawnNotifyFunc().
  88.   //
  89.   // =============================================================
  90.  
  91.  
  92.   ///////////////////////////////////////////////////////
  93.   #include <windows.h>
  94.   #include <toolhelp.h>      // for NotifyRegister()
  95.   #include <string.h>        // for strlen()
  96.   #include "wspawn.h"
  97.  
  98.  
  99.   //////////////////////////////////////////////////////
  100.   static BOOL IsWaiting = FALSE;
  101.   static HTASK hSpawnedTask;
  102.   static WORD WSRetVal;
  103.   static LPFNNOTIFYCALLBACK lpNotifyFunc;
  104.  
  105.  
  106.   //////////////////////////////////////////////////////
  107.   BOOL _export CALLBACK WspawnNotifyFunc ( WORD, DWORD );
  108.   static HTASK HINSTtoHTASK ( HINSTANCE );
  109.   static HINSTANCE HTASKtoHINST( HTASK );
  110.  
  111.  
  112.   ///////////////////////////////////////////////////////////
  113.   int wspawn( HINSTANCE hInst, int Mode, LPCSTR lpszCommand )
  114.   {
  115.       int nError;
  116.       HINSTANCE hSpawnedInst;
  117.  
  118.       // Check if wspawn is busy.
  119.       if ( IsWaiting )
  120.           return -1;
  121.  
  122.       // If hInst == NULL, determine hInst.
  123.       if ( hInst == NULL )
  124.       {
  125.           hInst = HTASKtoHINST( GetCurrentTask() );
  126.           if ( hInst == NULL )
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.   PRODUCT  :  Borland C++                           NUMBER  :  1560
  141.   VERSION  :  3.x
  142.        OS  :  Win
  143.      DATE  :  October 5, 1993                          PAGE  :  3/7
  144.  
  145.     TITLE  :  Spawning programs from within windows
  146.  
  147.  
  148.  
  149.  
  150.              return -1;
  151.       }
  152.  
  153.       switch ( Mode )
  154.       {
  155.           case WP_WAIT:
  156.               // Setup Notification
  157.               lpNotifyFunc = (LPFNNOTIFYCALLBACK)
  158.                               MakeProcInstance
  159.                               ( (FARPROC)WspawnNotifyFunc, hInst );
  160.  
  161.  
  162.               nError = NotifyRegister
  163.                        ( GetCurrentTask(),lpNotifyFunc,NF_NORMAL );
  164.               if ( !nError )
  165.                   return -1;
  166.  
  167.               // Execute command
  168.               hSpawnedInst = WinExec ( lpszCommand, SW_SHOW );
  169.               if ( hSpawnedInst < HINSTANCE_ERROR )
  170.                   return -1;
  171.  
  172.               IsWaiting = TRUE;
  173.               hSpawnedTask = HINSTtoHTASK( hSpawnedInst );
  174.  
  175.               // The following merely Yields while waiting for the
  176.               // child process to terminate... This approach
  177.               // prevents the user from closing the parent but may
  178.               // result in a system queue overflow if the user
  179.               // insists on reactivating the parent [with mouse
  180.               // clicks for example. An alternate method is to
  181.               // start a PeekMessage loop. The disadvantage of the
  182.               // PeekMessage loop is that the user may terminate
  183.               // the parent before the child process terminates
  184.               //
  185.   #if 1       // Change this to '#if 0' to use the PeekMessage
  186.   method
  187.               //
  188.               while ( IsWaiting )
  189.                   Yield();
  190.   #else
  191.               while( IsWaiting )
  192.               {
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.   PRODUCT  :  Borland C++                           NUMBER  :  1560
  207.   VERSION  :  3.x
  208.        OS  :  Win
  209.      DATE  :  October 5, 1993                          PAGE  :  4/7
  210.  
  211.     TITLE  :  Spawning programs from within windows
  212.  
  213.  
  214.  
  215.  
  216.                   MSG msg;
  217.                   if ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
  218.                   {
  219.                       if ( msg.message == WM_QUIT )
  220.                       {
  221.                           PostQuitMessage( msg.wParam );
  222.                           return -2;
  223.                       }
  224.                       else
  225.                       {
  226.                           TranslateMessage(&msg);
  227.                           DispatchMessage(&msg);
  228.                       }
  229.                   }
  230.               }
  231.   #endif
  232.               break;
  233.  
  234.           case WP_NOWAIT:
  235.               hSpawnedInst =
  236.                   WinExec ( lpszCommand, SW_SHOWMINNOACTIVE );
  237.               if ( hSpawnedInst < HINSTANCE_ERROR )
  238.                  return -1;
  239.               WSRetVal = 0;
  240.               break;
  241.  
  242.       }
  243.       return WSRetVal;
  244.   }
  245.  
  246.  
  247.   /////////////////////////////////////////////////////////////////
  248.   BOOL _export CALLBACK WspawnNotifyFunc ( WORD wID, DWORD dwData )
  249.   {
  250.       if ( wID == NFY_EXITTASK && GetCurrentTask() == hSpawnedTask
  251.   )
  252.       {
  253.           IsWaiting = FALSE;
  254.           WSRetVal = LOWORD(dwData);
  255.       }
  256.       return FALSE;
  257.   }
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.   PRODUCT  :  Borland C++                           NUMBER  :  1560
  273.   VERSION  :  3.x
  274.        OS  :  Win
  275.      DATE  :  October 5, 1993                          PAGE  :  5/7
  276.  
  277.     TITLE  :  Spawning programs from within windows
  278.  
  279.  
  280.  
  281.  
  282.   ///////////////////////////////////////
  283.   HTASK HINSTtoHTASK ( HINSTANCE hInst )
  284.   {
  285.       TASKENTRY TaskEntry;
  286.  
  287.       TaskEntry.dwSize = sizeof(TaskEntry);
  288.       if ( TaskFirst( &TaskEntry ) )
  289.       do
  290.       {
  291.           if ( TaskEntry.hInst == hInst )
  292.               return TaskEntry.hTask;
  293.       } while( TaskNext( &TaskEntry ) );
  294.       return 0;
  295.   }
  296.  
  297.  
  298.   /////////////////////////////////////
  299.   HINSTANCE HTASKtoHINST( HTASK hTask )
  300.   {
  301.       TASKENTRY TaskEntry;
  302.  
  303.       TaskEntry.dwSize = sizeof(TaskEntry);
  304.       TaskFindHandle( &TaskEntry, hTask );
  305.  
  306.       return TaskEntry.hInst;
  307.   }
  308.  
  309.  
  310.  
  311.   // ========= //
  312.   // WSPAWN.H  //
  313.   // ========= //
  314.  
  315.  
  316.   #if !defined(__WSPAWN_H)
  317.   #define      __WSPAWN_H
  318.  
  319.   #include <windows.h>
  320.  
  321.   #define    WP_WAIT     0
  322.   #define    WP_NOWAIT   1
  323.  
  324.   #if defined(__cplusplus)
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.   PRODUCT  :  Borland C++                           NUMBER  :  1560
  339.   VERSION  :  3.x
  340.        OS  :  Win
  341.      DATE  :  October 5, 1993                          PAGE  :  6/7
  342.  
  343.     TITLE  :  Spawning programs from within windows
  344.  
  345.  
  346.  
  347.  
  348.   extern "C" {
  349.   #endif
  350.  
  351.   int wspawn( HINSTANCE hInst, int Mode, LPCSTR lpszCommand );
  352.  
  353.   #if defined(__cplusplus)
  354.   }
  355.   #endif
  356.  
  357.   #endif  // __WSPAWN_H
  358.  
  359.  
  360.  
  361.  
  362.   // ======= //
  363.   // TEST.C  //
  364.   // ======= //
  365.  
  366.   //--------------------------------------------------------------
  367.   //
  368.   //  test.c - test program for wspawn()
  369.   //
  370.   //
  371.   //  Setup:
  372.   //     To create test.exe either
  373.   //
  374.   //         1. Compile via the command-line ( Borland C++ only )
  375.   //
  376.   //                 bcc -ml -W -v test.c wspawn.c
  377.   //
  378.   //
  379.   //         2. Build and compile a project in the IDE
  380.   //            ( BCW or TCW )
  381.   //
  382.   //               - Project|Open  test.prj
  383.   //               - Project|Add Item...  test.c & wspawn.c
  384.   //               - Compile|Build All
  385.   //
  386.   //  Remarks:
  387.   //     This test program spawns a DOS Shell using the
  388.   //     DOSPRMPT.PIF file located in the \windows directory.
  389.   //     Adjust the parameter below to match your directory setup.
  390.   //
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.  
  401.  
  402.  
  403.  
  404.   PRODUCT  :  Borland C++                           NUMBER  :  1560
  405.   VERSION  :  3.x
  406.        OS  :  Win
  407.      DATE  :  October 5, 1993                          PAGE  :  7/7
  408.  
  409.     TITLE  :  Spawning programs from within windows
  410.  
  411.  
  412.  
  413.  
  414.   //--------------------------------------------------------------
  415.  
  416.   #include <windows.h>
  417.   #include <stdio.h>
  418.   #include "wspawn.h"
  419.  
  420.  
  421.   void main(void)
  422.   {
  423.      printf( "Spawning DOS IDE...\n" );
  424.  
  425.      wspawn( NULL, WP_WAIT, "\\windows\\dosprmpt.pif dir" );
  426.  
  427.      printf( "\nFinished..." );
  428.   }
  429.  
  430.  
  431.   DISCLAIMER: You have the right to use this technical information
  432.   subject to the terms of the No-Nonsense License Statement that
  433.   you received with the Borland product to which this information
  434.   pertains.
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.