C# function pointers menu

User Tag List

Results 1 to 9 of 9
  1. #1
    lanman92's Avatar Active Member
    Reputation
    50
    Join Date
    Mar 2007
    Posts
    1,033
    Thanks G/R
    0/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    C# function pointers

    Okay, so I decided to start injecting my code into WoW. I was wondering if there was a way to set a delegate object's address? For example:

    unsafe public void main()
    {
    delegate void condition(bool value);
    condition * con = (condition*)0x1337;
    }

    Can you do this somehow? This is just how I thought it would be =/

    C# function pointers
  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)
    It's possible to do. Just definitely not the way you're trying.

    .NET inherently blocks you from any memory addresses outside of the actual process' own address space. (By 'process' in the .NET world, I'm talking the actual assembly, within the actual AppDomain. Something you don't get by injecting into WoW.)

    More importantly; your managed addresses are within a VM, whereas WoW's aren't. Definitely won't work that way!

    I suggest you do *a lot* of research on what Marshalling is, and the related information.

    If you want to call your managed code (i.e; using a delegate callback) you can do that easily from native->managed code.

    Code:
            [MarshalAs(UnmanagedType.FunctionPtr)]
            private delegate void Foo();
    
    IntPtr delegatePtr = Marshal.GetFunctionPointerForDelegate(new Foo(MyFunctionCallback));
    DllPinvokeCallSomeFunc(delegatePtr);
    Pretty self explanatory. You will need to do your code in C++ or C++/CLI to make it work properly unfortunately.

  3. #3
    lanman92's Avatar Active Member
    Reputation
    50
    Join Date
    Mar 2007
    Posts
    1,033
    Thanks G/R
    0/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow that's gonna be ugly. I might just P/Invoke it. I'm assuming you can't use pointers to variables in C# as well? Sigh.

  4. #4
    Kekke's Avatar Member
    Reputation
    10
    Join Date
    Jan 2009
    Posts
    30
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes you can, by using the keyword unsafe.

    Code:
        unsafe public static int Main()
        {
            int j=100;
            int k=100;
            Console.WriteLine("address of j={0} and address of k={1}",(int)&j,(int)&k);		
            Console.WriteLine("j={0} k={1}",j,k);
            int *p;
            p=&j;
            Console.WriteLine("p now points to {0}",(int)p);
            *p=200;
            Console.WriteLine("j={0} k={1}",j,k);
            p=&k;
            Console.WriteLine("p now points to {0}",(int)p);
            *p=300;
            Console.WriteLine("j={0} k={1}",j,k);
    
            return 0;
        }
    THINK!
    Everything Depends on it

  5. #5
    lanman92's Avatar Active Member
    Reputation
    50
    Join Date
    Mar 2007
    Posts
    1,033
    Thanks G/R
    0/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, thanks for the help. I was just thinking that the runtime might use some wierd type of pointers instead of actual pointers. Never know what to expect =/

    EDIT: On this topic, would using a string * in C# work for reading a games memory? I doubt this is so, but it's worth asking.

  6. #6
    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 Kekke View Post
    Yes you can, by using the keyword unsafe.

    Code:
        unsafe public static int Main()
        {
            int j=100;
            int k=100;
            Console.WriteLine("address of j={0} and address of k={1}",(int)&j,(int)&k);		
            Console.WriteLine("j={0} k={1}",j,k);
            int *p;
            p=&j;
            Console.WriteLine("p now points to {0}",(int)p);
            *p=200;
            Console.WriteLine("j={0} k={1}",j,k);
            p=&k;
            Console.WriteLine("p now points to {0}",(int)p);
            *p=300;
            Console.WriteLine("j={0} k={1}",j,k);
    
            return 0;
        }
    He's not talking about pointers for managed memory.

    This is stolen from the mem editing section due to laziness:

    Code:
    typedef void (__thiscall *tInteract) (void);
    tInteract oInteract;
    
    void Interact(DWORD baseAddress)
    {
    	DWORD_PTR unitInteractVMT = *reinterpret_cast<DWORD_PTR*>(*reinterpret_cast<DWORD_PTR*>(baseAddress) + (38 *4));
    	oInteract= reinterpret_cast<tInteract>(unitInteractVMT );
    	oInteract();
    }
    He's looking for something similar in C#. Would be nice if you could actually assign a delegate to an unmanaged function, but you can't.

    Pseudo code would be like this;

    Code:
    [MarshalAs(UnmanagedType.FunctionPtr)]
    delegate void InteractHandler();
    
    public unsafe void Interact()
    {
    InteractHandler i = (InteractHandler*)<address of func>;
    i.Invoke();
    }
    However; .NET restricts you to only the addresses within your own process. (Even if you host the CLR from within WoW, you can't directly manipulate WoW's memory.)

  7. #7
    lanman92's Avatar Active Member
    Reputation
    50
    Join Date
    Mar 2007
    Posts
    1,033
    Thanks G/R
    0/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So, you're pretty much restricted to P/Invoking things like this? I don't really mind making stubs for each function, but it'll be a little messy.

  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)
    I just found out (and was proven wrong; thanks jjaa) that you actually can use delegates as native function pointers. However, there's a hitch!

    You need to host the CLR in the app you want the delegates to work for. (WoW for example)

    Code:
            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
            public delegate long GetLocalPlayerGuid();
    
            public long Guid()
            {
                var gg = (GetLocalPlayerGuid) Marshal.GetDelegateForFunctionPointer((IntPtr) 0x7BC8D0, typeof(GetLocalPlayerGuid));
                return gg();
            }
    Works only as long as the .NET app is running from within WoW itself.

  9. #9
    lanman92's Avatar Active Member
    Reputation
    50
    Join Date
    Mar 2007
    Posts
    1,033
    Thanks G/R
    0/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Awesome! Can this be done to variables as well?

Similar Threads

  1. C++ Member Function Pointers (Help!)
    By Amrok in forum WoW Memory Editing
    Replies: 2
    Last Post: 10-09-2011, 12:30 PM
  2. Invalid function pointer
    By flo8464 in forum WoW Memory Editing
    Replies: 6
    Last Post: 05-31-2010, 05:28 PM
  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. The typedef keyword. Functions, function pointers, and you.
    By Cypher in forum WoW Memory Editing
    Replies: 16
    Last Post: 01-07-2009, 12:10 AM
  5. 3.0.2 some function/pointer addresses
    By g3gg0 in forum WoW Memory Editing
    Replies: 6
    Last Post: 11-04-2008, 12:23 AM
All times are GMT -5. The time now is 01:56 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