如何在 C++ 中读取硬编码地址的值?
我想要读取位于地址 302H 中的值。目的是从硬件(104pc 堆栈的一部分)读取输入。当我运行以下代码时,出现此错误: setOutput.exe 中 0x004134b9 处未处理的异常:0xC0000005:访问冲突读取位置 0x00000302。
#include <stdlib.h>
#define PORTBASE 0x302
int _tmain(int argc, char *argv[])
{
int value;
int volatile * port = (int *) PORTBASE;
printf("port = %d\n", port);
value = *port;
printf("port value = %d\n", value);
}
编辑:
我在 widows xp 下运行此代码。我在板上只能找到的文档如下
编辑:
从下面的答案中,我可以看到我需要为板子写一个驱动程序。有人可以向我指出如何执行此操作的资源吗?
I am looking to read the value that is located in address 302H. The purpose is to read an input from hardware (a part of a 104pc stack). When I run the following code a get this error: Unhandled exception at 0x004134b9 in setOutput.exe: 0xC0000005: Access violation reading location 0x00000302.
#include <stdlib.h>
#define PORTBASE 0x302
int _tmain(int argc, char *argv[])
{
int value;
int volatile * port = (int *) PORTBASE;
printf("port = %d\n", port);
value = *port;
printf("port value = %d\n", value);
}
EDIT:
I am running this under widows xp. Only Documentation I can find on the board is below
EDIT:
From your answers below, I can see that I need to write a driver for the board. Can someone point me to a resource on how to do so?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
为了在Windows下直接访问物理内存,需要开发驱动程序。我建议您阅读虚拟地址空间来了解原因。简而言之:您从用户模式进程看到的内存地址与物理内存地址无关,并且硬件所在的地址受到操作系统的保护,以防止用户模式应用程序搞乱事情。
In order to access physical memory directly under Windows, you need to develop a driver. I suggest you read up on Virtual Address Space to see why. The short story: The memory addresses you see from a usermode process has no relation to physical memory addresses, and the addresses where hardware lives are protected by the OS to prevent usermode applications from messing up things.
我假设您的程序正在以普通用户身份运行。为了防止您损坏操作系统并使系统崩溃,现代操作系统和 CPU 会阻止您访问不属于您的程序的内存。
为了访问此类设备内存,您需要在内核 CPU 模式而不是用户模式下运行。使用此类设备的通常方法是编写一个在内核模式下运行的低级设备驱动程序,并将其用作用户模式程序的接口。
I'm assuming your program is running as a normal user. To prevent you from damaging the OS and crashing the system, modern OSes&CPUs prevent you from accessing memory that doesn't belong to your program.
In order to access such device memory you'll need to run in kernel CPU mode rather than user mode. The usual way to user such devices is to write a low level device driver that runs in kernel mode and use it as the interface to your user mode program.
不允许您直接从用户模式程序访问硬件。为此,您需要一个设备驱动程序。
硬件不附带一些您应该安装的软件吗?查看软件文档了解如何调用它。
You are not allowed to access the hardware directly from a user mode program. You need a device driver for that.
Doesn't the hardware come with some software you should install? Check the software documentation on how to call it.
有几个现成的驱动程序可以让用户模式应用程序读写IO端口;最著名的之一是 inpout32.dll,其他都在这里提到,要找到它们,一个好的搜索关键字是“写入并行端口NT” (因为它们最常用于此任务)。
一般来说,它们的工作原理是加载内核模式驱动程序(需要管理权限的操作),然后每次调用 dll 函数执行读/写时从用户模式调用它。
但请注意,大多数这些库没有任何形式的访问控制,因此通过加载它们的驱动程序,您实际上允许任何知道如何使用它在 IO 端口上读/写的应用程序,这是相当安全的风险。
There are several ready-made drivers to let user-mode applications read and write IO ports; one of the most famous ones is inpout32.dll, other are mentioned here, to find them a good search key is "write parallel port NT" (since they are most often used for this task).
In general they work by loading a kernel-mode driver (action that requires administrative privileges) and then calling it from usermode every time you call the dll function to perform a read/write.
Notice, however, that most of these libraries do not have any form of access control, so by loading their driver you're actually allowing any application that knows how to use it to read/write on IO ports, and this is quite a security risk.
当然,如果您想全力以赴,可以下载Windows Device Driver SDK
Of course if you want to go the whole hog you can download Windows Device Driver SDK