You are here:

C++/Accessing the parallel port in XP


  I wrote a simple program to access the parallel port in C under Windows 98 and it worked fine.But when I run the program under XP nothing happens.I understand that XP somehow restricts access to the port.
  Can you please explain this and how can I access the port in XP?


Note: I do not see why you ask a professed and advertised C++ expert such a question. It has nothing to do with C++.

You are correct in that XP protects access to shared system resources, as do all NT-base OSes, as well an UN*X and UN*X like operating system and other similar operating systems. It also properly isolates the memory of one process from other processes. Windows 9x, ME and the 16-bit Windows variants all basically run on top of MS-DOS - which allowed anyone to do anything (the later version may have been a little less lax - however I have not really paid them much attention).

On the other hand, an OS such as UN*X, VMS or Windows NT or XP will protect system resources - code, memory, I/O ports etc... from abuse from user mode code (i.e. all non-system code) and isolates one process's memory from another's. This prevents one rogue piece of code from bringing down the whole system or similar. The exception is of course code trusted by the system - the system code itself and anything that runs with the same privileges - such as kernel mode device drivers. Since NT 4.0 NT (and XP) video (graphics) card drivers have been kernel mode drivers and as such a bad video card driver can blue screen your system. Previous to this such drivers were user mode drivers which was very safe but made for rather slow graphics performance - not so good when DirectX was slated to be implemented under NT - hence the change.

However in a perfect world nothing your user-mode application can do should be able to crash the system as the system should regulate access to all system resources for you. At worst you should crash the application instance but not the whole OS session!

In general there is only one way to access system port resources from user-mode code (i.e. your code!) - you need a device driver and a way to use that driver to allow the device to be opened and manipulated. In the case of the standard PC parallel port there should already be such a driver installed - so the next question is how do you access it. Well you can open the device like a file using a name such as "LPT1" (for the first parallel port device) using the likes of the Win32 API function CreateFile. Then you can read and write to it using the likes of ReadFile and WriteFile (or ReadFileEx and WriteFileEx). For more information on these topics look in the MSDN library (online at There is a section on Device I/O in the Windows Development / Windows Base Services area.

If you require lower level control then you might like to look at Win32 DeviceIoControl function and read the section on Parallel Ports and  Devices in the Windows Development / Driver Development Kit documentation (also in the MSDN library).
Another place to look is which gives a slightly longer explanation of system mode and user mode on an X86 processor and goes on to point out some other possibilities and a long description on using the porttalk general purpose driver. You might wish to look at some of the other pages on the BeyondLogic site - such as those for standard and enhanced parallel ports.  


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at


©2016 All rights reserved.