Pages

Friday 6 May 2011

Parallel Port INterfacing System Files




The Problem
             Writing programs to talk with parallel port was pretty easy in old DOS days and in Win95/98 too. We could use Inporb and outportb or _inp() or _Outp functions in our program without any problem if we are running the program on DOS or WIN95/98. But entering to the new era of NT clone operating systems like WIN NT4, WIN2000, WINXP, all this simplicity goes away. Being interested in Parallel port interfacing and programming you might have experienced the problems in writing a program that can talk to parallel port successfully in NT based operating systems. When we are trying to run a program which is written using the conventional software functions like Inporb, outportb, _inp() or _Outp on a WINNT or WIN2000 system, it will show an error message that "The exception privileged instruction occurred in the application at location ....". The picture of such a messagebox is given below.
Error
Staring to this messagebox, you might have been thinking.... "did i make a mistake in my program ?" it is working fine on WIN98 ... Who is guilty here. 'Nobody' that is the answer. Then why it is happening like this ..? The answer is in the next paragraph
                 Being a very secure operating system, Windows NT assigns some privileges and restrictions to different types of programs running on it.It classifies all the programs in to two categories , User mode and Kernel mode ie; running in ring3 and ring0 modes. user mode programs are running in ring3 mode and Kernel mode programs are running in ring0 mode. The programs you generally write falls in the user mode category. The user mode programs are restricted to use certain instructions like IN, OUT etc.. Whenever the operating system find that a user mode program is trying to execute such instructions , the operating system stops execution of those programs and will display an error message. Eventually our interfacing programs stops where they are executing IN or OUT instructions to read or write data to parallel port. But in the same time Kernel mode programs are in no way restricted in executing such instructions. Device drivers are capable of running in kernel mode. So the work around for the above stated problem is to write a kernel mode driver capable of reading and writing data to parallel port and let the user mode program to communicate with it. Writing a driver is not an easy job for even experienced programmers. But writing a simple driver for communicating with parallel port is a simple task when drivers like USB, sound card etc.. are concerned. Even though you get a working driver from somewhere else, installing and configuring it can be very cumbersome task.
The Solution
                 Introducing Inpout32.dll for WIN 98/NT/2000/XP. This dll have the following features
  1. Works seam less with all versions of windows (WIN 98, NT, 200 and XP)
  2. Using a kernel mode driver embedded in the dll
  3. No special software or driver installation required
  4. Driver will be automatically installed and configured automatically when the dll is loaded
  5. No special APIs required only two functions Inp32 and Out32
  6. Can be easily used with VC++ and VB
  7. Functions are compatible with Jan Axelsons Inpout32.dll (available at www.lvr.com). So this dll can be used with the sample programs available with the book Parallel Port Complete, without any modification.



DOWNLOAD

How Inpout32.dll works ?PDFPrintE-mail
Tried the Inpout32.dll..? then Learn how Inpout32.dll does the things. This brief tutorial explains about the working of Inpout32.dll in simple steps, with the help of a flow chart. This could help you much if you want to modify the Inpout32 dll source code
If you don't know what is Inpout32.dll, please read it here and then continue.

How it works 

The outstanding feature of Inpout32.dll is , it can work with all the windows versions without any modification in user code or the DLL itself. This tutorial describes how it is achieved, what programming methods used, what are the APIs used, etc.... The Dll will check the operating system version when functions are called, and if the operating system is WIN9X, the DLL will use _inp() and _outp functions for reading/writing the parallel port. On the other hand, if the operating system is WIN NT, 2000 or XP, it will install a kernel mode driver and talk to parallel port through that driver. The user code will not be aware of the OS version on which it is running. This DLL can be used in WIN NT clone operating systems as if it is WIN9X. The flow chart of the program is given below.
flow chart
The two important building blocks of this program are

1) A kernel mode device driver embedded in the DLL in binary form

2) The DLL itself

Kernel mode driver Hwinterface.sys 

The source code of Hwinterface.sys kernel mode driver is located in "kernel_mode_driver_source" directory. Where "hwinterfacedrv.c" is the main application source file. Three functions implemented in the driver are 

1) DriverEntry() , Called when driver is loaded. Creates device object and symbolic links.

2) hwinterfaceUnload(), Called when driver is unloaded, performs clean up

3) hwinterfaceDeviceControl(), handles calls made through DeviceIOControl API. Performs reading writing to the parallel port according to the control code passed.

The DLL Inpout32

The functions in the DLL are implemented in two source files, "inpout32drv.cpp" and "osversion.cpp". osversion.cpp checks the version of operating system. "inpout32drv.cpp" does installing the kernel mode driver, loading it , writing/ reading parallel port etc... The two functions exported from inpout32.dll are

1) Inp32(), reads data from a specified parallel port register.

2) Out32(), writes data to specified parallel port register.

the other functions implemented in Inpout32.dll are 

1) DllMain(), called when dll is loaded or unloaded. When the dll is loaded , it checks the OS version and loads hwinterface.sys if needed.

2) Closedriver(), close the opened driver handle. called before unloading the driver.

3) Opendriver(), open a handle to hwinterface driver.

4) inst() , Extract 'hwinterface.sys' from binary resource to 'systemroot\drivers' directory and creates a service. This function is called when 'Opendriver' function fails to open a valid handle to 'hwinterface' service.

5) start() , starts the hwinterface service using Service Control Manager APIs.

6) SystemVersion() Checks the OS version and returns appropriate code.

No comments:

Post a Comment