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 


[C#] ReadProcessMemory Question

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
hollow87
Cheater
Reputation: 0

Joined: 07 Feb 2015
Posts: 28

PostPosted: Mon Nov 16, 2015 9:07 pm    Post subject: [C#] ReadProcessMemory Question Reply with quote

Just a quick question regarding the p/invoke signature for ReadProcessMemory API

I currently have the following p/invoke signature for ReadProcessMemory
Code:

[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, [Out] byte[] lpBuffer, int nSize, IntPtr lpNumberOfBytesRead);


I know the C API declares nSize and lpNumberOfBytesRead to be of type SIZE_T with that being said should I change both nSize and lpNumberOfBytesRead to UIntPtr to fit closer to the SIZE_T definition or should I be able to use this p/invoke signature without any issues on 32 and 64bit processes.
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 471

Joined: 09 May 2003
Posts: 25814
Location: The netherlands

PostPosted: Tue Nov 17, 2015 3:08 am    Post subject: Reply with quote

change it to uintpt
lpNumberOfBytesRead is a pointer to a memoryaddress.
in 64 bit mode windows will write 8 bytes at the location it points to

so, if you used an int, 4 unknown other bytes would get overwritten causing unpredictable behaviour

_________________
Do not ask me about online cheats. I don't know any and wont help finding them.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
hollow87
Cheater
Reputation: 0

Joined: 07 Feb 2015
Posts: 28

PostPosted: Tue Nov 17, 2015 9:22 am    Post subject: Reply with quote

Right that is why I had lpNumberOfBytesRead as an IntPtr always size of a platform int, I just didn't know if it being unsigned or not would matter but might as well change both nSize and lpNumberOfBytesRead to UIntPtr just so I won't possibly have an issue with them being signed. Correct?
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Tue Nov 17, 2015 1:56 pm    Post subject: Reply with quote

lpNumberOfBytesRead is handled by reference or outvalue in C# so use it like this:
Code:
ref IntPtr lpNumberOfBytesRead
or
out IntPtr lpNumberOfBytesRead


Then for usage you would pass an intptr by reference like this:
Code:

var read = new IntPtr(0);
ReadProcessMemory(..., ref read);
or
ReadProcessMemory(..., out read);

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
hollow87
Cheater
Reputation: 0

Joined: 07 Feb 2015
Posts: 28

PostPosted: Tue Nov 17, 2015 2:23 pm    Post subject: Reply with quote

Right, but does it matter that the C API is a SIZE_T which is unsigned as opposed to C#'s IntPtr being signed.

Let me try and phrase my questions better.

Here is my current P/Invoke signature
Code:
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, [Out] byte[] lpBuffer, int nSize, IntPtr lpNumberOfBytesRead);


I'm essentially wondering which one is the correct p/invoke signature my current one above or one of the 2 below.

Code:
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, [Out] byte[] lpBuffer, IntPtr nSize, IntPtr lpNumberOfBytesRead);

Code:
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, [Out] byte[] lpBuffer, UIntPtr nSize, UIntPtr lpNumberOfBytesRead);


SIZE_T definition from BaseTsd.h
Code:

#if defined(_WIN64)
 typedef unsigned __int64 ULONG_PTR;
#else
 typedef unsigned long ULONG_PTR;
#endif
typedef ULONG_PTR SIZE_T;
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Tue Nov 17, 2015 2:25 pm    Post subject: Reply with quote

You can use UIntPtr instead.
_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
hollow87
Cheater
Reputation: 0

Joined: 07 Feb 2015
Posts: 28

PostPosted: Tue Nov 17, 2015 3:02 pm    Post subject: Reply with quote

Alright thanks that's what I thought just wanted yall's advice as well
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