#c++ reading string value from an address

30 messages · Page 1 of 1 (latest)

cinder spire
#

I stored all the values as DWORD_PTR object but when reading the values of the pointers its not read the values

#include <windows.h>
#include <vector>

using namespace std;

HANDLE proc = GetCurrentProcess();

class Object{
     public:
     DWORD_PTR address;
     Object(DWORD_PTR address):address(address){}
    
     template<typename T>
     T rpm() {
        T data;
        ReadProcessMemory(proc, reinterpret_cast<LPVOID>(address), &data, sizeof(T), 0);

        return data;
     }
};


int main(){
    int a = 5;
    string b = "somestr1";
    string c = "somestr2";
    vector<DWORD_PTR> mylist;
    mylist.push_back((DWORD_PTR)&c);
    mylist.push_back((DWORD_PTR)&a);
    mylist.push_back((DWORD_PTR)&b);
    cout<<Object(mylist[0]).rpm<string>()<<endl;
    cout<<Object(mylist[1]).rpm<int>()<<endl;
    CloseHandle(proc);
}``` l can get the first item by calling rpm but cant get the second item there is no error just prints nothing.
magic yarrowBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

sweet oxide
#

Now I get your question!

#

So yeah, what you're doing is quite dangerous for many reasons

#

Because of the internals of string

#

The issue is that you can't just copy bits from one object to another and how it works

cinder spire
sweet oxide
#

string is a complex object - it manages it's memory, allocates/deallocates it as needed. It only works assuming it is the sole owner of the memory, and nobody else uses it

#

Dangerous, as in it will crash

sweet oxide
#

So you end up with double free, which is illegal (for good reason)

#

Overall, you cannot move or copy complex objects by copying it's bitwise representation

cinder spire
#

you mean l should send referances to functions instead of creating copies?

sweet oxide
#

Maybe? I'm not exactly sure what's your final gole, and why do you use system API here

cinder spire
sweet oxide
#

Then don't use ReadProcessMemory, it doesn't make much sense. Just use a pointer and cast the pointer

#

And return the pointer from the rpm function (no idea what this name stands for)

#

Also, it is a bad idea to store arbitrary pointers like that without type annotations

#

Drop the winapi stuff, just use std::any

cinder spire
sweet oxide
#

you tell me, what do you need a list of pointer to values of different types for

#

and why do you use a function that is designed to reading memory of a process, instead of just accessing the pointer as you should, if you actually are doing what you are telling you are doing

cinder spire
#

a list class that can push_back string and integer

sweet oxide
#

Then use:
std::variant<std::string, int>

#

And have a vector of that

sweet oxide
#

Yeah, you didn't follow with anything. Which can only suggest you didn't really just want a list with strings and integers

#

!kick @cinder spire

solemn driftBOT
#

dynoSuccess kaan karakoc#6124 was kicked.

inner current