如何在 Windows XP 中写入 I/O 端口?(Delphi7)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2167579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:51:07  来源:igfitidea点击:

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

windowsdelphidelphi-7io

提问by Tom

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

我正在尝试写入端口 0x60 和 0x64,但没有成功。

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 instructionexception, because INand OUTmay only execute as Ring0.

在调用 PortOut 时,我得到一个 EPrivilege特权指令异常,因为IN并且OUT可能只能作为 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.

我想知道如何获得应用程序的 Ring0 权限,或者如何使用一些现有的外部库写入端口 0x60 和 0x64。

回答by stukelly

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

看看Geek HideoutIO.DLL

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

IO.DLL 允许使用相同库的 Windows 95/98/NT/2000/XP 的无缝端口 I/O 操作。

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

这是一个示例:使用 Delphi V 6.0 的并行端口 I/O

回答by stukelly

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 anyI/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.

处理这种情况的正确方法是编写Windows驱动程序,但由于缺乏支持,在Delphi中无法完成。它需要 DDK 和 C 编译器。此处介绍的其他解决方案有效,但请注意,通常可以访问任何I/O 端口,不仅是您的应用程序需要的端口。这可能是一个安全问题——如果 x86 架构允许系统程序员定义 IOPL(I/O 特权级别)并且大多数系统将其设置为环 0,那是有原因的。通用访问 I/O 端口驱动程序对于测试和原型设计或作为权宜之计很有用,但我会非常小心地部署它们,尤其是在系统不受严格控制的情况下。如果您需要这种访问权限,您肯定需要了解 Windows 内核及其驱动程序的工作原理,以及为什么 - 并实现您自己的驱动程序。大多数情况下,Windows 本身并不是不安全的——它运行在许多具有错误权限的不安全软件上。

回答by Marco van de Voort

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.

当然,这可能会给由驱动程序控制的设备带来麻烦。像 IO.DLL 这样的东西主要用于接口不存在 Windows 驱动程序的卡,或者 Windows 驱动程序在激活之前处于休眠状态。

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

由于端口 60h 是键盘控制器,并且通常使用键盘,因此可能会导致问题。

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.

如果您没有连接古老的硬件,而只是尝试移植 dos (TP) 代码,我强烈建议您基于普通的 Windows API 重写上述代码。

回答by BezantSoft

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....)

对我来说已经很长时间了。我刚刚启动了我的 DPro 2006 来查看有关此的 VCL 并且它爆炸了。(猜猜这就是我过去几年在这台机器上没有做任何 Delphi 代码的结果……并保持最新的补丁,加上在盒子上安装/卸载数以百万计的其他付费和 FOSS 包…… .)

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.

但在我看来,如果您获取了 Windows 驱动程序框架的头文件,或者查看 Project JEDI 的站点,您可能会找到一些可以组合 Miniport 驱动程序等的内容。

Just my $0.02 worth

只是我的 0.02 美元

/s/ BezantSoft

/s/ BezantSoft