I have some of unmanaged C++ dynamic library and C# GUI application, using it. I want to pass callback via parameters in some library provided methods. Is it possible to pass a callback to unmanaged C++ method from C#.
// unmanaged C++
typedef uint8_t (__stdcall *SomeCallback)();
MYDLL_API uint8_t someMethod(SomeCallback cb);
I'm trying to use library in this way:
// C# part
public delegate Byte SomeDelegate();
[DllImport("mylibraryname.dll")]
public static extern Byte someMethod(ref SomeDelegate pDelegate);
// actuak callback
Byte myCallback() {
// some code
}
...
// call unmanaged passing callback
static void Main(string[] args) {
someMethod(myCallback);
}
I receive error on compilation:
cannot convert from 'method group' to 'ref SomeDelegate
Am I totally wrong with my approach?