home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / memlink.zip / MDEMO.C next >
C/C++ Source or Header  |  1995-08-09  |  8KB  |  264 lines

  1. /*************************************************************************
  2. *  Filename:             mdemo.c
  3. *                  
  4. *  Description:          
  5. *            This module is meant to be a demo program to show
  6. *                       how the MEMLINK.DLL library works for Interprocess
  7. *                       Communication. 
  8. *************************************************************************/
  9.  
  10. #define INCL_GPI
  11. #define INCL_WIN
  12. #define INCL_DOS
  13. #include <os2.h>
  14. #include <stdlib.h>
  15. #include "mdemo.h"
  16. #include "pipes.h"
  17.  
  18. #define THREAD_STACK_SIZE    8000
  19.  
  20. /* function prototypes */
  21. static VOID APIENTRY thread1_routine(ULONG DummyValue);
  22. static VOID APIENTRY thread2_routine(ULONG DummyValue);
  23. static void HandleOutput(char *MsgString);
  24. static MRESULT EXPENTRY ClientWindowProc(HWND window, ULONG message,
  25.                                           MPARAM param1, MPARAM param2 );
  26. /* global variables */
  27. static HAB anchor_block;
  28. static char *pszMainWindowClass = "MEMLINK DEMO PROGRAM";
  29. static HWND  client_window;
  30. static HWND  main_window;
  31. static TID thread1_id, thread2_id;
  32. static short slowflag;
  33.  
  34. /*mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
  35. *  Description: This function is the main routine for the application.
  36. /*mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm*/
  37. int main( void )
  38. {
  39.  
  40.    HMQ   message_queue = NULLHANDLE;        
  41.    ULONG frame_flags   = 0UL;          
  42.    QMSG  message;                 
  43.    int   return_value  = 1;
  44.  
  45.    do
  46.      {
  47.      if (( anchor_block = WinInitialize(0UL)) == NULLHANDLE )
  48.        break;
  49.  
  50.      if (( message_queue = WinCreateMsgQueue( anchor_block, 0UL )) == 
  51.           NULLHANDLE )
  52.        break;
  53.  
  54.      if ( !WinRegisterClass( anchor_block, pszMainWindowClass, 
  55.             (PFNWP) ClientWindowProc, CS_SIZEREDRAW, 0UL ))       
  56.                break;
  57.  
  58.       /* Create the main window */
  59.       frame_flags = FCF_STANDARD & ~FCF_TASKLIST;
  60.       main_window =  WinCreateStdWindow( HWND_DESKTOP,  
  61.                                          WS_VISIBLE,    
  62.                                          &frame_flags,     
  63.                                          pszMainWindowClass,  
  64.                                          "",            
  65.                                          CS_SIZEREDRAW | WS_VISIBLE,
  66.                                          0UL,           
  67.                                          ID_WINDOW,     
  68.                                          &client_window );
  69.       if ( main_window == NULLHANDLE )
  70.         break;
  71.  
  72.       WinSetWindowText(main_window, pszMainWindowClass);
  73.  
  74.       while( WinGetMsg( anchor_block, &message, 0UL, 0UL, 0UL ) )
  75.         WinDispatchMsg( anchor_block, &message );  
  76.       return_value = 0;
  77.  
  78.    }  while ( FALSE );
  79.  
  80.    if ( main_window != NULLHANDLE )
  81.      WinDestroyWindow( main_window ); 
  82.  
  83.    if ( message_queue != NULLHANDLE )
  84.       WinDestroyMsgQueue( message_queue );
  85.  
  86.    if ( anchor_block != NULLHANDLE )
  87.      WinTerminate( anchor_block );
  88.  
  89.    return return_value;
  90. }
  91.  
  92.  
  93. /*mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
  94. *                                                                       
  95. *  Description: This function is the window procedure for the client
  96. *               window.
  97. *                                                                       
  98. /*mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm*/
  99. MRESULT EXPENTRY ClientWindowProc( HWND window, ULONG message, 
  100.                                    MPARAM param1, MPARAM param2 )
  101. {
  102.   APIRET    rc;
  103.   CONVCONTEXT   context;
  104.   RECTL         update_rect;
  105.   HPS           present_space;
  106.   HWND        ClientWnd, ServerWnd;
  107.   int           i;
  108.   static short  TimerFlag = 0;
  109.   short        MenuPick;
  110.  
  111.  
  112.   switch( message )
  113.     {
  114.     case WM_CLOSE:
  115.       WinPostMsg( window, WM_QUIT, NULL, NULL );
  116.       break;
  117.  
  118.     case WM_PAINT:
  119.       present_space = WinBeginPaint(window, NULLHANDLE, (PRECTL)&update_rect);
  120.       WinFillRect( present_space, (PRECTL)&update_rect, SYSCLR_WINDOW);
  121.       WinEndPaint( present_space );
  122.       break;
  123.  
  124.     case WM_COMMAND:
  125.       MenuPick = SHORT1FROMMP(param1);
  126.       switch (MenuPick)
  127.          {
  128.          case IDM_GOSLOW:
  129.          case IDM_GOFAST:
  130.               slowflag = (MenuPick == IDM_GOSLOW) ? 1 : 0;
  131.  
  132.           if (thread1_id || thread2_id) break;
  133.  
  134.           HandleOutput("Starting Thread1."); 
  135.               rc = DosCreateThread(&thread1_id, thread1_routine, 0L, 0L, THREAD_STACK_SIZE);
  136.               if (rc != 0) 
  137.               { 
  138.             DosBeep(4000,500); 
  139.             HandleOutput("Error in DosCreateThread()!  Could'nt start thread1." );
  140.           }
  141.  
  142.               rc = DosCreateThread(&thread2_id, thread2_routine, 0L, 0L, THREAD_STACK_SIZE);
  143.               if (rc != 0) 
  144.               { 
  145.             DosBeep(4000,500); 
  146.             HandleOutput("Error in DosCreateThread()!  Could'nt start thread2.");
  147.           }
  148.  
  149.           break;
  150.  
  151.          case IDM_STOP:
  152.              if (thread1_id) DosKillThread(thread1_id);
  153.              if (thread2_id) DosKillThread(thread2_id);
  154.              thread1_id = 0;
  155.              thread2_id = 0;
  156.          break;
  157.  
  158.          case IDM_EXITPROG:
  159.              WinPostMsg( window, WM_CLOSE, NULL, NULL );
  160.              break;
  161.          }
  162.       break;
  163.  
  164.     default:
  165.       return ( WinDefWindowProc( window, message, param1, param2 ) );
  166.     }
  167.  
  168.   return NULL;
  169. }
  170.  
  171. /*mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
  172. // thread1_routine    Routine that writes to the MEMLINK.DLL pipe used
  173. //            to communicate synchronously between thread1 (this 
  174. //                      thread) and thread2.  Thread1 writes the data
  175. //            to the MEMLINK pipe and thread2 reads it.
  176. //mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm*/
  177. static VOID APIENTRY thread1_routine(ULONG DummyValue)
  178. {
  179.     struct message m;
  180.     static long counter = 0;
  181.     char TempBuff[100];
  182.  
  183.     while (1)
  184.     {
  185.         counter++;
  186.         sprintf(m.Msg, "MESSAGE %d", counter);
  187.         sprintf(TempBuff, "thread1: sent %s", m.Msg); 
  188.     HandleOutput(TempBuff);
  189.     PipeWrite(PIPE_BOB, PIPE_END_SERVER, &m);
  190.         if (slowflag) DosSleep(1000);
  191.     }
  192. }
  193.  
  194. /*mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
  195. // thread2_routine    Routine that reads from the MEMLINK.DLL pipe used
  196. //            to communicate synchronously between thread1 and 
  197. //            thread2 (this thread).  Thread1 writes the data
  198. //            to the MEMLINK pipe and thread2 reads it.
  199. //mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm*/
  200. static VOID APIENTRY thread2_routine(ULONG DummyValue)
  201. {
  202.     struct message m;
  203.     char TempBuff[100];
  204.  
  205.     while (1)
  206.     {
  207.     PipeRead(PIPE_BOB, PIPE_END_CLIENT, &m);
  208.         sprintf(TempBuff, "thread2: received %s", m.Msg);
  209.     HandleOutput(TempBuff);
  210.     }
  211. }
  212.  
  213. /*mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
  214. // HandleOutput        Displays debug messages in test application window.
  215. //            Needs global variable "client_window" to be 
  216. //            initialized - the handle to the applications
  217. //            client window.
  218. //mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm*/
  219. static void HandleOutput(char *MsgString)
  220. {
  221.     RECTL Rectl;
  222.     POINTL Pointl;
  223.     FONTMETRICS FontMetrics;
  224.     static hPS = NULLHANDLE;
  225.     static int cyChar;
  226.     static int cxChar;
  227.     static int Top;
  228.     SIZEL  sizel;
  229.     LONG   i, Delay = 1;
  230.     float  f;
  231.  
  232.     WinQueryWindowRect(client_window, (PRECTL) &Rectl);
  233.  
  234.     if (hPS == NULLHANDLE)
  235.     {
  236.         sizel.cx = sizel.cy = 0;
  237.         hPS = GpiCreatePS(anchor_block, WinOpenWindowDC(client_window),
  238.             (PSIZEL) &sizel, PU_PELS | GPIF_DEFAULT |
  239.             GPIT_MICRO | GPIA_ASSOC);
  240.         GpiSetCharMode(hPS, CM_DEFAULT);
  241.         GpiSetCharSet(hPS, LCID_DEFAULT);
  242.         GpiQueryFontMetrics(hPS, sizeof(FONTMETRICS),
  243.             (PFONTMETRICS) &FontMetrics);
  244.         cyChar = FontMetrics.lMaxBaselineExt;
  245.         cxChar = FontMetrics.lAveCharWidth;
  246.         Top = Rectl.yTop - cyChar;
  247.     }
  248.  
  249.     /* set text position */
  250.     if (Top < Rectl.yBottom) 
  251.     {
  252.         Top = Rectl.yTop - cyChar;
  253.         WinInvalidateRect(client_window, 0, 0);
  254.     }
  255.         
  256.     Pointl.x = 5; 
  257.     Pointl.y = Top;    
  258.     Top -= cyChar;
  259.     
  260.     GpiCharStringAt(hPS, &Pointl, strlen(MsgString), MsgString);
  261. }
  262.  
  263.