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.