Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1392
  • Last Modified:

Accessing LPT port from NT4.0

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
nil_dib
Asked:
nil_dib
  • 3
  • 3
1 Solution
 
NickRepinCommented:
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
 
nil_dibAuthor Commented:
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
 
NickRepinCommented:
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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
nil_dibAuthor Commented:
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
 
nil_dibAuthor Commented:
I've forgot to say that I'm working with the NT4.0 SERVER VERSION.
Could this be the problem?
0
 
NickRepinCommented:
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

Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now