Which version of Windows? Which printer driver?
On Thu, 10 Jan 2008 07:17:40 -0800 (PST), hari <
haricibi83@gmail.com>wrote:
Quote
Hi all,
Im trying to send a comamnd to the printer and read the
response for it thru parallel port(LPT1:). The write file always
succeeds, but in readfile it goes to hanging state. I have given my
code below.
int g;
HANDLE h;
DWORD NumberOfBytesWritten,NumberOfBytesRead;
unsigned char buff1[12];
unsigned char buff_read[30];
h = CreateFile("LPT1:",
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ,
NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL |
FILE_FLAG_SEQUENTIAL_SCAN,
NULL);
if (h == INVALID_HANDLE_VALUE)
{
g= 0;
****
Since g is not used, why is it assigned? What does the return type indicate?
****
Quote
return 0;
}
else
{
g=1;
****
Since g is not used, why is it assigned? Given that g seems to have only two values, 0
and 1, why is it an int? It should be a BOOL and get TRUE or FALSE, but it still isn't
used and doesn't seem to have any meaning.
****
Quote
buff1[0] = '{';
buff1[1] = 'W';
buff1[2] = 'B';
buff1[3] = '|';
buff1[4] = '}';
i = WriteFile(h,buff1,5,&NumberOfBytesWritten ,NULL);
****
Seems unnecessarily complicated way to send a constant string; for example
i = WriteFile("{WB|}", 5, &NumberOfBytesWritten, NULL);
does it without requiring a byte-by-byte assignment of characters to an array
Note that you have not declared variable i, so we don't know its type. You should declare
it of type BOOL. Since you do not test it after the WriteFile, you have no idea if the
WriteFile actually worked, so perhaps the ReadFile hanging makes perfect sense, because
nothing was written.
****
Quote
i =ReadFile(h,buff_read,2,&NumberOfBytesRead,NULL);
****
If the driver is not a bidirectional driver; if the printer is not sending data; if the
driver is not prepared to handle data coming back from the printer, and so on, the
ReadFile will hang. You should probably use asynchronous I/O for this, but you have
omitted massive amounts of critical data here. Note that not all printer drivers are
prepared to deal with bidirectional printer ports, and you may need to replace your
printer driver.
joe
****
Quote
CloseHandle(h);
return 400;
}
Joseph M. Newcomer [MVP]
email:
newcomer@flounder.com
Web:
www.flounder.com">
www.flounder.com
MVP Tips:
www.flounder.com/mvp_tips.htm">
www.flounder.com/mvp_tips.htm
-