GetItemName and the so like problem menu

User Tag List

Results 1 to 8 of 8
  1. #1
    Valtharak's Avatar Master Sergeant
    Reputation
    51
    Join Date
    Feb 2011
    Posts
    105
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    GetItemName and the so like problem

    I'm having problem with GetItemName .

    the function work fine, i can get the string but when i try to return the string out of the function i just called GetItemName VS debugger just hang. seem like the GetItemName corrupt my stack or something.

    anyone had the same problem and got around it?

    GetItemName and the so like problem
  2. #2
    bmi's Avatar Private
    Reputation
    1
    Join Date
    Jul 2012
    Posts
    3
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're going to have to post some code.

    Are you sure you're not just returning a string that was on the stack ?

  3. #3
    Valtharak's Avatar Master Sergeant
    Reputation
    51
    Join Date
    Feb 2011
    Posts
    105
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Theres nothing special abotu my code.

    public string Name()
    {
    Int32 pString = 0;

    D3Functions.GetItemName(ref pString,BaseAddress,1,0); //offset is 0x969FD0

    string name = Memory.ReadString((IntPtr)(pString + 20));
    //name = Marshal.PtrToStringAnsi((IntPtr)(pString+20));

    return name;
    }

    GetString read the bytes and convert to string with Encoding.ASCII.GetString(bytes, 0, size);

    not sure if the problem is cause i'm not calling GetItemName from the main thread but from my own thread.
    It only crash after the return. and only my app crash not diablo.

  4. #4
    bmi's Avatar Private
    Reputation
    1
    Join Date
    Jul 2012
    Posts
    3
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    edit : nevermind, too much C these years and too many years since i touched c++ let alone c#

    You're returning a variable that is on the stack, that is your problem.

    Code:
    public string Name()
     {
    ...
    
     string name = Memory.ReadString((IntPtr)(pString + 20));
    ...
     return name;
     }
    "name" is allocated on the stack. Once you exit the "Name()" routine, it ceases to exist (or more exactly, it is now out of scope, it still exists, but in a place you shouldn't try to access). What you return is just a pointer to that memory space in the stack. The moment you call another sub-function, that place in the stack will be overwritten with other return pointers and local variables. I assume you're compiling with some sort of debug configuration or runtime protection that warns you of this behaviour.

    What you need to do is allocate some memory not on the stack for that string, either do an explicit dynamic memory allocation (think new or malloc), put it in a global variable somewhere, reserve some space in advance where you're going to put it, etc.

    I'm not sure of your level of knowledge, just google "stack memory" or "stack heap memory" to get you started if this sounds too foreign.
    Last edited by bmi; 07-26-2012 at 05:49 AM.

  5. #5
    bad6oy30's Avatar Member Authenticator enabled
    Reputation
    1
    Join Date
    Dec 2010
    Posts
    41
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think Valtharak is using c#, not c++

  6. #6
    _Mike's Avatar Contributor
    Reputation
    310
    Join Date
    Apr 2008
    Posts
    531
    Thanks G/R
    0/2
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by bmi View Post
    I'm not sure of your level of knowledge [...]
    Hahaha oh the irony Where do all these "experts" come from?
    FYI, look up how std::string works. (Yes I know this is c# but either way this guy couldn't have been more wrong even if it was c++)

  7. #7
    Valtharak's Avatar Master Sergeant
    Reputation
    51
    Join Date
    Feb 2011
    Posts
    105
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well i finaly got it working. i'm prety sure i tried this before, not sure why it was not working. just need to write some code clear the color marker now. thx for the hints.

    public string Name()
    {
    var ptr = Memory.AllocateMemory(;
    string name;
    try
    {
    UInt32 pItemName = D3Functions.GetItemName(ptr, BaseAddress, 1, 0);
    IntPtr pMarkedName = Memory.Read<IntPtr>(pItemName + 4);
    name = Marshal.PtrToStringAnsi(pMarkedName);
    }
    finally
    {
    Memory.FreeMemory(ptr);
    }
    return name;
    }

  8. #8
    bmi's Avatar Private
    Reputation
    1
    Join Date
    Jul 2012
    Posts
    3
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by _Mike View Post
    Hahaha oh the irony Where do all these "experts" come from?
    FYI, look up how std::string works. (Yes I know this is c# but either way this guy couldn't have been more wrong even if it was c++)
    Well that was embarassing haha, that should teach me to guesswork at 3am.
    Nevermind my post obviously, too much C these years and too many years since i touched c++ let alone c#

    Of course, barring any optimization, name is stack allocated, but that's only the string object container, the memory space for the content itself is elsewhere (likely allocated on the heap at object creation), and, most importantly, the return would make a copy of the object for the caller. With optimizations the string might directly be allocated at an address given by the caller.

    As penance for my crimes, I fired up visual studio to check the implementation in more detail. Microsoft's implementation of std::string is a 28 bytes structure (on a 32 bit architecture) or 32 bytes in debug mode, with a pointer to the string at offset 4 and the length at offset 20. Interestingly enough, for the same very simple example (std::string constructed from a C string), the libstd++ implementation is only 4 bytes big (32 bits architecture again) and simply contains a pointer to a copy of the string. This can't be all of it.

    Digging deeper, the MS standard lib implementation is big because it also has a short string buffer inside the structure, so very short strings aren't even dynamically allocated. If the string is 16 bytes or lower (including ending null), it's directly included in the container. So in short, MS implementation of std::string is (again, this is all on a 32 bits system) :
    Code:
    + 0 ( 4 bytes) : allocator base class ?
    + 4 (16 bytes) : either pointer to the allocated string, or the string itself if it fits inside 16 bytes
    +20 ( 4 bytes) : length of the string (not including ending null)
    +24 ( 4 bytes) : seems to be allocated memory for the string (not including ending null)
    The GCC library does it differently. The std::string container is just one pointer, pointing to the memory allocated for the string directly. The rationale behind it being that it's easier for debugging purposes if the string object directly points to the string itself (see libstdc++: basic_string.h Source File for the documentation). The rest of the structure is located before the string in memory :
    Code:
                                          (4 bytes) length (not including ending null)
                                          (4 bytes) allocated memory for the string (not including this 3 word header nor ending null)
                                          (4 bytes) reference count (-1:leaked, 0:one reference, 1+:multiple references)
    std::string : (4 bytes) pointer ----->          array of char
    PS : god, i hate^H^H^H^Hdislike c++.
    Last edited by bmi; 07-26-2012 at 08:34 AM.

Similar Threads

  1. Replies: 0
    Last Post: 10-30-2013, 04:34 PM
  2. [Large Art] Water brushes and the likes
    By Kirth in forum Art & Graphic Design
    Replies: 3
    Last Post: 12-25-2010, 07:10 AM
  3. Problem with 3ds Max 2008 and the .obj files
    By Toxikman in forum WoW ME Questions and Requests
    Replies: 2
    Last Post: 11-10-2009, 01:41 PM
  4. [Uploading] Ditch Rapidshare and the likes...
    By Kissy in forum World of Warcraft Guides
    Replies: 5
    Last Post: 08-03-2008, 06:07 PM
  5. human druids and the like with ascent?
    By trj0223 in forum World of Warcraft Emulator Servers
    Replies: 2
    Last Post: 01-25-2008, 05:45 PM
All times are GMT -5. The time now is 03:36 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