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