How to write to I/O ports in Windows XP? (Delphi7)
Asked Answered
O

5

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.

Origen answered 30/1, 2010 at 10:43 Comment(4)
I've solved the problem faster using stukelly's answer; alexandrul also posted a perfectly functional answer.Origen
If you're going to use the various IO port DLLs out there you haven't solved the problem. You've only dug yourself deeper into it. I forsee a LOT of BSODs (or whatever has replaced them in Windows 7) in your software's future.Divorcement
@ttmrichter Actually, IO.DLL works incredibly well (except that it's for 32-bit only and the author said it would be very difficult to make one for x64 and he's not bothering). I haven't had any issues at all and everyone is pleased with the outcome on Windows 2000, XP and 7.Origen
It works incredibly well in about the same way that a chainsaw without a tip guard works incredibly well. When the inevitable bug kills your system suddenly and thoroughly without any chance of collecting debugging information of any meaningful kind, you'll see what I mean.Divorcement
G
7

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

Giselle answered 30/1, 2010 at 10:54 Comment(1)
I got an error at runtime, but solved it using mycockpit.org/forums/…Origen
C
6

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.

Crowther answered 30/1, 2010 at 14:10 Comment(1)
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).Protein
B
4
Bhayani answered 30/1, 2010 at 10:49 Comment(1)
Very interesting, thank you! I will dig it a bit and see what I come up with before accepting your answer.Origen
A
3

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.

Anthropogeography answered 30/1, 2010 at 12:12 Comment(0)
C
0

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

Cowlick answered 16/9, 2010 at 8:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.