Reading and writing strings menu

User Tag List

Results 1 to 10 of 10
  1. #1
    miceiken's Avatar Contributor Authenticator enabled
    Reputation
    209
    Join Date
    Dec 2007
    Posts
    401
    Thanks G/R
    7/9
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Reading and writing strings

    So last night I decided to change from BlackMagic to Apoc's memory reading sample (http://www.mmowned.com/forums/wow-me...s-botting.html) - I have however, stumble up on a problem on both reading and writing strings.

    While reading strings I use the default function:
    Code:
    if (typeof(T) == typeof(string))
    {
        ret = Marshal.PtrToStringAnsi(new IntPtr(address));
        //Log.Output("Read<string>({0:X}): {1}", address, (T)ret);
        return (T)ret;
    }
    This however gives me in most cases a blank result and in some cases some random nonsenseness. Please note that the offsets I use worked fine while using the BlackMagic library and they dont work now.

    For writing there isn't a default function so I've tried to make my own (two actually), with no success:
    Code:
    if (typeof(T) == typeof(string))
    {
        string encode = (string)val;
        ASCIIEncoding encoding = new ASCIIEncoding();
        bytes = encoding.GetBytes(encode);
        return Native.WriteBytes(address, bytes) == bytes.Length;
    }
    and
    Code:
    if (typeof(T) == typeof(string))
    {
        IntPtr lpBuffer = IntPtr.Zero;
        lpBuffer = Marshal.StringToHGlobalAnsi((string)val);
        Marshal.WriteIntPtr(new IntPtr(address), lpBuffer);
        return lpBuffer != IntPtr.Zero;
    }
    Last edited by miceiken; 03-09-2010 at 05:17 PM.

    Reading and writing strings
  2. #2
    Jens's Avatar Contributor
    Reputation
    179
    Join Date
    Sep 2006
    Posts
    251
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are probsably trying to do this out of process? as that is what BlackMagic does, Apocs WhiteMagic however, you need to have injected, So you are probably just reading / writing to your own process with the Marshal

  3. #3
    miceiken's Avatar Contributor Authenticator enabled
    Reputation
    209
    Join Date
    Dec 2007
    Posts
    401
    Thanks G/R
    7/9
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This isn't the WhiteMagic library as the description suggests (link to post above), if he makes an "out-of-process" example, weird thing he includes in process stuff...

  4. #4
    adaephon's Avatar Active Member
    Reputation
    76
    Join Date
    May 2009
    Posts
    167
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    His OOP example still had remnants of his in-process code, including Marshal.ReadIntPtr stuff. For the reading, Marshal.PtrToStringAnsi(someAddress) will try to read an Ansi string from someAddress in your process space. So if someAddress is an address in WoW's process, you will understandably get gibberish, nothing, or access violation exceptions. You need to use ReadProcessMemory to read the bytes from WoW's memory. You can then convert the byte array to a string using the Encoding class.

    Same for writing. Anything involving Marshal is working within your own process space, so unless that process space is the same as WoW's (i.e. you're injected) it won't work. For writing you want WriteProcessMemory

  5. #5
    miceiken's Avatar Contributor Authenticator enabled
    Reputation
    209
    Join Date
    Dec 2007
    Posts
    401
    Thanks G/R
    7/9
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh ok, I'll try when I get home

  6. #6
    misz's Avatar Member
    Reputation
    1
    Join Date
    Nov 2007
    Posts
    17
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by miceiken View Post
    So last night I decided to change from BlackMagic to Apoc's memory reading sample (http://www.mmowned.com/forums/wow-me...s-botting.html) - I have however, stumble up on a problem on both reading and writing strings.

    While reading strings I use the default function:
    Code:
    if (typeof(T) == typeof(string))
    {
      ret = Marshal.PtrToStringAnsi(new IntPtr(address));
      //Log.Output("Read<string>({0:X}): {1}", address, (T)ret);
      return (T)ret;
    }
    
    This however gives me in most cases a blank result and in some cases some random nonsenseness. Please note that the offsets I use worked fine while using the BlackMagic library and they dont work now.

    For writing there isn't a default function so I've tried to make my own (two actually), with no success:
    Code:
    if (typeof(T) == typeof(string))
    {
      string encode = (string)val;
      ASCIIEncoding encoding = new ASCIIEncoding();
      bytes = encoding.GetBytes(encode);
      return Native.WriteBytes(address, bytes) == bytes.Length;
    }
    
    and
    Code:
    if (typeof(T) == typeof(string))
    {
      IntPtr lpBuffer = IntPtr.Zero;
      lpBuffer = Marshal.StringToHGlobalAnsi((string)val);
      Marshal.WriteIntPtr(new IntPtr(address), lpBuffer);
      return lpBuffer != IntPtr.Zero;
    }
    Little tip for you, you can use the "is" keywork instead of using typeof.
    ex:

    Code:
    if (T is string)
    {

  7. #7
    XTZGZoReX's Avatar Active Member
    Reputation
    32
    Join Date
    Apr 2008
    Posts
    173
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Little tip for you, you can use the "is" keywork instead of using typeof.
    ex:
    False. Only on variables; not generic type parameters.

  8. #8
    Apoc's Avatar Angry Penguin
    Reputation
    1388
    Join Date
    Jan 2008
    Posts
    2,750
    Thanks G/R
    0/13
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by misz View Post
    Little tip for you, you can use the "is" keywork instead of using typeof.
    ex:

    Code:
    if (T is string)
    {
    Little tip for you: You can't use the 'is' keyword on a generic type parameter. You have to use if (typeof(T) == typeof(string)).

  9. #9
    SinnerG's Avatar Member
    Reputation
    6
    Join Date
    Aug 2006
    Posts
    78
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if (VARIABLE is string) seems to work for me though (vs200

  10. #10
    Cypher's Avatar Kynox's Sister's Pimp
    Reputation
    1358
    Join Date
    Apr 2006
    Posts
    5,368
    Thanks G/R
    0/6
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by SinnerG View Post
    if (VARIABLE is string) seems to work for me though (vs200
    No shit. (See below)

    Originally Posted by XTZGZoReX View Post
    False. Only on variables; not generic type parameters.

Similar Threads

  1. Looking for a C# Programmer (memory reading and writing)
    By Vanguards in forum WoW Memory Editing
    Replies: 2
    Last Post: 02-05-2012, 12:31 PM
  2. How to get read AND write access to wow process?
    By Shutzler in forum WoW Memory Editing
    Replies: 8
    Last Post: 09-26-2009, 04:16 PM
  3. Two Request- Please Read and Reply
    By jib88 in forum WoW EMU Questions & Requests
    Replies: 3
    Last Post: 02-25-2009, 04:31 AM
  4. Cool website... must read and look.
    By #ClumsyWizard in forum Community Chat
    Replies: 0
    Last Post: 01-04-2008, 10:40 PM
All times are GMT -5. The time now is 12:02 AM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search