How to call C# delegate from a C++ function that is hooked

Rahul K 51 Reputation points
2020-09-07T05:31:32.65+00:00

What I want to do:
I want to call a C# delegate from a hooked C++ function
I have found links that call C# delegate from C++ functions and it works fine. but I want to call the delegate from a C++ function that is hooked

Steps tried:

  1. declared a delegate in C# (from UWP app).
  2. converted the delegate to function pointer using GetFunctionPointerForDelegate
  3. passed the function pointer to C++
  4. in C++ static library, saved the pointer in a variable
  5. the function in step 3 will hook some method "Method-X" which will be fired when some thread is created.
  6. in this hooked method I am passing the pointer created in step 2 converting it to address and calling the delegate. This gives an access violation
    If the callback function is directly called in step 3 it works fine. Issue is when it’s called from the hooked function.

Note: please note that structure of the app
UWP App ==> WinRT component ==> C++ Library

Please find the below code details .

1. C# code:

//declare a delegate
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate void MyDelegate(int iVal);

//declare vars
public static IntPtr pMyAction;
public static MyDelegate del;

//associate method Sum with the delegate
del = new MyDelegate(Sum);
pMyAction = Marshal.GetFunctionPointerForDelegate<MyDelegate>(del);

//call winrt method
wrt.MethodA(pMyAction.ToInt64());

2. WINRT code:

MethodA(__int64 pActPtr)
cpp.MethodB(pActPtr)

3. C++ code:

void* hPoint;
CallbackFunction ptr;
typedef void(WINAPI *CallbackFunction)(size_t);

MethodB(pActPtr)
hPoint = reinterpret_cast<void*>(pActPtr);
ptr = (CallbackFunction)hPoint;
More code to hook a method MethodH and call HookedFunc eventually

HookedFunc ()
{
ptr(10) -- this should call the Sum method from c# but currently gives AV
}

Developer technologies C++
{count} votes

1 answer

Sort by: Most helpful
  1. Abdulhakim M. Elrhumi 356 Reputation points
    2021-01-07T21:38:17.363+00:00

    Hi

    C# delegate pointing to different methods
    A delegate can point to different methods over time.

    using System;

    namespace DifferentMethods
    {
    public delegate void NameDelegate(string msg);

    public class Person
    {
        public string firstName;
        public string secondName;
    
        public Person(string firstName, string secondName)
        {
            this.firstName = firstName;
            this.secondName = secondName;
        }
    
        public void ShowFirstName(string msg)
        {
            Console.WriteLine(msg + this.firstName);
        }
    
        public void ShowSecondName(string msg)
        {
            Console.WriteLine(msg + this.secondName);
        }
    }
    
    class Program
    {
        public static void Main()
        {
            var per = new Person("Fabius", "Maximus");
    
            var nDelegate = new NameDelegate(per.ShowFirstName);
            nDelegate("Call 1: ");
    
            nDelegate = new NameDelegate(per.ShowSecondName);
            nDelegate("Call 2: ");
        }
    }
    

    }

    This delegate is used to point to two methods of the Person class. The methods are called with the delegate.

    public delegate void NameDelegate(string msg);
    The delegate is created with a delegate keyword. The delegate signature must match the signature of the method being called with the delegate.
    var nDelegate = new NameDelegate(per.ShowFirstName);
    nDelegate("Call 1: ");

    We create an instance of a new delegate that points to the ShowFirstName() method. Later we call the method via the delegate.

    $ dotnet run
    Call 1: Fabius
    Call 2: Maximus

    Best Regards.

    Please click the Mark as answer button and vote as helpful if this reply solves your problem.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.