home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 19.ddi / SAMPLES / DBWIN / COMOUT.C_ / COMOUT.C
Encoding:
C/C++ Source or Header  |  1993-02-08  |  2.9 KB  |  147 lines

  1. //
  2. // COM debug output routines
  3. //
  4. // WARNING - PLEASE NOTE
  5. //
  6. // The techniques employed in this file for performing COM output are
  7. // appropriate for debugging purposes only.
  8. //
  9. // The COM file is opened using DOS in the context of the DBWIN.EXE
  10. // application, but may be written to in the context of any application
  11. // that calls OutputDebugString() or causes some other ToolHelp notification.
  12. //
  13. // This is accomplished by switching the DOS PSP (which defines the DOS
  14. // open-file context) of the currently running task to that of DBWIN.EXE,
  15. // performing the output, then switching back.
  16. //
  17. // The ComOut() routine must NOT be called while inside of DOS or during
  18. // interrupt processing.
  19. //
  20. #include "dbwindlp.h"
  21.  
  22. #pragma optimize("gle", off)        // suppress compiler warnings
  23.  
  24. // DOS INT 21 AH function codes
  25. #define DOS_SETPSP  50h
  26. #define DOS_GETPSP  51h
  27.  
  28. HFILE hfCom = -1;
  29. HANDLE pspOpen = NULL;
  30.  
  31. BOOL ComQuery(int port)
  32. {
  33.     if (ComOpen(port))
  34.     {
  35.         ComClose();
  36.         return TRUE;
  37.     }
  38.     return FALSE;
  39. }
  40.  
  41. BOOL ComOpen(int port)
  42. {
  43.     // Return FALSE if port is already open.
  44.     //
  45.     if (hfCom != -1)
  46.         return FALSE;
  47.  
  48.     // Save a copy of caller's PSP
  49.     //
  50.     _asm
  51.     {
  52.         mov     ah,DOS_GETPSP   ; pspOpen = GetPSP();
  53.         int     21h
  54.         mov     pspOpen,bx
  55.     }
  56.  
  57.     hfCom =  _lopen(port == 2 ? "COM2" : "COM1", READ_WRITE);
  58.  
  59.     return hfCom != -1;
  60. }
  61.  
  62. BOOL ComClose(void)
  63. {
  64.     BOOL fSuccess;
  65.  
  66.     _asm
  67.     {
  68.         mov     bx,pspOpen      ; SetPSP(pspOpen);
  69.         mov     ah,DOS_SETPSP
  70.         int     21h
  71.     }
  72.  
  73.     fSuccess = (_lclose(hfCom) == NULL);
  74.  
  75.     hfCom = -1;
  76.  
  77.     return fSuccess;
  78. }
  79.  
  80. BOOL ComOut(LPCSTR lpsz)
  81. {
  82.     HANDLE pspSave;
  83.     UINT cch = lstrlen(lpsz);
  84.     UINT cchWritten;
  85.  
  86.     if (hfCom == -1)
  87.         return FALSE;
  88.  
  89.     _asm
  90.     {
  91.         mov     ah,DOS_GETPSP   ; pspSave = GetPSP();
  92.         int     21h
  93.         mov     pspSave,bx
  94.  
  95.         mov     bx,pspOpen      ; SetPSP(pspOpen);
  96.         mov     ah,DOS_SETPSP
  97.         int     21h
  98.     }
  99.  
  100.     cchWritten = _lwrite(hfCom, lpsz, cch);
  101.  
  102.     _asm
  103.     {
  104.         mov     bx,pspSave      ; SetPSP(pspSave);
  105.         mov     ah,DOS_SETPSP
  106.         int     21h
  107.     }
  108.  
  109.     return cch != cchWritten;
  110. }
  111.  
  112. int ComIn(void)
  113. {
  114.     char ch;
  115.     HANDLE pspSave;
  116.  
  117.     if (hfCom == -1)
  118.         return 0;
  119.  
  120.     _asm
  121.     {
  122.         mov     ah,DOS_GETPSP   ; pspSave = GetPSP();
  123.         int     21h
  124.         mov     pspSave,bx
  125.  
  126.         mov     bx,pspOpen      ; SetPSP(pspOpen);
  127.         mov     ah,DOS_SETPSP
  128.         int     21h
  129.     }
  130.  
  131.     // Read a single character.
  132.     //
  133.     while (_lread(hfCom, &ch, 1) == 0)
  134.         ;
  135.  
  136.     _asm
  137.     {
  138.         mov     bx,pspSave      ; SetPSP(pspSave);
  139.         mov     ah,DOS_SETPSP
  140.         int     21h
  141.     }
  142.  
  143.     return ch;
  144. }
  145.  
  146. #pragma optimize("", on)
  147.