. Net short talk about interoperability (3: Basic knowledge of dllimport features)

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: releasing unmanaged memory with basic knowledge)
    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. in the previous article, we introducedCodeHow to interoperate with the non-hosted code C ++. If you want to successfully make a non-hosted call in the managed code, you need to pay attention to a lot of details. Let's introduce it below, important code declarations for hosting Code in terms of interoperability lay the foundation for the following interoperability;

In. we are lucky to develop in the. NET platform. Microsoft has made a lot of convenient things for us, so we can get started quickly with it by just looking at it; in the interoperability, we only need to use the dllimport feature to get into the unmanaged code, microsoft has handled complicated tasks for us, such as memory allocation, dynamic function search, and address resolution. To do this, you must first use the tool. net is correct. Next we will explain some of the basic knowledge we will use in terms of interoperability, of course, is also a key technical point to step into the interoperability threshold;[Wang qingpei has all rights reserved. For more information, please sign it.]

Dllimport features

First, let's take a look at the dllimport code features. During interoperability, we need to use dllimport to identify this method as a non-hosted code method, during compilation, the compiler can correctly understand that the external code segment marked by this feature can be compiled smoothly.ProgramDuring running, we can also correctly understand that the code references the unmanaged code, so that our CLR can load the unmanaged DLL file and find the entry point for calling; let's take the example in the previous article as an example;

[Dllimport ("win32dll. dll", entrypoint = "add", charset = charset. Auto, callingconvention = callingconvention. stdcall)]
Public static extern int addnumber (int x, int y );

This Code declares an unmanaged code addnumber method. Let's take a look at the specific meanings of these attributes of the dllimport feature. In the dllimport constructor, there is a dllname parameter. See the figure below:

Figure 1:

We can see the annotation that the constructor parameter is the name of the unmanaged DLL, that is, the specific location of the import method we want to use; the parameter in is "win32dll. DLL "string, that is, the non-hosted C ++ file created in the previous article. There are several attributes that we will explain one by one;

Optional entrypoint attribute in the dllimport feature;

Figure 2:

The entrypoint attribute is used to determine the entry point of the unmanaged method. In Figure 1, the entry name of my unmanaged code is "add", that is, a problem occurs, the system recognizes unmanaged code names based on the entrypoint attribute, rather than the managed code name addnumber stated in the C # editor. The system only uses entrypoint to determine the unmanaged entry point, however, we can use arbitrary names to define the name of an unmanaged call. This is often a good method, because we often pay attention to some object-oriented programming methods for hosted code, therefore, naming is also crucial. We cannot create a name without any object meaning at will, so that we can define our own, composite the name of the current context, and have a very good image;[Wang qingpei has all rights reserved. For more information, please sign it.]

The charset optional attribute in the dllimport feature;

Figure 3:

The charset attribute is used to determine the character encoding used to block data in the process of hosting and unmanaged calls, because of our. the net platform uses unicode encoding, while the Standard C ++ uses ANSI encoding. After learning about the encoding method of unmanaged code, we are very sure what encoding to use, if we do not know the language in which the unmanaged code is written or how it is encoded, we can use the auto value in the charset enumeration, let CLR automatically process related details for us;

The callingconvention optional attribute in the dllimport feature;

The callingconvention attribute is also a very important attribute. It plays a role in finding the entry point during the platform call process. When the hosted code performs an unmanaged code entry point search, the call convention of the unmanaged entry point will be confirmed through the value in callingconvention. in the previous article, we mentioned some concepts of the call convention,

Extern "C" _ declspec (dllexport) int _ stdcall add (int x, int y)
{
Return X + Y;
}

This section of the unmanaged C ++ code contains a _ stdcall keyword before this method. This keyword indicates the call convention of the method. Let's take a look at the concept description of _ stdcall: function parameters are passed from right to left through the stack. The called function clears the memory stack of the transfer parameter before returning. This means that the called party cleans up the call stack;

There is also the key code of _ declspec (dllexport), which means to export the method as a callable method, that is, the method that outsiders can call, because not every method of DLL files can be called, it can only be used by the compiler ;;[Wang qingpei has all rights reserved. For more information, please sign it.]

The key code of extern "C" is the name modifier. during compilation, the C compilation option is used to compile this function. Because C and C ++ are different languages, the specific syntax is also different, so we provide a choice in C ++, so that we can choose the compilation method to use, if we use the "C" code to modify the name, the compiler will rename the method by distinguishing different call conventions;

Abstract:

The _ stdcall call Convention adds an underline prefix before the output function name, followed by a "@" symbol and the number of bytes of the parameter. The format is _ functionname @ number. For example: function (int A, int B), whose name is: _ FUNCTION @ 8
The _ cdecl call Convention only adds an underline prefix before the output function name in the format of _ functionname.
_ Fastcall: Add a "@" symbol before the output function name, followed by a "@" symbol and the number of bytes of the parameter. The format is @ functionname @ number.

(Because I am not a C ++ student, I have not been able to summarize a set of understanding ideas. I would like to explain it through the online extraction first)

conclusion: This article focuses on the use of the key features of dllimport. This feature must be used to represent the basic conventions in the P/invoke process, since the implementation of hosting and non-hosting is different, how can we coordinate the calls between the two platforms. net is ready for us; [All rights reserved by Wang qingpei. For details, refer to the signature.]

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.