1

If you have the memory address of a buffer, and this address is stored in a char, for example: char bufferAddress[] = "0024ABC3", how can you create a pointer using bufferAddress so that you can access variables in the buffer?

user3800036
  • 105
  • 1
  • 7
  • but this is 256-bit addressing. – huseyin tugrul buyukisik Aug 25 '14 at 17:39
  • 1
    @huseyin: There are only 32 bits in the OP's example. Basically, he wants to get a 32 bit number from a string hexadecimal representation, which is a fairly pedestrian conversion. http://stackoverflow.com/questions/1070497, http://stackoverflow.com/questions/15148495 – Robert Harvey Aug 25 '14 at 17:40
  • 2
    Convert that number to a `unsigned long` and cast it to `char*` :P ... – πάντα ῥεῖ Aug 25 '14 at 17:41
  • @user3800036 See my answer. It shows how to do the task. – Vlad from Moscow Aug 25 '14 at 18:04
  • 1
    I hope this is for a debug watch window or the equivalent, since pointer values tend to only be valid for a single execution of the process. – Ben Voigt Aug 25 '14 at 18:13
  • It gets a new pointer every time the program runs - Game Maker Studio is dumping some game data into a buffer, then passing the buffer address in string form to a DLL (GM Studio forces the address to be in string form). The DLL converts the string address into a pointer, then uses the pointer to access game data in the buffer. – user3800036 Aug 25 '14 at 18:25
  • 1
    That seems... odd. If it can pass a pointer to the string containing the buffer address, why can't it pass a pointer to the buffer? – Ben Voigt Aug 25 '14 at 18:28
  • Basically GM Studio can only pass types "string" and "real". I get the buffer address using [buffer_get_address](http://docs.yoyogames.com/source/dadiospice/002_reference/buffers/buffer_get_address.html), convert it to a string, then pass it to the DLL. Then the DLL converts it from a string to a pointer in order to access the buffer. – user3800036 Aug 25 '14 at 18:59

2 Answers2

3

If you can get the string into a number, then you can try something like this:

void *ptr = reinterpret_cast<void*> (0x0024ABC3);

There are a few other threads on here that deal with assigning addresses to pointers directly, so you could check those out as well. Here's one: How to initialize a pointer to a specific memory address in C++

Community
  • 1
  • 1
jackarms
  • 1,343
  • 7
  • 14
3

You can do the task using std::istringstream. For example

#include <iostream>
#include <sstream>

int main() 
{
    char bufferAddress[] = "0024ABC3";
    std::istringstream is( bufferAddress );
    void *p;

    is >> p;

    std::cout << "p = " << p << std::endl;  

    return 0;
}

The output is

p = 0x24abc3

If the buffer has type char * then you can reinterpret this pointer to void to pointer to char. For example

char *buffer = reinterpret_cast<char *>( p );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335