Mega Code Archive

 
Categories / Delphi / System
 

Direct port access even in Windows NT2000XP

Title: Direct port access even in Windows NT/2000/XP Question: Acces directly to paralell port, by example... Answer: Until the arrival of the Windows NT, acceding to port addresses was as simple as to use these two assembler functions: procedure WritePort(Direccion:word;valor:byte); begin asm mov dx,Direccion mov AL,valor out DX,AL end; end; function ReadPort(Direccion:Word):Byte; begin asm mov dx,Direccion in al,dx mov Result,al end; end; With the arrival of the Windows NT, Windows 2000 and Windows XP the thing became more difficult ... In these operating systems, the programs are executed in two differentiated modes: kernel mode and user mode. In kernel mode has access to everything. It is the mode used by the kernel and the drivers installed in the system. In this mode, the drivers can 'talk' with our peripherals, accesing the neccesary ports without problems. For security reasons, in user mode, the programs can't access to all the system resouces. For example, a user without privileges could handle the network card directly, read the keyboard, handle the circuitry of the IDE interface and read files to which he does not have permission to access, etc. The Delphi applications will be always executed in user mode. Knowing this... how we will make then to access the ports to which we do not have permission? Well, the solution is to use a driver installed in kernel. The Driver, as runs in kernel mode, have access to the ports, and we, in user mode can 'talk' with the driver, that simple. However, where we found a driver that it allows us to make these operations? Well, luckily, somebody has already done it by us. In particular, we must available a driver in kernel mode, with its sources (in case we wanted to look around them) already compiled in a DLL. The URL of the DLL is: http://www.logix4u.net/inpout32.htm The DLL works in any Windows (including Windows 98) In addition, it discriminates when it is necessary use the driver, therefore, it also works in the Windows98, in a transparent way for us. The DLL exports two functions that we can use. One is for sending a value to a port and the other is for reading the content of a port. We simply will have to define the functions to be able to use them in our application. Like whenever we used functions that a DLL exports, we have two ways to access to them: linking it statically or dynamically. Link the functions of the DLL to our application of static way is simplest, simply we must put these two lines of code in the implementation part: function Inp32(wAddr:word):byte; stdcall; external 'inpout32.dll'; function Out32(wAddr:word;bOut:byte):byte; stdcall; external 'inpout32.dll'; in order to be able to use them later. Here you have examples: {Send 65 to the port 378} Out32($378,65); {Read the port 379} ShowMessage( 'Port $379:'+ IntToStr(Inp32($379)) ); nevertheless, if the DLL were not accessible, or in the directory of your application, or in the directory of the system, when executing your application you would obtain a pretty exception when starting your application. In order to avoid this, you can use dynamic linking of the functions of the DLL, that is to say, to load the DLL by means of LoadLibrary and soon to find the direction of the functions and to assign them to it to two pointers towards those functions that previously you have defined.