home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / initcon.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  3KB  |  131 lines

  1. /***
  2. *initcon.c - direct console I/O initialization and termination for Win32
  3. *
  4. *       Copyright (c) 1991-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       Defines __initconin() and _initconout() and __termcon() routines.
  8. *       The first two are called on demand to initialize _coninpfh and
  9. *       _confh, and the third is called indirectly by CRTL termination.
  10. *
  11. *       NOTE:   The __termcon() routine is called indirectly by the C/C++
  12. *               Run-Time Library termination code.
  13. *
  14. *******************************************************************************/
  15.  
  16. #include <cruntime.h>
  17. #include <internal.h>
  18. #include <oscalls.h>
  19.  
  20. void __cdecl __termcon(void);
  21.  
  22. #ifdef _MSC_VER
  23.  
  24. #pragma data_seg(".CRT$XPX")
  25. static  _PVFV pterm = __termcon;
  26.  
  27. #pragma data_seg()
  28.  
  29. #endif  /* _MSC_VER */
  30.  
  31. /*
  32.  * define console handles. these definitions cause this file to be linked
  33.  * in if one of the direct console I/O functions is referenced.
  34.  * The value (-2) is used to indicate the un-initialized state.
  35.  */
  36. int _coninpfh = -2;     /* console input */
  37. int _confh = -2;        /* console output */
  38.  
  39.  
  40. /***
  41. *void __initconin(void) - open handles for console input
  42. *
  43. *Purpose:
  44. *       Opens handle for console input.
  45. *
  46. *Entry:
  47. *       None.
  48. *
  49. *Exit:
  50. *       No return value. If successful, the handle value is copied into the
  51. *       global variable _coninpfh.  Otherwise _coninpfh is set to -1.
  52. *
  53. *Exceptions:
  54. *
  55. *******************************************************************************/
  56.  
  57. void __cdecl __initconin (
  58.         void
  59.         )
  60. {
  61.         _coninpfh = (int)CreateFile( "CONIN$",
  62.                                      GENERIC_READ | GENERIC_WRITE,
  63.                                      FILE_SHARE_READ | FILE_SHARE_WRITE,
  64.                                      NULL,
  65.                                      OPEN_EXISTING,
  66.                                      0,
  67.                                      NULL );
  68.  
  69. }
  70.  
  71.  
  72. /***
  73. *void __initconout(void) - open handles for console output
  74. *
  75. *Purpose:
  76. *       Opens handle for console output.
  77. *
  78. *Entry:
  79. *       None.
  80. *
  81. *Exit:
  82. *       No return value. If successful, the handle value is copied into the
  83. *       global variable _confh.  Otherwise _confh is set to -1.
  84. *
  85. *Exceptions:
  86. *
  87. *******************************************************************************/
  88.  
  89. void __cdecl __initconout (
  90.         void
  91.         )
  92. {
  93.         _confh = (int)CreateFile( "CONOUT$",
  94.                                   GENERIC_WRITE,
  95.                                   FILE_SHARE_READ | FILE_SHARE_WRITE,
  96.                                   NULL,
  97.                                   OPEN_EXISTING,
  98.                                   0,
  99.                                   NULL );
  100. }
  101.  
  102.  
  103. /***
  104. *void __termcon(void) - close console I/O handles
  105. *
  106. *Purpose:
  107. *       Closes _coninpfh and _confh.
  108. *
  109. *Entry:
  110. *       None.
  111. *
  112. *Exit:
  113. *       No return value.
  114. *
  115. *Exceptions:
  116. *
  117. *******************************************************************************/
  118.  
  119. void __cdecl __termcon (
  120.         void
  121.         )
  122. {
  123.         if ( _confh != -1 && _confh != -2 ) {
  124.                 CloseHandle( (HANDLE)_confh );
  125.         }
  126.  
  127.         if ( _coninpfh != -1 && _coninpfh != -2 ) {
  128.                 CloseHandle( (HANDLE)_coninpfh );
  129.         }
  130. }
  131.