Jump to content
Tuts 4 You

Increase DWORD by one?


FastLife

Recommended Posts

Posted (edited)

hello you


 


i want to increase a dword value by 1.


So using c++ to get a value of a pointer, and using inline asm to increase this value by one. so far it works.


 


now i want to remove that inline asm, and increasing the value by one in c++ itself. but that doesn't work because somehow it increase the value by 4 instead of 1.


 


 


working with inline asm



DWORD *value = (DWORD*)(VAaddress);
__asm{
inc value
}

not working in fully c++



DWORD *value = (DWORD*)(VAaddress);
value++;

and ideas?


Edited by FastLife
Posted

value[0] +=1;

perhaps...

considering value is a pointer...

  • Like 1
Posted

thanks for helping but unfortunately it didn't work.


 


this



Pointer *pointerOfAddress = (Pointer*)(VAaddress);
pointerOfAddress[0] += 1;

will result in the error Size of the type 'void' is unknown or zero.


Posted

Try this


 


*value += 1;


  • Like 1
Posted

thanks deathway, this time no errors. however, it increments the value of 4, instead of one, like before.


probably because a dword is 4 bytes so thats why it adds 4. but don't know how to add just 1 byte.


Posted (edited)

Yes. Increments use the factor sizeof(source_type), so it increments by sizeof(DWORD), which equals 4. You can use a cast to make your intent clear by casting to unsigned char*. Eventually you'll want to have a macro for adding values to pointers..


 


Edit: Above assumes one wants to increment the pointer by one, not the value itself.


Edited by metr0
  • Like 1
Posted (edited)

You can just do:

*value += 1;

For example:


// Create some fake memory to store our value..
auto lpAddr = ::VirtualAlloc(NULL, 4, MEM_COMMIT, PAGE_EXECUTE_READWRITE); // Set the first dword in the allocated memory to 4..
*(DWORD*)lpAddr = 4; // Create pointer to the address..
DWORD* value = (DWORD*)lpAddr; // Read the current value.. (Value is currently 4.)
DWORD currValue = *value; // Increment the value by 1..
*value += 1; // Read the new value..
DWORD newValue = *value; (Value is now 5.)

Also if you want to just use ++, then with Visual Studio, at least, you must surround the pointer in parenthesis first before you attempt to alter it to ensure the actual object being altered is the pointed value, and not the pointer address. Like this:

(*value)++;

Edited by atom0s
  • Like 1
Posted

Awesome atm0s, it works now! Thank you very much.


also thanks to metr0 for the information and others who replied here, thank you guys!


Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...