BUG: 16-Bit OpenFile() to a Local Printer Sets Stream to Cooked (185252)
The information in this article applies to:
- Microsoft Win32 Application Programming Interface (API), when used with:
- the operating system: Microsoft Windows 95
- the operating system: Microsoft Windows NT 4.0
- the operating system: Microsoft Windows 2000
- the operating system: Microsoft Windows XP
This article was previously published under Q185252 SYMPTOMS
The following article in the Microsoft Knowledge Base:
11988
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 the operating systems listed above, 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.
Modification Type: | Major | Last Reviewed: | 4/9/2004 |
---|
Keywords: | kb16bitonly kbAPI kbbug kbKernBase kbpending KB185252 |
---|
|