[C#] Reading a std::string from memory menu

User Tag List

Results 1 to 4 of 4
  1. #1
    prixone's Avatar Member
    Reputation
    1
    Join Date
    Nov 2010
    Posts
    8
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [C#] Reading a std::string from memory

    Hi,

    I currently need to read a std::string from memory, so I came up with the following code:

    Code:
    internal static string ReadStdString(IntPtr address)
    {
        var length = Marshal.ReadInt32(address + 20);
        if (length <= 0)
        {
            return string.Empty;
        }
        var capacity = Marshal.ReadInt32(address + 24);
        var startStr = address + 4;
        if (capacity > 15)
        {
            startStr = Marshal.ReadIntPtr(address + 4);
        }
    
        var result = new StringBuilder(length);
        for (var i = 0; i < length; i++)
        {
            result.Append((char)Marshal.ReadByte(startStr, i));
        }
        return result.ToString();
    }
    It works for small/bigger strings, however I've been wondering if there is a more efficient way to achieve this or a better way to write this without the hard-coded offsets for example.

    Thanks for any answers/advice...

    [C#] Reading a std::string from memory
  2. #2
    Corthezz's Avatar Elite User Authenticator enabled
    Reputation
    386
    Join Date
    Nov 2011
    Posts
    325
    Thanks G/R
    182/98
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Afaik strings terminate with \0 which is 0x0 in hex. You could read new bytes until you reach the terminal symbol and then convert the byte array to a string array.
    Also take a look at unsafe code which is much faster than using the marshaler.
    Check my blog: https://zzuks.blogspot.com

  3. #3
    prixone's Avatar Member
    Reputation
    1
    Join Date
    Nov 2010
    Posts
    8
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the answer, I am still learning so for now I am trying to stay away from unsafe, marshal alone is already a pain, can't even image dealing with unsafe code...

    In regards looking for the terminator, wouldn't I still need to skip the initial 4 byte which means I would still be tied to hardcoded offsets?

    What if it was a std::vector<T> any advice or a sample code on reading that?

  4. #4
    d07RiV's Avatar Member
    Reputation
    5
    Join Date
    Jul 2010
    Posts
    22
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm pretty sure the standart does not describe the memory layout, so every compiler (or rather STL implementation) is free to choose what their offsets will look like - you have to figure out what works for your specific case and go with it. Assuming it was compiled with VS, you can always check the source code (which is very difficult to read mind you), or compile some test programs.

    I think in VS, vector contains a 'start' and 'end' pointers, not entirely sure about that though.

Similar Threads

  1. Read space mission timer from memory?
    By Teht in forum SWTOR Bots and Programs
    Replies: 1
    Last Post: 02-10-2012, 07:16 PM
  2. Reading multiple strings from another file?
    By Ryoushi. in forum Programming
    Replies: 0
    Last Post: 06-08-2009, 12:08 PM
  3. [Guide][VB.NET] Read a string from memory
    By Gothian in forum Programming
    Replies: 14
    Last Post: 08-18-2008, 04:39 PM
  4. Memory Reading Chat, w/ help from an Add-On
    By Vector0 in forum WoW Memory Editing
    Replies: 6
    Last Post: 05-08-2008, 10:00 AM
  5. [Guide][VB.NET] Reading a String From Memory
    By Gothian in forum WoW Memory Editing
    Replies: 14
    Last Post: 01-18-2008, 12:08 PM
All times are GMT -5. The time now is 01:19 AM. Powered by vBulletin® Version 4.2.3
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search