Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Accessing LPT port from NT4.0

Posted on 1997-05-15
6
Medium Priority
?
1,371 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 240 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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

916 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