Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


VirtualProtectEx and unbalanced stack?

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Dr.Disrespect
Grandmaster Cheater
Reputation: 3

Joined: 17 Feb 2016
Posts: 526

PostPosted: Fri Dec 08, 2017 5:24 pm    Post subject: VirtualProtectEx and unbalanced stack? Reply with quote

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. Very Happy
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 198

Joined: 25 Jan 2006
Posts: 8516
Location: 127.0.0.1

PostPosted: Mon Dec 11, 2017 5:03 am    Post subject: Reply with quote

Use IntPtr for addresses.
_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
Dr.Disrespect
Grandmaster Cheater
Reputation: 3

Joined: 17 Feb 2016
Posts: 526

PostPosted: Mon Dec 11, 2017 11:46 am    Post subject: Reply with quote

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. Sad

_________________
**************

A simple example is better then ten links. Very Happy
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 198

Joined: 25 Jan 2006
Posts: 8516
Location: 127.0.0.1

PostPosted: Mon Dec 11, 2017 1:22 pm    Post subject: This post has 1 review(s) Reply with quote

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)

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
Dr.Disrespect
Grandmaster Cheater
Reputation: 3

Joined: 17 Feb 2016
Posts: 526

PostPosted: Mon Dec 11, 2017 7:10 pm    Post subject: Reply with quote

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. Very Happy
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites