Access to specific memory points

6

How do I access an exact memory address in Windows?

unsigned char * mem = {??};
    
asked by anonymous 22.07.2015 / 01:16

2 answers

10

You can not access a random address like this in most situations. Today there is protection for access to memory.

In some cases you will be able to access by doing:

#include <cstdint>

uintptr_t p = 0x0001FFFF;
int value = *reinterpret_cast<int *>(p);

Nothing guarantees that access will work as you expect it to. The result may be different depending on the situation.

I found this another Guilherme Bernal's answer that shows you how to do something that works but again will not give consistent results:

#include <windows.h>
#include <stdio.h>

int main(void)
{
    SYSTEM_INFO sysinfo;
    GetSystemInfo(&sysinfo);
    unsigned pageSize = sysinfo.dwPageSize;
    printf("page size: %d\n", pageSize);

    void* target = (void*)0x4e0f68;
    printf("trying to allocate exactly one page containing 0x%p...\n", target);
    void* ptr = VirtualAlloc(target, pageSize, MEM_COMMIT, PAGE_READWRITE);

    if (ptr)
        printf("got: 0x%p\n", ptr); //   ptr <= target < ptr+pageSize
    else
        printf("failed! OS wont let us use that address.\n");

    return 0;
}

Note that this situation is somewhat more controlled.

In C void * means that you are using a pointer to anything.

    
22.07.2015 / 01:31
4

Your operating system will not allow you to access memory that does not belong to your program.

#include <stdio.h>

int main(void) {
    unsigned char *mem = 0xdeadbeef; // ou = 3735928559;
    printf("O endereco %p tem %d\n", (void*)mem, *mem);
    return 0;
}
    
22.07.2015 / 14:33