| View previous topic :: View next topic |
| Author |
Message |
KryziK Expert Cheater
Reputation: 3
Joined: 16 Aug 2009 Posts: 199
|
Posted: Fri Feb 03, 2012 6:22 pm Post subject: [Help] Reading and Writing to Pointer C++ |
|
|
Okay, so I have the following struct located inside a DLL:
| Code: | struct PLAYER_DATA{
int *Name;
int *Health;
} Player; |
Then, I use something like the following:
| Code: | Player.Health = (int *)0x12345678;
Player.Name = (int *)0x987654321; |
I can then get and set the health like so:
| Code: | *Player.Health = 100; //Set Health
int CurrentHealth = *Player.Health; //Get Health |
My question is: If name is a string, how can I get and set the value of it? I am trying:
| Code: | MessageBox(NULL, (char *)Player.Name, "Name", MB_OK); //Get Name
*(char *)Player.Name = "NewName"; //Set Name |
However, the output isn't even close to what I'm expecting, and the input is invalid because it is a 'constant char *' and cannot be assigned to a 'char'. How can I read/write strings like I do health?
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Fri Feb 03, 2012 6:53 pm Post subject: |
|
|
You only need to set the defined variable to the pointer, not each member.
So you would have something like this:
Player* p = *(Player*)x12345678;
This is assuming that your structure reflects one in memory of the target.
When you cast the pointer to your type it should automatically fill in the data.
_________________
- Retired. |
|
| Back to top |
|
 |
KryziK Expert Cheater
Reputation: 3
Joined: 16 Aug 2009 Posts: 199
|
Posted: Fri Feb 03, 2012 7:20 pm Post subject: |
|
|
Well the data is not placed together in the process; I simply put it in a struct to keep it all together and I assign each variable in the struct a different address manually. The setup I am using seems to work, at least for integers, however I can't figure out how to use it to read/write strings and other data types.
To be clearer, the struct isn't the issue, the struct gives me the correct address for each of the members inside. However, I'm not sure how I'd go about passing strings and floats through it, etc.
Last edited by KryziK on Fri Feb 03, 2012 7:33 pm; edited 1 time in total |
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Fri Feb 03, 2012 7:32 pm Post subject: |
|
|
Just use char* in your struct for a string pointer then.
| Code: | struct PLAYER_DATA{
char *Name;
int *Health;
} Player; |
_________________
- Retired. |
|
| Back to top |
|
 |
KryziK Expert Cheater
Reputation: 3
Joined: 16 Aug 2009 Posts: 199
|
Posted: Fri Feb 03, 2012 7:41 pm Post subject: |
|
|
| Wiccaan wrote: | Just use char* in your struct for a string pointer then.
| Code: | struct PLAYER_DATA{
char *Name;
int *Health;
} Player; |
|
When doing that, reading works:
| Code: | | MessageBox(NULL, Player.Name, "Before", MB_OK); |
However,
| Code: | | Player.Name = "NewName"; |
still won't work. I also tried *Player.Name. No luck. Is that the right way to be doing it? Here's what I'm trying to accomplish:
| Code: | MessageBox(NULL, Player.Name, "Before", MB_OK);
Player.Name = "NewName";
MessageBox(NULL, Player.Name, "After", MB_OK); |
The second MessageBox correctly shows "NewName", however it's not updated in the memory of the process, just in the struct. Or so it seems, at least.
Also, why would reading work like that when, for integers, I needed the extra * in front of the variable?
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Fri Feb 03, 2012 8:57 pm Post subject: |
|
|
Try using memcpy for strings instead.
memcpy( Player.Name, "NewName", 7 );
_________________
- Retired. |
|
| Back to top |
|
 |
KryziK Expert Cheater
Reputation: 3
Joined: 16 Aug 2009 Posts: 199
|
Posted: Fri Feb 03, 2012 9:01 pm Post subject: |
|
|
| Wiccaan wrote: | Try using memcpy for strings instead.
memcpy( Player.Name, "NewName", 7 ); |
Thanks, that works. It's just so inconsistent. *OCD kicks in*
|
|
| Back to top |
|
 |
|