Problem with marshalling native function menu

User Tag List

Results 1 to 6 of 6
  1. #1
    flo8464's Avatar Active Member
    Reputation
    30
    Join Date
    Apr 2009
    Posts
    434
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with marshalling native function

    Hey

    I currently try to marshall a function with this prototype

    Code:
    const char* __thiscall GetName(DWORD thisPointer);
    My attempt was

    Code:
    [UnmanagedFunctionPointer(CallingConvention.ThisCall)]
    private delegate [MarshalAs(UnmanagedType.LPStr)] string GetNameDelegate(uint thisPointer);
    but this obviously doesn't work. Any way to fix this?

    Thanks.
    Hey, it compiles! Ship it!

    Problem with marshalling native function
  2. #2
    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)
    [UnmanagedFunctionPointer(CallingConvention.ThisCall)]
    [return: MarshalAs(UnmanagedType.LPStr)]
    public delegate string GetName(IntPtr instance);

  3. #3
    flo8464's Avatar Active Member
    Reputation
    30
    Join Date
    Apr 2009
    Posts
    434
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you, works just fine
    Hey, it compiles! Ship it!

  4. #4
    flo8464's Avatar Active Member
    Reputation
    30
    Join Date
    Apr 2009
    Posts
    434
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For some strange reason, calling a specific function does just not work. I really can't figure out why. The prototype is 100% correct, the address also.

    (I use your WhiteMagic in that snippet)

    Code:
            #region Player__Interact
            /// <summary>
            /// Interacts with a object
            /// </summary>
            /// <param name="thisPointer">Pointer to the currently active player</param>
            /// <param name="targetPointer">Pointer to a valid object</param>
            /// <returns>Unknown error codes</returns>
            [UnmanagedFunctionPointer(CallingConvention.ThisCall)]
            public delegate int Player__InteractDelegate(uint thisPointer, uint targetPointer);
    
            public static readonly Player__InteractDelegate Player__Interact = Core.magic.RegisterDelegate<Player__InteractDelegate>((uint)StaticData.FunctionAddresses.PLAYER__INTERACT);
            #endregion
    Every time I call it, an exception is thrown:

    ************** Exception Text **************
    System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
    at Synthetic.Internals.EngineFunctions.Player__InteractDelegate.Invoke(UInt32 thisPointer, UInt32 targetPointer)
    at Synthetic.Objects.PlayerObject.Interact(GameObject target) in C:\Users\flo\Desktop\Synthetic\Synthetic\Objects\PlayerObject.cs:line 35
    at Synthetic.Form1.button3_Click(Object sender, EventArgs e) in C:\Users\flo\Desktop\Synthetic\Synthetic\Form1.cs:line 110
    at System.Windows.Forms.Control.OnClick(EventArgs e)
    at System.Windows.Forms.Button.OnClick(EventArgs e)
    at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
    at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
    at System.Windows.Forms.Control.WndProc(Message& m)
    at System.Windows.Forms.ButtonBase.WndProc(Message& m)
    at System.Windows.Forms.Button.WndProc(Message& m)
    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
    at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    I can't figure out why, calling it in my C++ app works perfectly.
    Oh, and I don't really know much about C#, just trying to port some code.

    I already attached a debugger, I never get to know where a problem occurs.

    Thanks
    Flo
    Hey, it compiles! Ship it!

  5. #5
    YetiHunter's Avatar Member
    Reputation
    6
    Join Date
    Aug 2006
    Posts
    57
    Thanks G/R
    0/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok i have no idea if what i'm saying is right, but why the "thispointer" i think it should be something like this:
    Code:
            [UnmanagedFunctionPointer(CallingConvention.ThisCall)]
            private delegate IntPtr InteractDelegate(IntPtr instance);
            private InteractDelegate _interact;
            public virtual void Interact()
            {
                if (_interact == null)
                {
                    _interact = Main.m.RegisterDelegate<InteractDelegate>(GetVFunc(VFunc.Interact));
                }
                _interact(this);
            }

  6. #6
    flo8464's Avatar Active Member
    Reputation
    30
    Join Date
    Apr 2009
    Posts
    434
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, but already found the answer (I am stupid).

    To come back to my previous problem, Apoc's solution tends to make problems. Here a version, which works without flaws, at least in my testings:

    Code:
    [UnmanagedFunctionPointer(CallingConvention.ThisCall)]
    public delegate IntPtr Object__GetNameDelegate(uint thisPointer);
    
    ....
    
            public string Name
            {
                get
                {
                    if (IsValid)
                    {
                        return Marshal.PtrToStringAnsi(Internals.EngineFunctions.Object__GetName(ObjectPointer));
                    }
    
                    return "InvalidObject";
                }
            }
    Hey, it compiles! Ship it!

Similar Threads

  1. annoying problem with IDA - decompiling wrong function
    By NitroGlycerine in forum WoW Memory Editing
    Replies: 3
    Last Post: 08-18-2016, 06:21 PM
  2. Replies: 0
    Last Post: 10-21-2010, 12:58 AM
  3. [Help me]Problem with using function pointer in Debug builds
    By wanyancan in forum WoW Memory Editing
    Replies: 6
    Last Post: 01-06-2010, 05:06 AM
  4. Problems with WoW's PrintChat function
    By MartyT in forum WoW Memory Editing
    Replies: 9
    Last Post: 01-05-2009, 06:42 AM
  5. I have problem with BHW 3.0
    By sunrize1 in forum World of Warcraft General
    Replies: 1
    Last Post: 07-17-2006, 08:49 AM
All times are GMT -5. The time now is 01:59 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