home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_lrn / spawn / winspawn.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-11  |  7.7 KB  |  248 lines

  1. /*
  2.  
  3. Function(s) demonstrated in this program: Int21Function4B, LockSegment, 
  4.    UnlockSegment
  5.  
  6. Windows version:  2.03
  7.  
  8. Windows SDK version:  2.00
  9.  
  10. Compiler version:  C 5.10
  11.  
  12. Description:  Int21Function4B is a Function created using Masm, and it 
  13.    corresponds with the MS-DOS interrupt 21 function 4BH.  The function
  14.    LockSegment locks the segment whose segment address is specified
  15.     by the parameter.  If the parameter is -1 the function locks the 
  16.    current data segment.  The function UnlockSegment unlocks the segment
  17.    whose address is specified by the parameter, -1 indicates current
  18.     segment.
  19.  
  20. Additional Comments:  Int21Function4B can be used to spawn windows
  21.    applications, as well as old MS-DOS applications from within a 
  22.    windows application.  A method of sending command line arguments
  23.    to the spawnded windows application is also discussed.
  24.  
  25. */
  26.  
  27. #define NOMINMAX
  28. #include <windows.h>
  29. #include <stdlib.h>
  30. #include <stdio.h>
  31. #include "WinSpawn.h"
  32.  
  33. HWND     hWndMain;
  34. HANDLE   hInstMain;
  35. char     szOutputBuffer1 [70];
  36. char     szOutputBuffer2 [500];
  37. char     szSpawnAppName  [40];
  38. char     szCommandLine   [40];
  39.  
  40. typedef struct {WORD   environment;
  41.                 LPSTR  commandline;
  42.                 LPSTR  FCB1;
  43.                 LPSTR  FCB2;
  44.                } EXECBLOCK;
  45. EXECBLOCK      exec;
  46. WORD           w[2];
  47.  
  48.  
  49. /****************************************************************************/
  50. /************************    Message Structure      *************************/
  51. /****************************************************************************/
  52.  
  53. struct { char *szMessage; }
  54.        Messages [] = {
  55. "About",
  56. "     This is a sample application to demonstrate the\n\
  57. use of the LockSegment and UnlockSegment Windows\n\
  58. Functions, as well as Interrupt 21 function 4B in\n\
  59. spawning Windows applications, and MS-DOS applications\n\
  60. from a Windows Windows application.",
  61.  
  62. "Help Message",
  63. "     This program demonstrates the use of the\n\
  64. Windows Function LockSegment, UnlockSegment, and\n\
  65. Int21Function4B.  Use the menu to select either a\n\
  66. MS-DOS application or a Windows application to be\n\
  67. spawned.  The program will also send command line\n\
  68. arguments to the spawned application.",
  69.  
  70. "LockSegment",
  71. "    The current data segment has been locked.",
  72.  
  73. "UnlockSegment",
  74. "    The current data segment has been unlocked.",
  75.  
  76. };    
  77.  
  78. /****************************************************************************/
  79.  
  80. void ProcessMessage (HWND, int); 
  81.  
  82. void ProcessMessage (hWnd, MessageNumber) 
  83.      HWND     hWnd;
  84.      int      MessageNumber;
  85. {
  86.      sprintf (szOutputBuffer1, "%s", Messages [MessageNumber]);
  87.      sprintf (szOutputBuffer2, "%s", Messages [MessageNumber + 1]);
  88.      MessageBox (hWnd, szOutputBuffer2, szOutputBuffer1, MB_OK);
  89. }       
  90.  
  91. /****************************************************************************/
  92.  
  93. int PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
  94.      HANDLE      hInstance, hPrevInstance ;
  95.      LPSTR       lpszCmdLine ;
  96.      int         nCmdShow ;
  97.      {
  98.      static char szAppName [] = "WinSpawn" ;
  99.      static char szChildClass [] = "WinSpawnChild" ;
  100.      HWND        hWnd ;
  101.      WNDCLASS    wndclass ;
  102.      MSG msg;
  103.  
  104.      if (!hPrevInstance) 
  105.           {
  106.           wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  107.           wndclass.lpfnWndProc   = WndProc ;
  108.           wndclass.cbClsExtra    = 0 ;
  109.           wndclass.cbWndExtra    = 0 ;
  110.           wndclass.hInstance     = hInstance ;
  111.           wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  112.           wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  113.           wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  114.           wndclass.lpszMenuName  = szAppName ;
  115.           wndclass.lpszClassName = szAppName ;
  116.  
  117.           if (!RegisterClass (&wndclass))
  118.                return FALSE ;
  119.  
  120.           }
  121.  
  122.      hWnd = CreateWindow (szAppName,            /* window class name       */
  123.                     "Windows Spawn",            /* window caption          */
  124.                     WS_OVERLAPPEDWINDOW,        /* window style            */
  125.                     CW_USEDEFAULT,              /* initial x position      */
  126.                     0,                          /* initial y position      */
  127.                     CW_USEDEFAULT,              /* initial x size          */
  128.                     0,                          /* initial y size          */
  129.                     NULL,                       /* parent window handle    */
  130.                     NULL,                       /* window menu handle      */
  131.                     hInstance,                  /* program instance handle */
  132.                     NULL) ;                     /* create parameters       */
  133.  
  134.      ShowWindow (hWnd, nCmdShow) ;
  135.      UpdateWindow (hWnd) ;
  136.  
  137.      hWndMain  = hWnd;
  138.      hInstMain = hInstance;
  139.  
  140.      while (GetMessage(&msg, NULL, 0, 0))
  141.      {
  142.       TranslateMessage(&msg);
  143.       DispatchMessage(&msg);
  144.      } 
  145.      return (msg.wParam) ;     
  146.      }
  147.  
  148. /****************************************************************************/
  149.  
  150. long FAR PASCAL WndProc (hWnd, iMessage, wParam, lParam)
  151. HWND     hWnd ;
  152. unsigned iMessage ;
  153. WORD     wParam ;
  154. LONG     lParam ;
  155. {
  156.  HMENU       hMenu;
  157.  PAINTSTRUCT ps;
  158.  
  159.  switch(iMessage)
  160.  {
  161.   case WM_CREATE:
  162.        hMenu = GetSystemMenu (hWnd, FALSE);
  163.  
  164.        ChangeMenu (hMenu, NULL, "&About", IDM_ABOUT, 
  165.                    MF_APPEND | MF_STRING);
  166.        break;
  167.  
  168.   case WM_SYSCOMMAND:
  169.        switch (wParam) {
  170.           case IDM_ABOUT:
  171.                ProcessMessage (hWnd, 0);
  172.                break;
  173.           default:
  174.                return DefWindowProc (hWnd, iMessage, wParam, lParam) ;
  175.        }
  176.        break;
  177.  
  178.   case WM_COMMAND:
  179.        switch (wParam) {
  180.  
  181.           case IDM_SPAWNIT:
  182.                GlobalCompact(-1L);
  183.                LockSegment (-1);
  184.                ProcessMessage (hWnd, 4);
  185.  
  186.                exec.environment = 0;
  187.                exec.commandline = szCommandLine;
  188.  
  189.                w[0] = 2;
  190.                w[1] = SW_SHOWNORMAL;
  191.                exec.FCB1 = (LPSTR)w;
  192.                exec.FCB2 = (LPSTR)NULL;
  193.  
  194.                Int21Function4B(0, (LPSTR)szSpawnAppName,
  195.                                (LPSTR)&exec);
  196.  
  197.                UnlockSegment (-1);
  198.                ProcessMessage (hWnd, 6);
  199.                break;
  200.  
  201.           case IDM_CLOCK:
  202.                sprintf (szSpawnAppName,"CLOCK.EXE");
  203.                sprintf (szCommandLine, "\0\r");
  204.                SendMessage (hWnd, WM_COMMAND, IDM_SPAWNIT, 0L);
  205.                break;
  206.  
  207.           case IDM_DOSDIRSORT:            
  208.                sprintf (szSpawnAppName,"COMMAND.COM");
  209.                sprintf (szCommandLine, "%c /c dir | sort\0\r", 14);
  210.                SendMessage (hWnd, WM_COMMAND, IDM_SPAWNIT, 0L);
  211.                break;
  212.  
  213.           case IDM_DOSTYPE:            
  214.                sprintf (szSpawnAppName,"COMMAND.COM");
  215.                sprintf (szCommandLine, "%c /c type winspawn.c\0\r", 0x13);
  216.                SendMessage (hWnd, WM_COMMAND, IDM_SPAWNIT, 0L);
  217.                break;
  218.  
  219.           case IDM_NOTEPAD:
  220.                sprintf (szSpawnAppName,"NOTEPAD.EXE");
  221.                sprintf (szCommandLine, " WINSPAWN.C\0\r");
  222.                SendMessage (hWnd, WM_COMMAND, IDM_SPAWNIT, 0L);
  223.                break;
  224.  
  225.  
  226.           case IDM_HELP:
  227.                ProcessMessage (hWnd, 2);
  228.                break;
  229.        }
  230.        break;
  231.  
  232.   case WM_PAINT:
  233.        BeginPaint(hWnd, (LPPAINTSTRUCT)&ps);
  234.        EndPaint(hWnd, (LPPAINTSTRUCT)&ps);
  235.        break;
  236.  
  237.   case WM_DESTROY:
  238.        PostQuitMessage(0);
  239.        break;
  240.  
  241.   default:
  242.   {
  243.    return DefWindowProc (hWnd, iMessage, wParam, lParam) ;
  244.   }
  245.  }
  246.  return (0L); 
  247. }
  248.