Solved

Accessing LPT port from NT4.0

Posted on 1997-05-15
6
1,290 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

747 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

12 Experts available now in Live!

Get 1:1 Help Now