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 


noncontiguous string reading

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

Joined: 07 Aug 2007
Posts: 10

PostPosted: Mon Jun 23, 2008 2:45 pm    Post subject: noncontiguous string reading Reply with quote

Hello! I'm a C# programmer / hax and well, I've sort of got stuck up on some painfully allocated targets. Traditionally, I've simply used ReadProcessMemory as shown below. However, my target reallocates the string and as a result XP splits it up into chunks. So, how do I piece back together all those chunks into a viable string? I'm assuming that since this is done by the OS and not the program there is probably something in WinAPI for this ... I just can't find it. WinAPI documentation = teh suxor.

Code:

[DllImport("kernel32.dll")]
        private static extern Int32 ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, IntPtr buffer, int size, ref IntPtr lpNumberOfBytesRead);

        public string ReadASCIIString(int Address, int maxLength)
        {
            IntPtr ptr = Marshal.AllocHGlobal(maxLength);
            IntPtr readedBytes = IntPtr.Zero;
            ReadProcessMemory(hProcess, new IntPtr(Address), ptr, (maxLength), ref readedBytes);
            byte[] buffer = new byte[maxLength];
            Marshal.Copy(ptr, buffer, 0, maxLength);
            Marshal.FreeHGlobal(ptr);

            ASCIIEncoding ascii = new ASCIIEncoding();
            string result = ascii.GetString(buffer);
            int nullpos = result.IndexOf("\0");
            if (nullpos != -1)
                result = result.Remove(nullpos, result.Length - nullpos);
            return result;
        }


Thanks!

[/code]
Back to top
View user's profile Send private message AIM Address
jackyyll
Expert Cheater
Reputation: 0

Joined: 28 Jan 2008
Posts: 143
Location: here

PostPosted: Mon Jun 23, 2008 3:13 pm    Post subject: Reply with quote

I don't know C# but... Why not just read it straight into a char/string buffer?... Instead of putting into a marshal and then into a byte array and then into a string..

i.e.

Code:

char buffer[100];
int bytesRead = 0;
ReadProcessMemory(hProcess, new IntPtr(Address), &buffer, maxLength, bytesRead);

return string(buffer);
Back to top
View user's profile Send private message AIM Address MSN Messenger
jester831
Newbie cheater
Reputation: 0

Joined: 07 Aug 2007
Posts: 10

PostPosted: Mon Jun 23, 2008 3:25 pm    Post subject: Reply with quote

Memory Efficiency - I'm looking at thousands of strings at indefinite lengths. However, this is somewhat irrelevant to my problem as I still need to capture noncontiguous strings... must be something WinAPI for this =/
Back to top
View user's profile Send private message AIM Address
samuri25404
Grandmaster Cheater
Reputation: 7

Joined: 04 May 2007
Posts: 955
Location: Why do you care?

PostPosted: Mon Jun 23, 2008 6:35 pm    Post subject: Reply with quote

I think there's something wrong with your declaration.

Code:
http://msdn.microsoft.com/en-us/library/ms680553(VS.85).aspx


This is how I'd have done it:

Code:

[DllImport("kernel32.dll", SetLastError = true)]
private static extern unsafe bool ReadProcessMemory(
                    IntPtr hProcess,
                    IntPtr lpBaseAddress,
                    byte *lpBuffer
                    uint nSize,
                    uint *lpNumberOfBytesRead);

public static unsafe string ReadASCII(IntPtr Address, int Length)
{
    if (Length == 0) //user is an ass
        return String.Empty;

    byte[] Buffer = new byte[Length]; uint NumberOfBytesRead;
   
    fixed (byte *lpBuffer = &Buffer[0])
        ReadProcessMemory(
            hProcess,
            Address,
            lpBuffer,
            nSize,
            &NumberOfBytesRead);

    int Error = Marshal.GetLastWin32Error();
    if (Error != 0)
    {
        MessageBox.Show("Error! Win32 error code: " + Error.ToString(), "Error!");
        return String.Empty;
    }

    return (new string(Buffer)); //I believe that string has a constructor that accepts a byte[]... if it doesn't, I know it accepts a char *, so you would do

    //char *ret = &((char)Buffer[0]);
    //return (new string(ret);
}

_________________
Wiccaan wrote:

Oh jeez, watchout I'm a bias person! Locked.


Auto Assembly Tuts:
In Depth Tutorial on AA
Extended
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