BUG: 16-Bit OpenFile() to a Local Printer Sets Stream to Cooked

ID: Q185252


The information in this article applies to:
  • Microsoft Win32 Application Programming Interface (API), used with:
    • Microsoft Windows 95
    • Microsoft Windows NT 4.0
    • Microsoft Windows 2000


SYMPTOMS

The following article in the Microsoft Knowledge Base:

Q11988 INFO: Windows File I/O vs. C Run-time File I/O
states that "An application should use the OpenFile() API any time an MS- DOS file handle is required. The open functions do not necessarily open a file in binary raw mode; the application is required to set the binary attribute explicitly. The OpenFile() function automatically performs this step."

This is indeed the case when the target of OpenFile is a file. However, under the 16-bit subsystems of Windows NT 4.0 and Windows 95, any attempts to open a local printer using OpenFile("LPTx", ...) will invariably set the output stream for cooked mode.


RESOLUTION

To work around this, explicitly set the stream for raw mode as follows:

Sample Code


   #include <windows.h>
   #include <stdio.h>

   int main()
   {
      OFSTRUCT ofin, ofout;
      int nBytesWritten = 0;
      int cbRead;
      BYTE *pBuf;
      HFILE hPrintFile;
      HFILE hReadFile;

      /* Allocate a buffer for file I/O. */ 
      hReadFile = OpenFile("a:\\Test1.pcl", &ofin, OF_READ);

      hPrinter = OpenFile("LPT1", &ofout, OF_WRITE);

      /* Explicitly set the output stream to raw mode. */ 
      _asm
      {
         mov bx,hPrinter
         mov dx,20h
         mov ax,4401h
         int 21h
      }

      pBuf = (PBYTE) LocalAlloc(LMEM_FIXED, 2048);

      /* Copy the input file to the printer. */ 
      do
      {
         cbRead = _lread(hReadFile, pBuf, 2048);
         nBytesWritten = _lwrite(hPrinter, pBuf, cbRead);
      } while (cbRead != 0);

      /* Free the buffer and close the streams. */ 
      LocalFree((HLOCAL) pBuf);
      _lclose(hReadFile);
      _lclose(hPrinter);

      return(0);
   } 


STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. We are researching this bug and will post new information here in the Microsoft Knowledge Base as it becomes available.


MORE INFORMATION

Steps to Reproduce Behavior

  1. Start OpenFile to obtain an HFILE handle to LPT1. For example, hPrinter = OpenFile("LPT1", &ofout, OF_WRITE);


  2. Pass the address of a buffer containing an embedded CTRL-Z (1Ah) to _lwrite. For example, nBytesWritten = _lwrite(hPrinter, &Buf, sizeof(Buf));


Result

The number of bytes output to the printer (nBytesWritten) will be less than the size of the buffer (sizeof(Buf)). Specifically, the stream will be truncated immediately following the initial CTRL-Z.

Expected Result

The number of bytes output to the printer (nBytesWritten) will be equal to the size of the buffer (sizeof(Buf)).

Additional query words:

Keywords : kb16bitonly kbAPI kbKernBase kbNTOS400 kbWinOS2000 kbWinOS95 kbDSupport kbGrpKernBase
Version : winnt:4.0
Platform : winnt
Issue type : kbbug


Last Reviewed: January 10, 2000
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.