How to read the value of a hard coded address in C ++?

I want to read the value that is located at 302H. The goal is to read hardware input (part of the 104pc stack). When I run the following code, 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 run this under xp widows. Only the documentation I can find on the board below enter image description here

EDIT:

From your answers below, I see that I need to write a driver for the board. Can someone point me to a resource on how to do this?

+7
source share
5 answers

To access physical memory directly under Windows, you need to develop a driver. I suggest you read the virtual address space to understand why. Short story. The memory addresses that you see from the usermode process have nothing to do with the addresses of the physical memory, and the addresses in which the hardware is protected by the OS prevent usermode application corruption.

+11
source

I assume that your program works as a regular user. To prevent damage to the OS and system crash, modern OS and processors do not allow you to access memory that does not belong to your program.

To access such device memory, you need to run CPU core mode, not user mode. A common way to use such devices is to write a low-level device driver that runs in kernel mode and uses it as an interface for your user mode program.

+1
source

You are not allowed to access the equipment directly from the user mode program. For this you need a device driver.

Doesn't the hardware come with some software that you need to install? Check the software documentation for how to call it.

+1
source

There are several off-the-shelf drivers that allow user-mode applications to read and write I / O ports; one of the most famous is inpout32.dll , others are mentioned here to find them a good search key, is to "write parallel port NT" (because they are most often used for this task).

In general, they work by loading the kernel-mode driver (an action that requires administrative rights), and then calling it from usermode every time you call the dll function to perform read / write.

Please note, however, that most of these libraries do not have any form of access control, so by downloading their driver, you will actually let any application know how to use it to read / write on the I / O ports, and this is a complete security risk.

+1
source

Of course, if you want to go through the whole pig, you can download the SDK for Windows device drivers

+1
source

All Articles