Microsoft KB Archive/51434

From BetaArchive Wiki
Knowledge Base

Article ID: 51434

Article Last Modified on 6/29/2004


  • The C Run-Time (CRT), when used with:
    • Microsoft C/C++ Professional Development System 7.0
    • Microsoft Visual C++ 1.0 Professional Edition
    • Microsoft Visual C++ 1.5 Professional Edition
    • Microsoft Visual C++ 1.0 Professional Edition
    • Microsoft Visual C++ 2.0 Professional Edition

This article was previously published under Q51434


The following code sample allows you to determine how many CRT file handles are available at run time for a particular process. The function makes use of the predefined pointers _iob and _lastiob, which are set to point to the first and the last input/output information blocks, respectively. The I/O blocks are examined to determine whether they are in use, and a total number of available file handles is produced. There is no equivalent C library function currently available.

NOTE: This is not necessarily an indication of the maximum number of physical files that can be opened. The system-wide limit is set in MS-DOS by FILES= in the CONFIG.SYS file. In OS/2, this limit is arbitrarily governed by system resources and is modified for each process by DosSetMaxFH().


Sample Code

   Compile options needed: /DNO_EXT_KEYS under Windows NT, none otherwise

   NOTE: This code will not compile unless it is compiled as a .C file
         and not a .CPP.

   #define FILE struct _iobuf
   #ifndef NO_EXT_KEYS           /* Extensions enabled */ 
       #define _CDECL  cdecl
       #define _NEAR   near
   #else                         /* Extensions not enabled */ 
       #define _CDECL
       #define _NEAR
   #endif                        /* NO_EXT_KEYS */ 

   #define _IOREAD 0x01          /* Open for read bit */ 
   #define _IOWRT  0x02          /* Open for write bit */ 
   #define _IORW   0x80          /* Open for read/write bit */ 

   FILE                          /* File handle table entry */ 
       char *_ptr;
       int   _cnt;
       char *_base;
       char  _flag;
       char  _file;
    }_NEAR _CDECL _iob[], /*Set to first I/O block at run time*/ 
                  *_lastiob;   /* Set to last I/O block */ 

      All of the above definitions were taken from the STDIO.H header
      file except for _lastiob[], which is not defined. This information
      was extracted to make the example more clear.

    - The following macro will check the availability of a file handle
      by examining the _flag member of the I/O block

   #define inuse(s) ((s)->_flag & (_IOREAD|_IOWRT|_IORW))

    - The following routine loops through the total number of I/O blocks
      and checks the flags to see if it is used or not. The number of
      unused handles is returned, which can be 1 to the maximum number of
      file handles as set by the operating system or the FILES= command
      in the CONFIG.SYS file.

   int Number_Of_Handles(void)
      FILE *stream = _iob;
      int count;

      count = 0;
         if (inuse(stream)) count++;
      while(stream++ < _lastiob);

      return(((_lastiob - _iob)+1)-count);

   void main(void)
      int i;

      i = Number_Of_Handles();
   /* i is now set to the number of available file handles */ 

The table of I/O blocks that is being checked here was allocated at run time according to the maximum number of file handles allowed per process.

Keywords: kbhowto kbcrt KB51434