An extremely complex marshal

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.