The dll has a structure whose fields are callbacks:

typedef struct callbacks { char(*callback1)(params); void(*callback2)(void); char(*callback3)(params); } Callbacks; 

How to present all this in C #? At the moment I am thinking about IntPtr , but maybe I'm wrong?

  • one
    Well, in Sharpe delegates for this use. - Sergey
  • If dll is yours, then I advise you to pay attention to such technology as COM . - Pavel Mayorov
  • one
    You can also use IntPtr using Marshal.GetDelegateForFunctionPointer ru.stackoverflow.com/questions/585743/… - Serginio
  • one
    About the interaction between Managed and Unmanaged you can see here habrahabr.ru/post/304482 - Serginio

1 answer 1

To present a function pointer in a managed form, you need to use a delegate. The delegate will also have to set the calling agreement using the UnmanagedFunctionPointer :

 [UnmanagedFunctionPointer(CallingConvention.Cdecl)] delegate byte Callback1Type(); 
  • one
    But does the function return strings and not just one character? - Grundy
  • @Grundy By the way, yes, a byte is returned from me, since in Unicode one char takes 2 bytes, and in ancy it is 1. Or can I put a char there? - Aynur Sibagatullin
  • @Grundy yes, I'm blind. Of course, one character. - Pavel Mayorov
  • @PavelMayorov, but can you just use Action or Func? Or do you only need delegates to have a specific attribute? - Grundy
  • one
    @Grundy just Action or Func will marshal like clrcall - Pavel Mayorov