Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Accessing LPT port from NT4.0

Posted on 1997-05-15
6
Medium Priority
?
1,361 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
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

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.

Question has a verified solution.

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

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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 video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

670 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