"Inside C #" notes (15) on unmanaged code

Source: Internet
Author: User

For backward compatibility, C # and. NET can run legacy code in an unmanaged way. Unmanaged code means that it is not. NET runtime to control the code. Unmanaged code mainly includes: Platform invoke service (platforminvocation services), unsafe code, COM Interop (COM interoperability).

One platform invoke service

The Platform invoke service (Platform invocation services) is also known as PInvoke, and you can use methods, structs, or even pass callback functions from unmanaged DLLs. Before using an unmanaged DLL, you need to know the parameters and return values of the DLLs ' internal methods beforehand.

A) the basic use method is:

MessageBoxA belongs to the Win32 API, you need to declare a method that is consistent with MessageBoxA signature method, and then DllImport import this DLL, the signature method must be decorated with static extern.

b) The signature method can also be different from the method in the DLL, but the original name needs to be specified in the DllImport entrypoint.

c) use of CharSet

CharSet can specify the character set used by the DLL. For example, the above MessageBoxA actually corresponds to the ANSI encoding, and the corresponding Unicode encoding of the MESSAGEBOXW, in addition to directly specify which to call, you can also use the following wording:

The compiler determines which MessageBox to call based on the type of CharSet. This should require a corresponding match within the DLL, at least to know the corresponding character set for each MessageBox.

d) Callback

Not only can C # code call the DLL's methods, but DLL methods also use C # code in the same way as callbacks.

Here, Printwindow is passed as a callback function to the EnumWindows method in the API.

e) Marshal(arrangement, collation?) )

In the previous example, the method parameters for the MessageBox in the DLL are:

the method signature in C # code does not exactly match it, but it works correctly because the compiler automatically makes default marshal, such as the string type of C # that corresponds to Win32 lpstr. This process can also be done manually, using MarshalAs:

?

If you want to marshal the return value, mark it above the method body.

Two writing unsafe code

The unsafe code here refers to the not being. NET runtime-managed code, memory allocation, deallocation, addressing, and so on are unconstrained, such as the ability to use pointers in C # code, where C # pointers are useful, such as when you need to invoke the C-language API, or when you need to have full control.

A) unsafe and Fixed keywords related to unsafe code

The unsafe keyword is used to inform. NET runtime, the associated code block will not be managed. An unmanaged block of code can be a method, a property, or a code fragment inside a method.

The Fix keyword is used to "pin" (pinning) an object so that the GC does not attempt to reclaim it. However, the address of the object in memory will not be fixed, and the address will still be floating at run time to avoid memory fragmentation. Because the address is not fixed, you should be careful when using pointers.

b) using pointers in C #

Pointers in C # are Special: they can only point to value types, arrays, strings, and if the pointer is to an array, the first element of the array must be a value type, because the pointer is actually pointing to the first element of the array;

pointer-related operators in C # are the same as C, C + +: &, gets the address of an object; *, gets the value of the object;-> Gets the value of a member in the object. A simple example would be:

Before compiling code that is marked unsafe, you need to set the Allow unsafe code in the project properties.

Learning materials: Inside C # by Tom Archer

"Inside C #" notes (15) on unmanaged code

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.