views:

720

answers:

5

I am trying to write to ports 0x60 and 0x64, with no luck.

Delphi code:

procedure PortOut(IOport: WORD; Value: BYTE); assembler; register;
asm
  XCHG DX,AX
  OUT DX,AL
end;

Upon calling PortOut, I get an EPrivilege Privileged instruction exception, because IN and OUT may only execute as Ring0.

I would like to know how I can get Ring0 privileges my an application or how I could write to ports 0x60 and 0x64 using some existing external library.

+3  A: 
alexandrul
Very interesting, thank you! I will dig it a bit and see what I come up with before accepting your answer.
Tom
+6  A: 

Have a look at the IO.DLL from Geek Hideout.

IO.DLL allows seamless port I/O operations for Windows 95/98/NT/2000/XP using the same library.

Here is an example: Parallel Port I/O Using Delphi V 6.0

stukelly
I got an error at runtime, but solved it using http://www.mycockpit.org/forums/showthread.php?15624-Unable-to-extract-IO-SYS-on-XP
Tom
+3  A: 

Of course that might cause trouble for devices that are controlled by a driver. Stuff like IO.DLL is mostly meant to interface cards for which no Windows drivers exist, or where the windows driver is dormant until activated.

And since port 60h is the keyboard controller, and the keyboardi is usually in use, it might cause problems.

If you are not interfacing ancient hardware, but just trying to port dos (TP) code, I urgently advise you to rewrite the said code based on normal Windows APIs.

Marco van de Voort
+6  A: 

The correct way to handle this situations is to write a Windows driver, but it can't be done in Delphi for lack of support. It requires the DDK and a C compiler. The other solutions presented here works, but be aware that usually the give access to any I/O port, not only those your application requires. And that could be a security issue - if the x86 architecture lets the system programmer to define the IOPL (I/O privilege level) and most systems set it a ring 0, there's a reason. General access I/O ports drivers are useful for tests and maybe prototyping or as stopgap measures, but I will be very careful to deploy them, especially if the system in not under strict control. If you need that kind of access, you definitely need to understand how the Windows kernel and its drivers works, and why - and implement your own driver. Most of the time Windows in not unsecure per se - it is running to many unsecure software with the wrong privileges that make it so.

ldsandon
Good answer. What's at least as important as the whole ring 0 stuff is to have a driver to coordinate and / or disallow I/O access from multiple processes or even threads. Wanting direct access to ports is a sign of still thinking in DOS terms - it wasn't a god idea even under 16 bit Windows (with multiple concurrent applications possible).
mghie
A: 

Jeez,

It has been a long time for me. I just launched my DPro 2006 to look at the VCL on this and it bombed. (Guess that is what I get for not doing any Delphi code in the the last couple of years on this machine... and keeping patching up to date, plus installing/uninstalling a jillion other paid and FOSS packages on the box....)

But it would seem to me that if you grabbed the header files for the Windows Driver Framework, or check out Project JEDI's site, you might find something to put together a Miniport driver or such.

Just my $0.02 worth

/s/ BezantSoft

BezantSoft