. Net short talk about interoperability (7: Introduction to data sending)

Source: Internet
Author: User
Interoperability Series Article :
    1. . Net short talk about interoperability (1: Introduction At the beginning)
    2. . Net short talk about interoperability (2: Faster first)
    3. . Net short talk about interoperability (3: Basic knowledge of dllimport features)
    4. . Net short talk about interoperability (4: Basic knowledge-based dispose unmanaged memory)
    5. . Net short talk about interoperability (5: Basic knowledge-based dynamic platform call)
    6. . Net short talk about interoperability (6: Improving platform calling performance based on basic knowledge)
    7. . Net short talk about interoperability (7: Introduction to data sending)

We continue to learn about. Net interoperability. The basic knowledge of interoperability is almost complete. Of course, it is difficult for a small article to fully describe all aspects of interoperability. I just want to summarize the key points so that we can enter the door, if you want to learn more deeply in the future, you must have a detailed and comprehensive book. If you want to be proficient in. Net interoperability, you must also learn about unmanaged technologies, such as C ++ and COM. You must be familiar with both. NET and unmanaged technologies to integrate interoperability. Starting from this article, we will enter the data encapsulation phase for. Net interoperability. Data encapsulation is a key part of. Net/pinvoke.CodeData transmission and return are indispensable for non-hosted code interoperability;[Wang qingpei has all rights reserved. For more information, please sign it.]

1:

When the managed code calls the unmanaged code, the data passes through complicated sending. Because the managed data type and the memory structure of the non-managed data type may be different, to pass the managed data parameters to the unmanaged code and successfully accept the unmanaged return values, we need to strictly follow the data types agreed by both parties. For example, char * in C ++ is a character pointer. When we want to pass a string to an unmanaged code, we need to consider how to send the parameter error-free messages to the unmanaged code. The string type in hosting C # Is a reference type, which has the same and different points.

We use. net/pinvoke for WIN32API calls, in most cases, we need to pass a certain structure type to the API, and then accept the return value; in the process of data sending, some concepts we never met at ordinary times .. NET data delivery is intelligent. the CLR mail delivery and splitting tool can automatically select the data delivery method by identifying the data type we pass, for example, if we pass the class type of a class to an unmanaged API, the default class cannot be used for interoperability, and corresponding features must be added for marking, during compilation, the compiler can identify the data type to be sent. When the class type is sent, the compiler will judge the data type, if all internal objects in the class passed to the unmanaged code are platform data types, the CLR will lock the object in the memory and then directly block the memory address to the unmanaged code, the unmanaged code operates the data directly. This is the method of sending a reference address. If the non-platform type that we pass to the unmanaged API is, then, the CLR mail, mail, split, and receiver will copy the hosted type for conversion of the unmanaged type, and then pass the converted data to the unmanaged type, this process is the process of copying data. These two types of interoperable data messages are basically the same. One is data replication and the other is data memory address;

Below we use a diagram to express the principle I mentioned above.

2:

If the hosted data type is equivalent to the non-hosted data type in the memory, the CLR blocking method is very simple. If the hosted data type is not equivalent to the non-hosted data type, the CLR will perform the corresponding copy and conversion operations. Of course, it is possible to lose the data memory leakage, but we don't have to worry ,. NET provides a good interoperability bridge for us. We only need to set a series of settings for the data to be sent, and we can successfully seal the data;

conclusion: This article mainly introduces the concepts related to hosted and unmanaged data mails. Next we will learn about interoperable data mails.

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.