lua_tolstring menu

Shout-Out

User Tag List

Results 1 to 7 of 7
  1. #1
    danwins's Avatar Contributor
    Reputation
    189
    Join Date
    Mar 2013
    Posts
    143
    Thanks G/R
    6/62
    Trade Feedback
    1 (100%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    lua_tolstring

    Having some issues trying to get this to work.

    Code:
            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
            private delegate string lua_tolstringDelegate(IntPtr pLuaState, int Index, int Length);
    
            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
            private delegate double lua_tonumberDelegate(IntPtr pLuaState, int Index);
    
            private static lua_tolstringDelegate lua_tolstringHandler;
            private static lua_tonumberDelegate lua_tonumberHandler;
    omitted some stuff for simplicity, but all the delegates are registered properly.

    when calling the script from in-game it reaches my callback:

    ex: /script TriggersCallback("test");

    Code:
            private static int CallBackHandler(pIntPtr LuaState)
            {
                int num = lua_gettopHandler(pLuaState);
                if (num >= 1) // atleast 1 arg passed
                {
                    string tmp = lua_tolstringHandler(pLuaState, 1, 0);
                    //tmp should now contain "test" but instead crashes wow.
                }
                return 0;
            }
    it crashes when it attempts to call lua_tolstring.
    the same method, but with tonumber works fine:

    ex: /script TriggersCallback(1234);

    Code:
            private static int CallBackHandler(IntPtr pLuaState)
            {
                int num = lua_gettopHandler(pLuaState);
                if (num >= 1) // atleast 1 arg passed
                {
                    double tmp = lua_tonumberHandler(pLuaState, 1);
                    //tmp now contains "1234" (it actually does).
                }
                return 0;
            }
    This crash also produces a blank crash log from WoW.

    my offsets:

    lua_tolstring = 0xD567F
    lua_tonumber = 0xD55C6

    Anyone know what my issue is?
    Last edited by danwins; 08-05-2013 at 02:13 AM.

    lua_tolstring
  2. #2
    Bananenbrot's Avatar Contributor
    Reputation
    153
    Join Date
    Nov 2009
    Posts
    384
    Thanks G/R
    1/3
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your tolstring delegate is wrong. You need to pass length by reference. So in C# you'd either declare it out or pass an IntPtr. I'd go for the former:
    Code:
    private delegate string lua_tolstringDelegate(IntPtr pLuaState, int Index, out int Length);
    If you are not interested in the length, you can also declare the IntPtr variant and pass IntPtr.Zero.

  3. #3
    danwins's Avatar Contributor
    Reputation
    189
    Join Date
    Mar 2013
    Posts
    143
    Thanks G/R
    6/62
    Trade Feedback
    1 (100%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the point is just to pass a null pointer, if i do it as IntPtr.Zero or uint 0x0 it still crashes.

  4. #4
    SKU's Avatar Contributor
    Reputation
    306
    Join Date
    May 2007
    Posts
    565
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have no idea of managed<->unmanaged operations, but can the CLR just deal with strings like that? Your address seems correct and declaring the last argument as an integer should work as well if you pass 0, assuming it's pushed onto the stack as 'expected'. Have you stepped through the code?

    c# - PInvoke for C function that returns char * - Stack Overflow

  5. #5
    Bananenbrot's Avatar Contributor
    Reputation
    153
    Join Date
    Nov 2009
    Posts
    384
    Thanks G/R
    1/3
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by SKU View Post
    I have no idea of managed<->unmanaged operations, but can the CLR just deal with strings like that? Your address seems correct and declaring the last argument as an integer should work as well if you pass 0, assuming it's pushed onto the stack as 'expected'. Have you stepped through the code?

    c# - PInvoke for C function that returns char * - Stack Overflow
    Right, you have to Marshal.PtrToStringAuto() the IntPtr return value.

  6. #6
    TOM_RUS's Avatar Legendary
    Reputation
    914
    Join Date
    May 2008
    Posts
    699
    Thanks G/R
    0/52
    Trade Feedback
    0 (0%)
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Since pointer returned by lua_tolstring points on UTF-8 encoded string, it's better read it manually. Marshal.PtrToStringXXX doesn't work with UTF-8.

  7. #7
    Bananenbrot's Avatar Contributor
    Reputation
    153
    Join Date
    Nov 2009
    Posts
    384
    Thanks G/R
    1/3
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by TOM_RUS View Post
    Since pointer returned by lua_tolstring points on UTF-8 encoded string, it's better read it manually. Marshal.PtrToStringXXX doesn't work with UTF-8.
    That's helpful... Thanks

All times are GMT -5. The time now is 09:49 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