Interoperability-Platform invoke

Source: Internet
Author: User

Interoperability refers to the mutual reference between managed and unmanaged code, which is code that executes under the control of the CLR, and unmanaged code is not controlled by the CLR. Our interoperability with managed and unmanaged code is primarily for the following reasons:

1. Some features C # class library is not implemented or C # Class library encapsulation method does not meet our requirements, but in Microsoft or third-party developed dynamic Link Library has the appropriate method, such as access to Internet Explorer cookie content, usually, We'll call the unmanaged method InternetSetCookie and

InternetGetCookie.

2. When executing managed code, the CLR may lose some of its performance due to the garbage collection mechanism, memory management, security assurance, and so on, and when we are demanding performance requirements, consider using unmanaged code.

3. Interoperability enables you to retain and leverage input in existing unmanaged code

4. Products that can be better compatible with unmanaged code are implemented by unmanaged code and managed code, as compared to the two features of a product, which can be easily implemented if there is a need for contact between the two, such as communication.

For the C # language, there are three ways to achieve the interoperability of managed and unmanaged code.

1. Platform Invoke Service

2. C + + Interoperability

3. COM Interoperability

The following first describes the platform invoke service:

Platform invoke is a service that enables managed code to invoke unmanaged code implemented in a dynamic-link library that locates and invokes an unmanaged function and marshals its parameters (integers, strings, arrays, structures, and so on) across the boundary of an interaction operation as needed.

The specific implementation steps for platform invoke are as follows (image from MSDN):

1. Locate the DLL that contains the function

2. Loading DLLs into memory

3. Get the address of the calling method in memory

4. Marshaling parameters, loading parameters from managed memory into unmanaged memory

5. Load the memory address of the unmanaged method, and execute the charge escrow method.

It is important to note that the address is loaded only the first time the unmanaged method is executed, and the memory address is cached until the program is run.

A winner is a dreamer who never gives up.

Interoperability-Platform invoke

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.