2

I would like to declare / define my delegate and callback function inside of the calling method. Is that possible ? If yes how ? This is my code that I want to execute my first implant operation on:

delegate bool myDelegate(IntPtr module, string type, IntPtr lParam);

public static bool EnumResTypeProc(IntPtr module, string typename, IntPtr lParam)
{
    (((GCHandle) lParam).Target as List<string>).Add(typename);
    return true;
}

public static string[] getResourceTypes(IntPtr module)
{
    List<string> result = new List<string>();
    GCHandle pin = GCHandle.Alloc(result);
    WinApi.EnumResourceTypes(module, Marshal.GetFunctionPointerForDelegate(new myDelegate(EnumResTypeProc)), (IntPtr)pin);
    pin.Free();
    return result.ToArray();
}


The closest I get:

delegate bool myDelegate(IntPtr module, string type, IntPtr lParam);

public static string[] getResourceTypes(IntPtr module)
{
    List<string> result = new List<string>();
    GCHandle pin = GCHandle.Alloc(result);
    myDelegate d = delegate(IntPtr handle, string typename, IntPtr lParam)
        { (((GCHandle) lParam).Target as List<string>).Add(typename); return true; };
    WinApi.EnumResourceTypes(module, Marshal.GetFunctionPointerForDelegate(d), (IntPtr) pin);
    pin.Free();
    return result.ToArray();
}


Declaring the delegate inside a method is not possible at this point. Even if compiled it causes unmanaged code to crash my application.

3 Answers 3

2

Yes, you can use an anonymous method or a lambda expression.

// untested
Func<IntPtr, string, IntPtr, bool> inline = (module, typename, lParam) =>
{
    (((GCHandle)lParam).Target as List<string>).Add(typename);
    return true;
};

WinApi.EnumResourceTypes(module, Marshal.GetFunctionPointerForDelegate(inline), (IntPtr)pin);
Sign up to request clarification or add additional context in comments.

9 Comments

I would avoid the type specification along with parameters. LHS would do it, isnt it?
Looks promising. But doesn't work. First Action must be Func. Then compilation ok, but crashes at the call. Not sure but keyword delegate is needed ?
Well, like I said: untested. You voided the warranty by running it.
@nawfal see original post + edit. Both versions work fine. Trying to declare the delegate inside of my method seems to cause crashes.
@nawfal I'll try it. Maybe important: App crashes in unmanaged code. So I don't get an exception.
|
0

Try to use this links:

microsoft support

How to write callbacks in C# and .NET

Comments

0

Or my example:

public delegate void AsyncMethodCaller(strind inputdata);


 void someMethod()
{
//
// ... some actions 
//
AsyncMethodCaller caller = new AsyncMethodCaller(this.ProcessInputData);

   // Initiate the asychronous call.
   IAsyncResult result = caller.BeginInvoke("my data");
}

void ProcessInputData(string inputData)
{
    // some actions
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.