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

  1. /*
  2.  *
  3.  *  Function demonstrated in this program: FlushComm
  4.  *  
  5.  *  This program demonstrates the use of the function FlushComm.
  6.  *  This function flushes all characters from the transmit or recieve
  7.  *  queue of the communication device specified by the first parameter.
  8.  *  The second parameter speifies which queue (transmit or receive) to
  9.  *  be flushed.
  10.  *  
  11.  */
  12.  
  13. #include <windows.h>
  14.  
  15. static char    szAppName[] = "flushcom";
  16. static char    szFuncName[] = "FlushComm";
  17.  
  18. long    FAR PASCAL WndProc (HANDLE, unsigned, WORD, LONG);
  19.  
  20. int     PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, cmdShow)
  21. HANDLE hInstance, hPrevInstance;
  22. LPSTR  lpszCmdLine;
  23. int    cmdShow;
  24.   {
  25.   HWND     hWnd;
  26.   WNDCLASS rClass;
  27.   MSG      msg;
  28.  
  29.   if (!hPrevInstance)
  30.     {
  31.     rClass.style         = CS_HREDRAW | CS_VREDRAW;
  32.     rClass.lpfnWndProc   = WndProc;
  33.     rClass.cbClsExtra    = 0;
  34.     rClass.cbWndExtra    = 0;
  35.     rClass.hInstance     = hInstance;
  36.     rClass.hCursor       = LoadCursor (NULL, IDC_ARROW);
  37.     rClass.hIcon         = LoadIcon (hInstance, IDI_APPLICATION);
  38.     rClass.hbrBackground = GetStockObject (WHITE_BRUSH);
  39.     rClass.lpszMenuName  = NULL;
  40.     rClass.lpszClassName = szAppName;
  41.  
  42.     if (!RegisterClass (&rClass))
  43.       return FALSE;
  44.     }
  45.  
  46.   hWnd = CreateWindow (szAppName,
  47.                       szFuncName,
  48.                       WS_OVERLAPPEDWINDOW,
  49.                       CW_USEDEFAULT,
  50.                       0,
  51.                       CW_USEDEFAULT,
  52.                       0,
  53.                       NULL,
  54.                       NULL,
  55.                       hInstance,
  56.                       NULL);
  57.  
  58.   ShowWindow (hWnd, cmdShow);
  59.   UpdateWindow (hWnd);
  60.  
  61.   while (GetMessage ( (LPMSG) & msg, NULL, 0, 0))
  62.     {
  63.     TranslateMessage ( (LPMSG) & msg);
  64.     DispatchMessage ( (LPMSG) & msg);
  65.     }
  66.   return (msg.wParam);
  67.   }
  68.  
  69. long    FAR PASCAL WndProc (hWnd, iMessage, wParam, lParam)
  70. HWND     hWnd;
  71. unsigned iMessage;
  72. WORD     wParam;
  73. LONG     lParam;
  74.   {
  75.   HMENU hMenu;
  76.   BOOL  bError;
  77.   int    nCid;
  78.   int    nResult;
  79.  
  80.   switch (iMessage)
  81.     {
  82.     case WM_CREATE:
  83.       hMenu = CreateMenu ();
  84.       ChangeMenu (hMenu, NULL, (LPSTR)"Flush Port", 100, MF_APPEND);
  85.       SetMenu (hWnd, hMenu);
  86.       break;
  87.  
  88.     case WM_COMMAND:
  89.       if (wParam == 100)
  90.         {
  91.         bError = FALSE;
  92.         nCid = OpenComm ( (LPSTR) "LPT1", 255, 256);
  93.  
  94.         if ( (nCid & IE_BADID) == IE_BADID)
  95.           {
  96.           MessageBox (hWnd, (LPSTR)"Invalid or unsupported ID.",
  97.               (LPSTR)szFuncName, MB_OK);
  98.           bError = TRUE;
  99.           }
  100.         if ( (nCid & IE_BAUDRATE) == IE_BAUDRATE)
  101.           {
  102.           MessageBox (hWnd, (LPSTR)"Unsupported baud rate.",
  103.               (LPSTR)szFuncName, MB_OK);
  104.           bError = TRUE;
  105.           }
  106.         if ( (nCid & IE_BYTESIZE) == IE_BYTESIZE)
  107.           {
  108.           MessageBox (hWnd, (LPSTR)"Invalid byte size.", (LPSTR)szFuncName,
  109.               MB_OK);
  110.           bError = TRUE;
  111.           }
  112.         if ( (nCid & IE_DEFAULT) == IE_DEFAULT)
  113.           {
  114.           MessageBox (hWnd, (LPSTR)"Error in default parameters.",
  115.               (LPSTR)szFuncName, MB_OK);
  116.           bError = TRUE;
  117.           }
  118.         if ( (nCid & IE_HARDWARE) == IE_HARDWARE)
  119.           {
  120.           MessageBox (hWnd, (LPSTR)"Hardware not present.", (LPSTR)szFuncName,
  121.               MB_OK);
  122.           bError = TRUE;
  123.           }
  124.         if ( (nCid & IE_MEMORY) == IE_MEMORY)
  125.           {
  126.           MessageBox (hWnd, (LPSTR)"Device not open.", (LPSTR)szFuncName,
  127.               MB_OK);
  128.           bError = TRUE;
  129.           }
  130.         if ( (nCid & IE_NOPEN) == IE_NOPEN)
  131.           {
  132.           MessageBox (hWnd, (LPSTR)"Device not opened.", (LPSTR)szFuncName,
  133.               MB_OK);
  134.           bError = TRUE;
  135.           }
  136.         if ( (nCid & IE_OPEN) == IE_OPEN)
  137.           {
  138.           MessageBox (hWnd, (LPSTR)"Device already open.", (LPSTR)szFuncName,
  139.               MB_OK);
  140.           bError = TRUE;
  141.           }
  142.         if (!bError)
  143.           {
  144.           MessageBox (hWnd, (LPSTR)"Flushing LPT1", (LPSTR)szFuncName, MB_OK);
  145.           nResult = FlushComm (nCid, 0);
  146.           if (nResult == 0)
  147.             MessageBox (hWnd, (LPSTR)"LPT1 flushed", (LPSTR)szFuncName,
  148.                 MB_OK);
  149.           else
  150.             MessageBox (hWnd, (LPSTR)"LPT1 not flushed", (LPSTR)szFuncName,
  151.                 MB_OK);
  152.           }
  153.         CloseComm (nCid);
  154.         }
  155.       break;
  156.  
  157.     case WM_DESTROY:
  158.       PostQuitMessage (0);
  159.       break;
  160.  
  161.     default:
  162.       return (DefWindowProc (hWnd, iMessage, wParam, lParam));
  163.     }
  164.   return (0L);
  165.   }
  166.