Solved

Accessing LPT port from NT4.0

Posted on 1997-05-15
6
1,298 Views
Last Modified: 2013-12-03
I wrote a programm (VC++) to access the parallel port under NT4.0.
I used the CreateFile, ReadFile and WriteFile methods to communicate with the port:

HANDLE hLPT;
BYTE ccb;
BOOL bWriteFile,bCloseHandle, bWait=FALSE, bOVResult;
OVERLAPPED st_Overlapped;
ccb=1;

hLPT=CreateFile("LPT1",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);

bWriteFile=WriteFile(hLPT, &ccb, 1, &nNumberOfBytesWritten, &st_Overlapped);

bCloseHandle=CloseHandle(hLPT);
.....

The result: If I have installed a printer + printer driver the printer reacts  (binking, message: receiving data ...). The main problem is that I will not print!
If there is no printer driver + printer  installed, several errors occur as the case may be the Overlapped structure is filled.
Some are:
Wrong or not defined access number
or
The parameter is wrong      (WHICH????)
..

My question:
How can I write a Byte to the parallel pot WITHOUT WRITING A VXD DRIVER!!!???

Thanks to all who have a comment or solution.
0
Comment
Question by:nil_dib
  • 3
  • 3
6 Comments
 
LVL 15

Accepted Solution

by:
NickRepin earned 120 total points
ID: 1397373
You should fill members of OVERLAPPED structure.
Here is my test program.
If there is no printer installed and attached to LPT1, WriteFile() reports ERROR_IO_PENDING error and GetOverlappedResult() reports ERROR_NOT_READY.
If there is a printer attached to LPT1, these functions return true.

#include <windows.h>
#include <iostream.h>

void main(void) {
   HANDLE hLPT;
   BYTE* ccb="Print string";
   BOOL bWriteFile;
   OVERLAPPED st_Overlapped;

   hLPT=CreateFile("LPT1",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
   cout<<"hLpt="<<hex<<hLPT<<endl;

   // Creating event
   memset(&st_Overlapped,0,sizeof(OVERLAPPED));
   st_Overlapped.hEvent=CreateEvent(NULL,TRUE,TRUE,NULL);

   DWORD num,numb;
   bWriteFile=WriteFile(hLPT, ccb, strlen(ccb), &num,&st_Overlapped);
   cout<<"bWriteFile="<<dec<<bWriteFile<<' '<<GetLastError()<<endl;
   if(GetLastError()!=ERROR_IO_PENDING) {
      cout<<"Fatal error"<<endl;
   }
   else {
      BOOL r=GetOverlappedResult(hLPT,&st_Overlapped,&numb,TRUE);
      cout<<"GetOverlappedResult()="<<r<<' '<<GetLastError()<<endl;
   }
   CloseHandle(hLPT);
}

0
 
LVL 4

Author Comment

by:nil_dib
ID: 1397374
Hi Nick,
I've tested the programm. If I have installed a printer driver + printer the
WriteFile function returns FALSE with  the last Error: overlapped I/O operation in process. But the printer reacts. The handle of CreateFile was 0x0000004c.
If there is no printer + printer driver installed (and thats what I want) the CreateFile handle is 0xFFFFFFFF and the WriteFile function returns FALSE with the last Error: The access nummber is not defined.
I don't know how to fill the OVERLAPPED structure to write a byte to the port without a printer installed on the PC.
0
 
LVL 15

Expert Comment

by:NickRepin
ID: 1397375
Please check numerical error code (GetLastError) after CreateFile and let me know. May be, you have no access rights to LPT?
I have an administrator account on NT4.0 and my test program works fine.
To fill OVERLAPPED, you have to set all its memebers to zero except hEvent (see code in my answer before).
Also check 'Parallel', 'Parport', 'ParVdm' drivers in Control panel->Devices - are they started or not?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Author Comment

by:nil_dib
ID: 1397376
Hi Nick,
I have an administrator account too. I checked the  'Parallel', 'Parport', 'ParVdm' drivers and they are started.
I 've tesed the programm one more time and the CreateFile function works well (returns true, HANDLE with printer+driver: 0x00000050, HANDLE without printer+driver: 0x0000004c).


#include <stdafx.h>  
#include <winbase.h>
#include <iostream.h>

void dispError ()
{
      LPVOID lpMsgBuf;
 
      FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,NULL,
                              GetLastError(),MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
                              (LPTSTR) &lpMsgBuf,0, NULL);

      // Display the string.
      printf(" last Error: %s",lpMsgBuf);

      // Free the buffer.
      LocalFree( lpMsgBuf );
}


void main(void)
{

                     HANDLE hLPT;
      BYTE ccb=6;
      BOOL bWriteFile;
      OVERLAPPED st_Overlapped;

    st_Overlapped.Internal=NULL;
    st_Overlapped.InternalHigh=NULL;
    st_Overlapped.Offset=NULL;
    st_Overlapped.OffsetHigh=NULL;
 

      hLPT=CreateFile("LPT1",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
      cout<<"hLpt="<<hex<<hLPT<<endl;
      if (hLPT==INVALID_HANDLE_VALUE) dispError();

      // Creating event
      memset(&st_Overlapped,0,sizeof(OVERLAPPED));
      st_Overlapped.hEvent=CreateEvent(NULL,TRUE,TRUE,NULL);

      DWORD num,numb;
      
      bWriteFile=WriteFile(hLPT, &ccb, 1, &num,&st_Overlapped);
      cout<<"bWriteFile="<<dec<<bWriteFile<<' '<<GetLastError()<<endl;

      if(GetLastError()!=ERROR_IO_PENDING)
      {
            dispError();
            cout<<"Fatal error"<<endl;
      }
      else
      {
            BOOL r=GetOverlappedResult(hLPT,&st_Overlapped,&numb,TRUE);
            cout<<"GetOverlappedResult()="<<r<<' '<<GetLastError()<<endl;
            dispError();
      }

      CloseHandle(hLPT);
}


The error occurs in the WriteFile and GetOverlappedResult  function:
 
With printer+driver:
CreateFile = TRUE
WriteFile:GetLastError = 997
GetOverlappedResult = TRUE

Without printer+driver:
CreateFile = TRUE
WriteFile:GetLastError = 997  (overlapped I/O operation in process)
GetOverlappedResult:GetLastError = 21 (the device is not ready)

any comment?


 
0
 
LVL 4

Author Comment

by:nil_dib
ID: 1397377
I've forgot to say that I'm working with the NT4.0 SERVER VERSION.
Could this be the problem?
0
 
LVL 15

Expert Comment

by:NickRepin
ID: 1397378
I'm working with server too. It's not mean. I checked ordinary user account, the program  works fine as well.

  With printer+driver:  
  CreateFile = TRUE
  WriteFile:GetLastError = 997  // ERROR_IO_PENDING
  GetOverlappedResult = TRUE
Actually, ERROR_IO_PENDING is not 'true' error, it reports that WriteFile() working in background. When you call GetOverlappedResult(), it waits while write operation complete and returns TRUE if write success. Ie all fine, you sent data to printer driver (and printer!).

   Without printer+driver:
   CreateFile = TRUE
   WriteFile:GetLastError = 997 (overlapped I/O operation in     process)
   GetOverlappedResult:GetLastError = 21 (the device is not ready)
All right, there is nothing attached to parallel port, so parallel driver tryes to write your data to LPT, but nobody respond that data is received (there is no 'device ready' signal on parallel port's pins). Try to connect a printer to parallel port (without installing printer driver), turn it 'on-line', and run program again.


Please see also my comments to http://www.experts-exchange.com/Q.8630003366.

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now