home *** CD-ROM | disk | FTP | other *** search
/ Windows Shareware GOLD / NuclearComputingVol3No1.cdr / _bbs1 / f3103.zip / SOURCE.TXT < prev   
Text File  |  1990-12-15  |  19KB  |  425 lines

  1. ----------------------------------------------------------------
  2. ----------------------------------------------------------------
  3. ----------------------------------------------------------------
  4. ----------------------------------------------------------------
  5. ----------------------------------------------------------------
  6. This text file contains four source files.  Use your editor to
  7. cut this file into these:
  8.  
  9.           lc_samp.c
  10.           lc_samp.h
  11.           lc_samp.mak
  12.           lc_samp.rc
  13.     
  14. ----------------------------------------------------------------
  15. ----------------------------------------------------------------
  16. ----------------------------------------------------------------
  17. ----------------------------------------------------------------
  18. ----------------------------------------------------------------
  19. ------------------  HERE BEGINS THE LC_SAMP.C FILE -------------
  20. /*  
  21.     Sample Non-DDE client program for LC.EXE
  22.     Garry J. Vass [72307,3311]
  23.     
  24.     This program (cloned from generic.c) uses the LC.EXE
  25.     Winapp to perform calculations on strings containing
  26.     expressions of the form:
  27.     
  28.          3+4+5+6
  29.          3*4
  30.          3/sin(3)*log(2)
  31.          and so on.
  32.          
  33.     For locating code that is specific to this application
  34.     (i.e., not generic), set your Search function for "COMMENT".
  35.     
  36.     Why not DDE?  
  37.     
  38.         1.  This particular client-server relationship
  39.             is straightforward, well structured, 
  40.             predictable, and requires expedience.
  41.         2.  About 12 lines of code for this versus
  42.             300+ lines to implement DDE.
  43.         3.  The downside is that the messages WM_USER+63
  44.             (request), and WM_USER+64 (reply) are
  45.             looked upon as private channels.  This is
  46.             conceptually similar to the old-world situation
  47.             where DOS resident programs argued over who
  48.             owned what hotkey (sigh).  I use a recognition
  49.             signature 0x4C43, 'L'<<4 + 'C', in the message
  50.             to establish ownership.
  51.  
  52.  
  53.                                                               */    
  54. /**************************************************************/
  55. /*                                                            */
  56. /*                                                            */
  57. /*                                                            */
  58. /*                                                            */
  59. /*                                                            */
  60. /**************************************************************/
  61. #include "windows.h"
  62. #include "lc_samp.h"
  63. int request_calculation( HWND sender, char *str );
  64. /**************************************************************/
  65. /*                                                            */
  66. /*                                                            */
  67. /*                                                            */
  68. /*                                                            */
  69. /*                                                            */
  70. /**************************************************************/
  71. HANDLE hInst;
  72. FARPROC Lproc;
  73. char Inputstring[ 255 ] = "2+2";
  74. char Resultstring[ 255 ] = "Result: 4";
  75. char Anystring[ 255 ];
  76. #define WAK( a ) MessageBox(  GetFocus(  ), a, "WIN-ACK", MB_OK  );
  77. /**************************************************************/
  78. /*                                                            */
  79. /*                                                            */
  80. /*                                                            */
  81. /*                                                            */
  82. /*                                                            */
  83. /**************************************************************/
  84. /*  COMMENT:
  85.     This variable is used to specify the handle (HWND) of
  86.     the LC application.
  87. */
  88. static HWND Lchwnd = -1;
  89. /**************************************************************/
  90. /*                                                            */
  91. /*                                                            */
  92. /*                                                            */
  93. /*                                                            */
  94. /*                                                            */
  95. /**************************************************************/
  96. int PASCAL WinMain( HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
  97. {
  98. MSG msg;
  99.     if( !hPrevInstance ) {
  100.         if( !InitApplication( hInstance ) )
  101.             return( FALSE );
  102.         /*  COMMENT:
  103.             This code starts the LC application.  If Windows
  104.             cannot start the program, she returns a value
  105.             less than 32.  
  106.         */
  107.         if( WinExec( "LC", SW_SHOWMINNOACTIVE ) < 32 ) {
  108.             MessageBox( GetFocus(), 
  109.                         (LPSTR)"I cannot find my server",
  110.                         (LPSTR)"ShowStopper", 
  111.                         MB_OK );
  112.             return( FALSE );
  113.         }
  114.     }
  115.     if( !InitInstance( hInstance, nCmdShow ) )
  116.         return( FALSE );
  117.     while( GetMessage( &msg, NULL, NULL, NULL ) ) {
  118.            TranslateMessage( &msg );
  119.            DispatchMessage( &msg );
  120.     }
  121.     return( msg.wParam );
  122. }
  123. /**************************************************************/
  124. /*                                                            */
  125. /*                                                            */
  126. /*                                                            */
  127. /*                                                            */
  128. /*                                                            */
  129. /**************************************************************/
  130. BOOL InitApplication( HANDLE hInstance )
  131. {
  132. WNDCLASS wc;
  133.      wc.style              = NULL;
  134.      wc.lpfnWndProc        = MainWndProc;
  135.      wc.cbClsExtra         = 0;
  136.      wc.cbWndExtra         = 0;
  137.      wc.hInstance          = hInstance;
  138.      wc.hIcon              = LoadIcon( NULL, IDI_APPLICATION );
  139.      wc.hCursor            = LoadCursor( NULL, IDC_ARROW );
  140.      wc.hbrBackground      = GetStockObject( WHITE_BRUSH );
  141.      wc.lpszMenuName       = "lcmenu";
  142.      wc.lpszClassName      = "lcclass";
  143.      return( RegisterClass( &wc ) );
  144. }
  145. /**************************************************************/
  146. /*                                                            */
  147. /*                                                            */
  148. /*                                                            */
  149. /*                                                            */
  150. /*                                                            */
  151. /**************************************************************/
  152. BOOL InitInstance( HANDLE hInstance, int nCmdShow )
  153. {
  154. HWND hWnd;
  155.      hInst = hInstance;
  156.      hWnd = CreateWindow( "lcclass",
  157.                           "LC Sample",
  158.                           WS_OVERLAPPEDWINDOW,
  159.                           CW_USEDEFAULT,
  160.                           CW_USEDEFAULT,
  161.                           CW_USEDEFAULT,
  162.                           CW_USEDEFAULT,
  163.                           NULL,
  164.                           NULL,
  165.                           hInstance,
  166.                           NULL );
  167.      if( !hWnd )
  168.          return( FALSE );
  169.      ShowWindow( hWnd, nCmdShow );
  170.      UpdateWindow( hWnd );
  171.      return( TRUE );
  172. }
  173. /**************************************************************/
  174. /*                                                            */
  175. /*                                                            */
  176. /*                                                            */
  177. /*                                                            */
  178. /*                                                            */
  179. /**************************************************************/
  180. long FAR PASCAL MainWndProc( HWND hWnd, unsigned message, WORD wParam, LONG lParam )
  181. {
  182.     switch( message ) {
  183.             case WM_COMMAND:
  184.                  if( wParam == IDM_ABOUT ) {
  185.                      Lproc = MakeProcInstance( About, hInst );
  186.                      DialogBox( hInst, "AboutBox", hWnd, Lproc );
  187.                      FreeProcInstance( Lproc );
  188.                      break;
  189.                  } else if( wParam == IDM_ABOUT+1 ) {
  190.                      /* COMMENT:
  191.                         This code opens the dialog box for
  192.                         entering expressions.
  193.                      */
  194.                      Lproc = MakeProcInstance( Samp, hInst );
  195.                      DialogBox( hInst, "LCSAMP", hWnd, Lproc );
  196.                      FreeProcInstance( Lproc );
  197.                      break;
  198.                  } else
  199.                      return( DefWindowProc( hWnd, message, wParam, lParam ) );
  200.             case WM_DESTROY:
  201.                  PostQuitMessage( 0 );
  202.                  break;
  203.             default:
  204.                  return( DefWindowProc( hWnd, message, wParam, lParam ) );
  205.     }
  206.     return( NULL );
  207. }
  208. /**************************************************************/
  209. /*                                                            */
  210. /*                                                            */
  211. /*                                                            */
  212. /*                                                            */
  213. /*                                                            */
  214. /**************************************************************/
  215. BOOL FAR PASCAL About( HWND hDlg, unsigned message, WORD wParam, LONG lParam )
  216. {
  217.     switch( message ) {
  218.             case WM_INITDIALOG:
  219.                  return( TRUE );
  220.             case WM_COMMAND:
  221.                  if( wParam == IDOK || wParam == IDCANCEL ) {
  222.                      EndDialog( hDlg, TRUE );
  223.                      return( TRUE );
  224.                  }
  225.                  break;
  226.     }
  227.     return( FALSE );
  228. }
  229. /**************************************************************/
  230. /*                                                            */
  231. /*                                                            */
  232. /*                                                            */
  233. /*                                                            */
  234. /*                                                            */
  235. /**************************************************************/
  236. BOOL FAR PASCAL Samp( HWND hDlg, unsigned message, WORD wParam, LONG lParam )
  237. {
  238. static int grab;
  239.  
  240.     switch( message ) {
  241.             case WM_INITDIALOG:
  242.                  SetDlgItemText( hDlg, 101, (LPSTR)Inputstring );
  243.                  return( TRUE );
  244.             case WM_COMMAND:
  245.                  if( wParam == 100 ) {
  246.                      EndDialog( hDlg, TRUE );
  247.                      return( TRUE );
  248.                  } else if( wParam == 107 ) { /* calc button */
  249.                      /*  COMMENT:
  250.                          When the calculate button has been pressed,
  251.                          this code acquires the string and prepares
  252.                          it for calculation.
  253.                      */
  254.                      GetDlgItemText( hDlg, 101, (LPSTR)Inputstring, 50 );
  255.                      request_calculation( hDlg, Inputstring );
  256.                  }
  257.                  break;
  258.             case WM_USER+64:
  259.                  /*  COMMENT:
  260.                      LC.EXE sends its results with the WM_USER+64
  261.                      message.  The format it uses is
  262.                              message:   WM_USER+64
  263.                              wParam:    The handle (HWND) to LC's window.
  264.                              lParam:    The low word contains a global
  265.                                         atom with LC's result.
  266.                                         The high word contains the
  267.                                         handle (HWND) of the application
  268.                                         that requested the service.
  269.                      The processing does not need to take place
  270.                      within a dialog callback, the reply message
  271.                      is sent to whatever handle requested the service.
  272.                  */
  273.                  if( HIWORD( lParam ) == hDlg ) {/*Is it ours?*/
  274.                      GlobalGetAtomName( LOWORD( lParam ), Anystring, 100 );
  275.                      SetDlgItemText( hDlg, 103, (LPSTR)Anystring );
  276.                      Lchwnd = (HWND)wParam;
  277.                  }
  278.                  break;
  279.     }
  280.     return( FALSE );
  281. }
  282. /**************************************************************/
  283. /*                                                            */
  284. /*                                                            */
  285. /*                                                            */
  286. /*                                                            */
  287. /*                                                            */
  288. /**************************************************************/
  289. int request_calculation( HWND sender, char *str )
  290. {
  291. ATOM anyatom;
  292.      /*  COMMENT:
  293.          This code prepares a string for calculation and sends
  294.          it to the server.  LC listens for WM_USER+63 messages
  295.          in the following format:
  296.          
  297.              message:  WM_USER+63
  298.              wParam:   The handle (HWND) of the sending,
  299.                        or client, window.
  300.              lParam:   The low word contains a global atom
  301.                        with the string to be calculated.
  302.                        The high word contains a recognition 
  303.                        signature, 0x4C43.
  304.      */
  305.      anyatom = GlobalAddAtom( (LPSTR) str );
  306.      SendMessage( Lchwnd, WM_USER+63, sender, MAKELONG( anyatom, 0x4C43 ) );
  307.      GlobalDeleteAtom( anyatom );
  308. }
  309. /**************************************************************/
  310. /*                                                            */
  311. /*                                                            */
  312. /*                                                            */
  313. /*                                                            */
  314. /*                                                            */
  315. /**************************************************************/
  316. ---------------------  HERE ENDS THE LC_SAMP.C FILE  -----------
  317. ----------------------------------------------------------------
  318. ----------------------------------------------------------------
  319. ----------------------------------------------------------------
  320. ----------------------------------------------------------------
  321. ----------------------------------------------------------------
  322.  
  323.  
  324.  
  325. ----------------------------------------------------------------
  326. ----------------------------------------------------------------
  327. ----------------------------------------------------------------
  328. ----------------------------------------------------------------
  329. ----------------------------------------------------------------
  330. ---------------------  HERE BEGINS THE LC_SAMP MAKE FILE  ------
  331. all: lc_samp.exe
  332.  
  333. lc_samp.res: lc_samp.rc lc_samp.h
  334.     rc -r lc_samp.rc
  335.  
  336. lc_samp.obj: lc_samp.c lc_samp.h
  337.     erase lc_samp.exe
  338.     cl -c -AS -Gsw -Oas -Zpe lc_samp.c
  339.  
  340. lc_samp.exe: lc_samp.obj lc_samp.def
  341.     link /NOD lc_samp,,, libw slibcew, lc_samp.def
  342.     rc lc_samp.res
  343.  
  344. lc_samp.exe: lc_samp.res
  345.     rc lc_samp.res
  346. -----------------------  HERE ENDS THE LC_SAMP MAKE FILE -------
  347. ----------------------------------------------------------------
  348. ----------------------------------------------------------------
  349. ----------------------------------------------------------------
  350. ----------------------------------------------------------------
  351. ----------------------------------------------------------------
  352.  
  353.  
  354. ----------------------------------------------------------------
  355. ----------------------------------------------------------------
  356. ----------------------------------------------------------------
  357. ----------------------------------------------------------------
  358. ----------------------------------------------------------------
  359. ---------------------  HERE BEGINS THE LC_SAMP.H FILE ----------
  360. #define IDM_ABOUT 100
  361. int PASCAL WinMain(HANDLE, HANDLE, LPSTR, int);
  362. BOOL InitApplication(HANDLE);
  363. BOOL InitInstance(HANDLE, int);
  364. long FAR PASCAL MainWndProc(HWND, unsigned, WORD, LONG);
  365. BOOL FAR PASCAL About(HWND, unsigned, WORD, LONG);
  366. BOOL FAR PASCAL Samp(HWND, unsigned, WORD, LONG);
  367. -----------------------  HERE ENDS THE LC_SAMP.H FILE ----------
  368. ----------------------------------------------------------------
  369. ----------------------------------------------------------------
  370. ----------------------------------------------------------------
  371. ----------------------------------------------------------------
  372. ----------------------------------------------------------------
  373.  
  374.  
  375. ----------------------------------------------------------------
  376. ----------------------------------------------------------------
  377. ----------------------------------------------------------------
  378. ----------------------------------------------------------------
  379. ----------------------------------------------------------------
  380. ----------------------  HERE BEGINS THE LC_SAMP.RC FILE --------
  381. #include "windows.h"
  382. #include "lc_samp.h"
  383.  
  384. lcmenu MENU
  385. BEGIN
  386.     POPUP "About"
  387.     BEGIN
  388.         MENUITEM "&About...", IDM_ABOUT
  389.     END
  390.     MENUITEM  "&Calculate", IDM_ABOUT+1
  391. END
  392.  
  393. LCSAMP DIALOG LOADONCALL MOVEABLE DISCARDABLE 11, 29, 258, 148
  394. CAPTION "LC-Sample"
  395. STYLE WS_BORDER | WS_CAPTION | WS_DLGFRAME | WS_SIZEBOX | WS_POPUP
  396. BEGIN
  397.     CONTROL "", 101, "edit", ES_LEFT | WS_BORDER | WS_TABSTOP | WS_CHILD, 140, 20, 70, 20
  398.     CONTROL "Press here to calculate", 107, "button", BS_PUSHBUTTON | WS_TABSTOP | WS_CHILD, 140, 50, 90, 20
  399.     CONTROL "Done", 100, "button", BS_PUSHBUTTON | WS_TABSTOP | WS_CHILD, 200, 130, 36, 14
  400.     CONTROL "Enter an expression here", 102, "static", SS_LEFT | WS_CHILD, 40, 20, 90, 10
  401.     CONTROL "Result:", 103, "static", SS_LEFT | WS_CHILD, 140, 80, 110, 10
  402.     CONTROL "Samples", 104, "static", SS_LEFT | WS_CHILD, 10, 40, 30, 10
  403.     CONTROL "3+4/(5+6)*(7*8)", 105, "static", SS_LEFT | WS_CHILD, 10, 60, 50, 10
  404.     CONTROL "2*sin(3)^3*sqr(7)", 106, "static", SS_LEFT | WS_CHILD, 10, 70, 60, 10
  405. END
  406.  
  407.  
  408.  
  409. AboutBox DIALOG 22, 17, 144, 75
  410. STYLE DS_MODALFRAME | WS_CAPTION | WS_SYSMENU
  411. CAPTION "About"
  412. BEGIN
  413.     CTEXT "Sample LC-Client" -1, 0, 5, 144, 8
  414.     DEFPUSHBUTTON "OK" IDOK, 53, 59, 32, 14, WS_GROUP
  415. END
  416. -----------------------  HERE ENDS THE LC_SAMP.RC FILE --------
  417. ---------------------------------------------------------------
  418. ---------------------------------------------------------------
  419. ---------------------------------------------------------------
  420. ---------------------------------------------------------------
  421. ---------------------------------------------------------------
  422.  
  423.  
  424.  
  425.