Original C # and unmanaged--marshaling and automatic marshaling

Source: Internet
Author: User

I've talked about how to generate a C # static reference declaration (C # and unmanaged-first experience) from a C function declaration with a simple lookup substitution, because it's just a simple explanation, so it's all based on basic type matching and automatic marshaling. Automatic marshaling Although we can save a lot of coding time, but if you do not understand the actual behavior behind the automatic marshaling, it is like looking at the Magician's black box, know it does not know why. Also, automatic marshaling is not always a panacea, so here's a record of marshaling-related understanding.

Non-basic types need to be marshaled when they interact with native code in C #, and the general marshaling method has a memory copy, fixed memory address, summarized as follows:

    1. For string, you can use Marshal.stringtohglobalansi, Marshal.stringtohglobalauto, Marshal.stringtohglobaluni the string to an unmanaged function and then calls Marshal.freehglobal to free memory, or use Marshal.ptrtostringansi, Marshal.ptrtostringauto, Marshal.ptrtostringuni Remove the string from the unmanaged function.

    2. for byte[] (or an array of underlying types) can be marshaled to an unmanaged function using marshal.copy from a byte[] copy or marshaled from an unmanaged function copy to byte[]. In addition, marshaling from byte[] to an unmanaged function can also take the original address of a fixed memory directly transmitting byte[] (eliminating the overhead of requesting memory and copying, faster) by using GCHandle and specifying the gchandletype.pinned type.

    3. For delegates, You can use Marshal.getfunctionpointerfordelegate to marshal from delegate to an unmanaged function, or use Marshal.getdelegateforfunctionpointer to remove it from an unmanaged function. As with byte[], you need to ensure that the GC does not reclaim delegates when marshaling to unmanaged functions (you need to keep the references but do not need to pinned).

    4. For structs, you can declare an identical struct in C # in the same order and corresponding data type (using only the underlying type and fixed-length arrays). And mark the LayoutKind.Sequential property of the StructLayout, and then marshal it with Marshal.structuretoptr and Marshal.PtrToStructure.

    5. C # objects cannot be processed in native, can only be saved to be passed back to the C # call, Save as C # with GCHandle Reference object, and then convert GCHandle to IntPtr, to native as a pointer to save.

When C # declares an extern function that returns a type and has a managed type in a parameter type (such as String, byte[], delegate, and so on), it is automatically marshaled, so most of the marshaling code can be saved. However, there are several situations that need to be marshaled manually, such as:

    1. String encoding is non-ANSI non-UTF16 encoding such as UTF8, you must manually marshal and convert the encoding at the same time.
    2. Delegate conversion to function pointers is time consuming, and if there are frequent marshaling calls to the same delegate, the results of the pre-staged conversion can significantly improve performance.

Original C # and unmanaged--marshaling and automatic marshaling

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.