Some time ago, when C # was used to call a DLL, a callback function was defined in C ++ as follows:
Register the callback function registerstreamdirectreadcallback.
Dllexport_api int _ stdcall registerstreamdirectreadcallback (stream_direct_read_callback streamdirectreadcallback, void * context );
The callback function is defined as follows:
Typedef int (* stream_direct_read_callback) (ulong channelnumber, void * databuf, DWORD length, int frametype, void * context );
You need to implement these two functions through marshal in C #. The first registered function is defined as follows:
[Dllimport ( " Ds40xxsdk. dll " )]
Public Static Extern Short Registerstreamdirectreadcallback (stream_direct_read_callback streamdirectreadcallback, intptr context );
The delegate callback function is defined as follows:
Public Unsafe Delegate Short Stream_direct_read_callback ( Uint Channelnumber, Void * Databuf, Uint Length, Int Frametype, Void * Context );
Then the implementation function is defined as follows:
Public Unsafe Short Streamdirectreadcallback ( Uint Channelnumber, Void * Databuf, Uint Length, Int Frametype, Void * Context ){}
The callback is successful at the end of the call. The call is as follows:
Registerstreamdirectreadcallback ( New Stream_direct_read_callback ( This . Streamdirectreadcallback ), New Intptr ( 0 ));
However, when the callback function ends, an error is reported, and noCodeSo there is no way to debug it. It is estimated that it is still a problem of definition. After a long study, it is still not possible. Finally, it is found that some things C # spend too much time, which is not worth the candle, therefore, we finally decided to use C ++ for implementation. But I still want to make it clear. Why?
you have tried intptr or unsafe for void *. In short, it is not possible because void * databuf does not know the size of this parameter, the entry cannot be found. Other callback functions can be directly returned because the parameter is simple and there is only one Int or one DWORD. This marshal should be well studied.