View previous topic :: View next topic |
Author |
Message |
Dr.Disrespect Grandmaster Cheater Reputation: 3
Joined: 17 Feb 2016 Posts: 526
|
Posted: Fri Dec 08, 2017 5:24 pm Post subject: VirtualProtectEx and unbalanced stack? |
|
|
So here is how I got the problem:
1. I want to read and write the memory of a 64-bit game using C#.
2. The memory I want to read is protected, so I have done some research and it seems that I need to use "VirtualProtectEx" to change the protection of the memory region. So I did this:
Code: |
[DllImport("kernel32.dll")]
public static extern bool VirtualProtectEx(
IntPtr hProcess,
uint dwAddress, //IntPtr lpAddress,
int nSize, //UIntPtr dwSize,
uint flNewProtect,
out uint lpflOldProtect);
......
VirtualProtectEx(processHandle, 0x19f69480000, 1, 0x40, out oldP);
|
However, it doesn't compile, because the address I want to read from is "0x19f69480000", which is "long" instead of "uint". Then I changed the above code to this:
Code: |
[DllImport("kernel32.dll")]
public static extern bool VirtualProtectEx(
IntPtr hProcess,
long dwAddress, <----------------I only changed this
int nSize,
uint flNewProtect,
out uint lpflOldProtect);
......
VirtualProtectEx(processHandle, 0x19f69480000, 1, 0x40, out oldP);
|
It can compile now but I got an error message saying that the stack is unbalanced at run time. How to fix this? Is there a 64-bit version of "VirtualProtectEx"?
Thanks in advance.
_________________
**************
A simple example is better then ten links. |
|
Back to top |
|
|
atom0s Moderator Reputation: 198
Joined: 25 Jan 2006 Posts: 8517 Location: 127.0.0.1
|
Posted: Mon Dec 11, 2017 5:03 am Post subject: |
|
|
Use IntPtr for addresses.
_________________
- Retired. |
|
Back to top |
|
|
Dr.Disrespect Grandmaster Cheater Reputation: 3
Joined: 17 Feb 2016 Posts: 526
|
Posted: Mon Dec 11, 2017 11:46 am Post subject: |
|
|
atom0s wrote: | Use IntPtr for addresses. |
Thanks for the reply. But I am such a noob... Can you kindly show an example? Such as using IntPrt for a 64-bit address? Now I still get a compile error saying cannot convert "long" to IntPtr.
_________________
**************
A simple example is better then ten links. |
|
Back to top |
|
|
atom0s Moderator Reputation: 198
Joined: 25 Jan 2006 Posts: 8517 Location: 127.0.0.1
|
|
Back to top |
|
|
Dr.Disrespect Grandmaster Cheater Reputation: 3
Joined: 17 Feb 2016 Posts: 526
|
Posted: Mon Dec 11, 2017 7:10 pm Post subject: |
|
|
atom0s wrote: | IntPtr will be 4 bytes when compiled under 32bit settings, and 8 bytes when compiled under 64bytes.
Your address can be defined via:
new IntPtr(0x19f69480000) |
Thanks a lot, atom0s. I was so stupid and forgot to compile under 64-bit mode. -_-
_________________
**************
A simple example is better then ten links. |
|
Back to top |
|
|
|