Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointer to function for unmanaged code in C#

I have a dll which accepts a struct that contains a pointer to a function to do a callback.

How can I get an IntPtr to a function of my application to build the struct?

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    public class OPERATION {
        public uint OperationID;
        public IntPtr Context;  
        public IntPtr Callback; -> How to pass this?
    }

Here is the delegate accepting the OPERATION struct

public delegate void MY_CALLBACK([In] OPERATION operation, [In] uint msgId, [In] IntPtr msgDataPtr);
like image 663
Sergi Avatar asked Dec 29 '22 07:12

Sergi


2 Answers

use Marshal.GetFunctionPointerForDelegate

like image 68
John Knoeller Avatar answered Jan 15 '23 05:01

John Knoeller


Maybe the Marshal.GetFunctionPointerForDelegate method may help you.

like image 35
Konamiman Avatar answered Jan 15 '23 03:01

Konamiman